[PATCH RFC V5 12/30] arm/virt: Release objects for *disabled* possible vCPUs after init

Salil Mehta via posted 30 patches 1 week ago
[PATCH RFC V5 12/30] arm/virt: Release objects for *disabled* possible vCPUs after init
Posted by Salil Mehta via 1 week ago
During `machvirt_init()`, QOM ARMCPU objects are pre-created along with the
corresponding KVM vCPUs in the host for all possible vCPUs. This is necessary
due to the architectural constraint that KVM restricts the deferred creation of
KVM vCPUs and VGIC initialization/sizing after VM initialization. Hence, VGIC is
pre-sized with possible vCPUs.

After the initialization of the machine is complete, the disabled possible KVM
vCPUs are parked in the per-virt-machine list "kvm_parked_vcpus," and we release
the QOM ARMCPU objects for the disabled vCPUs. These will be re-created when the
vCPU is hotplugged again. The QOM ARMCPU object is then re-attached to the
corresponding parked KVM vCPU.

Alternatively, we could have chosen not to release the QOM CPU objects and kept
reusing them. This approach might require some modifications to the
`qdevice_add()` interface to retrieve the old ARMCPU object instead of creating
a new one for the hotplug request.

Each of these approaches has its own pros and cons. This prototype uses the
first approach (suggestions are welcome!).

Co-developed-by: Keqian Zhu <zhukeqian1@huawei.com>
Signed-off-by: Keqian Zhu <zhukeqian1@huawei.com>
Signed-off-by: Salil Mehta <salil.mehta@huawei.com>
---
 hw/arm/virt.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/hw/arm/virt.c b/hw/arm/virt.c
index 6ac2d8826e..7cbc212130 100644
--- a/hw/arm/virt.c
+++ b/hw/arm/virt.c
@@ -2037,6 +2037,7 @@ static void virt_cpu_post_init(VirtMachineState *vms, MemoryRegion *sysmem)
 {
     CPUArchIdList *possible_cpus = vms->parent.possible_cpus;
     int max_cpus = MACHINE(vms)->smp.max_cpus;
+    int smp_cpus = MACHINE(vms)->smp.cpus;
     bool aarch64, steal_time;
     CPUState *cpu;
 
@@ -2095,6 +2096,30 @@ static void virt_cpu_post_init(VirtMachineState *vms, MemoryRegion *sysmem)
             }
         }
     }
+
+    if (kvm_enabled() || tcg_enabled()) {
+        int i = 0;
+        CPU_FOREACH_POSSIBLE(cpu, possible_cpus) {
+            /*
+             * Release the disabled ARMCPU objects that were previously used
+             * during initialization for pre-sizing host KVM.
+             *
+             * We simulate the presence of these non-existent vCPUs to the guest
+             * via ACPI by setting `_STA.PRES = 1` (indicating they are present)
+             * and mark them as disabled vCPUs by setting `_STA.ENA = 0`,
+             * ensuring they cannot be used. These vCPUs can be added back to
+             * the guest later through hotplug operations when ARMCPU objects
+             * are recreated using the '-device_add' QMP command.
+             */
+            if (i >= smp_cpus) {
+                CPUArchId *cpu_slot;
+                cpu_slot = virt_find_cpu_slot(cpu);
+                cpu_slot->cpu = NULL;
+                object_unref(OBJECT(cpu));
+            }
+            i++;
+        }
+    }
 }
 
 static void virt_cpu_set_properties(Object *cpuobj, const CPUArchId *cpu_slot,
-- 
2.34.1