* [PATCH 0/8] sched/numa: Placement signal for memory that NUMA balancing cannot migrate
@ 2026-12-06 11:38 Qiliang Yuan
2026-12-06 11:38 ` [PATCH 1/8] sched/numa: Sample the accessed bits of pinned memory Qiliang Yuan
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: Qiliang Yuan @ 2026-12-06 11:38 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Juri Lelli, Vincent Guittot,
Mel Gorman, K Prateek Nayak, David Hildenbrand, Andrew Morton
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Valentin Schneider,
Zi Yan, Gregory Price, Ying Huang, Kefeng Wang, linux-mm,
linux-kernel, Qiliang Yuan
NUMA balancing finds out where the memory of a task lives through
hinting faults, so it has nothing to go on for memory it never makes
fault: DMA pinned folios (VFIO, RDMA, io_uring registered buffers),
hugetlb pages and DAX mappings. A task working on such memory gets no
preferred node from it, and the load balancer moves it wherever CPUs are
idle, often to the other package, where every access stays remote
because the memory cannot follow.
This matters most on machines with several NUMA nodes per package, where
every die has its own memory controller (SLIT 10/15/25/27 for 2 packages
of 4 dies), and for workloads that pin or use hugetlb memory by design:
VMs with hugetlb and VFIO, RDMA and io_uring users, DAX.
Patches 1-4 sample the accessed bits of such memory from
task_numa_work() instead of making it fault: no hinting PTE is installed
and nothing is migrated, which keeps the reason for commit 03aa8e4f2732
("mm: mprotect: always skip dma pinned folio in prot_numa_skip()"), and
the accessed pages are accounted as faults on their node. Secondary MMUs
are tested, not cleared. Patches 5-7 record when most faults of a task
hit memory that cannot move, and keep such tasks inside the lowest NUMA
domain around their preferred node, in NUMA placement and in load
balancing alike; within that domain they are still spread as usual.
Patch 8 shares the samples between the threads of a process, so that all
of them get the signal and the protection, not only the one that ran the
scan.
Tested in a VM with 2 packages of 4 nodes (8 vCPUs and 4 LLCs per node,
SLIT 10/15/25/27, guest memory on host huge pages), all numbers from one
run alternating the kernels. Single tasks copying within 3-4 GiB of
pinned, hugetlb or DAX memory on another node (patches 1-3) settle on it
in every run, and the slowest of 6-9 runs copies 43-52% faster. A nested
guest copying in the part of its hugetlb memory on the other package
follows it there in every run and copies 35% faster (patch 4). Patches
5-7 act only on threads that have samples of their own, so in
multi-threaded processes their effect shows together with patch 8.
For processes where each thread pins 1 GiB on node 0 or maps it from
hugetlb and times 2^20 dependent loads through it starting from node 4,
the table compares mainline with the whole series: median (range) of
per-thread means over 3 alternating rounds of 25 s, cache-aware
scheduling off; "worst" is the slowest thread. Copy loops over the same
memory, bound by bandwidth, change by -14% to +0%, and hackbench with 16
groups takes 0.957 s against 0.959 s. With cache-aware scheduling on,
the medians change by -34%, -38%, -9% and -26% and the worst threads by
-49%, -36%, -43% and -42%, with 92-95% of run time on the package:
before after median worst on package (%)
threads chase (ms) chase (ms) change change before after
pinned 4 211 (111-229) 122 (117-130) -42% -43% 31 96
pinned 8 212 (122-231) 142 (131-149) -33% -35% 41 94
hugetlb 4 126 (115-222) 115 (113-125) -9% -44% 62 96
hugetlb 8 132 (116-223) 124 (116-130) -6% -42% 61 92
Cache-aware scheduling (CONFIG_SCHED_CACHE) aggregates the threads of a
process in one LLC by their CPU time and steps aside for processes whose
footprint exceeds the LLC; this series is about which node holds DRAM
the task uses, typically gigabytes. They meet in can_migrate_task(),
where migrate_strands_memory() is checked before migrate_degrades_llc().
Tried and dropped:
- Packing such tasks onto the memory node by allowing an imbalance up to
imb_numa_nr instead of NUMA_IMBALANCE_MIN. On top of this series it
leaves pointer chasing within 2% and makes copy loops 10-19% slower.
- Clearing accessed bits of secondary MMUs instead of testing them: the
same placement and copy times (133 ms against 134 ms) when the L1
memory is backed by huge pages, but with a 4K-backed L1 copy rounds of
about 285 ms in the L2 guest took 406-841 ms every 2.5-4.8 s.
- Device memory mappings (VM_IO/VM_PFNMAP, e.g. VFIO BARs) through a new
vm_operations_struct callback: implemented, not pursued.
Limitations:
- Measured in VMs only, on an AMD host; same-package remote latency
there is 5-13% over local.
- Threads that share pinned memory cannot be told apart, so the samples
are split evenly: when each thread uses memory on a different node,
every thread gets the same mixed signal.
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
Qiliang Yuan (8):
sched/numa: Sample the accessed bits of pinned memory
sched/numa: Cover hugetlb mappings with accessed-bit sampling
sched/numa: Extend accessed-bit sampling to DAX mappings
mm/mempolicy: consult secondary MMUs when sampling accessed bits
sched/numa: Track whether the memory of a task can follow it
sched/numa: Keep tasks near memory that cannot follow them
sched/fair: Avoid pulling tasks away from memory that cannot follow them
sched/numa: Share sampled accesses between the threads of a process
include/linux/mm.h | 3 +
include/linux/mm_types.h | 10 ++
include/linux/sched.h | 11 ++
include/linux/sched/numa_balancing.h | 1 +
kernel/fork.c | 7 ++
kernel/sched/fair.c | 218 ++++++++++++++++++++++++++++++++++-
mm/mempolicy.c | 154 +++++++++++++++++++++++++
7 files changed, 400 insertions(+), 4 deletions(-)
---
base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
change-id: 20261201-feat-numab-series-56b943ff20f5
Best regards,
--
Qiliang Yuan <qiliang.yuan@linux.dev>
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 1/8] sched/numa: Sample the accessed bits of pinned memory
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
2026-12-06 11:38 ` [PATCH 2/8] sched/numa: Cover hugetlb mappings with accessed-bit sampling Qiliang Yuan
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Qiliang Yuan @ 2026-12-06 11:38 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Juri Lelli, Vincent Guittot,
Mel Gorman, K Prateek Nayak, David Hildenbrand, Andrew Morton
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Valentin Schneider,
Zi Yan, Gregory Price, Ying Huang, Kefeng Wang, linux-mm,
linux-kernel, Qiliang Yuan
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
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 2/8] sched/numa: Cover hugetlb mappings with accessed-bit sampling
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 ` Qiliang Yuan
2026-12-06 11:38 ` [PATCH 3/8] sched/numa: Extend accessed-bit sampling to DAX mappings Qiliang Yuan
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Qiliang Yuan @ 2026-12-06 11:38 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Juri Lelli, Vincent Guittot,
Mel Gorman, K Prateek Nayak, David Hildenbrand, Andrew Morton
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Valentin Schneider,
Zi Yan, Gregory Price, Ying Huang, Kefeng Wang, linux-mm,
linux-kernel, Qiliang Yuan
task_numa_work() skips hugetlb VMAs entirely: hugetlb pages are not
migrated by NUMA balancing, so there is no point in making them fault.
Guest memory of virtual machines is commonly backed by hugetlb pages.
As with pinned memory, skipping them leaves the scheduler blind to where
that memory lives. A vCPU or any other task working on hugetlb memory
gets no preferred node from it and can stay on the far side of the
machine indefinitely.
Sample hugetlb VMAs instead: test and clear the accessed bit of each
present huge page in the scanned range and account the accessed pages as
faults on their node, without changing the protection. Such sample-only
ranges are charged to the virtual scan budget only, like ranges where no
PTE was updated, so they do not slow down the scanning of migratable
memory.
A task copying within 4 GiB of 2 MiB hugetlb pages 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 244 to 248 ms anywhere
in the package of the memory and 394 to 520 ms on the other one. With
the patch every run settled on node 0, within 3.4 to 5.6 s:
before after change
runs settled on node 0 0/9 9/9
runs ending off package 5/9 0/9
slowest run (ms) 520 248 -52%
median run (ms) 394 (245-520) 246 (244-248) -38%
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
kernel/sched/fair.c | 20 +++++++++++++-------
mm/mempolicy.c | 34 ++++++++++++++++++++++++++++++++--
2 files changed, 45 insertions(+), 9 deletions(-)
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 14a4a15ac2747..eaf8b77ecb6ac 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -4292,6 +4292,7 @@ static void task_numa_work(struct callback_head *work)
bool vma_pids_forced = false;
unsigned long *node_pages = NULL;
unsigned long nr_sampled;
+ bool sample_only;
int nid;
WARN_ON_ONCE(p != container_of(work, struct task_struct, numa_work));
@@ -4350,9 +4351,8 @@ 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);
+ node_pages = kcalloc(nr_node_ids, sizeof(*node_pages),
+ GFP_KERNEL | __GFP_NOWARN);
if (!mmap_read_trylock(mm)) {
kfree(node_pages);
@@ -4378,8 +4378,11 @@ static void task_numa_work(struct callback_head *work)
}
for (; vma; vma = vma_next(&vmi)) {
- if (!vma_migratable(vma) || !vma_policy_mof(vma) ||
- vma_is_hugetlb(vma) || !vma_is_mm_managed(vma)) {
+ /* Hugetlb pages are not migrated but their accesses are sampled. */
+ sample_only = node_pages && vma_is_hugetlb(vma);
+ if (!sample_only &&
+ (!vma_migratable(vma) || !vma_policy_mof(vma) ||
+ vma_is_hugetlb(vma) || !vma_is_mm_managed(vma))) {
trace_sched_skip_vma_numa(mm, vma, NUMAB_SKIP_UNSUITABLE);
continue;
}
@@ -4475,9 +4478,12 @@ static void task_numa_work(struct callback_head *work)
start = max(start, vma->vm_start);
end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
end = min(end, vma->vm_end);
- nr_pte_updates = change_prot_numa(vma, start, end);
+ nr_pte_updates = 0;
nr_sampled = 0;
- if (node_pages)
+ if (!sample_only)
+ nr_pte_updates = change_prot_numa(vma, start, end);
+ if (sample_only || (node_pages &&
+ mm_flags_test(MMF_HAS_PINNED, mm)))
nr_sampled = numa_sample_range(vma, start, end,
node_pages);
if (nr_sampled)
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index d11c38828ee87..fcdcf69f8e6c7 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -996,9 +996,39 @@ static int numa_sample_pmd_entry(pmd_t *pmd, unsigned long addr,
return 0;
}
+#ifdef CONFIG_HUGETLB_PAGE
+static int numa_sample_hugetlb_entry(pte_t *pte, unsigned long hmask,
+ unsigned long addr, unsigned long end,
+ struct mm_walk *walk)
+{
+ struct hstate *h = hstate_vma(walk->vma);
+ struct numa_sample_walk *sw = walk->private;
+ unsigned long psize = huge_page_size(h);
+ spinlock_t *ptl;
+ 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)) {
+ 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);
+ }
+ spin_unlock(ptl);
+
+ return 0;
+}
+#else
+#define numa_sample_hugetlb_entry NULL
+#endif
+
static const struct mm_walk_ops numa_sample_ops = {
.pud_entry = numa_sample_pud_entry,
.pmd_entry = numa_sample_pmd_entry,
+ .hugetlb_entry = numa_sample_hugetlb_entry,
.walk_lock = PGWALK_RDLOCK,
};
@@ -1009,8 +1039,8 @@ static const struct mm_walk_ops numa_sample_ops = {
* @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.
+ * 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.
*
* Return: The number of pages found accessed.
*/
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 3/8] sched/numa: Extend accessed-bit sampling to DAX mappings
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 ` Qiliang Yuan
2026-12-06 11:38 ` [PATCH 4/8] mm/mempolicy: consult secondary MMUs when sampling accessed bits Qiliang Yuan
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Qiliang Yuan @ 2026-12-06 11:38 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Juri Lelli, Vincent Guittot,
Mel Gorman, K Prateek Nayak, David Hildenbrand, Andrew Morton
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Valentin Schneider,
Zi Yan, Gregory Price, Ying Huang, Kefeng Wang, linux-mm,
linux-kernel, Qiliang Yuan
DAX mappings point straight at persistent memory or other device memory.
Their pages are not on the LRU and are never migrated, and
vma_migratable() makes task_numa_work() skip such VMAs.
Where that memory sits still matters to the task using it. A task
working on a DAX file whose device is attached to one node gets no
placement signal from it and may run on the other package for good.
Treat DAX VMAs like hugetlb ones: sample every folio in the range rather
than only pinned ones, and account the pages found accessed as faults on
the node of the device memory, without changing the protection.
A task copying within a 3 GiB file on ext4 mounted with dax=always on
emulated pmem (memmap=4G!8G) of node 0 in 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 4, 3 rounds of 2 runs of 40 s
alternating with the kernel without this patch; time of the last copy.
With the patch every run settled on node 0, within 1.4 to 1.8 s:
before after change
runs settled on node 0 0/6 6/6
runs ending off package 6/6 0/6
slowest run (ms) 362 189 -48%
median run (ms) 298 (292-362) 186 (183-189) -38%
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
include/linux/mm.h | 3 ++-
kernel/sched/fair.c | 11 ++++++++---
mm/mempolicy.c | 16 ++++++++++++----
3 files changed, 22 insertions(+), 8 deletions(-)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index d887d74d54975..df33d4519fae0 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -5030,7 +5030,8 @@ void vma_set_file(struct vm_area_struct *vma, struct file *file);
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);
+ unsigned long end, unsigned long *node_pages,
+ bool all);
#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 eaf8b77ecb6ac..520728ada00fb 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -4378,8 +4378,12 @@ static void task_numa_work(struct callback_head *work)
}
for (; vma; vma = vma_next(&vmi)) {
- /* Hugetlb pages are not migrated but their accesses are sampled. */
- sample_only = node_pages && vma_is_hugetlb(vma);
+ /*
+ * Hugetlb and DAX pages are not migrated but their accesses are
+ * sampled.
+ */
+ sample_only = node_pages &&
+ (vma_is_hugetlb(vma) || vma_is_dax(vma));
if (!sample_only &&
(!vma_migratable(vma) || !vma_policy_mof(vma) ||
vma_is_hugetlb(vma) || !vma_is_mm_managed(vma))) {
@@ -4485,7 +4489,8 @@ static void task_numa_work(struct callback_head *work)
if (sample_only || (node_pages &&
mm_flags_test(MMF_HAS_PINNED, mm)))
nr_sampled = numa_sample_range(vma, start, end,
- node_pages);
+ node_pages,
+ sample_only);
if (nr_sampled)
vma_set_access_pid_bit(vma);
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index fcdcf69f8e6c7..e69a4b38b136e 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -935,8 +935,14 @@ unsigned long change_prot_numa(struct vm_area_struct *vma,
struct numa_sample_walk {
unsigned long *node_pages;
unsigned long nr_young;
+ bool all;
};
+static bool numa_sample_folio(struct numa_sample_walk *sw, struct folio *folio)
+{
+ return folio && (sw->all || folio_maybe_dma_pinned(folio));
+}
+
static int numa_sample_pud_entry(pud_t *pud, unsigned long addr,
unsigned long next, struct mm_walk *walk)
{
@@ -962,7 +968,7 @@ 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 (folio && folio_maybe_dma_pinned(folio) &&
+ 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;
@@ -982,7 +988,7 @@ static int numa_sample_pmd_entry(pmd_t *pmd, unsigned long addr,
continue;
folio = vm_normal_folio(vma, addr, ptent);
- if (!folio || !folio_maybe_dma_pinned(folio))
+ if (!numa_sample_folio(sw, folio))
continue;
if (ptep_test_and_clear_young(vma, addr, pte)) {
@@ -1038,6 +1044,7 @@ static const struct mm_walk_ops numa_sample_ops = {
* @start: Start of the range.
* @end: End of the range.
* @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.
@@ -1045,9 +1052,10 @@ static const struct mm_walk_ops numa_sample_ops = {
* 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)
+ unsigned long end, unsigned long *node_pages,
+ bool all)
{
- struct numa_sample_walk sw = { .node_pages = node_pages };
+ struct numa_sample_walk sw = { .node_pages = node_pages, .all = all };
/*
* Like the MGLRU page table walk, clear the accessed bits without flushing
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 4/8] mm/mempolicy: consult secondary MMUs when sampling accessed bits
2026-12-06 11:38 [PATCH 0/8] sched/numa: Placement signal for memory that NUMA balancing cannot migrate Qiliang Yuan
` (2 preceding siblings ...)
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
2026-12-06 11:38 ` [PATCH 5/8] sched/numa: Track whether the memory of a task can follow it Qiliang Yuan
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Qiliang Yuan @ 2026-12-06 11:38 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Juri Lelli, Vincent Guittot,
Mel Gorman, K Prateek Nayak, David Hildenbrand, Andrew Morton
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Valentin Schneider,
Zi Yan, Gregory Price, Ying Huang, Kefeng Wang, linux-mm,
linux-kernel, Qiliang Yuan
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
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 5/8] sched/numa: Track whether the memory of a task can follow it
2026-12-06 11:38 [PATCH 0/8] sched/numa: Placement signal for memory that NUMA balancing cannot migrate Qiliang Yuan
` (3 preceding siblings ...)
2026-12-06 11:38 ` [PATCH 4/8] mm/mempolicy: consult secondary MMUs when sampling accessed bits Qiliang Yuan
@ 2026-12-06 11:38 ` Qiliang Yuan
2026-12-06 11:38 ` [PATCH 6/8] sched/numa: Keep tasks near memory that cannot follow them Qiliang Yuan
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Qiliang Yuan @ 2026-12-06 11:38 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Juri Lelli, Vincent Guittot,
Mel Gorman, K Prateek Nayak, David Hildenbrand, Andrew Morton
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Valentin Schneider,
Zi Yan, Gregory Price, Ying Huang, Kefeng Wang, linux-mm,
linux-kernel, Qiliang Yuan
Placement and load balancing for NUMA assume that memory follows a task:
moving a task off its preferred node costs locality only until hinting
faults migrate its pages over.
Memory that is pinned, hugetlb or DAX backed, and memory whose migration
failed, stays where it is, so moving its user away keeps every access
remote. Nothing tells placement and load balancing which tasks are in
that situation.
Record that: sampled accesses are now accounted with a new
TNF_UNMOVABLE flag, and faults with TNF_UNMOVABLE or TNF_MIGRATE_FAIL
add to numa_faults_stuck. At the end of each scan period, when
update_task_scan_period() resets the fault locality counters, fold the
period into sums of such faults and of all faults that are halved every
period like numa_faults, and set numa_mem_stuck while the former are the
majority. The counters of one period are not enough: they are reset
right before placement reads them, and samples arrive with the scans,
so some periods see none and would flip the verdict. Nothing uses
numa_mem_stuck yet.
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
include/linux/sched.h | 7 +++++++
include/linux/sched/numa_balancing.h | 1 +
kernel/sched/fair.c | 17 +++++++++++++++--
3 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e4f6323d78555..6b10159dc34de 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1434,6 +1434,13 @@ struct task_struct {
* weights depending on whether they were shared or private faults
*/
unsigned long numa_faults_locality[3];
+ /* Faults on memory that cannot migrate, reset with numa_faults_locality */
+ unsigned long numa_faults_stuck;
+ /* Decayed counts of unmovable and of all faults over scan periods */
+ unsigned long numa_stuck_avg;
+ unsigned long numa_total_avg;
+ /* Most faults of the last scan period hit memory that cannot move */
+ bool numa_mem_stuck;
unsigned long numa_pages_migrated;
#endif /* CONFIG_NUMA_BALANCING */
diff --git a/include/linux/sched/numa_balancing.h b/include/linux/sched/numa_balancing.h
index 52b22c5c396db..35cdb3db98276 100644
--- a/include/linux/sched/numa_balancing.h
+++ b/include/linux/sched/numa_balancing.h
@@ -14,6 +14,7 @@
#define TNF_SHARED 0x04
#define TNF_FAULT_LOCAL 0x08
#define TNF_MIGRATE_FAIL 0x10
+#define TNF_UNMOVABLE 0x20
enum numa_vmaskip_reason {
NUMAB_SKIP_UNSUITABLE,
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 520728ada00fb..ea483c8dabcf9 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -3710,8 +3710,15 @@ static void update_task_scan_period(struct task_struct *p,
task_scan_min(p), task_scan_max(p));
out:
+ /* Decay like numa_faults so that one quiet period cannot flip it */
+ p->numa_stuck_avg = p->numa_stuck_avg / 2 + p->numa_faults_stuck;
+ p->numa_total_avg = p->numa_total_avg / 2 +
+ p->numa_faults_locality[0] +
+ p->numa_faults_locality[1];
+ p->numa_mem_stuck = p->numa_stuck_avg * 2 > p->numa_total_avg;
memset(p->numa_faults_locality, 0,
sizeof(p->numa_faults_locality));
+ p->numa_faults_stuck = 0;
}
/*
@@ -4175,6 +4182,10 @@ void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags)
p->total_numa_faults = 0;
memset(p->numa_faults_locality, 0, sizeof(p->numa_faults_locality));
+ p->numa_faults_stuck = 0;
+ p->numa_stuck_avg = 0;
+ p->numa_total_avg = 0;
+ p->numa_mem_stuck = false;
}
/*
@@ -4214,6 +4225,8 @@ void task_numa_fault(int last_cpupid, int mem_node, int pages, int flags)
p->numa_pages_migrated += pages;
if (flags & TNF_MIGRATE_FAIL)
p->numa_faults_locality[2] += pages;
+ if (flags & (TNF_MIGRATE_FAIL | TNF_UNMOVABLE))
+ p->numa_faults_stuck += pages;
p->numa_faults[task_faults_idx(NUMA_MEMBUF, mem_node, priv)] += pages;
p->numa_faults[task_faults_idx(NUMA_CPUBUF, cpu_node, priv)] += pages;
@@ -4553,8 +4566,8 @@ static void task_numa_work(struct callback_head *work)
if (!node_pages[nid])
continue;
task_numa_fault(-1 & LAST_CPUPID_MASK, nid,
- node_pages[nid],
- nid == task_node(p) ? TNF_FAULT_LOCAL : 0);
+ node_pages[nid], TNF_UNMOVABLE |
+ (nid == task_node(p) ? TNF_FAULT_LOCAL : 0));
}
kfree(node_pages);
}
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 6/8] sched/numa: Keep tasks near memory that cannot follow them
2026-12-06 11:38 [PATCH 0/8] sched/numa: Placement signal for memory that NUMA balancing cannot migrate Qiliang Yuan
` (4 preceding siblings ...)
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 ` 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
7 siblings, 0 replies; 9+ messages in thread
From: Qiliang Yuan @ 2026-12-06 11:38 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Juri Lelli, Vincent Guittot,
Mel Gorman, K Prateek Nayak, David Hildenbrand, Andrew Morton
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Valentin Schneider,
Zi Yan, Gregory Price, Ying Huang, Kefeng Wang, linux-mm,
linux-kernel, Qiliang Yuan
When task_numa_migrate() finds no room on the preferred node of a task,
it tries the other nodes in turn and may pick one on another package.
For a task whose memory follows it that only delays the convergence.
Memory that cannot migrate stays behind, though, and every access from
the other package then crosses the link between the packages.
Skip candidate nodes outside the lowest NUMA domain around the preferred
node for tasks whose memory was found to be stuck, and do not swap such
a task out of that domain to make room for another one.
task_numa_migrate() already takes its imbalance settings from that
domain, sd_numa; with the distances firmware reports for multi-die
packages it spans the package of the memory. Nodes inside it are still
used to spread such tasks, so they do not all crowd onto the node of
their memory.
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
kernel/sched/fair.c | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index ea483c8dabcf9..7fc5845324f4f 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -3197,6 +3197,26 @@ static bool load_too_imbalanced(long src_load, long dst_load,
* into account that it might be best if task running on the dst_cpu should
* be exchanged with the source task
*/
+/*
+ * Return true if @nid lies inside the lowest NUMA domain around @pref_nid, the
+ * one task_numa_migrate() balances in. It covers the nodes at the first remote
+ * distance of sched_domains_numa_distance[], which is the package when the
+ * SLIT puts the nodes of a package at that distance.
+ */
+static bool numa_node_nearby(int pref_nid, int nid)
+{
+ int cpu = cpumask_first(cpumask_of_node(pref_nid));
+ struct sched_domain *sd;
+
+ if (cpu >= nr_cpu_ids)
+ return true;
+
+ guard(rcu)();
+ sd = rcu_dereference_all(per_cpu(sd_numa, cpu));
+ return !sd || cpumask_intersects(sched_domain_span(sd),
+ cpumask_of_node(nid));
+}
+
static bool task_numa_compare(struct task_numa_env *env,
long taskimp, long groupimp, bool maymove)
{
@@ -3239,6 +3259,11 @@ static bool task_numa_compare(struct task_numa_env *env,
if (!cpumask_test_cpu(env->src_cpu, cur->cpus_ptr))
goto unlock;
+ /* Nor if the swap would take it away from memory that cannot follow */
+ if (cur->numa_mem_stuck && cur->numa_preferred_nid != NUMA_NO_NODE &&
+ !numa_node_nearby(cur->numa_preferred_nid, env->src_nid))
+ goto unlock;
+
/*
* Skip this swap candidate if it is not moving to its preferred
* node and the best task is.
@@ -3514,6 +3539,11 @@ static int task_numa_migrate(struct task_struct *p)
if (nid == env.src_nid || nid == p->numa_preferred_nid)
continue;
+ /* Keep a task whose memory cannot move near that memory */
+ if (p->numa_mem_stuck &&
+ !numa_node_nearby(p->numa_preferred_nid, nid))
+ continue;
+
dist = node_distance(env.src_nid, env.dst_nid);
if (sched_numa_topology_type == NUMA_BACKPLANE &&
dist != env.dist) {
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 7/8] sched/fair: Avoid pulling tasks away from memory that cannot follow them
2026-12-06 11:38 [PATCH 0/8] sched/numa: Placement signal for memory that NUMA balancing cannot migrate Qiliang Yuan
` (5 preceding siblings ...)
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 ` Qiliang Yuan
2026-12-06 11:38 ` [PATCH 8/8] sched/numa: Share sampled accesses between the threads of a process Qiliang Yuan
7 siblings, 0 replies; 9+ messages in thread
From: Qiliang Yuan @ 2026-12-06 11:38 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Juri Lelli, Vincent Guittot,
Mel Gorman, K Prateek Nayak, David Hildenbrand, Andrew Morton
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Valentin Schneider,
Zi Yan, Gregory Price, Ying Huang, Kefeng Wang, linux-mm,
linux-kernel, Qiliang Yuan
The load balancer evens out busy CPUs across NUMA nodes and only
reluctantly moves a task away from its preferred node:
migrate_degrades_locality() makes such a task cache hot, but once a
domain has failed to balance more often than cache_nice_tries the move
happens anyway, and active balancing does not ask at all.
For a task whose memory cannot follow it, a move to another package is
not undone by migration, so the task keeps paying for remote accesses or
is pulled back by NUMA placement only to be pushed out again.
Refuse to pull a task whose memory was found stuck out of the lowest
NUMA domain around its preferred node. A task that shares its CPU may
still leave, but only when no CPU of that domain is idle to take it
instead. Moves within the domain are left to the usual rules. The check
comes before the active balance shortcut in can_migrate_task(), so both
regular and active balancing honour it, consistent with the placement
side, and sched_balance_rq() does not wake the stopper for an active
balance that would be refused.
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
kernel/sched/fair.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 7fc5845324f4f..515775a8db6ee 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -10823,7 +10823,53 @@ static long migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
return src_weight - dst_weight;
}
+/* Return true if @p could run on an idle CPU in the lowest NUMA domain of @cpu */
+static bool numa_nearby_cpu_idle(struct task_struct *p, int cpu)
+{
+ struct sched_domain *sd;
+ int i;
+
+ sd = rcu_dereference_all(per_cpu(sd_numa, cpu));
+ if (!sd)
+ return false;
+
+ for_each_cpu_and(i, sched_domain_span(sd), p->cpus_ptr) {
+ if (idle_cpu(i))
+ return true;
+ }
+ return false;
+}
+
+/*
+ * Pulling a task whose memory cannot follow it out of the NUMA neighbourhood of
+ * that memory puts every access on a remote link. Keep it there unless its CPU
+ * is contended and no CPU in the neighbourhood is idle to take it instead;
+ * moves within the neighbourhood are left to the usual rules.
+ */
+static bool migrate_strands_memory(struct task_struct *p, struct lb_env *env)
+{
+ int pref = p->numa_preferred_nid;
+
+ if (!static_branch_likely(&sched_numa_balancing) ||
+ !(env->sd->flags & SD_NUMA) || !p->numa_mem_stuck ||
+ pref == NUMA_NO_NODE)
+ return false;
+
+ if (!numa_node_nearby(pref, cpu_to_node(env->src_cpu)) ||
+ numa_node_nearby(pref, cpu_to_node(env->dst_cpu)))
+ return false;
+
+ return env->src_rq->cfs.h_nr_runnable <= 1 ||
+ numa_nearby_cpu_idle(p, env->src_cpu);
+}
+
#else /* !CONFIG_NUMA_BALANCING: */
+static inline bool migrate_strands_memory(struct task_struct *p,
+ struct lb_env *env)
+{
+ return false;
+}
+
static inline long migrate_degrades_locality(struct task_struct *p,
struct lb_env *env)
{
@@ -11298,6 +11344,9 @@ int can_migrate_task(struct task_struct *p, struct lb_env *env)
* 3) task is cache cold, or
* 4) too many balance attempts have failed.
*/
+ if (migrate_strands_memory(p, env))
+ return 0;
+
if (env->flags & LBF_ACTIVE_LB)
return !migrate_llc_task_wrong_dst(p, env);
@@ -14033,6 +14082,10 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq,
if (!cpumask_test_cpu(this_cpu, busiest->curr->cpus_ptr))
goto out_one_pinned;
+ /* Nor if can_migrate_task() would refuse to strand its memory */
+ if (migrate_strands_memory(busiest->curr, &env))
+ goto out_one_pinned;
+
/* Record that we found at least one task that could run on this_cpu */
env.flags &= ~LBF_ALL_PINNED;
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 8/8] sched/numa: Share sampled accesses between the threads of a process
2026-12-06 11:38 [PATCH 0/8] sched/numa: Placement signal for memory that NUMA balancing cannot migrate Qiliang Yuan
` (6 preceding siblings ...)
2026-12-06 11:38 ` [PATCH 7/8] sched/fair: Avoid pulling tasks away from " Qiliang Yuan
@ 2026-12-06 11:38 ` Qiliang Yuan
7 siblings, 0 replies; 9+ messages in thread
From: Qiliang Yuan @ 2026-12-06 11:38 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Juri Lelli, Vincent Guittot,
Mel Gorman, K Prateek Nayak, David Hildenbrand, Andrew Morton
Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Valentin Schneider,
Zi Yan, Gregory Price, Ying Huang, Kefeng Wang, linux-mm,
linux-kernel, Qiliang Yuan
Sampled accesses are accounted to the task that happens to run
task_numa_work(), and only one thread of a process scans at a time.
In a multi-threaded process the samples of the whole address space
therefore go to whichever thread won the scan. The threads that actually
use the memory get little or nothing, so they get no preferred node from
it and are never found to have stuck memory, and the previous patches do
not keep them near it.
Accessed bits do not tell which thread touched a page, so the best that
can be done is to share the samples. Keep the per-node counts in the mm,
let every thread remember how much of them it has folded in, and give
each thread an equal share of what it has not seen yet. A scan that
found accessed pages bumps a sequence count, which makes
task_tick_numa() queue task_numa_work() for the other threads too so
they fold their share in without waiting for their own scan. A new
thread starts from the current counts rather than claiming earlier
samples. A thread that has not folded for longer than one scan takes at
most what one scan can sample and drops the rest, and exec drops the
snapshot together with the old mm.
In 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), each thread of a
process pins 1 GiB on node 0 as an io_uring buffer or maps it from
hugetlb, then times 2^20 dependent loads through it, starting from node
4. Per-thread means of the second half of 25 s runs, median (range) over
3 rounds alternating with the kernel without this patch; "worst" is the
slowest thread, "on package" the share of run time spent on the package
of the memory:
before after median worst on package (%)
threads chase (ms) chase (ms) change change before after
pinned 4 124 (115-220) 122 (117-130) -2% -41% 79 96
pinned 8 143 (127-229) 142 (131-149) -1% -35% 73 94
hugetlb 4 120 (113-217) 115 (113-125) -5% -42% 81 96
hugetlb 8 128 (116-223) 124 (116-130) -2% -42% 66 92
Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
include/linux/mm.h | 2 +-
include/linux/mm_types.h | 10 ++++
include/linux/sched.h | 4 ++
kernel/fork.c | 7 +++
kernel/sched/fair.c | 117 +++++++++++++++++++++++++++++++++++++++--------
mm/mempolicy.c | 11 +++--
6 files changed, 125 insertions(+), 26 deletions(-)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index df33d4519fae0..938749ff2185c 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -5030,7 +5030,7 @@ void vma_set_file(struct vm_area_struct *vma, struct file *file);
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,
+ unsigned long end, atomic_long_t *node_pages,
bool all);
#endif
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 1ed35d258d2e3..5acae2c21b36a 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -1398,6 +1398,16 @@ struct mm_struct {
/* numa_scan_seq prevents two threads remapping PTEs. */
int numa_scan_seq;
+
+ /*
+ * Pages found accessed by sampling accessed bits, per node.
+ * Only ever grows; each thread folds what it has not seen yet
+ * into its own NUMA statistics.
+ */
+ atomic_long_t *numa_sample_pages;
+
+ /* Bumped by every scan that added to numa_sample_pages. */
+ int numa_sample_seq;
#endif
/*
* An operation with batched TLB flushing is going on. Anything
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 6b10159dc34de..3967d84370587 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1427,6 +1427,10 @@ struct task_struct {
unsigned long *numa_faults;
unsigned long total_numa_faults;
+ /* mm->numa_sample_pages as of the last time this task folded it in */
+ unsigned long *numa_sample_seen;
+ int numa_sample_seq;
+
/*
* numa_faults_locality tracks if faults recorded during the last
* scan window were remote/local or failed to migrate. The task scan
diff --git a/kernel/fork.c b/kernel/fork.c
index d55a7d21a8fb2..86b50bf070d9d 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -731,6 +731,9 @@ void __mmdrop(struct mm_struct *mm)
WARN_ON_ONCE(mm == current->active_mm);
mm_destroy_sched(mm);
+#ifdef CONFIG_NUMA_BALANCING
+ kfree(mm->numa_sample_pages);
+#endif
mm_free_pgd(mm);
mm_free_id(mm);
destroy_context(mm);
@@ -1104,6 +1107,10 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
spin_lock_init(&mm->page_table_lock);
spin_lock_init(&mm->arg_lock);
mm_init_cpumask(mm);
+#ifdef CONFIG_NUMA_BALANCING
+ mm->numa_sample_pages = NULL;
+ mm->numa_sample_seq = 0;
+#endif
mm_init_aio(mm);
mm_init_owner(mm, p);
mm_pasid_init(mm);
diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index 515775a8db6ee..4809c06c4de6e 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -4148,6 +4148,10 @@ void task_numa_free(struct task_struct *p, bool final)
unsigned long flags;
int i;
+ /* Also on exec: the snapshot belongs to the old mm */
+ kfree(p->numa_sample_seen);
+ p->numa_sample_seen = NULL;
+
if (!numa_faults)
return;
@@ -4316,6 +4320,81 @@ static bool vma_is_accessed(struct mm_struct *mm, struct vm_area_struct *vma)
#define VMA_PID_RESET_PERIOD (4 * sysctl_numa_balancing_scan_delay)
+static atomic_long_t *mm_numa_sample_pages(struct mm_struct *mm)
+{
+ atomic_long_t *pages = READ_ONCE(mm->numa_sample_pages);
+
+ if (pages)
+ return pages;
+
+ pages = kcalloc(nr_node_ids, sizeof(*pages), GFP_KERNEL | __GFP_NOWARN);
+ if (!pages)
+ return NULL;
+
+ if (cmpxchg(&mm->numa_sample_pages, NULL, pages)) {
+ kfree(pages);
+ pages = READ_ONCE(mm->numa_sample_pages);
+ }
+
+ return pages;
+}
+
+/*
+ * Fold the accesses sampled in @mm since the last call into the statistics of
+ * @p, as faults on the node of the pages. Accessed bits do not tell which
+ * thread touched a page, so give every thread an equal share.
+ */
+static void task_numa_fold_samples(struct task_struct *p, struct mm_struct *mm)
+{
+ unsigned long scan = READ_ONCE(sysctl_numa_balancing_scan_size);
+ atomic_long_t *pages = READ_ONCE(mm->numa_sample_pages);
+ unsigned long *seen = p->numa_sample_seen;
+ int nr_threads = get_nr_threads(p);
+ unsigned long cur, delta, max;
+ int nid;
+
+ p->numa_sample_seq = READ_ONCE(mm->numa_sample_seq);
+ if (!pages)
+ return;
+
+ if (!seen) {
+ seen = kcalloc(nr_node_ids, sizeof(*seen),
+ GFP_KERNEL | __GFP_NOWARN);
+ if (!seen)
+ return;
+
+ /* Start from now, do not claim what was sampled earlier. */
+ for (nid = 0; nid < nr_node_ids; nid++)
+ seen[nid] = atomic_long_read(&pages[nid]);
+ p->numa_sample_seen = seen;
+ return;
+ }
+
+ /*
+ * One scan samples at most its virtual budget. A thread that has not
+ * folded for longer gets no more than that and drops the rest, so a
+ * long sleep cannot swamp its statistics or overflow task_numa_fault().
+ */
+ max = min_t(unsigned long, INT_MAX, MB_TO_PAGES(scan) * 8);
+
+ for (nid = 0; nid < nr_node_ids; nid++) {
+ cur = atomic_long_read(&pages[nid]);
+ delta = (cur - seen[nid]) / nr_threads;
+ if (!delta)
+ continue;
+ if (delta > max) {
+ seen[nid] = cur;
+ delta = max;
+ } else {
+ /* Keep the remainder for the next fold */
+ seen[nid] += delta * nr_threads;
+ }
+
+ task_numa_fault(-1 & LAST_CPUPID_MASK, nid, delta, TNF_UNMOVABLE |
+ (nid == task_node(p) ? TNF_FAULT_LOCAL : 0));
+ }
+}
+
/*
* The expensive part of numa migration is done from task_work context.
* Triggered from task_tick_numa().
@@ -4333,10 +4412,10 @@ 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;
+ atomic_long_t *node_pages;
unsigned long nr_sampled;
+ bool sampled = false;
bool sample_only;
- int nid;
WARN_ON_ONCE(p != container_of(work, struct task_struct, numa_work));
@@ -4352,6 +4431,8 @@ static void task_numa_work(struct callback_head *work)
if (p->flags & PF_EXITING)
return;
+ task_numa_fold_samples(p, mm);
+
/*
* Memory is pinned to only one NUMA node via cpuset.mems, naturally
* no page can be migrated.
@@ -4394,13 +4475,10 @@ static void task_numa_work(struct callback_head *work)
if (!pages)
return;
- node_pages = kcalloc(nr_node_ids, sizeof(*node_pages),
- GFP_KERNEL | __GFP_NOWARN);
+ node_pages = mm_numa_sample_pages(mm);
- if (!mmap_read_trylock(mm)) {
- kfree(node_pages);
+ if (!mmap_read_trylock(mm))
return;
- }
/*
* VMAs are skipped if the current PID has not trapped a fault within
@@ -4534,8 +4612,10 @@ static void task_numa_work(struct callback_head *work)
nr_sampled = numa_sample_range(vma, start, end,
node_pages,
sample_only);
- if (nr_sampled)
+ if (nr_sampled) {
vma_set_access_pid_bit(vma);
+ sampled = true;
+ }
/*
* Try to scan sysctl_numa_balancing_size worth of
@@ -4590,17 +4670,11 @@ 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], TNF_UNMOVABLE |
- (nid == task_node(p) ? TNF_FAULT_LOCAL : 0));
- }
- kfree(node_pages);
- }
+ /* Have the other threads of the process fold the new samples in. */
+ if (sampled)
+ WRITE_ONCE(mm->numa_sample_seq,
+ READ_ONCE(mm->numa_sample_seq) + 1);
+ task_numa_fold_samples(p, mm);
/*
* Make sure tasks use at least 32x as much time to run other code
@@ -4633,6 +4707,8 @@ void init_numa_balancing(u64 clone_flags, struct task_struct *p)
/* Protect against double add, see task_tick_numa and task_numa_work */
p->numa_work.next = &p->numa_work;
p->numa_faults = NULL;
+ p->numa_sample_seen = NULL;
+ p->numa_sample_seq = mm ? mm->numa_sample_seq : 0;
p->numa_pages_migrated = 0;
p->total_numa_faults = 0;
RCU_INIT_POINTER(p->numa_group, NULL);
@@ -4689,7 +4765,8 @@ static void task_tick_numa(struct rq *rq, struct task_struct *curr)
curr->numa_scan_period = task_scan_start(curr);
curr->node_stamp += period;
- if (!time_before(jiffies, curr->mm->numa_next_scan))
+ if (!time_before(jiffies, curr->mm->numa_next_scan) ||
+ curr->numa_sample_seq != READ_ONCE(curr->mm->numa_sample_seq))
task_work_add(curr, work, TWA_RESUME);
}
}
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index f4bb2a980072e..12db0d179b299 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -933,7 +933,7 @@ unsigned long change_prot_numa(struct vm_area_struct *vma,
}
struct numa_sample_walk {
- unsigned long *node_pages;
+ atomic_long_t *node_pages;
unsigned long nr_young;
bool all;
};
@@ -973,7 +973,8 @@ static int numa_sample_pmd_entry(pmd_t *pmd, unsigned long addr,
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;
+ atomic_long_add(HPAGE_PMD_NR,
+ &sw->node_pages[folio_nid(folio)]);
sw->nr_young += HPAGE_PMD_NR;
}
}
@@ -998,7 +999,7 @@ static int numa_sample_pmd_entry(pmd_t *pmd, unsigned long addr,
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)]++;
+ atomic_long_inc(&sw->node_pages[folio_nid(folio)]);
sw->nr_young++;
}
}
@@ -1034,7 +1035,7 @@ static int numa_sample_hugetlb_entry(pte_t *pte, unsigned long hmask,
}
young |= mmu_notifier_test_young(walk->mm, addr);
if (young) {
- sw->node_pages[nid] += pages_per_huge_page(h);
+ atomic_long_add(pages_per_huge_page(h), &sw->node_pages[nid]);
sw->nr_young += pages_per_huge_page(h);
}
}
@@ -1070,7 +1071,7 @@ static const struct mm_walk_ops numa_sample_ops = {
* 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,
+ unsigned long end, atomic_long_t *node_pages,
bool all)
{
struct numa_sample_walk sw = { .node_pages = node_pages, .all = all };
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-12-06 11:40 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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
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®