rcu.c 13 KB

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