Skip to contentSkip to conversation

accel/tcg: Yield round-robin execution when another vCPU wakes

PATCH In review Philippe Mathieu-Daudé 2 participants 9 messages Nov 19 – Nov 20

lore
Share & tools
lore
#0 lore raw

v2 cover accel/tcg: Yield round-robin execution when another vCPU wakes

v2:
- revert c5ec1bfad103 (Peter)
- cover icount_handle_interrupt (Alex)

Philippe Mathieu-Daudé (4):
accel/tcg: Yield round-robin execution when another vCPU wakes accel/tcg: Check current CPU in icount interrupt handler Revert "target/arm: don't pass env to the sev helper" target/arm: Kick other vCPUs on SEV under TCG round-robin

 target/arm/tcg/helper-defs.h     | 2 +-
 accel/tcg/tcg-accel-ops-icount.c | 2 +-
 accel/tcg/tcg-accel-ops.c        | 3 +++
 cpu-common.c                     | 3 +++
 target/arm/tcg/op_helper.c       | 6 +++---
 target/arm/tcg/translate-a64.c   | 2 +-
 target/arm/tcg/translate.c       | 2 +-
 7 files changed, 13 insertions(+), 7 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 1/4 accel/tcg: Yield round-robin execution when another vCPU wakes

An interrupt or inline run_on_cpu callback can wake another CPU that shares the sender's host thread. End the current slice in both paths so the target can run promptly.

---
 accel/tcg/tcg-accel-ops.c | 3 +++
 cpu-common.c              | 3 +++
 2 files changed, 6 insertions(+)
accel/tcg/tcg-accel-ops.c +3 -0
index 61e0051b53be..ec4e3577ead1 100644
@@ -107,6 +107,9 @@ void tcg_handle_interrupt(CPUState *cpu, int mask)
         qemu_cpu_kick(cpu);
     } else {
         qatomic_set(&cpu->neg.icount_decr.u16.high, -1);
+        if (current_cpu && current_cpu != cpu) {
+            cpu_exit(current_cpu);
+        }
     }
 }
 
cpu-common.c +3 -0
index adb76b3a786c..d518d1f40e81 100644
@@ -148,6 +148,9 @@ void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
 
     if (qemu_cpu_is_self(cpu)) {
         func(cpu, data);
+        if (current_cpu && current_cpu != cpu) {
+            cpu_exit(current_cpu);
+        }
         return;
     }
 
Signature · 1 line
2.55.0
#2 lore raw
Quoted 3 lines · Philippe Mathieu-Daudé

An interrupt or inline run_on_cpu callback can wake another CPU that shares the sender's host thread. End the current slice in both paths so the target can run promptly.

Why? Isn't this going to interfere with icount/replay?

r~

Quoted 35 lines
---
  accel/tcg/tcg-accel-ops.c | 3 +++
  cpu-common.c              | 3 +++
  2 files changed, 6 insertions(+)
accel/tcg/tcg-accel-ops.c +3 -0
index 61e0051b53be..ec4e3577ead1 100644
@@ -107,6 +107,9 @@ void tcg_handle_interrupt(CPUState *cpu, int mask)
          qemu_cpu_kick(cpu);
      } else {
          qatomic_set(&cpu->neg.icount_decr.u16.high, -1);
+        if (current_cpu && current_cpu != cpu) {
+            cpu_exit(current_cpu);
+        }
      }
  }
 
cpu-common.c +3 -0
index adb76b3a786c..d518d1f40e81 100644
@@ -148,6 +148,9 @@ void do_run_on_cpu(CPUState *cpu, run_on_cpu_func func, run_on_cpu_data data,
 
      if (qemu_cpu_is_self(cpu)) {
          func(cpu, data);
+        if (current_cpu && current_cpu != cpu) {
+            cpu_exit(current_cpu);
+        }
          return;
      }
 
#3 lore raw

PATCH v2 2/4 accel/tcg: Check current CPU in icount interrupt handler

In reply to Philippe Mathieu-Daudé

Under round-robin TCG, all vCPUs share one host thread, so qemu_cpu_is_self() is true for every vCPU. The icount interrupt sanity check can therefore abort when an interrupt targets a different vCPU sharing the current thread. Compare current_cpu with the target CPU instead.

---
 accel/tcg/tcg-accel-ops-icount.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
accel/tcg/tcg-accel-ops-icount.c +1 -1
index d0f7b410fab3..0313a4d128c5 100644
@@ -152,7 +152,7 @@ void icount_handle_interrupt(CPUState *cpu, int mask)
     int old_mask = cpu->interrupt_request;
 
     tcg_handle_interrupt(cpu, mask);
-    if (qemu_cpu_is_self(cpu) &&
+    if (current_cpu == cpu &&
         !cpu->neg.can_do_io
         && (mask & ~old_mask) != 0) {
         cpu_abort(cpu, "Raised interrupt while not in I/O function");
Signature · 1 line
2.55.0
Reviewed-by on v2 2/4
#4 lore raw

Re: [PATCH v2 2/4] accel/tcg: Check current CPU in icount interrupt handler

Quoted 11 lines · Philippe Mathieu-Daudé

Under round-robin TCG, all vCPUs share one host thread, so qemu_cpu_is_self() is true for every vCPU. The icount interrupt sanity check can therefore abort when an interrupt targets a different vCPU sharing the current thread. Compare current_cpu with the target CPU instead.

---
  accel/tcg/tcg-accel-ops-icount.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~

Quoted 14 lines
accel/tcg/tcg-accel-ops-icount.c +1 -1
index d0f7b410fab3..0313a4d128c5 100644
@@ -152,7 +152,7 @@ void icount_handle_interrupt(CPUState *cpu, int mask)
      int old_mask = cpu->interrupt_request;
 
      tcg_handle_interrupt(cpu, mask);
-    if (qemu_cpu_is_self(cpu) &&
+    if (current_cpu == cpu &&
          !cpu->neg.can_do_io
          && (mask & ~old_mask) != 0) {
          cpu_abort(cpu, "Raised interrupt while not in I/O function");
#5 lore raw

PATCH v2 3/4 Revert "target/arm: don't pass env to the sev helper"

In reply to Philippe Mathieu-Daudé

SEV needs the issuing CPU to distinguish it from other vCPUs sharing its host thread under round-robin TCG. Restore the explicit environment argument before fixing that comparison, avoiding a dependency on the global current_cpu pointer in the helper.

This reverts commit c5ec1bfad10346aefb8d7a18cea5118f2056f8a6.

---
 target/arm/tcg/helper-defs.h   | 2 +-
 target/arm/tcg/op_helper.c     | 2 +-
 target/arm/tcg/translate-a64.c | 2 +-
 target/arm/tcg/translate.c     | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
target/arm/tcg/helper-defs.h +1 -1
index 4f9736e6796d..269ac5150657 100644
@@ -60,7 +60,7 @@ DEF_HELPER_1(yield, void, env)
 DEF_HELPER_1(pre_hvc, void, env)
 DEF_HELPER_2(pre_smc, void, env, i32)
 DEF_HELPER_1(vesb, void, env)
-DEF_HELPER_0(sev, void)
+DEF_HELPER_1(sev, void, env)
 
 DEF_HELPER_3(cpsr_write, void, env, i32, i32)
 DEF_HELPER_2(cpsr_write_eret, void, env, i32)
target/arm/tcg/op_helper.c +1 -1
index b6113096b53b..a862b9eb02da 100644
@@ -468,7 +468,7 @@ void HELPER(wfit)(CPUARMState *env, uint32_t rd)
 #endif
 }
 
-void HELPER(sev)(void)
+void HELPER(sev)(CPUARMState *env)
 {
     CPUState *cs;
 
target/arm/tcg/translate-a64.c +1 -1
index 49472f7d9757..14888202bada 100644
@@ -2148,7 +2148,7 @@ static bool trans_SEV(DisasContext *s, arg_SEV *a)
      * SEV is a NOP for user-mode emulation.
      */
 #ifndef CONFIG_USER_ONLY
-    gen_helper_sev();
+    gen_helper_sev(tcg_env);
 #endif
     return true;
 }
target/arm/tcg/translate.c +1 -1
index 442c34ce84fc..9d80294c9bb2 100644
@@ -3282,7 +3282,7 @@ static bool trans_SEV(DisasContext *s, arg_SEV *a)
 #ifndef CONFIG_USER_ONLY
     if (arm_dc_feature(s, ARM_FEATURE_M) ||
         arm_dc_feature(s, ARM_FEATURE_V6K)) {
-        gen_helper_sev();
+        gen_helper_sev(tcg_env);
     }
 #endif
     return true;
Signature · 1 line
2.55.0
Reviewed-by on v2 3/4
#6 lore raw

Re: [PATCH v2 3/4] Revert "target/arm: don't pass env to the sev helper"

Quoted 8 lines · Philippe Mathieu-Daudé

SEV needs the issuing CPU to distinguish it from other vCPUs sharing its host thread under round-robin TCG. Restore the explicit environment argument before fixing that comparison, avoiding a dependency on the global current_cpu pointer in the helper.

This reverts commit c5ec1bfad10346aefb8d7a18cea5118f2056f8a6.

Quoted 8 lines
---
  target/arm/tcg/helper-defs.h   | 2 +-
  target/arm/tcg/op_helper.c     | 2 +-
  target/arm/tcg/translate-a64.c | 2 +-
  target/arm/tcg/translate.c     | 2 +-
  4 files changed, 4 insertions(+), 4 deletions(-)

r~

#7 lore raw

PATCH v2 4/4 target/arm: Kick other vCPUs on SEV under TCG round-robin

In reply to Philippe Mathieu-Daudé

In round-robin (single-thread) TCG, all vCPUs share one host thread, so qemu_cpu_is_self() is true for every vCPU.

The SEV helper used it to skip the kick of the issuing vCPU, and so skipped it for every other vCPU too: a vCPU sleeping in WFE is only woken when the RR kick timer fires.

Compare against the issuing vCPU instead.

Fixes d238858bff60 ("target/arm: Implement WFE, SEV and SEVONPEND")
---
 target/arm/tcg/op_helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/tcg/op_helper.c +2 -2
index a862b9eb02da..c2fdad7411db 100644
@@ -470,14 +470,14 @@ void HELPER(wfit)(CPUARMState *env, uint32_t rd)
 
 void HELPER(sev)(CPUARMState *env)
 {
-    CPUState *cs;
+    CPUState *self = env_cpu(env), *cs;
 
     BQL_LOCK_GUARD();
 
     CPU_FOREACH(cs) {
         ARMCPU *target_cpu = ARM_CPU(cs);
         target_cpu->env.event_register = true;
-        if (!qemu_cpu_is_self(cs)) {
+        if (cs != self) {
             qemu_cpu_kick(cs);
         }
     }
Signature · 1 line
2.55.0
Reviewed-by on v2 4/4
#8 lore raw

Re: [PATCH v2 4/4] target/arm: Kick other vCPUs on SEV under TCG round-robin

Quoted 7 lines · Philippe Mathieu-Daudé

In round-robin (single-thread) TCG, all vCPUs share one host thread, so qemu_cpu_is_self() is true for every vCPU.

The SEV helper used it to skip the kick of the issuing vCPU, and so skipped it for every other vCPU too: a vCPU sleeping in WFE is only woken when the RR kick timer fires.

Quoted 7 lines

Compare against the issuing vCPU instead.

Fixes d238858bff60 ("target/arm: Implement WFE, SEV and SEVONPEND")
---
  target/arm/tcg/op_helper.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

r~