Re: [PATCH 1/1] mm/mm_init: compute hash mask with unsigned arithmetic

Matthew Wilcox <[email protected]>
Newsgroups gmane.linux.kernel,gmane.linux.kernel.mm
Message-ID <[email protected]>
On Sun, Aug 09, 2026 at 06:14:00PM -0700, Andrew Morton wrote:
> > >> Use an unsigned literal so the mask is computed with unsigned
> > >> arithmetic. Supported compiler settings already produce the same
> > >> result, so this is a source-level cleanup with no functional change.
> > > If complier already takes care of this then why do we want this patch?
> >   
> > Fair point. Since supported compilers already produce the intended 
> > result and this patch has no functional impact, the benefit is only 
> > making the unsigned arithmetic explicit. So please disregard this patch.
> 
> It's a very small thing, but I believe the patch improves the code.
> 
> I mean, we erroneously compute a large negative number then subtract 1
> from it then copy that larger negative number into a signed scalar.

That is an erroneous description of this code.

1 << 31 is the largest-magnitude negative number, ie it's INT_MIN.
We then subtract one from it, so it wraps back around to INT_MAX.
So the number assigned to *_hash_mask is always positive.

> The copied bit pattern happens to be what we'd have got if the code had
> been correct, but the code isn't correct!

The code is correct as long as we compile with -fwrapv or whatever that
got renamed to.  And I don't see that changing.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.