[PATCH] mempolicy: fix mod-by-zero in weighted_interleave_nid

Liu Jing posted 1 patch 3 weeks, 3 days ago
mm/mempolicy.c | 4 ++++
1 file changed, 4 insertion(+), 0 deletion(-)
[PATCH] mempolicy: fix mod-by-zero in weighted_interleave_nid
Posted by Liu Jing 3 weeks, 3 days ago
In weighted_interleave_nid(), if the iw_table contains all-zero
weights for every node in the policy nodemask, weight_total sums to
zero. The subsequent "ilx % weight_total" triggers a divide-by-zero
panic.

Add a zero check returning the current NUMA node id when the total
weight is zero, consistent with the existing !nr_nodes guard.

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

--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -2218,6 +2218,10 @@
 	/* calculate the total weight */
 	for_each_node_mask(nid, nodemask)
 		weight_total += table ? table[nid] : 1;
+
+
+	if (!weight_total)
+		return numa_node_id();
 
 	/* Calculate the node offset based on totals */
 	target = ilx % weight_total;

--
2.43.0
Re: [PATCH] mempolicy: fix mod-by-zero in weighted_interleave_nid
Posted by Gregory Price 3 weeks, 2 days ago
On Wed, Sep 02, 2026 at 05:03:46PM +0800, Liu Jing wrote:
> In weighted_interleave_nid(), if the iw_table contains all-zero
> weights for every node in the policy nodemask, weight_total sums to
> zero. The subsequent "ilx % weight_total" triggers a divide-by-zero
> panic.
> 

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

if you're actually seeing this then there's a different bug to fix

> Add a zero check returning the current NUMA node id when the total
> weight is zero, consistent with the existing !nr_nodes guard.
> 
> Signed-off-by: Liu Jing <liujing@cmss.chinamobile.com>
> ---
>  mm/mempolicy.c | 4 ++++
>  1 file changed, 4 insertion(+), 0 deletion(-)
> 
> --- a/mm/mempolicy.c
> +++ b/mm/mempolicy.c
> @@ -2218,6 +2218,10 @@
>  	/* calculate the total weight */
>  	for_each_node_mask(nid, nodemask)
>  		weight_total += table ? table[nid] : 1;
> +
> +
> +	if (!weight_total)
> +		return numa_node_id();
>  
>  	/* Calculate the node offset based on totals */
>  	target = ilx % weight_total;
> 
> --
> 2.43.0
> 
>