: timers thread-safe stuff
cover : timers thread-safe stuff
The patches has been rebased onto Alex's [RFC] [PATCHv5 00/16] aio / timers: Add AioContext timers and use ppoll
permalink.gmane.org/gmane.comp.emulators.qemu/226333
For some other complied error issue, I can not finish compiling, will fix it later.
Changes since last version:
1. drop the overlap partition and leave only thread-safe stuff
2. For timers_state, since currently, only vm_clock can be read outside BQL. There is no protection with ticks(since the protection will cost more in read_tsc path).
3. use light weight QemuEvent to re-implement the qemu_clock_enable(foo,false)
Liu Ping Fan (2):
timer: protect timers_state's clock with seqlock
timer: make qemu_clock_enable sync between disable and timer's cb
Paolo Bonzini (2):
seqlock: introduce read-write seqlock
qemu-thread: add QemuEvent
cpus.c | 36 +++++++++++--- include/qemu/seqlock.h | 72 +++++++++++++++++++++++++++ include/qemu/thread-posix.h | 8 +++ include/qemu/thread-win32.h | 4 ++ include/qemu/thread.h | 7 +++ include/qemu/timer.h | 1 + qemu-timer.c | 11 +++++ util/qemu-thread-posix.c | 116 ++++++++++++++++++++++++++++++++++++++++++++ util/qemu-thread-win32.c | 26 ++++++++++ 9 files changed, 274 insertions(+), 7 deletions(-) create mode 100644 include/qemu/seqlock.h
Signature · 1 line
1.8.1.4
PATCH 1/4 seqlock: introduce read-write seqlock
From: Paolo Bonzini <[email protected]>
This lets the read-side access run outside the BQL.
--- include/qemu/seqlock.h | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 include/qemu/seqlock.h
include/qemu/seqlock.h new file +72 -0
@@ -0,0 +1,72 @@ +/* +* Seqlock implementation for QEMU +* +* Copyright Red Hat, Inc. 2013 +* +* Author: +* Paolo Bonzini <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ +#ifndef QEMU_SEQLOCK_H +#define QEMU_SEQLOCK_H 1 + +#include <qemu/atomic.h> +#include <qemu/thread.h> + +typedef struct QemuSeqLock QemuSeqLock ; + +struct QemuSeqLock { +QemuMutex *mutex; +unsigned sequence; +}; + +static inline void seqlock_init (QemuSeqLock *sl,QemuMutex *mutex) +{ + sl->mutex = mutex; + sl->sequence =0 ; +} + +/* Lock out other writers and update the count. */ +static inline void seqlock_write_lock (QemuSeqLock *sl) +{ +if (sl->mutex) { +qemu_mutex_lock (sl->mutex); + } + ++sl->sequence; + +/* Write sequence before updating other fields. */ +smp_wmb (); +} + +static inline void seqlock_write_unlock (QemuSeqLock *sl) +{ +/* Write other fields before finalizing sequence. */ +smp_wmb (); + + ++sl->sequence; +if (sl->mutex) { +qemu_mutex_unlock (sl->mutex); + } +} + +static inline unsigned seqlock_read_begin (QemuSeqLock *sl) +{ +/* Always fail if a write is in progress. */ +unsigned ret = sl->sequence & ~1 ; + +/* Read sequence before reading other fields. */ +smp_rmb (); +return ret; +} + +static int seqlock_read_check (const QemuSeqLock *sl,unsigned start) +{ +/* Read other fields before reading final sequence. */ +smp_rmb (); +return unlikely (sl->sequence != start); +} + +#endif
Signature · 1 line
1.8.1.4
PATCH 2/4 timer: protect timers_state's clock with seqlock
In reply to Liu Ping FanIn kvm mode, vm_clock may be read outside BQL. This will make timers_state --the foundation of vm_clock exposed to race condition. Using private lock to protect it.
Note in tcg mode, vm_clock still read inside BQL, so icount is left without change. As for cpu_ticks in timers_state, it is still protected by BQL.
Lock rule: private lock innermost, ie BQL->"this lock"
--- cpus.c | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-)
cpus.c +29 -7
@@ -107,12 +107,17 @@ static int64_t qemu_icount;typedef struct TimersState {int64_t cpu_ticks_prev;int64_t cpu_ticks_offset; +/* QemuClock will be read out of BQL, so protect is with private lock. +* As for cpu_ticks, no requirement to read it outside BQL. +* Lock rule: innermost +*/ +QemuSeqLock clock_seqlock;int64_t cpu_clock_offset;int32_t cpu_ticks_enabled;int64_t dummy; }TimersState ; -TimersState timers_state; +static TimersState timers_state;/* Return the virtual CPU time, based on the instruction counter. */ int64_t cpu_get_icount (void )
@@ -132,6 +137,7 @@ int64_t cpu_get_icount(void) }/* return the host CPU cycle counter and handle stop/restart */ +/* cpu_ticks is safely if holding BQL */ int64_t cpu_get_ticks (void ) {if (use_icount) {
@@ -156,33 +162,46 @@ 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.clock_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_check(&timers_state.clock_seqlock, start); + +return ti; }/* enable cpu_get_ticks() */ void cpu_enable_ticks (void ) { +/* Here, the really thing protected by seqlock is cpu_clock. */ +seqlock_write_lock (&timers_state.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.clock_seqlock); }/* disable cpu_get_ticks() : the clock is stopped. You must not call cpu_get_ticks() after that. */ void cpu_disable_ticks (void ) { +/* Here, the really thing protected by seqlock is cpu_clock. */ +seqlock_write_lock (&timers_state.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_ticks_enabled =0 ; } +seqlock_write_unlock (&timers_state.clock_seqlock); }/* Correlation between real and virtual time is always going to be
@@ -364,6 +383,9 @@ static const VMStateDescription vmstate_timers = {void configure_icount (const char *option) { +QemuMutex *mutex =g_malloc0 (sizeof (QemuMutex)); +qemu_mutex_init (mutex); +seqlock_init (&timers_state.clock_seqlock, mutex);vmstate_register (NULL ,0 , &vmstate_timers, &timers_state);if (!option) {return ;
Signature · 1 line
1.8.1.4
Re: [Qemu-devel] [PATCH 2/4] timer: protect timers_state's clock with seqlock
Quoted 2 lines
In kvm mode,
vm_clockmay be read outside BQL.
Not just in KVM mode (we will be able to use dataplane with TCG sooner or later), actually.
Otherwise looks good!
Paolo
Quoted 113 lines
This will make
timers_state--the foundation ofvm_clockexposed to race condition. Using private lock to protect it.Note in tcg mode,
vm_clockstill read inside BQL, so icount is left without change. As forcpu_ticksintimers_state, it is still protected by BQL.Lock rule: private lock innermost, ie BQL->"this lock"
Signed-off-by Liu Ping Fan <[email protected]>--- cpus.c | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-)cpus.c +29
-8@@ -107,12 +107,17 @@ static int64_t qemu_icount;typedef struct TimersState {int64_t cpu_ticks_prev;int64_t cpu_ticks_offset; +/* QemuClock will be read out of BQL, so protect is with private lock. +* As for cpu_ticks, no requirement to read it outside BQL. +* Lock rule: innermost +*/ +QemuSeqLock clock_seqlock;int64_t cpu_clock_offset;int32_t cpu_ticks_enabled;int64_t dummy; }TimersState ; -TimersState timers_state; +static TimersState timers_state;/* Return the virtual CPU time, based on the instruction counter. */ int64_t cpu_get_icount (void )@@ -132,6 +137,7 @@ int64_t cpu_get_icount(void) }/* return the host CPU cycle counter and handle stop/restart */ +/* cpu_ticks is safely if holding BQL */ int64_t cpu_get_ticks (void ) {if (use_icount) {@@ -156,33 +162,46 @@ 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.clock_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_check(&timers_state.clock_seqlock, start); + +return ti; }/* enable cpu_get_ticks() */ void cpu_enable_ticks (void ) { +/* Here, the really thing protected by seqlock is cpu_clock. */ +seqlock_write_lock (&timers_state.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.clock_seqlock); }/* disable cpu_get_ticks() : the clock is stopped. You must not call cpu_get_ticks() after that. */ void cpu_disable_ticks (void ) { +/* Here, the really thing protected by seqlock is cpu_clock. */ +seqlock_write_lock (&timers_state.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_ticks_enabled =0 ; } +seqlock_write_unlock (&timers_state.clock_seqlock); }/* Correlation between real and virtual time is always going to be @@ -364,6 +383,9 @@ static const VMStateDescription vmstate_timers = {void configure_icount (const char *option) { +QemuMutex *mutex =g_malloc0 (sizeof (QemuMutex)); +qemu_mutex_init (mutex); +seqlock_init (&timers_state.clock_seqlock, mutex);vmstate_register (NULL ,0 , &vmstate_timers, &timers_state);if (!option) {return ; --1.8.1.4
Re: [Qemu-devel] [PATCH 2/4] timer: protect timers_state's clock with seqlock
Quoted 6 lines · Paolo Bonzini
Quoted 2 lines
In kvm mode,
vm_clockmay be read outside BQL.Not just in KVM mode (we will be able to use dataplane with TCG sooner or later), actually.
Oh. But this patch does not fix cpu_get_icount()'s thread-safe issue. So currently, could I just change the commit log instead of fixing it?
Regards,
Pingfan
Quoted 118 lines
Otherwise looks good!
Paolo
Quoted 113 lines
This will make
timers_state--the foundation ofvm_clockexposed to race condition. Using private lock to protect it.Note in tcg mode,
vm_clockstill read inside BQL, so icount is left without change. As forcpu_ticksintimers_state, it is still protected by BQL.Lock rule: private lock innermost, ie BQL->"this lock"
Signed-off-by Liu Ping Fan <[email protected]>--- cpus.c | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-)cpus.c +29
-8@@ -107,12 +107,17 @@ static int64_t qemu_icount;typedef struct TimersState {int64_t cpu_ticks_prev;int64_t cpu_ticks_offset; +/* QemuClock will be read out of BQL, so protect is with private lock. +* As for cpu_ticks, no requirement to read it outside BQL. +* Lock rule: innermost +*/ +QemuSeqLock clock_seqlock;int64_t cpu_clock_offset;int32_t cpu_ticks_enabled;int64_t dummy; }TimersState ; -TimersState timers_state; +static TimersState timers_state;/* Return the virtual CPU time, based on the instruction counter. */ int64_t cpu_get_icount (void )@@ -132,6 +137,7 @@ int64_t cpu_get_icount(void) }/* return the host CPU cycle counter and handle stop/restart */ +/* cpu_ticks is safely if holding BQL */ int64_t cpu_get_ticks (void ) {if (use_icount) {@@ -156,33 +162,46 @@ 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.clock_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_check(&timers_state.clock_seqlock, start); + +return ti; }/* enable cpu_get_ticks() */ void cpu_enable_ticks (void ) { +/* Here, the really thing protected by seqlock is cpu_clock. */ +seqlock_write_lock (&timers_state.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.clock_seqlock); }/* disable cpu_get_ticks() : the clock is stopped. You must not call cpu_get_ticks() after that. */ void cpu_disable_ticks (void ) { +/* Here, the really thing protected by seqlock is cpu_clock. */ +seqlock_write_lock (&timers_state.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_ticks_enabled =0 ; } +seqlock_write_unlock (&timers_state.clock_seqlock); }/* Correlation between real and virtual time is always going to be @@ -364,6 +383,9 @@ static const VMStateDescription vmstate_timers = {void configure_icount (const char *option) { +QemuMutex *mutex =g_malloc0 (sizeof (QemuMutex)); +qemu_mutex_init (mutex); +seqlock_init (&timers_state.clock_seqlock, mutex);vmstate_register (NULL ,0 , &vmstate_timers, &timers_state);if (!option) {return ; --1.8.1.4
Re: [Qemu-devel] [PATCH 2/4] timer: protect timers_state's clock with seqlock
Quoted 9 lines · liu ping fan
Quoted 6 lines · Paolo Bonzini
Quoted 2 lines
In kvm mode,
vm_clockmay be read outside BQL.Not just in KVM mode (we will be able to use dataplane with TCG sooner or later), actually.
Oh. But this patch does not fix cpu_get_icount()'s thread-safe issue. So currently, could I just change the commit log instead of fixing it?
Yeah, icount is a bit more complicated. Just change the commit log.
Paolo
Re: [Qemu-devel] [PATCH 2/4] timer: protect timers_state's clock with seqlock
In reply to Liu Ping Fancpus.c static int64_t qemu_icount; · line 113
Quoted 12 lines · Liu Ping Fan
cpus.c +4
-0@@ -107,12 +107,17 @@ static int64_t qemu_icount; typedef struct TimersState { int64_t cpu_ticks_prev; int64_t cpu_ticks_offset; + /* QemuClock will be read out of BQL, so protect is with private lock. + * As for cpu_ticks, no requirement to read it outside BQL. + * Lock rule: innermost + */
Please document exactly which fields the lock protects.
cpus.c int64_t cpu_get_ticks(void) · line 183
Quoted 4 lines
cpus.c +1
-0/* enable cpu_get_ticks() */ void cpu_enable_ticks (void ) { +/* Here, the really thing protected by seqlock is cpu_clock. */
What is cpu_clock?
cpus.c static const VMStateDescription vmstate_timers = { · line 388
Quoted 7 lines
cpus.c +3
-0@@ -364,6 +383,9 @@ static const VMStateDescription vmstate_timers = {void configure_icount (const char *option) { +QemuMutex *mutex =g_malloc0 (sizeof (QemuMutex)); +qemu_mutex_init (mutex); +seqlock_init (&timers_state.clock_seqlock, mutex);
We always set up this mutex, so it could be a field in timers_state. That avoids the g_malloc() without g_free().
Re: [Qemu-devel] [PATCH 2/4] timer: protect timers_state's clock with seqlock
Quoted 16 lines · Stefan Hajnoczi
cpus.c static int64_t qemu_icount; · line 113
Quoted 12 lines · Liu Ping Fan
cpus.c +4
-0@@ -107,12 +107,17 @@ static int64_t qemu_icount; typedef struct TimersState { int64_t cpu_ticks_prev; int64_t cpu_ticks_offset; + /* QemuClock will be read out of BQL, so protect is with private lock. + * As for cpu_ticks, no requirement to read it outside BQL. + * Lock rule: innermost + */Please document exactly which fields the lock protects.
The lock protects cpu_clock_offset. Will fix it.
Quoted 7 lines
cpus.c int64_t cpu_get_ticks(void) · line 183
Quoted 4 lines
cpus.c +1
-0/* enable cpu_get_ticks() */ void cpu_enable_ticks (void ) { +/* Here, the really thing protected by seqlock is cpu_clock. */ What is
cpu_clock?
cpu_clock_offset
Quoted 11 lines
cpus.c static const VMStateDescription vmstate_timers = { · line 388
Quoted 7 lines
cpus.c +3
-0@@ -364,6 +383,9 @@ static const VMStateDescription vmstate_timers = {void configure_icount (const char *option) { +QemuMutex *mutex =g_malloc0 (sizeof (QemuMutex)); +qemu_mutex_init (mutex); +seqlock_init (&timers_state.clock_seqlock, mutex);We always set up this mutex, so it could be a field in
timers_state. That avoids theg_malloc()withoutg_free().
Will fix in next version
Thx®ards,
Pingfan
PATCH 3/4 qemu-thread: add QemuEvent
In reply to Liu Ping FanFrom: Paolo Bonzini <[email protected]>
This emulates Win32 manual-reset events using futexes or conditional variables. Typical ways to use them are with multi-producer, single-consumer data structures, to test for a complex condition whose elements come from different threads:
for (;;) {
qemu_event_reset(ev);
... test complex condition ...
if (condition is true) {
break;
}
qemu_event_wait(ev);
}Or more efficiently (but with some duplication):
... evaluate condition ...
while (!condition) {
qemu_event_reset(ev);
... evaluate condition ...
if (!condition) {
qemu_event_wait(ev);
... evaluate condition ...
}
}QemuEvent provides a very fast userspace path in the common case when no other thread is waiting, or the event is not changing state. It is used to report RCU quiescent states to the thread calling synchronize_rcu (the latter being the single consumer), and to report call_rcu invocations to the thread that receives them.
--- include/qemu/thread-posix.h | 8 +++ include/qemu/thread-win32.h | 4 ++ include/qemu/thread.h | 7 +++ util/qemu-thread-posix.c | 116 ++++++++++++++++++++++++++++++++++++++++++++ util/qemu-thread-win32.c | 26 ++++++++++ 5 files changed, 161 insertions(+)
include/qemu/thread-posix.h +8 -0
@@ -21,6 +21,14 @@ struct QemuSemaphore {#endif }; +struct QemuEvent { +#ifndef __linux__ +pthread_mutex_t lock; +pthread_cond_t cond; +#endif +unsigned value; +}; +struct QemuThread {pthread_t thread; };
include/qemu/thread-win32.h +4 -0
@@ -17,6 +17,10 @@ struct QemuSemaphore {HANDLE sema; }; +struct QemuEvent { +HANDLE event; +}; +typedef struct QemuThreadData QemuThreadData ;struct QemuThread {QemuThreadData *data;
include/qemu/thread.h +7 -0
@@ -7,6 +7,7 @@typedef struct QemuMutex QemuMutex ;typedef struct QemuCond QemuCond ;typedef struct QemuSemaphore QemuSemaphore ; +typedef struct QemuEvent QemuEvent ;typedef struct QemuThread QemuThread ;#ifdef _WIN32
@@ -45,6 +46,12 @@ void qemu_sem_wait(QemuSemaphore *sem);int qemu_sem_timedwait (QemuSemaphore *sem,int ms);void qemu_sem_destroy (QemuSemaphore *sem); +void qemu_event_init (QemuEvent *ev,bool init); +void qemu_event_set (QemuEvent *ev); +void qemu_event_reset (QemuEvent *ev); +void qemu_event_wait (QemuEvent *ev); +void qemu_event_destroy (QemuEvent *ev); +void qemu_thread_create (QemuThread *thread,void *(*start_routine)(void *),void *arg,int mode);
util/qemu-thread-posix.c +116 -0
129 lines not on this page · raw message
util/qemu-thread-win32.c +26 -0
@@ -227,6 +227,32 @@ void qemu_sem_wait(QemuSemaphore *sem) } } +void qemu_event_init (QemuEvent *ev,bool init) +{ +/* Manual reset. */ + ev->event =CreateEvent (NULL , TRUE, init,NULL ); +} + +void qemu_event_destroy (QemuEvent *ev) +{ +CloseHandle (ev->event); +} + +void qemu_event_set (QemuEvent *ev) +{ +SetEvent (ev->event); +} + +void qemu_event_reset (QemuEvent *ev) +{ +ResetEvent (ev->event); +} + +void qemu_event_wait (QemuEvent *ev) +{ +WaitForSingleObject (ev->event,INFINITE ); +} +struct QemuThreadData {/* Passed to win32_start_routine. */ void *(*start_routine)(void *);
Signature · 1 line
1.8.1.4
PATCH 4/4 timer: make qemu_clock_enable sync between disable and timer's cb
In reply to Liu Ping FanAfter disabling the QemuClock, we should make sure that no QemuTimers are still in flight. To implement that with light overhead, we resort to QemuEvent. The caller of disabling will wait on QemuEvent of each timerlist.
Note, qemu_clock_enable(foo,false) can _not_ be called from timer's cb. And the callers of qemu_clock_enable() should be sync by themselves, not protected by this patch.
--- include/qemu/timer.h | 1 + qemu-timer.c | 11 +++++++++++ 2 files changed, 12 insertions(+)
include/qemu/timer.h +1 -0
@@ -85,6 +85,7 @@ int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup tlg);int qemu_timeout_ns_to_ms (int64_t ns);int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout); +/* The disable of clock can not be called in timer's cb */ void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);
qemu-timer.c +11 -0
@@ -71,6 +71,8 @@ struct QEMUTimerList {QLIST_ENTRY (QEMUTimerList) list;QEMUTimerListNotifyCB *notify_cb;void *notify_opaque; +/* light weight method to mark the end of timerlist's running */ +QemuEvent ev; };struct QEMUTimer {
@@ -92,6 +94,7 @@ static QEMUTimerList *timerlist_new_from_clock(QEMUClock *clock)QEMUTimerList *tl; tl =g_malloc0 (sizeof (QEMUTimerList)); +qemu_event_init (&tl->ev, false); tl->clock = clock;QLIST_INSERT_HEAD (&clock->timerlists, tl, list);return tl;
@@ -145,12 +148,18 @@ void qemu_clock_notify(QEMUClock *clock) } } +/* The disable of clock can _not_ be called from timer's cb */ void qemu_clock_enable (QEMUClock *clock,bool enabled) { +QEMUTimerList *tl;bool old = clock->enabled; clock->enabled = enabled;if (enabled && !old) {qemu_clock_notify (clock); + }else if (!enabled && old) { + QLIST_FOREACH(tl, &clock->timerlists, list) { +qemu_event_wait (&tl->ev); + } } }
@@ -419,6 +428,7 @@ bool timerlist_run_timers(QEMUTimerList *tl) } current_time =qemu_get_clock_ns (tl->clock); +qemu_event_reset (&tl->ev);for (;;) { ts = tl->active_timers;if (!qemu_timer_expired_ns (ts, current_time)) {
@@ -432,6 +442,7 @@ bool timerlist_run_timers(QEMUTimerList *tl) ts->cb (ts->opaque); progress = true; } +qemu_event_set (&tl->ev);return progress; }
Signature · 1 line
1.8.1.4
Re: [Qemu-devel] [PATCH 4/4] timer: make qemu_clock_enable sync between disable and timer's cb
Quoted 20 lines · Liu Ping Fan
After disabling the QemuClock, we should make sure that no QemuTimers are still in flight. To implement that with light overhead, we resort to QemuEvent. The caller of disabling will wait on QemuEvent of each timerlist.
Note,
qemu_clock_enable(foo,false)can _not_ be called from timer's cb. And the callers ofqemu_clock_enable()should be sync by themselves, not protected by this patch.Signed-off-by Liu Ping Fan <[email protected]>--- include/qemu/timer.h | 1 + qemu-timer.c | 11 +++++++++++ 2 files changed, 12 insertions(+)include/qemu/timer.h +0
-0
include/qemu/timer.h int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup tlg); · line 88
Quoted 4 lines
include/qemu/timer.h +1
-0@@ -85,6 +85,7 @@ int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup tlg);int qemu_timeout_ns_to_ms (int64_t ns);int qemu_poll_ns (GPollFD *fds,uint nfds,int64_t timeout); +/* The disable of clock can not be called in timer's cb */
See below for a more verbose version of the comment. For now leave it only in the .c file, we should add comments to all of timer.h.
Quoted 17 lines
include/qemu/timer.h +0
-0void qemu_clock_enable (QEMUClock *clock,bool enabled);void qemu_clock_warp (QEMUClock *clock);qemu-timer.c +2
-0@@ -71,6 +71,8 @@ struct QEMUTimerList {QLIST_ENTRY (QEMUTimerList) list;QEMUTimerListNotifyCB *notify_cb;void *notify_opaque; +/* light weight method to mark the end of timerlist's running */ +QemuEvent ev; };struct QEMUTimer {
qemu-timer.c static QEMUTimerList *timerlist_new_from_clock(QEMUClock *clock) · line 97
Quoted 4 lines
qemu-timer.c +1
-0@@ -92,6 +94,7 @@ static QEMUTimerList *timerlist_new_from_clock(QEMUClock *clock)QEMUTimerList *tl; tl =g_malloc0 (sizeof (QEMUTimerList)); +qemu_event_init (&tl->ev, false);
The event should start as "set", since "set" means "not inside qemu_run_timers".
qemu-timer.c void qemu_clock_notify(QEMUClock *clock) · line 151
Quoted 8 lines
qemu-timer.c +1
-0tl->clock = clock;QLIST_INSERT_HEAD (&clock->timerlists, tl, list);return tl;@@ -145,12 +148,18 @@ void qemu_clock_notify(QEMUClock *clock) } } +/* The disable of clock can _not_ be called from timer's cb */
/* Disabling the clock will wait for related timerlists to stop
* executing qemu_run_timers. Thus, this functions should not
* be used from the callback of a timer that is based on @clock.
* Doing so would cause a deadlock.
*/
Quoted 15 lines
qemu-timer.c +5
-0void qemu_clock_enable (QEMUClock *clock,bool enabled) { +QEMUTimerList *tl;bool old = clock->enabled; clock->enabled = enabled;if (enabled && !old) {qemu_clock_notify (clock); + }else if (!enabled && old) { + QLIST_FOREACH(tl, &clock->timerlists, list) { +qemu_event_wait (&tl->ev); + } } }
qemu-timer.c bool timerlist_run_timers(QEMUTimerList *tl) · line 431
Quoted 4 lines
qemu-timer.c +1
-0@@ -419,6 +428,7 @@ bool timerlist_run_timers(QEMUTimerList *tl) } current_time =qemu_get_clock_ns (tl->clock); +qemu_event_reset (&tl->ev);
Race condition here. You need to test clock->enabled while the event is reset. Otherwise you get:
-------------------------------------------------------------------------
thread 1 is running thread 2 is running
qemu_clock_enable(foo, false) qemu_run_timers(tl);
-------------------------------------------------------------------------
** event is initially set **
if (!clock->enabled) return;
clock->enabled = false;
qemu_event_wait(&tl->ev);
return;
qemu_event_reset(&tl->ev);
invokes callback
qemu_event_set(&tl->ev);-------------------------------------------------------------------------
violating the invariant that no callbacks are invoked after the return from qemu_clock_enable(foo, false).
Paolo
Quoted 14 lines
qemu-timer.c +1
-0for (;;) { ts = tl->active_timers;if (!qemu_timer_expired_ns (ts, current_time)) {@@ -432,6 +442,7 @@ bool timerlist_run_timers(QEMUTimerList *tl) ts->cb (ts->opaque); progress = true; } +qemu_event_set (&tl->ev);return progress; }Signature · 1 line
1.8.1.4
Pingfan,
Quoted 7 lines · --On 5 August 2013 15:33:22 +0800 Liu Ping Fan <[email protected]>…
The patches has been rebased onto Alex's [RFC] [PATCHv5 00/16] aio /
timers: Add AioContext timers and use ppoll permalink.gmane.org/gmane.comp.emulators.qemu/226333For some other complied error issue, I can not finish compiling, will fix it later.
Quoted 6 lines
Changes since last version:
1. drop the overlap partition and leave only thread-safe stuff
2. Fortimers_state, since currently, onlyvm_clockcan be read outside
BQL. There is no protection with ticks(since the protection will cost
more inread_tscpath). 3. use light weight QemuEvent to re-implement theqemu_clock_enable(foo,false)
I think you may need to protect a little more.
For instance, do we need to take a lock whilst traversing a QEMUTimerList? I hope the answer to this is no. The design idea of my stuff was that only one thread would be manipulating or traversing this list. As the notify function is a property of the QEMUTimerList itself, no traversal is necessary for that. However, the icount stuff (per my patch 15) does look at the deadlines for the vm_clock QEMUTimerLists (which is the first entry). Is that always going to be thread safe? Before the icount stuff, I was pretty certain this did not need a lock, but perhaps it does now. If the soonest deadline on any QEMUTimerList was stored in a 64 bit atomic variable, this might remove the need for a lock; it's possible that putting some memory barrier operations in might be enough.
Also, we maintain a per-clock list of QEMUTimerLists. This list is traversed by the icount stuff, by things adding and removing timers (e.g. creation/deletion of AioContext) and whenever a QEMUClock is reenabled. I think this DOES need a lock. Aside from the icount stuff, usage is very infrequent. It's far more frequent with the icount stuff, so it should probably be optimised for that case.
Signature · 1 line
Alex Bligh
Quoted 21 lines · Alex Bligh
Pingfan,
--On 5 August 2013 15:33:22 +0800 Liu Ping Fan <[email protected]>
Quoted 7 lines · wrote:
The patches has been rebased onto Alex's [RFC] [PATCHv5 00/16] aio /
timers: Add AioContext timers and use ppoll permalink.gmane.org/gmane.comp.emulators.qemu/226333For some other complied error issue, I can not finish compiling, will fix it later.
Quoted 6 lines
Changes since last version:
1. drop the overlap partition and leave only thread-safe stuff
2. Fortimers_state, since currently, onlyvm_clockcan be read outside
BQL. There is no protection with ticks(since the protection will cost
more inread_tscpath). 3. use light weight QemuEvent to re-implement theqemu_clock_enable(foo,false)
Quoted 2 lines
I think you may need to protect a little more.
Yes. There is still race issue left. If Stefanha and you will not do it, I am pleased to do that.
Quoted 23 lines
For instance, do we need to take a lock whilst traversing a QEMUTimerList? I hope the answer to this is no. The design idea of my stuff was that only one thread would be manipulating or traversing this list. As the notify function is a property of the QEMUTimerList itself, no traversal is necessary for that. However, the icount stuff (per my patch 15) does look at the deadlines for the
vm_clockQEMUTimerLists (which is the first entry). Is that always going to be thread safe? Before the icount stuff, I was pretty certain this did not need a lock, but perhaps it does now. If the soonest deadline on any QEMUTimerList was stored in a 64 bit atomic variable, this might remove the need for a lock; it's possible that putting some memory barrier operations in might be enough.Also, we maintain a per-clock list of QEMUTimerLists. This list is traversed by the icount stuff, by things adding and removing timers (e.g. creation/deletion of AioContext) and whenever a QEMUClock is reenabled. I think this DOES need a lock. Aside from the icount stuff, usage is very infrequent. It's far more frequent with the icount stuff, so it should probably be optimised for that case.
--
Alex Bligh
Pingfan,
Quoted 4 lines · --On 6 August 2013 13:37:02 +0800 liu ping fan <[email protected]> wrote:
Quoted 2 lines
I think you may need to protect a little more.
Yes. There is still race issue left. If Stefanha and you will not do it, I am pleased to do that.
I think you've probably got a better view of what to put in than I have. I'd just stick a couple of dumb mutexes in and wait until someone complains about performance. Quite happy to do that but your approach appeared a bit more nuanced.
Signature · 1 line
Alex Bligh