RE: [PATCH v6 12/26] bitfield: Add less-checking __FIELD_{GET,PREP}()

Ping-Ke Shih
2025-11-07T01:19:53Z

Geert Uytterhoeven <geert@linux-m68k.org> wrote:
> Hi Andy,
>
> On Thu, 6 Nov 2025 at 17:09, Andy Shevchenko
> <andriy.shevchenko@intel.com> wrote:
> > On Thu, Nov 06, 2025 at 03:49:03PM +0100, Geert Uytterhoeven wrote:
> > > On Thu, 6 Nov 2025 at 15:44, Andy Shevchenko
> > > <andriy.shevchenko@intel.com> wrote:
> > > > On Thu, Nov 06, 2025 at 02:34:00PM +0100, Geert Uytterhoeven wrote:
> > > > > The BUILD_BUG_ON_MSG() check against "~0ull" works only with "unsigned
> > > > > (long) long" _mask types. For constant masks, that condition is usually
> > > > > met, as GENMASK() yields an UL value. The few places where the
> > > > > constant mask is stored in an intermediate variable were fixed by
> > > > > changing the variable type to u64 (see e.g. [1] and [2]).
> > > > >
> > > > > However, for non-constant masks, smaller unsigned types should be valid,
> > > > > too, but currently lead to "result of comparison of constant
> > > > > 18446744073709551615 with expression of type ... is always
> > > > > false"-warnings with clang and W=1.
> > > > >
> > > > > Hence refactor the __BF_FIELD_CHECK() helper, and factor out
> > > > > __FIELD_{GET,PREP}(). The later lack the single problematic check, but
> > > > > are otherwise identical to FIELD_{GET,PREP}(), and are intended to be
> > > > > used in the fully non-const variants later.
>
> > > > > + BUILD_BUG_ON_MSG(__bf_cast_unsigned(mask, mask) > \
> > > > > + __bf_cast_unsigned(reg, ~0ull), \
> > > > > + pfx "type of reg too small for mask")
> > > >
> > > > Perhaps we may convert this (and others?) to static_assert():s at some point?
> > >
> > > Nick tried that before, without success:
> > > https://lore.kernel.org/all/CAKwvOdm_prtk1UQNJQGidZm44Lk582S3p=of0y46+rVjnSgXJg@mail.gmail.com
> >
> > Ah, this is unfortunate.
>
> Of course, it might be an actual bug in the i915 driver...
>
> The extra checking in field_prep() in case the compiler can
> determine that the mask is a constant already found a possible bug
> in drivers/net/wireless/realtek/rtw89/core.c:rtw89_roc_end():
>
> rtw89_write32_mask(rtwdev, reg, B_AX_RX_FLTR_CFG_MASK, rtwdev->hal.rx_fltr);
>
> drivers/net/wireless/realtek/rtw89/reg.h:
>
> #define B_AX_RX_MPDU_MAX_LEN_MASK GENMASK(21, 16)
> #define B_AX_RX_FLTR_CFG_MASK ((u32)~B_AX_RX_MPDU_MAX_LEN_MASK)
>
> so it looks like B_AX_RX_FLTR_CFG_MASK is not the proper mask for
> this operation...

The purpose of the statements is to update values excluding bits of
B_AX_RX_MPDU_MAX_LEN_MASK. The use of B_AX_RX_FLTR_CFG_MASK is tricky, but
the operation is correct because bit 0 is set, so __ffs(mask) returns 0 in
rtw89_write32_mask(). Then, operation looks like

orig = read(reg);
new = (orig & ~mask) | (data & mask);
write(new);

Since we don't use FIELD_{GET,PREP} macros with B_AX_RX_FLTR_CFG_MASK, how
can you find the problem? Please guide us. Thanks.

Ping-Ke