Convert Clock lists to RCU (V3)
cover Convert Clock lists to RCU (V3)
The first patch in the series converts the clock->timerlists->active_timers list to RCU. It also creates QemuMutex in the parent QemuClock data structure.
The second patch converts clock->timerlists to RCU.
Mike Day (2):
[RFC] Convert active timers list to use RCU V3
[RFC] Convert Clock Timerlists to RCU V3
include/qemu/timer.h | 19 ++++--- qemu-timer.c | 142 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 98 insertions(+), 63 deletions(-)
Signature · 1 line
1.9.0
RFC PATCH 1/2 Convert active timers list to use RCU V3
active_timers is a list of timer lists, associated with a Qemu Clock, that is read-mostly. This patch converts read accesses to the list to use RCU, which should hopefully mitigate most of the synchronization overhead. Write accesses are now via a mutex in the parent data structure. Functions that change the change the parent data structure are also protected by the mutex.
This patch applies against Paolo Bonzini's rcu branch:
https://github.com/bonzini/qemu/tree/rcu
V3:
- Address comments from Alex Bligh and Paolo Bonzini
- Move the mutex into the parent QemuClock structure
--- include/qemu/timer.h | 19 +++++---- qemu-timer.c | 111 +++++++++++++++++++++++++++++---------------------- 2 files changed, 72 insertions(+), 58 deletions(-)
include/qemu/timer.h +9 -10
@@ -5,8 +5,15 @@#include "qemu-common.h" #include "qemu/notify.h" -/* timers */ +#ifdef __GNUC__ +#ifndef __ATOMIC_RELEASE +#define __ATOMIC_RELEASE +#endif +#endif +#include "qemu/atomic.h" +#include "qemu/rcu.h" +/* timers */ #define SCALE_MS 1000000#define SCALE_US 1000#define SCALE_NS 1
@@ -61,6 +68,7 @@ struct QEMUTimer {void *opaque;QEMUTimer *next;int scale; +struct rcu_head rcu; };extern QEMUTimerListGroup main_loop_tlg;
@@ -189,12 +197,6 @@ void qemu_clock_notify(QEMUClockType type);* @enabled: true to enable, false to disable * * Enable or disable a clock -* 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. -* -* Caller should hold BQL. */ void qemu_clock_enable (QEMUClockType type,bool enabled);
@@ -543,7 +545,6 @@ void timer_del(QEMUTimer *ts);* freed while this function is running. */ void timer_mod_ns (QEMUTimer *ts,int64_t expire_time); -/** * timer_mod_anticipate_ns: * @ts: the timer
@@ -685,9 +686,7 @@ 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 )
qemu-timer.c +63 -48
267 lines not on this page · raw message
Signature · 1 line
1.9.0
RFC PATCH 2/2 Convert Clock Timerlists to RCU V3
In reply to Mike Daytimerlists is a list of lists that holds active timers, among other items. It is read-mostly. This patch converts read access to the timerlists to use RCU.
Rather than introduce a second mutex for timerlists, which would require nested mutexes to in order to write to the timerlists, use one QemuMutex in the QemuClock structure for all write access to any list hanging off the QemuClock structure.
This patch applies against Paolo Bonzini's rcu branch:
https://github.com/bonzini/qemu/tree/rcu and also requires the previously submitted patch ae11e1c "Convert active timers list to use RCU for read operations V3."
V3:
- timerlists modifications split to a separate patch (this one).
- Addressed comments from Alex Bligh and Paolo Bonzini.
--- qemu-timer.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)
qemu-timer.c +26 -5
@@ -74,6 +74,7 @@ struct QEMUTimerList {QEMUTimerListNotifyCB *notify_cb;void *notify_opaque;QemuEvent timers_done_ev; +struct rcu_head rcu; };/**
@@ -111,6 +112,13 @@ QEMUTimerList *timerlist_new(QEMUClockType type,return timer_list; } +static void reclaim_timerlist (struct rcu_head *rcu) +{ +QEMUTimerList *tl =container_of (rcu, QEMUTimerList, rcu); +g_free (tl); +} + +void timerlist_free (QEMUTimerList *timer_list) {assert (!timerlist_has_timers (timer_list));
@@ -118,7 +126,7 @@ void timerlist_free(QEMUTimerList *timer_list)QLIST_REMOVE (timer_list, list); }qemu_event_destroy (&timer_list->timers_done_ev); -g_free (timer_list); +call_rcu1 (&timer_list->rcu, reclaim_timerlist); }static void qemu_clock_init (QEMUClockType type)
@@ -143,9 +151,11 @@ void qemu_clock_notify(QEMUClockType type) {QEMUTimerList *timer_list;QEMUClock *clock =qemu_clock_ptr (type); - QLIST_FOREACH(timer_list, &clock->timerlists, list) { +rcu_read_lock (); + QLIST_FOREACH_RCU(timer_list, &clock->timerlists, list) {timerlist_notify (timer_list); } +rcu_read_unlock (); }void qemu_clock_enable (QEMUClockType type,bool enabled)
@@ -157,9 +167,11 @@ void qemu_clock_enable(QEMUClockType type, bool enabled)if (enabled && !old) {qemu_clock_notify (type); }else if (!enabled && old) { - QLIST_FOREACH(tl, &clock->timerlists, list) { +rcu_read_lock (); + QLIST_FOREACH_RCU(tl, &clock->timerlists, list) {qemu_event_wait (&tl->timers_done_ev); } +rcu_read_unlock (); } }
@@ -243,10 +255,12 @@ int64_t qemu_clock_deadline_ns_all(QEMUClockType type)int64_t deadline =-1 ;QEMUTimerList *timer_list;QEMUClock *clock =qemu_clock_ptr (type); - QLIST_FOREACH(timer_list, &clock->timerlists, list) { +rcu_read_lock (); + QLIST_FOREACH_RCU(timer_list, &clock->timerlists, list) { deadline =qemu_soonest_timeout (deadline,timerlist_deadline_ns (timer_list)); } +rcu_read_unlock ();return deadline; }
@@ -262,11 +276,13 @@ QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type)void timerlist_notify (QEMUTimerList *timer_list) { -if (timer_list->notify_cb) { +rcu_read_lock (); +if (atomic_rcu_read (&timer_list->notify_cb)) { timer_list->notify_cb (timer_list->notify_opaque); }else {qemu_notify_event (); } +rcu_read_unlock (); }/* Transition function to convert a nanosecond timeout to ms
@@ -585,13 +601,18 @@ void qemu_clock_register_reset_notifier(QEMUClockType type, Notifier *notifier) {QEMUClock *clock =qemu_clock_ptr (type); +qemu_mutex_lock (&clock->timer_lock);notifier_list_add (&clock->reset_notifiers, notifier); +qemu_mutex_unlock (&clock->timer_lock); }void qemu_clock_unregister_reset_notifier (QEMUClockType type,Notifier *notifier) { +QEMUClock *clock =qemu_clock_ptr (type); +qemu_mutex_lock (&clock->timer_lock);notifier_remove (notifier); +qemu_mutex_unlock (&clock->timer_lock); }void init_clocks (void )
Signature · 1 line
1.9.0