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

From: sashiko-bot

Date: Wed Nov 25 2026 - 04:39:35 EST


Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm/rmap: uninitialized tlb_flush_batch counters in mm_init()
on fork()
--

commit 087cd5390dc5a69ffce740207a46d0df64ca3039
Author: David Hildenbrand (Arm) <david@xxxxxxxxxx>

mm/rmap: avoid reusing batched TLB flush generations

This commit replaces the packed mm->tlb_flush_batched counter with separate
32-bit mm->tlb_flush_batch_pending and mm->tlb_flush_batch_flushed counters
to prevent generation reuse races during batched TLB flushes.

> diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
> index 1ed35d258d2e3..9f51a0ec67451 100644
> --- a/include/linux/mm_types.h
> +++ b/include/linux/mm_types.h
> @@ -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

[Severity: Medium]
Should init_tlb_flush_pending() also zero tlb_flush_batch_pending and
tlb_flush_batch_flushed when initializing a new mm?

During fork(), dup_mm() copies oldmm with memcpy() and then calls
mm_init() -> init_tlb_flush_pending(), which only resets
mm->tlb_flush_pending. Because tlb_flush_batch_pending sits before
tlb_flush_batch_flushed in struct mm_struct, memcpy() copies pending before
flushed, the opposite of the flushed-before-pending order required by
flush_tlb_batched_pending():

CPU0 (fork) CPU1 (reclaim on oldmm) CPU2 (flusher on oldmm)
dup_mm()
memcpy(mm, oldmm, ...)
reads pending = P
try_to_unmap_one()
set_tlb_ubc_flush_pending()
pending = P + 1
flush_tlb_batched_pending()
flush_tlb_mm(oldmm)
flushed = P + 1
reads flushed = P + 1

The child mm starts with tlb_flush_batch_pending == P and
tlb_flush_batch_flushed == P + 1. On the first batched unmap in the child,
set_tlb_ubc_flush_pending() increments pending to P + 1 (where
pending - flushed == 0), leaving pending == flushed while a batched flush
is still pending. A racing flush_tlb_batched_pending() in the child then
sees pending == flushed and skips flush_tlb_mm(), leaving stale writable
TLB entries behind.

Even without a concurrent update during memcpy(), inheriting unequal
counters from oldmm causes an unnecessary flush_tlb_mm() in the child and a
data race on oldmm during memcpy().

--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124-rmap-v1-1-e3dc48a4b527@xxxxxxxxxx?part=1