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 1/8] sched/numa: Sample the accessed bits of pinned memory
Date: Sun, 06 Dec 2026 06:38:38 -0500 [thread overview]
Message-ID: <20261206-feat-numab-series-v1-1-2af21ad24718@linux.dev> (raw)
In-Reply-To: <20261206-feat-numab-series-v1-0-2af21ad24718@linux.dev>
NUMA balancing learns where the memory of a task lives from hinting
faults: task_numa_work() makes PTEs inaccessible with change_prot_numa()
and task_numa_fault() records the node of each page that faults. Since
commit 03aa8e4f2732 ("mm: mprotect: always skip dma pinned folio in
prot_numa_skip()") DMA pinned folios, such as VFIO, RDMA and io_uring
registered buffers, are left alone because they cannot be migrated.
That skip also removes the only signal of where the task should run. A
task whose working set is pinned takes no faults on it, gets no
preferred node from it, and the load balancer is free to move it away
from that memory for good.
Sample the pinned folios instead of protecting them. In mms that have
pinned pages, test and clear the accessed bit of the pinned folios in
the range being scanned and account the pages found accessed as faults
on their node. No hinting PTE is installed and nothing is migrated, so
the reason for the skip still holds. Like the MGLRU page table walk, the
TLB is not flushed after clearing the bits.
A task copying within 4 GiB of io_uring registered buffers on node 0 of
an 8-node VM (2 packages of 4 nodes, 8 vCPUs and 4 LLCs per node, SLIT
10/15/25/27, guest memory on host huge pages), started on node 1, 4 or
5, 3 runs of 40 s per start node, alternating with the kernel without
this patch; time of the last copy. A copy takes 245 to 252 ms anywhere
in the package of the memory and 393 to 442 ms on the other one. With
the patch every run settled on node 0, within 1.6 to 2.7 s:
before after change
runs settled on node 0 0/9 9/9
runs ending off package 5/9 0/9
slowest run (ms) 442 252 -43%
median run (ms) 393 (245-442) 247 (245-252) -37%
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
include/linux/mm.h | 2 ++
kernel/sched/fair.c | 28 +++++++++++++++-
mm/mempolicy.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 126 insertions(+), 1 deletion(-)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 040cff64381bf..d887d74d54975 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -5029,6 +5029,8 @@ void vma_set_file(struct vm_area_struct *vma, struct file *file);
#ifdef CONFIG_NUMA_BALANCING
unsigned long change_prot_numa(struct vm_area_struct *vma,
unsigned long start, unsigned long end);
+unsigned long numa_sample_range(struct vm_area_struct *vma, unsigned long start,
+ unsigned long end, unsigned long *node_pages);
#endif
struct vm_area_struct *find_extend_vma_locked(struct mm_struct *,
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 8a55571b9719d..14a4a15ac2747 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -4290,6 +4290,9 @@ static void task_numa_work(struct callback_head *work)
struct vma_iterator vmi;
bool vma_pids_skipped;
bool vma_pids_forced = false;
+ unsigned long *node_pages = NULL;
+ unsigned long nr_sampled;
+ int nid;
WARN_ON_ONCE(p != container_of(work, struct task_struct, numa_work));
@@ -4347,9 +4350,14 @@ static void task_numa_work(struct callback_head *work)
if (!pages)
return;
+ if (mm_flags_test(MMF_HAS_PINNED, mm))
+ node_pages = kcalloc(nr_node_ids, sizeof(*node_pages),
+ GFP_KERNEL | __GFP_NOWARN);
- if (!mmap_read_trylock(mm))
+ if (!mmap_read_trylock(mm)) {
+ kfree(node_pages);
return;
+ }
/*
* VMAs are skipped if the current PID has not trapped a fault within
@@ -4468,6 +4476,12 @@ static void task_numa_work(struct callback_head *work)
end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
end = min(end, vma->vm_end);
nr_pte_updates = change_prot_numa(vma, start, end);
+ nr_sampled = 0;
+ if (node_pages)
+ nr_sampled = numa_sample_range(vma, start, end,
+ node_pages);
+ if (nr_sampled)
+ vma_set_access_pid_bit(vma);
/*
* Try to scan sysctl_numa_balancing_size worth of
@@ -4522,6 +4536,18 @@ static void task_numa_work(struct callback_head *work)
reset_ptenuma_scan(p);
mmap_read_unlock(mm);
+ /* Account the sampled accesses as faults on the nodes of the pages. */
+ if (node_pages) {
+ for (nid = 0; nid < nr_node_ids; nid++) {
+ if (!node_pages[nid])
+ continue;
+ task_numa_fault(-1 & LAST_CPUPID_MASK, nid,
+ node_pages[nid],
+ nid == task_node(p) ? TNF_FAULT_LOCAL : 0);
+ }
+ kfree(node_pages);
+ }
+
/*
* Make sure tasks use at least 32x as much time to run other code
* than they used here, to limit NUMA PTE scanning overhead to 3% max.
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index ffe4e45cc97d1..d11c38828ee87 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -931,6 +931,103 @@ unsigned long change_prot_numa(struct vm_area_struct *vma,
return nr_updated;
}
+
+struct numa_sample_walk {
+ unsigned long *node_pages;
+ unsigned long nr_young;
+};
+
+static int numa_sample_pud_entry(pud_t *pud, unsigned long addr,
+ unsigned long next, struct mm_walk *walk)
+{
+ /* Leave huge PUD mappings alone instead of having the walk split them. */
+ if (pud_leaf(pudp_get(pud)))
+ walk->action = ACTION_CONTINUE;
+
+ return 0;
+}
+
+static int numa_sample_pmd_entry(pmd_t *pmd, unsigned long addr,
+ unsigned long next, struct mm_walk *walk)
+{
+ struct numa_sample_walk *sw = walk->private;
+ struct vm_area_struct *vma = walk->vma;
+ pte_t *start_pte, *pte, ptent;
+ struct folio *folio;
+ spinlock_t *ptl;
+
+ ptl = pmd_trans_huge_lock(pmd, vma);
+ if (ptl) {
+ pmd_t pmdval = pmdp_get(pmd);
+
+ folio = pmd_present(pmdval) ?
+ vm_normal_folio_pmd(vma, addr, pmdval) : NULL;
+ if (folio && folio_maybe_dma_pinned(folio) &&
+ pmdp_test_and_clear_young(vma, addr, pmd)) {
+ sw->node_pages[folio_nid(folio)] += HPAGE_PMD_NR;
+ sw->nr_young += HPAGE_PMD_NR;
+ }
+ spin_unlock(ptl);
+ return 0;
+ }
+
+ pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
+ if (!pte)
+ return 0;
+ start_pte = pte;
+
+ for (; addr != next; pte++, addr += PAGE_SIZE) {
+ ptent = ptep_get(pte);
+ if (!pte_present(ptent))
+ continue;
+
+ folio = vm_normal_folio(vma, addr, ptent);
+ if (!folio || !folio_maybe_dma_pinned(folio))
+ continue;
+
+ if (ptep_test_and_clear_young(vma, addr, pte)) {
+ sw->node_pages[folio_nid(folio)]++;
+ sw->nr_young++;
+ }
+ }
+ pte_unmap_unlock(start_pte, ptl);
+ cond_resched();
+
+ return 0;
+}
+
+static const struct mm_walk_ops numa_sample_ops = {
+ .pud_entry = numa_sample_pud_entry,
+ .pmd_entry = numa_sample_pmd_entry,
+ .walk_lock = PGWALK_RDLOCK,
+};
+
+/**
+ * numa_sample_range() - Sample accesses to memory that takes no hinting faults
+ * @vma: The VMA to sample.
+ * @start: Start of the range.
+ * @end: End of the range.
+ * @node_pages: Per-node counters, indexed by node id.
+ *
+ * Test and clear the accessed bit of pinned folios and add the pages found
+ * accessed to @node_pages for the node they reside on.
+ *
+ * Return: The number of pages found accessed.
+ */
+unsigned long numa_sample_range(struct vm_area_struct *vma, unsigned long start,
+ unsigned long end, unsigned long *node_pages)
+{
+ struct numa_sample_walk sw = { .node_pages = node_pages };
+
+ /*
+ * Like the MGLRU page table walk, clear the accessed bits without flushing
+ * the TLB: a stale TLB entry only hides some accesses until it is evicted,
+ * and a flush here would interrupt every CPU running the mm.
+ */
+ walk_page_range_vma(vma, start, end, &numa_sample_ops, &sw);
+
+ return sw.nr_young;
+}
#endif /* CONFIG_NUMA_BALANCING */
static int queue_pages_test_walk(unsigned long start, unsigned long end,
--
2.43.0
next prev parent reply other threads:[~2026-12-06 11:38 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 ` Qiliang Yuan [this message]
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 ` [PATCH 4/8] mm/mempolicy: consult secondary MMUs when sampling accessed bits Qiliang Yuan
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-1-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®