mm/rmap: avoid reusing batched TLB flush generations
Subsystems (9)
David Hildenbrand (Arm)
maintainerPATCH v2 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()) implies a barrier. This should be cleaned up or documented separately.
As dup_mm() copies the counters using memcpy() while they can get updated concurrently, a newly forked child could end up with "flushed" being "pending + 1" and we could skip a later flush in the child. In theory, this might have been possible before (load tearing), but two separate counters make that easier to trigger. So let's just reset them to 0 during fork(). Put it into the existing init_tlb_flush_pending() and rename the function to make the name better match the wider scope.
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.
[ david: use atomic_t instead of atomic64_t, reset counters during fork(),
adjust description ]
---
Changes in v2:
- Reset counters during fork()
- Adjust description
- Pick up tags (thanks!)
- Link to v1: https://patch.msgid.link/[email protected]
--- include/linux/mm_inline.h | 6 +++- include/linux/mm_types.h | 3 +- kernel/fork.c | 2 +- mm/rmap.c | 70 +++++++++++++++++++++-------------------------- 4 files changed, 39 insertions(+), 42 deletions(-)
include/linux/mm_inline.h +5 -1
@@ -551,9 +551,13 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1,void pfnmap_track_ctx_release (struct kref *ref); -static inline void init_tlb_flush_pending (struct mm_struct *mm) +static inline void mm_init_tlb_flush (struct mm_struct *mm) {atomic_set (&mm->tlb_flush_pending,0 ); +#ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH +atomic_set (&mm->tlb_flush_batch_pending,0 ); +atomic_set (&mm->tlb_flush_batch_flushed,0 ); +#endif }static inline void inc_tlb_flush_pending (struct mm_struct *mm)
include/linux/mm_types.h +2 -1
@@ -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
kernel/fork.c +1 -1
@@ -1109,7 +1109,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)mm_pasid_init (mm);RCU_INIT_POINTER (mm->exe_file,NULL );mmu_notifier_subscriptions_init (mm); -init_tlb_flush_pending (mm); +mm_init_tlb_flush (mm);#if defined(CONFIG_TRANSPARENT_HUGEPAGE ) && !defined(CONFIG_SPLIT_PMD_PTLOCKS ) mm->pmd_huge_pte =NULL ;#endif
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]>
Quoted 58 lines · David Hildenbrand (Arm)
mm->tlb_flush_batchedpacks a 15-bit pending and a 15-bit flushed counter into oneatomic_t, and resets both once pending gets large.flush_tlb_batched_pending()updates flushed with acmpxchg()against the value it read before flushing.If the counters get reset and reach that same value again while
flush_tlb_mm()is running, thecmpxchg()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_MAXahead.Preventing the wrapping is primarily required because
try_to_unmap_flush()as called from migration and reclaim code does *not* go throughflush_tlb_batched_pending(), as it does not iterate MMs. So if nothing in the MM triggersflush_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 asmp_mb__before_atomic(), but existing arch code (x86:inc_mm_tlb_gen(), arm64: dsb(ishst) before the TLBI, riscv: fully-orderedxchg()inptep_get_and_clear()) implies a barrier. This should be cleaned up or documented separately.As
dup_mm()copies the counters usingmemcpy()while they can get updated concurrently, a newly forked child could end up with "flushed" being "pending + 1" and we could skip a later flush in the child. In theory, this might have been possible before (load tearing), but two separate counters make that easier to trigger. So let's just reset them to 0 duringfork(). Put it into the existinginit_tlb_flush_pending()and rename the function to make the name better match the wider scope.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_RTthey 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 withPREEMPT_RTor 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.[ david: use
atomic_tinstead ofatomic64_t, reset counters duringfork(),
adjust description ]
Quoted 7 lines
Fixes 5ee2fa2f0636 ("mm/rmap: fix potential batched TLB flush race")Reported-by Kirk Swidowski <[email protected]>Signed-off-by Andrew Morton <[email protected]>Reviewed-by Huang Ying <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>---
Looks good to me,
Signature · 2 lines
Cheers, Harry / Hyeonggon