[PATCH] mempolicy: fix div-by-zero in alloc_pages_bulk_weighted_interleave

From: Liu Jing

Date: Wed Sep 02 2026 - 05:15:44 EST


In alloc_pages_bulk_weighted_interleave(), if the iw_table contains
all-zero weights for every node in the policy nodemask, weight_total
sums to zero. The subsequent "rem_pages / weight_total" and
"rem_pages % weight_total" trigger a divide-by-zero panic.

The function already guards against !nnodes but not against a zero
weight_total. Add the missing check, freeing the locally allocated
weights buffer before returning.

Signed-off-by: Liu Jing <liujing@xxxxxxxxxxxxxxxxxxxx>
---
mm/mempolicy.c | 5 +++++
1 file changed, 5 insertion(+), 0 deletion(-)

--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -2697,6 +2697,11 @@
/* calculate total, detect system default usage */
for_each_node_mask(node, nodes)
weight_total += weights[node];
+
+ if (!weight_total) {
+ kfree(weights);
+ return total_allocated;
+ }

/*
* Calculate rounds/partial rounds to minimize __alloc_pages_bulk calls.

--
2.43.0