[PATCH v2 0/3] mm: fix workingset refaults in the zswap writeback path

From: Alexandre Ghiti

Date: Fri Aug 21 2026 - 05:53:35 EST


When an anonymous folio is reclaimed, workingset_eviction() stores a
"shadow" (the eviction cookie) in the swap slot so that a later swap-in
can be recognised as a refault and, if the refault distance is short
enough, the page can be re-activated. This is how anon workingset/refault
detection has worked since commit aae466b0052e ("mm/swap: implement
workingset detection for anonymous LRU").

zswap writeback breaks this in two independent ways:

- Over-count at writeback: the shrinker allocates a buffer folio in the
swap cache, and the allocation path counts that folio as a refault.

- Lost eviction cookie at reclaim: adding the buffer to the swap cache
overwrites the slot's shadow, so the original cookie is lost; when the
buffer folio is finally reclaimed a fresh, inaccurate cookie is minted
in its place.

This series preserves the shadow within zswap itself, without any new
swap-table or swap-slot state. At writeback, instead of erasing the freed
zswap entry, the captured shadow is parked in the zswap tree in its place,
so it outlives the writeback buffer folio.

Finally, the refault evaluation is moved out of the swap-cache allocator
into the swap-in callers, so allocating the writeback buffer is no longer
miscounted as a refault.

Results
-------

Measured with a sysbench OLTP (MariaDB) workload in a memory cgroup sized
so the dataset and InnoDB buffer pool both overcommit it, with the zswap
shrinker on so entries are continuously written back to an NVMe swap
device (classic LRU; MGLRU off). Anon workingset counters over the
measured window, baseline vs this series, mean +/- stddev over 10 runs:

workingset_refault_anon 383,242 +/- 59,345 -> 199,355 +/- 27,042 -48%
workingset_activate_anon 54,890 +/- 11,742 -> 22,607 +/- 3,118 -59%
workingset_restore_anon 16,212 +/- 4,590 -> 7,599 +/- 1,190 -53%

Writeback volume is comparable (zswpwb 183k +/- 16k -> 178k +/- 15k), so
the reduction is not from doing less work. Normalised per transaction the
reduction holds (-53%/-49%/-39%) while the swap work per transaction is
unchanged. A kernel build under the same pressure moves all three
counters in the same direction.

The run-to-run variance of these counters drops as well.

Throughput is unaffected: over the same 10 runs, transactions/s is
18.50 +/- 1.08 -> 18.65 +/- 0.94, i.e. +0.8% with a 95% confidence
interval of +/- 7.7%.

Changes in v2:
- Sashiko pointed out that v1 evaluated the refault after folio_add_lru(),
which picks the MGLRU generation before PG_workingset is set. Patch 1
now moves the LRU insertion out of the swap cache allocator so the
refault is evaluated before it, as it was originally.
- Sashiko also pointed out that a failed writeback redirties the buffer
and leaves it in the swap cache with its shadow still parked, so a
later zswap_store() on it would hand the parked value to
zswap_entry_free(). zswap_store() now bails out for such a folio:
writeback already decided that data belongs on disk, so it is written
there instead of being compressed again, which also keeps the parked
shadow intact until the folio leaves the swap cache.
- The buffer folio a swap-in consumes is already on the LRU, so the
refault there cannot activate it by setting PG_active: that leaves the
flag disagreeing with the list the folio is on, which shows up as an
mm/memcontrol.c lru_size underflow when it is freed. workingset_refault()
now takes whether the caller has already put the folio on the LRU and
uses folio_activate() in that case. A folio still sitting in a per-CPU
batch cannot be moved safely and just misses the activation; a counter
on that path measured 0.0007% of the activations over a 10 run test.
This is only needed because zswap writeback still puts its buffer on the
LRU: once it stops doing so, every caller passes false and the argument
can go away.
- Patch 1 is shared with the zswap dropbehind series; whichever lands
first, the other should drop it.

v1: https://lore.kernel.org/all/20260817144622.137133-1-alex@xxxxxxxx/

Alexandre Ghiti (3):
mm/swap: move LRU insertion out of the swap cache allocator
mm/swap: refault on swap-in, not in the swap cache allocator
mm/zswap: preserve the workingset shadow across writeback

include/linux/swap.h | 2 +-
include/linux/zswap.h | 12 ++++++
mm/filemap.c | 2 +-
mm/memory.c | 5 +++
mm/shmem.c | 5 +++
mm/swap.h | 6 +--
mm/swap_state.c | 50 +++++++++++++++++-----
mm/vmscan.c | 4 +-
mm/workingset.c | 16 ++++++--
mm/zswap.c | 96 +++++++++++++++++++++++++++++++++++++++++--
10 files changed, 176 insertions(+), 22 deletions(-)


base-commit: 626acb37cd445144f321f1b64cac9a93760fa716
--
2.53.0-Meta