NUMA balancing remembers the lower PID bits of the task that last triggered a NUMA hinting fault on a folio alongside the CPU. Since 2013 through commit 8c8a743c5087 ("sched/numa: Use {cpu, pid} to create task groups for shared faults"), we reset that folio information when reusing a folio during a write fault. That can result in some suboptimal behavior especially for anon folios: (a) NUMA balancing records the task+CPU and decides to migrate the folio. (b) Migration code maps the folio R/O, and on next write access we reset that information. (c) The next NUMA hinting fault then looks like a first access, so a single access from another node can be enough to trigger the NUMA balancing migration machinery again. As Simon Inizan reports, that can result in some unexpected folio migration back and forth between NUMA nodes for some workloads. The original reasoning why we added that NUMA-hinting information reset was that the folio might now belong to an unrelated task. For example, after fork() a child might reuse an anonymous folio that its parent last accessed, and we don't want to group both tasks. However, since commit dabe1d992414 ("sched/numa: Be more careful about joining numa groups"), task_numa_group() only groups tasks that share the MM or that share the folio through a MAP_SHARED mapping, so a stale cpupid can no longer group tasks that merely shared an anonymous folio after fork(). The child's first NUMA hinting fault might still see the stale information, such that we treat it as a shared fault and delay migration until a later fault. CoW-reuse in the child is less common, so it shouldn't move the needle. If we ever care, we could decide based on whether we likely saw fork() before, because then we have to set PageAnonExclusive again. Further: (1) do_huge_pmd_wp_page() doesn't reset that metadata, so our handling is inconsistent. (2) there are other valid cases where we run into wp_page_reuse() without ever involving fork(), where that information is still perfectly fine. (3) In the context of pte-mapped THPs where we reuse individual pages, it doesn't quite make sense. So let's just stop resetting that information in wp_page_reuse(). Note that task_numa_group() will now also see accesses from different tasks that we previously treated as private first accesses, which is how it is supposed to work. While at it, convert the VM_BUG_ON() to a VM_WARN_ON_ONCE(). Cc: Simon Inizan Signed-off-by: David Hildenbrand (Arm) --- Hi, this was reported internally; I don't really consider it a BUG, so no Fixes. This resolves the issue with repeated migration back and forth that Simon was seeing. I don't really know which benchmarks to run where we could see a regression through this change. Migration might now be delayed until a later NUMA hinting fault when a child reuses a folio after fork(), but I don't expect that to matter in practice. I'd assume most performance-sensitive workloads will use plenty of PMD-mapped THPs where the behavior was always like that. So in the end this just makes the handling consistent. --- mm/memory.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index b794444db449f..c53950d01e3e6 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3988,17 +3988,8 @@ static inline void wp_page_reuse(struct vm_fault *vmf, struct folio *folio) VM_BUG_ON(!(vmf->flags & FAULT_FLAG_WRITE)); VM_WARN_ON(is_zero_pfn(pte_pfn(vmf->orig_pte))); - - if (folio) { - VM_BUG_ON(folio_test_anon(folio) && - !PageAnonExclusive(vmf->page)); - /* - * Clear the folio's cpupid information as the existing - * information potentially belongs to a now completely - * unrelated process. - */ - folio_xchg_last_cpupid(folio, (1 << LAST_CPUPID_SHIFT) - 1); - } + VM_WARN_ON_ONCE(folio && folio_test_anon(folio) && + !PageAnonExclusive(vmf->page)); flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); entry = pte_mkyoung(vmf->orig_pte); --- base-commit: c03465593e556e594b4efb1f6cb26565f11dc8b8 change-id: 20261203-wp_reuse_cpupid-ae4ab99920c8 Best regards, -- David Hildenbrand (Arm)