accel, cpus: clean up cpu->exit_request
Subsystems (2)
v3 cover accel, cpus: clean up cpu->exit_request
cpu->exit_request is used to kick vCPU threads into qemu_wait_io_event(). The code that handles the signaling of cpu->exit_request is messy, mostly due to no one ever taking a look at it as a whole. In fact already in commit 4b8523ee896 ("kvm: First step to push iothread lock out of inner run loop", 2015-07-01), the read of cpu->exit_request was placed outside the BQL critical section without much attention to ordering; and it only got worse from that point, in no small part due to a young and naive me.
This series is complementary to the cpu->interrupt_request cleanups that Igor did in "memory: reintroduce BQL-free fine-grained PIO/MMIO"
(https://lore.kernel.org/qemu-devel/[email protected]/T/),
and in fact includes some extra changes to interrupt_request at the beginning.
Paolo
Paolo Bonzini (19):
target/ppc: limit cpu_interrupt_exittb to system emulation target/sparc: limit cpu_check_irqs to system emulation target/i386: limit a20 to system emulation
target-arm: remove uses of cpu_interrupt() for user-mode emulation
user-exec: remove cpu_interrupt() stub
treewide: clear bits of cs->interrupt_request with cpu_reset_interrupt()
cpu-common: use atomic access for interrupt_request
cpus: document that qemu_cpu_kick() can be used for BQL-less operation
accel: use store_release/load_acquire for cross-thread exit_request
accel: use atomic accesses for exit_request
accel/tcg: create a thread-kick function for TCG
accel/tcg: inline cpu_exit()
cpus: remove TCG-ism from cpu_exit()
cpus: properly kick CPUs out of inner execution loop
treewide: rename qemu_wait_io_event/qemu_wait_io_event_common bsd-user, linux-user: introduce qemu_process_cpu_events
cpus: clear exit_request in qemu_process_cpu_events
accel: make all calls to qemu_process_cpu_events look the same tcg/user: do not set exit_request gratuitously
v2->v3:
- [accel: use store_release/load_acquire for cross-thread exit_request] improve comments for memory barriers, match rr_kick_next_cpu() more accurately
- [cpus: properly kick CPUs out of inner execution loop] fix comment
- [treewide: rename qemu_wait_io_event/qemu_wait_io_event_common] new patch; new names used throughout in patches 16-19
docs/devel/tcg-icount.rst | 2 +- accel/tcg/tcg-accel-ops-mttcg.h | 3 - accel/tcg/tcg-accel-ops.h | 1 + bsd-user/aarch64/target_arch_cpu.h | 2 +- bsd-user/arm/target_arch_cpu.h | 2 +- bsd-user/i386/target_arch_cpu.h | 2 +- bsd-user/riscv/target_arch_cpu.h | 2 +- bsd-user/x86_64/target_arch_cpu.h | 2 +- include/hw/core/cpu.h | 22 +- include/system/cpus.h | 3 +- target/arm/internals.h | 5 + accel/dummy-cpus.c | 2 +- accel/hvf/hvf-accel-ops.c | 2 +- accel/kvm/kvm-accel-ops.c | 3 +- accel/kvm/kvm-all.c | 23 +- accel/tcg/cpu-exec.c | 34 ++- accel/tcg/tcg-accel-ops-mttcg.c | 12 +- accel/tcg/tcg-accel-ops-rr.c | 55 ++-- accel/tcg/tcg-accel-ops.c | 4 +- accel/tcg/user-exec.c | 11 +- bsd-user/main.c | 5 - cpu-common.c | 3 +- hw/core/cpu-common.c | 19 +- hw/core/cpu-system.c | 2 +- hw/ppc/ppc.c | 2 + hw/ppc/spapr_hcall.c | 7 +- hw/ppc/spapr_rtas.c | 2 +- linux-user/aarch64/cpu_loop.c | 2 +- linux-user/alpha/cpu_loop.c | 2 +- linux-user/arm/cpu_loop.c | 2 +- linux-user/hexagon/cpu_loop.c | 2 +- linux-user/hppa/cpu_loop.c | 2 +- linux-user/i386/cpu_loop.c | 2 +- linux-user/loongarch64/cpu_loop.c | 2 +- linux-user/m68k/cpu_loop.c | 2 +- linux-user/main.c | 5 - linux-user/microblaze/cpu_loop.c | 2 +- linux-user/mips/cpu_loop.c | 2 +- linux-user/openrisc/cpu_loop.c | 2 +- linux-user/ppc/cpu_loop.c | 2 +- linux-user/riscv/cpu_loop.c | 2 +- linux-user/s390x/cpu_loop.c | 2 +- linux-user/sh4/cpu_loop.c | 2 +- linux-user/sparc/cpu_loop.c | 2 +- linux-user/xtensa/cpu_loop.c | 2 +- replay/replay-events.c | 3 +- system/cpu-timers.c | 6 +- system/cpus.c | 15 +- target/arm/cpu-irq.c | 381 ++++++++++++++++++++++++++++ target/arm/cpu.c | 370 --------------------------- target/arm/el2-stubs.c | 37 +++ target/arm/helper.c | 4 + target/arm/tcg/mte_helper.c | 2 +- target/avr/helper.c | 4 +- target/i386/helper.c | 2 + target/i386/hvf/x86hvf.c | 8 +- target/i386/kvm/hyperv.c | 1 - target/i386/kvm/kvm.c | 20 +- target/i386/nvmm/nvmm-accel-ops.c | 8 +- target/i386/nvmm/nvmm-all.c | 19 +- target/i386/tcg/system/seg_helper.c | 13 +- target/i386/tcg/system/svm_helper.c | 2 +- target/i386/whpx/whpx-accel-ops.c | 6 +- target/i386/whpx/whpx-all.c | 23 +- target/openrisc/sys_helper.c | 2 +- target/ppc/helper_regs.c | 2 + target/rx/helper.c | 4 +- target/s390x/tcg/excp_helper.c | 2 +- target/sparc/int32_helper.c | 2 + target/sparc/int64_helper.c | 2 + target/arm/meson.build | 2 + 71 files changed, 645 insertions(+), 566 deletions(-) create mode 100644 target/arm/cpu-irq.c create mode 100644 target/arm/el2-stubs.c
Signature · 1 line
2.51.0
PATCH v3 1/19 target/ppc: limit cpu_interrupt_exittb to system emulation
It is not used by user-mode emulation and is the only caller of cpu_interrupt() in qemu-ppc* binaries.
--- target/ppc/helper_regs.c | 2 ++ 1 file changed, 2 insertions(+)
target/ppc/helper_regs.c +2 -0
@@ -274,6 +274,7 @@ TCGTBCPUState ppc_get_tb_cpu_state(CPUState *cs)return (TCGTBCPUState ){ .pc = env->nip, .flags = hflags_current }; } +#ifndef CONFIG_USER_ONLY void cpu_interrupt_exittb (CPUState *cs) {/*
@@ -285,6 +286,7 @@ void cpu_interrupt_exittb(CPUState *cs) cpu_interrupt(cs, CPU_INTERRUPT_EXITTB); } } +#endif int hreg_store_msr (CPUPPCState *env,target_ulong value,int alter_hv) {
Signature · 1 line
2.51.0
PATCH v3 2/19 target/sparc: limit cpu_check_irqs to system emulation
In reply to Paolo BonziniIt is not used by user-mode emulation and is the only caller of cpu_interrupt() in qemu-sparc* binaries.
--- target/sparc/int32_helper.c | 2 ++ target/sparc/int64_helper.c | 2 ++ 2 files changed, 4 insertions(+)
target/sparc/int32_helper.c +2 -0
@@ -65,6 +65,7 @@ static const char *excp_name_str(int32_t exception_index)return excp_names[exception_index]; } +#if !defined(CONFIG_USER_ONLY )void cpu_check_irqs (CPUSPARCState *env) {CPUState *cs;
@@ -96,6 +97,7 @@ void cpu_check_irqs(CPUSPARCState *env) cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); } } +#endif void sparc_cpu_do_interrupt (CPUState *cs) {
target/sparc/int64_helper.c +2 -0
@@ -62,6 +62,7 @@ static const char * const excp_names[0x80] = { };#endif +#if !defined(CONFIG_USER_ONLY )void cpu_check_irqs (CPUSPARCState *env) {CPUState *cs;
@@ -127,6 +128,7 @@ void cpu_check_irqs(CPUSPARCState *env) cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); } } +#endif void sparc_cpu_do_interrupt (CPUState *cs) {
Signature · 1 line
2.51.0
PATCH v3 3/19 target/i386: limit a20 to system emulation
In reply to Paolo BonziniIt is not used by user-mode emulation and is the only caller of cpu_interrupt() in qemu-i386 and qemu-x86_64.
--- target/i386/helper.c | 2 ++ 1 file changed, 2 insertions(+)
target/i386/helper.c +2 -0
@@ -110,6 +110,7 @@ int cpu_x86_support_mca_broadcast(CPUX86State *env)/* x86 mmu */ /* XXX: add PGE support */ +#ifndef CONFIG_USER_ONLY void x86_cpu_set_a20 (X86CPU *cpu,int a20_state) {CPUX86State *env = &cpu->env;
@@ -129,6 +130,7 @@ void x86_cpu_set_a20(X86CPU *cpu, int a20_state) env->a20_mask = ~(1 << 20) | (a20_state << 20); } } +#endif void cpu_x86_update_cr0 (CPUX86State *env,uint32_t new_cr0) {
Signature · 1 line
2.51.0
PATCH v3 4/19 target-arm: remove uses of cpu_interrupt() for user-mode emulation
In reply to Paolo BonziniArm leaves around some functions that use cpu_interrupt(), even for user-mode emulation when the code is unreachable. Pull out the system-mode implementation to a separate file, and add stubs for CONFIG_USER_ONLY.
--- target/arm/internals.h | 5 + target/arm/cpu-irq.c | 381 +++++++++++++++++++++++++++++++++++++++++ target/arm/cpu.c | 370 --------------------------------------- target/arm/el2-stubs.c | 37 ++++ target/arm/helper.c | 4 + target/arm/meson.build | 2 + 6 files changed, 429 insertions(+), 370 deletions(-) create mode 100644 target/arm/cpu-irq.c create mode 100644 target/arm/el2-stubs.c
target/arm/internals.h +5 -0
@@ -1292,6 +1292,11 @@ static inline const char *aarch32_mode_name(uint32_t psr)return cpu_mode_names[psr &0xf ]; } +/** +* arm_cpu_exec_interrupt(): Implementation of the cpu_exec_inrerrupt hook. +*/ +bool arm_cpu_exec_interrupt (CPUState *cs,int interrupt_request); +/** * arm_cpu_update_virq: Update CPU_INTERRUPT_VIRQ bit in cs->interrupt_request *
target/arm/cpu-irq.c new file +381 -0
381 lines not on this page · raw message
target/arm/cpu.c +0 -370
376 lines not on this page · raw message
target/arm/el2-stubs.c new file +37 -0
@@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* QEMU ARM CPU - user-mode emulation stubs for EL2 interrupts +* +* These should not really be needed, but CP registers for EL2 +* are not elided by user-mode emulation and they call these +* functions. Leave them as stubs until it's cleaned up. +*/ + +#include "qemu/osdep.h" +#include "cpu.h" +#include "internals.h" + +void arm_cpu_update_virq (ARMCPU *cpu) +{ +g_assert_not_reached (); +} + +void arm_cpu_update_vfiq (ARMCPU *cpu) +{ +g_assert_not_reached (); +} + +void arm_cpu_update_vinmi (ARMCPU *cpu) +{ +g_assert_not_reached (); +} + +void arm_cpu_update_vfnmi (ARMCPU *cpu) +{ +g_assert_not_reached (); +} + +void arm_cpu_update_vserr (ARMCPU *cpu) +{ +g_assert_not_reached (); +}
target/arm/helper.c +4 -0
@@ -2868,8 +2868,12 @@ static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,static void omap_wfi_write (CPUARMState *env,const ARMCPRegInfo *ri,uint64_t value) { +#ifdef CONFIG_USER_ONLY +g_assert_not_reached (); +#else /* Wait-for-interrupt (deprecated) */ cpu_interrupt (env_cpu (env),CPU_INTERRUPT_HALT ); +#endif } static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
target/arm/meson.build +2 -0
@@ -26,6 +26,7 @@ arm_user_ss.add(files( 'debug_helper.c', 'helper.c', 'vfp_fpscr.c', + 'el2-stubs.c', )) arm_common_system_ss.add(files('cpu.c'))
@@ -38,6 +39,7 @@ arm_common_system_ss.add(files( 'arm-powerctl.c', 'cortex-regs.c', 'cpregs-pmu.c', + 'cpu-irq.c', 'debug_helper.c', 'helper.c', 'machine.c',
Signature · 1 line
2.51.0
PATCH v3 5/19 user-exec: remove cpu_interrupt() stub
In reply to Paolo Bonzini--- accel/tcg/user-exec.c | 5 ----- 1 file changed, 5 deletions(-)
accel/tcg/user-exec.c +0 -5
@@ -46,11 +46,6 @@ __thread uintptr_t helper_retaddr;//#define DEBUG_SIGNAL -void cpu_interrupt (CPUState *cpu,int mask) -{ -g_assert_not_reached (); -} -/* * Adjust the pc to pass to cpu_restore_state; return the memop type. */
Signature · 1 line
2.51.0
PATCH v3 6/19 treewide: clear bits of cs->interrupt_request with cpu_reset_interrupt()
In reply to Paolo BonziniOpen coding cpu_reset_interrupt() can cause bugs if the BQL is not taken, for example i386 has the call chain kvm_cpu_exec() -> kvm_put_vcpu_events() -> kvm_arch_put_registers().
--- accel/tcg/cpu-exec.c | 6 +++--- hw/core/cpu-system.c | 2 +- target/avr/helper.c | 4 ++-- target/i386/hvf/x86hvf.c | 8 ++++---- target/i386/kvm/kvm.c | 14 +++++++------- target/i386/nvmm/nvmm-all.c | 10 +++++----- target/i386/tcg/system/seg_helper.c | 13 ++++++------- target/i386/tcg/system/svm_helper.c | 2 +- target/i386/whpx/whpx-all.c | 12 ++++++------ target/openrisc/sys_helper.c | 2 +- target/rx/helper.c | 4 ++-- target/s390x/tcg/excp_helper.c | 2 +- 12 files changed, 39 insertions(+), 40 deletions(-)
accel/tcg/cpu-exec.c +3 -3
@@ -784,7 +784,7 @@ static inline bool cpu_handle_interrupt(CPUState *cpu,if (unlikely (cpu_test_interrupt (cpu, ~0 ))) {bql_lock ();if (cpu_test_interrupt (cpu,CPU_INTERRUPT_DEBUG )) { - cpu->interrupt_request &= ~CPU_INTERRUPT_DEBUG ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_DEBUG ); cpu->exception_index =EXCP_DEBUG ;bql_unlock ();return true;
@@ -793,7 +793,7 @@ static inline bool cpu_handle_interrupt(CPUState *cpu,/* Do nothing */ }else if (cpu_test_interrupt (cpu,CPU_INTERRUPT_HALT )) {replay_interrupt (); - cpu->interrupt_request &= ~CPU_INTERRUPT_HALT ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_HALT ); cpu->halted =1 ; cpu->exception_index =EXCP_HLT ;bql_unlock ();
@@ -840,7 +840,7 @@ static inline bool cpu_handle_interrupt(CPUState *cpu, } }if (cpu_test_interrupt (cpu,CPU_INTERRUPT_EXITTB )) { - cpu->interrupt_request &= ~CPU_INTERRUPT_EXITTB ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_EXITTB );/* ensure that no TB jump will be modified as the program flow was changed */ *last_tb =NULL ;
hw/core/cpu-system.c +1 -1
@@ -204,7 +204,7 @@ static int cpu_common_post_load(void *opaque, int version_id)* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the * version_id is increased. */ - cpu->interrupt_request &= ~0x01 ; +cpu_reset_interrupt (cpu,0x01 );tlb_flush (cpu);
target/avr/helper.c +2 -2
@@ -47,7 +47,7 @@ bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request) cs->exception_index =EXCP_RESET ;avr_cpu_do_interrupt (cs); - cs->interrupt_request &= ~CPU_INTERRUPT_RESET ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_RESET );return true; } }
@@ -59,7 +59,7 @@ bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request) env->intsrc &= env->intsrc -1 ;/* clear the interrupt */ if (!env->intsrc) { - cs->interrupt_request &= ~CPU_INTERRUPT_HARD ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_HARD ); }return true; }
target/i386/hvf/x86hvf.c +4 -4
@@ -397,7 +397,7 @@ bool hvf_inject_interrupts(CPUState *cs)if (cpu_test_interrupt (cs,CPU_INTERRUPT_NMI )) {if (!(env->hflags2 &HF2_NMI_MASK ) && !(info &VMCS_INTR_VALID )) { - cs->interrupt_request &= ~CPU_INTERRUPT_NMI ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_NMI ); info =VMCS_INTR_VALID |VMCS_INTR_T_NMI |EXCP02_NMI ;wvmcs (cs->accel->fd,VMCS_ENTRY_INTR_INFO , info); }else {
@@ -409,7 +409,7 @@ bool hvf_inject_interrupts(CPUState *cs) cpu_test_interrupt(cs, CPU_INTERRUPT_HARD) && (env->eflags & IF_MASK) && !(info & VMCS_INTR_VALID)) {int line =cpu_get_pic_interrupt (env); - cs->interrupt_request &= ~CPU_INTERRUPT_HARD ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_HARD );if (line >=0 ) {wvmcs (cs->accel->fd,VMCS_ENTRY_INTR_INFO , line |VMCS_INTR_VALID |VMCS_INTR_T_HWINTR );
@@ -437,7 +437,7 @@ int hvf_process_events(CPUState *cs) }if (cpu_test_interrupt (cs,CPU_INTERRUPT_POLL )) { - cs->interrupt_request &= ~CPU_INTERRUPT_POLL ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_POLL );apic_poll_irq (cpu->apic_state); } if ((cpu_test_interrupt(cs, CPU_INTERRUPT_HARD) &&
@@ -450,7 +450,7 @@ int hvf_process_events(CPUState *cs)do_cpu_sipi (cpu); }if (cpu_test_interrupt (cs,CPU_INTERRUPT_TPR )) { - cs->interrupt_request &= ~CPU_INTERRUPT_TPR ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_TPR );cpu_synchronize_state (cs);apic_handle_tpr_access_report (cpu->apic_state, env->eip, env->tpr_access_type);
target/i386/kvm/kvm.c +7 -7
@@ -5066,7 +5066,7 @@ static int kvm_put_vcpu_events(X86CPU *cpu, int level)*/ events.smi.pending = cs->interrupt_request &CPU_INTERRUPT_SMI ; events.smi.latched_init = cs->interrupt_request &CPU_INTERRUPT_INIT ; - cs->interrupt_request &= ~(CPU_INTERRUPT_INIT |CPU_INTERRUPT_SMI ); +cpu_reset_interrupt (cs,CPU_INTERRUPT_INIT |CPU_INTERRUPT_SMI ); }else {/* Keep these in cs->interrupt_request. */ events.smi.pending =0 ;
@@ -5456,7 +5456,7 @@ void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)if (cpu_test_interrupt (cpu,CPU_INTERRUPT_NMI |CPU_INTERRUPT_SMI )) {if (cpu_test_interrupt (cpu,CPU_INTERRUPT_NMI )) {bql_lock (); - cpu->interrupt_request &= ~CPU_INTERRUPT_NMI ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_NMI );bql_unlock ();DPRINTF ("injected NMI\n" ); ret =kvm_vcpu_ioctl (cpu,KVM_NMI );
@@ -5467,7 +5467,7 @@ void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run) }if (cpu_test_interrupt (cpu,CPU_INTERRUPT_SMI )) {bql_lock (); - cpu->interrupt_request &= ~CPU_INTERRUPT_SMI ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_SMI );bql_unlock ();DPRINTF ("injected SMI\n" ); ret =kvm_vcpu_ioctl (cpu,KVM_SMI );
@@ -5502,7 +5502,7 @@ void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)bql_lock (); - cpu->interrupt_request &= ~CPU_INTERRUPT_HARD ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_HARD ); irq =cpu_get_pic_interrupt (env);if (irq >=0 ) {struct kvm_interrupt intr;
@@ -5597,7 +5597,7 @@ int kvm_arch_process_async_events(CPUState *cs)/* We must not raise CPU_INTERRUPT_MCE if it's not supported. */ assert (env->mcg_cap); - cs->interrupt_request &= ~CPU_INTERRUPT_MCE ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_MCE );kvm_cpu_synchronize_state (cs);
@@ -5627,7 +5627,7 @@ int kvm_arch_process_async_events(CPUState *cs) }if (cpu_test_interrupt (cs,CPU_INTERRUPT_POLL )) { - cs->interrupt_request &= ~CPU_INTERRUPT_POLL ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_POLL );apic_poll_irq (cpu->apic_state); } if ((cpu_test_interrupt(cs, CPU_INTERRUPT_HARD) &&
@@ -5640,7 +5640,7 @@ int kvm_arch_process_async_events(CPUState *cs)do_cpu_sipi (cpu); }if (cpu_test_interrupt (cs,CPU_INTERRUPT_TPR )) { - cs->interrupt_request &= ~CPU_INTERRUPT_TPR ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_TPR );kvm_cpu_synchronize_state (cs);apic_handle_tpr_access_report (cpu->apic_state, env->eip, env->tpr_access_type);
target/i386/nvmm/nvmm-all.c +5 -5
@@ -419,7 +419,7 @@ nvmm_vcpu_pre_run(CPUState *cpu)if (!has_event &&cpu_test_interrupt (cpu,CPU_INTERRUPT_NMI )) {if (nvmm_can_take_nmi (cpu)) { - cpu->interrupt_request &= ~CPU_INTERRUPT_NMI ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_NMI ); event->type =NVMM_VCPU_EVENT_INTR ; event->vector =2 ; has_event = true;
@@ -428,7 +428,7 @@ nvmm_vcpu_pre_run(CPUState *cpu)if (!has_event &&cpu_test_interrupt (cpu,CPU_INTERRUPT_HARD )) {if (nvmm_can_take_int (cpu)) { - cpu->interrupt_request &= ~CPU_INTERRUPT_HARD ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_HARD ); event->type =NVMM_VCPU_EVENT_INTR ; event->vector =cpu_get_pic_interrupt (env); has_event = true;
@@ -437,7 +437,7 @@ nvmm_vcpu_pre_run(CPUState *cpu)/* Don't want SMIs. */ if (cpu_test_interrupt (cpu,CPU_INTERRUPT_SMI )) { - cpu->interrupt_request &= ~CPU_INTERRUPT_SMI ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_SMI ); }if (sync_tpr) {
@@ -697,7 +697,7 @@ nvmm_vcpu_loop(CPUState *cpu)/* set int/nmi windows back to the reset state */ }if (cpu_test_interrupt (cpu,CPU_INTERRUPT_POLL )) { - cpu->interrupt_request &= ~CPU_INTERRUPT_POLL ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_POLL );apic_poll_irq (x86_cpu->apic_state); } if ((cpu_test_interrupt(cpu, CPU_INTERRUPT_HARD) &&
@@ -710,7 +710,7 @@ nvmm_vcpu_loop(CPUState *cpu)do_cpu_sipi (x86_cpu); }if (cpu_test_interrupt (cpu,CPU_INTERRUPT_TPR )) { - cpu->interrupt_request &= ~CPU_INTERRUPT_TPR ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_TPR );nvmm_cpu_synchronize_state (cpu);apic_handle_tpr_access_report (x86_cpu->apic_state, env->eip, env->tpr_access_type);
target/i386/tcg/system/seg_helper.c +6 -7
@@ -178,7 +178,7 @@ bool x86_cpu_exec_interrupt(CPUState *cs, int interrupt_request)*/ switch (interrupt_request) {case CPU_INTERRUPT_POLL : - cs->interrupt_request &= ~CPU_INTERRUPT_POLL ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_POLL );apic_poll_irq (cpu->apic_state);break ;case CPU_INTERRUPT_SIPI :
@@ -186,23 +186,22 @@ bool x86_cpu_exec_interrupt(CPUState *cs, int interrupt_request)break ;case CPU_INTERRUPT_SMI :cpu_svm_check_intercept_param (env,SVM_EXIT_SMI ,0 ,0 ); - cs->interrupt_request &= ~CPU_INTERRUPT_SMI ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_SMI );do_smm_enter (cpu);break ;case CPU_INTERRUPT_NMI :cpu_svm_check_intercept_param (env,SVM_EXIT_NMI ,0 ,0 ); - cs->interrupt_request &= ~CPU_INTERRUPT_NMI ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_NMI ); env->hflags2 |=HF2_NMI_MASK ;do_interrupt_x86_hardirq (env,EXCP02_NMI ,1 );break ;case CPU_INTERRUPT_MCE : - cs->interrupt_request &= ~CPU_INTERRUPT_MCE ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_MCE );do_interrupt_x86_hardirq (env,EXCP12_MCHK ,0 );break ;case CPU_INTERRUPT_HARD :cpu_svm_check_intercept_param (env,SVM_EXIT_INTR ,0 ,0 ); - cs->interrupt_request &= ~(CPU_INTERRUPT_HARD | -CPU_INTERRUPT_VIRQ ); +cpu_reset_interrupt (cs,CPU_INTERRUPT_HARD |CPU_INTERRUPT_VIRQ ); intno =cpu_get_pic_interrupt (env);qemu_log_mask (CPU_LOG_INT ,"Servicing hardware INT=0x%02x\n" , intno);
@@ -215,7 +214,7 @@ bool x86_cpu_exec_interrupt(CPUState *cs, int interrupt_request)qemu_log_mask (CPU_LOG_INT ,"Servicing virtual hardware INT=0x%02x\n" , intno);do_interrupt_x86_hardirq (env, intno,1 ); - cs->interrupt_request &= ~CPU_INTERRUPT_VIRQ ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_VIRQ ); env->int_ctl &= ~V_IRQ_MASK ;break ; }
target/i386/tcg/system/svm_helper.c +1 -1
@@ -824,7 +824,7 @@ void do_vmexit(CPUX86State *env) env->intercept_exceptions =0 ;/* Clears the V_IRQ and V_INTR_MASKING bits inside the processor. */ - cs->interrupt_request &= ~CPU_INTERRUPT_VIRQ ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_VIRQ ); env->int_ctl =0 ;/* Clears the TSC_OFFSET inside the processor. */
target/i386/whpx/whpx-all.c +6 -6
@@ -1471,14 +1471,14 @@ static void whpx_vcpu_pre_run(CPUState *cpu)if (!vcpu->interruption_pending &&cpu_test_interrupt (cpu,CPU_INTERRUPT_NMI |CPU_INTERRUPT_SMI )) {if (cpu_test_interrupt (cpu,CPU_INTERRUPT_NMI )) { - cpu->interrupt_request &= ~CPU_INTERRUPT_NMI ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_NMI ); vcpu->interruptable = false; new_int.InterruptionType = WHvX64PendingNmi; new_int.InterruptionPending =1 ; new_int.InterruptionVector =2 ; }if (cpu_test_interrupt (cpu,CPU_INTERRUPT_SMI )) { - cpu->interrupt_request &= ~CPU_INTERRUPT_SMI ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_SMI ); } }
@@ -1502,7 +1502,7 @@ static void whpx_vcpu_pre_run(CPUState *cpu) vcpu->interruptable && (env->eflags & IF_MASK)) {assert (!new_int.InterruptionPending);if (cpu_test_interrupt (cpu,CPU_INTERRUPT_HARD )) { - cpu->interrupt_request &= ~CPU_INTERRUPT_HARD ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_HARD ); irq =cpu_get_pic_interrupt (env);if (irq >=0 ) { new_int.InterruptionType = WHvX64PendingInterrupt;
@@ -1520,7 +1520,7 @@ static void whpx_vcpu_pre_run(CPUState *cpu) } }else if (vcpu->ready_for_pic_interrupt &&cpu_test_interrupt (cpu,CPU_INTERRUPT_HARD )) { - cpu->interrupt_request &= ~CPU_INTERRUPT_HARD ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_HARD ); irq =cpu_get_pic_interrupt (env);if (irq >=0 ) { reg_names[reg_count] = WHvRegisterPendingEvent;
@@ -1607,7 +1607,7 @@ static void whpx_vcpu_process_async_events(CPUState *cpu) }if (cpu_test_interrupt (cpu,CPU_INTERRUPT_POLL )) { - cpu->interrupt_request &= ~CPU_INTERRUPT_POLL ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_POLL );apic_poll_irq (x86_cpu->apic_state); }
@@ -1623,7 +1623,7 @@ static void whpx_vcpu_process_async_events(CPUState *cpu) }if (cpu_test_interrupt (cpu,CPU_INTERRUPT_TPR )) { - cpu->interrupt_request &= ~CPU_INTERRUPT_TPR ; +cpu_reset_interrupt (cpu,CPU_INTERRUPT_TPR );whpx_cpu_synchronize_state (cpu);apic_handle_tpr_access_report (x86_cpu->apic_state, env->eip, env->tpr_access_type);
target/openrisc/sys_helper.c +1 -1
@@ -196,7 +196,7 @@ void HELPER(mtspr)(CPUOpenRISCState *env, target_ulong spr, target_ulong rb) env->ttmr = (rb & ~TTMR_IP ) | ip; }else {/* Clear IP bit. */ env->ttmr = rb & ~TTMR_IP ; - cs->interrupt_request &= ~CPU_INTERRUPT_TIMER ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_TIMER ); }cpu_openrisc_timer_update (cpu);bql_unlock ();
target/rx/helper.c +2 -2
@@ -63,7 +63,7 @@ void rx_cpu_do_interrupt(CPUState *cs) env->bpsw = save_psw; env->pc = env->fintv; env->psw_ipl =15 ; - cs->interrupt_request &= ~CPU_INTERRUPT_FIR ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_FIR );qemu_set_irq (env->ack, env->ack_irq);qemu_log_mask (CPU_LOG_INT ,"fast interrupt raised\n" ); }else if (do_irq &CPU_INTERRUPT_HARD ) {
@@ -73,7 +73,7 @@ void rx_cpu_do_interrupt(CPUState *cs)cpu_stl_data (env, env->isp, env->pc); env->pc =cpu_ldl_data (env, env->intb + env->ack_irq *4 ); env->psw_ipl = env->ack_ipl; - cs->interrupt_request &= ~CPU_INTERRUPT_HARD ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_HARD );qemu_set_irq (env->ack, env->ack_irq);qemu_log_mask (CPU_LOG_INT ,"interrupt 0x%02x raised\n" , env->ack_irq);
target/s390x/tcg/excp_helper.c +1 -1
@@ -559,7 +559,7 @@ try_deliver:/* we might still have pending interrupts, but not deliverable */ if (!env->pending_int && !qemu_s390_flic_has_any (flic)) { - cs->interrupt_request &= ~CPU_INTERRUPT_HARD ; +cpu_reset_interrupt (cs,CPU_INTERRUPT_HARD ); }/* WAIT PSW during interrupt injection or STOP interrupt */
Signature · 1 line
2.51.0
PATCH v3 7/19 cpu-common: use atomic access for interrupt_request
In reply to Paolo BonziniWrites to interrupt_request used non-atomic accesses, but there are a few cases where the access was not protected by the BQL. Now that there is a full set of helpers, it's easier to guarantee that interrupt_request accesses are fully atomic, so just drop the requirement instead of fixing them.
--- include/hw/core/cpu.h | 1 - hw/core/cpu-common.c | 12 +----------- system/cpus.c | 3 +-- 3 files changed, 2 insertions(+), 14 deletions(-)
include/hw/core/cpu.h +0 -1
@@ -495,7 +495,6 @@ struct CPUState {bool exit_request;int exclusive_context_count;uint32_t cflags_next_tb; -/* updates protected by BQL */ uint32_t interrupt_request;int singlestep_enabled;int64_t icount_budget;
hw/core/cpu-common.c +1 -11
@@ -67,19 +67,9 @@ CPUState *cpu_create(const char *typename)return cpu; } -/* Resetting the IRQ comes from across the code base so we take the -* BQL here if we need to. cpu_interrupt assumes it is held.*/ void cpu_reset_interrupt (CPUState *cpu,int mask) { -bool need_lock = !bql_locked (); - -if (need_lock) { -bql_lock (); - } - cpu->interrupt_request &= ~mask; -if (need_lock) { -bql_unlock (); - } +qatomic_and (&cpu->interrupt_request, ~mask); }void cpu_exit (CPUState *cpu)
system/cpus.c +1 -2
@@ -257,8 +257,7 @@ int64_t cpus_get_elapsed_ticks(void)void cpu_set_interrupt (CPUState *cpu,int mask) {/* Pairs with cpu_test_interrupt(). */ -qatomic_store_release (&cpu->interrupt_request, - cpu->interrupt_request | mask); +qatomic_or (&cpu->interrupt_request, mask); }void generic_handle_interrupt (CPUState *cpu,int mask)
Signature · 1 line
2.51.0
PATCH v3 8/19 cpus: document that qemu_cpu_kick() can be used for BQL-less operation
In reply to Paolo Bonzini--- include/hw/core/cpu.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/hw/core/cpu.h +2 -1
@@ -829,7 +829,8 @@ bool qemu_cpu_is_self(CPUState *cpu);* qemu_cpu_kick: * @cpu: The vCPU to kick. * -* Kicks @cpu's thread. +* Kicks @cpu's thread to exit the accelerator. For accelerators that +* can do that, the target vCPU thread will try not to take the BQL. */ void qemu_cpu_kick (CPUState *cpu);
Signature · 1 line
2.51.0
PATCH v3 9/19 accel: use store_release/load_acquire for cross-thread exit_request
In reply to Paolo BonziniReads and writes cpu->exit_request do not use a load-acquire/store-release pair right now, but this means that cpu_exit() may not write cpu->exit_request after any flags that are read by the vCPU thread.
Probably everything is protected one way or the other by the BQL, because cpu->exit_request leads to the slow path, where the CPU thread often takes the BQL (for example, to go to sleep by waiting on the BQL-protected cpu->halt_cond); but it's not clear, so use load-acquire/store-release consistently.
--- accel/kvm/kvm-all.c | 19 +++++++++---------- accel/tcg/cpu-exec.c | 7 +++++-- accel/tcg/tcg-accel-ops-rr.c | 11 +++++++++-- hw/core/cpu-common.c | 3 ++- target/i386/nvmm/nvmm-all.c | 5 ++--- target/i386/whpx/whpx-all.c | 3 ++- 6 files changed, 29 insertions(+), 19 deletions(-)
accel/kvm/kvm-all.c +9 -10
@@ -3029,10 +3029,6 @@ static void kvm_eat_signals(CPUState *cpu)if (kvm_immediate_exit) {qatomic_set (&cpu->kvm_run->immediate_exit,0 ); -/* Write kvm_run->immediate_exit before the cpu->exit_request -* write in kvm_cpu_exec. -*/ -smp_wmb ();return ; }
@@ -3187,7 +3183,8 @@ int kvm_cpu_exec(CPUState *cpu) }kvm_arch_pre_run (cpu, run); -if (qatomic_read (&cpu->exit_request)) { +/* Corresponding store-release is in cpu_exit. */ +if (qatomic_load_acquire (&cpu->exit_request)) {trace_kvm_interrupt_exit_request ();/* * KVM requires us to reenter the kernel after IO exits to complete
@@ -3197,13 +3194,15 @@ int kvm_cpu_exec(CPUState *cpu)kvm_cpu_kick_self (); } -/* Read cpu->exit_request before KVM_RUN reads run->immediate_exit. -* Matching barrier in kvm_eat_signals. -*/ -smp_rmb (); - run_ret =kvm_vcpu_ioctl (cpu,KVM_RUN ,0 ); +/* +* After writing cpu->exit_request, cpu_exit() sends a signal that writes +* kvm->run->immediate_exit. The signal is already happening after the +* write to cpu->exit_request so, if KVM read kvm->run->immediate_exit +* as true, cpu->exit_request will always read as true. +*/ + attrs =kvm_arch_post_run (cpu, run);#ifdef KVM_HAVE_MCE_INJECTION
accel/tcg/cpu-exec.c +5 -2
@@ -851,8 +851,11 @@ static inline bool cpu_handle_interrupt(CPUState *cpu, }#endif /* !CONFIG_USER_ONLY */ -/* Finally, check if we need to exit to the main loop. */ -if (unlikely (qatomic_read (&cpu->exit_request)) ||icount_exit_request (cpu)) { +/* +* Finally, check if we need to exit to the main loop. +* The corresponding store-release is in cpu_exit. +*/ +if (unlikely (qatomic_load_acquire (&cpu->exit_request)) ||icount_exit_request (cpu)) {qatomic_set (&cpu->exit_request,0 );if (cpu->exception_index ==-1 ) { cpu->exception_index =EXCP_INTERRUPT ;
accel/tcg/tcg-accel-ops-rr.c +9 -2
@@ -242,10 +242,17 @@ static void *rr_cpu_thread_fn(void *arg) cpu = first_cpu; } -while (cpu &&cpu_work_list_empty (cpu) && !cpu->exit_request) { -/* Store rr_current_cpu before evaluating cpu_can_run(). */ +while (cpu &&cpu_work_list_empty (cpu)) { +/* +* Store rr_current_cpu before evaluating cpu->exit_request. +* Pairs with rr_kick_next_cpu(). +*/ qatomic_set_mb (&rr_current_cpu, cpu); +/* Pairs with store-release in cpu_exit. */ +if (qatomic_load_acquire (&cpu->exit_request)) { +break ; + } current_cpu = cpu; qemu_clock_enable(QEMU_CLOCK_VIRTUAL,
hw/core/cpu-common.c +2 -1
@@ -74,7 +74,8 @@ void cpu_reset_interrupt(CPUState *cpu, int mask)void cpu_exit (CPUState *cpu) { -qatomic_set (&cpu->exit_request,1 ); +/* Ensure cpu_exec will see the reason why the exit request was set. */ +qatomic_store_release (&cpu->exit_request, true);/* Ensure cpu_exec will see the exit request after TCG has exited. */ smp_wmb ();qatomic_set (&cpu->neg.icount_decr.u16.high,-1 );
target/i386/nvmm/nvmm-all.c +2 -3
@@ -743,7 +743,8 @@ nvmm_vcpu_loop(CPUState *cpu)nvmm_vcpu_pre_run (cpu); -if (qatomic_read (&cpu->exit_request)) { +/* Corresponding store-release is in cpu_exit. */ +if (qatomic_load_acquire (&cpu->exit_request)) {#if NVMM_USER_VERSION >=2 nvmm_vcpu_stop (vcpu);#else
@@ -751,8 +752,6 @@ nvmm_vcpu_loop(CPUState *cpu)#endif } -/* Read exit_request before the kernel reads the immediate exit flag */ -smp_rmb (); ret =nvmm_vcpu_run (mach, vcpu);if (ret ==-1 ) { error_report("NVMM: Failed to exec a virtual processor,"
target/i386/whpx/whpx-all.c +2 -1
@@ -1714,7 +1714,8 @@ static int whpx_vcpu_run(CPUState *cpu)if (exclusive_step_mode ==WHPX_STEP_NONE ) {whpx_vcpu_pre_run (cpu); -if (qatomic_read (&cpu->exit_request)) { +/* Corresponding store-release is in cpu_exit. */ +if (qatomic_load_acquire (&cpu->exit_request)) {whpx_vcpu_kick (cpu); } }
Signature · 1 line
2.51.0
PATCH v3 10/19 accel: use atomic accesses for exit_request
In reply to Paolo BonziniCPU threads write exit_request as a "note to self" that they need to go out to a slow path. This write happens out of the BQL and can be a data race with another threads' cpu_exit(); use atomic accesses consistently.
While at it, change the source argument from int ("1") to bool ("true").
--- include/hw/core/cpu.h | 9 +++++++++ accel/kvm/kvm-all.c | 2 +- accel/tcg/tcg-accel-ops-mttcg.c | 2 +- accel/tcg/tcg-accel-ops-rr.c | 4 ++-- hw/ppc/spapr_hcall.c | 6 +++--- target/i386/kvm/kvm.c | 6 +++--- target/i386/nvmm/nvmm-accel-ops.c | 2 +- target/i386/nvmm/nvmm-all.c | 2 +- target/i386/whpx/whpx-all.c | 6 +++--- 9 files changed, 24 insertions(+), 15 deletions(-)
include/hw/core/cpu.h +9 -0
@@ -422,6 +422,15 @@ struct qemu_work_item;* valid under cpu_list_lock. * @created: Indicates whether the CPU thread has been successfully created. * @halt_cond: condition variable sleeping threads can wait on. +* @exit_request: Another thread requests the CPU to call qemu_wait_io_event(). +* Should be read only by CPU thread with load-acquire, to synchronize with +* other threads' store-release operation. +* +* In some cases, accelerator-specific code will write exit_request from +* within the same thread, to "bump" the effect of qemu_cpu_kick() to +* the one provided by cpu_exit(), especially when processing interrupt +* flags. In this case, the write and read happen in the same thread +* and the write therefore can use qemu_atomic_set(). * @interrupt_request: Indicates a pending interrupt request. * Only used by system emulation. * @halted: Nonzero if the CPU is in suspended state.
accel/kvm/kvm-all.c +1 -1
@@ -3730,7 +3730,7 @@ int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr) have_sigbus_pending = true; pending_sigbus_addr = addr; pending_sigbus_code = code; -qatomic_set (&cpu->exit_request,1 ); +qatomic_set (&cpu->exit_request, true);return 0 ;#else return 1 ;
accel/tcg/tcg-accel-ops-mttcg.c +1 -1
@@ -85,7 +85,7 @@ static void *mttcg_cpu_thread_fn(void *arg)qemu_guest_random_seed_thread_part2 (cpu->random_seed);/* process any pending work */ - cpu->exit_request =1 ; +qatomic_set (&cpu->exit_request, true); do { if (cpu_can_run(cpu)) {
accel/tcg/tcg-accel-ops-rr.c +2 -2
@@ -212,7 +212,7 @@ static void *rr_cpu_thread_fn(void *arg) cpu = first_cpu;/* process any pending work */ - cpu->exit_request =1 ; +qatomic_set (&cpu->exit_request, true);while (1 ) {/* Only used for icount_enabled() */
@@ -293,7 +293,7 @@ static void *rr_cpu_thread_fn(void *arg)/* Does not need a memory barrier because a spurious wakeup is okay. */ qatomic_set (&rr_current_cpu,NULL ); -if (cpu && cpu->exit_request) { +if (cpu &&qatomic_read (&cpu->exit_request)) {qatomic_set_mb (&cpu->exit_request,0 ); }
hw/ppc/spapr_hcall.c +3 -3
@@ -509,7 +509,7 @@ static target_ulong h_cede(PowerPCCPU *cpu, SpaprMachineState *spapr,if (!cpu_has_work (cs)) { cs->halted =1 ; cs->exception_index =EXCP_HLT ; - cs->exit_request =1 ; +qatomic_set (&cs->exit_request, true);ppc_maybe_interrupt (env); }
@@ -531,7 +531,7 @@ static target_ulong h_confer_self(PowerPCCPU *cpu) } cs->halted =1 ; cs->exception_index =EXCP_HALTED ; - cs->exit_request =1 ; +qatomic_set (&cs->exit_request, true);ppc_maybe_interrupt (&cpu->env);return H_SUCCESS ;
@@ -624,7 +624,7 @@ static target_ulong h_confer(PowerPCCPU *cpu, SpaprMachineState *spapr, } cs->exception_index =EXCP_YIELD ; - cs->exit_request =1 ; +qatomic_set (&cs->exit_request, true);cpu_loop_exit (cs);return H_SUCCESS ;
target/i386/kvm/kvm.c +3 -3
@@ -5486,10 +5486,10 @@ void kvm_arch_pre_run(CPUState *cpu, struct kvm_run *run)if (cpu_test_interrupt (cpu,CPU_INTERRUPT_INIT |CPU_INTERRUPT_TPR )) {if (cpu_test_interrupt (cpu,CPU_INTERRUPT_INIT ) && !(env->hflags &HF_SMM_MASK )) { -qatomic_set (&cpu->exit_request,1 ); +qatomic_set (&cpu->exit_request, true); }if (cpu_test_interrupt (cpu,CPU_INTERRUPT_TPR )) { -qatomic_set (&cpu->exit_request,1 ); +qatomic_set (&cpu->exit_request, true); } }
@@ -5604,7 +5604,7 @@ int kvm_arch_process_async_events(CPUState *cs)if (env->exception_nr ==EXCP08_DBLE ) {/* this means triple fault */ qemu_system_reset_request (SHUTDOWN_CAUSE_GUEST_RESET ); - cs->exit_request =1 ; +qatomic_set (&cs->exit_request, true);return 0 ; }kvm_queue_exception (env,EXCP12_MCHK ,0 ,0 );
target/i386/nvmm/nvmm-accel-ops.c +1 -1
@@ -77,7 +77,7 @@ static void nvmm_start_vcpu_thread(CPUState *cpu)*/ static void nvmm_kick_vcpu_thread (CPUState *cpu) { - cpu->exit_request =1 ; +qatomic_set (&cpu->exit_request, true);cpus_kick_thread (cpu); }
target/i386/nvmm/nvmm-all.c +1 -1
@@ -414,7 +414,7 @@ nvmm_vcpu_pre_run(CPUState *cpu)* or commit pending TPR access. */ if (cpu_test_interrupt (cpu,CPU_INTERRUPT_INIT |CPU_INTERRUPT_TPR )) { - cpu->exit_request =1 ; +qatomic_set (&cpu->exit_request, true); }if (!has_event &&cpu_test_interrupt (cpu,CPU_INTERRUPT_NMI )) {
target/i386/whpx/whpx-all.c +3 -3
@@ -1489,10 +1489,10 @@ static void whpx_vcpu_pre_run(CPUState *cpu)if (cpu_test_interrupt (cpu,CPU_INTERRUPT_INIT |CPU_INTERRUPT_TPR )) {if (cpu_test_interrupt (cpu,CPU_INTERRUPT_INIT ) && !(env->hflags &HF_SMM_MASK )) { - cpu->exit_request =1 ; +qatomic_set (&cpu->exit_request, true); }if (cpu_test_interrupt (cpu,CPU_INTERRUPT_TPR )) { - cpu->exit_request =1 ; +qatomic_set (&cpu->exit_request, true); } }
@@ -1539,7 +1539,7 @@ static void whpx_vcpu_pre_run(CPUState *cpu)if (tpr != vcpu->tpr) { vcpu->tpr = tpr; reg_values[reg_count].Reg64 = tpr; - cpu->exit_request =1 ; +qatomic_set (&cpu->exit_request, true); reg_names[reg_count] = WHvX64RegisterCr8; reg_count +=1 ; }
Signature · 1 line
2.51.0
PATCH v3 11/19 accel/tcg: create a thread-kick function for TCG
In reply to Paolo BonziniRound-robin TCG is calling into cpu_exit() directly. In preparation for making cpu_exit() usable from all accelerators, define a generic thread-kick function for TCG which is used directly in the multi-threaded case, and through CPU_FOREACH in the round-robin case.
Use it also for user-mode emulation, and take the occasion to move the implementation to accel/tcg/user-exec.c.
--- docs/devel/tcg-icount.rst | 2 +- accel/tcg/tcg-accel-ops-mttcg.h | 3 --- accel/tcg/tcg-accel-ops.h | 1 + accel/tcg/cpu-exec.c | 6 ++++++ accel/tcg/tcg-accel-ops-mttcg.c | 5 ----- accel/tcg/tcg-accel-ops-rr.c | 2 +- accel/tcg/tcg-accel-ops.c | 2 +- accel/tcg/user-exec.c | 6 ++++++ bsd-user/main.c | 5 ----- linux-user/main.c | 5 ----- 10 files changed, 16 insertions(+), 21 deletions(-)
docs/devel/tcg-icount.rst +1 -1
@@ -37,7 +37,7 @@ translator starts by allocating a budget of instructions to be executed. The budget of instructions is limited by how long it will be until the next timer will expire. We store this budget as part of a vCPU icount_decr field which shared with the machinery for handling -cpu_exit(). The whole field is checked at the start of every +qemu_cpu_kick(). The whole field is checked at the start of every translated block and will cause a return to the outer loop to deal with whatever caused the exit.
accel/tcg/tcg-accel-ops-mttcg.h +0 -3
@@ -10,9 +10,6 @@#ifndef TCG_ACCEL_OPS_MTTCG_H #define TCG_ACCEL_OPS_MTTCG_H -/* kick MTTCG vCPU thread */ -void mttcg_kick_vcpu_thread (CPUState *cpu); -/* start an mttcg vCPU thread */ void mttcg_start_vcpu_thread (CPUState *cpu);
accel/tcg/tcg-accel-ops.h +1 -0
@@ -18,5 +18,6 @@ void tcg_cpu_destroy(CPUState *cpu);int tcg_cpu_exec (CPUState *cpu);void tcg_handle_interrupt (CPUState *cpu,int mask);void tcg_cpu_init_cflags (CPUState *cpu,bool parallel); +void tcg_kick_vcpu_thread (CPUState *cpu);#endif /* TCG_ACCEL_OPS_H */
accel/tcg/cpu-exec.c +6 -0
@@ -40,6 +40,7 @@#include "exec/replay-core.h" #include "system/tcg.h" #include "exec/helper-proto-common.h" +#include "tcg-accel-ops.h" #include "tb-jmp-cache.h" #include "tb-hash.h" #include "tb-context.h"
@@ -748,6 +749,11 @@ static inline bool cpu_handle_exception(CPUState *cpu, int *ret)return false; } +void tcg_kick_vcpu_thread (CPUState *cpu) +{ +cpu_exit (cpu); +} +static inline bool icount_exit_request (CPUState *cpu) {if (!icount_enabled ()) {
accel/tcg/tcg-accel-ops-mttcg.c +0 -5
@@ -123,11 +123,6 @@ static void *mttcg_cpu_thread_fn(void *arg)return NULL ; } -void mttcg_kick_vcpu_thread (CPUState *cpu) -{ -cpu_exit (cpu); -} -void mttcg_start_vcpu_thread (CPUState *cpu) {char thread_name[VCPU_THREAD_NAME_SIZE ];
accel/tcg/tcg-accel-ops-rr.c +1 -1
@@ -43,7 +43,7 @@ void rr_kick_vcpu_thread(CPUState *unused)CPUState *cpu;CPU_FOREACH (cpu) { -cpu_exit (cpu); +tcg_kick_vcpu_thread (cpu); }; }
accel/tcg/tcg-accel-ops.c +1 -1
@@ -206,7 +206,7 @@ static void tcg_accel_ops_init(AccelClass *ac)if (qemu_tcg_mttcg_enabled ()) { ops->create_vcpu_thread = mttcg_start_vcpu_thread; - ops->kick_vcpu_thread = mttcg_kick_vcpu_thread; + ops->kick_vcpu_thread = tcg_kick_vcpu_thread; ops->handle_interrupt = tcg_handle_interrupt; }else { ops->create_vcpu_thread = rr_start_vcpu_thread;
accel/tcg/user-exec.c +6 -0
@@ -38,6 +38,7 @@#include "qemu/int128.h" #include "trace.h" #include "tcg/tcg-ldst.h" +#include "tcg-accel-ops.h" #include "backend-ldst.h" #include "internal-common.h" #include "tb-internal.h"
@@ -46,6 +47,11 @@ __thread uintptr_t helper_retaddr;//#define DEBUG_SIGNAL +void qemu_cpu_kick (CPUState *cpu) +{ +tcg_kick_vcpu_thread (cpu); +} +/* * Adjust the pc to pass to cpu_restore_state; return the memop type. */
bsd-user/main.c +0 -5
@@ -214,11 +214,6 @@ bool qemu_cpu_is_self(CPUState *cpu)return thread_cpu == cpu; } -void qemu_cpu_kick (CPUState *cpu) -{ -cpu_exit (cpu); -} -/* Assumes contents are already zeroed. */ static void init_task_state (TaskState *ts) {
linux-user/main.c +0 -5
@@ -189,11 +189,6 @@ bool qemu_cpu_is_self(CPUState *cpu)return thread_cpu == cpu; } -void qemu_cpu_kick (CPUState *cpu) -{ -cpu_exit (cpu); -} -void task_settid (TaskState *ts) {if (ts->ts_tid ==0 ) {
Signature · 1 line
2.51.0
PATCH v3 12/19 accel/tcg: inline cpu_exit()
In reply to Paolo BonziniRight now, cpu_exit() is not usable from all accelerators because it includes a TCG-specific thread kick. In fact, cpu_exit() doubles as the TCG thread-kick via tcg_kick_vcpu_thread().
In preparation for changing that, inline cpu_exit() into tcg_kick_vcpu_thread(). The direction of the calls can then be reversed, with an accelerator-independent cpu_exit() calling into qemu_vcpu_kick() rather than the opposite.
--- accel/tcg/cpu-exec.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
accel/tcg/cpu-exec.c +12 -2
@@ -751,7 +751,16 @@ static inline bool cpu_handle_exception(CPUState *cpu, int *ret)void tcg_kick_vcpu_thread (CPUState *cpu) { -cpu_exit (cpu); +/* +* Ensure cpu_exec will see the reason why the exit request was set. +* FIXME: this is not always needed. Other accelerators instead +* read interrupt_request and set exit_request on demand from the +* CPU thread; see kvm_arch_pre_run() for example. +*/ +qatomic_store_release (&cpu->exit_request, true); + +/* Ensure cpu_exec will see the exit request after TCG has exited. */ +qatomic_store_release (&cpu->neg.icount_decr.u16.high,-1 ); }static inline bool icount_exit_request (CPUState *cpu)
@@ -780,7 +789,8 @@ static inline bool cpu_handle_interrupt(CPUState *cpu,/* Clear the interrupt flag now since we're processing * cpu->interrupt_request and cpu->exit_request. * Ensure zeroing happens before reading cpu->exit_request or -* cpu->interrupt_request (see also smp_wmb in cpu_exit()) +* cpu->interrupt_request (see also store-release in +* tcg_kick_vcpu_thread()) */ qatomic_set_mb (&cpu->neg.icount_decr.u16.high,0 );
Signature · 1 line
2.51.0
PATCH v3 13/19 cpus: remove TCG-ism from cpu_exit()
In reply to Paolo BonziniNow that TCG has its own kick function, make cpu_exit() do the right kick for all accelerators.
--- hw/core/cpu-common.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
hw/core/cpu-common.c +1 -3
@@ -76,9 +76,7 @@ void cpu_exit(CPUState *cpu) {/* Ensure cpu_exec will see the reason why the exit request was set. */ qatomic_store_release (&cpu->exit_request, true); -/* Ensure cpu_exec will see the exit request after TCG has exited. */ -smp_wmb (); -qatomic_set (&cpu->neg.icount_decr.u16.high,-1 ); +qemu_cpu_kick (cpu); }static int cpu_common_gdb_read_register (CPUState *cpu,GByteArray *buf,int reg)
Signature · 1 line
2.51.0
PATCH v3 14/19 cpus: properly kick CPUs out of inner execution loop
In reply to Paolo BonziniNow that cpu_exit() actually kicks all accelerators, use it whenever the message to another thread is processed in qemu_wait_io_event().
--- cpu-common.c | 3 ++- hw/ppc/ppc.c | 2 ++ hw/ppc/spapr_hcall.c | 7 +++---- hw/ppc/spapr_rtas.c | 2 +- replay/replay-events.c | 3 ++- system/cpu-timers.c | 6 +++--- system/cpus.c | 5 +++-- target/arm/tcg/mte_helper.c | 2 +- target/i386/kvm/hyperv.c | 1 - 9 files changed, 17 insertions(+), 14 deletions(-)
cpu-common.c +2 -1
@@ -137,7 +137,8 @@ static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi) wi->done = false;qemu_mutex_unlock (&cpu->work_mutex); -qemu_cpu_kick (cpu); +/* exit the inner loop and reach qemu_wait_io_event_common(). */ +cpu_exit (cpu); } void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
hw/ppc/ppc.c +2 -0
@@ -190,6 +190,7 @@ static void ppc970_set_irq(void *opaque, int pin, int level)if (level) {trace_ppc_irq_cpu ("stop" ); cs->halted =1 ; +cpu_exit (cs); }else {trace_ppc_irq_cpu ("restart" ); cs->halted =0 ;
@@ -386,6 +387,7 @@ static void ppc40x_set_irq(void *opaque, int pin, int level)if (level) {trace_ppc_irq_cpu ("stop" ); cs->halted =1 ; +cpu_exit (cs); }else {trace_ppc_irq_cpu ("restart" ); cs->halted =0 ;
hw/ppc/spapr_hcall.c +3 -4
@@ -509,8 +509,8 @@ static target_ulong h_cede(PowerPCCPU *cpu, SpaprMachineState *spapr,if (!cpu_has_work (cs)) { cs->halted =1 ; cs->exception_index =EXCP_HLT ; -qatomic_set (&cs->exit_request, true);ppc_maybe_interrupt (env); +cpu_exit (cs); }return H_SUCCESS ;
@@ -531,8 +531,8 @@ static target_ulong h_confer_self(PowerPCCPU *cpu) } cs->halted =1 ; cs->exception_index =EXCP_HALTED ; -qatomic_set (&cs->exit_request, true);ppc_maybe_interrupt (&cpu->env); +cpu_exit (cs);return H_SUCCESS ; }
@@ -624,8 +624,7 @@ static target_ulong h_confer(PowerPCCPU *cpu, SpaprMachineState *spapr, } cs->exception_index =EXCP_YIELD ; -qatomic_set (&cs->exit_request, true); -cpu_loop_exit (cs); +cpu_exit (cs);return H_SUCCESS ; }
hw/ppc/spapr_rtas.c +1 -1
@@ -221,7 +221,7 @@ static void rtas_stop_self(PowerPCCPU *cpu, SpaprMachineState *spapr, cs->halted =1 ;ppc_store_lpcr (cpu, env->spr[SPR_LPCR ] & ~pcc->lpcr_pm);kvmppc_set_reg_ppc_online (cpu,0 ); -qemu_cpu_kick (cs); +cpu_exit (cs); } static void rtas_ibm_suspend_me(PowerPCCPU *cpu, SpaprMachineState *spapr,
replay/replay-events.c +2 -1
@@ -118,7 +118,8 @@ void replay_add_event(ReplayAsyncEventKind event_kind,g_assert (replay_mutex_locked ());QTAILQ_INSERT_TAIL (&events_list, event, events); -qemu_cpu_kick (first_cpu); +/* Kick the TCG thread out of tcg_cpu_exec(). */ +cpu_exit (first_cpu); }void replay_bh_schedule_event (QEMUBH *bh)
system/cpu-timers.c +3 -3
@@ -246,14 +246,14 @@ void qemu_timer_notify_cb(void *opaque, QEMUClockType type)if (qemu_in_vcpu_thread ()) {/* -* A CPU is currently running; kick it back out to the +* A CPU is currently running; send it out of the * tcg_cpu_exec() loop so it will recalculate its * icount deadline immediately. */ -qemu_cpu_kick (current_cpu); +cpu_exit (current_cpu); }else if (first_cpu) {/* -* qemu_cpu_kick is not enough to kick a halted CPU out of +* cpu_exit() is not enough to kick a halted CPU out of * qemu_tcg_wait_io_event. async_run_on_cpu, instead, * causes cpu_thread_is_idle to return false. This way, * handle_icount_deadline can run.
system/cpus.c +3 -2
@@ -604,7 +604,7 @@ void cpu_pause(CPUState *cpu)qemu_cpu_stop (cpu, true); }else { cpu->stop = true; -qemu_cpu_kick (cpu); +cpu_exit (cpu); } }
@@ -644,6 +644,7 @@ void pause_all_vcpus(void)while (!all_vcpus_paused ()) {qemu_cond_wait (&qemu_pause_cond, &bql); +/* FIXME: is this needed? */ CPU_FOREACH (cpu) {qemu_cpu_kick (cpu); }
@@ -672,7 +673,7 @@ void cpu_remove_sync(CPUState *cpu) { cpu->stop = true; cpu->unplug = true; -qemu_cpu_kick (cpu); +cpu_exit (cpu);bql_unlock ();qemu_thread_join (cpu->thread);bql_lock ();
target/arm/tcg/mte_helper.c +1 -1
@@ -591,7 +591,7 @@ static void mte_async_check_fail(CPUARMState *env, uint64_t dirty_ptr,* which is rather sooner than "normal". But the alternative * is waiting until the next syscall. */ -qemu_cpu_kick (env_cpu (env)); +cpu_exit (env_cpu (env));#endif }
target/i386/kvm/hyperv.c +0 -1
@@ -81,7 +81,6 @@ int kvm_hv_handle_exit(X86CPU *cpu, struct kvm_hyperv_exit *exit)* necessary because memory hierarchy is being changed */ async_safe_run_on_cpu (CPU (cpu), async_synic_update,RUN_ON_CPU_NULL ); -cpu_exit (CPU (cpu));return EXCP_INTERRUPT ;case KVM_EXIT_HYPERV_HCALL : {
Signature · 1 line
2.51.0
Re: [PATCH 14/19] cpus: properly kick CPUs out of inner execution loop
Quoted 4 lines · Paolo Bonzini
Now that
cpu_exit()actually kicks all accelerators, use it whenever the message to another thread is processed inqemu_wait_io_event().Signed-off-by Paolo Bonzini<[email protected]>
Quoted 11 lines
--- cpu-common.c | 3 ++- hw/ppc/ppc.c | 2 ++ hw/ppc/spapr_hcall.c | 7 +++---- hw/ppc/spapr_rtas.c | 2 +- replay/replay-events.c | 3 ++- system/cpu-timers.c | 6 +++--- system/cpus.c | 5 +++-- target/arm/tcg/mte_helper.c | 2 +- target/i386/kvm/hyperv.c | 1 - 9 files changed, 17 insertions(+), 14 deletions(-)
r~
PATCH v3 15/19 treewide: rename qemu_wait_io_event/qemu_wait_io_event_common
In reply to Paolo BonziniDo so before extending it to the user-mode emulators, where there is no such thing as an "I/O thread".
--- include/hw/core/cpu.h | 2 +- include/system/cpus.h | 4 ++-- accel/dummy-cpus.c | 2 +- accel/hvf/hvf-accel-ops.c | 2 +- accel/kvm/kvm-accel-ops.c | 2 +- accel/tcg/tcg-accel-ops-mttcg.c | 2 +- accel/tcg/tcg-accel-ops-rr.c | 4 ++-- cpu-common.c | 2 +- system/cpus.c | 6 +++--- target/i386/nvmm/nvmm-accel-ops.c | 2 +- target/i386/whpx/whpx-accel-ops.c | 2 +- 11 files changed, 15 insertions(+), 15 deletions(-)
include/hw/core/cpu.h +1 -1
@@ -422,7 +422,7 @@ struct qemu_work_item;* valid under cpu_list_lock. * @created: Indicates whether the CPU thread has been successfully created. * @halt_cond: condition variable sleeping threads can wait on. -* @exit_request: Another thread requests the CPU to call qemu_wait_io_event(). +* @exit_request: Another thread requests the CPU to call qemu_process_cpu_events(). * Should be read only by CPU thread with load-acquire, to synchronize with * other threads' store-release operation. *
include/system/cpus.h +2 -2
@@ -17,8 +17,8 @@ bool cpu_work_list_empty(CPUState *cpu);bool cpu_thread_is_idle (CPUState *cpu);bool all_cpu_threads_idle (void );bool cpu_can_run (CPUState *cpu); -void qemu_wait_io_event_common (CPUState *cpu); -void qemu_wait_io_event (CPUState *cpu); +void qemu_process_cpu_events_common (CPUState *cpu); +void qemu_process_cpu_events (CPUState *cpu);void cpu_thread_signal_created (CPUState *cpu);void cpu_thread_signal_destroyed (CPUState *cpu);void cpu_handle_guest_debug (CPUState *cpu);
accel/dummy-cpus.c +1 -1
@@ -57,7 +57,7 @@ static void *dummy_cpu_thread_fn(void *arg)qemu_sem_wait (&cpu->sem);#endif bql_lock (); -qemu_wait_io_event (cpu); +qemu_process_cpu_events (cpu); }while (!cpu->unplug);bql_unlock ();
accel/hvf/hvf-accel-ops.c +1 -1
@@ -198,7 +198,7 @@ static void *hvf_cpu_thread_fn(void *arg)cpu_handle_guest_debug (cpu); } } -qemu_wait_io_event (cpu); +qemu_process_cpu_events (cpu); }while (!cpu->unplug ||cpu_can_run (cpu));hvf_vcpu_destroy (cpu);
accel/kvm/kvm-accel-ops.c +1 -1
@@ -53,7 +53,7 @@ static void *kvm_vcpu_thread_fn(void *arg)cpu_handle_guest_debug (cpu); } } -qemu_wait_io_event (cpu); +qemu_process_cpu_events (cpu); }while (!cpu->unplug ||cpu_can_run (cpu));kvm_destroy_vcpu (cpu);
accel/tcg/tcg-accel-ops-mttcg.c +1 -1
@@ -113,7 +113,7 @@ static void *mttcg_cpu_thread_fn(void *arg) } } -qemu_wait_io_event (cpu); +qemu_process_cpu_events (cpu); }while (!cpu->unplug ||cpu_can_run (cpu));tcg_cpu_destroy (cpu);
accel/tcg/tcg-accel-ops-rr.c +2 -2
@@ -117,7 +117,7 @@ static void rr_wait_io_event(void)rr_start_kick_timer ();CPU_FOREACH (cpu) { -qemu_wait_io_event_common (cpu); +qemu_process_cpu_events_common (cpu); } }
@@ -203,7 +203,7 @@ static void *rr_cpu_thread_fn(void *arg)/* process any pending work */ CPU_FOREACH (cpu) { current_cpu = cpu; -qemu_wait_io_event_common (cpu); +qemu_process_cpu_events_common (cpu); } }
cpu-common.c +1 -1
@@ -137,7 +137,7 @@ static void queue_work_on_cpu(CPUState *cpu, struct qemu_work_item *wi) wi->done = false;qemu_mutex_unlock (&cpu->work_mutex); -/* exit the inner loop and reach qemu_wait_io_event_common(). */ +/* exit the inner loop and reach qemu_process_cpu_events_common(). */ cpu_exit (cpu); }
system/cpus.c +3 -3
@@ -450,7 +450,7 @@ static void qemu_cpu_stop(CPUState *cpu, bool exit)qemu_cond_broadcast (&qemu_pause_cond); } -void qemu_wait_io_event_common (CPUState *cpu) +void qemu_process_cpu_events_common (CPUState *cpu) {qatomic_set_mb (&cpu->thread_kicked, false);if (cpu->stop) {
@@ -459,7 +459,7 @@ void qemu_wait_io_event_common(CPUState *cpu)process_queued_cpu_work (cpu); } -void qemu_wait_io_event (CPUState *cpu) +void qemu_process_cpu_events (CPUState *cpu) {bool slept = false;
@@ -474,7 +474,7 @@ void qemu_wait_io_event(CPUState *cpu)qemu_plugin_vcpu_resume_cb (cpu); } -qemu_wait_io_event_common (cpu); +qemu_process_cpu_events_common (cpu); }void cpus_kick_thread (CPUState *cpu)
target/i386/nvmm/nvmm-accel-ops.c +1 -1
@@ -51,7 +51,7 @@ static void *qemu_nvmm_cpu_thread_fn(void *arg)while (cpu_thread_is_idle (cpu)) {qemu_cond_wait_bql (cpu->halt_cond); } -qemu_wait_io_event_common (cpu); +qemu_process_cpu_events_common (cpu); }while (!cpu->unplug ||cpu_can_run (cpu));nvmm_destroy_vcpu (cpu);
target/i386/whpx/whpx-accel-ops.c +1 -1
@@ -51,7 +51,7 @@ static void *whpx_cpu_thread_fn(void *arg)while (cpu_thread_is_idle (cpu)) {qemu_cond_wait_bql (cpu->halt_cond); } -qemu_wait_io_event_common (cpu); +qemu_process_cpu_events_common (cpu); }while (!cpu->unplug ||cpu_can_run (cpu));whpx_destroy_vcpu (cpu);
Signature · 1 line
2.51.0
Re: [PATCH 15/19] treewide: rename qemu_wait_io_event/qemu_wait_io_event_common
Quoted 4 lines · Paolo Bonzini
Do so before extending it to the user-mode emulators, where there is no such thing as an "I/O thread".
Signed-off-by Paolo Bonzini<[email protected]>
Quoted 13 lines
--- include/hw/core/cpu.h | 2 +- include/system/cpus.h | 4 ++-- accel/dummy-cpus.c | 2 +- accel/hvf/hvf-accel-ops.c | 2 +- accel/kvm/kvm-accel-ops.c | 2 +- accel/tcg/tcg-accel-ops-mttcg.c | 2 +- accel/tcg/tcg-accel-ops-rr.c | 4 ++-- cpu-common.c | 2 +- system/cpus.c | 6 +++--- target/i386/nvmm/nvmm-accel-ops.c | 2 +- target/i386/whpx/whpx-accel-ops.c | 2 +- 11 files changed, 15 insertions(+), 15 deletions(-)
r~
PATCH v3 16/19 bsd-user, linux-user: introduce qemu_process_cpu_events
In reply to Paolo BonziniAdd a user-mode emulation version of the function. More will be added later, for now it is just process_queued_cpu_work.
--- bsd-user/aarch64/target_arch_cpu.h | 2 +- bsd-user/arm/target_arch_cpu.h | 2 +- bsd-user/i386/target_arch_cpu.h | 2 +- bsd-user/riscv/target_arch_cpu.h | 2 +- bsd-user/x86_64/target_arch_cpu.h | 2 +- include/hw/core/cpu.h | 9 +++++++++ include/system/cpus.h | 1 - accel/tcg/user-exec.c | 5 +++++ linux-user/aarch64/cpu_loop.c | 2 +- linux-user/alpha/cpu_loop.c | 2 +- linux-user/arm/cpu_loop.c | 2 +- linux-user/hexagon/cpu_loop.c | 2 +- linux-user/hppa/cpu_loop.c | 2 +- linux-user/i386/cpu_loop.c | 2 +- linux-user/loongarch64/cpu_loop.c | 2 +- linux-user/m68k/cpu_loop.c | 2 +- linux-user/microblaze/cpu_loop.c | 2 +- linux-user/mips/cpu_loop.c | 2 +- linux-user/openrisc/cpu_loop.c | 2 +- linux-user/ppc/cpu_loop.c | 2 +- linux-user/riscv/cpu_loop.c | 2 +- linux-user/s390x/cpu_loop.c | 2 +- linux-user/sh4/cpu_loop.c | 2 +- linux-user/sparc/cpu_loop.c | 2 +- linux-user/xtensa/cpu_loop.c | 2 +- 25 files changed, 36 insertions(+), 23 deletions(-)
bsd-user/aarch64/target_arch_cpu.h +1 -1
@@ -54,7 +54,7 @@ static inline G_NORETURN void target_cpu_loop(CPUARMState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_SWI :
bsd-user/arm/target_arch_cpu.h +1 -1
@@ -46,7 +46,7 @@ static inline G_NORETURN void target_cpu_loop(CPUARMState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_UDEF :case EXCP_NOCP :
bsd-user/i386/target_arch_cpu.h +1 -1
@@ -113,7 +113,7 @@ static inline G_NORETURN void target_cpu_loop(CPUX86State *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case 0x80 : {
bsd-user/riscv/target_arch_cpu.h +1 -1
@@ -49,7 +49,7 @@ static inline G_NORETURN void target_cpu_loop(CPURISCVState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs); signo =0 ;
bsd-user/x86_64/target_arch_cpu.h +1 -1
@@ -121,7 +121,7 @@ static inline G_NORETURN void target_cpu_loop(CPUX86State *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_SYSCALL :
include/hw/core/cpu.h +9 -0
@@ -1145,6 +1145,15 @@ AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx); G_NORETURN void cpu_abort(CPUState *cpu, const char *fmt, ...) G_GNUC_PRINTF(2, 3); +/** +* qemu_process_cpu_events: +* @cpu: CPU that left the execution loop +* +* Perform accelerator-independent work after the CPU has left +* the inner execution loop. +*/ +void qemu_process_cpu_events (CPUState *cpu); +/* $(top_srcdir)/cpu.c */ void cpu_class_init_props (DeviceClass *dc);void cpu_exec_class_post_init (CPUClass *cc);
include/system/cpus.h +0 -1
@@ -18,7 +18,6 @@ bool cpu_thread_is_idle(CPUState *cpu);bool all_cpu_threads_idle (void );bool cpu_can_run (CPUState *cpu);void qemu_process_cpu_events_common (CPUState *cpu); -void qemu_process_cpu_events (CPUState *cpu);void cpu_thread_signal_created (CPUState *cpu);void cpu_thread_signal_destroyed (CPUState *cpu);void cpu_handle_guest_debug (CPUState *cpu);
accel/tcg/user-exec.c +5 -0
@@ -52,6 +52,11 @@ void qemu_cpu_kick(CPUState *cpu)tcg_kick_vcpu_thread (cpu); } +void qemu_process_cpu_events (CPUState *cpu) +{ +process_queued_cpu_work (cpu); +} +/* * Adjust the pc to pass to cpu_restore_state; return the memop type. */
linux-user/aarch64/cpu_loop.c +1 -1
@@ -38,7 +38,7 @@ void cpu_loop(CPUARMState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_SWI :
linux-user/alpha/cpu_loop.c +1 -1
@@ -35,7 +35,7 @@ void cpu_loop(CPUAlphaState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_RESET :
linux-user/arm/cpu_loop.c +1 -1
@@ -295,7 +295,7 @@ void cpu_loop(CPUARMState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_UDEF :
linux-user/hexagon/cpu_loop.c +1 -1
@@ -36,7 +36,7 @@ void cpu_loop(CPUHexagonState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_INTERRUPT :
linux-user/hppa/cpu_loop.c +1 -1
@@ -119,7 +119,7 @@ void cpu_loop(CPUHPPAState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_SYSCALL :
linux-user/i386/cpu_loop.c +1 -1
@@ -214,7 +214,7 @@ void cpu_loop(CPUX86State *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case 0x80 :
linux-user/loongarch64/cpu_loop.c +1 -1
@@ -27,7 +27,7 @@ void cpu_loop(CPULoongArchState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_INTERRUPT :
linux-user/m68k/cpu_loop.c +1 -1
@@ -33,7 +33,7 @@ void cpu_loop(CPUM68KState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_ILLEGAL :
linux-user/microblaze/cpu_loop.c +1 -1
@@ -32,7 +32,7 @@ void cpu_loop(CPUMBState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_INTERRUPT :
linux-user/mips/cpu_loop.c +1 -1
@@ -74,7 +74,7 @@ void cpu_loop(CPUMIPSState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_SYSCALL :
linux-user/openrisc/cpu_loop.c +1 -1
@@ -33,7 +33,7 @@ void cpu_loop(CPUOpenRISCState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_SYSCALL :
linux-user/ppc/cpu_loop.c +1 -1
@@ -77,7 +77,7 @@ void cpu_loop(CPUPPCState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs); arch_interrupt = true;switch (trapnr) {
linux-user/riscv/cpu_loop.c +1 -1
@@ -36,7 +36,7 @@ void cpu_loop(CPURISCVState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_INTERRUPT :
linux-user/s390x/cpu_loop.c +1 -1
@@ -64,7 +64,7 @@ void cpu_loop(CPUS390XState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case EXCP_INTERRUPT :
linux-user/sh4/cpu_loop.c +1 -1
@@ -34,7 +34,7 @@ void cpu_loop(CPUSH4State *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case 0x160 :
linux-user/sparc/cpu_loop.c +1 -1
@@ -220,7 +220,7 @@ void cpu_loop (CPUSPARCState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs);switch (trapnr) {case TARGET_TT_SYSCALL :
linux-user/xtensa/cpu_loop.c +1 -1
@@ -133,7 +133,7 @@ void cpu_loop(CPUXtensaState *env)cpu_exec_start (cs); trapnr =cpu_exec (cs);cpu_exec_end (cs); -process_queued_cpu_work (cs); +qemu_process_cpu_events (cs); env->sregs[PS ] &= ~PS_EXCM ;switch (trapnr) {
Signature · 1 line
2.51.0
PATCH v3 17/19 cpus: clear exit_request in qemu_process_cpu_events
In reply to Paolo BonziniMake the code common to all accelerators: after seeing cpu->exit_request set to true, accelerator code needs to reach qemu_process_cpu_events_common().
So for the common cases where they use qemu_process_cpu_events(), go ahead and clear it in there. Note that the cheap qatomic_set() is enough because at this point the thread has taken the BQL; qatomic_set_mb() is not needed. In particular, this is the ordering of the communication between I/O and vCPU threads is always the same.
In the I/O thread:
(a) store other memory locations that will be checked if cpu->exit_request
or cpu->interrupt_request is 1 (for example cpu->stop or cpu->work_list
for cpu->exit_request)(b) cpu_exit(): store-release cpu->exit_request, or
(b) cpu_interrupt(): store-release cpu->interrupt_request
Quoted 1 line
Quoted 1 line
Quoted 1 line
at this point,
cpu->halt_condis broadcast and the BQL released
(c) do the accelerator-specific kick (e.g. write icount_decr for TCG, pthread_kill for KVM, etc.)
In the vCPU thread instead the opposite order is respected:
(c) the accelerator's execution loop exits thanks to the kick
(b) then the inner execution loop checks cpu->interrupt_request
and cpu->exit_request. If needed cpu->interrupt_request is
converted into cpu->exit_request when work is needed outside
the execution loop.(a) then the other memory locations are checked. Some may need to be read under the BQL, but the vCPU thread may also take other locks (e.g. for queued work items) or none at all.
qatomic_set_mb() would only be needed if the halt sleep was done outside the BQL (though in that case, cpu->exit_request probably would be replaced by a QemuEvent or something like that).
--- accel/kvm/kvm-all.c | 2 -- accel/tcg/cpu-exec.c | 1 - accel/tcg/tcg-accel-ops-rr.c | 9 +++++++-- accel/tcg/tcg-accel-ops.c | 2 -- accel/tcg/user-exec.c | 1 + system/cpus.c | 1 + target/i386/nvmm/nvmm-all.c | 2 -- target/i386/whpx/whpx-all.c | 2 -- 8 files changed, 9 insertions(+), 11 deletions(-)
accel/kvm/kvm-all.c +0 -2
@@ -3155,7 +3155,6 @@ int kvm_cpu_exec(CPUState *cpu)trace_kvm_cpu_exec ();if (kvm_arch_process_async_events (cpu)) { -qatomic_set (&cpu->exit_request,0 );return EXCP_HLT ; }
@@ -3345,7 +3344,6 @@ int kvm_cpu_exec(CPUState *cpu)vm_stop (RUN_STATE_INTERNAL_ERROR ); } -qatomic_set (&cpu->exit_request,0 );return ret; }
accel/tcg/cpu-exec.c +0 -1
@@ -872,7 +872,6 @@ static inline bool cpu_handle_interrupt(CPUState *cpu,* The corresponding store-release is in cpu_exit. */ if (unlikely (qatomic_load_acquire (&cpu->exit_request)) ||icount_exit_request (cpu)) { -qatomic_set (&cpu->exit_request,0 );if (cpu->exception_index ==-1 ) { cpu->exception_index =EXCP_INTERRUPT ; }
accel/tcg/tcg-accel-ops-rr.c +7 -2
@@ -293,8 +293,13 @@ static void *rr_cpu_thread_fn(void *arg)/* Does not need a memory barrier because a spurious wakeup is okay. */ qatomic_set (&rr_current_cpu,NULL ); -if (cpu &&qatomic_read (&cpu->exit_request)) { -qatomic_set_mb (&cpu->exit_request,0 ); +if (cpu) { +/* +* This could even reset exit_request for all CPUs, but in practice +* races between CPU exits and changes to "cpu" are so rare that +* there's no advantage in doing so. +*/ +qatomic_set (&cpu->exit_request, false); }if (icount_enabled () &&all_cpu_threads_idle ()) {
accel/tcg/tcg-accel-ops.c +0 -2
@@ -82,8 +82,6 @@ int tcg_cpu_exec(CPUState *cpu) ret =cpu_exec (cpu);cpu_exec_end (cpu); -qatomic_set_mb (&cpu->exit_request,0 ); -return ret; }
accel/tcg/user-exec.c +1 -0
@@ -54,6 +54,7 @@ void qemu_cpu_kick(CPUState *cpu)void qemu_process_cpu_events (CPUState *cpu) { +qatomic_set (&cpu->exit_request, false);process_queued_cpu_work (cpu); }
system/cpus.c +1 -0
@@ -463,6 +463,7 @@ void qemu_process_cpu_events(CPUState *cpu) {bool slept = false; +qatomic_set (&cpu->exit_request, false);while (cpu_thread_is_idle (cpu)) {if (!slept) { slept = true;
target/i386/nvmm/nvmm-all.c +0 -2
@@ -817,8 +817,6 @@ nvmm_vcpu_loop(CPUState *cpu)cpu_exec_end (cpu);bql_lock (); -qatomic_set (&cpu->exit_request, false); -return ret <0 ; }
target/i386/whpx/whpx-all.c +0 -2
@@ -2050,8 +2050,6 @@ static int whpx_vcpu_run(CPUState *cpu)whpx_last_vcpu_stopping (cpu); } -qatomic_set (&cpu->exit_request, false); -return ret <0 ; }
Signature · 1 line
2.51.0
PATCH v3 18/19 accel: make all calls to qemu_process_cpu_events look the same
In reply to Paolo BonziniThere is no reason for some accelerators to use qemu_process_cpu_events_common (which is separated from qemu_process_cpu_events() specifically for round robin TCG). They can also check for events directly on the first pass through the loop, instead of setting cpu->exit_request to true.
--- accel/dummy-cpus.c | 2 +- accel/hvf/hvf-accel-ops.c | 2 +- accel/kvm/kvm-accel-ops.c | 3 ++- accel/tcg/tcg-accel-ops-mttcg.c | 7 ++--- accel/tcg/tcg-accel-ops-rr.c | 43 ++++++++++++++----------------- target/i386/nvmm/nvmm-accel-ops.c | 6 ++--- target/i386/whpx/whpx-accel-ops.c | 6 ++--- 7 files changed, 30 insertions(+), 39 deletions(-)
accel/dummy-cpus.c +1 -1
@@ -43,6 +43,7 @@ static void *dummy_cpu_thread_fn(void *arg)qemu_guest_random_seed_thread_part2 (cpu->random_seed); do { + qemu_process_cpu_events(cpu); bql_unlock(); #ifndef _WIN32 do {
@@ -57,7 +58,6 @@ static void *dummy_cpu_thread_fn(void *arg)qemu_sem_wait (&cpu->sem);#endif bql_lock (); -qemu_process_cpu_events (cpu); }while (!cpu->unplug);bql_unlock ();
accel/hvf/hvf-accel-ops.c +1 -1
@@ -192,13 +192,13 @@ static void *hvf_cpu_thread_fn(void *arg)qemu_guest_random_seed_thread_part2 (cpu->random_seed);do { +qemu_process_cpu_events (cpu);if (cpu_can_run (cpu)) { r =hvf_vcpu_exec (cpu);if (r ==EXCP_DEBUG ) {cpu_handle_guest_debug (cpu); } } -qemu_process_cpu_events (cpu); }while (!cpu->unplug ||cpu_can_run (cpu));hvf_vcpu_destroy (cpu);
accel/kvm/kvm-accel-ops.c +2 -1
@@ -47,13 +47,14 @@ static void *kvm_vcpu_thread_fn(void *arg)qemu_guest_random_seed_thread_part2 (cpu->random_seed);do { +qemu_process_cpu_events (cpu); +if (cpu_can_run (cpu)) { r =kvm_cpu_exec (cpu);if (r ==EXCP_DEBUG ) {cpu_handle_guest_debug (cpu); } } -qemu_process_cpu_events (cpu); }while (!cpu->unplug ||cpu_can_run (cpu));kvm_destroy_vcpu (cpu);
accel/tcg/tcg-accel-ops-mttcg.c +2 -5
@@ -84,10 +84,9 @@ static void *mttcg_cpu_thread_fn(void *arg)cpu_thread_signal_created (cpu);qemu_guest_random_seed_thread_part2 (cpu->random_seed); -/* process any pending work */ -qatomic_set (&cpu->exit_request, true); - do { + qemu_process_cpu_events(cpu); + if (cpu_can_run(cpu)) { int r; bql_unlock();
@@ -112,8 +111,6 @@ static void *mttcg_cpu_thread_fn(void *arg)break ; } } - -qemu_process_cpu_events (cpu); }while (!cpu->unplug ||cpu_can_run (cpu));tcg_cpu_destroy (cpu);
accel/tcg/tcg-accel-ops-rr.c +20 -23
59 lines not on this page · raw message
target/i386/nvmm/nvmm-accel-ops.c +2 -4
@@ -42,16 +42,14 @@ static void *qemu_nvmm_cpu_thread_fn(void *arg)qemu_guest_random_seed_thread_part2 (cpu->random_seed);do { +qemu_process_cpu_events (cpu); +if (cpu_can_run (cpu)) { r =nvmm_vcpu_exec (cpu);if (r ==EXCP_DEBUG ) {cpu_handle_guest_debug (cpu); } } -while (cpu_thread_is_idle (cpu)) { -qemu_cond_wait_bql (cpu->halt_cond); - } -qemu_process_cpu_events_common (cpu); }while (!cpu->unplug ||cpu_can_run (cpu));nvmm_destroy_vcpu (cpu);
target/i386/whpx/whpx-accel-ops.c +2 -4
@@ -42,16 +42,14 @@ static void *whpx_cpu_thread_fn(void *arg)qemu_guest_random_seed_thread_part2 (cpu->random_seed);do { +qemu_process_cpu_events (cpu); +if (cpu_can_run (cpu)) { r =whpx_vcpu_exec (cpu);if (r ==EXCP_DEBUG ) {cpu_handle_guest_debug (cpu); } } -while (cpu_thread_is_idle (cpu)) { -qemu_cond_wait_bql (cpu->halt_cond); - } -qemu_process_cpu_events_common (cpu); }while (!cpu->unplug ||cpu_can_run (cpu));whpx_destroy_vcpu (cpu);
Signature · 1 line
2.51.0
PATCH v3 19/19 tcg/user: do not set exit_request gratuitously
In reply to Paolo BonziniWhenever user-mode emulation needs to go all the way out of the cpu exec loop, it uses cpu_exit(), which already sets cpu->exit_request.
Therefore, there is no need for tcg_kick_vcpu_thread() to set cpu->exit_request again outside system emulation.
--- accel/tcg/cpu-exec.c | 2 ++ 1 file changed, 2 insertions(+)
accel/tcg/cpu-exec.c +2 -0
@@ -751,6 +751,7 @@ static inline bool cpu_handle_exception(CPUState *cpu, int *ret)void tcg_kick_vcpu_thread (CPUState *cpu) { +#ifndef CONFIG_USER_ONLY /* * Ensure cpu_exec will see the reason why the exit request was set. * FIXME: this is not always needed. Other accelerators instead
@@ -758,6 +759,7 @@ void tcg_kick_vcpu_thread(CPUState *cpu)* CPU thread; see kvm_arch_pre_run() for example. */ qatomic_store_release (&cpu->exit_request, true); +#endif /* Ensure cpu_exec will see the exit request after TCG has exited. */ qatomic_store_release (&cpu->neg.icount_decr.u16.high,-1 );
Signature · 1 line
2.51.0
Re: [PATCH 19/19] tcg/user: do not set exit_request gratuitously
Quoted 5 lines · Paolo Bonzini
Whenever user-mode emulation needs to go all the way out of the cpu exec loop, it uses
cpu_exit(), which already setscpu->exit_request.Therefore, there is no need for
tcg_kick_vcpu_thread()to setcpu->exit_requestagain outside system emulation.
This breaks go. If you run go build in a loop, it eventually hangs uninterruptible (except -9) with a couple of zombie children left over.
Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected] GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7 "And now for something completely different."
Re: [PATCH 19/19] tcg/user: do not set exit_request gratuitously
I assume you have already tried to revert it?
Thanks,
Paolo
Il gio 29 gen 2026, 10:11 Andreas Schwab <[email protected]> ha scritto:
Quoted 17 lines · Paolo Bonzini
Quoted 6 lines
Whenever user-mode emulation needs to go all the way out of the cpu exec loop, it uses
cpu_exit(), which already setscpu->exit_request.Therefore, there is no need for
tcg_kick_vcpu_thread()to setcpu->exit_requestagain outside system emulation.This breaks go. If you run go build in a loop, it eventually hangs uninterruptible (except -9) with a couple of zombie children left over.
--
Andreas Schwab, SUSE Labs, [email protected]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7 "And now for something completely different."
Re: [PATCH 19/19] tcg/user: do not set exit_request gratuitously
Quoted 1 line · Paolo Bonzini
I assume you have already tried to revert it?
Sure, that's how I verified it.
Signature · 3 lines
Andreas Schwab, SUSE Labs, [email protected] GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7 "And now for something completely different."
Re: [PATCH 19/19] tcg/user: do not set exit_request gratuitously
Quoted 5 lines · Andreas Schwab
Quoted 1 line · Paolo Bonzini
I assume you have already tried to revert it?
Sure, that's how I verified it.
Yeah, sorry for the badly worded question---I meant reverting it from master as opposed to just reporting the bisection result. I'll send a revert.
Paolo