From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.192 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796557139; b=puRcb+tHS82S2z0/kaSFKvw4ds5B+xLpZDaoBSRRNQt3H/+oPFA2yetelGmCvCY1nOZG y6xqSVRzsOyGhIjqmXPwwE9sENXfUMUrf4qphiYppPKCUAsKbgR4/0vGsqazwaJs6e4fn OndasbvommcqDxiOuAD/ANPstcsMAYcShmW6JZR3oPd+edbQsRu4VdNacQmaex7tEmTk7 88l9TJFtMqjAbloXDNhM5pJYnJYQBDrK40POnk4+b/61dgoZlQUw5p53axibQ07lnXNh7 hKWWZugE0V4KCzGwxUR8HWK3ptD/ajpGOIMdzVTEp27tGi1dg0rHJGjXx34BeKOCcYw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796557139; 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=Bw4vTM0to187QaYtemiSUm4kgupq6ZEQKvaLPOJuHeY=; b=1s9XnWipVVBdT54XWyEp50NdMbKRhw0q8dNKQJIjlyEaEESQoVwVkySmgMMU1B2NRu22 sPD9ImBQG3J8biq3/C/75sK0lBbqMy4BeIUx9RBvBMh9BwvawMcuFxLT9NU0YP44EvO8w R6Anw5zrpTOVCPie+p2pbaGYQmDgeFbspyurNv6mqARtZzdFojpx56hZGNsiclqYRljnn hIqzwRgtvk1EUsPdjL+X8ls+dtbpsYU7q5jvtrdql9lR5uI+7QrUT4tiCR8pbMYrmQpak 6x9hUjzzbCerUnQ4uhjDOlr55VCLz+ssNySxbi6VgwNbwG10o37I4rkRayM+wWhSSGg== 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=ULpa9ZJz; arc=none smtp.remote-ip=91.218.175.192 Received: from mta0.migadu.com (out-192.mta0.migadu.com [91.218.175.192]) (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 3ADC7606A33E for ; Sun, 6 Dec 2026 11:38:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3ADC7606A33E 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=ULpa9ZJz X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=i1ksAwJrXA1K8nwkgYQe9PgNMvYdJ4w2LzCMjFpuy1c=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796557135; v=1; x=1797161935; b=ULpa9ZJzWWeNBQnljVYdquAHj3XUzX4f+AduJJMWH5uyYt98iy8YaRJR4utYNAS+6K/AgMXh dalLrID0kdl4K1aLPbU55GT0lHb5eP6sKQoJ9/qsLBlbCBwDZtqKvz5FWjQdzZUTPJmBluJcGkT QQlo/Ly3JoqMMVImNgvdram0= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 5d2c2d2edf275f0c; Sun, 06 Dec 2026 11:38:55 +0000 X-Mizu-Trace-ID: 5d2c2d2edf275f0c X-Migadu-Flow: FLOW_OUT From: Qiliang Yuan Date: Sun, 06 Dec 2026 06:38:38 -0500 Subject: [PATCH 1/8] sched/numa: Sample the accessed bits of pinned memory 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-1-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 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 --- 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