From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.133 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796557224; b=ggBtgVJ2Lc8G+VtD4zF1vw946lfxZcPTIiUIIluSDt0NZUG6gcaDdGTwQIwCHUZ8E8Qg kfqZgcGh//qOyp14pW8cVqFzaSBRNwPYGzK9LhMlbHdGKJUZ4n6A6UpnjatDWD61biJ1G sdzbtQZ+EbWBoYo/SDOEEvVPLs3N3pITQIKhJFmWJTSU7DZOUr+/uiloThj9qQCsM71AC Z2qYx1Fg3yfaiEHWdMWg4CO/6mxmdZRyf2gXCEwIoGME7INkEvUxL8e0ektWxiRfWvIt1 ODxtQunq4H2OVxBXkCAHtJoQhAKc4SMMmT1yxYSTbdz7LRBf9c1mYZtEiHAEnwcnV7g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796557224; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:Date:Subject:MIME-Version: Content-Type:Content-Transfer-Encoding:Message-Id:References: In-Reply-To:To:Cc:X-Mailer; bh=v8EhtZeLQB3AsiaFhxd0hBkzc1SPy0E4cYhHzVij0KA=; b=dnYtzR9vL3ITx+oHGzyvMNVqvYlQxqtYmOIbrfpUMejmn4XBzoqK/AsmMOvcE4domG51 tzBJMMC4fizcxnBs9SRU21jAaMkkmRTvnyrHEaA9dFsuq4dFKVzYfq7O1qTcKt5Ugr35E tB38nppi0Ogzn34b09uOBUz+Y4FMxYdKtY1L0KmyZO9cQ/b7Ue7JFC3tKYNWCkoQlOhfQ A59Etpzab79ZdidGxsw1HhMm0rawIczBtciAceK0DNRlz0qvyY1e+6Xrj672s1XXRt1R7 JCxy77qsUhdc2zBI6zpTlptGqeEjEWYhbZ34UsaQgtGhsT0f6/iceRl/ZaqRQ3c6GwQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=LieVeqG3; arc=none smtp.remote-ip=95.215.58.133 Received: from mta1.migadu.com (out-133.mta1.migadu.com [95.215.58.133]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6220606A334 for ; Sun, 6 Dec 2026 11:40:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B6220606A334 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=LieVeqG3 X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=OAs3lo8mp408p1fO8HyMNT5yRMrvhsIbtOeUkv/UZHE=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796557221; v=1; x=1797162021; b=LieVeqG3xscFNb1KhjZyUZtHtlVRKZ/NMOB1TQXWiRilLXDSX3VUwwAKo0CFCMgJH2Mz+waW 6omrq2LjBR3rcxKK7zii1q2rHVYt4l0NqA9UkIV3xTjtwftrfsTnLf3uVPOAU6qgz0nc89cReEJ e6+9x383ZsPMxsTk+5zCCIWM= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 49f7ebadd5ded6cf; Sun, 06 Dec 2026 11:40:19 +0000 X-Mizu-Trace-ID: 49f7ebadd5ded6cf X-Migadu-Flow: FLOW_OUT From: Qiliang Yuan Date: Sun, 06 Dec 2026 06:38:45 -0500 Subject: [PATCH 8/8] sched/numa: Share sampled accesses between the threads of a process X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261206-feat-numab-series-v1-8-2af21ad24718@linux.dev> References: <20261206-feat-numab-series-v1-0-2af21ad24718@linux.dev> In-Reply-To: <20261206-feat-numab-series-v1-0-2af21ad24718@linux.dev> 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@kvack.org, linux-kernel@vger.kernel.org, Qiliang Yuan X-Mailer: b4 0.13.0 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 --- 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