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:34:03 EST
... coming back to this.
>> This can be reproduced easily by swapping out an anon memory range with
>> MADV_PAGEOUT, reading it back in, and swapping it out again
>> (MADV_PAGEOUT) while one thread keeps reading the range and another
>> thread writes to pages as soon as reclaim removed the folios from the
>> swap-cache. The first thread will occasionally read the old content.
>
> Who would have thought about it? I also wonder why we never get a report
> for this issue.
>
I am asking myself the same question. It's rather obvious how this can happen
once you know about the race.
>>
>> 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.
>>
>> (b) Flush the TLB once, and unlock all folios that are still locked.
>>
>> (c) Lock the folios again, one at a time, to try reclaiming them.
>> Activate folios that are still or again mapped, and skip folios
>> that are under writeback or poisoned by now, or that somebody else
>> holds locked.
>>
>> As others might map or unmap folios while they are unlocked, we might
>> now activate or reclaim some folios that we would previously have kept
>> or activated, which is fine.
>>
>> Keeping folios locked until the TLB flush makes sure that nobody can
>> remove them from the page-cache / swap-cache, copy them or start
>> writeback. Page faults will either wait on the folio lock, or find the
>> old folio and map it again, which is fine.
>>
>> Unlocking folios before trying to reclaim them makes sure that we never
>> hold more than one folio lock while performing I/O or waiting on the
>> filesystem.
>>
>> The idea of unlocking folios and locking them again using a trylock
>> before trying to reclaim them was inspired by [1] from Zhang Peng.
>
> This solution makes sense to me.
>
Thanks!
[...]
>> +static struct folio *staged_folios_next(struct staged_folios *staged,
>> + struct list_head *folio_list)
>> +{
>> + struct folio *folio;
>> +
>> + if (staged_folios_empty(staged))
>> + return NULL;
>> + /* Collect more folios until we have enough or there are no more. */
>> + if (staged->nr_folios < MAX_STAGED_FOLIOS && !list_empty(folio_list))
>
> One thing to note here is that !list_empty(folio_list) can happen
> in the middle of phase (c), when pageout() splits a large shmem folio,
> and the code switches back to phase (a) and folios on ->flushed
> will stay unlocked longer. This is discovered by AI and I verified it.
Ack.
>
> To make the code strictly follows the comment, you will want to
> add ->draining and set it to true at phase (c) and back to false
> at phase (a) and check it instead of !list_empty(folio_list).
Sounds good. I'll use "reclaiming".
>> retry:
>> - while (!list_empty(folio_list)) {
>> + /*
>> + * We take folios from folio_list to pre-process them (lock, check,
>> + * unmap). Then, they move to the staged(unflushed) list, from where
>> + * they move to the staged(flushed) list after a batched TLB flush
>> + * and after unlocking them. If no TLB flush is pending for a folio,
>> + * it moves to the staged(flushed) list directly.
>> + *
>> + * We then take folios from the staged(flushed) list to actually
>> + * reclaim them, or to activate them if they are still mapped.
>> + *
>> + * So we pre-process folios from folio_list until we collected a batch
>> + * of staged folios, or until there are no more folios to
>> + * pre-process. Then, we perform a single TLB flush and reclaim all
>> + * folios of that batch, before we pre-process more folios.
>> + *
>> + * Once all lists are empty, we are done.
>
> I wonder if you want to label the above three phases/steps and
> add comments in staged_folios_next() to clarify how phase transition
> happens.
Ack, will do. But this will need proper cleanups on top to cleanly split all of
that.
Thanks!
--
Cheers,
David