* [PATCH 1/1] mm/mm_init: compute hash mask with unsigned arithmetic
@ 2026-08-09 12:31 Longlong Xia
2026-08-09 14:03 ` Mike Rapoport
0 siblings, 1 reply; 5+ messages in thread
From: Longlong Xia @ 2026-08-09 12:31 UTC (permalink / raw)
To: rppt; +Cc: akpm, linux-mm, linux-kernel, Longlong Xia
From: Longlong Xia <xialonglong@kylinos.cn>
alloc_large_system_hash() allows up to 2^31 buckets, so log2qty can
reach 31. At that limit, the current expression relies on signed
integer wrapping to produce the unsigned hash mask.
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.
Assisted-by: Codex:gpt-5.6-sol
Signed-off-by: Longlong Xia <xialonglong@kylinos.cn>
---
mm/mm_init.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mm/mm_init.c b/mm/mm_init.c
index 498d62c4ece3..1883fe923ffb 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -2469,7 +2469,7 @@ void *__init alloc_large_system_hash(const char *tablename,
if (_hash_shift)
*_hash_shift = log2qty;
if (_hash_mask)
- *_hash_mask = (1 << log2qty) - 1;
+ *_hash_mask = (1U << log2qty) - 1;
return table;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] mm/mm_init: compute hash mask with unsigned arithmetic
2026-08-09 12:31 [PATCH 1/1] mm/mm_init: compute hash mask with unsigned arithmetic Longlong Xia
@ 2026-08-09 14:03 ` Mike Rapoport
2026-08-09 14:25 ` Longlong Xia
0 siblings, 1 reply; 5+ messages in thread
From: Mike Rapoport @ 2026-08-09 14:03 UTC (permalink / raw)
To: Longlong Xia; +Cc: akpm, linux-mm, linux-kernel, Longlong Xia
Hi,
On Sun, Aug 09, 2026 at 08:31:19PM +0800, Longlong Xia wrote:
> From: Longlong Xia <xialonglong@kylinos.cn>
>
> alloc_large_system_hash() allows up to 2^31 buckets, so log2qty can
> reach 31. At that limit, the current expression relies on signed
> integer wrapping to produce the unsigned hash mask.
>
> 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?
> Assisted-by: Codex:gpt-5.6-sol
> Signed-off-by: Longlong Xia <xialonglong@kylinos.cn>
> ---
> mm/mm_init.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/mm_init.c b/mm/mm_init.c
> index 498d62c4ece3..1883fe923ffb 100644
> --- a/mm/mm_init.c
> +++ b/mm/mm_init.c
> @@ -2469,7 +2469,7 @@ void *__init alloc_large_system_hash(const char *tablename,
> if (_hash_shift)
> *_hash_shift = log2qty;
> if (_hash_mask)
> - *_hash_mask = (1 << log2qty) - 1;
> + *_hash_mask = (1U << log2qty) - 1;
>
> return table;
> }
> --
> 2.43.0
>
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] mm/mm_init: compute hash mask with unsigned arithmetic
2026-08-09 14:03 ` Mike Rapoport
@ 2026-08-09 14:25 ` Longlong Xia
2026-08-10 1:14 ` Andrew Morton
0 siblings, 1 reply; 5+ messages in thread
From: Longlong Xia @ 2026-08-09 14:25 UTC (permalink / raw)
To: Mike Rapoport; +Cc: akpm, linux-mm, linux-kernel, Longlong Xia
在 2026/8/9 22:03, Mike Rapoport 写道:
> Hi,
>
> On Sun, Aug 09, 2026 at 08:31:19PM +0800, Longlong Xia wrote:
>> From: Longlong Xia <xialonglong@kylinos.cn>
>>
>> alloc_large_system_hash() allows up to 2^31 buckets, so log2qty can
>> reach 31. At that limit, the current expression relies on signed
>> integer wrapping to produce the unsigned hash mask.
>>
>> 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.
Thanks for the review.
Best regards,
Longlong
>> Assisted-by: Codex:gpt-5.6-sol
>> Signed-off-by: Longlong Xia <xialonglong@kylinos.cn>
>> ---
>> mm/mm_init.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/mm/mm_init.c b/mm/mm_init.c
>> index 498d62c4ece3..1883fe923ffb 100644
>> --- a/mm/mm_init.c
>> +++ b/mm/mm_init.c
>> @@ -2469,7 +2469,7 @@ void *__init alloc_large_system_hash(const char *tablename,
>> if (_hash_shift)
>> *_hash_shift = log2qty;
>> if (_hash_mask)
>> - *_hash_mask = (1 << log2qty) - 1;
>> + *_hash_mask = (1U << log2qty) - 1;
>>
>> return table;
>> }
>> --
>> 2.43.0
>>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] mm/mm_init: compute hash mask with unsigned arithmetic
2026-08-09 14:25 ` Longlong Xia
@ 2026-08-10 1:14 ` Andrew Morton
2026-08-10 3:21 ` Matthew Wilcox
0 siblings, 1 reply; 5+ messages in thread
From: Andrew Morton @ 2026-08-10 1:14 UTC (permalink / raw)
To: Longlong Xia; +Cc: Mike Rapoport, linux-mm, linux-kernel, Longlong Xia
On Sun, 9 Aug 2026 22:25:35 +0800 Longlong Xia <xialonglong2025@163.com> wrote:
> 在 2026/8/9 22:03, Mike Rapoport 写道:
> > Hi,
> >
> > On Sun, Aug 09, 2026 at 08:31:19PM +0800, Longlong Xia wrote:
> >> From: Longlong Xia <xialonglong@kylinos.cn>
> >>
> >> alloc_large_system_hash() allows up to 2^31 buckets, so log2qty can
> >> reach 31. At that limit, the current expression relies on signed
> >> integer wrapping to produce the unsigned hash mask.
> >>
> >> 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.
The copied bit pattern happens to be what we'd have got if the code had
been correct, but the code isn't correct!
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/1] mm/mm_init: compute hash mask with unsigned arithmetic
2026-08-10 1:14 ` Andrew Morton
@ 2026-08-10 3:21 ` Matthew Wilcox
0 siblings, 0 replies; 5+ messages in thread
From: Matthew Wilcox @ 2026-08-10 3:21 UTC (permalink / raw)
To: Andrew Morton
Cc: Longlong Xia, Mike Rapoport, linux-mm, linux-kernel, Longlong Xia
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.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-08-10 3:22 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-09 12:31 [PATCH 1/1] mm/mm_init: compute hash mask with unsigned arithmetic Longlong Xia
2026-08-09 14:03 ` Mike Rapoport
2026-08-09 14:25 ` Longlong Xia
2026-08-10 1:14 ` Andrew Morton
2026-08-10 3:21 ` Matthew Wilcox
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox