Re: [PATCH v2] mm: free the swap slot of a dirty swapin folio

From: Joshua Hahn

Date: Mon Nov 30 2026 - 13:36:50 EST


On Fri, Nov 27, 2026 at 03:23:27PM +0100, Alexandre Ghiti wrote:
> do_swap_page() frees the swap cache of an exclusive folio on a write
> fault only. But when the folio is already dirty, the copy in its swap
> slot is stale: the slot only reserves swap space until the folio is
> reclaimed again or its owner exits, and the folio has to be written out
> again anyway.

[... snip ...]

> base patched
> exit time 989 +- 24 ms 502 +- 28 ms (-49%)
>
> The cost is paid by folios that keep cycling between zswap and memory:
> each reclaim allocates a new slot and adds the folio to the swap cache
> again. On a kernel build in a 600 MiB memcg (-j4, defconfig), this is
> not measurable (mean +- stddev of 5 runs on bare metal):
>
> base patched
> time (s) 592.4 +- 1.1 592.4 +- 0.9 (+0.0%)
> sys (s) 256.6 +- 1.5 257.5 +- 1.4 (+0.3%)
>
> In the worst case, random reads over 4 GiB of anonymous memory in a
> 3 GiB memcg, so that about half the reads are zswap read faults and the
> task spends all its time in the kernel, throughput drops by 1.1-1.4%
> (mean +- stddev of 5 runs on bare metal):
>
> base patched
> reads/s, 1 thread 37310 +- 182 36889 +- 77 (-1.1%)
> reads/s, 8 threads 291937 +- 802 287954 +- 373 (-1.4%)
>
> Signed-off-by: Alexandre Ghiti <alex@xxxxxxxx>

Hi Alex,

Thank you for the patch! This patch makes sense and looks good to me.
The numbers are also really great!

Sorry to do this, but I just have one nit below...

> ---
>
> Changes in v2:
> - Free the swap cache of a dirty folio even when not exclusive (David)
> - Make the comment and the changelog generic, zswap being the common
> case (David, Nhat)
>
> v1: https://lore.kernel.org/all/20261126094318.244973-1-alex@xxxxxxxx/
>
> mm/memory.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/mm/memory.c b/mm/memory.c
> index d477a4f986bc..c83c67cb759b 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -4711,9 +4711,12 @@ static inline bool should_try_to_free_swap(struct swap_info_struct *si,
> return true;
>
> /*
> - * Free the swapcache only if we are the exclusive user and
> - * this is a write fault.
> + * Free the swapcache if the folio is already dirty, or if we will dirty it
> + * immediately afterwards by writing to it.
> */

I'm not sure this comment really helps explain why being dirty matters
here, which I think is the interesting insight captured by your commit
message. The idea is that dirty folio == swap slot is stale, right?

What about:

/*
* Free the swapcache if the folio is dirty (it has diverged from its
* swap slot) or if an exclusive write will make it diverge.
*/

Let me know what you think. Regardless I think the change is good, so
please feel free to add:
Reviewed-by: Joshua Hahn <joshua.hahnjy@xxxxxxxxx>

Thanks again Alex! Have a great day,
Joshua

> + if (folio_test_dirty(folio))
> + return true;
> +
> return (fault_flags & FAULT_FLAG_WRITE) && exclusive;
> }
>
> --
> 2.53.1-Meta
>
>