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 2/2] bitfield: kunit: test FIELD_FIT() with values that overflow the shift
Date: Fri, 4 Dec 2026 22:03:12 +0530 [thread overview]
Message-ID: <20261204163317.924658-3-kapoorarnav43@gmail.com> (raw)
In-Reply-To: <20261204163317.924658-1-kapoorarnav43@gmail.com>
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
next prev parent 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 " 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 [this message]
2026-12-04 17:26 ` [PATCH 2/2] bitfield: kunit: test FIELD_FIT() with values that overflow the shift 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-3-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®