rcu.c 13 KB

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