Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/memory: don't clear the last cpupid in wp_page_reuse()
@ 2026-12-04 13:29 David Hildenbrand (Arm)
  2026-12-05  6:37 ` Lance Yang
  2026-12-05 12:31 ` Jose A. Perez de Azpillaga
  0 siblings, 2 replies; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-12-04 13:29 UTC (permalink / raw)
  To: Liam R. Howlett, Vlastimil Babka, Mike Rapoport, Michal Hocko,
	Andrew Morton, Mel Gorman, Peter Zijlstra, Rik van Riel,
	Ying Huang, Zi Yan, Baolin Wang, Gregory Price, Joshua Hahn,
	Rakie Kim
  Cc: linux-mm, Simon Inizan, David Hildenbrand (Arm)

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 <simon.inizan@arm.com>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
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) <david@kernel.org>



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/memory: don't clear the last cpupid in wp_page_reuse()
  2026-12-04 13:29 [PATCH] mm/memory: don't clear the last cpupid in wp_page_reuse() David Hildenbrand (Arm)
@ 2026-12-05  6:37 ` Lance Yang
  2026-12-05 12:31 ` Jose A. Perez de Azpillaga
  1 sibling, 0 replies; 3+ messages in thread
From: Lance Yang @ 2026-12-05  6:37 UTC (permalink / raw)
  To: david
  Cc: liam, vbabka, rppt, mhocko, akpm, mgorman, peterz, riel,
	ying.huang, ziy, baolin.wang, gourry, joshua.hahnjy, rakie.kim,
	linux-mm, simon.inizan


On Fri, Dec 04, 2026 at 02:29:52PM +0100, David Hildenbrand (Arm) wrote:
>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().

Cool! Also stops wp_page_reuse() from clobbering the page access time of
folios on slow memory nodes in memory tiering mode :)

>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 <simon.inizan@arm.com>
>Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
>---

Nice work!

Reviewed-by: Lance Yang <lance.yang@linux.dev>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/memory: don't clear the last cpupid in wp_page_reuse()
  2026-12-04 13:29 [PATCH] mm/memory: don't clear the last cpupid in wp_page_reuse() David Hildenbrand (Arm)
  2026-12-05  6:37 ` Lance Yang
@ 2026-12-05 12:31 ` Jose A. Perez de Azpillaga
  1 sibling, 0 replies; 3+ messages in thread
From: Jose A. Perez de Azpillaga @ 2026-12-05 12:31 UTC (permalink / raw)
  To: David Hildenbrand (Arm), Liam R. Howlett, Vlastimil Babka,
	Mike Rapoport, Michal Hocko, Andrew Morton, Mel Gorman,
	Peter Zijlstra, Rik van Riel, Ying Huang, Zi Yan, Baolin Wang,
	Gregory Price, Joshua Hahn, Rakie Kim
  Cc: linux-mm, Simon Inizan

On 12/4/26 2:29 PM, David Hildenbrand (Arm) wrote:
> 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 <simon.inizan@arm.com>
> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
> ---
> 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.

LGTM:

Reviewed-by: Jose A. Perez de Azpillaga <azpijr@gmail.com>

-- 
cheers,
jose a. p-a


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-05 12:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 13:29 [PATCH] mm/memory: don't clear the last cpupid in wp_page_reuse() David Hildenbrand (Arm)
2026-12-05  6:37 ` Lance Yang
2026-12-05 12:31 ` Jose A. Perez de Azpillaga

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox