The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Kairui Song <ryncsn@gmail.com>, Zhang Peng <zippermonkey@icloud.com>
Cc: "Lorenzo Stoakes" <ljs@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Borislav Petkov (AMD)" <bp@alien8.de>,
	"Cenk Yılmaz" <cenk@deepfinresearch.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>,
	"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>,
	"Rik van Riel" <riel@redhat.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>,
	"Andrea Arcangeli" <aarcange@redhat.com>,
	"Thomas Gleixner" <tglx@kernel.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH 4/4] RFC: mm/vmscan: unlock unmapped folios only after the deferred TLB flush
Date: Sun, 29 Nov 2026 12:30:19 +0100	[thread overview]
Message-ID: <a4e42a3c-de26-40f1-8899-ae3f551ee3de@kernel.org> (raw)
In-Reply-To: <CAMgjq7DWWCF_iB+GmTciuXr=m=uk7mMmidvY=8yg6MH98X4oPg@mail.gmail.com>

On 11/29/26 12:20, Kairui Song wrote:
> On Wed, Nov 18, 2026 at 5:22 PM David Hildenbrand (Arm)
> <david@kernel.org> wrote:
>> try_to_unmap() with TTU_BATCH_FLUSH zaps the PTEs, transfers the PTE
>> dirty bit to the folio and defers the TLB flush. Until the TLB is
>> flushed, other CPUs might still access the folio through stale TLB
>> entries.
> 
> Thanks for taking care of this!

Thanks for the review, I'm planning on sending #3 and #4 out soon.

> 
>> Fix it by reclaiming folios in batches of up to 64 folios:
>>
>>  (a) Lock each folio, check whether we want to reclaim it, and unmap it.
>>      Keep the folio locked if any deferred TLB flush is pending after
>>      unmapping it, otherwise unlock it right away.
> 
> I'm not sure locking that many folios at once is safe under pressure.
> Our first TLB batch flush series internally kept every folio locked,
> but we found that unlock them improved performance further for
> some workloads so we switched to the posted version from Peng.

Yeah, I don't see a way around not destorying performance:

I have further below.

"Note that khugepaged and batched (async) folio migration already keep up
to HPAGE_PMD_NR (e.g., 512 with 4 KiB pages) small folios locked after
unmapping them with a deferred TLB flush; migration even allocates new
folios, possibly entering direct reclaim, before flushing the TLB and
unlocking them. Both also call MMU notifiers while keeping other folios
locked, just like we do now."

> But to fix the flush issue we do have to keep them locked though.

Yes, unfortunately.

> 
> There is no deadlock, but seems fragile. e.g. shmem_get_folio_gfp()
> takes a plain folio_lock(), so a thread faulting a parked folio waits for
> the whole batch while holding mmap_lock, and the batch can be
> blocked by any non-trylock part of rmap/splitting. The per-folio hold
> time is much longer too. Maybe we can optimize other parts later
> instead if there is any actual regression.

Yes!

> 
>> Allocating swap space can otherwise only sleep when allocating memory
>> for the swap table, which cannot enter direct reclaim or compaction,
>> as reclaim runs with PF_MEMALLOC.
> 
> I'm trying to make the swap table allocation sleepless, so we no longer
> need to worry about that :-D Lian and Chris are pushing on the swap
> queue series and it should come after that.

That would be nice" :)

> 
>> +               if (!may_sync_discard)
>> +                       return -EBUSY;
>>                 if (swap_sync_discard())
>>                         goto again;
> 
> Hopefully the swap side can get rid of this too. The discard attempt is a
> historical thing to avoid OOM on super slow discarding devices, reclaim
> throttling (which is being unified for MGLRU) can handle that instead.

Ack!

> 
>> +struct staged_folios {
>> +       /* Locked folios, waiting for the TLB flush. */
>> +       struct list_head unflushed;
>> +       /* Unlocked folios, after the TLB flush (if required). */
>> +       struct list_head flushed;
>> +       /* Number of folios in the current batch. */
>> +       unsigned int nr_folios;
>> +};
> 
> Using a list is usually bad for performance IMO, Peng used a folio_batch,
> which I'm also a bit concerned about stack usage-wise.. The overhead
> could be trivial though, I tested this series and it's working

Yeah, if a batch is a better fit (and we can tolerate stack usage) we should
ideally optimize it on top of this here.

> 
> Ultimately I think we want just one or two unified batches for the whole
> reclaim loop: from the very begiing of the call stack,
> SWAP_CLUSTER_MAX is 32 which is the typical starting budget
> under pressure, close to the folio_batch limit 31 already, and
> we have a few of them already too.

Ack.

> 
>> +       /* Collect more folios until we have enough or there are no more. */
>> +       if (staged->nr_folios < MAX_STAGED_FOLIOS && !list_empty(folio_list))
>> +               return NULL;
>> +
>> +       /* Flush the TLB and unlock the folios that waited for the flush. */
>> +       staged_folios_flush(staged);
> 
> folio_list can get repopulated from shmem (pageout() passes it to
> shmem_writeout()), so the loop can leave N already-flushed folios behind and
> get NULL from it. Should be fine, but it blurs the two stages.

Yes, I didn't find a simple way to improve that without reshuffling the code
massively.

> 
>> +       while (!list_empty(folio_list) || !staged_folios_empty(&staged)) {
> ...
>> +               folio = staged_folios_next(&staged, folio_list);
>> +               if (folio) {
>> +                       nr_pages = folio_nr_pages(folio);
>> +                       goto reclaim_staged;
>> +               }
> 
> This looks... really weird; it will be much cleaner with Peng's cleanup I
> guess.

Yes, please do cleanups on top once we have it fixed. This all needs to be
separately cleanly into stages.

> 
>> +reclaim_staged:
>> +               if (!folio_trylock(folio))
>> +                       goto keep;
>> +
>> +               /*
>> +                * We failed to unmap the folio, or it was mapped again while it
>> +                * was unlocked.
>> +                */
>> +               if (folio_mapped(folio))
>> +                       goto activate_locked;
>> +               /*
>> +                * While the folio was unlocked, writeback might have started, or
>> +                * it might have been poisoned.
>> +                */
>> +               if (folio_test_writeback(folio) ||
>> +                   folio_contain_hwpoisoned_page(folio))
>> +                       goto keep_locked;
> 
> The writeback part may better at least have a folio_set_reclaim() so they
> don't miss a rotation? Better if we can reuse the pre-stage checks by
> splitting that part out properly.

Is that something you want me to change in this patch?

> 
> I tested this on x86 under pressure (40 GiB in one process with
> swap, 4 processes concurrently, and 4 memcg-limited cgroups reclaiming
> concurrently): TLB shootdown IPIs dropped by 90%, wall time by 30%.
> Clean page cache seems unchanged, and on arm64 nothing changes
> either. Really nice result!

Thanks for the test! That's what I was expecting in the common case. It took a
while for me to figure out how to have the patch minimal and yet not harm
performance ... and while it even improve performance for the dirty case.

> 
> So it looks good to me, just a few nits and a lot of cleanup to be done.

Thanks! Yes, I hope you guys can figure out cleanups on top of this.

Is there anything specific you want me to tackle in this patch before I resend?


-- 
Cheers,

David

  reply	other threads:[~2026-11-29 11:30 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  9:20 [PATCH 0/4] mm: fixes for stale TLB entries due to delayed/batched TLB flushes David Hildenbrand (Arm)
2026-11-18  9:20 ` [PATCH 1/4] mm: flush stale TLB entries before installing a CoW'ed folio David Hildenbrand (Arm)
2026-11-18 13:55   ` Cenk Yılmaz
2026-11-18 22:35   ` Jose A. Perez de Azpillaga
2026-11-19 16:47   ` Zi Yan
2026-11-19 16:49     ` David Hildenbrand (Arm)
2026-11-19 17:04       ` Zi Yan
2026-11-19 17:07         ` David Hildenbrand (Arm)
2026-11-20 17:39   ` Vlastimil Babka (SUSE)
2026-11-18  9:20 ` [PATCH 2/4] mm/vmscan: flush deferred writable TLB entries before unlocking the folio David Hildenbrand (Arm)
2026-11-18 22:35   ` Jose A. Perez de Azpillaga
2026-11-19  0:13   ` Johannes Weiner
2026-11-19 21:47   ` Zi Yan
2026-11-20 17:42   ` Vlastimil Babka (SUSE)
2026-11-18  9:20 ` [PATCH 3/4] RFC: mm/rmap: don't defer TLB flushes when unmapping lazyfree folios David Hildenbrand (Arm)
2026-11-19 22:10   ` Zi Yan
2026-11-18  9:20 ` [PATCH 4/4] RFC: mm/vmscan: unlock unmapped folios only after the deferred TLB flush David Hildenbrand (Arm)
2026-11-23  4:08   ` Zi Yan
2026-11-29 11:20   ` Kairui Song
2026-11-29 11:30     ` David Hildenbrand (Arm) [this message]
2026-11-29 11:41       ` Kairui Song
2026-11-29 12:44       ` Zhang Peng
2026-11-20  9:00 ` [PATCH 0/4] mm: fixes for stale TLB entries due to delayed/batched TLB flushes David Hildenbrand (Arm)
2026-11-20  9:01 ` (subset) " David Hildenbrand

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=a4e42a3c-de26-40f1-8899-ae3f551ee3de@kernel.org \
    --to=david@kernel.org \
    --cc=aarcange@redhat.com \
    --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=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=riel@redhat.com \
    --cc=rppt@kernel.org \
    --cc=ryncsn@gmail.com \
    --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