Skip to contentSkip to conversation

accel/tcg: Do not write qemu_icount from seqlock readers

PATCH In review Philippe Mathieu-Daudé 3 participants 9 messages Nov 26 – Nov 27

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index b1b6c005fe71..a11f2397489b 100644
@@ -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
Reviewed-by on v1 1/3
#2 lore raw

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.

Quoted 45 lines

---
accel/tcg/icount-common.c | 23 +++++++++++++++++------

1 file changed, 17 insertions(+), 6 deletions(-)
accel/tcg/icount-common.c +17 -6
index b1b6c005fe71..a11f2397489b 100644
@@ -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
Reviewed-by on v1 1/3
#3 lore raw

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.

---
 accel/tcg/icount-common.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
#4 lore raw

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
index a11f2397489b..b52d22f3a1cf 100644
@@ -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
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] accel/tcg: Factor icount_to_virtual_ns() out

Quoted 10 lines · Philippe Mathieu-Daudé

Extract the QEMU_CLOCK_VIRTUAL computation from icount_get_locked(), to reuse it in a following commit.

No functional change.

Quoted 35 lines

---
accel/tcg/icount-common.c | 9 +++++++--

1 file changed, 7 insertions(+), 2 deletions(-)
accel/tcg/icount-common.c +7 -2
index a11f2397489b..b52d22f3a1cf 100644
@@ -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
Reviewed-by on v1 2/3
#6 lore raw

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_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(-)
#7 lore raw

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
index b52d22f3a1cf..ababf7745b27 100644
@@ -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
#8 lore raw
Quoted 67 lines · 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 +12 -1
index b52d22f3a1cf..ababf7745b27 100644
@@ -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