mm/page_alloc: simplify lowmem_reserve max calculation
Subsystems (2)
PATCH v3 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 v3:
- max = min_t(unsigned long, max, managed_pages);
- Link to v2:https://lore.kernel.org/all/[email protected]/
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
@@ -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; + max = min_t(unsigned long, max, managed_pages); pgdat->totalreserve_pages += max;
Signature · 1 line
2.43.0
Quoted 9 lines · Ye Liu
From: Ye Liu <[email protected]>
Use
max()to find the maximumlowmem_reservevalue andmin_t()to cap it tomanaged_pagesincalculate_totalreserve_pages(), instead of open-coding the comparisons. No functional change.Signed-off-by Ye Liu <[email protected]>Acked-by Johannes Weiner <[email protected]>Acked-by Zi Yan <[email protected]>
Signature · 2 lines
Wei Yang Help you, Help me
Andrew Morton
maintainerQuoted 18 lines · On Fri, 15 Aug 2025 10:45:09 +0800 Ye Liu <[email protected]> wrote:
From: Ye Liu <[email protected]>
Use
max()to find the maximumlowmem_reservevalue andmin_t()to cap it tomanaged_pagesincalculate_totalreserve_pages(), instead of open-coding the comparisons. No functional change....
mm/page_alloc.c +0
-4@@ -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 6245
Quoted 10 lines
mm/page_alloc.c +3
-2+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; + max = min_t(unsigned long, max, managed_pages);
Use of max_t/min_t is usually a sign that we messed up the type choices somewhere.
In this case, I'd say that zone.lowmem_reserve[] should have been ulong. Oh well.
Zi Yan
reviewerQuoted 34 lines · Andrew Morton
Quoted 18 lines · On Fri, 15 Aug 2025 10:45:09 +0800 Ye Liu <[email protected]> wrote:
From: Ye Liu <[email protected]>
Use
max()to find the maximumlowmem_reservevalue andmin_t()to cap it tomanaged_pagesincalculate_totalreserve_pages(), instead of open-coding the comparisons. No functional change....
mm/page_alloc.c +0
-4@@ -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 6245
Quoted 10 lines
mm/page_alloc.c +3
-2+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; + max = min_t(unsigned long, max, managed_pages);Use of max_t/min_t is usually a sign that we messed up the type choices somewhere.
Quoted 2 lines
In this case, I'd say that zone.lowmem_reserve[] should have been ulong. Oh well.
It was, but then changed into long because in __zone_watermark_ok() it is compared to can-be-negative free_pages (see commit 3484b2de9499). Maybe it is better to revert that change and convert ulong lowmem_reserve to long in __zone_watermark_ok() for the comparison.
+Mel, for his opinion on it, since he made the change in 3484b2de9499.
--
Best Regards,
Yan, Zi