Re: [PATCH 3/4] RFC: mm/rmap: don't defer TLB flushes when unmapping lazyfree folios

From: Zi Yan

Date: Thu Nov 19 2026 - 17:10:50 EST


On 18 Nov 2026, at 4:20, David Hildenbrand (Arm) wrote:

> When reclaim discards a lazyfree folio, it clears the PTE but delays the
> TLB flush. Until flushed, mapping a new folio into the now-none PTE
> can leave some CPUs still reading from the old folio while others
> already write to the new folio.
>
> This is hard to trigger in practice and would mostly look like a weird
> use-after-free bug in an allocator that uses MADV_FREE, but it can be
> reproduced reliably with three threads pinned to different CPUs:
>
> (1) Thread A populates the pages, MADV_FREE's them and then
> MADV_PAGEOUT's them.
> (2) Thread B reads all pages to keep TLB entries cached and verifies the
> content of pages published by thread C.
> (3) Thread C waits until mincore() indicates that a page is no longer
> present, writes a new value to it, and publishes it to thread B.
>
> Thread B can then read the old value after the new folio was mapped and
> the content updated.
>
> One way to handle it would be adding conditional flushes to
> do_anonymous_page(), migrate_vma_insert_page(), UFFDIO_COPY and
> UFFDIO_MOVE ... essentially anywhere we install new anonymous folios,
> which would mean that these places flush quite frequently on concurrent
> reclaim.
>
> To perform targeted flushes, we'd have to keep some PTE markers behind,
> which is also rather nasty and not an easy fix.

Maybe that is the long term solution. We basically overloads p*d_none()
with multiple meanings.

>
> So let's not defer the TLB flush for lazyfree folios, which includes
> folios in VM_DROPPABLE mappings.
>
> To avoid flushing if the folio was redirtied or has additional
> references, perform all non-deferred TLB flushes only once we know that
> we won't restore the PTEs, right before removing the rmap.
>
> The downside is that we no longer batch TLB flushes when discarding
> lazyfree folios, which harms performance, but is the right thing to do
> for now.
>
> Fixes: 854e9ed09ded ("mm: support madvise(MADV_FREE)")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> ---
> mm/rmap.c | 15 +++++++++++++--
> 1 file changed, 13 insertions(+), 2 deletions(-)
>
Anyway,

Acked-by: Zi Yan <ziy@xxxxxxxxxx>

Best Regards,
Yan, Zi