All of lore.kernel.org
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Joanne Koong <joannelkoong@gmail.com>,
	akpm@linux-foundation.org, ljs@kernel.org
Cc: baohua@kernel.org, usama.arif@linux.dev, hannes@cmpxchg.org,
	kas@kernel.org, alex@ghiti.fr, ziy@nvidia.com,
	baolin.wang@linux.alibaba.com, liam@infradead.org,
	npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com,
	lance.yang@linux.dev, vbabka@kernel.org, rppt@kernel.org,
	surenb@google.com, mhocko@suse.com, willy@infradead.org,
	linux-mm@kvack.org
Subject: Re: [PATCH v4 4/4] mm: add anonymous mTHP folios to the deferred split list
Date: Tue, 17 Nov 2026 23:21:08 +0100	[thread overview]
Message-ID: <0d75991d-9381-440c-8d7c-07bca53cc8ff@kernel.org> (raw)
In-Reply-To: <20261103212004.1356327-5-joannelkoong@gmail.com>

On 11/3/26 22:20, Joanne Koong wrote:
> Unlike for PMD-sized folios, an anonymous mTHP folio doesn't get added
> to the deferred split list at fault or collapse time. As a result, a
> fully mapped mTHP folio that is mostly zero-filled doesn't get split by
> the deferred split shrinker when the system is under memory pressure.
> 
> At Meta we would like to deploy 2M THP=always on arm64 with 64k base
> pages, as 2M gives the contpte benefits while the PMD size (512M) is too
> big to use. Without underused splitting this causes memory regressions,
> as the unused parts of those folios can never be broken down and
> reclaimed.
> 
> Add anonymous mTHP folios to the deferred split list from
> map_anon_folio_pte_nopf(), mirroring what map_anon_folio_pmd_nopf()
> already does for PMD-sized folios. This covers both the fault path and
> the khugepaged mTHP collapse path. If there is memory pressure, a
> zero-filled mTHP can then be split with its zero pages remapped to the
> shared zero page and reclaimed.
> 
> Only queue mTHP folios of at least 2M for underused splitting. With
> thp_underused() scaling khugepaged/max_ptes_none to the folio size, any
> folio of order 2 or above could in principle be underused, but for small
> folios it is unlikely to be worth it. An order-2 folio on 4k base pages
> can free at most four pages and on systems that only enable small mTHP
> sizes (eg Android's order-2-only configuration) zero-filled subpages
> tend to be few and short-lived.
> 
> The minimum is hard-coded rather than a knob, since admins have no good
> way of choosing it, and is a size rather than an order since what a
> split recovers depends on the folio's size in bytes, and the same order
> means very different sizes across different base page sizes. It can be
> lowered later if data shows smaller sizes are worth it. PMD-sized folios
> are exempt from the minimum in order to maintain backwards compatibility
> since on some architectures (s390, with a 1M PMD) the PMD size can be
> below 2M.
> 
> To minimize overhead on the common order-0 fault path, the
> deferred_split_folio() call is guarded by an inline folio_test_large()
> check.
> 
> Suggested-by: Usama Arif <usama.arif@linux.dev>
> Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
> ---
>  mm/huge_memory.c | 14 ++++++++++++++
>  mm/memory.c      |  2 ++
>  2 files changed, 16 insertions(+)
> 
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 8f90fb609e57..cda3c886ec53 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -4575,6 +4575,13 @@ static bool thp_underused_never(unsigned long max_ptes_none)
>  	return max_ptes_none == HPAGE_PMD_NR - 1;
>  }
>  
> +/*
> + * Smallest mTHP folio that is queued for underused splitting. Kept
> + * conservative, as small folios are unlikely to free enough memory to be
> + * worth queueing and scanning.
> + */
> +#define THP_UNDERUSED_MIN_SIZE	SZ_2M

Working on orders would be nice.

I think you can use something like

	order_base_2(SZ_2M >> PAGE_SHIFT)

for that.

And then you can just do something like

	max(order_base_2(SZ_2M >> PAGE_SHIFT), HPAGE_PMD_ORDER)

And place the comment next to this definition.

> +
>  /* partially_mapped=false won't clear PG_partially_mapped folio flag */
>  void deferred_split_folio(struct folio *folio, bool partially_mapped)
>  {
> @@ -4600,6 +4607,13 @@ void deferred_split_folio(struct folio *folio, bool partially_mapped)
>  		 */
>  		if (thp_underused_never(khugepaged_max_ptes_none))
>  			return;
> +		/*
> +		 * Keep queueing PMD-sized folios as before, even where the PMD
> +		 * size is below the minimum (eg 1M on s390).
> +		 */
> +		if (folio_size(folio) < THP_UNDERUSED_MIN_SIZE &&
> +		    !is_pmd_order(folio_order(folio)))
> +			return;
>  	}

Shame that we already did a function call just to figure that out. Shouldn't we
just replace the folio_test_large() thingy below by helper that embeds this
directly, with a focus on underused shrinking?

>  
>  	/*
> diff --git a/mm/memory.c b/mm/memory.c
> index 0c9ed04528d8..44376321180b 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -5477,6 +5477,8 @@ void map_anon_folio_pte_nopf(struct folio *folio, pte_t *pte,
>  	folio_add_lru_vma(folio, vma);
>  	set_ptes(vma->vm_mm, addr, pte, entry, nr_pages);
>  	update_mmu_cache_range(NULL, vma, addr, pte, nr_pages);
> +	if (folio_test_large(folio))
> +		deferred_split_folio(folio, false);

Do we also ahve to take care of mTHP placed by khugepaged?

-- 
Cheers,

David


      parent reply	other threads:[~2026-11-17 22:21 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03 21:20 [PATCH v4 0/4] mm: split underused anonymous mTHP folios Joanne Koong
2026-11-03 21:20 ` [PATCH v4 1/4] mm/huge_memory: scale thp_underused() to the folio size Joanne Koong
2026-11-05 12:20   ` Kiryl Shutsemau
2026-11-10  3:54   ` Zi Yan
2026-11-12 23:50     ` Joanne Koong
2026-11-10  8:00   ` Baolin Wang
2026-11-17 22:12   ` David Hildenbrand (Arm)
2026-11-03 21:20 ` [PATCH v4 2/4] mm/huge_memory: don't queue folios when underused splitting is inactive Joanne Koong
2026-11-03 22:15   ` Usama Arif
2026-11-05 12:23   ` Kiryl Shutsemau
2026-11-10  8:13   ` Baolin Wang
2026-11-03 21:20 ` [PATCH v4 3/4] mm/huge_memory: add a per-order counter for underused splits Joanne Koong
2026-11-03 22:20   ` Usama Arif
2026-11-05 12:26     ` Kiryl Shutsemau
2026-11-06  0:06       ` Joanne Koong
2026-11-10  8:10   ` Baolin Wang
2026-11-17 22:22   ` David Hildenbrand (Arm)
2026-11-03 21:20 ` [PATCH v4 4/4] mm: add anonymous mTHP folios to the deferred split list Joanne Koong
2026-11-03 22:20   ` Barry Song
2026-11-04 21:21   ` sashiko-bot
2026-11-04 23:50     ` Joanne Koong
2026-11-05 12:37       ` Kiryl Shutsemau
2026-11-06  0:05         ` Joanne Koong
2026-11-09 22:29           ` Joanne Koong
2026-11-17 22:14             ` David Hildenbrand (Arm)
2026-11-17 22:21   ` David Hildenbrand (Arm) [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0d75991d-9381-440c-8d7c-07bca53cc8ff@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=alex@ghiti.fr \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=dev.jain@arm.com \
    --cc=hannes@cmpxchg.org \
    --cc=joannelkoong@gmail.com \
    --cc=kas@kernel.org \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@suse.com \
    --cc=npache@redhat.com \
    --cc=rppt@kernel.org \
    --cc=ryan.roberts@arm.com \
    --cc=surenb@google.com \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=willy@infradead.org \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.