[PATCH] mm/rmap: avoid reusing batched TLB flush generations

David Hildenbrand (Arm) david at kernel.org
Wed Nov 25 01:19:02 PST 2026


On 11/25/26 09:51, Huang, Ying wrote:
> Hi, David,
> 
> "David Hildenbrand (Arm)" <david at kernel.org> writes:
> 
>> 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")
>> Cc: stable at vger.kernel.org
>> Reported-by: Kirk Swidowski <swidowski at google.com>
>> Signed-off-by: Andrew Morton <akpm at linux-foundation.org>
>> [ david: use atomic_t instead of atomic64_t, adjust description ]
>> Signed-off-by: David Hildenbrand (Arm) <david at kernel.org>
> 
> Except the fork issue pointed out by Harry, the patch looks good to me.
> Thanks for fixing this problem.  Feel free to add my
> 
> Reviewed-by: Huang Ying <ying.huang at linux.alibaba.com>

Thanks!

-- 
Cheers,

David



More information about the linux-riscv mailing list