Skip to contentSkip to conversation

cpus: Cleanups around cpu running state changes

PATCH In review Philippe Mathieu-Daudé 3 participants 9 messages Sep 25, 2025 – Sep 29, 2025

lore
Share & tools
lore
#0 lore raw

cover cpus: Cleanups around cpu running state changes

Few cleanups following Paolo's recent "accel, cpus: clean up cpu->exit_request" work (now merged), extracted from my "split-accel" work.

Philippe Mathieu-Daudé (4):
cpus: Only resume halted CPUs
cpus: Access CPUState::thread_kicked atomically accel/hvf: Make async_safe_run_on_cpu() safe
accel/tcg: Use cpu_is_stopped() helper to access CPUState::stopped

 accel/tcg/tcg-accel-ops-rr.c | 2 +-
 system/cpus.c                | 8 +++++---
 target/arm/hvf/hvf.c         | 2 ++
 target/i386/hvf/hvf.c        | 2 ++
 4 files changed, 10 insertions(+), 4 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/4 cpus: Only resume halted CPUs

Avoid kicking running CPUs, trying to resume them.

---
 system/cpus.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
system/cpus.c +3 -1
index aa7bfcf56e5..6062226d4ac 100644
@@ -666,7 +666,9 @@ void resume_all_vcpus(void)
 
     qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
     CPU_FOREACH(cpu) {
-        cpu_resume(cpu);
+        if (cpu->halted) {
+            cpu_resume(cpu);
+        }
     }
 }
 
Signature · 1 line
2.51.0
#2 lore raw

Re: [PATCH 1/4] cpus: Only resume halted CPUs

Quoted 12 lines · Philippe Mathieu-Daudé

Avoid kicking running CPUs, trying to resume them.

---
  system/cpus.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
system/cpus.c +0 -0
index aa7bfcf56e5..6062226d4ac 100644

system/cpus.c void resume_all_vcpus(void) · line 674

Quoted 10 lines
system/cpus.c +3 -1
@@ -666,7 +666,9 @@ void resume_all_vcpus(void)
 
      qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
      CPU_FOREACH(cpu) {
-        cpu_resume(cpu);
+        if (cpu->halted) {
+            cpu_resume(cpu);
+        }
      }
  }
 

Please disregard this patch for now, as other changes (non included in this series) are required before this change is correct.

#3 lore raw

PATCH 2/4 cpus: Access CPUState::thread_kicked atomically

In reply to Philippe Mathieu-Daudé

cpus_kick_thread() is called via cpu_exit() -> qemu_cpu_kick(), and also via gdb_syscall_handling(). Access the CPUState field using atomic accesses. See commit 8ac2ca02744 ("accel: use atomic accesses for exit_request") for rationale.

---
 system/cpus.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
system/cpus.c +2 -2
index 6062226d4ac..818a8047198 100644
@@ -480,10 +480,10 @@ void qemu_process_cpu_events(CPUState *cpu)
 
 void cpus_kick_thread(CPUState *cpu)
 {
-    if (cpu->thread_kicked) {
+    if (qatomic_read(&cpu->thread_kicked)) {
         return;
     }
-    cpu->thread_kicked = true;
+    qatomic_set(&cpu->thread_kicked, true);
 
 #ifndef _WIN32
     int err = pthread_kill(cpu->thread->thread, SIG_IPI);
Signature · 1 line
2.51.0
Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] cpus: Access CPUState::thread_kicked atomically

Quoted 29 lines · Philippe Mathieu-Daudé

cpus_kick_thread() is called via cpu_exit() -> qemu_cpu_kick(), and also via gdb_syscall_handling(). Access the CPUState field using atomic accesses. See commit 8ac2ca02744 ("accel: use atomic accesses for exit_request") for rationale.

---
 system/cpus.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
system/cpus.c +2 -3
index 6062226d4ac..818a8047198 100644
@@ -480,10 +480,10 @@ void qemu_process_cpu_events(CPUState *cpu)
 
 void cpus_kick_thread(CPUState *cpu)
 {
-    if (cpu->thread_kicked) {
+    if (qatomic_read(&cpu->thread_kicked)) {
         return;
     }
-    cpu->thread_kicked = true;
+    qatomic_set(&cpu->thread_kicked, true);
 
 #ifndef _WIN32
     int err = pthread_kill(cpu->thread->thread, SIG_IPI);
--
Quoted 2 lines

2.51.0

#5 lore raw

PATCH 3/4 accel/hvf: Make async_safe_run_on_cpu() safe

In reply to Philippe Mathieu-Daudé

Wrap hv_vcpu_run() calls with cpu_exec_start/end() in order to have the main loop perform more exclusive sections while all vCPUs are quiescent.

---
Could be related to the dubious thread_kicked access and pselect() in hvf_wait_for_ipi() (commit 219c101fa7f "arm/hvf: Add a WFI handler").

Cc Peter Collingbourne <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Roman Bolshakov <[email protected]>
Cc Sergio Lopez <[email protected]>
---
 target/arm/hvf/hvf.c  | 2 ++
 target/i386/hvf/hvf.c | 2 ++
 2 files changed, 4 insertions(+)
target/arm/hvf/hvf.c +2 -0
index b77db99079e..377eb4bbc6b 100644
@@ -1927,7 +1927,9 @@ int hvf_vcpu_exec(CPUState *cpu)
     flush_cpu_state(cpu);
 
     bql_unlock();
+    cpu_exec_start(cpu);
     r = hv_vcpu_run(cpu->accel->fd);
+    cpu_exec_end(cpu);
     bql_lock();
     switch (r) {
     case HV_SUCCESS:
target/i386/hvf/hvf.c +2 -0
index 8445cadecec..913ad47f3d8 100644
@@ -749,8 +749,10 @@ int hvf_vcpu_exec(CPUState *cpu)
             return EXCP_HLT;
         }
 
+        cpu_exec_start(cpu);
         hv_return_t r = hv_vcpu_run_until(cpu->accel->fd, HV_DEADLINE_FOREVER);
         assert_hvf_ok(r);
+        cpu_exec_end(cpu);
 
         /* handle VMEXIT */
         uint64_t exit_reason = rvmcs(cpu->accel->fd, VMCS_EXIT_REASON);
Signature · 1 line
2.51.0
Reviewed-by on v1 3/4
#6 lore raw

Re: [PATCH 3/4] accel/hvf: Make async_safe_run_on_cpu() safe

Quoted 9 lines · Philippe Mathieu-Daudé

Wrap hv_vcpu_run() calls with cpu_exec_start/end() in order to have the main loop perform more exclusive sections while all vCPUs are quiescent.

---
Could be related to the dubious thread_kicked access and pselect() in hvf_wait_for_ipi() (commit 219c101fa7f "arm/hvf: Add a WFI handler").

Quoted 8 lines
Cc Peter Collingbourne <[email protected]>
Cc Alexander Graf <[email protected]>
Cc Roman Bolshakov <[email protected]>
Cc Sergio Lopez <[email protected]>
---
  target/arm/hvf/hvf.c  | 2 ++
  target/i386/hvf/hvf.c | 2 ++
  2 files changed, 4 insertions(+)

r~

Quoted 30 lines
target/arm/hvf/hvf.c +2 -0
index b77db99079e..377eb4bbc6b 100644
@@ -1927,7 +1927,9 @@ int hvf_vcpu_exec(CPUState *cpu)
      flush_cpu_state(cpu);
 
      bql_unlock();
+    cpu_exec_start(cpu);
      r = hv_vcpu_run(cpu->accel->fd);
+    cpu_exec_end(cpu);
      bql_lock();
      switch (r) {
      case HV_SUCCESS:
target/i386/hvf/hvf.c +2 -0
index 8445cadecec..913ad47f3d8 100644
@@ -749,8 +749,10 @@ int hvf_vcpu_exec(CPUState *cpu)
              return EXCP_HLT;
          }
 
+        cpu_exec_start(cpu);
          hv_return_t r = hv_vcpu_run_until(cpu->accel->fd, HV_DEADLINE_FOREVER);
          assert_hvf_ok(r);
+        cpu_exec_end(cpu);
 
          /* handle VMEXIT */
          uint64_t exit_reason = rvmcs(cpu->accel->fd, VMCS_EXIT_REASON);
#7 lore raw

PATCH 4/4 accel/tcg: Use cpu_is_stopped() helper to access CPUState::stopped

In reply to Philippe Mathieu-Daudé
---
 accel/tcg/tcg-accel-ops-rr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
accel/tcg/tcg-accel-ops-rr.c +1 -1
index 2fb46439971..f84342e0449 100644
@@ -197,7 +197,7 @@ static void *rr_cpu_thread_fn(void *arg)
     qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
     /* wait for initial kick-off after machine start */
-    while (first_cpu->stopped) {
+    while (cpu_is_stopped(first_cpu)) {
         qemu_cond_wait_bql(first_cpu->halt_cond);
 
         /* process any pending work */
Signature · 1 line
2.51.0
Reviewed-by on v1 4/4
#8 lore raw

Re: [PATCH 4/4] accel/tcg: Use cpu_is_stopped() helper to access CPUState::stopped

Quoted 10 lines · Philippe Mathieu-Daudé
---
  accel/tcg/tcg-accel-ops-rr.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
accel/tcg/tcg-accel-ops-rr.c +0 -0
index 2fb46439971..f84342e0449 100644

accel/tcg/tcg-accel-ops-rr.c static void *rr_cpu_thread_fn(void *arg) · line 203

Quoted 8 lines
accel/tcg/tcg-accel-ops-rr.c +1 -1
@@ -197,7 +197,7 @@ static void *rr_cpu_thread_fn(void *arg)
      qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
      /* wait for initial kick-off after machine start */
-    while (first_cpu->stopped) {
+    while (cpu_is_stopped(first_cpu)) {
          qemu_cond_wait_bql(first_cpu->halt_cond);
 
          /* process any pending work */

r~