| 1 | /* |
| 2 | * urcu-mb.c |
| 3 | * |
| 4 | * Userspace RCU library with explicit memory barriers |
| 5 | * |
| 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. |
| 9 | * |
| 10 | * Ported to QEMU by Paolo Bonzini <pbonzini@redhat.com> |
| 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, see |
| 24 | * <https://www.gnu.org/licenses/>. |
| 25 | * |
| 26 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. |
| 27 | */ |
| 28 | |
| 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 |
| 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 | |
| 47 | #define RCU_CALL_MIN_SIZE 30 |
| 48 | |
| 49 | unsigned long rcu_gp_ctr = RCU_GP_LOCKED; |
| 50 | |
| 51 | QemuEvent rcu_gp_event; |
| 52 | static int in_drain_call_rcu; |
| 53 | static int rcu_call_count; |
| 54 | static QemuMutex rcu_registry_lock; |
| 55 | static QemuMutex rcu_sync_lock; |
| 56 | |
| 57 | /* |
| 58 | * Check whether a quiescent state was crossed between the beginning of |
| 59 | * update_counter_and_wait and now. |
| 60 | */ |
| 61 | static inline int rcu_gp_ongoing(unsigned long *ctr) |
| 62 | { |
| 63 | unsigned long v; |
| 64 | |
| 65 | v = qatomic_read(ctr); |
| 66 | return v && (v != rcu_gp_ctr); |
| 67 | } |
| 68 | |
| 69 | /* Written to only by each individual reader. Read by both the reader and the |
| 70 | * writers. |
| 71 | */ |
| 72 | QEMU_DEFINE_CO_TLS(struct rcu_reader_data, rcu_reader) |
| 73 | |
| 74 | /* Protected by rcu_registry_lock. */ |
| 75 | typedef QLIST_HEAD(, rcu_reader_data) ThreadList; |
| 76 | static ThreadList registry = QLIST_HEAD_INITIALIZER(registry); |
| 77 | |
| 78 | /* Wait for previous parity/grace period to be empty of readers. */ |
| 79 | static void wait_for_readers(void) |
| 80 | { |
| 81 | ThreadList qsreaders = QLIST_HEAD_INITIALIZER(qsreaders); |
| 82 | struct rcu_reader_data *index, *tmp; |
| 83 | int sleeps = 0; |
| 84 | bool forced = false; |
| 85 | |
| 86 | for (;;) { |
| 87 | /* |
| 88 | * Force the grace period to end and wait for it if any of the |
| 89 | * following heuristical conditions are satisfied: |
| 90 | * - A decent number of callbacks piled up. |
| 91 | * - It timed out. |
| 92 | * - It is in a drain_call_rcu() call. |
| 93 | * |
| 94 | * Otherwise, periodically poll the grace period, hoping it ends |
| 95 | * promptly. |
| 96 | */ |
| 97 | if (!forced && |
| 98 | (qatomic_read(&rcu_call_count) >= RCU_CALL_MIN_SIZE || |
| 99 | sleeps >= 5 || qatomic_read(&in_drain_call_rcu))) { |
| 100 | forced = true; |
| 101 | |
| 102 | QLIST_FOREACH(index, ®istry, node) { |
| 103 | notifier_list_notify(&index->force_rcu, NULL); |
| 104 | qatomic_set(&index->waiting, true); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* Here, order the stores to index->waiting before the loads of |
| 109 | * index->ctr. Pairs with smp_mb_placeholder() in rcu_read_unlock(), |
| 110 | * ensuring that the loads of index->ctr are sequentially consistent. |
| 111 | * |
| 112 | * If this is the last iteration, this barrier also prevents |
| 113 | * frees from seeping upwards, and orders the two wait phases |
| 114 | * on architectures with 32-bit longs; see synchronize_rcu(). |
| 115 | */ |
| 116 | smp_mb_global(); |
| 117 | |
| 118 | QLIST_FOREACH_SAFE(index, ®istry, node, tmp) { |
| 119 | if (!rcu_gp_ongoing(&index->ctr)) { |
| 120 | QLIST_REMOVE(index, node); |
| 121 | QLIST_INSERT_HEAD(&qsreaders, index, node); |
| 122 | |
| 123 | /* No need for memory barriers here, worst of all we |
| 124 | * get some extra futex wakeups. |
| 125 | */ |
| 126 | qatomic_set(&index->waiting, false); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (QLIST_EMPTY(®istry)) { |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Sleep for a while and try again. |
| 136 | * Release rcu_registry_lock, so rcu_(un)register_thread() doesn't |
| 137 | * wait too much time. |
| 138 | * |
| 139 | * rcu_register_thread() may add nodes to ®istry; it will not |
| 140 | * wake up synchronize_rcu, but that is okay because at least another |
| 141 | * thread must exit its RCU read-side critical section before |
| 142 | * synchronize_rcu is done. The next iteration of the loop will |
| 143 | * move the new thread's rcu_reader from ®istry to &qsreaders, |
| 144 | * because rcu_gp_ongoing() will return false. |
| 145 | * |
| 146 | * rcu_unregister_thread() may remove nodes from &qsreaders instead |
| 147 | * of ®istry if it runs during qemu_event_wait. That's okay; |
| 148 | * the node then will not be added back to ®istry by QLIST_SWAP |
| 149 | * below. The invariant is that the node is part of one list when |
| 150 | * rcu_registry_lock is released. |
| 151 | */ |
| 152 | qemu_mutex_unlock(&rcu_registry_lock); |
| 153 | |
| 154 | if (forced) { |
| 155 | qemu_event_wait(&rcu_gp_event); |
| 156 | |
| 157 | /* |
| 158 | * We want to be notified of changes made to rcu_gp_ongoing |
| 159 | * while we walk the list. |
| 160 | */ |
| 161 | qemu_event_reset(&rcu_gp_event); |
| 162 | } else { |
| 163 | g_usleep(10000); |
| 164 | sleeps++; |
| 165 | } |
| 166 | |
| 167 | qemu_mutex_lock(&rcu_registry_lock); |
| 168 | } |
| 169 | |
| 170 | /* put back the reader list in the registry */ |
| 171 | QLIST_SWAP(®istry, &qsreaders, node); |
| 172 | } |
| 173 | |
| 174 | void synchronize_rcu(void) |
| 175 | { |
| 176 | QEMU_LOCK_GUARD(&rcu_sync_lock); |
| 177 | |
| 178 | /* Write RCU-protected pointers before reading p_rcu_reader->ctr. |
| 179 | * Pairs with smp_mb_placeholder() in rcu_read_lock(). |
| 180 | * |
| 181 | * Also orders write to RCU-protected pointers before |
| 182 | * write to rcu_gp_ctr. |
| 183 | */ |
| 184 | smp_mb_global(); |
| 185 | |
| 186 | QEMU_LOCK_GUARD(&rcu_registry_lock); |
| 187 | if (!QLIST_EMPTY(®istry)) { |
| 188 | if (sizeof(rcu_gp_ctr) < 8) { |
| 189 | /* For architectures with 32-bit longs, a two-subphases algorithm |
| 190 | * ensures we do not encounter overflow bugs. |
| 191 | * |
| 192 | * Switch parity: 0 -> 1, 1 -> 0. |
| 193 | */ |
| 194 | qatomic_set(&rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR); |
| 195 | wait_for_readers(); |
| 196 | qatomic_set(&rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR); |
| 197 | } else { |
| 198 | /* Increment current grace period. */ |
| 199 | qatomic_set(&rcu_gp_ctr, rcu_gp_ctr + RCU_GP_CTR); |
| 200 | } |
| 201 | |
| 202 | wait_for_readers(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /* Multi-producer, single-consumer queue based on urcu/static/wfqueue.h |
| 207 | * from liburcu. Note that head is only used by the consumer. |
| 208 | */ |
| 209 | static struct rcu_head dummy; |
| 210 | static struct rcu_head *head = &dummy, **tail = &dummy.next; |
| 211 | static QemuEvent rcu_call_ready_event; |
| 212 | |
| 213 | static void enqueue(struct rcu_head *node) |
| 214 | { |
| 215 | struct rcu_head **old_tail; |
| 216 | |
| 217 | node->next = NULL; |
| 218 | |
| 219 | /* |
| 220 | * Make this node the tail of the list. The node will be |
| 221 | * used by further enqueue operations, but it will not |
| 222 | * be dequeued yet... |
| 223 | */ |
| 224 | old_tail = qatomic_xchg(&tail, &node->next); |
| 225 | |
| 226 | /* |
| 227 | * ... until it is pointed to from another item in the list. |
| 228 | * In the meantime, try_dequeue() will find a NULL next pointer |
| 229 | * and loop. |
| 230 | * |
| 231 | * Synchronizes with qatomic_load_acquire() in try_dequeue(). |
| 232 | */ |
| 233 | qatomic_store_release(old_tail, node); |
| 234 | } |
| 235 | |
| 236 | static struct rcu_head *try_dequeue(void) |
| 237 | { |
| 238 | struct rcu_head *node, *next; |
| 239 | |
| 240 | retry: |
| 241 | /* Head is only written by this thread, so no need for barriers. */ |
| 242 | node = head; |
| 243 | |
| 244 | /* |
| 245 | * If the head node has NULL in its next pointer, the value is |
| 246 | * wrong and we need to wait until its enqueuer finishes the update. |
| 247 | */ |
| 248 | next = qatomic_load_acquire(&node->next); |
| 249 | if (!next) { |
| 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Test for an empty list, which we do not expect. Note that for |
| 255 | * the consumer head and tail are always consistent. The head |
| 256 | * is consistent because only the consumer reads/writes it. |
| 257 | * The tail, because it is the first step in the enqueuing. |
| 258 | * It is only the next pointers that might be inconsistent. |
| 259 | */ |
| 260 | if (head == &dummy && qatomic_read(&tail) == &dummy.next) { |
| 261 | abort(); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Since we are the sole consumer, and we excluded the empty case |
| 266 | * above, the queue will always have at least two nodes: the |
| 267 | * dummy node, and the one being removed. So we do not need to update |
| 268 | * the tail pointer. |
| 269 | */ |
| 270 | head = next; |
| 271 | |
| 272 | /* If we dequeued the dummy node, add it back at the end and retry. */ |
| 273 | if (node == &dummy) { |
| 274 | enqueue(node); |
| 275 | goto retry; |
| 276 | } |
| 277 | |
| 278 | return node; |
| 279 | } |
| 280 | |
| 281 | static void *call_rcu_thread(void *opaque) |
| 282 | { |
| 283 | struct rcu_head *node; |
| 284 | |
| 285 | rcu_register_thread(); |
| 286 | |
| 287 | for (;;) { |
| 288 | int n; |
| 289 | |
| 290 | /* |
| 291 | * Fetch rcu_call_count now, we only must process elements that were |
| 292 | * added before synchronize_rcu() starts. |
| 293 | */ |
| 294 | for (;;) { |
| 295 | qemu_event_reset(&rcu_call_ready_event); |
| 296 | n = qatomic_read(&rcu_call_count); |
| 297 | if (n) { |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | #if defined(CONFIG_MALLOC_TRIM) |
| 302 | malloc_trim(4 * 1024 * 1024); |
| 303 | #endif |
| 304 | qemu_event_wait(&rcu_call_ready_event); |
| 305 | } |
| 306 | |
| 307 | synchronize_rcu(); |
| 308 | qatomic_sub(&rcu_call_count, n); |
| 309 | bql_lock(); |
| 310 | while (n > 0) { |
| 311 | node = try_dequeue(); |
| 312 | while (!node) { |
| 313 | bql_unlock(); |
| 314 | qemu_event_reset(&rcu_call_ready_event); |
| 315 | node = try_dequeue(); |
| 316 | if (!node) { |
| 317 | qemu_event_wait(&rcu_call_ready_event); |
| 318 | node = try_dequeue(); |
| 319 | } |
| 320 | bql_lock(); |
| 321 | } |
| 322 | |
| 323 | n--; |
| 324 | node->func(node); |
| 325 | } |
| 326 | bql_unlock(); |
| 327 | } |
| 328 | abort(); |
| 329 | } |
| 330 | |
| 331 | void call_rcu1(struct rcu_head *node, void (*func)(struct rcu_head *node)) |
| 332 | { |
| 333 | node->func = func; |
| 334 | enqueue(node); |
| 335 | qatomic_inc(&rcu_call_count); |
| 336 | qemu_event_set(&rcu_call_ready_event); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | struct rcu_drain { |
| 341 | struct rcu_head rcu; |
| 342 | QemuEvent drain_complete_event; |
| 343 | }; |
| 344 | |
| 345 | static void drain_rcu_callback(struct rcu_head *node) |
| 346 | { |
| 347 | struct rcu_drain *event = (struct rcu_drain *)node; |
| 348 | qemu_event_set(&event->drain_complete_event); |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * This function ensures that all pending RCU callbacks |
| 353 | * on the current thread are done executing |
| 354 | |
| 355 | * drops big qemu lock during the wait to allow RCU thread |
| 356 | * to process the callbacks |
| 357 | * |
| 358 | */ |
| 359 | |
| 360 | void drain_call_rcu(void) |
| 361 | { |
| 362 | struct rcu_drain rcu_drain; |
| 363 | bool locked = bql_locked(); |
| 364 | |
| 365 | memset(&rcu_drain, 0, sizeof(struct rcu_drain)); |
| 366 | qemu_event_init(&rcu_drain.drain_complete_event, false); |
| 367 | |
| 368 | if (locked) { |
| 369 | bql_unlock(); |
| 370 | } |
| 371 | |
| 372 | |
| 373 | /* |
| 374 | * RCU callbacks are invoked in the same order as in which they |
| 375 | * are registered, thus we can be sure that when 'drain_rcu_callback' |
| 376 | * is called, all RCU callbacks that were registered on this thread |
| 377 | * prior to calling this function are completed. |
| 378 | * |
| 379 | * Note that since we have only one global queue of the RCU callbacks, |
| 380 | * we also end up waiting for most of RCU callbacks that were registered |
| 381 | * on the other threads, but this is a side effect that shouldn't be |
| 382 | * assumed. |
| 383 | */ |
| 384 | |
| 385 | qatomic_inc(&in_drain_call_rcu); |
| 386 | call_rcu1(&rcu_drain.rcu, drain_rcu_callback); |
| 387 | qemu_event_wait(&rcu_drain.drain_complete_event); |
| 388 | qatomic_dec(&in_drain_call_rcu); |
| 389 | |
| 390 | if (locked) { |
| 391 | bql_lock(); |
| 392 | } |
| 393 | |
| 394 | } |
| 395 | |
| 396 | void rcu_register_thread(void) |
| 397 | { |
| 398 | assert(get_ptr_rcu_reader()->ctr == 0); |
| 399 | qemu_mutex_lock(&rcu_registry_lock); |
| 400 | QLIST_INSERT_HEAD(®istry, get_ptr_rcu_reader(), node); |
| 401 | qemu_mutex_unlock(&rcu_registry_lock); |
| 402 | } |
| 403 | |
| 404 | void rcu_unregister_thread(void) |
| 405 | { |
| 406 | qemu_mutex_lock(&rcu_registry_lock); |
| 407 | QLIST_REMOVE(get_ptr_rcu_reader(), node); |
| 408 | qemu_mutex_unlock(&rcu_registry_lock); |
| 409 | } |
| 410 | |
| 411 | void rcu_add_force_rcu_notifier(Notifier *n) |
| 412 | { |
| 413 | qemu_mutex_lock(&rcu_registry_lock); |
| 414 | notifier_list_add(&get_ptr_rcu_reader()->force_rcu, n); |
| 415 | qemu_mutex_unlock(&rcu_registry_lock); |
| 416 | } |
| 417 | |
| 418 | void rcu_remove_force_rcu_notifier(Notifier *n) |
| 419 | { |
| 420 | qemu_mutex_lock(&rcu_registry_lock); |
| 421 | notifier_remove(n); |
| 422 | qemu_mutex_unlock(&rcu_registry_lock); |
| 423 | } |
| 424 | |
| 425 | static void rcu_init_complete(void) |
| 426 | { |
| 427 | QemuThread thread; |
| 428 | |
| 429 | qemu_mutex_init(&rcu_registry_lock); |
| 430 | qemu_mutex_init(&rcu_sync_lock); |
| 431 | qemu_event_init(&rcu_gp_event, true); |
| 432 | |
| 433 | qemu_event_init(&rcu_call_ready_event, false); |
| 434 | |
| 435 | /* The caller is assumed to have BQL, so the call_rcu thread |
| 436 | * must have been quiescent even after forking, just recreate it. |
| 437 | */ |
| 438 | qemu_thread_create(&thread, "call_rcu", call_rcu_thread, |
| 439 | NULL, QEMU_THREAD_DETACHED); |
| 440 | |
| 441 | rcu_register_thread(); |
| 442 | } |
| 443 | |
| 444 | static int atfork_depth = 1; |
| 445 | |
| 446 | void rcu_enable_atfork(void) |
| 447 | { |
| 448 | atfork_depth++; |
| 449 | } |
| 450 | |
| 451 | void rcu_disable_atfork(void) |
| 452 | { |
| 453 | atfork_depth--; |
| 454 | } |
| 455 | |
| 456 | #ifdef CONFIG_POSIX |
| 457 | static void rcu_init_lock(void) |
| 458 | { |
| 459 | if (atfork_depth < 1) { |
| 460 | return; |
| 461 | } |
| 462 | |
| 463 | qemu_mutex_lock(&rcu_sync_lock); |
| 464 | qemu_mutex_lock(&rcu_registry_lock); |
| 465 | } |
| 466 | |
| 467 | static void rcu_init_unlock(void) |
| 468 | { |
| 469 | if (atfork_depth < 1) { |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | qemu_mutex_unlock(&rcu_registry_lock); |
| 474 | qemu_mutex_unlock(&rcu_sync_lock); |
| 475 | } |
| 476 | |
| 477 | static void rcu_init_child(void) |
| 478 | { |
| 479 | if (atfork_depth < 1) { |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | memset(®istry, 0, sizeof(registry)); |
| 484 | rcu_init_complete(); |
| 485 | } |
| 486 | #endif |
| 487 | |
| 488 | static void __attribute__((__constructor__)) rcu_init(void) |
| 489 | { |
| 490 | smp_mb_global_init(); |
| 491 | #ifdef CONFIG_POSIX |
| 492 | pthread_atfork(rcu_init_lock, rcu_init_unlock, rcu_init_child); |
| 493 | #endif |
| 494 | rcu_init_complete(); |
| 495 | } |