libvhost-user: Factor out adding a memory region
[qemu/ar7.git] / util / rcu.c
blobfa32c942e4bb98b4b69da6bc28213036a291002d
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/rcu.h"
31 #include "qemu/atomic.h"
32 #include "qemu/thread.h"
33 #include "qemu/main-loop.h"
34 #include "qemu/lockable.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 int in_drain_call_rcu;
50 static QemuMutex rcu_registry_lock;
51 static QemuMutex rcu_sync_lock;
54 * Check whether a quiescent state was crossed between the beginning of
55 * update_counter_and_wait and now.
57 static inline int rcu_gp_ongoing(unsigned long *ctr)
59 unsigned long v;
61 v = qatomic_read(ctr);
62 return v && (v != rcu_gp_ctr);
65 /* Written to only by each individual reader. Read by both the reader and the
66 * writers.
68 QEMU_DEFINE_CO_TLS(struct rcu_reader_data, rcu_reader)
70 /* Protected by rcu_registry_lock. */
71 typedef QLIST_HEAD(, rcu_reader_data) ThreadList;
72 static ThreadList registry = QLIST_HEAD_INITIALIZER(registry);
74 /* Wait for previous parity/grace period to be empty of readers. */
75 static void wait_for_readers(void)
77 ThreadList qsreaders = QLIST_HEAD_INITIALIZER(qsreaders);
78 struct rcu_reader_data *index, *tmp;
80 for (;;) {
81 /* We want to be notified of changes made to rcu_gp_ongoing
82 * while we walk the list.
84 qemu_event_reset(&rcu_gp_event);
86 QLIST_FOREACH(index, &registry, node) {
87 qatomic_set(&index->waiting, true);
90 /* Here, order the stores to index->waiting before the loads of
91 * index->ctr. Pairs with smp_mb_placeholder() in rcu_read_unlock(),
92 * ensuring that the loads of index->ctr are sequentially consistent.
94 * If this is the last iteration, this barrier also prevents
95 * frees from seeping upwards, and orders the two wait phases
96 * on architectures with 32-bit longs; see synchronize_rcu().
98 smp_mb_global();
100 QLIST_FOREACH_SAFE(index, &registry, node, tmp) {
101 if (!rcu_gp_ongoing(&index->ctr)) {
102 QLIST_REMOVE(index, node);
103 QLIST_INSERT_HEAD(&qsreaders, index, node);
105 /* No need for memory barriers here, worst of all we
106 * get some extra futex wakeups.
108 qatomic_set(&index->waiting, false);
109 } else if (qatomic_read(&in_drain_call_rcu)) {
110 notifier_list_notify(&index->force_rcu, NULL);
114 if (QLIST_EMPTY(&registry)) {
115 break;
118 /* Wait for one thread to report a quiescent state and try again.
119 * Release rcu_registry_lock, so rcu_(un)register_thread() doesn't
120 * wait too much time.
122 * rcu_register_thread() may add nodes to &registry; it will not
123 * wake up synchronize_rcu, but that is okay because at least another
124 * thread must exit its RCU read-side critical section before
125 * synchronize_rcu is done. The next iteration of the loop will
126 * move the new thread's rcu_reader from &registry to &qsreaders,
127 * because rcu_gp_ongoing() will return false.
129 * rcu_unregister_thread() may remove nodes from &qsreaders instead
130 * of &registry if it runs during qemu_event_wait. That's okay;
131 * the node then will not be added back to &registry by QLIST_SWAP
132 * below. The invariant is that the node is part of one list when
133 * rcu_registry_lock is released.
135 qemu_mutex_unlock(&rcu_registry_lock);
136 qemu_event_wait(&rcu_gp_event);
137 qemu_mutex_lock(&rcu_registry_lock);
140 /* put back the reader list in the registry */
141 QLIST_SWAP(&registry, &qsreaders, node);
144 void synchronize_rcu(void)
146 QEMU_LOCK_GUARD(&rcu_sync_lock);
148 /* Write RCU-protected pointers before reading p_rcu_reader->ctr.
149 * Pairs with smp_mb_placeholder() in rcu_read_lock().
151 * Also orders write to RCU-protected pointers before
152 * write to rcu_gp_ctr.
154 smp_mb_global();
156 QEMU_LOCK_GUARD(&rcu_registry_lock);
157 if (!QLIST_EMPTY(&registry)) {
158 if (sizeof(rcu_gp_ctr) < 8) {
159 /* For architectures with 32-bit longs, a two-subphases algorithm
160 * ensures we do not encounter overflow bugs.
162 * Switch parity: 0 -> 1, 1 -> 0.
164 qatomic_set(&rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR);
165 wait_for_readers();
166 qatomic_set(&rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR);
167 } else {
168 /* Increment current grace period. */
169 qatomic_set(&rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR);
172 wait_for_readers();
177 #define RCU_CALL_MIN_SIZE 30
179 /* Multi-producer, single-consumer queue based on urcu/static/wfqueue.h
180 * from liburcu. Note that head is only used by the consumer.
182 static struct rcu_head dummy;
183 static struct rcu_head *head = &dummy, **tail = &dummy.next;
184 static int rcu_call_count;
185 static QemuEvent rcu_call_ready_event;
187 static void enqueue(struct rcu_head *node)
189 struct rcu_head **old_tail;
191 node->next = NULL;
194 * Make this node the tail of the list. The node will be
195 * used by further enqueue operations, but it will not
196 * be dequeued yet...
198 old_tail = qatomic_xchg(&tail, &node->next);
201 * ... until it is pointed to from another item in the list.
202 * In the meantime, try_dequeue() will find a NULL next pointer
203 * and loop.
205 * Synchronizes with qatomic_load_acquire() in try_dequeue().
207 qatomic_store_release(old_tail, node);
210 static struct rcu_head *try_dequeue(void)
212 struct rcu_head *node, *next;
214 retry:
215 /* Head is only written by this thread, so no need for barriers. */
216 node = head;
219 * If the head node has NULL in its next pointer, the value is
220 * wrong and we need to wait until its enqueuer finishes the update.
222 next = qatomic_load_acquire(&node->next);
223 if (!next) {
224 return NULL;
228 * Test for an empty list, which we do not expect. Note that for
229 * the consumer head and tail are always consistent. The head
230 * is consistent because only the consumer reads/writes it.
231 * The tail, because it is the first step in the enqueuing.
232 * It is only the next pointers that might be inconsistent.
234 if (head == &dummy && qatomic_read(&tail) == &dummy.next) {
235 abort();
239 * Since we are the sole consumer, and we excluded the empty case
240 * above, the queue will always have at least two nodes: the
241 * dummy node, and the one being removed. So we do not need to update
242 * the tail pointer.
244 head = next;
246 /* If we dequeued the dummy node, add it back at the end and retry. */
247 if (node == &dummy) {
248 enqueue(node);
249 goto retry;
252 return node;
255 static void *call_rcu_thread(void *opaque)
257 struct rcu_head *node;
259 rcu_register_thread();
261 for (;;) {
262 int tries = 0;
263 int n = qatomic_read(&rcu_call_count);
265 /* Heuristically wait for a decent number of callbacks to pile up.
266 * Fetch rcu_call_count now, we only must process elements that were
267 * added before synchronize_rcu() starts.
269 while (n == 0 || (n < RCU_CALL_MIN_SIZE && ++tries <= 5)) {
270 g_usleep(10000);
271 if (n == 0) {
272 qemu_event_reset(&rcu_call_ready_event);
273 n = qatomic_read(&rcu_call_count);
274 if (n == 0) {
275 #if defined(CONFIG_MALLOC_TRIM)
276 malloc_trim(4 * 1024 * 1024);
277 #endif
278 qemu_event_wait(&rcu_call_ready_event);
281 n = qatomic_read(&rcu_call_count);
284 qatomic_sub(&rcu_call_count, n);
285 synchronize_rcu();
286 bql_lock();
287 while (n > 0) {
288 node = try_dequeue();
289 while (!node) {
290 bql_unlock();
291 qemu_event_reset(&rcu_call_ready_event);
292 node = try_dequeue();
293 if (!node) {
294 qemu_event_wait(&rcu_call_ready_event);
295 node = try_dequeue();
297 bql_lock();
300 n--;
301 node->func(node);
303 bql_unlock();
305 abort();
308 void call_rcu1(struct rcu_head *node, void (*func)(struct rcu_head *node))
310 node->func = func;
311 enqueue(node);
312 qatomic_inc(&rcu_call_count);
313 qemu_event_set(&rcu_call_ready_event);
317 struct rcu_drain {
318 struct rcu_head rcu;
319 QemuEvent drain_complete_event;
322 static void drain_rcu_callback(struct rcu_head *node)
324 struct rcu_drain *event = (struct rcu_drain *)node;
325 qemu_event_set(&event->drain_complete_event);
329 * This function ensures that all pending RCU callbacks
330 * on the current thread are done executing
332 * drops big qemu lock during the wait to allow RCU thread
333 * to process the callbacks
337 void drain_call_rcu(void)
339 struct rcu_drain rcu_drain;
340 bool locked = bql_locked();
342 memset(&rcu_drain, 0, sizeof(struct rcu_drain));
343 qemu_event_init(&rcu_drain.drain_complete_event, false);
345 if (locked) {
346 bql_unlock();
351 * RCU callbacks are invoked in the same order as in which they
352 * are registered, thus we can be sure that when 'drain_rcu_callback'
353 * is called, all RCU callbacks that were registered on this thread
354 * prior to calling this function are completed.
356 * Note that since we have only one global queue of the RCU callbacks,
357 * we also end up waiting for most of RCU callbacks that were registered
358 * on the other threads, but this is a side effect that shouldn't be
359 * assumed.
362 qatomic_inc(&in_drain_call_rcu);
363 call_rcu1(&rcu_drain.rcu, drain_rcu_callback);
364 qemu_event_wait(&rcu_drain.drain_complete_event);
365 qatomic_dec(&in_drain_call_rcu);
367 if (locked) {
368 bql_lock();
373 void rcu_register_thread(void)
375 assert(get_ptr_rcu_reader()->ctr == 0);
376 qemu_mutex_lock(&rcu_registry_lock);
377 QLIST_INSERT_HEAD(&registry, get_ptr_rcu_reader(), node);
378 qemu_mutex_unlock(&rcu_registry_lock);
381 void rcu_unregister_thread(void)
383 qemu_mutex_lock(&rcu_registry_lock);
384 QLIST_REMOVE(get_ptr_rcu_reader(), node);
385 qemu_mutex_unlock(&rcu_registry_lock);
388 void rcu_add_force_rcu_notifier(Notifier *n)
390 qemu_mutex_lock(&rcu_registry_lock);
391 notifier_list_add(&get_ptr_rcu_reader()->force_rcu, n);
392 qemu_mutex_unlock(&rcu_registry_lock);
395 void rcu_remove_force_rcu_notifier(Notifier *n)
397 qemu_mutex_lock(&rcu_registry_lock);
398 notifier_remove(n);
399 qemu_mutex_unlock(&rcu_registry_lock);
402 static void rcu_init_complete(void)
404 QemuThread thread;
406 qemu_mutex_init(&rcu_registry_lock);
407 qemu_mutex_init(&rcu_sync_lock);
408 qemu_event_init(&rcu_gp_event, true);
410 qemu_event_init(&rcu_call_ready_event, false);
412 /* The caller is assumed to have BQL, so the call_rcu thread
413 * must have been quiescent even after forking, just recreate it.
415 qemu_thread_create(&thread, "call_rcu", call_rcu_thread,
416 NULL, QEMU_THREAD_DETACHED);
418 rcu_register_thread();
421 static int atfork_depth = 1;
423 void rcu_enable_atfork(void)
425 atfork_depth++;
428 void rcu_disable_atfork(void)
430 atfork_depth--;
433 #ifdef CONFIG_POSIX
434 static void rcu_init_lock(void)
436 if (atfork_depth < 1) {
437 return;
440 qemu_mutex_lock(&rcu_sync_lock);
441 qemu_mutex_lock(&rcu_registry_lock);
444 static void rcu_init_unlock(void)
446 if (atfork_depth < 1) {
447 return;
450 qemu_mutex_unlock(&rcu_registry_lock);
451 qemu_mutex_unlock(&rcu_sync_lock);
454 static void rcu_init_child(void)
456 if (atfork_depth < 1) {
457 return;
460 memset(&registry, 0, sizeof(registry));
461 rcu_init_complete();
463 #endif
465 static void __attribute__((__constructor__)) rcu_init(void)
467 smp_mb_global_init();
468 #ifdef CONFIG_POSIX
469 pthread_atfork(rcu_init_lock, rcu_init_unlock, rcu_init_child);
470 #endif
471 rcu_init_complete();