Re: [PATCH 1/4] mm: flush stale TLB entries before installing a CoW'ed folio

From: Jose A. Perez de Azpillaga

Date: Wed Nov 18 2026 - 17:35:17 EST


On 11/18/26 10:20 AM, David Hildenbrand (Arm) wrote:
> Zapping PTEs in zap_pte_range() defers the TLB flush until after dropping
> the PTL for clean page-cache folios.
>
> A concurrent write fault in a MAP_PRIVATE mapping can then CoW the
> page-cache folio and install a new anon folio without a prior TLB
> flush:
>
> CPU0 CPU1
> LOAD(ADDR)
> -> #PF
> installs R/O PTE -> A
> fuse_open()
> -> invalidate_inode_pages2()
> -> zap_pte_range()
> clears R/O PTE
> // delayed flush
> // Other CPUs (incl CPU0) still cache ADDR -> A
> STORE(ADDR)
> -> #PF
> do_cow_fault()
> copies A to B, installs R/W PTE -> B
> // no TLB flush: PTE was none
>
> CPUs with stale TLB entries will still read from the old folio while writes
> go to the new folio, until the TLB is flushed.
>
> We really must process pending TLB flushes if we are not faulting the
> same folio back in.
>
> Fix it by flushing the TLB before installing an anon (CoW'ed) folio in
> set_pte_range() if a TLB flush is pending. Zapping itself stays as fast
> as before.
>
> Other faults are not affected: unmap_mapping_*() users only remove
> folios from the page-cache after their own TLB flush, so until then, a
> refault maps the same folio again. The exception is a concurrent
> invalidation that removes the already-unmapped folio from the
> page-cache before the zapper flushed the TLB; that is an unlikely corner
> case that we'll not handle for now.
>
> Note that we don't care about MADV_DONTNEED followed by a new read
> fault, as MADV_DONTNEED only guarantees its semantics once the operation
> completed. CoW faults will flush the TLB either way.
>
> For example, imagine that MADV_DONTNEED zaps a page-cache folio and
> doesn't flush the TLB (but still holds a reference). If the page-cache
> replaces the folio, a new read fault might map the new folio without
> flushing the TLB. That's okay, as MADV_DONTNEED did not complete yet.
> Same applies to MADV_DONTNEED of CoW'ed folios when re-faulting the
> page-cache folio.
>
> Memory reclaim, which removes folios from the page-cache before performing
> its own batched TLB flush, will be handled separately.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Reported-by: Cenk Yılmaz <cenk@xxxxxxxxxxxxxxxxxxx>
> Debugged-by: Cenk Yılmaz <cenk@xxxxxxxxxxxxxxxxxxx>
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

LGTM:

Reviewed-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>

--
cheers,
jose a. p-a