From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796413993; b=1TnffgswxRQzL+yd6fIEjLTZ0UEvIpbNSlkEa0YjaMFucaoguKW5RqWOk91fr3mmnhaU XldkNC2m2Hnm1MGp1aLMiVjn1FpS7PaPeD4Y2RlL+1iTAvflPXtdyleGWJWiaAaSDMddy lVjB1pRHQDnmiikuRaH6fPj+GuorFKoPcHfY1gLeWX9ajEqR/SE71Zo2TNTmV6aWT87EG Rel1+IN+l2GwxvTZjYaulZGPQ5rpcDy+sWZITw3u6bobG7pjktiJB8ZWYCUmnNpLCGmgi qWMIFDRel9YMPFXvXi+6DpixjWSoeQA1dVdmE5HXaURw2FkvEv7RBu92BqC3kmEgTfQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796413993; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: In-Reply-To:References:X-Mailer:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=h1XsNwD3O0IWicBsESMi1dvPuIzIwPNZl3xt90qk6pY=; b=OIEYytxkvlb6TTTNCEQgcfMhAFdX01xlR0TsK041Subs8w2cf0X+T5OdT7S6kce8k9t0 gkgNM4sylXhHo3fcGEP0MlDVM+qckIXVuoX1gW9eLu/pNEpSTH8uBbow6ARKKYm0lqZnT uOHcnENsKl01vmGYwFqkx/govMvBCGJv7tpeBz9+zrV0vmrlJ/hy8OSVpb2BilwSqRYx+ duYjI+cUi2U4PUnTprvi24LQ7LyRfrLTt7gHBZ3DPjN9agIZiBergeDmiEdYvGQkCQhQt OewfPIxMV3QOuukrJvdMh+SL8u/Rd5sgP2Djt5k1WmfdHzxWrKI51kj+WB3Dws/sUDA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=PIM1ZTz+; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EA11D606A32D for ; Fri, 4 Dec 2026 19:53:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=PIM1ZTz+ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F5A41F00936; Fri, 4 Dec 2026 19:53:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796413992; bh=h1XsNwD3O0IWicBsESMi1dvPuIzIwPNZl3xt90qk6pY=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=PIM1ZTz+f8sMONHHUBP8VITCbO48sfDIh4wO73FzGm/sk9MYXR2nmnK1UnRrQKo3A r6D2uV9N38ZY+d2gvX3GqA0dmxQhmuiKlnkgNz9veHg4ReJpbZt3lc7f9TbhWKwjUh XHlKsD7qvAk8bPTw68pbe3K5TOfvwun/zg3zcnSHOASNsyLilVvI23ZhDwPsdRaGYK qHFupff8Yiv/Lo2x/015ZdEUfh3f7uAmZG59qn07fxX5tkVITzPrZMFseJJyT4IkJ2 gJj7pEbCwtFYIFwUZ1hAqMZ1TVvKeIKVHBh0eBqieSXWyaEU9o9wrdKwy2aIXfhAMu ltlTLzcczkQ4w== Date: Fri, 4 Dec 2026 19:53:07 +0000 From: Jonathan Cameron To: Andy Shevchenko Cc: Arnav Kapoor , Yury Norov , Rasmus Villemoes , linux-kernel@vger.kernel.org Subject: Re: [PATCH 0/2] bitfield: fix FIELD_FIT() for values that overflow the shift Message-ID: <20261204195307.6ba97beb@jic23-hlaptop> In-Reply-To: References: <20261204163317.924658-1-kapoorarnav43@gmail.com> X-Mailer: Claws Mail 4.4.0 (GTK 3.24.52; x86_64-pc-linux-gnu) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Fri, 4 Dec 2026 19:27:40 +0200 Andy Shevchenko wrote: > On Fri, Dec 04, 2026 at 10:03:10PM +0530, Arnav Kapoor wrote: > > FIELD_FIT() shifts the value up into the field before checking it, so > > high bits that fall off the top of the mask's type are lost and a value > > that is too big passes. On 32-bit, FIELD_FIT(GENMASK(9, 8), BIT(24)) and > > FIELD_FIT(GENMASK(9, 8), INT_MIN) are true; on 64-bit the same happens > > with a u32 mask. This came up in the aw96103 v2 review on linux-iio, > > where Jonathan suggested shifting the mask down instead and asked > > whether the compiler still produces good code. > > > > 1: shift the mask down and compare, as FIELD_MAX() does. > > 2: kunit cases that fail before patch 1 (on um and i386) and pass after. > > > > Code generation, FIELD_FIT(GENMASK(9, 8), int): > > - gcc -m32 -O2: cmpl $3 + setbe (was a test against 0x00fffffc, which > > is the bug) > > - clang ARMv7 -O2: cmp #4 + movwlo > > - gcc x86-64 -O2: one instruction shorter than before > > > > Not changed: the value is still cast to the mask's type first, so with > > a u8 or u16 mask a wider value is still truncated (FIELD_FIT((u8)0xf0, > > 0x100) is true). Casting to u64 instead fixes that as well, but gcc -m32 > > then emits 12 instructions instead of 4 for an int value, so I left it > > out; happy to add it if you prefer. > > > > Tested with kunit (bitfields suite) on um and i386, and W=1 builds of > > every non-arch FIELD_FIT() user (allmodconfig, x86_64) with no new > > warnings. > > I like the series and I was attending the discussion in the mailing list when > this bug was discovered. > > Reviewed-by: Andy Shevchenko > Looks good to me as well. Reviewed-by: Jonathan Cameron