mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Arnav Kapoor <kapoorarnav43@gmail.com>
To: Yury Norov <yury.norov@gmail.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Jonathan Cameron <jic23@kernel.org>,
	Andy Shevchenko <andriy.shevchenko@intel.com>,
	linux-kernel@vger.kernel.org,
	Arnav Kapoor <kapoorarnav43@gmail.com>
Subject: [PATCH 1/2] bitfield: make FIELD_FIT() check the value, not the shifted value
Date: Fri,  4 Dec 2026 22:03:11 +0530	[thread overview]
Message-ID: <20261204163317.924658-2-kapoorarnav43@gmail.com> (raw)
In-Reply-To: <20261204163317.924658-1-kapoorarnav43@gmail.com>

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


  reply	other threads:[~2026-12-04 16:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-12-04 16:33 ` [PATCH 2/2] bitfield: kunit: test FIELD_FIT() with " Arnav Kapoor
2026-12-04 17:26   ` Andy Shevchenko
2026-12-05 19:43     ` Yury Norov
2026-12-06 11:09       ` 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204163317.924658-2-kapoorarnav43@gmail.com \
    --to=kapoorarnav43@gmail.com \
    --cc=andriy.shevchenko@intel.com \
    --cc=jic23@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®