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 8/8] sched/numa: Share sampled accesses between the threads of a process
Date: Sun, 06 Dec 2026 06:38:45 -0500 [thread overview]
Message-ID: <20261206-feat-numab-series-v1-8-2af21ad24718@linux.dev> (raw)
In-Reply-To: <20261206-feat-numab-series-v1-0-2af21ad24718@linux.dev>
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
prev parent reply other threads:[~2026-12-06 11:40 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 ` [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 ` Qiliang Yuan [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=20261206-feat-numab-series-v1-8-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®