mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] mm: free the swap slot of a dirty swapin folio
@ 2026-11-27 14:23 Alexandre Ghiti
  2026-11-29 10:27 ` David Hildenbrand (Arm)
  0 siblings, 1 reply; 2+ messages in thread
From: Alexandre Ghiti @ 2026-11-27 14:23 UTC (permalink / raw)
  To: Andrew Morton, linux-mm
  Cc: 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, Alexandre Ghiti

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>
---

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.
 	 */
+	if (folio_test_dirty(folio))
+		return true;
+
 	return (fault_flags & FAULT_FLAG_WRITE) && exclusive;
 }
 
-- 
2.53.1-Meta


^ permalink raw reply	[flat|nested] 2+ 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)
  0 siblings, 0 replies; 2+ 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] 2+ messages in thread

end of thread, other threads:[~2026-11-29 10:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®