linux-wireless.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: [PATCH v6 12/26] bitfield: Add less-checking __FIELD_{GET,PREP}()
       [not found]       ` <aQzIIqNnTY41giH_@smile.fi.intel.com>
@ 2025-11-06 16:20         ` Geert Uytterhoeven
  2025-11-07  1:13           ` Ping-Ke Shih
  0 siblings, 1 reply; 7+ messages in thread
From: Geert Uytterhoeven @ 2025-11-06 16:20 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Yury Norov, Michael Turquette, Stephen Boyd, Nicolas Ferre,
	Alexandre Belloni, Claudiu Beznea, Giovanni Cabiddu, Herbert Xu,
	David Miller, Linus Walleij, Bartosz Golaszewski, Joel Stanley,
	Andrew Jeffery, Crt Mori, Jonathan Cameron, Lars-Peter Clausen,
	Jacky Huang, Shan-Chun Hung, Rasmus Villemoes, Jaroslav Kysela,
	Takashi Iwai, Johannes Berg, Jakub Kicinski, Alex Elder,
	David Laight, Vincent Mailhol, Jason Baron, Borislav Petkov,
	Tony Luck, Michael Hennerich, Kim Seer Paller, David Lechner,
	Nuno Sá, Andy Shevchenko, Richard Genoud, Cosmin Tanislav,
	Biju Das, Jianping Shen, Nathan Chancellor, Nick Desaulniers,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, linux-clk,
	linux-arm-kernel, linux-renesas-soc, linux-crypto, linux-edac,
	qat-linux, linux-gpio, linux-aspeed, linux-iio, linux-sound,
	linux-mtd, linux-kernel, Ping-Ke Shih, linux-wireless

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...

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

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

* RE: [PATCH v6 12/26] bitfield: Add less-checking __FIELD_{GET,PREP}()
  2025-11-06 16:20         ` [PATCH v6 12/26] bitfield: Add less-checking __FIELD_{GET,PREP}() Geert Uytterhoeven
@ 2025-11-07  1:13           ` Ping-Ke Shih
  2025-11-07  7:59             ` Andy Shevchenko
  2025-11-07  8:35             ` Geert Uytterhoeven
  0 siblings, 2 replies; 7+ messages in thread
From: Ping-Ke Shih @ 2025-11-07  1:13 UTC (permalink / raw)
  To: Geert Uytterhoeven, Andy Shevchenko
  Cc: Yury Norov, Michael Turquette, Stephen Boyd, Nicolas Ferre,
	Alexandre Belloni, Claudiu Beznea, Giovanni Cabiddu, Herbert Xu,
	David Miller, Linus Walleij, Bartosz Golaszewski, Joel Stanley,
	Andrew Jeffery, Crt Mori, Jonathan Cameron, Lars-Peter Clausen,
	Jacky Huang, Shan-Chun Hung, Rasmus Villemoes, Jaroslav Kysela,
	Takashi Iwai, Johannes Berg, Jakub Kicinski, Alex Elder,
	David Laight, Vincent Mailhol, Jason Baron, Borislav Petkov,
	Tony Luck, Michael Hennerich, Kim Seer Paller, David Lechner,
	Nuno Sá, Andy Shevchenko, Richard Genoud, Cosmin Tanislav,
	Biju Das, Jianping Shen, Nathan Chancellor, Nick Desaulniers,
	Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-renesas-soc@vger.kernel.org, linux-crypto@vger.kernel.org,
	linux-edac@vger.kernel.org, qat-linux@intel.com,
	linux-gpio@vger.kernel.org, linux-aspeed@lists.ozlabs.org,
	linux-iio@vger.kernel.org, linux-sound@vger.kernel.org,
	linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
	linux-wireless

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


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

* Re: [PATCH v6 12/26] bitfield: Add less-checking __FIELD_{GET,PREP}()
  2025-11-07  1:13           ` Ping-Ke Shih
@ 2025-11-07  7:59             ` Andy Shevchenko
  2025-11-10  9:33               ` Geert Uytterhoeven
  2025-11-07  8:35             ` Geert Uytterhoeven
  1 sibling, 1 reply; 7+ messages in thread
From: Andy Shevchenko @ 2025-11-07  7:59 UTC (permalink / raw)
  To: Ping-Ke Shih
  Cc: Geert Uytterhoeven, Andy Shevchenko, Yury Norov,
	Michael Turquette, Stephen Boyd, Nicolas Ferre, Alexandre Belloni,
	Claudiu Beznea, Giovanni Cabiddu, Herbert Xu, David Miller,
	Linus Walleij, Bartosz Golaszewski, Joel Stanley, Andrew Jeffery,
	Crt Mori, Jonathan Cameron, Lars-Peter Clausen, Jacky Huang,
	Shan-Chun Hung, Rasmus Villemoes, Jaroslav Kysela, Takashi Iwai,
	Johannes Berg, Jakub Kicinski, Alex Elder, David Laight,
	Vincent Mailhol, Jason Baron, Borislav Petkov, Tony Luck,
	Michael Hennerich, Kim Seer Paller, David Lechner, Nuno Sá,
	Andy Shevchenko, Richard Genoud, Cosmin Tanislav, Biju Das,
	Jianping Shen, Nathan Chancellor, Nick Desaulniers, Miquel Raynal,
	Richard Weinberger, Vignesh Raghavendra,
	linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-renesas-soc@vger.kernel.org, linux-crypto@vger.kernel.org,
	linux-edac@vger.kernel.org, qat-linux@intel.com,
	linux-gpio@vger.kernel.org, linux-aspeed@lists.ozlabs.org,
	linux-iio@vger.kernel.org, linux-sound@vger.kernel.org,
	linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
	linux-wireless

On Fri, Nov 7, 2025 at 3:16 AM Ping-Ke Shih <pkshih@realtek.com> wrote:
>
> Geert Uytterhoeven <geert@linux-m68k.org> wrote:
> > 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.

Isn't FIELD_MODIFY() what you want to use?

-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH v6 12/26] bitfield: Add less-checking __FIELD_{GET,PREP}()
  2025-11-07  1:13           ` Ping-Ke Shih
  2025-11-07  7:59             ` Andy Shevchenko
@ 2025-11-07  8:35             ` Geert Uytterhoeven
  2025-11-07  9:16               ` Ping-Ke Shih
  2025-11-10  2:43               ` Ping-Ke Shih
  1 sibling, 2 replies; 7+ messages in thread
From: Geert Uytterhoeven @ 2025-11-07  8:35 UTC (permalink / raw)
  To: Ping-Ke Shih
  Cc: Andy Shevchenko, Yury Norov, Michael Turquette, Stephen Boyd,
	Nicolas Ferre, Alexandre Belloni, Claudiu Beznea,
	Giovanni Cabiddu, Herbert Xu, David Miller, Linus Walleij,
	Bartosz Golaszewski, Joel Stanley, Andrew Jeffery, Crt Mori,
	Jonathan Cameron, Lars-Peter Clausen, Jacky Huang, Shan-Chun Hung,
	Rasmus Villemoes, Jaroslav Kysela, Takashi Iwai, Johannes Berg,
	Jakub Kicinski, Alex Elder, David Laight, Vincent Mailhol,
	Jason Baron, Borislav Petkov, Tony Luck, Michael Hennerich,
	Kim Seer Paller, David Lechner, Nuno Sá, Andy Shevchenko,
	Richard Genoud, Cosmin Tanislav, Biju Das, Jianping Shen,
	Nathan Chancellor, Nick Desaulniers, Miquel Raynal,
	Richard Weinberger, Vignesh Raghavendra,
	linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-renesas-soc@vger.kernel.org, linux-crypto@vger.kernel.org,
	linux-edac@vger.kernel.org, qat-linux@intel.com,
	linux-gpio@vger.kernel.org, linux-aspeed@lists.ozlabs.org,
	linux-iio@vger.kernel.org, linux-sound@vger.kernel.org,
	linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
	linux-wireless

Hi Ping-Ke,

On Fri, 7 Nov 2025 at 02:16, Ping-Ke Shih <pkshih@realtek.com> wrote:
> Geert Uytterhoeven <geert@linux-m68k.org> wrote:
> > 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);

Thanks for your quick confirmation!
So the intention really is to clear bits 22-31, and write the rx_fltr
value to bits 0-15?

if the clearing is not needed, it would be better to use
#define B_AX_RX_FLTR_CFG_MASK GENMASK(15, 0)

If the clearing is needed, I still think it would be better to
change B_AX_RX_FLTR_CFG_MASK, and split the clearing off in a separate
operation, to make it more explicit and obvious for the casual reader.

> 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.

I still have "[PATCH/RFC 17/17] rtw89: Use bitfield helpers"
https://lore.kernel.org/all/f7b81122f7596fa004188bfae68f25a68c2d2392.1637592133.git.geert+renesas@glider.be/
in my local tree, which started flagging the use of a discontiguous
mask with the improved checking in field_prep().

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

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

* RE: [PATCH v6 12/26] bitfield: Add less-checking __FIELD_{GET,PREP}()
  2025-11-07  8:35             ` Geert Uytterhoeven
@ 2025-11-07  9:16               ` Ping-Ke Shih
  2025-11-10  2:43               ` Ping-Ke Shih
  1 sibling, 0 replies; 7+ messages in thread
From: Ping-Ke Shih @ 2025-11-07  9:16 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Andy Shevchenko, Yury Norov, Michael Turquette, Stephen Boyd,
	Nicolas Ferre, Alexandre Belloni, Claudiu Beznea,
	Giovanni Cabiddu, Herbert Xu, David Miller, Linus Walleij,
	Bartosz Golaszewski, Joel Stanley, Andrew Jeffery, Crt Mori,
	Jonathan Cameron, Lars-Peter Clausen, Jacky Huang, Shan-Chun Hung,
	Rasmus Villemoes, Jaroslav Kysela, Takashi Iwai, Johannes Berg,
	Jakub Kicinski, Alex Elder, David Laight, Vincent Mailhol,
	Jason Baron, Borislav Petkov, Tony Luck, Michael Hennerich,
	Kim Seer Paller, David Lechner, Nuno Sá, Andy Shevchenko,
	Richard Genoud, Cosmin Tanislav, Biju Das, Jianping Shen,
	Nathan Chancellor, Nick Desaulniers, Miquel Raynal,
	Richard Weinberger, Vignesh Raghavendra,
	linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-renesas-soc@vger.kernel.org, linux-crypto@vger.kernel.org,
	linux-edac@vger.kernel.org, qat-linux@intel.com,
	linux-gpio@vger.kernel.org, linux-aspeed@lists.ozlabs.org,
	linux-iio@vger.kernel.org, linux-sound@vger.kernel.org,
	linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
	linux-wireless

Geert Uytterhoeven <geert@linux-m68k.org> wrote:
> Hi Ping-Ke,
> 
> On Fri, 7 Nov 2025 at 02:16, Ping-Ke Shih <pkshih@realtek.com> wrote:
> > Geert Uytterhoeven <geert@linux-m68k.org> wrote:
> > > 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);
> 
> Thanks for your quick confirmation!
> So the intention really is to clear bits 22-31, and write the rx_fltr
> value to bits 0-15?
> 
> if the clearing is not needed, it would be better to use
> #define B_AX_RX_FLTR_CFG_MASK GENMASK(15, 0)

But it should be 
#define B_AX_RX_FLTR_CFG_MASK (GENMASK(31, 22) | GENMASK(15, 0))

Originally (with bug) we just backup rx_fltr and write whole 32-bits back,
but it's incorrect to modify GENMASK(21, 16) which is written by another
code.

One way is to implement a special function to replace
  rtw89_write32_mask(rtwdev, reg, B_AX_RX_FLTR_CFG_MASK, rtwdev->hal.rx_fltr);
Such as
  rtw89_write_rx_flter(rtwdev, rtwdev->hal.rx_fltr)
  {
    orig = read(reg);
    new = (orig & ~mask) | (data & mask);
    write(new);
  }

Another way is that I add value of B_AX_RX_MPDU_MAX_LEN_MASK into
rtwdev->hal.rx_fltr. Then, just write whole 32-bit, no need mask.

> 
> If the clearing is needed, I still think it would be better to
> change B_AX_RX_FLTR_CFG_MASK, and split the clearing off in a separate
> operation, to make it more explicit and obvious for the casual reader.
> 
> > 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.
> 
> I still have "[PATCH/RFC 17/17] rtw89: Use bitfield helpers"
> https://lore.kernel.org/all/f7b81122f7596fa004188bfae68f25a68c2d2392.1637592133.git.geert+renesas@glid
> er.be/
> in my local tree, which started flagging the use of a discontiguous
> mask with the improved checking in field_prep().

Got it. You are doing "Non-const bitfield helper conversions". 

Ping-Ke


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

* RE: [PATCH v6 12/26] bitfield: Add less-checking __FIELD_{GET,PREP}()
  2025-11-07  8:35             ` Geert Uytterhoeven
  2025-11-07  9:16               ` Ping-Ke Shih
@ 2025-11-10  2:43               ` Ping-Ke Shih
  1 sibling, 0 replies; 7+ messages in thread
From: Ping-Ke Shih @ 2025-11-10  2:43 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Andy Shevchenko, Yury Norov, Michael Turquette, Stephen Boyd,
	Nicolas Ferre, Alexandre Belloni, Claudiu Beznea,
	Giovanni Cabiddu, Herbert Xu, David Miller, Linus Walleij,
	Bartosz Golaszewski, Joel Stanley, Andrew Jeffery, Crt Mori,
	Jonathan Cameron, Lars-Peter Clausen, Jacky Huang, Shan-Chun Hung,
	Rasmus Villemoes, Jaroslav Kysela, Takashi Iwai, Johannes Berg,
	Jakub Kicinski, Alex Elder, David Laight, Vincent Mailhol,
	Jason Baron, Borislav Petkov, Tony Luck, Michael Hennerich,
	Kim Seer Paller, David Lechner, Nuno Sá, Andy Shevchenko,
	Richard Genoud, Cosmin Tanislav, Biju Das, Jianping Shen,
	Nathan Chancellor, Nick Desaulniers, Miquel Raynal,
	Richard Weinberger, Vignesh Raghavendra,
	linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-renesas-soc@vger.kernel.org, linux-crypto@vger.kernel.org,
	linux-edac@vger.kernel.org, qat-linux@intel.com,
	linux-gpio@vger.kernel.org, linux-aspeed@lists.ozlabs.org,
	linux-iio@vger.kernel.org, linux-sound@vger.kernel.org,
	linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
	linux-wireless

Hi Geert,

Ping-Ke Shih <pkshih@realtek.com> wrote:
> Geert Uytterhoeven <geert@linux-m68k.org> wrote:
> > Hi Ping-Ke,
> >
> > On Fri, 7 Nov 2025 at 02:16, Ping-Ke Shih <pkshih@realtek.com> wrote:
> > > Geert Uytterhoeven <geert@linux-m68k.org> wrote:
> > > > 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);
> >
> > Thanks for your quick confirmation!
> > So the intention really is to clear bits 22-31, and write the rx_fltr
> > value to bits 0-15?
> >
> > if the clearing is not needed, it would be better to use
> > #define B_AX_RX_FLTR_CFG_MASK GENMASK(15, 0)
> 
> But it should be
> #define B_AX_RX_FLTR_CFG_MASK (GENMASK(31, 22) | GENMASK(15, 0))
> 
> Originally (with bug) we just backup rx_fltr and write whole 32-bits back,
> but it's incorrect to modify GENMASK(21, 16) which is written by another
> code.
> 
> One way is to implement a special function to replace
>   rtw89_write32_mask(rtwdev, reg, B_AX_RX_FLTR_CFG_MASK, rtwdev->hal.rx_fltr);
> Such as
>   rtw89_write_rx_flter(rtwdev, rtwdev->hal.rx_fltr)
>   {
>     orig = read(reg);
>     new = (orig & ~mask) | (data & mask);
>     write(new);
>   }
> 
> Another way is that I add value of B_AX_RX_MPDU_MAX_LEN_MASK into
> rtwdev->hal.rx_fltr. Then, just write whole 32-bit, no need mask.
> 
> >
> > If the clearing is needed, I still think it would be better to
> > change B_AX_RX_FLTR_CFG_MASK, and split the clearing off in a separate
> > operation, to make it more explicit and obvious for the casual reader.

I missed this paragraph last week, but that is like my thought.
Then I spin a RFT [1]. Please try if it can work with your changes.

[1] https://lore.kernel.org/linux-wireless/20251110023707.6272-1-pkshih@realtek.com/T/#u

> >
> > > 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.
> >
> > I still have "[PATCH/RFC 17/17] rtw89: Use bitfield helpers"
> >
> https://lore.kernel.org/all/f7b81122f7596fa004188bfae68f25a68c2d2392.1637592133.git.geert+renesas@glid
> > er.be/
> > in my local tree, which started flagging the use of a discontiguous
> > mask with the improved checking in field_prep().
> 
> Got it. You are doing "Non-const bitfield helper conversions".
> 
> Ping-Ke


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

* Re: [PATCH v6 12/26] bitfield: Add less-checking __FIELD_{GET,PREP}()
  2025-11-07  7:59             ` Andy Shevchenko
@ 2025-11-10  9:33               ` Geert Uytterhoeven
  0 siblings, 0 replies; 7+ messages in thread
From: Geert Uytterhoeven @ 2025-11-10  9:33 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Ping-Ke Shih, Andy Shevchenko, Yury Norov, Michael Turquette,
	Stephen Boyd, Nicolas Ferre, Alexandre Belloni, Claudiu Beznea,
	Giovanni Cabiddu, Herbert Xu, David Miller, Linus Walleij,
	Bartosz Golaszewski, Joel Stanley, Andrew Jeffery, Crt Mori,
	Jonathan Cameron, Lars-Peter Clausen, Jacky Huang, Shan-Chun Hung,
	Rasmus Villemoes, Jaroslav Kysela, Takashi Iwai, Johannes Berg,
	Jakub Kicinski, Alex Elder, David Laight, Vincent Mailhol,
	Jason Baron, Borislav Petkov, Tony Luck, Michael Hennerich,
	Kim Seer Paller, David Lechner, Nuno Sá, Andy Shevchenko,
	Richard Genoud, Cosmin Tanislav, Biju Das, Jianping Shen,
	Nathan Chancellor, Nick Desaulniers, Miquel Raynal,
	Richard Weinberger, Vignesh Raghavendra,
	linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-renesas-soc@vger.kernel.org, linux-crypto@vger.kernel.org,
	linux-edac@vger.kernel.org, qat-linux@intel.com,
	linux-gpio@vger.kernel.org, linux-aspeed@lists.ozlabs.org,
	linux-iio@vger.kernel.org, linux-sound@vger.kernel.org,
	linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
	linux-wireless

Hi Ady,

On Fri, 7 Nov 2025 at 09:00, Andy Shevchenko <andy.shevchenko@gmail.com> wrote:
> On Fri, Nov 7, 2025 at 3:16 AM Ping-Ke Shih <pkshih@realtek.com> wrote:
> > Geert Uytterhoeven <geert@linux-m68k.org> wrote:
> > > 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.
>
> Isn't FIELD_MODIFY() what you want to use?

FIELD_MODIFY() is a rather recent addition.
That is also the reason why I didn't add a non-const field_modify() yet
(I didn't want to risk delaying this series even more ;-)

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

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

end of thread, other threads:[~2025-11-10  9:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <cover.1762435376.git.geert+renesas@glider.be>
     [not found] ` <cfc32f8530d5c0d4a7fb33c482a4bf549f26ec24.1762435376.git.geert+renesas@glider.be>
     [not found]   ` <aQy0T2vUINze_6_q@smile.fi.intel.com>
     [not found]     ` <CAMuHMdXVUJq36GvNUQE8FnHsX+=1jG4GOJ_034r=fgr_Rw4Djg@mail.gmail.com>
     [not found]       ` <aQzIIqNnTY41giH_@smile.fi.intel.com>
2025-11-06 16:20         ` [PATCH v6 12/26] bitfield: Add less-checking __FIELD_{GET,PREP}() Geert Uytterhoeven
2025-11-07  1:13           ` Ping-Ke Shih
2025-11-07  7:59             ` Andy Shevchenko
2025-11-10  9:33               ` Geert Uytterhoeven
2025-11-07  8:35             ` Geert Uytterhoeven
2025-11-07  9:16               ` Ping-Ke Shih
2025-11-10  2:43               ` Ping-Ke Shih

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).