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

From: Kairui Song

Date: Thu Dec 03 2026 - 11:32:21 EST


On Wed, Dec 2, 2026 at 1:58 AM Alexandre Ghiti <alex@xxxxxxxx> wrote:
>
> 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 each run,
> baseline vs this series, mean +/- stddev over 6 and 8 runs (runs where
> mysqld aborted, which happened on both kernels, are left out):
>
> workingset_refault_anon 17.93M +/- 2.17M -> 12.70M +/- 1.08M -29%
> workingset_activate_anon 3.52M +/- 0.45M -> 1.03M +/- 0.09M -71%
> workingset_restore_anon 721k +/- 126k -> 153k +/- 18k -79%
>
> Normalised per transaction, the reduction is -34%/-73%/-80%, while the
> swap work per transaction is unchanged (zswpwb -1%, pswpin -1%), so the
> reduction is not from doing less work. The run-to-run variance of these
> counters drops as well.
>
> Throughput is unaffected: transactions/s is 806 +/- 122 -> 861 +/- 94,
> within the run-to-run variance.
>

Hello Alexandre,

Sorry I joined the discussion late again :P. was busy with a few other
things. But as I took a look, this one is really not just a zswap problem.

Swap readhead also suffers similiar problem and about 3 years ago I
tried to implemeted something like hold the shadow in page->index
until the folio is added back to the VMA (actual fault) and it worked
internally. Because when the folio is still in swap cache and being
mappingless, it's folio->index is compeletly idle. And given the fact
that folio->index is suppose to mean the offset in the mapping, when
the folio is in swap cache mapping, it's offset is swap entry which is
in folio->private already, folio->index then can be used for something
else. IIRC that's not hard to implement, but it might be a bit ugly
unless we find good definitions for helpers and structs and need some
smart tricks to handle a few corner cases. We moved away from that
during the rebase because the gain was trivial for ZRAM, and since we
mostly use ZRAM, we tried pushing that upstream, but it's very
outdated. But will something like that work better? And maybe we can
work on a similar approach? So we can reuse it for both ZSWAP / common
SWAP readahead.

Oh and, we need to carefully handle the usage of folio_add_lru(),
refault evluation must be done before that. For readahead and folio to
be actually installed later, we need folio_mark_accessex(). (This is
somehow related to my recnent work with MGLRU-FG, but for now let's
just use folio_mark_access, later we can use a flag to tell LRU that
it's a mapped fault and not a common FD fault, LRU_REF_MAPPED there).