accel: Add per-accelerator vCPUs queue
cover accel: Add per-accelerator vCPUs queue
Hi,
Currently we register all vCPUs to the global 'cpus_queue' queue, however we can not discriminate per accelerator or per target architecture (which might happen in a soon future).
This series tries to add an accelerator discriminator, so accelerator specific code can iterate on its own vCPUs. This is required to run a pair of HW + SW accelerators like the (HVF, TCG) or (KVM, TCG) combinations. Otherwise, i.e. the HVF core code could iterate on TCG vCPUs...
To keep it simple and not refactor heavily the code base, we introduce the CPU_FOREACH_TCG/HVF/KVM() macros, only defined for each accelerator.
This is just a RFC to get some thoughts whether this is heading in the correct direction or not ;)
Regards,
Phil.
Philippe Mathieu-Daudé (7):
cpus: Restrict CPU_FOREACH_SAFE() to user emulation
cpus: Introduce AccelOpsClass::get_cpus_queue() accel/tcg: Implement tcg_get_cpus_queue() accel/tcg: Use CPU_FOREACH_TCG()
accel/hw: Implement hw_accel_get_cpus_queue() accel/hvf: Use CPU_FOREACH_HVF()
accel/kvm: Use CPU_FOREACH_KVM()
accel/tcg/tcg-accel-ops.h | 10 ++++++++++ include/hw/core/cpu.h | 11 +++++++++++ include/system/accel-ops.h | 6 ++++++ include/system/hvf_int.h | 4 ++++ include/system/hw_accel.h | 9 +++++++++ include/system/kvm_int.h | 3 +++ accel/accel-system.c | 8 ++++++++ accel/hvf/hvf-accel-ops.c | 9 +++++---- accel/kvm/kvm-accel-ops.c | 1 + accel/kvm/kvm-all.c | 14 +++++++------- accel/tcg/cputlb.c | 7 ++++--- accel/tcg/monitor.c | 3 ++- accel/tcg/tb-maint.c | 7 ++++--- accel/tcg/tcg-accel-ops-rr.c | 10 +++++----- accel/tcg/tcg-accel-ops.c | 16 ++++++++++++---- accel/tcg/user-exec-stub.c | 5 +++++ accel/xen/xen-all.c | 1 + cpu-common.c | 10 ++++++++++ hw/i386/kvm/clock.c | 3 ++- hw/intc/spapr_xive_kvm.c | 5 +++-- hw/intc/xics_kvm.c | 5 +++-- system/cpus.c | 5 +++++ target/arm/hvf/hvf.c | 4 ++-- target/i386/kvm/kvm.c | 4 ++-- target/i386/kvm/xen-emu.c | 2 +- target/i386/nvmm/nvmm-accel-ops.c | 1 + target/i386/whpx/whpx-accel-ops.c | 1 + target/s390x/kvm/kvm.c | 2 +- target/s390x/kvm/stsi-topology.c | 3 ++- 29 files changed, 130 insertions(+), 39 deletions(-)
Signature · 1 line
2.47.1
RFC PATCH 1/7 cpus: Restrict CPU_FOREACH_SAFE() to user emulation
--- include/hw/core/cpu.h | 3 +++ 1 file changed, 3 insertions(+)
include/hw/core/cpu.h +3 -0
@@ -594,8 +594,11 @@ extern CPUTailQ cpus_queue;#define first_cpu QTAILQ_FIRST_RCU(&cpus_queue)#define CPU_NEXT (cpu) QTAILQ_NEXT_RCU(cpu, node)#define CPU_FOREACH (cpu) QTAILQ_FOREACH_RCU(cpu, &cpus_queue, node) + +#if defined(CONFIG_USER_ONLY )#define CPU_FOREACH_SAFE (cpu, next_cpu) \ QTAILQ_FOREACH_SAFE_RCU(cpu, &cpus_queue, node, next_cpu) +#endif extern __threadCPUState *current_cpu;
Signature · 1 line
2.47.1
Re: [RFC PATCH 1/7] cpus: Restrict CPU_FOREACH_SAFE() to user emulation
Perhaps add in the commit msg something like "it's only being used in bsd-user and linux-user code"
Quoted 2 lines · Philippe Mathieu-Daudé
Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
Quoted 19 lines
include/hw/core/cpu.h | 3 +++ 1 file changed, 3 insertions(+)include/hw/core/cpu.h +3
-0@@ -594,8 +594,11 @@ extern CPUTailQ cpus_queue;#define first_cpu QTAILQ_FIRST_RCU(&cpus_queue)#define CPU_NEXT (cpu) QTAILQ_NEXT_RCU(cpu, node)#define CPU_FOREACH (cpu) QTAILQ_FOREACH_RCU(cpu, &cpus_queue, node) + +#if defined(CONFIG_USER_ONLY )#define CPU_FOREACH_SAFE (cpu, next_cpu) \ QTAILQ_FOREACH_SAFE_RCU(cpu, &cpus_queue, node, next_cpu) +#endif extern __threadCPUState *current_cpu;
RFC PATCH 2/7 cpus: Introduce AccelOpsClass::get_cpus_queue()
In reply to Philippe Mathieu-DaudéWe want the ability to iterate over vCPUs of a specific accelerator. Introduce cpus_get_accel_cpus_queue() to be used by accelerator common code, and expose the get_cpus_queue() in AccelOpsClass, so each accelerator can register its own queue of vCPUs.
--- include/hw/core/cpu.h | 8 ++++++++ include/system/accel-ops.h | 6 ++++++ accel/tcg/user-exec-stub.c | 5 +++++ cpu-common.c | 10 ++++++++++ system/cpus.c | 5 +++++ 5 files changed, 34 insertions(+)
include/hw/core/cpu.h +8 -0
@@ -591,6 +591,14 @@ static inline CPUArchState *cpu_env(CPUState *cpu) typedef QTAILQ_HEAD(CPUTailQ, CPUState) CPUTailQ;extern CPUTailQ cpus_queue; +/** +* cpus_get_accel_cpus_queue: +* @cpu: The vCPU to get the accelerator #CPUTailQ. +* +* Returns the #CPUTailQ associated with the accelerator of the vCPU. +*/ +CPUTailQ *cpus_get_accel_cpus_queue (CPUState *cpu); +#define first_cpu QTAILQ_FIRST_RCU(&cpus_queue)#define CPU_NEXT (cpu) QTAILQ_NEXT_RCU(cpu, node)#define CPU_FOREACH (cpu) QTAILQ_FOREACH_RCU(cpu, &cpus_queue, node)
include/system/accel-ops.h +6 -0
@@ -12,6 +12,7 @@#include "exec/vaddr.h" #include "qom/object.h" +#include "hw/core/cpu.h" #define ACCEL_OPS_SUFFIX "-ops"#define TYPE_ACCEL_OPS "accel" ACCEL_OPS_SUFFIX
@@ -37,6 +38,11 @@ struct AccelOpsClass {bool (*cpus_are_resettable)(void );void (*cpu_reset_hold)(CPUState *cpu); +/** +* get_cpus_queue: +* Returns the #CPUTailQ maintained by this accelerator. +*/ + CPUTailQ *(*get_cpus_queue)(void);void (*create_vcpu_thread)(CPUState *cpu);/* MANDATORY NON-NULL */ void (*kick_vcpu_thread)(CPUState *cpu);bool (*cpu_thread_is_idle)(CPUState *cpu);
accel/tcg/user-exec-stub.c +5 -0
@@ -18,6 +18,11 @@ void cpu_exec_reset_hold(CPUState *cpu) { } +CPUTailQ *cpus_get_accel_cpus_queue (CPUState *cpu) +{ +return NULL ; +} +/* User mode emulation does not support record/replay yet. */ bool replay_exception (void )
cpu-common.c +10 -0
@@ -82,6 +82,7 @@ unsigned int cpu_list_generation_id_get(void)void cpu_list_add (CPUState *cpu) {static bool cpu_index_auto_assigned; +CPUTailQ *accel_cpus_queue =cpus_get_accel_cpus_queue (cpu);QEMU_LOCK_GUARD (&qemu_cpu_list_lock);if (cpu->cpu_index ==UNASSIGNED_CPU_INDEX ) {
@@ -92,17 +93,26 @@ void cpu_list_add(CPUState *cpu)assert (!cpu_index_auto_assigned); }QTAILQ_INSERT_TAIL_RCU (&cpus_queue, cpu, node); +if (accel_cpus_queue) { +QTAILQ_INSERT_TAIL_RCU (accel_cpus_queue, cpu, node); + } + cpu_list_generation_id++; }void cpu_list_remove (CPUState *cpu) { +CPUTailQ *accel_cpus_queue =cpus_get_accel_cpus_queue (cpu); +QEMU_LOCK_GUARD (&qemu_cpu_list_lock);if (!QTAILQ_IN_USE (cpu, node)) {/* there is nothing to undo since cpu_exec_init() hasn't been called */ return ; } +if (accel_cpus_queue) { +QTAILQ_REMOVE_RCU (accel_cpus_queue, cpu, node); + }QTAILQ_REMOVE_RCU (&cpus_queue, cpu, node); cpu->cpu_index =UNASSIGNED_CPU_INDEX ; cpu_list_generation_id++;
system/cpus.c +5 -0
@@ -209,6 +209,11 @@ void cpu_exec_reset_hold(CPUState *cpu) } } +CPUTailQ *cpus_get_accel_cpus_queue (CPUState *cpu) +{ +return cpus_accel ? cpus_accel->get_cpus_queue () :NULL ; +} +int64_t cpus_get_virtual_clock (void ) {/*
Signature · 1 line
2.47.1
RFC PATCH 3/7 accel/tcg: Implement tcg_get_cpus_queue()
In reply to Philippe Mathieu-DaudéUse a specific vCPUs queue for our unique software accelerator. Register the AccelOpsClass::get_cpus_queue() handler.
--- accel/tcg/tcg-accel-ops.h | 10 ++++++++++ accel/tcg/tcg-accel-ops.c | 8 ++++++++ 2 files changed, 18 insertions(+)
accel/tcg/tcg-accel-ops.h +10 -0
@@ -13,10 +13,20 @@#define TCG_ACCEL_OPS_H #include "system/cpus.h" +#include "hw/core/cpu.h" void tcg_cpu_destroy (CPUState *cpu);int tcg_cpu_exec (CPUState *cpu);void tcg_handle_interrupt (CPUState *cpu,int mask);void tcg_cpu_init_cflags (CPUState *cpu,bool parallel); +#ifdef CONFIG_USER_ONLY +#define tcg_cpus_queue cpus_queue +#else +/* Guard with qemu_cpu_list_lock */ +extern CPUTailQ tcg_cpus_queue; +#endif + +#define CPU_FOREACH_TCG (cpu) QTAILQ_FOREACH_RCU(cpu, &tcg_cpus_queue, node) +#endif /* TCG_ACCEL_OPS_H */
accel/tcg/tcg-accel-ops.c +8 -0
@@ -47,6 +47,13 @@/* common functionality among all TCG variants */ +CPUTailQ tcg_cpus_queue =QTAILQ_HEAD_INITIALIZER (tcg_cpus_queue); + +static CPUTailQ *tcg_get_cpus_queue (void ) +{ +return &tcg_cpus_queue; +} +void tcg_cpu_init_cflags (CPUState *cpu,bool parallel) {uint32_t cflags;
@@ -199,6 +206,7 @@ static inline void tcg_remove_all_breakpoints(CPUState *cpu)static void tcg_accel_ops_init (AccelOpsClass *ops) { + ops->get_cpus_queue = tcg_get_cpus_queue;if (qemu_tcg_mttcg_enabled ()) { ops->create_vcpu_thread = mttcg_start_vcpu_thread; ops->kick_vcpu_thread = mttcg_kick_vcpu_thread;
Signature · 1 line
2.47.1
RFC PATCH 4/7 accel/tcg: Use CPU_FOREACH_TCG()
In reply to Philippe Mathieu-DaudéOnly iterate over TCG vCPUs when running TCG specific code.
--- accel/tcg/cputlb.c | 7 ++++--- accel/tcg/monitor.c | 3 ++- accel/tcg/tb-maint.c | 7 ++++--- accel/tcg/tcg-accel-ops-rr.c | 10 +++++----- accel/tcg/tcg-accel-ops.c | 8 ++++---- 5 files changed, 19 insertions(+), 16 deletions(-)
accel/tcg/cputlb.c +4 -3
@@ -48,6 +48,7 @@#endif #include "tcg/tcg-ldst.h" #include "tcg/oversized-guest.h" +#include "tcg-accel-ops.h" /* DEBUG defines, enable DEBUG_TLB_LOG to log to the CPU_LOG_MMU target */ /* #define DEBUG_TLB */
@@ -368,7 +369,7 @@ static void flush_all_helper(CPUState *src, run_on_cpu_func fn, {CPUState *cpu; -CPU_FOREACH (cpu) { +CPU_FOREACH_TCG (cpu) {if (cpu != src) {async_run_on_cpu (cpu, fn, d); }
@@ -646,7 +647,7 @@ void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *src_cpu,TLBFlushPageByMMUIdxData *d;/* Allocate a separate data block for each destination cpu. */ -CPU_FOREACH (dst_cpu) { +CPU_FOREACH_TCG (dst_cpu) {if (dst_cpu != src_cpu) { d =g_new (TLBFlushPageByMMUIdxData,1 ); d->addr = addr;
@@ -839,7 +840,7 @@ void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState *src_cpu, d.bits = bits;/* Allocate a separate data block for each destination cpu. */ -CPU_FOREACH (dst_cpu) { +CPU_FOREACH_TCG (dst_cpu) {if (dst_cpu != src_cpu) { p =g_memdup (&d,sizeof (d)); async_run_on_cpu(dst_cpu, tlb_flush_range_by_mmuidx_async_1,
accel/tcg/monitor.c +2 -1
@@ -19,6 +19,7 @@#include "tcg/tcg.h" #include "internal-common.h" #include "tb-context.h" +#include "tcg-accel-ops.h" static void dump_drift_info (GString *buf)
@@ -131,7 +132,7 @@ static void tlb_flush_counts(size_t *pfull, size_t *ppart, size_t *pelide)CPUState *cpu;size_t full =0 , part =0 , elide =0 ; -CPU_FOREACH (cpu) { +CPU_FOREACH_TCG (cpu) { full +=qatomic_read (&cpu->neg.tlb.c.full_flush_count); part +=qatomic_read (&cpu->neg.tlb.c.part_flush_count); elide +=qatomic_read (&cpu->neg.tlb.c.elide_flush_count);
accel/tcg/tb-maint.c +4 -3
@@ -36,6 +36,7 @@#ifdef CONFIG_USER_ONLY #include "user/page-protection.h" #endif +#include "tcg-accel-ops.h" /* List iterators for lists of tagged pointers in TranslationBlock. */
@@ -771,7 +772,7 @@ static void do_tb_flush(CPUState *cpu, run_on_cpu_data tb_flush_count) } did_flush = true; -CPU_FOREACH (cpu) { +CPU_FOREACH_TCG (cpu) {tcg_flush_jmp_cache (cpu); }
@@ -885,13 +886,13 @@ static void tb_jmp_cache_inval_tb(TranslationBlock *tb)if (tb_cflags (tb) &CF_PCREL ) {/* A TB may be at any virtual address */ -CPU_FOREACH (cpu) { +CPU_FOREACH_TCG (cpu) {tcg_flush_jmp_cache (cpu); } }else {uint32_t h =tb_jmp_cache_hash_func (tb->pc); -CPU_FOREACH (cpu) { +CPU_FOREACH_TCG (cpu) {CPUJumpCache *jc = cpu->tb_jmp_cache;if (qatomic_read (&jc->array[h].tb) == tb) {
accel/tcg/tcg-accel-ops-rr.c +5 -5
@@ -42,7 +42,7 @@ void rr_kick_vcpu_thread(CPUState *unused) {CPUState *cpu; -CPU_FOREACH (cpu) { +CPU_FOREACH_TCG (cpu) {cpu_exit (cpu); }; }
@@ -116,7 +116,7 @@ static void rr_wait_io_event(void)rr_start_kick_timer (); -CPU_FOREACH (cpu) { +CPU_FOREACH_TCG (cpu) {qemu_wait_io_event_common (cpu); } }
@@ -129,7 +129,7 @@ static void rr_deal_with_unplugged_cpus(void) {CPUState *cpu; -CPU_FOREACH (cpu) { +CPU_FOREACH_TCG (cpu) {if (cpu->unplug && !cpu_can_run (cpu)) {tcg_cpu_destroy (cpu);break ;
@@ -160,7 +160,7 @@ static int rr_cpu_count(void)if (cpu_list_generation_id_get () != last_gen_id) { cpu_count =0 ; -CPU_FOREACH (cpu) { +CPU_FOREACH_TCG (cpu) { ++cpu_count; } last_gen_id =cpu_list_generation_id_get ();
@@ -201,7 +201,7 @@ static void *rr_cpu_thread_fn(void *arg)qemu_cond_wait_bql (first_cpu->halt_cond);/* process any pending work */ -CPU_FOREACH (cpu) { +CPU_FOREACH_TCG (cpu) { current_cpu = cpu;qemu_wait_io_event_common (cpu); }
accel/tcg/tcg-accel-ops.c +4 -4
@@ -144,7 +144,7 @@ static int tcg_insert_breakpoint(CPUState *cs, int type, vaddr addr, vaddr len)switch (type) {case GDB_BREAKPOINT_SW :case GDB_BREAKPOINT_HW : - CPU_FOREACH(cpu) { + CPU_FOREACH_TCG(cpu) { err =cpu_breakpoint_insert (cpu, addr,BP_GDB ,NULL );if (err) {break ;
@@ -154,7 +154,7 @@ static int tcg_insert_breakpoint(CPUState *cs, int type, vaddr addr, vaddr len)case GDB_WATCHPOINT_WRITE :case GDB_WATCHPOINT_READ :case GDB_WATCHPOINT_ACCESS : - CPU_FOREACH(cpu) { + CPU_FOREACH_TCG(cpu) { err =cpu_watchpoint_insert (cpu, addr, len,xlat_gdb_type (cpu, type),NULL );if (err) {
@@ -175,7 +175,7 @@ static int tcg_remove_breakpoint(CPUState *cs, int type, vaddr addr, vaddr len)switch (type) {case GDB_BREAKPOINT_SW :case GDB_BREAKPOINT_HW : - CPU_FOREACH(cpu) { + CPU_FOREACH_TCG(cpu) { err =cpu_breakpoint_remove (cpu, addr,BP_GDB );if (err) {break ;
@@ -185,7 +185,7 @@ static int tcg_remove_breakpoint(CPUState *cs, int type, vaddr addr, vaddr len)case GDB_WATCHPOINT_WRITE :case GDB_WATCHPOINT_READ :case GDB_WATCHPOINT_ACCESS : - CPU_FOREACH(cpu) { + CPU_FOREACH_TCG(cpu) { err =cpu_watchpoint_remove (cpu, addr, len,xlat_gdb_type (cpu, type));if (err) {
Signature · 1 line
2.47.1
RFC PATCH 5/7 accel/hw: Implement hw_accel_get_cpus_queue()
In reply to Philippe Mathieu-DaudéWe can only run a single hardware accelerator at a time, so add a generic hw_accel_get_cpus_queue() helper in accel-system.c, a file common to all HW accelerators.
Register AccelOpsClass::get_cpus_queue() for each HW accelerator. Add a generic CPU_FOREACH_HWACCEL() macro.
--- include/system/hw_accel.h | 9 +++++++++ accel/accel-system.c | 8 ++++++++ accel/kvm/kvm-accel-ops.c | 1 + accel/xen/xen-all.c | 1 + target/i386/nvmm/nvmm-accel-ops.c | 1 + target/i386/whpx/whpx-accel-ops.c | 1 + 6 files changed, 21 insertions(+)
include/system/hw_accel.h +9 -0
@@ -2,6 +2,7 @@* QEMU Hardware accelerators support * * Copyright 2016 Google, Inc. +* Copyright 2025 Linaro Ltd. * * This work is licensed under the terms of the GNU GPL, version 2 or later. * See the COPYING file in the top-level directory.
@@ -17,6 +18,14 @@#include "system/whpx.h" #include "system/nvmm.h" +/* Guard with qemu_cpu_list_lock */ +extern CPUTailQ hw_accel_cpus_queue; + +#define CPU_FOREACH_HWACCEL (cpu) \ + QTAILQ_FOREACH_RCU(cpu, &hw_accel_cpus_queue, node) + +CPUTailQ *hw_accel_get_cpus_queue (void ); +void cpu_synchronize_state (CPUState *cpu);void cpu_synchronize_post_reset (CPUState *cpu);void cpu_synchronize_post_init (CPUState *cpu);
accel/accel-system.c +8 -0
@@ -27,9 +27,17 @@#include "qemu/accel.h" #include "hw/boards.h" #include "system/cpus.h" +#include "system/hw_accel.h" #include "qemu/error-report.h" #include "accel-system.h" +CPUTailQ hw_accel_cpus_queue =QTAILQ_HEAD_INITIALIZER (hw_accel_cpus_queue); + +CPUTailQ *hw_accel_get_cpus_queue (void ) +{ +return &hw_accel_cpus_queue; +} +int accel_init_machine (AccelState *accel,MachineState *ms) {AccelClass *acc =ACCEL_GET_CLASS (accel);
accel/kvm/kvm-accel-ops.c +1 -0
@@ -93,6 +93,7 @@ static void kvm_accel_ops_class_init(ObjectClass *oc, void *data) {AccelOpsClass *ops =ACCEL_OPS_CLASS (oc); + ops->get_cpus_queue = hw_accel_get_cpus_queue; ops->create_vcpu_thread = kvm_start_vcpu_thread; ops->cpu_thread_is_idle = kvm_vcpu_thread_is_idle; ops->cpus_are_resettable = kvm_cpus_are_resettable;
accel/xen/xen-all.c +1 -0
@@ -150,6 +150,7 @@ static void xen_accel_ops_class_init(ObjectClass *oc, void *data) {AccelOpsClass *ops =ACCEL_OPS_CLASS (oc); + ops->get_cpus_queue = hw_accel_get_cpus_queue; ops->create_vcpu_thread = dummy_start_vcpu_thread; }
target/i386/nvmm/nvmm-accel-ops.c +1 -0
@@ -84,6 +84,7 @@ static void nvmm_accel_ops_class_init(ObjectClass *oc, void *data) {AccelOpsClass *ops =ACCEL_OPS_CLASS (oc); + ops->get_cpus_queue = hw_accel_get_cpus_queue; ops->create_vcpu_thread = nvmm_start_vcpu_thread; ops->kick_vcpu_thread = nvmm_kick_vcpu_thread;
target/i386/whpx/whpx-accel-ops.c +1 -0
@@ -86,6 +86,7 @@ static void whpx_accel_ops_class_init(ObjectClass *oc, void *data) {AccelOpsClass *ops =ACCEL_OPS_CLASS (oc); + ops->get_cpus_queue = hw_accel_get_cpus_queue; ops->create_vcpu_thread = whpx_start_vcpu_thread; ops->kick_vcpu_thread = whpx_kick_vcpu_thread; ops->cpu_thread_is_idle = whpx_vcpu_thread_is_idle;
Signature · 1 line
2.47.1
RFC PATCH 6/7 accel/hvf: Use CPU_FOREACH_HVF()
In reply to Philippe Mathieu-DaudéOnly iterate over HVF vCPUs when running HVF specific code.
--- include/system/hvf_int.h | 4 ++++ accel/hvf/hvf-accel-ops.c | 9 +++++---- target/arm/hvf/hvf.c | 4 ++-- 3 files changed, 11 insertions(+), 6 deletions(-)
include/system/hvf_int.h +4 -0
@@ -11,6 +11,8 @@#ifndef HVF_INT_H #define HVF_INT_H +#include "system/hw_accel.h" +#ifdef __aarch64__#include <Hypervisor/Hypervisor.h> typedef hv_vcpu_t hvf_vcpuid ;
@@ -74,4 +76,6 @@ int hvf_put_registers(CPUState *);int hvf_get_registers (CPUState *);void hvf_kick_vcpu_thread (CPUState *cpu); +#define CPU_FOREACH_HVF (cpu) CPU_FOREACH_HWACCEL(cpu) +#endif
accel/hvf/hvf-accel-ops.c +5 -4
@@ -504,7 +504,7 @@ static int hvf_insert_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len) } } -CPU_FOREACH (cpu) { +CPU_FOREACH_HVF (cpu) { err =hvf_update_guest_debug (cpu);if (err) {return err;
@@ -543,7 +543,7 @@ static int hvf_remove_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len) } } -CPU_FOREACH (cpu) { +CPU_FOREACH_HVF (cpu) { err =hvf_update_guest_debug (cpu);if (err) {return err;
@@ -560,7 +560,7 @@ static void hvf_remove_all_breakpoints(CPUState *cpu) QTAILQ_FOREACH_SAFE(bp, &hvf_state->hvf_sw_breakpoints, entry, next) {if (hvf_arch_remove_sw_breakpoint (cpu, bp) !=0 ) {/* Try harder to find a CPU that currently sees the breakpoint. */ -CPU_FOREACH (tmpcpu) +CPU_FOREACH_HVF (tmpcpu) {if (hvf_arch_remove_sw_breakpoint (tmpcpu, bp) ==0 ) {break ;
@@ -572,7 +572,7 @@ static void hvf_remove_all_breakpoints(CPUState *cpu) }hvf_arch_remove_all_hw_breakpoints (); -CPU_FOREACH (cpu) { +CPU_FOREACH_HVF (cpu) {hvf_update_guest_debug (cpu); } }
@@ -581,6 +581,7 @@ static void hvf_accel_ops_class_init(ObjectClass *oc, void *data) {AccelOpsClass *ops =ACCEL_OPS_CLASS (oc); + ops->get_cpus_queue = hw_accel_get_cpus_queue; ops->create_vcpu_thread = hvf_start_vcpu_thread; ops->kick_vcpu_thread = hvf_kick_vcpu_thread;
target/arm/hvf/hvf.c +2 -2
@@ -2269,10 +2269,10 @@ static void hvf_arch_set_traps(void)/* Check whether guest debugging is enabled for at least one vCPU; if it * is, enable exiting the guest on all vCPUs */ -CPU_FOREACH (cpu) { +CPU_FOREACH_HVF (cpu) { should_enable_traps |= cpu->accel->guest_debug_enabled; } -CPU_FOREACH (cpu) { +CPU_FOREACH_HVF (cpu) {/* Set whether debug exceptions exit the guest */ r =hv_vcpu_set_trap_debug_exceptions (cpu->accel->fd, should_enable_traps);
Signature · 1 line
2.47.1
Re: [RFC PATCH 6/7] accel/hvf: Use CPU_FOREACH_HVF()
Quoted 23 lines · Philippe Mathieu-Daudé
Only iterate over HVF vCPUs when running HVF specific code.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/system/hvf_int.h | 4 ++++ accel/hvf/hvf-accel-ops.c | 9 +++++---- target/arm/hvf/hvf.c | 4 ++-- 3 files changed, 11 insertions(+), 6 deletions(-)include/system/hvf_int.h +2
-0@@ -11,6 +11,8 @@#ifndef HVF_INT_H #define HVF_INT_H +#include "system/hw_accel.h" +#ifdef __aarch64__#include <Hypervisor/Hypervisor.h> typedef hv_vcpu_t hvf_vcpuid ;
include/system/hvf_int.h int hvf_put_registers(CPUState *); · line 79
Quoted 4 lines
include/system/hvf_int.h +1
-0@@ -74,4 +76,6 @@ int hvf_put_registers(CPUState *);int hvf_get_registers (CPUState *);void hvf_kick_vcpu_thread (CPUState *cpu); +#define CPU_FOREACH_HVF (cpu) CPU_FOREACH_HWACCEL(cpu)
Cosmetic comment: given that this is HVF specific code and we only support one hw accelerator at a time, I'd skip this alias and use CPU_FOREACH_HWACCEL(cpu) directly. It would make it easier when grepping to see where and how the macro is being used. Same thing in the next patch with CPU_FOREACH_KVM().
LGTM otherwise. Thanks,
Daniel
Quoted 67 lines
include/system/hvf_int.h +1
-0+#endif accel/hvf/hvf-accel-ops.c +5
-4@@ -504,7 +504,7 @@ static int hvf_insert_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len) } } -CPU_FOREACH (cpu) { +CPU_FOREACH_HVF (cpu) { err =hvf_update_guest_debug (cpu);if (err) {return err;@@ -543,7 +543,7 @@ static int hvf_remove_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len) } } -CPU_FOREACH (cpu) { +CPU_FOREACH_HVF (cpu) { err =hvf_update_guest_debug (cpu);if (err) {return err;@@ -560,7 +560,7 @@ static void hvf_remove_all_breakpoints(CPUState *cpu) QTAILQ_FOREACH_SAFE(bp, &hvf_state->hvf_sw_breakpoints, entry, next) {if (hvf_arch_remove_sw_breakpoint (cpu, bp) !=0 ) {/* Try harder to find a CPU that currently sees the breakpoint. */ -CPU_FOREACH (tmpcpu) +CPU_FOREACH_HVF (tmpcpu) {if (hvf_arch_remove_sw_breakpoint (tmpcpu, bp) ==0 ) {break ;@@ -572,7 +572,7 @@ static void hvf_remove_all_breakpoints(CPUState *cpu) }hvf_arch_remove_all_hw_breakpoints (); -CPU_FOREACH (cpu) { +CPU_FOREACH_HVF (cpu) {hvf_update_guest_debug (cpu); } }@@ -581,6 +581,7 @@ static void hvf_accel_ops_class_init(ObjectClass *oc, void *data) {AccelOpsClass *ops =ACCEL_OPS_CLASS (oc); + ops->get_cpus_queue = hw_accel_get_cpus_queue; ops->create_vcpu_thread = hvf_start_vcpu_thread; ops->kick_vcpu_thread = hvf_kick_vcpu_thread;target/arm/hvf/hvf.c +2
-2@@ -2269,10 +2269,10 @@ static void hvf_arch_set_traps(void)/* Check whether guest debugging is enabled for at least one vCPU; if it * is, enable exiting the guest on all vCPUs */ -CPU_FOREACH (cpu) { +CPU_FOREACH_HVF (cpu) { should_enable_traps |= cpu->accel->guest_debug_enabled; } -CPU_FOREACH (cpu) { +CPU_FOREACH_HVF (cpu) {/* Set whether debug exceptions exit the guest */ r =hv_vcpu_set_trap_debug_exceptions (cpu->accel->fd, should_enable_traps);
Re: [RFC PATCH 6/7] accel/hvf: Use CPU_FOREACH_HVF()
Quoted 25 lines · Daniel Henrique Barboza
Quoted 22 lines · Philippe Mathieu-Daudé
Only iterate over HVF vCPUs when running HVF specific code.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
include/system/hvf_int.h | 4 ++++
accel/hvf/hvf-accel-ops.c| 9 +++++----
target/arm/hvf/hvf.c | 4 ++--3 files changed, 11 insertions(+), 6 deletions(-)include/system/hvf_int.h +2
-0@@ -11,6 +11,8 @@ #ifndef HVF_INT_H #define HVF_INT_H +#include "system/hw_accel.h" + #ifdef __aarch64__ #include <Hypervisor/Hypervisor.h> typedef hv_vcpu_t hvf_vcpuid;@@ -74,4 +76,6 @@ int hvf_put_registers(CPUState *);
Quoted 8 lines
include/system/hvf_int.h int hvf_put_registers(CPUState *); · line 79
Quoted 3 lines
include/system/hvf_int.h +1
-0int hvf_get_registers(CPUState *); void hvf_kick_vcpu_thread(CPUState *cpu); +#define CPU_FOREACH_HVF (cpu) CPU_FOREACH_HWACCEL(cpu)Cosmetic comment: given that this is HVF specific code and we only support one hw accelerator at a time, I'd skip this alias and use
CPU_FOREACH_HWACCEL(cpu)directly. It would make it easier when grepping to see where and how the macro is being used.
I find it more useful to grep for a particular accelerator, or for all of them:
$ git grep CPU_FOREACH_
accel/hvf/hvf-accel-ops.c:507: CPU_FOREACH_HVF(cpu) {
accel/hvf/hvf-accel-ops.c:546: CPU_FOREACH_HVF(cpu) {
accel/kvm/kvm-all.c:875: CPU_FOREACH_KVM(cpu) {
accel/kvm/kvm-all.c:938: CPU_FOREACH_KVM(cpu) {
accel/tcg/cputlb.c:372: CPU_FOREACH_TCG(cpu) {
accel/tcg/cputlb.c:650: CPU_FOREACH_TCG(dst_cpu) {Re: [RFC PATCH 6/7] accel/hvf: Use CPU_FOREACH_HVF()
Quoted 38 lines · Philippe Mathieu-Daudé
Quoted 25 lines · Daniel Henrique Barboza
Quoted 22 lines · Philippe Mathieu-Daudé
Only iterate over HVF vCPUs when running HVF specific code.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
include/system/hvf_int.h | 4 ++++
accel/hvf/hvf-accel-ops.c| 9 +++++----
target/arm/hvf/hvf.c | 4 ++--3 files changed, 11 insertions(+), 6 deletions(-)include/system/hvf_int.h +2
-0@@ -11,6 +11,8 @@ #ifndef HVF_INT_H #define HVF_INT_H +#include "system/hw_accel.h" + #ifdef __aarch64__ #include <Hypervisor/Hypervisor.h> typedef hv_vcpu_t hvf_vcpuid;@@ -74,4 +76,6 @@ int hvf_put_registers(CPUState *);Quoted 8 lines
include/system/hvf_int.h int hvf_put_registers(CPUState *); · line 79
Quoted 3 lines
include/system/hvf_int.h +1
-0int hvf_get_registers(CPUState *); void hvf_kick_vcpu_thread(CPUState *cpu); +#define CPU_FOREACH_HVF (cpu) CPU_FOREACH_HWACCEL(cpu)Cosmetic comment: given that this is HVF specific code and we only support one hw accelerator at a time, I'd skip this alias and use
CPU_FOREACH_HWACCEL(cpu)directly. It would make it easier when grepping to see where and how the macro is being used.I find it more useful to grep for a particular accelerator, or for all of them:
Quoted 7 lines
$ git grep CPU_FOREACH_
accel/hvf/hvf-accel-ops.c:507: CPU_FOREACH_HVF(cpu) {
accel/hvf/hvf-accel-ops.c:546: CPU_FOREACH_HVF(cpu) {
accel/kvm/kvm-all.c:875: CPU_FOREACH_KVM(cpu) {
accel/kvm/kvm-all.c:938: CPU_FOREACH_KVM(cpu) {
accel/tcg/cputlb.c:372: CPU_FOREACH_TCG(cpu) {
accel/tcg/cputlb.c:650: CPU_FOREACH_TCG(dst_cpu) {But then you need to define a new macro for every new accelerator. Maybe it's simpler to have CPU_FOREACH take the queue as a parameter so no separate macro is needed for each accel (and they cannot get inconsistent by changing only one of them in the future).
Regards,
BALATON Zoltan
RFC PATCH 7/7 accel/kvm: Use CPU_FOREACH_KVM()
In reply to Philippe Mathieu-DaudéOnly iterate over KVM vCPUs when running KVM specific code.
--- include/system/kvm_int.h | 3 +++ accel/kvm/kvm-all.c | 14 +++++++------- hw/i386/kvm/clock.c | 3 ++- hw/intc/spapr_xive_kvm.c | 5 +++-- hw/intc/xics_kvm.c | 5 +++-- target/i386/kvm/kvm.c | 4 ++-- target/i386/kvm/xen-emu.c | 2 +- target/s390x/kvm/kvm.c | 2 +- target/s390x/kvm/stsi-topology.c | 3 ++- 9 files changed, 24 insertions(+), 17 deletions(-)
include/system/kvm_int.h +3 -0
@@ -13,6 +13,7 @@#include "qapi/qapi-types-common.h" #include "qemu/accel.h" #include "qemu/queue.h" +#include "system/hw_accel.h" #include "system/kvm.h" #include "hw/boards.h" #include "hw/i386/topology.h"
@@ -168,6 +169,8 @@ struct KVMStatechar *device; }; +#define CPU_FOREACH_KVM (cpu) CPU_FOREACH_HWACCEL(cpu) +void kvm_memory_listener_register (KVMState *s,KVMMemoryListener *kml,AddressSpace *as,int as_id,const char *name);
accel/kvm/kvm-all.c +7 -7
@@ -872,7 +872,7 @@ static uint64_t kvm_dirty_ring_reap_locked(KVMState *s, CPUState* cpu)if (cpu) { total =kvm_dirty_ring_reap_one (s, cpu); }else { -CPU_FOREACH (cpu) { +CPU_FOREACH_KVM (cpu) { total +=kvm_dirty_ring_reap_one (s, cpu); } }
@@ -935,7 +935,7 @@ static void kvm_cpu_synchronize_kick_all(void) {CPUState *cpu; -CPU_FOREACH (cpu) { +CPU_FOREACH_KVM (cpu) {run_on_cpu (cpu, do_kvm_cpu_synchronize_kick,RUN_ON_CPU_NULL ); } }
@@ -3535,7 +3535,7 @@ int kvm_insert_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len) } } -CPU_FOREACH (cpu) { +CPU_FOREACH_KVM (cpu) { err =kvm_update_guest_debug (cpu,0 );if (err) {return err;
@@ -3574,7 +3574,7 @@ int kvm_remove_breakpoint(CPUState *cpu, int type, vaddr addr, vaddr len) } } -CPU_FOREACH (cpu) { +CPU_FOREACH_KVM (cpu) { err =kvm_update_guest_debug (cpu,0 );if (err) {return err;
@@ -3592,7 +3592,7 @@ void kvm_remove_all_breakpoints(CPUState *cpu) QTAILQ_FOREACH_SAFE(bp, &s->kvm_sw_breakpoints, entry, next) {if (kvm_arch_remove_sw_breakpoint (cpu, bp) !=0 ) {/* Try harder to find a CPU that currently sees the breakpoint. */ -CPU_FOREACH (tmpcpu) { +CPU_FOREACH_KVM (tmpcpu) {if (kvm_arch_remove_sw_breakpoint (tmpcpu, bp) ==0 ) {break ; }
@@ -3603,7 +3603,7 @@ void kvm_remove_all_breakpoints(CPUState *cpu) }kvm_arch_remove_all_hw_breakpoints (); -CPU_FOREACH (cpu) { +CPU_FOREACH_KVM (cpu) {kvm_update_guest_debug (cpu,0 ); } }
@@ -4384,7 +4384,7 @@ static void query_stats_cb(StatsResultList **result, StatsTarget target, stats_args.result.stats = result; stats_args.names = names; stats_args.errp = errp; -CPU_FOREACH (cpu) { +CPU_FOREACH_KVM (cpu) {if (!apply_str_list_filter (cpu->parent_obj.canonical_path, targets)) {continue ; }
hw/i386/kvm/clock.c +2 -1
@@ -17,6 +17,7 @@#include "qemu/host-utils.h" #include "qemu/module.h" #include "system/kvm.h" +#include "system/kvm_int.h" #include "system/runstate.h" #include "system/hw_accel.h" #include "kvm/kvm_i386.h"
@@ -196,7 +197,7 @@ static void kvmclock_vm_state_change(void *opaque, bool running,if (!cap_clock_ctrl) {return ; } -CPU_FOREACH (cpu) { +CPU_FOREACH_KVM (cpu) {run_on_cpu (cpu, do_kvmclock_ctrl,RUN_ON_CPU_NULL ); } }else {
hw/intc/spapr_xive_kvm.c +3 -2
@@ -14,6 +14,7 @@#include "target/ppc/cpu.h" #include "system/cpus.h" #include "system/kvm.h" +#include "system/kvm_int.h" #include "system/runstate.h" #include "hw/ppc/spapr.h" #include "hw/ppc/spapr_cpu_core.h"
@@ -678,7 +679,7 @@ int kvmppc_xive_post_load(SpaprXive *xive, int version_id)* 'post_load' handler of XiveTCTX because the machine is not * necessarily connected to the KVM device at that time. */ -CPU_FOREACH (cs) { +CPU_FOREACH_KVM (cs) {PowerPCCPU *cpu =POWERPC_CPU (cs); ret =kvmppc_xive_cpu_set_state (spapr_cpu_state (cpu)->tctx, &local_err);
@@ -795,7 +796,7 @@ int kvmppc_xive_connect(SpaprInterruptController *intc, uint32_t nr_servers, kvmppc_xive_change_state_handler, xive);/* Connect the presenters to the initial VCPUs of the machine */ -CPU_FOREACH (cs) { +CPU_FOREACH_KVM (cs) {PowerPCCPU *cpu =POWERPC_CPU (cs); ret =kvmppc_xive_cpu_connect (spapr_cpu_state (cpu)->tctx, errp);
hw/intc/xics_kvm.c +3 -2
@@ -29,6 +29,7 @@#include "qapi/error.h" #include "trace.h" #include "system/kvm.h" +#include "system/kvm_int.h" #include "hw/ppc/spapr.h" #include "hw/ppc/spapr_cpu_core.h" #include "hw/ppc/xics.h"
@@ -418,7 +419,7 @@ int xics_kvm_connect(SpaprInterruptController *intc, uint32_t nr_servers, kvm_gsi_direct_mapping = true;/* Create the presenters */ -CPU_FOREACH (cs) { +CPU_FOREACH_KVM (cs) {PowerPCCPU *cpu =POWERPC_CPU (cs);icp_kvm_realize (DEVICE (spapr_cpu_state (cpu)->icp), &local_err);
@@ -434,7 +435,7 @@ int xics_kvm_connect(SpaprInterruptController *intc, uint32_t nr_servers, }/* Connect the presenters to the initial VCPUs of the machine */ -CPU_FOREACH (cs) { +CPU_FOREACH_KVM (cs) {PowerPCCPU *cpu =POWERPC_CPU (cs);icp_set_kvm_state (spapr_cpu_state (cpu)->icp, &local_err);if (local_err) {
target/i386/kvm/kvm.c +2 -2
@@ -329,7 +329,7 @@ void kvm_synchronize_all_tsc(void)CPUState *cpu;if (kvm_enabled ()) { -CPU_FOREACH (cpu) { +CPU_FOREACH_KVM (cpu) {run_on_cpu (cpu, do_kvm_synchronize_tsc,RUN_ON_CPU_NULL ); } }
@@ -2847,7 +2847,7 @@ static void *kvm_msr_energy_thread(void *data)* Identify the vcpu threads * Calculate the number of vcpu per package */ -CPU_FOREACH (cpu) { +CPU_FOREACH_KVM (cpu) {for (int i =0 ; i < num_threads; i++) {if (cpu->thread_id == thd_stat[i].thread_id) { thd_stat[i].is_vcpu = true;
target/i386/kvm/xen-emu.c +1 -1
@@ -1422,7 +1422,7 @@ int kvm_xen_soft_reset(void)return err; } -CPU_FOREACH (cpu) { +CPU_FOREACH_KVM (cpu) {async_run_on_cpu (cpu, do_vcpu_soft_reset,RUN_ON_CPU_NULL ); }
target/s390x/kvm/kvm.c +1 -1
@@ -1559,7 +1559,7 @@ static void handle_diag_318(S390CPU *cpu, struct kvm_run *run)return ; } -CPU_FOREACH (t) { +CPU_FOREACH_KVM (t) {run_on_cpu (t, s390_do_cpu_set_diag318,RUN_ON_CPU_HOST_ULONG (diag318_info)); }
target/s390x/kvm/stsi-topology.c +2 -1
@@ -10,6 +10,7 @@#include "cpu.h" #include "hw/s390x/sclp.h" #include "hw/s390x/cpu-topology.h" +#include "system/kvm_int.h" QEMU_BUILD_BUG_ON (S390_CPU_ENTITLEMENT_LOW !=1 );QEMU_BUILD_BUG_ON (S390_CPU_ENTITLEMENT_MEDIUM !=2 );
@@ -256,7 +257,7 @@ static void s390_topology_fill_list_sorted(S390TopologyList *topology_list)QTAILQ_INSERT_HEAD (topology_list, &sentinel, next); -CPU_FOREACH (cs) { +CPU_FOREACH_KVM (cs) {S390TopologyId id =s390_topology_from_cpu (S390_CPU (cs));S390TopologyEntry *entry =NULL , *tmp;
Signature · 1 line
2.47.1
Cc'ing Pierrick & Salil.
Quoted 62 lines · Philippe Mathieu-Daudé
Hi,
Currently we register all vCPUs to the global '
cpus_queue' queue, however we can not discriminate per accelerator or per target architecture (which might happen in a soon future).This series tries to add an accelerator discriminator, so accelerator specific code can iterate on its own vCPUs. This is required to run a pair of HW + SW accelerators like the (HVF, TCG) or (KVM, TCG) combinations. Otherwise, i.e. the HVF core code could iterate on TCG vCPUs...
To keep it simple and not refactor heavily the code base, we introduce the CPU_FOREACH_TCG/HVF/KVM() macros, only defined for each accelerator.This is just a RFC to get some thoughts whether this is heading in the correct direction or not ;)
Regards,
Phil.
Philippe Mathieu-Daudé (7):
cpus: RestrictCPU_FOREACH_SAFE()to user emulation
cpus: IntroduceAccelOpsClass::get_cpus_queue()accel/tcg: Implementtcg_get_cpus_queue()accel/tcg: UseCPU_FOREACH_TCG()
accel/hw: Implementhw_accel_get_cpus_queue()accel/hvf: UseCPU_FOREACH_HVF()accel/kvm: UseCPU_FOREACH_KVM()accel/tcg/tcg-accel-ops.h | 10 ++++++++++ include/hw/core/cpu.h | 11 +++++++++++ include/system/accel-ops.h | 6 ++++++ include/system/hvf_int.h | 4 ++++ include/system/hw_accel.h | 9 +++++++++ include/system/kvm_int.h | 3 +++ accel/accel-system.c | 8 ++++++++ accel/hvf/hvf-accel-ops.c | 9 +++++---- accel/kvm/kvm-accel-ops.c | 1 + accel/kvm/kvm-all.c | 14 +++++++------- accel/tcg/cputlb.c | 7 ++++--- accel/tcg/monitor.c | 3 ++- accel/tcg/tb-maint.c | 7 ++++--- accel/tcg/tcg-accel-ops-rr.c | 10 +++++----- accel/tcg/tcg-accel-ops.c | 16 ++++++++++++---- accel/tcg/user-exec-stub.c | 5 +++++ accel/xen/xen-all.c | 1 + cpu-common.c | 10 ++++++++++ hw/i386/kvm/clock.c | 3 ++- hw/intc/spapr_xive_kvm.c | 5 +++-- hw/intc/xics_kvm.c | 5 +++-- system/cpus.c | 5 +++++ target/arm/hvf/hvf.c | 4 ++-- target/i386/kvm/kvm.c | 4 ++-- target/i386/kvm/xen-emu.c | 2 +- target/i386/nvmm/nvmm-accel-ops.c | 1 + target/i386/whpx/whpx-accel-ops.c | 1 + target/s390x/kvm/kvm.c | 2 +- target/s390x/kvm/stsi-topology.c | 3 ++- 29 files changed, 130 insertions(+), 39 deletions(-)