Re: [RFC PATCH] mm: madvise: don't split a PMD on partial THP MADV_DONTNEED
From: Shakeel Butt
Date: Thu Dec 03 2026 - 15:08:04 EST
On Thu, Dec 03, 2026 at 04:36:31PM +0100, Alexandre Ghiti wrote:
> Hi Pedro,
>
> On Thu, Dec 3, 2026 at 4:08 PM Pedro Falcato <pfalcato@xxxxxxx> wrote:
> >
> > >
> > On Thu, Dec 03, 2026 at 03:53:08PM +0100, Alexandre Ghiti wrote:
> > > Hi Shakeel,
> > >
> > > On Wed, Dec 2, 2026 at 7:04 PM Shakeel Butt <shakeel.butt@xxxxxxxxx> wrote:
> > > >
> > > > >
> > > > On Wed, Dec 02, 2026 at 11:28:10AM +0100, 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.
> > > >
> > > > Isn't the actual problem higher and unexpected memory usage? Application or the
> > > > malloc implementations do madvise(MADV_DONTNEED) to release memory and expect at
> > > > least decrease in RSS. Why not fix the cgroup memory usage as well by
> > > > synchronously break the THP as well. See
> > > > https://lore.kernel.org/all/20211120201230.920082-1-shakeelb@xxxxxxxxxx/
> > > >
> > > >
> > >
> > > The memcg charge is not the problem I'm facing (though it exists!). My
> > > issue is that the allocator leaves many partially mapped THPs, even
> > > when that memory may be reused soon after (which userspace can't
> > > easily know), and the TLB benefit goes away.
> >
> > I would say doing a madvise() *at all* is silly; at least every time a range
> > is free.
>
> It doesn't happen every time, it only happens after some threshold
> that needs adjustment.
>
> >
> > >
> > > If the allocator stops purging hugepages that are only partly free,
> > > the THPs stay intact (along with the TLB benefit) but RSS grows
> > > noticeably, which is also unacceptable.
> > >
> > > So what I'm looking for is a way for userspace to tell the kernel
> > > which parts of a THP are free, so that the kernel can reclaim that
> > > internal fragmentation when the memory is actually needed.
> > >
> > > But since David says MADV_DONTNEED means zap, I'll try something else.
> >
> > MADV_NOHUGEPAGE? But that requires a VMA split, which sucks. I had the
> > idea (from Kiryl IIRC) on my post-LPC list that splitting off these
> > hints onto a separate tree (freeing us from VMA splitting) could be a
> > good idea.
> >
>
> I thought about using MADV_NOHUGEPAGE indeed (and all the other
> madvise flags)...The hint tree sounds like a nice idea indeed.
>
MADV_NOHUGEPAGE takes mmap lock in write mode and malloc implementations are not
keep to do operations that take mmap_lock in write mode.
> > Either way, you still pay the system call, which still sucks.
> >
> > *Something something *transparent* huge pages*
> > (I didn't have time lately to work on this :/)
> >
> >
> > --
> > Pedro
>
> Thanks for your comments!
>
> Alex