qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Zhao Liu <zhao1.liu@linux.intel.com>
To: "Paolo Bonzini" <pbonzini@redhat.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
	"Yanan Wang" <wangyanan55@huawei.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Michael S . Tsirkin" <mst@redhat.com>,
	"Jason Wang" <jasowang@redhat.com>,
	"Nicholas Piggin" <npiggin@gmail.com>,
	"Daniel Henrique Barboza" <danielhb413@gmail.com>,
	"Igor Mammedov" <imammedo@redhat.com>,
	"Cédric Le Goater" <clg@kaod.org>,
	"Frédéric Barrat" <fbarrat@linux.ibm.com>,
	"David Gibson" <david@gibson.dropbear.id.au>,
	"Harsh Prateek Bora" <harshpb@linux.ibm.com>,
	"Stefano Stabellini" <sstabellini@kernel.org>,
	"Anthony Perard" <anthony.perard@citrix.com>,
	"Paul Durrant" <paul@xen.org>,
	"Gerd Hoffmann" <kraxel@redhat.com>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Alistair Francis" <alistair@alistair23.me>,
	"Edgar E . Iglesias" <edgar.iglesias@gmail.com>,
	"Daniel P . Berrangé" <berrange@redhat.com>,
	"Bin Meng" <bin.meng@windriver.com>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Weiwei Li" <liwei1518@gmail.com>,
	"Liu Zhiwei" <zhiwei_liu@linux.alibaba.com>,
	qemu-devel@nongnu.org, kvm@vger.kernel.org, qemu-ppc@nongnu.org,
	xen-devel@lists.xenproject.org, qemu-arm@nongnu.org,
	qemu-riscv@nongnu.org, qemu-s390x@nongnu.org
Cc: Nina Schoetterl-Glausch <nsg@linux.ibm.com>,
	Thomas Huth <thuth@redhat.com>, Zhiyuan Lv <zhiyuan.lv@intel.com>,
	Zhenyu Wang <zhenyu.z.wang@intel.com>,
	Yongwei Ma <yongwei.ma@intel.com>, Zhao Liu <zhao1.liu@intel.com>
Subject: [RFC 33/41] hw/machine: Validate smp topology tree without -smp
Date: Thu, 30 Nov 2023 22:41:55 +0800	[thread overview]
Message-ID: <20231130144203.2307629-34-zhao1.liu@linux.intel.com> (raw)
In-Reply-To: <20231130144203.2307629-1-zhao1.liu@linux.intel.com>

From: Zhao Liu <zhao1.liu@intel.com>

QOM topology allows user to create topology tree from cli without -smp,
in this case, validate the topology tree to meet the smp requirement.

Currently, for compatibility with MachineState.smp, initialize
MachineState.smp from topology tree in this case.

Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
 hw/core/cpu-slot.c         | 146 +++++++++++++++++++++++++++++++++++++
 hw/core/machine.c          |  10 +++
 include/hw/core/cpu-slot.h |   1 +
 3 files changed, 157 insertions(+)

diff --git a/hw/core/cpu-slot.c b/hw/core/cpu-slot.c
index ade155baf60b..45b6aef0750a 100644
--- a/hw/core/cpu-slot.c
+++ b/hw/core/cpu-slot.c
@@ -413,3 +413,149 @@ void machine_create_smp_topo_tree(MachineState *ms, Error **errp)
     }
     slot->smp_parsed = true;
 }
+
+static void set_smp_child_topo_info(CpuTopology *smp_info,
+                                    CPUTopoStat *stat,
+                                    CPUTopoLevel child_level)
+{
+    unsigned int *smp_count;
+    CPUTopoStatEntry *entry;
+
+    smp_count = get_smp_info_by_level(smp_info, child_level);
+    entry = get_topo_stat_entry(stat, child_level);
+    *smp_count = entry->max_units ? entry->max_units : 1;
+
+    return;
+}
+
+typedef struct ValidateCbData {
+    CPUTopoStat *stat;
+    CpuTopology *smp_info;
+    Error **errp;
+} ValidateCbData;
+
+static int validate_topo_children(CPUTopoState *topo, void *opaque)
+{
+    CPUTopoLevel level = CPU_TOPO_LEVEL(topo), next_level;
+    ValidateCbData *cb = opaque;
+    unsigned int max_children;
+    CPUTopoStatEntry *entry;
+    Error **errp = cb->errp;
+
+    if (level != CPU_TOPO_THREAD && !topo->num_children &&
+        !topo->max_children) {
+        error_setg(errp, "Invalid topology: the CPU topology "
+                   "(level: %s, index: %d) isn't completed.",
+                   cpu_topo_level_to_string(level), topo->index);
+        return TOPO_FOREACH_ERR;
+    }
+
+    if (level == CPU_TOPO_UNKNOWN) {
+        error_setg(errp, "Invalid CPU topology: unknown topology level.");
+        return TOPO_FOREACH_ERR;
+    }
+
+    /*
+     * Only CPU_TOPO_THREAD level's child_level could be CPU_TOPO_UNKNOWN,
+     * but machine_validate_cpu_topology() is before CPU creation.
+     */
+    if (topo->child_level == CPU_TOPO_UNKNOWN) {
+        error_setg(errp, "Invalid CPU topology: incomplete topology "
+                   "(level: %s, index: %d), no child?.",
+                   cpu_topo_level_to_string(level), topo->index);
+        return TOPO_FOREACH_ERR;
+    }
+
+    /*
+     * Currently hybrid topology isn't supported, so only SMP topology
+     * is allowed.
+     */
+
+    entry = get_topo_stat_entry(cb->stat, topo->child_level);
+
+    /* Max threads per core is pre-configured by "nr-threads". */
+    max_children = topo->child_level != CPU_TOPO_THREAD ?
+                   topo->num_children : topo->max_children;
+
+    if (entry->max_units != max_children) {
+        error_setg(errp, "Invalid SMP topology: "
+                   "The %s topology is asymmetric.",
+                   cpu_topo_level_to_string(level));
+        return TOPO_FOREACH_ERR;
+    }
+
+    next_level = find_next_bit(cb->stat->curr_levels, USER_AVAIL_LEVEL_NUM,
+                               topo->child_level + 1);
+
+    if (next_level != level) {
+        error_setg(errp, "Invalid smp topology: "
+                   "asymmetric CPU topology depth.");
+        return TOPO_FOREACH_ERR;
+    }
+
+    set_smp_child_topo_info(cb->smp_info, cb->stat, topo->child_level);
+
+    return TOPO_FOREACH_CONTINUE;
+}
+
+/*
+ * Only check the case user configures CPU topology via -device
+ * without -smp. In this case, MachineState.smp also needs to be
+ * initialized based on topology tree.
+ */
+bool machine_validate_cpu_topology(MachineState *ms, Error **errp)
+{
+    MachineClass *mc = MACHINE_GET_CLASS(ms);
+    CPUTopoState *slot_topo = CPU_TOPO(ms->topo);
+    CPUTopoStat *stat = &ms->topo->stat;
+    CpuTopology *smp_info = &ms->smp;
+    unsigned int total_cpus;
+    ValidateCbData cb;
+
+    if (ms->topo->smp_parsed) {
+        return true;
+    } else if (!slot_topo->num_children) {
+        /*
+         * If there's no -smp nor -device to add topology children,
+         * then create the default topology.
+         */
+        machine_create_smp_topo_tree(ms, errp);
+        if (*errp) {
+            return false;
+        }
+        return true;
+    }
+
+    if (test_bit(CPU_TOPO_CLUSTER, stat->curr_levels)) {
+        mc->smp_props.has_clusters = true;
+    }
+
+    /*
+     * The next cpu_topo_child_foreach_recursive() doesn't cover the
+     * parent topology unit. Update information for root here.
+     */
+    set_smp_child_topo_info(smp_info, stat, slot_topo->child_level);
+
+    cb.stat = stat;
+    cb.smp_info = smp_info;
+    cb.errp = errp;
+
+    cpu_topo_child_foreach_recursive(slot_topo, NULL,
+                                     validate_topo_children, &cb);
+    if (*errp) {
+        return false;
+    }
+
+    ms->smp.cpus = stat->pre_plugged_cpus ?
+                   stat->pre_plugged_cpus : 1;
+    ms->smp.max_cpus = stat->max_cpus ?
+                       stat->max_cpus : 1;
+
+    total_cpus = ms->smp.drawers * ms->smp.books *
+                 ms->smp.sockets * ms->smp.dies *
+                 ms->smp.clusters * ms->smp.cores *
+                 ms->smp.threads;
+    g_assert(total_cpus == ms->smp.max_cpus);
+
+    return true;
+}
diff --git a/hw/core/machine.c b/hw/core/machine.c
index 0c1739814124..5fa0c826f35e 100644
--- a/hw/core/machine.c
+++ b/hw/core/machine.c
@@ -1505,6 +1505,16 @@ void machine_run_board_init(MachineState *machine, const char *mem_path, Error *
                                    "on", false);
     }
 
+    /*
+     * TODO: drop this check and validate topology tree by default
+     * when all arches support QOM topology.
+     */
+    if (machine_class->smp_props.possible_cpus_qom_granu) {
+        if (!machine_validate_cpu_topology(machine, errp)) {
+            return;
+        }
+    }
+
     accel_init_interfaces(ACCEL_GET_CLASS(machine->accelerator));
     machine_class->init(machine);
     phase_advance(PHASE_MACHINE_INITIALIZED);
diff --git a/include/hw/core/cpu-slot.h b/include/hw/core/cpu-slot.h
index de3d08fcb2ac..9e1c6d6b7ff2 100644
--- a/include/hw/core/cpu-slot.h
+++ b/include/hw/core/cpu-slot.h
@@ -100,5 +100,6 @@ struct CPUSlot {
 
 void machine_plug_cpu_slot(MachineState *ms);
 void machine_create_smp_topo_tree(MachineState *ms, Error **errp);
+bool machine_validate_cpu_topology(MachineState *ms, Error **errp);
 
 #endif /* CPU_SLOT_H */
-- 
2.34.1



  parent reply	other threads:[~2023-11-30 14:37 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-30 14:41 [RFC 00/41] qom-topo: Abstract Everything about CPU Topology Zhao Liu
2023-11-30 14:41 ` [RFC 01/41] qdev: Introduce new device category to cover basic topology device Zhao Liu
2023-11-30 14:41 ` [RFC 02/41] qdev: Allow qdev_device_add() to add specific category device Zhao Liu
2023-11-30 14:41 ` [RFC 03/41] system: Create base category devices from cli before board initialization Zhao Liu
2023-11-30 14:41 ` [RFC 04/41] qom/object: Introduce helper to resolve path from non-direct parent Zhao Liu
2023-11-30 14:41 ` [RFC 05/41] qdev: Set device parent and id after setting properties Zhao Liu
2023-11-30 14:41 ` [RFC 06/41] qdev: Introduce user-child interface to collect devices from -device Zhao Liu
2023-11-30 14:41 ` [RFC 07/41] qdev: Introduce parent option in -device Zhao Liu
2023-11-30 14:41 ` [RFC 08/41] hw/core/topo: Introduce CPU topology device abstraction Zhao Liu
2023-11-30 14:41 ` [RFC 09/41] hw/core/topo: Support topology index for topology device Zhao Liu
2023-11-30 14:41 ` [RFC 10/41] hw/core/topo: Add virtual method to update topology info for parent Zhao Liu
2023-11-30 14:41 ` [RFC 11/41] hw/core/topo: Add virtual method to check topology child Zhao Liu
2023-11-30 14:41 ` [RFC 12/41] hw/core/topo: Add helpers to traverse the CPU topology tree Zhao Liu
2023-11-30 14:41 ` [RFC 13/41] hw/core/cpu: Convert CPU from general device to topology device Zhao Liu
2023-11-30 14:41 ` [RFC 14/41] PPC/ppc-core: Offload core-id to PPC specific core abstarction Zhao Liu
2023-11-30 14:41 ` [RFC 15/41] hw/cpu/core: Allow to configure plugged threads for cpu-core Zhao Liu
2023-11-30 14:41 ` [RFC 16/41] PPC/ppc-core: Limit plugged-threads and nr-threads to be equal Zhao Liu
2023-11-30 14:41 ` [RFC 17/41] hw/cpu/core: Convert cpu-core from general device to topology device Zhao Liu
2023-11-30 14:41 ` [RFC 18/41] hw/cpu/cluster: Rename CPUClusterState to CPUCluster Zhao Liu
2023-11-30 14:41 ` [RFC 19/41] hw/cpu/cluster: Wrap TCG related ops and props into CONFIG_TCG Zhao Liu
2023-11-30 14:41 ` [RFC 20/41] hw/cpu/cluster: Descript cluster is not only used for TCG in comment Zhao Liu
2023-11-30 14:41 ` [RFC 21/41] hw/cpu/cluster: Allow cpu-cluster to be created by -device Zhao Liu
2023-11-30 14:41 ` [RFC 22/41] hw/cpu/cluster: Convert cpu-cluster from general device to topology device Zhao Liu
2023-11-30 14:41 ` [RFC 23/41] hw/cpu/die: Abstract cpu-die level as " Zhao Liu
2023-11-30 14:41 ` [RFC 24/41] hw/cpu/socket: Abstract cpu-socket " Zhao Liu
2023-11-30 14:41 ` [RFC 25/41] hw/cpu/book: Abstract cpu-book " Zhao Liu
2023-11-30 14:41 ` [RFC 26/41] hw/cpu/drawer: Abstract cpu-drawer " Zhao Liu
2023-11-30 14:41 ` [RFC 27/41] hw/core/slot: Introduce CPU slot as the root of CPU topology Zhao Liu
2023-11-30 14:41 ` [RFC 28/41] hw/core/slot: Maintain the core queue in CPU slot Zhao Liu
2023-11-30 14:41 ` [RFC 29/41] hw/core/slot: Statistics topology information " Zhao Liu
2023-11-30 14:41 ` [RFC 30/41] hw/core/slot: Check topology child to be added under " Zhao Liu
2023-11-30 14:41 ` [RFC 31/41] hw/machine: Plug cpu-slot into machine to maintain topology tree Zhao Liu
2023-11-30 14:41 ` [RFC 32/41] hw/machine: Build smp topology tree from -smp Zhao Liu
2023-11-30 14:41 ` Zhao Liu [this message]
2023-11-30 14:41 ` [RFC 34/41] hw/core/topo: Implement user-child to collect topology device from cli Zhao Liu
2023-11-30 14:41 ` [RFC 35/41] hw/i386: Make x86_cpu_new() private in x86.c Zhao Liu
2023-11-30 14:41 ` [RFC 36/41] hw/i386: Allow x86_cpu_new() to specify parent for new CPU Zhao Liu
2023-11-30 14:41 ` [RFC 37/41] hw/i386: Allow i386 to create new CPUs from QOM topology Zhao Liu
2023-11-30 14:42 ` [RFC 38/41] hw/i386: Wrap apic id and topology sub ids assigning as helpers Zhao Liu
2023-11-30 14:42 ` [RFC 39/41] hw/i386: Add the interface to search parent for QOM topology Zhao Liu
2023-11-30 14:42 ` [RFC 40/41] hw/i386: Support " Zhao Liu
2023-11-30 14:42 ` [RFC 41/41] hw/i386: Cleanup non-QOM topology support Zhao Liu
2023-12-11 13:36 ` [RFC 00/41] qom-topo: Abstract Everything about CPU Topology Zhao Liu

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=20231130144203.2307629-34-zhao1.liu@linux.intel.com \
    --to=zhao1.liu@linux.intel.com \
    --cc=alex.bennee@linaro.org \
    --cc=alistair@alistair23.me \
    --cc=anthony.perard@citrix.com \
    --cc=berrange@redhat.com \
    --cc=bin.meng@windriver.com \
    --cc=clg@kaod.org \
    --cc=danielhb413@gmail.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=edgar.iglesias@gmail.com \
    --cc=eduardo@habkost.net \
    --cc=fbarrat@linux.ibm.com \
    --cc=harshpb@linux.ibm.com \
    --cc=imammedo@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=liwei1518@gmail.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=npiggin@gmail.com \
    --cc=nsg@linux.ibm.com \
    --cc=palmer@dabbelt.com \
    --cc=paul@xen.org \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=qemu-s390x@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=sstabellini@kernel.org \
    --cc=thuth@redhat.com \
    --cc=wangyanan55@huawei.com \
    --cc=xen-devel@lists.xenproject.org \
    --cc=yongwei.ma@intel.com \
    --cc=zhao1.liu@intel.com \
    --cc=zhenyu.z.wang@intel.com \
    --cc=zhiwei_liu@linux.alibaba.com \
    --cc=zhiyuan.lv@intel.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;
as well as URLs for NNTP newsgroup(s).