From: Liu Ping Fan <qemulist@gmail.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Alex Bligh <alex@alex.org.uk>,
Stefan Hajnoczi <stefanha@redhat.com>,
Jan Kiszka <jan.kiszka@siemens.com>
Subject: [Qemu-devel] [PATCH v5 2/4] timer: protect timers_state's clock with seqlock
Date: Wed, 25 Sep 2013 14:20:58 +0800 [thread overview]
Message-ID: <1380090060-6764-3-git-send-email-pingfank@linux.vnet.ibm.com> (raw)
In-Reply-To: <1380090060-6764-1-git-send-email-pingfank@linux.vnet.ibm.com>
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 <pingfank@linux.vnet.ibm.com>
---
cpus.c | 41 +++++++++++++++++++++++++++++++++--------
include/qemu/timer.h | 2 ++
2 files changed, 35 insertions(+), 8 deletions(-)
diff --git a/cpus.c b/cpus.c
index e566297..5baa76d 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"
@@ -112,6 +113,11 @@ static int64_t qemu_icount;
typedef struct TimersState {
int64_t cpu_ticks_prev;
int64_t cpu_ticks_offset;
+ /* cpu_clock_offset will be read out of BQL, so protect it with private
+ * lock. As for cpu_ticks_*, no requirement to read it outside BQL yet.
+ * Lock rule: innermost
+ */
+ QemuSeqLock cpu_clock_offset_seqlock;
int64_t cpu_clock_offset;
int32_t cpu_ticks_enabled;
int64_t dummy;
@@ -137,6 +143,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) {
@@ -161,33 +168,50 @@ int64_t cpu_get_ticks(void)
int64_t cpu_get_clock(void)
{
int64_t ti;
- if (!timers_state.cpu_ticks_enabled) {
- return timers_state.cpu_clock_offset;
- } else {
- ti = get_clock();
- return ti + timers_state.cpu_clock_offset;
- }
+ unsigned start;
+
+ do {
+ start = seqlock_read_begin(&timers_state.cpu_clock_offset_seqlock);
+ if (!timers_state.cpu_ticks_enabled) {
+ ti = timers_state.cpu_clock_offset;
+ } else {
+ ti = get_clock();
+ ti += timers_state.cpu_clock_offset;
+ }
+ } while (seqlock_read_retry(&timers_state.cpu_clock_offset_seqlock, start));
+
+ return ti;
}
-/* enable cpu_get_ticks() */
+/* enable cpu_get_ticks()
+ * Caller must hold BQL which server as mutex for cpu_clock_offset_seqlock.
+ */
void cpu_enable_ticks(void)
{
+ /* Here, the really thing protected by seqlock is cpu_clock_offset. */
+ seqlock_write_lock(&timers_state.cpu_clock_offset_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.cpu_clock_offset_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 cpu_clock_offset_seqlock.
+ */
void cpu_disable_ticks(void)
{
+ /* Here, the really thing protected by seqlock is cpu_clock_offset. */
+ seqlock_write_lock(&timers_state.cpu_clock_offset_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_ticks_enabled = 0;
}
+ seqlock_write_unlock(&timers_state.cpu_clock_offset_seqlock);
}
/* Correlation between real and virtual time is always going to be
@@ -371,6 +395,7 @@ static const VMStateDescription vmstate_timers = {
void configure_icount(const char *option)
{
+ seqlock_init(&timers_state.cpu_clock_offset_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 e4934dd..bb1de23 100644
--- a/include/qemu/timer.h
+++ b/include/qemu/timer.h
@@ -636,7 +636,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.1.4
next prev parent reply other threads:[~2013-09-25 6:21 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-25 6:20 [Qemu-devel] [PATCH v5 0/4] timers thread-safe stuff Liu Ping Fan
2013-09-25 6:20 ` [Qemu-devel] [PATCH v5 1/4] seqlock: introduce read-write seqlock Liu Ping Fan
2013-09-25 7:30 ` Paolo Bonzini
2013-09-25 9:28 ` liu ping fan
2013-09-25 6:20 ` Liu Ping Fan [this message]
2013-09-25 6:20 ` [Qemu-devel] [PATCH v5 3/4] qemu-thread: add QemuEvent Liu Ping Fan
2013-09-25 9:29 ` liu ping fan
2013-09-25 6:21 ` [Qemu-devel] [PATCH v5 4/4] timer: make qemu_clock_enable sync between disable and timer's cb Liu Ping Fan
2013-10-07 12:24 ` [Qemu-devel] [PATCH v5 0/4] timers thread-safe stuff Paolo Bonzini
2013-10-08 7:18 ` Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1380090060-6764-3-git-send-email-pingfank@linux.vnet.ibm.com \
--to=qemulist@gmail.com \
--cc=alex@alex.org.uk \
--cc=jan.kiszka@siemens.com \
--cc=kwolf@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).