Re: [PATCH mm-next] mm/swap: free swap cache folios the zap path gave up on

From: Andrew Morton

Date: Wed Oct 14 2026 - 19:02:52 EST


On Wed, 14 Oct 2026 17:08:19 +0800 Jiayuan Chen <jiayuan.chen@xxxxxxxxx> wrote:

> From: Jiayuan Chen <jiayuan.chen@xxxxxxxxxx>
>
> 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.

That's a pretty nasty problem - thanks for working on it.

> Options considered:
>
> 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.

It's a substantial patch. There's no quick-n-dirty fixup we can
prepare for backporting into older kernels?

> include/linux/swap.h | 1 +
> mm/swap.h | 1 +
> mm/swap_state.c | 9 ++--
> mm/swapfile.c | 101 ++++++++++++++++++++++++++++++++++++++++++-
> 4 files changed, 104 insertions(+), 8 deletions(-)

Sashiko had a few things to say:
https://sashiko.dev/#/patchset/20261014090821.227366-1-jiayuan.chen@xxxxxxxxx