Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path
@ 2026-12-01 17:57 Alexandre Ghiti
  2026-12-01 17:57 ` [PATCH v5 1/3] mm: swap: refault on swap-in, not in the swap cache allocator Alexandre Ghiti
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Alexandre Ghiti @ 2026-12-01 17:57 UTC (permalink / raw)
  To: Johannes Weiner, Yosry Ahmed, Nhat Pham, Chengming Zhou,
	Andrew Morton, David Hildenbrand, Lorenzo Stoakes,
	Liam R. Howlett, Vlastimil Babka, Mike Rapoport,
	Suren Baghdasaryan, Michal Hocko, Hugh Dickins, Baolin Wang,
	Chris Li, Kairui Song, Kemeng Shi, Baoquan He, Barry Song,
	Youngjun Park, Qi Zheng, Shakeel Butt, Axel Rasmussen,
	Yuanchu Xie, Wei Xu, Joonsoo Kim
  Cc: linux-mm, linux-kernel, Alexandre Ghiti

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.

Changes in v5:
- Patch 1 of v4 ("mm: swap: move LRU insertion out of the swap cache
  allocator") landed with the zswap writeback dropbehind series and is
  dropped.
- Rebased on mm-new, which has zswap writeback dropbehind: the writeback
  buffer is no longer put on the LRU, and it is freed when its writeback
  completes.
- Sashiko found that a refaulted writeback buffer was not activated with
  MGLRU. A swap-in now takes the buffer over from its writeback completion
  and adds it to the LRU itself, and the completion drains its LRU batch
  after putting a buffer back, so that it can be activated. The drain is
  rare: 2 out of 48.8M writebacks over 16 runs of MariaDB sysbench and
  kernel builds on NVMe swap.
- workingset_refault_lru_managed() is introduced in its own patch.
- zswap_folio_is_writeback_buffer() and zswap_lookup_and_clear_shadows()
  are renamed zswap_is_writeback_folio() and
  zswap_get_and_clear_writeback_shadow(), and the swap-in handling of a
  writeback buffer moves to zswap.c as zswap_swapin_writeback_folio().
- zswap_is_writeback_folio() now walks a folio's offsets with a
  single xarray walk rather than one lookup per page.
- zswap_invalidate() now leaves a parked shadow alone rather than handing
  it to zswap_entry_free(). A buffer whose write failed is redirtied and
  stays in the swap cache. If it is all zeroes, which happens for a zero
  page stored in zswap as part of a large folio, the next reclaim takes
  the zero-filled path of swap_writeout(), which invalidates the slot.

Changes in v4:
- Usama pointed out that patch 1's changelog referred to "the next patch"
  and to the "upcoming" dropbehind series, which does not work for a patch
  posted in two series. Both references are gone.
- Kunwu Chan pointed out that mm/swapfile.c still refers to the function
  by its old name in a comment; patch 1 renames it there too.
- Usama pointed out that reading the slot's shadow in the swap-in path
  before allocating races: the allocation can sleep, so another swap-in
  can install a folio, have it reclaimed and leave a newer shadow behind,
  and this caller would then win the insertion but refault against the
  stale snapshot. __swap_cache_alloc_folio() now hands the shadow back
  from __swap_cache_add_check(), which captures it under ci->lock at the
  point the displacing insertion succeeds. zswap writeback took the same
  racy snapshot and now uses the same output.

Changes in v3:
- David pointed out that the boolean added to workingset_refault() in v2
  makes the calling code hard to read. The boolean is now internal to
  mm/workingset.c and the callers use workingset_refault() or
  workingset_refault_lru_managed(), the same way remove_mapping() and
  remove_mapping_reclaim() do. The three existing callers are left
  untouched.
- zswap_writeback_entry() now restores the shadow into the slot on the
  error paths taken after the buffer was allocated: the allocation had
  already overwritten it and nothing was parked yet, so it was lost.
- Dropped the "if (!shadow) shadow = ZSWAP_WRITEBACK_NO_SHADOW" fallback,
  which can never be taken: the swap table already marks a swapped out
  slot with xa_mk_value(0), so swap_cache_get_shadow() never returns NULL
  for one.
- Rebased on mm-new.

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. Such a folio is now
  activated with folio_activate() instead. One 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, workingset_refault_lru_managed() has no
  caller left and can go away.

v1: https://lore.kernel.org/all/20260817144622.137133-1-alex@ghiti.fr/
v2: https://lore.kernel.org/linux-mm/20260821093606.2231216-1-alex@ghiti.fr/
v3: https://lore.kernel.org/linux-mm/20260825172604.3243589-1-alex@ghiti.fr/
v4: https://lore.kernel.org/linux-mm/20260911092012.92399-1-alex@ghiti.fr/

Alexandre Ghiti (3):
  mm: swap: refault on swap-in, not in the swap cache allocator
  mm: workingset: add workingset_refault_lru_managed()
  mm: zswap: preserve the workingset shadow across writeback

 include/linux/zswap.h |  17 +++++
 mm/internal.h         |   1 +
 mm/memory.c           |   2 +
 mm/shmem.c            |   2 +
 mm/swap.h             |   3 +-
 mm/swap_state.c       |  51 +++++++++++--
 mm/vmscan.c           |   4 +-
 mm/workingset.c       |  64 +++++++++++++---
 mm/zswap.c            | 168 ++++++++++++++++++++++++++++++++++++++++--
 9 files changed, 284 insertions(+), 28 deletions(-)


base-commit: fc6d9d5585a0d285bb857d11ef5ac1b972507d08
-- 
2.53.1-Meta



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2026-12-02 18:01 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 17:57 [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path Alexandre Ghiti
2026-12-01 17:57 ` [PATCH v5 1/3] mm: swap: refault on swap-in, not in the swap cache allocator Alexandre Ghiti
2026-12-01 17:57 ` [PATCH v5 2/3] mm: workingset: add workingset_refault_lru_managed() Alexandre Ghiti
2026-12-01 17:57 ` [PATCH v5 3/3] mm: zswap: preserve the workingset shadow across writeback Alexandre Ghiti
2026-12-02 18:01   ` sashiko-bot
2026-12-01 18:17 ` [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path Yosry Ahmed
2026-12-02  3:33   ` Nhat Pham
2026-12-02  4:33     ` Yosry Ahmed
2026-12-02  4:40       ` Nhat Pham
2026-12-02  4:59         ` Yosry Ahmed
2026-12-02  9:25           ` Alexandre Ghiti
2026-12-02  7:29   ` Chris Li
2026-12-02  9:49     ` Alexandre Ghiti

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox