Skip to contentSkip to conversation

accel, cpus: clean up cpu->exit_request

PATCH Superseded Paolo Bonzini 4 participants 58 messages Aug 30, 2025 – Sep 1, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 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.

v1->v2:
- new patches 1-8, leftovers from Igor's own cleanup
- patch 9: use "true" instead of 1, editorial cleanups
- dropped "accel/hvf: check exit_request before running the vCPU"
- patch 10: add more comments, use "true" instead of 1
- patch 12: use accel/tcg/tcg-accel-ops.h for prototype, split out patch 12 ("accel/tcg: inline cpu_exit()"), place user-mode emulation version of qemu_cpu_kick() in accel/tcg/user-exec.c
- patch 14: fix compilation errors
- patch 15: new, fixes infinite loops in user-mode emulation
- patches 16-17: split in two parts
- patch 18: improve commit message

Paolo Bonzini (18):
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 bsd-user, linux-user: introduce qemu_wait_io_event
cpus: clear exit_request in qemu_wait_io_event
accel: make all calls to qemu_wait_io_event look the same tcg/user: do not set exit_request gratuitously

 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               |   1 -
 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        |  42 +--
 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                       |   9 +-
 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, 631 insertions(+), 559 deletions(-)
 create mode 100644 target/arm/cpu-irq.c
 create mode 100644 target/arm/el2-stubs.c
Signature · 1 line
2.51.0
#1 lore raw

PATCH v2 1/18 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
index 7e5726871e5..5f217397490 100644
@@ -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
Reviewed-by on v2 1/18
#2 lore raw

Re: [PATCH 01/18] target/ppc: limit cpu_interrupt_exittb to system emulation

Quoted 21 lines · Paolo Bonzini

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 +1 -0
index 7e5726871e5..5f217397490 100644
@@ -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)
  {
      /*

target/ppc/helper_regs.c void cpu_interrupt_exittb(CPUState *cs) · line 292

Quoted 7 lines
target/ppc/helper_regs.c +1 -0
@@ -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)
  {

r~

Reviewed-by on v2 1/18
#3 lore raw

Re: [PATCH 01/18] target/ppc: limit cpu_interrupt_exittb to system emulation

In reply to Paolo Bonzini
Quoted 4 lines · On Fri, 29 Aug 2025 17:28:52 +0200 Paolo Bonzini <[email protected]> wrote:

It is not used by user-mode emulation and is the only caller of cpu_interrupt() in qemu-ppc* binaries.

Quoted 24 lines
---
 target/ppc/helper_regs.c | 2 ++
 1 file changed, 2 insertions(+)
target/ppc/helper_regs.c +2 -0
index 7e5726871e5..5f217397490 100644
@@ -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)
 {
#4 lore raw

PATCH v2 2/18 target/sparc: limit cpu_check_irqs to system emulation

In reply to Paolo Bonzini

It 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
index 39db4ffa70a..fdcaa0a578b 100644
@@ -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
index 49e4e51c6dc..23adda4cad7 100644
@@ -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
Reviewed-by on v2 2/18
#6 lore raw

Re: [PATCH 02/18] target/sparc: limit cpu_check_irqs to system emulation

In reply to Paolo Bonzini
Quoted 22 lines · Paolo Bonzini

It 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 +1 -0
index 39db4ffa70a..fdcaa0a578b 100644
@@ -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;

target/sparc/int32_helper.c void cpu_check_irqs(CPUSPARCState *env) · line 100

Quoted 4 lines
target/sparc/int32_helper.c +1 -0
@@ -96,6 +97,7 @@ void cpu_check_irqs(CPUSPARCState *env)
          cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
      }
  }
+#endif

I'd rather see these moved in system-specific files.

#7 lore raw

PATCH v2 3/18 target/i386: limit a20 to system emulation

In reply to Paolo Bonzini

It 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
index e0aaed3c4c4..651041ccfa6 100644
@@ -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
Reviewed-by on v2 3/18
#8 lore raw

Re: [PATCH 03/18] target/i386: limit a20 to system emulation

Quoted 21 lines · Paolo Bonzini

It 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 +1 -0
index e0aaed3c4c4..651041ccfa6 100644
@@ -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;

target/i386/helper.c void x86_cpu_set_a20(X86CPU *cpu, int a20_state) · line 136

Quoted 7 lines
target/i386/helper.c +1 -0
@@ -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)
  {

r~

Reviewed-by on v2 3/18
#9 lore raw

Re: [PATCH 03/18] target/i386: limit a20 to system emulation

In reply to Paolo Bonzini
Quoted 21 lines · Paolo Bonzini

It 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 +1 -0
index e0aaed3c4c4..651041ccfa6 100644
@@ -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;

target/i386/helper.c void x86_cpu_set_a20(X86CPU *cpu, int a20_state) · line 133

Quoted 4 lines
target/i386/helper.c +1 -0
@@ -129,6 +130,7 @@ void x86_cpu_set_a20(X86CPU *cpu, int a20_state)
          env->a20_mask = ~(1 << 20) | (a20_state << 20);
      }
  }
+#endif

Directly move to target/i386/cpu-system.c?

Reviewed-by on v2 3/18
#10 lore raw

Re: [PATCH 03/18] target/i386: limit a20 to system emulation

In reply to Paolo Bonzini
Quoted 4 lines · On Fri, 29 Aug 2025 17:28:54 +0200 Paolo Bonzini <[email protected]> wrote:

It is not used by user-mode emulation and is the only caller of cpu_interrupt() in qemu-i386 and qemu-x86_64.

Quoted 24 lines
---
 target/i386/helper.c | 2 ++
 1 file changed, 2 insertions(+)
target/i386/helper.c +2 -0
index e0aaed3c4c4..651041ccfa6 100644
@@ -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)
 {
#11 lore raw

PATCH v2 4/18 target-arm: remove uses of cpu_interrupt() for user-mode emulation

In reply to Paolo Bonzini

Arm 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
index 1b3d0244fd6..0561c2e2cc7 100644
@@ -1274,6 +1274,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
new file mode 100644
index 00000000000..972023c337f
@@ -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
index 4cd36e950aa..983eb2c4ecd 100644
@@ -2862,8 +2862,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
index 07d9271aa4d..914f1498fc5 100644
@@ -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
Reviewed-by on v2 4/18
#12 lore raw

Re: [PATCH 04/18] target-arm: remove uses of cpu_interrupt() for user-mode emulation

Quoted 15 lines · Paolo Bonzini

Arm 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(-)
Quoted 2 lines

create mode 100644 target/arm/cpu-irq.c
create mode 100644 target/arm/el2-stubs.c

r~

#13 lore raw

PATCH v2 5/18 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
index 748bfab04a7..66c25fba7dd 100644
@@ -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
Reviewed-by on v2 5/18
#14 lore raw

Re: [PATCH 05/18] user-exec: remove cpu_interrupt() stub

Quoted 11 lines · Paolo Bonzini
---
  accel/tcg/user-exec.c | 5 -----
  1 file changed, 5 deletions(-)
accel/tcg/user-exec.c +0 -0
index 748bfab04a7..66c25fba7dd 100644
@@ -46,11 +46,6 @@ __thread uintptr_t helper_retaddr;
 

accel/tcg/user-exec.c __thread uintptr_t helper_retaddr; · line 51

Quoted 10 lines
accel/tcg/user-exec.c +0 -5
  //#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.
   */

r~

Reviewed-by on v2 5/18
#15 lore raw

Re: [PATCH 05/18] user-exec: remove cpu_interrupt() stub

In reply to Paolo Bonzini
Quoted 10 lines · Paolo Bonzini
---
  accel/tcg/user-exec.c | 5 -----
  1 file changed, 5 deletions(-)
accel/tcg/user-exec.c +0 -0
index 748bfab04a7..66c25fba7dd 100644

accel/tcg/user-exec.c __thread uintptr_t helper_retaddr; · line 53

Quoted 8 lines
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();
-}
-

It would be nice to have the declaration also restricted to system (IOW not exposed to user). Anyhow,

#16 lore raw

PATCH v2 6/18 treewide: clear bits of cs->interrupt_request with cpu_reset_interrupt()

In reply to Paolo Bonzini
---
 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
index 8491e5badd1..508d2d2d9e2 100644
@@ -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
index a975405d3a0..09c928c1f92 100644
@@ -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
index b9cd6d5ef27..4b29ab35263 100644
@@ -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
index 9e05e0e5765..a502437c303 100644
@@ -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
index 306430a0521..8420c4090ef 100644
@@ -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
index c1ac74c4f04..e1151b04c6e 100644
@@ -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
index 794a23ddfc4..38072e51d72 100644
@@ -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
index 3569196bdda..505788b0e26 100644
@@ -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
index 878cdd1668c..c09a0a64f22 100644
@@ -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
index d96b41a01c2..b091a9c6685 100644
@@ -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
index ce003af4219..41c9606fd1d 100644
@@ -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
index e4c75d0ce01..4c7faeee82b 100644
@@ -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
Reviewed-by on v2 6/18
#17 lore raw

Re: [PATCH 06/18] treewide: clear bits of cs->interrupt_request with cpu_reset_interrupt()

Quoted 6 lines · Paolo Bonzini
---
  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 +++++++-------
Quoted 8 lines
  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(-)

r~

Reviewed-by on v2 6/18
#18 lore raw

Re: [PATCH 06/18] treewide: clear bits of cs->interrupt_request with cpu_reset_interrupt()

In reply to Paolo Bonzini
Quoted 6 lines · Paolo Bonzini
---
  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 +++++++-------
Quoted 8 lines
  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(-)

Good cleanup.

#19 lore raw

Re: [PATCH 06/18] treewide: clear bits of cs->interrupt_request with cpu_reset_interrupt()

Quoted 1 line · Philippe Mathieu-Daudé

On 29/8/25 17:31, Paolo Bonzini wrote:

BTW, missing your S-o-b ;)

Quoted 19 lines
Quoted 13 lines

---
  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 +-

Quoted 1 line
  12 files changed, 39 insertions(+), 40 deletions(-)

Good cleanup.

Reviewed-by on v2 6/18
#20 lore raw

Re: [PATCH 06/18] treewide: clear bits of cs->interrupt_request with cpu_reset_interrupt()

In reply to Paolo Bonzini

On Fri, 29 Aug 2025 17:31:03 +0200
Paolo Bonzini <[email protected]> wrote:

beside nit below

Quoted 126 lines
---
 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
index 8491e5badd1..508d2d2d9e2 100644
@@ -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
index a975405d3a0..09c928c1f92 100644
@@ -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
index b9cd6d5ef27..4b29ab35263 100644
@@ -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
index 9e05e0e5765..a502437c303 100644
@@ -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 +0 -0
index 306430a0521..8420c4090ef 100644

target/i386/kvm/kvm.c static int kvm_put_vcpu_events(X86CPU *cpu, int level) · line 5069

Quoted 5 lines
target/i386/kvm/kvm.c +1 -1
@@ -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);

this was called without BQL in kvm_cpu_exec(),
perhaps mention in commit message that beside replacing
open coded interrupt reset commit also fixes potential bug since cpu_reset_interrupt() would take BQL now. (or make this hunk a separate patch)

not sure but perhaps the same applies to hunk in target/i386/tcg/system/svm_helper.c

Quoted 274 lines
target/i386/kvm/kvm.c +6 -6
         } 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
index c1ac74c4f04..e1151b04c6e 100644
@@ -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
index 794a23ddfc4..38072e51d72 100644
@@ -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
index 3569196bdda..505788b0e26 100644
@@ -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
index 878cdd1668c..c09a0a64f22 100644
@@ -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
index d96b41a01c2..b091a9c6685 100644
@@ -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
index ce003af4219..41c9606fd1d 100644
@@ -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
index e4c75d0ce01..4c7faeee82b 100644
@@ -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 */
#21 lore raw

PATCH v2 7/18 cpu-common: use atomic access for interrupt_request

In reply to Paolo Bonzini

Writes 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
index b01a0cffd64..23bd02277f4 100644
@@ -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
index 39e674aca21..9ea1f3764a8 100644
@@ -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
index 437848b5eb4..9bfbe2b0607 100644
@@ -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
Reviewed-by on v2 7/18
#22 lore raw

Re: [PATCH 07/18] cpu-common: use atomic access for interrupt_request

Quoted 6 lines · Paolo Bonzini

Writes 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.

Quoted 6 lines
---
  include/hw/core/cpu.h |  1 -
  hw/core/cpu-common.c  | 12 +-----------
  system/cpus.c         |  3 +--
  3 files changed, 2 insertions(+), 14 deletions(-)

I guess we didn't need SEQ_CST, but since we don't choose to play with relaxed atomics elsewhere, it would simply complicate things to start.

r~

Reviewed-by on v2 7/18
#23 lore raw

Re: [PATCH 07/18] cpu-common: use atomic access for interrupt_request

In reply to Paolo Bonzini
Quoted 6 lines · Paolo Bonzini

Writes 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.

Quoted 6 lines
---
  include/hw/core/cpu.h |  1 -
  hw/core/cpu-common.c  | 12 +-----------
  system/cpus.c         |  3 +--
  3 files changed, 2 insertions(+), 14 deletions(-)
Reviewed-by on v2 7/18
#24 lore raw

Re: [PATCH 07/18] cpu-common: use atomic access for interrupt_request

In reply to Paolo Bonzini
Quoted 7 lines · On Fri, 29 Aug 2025 17:31:04 +0200 Paolo Bonzini <[email protected]> wrote:

Writes 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.

Quoted 57 lines
---
 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
index b01a0cffd64..23bd02277f4 100644
@@ -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
index 39e674aca21..9ea1f3764a8 100644
@@ -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
index 437848b5eb4..9bfbe2b0607 100644
@@ -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)
#25 lore raw

PATCH v2 8/18 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
index 23bd02277f4..8b57bcd92c9 100644
@@ -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
Reviewed-by on v2 8/18
#26 lore raw

Re: [PATCH 08/18] cpus: document that qemu_cpu_kick() can be used for BQL-less operation

Quoted 10 lines · Paolo Bonzini
---
  include/hw/core/cpu.h | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
include/hw/core/cpu.h +0 -0
index 23bd02277f4..8b57bcd92c9 100644

include/hw/core/cpu.h bool qemu_cpu_is_self(CPUState *cpu); · line 836

Quoted 9 lines
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);
 

r~

#27 lore raw

PATCH v2 9/18 accel: use store_release/load_acquire for cross-thread exit_request

In reply to Paolo Bonzini

Reads 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 |  2 +-
 hw/core/cpu-common.c         |  3 ++-
 target/i386/nvmm/nvmm-all.c  |  5 ++---
 target/i386/whpx/whpx-all.c  |  3 ++-
 6 files changed, 21 insertions(+), 18 deletions(-)
accel/kvm/kvm-all.c +9 -10
index f36dfe33492..bd9e5e3886d 100644
@@ -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
index 508d2d2d9e2..f838535d111 100644
@@ -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 +1 -1
index 6eec5c9eee9..1e551e92d6d 100644
@@ -242,7 +242,7 @@ static void *rr_cpu_thread_fn(void *arg)
             cpu = first_cpu;
         }
 
-        while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) {
+        while (cpu && cpu_work_list_empty(cpu) && !qatomic_load_acquire(&cpu->exit_request)) {
             /* Store rr_current_cpu before evaluating cpu_can_run().  */
             qatomic_set_mb(&rr_current_cpu, cpu);
 
hw/core/cpu-common.c +2 -1
index 9ea1f3764a8..ca00accd162 100644
@@ -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
index e1151b04c6e..10bd51d9b59 100644
@@ -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
index c09a0a64f22..2106c29c3a0 100644
@@ -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
Reviewed-by on v2 9/18
#28 lore raw

Re: [PATCH 09/18] accel: use store_release/load_acquire for cross-thread exit_request

Quoted 10 lines · On Fri, 29 Aug 2025 17:31:06 +0200 Paolo Bonzini <[email protected]> wrote:

Reads 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.

Quoted 3 lines

nit below:

Quoted 78 lines
---
 accel/kvm/kvm-all.c          | 19 +++++++++----------
 accel/tcg/cpu-exec.c         |  7 +++++--
 accel/tcg/tcg-accel-ops-rr.c |  2 +-
 hw/core/cpu-common.c         |  3 ++-
 target/i386/nvmm/nvmm-all.c  |  5 ++---
 target/i386/whpx/whpx-all.c  |  3 ++-
 6 files changed, 21 insertions(+), 18 deletions(-)
accel/kvm/kvm-all.c +9 -10
index f36dfe33492..bd9e5e3886d 100644
@@ -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
index 508d2d2d9e2..f838535d111 100644
@@ -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 +0 -0
index 6eec5c9eee9..1e551e92d6d 100644

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

Quoted 3 lines
accel/tcg/tcg-accel-ops-rr.c +0 -0
@@ -242,7 +242,7 @@ static void *rr_cpu_thread_fn(void *arg)
             cpu = first_cpu;
         }
 

should we have here a similar comment as in the previous hunk?

Quoted 56 lines
accel/tcg/tcg-accel-ops-rr.c +1 -1
-        while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) {
+        while (cpu && cpu_work_list_empty(cpu) && !qatomic_load_acquire(&cpu->exit_request)) {
             /* Store rr_current_cpu before evaluating cpu_can_run().  */
             qatomic_set_mb(&rr_current_cpu, cpu);
 
hw/core/cpu-common.c +2 -1
index 9ea1f3764a8..ca00accd162 100644
@@ -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
index e1151b04c6e..10bd51d9b59 100644
@@ -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
index c09a0a64f22..2106c29c3a0 100644
@@ -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);
             }
         }
#29 lore raw

PATCH v2 10/18 accel: use atomic accesses for exit_request

In reply to Paolo Bonzini

CPU 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
index 8b57bcd92c9..338757e5254 100644
@@ -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
index bd9e5e3886d..e4167d94b4f 100644
@@ -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
index 337b993d3da..b12b7a36b5d 100644
@@ -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
index 1e551e92d6d..c2468d15d4f 100644
@@ -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() */
@@ -286,7 +286,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
index 1e936f35e44..51875e32a09 100644
@@ -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
index 8420c4090ef..34e74f24470 100644
@@ -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
index 3799260bbde..86869f133e9 100644
@@ -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
index 10bd51d9b59..7e36c42fbb4 100644
@@ -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
index 2106c29c3a0..00fb7e23100 100644
@@ -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
Reviewed-by on v2 10/18
#30 lore raw

Re: [PATCH 10/18] accel: use atomic accesses for exit_request

Quoted 11 lines · On Fri, 29 Aug 2025 17:31:07 +0200 Paolo Bonzini <[email protected]> wrote:

CPU 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").

Quoted 189 lines
---
 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
index 8b57bcd92c9..338757e5254 100644
@@ -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
index bd9e5e3886d..e4167d94b4f 100644
@@ -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
index 337b993d3da..b12b7a36b5d 100644
@@ -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
index 1e551e92d6d..c2468d15d4f 100644
@@ -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() */
@@ -286,7 +286,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
index 1e936f35e44..51875e32a09 100644
@@ -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
index 8420c4090ef..34e74f24470 100644
@@ -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
index 3799260bbde..86869f133e9 100644
@@ -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
index 10bd51d9b59..7e36c42fbb4 100644
@@ -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
index 2106c29c3a0..00fb7e23100 100644
@@ -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;
     }
#31 lore raw

PATCH v2 11/18 accel/tcg: create a thread-kick function for TCG

In reply to Paolo Bonzini

Round-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
index 7df883446a7..a1dcd79e0fd 100644
@@ -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
index 8ffa7a9a9fe..5c145cc8595 100644
@@ -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
index 6feeb3f3e9b..aecce605d7b 100644
@@ -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
index f838535d111..9241bcadb5f 100644
@@ -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
index b12b7a36b5d..1148ebcaae5 100644
@@ -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
index c2468d15d4f..610292d3bac 100644
@@ -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
index 9c37266c1e0..1f662a9c745 100644
@@ -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
index 66c25fba7dd..3c072fd868f 100644
@@ -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
index 7e5d4bbce09..fc33e4d4880 100644
@@ -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
index 6edeeecef38..2ba073eb830 100644
@@ -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
Reviewed-by on v2 11/18
#32 lore raw

Re: [PATCH 11/18] accel/tcg: create a thread-kick function for TCG

Quoted 9 lines · Paolo Bonzini

Round-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.

Quoted 12 lines
---
  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(-)

r~

Reviewed-by on v2 11/18
#33 lore raw

Re: [PATCH 11/18] accel/tcg: create a thread-kick function for TCG

In reply to Paolo Bonzini
Quoted 9 lines · Paolo Bonzini

Round-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.

Quoted 12 lines
---
  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(-)
Reviewed-by on v2 11/18
#34 lore raw

Re: [PATCH 11/18] accel/tcg: create a thread-kick function for TCG

In reply to Paolo Bonzini
Quoted 9 lines · On Fri, 29 Aug 2025 17:31:08 +0200 Paolo Bonzini <[email protected]> wrote:

Round-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.

Quoted 173 lines
---
 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
index 7df883446a7..a1dcd79e0fd 100644
@@ -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
index 8ffa7a9a9fe..5c145cc8595 100644
@@ -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
index 6feeb3f3e9b..aecce605d7b 100644
@@ -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
index f838535d111..9241bcadb5f 100644
@@ -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
index b12b7a36b5d..1148ebcaae5 100644
@@ -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
index c2468d15d4f..610292d3bac 100644
@@ -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
index 9c37266c1e0..1f662a9c745 100644
@@ -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
index 66c25fba7dd..3c072fd868f 100644
@@ -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
index 7e5d4bbce09..fc33e4d4880 100644
@@ -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
index 6edeeecef38..2ba073eb830 100644
@@ -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) {
#35 lore raw

PATCH v2 12/18 accel/tcg: inline cpu_exit()

In reply to Paolo Bonzini

Right 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
index 9241bcadb5f..3ae545e888f 100644
@@ -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
Reviewed-by on v2 12/18
#36 lore raw

Re: [PATCH 12/18] accel/tcg: inline cpu_exit()

Quoted 9 lines · Paolo Bonzini

Right 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.

Quoted 4 lines
---
  accel/tcg/cpu-exec.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)

r~

Reviewed-by on v2 12/18
#37 lore raw

Re: [PATCH 12/18] accel/tcg: inline cpu_exit()

In reply to Paolo Bonzini
Quoted 10 lines · On Fri, 29 Aug 2025 17:31:09 +0200 Paolo Bonzini <[email protected]> wrote:

Right 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.

Quoted 36 lines
---
 accel/tcg/cpu-exec.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
accel/tcg/cpu-exec.c +12 -2
index 9241bcadb5f..3ae545e888f 100644
@@ -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);
 
#38 lore raw

PATCH v2 13/18 cpus: remove TCG-ism from cpu_exit()

In reply to Paolo Bonzini

Now 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
index ca00accd162..3fa9fa82228 100644
@@ -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
Reviewed-by on v2 13/18
#39 lore raw

Re: [PATCH 13/18] cpus: remove TCG-ism from cpu_exit()

Quoted 6 lines · On Fri, 29 Aug 2025 17:31:10 +0200 Paolo Bonzini <[email protected]> wrote:

Now that TCG has its own kick function, make cpu_exit() do the right kick for all accelerators.

Quoted 19 lines
---
 hw/core/cpu-common.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
hw/core/cpu-common.c +1 -3
index ca00accd162..3fa9fa82228 100644
@@ -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)
#40 lore raw

PATCH v2 14/18 cpus: properly kick CPUs out of inner execution loop

In reply to Paolo Bonzini

Now 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
index ef5757d23bf..152661df8e9 100644
@@ -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
index 43d0d0e7553..3e436c70413 100644
@@ -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
index 51875e32a09..c594d4b916e 100644
@@ -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
index 78309dbb09d..143bc8c3794 100644
@@ -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
index 8959da9f1fa..a96e47e7740 100644
@@ -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
index cb35fa62b8a..6a00691b8d5 100644
@@ -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; kick it back 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
index 9bfbe2b0607..bb13942cbb7 100644
@@ -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
index 0efc18a181e..302e899287c 100644
@@ -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
index 9865120cc43..f7a81bd2700 100644
@@ -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
#41 lore raw

Re: [PATCH 14/18] cpus: properly kick CPUs out of inner execution loop

hw/ppc/spapr_hcall.c static target_ulong h_confer(PowerPCCPU *cpu, SpaprMachineState *spapr, · line 630

Quoted 10 lines · Paolo Bonzini
hw/ppc/spapr_hcall.c +1 -2
@@ -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;
  }

cpu_loop_exit does a longjmp; cpu_exit does not.

This may be a bug fix, but it's hard to tell.
If it is a bug fix, it should be separated.

I'm also having a hard time with e.g.

system/cpu-timers.c void qemu_timer_notify_cb(void *opaque, QEMUClockType type) · line 253

Quoted 12 lines

+++ b/system/cpu-timers.c

system/cpu-timers.c +2 -2
@@ -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; kick it back out of the
          * tcg_cpu_exec() loop so it will recalculate its
          * icount deadline immediately.
          */
-        qemu_cpu_kick(current_cpu);
+        cpu_exit(current_cpu);

where the comment still says kick and we're replacing kick with exit.

I guess the root of this problem is that "kick" isn't a precise term, we ought to name it something else, and we should paint the bike shed green.

r~

#42 lore raw

Re: [PATCH 14/18] cpus: properly kick CPUs out of inner execution loop

Quoted 10 lines · Richard Henderson
Quoted 8 lines · Paolo Bonzini
hw/ppc/spapr_hcall.c +1 -2
@@ -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);
 
Quoted 7 lines

Review static target_ulong h_confer(PowerPCCPU *cpu, SpaprMachineState *spapr, · line 630

Quoted 2 lines
(quoted diff) +0 -0
      return H_SUCCESS;
  }

cpu_loop_exit does a longjmp; cpu_exit does not.

This may be a bug fix, but it's hard to tell.
If it is a bug fix, it should be separated.

The longjmp is overkill but works. Not doing the longjmp also works because gen_sc() finishes the TB and then you go all the way (check interrupt_request -> check exit_request -> write exception_index -> cpu_exec_end) out of tcg_cpu_exec().

I like cpu_loop_exit() to signify that I am in the middle of the TB. That said, I'm always conflicted between renaming badly-named functions and keeping historical names. qemu_wait_io_event() is also horrible.

Quoted 15 lines

system/cpu-timers.c void qemu_timer_notify_cb(void *opaque, QEMUClockType type) · line 253

Quoted 12 lines

+++ b/system/cpu-timers.c

system/cpu-timers.c +2 -2
@@ -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; kick it back out of the
          * tcg_cpu_exec() loop so it will recalculate its
          * icount deadline immediately.
          */
-        qemu_cpu_kick(current_cpu);
+        cpu_exit(current_cpu);

where the comment still says kick and we're replacing kick with exit.

Quoted 2 lines

I guess the root of this problem is that "kick" isn't a precise term, we ought to name it something else, and we should paint the bike shed green.

Yes, I agree. "send it out of" can work too in this case, I'll chanbge it.

Paolo

#43 lore raw

Re: [PATCH 14/18] cpus: properly kick CPUs out of inner execution loop

Quoted 17 lines · Paolo Bonzini
Quoted 15 lines · Richard Henderson

hw/ppc/spapr_hcall.c static target_ulong h_confer(PowerPCCPU *cpu, SpaprMachineState *spapr, · line 630

Quoted 10 lines · Paolo Bonzini
hw/ppc/spapr_hcall.c +1 -2
@@ -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;
   }

cpu_loop_exit does a longjmp; cpu_exit does not.

Quoted 7 lines
Quoted 2 lines

This may be a bug fix, but it's hard to tell.
If it is a bug fix, it should be separated.

The longjmp is overkill but works. Not doing the longjmp also works because gen_sc() finishes the TB and then you go all the way (check interrupt_request -> check exit_request -> write exception_index -> cpu_exec_end) out of tcg_cpu_exec().

Ok.

Quoted 2 lines

I like cpu_loop_exit() to signify that I am in the middle of the TB.

Agreed. I suspect we over-use longjmp and could perhaps do better at simply returning up the call-stack.

Quoted 3 lines

That said, I'm always conflicted between renaming badly-named functions and keeping historical names. qemu_wait_io_event() is also horrible.

...

Quoted 16 lines
Quoted 16 lines

system/cpu-timers.c void qemu_timer_notify_cb(void *opaque, QEMUClockType type) · line 253

Quoted 13 lines

+++ b/system/cpu-timers.c

system/cpu-timers.c +2 -2
@@ -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; kick it back out of the
           * tcg_cpu_exec() loop so it will recalculate its
           * icount deadline immediately.
           */
-        qemu_cpu_kick(current_cpu);
+        cpu_exit(current_cpu);

where the comment still says kick and we're replacing kick with exit.

Quoted 4 lines
Quoted 2 lines

I guess the root of this problem is that "kick" isn't a precise term, we ought to name it something else, and we should paint the bike shed green.

Yes, I agree. "send it out of" can work too in this case, I'll chanbge it.

I was actually talking about renaming qemu_cpu_kick. But per above, that's hard. :-)

I guess just updating the comment is fine for now.

r~

#44 lore raw

PATCH v2 15/18 bsd-user, linux-user: introduce qemu_wait_io_event

In reply to Paolo Bonzini

Add 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
index 87fbf6d6775..4407f35fb97 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case EXCP_SWI:
bsd-user/arm/target_arch_cpu.h +1 -1
index bc2eaa0bf4e..a79ecf15f8f 100644
@@ -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_wait_io_event(cs);
         switch (trapnr) {
         case EXCP_UDEF:
         case EXCP_NOCP:
bsd-user/i386/target_arch_cpu.h +1 -1
index 5d4c931decd..592702a8a1e 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case 0x80: {
bsd-user/riscv/target_arch_cpu.h +1 -1
index ef92f004803..dbe7c7231f5 100644
@@ -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_wait_io_event(cs);
 
         signo = 0;
 
bsd-user/x86_64/target_arch_cpu.h +1 -1
index f82042e30af..f298fbc9808 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case EXCP_SYSCALL:
include/hw/core/cpu.h +9 -0
index 338757e5254..ffa553b2318 100644
@@ -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_wait_io_event:
+ * @cpu: CPU that left the execution loop
+ *
+ * Perform accelerator-independent work after the CPU has left
+ * the inner execution loop.
+ */
+void qemu_wait_io_event(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
index 69be6a77a75..e6864861c0b 100644
@@ -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_wait_io_event_common(CPUState *cpu);
-void qemu_wait_io_event(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
index 3c072fd868f..81906d2e033 100644
@@ -52,6 +52,11 @@ void qemu_cpu_kick(CPUState *cpu)
     tcg_kick_vcpu_thread(cpu);
 }
 
+void qemu_wait_io_event(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
index 4c4921152e8..9d0f09c3a13 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case EXCP_SWI:
linux-user/alpha/cpu_loop.c +1 -1
index 728b64906d9..1f2d1c5565f 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case EXCP_RESET:
linux-user/arm/cpu_loop.c +1 -1
index 9aeb9b0087f..026a189b884 100644
@@ -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_wait_io_event(cs);
 
         switch(trapnr) {
         case EXCP_UDEF:
linux-user/hexagon/cpu_loop.c +1 -1
index 25c97edcaef..675c157a3de 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case EXCP_INTERRUPT:
linux-user/hppa/cpu_loop.c +1 -1
index 3af50653bb7..a8e715cb59b 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case EXCP_SYSCALL:
linux-user/i386/cpu_loop.c +1 -1
index 7b2d8b03d84..7af476c9d44 100644
@@ -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_wait_io_event(cs);
 
         switch(trapnr) {
         case 0x80:
linux-user/loongarch64/cpu_loop.c +1 -1
index a0a4cbb7cc3..dc83118e389 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case EXCP_INTERRUPT:
linux-user/m68k/cpu_loop.c +1 -1
index aca0bf23dc6..5b62260212d 100644
@@ -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_wait_io_event(cs);
 
         switch(trapnr) {
         case EXCP_ILLEGAL:
linux-user/microblaze/cpu_loop.c +1 -1
index d8277961c73..a7f3f0e6a68 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case EXCP_INTERRUPT:
linux-user/mips/cpu_loop.c +1 -1
index e67b8a2e463..9ac4af6ae52 100644
@@ -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_wait_io_event(cs);
 
         switch(trapnr) {
         case EXCP_SYSCALL:
linux-user/openrisc/cpu_loop.c +1 -1
index 8c72347a99a..9512e34e2af 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case EXCP_SYSCALL:
linux-user/ppc/cpu_loop.c +1 -1
index 22885ffd906..3b5d775a49f 100644
@@ -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_wait_io_event(cs);
 
         arch_interrupt = true;
         switch (trapnr) {
linux-user/riscv/cpu_loop.c +1 -1
index b3162815320..940fd67f7b3 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case EXCP_INTERRUPT:
linux-user/s390x/cpu_loop.c +1 -1
index 49e44548f85..be179a073f6 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case EXCP_INTERRUPT:
linux-user/sh4/cpu_loop.c +1 -1
index 259ea1cc8bb..a7edd52e37c 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case 0x160:
linux-user/sparc/cpu_loop.c +1 -1
index 7d30cd1ff22..b9228708bf4 100644
@@ -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_wait_io_event(cs);
 
         switch (trapnr) {
         case TARGET_TT_SYSCALL:
linux-user/xtensa/cpu_loop.c +1 -1
index 43a194fc4a4..bf19377dc29 100644
@@ -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_wait_io_event(cs);
 
         env->sregs[PS] &= ~PS_EXCM;
         switch (trapnr) {
Signature · 1 line
2.51.0
Reviewed-by on v2 15/18
#45 lore raw

Re: [PATCH 15/18] bsd-user, linux-user: introduce qemu_wait_io_event

Quoted 23 lines · Paolo Bonzini

Add 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 +-
Quoted 8 lines
  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(-)

r~

Reviewed-by on v2 15/18
#46 lore raw

Re: [PATCH 15/18] bsd-user, linux-user: introduce qemu_wait_io_event

In reply to Paolo Bonzini
Quoted 23 lines · Paolo Bonzini

Add 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 +-
Quoted 8 lines
  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(-)
#47 lore raw

PATCH v2 16/18 cpus: clear exit_request in qemu_wait_io_event

In reply to Paolo Bonzini

Make the code common to all accelerators: after seeing cpu->exit_request set to true, accelerator code needs to reach qemu_wait_io_event_common().

So for the common cases where they use qemu_wait_io_event(), 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_cond is 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
index e4167d94b4f..d13156bee87 100644
@@ -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
index 3ae545e888f..ad94f96b252 100644
@@ -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
index 610292d3bac..e9d291dc391 100644
@@ -286,8 +286,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
index 1f662a9c745..3bd98005042 100644
@@ -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
index 81906d2e033..8f4f049b924 100644
@@ -54,6 +54,7 @@ void qemu_cpu_kick(CPUState *cpu)
 
 void qemu_wait_io_event(CPUState *cpu)
 {
+    qatomic_set(&cpu->exit_request, false);
     process_queued_cpu_work(cpu);
 }
 
system/cpus.c +1 -0
index bb13942cbb7..f989d9938b6 100644
@@ -463,6 +463,7 @@ void qemu_wait_io_event(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
index 7e36c42fbb4..ed424251673 100644
@@ -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
index 00fb7e23100..2a85168ed51 100644
@@ -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
Reviewed-by on v2 16/18
#48 lore raw

Re: [PATCH 16/18] cpus: clear exit_request in qemu_wait_io_event

Quoted 9 lines · Paolo Bonzini

Make the code common to all accelerators: after seeing cpu->exit_request set to true, accelerator code needs to reach qemu_wait_io_event_common().

So for the common cases where they use qemu_wait_io_event(), 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.

Quoted 8 lines

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

Mm. This is the reason we want the seq_cst of the qatomic_or. Perhaps comments in patch 7 should be expanded to document this?

Anyway for this patch,

r~

#49 lore raw

Re: [PATCH 16/18] cpus: clear exit_request in qemu_wait_io_event

Quoted 5 lines · Richard Henderson
Quoted 2 lines

(b) cpu_exit(): store-release cpu->exit_request, or
(b) cpu_interrupt(): store-release cpu->interrupt_request

Mm. This is the reason we want the seq_cst of the qatomic_or. Perhaps comments in patch 7 should be expanded to document this?

Not entirely. There are cases in which a relaxed RMW atomic would also work, for example this one in the CPU thread:

    if (ctl_has_irq(env)) {
        cpu_set_interrupt(cs, CPU_INTERRUPT_VIRQ);
    }

and there are cases in which store-release is needed but atomicity isn't (the ones under the BQL). It's putting the two requirements together that requires patch 7.

And to be honest I am not sure there are other cases than the one above, where cpu_set_interrupt()/cpu_reset_interrupt() is called outside the BQL. But it's really a matter of time. Sooner or later someone will move the APIC or GIC under its own lock, therefore we might as well keep things clean already. "Written only under lock X" is handy but I prefer to use it with moderation - like it's okay if it's a primitive but not if there are dozens of calls across the code base.

Thanks for the speedy review!

Paolo

Reviewed-by on v2 16/18
#50 lore raw

Re: [PATCH 16/18] cpus: clear exit_request in qemu_wait_io_event

In reply to Paolo Bonzini
Quoted 36 lines · On Fri, 29 Aug 2025 17:31:13 +0200 Paolo Bonzini <[email protected]> wrote:

Make the code common to all accelerators: after seeing cpu->exit_request set to true, accelerator code needs to reach qemu_wait_io_event_common().

So for the common cases where they use qemu_wait_io_event(), 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_cond is 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.

Quoted 5 lines

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).

Quoted 126 lines
---
 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
index e4167d94b4f..d13156bee87 100644
@@ -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
index 3ae545e888f..ad94f96b252 100644
@@ -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
index 610292d3bac..e9d291dc391 100644
@@ -286,8 +286,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
index 1f662a9c745..3bd98005042 100644
@@ -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
index 81906d2e033..8f4f049b924 100644
@@ -54,6 +54,7 @@ void qemu_cpu_kick(CPUState *cpu)
 
 void qemu_wait_io_event(CPUState *cpu)
 {
+    qatomic_set(&cpu->exit_request, false);
     process_queued_cpu_work(cpu);
 }
 
system/cpus.c +1 -0
index bb13942cbb7..f989d9938b6 100644
@@ -463,6 +463,7 @@ void qemu_wait_io_event(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
index 7e36c42fbb4..ed424251673 100644
@@ -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
index 00fb7e23100..2a85168ed51 100644
@@ -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;
 }
 
#51 lore raw

PATCH v2 17/18 accel: make all calls to qemu_wait_io_event look the same

In reply to Paolo Bonzini

There is no reason for some accelerators to use qemu_wait_io_event_common (which is separated from qemu_wait_io_event() specifically for round robin). 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
index 03cfc0fa01e..1f74c727c42 100644
@@ -43,6 +43,7 @@ static void *dummy_cpu_thread_fn(void *arg)
     qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
     do {
+        qemu_wait_io_event(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_wait_io_event(cpu);
     } while (!cpu->unplug);
 
     bql_unlock();
accel/hvf/hvf-accel-ops.c +1 -1
index d488d6afbac..4ba3e40831f 100644
@@ -192,13 +192,13 @@ static void *hvf_cpu_thread_fn(void *arg)
     qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
     do {
+        qemu_wait_io_event(cpu);
         if (cpu_can_run(cpu)) {
             r = hvf_vcpu_exec(cpu);
             if (r == EXCP_DEBUG) {
                 cpu_handle_guest_debug(cpu);
             }
         }
-        qemu_wait_io_event(cpu);
     } while (!cpu->unplug || cpu_can_run(cpu));
 
     hvf_vcpu_destroy(cpu);
accel/kvm/kvm-accel-ops.c +2 -1
index b709187c7d7..80f0141a8a6 100644
@@ -47,13 +47,14 @@ static void *kvm_vcpu_thread_fn(void *arg)
     qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
     do {
+        qemu_wait_io_event(cpu);
+
         if (cpu_can_run(cpu)) {
             r = kvm_cpu_exec(cpu);
             if (r == EXCP_DEBUG) {
                 cpu_handle_guest_debug(cpu);
             }
         }
-        qemu_wait_io_event(cpu);
     } while (!cpu->unplug || cpu_can_run(cpu));
 
     kvm_destroy_vcpu(cpu);
accel/tcg/tcg-accel-ops-mttcg.c +2 -5
index 1148ebcaae5..04012900a30 100644
@@ -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_wait_io_event(cpu);
+
         if (cpu_can_run(cpu)) {
             int r;
             bql_unlock();
@@ -112,8 +111,6 @@ static void *mttcg_cpu_thread_fn(void *arg)
                 break;
             }
         }
-
-        qemu_wait_io_event(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
index 86869f133e9..f51244740d8 100644
@@ -42,16 +42,14 @@ static void *qemu_nvmm_cpu_thread_fn(void *arg)
     qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
     do {
+        qemu_wait_io_event(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_wait_io_event_common(cpu);
     } while (!cpu->unplug || cpu_can_run(cpu));
 
     nvmm_destroy_vcpu(cpu);
target/i386/whpx/whpx-accel-ops.c +2 -4
index da58805b1a6..611eeedeef7 100644
@@ -42,16 +42,14 @@ static void *whpx_cpu_thread_fn(void *arg)
     qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
     do {
+        qemu_wait_io_event(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_wait_io_event_common(cpu);
     } while (!cpu->unplug || cpu_can_run(cpu));
 
     whpx_destroy_vcpu(cpu);
Signature · 1 line
2.51.0
Reviewed-by on v2 17/18
#52 lore raw

Re: [PATCH 17/18] accel: make all calls to qemu_wait_io_event look the same

Quoted 6 lines · Paolo Bonzini

There is no reason for some accelerators to use qemu_wait_io_event_common (which is separated from qemu_wait_io_event() specifically for round robin). They can also check for events directly on the first pass through the loop, instead of setting cpu->exit_request to true.

Quoted 9 lines
---
  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(-)

To-do for myself: It appears as if we can reduce the number of checks for cpu == NULL in the rr loop by moving the cpu=first_cpu assignment to the right place.

r~

#53 lore raw

Re: [PATCH 17/18] accel: make all calls to qemu_wait_io_event look the same

Quoted 18 lines · Richard Henderson
Quoted 14 lines · Paolo Bonzini

There is no reason for some accelerators to use qemu_wait_io_event_common (which is separated from qemu_wait_io_event() specifically for round robin).  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 ++---

Quoted 1 line
  7 files changed, 30 insertions(+), 39 deletions(-)
Quoted 4 lines

To-do for myself: It appears as if we can reduce the number of checks for cpu == NULL in the rr loop by moving the cpu=first_cpu assignment to the right place.

This was my intent here:
https://lore.kernel.org/qemu-devel/[email protected]/

Reviewed-by on v2 17/18
#54 lore raw

Re: [PATCH 17/18] accel: make all calls to qemu_wait_io_event look the same

In reply to Paolo Bonzini
Quoted 6 lines · Paolo Bonzini

There is no reason for some accelerators to use qemu_wait_io_event_common (which is separated from qemu_wait_io_event() specifically for round robin). They can also check for events directly on the first pass through the loop, instead of setting cpu->exit_request to true.

Quoted 9 lines
---
  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(-)

Nice.

Reviewed-by on v2 17/18
#55 lore raw

Re: [PATCH 17/18] accel: make all calls to qemu_wait_io_event look the same

In reply to Paolo Bonzini
Quoted 6 lines · On Fri, 29 Aug 2025 17:31:14 +0200 Paolo Bonzini <[email protected]> wrote:

There is no reason for some accelerators to use qemu_wait_io_event_common (which is separated from qemu_wait_io_event() specifically for round robin). They can also check for events directly on the first pass through the loop, instead of setting cpu->exit_request to true.

Quoted 206 lines
---
 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
index 03cfc0fa01e..1f74c727c42 100644
@@ -43,6 +43,7 @@ static void *dummy_cpu_thread_fn(void *arg)
     qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
     do {
+        qemu_wait_io_event(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_wait_io_event(cpu);
     } while (!cpu->unplug);
 
     bql_unlock();
accel/hvf/hvf-accel-ops.c +1 -1
index d488d6afbac..4ba3e40831f 100644
@@ -192,13 +192,13 @@ static void *hvf_cpu_thread_fn(void *arg)
     qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
     do {
+        qemu_wait_io_event(cpu);
         if (cpu_can_run(cpu)) {
             r = hvf_vcpu_exec(cpu);
             if (r == EXCP_DEBUG) {
                 cpu_handle_guest_debug(cpu);
             }
         }
-        qemu_wait_io_event(cpu);
     } while (!cpu->unplug || cpu_can_run(cpu));
 
     hvf_vcpu_destroy(cpu);
accel/kvm/kvm-accel-ops.c +2 -1
index b709187c7d7..80f0141a8a6 100644
@@ -47,13 +47,14 @@ static void *kvm_vcpu_thread_fn(void *arg)
     qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
     do {
+        qemu_wait_io_event(cpu);
+
         if (cpu_can_run(cpu)) {
             r = kvm_cpu_exec(cpu);
             if (r == EXCP_DEBUG) {
                 cpu_handle_guest_debug(cpu);
             }
         }
-        qemu_wait_io_event(cpu);
     } while (!cpu->unplug || cpu_can_run(cpu));
 
     kvm_destroy_vcpu(cpu);
accel/tcg/tcg-accel-ops-mttcg.c +2 -5
index 1148ebcaae5..04012900a30 100644
@@ -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_wait_io_event(cpu);
+
         if (cpu_can_run(cpu)) {
             int r;
             bql_unlock();
@@ -112,8 +111,6 @@ static void *mttcg_cpu_thread_fn(void *arg)
                 break;
             }
         }
-
-        qemu_wait_io_event(cpu);
     } while (!cpu->unplug || cpu_can_run(cpu));
 
     tcg_cpu_destroy(cpu);
accel/tcg/tcg-accel-ops-rr.c +20 -23
index e9d291dc391..28897288db7 100644
@@ -211,13 +211,30 @@ static void *rr_cpu_thread_fn(void *arg)
 
     cpu = first_cpu;
 
-    /* process any pending work */
-    qatomic_set(&cpu->exit_request, true);
-
     while (1) {
         /* Only used for icount_enabled() */
         int64_t cpu_budget = 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()) {
+            /*
+             * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
+             * in the main_loop, wake it up in order to start the warp timer.
+             */
+            qemu_notify_event();
+        }
+
+        rr_wait_io_event();
+        rr_deal_with_unplugged_cpus();
+
         bql_unlock();
         replay_mutex_lock();
         bql_lock();
@@ -285,26 +302,6 @@ 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) {
-            /*
-             * 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()) {
-            /*
-             * When all cpus are sleeping (e.g in WFI), to avoid a deadlock
-             * in the main_loop, wake it up in order to start the warp timer.
-             */
-            qemu_notify_event();
-        }
-
-        rr_wait_io_event();
-        rr_deal_with_unplugged_cpus();
     }
 
     g_assert_not_reached();
target/i386/nvmm/nvmm-accel-ops.c +2 -4
index 86869f133e9..f51244740d8 100644
@@ -42,16 +42,14 @@ static void *qemu_nvmm_cpu_thread_fn(void *arg)
     qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
     do {
+        qemu_wait_io_event(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_wait_io_event_common(cpu);
     } while (!cpu->unplug || cpu_can_run(cpu));
 
     nvmm_destroy_vcpu(cpu);
target/i386/whpx/whpx-accel-ops.c +2 -4
index da58805b1a6..611eeedeef7 100644
@@ -42,16 +42,14 @@ static void *whpx_cpu_thread_fn(void *arg)
     qemu_guest_random_seed_thread_part2(cpu->random_seed);
 
     do {
+        qemu_wait_io_event(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_wait_io_event_common(cpu);
     } while (!cpu->unplug || cpu_can_run(cpu));
 
     whpx_destroy_vcpu(cpu);
#56 lore raw

PATCH v2 18/18 tcg/user: do not set exit_request gratuitously

In reply to Paolo Bonzini

Whenever 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
index ad94f96b252..7c20d9db122 100644
@@ -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
Reviewed-by on v2 18/18
#57 lore raw

Re: [PATCH 18/18] tcg/user: do not set exit_request gratuitously

Quoted 7 lines · On Fri, 29 Aug 2025 17:31:15 +0200 Paolo Bonzini <[email protected]> wrote:

Whenever 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.

Quoted 24 lines
---
 accel/tcg/cpu-exec.c | 2 ++
 1 file changed, 2 insertions(+)
accel/tcg/cpu-exec.c +2 -0
index ad94f96b252..7c20d9db122 100644
@@ -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);