accel: Restrict TCG plugin (un)registration to TCG accel
cover accel: Restrict TCG plugin (un)registration to TCG accel
Hi,
TL;DR; this series remove TCG plugin code from generic accel code.
Since the introduction of the scoreboard in plugins, the INIT hook use the vCPU index, which is only available somewhere during the vCPU REALIZE() step (see below for call tree).
In order to clarify that, we split accel_cpu_common_[un]realize as *unassigned and *assigned steps. This allow to remove the plugin [un]registration code from common accel code.
Another approach suggested by rth is to add a PostRealize() handler in DeviceClass. This was already experimented here:
https://lore.kernel.org/qemu-devel/[email protected]/
Since it is a change harder to sell, I took this simplified path which just make the vCPU REALIZE a bit more complex, but not really an concern since the current call tree is
https://etherpad.opendev.org/p/QEMU_vCPU_life.
Philippe Mathieu-Daudé (6):
system/runstate: Remove unused 'qemu/plugin.h' header accel/tcg: Move common declarations to 'internal-common.h'
accel: Clarify accel_cpu_common_[un]realize() use unassigned vCPU
accel: Introduce accel_cpu_common_[un]realize_assigned() handlers
accel: Restrict TCG plugin (un)registration to TCG accel accel/tcg: Move qemu_plugin_vcpu_init__async() to plugins/
accel/tcg/internal-common.h | 17 ++++++++++++++++ accel/tcg/internal-target.h | 14 ------------- include/qemu/accel.h | 39 ++++++++++++++++++++++++++++++------- include/qemu/plugin.h | 6 ------ accel/accel-target.c | 34 +++++++++++++++++++++++++++----- accel/tcg/cpu-exec-common.c | 20 +++++++++++++++++++ accel/tcg/cpu-exec.c | 4 ++-- accel/tcg/tcg-all.c | 8 +++++--- cpu-target.c | 10 ++++++++-- hw/core/cpu-common.c | 25 ------------------------ plugins/core.c | 8 +++++++- system/runstate.c | 1 - 12 files changed, 120 insertions(+), 66 deletions(-)
Signature · 1 line
2.41.0
PATCH 1/6 system/runstate: Remove unused 'qemu/plugin.h' header
system/runstate.c never required "qemu/plugin.h".
--- system/runstate.c | 1 - 1 file changed, 1 deletion(-)
system/runstate.c +0 -1
@@ -45,7 +45,6 @@#include "qemu/job.h" #include "qemu/log.h" #include "qemu/module.h" -#include "qemu/plugin.h" #include "qemu/sockets.h" #include "qemu/timer.h" #include "qemu/thread.h"
Signature · 1 line
2.41.0
PATCH 2/6 accel/tcg: Move common declarations to 'internal-common.h'
In reply to Philippe Mathieu-Daudé'internal-target.h' is meant for target-specific declarations, while 'internal-common.h' for common ones. Move common declarations to it.
--- accel/tcg/internal-common.h | 15 +++++++++++++++ accel/tcg/internal-target.h | 14 -------------- accel/tcg/tcg-all.c | 2 +- 3 files changed, 16 insertions(+), 15 deletions(-)
accel/tcg/internal-common.h +15 -0
@@ -15,6 +15,8 @@extern int64_t max_delay;extern int64_t max_advance; +extern bool one_insn_per_tb; +/* * Return true if CS is not running in parallel with other cpus, either * because there are no other cpus or we are within an exclusive context.
@@ -41,4 +43,17 @@ static inline bool cpu_plugin_mem_cbs_enabled(const CPUState *cpu) #endif } +TranslationBlock *tb_gen_code (CPUState *cpu,vaddr pc, +uint64_t cs_base,uint32_t flags, +int cflags); +void page_init (void ); +void tb_htable_init (void ); +void tb_reset_jump (TranslationBlock *tb,int n); +TranslationBlock *tb_link_page (TranslationBlock *tb); +void cpu_restore_state_from_tb (CPUState *cpu,TranslationBlock *tb, +uintptr_t host_pc); + +bool tcg_exec_realizefn (CPUState *cpu,Error **errp); +void tcg_exec_unrealizefn (CPUState *cpu); +#endif
accel/tcg/internal-target.h +0 -14
@@ -69,19 +69,7 @@ void tb_invalidate_phys_range_fast(ram_addr_t ram_addr, G_NORETURN void cpu_io_recompile(CPUState *cpu, uintptr_t retaddr);#endif /* CONFIG_SOFTMMU */ -TranslationBlock *tb_gen_code (CPUState *cpu,vaddr pc, -uint64_t cs_base,uint32_t flags, -int cflags); -void page_init (void ); -void tb_htable_init (void ); -void tb_reset_jump (TranslationBlock *tb,int n); -TranslationBlock *tb_link_page (TranslationBlock *tb);bool tb_invalidate_phys_page_unwind (tb_page_addr_t addr,uintptr_t pc); -void cpu_restore_state_from_tb (CPUState *cpu,TranslationBlock *tb, -uintptr_t host_pc); - -bool tcg_exec_realizefn (CPUState *cpu,Error **errp); -void tcg_exec_unrealizefn (CPUState *cpu);/* Return the current PC from CPU, which may be cached in TB. */ static inline vaddr log_pc (CPUState *cpu,const TranslationBlock *tb)
@@ -93,8 +81,6 @@ static inline vaddr log_pc(CPUState *cpu, const TranslationBlock *tb) } } -extern bool one_insn_per_tb; -/** * tcg_req_mo: * @type: TCGBar
accel/tcg/tcg-all.c +1 -1
@@ -38,7 +38,7 @@#if !defined(CONFIG_USER_ONLY )#include "hw/boards.h" #endif -#include "internal-target.h" +#include "internal-common.h" struct TCGState {AccelState parent_obj;
Signature · 1 line
2.41.0
PATCH 3/6 accel: Clarify accel_cpu_common_[un]realize() use unassigned vCPU
In reply to Philippe Mathieu-DaudéIn preparation of introducing [un]realize handlers for when vCPUs are assigned, rename current handlers using the '_unassigned' suffix.
--- accel/tcg/internal-common.h | 4 ++-- include/qemu/accel.h | 17 +++++++++++------ accel/accel-target.c | 11 ++++++----- accel/tcg/cpu-exec.c | 4 ++-- accel/tcg/tcg-all.c | 4 ++-- cpu-target.c | 4 ++-- 6 files changed, 25 insertions(+), 19 deletions(-)
accel/tcg/internal-common.h +2 -2
@@ -53,7 +53,7 @@ TranslationBlock *tb_link_page(TranslationBlock *tb);void cpu_restore_state_from_tb (CPUState *cpu,TranslationBlock *tb,uintptr_t host_pc); -bool tcg_exec_realizefn (CPUState *cpu,Error **errp); -void tcg_exec_unrealizefn (CPUState *cpu); +bool tcg_exec_realize_unassigned (CPUState *cpu,Error **errp); +void tcg_exec_unrealize_unassigned (CPUState *cpu);#endif
include/qemu/accel.h +11 -6
@@ -43,8 +43,8 @@ typedef struct AccelClass {bool (*has_memory)(MachineState *ms,AddressSpace *as,hwaddr start_addr,hwaddr size);#endif -bool (*cpu_common_realize)(CPUState *cpu,Error **errp); -void (*cpu_common_unrealize)(CPUState *cpu); +bool (*cpu_common_realize_unassigned)(CPUState *cpu,Error **errp); +void (*cpu_common_unrealize_unassigned)(CPUState *cpu);/* gdbstub related hooks */ int (*gdbstub_supported_sstep_flags)(void );
@@ -92,17 +92,22 @@ void accel_setup_post(MachineState *ms);void accel_cpu_instance_init (CPUState *cpu);/** -* accel_cpu_common_realize: +* accel_cpu_common_realize_unassigned: * @cpu: The CPU that needs to call accel-specific cpu realization. * @errp: currently unused. +* +* The @cpu index is not yet assigned. */ -bool accel_cpu_common_realize (CPUState *cpu,Error **errp); +bool accel_cpu_common_realize_unassigned (CPUState *cpu,Error **errp);/** -* accel_cpu_common_unrealize: +* accel_cpu_common_unrealize_unassigned: * @cpu: The CPU that needs to call accel-specific cpu unrealization. +* +* The @cpu index is no more assigned, @cpu has been removed from the global +* #cpus_queue. */ -void accel_cpu_common_unrealize (CPUState *cpu); +void accel_cpu_common_unrealize_unassigned (CPUState *cpu);/** * accel_supported_gdbstub_sstep_flags:
accel/accel-target.c +6 -5
@@ -119,7 +119,7 @@ void accel_cpu_instance_init(CPUState *cpu) } } -bool accel_cpu_common_realize (CPUState *cpu,Error **errp) +bool accel_cpu_common_realize_unassigned (CPUState *cpu,Error **errp) {CPUClass *cc =CPU_GET_CLASS (cpu);AccelState *accel =current_accel ();
@@ -132,21 +132,22 @@ bool accel_cpu_common_realize(CPUState *cpu, Error **errp) }/* generic realization */ -if (acc->cpu_common_realize && !acc->cpu_common_realize (cpu, errp)) { +if (acc->cpu_common_realize_unassigned + && !acc->cpu_common_realize_unassigned (cpu, errp)) {return false; }return true; } -void accel_cpu_common_unrealize (CPUState *cpu) +void accel_cpu_common_unrealize_unassigned (CPUState *cpu) {AccelState *accel =current_accel ();AccelClass *acc =ACCEL_GET_CLASS (accel);/* generic unrealization */ -if (acc->cpu_common_unrealize) { - acc->cpu_common_unrealize (cpu); +if (acc->cpu_common_unrealize_unassigned) { + acc->cpu_common_unrealize_unassigned (cpu); } }
accel/tcg/cpu-exec.c +2 -2
@@ -1074,7 +1074,7 @@ int cpu_exec(CPUState *cpu)return ret; } -bool tcg_exec_realizefn (CPUState *cpu,Error **errp) +bool tcg_exec_realize_unassigned (CPUState *cpu,Error **errp) {static bool tcg_target_initialized;
@@ -1094,7 +1094,7 @@ bool tcg_exec_realizefn(CPUState *cpu, Error **errp) }/* undo the initializations in reverse order */ -void tcg_exec_unrealizefn (CPUState *cpu) +void tcg_exec_unrealize_unassigned (CPUState *cpu) {#ifndef CONFIG_USER_ONLY tcg_iommu_free_notifier_list (cpu);
accel/tcg/tcg-all.c +2 -2
@@ -227,8 +227,8 @@ static void tcg_accel_class_init(ObjectClass *oc, void *data)AccelClass *ac =ACCEL_CLASS (oc); ac->name ="tcg" ; ac->init_machine = tcg_init_machine; - ac->cpu_common_realize = tcg_exec_realizefn; - ac->cpu_common_unrealize = tcg_exec_unrealizefn; + ac->cpu_common_realize_unassigned = tcg_exec_realize_unassigned; + ac->cpu_common_unrealize_unassigned = tcg_exec_unrealize_unassigned; ac->allowed = &tcg_allowed; ac->gdbstub_supported_sstep_flags = tcg_gdbstub_supported_sstep_flags;
cpu-target.c +2 -2
@@ -136,7 +136,7 @@ bool cpu_exec_realizefn(CPUState *cpu, Error **errp)/* cache the cpu class for the hotpath */ cpu->cc =CPU_GET_CLASS (cpu); -if (!accel_cpu_common_realize (cpu, errp)) { +if (!accel_cpu_common_realize_unassigned (cpu, errp)) {return false; }
@@ -176,7 +176,7 @@ void cpu_exec_unrealizefn(CPUState *cpu)* Now that the vCPU has been removed from the RCU list, we can call * accel_cpu_common_unrealize, which may free fields using call_rcu. */ -accel_cpu_common_unrealize (cpu); +accel_cpu_common_unrealize_unassigned (cpu); }/*
Signature · 1 line
2.41.0
PATCH 4/6 accel: Introduce accel_cpu_common_[un]realize_assigned() handlers
In reply to Philippe Mathieu-DaudéIntroduce handlers called while the vCPU has an assigned index and is still in the global %cpus_queue.
--- include/qemu/accel.h | 20 ++++++++++++++++++++ accel/accel-target.c | 23 +++++++++++++++++++++++ cpu-target.c | 6 ++++++ 3 files changed, 49 insertions(+)
include/qemu/accel.h +20 -0
@@ -44,6 +44,8 @@ typedef struct AccelClass { hwaddr start_addr, hwaddr size);#endif bool (*cpu_common_realize_unassigned)(CPUState *cpu,Error **errp); +bool (*cpu_common_realize_assigned)(CPUState *cpu,Error **errp); +void (*cpu_common_unrealize_assigned)(CPUState *cpu);void (*cpu_common_unrealize_unassigned)(CPUState *cpu);/* gdbstub related hooks */
@@ -100,6 +102,24 @@ void accel_cpu_instance_init(CPUState *cpu);*/ bool accel_cpu_common_realize_unassigned (CPUState *cpu,Error **errp); +/** +* accel_cpu_common_realize_assigned: +* @cpu: The CPU that needs to call accel-specific cpu realization. +* @errp: currently unused. +* +* The @cpu index is assigned, @cpu is added to the global #cpus_queue. +*/ +bool accel_cpu_common_realize_assigned (CPUState *cpu,Error **errp); + +/** +* accel_cpu_common_unrealize_unassigned: +* @cpu: The CPU that needs to call accel-specific cpu unrealization. +* +* The @cpu index is still assigned, @cpu is still part of the global +* #cpus_queue. +*/ +void accel_cpu_common_unrealize_assigned (CPUState *cpu); +/** * accel_cpu_common_unrealize_unassigned: * @cpu: The CPU that needs to call accel-specific cpu unrealization.
accel/accel-target.c +23 -0
@@ -140,6 +140,29 @@ bool accel_cpu_common_realize_unassigned(CPUState *cpu, Error **errp)return true; } +bool accel_cpu_common_realize_assigned (CPUState *cpu,Error **errp) +{ +AccelState *accel =current_accel (); +AccelClass *acc =ACCEL_GET_CLASS (accel); + +if (acc->cpu_common_realize_assigned + && !acc->cpu_common_realize_assigned (cpu, errp)) { +return false; + } + +return true; +} + +void accel_cpu_common_unrealize_assigned (CPUState *cpu) +{ +AccelState *accel =current_accel (); +AccelClass *acc =ACCEL_GET_CLASS (accel); + +if (acc->cpu_common_unrealize_assigned) { + acc->cpu_common_unrealize_assigned (cpu); + } +} +void accel_cpu_common_unrealize_unassigned (CPUState *cpu) {AccelState *accel =current_accel ();
cpu-target.c +6 -0
@@ -143,6 +143,10 @@ bool cpu_exec_realizefn(CPUState *cpu, Error **errp)/* Wait until cpu initialization complete before exposing cpu. */ cpu_list_add (cpu); +if (!accel_cpu_common_realize_assigned (cpu, errp)) { +return false; + } +#ifdef CONFIG_USER_ONLY assert (qdev_get_vmsd (DEVICE (cpu)) ==NULL ||qdev_get_vmsd (DEVICE (cpu))->unmigratable);
@@ -171,6 +175,8 @@ void cpu_exec_unrealizefn(CPUState *cpu) }#endif +accel_cpu_common_unrealize_assigned (cpu); +cpu_list_remove (cpu);/* * Now that the vCPU has been removed from the RCU list, we can call
Signature · 1 line
2.41.0
PATCH 5/6 accel: Restrict TCG plugin (un)registration to TCG accel
In reply to Philippe Mathieu-DaudéUse the AccelClass::cpu_common_[un]realize_assigned() handlers to [un]register the TCG plugin handlers, allowing to remove accel specific code from the common hw/core/cpu-common.c file.
Remove the now unnecessary qemu_plugin_vcpu_init_hook() and qemu_plugin_vcpu_exit_hook() stub.
--- accel/tcg/internal-common.h | 2 ++ include/qemu/plugin.h | 6 ------ accel/tcg/cpu-exec-common.c | 27 +++++++++++++++++++++++++++ accel/tcg/tcg-all.c | 2 ++ hw/core/cpu-common.c | 25 ------------------------- 5 files changed, 31 insertions(+), 31 deletions(-)
accel/tcg/internal-common.h +2 -0
@@ -54,6 +54,8 @@ void cpu_restore_state_from_tb(CPUState *cpu, TranslationBlock *tb, uintptr_t host_pc);bool tcg_exec_realize_unassigned (CPUState *cpu,Error **errp); +bool tcg_exec_realize_assigned (CPUState *cpu,Error **errp); +void tcg_exec_unrealize_assigned (CPUState *cpu);void tcg_exec_unrealize_unassigned (CPUState *cpu);#endif
include/qemu/plugin.h +0 -6
@@ -221,12 +221,6 @@ static inline int qemu_plugin_load_list(QemuPluginList *head, Error **errp)return 0 ; } -static inline void qemu_plugin_vcpu_init_hook (CPUState *cpu) -{ } - -static inline void qemu_plugin_vcpu_exit_hook (CPUState *cpu) -{ } -static inline void qemu_plugin_tb_trans_cb (CPUState *cpu,struct qemu_plugin_tb *tb) { }
accel/tcg/cpu-exec-common.c +27 -0
@@ -56,3 +56,30 @@ void cpu_loop_exit_atomic(CPUState *cpu, uintptr_t pc) cpu->exception_index =EXCP_ATOMIC ;cpu_loop_exit_restore (cpu, pc); } + +#ifdef CONFIG_PLUGIN +static void qemu_plugin_vcpu_init__async (CPUState *cpu,run_on_cpu_data unused) +{ +qemu_plugin_vcpu_init_hook (cpu); +} +#endif + +bool tcg_exec_realize_assigned (CPUState *cpu,Error **errp) +{ +#ifdef CONFIG_PLUGIN + cpu->plugin_state =qemu_plugin_create_vcpu_state (); +/* Plugin initialization must wait until the cpu start executing code */ +async_run_on_cpu (cpu, qemu_plugin_vcpu_init__async,RUN_ON_CPU_NULL ); +#endif + +return true; +} + +/* undo the initializations in reverse order */ +void tcg_exec_unrealize_assigned (CPUState *cpu) +{ +#ifdef CONFIG_PLUGIN +/* Call the plugin hook before clearing the cpu is fully unrealized */ +qemu_plugin_vcpu_exit_hook (cpu); +#endif +}
accel/tcg/tcg-all.c +2 -0
@@ -228,6 +228,8 @@ static void tcg_accel_class_init(ObjectClass *oc, void *data) ac->name ="tcg" ; ac->init_machine = tcg_init_machine; ac->cpu_common_realize_unassigned = tcg_exec_realize_unassigned; + ac->cpu_common_realize_assigned = tcg_exec_realize_assigned; + ac->cpu_common_unrealize_assigned = tcg_exec_unrealize_assigned; ac->cpu_common_unrealize_unassigned = tcg_exec_unrealize_unassigned; ac->allowed = &tcg_allowed; ac->gdbstub_supported_sstep_flags = tcg_gdbstub_supported_sstep_flags;
hw/core/cpu-common.c +0 -25
@@ -30,9 +30,6 @@#include "hw/boards.h" #include "hw/qdev-properties.h" #include "trace.h" -#ifdef CONFIG_PLUGIN -#include "qemu/plugin.h" -#endif CPUState *cpu_by_arch_id (int64_t id) {
@@ -192,13 +189,6 @@ static void cpu_common_parse_features(const char *typename, char *features, } } -#ifdef CONFIG_PLUGIN -static void qemu_plugin_vcpu_init__async (CPUState *cpu,run_on_cpu_data unused) -{ -qemu_plugin_vcpu_init_hook (cpu); -} -#endif -static void cpu_common_realizefn (DeviceState *dev,Error **errp) {CPUState *cpu =CPU (dev);
@@ -222,14 +212,6 @@ static void cpu_common_realizefn(DeviceState *dev, Error **errp)cpu_resume (cpu); } -/* Plugin initialization must wait until the cpu start executing code */ -#ifdef CONFIG_PLUGIN -if (tcg_enabled ()) { - cpu->plugin_state =qemu_plugin_create_vcpu_state (); -async_run_on_cpu (cpu, qemu_plugin_vcpu_init__async,RUN_ON_CPU_NULL ); - } -#endif -/* NOTE: latest generic point where the cpu is fully realized */ }
@@ -237,13 +219,6 @@ static void cpu_common_unrealizefn(DeviceState *dev) {CPUState *cpu =CPU (dev); -/* Call the plugin hook before clearing the cpu is fully unrealized */ -#ifdef CONFIG_PLUGIN -if (tcg_enabled ()) { -qemu_plugin_vcpu_exit_hook (cpu); - } -#endif -/* NOTE: latest generic point before the cpu is fully unrealized */ cpu_exec_unrealizefn (cpu); }
Signature · 1 line
2.41.0
PATCH 6/6 accel/tcg: Move qemu_plugin_vcpu_init__async() to plugins/
In reply to Philippe Mathieu-DaudéCalling qemu_plugin_vcpu_init__async() on the vCPU thread is a detail of plugins, not relevant to TCG vCPU management.
--- accel/tcg/cpu-exec-common.c | 11 ++--------- plugins/core.c | 8 +++++++- 2 files changed, 9 insertions(+), 10 deletions(-)
accel/tcg/cpu-exec-common.c +2 -9
@@ -57,19 +57,12 @@ void cpu_loop_exit_atomic(CPUState *cpu, uintptr_t pc)cpu_loop_exit_restore (cpu, pc); } -#ifdef CONFIG_PLUGIN -static void qemu_plugin_vcpu_init__async (CPUState *cpu,run_on_cpu_data unused) -{ -qemu_plugin_vcpu_init_hook (cpu); -} -#endif -bool tcg_exec_realize_assigned (CPUState *cpu,Error **errp) {#ifdef CONFIG_PLUGIN cpu->plugin_state =qemu_plugin_create_vcpu_state (); -/* Plugin initialization must wait until the cpu start executing code */ -async_run_on_cpu (cpu, qemu_plugin_vcpu_init__async,RUN_ON_CPU_NULL ); + +qemu_plugin_vcpu_init_hook (cpu);#endif return true;
plugins/core.c +7 -1
@@ -245,7 +245,7 @@ static void plugin_grow_scoreboards__locked(CPUState *cpu)end_exclusive (); } -void qemu_plugin_vcpu_init_hook (CPUState *cpu) +static void qemu_plugin_vcpu_init__async (CPUState *cpu,run_on_cpu_data unused) {bool success;
@@ -261,6 +261,12 @@ void qemu_plugin_vcpu_init_hook(CPUState *cpu)plugin_vcpu_cb__simple (cpu,QEMU_PLUGIN_EV_VCPU_INIT ); } +void qemu_plugin_vcpu_init_hook (CPUState *cpu) +{ +/* Plugin initialization must wait until the cpu start executing code */ +async_run_on_cpu (cpu, qemu_plugin_vcpu_init__async,RUN_ON_CPU_NULL ); +} +void qemu_plugin_vcpu_exit_hook (CPUState *cpu) {bool success;
Signature · 1 line
2.41.0
Quoted 7 lines · Philippe Mathieu-Daudé
Philippe Mathieu-Daudé (6):
system/runstate: Remove unused 'qemu/plugin.h' header accel/tcg: Move common declarations to 'internal-common.h'
accel: Clarify accel_cpu_common_[un]realize() use unassigned vCPU
accel: Introduce accel_cpu_common_[un]realize_assigned() handlers
accel: Restrict TCG plugin (un)registration to TCG accel accel/tcg: Moveqemu_plugin_vcpu_init__async()to plugins/
r~
On 5/28/24 07:59, Philippe Mathieu-Daudé wrote: > Philippe Mathieu-Daudé (6):
Quoted 7 lines
system/runstate: Remove unused '
qemu/plugin.h' header accel/tcg: Move common declarations to 'internal-common.h'
accel: Clarify accel_cpu_common_[un]realize() use unassigned vCPU
accel: Introduce accel_cpu_common_[un]realize_assigned() handlers
accel: Restrict TCG plugin (un)registration to TCG accel accel/tcg: Moveqemu_plugin_vcpu_init__async()to plugins/