Re: [PATCH 3/3] compat: make compat_sysinfo() scale far enough to actually fit
From: Bradley Morgan
Date: Thu Nov 05 2026 - 12:18:32 EST
On 5 November 2026 07:25:22 GMT, Arnd Bergmann <arnd@xxxxxxxx> wrote:
>On Thu, Nov 5, 2026, at 01:31, Bradley Morgan wrote:
>> compat_sysinfo() checks whether totalram or totalswap is too big for the
>> compat struct and scales the values down to fit, but the loop stops as
>> soon as s.mem_unit reaches PAGE_SIZE. That caps the compression at one
>> page, 12 bits with 4K pages. On a 64 bit kernel do_sysinfo() always
>> hands it values in bytes with mem_unit == 1, its own overflow math runs
>> on unsigned long so it does not bail until 16 EiB, so the loop runs its
>> 12 turns, the values come back out as a page count, and anything past
>> 2^32 pages still does not fit. Storing that into the u32 field then
>> just drops the top bits.
>>
>> So a 32 bit process on a box past 2^32 pages gets a wrapped number
>> instead of a coarse one, with 4K pages that starts at 16 TiB:
>>
>> ram old new
>> 8 TiB 2147483648 x 4096 unchanged
>> 16 TiB 0 x 4096 2147483648 x 8192
>> 17 TiB 268435456 x 4096 2281701376 x 8192
>> 32 TiB 0 x 4096 2147483648 x 16384
>>
>> The 17 TiB row is the nasty one, a 32 bit process is told the box has
>> 1 TiB of ram. Keep scaling until the values fit instead of until the
>> unit reaches a page, the unit then says how coarse the number is, which
>> is what mem_unit is for.
>>
>> Nothing that works today changes. I ran both loops side by side over
>> ram from 1 MiB to 2^56 with 4K and 64K pages, they disagree on exactly
>> the inputs where the old one dropped bits and agree on every one it got
>> right.
>>
>> Signed-off-by: Bradley Morgan <brads@xxxxxxxxxxxxxx>
>
>Makes sense, thanks for the fix!
>
>Reviewed-by: Arnd Bergmann <arnd@xxxxxxxx>
Do you want to take this or do you want Andrew to?
--- Thanks!
https://lore.kernel.org/all/EE579805-42F2-4C58-B752-F28779EEB717@xxxxxxxxx/