[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CABzRoyb7_WfO6GmTnoUdDtejvchvW=xtsTbrdYtuhXD_9=cBfw@mail.gmail.com>
Date: Sat, 6 Sep 2025 14:35:18 +0800
From: Lance Yang <lance.yang@...ux.dev>
To: David Hildenbrand <david@...hat.com>
Cc: Zi Yan <ziy@...dia.com>, linux-kernel@...r.kernel.org, linux-mm@...ck.org,
Andrew Morton <akpm@...ux-foundation.org>, Lorenzo Stoakes <lorenzo.stoakes@...cle.com>,
Baolin Wang <baolin.wang@...ux.alibaba.com>, "Liam R. Howlett" <Liam.Howlett@...cle.com>,
Nico Pache <npache@...hat.com>, Ryan Roberts <ryan.roberts@....com>, Dev Jain <dev.jain@....com>,
Barry Song <baohua@...nel.org>, Usama Arif <usamaarif642@...il.com>
Subject: Re: [PATCH v1] mm/huge_memory: fix shrinking of all-zero THPs with
max_ptes_none default
On Fri, Sep 5, 2025 at 11:17 PM David Hildenbrand <david@...hat.com> wrote:
>
> On 05.09.25 16:37, Zi Yan wrote:
> > On 5 Sep 2025, at 10:11, David Hildenbrand wrote:
> >
> >> We added an early exit in thp_underused(), probably to avoid scanning
> >> pages when there is no chance for success.
> >>
> >> However, assume we have max_ptes_none = 511 (default).
> >>
> >> Nothing should stop us from freeing all pages part of a THP that
> >> is completely zero (512) and khugepaged will for sure not try to
> >> instantiate a THP in that case (512 shared zeropages).
> >>
> >> This can just trivially happen if someone writes a single 0 byte into a
> >> PMD area, or of course, when data ends up being zero later.
> >>
> >> So let's remove that early exit.
> >>
> >> Do we want to CC stable? Hm, not sure. Probably not urgent.
> >>
> >> Note that, as default, the THP shrinker is active
> >> (/sys/kernel/mm/transparent_hugepage/shrink_underused = 1), and all
> >> THPs are added to the deferred split lists. However, with the
> >> max_ptes_none default we would never scan them. We would not do that. If
> >> that's not desirable, we should just disable the shrinker as default,
> >> also not adding all THPs to the deferred split lists.
> >>
> >> Easy to reproduce:
> >>
> >> 1) Allocate some THPs filled with 0s
> >>
> >> <prog.c>
> >> #include <string.h>
> >> #include <stdio.h>
> >> #include <stdlib.h>
> >> #include <unistd.h>
> >> #include <sys/mman.h>
> >>
> >> const size_t size = 1024*1024*1024;
> >>
> >> int main(void)
> >> {
> >> size_t offs;
> >> char *area;
> >>
> >> area = mmap(0, size, PROT_READ | PROT_WRITE,
> >> MAP_ANON | MAP_PRIVATE, -1, 0);
> >> if (area == MAP_FAILED) {
> >> printf("mmap failed\n");
> >> exit(-1);
> >> }
> >> madvise(area, size, MADV_HUGEPAGE);
> >>
> >> for (offs = 0; offs < size; offs += getpagesize())
> >> area[offs] = 0;
> >> pause();
> >> }
> >> <\prog.c>
> >>
> >> 2) Trigger the shrinker
> >>
> >> E.g., memory pressure through memhog
> >>
> >> 3) Observe that THPs are not getting reclaimed
> >>
> >> $ cat /proc/`pgrep prog`/smaps_rollup
> >>
> >> Would list ~1GiB of AnonHugePages. With this fix, they would get
> >> reclaimed as expected.
> >>
> >> Fixes: dafff3f4c850 ("mm: split underused THPs")
> >> Cc: Andrew Morton <akpm@...ux-foundation.org>
> >> Cc: Lorenzo Stoakes <lorenzo.stoakes@...cle.com>
> >> Cc: Zi Yan <ziy@...dia.com>
> >> Cc: Baolin Wang <baolin.wang@...ux.alibaba.com>
> >> Cc: "Liam R. Howlett" <Liam.Howlett@...cle.com>
> >> Cc: Nico Pache <npache@...hat.com>
> >> Cc: Ryan Roberts <ryan.roberts@....com>
> >> Cc: Dev Jain <dev.jain@....com>
> >> Cc: Barry Song <baohua@...nel.org>
> >> Cc: Usama Arif <usamaarif642@...il.com>
> >> Signed-off-by: David Hildenbrand <david@...hat.com>
> >> ---
> >> mm/huge_memory.c | 3 ---
> >> 1 file changed, 3 deletions(-)
> >>
> > LGTM. Acked-by: Zi Yan <ziy@...dia.com>
> >
> > I also notice that thp_underused() checks num_zero_pages directly
> > against khugepaged_max_ptes_none. This means mTHPs will never be regarded
> > as underused. A similar issue you are discussing in Nico’s khugepaged
> > mTHP support. Maybe checks against these khugepaged_max* variables
> > should be calculated based on nr_pages of a large folio, like
> > making these variables a ratio in other discussion.
>
> Yeah, factoring that out and cleaning it up is my next step.
>
> But note that mTHPs are not a candidate for the shrinker right now. (see
> my explanation in reply to Nicos patch)
Right. IIUC, the logic in deferred_split_scan() is gated by
!folio_test_partially_mapped(folio). This creates two paths:
1) Splits for mTHPs that are partially mapped. These set the flag and
corectly bypass thp_underused().
2) Optional shrinker splits for new or collapsed THPs. These don't set the
flag and must go through thp_underused().
So, thp_underused() is only for whole, PMD-sized THPs right now. Please
correct me if my understanding is wrong here ;)
Cheers,
Lance
Powered by blists - more mailing lists