accel/tcg: Do not write qemu_icount from seqlock readers
cover 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
PATCH 1/3 accel/tcg: Factor icount_running_cpu() out
Extract the running vCPU check from icount_get_raw_locked(), to reuse it in a following commit.
No functional change.
--- accel/tcg/icount-common.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)
accel/tcg/icount-common.c +17 -6
@@ -104,15 +104,26 @@ void icount_update(CPUState *cpu) &timers_state.vm_clock_lock); } -static int64_t icount_get_raw_locked (void ) +/* Return the running vCPU of this thread, if any, checking it may do I/O */ +static CPUState *icount_running_cpu (void ) {CPUState *cpu = current_cpu; -if (cpu && cpu->running) { -if (!cpu->neg.can_do_io) { -error_report ("Bad icount read" ); -exit (1 ); - } +if (!cpu || !cpu->running) { +return NULL ; + } +if (!cpu->neg.can_do_io) { +error_report ("Bad icount read" ); +exit (1 ); + } +return cpu; +} + +static int64_t icount_get_raw_locked (void ) +{ +CPUState *cpu =icount_running_cpu (); + +if (cpu) {/* Take into account what has run */ icount_update_locked (cpu); }
Signature · 1 line
2.55.0
Re: [PATCH 1/3] accel/tcg: Factor icount_running_cpu() out
Quoted 10 lines · Philippe Mathieu-Daudé
Extract the running vCPU check from
icount_get_raw_locked(), to reuse it in a following commit.No functional change.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 45 lines
---
accel/tcg/icount-common.c| 23 +++++++++++++++++------1 file changed, 17 insertions(+), 6 deletions(-)accel/tcg/icount-common.c +17
-6@@ -104,15 +104,26 @@ void icount_update(CPUState *cpu) &timers_state.vm_clock_lock); } -static int64_t icount_get_raw_locked (void ) +/* Return the running vCPU of this thread, if any, checking it may do I/O */ +static CPUState *icount_running_cpu (void ) {CPUState *cpu = current_cpu; -if (cpu && cpu->running) { -if (!cpu->neg.can_do_io) { -error_report ("Bad icount read" ); -exit (1 ); - } +if (!cpu || !cpu->running) { +return NULL ; + } +if (!cpu->neg.can_do_io) { +error_report ("Bad icount read" ); +exit (1 ); + } +return cpu; +} + +static int64_t icount_get_raw_locked (void ) +{ +CPUState *cpu =icount_running_cpu (); + +if (cpu) {/* Take into account what has run */ icount_update_locked (cpu); }Signature · 1 line
2.55.0
Re: [PATCH 1/3] accel/tcg: Factor icount_running_cpu() out
In reply to Philippe Mathieu-DaudéQuoted 10 lines · Philippe Mathieu-Daudé
Extract the running vCPU check from
icount_get_raw_locked(), to reuse it in a following commit.No functional change.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- accel/tcg/icount-common.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-)
PATCH 2/3 accel/tcg: Factor icount_to_virtual_ns() out
In reply to Philippe Mathieu-DaudéExtract the QEMU_CLOCK_VIRTUAL computation from icount_get_locked(), to reuse it in a following commit.
No functional change.
--- accel/tcg/icount-common.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
accel/tcg/icount-common.c +7 -2
@@ -119,6 +119,12 @@ static CPUState *icount_running_cpu(void)return cpu; } +/* Convert @icount executed instructions to QEMU_CLOCK_VIRTUAL ns */ +static int64_t icount_to_virtual_ns (int64_t icount) +{ +return qatomic_read (&timers_state.qemu_icount_bias) +icount_to_ns (icount); +} +static int64_t icount_get_raw_locked (void ) {CPUState *cpu =icount_running_cpu ();
@@ -133,8 +139,7 @@ static int64_t icount_get_raw_locked(void)static int64_t icount_get_locked (void ) { -int64_t icount =icount_get_raw_locked (); -return qatomic_read (&timers_state.qemu_icount_bias) +icount_to_ns (icount); +return icount_to_virtual_ns (icount_get_raw_locked ()); }int64_t icount_get_raw (void )
Signature · 1 line
2.55.0
Re: [PATCH 2/3] accel/tcg: Factor icount_to_virtual_ns() out
Quoted 10 lines · Philippe Mathieu-Daudé
Extract the
QEMU_CLOCK_VIRTUALcomputation fromicount_get_locked(), to reuse it in a following commit.No functional change.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 35 lines
---
accel/tcg/icount-common.c| 9 +++++++--1 file changed, 7 insertions(+), 2 deletions(-)accel/tcg/icount-common.c +7
-2@@ -119,6 +119,12 @@ static CPUState *icount_running_cpu(void)return cpu; } +/* Convert @icount executed instructions to QEMU_CLOCK_VIRTUAL ns */ +static int64_t icount_to_virtual_ns (int64_t icount) +{ +return qatomic_read (&timers_state.qemu_icount_bias) +icount_to_ns (icount); +} +static int64_t icount_get_raw_locked (void ) {CPUState *cpu =icount_running_cpu ();@@ -133,8 +139,7 @@ static int64_t icount_get_raw_locked(void)static int64_t icount_get_locked (void ) { -int64_t icount =icount_get_raw_locked (); -return qatomic_read (&timers_state.qemu_icount_bias) +icount_to_ns (icount); +return icount_to_virtual_ns (icount_get_raw_locked ()); }int64_t icount_get_raw (void )Signature · 1 line
2.55.0
Re: [PATCH 2/3] accel/tcg: Factor icount_to_virtual_ns() out
In reply to Philippe Mathieu-DaudéQuoted 10 lines · Philippe Mathieu-Daudé
Extract the
QEMU_CLOCK_VIRTUALcomputation fromicount_get_locked(), to reuse it in a following commit.No functional change.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- accel/tcg/icount-common.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
PATCH 3/3 accel/tcg: Do not write qemu_icount from seqlock readers
In reply to Philippe Mathieu-Daudéicount_get() and icount_get_raw() updated qemu_icount inside the seqlock read section, without vm_clock_lock.
This fixes a narrow time-skew race in adaptive icount:icount_adjust() reads qemu_icount twice under the write lock, and a vCPU update in between is lost from QEMU_CLOCK_VIRTUAL (up to 0xffff instructions). Reproduced on riscv64 'virt', whose cycle/instret CSRs read the clock without the BQL, by widening the window with an injected delay.
Update under the write lock before reading.
--- accel/tcg/icount-common.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
accel/tcg/icount-common.c +16 -2
@@ -125,6 +125,7 @@ static int64_t icount_to_virtual_ns(int64_t icount)return qatomic_read (&timers_state.qemu_icount_bias) +icount_to_ns (icount); } +/* Callers hold vm_clock_lock (used by REPLAY_CLOCK_LOCKED) */ static int64_t icount_get_raw_locked (void ) {CPUState *cpu =icount_running_cpu ();
@@ -137,19 +138,28 @@ static int64_t icount_get_raw_locked(void)return qatomic_read (&timers_state.qemu_icount); } +/* Callers hold vm_clock_lock */ static int64_t icount_get_locked (void ) {return icount_to_virtual_ns (icount_get_raw_locked ()); } +/* +* Readers never write qemu_icount inside the seqlock read section: +* the running vCPU folds in what it has executed under the write lock. +*/ int64_t icount_get_raw (void ) { +CPUState *cpu =icount_running_cpu ();int64_t icount;unsigned start; +if (cpu) { +icount_update (cpu); + }do { start =seqlock_read_begin (&timers_state.vm_clock_seqlock); - icount =icount_get_raw_locked (); + icount =qatomic_read (&timers_state.qemu_icount); }while (seqlock_read_retry (&timers_state.vm_clock_seqlock, start));return icount;
@@ -158,12 +168,16 @@ int64_t icount_get_raw(void)/* Return the virtual CPU time, based on the instruction counter. */ int64_t icount_get (void ) { +CPUState *cpu =icount_running_cpu ();int64_t icount;unsigned start; +if (cpu) { +icount_update (cpu); + }do { start =seqlock_read_begin (&timers_state.vm_clock_seqlock); - icount =icount_get_locked (); + icount =icount_to_virtual_ns (qatomic_read (&timers_state.qemu_icount)); }while (seqlock_read_retry (&timers_state.vm_clock_seqlock, start));return icount;
Signature · 1 line
2.55.0
Quoted 67 lines · Philippe Mathieu-Daudé
icount_get()andicount_get_raw()updatedqemu_icountinside the seqlock read section, withoutvm_clock_lock.This fixes a narrow time-skew race in adaptive icount:
icount_adjust()readsqemu_icounttwice under the write lock, and a vCPU update in between is lost fromQEMU_CLOCK_VIRTUAL(up to0xffffinstructions). Reproduced on riscv64 'virt', whose cycle/instret CSRs read the clock without the BQL, by widening the window with an injected delay.Update under the write lock before reading.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- accel/tcg/icount-common.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)accel/tcg/icount-common.c +12
-1@@ -125,6 +125,7 @@ static int64_t icount_to_virtual_ns(int64_t icount)return qatomic_read (&timers_state.qemu_icount_bias) +icount_to_ns (icount); } +/* Callers hold vm_clock_lock (used by REPLAY_CLOCK_LOCKED) */ static int64_t icount_get_raw_locked (void ) {CPUState *cpu =icount_running_cpu ();@@ -137,19 +138,28 @@ static int64_t icount_get_raw_locked(void)return qatomic_read (&timers_state.qemu_icount); } +/* Callers hold vm_clock_lock */ static int64_t icount_get_locked (void ) {return icount_to_virtual_ns (icount_get_raw_locked ()); } +/* +* Readers never write qemu_icount inside the seqlock read section: +* the running vCPU folds in what it has executed under the write lock. +*/ int64_t icount_get_raw (void ) { +CPUState *cpu =icount_running_cpu ();int64_t icount;unsigned start; +if (cpu) { +icount_update (cpu); + }do { start =seqlock_read_begin (&timers_state.vm_clock_seqlock); - icount =icount_get_raw_locked (); + icount =qatomic_read (&timers_state.qemu_icount); }while (seqlock_read_retry (&timers_state.vm_clock_seqlock, start));return icount;@@ -158,12 +168,16 @@ int64_t icount_get_raw(void)/* Return the virtual CPU time, based on the instruction counter. */ int64_t icount_get (void ) { +CPUState *cpu =icount_running_cpu ();int64_t icount;unsigned start;
accel/tcg/icount-common.c int64_t icount_get_raw(void) · line 183
Quoted 10 lines
accel/tcg/icount-common.c +4
-1+if (cpu) { +icount_update (cpu); + }do { start =seqlock_read_begin (&timers_state.vm_clock_seqlock); - icount =icount_get_locked (); + icount =icount_to_virtual_ns (qatomic_read (&timers_state.qemu_icount)); }while (seqlock_read_retry (&timers_state.vm_clock_seqlock, start));return icount;
The patch seems to fix two things at the same time:
- ensure that current icount is read with vm_clock_lock held, that's fine.
- add a new call to icount_update.
Can you please explain why this new call fix things?
Regards,
Pierrick