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

From: Alexandre Ghiti

Date: Fri Nov 27 2026 - 04:40:17 EST


Hi Nhat,

On Thu, Nov 26, 2026 at 7:15 PM Nhat Pham <nphamcs@xxxxxxxxx> wrote:
>
> >
> On Thu, Nov 26, 2026 at 2:43 AM Alexandre Ghiti <alex@xxxxxxxx> 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%)
>

First, thanks for folio_free_swap() comment from your other message :)

> I think this worst-case throughput drop fine.
>
> With SSD swap, I think the number is even going to be less
> significant, and furthermore it will be much rarer to hit this case.
> When SSD swap IO completes, it's going to be in clean state. I guess
> you need to have a reader race with a writer who wins the race and
> dirties that page before the reader?
>

The race you describe would not produce a swap slot backed by a dirty
folio since the "winning" write would delete the swap slot.

I (and my nice AI companion) think the only way (apart from zswap) to
reproduce this is if a swap write fails or reclaim bails out, which
should be very rare.

Thanks,

Alex