Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1218735lqp; Fri, 22 Mar 2024 08:38:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWWTYBtQ7JQ0YS2G1wyDZLiY57j29nY/l00/vRTbi1Nbrh2q+rGnnfHvksFaR5mAhL5OriDpjBYShQAklHSUJmM7OuBTJ03p41+UpO1uQ== X-Google-Smtp-Source: AGHT+IGRmPWDzkEkf1EOB/rlT7Y92hSnyW89MnJZqyROFcNs+f1ZFNQ5H6pHtjOoDidCDIR3HBmn X-Received: by 2002:a05:6a00:3d02:b0:6e7:3d21:e629 with SMTP id lo2-20020a056a003d0200b006e73d21e629mr75171pfb.32.1711121901800; Fri, 22 Mar 2024 08:38:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711121901; cv=pass; d=google.com; s=arc-20160816; b=RZ7szo+JwrDYzOcO1vwrnwlxjS587zKvuU73+vhrT4Ki+Ue6qm4s2LXat+P4LehcUD gLCsMzpBuGrOkRLgVIuZRwpxS3rIO2Xp+jq1ODMWXT7kMm04hYvv7ixTUBzQjghMD93B WxCTjiJEINoQQ6MNdxsmaHWuf6kPCd//rbzmMABoxgR2Nv4WTr4AC9oKuiQP/E/Tarci 7H4Uz/HAw58dU6Fm5kUOYrDZPSsn5VrHGZVLWECNOVXEGQlQes10ASFJIW6WhYvCtRiy e091aOoDHTHy4gVNUumCECsBmX5cTCsv293CPr2PuKryFMxU+R3XS6zRyg30EKJ74xdr iWag== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :message-id:subject:cc:to:from:date:dkim-signature; bh=sx498ZxnHzg9ua0XuRlLSuzXcBbgXEoss8ARW7uvvuY=; fh=Ha/C7YpHv3fiBqBQ1xQW3CsHnqYsHXz7r/971VqgfO4=; b=eq+fLgRJ3jvumAvw0kNvSa17jpOACKj01BZod03rH6zP0V36/J2llO7WF6KdysCPtI yI0moaCn1ODM9RhjuxVEYK6Zva5X6qQZcakc1bkVqIjajj4xe8vE2Zsb3EvriTCE5QUc joiLkRqz1wxiO+vrQAA9klyTGiIkzn+UN30gm1omHePhlBTLRHJX9jBpEfY2KQVr2UxC 2Kaa+GUNC7G5R8fDx85sGHERp3tfs/O529OuANYwYo7pXXZeTi88kfBG6FKePtmJsLCr wjvduJzqUcJ9H8Phl3Y4+IOfAKpA025Ldt9FGpHifdkqzrDikNP3fjMysJp/NZNBGjGf YVMg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=na1dbiZm; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-111706-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111706-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id j25-20020aa783d9000000b006ea78a806f0si2079072pfn.269.2024.03.22.08.38.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 08:38:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-111706-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=na1dbiZm; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-111706-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111706-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 7D9432836F3 for ; Fri, 22 Mar 2024 15:37:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EE213535A6; Fri, 22 Mar 2024 15:37:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="na1dbiZm" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 15B6B524DF; Fri, 22 Mar 2024 15:37:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711121865; cv=none; b=p3BIsiPYhPXNQBSI3PSTju1zkBR5uHCUtYQlGgmdG4PzJckcmslkni9FIpl4hWeFvufYVo2pSQaIFagSQQtoXkwdFTdpf2gpiUxqp7R2ybzjeN2OgwTR0GSXtWuZsYp4u6f1fl0HoxBgdRXG8uWD4eF4k/Sn4Y+DDvKXzbN2fOY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711121865; c=relaxed/simple; bh=GzNKDIuOGjs6k2ffastjq4w0MJc1MWTYP5L+bid8nNs=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=pLlFV5S8+kf7wEHRlbsit2M9IXnxH0AQli1t1KQ8BrHi1ZUqhelTK+OwOQzb+z3nLHxhqZdYbDiDmPuGt4l7SgGaZcB4q2rRqoC4FLCARXkv1/v2KrU0MF06vqPuioGuQbLPdTyKLhOIN52CkdB43gGIWglLNjEWgkSPTQK03KA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=na1dbiZm; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A6846C433C7; Fri, 22 Mar 2024 15:37:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711121864; bh=GzNKDIuOGjs6k2ffastjq4w0MJc1MWTYP5L+bid8nNs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=na1dbiZmzOLr8mIcs9+E2IaW2RUObCivfYvEzZKPG4SFSlvf9aq8eObRqP1IiD7wi IDHtIaNcAMoWlxkFxZOSzpRS2+ZSZQUPoYd3peMdiBa2YeMNNAogI1/B/v/ljLHlGk tjua4Bci0DInz97kRAPhVLM8s2xMOQukUw4UUWIcNK0ByHz6itpkD1UoGxykcwPFRP BiIeWb4OS3zPFWNzTMtX9mnW1FRWibMz3sBaOhM6Z+vCnAVU0VX5ltSWBB+fHkw4rM BxN6LPaYgIzD2wsfyGRTsSeYvl7obc/bNlsa2tohVoeZqxptYrWNVxon8+fpaahzVO bUuy2wue7+X9w== Date: Fri, 22 Mar 2024 15:37:38 +0000 From: Will Deacon To: Petr Tesarik Cc: Christoph Hellwig , Marek Szyprowski , Robin Murphy , Petr Tesarik , Michael Kelley , open list , "open list:DMA MAPPING HELPERS" , Roberto Sassu , Petr Tesarik Subject: Re: [PATCH v3 2/2] bug: introduce ASSERT_VAR_CAN_HOLD() Message-ID: <20240322153738.GC5634@willie-the-truck> References: <20240321171902.85-1-petrtesarik@huaweicloud.com> <20240321171902.85-3-petrtesarik@huaweicloud.com> 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-Disposition: inline In-Reply-To: <20240321171902.85-3-petrtesarik@huaweicloud.com> User-Agent: Mutt/1.10.1 (2018-07-13) 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. Will