* [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
* [PATCH v5 1/3] mm: swap: refault on swap-in, not in the swap cache allocator
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 ` Alexandre Ghiti
2026-12-01 17:57 ` [PATCH v5 2/3] mm: workingset: add workingset_refault_lru_managed() Alexandre Ghiti
` (2 subsequent siblings)
3 siblings, 0 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
The swap cache allocator evaluates the refault of every folio it
allocates. zswap writeback also allocates through it: the shrinker puts
a buffer folio in the swap cache to write the compressed data out, and
that allocation is then counted as an anon refault (and, if the eviction
looks recent, as an activation) even though nothing faulted the page
back in. On a workload that writes back continuously this inflates
workingset_refault_anon and workingset_activate_anon substantially.
Move the refault evaluation out of the allocator and into the two
swap-in callers. zswap writeback keeps allocating the buffer, but no
longer reports a refault for it.
The callers cannot read the shadow themselves before allocating: the
allocation can sleep, so another swap-in may 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. Hand the
shadow back from __swap_cache_alloc_folio() instead, which already
captures it under ci->lock in __swap_cache_add_check(), at the point the
insertion that displaces it succeeds.
The refault is evaluated before folio_add_lru(), as it was before this
patch, so workingset_refault() still sets PG_workingset/PG_active while
the folio is off the LRU: folio_add_lru() consumes both when it picks
the LRU list, and under MGLRU when it picks the generation.
Fixes: aae466b0052e ("mm/swap: implement workingset detection for anonymous LRU")
Co-developed-by: Nhat Pham <nphamcs@gmail.com>
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
Signed-off-by: Alexandre Ghiti <alex@ghiti.fr>
---
mm/swap.h | 3 ++-
mm/swap_state.c | 28 +++++++++++++++++++++-------
mm/zswap.c | 2 +-
3 files changed, 24 insertions(+), 9 deletions(-)
diff --git a/mm/swap.h b/mm/swap.h
index d488a69c2809..375a1d46323a 100644
--- a/mm/swap.h
+++ b/mm/swap.h
@@ -315,7 +315,8 @@ void *swap_cache_get_shadow(swp_entry_t entry);
void swap_cache_del_folio(struct folio *folio);
struct folio *__swap_cache_alloc_folio(swp_entry_t target_entry, gfp_t gfp_mask,
unsigned long orders, struct vm_fault *vmf,
- struct mempolicy *mpol, pgoff_t ilx);
+ struct mempolicy *mpol, pgoff_t ilx,
+ void **shadowp);
/* Below helpers require the caller to lock and pass in the swap cluster. */
void __swap_cache_add_folio(struct swap_cluster_info *ci,
struct folio *folio, swp_entry_t entry);
diff --git a/mm/swap_state.c b/mm/swap_state.c
index da65d75ceead..32d24df42700 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -432,7 +432,8 @@ void __swap_cache_replace_folio(struct swap_cluster_info *ci,
static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
swp_entry_t targ_entry, gfp_t gfp,
unsigned int order, struct vm_fault *vmf,
- struct mempolicy *mpol, pgoff_t ilx)
+ struct mempolicy *mpol, pgoff_t ilx,
+ void **shadowp)
{
int err;
swp_entry_t entry;
@@ -506,12 +507,13 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
/* memsw uncharges swap when folio is added to swap cache */
memcg1_swapin(folio);
- if (shadow)
- workingset_refault(folio, shadow);
node_stat_mod_folio(folio, NR_FILE_PAGES, nr_pages);
lruvec_stat_mod_folio(folio, NR_SWAPCACHE, nr_pages);
+ if (shadowp)
+ *shadowp = shadow;
+
return folio;
}
@@ -523,6 +525,7 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
* @vmf: fault information
* @mpol: NUMA memory allocation policy to be applied
* @ilx: NUMA interleave index, for use only when MPOL_INTERLEAVE
+ * @shadowp: Returns the shadow the allocation displaced, NULL to ignore
*
* Allocate a folio in the swap cache for one swap slot, typically before
* doing IO (e.g. swap in or zswap writeback). The swap slot indicated by
@@ -538,7 +541,8 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,
*/
struct folio *__swap_cache_alloc_folio(swp_entry_t targ_entry, gfp_t gfp,
unsigned long orders, struct vm_fault *vmf,
- struct mempolicy *mpol, pgoff_t ilx)
+ struct mempolicy *mpol, pgoff_t ilx,
+ void **shadowp)
{
int order, err;
struct folio *ret;
@@ -553,7 +557,7 @@ struct folio *__swap_cache_alloc_folio(swp_entry_t targ_entry, gfp_t gfp,
do {
ret = __swap_cache_alloc(ci, targ_entry, gfp, order,
- vmf, mpol, ilx);
+ vmf, mpol, ilx, shadowp);
if (!IS_ERR(ret))
break;
err = PTR_ERR(ret);
@@ -711,17 +715,22 @@ static struct folio *swap_cache_read_folio(struct swap_io_ctx *ctx,
pgoff_t ilx, bool readahead)
{
struct folio *folio;
+ void *shadow = NULL;
do {
folio = swap_cache_get_folio(entry);
if (folio)
return folio;
- folio = __swap_cache_alloc_folio(entry, gfp, BIT(0), NULL, mpol, ilx);
+ folio = __swap_cache_alloc_folio(entry, gfp, BIT(0), NULL, mpol,
+ ilx, &shadow);
} while (PTR_ERR(folio) == -EEXIST);
if (IS_ERR_OR_NULL(folio))
return NULL;
+ if (shadow)
+ workingset_refault(folio, shadow);
+
folio_add_lru(folio);
swap_read_folio(ctx, folio);
if (readahead) {
@@ -753,17 +762,22 @@ struct folio *swapin_sync(swp_entry_t entry, gfp_t gfp, unsigned long orders,
{
struct swap_io_ctx ctx = {};
struct folio *folio;
+ void *shadow = NULL;
do {
folio = swap_cache_get_folio(entry);
if (folio)
return folio;
- folio = __swap_cache_alloc_folio(entry, gfp, orders, vmf, mpol, ilx);
+ folio = __swap_cache_alloc_folio(entry, gfp, orders, vmf, mpol,
+ ilx, &shadow);
} while (PTR_ERR(folio) == -EEXIST);
if (IS_ERR(folio))
return folio;
+ if (shadow)
+ workingset_refault(folio, shadow);
+
folio_add_lru(folio);
swap_read_folio(&ctx, folio);
swap_read_submit(&ctx);
diff --git a/mm/zswap.c b/mm/zswap.c
index a2e0314db1cf..4f0a0afe534d 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1095,7 +1095,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
mpol = get_task_policy(current);
folio = __swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol,
- NO_INTERLEAVE_INDEX);
+ NO_INTERLEAVE_INDEX, NULL);
put_swap_device(si);
/*
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v5 2/3] mm: workingset: add workingset_refault_lru_managed()
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 ` Alexandre Ghiti
2026-12-01 17:57 ` [PATCH v5 3/3] mm: zswap: preserve the workingset shadow across writeback Alexandre Ghiti
2026-12-01 18:17 ` [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path Yosry Ahmed
3 siblings, 0 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
workingset_refault() evaluates the refault of a folio that is not on the
LRU yet. On the classic LRU, it activates it by setting PG_active, which
the folio_add_lru() that follows honours. With MGLRU, lru_gen_refault()
only marks a workingset folio, and relies on folio_add_lru() being called
in the fault to activate it.
Neither works for a folio that is already on the LRU, such as a zswap
writeback buffer that its writeback completion put back on the LRU before
a swap-in found it: setting PG_active would leave the flag disagreeing
with the list the folio is on, and no folio_add_lru() follows.
Add workingset_refault_lru_managed() for such a folio. It activates the
folio with folio_activate() instead, which moves it to the active list,
or to the youngest generation with MGLRU, under the same conditions as
workingset_refault() and folio_add_lru(). folio_activate() cannot move a
folio still in a per-CPU LRU batch, where a buffer put back by its
writeback completion nearly always is when the swap-in finds it, so the
completion now drains its batch after putting a buffer back.
Fixes: aae466b0052e ("mm/swap: implement workingset detection for anonymous LRU")
Co-developed-by: Nhat Pham <nphamcs@gmail.com>
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
Signed-off-by: Alexandre Ghiti <alex@ghiti.fr>
---
mm/internal.h | 1 +
mm/swap_state.c | 7 ++++++
mm/workingset.c | 64 ++++++++++++++++++++++++++++++++++++++++---------
3 files changed, 61 insertions(+), 11 deletions(-)
diff --git a/mm/internal.h b/mm/internal.h
index 334f6ae0bdb5..9858f0118a16 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -35,6 +35,7 @@ void workingset_age_nonresident(struct lruvec *lruvec, unsigned long nr_pages);
void *workingset_eviction(struct folio *folio,
struct mem_cgroup *target_memcg);
void workingset_refault(struct folio *folio, void *shadow);
+void workingset_refault_lru_managed(struct folio *folio, void *shadow);
void workingset_activation(struct folio *folio);
/* mm/folio.c */
diff --git a/mm/swap_state.c b/mm/swap_state.c
index 32d24df42700..897e3b503487 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -604,6 +604,13 @@ void swap_writeback_dropbehind_folio(struct folio *folio)
/* Raced: the folio is now owned by the swapin; put it back. */
folio_clear_dropbehind(folio);
folio_add_lru(folio);
+ /*
+ * The extra reference is usually a swapin waiting for the folio
+ * lock, which then activates the folio if its refault is
+ * recent. It can only move a folio on the LRU, not one still
+ * in this CPU's batch: drain it.
+ */
+ lru_add_drain();
}
mem_cgroup_put(memcg);
diff --git a/mm/workingset.c b/mm/workingset.c
index 1504f91cdca5..2d3dcefe9805 100644
--- a/mm/workingset.c
+++ b/mm/workingset.c
@@ -536,16 +536,8 @@ bool workingset_test_recent(void *shadow, bool file, bool *workingset,
return refault_distance <= workingset_size;
}
-/**
- * workingset_refault - Evaluate the refault of a previously evicted folio.
- * @folio: The freshly allocated replacement folio.
- * @shadow: Shadow entry of the evicted folio.
- *
- * Calculates and evaluates the refault distance of the previously
- * evicted folio in the context of the node and the memcg whose memory
- * pressure caused the eviction.
- */
-void workingset_refault(struct folio *folio, void *shadow)
+static void __workingset_refault(struct folio *folio, void *shadow,
+ bool lru_managed)
{
bool file = folio_is_file_lru(folio);
struct mem_cgroup *memcg;
@@ -557,6 +549,14 @@ void workingset_refault(struct folio *folio, void *shadow)
if (lru_gen_enabled()) {
lru_gen_refault(folio, shadow);
+ /*
+ * folio_add_lru() activates a workingset folio added in the
+ * fault: one already added has to be moved instead.
+ */
+ if (lru_managed && lru_gen_in_fault() &&
+ !(current->flags & PF_MEMALLOC) &&
+ folio_test_workingset(folio))
+ folio_activate(folio);
return;
}
@@ -577,7 +577,14 @@ void workingset_refault(struct folio *folio, void *shadow)
if (!workingset_test_recent(shadow, file, &workingset, true))
goto out;
- folio_set_active(folio);
+ /*
+ * Setting PG_active on a folio already on the LRU would leave the flag
+ * disagreeing with the list it is on: move it instead.
+ */
+ if (lru_managed)
+ folio_activate(folio);
+ else
+ folio_set_active(folio);
workingset_age_nonresident(lruvec, nr);
mod_lruvec_state(lruvec, WORKINGSET_ACTIVATE_BASE + file, nr);
@@ -590,6 +597,41 @@ void workingset_refault(struct folio *folio, void *shadow)
mem_cgroup_put(memcg);
}
+/**
+ * workingset_refault - Evaluate the refault of a previously evicted folio.
+ * @folio: The freshly allocated replacement folio.
+ * @shadow: Shadow entry of the evicted folio.
+ *
+ * Calculates and evaluates the refault distance of the previously
+ * evicted folio in the context of the node and the memcg whose memory
+ * pressure caused the eviction.
+ *
+ * Context: @folio must be locked and not on the LRU yet, the caller adds
+ * it with folio_add_lru() afterwards.
+ */
+void workingset_refault(struct folio *folio, void *shadow)
+{
+ __workingset_refault(folio, shadow, false);
+}
+
+/**
+ * workingset_refault_lru_managed - Evaluate the refault of a previously
+ * evicted folio that is already on the LRU.
+ * @folio: The folio the eviction is refaulted into.
+ * @shadow: Shadow entry of the evicted folio.
+ *
+ * Like workingset_refault(), but for a folio the caller has already added
+ * to the LRU, which has to be activated by moving it, to the active list or
+ * to the youngest MGLRU generation, rather than by setting PG_active.
+ *
+ * Context: @folio must be locked and on the LRU: folio_activate() cannot
+ * move a folio still in a per-CPU LRU batch.
+ */
+void workingset_refault_lru_managed(struct folio *folio, void *shadow)
+{
+ __workingset_refault(folio, shadow, true);
+}
+
/**
* workingset_activation - note a page activation
* @folio: Folio that is being activated.
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH v5 3/3] mm: zswap: preserve the workingset shadow across writeback
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 ` 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
3 siblings, 1 reply; 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 zswap writes an entry back, allocating the buffer folio in the swap
cache overwrites the swap slot's workingset shadow. It is lost and later
re-minted inaccurately, which corrupts anon refault/workingset accounting
for the written-back data.
Preserve it within zswap itself: at writeback, park the shadow in the
zswap tree in place of the freed entry, so it outlives the writeback
buffer folio. The buffer then meets one of three fates, and the parked
shadow is handled at whichever happens first:
- swap-in: do_swap_page()/shmem_swapin_folio() find the buffer in the swap
cache, take the shadow and account the refault of the now-resident
folio, then drop it.
- removal: the writeback completion frees the buffer, or reclaim does
if the completion put it back on the LRU. __remove_mapping() skips
minting a fresh shadow for it (minting would double-count the eviction)
and __swap_cache_do_del_folio() restores the parked shadow into the
slot, so a later disk swap-in still refaults against the original
eviction.
- slot free (e.g. process exit): the shadow is not needed and is cleared.
The last two both go through __swap_cache_do_del_folio(), the single point
every swap-cache removal passes through, which is why the shadow is read
and cleared there.
Fixes: aae466b0052e ("mm/swap: implement workingset detection for anonymous LRU")
Co-developed-by: Nhat Pham <nphamcs@gmail.com>
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
Signed-off-by: Alexandre Ghiti <alex@ghiti.fr>
---
include/linux/zswap.h | 17 +++++
mm/memory.c | 2 +
mm/shmem.c | 2 +
mm/swap_state.c | 16 +++-
mm/vmscan.c | 4 +-
mm/zswap.c | 168 ++++++++++++++++++++++++++++++++++++++++--
6 files changed, 200 insertions(+), 9 deletions(-)
diff --git a/include/linux/zswap.h b/include/linux/zswap.h
index 94746fb71bb6..b877eae6edf5 100644
--- a/include/linux/zswap.h
+++ b/include/linux/zswap.h
@@ -36,6 +36,9 @@ void zswap_folio_swapin(struct folio *folio);
bool zswap_is_enabled(void);
bool zswap_never_enabled(void);
bool zswap_is_present(swp_entry_t entry, unsigned int nr);
+bool zswap_is_writeback_folio(struct folio *folio);
+void *zswap_get_and_clear_writeback_shadow(struct folio *folio);
+void zswap_swapin_writeback_folio(struct folio *folio);
#else
struct zswap_lruvec_state {};
@@ -79,6 +82,20 @@ static inline bool zswap_is_present(swp_entry_t entry, unsigned int nr)
return false;
}
+static inline bool zswap_is_writeback_folio(struct folio *folio)
+{
+ return false;
+}
+
+static inline void *zswap_get_and_clear_writeback_shadow(struct folio *folio)
+{
+ return NULL;
+}
+
+static inline void zswap_swapin_writeback_folio(struct folio *folio)
+{
+}
+
#endif
#endif /* _LINUX_ZSWAP_H */
diff --git a/mm/memory.c b/mm/memory.c
index d477a4f986bc..6fe6ab2ad315 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5104,6 +5104,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
goto out_page;
}
+ zswap_swapin_writeback_folio(folio);
+
/*
* KSM sometimes has to copy on read faults, for example, if
* folio->index of non-ksm folios would be nonlinear inside the
diff --git a/mm/shmem.c b/mm/shmem.c
index 4686875a3add..fdce6c4fe8f8 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2632,6 +2632,8 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
*/
arch_swap_restore(folio_swap(swap, folio), folio);
+ zswap_swapin_writeback_folio(folio);
+
if (shmem_should_replace_folio(folio, gfp)) {
error = shmem_replace_folio(&folio, gfp, info, index, vma);
if (error)
diff --git a/mm/swap_state.c b/mm/swap_state.c
index 897e3b503487..83dbc55e7db6 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -24,6 +24,7 @@
#include <linux/shmem_fs.h>
#include <linux/sysctl.h>
#include <linux/swap_ops.h>
+#include <linux/zswap.h>
#include "internal.h"
#include "swap_table.h"
#include "swap.h"
@@ -278,12 +279,21 @@ static void __swap_cache_do_del_folio(struct swap_cluster_info *ci,
unsigned int ci_start, ci_off, ci_end;
bool folio_swapped = false, need_free = false;
unsigned long nr_pages = folio_nr_pages(folio);
+ void *shadow_parked;
VM_WARN_ON_ONCE(__swap_entry_to_cluster(entry) != ci);
VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio);
VM_WARN_ON_ONCE_FOLIO(folio_test_writeback(folio), folio);
+ /*
+ * A zswap writeback buffer parked the slot's original shadow in the
+ * zswap tree: restore it into the slot for later swap-in.
+ */
+ shadow_parked = zswap_get_and_clear_writeback_shadow(folio);
+ if (shadow_parked)
+ shadow = shadow_parked;
+
si = __swap_entry_to_info(entry);
ci_start = swp_cluster_offset(entry);
ci_end = ci_start + nr_pages;
@@ -583,6 +593,10 @@ void swap_writeback_dropbehind_folio(struct folio *folio)
folio_lock(folio);
+ /* A swapin took the folio over and added it to the LRU itself. */
+ if (!folio_test_dropbehind(folio))
+ goto out;
+
/* The folio was allocated off the LRU and nothing re-adds it here. */
VM_WARN_ON_ONCE_FOLIO(folio_test_lru(folio), folio);
@@ -614,7 +628,7 @@ void swap_writeback_dropbehind_folio(struct folio *folio)
}
mem_cgroup_put(memcg);
-
+out:
folio_unlock(folio);
folio_put(folio);
}
diff --git a/mm/vmscan.c b/mm/vmscan.c
index fe1fe1439c7c..b128f1255866 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -48,6 +48,7 @@
#include <linux/prefetch.h>
#include <linux/printk.h>
#include <linux/dax.h>
+#include <linux/zswap.h>
#include <linux/psi.h>
#include <linux/pagewalk.h>
#include <linux/shmem_fs.h>
@@ -692,7 +693,8 @@ static int __remove_mapping(struct address_space *mapping, struct folio *folio,
if (folio_test_swapcache(folio)) {
swp_entry_t swap = folio->swap;
- if (reclaimed && !mapping_exiting(mapping))
+ if (reclaimed && !mapping_exiting(mapping) &&
+ !zswap_is_writeback_folio(folio))
shadow = workingset_eviction(folio, target_memcg);
__swap_cache_del_folio(ci, folio, swap, shadow, true);
swap_cluster_unlock_irq(ci);
diff --git a/mm/zswap.c b/mm/zswap.c
index 4f0a0afe534d..4096abafaf2a 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1062,6 +1062,122 @@ struct zswap_shrink_ctl {
bool *encountered_page_in_swapcache;
};
+#define ZSWAP_WRITEBACK_NO_SHADOW xa_mk_value(0)
+
+/**
+ * zswap_is_writeback_folio - is @folio a zswap writeback buffer?
+ * @folio: the folio being examined (typically a swap cache folio under reclaim)
+ *
+ * A folio is a zswap writeback buffer when every one of its swap offsets holds
+ * a parked writeback shadow (a real shadow or the ZSWAP_WRITEBACK_NO_SHADOW
+ * sentinel) in the zswap tree rather than a live zswap entry.
+ *
+ * Context: @folio must be locked, which keeps the answer stable (see
+ * zswap_get_and_clear_writeback_shadow()).
+ *
+ * Return: true if @folio is a writeback buffer, in which case the reclaim path
+ * must not mint a fresh workingset shadow for it.
+ */
+bool zswap_is_writeback_folio(struct folio *folio)
+{
+ unsigned long nr_pages = folio_nr_pages(folio);
+ pgoff_t offset = swp_offset(folio->swap);
+ /*
+ * A folio is naturally aligned in swap space and smaller than a zswap
+ * tree, so all its offsets live in the same tree.
+ */
+ XA_STATE(xas, swap_zswap_tree(folio->swap), offset);
+ unsigned long nr_parked = 0;
+ void *entry;
+
+ if (zswap_never_enabled())
+ return false;
+
+ rcu_read_lock();
+ xas_for_each(&xas, entry, offset + nr_pages - 1) {
+ if (xas_retry(&xas, entry))
+ continue;
+ if (!xa_is_value(entry))
+ break;
+ nr_parked++;
+ }
+ rcu_read_unlock();
+
+ return nr_parked == nr_pages;
+}
+
+/**
+ * zswap_get_and_clear_writeback_shadow - get and clear @folio's parked shadows
+ * @folio: the writeback buffer folio (or the swapin folio that consumed it)
+ *
+ * Remove any parked writeback shadows for @folio's swap offset(s) from the
+ * zswap tree.
+ *
+ * Context: @folio must be locked. Shadows are only parked and cleared under
+ * the lock of the swap cache folio occupying their slots, which is what makes
+ * the lockless lookup and the separate erase safe.
+ *
+ * Return: the preserved workingset shadow, or NULL if the slot(s) had no shadow
+ * (sentinel only) or nothing parked. The caller either restores the returned
+ * shadow into the swap slot (buffer dropped) or feeds it to the refault code
+ * (buffer consumed by a swapin); clearing here ensures the two paths never
+ * double-count.
+ */
+void *zswap_get_and_clear_writeback_shadow(struct folio *folio)
+{
+ swp_entry_t swp = folio->swap;
+ unsigned long nr_pages = folio_nr_pages(folio);
+ pgoff_t offset = swp_offset(swp);
+ void *shadow = NULL;
+ unsigned long i;
+
+ if (zswap_never_enabled())
+ return NULL;
+
+ for (i = 0; i < nr_pages; i++) {
+ swp_entry_t e = swp_entry(swp_type(swp), offset + i);
+ struct xarray *tree = swap_zswap_tree(e);
+ void *parked = xa_load(tree, offset + i);
+
+ if (!xa_is_value(parked))
+ continue;
+ xa_erase(tree, offset + i);
+ if (parked != ZSWAP_WRITEBACK_NO_SHADOW)
+ shadow = parked;
+ }
+
+ return shadow;
+}
+
+/**
+ * zswap_swapin_writeback_folio - handle a zswap writeback buffer found
+ * by a swap-in
+ * @folio: the swap cache folio the swap-in found
+ *
+ * zswap writeback keeps its buffer off the LRU and marked dropbehind, so that
+ * its writeback completion can free it, and parks the slot's workingset
+ * shadow. A swap-in that finds the buffer still marked takes it over instead:
+ * it evaluates the refault as for a new folio and adds the folio to the LRU
+ * itself, in the fault, as any other swap-in does; the completion then leaves
+ * it alone. A buffer the completion already put back on the LRU, outside the
+ * fault, goes through workingset_refault_lru_managed() instead. Nothing is
+ * done for any other folio.
+ *
+ * Context: @folio must be locked.
+ */
+void zswap_swapin_writeback_folio(struct folio *folio)
+{
+ void *shadow = zswap_get_and_clear_writeback_shadow(folio);
+
+ if (folio_test_clear_dropbehind(folio)) {
+ if (shadow)
+ workingset_refault(folio, shadow);
+ folio_add_lru(folio);
+ } else if (shadow) {
+ workingset_refault_lru_managed(folio, shadow);
+ }
+}
+
/*
* Attempts to free an entry by adding a folio to the swap cache,
* decompressing the entry data into the folio, and issuing a
@@ -1081,11 +1197,13 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
struct zswap_entry *entry,
swp_entry_t swpentry)
{
+ struct swap_cluster_info *ci;
struct xarray *tree;
pgoff_t offset = swp_offset(swpentry);
struct folio *folio;
struct mempolicy *mpol;
struct swap_info_struct *si;
+ void *shadow;
int ret = 0;
/* try to allocate swap cache folio */
@@ -1095,7 +1213,7 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
mpol = get_task_policy(current);
folio = __swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol,
- NO_INTERLEAVE_INDEX, NULL);
+ NO_INTERLEAVE_INDEX, &shadow);
put_swap_device(si);
/*
@@ -1130,7 +1248,12 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
goto err;
}
- xa_erase(tree, offset);
+ /*
+ * A slot with no shadow already reads back as ZSWAP_WRITEBACK_NO_SHADOW:
+ * the swap table marks a swapped out slot with xa_mk_value(0) and holds
+ * the shadow, when there is one, in that same value.
+ */
+ xa_store(tree, offset, shadow, GFP_KERNEL);
count_vm_event(ZSWPWB);
if (entry->objcg)
@@ -1162,7 +1285,15 @@ static int zswap_writeback_entry(struct swap_io_ctx *ctx,
return 0;
err:
- swap_cache_del_folio(folio);
+ /*
+ * The allocation above overwrote the slot's shadow and nothing has
+ * been parked for it yet, so restore it as the folio leaves the swap
+ * cache, or the eviction it records is lost.
+ */
+ ci = swap_cluster_lock(__swap_entry_to_info(swpentry), offset);
+ __swap_cache_del_folio(ci, folio, swpentry, shadow, false);
+ swap_cluster_unlock(ci);
+ folio_ref_sub(folio, folio_nr_pages(folio));
folio_unlock(folio);
folio_put(folio);
return ret;
@@ -1624,6 +1755,17 @@ bool zswap_store(struct folio *folio)
VM_WARN_ON_ONCE(!folio_test_locked(folio));
VM_WARN_ON_ONCE(!folio_test_swapcache(folio));
+ /*
+ * A writeback buffer whose IO failed is redirtied and left in the
+ * swap cache, so reclaim writes it out again. Writeback already
+ * decided this data belongs on disk, so send it there instead of
+ * compressing it back into zswap. This also leaves the shadow
+ * parked for the buffer in place, so it is still restored into
+ * the slot once the folio leaves the swap cache.
+ */
+ if (zswap_is_writeback_folio(folio))
+ return false;
+
if (!zswap_enabled)
goto check_old;
@@ -1791,19 +1933,31 @@ int zswap_load(struct folio *folio)
void zswap_invalidate(int type, pgoff_t offset, unsigned long nr_entries)
{
struct zswap_entry *entry;
- struct xarray *tree;
unsigned long i;
if (zswap_never_enabled())
return;
for (i = 0; i < nr_entries; i++) {
- tree = zswap_tree(type, offset + i);
+ XA_STATE(xas, zswap_tree(type, offset + i), offset + i);
- if (xa_empty(tree))
+ if (xa_empty(xas.xa))
continue;
- entry = xa_erase(tree, offset + i);
+ /*
+ * A parked writeback shadow is not an entry: it belongs to the
+ * buffer still in the swap cache, which restores it when it
+ * leaves (see __swap_cache_do_del_folio()). This happens when a
+ * redirtied zero-filled buffer is written out again.
+ */
+ xas_lock(&xas);
+ entry = xas_load(&xas);
+ if (xa_is_value(entry))
+ entry = NULL;
+ else if (entry)
+ xas_store(&xas, NULL);
+ xas_unlock(&xas);
+
if (entry)
zswap_entry_free(entry);
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path
2026-12-01 17:57 [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path Alexandre Ghiti
` (2 preceding siblings ...)
2026-12-01 17:57 ` [PATCH v5 3/3] mm: zswap: preserve the workingset shadow across writeback Alexandre Ghiti
@ 2026-12-01 18:17 ` Yosry Ahmed
2026-12-02 3:33 ` Nhat Pham
2026-12-02 7:29 ` Chris Li
3 siblings, 2 replies; 13+ messages in thread
From: Yosry Ahmed @ 2026-12-01 18:17 UTC (permalink / raw)
To: Alexandre Ghiti
Cc: Johannes Weiner, 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, linux-mm,
linux-kernel
On Tue, Dec 1, 2026 at 9:57 AM Alexandre Ghiti <alex@ghiti.fr> 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.
I admittedly did not look at the individual patches yet, but this
seems to be too invasive. I also wonder how this would interact with
incoming vswap and swap tiering work. Seems like it would become a
generic problem whenever we need to move a page between swap tiers, we
have to allocate a folio in the swap cache, but we want to keep the
shadow intact and avoid reactivation.
Seems like both problems can be fixed if we can teach the swap cache
about a folio being added for paths other than swapin? Patch 1 reads
like it does this, but seems like we still need patch 3 to keep the
shadow alive inside zswap on writeback. I think patch 1 only moved the
refault out of the swap cache but kept allocating a new shadow?
I wonder if both problems can be tackled at once if we handle this
within the swap cache, and if the implementation would be simpler.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path
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 7:29 ` Chris Li
1 sibling, 1 reply; 13+ messages in thread
From: Nhat Pham @ 2026-12-02 3:33 UTC (permalink / raw)
To: Yosry Ahmed
Cc: Alexandre Ghiti, Johannes Weiner, 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, linux-mm,
linux-kernel
On Tue, Dec 1, 2026 at 11:17 AM Yosry Ahmed <yosry@kernel.org> wrote:
>
> On Tue, Dec 1, 2026 at 9:57 AM Alexandre Ghiti <alex@ghiti.fr> 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.
>
> I admittedly did not look at the individual patches yet, but this
> seems to be too invasive. I also wonder how this would interact with
> incoming vswap and swap tiering work. Seems like it would become a
> generic problem whenever we need to move a page between swap tiers, we
> have to allocate a folio in the swap cache, but we want to keep the
> shadow intact and avoid reactivation.
Yep. We rely on swap cache as the synchronization point, so
unfortunately it will be a problem for all form of backend transfer
down the line.
>
> Seems like both problems can be fixed if we can teach the swap cache
> about a folio being added for paths other than swapin? Patch 1 reads
> like it does this, but seems like we still need patch 3 to keep the
> shadow alive inside zswap on writeback. I think patch 1 only moved the
> refault out of the swap cache but kept allocating a new shadow?
>
> I wonder if both problems can be tackled at once if we handle this
> within the swap cache, and if the implementation would be simpler.
Porting this solution to vswap kind of gives you the generic solution
that you're interested in.
In vswap world, we add a per-swap-cluster array for the backend. So
instead of moving the shadow to the zswap tree during writeback, we
will move the shadow to that array instead (and everything else in
this patch series stay the same). That is plenty generic - it will
work for all form of backend transfer mechanisms that rely on swap
cache as the synchronization point.
(I'm not sure if we'll have enough tags in 32 bit systems though -
might have to add a type field for that setup).
So basically, this is another reason that you kind of need vswap for
efficient and sound backend transfer :)
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path
2026-12-02 3:33 ` Nhat Pham
@ 2026-12-02 4:33 ` Yosry Ahmed
2026-12-02 4:40 ` Nhat Pham
0 siblings, 1 reply; 13+ messages in thread
From: Yosry Ahmed @ 2026-12-02 4:33 UTC (permalink / raw)
To: Nhat Pham
Cc: Alexandre Ghiti, Johannes Weiner, 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, linux-mm,
linux-kernel
On Tue, Dec 1, 2026 at 7:33 PM Nhat Pham <nphamcs@gmail.com> wrote:
>
> On Tue, Dec 1, 2026 at 11:17 AM Yosry Ahmed <yosry@kernel.org> wrote:
> >
> > On Tue, Dec 1, 2026 at 9:57 AM Alexandre Ghiti <alex@ghiti.fr> 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.
> >
> > I admittedly did not look at the individual patches yet, but this
> > seems to be too invasive. I also wonder how this would interact with
> > incoming vswap and swap tiering work. Seems like it would become a
> > generic problem whenever we need to move a page between swap tiers, we
> > have to allocate a folio in the swap cache, but we want to keep the
> > shadow intact and avoid reactivation.
>
> Yep. We rely on swap cache as the synchronization point, so
> unfortunately it will be a problem for all form of backend transfer
> down the line.
>
> >
> > Seems like both problems can be fixed if we can teach the swap cache
> > about a folio being added for paths other than swapin? Patch 1 reads
> > like it does this, but seems like we still need patch 3 to keep the
> > shadow alive inside zswap on writeback. I think patch 1 only moved the
> > refault out of the swap cache but kept allocating a new shadow?
> >
> > I wonder if both problems can be tackled at once if we handle this
> > within the swap cache, and if the implementation would be simpler.
>
> Porting this solution to vswap kind of gives you the generic solution
> that you're interested in.
>
> In vswap world, we add a per-swap-cluster array for the backend. So
> instead of moving the shadow to the zswap tree during writeback, we
> will move the shadow to that array instead (and everything else in
> this patch series stay the same). That is plenty generic - it will
> work for all form of backend transfer mechanisms that rely on swap
> cache as the synchronization point.
>
> (I'm not sure if we'll have enough tags in 32 bit systems though -
> might have to add a type field for that setup).
>
> So basically, this is another reason that you kind of need vswap for
> efficient and sound backend transfer :)
I agree that vswap provides a better place for this, but I honestly
don't like shoving the shadow into the zswap entry in the meantime.
Would this be cleaner / more generic if it's done within the
swapcache? We can always move it to vswap later as needed.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path
2026-12-02 4:33 ` Yosry Ahmed
@ 2026-12-02 4:40 ` Nhat Pham
2026-12-02 4:59 ` Yosry Ahmed
0 siblings, 1 reply; 13+ messages in thread
From: Nhat Pham @ 2026-12-02 4:40 UTC (permalink / raw)
To: Yosry Ahmed
Cc: Alexandre Ghiti, Johannes Weiner, 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, linux-mm,
linux-kernel
On Tue, Dec 1, 2026 at 9:33 PM Yosry Ahmed <yosry@kernel.org> wrote:
>
> On Tue, Dec 1, 2026 at 7:33 PM Nhat Pham <nphamcs@gmail.com> wrote:
> >
> > On Tue, Dec 1, 2026 at 11:17 AM Yosry Ahmed <yosry@kernel.org> wrote:
> > >
> > > On Tue, Dec 1, 2026 at 9:57 AM Alexandre Ghiti <alex@ghiti.fr> 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.
> > >
> > > I admittedly did not look at the individual patches yet, but this
> > > seems to be too invasive. I also wonder how this would interact with
> > > incoming vswap and swap tiering work. Seems like it would become a
> > > generic problem whenever we need to move a page between swap tiers, we
> > > have to allocate a folio in the swap cache, but we want to keep the
> > > shadow intact and avoid reactivation.
> >
> > Yep. We rely on swap cache as the synchronization point, so
> > unfortunately it will be a problem for all form of backend transfer
> > down the line.
> >
> > >
> > > Seems like both problems can be fixed if we can teach the swap cache
> > > about a folio being added for paths other than swapin? Patch 1 reads
> > > like it does this, but seems like we still need patch 3 to keep the
> > > shadow alive inside zswap on writeback. I think patch 1 only moved the
> > > refault out of the swap cache but kept allocating a new shadow?
> > >
> > > I wonder if both problems can be tackled at once if we handle this
> > > within the swap cache, and if the implementation would be simpler.
> >
> > Porting this solution to vswap kind of gives you the generic solution
> > that you're interested in.
> >
> > In vswap world, we add a per-swap-cluster array for the backend. So
> > instead of moving the shadow to the zswap tree during writeback, we
> > will move the shadow to that array instead (and everything else in
> > this patch series stay the same). That is plenty generic - it will
> > work for all form of backend transfer mechanisms that rely on swap
> > cache as the synchronization point.
> >
> > (I'm not sure if we'll have enough tags in 32 bit systems though -
> > might have to add a type field for that setup).
> >
> > So basically, this is another reason that you kind of need vswap for
> > efficient and sound backend transfer :)
>
> I agree that vswap provides a better place for this, but I honestly
> don't like shoving the shadow into the zswap entry in the meantime.
> Would this be cleaner / more generic if it's done within the
> swapcache? We can always move it to vswap later as needed.
What do you mean by "within the swap cache"? The swap table slot that
used to hold the shadow now carries the swap cache folio, so we can't
really store it there. I guess we can tinker with the folio's fields?
Not sure if that's cleaner though...
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path
2026-12-02 4:40 ` Nhat Pham
@ 2026-12-02 4:59 ` Yosry Ahmed
2026-12-02 9:25 ` Alexandre Ghiti
0 siblings, 1 reply; 13+ messages in thread
From: Yosry Ahmed @ 2026-12-02 4:59 UTC (permalink / raw)
To: Nhat Pham
Cc: Alexandre Ghiti, Johannes Weiner, 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, linux-mm,
linux-kernel
On Tue, Dec 1, 2026 at 8:40 PM Nhat Pham <nphamcs@gmail.com> wrote:
>
> On Tue, Dec 1, 2026 at 9:33 PM Yosry Ahmed <yosry@kernel.org> wrote:
> >
> > On Tue, Dec 1, 2026 at 7:33 PM Nhat Pham <nphamcs@gmail.com> wrote:
> > >
> > > On Tue, Dec 1, 2026 at 11:17 AM Yosry Ahmed <yosry@kernel.org> wrote:
> > > >
> > > > On Tue, Dec 1, 2026 at 9:57 AM Alexandre Ghiti <alex@ghiti.fr> 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.
> > > >
> > > > I admittedly did not look at the individual patches yet, but this
> > > > seems to be too invasive. I also wonder how this would interact with
> > > > incoming vswap and swap tiering work. Seems like it would become a
> > > > generic problem whenever we need to move a page between swap tiers, we
> > > > have to allocate a folio in the swap cache, but we want to keep the
> > > > shadow intact and avoid reactivation.
> > >
> > > Yep. We rely on swap cache as the synchronization point, so
> > > unfortunately it will be a problem for all form of backend transfer
> > > down the line.
> > >
> > > >
> > > > Seems like both problems can be fixed if we can teach the swap cache
> > > > about a folio being added for paths other than swapin? Patch 1 reads
> > > > like it does this, but seems like we still need patch 3 to keep the
> > > > shadow alive inside zswap on writeback. I think patch 1 only moved the
> > > > refault out of the swap cache but kept allocating a new shadow?
> > > >
> > > > I wonder if both problems can be tackled at once if we handle this
> > > > within the swap cache, and if the implementation would be simpler.
> > >
> > > Porting this solution to vswap kind of gives you the generic solution
> > > that you're interested in.
> > >
> > > In vswap world, we add a per-swap-cluster array for the backend. So
> > > instead of moving the shadow to the zswap tree during writeback, we
> > > will move the shadow to that array instead (and everything else in
> > > this patch series stay the same). That is plenty generic - it will
> > > work for all form of backend transfer mechanisms that rely on swap
> > > cache as the synchronization point.
> > >
> > > (I'm not sure if we'll have enough tags in 32 bit systems though -
> > > might have to add a type field for that setup).
> > >
> > > So basically, this is another reason that you kind of need vswap for
> > > efficient and sound backend transfer :)
> >
> > I agree that vswap provides a better place for this, but I honestly
> > don't like shoving the shadow into the zswap entry in the meantime.
> > Would this be cleaner / more generic if it's done within the
> > swapcache? We can always move it to vswap later as needed.
>
> What do you mean by "within the swap cache"? The swap table slot that
> used to hold the shadow now carries the swap cache folio, so we can't
> really store it there. I guess we can tinker with the folio's fields?
> Not sure if that's cleaner though...
Hmm yeah.. I honestly wonder if we should table this (patch 3
specifically) until vswap lands. More motivation for landing vswap :P
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path
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 7:29 ` Chris Li
2026-12-02 9:49 ` Alexandre Ghiti
1 sibling, 1 reply; 13+ messages in thread
From: Chris Li @ 2026-12-02 7:29 UTC (permalink / raw)
To: Yosry Ahmed
Cc: Alexandre Ghiti, Johannes Weiner, 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,
Kairui Song, Kemeng Shi, Baoquan He, Barry Song, Youngjun Park,
Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie, Wei Xu,
Joonsoo Kim, linux-mm, linux-kernel
On Tue, Dec 1, 2026 at 10:17 AM Yosry Ahmed <yosry@kernel.org> wrote:
>
> On Tue, Dec 1, 2026 at 9:57 AM Alexandre Ghiti <alex@ghiti.fr> 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.
>
> I admittedly did not look at the individual patches yet, but this
> seems to be too invasive. I also wonder how this would interact with
> incoming vswap and swap tiering work. Seems like it would become a
> generic problem whenever we need to move a page between swap tiers, we
> have to allocate a folio in the swap cache, but we want to keep the
> shadow intact and avoid reactivation.
I feel it is too invasive as well.
>
> Seems like both problems can be fixed if we can teach the swap cache
> about a folio being added for paths other than swapin? Patch 1 reads
I am actually thinking the opposite. How about we avoid loading into
the swap cache if it is not a page fault? The swap cache's folio
loading assumes it is for a page fault; there are other counter side
effects as well as shadows. If we keep it that way. During writeback,
let zswap load into a folio, mark the zswap entry as writing back, and
submit it to the block layer. Then when the io completes. There are
two case. If there is no page fault for this entry, free the zswap
entry and you are done. If a page fault occurs, the page fault handler
can notice this zswap entry is writing back. Serve the swap cache
normally by installing a new folio into the swap cache, but don't
delete the zswap entry until the writeback completion callback. The
data on the block device is outdated (useless).
Do I miss any race condition with this approach?
This fits into my original design where the swap cache is the top
half, tightly coupled to the user page fault. Moving data between
backends is at the bottom half of the swap entry.
Chris
> like it does this, but seems like we still need patch 3 to keep the
> shadow alive inside zswap on writeback. I think patch 1 only moved the
> refault out of the swap cache but kept allocating a new shadow?
>
> I wonder if both problems can be tackled at once if we handle this
> within the swap cache, and if the implementation would be simpler.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path
2026-12-02 4:59 ` Yosry Ahmed
@ 2026-12-02 9:25 ` Alexandre Ghiti
0 siblings, 0 replies; 13+ messages in thread
From: Alexandre Ghiti @ 2026-12-02 9:25 UTC (permalink / raw)
To: Yosry Ahmed
Cc: Nhat Pham, Alexandre Ghiti, Johannes Weiner, 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, linux-mm, linux-kernel
Hi Yosry, Nhat,
On Wed, Dec 2, 2026 at 5:59 AM Yosry Ahmed <yosry@kernel.org> wrote:
>
> >
> On Tue, Dec 1, 2026 at 8:40 PM Nhat Pham <nphamcs@gmail.com> wrote:
> >
> > On Tue, Dec 1, 2026 at 9:33 PM Yosry Ahmed <yosry@kernel.org> wrote:
> > >
> > > On Tue, Dec 1, 2026 at 7:33 PM Nhat Pham <nphamcs@gmail.com> wrote:
> > > >
> > > > On Tue, Dec 1, 2026 at 11:17 AM Yosry Ahmed <yosry@kernel.org> wrote:
> > > > >
> > > > > On Tue, Dec 1, 2026 at 9:57 AM Alexandre Ghiti <alex@ghiti.fr> 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.
> > > > >
> > > > > I admittedly did not look at the individual patches yet, but this
> > > > > seems to be too invasive. I also wonder how this would interact with
> > > > > incoming vswap and swap tiering work. Seems like it would become a
> > > > > generic problem whenever we need to move a page between swap tiers, we
> > > > > have to allocate a folio in the swap cache, but we want to keep the
> > > > > shadow intact and avoid reactivation.
> > > >
Yes it is invasive, I never found a less invasive way to do that!
> > > > Yep. We rely on swap cache as the synchronization point, so
> > > > unfortunately it will be a problem for all form of backend transfer
> > > > down the line.
> > > >
> > > > >
> > > > > Seems like both problems can be fixed if we can teach the swap cache
> > > > > about a folio being added for paths other than swapin? Patch 1 reads
> > > > > like it does this, but seems like we still need patch 3 to keep the
> > > > > shadow alive inside zswap on writeback. I think patch 1 only moved the
> > > > > refault out of the swap cache but kept allocating a new shadow?
If you prefer, I can come up with a new variant of patch 1 that
teaches swapcache about swapin and other paths so it does not wrongly
count the refault during writeback.
But patch 3 is still needed because otherwise a new shadow would be
minted when the buffer is freed.
> > > > >
> > > > > I wonder if both problems can be tackled at once if we handle this
> > > > > within the swap cache, and if the implementation would be simpler.
> > > >
> > > > Porting this solution to vswap kind of gives you the generic solution
> > > > that you're interested in.
> > > >
> > > > In vswap world, we add a per-swap-cluster array for the backend. So
> > > > instead of moving the shadow to the zswap tree during writeback, we
> > > > will move the shadow to that array instead (and everything else in
> > > > this patch series stay the same). That is plenty generic - it will
> > > > work for all form of backend transfer mechanisms that rely on swap
> > > > cache as the synchronization point.
> > > >
> > > > (I'm not sure if we'll have enough tags in 32 bit systems though -
> > > > might have to add a type field for that setup).
> > > >
> > > > So basically, this is another reason that you kind of need vswap for
> > > > efficient and sound backend transfer :)
> > >
> > > I agree that vswap provides a better place for this, but I honestly
> > > don't like shoving the shadow into the zswap entry in the meantime.
> > > Would this be cleaner / more generic if it's done within the
> > > swapcache? We can always move it to vswap later as needed.
> >
> > What do you mean by "within the swap cache"? The swap table slot that
> > used to hold the shadow now carries the swap cache folio, so we can't
> > really store it there. I guess we can tinker with the folio's fields?
> > Not sure if that's cleaner though...
>
> Hmm yeah.. I honestly wonder if we should table this (patch 3
> specifically) until vswap lands. More motivation for landing vswap :P
>
Yes the virtual_table of vswap would be "prettier" (even though my llm
and I did not find an easy way to use it during writeback @Nhat).
But as Nhat said (and unless someone has a better idea), this is the
only thing that would change with vswap, so it won't make it easier to
review.
In addition, this fix then becomes totally un-backportable to stable
releases (this bug has been present since 5.9).
Thanks for the quick feedback though,
Alex
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v5 0/3] mm: fix workingset refaults in the zswap writeback path
2026-12-02 7:29 ` Chris Li
@ 2026-12-02 9:49 ` Alexandre Ghiti
0 siblings, 0 replies; 13+ messages in thread
From: Alexandre Ghiti @ 2026-12-02 9:49 UTC (permalink / raw)
To: Chris Li
Cc: Yosry Ahmed, Alexandre Ghiti, Johannes Weiner, 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,
Kairui Song, Kemeng Shi, Baoquan He, Barry Song, Youngjun Park,
Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie, Wei Xu,
Joonsoo Kim, linux-mm, linux-kernel
Hi Chris,
On Wed, Dec 2, 2026 at 8:30 AM Chris Li <chrisl@kernel.org> wrote:
>
> >
> On Tue, Dec 1, 2026 at 10:17 AM Yosry Ahmed <yosry@kernel.org> wrote:
> >
> > On Tue, Dec 1, 2026 at 9:57 AM Alexandre Ghiti <alex@ghiti.fr> 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.
> >
> > I admittedly did not look at the individual patches yet, but this
> > seems to be too invasive. I also wonder how this would interact with
> > incoming vswap and swap tiering work. Seems like it would become a
> > generic problem whenever we need to move a page between swap tiers, we
> > have to allocate a folio in the swap cache, but we want to keep the
> > shadow intact and avoid reactivation.
>
> I feel it is too invasive as well.
>
> >
> > Seems like both problems can be fixed if we can teach the swap cache
> > about a folio being added for paths other than swapin? Patch 1 reads
>
> I am actually thinking the opposite. How about we avoid loading into
> the swap cache if it is not a page fault? The swap cache's folio
> loading assumes it is for a page fault; there are other counter side
> effects as well as shadows. If we keep it that way. During writeback,
> let zswap load into a folio, mark the zswap entry as writing back, and
> submit it to the block layer. Then when the io completes. There are
> two case. If there is no page fault for this entry, free the zswap
> entry and you are done. If a page fault occurs, the page fault handler
> can notice this zswap entry is writing back. Serve the swap cache
> normally by installing a new folio into the swap cache, but don't
> delete the zswap entry until the writeback completion callback. The
> data on the block device is outdated (useless).
> Do I miss any race condition with this approach?
>
> This fits into my original design where the swap cache is the top
> half, tightly coupled to the user page fault. Moving data between
> backends is at the bottom half of the swap entry.
Nothing seems to prevent your approach: the races we (my llm and I,
mostly my llm tbh) found (the slot freed and reused while the write is
still in flight, a second write to the same slot after a fault,
load/invalidate freeing an entry under writeback, swapoff waiting for
in-flight writes) can all be fixed.
But wouldn't that defeat the purpose of the swap cache as the
synchronization layer? We would have to reimplement that
synchronization inside zswap.
Thanks,
Alex
>
> Chris
>
> > like it does this, but seems like we still need patch 3 to keep the
> > shadow alive inside zswap on writeback. I think patch 1 only moved the
> > refault out of the swap cache but kept allocating a new shadow?
> >
> > I wonder if both problems can be tackled at once if we handle this
> > within the swap cache, and if the implementation would be simpler.
>
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH v5 3/3] mm: zswap: preserve the workingset shadow across writeback
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
0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-12-02 18:01 UTC (permalink / raw)
To: Alexandre Ghiti, 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
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] mm: missing zswap_swapin_writeback_folio() call in
unuse_pte_range()
- [Medium] mm: zswap: unnecessary O(nr_pages) xa_load() loop under
ci->lock
--
commit 7f5e8c1d5ef64588519452bf15c64dc4a8d5a95f
Author: Alexandre Ghiti <alex@ghiti.fr>
mm: zswap: preserve the workingset shadow across writeback
This commit preserves a swap slot's workingset shadow across zswap
writeback by parking it in the zswap tree in place of the freed entry and
consuming or restoring it when the writeback buffer folio is swapped in or
removed from the swap cache.
> diff --git a/mm/memory.c b/mm/memory.c
> index d477a4f986bc8..6fe6ab2ad315e 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -5104,6 +5104,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
> goto out_page;
> }
>
> + zswap_swapin_writeback_folio(folio);
> +
> /*
> * KSM sometimes has to copy on read faults, for example, if
> * folio->index of non-ksm folios would be nonlinear inside the
[Severity: Medium]
Should unuse_pte_range() also call zswap_swapin_writeback_folio(folio)
before unuse_pte()?
When swapoff finds a zswap writeback buffer via swap_cache_get_folio(),
unuse_pte_range() locks the folio, waits for writeback, maps it with
unuse_pte(), and calls folio_free_swap() without consuming the parked
shadow. If folio_free_swap() removes the folio from the swap cache,
__swap_cache_do_del_folio() discards the shadow without evaluating
workingset_refault(). If the folio stays in the swap cache (for example,
the swap entry is shared across fork() and try_to_unuse() is interrupted
by a signal before the other process exits), the parked shadow remains in
the zswap tree on the now-resident folio, so later reclaim treats it as a
writeback buffer in zswap_is_writeback_folio(), skipping zswap_store() and
workingset_eviction().
> diff --git a/mm/zswap.c b/mm/zswap.c
> index 4f0a0afe534d3..4096abafaf2ac 100644
> --- a/mm/zswap.c
> +++ b/mm/zswap.c
> @@ -1062,6 +1062,122 @@ struct zswap_shrink_ctl {
[ ... ]
> +void *zswap_get_and_clear_writeback_shadow(struct folio *folio)
> +{
> + swp_entry_t swp = folio->swap;
> + unsigned long nr_pages = folio_nr_pages(folio);
> + pgoff_t offset = swp_offset(swp);
> + void *shadow = NULL;
> + unsigned long i;
> +
> + if (zswap_never_enabled())
> + return NULL;
> +
> + for (i = 0; i < nr_pages; i++) {
> + swp_entry_t e = swp_entry(swp_type(swp), offset + i);
> + struct xarray *tree = swap_zswap_tree(e);
> + void *parked = xa_load(tree, offset + i);
> +
> + if (!xa_is_value(parked))
> + continue;
> + xa_erase(tree, offset + i);
> + if (parked != ZSWAP_WRITEBACK_NO_SHADOW)
> + shadow = parked;
> + }
> +
> + return shadow;
> +}
[Severity: Medium]
Can this break (or walk with an XA_STATE) when !xa_is_value(parked), as
zswap_is_writeback_folio() does?
__swap_cache_do_del_folio() calls zswap_get_and_clear_writeback_shadow()
on every swap-cache deletion while holding ci->lock (with IRQs disabled
from __remove_mapping()). For a normal large folio whose zswap slots hold
zswap_entry pointers or NULL, using continue instead of break performs up
to 512 separate xa_load() walks under ci->lock.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201175729.790028-1-alex@ghiti.fr?part=3
^ 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