Re: [PATCH 07/10] mm: correct sign-extension issue in MMF_* flag masks

Lorenzo Stoakes
2025-08-26T14:00:28Z

On Tue, Aug 26, 2025 at 03:05:27PM +0200, David Hildenbrand wrote:
> On 12.08.25 17:44, Lorenzo Stoakes wrote:
> > There is an issue with the mask declarations in linux/mm_types.h, which
> > naively do (1 << bit) operations. Unfortunately this results in the 1 being
> > defaulted as a signed (32-bit) integer.
> >
> > When the compiler expands the MMF_INIT_MASK bitmask it comes up with:
> >
> > (((1 << 2) - 1) | (((1 << 9) - 1) << 2) | (1 << 24) | (1 << 28) | (1 << 30)
> > | (1 << 31))
> >
> > Which overflows the signed integer to -788,527,105. Implicitly casting this
> > to an unsigned integer results in sign-expansion, and thus this value
> > becomes 0xffffffffd10007ff, rather than the intended 0xd10007ff.
> >
> > While we're limited to a maximum of 32 bits in mm->flags, this isn't an
> > issue as the remaining bits being masked will always be zero.
> >
> > However, now we are moving towards having more bits in this flag, this
> > becomes an issue.
> >
> > Simply resolve this by using the _BITUL() helper to cast the shifted value
> > to an unsigned long.
>
> Hmm, I thought BIT() should be used and would just fine?

Sure.