]>
Commit | Line | Data |
---|---|---|
7911747b PB |
1 | /* |
2 | * urcu-mb.c | |
3 | * | |
4 | * Userspace RCU library with explicit memory barriers | |
5 | * | |
6 | * Copyright (c) 2009 Mathieu Desnoyers <[email protected]> | |
7 | * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. | |
8 | * Copyright 2015 Red Hat, Inc. | |
9 | * | |
10 | * Ported to QEMU by Paolo Bonzini <[email protected]> | |
11 | * | |
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. | |
16 | * | |
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. | |
21 | * | |
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 | |
25 | * | |
26 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. | |
27 | */ | |
28 | ||
aafd7584 | 29 | #include "qemu/osdep.h" |
7911747b PB |
30 | #include "qemu/rcu.h" |
31 | #include "qemu/atomic.h" | |
26387f86 | 32 | #include "qemu/thread.h" |
a4649824 | 33 | #include "qemu/main-loop.h" |
6e8a355d | 34 | #include "qemu/lockable.h" |
5a22ab71 YZ |
35 | #if defined(CONFIG_MALLOC_TRIM) |
36 | #include <malloc.h> | |
37 | #endif | |
7911747b PB |
38 | |
39 | /* | |
40 | * Global grace period counter. Bit 0 is always one in rcu_gp_ctr. | |
41 | * Bits 1 and above are defined in synchronize_rcu. | |
42 | */ | |
43 | #define RCU_GP_LOCKED (1UL << 0) | |
44 | #define RCU_GP_CTR (1UL << 1) | |
45 | ||
46 | unsigned long rcu_gp_ctr = RCU_GP_LOCKED; | |
47 | ||
48 | QemuEvent rcu_gp_event; | |
ef149763 | 49 | static int in_drain_call_rcu; |
c097a60b WC |
50 | static QemuMutex rcu_registry_lock; |
51 | static QemuMutex rcu_sync_lock; | |
7911747b PB |
52 | |
53 | /* | |
54 | * Check whether a quiescent state was crossed between the beginning of | |
55 | * update_counter_and_wait and now. | |
56 | */ | |
57 | static inline int rcu_gp_ongoing(unsigned long *ctr) | |
58 | { | |
59 | unsigned long v; | |
60 | ||
d73415a3 | 61 | v = qatomic_read(ctr); |
7911747b PB |
62 | return v && (v != rcu_gp_ctr); |
63 | } | |
64 | ||
65 | /* Written to only by each individual reader. Read by both the reader and the | |
66 | * writers. | |
67 | */ | |
17c78154 | 68 | QEMU_DEFINE_CO_TLS(struct rcu_reader_data, rcu_reader) |
7911747b | 69 | |
c097a60b | 70 | /* Protected by rcu_registry_lock. */ |
7911747b PB |
71 | typedef QLIST_HEAD(, rcu_reader_data) ThreadList; |
72 | static ThreadList registry = QLIST_HEAD_INITIALIZER(registry); | |
73 | ||
74 | /* Wait for previous parity/grace period to be empty of readers. */ | |
75 | static void wait_for_readers(void) | |
76 | { | |
77 | ThreadList qsreaders = QLIST_HEAD_INITIALIZER(qsreaders); | |
78 | struct rcu_reader_data *index, *tmp; | |
79 | ||
80 | for (;;) { | |
81 | /* We want to be notified of changes made to rcu_gp_ongoing | |
82 | * while we walk the list. | |
83 | */ | |
84 | qemu_event_reset(&rcu_gp_event); | |
85 | ||
d73415a3 SH |
86 | /* Instead of using qatomic_mb_set for index->waiting, and |
87 | * qatomic_mb_read for index->ctr, memory barriers are placed | |
7911747b | 88 | * manually since writes to different threads are independent. |
e11131b0 PB |
89 | * qemu_event_reset has acquire semantics, so no memory barrier |
90 | * is needed here. | |
7911747b | 91 | */ |
7911747b | 92 | QLIST_FOREACH(index, ®istry, node) { |
d73415a3 | 93 | qatomic_set(&index->waiting, true); |
7911747b PB |
94 | } |
95 | ||
77a8b846 | 96 | /* Here, order the stores to index->waiting before the loads of |
c8d3877e | 97 | * index->ctr. Pairs with smp_mb_placeholder() in rcu_read_unlock(), |
77a8b846 | 98 | * ensuring that the loads of index->ctr are sequentially consistent. |
e11131b0 | 99 | */ |
c8d3877e | 100 | smp_mb_global(); |
7911747b PB |
101 | |
102 | QLIST_FOREACH_SAFE(index, ®istry, node, tmp) { | |
103 | if (!rcu_gp_ongoing(&index->ctr)) { | |
104 | QLIST_REMOVE(index, node); | |
105 | QLIST_INSERT_HEAD(&qsreaders, index, node); | |
106 | ||
107 | /* No need for mb_set here, worst of all we | |
108 | * get some extra futex wakeups. | |
109 | */ | |
d73415a3 | 110 | qatomic_set(&index->waiting, false); |
ef149763 GK |
111 | } else if (qatomic_read(&in_drain_call_rcu)) { |
112 | notifier_list_notify(&index->force_rcu, NULL); | |
7911747b PB |
113 | } |
114 | } | |
115 | ||
7911747b PB |
116 | if (QLIST_EMPTY(®istry)) { |
117 | break; | |
118 | } | |
119 | ||
c097a60b WC |
120 | /* Wait for one thread to report a quiescent state and try again. |
121 | * Release rcu_registry_lock, so rcu_(un)register_thread() doesn't | |
122 | * wait too much time. | |
123 | * | |
124 | * rcu_register_thread() may add nodes to ®istry; it will not | |
125 | * wake up synchronize_rcu, but that is okay because at least another | |
126 | * thread must exit its RCU read-side critical section before | |
127 | * synchronize_rcu is done. The next iteration of the loop will | |
128 | * move the new thread's rcu_reader from ®istry to &qsreaders, | |
129 | * because rcu_gp_ongoing() will return false. | |
130 | * | |
131 | * rcu_unregister_thread() may remove nodes from &qsreaders instead | |
132 | * of ®istry if it runs during qemu_event_wait. That's okay; | |
133 | * the node then will not be added back to ®istry by QLIST_SWAP | |
134 | * below. The invariant is that the node is part of one list when | |
135 | * rcu_registry_lock is released. | |
7911747b | 136 | */ |
c097a60b | 137 | qemu_mutex_unlock(&rcu_registry_lock); |
7911747b | 138 | qemu_event_wait(&rcu_gp_event); |
c097a60b | 139 | qemu_mutex_lock(&rcu_registry_lock); |
7911747b PB |
140 | } |
141 | ||
142 | /* put back the reader list in the registry */ | |
143 | QLIST_SWAP(®istry, &qsreaders, node); | |
144 | } | |
145 | ||
146 | void synchronize_rcu(void) | |
147 | { | |
6e8a355d | 148 | QEMU_LOCK_GUARD(&rcu_sync_lock); |
7911747b | 149 | |
77a8b846 | 150 | /* Write RCU-protected pointers before reading p_rcu_reader->ctr. |
c8d3877e | 151 | * Pairs with smp_mb_placeholder() in rcu_read_lock(). |
77a8b846 | 152 | */ |
c8d3877e | 153 | smp_mb_global(); |
77a8b846 | 154 | |
6e8a355d | 155 | QEMU_LOCK_GUARD(&rcu_registry_lock); |
7911747b | 156 | if (!QLIST_EMPTY(®istry)) { |
d73415a3 | 157 | /* In either case, the qatomic_mb_set below blocks stores that free |
7911747b PB |
158 | * old RCU-protected pointers. |
159 | */ | |
160 | if (sizeof(rcu_gp_ctr) < 8) { | |
161 | /* For architectures with 32-bit longs, a two-subphases algorithm | |
162 | * ensures we do not encounter overflow bugs. | |
163 | * | |
164 | * Switch parity: 0 -> 1, 1 -> 0. | |
165 | */ | |
d73415a3 | 166 | qatomic_mb_set(&rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR); |
7911747b | 167 | wait_for_readers(); |
d73415a3 | 168 | qatomic_mb_set(&rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR); |
7911747b PB |
169 | } else { |
170 | /* Increment current grace period. */ | |
d73415a3 | 171 | qatomic_mb_set(&rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR); |
7911747b PB |
172 | } |
173 | ||
174 | wait_for_readers(); | |
175 | } | |
7911747b PB |
176 | } |
177 | ||
26387f86 PB |
178 | |
179 | #define RCU_CALL_MIN_SIZE 30 | |
180 | ||
181 | /* Multi-producer, single-consumer queue based on urcu/static/wfqueue.h | |
182 | * from liburcu. Note that head is only used by the consumer. | |
183 | */ | |
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; | |
188 | ||
189 | static void enqueue(struct rcu_head *node) | |
190 | { | |
191 | struct rcu_head **old_tail; | |
192 | ||
193 | node->next = NULL; | |
d73415a3 SH |
194 | old_tail = qatomic_xchg(&tail, &node->next); |
195 | qatomic_mb_set(old_tail, node); | |
26387f86 PB |
196 | } |
197 | ||
198 | static struct rcu_head *try_dequeue(void) | |
199 | { | |
200 | struct rcu_head *node, *next; | |
201 | ||
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. | |
208 | */ | |
d73415a3 | 209 | if (head == &dummy && qatomic_mb_read(&tail) == &dummy.next) { |
26387f86 PB |
210 | abort(); |
211 | } | |
212 | ||
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. | |
215 | */ | |
216 | node = head; | |
d73415a3 | 217 | next = qatomic_mb_read(&head->next); |
26387f86 PB |
218 | if (!next) { |
219 | return NULL; | |
220 | } | |
221 | ||
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. | |
226 | */ | |
227 | head = next; | |
228 | ||
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; | |
233 | } | |
234 | ||
235 | return node; | |
236 | } | |
237 | ||
238 | static void *call_rcu_thread(void *opaque) | |
239 | { | |
240 | struct rcu_head *node; | |
241 | ||
ab28bd23 PB |
242 | rcu_register_thread(); |
243 | ||
26387f86 PB |
244 | for (;;) { |
245 | int tries = 0; | |
d73415a3 | 246 | int n = qatomic_read(&rcu_call_count); |
26387f86 PB |
247 | |
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. | |
251 | */ | |
a7d1d636 PB |
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); | |
d73415a3 | 256 | n = qatomic_read(&rcu_call_count); |
a7d1d636 | 257 | if (n == 0) { |
5a22ab71 YZ |
258 | #if defined(CONFIG_MALLOC_TRIM) |
259 | malloc_trim(4 * 1024 * 1024); | |
260 | #endif | |
a7d1d636 PB |
261 | qemu_event_wait(&rcu_call_ready_event); |
262 | } | |
26387f86 | 263 | } |
d73415a3 | 264 | n = qatomic_read(&rcu_call_count); |
26387f86 PB |
265 | } |
266 | ||
d73415a3 | 267 | qatomic_sub(&rcu_call_count, n); |
26387f86 | 268 | synchronize_rcu(); |
a4649824 | 269 | qemu_mutex_lock_iothread(); |
26387f86 PB |
270 | while (n > 0) { |
271 | node = try_dequeue(); | |
272 | while (!node) { | |
a4649824 | 273 | qemu_mutex_unlock_iothread(); |
26387f86 PB |
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(); | |
279 | } | |
a4649824 | 280 | qemu_mutex_lock_iothread(); |
26387f86 PB |
281 | } |
282 | ||
283 | n--; | |
284 | node->func(node); | |
285 | } | |
a4649824 | 286 | qemu_mutex_unlock_iothread(); |
26387f86 PB |
287 | } |
288 | abort(); | |
289 | } | |
290 | ||
291 | void call_rcu1(struct rcu_head *node, void (*func)(struct rcu_head *node)) | |
292 | { | |
293 | node->func = func; | |
294 | enqueue(node); | |
d73415a3 | 295 | qatomic_inc(&rcu_call_count); |
26387f86 PB |
296 | qemu_event_set(&rcu_call_ready_event); |
297 | } | |
298 | ||
d816614c ML |
299 | |
300 | struct rcu_drain { | |
301 | struct rcu_head rcu; | |
302 | QemuEvent drain_complete_event; | |
303 | }; | |
304 | ||
305 | static void drain_rcu_callback(struct rcu_head *node) | |
306 | { | |
307 | struct rcu_drain *event = (struct rcu_drain *)node; | |
308 | qemu_event_set(&event->drain_complete_event); | |
309 | } | |
310 | ||
311 | /* | |
312 | * This function ensures that all pending RCU callbacks | |
313 | * on the current thread are done executing | |
314 | ||
315 | * drops big qemu lock during the wait to allow RCU thread | |
316 | * to process the callbacks | |
317 | * | |
318 | */ | |
319 | ||
320 | void drain_call_rcu(void) | |
321 | { | |
322 | struct rcu_drain rcu_drain; | |
323 | bool locked = qemu_mutex_iothread_locked(); | |
324 | ||
325 | memset(&rcu_drain, 0, sizeof(struct rcu_drain)); | |
326 | qemu_event_init(&rcu_drain.drain_complete_event, false); | |
327 | ||
328 | if (locked) { | |
329 | qemu_mutex_unlock_iothread(); | |
330 | } | |
331 | ||
332 | ||
333 | /* | |
334 | * RCU callbacks are invoked in the same order as in which they | |
335 | * are registered, thus we can be sure that when 'drain_rcu_callback' | |
336 | * is called, all RCU callbacks that were registered on this thread | |
337 | * prior to calling this function are completed. | |
338 | * | |
339 | * Note that since we have only one global queue of the RCU callbacks, | |
340 | * we also end up waiting for most of RCU callbacks that were registered | |
341 | * on the other threads, but this is a side effect that shoudn't be | |
342 | * assumed. | |
343 | */ | |
344 | ||
ef149763 | 345 | qatomic_inc(&in_drain_call_rcu); |
d816614c ML |
346 | call_rcu1(&rcu_drain.rcu, drain_rcu_callback); |
347 | qemu_event_wait(&rcu_drain.drain_complete_event); | |
ef149763 | 348 | qatomic_dec(&in_drain_call_rcu); |
d816614c ML |
349 | |
350 | if (locked) { | |
351 | qemu_mutex_lock_iothread(); | |
352 | } | |
353 | ||
354 | } | |
355 | ||
7911747b PB |
356 | void rcu_register_thread(void) |
357 | { | |
17c78154 | 358 | assert(get_ptr_rcu_reader()->ctr == 0); |
c097a60b | 359 | qemu_mutex_lock(&rcu_registry_lock); |
17c78154 | 360 | QLIST_INSERT_HEAD(®istry, get_ptr_rcu_reader(), node); |
c097a60b | 361 | qemu_mutex_unlock(&rcu_registry_lock); |
7911747b PB |
362 | } |
363 | ||
364 | void rcu_unregister_thread(void) | |
365 | { | |
c097a60b | 366 | qemu_mutex_lock(&rcu_registry_lock); |
17c78154 | 367 | QLIST_REMOVE(get_ptr_rcu_reader(), node); |
c097a60b | 368 | qemu_mutex_unlock(&rcu_registry_lock); |
7911747b PB |
369 | } |
370 | ||
ef149763 GK |
371 | void rcu_add_force_rcu_notifier(Notifier *n) |
372 | { | |
373 | qemu_mutex_lock(&rcu_registry_lock); | |
17c78154 | 374 | notifier_list_add(&get_ptr_rcu_reader()->force_rcu, n); |
ef149763 GK |
375 | qemu_mutex_unlock(&rcu_registry_lock); |
376 | } | |
377 | ||
378 | void rcu_remove_force_rcu_notifier(Notifier *n) | |
379 | { | |
380 | qemu_mutex_lock(&rcu_registry_lock); | |
381 | notifier_remove(n); | |
382 | qemu_mutex_unlock(&rcu_registry_lock); | |
383 | } | |
384 | ||
21b7cf9e | 385 | static void rcu_init_complete(void) |
7911747b | 386 | { |
26387f86 PB |
387 | QemuThread thread; |
388 | ||
c097a60b WC |
389 | qemu_mutex_init(&rcu_registry_lock); |
390 | qemu_mutex_init(&rcu_sync_lock); | |
7911747b | 391 | qemu_event_init(&rcu_gp_event, true); |
26387f86 PB |
392 | |
393 | qemu_event_init(&rcu_call_ready_event, false); | |
21b7cf9e PB |
394 | |
395 | /* The caller is assumed to have iothread lock, so the call_rcu thread | |
396 | * must have been quiescent even after forking, just recreate it. | |
397 | */ | |
26387f86 PB |
398 | qemu_thread_create(&thread, "call_rcu", call_rcu_thread, |
399 | NULL, QEMU_THREAD_DETACHED); | |
400 | ||
7911747b PB |
401 | rcu_register_thread(); |
402 | } | |
21b7cf9e | 403 | |
73c6e401 PB |
404 | static int atfork_depth = 1; |
405 | ||
406 | void rcu_enable_atfork(void) | |
407 | { | |
408 | atfork_depth++; | |
409 | } | |
410 | ||
411 | void rcu_disable_atfork(void) | |
412 | { | |
413 | atfork_depth--; | |
414 | } | |
415 | ||
21b7cf9e PB |
416 | #ifdef CONFIG_POSIX |
417 | static void rcu_init_lock(void) | |
418 | { | |
73c6e401 PB |
419 | if (atfork_depth < 1) { |
420 | return; | |
421 | } | |
422 | ||
c097a60b WC |
423 | qemu_mutex_lock(&rcu_sync_lock); |
424 | qemu_mutex_lock(&rcu_registry_lock); | |
21b7cf9e PB |
425 | } |
426 | ||
427 | static void rcu_init_unlock(void) | |
428 | { | |
73c6e401 PB |
429 | if (atfork_depth < 1) { |
430 | return; | |
431 | } | |
432 | ||
c097a60b WC |
433 | qemu_mutex_unlock(&rcu_registry_lock); |
434 | qemu_mutex_unlock(&rcu_sync_lock); | |
21b7cf9e PB |
435 | } |
436 | ||
2a96a552 | 437 | static void rcu_init_child(void) |
21b7cf9e | 438 | { |
2a96a552 PB |
439 | if (atfork_depth < 1) { |
440 | return; | |
441 | } | |
442 | ||
21b7cf9e PB |
443 | memset(®istry, 0, sizeof(registry)); |
444 | rcu_init_complete(); | |
445 | } | |
2a96a552 | 446 | #endif |
21b7cf9e PB |
447 | |
448 | static void __attribute__((__constructor__)) rcu_init(void) | |
449 | { | |
c8d3877e | 450 | smp_mb_global_init(); |
21b7cf9e | 451 | #ifdef CONFIG_POSIX |
2a96a552 | 452 | pthread_atfork(rcu_init_lock, rcu_init_unlock, rcu_init_child); |
21b7cf9e PB |
453 | #endif |
454 | rcu_init_complete(); | |
455 | } |