Skip to contentSkip to conversation

mm/page_alloc: simplify lowmem_reserve max calculation

PATCH Superseded Ye Liu 2 participants 3 messages Aug 15, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ye Liu

author
#0 lore raw

PATCH v2 1/1 mm/page_alloc: simplify lowmem_reserve max calculation

From: Ye Liu <[email protected]>

Use max() to find the maximum lowmem_reserve value and min_t() to cap it to managed_pages in calculate_totalreserve_pages(), instead of open-coding the comparisons. No functional change.

Changes in v2:
- Drop unnecessary braces
- Replace "if (max > managed_pages)" with min_t()
- Link to v1:https://lore.kernel.org/all/[email protected]/

---
 mm/page_alloc.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
mm/page_alloc.c +3 -6
index 64872214bc7d..2617fd2f4b73 100644
@@ -6235,16 +6235,13 @@ static void calculate_totalreserve_pages(void)
 			unsigned long managed_pages = zone_managed_pages(zone);
 
 			/* Find valid and maximum lowmem_reserve in the zone */
-			for (j = i; j < MAX_NR_ZONES; j++) {
-				if (zone->lowmem_reserve[j] > max)
-					max = zone->lowmem_reserve[j];
-			}
+			for (j = i; j < MAX_NR_ZONES; j++)
+				max = max(max, zone->lowmem_reserve[j]);
 
 			/* we treat the high watermark as reserved pages. */
 			max += high_wmark_pages(zone);
 
-			if (max > managed_pages)
-				max = managed_pages;
+			min_t(unsigned long, max, managed_pages);
 
 			pgdat->totalreserve_pages += max;
 
Signature · 1 line
2.43.0
reviewer
#1 lore raw
Quoted 29 lines · Ye Liu

From: Ye Liu <[email protected]>

Use max() to find the maximum lowmem_reserve value and min_t() to cap it to managed_pages in calculate_totalreserve_pages(), instead of open-coding the comparisons. No functional change.

Changes in v2:
- Drop unnecessary braces
- Replace "if (max > managed_pages)" with min_t()
- Link to v1:https://lore.kernel.org/all/[email protected]/

---
 mm/page_alloc.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
mm/page_alloc.c +0 -3
index 64872214bc7d..2617fd2f4b73 100644
@@ -6235,16 +6235,13 @@ static void calculate_totalreserve_pages(void)
 			unsigned long managed_pages = zone_managed_pages(zone);
 
 			/* Find valid and maximum lowmem_reserve in the zone */
-			for (j = i; j < MAX_NR_ZONES; j++) {
-				if (zone->lowmem_reserve[j] > max)
-					max = zone->lowmem_reserve[j];

mm/page_alloc.c static void calculate_totalreserve_pages(void) · line 6244

Quoted 10 lines
mm/page_alloc.c +3 -3
-			}
+			for (j = i; j < MAX_NR_ZONES; j++)
+				max = max(max, zone->lowmem_reserve[j]);
 
 			/* we treat the high watermark as reserved pages. */
 			max += high_wmark_pages(zone);
 
-			if (max > managed_pages)
-				max = managed_pages;
+			min_t(unsigned long, max, managed_pages);

Should be

max = min_t(unsigned long, max, managed_pages);

;)

Quoted 5 lines
mm/page_alloc.c +0 -0
 			pgdat->totalreserve_pages += max;
 
Signature · 1 line
2.43.0

Best Regards,
Yan, Zi

author
#2 lore raw
Quoted 44 lines · Zi Yan
Quoted 29 lines · Ye Liu

From: Ye Liu <[email protected]>

Use max() to find the maximum lowmem_reserve value and min_t() to cap it to managed_pages in calculate_totalreserve_pages(), instead of open-coding the comparisons. No functional change.

Changes in v2:
- Drop unnecessary braces
- Replace "if (max > managed_pages)" with min_t()
- Link to v1:https://lore.kernel.org/all/[email protected]/

---
 mm/page_alloc.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
mm/page_alloc.c +0 -3
index 64872214bc7d..2617fd2f4b73 100644
@@ -6235,16 +6235,13 @@ static void calculate_totalreserve_pages(void)
 			unsigned long managed_pages = zone_managed_pages(zone);
 
 			/* Find valid and maximum lowmem_reserve in the zone */
-			for (j = i; j < MAX_NR_ZONES; j++) {
-				if (zone->lowmem_reserve[j] > max)
-					max = zone->lowmem_reserve[j];

mm/page_alloc.c static void calculate_totalreserve_pages(void) · line 6244

Quoted 10 lines
mm/page_alloc.c +3 -3
-			}
+			for (j = i; j < MAX_NR_ZONES; j++)
+				max = max(max, zone->lowmem_reserve[j]);
 
 			/* we treat the high watermark as reserved pages. */
 			max += high_wmark_pages(zone);
 
-			if (max > managed_pages)
-				max = managed_pages;
+			min_t(unsigned long, max, managed_pages);

Should be

Quoted 3 lines

max = min_t(unsigned long, max, managed_pages);

;)

Haha I forgot to git add after I modified it ;)

Quoted 10 lines
Quoted 6 lines
mm/page_alloc.c +0 -0
 			pgdat->totalreserve_pages += max;
 
Signature · 1 line
2.43.0

Best Regards,
Yan, Zi

Signature · 2 lines
Thanks,
Ye Liu