The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Penny Zheng <penny.zheng@amd.com>
To: <x86@kernel.org>
Cc: <ray.huang@amd.com>, <Jason.Andryuk@amd.com>,
	<stefano.stabellini@amd.com>, Penny Zheng <penny.zheng@amd.com>,
	"Mario Limonciello" <mario.limonciello@amd.com>,
	Yazen Ghannam <yazen.ghannam@amd.com>,
	Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	<xen-devel@lists.xenproject.org>, <linux-kernel@vger.kernel.org>
Subject: [PATCH v1 1/2] x86/amd_node: avoid divide-by-zero in amd_smn_init() under Xen dom0
Date: Wed, 6 May 2026 13:55:18 +0800	[thread overview]
Message-ID: <20260506055528.476493-2-penny.zheng@amd.com> (raw)
In-Reply-To: <20260506055528.476493-1-penny.zheng@amd.com>

To prevent each dom0 vCPU from looking like an SMT sibling of another
vCPU, Xen synthesizes guest x2APIC IDs as vcpu_index * 2.
While the spacing every vCPU's APIC ID by 2 can therefore push the IDs
past the package-field boundary, making Linux see more packages than
the platform actually has. amd_num_nodes() inherits that inflated count,
so num_nodes can exceed num_roots (the number of AMD root complexes
discovered on the PCI bus). The subsequent

    roots_per_node = num_roots / num_nodes;
    ... count % roots_per_node ...

then divides by zero in amd_smn_init().

Reject num_roots < num_nodes explicitly and bail out with -ENODEV.

Signed-off-by: Penny Zheng <penny.zheng@amd.com>
---
 arch/x86/kernel/amd_node.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/arch/x86/kernel/amd_node.c b/arch/x86/kernel/amd_node.c
index 0be01725a2a4..c896060fe0df 100644
--- a/arch/x86/kernel/amd_node.c
+++ b/arch/x86/kernel/amd_node.c
@@ -282,6 +282,18 @@ static int __init amd_smn_init(void)
 		return -ENODEV;
 
 	num_nodes = amd_num_nodes();
+
+	/*
+	 * Xen dom0's synthetic APIC IDs may imply more nodes than host
+	 * bridges visible in PCI config space. Bail out to avoid a
+	 * divide-by-zero when later computing roots_per_node.
+	 */
+	if (num_roots < num_nodes) {
+		pr_debug("AMD root count (%u) < node count (%u); skipping SMN init\n",
+			 num_roots, num_nodes);
+		return -ENODEV;
+	}
+
 	amd_roots = kzalloc_objs(*amd_roots, num_nodes);
 	if (!amd_roots)
 		return -ENOMEM;
-- 
2.43.0


  reply	other threads:[~2026-05-06  5:56 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-06  5:55 [PATCH v1 0/2] x86/amd_node: harden amd_smn_init() against Xen dom0 topology Penny Zheng
2026-05-06  5:55 ` Penny Zheng [this message]
2026-05-06  5:55 ` [PATCH v1 2/2] x86/amd_node: reject SMN access when amd_smn_init() did not complete Penny Zheng
2026-05-06 17:17 ` [PATCH v1 0/2] x86/amd_node: harden amd_smn_init() against Xen dom0 topology Mario Limonciello
2026-05-07  8:37 ` Jiaqing Zhao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260506055528.476493-2-penny.zheng@amd.com \
    --to=penny.zheng@amd.com \
    --cc=Jason.Andryuk@amd.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mario.limonciello@amd.com \
    --cc=mingo@redhat.com \
    --cc=ray.huang@amd.com \
    --cc=stefano.stabellini@amd.com \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    --cc=yazen.ghannam@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox