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

From: Gregory Price

Date: Wed Sep 02 2026 - 10:04:34 EST


On Wed, Sep 02, 2026 at 05:04:12PM +0800, Liu Jing wrote:
> 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.
>

weights can't be zero and we just fixed the rebind race

> 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
>
>