[PATCH v5 2/3] mm: workingset: add workingset_refault_lru_managed()
From: Alexandre Ghiti
Date: Tue Dec 01 2026 - 12:59:58 EST
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@xxxxxxxxx>
Signed-off-by: Nhat Pham <nphamcs@xxxxxxxxx>
Signed-off-by: Alexandre Ghiti <alex@xxxxxxxx>
---
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