4 * Userspace RCU library with explicit memory barriers
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
8 * Copyright 2015 Red Hat, Inc.
10 * Ported to QEMU by Paolo Bonzini <pbonzini@redhat.com>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
29 #include "qemu/osdep.h"
30 #include "qemu-common.h"
32 #include "qemu/atomic.h"
33 #include "qemu/thread.h"
34 #include "qemu/main-loop.h"
37 * Global grace period counter. Bit 0 is always one in rcu_gp_ctr.
38 * Bits 1 and above are defined in synchronize_rcu.
40 #define RCU_GP_LOCKED (1UL << 0)
41 #define RCU_GP_CTR (1UL << 1)
43 unsigned long rcu_gp_ctr
= RCU_GP_LOCKED
;
45 QemuEvent rcu_gp_event
;
46 static QemuMutex rcu_registry_lock
;
47 static QemuMutex rcu_sync_lock
;
50 * Check whether a quiescent state was crossed between the beginning of
51 * update_counter_and_wait and now.
53 static inline int rcu_gp_ongoing(unsigned long *ctr
)
58 return v
&& (v
!= rcu_gp_ctr
);
61 /* Written to only by each individual reader. Read by both the reader and the
64 __thread
struct rcu_reader_data rcu_reader
;
66 /* Protected by rcu_registry_lock. */
67 typedef QLIST_HEAD(, rcu_reader_data
) ThreadList
;
68 static ThreadList registry
= QLIST_HEAD_INITIALIZER(registry
);
70 /* Wait for previous parity/grace period to be empty of readers. */
71 static void wait_for_readers(void)
73 ThreadList qsreaders
= QLIST_HEAD_INITIALIZER(qsreaders
);
74 struct rcu_reader_data
*index
, *tmp
;
77 /* We want to be notified of changes made to rcu_gp_ongoing
78 * while we walk the list.
80 qemu_event_reset(&rcu_gp_event
);
82 /* Instead of using atomic_mb_set for index->waiting, and
83 * atomic_mb_read for index->ctr, memory barriers are placed
84 * manually since writes to different threads are independent.
85 * qemu_event_reset has acquire semantics, so no memory barrier
88 QLIST_FOREACH(index
, ®istry
, node
) {
89 atomic_set(&index
->waiting
, true);
92 /* Here, order the stores to index->waiting before the
93 * loads of index->ctr.
97 QLIST_FOREACH_SAFE(index
, ®istry
, node
, tmp
) {
98 if (!rcu_gp_ongoing(&index
->ctr
)) {
99 QLIST_REMOVE(index
, node
);
100 QLIST_INSERT_HEAD(&qsreaders
, index
, node
);
102 /* No need for mb_set here, worst of all we
103 * get some extra futex wakeups.
105 atomic_set(&index
->waiting
, false);
109 if (QLIST_EMPTY(®istry
)) {
113 /* Wait for one thread to report a quiescent state and try again.
114 * Release rcu_registry_lock, so rcu_(un)register_thread() doesn't
115 * wait too much time.
117 * rcu_register_thread() may add nodes to ®istry; it will not
118 * wake up synchronize_rcu, but that is okay because at least another
119 * thread must exit its RCU read-side critical section before
120 * synchronize_rcu is done. The next iteration of the loop will
121 * move the new thread's rcu_reader from ®istry to &qsreaders,
122 * because rcu_gp_ongoing() will return false.
124 * rcu_unregister_thread() may remove nodes from &qsreaders instead
125 * of ®istry if it runs during qemu_event_wait. That's okay;
126 * the node then will not be added back to ®istry by QLIST_SWAP
127 * below. The invariant is that the node is part of one list when
128 * rcu_registry_lock is released.
130 qemu_mutex_unlock(&rcu_registry_lock
);
131 qemu_event_wait(&rcu_gp_event
);
132 qemu_mutex_lock(&rcu_registry_lock
);
135 /* put back the reader list in the registry */
136 QLIST_SWAP(®istry
, &qsreaders
, node
);
139 void synchronize_rcu(void)
141 qemu_mutex_lock(&rcu_sync_lock
);
142 qemu_mutex_lock(&rcu_registry_lock
);
144 if (!QLIST_EMPTY(®istry
)) {
145 /* In either case, the atomic_mb_set below blocks stores that free
146 * old RCU-protected pointers.
148 if (sizeof(rcu_gp_ctr
) < 8) {
149 /* For architectures with 32-bit longs, a two-subphases algorithm
150 * ensures we do not encounter overflow bugs.
152 * Switch parity: 0 -> 1, 1 -> 0.
154 atomic_mb_set(&rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR
);
156 atomic_mb_set(&rcu_gp_ctr
, rcu_gp_ctr
^ RCU_GP_CTR
);
158 /* Increment current grace period. */
159 atomic_mb_set(&rcu_gp_ctr
, rcu_gp_ctr
+ RCU_GP_CTR
);
165 qemu_mutex_unlock(&rcu_registry_lock
);
166 qemu_mutex_unlock(&rcu_sync_lock
);
170 #define RCU_CALL_MIN_SIZE 30
172 /* Multi-producer, single-consumer queue based on urcu/static/wfqueue.h
173 * from liburcu. Note that head is only used by the consumer.
175 static struct rcu_head dummy
;
176 static struct rcu_head
*head
= &dummy
, **tail
= &dummy
.next
;
177 static int rcu_call_count
;
178 static QemuEvent rcu_call_ready_event
;
180 static void enqueue(struct rcu_head
*node
)
182 struct rcu_head
**old_tail
;
185 old_tail
= atomic_xchg(&tail
, &node
->next
);
186 atomic_mb_set(old_tail
, node
);
189 static struct rcu_head
*try_dequeue(void)
191 struct rcu_head
*node
, *next
;
194 /* Test for an empty list, which we do not expect. Note that for
195 * the consumer head and tail are always consistent. The head
196 * is consistent because only the consumer reads/writes it.
197 * The tail, because it is the first step in the enqueuing.
198 * It is only the next pointers that might be inconsistent.
200 if (head
== &dummy
&& atomic_mb_read(&tail
) == &dummy
.next
) {
204 /* If the head node has NULL in its next pointer, the value is
205 * wrong and we need to wait until its enqueuer finishes the update.
208 next
= atomic_mb_read(&head
->next
);
213 /* Since we are the sole consumer, and we excluded the empty case
214 * above, the queue will always have at least two nodes: the
215 * dummy node, and the one being removed. So we do not need to update
220 /* If we dequeued the dummy node, add it back at the end and retry. */
221 if (node
== &dummy
) {
229 static void *call_rcu_thread(void *opaque
)
231 struct rcu_head
*node
;
233 rcu_register_thread();
237 int n
= atomic_read(&rcu_call_count
);
239 /* Heuristically wait for a decent number of callbacks to pile up.
240 * Fetch rcu_call_count now, we only must process elements that were
241 * added before synchronize_rcu() starts.
243 while (n
== 0 || (n
< RCU_CALL_MIN_SIZE
&& ++tries
<= 5)) {
246 qemu_event_reset(&rcu_call_ready_event
);
247 n
= atomic_read(&rcu_call_count
);
249 qemu_event_wait(&rcu_call_ready_event
);
252 n
= atomic_read(&rcu_call_count
);
255 atomic_sub(&rcu_call_count
, n
);
257 qemu_mutex_lock_iothread();
259 node
= try_dequeue();
261 qemu_mutex_unlock_iothread();
262 qemu_event_reset(&rcu_call_ready_event
);
263 node
= try_dequeue();
265 qemu_event_wait(&rcu_call_ready_event
);
266 node
= try_dequeue();
268 qemu_mutex_lock_iothread();
274 qemu_mutex_unlock_iothread();
279 void call_rcu1(struct rcu_head
*node
, void (*func
)(struct rcu_head
*node
))
283 atomic_inc(&rcu_call_count
);
284 qemu_event_set(&rcu_call_ready_event
);
287 void rcu_register_thread(void)
289 assert(rcu_reader
.ctr
== 0);
290 qemu_mutex_lock(&rcu_registry_lock
);
291 QLIST_INSERT_HEAD(®istry
, &rcu_reader
, node
);
292 qemu_mutex_unlock(&rcu_registry_lock
);
295 void rcu_unregister_thread(void)
297 qemu_mutex_lock(&rcu_registry_lock
);
298 QLIST_REMOVE(&rcu_reader
, node
);
299 qemu_mutex_unlock(&rcu_registry_lock
);
302 static void rcu_init_complete(void)
306 qemu_mutex_init(&rcu_registry_lock
);
307 qemu_mutex_init(&rcu_sync_lock
);
308 qemu_event_init(&rcu_gp_event
, true);
310 qemu_event_init(&rcu_call_ready_event
, false);
312 /* The caller is assumed to have iothread lock, so the call_rcu thread
313 * must have been quiescent even after forking, just recreate it.
315 qemu_thread_create(&thread
, "call_rcu", call_rcu_thread
,
316 NULL
, QEMU_THREAD_DETACHED
);
318 rcu_register_thread();
321 static int atfork_depth
= 1;
323 void rcu_enable_atfork(void)
328 void rcu_disable_atfork(void)
334 static void rcu_init_lock(void)
336 if (atfork_depth
< 1) {
340 qemu_mutex_lock(&rcu_sync_lock
);
341 qemu_mutex_lock(&rcu_registry_lock
);
344 static void rcu_init_unlock(void)
346 if (atfork_depth
< 1) {
350 qemu_mutex_unlock(&rcu_registry_lock
);
351 qemu_mutex_unlock(&rcu_sync_lock
);
354 static void rcu_init_child(void)
356 if (atfork_depth
< 1) {
360 memset(®istry
, 0, sizeof(registry
));
365 static void __attribute__((__constructor__
)) rcu_init(void)
368 pthread_atfork(rcu_init_lock
, rcu_init_unlock
, rcu_init_child
);