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

From: Nhat Pham

Date: Mon Nov 30 2026 - 14:11:36 EST


On Fri, Nov 27, 2026 at 7:24 AM Alexandre Ghiti <alex@xxxxxxxx> 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@xxxxxxxx>

LGTM FWIW.

Reviewed-by: Nhat Pham <nphamcs@xxxxxxxxx>