cpus: Cleanups around cpu running state changes
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
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
@@ -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
Re: [PATCH 1/4] cpus: Only resume halted CPUs
Quoted 12 lines · Philippe Mathieu-Daudé
Avoid kicking running CPUs, trying to resume them.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- system/cpus.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)system/cpus.c +0
-0
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.
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
@@ -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 _WIN32int err =pthread_kill (cpu->thread->thread,SIG_IPI );
Signature · 1 line
2.51.0
Re: [PATCH 2/4] cpus: Access CPUState::thread_kicked atomically
Quoted 29 lines · Philippe Mathieu-Daudé
cpus_kick_thread()is called viacpu_exit()->qemu_cpu_kick(), and also viagdb_syscall_handling(). Access the CPUState field using atomic accesses. See commit8ac2ca02744("accel: use atomic accesses forexit_request") for rationale.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- system/cpus.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)system/cpus.c +2
-3@@ -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 _WIN32int err =pthread_kill (cpu->thread->thread,SIG_IPI ); --
Quoted 2 lines
2.51.0
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").
--- target/arm/hvf/hvf.c | 2 ++ target/i386/hvf/hvf.c | 2 ++ 2 files changed, 4 insertions(+)
target/arm/hvf/hvf.c +2 -0
@@ -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
@@ -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
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.Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
Could be related to the dubiousthread_kickedaccess andpselect()inhvf_wait_for_ipi()(commit219c101fa7f"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@@ -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@@ -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 );
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
@@ -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
Re: [PATCH 4/4] accel/tcg: Use cpu_is_stopped() helper to access CPUState::stopped
Quoted 10 lines · Philippe Mathieu-Daudé
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- accel/tcg/tcg-accel-ops-rr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)accel/tcg/tcg-accel-ops-rr.c +0
-0
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~