x86/xen/smp_pv: Count number of vCPUs early
authorThomas Gleixner <tglx@linutronix.de>
Tue, 13 Feb 2024 21:06:00 +0000 (22:06 +0100)
committerThomas Gleixner <tglx@linutronix.de>
Thu, 15 Feb 2024 21:07:44 +0000 (22:07 +0100)
XEN/PV has a completely broken vCPU enumeration scheme, which just works by
chance and provides zero topology information. Each vCPU ends up being a
single core package.

Dom0 provides MADT which can be used for topology information, but that
table is the unmodified host table, which means that there can be more CPUs
registered than the number of vCPUs XEN provides for the dom0 guest.

DomU does not have ACPI and both rely on counting the possible vCPUs via an
hypercall.

To prepare for using CPUID topology information either via MADT or via fake
APIC IDs count the number of possible CPUs during early boot and adjust
nr_cpu_ids() accordingly.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Michael Kelley <mhklinux@outlook.com>
Tested-by: Sohil Mehta <sohil.mehta@intel.com>
Link: https://lore.kernel.org/r/20240213210252.571795063@linutronix.de
arch/x86/xen/enlighten_pv.c
arch/x86/xen/smp.h
arch/x86/xen/smp_pv.c

index aeb33e0a3f763370b00daf427063b2010f0bcf40..ace2eb054053f95e53ac43992d9b778335e68748 100644 (file)
@@ -200,6 +200,9 @@ static void __init xen_pv_init_platform(void)
                xen_set_mtrr_data();
        else
                mtrr_overwrite_state(NULL, 0, MTRR_TYPE_WRBACK);
+
+       /* Adjust nr_cpu_ids before "enumeration" happens */
+       xen_smp_count_cpus();
 }
 
 static void __init xen_pv_guest_late_init(void)
index c20cbb14c82bad72f5703297d49ac8aa775e4425..b8efdbc693f7a86820549e10428445150a04e592 100644 (file)
@@ -19,6 +19,7 @@ extern void xen_smp_intr_free(unsigned int cpu);
 int xen_smp_intr_init_pv(unsigned int cpu);
 void xen_smp_intr_free_pv(unsigned int cpu);
 
+void xen_smp_count_cpus(void);
 void xen_smp_cpus_done(unsigned int max_cpus);
 
 void xen_smp_send_reschedule(int cpu);
@@ -44,6 +45,7 @@ static inline int xen_smp_intr_init_pv(unsigned int cpu)
        return 0;
 }
 static inline void xen_smp_intr_free_pv(unsigned int cpu) {}
+static inline void xen_smp_count_cpus(void) { }
 #endif /* CONFIG_SMP */
 
 #endif
index 98849b3f5eac2e881b53fcaee27d2b3d9a5da309..44706f01bb9bcfb82dad15cbd1ed6eafa46a77b8 100644 (file)
@@ -411,6 +411,20 @@ static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id)
        return IRQ_HANDLED;
 }
 
+void __init xen_smp_count_cpus(void)
+{
+       unsigned int cpus;
+
+       for (cpus = 0; cpus < nr_cpu_ids; cpus++) {
+               if (HYPERVISOR_vcpu_op(VCPUOP_is_up, cpus, NULL) < 0)
+                       break;
+       }
+
+       pr_info("Xen PV: Detected %u vCPUS\n", cpus);
+       if (cpus < nr_cpu_ids)
+               set_nr_cpu_ids(cpus);
+}
+
 static const struct smp_ops xen_smp_ops __initconst = {
        .smp_prepare_boot_cpu = xen_pv_smp_prepare_boot_cpu,
        .smp_prepare_cpus = xen_pv_smp_prepare_cpus,