Skip to contentSkip to conversation

mm/page_alloc: simplify lowmem_reserve max calculation

PATCH Superseded Ye Liu 3 participants 7 messages Aug 14, 2025 – Aug 15, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ye Liu

author
#0 lore raw

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

From: Ye Liu <[email protected]>

Use max() macro to simplify the calculation of maximum lowmem_reserve value in calculate_totalreserve_pages(), instead of open-coding the comparison. The functionality remains identical.

---
 mm/page_alloc.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/page_alloc.c +1 -2
index 64872214bc7d..8a55a4951d19 100644
@@ -6236,8 +6236,7 @@ static void calculate_totalreserve_pages(void)
 
 			/* 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];
+				max = max(max, zone->lowmem_reserve[j]);
 			}
 
 			/* we treat the high watermark as reserved pages. */
Signature · 1 line
2.43.0
author
#2 lore raw
Quoted 10 lines · Johannes Weiner
Quoted 7 lines · Ye Liu

From: Ye Liu <[email protected]>

Use max() macro to simplify the calculation of maximum lowmem_reserve value in calculate_totalreserve_pages(), instead of open-coding the comparison. The functionality remains identical.

You can remove the {} from the for block as well now.

I agree to delete it, but I noticed a patch has been added to the -mm mm-new branch by Andrew Morton[1]. What should I do now? Send v2 ?
Or send a new patch? If this is my first time, please feel free to give me some advice. [1]:https://lore.kernel.org/all/[email protected]/

Quoted 2 lines
Signature · 2 lines
Thanks,
Ye Liu
reviewer
#3 lore raw
Quoted 8 lines · Ye Liu
Quoted 7 lines · Johannes Weiner
Quoted 6 lines · Ye Liu

From: Ye Liu <[email protected]>

Use max() macro to simplify the calculation of maximum lowmem_reserve value in calculate_totalreserve_pages(), instead of open-coding the comparison. The functionality remains identical.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

You can remove the {} from the for block as well now.

I agree to delete it, but I noticed a patch has been added to the -mm mm-new branch by Andrew Morton[1]. What should I do now? Send v2 ?
Or send a new patch? If this is my first time, please feel free to give me some advice. [1]:https://lore.kernel.org/all/[email protected]/

If you read the message carefully, you will see:

“Please do not hesitate to respond to
review feedback and post updated versions to replace or incrementally fixup patches in mm-new.”

So feel free to send v2.

Best Regards,
Yan, Zi

reviewer
#4 lore raw
In reply to Ye Liu
Quoted 16 lines · Ye Liu

From: Ye Liu <[email protected]>

Use max() macro to simplify the calculation of maximum lowmem_reserve value in calculate_totalreserve_pages(), instead of open-coding the comparison. The functionality remains identical.

---
 mm/page_alloc.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/page_alloc.c +0 -0
index 64872214bc7d..8a55a4951d19 100644

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

Quoted 7 lines
mm/page_alloc.c +1 -2
@@ -6236,8 +6236,7 @@ static void calculate_totalreserve_pages(void)
 
 			/* 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];
+				max = max(max, zone->lowmem_reserve[j]);
 			}

There is a “if (max > managed_pages)” below. Maybe convert that as well?

Feel free to add Acked-by: Zi Yan <[email protected]>.

Best Regards,
Yan, Zi

author
#5 lore raw
Quoted 19 lines · Zi Yan
Quoted 17 lines · Ye Liu

From: Ye Liu <[email protected]>

Use max() macro to simplify the calculation of maximum lowmem_reserve value in calculate_totalreserve_pages(), instead of open-coding the comparison. The functionality remains identical.

---
 mm/page_alloc.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/page_alloc.c +0 -0
index 64872214bc7d..8a55a4951d19 100644
@@ -6236,8 +6236,7 @@ static void calculate_totalreserve_pages(void)
 
Quoted 8 lines

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

Quoted 6 lines
mm/page_alloc.c +1 -2
 			/* 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];
+				max = max(max, zone->lowmem_reserve[j]);
 			}

There is a “if (max > managed_pages)” below. Maybe convert that as well?

I should use min() here, but I noticed the two variables have different types:
one is 'long' and the other is 'unsigned long'. So, I should use min_t(). But then again, why is lowmem_reserve of type 'long'?
It should be a non-negative number, right?
Is it possible to change the type of lowmem_reserve to 'unsigned long' and change all uses of it at the same time? Is it necessary?

Quoted 5 lines

Feel free to add Acked-by: Zi Yan <[email protected]>.

Best Regards,
Yan, Zi

Signature · 2 lines
Thanks,
Ye Liu
reviewer
#6 lore raw
Quoted 29 lines · Ye Liu
Quoted 19 lines · Zi Yan
Quoted 17 lines · Ye Liu

From: Ye Liu <[email protected]>

Use max() macro to simplify the calculation of maximum lowmem_reserve value in calculate_totalreserve_pages(), instead of open-coding the comparison. The functionality remains identical.

---
 mm/page_alloc.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/page_alloc.c +0 -0
index 64872214bc7d..8a55a4951d19 100644
@@ -6236,8 +6236,7 @@ static void calculate_totalreserve_pages(void)
 
Quoted 8 lines

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

Quoted 6 lines
mm/page_alloc.c +1 -2
 			/* 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];
+				max = max(max, zone->lowmem_reserve[j]);
 			}

There is a “if (max > managed_pages)” below. Maybe convert that as well?

Quoted 3 lines

I should use min() here, but I noticed the two variables have different types:
one is 'long' and the other is 'unsigned long'. So, I should use min_t(). But then again, why is lowmem_reserve of type 'long'?

Based on commit 3484b2de9499 ("mm: rearrange zone fields into read-only, page alloc, statistics and page reclaim lines”), lowmem_reserve type is changed due to the watermark calculation. Looking at current __zone_watermark_ok(), the comment above free_pages -= ... says free_pages may go negative and later free_pages is compared with lowmem_reserve.

Quoted 3 lines

It should be a non-negative number, right?
Is it possible to change the type of lowmem_reserve to 'unsigned long' and change all uses of it at the same time? Is it necessary?

In calculate_totalreserve_pages(), max cannot be negative, so you probably can use min_t(unsigned long, max, managed_pages) and without changing lowmem_reserve type.

Quoted 10 lines
Quoted 6 lines

Feel free to add Acked-by: Zi Yan <[email protected]>.

Best Regards,
Yan, Zi

Signature · 2 lines
Thanks,
Ye Liu

Best Regards,
Yan, Zi