Skip to contentSkip to conversation

mm/page_alloc: avoid overcounting bulk alloc in watermark check

PATCH Applied shengminghu512 3 participants 4 messages Jan 29 – Feb 12

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

PATCH 1/1 mm/page_alloc: avoid overcounting bulk alloc in watermark check

From: Shengming Hu <[email protected]>

alloc_pages_bulk_noprof() only fills NULL slots and already tracks how many entries are pre-populated via nr_populated.

The fast watermark check was adding nr_pages unconditionally, which can overestimate the demand. Use (nr_pages - nr_populated) instead, as an upper bound on the remaining pages this call can still allocate without scanning the whole array.

---
 mm/page_alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_alloc.c +1 -1
index 5fd9e4a03..90b978802 100644
@@ -5130,7 +5130,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 
 		cond_accept_memory(zone, 0, alloc_flags);
 retry_this_zone:
-		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages;
+		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)) {
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 9 lines · On Thu, 29 Jan 2026 22:38:14 +0800 "shengminghu512" <[email protected]>…

From: Shengming Hu <[email protected]>

alloc_pages_bulk_noprof() only fills NULL slots and already tracks how many entries are pre-populated via nr_populated.

The fast watermark check was adding nr_pages unconditionally, which can overestimate the demand. Use (nr_pages - nr_populated) instead, as an upper bound on the remaining pages this call can still allocate without scanning the whole array.

Thanks.

mm/page_alloc.c unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid, · line 5136

Quoted 11 lines
mm/page_alloc.c +1 -1
@@ -5130,7 +5130,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 
 		cond_accept_memory(zone, 0, alloc_flags);
 retry_this_zone:
-		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages;
+		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)) {

So that little optimization hasn't been working for four years?

#2 lore raw

Re: [PATCH linux-next] mm/page_alloc: avoid overcounting bulk allocin watermark check

Quoted 18 lines · On Thu, 29 Jan 2026 22:38:14 +0800 "shengminghu512" <[email protected]>…
Quoted 10 lines

From: Shengming Hu <[email protected]>

alloc_pages_bulk_noprof() only fills NULL slots and already tracks how many entries are pre-populated via nr_populated.

The fast watermark check was adding nr_pages unconditionally, which can overestimate the demand. Use (nr_pages - nr_populated) instead, as an upper bound on the remaining pages this call can still allocate without scanning the whole array.

Thanks.

Quoted 4 lines
mm/page_alloc.c +0 -0
@@ -5130,7 +5130,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 
Quoted 9 lines

mm/page_alloc.c unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid, · line 5136

Quoted 7 lines
mm/page_alloc.c +1 -1
  cond_accept_memory(zone, 0, alloc_flags);
 retry_this_zone:
- mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages;
+ 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)) {

So that little optimization hasn't been working for four years?

Yeah, looks like it’s been conservative for a long time :)

It didn’t break correctness, but it likely made the fast watermark check less effective by overestimating demand (counting already-populated entries again), so we’d drop out of zone_watermark_fast() earlier and hit the slow path more often.

--
With Best Regards,
Shengming

Reviewed-by on v1 1/1
#3 lore raw
In reply to shengminghu512
Quoted 11 lines · On 1/29/26 15:38, shengminghu512 wrote:

From: Shengming Hu <[email protected]>

alloc_pages_bulk_noprof() only fills NULL slots and already tracks how many entries are pre-populated via nr_populated.

The fast watermark check was adding nr_pages unconditionally, which can overestimate the demand. Use (nr_pages - nr_populated) instead, as an upper bound on the remaining pages this call can still allocate without scanning the whole array.

Quoted 17 lines
---
 mm/page_alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_alloc.c +1 -1
index 5fd9e4a03..90b978802 100644
@@ -5130,7 +5130,7 @@ unsigned long alloc_pages_bulk_noprof(gfp_t gfp, int preferred_nid,
 
 		cond_accept_memory(zone, 0, alloc_flags);
 retry_this_zone:
-		mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages;
+		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)) {