Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Cc: "Liam R. Howlett" <liam@infradead.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Borislav Petkov (AMD)" <bp@alien8.de>,
	"Cenk Yılmaz" <cenk@deepfinresearch.com>,
	"Zhang Peng" <zippermonkey@icloud.com>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Michal Hocko" <mhocko@suse.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Zi Yan" <ziy@nvidia.com>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Joshua Hahn" <joshua.hahnjy@gmail.com>,
	"Byungchul Park" <byungchul@sk.com>,
	"Gregory Price" <gourry@gourry.net>,
	"Ying Huang" <ying.huang@linux.alibaba.com>,
	"Alistair Popple" <apopple@nvidia.com>,
	"Chris Li" <chrisl@kernel.org>,
	"Kairui Song" <kasong@tencent.com>,
	"Kemeng Shi" <shikemeng@huaweicloud.com>,
	"Nhat Pham" <nphamcs@gmail.com>,
	"Baoquan He" <baoquan.he@linux.dev>,
	"Barry Song" <baohua@kernel.org>,
	"Youngjun Park" <youngjun.park@lge.com>,
	"Mel Gorman" <mgorman@suse.de>,
	"Sasha Levin" <sasha.levin@oracle.com>,
	"Ingo Molnar" <mingo@kernel.org>, "Peter Xu" <peterx@redhat.com>,
	"Hugh Dickins" <hughd@google.com>,
	"Axel Rasmussen" <axelrasmussen@google.com>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Michal Hocko" <mhocko@kernel.org>,
	"Qi Zheng" <qi.zheng@linux.dev>,
	"Shakeel Butt" <shakeel.butt@linux.dev>,
	"Yuanchu Xie" <yuanchu@google.com>, "Wei Xu" <weixugc@google.com>,
	"Baolin Wang" <baolin.wang@linux.alibaba.com>,
	"Thomas Gleixner" <tglx@kernel.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH v2 2/2] mm/vmscan: unlock unmapped folios only after the deferred TLB flush
Date: Wed, 2 Dec 2026 22:35:59 +0100	[thread overview]
Message-ID: <e22f00be-8914-4b42-8377-07bba44a5fd5@kernel.org> (raw)
In-Reply-To: <axB7IBBaEEbsMrgl@gremlin>

[...]

>>  (1) Cleanly separate the stages of shrink_folio_list() (see the TODO).
>>
>>  (2) Avoid unlocking and locking folios again that do not require a TLB
>>      flush. But only if it's worth it and doesn't make the code too
>>      ugly.
>>
>>  (3) Remove the remaining try_to_unmap_flush() calls before freeing
>>      folios; we only free folios that we flushed already or that we
>>      never unmapped.
>>
>>  (4) Remove the now-unused try_to_unmap_flush_dirty() infrastructure.
>>
>> Fixes: 72b252aed506 ("mm: send one IPI per CPU to TLB flush all entries after unmapping pages")
>> Link: https://lore.kernel.org/linux-mm/20261031-batch-tlb-flush-v6-0-190539839eec@tencent.com/ [1]
>> Cc: stable@vger.kernel.org
>> Acked-by: Zi Yan <ziy@nvidia.com>
>> Reviewed-by: Kairui Song <kasong@tencent.com>
>> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
> 
> Hmm this seems like a really heavyweight and significant change to be
> backporting, especially so far.
> > It's like you're implementing a whole new mechanism here, but also backporting
> it.
> 

No way around fixing it without destroying performance, I'm afraid. I tried and
it would mean giving up delayed tlb flushing completely for reclaim, which is
just unacceptable.

It's a slightly biggger fix, yes, but I tried to be as little intrusive as
possible. That's why I'm not sending it as a hotfix but will give it a bit of
soaking time in -next.

> I wonder a bit how the net of these backported patches are going to interact
> honestly. But maybe I worry too much:)
> 
> Maybe I'm just a bit frustrated at the rate of change going on ATM and how
> completely unmanageable it is to keep up with it all.
> 

Yeah. Fortunately we have memory reclaim experts around that know this code
better than I do.

> I fear waking up to an mm with about 20 new mechanisms I wasn't aware of + now I
> have to figure them out/support them/etc :)))
> 

Yes, same feeling here having to write fixes for serious problems on a weekly basis.

[...]

>> +	/*
>> +	 * Don't wait for synchronous discard I/O while keeping other folios
>> +	 * locked: there is potential for deadlocks and long delays.
>> +	 */
>> +	staged_folios_flush(staged);
>> +	may_sync_discard = true;
>> +	return folio_alloc_swap(folio, may_sync_discard);
>> +}
>> +
> 
> I genuinely do not think I've ever seen a Cc: stable patch add this much before,
> it's really crazy.

Oh, you didn't see my PAE patches back then? This here is trivial lol.

> 
> Can we find some way of breaking this up or something? :)

I wrote this fix to minimally impact existing code. We are just adding helpers
here to make the changes to shrink_folio_list() below as minimal as possible.

I don't see how splitting up the current patch would make any sense.

Doing the full thing with cleanups first would be something different, but there
is no way we could backport that.

[...]

>> -	while (!list_empty(folio_list)) {
>> +	/*
>> +	 * We reclaim folios in batches, in three phases:
>> +	 *
>> +	 * (a) Collect: we take folios from folio_list to pre-process them
>> +	 *     (lock, check, unmap). Then, they move to the staged(unflushed)
>> +	 *     list. If no TLB flush is pending for a folio, it moves to the
>> +	 *     staged(flushed) list directly.
>> +	 *
>> +	 * (b) Flush: once we collected a batch of staged folios, or there are
>> +	 *     no more folios to pre-process, we perform a single TLB flush and
>> +	 *     move the staged(unflushed) folios to the staged(flushed) list
>> +	 *     after unlocking them.
>> +	 *
>> +	 * (c) Reclaim: we take folios from the staged(flushed) list to actually
>> +	 *     reclaim them, or to activate them if they are still mapped. Once
>> +	 *     we took all folios of the batch, we start collecting again (a).
>> +	 *
>> +	 * Once all lists are empty, we are done.
>> +	 *
>> +	 * TODO: the stages of this algorithm should be cleanly separated.
> 
> I mean this TODO is I guess you intended this patch to be reworked right? :)

You mean the code? Yes, as also spelled out in the description above. Not this
patch.

[...]

>> -			/*
>> -			 * 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();
>> +			unmapped = true;
> 
> This seems like you're throwing in an optimisation for fun here too, which isn't
> really great.

I think it's the same thing we effectively had before: if nobody set delayed
flushes, no flush for the folio was required.

So it's not something new. if we didn't unmap, there is no need to flush for
this folio.

> 
> But claude raised the fact that PTE markers rely on this too (e.g. guard regions
> can be applied to file-backed mappings, but also UFFDIO_POISON) and you'll get
> writers writing through stale TLB entries this way, which seems like a really
> bad (TM) thing.

There might be other delayed TLB flushing issues, sure. I would be surprised if
there wouldn't be some, because it's complicated.

I spelled one out in the cover letter of v1.

We have flush_tlb_batched_pending() to sync against exactly what you describe.

I am pretty sure guard marker installation so far missed to call
flush_tlb_batched_pending(). They really have to if they want to make sure no
stale TLB entries can be used.

zapping will call flush_tlb_batched_pending(), but if guard markers don't need
that, there might be stale tlb entries (which wont cause corruption or anything
like that).

I had uffd in mind when working on this. But I was able to convince myself
(well, AI helped) that it's just no something relevant to happen for uffd.

uffd will just fail if something is already mapped. Which is why it's only used
either after zapping (flush_tlb_batched_pending()) or on unpopulated ranges. so
the race doesn't matter, in particular, about UFFDIO_POISON.

Even for guard markers, there is only a tiny time window where you could still
access the stale entries (while the folio is still in the page cache). It's hard
to believe that that's really relevant in practice.

The right fix would be to throw in a flush_tlb_batched_pending(), if we care.

But that's completely independent of the changes here. That's just "syncing with
reclaim" in a way people envisioned. Unfortunately.

> 
> It's gross to have to 'just know' to think about these things here but I think
> this should be reconsidered?

Yes, a lot of the delayed tlb flushing is "you have to do the right thing". But
I don't see us changing that in a fix :)

We'll definetly need better docs, and I packed quite some information into my
recent tlb flushing commit descriptions + cover letters.

[...[

>>
> 
> I think the rest of it needs to be chopped up to be really reviewable, or at
> least it's late here so :)

This is not really the material you're supposed to review after a long day :) .

If Kairui, Zi and others that are familiar with this code give it their
blessing, it should be good enough as a fix.

-- 
Cheers,

David


  reply	other threads:[~2026-12-02 21:36 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 16:25 [PATCH v2 0/2] mm: fixes for stale TLB entries due to delayed/batched TLB flushes David Hildenbrand (Arm)
2026-12-01 16:25 ` [PATCH v2 1/2] mm/rmap: don't defer TLB flushes when unmapping lazyfree folios David Hildenbrand (Arm)
2026-12-02 19:49   ` Lorenzo Stoakes (ARM)
2026-12-02 20:36     ` David Hildenbrand (Arm)
2026-12-02 20:39       ` Lorenzo Stoakes (ARM)
2026-12-02 20:40       ` David Hildenbrand (Arm)
2026-12-02 20:52         ` Lorenzo Stoakes (ARM)
2026-12-01 16:25 ` [PATCH v2 2/2] mm/vmscan: unlock unmapped folios only after the deferred TLB flush David Hildenbrand (Arm)
2026-12-02 20:37   ` Lorenzo Stoakes (ARM)
2026-12-02 21:35     ` David Hildenbrand (Arm) [this message]
2026-12-02 13:47 ` [PATCH v2 0/2] mm: fixes for stale TLB entries due to delayed/batched TLB flushes David Hildenbrand (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e22f00be-8914-4b42-8377-07bba44a5fd5@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=apopple@nvidia.com \
    --cc=axelrasmussen@google.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=baoquan.he@linux.dev \
    --cc=bp@alien8.de \
    --cc=byungchul@sk.com \
    --cc=cenk@deepfinresearch.com \
    --cc=chrisl@kernel.org \
    --cc=gourry@gourry.net \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=joshua.hahnjy@gmail.com \
    --cc=kasong@tencent.com \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=matthew.brost@intel.com \
    --cc=mgorman@suse.de \
    --cc=mhocko@kernel.org \
    --cc=mhocko@suse.com \
    --cc=mingo@kernel.org \
    --cc=nphamcs@gmail.com \
    --cc=peterx@redhat.com \
    --cc=qi.zheng@linux.dev \
    --cc=rppt@kernel.org \
    --cc=sasha.levin@oracle.com \
    --cc=shakeel.butt@linux.dev \
    --cc=shikemeng@huaweicloud.com \
    --cc=stable@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=tglx@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=weixugc@google.com \
    --cc=ying.huang@linux.alibaba.com \
    --cc=youngjun.park@lge.com \
    --cc=yuanchu@google.com \
    --cc=zippermonkey@icloud.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox