All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH for-11.2 v2 0/2] target/arm: WFE wakeup after a failed store-exclusive
@ 2026-11-26 14:32 Frank Mehnert
  2026-11-26 14:32 ` [PATCH for-11.2 v2 1/2] target/arm: set the event register on " Frank Mehnert
  2026-11-26 14:32 ` [PATCH for-11.2 v2 2/2] tests/tcg/aarch64: test WFE after " Frank Mehnert
  0 siblings, 2 replies; 11+ messages in thread
From: Frank Mehnert @ 2026-11-26 14:32 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-arm, peter.maydell, alex.bennee, Frank Mehnert

A failing store-exclusive means that the global monitor of the PE was
cleared, which generates a WFE wakeup event. QEMU does not set the
event register in this case, so a vCPU spinning in the lock pattern
"WFE, load-exclusive, store-exclusive, on failure back to WFE" can sleep
in WFE forever although the lock is free.

Patch 1 sets the event register on a failed store-exclusive. Patch 2
adds a test case for this lock pattern to the WFxT test.

Changes since v1:
- Add the test case (patch 2).
- Add the Reviewed-by of Alex Bennée to patch 1.

Frank Mehnert (2):
  target/arm: set the event register on a failed store-exclusive
  tests/tcg/aarch64: test WFE after a failed store-exclusive

 target/arm/tcg/translate-a64.c       |   7 +-
 target/arm/tcg/translate.c           |   7 +-
 tests/tcg/aarch64/system/meson.build |   2 +-
 tests/tcg/aarch64/system/wfxt.c      | 103 ++++++++++++++++++++++++++-
 4 files changed, 115 insertions(+), 4 deletions(-)

-- 
2.55.0



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH for-11.2 v2 1/2] target/arm: set the event register on a failed store-exclusive
  2026-11-26 14:32 [PATCH for-11.2 v2 0/2] target/arm: WFE wakeup after a failed store-exclusive Frank Mehnert
@ 2026-11-26 14:32 ` Frank Mehnert
  2026-11-30 10:10   ` Peter Maydell
  2026-11-26 14:32 ` [PATCH for-11.2 v2 2/2] tests/tcg/aarch64: test WFE after " Frank Mehnert
  1 sibling, 1 reply; 11+ messages in thread
From: Frank Mehnert @ 2026-11-26 14:32 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-arm, peter.maydell, alex.bennee, Frank Mehnert

Since WFE may sleep on A-profile, a WFE executed outside an exclusive
region halts the vCPU until an event arrives. Lock code commonly
branches back to WFE after a failed store-exclusive, for example:

  1: wfe
     ldaxr  w1, [x0]
     cbnz   w1, 1b
     stxr   w2, w3, [x0]
     cbnz   w2, 1b

The store-exclusive clears exclusive_addr, so the following WFE sleeps.
On hardware, the store by another PE that made the store-exclusive fail
cleared the global monitor of this PE, and clearing the global monitor
sets the Event Register (Arm ARM D1.6.1, R_XRZRK). The WFE then
completes immediately. QEMU does not model this, so with interrupts
masked and no event stream the vCPU sleeps until another PE executes
SEV, which such lock code never does.

Set the Event Register when a store-exclusive fails, in both the
AArch64 and the AArch32 translator. A failure for another reason causes
a spurious wakeup, which the architecture permits.

Fixes: 01b223f895a7 ("target/arm: enable WFE sleeping for A-profile")
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4938
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>
---
 target/arm/tcg/translate-a64.c | 7 ++++++-
 target/arm/tcg/translate.c     | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c
index 49472f7d97..45f3b3df03 100644
--- a/target/arm/tcg/translate-a64.c
+++ b/target/arm/tcg/translate-a64.c
@@ -3531,10 +3531,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
         tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
     }
     tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
-    tcg_gen_br(done_label);
+    tcg_gen_brcondi_i64(TCG_COND_EQ, tmp, 0, done_label);
 
     gen_set_label(fail_label);
     tcg_gen_movi_i64(cpu_reg(s, rd), 1);
+    /*
+     * A failing store-exclusive means the global monitor was cleared, which
+     * generates a WFE wakeup event.
+     */
+    gen_event_reg();
     gen_set_label(done_label);
     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
 }
diff --git a/target/arm/tcg/translate.c b/target/arm/tcg/translate.c
index 442c34ce84..2eef2ceef2 100644
--- a/target/arm/tcg/translate.c
+++ b/target/arm/tcg/translate.c
@@ -2137,10 +2137,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
         tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t2);
     }
     tcg_gen_mov_i32(cpu_R[rd], t0);
-    tcg_gen_br(done_label);
+    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, done_label);
 
     gen_set_label(fail_label);
     tcg_gen_movi_i32(cpu_R[rd], 1);
+    /*
+     * A failing store-exclusive means the global monitor was cleared, which
+     * generates a WFE wakeup event.
+     */
+    gen_event_reg();
     gen_set_label(done_label);
     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
 }
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH for-11.2 v2 2/2] tests/tcg/aarch64: test WFE after a failed store-exclusive
  2026-11-26 14:32 [PATCH for-11.2 v2 0/2] target/arm: WFE wakeup after a failed store-exclusive Frank Mehnert
  2026-11-26 14:32 ` [PATCH for-11.2 v2 1/2] target/arm: set the event register on " Frank Mehnert
@ 2026-11-26 14:32 ` Frank Mehnert
  2026-11-26 15:09   ` Alex Bennée
  1 sibling, 1 reply; 11+ messages in thread
From: Frank Mehnert @ 2026-11-26 14:32 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-arm, peter.maydell, alex.bennee, Frank Mehnert

Add a test case for the common spin lock pattern "WFE, load-exclusive,
store-exclusive, on failure back to WFE", where the lock is released by
a plain store and without SEV.

CPU 0 executes LDAXR on the free lock, then CPU 1 takes the lock with
a plain store. The following STXR of CPU 0 must fail, and the WFE after
it must complete at once because the store of CPU 1 cleared the global
monitor of CPU 0, which generates a WFE wakeup event. The virtual
timer interrupt, 1s out, ends a WFE that misses the event, so the test
fails instead of hanging.

The test starts CPU 1 through PSCI and therefore needs two CPUs.

Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>
---
 tests/tcg/aarch64/system/meson.build |   2 +-
 tests/tcg/aarch64/system/wfxt.c      | 103 ++++++++++++++++++++++++++-
 2 files changed, 103 insertions(+), 2 deletions(-)

diff --git a/tests/tcg/aarch64/system/meson.build b/tests/tcg/aarch64/system/meson.build
index 8710ebfdad..a3cc77c1a0 100644
--- a/tests/tcg/aarch64/system/meson.build
+++ b/tests/tcg/aarch64/system/meson.build
@@ -75,7 +75,7 @@ tests += {
   'wfxt.c': {
     'cflags': cflags,
     'qemu_args': ['-M', 'virt,virtualization=on,gic-version=2',
-                  '-cpu', 'max', '-smp', '1',
+                  '-cpu', 'max', '-smp', '2',
                   '-semihosting-config', 'enable=on,arg=2',
                   qemu_base_args]
   },
diff --git a/tests/tcg/aarch64/system/wfxt.c b/tests/tcg/aarch64/system/wfxt.c
index 9d50590a68..2cec6cfbbe 100644
--- a/tests/tcg/aarch64/system/wfxt.c
+++ b/tests/tcg/aarch64/system/wfxt.c
@@ -1,5 +1,5 @@
 /*
- * WFIT/WFET timeout test
+ * WFIT/WFET timeout test, WFE wakeup after a failed store-exclusive
  *
  * Copyright (c) 2026 Google LLC
  * Author: Fuad Tabba <fuad.tabba@linux.dev>
@@ -91,6 +91,106 @@ static int test_one(const char *name, void (*wait)(uint64_t), uint64_t freq)
     return 0;
 }
 
+/*
+ * At EL2 with virtualization enabled, QEMU's PSCI implementation uses the
+ * SMC conduit.
+ */
+#define PSCI_CPU_ON 0xc4000003UL
+
+static uint64_t psci_cpu_on(uint64_t mpidr, uint64_t entry)
+{
+    register uint64_t x0 asm("x0") = PSCI_CPU_ON;
+    register uint64_t x1 asm("x1") = mpidr;
+    register uint64_t x2 asm("x2") = entry;
+    register uint64_t x3 asm("x3") = 0;
+
+    asm volatile("smc #0" : "+r" (x0) : "r" (x1), "r" (x2), "r" (x3)
+                 : "memory");
+    return x0;
+}
+
+static uint32_t lock_word;
+static uint32_t lock_step;
+
+asm(".pushsection .bss\n"
+    ".balign 16\n"
+    "secondary_stack:\n"
+    ".space 4096\n"
+    "secondary_stack_end:\n"
+    ".popsection\n"
+    ".global secondary_entry\n"
+    "secondary_entry:\n"
+    "adrp x0, secondary_stack_end\n"
+    "add x0, x0, :lo12:secondary_stack_end\n"
+    "mov sp, x0\n"
+    "b secondary_main\n");
+
+/* CPU 1 takes the lock while CPU 0 is between LDAXR and STXR. */
+static void __attribute__((used)) secondary_main(void)
+{
+    while (__atomic_load_n(&lock_step, __ATOMIC_ACQUIRE) != 1) {
+    }
+    __atomic_store_n(&lock_word, 1, __ATOMIC_RELAXED);
+    __atomic_store_n(&lock_step, 2, __ATOMIC_RELEASE);
+
+    for (;;) {
+        asm volatile("wfi");
+    }
+}
+
+/*
+ * Spin lock pattern: "LDAXR, STXR, on failure WFE" with a lock released
+ * by a plain store and without SEV. The store-exclusive fails because
+ * another CPU took the lock. That store cleared the global monitor of
+ * this CPU, which generates a WFE wakeup event, so the following WFE
+ * must complete at once.
+ */
+static int test_wfe_lock(uint64_t freq)
+{
+    uint64_t entry, now, elapsed;
+    uint32_t val, status;
+
+    asm("adrp %0, secondary_entry\n"
+        "add %0, %0, :lo12:secondary_entry" : "=r" (entry));
+    if (psci_cpu_on(1, entry) != 0) {
+        ml_printf("FAIL: cannot start CPU 1\n");
+        return 1;
+    }
+
+    /* Clear the event register */
+    asm volatile("sevl; wfe");
+
+    asm volatile("ldaxr %w0, [%1]" : "=r" (val) : "r" (&lock_word)
+                 : "memory");
+    __atomic_store_n(&lock_step, 1, __ATOMIC_RELEASE);
+    while (__atomic_load_n(&lock_step, __ATOMIC_ACQUIRE) != 2) {
+    }
+    asm volatile("stxr %w0, %w2, [%1]" : "=&r" (status)
+                 : "r" (&lock_word), "r" (1) : "memory");
+
+    if (val != 0 || status == 0) {
+        ml_printf("FAIL: store-exclusive succeeded on a taken lock\n");
+        return 1;
+    }
+
+    /* The virtual timer interrupt, 1s out, wakes the CPU if WFE sleeps */
+    now = read_sysreg(cntvct_el0);
+    write_sysreg(cntv_cval_el0, now + freq);
+    write_sysreg(cntv_ctl_el0, 1);
+
+    asm volatile("wfe");
+
+    elapsed = read_sysreg(cntvct_el0) - now;
+    write_sysreg(cntv_ctl_el0, 0);
+
+    ml_printf("wfe after failed stxr: woke after %ld ticks\n", elapsed);
+    if (elapsed > freq / 2) {
+        ml_printf("FAIL: wfe missed the event of the failed stxr\n");
+        return 1;
+    }
+    return 0;
+}
+
 int main(void)
 {
     uint64_t freq;
@@ -113,6 +213,7 @@ int main(void)
 
     ret = test_one("wfit", wfit, freq);
     ret |= test_one("wfet", wfet, freq);
+    ret |= test_wfe_lock(freq);
 
     return ret;
 }
-- 
2.55.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH for-11.2 v2 2/2] tests/tcg/aarch64: test WFE after a failed store-exclusive
  2026-11-26 14:32 ` [PATCH for-11.2 v2 2/2] tests/tcg/aarch64: test WFE after " Frank Mehnert
@ 2026-11-26 15:09   ` Alex Bennée
  0 siblings, 0 replies; 11+ messages in thread
From: Alex Bennée @ 2026-11-26 15:09 UTC (permalink / raw)
  To: Frank Mehnert; +Cc: qemu-devel, qemu-arm, peter.maydell, Frank Mehnert

Frank Mehnert <frank.mehnert@gmail.com> writes:

> Add a test case for the common spin lock pattern "WFE, load-exclusive,
> store-exclusive, on failure back to WFE", where the lock is released by
> a plain store and without SEV.
>
> CPU 0 executes LDAXR on the free lock, then CPU 1 takes the lock with
> a plain store. The following STXR of CPU 0 must fail, and the WFE after
> it must complete at once because the store of CPU 1 cleared the global
> monitor of CPU 0, which generates a WFE wakeup event. The virtual
> timer interrupt, 1s out, ends a WFE that misses the event, so the test
> fails instead of hanging.
>
> The test starts CPU 1 through PSCI and therefore needs two CPUs.
>
> Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>

Tested-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH for-11.2 v2 1/2] target/arm: set the event register on a failed store-exclusive
  2026-11-26 14:32 ` [PATCH for-11.2 v2 1/2] target/arm: set the event register on " Frank Mehnert
@ 2026-11-30 10:10   ` Peter Maydell
  2026-11-30 13:56     ` Alex Bennée
  0 siblings, 1 reply; 11+ messages in thread
From: Peter Maydell @ 2026-11-30 10:10 UTC (permalink / raw)
  To: Frank Mehnert
  Cc: qemu-devel, qemu-arm, alex.bennee, Frank Mehnert,
	Richard Henderson

On Thu, 26 Nov 2026 at 14:33, Frank Mehnert <frank.mehnert@gmail.com> wrote:
>
> Since WFE may sleep on A-profile, a WFE executed outside an exclusive
> region halts the vCPU until an event arrives. Lock code commonly
> branches back to WFE after a failed store-exclusive, for example:
>
>   1: wfe
>      ldaxr  w1, [x0]
>      cbnz   w1, 1b
>      stxr   w2, w3, [x0]
>      cbnz   w2, 1b
>
> The store-exclusive clears exclusive_addr, so the following WFE sleeps.
> On hardware, the store by another PE that made the store-exclusive fail
> cleared the global monitor of this PE, and clearing the global monitor
> sets the Event Register (Arm ARM D1.6.1, R_XRZRK). The WFE then
> completes immediately. QEMU does not model this, so with interrupts
> masked and no event stream the vCPU sleeps until another PE executes
> SEV, which such lock code never does.
>
> Set the Event Register when a store-exclusive fails, in both the
> AArch64 and the AArch32 translator. A failure for another reason causes
> a spurious wakeup, which the architecture permits.
>
> Fixes: 01b223f895a7 ("target/arm: enable WFE sleeping for A-profile")
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4938
> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>
> ---
>  target/arm/tcg/translate-a64.c | 7 ++++++-
>  target/arm/tcg/translate.c     | 7 ++++++-
>  2 files changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c
> index 49472f7d97..45f3b3df03 100644
> --- a/target/arm/tcg/translate-a64.c
> +++ b/target/arm/tcg/translate-a64.c
> @@ -3531,10 +3531,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
>          tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
>      }
>      tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
> -    tcg_gen_br(done_label);
> +    tcg_gen_brcondi_i64(TCG_COND_EQ, tmp, 0, done_label);
>
>      gen_set_label(fail_label);
>      tcg_gen_movi_i64(cpu_reg(s, rd), 1);
> +    /*
> +     * A failing store-exclusive means the global monitor was cleared, which
> +     * generates a WFE wakeup event.
> +     */

We might add "Architecturally the wakeup event should have happened when
the store by the other CPU happened, but we do it here."

> +    gen_event_reg();
>      gen_set_label(done_label);
>      tcg_gen_movi_i64(cpu_exclusive_addr, -1);

The architecture permits the global monitor to be cleared when a
StoreExcl() succeeds as well as when it fails. Given that gen_event_reg()
is just a byte-store, we could also unconditionally do it after the
done_label, to avoid the branching around. Not sure which is preferable.

>  }
> diff --git a/target/arm/tcg/translate.c b/target/arm/tcg/translate.c
> index 442c34ce84..2eef2ceef2 100644
> --- a/target/arm/tcg/translate.c
> +++ b/target/arm/tcg/translate.c
> @@ -2137,10 +2137,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
>          tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t2);
>      }
>      tcg_gen_mov_i32(cpu_R[rd], t0);
> -    tcg_gen_br(done_label);
> +    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, done_label);
>
>      gen_set_label(fail_label);
>      tcg_gen_movi_i32(cpu_R[rd], 1);
> +    /*
> +     * A failing store-exclusive means the global monitor was cleared, which
> +     * generates a WFE wakeup event.
> +     */
> +    gen_event_reg();
>      gen_set_label(done_label);
>      tcg_gen_movi_i64(cpu_exclusive_addr, -1);
>  }
> --
> 2.55.0

-- PMM


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH for-11.2 v2 1/2] target/arm: set the event register on a failed store-exclusive
  2026-11-30 10:10   ` Peter Maydell
@ 2026-11-30 13:56     ` Alex Bennée
  2026-11-30 15:00       ` Peter Maydell
  0 siblings, 1 reply; 11+ messages in thread
From: Alex Bennée @ 2026-11-30 13:56 UTC (permalink / raw)
  To: Peter Maydell
  Cc: Frank Mehnert, qemu-devel, qemu-arm, Frank Mehnert,
	Richard Henderson

Peter Maydell <peter.maydell@linaro.org> writes:

> On Thu, 26 Nov 2026 at 14:33, Frank Mehnert <frank.mehnert@gmail.com> wrote:
>>
>> Since WFE may sleep on A-profile, a WFE executed outside an exclusive
>> region halts the vCPU until an event arrives. Lock code commonly
>> branches back to WFE after a failed store-exclusive, for example:
>>
>>   1: wfe
>>      ldaxr  w1, [x0]
>>      cbnz   w1, 1b
>>      stxr   w2, w3, [x0]
>>      cbnz   w2, 1b
>>
>> The store-exclusive clears exclusive_addr, so the following WFE sleeps.
>> On hardware, the store by another PE that made the store-exclusive fail
>> cleared the global monitor of this PE, and clearing the global monitor
>> sets the Event Register (Arm ARM D1.6.1, R_XRZRK). The WFE then
>> completes immediately. QEMU does not model this, so with interrupts
>> masked and no event stream the vCPU sleeps until another PE executes
>> SEV, which such lock code never does.
>>
>> Set the Event Register when a store-exclusive fails, in both the
>> AArch64 and the AArch32 translator. A failure for another reason causes
>> a spurious wakeup, which the architecture permits.
>>
>> Fixes: 01b223f895a7 ("target/arm: enable WFE sleeping for A-profile")
>> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4938
>> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
>> Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>
>> ---
>>  target/arm/tcg/translate-a64.c | 7 ++++++-
>>  target/arm/tcg/translate.c     | 7 ++++++-
>>  2 files changed, 12 insertions(+), 2 deletions(-)
>>
>> diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c
>> index 49472f7d97..45f3b3df03 100644
>> --- a/target/arm/tcg/translate-a64.c
>> +++ b/target/arm/tcg/translate-a64.c
>> @@ -3531,10 +3531,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
>>          tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
>>      }
>>      tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
>> -    tcg_gen_br(done_label);
>> +    tcg_gen_brcondi_i64(TCG_COND_EQ, tmp, 0, done_label);
>>
>>      gen_set_label(fail_label);
>>      tcg_gen_movi_i64(cpu_reg(s, rd), 1);
>> +    /*
>> +     * A failing store-exclusive means the global monitor was cleared, which
>> +     * generates a WFE wakeup event.
>> +     */
>
> We might add "Architecturally the wakeup event should have happened when
> the store by the other CPU happened, but we do it here."
>
>> +    gen_event_reg();
>>      gen_set_label(done_label);
>>      tcg_gen_movi_i64(cpu_exclusive_addr, -1);
>
> The architecture permits the global monitor to be cleared when a
> StoreExcl() succeeds as well as when it fails. Given that gen_event_reg()
> is just a byte-store, we could also unconditionally do it after the
> done_label, to avoid the branching around. Not sure which is
> preferable.

Isn't the global monitor subtly different from the per-vcpu event
register in this case as we are modelling the effect of it being cleared
without properly modelling the actual global monitor itself? Or does our
messing with cpu_exclusive_addr count?

However making the store occur always still passes the test case so I
don't have any objection if this is easier.

>
>>  }
>> diff --git a/target/arm/tcg/translate.c b/target/arm/tcg/translate.c
>> index 442c34ce84..2eef2ceef2 100644
>> --- a/target/arm/tcg/translate.c
>> +++ b/target/arm/tcg/translate.c
>> @@ -2137,10 +2137,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
>>          tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t2);
>>      }
>>      tcg_gen_mov_i32(cpu_R[rd], t0);
>> -    tcg_gen_br(done_label);
>> +    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, done_label);
>>
>>      gen_set_label(fail_label);
>>      tcg_gen_movi_i32(cpu_R[rd], 1);
>> +    /*
>> +     * A failing store-exclusive means the global monitor was cleared, which
>> +     * generates a WFE wakeup event.
>> +     */
>> +    gen_event_reg();
>>      gen_set_label(done_label);
>>      tcg_gen_movi_i64(cpu_exclusive_addr, -1);
>>  }
>> --
>> 2.55.0
>
> -- PMM

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH for-11.2 v2 1/2] target/arm: set the event register on a failed store-exclusive
  2026-11-30 13:56     ` Alex Bennée
@ 2026-11-30 15:00       ` Peter Maydell
  2026-11-30 15:53         ` Alex Bennée
  0 siblings, 1 reply; 11+ messages in thread
From: Peter Maydell @ 2026-11-30 15:00 UTC (permalink / raw)
  To: Alex Bennée
  Cc: Frank Mehnert, qemu-devel, qemu-arm, Frank Mehnert,
	Richard Henderson

On Mon, 30 Nov 2026 at 13:56, Alex Bennée <alex.bennee@linaro.org> wrote:
>
> Peter Maydell <peter.maydell@linaro.org> writes:
>
> > On Thu, 26 Nov 2026 at 14:33, Frank Mehnert <frank.mehnert@gmail.com> wrote:
> >>
> >> Since WFE may sleep on A-profile, a WFE executed outside an exclusive
> >> region halts the vCPU until an event arrives. Lock code commonly
> >> branches back to WFE after a failed store-exclusive, for example:
> >>
> >>   1: wfe
> >>      ldaxr  w1, [x0]
> >>      cbnz   w1, 1b
> >>      stxr   w2, w3, [x0]
> >>      cbnz   w2, 1b
> >>
> >> The store-exclusive clears exclusive_addr, so the following WFE sleeps.
> >> On hardware, the store by another PE that made the store-exclusive fail
> >> cleared the global monitor of this PE, and clearing the global monitor
> >> sets the Event Register (Arm ARM D1.6.1, R_XRZRK). The WFE then
> >> completes immediately. QEMU does not model this, so with interrupts
> >> masked and no event stream the vCPU sleeps until another PE executes
> >> SEV, which such lock code never does.
> >>
> >> Set the Event Register when a store-exclusive fails, in both the
> >> AArch64 and the AArch32 translator. A failure for another reason causes
> >> a spurious wakeup, which the architecture permits.
> >>
> >> Fixes: 01b223f895a7 ("target/arm: enable WFE sleeping for A-profile")
> >> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4938
> >> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> >> Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>
> >> ---
> >>  target/arm/tcg/translate-a64.c | 7 ++++++-
> >>  target/arm/tcg/translate.c     | 7 ++++++-
> >>  2 files changed, 12 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c
> >> index 49472f7d97..45f3b3df03 100644
> >> --- a/target/arm/tcg/translate-a64.c
> >> +++ b/target/arm/tcg/translate-a64.c
> >> @@ -3531,10 +3531,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
> >>          tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
> >>      }
> >>      tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
> >> -    tcg_gen_br(done_label);
> >> +    tcg_gen_brcondi_i64(TCG_COND_EQ, tmp, 0, done_label);
> >>
> >>      gen_set_label(fail_label);
> >>      tcg_gen_movi_i64(cpu_reg(s, rd), 1);
> >> +    /*
> >> +     * A failing store-exclusive means the global monitor was cleared, which
> >> +     * generates a WFE wakeup event.
> >> +     */
> >
> > We might add "Architecturally the wakeup event should have happened when
> > the store by the other CPU happened, but we do it here."
> >
> >> +    gen_event_reg();
> >>      gen_set_label(done_label);
> >>      tcg_gen_movi_i64(cpu_exclusive_addr, -1);
> >
> > The architecture permits the global monitor to be cleared when a
> > StoreExcl() succeeds as well as when it fails. Given that gen_event_reg()
> > is just a byte-store, we could also unconditionally do it after the
> > done_label, to avoid the branching around. Not sure which is
> > preferable.
>
> Isn't the global monitor subtly different from the per-vcpu event
> register in this case as we are modelling the effect of it being cleared
> without properly modelling the actual global monitor itself? Or does our
> messing with cpu_exclusive_addr count?

We're quite a long way out of spec on our exclusives handling, so
anything we're doing here is in the realm of "try to make the common
cases continue to work". The interaction here is that the event register
spec defines that the CPU's event register is cleared when the global
monitor for that CPU goes from "exclusive" to "open". Since we don't
really model the global monitor we're trying to patch things up by
doing it after the fact for at least some cases where we notice that
that transition would have happened. There will still be cases
where we should have set the event register and don't, we just hope
they don't matter in practice...

> However making the store occur always still passes the test case so I
> don't have any objection if this is easier.

Given that you're allowed to set the event register for any
IMPDEF reason, setting it more often is always safe. The tradeoff
I think is between "every store exclusive takes an extra conditional
branch" and "WFE spuriously wakes up more often". As I say, I don't
know which side of that we should prefer.

thanks
-- PMM


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH for-11.2 v2 1/2] target/arm: set the event register on a failed store-exclusive
  2026-11-30 15:00       ` Peter Maydell
@ 2026-11-30 15:53         ` Alex Bennée
  2026-11-30 16:23           ` Peter Maydell
  0 siblings, 1 reply; 11+ messages in thread
From: Alex Bennée @ 2026-11-30 15:53 UTC (permalink / raw)
  To: Peter Maydell
  Cc: Frank Mehnert, qemu-devel, qemu-arm, Frank Mehnert,
	Richard Henderson

Peter Maydell <peter.maydell@linaro.org> writes:

> On Mon, 30 Nov 2026 at 13:56, Alex Bennée <alex.bennee@linaro.org> wrote:
>>
>> Peter Maydell <peter.maydell@linaro.org> writes:
>>
>> > On Thu, 26 Nov 2026 at 14:33, Frank Mehnert <frank.mehnert@gmail.com> wrote:
>> >>
>> >> Since WFE may sleep on A-profile, a WFE executed outside an exclusive
>> >> region halts the vCPU until an event arrives. Lock code commonly
>> >> branches back to WFE after a failed store-exclusive, for example:
>> >>
>> >>   1: wfe
>> >>      ldaxr  w1, [x0]
>> >>      cbnz   w1, 1b
>> >>      stxr   w2, w3, [x0]
>> >>      cbnz   w2, 1b
>> >>
>> >> The store-exclusive clears exclusive_addr, so the following WFE sleeps.
>> >> On hardware, the store by another PE that made the store-exclusive fail
>> >> cleared the global monitor of this PE, and clearing the global monitor
>> >> sets the Event Register (Arm ARM D1.6.1, R_XRZRK). The WFE then
>> >> completes immediately. QEMU does not model this, so with interrupts
>> >> masked and no event stream the vCPU sleeps until another PE executes
>> >> SEV, which such lock code never does.
>> >>
>> >> Set the Event Register when a store-exclusive fails, in both the
>> >> AArch64 and the AArch32 translator. A failure for another reason causes
>> >> a spurious wakeup, which the architecture permits.
>> >>
>> >> Fixes: 01b223f895a7 ("target/arm: enable WFE sleeping for A-profile")
>> >> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4938
>> >> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
>> >> Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>
>> >> ---
>> >>  target/arm/tcg/translate-a64.c | 7 ++++++-
>> >>  target/arm/tcg/translate.c     | 7 ++++++-
>> >>  2 files changed, 12 insertions(+), 2 deletions(-)
>> >>
>> >> diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c
>> >> index 49472f7d97..45f3b3df03 100644
>> >> --- a/target/arm/tcg/translate-a64.c
>> >> +++ b/target/arm/tcg/translate-a64.c
>> >> @@ -3531,10 +3531,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
>> >>          tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
>> >>      }
>> >>      tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
>> >> -    tcg_gen_br(done_label);
>> >> +    tcg_gen_brcondi_i64(TCG_COND_EQ, tmp, 0, done_label);
>> >>
>> >>      gen_set_label(fail_label);
>> >>      tcg_gen_movi_i64(cpu_reg(s, rd), 1);
>> >> +    /*
>> >> +     * A failing store-exclusive means the global monitor was cleared, which
>> >> +     * generates a WFE wakeup event.
>> >> +     */
>> >
>> > We might add "Architecturally the wakeup event should have happened when
>> > the store by the other CPU happened, but we do it here."
>> >
>> >> +    gen_event_reg();
>> >>      gen_set_label(done_label);
>> >>      tcg_gen_movi_i64(cpu_exclusive_addr, -1);
>> >
>> > The architecture permits the global monitor to be cleared when a
>> > StoreExcl() succeeds as well as when it fails. Given that gen_event_reg()
>> > is just a byte-store, we could also unconditionally do it after the
>> > done_label, to avoid the branching around. Not sure which is
>> > preferable.
>>
>> Isn't the global monitor subtly different from the per-vcpu event
>> register in this case as we are modelling the effect of it being cleared
>> without properly modelling the actual global monitor itself? Or does our
>> messing with cpu_exclusive_addr count?
>
> We're quite a long way out of spec on our exclusives handling, so
> anything we're doing here is in the realm of "try to make the common
> cases continue to work". The interaction here is that the event register
> spec defines that the CPU's event register is cleared when the global
> monitor for that CPU goes from "exclusive" to "open". Since we don't
> really model the global monitor we're trying to patch things up by
> doing it after the fact for at least some cases where we notice that
> that transition would have happened. There will still be cases
> where we should have set the event register and don't, we just hope
> they don't matter in practice...
>
>> However making the store occur always still passes the test case so I
>> don't have any objection if this is easier.
>
> Given that you're allowed to set the event register for any
> IMPDEF reason, setting it more often is always safe. The tradeoff
> I think is between "every store exclusive takes an extra conditional
> branch" and "WFE spuriously wakes up more often". As I say, I don't
> know which side of that we should prefer.

WFE doesn't wake up here, the event register is local to the PE which is
currently executing a StoreExcl - it just skips the first attempt to
sleep after the StoreExcl.

Only SEV broadcasts causes the other PE's to wake up.

>
> thanks
> -- PMM

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH for-11.2 v2 1/2] target/arm: set the event register on a failed store-exclusive
  2026-11-30 15:53         ` Alex Bennée
@ 2026-11-30 16:23           ` Peter Maydell
  2026-11-30 16:35             ` Frank Mehnert
  0 siblings, 1 reply; 11+ messages in thread
From: Peter Maydell @ 2026-11-30 16:23 UTC (permalink / raw)
  To: Alex Bennée
  Cc: Frank Mehnert, qemu-devel, qemu-arm, Frank Mehnert,
	Richard Henderson

On Mon, 30 Nov 2026 at 15:53, Alex Bennée <alex.bennee@linaro.org> wrote:
>
> Peter Maydell <peter.maydell@linaro.org> writes:
>
> > On Mon, 30 Nov 2026 at 13:56, Alex Bennée <alex.bennee@linaro.org> wrote:
> >>
> >> Peter Maydell <peter.maydell@linaro.org> writes:
> >>
> >> > On Thu, 26 Nov 2026 at 14:33, Frank Mehnert <frank.mehnert@gmail.com> wrote:
> >> >>
> >> >> Since WFE may sleep on A-profile, a WFE executed outside an exclusive
> >> >> region halts the vCPU until an event arrives. Lock code commonly
> >> >> branches back to WFE after a failed store-exclusive, for example:
> >> >>
> >> >>   1: wfe
> >> >>      ldaxr  w1, [x0]
> >> >>      cbnz   w1, 1b
> >> >>      stxr   w2, w3, [x0]
> >> >>      cbnz   w2, 1b
> >> >>
> >> >> The store-exclusive clears exclusive_addr, so the following WFE sleeps.
> >> >> On hardware, the store by another PE that made the store-exclusive fail
> >> >> cleared the global monitor of this PE, and clearing the global monitor
> >> >> sets the Event Register (Arm ARM D1.6.1, R_XRZRK). The WFE then
> >> >> completes immediately. QEMU does not model this, so with interrupts
> >> >> masked and no event stream the vCPU sleeps until another PE executes
> >> >> SEV, which such lock code never does.
> >> >>
> >> >> Set the Event Register when a store-exclusive fails, in both the
> >> >> AArch64 and the AArch32 translator. A failure for another reason causes
> >> >> a spurious wakeup, which the architecture permits.
> >> >>
> >> >> Fixes: 01b223f895a7 ("target/arm: enable WFE sleeping for A-profile")
> >> >> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4938
> >> >> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> >> >> Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>
> >> >> ---
> >> >>  target/arm/tcg/translate-a64.c | 7 ++++++-
> >> >>  target/arm/tcg/translate.c     | 7 ++++++-
> >> >>  2 files changed, 12 insertions(+), 2 deletions(-)
> >> >>
> >> >> diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c
> >> >> index 49472f7d97..45f3b3df03 100644
> >> >> --- a/target/arm/tcg/translate-a64.c
> >> >> +++ b/target/arm/tcg/translate-a64.c
> >> >> @@ -3531,10 +3531,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
> >> >>          tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
> >> >>      }
> >> >>      tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
> >> >> -    tcg_gen_br(done_label);
> >> >> +    tcg_gen_brcondi_i64(TCG_COND_EQ, tmp, 0, done_label);
> >> >>
> >> >>      gen_set_label(fail_label);
> >> >>      tcg_gen_movi_i64(cpu_reg(s, rd), 1);
> >> >> +    /*
> >> >> +     * A failing store-exclusive means the global monitor was cleared, which
> >> >> +     * generates a WFE wakeup event.
> >> >> +     */
> >> >
> >> > We might add "Architecturally the wakeup event should have happened when
> >> > the store by the other CPU happened, but we do it here."
> >> >
> >> >> +    gen_event_reg();
> >> >>      gen_set_label(done_label);
> >> >>      tcg_gen_movi_i64(cpu_exclusive_addr, -1);
> >> >
> >> > The architecture permits the global monitor to be cleared when a
> >> > StoreExcl() succeeds as well as when it fails. Given that gen_event_reg()
> >> > is just a byte-store, we could also unconditionally do it after the
> >> > done_label, to avoid the branching around. Not sure which is
> >> > preferable.
> >>
> >> Isn't the global monitor subtly different from the per-vcpu event
> >> register in this case as we are modelling the effect of it being cleared
> >> without properly modelling the actual global monitor itself? Or does our
> >> messing with cpu_exclusive_addr count?
> >
> > We're quite a long way out of spec on our exclusives handling, so
> > anything we're doing here is in the realm of "try to make the common
> > cases continue to work". The interaction here is that the event register
> > spec defines that the CPU's event register is cleared when the global
> > monitor for that CPU goes from "exclusive" to "open". Since we don't
> > really model the global monitor we're trying to patch things up by
> > doing it after the fact for at least some cases where we notice that
> > that transition would have happened. There will still be cases
> > where we should have set the event register and don't, we just hope
> > they don't matter in practice...
> >
> >> However making the store occur always still passes the test case so I
> >> don't have any objection if this is easier.
> >
> > Given that you're allowed to set the event register for any
> > IMPDEF reason, setting it more often is always safe. The tradeoff
> > I think is between "every store exclusive takes an extra conditional
> > branch" and "WFE spuriously wakes up more often". As I say, I don't
> > know which side of that we should prefer.
>
> WFE doesn't wake up here, the event register is local to the PE which is
> currently executing a StoreExcl - it just skips the first attempt to
> sleep after the StoreExcl.

Yes, which is WFE waking up spuriously (it could/should have gone to
sleep but immediately returned instead).

> Only SEV broadcasts causes the other PE's to wake up.

Other PEs doing stores can also cause other PEs to wake up:

 * the state for CPU X in a global monitor goes from "Exclusive
   Access" to "Open Access" if CPU Y stores to the marked address
   (i.e. to an address that CPU X has done a LoadExcl on)
   [fig B2-5 in B2.12.2.1.1 in rev M.d of the Arm ARM, specifically
   the "Store(Marked_address,!n)" transition]
 * clearing the global monitor for CPU X sets the event
   register for CPU X
   [R_XRZRK, section D1.7.1]
 * this event is a WFE wakeup event for CPU X
   [R_KBMFC, section D1.7.1.4]

So architecturally a sequence like "ldrex from addr; wfe" is supposed to
be wakeable by another CPU doing a plain store to addr.

Actually that makes me wonder; in the quoted lock sequence in the
commit message / Arm ARM K11.3.4.1:

     sevl   // make sure first iteration wfe doesn't sleep
  1: wfe
     ldaxr  w1, [x0]
     cbnz   w1, 1b
     stxr   w2, w3, [x0]
     cbnz   w2, 1b

and its corresponding lock-release:
     stlr wzr, [x1]

aren't we missing this case?

This patch sets the event register when the stxr fails, which
means we won't deadlock when we go round the loop from the
second cbnz. But there's also a branch back up to the WFE for
the "ldaxr load was not 0" case (i.e. where some other CPU already
holds the lock). Architecturally the thing that wakes us up from
that WFE is the fact that the ldaxr means our global monitor
state is "Exclusive Access" and the other CPU doing the STLR
into the lock address will push us out to Open Access, setting
the event register and waking up the WFE.

thanks
-- PMM


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH for-11.2 v2 1/2] target/arm: set the event register on a failed store-exclusive
  2026-11-30 16:23           ` Peter Maydell
@ 2026-11-30 16:35             ` Frank Mehnert
  2026-11-30 16:53               ` Peter Maydell
  0 siblings, 1 reply; 11+ messages in thread
From: Frank Mehnert @ 2026-11-30 16:35 UTC (permalink / raw)
  To: Alex Bennée, Peter Maydell; +Cc: qemu-devel, qemu-arm, Richard Henderson

On Montag, 30. November 2026 17:23 Peter Maydell wrote:
> On Mon, 30 Nov 2026 at 15:53, Alex Bennée <alex.bennee@linaro.org> wrote:
> >
> > Peter Maydell <peter.maydell@linaro.org> writes:
> >
> > > On Mon, 30 Nov 2026 at 13:56, Alex Bennée <alex.bennee@linaro.org> wrote:
> > >>
> > >> Peter Maydell <peter.maydell@linaro.org> writes:
> > >>
> > >> > On Thu, 26 Nov 2026 at 14:33, Frank Mehnert <frank.mehnert@gmail.com> wrote:
> > >> >>
> > >> >> Since WFE may sleep on A-profile, a WFE executed outside an exclusive
> > >> >> region halts the vCPU until an event arrives. Lock code commonly
> > >> >> branches back to WFE after a failed store-exclusive, for example:
> > >> >>
> > >> >>   1: wfe
> > >> >>      ldaxr  w1, [x0]
> > >> >>      cbnz   w1, 1b
> > >> >>      stxr   w2, w3, [x0]
> > >> >>      cbnz   w2, 1b
> > >> >>
> > >> >> The store-exclusive clears exclusive_addr, so the following WFE sleeps.
> > >> >> On hardware, the store by another PE that made the store-exclusive fail
> > >> >> cleared the global monitor of this PE, and clearing the global monitor
> > >> >> sets the Event Register (Arm ARM D1.6.1, R_XRZRK). The WFE then
> > >> >> completes immediately. QEMU does not model this, so with interrupts
> > >> >> masked and no event stream the vCPU sleeps until another PE executes
> > >> >> SEV, which such lock code never does.
> > >> >>
> > >> >> Set the Event Register when a store-exclusive fails, in both the
> > >> >> AArch64 and the AArch32 translator. A failure for another reason causes
> > >> >> a spurious wakeup, which the architecture permits.
> > >> >>
> > >> >> Fixes: 01b223f895a7 ("target/arm: enable WFE sleeping for A-profile")
> > >> >> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4938
> > >> >> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> > >> >> Signed-off-by: Frank Mehnert <frank.mehnert@kernkonzept.com>
> > >> >> ---
> > >> >>  target/arm/tcg/translate-a64.c | 7 ++++++-
> > >> >>  target/arm/tcg/translate.c     | 7 ++++++-
> > >> >>  2 files changed, 12 insertions(+), 2 deletions(-)
> > >> >>
> > >> >> diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c
> > >> >> index 49472f7d97..45f3b3df03 100644
> > >> >> --- a/target/arm/tcg/translate-a64.c
> > >> >> +++ b/target/arm/tcg/translate-a64.c
> > >> >> @@ -3531,10 +3531,15 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
> > >> >>          tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
> > >> >>      }
> > >> >>      tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
> > >> >> -    tcg_gen_br(done_label);
> > >> >> +    tcg_gen_brcondi_i64(TCG_COND_EQ, tmp, 0, done_label);
> > >> >>
> > >> >>      gen_set_label(fail_label);
> > >> >>      tcg_gen_movi_i64(cpu_reg(s, rd), 1);
> > >> >> +    /*
> > >> >> +     * A failing store-exclusive means the global monitor was cleared, which
> > >> >> +     * generates a WFE wakeup event.
> > >> >> +     */
> > >> >
> > >> > We might add "Architecturally the wakeup event should have happened when
> > >> > the store by the other CPU happened, but we do it here."
> > >> >
> > >> >> +    gen_event_reg();
> > >> >>      gen_set_label(done_label);
> > >> >>      tcg_gen_movi_i64(cpu_exclusive_addr, -1);
> > >> >
> > >> > The architecture permits the global monitor to be cleared when a
> > >> > StoreExcl() succeeds as well as when it fails. Given that gen_event_reg()
> > >> > is just a byte-store, we could also unconditionally do it after the
> > >> > done_label, to avoid the branching around. Not sure which is
> > >> > preferable.
> > >>
> > >> Isn't the global monitor subtly different from the per-vcpu event
> > >> register in this case as we are modelling the effect of it being cleared
> > >> without properly modelling the actual global monitor itself? Or does our
> > >> messing with cpu_exclusive_addr count?
> > >
> > > We're quite a long way out of spec on our exclusives handling, so
> > > anything we're doing here is in the realm of "try to make the common
> > > cases continue to work". The interaction here is that the event register
> > > spec defines that the CPU's event register is cleared when the global
> > > monitor for that CPU goes from "exclusive" to "open". Since we don't
> > > really model the global monitor we're trying to patch things up by
> > > doing it after the fact for at least some cases where we notice that
> > > that transition would have happened. There will still be cases
> > > where we should have set the event register and don't, we just hope
> > > they don't matter in practice...
> > >
> > >> However making the store occur always still passes the test case so I
> > >> don't have any objection if this is easier.
> > >
> > > Given that you're allowed to set the event register for any
> > > IMPDEF reason, setting it more often is always safe. The tradeoff
> > > I think is between "every store exclusive takes an extra conditional
> > > branch" and "WFE spuriously wakes up more often". As I say, I don't
> > > know which side of that we should prefer.
> >
> > WFE doesn't wake up here, the event register is local to the PE which is
> > currently executing a StoreExcl - it just skips the first attempt to
> > sleep after the StoreExcl.
> 
> Yes, which is WFE waking up spuriously (it could/should have gone to
> sleep but immediately returned instead).
> 
> > Only SEV broadcasts causes the other PE's to wake up.
> 
> Other PEs doing stores can also cause other PEs to wake up:
> 
>  * the state for CPU X in a global monitor goes from "Exclusive
>    Access" to "Open Access" if CPU Y stores to the marked address
>    (i.e. to an address that CPU X has done a LoadExcl on)
>    [fig B2-5 in B2.12.2.1.1 in rev M.d of the Arm ARM, specifically
>    the "Store(Marked_address,!n)" transition]
>  * clearing the global monitor for CPU X sets the event
>    register for CPU X
>    [R_XRZRK, section D1.7.1]
>  * this event is a WFE wakeup event for CPU X
>    [R_KBMFC, section D1.7.1.4]
> 
> So architecturally a sequence like "ldrex from addr; wfe" is supposed to
> be wakeable by another CPU doing a plain store to addr.
> 
> Actually that makes me wonder; in the quoted lock sequence in the
> commit message / Arm ARM K11.3.4.1:
> 
>      sevl   // make sure first iteration wfe doesn't sleep
>   1: wfe
>      ldaxr  w1, [x0]
>      cbnz   w1, 1b
>      stxr   w2, w3, [x0]
>      cbnz   w2, 1b
> 
> and its corresponding lock-release:
>      stlr wzr, [x1]
> 
> aren't we missing this case?
> 
> This patch sets the event register when the stxr fails, which
> means we won't deadlock when we go round the loop from the
> second cbnz. But there's also a branch back up to the WFE for
> the "ldaxr load was not 0" case (i.e. where some other CPU already
> holds the lock). Architecturally the thing that wakes us up from
> that WFE is the fact that the ldaxr means our global monitor
> state is "Exclusive Access" and the other CPU doing the STLR
> into the lock address will push us out to Open Access, setting
> the event register and waking up the WFE.

That case is already covered by HELPER(wfe): after the ldaxr,
exclusive_addr is still set when the cbnz branches back, and WFE
returns immediately while the CPU is in an exclusive region (the check
added in 01b223f895a7). So the waiter spins instead of sleeping, but
it does not deadlock. Only the path from the failed stxr reaches WFE
with exclusive_addr == -1, which is what this patch addresses.

Regarding setting the event register unconditionally after done_label:
I agree. The event register is local to the PE, so after a successful
store-exclusive only the next WFE of this PE returns once without
sleeping, and lock loops typically start with SEVL anyway.

I can send a v3, unless you prefer to keep the conditional version.

Thanks,

Frank




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH for-11.2 v2 1/2] target/arm: set the event register on a failed store-exclusive
  2026-11-30 16:35             ` Frank Mehnert
@ 2026-11-30 16:53               ` Peter Maydell
  0 siblings, 0 replies; 11+ messages in thread
From: Peter Maydell @ 2026-11-30 16:53 UTC (permalink / raw)
  To: Frank Mehnert; +Cc: Alex Bennée, qemu-devel, qemu-arm, Richard Henderson

On Mon, 30 Nov 2026 at 16:36, Frank Mehnert <frank.mehnert@gmail.com> wrote:
>
> On Montag, 30. November 2026 17:23 Peter Maydell wrote:
> > Actually that makes me wonder; in the quoted lock sequence in the
> > commit message / Arm ARM K11.3.4.1:
> >
> >      sevl   // make sure first iteration wfe doesn't sleep
> >   1: wfe
> >      ldaxr  w1, [x0]
> >      cbnz   w1, 1b
> >      stxr   w2, w3, [x0]
> >      cbnz   w2, 1b
> >
> > and its corresponding lock-release:
> >      stlr wzr, [x1]
> >
> > aren't we missing this case?
> >
> > This patch sets the event register when the stxr fails, which
> > means we won't deadlock when we go round the loop from the
> > second cbnz. But there's also a branch back up to the WFE for
> > the "ldaxr load was not 0" case (i.e. where some other CPU already
> > holds the lock). Architecturally the thing that wakes us up from
> > that WFE is the fact that the ldaxr means our global monitor
> > state is "Exclusive Access" and the other CPU doing the STLR
> > into the lock address will push us out to Open Access, setting
> > the event register and waking up the WFE.
>
> That case is already covered by HELPER(wfe): after the ldaxr,
> exclusive_addr is still set when the cbnz branches back, and WFE
> returns immediately while the CPU is in an exclusive region (the check
> added in 01b223f895a7). So the waiter spins instead of sleeping, but
> it does not deadlock. Only the path from the failed stxr reaches WFE
> with exclusive_addr == -1, which is what this patch addresses.

Thanks; yes, I'd just found that bit of code after sending the mail.

> Regarding setting the event register unconditionally after done_label:
> I agree. The event register is local to the PE, so after a successful
> store-exclusive only the next WFE of this PE returns once without
> sleeping, and lock loops typically start with SEVL anyway.

I think we'll go with the version you have here. It's only
turning an unconditional branch into a conditional one,
rather than adding a branch where there was none before.

I've queued these patches to target-arm.next, with a slightly
beefed-up comment:

+    /*
+     * A failing store-exclusive means the global monitor for this CPU
+     * was cleared, which is a transition that sets the event register.
+     * Architecturally the transition and the wakeup event should have
+     * happened when the store by the other CPU occurred; because we
+     * don't model the local or global monitors we do it here, which is
+     * late but good enough for the usual "lock loop with a WFE" idiom.
+     */

thanks
-- PMM


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-30 16:53 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 14:32 [PATCH for-11.2 v2 0/2] target/arm: WFE wakeup after a failed store-exclusive Frank Mehnert
2026-11-26 14:32 ` [PATCH for-11.2 v2 1/2] target/arm: set the event register on " Frank Mehnert
2026-11-30 10:10   ` Peter Maydell
2026-11-30 13:56     ` Alex Bennée
2026-11-30 15:00       ` Peter Maydell
2026-11-30 15:53         ` Alex Bennée
2026-11-30 16:23           ` Peter Maydell
2026-11-30 16:35             ` Frank Mehnert
2026-11-30 16:53               ` Peter Maydell
2026-11-26 14:32 ` [PATCH for-11.2 v2 2/2] tests/tcg/aarch64: test WFE after " Frank Mehnert
2026-11-26 15:09   ` Alex Bennée

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.