Re: [PATCH] mm: free the swap slot of a dirty swapin folio
From: Alexandre Ghiti
Date: Thu Nov 26 2026 - 10:02:05 EST
Hi David,
On Thu, Nov 26, 2026 at 1:54 PM David Hildenbrand (Arm)
<david@xxxxxxxxxx> wrote:
>
> >
> On 11/26/26 10:42, Alexandre Ghiti wrote:
> > do_swap_page() frees the swap cache of an exclusive folio on a write
> > fault only. A folio loaded from zswap is dirty: zswap drops its copy on
> > load, so the slot no longer holds the data and only reserves swap space
> > until the folio is reclaimed again or its owner exits.
> >
> > 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 an exclusive dirty folio as well, so that the
> > slot is released at swapin, as is already done on an exclusive write
> > fault. The folio is dirty, so it would get written out again anyway:
> > the only cost is a new slot allocation on its 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 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>
> > ---
> >
> > Notes:
> > Previous discussion:
> > https://lore.kernel.org/all/CAGsJ_4xBtKGqXFP4zvp+Qn+tb4X7aCvXn+wqDqot_cXR92DjhQ@xxxxxxxxxxxxxx/
> >
> > mm/memory.c | 9 ++++++---
> > 1 file changed, 6 insertions(+), 3 deletions(-)
> >
> > diff --git a/mm/memory.c b/mm/memory.c
> > index d477a4f986bc..dac0f0a24bc7 100644
> > --- a/mm/memory.c
> > +++ b/mm/memory.c
> > @@ -4711,10 +4711,13 @@ 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 only if we are the exclusive user and this is a
> > + * write fault, or the folio is dirty: its copy in the swap slot is
> > + * stale then (zswap loads are exclusive and dirty the folio), so the
> > + * slot only holds swap space.
> > */
> > - return (fault_flags & FAULT_FLAG_WRITE) && exclusive;
> > + return exclusive && ((fault_flags & FAULT_FLAG_WRITE) ||
> > + folio_test_dirty(folio));
>
> Why not more readable as
>
> if (!exclusive)
> return false;
> return (fault_flags & FAULT_FLAG_WRITE) || folio_test_dirty(folio);
Agree, I'll change that for v2.
>
> But ... isn't this zswap specific? Why do we have zswap specific stuff in this
> common code?
>
> The comment already gives away that something is not quite right here.
I am mostly interested in the zswap benefits, but nothing is zswap
specific here: do_swap_page() can also find a dirty folio in the swap
cache when reclaim bailed out before writeout, or when the swap write
failed. In all those cases, the copy in the slot is stale, so the slot
only reserves swap space. zswap is simply the common case, since
zswap_load() dirties the folio.
What do you think?
Thanks,
Alex
>
> --
> Cheers,
>
> David
>