Skip to contentSkip to conversation

mm/page_alloc: get_page_from_freelist cleanups

PATCH Superseded Joshua Hahn 3 participants 13 messages Nov 16 – Nov 18

Subsystems (5)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover mm/page_alloc: get_page_from_freelist cleanups

v1 --> v2:
==========
- Typo fix (s/patern/pattern) as pointed out by SJ Park
- Wordsmithing kswapd waitqueue condition as pointed out by Chris Down
- Collect signatures
- No code changes

3 cleanups for vmscan and page_alloc code. The primary goal is to make get_page_from_freelist a bit easier to read, especially after the new kswapd zone skip conditions [1].

1/3 is just a nice cleanup that I found along the way.

Based on the latest page_alloc/for-next branch as of 11/13/2026. Notably, there is a dependency on the aforementioned kswapd zone skip, 677dfdfad244a "mm/page_alloc: only skip kswapd nodes below their kswapd watermark".

No functional changes intended.

[1] https://lore.kernel.org/all/CAN+CAwNsqWpPoag_XE6bxbrfi6m71xw1NS_wc3h_ShZjNspSjg@mail.gmail.com/

Joshua Hahn (3):
mm/page_alloc: drop unused gfp_mask from zone_watermark_fast() mm/page_alloc: move the kswapd node skip check into a helper mm/vmscan: consolidate promo vs. high watermark checks

 mm/internal.h   |  1 +
 mm/page_alloc.c | 55 +++++++++++++++++++++++++++----------------------
 mm/vmscan.c     | 22 ++++++++++++--------
 3 files changed, 44 insertions(+), 34 deletions(-)

base-commit: 3a99594e62fce8c646e8aac01d38af16d6833a40

Signature · 1 line
2.53.1-Meta
#1 lore raw

PATCH v2 1/3 mm/page_alloc: drop unused gfp_mask from zone_watermark_fast()

zone_watermark_fast() has not used its gfp_mask argument since commit 2973d8229b78 ("mm: discard __GFP_ATOMIC") switched the watermark boost check from __GFP_ATOMIC to ALLOC_MIN_RESERVE.

Remove the argument, no functional changes intended.

---
 mm/page_alloc.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
mm/page_alloc.c +5 -7
index 9e1261521b6a3..4d829e5eac49d 100644
@@ -3675,7 +3675,7 @@ bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
 
 static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
 				unsigned long mark, int highest_zoneidx,
-				unsigned int alloc_flags, gfp_t gfp_mask)
+				unsigned int alloc_flags)
 {
 	long free_pages;
 
@@ -3877,7 +3877,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 		    !waitqueue_active(&zone->zone_pgdat->kswapd_wait)) {
 			mark = wmark_pages(zone, kswapd_watermark);
 			if (!zone_watermark_fast(zone, order, mark,
-					ac->highest_zoneidx, 0, gfp_mask)) {
+					ac->highest_zoneidx, 0)) {
 				skipped_kswapd_nodes = true;
 				continue;
 			}
@@ -3897,8 +3897,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 
 		mark = high_wmark_pages(zone);
 		if (zone_watermark_fast(zone, order, mark,
-					ac->highest_zoneidx, alloc_flags,
-					gfp_mask))
+					ac->highest_zoneidx, alloc_flags))
 			goto try_this_zone;
 		else
 			set_bit(ZONE_BELOW_HIGH, &zone->flags);
@@ -3906,8 +3905,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 check_alloc_wmark:
 		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
 		if (!zone_watermark_fast(zone, order, mark,
-				       ac->highest_zoneidx, alloc_flags,
-				       gfp_mask)) {
+				       ac->highest_zoneidx, alloc_flags)) {
 			if (cond_accept_memory(zone, order, alloc_flags))
 				goto try_this_zone;
 
@@ -5245,7 +5243,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages - nr_populated;
 		if (zone_watermark_fast(zone, 0,  mark,
 				zonelist_zone_idx(ac.preferred_zoneref),
-				alloc_flags, gfp)) {
+				alloc_flags)) {
 			break;
 		}
 
Signature · 1 line
2.53.1-Meta
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] mm/page_alloc: drop unused gfp_mask from zone_watermark_fast()

Quoted 10 lines · Joshua Hahn

zone_watermark_fast() has not used its gfp_mask argument since commit 2973d8229b78 ("mm: discard __GFP_ATOMIC") switched the watermark boost check from __GFP_ATOMIC to ALLOC_MIN_RESERVE.

Remove the argument, no functional changes intended.

Signature · 1 line
~Gregory
#3 lore raw

Re: [PATCH v2 1/3] mm/page_alloc: drop unused gfp_mask from zone_watermark_fast()

In reply to Joshua Hahn
Quoted 11 lines · Joshua Hahn

zone_watermark_fast() has not used its gfp_mask argument since commit 2973d8229b78 ("mm: discard __GFP_ATOMIC") switched the watermark boost check from __GFP_ATOMIC to ALLOC_MIN_RESERVE.

Remove the argument, no functional changes intended.

---

Note that this series:
https://lore.kernel.org/all/[email protected]/

needs to use the gfp mask. So perhaps drop this patch? Otherwise we will just make noise on the list by adding it back :)

Quoted 54 lines
 mm/page_alloc.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
mm/page_alloc.c +5 -7
index 9e1261521b6a3..4d829e5eac49d 100644
@@ -3675,7 +3675,7 @@ bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
 
 static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
 				unsigned long mark, int highest_zoneidx,
-				unsigned int alloc_flags, gfp_t gfp_mask)
+				unsigned int alloc_flags)
 {
 	long free_pages;
 
@@ -3877,7 +3877,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 		    !waitqueue_active(&zone->zone_pgdat->kswapd_wait)) {
 			mark = wmark_pages(zone, kswapd_watermark);
 			if (!zone_watermark_fast(zone, order, mark,
-					ac->highest_zoneidx, 0, gfp_mask)) {
+					ac->highest_zoneidx, 0)) {
 				skipped_kswapd_nodes = true;
 				continue;
 			}
@@ -3897,8 +3897,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 
 		mark = high_wmark_pages(zone);
 		if (zone_watermark_fast(zone, order, mark,
-					ac->highest_zoneidx, alloc_flags,
-					gfp_mask))
+					ac->highest_zoneidx, alloc_flags))
 			goto try_this_zone;
 		else
 			set_bit(ZONE_BELOW_HIGH, &zone->flags);
@@ -3906,8 +3905,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 check_alloc_wmark:
 		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
 		if (!zone_watermark_fast(zone, order, mark,
-				       ac->highest_zoneidx, alloc_flags,
-				       gfp_mask)) {
+				       ac->highest_zoneidx, alloc_flags)) {
 			if (cond_accept_memory(zone, order, alloc_flags))
 				goto try_this_zone;
 
@@ -5245,7 +5243,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages - nr_populated;
 		if (zone_watermark_fast(zone, 0,  mark,
 				zonelist_zone_idx(ac.preferred_zoneref),
-				alloc_flags, gfp)) {
+				alloc_flags)) {
 			break;
 		}
 
#4 lore raw

Re: [PATCH v2 1/3] mm/page_alloc: drop unused gfp_mask from zone_watermark_fast()

+ Kiryl if he has different opinions

Quoted 77 lines · Dev Jain
Quoted 11 lines · Joshua Hahn

zone_watermark_fast() has not used its gfp_mask argument since commit 2973d8229b78 ("mm: discard __GFP_ATOMIC") switched the watermark boost check from __GFP_ATOMIC to ALLOC_MIN_RESERVE.

Remove the argument, no functional changes intended.

---

Note that this series:
https://lore.kernel.org/all/[email protected]/

needs to use the gfp mask. So perhaps drop this patch? Otherwise we will just make noise on the list by adding it back :)

Quoted 54 lines
 mm/page_alloc.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
mm/page_alloc.c +5 -7
index 9e1261521b6a3..4d829e5eac49d 100644
@@ -3675,7 +3675,7 @@ bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
 
 static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
 				unsigned long mark, int highest_zoneidx,
-				unsigned int alloc_flags, gfp_t gfp_mask)
+				unsigned int alloc_flags)
 {
 	long free_pages;
 
@@ -3877,7 +3877,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 		    !waitqueue_active(&zone->zone_pgdat->kswapd_wait)) {
 			mark = wmark_pages(zone, kswapd_watermark);
 			if (!zone_watermark_fast(zone, order, mark,
-					ac->highest_zoneidx, 0, gfp_mask)) {
+					ac->highest_zoneidx, 0)) {
 				skipped_kswapd_nodes = true;
 				continue;
 			}
@@ -3897,8 +3897,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 
 		mark = high_wmark_pages(zone);
 		if (zone_watermark_fast(zone, order, mark,
-					ac->highest_zoneidx, alloc_flags,
-					gfp_mask))
+					ac->highest_zoneidx, alloc_flags))
 			goto try_this_zone;
 		else
 			set_bit(ZONE_BELOW_HIGH, &zone->flags);
@@ -3906,8 +3905,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 check_alloc_wmark:
 		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
 		if (!zone_watermark_fast(zone, order, mark,
-				       ac->highest_zoneidx, alloc_flags,
-				       gfp_mask)) {
+				       ac->highest_zoneidx, alloc_flags)) {
 			if (cond_accept_memory(zone, order, alloc_flags))
 				goto try_this_zone;
 
@@ -5245,7 +5243,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages - nr_populated;
 		if (zone_watermark_fast(zone, 0,  mark,
 				zonelist_zone_idx(ac.preferred_zoneref),
-				alloc_flags, gfp)) {
+				alloc_flags)) {
 			break;
 		}
 
#5 lore raw

Re: [PATCH v2 1/3] mm/page_alloc: drop unused gfp_mask from zone_watermark_fast()

Quoted 23 lines · Dev Jain

+ Kiryl if he has different opinions

Quoted 20 lines · Dev Jain
Quoted 11 lines · Joshua Hahn

zone_watermark_fast() has not used its gfp_mask argument since commit 2973d8229b78 ("mm: discard __GFP_ATOMIC") switched the watermark boost check from __GFP_ATOMIC to ALLOC_MIN_RESERVE.

Remove the argument, no functional changes intended.

---

Note that this series:
https://lore.kernel.org/all/[email protected]/

needs to use the gfp mask. So perhaps drop this patch? Otherwise we will just make noise on the list by adding it back :)

Hi Dev,

Thank you for your review on the series. Also, this was a really good catch, thanks a lot! I'll just drop this as it was just meant to be a cleanup if there was no new user, but it seems like there will be a real usecase.

Have a great day!
Joshua

Quoted 56 lines
Quoted 55 lines
Quoted 54 lines
 mm/page_alloc.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
mm/page_alloc.c +5 -7
index 9e1261521b6a3..4d829e5eac49d 100644
@@ -3675,7 +3675,7 @@ bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
 
 static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
 				unsigned long mark, int highest_zoneidx,
-				unsigned int alloc_flags, gfp_t gfp_mask)
+				unsigned int alloc_flags)
 {
 	long free_pages;
 
@@ -3877,7 +3877,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 		    !waitqueue_active(&zone->zone_pgdat->kswapd_wait)) {
 			mark = wmark_pages(zone, kswapd_watermark);
 			if (!zone_watermark_fast(zone, order, mark,
-					ac->highest_zoneidx, 0, gfp_mask)) {
+					ac->highest_zoneidx, 0)) {
 				skipped_kswapd_nodes = true;
 				continue;
 			}
@@ -3897,8 +3897,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 
 		mark = high_wmark_pages(zone);
 		if (zone_watermark_fast(zone, order, mark,
-					ac->highest_zoneidx, alloc_flags,
-					gfp_mask))
+					ac->highest_zoneidx, alloc_flags))
 			goto try_this_zone;
 		else
 			set_bit(ZONE_BELOW_HIGH, &zone->flags);
@@ -3906,8 +3905,7 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 check_alloc_wmark:
 		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
 		if (!zone_watermark_fast(zone, order, mark,
-				       ac->highest_zoneidx, alloc_flags,
-				       gfp_mask)) {
+				       ac->highest_zoneidx, alloc_flags)) {
 			if (cond_accept_memory(zone, order, alloc_flags))
 				goto try_this_zone;
 
@@ -5245,7 +5243,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages - nr_populated;
 		if (zone_watermark_fast(zone, 0,  mark,
 				zonelist_zone_idx(ac.preferred_zoneref),
-				alloc_flags, gfp)) {
+				alloc_flags)) {
 			break;
 		}
 
#6 lore raw

PATCH v2 2/3 mm/page_alloc: move the kswapd node skip check into a helper

In reply to Joshua Hahn

get_page_from_freelist() checks many conditions for each zone, often with a big comment block explaining the conditions.

Move the kswapd check and its comment into should_skip_kswapd_zone() to make the zone iteration cleaner to read.

While moving this, also resolve a checkpatch warning which complains of a waitqueue_active() call with no corresponding comment explaining why the lockless use is OK in this case.

No functional change intended.

---
 mm/page_alloc.c | 47 +++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)
mm/page_alloc.c +29 -18
index 4d829e5eac49d..43929faffb989 100644
@@ -3785,6 +3785,32 @@ static inline unsigned int alloc_flags_cma(gfp_t gfp_mask)
 	return ALLOC_DEFAULT;
 }
 
+/*
+ * If kswapd is already active on a node and the zone is below its kswapd
+ * watermark for this request, keep looking for other nodes that might be idle.
+ * This can happen if another process has NUMA bindings and is causing kswapd
+ * wakeups on only some nodes. Avoid accidental "node_reclaim_mode"-like
+ * behavior in this case.
+ */
+static bool should_skip_kswapd_zone(struct zone *zone, unsigned int order,
+				    const struct alloc_context *ac)
+{
+	unsigned long mark;
+
+	/*
+	 * This means kswapd is asleep, so don't skip the zone. This is a
+	 * lockless check, since a stale result only affects zone selection.
+	 */
+	if (waitqueue_active(&zone->zone_pgdat->kswapd_wait))
+		return false;
+
+	if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
+		mark = promo_wmark_pages(zone);
+	else
+		mark = high_wmark_pages(zone);
+	return !zone_watermark_fast(zone, order, mark, ac->highest_zoneidx, 0);
+}
+
 /*
  * get_page_from_freelist goes through the zonelist trying to allocate
  * a page.
@@ -3796,9 +3822,6 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 	struct zoneref *z;
 	struct zone *zone;
 	struct pglist_data *last_pgdat = NULL;
-	enum zone_watermarks kswapd_watermark =
-		sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING ?
-		WMARK_PROMO : WMARK_HIGH;
 	bool last_pgdat_dirty_ok = false;
 	bool no_fallback;
 	bool skip_kswapd_nodes = nr_online_nodes > 1;
@@ -3865,22 +3888,10 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 			}
 		}
 
-		/*
-		 * If kswapd is already active on a node and the zone is below
-		 * its kswapd watermark for this request, keep looking for
-		 * other nodes that might be idle. This can happen if another
-		 * process has NUMA bindings and is causing kswapd wakeups on
-		 * only some nodes. Avoid accidental "node_reclaim_mode"-like
-		 * behavior in this case.
-		 */
 		if (skip_kswapd_nodes &&
-		    !waitqueue_active(&zone->zone_pgdat->kswapd_wait)) {
-			mark = wmark_pages(zone, kswapd_watermark);
-			if (!zone_watermark_fast(zone, order, mark,
-					ac->highest_zoneidx, 0)) {
-				skipped_kswapd_nodes = true;
-				continue;
-			}
+		    should_skip_kswapd_zone(zone, order, ac)) {
+			skipped_kswapd_nodes = true;
+			continue;
 		}
 
 		cond_accept_memory(zone, order, alloc_flags);
Signature · 1 line
2.53.1-Meta
Reviewed-by on v2 2/3
#7 lore raw

Re: [PATCH v2 2/3] mm/page_alloc: move the kswapd node skip check into a helper

Quoted 10 lines · Joshua Hahn

get_page_from_freelist() checks many conditions for each zone, often with a big comment block explaining the conditions.

Move the kswapd check and its comment into should_skip_kswapd_zone() to make the zone iteration cleaner to read.

While moving this, also resolve a checkpatch warning which complains of a waitqueue_active() call with no corresponding comment explaining why the lockless use is OK in this case.

Quoted 7 lines

No functional change intended.

Signature · 1 line
~Gregory
Reviewed-by on v2 2/3
#8 lore raw

Re: [PATCH v2 2/3] mm/page_alloc: move the kswapd node skip check into a helper

In reply to Joshua Hahn
Quoted 10 lines · Joshua Hahn

get_page_from_freelist() checks many conditions for each zone, often with a big comment block explaining the conditions.

Move the kswapd check and its comment into should_skip_kswapd_zone() to make the zone iteration cleaner to read.

While moving this, also resolve a checkpatch warning which complains of a waitqueue_active() call with no corresponding comment explaining why the lockless use is OK in this case.

Quoted 8 lines

No functional change intended.

---

Thanks looks better.

But see below.

Quoted 10 lines
 mm/page_alloc.c | 47 +++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)
mm/page_alloc.c +0 -0
index 4d829e5eac49d..43929faffb989 100644
@@ -3785,6 +3785,32 @@ static inline unsigned int alloc_flags_cma(gfp_t gfp_mask)
 	return ALLOC_DEFAULT;
 }

mm/page_alloc.c static inline unsigned int alloc_flags_cma(gfp_t gfp_mask) · line 3796

Quoted 10 lines
mm/page_alloc.c +9 -0
 
+/*
+ * If kswapd is already active on a node and the zone is below its kswapd
+ * watermark for this request, keep looking for other nodes that might be idle.
+ * This can happen if another process has NUMA bindings and is causing kswapd
+ * wakeups on only some nodes. Avoid accidental "node_reclaim_mode"-like
+ * behavior in this case.
+ */
+static bool should_skip_kswapd_zone(struct zone *zone, unsigned int order,
+				    const struct alloc_context *ac)

Make this inline? This is the very hot page allocator code that's why :) I may be overthinking but in page_alloc.c I do see functions with similar size being marked inline.

Quoted 56 lines
mm/page_alloc.c +20 -18
+{
+	unsigned long mark;
+
+	/*
+	 * This means kswapd is asleep, so don't skip the zone. This is a
+	 * lockless check, since a stale result only affects zone selection.
+	 */
+	if (waitqueue_active(&zone->zone_pgdat->kswapd_wait))
+		return false;
+
+	if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
+		mark = promo_wmark_pages(zone);
+	else
+		mark = high_wmark_pages(zone);
+	return !zone_watermark_fast(zone, order, mark, ac->highest_zoneidx, 0);
+}
+
 /*
  * get_page_from_freelist goes through the zonelist trying to allocate
  * a page.
@@ -3796,9 +3822,6 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 	struct zoneref *z;
 	struct zone *zone;
 	struct pglist_data *last_pgdat = NULL;
-	enum zone_watermarks kswapd_watermark =
-		sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING ?
-		WMARK_PROMO : WMARK_HIGH;
 	bool last_pgdat_dirty_ok = false;
 	bool no_fallback;
 	bool skip_kswapd_nodes = nr_online_nodes > 1;
@@ -3865,22 +3888,10 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 			}
 		}
 
-		/*
-		 * If kswapd is already active on a node and the zone is below
-		 * its kswapd watermark for this request, keep looking for
-		 * other nodes that might be idle. This can happen if another
-		 * process has NUMA bindings and is causing kswapd wakeups on
-		 * only some nodes. Avoid accidental "node_reclaim_mode"-like
-		 * behavior in this case.
-		 */
 		if (skip_kswapd_nodes &&
-		    !waitqueue_active(&zone->zone_pgdat->kswapd_wait)) {
-			mark = wmark_pages(zone, kswapd_watermark);
-			if (!zone_watermark_fast(zone, order, mark,
-					ac->highest_zoneidx, 0)) {
-				skipped_kswapd_nodes = true;
-				continue;
-			}
+		    should_skip_kswapd_zone(zone, order, ac)) {
+			skipped_kswapd_nodes = true;
+			continue;
 		}
 
 		cond_accept_memory(zone, order, alloc_flags);
#9 lore raw

Re: [PATCH v2 2/3] mm/page_alloc: move the kswapd node skip check into a helper

Quoted 40 lines · Dev Jain
Quoted 10 lines · Joshua Hahn

get_page_from_freelist() checks many conditions for each zone, often with a big comment block explaining the conditions.

Move the kswapd check and its comment into should_skip_kswapd_zone() to make the zone iteration cleaner to read.

While moving this, also resolve a checkpatch warning which complains of a waitqueue_active() call with no corresponding comment explaining why the lockless use is OK in this case.

Quoted 8 lines

No functional change intended.

---

Thanks looks better.

But see below.

Quoted 11 lines
 mm/page_alloc.c | 47 +++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)
mm/page_alloc.c +0 -0
index 4d829e5eac49d..43929faffb989 100644
@@ -3785,6 +3785,32 @@ static inline unsigned int alloc_flags_cma(gfp_t gfp_mask)
 	return ALLOC_DEFAULT;
 }
 
Quoted 13 lines

mm/page_alloc.c static inline unsigned int alloc_flags_cma(gfp_t gfp_mask) · line 3796

Quoted 9 lines
mm/page_alloc.c +9 -0
+/*
+ * If kswapd is already active on a node and the zone is below its kswapd
+ * watermark for this request, keep looking for other nodes that might be idle.
+ * This can happen if another process has NUMA bindings and is causing kswapd
+ * wakeups on only some nodes. Avoid accidental "node_reclaim_mode"-like
+ * behavior in this case.
+ */
+static bool should_skip_kswapd_zone(struct zone *zone, unsigned int order,
+				    const struct alloc_context *ac)

Make this inline? This is the very hot page allocator code that's why :) I may be overthinking but in page_alloc.c I do see functions with similar size being marked inline.

Very reasonable! I think it's just a few checks and bitwise comparisons at most (apart from the zone watermark checks). I'll make that change.

Thanks Dev!
Joshua

Quoted 57 lines
Quoted 56 lines
mm/page_alloc.c +20 -18
+{
+	unsigned long mark;
+
+	/*
+	 * This means kswapd is asleep, so don't skip the zone. This is a
+	 * lockless check, since a stale result only affects zone selection.
+	 */
+	if (waitqueue_active(&zone->zone_pgdat->kswapd_wait))
+		return false;
+
+	if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
+		mark = promo_wmark_pages(zone);
+	else
+		mark = high_wmark_pages(zone);
+	return !zone_watermark_fast(zone, order, mark, ac->highest_zoneidx, 0);
+}
+
 /*
  * get_page_from_freelist goes through the zonelist trying to allocate
  * a page.
@@ -3796,9 +3822,6 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 	struct zoneref *z;
 	struct zone *zone;
 	struct pglist_data *last_pgdat = NULL;
-	enum zone_watermarks kswapd_watermark =
-		sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING ?
-		WMARK_PROMO : WMARK_HIGH;
 	bool last_pgdat_dirty_ok = false;
 	bool no_fallback;
 	bool skip_kswapd_nodes = nr_online_nodes > 1;
@@ -3865,22 +3888,10 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
 			}
 		}
 
-		/*
-		 * If kswapd is already active on a node and the zone is below
-		 * its kswapd watermark for this request, keep looking for
-		 * other nodes that might be idle. This can happen if another
-		 * process has NUMA bindings and is causing kswapd wakeups on
-		 * only some nodes. Avoid accidental "node_reclaim_mode"-like
-		 * behavior in this case.
-		 */
 		if (skip_kswapd_nodes &&
-		    !waitqueue_active(&zone->zone_pgdat->kswapd_wait)) {
-			mark = wmark_pages(zone, kswapd_watermark);
-			if (!zone_watermark_fast(zone, order, mark,
-					ac->highest_zoneidx, 0)) {
-				skipped_kswapd_nodes = true;
-				continue;
-			}
+		    should_skip_kswapd_zone(zone, order, ac)) {
+			skipped_kswapd_nodes = true;
+			continue;
 		}
 
 		cond_accept_memory(zone, order, alloc_flags);
#10 lore raw

PATCH v2 3/3 mm/vmscan: consolidate promo vs. high watermark checks

In reply to Joshua Hahn

There are three sites which all check the watermark that kswapd reclaims zones up to, which is the promo watermark when NUMA balancing memory tiering is enabled, and the high watermark otherwise.

Consolidate the pattern into a helper function. While at it, remove the include for <linux/sched/sysctl.h> in mm/page_alloc.c now that the dependency on sysctl_numa_balancing_mode has been abstracted into vmscan code.

No functional changes intended.

---
 mm/internal.h   |  1 +
 mm/page_alloc.c |  6 +-----
 mm/vmscan.c     | 22 +++++++++++++---------
 3 files changed, 15 insertions(+), 14 deletions(-)
mm/internal.h +1 -0
index 566cbd2dce485..8beb5b263ca1f 100644
@@ -68,6 +68,7 @@ unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
 				gfp_t gfp_mask, const nodemask_t *mask);
 unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru,
 			      int zone_idx);
+unsigned long kswapd_wmark_pages(const struct zone *zone);
 
 #define MEMCG_RECLAIM_MAY_SWAP (1 << 1)
 #define MEMCG_RECLAIM_PROACTIVE (1 << 2)
mm/page_alloc.c +1 -5
index 43929faffb989..d7320566a9509 100644
@@ -45,7 +45,6 @@
 #include <linux/mmu_notifier.h>
 #include <linux/migrate.h>
 #include <linux/sched/mm.h>
-#include <linux/sched/sysctl.h>
 #include <linux/page_owner.h>
 #include <linux/page_table_check.h>
 #include <linux/memcontrol.h>
@@ -3804,10 +3803,7 @@ static bool should_skip_kswapd_zone(struct zone *zone, unsigned int order,
 	if (waitqueue_active(&zone->zone_pgdat->kswapd_wait))
 		return false;
 
-	if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
-		mark = promo_wmark_pages(zone);
-	else
-		mark = high_wmark_pages(zone);
+	mark = kswapd_wmark_pages(zone);
 	return !zone_watermark_fast(zone, order, mark, ac->highest_zoneidx, 0);
 }
 
mm/vmscan.c +13 -9
index 9959ca158ce83..9af50783fc824 100644
@@ -478,6 +478,17 @@ unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone
 	return size;
 }
 
+/*
+ * The watermark kswapd reclaims zones up to. With NUMA balancing memory
+ * tiering, kswapd keeps room above the high watermark for promotions.
+ */
+unsigned long kswapd_wmark_pages(const struct zone *zone)
+{
+	if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
+		return promo_wmark_pages(zone);
+	return high_wmark_pages(zone);
+}
+
 static unsigned long drop_slab_node(int nid)
 {
 	unsigned long freed = 0;
@@ -5212,7 +5223,6 @@ static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc,
 static bool should_abort_scan(struct lruvec *lruvec, struct scan_control *sc)
 {
 	int i;
-	enum zone_watermarks mark;
 
 	if (unlikely(sc->proactive && signal_pending(current)))
 		return true;
@@ -5224,12 +5234,9 @@ static bool should_abort_scan(struct lruvec *lruvec, struct scan_control *sc)
 	if (!current_is_kswapd() || sc->order)
 		return false;
 
-	mark = sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING ?
-	       WMARK_PROMO : WMARK_HIGH;
-
 	for (i = 0; i <= sc->reclaim_idx; i++) {
 		struct zone *zone = lruvec_pgdat(lruvec)->node_zones + i;
-		unsigned long size = wmark_pages(zone, mark) + MIN_LRU_BATCH;
+		unsigned long size = kswapd_wmark_pages(zone) + MIN_LRU_BATCH;
 
 		if (managed_zone(zone) && !zone_watermark_ok(zone, 0, size, sc->reclaim_idx, 0))
 			return false;
@@ -7131,10 +7138,7 @@ static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx)
 		enum zone_stat_item item;
 		unsigned long free_pages;
 
-		if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
-			mark = promo_wmark_pages(zone);
-		else
-			mark = high_wmark_pages(zone);
+		mark = kswapd_wmark_pages(zone);
 
 		/*
 		 * In defrag_mode, watermarks must be met in whole
Signature · 1 line
2.53.1-Meta
Reviewed-by on v2 3/3
#11 lore raw

Re: [PATCH v2 3/3] mm/vmscan: consolidate promo vs. high watermark checks

Quoted 9 lines · Joshua Hahn

There are three sites which all check the watermark that kswapd reclaims zones up to, which is the promo watermark when NUMA balancing memory tiering is enabled, and the high watermark otherwise.

Consolidate the pattern into a helper function. While at it, remove the include for <linux/sched/sysctl.h> in mm/page_alloc.c now that the dependency on sysctl_numa_balancing_mode has been abstracted into vmscan code.

Quoted 7 lines

No functional changes intended.

Signature · 1 line
~Gregory
Reviewed-by on v2 3/3
#12 lore raw

Re: [PATCH v2 3/3] mm/vmscan: consolidate promo vs. high watermark checks

In reply to Joshua Hahn
Quoted 9 lines · Joshua Hahn

There are three sites which all check the watermark that kswapd reclaims zones up to, which is the promo watermark when NUMA balancing memory tiering is enabled, and the high watermark otherwise.

Consolidate the pattern into a helper function. While at it, remove the include for <linux/sched/sysctl.h> in mm/page_alloc.c now that the dependency on sysctl_numa_balancing_mode has been abstracted into vmscan code.

Quoted 8 lines

No functional changes intended.

---

Quoted 97 lines
 mm/internal.h   |  1 +
 mm/page_alloc.c |  6 +-----
 mm/vmscan.c     | 22 +++++++++++++---------
 3 files changed, 15 insertions(+), 14 deletions(-)
mm/internal.h +1 -0
index 566cbd2dce485..8beb5b263ca1f 100644
@@ -68,6 +68,7 @@ unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
 				gfp_t gfp_mask, const nodemask_t *mask);
 unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru,
 			      int zone_idx);
+unsigned long kswapd_wmark_pages(const struct zone *zone);
 
 #define MEMCG_RECLAIM_MAY_SWAP (1 << 1)
 #define MEMCG_RECLAIM_PROACTIVE (1 << 2)
mm/page_alloc.c +1 -5
index 43929faffb989..d7320566a9509 100644
@@ -45,7 +45,6 @@
 #include <linux/mmu_notifier.h>
 #include <linux/migrate.h>
 #include <linux/sched/mm.h>
-#include <linux/sched/sysctl.h>
 #include <linux/page_owner.h>
 #include <linux/page_table_check.h>
 #include <linux/memcontrol.h>
@@ -3804,10 +3803,7 @@ static bool should_skip_kswapd_zone(struct zone *zone, unsigned int order,
 	if (waitqueue_active(&zone->zone_pgdat->kswapd_wait))
 		return false;
 
-	if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
-		mark = promo_wmark_pages(zone);
-	else
-		mark = high_wmark_pages(zone);
+	mark = kswapd_wmark_pages(zone);
 	return !zone_watermark_fast(zone, order, mark, ac->highest_zoneidx, 0);
 }
 
mm/vmscan.c +13 -9
index 9959ca158ce83..9af50783fc824 100644
@@ -478,6 +478,17 @@ unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone
 	return size;
 }
 
+/*
+ * The watermark kswapd reclaims zones up to. With NUMA balancing memory
+ * tiering, kswapd keeps room above the high watermark for promotions.
+ */
+unsigned long kswapd_wmark_pages(const struct zone *zone)
+{
+	if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
+		return promo_wmark_pages(zone);
+	return high_wmark_pages(zone);
+}
+
 static unsigned long drop_slab_node(int nid)
 {
 	unsigned long freed = 0;
@@ -5212,7 +5223,6 @@ static long get_nr_to_scan(struct lruvec *lruvec, struct scan_control *sc,
 static bool should_abort_scan(struct lruvec *lruvec, struct scan_control *sc)
 {
 	int i;
-	enum zone_watermarks mark;
 
 	if (unlikely(sc->proactive && signal_pending(current)))
 		return true;
@@ -5224,12 +5234,9 @@ static bool should_abort_scan(struct lruvec *lruvec, struct scan_control *sc)
 	if (!current_is_kswapd() || sc->order)
 		return false;
 
-	mark = sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING ?
-	       WMARK_PROMO : WMARK_HIGH;
-
 	for (i = 0; i <= sc->reclaim_idx; i++) {
 		struct zone *zone = lruvec_pgdat(lruvec)->node_zones + i;
-		unsigned long size = wmark_pages(zone, mark) + MIN_LRU_BATCH;
+		unsigned long size = kswapd_wmark_pages(zone) + MIN_LRU_BATCH;
 
 		if (managed_zone(zone) && !zone_watermark_ok(zone, 0, size, sc->reclaim_idx, 0))
 			return false;
@@ -7131,10 +7138,7 @@ static bool pgdat_balanced(pg_data_t *pgdat, int order, int highest_zoneidx)
 		enum zone_stat_item item;
 		unsigned long free_pages;
 
-		if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
-			mark = promo_wmark_pages(zone);
-		else
-			mark = high_wmark_pages(zone);
+		mark = kswapd_wmark_pages(zone);
 
 		/*
 		 * In defrag_mode, watermarks must be met in whole