mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Qiliang Yuan <qiliang.yuan@linux.dev>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Juri Lelli <juri.lelli@redhat.com>,
	 Vincent Guittot <vincent.guittot@linaro.org>,
	Mel Gorman <mgorman@suse.de>,
	 K Prateek Nayak <kprateek.nayak@amd.com>,
	 David Hildenbrand <david@kernel.org>,
	 Andrew Morton <akpm@linux-foundation.org>
Cc: Dietmar Eggemann <dietmar.eggemann@arm.com>,
	 Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>,
	 Valentin Schneider <vschneid@redhat.com>,
	Zi Yan <ziy@nvidia.com>,  Gregory Price <gourry@gourry.net>,
	 Ying Huang <ying.huang@linux.alibaba.com>,
	 Kefeng Wang <wangkefeng.wang@huawei.com>,
	linux-mm@kvack.org,  linux-kernel@vger.kernel.org,
	Qiliang Yuan <qiliang.yuan@linux.dev>
Subject: [PATCH 4/8] mm/mempolicy: consult secondary MMUs when sampling accessed bits
Date: Sun, 06 Dec 2026 06:38:41 -0500	[thread overview]
Message-ID: <20261206-feat-numab-series-v1-4-2af21ad24718@linux.dev> (raw)
In-Reply-To: <20261206-feat-numab-series-v1-0-2af21ad24718@linux.dev>

Accesses made through a secondary MMU do not set the accessed bit of
the primary page table. A KVM guest reading its hugetlb backed memory
sets the bit in the stage-2 or nested page table only.

Sampling the primary PTEs of the VMM therefore finds that memory idle,
apart from the bits left behind once when it was populated, and the
vCPU threads get no lasting placement signal from it.

Test the accessed bit of secondary MMUs too, with
mmu_notifier_test_young(), and count the page as accessed if either is
set. mmu_notifier_test_young() takes one address, so for a huge mapping
only its first base page is tested, and a secondary MMU that maps it
with smaller entries is undersampled. Testing is enough to tell where
the guest memory lives, and clearing the bit with
mmu_notifier_clear_young() instead is costly when the L1 memory itself
is not backed by huge pages: with such an L1, copy rounds of about 285
ms in an L2 guest with 6 GiB of hugetlb memory took 406 to 841 ms every
2.5 to 4.8 s, and none did with testing alone. With huge pages behind
the L1, both give the same placement and copy times: 133 against 134 ms
with the whole series in the test below.

An L2 guest whose 6 GiB of hugetlb memory are split between node 0 and
node 4 of the L1 VM used for the previous patches copies within 2 GiB of
the node 4 part, its vCPU started on node 0; 3 rounds of 2 runs of 60 s
alternating with the kernel without this patch, median copy time over
the second half of each run. Without the patch the accessed bits left by
populating the guest memory are found once, on both nodes alike, and the
vCPU never stays on node 4. With it the vCPU moves there after 20 to 27
s in every run and stays:

                                before          after  change
  runs ending on node 4            0/6            6/6
  copy 2 GiB (ms)        205 (203-207)  134 (132-134)    -35%

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 mm/mempolicy.c | 42 ++++++++++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 12 deletions(-)

diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index e69a4b38b136e..f4bb2a980072e 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -961,6 +961,7 @@ static int numa_sample_pmd_entry(pmd_t *pmd, unsigned long addr,
 	pte_t *start_pte, *pte, ptent;
 	struct folio *folio;
 	spinlock_t *ptl;
+	bool young;
 
 	ptl = pmd_trans_huge_lock(pmd, vma);
 	if (ptl) {
@@ -968,10 +969,13 @@ static int numa_sample_pmd_entry(pmd_t *pmd, unsigned long addr,
 
 		folio = pmd_present(pmdval) ?
 			vm_normal_folio_pmd(vma, addr, pmdval) : NULL;
-		if (numa_sample_folio(sw, folio) &&
-		    pmdp_test_and_clear_young(vma, addr, pmd)) {
-			sw->node_pages[folio_nid(folio)] += HPAGE_PMD_NR;
-			sw->nr_young += HPAGE_PMD_NR;
+		if (numa_sample_folio(sw, folio)) {
+			young = pmdp_test_and_clear_young(vma, addr, pmd);
+			young |= mmu_notifier_test_young(walk->mm, addr);
+			if (young) {
+				sw->node_pages[folio_nid(folio)] += HPAGE_PMD_NR;
+				sw->nr_young += HPAGE_PMD_NR;
+			}
 		}
 		spin_unlock(ptl);
 		return 0;
@@ -991,7 +995,9 @@ static int numa_sample_pmd_entry(pmd_t *pmd, unsigned long addr,
 		if (!numa_sample_folio(sw, folio))
 			continue;
 
-		if (ptep_test_and_clear_young(vma, addr, pte)) {
+		young = ptep_test_and_clear_young(vma, addr, pte);
+		young |= mmu_notifier_test_young(walk->mm, addr);
+		if (young) {
 			sw->node_pages[folio_nid(folio)]++;
 			sw->nr_young++;
 		}
@@ -1011,17 +1017,26 @@ static int numa_sample_hugetlb_entry(pte_t *pte, unsigned long hmask,
 	struct numa_sample_walk *sw = walk->private;
 	unsigned long psize = huge_page_size(h);
 	spinlock_t *ptl;
+	bool young;
 	pte_t entry;
 	int nid;
 
 	ptl = huge_pte_lock(h, walk->mm, pte);
 	entry = huge_ptep_get(walk->mm, addr, pte);
-	if (pte_present(entry) && pte_young(entry)) {
+	if (pte_present(entry)) {
 		nid = folio_nid(pfn_folio(pte_pfn(entry)));
-		entry = huge_ptep_get_and_clear(walk->mm, addr, pte, psize);
-		set_huge_pte_at(walk->mm, addr, pte, pte_mkold(entry), psize);
-		sw->node_pages[nid] += pages_per_huge_page(h);
-		sw->nr_young += pages_per_huge_page(h);
+		young = pte_young(entry);
+		if (young) {
+			entry = huge_ptep_get_and_clear(walk->mm, addr, pte,
+							psize);
+			set_huge_pte_at(walk->mm, addr, pte, pte_mkold(entry),
+					psize);
+		}
+		young |= mmu_notifier_test_young(walk->mm, addr);
+		if (young) {
+			sw->node_pages[nid] += pages_per_huge_page(h);
+			sw->nr_young += pages_per_huge_page(h);
+		}
 	}
 	spin_unlock(ptl);
 
@@ -1046,8 +1061,11 @@ static const struct mm_walk_ops numa_sample_ops = {
  * @node_pages: Per-node counters, indexed by node id.
  * @all: Sample every folio instead of pinned folios only.
  *
- * Test and clear the accessed bit of hugetlb pages and of pinned folios and
- * add the pages found accessed to @node_pages for the node they reside on.
+ * Test and clear the accessed bit of hugetlb pages and of pinned folios, test
+ * the accessed bit kept by secondary MMUs, and add the pages found accessed to
+ * @node_pages for the node they reside on. mmu_notifier_test_young() takes a
+ * single address, so a secondary MMU is asked about the first base page of a
+ * huge mapping only; one that maps it with smaller entries is undersampled.
  *
  * Return: The number of pages found accessed.
  */

-- 
2.43.0


  parent reply	other threads:[~2026-12-06 11:39 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06 11:38 [PATCH 0/8] sched/numa: Placement signal for memory that NUMA balancing cannot migrate Qiliang Yuan
2026-12-06 11:38 ` [PATCH 1/8] sched/numa: Sample the accessed bits of pinned memory Qiliang Yuan
2026-12-06 11:38 ` [PATCH 2/8] sched/numa: Cover hugetlb mappings with accessed-bit sampling Qiliang Yuan
2026-12-06 11:38 ` [PATCH 3/8] sched/numa: Extend accessed-bit sampling to DAX mappings Qiliang Yuan
2026-12-06 11:38 ` Qiliang Yuan [this message]
2026-12-06 11:38 ` [PATCH 5/8] sched/numa: Track whether the memory of a task can follow it Qiliang Yuan
2026-12-06 11:38 ` [PATCH 6/8] sched/numa: Keep tasks near memory that cannot follow them Qiliang Yuan
2026-12-06 11:38 ` [PATCH 7/8] sched/fair: Avoid pulling tasks away from " Qiliang Yuan
2026-12-06 11:38 ` [PATCH 8/8] sched/numa: Share sampled accesses between the threads of a process Qiliang Yuan

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=20261206-feat-numab-series-v1-4-2af21ad24718@linux.dev \
    --to=qiliang.yuan@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=bsegall@google.com \
    --cc=david@kernel.org \
    --cc=dietmar.eggemann@arm.com \
    --cc=gourry@gourry.net \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    --cc=wangkefeng.wang@huawei.com \
    --cc=ying.huang@linux.alibaba.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®