Skip to contentSkip to conversation

mm: re-enable kswapd when memory pressure subsides or demotion is toggled

PATCH Applied Chanwon Park 3 participants 4 messages Sep 8, 2025 – Sep 30, 2025

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: re-enable kswapd when memory pressure subsides or demotion is toggled

If kswapd fails to reclaim pages from a node MAX_RECLAIM_RETRIES in a row, kswapd on that node gets disabled. That is, the system won't wakeup kswapd for that node until page reclamation is observed at least once. That reclamation is mostly done by direct reclaim, which in turn enables kswapd back.

However, on systems with CXL memory nodes, workloads with high anon page usage can disable kswapd indefinitely, without triggering direct reclaim. This can be reproduced with following steps:

numa node 0 (32GB memory, 48 CPUs)
numa node 2~5 (512GB CXL memory, 128GB each)
(numa node 1 is disabled)
swap space 8GB

1) Set /sys/kernel/mm/demotion_enabled to 0.
2) Set /proc/sys/kernel/numa_balancing to 0.
3) Run a process that allocates and random accesses 500GB of anon pages.
4) Let the process exit normally.

During 3), free memory on node 0 gets lower than low watermark, and kswapd runs and depletes swap space. Then, kswapd fails consecutively and gets disabled. Allocation afterwards happens on CXL memory, so node 0 never gains more memory pressure to trigger direct reclaim.

After 4), kswapd on node 0 remains disabled, and tasks running on that node are unable to swap. If you turn on NUMA_BALANCING_MEMORY_TIERING and demotion now, it won't work properly since kswapd is disabled.

To mitigate this problem, reset kswapd_failures to 0 on following conditions:

a) ZONE_BELOW_HIGH bit of a zone in hopeless node with a fallback memory node gets cleared.
b) demotion_enabled is changed from false to true.

Rationale for a):
ZONE_BELOW_HIGH bit being cleared might be a sign that the node may be reclaimable afterwards. This won't help much if the memory-hungry process keeps running without freeing anything, but at least the node will go back to reclaimable state when the process exits.

Rationale for b):
When demotion_enabled is false, kswapd can only reclaim anon pages by swapping them out to swap space. If demotion_enabled is turned on, kswapd can demote anon pages to another node for reclaiming. So, the original failure count for determining reclaimability is no longer valid.

Since kswapd_failures resets may be missed by ++ operation, it is changed from int to atomic_t.

---
 include/linux/mmzone.h |  2 +-
 mm/memory-tiers.c      | 12 ++++++++++++
 mm/page_alloc.c        | 17 ++++++++++++++++-
 mm/show_mem.c          |  3 ++-
 mm/vmscan.c            | 14 +++++++-------
 mm/vmstat.c            |  2 +-
 6 files changed, 39 insertions(+), 11 deletions(-)
include/linux/mmzone.h +1 -1
index 283913d42d7b..68db1dbf375d 100644
@@ -1411,7 +1411,7 @@ typedef struct pglist_data {
 	int kswapd_order;
 	enum zone_type kswapd_highest_zoneidx;
 
-	int kswapd_failures;		/* Number of 'reclaimed == 0' runs */
+	atomic_t kswapd_failures;	/* Number of 'reclaimed == 0' runs */
 
 #ifdef CONFIG_COMPACTION
 	int kcompactd_max_order;
mm/memory-tiers.c +12 -0
index fc14fe53e9b7..f8f8f66fc4c0 100644
@@ -949,11 +949,23 @@ static ssize_t demotion_enabled_store(struct kobject *kobj,
 				      const char *buf, size_t count)
 {
 	ssize_t ret;
+	bool before = numa_demotion_enabled;
 
 	ret = kstrtobool(buf, &numa_demotion_enabled);
 	if (ret)
 		return ret;
 
+	/*
+	 * Reset kswapd_failures statistics. They may no longer be
+	 * valid since the policy for kswapd has changed.
+	 */
+	if (before == false && numa_demotion_enabled == true) {
+		struct pglist_data *pgdat;
+
+		for_each_online_pgdat(pgdat)
+			atomic_set(&pgdat->kswapd_failures, 0);
+	}
+
 	return count;
 }
 
mm/page_alloc.c +16 -1
index 2ef3c07266b3..827c9a949987 100644
@@ -2681,8 +2681,23 @@ static void free_frozen_page_commit(struct zone *zone,
 				   pcp, pindex);
 		if (test_bit(ZONE_BELOW_HIGH, &zone->flags) &&
 		    zone_watermark_ok(zone, 0, high_wmark_pages(zone),
-				      ZONE_MOVABLE, 0))
+				      ZONE_MOVABLE, 0)) {
+			struct pglist_data *pgdat = zone->zone_pgdat;
 			clear_bit(ZONE_BELOW_HIGH, &zone->flags);
+
+			/*
+			 * Assume that memory pressure on this node is gone
+			 * and may be in a reclaimable state. If a memory
+			 * fallback node exists, direct reclaim may not have
+			 * been triggered, leaving 'hopeless node' stay in
+			 * that state for a while. Let kswapd work again by
+			 * resetting kswapd_failures.
+			 */
+			if (atomic_read(&pgdat->kswapd_failures)
+			    >= MAX_RECLAIM_RETRIES &&
+			    next_memory_node(pgdat->node_id) < MAX_NUMNODES)
+				atomic_set(&pgdat->kswapd_failures, 0);
+		}
 	}
 }
 
mm/show_mem.c +2 -1
index 0cf8bf5d832d..18b3b32a9ccf 100644
@@ -280,7 +280,8 @@ static void show_free_areas(unsigned int filter, nodemask_t *nodemask, int max_z
 #endif
 			K(node_page_state(pgdat, NR_PAGETABLE)),
 			K(node_page_state(pgdat, NR_SECONDARY_PAGETABLE)),
-			str_yes_no(pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES),
+			str_yes_no(atomic_read(&pgdat->kswapd_failures)
+				   >= MAX_RECLAIM_RETRIES),
 			K(node_page_state(pgdat, NR_BALLOON_PAGES)));
 	}
 
mm/vmscan.c +7 -7
index 424412680cfc..e09d69b1f873 100644
@@ -526,7 +526,7 @@ static bool skip_throttle_noprogress(pg_data_t *pgdat)
 	 * If kswapd is disabled, reschedule if necessary but do not
 	 * throttle as the system is likely near OOM.
 	 */
-	if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
+	if (atomic_read(&pgdat->kswapd_failures) >= MAX_RECLAIM_RETRIES)
 		return true;
 
 	/*
@@ -5093,7 +5093,7 @@ static void lru_gen_shrink_node(struct pglist_data *pgdat, struct scan_control *
 	blk_finish_plug(&plug);
 done:
 	if (sc->nr_reclaimed > reclaimed)
-		pgdat->kswapd_failures = 0;
+		atomic_set(&pgdat->kswapd_failures, 0);
 }
 
 /******************************************************************************
@@ -6167,7 +6167,7 @@ static void shrink_node(pg_data_t *pgdat, struct scan_control *sc)
 	 * successful direct reclaim run will revive a dormant kswapd.
 	 */
 	if (reclaimable)
-		pgdat->kswapd_failures = 0;
+		atomic_set(&pgdat->kswapd_failures, 0);
 	else if (sc->cache_trim_mode)
 		sc->cache_trim_mode_failed = 1;
 }
@@ -6479,7 +6479,7 @@ static bool allow_direct_reclaim(pg_data_t *pgdat)
 	int i;
 	bool wmark_ok;
 
-	if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
+	if (atomic_read(&pgdat->kswapd_failures) >= MAX_RECLAIM_RETRIES)
 		return true;
 
 	for_each_managed_zone_pgdat(zone, pgdat, i, ZONE_NORMAL) {
@@ -6880,7 +6880,7 @@ static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order,
 		wake_up_all(&pgdat->pfmemalloc_wait);
 
 	/* Hopeless node, leave it to direct reclaim */
-	if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
+	if (atomic_read(&pgdat->kswapd_failures) >= MAX_RECLAIM_RETRIES)
 		return true;
 
 	if (pgdat_balanced(pgdat, order, highest_zoneidx)) {
@@ -7148,7 +7148,7 @@ static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx)
 	}
 
 	if (!sc.nr_reclaimed)
-		pgdat->kswapd_failures++;
+		atomic_inc(&pgdat->kswapd_failures);
 
 out:
 	clear_reclaim_active(pgdat, highest_zoneidx);
@@ -7407,7 +7407,7 @@ void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order,
 		return;
 
 	/* Hopeless node, leave it to direct reclaim if possible */
-	if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ||
+	if (atomic_read(&pgdat->kswapd_failures) >= MAX_RECLAIM_RETRIES ||
 	    (pgdat_balanced(pgdat, order, highest_zoneidx) &&
 	     !pgdat_watermark_boosted(pgdat, highest_zoneidx))) {
 		/*
mm/vmstat.c +1 -1
index a78d70ddeacd..3c0ea637ed85 100644
@@ -1826,7 +1826,7 @@ static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
 	seq_printf(m,
 		   "\n  node_unreclaimable:  %u"
 		   "\n  start_pfn:           %lu",
-		   pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES,
+		   atomic_read(&pgdat->kswapd_failures) >= MAX_RECLAIM_RETRIES,
 		   zone->zone_start_pfn);
 	seq_putc(m, '\n');
 }
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 15 lines · On Mon, 8 Sep 2025 19:04:10 +0900 Chanwon Park <[email protected]> wrote:

If kswapd fails to reclaim pages from a node MAX_RECLAIM_RETRIES in a row, kswapd on that node gets disabled. That is, the system won't wakeup kswapd for that node until page reclamation is observed at least once. That reclamation is mostly done by direct reclaim, which in turn enables kswapd back.

However, on systems with CXL memory nodes, workloads with high anon page usage can disable kswapd indefinitely, without triggering direct reclaim. This can be reproduced with following steps:

numa node 0 (32GB memory, 48 CPUs)
numa node 2~5 (512GB CXL memory, 128GB each)
(numa node 1 is disabled)
swap space 8GB

Quoted 5 lines

1) Set /sys/kernel/mm/demotion_enabled to 0.
2) Set /proc/sys/kernel/numa_balancing to 0.
3) Run a process that allocates and random accesses 500GB of anon pages.
4) Let the process exit normally.

hm, OK, I guess this is longstanding misbehavior?

Quoted 3 lines

Since kswapd_failures resets may be missed by ++ operation, it is changed from int to atomic_t.

Possibly this should have been a separate (earlier) patch. But I assume the need for this conversion was inroduced by this patch, so it's debatable.

include/linux/mmzone.h typedef struct pglist_data { · line 1414

Quoted 8 lines
include/linux/mmzone.h +1 -1
@@ -1411,7 +1411,7 @@ typedef struct pglist_data {
 	int kswapd_order;
 	enum zone_type kswapd_highest_zoneidx;
 
-	int kswapd_failures;		/* Number of 'reclaimed == 0' runs */
+	atomic_t kswapd_failures;	/* Number of 'reclaimed == 0' runs */

This caused a number of 80-column horrors! I had a fiddle, what do you think?

mm/page_alloc.c renamed from mm/page_alloc.c~mm-re-enable-kswapd-when-memory-pressure-subsides-or-demotion-is-toggled-fix +21 -21
@@ -2860,29 +2860,29 @@ static void free_frozen_page_commit(stru
 		 */
 		return;
 	}
+
 	high = nr_pcp_high(pcp, zone, batch, free_high);
-	if (pcp->count >= high) {
-		free_pcppages_bulk(zone, nr_pcp_free(pcp, batch, high, free_high),
-				   pcp, pindex);
-		if (test_bit(ZONE_BELOW_HIGH, &zone->flags) &&
-		    zone_watermark_ok(zone, 0, high_wmark_pages(zone),
-				      ZONE_MOVABLE, 0)) {
-			struct pglist_data *pgdat = zone->zone_pgdat;
-			clear_bit(ZONE_BELOW_HIGH, &zone->flags);
+	if (pcp->count < high)
+		return;
 
-			/*
-			 * Assume that memory pressure on this node is gone
-			 * and may be in a reclaimable state. If a memory
-			 * fallback node exists, direct reclaim may not have
-			 * been triggered, leaving 'hopeless node' stay in
-			 * that state for a while. Let kswapd work again by
-			 * resetting kswapd_failures.
-			 */
-			if (atomic_read(&pgdat->kswapd_failures)
-			    >= MAX_RECLAIM_RETRIES &&
-			    next_memory_node(pgdat->node_id) < MAX_NUMNODES)
-				atomic_set(&pgdat->kswapd_failures, 0);
-		}
+	free_pcppages_bulk(zone, nr_pcp_free(pcp, batch, high, free_high),
+			   pcp, pindex);
+	if (test_bit(ZONE_BELOW_HIGH, &zone->flags) &&
+	    zone_watermark_ok(zone, 0, high_wmark_pages(zone),
+			      ZONE_MOVABLE, 0)) {
+		struct pglist_data *pgdat = zone->zone_pgdat;
+		clear_bit(ZONE_BELOW_HIGH, &zone->flags);
+
+		/*
+		 * Assume that memory pressure on this node is gone and may be
+		 * in a reclaimable state. If a memory fallback node exists,
+		 * direct reclaim may not have been triggered, causing a
+		 * 'hopeless node' to stay in that state for a while.  Let
+		 * kswapd work again by resetting kswapd_failures.
+		 */
+		if (atomic_read(&pgdat->kswapd_failures) >= MAX_RECLAIM_RETRIES &&
+		    next_memory_node(pgdat->node_id) < MAX_NUMNODES)
+			atomic_set(&pgdat->kswapd_failures, 0);
 	}
 }
 
mm/show_mem.c renamed from mm/show_mem.c~mm-re-enable-kswapd-when-memory-pressure-subsides-or-demotion-is-toggled-fix +2 -2
@@ -278,8 +278,8 @@ static void show_free_areas(unsigned int
 #endif
 			K(node_page_state(pgdat, NR_PAGETABLE)),
 			K(node_page_state(pgdat, NR_SECONDARY_PAGETABLE)),
-			str_yes_no(atomic_read(&pgdat->kswapd_failures)
-				   >= MAX_RECLAIM_RETRIES),
+			str_yes_no(atomic_read(&pgdat->kswapd_failures) >=
+				   MAX_RECLAIM_RETRIES),
 			K(node_page_state(pgdat, NR_BALLOON_PAGES)));
 	}
 

_

#2 lore raw
Quoted 17 lines · Andrew Morton
Quoted 15 lines · On Mon, 8 Sep 2025 19:04:10 +0900 Chanwon Park <[email protected]> wrote:

If kswapd fails to reclaim pages from a node MAX_RECLAIM_RETRIES in a row, kswapd on that node gets disabled. That is, the system won't wakeup kswapd for that node until page reclamation is observed at least once. That reclamation is mostly done by direct reclaim, which in turn enables kswapd back.

However, on systems with CXL memory nodes, workloads with high anon page usage can disable kswapd indefinitely, without triggering direct reclaim. This can be reproduced with following steps:

numa node 0 (32GB memory, 48 CPUs)
numa node 2~5 (512GB CXL memory, 128GB each)
(numa node 1 is disabled)
swap space 8GB

Quoted 8 lines
Quoted 5 lines

1) Set /sys/kernel/mm/demotion_enabled to 0.
2) Set /proc/sys/kernel/numa_balancing to 0.
3) Run a process that allocates and random accesses 500GB of anon pages.
4) Let the process exit normally.

hm, OK, I guess this is longstanding misbehavior?

Yes, unless there's any application forced to allocate pages on node 0 running, kswapd stays disabled until reboot.

Quoted 8 lines
Quoted 3 lines

Since kswapd_failures resets may be missed by ++ operation, it is changed from int to atomic_t.

Possibly this should have been a separate (earlier) patch. But I assume the need for this conversion was inroduced by this patch, so it's debatable.

May be I should've done that, but I wasn't sure if it was the right thing to do... It seemed that atomic_t was not needed before, and changing the type alone meant it just adds overhead without any gain (for that patch). But I also think splitting them is a logical thing to do. Should I split and reupload the patch (with changes you made)?

Quoted 6 lines
Quoted 6 lines
include/linux/mmzone.h +0 -0
@@ -1411,7 +1411,7 @@ typedef struct pglist_data {
 	int kswapd_order;
 	enum zone_type kswapd_highest_zoneidx;
 
Quoted 6 lines

include/linux/mmzone.h typedef struct pglist_data { · line 1414

Quoted 2 lines
include/linux/mmzone.h +1 -1
-	int kswapd_failures;		/* Number of 'reclaimed == 0' runs */
+	atomic_t kswapd_failures;	/* Number of 'reclaimed == 0' runs */

This caused a number of 80-column horrors! I had a fiddle, what do you think?

The changes you made look good to me! Sorry for the noise.

Sorry, my previous reply missed the mailing lists. Resending with proper Cc.

--
Best regards,
Chanwon Park

Quoted 68 lines
mm/page_alloc.c renamed from mm/page_alloc.c~mm-re-enable-kswapd-when-memory-pressure-subsides-or-demotion-is-toggled-fix +21 -21
@@ -2860,29 +2860,29 @@ static void free_frozen_page_commit(stru
 		 */
 		return;
 	}
+
 	high = nr_pcp_high(pcp, zone, batch, free_high);
-	if (pcp->count >= high) {
-		free_pcppages_bulk(zone, nr_pcp_free(pcp, batch, high, free_high),
-				   pcp, pindex);
-		if (test_bit(ZONE_BELOW_HIGH, &zone->flags) &&
-		    zone_watermark_ok(zone, 0, high_wmark_pages(zone),
-				      ZONE_MOVABLE, 0)) {
-			struct pglist_data *pgdat = zone->zone_pgdat;
-			clear_bit(ZONE_BELOW_HIGH, &zone->flags);
+	if (pcp->count < high)
+		return;
 
-			/*
-			 * Assume that memory pressure on this node is gone
-			 * and may be in a reclaimable state. If a memory
-			 * fallback node exists, direct reclaim may not have
-			 * been triggered, leaving 'hopeless node' stay in
-			 * that state for a while. Let kswapd work again by
-			 * resetting kswapd_failures.
-			 */
-			if (atomic_read(&pgdat->kswapd_failures)
-			    >= MAX_RECLAIM_RETRIES &&
-			    next_memory_node(pgdat->node_id) < MAX_NUMNODES)
-				atomic_set(&pgdat->kswapd_failures, 0);
-		}
+	free_pcppages_bulk(zone, nr_pcp_free(pcp, batch, high, free_high),
+			   pcp, pindex);
+	if (test_bit(ZONE_BELOW_HIGH, &zone->flags) &&
+	    zone_watermark_ok(zone, 0, high_wmark_pages(zone),
+			      ZONE_MOVABLE, 0)) {
+		struct pglist_data *pgdat = zone->zone_pgdat;
+		clear_bit(ZONE_BELOW_HIGH, &zone->flags);
+
+		/*
+		 * Assume that memory pressure on this node is gone and may be
+		 * in a reclaimable state. If a memory fallback node exists,
+		 * direct reclaim may not have been triggered, causing a
+		 * 'hopeless node' to stay in that state for a while.  Let
+		 * kswapd work again by resetting kswapd_failures.
+		 */
+		if (atomic_read(&pgdat->kswapd_failures) >= MAX_RECLAIM_RETRIES &&
+		    next_memory_node(pgdat->node_id) < MAX_NUMNODES)
+			atomic_set(&pgdat->kswapd_failures, 0);
 	}
 }
 
mm/show_mem.c renamed from mm/show_mem.c~mm-re-enable-kswapd-when-memory-pressure-subsides-or-demotion-is-toggled-fix +2 -2
@@ -278,8 +278,8 @@ static void show_free_areas(unsigned int
 #endif
 			K(node_page_state(pgdat, NR_PAGETABLE)),
 			K(node_page_state(pgdat, NR_SECONDARY_PAGETABLE)),
-			str_yes_no(atomic_read(&pgdat->kswapd_failures)
-				   >= MAX_RECLAIM_RETRIES),
+			str_yes_no(atomic_read(&pgdat->kswapd_failures) >=
+				   MAX_RECLAIM_RETRIES),
 			K(node_page_state(pgdat, NR_BALLOON_PAGES)));
 	}
 

_

Acked-by on v1 1/1
#3 lore raw
In reply to Chanwon Park
Quoted 50 lines · Chanwon Park

If kswapd fails to reclaim pages from a node MAX_RECLAIM_RETRIES in a row, kswapd on that node gets disabled. That is, the system won't wakeup kswapd for that node until page reclamation is observed at least once. That reclamation is mostly done by direct reclaim, which in turn enables kswapd back.

However, on systems with CXL memory nodes, workloads with high anon page usage can disable kswapd indefinitely, without triggering direct reclaim. This can be reproduced with following steps:

numa node 0 (32GB memory, 48 CPUs)
numa node 2~5 (512GB CXL memory, 128GB each)
(numa node 1 is disabled)
swap space 8GB

1) Set /sys/kernel/mm/demotion_enabled to 0.
2) Set /proc/sys/kernel/numa_balancing to 0.
3) Run a process that allocates and random accesses 500GB of anon pages.
4) Let the process exit normally.

During 3), free memory on node 0 gets lower than low watermark, and kswapd runs and depletes swap space. Then, kswapd fails consecutively and gets disabled. Allocation afterwards happens on CXL memory, so node 0 never gains more memory pressure to trigger direct reclaim.

After 4), kswapd on node 0 remains disabled, and tasks running on that node are unable to swap. If you turn on NUMA_BALANCING_MEMORY_TIERING and demotion now, it won't work properly since kswapd is disabled.

To mitigate this problem, reset kswapd_failures to 0 on following conditions:

a) ZONE_BELOW_HIGH bit of a zone in hopeless node with a fallback memory node gets cleared.
b) demotion_enabled is changed from false to true.

Rationale for a):
ZONE_BELOW_HIGH bit being cleared might be a sign that the node may be reclaimable afterwards. This won't help much if the memory-hungry process keeps running without freeing anything, but at least the node will go back to reclaimable state when the process exits.

Rationale for b):
When demotion_enabled is false, kswapd can only reclaim anon pages by swapping them out to swap space. If demotion_enabled is turned on, kswapd can demote anon pages to another node for reclaiming. So, the original failure count for determining reclaimability is no longer valid.

Quoted 4 lines

Since kswapd_failures resets may be missed by ++ operation, it is changed from int to atomic_t.