* Re: [PATCH v2] mm: free the swap slot of a dirty swapin folio
2026-11-27 14:23 [PATCH v2] mm: free the swap slot of a dirty swapin folio Alexandre Ghiti
@ 2026-11-29 10:27 ` David Hildenbrand (Arm)
2026-11-30 18:36 ` Joshua Hahn
2026-11-30 19:11 ` Nhat Pham
2 siblings, 0 replies; 4+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-29 10:27 UTC (permalink / raw)
To: Alexandre Ghiti, Andrew Morton, linux-mm
Cc: linux-kernel, Barry Song, Kairui Song, Chris Li, Nhat Pham,
Johannes Weiner, Yosry Ahmed, Chengming Zhou, Baoquan He,
Kemeng Shi, Youngjun Park, Liam R . Howlett, Vlastimil Babka,
Mike Rapoport, Suren Baghdasaryan, Michal Hocko
On 11/27/26 15:23, 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.
>
> The common case is zswap: zswap_load() drops its compressed copy and
> dirties the folio, so every zswap read fault leaves such a folio behind.
> Without zswap, it only happens when reclaim bailed out after adding the
> folio to the swap cache but before writing it out, or when the swap
> write failed.
>
> Such folios add up, and they are expensive to free at exit: on a large
> production workload, the exit time doubled when zswap was enabled. A
> third of the increase comes from the folios left in the swap cache,
> each costing about 3x a resident page to tear down; the rest comes from
> freeing the zswap entries.
>
> So free the swap cache of a dirty folio as well: folio_free_swap() only
> releases the slot once no other page table references it. The only cost
> is a new slot allocation on the folio's next reclaim, which write faults
> already pay.
>
> On the production workload, this patch removed that third: the exit time
> decreased by about 15%. On a simple microbenchmark, 4 GiB of anonymous
> memory in a memcg pushed to swap, read back, then SIGKILLed in a QEMU
> guest (mean +- stddev of 9 runs):
>
> 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@ghiti.fr>
> ---
Reviewed-by: David Hildenbrand (Arm) <david@kernel.org>
I'll wait for more review before picking this up.
--
Cheers,
David
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] mm: free the swap slot of a dirty swapin folio
2026-11-27 14:23 [PATCH v2] mm: free the swap slot of a dirty swapin folio Alexandre Ghiti
2026-11-29 10:27 ` David Hildenbrand (Arm)
@ 2026-11-30 18:36 ` Joshua Hahn
2026-11-30 19:11 ` Nhat Pham
2 siblings, 0 replies; 4+ messages in thread
From: Joshua Hahn @ 2026-11-30 18:36 UTC (permalink / raw)
To: Alexandre Ghiti
Cc: Andrew Morton, linux-mm, linux-kernel, Barry Song,
David Hildenbrand, Kairui Song, Chris Li, Nhat Pham,
Johannes Weiner, Yosry Ahmed, Chengming Zhou, Baoquan He,
Kemeng Shi, Youngjun Park, Liam R . Howlett, Vlastimil Babka,
Mike Rapoport, Suren Baghdasaryan, Michal Hocko
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@ghiti.fr>
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@ghiti.fr/
>
> 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@gmail.com>
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
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] mm: free the swap slot of a dirty swapin folio
2026-11-27 14:23 [PATCH v2] mm: free the swap slot of a dirty swapin folio Alexandre Ghiti
2026-11-29 10:27 ` David Hildenbrand (Arm)
2026-11-30 18:36 ` Joshua Hahn
@ 2026-11-30 19:11 ` Nhat Pham
2 siblings, 0 replies; 4+ messages in thread
From: Nhat Pham @ 2026-11-30 19:11 UTC (permalink / raw)
To: Alexandre Ghiti
Cc: Andrew Morton, linux-mm, linux-kernel, Barry Song,
David Hildenbrand, Kairui Song, Chris Li, Johannes Weiner,
Yosry Ahmed, Chengming Zhou, Baoquan He, Kemeng Shi,
Youngjun Park, Liam R . Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, Michal Hocko
On Fri, Nov 27, 2026 at 7:24 AM Alexandre Ghiti <alex@ghiti.fr> 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.
>
> The common case is zswap: zswap_load() drops its compressed copy and
> dirties the folio, so every zswap read fault leaves such a folio behind.
> Without zswap, it only happens when reclaim bailed out after adding the
> folio to the swap cache but before writing it out, or when the swap
> write failed.
>
> Such folios add up, and they are expensive to free at exit: on a large
> production workload, the exit time doubled when zswap was enabled. A
> third of the increase comes from the folios left in the swap cache,
> each costing about 3x a resident page to tear down; the rest comes from
> freeing the zswap entries.
>
> So free the swap cache of a dirty folio as well: folio_free_swap() only
> releases the slot once no other page table references it. The only cost
> is a new slot allocation on the folio's next reclaim, which write faults
> already pay.
>
> On the production workload, this patch removed that third: the exit time
> decreased by about 15%. On a simple microbenchmark, 4 GiB of anonymous
> memory in a memcg pushed to swap, read back, then SIGKILLed in a QEMU
> guest (mean +- stddev of 9 runs):
>
> 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@ghiti.fr>
LGTM FWIW.
Reviewed-by: Nhat Pham <nphamcs@gmail.com>
^ permalink raw reply [flat|nested] 4+ messages in thread