The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH 0/2] bitfield: fix FIELD_FIT() for values that overflow the shift
@ 2026-12-04 16:33 Arnav Kapoor
  2026-12-04 16:33 ` [PATCH 1/2] bitfield: make FIELD_FIT() check the value, not the shifted value Arnav Kapoor
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Arnav Kapoor @ 2026-12-04 16:33 UTC (permalink / raw)
  To: Yury Norov
  Cc: Rasmus Villemoes, Jonathan Cameron, Andy Shevchenko, linux-kernel,
	Arnav Kapoor

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.

Arnav Kapoor (2):
  bitfield: make FIELD_FIT() check the value, not the shifted value
  bitfield: kunit: test FIELD_FIT() with values that overflow the shift

 include/linux/bitfield.h   |  2 +-
 lib/tests/bitfield_kunit.c | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)


base-commit: 209e46e8890d3618ed883eaf816625d9f22df52f
-- 
2.53.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/2] bitfield: make FIELD_FIT() check the value, not the shifted value
  2026-12-04 16:33 [PATCH 0/2] bitfield: fix FIELD_FIT() for values that overflow the shift Arnav Kapoor
@ 2026-12-04 16:33 ` Arnav Kapoor
  2026-12-04 16:33 ` [PATCH 2/2] bitfield: kunit: test FIELD_FIT() with values that overflow the shift Arnav Kapoor
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Arnav Kapoor @ 2026-12-04 16:33 UTC (permalink / raw)
  To: Yury Norov
  Cc: Rasmus Villemoes, Jonathan Cameron, Andy Shevchenko, linux-kernel,
	Arnav Kapoor

FIELD_FIT() casts the value to the mask's type, shifts it up into the
field and checks for bits outside the mask. Bits that the shift moves
past the top of the type are lost, so a value that is too big can pass.
On 32-bit, FIELD_FIT(GENMASK(9, 8), val) is true for val = BIT(24) and
for val = INT_MIN, and FIELD_FIT(GENMASK(31, 28), val) is true for every
val, since only the low four bits survive the shift. The same happens on
64-bit with a 32-bit mask such as (u32)GENMASK(9, 8). For a signed mask
type the shift can also overflow, which is undefined behaviour.

Shift the mask down instead and check the value against it, as
FIELD_MAX() does. Nothing is shifted out, so every value that is
outside the field after the cast to the mask's type is now rejected.
Negative values still fail, as before, because the cast turns them into
large unsigned values.

The result is a plain compare against FIELD_MAX(): for
FIELD_FIT(GENMASK(9, 8), int) gcc -m32 -O2 emits cmpl $3 + setbe, where
it used to emit a test against 0x00fffffc, and clang for ARMv7 a single
cmp, so the code is not worse.

Suggested-by: Jonathan Cameron <jic23@kernel.org>
Fixes: 1697599ee301 ("bitfield.h: add FIELD_FIT() helper")
Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
 include/linux/bitfield.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index e3f7315c1d53..773874724b8e 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -121,7 +121,7 @@
 #define FIELD_FIT(_mask, _val)						\
 	({								\
 		__BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: ");	\
-		!((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \
+		!(((typeof(_mask))(_val)) & ~((_mask) >> __bf_shf(_mask))); \
 	})
 
 /**
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/2] bitfield: kunit: test FIELD_FIT() with values that overflow the shift
  2026-12-04 16:33 [PATCH 0/2] bitfield: fix FIELD_FIT() for values that overflow the shift Arnav Kapoor
  2026-12-04 16:33 ` [PATCH 1/2] bitfield: make FIELD_FIT() check the value, not the shifted value Arnav Kapoor
@ 2026-12-04 16:33 ` Arnav Kapoor
  2026-12-04 17:26   ` Andy Shevchenko
  2026-12-04 17:27 ` [PATCH 0/2] bitfield: fix FIELD_FIT() for " Andy Shevchenko
  2026-12-05  4:52 ` Yury Norov
  3 siblings, 1 reply; 7+ messages in thread
From: Arnav Kapoor @ 2026-12-04 16:33 UTC (permalink / raw)
  To: Yury Norov
  Cc: Rasmus Villemoes, Jonathan Cameron, Andy Shevchenko, linux-kernel,
	Arnav Kapoor

Check that FIELD_FIT() rejects values that are only too big once they
are shifted into the field: BIT(24) and S32_MIN against a 2-bit field at
bit 8, as a u32 mask and as GENMASK() (an unsigned long, so 32 bits on
32-bit architectures), and BIT(24) against a field at the top of a u32.
The old implementation shifted their high bits out and accepted them.

Also cover the plain cases: the largest value that fits, the next one,
and -1.

Signed-off-by: Arnav Kapoor <kapoorarnav43@gmail.com>
---
 lib/tests/bitfield_kunit.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/lib/tests/bitfield_kunit.c b/lib/tests/bitfield_kunit.c
index 5ccd86f61896..3fdc8f997ddb 100644
--- a/lib/tests/bitfield_kunit.c
+++ b/lib/tests/bitfield_kunit.c
@@ -125,6 +125,30 @@ static void __init test_bitfields_variables(struct kunit *context)
 	CHECK(u64, 0x0000001f8000000ull);
 }
 
+#define CHECK_FIT(mask, val, res) do {					\
+		bool fit = FIELD_FIT(mask, val);			\
+									\
+		KUNIT_EXPECT_EQ_MSG(context, fit, res,			\
+				    "FIELD_FIT(%s, %s)", #mask, #val);	\
+	} while (0)
+
+static void __init test_bitfields_fit(struct kunit *context)
+{
+	s32 big = BIT(24), neg = -1, min = S32_MIN;
+	u32 three = 3, four = 4;
+
+	CHECK_FIT((u32)0x00000300, three, true);
+	CHECK_FIT((u32)0x00000300, four, false);
+	CHECK_FIT((u32)0x00000300, neg, false);
+	/* These must not pass by having their high bits shifted out */
+	CHECK_FIT((u32)0x00000300, big, false);
+	CHECK_FIT((u32)0x00000300, min, false);
+	CHECK_FIT((u32)0xf0000000, big, false);
+
+	CHECK_FIT(GENMASK(9, 8), big, false);
+	CHECK_FIT(GENMASK(9, 8), min, false);
+}
+
 #ifdef TEST_BITFIELD_COMPILE
 static void __init test_bitfields_compile(struct kunit *context)
 {
@@ -140,6 +164,7 @@ static void __init test_bitfields_compile(struct kunit *context)
 static struct kunit_case __refdata bitfields_test_cases[] = {
 	KUNIT_CASE(test_bitfields_constants),
 	KUNIT_CASE(test_bitfields_variables),
+	KUNIT_CASE(test_bitfields_fit),
 	{}
 };
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/2] bitfield: kunit: test FIELD_FIT() with values that overflow the shift
  2026-12-04 16:33 ` [PATCH 2/2] bitfield: kunit: test FIELD_FIT() with values that overflow the shift Arnav Kapoor
@ 2026-12-04 17:26   ` Andy Shevchenko
  0 siblings, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2026-12-04 17:26 UTC (permalink / raw)
  To: Arnav Kapoor; +Cc: Yury Norov, Rasmus Villemoes, Jonathan Cameron, linux-kernel

On Fri, Dec 04, 2026 at 10:03:12PM +0530, Arnav Kapoor wrote:
> Check that FIELD_FIT() rejects values that are only too big once they
> are shifted into the field: BIT(24) and S32_MIN against a 2-bit field at
> bit 8, as a u32 mask and as GENMASK() (an unsigned long, so 32 bits on
> 32-bit architectures), and BIT(24) against a field at the top of a u32.
> The old implementation shifted their high bits out and accepted them.
> 
> Also cover the plain cases: the largest value that fits, the next one,
> and -1.

...

> +#define CHECK_FIT(mask, val, res) do {					\
> +		bool fit = FIELD_FIT(mask, val);			\
> +									\
> +		KUNIT_EXPECT_EQ_MSG(context, fit, res,			\
> +				    "FIELD_FIT(%s, %s)", #mask, #val);	\
> +	} while (0)
> +
> +static void __init test_bitfields_fit(struct kunit *context)
> +{
> +	s32 big = BIT(24), neg = -1, min = S32_MIN;
> +	u32 three = 3, four = 4;
> +
> +	CHECK_FIT((u32)0x00000300, three, true);
> +	CHECK_FIT((u32)0x00000300, four, false);
> +	CHECK_FIT((u32)0x00000300, neg, false);
> +	/* These must not pass by having their high bits shifted out */
> +	CHECK_FIT((u32)0x00000300, big, false);
> +	CHECK_FIT((u32)0x00000300, min, false);
> +	CHECK_FIT((u32)0xf0000000, big, false);
> +
> +	CHECK_FIT(GENMASK(9, 8), big, false);
> +	CHECK_FIT(GENMASK(9, 8), min, false);
> +}

#undef CHECK_FIT

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] bitfield: fix FIELD_FIT() for values that overflow the shift
  2026-12-04 16:33 [PATCH 0/2] bitfield: fix FIELD_FIT() for values that overflow the shift Arnav Kapoor
  2026-12-04 16:33 ` [PATCH 1/2] bitfield: make FIELD_FIT() check the value, not the shifted value Arnav Kapoor
  2026-12-04 16:33 ` [PATCH 2/2] bitfield: kunit: test FIELD_FIT() with values that overflow the shift Arnav Kapoor
@ 2026-12-04 17:27 ` Andy Shevchenko
  2026-12-04 19:53   ` Jonathan Cameron
  2026-12-05  4:52 ` Yury Norov
  3 siblings, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2026-12-04 17:27 UTC (permalink / raw)
  To: Arnav Kapoor; +Cc: Yury Norov, Rasmus Villemoes, Jonathan Cameron, linux-kernel

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 <andriy.shevchenko@intel.com>

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] bitfield: fix FIELD_FIT() for values that overflow the shift
  2026-12-04 17:27 ` [PATCH 0/2] bitfield: fix FIELD_FIT() for " Andy Shevchenko
@ 2026-12-04 19:53   ` Jonathan Cameron
  0 siblings, 0 replies; 7+ messages in thread
From: Jonathan Cameron @ 2026-12-04 19:53 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: Arnav Kapoor, Yury Norov, Rasmus Villemoes, linux-kernel

On Fri, 4 Dec 2026 19:27:40 +0200
Andy Shevchenko <andriy.shevchenko@intel.com> 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 <andriy.shevchenko@intel.com>
> 

Looks good to me as well. 

Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/2] bitfield: fix FIELD_FIT() for values that overflow the shift
  2026-12-04 16:33 [PATCH 0/2] bitfield: fix FIELD_FIT() for values that overflow the shift Arnav Kapoor
                   ` (2 preceding siblings ...)
  2026-12-04 17:27 ` [PATCH 0/2] bitfield: fix FIELD_FIT() for " Andy Shevchenko
@ 2026-12-05  4:52 ` Yury Norov
  3 siblings, 0 replies; 7+ messages in thread
From: Yury Norov @ 2026-12-05  4:52 UTC (permalink / raw)
  To: Arnav Kapoor
  Cc: Yury Norov, Rasmus Villemoes, Jonathan Cameron, Andy Shevchenko,
	linux-kernel

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.

Added in bitmap-for-next, thanks!
 
> Arnav Kapoor (2):
>   bitfield: make FIELD_FIT() check the value, not the shifted value
>   bitfield: kunit: test FIELD_FIT() with values that overflow the shift
> 
>  include/linux/bitfield.h   |  2 +-
>  lib/tests/bitfield_kunit.c | 25 +++++++++++++++++++++++++
>  2 files changed, 26 insertions(+), 1 deletion(-)
> 
> 
> base-commit: 209e46e8890d3618ed883eaf816625d9f22df52f
> -- 
> 2.53.0

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-12-05  4:52 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 16:33 [PATCH 0/2] bitfield: fix FIELD_FIT() for values that overflow the shift Arnav Kapoor
2026-12-04 16:33 ` [PATCH 1/2] bitfield: make FIELD_FIT() check the value, not the shifted value Arnav Kapoor
2026-12-04 16:33 ` [PATCH 2/2] bitfield: kunit: test FIELD_FIT() with values that overflow the shift Arnav Kapoor
2026-12-04 17:26   ` Andy Shevchenko
2026-12-04 17:27 ` [PATCH 0/2] bitfield: fix FIELD_FIT() for " Andy Shevchenko
2026-12-04 19:53   ` Jonathan Cameron
2026-12-05  4:52 ` Yury Norov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox