Skip to content

accel/tcg: Do not write qemu_icount from seqlock readers

Philippe Mathieu-Daudé qemu-devel qemu-riscv 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest3 patches 5 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3accel/tcg: Factor icount_running_cpu() outaccel/tcg/icount-common.c+17 -62 replies
2/3accel/tcg: Factor icount_to_virtual_ns() outaccel/tcg/icount-common.c+7 -22 replies
3/3accel/tcg: Do not write qemu_icount from seqlock readersaccel/tcg/icount-common.c+16 -21 reply

Reviews trailers per patch

2 of 3 patches have a Reviewed-by or Acked-by.

PatchMohamed MediouniPierrick Bouvier
1/3R-b R-b
2/3R-b R-b
3/3

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [PATCH 0/3] accel/tcg: Do not write qemu_icount from seqlock readers

icount_get() and icount_get_raw() fold the running vCPU's executed instructions into qemu_icount inside the seqlock read section, without holding vm_clock_lock.

In adaptive icount (-icount shift=auto), icount_adjust() reads qemu_icount twice under the write lock; a vCPU update landing in between is lost from QEMU_CLOCK_VIRTUAL (up to 0xffff instructions). Targets reading the clock from a helper without the BQL can hit it, e.g. the RISC-V cycle/instret CSRs.
(It is also a prerequisite for reading the Arm generic timer counters without the BQL, which will follow as distinct series).

First refactor the helpers (patches 1 and 2) then performs the update under the write lock before reading, so readers never write (patch 3).

Reproduced on riscv64 'virt' by widening the window with an injected
delay: 7 hits in ~350 adjust windows before, none in 234 after.

Philippe Mathieu-Daudé (3):
accel/tcg: Factor icount_running_cpu() out accel/tcg: Factor icount_to_virtual_ns() out accel/tcg: Do not write qemu_icount from seqlock readers

 accel/tcg/icount-common.c | 50 +++++++++++++++++++++++++++++++--------
 1 file changed, 40 insertions(+), 10 deletions(-)
Signature · 1 line
2.55.0