[PATCH v5 3/3] mm: zswap: preserve the workingset shadow across writeback
From: Alexandre Ghiti
Date: Tue Dec 01 2026 - 13:00:56 EST
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@xxxxxxxxx>
Signed-off-by: Nhat Pham <nphamcs@xxxxxxxxx>
Signed-off-by: Alexandre Ghiti <alex@xxxxxxxx>
---
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