From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 11CD0606A322 for ; Thu, 3 Dec 2026 19:01:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NoL9W28F Received: by kanga.kvack.org (Postfix) id 1487E6B00EF; Thu, 3 Dec 2026 14:01:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0F9396B05B1; Thu, 3 Dec 2026 14:01:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 00EE16B05B2; Thu, 3 Dec 2026 14:01:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id C4DA86B00EF for ; Thu, 3 Dec 2026 14:01:45 -0500 (EST) Received: from smtpin05.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A406316081B for ; Thu, 3 Dec 2026 19:01:43 +0000 (UTC) X-FDA: 85503590886.05.1572BE1 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf19.hostedemail.com (Postfix) with ESMTP id EF63B1A001A for ; Thu, 3 Dec 2026 19:01:41 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=NoL9W28F; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of ljs@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=ljs@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796324502; b=0rg1/Rpwnta6F3K796R9c2XcAADI4E5/lFtNQh90xAlc5x0bwEF33W4dDbH6T6xDxTtZbP aOr+woZtJV9G7D0+X1NpBz7AaOINMM/qhVyyCmHZAo7nJUqOqIRGpxedL+cK6RZDaUONR8 3EEIzKoeamFRQAodjCdxtgbBnRU0pVI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796324502; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=phIPulS8TFtJsCwtTY4RQVVL9zA9TIR9usrVeVdYQUs=; b=UGGdV6kYeGymQPwTZSgTO2XS24MLBY9RllPBN6qBW/Ha93bI8k4gUwnm1lCwOxK8j+G0N3 0wknXgGDeorUxhGcppB6sjsQxNbYRFMDmuZyJzc8qfNeXBzeNaOluYAjSuMd0MCr0R4P8P E2Jq7yRkCvK6JZEZuuWa2vkkFvUcESc= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=NoL9W28F; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf19.hostedemail.com: domain of ljs@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=ljs@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 072876224C; Thu, 3 Dec 2026 19:01:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B7CC1F00937; Thu, 3 Dec 2026 19:01:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796324500; bh=phIPulS8TFtJsCwtTY4RQVVL9zA9TIR9usrVeVdYQUs=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=NoL9W28FlQtL62X1/GhDnR+0BLRGrpjDNelCt2ymjGHbnorm+YWHnMOB+sMuL6qk+ LLCUOlxQz3bfscs2GH92qRWT0eY88JXEO3G7TOCDVuMG5bXXJdVtae1nuQJdxP8WoW HkgGOMun+n5wxwfBzSMgGDvxIBIpiaYAA6s8/LzXwgih5dgvwePXGwuhskkgVfe647 V4d9kBApQ8aO/tcuZKtha+pMr5JOJKJUCXxkeeRtnOTmqjy+XM7TP/GOqBtDAfoDeJ sg0k3QhUouhp5Qtrz5o1Fd51okzktw5kEwsdJYTL6V2v+mGNJwo+U++hdFYYUHH0Uk Rxc+K/ujVFXGQ== Date: Thu, 3 Dec 2026 19:01:31 +0000 From: "Lorenzo Stoakes (ARM)" To: "David Hildenbrand (Arm)" Cc: "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Rik van Riel , Harry Yoo , Jann Horn , Lance Yang , Andrew Morton , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Ying Huang , linux-mm@kvack.org, linux-rt-devel@lists.linux.dev, linux-riscv@lists.infradead.org, stable@vger.kernel.org, Kirk Swidowski , Huang Ying Subject: Re: [PATCH v2] mm/rmap: avoid reusing batched TLB flush generations Message-ID: References: <20261125-rmap-v2-1-0ba817662eac@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261125-rmap-v2-1-0ba817662eac@kernel.org> X-Stat-Signature: ndjxrtpx4d9wnm8zfms4fwn8dyjt45wn X-Rspam-User: X-Rspamd-Server: rspam11 X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Rspamd-Queue-Id: EF63B1A001A X-HE-Tag: 1796324501-427488 X-HE-Meta: U2FsdGVkX18KaEvQ+m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T0Oz9PZ5CsrnI2an0s9RERmfjs3ZENGXrgvL+lcfARwgEgSW53ZBEgBlWWWAwSMLfTeVPjU9ngRiBNMpOKNzRP5S6oxDqcBuG/7w7M4NMh+Mqqp/601ImZ+OSCc0HcECeNe17wFmmwnPWtpIIHinQPD/T/iS31AXdiZXYupZR/P/9Zprl/7Dkkqh/zgRoJTaO/vae/IOhsHnwDlY5RYxdBlYQPSSHFckuVwn6ChWCHlwGCsUsmAgTy+iFTwcSCPI2wzs4CkRLqoNXb3BTLhaXI2R1JnzyFPIFKI6G0mH3McFwGQgEUJNWovuwdzZQsn4msHNT/NlqWEuu4o9ELSNhvEpTG9zcADFX4fZ0JGWCtBFLwadE2Z3Yf05walpDzUScmX9UNQmM1Bl9Pt08M6oPo35c4ycveF9OnAn81T1o0+3gMuzxjC9mq4UGFCpKjImWuGN1BdkzojE1bohfVuHcIKxEHqJgLp/pbKjoaugjvZ/RZiRvmVXRoCxvvEpHT15A/3I9wAPTJb7QCu8gRJvWMNBaA8DS2O1hZ6oaDsTXbz+I9JDG8RlCizyah06Sl7c+yL2JF4TrCQuKzDu0KpIlMZc2yeIcG2AYS6NGcoLZVQK9RBCkVE/UHRFqcwuO2ik0usogPFdKbxQAWa7tEEvMn+WGdX6 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Wed, Nov 25, 2026 at 10:34:46AM +0100, David Hildenbrand (Arm) wrote: > 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 ] > > Fixes: 5ee2fa2f0636 ("mm/rmap: fix potential batched TLB flush race") > Cc: stable@vger.kernel.org > Reported-by: Kirk Swidowski > Signed-off-by: Andrew Morton > Reviewed-by: Huang Ying > Signed-off-by: David Hildenbrand (Arm) Looks sensible so: Acked-by: Lorenzo Stoakes (ARM) > --- > Changes in v2: > - Reset counters during fork() > - Adjust description > - Pick up tags (thanks!) > - Link to v1: https://patch.msgid.link/20261124-rmap-v1-1-e3dc48a4b527@kernel.org > --- > 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(-) > > diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h > index ab69b9930893f..71fa0596ff6f7 100644 > --- a/include/linux/mm_inline.h > +++ b/include/linux/mm_inline.h > @@ -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) > 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 > diff --git a/kernel/fork.c b/kernel/fork.c > index 77d6c8fc38c66..63014f5fef42f 100644 > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -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 > diff --git a/mm/rmap.c b/mm/rmap.c > index 65646ed6fc453..038435994b99a 100644 > --- a/mm/rmap.c > +++ b/mm/rmap.c > @@ -731,22 +731,12 @@ void try_to_unmap_flush_dirty(void) > try_to_unmap_flush(); > } > > -/* > - * Bits 0-14 of mm->tlb_flush_batched record pending generations. > - * Bits 16-30 of mm->tlb_flush_batched bit record flushed generations. > - */ > -#define TLB_FLUSH_BATCH_FLUSHED_SHIFT 16 > -#define TLB_FLUSH_BATCH_PENDING_MASK \ > - ((1 << (TLB_FLUSH_BATCH_FLUSHED_SHIFT - 1)) - 1) > -#define TLB_FLUSH_BATCH_PENDING_LARGE \ > - (TLB_FLUSH_BATCH_PENDING_MASK / 2) > - > static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval, > unsigned long start, unsigned long end) > { > struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; > - int batch; > bool writable = pte_dirty(pteval); > + unsigned int pending, flushed; > > if (!pte_accessible(mm, pteval)) > return; > @@ -755,23 +745,21 @@ static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval, > tlb_ubc->flush_required = true; > > /* > - * Ensure compiler does not re-order the setting of tlb_flush_batched > + * Ensure compiler does not re-order the pending count update > * before the PTE is cleared. > */ > barrier(); > - batch = atomic_read(&mm->tlb_flush_batched); > -retry: > - if ((batch & TLB_FLUSH_BATCH_PENDING_MASK) > TLB_FLUSH_BATCH_PENDING_LARGE) { > - /* > - * Prevent `pending' from catching up with `flushed' because of > - * overflow. Reset `pending' and `flushed' to be 1 and 0 if > - * `pending' becomes large. > - */ > - if (!atomic_try_cmpxchg(&mm->tlb_flush_batched, &batch, 1)) > - goto retry; > - } else { > - atomic_inc(&mm->tlb_flush_batched); > - } > + atomic_inc(&mm->tlb_flush_batch_pending); > + > + /* > + * If no flusher ran for a long time, "pending" could wrap around and > + * catch up with "flushed". Move "flushed" forward to prevent that. > + */ > + pending = atomic_read(&mm->tlb_flush_batch_pending); > + flushed = atomic_read(&mm->tlb_flush_batch_flushed); > + if (unlikely(pending - flushed > INT_MAX)) > + /* Pairs with atomic_read_acquire() in flush_tlb_batched_pending() */ > + atomic_set_release(&mm->tlb_flush_batch_flushed, pending - 1); > > /* > * If the PTE was dirty then it's best to assume it's writable. The > @@ -805,25 +793,29 @@ static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags) > * if required. This will cost one additional flush per reclaim cycle paid > * by the first operation at risk such as mprotect and mumap. > * > - * This must be called under the PTL so that an access to tlb_flush_batched > - * that is potentially a "reclaim vs mprotect/munmap/etc" race will synchronise > + * This must be called under the PTL so that an access to the batch counters > + * that is potentially a "reclaim vs mprotect/munmap/etc" race will synchronize > * via the PTL. > */ > void flush_tlb_batched_pending(struct mm_struct *mm) > { > - int batch = atomic_read(&mm->tlb_flush_batched); > - int pending = batch & TLB_FLUSH_BATCH_PENDING_MASK; > - int flushed = batch >> TLB_FLUSH_BATCH_FLUSHED_SHIFT; > + /* > + * Read flushed before pending. Pairs with atomic_set_release() in > + * set_tlb_ubc_flush_pending(), which updates flushed without a flush. > + */ > + const int flushed = atomic_read_acquire(&mm->tlb_flush_batch_flushed); > + const int pending = atomic_read(&mm->tlb_flush_batch_pending); > > - if (pending != flushed) { > - flush_tlb_mm(mm); > - /* > - * If the new TLB flushing is pending during flushing, leave > - * mm->tlb_flush_batched as is, to avoid losing flushing. > - */ > - atomic_cmpxchg(&mm->tlb_flush_batched, batch, > - pending | (pending << TLB_FLUSH_BATCH_FLUSHED_SHIFT)); > - } > + if (pending == flushed) > + return; > + > + flush_tlb_mm(mm); > + /* > + * Only set "flushed" equal to "pending" after flushing the TLB. > + * Racing flushers that store an older value can only cause an extra > + * flush. > + */ > + atomic_set(&mm->tlb_flush_batch_flushed, pending); > } > #else > static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval, > > --- > base-commit: 1c1815d259fc11f14cfd80f8cc37fc067bf6c69e > change-id: 20261123-rmap-9b4b4cd2f886 > > Best regards, > -- > David Hildenbrand (Arm) > -- Cheers, Lorenzo