Re: [RFC PATCH] mm: madvise: don't split a PMD on partial THP MADV_DONTNEED

From: David Hildenbrand (Arm)

Date: Thu Dec 03 2026 - 07:32:43 EST


On 12/2/26 15:26, Alexandre Ghiti wrote:
> Hi David,
>
> On Wed, Dec 2, 2026 at 12:22 PM David Hildenbrand (Arm)
> <david@xxxxxxxxxx> wrote:
>>
>>>
>> On 12/2/26 11:28, Alexandre Ghiti wrote:
>>
>> Hi,
>>
>>> MADV_DONTNEED on a range that covers only part of a PMD-mapped THP splits
>>> the PMD and zaps the PTEs of the range. The THP itself is not freed: it
>>> stays allocated and charged as a whole, now partially mapped, until the
>>> deferred split shrinker splits it under memory pressure. Until then the
>>> process has lost the PMD mapping, and the TLB reach that comes with it, for
>>> no memory benefit.
>>
>> Right. User space should avoid doing stupid things.
>
> I think it's hard for userspace when a hugepage becomes partly used,
> it can either keep it whole and waste its free memory, or give the
> free pages back with MADV_DONTNEED and lose the PMD. I see
> MADV_DONTNEED as an honest hint from userspace: "I have used this THP
> for a while, but I'm not sure I'll need all of it in the near future,
> but I'd like to keep the TLB benefits as long as possible. But under
> memory pressure, the kernel can take it."

MADV_DONTNEED means "zap". We'd have to introduce something new with different
semantics. Which I am not a fan of.

If you care about THP benefits, don't shatter THPs with MADV_DONTNEED.

>
>>
>>>
>>> Besides, a THP is checked for being underused only once: it is queued on
>>> the deferred split list when it is created, and the shrinker drops it from
>>> the list for good once it has found it not underused. A long-lived THP
>>> whose pages are freed later is never looked at again, so its unused memory
>>> can't be reclaimed.
>>
>> The job of the underused shrinking is to detect over-allocation. Not "user space
>> used it and then partially freed it".
>
> I thought the goal of the underused shrinker was broader than just
> over-allocation, which raises these questions:

No. It's all about over-allocation right now. That's why we scan exactly once.

--
Cheers,

David