Noticed the following command failure while testing CPU hotplug. $ qemu-system-x86_64 -machine q35,accel=kvm -smp 1,maxcpus=2, cores=1, threads=1,sockets=2 -cpu EPYC -device EPYC-x86_64- cpu,core-id=0,socket-id=1,thread-id=0
qemu-system-x86_64: -device EPYC-x86_64-cpu,core-id=0,socket-id=1, thread-id=0: Invalid CPU [socket: 21855, die: 0, core: 0, thread: 0] with APIC ID 21855, valid index range 0:1 This happens because APIC ID is calculated using uninitialized memory. This is happening after the addition of new field node_id in X86CPUTopoIDs structure. The node_id field is uninitialized while calling apicid_from_topo_ids. The problem is discussed in the thread below. https://lore.kernel.org/qemu-devel/20200602171838.gg577...@habkost.net/ Fix the problem by initializing the node_id properly. Signed-off-by: Babu Moger <babu.mo...@amd.com> --- hw/i386/pc.c | 2 ++ include/hw/i386/topology.h | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/hw/i386/pc.c b/hw/i386/pc.c index 2128f3d6fe..974cc30891 100644 --- a/hw/i386/pc.c +++ b/hw/i386/pc.c @@ -1585,6 +1585,8 @@ static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev, topo_ids.die_id = cpu->die_id; topo_ids.core_id = cpu->core_id; topo_ids.smt_id = cpu->thread_id; + topo_ids.node_id = cpu_x86_use_epyc_apic_id_encoding(ms->cpu_type) ? + x86_node_id_for_epyc(&topo_info, &topo_ids) : 0; cpu->apic_id = x86ms->apicid_from_topo_ids(&topo_info, &topo_ids); } diff --git a/include/hw/i386/topology.h b/include/hw/i386/topology.h index 07239f95f4..ee4deb84c4 100644 --- a/include/hw/i386/topology.h +++ b/include/hw/i386/topology.h @@ -140,6 +140,17 @@ static inline unsigned apicid_pkg_offset_epyc(X86CPUTopoInfo *topo_info) apicid_node_width_epyc(topo_info); } +static inline unsigned x86_node_id_for_epyc(X86CPUTopoInfo *topo_info, + const X86CPUTopoIDs *topo_ids) +{ + unsigned nr_nodes = MAX(topo_info->nodes_per_pkg, 1); + unsigned cores_per_node = DIV_ROUND_UP((topo_info->dies_per_pkg * + topo_info->cores_per_die * + topo_info->threads_per_core), + nr_nodes); + + return (topo_ids->core_id / cores_per_node) % nr_nodes; +} /* * Make APIC ID for the CPU based on Pkg_ID, Core_ID, SMT_ID *