[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 07/17] timer: protect timers_state's clock with seqlo
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [PULL 07/17] timer: protect timers_state's clock with seqlock |
Date: |
Thu, 17 Oct 2013 17:48:45 +0200 |
From: Liu Ping Fan <address@hidden>
QEMU_CLOCK_VIRTUAL may be read outside BQL. This will make its
foundation, i.e. cpu_clock_offset exposed to race condition.
Using private lock to protect it.
After this patch, reading QEMU_CLOCK_VIRTUAL is thread safe
unless use_icount is true, in which case the existing callers
still rely on the BQL.
Lock rule: private lock innermost, ie BQL->"this lock"
Signed-off-by: Liu Ping Fan <address@hidden>
Signed-off-by: Paolo Bonzini <address@hidden>
---
cpus.c | 49 ++++++++++++++++++++++++++++++++++++++++++-------
include/qemu/timer.h | 2 ++
2 files changed, 44 insertions(+), 7 deletions(-)
diff --git a/cpus.c b/cpus.c
index e566297..f075335 100644
--- a/cpus.c
+++ b/cpus.c
@@ -37,6 +37,7 @@
#include "sysemu/qtest.h"
#include "qemu/main-loop.h"
#include "qemu/bitmap.h"
+#include "qemu/seqlock.h"
#ifndef _WIN32
#include "qemu/compatfd.h"
@@ -110,8 +111,14 @@ static int64_t vm_clock_warp_start;
static int64_t qemu_icount;
typedef struct TimersState {
+ /* Protected by BQL. */
int64_t cpu_ticks_prev;
int64_t cpu_ticks_offset;
+
+ /* cpu_clock_offset can be read out of BQL, so protect it with
+ * this lock.
+ */
+ QemuSeqLock vm_clock_seqlock;
int64_t cpu_clock_offset;
int32_t cpu_ticks_enabled;
int64_t dummy;
@@ -137,6 +144,7 @@ int64_t cpu_get_icount(void)
}
/* return the host CPU cycle counter and handle stop/restart */
+/* Caller must hold the BQL */
int64_t cpu_get_ticks(void)
{
if (use_icount) {
@@ -157,37 +165,63 @@ int64_t cpu_get_ticks(void)
}
}
-/* return the host CPU monotonic timer and handle stop/restart */
-int64_t cpu_get_clock(void)
+static int64_t cpu_get_clock_locked(void)
{
int64_t ti;
+
if (!timers_state.cpu_ticks_enabled) {
- return timers_state.cpu_clock_offset;
+ ti = timers_state.cpu_clock_offset;
} else {
ti = get_clock();
- return ti + timers_state.cpu_clock_offset;
+ ti += timers_state.cpu_clock_offset;
}
+
+ return ti;
}
-/* enable cpu_get_ticks() */
+/* return the host CPU monotonic timer and handle stop/restart */
+int64_t cpu_get_clock(void)
+{
+ int64_t ti;
+ unsigned start;
+
+ do {
+ start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
+ ti = cpu_get_clock_locked();
+ } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
+
+ return ti;
+}
+
+/* enable cpu_get_ticks()
+ * Caller must hold BQL which server as mutex for vm_clock_seqlock.
+ */
void cpu_enable_ticks(void)
{
+ /* Here, the really thing protected by seqlock is cpu_clock_offset. */
+ seqlock_write_lock(&timers_state.vm_clock_seqlock);
if (!timers_state.cpu_ticks_enabled) {
timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
timers_state.cpu_clock_offset -= get_clock();
timers_state.cpu_ticks_enabled = 1;
}
+ seqlock_write_unlock(&timers_state.vm_clock_seqlock);
}
/* disable cpu_get_ticks() : the clock is stopped. You must not call
- cpu_get_ticks() after that. */
+ * cpu_get_ticks() after that.
+ * Caller must hold BQL which server as mutex for vm_clock_seqlock.
+ */
void cpu_disable_ticks(void)
{
+ /* Here, the really thing protected by seqlock is cpu_clock_offset. */
+ seqlock_write_lock(&timers_state.vm_clock_seqlock);
if (timers_state.cpu_ticks_enabled) {
timers_state.cpu_ticks_offset = cpu_get_ticks();
- timers_state.cpu_clock_offset = cpu_get_clock();
+ timers_state.cpu_clock_offset = cpu_get_clock_locked();
timers_state.cpu_ticks_enabled = 0;
}
+ seqlock_write_unlock(&timers_state.vm_clock_seqlock);
}
/* Correlation between real and virtual time is always going to be
@@ -371,6 +405,7 @@ static const VMStateDescription vmstate_timers = {
void configure_icount(const char *option)
{
+ seqlock_init(&timers_state.vm_clock_seqlock, NULL);
vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
if (!option) {
return;
diff --git a/include/qemu/timer.h b/include/qemu/timer.h
index b58903b..016e29a 100644
--- a/include/qemu/timer.h
+++ b/include/qemu/timer.h
@@ -653,7 +653,9 @@ static inline int64_t qemu_soonest_timeout(int64_t
timeout1, int64_t timeout2)
void init_clocks(void);
int64_t cpu_get_ticks(void);
+/* Caller must hold BQL */
void cpu_enable_ticks(void);
+/* Caller must hold BQL */
void cpu_disable_ticks(void);
static inline int64_t get_ticks_per_sec(void)
--
1.8.3.1
- [Qemu-devel] [PULL 00/17] Memory/threading changes for 1.7, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 01/17] memory: fix 128 arithmetic in info mtree, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 02/17] compatfd: switch to QemuThread, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 04/17] cirrus: Mark vga io region as coalesced MMIO flushing, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 05/17] vga: Mark relevant portio lists regions as coalesced MMIO flushing, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 03/17] portio: Allow to mark portio lists as coalesced MMIO flushing, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 10/17] timer: extract timer_mod_ns_locked and timerlist_rearm, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 11/17] timer: add timer_mod_anticipate and timer_mod_anticipate_ns, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 07/17] timer: protect timers_state's clock with seqlock,
Paolo Bonzini <=
- [Qemu-devel] [PULL 06/17] seqlock: introduce read-write seqlock, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 08/17] qemu-thread: add QemuEvent, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 12/17] icount: use cpu_get_icount() directly, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 09/17] timer: make qemu_clock_enable sync between disable and timer's cb, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 15/17] icount: document (future) locking rules for icount, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 13/17] icount: reorganize icount_warp_rt, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 14/17] icount: prepare the code for future races in calling qemu_clock_warp, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 16/17] icount: make it thread-safe, Paolo Bonzini, 2013/10/17
- [Qemu-devel] [PULL 17/17] exec: remove qemu_safe_ram_ptr, Paolo Bonzini, 2013/10/17