Skip to contentSkip to conversation

mm/rmap: avoid reusing batched TLB flush generations

PATCH In review David Hildenbrand (Arm) 2 participants 4 messages Nov 24

Subsystems (6)
lore
Share & tools
lore

David Hildenbrand (Arm)

author maintainer
#0 lore raw

PATCH 1/1 mm/rmap: avoid reusing batched TLB flush generations

mm->tlb_flush_batched packs a 15-bit pending and a 15-bit flushed counter into one atomic_t, and resets both once pending gets large. flush_tlb_batched_pending() updates flushed with a cmpxchg() against the value it read before flushing.

If the counters get reset and reach that same value again while flush_tlb_mm() is running, the cmpxchg() succeeds and unmaps that happened in the meantime are considered flushed. A later zap can then skip the flush and leave stale writable TLB entries behind.

Use two separate counters, never reset pending, and let the flusher simply store the pending value it read before flushing. To keep pending from wrapping around to flushed, pull flushed to pending - 1 once pending is more than INT_MAX ahead.

Preventing the wrapping is primarily required because try_to_unmap_flush() as called from migration and reclaim code does *not* go through flush_tlb_batched_pending(), as it does not iterate MMs. So if nothing in the MM triggers flush_tlb_batched_pending(), there would be a problem.

As the separate counters are no longer read atomically together, we must read "flushed" before "pending" (acquire/release), so a reader can never pair a just moved-forward "flushed" value with an older "pending" value.

We'll leave the barrier() between clearing the PTE and incrementing "pending" alone for now: it should be a smp_mb__before_atomic(), but existing arch code (x86: inc_mm_tlb_gen(), arm64: dsb(ishst) before the TLBI, riscv: fully-ordered xchg() in ptep_get_and_clear()) imply a barrier. This should be cleaned up or documented separately.

Note: if a flusher stalls between reading "pending" and storing it, while "pending" advances by an exact multiple of 2^32, the counters could compare equal and we could miss a flush. The same applies to a writer that stalls while moving "flushed" forward.

That is irrelevant in practice: both hold the PTL, so without PREEMPT_RT they cannot get preempted, and wrapping within the ~20s soft lockup and RCU stall timeouts would require more than 200 million batched unmaps per second on a single mm, which is impossible in practice. Even with PREEMPT_RT or long vCPU preemption, we would need minutes of unmap and refault churn while the stalled task stays off the CPU, which is highly unlikely and not a realistic attack vector.

This effectively adds 4 bytes to mm_struct. However, on common 64-bit configs it fits into the existing padding, leaving the size unchanged.

Fixes 5ee2fa2f0636 ("mm/rmap: fix potential batched TLB flush race")
[ david: use atomic_t instead of atomic64_t, adjust description ]
---
 include/linux/mm_types.h |  3 ++-
 mm/rmap.c                | 70 +++++++++++++++++++++---------------------------
 2 files changed, 33 insertions(+), 40 deletions(-)
include/linux/mm_types.h +2 -1
index 1ed35d258d2e3..9f51a0ec67451 100644
@@ -1407,7 +1407,8 @@ struct mm_struct {
 		atomic_t tlb_flush_pending;
 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
 		/* See flush_tlb_batched_pending() */
-		atomic_t tlb_flush_batched;
+		atomic_t tlb_flush_batch_pending;
+		atomic_t tlb_flush_batch_flushed;
 #endif
 		struct uprobes_state uprobes_state;
 #ifdef CONFIG_PREEMPT_RT
mm/rmap.c +31 -39

101 lines not on this page · raw message

---
base-commit: 1c1815d259fc11f14cfd80f8cc37fc067bf6c69e
change-id: 20261123-rmap-9b4b4cd2f886

Best regards,

Signature · 1 line
David Hildenbrand (Arm) <[email protected]>
reviewer
#1 lore raw
Quoted 47 lines · David Hildenbrand (Arm)

mm->tlb_flush_batched packs a 15-bit pending and a 15-bit flushed counter into one atomic_t, and resets both once pending gets large. flush_tlb_batched_pending() updates flushed with a cmpxchg() against the value it read before flushing.

If the counters get reset and reach that same value again while flush_tlb_mm() is running, the cmpxchg() succeeds and unmaps that happened in the meantime are considered flushed. A later zap can then skip the flush and leave stale writable TLB entries behind.

Use two separate counters, never reset pending, and let the flusher simply store the pending value it read before flushing. To keep pending from wrapping around to flushed, pull flushed to pending - 1 once pending is more than INT_MAX ahead.

Preventing the wrapping is primarily required because try_to_unmap_flush() as called from migration and reclaim code does *not* go through flush_tlb_batched_pending(), as it does not iterate MMs. So if nothing in the MM triggers flush_tlb_batched_pending(), there would be a problem.

As the separate counters are no longer read atomically together, we must read "flushed" before "pending" (acquire/release), so a reader can never pair a just moved-forward "flushed" value with an older "pending" value.

We'll leave the barrier() between clearing the PTE and incrementing "pending" alone for now: it should be a smp_mb__before_atomic(), but existing arch code (x86: inc_mm_tlb_gen(), arm64: dsb(ishst) before the TLBI, riscv: fully-ordered xchg() in ptep_get_and_clear()) imply a barrier. This should be cleaned up or documented separately.

Note: if a flusher stalls between reading "pending" and storing it, while "pending" advances by an exact multiple of 2^32, the counters could compare equal and we could miss a flush. The same applies to a writer that stalls while moving "flushed" forward.

That is irrelevant in practice: both hold the PTL, so without PREEMPT_RT they cannot get preempted, and wrapping within the ~20s soft lockup and RCU stall timeouts would require more than 200 million batched unmaps per second on a single mm, which is impossible in practice. Even with PREEMPT_RT or long vCPU preemption, we would need minutes of unmap and refault churn while the stalled task stays off the CPU, which is highly unlikely and not a realistic attack vector.

This effectively adds 4 bytes to mm_struct. However, on common 64-bit configs it fits into the existing padding, leaving the size unchanged.

Quoted 7 lines
Fixes 5ee2fa2f0636 ("mm/rmap: fix potential batched TLB flush race")
[ david: use atomic_t instead of atomic64_t, adjust description ]

---

Uh, I spent a couple of hours to review and make sure this is correct. I thought this is correct but then my claude flagged a race during fork.

dup_mm() copies the counters with memcpy() and it doesn't know the ordering. When flushed and pending fields sit in separate words, dup_mm() could read them in two separate loads, but in the wrong order.

It's especially problematic in a very rare (IMO) case where flushed == pending + 1 becomes true in the child (which means the first pending flush in the child might not be flushed properly).

fork reclaim another process
memcpy reads pending = X

				pending++ (X + 1)
							truncate()
							-> flush_tlb_batched_pending()
							-> flushed = X + 1

memcpy reads flushed = X + 1

Maybe reset the counters after copying mm_struct?

Signature · 2 lines
Cheers,
Harry / Hyeonggon
author maintainer
#2 lore raw
Quoted 48 lines · Harry Yoo
Quoted 47 lines · David Hildenbrand (Arm)

mm->tlb_flush_batched packs a 15-bit pending and a 15-bit flushed counter into one atomic_t, and resets both once pending gets large. flush_tlb_batched_pending() updates flushed with a cmpxchg() against the value it read before flushing.

If the counters get reset and reach that same value again while flush_tlb_mm() is running, the cmpxchg() succeeds and unmaps that happened in the meantime are considered flushed. A later zap can then skip the flush and leave stale writable TLB entries behind.

Use two separate counters, never reset pending, and let the flusher simply store the pending value it read before flushing. To keep pending from wrapping around to flushed, pull flushed to pending - 1 once pending is more than INT_MAX ahead.

Preventing the wrapping is primarily required because try_to_unmap_flush() as called from migration and reclaim code does *not* go through flush_tlb_batched_pending(), as it does not iterate MMs. So if nothing in the MM triggers flush_tlb_batched_pending(), there would be a problem.

As the separate counters are no longer read atomically together, we must read "flushed" before "pending" (acquire/release), so a reader can never pair a just moved-forward "flushed" value with an older "pending" value.

We'll leave the barrier() between clearing the PTE and incrementing "pending" alone for now: it should be a smp_mb__before_atomic(), but existing arch code (x86: inc_mm_tlb_gen(), arm64: dsb(ishst) before the TLBI, riscv: fully-ordered xchg() in ptep_get_and_clear()) imply a barrier. This should be cleaned up or documented separately.

Note: if a flusher stalls between reading "pending" and storing it, while "pending" advances by an exact multiple of 2^32, the counters could compare equal and we could miss a flush. The same applies to a writer that stalls while moving "flushed" forward.

That is irrelevant in practice: both hold the PTL, so without PREEMPT_RT they cannot get preempted, and wrapping within the ~20s soft lockup and RCU stall timeouts would require more than 200 million batched unmaps per second on a single mm, which is impossible in practice. Even with PREEMPT_RT or long vCPU preemption, we would need minutes of unmap and refault churn while the stalled task stays off the CPU, which is highly unlikely and not a realistic attack vector.

This effectively adds 4 bytes to mm_struct. However, on common 64-bit configs it fits into the existing padding, leaving the size unchanged.

Quoted 8 lines
Quoted 7 lines
Fixes 5ee2fa2f0636 ("mm/rmap: fix potential batched TLB flush race")
[ david: use atomic_t instead of atomic64_t, adjust description ]

---

Hi Harry,

thanks for all that effort!

Quoted 13 lines

Uh, I spent a couple of hours to review and make sure this is correct. I thought this is correct but then my claude flagged a race during fork.

dup_mm() copies the counters with memcpy() and it doesn't know the ordering. When flushed and pending fields sit in separate words, dup_mm() could read them in two separate loads, but in the wrong order.

It's especially problematic in a very rare (IMO) case where flushed == pending + 1 becomes true in the child (which means the first pending flush in the child might not be flushed properly).

fork reclaim another process
memcpy reads pending = X

Quoted 7 lines
				pending++ (X + 1)
							truncate()
							-> flush_tlb_batched_pending()
							-> flushed = X + 1

memcpy reads flushed = X + 1

Maybe reset the counters after copying mm_struct?

Resetting sounds in general like the right thing to do. Ugh, this copying of random state is nasty.

The child never ran, so there isn't anything to flush for it.

So what you say is that the first deferred flush in the child will be missed. I agree that it's extremely unlikely, but we should handle it.

We currently have:

if (unlikely(pending - flushed > INT_MAX))

So I guess an alternative would be:

if (unlikely(pending - flushed - 1 >= INT_MAX))

pending = x + 1
flushed = x + 1

(x + 1 - (x + 1 ) - 1) = (unsigned)-1 >= INT_MAX

But yeah, let's just initialize it during fork(), thanks!

Signature · 3 lines
Cheers,

David
reviewer
#3 lore raw
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 1 line · Harry Yoo

On Tue, Nov 24, 2026 at 10:39:11AM +0100, David Hildenbrand (Arm) wrote:

Hi Harry,

thanks for all that effort!

No problem, thanks for looking at the issue ;)

Quoted 19 lines
Quoted 19 lines

Uh, I spent a couple of hours to review and make sure this is correct. I thought this is correct but then my claude flagged a race during fork.

dup_mm() copies the counters with memcpy() and it doesn't know the ordering. When flushed and pending fields sit in separate words, dup_mm() could read them in two separate loads, but in the wrong order.

It's especially problematic in a very rare (IMO) case where flushed == pending + 1 becomes true in the child (which means the first pending flush in the child might not be flushed properly).

fork reclaim another process
memcpy reads pending = X

				pending++ (X + 1)
							truncate()
							-> flush_tlb_batched_pending()
							-> flushed = X + 1

memcpy reads flushed = X + 1

Quoted 4 lines
Quoted 1 line

Maybe reset the counters after copying mm_struct?

Resetting sounds in general like the right thing to do. Ugh, this copying of random state is nasty.

Indeed.

Quoted 3 lines

The child never ran, so there isn't anything to flush for it.

So what you say is that the first deferred flush in the child will be missed.

Yes, I exactly meant that.

Quoted 1 line

I agree that it's extremely unlikely, but we should handle it.

Agreed.

Quoted 6 lines

We currently have:

if (unlikely(pending - flushed > INT_MAX))

So I guess an alternative would be:

Quoted 7 lines

if (unlikely(pending - flushed - 1 >= INT_MAX))

pending = x + 1
flushed = x + 1

(x + 1 - (x + 1 ) - 1) = (unsigned)-1 >= INT_MAX

Oh that's subtle :)

Quoted 1 line

But yeah, let's just initialize it during fork(), thanks!

Sounds good, thanks!

Signature · 2 lines
Cheers,
Harry / Hyeonggon