Re: [PATCH 4/4] RFC: mm/vmscan: unlock unmapped folios only after the deferred TLB flush

From: David Hildenbrand (Arm)

Date: Tue Dec 01 2026 - 10:35:11 EST


On 11/29/26 12:41, Kairui Song wrote:
> On Sun, Nov 29, 2026 at 7:30 PM David Hildenbrand (Arm)
> <david@xxxxxxxxxx> wrote:
>> On 11/29/26 12:20, Kairui Song wrote:
>>> On Wed, Nov 18, 2026 at 5:22 PM David Hildenbrand (Arm)
>>> <david@xxxxxxxxxx> wrote:
>>>
>>> Thanks for taking care of this!
>>
>> Thanks for the review, I'm planning on sending #3 and #4 out soon.
>>
>>>
>>>
>>> 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!
>>
>>>
>>>
>>> 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" :)
>>
>>>
>>>
>>> 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!
>>
>>>
>>>
>>> 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.
>>
>>>
>>>
>>> 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.
>>
>>>
>>> ...
>>>
>>> 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.
>>
>>>
>>>
>>> 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?
>
> I just noticed the folio_list issue was also pointed out by Zi, with
> that fixed I think it's good enough.
>
> The folio_set_reclaim part is kind of trivial
Yes, let me just include that.

--
Cheers,

David