Skip to contentSkip to conversation

accel: Restrict tcg_exec_[un]realizefn() to TCG

PATCH In review Philippe Mathieu-Daudé 3 participants 10 messages Oct 3, 2023 KVM

lore
Share & tools
lore
#0 lore raw

v2 cover accel: Restrict tcg_exec_[un]realizefn() to TCG

Since v1:
- Use 'target'/'common' in function names (Claudio)
- Added Claudio R-b tags

From v1:
- Add missing accel_cpu_common_unrealize()
- Add AccelClass::cpu_common_[un]realize handlers
- Use tcg_exec_[un]realizefn as AccelClass handlers

Philippe Mathieu-Daudé (7):
accel: Rename accel_cpu_realizefn() -> accel_cpu_realize()
accel: Rename AccelCPUClass::cpu_realizefn() -> cpu_target_realize()
accel: Rename accel_cpu_realize() -> accel_cpu_common_realize()
accel: Introduce accel_cpu_common_unrealize() stub
accel: Declare AccelClass::cpu_common_[un]realize() handlers accel/tcg: Have tcg_exec_realizefn() return a boolean accel/tcg: Restrict tcg_exec_[un]realizefn() to TCG

 accel/tcg/internal.h        |  3 +++
 include/exec/cpu-all.h      |  2 --
 include/hw/core/accel-cpu.h |  2 +-
 include/qemu/accel.h        | 12 ++++++++++--
 accel/accel-common.c        | 27 ++++++++++++++++++++++++---
 accel/tcg/cpu-exec.c        |  4 +++-
 accel/tcg/tcg-all.c         |  2 ++
 cpu.c                       | 13 +++----------
 target/i386/hvf/hvf-cpu.c   |  2 +-
 target/i386/kvm/kvm-cpu.c   |  4 ++--
 target/i386/tcg/tcg-cpu.c   |  2 +-
 11 files changed, 50 insertions(+), 23 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH v2 1/7 accel: Rename accel_cpu_realizefn() -> accel_cpu_realize()

We use the '*fn' suffix for handlers, this is a public method. Drop the suffix.

---
 include/qemu/accel.h      | 4 ++--
 accel/accel-common.c      | 2 +-
 cpu.c                     | 2 +-
 target/i386/kvm/kvm-cpu.c | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)
include/qemu/accel.h +2 -2
index e84db2e3e5..cb64a07b84 100644
@@ -90,11 +90,11 @@ void accel_setup_post(MachineState *ms);
 void accel_cpu_instance_init(CPUState *cpu);
 
 /**
- * accel_cpu_realizefn:
+ * accel_cpu_realize:
  * @cpu: The CPU that needs to call accel-specific cpu realization.
  * @errp: currently unused.
  */
-bool accel_cpu_realizefn(CPUState *cpu, Error **errp);
+bool accel_cpu_realize(CPUState *cpu, Error **errp);
 
 /**
  * accel_supported_gdbstub_sstep_flags:
accel/accel-common.c +1 -1
index df72cc989a..b953855e8b 100644
@@ -119,7 +119,7 @@ void accel_cpu_instance_init(CPUState *cpu)
     }
 }
 
-bool accel_cpu_realizefn(CPUState *cpu, Error **errp)
+bool accel_cpu_realize(CPUState *cpu, Error **errp)
 {
     CPUClass *cc = CPU_GET_CLASS(cpu);
 
cpu.c +1 -1
index 0769b0b153..61c9760e62 100644
@@ -136,7 +136,7 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp)
     /* cache the cpu class for the hotpath */
     cpu->cc = CPU_GET_CLASS(cpu);
 
-    if (!accel_cpu_realizefn(cpu, errp)) {
+    if (!accel_cpu_realize(cpu, errp)) {
         return;
     }
 
target/i386/kvm/kvm-cpu.c +1 -1
index 7237378a7d..4474689f81 100644
@@ -35,7 +35,7 @@ static bool kvm_cpu_realizefn(CPUState *cs, Error **errp)
      * x86_cpu_realize():
      *  -> x86_cpu_expand_features()
      *  -> cpu_exec_realizefn():
-     *            -> accel_cpu_realizefn()
+     *            -> accel_cpu_realize()
      *               kvm_cpu_realizefn() -> host_cpu_realizefn()
      *  -> check/update ucode_rev, phys_bits, mwait
      */
Signature · 1 line
2.41.0
#2 lore raw

PATCH v2 2/7 accel: Rename AccelCPUClass::cpu_realizefn() -> cpu_target_realize()

In reply to Philippe Mathieu-Daudé

The AccelCPUClass::cpu_realizefn handler is meant for target specific code, rename it using '_target_' to emphasis it.

---
 include/hw/core/accel-cpu.h | 2 +-
 accel/accel-common.c        | 4 ++--
 target/i386/hvf/hvf-cpu.c   | 2 +-
 target/i386/kvm/kvm-cpu.c   | 2 +-
 target/i386/tcg/tcg-cpu.c   | 2 +-
 5 files changed, 6 insertions(+), 6 deletions(-)
include/hw/core/accel-cpu.h +1 -1
index 5dbfd79955..24dad45ab9 100644
@@ -32,7 +32,7 @@ typedef struct AccelCPUClass {
 
     void (*cpu_class_init)(CPUClass *cc);
     void (*cpu_instance_init)(CPUState *cpu);
-    bool (*cpu_realizefn)(CPUState *cpu, Error **errp);
+    bool (*cpu_target_realize)(CPUState *cpu, Error **errp);
 } AccelCPUClass;
 
 #endif /* ACCEL_CPU_H */
accel/accel-common.c +2 -2
index b953855e8b..2e30b9d8f0 100644
@@ -123,8 +123,8 @@ bool accel_cpu_realize(CPUState *cpu, Error **errp)
 {
     CPUClass *cc = CPU_GET_CLASS(cpu);
 
-    if (cc->accel_cpu && cc->accel_cpu->cpu_realizefn) {
-        return cc->accel_cpu->cpu_realizefn(cpu, errp);
+    if (cc->accel_cpu && cc->accel_cpu->cpu_target_realize) {
+        return cc->accel_cpu->cpu_target_realize(cpu, errp);
     }
     return true;
 }
target/i386/hvf/hvf-cpu.c +1 -1
index 333db59898..bb0da3947a 100644
@@ -77,7 +77,7 @@ static void hvf_cpu_accel_class_init(ObjectClass *oc, void *data)
 {
     AccelCPUClass *acc = ACCEL_CPU_CLASS(oc);
 
-    acc->cpu_realizefn = host_cpu_realizefn;
+    acc->cpu_target_realize = host_cpu_realizefn;
     acc->cpu_instance_init = hvf_cpu_instance_init;
 }
 
target/i386/kvm/kvm-cpu.c +1 -1
index 4474689f81..9a5e105e4e 100644
@@ -190,7 +190,7 @@ static void kvm_cpu_accel_class_init(ObjectClass *oc, void *data)
 {
     AccelCPUClass *acc = ACCEL_CPU_CLASS(oc);
 
-    acc->cpu_realizefn = kvm_cpu_realizefn;
+    acc->cpu_target_realize = kvm_cpu_realizefn;
     acc->cpu_instance_init = kvm_cpu_instance_init;
 }
 static const TypeInfo kvm_cpu_accel_type_info = {
target/i386/tcg/tcg-cpu.c +1 -1
index b942c306d6..5c3a508ddc 100644
@@ -163,7 +163,7 @@ static void tcg_cpu_accel_class_init(ObjectClass *oc, void *data)
     AccelCPUClass *acc = ACCEL_CPU_CLASS(oc);
 
 #ifndef CONFIG_USER_ONLY
-    acc->cpu_realizefn = tcg_cpu_realizefn;
+    acc->cpu_target_realize = tcg_cpu_realizefn;
 #endif /* CONFIG_USER_ONLY */
 
     acc->cpu_class_init = tcg_cpu_class_init;
Signature · 1 line
2.41.0
#3 lore raw

PATCH v2 3/7 accel: Rename accel_cpu_realize() -> accel_cpu_common_realize()

In reply to Philippe Mathieu-Daudé

accel_cpu_realize() is a generic function working with CPUs from any target. Rename it using '_common_' to emphasis it is not target specific.

---
 include/qemu/accel.h      | 4 ++--
 accel/accel-common.c      | 2 +-
 cpu.c                     | 2 +-
 target/i386/kvm/kvm-cpu.c | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)
include/qemu/accel.h +2 -2
index cb64a07b84..898159c001 100644
@@ -90,11 +90,11 @@ void accel_setup_post(MachineState *ms);
 void accel_cpu_instance_init(CPUState *cpu);
 
 /**
- * accel_cpu_realize:
+ * accel_cpu_common_realize:
  * @cpu: The CPU that needs to call accel-specific cpu realization.
  * @errp: currently unused.
  */
-bool accel_cpu_realize(CPUState *cpu, Error **errp);
+bool accel_cpu_common_realize(CPUState *cpu, Error **errp);
 
 /**
  * accel_supported_gdbstub_sstep_flags:
accel/accel-common.c +1 -1
index 2e30b9d8f0..53cf08a89a 100644
@@ -119,7 +119,7 @@ void accel_cpu_instance_init(CPUState *cpu)
     }
 }
 
-bool accel_cpu_realize(CPUState *cpu, Error **errp)
+bool accel_cpu_common_realize(CPUState *cpu, Error **errp)
 {
     CPUClass *cc = CPU_GET_CLASS(cpu);
 
cpu.c +1 -1
index 61c9760e62..1e2649a706 100644
@@ -136,7 +136,7 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp)
     /* cache the cpu class for the hotpath */
     cpu->cc = CPU_GET_CLASS(cpu);
 
-    if (!accel_cpu_realize(cpu, errp)) {
+    if (!accel_cpu_common_realize(cpu, errp)) {
         return;
     }
 
target/i386/kvm/kvm-cpu.c +1 -1
index 9a5e105e4e..56c72f3c45 100644
@@ -35,7 +35,7 @@ static bool kvm_cpu_realizefn(CPUState *cs, Error **errp)
      * x86_cpu_realize():
      *  -> x86_cpu_expand_features()
      *  -> cpu_exec_realizefn():
-     *            -> accel_cpu_realize()
+     *            -> accel_cpu_common_realize()
      *               kvm_cpu_realizefn() -> host_cpu_realizefn()
      *  -> check/update ucode_rev, phys_bits, mwait
      */
Signature · 1 line
2.41.0
#4 lore raw

PATCH v2 4/7 accel: Introduce accel_cpu_common_unrealize() stub

In reply to Philippe Mathieu-Daudé

Prepare the stub for parity with accel_cpu_common_realize().

---
 include/qemu/accel.h | 6 ++++++
 accel/accel-common.c | 4 ++++
 cpu.c                | 4 +++-
 3 files changed, 13 insertions(+), 1 deletion(-)
include/qemu/accel.h +6 -0
index 898159c001..446153b145 100644
@@ -96,6 +96,12 @@ void accel_cpu_instance_init(CPUState *cpu);
  */
 bool accel_cpu_common_realize(CPUState *cpu, Error **errp);
 
+/**
+ * accel_cpu_common_unrealize:
+ * @cpu: The CPU that needs to call accel-specific cpu unrealization.
+ */
+void accel_cpu_common_unrealize(CPUState *cpu);
+
 /**
  * accel_supported_gdbstub_sstep_flags:
  *
accel/accel-common.c +4 -0
index 53cf08a89a..e9548eac29 100644
@@ -129,6 +129,10 @@ bool accel_cpu_common_realize(CPUState *cpu, Error **errp)
     return true;
 }
 
+void accel_cpu_common_unrealize(CPUState *cpu)
+{
+}
+
 int accel_supported_gdbstub_sstep_flags(void)
 {
     AccelState *accel = current_accel();
cpu.c +3 -1
index 1e2649a706..2a1eff948b 100644
@@ -187,8 +187,10 @@ void cpu_exec_unrealizefn(CPUState *cpu)
     cpu_list_remove(cpu);
     /*
      * Now that the vCPU has been removed from the RCU list, we can call
-     * tcg_exec_unrealizefn, which may free fields using call_rcu.
+     * tcg_exec_unrealizefn and
+     * accel_cpu_common_unrealize, which may free fields using call_rcu.
      */
+    accel_cpu_common_unrealize(cpu);
     if (tcg_enabled()) {
         tcg_exec_unrealizefn(cpu);
     }
Signature · 1 line
2.41.0
#5 lore raw

PATCH v2 5/7 accel: Declare AccelClass::cpu_common_[un]realize() handlers

In reply to Philippe Mathieu-Daudé

Currently accel_cpu_realize() only performs target-specific realization. Introduce the cpu_common_[un]realize fields in the base AccelClass to be able to perform target-agnostic [un]realization of vCPUs.

---
 include/qemu/accel.h |  2 ++
 accel/accel-common.c | 21 +++++++++++++++++++--
 2 files changed, 21 insertions(+), 2 deletions(-)
include/qemu/accel.h +2 -0
index 446153b145..972a849a2b 100644
@@ -43,6 +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);
 
     /* gdbstub related hooks */
     int (*gdbstub_supported_sstep_flags)(void);
accel/accel-common.c +19 -2
index e9548eac29..11d74b4ad7 100644
@@ -122,15 +122,32 @@ void accel_cpu_instance_init(CPUState *cpu)
 bool accel_cpu_common_realize(CPUState *cpu, Error **errp)
 {
     CPUClass *cc = CPU_GET_CLASS(cpu);
+    AccelState *accel = current_accel();
+    AccelClass *acc = ACCEL_GET_CLASS(accel);
 
-    if (cc->accel_cpu && cc->accel_cpu->cpu_target_realize) {
-        return cc->accel_cpu->cpu_target_realize(cpu, errp);
+    /* target specific realization */
+    if (cc->accel_cpu && cc->accel_cpu->cpu_target_realize
+        && !cc->accel_cpu->cpu_target_realize(cpu, errp)) {
+        return false;
     }
+
+    /* generic realization */
+    if (acc->cpu_common_realize && !acc->cpu_common_realize(cpu, errp)) {
+        return false;
+    }
+
     return true;
 }
 
 void accel_cpu_common_unrealize(CPUState *cpu)
 {
+    AccelState *accel = current_accel();
+    AccelClass *acc = ACCEL_GET_CLASS(accel);
+
+    /* generic unrealization */
+    if (acc->cpu_common_unrealize) {
+        acc->cpu_common_unrealize(cpu);
+    }
 }
 
 int accel_supported_gdbstub_sstep_flags(void)
Signature · 1 line
2.41.0
#6 lore raw

PATCH v2 6/7 accel/tcg: Have tcg_exec_realizefn() return a boolean

In reply to Philippe Mathieu-Daudé

Following the example documented since commit e3fe3988d7 ("error:
Document Error API usage rules"), have tcg_exec_realizefn() return a boolean indicating whether an error is set or not.

---
 include/exec/cpu-all.h | 2 +-
 accel/tcg/cpu-exec.c   | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)
include/exec/cpu-all.h +1 -1
index c2c62160c6..1e5c530ee1 100644
@@ -422,7 +422,7 @@ void dump_exec_info(GString *buf);
 
 /* accel/tcg/cpu-exec.c */
 int cpu_exec(CPUState *cpu);
-void tcg_exec_realizefn(CPUState *cpu, Error **errp);
+bool tcg_exec_realizefn(CPUState *cpu, Error **errp);
 void tcg_exec_unrealizefn(CPUState *cpu);
 
 /**
accel/tcg/cpu-exec.c +3 -1
index c724e8b6f1..60f1986b85 100644
@@ -1088,7 +1088,7 @@ int cpu_exec(CPUState *cpu)
     return ret;
 }
 
-void tcg_exec_realizefn(CPUState *cpu, Error **errp)
+bool tcg_exec_realizefn(CPUState *cpu, Error **errp)
 {
     static bool tcg_target_initialized;
     CPUClass *cc = CPU_GET_CLASS(cpu);
@@ -1104,6 +1104,8 @@ void tcg_exec_realizefn(CPUState *cpu, Error **errp)
     tcg_iommu_init_notifier_list(cpu);
 #endif /* !CONFIG_USER_ONLY */
     /* qemu_plugin_vcpu_init_hook delayed until cpu_index assigned. */
+
+    return true;
 }
 
 /* undo the initializations in reverse order */
Signature · 1 line
2.41.0
#7 lore raw

PATCH v2 7/7 accel/tcg: Restrict tcg_exec_[un]realizefn() to TCG

In reply to Philippe Mathieu-Daudé

We don't need to expose these TCG-specific methods to the whole code base. Register them as AccelClass handlers, they will be called by the generic accel_cpu_[un]realize() methods.

---
 accel/tcg/internal.h   | 3 +++
 include/exec/cpu-all.h | 2 --
 accel/tcg/tcg-all.c    | 2 ++
 cpu.c                  | 9 ---------
 4 files changed, 5 insertions(+), 11 deletions(-)
accel/tcg/internal.h +3 -0
index e8cbbde581..57ab397df1 100644
@@ -80,6 +80,9 @@ 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)
 {
include/exec/cpu-all.h +0 -2
index 1e5c530ee1..230525ebf7 100644
@@ -422,8 +422,6 @@ void dump_exec_info(GString *buf);
 
 /* accel/tcg/cpu-exec.c */
 int cpu_exec(CPUState *cpu);
-bool tcg_exec_realizefn(CPUState *cpu, Error **errp);
-void tcg_exec_unrealizefn(CPUState *cpu);
 
 /**
  * cpu_set_cpustate_pointers(cpu)
accel/tcg/tcg-all.c +2 -0
index 03dfd67e9e..8ab873a5ab 100644
@@ -227,6 +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->allowed = &tcg_allowed;
     ac->gdbstub_supported_sstep_flags = tcg_gdbstub_supported_sstep_flags;
 
cpu.c +0 -9
index 2a1eff948b..658d179582 100644
@@ -140,11 +140,6 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp)
         return;
     }
 
-    /* NB: errp parameter is unused currently */
-    if (tcg_enabled()) {
-        tcg_exec_realizefn(cpu, errp);
-    }
-
     /* Wait until cpu initialization complete before exposing cpu. */
     cpu_list_add(cpu);
 
@@ -187,13 +182,9 @@ void cpu_exec_unrealizefn(CPUState *cpu)
     cpu_list_remove(cpu);
     /*
      * Now that the vCPU has been removed from the RCU list, we can call
-     * tcg_exec_unrealizefn and
      * accel_cpu_common_unrealize, which may free fields using call_rcu.
      */
     accel_cpu_common_unrealize(cpu);
-    if (tcg_enabled()) {
-        tcg_exec_unrealizefn(cpu);
-    }
 }
 
 /*
Signature · 1 line
2.41.0
Reviewed-by Tested-by on the v2 series
#8 lore raw
In reply to Philippe Mathieu-Daudé

Nice. I build-tested and make checked this as well,

Quoted 31 lines · Philippe Mathieu-Daudé

Since v1:
- Use 'target'/'common' in function names (Claudio)
- Added Claudio R-b tags

From v1:
- Add missing accel_cpu_common_unrealize()
- Add AccelClass::cpu_common_[un]realize handlers
- Use tcg_exec_[un]realizefn as AccelClass handlers

Philippe Mathieu-Daudé (7):
accel: Rename accel_cpu_realizefn() -> accel_cpu_realize()
accel: Rename AccelCPUClass::cpu_realizefn() -> cpu_target_realize()
accel: Rename accel_cpu_realize() -> accel_cpu_common_realize()
accel: Introduce accel_cpu_common_unrealize() stub
accel: Declare AccelClass::cpu_common_[un]realize() handlers accel/tcg: Have tcg_exec_realizefn() return a boolean accel/tcg: Restrict tcg_exec_[un]realizefn() to TCG

 accel/tcg/internal.h        |  3 +++
 include/exec/cpu-all.h      |  2 --
 include/hw/core/accel-cpu.h |  2 +-
 include/qemu/accel.h        | 12 ++++++++++--
 accel/accel-common.c        | 27 ++++++++++++++++++++++++---
 accel/tcg/cpu-exec.c        |  4 +++-
 accel/tcg/tcg-all.c         |  2 ++
 cpu.c                       | 13 +++----------
 target/i386/hvf/hvf-cpu.c   |  2 +-
 target/i386/kvm/kvm-cpu.c   |  4 ++--
 target/i386/tcg/tcg-cpu.c   |  2 +-
 11 files changed, 50 insertions(+), 23 deletions(-)
#9 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 8 lines · Philippe Mathieu-Daudé

Since v1:
- Use 'target'/'common' in function names (Claudio)
- Added Claudio R-b tags

From v1:
- Add missing accel_cpu_common_unrealize()
- Add AccelClass::cpu_common_[un]realize handlers
- Use tcg_exec_[un]realizefn as AccelClass handlers

Thanks, queued to tcg-next, replacing v1 that I queued before my holiday. :-)

r~