Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1295728lqp; Fri, 22 Mar 2024 10:33:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUlLqENF9K1tzfU3UqMUnzo8+9LfuQR1hKurhN47TxdgRnG3xf7ISo6cFLnPDGIz8ydQLDzY051oSRzWxnSitWGEuG/i13UGPongsAuLw== X-Google-Smtp-Source: AGHT+IEQ41MF5kFnzXRuhmBglyb3DtYKU5dF16AZrgT4nDtjL1+fsVEgONssSSLvqVoiKzn2DVhJ X-Received: by 2002:a17:907:6d15:b0:a47:3f10:b3c8 with SMTP id sa21-20020a1709076d1500b00a473f10b3c8mr134040ejc.26.1711128814872; Fri, 22 Mar 2024 10:33:34 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711128814; cv=pass; d=google.com; s=arc-20160816; b=u3T/qle8FbJihU+aPGqFPkmc2Uvf005T0AaAZA30R6wz8pW7Z3xCBRNPlpvBaPa9Gb lj5jhduXUTe19g1bzZ70ypfyPKOOfdclioV4XjS4tIhY1hXCa8IC6uG+UDdmIw00xw7c NWTzM0nIPDsvYngRJQavvSK9cCZ9aiyDHNN3TN16nmNCnc3tWKe0cmgxwx0INYiragBa X+AzkqJVnz+nnzjkxXSTuyFGoHcx5XwJaRzwAdNVonP0URRhpBJvkZtbIV4NkFyVcu6H IeXiDDo2pqJckr12nNdpSvPTvjX7W734XR1MmU6Gt8SCUKD/nU7hC9XJeYph7cyMXv30 wl8w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=uSujtAeKaM3ubzxeNWEHiD7u5zka7YhzUm+SfYHNOq4=; fh=QHy/abdxAvqUcdaSLXzWcfQH/hJ3sh+RPymszvFiAA8=; b=x3gSR3gJ5eGvPDg7cYVXGOXbf76O4hfJPKqhi82MGRDxoRxES9nil73kmEfTrLHK+M /1SwrAhBzeX8ufNFrFWTGqDMoakC6vNoNZG0ALBXuXSC/mJjeiKhr2Imoy4oYEnwrZ4k 1jMjwPSIbozKF7Tevu6hUnfHY2nYHjQgEPCTq/GEVFH1cdppsAbkdKAOVK1NbFUJrTyy oaqRDACliKJz3mJxkz7HXw4+M38lfN5Y7zF8vXwnY1fCbS8v1mv26IfVr19XwEuOig6/ uH007vok37/x4WJZpmDs1B/1koFipWKmNXca14QDtjPiJSBoEmBI+izqcmzxZitwwbM1 9nNg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@tesarici.cz header.s=mail header.b=jdYGucEV; arc=pass (i=1 spf=pass spfdomain=tesarici.cz dkim=pass dkdomain=tesarici.cz dmarc=pass fromdomain=tesarici.cz); spf=pass (google.com: domain of linux-kernel+bounces-111868-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111868-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=tesarici.cz Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id n20-20020a1709062bd400b00a46bf6e2fa0si39897ejg.203.2024.03.22.10.33.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 10:33:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-111868-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@tesarici.cz header.s=mail header.b=jdYGucEV; arc=pass (i=1 spf=pass spfdomain=tesarici.cz dkim=pass dkdomain=tesarici.cz dmarc=pass fromdomain=tesarici.cz); spf=pass (google.com: domain of linux-kernel+bounces-111868-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111868-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=tesarici.cz Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 9835E1F23632 for ; Fri, 22 Mar 2024 17:33:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 74ADA5FBB3; Fri, 22 Mar 2024 17:33:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tesarici.cz header.i=@tesarici.cz header.b="jdYGucEV" Received: from bee.tesarici.cz (bee.tesarici.cz [37.205.15.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E25D47793 for ; Fri, 22 Mar 2024 17:33:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=37.205.15.56 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711128809; cv=none; b=WRId1cNvYmfSkx8aimB4QxPEcRzI+RIy1eaunUZ8DqZyoQWpwaV2AUyP+CUXy0wabBOZTCIKi1P/aiN8P2isaDSdBcbdNIWqCL4lc2W7l9R6ccCD8CCNT6pQ7y3usj45M+Ib524fZazvG6IbuizGNDQoTI17zMbgdcoeYh/k8Ro= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711128809; c=relaxed/simple; bh=Yq6TPIOySs5SbkTECG8fTC9rHi3vyaMmLcUMisKVIDY=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=A7GaP+tq4vqPL87zH/KrErkyUdlTi/0NMwxeRk6xD1i/0siEsqkHP0guKU6WJH71eroQishi7LQqKmzNbSnIwjNnmG0tGE7J1yczdL7T1zaoITY4FOVRlZydM59z4PXMW2LxDqhWSZb/ZgiboXNjhdEramVSVgZheQr+44PnOr8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tesarici.cz; spf=pass smtp.mailfrom=tesarici.cz; dkim=pass (2048-bit key) header.d=tesarici.cz header.i=@tesarici.cz header.b=jdYGucEV; arc=none smtp.client-ip=37.205.15.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tesarici.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tesarici.cz Received: from meshulam.tesarici.cz (dynamic-2a00-1028-83b8-1e7a-b985-910f-39e1-703f.ipv6.o2.cz [IPv6:2a00:1028:83b8:1e7a:b985:910f:39e1:703f]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bee.tesarici.cz (Postfix) with ESMTPSA id B25D71AA455; Fri, 22 Mar 2024 18:33:16 +0100 (CET) Authentication-Results: mail.tesarici.cz; dmarc=fail (p=quarantine dis=none) header.from=tesarici.cz DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tesarici.cz; s=mail; t=1711128796; bh=uSujtAeKaM3ubzxeNWEHiD7u5zka7YhzUm+SfYHNOq4=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=jdYGucEVaESbR0fs1Ior9j0PrsOWd3/gK8UJHLlP/YuvCgjL/L3Yol+RFfoh4ECyq MFs8QxbkGQRCFZsbo1rMYejOwzUmzIukrhUVr6bV7E29oaXKRgZdu4o7gjVS+7x077 I0vJYRoyo1NEt6GrWM7giiZr5yNIgtaQXGF2qHMDOT9qnMx/3tprgDw3lkaljVcYU2 e7YxG9ehmgRTtQRyljUekEyMuNe1XOhjqRR4KbY1Fs83pNBltviMhrCbEdaS5L5CYi r1YMjqfZvPQZI+tj/eloS/zk8sbfcr+tAh0CFxrp/aXWopaF36oOXOKvu8XjLRnHGr sZKxeTaedaplA== Date: Fri, 22 Mar 2024 18:33:15 +0100 From: Petr =?UTF-8?B?VGVzYcWZw61r?= To: Will Deacon Cc: Christoph Hellwig , Marek Szyprowski , Robin Murphy , Petr Tesarik , Michael Kelley , open list , "open list:DMA MAPPING HELPERS" , Roberto Sassu Subject: Re: [PATCH v3 2/2] bug: introduce ASSERT_VAR_CAN_HOLD() Message-ID: <20240322183302.197c9b5f@meshulam.tesarici.cz> In-Reply-To: <20240322153738.GC5634@willie-the-truck> References: <20240321171902.85-1-petrtesarik@huaweicloud.com> <20240321171902.85-3-petrtesarik@huaweicloud.com> <20240322153738.GC5634@willie-the-truck> X-Mailer: Claws Mail 4.2.0 (GTK 3.24.39; x86_64-suse-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 22 Mar 2024 15:37:38 +0000 Will Deacon wrote: > On Thu, Mar 21, 2024 at 06:19:02PM +0100, Petr Tesarik wrote: > > From: Petr Tesarik > > > > Introduce an ASSERT_VAR_CAN_HOLD() macro to check at build time that a > > variable can hold the given value. > > > > Use this macro in swiotlb to make sure that the list and pad_slots fields > > of struct io_tlb_slot are big enough to hold the maximum possible value of > > IO_TLB_SEGSIZE. > > > > Signed-off-by: Petr Tesarik > > --- > > include/linux/build_bug.h | 10 ++++++++++ > > kernel/dma/swiotlb.c | 2 ++ > > 2 files changed, 12 insertions(+) > > > > diff --git a/include/linux/build_bug.h b/include/linux/build_bug.h > > index 3aa3640f8c18..6e2486508af0 100644 > > --- a/include/linux/build_bug.h > > +++ b/include/linux/build_bug.h > > @@ -86,4 +86,14 @@ > > "Offset of " #field " in " #type " has changed.") > > > > > > +/* > > + * Compile time check that a variable can hold the given value > > + */ > > +#define ASSERT_VAR_CAN_HOLD(var, value) ({ \ > > + typeof(value) __val = (value); \ > > + typeof(var) __tmp = __val; \ > > + BUILD_BUG_ON_MSG(__tmp != __val, \ > > + #var " cannot hold " #value "."); \ > > +}) > > nit, but I think this prevents putting negative values into unsigned > types. Not sure whether we care? Arguably it's even correct to complain. > > e.g. > > u16 s; > ASSERT_VAR_CAN_HOLD(s, -1); > > explodes for me. Then it works as intended. I specifically aimed at making a macro that checks at build time whether a given constant is within the value range of a variable, so it explodes for a signed overflow (in either direction). To check the size of a variable, I could have gone with something simpler like BUG_ON(sizeof(var) < sizeof(val)). Petr T