Re: [PATCH v3] mm/khugepaged: avoid underflow in madvise_collapse for sub-PMD MADV_COLLAPSE

From: David Hildenbrand (Arm)

Date: Wed May 13 2026 - 03:25:09 EST


On 5/13/26 07:54, 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 zero early when
> hstart > hend. The return value is due to input is valid to madvise(),
> and there is nothing to collapse.
>
> madvise(PMD-aligned + PAGE_SIZE, PAGE_SIZE, MADV_COLLAPSE);
>
> In addition, kmalloc_obj(), mmgrab() and lru_add_drain_all() are
> unnecessary when hstart == hend, so skip these operations by
> returning early too.
>
> Signed-off-by: Chen Wandun <chenwandun@xxxxxxxxxxx>
> ---

Acked-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

--
Cheers,

David