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

From: Alexandre Ghiti

Date: Wed Dec 02 2026 - 12:40:14 EST


Hi Zi,

On Wed, Dec 2, 2026 at 5:37 PM Zi Yan <ziy@xxxxxxxxxx> wrote:
>
> >
> On Wed Dec 2, 2026 at 5:28 AM EST, Alexandre Ghiti wrote:
> > 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.
>
> Why would you expect the PMD mapping still exists after DONTNEED marks
> part of a PMD THP? I do not think it is reasonable.

Because the folio is not split yet and is still fully charged to the
cgroup, so let's keep the tlb benefits too?

> Could someone abuse
> it by always allocating PMD THPs and DONTNEED part of it just to get PMD
> mappings?

I'm probably missing your point but I don't see the benefit of doing
that, the thp would still be charged and RSS would not decrease.

>
> >
> > 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.
> >
> > When the underused THP shrinker can reclaim the range later, zero it in
> > place instead: it reads as zeroes, as MADV_DONTNEED requires, and the PMD
> > mapping stays. The THP is queued again on the deferred split list, so the
> > shrinker looks at it again.
> >
> > For an exclusive anonymous THP, MADV_DONTNEED on part of it thus no longer
> > leaves it partially mapped: the range reads as zeroes at once, and the
> > memory is given back when reclaim needs it, once the THP holds more than
> > max_ptes_none zeroed pages. Below that, the THP is not split even under
> > memory pressure, where the partially mapped THP used to be split: it keeps
> > up to max_ptes_none zeroed pages, the waste khugepaged already allows when
> > collapsing.
> >
> > The range is zapped as before when the zeroed pages could not be freed,
> > e.g. with max_ptes_none at 511 (the default), or when anyone else may see
> > the THP, e.g. after fork or through a GUP pin.
> >
> > Results on a large production workload (THP=always, max_ptes_none=0,
> > allocator purging with MADV_DONTNEED), same allocator configuration before
> > and after this patch:
>
> Since MADV_DONTNEED is slower, why is the perf not affected by it? The
> purge is done in the background?

No, the purge is done inline. But I measured roughly that the split of
a PMD costs at least as much as zeroing ~128 pages, so we can come up
with a "good" threshold: this patch does not make MADV_DONTNEED more
costly.

>
>
> >
> > THPs and memory change
> > ------------------------------------ ------
> > THPs partially mapped -97%
> > memory mapped by THPs (AnonHugePages) +29%
> > underused THP splits +145%
> > PMD splits +10%
> > workload anonymous memory +0.2%
> >
> > Performance change
> > ------------------------------------ ------
> > p50 latency -1.7%
> > p99 latency -2.3%
> > throughput within noise
> > dTLB misses per instruction -0.4%
> > IPC +0.2%
> >
> > Signed-off-by: Alexandre Ghiti <alex@xxxxxxxx>
> > ---
> > Open questions:
> >
> > - MADV_DONTNEED now zeroes the range synchronously, up to a whole PMD
> > minus one page (2M on x86, 512M with 64K base pages on arm64). Should
> > zeroing be capped, falling back to the split above some size?
> >
> > - A pin taken while the range is being zeroed goes unnoticed, as neither
> > GUP-fast nor GUP takes the folio lock: a device write through it (e.g. an
> > O_DIRECT read into the range from another thread) can land after
> > MADV_DONTNEED returns. Only a process doing I/O into memory it is
> > discarding can see it; pins that exist when the THP is checked fall back
> > to the zap. Closing the window would take a PMD invalidation and a TLB
> > flush per call, which this patch avoids, or GUP backing off on a folio
> > being zeroed. Is the race acceptable?
> >
> > - RSS (and so oom_badness()), /proc/pid/pagemap and mincore() keep showing
> > the range as resident until the THP is split, where the zap made it
> > disappear at once. memcg usage is unchanged: the THP stayed charged until
> > its split before too.
>
> It affects userspace apps checking RSS stats. What is the impact?

It's hard to know how userspace uses RSS, OOM victim selection uses
RSS (according to my AI) so this might make a process more likely to
be OOMed under pressure?

But note that the production workload I'm mentioning is a huge
partially-mapped consumer and you can see that it only increased RSS
by 0.2%.

Thanks for your feedback!

Alex

>
> >
> > - The cow (THP_RUN_SINGLE_PTE), khugepaged and transhuge-stress selftests
> > use a partial MADV_DONTNEED to split a PMD, which MADV_DONTNEED never
> > guaranteed. khugepaged sets max_ptes_none to 511 first, but cow and
> > transhuge-stress keep the host's value: with a lower max_ptes_none they
> > still pass, while silently testing a PMD-mapped THP. They should pin
> > max_ptes_none or split the PMD explicitly (MADV_NOHUGEPAGE, mprotect() or
> > debugfs).
> >
> > PTE-mapped large folios (mTHP) are left for later.
> >
> > include/linux/huge_mm.h | 2 ++
> > include/linux/mm.h | 2 ++
> > mm/huge_memory.c | 69 +++++++++++++++++++++++++++++++++++++++++
> > mm/internal.h | 3 ++
> > mm/madvise.c | 2 +-
> > mm/memory.c | 14 ++++++---
> > 6 files changed, 87 insertions(+), 5 deletions(-)
> >
>
>
> --
> Best Regards,
> Yan, Zi
>
>