target/arm: WFE sleeps forever after a failed store-exclusive

Host environment

  • Operating system: Debian (x86_64 host)

  • QEMU flavor: qemu-system-aarch64, TCG

  • QEMU versions: 11.1.0 and later; reproduced with Debian 11.1.3, with v11.2.0-rc1, and with master at 6bf90d4f (v11.2.0-rc1-67)

  • QEMU command line:

    qemu-system-aarch64 -M virt -cpu cortex-a57 -smp 2 -nographic \
      -kernel wfe-repro.elf

Emulated/Virtualized environment

  • Machine: virt, Cortex-A57, 2 CPUs, TCG
  • Guest: bare-metal reproducer (attached), originally found with the L4Re/Fiasco microkernel

Description of problem

A vCPU spinning on a lock in the common Armv8 pattern "WFE, load-exclusive, store-exclusive, on failure back to WFE" with interrupts masked can go to sleep in WFE and never wake up, although the lock is free.

The lock is released with a plain store-release and without SEV. The architecture allows this, because clearing a PE's global monitor generates a WFE wakeup event for that PE:

  • Arm ARM DDI 0487K.a, D1.6.1, R_XRZRK: "The Event Register for a PE is set by any of the following: [...] The clearing of the global monitor for the PE."
  • Arm ARM DDI 0487K.a, K14.3: "the Armv8 and later specifications require that the clearing of a global monitor will generate an event for the PE associated with the global monitor, which can simplify the use of WFE, by removing the need for a DSB barrier and SEV instruction."

Commit 01b223f8 ("target/arm: enable WFE sleeping for A-profile") lets WFE halt the vCPU. As QEMU does not model the global monitor, HELPER(wfe) does not sleep while env->exclusive_addr != -1. This covers waiting in WFE after a load-exclusive, but not the path after a failed store-exclusive:

1:  wfe
    ldaxr   w1, [x0]          // exclusive_addr = x0
    cbnz    w1, 1b            // lock held: WFE returns at once, fine
    mov     w1, #1
    stxr    w2, w1, [x0]      // clears exclusive_addr, also on failure
    cbnz    w2, 1b            // failed: WFE now sleeps

The store-exclusive fails because another vCPU stored to the lock in between. On hardware, that store cleared this PE's global monitor and set its Event Register, so the following WFE completes immediately. In QEMU, no event is generated. With interrupts masked and no generic timer event stream, the vCPU sleeps until some vCPU executes SEV, which this lock code never does.

The bug was found with the Fiasco microkernel, whose arm64 spin lock uses this pattern. A vCPU spinning on a lock in an interrupt handler stayed halted forever. gdb showed the vCPU halted in WFE inside the lock loop with the lock value 0 (free), while the other vCPU was idle.

Steps to reproduce

  1. Build the attached reproducer wfe-repro.S:

    aarch64-linux-gnu-gcc -nostdlib -static -Wl,-Ttext=0x40080000 \
      -o wfe-repro.elf wfe-repro.S
  2. Run it:

    qemu-system-aarch64 -M virt -cpu cortex-a57 -smp 2 -nographic \
      -kernel wfe-repro.elf

Both CPUs take and release a shared lock 0x200000 times with interrupts masked. CPU 0 prints a dot every 0x10000 acquisitions, then "PASS", and powers off through PSCI SYSTEM_OFF.

Expected: "PASS" and QEMU exits within about a second.

Actual: QEMU keeps running without printing "PASS". Either CPU 0 hangs right away, or CPU 0 prints all its dots and then waits for CPU 1, which is stuck in WFE. This happened in 5 of 5 runs with v11.2.0-rc1-67.

Additional information

A fix is to set the Event Register whenever a store-exclusive fails, in gen_store_exclusive() of both translate-a64.c and translate.c. A failure for another reason, for example after CLREX, then causes a spurious wakeup, which the architecture permits ("Software using the Wait for Event mechanism must tolerate spurious wakeup events"). With this change, the reproducer passed in 5 of 5 runs, and the original Fiasco test passed in 6 of 6 runs.

Note that QEMU's generic code reports a failing store-exclusive in two ways: through fail_label when the address does not match, and through the result of the cmpxchg when the value changed. The hang happens in the second case, so both have to set the event.

A related deviation, not needed for this hang: R_XRZRK also lists "An exception return" as setting the Event Register, which HELPER(exception_return) and HELPER(cpsr_write_eret) do not do either.

Proposed patch:

--- a/target/arm/tcg/translate-a64.c
+++ b/target/arm/tcg/translate-a64.c
@@ -3531,10 +3531,15 @@ static void gen_store_exclusive(...)
         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);
 }
--- a/target/arm/tcg/translate.c
+++ b/target/arm/tcg/translate.c
@@ -2137,10 +2137,15 @@ static void gen_store_exclusive(...)
         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);
 }

wfe-repro.S