Skip to contentSkip to conversation

cpu: fix memleak of 'halt_cond' and 'thread'

PATCH Applied Matheus Tavares Bernardino 4 participants 4 messages Jun 12, 2024 – Jun 17, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpu: fix memleak of 'halt_cond' and 'thread'

Since a4c2735f35 (cpu: move Qemu[Thread|Cond] setup into common code, 2024-05-30) these fields are now allocated at cpu_common_initfn(). So let's make sure we also free them at cpu_common_finalize().

Furthermore, the code also frees these on round robin, but we missed 'halt_cond'.

---
 accel/tcg/tcg-accel-ops-rr.c | 1 +
 hw/core/cpu-common.c         | 3 +++
 2 files changed, 4 insertions(+)
accel/tcg/tcg-accel-ops-rr.c +1 -0
index 84c36c1450..48c38714bd 100644
@@ -329,6 +329,7 @@ void rr_start_vcpu_thread(CPUState *cpu)
         /* we share the thread, dump spare data */
         g_free(cpu->thread);
         qemu_cond_destroy(cpu->halt_cond);
+        g_free(cpu->halt_cond);
         cpu->thread = single_tcg_cpu_thread;
         cpu->halt_cond = single_tcg_halt_cond;
 
hw/core/cpu-common.c +3 -0
index bf1a7b8892..f131cde2c0 100644
@@ -286,6 +286,9 @@ static void cpu_common_finalize(Object *obj)
     g_array_free(cpu->gdb_regs, TRUE);
     qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
     qemu_mutex_destroy(&cpu->work_mutex);
+    qemu_cond_destroy(cpu->halt_cond);
+    g_free(cpu->halt_cond);
+    g_free(cpu->thread);
 }
 
 static int64_t cpu_common_get_arch_id(CPUState *cpu)
Signature · 1 line
2.37.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Matheus Tavares Bernardino

Since a4c2735f35 (cpu: move Qemu[Thread|Cond] setup into common code, 2024-05-30) these fields are now allocated at cpu_common_initfn(). So let's make sure we also free them at cpu_common_finalize().

Quoted 8 lines

Furthermore, the code also frees these on round robin, but we missed 'halt_cond'.

---
  accel/tcg/tcg-accel-ops-rr.c | 1 +
  hw/core/cpu-common.c         | 3 +++
  2 files changed, 4 insertions(+)
Reviewed-by on v1 1/1
#2 lore raw
In reply to Matheus Tavares Bernardino
Quoted 30 lines · Matheus Tavares Bernardino

Since a4c2735f35 (cpu: move Qemu[Thread|Cond] setup into common code, 2024-05-30) these fields are now allocated at cpu_common_initfn(). So let's make sure we also free them at cpu_common_finalize().

Furthermore, the code also frees these on round robin, but we missed 'halt_cond'.

---
  accel/tcg/tcg-accel-ops-rr.c | 1 +
  hw/core/cpu-common.c         | 3 +++
  2 files changed, 4 insertions(+)
accel/tcg/tcg-accel-ops-rr.c +1 -0
index 84c36c1450..48c38714bd 100644
@@ -329,6 +329,7 @@ void rr_start_vcpu_thread(CPUState *cpu)
          /* we share the thread, dump spare data */
          g_free(cpu->thread);
          qemu_cond_destroy(cpu->halt_cond);
+        g_free(cpu->halt_cond);
          cpu->thread = single_tcg_cpu_thread;
          cpu->halt_cond = single_tcg_halt_cond;
 
hw/core/cpu-common.c +0 -0
index bf1a7b8892..f131cde2c0 100644

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

Quoted 9 lines
hw/core/cpu-common.c +3 -0
@@ -286,6 +286,9 @@ static void cpu_common_finalize(Object *obj)
      g_array_free(cpu->gdb_regs, TRUE);
      qemu_lockcnt_destroy(&cpu->in_ioctl_lock);
      qemu_mutex_destroy(&cpu->work_mutex);
+    qemu_cond_destroy(cpu->halt_cond);
+    g_free(cpu->halt_cond);
+    g_free(cpu->thread);
  }
 
  static int64_t cpu_common_get_arch_id(CPUState *cpu)
#3 lore raw
In reply to Matheus Tavares Bernardino
Quoted 6 lines · Matheus Tavares Bernardino

Since a4c2735f35 (cpu: move Qemu[Thread|Cond] setup into common code, 2024-05-30) these fields are now allocated at cpu_common_initfn(). So let's make sure we also free them at cpu_common_finalize().

Furthermore, the code also frees these on round robin, but we missed 'halt_cond'.

Applied to trivial-patches, thanks!

/mjt