Skip to contentSkip to conversation

Add architecture agnostic code to support vCPU Hotplug

PATCH Superseded Salil Mehta 6 participants 42 messages Jun 7, 2024 – Jul 11, 2024 3 messages not in the archive

v13 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v13 cover Add architecture agnostic code to support vCPU Hotplug

Virtual CPU hotplug support is being added across various architectures[1][3]. This series adds various code bits common across all architectures:

1. vCPU creation and Parking code refactor [Patch 1]
2. Update ACPI GED framework to support vCPU Hotplug [Patch 2,3]
3. ACPI CPUs AML code change [Patch 4,5]
4. Helper functions to support unrealization of CPU objects [Patch 6,7]
5. Docs [Patch 8]

Repository:

[*] https://github.com/salil-mehta/qemu.git virt-cpuhp-armv8/rfc-v3.arch.agnostic.v13

NOTE: This series is meant to work in conjunction with Architecture specific patch-set. For ARM, a combined patch-set (arch agnostic + specific) was earlier pushed as RFC V2[1]. A ARM architecture specific patch-set (subset of RFC V2) is being finalized and shall be floated soon as RFC-V3. Please find a version of it compatible with this patch-set at the below location. (TCG support is broken in below and is being fixed)

[*] https://github.com/salil-mehta/qemu/tree/virt-cpuhp-armv8/rfc-v3-rc2

Revision History:

Patch-set V12 -> V13
1. Added Reviewed-by Tag of Harsh Prateek Bora's (IBM) [PATCH V12 1/8]
2. Moved the kvm_{create,park,unpark}_vcpu prototypes from accel/kvm/kvm-cpus.h to include/sysemu/kvm.h. These can later be exported through AccelOps.

Patch-set V11 -> V12
1. Addressed Harsh Prateek Bora's (IBM) comment
- Changed @cpu to @vcpu_id in the kvm_unpark_vcpu protoype header/
2. Added Zhao Liu's (Intel) Tested-by for whole series
- Qtest does not breaks on Intel platforms now.
3. Added Zhao Liu's (Intel) Reviewed-by for [PATCH V11 {1/8 - 3/8}]

Patch-set V10 -> V11
1. Addressed Nicholas Piggin's (IBM) comment
- moved the traces in kvm_unpark_vcpu and kvm_create_vcpu at the end
- Added the Reviewed-by Tag for [PATCH V10 1/8]
2. Addressed Alex Bennée's (Linaro) comments
- Added a note explaining dependency of the [PATCH V10 7/8] on Arch specific patch-set

Patch-set V9 -> V10
1. Addressed Nicholas Piggin's (IBM) & Philippe Mathieu-Daudé (Linaro) comments
- carved out kvm_unpark_vcpu and added its trace
- Widened the scope of the kvm_unpark_vcpu so that it can be used by generic framework being thought out

Patch-set V8 -> V9
1. Addressed Vishnu Pajjuri's (Ampere) comments
- Added kvm_fd to trace in kvm_create_vcpu
- Some clean ups: arch vcpu-id and sbd variable
- Added the missed initialization of cpu->gdb_num_regs
2. Addressed the commnet from Zhao Liu (Intel)
- Make initialization of CPU Hotplug state conditional (possible_cpu_arch_ids!=NULL)

Patch-set V7 -> V8
1. Rebased and Fixed the conflicts

Patch-set V6 -> V7
1. Addressed Alex Bennée's comments
- Updated the docs
2. Addressed Igor Mammedov's comments
- Merged patches [Patch V6 3/9] & [Patch V6 7/9] with [Patch V6 4/9]
- Updated commit-log of [Patch V6 1/9] and [Patch V6 5/9]
3. Added Shaoqin Huang's Reviewed-by tags for whole series.

Patch-set V5 -> V6
1. Addressed Gavin Shan's comments
- Fixed the assert() ranges of address spaces
- Rebased the patch-set to latest changes in the qemu.git
- Added Reviewed-by tags for patches {8,9}
2. Addressed Jonathan Cameron's comments
- Updated commit-log for [Patch V5 1/9] with mention of trace events
- Added Reviewed-by tags for patches {1,5}
3. Added Tested-by tags from Xianglai Li
4. Fixed checkpatch.pl error "Qemu -> QEMU" in [Patch V5 1/9]

Patch-set V4 -> V5
1. Addressed Gavin Shan's comments
- Fixed the trace events print string for kvm_{create,get,park,destroy}_vcpu
- Added Reviewed-by tag for patch {1}
2. Added Shaoqin Huang's Reviewed-by tags for Patches {2,3}
3. Added Tested-by Tag from Vishnu Pajjuri to the patch-set
4. Dropped the ARM specific [Patch V4 10/10]

Patch-set V3 -> V4
1. Addressed David Hilderbrand's comments
- Fixed the wrong doc comment of kvm_park_vcpu API prototype
- Added Reviewed-by tags for patches {2,4}

Patch-set V2 -> V3
1. Addressed Jonathan Cameron's comments
- Fixed 'vcpu-id' type wrongly changed from 'unsigned long' to 'integer'
- Removed unnecessary use of variable 'vcpu_id' in kvm_park_vcpu
- Updated [Patch V2 3/10] commit-log with details of ACPI_CPU_SCAN_METHOD macro
- Updated [Patch V2 5/10] commit-log with details of conditional event handler method
- Added Reviewed-by tags for patches {2,3,4,6,7}
2. Addressed Gavin Shan's comments
- Remove unnecessary use of variable 'vcpu_id' in kvm_par_vcpu
- Fixed return value in kvm_get_vcpu from -1 to -ENOENT
- Reset the value of 'gdb_num_g_regs' in gdb_unregister_coprocessor_all
- Fixed the kvm_{create,park}_vcpu prototypes docs
- Added Reviewed-by tags for patches {2,3,4,5,6,7,9,10}
3. Addressed one earlier missed comment by Alex Bennée in RFC V1
- Added traces instead of DPRINTF in the newly added and some existing functions

Patch-set V1 -> V2
1. Addressed Alex Bennée's comments
- Refactored the kvm_create_vcpu logic to get rid of goto
- Added the docs for kvm_{create,park}_vcpu prototypes
- Splitted the gdbstub and AddressSpace destruction change into separate patches
- Added Reviewed-by tags for patches {2,10}

References:

[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/qemu-devel/[email protected]/

Salil Mehta (8):
accel/kvm: Extract common KVM vCPU {creation,parking} code hw/acpi: Move CPU ctrl-dev MMIO region len macro to common header file hw/acpi: Update ACPI GED framework to support vCPU Hotplug hw/acpi: Update GED _EVT method AML with CPU scan
hw/acpi: Update CPUs AML with cpu-(ctrl)dev change
physmem: Add helper function to destroy CPU AddressSpace
gdbstub: Add helper function to unregister GDB register space docs/specs/acpi_hw_reduced_hotplug: Add the CPU Hotplug Event Bit

 accel/kvm/kvm-all.c                    | 95 +++++++++++++++++---------
 accel/kvm/kvm-cpus.h                   |  1 -
 accel/kvm/trace-events                 |  5 +-
 docs/specs/acpi_hw_reduced_hotplug.rst |  3 +-
 gdbstub/gdbstub.c                      | 13 ++++
 hw/acpi/acpi-cpu-hotplug-stub.c        |  6 ++
 hw/acpi/cpu.c                          | 33 ++++++---
 hw/acpi/generic_event_device.c         | 21 ++++++
 hw/core/cpu-common.c                   |  1 -
 hw/i386/acpi-build.c                   |  3 +-
 include/exec/cpu-common.h              |  8 +++
 include/exec/gdbstub.h                 |  6 ++
 include/hw/acpi/cpu.h                  |  5 +-
 include/hw/acpi/cpu_hotplug.h          |  4 ++
 include/hw/acpi/generic_event_device.h |  4 ++
 include/hw/core/cpu.h                  |  1 +
 include/sysemu/kvm.h                   | 25 +++++++
 system/physmem.c                       | 29 ++++++++
 18 files changed, 214 insertions(+), 49 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v13 1/8 accel/kvm: Extract common KVM vCPU {creation,parking} code

KVM vCPU creation is done once during the vCPU realization when Qemu vCPU thread is spawned. This is common to all the architectures as of now.

Hot-unplug of vCPU results in destruction of the vCPU object in QOM but the corresponding KVM vCPU object in the Host KVM is not destroyed as KVM doesn't support vCPU removal. Therefore, its representative KVM vCPU object/context in Qemu is parked.

Refactor architecture common logic so that some APIs could be reused by vCPU Hotplug code of some architectures likes ARM, Loongson etc. Update new/old APIs with trace events. No functional change is intended here.

---
 accel/kvm/kvm-all.c    | 95 ++++++++++++++++++++++++++++--------------
 accel/kvm/kvm-cpus.h   |  1 -
 accel/kvm/trace-events |  5 ++-
 include/sysemu/kvm.h   | 25 +++++++++++
 4 files changed, 92 insertions(+), 34 deletions(-)
accel/kvm/kvm-all.c +63 -32
index c0be9f5eed..8f9128bb92 100644
@@ -340,14 +340,71 @@ err:
     return ret;
 }
 
+void kvm_park_vcpu(CPUState *cpu)
+{
+    struct KVMParkedVcpu *vcpu;
+
+    trace_kvm_park_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
+
+    vcpu = g_malloc0(sizeof(*vcpu));
+    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
+    vcpu->kvm_fd = cpu->kvm_fd;
+    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node);
+}
+
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id)
+{
+    struct KVMParkedVcpu *cpu;
+    int kvm_fd = -ENOENT;
+
+    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
+        if (cpu->vcpu_id == vcpu_id) {
+            QLIST_REMOVE(cpu, node);
+            kvm_fd = cpu->kvm_fd;
+            g_free(cpu);
+        }
+    }
+
+    trace_kvm_unpark_vcpu(vcpu_id, kvm_fd > 0 ? "unparked" : "not found parked");
+
+    return kvm_fd;
+}
+
+int kvm_create_vcpu(CPUState *cpu)
+{
+    unsigned long vcpu_id = kvm_arch_vcpu_id(cpu);
+    KVMState *s = kvm_state;
+    int kvm_fd;
+
+    /* check if the KVM vCPU already exist but is parked */
+    kvm_fd = kvm_unpark_vcpu(s, vcpu_id);
+    if (kvm_fd < 0) {
+        /* vCPU not parked: create a new KVM vCPU */
+        kvm_fd = kvm_vm_ioctl(s, KVM_CREATE_VCPU, vcpu_id);
+        if (kvm_fd < 0) {
+            error_report("KVM_CREATE_VCPU IOCTL failed for vCPU %lu", vcpu_id);
+            return kvm_fd;
+        }
+    }
+
+    cpu->kvm_fd = kvm_fd;
+    cpu->kvm_state = s;
+    cpu->vcpu_dirty = true;
+    cpu->dirty_pages = 0;
+    cpu->throttle_us_per_full = 0;
+
+    trace_kvm_create_vcpu(cpu->cpu_index, vcpu_id, kvm_fd);
+
+    return 0;
+}
+
 static int do_kvm_destroy_vcpu(CPUState *cpu)
 {
     KVMState *s = kvm_state;
     long mmap_size;
-    struct KVMParkedVcpu *vcpu = NULL;
     int ret = 0;
 
-    trace_kvm_destroy_vcpu();
+    trace_kvm_destroy_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
 
     ret = kvm_arch_destroy_vcpu(cpu);
     if (ret < 0) {
@@ -373,10 +430,7 @@ static int do_kvm_destroy_vcpu(CPUState *cpu)
         }
     }
 
-    vcpu = g_malloc0(sizeof(*vcpu));
-    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
-    vcpu->kvm_fd = cpu->kvm_fd;
-    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node);
+    kvm_park_vcpu(cpu);
 err:
     return ret;
 }
@@ -389,24 +443,6 @@ void kvm_destroy_vcpu(CPUState *cpu)
     }
 }
 
-static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id)
-{
-    struct KVMParkedVcpu *cpu;
-
-    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
-        if (cpu->vcpu_id == vcpu_id) {
-            int kvm_fd;
-
-            QLIST_REMOVE(cpu, node);
-            kvm_fd = cpu->kvm_fd;
-            g_free(cpu);
-            return kvm_fd;
-        }
-    }
-
-    return kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)vcpu_id);
-}
-
 int kvm_init_vcpu(CPUState *cpu, Error **errp)
 {
     KVMState *s = kvm_state;
@@ -415,19 +451,14 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp)
 
     trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
 
-    ret = kvm_get_vcpu(s, kvm_arch_vcpu_id(cpu));
+    ret = kvm_create_vcpu(cpu);
     if (ret < 0) {
-        error_setg_errno(errp, -ret, "kvm_init_vcpu: kvm_get_vcpu failed (%lu)",
+        error_setg_errno(errp, -ret,
+                         "kvm_init_vcpu: kvm_create_vcpu failed (%lu)",
                          kvm_arch_vcpu_id(cpu));
         goto err;
     }
 
-    cpu->kvm_fd = ret;
-    cpu->kvm_state = s;
-    cpu->vcpu_dirty = true;
-    cpu->dirty_pages = 0;
-    cpu->throttle_us_per_full = 0;
-
     mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
     if (mmap_size < 0) {
         ret = mmap_size;
accel/kvm/kvm-cpus.h +0 -1
index ca40add32c..171b22fd29 100644
@@ -22,5 +22,4 @@ bool kvm_supports_guest_debug(void);
 int kvm_insert_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len);
 int kvm_remove_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len);
 void kvm_remove_all_breakpoints(CPUState *cpu);
-
 #endif /* KVM_CPUS_H */
accel/kvm/trace-events +4 -1
index 681ccb667d..37626c1ac5 100644
@@ -9,6 +9,10 @@ kvm_device_ioctl(int fd, int type, void *arg) "dev fd %d, type 0x%x, arg %p"
 kvm_failed_reg_get(uint64_t id, const char *msg) "Warning: Unable to retrieve ONEREG %" PRIu64 " from KVM: %s"
 kvm_failed_reg_set(uint64_t id, const char *msg) "Warning: Unable to set ONEREG %" PRIu64 " to KVM: %s"
 kvm_init_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id: %lu"
+kvm_create_vcpu(int cpu_index, unsigned long arch_cpu_id, int kvm_fd) "index: %d, id: %lu, kvm fd: %d"
+kvm_destroy_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id: %lu"
+kvm_park_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id: %lu"
+kvm_unpark_vcpu(unsigned long arch_cpu_id, const char *msg) "id: %lu %s"
 kvm_irqchip_commit_routes(void) ""
 kvm_irqchip_add_msi_route(char *name, int vector, int virq) "dev %s vector %d virq %d"
 kvm_irqchip_update_msi_route(int virq) "Updating MSI route virq=%d"
@@ -25,7 +29,6 @@ kvm_dirty_ring_reaper(const char *s) "%s"
 kvm_dirty_ring_reap(uint64_t count, int64_t t) "reaped %"PRIu64" pages (took %"PRIi64" us)"
 kvm_dirty_ring_reaper_kick(const char *reason) "%s"
 kvm_dirty_ring_flush(int finished) "%d"
-kvm_destroy_vcpu(void) ""
 kvm_failed_get_vcpu_mmap_size(void) ""
 kvm_cpu_exec(void) ""
 kvm_interrupt_exit_request(void) ""
include/sysemu/kvm.h +25 -0
index c31d9c7356..c4a914b3d8 100644
@@ -313,6 +313,31 @@ int kvm_create_device(KVMState *s, uint64_t type, bool test);
  */
 bool kvm_device_supported(int vmfd, uint64_t type);
 
+/**
+ * kvm_create_vcpu - Gets a parked KVM vCPU or creates a KVM vCPU
+ * @cpu: QOM CPUState object for which KVM vCPU has to be fetched/created.
+ *
+ * @returns: 0 when success, errno (<0) when failed.
+ */
+int kvm_create_vcpu(CPUState *cpu);
+
+/**
+ * kvm_park_vcpu - Park QEMU KVM vCPU context
+ * @cpu: QOM CPUState object for which QEMU KVM vCPU context has to be parked.
+ *
+ * @returns: none
+ */
+void kvm_park_vcpu(CPUState *cpu);
+
+/**
+ * kvm_unpark_vcpu - unpark QEMU KVM vCPU context
+ * @s: KVM State
+ * @vcpu_id: Architecture vCPU ID of the parked vCPU
+ *
+ * @returns: KVM fd
+ */
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id);
+
 /* Arch specific hooks */
 
 extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
Signature · 1 line
2.34.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw

Re: [PATCH V13 1/8] accel/kvm: Extract common KVM vCPU {creation,parking} code

+qemu-devel, qemu-ppc

Ping!

Quoted 298 lines · Harsh Prateek Bora

+ MST, Igor - to help with early review/merge. TIA.

Quoted 294 lines · Salil Mehta

Hello

Quoted 9 lines

  From: Harsh Prateek Bora <[email protected]>
  Sent: Friday, June 14, 2024 6:24 AM
  Hi Paolo, Nick,
  Can this patch 1/8 be merged earlier provided we have got sufficient R-bys for it and the review of entire series may take a longer time? We have some ppc64 patches based on it, hence the ask.
  Hi Salil,
  I am hoping we are not expecting anymore changes to this patch, please confirm.

I do not expect any change. I had requested Michael to merge the complete series as it is stranding other users. He then requested Igor to take a final look but he has not reverted yet. I'll remind Michael again. BTW, can you reply to below patch explicitly indicating your interest in the series so that MST knows who else are the stake holders here

https://lore.kernel.org/qemu-devel/[email protected]/

Hi Paolo,

A request, would it be possible to skim through this series from KVM perspective? (although nothing has changed which will affect the KVM and this is architecture agnostic patch-set)

Many thanks!

Best
Salil.

Quoted 260 lines

  regards,
  Harsh
  On 6/7/24 17:26, Salil Mehta wrote:
  > KVM vCPU creation is done once during the vCPU realization when Qemu > vCPU thread is spawned. This is common to all the architectures as of now. >
  > Hot-unplug of vCPU results in destruction of the vCPU object in QOM > but the corresponding KVM vCPU object in the Host KVM is not destroyed > as KVM doesn't support vCPU removal. Therefore, its representative KVM > vCPU object/context in Qemu is parked.
  >
  > Refactor architecture common logic so that some APIs could be reused > by vCPU Hotplug code of some architectures likes ARM, Loongson etc. > Update new/old APIs with trace events. No functional change is intended here.
  >
  > Signed-off-by: Salil Mehta <[email protected]>
  > Reviewed-by: Gavin Shan <[email protected]>
  > Tested-by: Vishnu Pajjuri <[email protected]>
  > Reviewed-by: Jonathan Cameron <[email protected]>
  > Tested-by: Xianglai Li <[email protected]>
  > Tested-by: Miguel Luis <[email protected]>
  > Reviewed-by: Shaoqin Huang <[email protected]>
  > Reviewed-by: Vishnu Pajjuri <[email protected]>
  > Reviewed-by: Nicholas Piggin <[email protected]>
  > Tested-by: Zhao Liu <[email protected]>
  > Reviewed-by: Zhao Liu <[email protected]>
  > Reviewed-by: Harsh Prateek Bora <[email protected]>
  > ---
  >   accel/kvm/kvm-all.c    | 95
++++++++++++++++++++++++++++------------
  --
  >   accel/kvm/kvm-cpus.h   |  1 -
  >   accel/kvm/trace-events |  5 ++-
  >   include/sysemu/kvm.h   | 25 +++++++++++
  >   4 files changed, 92 insertions(+), 34 deletions(-)
  >
  > diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index > c0be9f5eed..8f9128bb92 100644
  > --- a/accel/kvm/kvm-all.c
  > +++ b/accel/kvm/kvm-all.c
  > @@ -340,14 +340,71 @@ err:
  >       return ret;
  >   }
  >
  > +void kvm_park_vcpu(CPUState *cpu)
  > +{
  > +    struct KVMParkedVcpu *vcpu;
  > +
  > +    trace_kvm_park_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); > +
  > +    vcpu = g_malloc0(sizeof(*vcpu));
  > +    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
  > +    vcpu->kvm_fd = cpu->kvm_fd;
  > +    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node); } > +
  > +int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id) {
  > +    struct KVMParkedVcpu *cpu;
  > +    int kvm_fd = -ENOENT;
  > +
  > +    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
  > +        if (cpu->vcpu_id == vcpu_id) {
  > +            QLIST_REMOVE(cpu, node);
  > +            kvm_fd = cpu->kvm_fd;
  > +            g_free(cpu);
  > +        }
  > +    }
  > +
  > +    trace_kvm_unpark_vcpu(vcpu_id, kvm_fd > 0 ? "unparked" : "not > + found parked");
  > +
  > +    return kvm_fd;
  > +}
  > +
  > +int kvm_create_vcpu(CPUState *cpu)
  > +{
  > +    unsigned long vcpu_id = kvm_arch_vcpu_id(cpu);
  > +    KVMState *s = kvm_state;
  > +    int kvm_fd;
  > +
  > +    /* check if the KVM vCPU already exist but is parked */ > +    kvm_fd = kvm_unpark_vcpu(s, vcpu_id);
  > +    if (kvm_fd < 0) {
  > +        /* vCPU not parked: create a new KVM vCPU */
  > +        kvm_fd = kvm_vm_ioctl(s, KVM_CREATE_VCPU, vcpu_id); > +        if (kvm_fd < 0) {
  > +            error_report("KVM_CREATE_VCPU IOCTL failed for vCPU %lu", vcpu_id);
  > +            return kvm_fd;
  > +        }
  > +    }
  > +
  > +    cpu->kvm_fd = kvm_fd;
  > +    cpu->kvm_state = s;
  > +    cpu->vcpu_dirty = true;
  > +    cpu->dirty_pages = 0;
  > +    cpu->throttle_us_per_full = 0;
  > +
  > +    trace_kvm_create_vcpu(cpu->cpu_index, vcpu_id, kvm_fd); > +
  > +    return 0;
  > +}
  > +
  >   static int do_kvm_destroy_vcpu(CPUState *cpu)
  >   {
  >       KVMState *s = kvm_state;
  >       long mmap_size;
  > -    struct KVMParkedVcpu *vcpu = NULL;
  >       int ret = 0;
  >
  > -    trace_kvm_destroy_vcpu();
  > +    trace_kvm_destroy_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); >
  >       ret = kvm_arch_destroy_vcpu(cpu);
  >       if (ret < 0) {
  > @@ -373,10 +430,7 @@ static int do_kvm_destroy_vcpu(CPUState *cpu) >           }
  >       }
  >
  > -    vcpu = g_malloc0(sizeof(*vcpu));
  > -    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
  > -    vcpu->kvm_fd = cpu->kvm_fd;
  > -    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node); > +    kvm_park_vcpu(cpu);
  >   err:
  >       return ret;
  >   }
  > @@ -389,24 +443,6 @@ void kvm_destroy_vcpu(CPUState *cpu)
  >       }
  >   }
  >
  > -static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id) -{ > -    struct KVMParkedVcpu *cpu;
  > -
  > -    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
  > -        if (cpu->vcpu_id == vcpu_id) {
  > -            int kvm_fd;
  > -
  > -            QLIST_REMOVE(cpu, node);
  > -            kvm_fd = cpu->kvm_fd;
  > -            g_free(cpu);
  > -            return kvm_fd;
  > -        }
  > -    }
  > -
  > -    return kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)vcpu_id); > -}
  > -
  >   int kvm_init_vcpu(CPUState *cpu, Error **errp)
  >   {
  >       KVMState *s = kvm_state;
  > @@ -415,19 +451,14 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp) >
  >       trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); >
  > -    ret = kvm_get_vcpu(s, kvm_arch_vcpu_id(cpu));
  > +    ret = kvm_create_vcpu(cpu);
  >       if (ret < 0) {
  > -        error_setg_errno(errp, -ret, "kvm_init_vcpu: kvm_get_vcpu failed (%lu)",
  > +        error_setg_errno(errp, -ret,
  > +                         "kvm_init_vcpu: kvm_create_vcpu failed > + (%lu)",
  >                            kvm_arch_vcpu_id(cpu));
  >           goto err;
  >       }
  >
  > -    cpu->kvm_fd = ret;
  > -    cpu->kvm_state = s;
  > -    cpu->vcpu_dirty = true;
  > -    cpu->dirty_pages = 0;
  > -    cpu->throttle_us_per_full = 0;
  > -
  >       mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
  >       if (mmap_size < 0) {
  >           ret = mmap_size;
  > diff --git a/accel/kvm/kvm-cpus.h b/accel/kvm/kvm-cpus.h index > ca40add32c..171b22fd29 100644
  > --- a/accel/kvm/kvm-cpus.h
  > +++ b/accel/kvm/kvm-cpus.h
  > @@ -22,5 +22,4 @@ bool kvm_supports_guest_debug(void);
  >   int kvm_insert_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len);
  >   int kvm_remove_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len);
  >   void kvm_remove_all_breakpoints(CPUState *cpu);
  > -
  >   #endif /* KVM_CPUS_H */
  > diff --git a/accel/kvm/trace-events b/accel/kvm/trace-events index > 681ccb667d..37626c1ac5 100644
  > --- a/accel/kvm/trace-events
  > +++ b/accel/kvm/trace-events
  > @@ -9,6 +9,10 @@ kvm_device_ioctl(int fd, int type, void *arg) "dev fd %d, type 0x%x, arg %p"
  >   kvm_failed_reg_get(uint64_t id, const char *msg) "Warning: Unable to retrieve ONEREG %" PRIu64 " from KVM: %s"
  >   kvm_failed_reg_set(uint64_t id, const char *msg) "Warning: Unable to set ONEREG %" PRIu64 " to KVM: %s"
  >   kvm_init_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id:
  %lu"
  > +kvm_create_vcpu(int cpu_index, unsigned long arch_cpu_id, int kvm_fd) "index: %d, id: %lu, kvm fd: %d"
  > +kvm_destroy_vcpu(int cpu_index, unsigned long arch_cpu_id) "index:
  %d id: %lu"
  > +kvm_park_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d
  id: %lu"
  > +kvm_unpark_vcpu(unsigned long arch_cpu_id, const char *msg) "id: %lu %s"
  >   kvm_irqchip_commit_routes(void) ""
  >   kvm_irqchip_add_msi_route(char *name, int vector, int virq) "dev %s vector %d virq %d"
  >   kvm_irqchip_update_msi_route(int virq) "Updating MSI route virq=%d" > @@ -25,7 +29,6 @@ kvm_dirty_ring_reaper(const char *s) "%s"
  >   kvm_dirty_ring_reap(uint64_t count, int64_t t) "reaped %"PRIu64" pages (took %"PRIi64" us)"
  >   kvm_dirty_ring_reaper_kick(const char *reason) "%s"
  >   kvm_dirty_ring_flush(int finished) "%d"
  > -kvm_destroy_vcpu(void) ""
  >   kvm_failed_get_vcpu_mmap_size(void) ""
  >   kvm_cpu_exec(void) ""
  >   kvm_interrupt_exit_request(void) ""
  > diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index > c31d9c7356..c4a914b3d8 100644
  > --- a/include/sysemu/kvm.h
  > +++ b/include/sysemu/kvm.h
  > @@ -313,6 +313,31 @@ int kvm_create_device(KVMState *s, uint64_t type, bool test);
  >    */
  >   bool kvm_device_supported(int vmfd, uint64_t type);
  >
  > +/**
  > + * kvm_create_vcpu - Gets a parked KVM vCPU or creates a KVM vCPU > + * @cpu: QOM CPUState object for which KVM vCPU has to be fetched/created.
  > + *
  > + * @returns: 0 when success, errno (<0) when failed.
  > + */
  > +int kvm_create_vcpu(CPUState *cpu);
  > +
  > +/**
  > + * kvm_park_vcpu - Park QEMU KVM vCPU context
  > + * @cpu: QOM CPUState object for which QEMU KVM vCPU context has to be parked.
  > + *
  > + * @returns: none
  > + */
  > +void kvm_park_vcpu(CPUState *cpu);
  > +
  > +/**
  > + * kvm_unpark_vcpu - unpark QEMU KVM vCPU context
  > + * @s: KVM State
  > + * @vcpu_id: Architecture vCPU ID of the parked vCPU
  > + *
  > + * @returns: KVM fd
  > + */
  > +int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id);
  > +
  >   /* Arch specific hooks */
  >
  >   extern const KVMCapabilityInfo kvm_arch_required_capabilities[];

#6 lore raw

Re: [PATCH V13 1/8] accel/kvm: Extract common KVM vCPU {creation,parking} code

Looks like there is a bit of noise around this recently. Do we think the hotplug patches can get over the line this time?

If not, perhaps we work with Salil to get this patch 1 upstream at least.

Thanks,
Nick

Quoted 325 lines · Harsh Prateek Bora

+qemu-devel, qemu-ppc

Ping!

Quoted 320 lines · Harsh Prateek Bora

+ MST, Igor - to help with early review/merge. TIA.

Quoted 316 lines · Salil Mehta

Hello

Quoted 10 lines

  From: Harsh Prateek Bora <[email protected]>
  Sent: Friday, June 14, 2024 6:24 AM
  Hi Paolo, Nick,
  Can this patch 1/8 be merged earlier provided we have got sufficient R-bys
  for it and the review of entire series may take a longer time? We have some ppc64 patches based on it, hence the ask.
  Hi Salil,
  I am hoping we are not expecting anymore changes to this patch, please confirm.

I do not expect any change. I had requested Michael to merge the complete series as it is stranding other users. He then requested Igor to take a final look but
he has not reverted yet. I'll remind Michael again. BTW, can you reply to below
patch explicitly indicating your interest in the series so that MST knows who else
are the stake holders here

https://lore.kernel.org/qemu-devel/[email protected]/

Hi Paolo,

A request, would it be possible to skim through this series from KVM perspective?
(although nothing has changed which will affect the KVM and this is architecture
agnostic patch-set)

Many thanks!

Best
Salil.

Quoted 276 lines

  regards,
  Harsh
  On 6/7/24 17:26, Salil Mehta wrote:
  > KVM vCPU creation is done once during the vCPU realization when Qemu > vCPU thread is spawned. This is common to all the architectures as of now.
  >
  > Hot-unplug of vCPU results in destruction of the vCPU object in QOM > but the corresponding KVM vCPU object in the Host KVM is not destroyed
  > as KVM doesn't support vCPU removal. Therefore, its representative KVM
  > vCPU object/context in Qemu is parked.
  >
  > Refactor architecture common logic so that some APIs could be reused > by vCPU Hotplug code of some architectures likes ARM, Loongson etc. > Update new/old APIs with trace events. No functional change is intended
  here.
  >
  > Signed-off-by: Salil Mehta <[email protected]>
  > Reviewed-by: Gavin Shan <[email protected]>
  > Tested-by: Vishnu Pajjuri <[email protected]> > Reviewed-by: Jonathan Cameron <[email protected]> > Tested-by: Xianglai Li <[email protected]>
  > Tested-by: Miguel Luis <[email protected]>
  > Reviewed-by: Shaoqin Huang <[email protected]>
  > Reviewed-by: Vishnu Pajjuri <[email protected]> > Reviewed-by: Nicholas Piggin <[email protected]>
  > Tested-by: Zhao Liu <[email protected]>
  > Reviewed-by: Zhao Liu <[email protected]>
  > Reviewed-by: Harsh Prateek Bora <[email protected]> > ---
  >   accel/kvm/kvm-all.c    | 95
++++++++++++++++++++++++++++------------
  --
  >   accel/kvm/kvm-cpus.h   |  1 -
  >   accel/kvm/trace-events |  5 ++-
  >   include/sysemu/kvm.h   | 25 +++++++++++
  >   4 files changed, 92 insertions(+), 34 deletions(-)
  >
  > diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index > c0be9f5eed..8f9128bb92 100644
  > --- a/accel/kvm/kvm-all.c
  > +++ b/accel/kvm/kvm-all.c
  > @@ -340,14 +340,71 @@ err:
  >       return ret;
  >   }
  >
  > +void kvm_park_vcpu(CPUState *cpu)
  > +{
  > +    struct KVMParkedVcpu *vcpu;
  > +
  > +    trace_kvm_park_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); > +
  > +    vcpu = g_malloc0(sizeof(*vcpu));
  > +    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
  > +    vcpu->kvm_fd = cpu->kvm_fd;
  > +    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node); } > +
  > +int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id) { > +    struct KVMParkedVcpu *cpu;
  > +    int kvm_fd = -ENOENT;
  > +
  > +    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
  > +        if (cpu->vcpu_id == vcpu_id) {
  > +            QLIST_REMOVE(cpu, node);
  > +            kvm_fd = cpu->kvm_fd;
  > +            g_free(cpu);
  > +        }
  > +    }
  > +
  > +    trace_kvm_unpark_vcpu(vcpu_id, kvm_fd > 0 ? "unparked" : "not > + found parked");
  > +
  > +    return kvm_fd;
  > +}
  > +
  > +int kvm_create_vcpu(CPUState *cpu)
  > +{
  > +    unsigned long vcpu_id = kvm_arch_vcpu_id(cpu);
  > +    KVMState *s = kvm_state;
  > +    int kvm_fd;
  > +
  > +    /* check if the KVM vCPU already exist but is parked */ > +    kvm_fd = kvm_unpark_vcpu(s, vcpu_id);
  > +    if (kvm_fd < 0) {
  > +        /* vCPU not parked: create a new KVM vCPU */
  > +        kvm_fd = kvm_vm_ioctl(s, KVM_CREATE_VCPU, vcpu_id); > +        if (kvm_fd < 0) {
  > +            error_report("KVM_CREATE_VCPU IOCTL failed for vCPU %lu",
  vcpu_id);
  > +            return kvm_fd;
  > +        }
  > +    }
  > +
  > +    cpu->kvm_fd = kvm_fd;
  > +    cpu->kvm_state = s;
  > +    cpu->vcpu_dirty = true;
  > +    cpu->dirty_pages = 0;
  > +    cpu->throttle_us_per_full = 0;
  > +
  > +    trace_kvm_create_vcpu(cpu->cpu_index, vcpu_id, kvm_fd); > +
  > +    return 0;
  > +}
  > +
  >   static int do_kvm_destroy_vcpu(CPUState *cpu)
  >   {
  >       KVMState *s = kvm_state;
  >       long mmap_size;
  > -    struct KVMParkedVcpu *vcpu = NULL;
  >       int ret = 0;
  >
  > -    trace_kvm_destroy_vcpu();
  > +    trace_kvm_destroy_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); >
  >       ret = kvm_arch_destroy_vcpu(cpu);
  >       if (ret < 0) {
  > @@ -373,10 +430,7 @@ static int do_kvm_destroy_vcpu(CPUState *cpu) >           }
  >       }
  >
  > -    vcpu = g_malloc0(sizeof(*vcpu));
  > -    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
  > -    vcpu->kvm_fd = cpu->kvm_fd;
  > -    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node); > +    kvm_park_vcpu(cpu);
  >   err:
  >       return ret;
  >   }
  > @@ -389,24 +443,6 @@ void kvm_destroy_vcpu(CPUState *cpu) >       }
  >   }
  >
  > -static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id) -{ > -    struct KVMParkedVcpu *cpu;
  > -
  > -    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
  > -        if (cpu->vcpu_id == vcpu_id) {
  > -            int kvm_fd;
  > -
  > -            QLIST_REMOVE(cpu, node);
  > -            kvm_fd = cpu->kvm_fd;
  > -            g_free(cpu);
  > -            return kvm_fd;
  > -        }
  > -    }
  > -
  > -    return kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)vcpu_id); > -}
  > -
  >   int kvm_init_vcpu(CPUState *cpu, Error **errp)
  >   {
  >       KVMState *s = kvm_state;
  > @@ -415,19 +451,14 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp) >
  >       trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu)); >
  > -    ret = kvm_get_vcpu(s, kvm_arch_vcpu_id(cpu));
  > +    ret = kvm_create_vcpu(cpu);
  >       if (ret < 0) {
  > -        error_setg_errno(errp, -ret, "kvm_init_vcpu:
kvm_get_vcpu failed
  (%lu)",
  > +        error_setg_errno(errp, -ret,
  > +                         "kvm_init_vcpu: kvm_create_vcpu failed > + (%lu)",
  >                            kvm_arch_vcpu_id(cpu));
  >           goto err;
  >       }
  >
  > -    cpu->kvm_fd = ret;
  > -    cpu->kvm_state = s;
  > -    cpu->vcpu_dirty = true;
  > -    cpu->dirty_pages = 0;
  > -    cpu->throttle_us_per_full = 0;
  > -
  >       mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0); >       if (mmap_size < 0) {
  >           ret = mmap_size;
  > diff --git a/accel/kvm/kvm-cpus.h b/accel/kvm/kvm-cpus.h index > ca40add32c..171b22fd29 100644
  > --- a/accel/kvm/kvm-cpus.h
  > +++ b/accel/kvm/kvm-cpus.h
  > @@ -22,5 +22,4 @@ bool kvm_supports_guest_debug(void);
  >   int kvm_insert_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr
  len);
  >   int kvm_remove_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr
  len);
  >   void kvm_remove_all_breakpoints(CPUState *cpu);
  > -
  >   #endif /* KVM_CPUS_H */
  > diff --git a/accel/kvm/trace-events b/accel/kvm/trace-events index > 681ccb667d..37626c1ac5 100644
  > --- a/accel/kvm/trace-events
  > +++ b/accel/kvm/trace-events
  > @@ -9,6 +9,10 @@ kvm_device_ioctl(int fd, int type, void *arg) "dev fd
  %d, type 0x%x, arg %p"
  >   kvm_failed_reg_get(uint64_t id, const char *msg) "Warning:
Unable to
  retrieve ONEREG %" PRIu64 " from KVM: %s"
  >   kvm_failed_reg_set(uint64_t id, const char *msg) "Warning:
Unable to
  set ONEREG %" PRIu64 " to KVM: %s"
  >   kvm_init_vcpu(int cpu_index, unsigned long arch_cpu_id) "index:
%d id:
  %lu"
  > +kvm_create_vcpu(int cpu_index, unsigned long arch_cpu_id, int kvm_fd) "index: %d, id: %lu, kvm fd: %d"
  > +kvm_destroy_vcpu(int cpu_index, unsigned long arch_cpu_id) "index:
  %d id: %lu"
  > +kvm_park_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d
  id: %lu"
  > +kvm_unpark_vcpu(unsigned long arch_cpu_id, const char *msg) "id:
%lu
  %s"
  >   kvm_irqchip_commit_routes(void) ""
  >   kvm_irqchip_add_msi_route(char *name, int vector, int virq) "dev %s
  vector %d virq %d"
  >   kvm_irqchip_update_msi_route(int virq) "Updating MSI route virq=%d"
  > @@ -25,7 +29,6 @@ kvm_dirty_ring_reaper(const char *s) "%s" >   kvm_dirty_ring_reap(uint64_t count, int64_t t) "reaped %"PRIu64" pages
  (took %"PRIi64" us)"
  >   kvm_dirty_ring_reaper_kick(const char *reason) "%s"
  >   kvm_dirty_ring_flush(int finished) "%d"
  > -kvm_destroy_vcpu(void) ""
  >   kvm_failed_get_vcpu_mmap_size(void) ""
  >   kvm_cpu_exec(void) ""
  >   kvm_interrupt_exit_request(void) ""
  > diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index > c31d9c7356..c4a914b3d8 100644
  > --- a/include/sysemu/kvm.h
  > +++ b/include/sysemu/kvm.h
  > @@ -313,6 +313,31 @@ int kvm_create_device(KVMState *s, uint64_t type, bool test);
  >    */
  >   bool kvm_device_supported(int vmfd, uint64_t type);
  >
  > +/**
  > + * kvm_create_vcpu - Gets a parked KVM vCPU or creates a KVM vCPU > + * @cpu: QOM CPUState object for which KVM vCPU has to be fetched/created.
  > + *
  > + * @returns: 0 when success, errno (<0) when failed.
  > + */
  > +int kvm_create_vcpu(CPUState *cpu);
  > +
  > +/**
  > + * kvm_park_vcpu - Park QEMU KVM vCPU context
  > + * @cpu: QOM CPUState object for which QEMU KVM vCPU context has to be parked.
  > + *
  > + * @returns: none
  > + */
  > +void kvm_park_vcpu(CPUState *cpu);
  > +
  > +/**
  > + * kvm_unpark_vcpu - unpark QEMU KVM vCPU context
  > + * @s: KVM State
  > + * @vcpu_id: Architecture vCPU ID of the parked vCPU
  > + *
  > + * @returns: KVM fd
  > + */
  > +int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id); > +
  >   /* Arch specific hooks */
  >
  >   extern const KVMCapabilityInfo kvm_arch_required_capabilities[];

#7 lore raw

Re: [PATCH V13 1/8] accel/kvm: Extract common KVM vCPU {creation,parking} code

In reply to Salil Mehta
Quoted 86 lines · On Fri, 7 Jun 2024 12:56:42 +0100 Salil Mehta <[email protected]> wrote:

KVM vCPU creation is done once during the vCPU realization when Qemu vCPU thread is spawned. This is common to all the architectures as of now.

Hot-unplug of vCPU results in destruction of the vCPU object in QOM but the corresponding KVM vCPU object in the Host KVM is not destroyed as KVM doesn't support vCPU removal. Therefore, its representative KVM vCPU object/context in Qemu is parked.

Refactor architecture common logic so that some APIs could be reused by vCPU Hotplug code of some architectures likes ARM, Loongson etc. Update new/old APIs with trace events. No functional change is intended here.

---
 accel/kvm/kvm-all.c    | 95 ++++++++++++++++++++++++++++--------------
 accel/kvm/kvm-cpus.h   |  1 -
 accel/kvm/trace-events |  5 ++-
 include/sysemu/kvm.h   | 25 +++++++++++
 4 files changed, 92 insertions(+), 34 deletions(-)
accel/kvm/kvm-all.c +47 -0
index c0be9f5eed..8f9128bb92 100644
@@ -340,14 +340,71 @@ err:
     return ret;
 }
 
+void kvm_park_vcpu(CPUState *cpu)
+{
+    struct KVMParkedVcpu *vcpu;
+
+    trace_kvm_park_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
+
+    vcpu = g_malloc0(sizeof(*vcpu));
+    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
+    vcpu->kvm_fd = cpu->kvm_fd;
+    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node);
+}
+
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id)
+{
+    struct KVMParkedVcpu *cpu;
+    int kvm_fd = -ENOENT;
+
+    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
+        if (cpu->vcpu_id == vcpu_id) {
+            QLIST_REMOVE(cpu, node);
+            kvm_fd = cpu->kvm_fd;
+            g_free(cpu);
+        }
+    }
+
+    trace_kvm_unpark_vcpu(vcpu_id, kvm_fd > 0 ? "unparked" : "not found parked");
+
+    return kvm_fd;
+}
+
+int kvm_create_vcpu(CPUState *cpu)
+{
+    unsigned long vcpu_id = kvm_arch_vcpu_id(cpu);
+    KVMState *s = kvm_state;
+    int kvm_fd;
+
+    /* check if the KVM vCPU already exist but is parked */
+    kvm_fd = kvm_unpark_vcpu(s, vcpu_id);
+    if (kvm_fd < 0) {
+        /* vCPU not parked: create a new KVM vCPU */
+        kvm_fd = kvm_vm_ioctl(s, KVM_CREATE_VCPU, vcpu_id);
+        if (kvm_fd < 0) {
+            error_report("KVM_CREATE_VCPU IOCTL failed for vCPU %lu", vcpu_id);
+            return kvm_fd;
+        }
+    }
+

accel/kvm/kvm-all.c err: · line 399

Quoted 10 lines
accel/kvm/kvm-all.c +10 -0
+    cpu->kvm_fd = kvm_fd;
+    cpu->kvm_state = s;
+    cpu->vcpu_dirty = true;
+    cpu->dirty_pages = 0;
+    cpu->throttle_us_per_full = 0;
+
+    trace_kvm_create_vcpu(cpu->cpu_index, vcpu_id, kvm_fd);
+
+    return 0;
+}

Is there any reason why you are embedding/hiding kvm_state in new API instead of passing it as argument (all callers have it defined, so why not reuse that)?

otherwise patch lgtm

Quoted 142 lines
accel/kvm/kvm-all.c +6 -32
+
 static int do_kvm_destroy_vcpu(CPUState *cpu)
 {
     KVMState *s = kvm_state;
     long mmap_size;
-    struct KVMParkedVcpu *vcpu = NULL;
     int ret = 0;
 
-    trace_kvm_destroy_vcpu();
+    trace_kvm_destroy_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
 
     ret = kvm_arch_destroy_vcpu(cpu);
     if (ret < 0) {
@@ -373,10 +430,7 @@ static int do_kvm_destroy_vcpu(CPUState *cpu)
         }
     }
 
-    vcpu = g_malloc0(sizeof(*vcpu));
-    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
-    vcpu->kvm_fd = cpu->kvm_fd;
-    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node);
+    kvm_park_vcpu(cpu);
 err:
     return ret;
 }
@@ -389,24 +443,6 @@ void kvm_destroy_vcpu(CPUState *cpu)
     }
 }
 
-static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id)
-{
-    struct KVMParkedVcpu *cpu;
-
-    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
-        if (cpu->vcpu_id == vcpu_id) {
-            int kvm_fd;
-
-            QLIST_REMOVE(cpu, node);
-            kvm_fd = cpu->kvm_fd;
-            g_free(cpu);
-            return kvm_fd;
-        }
-    }
-
-    return kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)vcpu_id);
-}
-
 int kvm_init_vcpu(CPUState *cpu, Error **errp)
 {
     KVMState *s = kvm_state;
@@ -415,19 +451,14 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp)
 
     trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
 
-    ret = kvm_get_vcpu(s, kvm_arch_vcpu_id(cpu));
+    ret = kvm_create_vcpu(cpu);
     if (ret < 0) {
-        error_setg_errno(errp, -ret, "kvm_init_vcpu: kvm_get_vcpu failed (%lu)",
+        error_setg_errno(errp, -ret,
+                         "kvm_init_vcpu: kvm_create_vcpu failed (%lu)",
                          kvm_arch_vcpu_id(cpu));
         goto err;
     }
 
-    cpu->kvm_fd = ret;
-    cpu->kvm_state = s;
-    cpu->vcpu_dirty = true;
-    cpu->dirty_pages = 0;
-    cpu->throttle_us_per_full = 0;
-
     mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
     if (mmap_size < 0) {
         ret = mmap_size;
accel/kvm/kvm-cpus.h +0 -1
index ca40add32c..171b22fd29 100644
@@ -22,5 +22,4 @@ bool kvm_supports_guest_debug(void);
 int kvm_insert_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len);
 int kvm_remove_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len);
 void kvm_remove_all_breakpoints(CPUState *cpu);
-
 #endif /* KVM_CPUS_H */
accel/kvm/trace-events +4 -1
index 681ccb667d..37626c1ac5 100644
@@ -9,6 +9,10 @@ kvm_device_ioctl(int fd, int type, void *arg) "dev fd %d, type 0x%x, arg %p"
 kvm_failed_reg_get(uint64_t id, const char *msg) "Warning: Unable to retrieve ONEREG %" PRIu64 " from KVM: %s"
 kvm_failed_reg_set(uint64_t id, const char *msg) "Warning: Unable to set ONEREG %" PRIu64 " to KVM: %s"
 kvm_init_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id: %lu"
+kvm_create_vcpu(int cpu_index, unsigned long arch_cpu_id, int kvm_fd) "index: %d, id: %lu, kvm fd: %d"
+kvm_destroy_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id: %lu"
+kvm_park_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id: %lu"
+kvm_unpark_vcpu(unsigned long arch_cpu_id, const char *msg) "id: %lu %s"
 kvm_irqchip_commit_routes(void) ""
 kvm_irqchip_add_msi_route(char *name, int vector, int virq) "dev %s vector %d virq %d"
 kvm_irqchip_update_msi_route(int virq) "Updating MSI route virq=%d"
@@ -25,7 +29,6 @@ kvm_dirty_ring_reaper(const char *s) "%s"
 kvm_dirty_ring_reap(uint64_t count, int64_t t) "reaped %"PRIu64" pages (took %"PRIi64" us)"
 kvm_dirty_ring_reaper_kick(const char *reason) "%s"
 kvm_dirty_ring_flush(int finished) "%d"
-kvm_destroy_vcpu(void) ""
 kvm_failed_get_vcpu_mmap_size(void) ""
 kvm_cpu_exec(void) ""
 kvm_interrupt_exit_request(void) ""
include/sysemu/kvm.h +25 -0
index c31d9c7356..c4a914b3d8 100644
@@ -313,6 +313,31 @@ int kvm_create_device(KVMState *s, uint64_t type, bool test);
  */
 bool kvm_device_supported(int vmfd, uint64_t type);
 
+/**
+ * kvm_create_vcpu - Gets a parked KVM vCPU or creates a KVM vCPU
+ * @cpu: QOM CPUState object for which KVM vCPU has to be fetched/created.
+ *
+ * @returns: 0 when success, errno (<0) when failed.
+ */
+int kvm_create_vcpu(CPUState *cpu);
+
+/**
+ * kvm_park_vcpu - Park QEMU KVM vCPU context
+ * @cpu: QOM CPUState object for which QEMU KVM vCPU context has to be parked.
+ *
+ * @returns: none
+ */
+void kvm_park_vcpu(CPUState *cpu);
+
+/**
+ * kvm_unpark_vcpu - unpark QEMU KVM vCPU context
+ * @s: KVM State
+ * @vcpu_id: Architecture vCPU ID of the parked vCPU
+ *
+ * @returns: KVM fd
+ */
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id);
+
 /* Arch specific hooks */
 
 extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
#8 lore raw

Re: [PATCH V13 1/8] accel/kvm: Extract common KVM vCPU {creation,parking} code

Hi Igor,
Thanks for taking out time to review.

Quoted 103 lines · Igor Mammedov
Quoted 1 line · On Fri, 7 Jun 2024 12:56:42 +0100 Salil Mehta <[email protected]> wrote:

KVM vCPU creation is done once during the vCPU realization when Qemu

vCPU thread

Quoted 3 lines

is spawned. This is common to all the architectures as of now.

Hot-unplug of vCPU results in destruction of the vCPU object in QOM but

the

Quoted 1 line

corresponding KVM vCPU object in the Host KVM is not destroyed as KVM

doesn't

Quoted 1 line

support vCPU removal. Therefore, its representative KVM vCPU

object/context in

Quoted 3 lines

Qemu is parked.

Refactor architecture common logic so that some APIs could be reused by

vCPU

Quoted 1 line

Hotplug code of some architectures likes ARM, Loongson etc. Update

new/old APIs

Quoted 45 lines

with trace events. No functional change is intended here.

---
 accel/kvm/kvm-all.c    | 95 ++++++++++++++++++++++++++++--------------
 accel/kvm/kvm-cpus.h   |  1 -
 accel/kvm/trace-events |  5 ++-
 include/sysemu/kvm.h   | 25 +++++++++++
 4 files changed, 92 insertions(+), 34 deletions(-)
accel/kvm/kvm-all.c +16 -0
index c0be9f5eed..8f9128bb92 100644
@@ -340,14 +340,71 @@ err:
     return ret;
 }
 
+void kvm_park_vcpu(CPUState *cpu)
+{
+    struct KVMParkedVcpu *vcpu;
+
+    trace_kvm_park_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
+
+    vcpu = g_malloc0(sizeof(*vcpu));
+    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
+    vcpu->kvm_fd = cpu->kvm_fd;
+    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node);
+}
+
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id)
+{
+    struct KVMParkedVcpu *cpu;
+    int kvm_fd = -ENOENT;

accel/kvm/kvm-all.c err: · line 368

Quoted 10 lines
accel/kvm/kvm-all.c +10 -0
+
+    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
+        if (cpu->vcpu_id == vcpu_id) {
+            QLIST_REMOVE(cpu, node);
+            kvm_fd = cpu->kvm_fd;
+            g_free(cpu);
+        }
+    }
+
+    trace_kvm_unpark_vcpu(vcpu_id, kvm_fd > 0 ? "unparked" : "not found

parked");

Quoted 7 lines
accel/kvm/kvm-all.c +7 -0
+
+    return kvm_fd;
+}
+
+int kvm_create_vcpu(CPUState *cpu)
+{
+    unsigned long vcpu_id = kvm_arch_vcpu_id(cpu);

accel/kvm/kvm-all.c err:

Quoted 10 lines
accel/kvm/kvm-all.c +10 -0
+    KVMState *s = kvm_state;
+    int kvm_fd;
+
+    /* check if the KVM vCPU already exist but is parked */
+    kvm_fd = kvm_unpark_vcpu(s, vcpu_id);
+    if (kvm_fd < 0) {
+        /* vCPU not parked: create a new KVM vCPU */
+        kvm_fd = kvm_vm_ioctl(s, KVM_CREATE_VCPU, vcpu_id);
+        if (kvm_fd < 0) {
+            error_report("KVM_CREATE_VCPU IOCTL failed for vCPU %lu",

vcpu_id);

Quoted 10 lines
accel/kvm/kvm-all.c +10 -0
+            return kvm_fd;
+        }
+    }
+
+    cpu->kvm_fd = kvm_fd;
+    cpu->kvm_state = s;
+    cpu->vcpu_dirty = true;
+    cpu->dirty_pages = 0;
+    cpu->throttle_us_per_full = 0;
+
Quoted 9 lines

accel/kvm/kvm-all.c err: · line 399

Quoted 4 lines
accel/kvm/kvm-all.c +4 -0
+    trace_kvm_create_vcpu(cpu->cpu_index, vcpu_id, kvm_fd);
+
+    return 0;
+}

Is there any reason why you are embedding/hiding kvm_state in new API instead of passing it as argument (all callers have it defined, so why not reuse that)?

It is a global variable and I don't think it is a usual practice to specify the global variable
as an input parameter.

Quoted 2 lines

otherwise patch lgtm

May I request your Reviewed-by for this patch?

Thanks
Salil.

Quoted 161 lines
Quoted 53 lines
accel/kvm/kvm-all.c +3 -24
+
 static int do_kvm_destroy_vcpu(CPUState *cpu)
 {
     KVMState *s = kvm_state;
     long mmap_size;
-    struct KVMParkedVcpu *vcpu = NULL;
     int ret = 0;
 
-    trace_kvm_destroy_vcpu();
+    trace_kvm_destroy_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
 
     ret = kvm_arch_destroy_vcpu(cpu);
     if (ret < 0) {
@@ -373,10 +430,7 @@ static int do_kvm_destroy_vcpu(CPUState *cpu)
         }
     }
 
-    vcpu = g_malloc0(sizeof(*vcpu));
-    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
-    vcpu->kvm_fd = cpu->kvm_fd;
-    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node);
+    kvm_park_vcpu(cpu);
 err:
     return ret;
 }
@@ -389,24 +443,6 @@ void kvm_destroy_vcpu(CPUState *cpu)
     }
 }
 
-static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id)
-{
-    struct KVMParkedVcpu *cpu;
-
-    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
-        if (cpu->vcpu_id == vcpu_id) {
-            int kvm_fd;
-
-            QLIST_REMOVE(cpu, node);
-            kvm_fd = cpu->kvm_fd;
-            g_free(cpu);
-            return kvm_fd;
-        }
-    }
-
-    return kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)vcpu_id);
-}
-
 int kvm_init_vcpu(CPUState *cpu, Error **errp)
 {
     KVMState *s = kvm_state;

accel/kvm/kvm-all.c int kvm_init_vcpu(CPUState *cpu, Error **errp) · line 420

Quoted 7 lines
accel/kvm/kvm-all.c +1 -2
@@ -415,19 +451,14 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp)
 
     trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
 
-    ret = kvm_get_vcpu(s, kvm_arch_vcpu_id(cpu));
+    ret = kvm_create_vcpu(cpu);
     if (ret < 0) {
-        error_setg_errno(errp, -ret, "kvm_init_vcpu: kvm_get_vcpu

failed (%lu)",

Quoted 20 lines
accel/kvm/kvm-all.c +2 -6
+        error_setg_errno(errp, -ret,
+                         "kvm_init_vcpu: kvm_create_vcpu failed (%lu)",
                          kvm_arch_vcpu_id(cpu));
         goto err;
     }
 
-    cpu->kvm_fd = ret;
-    cpu->kvm_state = s;
-    cpu->vcpu_dirty = true;
-    cpu->dirty_pages = 0;
-    cpu->throttle_us_per_full = 0;
-
     mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
     if (mmap_size < 0) {
         ret = mmap_size;
accel/kvm/kvm-cpus.h +0 -0
index ca40add32c..171b22fd29 100644

accel/kvm/kvm-cpus.h bool kvm_supports_guest_debug(void); · line 22

Quoted 1 line
accel/kvm/kvm-cpus.h +0 -0
@@ -22,5 +22,4 @@ bool kvm_supports_guest_debug(void);
 int kvm_insert_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr

len);

accel/kvm/kvm-cpus.h bool kvm_supports_guest_debug(void); · line 23

Quoted 1 line
accel/kvm/kvm-cpus.h +0 -0
 int kvm_remove_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr

len);

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd

Quoted 8 lines
accel/kvm/kvm-cpus.h +0 -1
 void kvm_remove_all_breakpoints(CPUState *cpu);
-
 #endif /* KVM_CPUS_H */
accel/kvm/trace-events +0 -0
index 681ccb667d..37626c1ac5 100644
@@ -9,6 +9,10 @@ kvm_device_ioctl(int fd, int type, void *arg) "dev fd

%d, type 0x%x, arg %p"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 9

Quoted 1 line
accel/kvm/trace-events +0 -0
 kvm_failed_reg_get(uint64_t id, const char *msg) "Warning: Unable to

retrieve ONEREG %" PRIu64 " from KVM: %s"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 10

Quoted 1 line
accel/kvm/trace-events +0 -0
 kvm_failed_reg_set(uint64_t id, const char *msg) "Warning: Unable to

set ONEREG %" PRIu64 " to KVM: %s"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 11

Quoted 1 line
accel/kvm/trace-events +0 -0
 kvm_init_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id:

%lu"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 12

Quoted 1 line
accel/kvm/trace-events +1 -0
+kvm_create_vcpu(int cpu_index, unsigned long arch_cpu_id, int kvm_fd)

"index: %d, id: %lu, kvm fd: %d"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 13

Quoted 1 line
accel/kvm/trace-events +1 -0
+kvm_destroy_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d

id: %lu"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 14

Quoted 1 line
accel/kvm/trace-events +1 -0
+kvm_park_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id:

%lu"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd %d, type 0x%x, arg %p"

Quoted 3 lines
accel/kvm/trace-events +1 -0
+kvm_unpark_vcpu(unsigned long arch_cpu_id, const char *msg) "id: %lu %s"
 kvm_irqchip_commit_routes(void) ""
 kvm_irqchip_add_msi_route(char *name, int vector, int virq) "dev %s

vector %d virq %d"

accel/kvm/trace-events kvm_dirty_ring_reaper(const char *s) "%s" · line 29

Quoted 3 lines
accel/kvm/trace-events +0 -0
 kvm_irqchip_update_msi_route(int virq) "Updating MSI route virq=%d"
@@ -25,7 +29,6 @@ kvm_dirty_ring_reaper(const char *s) "%s"
 kvm_dirty_ring_reap(uint64_t count, int64_t t) "reaped %"PRIu64" pages

(took %"PRIi64" us)"

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type,

Quoted 11 lines
accel/kvm/trace-events +0 -1
 kvm_dirty_ring_reaper_kick(const char *reason) "%s"
 kvm_dirty_ring_flush(int finished) "%d"
-kvm_destroy_vcpu(void) ""
 kvm_failed_get_vcpu_mmap_size(void) ""
 kvm_cpu_exec(void) ""
 kvm_interrupt_exit_request(void) ""
include/sysemu/kvm.h +0 -0
index c31d9c7356..c4a914b3d8 100644
@@ -313,6 +313,31 @@ int kvm_create_device(KVMState *s, uint64_t type,

bool test);

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type, bool test);

Quoted 6 lines
include/sysemu/kvm.h +3 -0
  */
 bool kvm_device_supported(int vmfd, uint64_t type);
 
+/**
+ * kvm_create_vcpu - Gets a parked KVM vCPU or creates a KVM vCPU
+ * @cpu: QOM CPUState object for which KVM vCPU has to be

fetched/created.

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type, bool test);

Quoted 8 lines
include/sysemu/kvm.h +8 -0
+ *
+ * @returns: 0 when success, errno (<0) when failed.
+ */
+int kvm_create_vcpu(CPUState *cpu);
+
+/**
+ * kvm_park_vcpu - Park QEMU KVM vCPU context
+ * @cpu: QOM CPUState object for which QEMU KVM vCPU context has to be

parked.

Quoted 17 lines
include/sysemu/kvm.h +14 -0
+ *
+ * @returns: none
+ */
+void kvm_park_vcpu(CPUState *cpu);
+
+/**
+ * kvm_unpark_vcpu - unpark QEMU KVM vCPU context
+ * @s: KVM State
+ * @vcpu_id: Architecture vCPU ID of the parked vCPU
+ *
+ * @returns: KVM fd
+ */
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id);
+
 /* Arch specific hooks */
 
 extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
#9 lore raw

Re: [PATCH V13 1/8] accel/kvm: Extract common KVM vCPU {creation,parking} code

Quoted 118 lines · On Sat, 6 Jul 2024 15:43:01 +0000 Salil Mehta <[email protected]> wrote:

Hi Igor,
Thanks for taking out time to review.

Quoted 103 lines · Igor Mammedov
Quoted 1 line · On Fri, 7 Jun 2024 12:56:42 +0100 Salil Mehta <[email protected]> wrote:

KVM vCPU creation is done once during the vCPU realization when Qemu

vCPU thread

Quoted 3 lines

is spawned. This is common to all the architectures as of now.

Hot-unplug of vCPU results in destruction of the vCPU object in QOM but

the

Quoted 1 line

corresponding KVM vCPU object in the Host KVM is not destroyed as KVM

doesn't

Quoted 1 line

support vCPU removal. Therefore, its representative KVM vCPU

object/context in

Quoted 3 lines

Qemu is parked.

Refactor architecture common logic so that some APIs could be reused by

vCPU

Quoted 1 line

Hotplug code of some architectures likes ARM, Loongson etc. Update

new/old APIs

Quoted 45 lines

with trace events. No functional change is intended here.

---
 accel/kvm/kvm-all.c    | 95 ++++++++++++++++++++++++++++--------------
 accel/kvm/kvm-cpus.h   |  1 -
 accel/kvm/trace-events |  5 ++-
 include/sysemu/kvm.h   | 25 +++++++++++
 4 files changed, 92 insertions(+), 34 deletions(-)
accel/kvm/kvm-all.c +16 -0
index c0be9f5eed..8f9128bb92 100644
@@ -340,14 +340,71 @@ err:
     return ret;
 }
 
+void kvm_park_vcpu(CPUState *cpu)
+{
+    struct KVMParkedVcpu *vcpu;
+
+    trace_kvm_park_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
+
+    vcpu = g_malloc0(sizeof(*vcpu));
+    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
+    vcpu->kvm_fd = cpu->kvm_fd;
+    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node);
+}
+
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id)
+{
+    struct KVMParkedVcpu *cpu;
+    int kvm_fd = -ENOENT;

accel/kvm/kvm-all.c err: · line 368

Quoted 10 lines
accel/kvm/kvm-all.c +10 -0
+
+    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
+        if (cpu->vcpu_id == vcpu_id) {
+            QLIST_REMOVE(cpu, node);
+            kvm_fd = cpu->kvm_fd;
+            g_free(cpu);
+        }
+    }
+
+    trace_kvm_unpark_vcpu(vcpu_id, kvm_fd > 0 ? "unparked" : "not found  

parked");

Quoted 7 lines
accel/kvm/kvm-all.c +7 -0
+
+    return kvm_fd;
+}
+
+int kvm_create_vcpu(CPUState *cpu)
+{
+    unsigned long vcpu_id = kvm_arch_vcpu_id(cpu);

accel/kvm/kvm-all.c err:

Quoted 10 lines
accel/kvm/kvm-all.c +10 -0
+    KVMState *s = kvm_state;
+    int kvm_fd;
+
+    /* check if the KVM vCPU already exist but is parked */
+    kvm_fd = kvm_unpark_vcpu(s, vcpu_id);
+    if (kvm_fd < 0) {
+        /* vCPU not parked: create a new KVM vCPU */
+        kvm_fd = kvm_vm_ioctl(s, KVM_CREATE_VCPU, vcpu_id);
+        if (kvm_fd < 0) {
+            error_report("KVM_CREATE_VCPU IOCTL failed for vCPU %lu",  

vcpu_id);

Quoted 10 lines
accel/kvm/kvm-all.c +10 -0
+            return kvm_fd;
+        }
+    }
+
+    cpu->kvm_fd = kvm_fd;
+    cpu->kvm_state = s;
+    cpu->vcpu_dirty = true;
+    cpu->dirty_pages = 0;
+    cpu->throttle_us_per_full = 0;
+
Quoted 9 lines

accel/kvm/kvm-all.c err: · line 399

Quoted 4 lines
accel/kvm/kvm-all.c +4 -0
+    trace_kvm_create_vcpu(cpu->cpu_index, vcpu_id, kvm_fd);
+
+    return 0;
+}  

Is there any reason why you are embedding/hiding kvm_state in new API instead of passing it as argument (all callers have it defined, so why not reuse that)?

Quoted 3 lines

It is a global variable and I don't think it is a usual practice to specify the global variable
as an input parameter.

Ideally, global would be accessed once at API boundary entry and the passed as an argument to functions it calls.
It makes it easier to follow as opposed to mixed access we have now, which is harder to review since one has to check both flavors (argument passed or directly accessed).

in this patch kvm_init_vcpu() calls new kvm_create_vcpu()
and the former caches these global into 's' local variable,
so I'd reuse that local variable like kvm_get_vcpu() you are removing here did.

Quoted 174 lines
Quoted 5 lines

otherwise patch lgtm

May I request your Reviewed-by for this patch?

Thanks
Salil.

Quoted 161 lines
Quoted 53 lines
accel/kvm/kvm-all.c +3 -24
+
 static int do_kvm_destroy_vcpu(CPUState *cpu)
 {
     KVMState *s = kvm_state;
     long mmap_size;
-    struct KVMParkedVcpu *vcpu = NULL;
     int ret = 0;
 
-    trace_kvm_destroy_vcpu();
+    trace_kvm_destroy_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
 
     ret = kvm_arch_destroy_vcpu(cpu);
     if (ret < 0) {
@@ -373,10 +430,7 @@ static int do_kvm_destroy_vcpu(CPUState *cpu)
         }
     }
 
-    vcpu = g_malloc0(sizeof(*vcpu));
-    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
-    vcpu->kvm_fd = cpu->kvm_fd;
-    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node);
+    kvm_park_vcpu(cpu);
 err:
     return ret;
 }
@@ -389,24 +443,6 @@ void kvm_destroy_vcpu(CPUState *cpu)
     }
 }
 
-static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id)
-{
-    struct KVMParkedVcpu *cpu;
-
-    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
-        if (cpu->vcpu_id == vcpu_id) {
-            int kvm_fd;
-
-            QLIST_REMOVE(cpu, node);
-            kvm_fd = cpu->kvm_fd;
-            g_free(cpu);
-            return kvm_fd;
-        }
-    }
-
-    return kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)vcpu_id);
-}
-
 int kvm_init_vcpu(CPUState *cpu, Error **errp)
 {
     KVMState *s = kvm_state;

accel/kvm/kvm-all.c int kvm_init_vcpu(CPUState *cpu, Error **errp) · line 420

Quoted 7 lines
accel/kvm/kvm-all.c +1 -2
@@ -415,19 +451,14 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp)
 
     trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
 
-    ret = kvm_get_vcpu(s, kvm_arch_vcpu_id(cpu));
+    ret = kvm_create_vcpu(cpu);
     if (ret < 0) {
-        error_setg_errno(errp, -ret, "kvm_init_vcpu: kvm_get_vcpu  

failed (%lu)",

Quoted 20 lines
accel/kvm/kvm-all.c +2 -6
+        error_setg_errno(errp, -ret,
+                         "kvm_init_vcpu: kvm_create_vcpu failed (%lu)",
                          kvm_arch_vcpu_id(cpu));
         goto err;
     }
 
-    cpu->kvm_fd = ret;
-    cpu->kvm_state = s;
-    cpu->vcpu_dirty = true;
-    cpu->dirty_pages = 0;
-    cpu->throttle_us_per_full = 0;
-
     mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
     if (mmap_size < 0) {
         ret = mmap_size;
accel/kvm/kvm-cpus.h +0 -0
index ca40add32c..171b22fd29 100644

accel/kvm/kvm-cpus.h bool kvm_supports_guest_debug(void); · line 22

Quoted 1 line
accel/kvm/kvm-cpus.h +0 -0
@@ -22,5 +22,4 @@ bool kvm_supports_guest_debug(void);
 int kvm_insert_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr  

len);

accel/kvm/kvm-cpus.h bool kvm_supports_guest_debug(void); · line 23

Quoted 1 line
accel/kvm/kvm-cpus.h +0 -0
 int kvm_remove_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr  

len);

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd

Quoted 8 lines
accel/kvm/kvm-cpus.h +0 -1
 void kvm_remove_all_breakpoints(CPUState *cpu);
-
 #endif /* KVM_CPUS_H */
accel/kvm/trace-events +0 -0
index 681ccb667d..37626c1ac5 100644
@@ -9,6 +9,10 @@ kvm_device_ioctl(int fd, int type, void *arg) "dev fd  

%d, type 0x%x, arg %p"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 9

Quoted 1 line
accel/kvm/trace-events +0 -0
 kvm_failed_reg_get(uint64_t id, const char *msg) "Warning: Unable to  

retrieve ONEREG %" PRIu64 " from KVM: %s"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 10

Quoted 1 line
accel/kvm/trace-events +0 -0
 kvm_failed_reg_set(uint64_t id, const char *msg) "Warning: Unable to  

set ONEREG %" PRIu64 " to KVM: %s"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 11

Quoted 1 line
accel/kvm/trace-events +0 -0
 kvm_init_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id:  

%lu"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 12

Quoted 1 line
accel/kvm/trace-events +1 -0
+kvm_create_vcpu(int cpu_index, unsigned long arch_cpu_id, int kvm_fd)  

"index: %d, id: %lu, kvm fd: %d"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 13

Quoted 1 line
accel/kvm/trace-events +1 -0
+kvm_destroy_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d  

id: %lu"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 14

Quoted 1 line
accel/kvm/trace-events +1 -0
+kvm_park_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id:  

%lu"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd %d, type 0x%x, arg %p"

Quoted 3 lines
accel/kvm/trace-events +1 -0
+kvm_unpark_vcpu(unsigned long arch_cpu_id, const char *msg) "id: %lu %s"
 kvm_irqchip_commit_routes(void) ""
 kvm_irqchip_add_msi_route(char *name, int vector, int virq) "dev %s  

vector %d virq %d"

accel/kvm/trace-events kvm_dirty_ring_reaper(const char *s) "%s" · line 29

Quoted 3 lines
accel/kvm/trace-events +0 -0
 kvm_irqchip_update_msi_route(int virq) "Updating MSI route virq=%d"
@@ -25,7 +29,6 @@ kvm_dirty_ring_reaper(const char *s) "%s"
 kvm_dirty_ring_reap(uint64_t count, int64_t t) "reaped %"PRIu64" pages  

(took %"PRIi64" us)"

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type,

Quoted 11 lines
accel/kvm/trace-events +0 -1
 kvm_dirty_ring_reaper_kick(const char *reason) "%s"
 kvm_dirty_ring_flush(int finished) "%d"
-kvm_destroy_vcpu(void) ""
 kvm_failed_get_vcpu_mmap_size(void) ""
 kvm_cpu_exec(void) ""
 kvm_interrupt_exit_request(void) ""
include/sysemu/kvm.h +0 -0
index c31d9c7356..c4a914b3d8 100644
@@ -313,6 +313,31 @@ int kvm_create_device(KVMState *s, uint64_t type,  

bool test);

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type, bool test);

Quoted 6 lines
include/sysemu/kvm.h +3 -0
  */
 bool kvm_device_supported(int vmfd, uint64_t type);
 
+/**
+ * kvm_create_vcpu - Gets a parked KVM vCPU or creates a KVM vCPU
+ * @cpu: QOM CPUState object for which KVM vCPU has to be  

fetched/created.

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type, bool test);

Quoted 8 lines
include/sysemu/kvm.h +8 -0
+ *
+ * @returns: 0 when success, errno (<0) when failed.
+ */
+int kvm_create_vcpu(CPUState *cpu);
+
+/**
+ * kvm_park_vcpu - Park QEMU KVM vCPU context
+ * @cpu: QOM CPUState object for which QEMU KVM vCPU context has to be  

parked.

Quoted 17 lines
include/sysemu/kvm.h +14 -0
+ *
+ * @returns: none
+ */
+void kvm_park_vcpu(CPUState *cpu);
+
+/**
+ * kvm_unpark_vcpu - unpark QEMU KVM vCPU context
+ * @s: KVM State
+ * @vcpu_id: Architecture vCPU ID of the parked vCPU
+ *
+ * @returns: KVM fd
+ */
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id);
+
 /* Arch specific hooks */
 
 extern const KVMCapabilityInfo kvm_arch_required_capabilities[];  
#10 lore raw

Re: [PATCH V13 1/8] accel/kvm: Extract common KVM vCPU {creation,parking} code

Hi Igor,

Quoted 128 lines · Igor Mammedov
Quoted 116 lines · On Sat, 6 Jul 2024 15:43:01 +0000 Salil Mehta <[email protected]> wrote:

Hi Igor,
Thanks for taking out time to review.

Quoted 102 lines · Igor Mammedov
Quoted 1 line · On Fri, 7 Jun 2024 12:56:42 +0100 Salil Mehta <[email protected]> wrote:

KVM vCPU creation is done once during the vCPU realization when Qemu

vCPU thread

Quoted 3 lines

is spawned. This is common to all the architectures as of now.

Hot-unplug of vCPU results in destruction of the vCPU object in QOM but

the

Quoted 1 line

corresponding KVM vCPU object in the Host KVM is not destroyed as KVM

doesn't

Quoted 1 line

support vCPU removal. Therefore, its representative KVM vCPU

object/context in

Quoted 3 lines

Qemu is parked.

Refactor architecture common logic so that some APIs could be reused by

vCPU

Quoted 1 line

Hotplug code of some architectures likes ARM, Loongson etc. Update

new/old APIs

Quoted 45 lines

with trace events. No functional change is intended here.

---
  accel/kvm/kvm-all.c    | 95 ++++++++++++++++++++++++++++--------------
  accel/kvm/kvm-cpus.h   |  1 -
  accel/kvm/trace-events |  5 ++-
  include/sysemu/kvm.h   | 25 +++++++++++
  4 files changed, 92 insertions(+), 34 deletions(-)
accel/kvm/kvm-all.c +16 -0
index c0be9f5eed..8f9128bb92 100644
@@ -340,14 +340,71 @@ err:
      return ret;
  }
 
+void kvm_park_vcpu(CPUState *cpu)
+{
+    struct KVMParkedVcpu *vcpu;
+
+    trace_kvm_park_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
+
+    vcpu = g_malloc0(sizeof(*vcpu));
+    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
+    vcpu->kvm_fd = cpu->kvm_fd;
+    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node);
+}
+
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id)
+{
+    struct KVMParkedVcpu *cpu;
+    int kvm_fd = -ENOENT;

accel/kvm/kvm-all.c err: · line 368

Quoted 10 lines
accel/kvm/kvm-all.c +10 -0
+
+    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
+        if (cpu->vcpu_id == vcpu_id) {
+            QLIST_REMOVE(cpu, node);
+            kvm_fd = cpu->kvm_fd;
+            g_free(cpu);
+        }
+    }
+
+    trace_kvm_unpark_vcpu(vcpu_id, kvm_fd > 0 ? "unparked" : "not found

parked");

Quoted 7 lines
accel/kvm/kvm-all.c +7 -0
+
+    return kvm_fd;
+}
+
+int kvm_create_vcpu(CPUState *cpu)
+{
+    unsigned long vcpu_id = kvm_arch_vcpu_id(cpu);

accel/kvm/kvm-all.c err:

Quoted 10 lines
accel/kvm/kvm-all.c +10 -0
+    KVMState *s = kvm_state;
+    int kvm_fd;
+
+    /* check if the KVM vCPU already exist but is parked */
+    kvm_fd = kvm_unpark_vcpu(s, vcpu_id);
+    if (kvm_fd < 0) {
+        /* vCPU not parked: create a new KVM vCPU */
+        kvm_fd = kvm_vm_ioctl(s, KVM_CREATE_VCPU, vcpu_id);
+        if (kvm_fd < 0) {
+            error_report("KVM_CREATE_VCPU IOCTL failed for vCPU %lu",

vcpu_id);

Quoted 9 lines
accel/kvm/kvm-all.c +9 -0
+            return kvm_fd;
+        }
+    }
+
+    cpu->kvm_fd = kvm_fd;
+    cpu->kvm_state = s;
+    cpu->vcpu_dirty = true;
+    cpu->dirty_pages = 0;
+    cpu->throttle_us_per_full = 0;
Quoted 9 lines

accel/kvm/kvm-all.c err: · line 399

Quoted 5 lines
accel/kvm/kvm-all.c +5 -0
+
+    trace_kvm_create_vcpu(cpu->cpu_index, vcpu_id, kvm_fd);
+
+    return 0;
+}

Is there any reason why you are embedding/hiding kvm_state in new API instead of passing it as argument (all callers have it defined, so why not reuse that)?

Quoted 3 lines

It is a global variable and I don't think it is a usual practice to specify the global variable
as an input parameter.

Ideally, global would be accessed once at API boundary entry and the passed as an argument to functions it calls.
It makes it easier to follow as opposed to mixed access we have now, which is harder to review since one has to check both flavors (argument passed or directly accessed).

Quoted 3 lines

in this patch kvm_init_vcpu() calls new kvm_create_vcpu()
and the former caches these global into 's' local variable,
so I'd reuse that local variable like kvm_get_vcpu() you are removing here did.

That is one perspective, but:
1. kvm_create_vcpu() will also be called externally from other contexts. It would be awkward to pass this variable from those non-local places where it would seem unnecessary.
2. If you look at other symmetrical functions like kvm_destroy_vcpu(), they also have a similar prototype.

I think it is about doing a slight trade-off. If you really believe this change is necessary for us to proceed, I will make the adjustment. However, please note that it will affect the IBM team as well.

Hi Nick,

I hope you are okay with this suggested change?

Thanks,
Salil

Quoted 167 lines
Quoted 167 lines
Quoted 1 line

otherwise patch lgtm

May I request your Reviewed-by for this patch?

Thanks
Salil.

Quoted 159 lines
Quoted 52 lines
accel/kvm/kvm-all.c +3 -24
+
  static int do_kvm_destroy_vcpu(CPUState *cpu)
  {
      KVMState *s = kvm_state;
      long mmap_size;
-    struct KVMParkedVcpu *vcpu = NULL;
      int ret = 0;
 
-    trace_kvm_destroy_vcpu();
+    trace_kvm_destroy_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
 
      ret = kvm_arch_destroy_vcpu(cpu);
      if (ret < 0) {
@@ -373,10 +430,7 @@ static int do_kvm_destroy_vcpu(CPUState *cpu)
          }
      }
 
-    vcpu = g_malloc0(sizeof(*vcpu));
-    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
-    vcpu->kvm_fd = cpu->kvm_fd;
-    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node);
+    kvm_park_vcpu(cpu);
  err:
      return ret;
  }
@@ -389,24 +443,6 @@ void kvm_destroy_vcpu(CPUState *cpu)
      }
  }
 
-static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id)
-{
-    struct KVMParkedVcpu *cpu;
-
-    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
-        if (cpu->vcpu_id == vcpu_id) {
-            int kvm_fd;
-
-            QLIST_REMOVE(cpu, node);
-            kvm_fd = cpu->kvm_fd;
-            g_free(cpu);
-            return kvm_fd;
-        }
-    }
-
-    return kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)vcpu_id);
-}
-
  int kvm_init_vcpu(CPUState *cpu, Error **errp)
  {
      KVMState *s = kvm_state;

accel/kvm/kvm-all.c int kvm_init_vcpu(CPUState *cpu, Error **errp) · line 420

Quoted 7 lines
accel/kvm/kvm-all.c +1 -2
@@ -415,19 +451,14 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp)
 
      trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
 
-    ret = kvm_get_vcpu(s, kvm_arch_vcpu_id(cpu));
+    ret = kvm_create_vcpu(cpu);
      if (ret < 0) {
-        error_setg_errno(errp, -ret, "kvm_init_vcpu: kvm_get_vcpu

failed (%lu)",

Quoted 20 lines
accel/kvm/kvm-all.c +2 -6
+        error_setg_errno(errp, -ret,
+                         "kvm_init_vcpu: kvm_create_vcpu failed (%lu)",
                           kvm_arch_vcpu_id(cpu));
          goto err;
      }
 
-    cpu->kvm_fd = ret;
-    cpu->kvm_state = s;
-    cpu->vcpu_dirty = true;
-    cpu->dirty_pages = 0;
-    cpu->throttle_us_per_full = 0;
-
      mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
      if (mmap_size < 0) {
          ret = mmap_size;
accel/kvm/kvm-cpus.h +0 -0
index ca40add32c..171b22fd29 100644

accel/kvm/kvm-cpus.h bool kvm_supports_guest_debug(void); · line 22

Quoted 1 line
accel/kvm/kvm-cpus.h +0 -0
@@ -22,5 +22,4 @@ bool kvm_supports_guest_debug(void);
  int kvm_insert_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr

len);

accel/kvm/kvm-cpus.h bool kvm_supports_guest_debug(void); · line 23

Quoted 1 line
accel/kvm/kvm-cpus.h +0 -0
  int kvm_remove_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr

len);

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd

Quoted 8 lines
accel/kvm/kvm-cpus.h +0 -1
  void kvm_remove_all_breakpoints(CPUState *cpu);
-
  #endif /* KVM_CPUS_H */
accel/kvm/trace-events +0 -0
index 681ccb667d..37626c1ac5 100644
@@ -9,6 +9,10 @@ kvm_device_ioctl(int fd, int type, void *arg) "dev fd

%d, type 0x%x, arg %p"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 9

Quoted 1 line
accel/kvm/trace-events +0 -0
  kvm_failed_reg_get(uint64_t id, const char *msg) "Warning: Unable to

retrieve ONEREG %" PRIu64 " from KVM: %s"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 10

Quoted 1 line
accel/kvm/trace-events +0 -0
  kvm_failed_reg_set(uint64_t id, const char *msg) "Warning: Unable to

set ONEREG %" PRIu64 " to KVM: %s"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 11

Quoted 1 line
accel/kvm/trace-events +0 -0
  kvm_init_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id:

%lu"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 12

Quoted 1 line
accel/kvm/trace-events +1 -0
+kvm_create_vcpu(int cpu_index, unsigned long arch_cpu_id, int kvm_fd)

"index: %d, id: %lu, kvm fd: %d"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 13

Quoted 1 line
accel/kvm/trace-events +1 -0
+kvm_destroy_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d

id: %lu"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 14

Quoted 1 line
accel/kvm/trace-events +1 -0
+kvm_park_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id:

%lu"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd %d, type 0x%x, arg %p"

Quoted 3 lines
accel/kvm/trace-events +1 -0
+kvm_unpark_vcpu(unsigned long arch_cpu_id, const char *msg) "id: %lu %s"
  kvm_irqchip_commit_routes(void) ""
  kvm_irqchip_add_msi_route(char *name, int vector, int virq) "dev %s

vector %d virq %d"

accel/kvm/trace-events kvm_dirty_ring_reaper(const char *s) "%s" · line 29

Quoted 3 lines
accel/kvm/trace-events +0 -0
  kvm_irqchip_update_msi_route(int virq) "Updating MSI route virq=%d"
@@ -25,7 +29,6 @@ kvm_dirty_ring_reaper(const char *s) "%s"
  kvm_dirty_ring_reap(uint64_t count, int64_t t) "reaped %"PRIu64" pages

(took %"PRIi64" us)"

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type,

Quoted 11 lines
accel/kvm/trace-events +0 -1
  kvm_dirty_ring_reaper_kick(const char *reason) "%s"
  kvm_dirty_ring_flush(int finished) "%d"
-kvm_destroy_vcpu(void) ""
  kvm_failed_get_vcpu_mmap_size(void) ""
  kvm_cpu_exec(void) ""
  kvm_interrupt_exit_request(void) ""
include/sysemu/kvm.h +0 -0
index c31d9c7356..c4a914b3d8 100644
@@ -313,6 +313,31 @@ int kvm_create_device(KVMState *s, uint64_t type,

bool test);

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type, bool test);

Quoted 6 lines
include/sysemu/kvm.h +3 -0
   */
  bool kvm_device_supported(int vmfd, uint64_t type);
 
+/**
+ * kvm_create_vcpu - Gets a parked KVM vCPU or creates a KVM vCPU
+ * @cpu: QOM CPUState object for which KVM vCPU has to be

fetched/created.

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type, bool test);

Quoted 8 lines
include/sysemu/kvm.h +8 -0
+ *
+ * @returns: 0 when success, errno (<0) when failed.
+ */
+int kvm_create_vcpu(CPUState *cpu);
+
+/**
+ * kvm_park_vcpu - Park QEMU KVM vCPU context
+ * @cpu: QOM CPUState object for which QEMU KVM vCPU context has to be

parked.

Quoted 17 lines
include/sysemu/kvm.h +14 -0
+ *
+ * @returns: none
+ */
+void kvm_park_vcpu(CPUState *cpu);
+
+/**
+ * kvm_unpark_vcpu - unpark QEMU KVM vCPU context
+ * @s: KVM State
+ * @vcpu_id: Architecture vCPU ID of the parked vCPU
+ *
+ * @returns: KVM fd
+ */
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id);
+
  /* Arch specific hooks */
 
  extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
#11 lore raw

Re: [PATCH V13 1/8] accel/kvm: Extract common KVM vCPU {creation,parking} code

Quoted 131 lines · On Mon, 8 Jul 2024 23:30:01 +0000 Salil Mehta <[email protected]> wrote:

Hi Igor,

Quoted 128 lines · Igor Mammedov
Quoted 116 lines · On Sat, 6 Jul 2024 15:43:01 +0000 Salil Mehta <[email protected]> wrote:

Hi Igor,
Thanks for taking out time to review.

Quoted 102 lines · Igor Mammedov
Quoted 1 line · On Fri, 7 Jun 2024 12:56:42 +0100 Salil Mehta <[email protected]> wrote:

KVM vCPU creation is done once during the vCPU realization when Qemu

vCPU thread

Quoted 3 lines

is spawned. This is common to all the architectures as of now.

Hot-unplug of vCPU results in destruction of the vCPU object in QOM but

the

Quoted 1 line

corresponding KVM vCPU object in the Host KVM is not destroyed as KVM

doesn't

Quoted 1 line

support vCPU removal. Therefore, its representative KVM vCPU

object/context in

Quoted 3 lines

Qemu is parked.

Refactor architecture common logic so that some APIs could be reused by

vCPU

Quoted 1 line

Hotplug code of some architectures likes ARM, Loongson etc. Update

new/old APIs

Quoted 45 lines

with trace events. No functional change is intended here.

---
  accel/kvm/kvm-all.c    | 95 ++++++++++++++++++++++++++++--------------
  accel/kvm/kvm-cpus.h   |  1 -
  accel/kvm/trace-events |  5 ++-
  include/sysemu/kvm.h   | 25 +++++++++++
  4 files changed, 92 insertions(+), 34 deletions(-)
accel/kvm/kvm-all.c +16 -0
index c0be9f5eed..8f9128bb92 100644
@@ -340,14 +340,71 @@ err:
      return ret;
  }
 
+void kvm_park_vcpu(CPUState *cpu)
+{
+    struct KVMParkedVcpu *vcpu;
+
+    trace_kvm_park_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
+
+    vcpu = g_malloc0(sizeof(*vcpu));
+    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
+    vcpu->kvm_fd = cpu->kvm_fd;
+    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node);
+}
+
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id)
+{
+    struct KVMParkedVcpu *cpu;
+    int kvm_fd = -ENOENT;

accel/kvm/kvm-all.c err: · line 368

Quoted 10 lines
accel/kvm/kvm-all.c +10 -0
+
+    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
+        if (cpu->vcpu_id == vcpu_id) {
+            QLIST_REMOVE(cpu, node);
+            kvm_fd = cpu->kvm_fd;
+            g_free(cpu);
+        }
+    }
+
+    trace_kvm_unpark_vcpu(vcpu_id, kvm_fd > 0 ? "unparked" : "not found  

parked");

Quoted 7 lines
accel/kvm/kvm-all.c +7 -0
+
+    return kvm_fd;
+}
+
+int kvm_create_vcpu(CPUState *cpu)
+{
+    unsigned long vcpu_id = kvm_arch_vcpu_id(cpu);

accel/kvm/kvm-all.c err:

Quoted 10 lines
accel/kvm/kvm-all.c +10 -0
+    KVMState *s = kvm_state;
+    int kvm_fd;
+
+    /* check if the KVM vCPU already exist but is parked */
+    kvm_fd = kvm_unpark_vcpu(s, vcpu_id);
+    if (kvm_fd < 0) {
+        /* vCPU not parked: create a new KVM vCPU */
+        kvm_fd = kvm_vm_ioctl(s, KVM_CREATE_VCPU, vcpu_id);
+        if (kvm_fd < 0) {
+            error_report("KVM_CREATE_VCPU IOCTL failed for vCPU %lu",  

vcpu_id);

Quoted 9 lines
accel/kvm/kvm-all.c +9 -0
+            return kvm_fd;
+        }
+    }
+
+    cpu->kvm_fd = kvm_fd;
+    cpu->kvm_state = s;
+    cpu->vcpu_dirty = true;
+    cpu->dirty_pages = 0;
+    cpu->throttle_us_per_full = 0;
Quoted 9 lines

accel/kvm/kvm-all.c err: · line 399

Quoted 5 lines
accel/kvm/kvm-all.c +5 -0
+
+    trace_kvm_create_vcpu(cpu->cpu_index, vcpu_id, kvm_fd);
+
+    return 0;
+}  

Is there any reason why you are embedding/hiding kvm_state in new API instead of passing it as argument (all callers have it defined, so why not reuse that)?

Quoted 3 lines

It is a global variable and I don't think it is a usual practice to specify the global variable
as an input parameter.

Ideally, global would be accessed once at API boundary entry and the passed as an argument to functions it calls.
It makes it easier to follow as opposed to mixed access we have now, which is harder to review since one has to check both flavors (argument passed or directly accessed).

Quoted 5 lines
Quoted 3 lines

in this patch kvm_init_vcpu() calls new kvm_create_vcpu()
and the former caches these global into 's' local variable,
so I'd reuse that local variable like kvm_get_vcpu() you are removing here did.

That is one perspective, but:
1. kvm_create_vcpu() will also be called externally from other contexts.

I'm fine with this as is, it would be better to mention in commit message that this (others) new API will be called externally. Otherwise it's not clear why KVMState is hidden inside.

Quoted 183 lines

   It would be awkward to pass this variable from those non-local places where it would seem unnecessary.
2. If you look at other symmetrical functions like kvm_destroy_vcpu(), they also have a similar prototype.

I think it is about doing a slight trade-off. If you really believe this change is necessary for us to proceed, I will make the adjustment. However, please note that it will affect the IBM team as well.

Hi Nick,

I hope you are okay with this suggested change?

Thanks,
Salil

Quoted 167 lines
Quoted 167 lines
Quoted 1 line

otherwise patch lgtm

May I request your Reviewed-by for this patch?

Thanks
Salil.

Quoted 159 lines
Quoted 52 lines
accel/kvm/kvm-all.c +3 -24
+
  static int do_kvm_destroy_vcpu(CPUState *cpu)
  {
      KVMState *s = kvm_state;
      long mmap_size;
-    struct KVMParkedVcpu *vcpu = NULL;
      int ret = 0;
 
-    trace_kvm_destroy_vcpu();
+    trace_kvm_destroy_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
 
      ret = kvm_arch_destroy_vcpu(cpu);
      if (ret < 0) {
@@ -373,10 +430,7 @@ static int do_kvm_destroy_vcpu(CPUState *cpu)
          }
      }
 
-    vcpu = g_malloc0(sizeof(*vcpu));
-    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
-    vcpu->kvm_fd = cpu->kvm_fd;
-    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu, node);
+    kvm_park_vcpu(cpu);
  err:
      return ret;
  }
@@ -389,24 +443,6 @@ void kvm_destroy_vcpu(CPUState *cpu)
      }
  }
 
-static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id)
-{
-    struct KVMParkedVcpu *cpu;
-
-    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
-        if (cpu->vcpu_id == vcpu_id) {
-            int kvm_fd;
-
-            QLIST_REMOVE(cpu, node);
-            kvm_fd = cpu->kvm_fd;
-            g_free(cpu);
-            return kvm_fd;
-        }
-    }
-
-    return kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)vcpu_id);
-}
-
  int kvm_init_vcpu(CPUState *cpu, Error **errp)
  {
      KVMState *s = kvm_state;

accel/kvm/kvm-all.c int kvm_init_vcpu(CPUState *cpu, Error **errp) · line 420

Quoted 7 lines
accel/kvm/kvm-all.c +1 -2
@@ -415,19 +451,14 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp)
 
      trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
 
-    ret = kvm_get_vcpu(s, kvm_arch_vcpu_id(cpu));
+    ret = kvm_create_vcpu(cpu);
      if (ret < 0) {
-        error_setg_errno(errp, -ret, "kvm_init_vcpu: kvm_get_vcpu  

failed (%lu)",

Quoted 20 lines
accel/kvm/kvm-all.c +2 -6
+        error_setg_errno(errp, -ret,
+                         "kvm_init_vcpu: kvm_create_vcpu failed (%lu)",
                           kvm_arch_vcpu_id(cpu));
          goto err;
      }
 
-    cpu->kvm_fd = ret;
-    cpu->kvm_state = s;
-    cpu->vcpu_dirty = true;
-    cpu->dirty_pages = 0;
-    cpu->throttle_us_per_full = 0;
-
      mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
      if (mmap_size < 0) {
          ret = mmap_size;
accel/kvm/kvm-cpus.h +0 -0
index ca40add32c..171b22fd29 100644

accel/kvm/kvm-cpus.h bool kvm_supports_guest_debug(void); · line 22

Quoted 1 line
accel/kvm/kvm-cpus.h +0 -0
@@ -22,5 +22,4 @@ bool kvm_supports_guest_debug(void);
  int kvm_insert_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr  

len);

accel/kvm/kvm-cpus.h bool kvm_supports_guest_debug(void); · line 23

Quoted 1 line
accel/kvm/kvm-cpus.h +0 -0
  int kvm_remove_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr  

len);

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd

Quoted 8 lines
accel/kvm/kvm-cpus.h +0 -1
  void kvm_remove_all_breakpoints(CPUState *cpu);
-
  #endif /* KVM_CPUS_H */
accel/kvm/trace-events +0 -0
index 681ccb667d..37626c1ac5 100644
@@ -9,6 +9,10 @@ kvm_device_ioctl(int fd, int type, void *arg) "dev fd  

%d, type 0x%x, arg %p"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 9

Quoted 1 line
accel/kvm/trace-events +0 -0
  kvm_failed_reg_get(uint64_t id, const char *msg) "Warning: Unable to  

retrieve ONEREG %" PRIu64 " from KVM: %s"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 10

Quoted 1 line
accel/kvm/trace-events +0 -0
  kvm_failed_reg_set(uint64_t id, const char *msg) "Warning: Unable to  

set ONEREG %" PRIu64 " to KVM: %s"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 11

Quoted 1 line
accel/kvm/trace-events +0 -0
  kvm_init_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id:  

%lu"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 12

Quoted 1 line
accel/kvm/trace-events +1 -0
+kvm_create_vcpu(int cpu_index, unsigned long arch_cpu_id, int kvm_fd)  

"index: %d, id: %lu, kvm fd: %d"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 13

Quoted 1 line
accel/kvm/trace-events +1 -0
+kvm_destroy_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d  

id: %lu"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd · line 14

Quoted 1 line
accel/kvm/trace-events +1 -0
+kvm_park_vcpu(int cpu_index, unsigned long arch_cpu_id) "index: %d id:  

%lu"

accel/kvm/trace-events kvm_device_ioctl(int fd, int type, void *arg) "dev fd %d, type 0x%x, arg %p"

Quoted 3 lines
accel/kvm/trace-events +1 -0
+kvm_unpark_vcpu(unsigned long arch_cpu_id, const char *msg) "id: %lu %s"
  kvm_irqchip_commit_routes(void) ""
  kvm_irqchip_add_msi_route(char *name, int vector, int virq) "dev %s  

vector %d virq %d"

accel/kvm/trace-events kvm_dirty_ring_reaper(const char *s) "%s" · line 29

Quoted 3 lines
accel/kvm/trace-events +0 -0
  kvm_irqchip_update_msi_route(int virq) "Updating MSI route virq=%d"
@@ -25,7 +29,6 @@ kvm_dirty_ring_reaper(const char *s) "%s"
  kvm_dirty_ring_reap(uint64_t count, int64_t t) "reaped %"PRIu64" pages  

(took %"PRIi64" us)"

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type,

Quoted 11 lines
accel/kvm/trace-events +0 -1
  kvm_dirty_ring_reaper_kick(const char *reason) "%s"
  kvm_dirty_ring_flush(int finished) "%d"
-kvm_destroy_vcpu(void) ""
  kvm_failed_get_vcpu_mmap_size(void) ""
  kvm_cpu_exec(void) ""
  kvm_interrupt_exit_request(void) ""
include/sysemu/kvm.h +0 -0
index c31d9c7356..c4a914b3d8 100644
@@ -313,6 +313,31 @@ int kvm_create_device(KVMState *s, uint64_t type,  

bool test);

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type, bool test);

Quoted 6 lines
include/sysemu/kvm.h +3 -0
   */
  bool kvm_device_supported(int vmfd, uint64_t type);
 
+/**
+ * kvm_create_vcpu - Gets a parked KVM vCPU or creates a KVM vCPU
+ * @cpu: QOM CPUState object for which KVM vCPU has to be  

fetched/created.

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type, bool test);

Quoted 8 lines
include/sysemu/kvm.h +8 -0
+ *
+ * @returns: 0 when success, errno (<0) when failed.
+ */
+int kvm_create_vcpu(CPUState *cpu);
+
+/**
+ * kvm_park_vcpu - Park QEMU KVM vCPU context
+ * @cpu: QOM CPUState object for which QEMU KVM vCPU context has to be  

parked.

Quoted 17 lines
include/sysemu/kvm.h +14 -0
+ *
+ * @returns: none
+ */
+void kvm_park_vcpu(CPUState *cpu);
+
+/**
+ * kvm_unpark_vcpu - unpark QEMU KVM vCPU context
+ * @s: KVM State
+ * @vcpu_id: Architecture vCPU ID of the parked vCPU
+ *
+ * @returns: KVM fd
+ */
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id);
+
  /* Arch specific hooks */
 
  extern const KVMCapabilityInfo kvm_arch_required_capabilities[];  
#12 lore raw

RE: [PATCH V13 1/8] accel/kvm: Extract common KVM vCPU {creation,parking} code

Quoted 149 lines

From: Igor Mammedov <[email protected]>
Sent: Tuesday, July 9, 2024 9:06 AM
To: Salil Mehta <[email protected]>

Quoted 10 lines · On Mon, 8 Jul 2024 23:30:01 +0000 Salil Mehta <[email protected]> wrote:

Hi Igor,

Quoted 7 lines · Igor Mammedov
Quoted 4 lines · On Sat, 6 Jul 2024 15:43:01 +0000 Salil Mehta <[email protected]> wrote:

Hi Igor,
Thanks for taking out time to review.

On Sat, Jul 6, 2024 at 1:12 PM Igor Mammedov

Quoted 27 lines · <[email protected]> wrote:
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 2 lines · On Fri, 7 Jun 2024 12:56:42 +0100 Salil Mehta <[email protected]> wrote:

KVM vCPU creation is done once during the vCPU realization when Qemu

vCPU thread

Quoted 4 lines

is spawned. This is common to all the architectures as of now.

Hot-unplug of vCPU results in destruction of the vCPU object in QOM but

the

Quoted 2 lines

corresponding KVM vCPU object in the Host KVM is not destroyed as KVM

doesn't

Quoted 1 line

support vCPU removal. Therefore, its representative KVM vCPU

object/context in

Quoted 4 lines

Qemu is parked.

Refactor architecture common logic so that some APIs could be reused by

vCPU

Quoted 2 lines

Hotplug code of some architectures likes ARM, Loongson etc. Update

new/old APIs

Quoted 2 lines

with trace events. No functional change is intended here.

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
---
  accel/kvm/kvm-all.c    | 95 ++++++++++++++++++++++++++++-----

---------

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
  accel/kvm/kvm-cpus.h   |  1 -
  accel/kvm/trace-events |  5 ++-
  include/sysemu/kvm.h   | 25 +++++++++++
  4 files changed, 92 insertions(+), 34 deletions(-)
accel/kvm/kvm-all.c index renamed from accel/kvm/kvm-all.c +0 -0

c0be9f5eed..8f9128bb92 100644

accel/kvm/kvm-all.c +0 -0
@@ -340,14 +340,71 @@ err:
      return ret;
  }
 
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

accel/kvm/kvm-all.c err:

Quoted 4 lines
accel/kvm/kvm-all.c +4 -0
+void kvm_park_vcpu(CPUState *cpu) {
+    struct KVMParkedVcpu *vcpu;
+
+    trace_kvm_park_vcpu(cpu->cpu_index,

kvm_arch_vcpu_id(cpu));

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

accel/kvm/kvm-all.c err:

Quoted 5 lines
accel/kvm/kvm-all.c +5 -0
+
+    vcpu = g_malloc0(sizeof(*vcpu));
+    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
+    vcpu->kvm_fd = cpu->kvm_fd;
+    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu,

node);

Quoted 64 lines
Quoted 64 lines
Quoted 53 lines
Quoted 44 lines
Quoted 6 lines
accel/kvm/kvm-all.c +6 -0
+}
+
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id) {
+    struct KVMParkedVcpu *cpu;
+    int kvm_fd = -ENOENT;
+

accel/kvm/kvm-all.c err:

Quoted 10 lines
accel/kvm/kvm-all.c +10 -0
+    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
+        if (cpu->vcpu_id == vcpu_id) {
+            QLIST_REMOVE(cpu, node);
+            kvm_fd = cpu->kvm_fd;
+            g_free(cpu);
+        }
+    }
+
+    trace_kvm_unpark_vcpu(vcpu_id, kvm_fd > 0 ? "unparked" :
+ "not found

parked");

Quoted 7 lines
accel/kvm/kvm-all.c +7 -0
+
+    return kvm_fd;
+}
+
+int kvm_create_vcpu(CPUState *cpu) {
+    unsigned long vcpu_id = kvm_arch_vcpu_id(cpu);
+    KVMState *s = kvm_state;

accel/kvm/kvm-all.c err:

Quoted 10 lines
accel/kvm/kvm-all.c +10 -0
+    int kvm_fd;
+
+    /* check if the KVM vCPU already exist but is parked */
+    kvm_fd = kvm_unpark_vcpu(s, vcpu_id);
+    if (kvm_fd < 0) {
+        /* vCPU not parked: create a new KVM vCPU */
+        kvm_fd = kvm_vm_ioctl(s, KVM_CREATE_VCPU, vcpu_id);
+        if (kvm_fd < 0) {
+            error_report("KVM_CREATE_VCPU IOCTL failed for vCPU
+ %lu",

vcpu_id);

Quoted 9 lines
accel/kvm/kvm-all.c +9 -0
+            return kvm_fd;
+        }
+    }
+
+    cpu->kvm_fd = kvm_fd;
+    cpu->kvm_state = s;
+    cpu->vcpu_dirty = true;
+    cpu->dirty_pages = 0;
+    cpu->throttle_us_per_full = 0;
Quoted 9 lines

accel/kvm/kvm-all.c err: · line 399

Quoted 5 lines
accel/kvm/kvm-all.c +5 -0
+
+    trace_kvm_create_vcpu(cpu->cpu_index, vcpu_id, kvm_fd);
+
+    return 0;
+}

Is there any reason why you are embedding/hiding kvm_state in new API instead of passing it as argument (all callers have it defined, so why not reuse that)?

Quoted 2 lines

It is a global variable and I don't think it is a usual practice to specify the global variable as an input parameter.

Ideally, global would be accessed once at API boundary entry and the passed as an argument to functions it calls.
It makes it easier to follow as opposed to mixed access we have now, which is harder to review since one has to check both flavors (argument passed or directly accessed).

in this patch kvm_init_vcpu() calls new kvm_create_vcpu() and the former caches these global into 's' local variable, so I'd reuse that local variable like kvm_get_vcpu() you are removing here did.

Quoted 6 lines
Quoted 2 lines

That is one perspective, but:
1. kvm_create_vcpu() will also be called externally from other contexts.

I'm fine with this as is, it would be better to mention in commit message that this (others) new API will be called externally. Otherwise it's not clear why KVMState is hidden inside.

ok, thanks.

Best regards
Salil.

Quoted 208 lines
Quoted 33 lines

   It would be awkward to pass this variable from those non-local
places
   where it would seem unnecessary.
2. If you look at other symmetrical functions like kvm_destroy_vcpu(), they also have a similar prototype.

I think it is about doing a slight trade-off. If you really believe this change is necessary for us to proceed, I will make the adjustment. However, please note that it will affect the IBM team as well.

Hi Nick,

I hope you are okay with this suggested change?

Thanks,
Salil

Quoted 17 lines
Quoted 17 lines
Quoted 1 line

otherwise patch lgtm

May I request your Reviewed-by for this patch?

Thanks
Salil.

Quoted 9 lines
Quoted 9 lines
accel/kvm/kvm-all.c +1 -1
+
  static int do_kvm_destroy_vcpu(CPUState *cpu)
  {
      KVMState *s = kvm_state;
      long mmap_size;
-    struct KVMParkedVcpu *vcpu = NULL;
      int ret = 0;
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

accel/kvm/kvm-all.c static int do_kvm_destroy_vcpu(CPUState

Quoted 7 lines
accel/kvm/kvm-all.c +2 -1
-    trace_kvm_destroy_vcpu();
+    trace_kvm_destroy_vcpu(cpu->cpu_index,
+ kvm_arch_vcpu_id(cpu));
 
      ret = kvm_arch_destroy_vcpu(cpu);
      if (ret < 0) {
@@ -373,10 +430,7 @@ static int do_kvm_destroy_vcpu(CPUState

*cpu)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

accel/kvm/kvm-all.c static int do_kvm_destroy_vcpu(CPUState *cpu)

Quoted 7 lines
accel/kvm/kvm-all.c +0 -4
          }
      }
 
-    vcpu = g_malloc0(sizeof(*vcpu));
-    vcpu->vcpu_id = kvm_arch_vcpu_id(cpu);
-    vcpu->kvm_fd = cpu->kvm_fd;
-    QLIST_INSERT_HEAD(&kvm_state->kvm_parked_vcpus, vcpu,

node);

Quoted 75 lines
Quoted 75 lines
Quoted 75 lines
Quoted 75 lines
Quoted 29 lines
accel/kvm/kvm-all.c +1 -17
+    kvm_park_vcpu(cpu);
  err:
      return ret;
  }
@@ -389,24 +443,6 @@ void kvm_destroy_vcpu(CPUState *cpu)
      }
  }
 
-static int kvm_get_vcpu(KVMState *s, unsigned long vcpu_id) -{
-    struct KVMParkedVcpu *cpu;
-
-    QLIST_FOREACH(cpu, &s->kvm_parked_vcpus, node) {
-        if (cpu->vcpu_id == vcpu_id) {
-            int kvm_fd;
-
-            QLIST_REMOVE(cpu, node);
-            kvm_fd = cpu->kvm_fd;
-            g_free(cpu);
-            return kvm_fd;
-        }
-    }
-
-    return kvm_vm_ioctl(s, KVM_CREATE_VCPU, (void *)vcpu_id);
-}
-
  int kvm_init_vcpu(CPUState *cpu, Error **errp)
  {
      KVMState *s = kvm_state;

accel/kvm/kvm-all.c int kvm_init_vcpu(CPUState *cpu, Error · line 421

Quoted 8 lines
accel/kvm/kvm-all.c +1 -2
@@ -415,19 +451,14 @@ int kvm_init_vcpu(CPUState *cpu, Error
 **errp)
 
      trace_kvm_init_vcpu(cpu->cpu_index, kvm_arch_vcpu_id(cpu));
 
-    ret = kvm_get_vcpu(s, kvm_arch_vcpu_id(cpu));
+    ret = kvm_create_vcpu(cpu);
      if (ret < 0) {
-        error_setg_errno(errp, -ret, "kvm_init_vcpu: kvm_get_vcpu

failed (%lu)",

Quoted 22 lines
accel/kvm/kvm-all.c +3 -6
+        error_setg_errno(errp, -ret,
+                         "kvm_init_vcpu: kvm_create_vcpu failed
+ (%lu)",
                           kvm_arch_vcpu_id(cpu));
          goto err;
      }
 
-    cpu->kvm_fd = ret;
-    cpu->kvm_state = s;
-    cpu->vcpu_dirty = true;
-    cpu->dirty_pages = 0;
-    cpu->throttle_us_per_full = 0;
-
      mmap_size = kvm_ioctl(s, KVM_GET_VCPU_MMAP_SIZE, 0);
      if (mmap_size < 0) {
          ret = mmap_size;
accel/kvm/kvm-cpus.h index renamed from accel/kvm/kvm-cpus.h +0 -0

ca40add32c..171b22fd29 100644

accel/kvm/kvm-cpus.h +0 -0
@@ -22,5 +22,4 @@ bool kvm_supports_guest_debug(void);
  int kvm_insert_breakpoint(CPUState *cpu, int type, vaddr addr,
Quoted 1 line

vaddr

len);

Quoted 2 lines

int kvm_remove_breakpoint(CPUState *cpu, int type, vaddr addr,
vaddr

len);

Quoted 9 lines
  void kvm_remove_all_breakpoints(CPUState *cpu);
accel/kvm/kvm-cpus.h +0 -1
-
  #endif /* KVM_CPUS_H */
accel/kvm/trace-events +0 -0
index 681ccb667d..37626c1ac5 100644
@@ -9,6 +9,10 @@ kvm_device_ioctl(int fd, int type, void *arg)

"dev fd

%d, type 0x%x, arg %p"

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

kvm_failed_reg_get(uint64_t id, const char *msg) "Warning:
Unable to

retrieve ONEREG %" PRIu64 " from KVM: %s"

Quoted 2 lines

kvm_failed_reg_set(uint64_t id, const char *msg) "Warning:
Unable to

set ONEREG %" PRIu64 " to KVM: %s"

Quoted 1 line

kvm_init_vcpu(int cpu_index, unsigned long arch_cpu_id) "index:

%d id:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

%lu"

accel/kvm/trace-events

Quoted 2 lines
accel/kvm/trace-events +2 -0
+kvm_create_vcpu(int cpu_index, unsigned long arch_cpu_id, int
+kvm_fd)

"index: %d, id: %lu, kvm fd: %d"

Quoted 2 lines

+kvm_destroy_vcpu(int cpu_index, unsigned long arch_cpu_id) +"index: %d

id: %lu"

Quoted 1 line

+kvm_park_vcpu(int cpu_index, unsigned long arch_cpu_id) "index:

%d id:

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

%lu"

Quoted 1 line

+kvm_unpark_vcpu(unsigned long arch_cpu_id, const char *msg)

"id: %lu %s"

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

kvm_irqchip_commit_routes(void) "" kvm_irqchip_add_msi_route(char *name, int vector, int virq)
"dev %s

vector %d virq %d"

Quoted 1 line

kvm_irqchip_update_msi_route(int virq) "Updating MSI route

virq=%d"

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
accel/kvm/trace-events +0 -0
@@ -25,7 +29,6 @@ kvm_dirty_ring_reaper(const char *s) "%s"
  kvm_dirty_ring_reap(uint64_t count, int64_t t) "reaped

%"PRIu64" pages

(took %"PRIi64" us)"

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

include/sysemu/kvm.h int kvm_create_device(KVMState *s,

Quoted 11 lines

kvm_dirty_ring_reaper_kick(const char *reason) "%s" kvm_dirty_ring_flush(int finished) "%d"

accel/kvm/trace-events +0 -1
-kvm_destroy_vcpu(void) ""
  kvm_failed_get_vcpu_mmap_size(void) ""
  kvm_cpu_exec(void) ""
  kvm_interrupt_exit_request(void) ""
include/sysemu/kvm.h index renamed from include/sysemu/kvm.h +0 -0

c31d9c7356..c4a914b3d8 100644

include/sysemu/kvm.h +0 -0
@@ -313,6 +313,31 @@ int kvm_create_device(KVMState *s,

uint64_t

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

include/sysemu/kvm.h int kvm_create_device(KVMState *s, · line 313

Quoted 1 line
include/sysemu/kvm.h +0 -0
 type,

bool test);

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type, bool test);

Quoted 7 lines
include/sysemu/kvm.h +4 -0
   */
  bool kvm_device_supported(int vmfd, uint64_t type);
 
+/**
+ * kvm_create_vcpu - Gets a parked KVM vCPU or creates a KVM
+vCPU
+ * @cpu: QOM CPUState object for which KVM vCPU has to be

fetched/created.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

include/sysemu/kvm.h int kvm_create_device(KVMState *s, uint64_t type, bool test);

Quoted 8 lines
include/sysemu/kvm.h +8 -0
+ *
+ * @returns: 0 when success, errno (<0) when failed.
+ */
+int kvm_create_vcpu(CPUState *cpu);
+
+/**
+ * kvm_park_vcpu - Park QEMU KVM vCPU context
+ * @cpu: QOM CPUState object for which QEMU KVM vCPU

context has

Quoted 22 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

include/sysemu/kvm.h int kvm_create_device(KVMState *s, · line 329

Quoted 1 line
include/sysemu/kvm.h +1 -0
+to be

parked.

Quoted 18 lines
include/sysemu/kvm.h +14 -0
+ *
+ * @returns: none
+ */
+void kvm_park_vcpu(CPUState *cpu);
+
+/**
+ * kvm_unpark_vcpu - unpark QEMU KVM vCPU context
+ * @s: KVM State
+ * @vcpu_id: Architecture vCPU ID of the parked vCPU
+ *
+ * @returns: KVM fd
+ */
+int kvm_unpark_vcpu(KVMState *s, unsigned long vcpu_id);
+
  /* Arch specific hooks */
 
  extern const KVMCapabilityInfo

kvm_arch_required_capabilities[];

#13 lore raw

PATCH v13 2/8 hw/acpi: Move CPU ctrl-dev MMIO region len macro to common header file

In reply to Salil Mehta

CPU ctrl-dev MMIO region length could be used in ACPI GED and various other architecture specific places. Move ACPI_CPU_HOTPLUG_REG_LEN macro to more appropriate common header file.

---
 hw/acpi/cpu.c                 | 2 +-
 include/hw/acpi/cpu_hotplug.h | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
hw/acpi/cpu.c +1 -1
index 2d81c1e790..69aaa563db 100644
@@ -1,13 +1,13 @@
 #include "qemu/osdep.h"
 #include "migration/vmstate.h"
 #include "hw/acpi/cpu.h"
+#include "hw/acpi/cpu_hotplug.h"
 #include "hw/core/cpu.h"
 #include "qapi/error.h"
 #include "qapi/qapi-events-acpi.h"
 #include "trace.h"
 #include "sysemu/numa.h"
 
-#define ACPI_CPU_HOTPLUG_REG_LEN 12
 #define ACPI_CPU_SELECTOR_OFFSET_WR 0
 #define ACPI_CPU_FLAGS_OFFSET_RW 4
 #define ACPI_CPU_CMD_OFFSET_WR 5
include/hw/acpi/cpu_hotplug.h +2 -0
index 3b932abbbb..48b291e45e 100644
@@ -19,6 +19,8 @@
 #include "hw/hotplug.h"
 #include "hw/acpi/cpu.h"
 
+#define ACPI_CPU_HOTPLUG_REG_LEN 12
+
 typedef struct AcpiCpuHotplug {
     Object *device;
     MemoryRegion io;
Signature · 1 line
2.34.1
#14 lore raw

Re: [PATCH V13 2/8] hw/acpi: Move CPU ctrl-dev MMIO region len macro to common header file

include/hw/acpi/cpu_hotplug.h

Quoted 43 lines · On Fri, 7 Jun 2024 12:56:43 +0100 Salil Mehta <[email protected]> wrote:

CPU ctrl-dev MMIO region length could be used in ACPI GED and various other architecture specific places. Move ACPI_CPU_HOTPLUG_REG_LEN macro to more appropriate common header file.

---
 hw/acpi/cpu.c                 | 2 +-
 include/hw/acpi/cpu_hotplug.h | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
hw/acpi/cpu.c +1 -1
index 2d81c1e790..69aaa563db 100644
@@ -1,13 +1,13 @@
 #include "qemu/osdep.h"
 #include "migration/vmstate.h"
 #include "hw/acpi/cpu.h"
+#include "hw/acpi/cpu_hotplug.h"
 #include "hw/core/cpu.h"
 #include "qapi/error.h"
 #include "qapi/qapi-events-acpi.h"
 #include "trace.h"
 #include "sysemu/numa.h"
 
-#define ACPI_CPU_HOTPLUG_REG_LEN 12
 #define ACPI_CPU_SELECTOR_OFFSET_WR 0
 #define ACPI_CPU_FLAGS_OFFSET_RW 4
 #define ACPI_CPU_CMD_OFFSET_WR 5
include/hw/acpi/cpu_hotplug.h +0 -0
index 3b932abbbb..48b291e45e 100644

this file has deps on x86 machine and contains mainly legacy CPU hotplug API for x86.

include/hw/acpi/cpu_hotplug.h line 22

Quoted 5 lines
include/hw/acpi/cpu_hotplug.h +1 -0
@@ -19,6 +19,8 @@
 #include "hw/hotplug.h"
 #include "hw/acpi/cpu.h"
 
+#define ACPI_CPU_HOTPLUG_REG_LEN 12

the better place for it would be include/hw/acpi/cpu.h

Quoted 3 lines
include/hw/acpi/cpu_hotplug.h +0 -0
 typedef struct AcpiCpuHotplug {
     Object *device;
     MemoryRegion io;
#15 lore raw

Re: [PATCH V13 2/8] hw/acpi: Move CPU ctrl-dev MMIO region len macro to common header file

Hi Igor,

Quoted 38 lines · Igor Mammedov
Quoted 35 lines · On Fri, 7 Jun 2024 12:56:43 +0100 Salil Mehta <[email protected]> wrote:

CPU ctrl-dev MMIO region length could be used in ACPI GED and various other architecture specific places. Move ACPI_CPU_HOTPLUG_REG_LEN macro to more appropriate common header file.

---
  hw/acpi/cpu.c                 | 2 +-
  include/hw/acpi/cpu_hotplug.h | 2 ++
  2 files changed, 3 insertions(+), 1 deletion(-)
hw/acpi/cpu.c +1 -0
index 2d81c1e790..69aaa563db 100644
@@ -1,13 +1,13 @@
  #include "qemu/osdep.h"
  #include "migration/vmstate.h"
  #include "hw/acpi/cpu.h"
+#include "hw/acpi/cpu_hotplug.h"
  #include "hw/core/cpu.h"
  #include "qapi/error.h"
  #include "qapi/qapi-events-acpi.h"
  #include "trace.h"
  #include "sysemu/numa.h"
 
Quoted 10 lines

include/hw/acpi/cpu_hotplug.h

Quoted 8 lines
hw/acpi/cpu.c +0 -1
-#define ACPI_CPU_HOTPLUG_REG_LEN 12
  #define ACPI_CPU_SELECTOR_OFFSET_WR 0
  #define ACPI_CPU_FLAGS_OFFSET_RW 4
  #define ACPI_CPU_CMD_OFFSET_WR 5
include/hw/acpi/cpu_hotplug.h +0 -0
index 3b932abbbb..48b291e45e 100644

this file has deps on x86 machine and contains mainly legacy CPU hotplug API for x86.

sure.

Quoted 7 lines

include/hw/acpi/cpu_hotplug.h line 22

Quoted 6 lines
include/hw/acpi/cpu_hotplug.h +1 -0
@@ -19,6 +19,8 @@
  #include "hw/hotplug.h"
  #include "hw/acpi/cpu.h"
 
+#define ACPI_CPU_HOTPLUG_REG_LEN 12

the better place for it would be include/hw/acpi/cpu.h

ok.

Thanks

Salil.

Quoted 4 lines
Quoted 4 lines
include/hw/acpi/cpu_hotplug.h +0 -0
  typedef struct AcpiCpuHotplug {
      Object *device;
      MemoryRegion io;
#16 lore raw

PATCH v13 3/8 hw/acpi: Update ACPI GED framework to support vCPU Hotplug

In reply to Salil Mehta

ACPI GED (as described in the ACPI 6.4 spec) uses an interrupt listed in the _CRS object of GED to intimate OSPM about an event. Later then demultiplexes the notified event by evaluating ACPI _EVT method to know the type of event. Use ACPI GED to also notify the guest kernel about any CPU hot(un)plug events.

ACPI CPU hotplug related initialization should only happen if ACPI_CPU_HOTPLUG support has been enabled for particular architecture. Add cpu_hotplug_hw_init() stub to avoid compilation break.

---
 hw/acpi/acpi-cpu-hotplug-stub.c        |  6 ++++++
 hw/acpi/cpu.c                          |  6 +++++-
 hw/acpi/generic_event_device.c         | 17 +++++++++++++++++
 include/hw/acpi/generic_event_device.h |  4 ++++
 4 files changed, 32 insertions(+), 1 deletion(-)
hw/acpi/acpi-cpu-hotplug-stub.c +6 -0
index 3fc4b14c26..c6c61bb9cd 100644
@@ -19,6 +19,12 @@ void legacy_acpi_cpu_hotplug_init(MemoryRegion *parent, Object *owner,
     return;
 }
 
+void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner,
+                         CPUHotplugState *state, hwaddr base_addr)
+{
+    return;
+}
+
 void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list)
 {
     return;
hw/acpi/cpu.c +5 -1
index 69aaa563db..473b37ba88 100644
@@ -221,7 +221,11 @@ void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner,
     const CPUArchIdList *id_list;
     int i;
 
-    assert(mc->possible_cpu_arch_ids);
+    /* hotplug might not be available for all types like x86/microvm etc. */
+    if (!mc->possible_cpu_arch_ids) {
+        return;
+    }
+
     id_list = mc->possible_cpu_arch_ids(machine);
     state->dev_count = id_list->len;
     state->devs = g_new0(typeof(*state->devs), state->dev_count);
hw/acpi/generic_event_device.c +17 -0
index 2d6e91b124..54d3b4bf9d 100644
@@ -12,6 +12,7 @@
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "hw/acpi/acpi.h"
+#include "hw/acpi/cpu.h"
 #include "hw/acpi/generic_event_device.h"
 #include "hw/irq.h"
 #include "hw/mem/pc-dimm.h"
@@ -25,6 +26,7 @@ static const uint32_t ged_supported_events[] = {
     ACPI_GED_MEM_HOTPLUG_EVT,
     ACPI_GED_PWR_DOWN_EVT,
     ACPI_GED_NVDIMM_HOTPLUG_EVT,
+    ACPI_GED_CPU_HOTPLUG_EVT,
 };
 
 /*
@@ -234,6 +236,8 @@ static void acpi_ged_device_plug_cb(HotplugHandler *hotplug_dev,
         } else {
             acpi_memory_plug_cb(hotplug_dev, &s->memhp_state, dev, errp);
         }
+    } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
+        acpi_cpu_plug_cb(hotplug_dev, &s->cpuhp_state, dev, errp);
     } else {
         error_setg(errp, "virt: device plug request for unsupported device"
                    " type: %s", object_get_typename(OBJECT(dev)));
@@ -248,6 +252,8 @@ static void acpi_ged_unplug_request_cb(HotplugHandler *hotplug_dev,
     if ((object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) &&
                        !(object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)))) {
         acpi_memory_unplug_request_cb(hotplug_dev, &s->memhp_state, dev, errp);
+    } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
+        acpi_cpu_unplug_request_cb(hotplug_dev, &s->cpuhp_state, dev, errp);
     } else {
         error_setg(errp, "acpi: device unplug request for unsupported device"
                    " type: %s", object_get_typename(OBJECT(dev)));
@@ -261,6 +267,8 @@ static void acpi_ged_unplug_cb(HotplugHandler *hotplug_dev,
 
     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
         acpi_memory_unplug_cb(&s->memhp_state, dev, errp);
+    } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
+        acpi_cpu_unplug_cb(&s->cpuhp_state, dev, errp);
     } else {
         error_setg(errp, "acpi: device unplug for unsupported device"
                    " type: %s", object_get_typename(OBJECT(dev)));
@@ -272,6 +280,7 @@ static void acpi_ged_ospm_status(AcpiDeviceIf *adev, ACPIOSTInfoList ***list)
     AcpiGedState *s = ACPI_GED(adev);
 
     acpi_memory_ospm_status(&s->memhp_state, list);
+    acpi_cpu_ospm_status(&s->cpuhp_state, list);
 }
 
 static void acpi_ged_send_event(AcpiDeviceIf *adev, AcpiEventStatusBits ev)
@@ -286,6 +295,8 @@ static void acpi_ged_send_event(AcpiDeviceIf *adev, AcpiEventStatusBits ev)
         sel = ACPI_GED_PWR_DOWN_EVT;
     } else if (ev & ACPI_NVDIMM_HOTPLUG_STATUS) {
         sel = ACPI_GED_NVDIMM_HOTPLUG_EVT;
+    } else if (ev & ACPI_CPU_HOTPLUG_STATUS) {
+        sel = ACPI_GED_CPU_HOTPLUG_EVT;
     } else {
         /* Unknown event. Return without generating interrupt. */
         warn_report("GED: Unsupported event %d. No irq injected", ev);
@@ -400,6 +411,12 @@ static void acpi_ged_initfn(Object *obj)
     memory_region_init_io(&ged_st->regs, obj, &ged_regs_ops, ged_st,
                           TYPE_ACPI_GED "-regs", ACPI_GED_REG_COUNT);
     sysbus_init_mmio(sbd, &ged_st->regs);
+
+    memory_region_init(&s->container_cpuhp, OBJECT(dev), "cpuhp container",
+                       ACPI_CPU_HOTPLUG_REG_LEN);
+    sysbus_init_mmio(sbd, &s->container_cpuhp);
+    cpu_hotplug_hw_init(&s->container_cpuhp, OBJECT(dev),
+                        &s->cpuhp_state, 0);
 }
 
 static void acpi_ged_class_init(ObjectClass *class, void *data)
include/hw/acpi/generic_event_device.h +4 -0
index ba84ce0214..90fc41cbb8 100644
@@ -60,6 +60,7 @@
 #define HW_ACPI_GENERIC_EVENT_DEVICE_H
 
 #include "hw/sysbus.h"
+#include "hw/acpi/cpu_hotplug.h"
 #include "hw/acpi/memory_hotplug.h"
 #include "hw/acpi/ghes.h"
 #include "qom/object.h"
@@ -95,6 +96,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(AcpiGedState, ACPI_GED)
 #define ACPI_GED_MEM_HOTPLUG_EVT   0x1
 #define ACPI_GED_PWR_DOWN_EVT      0x2
 #define ACPI_GED_NVDIMM_HOTPLUG_EVT 0x4
+#define ACPI_GED_CPU_HOTPLUG_EVT    0x8
 
 typedef struct GEDState {
     MemoryRegion evt;
@@ -106,6 +108,8 @@ struct AcpiGedState {
     SysBusDevice parent_obj;
     MemHotplugState memhp_state;
     MemoryRegion container_memhp;
+    CPUHotplugState cpuhp_state;
+    MemoryRegion container_cpuhp;
     GEDState ged_state;
     uint32_t ged_event_bitmap;
     qemu_irq irq;
Signature · 1 line
2.34.1
#17 lore raw

Re: [PATCH V13 3/8] hw/acpi: Update ACPI GED framework to support vCPU Hotplug

hw/acpi/generic_event_device.c static void acpi_ged_initfn(Object *obj) · line 420

Quoted 11 lines · Salil Mehta
hw/acpi/generic_event_device.c +6 -0
@@ -400,6 +411,12 @@ static void acpi_ged_initfn(Object *obj)
     memory_region_init_io(&ged_st->regs, obj, &ged_regs_ops, ged_st,
                           TYPE_ACPI_GED "-regs", ACPI_GED_REG_COUNT);
     sysbus_init_mmio(sbd, &ged_st->regs);
+
+    memory_region_init(&s->container_cpuhp, OBJECT(dev), "cpuhp container",
+                       ACPI_CPU_HOTPLUG_REG_LEN);
+    sysbus_init_mmio(sbd, &s->container_cpuhp);
+    cpu_hotplug_hw_init(&s->container_cpuhp, OBJECT(dev),
+                        &s->cpuhp_state, 0);
 }

Could the ACPI persistent presence ARM requires be a property of the ACPI device?

Thanks,
Nick

#18 lore raw

RE: [PATCH V13 3/8] hw/acpi: Update ACPI GED framework to support vCPU Hotplug

HI Nick,

Quoted 13 lines

From: Nicholas Piggin <[email protected]>
Sent: Thursday, July 4, 2024 4:03 AM
To: Salil Mehta <[email protected]>; [email protected]; [email protected]

hw/acpi/generic_event_device.c static void acpi_ged_initfn(Object *obj) · line 415

Quoted 6 lines · Salil Mehta
hw/acpi/generic_event_device.c +2 -0
@@ -400,6 +411,12 @@ static void acpi_ged_initfn(Object *obj)
     memory_region_init_io(&ged_st->regs, obj, &ged_regs_ops, ged_st,
                           TYPE_ACPI_GED "-regs", ACPI_GED_REG_COUNT);
     sysbus_init_mmio(sbd, &ged_st->regs);
+
+    memory_region_init(&s->container_cpuhp, OBJECT(dev), "cpuhp

container",

Quoted 8 lines

hw/acpi/generic_event_device.c static void acpi_ged_initfn(Object *obj) · line 420

Quoted 5 lines
hw/acpi/generic_event_device.c +4 -0
+                       ACPI_CPU_HOTPLUG_REG_LEN);
+    sysbus_init_mmio(sbd, &s->container_cpuhp);
+    cpu_hotplug_hw_init(&s->container_cpuhp, OBJECT(dev),
+                        &s->cpuhp_state, 0);
 }

Could the ACPI persistent presence ARM requires be a property of the ACPI device?

I think it is more of a CPU property rather than a GED device?

Quoted 3 lines

Thanks,
Nick

#19 lore raw

Re: [PATCH V13 3/8] hw/acpi: Update ACPI GED framework to support vCPU Hotplug

In reply to Salil Mehta
Quoted 51 lines · On Fri, 7 Jun 2024 12:56:44 +0100 Salil Mehta <[email protected]> wrote:

ACPI GED (as described in the ACPI 6.4 spec) uses an interrupt listed in the _CRS object of GED to intimate OSPM about an event. Later then demultiplexes the notified event by evaluating ACPI _EVT method to know the type of event. Use ACPI GED to also notify the guest kernel about any CPU hot(un)plug events.

ACPI CPU hotplug related initialization should only happen if ACPI_CPU_HOTPLUG support has been enabled for particular architecture. Add cpu_hotplug_hw_init() stub to avoid compilation break.

---
 hw/acpi/acpi-cpu-hotplug-stub.c        |  6 ++++++
 hw/acpi/cpu.c                          |  6 +++++-
 hw/acpi/generic_event_device.c         | 17 +++++++++++++++++
 include/hw/acpi/generic_event_device.h |  4 ++++
 4 files changed, 32 insertions(+), 1 deletion(-)
hw/acpi/acpi-cpu-hotplug-stub.c +6 -0
index 3fc4b14c26..c6c61bb9cd 100644
@@ -19,6 +19,12 @@ void legacy_acpi_cpu_hotplug_init(MemoryRegion *parent, Object *owner,
     return;
 }
 
+void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner,
+                         CPUHotplugState *state, hwaddr base_addr)
+{
+    return;
+}
+
 void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list)
 {
     return;
hw/acpi/cpu.c +0 -0
index 69aaa563db..473b37ba88 100644

hw/acpi/cpu.c void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner, · line 227

Quoted 8 lines
hw/acpi/cpu.c +4 -1
@@ -221,7 +221,11 @@ void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner,
     const CPUArchIdList *id_list;
     int i;
 
-    assert(mc->possible_cpu_arch_ids);
+    /* hotplug might not be available for all types like x86/microvm etc. */
+    if (!mc->possible_cpu_arch_ids) {
+        return;
+    }

if hotplug is not supported, this function shouldn't be called at all.

[...]

hw/acpi/cpu.c static void acpi_ged_initfn(Object *obj) · line 420

Quoted 11 lines
hw/acpi/cpu.c +6 -0
@@ -400,6 +411,12 @@ static void acpi_ged_initfn(Object *obj)
     memory_region_init_io(&ged_st->regs, obj, &ged_regs_ops, ged_st,
                           TYPE_ACPI_GED "-regs", ACPI_GED_REG_COUNT);
     sysbus_init_mmio(sbd, &ged_st->regs);
+
+    memory_region_init(&s->container_cpuhp, OBJECT(dev), "cpuhp container",
+                       ACPI_CPU_HOTPLUG_REG_LEN);
+    sysbus_init_mmio(sbd, &s->container_cpuhp);
 
+    cpu_hotplug_hw_init(&s->container_cpuhp, OBJECT(dev),
+                        &s->cpuhp_state, 0);

suggest to move this call to realize time, and gate it on ACPI_GED_CPU_HOTPLUG_EVT being set.
Platform that supports cpu hotplug must optin, setting ACPI_GED_CPU_HOTPLUG_EVT, while for the rest it will be ignored.

for example: create_acpi_ged() : event |= ACPI_GED_NVDIMM_HOTPLUG_EVT;

Quoted 32 lines
hw/acpi/cpu.c +0 -0
 }
 

static void acpi_ged_class_init(ObjectClass *class, void *data)

include/hw/acpi/generic_event_device.h +4 -0
index ba84ce0214..90fc41cbb8 100644
@@ -60,6 +60,7 @@
 #define HW_ACPI_GENERIC_EVENT_DEVICE_H
 
 #include "hw/sysbus.h"
+#include "hw/acpi/cpu_hotplug.h"
 #include "hw/acpi/memory_hotplug.h"
 #include "hw/acpi/ghes.h"
 #include "qom/object.h"
@@ -95,6 +96,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(AcpiGedState, ACPI_GED)
 #define ACPI_GED_MEM_HOTPLUG_EVT   0x1
 #define ACPI_GED_PWR_DOWN_EVT      0x2
 #define ACPI_GED_NVDIMM_HOTPLUG_EVT 0x4
+#define ACPI_GED_CPU_HOTPLUG_EVT    0x8
 
 typedef struct GEDState {
     MemoryRegion evt;
@@ -106,6 +108,8 @@ struct AcpiGedState {
     SysBusDevice parent_obj;
     MemHotplugState memhp_state;
     MemoryRegion container_memhp;
+    CPUHotplugState cpuhp_state;
+    MemoryRegion container_cpuhp;
     GEDState ged_state;
     uint32_t ged_event_bitmap;
     qemu_irq irq;
#20 lore raw

Re: [PATCH V13 3/8] hw/acpi: Update ACPI GED framework to support vCPU Hotplug

Quoted 57 lines · Igor Mammedov
Quoted 54 lines · On Fri, 7 Jun 2024 12:56:44 +0100 Salil Mehta <[email protected]> wrote:

ACPI GED (as described in the ACPI 6.4 spec) uses an interrupt listed in the _CRS object of GED to intimate OSPM about an event. Later then demultiplexes the notified event by evaluating ACPI _EVT method to know the type of event. Use ACPI GED to also notify the guest kernel about any CPU hot(un)plug events.

ACPI CPU hotplug related initialization should only happen if ACPI_CPU_HOTPLUG support has been enabled for particular architecture. Add cpu_hotplug_hw_init() stub to avoid compilation break.

---
  hw/acpi/acpi-cpu-hotplug-stub.c        |  6 ++++++
  hw/acpi/cpu.c                          |  6 +++++-
  hw/acpi/generic_event_device.c         | 17 +++++++++++++++++
  include/hw/acpi/generic_event_device.h |  4 ++++
  4 files changed, 32 insertions(+), 1 deletion(-)
hw/acpi/acpi-cpu-hotplug-stub.c +6 -0
index 3fc4b14c26..c6c61bb9cd 100644
@@ -19,6 +19,12 @@ void legacy_acpi_cpu_hotplug_init(MemoryRegion *parent, Object *owner,
      return;
  }
 
+void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner,
+                         CPUHotplugState *state, hwaddr base_addr)
+{
+    return;
+}
+
  void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list)
  {
      return;
hw/acpi/cpu.c +0 -0
index 69aaa563db..473b37ba88 100644
@@ -221,7 +221,11 @@ void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner,
      const CPUArchIdList *id_list;
      int i;
 
Quoted 6 lines

hw/acpi/cpu.c void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner, · line 227

Quoted 5 lines
hw/acpi/cpu.c +4 -1
-    assert(mc->possible_cpu_arch_ids);
+    /* hotplug might not be available for all types like x86/microvm etc. */
+    if (!mc->possible_cpu_arch_ids) {
+        return;
+    }

if hotplug is not supported, this function shouldn't be called at all.

True. But none the less this gets called for Intel/microvm and causes qtest to fail.

I think, we've had this discussion before last year as well. Please check below:

https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 17 lines

[...]

hw/acpi/cpu.c static void acpi_ged_initfn(Object *obj) · line 419

Quoted 10 lines
hw/acpi/cpu.c +6 -0
@@ -400,6 +411,12 @@ static void acpi_ged_initfn(Object *obj)
      memory_region_init_io(&ged_st->regs, obj, &ged_regs_ops, ged_st,
                            TYPE_ACPI_GED "-regs", ACPI_GED_REG_COUNT);
      sysbus_init_mmio(sbd, &ged_st->regs);
+
+    memory_region_init(&s->container_cpuhp, OBJECT(dev), "cpuhp container",
+                       ACPI_CPU_HOTPLUG_REG_LEN);
+    sysbus_init_mmio(sbd, &s->container_cpuhp);
+    cpu_hotplug_hw_init(&s->container_cpuhp, OBJECT(dev),
+                        &s->cpuhp_state, 0);

suggest to move this call to realize time, and gate it on ACPI_GED_CPU_HOTPLUG_EVT being set.
Platform that supports cpu hotplug must optin, setting ACPI_GED_CPU_HOTPLUG_EVT, while for the rest it will be ignored.

Quoted 1 line

for example: create_acpi_ged() : event |= ACPI_GED_NVDIMM_HOTPLUG_EVT;

Similar case applies to the Memory hotplug as well and any cleaning here

will mean going beyond the realms of this patch-set. But I can definitely

take this activity in a separate patch-set if you wish?

Thanks

Quoted 33 lines
Quoted 33 lines
hw/acpi/cpu.c +0 -0
  }
 
  static void acpi_ged_class_init(ObjectClass *class, void *data)
include/hw/acpi/generic_event_device.h +4 -0
index ba84ce0214..90fc41cbb8 100644
@@ -60,6 +60,7 @@
  #define HW_ACPI_GENERIC_EVENT_DEVICE_H
 
  #include "hw/sysbus.h"
+#include "hw/acpi/cpu_hotplug.h"
  #include "hw/acpi/memory_hotplug.h"
  #include "hw/acpi/ghes.h"
  #include "qom/object.h"
@@ -95,6 +96,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(AcpiGedState, ACPI_GED)
  #define ACPI_GED_MEM_HOTPLUG_EVT   0x1
  #define ACPI_GED_PWR_DOWN_EVT      0x2
  #define ACPI_GED_NVDIMM_HOTPLUG_EVT 0x4
+#define ACPI_GED_CPU_HOTPLUG_EVT    0x8
 
  typedef struct GEDState {
      MemoryRegion evt;
@@ -106,6 +108,8 @@ struct AcpiGedState {
      SysBusDevice parent_obj;
      MemHotplugState memhp_state;
      MemoryRegion container_memhp;
+    CPUHotplugState cpuhp_state;
+    MemoryRegion container_cpuhp;
      GEDState ged_state;
      uint32_t ged_event_bitmap;
      qemu_irq irq;
#21 lore raw

Re: [PATCH V13 3/8] hw/acpi: Update ACPI GED framework to support vCPU Hotplug

Quoted 65 lines · On Mon, 8 Jul 2024 05:12:48 +0000 Salil Mehta <[email protected]> wrote:
Quoted 57 lines · Igor Mammedov
Quoted 54 lines · On Fri, 7 Jun 2024 12:56:44 +0100 Salil Mehta <[email protected]> wrote:

ACPI GED (as described in the ACPI 6.4 spec) uses an interrupt listed in the _CRS object of GED to intimate OSPM about an event. Later then demultiplexes the notified event by evaluating ACPI _EVT method to know the type of event. Use ACPI GED to also notify the guest kernel about any CPU hot(un)plug events.

ACPI CPU hotplug related initialization should only happen if ACPI_CPU_HOTPLUG support has been enabled for particular architecture. Add cpu_hotplug_hw_init() stub to avoid compilation break.

---
  hw/acpi/acpi-cpu-hotplug-stub.c        |  6 ++++++
  hw/acpi/cpu.c                          |  6 +++++-
  hw/acpi/generic_event_device.c         | 17 +++++++++++++++++
  include/hw/acpi/generic_event_device.h |  4 ++++
  4 files changed, 32 insertions(+), 1 deletion(-)
hw/acpi/acpi-cpu-hotplug-stub.c +6 -0
index 3fc4b14c26..c6c61bb9cd 100644
@@ -19,6 +19,12 @@ void legacy_acpi_cpu_hotplug_init(MemoryRegion *parent, Object *owner,
      return;
  }
 
+void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner,
+                         CPUHotplugState *state, hwaddr base_addr)
+{
+    return;
+}
+
  void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list)
  {
      return;
hw/acpi/cpu.c +0 -0
index 69aaa563db..473b37ba88 100644
@@ -221,7 +221,11 @@ void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner,
      const CPUArchIdList *id_list;
      int i;
 
Quoted 6 lines

hw/acpi/cpu.c void cpu_hotplug_hw_init(MemoryRegion *as, Object *owner, · line 227

Quoted 5 lines
hw/acpi/cpu.c +4 -1
-    assert(mc->possible_cpu_arch_ids);
+    /* hotplug might not be available for all types like x86/microvm etc. */
+    if (!mc->possible_cpu_arch_ids) {
+        return;
+    }  

if hotplug is not supported, this function shouldn't be called at all.

Quoted 7 lines

True. But none the less this gets called for Intel/microvm and causes qtest to fail.

I think, we've had this discussion before last year as well. Please check below:

https://lore.kernel.org/qemu-devel/[email protected]/

And I see that I had the same objection,
'
cpu_hotplug_hw_init() should not be called at initfn time, but rather at realize time.
'

Quoted 17 lines
Quoted 17 lines

[...]

hw/acpi/cpu.c static void acpi_ged_initfn(Object *obj) · line 419

Quoted 10 lines
hw/acpi/cpu.c +6 -0
@@ -400,6 +411,12 @@ static void acpi_ged_initfn(Object *obj)
      memory_region_init_io(&ged_st->regs, obj, &ged_regs_ops, ged_st,
                            TYPE_ACPI_GED "-regs", ACPI_GED_REG_COUNT);
      sysbus_init_mmio(sbd, &ged_st->regs);
+
+    memory_region_init(&s->container_cpuhp, OBJECT(dev), "cpuhp container",
+                       ACPI_CPU_HOTPLUG_REG_LEN);
+    sysbus_init_mmio(sbd, &s->container_cpuhp);
+    cpu_hotplug_hw_init(&s->container_cpuhp, OBJECT(dev),
+                        &s->cpuhp_state, 0);  

suggest to move this call to realize time, and gate it on ACPI_GED_CPU_HOTPLUG_EVT being set.
Platform that supports cpu hotplug must optin, setting ACPI_GED_CPU_HOTPLUG_EVT, while for the rest it will be ignored.

which I've just suggested again ^^^.

Quoted 9 lines
Quoted 2 lines

for example: create_acpi_ged() : event |= ACPI_GED_NVDIMM_HOTPLUG_EVT;

Similar case applies to the Memory hotplug as well and any cleaning here

will mean going beyond the realms of this patch-set. But I can definitely

take this activity in a separate patch-set if you wish?

For memory hotplug cleanup it's fine to be separate series, but for cpu hotplug parts you are touching I'd very much prefer done it right from the start.

That might also help to reduce code churn within this series.

Quoted 37 lines

Thanks

Quoted 33 lines
Quoted 33 lines
hw/acpi/cpu.c +0 -0
  }
 
  static void acpi_ged_class_init(ObjectClass *class, void *data)
include/hw/acpi/generic_event_device.h +4 -0
index ba84ce0214..90fc41cbb8 100644
@@ -60,6 +60,7 @@
  #define HW_ACPI_GENERIC_EVENT_DEVICE_H
 
  #include "hw/sysbus.h"
+#include "hw/acpi/cpu_hotplug.h"
  #include "hw/acpi/memory_hotplug.h"
  #include "hw/acpi/ghes.h"
  #include "qom/object.h"
@@ -95,6 +96,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(AcpiGedState, ACPI_GED)
  #define ACPI_GED_MEM_HOTPLUG_EVT   0x1
  #define ACPI_GED_PWR_DOWN_EVT      0x2
  #define ACPI_GED_NVDIMM_HOTPLUG_EVT 0x4
+#define ACPI_GED_CPU_HOTPLUG_EVT    0x8
 
  typedef struct GEDState {
      MemoryRegion evt;
@@ -106,6 +108,8 @@ struct AcpiGedState {
      SysBusDevice parent_obj;
      MemHotplugState memhp_state;
      MemoryRegion container_memhp;
+    CPUHotplugState cpuhp_state;
+    MemoryRegion container_cpuhp;
      GEDState ged_state;
      uint32_t ged_event_bitmap;
      qemu_irq irq;  
#22 lore raw

PATCH v13 4/8 hw/acpi: Update GED _EVT method AML with CPU scan

In reply to Salil Mehta

OSPM evaluates _EVT method to map the event. The CPU hotplug event eventually results in start of the CPU scan. Scan figures out the CPU and the kind of event(plug/unplug) and notifies it back to the guest. Update the GED AML _EVT method with the call to \\_SB.CPUS.CSCN

Also, macro CPU_SCAN_METHOD might be referred in other places like during GED intialization so it makes sense to have its definition placed in some common header file like cpu_hotplug.h. But doing this can cause compilation break because of the conflicting macro definitions present in cpu.c and cpu_hotplug.c and because both these files get compiled due to historic reasons of x86 world i.e. decision to use legacy(GPE.2)/modern(GED) CPU hotplug interface happens during runtime [1]. To mitigate above, for now, declare a new common macro ACPI_CPU_SCAN_METHOD for CPU scan method instead.
(This needs a separate discussion later on for clean-up)

Reference:
[1] https://lore.kernel.org/qemu-devel/[email protected]/

---
 hw/acpi/cpu.c                  | 2 +-
 hw/acpi/generic_event_device.c | 4 ++++
 include/hw/acpi/cpu_hotplug.h  | 2 ++
 3 files changed, 7 insertions(+), 1 deletion(-)
hw/acpi/cpu.c +1 -1
index 473b37ba88..af2b6655d2 100644
@@ -327,7 +327,7 @@ const VMStateDescription vmstate_cpu_hotplug = {
 #define CPUHP_RES_DEVICE  "PRES"
 #define CPU_LOCK          "CPLK"
 #define CPU_STS_METHOD    "CSTA"
-#define CPU_SCAN_METHOD   "CSCN"
+#define CPU_SCAN_METHOD   ACPI_CPU_SCAN_METHOD
 #define CPU_NOTIFY_METHOD "CTFY"
 #define CPU_EJECT_METHOD  "CEJ0"
 #define CPU_OST_METHOD    "COST"
hw/acpi/generic_event_device.c +4 -0
index 54d3b4bf9d..63226b0040 100644
@@ -109,6 +109,10 @@ void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev,
                 aml_append(if_ctx, aml_call0(MEMORY_DEVICES_CONTAINER "."
                                              MEMORY_SLOT_SCAN_METHOD));
                 break;
+            case ACPI_GED_CPU_HOTPLUG_EVT:
+                aml_append(if_ctx, aml_call0(ACPI_CPU_CONTAINER "."
+                                             ACPI_CPU_SCAN_METHOD));
+                break;
             case ACPI_GED_PWR_DOWN_EVT:
                 aml_append(if_ctx,
                            aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE),
include/hw/acpi/cpu_hotplug.h +2 -0
index 48b291e45e..ef631750b4 100644
@@ -20,6 +20,8 @@
 #include "hw/acpi/cpu.h"
 
 #define ACPI_CPU_HOTPLUG_REG_LEN 12
+#define ACPI_CPU_SCAN_METHOD "CSCN"
+#define ACPI_CPU_CONTAINER "\\_SB.CPUS"
 
 typedef struct AcpiCpuHotplug {
     Object *device;
Signature · 1 line
2.34.1
#23 lore raw

Re: [PATCH V13 4/8] hw/acpi: Update GED _EVT method AML with CPU scan

Quoted 9 lines · On Fri, 7 Jun 2024 12:56:45 +0100 Salil Mehta <[email protected]> wrote:

OSPM evaluates _EVT method to map the event. The CPU hotplug event eventually results in start of the CPU scan. Scan figures out the CPU and the kind of event(plug/unplug) and notifies it back to the guest. Update the GED AML _EVT method with the call to \\_SB.CPUS.CSCN

Also, macro CPU_SCAN_METHOD might be referred in other places like during GED intialization so it makes sense to have its definition placed in some common header file like cpu_hotplug.h. But doing this can cause compilation break because of the conflicting macro definitions present in cpu.c and cpu_hotplug.c

one of the reasons is that you reusing legacy hw/acpi/cpu_hotplug.h, see below for suggestion.

Quoted 43 lines

and because both these files get compiled due to historic reasons of x86 world i.e. decision to use legacy(GPE.2)/modern(GED) CPU hotplug interface happens during runtime [1]. To mitigate above, for now, declare a new common macro ACPI_CPU_SCAN_METHOD for CPU scan method instead.
(This needs a separate discussion later on for clean-up)

Reference:
[1] https://lore.kernel.org/qemu-devel/[email protected]/

---
 hw/acpi/cpu.c                  | 2 +-
 hw/acpi/generic_event_device.c | 4 ++++
 include/hw/acpi/cpu_hotplug.h  | 2 ++
 3 files changed, 7 insertions(+), 1 deletion(-)
hw/acpi/cpu.c +1 -1
index 473b37ba88..af2b6655d2 100644
@@ -327,7 +327,7 @@ const VMStateDescription vmstate_cpu_hotplug = {
 #define CPUHP_RES_DEVICE  "PRES"
 #define CPU_LOCK          "CPLK"
 #define CPU_STS_METHOD    "CSTA"
-#define CPU_SCAN_METHOD   "CSCN"
+#define CPU_SCAN_METHOD   ACPI_CPU_SCAN_METHOD
 #define CPU_NOTIFY_METHOD "CTFY"
 #define CPU_EJECT_METHOD  "CEJ0"
 #define CPU_OST_METHOD    "COST"
hw/acpi/generic_event_device.c +0 -0
index 54d3b4bf9d..63226b0040 100644

hw/acpi/generic_event_device.c void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev, · line 114

Quoted 6 lines
hw/acpi/generic_event_device.c +3 -0
@@ -109,6 +109,10 @@ void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev,
                 aml_append(if_ctx, aml_call0(MEMORY_DEVICES_CONTAINER "."
                                              MEMORY_SLOT_SCAN_METHOD));
                 break;
+            case ACPI_GED_CPU_HOTPLUG_EVT:
+                aml_append(if_ctx, aml_call0(ACPI_CPU_CONTAINER "."
+                                             ACPI_CPU_SCAN_METHOD));

I don't particularly like exposing cpu hotplug internals for outside code and then making that code do plumbing hoping that nothing will explode in the future.

build_cpus_aml() takes event_handler_method to create a method that can be called by platform. What I suggest is to call that method here instead of trying to expose CPU hotplug internals and manually building call path here.
aka:
build_cpus_aml(event_handler_method = PATH_TO_GED_DEVICE.CSCN)
and then call here
aml_append(if_ctx, aml_call0(CSCN));
which will call CSCN in GED scope, that was be populated by build_cpus_aml() to do cpu scan properly without need to expose cpu hotplug internal names and then trying to fixup conflicts caused by that.

PS:
we should do the same for memory hotplug, we see in context above

Quoted 17 lines
hw/acpi/generic_event_device.c +1 -0
+                break;
             case ACPI_GED_PWR_DOWN_EVT:
                 aml_append(if_ctx,
                            aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE),
include/hw/acpi/cpu_hotplug.h +2 -0
index 48b291e45e..ef631750b4 100644
@@ -20,6 +20,8 @@
 #include "hw/acpi/cpu.h"
 
 #define ACPI_CPU_HOTPLUG_REG_LEN 12
+#define ACPI_CPU_SCAN_METHOD "CSCN"
+#define ACPI_CPU_CONTAINER "\\_SB.CPUS"
 
 typedef struct AcpiCpuHotplug {
     Object *device;
#24 lore raw

Re: [PATCH V13 4/8] hw/acpi: Update GED _EVT method AML with CPU scan

Hi Igor,

Quoted 8 lines · Igor Mammedov
Quoted 5 lines · On Fri, 7 Jun 2024 12:56:45 +0100 Salil Mehta <[email protected]> wrote:

OSPM evaluates _EVT method to map the event. The CPU hotplug event eventually results in start of the CPU scan. Scan figures out the CPU and the kind of event(plug/unplug) and notifies it back to the guest. Update the GED AML _EVT method with the call to \\_SB.CPUS.CSCN

Quoted 6 lines
Quoted 4 lines

Also, macro CPU_SCAN_METHOD might be referred in other places like during GED intialization so it makes sense to have its definition placed in some common header file like cpu_hotplug.h. But doing this can cause compilation break because of the conflicting macro definitions present in cpu.c and cpu_hotplug.c

one of the reasons is that you reusing legacy hw/acpi/cpu_hotplug.h, see below for suggestion.

ok

Quoted 45 lines
Quoted 45 lines

and because both these files get compiled due to historic reasons of x86 world i.e. decision to use legacy(GPE.2)/modern(GED) CPU hotplug interface happens during runtime [1]. To mitigate above, for now, declare a new common macro ACPI_CPU_SCAN_METHOD for CPU scan method instead.
(This needs a separate discussion later on for clean-up)

Reference:
[1] https://lore.kernel.org/qemu-devel/[email protected]/

---
  hw/acpi/cpu.c                  | 2 +-
  hw/acpi/generic_event_device.c | 4 ++++
  include/hw/acpi/cpu_hotplug.h  | 2 ++
  3 files changed, 7 insertions(+), 1 deletion(-)
hw/acpi/cpu.c +1 -1
index 473b37ba88..af2b6655d2 100644
@@ -327,7 +327,7 @@ const VMStateDescription vmstate_cpu_hotplug = {
  #define CPUHP_RES_DEVICE  "PRES"
  #define CPU_LOCK          "CPLK"
  #define CPU_STS_METHOD    "CSTA"
-#define CPU_SCAN_METHOD   "CSCN"
+#define CPU_SCAN_METHOD   ACPI_CPU_SCAN_METHOD
  #define CPU_NOTIFY_METHOD "CTFY"
  #define CPU_EJECT_METHOD  "CEJ0"
  #define CPU_OST_METHOD    "COST"
hw/acpi/generic_event_device.c +0 -0
index 54d3b4bf9d..63226b0040 100644
@@ -109,6 +109,10 @@ void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev,
                  aml_append(if_ctx, aml_call0(MEMORY_DEVICES_CONTAINER "."
Quoted 8 lines

hw/acpi/generic_event_device.c void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev, · line 114

Quoted 5 lines
hw/acpi/generic_event_device.c +3 -0
                                               MEMORY_SLOT_SCAN_METHOD));
                  break;
+            case ACPI_GED_CPU_HOTPLUG_EVT:
+                aml_append(if_ctx, aml_call0(ACPI_CPU_CONTAINER "."
+                                             ACPI_CPU_SCAN_METHOD));

I don't particularly like exposing cpu hotplug internals for outside code and then making that code do plumbing hoping that nothing will explode in the future.

I understand your point but I've followed what was already existing.

For example,

build_dsdt()

{

       [...]

    acpi_dsdt_add_uart(scope, &memmap[VIRT_UART], (irqmap[VIRT_UART] + ARM_SPI_BASE));
    if (vmc->acpi_expose_flash) {
        acpi_dsdt_add_flash(scope, &memmap[VIRT_FLASH]);
    }
    fw_cfg_acpi_dsdt_add(scope, &memmap[VIRT_FW_CFG]); virtio_acpi_dsdt_add(scope, memmap[VIRT_MMIO].base, memmap[VIRT_MMIO].size, (irqmap[VIRT_MMIO] + ARM_SPI_BASE),
                         0, NUM_VIRTIO_TRANSPORTS); acpi_dsdt_add_pci(scope, memmap, irqmap[VIRT_PCIE] + ARM_SPI_BASE, vms); if (vms->acpi_dev) {
        build_ged_aml(scope, "\\_SB."GED_DEVICE, HOTPLUG_HANDLER(vms->acpi_dev), irqmap[VIRT_ACPI_GED] + ARM_SPI_BASE, AML_SYSTEM_MEMORY, memmap[VIRT_ACPI_GED].base);
    } else {
        acpi_dsdt_add_gpio(scope, &memmap[VIRT_GPIO], (irqmap[VIRT_GPIO] + ARM_SPI_BASE));
    }

      [...]

}

Refactoring all of this code will create a noise in this patch-set.

Quoted 12 lines

build_cpus_aml() takes event_handler_method to create a method that can be called by platform. What I suggest is to call that method here instead of trying to expose CPU hotplug internals and manually building call path here.
aka:
build_cpus_aml(event_handler_method = PATH_TO_GED_DEVICE.CSCN)
and then call here
aml_append(if_ctx, aml_call0(CSCN));
which will call CSCN in GED scope, that was be populated by build_cpus_aml() to do cpu scan properly without need to expose cpu hotplug internal names and then trying to fixup conflicts caused by that.

Quoted 3 lines

PS:
we should do the same for memory hotplug, we see in context above

Although. I agree with your suggested change but I think this should be

carried in another patch-set.

Best, Salil.

Quoted 18 lines
Quoted 18 lines
hw/acpi/generic_event_device.c +1 -0
+                break;
              case ACPI_GED_PWR_DOWN_EVT:
                  aml_append(if_ctx,
                             aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE),
include/hw/acpi/cpu_hotplug.h +2 -0
index 48b291e45e..ef631750b4 100644
@@ -20,6 +20,8 @@
  #include "hw/acpi/cpu.h"
 
  #define ACPI_CPU_HOTPLUG_REG_LEN 12
+#define ACPI_CPU_SCAN_METHOD "CSCN"
+#define ACPI_CPU_CONTAINER "\\_SB.CPUS"
 
  typedef struct AcpiCpuHotplug {
      Object *device;
#25 lore raw

Re: [PATCH V13 4/8] hw/acpi: Update GED _EVT method AML with CPU scan

Quoted 122 lines · On Mon, 8 Jul 2024 05:21:06 +0000 Salil Mehta <[email protected]> wrote:

Hi Igor,

Quoted 8 lines · Igor Mammedov
Quoted 5 lines · On Fri, 7 Jun 2024 12:56:45 +0100 Salil Mehta <[email protected]> wrote:

OSPM evaluates _EVT method to map the event. The CPU hotplug event eventually results in start of the CPU scan. Scan figures out the CPU and the kind of event(plug/unplug) and notifies it back to the guest. Update the GED AML _EVT method with the call to \\_SB.CPUS.CSCN

Quoted 6 lines
Quoted 4 lines

Also, macro CPU_SCAN_METHOD might be referred in other places like during GED intialization so it makes sense to have its definition placed in some common header file like cpu_hotplug.h. But doing this can cause compilation break because of the conflicting macro definitions present in cpu.c and cpu_hotplug.c

one of the reasons is that you reusing legacy hw/acpi/cpu_hotplug.h, see below for suggestion.

ok

Quoted 45 lines
Quoted 45 lines

and because both these files get compiled due to historic reasons of x86 world i.e. decision to use legacy(GPE.2)/modern(GED) CPU hotplug interface happens during runtime [1]. To mitigate above, for now, declare a new common macro ACPI_CPU_SCAN_METHOD for CPU scan method instead.
(This needs a separate discussion later on for clean-up)

Reference:
[1] https://lore.kernel.org/qemu-devel/[email protected]/

---
  hw/acpi/cpu.c                  | 2 +-
  hw/acpi/generic_event_device.c | 4 ++++
  include/hw/acpi/cpu_hotplug.h  | 2 ++
  3 files changed, 7 insertions(+), 1 deletion(-)
hw/acpi/cpu.c +1 -1
index 473b37ba88..af2b6655d2 100644
@@ -327,7 +327,7 @@ const VMStateDescription vmstate_cpu_hotplug = {
  #define CPUHP_RES_DEVICE  "PRES"
  #define CPU_LOCK          "CPLK"
  #define CPU_STS_METHOD    "CSTA"
-#define CPU_SCAN_METHOD   "CSCN"
+#define CPU_SCAN_METHOD   ACPI_CPU_SCAN_METHOD
  #define CPU_NOTIFY_METHOD "CTFY"
  #define CPU_EJECT_METHOD  "CEJ0"
  #define CPU_OST_METHOD    "COST"
hw/acpi/generic_event_device.c +0 -0
index 54d3b4bf9d..63226b0040 100644
@@ -109,6 +109,10 @@ void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev,
                  aml_append(if_ctx, aml_call0(MEMORY_DEVICES_CONTAINER "."
Quoted 8 lines

hw/acpi/generic_event_device.c void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev, · line 114

Quoted 5 lines
hw/acpi/generic_event_device.c +3 -0
                                               MEMORY_SLOT_SCAN_METHOD));
                  break;
+            case ACPI_GED_CPU_HOTPLUG_EVT:
+                aml_append(if_ctx, aml_call0(ACPI_CPU_CONTAINER "."
+                                             ACPI_CPU_SCAN_METHOD));  

I don't particularly like exposing cpu hotplug internals for outside code and then making that code do plumbing hoping that nothing will explode in the future.

I understand your point but I've followed what was already existing.

For example,

build_dsdt()

{

       [...]

    acpi_dsdt_add_uart(scope, &memmap[VIRT_UART], (irqmap[VIRT_UART] + ARM_SPI_BASE)); if (vmc->acpi_expose_flash) { acpi_dsdt_add_flash(scope, &memmap[VIRT_FLASH]);
    }
    fw_cfg_acpi_dsdt_add(scope, &memmap[VIRT_FW_CFG]); virtio_acpi_dsdt_add(scope, memmap[VIRT_MMIO].base,
memmap[VIRT_MMIO].size,
                         (irqmap[VIRT_MMIO] + ARM_SPI_BASE), 0, NUM_VIRTIO_TRANSPORTS); acpi_dsdt_add_pci(scope, memmap, irqmap[VIRT_PCIE] + ARM_SPI_BASE,
vms);
    if (vms->acpi_dev) {
        build_ged_aml(scope, "\\_SB."GED_DEVICE, HOTPLUG_HANDLER(vms->acpi_dev), irqmap[VIRT_ACPI_GED] + ARM_SPI_BASE,
AML_SYSTEM_MEMORY,
                      memmap[VIRT_ACPI_GED].base);
    } else {
        acpi_dsdt_add_gpio(scope, &memmap[VIRT_GPIO], (irqmap[VIRT_GPIO] + ARM_SPI_BASE)); }

      [...]

}

Refactoring all of this code will create a noise in this patch-set.

Quoted 12 lines

build_cpus_aml() takes event_handler_method to create a method that can be called by platform. What I suggest is to call that method here instead of trying to expose CPU hotplug internals and manually building call path here.
aka:
build_cpus_aml(event_handler_method = PATH_TO_GED_DEVICE.CSCN)
and then call here
aml_append(if_ctx, aml_call0(CSCN));
which will call CSCN in GED scope, that was be populated by build_cpus_aml() to do cpu scan properly without need to expose cpu hotplug internal names and then trying to fixup conflicts caused by that.

Quoted 6 lines
Quoted 3 lines

PS:
we should do the same for memory hotplug, we see in context above

Although. I agree with your suggested change but I think this should be

carried in another patch-set.

I have to disagree with another patch-set on top as it introduces unnecessary code changes, wich 'another patch-set' will put back. (talking specifically about CPU hotplug)

Not to mention doing as it was suggested should reduce size of your series and overall complexity.

Quoted 22 lines

Best, Salil.

Quoted 18 lines
Quoted 18 lines
hw/acpi/generic_event_device.c +1 -0
+                break;
              case ACPI_GED_PWR_DOWN_EVT:
                  aml_append(if_ctx,
                             aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE),
include/hw/acpi/cpu_hotplug.h +2 -0
index 48b291e45e..ef631750b4 100644
@@ -20,6 +20,8 @@
  #include "hw/acpi/cpu.h"
 
  #define ACPI_CPU_HOTPLUG_REG_LEN 12
+#define ACPI_CPU_SCAN_METHOD "CSCN"
+#define ACPI_CPU_CONTAINER "\\_SB.CPUS"
 
  typedef struct AcpiCpuHotplug {
      Object *device;  
#26 lore raw

Re: [PATCH V13 4/8] hw/acpi: Update GED _EVT method AML with CPU scan

In reply to Igor Mammedov

Hi Igor,

Quoted 80 lines · Igor Mammedov
Quoted 9 lines · On Fri, 7 Jun 2024 12:56:45 +0100 Salil Mehta <[email protected]> wrote:

OSPM evaluates _EVT method to map the event. The CPU hotplug event eventually results in start of the CPU scan. Scan figures out the CPU and the kind of event(plug/unplug) and notifies it back to the guest. Update the GED AML _EVT method with the call to \\_SB.CPUS.CSCN

Also, macro CPU_SCAN_METHOD might be referred in other places like during GED intialization so it makes sense to have its definition placed in some common header file like cpu_hotplug.h. But doing this can cause compilation break because of the conflicting macro definitions present in cpu.c and cpu_hotplug.c

one of the reasons is that you reusing legacy hw/acpi/cpu_hotplug.h, see below for suggestion.

Quoted 43 lines

and because both these files get compiled due to historic reasons of x86 world i.e. decision to use legacy(GPE.2)/modern(GED) CPU hotplug interface happens during runtime [1]. To mitigate above, for now, declare a new common macro ACPI_CPU_SCAN_METHOD for CPU scan method instead.
(This needs a separate discussion later on for clean-up)

Reference:
[1] https://lore.kernel.org/qemu-devel/[email protected]/

---
  hw/acpi/cpu.c                  | 2 +-
  hw/acpi/generic_event_device.c | 4 ++++
  include/hw/acpi/cpu_hotplug.h  | 2 ++
  3 files changed, 7 insertions(+), 1 deletion(-)
hw/acpi/cpu.c +1 -1
index 473b37ba88..af2b6655d2 100644
@@ -327,7 +327,7 @@ const VMStateDescription vmstate_cpu_hotplug = {
  #define CPUHP_RES_DEVICE  "PRES"
  #define CPU_LOCK          "CPLK"
  #define CPU_STS_METHOD    "CSTA"
-#define CPU_SCAN_METHOD   "CSCN"
+#define CPU_SCAN_METHOD   ACPI_CPU_SCAN_METHOD
  #define CPU_NOTIFY_METHOD "CTFY"
  #define CPU_EJECT_METHOD  "CEJ0"
  #define CPU_OST_METHOD    "COST"
hw/acpi/generic_event_device.c +0 -0
index 54d3b4bf9d..63226b0040 100644

hw/acpi/generic_event_device.c void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev, · line 114

Quoted 6 lines
hw/acpi/generic_event_device.c +3 -0
@@ -109,6 +109,10 @@ void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev,
                  aml_append(if_ctx, aml_call0(MEMORY_DEVICES_CONTAINER "."
                                               MEMORY_SLOT_SCAN_METHOD));
                  break;
+            case ACPI_GED_CPU_HOTPLUG_EVT:
+                aml_append(if_ctx, aml_call0(ACPI_CPU_CONTAINER "."
+                                             ACPI_CPU_SCAN_METHOD));

I don't particularly like exposing cpu hotplug internals for outside code and then making that code do plumbing hoping that nothing will explode in the future.

build_cpus_aml() takes event_handler_method to create a method that can be called by platform. What I suggest is to call that method here instead of trying to expose CPU hotplug internals and manually building call path here.
aka:
build_cpus_aml(event_handler_method = PATH_TO_GED_DEVICE.CSCN)
and then call here
aml_append(if_ctx, aml_call0(CSCN));
which will call CSCN in GED scope, that was be populated by build_cpus_aml() to do cpu scan properly without need to expose cpu hotplug internal names and then trying to fixup conflicts caused by that.

Quoted 2 lines

PS:
we should do the same for memory hotplug, we see in context above

In the x86 world and ARM, two different types of event handling mechanisms are
used: one based on GPE and the other on GED. The latter has its own placeholder within the generic_event_device.c file, which also multiplexes other GED events. Multiplexing AMLs for two different types of handlers into build_cpus_aml() seems to be a fundamental mistake here. For CPU handling, this should also not be done because x86 deals with legacy hotplug and modern hotplug differently but still uses the same GPE-based event handling for both. Moreover, which type of handler should be used depends upon the context from which build_cpus_aml() is called and it would be ugly to demultiplex these inside the build_cpus_aml() code.

Therefore, my suggestion is:
1. Keep the CPU’s _EVT handling code in the generic_event_device.c as it is.
2. Pull out the event handler method and CPU scan call-related initialization entirely out of the CPU’s AML code (i.e., both in build_cpus_aml() and build_legacy_cpu_hotplug_aml()).
3. Remove the input parameter of event_handler in build_cpus_aml().
4. Create a separate function like build_gpe_aml() and use this function in the following places:

File: hw/i386/acpi-build.c

build_dsdt()
{
   [...]
    scope =  aml_scope("_GPE");
   [...]
    aml_append(dsdt, scope);

    if (pcmc->legacy_cpu_hotplug) { build_legacy_cpu_hotplug_aml(dsdt, machine, pm->cpu_hp_io_base); } else {
        CPUHotplugFeatures opts = {
            .acpi_1_compatible = true, .has_legacy_cphp = true, .smi_path = pm->smi_on_cpuhp ? "\\_SB.PCI0.SMI0.SMIC" : NULL, .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
        };
        build_cpus_aml(dsdt, machine, opts, pc_madt_cpu_entry, -                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02"); +                      pm->cpu_hp_io_base, "\\_SB.PCI0");
+      build_gpe_aml(...,, ,"\\_GPE._E02")

    }
   [...]
}

File: hw/acpi/cpu.c

@@ -343,9 +343,10 @@ const VMStateDescription vmstate_cpu_hotplug = { #define CPU_FW_EJECT_EVENT "CEJF"

 void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts, -                    build_madt_cpu_fn build_madt_cpu, hwaddr io_base, +                    build_madt_cpu_fn build_madt_cpu, hwaddr base_addr, const char *res_root,
-                    const char *event_handler_method)
+                    AmlRegionSpace rs)
 {
     Aml *ifctx;
     Aml *field;

That said, if you still wish you proceed with your suggestions I can go ahead and do it but please understand that I'll have to put a check to avoid adding call to CPU Scan for am/virt platform since we would want to add that call as part of the GED/AML code. This is unnecessary and would look ugly.

Thanks
Salil.

Quoted 19 lines
Quoted 19 lines
hw/acpi/generic_event_device.c +1 -0
+                break;
              case ACPI_GED_PWR_DOWN_EVT:
                  aml_append(if_ctx,
                             aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE),
include/hw/acpi/cpu_hotplug.h +2 -0
index 48b291e45e..ef631750b4 100644
@@ -20,6 +20,8 @@
  #include "hw/acpi/cpu.h"
 
  #define ACPI_CPU_HOTPLUG_REG_LEN 12
+#define ACPI_CPU_SCAN_METHOD "CSCN"
+#define ACPI_CPU_CONTAINER "\\_SB.CPUS"
 
  typedef struct AcpiCpuHotplug {
      Object *device;
#27 lore raw

Re: [PATCH V13 4/8] hw/acpi: Update GED _EVT method AML with CPU scan

Quoted 116 lines · On Thu, 11 Jul 2024 03:29:40 +0000 Salil Mehta <[email protected]> wrote:

Hi Igor,

Quoted 80 lines · Igor Mammedov
Quoted 9 lines · On Fri, 7 Jun 2024 12:56:45 +0100 Salil Mehta <[email protected]> wrote:

OSPM evaluates _EVT method to map the event. The CPU hotplug event eventually results in start of the CPU scan. Scan figures out the CPU and the kind of event(plug/unplug) and notifies it back to the guest. Update the GED AML _EVT method with the call to \\_SB.CPUS.CSCN

Also, macro CPU_SCAN_METHOD might be referred in other places like during GED intialization so it makes sense to have its definition placed in some common header file like cpu_hotplug.h. But doing this can cause compilation break because of the conflicting macro definitions present in cpu.c and cpu_hotplug.c

one of the reasons is that you reusing legacy hw/acpi/cpu_hotplug.h, see below for suggestion.

Quoted 43 lines

and because both these files get compiled due to historic reasons of x86 world i.e. decision to use legacy(GPE.2)/modern(GED) CPU hotplug interface happens during runtime [1]. To mitigate above, for now, declare a new common macro ACPI_CPU_SCAN_METHOD for CPU scan method instead.
(This needs a separate discussion later on for clean-up)

Reference:
[1] https://lore.kernel.org/qemu-devel/[email protected]/

---
  hw/acpi/cpu.c                  | 2 +-
  hw/acpi/generic_event_device.c | 4 ++++
  include/hw/acpi/cpu_hotplug.h  | 2 ++
  3 files changed, 7 insertions(+), 1 deletion(-)
hw/acpi/cpu.c +1 -1
index 473b37ba88..af2b6655d2 100644
@@ -327,7 +327,7 @@ const VMStateDescription vmstate_cpu_hotplug = {
  #define CPUHP_RES_DEVICE  "PRES"
  #define CPU_LOCK          "CPLK"
  #define CPU_STS_METHOD    "CSTA"
-#define CPU_SCAN_METHOD   "CSCN"
+#define CPU_SCAN_METHOD   ACPI_CPU_SCAN_METHOD
  #define CPU_NOTIFY_METHOD "CTFY"
  #define CPU_EJECT_METHOD  "CEJ0"
  #define CPU_OST_METHOD    "COST"
hw/acpi/generic_event_device.c +0 -0
index 54d3b4bf9d..63226b0040 100644

hw/acpi/generic_event_device.c void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev, · line 114

Quoted 6 lines
hw/acpi/generic_event_device.c +3 -0
@@ -109,6 +109,10 @@ void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev,
                  aml_append(if_ctx, aml_call0(MEMORY_DEVICES_CONTAINER "."
                                               MEMORY_SLOT_SCAN_METHOD));
                  break;
+            case ACPI_GED_CPU_HOTPLUG_EVT:
+                aml_append(if_ctx, aml_call0(ACPI_CPU_CONTAINER "."
+                                             ACPI_CPU_SCAN_METHOD));  

I don't particularly like exposing cpu hotplug internals for outside code and then making that code do plumbing hoping that nothing will explode in the future.

build_cpus_aml() takes event_handler_method to create a method that can be called by platform. What I suggest is to call that method here instead of trying to expose CPU hotplug internals and manually building call path here.
aka:
build_cpus_aml(event_handler_method = PATH_TO_GED_DEVICE.CSCN)
and then call here
aml_append(if_ctx, aml_call0(CSCN));
which will call CSCN in GED scope, that was be populated by build_cpus_aml() to do cpu scan properly without need to expose cpu hotplug internal names and then trying to fixup conflicts caused by that.

Quoted 2 lines

PS:
we should do the same for memory hotplug, we see in context above

In the x86 world and ARM, two different types of event handling mechanisms are
used: one based on GPE and the other on GED. The latter has its own placeholder
within the generic_event_device.c file, which also multiplexes other GED events.
Multiplexing AMLs for two different types of handlers into build_cpus_aml() seems
to be a fundamental mistake here. For CPU handling, this should also not be done
because x86 deals with legacy hotplug and modern hotplug differently but still
uses the same GPE-based event handling for both. Moreover, which type of handler
should be used depends upon the context from which build_cpus_aml() is called
and it would be ugly to demultiplex these inside the build_cpus_aml() code.

Therefore, my suggestion is:
1. Keep the CPU’s _EVT handling code in the generic_event_device.c as it is.
2. Pull out the event handler method and CPU scan call-related initialization entirely
    out of the CPU’s AML code (i.e., both in build_cpus_aml() and
build_legacy_cpu_hotplug_aml()).
3. Remove the input parameter of event_handler in build_cpus_aml().
4. Create a separate function like build_gpe_aml() and use this function in the
     following places:

Quoted 1 line

File: hw/i386/acpi-build.c

I'm not convinced by above arguments yet (perhaps I don't see a problem you are observing), so I'd like to keep cpu hotplug isolated/not exposed as much as possible unless it's proven that it will not work as is. (see for more below)

Quoted 41 lines

build_dsdt()
{
   [...]
    scope =  aml_scope("_GPE");
   [...]
    aml_append(dsdt, scope);

    if (pcmc->legacy_cpu_hotplug) { build_legacy_cpu_hotplug_aml(dsdt, machine, pm->cpu_hp_io_base); } else {
        CPUHotplugFeatures opts = {
            .acpi_1_compatible = true, .has_legacy_cphp = true, .smi_path = pm->smi_on_cpuhp ? "\\_SB.PCI0.SMI0.SMIC" : NULL, .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
        };
        build_cpus_aml(dsdt, machine, opts, pc_madt_cpu_entry,
-                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02"); +                      pm->cpu_hp_io_base, "\\_SB.PCI0");
+      build_gpe_aml(...,, ,"\\_GPE._E02")

    }
   [...]
}

File: hw/acpi/cpu.c

(quoted diff) +2 -2
@@ -343,9 +343,10 @@ const VMStateDescription vmstate_cpu_hotplug = {
  #define CPU_FW_EJECT_EVENT "CEJF"
 
  void build_cpus_aml(Aml *table, MachineState *machine, 
 CPUHotplugFeatures opts,
-                    build_madt_cpu_fn build_madt_cpu, hwaddr io_base,
+                    build_madt_cpu_fn build_madt_cpu, hwaddr base_addr,
                      const char *res_root,
-                    const char *event_handler_method)
+                    AmlRegionSpace rs)
  {
      Aml *ifctx;
      Aml *field;
Quoted 5 lines

That said, if you still wish you proceed with your suggestions I can go ahead and do it but please understand that I'll have to put a check to avoid adding call to CPU Scan for am/virt platform since we would want to add that call as part of the GED/AML code. This is unnecessary and would look ugly.

I just don't get why you have to call CPU Scan explicitly from arm/virt side. (maybe my suggestion was lost in translation/was not clear enough) Let's pretend that hw/i386/acpi-build.c is an arm/virt code and demo what I was suggesting.

hw/acpi/generic_event_device.c +3 -0
index 2d6e91b124..33addb6275 100644
@@ -117,6 +117,9 @@ void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev,
                            aml_notify(aml_name("\\_SB.NVDR"),
                                       aml_int(0x80)));
                 break;
+            case ACPI_GED_CPU_HOTPLUG_EVT:
+                aml_append(if_ctx, aml_call0("\\_SB.GED.CPEV"));
+                break
             default:
                 /*
                  * Please make sure all the events in ged_supported_events[]
hw/i386/acpi-build.c +1 -1
index f4e366f64f..8b4f422652 100644
@@ -1536,7 +1536,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
             .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
         };
         build_cpus_aml(dsdt, machine, opts, pc_madt_cpu_entry,
-                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02");
+                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_SB.GED.CPEV");
     }
 

this way build_cpus_aml() will create and populate with scan \\_SB.GED.CPEV method.

which is then called by
GED._EVT()

    ...
    if cpuhotplug
          \\_SB.GED.CPEV()
    ...

there is no multiplexing of cpuhp event in build_cpus_aml, the only event multiplexer is GED._EVT() as it should be. And you don't have to expose cpu hotplug internals to any other place[s].

PS:
For legacy cphp handling acpi/cpu.c has only _INI method that is created when opts.has_legacy_cphp is true.
we should be able to get rid of it when 2.6 machine type is removed. But ARM [or anything else] don't have to be aware of it if you use static initializer like it's done in hw/i386/acpi-build.c and just ignore non relevant fields.

Quoted 24 lines

Thanks
Salil.

Quoted 19 lines
Quoted 19 lines
hw/acpi/generic_event_device.c +1 -0
+                break;
              case ACPI_GED_PWR_DOWN_EVT:
                  aml_append(if_ctx,
                             aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE),
include/hw/acpi/cpu_hotplug.h +2 -0
index 48b291e45e..ef631750b4 100644
@@ -20,6 +20,8 @@
  #include "hw/acpi/cpu.h"
 
  #define ACPI_CPU_HOTPLUG_REG_LEN 12
+#define ACPI_CPU_SCAN_METHOD "CSCN"
+#define ACPI_CPU_CONTAINER "\\_SB.CPUS"
 
  typedef struct AcpiCpuHotplug {
      Object *device;  
#28 lore raw

Re: [PATCH V13 4/8] hw/acpi: Update GED _EVT method AML with CPU scan

Quoted 41 lines · Igor Mammedov
Quoted 38 lines · On Thu, 11 Jul 2024 03:29:40 +0000 Salil Mehta <[email protected]> wrote:

Hi Igor,

Quoted 34 lines · Igor Mammedov
Quoted 9 lines · On Fri, 7 Jun 2024 12:56:45 +0100 Salil Mehta <[email protected]> wrote:

OSPM evaluates _EVT method to map the event. The CPU hotplug event eventually results in start of the CPU scan. Scan figures out the CPU and the kind of event(plug/unplug) and notifies it back to the guest. Update the GED AML _EVT method with the call to \\_SB.CPUS.CSCN

Also, macro CPU_SCAN_METHOD might be referred in other places like during GED intialization so it makes sense to have its definition placed in some common header file like cpu_hotplug.h. But doing this can cause compilation break because of the conflicting macro definitions present in cpu.c and cpu_hotplug.c

one of the reasons is that you reusing legacy hw/acpi/cpu_hotplug.h, see below for suggestion.

Quoted 19 lines

and because both these files get compiled due to historic reasons of x86 world i.e. decision to use legacy(GPE.2)/modern(GED) CPU hotplug interface happens during runtime [1]. To mitigate above, for now, declare a new common macro ACPI_CPU_SCAN_METHOD for CPU scan method instead.
(This needs a separate discussion later on for clean-up)

Reference:
[1] https://lore.kernel.org/qemu-devel/[email protected]/

[...]

Quoted 26 lines
hw/acpi/generic_event_device.c +3 -0
index 2d6e91b124..33addb6275 100644
@@ -117,6 +117,9 @@ void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev,
                             aml_notify(aml_name("\\_SB.NVDR"),
                                        aml_int(0x80)));
                  break;
+            case ACPI_GED_CPU_HOTPLUG_EVT:
+                aml_append(if_ctx, aml_call0("\\_SB.GED.CPEV"));
+                break
              default:
                  /*
                   * Please make sure all the events in ged_supported_events[]
hw/i386/acpi-build.c +1 -1
index f4e366f64f..8b4f422652 100644
@@ -1536,7 +1536,7 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
              .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
          };
          build_cpus_aml(dsdt, machine, opts, pc_madt_cpu_entry,
-                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02");
+                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_SB.GED.CPEV");
      }
 
Quoted 2 lines

this way build_cpus_aml() will create and populate with scan \\_SB.GED.CPEV method.

I see. Perhaps, I failed to realise the semantics in the AML sense i.e. exporting a call to a method belonging to one scope to another (\\_SB.CPUS to \\_SB.GED). Yes, with above change we can avoid calling \\_SB.CPUS.CSCN method direclty from within GED _EVT. Sorry for the noise.

I will do the change.

Thanks

Quoted 7 lines

PS:
For legacy cphp handling acpi/cpu.c has only _INI method that is created when opts.has_legacy_cphp is true.
we should be able to get rid of it when 2.6 machine type is removed. But ARM [or anything else] don't have to be aware of it if you use static initializer like it's done in hw/i386/acpi-build.c and just ignore non relevant fields.

ok. thanks for the clarification.

Best regards
Salil.

#29 lore raw

PATCH v13 5/8 hw/acpi: Update CPUs AML with cpu-(ctrl)dev change

In reply to Salil Mehta

CPUs Control device(\\_SB.PCI0) register interface for the x86 arch is IO port based and existing CPUs AML code assumes _CRS objects would evaluate to a system resource which describes IO Port address. But on ARM arch CPUs control device(\\_SB.PRES) register interface is memory-mapped hence _CRS object should evaluate to system resource which describes memory-mapped base address. Update build CPUs AML function to accept both IO/MEMORY region spaces and accordingly update the _CRS object.

On x86, CPU Hotplug uses Generic ACPI GPE Block Bit 2 (GPE.2) event handler to notify OSPM about any CPU hot(un)plug events. Latest CPU Hotplug is based on ACPI Generic Event Device framework and uses ACPI GED device for the same. Not all architectures support GPE based CPU Hotplug event handler. Hence, make AML for GPE.2 event handler conditional.

---
 hw/acpi/cpu.c         | 23 ++++++++++++++++-------
 hw/i386/acpi-build.c  |  3 ++-
 include/hw/acpi/cpu.h |  5 +++--
 3 files changed, 21 insertions(+), 10 deletions(-)
hw/acpi/cpu.c +16 -7
index af2b6655d2..4c63514b16 100644
@@ -343,9 +343,10 @@ const VMStateDescription vmstate_cpu_hotplug = {
 #define CPU_FW_EJECT_EVENT "CEJF"
 
 void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
-                    build_madt_cpu_fn build_madt_cpu, hwaddr io_base,
+                    build_madt_cpu_fn build_madt_cpu, hwaddr base_addr,
                     const char *res_root,
-                    const char *event_handler_method)
+                    const char *event_handler_method,
+                    AmlRegionSpace rs)
 {
     Aml *ifctx;
     Aml *field;
@@ -370,13 +371,19 @@ void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
         aml_append(cpu_ctrl_dev, aml_mutex(CPU_LOCK, 0));
 
         crs = aml_resource_template();
-        aml_append(crs, aml_io(AML_DECODE16, io_base, io_base, 1,
+        if (rs == AML_SYSTEM_IO) {
+            aml_append(crs, aml_io(AML_DECODE16, base_addr, base_addr, 1,
                                ACPI_CPU_HOTPLUG_REG_LEN));
+        } else {
+            aml_append(crs, aml_memory32_fixed(base_addr,
+                               ACPI_CPU_HOTPLUG_REG_LEN, AML_READ_WRITE));
+        }
+
         aml_append(cpu_ctrl_dev, aml_name_decl("_CRS", crs));
 
         /* declare CPU hotplug MMIO region with related access fields */
         aml_append(cpu_ctrl_dev,
-            aml_operation_region("PRST", AML_SYSTEM_IO, aml_int(io_base),
+            aml_operation_region("PRST", rs, aml_int(base_addr),
                                  ACPI_CPU_HOTPLUG_REG_LEN));
 
         field = aml_field("PRST", AML_BYTE_ACC, AML_NOLOCK,
@@ -700,9 +707,11 @@ void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
     aml_append(sb_scope, cpus_dev);
     aml_append(table, sb_scope);
 
-    method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
-    aml_append(method, aml_call0("\\_SB.CPUS." CPU_SCAN_METHOD));
-    aml_append(table, method);
+    if (event_handler_method) {
+        method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
+        aml_append(method, aml_call0("\\_SB.CPUS." CPU_SCAN_METHOD));
+        aml_append(table, method);
+    }
 
     g_free(cphp_res_path);
 }
hw/i386/acpi-build.c +2 -1
index 53f804ac16..b73b136605 100644
@@ -1537,7 +1537,8 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
             .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
         };
         build_cpus_aml(dsdt, machine, opts, pc_madt_cpu_entry,
-                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02");
+                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02",
+                       AML_SYSTEM_IO);
     }
 
     if (pcms->memhp_io_base && nr_mem) {
include/hw/acpi/cpu.h +3 -2
index e6e1a9ef59..48cded697c 100644
@@ -61,9 +61,10 @@ typedef void (*build_madt_cpu_fn)(int uid, const CPUArchIdList *apic_ids,
                                   GArray *entry, bool force_enabled);
 
 void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
-                    build_madt_cpu_fn build_madt_cpu, hwaddr io_base,
+                    build_madt_cpu_fn build_madt_cpu, hwaddr base_addr,
                     const char *res_root,
-                    const char *event_handler_method);
+                    const char *event_handler_method,
+                    AmlRegionSpace rs);
 
 void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list);
 
Signature · 1 line
2.34.1
#30 lore raw

Re: [PATCH V13 5/8] hw/acpi: Update CPUs AML with cpu-(ctrl)dev change

Quoted 7 lines · On Fri, 7 Jun 2024 12:56:46 +0100 Salil Mehta <[email protected]> wrote:

CPUs Control device(\\_SB.PCI0) register interface for the x86 arch is IO port based and existing CPUs AML code assumes _CRS objects would evaluate to a system resource which describes IO Port address. But on ARM arch CPUs control device(\\_SB.PRES) register interface is memory-mapped hence _CRS object should evaluate to system resource which describes memory-mapped base address. Update build CPUs AML function to accept both IO/MEMORY region spaces and accordingly update the _CRS object.

ack for above change

but below part is one too many different changes withing 1 patch. anyways, GPE part probably won't be needed if you follow suggestion made on previous patch.

Quoted 40 lines

On x86, CPU Hotplug uses Generic ACPI GPE Block Bit 2 (GPE.2) event handler to notify OSPM about any CPU hot(un)plug events. Latest CPU Hotplug is based on ACPI Generic Event Device framework and uses ACPI GED device for the same. Not all architectures support GPE based CPU Hotplug event handler. Hence, make AML for GPE.2 event handler conditional.

---
 hw/acpi/cpu.c         | 23 ++++++++++++++++-------
 hw/i386/acpi-build.c  |  3 ++-
 include/hw/acpi/cpu.h |  5 +++--
 3 files changed, 21 insertions(+), 10 deletions(-)
hw/acpi/cpu.c +3 -2
index af2b6655d2..4c63514b16 100644
@@ -343,9 +343,10 @@ const VMStateDescription vmstate_cpu_hotplug = {
 #define CPU_FW_EJECT_EVENT "CEJF"
 
 void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
-                    build_madt_cpu_fn build_madt_cpu, hwaddr io_base,
+                    build_madt_cpu_fn build_madt_cpu, hwaddr base_addr,
                     const char *res_root,
-                    const char *event_handler_method)
+                    const char *event_handler_method,
+                    AmlRegionSpace rs)
 {
     Aml *ifctx;
     Aml *field;

hw/acpi/cpu.c void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts, · line 377

Quoted 8 lines
hw/acpi/cpu.c +3 -1
@@ -370,13 +371,19 @@ void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
         aml_append(cpu_ctrl_dev, aml_mutex(CPU_LOCK, 0));
 
         crs = aml_resource_template();
-        aml_append(crs, aml_io(AML_DECODE16, io_base, io_base, 1,
+        if (rs == AML_SYSTEM_IO) {
+            aml_append(crs, aml_io(AML_DECODE16, base_addr, base_addr, 1,
                                ACPI_CPU_HOTPLUG_REG_LEN));
+        } else {

else
if (rs == yours type)

hw/acpi/cpu.c void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts, · line 380

Quoted 3 lines
hw/acpi/cpu.c +3 -0
+            aml_append(crs, aml_memory32_fixed(base_addr,
+                               ACPI_CPU_HOTPLUG_REG_LEN, AML_READ_WRITE));
+        }

else assert on not supported input

Quoted 56 lines
hw/acpi/cpu.c +7 -4
+
         aml_append(cpu_ctrl_dev, aml_name_decl("_CRS", crs));
 
         /* declare CPU hotplug MMIO region with related access fields */
         aml_append(cpu_ctrl_dev,
-            aml_operation_region("PRST", AML_SYSTEM_IO, aml_int(io_base),
+            aml_operation_region("PRST", rs, aml_int(base_addr),
                                  ACPI_CPU_HOTPLUG_REG_LEN));
 
         field = aml_field("PRST", AML_BYTE_ACC, AML_NOLOCK,
@@ -700,9 +707,11 @@ void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
     aml_append(sb_scope, cpus_dev);
     aml_append(table, sb_scope);
 
-    method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
-    aml_append(method, aml_call0("\\_SB.CPUS." CPU_SCAN_METHOD));
-    aml_append(table, method);
+    if (event_handler_method) {
+        method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
+        aml_append(method, aml_call0("\\_SB.CPUS." CPU_SCAN_METHOD));
+        aml_append(table, method);
+    }
 
     g_free(cphp_res_path);
 }
hw/i386/acpi-build.c +2 -1
index 53f804ac16..b73b136605 100644
@@ -1537,7 +1537,8 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
             .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
         };
         build_cpus_aml(dsdt, machine, opts, pc_madt_cpu_entry,
-                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02");
+                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02",
+                       AML_SYSTEM_IO);
     }
 
     if (pcms->memhp_io_base && nr_mem) {
include/hw/acpi/cpu.h +3 -2
index e6e1a9ef59..48cded697c 100644
@@ -61,9 +61,10 @@ typedef void (*build_madt_cpu_fn)(int uid, const CPUArchIdList *apic_ids,
                                   GArray *entry, bool force_enabled);
 
 void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
-                    build_madt_cpu_fn build_madt_cpu, hwaddr io_base,
+                    build_madt_cpu_fn build_madt_cpu, hwaddr base_addr,
                     const char *res_root,
-                    const char *event_handler_method);
+                    const char *event_handler_method,
+                    AmlRegionSpace rs);
 
 void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list);
 
#31 lore raw

Re: [PATCH V13 5/8] hw/acpi: Update CPUs AML with cpu-(ctrl)dev change

Quoted 11 lines · Igor Mammedov
Quoted 7 lines · On Fri, 7 Jun 2024 12:56:46 +0100 Salil Mehta <[email protected]> wrote:

CPUs Control device(\\_SB.PCI0) register interface for the x86 arch is IO port based and existing CPUs AML code assumes _CRS objects would evaluate to a system resource which describes IO Port address. But on ARM arch CPUs control device(\\_SB.PRES) register interface is memory-mapped hence _CRS object should evaluate to system resource which describes memory-mapped base address. Update build CPUs AML function to accept both IO/MEMORY region spaces and accordingly update the _CRS object.

ack for above change

Thanks

Quoted 5 lines

but below part is one too many different changes withing 1 patch. anyways, GPE part probably won't be needed if you follow suggestion made on previous patch.

The change mentioned in the earlier patches might end up creating

noise for this patch-set as one will have to touch the Memory Hotplug

part as well. I'm willing to do that change but I think it is a noise for

this patch-set, really.

Quoted 51 lines
Quoted 41 lines

On x86, CPU Hotplug uses Generic ACPI GPE Block Bit 2 (GPE.2) event handler to notify OSPM about any CPU hot(un)plug events. Latest CPU Hotplug is based on ACPI Generic Event Device framework and uses ACPI GED device for the same. Not all architectures support GPE based CPU Hotplug event handler. Hence, make AML for GPE.2 event handler conditional.

---
  hw/acpi/cpu.c         | 23 ++++++++++++++++-------
  hw/i386/acpi-build.c  |  3 ++-
  include/hw/acpi/cpu.h |  5 +++--
  3 files changed, 21 insertions(+), 10 deletions(-)
hw/acpi/cpu.c +3 -2
index af2b6655d2..4c63514b16 100644
@@ -343,9 +343,10 @@ const VMStateDescription vmstate_cpu_hotplug = {
  #define CPU_FW_EJECT_EVENT "CEJF"
 
  void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
-                    build_madt_cpu_fn build_madt_cpu, hwaddr io_base,
+                    build_madt_cpu_fn build_madt_cpu, hwaddr base_addr,
                      const char *res_root,
-                    const char *event_handler_method)
+                    const char *event_handler_method,
+                    AmlRegionSpace rs)
  {
      Aml *ifctx;
      Aml *field;

hw/acpi/cpu.c void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts, · line 377

Quoted 8 lines
hw/acpi/cpu.c +3 -1
@@ -370,13 +371,19 @@ void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
          aml_append(cpu_ctrl_dev, aml_mutex(CPU_LOCK, 0));
 
          crs = aml_resource_template();
-        aml_append(crs, aml_io(AML_DECODE16, io_base, io_base, 1,
+        if (rs == AML_SYSTEM_IO) {
+            aml_append(crs, aml_io(AML_DECODE16, base_addr, base_addr, 1,
                                 ACPI_CPU_HOTPLUG_REG_LEN));
+        } else {

else
if (rs == yours type)

Quoted 4 lines

hw/acpi/cpu.c void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts, · line 380

Quoted 3 lines
hw/acpi/cpu.c +3 -0
+            aml_append(crs, aml_memory32_fixed(base_addr,
+                               ACPI_CPU_HOTPLUG_REG_LEN, AML_READ_WRITE));
+        }

else assert on not supported input

Sure, no problem. I can incorporate the change.

Thanks, Salil.

Quoted 57 lines
Quoted 57 lines
hw/acpi/cpu.c +7 -4
+
          aml_append(cpu_ctrl_dev, aml_name_decl("_CRS", crs));
 
          /* declare CPU hotplug MMIO region with related access fields */
          aml_append(cpu_ctrl_dev,
-            aml_operation_region("PRST", AML_SYSTEM_IO, aml_int(io_base),
+            aml_operation_region("PRST", rs, aml_int(base_addr),
                                   ACPI_CPU_HOTPLUG_REG_LEN));
 
          field = aml_field("PRST", AML_BYTE_ACC, AML_NOLOCK,
@@ -700,9 +707,11 @@ void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
      aml_append(sb_scope, cpus_dev);
      aml_append(table, sb_scope);
 
-    method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
-    aml_append(method, aml_call0("\\_SB.CPUS." CPU_SCAN_METHOD));
-    aml_append(table, method);
+    if (event_handler_method) {
+        method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
+        aml_append(method, aml_call0("\\_SB.CPUS." CPU_SCAN_METHOD));
+        aml_append(table, method);
+    }
 
      g_free(cphp_res_path);
  }
hw/i386/acpi-build.c +2 -1
index 53f804ac16..b73b136605 100644
@@ -1537,7 +1537,8 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
              .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
          };
          build_cpus_aml(dsdt, machine, opts, pc_madt_cpu_entry,
-                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02");
+                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02",
+                       AML_SYSTEM_IO);
      }
 
      if (pcms->memhp_io_base && nr_mem) {
include/hw/acpi/cpu.h +3 -2
index e6e1a9ef59..48cded697c 100644
@@ -61,9 +61,10 @@ typedef void (*build_madt_cpu_fn)(int uid, const CPUArchIdList *apic_ids,
                                    GArray *entry, bool force_enabled);
 
  void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
-                    build_madt_cpu_fn build_madt_cpu, hwaddr io_base,
+                    build_madt_cpu_fn build_madt_cpu, hwaddr base_addr,
                      const char *res_root,
-                    const char *event_handler_method);
+                    const char *event_handler_method,
+                    AmlRegionSpace rs);
 
  void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list);
 
#32 lore raw

Re: [PATCH V13 5/8] hw/acpi: Update CPUs AML with cpu-(ctrl)dev change

Quoted 19 lines · On Mon, 8 Jul 2024 05:26:00 +0000 Salil Mehta <[email protected]> wrote:
Quoted 11 lines · Igor Mammedov
Quoted 7 lines · On Fri, 7 Jun 2024 12:56:46 +0100 Salil Mehta <[email protected]> wrote:

CPUs Control device(\\_SB.PCI0) register interface for the x86 arch is IO port based and existing CPUs AML code assumes _CRS objects would evaluate to a system resource which describes IO Port address. But on ARM arch CPUs control device(\\_SB.PRES) register interface is memory-mapped hence _CRS object should evaluate to system resource which describes memory-mapped base address. Update build CPUs AML function to accept both IO/MEMORY region spaces and accordingly update the _CRS object.

ack for above change

Thanks

Quoted 5 lines

but below part is one too many different changes withing 1 patch. anyways, GPE part probably won't be needed if you follow suggestion made on previous patch.

Quoted 7 lines

The change mentioned in the earlier patches might end up creating

noise for this patch-set as one will have to touch the Memory Hotplug

part as well. I'm willing to do that change but I think it is a noise for

this patch-set, really.

you don't have to touch memory hotplug,
but fixing it up (as a separate patch of cause) to be consistent with cpu hotplug would be nice.

Quoted 119 lines
Quoted 52 lines
Quoted 42 lines

On x86, CPU Hotplug uses Generic ACPI GPE Block Bit 2 (GPE.2) event handler to notify OSPM about any CPU hot(un)plug events. Latest CPU Hotplug is based on ACPI Generic Event Device framework and uses ACPI GED device for the same. Not all architectures support GPE based CPU Hotplug event handler. Hence, make AML for GPE.2 event handler conditional.

---
  hw/acpi/cpu.c         | 23 ++++++++++++++++-------
  hw/i386/acpi-build.c  |  3 ++-
  include/hw/acpi/cpu.h |  5 +++--
  3 files changed, 21 insertions(+), 10 deletions(-)
hw/acpi/cpu.c +3 -2
index af2b6655d2..4c63514b16 100644
@@ -343,9 +343,10 @@ const VMStateDescription vmstate_cpu_hotplug = {
  #define CPU_FW_EJECT_EVENT "CEJF"
 
  void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
-                    build_madt_cpu_fn build_madt_cpu, hwaddr io_base,
+                    build_madt_cpu_fn build_madt_cpu, hwaddr base_addr,
                      const char *res_root,
-                    const char *event_handler_method)
+                    const char *event_handler_method,
+                    AmlRegionSpace rs)
  {
      Aml *ifctx;
      Aml *field;

hw/acpi/cpu.c void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts, · line 377

Quoted 8 lines
hw/acpi/cpu.c +3 -1
@@ -370,13 +371,19 @@ void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
          aml_append(cpu_ctrl_dev, aml_mutex(CPU_LOCK, 0));
 
          crs = aml_resource_template();
-        aml_append(crs, aml_io(AML_DECODE16, io_base, io_base, 1,
+        if (rs == AML_SYSTEM_IO) {
+            aml_append(crs, aml_io(AML_DECODE16, base_addr, base_addr, 1,
                                 ACPI_CPU_HOTPLUG_REG_LEN));
+        } else {  

else
if (rs == yours type)

Quoted 4 lines

hw/acpi/cpu.c void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts, · line 380

Quoted 3 lines
hw/acpi/cpu.c +3 -0
+            aml_append(crs, aml_memory32_fixed(base_addr,
+                               ACPI_CPU_HOTPLUG_REG_LEN, AML_READ_WRITE));
+        }  

else assert on not supported input

Sure, no problem. I can incorporate the change.

Thanks, Salil.

Quoted 57 lines
Quoted 57 lines
hw/acpi/cpu.c +7 -4
+
          aml_append(cpu_ctrl_dev, aml_name_decl("_CRS", crs));
 
          /* declare CPU hotplug MMIO region with related access fields */
          aml_append(cpu_ctrl_dev,
-            aml_operation_region("PRST", AML_SYSTEM_IO, aml_int(io_base),
+            aml_operation_region("PRST", rs, aml_int(base_addr),
                                   ACPI_CPU_HOTPLUG_REG_LEN));
 
          field = aml_field("PRST", AML_BYTE_ACC, AML_NOLOCK,
@@ -700,9 +707,11 @@ void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
      aml_append(sb_scope, cpus_dev);
      aml_append(table, sb_scope);
 
-    method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
-    aml_append(method, aml_call0("\\_SB.CPUS." CPU_SCAN_METHOD));
-    aml_append(table, method);
+    if (event_handler_method) {
+        method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
+        aml_append(method, aml_call0("\\_SB.CPUS." CPU_SCAN_METHOD));
+        aml_append(table, method);
+    }
 
      g_free(cphp_res_path);
  }
hw/i386/acpi-build.c +2 -1
index 53f804ac16..b73b136605 100644
@@ -1537,7 +1537,8 @@ build_dsdt(GArray *table_data, BIOSLinker *linker,
              .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
          };
          build_cpus_aml(dsdt, machine, opts, pc_madt_cpu_entry,
-                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02");
+                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02",
+                       AML_SYSTEM_IO);
      }
 
      if (pcms->memhp_io_base && nr_mem) {
include/hw/acpi/cpu.h +3 -2
index e6e1a9ef59..48cded697c 100644
@@ -61,9 +61,10 @@ typedef void (*build_madt_cpu_fn)(int uid, const CPUArchIdList *apic_ids,
                                    GArray *entry, bool force_enabled);
 
  void build_cpus_aml(Aml *table, MachineState *machine, CPUHotplugFeatures opts,
-                    build_madt_cpu_fn build_madt_cpu, hwaddr io_base,
+                    build_madt_cpu_fn build_madt_cpu, hwaddr base_addr,
                      const char *res_root,
-                    const char *event_handler_method);
+                    const char *event_handler_method,
+                    AmlRegionSpace rs);
 
  void acpi_cpu_ospm_status(CPUHotplugState *cpu_st, ACPIOSTInfoList ***list);
 
#33 lore raw

PATCH v13 6/8 physmem: Add helper function to destroy CPU AddressSpace

In reply to Salil Mehta

Virtual CPU Hot-unplug leads to unrealization of a CPU object. This also involves destruction of the CPU AddressSpace. Add common function to help destroy the CPU AddressSpace.

---
 include/exec/cpu-common.h |  8 ++++++++
 include/hw/core/cpu.h     |  1 +
 system/physmem.c          | 29 +++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+)
include/exec/cpu-common.h +8 -0
index 815342d043..240ee04369 100644
@@ -129,6 +129,14 @@ size_t qemu_ram_pagesize_largest(void);
  */
 void cpu_address_space_init(CPUState *cpu, int asidx,
                             const char *prefix, MemoryRegion *mr);
+/**
+ * cpu_address_space_destroy:
+ * @cpu: CPU for which address space needs to be destroyed
+ * @asidx: integer index of this address space
+ *
+ * Note that with KVM only one address space is supported.
+ */
+void cpu_address_space_destroy(CPUState *cpu, int asidx);
 
 void cpu_physical_memory_rw(hwaddr addr, void *buf,
                             hwaddr len, bool is_write);
include/hw/core/cpu.h +1 -0
index bb398e8237..60b160d0b4 100644
@@ -486,6 +486,7 @@ struct CPUState {
     QSIMPLEQ_HEAD(, qemu_work_item) work_list;
 
     struct CPUAddressSpace *cpu_ases;
+    int cpu_ases_count;
     int num_ases;
     AddressSpace *as;
     MemoryRegion *memory;
system/physmem.c +29 -0
index 342b7a8fd4..146f17826a 100644
@@ -763,6 +763,7 @@ void cpu_address_space_init(CPUState *cpu, int asidx,
 
     if (!cpu->cpu_ases) {
         cpu->cpu_ases = g_new0(CPUAddressSpace, cpu->num_ases);
+        cpu->cpu_ases_count = cpu->num_ases;
     }
 
     newas = &cpu->cpu_ases[asidx];
@@ -776,6 +777,34 @@ void cpu_address_space_init(CPUState *cpu, int asidx,
     }
 }
 
+void cpu_address_space_destroy(CPUState *cpu, int asidx)
+{
+    CPUAddressSpace *cpuas;
+
+    assert(cpu->cpu_ases);
+    assert(asidx >= 0 && asidx < cpu->num_ases);
+    /* KVM cannot currently support multiple address spaces. */
+    assert(asidx == 0 || !kvm_enabled());
+
+    cpuas = &cpu->cpu_ases[asidx];
+    if (tcg_enabled()) {
+        memory_listener_unregister(&cpuas->tcg_as_listener);
+    }
+
+    address_space_destroy(cpuas->as);
+    g_free_rcu(cpuas->as, rcu);
+
+    if (asidx == 0) {
+        /* reset the convenience alias for address space 0 */
+        cpu->as = NULL;
+    }
+
+    if (--cpu->cpu_ases_count == 0) {
+        g_free(cpu->cpu_ases);
+        cpu->cpu_ases = NULL;
+    }
+}
+
 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx)
 {
     /* Return the AddressSpace corresponding to the specified index */
Signature · 1 line
2.34.1
Acked-by on v13 6/8
#34 lore raw

Re: [PATCH V13 6/8] physmem: Add helper function to destroy CPU AddressSpace

Quoted 11 lines · On Fri, 7 Jun 2024 12:56:47 +0100 Salil Mehta <[email protected]> wrote:

Virtual CPU Hot-unplug leads to unrealization of a CPU object. This also involves destruction of the CPU AddressSpace. Add common function to help destroy the CPU AddressSpace.

Quoted 84 lines
---
 include/exec/cpu-common.h |  8 ++++++++
 include/hw/core/cpu.h     |  1 +
 system/physmem.c          | 29 +++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+)
include/exec/cpu-common.h +8 -0
index 815342d043..240ee04369 100644
@@ -129,6 +129,14 @@ size_t qemu_ram_pagesize_largest(void);
  */
 void cpu_address_space_init(CPUState *cpu, int asidx,
                             const char *prefix, MemoryRegion *mr);
+/**
+ * cpu_address_space_destroy:
+ * @cpu: CPU for which address space needs to be destroyed
+ * @asidx: integer index of this address space
+ *
+ * Note that with KVM only one address space is supported.
+ */
+void cpu_address_space_destroy(CPUState *cpu, int asidx);
 
 void cpu_physical_memory_rw(hwaddr addr, void *buf,
                             hwaddr len, bool is_write);
include/hw/core/cpu.h +1 -0
index bb398e8237..60b160d0b4 100644
@@ -486,6 +486,7 @@ struct CPUState {
     QSIMPLEQ_HEAD(, qemu_work_item) work_list;
 
     struct CPUAddressSpace *cpu_ases;
+    int cpu_ases_count;
     int num_ases;
     AddressSpace *as;
     MemoryRegion *memory;
system/physmem.c +29 -0
index 342b7a8fd4..146f17826a 100644
@@ -763,6 +763,7 @@ void cpu_address_space_init(CPUState *cpu, int asidx,
 
     if (!cpu->cpu_ases) {
         cpu->cpu_ases = g_new0(CPUAddressSpace, cpu->num_ases);
+        cpu->cpu_ases_count = cpu->num_ases;
     }
 
     newas = &cpu->cpu_ases[asidx];
@@ -776,6 +777,34 @@ void cpu_address_space_init(CPUState *cpu, int asidx,
     }
 }
 
+void cpu_address_space_destroy(CPUState *cpu, int asidx)
+{
+    CPUAddressSpace *cpuas;
+
+    assert(cpu->cpu_ases);
+    assert(asidx >= 0 && asidx < cpu->num_ases);
+    /* KVM cannot currently support multiple address spaces. */
+    assert(asidx == 0 || !kvm_enabled());
+
+    cpuas = &cpu->cpu_ases[asidx];
+    if (tcg_enabled()) {
+        memory_listener_unregister(&cpuas->tcg_as_listener);
+    }
+
+    address_space_destroy(cpuas->as);
+    g_free_rcu(cpuas->as, rcu);
+
+    if (asidx == 0) {
+        /* reset the convenience alias for address space 0 */
+        cpu->as = NULL;
+    }
+
+    if (--cpu->cpu_ases_count == 0) {
+        g_free(cpu->cpu_ases);
+        cpu->cpu_ases = NULL;
+    }
+}
+
 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx)
 {
     /* Return the AddressSpace corresponding to the specified index */
#35 lore raw

Re: [PATCH V13 6/8] physmem: Add helper function to destroy CPU AddressSpace

Quoted 15 lines · Igor Mammedov
Quoted 11 lines · On Fri, 7 Jun 2024 12:56:47 +0100 Salil Mehta <[email protected]> wrote:

Virtual CPU Hot-unplug leads to unrealization of a CPU object. This also involves destruction of the CPU AddressSpace. Add common function to help destroy the CPU AddressSpace.

Quoted 1 line

Thanks, Salil.

Quoted 85 lines
Quoted 85 lines
---
  include/exec/cpu-common.h |  8 ++++++++
  include/hw/core/cpu.h     |  1 +
  system/physmem.c          | 29 +++++++++++++++++++++++++++++
  3 files changed, 38 insertions(+)
include/exec/cpu-common.h +8 -0
index 815342d043..240ee04369 100644
@@ -129,6 +129,14 @@ size_t qemu_ram_pagesize_largest(void);
   */
  void cpu_address_space_init(CPUState *cpu, int asidx,
                              const char *prefix, MemoryRegion *mr);
+/**
+ * cpu_address_space_destroy:
+ * @cpu: CPU for which address space needs to be destroyed
+ * @asidx: integer index of this address space
+ *
+ * Note that with KVM only one address space is supported.
+ */
+void cpu_address_space_destroy(CPUState *cpu, int asidx);
 
  void cpu_physical_memory_rw(hwaddr addr, void *buf,
                              hwaddr len, bool is_write);
include/hw/core/cpu.h +1 -0
index bb398e8237..60b160d0b4 100644
@@ -486,6 +486,7 @@ struct CPUState {
      QSIMPLEQ_HEAD(, qemu_work_item) work_list;
 
      struct CPUAddressSpace *cpu_ases;
+    int cpu_ases_count;
      int num_ases;
      AddressSpace *as;
      MemoryRegion *memory;
system/physmem.c +29 -0
index 342b7a8fd4..146f17826a 100644
@@ -763,6 +763,7 @@ void cpu_address_space_init(CPUState *cpu, int asidx,
 
      if (!cpu->cpu_ases) {
          cpu->cpu_ases = g_new0(CPUAddressSpace, cpu->num_ases);
+        cpu->cpu_ases_count = cpu->num_ases;
      }
 
      newas = &cpu->cpu_ases[asidx];
@@ -776,6 +777,34 @@ void cpu_address_space_init(CPUState *cpu, int asidx,
      }
  }
 
+void cpu_address_space_destroy(CPUState *cpu, int asidx)
+{
+    CPUAddressSpace *cpuas;
+
+    assert(cpu->cpu_ases);
+    assert(asidx >= 0 && asidx < cpu->num_ases);
+    /* KVM cannot currently support multiple address spaces. */
+    assert(asidx == 0 || !kvm_enabled());
+
+    cpuas = &cpu->cpu_ases[asidx];
+    if (tcg_enabled()) {
+        memory_listener_unregister(&cpuas->tcg_as_listener);
+    }
+
+    address_space_destroy(cpuas->as);
+    g_free_rcu(cpuas->as, rcu);
+
+    if (asidx == 0) {
+        /* reset the convenience alias for address space 0 */
+        cpu->as = NULL;
+    }
+
+    if (--cpu->cpu_ases_count == 0) {
+        g_free(cpu->cpu_ases);
+        cpu->cpu_ases = NULL;
+    }
+}
+
  AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx)
  {
      /* Return the AddressSpace corresponding to the specified index */
#36 lore raw

PATCH v13 7/8 gdbstub: Add helper function to unregister GDB register space

In reply to Salil Mehta

Add common function to help unregister the GDB register space. This shall be done in context to the CPU unrealization.

Note: These are common functions exported to arch specific code. For example, for ARM this code is being referred in associated arch specific patch-set:

---
 gdbstub/gdbstub.c      | 13 +++++++++++++
 hw/core/cpu-common.c   |  1 -
 include/exec/gdbstub.h |  6 ++++++
 3 files changed, 19 insertions(+), 1 deletion(-)
gdbstub/gdbstub.c +13 -0
index b3574997ea..1949b09240 100644
@@ -617,6 +617,19 @@ void gdb_register_coprocessor(CPUState *cpu,
     }
 }
 
+void gdb_unregister_coprocessor_all(CPUState *cpu)
+{
+    /*
+     * Safe to nuke everything. GDBRegisterState::xml is static const char so
+     * it won't be freed
+     */
+    g_array_free(cpu->gdb_regs, true);
+
+    cpu->gdb_regs = NULL;
+    cpu->gdb_num_regs = 0;
+    cpu->gdb_num_g_regs = 0;
+}
+
 static void gdb_process_breakpoint_remove_all(GDBProcess *p)
 {
     CPUState *cpu = gdb_get_first_cpu_in_process(p);
hw/core/cpu-common.c +0 -1
index 0f0a247f56..e5140b4bc1 100644
@@ -274,7 +274,6 @@ static void cpu_common_finalize(Object *obj)
 {
     CPUState *cpu = CPU(obj);
 
-    g_array_free(cpu->gdb_regs, TRUE);
     qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
     qemu_mutex_destroy(&cpu->work_mutex);
 }
include/exec/gdbstub.h +6 -0
index eb14b91139..249d4d4bc8 100644
@@ -49,6 +49,12 @@ void gdb_register_coprocessor(CPUState *cpu,
                               gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
                               const GDBFeature *feature, int g_pos);
 
+/**
+ * gdb_unregister_coprocessor_all() - unregisters supplemental set of registers
+ * @cpu - the CPU associated with registers
+ */
+void gdb_unregister_coprocessor_all(CPUState *cpu);
+
 /**
  * gdbserver_start: start the gdb server
  * @port_or_device: connection spec for gdb
Signature · 1 line
2.34.1
#37 lore raw

Re: [PATCH V13 7/8] gdbstub: Add helper function to unregister GDB register space

Quoted 51 lines · On Fri, 7 Jun 2024 12:56:48 +0100 Salil Mehta <[email protected]> wrote:

Add common function to help unregister the GDB register space. This shall be done in context to the CPU unrealization.

Note: These are common functions exported to arch specific code. For example, for ARM this code is being referred in associated arch specific patch-set:

---
 gdbstub/gdbstub.c      | 13 +++++++++++++
 hw/core/cpu-common.c   |  1 -
 include/exec/gdbstub.h |  6 ++++++
 3 files changed, 19 insertions(+), 1 deletion(-)
gdbstub/gdbstub.c +13 -0
index b3574997ea..1949b09240 100644
@@ -617,6 +617,19 @@ void gdb_register_coprocessor(CPUState *cpu,
     }
 }
 
+void gdb_unregister_coprocessor_all(CPUState *cpu)
+{
+    /*
+     * Safe to nuke everything. GDBRegisterState::xml is static const char so
+     * it won't be freed
+     */
+    g_array_free(cpu->gdb_regs, true);
+
+    cpu->gdb_regs = NULL;
+    cpu->gdb_num_regs = 0;
+    cpu->gdb_num_g_regs = 0;
+}
+
 static void gdb_process_breakpoint_remove_all(GDBProcess *p)
 {
     CPUState *cpu = gdb_get_first_cpu_in_process(p);
hw/core/cpu-common.c +0 -0
index 0f0a247f56..e5140b4bc1 100644

hw/core/cpu-common.c static void cpu_common_finalize(Object *obj) · line 277

Quoted 4 lines
hw/core/cpu-common.c +0 -1
@@ -274,7 +274,6 @@ static void cpu_common_finalize(Object *obj)
 {
     CPUState *cpu = CPU(obj);
 
-    g_array_free(cpu->gdb_regs, TRUE);

so free() is gone but new gdb_unregister_coprocessor_all() ain't called, are we staring to leak some memory here?

Quoted 20 lines
hw/core/cpu-common.c +0 -0
     qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
     qemu_mutex_destroy(&cpu->work_mutex);
 }
include/exec/gdbstub.h +6 -0
index eb14b91139..249d4d4bc8 100644
@@ -49,6 +49,12 @@ void gdb_register_coprocessor(CPUState *cpu,
                               gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
                               const GDBFeature *feature, int g_pos);
 
+/**
+ * gdb_unregister_coprocessor_all() - unregisters supplemental set of registers
+ * @cpu - the CPU associated with registers
+ */
+void gdb_unregister_coprocessor_all(CPUState *cpu);
+
 /**
  * gdbserver_start: start the gdb server
  * @port_or_device: connection spec for gdb
#38 lore raw

Re: [PATCH V13 7/8] gdbstub: Add helper function to unregister GDB register space

Hi Igor,

Quoted 57 lines · Igor Mammedov
Quoted 54 lines · On Fri, 7 Jun 2024 12:56:48 +0100 Salil Mehta <[email protected]> wrote:

Add common function to help unregister the GDB register space. This shall be done in context to the CPU unrealization.

Note: These are common functions exported to arch specific code. For example, for ARM this code is being referred in associated arch specific patch-set:

---
  gdbstub/gdbstub.c      | 13 +++++++++++++
  hw/core/cpu-common.c   |  1 -
  include/exec/gdbstub.h |  6 ++++++
  3 files changed, 19 insertions(+), 1 deletion(-)
gdbstub/gdbstub.c +13 -0
index b3574997ea..1949b09240 100644
@@ -617,6 +617,19 @@ void gdb_register_coprocessor(CPUState *cpu,
      }
  }
 
+void gdb_unregister_coprocessor_all(CPUState *cpu)
+{
+    /*
+     * Safe to nuke everything. GDBRegisterState::xml is static const char so
+     * it won't be freed
+     */
+    g_array_free(cpu->gdb_regs, true);
+
+    cpu->gdb_regs = NULL;
+    cpu->gdb_num_regs = 0;
+    cpu->gdb_num_g_regs = 0;
+}
+
  static void gdb_process_breakpoint_remove_all(GDBProcess *p)
  {
      CPUState *cpu = gdb_get_first_cpu_in_process(p);
hw/core/cpu-common.c +0 -0
index 0f0a247f56..e5140b4bc1 100644
@@ -274,7 +274,6 @@ static void cpu_common_finalize(Object *obj)
  {
      CPUState *cpu = CPU(obj);
 
Quoted 3 lines

hw/core/cpu-common.c static void cpu_common_finalize(Object *obj) · line 277

Quoted 1 line
hw/core/cpu-common.c +0 -1
-    g_array_free(cpu->gdb_regs, TRUE);

so free() is gone but new gdb_unregister_coprocessor_all() ain't called, are we staring to leak some memory here?

Ah, sorry yes for other architectures. Maybe I can put below check

and keep the free'ing code?

+     if (cpu->gdb_regs)

+ g_array_free(cpu->gdb_regs, TRUE);

Thanks, Salil.

Quoted 20 lines
Quoted 20 lines
hw/core/cpu-common.c +0 -0
      qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
      qemu_mutex_destroy(&cpu->work_mutex);
  }
include/exec/gdbstub.h +6 -0
index eb14b91139..249d4d4bc8 100644
@@ -49,6 +49,12 @@ void gdb_register_coprocessor(CPUState *cpu,
                                gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
                                const GDBFeature *feature, int g_pos);
 
+/**
+ * gdb_unregister_coprocessor_all() - unregisters supplemental set of registers
+ * @cpu - the CPU associated with registers
+ */
+void gdb_unregister_coprocessor_all(CPUState *cpu);
+
  /**
   * gdbserver_start: start the gdb server
   * @port_or_device: connection spec for gdb
#39 lore raw

PATCH v13 8/8 docs/specs/acpi_hw_reduced_hotplug: Add the CPU Hotplug Event Bit

In reply to Salil Mehta

GED interface is used by many hotplug events like memory hotplug, NVDIMM hotplug and non-hotplug events like system power down event. Each of these can be selected using a bit in the 32 bit GED IO interface. A bit has been reserved for the CPU hotplug event.

---
 docs/specs/acpi_hw_reduced_hotplug.rst | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
docs/specs/acpi_hw_reduced_hotplug.rst +2 -1
index 0bd3f9399f..3acd6fcd8b 100644
@@ -64,7 +64,8 @@ GED IO interface (4 byte access)
        0: Memory hotplug event
        1: System power down event
        2: NVDIMM hotplug event
-    3-31: Reserved
+       3: CPU hotplug event
+    4-31: Reserved
 
 **write_access:**
 
Signature · 1 line
2.34.1
#40 lore raw

Re: [PATCH V13 8/8] docs/specs/acpi_hw_reduced_hotplug: Add the CPU Hotplug Event Bit

Quoted 8 lines · On Fri, 7 Jun 2024 12:56:49 +0100 Salil Mehta <[email protected]> wrote:

GED interface is used by many hotplug events like memory hotplug, NVDIMM hotplug and non-hotplug events like system power down event. Each of these can be selected using a bit in the 32 bit GED IO interface. A bit has been reserved for the CPU hotplug event.

suggest to squash this into the patch that introduces this bit [3/8]

Quoted 9 lines
---
 docs/specs/acpi_hw_reduced_hotplug.rst | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
docs/specs/acpi_hw_reduced_hotplug.rst +0 -0
index 0bd3f9399f..3acd6fcd8b 100644

docs/specs/acpi_hw_reduced_hotplug.rst GED IO interface (4 byte access) · line 71

Quoted 9 lines
docs/specs/acpi_hw_reduced_hotplug.rst +2 -1
@@ -64,7 +64,8 @@ GED IO interface (4 byte access)
        0: Memory hotplug event
        1: System power down event
        2: NVDIMM hotplug event
-    3-31: Reserved
+       3: CPU hotplug event
+    4-31: Reserved
 
 **write_access:**
 

:

#41 lore raw

Re: [PATCH V13 8/8] docs/specs/acpi_hw_reduced_hotplug: Add the CPU Hotplug Event Bit

Quoted 8 lines · Igor Mammedov
Quoted 5 lines · On Fri, 7 Jun 2024 12:56:49 +0100 Salil Mehta <[email protected]> wrote:

GED interface is used by many hotplug events like memory hotplug, NVDIMM hotplug and non-hotplug events like system power down event. Each of these can be selected using a bit in the 32 bit GED IO interface. A bit has been reserved for the CPU hotplug event.

Quoted 4 lines
Quoted 3 lines

suggest to squash this into the patch that introduces this bit [3/8]

I thought, we are introducing a change common to all architectures?

Best, Salil.

Quoted 20 lines
Quoted 9 lines
---
  docs/specs/acpi_hw_reduced_hotplug.rst | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
docs/specs/acpi_hw_reduced_hotplug.rst +0 -0
index 0bd3f9399f..3acd6fcd8b 100644

docs/specs/acpi_hw_reduced_hotplug.rst GED IO interface (4 byte access) · line 71

Quoted 9 lines
docs/specs/acpi_hw_reduced_hotplug.rst +2 -1
@@ -64,7 +64,8 @@ GED IO interface (4 byte access)
         0: Memory hotplug event
         1: System power down event
         2: NVDIMM hotplug event
-    3-31: Reserved
+       3: CPU hotplug event
+    4-31: Reserved
 
  **write_access:**
 

:

#42 lore raw

Re: [PATCH V13 8/8] docs/specs/acpi_hw_reduced_hotplug: Add the CPU Hotplug Event Bit

Quoted 9 lines · On Mon, 8 Jul 2024 05:32:28 +0000 Salil Mehta <[email protected]> wrote:
Quoted 8 lines · Igor Mammedov
Quoted 5 lines · On Fri, 7 Jun 2024 12:56:49 +0100 Salil Mehta <[email protected]> wrote:

GED interface is used by many hotplug events like memory hotplug, NVDIMM hotplug and non-hotplug events like system power down event. Each of these can be selected using a bit in the 32 bit GED IO interface. A bit has been reserved for the CPU hotplug event.

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

suggest to squash this into the patch that introduces this bit [3/8]

I thought, we are introducing a change common to all architectures?

hw reduced hotplug implies GED, so including doc change into the patch that introduces bit in the code is better option.

it also easier on the folks that come later and find doc and code in the same commit (which is easier to follow than looking for different commits in git log).

Quoted 23 lines
Quoted 22 lines

Best, Salil.

Quoted 9 lines
---
  docs/specs/acpi_hw_reduced_hotplug.rst | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
docs/specs/acpi_hw_reduced_hotplug.rst +0 -0
index 0bd3f9399f..3acd6fcd8b 100644

docs/specs/acpi_hw_reduced_hotplug.rst GED IO interface (4 byte access) · line 71

Quoted 9 lines
docs/specs/acpi_hw_reduced_hotplug.rst +2 -1
@@ -64,7 +64,8 @@ GED IO interface (4 byte access)
         0: Memory hotplug event
         1: System power down event
         2: NVDIMM hotplug event
-    3-31: Reserved
+       3: CPU hotplug event
+    4-31: Reserved
 
  **write_access:**
 

:

#43 lore raw

Re: [PATCH V13 8/8] docs/specs/acpi_hw_reduced_hotplug: Add the CPU Hotplug Event Bit

Quoted 16 lines · Igor Mammedov
Quoted 13 lines · On Mon, 8 Jul 2024 05:32:28 +0000 Salil Mehta <[email protected]> wrote:
Quoted 12 lines · Igor Mammedov
Quoted 8 lines · On Fri, 7 Jun 2024 12:56:49 +0100 Salil Mehta <[email protected]> wrote:

GED interface is used by many hotplug events like memory hotplug, NVDIMM hotplug and non-hotplug events like system power down event. Each of these can be selected using a bit in the 32 bit GED IO interface. A bit has been reserved for the CPU hotplug event.

suggest to squash this into the patch that introduces this bit [3/8]

Quoted 8 lines
Quoted 1 line

I thought, we are introducing a change common to all architectures?

hw reduced hotplug implies GED, so including doc change into the patch that introduces bit in the code is better option.

it also easier on the folks that come later and find doc and code in the same commit (which is easier to follow than looking for different commits in git log).

Ok. I'll squash this into the 3rd patch of this series.

Thanks

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines

Best, Salil.

Quoted 9 lines
---
   docs/specs/acpi_hw_reduced_hotplug.rst | 3 ++-
   1 file changed, 2 insertions(+), 1 deletion(-)
docs/specs/acpi_hw_reduced_hotplug.rst +0 -0
index 0bd3f9399f..3acd6fcd8b 100644

docs/specs/acpi_hw_reduced_hotplug.rst GED IO interface (4 byte access) · line 71

Quoted 9 lines
docs/specs/acpi_hw_reduced_hotplug.rst +2 -1
@@ -64,7 +64,8 @@ GED IO interface (4 byte access)
          0: Memory hotplug event
          1: System power down event
          2: NVDIMM hotplug event
-    3-31: Reserved
+       3: CPU hotplug event
+    4-31: Reserved
 
   **write_access:**
 

:

#44 lore raw
In reply to Salil Mehta
Quoted 8 lines · Salil Mehta

Virtual CPU hotplug support is being added across various architectures[1][3]. This series adds various code bits common across all architectures:

1. vCPU creation and Parking code refactor [Patch 1]
2. Update ACPI GED framework to support vCPU Hotplug [Patch 2,3]
3. ACPI CPUs AML code change [Patch 4,5]
4. Helper functions to support unrealization of CPU objects [Patch 6,7]
5. Docs [Patch 8]

Igor - it's been a while. Looks ok to me superficially. Do you plan to review?

Quoted 161 lines

Repository:

[*] https://github.com/salil-mehta/qemu.git virt-cpuhp-armv8/rfc-v3.arch.agnostic.v13

NOTE: This series is meant to work in conjunction with Architecture specific patch-set. For ARM, a combined patch-set (arch agnostic + specific) was earlier pushed as RFC V2[1]. A ARM architecture specific patch-set (subset of RFC V2) is being finalized and shall be floated soon as RFC-V3. Please find a version of it compatible with this patch-set at the below location. (TCG support is broken in below and is being fixed)

[*] https://github.com/salil-mehta/qemu/tree/virt-cpuhp-armv8/rfc-v3-rc2

Revision History:

Patch-set V12 -> V13
1. Added Reviewed-by Tag of Harsh Prateek Bora's (IBM) [PATCH V12 1/8]
2. Moved the kvm_{create,park,unpark}_vcpu prototypes from accel/kvm/kvm-cpus.h to include/sysemu/kvm.h. These can later be exported through AccelOps.

Patch-set V11 -> V12
1. Addressed Harsh Prateek Bora's (IBM) comment
- Changed @cpu to @vcpu_id in the kvm_unpark_vcpu protoype header/
2. Added Zhao Liu's (Intel) Tested-by for whole series
- Qtest does not breaks on Intel platforms now.
3. Added Zhao Liu's (Intel) Reviewed-by for [PATCH V11 {1/8 - 3/8}]

Patch-set V10 -> V11
1. Addressed Nicholas Piggin's (IBM) comment
- moved the traces in kvm_unpark_vcpu and kvm_create_vcpu at the end
- Added the Reviewed-by Tag for [PATCH V10 1/8]
2. Addressed Alex Bennée's (Linaro) comments
- Added a note explaining dependency of the [PATCH V10 7/8] on Arch specific patch-set

Patch-set V9 -> V10
1. Addressed Nicholas Piggin's (IBM) & Philippe Mathieu-Daudé (Linaro) comments
- carved out kvm_unpark_vcpu and added its trace
- Widened the scope of the kvm_unpark_vcpu so that it can be used by generic framework being thought out

Patch-set V8 -> V9
1. Addressed Vishnu Pajjuri's (Ampere) comments
- Added kvm_fd to trace in kvm_create_vcpu
- Some clean ups: arch vcpu-id and sbd variable
- Added the missed initialization of cpu->gdb_num_regs
2. Addressed the commnet from Zhao Liu (Intel)
- Make initialization of CPU Hotplug state conditional (possible_cpu_arch_ids!=NULL)

Patch-set V7 -> V8
1. Rebased and Fixed the conflicts

Patch-set V6 -> V7
1. Addressed Alex Bennée's comments
- Updated the docs
2. Addressed Igor Mammedov's comments
- Merged patches [Patch V6 3/9] & [Patch V6 7/9] with [Patch V6 4/9]
- Updated commit-log of [Patch V6 1/9] and [Patch V6 5/9]
3. Added Shaoqin Huang's Reviewed-by tags for whole series.

Patch-set V5 -> V6
1. Addressed Gavin Shan's comments
- Fixed the assert() ranges of address spaces
- Rebased the patch-set to latest changes in the qemu.git
- Added Reviewed-by tags for patches {8,9}
2. Addressed Jonathan Cameron's comments
- Updated commit-log for [Patch V5 1/9] with mention of trace events
- Added Reviewed-by tags for patches {1,5}
3. Added Tested-by tags from Xianglai Li
4. Fixed checkpatch.pl error "Qemu -> QEMU" in [Patch V5 1/9]

Patch-set V4 -> V5
1. Addressed Gavin Shan's comments
- Fixed the trace events print string for kvm_{create,get,park,destroy}_vcpu
- Added Reviewed-by tag for patch {1}
2. Added Shaoqin Huang's Reviewed-by tags for Patches {2,3}
3. Added Tested-by Tag from Vishnu Pajjuri to the patch-set
4. Dropped the ARM specific [Patch V4 10/10]

Patch-set V3 -> V4
1. Addressed David Hilderbrand's comments
- Fixed the wrong doc comment of kvm_park_vcpu API prototype
- Added Reviewed-by tags for patches {2,4}

Patch-set V2 -> V3
1. Addressed Jonathan Cameron's comments
- Fixed 'vcpu-id' type wrongly changed from 'unsigned long' to 'integer'
- Removed unnecessary use of variable 'vcpu_id' in kvm_park_vcpu
- Updated [Patch V2 3/10] commit-log with details of ACPI_CPU_SCAN_METHOD macro
- Updated [Patch V2 5/10] commit-log with details of conditional event handler method
- Added Reviewed-by tags for patches {2,3,4,6,7}
2. Addressed Gavin Shan's comments
- Remove unnecessary use of variable 'vcpu_id' in kvm_par_vcpu
- Fixed return value in kvm_get_vcpu from -1 to -ENOENT
- Reset the value of 'gdb_num_g_regs' in gdb_unregister_coprocessor_all
- Fixed the kvm_{create,park}_vcpu prototypes docs
- Added Reviewed-by tags for patches {2,3,4,5,6,7,9,10}
3. Addressed one earlier missed comment by Alex Bennée in RFC V1
- Added traces instead of DPRINTF in the newly added and some existing functions

Patch-set V1 -> V2
1. Addressed Alex Bennée's comments
- Refactored the kvm_create_vcpu logic to get rid of goto
- Added the docs for kvm_{create,park}_vcpu prototypes
- Splitted the gdbstub and AddressSpace destruction change into separate patches
- Added Reviewed-by tags for patches {2,10}

References:

[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/qemu-devel/[email protected]/

Salil Mehta (8):
accel/kvm: Extract common KVM vCPU {creation,parking} code hw/acpi: Move CPU ctrl-dev MMIO region len macro to common header file hw/acpi: Update ACPI GED framework to support vCPU Hotplug hw/acpi: Update GED _EVT method AML with CPU scan
hw/acpi: Update CPUs AML with cpu-(ctrl)dev change
physmem: Add helper function to destroy CPU AddressSpace
gdbstub: Add helper function to unregister GDB register space docs/specs/acpi_hw_reduced_hotplug: Add the CPU Hotplug Event Bit

 accel/kvm/kvm-all.c                    | 95 +++++++++++++++++---------
 accel/kvm/kvm-cpus.h                   |  1 -
 accel/kvm/trace-events                 |  5 +-
 docs/specs/acpi_hw_reduced_hotplug.rst |  3 +-
 gdbstub/gdbstub.c                      | 13 ++++
 hw/acpi/acpi-cpu-hotplug-stub.c        |  6 ++
 hw/acpi/cpu.c                          | 33 ++++++---
 hw/acpi/generic_event_device.c         | 21 ++++++
 hw/core/cpu-common.c                   |  1 -
 hw/i386/acpi-build.c                   |  3 +-
 include/exec/cpu-common.h              |  8 +++
 include/exec/gdbstub.h                 |  6 ++
 include/hw/acpi/cpu.h                  |  5 +-
 include/hw/acpi/cpu_hotplug.h          |  4 ++
 include/hw/acpi/generic_event_device.h |  4 ++
 include/hw/core/cpu.h                  |  1 +
 include/sysemu/kvm.h                   | 25 +++++++
 system/physmem.c                       | 29 ++++++++
 18 files changed, 214 insertions(+), 49 deletions(-)
Signature · 1 line
2.34.1