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

From: Jason Andryuk

Date: Wed Jun 24 2026 - 11:43:54 EST


On 2026-06-23 17:35, Borislav Petkov wrote:
On Tue, Jun 23, 2026 at 05:19:03PM -0400, Jason Andryuk wrote:
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.

Does this alternative work too?

https://lore.kernel.org/r/20260605230949.GBaiNXPZ2ztjVL7DBg@fat_crate.local

It boots as dom0, but smn is disabled.

Yours:
$ journalctl -b | grep -e amd-pmf -e ypervisor
Hypervisor detected: Xen HVM
amd-pmf AMDI0107:00: error -EINVAL: error in reading from 0x13b102e8
amd-pmf AMDI0107:00: probe with driver amd-pmf failed with error -22

Mine:
$ journalctl -b -2 | grep -e amd-pmf -e ypervisor
Hypervisor detected: Xen HVM
amd-pmf AMDI0107:00: No Smart PC policy present
amd-pmf AMDI0107:00: registered PMF device successfully

amd-pmc also fails with yours.

dom0 is the privileged hardware domain and sees the physical PCI devices. get_next_root() is looking for AMD or Hygon vendor ids, and dom0 will find those. A regular domain (domU) would see QEMU's emulated pci host bridges, which will be Intel.

I have wip s0ix support with Xen where dom0 issues the amd-pmc calls to enter s0ix. I'm not sure of all the uses of SMN, but with Xen dom0/hardware domain running most drivers, I think it should be available.

Regards,
Jason