Skip to contentSkip to conversation

mm/rmap: use release ordering for small-folio mapcount removal

RFC PATCH In review Pranith Kumar 4 participants 8 messages Nov 4

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 mm/rmap: use release ordering for small-folio mapcount removal

Removing a PTE mapping must precede lowering its folio's mapcount. The COW path relies on this order to keep the old page from being reused while a stale translation can still access it. The mapcount update needs release ordering for that edge, rather than the full ordering of atomic_add_negative().

Use atomic_add_negative_release() for non-large folios removed at PTE level. Leave large-folio operations fully ordered; their path can queue a partially mapped folio for deferred splitting. Update the COW ordering comment to describe the required guarantee.

In an arm64 page map/unmap microbenchmark with 20,480 minor faults, simulated cycles fell by 1.54%.

---
 mm/memory.c | 4 ++--
 mm/rmap.c   | 3 ++-
 2 files changed, 4 insertions(+), 3 deletions(-)
mm/memory.c +2 -2
index b65b56fe9714..9e23c64bc743 100644
@@ -4158,9 +4158,9 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf)
 			 * threads.
 			 *
 			 * The critical issue is to order this
-			 * folio_remove_rmap_pte() with the ptp_clear_flush
+			 * folio_remove_rmap_pte() with the ptep_clear_flush
 			 * above. Those stores are ordered by (if nothing else,)
-			 * the barrier present in the atomic_add_negative
+			 * the release ordering of the mapcount decrement
 			 * in folio_remove_rmap_pte();
 			 *
 			 * Then the TLB flush in ptep_clear_flush ensures that
mm/rmap.c +2 -1
index 1131b76bbbc2..62e623d7563b 100644
@@ -1794,7 +1794,8 @@ static __always_inline void __folio_remove_rmap(struct folio *folio,
 	switch (level) {
 	case PGTABLE_LEVEL_PTE:
 		if (!folio_test_large(folio)) {
-			nr = atomic_add_negative(-1, &folio->_mapcount);
+			/* Publish the PTE removal before lowering mapcount. */
+			nr = atomic_add_negative_release(-1, &folio->_mapcount);
 			break;
 		}
 
Signature · 1 line
2.47.3
#1 lore raw
Quoted 6 lines · Pranith Kumar

Removing a PTE mapping must precede lowering its folio's mapcount. The COW path relies on this order to keep the old page from being reused while a stale translation can still access it. The mapcount update needs release ordering for that edge, rather than the full ordering of atomic_add_negative().

Quoted 7 lines

Use atomic_add_negative_release() for non-large folios removed at PTE level. Leave large-folio operations fully ordered; their path can queue a partially mapped folio for deferred splitting. Update the COW ordering comment to describe the required guarantee.

In an arm64 page map/unmap microbenchmark with 20,480 minor faults, simulated cycles fell by 1.54%.

Thanks for the patch.

While I see value, I am not convinced this is the right time for this micro-optimization while we are busy with other things.

This needs a better evaluation: 1.54% in a micro-benchmark without knowing the stddev isn't particularly helpful.

Signature · 3 lines
Cheers,

David
#2 lore raw

Hi David,

Thank you for looking at the patch.

Quoted 5 lines · David Hildenbrand (Arm)
Quoted 4 lines

In an arm64 page map/unmap microbenchmark with 20,480 minor faults, simulated cycles fell by 1.54%.

Quoted 8 lines

Thanks for the patch.

While I see value, I am not convinced this is the right time for this micro-optimization while we are busy with other things.

This needs a better evaluation: 1.54% in a micro-benchmark without knowing the stddev isn't particularly helpful.

This result is from a deterministic simulation, hence no stddev.

Signature · 1 line
Pranith
#3 lore raw
Quoted 12 lines · Pranith Kumar

Hi David,

Thank you for looking at the patch.

Quoted 7 lines · David Hildenbrand (Arm)
Quoted 4 lines

In an arm64 page map/unmap microbenchmark with 20,480 minor faults, simulated cycles fell by 1.54%.

Thanks for the patch.

Quoted 8 lines
Quoted 6 lines

While I see value, I am not convinced this is the right time for this micro-optimization while we are busy with other things.

This needs a better evaluation: 1.54% in a micro-benchmark without knowing the stddev isn't particularly helpful.

This result is from a deterministic simulation, hence no stddev.

Ah, I'd assume even a deterministic simulator would have some variance due to preemption, TLB flushes etc.

But the bigger question is: which arm64 variant is simulated? I'd assume it might vary heavily no?

Signature · 3 lines
Cheers,

David
#4 lore raw
Quoted 20 lines · David Hildenbrand (Arm)
Quoted 19 lines · Pranith Kumar

Hi David,

Thank you for looking at the patch.

Quoted 13 lines · David Hildenbrand (Arm)
Quoted 4 lines

In an arm64 page map/unmap microbenchmark with 20,480 minor faults, simulated cycles fell by 1.54%.

Thanks for the patch.

While I see value, I am not convinced this is the right time for this micro-optimization while we are busy with other things.

This needs a better evaluation: 1.54% in a micro-benchmark without knowing the stddev isn't particularly helpful.

Quoted 7 lines
Quoted 1 line

This result is from a deterministic simulation, hence no stddev.

Ah, I'd assume even a deterministic simulator would have some variance due to preemption, TLB flushes etc.

But the bigger question is: which arm64 variant is simulated? I'd assume it might vary heavily no?

I mean 1.54%, theoretically is even less convincing for me :)

In any case for something touching a sensitive memory barrier operation to have a stronger argument put forward for release ordering being suitable than simply stating it's fine in the commit message and updating a comment.

--
Cheers, Lorenzo

#5 lore raw

Hi Lorenzo,

Quoted 4 lines · Lorenzo Stoakes (ARM)

On Wed, Nov 04, 2026 at 02:37:39PM +0100, David Hildenbrand (Arm) wrote:

I mean 1.54%, theoretically is even less convincing for me :)

With reducing the strength of one barrier, I think this is a non-trivial gain.

Quoted 6 lines

In any case for something touching a sensitive memory barrier operation to have a stronger argument put forward for release ordering being suitable than simply stating it's fine in the commit message and updating a comment.

Can you please explain what information is missing? I think the commit message has all the details of why release is sufficient instead of full barrier.

Thanks,
--
Pranith

#6 lore raw
Quoted 9 lines · Pranith Kumar

Hi Lorenzo,

Quoted 4 lines · Lorenzo Stoakes (ARM)

On Wed, Nov 04, 2026 at 02:37:39PM +0100, David Hildenbrand (Arm) wrote:

I mean 1.54%, theoretically is even less convincing for me :)

With reducing the strength of one barrier, I think this is a non-trivial gain.

Counting cycles on an arm64 simulator for a microbenchmark is completely unconvincing.

Quoted 9 lines
Quoted 5 lines

In any case for something touching a sensitive memory barrier operation to have a stronger argument put forward for release ordering being suitable than simply stating it's fine in the commit message and updating a comment.

Can you please explain what information is missing? I think the commit message has all the details of why release is sufficient instead of full barrier.

rmap is gnarly code and I'm not sure people have an appetite towards weakening random atomic ordering semantics for a virtual 1.54% cycles gain in some microbenchmark, without actually explaining why it is safe. __folio_remove_rmap() has (indirectly) a lot of callers which may subtly expect different semantics.

Signature · 1 line
Pedro
#7 lore raw
Quoted 5 lines · Pedro Falcato
Quoted 4 lines · Pranith Kumar

Hi Lorenzo,

Quoted 1 line · Lorenzo Stoakes (ARM)
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines · David Hildenbrand (Arm)

I mean 1.54%, theoretically is even less convincing for me :)

With reducing the strength of one barrier, I think this is a non-trivial gain.

Counting cycles on an arm64 simulator for a microbenchmark is completely unconvincing.

Exactly.

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

In any case for something touching a sensitive memory barrier operation to have a stronger argument put forward for release ordering being suitable than simply stating it's fine in the commit message and updating a comment.

Can you please explain what information is missing? I think the commit

All of it? Your commit message simply asserts that this is true.

Quoted 7 lines
Quoted 2 lines

message has all the details of why release is sufficient instead of full barrier.

rmap is gnarly code and I'm not sure people have an appetite towards weakening random atomic ordering semantics for a virtual 1.54% cycles gain in some microbenchmark, without actually explaining why it is safe. __folio_remove_rmap() has (indirectly) a lot of callers which may subtly expect different semantics.

Exactly. And this patch boldly decides to CHANGE the invariant here without evidence for a theorised perf gain.

Sorry we don't have time for this. NAK.

--
Cheers, Lorenzo