Skip to contentSkip to conversation

plugins: Few debugging cleanups

PATCH Applied Philippe Mathieu-Daudé 3 participants 10 messages Jun 6, 2024 – Jul 4, 2024

lore
Share & tools
lore
#0 lore raw

cover plugins: Few debugging cleanups

- Assert cpu_index is assigned in INIT/EXIT hooks
- Free cpu->plugin_state
- Restrict qemu_plugin_vcpu_init__async() to plugins/

Philippe Mathieu-Daudé (3):
plugins: Ensure vCPU index is assigned in init/exit hooks
plugins: Free CPUPluginState before destroying vCPU state accel/tcg: Move qemu_plugin_vcpu_init__async() to plugins/

 include/qemu/plugin.h |  3 +++
 hw/core/cpu-common.c  | 14 ++++++--------
 plugins/core.c        | 10 +++++++++-
 3 files changed, 18 insertions(+), 9 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/3 plugins: Ensure vCPU index is assigned in init/exit hooks

Since vCPUs are hashed by their index, this index can't be uninitialized (UNASSIGNED_CPU_INDEX).

---
 plugins/core.c | 2 ++
 1 file changed, 2 insertions(+)
plugins/core.c +2 -0
index badede28cf..d339b3db4d 100644
@@ -245,6 +245,7 @@ void qemu_plugin_vcpu_init_hook(CPUState *cpu)
 {
     bool success;
 
+    assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
     qemu_rec_mutex_lock(&plugin.lock);
     plugin.num_vcpus = MAX(plugin.num_vcpus, cpu->cpu_index + 1);
     plugin_cpu_update__locked(&cpu->cpu_index, NULL, NULL);
@@ -263,6 +264,7 @@ void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
 
     plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_EXIT);
 
+    assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
     qemu_rec_mutex_lock(&plugin.lock);
     success = g_hash_table_remove(plugin.cpu_ht, &cpu->cpu_index);
     g_assert(success);
Signature · 1 line
2.41.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] plugins: Ensure vCPU index is assigned in init/exit hooks

Quoted 21 lines · Philippe Mathieu-Daudé

Since vCPUs are hashed by their index, this index can't be uninitialized (UNASSIGNED_CPU_INDEX).

---
  plugins/core.c | 2 ++
  1 file changed, 2 insertions(+)
plugins/core.c +1 -0
index badede28cf..d339b3db4d 100644
@@ -245,6 +245,7 @@ void qemu_plugin_vcpu_init_hook(CPUState *cpu)
  {
      bool success;
 
+    assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
      qemu_rec_mutex_lock(&plugin.lock);
      plugin.num_vcpus = MAX(plugin.num_vcpus, cpu->cpu_index + 1);
      plugin_cpu_update__locked(&cpu->cpu_index, NULL, NULL);

plugins/core.c void qemu_plugin_vcpu_exit_hook(CPUState *cpu) · line 270

Quoted 7 lines
plugins/core.c +1 -0
@@ -263,6 +264,7 @@ void qemu_plugin_vcpu_exit_hook(CPUState *cpu)
 
      plugin_vcpu_cb__simple(cpu, QEMU_PLUGIN_EV_VCPU_EXIT);
 
+    assert(cpu->cpu_index != UNASSIGNED_CPU_INDEX);
      qemu_rec_mutex_lock(&plugin.lock);
      success = g_hash_table_remove(plugin.cpu_ht, &cpu->cpu_index);
      g_assert(success);
#3 lore raw

PATCH 2/3 plugins: Free CPUPluginState before destroying vCPU state

In reply to Philippe Mathieu-Daudé

cpu::plugin_state is allocated in cpu_common_initfn() when the vCPU state is created. Release it in cpu_common_finalize() when we are done.

---
 include/qemu/plugin.h | 3 +++
 hw/core/cpu-common.c  | 5 +++++
 2 files changed, 8 insertions(+)
include/qemu/plugin.h +3 -0
index bc5aef979e..af5f9db469 100644
@@ -149,6 +149,9 @@ struct CPUPluginState {
 
 /**
  * qemu_plugin_create_vcpu_state: allocate plugin state
+ *
+ * The returned data must be released with g_free()
+ * when no longer required.
  */
 CPUPluginState *qemu_plugin_create_vcpu_state(void);
 
hw/core/cpu-common.c +5 -0
index bf1a7b8892..cd15402552 100644
@@ -283,6 +283,11 @@ static void cpu_common_finalize(Object *obj)
 {
     CPUState *cpu = CPU(obj);
 
+#ifdef CONFIG_PLUGIN
+    if (tcg_enabled()) {
+        g_free(cpu->plugin_state);
+    }
+#endif
     g_array_free(cpu->gdb_regs, TRUE);
     qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
     qemu_mutex_destroy(&cpu->work_mutex);
Signature · 1 line
2.41.0
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] plugins: Free CPUPluginState before destroying vCPU state

Quoted 30 lines · Philippe Mathieu-Daudé

cpu::plugin_state is allocated in cpu_common_initfn() when the vCPU state is created. Release it in cpu_common_finalize() when we are done.

---
  include/qemu/plugin.h | 3 +++
  hw/core/cpu-common.c  | 5 +++++
  2 files changed, 8 insertions(+)
include/qemu/plugin.h +3 -0
index bc5aef979e..af5f9db469 100644
@@ -149,6 +149,9 @@ struct CPUPluginState {
 
  /**
   * qemu_plugin_create_vcpu_state: allocate plugin state
+ *
+ * The returned data must be released with g_free()
+ * when no longer required.
   */
  CPUPluginState *qemu_plugin_create_vcpu_state(void);
 
hw/core/cpu-common.c +0 -0
index bf1a7b8892..cd15402552 100644
@@ -283,6 +283,11 @@ static void cpu_common_finalize(Object *obj)
  {

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

Quoted 10 lines
hw/core/cpu-common.c +5 -0
      CPUState *cpu = CPU(obj);
 
+#ifdef CONFIG_PLUGIN
+    if (tcg_enabled()) {
+        g_free(cpu->plugin_state);
+    }
+#endif
      g_array_free(cpu->gdb_regs, TRUE);
      qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
      qemu_mutex_destroy(&cpu->work_mutex);

To ensure I get it right, order of cpu init/deinit is:
- init
- realize
- unrealize
- finalize
Is that correct?

#5 lore raw

Re: [PATCH 2/3] plugins: Free CPUPluginState before destroying vCPU state

Quoted 37 lines · Pierrick Bouvier
Quoted 36 lines · Philippe Mathieu-Daudé

cpu::plugin_state is allocated in cpu_common_initfn() when the vCPU state is created. Release it in cpu_common_finalize() when we are done.

---
  include/qemu/plugin.h | 3 +++
  hw/core/cpu-common.c  | 5 +++++

  2 files changed, 8 insertions(+)
include/qemu/plugin.h +3 -0
index bc5aef979e..af5f9db469 100644
@@ -149,6 +149,9 @@ struct CPUPluginState {
   /**
    * qemu_plugin_create_vcpu_state: allocate plugin state
+ *
+ * The returned data must be released with g_free()
+ * when no longer required.
    */
   CPUPluginState *qemu_plugin_create_vcpu_state(void);
hw/core/cpu-common.c +5 -0
index bf1a7b8892..cd15402552 100644
@@ -283,6 +283,11 @@ static void cpu_common_finalize(Object *obj)
   {
       CPUState *cpu = CPU(obj);
+#ifdef CONFIG_PLUGIN
+    if (tcg_enabled()) {
+        g_free(cpu->plugin_state);
+    }
+#endif
       g_array_free(cpu->gdb_regs, TRUE);
       qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
Quoted 8 lines

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

Quoted 1 line
hw/core/cpu-common.c +0 -0
       qemu_mutex_destroy(&cpu->work_mutex);

To ensure I get it right, order of cpu init/deinit is:
- init
- realize
- unrealize
- finalize
Is that correct?

Yes, this is valid for all QDev (CPU is based on it).

+ init: allocate state, expose configurable properties . user configure properties
+ realize: consume properties to tune the object
+ reset: set default values
. object is used
+ unrealize: undo stuff from realize because the object might be realized again (unplug - plug)
+ finalize: release resources

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

Quoted 1 line

Thanks!

#6 lore raw

Re: [PATCH 2/3] plugins: Free CPUPluginState before destroying vCPU state

Quoted 58 lines · Philippe Mathieu-Daudé
Quoted 37 lines · Pierrick Bouvier
Quoted 36 lines · Philippe Mathieu-Daudé

cpu::plugin_state is allocated in cpu_common_initfn() when the vCPU state is created. Release it in cpu_common_finalize() when we are done.

---
   include/qemu/plugin.h | 3 +++
   hw/core/cpu-common.c  | 5 +++++
   2 files changed, 8 insertions(+)
include/qemu/plugin.h +3 -0
index bc5aef979e..af5f9db469 100644
@@ -149,6 +149,9 @@ struct CPUPluginState {
   /**
    * qemu_plugin_create_vcpu_state: allocate plugin state
+ *
+ * The returned data must be released with g_free()
+ * when no longer required.
    */
   CPUPluginState *qemu_plugin_create_vcpu_state(void);
hw/core/cpu-common.c +5 -0
index bf1a7b8892..cd15402552 100644
@@ -283,6 +283,11 @@ static void cpu_common_finalize(Object *obj)
   {
       CPUState *cpu = CPU(obj);
+#ifdef CONFIG_PLUGIN
+    if (tcg_enabled()) {
+        g_free(cpu->plugin_state);
+    }
+#endif
       g_array_free(cpu->gdb_regs, TRUE);
       qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
Quoted 8 lines

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

Quoted 1 line
hw/core/cpu-common.c +0 -0
       qemu_mutex_destroy(&cpu->work_mutex);

To ensure I get it right, order of cpu init/deinit is:
- init
- realize
- unrealize
- finalize
Is that correct?

Yes, this is valid for all QDev (CPU is based on it).

+ init: allocate state, expose configurable properties . user configure properties
+ realize: consume properties to tune the object
+ reset: set default values
. object is used
+ unrealize: undo stuff from realize because the object might be realized again (unplug - plug)
+ finalize: release resources

Quoted 6 lines

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

Quoted 1 line

Thanks!

Thanks, it definitely have its place in the official documentation, if you feel like adding it.

#7 lore raw

PATCH 3/3 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.

---
 hw/core/cpu-common.c | 9 +--------
 plugins/core.c       | 8 +++++++-
 2 files changed, 8 insertions(+), 9 deletions(-)
hw/core/cpu-common.c +1 -8
index cd15402552..79fcc0b286 100644
@@ -192,13 +192,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);
@@ -274,7 +267,7 @@ static void cpu_common_initfn(Object *obj)
 #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);
+        qemu_plugin_vcpu_init_hook(cpu);
     }
 #endif
 }
plugins/core.c +7 -1
index d339b3db4d..3dec3556c3 100644
@@ -241,7 +241,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;
 
@@ -258,6 +258,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
#8 lore raw
In reply to Philippe Mathieu-Daudé

Ping? (series reviewed)

Quoted 8 lines · Philippe Mathieu-Daudé

- Assert cpu_index is assigned in INIT/EXIT hooks
- Free cpu->plugin_state
- Restrict qemu_plugin_vcpu_init__async() to plugins/

Philippe Mathieu-Daudé (3):
plugins: Ensure vCPU index is assigned in init/exit hooks
plugins: Free CPUPluginState before destroying vCPU state accel/tcg: Move qemu_plugin_vcpu_init__async() to plugins/

Applied
#9 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 9 lines · Philippe Mathieu-Daudé

- Assert cpu_index is assigned in INIT/EXIT hooks
- Free cpu->plugin_state
- Restrict qemu_plugin_vcpu_init__async() to plugins/

Philippe Mathieu-Daudé (3):
plugins: Ensure vCPU index is assigned in init/exit hooks
plugins: Free CPUPluginState before destroying vCPU state accel/tcg: Move qemu_plugin_vcpu_init__async() to plugins/

Quoted 4 lines
 include/qemu/plugin.h |  3 +++
 hw/core/cpu-common.c  | 14 ++++++--------
 plugins/core.c        | 10 +++++++++-
 3 files changed, 18 insertions(+), 9 deletions(-)

Queued to plugins/next, thanks.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro