Skip to contentSkip to conversation

mm/mm_init: avoid overflow in percentage-based core sizing

PATCH Applied Avi Weiss 2 participants 2 messages Nov 6 – Nov 7

Subsystems (2)
lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

PATCH 1/1 mm/mm_init: avoid overflow in percentage-based core sizing

The kernelcore= and movablecore= percentage calculations multiply the total number of pages before dividing. On 32-bit kernels, the intermediate unsigned long product can overflow, producing incorrect memory zone boundaries. With 2 GiB of RAM and kernelcore=100%, a 32-bit HIGHMEM kernel incorrectly creates ZONE_MOVABLE.

Use mult_frac() to calculate both percentages without overflowing the intermediate product.

Fixes a5c6d6509342 ("mm, page_alloc: extend kernelcore and movablecore for percent")
---
 mm/mm_init.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
mm/mm_init.c +3 -4
index 2a0e782a151d..27a50ecd5c20 100644
@@ -7,6 +7,7 @@
  *
  */
 #include <linux/kernel.h>
+#include <linux/math.h>
 #include <linux/init.h>
 #include <linux/kobject.h>
 #include <linux/export.h>
@@ -448,11 +449,9 @@ static void __init find_zone_movable_pfns_for_nodes(void)
 	 * amount of necessary memory.
 	 */
 	if (required_kernelcore_percent)
-		required_kernelcore = (totalpages * 100 * required_kernelcore_percent) /
-				       10000UL;
+		required_kernelcore = mult_frac(totalpages, required_kernelcore_percent, 100);
 	if (required_movablecore_percent)
-		required_movablecore = (totalpages * 100 * required_movablecore_percent) /
-					10000UL;
+		required_movablecore = mult_frac(totalpages, required_movablecore_percent, 100);
 
 	/*
 	 * If movablecore= was specified, calculate what size of

base-commit: 36aad21d50d1af3f6954bb0c988257a908202477

Signature · 1 line
2.43.0
maintainer Applied
#1 lore raw

From: Mike Rapoport (Microsoft) <[email protected]>

Quoted 10 lines · Avi Weiss

The kernelcore= and movablecore= percentage calculations multiply the total number of pages before dividing. On 32-bit kernels, the intermediate unsigned long product can overflow, producing incorrect memory zone boundaries. With 2 GiB of RAM and kernelcore=100%, a 32-bit HIGHMEM kernel incorrectly creates ZONE_MOVABLE.

Use mult_frac() to calculate both percentages without overflowing the intermediate product.

[...]

Applied to for-next branch of memblock.git tree, thanks!

[1/1] mm/mm_init: avoid overflow in percentage-based core sizing

      commit: 36557d74a757a4bb71bb1492449680177687f5a1

tree: https://git.kernel.org/pub/scm/linux/kernel/git/mm/memblock
branch: for-next

--
Sincerely yours,
Mike.