Re: [PATCH 2/4] mm/vmscan: flush deferred writable TLB entries before unlocking the folio

From: Vlastimil Babka (SUSE)

Date: Fri Nov 20 2026 - 12:44:05 EST


On 11/18/26 10:20, David Hildenbrand (Arm) wrote:
> try_to_unmap() with TTU_BATCH_FLUSH transfers the PTE dirty bit to the
> folio and defers the TLB flush.
>
> Reclaim must flush the TLB before unlocking the folio, otherwise stale
> writable TLB entries might be left behind temporarily: writeback started
> by other mechanisms (e.g., fsync) will not find any mappings in
> folio_mkclean(), so it will neither write-protect nor flush anything.
>
> A write through a stale TLB entry after the folio was cleaned gets lost
> once the folio is reclaimed.
>
> However, reclaim currently does not flush the TLB if the now-dirty folio
> does not get written back (e.g., gets activated or kept instead). Fix it
> by flushing right after try_to_unmap() if the folio is dirty, which makes
> the existing flush before pageout() unnecessary.
>
> For dirty page-cache folios that reclaim activates, we will now flush the
> TLB immediately after unmapping them instead of batching the flush, which
> is the right thing to do. Performance-wise, it shouldn't really matter
> much, because folios with dirty PTEs are usually young and reclaim won't
> even try to unmap them.
>
> Fixes: d950c9477d51 ("mm: defer flush of writable TLB entries")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

Acked-by: Vlastimil Babka (SUSE) <vbabka@xxxxxxxxxx>

> ---
> mm/vmscan.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/mm/vmscan.c b/mm/vmscan.c
> index 5495d9be90e88..af7831643ecf5 100644
> --- a/mm/vmscan.c
> +++ b/mm/vmscan.c
> @@ -1455,6 +1455,12 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
> flags |= TTU_SYNC;
>
> try_to_unmap(folio, flags);
> + /*
> + * Flush deferred writable TLB entries before we could
> + * drop the folio lock and others could start writeback.
> + */
> + if (folio_test_dirty(folio))
> + try_to_unmap_flush_dirty();
> if (folio_mapped(folio)) {
> stat->nr_unmap_fail += nr_pages;
> if (!was_swapbacked &&
> @@ -1496,12 +1502,6 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
> if (!sc->may_writepage)
> goto keep_locked;
>
> - /*
> - * Folio is dirty. Flush the TLB if a writable entry
> - * potentially exists to avoid CPU writes after I/O
> - * starts and then write it out here.
> - */
> - try_to_unmap_flush_dirty();
> switch (pageout(&ctx, mapping, folio, folio_list)) {
> case PAGE_KEEP:
> goto keep_locked;
>