uatomic/x86: Remove redundant memory barriers
[urcu.git] / src / urcu-call-rcu-impl.h
1 /*
2 * urcu-call-rcu.c
3 *
4 * Userspace RCU library - batch memory reclamation with kernel API
5 *
6 * Copyright (c) 2010 Paul E. McKenney <paulmck@linux.vnet.ibm.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #define _LGPL_SOURCE
24 #include <stdio.h>
25 #include <pthread.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <poll.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34 #include <sched.h>
35
36 #include "compat-getcpu.h"
37 #include <urcu/assert.h>
38 #include <urcu/wfcqueue.h>
39 #include <urcu/call-rcu.h>
40 #include <urcu/pointer.h>
41 #include <urcu/list.h>
42 #include <urcu/futex.h>
43 #include <urcu/tls-compat.h>
44 #include <urcu/ref.h>
45 #include "urcu-die.h"
46 #include "urcu-utils.h"
47 #include "compat-smp.h"
48
49 #define SET_AFFINITY_CHECK_PERIOD (1U << 8) /* 256 */
50 #define SET_AFFINITY_CHECK_PERIOD_MASK (SET_AFFINITY_CHECK_PERIOD - 1)
51
52 /* Data structure that identifies a call_rcu thread. */
53
54 struct call_rcu_data {
55 /*
56 * We do not align head on a different cache-line than tail
57 * mainly because call_rcu callback-invocation threads use
58 * batching ("splice") to get an entire list of callbacks, which
59 * effectively empties the queue, and requires to touch the tail
60 * anyway.
61 */
62 struct cds_wfcq_tail cbs_tail;
63 struct cds_wfcq_head cbs_head;
64 unsigned long flags;
65 int32_t futex;
66 unsigned long qlen; /* maintained for debugging. */
67 pthread_t tid;
68 int cpu_affinity;
69 unsigned long gp_count;
70 struct cds_list_head list;
71 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
72
73 struct call_rcu_completion {
74 int barrier_count;
75 int32_t futex;
76 struct urcu_ref ref;
77 };
78
79 struct call_rcu_completion_work {
80 struct rcu_head head;
81 struct call_rcu_completion *completion;
82 };
83
84 enum crdf_flags {
85 CRDF_FLAG_JOIN_THREAD = (1 << 0),
86 };
87
88 /*
89 * List of all call_rcu_data structures to keep valgrind happy.
90 * Protected by call_rcu_mutex.
91 */
92
93 static CDS_LIST_HEAD(call_rcu_data_list);
94
95 /* Link a thread using call_rcu() to its call_rcu thread. */
96
97 static DEFINE_URCU_TLS(struct call_rcu_data *, thread_call_rcu_data);
98
99 /*
100 * Guard call_rcu thread creation and atfork handlers.
101 */
102 static pthread_mutex_t call_rcu_mutex = PTHREAD_MUTEX_INITIALIZER;
103
104 /* If a given thread does not have its own call_rcu thread, this is default. */
105
106 static struct call_rcu_data *default_call_rcu_data;
107
108 static struct urcu_atfork *registered_rculfhash_atfork;
109
110 /*
111 * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
112 * available, then we can have call_rcu threads assigned to individual
113 * CPUs rather than only to specific threads.
114 */
115
116 #if defined(HAVE_SYSCONF) && (defined(HAVE_SCHED_GETCPU) || defined(HAVE_GETCPUID))
117
118 /*
119 * Pointer to array of pointers to per-CPU call_rcu_data structures
120 * and # CPUs. per_cpu_call_rcu_data is a RCU-protected pointer to an
121 * array of RCU-protected pointers to call_rcu_data. call_rcu acts as a
122 * RCU read-side and reads per_cpu_call_rcu_data and the per-cpu pointer
123 * without mutex. The call_rcu_mutex protects updates.
124 */
125
126 static struct call_rcu_data **per_cpu_call_rcu_data;
127 static long cpus_array_len;
128
129 static void cpus_array_len_reset(void)
130 {
131 cpus_array_len = 0;
132 }
133
134 /* Allocate the array if it has not already been allocated. */
135
136 static void alloc_cpu_call_rcu_data(void)
137 {
138 struct call_rcu_data **p;
139 static int warned = 0;
140
141 if (cpus_array_len != 0)
142 return;
143 cpus_array_len = get_possible_cpus_array_len();
144 if (cpus_array_len <= 0) {
145 return;
146 }
147 p = malloc(cpus_array_len * sizeof(*per_cpu_call_rcu_data));
148 if (p != NULL) {
149 memset(p, '\0', cpus_array_len * sizeof(*per_cpu_call_rcu_data));
150 rcu_set_pointer(&per_cpu_call_rcu_data, p);
151 } else {
152 if (!warned) {
153 fprintf(stderr, "[error] liburcu: unable to allocate per-CPU pointer array\n");
154 }
155 warned = 1;
156 }
157 }
158
159 #else /* #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */
160
161 /*
162 * per_cpu_call_rcu_data should be constant, but some functions below, used both
163 * for cases where cpu number is available and not available, assume it it not
164 * constant.
165 */
166 static struct call_rcu_data **per_cpu_call_rcu_data = NULL;
167 static const long cpus_array_len = -1;
168
169 static void cpus_array_len_reset(void)
170 {
171 }
172
173 static void alloc_cpu_call_rcu_data(void)
174 {
175 }
176
177 #endif /* #else #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */
178
179 /* Acquire the specified pthread mutex. */
180
181 static void call_rcu_lock(pthread_mutex_t *pmp)
182 {
183 int ret;
184
185 ret = pthread_mutex_lock(pmp);
186 if (ret)
187 urcu_die(ret);
188 }
189
190 /* Release the specified pthread mutex. */
191
192 static void call_rcu_unlock(pthread_mutex_t *pmp)
193 {
194 int ret;
195
196 ret = pthread_mutex_unlock(pmp);
197 if (ret)
198 urcu_die(ret);
199 }
200
201 /*
202 * Periodically retry setting CPU affinity if we migrate.
203 * Losing affinity can be caused by CPU hotunplug/hotplug, or by
204 * cpuset(7).
205 */
206 #ifdef HAVE_SCHED_SETAFFINITY
207 static
208 int set_thread_cpu_affinity(struct call_rcu_data *crdp)
209 {
210 cpu_set_t mask;
211 int ret;
212
213 if (crdp->cpu_affinity < 0)
214 return 0;
215 if (++crdp->gp_count & SET_AFFINITY_CHECK_PERIOD_MASK)
216 return 0;
217 if (urcu_sched_getcpu() == crdp->cpu_affinity)
218 return 0;
219
220 CPU_ZERO(&mask);
221 CPU_SET(crdp->cpu_affinity, &mask);
222 ret = sched_setaffinity(0, sizeof(mask), &mask);
223
224 /*
225 * EINVAL is fine: can be caused by hotunplugged CPUs, or by
226 * cpuset(7). This is why we should always retry if we detect
227 * migration.
228 */
229 if (ret && errno == EINVAL) {
230 ret = 0;
231 errno = 0;
232 }
233 return ret;
234 }
235 #else
236 static
237 int set_thread_cpu_affinity(struct call_rcu_data *crdp __attribute__((unused)))
238 {
239 return 0;
240 }
241 #endif
242
243 static void call_rcu_wait(struct call_rcu_data *crdp)
244 {
245 /* Read call_rcu list before read futex */
246 cmm_smp_mb();
247 while (uatomic_read(&crdp->futex) == -1) {
248 if (!futex_async(&crdp->futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
249 /*
250 * Prior queued wakeups queued by unrelated code
251 * using the same address can cause futex wait to
252 * return 0 even through the futex value is still
253 * -1 (spurious wakeups). Check the value again
254 * in user-space to validate whether it really
255 * differs from -1.
256 */
257 continue;
258 }
259 switch (errno) {
260 case EAGAIN:
261 /* Value already changed. */
262 return;
263 case EINTR:
264 /* Retry if interrupted by signal. */
265 break; /* Get out of switch. Check again. */
266 default:
267 /* Unexpected error. */
268 urcu_die(errno);
269 }
270 }
271 }
272
273 static void call_rcu_wake_up(struct call_rcu_data *crdp)
274 {
275 /* Write to call_rcu list before reading/writing futex */
276 cmm_smp_mb();
277 if (caa_unlikely(uatomic_read(&crdp->futex) == -1)) {
278 uatomic_set(&crdp->futex, 0);
279 if (futex_async(&crdp->futex, FUTEX_WAKE, 1,
280 NULL, NULL, 0) < 0)
281 urcu_die(errno);
282 }
283 }
284
285 static void call_rcu_completion_wait(struct call_rcu_completion *completion)
286 {
287 /* Read completion barrier count before read futex */
288 cmm_smp_mb();
289 while (uatomic_read(&completion->futex) == -1) {
290 if (!futex_async(&completion->futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
291 /*
292 * Prior queued wakeups queued by unrelated code
293 * using the same address can cause futex wait to
294 * return 0 even through the futex value is still
295 * -1 (spurious wakeups). Check the value again
296 * in user-space to validate whether it really
297 * differs from -1.
298 */
299 continue;
300 }
301 switch (errno) {
302 case EAGAIN:
303 /* Value already changed. */
304 return;
305 case EINTR:
306 /* Retry if interrupted by signal. */
307 break; /* Get out of switch. Check again. */
308 default:
309 /* Unexpected error. */
310 urcu_die(errno);
311 }
312 }
313 }
314
315 static void call_rcu_completion_wake_up(struct call_rcu_completion *completion)
316 {
317 /* Write to completion barrier count before reading/writing futex */
318 cmm_smp_mb();
319 if (caa_unlikely(uatomic_read(&completion->futex) == -1)) {
320 uatomic_set(&completion->futex, 0);
321 if (futex_async(&completion->futex, FUTEX_WAKE, 1,
322 NULL, NULL, 0) < 0)
323 urcu_die(errno);
324 }
325 }
326
327 /* This is the code run by each call_rcu thread. */
328
329 static void *call_rcu_thread(void *arg)
330 {
331 unsigned long cbcount;
332 struct call_rcu_data *crdp = (struct call_rcu_data *) arg;
333 int rt = !!(uatomic_read(&crdp->flags) & URCU_CALL_RCU_RT);
334
335 if (set_thread_cpu_affinity(crdp))
336 urcu_die(errno);
337
338 /*
339 * If callbacks take a read-side lock, we need to be registered.
340 */
341 rcu_register_thread();
342
343 URCU_TLS(thread_call_rcu_data) = crdp;
344 if (!rt) {
345 uatomic_dec(&crdp->futex);
346 /* Decrement futex before reading call_rcu list */
347 cmm_smp_mb();
348 }
349 for (;;) {
350 struct cds_wfcq_head cbs_tmp_head;
351 struct cds_wfcq_tail cbs_tmp_tail;
352 struct cds_wfcq_node *cbs, *cbs_tmp_n;
353 enum cds_wfcq_ret splice_ret;
354
355 if (set_thread_cpu_affinity(crdp))
356 urcu_die(errno);
357
358 if (uatomic_read(&crdp->flags) & URCU_CALL_RCU_PAUSE) {
359 /*
360 * Pause requested. Become quiescent: remove
361 * ourself from all global lists, and don't
362 * process any callback. The callback lists may
363 * still be non-empty though.
364 */
365 rcu_unregister_thread();
366 cmm_smp_mb__before_uatomic_or();
367 uatomic_or(&crdp->flags, URCU_CALL_RCU_PAUSED);
368 while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_PAUSE) != 0)
369 (void) poll(NULL, 0, 1);
370 uatomic_and(&crdp->flags, ~URCU_CALL_RCU_PAUSED);
371 cmm_smp_mb__after_uatomic_and();
372 rcu_register_thread();
373 }
374
375 cds_wfcq_init(&cbs_tmp_head, &cbs_tmp_tail);
376 splice_ret = __cds_wfcq_splice_blocking(&cbs_tmp_head,
377 &cbs_tmp_tail, &crdp->cbs_head, &crdp->cbs_tail);
378 urcu_posix_assert(splice_ret != CDS_WFCQ_RET_WOULDBLOCK);
379 urcu_posix_assert(splice_ret != CDS_WFCQ_RET_DEST_NON_EMPTY);
380 if (splice_ret != CDS_WFCQ_RET_SRC_EMPTY) {
381 synchronize_rcu();
382 cbcount = 0;
383 __cds_wfcq_for_each_blocking_safe(&cbs_tmp_head,
384 &cbs_tmp_tail, cbs, cbs_tmp_n) {
385 struct rcu_head *rhp;
386
387 rhp = caa_container_of(cbs,
388 struct rcu_head, next);
389 rhp->func(rhp);
390 cbcount++;
391 }
392 uatomic_sub(&crdp->qlen, cbcount);
393 }
394 if (uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOP)
395 break;
396 rcu_thread_offline();
397 if (!rt) {
398 if (cds_wfcq_empty(&crdp->cbs_head,
399 &crdp->cbs_tail)) {
400 call_rcu_wait(crdp);
401 (void) poll(NULL, 0, 10);
402 uatomic_dec(&crdp->futex);
403 /*
404 * Decrement futex before reading
405 * call_rcu list.
406 */
407 cmm_smp_mb();
408 } else {
409 (void) poll(NULL, 0, 10);
410 }
411 } else {
412 (void) poll(NULL, 0, 10);
413 }
414 rcu_thread_online();
415 }
416 if (!rt) {
417 /*
418 * Read call_rcu list before write futex.
419 */
420 cmm_smp_mb();
421 uatomic_set(&crdp->futex, 0);
422 }
423 uatomic_or(&crdp->flags, URCU_CALL_RCU_STOPPED);
424 rcu_unregister_thread();
425 return NULL;
426 }
427
428 /*
429 * Create both a call_rcu thread and the corresponding call_rcu_data
430 * structure, linking the structure in as specified. Caller must hold
431 * call_rcu_mutex.
432 */
433
434 static void call_rcu_data_init(struct call_rcu_data **crdpp,
435 unsigned long flags,
436 int cpu_affinity)
437 {
438 struct call_rcu_data *crdp;
439 int ret;
440 sigset_t newmask, oldmask;
441
442 crdp = malloc(sizeof(*crdp));
443 if (crdp == NULL)
444 urcu_die(errno);
445 memset(crdp, '\0', sizeof(*crdp));
446 cds_wfcq_init(&crdp->cbs_head, &crdp->cbs_tail);
447 crdp->qlen = 0;
448 crdp->futex = 0;
449 crdp->flags = flags;
450 cds_list_add(&crdp->list, &call_rcu_data_list);
451 crdp->cpu_affinity = cpu_affinity;
452 crdp->gp_count = 0;
453 rcu_set_pointer(crdpp, crdp);
454
455 ret = sigfillset(&newmask);
456 urcu_posix_assert(!ret);
457 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
458 urcu_posix_assert(!ret);
459
460 ret = pthread_create(&crdp->tid, NULL, call_rcu_thread, crdp);
461 if (ret)
462 urcu_die(ret);
463
464 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
465 urcu_posix_assert(!ret);
466 }
467
468 /*
469 * Return a pointer to the call_rcu_data structure for the specified
470 * CPU, returning NULL if there is none. We cannot automatically
471 * created it because the platform we are running on might not define
472 * urcu_sched_getcpu().
473 *
474 * The call to this function and use of the returned call_rcu_data
475 * should be protected by RCU read-side lock.
476 */
477
478 struct call_rcu_data *get_cpu_call_rcu_data(int cpu)
479 {
480 static int warned = 0;
481 struct call_rcu_data **pcpu_crdp;
482
483 pcpu_crdp = rcu_dereference(per_cpu_call_rcu_data);
484 if (pcpu_crdp == NULL)
485 return NULL;
486 if (!warned && cpus_array_len > 0 && (cpu < 0 || cpus_array_len <= cpu)) {
487 fprintf(stderr, "[error] liburcu: get CPU # out of range\n");
488 warned = 1;
489 }
490 if (cpu < 0 || cpus_array_len <= cpu)
491 return NULL;
492 return rcu_dereference(pcpu_crdp[cpu]);
493 }
494
495 /*
496 * Return the tid corresponding to the call_rcu thread whose
497 * call_rcu_data structure is specified.
498 */
499
500 pthread_t get_call_rcu_thread(struct call_rcu_data *crdp)
501 {
502 return crdp->tid;
503 }
504
505 /*
506 * Create a call_rcu_data structure (with thread) and return a pointer.
507 */
508
509 static struct call_rcu_data *__create_call_rcu_data(unsigned long flags,
510 int cpu_affinity)
511 {
512 struct call_rcu_data *crdp;
513
514 call_rcu_data_init(&crdp, flags, cpu_affinity);
515 return crdp;
516 }
517
518 struct call_rcu_data *create_call_rcu_data(unsigned long flags,
519 int cpu_affinity)
520 {
521 struct call_rcu_data *crdp;
522
523 call_rcu_lock(&call_rcu_mutex);
524 crdp = __create_call_rcu_data(flags, cpu_affinity);
525 call_rcu_unlock(&call_rcu_mutex);
526 return crdp;
527 }
528
529 /*
530 * Set the specified CPU to use the specified call_rcu_data structure.
531 *
532 * Use NULL to remove a CPU's call_rcu_data structure, but it is
533 * the caller's responsibility to dispose of the removed structure.
534 * Use get_cpu_call_rcu_data() to obtain a pointer to the old structure
535 * (prior to NULLing it out, of course).
536 *
537 * The caller must wait for a grace-period to pass between return from
538 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
539 * previous call rcu data as argument.
540 */
541
542 int set_cpu_call_rcu_data(int cpu, struct call_rcu_data *crdp)
543 {
544 static int warned = 0;
545
546 call_rcu_lock(&call_rcu_mutex);
547 alloc_cpu_call_rcu_data();
548 if (cpu < 0 || cpus_array_len <= cpu) {
549 if (!warned) {
550 fprintf(stderr, "[error] liburcu: set CPU # out of range\n");
551 warned = 1;
552 }
553 call_rcu_unlock(&call_rcu_mutex);
554 errno = EINVAL;
555 return -EINVAL;
556 }
557
558 if (per_cpu_call_rcu_data == NULL) {
559 call_rcu_unlock(&call_rcu_mutex);
560 errno = ENOMEM;
561 return -ENOMEM;
562 }
563
564 if (per_cpu_call_rcu_data[cpu] != NULL && crdp != NULL) {
565 call_rcu_unlock(&call_rcu_mutex);
566 errno = EEXIST;
567 return -EEXIST;
568 }
569
570 rcu_set_pointer(&per_cpu_call_rcu_data[cpu], crdp);
571 call_rcu_unlock(&call_rcu_mutex);
572 return 0;
573 }
574
575 /*
576 * Return a pointer to the default call_rcu_data structure, creating
577 * one if need be.
578 *
579 * The call to this function with intent to use the returned
580 * call_rcu_data should be protected by RCU read-side lock.
581 */
582
583 struct call_rcu_data *get_default_call_rcu_data(void)
584 {
585 struct call_rcu_data *crdp;
586
587 crdp = rcu_dereference(default_call_rcu_data);
588 if (crdp != NULL)
589 return crdp;
590
591 call_rcu_lock(&call_rcu_mutex);
592 if (default_call_rcu_data == NULL)
593 call_rcu_data_init(&default_call_rcu_data, 0, -1);
594 crdp = default_call_rcu_data;
595 call_rcu_unlock(&call_rcu_mutex);
596
597 return crdp;
598 }
599
600 /*
601 * Return the call_rcu_data structure that applies to the currently
602 * running thread. Any call_rcu_data structure assigned specifically
603 * to this thread has first priority, followed by any call_rcu_data
604 * structure assigned to the CPU on which the thread is running,
605 * followed by the default call_rcu_data structure. If there is not
606 * yet a default call_rcu_data structure, one will be created.
607 *
608 * Calls to this function and use of the returned call_rcu_data should
609 * be protected by RCU read-side lock.
610 */
611 struct call_rcu_data *get_call_rcu_data(void)
612 {
613 struct call_rcu_data *crd;
614
615 if (URCU_TLS(thread_call_rcu_data) != NULL)
616 return URCU_TLS(thread_call_rcu_data);
617
618 if (cpus_array_len > 0) {
619 crd = get_cpu_call_rcu_data(urcu_sched_getcpu());
620 if (crd)
621 return crd;
622 }
623
624 return get_default_call_rcu_data();
625 }
626
627 /*
628 * Return a pointer to this task's call_rcu_data if there is one.
629 */
630
631 struct call_rcu_data *get_thread_call_rcu_data(void)
632 {
633 return URCU_TLS(thread_call_rcu_data);
634 }
635
636 /*
637 * Set this task's call_rcu_data structure as specified, regardless
638 * of whether or not this task already had one. (This allows switching
639 * to and from real-time call_rcu threads, for example.)
640 *
641 * Use NULL to remove a thread's call_rcu_data structure, but it is
642 * the caller's responsibility to dispose of the removed structure.
643 * Use get_thread_call_rcu_data() to obtain a pointer to the old structure
644 * (prior to NULLing it out, of course).
645 */
646
647 void set_thread_call_rcu_data(struct call_rcu_data *crdp)
648 {
649 URCU_TLS(thread_call_rcu_data) = crdp;
650 }
651
652 /*
653 * Create a separate call_rcu thread for each CPU. This does not
654 * replace a pre-existing call_rcu thread -- use the set_cpu_call_rcu_data()
655 * function if you want that behavior. Should be paired with
656 * free_all_cpu_call_rcu_data() to teardown these call_rcu worker
657 * threads.
658 */
659
660 int create_all_cpu_call_rcu_data(unsigned long flags)
661 {
662 int i;
663 struct call_rcu_data *crdp;
664 int ret;
665
666 call_rcu_lock(&call_rcu_mutex);
667 alloc_cpu_call_rcu_data();
668 call_rcu_unlock(&call_rcu_mutex);
669 if (cpus_array_len <= 0) {
670 errno = EINVAL;
671 return -EINVAL;
672 }
673 if (per_cpu_call_rcu_data == NULL) {
674 errno = ENOMEM;
675 return -ENOMEM;
676 }
677 for (i = 0; i < cpus_array_len; i++) {
678 call_rcu_lock(&call_rcu_mutex);
679 if (get_cpu_call_rcu_data(i)) {
680 call_rcu_unlock(&call_rcu_mutex);
681 continue;
682 }
683 crdp = __create_call_rcu_data(flags, i);
684 if (crdp == NULL) {
685 call_rcu_unlock(&call_rcu_mutex);
686 errno = ENOMEM;
687 return -ENOMEM;
688 }
689 call_rcu_unlock(&call_rcu_mutex);
690 if ((ret = set_cpu_call_rcu_data(i, crdp)) != 0) {
691 call_rcu_data_free(crdp);
692
693 /* it has been created by other thread */
694 if (ret == -EEXIST)
695 continue;
696
697 return ret;
698 }
699 }
700 return 0;
701 }
702
703 /*
704 * Wake up the call_rcu thread corresponding to the specified
705 * call_rcu_data structure.
706 */
707 static void wake_call_rcu_thread(struct call_rcu_data *crdp)
708 {
709 if (!(_CMM_LOAD_SHARED(crdp->flags) & URCU_CALL_RCU_RT))
710 call_rcu_wake_up(crdp);
711 }
712
713 static void _call_rcu(struct rcu_head *head,
714 void (*func)(struct rcu_head *head),
715 struct call_rcu_data *crdp)
716 {
717 cds_wfcq_node_init(&head->next);
718 head->func = func;
719 cds_wfcq_enqueue(&crdp->cbs_head, &crdp->cbs_tail, &head->next);
720 uatomic_inc(&crdp->qlen);
721 wake_call_rcu_thread(crdp);
722 }
723
724 /*
725 * Schedule a function to be invoked after a following grace period.
726 * This is the only function that must be called -- the others are
727 * only present to allow applications to tune their use of RCU for
728 * maximum performance.
729 *
730 * Note that unless a call_rcu thread has not already been created,
731 * the first invocation of call_rcu() will create one. So, if you
732 * need the first invocation of call_rcu() to be fast, make sure
733 * to create a call_rcu thread first. One way to accomplish this is
734 * "get_call_rcu_data();", and another is create_all_cpu_call_rcu_data().
735 *
736 * call_rcu must be called by registered RCU read-side threads.
737 */
738 void call_rcu(struct rcu_head *head,
739 void (*func)(struct rcu_head *head))
740 {
741 struct call_rcu_data *crdp;
742
743 /* Holding rcu read-side lock across use of per-cpu crdp */
744 _rcu_read_lock();
745 crdp = get_call_rcu_data();
746 _call_rcu(head, func, crdp);
747 _rcu_read_unlock();
748 }
749
750 /*
751 * Free up the specified call_rcu_data structure, terminating the
752 * associated call_rcu thread. The caller must have previously
753 * removed the call_rcu_data structure from per-thread or per-CPU
754 * usage. For example, set_cpu_call_rcu_data(cpu, NULL) for per-CPU
755 * call_rcu_data structures or set_thread_call_rcu_data(NULL) for
756 * per-thread call_rcu_data structures.
757 *
758 * We silently refuse to free up the default call_rcu_data structure
759 * because that is where we put any leftover callbacks. Note that
760 * the possibility of self-spawning callbacks makes it impossible
761 * to execute all the callbacks in finite time without putting any
762 * newly spawned callbacks somewhere else. The "somewhere else" of
763 * last resort is the default call_rcu_data structure.
764 *
765 * We also silently refuse to free NULL pointers. This simplifies
766 * the calling code.
767 *
768 * The caller must wait for a grace-period to pass between return from
769 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
770 * previous call rcu data as argument.
771 *
772 * Note: introducing __cds_wfcq_splice_blocking() in this function fixed
773 * a list corruption bug in the 0.7.x series. The equivalent fix
774 * appeared in 0.6.8 for the stable-0.6 branch.
775 */
776 static
777 void _call_rcu_data_free(struct call_rcu_data *crdp, unsigned int flags)
778 {
779 if (crdp == NULL || crdp == default_call_rcu_data) {
780 return;
781 }
782 if ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0) {
783 uatomic_or(&crdp->flags, URCU_CALL_RCU_STOP);
784 wake_call_rcu_thread(crdp);
785 while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0)
786 (void) poll(NULL, 0, 1);
787 }
788 call_rcu_lock(&call_rcu_mutex);
789 if (!cds_wfcq_empty(&crdp->cbs_head, &crdp->cbs_tail)) {
790 call_rcu_unlock(&call_rcu_mutex);
791 /* Create default call rcu data if need be. */
792 /* CBs queued here will be handed to the default list. */
793 (void) get_default_call_rcu_data();
794 call_rcu_lock(&call_rcu_mutex);
795 __cds_wfcq_splice_blocking(&default_call_rcu_data->cbs_head,
796 &default_call_rcu_data->cbs_tail,
797 &crdp->cbs_head, &crdp->cbs_tail);
798 uatomic_add(&default_call_rcu_data->qlen,
799 uatomic_read(&crdp->qlen));
800 wake_call_rcu_thread(default_call_rcu_data);
801 }
802
803 cds_list_del(&crdp->list);
804 call_rcu_unlock(&call_rcu_mutex);
805
806 if (flags & CRDF_FLAG_JOIN_THREAD) {
807 int ret;
808
809 ret = pthread_join(get_call_rcu_thread(crdp), NULL);
810 if (ret)
811 urcu_die(ret);
812 }
813 free(crdp);
814 }
815
816 void call_rcu_data_free(struct call_rcu_data *crdp)
817 {
818 _call_rcu_data_free(crdp, CRDF_FLAG_JOIN_THREAD);
819 }
820
821 /*
822 * Clean up all the per-CPU call_rcu threads.
823 */
824 void free_all_cpu_call_rcu_data(void)
825 {
826 int cpu;
827 struct call_rcu_data **crdp;
828 static int warned = 0;
829
830 if (cpus_array_len <= 0)
831 return;
832
833 crdp = malloc(sizeof(*crdp) * cpus_array_len);
834 if (!crdp) {
835 if (!warned) {
836 fprintf(stderr, "[error] liburcu: unable to allocate per-CPU pointer array\n");
837 }
838 warned = 1;
839 return;
840 }
841
842 for (cpu = 0; cpu < cpus_array_len; cpu++) {
843 crdp[cpu] = get_cpu_call_rcu_data(cpu);
844 if (crdp[cpu] == NULL)
845 continue;
846 set_cpu_call_rcu_data(cpu, NULL);
847 }
848 /*
849 * Wait for call_rcu sites acting as RCU readers of the
850 * call_rcu_data to become quiescent.
851 */
852 synchronize_rcu();
853 for (cpu = 0; cpu < cpus_array_len; cpu++) {
854 if (crdp[cpu] == NULL)
855 continue;
856 call_rcu_data_free(crdp[cpu]);
857 }
858 free(crdp);
859 }
860
861 static
862 void free_completion(struct urcu_ref *ref)
863 {
864 struct call_rcu_completion *completion;
865
866 completion = caa_container_of(ref, struct call_rcu_completion, ref);
867 free(completion);
868 }
869
870 static
871 void _rcu_barrier_complete(struct rcu_head *head)
872 {
873 struct call_rcu_completion_work *work;
874 struct call_rcu_completion *completion;
875
876 work = caa_container_of(head, struct call_rcu_completion_work, head);
877 completion = work->completion;
878 if (!uatomic_sub_return(&completion->barrier_count, 1))
879 call_rcu_completion_wake_up(completion);
880 urcu_ref_put(&completion->ref, free_completion);
881 free(work);
882 }
883
884 /*
885 * Wait for all in-flight call_rcu callbacks to complete execution.
886 */
887 void rcu_barrier(void)
888 {
889 struct call_rcu_data *crdp;
890 struct call_rcu_completion *completion;
891 int count = 0;
892 int was_online;
893
894 /* Put in offline state in QSBR. */
895 was_online = _rcu_read_ongoing();
896 if (was_online)
897 rcu_thread_offline();
898 /*
899 * Calling a rcu_barrier() within a RCU read-side critical
900 * section is an error.
901 */
902 if (_rcu_read_ongoing()) {
903 static int warned = 0;
904
905 if (!warned) {
906 fprintf(stderr, "[error] liburcu: rcu_barrier() called from within RCU read-side critical section.\n");
907 }
908 warned = 1;
909 goto online;
910 }
911
912 completion = calloc(sizeof(*completion), 1);
913 if (!completion)
914 urcu_die(errno);
915
916 call_rcu_lock(&call_rcu_mutex);
917 cds_list_for_each_entry(crdp, &call_rcu_data_list, list)
918 count++;
919
920 /* Referenced by rcu_barrier() and each call_rcu thread. */
921 urcu_ref_set(&completion->ref, count + 1);
922 completion->barrier_count = count;
923
924 cds_list_for_each_entry(crdp, &call_rcu_data_list, list) {
925 struct call_rcu_completion_work *work;
926
927 work = calloc(sizeof(*work), 1);
928 if (!work)
929 urcu_die(errno);
930 work->completion = completion;
931 _call_rcu(&work->head, _rcu_barrier_complete, crdp);
932 }
933 call_rcu_unlock(&call_rcu_mutex);
934
935 /* Wait for them */
936 for (;;) {
937 uatomic_dec(&completion->futex);
938 /* Decrement futex before reading barrier_count */
939 cmm_smp_mb();
940 if (!uatomic_read(&completion->barrier_count))
941 break;
942 call_rcu_completion_wait(completion);
943 }
944
945 urcu_ref_put(&completion->ref, free_completion);
946
947 online:
948 if (was_online)
949 rcu_thread_online();
950 }
951
952 /*
953 * Acquire the call_rcu_mutex in order to ensure that the child sees
954 * all of the call_rcu() data structures in a consistent state. Ensure
955 * that all call_rcu threads are in a quiescent state across fork.
956 * Suitable for pthread_atfork() and friends.
957 */
958 void call_rcu_before_fork(void)
959 {
960 struct call_rcu_data *crdp;
961 struct urcu_atfork *atfork;
962
963 call_rcu_lock(&call_rcu_mutex);
964
965 atfork = registered_rculfhash_atfork;
966 if (atfork)
967 atfork->before_fork(atfork->priv);
968
969 cds_list_for_each_entry(crdp, &call_rcu_data_list, list) {
970 uatomic_or(&crdp->flags, URCU_CALL_RCU_PAUSE);
971 cmm_smp_mb__after_uatomic_or();
972 wake_call_rcu_thread(crdp);
973 }
974 cds_list_for_each_entry(crdp, &call_rcu_data_list, list) {
975 while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_PAUSED) == 0)
976 (void) poll(NULL, 0, 1);
977 }
978 }
979
980 /*
981 * Clean up call_rcu data structures in the parent of a successful fork()
982 * that is not followed by exec() in the child. Suitable for
983 * pthread_atfork() and friends.
984 */
985 void call_rcu_after_fork_parent(void)
986 {
987 struct call_rcu_data *crdp;
988 struct urcu_atfork *atfork;
989
990 cds_list_for_each_entry(crdp, &call_rcu_data_list, list)
991 uatomic_and(&crdp->flags, ~URCU_CALL_RCU_PAUSE);
992 cds_list_for_each_entry(crdp, &call_rcu_data_list, list) {
993 while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_PAUSED) != 0)
994 (void) poll(NULL, 0, 1);
995 }
996 atfork = registered_rculfhash_atfork;
997 if (atfork)
998 atfork->after_fork_parent(atfork->priv);
999 call_rcu_unlock(&call_rcu_mutex);
1000 }
1001
1002 /*
1003 * Clean up call_rcu data structures in the child of a successful fork()
1004 * that is not followed by exec(). Suitable for pthread_atfork() and
1005 * friends.
1006 */
1007 void call_rcu_after_fork_child(void)
1008 {
1009 struct call_rcu_data *crdp, *next;
1010 struct urcu_atfork *atfork;
1011
1012 /* Release the mutex. */
1013 call_rcu_unlock(&call_rcu_mutex);
1014
1015 atfork = registered_rculfhash_atfork;
1016 if (atfork)
1017 atfork->after_fork_child(atfork->priv);
1018
1019 /* Do nothing when call_rcu() has not been used */
1020 if (cds_list_empty(&call_rcu_data_list))
1021 return;
1022
1023 /*
1024 * Allocate a new default call_rcu_data structure in order
1025 * to get a working call_rcu thread to go with it.
1026 */
1027 default_call_rcu_data = NULL;
1028 (void)get_default_call_rcu_data();
1029
1030 /* Cleanup call_rcu_data pointers before use */
1031 cpus_array_len_reset();
1032 free(per_cpu_call_rcu_data);
1033 rcu_set_pointer(&per_cpu_call_rcu_data, NULL);
1034 URCU_TLS(thread_call_rcu_data) = NULL;
1035
1036 /*
1037 * Dispose of all of the rest of the call_rcu_data structures.
1038 * Leftover call_rcu callbacks will be merged into the new
1039 * default call_rcu thread queue.
1040 */
1041 cds_list_for_each_entry_safe(crdp, next, &call_rcu_data_list, list) {
1042 if (crdp == default_call_rcu_data)
1043 continue;
1044 uatomic_set(&crdp->flags, URCU_CALL_RCU_STOPPED);
1045 /*
1046 * Do not join the thread because it does not exist in
1047 * the child.
1048 */
1049 _call_rcu_data_free(crdp, 0);
1050 }
1051 }
1052
1053 void urcu_register_rculfhash_atfork(struct urcu_atfork *atfork)
1054 {
1055 if (CMM_LOAD_SHARED(registered_rculfhash_atfork))
1056 return;
1057 call_rcu_lock(&call_rcu_mutex);
1058 if (!registered_rculfhash_atfork)
1059 registered_rculfhash_atfork = atfork;
1060 call_rcu_unlock(&call_rcu_mutex);
1061 }
1062
1063 /*
1064 * This unregistration function is deprecated, meant only for internal
1065 * use by rculfhash.
1066 */
1067 __attribute__((__noreturn__))
1068 void urcu_unregister_rculfhash_atfork(struct urcu_atfork *atfork __attribute__((unused)))
1069 {
1070 urcu_die(EPERM);
1071 }
1072
1073 /*
1074 * Teardown the default call_rcu worker thread if there are no queued
1075 * callbacks on process exit. This prevents leaking memory.
1076 *
1077 * Here is how an application can ensure graceful teardown of this
1078 * worker thread:
1079 *
1080 * - An application queuing call_rcu callbacks should invoke
1081 * rcu_barrier() before it exits.
1082 * - When chaining call_rcu callbacks, the number of calls to
1083 * rcu_barrier() on application exit must match at least the maximum
1084 * number of chained callbacks.
1085 * - If an application chains callbacks endlessly, it would have to be
1086 * modified to stop chaining callbacks when it detects an application
1087 * exit (e.g. with a flag), and wait for quiescence with rcu_barrier()
1088 * after setting that flag.
1089 * - The statements above apply to a library which queues call_rcu
1090 * callbacks, only it needs to invoke rcu_barrier in its library
1091 * destructor.
1092 *
1093 * Note that this function does not presume it is being called when the
1094 * application is single-threaded even though this is invoked from a
1095 * destructor: this function synchronizes against concurrent calls to
1096 * get_default_call_rcu_data().
1097 */
1098 static void urcu_call_rcu_exit(void)
1099 {
1100 struct call_rcu_data *crdp;
1101 bool teardown = true;
1102
1103 if (default_call_rcu_data == NULL)
1104 return;
1105 call_rcu_lock(&call_rcu_mutex);
1106 /*
1107 * If the application leaves callbacks in the default call_rcu
1108 * worker queue, keep the default worker in place.
1109 */
1110 crdp = default_call_rcu_data;
1111 if (!crdp) {
1112 teardown = false;
1113 goto unlock;
1114 }
1115 if (!cds_wfcq_empty(&crdp->cbs_head, &crdp->cbs_tail)) {
1116 teardown = false;
1117 goto unlock;
1118 }
1119 rcu_set_pointer(&default_call_rcu_data, NULL);
1120 unlock:
1121 call_rcu_unlock(&call_rcu_mutex);
1122 if (teardown) {
1123 synchronize_rcu();
1124 call_rcu_data_free(crdp);
1125 }
1126 }
This page took 0.050071 seconds and 4 git commands to generate.