qdev: Remove DeviceClass::init() and ::exit()
[qemu/ar7.git] / util / rcu.c
blob5676c22bd1f571ca521f14a222ae88418b1eeacb
1 /*
2 * urcu-mb.c
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"
31 #include "qemu/rcu.h"
32 #include "qemu/atomic.h"
33 #include "qemu/thread.h"
34 #include "qemu/main-loop.h"
35 #if defined(CONFIG_MALLOC_TRIM)
36 #include <malloc.h>
37 #endif
40 * Global grace period counter. Bit 0 is always one in rcu_gp_ctr.
41 * Bits 1 and above are defined in synchronize_rcu.
43 #define RCU_GP_LOCKED (1UL << 0)
44 #define RCU_GP_CTR (1UL << 1)
46 unsigned long rcu_gp_ctr = RCU_GP_LOCKED;
48 QemuEvent rcu_gp_event;
49 static QemuMutex rcu_registry_lock;
50 static QemuMutex rcu_sync_lock;
53 * Check whether a quiescent state was crossed between the beginning of
54 * update_counter_and_wait and now.
56 static inline int rcu_gp_ongoing(unsigned long *ctr)
58 unsigned long v;
60 v = atomic_read(ctr);
61 return v && (v != rcu_gp_ctr);
64 /* Written to only by each individual reader. Read by both the reader and the
65 * writers.
67 __thread struct rcu_reader_data rcu_reader;
69 /* Protected by rcu_registry_lock. */
70 typedef QLIST_HEAD(, rcu_reader_data) ThreadList;
71 static ThreadList registry = QLIST_HEAD_INITIALIZER(registry);
73 /* Wait for previous parity/grace period to be empty of readers. */
74 static void wait_for_readers(void)
76 ThreadList qsreaders = QLIST_HEAD_INITIALIZER(qsreaders);
77 struct rcu_reader_data *index, *tmp;
79 for (;;) {
80 /* We want to be notified of changes made to rcu_gp_ongoing
81 * while we walk the list.
83 qemu_event_reset(&rcu_gp_event);
85 /* Instead of using atomic_mb_set for index->waiting, and
86 * atomic_mb_read for index->ctr, memory barriers are placed
87 * manually since writes to different threads are independent.
88 * qemu_event_reset has acquire semantics, so no memory barrier
89 * is needed here.
91 QLIST_FOREACH(index, &registry, node) {
92 atomic_set(&index->waiting, true);
95 /* Here, order the stores to index->waiting before the loads of
96 * index->ctr. Pairs with smp_mb_placeholder() in rcu_read_unlock(),
97 * ensuring that the loads of index->ctr are sequentially consistent.
99 smp_mb_global();
101 QLIST_FOREACH_SAFE(index, &registry, node, tmp) {
102 if (!rcu_gp_ongoing(&index->ctr)) {
103 QLIST_REMOVE(index, node);
104 QLIST_INSERT_HEAD(&qsreaders, index, node);
106 /* No need for mb_set here, worst of all we
107 * get some extra futex wakeups.
109 atomic_set(&index->waiting, false);
113 if (QLIST_EMPTY(&registry)) {
114 break;
117 /* Wait for one thread to report a quiescent state and try again.
118 * Release rcu_registry_lock, so rcu_(un)register_thread() doesn't
119 * wait too much time.
121 * rcu_register_thread() may add nodes to &registry; it will not
122 * wake up synchronize_rcu, but that is okay because at least another
123 * thread must exit its RCU read-side critical section before
124 * synchronize_rcu is done. The next iteration of the loop will
125 * move the new thread's rcu_reader from &registry to &qsreaders,
126 * because rcu_gp_ongoing() will return false.
128 * rcu_unregister_thread() may remove nodes from &qsreaders instead
129 * of &registry if it runs during qemu_event_wait. That's okay;
130 * the node then will not be added back to &registry by QLIST_SWAP
131 * below. The invariant is that the node is part of one list when
132 * rcu_registry_lock is released.
134 qemu_mutex_unlock(&rcu_registry_lock);
135 qemu_event_wait(&rcu_gp_event);
136 qemu_mutex_lock(&rcu_registry_lock);
139 /* put back the reader list in the registry */
140 QLIST_SWAP(&registry, &qsreaders, node);
143 void synchronize_rcu(void)
145 qemu_mutex_lock(&rcu_sync_lock);
147 /* Write RCU-protected pointers before reading p_rcu_reader->ctr.
148 * Pairs with smp_mb_placeholder() in rcu_read_lock().
150 smp_mb_global();
152 qemu_mutex_lock(&rcu_registry_lock);
153 if (!QLIST_EMPTY(&registry)) {
154 /* In either case, the atomic_mb_set below blocks stores that free
155 * old RCU-protected pointers.
157 if (sizeof(rcu_gp_ctr) < 8) {
158 /* For architectures with 32-bit longs, a two-subphases algorithm
159 * ensures we do not encounter overflow bugs.
161 * Switch parity: 0 -> 1, 1 -> 0.
163 atomic_mb_set(&rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR);
164 wait_for_readers();
165 atomic_mb_set(&rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR);
166 } else {
167 /* Increment current grace period. */
168 atomic_mb_set(&rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR);
171 wait_for_readers();
174 qemu_mutex_unlock(&rcu_registry_lock);
175 qemu_mutex_unlock(&rcu_sync_lock);
179 #define RCU_CALL_MIN_SIZE 30
181 /* Multi-producer, single-consumer queue based on urcu/static/wfqueue.h
182 * from liburcu. Note that head is only used by the consumer.
184 static struct rcu_head dummy;
185 static struct rcu_head *head = &dummy, **tail = &dummy.next;
186 static int rcu_call_count;
187 static QemuEvent rcu_call_ready_event;
189 static void enqueue(struct rcu_head *node)
191 struct rcu_head **old_tail;
193 node->next = NULL;
194 old_tail = atomic_xchg(&tail, &node->next);
195 atomic_mb_set(old_tail, node);
198 static struct rcu_head *try_dequeue(void)
200 struct rcu_head *node, *next;
202 retry:
203 /* Test for an empty list, which we do not expect. Note that for
204 * the consumer head and tail are always consistent. The head
205 * is consistent because only the consumer reads/writes it.
206 * The tail, because it is the first step in the enqueuing.
207 * It is only the next pointers that might be inconsistent.
209 if (head == &dummy && atomic_mb_read(&tail) == &dummy.next) {
210 abort();
213 /* If the head node has NULL in its next pointer, the value is
214 * wrong and we need to wait until its enqueuer finishes the update.
216 node = head;
217 next = atomic_mb_read(&head->next);
218 if (!next) {
219 return NULL;
222 /* Since we are the sole consumer, and we excluded the empty case
223 * above, the queue will always have at least two nodes: the
224 * dummy node, and the one being removed. So we do not need to update
225 * the tail pointer.
227 head = next;
229 /* If we dequeued the dummy node, add it back at the end and retry. */
230 if (node == &dummy) {
231 enqueue(node);
232 goto retry;
235 return node;
238 static void *call_rcu_thread(void *opaque)
240 struct rcu_head *node;
242 rcu_register_thread();
244 for (;;) {
245 int tries = 0;
246 int n = atomic_read(&rcu_call_count);
248 /* Heuristically wait for a decent number of callbacks to pile up.
249 * Fetch rcu_call_count now, we only must process elements that were
250 * added before synchronize_rcu() starts.
252 while (n == 0 || (n < RCU_CALL_MIN_SIZE && ++tries <= 5)) {
253 g_usleep(10000);
254 if (n == 0) {
255 qemu_event_reset(&rcu_call_ready_event);
256 n = atomic_read(&rcu_call_count);
257 if (n == 0) {
258 #if defined(CONFIG_MALLOC_TRIM)
259 malloc_trim(4 * 1024 * 1024);
260 #endif
261 qemu_event_wait(&rcu_call_ready_event);
264 n = atomic_read(&rcu_call_count);
267 atomic_sub(&rcu_call_count, n);
268 synchronize_rcu();
269 qemu_mutex_lock_iothread();
270 while (n > 0) {
271 node = try_dequeue();
272 while (!node) {
273 qemu_mutex_unlock_iothread();
274 qemu_event_reset(&rcu_call_ready_event);
275 node = try_dequeue();
276 if (!node) {
277 qemu_event_wait(&rcu_call_ready_event);
278 node = try_dequeue();
280 qemu_mutex_lock_iothread();
283 n--;
284 node->func(node);
286 qemu_mutex_unlock_iothread();
288 abort();
291 void call_rcu1(struct rcu_head *node, void (*func)(struct rcu_head *node))
293 node->func = func;
294 enqueue(node);
295 atomic_inc(&rcu_call_count);
296 qemu_event_set(&rcu_call_ready_event);
299 void rcu_register_thread(void)
301 assert(rcu_reader.ctr == 0);
302 qemu_mutex_lock(&rcu_registry_lock);
303 QLIST_INSERT_HEAD(&registry, &rcu_reader, node);
304 qemu_mutex_unlock(&rcu_registry_lock);
307 void rcu_unregister_thread(void)
309 qemu_mutex_lock(&rcu_registry_lock);
310 QLIST_REMOVE(&rcu_reader, node);
311 qemu_mutex_unlock(&rcu_registry_lock);
314 static void rcu_init_complete(void)
316 QemuThread thread;
318 qemu_mutex_init(&rcu_registry_lock);
319 qemu_mutex_init(&rcu_sync_lock);
320 qemu_event_init(&rcu_gp_event, true);
322 qemu_event_init(&rcu_call_ready_event, false);
324 /* The caller is assumed to have iothread lock, so the call_rcu thread
325 * must have been quiescent even after forking, just recreate it.
327 qemu_thread_create(&thread, "call_rcu", call_rcu_thread,
328 NULL, QEMU_THREAD_DETACHED);
330 rcu_register_thread();
333 static int atfork_depth = 1;
335 void rcu_enable_atfork(void)
337 atfork_depth++;
340 void rcu_disable_atfork(void)
342 atfork_depth--;
345 #ifdef CONFIG_POSIX
346 static void rcu_init_lock(void)
348 if (atfork_depth < 1) {
349 return;
352 qemu_mutex_lock(&rcu_sync_lock);
353 qemu_mutex_lock(&rcu_registry_lock);
356 static void rcu_init_unlock(void)
358 if (atfork_depth < 1) {
359 return;
362 qemu_mutex_unlock(&rcu_registry_lock);
363 qemu_mutex_unlock(&rcu_sync_lock);
366 static void rcu_init_child(void)
368 if (atfork_depth < 1) {
369 return;
372 memset(&registry, 0, sizeof(registry));
373 rcu_init_complete();
375 #endif
377 static void __attribute__((__constructor__)) rcu_init(void)
379 smp_mb_global_init();
380 #ifdef CONFIG_POSIX
381 pthread_atfork(rcu_init_lock, rcu_init_unlock, rcu_init_child);
382 #endif
383 rcu_init_complete();