Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] mm/vmscan: clear hopeless kswapd when global direct reclaim makes progress
@ 2026-07-11  4:39 Altan Hacigumus
  0 siblings, 0 replies; only message in thread
From: Altan Hacigumus @ 2026-07-11  4:39 UTC (permalink / raw)
  To: Andrew Morton, David Hildenbrand, Johannes Weiner
  Cc: Kairui Song, Qi Zheng, Shakeel Butt, Barry Song, Axel Rasmussen,
	Yuanchu Xie, Wei Xu, Lorenzo Stoakes, Liam R . Howlett,
	Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
	Jiayuan Chen, linux-mm, linux-kernel

Direct reclaim clears the hopeless kswapd state only once the node
becomes balanced - added by commit dc9fe9b7056a ("mm/vmscan: mitigate
spurious kswapd_failures reset from direct reclaim") so that cgroup
memory.high reclaim cannot repeatedly revive a kswapd that is unable to
balance the node. Before it, any reclaim progress revived kswapd.

However, this restriction also prevents global direct reclaim from
reviving kswapd. Under sustained memory pressure, global direct reclaim
may continue making progress without the node ever reaching the high
watermark, leaving reclaim to allocating tasks.

The effect is visible on systems where a database workload mlocks most
of memory and the remainder is under continuous pressure: allocations
stall long enough that ordinary tasks (e.g. ssh) become slow or
unresponsive, while kswapd sits idle.

Unlike memcg reclaim, global direct reclaim follows the same node-wide
reclaim path as kswapd. Clear the hopeless state when global direct
reclaim makes progress, while continuing to require a balanced node for
memcg reclaim. kswapd_try_clear_hopeless() becomes static since struct
scan_control is private to vmscan.c.

On a workload with most memory mlocked and the remainder under sustained
churn, with swap enabled, comparing the same 60s window:
                              base       patched
  allocstall (all zones)    413441          6532
  pgsteal_direct          15889552        255619
  pgsteal_kswapd                 0      26079382
  PSI memory full avg60     13.10%         9.37%

Direct reclaim was already reclaiming many pages in the baseline;
this change lets kswapd resume doing that work asynchronously.

Signed-off-by: Altan Hacigumus <ahacigu.linux@gmail.com>
---
v2: fold the impact and the origin commit into the changelog
v1: https://lore.kernel.org/all/20260710024429.70923-1-ahacigu.linux@gmail.com/
---
 include/linux/mmzone.h |  2 --
 mm/vmscan.c            | 23 ++++++++++++++---------
 2 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index ca2712187147..1db6f8dac927 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -1630,8 +1630,6 @@ enum kswapd_clear_hopeless_reason {
 
 void wakeup_kswapd(struct zone *zone, gfp_t gfp_mask, int order,
 		   enum zone_type highest_zoneidx);
-void kswapd_try_clear_hopeless(struct pglist_data *pgdat,
-			       unsigned int order, int highest_zoneidx);
 void kswapd_clear_hopeless(pg_data_t *pgdat, enum kswapd_clear_hopeless_reason reason);
 bool kswapd_test_hopeless(pg_data_t *pgdat);
 
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 35c3bb15ae96..78fd35a11eee 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -198,6 +198,9 @@ struct scan_control {
  */
 int vm_swappiness = 60;
 
+static void kswapd_try_clear_hopeless(struct pglist_data *pgdat,
+				      struct scan_control *sc);
+
 #ifdef CONFIG_MEMCG
 
 /* Returns true for reclaim through cgroup limits or cgroup interfaces. */
@@ -5172,7 +5175,7 @@ static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *
 	blk_finish_plug(&plug);
 done:
 	if (sc->nr_reclaimed > reclaimed)
-		kswapd_try_clear_hopeless(pgdat, sc->order, sc->reclaim_idx);
+		kswapd_try_clear_hopeless(pgdat, sc);
 }
 
 /******************************************************************************
@@ -6251,7 +6254,7 @@ static void shrink_node(pg_data_t *pgdat, struct scan_control *sc)
 	 * successful direct reclaim run will revive a dormant kswapd.
 	 */
 	if (reclaimable)
-		kswapd_try_clear_hopeless(pgdat, sc->order, sc->reclaim_idx);
+		kswapd_try_clear_hopeless(pgdat, sc);
 	else if (sc->cache_trim_mode)
 		sc->cache_trim_mode_failed = 1;
 }
@@ -7530,15 +7533,17 @@ void kswapd_clear_hopeless(pg_data_t *pgdat, enum kswapd_clear_hopeless_reason r
 }
 
 /*
- * Reset kswapd_failures only when the node is balanced. Without this
- * check, successful direct reclaim (e.g., from cgroup memory.high
- * throttling) can keep resetting kswapd_failures even when the node
- * cannot be balanced, causing kswapd to run endlessly.
+ * Reset kswapd_failures when the node is balanced, or when global
+ * direct reclaim makes progress - then kswapd can make progress too.
+ * Memcg reclaim can succeed where kswapd cannot (memcg protection is
+ * not enforced against the reclaim target), so its progress resets
+ * kswapd_failures only when the node is balanced.
  */
-void kswapd_try_clear_hopeless(struct pglist_data *pgdat,
-			       unsigned int order, int highest_zoneidx)
+static void kswapd_try_clear_hopeless(struct pglist_data *pgdat,
+				      struct scan_control *sc)
 {
-	if (pgdat_balanced(pgdat, order, highest_zoneidx))
+	if ((!current_is_kswapd() && !cgroup_reclaim(sc)) ||
+	    pgdat_balanced(pgdat, sc->order, sc->reclaim_idx))
 		kswapd_clear_hopeless(pgdat, current_is_kswapd() ?
 			KSWAPD_CLEAR_HOPELESS_KSWAPD : KSWAPD_CLEAR_HOPELESS_DIRECT);
 }
-- 
2.55.0



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-07-11  4:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-11  4:39 [PATCH v2] mm/vmscan: clear hopeless kswapd when global direct reclaim makes progress Altan Hacigumus

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