[PATCH] x86: Avoid divide by 0 in amd_smn_init()

From: Jason Andryuk

Date: Tue Jun 23 2026 - 17:19:31 EST


Xen synthesizes the CPU topology, so the num_nodes and num_roots values
may be surprising for amd_smn_init(). Specifically:

roots_per_node = num_roots / num_nodes;

may results in roots_per_node == 0 which leads to divide by zero in

count % roots_per_node

As an example, I have a system with a Xen PVH dom0 that reports:
Found 1 AMD root devices
Found 2 AMD nodes

Ensure roots_per_node is at least 1 to avoid the divide by zero errors.
num_nodes are allocated for amd_roots, so roots_per_node = 1 will
populate all the entries.

Also add a pr_debug() for the number of nodes.

Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Jason Andryuk <jason.andryuk@xxxxxxx>
---
This is an alternative to
https://lore.kernel.org/xen-devel/20260506055528.476493-2-penny.zheng@xxxxxxx/
but it leaves smn available for dom0.
---
arch/x86/kernel/amd_node.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/arch/x86/kernel/amd_node.c b/arch/x86/kernel/amd_node.c
index 0be01725a2a4..f335c5f1ae1d 100644
--- a/arch/x86/kernel/amd_node.c
+++ b/arch/x86/kernel/amd_node.c
@@ -282,11 +282,14 @@ static int __init amd_smn_init(void)
return -ENODEV;

num_nodes = amd_num_nodes();
+ pr_debug("Found %d AMD nodes\n", num_nodes);
amd_roots = kzalloc_objs(*amd_roots, num_nodes);
if (!amd_roots)
return -ENOMEM;

roots_per_node = num_roots / num_nodes;
+ if (roots_per_node == 0)
+ roots_per_node = 1;

count = 0;
node = 0;
--
2.34.1