accel/tcg: Make halt-to-exec transition explicit and remove cpu_exec_halt
v4 cover accel/tcg: Make halt-to-exec transition explicit and remove cpu_exec_halt
Missing review: 9
Since v3:
- Rebased (s390x, x86 and some ARM patches merged)
- RISC-V posted separately as [1]
- Better describe ARM PSCI power-state update issue
Since v2:
- Make all ARM halt_reason accesses atomic, including WFI/WFE helpers and halt-exit handling, and read event_register atomically.
- Keep the RISC-V changes focused on MIP: skip empty updates, add a shared accessor, and convert existing MIP reads to use it.
Since v1:
- Collected Richard R-b tags (sometimes commit description updated).
- Documented the BQL and synchronization requirements of has_work().
- Renamed the callbacks to poll_during_halt() and leaving_halt().
- Addressed Peter's comments by removing racy halt-work assertions, documenting the BQL contract, and explaining why x86 #DB delivery through do_interrupt_all() is safe without the BQL.
- Addressed Paolo's comments by making ARM PSCI state updates atomic, kicking remote vCPUs after power-state changes, and ensuring x86 APIC polling runs before cpu_has_work() with the BQL held only around APIC access.
- Sanitized s390_cpu_get_state() for atomic accesses without BQL.
This series was inspired by a previous thread on the list [2].
Refactor the CPU halt-to-execution transition logic in TCG as something more explicit and composable.
Core problem: TCGCPUOps::cpu_exec_halt callback mixed concerns, it checked for work, processed async events, and handled state transitions all in one place.
Solution: introduces two dedicated callbacks:
* poll_during_halt(): Process target-specific async events before checking for work. Called early in cpu_exec().
* leaving_halt(): Perform target-specific state updates when transitioning from halt to execution.
This separation allows the generic cpu_exec() code to orchestrate the flow cleanly (process events, check for work, transition state).
Only 2 targets need to be migrated (x86 and ARM) then we can remove the redundant cpu_exec_halt() hook.
The changes are expected to be purely refactoring with no functional impact.
Testing: Usual CI.
Based-on: <[email protected]>
[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/qemu-devel/CABgObfaDAhrpnVqQaKgG6uxPQe1YDu77YOsUEx9nqrN=3M2cGw@mail.gmail.com/
Philippe Mathieu-Daudé (9):
accel/tcg: Refactor poll-while-halted flow in cpu_exec() accel/tcg: Introduce poll_during_halt() and leaving_halt() target/arm: Split leaving-halt handling from arm_cpu_exec_halt() target/arm: Convert cpu_exec_halt() to leaving_halt() target/i386: Split APIC polling from x86_cpu_exec_halt() target/i386: Split leaving-halt handling from x86_cpu_exec_halt() target/i386: Use generic halt transition handlers accel/tcg: Remove the redundant cpu_exec_halt() hook target/arm: Kick vCPU after updating PSCI power state
include/accel/tcg/cpu-ops.h | 36 ++++++++++++++++------------- target/arm/internals.h | 3 --- target/i386/tcg/helper-tcg.h | 3 ++- target/loongarch/internals.h | 1 - target/riscv/internals.h | 5 ---- target/s390x/s390x-internal.h | 1 - accel/tcg/cpu-exec.c | 20 ++++++++++------ target/alpha/cpu.c | 1 - target/arm/arm-powerctl.c | 5 ++++ target/arm/cpu.c | 20 +++++++--------- target/arm/tcg/cpu-v7m.c | 1 - target/avr/cpu.c | 1 - target/hexagon/cpu.c | 1 - target/hppa/cpu.c | 1 - target/i386/tcg/system/seg_helper.c | 17 +++++++++----- target/i386/tcg/tcg-cpu.c | 3 ++- target/loongarch/cpu.c | 2 +- target/loongarch/tcg/tcg_cpu.c | 1 - target/m68k/cpu.c | 1 - target/microblaze/cpu.c | 1 - target/mips/cpu.c | 1 - target/or1k/cpu.c | 1 - target/ppc/cpu_init.c | 1 - target/riscv/cpu.c | 2 +- target/riscv/tcg/tcg-cpu.c | 1 - target/rx/cpu.c | 1 - target/s390x/cpu-system.c | 2 +- target/s390x/cpu.c | 1 - target/sh4/cpu.c | 1 - target/sparc/cpu.c | 1 - target/tricore/cpu.c | 1 - target/xtensa/cpu.c | 1 - 32 files changed, 64 insertions(+), 74 deletions(-)
Signature · 1 line
2.55.0
PATCH v4 1/9 accel/tcg: Refactor poll-while-halted flow in cpu_exec()
Invert cpu_handle_halt()'s logic to read in cpu_exec() as "if halted, poll for async events and find work; if no work, return". Rename as cpu_poll_while_halted().
--- accel/tcg/cpu-exec.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
accel/tcg/cpu-exec.c +4 -4
@@ -656,18 +656,18 @@ static inline void tb_add_jump(TranslationBlock *tb, int n, }#ifndef CONFIG_USER_ONLY -static inline bool cpu_handle_halt (CPUState *cpu) +static bool cpu_poll_while_halted (CPUState *cpu) {const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;bool leave_halt = tcg_ops->cpu_exec_halt (cpu);if (!leave_halt) { -return true; +return false; } cpu->halted =0 ;/* allow execution */ -return false; +return true; }#endif /* !CONFIG_USER_ONLY */
@@ -1029,7 +1029,7 @@ int cpu_exec(CPUState *cpu)#ifndef CONFIG_USER_ONLY if (cpu->halted) { -if (cpu_handle_halt (cpu)) { +if (!cpu_poll_while_halted (cpu)) {return EXCP_HALTED ; }assert (!cpu->halted);
Signature · 1 line
2.55.0
PATCH v4 2/9 accel/tcg: Introduce poll_during_halt() and leaving_halt()
In reply to Philippe Mathieu-DaudéSplit target-specific halt handling into an optional poll_during_halt() callback before cpu_has_work() and a leaving_halt() callback after work is found.
Keep cpu_exec_halt() as a legacy alternative for targets that have not migrated. Require each target to provide either cpu_exec_halt() or leaving_halt(), but not both.
--- include/accel/tcg/cpu-ops.h | 24 +++++++++++++++++++++++- accel/tcg/cpu-exec.c | 21 +++++++++++++++++---- 2 files changed, 40 insertions(+), 5 deletions(-)
include/accel/tcg/cpu-ops.h +23 -1
@@ -169,6 +169,27 @@ struct TCGCPUOps {*/ vaddr (*untagged_addr)(CPUState *cs,vaddr addr);#else +/** +* @poll_during_halt: Poll target-specific events while the CPU is halted. +* @cpu: vCPU context +* +* Called when the CPU is halted to handle target-specific asynchronous +* event processing before instruction execution begins. +* The caller does not hold the BQL. +*/ +void (*poll_during_halt)(CPUState *cpu); +/** +* @leaving_halt: Perform target-specific cleanup before resuming +* execution. +* @cpu: vCPU context +* +* Called after a halted CPU has detected pending work and is about to +* resume execution from the halted state. This callback performs any +* necessary target-specific state transitions or synchronization before +* instruction execution resumes. The caller does not hold the BQL. +* Either this callback or @cpu_exec_halt must be provided (but not both). +*/ +void (*leaving_halt)(CPUState *cpu);/** @do_interrupt: Callback for interrupt handling. */ void (*do_interrupt)(CPUState *cpu);/** @cpu_exec_interrupt: Callback for processing interrupts in cpu_exec */
@@ -185,7 +206,8 @@ struct TCGCPUOps {* if it should remain in the halted state. (This should generally * be the same value that cpu_has_work() would return.) * -* This method must be provided. If the target does not need to +* Either @leaving_halt or this method must be provided, but not both. +* If the target does not need to * do anything special for halt, the same function used for its * SysemuCPUOps::has_work method can be used here, as they have the * same function signature.
accel/tcg/cpu-exec.c +17 -4
@@ -48,6 +48,7 @@#include "internal-common.h" #if !defined(CONFIG_USER_ONLY )#include "accel/tcg/iommu.h" +#include "hw/core/sysemu-cpu-ops.h" #endif /* -icount align implementation. */
@@ -659,10 +660,22 @@ static inline void tb_add_jump(TranslationBlock *tb, int n,static bool cpu_poll_while_halted (CPUState *cpu) {const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops; -bool leave_halt = tcg_ops->cpu_exec_halt (cpu); -if (!leave_halt) { -return false; +if (tcg_ops->leaving_halt) { +assert (!tcg_ops->cpu_exec_halt); +if (tcg_ops->poll_during_halt) { + tcg_ops->poll_during_halt (cpu); + } +if (!cpu_has_work (cpu)) { +return false; + } + tcg_ops->leaving_halt (cpu); + }else { +assert (!tcg_ops->poll_during_halt); +assert (cpu->cc->sysemu_ops->has_work == tcg_ops->cpu_exec_halt); +if (!tcg_ops->cpu_exec_halt (cpu)) { +return false; + } } cpu->halted =0 ;/* allow execution */
@@ -1061,7 +1074,7 @@ bool tcg_exec_realizefn(CPUState *cpu, Error **errp)/* Check mandatory TCGCPUOps handlers */ const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;#ifndef CONFIG_USER_ONLY -assert (tcg_ops->cpu_exec_halt); +assert (tcg_ops->cpu_exec_halt || tcg_ops->leaving_halt);assert (tcg_ops->cpu_exec_interrupt);assert (tcg_ops->cpu_exec_reset);assert (tcg_ops->pointer_wrap);
Signature · 1 line
2.55.0
PATCH v4 3/9 target/arm: Split leaving-halt handling from arm_cpu_exec_halt()
In reply to Philippe Mathieu-DaudéMove WFxT timer cleanup and halt-reason reset into arm_cpu_leaving_halt(). Keep arm_cpu_exec_halt() focused on deciding whether the CPU can leave the halted state.
--- target/arm/cpu.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
target/arm/cpu.c +13 -7
@@ -872,18 +872,24 @@ static bool arm_cpu_internal_is_big_endian(CPUState *cs) }#ifdef CONFIG_TCG +static void arm_cpu_leaving_halt (CPUState *cs) +{ +ARMCPU *cpu =ARM_CPU (cs); + +/* We're about to come out of WFI/WFE: disable the WFxT timer */ +if (cpu->wfxt_timer) { +timer_del (cpu->wfxt_timer); + } +/* clear the halt reason */ +qatomic_set (&cpu->env.halt_reason,NOT_HALTED ); +} +bool arm_cpu_exec_halt (CPUState *cs) {bool leave_halt =cpu_has_work (cs);if (leave_halt) { -/* We're about to come out of WFI/WFE: disable the WFxT timer */ -ARMCPU *cpu =ARM_CPU (cs); -if (cpu->wfxt_timer) { -timer_del (cpu->wfxt_timer); - } -/* clear the halt reason */ -qatomic_set (&cpu->env.halt_reason,NOT_HALTED ); +arm_cpu_leaving_halt (cs); }return leave_halt; }
Signature · 1 line
2.55.0
PATCH v4 4/9 target/arm: Convert cpu_exec_halt() to leaving_halt()
In reply to Philippe Mathieu-Daudéarm_cpu_exec_halt() now merely checks cpu_has_work() and conditionally call arm_cpu_leaving_halt(), which is now the generic flow in cpu_has_work_after_processing_async_events().
Register the leaving_halt handler to use the generic target-agnostic flow and remove the arm_cpu_exec_halt() wrapper.
--- target/arm/internals.h | 3 --- target/arm/cpu.c | 12 +----------- target/arm/tcg/cpu-v7m.c | 1 - 3 files changed, 1 insertion(+), 15 deletions(-)
target/arm/internals.h +0 -3
@@ -431,9 +431,6 @@ void arm_restore_state_to_opc(CPUState *cs,#ifdef CONFIG_TCG TCGTBCPUState arm_get_tb_cpu_state (CPUState *cs);void arm_cpu_synchronize_from_tb (CPUState *cs,const TranslationBlock *tb); - -/* Our implementation of TCGCPUOps::cpu_exec_halt */ -bool arm_cpu_exec_halt (CPUState *cs);int arm_cpu_mmu_index (CPUState *cs,bool ifetch);#endif /* CONFIG_TCG */
target/arm/cpu.c +1 -11
@@ -883,16 +883,6 @@ static void arm_cpu_leaving_halt(CPUState *cs)/* clear the halt reason */ qatomic_set(&cpu->env.halt_reason, NOT_HALTED); } - -bool arm_cpu_exec_halt (CPUState *cs) -{ -bool leave_halt =cpu_has_work (cs); - -if (leave_halt) { -arm_cpu_leaving_halt (cs); - } -return leave_halt; -}#endif /*
@@ -2625,7 +2615,7 @@ static const TCGCPUOps arm_tcg_ops = { .tlb_fill_align = arm_cpu_tlb_fill_align, .pointer_wrap = aprofile_pointer_wrap, .cpu_exec_interrupt = arm_cpu_exec_interrupt, - .cpu_exec_halt = arm_cpu_exec_halt, + .leaving_halt = arm_cpu_leaving_halt, .cpu_exec_reset = cpu_reset, .do_interrupt = arm_cpu_do_interrupt, .do_transaction_failed = arm_cpu_do_transaction_failed,
target/arm/tcg/cpu-v7m.c +0 -1
@@ -302,7 +302,6 @@ static const TCGCPUOps arm_v7m_tcg_ops = { .tlb_fill_align = arm_cpu_tlb_fill_align, .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = arm_v7m_cpu_exec_interrupt, - .cpu_exec_halt = arm_cpu_exec_halt, .cpu_exec_reset = cpu_reset, .do_interrupt = arm_v7m_cpu_do_interrupt, .do_transaction_failed = arm_cpu_do_transaction_failed,
Signature · 1 line
2.55.0
PATCH v4 5/9 target/i386: Split APIC polling from x86_cpu_exec_halt()
In reply to Philippe Mathieu-DaudéMove APIC polling into x86_cpu_poll_during_halt() while keeping the remaining halt handling in x86_cpu_exec_halt().
--- target/i386/tcg/helper-tcg.h | 1 + target/i386/tcg/system/seg_helper.c | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-)
target/i386/tcg/helper-tcg.h +1 -0
@@ -37,6 +37,7 @@*/ void x86_cpu_do_interrupt (CPUState *cpu);#ifndef CONFIG_USER_ONLY +void x86_cpu_poll_during_halt (CPUState *cpu);bool x86_cpu_exec_halt (CPUState *cpu);bool x86_need_replay_interrupt (int interrupt_request);bool x86_cpu_exec_interrupt (CPUState *cpu,int int_req);
target/i386/tcg/system/seg_helper.c +9 -2
@@ -128,10 +128,9 @@ void x86_cpu_do_interrupt(CPUState *cs) } } -bool x86_cpu_exec_halt (CPUState *cpu) +void x86_cpu_poll_during_halt (CPUState *cpu) {X86CPU *x86_cpu =X86_CPU (cpu); -CPUX86State *env = &x86_cpu->env;if (cpu_test_interrupt (cpu,CPU_INTERRUPT_POLL )) {bql_lock ();
@@ -139,6 +138,14 @@ bool x86_cpu_exec_halt(CPUState *cpu)cpu_reset_interrupt (cpu,CPU_INTERRUPT_POLL );bql_unlock (); } +} + +bool x86_cpu_exec_halt (CPUState *cpu) +{ +X86CPU *x86_cpu =X86_CPU (cpu); +CPUX86State *env = &x86_cpu->env; + +x86_cpu_poll_during_halt (cpu);if (!cpu_has_work (cpu)) {return false;
Signature · 1 line
2.55.0
PATCH v4 6/9 target/i386: Split leaving-halt handling from x86_cpu_exec_halt()
In reply to Philippe Mathieu-DaudéMove debug exception handling performed when leaving HLT into x86_cpu_leaving_halt(). Keep x86_cpu_exec_halt() responsible for checking whether the CPU can resume.
Document that #DB IRQ delivery through do_interrupt_all() runs in the vCPU thread and does not require BQL protection.
--- target/i386/tcg/helper-tcg.h | 1 + target/i386/tcg/system/seg_helper.c | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-)
target/i386/tcg/helper-tcg.h +1 -0
@@ -39,6 +39,7 @@ void x86_cpu_do_interrupt(CPUState *cpu);#ifndef CONFIG_USER_ONLY void x86_cpu_poll_during_halt (CPUState *cpu);bool x86_cpu_exec_halt (CPUState *cpu); +void x86_cpu_leaving_halt (CPUState *cpu);bool x86_need_replay_interrupt (int interrupt_request);bool x86_cpu_exec_interrupt (CPUState *cpu,int int_req);#endif
target/i386/tcg/system/seg_helper.c +17 -8
@@ -140,22 +140,31 @@ void x86_cpu_poll_during_halt(CPUState *cpu) } } -bool x86_cpu_exec_halt (CPUState *cpu) +void x86_cpu_leaving_halt (CPUState *cpu) {X86CPU *x86_cpu =X86_CPU (cpu); -CPUX86State *env = &x86_cpu->env; - -x86_cpu_poll_during_halt (cpu); - -if (!cpu_has_work (cpu)) { -return false; - } +CPUX86State *env =cpu_env (cpu);/* Complete HLT instruction. */ if (env->eflags &TF_MASK ) { env->dr[6 ] |=DR6_BS ; +/* +* do_interrupt_all() is the normal TCG exception-delivery path +* and runs in the same vCPU thread context, so #DB delivery does +* not require BQL protection here. +*/ do_interrupt_all (x86_cpu,EXCP01_DB ,0 ,0 , env->eip,0 ); } +} + +bool x86_cpu_exec_halt (CPUState *cpu) +{ +if (!cpu_has_work (cpu)) { +return false; + } + +x86_cpu_leaving_halt (cpu); +return true; }
Signature · 1 line
2.55.0
PATCH v4 7/9 target/i386: Use generic halt transition handlers
In reply to Philippe Mathieu-DaudéRegister x86_cpu_poll_during_halt() and x86_cpu_leaving_halt() with the generic TCG halt flow. Remove the target-specific x86_cpu_exec_halt() wrapper.
--- target/i386/tcg/helper-tcg.h | 1 - target/i386/tcg/system/seg_helper.c | 11 ----------- target/i386/tcg/tcg-cpu.c | 3 ++- 3 files changed, 2 insertions(+), 13 deletions(-)
target/i386/tcg/helper-tcg.h +0 -1
@@ -38,7 +38,6 @@void x86_cpu_do_interrupt (CPUState *cpu);#ifndef CONFIG_USER_ONLY void x86_cpu_poll_during_halt (CPUState *cpu); -bool x86_cpu_exec_halt (CPUState *cpu);void x86_cpu_leaving_halt (CPUState *cpu);bool x86_need_replay_interrupt (int interrupt_request);bool x86_cpu_exec_interrupt (CPUState *cpu,int int_req);
target/i386/tcg/system/seg_helper.c +0 -11
@@ -157,17 +157,6 @@ void x86_cpu_leaving_halt(CPUState *cpu) } } -bool x86_cpu_exec_halt (CPUState *cpu) -{ -if (!cpu_has_work (cpu)) { -return false; - } - -x86_cpu_leaving_halt (cpu); - -return true; -} -bool x86_need_replay_interrupt (int interrupt_request) {/*
target/i386/tcg/tcg-cpu.c +2 -1
@@ -180,7 +180,8 @@ const TCGCPUOps x86_tcg_ops = { .tlb_fill = x86_cpu_tlb_fill, .pointer_wrap = x86_pointer_wrap, .do_interrupt = x86_cpu_do_interrupt, - .cpu_exec_halt = x86_cpu_exec_halt, + .poll_during_halt = x86_cpu_poll_during_halt, + .leaving_halt = x86_cpu_leaving_halt, .cpu_exec_interrupt = x86_cpu_exec_interrupt, .cpu_exec_reset = x86_cpu_exec_reset, .do_unaligned_access = x86_cpu_do_unaligned_access,
Signature · 1 line
2.55.0
PATCH v4 8/9 accel/tcg: Remove the redundant cpu_exec_halt() hook
In reply to Philippe Mathieu-DaudéAll remaining users of cpu_exec_halt simply alias cpu_has_work(). Call cpu_has_work() directly from the generic halt flow and remove the redundant hook registrations and interface definition.
Make affected cpu_has_work() helpers private where possible.
--- include/accel/tcg/cpu-ops.h | 18 ------------------ target/loongarch/internals.h | 1 - target/riscv/internals.h | 5 ----- target/s390x/s390x-internal.h | 1 - accel/tcg/cpu-exec.c | 23 ++++++++--------------- target/alpha/cpu.c | 1 - target/avr/cpu.c | 1 - target/hexagon/cpu.c | 1 - target/hppa/cpu.c | 1 - target/loongarch/cpu.c | 2 +- target/loongarch/tcg/tcg_cpu.c | 1 - target/m68k/cpu.c | 1 - target/microblaze/cpu.c | 1 - target/mips/cpu.c | 1 - target/or1k/cpu.c | 1 - target/ppc/cpu_init.c | 1 - target/riscv/cpu.c | 2 +- target/riscv/tcg/tcg-cpu.c | 1 - target/rx/cpu.c | 1 - target/s390x/cpu-system.c | 2 +- target/s390x/cpu.c | 1 - target/sh4/cpu.c | 1 - target/sparc/cpu.c | 1 - target/tricore/cpu.c | 1 - target/xtensa/cpu.c | 1 - 25 files changed, 11 insertions(+), 60 deletions(-)
include/accel/tcg/cpu-ops.h +0 -18
@@ -187,7 +187,6 @@ struct TCGCPUOps {* resume execution from the halted state. This callback performs any * necessary target-specific state transitions or synchronization before * instruction execution resumes. The caller does not hold the BQL. -* Either this callback or @cpu_exec_halt must be provided (but not both). */ void (*leaving_halt)(CPUState *cpu);/** @do_interrupt: Callback for interrupt handling. */
@@ -196,23 +195,6 @@ struct TCGCPUOps {bool (*cpu_exec_interrupt)(CPUState *cpu,int interrupt_request);/** @cpu_exec_reset: Callback for reset in cpu_exec. */ void (*cpu_exec_reset)(CPUState *cpu); -/** -* @cpu_exec_halt: Callback for handling halt in cpu_exec. -* -* The target CPU should do any special processing here that it needs -* to do when the CPU is in the halted state. -* -* Return true to indicate that the CPU should now leave halt, false -* if it should remain in the halted state. (This should generally -* be the same value that cpu_has_work() would return.) -* -* Either @leaving_halt or this method must be provided, but not both. -* If the target does not need to -* do anything special for halt, the same function used for its -* SysemuCPUOps::has_work method can be used here, as they have the -* same function signature. -*/ -bool (*cpu_exec_halt)(CPUState *cpu);/** * @tlb_fill_align: Handle a softmmu tlb miss * @cpu: cpu context
target/loongarch/internals.h +0 -1
@@ -38,7 +38,6 @@ void cpu_loongarch_timer_cb(void *opaque);uint64_t cpu_loongarch_get_timer_counter (CPUTimerState *timer);uint64_t cpu_loongarch_get_timer_ticks (CPUTimerState *timer);void cpu_loongarch_set_timer_config (CPUTimerState *timer,uint64_t value); -bool loongarch_cpu_has_work (CPUState *cs);bool cpu_loongarch_hw_interrupts_pending (CPULoongArchState *env);#endif /* !CONFIG_USER_ONLY */
target/riscv/internals.h +0 -5
@@ -201,11 +201,6 @@ static inline target_ulong get_xepc_mask(CPURISCVState *env) } } -#ifndef CONFIG_USER_ONLY -/* Our implementation of SysemuCPUOps::has_work */ -bool riscv_cpu_has_work (CPUState *cs); -#endif -/* Zjpm addr masking routine */ static inline target_ulong adjust_addr_body(CPURISCVState *env, target_ulong addr,
target/s390x/s390x-internal.h +0 -1
@@ -248,7 +248,6 @@ bool s390_cpu_system_realize(DeviceState *dev, Error **errp);void s390_cpu_finalize (Object *obj);void s390_cpu_system_class_init (CPUClass *cc);void s390_cpu_machine_reset_cb (void *opaque); -bool s390_cpu_has_work (CPUState *cs);#endif /* CONFIG_USER_ONLY */
accel/tcg/cpu-exec.c +8 -15
@@ -48,7 +48,6 @@#include "internal-common.h" #if !defined(CONFIG_USER_ONLY )#include "accel/tcg/iommu.h" -#include "hw/core/sysemu-cpu-ops.h" #endif /* -icount align implementation. */
@@ -661,21 +660,16 @@ static bool cpu_poll_while_halted(CPUState *cpu) {const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops; +if (tcg_ops->poll_during_halt) { + tcg_ops->poll_during_halt (cpu); + } + +if (!cpu_has_work (cpu)) { +return false; + } +if (tcg_ops->leaving_halt) { -assert (!tcg_ops->cpu_exec_halt); -if (tcg_ops->poll_during_halt) { - tcg_ops->poll_during_halt (cpu); - } -if (!cpu_has_work (cpu)) { -return false; - } tcg_ops->leaving_halt (cpu); - }else { -assert (!tcg_ops->poll_during_halt); -assert (cpu->cc->sysemu_ops->has_work == tcg_ops->cpu_exec_halt); -if (!tcg_ops->cpu_exec_halt (cpu)) { -return false; - } } cpu->halted =0 ;/* allow execution */
@@ -1074,7 +1068,6 @@ bool tcg_exec_realizefn(CPUState *cpu, Error **errp)/* Check mandatory TCGCPUOps handlers */ const TCGCPUOps *tcg_ops = cpu->cc->tcg_ops;#ifndef CONFIG_USER_ONLY -assert (tcg_ops->cpu_exec_halt || tcg_ops->leaving_halt);assert (tcg_ops->cpu_exec_interrupt);assert (tcg_ops->cpu_exec_reset);assert (tcg_ops->pointer_wrap);
target/alpha/cpu.c +0 -1
@@ -265,7 +265,6 @@ static const TCGCPUOps alpha_tcg_ops = { .tlb_fill = alpha_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_notreached, .cpu_exec_interrupt = alpha_cpu_exec_interrupt, - .cpu_exec_halt = alpha_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = alpha_cpu_do_interrupt, .do_transaction_failed = alpha_cpu_do_transaction_failed,
target/avr/cpu.c +0 -1
@@ -246,7 +246,6 @@ static const TCGCPUOps avr_tcg_ops = { .restore_state_to_opc = avr_restore_state_to_opc, .mmu_index = avr_cpu_mmu_index, .cpu_exec_interrupt = avr_cpu_exec_interrupt, - .cpu_exec_halt = avr_cpu_has_work, .cpu_exec_reset = cpu_reset, .tlb_fill = avr_cpu_tlb_fill, .do_interrupt = avr_cpu_do_interrupt,
target/hexagon/cpu.c +0 -1
@@ -806,7 +806,6 @@ static const TCGCPUOps hexagon_tcg_ops = { .cpu_exec_reset = cpu_reset, .tlb_fill = hexagon_tlb_fill, .do_unaligned_access = hexagon_cpu_do_unaligned_access, - .cpu_exec_halt = hexagon_cpu_has_work, .do_interrupt = hexagon_cpu_do_interrupt,#endif /* !CONFIG_USER_ONLY */ };
target/hppa/cpu.c +0 -1
@@ -270,7 +270,6 @@ static const TCGCPUOps hppa_tcg_ops = { .tlb_fill_align = hppa_cpu_tlb_fill_align, .pointer_wrap = cpu_pointer_wrap_notreached, .cpu_exec_interrupt = hppa_cpu_exec_interrupt, - .cpu_exec_halt = hppa_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = hppa_cpu_do_interrupt, .do_unaligned_access = hppa_cpu_do_unaligned_access,
target/loongarch/cpu.c +1 -1
@@ -123,7 +123,7 @@ bool cpu_loongarch_hw_interrupts_pending(CPULoongArchState *env)#endif #ifndef CONFIG_USER_ONLY -bool loongarch_cpu_has_work (CPUState *cs) +static bool loongarch_cpu_has_work (CPUState *cs) {bool has_work = false;
target/loongarch/tcg/tcg_cpu.c +0 -1
@@ -327,7 +327,6 @@ const TCGCPUOps loongarch_tcg_ops = { .tlb_fill = loongarch_cpu_tlb_fill, .pointer_wrap = loongarch_pointer_wrap, .cpu_exec_interrupt = loongarch_cpu_exec_interrupt, - .cpu_exec_halt = loongarch_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = loongarch_cpu_do_interrupt, .do_transaction_failed = loongarch_cpu_do_transaction_failed,
target/m68k/cpu.c +0 -1
@@ -710,7 +710,6 @@ static const TCGCPUOps m68k_tcg_ops = { .tlb_fill = m68k_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = m68k_cpu_exec_interrupt, - .cpu_exec_halt = m68k_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = m68k_cpu_do_interrupt, .do_transaction_failed = m68k_cpu_transaction_failed,
target/microblaze/cpu.c +0 -1
@@ -448,7 +448,6 @@ static const TCGCPUOps mb_tcg_ops = { .tlb_fill = mb_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = mb_cpu_exec_interrupt, - .cpu_exec_halt = mb_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = mb_cpu_do_interrupt, .do_transaction_failed = mb_cpu_transaction_failed,
target/mips/cpu.c +0 -1
@@ -716,7 +716,6 @@ static const TCGCPUOps mips_tcg_ops = { .tlb_fill = mips_cpu_tlb_fill, .pointer_wrap = mips_pointer_wrap, .cpu_exec_interrupt = mips_cpu_exec_interrupt, - .cpu_exec_halt = mips_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = mips_cpu_do_interrupt, .do_transaction_failed = mips_cpu_do_transaction_failed,
target/or1k/cpu.c +0 -1
@@ -266,7 +266,6 @@ static const TCGCPUOps openrisc_tcg_ops = { .tlb_fill = openrisc_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = openrisc_cpu_exec_interrupt, - .cpu_exec_halt = openrisc_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = openrisc_cpu_do_interrupt,#endif /* !CONFIG_USER_ONLY */
target/ppc/cpu_init.c +0 -1
@@ -7496,7 +7496,6 @@ static const TCGCPUOps ppc_tcg_ops = { .tlb_fill = ppc_cpu_tlb_fill, .pointer_wrap = ppc_pointer_wrap, .cpu_exec_interrupt = ppc_cpu_exec_interrupt, - .cpu_exec_halt = ppc_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = ppc_cpu_do_interrupt, .cpu_exec_enter = ppc_cpu_exec_enter,
target/riscv/cpu.c +1 -1
@@ -960,7 +960,7 @@ uint64_t riscv_cpu_get_mip(const CPURISCVState *env)return qatomic_read (&env->mip); } -bool riscv_cpu_has_work (CPUState *cs) +static bool riscv_cpu_has_work (CPUState *cs) {RISCVCPU *cpu =RISCV_CPU (cs);CPURISCVState *env = &cpu->env;
target/riscv/tcg/tcg-cpu.c +0 -1
@@ -286,7 +286,6 @@ const TCGCPUOps riscv_tcg_ops = { .tlb_fill = riscv_cpu_tlb_fill, .pointer_wrap = riscv_pointer_wrap, .cpu_exec_interrupt = riscv_cpu_exec_interrupt, - .cpu_exec_halt = riscv_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = riscv_cpu_do_interrupt, .do_transaction_failed = riscv_cpu_do_transaction_failed,
target/rx/cpu.c +0 -1
@@ -227,7 +227,6 @@ static const TCGCPUOps rx_tcg_ops = { .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = rx_cpu_exec_interrupt, - .cpu_exec_halt = rx_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = rx_cpu_do_interrupt, };
target/s390x/cpu-system.c +1 -1
@@ -39,7 +39,7 @@#include "system/tcg.h" #include "hw/core/sysemu-cpu-ops.h" -bool s390_cpu_has_work (CPUState *cs) +static bool s390_cpu_has_work (CPUState *cs) {S390CPU *cpu =S390_CPU (cs);
target/s390x/cpu.c +0 -1
@@ -384,7 +384,6 @@ static const TCGCPUOps s390_tcg_ops = { .tlb_fill = s390_cpu_tlb_fill, .pointer_wrap = s390_pointer_wrap, .cpu_exec_interrupt = s390_cpu_exec_interrupt, - .cpu_exec_halt = s390_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = s390_cpu_do_interrupt, .debug_excp_handler = s390x_cpu_debug_excp_handler,
target/sh4/cpu.c +0 -1
@@ -312,7 +312,6 @@ static const TCGCPUOps superh_tcg_ops = { .tlb_fill = superh_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_notreached, .cpu_exec_interrupt = superh_cpu_exec_interrupt, - .cpu_exec_halt = superh_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = superh_cpu_do_interrupt, .do_unaligned_access = superh_cpu_do_unaligned_access,
target/sparc/cpu.c +0 -1
@@ -1074,7 +1074,6 @@ static const TCGCPUOps sparc_tcg_ops = { .tlb_fill = sparc_cpu_tlb_fill, .pointer_wrap = sparc_pointer_wrap, .cpu_exec_interrupt = sparc_cpu_exec_interrupt, - .cpu_exec_halt = sparc_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = sparc_cpu_do_interrupt, .do_transaction_failed = sparc_cpu_do_transaction_failed,
target/tricore/cpu.c +0 -1
@@ -223,7 +223,6 @@ static const TCGCPUOps tricore_tcg_ops = { .tlb_fill = tricore_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = tricore_cpu_exec_interrupt, - .cpu_exec_halt = tricore_cpu_has_work, .cpu_exec_reset = cpu_reset, };
target/xtensa/cpu.c +0 -1
@@ -325,7 +325,6 @@ static const TCGCPUOps xtensa_tcg_ops = { .tlb_fill = xtensa_cpu_tlb_fill, .pointer_wrap = cpu_pointer_wrap_uint32, .cpu_exec_interrupt = xtensa_cpu_exec_interrupt, - .cpu_exec_halt = xtensa_cpu_has_work, .cpu_exec_reset = cpu_reset, .do_interrupt = xtensa_cpu_do_interrupt, .do_transaction_failed = xtensa_cpu_do_transaction_failed,
Signature · 1 line
2.55.0
PATCH v4 9/9 target/arm: Kick vCPU after updating PSCI power state
In reply to Philippe Mathieu-DaudéPSCI can update the power state of a vCPU other than the caller. arm_cpu_has_work() reads env->halt_reason to decide whether that vCPU has work to do, and is called from the vCPU thread without the BQL held. A remote power_state/halt_reason update is therefore not observed until the target vCPU is rescheduled, so the vCPU stays halted and a PSCI CPU_ON of a secondary vCPU can hang.
Kick the vCPU after changing its state so it re-evaluates promptly.
--- target/arm/arm-powerctl.c | 5 +++++ 1 file changed, 5 insertions(+)
target/arm/arm-powerctl.c +5 -0
@@ -48,9 +48,14 @@ struct CpuOnInfo {void arm_set_cpu_power_state (ARMCPU *cpu,ARMPSCIState state) {CPUARMState *env = &cpu->env; +CPUState *cs =CPU (cpu);qatomic_set (&cpu->power_state, state);qatomic_set (&env->halt_reason, state ==PSCI_OFF ?HALT_PSCI :NOT_HALTED ); + +if (!qemu_cpu_is_self (cs)) { +qemu_cpu_kick (cs); + } } static void arm_set_cpu_on_async_work(CPUState *target_cpu_state,
Signature · 1 line
2.55.0
Re: [PATCH v4 9/9] target/arm: Kick vCPU after updating PSCI power state
Quoted 23 lines · Philippe Mathieu-Daudé
PSCI can update the power state of a vCPU other than the caller.
arm_cpu_has_work()readsenv->halt_reasonto decide whether that vCPU has work to do, and is called from the vCPU thread without the BQL held. A remote power_state/halt_reason update is therefore not observed until the target vCPU is rescheduled, so the vCPU stays halted and a PSCICPU_ONof a secondary vCPU can hang.Kick the vCPU after changing its state so it re-evaluates promptly.
Reported-by Paolo Bonzini <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/arm/arm-powerctl.c | 5 +++++ 1 file changed, 5 insertions(+)target/arm/arm-powerctl.c +0
-0@@ -48,9 +48,14 @@ struct CpuOnInfo {void arm_set_cpu_power_state (ARMCPU *cpu,ARMPSCIState state) {
target/arm/arm-powerctl.c struct CpuOnInfo { · line 59
Quoted 10 lines
target/arm/arm-powerctl.c +5
-0CPUARMState *env = &cpu->env; +CPUState *cs =CPU (cpu);qatomic_set (&cpu->power_state, state);qatomic_set (&env->halt_reason, state ==PSCI_OFF ?HALT_PSCI :NOT_HALTED ); + +if (!qemu_cpu_is_self (cs)) { +qemu_cpu_kick (cs); + } }
Looking at the callsites of this function:
* three places in arm-powerctl.c call it from an async_run_on_cpu callback function which runs it in the context of the vCPU it is changing the power state for
* target/arm/cpu.c calls it from within reset for the vCPU being reset, so we definitely don't want to kick the vCPU
* target/arm/kvm.c calls it as part of the "sync the vCPU state to/from KVM" function, which should only be running from that vCPU's thread
* target/arm/machine.c calls it as part of an inbound migration to set the vCPU's power-state to whatever it was on the source
So I think that in the cases where we're actually running the VM the if() check will fail and we do nothing anyway, and in the other cases (reset, migration) we don't want to be kicking the vCPU.
thanks
-- PMM
Re: [PATCH v4 9/9] target/arm: Kick vCPU after updating PSCI power state
Quoted 49 lines · Peter Maydell
Quoted 23 lines · Philippe Mathieu-Daudé
PSCI can update the power state of a vCPU other than the caller.
arm_cpu_has_work()readsenv->halt_reasonto decide whether that vCPU has work to do, and is called from the vCPU thread without the BQL held. A remote power_state/halt_reason update is therefore not observed until the target vCPU is rescheduled, so the vCPU stays halted and a PSCICPU_ONof a secondary vCPU can hang.Kick the vCPU after changing its state so it re-evaluates promptly.
Reported-by Paolo Bonzini <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/arm/arm-powerctl.c | 5 +++++ 1 file changed, 5 insertions(+)target/arm/arm-powerctl.c +0
-0@@ -48,9 +48,14 @@ struct CpuOnInfo {void arm_set_cpu_power_state (ARMCPU *cpu,ARMPSCIState state) {target/arm/arm-powerctl.c struct CpuOnInfo { · line 59
Quoted 10 lines
target/arm/arm-powerctl.c +5
-0CPUARMState *env = &cpu->env; +CPUState *cs =CPU (cpu);qatomic_set (&cpu->power_state, state);qatomic_set (&env->halt_reason, state ==PSCI_OFF ?HALT_PSCI :NOT_HALTED ); + +if (!qemu_cpu_is_self (cs)) { +qemu_cpu_kick (cs); + } }Looking at the callsites of this function:
* three places in
arm-powerctl.ccall it from anasync_run_on_cpucallback function which runs it in the context of the vCPU it is changing the power state for
*target/arm/cpu.ccalls it from within reset for the vCPU being reset, so we definitely don't want to kick the vCPU
*target/arm/kvm.ccalls it as part of the "sync the vCPU state to/from KVM" function, which should only be running from that vCPU's thread
*target/arm/machine.ccalls it as part of an inbound migration to set the vCPU's power-state to whatever it was on the source
Quoted 3 lines
So I think that in the cases where we're actually running the VM the
if()check will fail and we do nothing anyway, and in the other cases (reset, migration) we don't want to be kicking the vCPU.
Indeed... Paolo identified a possible race problem when discussing the generic sync changes around has_work() so I ran to address it but failed to correctly audit the call sites for the ARM case, and this indeed does not occurs. Hopefully the new comment added in commit fbf08c0fbd5 will help reminding we need to be careful and not try to protect unreachable cases.
Thanks for the audit,
Phil.
Quoted 10 lines · Philippe Mathieu-Daudé
Missing review: 9
Since v3:
- Rebased (s390x, x86 and some ARM patches merged)
- RISC-V posted separately as [1]
- Better describe ARM PSCI power-state update issue
Quoted 9 lines
Philippe Mathieu-Daudé (9):
accel/tcg: Refactor poll-while-halted flow incpu_exec()accel/tcg: Introducepoll_during_halt()andleaving_halt()target/arm: Split leaving-halt handling fromarm_cpu_exec_halt()target/arm: Convertcpu_exec_halt()toleaving_halt()target/i386: Split APIC polling fromx86_cpu_exec_halt()target/i386: Split leaving-halt handling fromx86_cpu_exec_halt()target/i386: Use generic halt transition handlers accel/tcg: Remove the redundantcpu_exec_halt()hook
Since Alistair queued the prerequisite patches [1], I'm directly queuing the reviewed patches 1-8 via accel-next tree.
Quoted 1 line
target/arm: Kick vCPU after updating PSCI power state
I might repost this single one (arm specific) later when the rest landed in /master.
Thanks,
Phil.
Quoted 22 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Philippe Mathieu-Daudé
Missing review: 9
Since v3:
- Rebased (s390x, x86 and some ARM patches merged)
- RISC-V posted separately as [1]
- Better describe ARM PSCI power-state update issueQuoted 9 lines
Philippe Mathieu-Daudé (9):
accel/tcg: Refactor poll-while-halted flow incpu_exec()accel/tcg: Introducepoll_during_halt()andleaving_halt()target/arm: Split leaving-halt handling fromarm_cpu_exec_halt()target/arm: Convertcpu_exec_halt()toleaving_halt()target/i386: Split APIC polling fromx86_cpu_exec_halt()target/i386: Split leaving-halt handling fromx86_cpu_exec_halt()target/i386: Use generic halt transition handlers accel/tcg: Remove the redundantcpu_exec_halt()hook
Quoted 2 lines
Since Alistair queued the prerequisite patches [1], I'm directly queuing the reviewed patches 1-8 via accel-next tree.
No objections here.
Quoted 4 lines
Quoted 1 line
target/arm: Kick vCPU after updating PSCI power state
I might repost this single one (arm specific) later when the rest landed in /master.
I reviewed this one (as "I think this isn't a correct change").
thanks
-- PMM