The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] x86: Avoid divide by 0 in amd_smn_init()
@ 2026-06-23 21:19 Jason Andryuk
  2026-06-23 21:35 ` Borislav Petkov
  2026-06-24  9:21 ` Ingo Molnar
  0 siblings, 2 replies; 4+ messages in thread
From: Jason Andryuk @ 2026-06-23 21:19 UTC (permalink / raw)
  To: Mario Limonciello, Yazen Ghannam, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin
  Cc: Penny Zheng, Jason Andryuk, stable, linux-kernel

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@vger.kernel.org
Signed-off-by: Jason Andryuk <jason.andryuk@amd.com>
---
This is an alternative to
https://lore.kernel.org/xen-devel/20260506055528.476493-2-penny.zheng@amd.com/
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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86: Avoid divide by 0 in amd_smn_init()
  2026-06-23 21:19 [PATCH] x86: Avoid divide by 0 in amd_smn_init() Jason Andryuk
@ 2026-06-23 21:35 ` Borislav Petkov
  2026-06-24  9:21 ` Ingo Molnar
  1 sibling, 0 replies; 4+ messages in thread
From: Borislav Petkov @ 2026-06-23 21:35 UTC (permalink / raw)
  To: Jason Andryuk
  Cc: Mario Limonciello, Yazen Ghannam, Thomas Gleixner, Ingo Molnar,
	Dave Hansen, x86, H. Peter Anvin, Penny Zheng, stable,
	linux-kernel

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@vger.kernel.org
> Signed-off-by: Jason Andryuk <jason.andryuk@amd.com>
> ---
> This is an alternative to
> https://lore.kernel.org/xen-devel/20260506055528.476493-2-penny.zheng@amd.com/
> but it leaves smn available for dom0.

Does this alternative work too?

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

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86: Avoid divide by 0 in amd_smn_init()
  2026-06-23 21:19 [PATCH] x86: Avoid divide by 0 in amd_smn_init() Jason Andryuk
  2026-06-23 21:35 ` Borislav Petkov
@ 2026-06-24  9:21 ` Ingo Molnar
  2026-06-24 14:54   ` Borislav Petkov
  1 sibling, 1 reply; 4+ messages in thread
From: Ingo Molnar @ 2026-06-24  9:21 UTC (permalink / raw)
  To: Jason Andryuk
  Cc: Mario Limonciello, Yazen Ghannam, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Penny Zheng,
	stable, linux-kernel


* Jason Andryuk <jason.andryuk@amd.com> 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.

So arguably this Xen PHV dom0 PCI configuration is bogus,
because it violates the roots % nodes rule, right?

Why should we not go back to something similar to the pre-40a5f6ffdfc8
state of things, which warned about such bogus configs in the syslog,
so that it could be seen and fixed:

-               /*
-                * There should be _exactly_ N roots for each DF/SMN
-                * interface.
-                */
-               if (!roots_per_misc || (root_count % roots_per_misc)) {
-                       pr_info("Unsupported AMD DF/PCI configuration found\n");
-                       return -ENODEV;
-               }

Instead of your patch which just silently works around the
borkage and issues a pr_debug() that nobody reads?

AFAICS the following fix:

  0a4b61d9c2e4 ("x86/amd_node: Fix AMD root device caching")

Never restored that sanity check & warning about such firmware
bogosity.

Thanks,

	Ingo

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86: Avoid divide by 0 in amd_smn_init()
  2026-06-24  9:21 ` Ingo Molnar
@ 2026-06-24 14:54   ` Borislav Petkov
  0 siblings, 0 replies; 4+ messages in thread
From: Borislav Petkov @ 2026-06-24 14:54 UTC (permalink / raw)
  To: Ingo Molnar
  Cc: Jason Andryuk, Mario Limonciello, Yazen Ghannam, Thomas Gleixner,
	Ingo Molnar, Dave Hansen, x86, H. Peter Anvin, Penny Zheng,
	stable, linux-kernel

On Wed, Jun 24, 2026 at 11:21:51AM +0200, Ingo Molnar wrote:
> Why should we not go back to something similar to the pre-40a5f6ffdfc8

Because this code obviously cannot run in a guest. See my other reply.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-06-24 14:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-23 21:19 [PATCH] x86: Avoid divide by 0 in amd_smn_init() Jason Andryuk
2026-06-23 21:35 ` Borislav Petkov
2026-06-24  9:21 ` Ingo Molnar
2026-06-24 14:54   ` Borislav Petkov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox