Re: [PATCH 2/2] bitfield: kunit: test FIELD_FIT() with values that overflow the shift

From: Andy Shevchenko

Date: Fri Dec 04 2026 - 12:26:20 EST


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