Re: [PATCH v2] mm/khugepaged: avoid underflow in madvise_collapse for sub-PMD MADV_COLLAPSE
From: David Hildenbrand (Arm)
Date: Mon May 11 2026 - 04:26:44 EST
On 5/11/26 09:35, Wandun wrote:
>
>
> On 5/11/26 15:17, David Hildenbrand (Arm) wrote:
>> On 5/11/26 08:57, Wandun Chen wrote:
>>> From: Chen Wandun <chenwandun@xxxxxxxxxxx>
>>>
>>> madvise_collapse() computes the THP-aligned window:
>>>
>>> hstart = ALIGN(start, HPAGE_PMD_SIZE); /* round up */
>>> hend = ALIGN_DOWN(end, HPAGE_PMD_SIZE); /* round down */
>>>
>>> The following case will cause hstart > hend, and result in underflow
>>> in the return statement, avoid it by returning -EINVAL early when
>>> hstart > hend.
>>>
>>> madvise(PMD-aligned + PAGE_SIZE, PAGE_SIZE, MADV_COLLAPSE);
>> Ok, so providing a PMD-aligned address as start will result in 0 and a
>> non-aligned address will result in -EINVAL.
>>
>> Didn't Lorenzo agree that just returning 0 in both cases would be clearer? But I
>> might have misunderstood it.
> Lorenzo suggested retuern -EINVAL for both case at the beginning,
> Later, Lorenzo add an correction, suggested should return 0 for
> compatibilty reasons for hstart == hend case.
> (If I haven't missed any information)
Let's wait for Lorenzo's confirmation.
I think the important part is that we cannot have a situation where start < end
(given that madvise() consumes a length). Because, there we really should have
returned -EINVAL.
For start <= end, if there is nothing suitable to collapse, I'd say we'd just
consistently return 0.
--
Cheers,
David