Subject: Re: [PATCH 3/9] bitmap: Use FIELD_PREP() in expansion of FIELD_PREP_WM16()

From: David Laight <[email-masked]>
Date: Thu, 11 Dec 2025 17:52:50 +0000

On Thu, 11 Dec 2025 13:50:28 +0100
Nicolas Frattaroli <[email-masked]> wrote:

> On Wednesday, 10 December 2025 21:59:15 Central European Standard Time David Laight wrote:
> > On Wed, 10 Dec 2025 20:18:30 +0100
> > Nicolas Frattaroli <[email-masked]> wrote:
> >   
> > > On Tuesday, 9 December 2025 11:03:07 Central European Standard Time [email-masked] wrote:  
> > > > From: David Laight <[email-masked]>
> > > > 
> > > > Instead of directly expanding __BF_FIELD_CHECK() (which really ought
> > > > not be used outside bitfield) and open-coding the generation of the
> > > > masked value, just call FIELD_PREP() and add an extra check for
> > > > the mask being at most 16 bits.
> > > > 
> > > > Signed-off-by: David Laight <[email-masked]>
> > > > ---
> > > >  include/linux/hw_bitfield.h | 17 ++++++++---------
> > > >  1 file changed, 8 insertions(+), 9 deletions(-)
> > > > 
> > > > diff --git a/include/linux/hw_bitfield.h b/include/linux/hw_bitfield.h
> > > > index df202e167ce4..d7f21b60449b 100644
> > > > --- a/include/linux/hw_bitfield.h
> > > > +++ b/include/linux/hw_bitfield.h
> > > > @@ -23,15 +23,14 @@
> > > >   * register, a bit in the lower half is only updated if the corresponding bit
> > > >   * in the upper half is high.
> > > >   */
> > > > -#define FIELD_PREP_WM16(_mask, _val)					     \
> > > > -	({								     \
> > > > -		typeof(_val) __val = _val;				     \
> > > > -		typeof(_mask) __mask = _mask;				     \
> > > > -		__BF_FIELD_CHECK(__mask, ((u16)0U), __val,		     \
> > > > -				 "HWORD_UPDATE: ");			     \
> > > > -		(((typeof(__mask))(__val) << __bf_shf(__mask)) & (__mask)) | \
> > > > -		((__mask) << 16);					     \
> > > > -	})
> > > > +#define FIELD_PREP_WM16(mask, val)				\
> > > > +({								\
> > > > +	__auto_type _mask = mask;				\
> > > > +	u32 _val = FIELD_PREP(_mask, val);			\
> > > > +	BUILD_BUG_ON_MSG(_mask > 0xffffu,			\
> > > > +			 "FIELD_PREP_WM16: mask too large");	\
> > > > +	_val | (_mask << 16);					\
> > > > +})
> > > >  
> > > >  /**
> > > >   * FIELD_PREP_WM16_CONST() - prepare a constant bitfield element with a mask in
> > > >     
> > > 
> > > This breaks the build for at least one driver that uses
> > > FIELD_PREP_WM16, namely phy-rockchip-emmc.c:  
> > 
> > Not in my allmodconfig build.
> > ...   
> > > pcie-dw-rockchip.c is similarly broken by this change, except
> > > without the superfluous wrapper:  
> > 
> > That one did get built.  
> 
> I build with clang 21.1.6 for arm64, in case that's any help.
> I don't see how pcie-dw-rockchip.c built for you if FIELD_PREP
> and FIELD_PREP_WM16 have conflicting symbol names?

I would expect gcc to have found that 'silly' error.
I'll generate a v2 soon.

	David

> 
> > 
> > The problem is that FIELD_PREP_WM16() needs to use different 'local'
> > variables than FIELD_PREP().
> > The 'proper' fix is to use unique names (as min() and max() do), but that
> > makes the whole thing unreadable and is best avoided unless nesting is
> > likely.
> > In this case s/mask/wm16_mask/ and s/val/wm16_val/ might be best.
> > 
> > 	David
> > 
> >   
> 
> 
> 
> 
>