Re: [PATCH mm-next] mm/swap: free swap cache folios the zap path gave up on
From: Jiayuan Chen
Date: Wed Oct 14 2026 - 23:23:09 EST
On 10/15/26 7:02 AM, Andrew Morton wrote:
On Wed, 14 Oct 2026 17:08:19 +0800 Jiayuan Chen <jiayuan.chen@xxxxxxxxx> wrote:
From: Jiayuan Chen <jiayuan.chen@xxxxxxxxxx>That's a pretty nasty problem - thanks for working on it.
On our production hosts we saw swap cache only pages keep growing for days:
on a box with a 500 GiB swap partition, 250 GiB of RAM and 250 GiB of
swap slots were held by folios that no process mapped any more, charged
to cgroups whose workloads had long exited. The only way to get them
back was swapoff, and with other services actively swapping on the
same host that is not an option.
Such a slot has swap count 0 and an unmapped folio pinned in the swap
cache, left there after a process exited or did MADV_DONTNEED. No page
table holds its swap entry any more, so nothing comes back for the
slot; LRU reclaim drops it eventually, and the allocator only reclaims
such slots once swap is half full. The growth stopped exactly there:
at 50% of the device, where vm_swap_full() becomes true.
Options considered:It's a substantial patch. There's no quick-n-dirty fixup we can
1. Fix it on the readahead side: after the read, check the swap count
and free the slot. Covers the lock, not writeback, and the
asynchronous read path unlocks in the bio completion where the slot
cannot be freed. Two places to patch, neither clean.
2. swapoff/swapon clears it, as does memory.reclaim on the right cgroup.
That is a workaround, not a fix.
3. Have the side that gives up come back later. This patch.
The zap runs under the PTL and the fault path takes the folio lock
before the PTL, so waiting there is out; a worker has to do it. On a
lost trylock or a writeback refusal under TTRS_UNMAPPED,
__try_to_reclaim_swap() schedules si->sweep_work. The worker walks the
in-use clusters and, for every slot with swap count 0 whose folio is
unmapped, calls __try_to_reclaim_swap() with TTRS_WAIT, which takes the
folio lock and waits for writeback instead of giving up.
free_swap_cache() now calls __try_to_reclaim_swap() as well, so both
paths give up, and wake the worker, in the same place.
For now the worker scans every in-use cluster of the device; marking
the clusters that need it can reduce that later.
prepare for backporting into older kernels?
I couldn't find a small one. I tried the other options listed in the
changelog, and each was either incomplete or spread over many places.
The zap can only trylock, and the folio can be held by many things at
that moment: readahead (sync and async reads), writeback, zswap
writeback, reclaim, migration, folio split and so on. Fixing it on that
side means patching each of them, and missing one leaves the leak. This
patch fixes it in the one place they all have in common, the zap that
gives up.
As for older kernels, I found this on 6.6 and I'm happy to do the
backports.
include/linux/swap.h | 1 +Sashiko had a few things to say:
mm/swap.h | 1 +
mm/swap_state.c | 9 ++--
mm/swapfile.c | 101 ++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 104 insertions(+), 8 deletions(-)
https://sashiko.dev/#/patchset/20261014090821.227366-1-jiayuan.chen@xxxxxxxxx
Thanks. The large folio split and the offset step are real and come
from TTRS_WAIT.
The swap_cache_get_folio() race is pre-existing, I could reproduce it and will send a separate fix.