4 * Userspace RCU library - batch memory reclamation with kernel API
6 * Copyright (c) 2010 Paul E. McKenney <paulmck@linux.vnet.ibm.com>
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.
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.
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
39 #include "urcu/wfcqueue.h"
40 #include "urcu-call-rcu.h"
41 #include "urcu-pointer.h"
42 #include "urcu/list.h"
43 #include "urcu/futex.h"
44 #include "urcu/tls-compat.h"
47 /* Data structure that identifies a call_rcu thread. */
49 struct call_rcu_data
{
51 * We do not align head on a different cache-line than tail
52 * mainly because call_rcu callback-invocation threads use
53 * batching ("splice") to get an entire list of callbacks, which
54 * effectively empties the queue, and requires to touch the tail
57 struct cds_wfcq_tail cbs_tail
;
58 struct cds_wfcq_head cbs_head
;
61 unsigned long qlen
; /* maintained for debugging. */
64 struct cds_list_head list
;
65 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
68 * List of all call_rcu_data structures to keep valgrind happy.
69 * Protected by call_rcu_mutex.
72 static CDS_LIST_HEAD(call_rcu_data_list
);
74 /* Link a thread using call_rcu() to its call_rcu thread. */
76 static DEFINE_URCU_TLS(struct call_rcu_data
*, thread_call_rcu_data
);
79 * Guard call_rcu thread creation and atfork handlers.
81 static pthread_mutex_t call_rcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
83 /* If a given thread does not have its own call_rcu thread, this is default. */
85 static struct call_rcu_data
*default_call_rcu_data
;
88 * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
89 * available, then we can have call_rcu threads assigned to individual
90 * CPUs rather than only to specific threads.
93 #ifdef HAVE_SCHED_GETCPU
95 static int urcu_sched_getcpu(void)
97 return sched_getcpu();
100 #else /* #ifdef HAVE_SCHED_GETCPU */
102 static int urcu_sched_getcpu(void)
107 #endif /* #else #ifdef HAVE_SCHED_GETCPU */
109 #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU)
112 * Pointer to array of pointers to per-CPU call_rcu_data structures
113 * and # CPUs. per_cpu_call_rcu_data is a RCU-protected pointer to an
114 * array of RCU-protected pointers to call_rcu_data. call_rcu acts as a
115 * RCU read-side and reads per_cpu_call_rcu_data and the per-cpu pointer
116 * without mutex. The call_rcu_mutex protects updates.
119 static struct call_rcu_data
**per_cpu_call_rcu_data
;
122 static void maxcpus_reset(void)
127 /* Allocate the array if it has not already been allocated. */
129 static void alloc_cpu_call_rcu_data(void)
131 struct call_rcu_data
**p
;
132 static int warned
= 0;
136 maxcpus
= sysconf(_SC_NPROCESSORS_CONF
);
140 p
= malloc(maxcpus
* sizeof(*per_cpu_call_rcu_data
));
142 memset(p
, '\0', maxcpus
* sizeof(*per_cpu_call_rcu_data
));
143 rcu_set_pointer(&per_cpu_call_rcu_data
, p
);
146 fprintf(stderr
, "[error] liburcu: unable to allocate per-CPU pointer array\n");
152 #else /* #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */
155 * per_cpu_call_rcu_data should be constant, but some functions below, used both
156 * for cases where cpu number is available and not available, assume it it not
159 static struct call_rcu_data
**per_cpu_call_rcu_data
= NULL
;
160 static const long maxcpus
= -1;
162 static void maxcpus_reset(void)
166 static void alloc_cpu_call_rcu_data(void)
170 #endif /* #else #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */
172 /* Acquire the specified pthread mutex. */
174 static void call_rcu_lock(pthread_mutex_t
*pmp
)
178 ret
= pthread_mutex_lock(pmp
);
183 /* Release the specified pthread mutex. */
185 static void call_rcu_unlock(pthread_mutex_t
*pmp
)
189 ret
= pthread_mutex_unlock(pmp
);
194 #if HAVE_SCHED_SETAFFINITY
196 int set_thread_cpu_affinity(struct call_rcu_data
*crdp
)
200 if (crdp
->cpu_affinity
< 0)
204 CPU_SET(crdp
->cpu_affinity
, &mask
);
205 #if SCHED_SETAFFINITY_ARGS == 2
206 return sched_setaffinity(0, &mask
);
208 return sched_setaffinity(0, sizeof(mask
), &mask
);
213 int set_thread_cpu_affinity(struct call_rcu_data
*crdp
)
219 static void call_rcu_wait(struct call_rcu_data
*crdp
)
221 /* Read call_rcu list before read futex */
223 if (uatomic_read(&crdp
->futex
) == -1)
224 futex_async(&crdp
->futex
, FUTEX_WAIT
, -1,
228 static void call_rcu_wake_up(struct call_rcu_data
*crdp
)
230 /* Write to call_rcu list before reading/writing futex */
232 if (caa_unlikely(uatomic_read(&crdp
->futex
) == -1)) {
233 uatomic_set(&crdp
->futex
, 0);
234 futex_async(&crdp
->futex
, FUTEX_WAKE
, 1,
239 /* This is the code run by each call_rcu thread. */
241 static void *call_rcu_thread(void *arg
)
243 unsigned long cbcount
;
244 struct call_rcu_data
*crdp
= (struct call_rcu_data
*) arg
;
245 int rt
= !!(uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_RT
);
248 ret
= set_thread_cpu_affinity(crdp
);
253 * If callbacks take a read-side lock, we need to be registered.
255 rcu_register_thread();
257 URCU_TLS(thread_call_rcu_data
) = crdp
;
259 uatomic_dec(&crdp
->futex
);
260 /* Decrement futex before reading call_rcu list */
264 struct cds_wfcq_head cbs_tmp_head
;
265 struct cds_wfcq_tail cbs_tmp_tail
;
266 struct cds_wfcq_node
*cbs
, *cbs_tmp_n
;
267 enum cds_wfcq_ret splice_ret
;
269 if (uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_PAUSE
) {
271 * Pause requested. Become quiescent: remove
272 * ourself from all global lists, and don't
273 * process any callback. The callback lists may
274 * still be non-empty though.
276 rcu_unregister_thread();
277 cmm_smp_mb__before_uatomic_or();
278 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_PAUSED
);
279 while ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_PAUSE
) != 0)
281 rcu_register_thread();
284 cds_wfcq_init(&cbs_tmp_head
, &cbs_tmp_tail
);
285 splice_ret
= __cds_wfcq_splice_blocking(&cbs_tmp_head
,
286 &cbs_tmp_tail
, &crdp
->cbs_head
, &crdp
->cbs_tail
);
287 assert(splice_ret
!= CDS_WFCQ_RET_WOULDBLOCK
);
288 assert(splice_ret
!= CDS_WFCQ_RET_DEST_NON_EMPTY
);
289 if (splice_ret
!= CDS_WFCQ_RET_SRC_EMPTY
) {
292 __cds_wfcq_for_each_blocking_safe(&cbs_tmp_head
,
293 &cbs_tmp_tail
, cbs
, cbs_tmp_n
) {
294 struct rcu_head
*rhp
;
296 rhp
= caa_container_of(cbs
,
297 struct rcu_head
, next
);
301 uatomic_sub(&crdp
->qlen
, cbcount
);
303 if (uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_STOP
)
305 rcu_thread_offline();
307 if (cds_wfcq_empty(&crdp
->cbs_head
,
311 uatomic_dec(&crdp
->futex
);
313 * Decrement futex before reading
327 * Read call_rcu list before write futex.
330 uatomic_set(&crdp
->futex
, 0);
332 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_STOPPED
);
333 rcu_unregister_thread();
338 * Create both a call_rcu thread and the corresponding call_rcu_data
339 * structure, linking the structure in as specified. Caller must hold
343 static void call_rcu_data_init(struct call_rcu_data
**crdpp
,
347 struct call_rcu_data
*crdp
;
350 crdp
= malloc(sizeof(*crdp
));
353 memset(crdp
, '\0', sizeof(*crdp
));
354 cds_wfcq_init(&crdp
->cbs_head
, &crdp
->cbs_tail
);
358 cds_list_add(&crdp
->list
, &call_rcu_data_list
);
359 crdp
->cpu_affinity
= cpu_affinity
;
360 cmm_smp_mb(); /* Structure initialized before pointer is planted. */
362 ret
= pthread_create(&crdp
->tid
, NULL
, call_rcu_thread
, crdp
);
368 * Return a pointer to the call_rcu_data structure for the specified
369 * CPU, returning NULL if there is none. We cannot automatically
370 * created it because the platform we are running on might not define
371 * urcu_sched_getcpu().
373 * The call to this function and use of the returned call_rcu_data
374 * should be protected by RCU read-side lock.
377 struct call_rcu_data
*get_cpu_call_rcu_data(int cpu
)
379 static int warned
= 0;
380 struct call_rcu_data
**pcpu_crdp
;
382 pcpu_crdp
= rcu_dereference(per_cpu_call_rcu_data
);
383 if (pcpu_crdp
== NULL
)
385 if (!warned
&& maxcpus
> 0 && (cpu
< 0 || maxcpus
<= cpu
)) {
386 fprintf(stderr
, "[error] liburcu: get CPU # out of range\n");
389 if (cpu
< 0 || maxcpus
<= cpu
)
391 return rcu_dereference(pcpu_crdp
[cpu
]);
395 * Return the tid corresponding to the call_rcu thread whose
396 * call_rcu_data structure is specified.
399 pthread_t
get_call_rcu_thread(struct call_rcu_data
*crdp
)
405 * Create a call_rcu_data structure (with thread) and return a pointer.
408 static struct call_rcu_data
*__create_call_rcu_data(unsigned long flags
,
411 struct call_rcu_data
*crdp
;
413 call_rcu_data_init(&crdp
, flags
, cpu_affinity
);
417 struct call_rcu_data
*create_call_rcu_data(unsigned long flags
,
420 struct call_rcu_data
*crdp
;
422 call_rcu_lock(&call_rcu_mutex
);
423 crdp
= __create_call_rcu_data(flags
, cpu_affinity
);
424 call_rcu_unlock(&call_rcu_mutex
);
429 * Set the specified CPU to use the specified call_rcu_data structure.
431 * Use NULL to remove a CPU's call_rcu_data structure, but it is
432 * the caller's responsibility to dispose of the removed structure.
433 * Use get_cpu_call_rcu_data() to obtain a pointer to the old structure
434 * (prior to NULLing it out, of course).
436 * The caller must wait for a grace-period to pass between return from
437 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
438 * previous call rcu data as argument.
441 int set_cpu_call_rcu_data(int cpu
, struct call_rcu_data
*crdp
)
443 static int warned
= 0;
445 call_rcu_lock(&call_rcu_mutex
);
446 alloc_cpu_call_rcu_data();
447 if (cpu
< 0 || maxcpus
<= cpu
) {
449 fprintf(stderr
, "[error] liburcu: set CPU # out of range\n");
452 call_rcu_unlock(&call_rcu_mutex
);
457 if (per_cpu_call_rcu_data
== NULL
) {
458 call_rcu_unlock(&call_rcu_mutex
);
463 if (per_cpu_call_rcu_data
[cpu
] != NULL
&& crdp
!= NULL
) {
464 call_rcu_unlock(&call_rcu_mutex
);
469 rcu_set_pointer(&per_cpu_call_rcu_data
[cpu
], crdp
);
470 call_rcu_unlock(&call_rcu_mutex
);
475 * Return a pointer to the default call_rcu_data structure, creating
476 * one if need be. Because we never free call_rcu_data structures,
477 * we don't need to be in an RCU read-side critical section.
480 struct call_rcu_data
*get_default_call_rcu_data(void)
482 if (default_call_rcu_data
!= NULL
)
483 return rcu_dereference(default_call_rcu_data
);
484 call_rcu_lock(&call_rcu_mutex
);
485 if (default_call_rcu_data
!= NULL
) {
486 call_rcu_unlock(&call_rcu_mutex
);
487 return default_call_rcu_data
;
489 call_rcu_data_init(&default_call_rcu_data
, 0, -1);
490 call_rcu_unlock(&call_rcu_mutex
);
491 return default_call_rcu_data
;
495 * Return the call_rcu_data structure that applies to the currently
496 * running thread. Any call_rcu_data structure assigned specifically
497 * to this thread has first priority, followed by any call_rcu_data
498 * structure assigned to the CPU on which the thread is running,
499 * followed by the default call_rcu_data structure. If there is not
500 * yet a default call_rcu_data structure, one will be created.
502 * Calls to this function and use of the returned call_rcu_data should
503 * be protected by RCU read-side lock.
505 struct call_rcu_data
*get_call_rcu_data(void)
507 struct call_rcu_data
*crd
;
509 if (URCU_TLS(thread_call_rcu_data
) != NULL
)
510 return URCU_TLS(thread_call_rcu_data
);
513 crd
= get_cpu_call_rcu_data(urcu_sched_getcpu());
518 return get_default_call_rcu_data();
522 * Return a pointer to this task's call_rcu_data if there is one.
525 struct call_rcu_data
*get_thread_call_rcu_data(void)
527 return URCU_TLS(thread_call_rcu_data
);
531 * Set this task's call_rcu_data structure as specified, regardless
532 * of whether or not this task already had one. (This allows switching
533 * to and from real-time call_rcu threads, for example.)
535 * Use NULL to remove a thread's call_rcu_data structure, but it is
536 * the caller's responsibility to dispose of the removed structure.
537 * Use get_thread_call_rcu_data() to obtain a pointer to the old structure
538 * (prior to NULLing it out, of course).
541 void set_thread_call_rcu_data(struct call_rcu_data
*crdp
)
543 URCU_TLS(thread_call_rcu_data
) = crdp
;
547 * Create a separate call_rcu thread for each CPU. This does not
548 * replace a pre-existing call_rcu thread -- use the set_cpu_call_rcu_data()
549 * function if you want that behavior. Should be paired with
550 * free_all_cpu_call_rcu_data() to teardown these call_rcu worker
554 int create_all_cpu_call_rcu_data(unsigned long flags
)
557 struct call_rcu_data
*crdp
;
560 call_rcu_lock(&call_rcu_mutex
);
561 alloc_cpu_call_rcu_data();
562 call_rcu_unlock(&call_rcu_mutex
);
567 if (per_cpu_call_rcu_data
== NULL
) {
571 for (i
= 0; i
< maxcpus
; i
++) {
572 call_rcu_lock(&call_rcu_mutex
);
573 if (get_cpu_call_rcu_data(i
)) {
574 call_rcu_unlock(&call_rcu_mutex
);
577 crdp
= __create_call_rcu_data(flags
, i
);
579 call_rcu_unlock(&call_rcu_mutex
);
583 call_rcu_unlock(&call_rcu_mutex
);
584 if ((ret
= set_cpu_call_rcu_data(i
, crdp
)) != 0) {
585 call_rcu_data_free(crdp
);
587 /* it has been created by other thread */
598 * Wake up the call_rcu thread corresponding to the specified
599 * call_rcu_data structure.
601 static void wake_call_rcu_thread(struct call_rcu_data
*crdp
)
603 if (!(_CMM_LOAD_SHARED(crdp
->flags
) & URCU_CALL_RCU_RT
))
604 call_rcu_wake_up(crdp
);
608 * Schedule a function to be invoked after a following grace period.
609 * This is the only function that must be called -- the others are
610 * only present to allow applications to tune their use of RCU for
611 * maximum performance.
613 * Note that unless a call_rcu thread has not already been created,
614 * the first invocation of call_rcu() will create one. So, if you
615 * need the first invocation of call_rcu() to be fast, make sure
616 * to create a call_rcu thread first. One way to accomplish this is
617 * "get_call_rcu_data();", and another is create_all_cpu_call_rcu_data().
619 * call_rcu must be called by registered RCU read-side threads.
622 void call_rcu(struct rcu_head
*head
,
623 void (*func
)(struct rcu_head
*head
))
625 struct call_rcu_data
*crdp
;
627 cds_wfcq_node_init(&head
->next
);
629 /* Holding rcu read-side lock across use of per-cpu crdp */
631 crdp
= get_call_rcu_data();
632 cds_wfcq_enqueue(&crdp
->cbs_head
, &crdp
->cbs_tail
, &head
->next
);
633 uatomic_inc(&crdp
->qlen
);
634 wake_call_rcu_thread(crdp
);
639 * Free up the specified call_rcu_data structure, terminating the
640 * associated call_rcu thread. The caller must have previously
641 * removed the call_rcu_data structure from per-thread or per-CPU
642 * usage. For example, set_cpu_call_rcu_data(cpu, NULL) for per-CPU
643 * call_rcu_data structures or set_thread_call_rcu_data(NULL) for
644 * per-thread call_rcu_data structures.
646 * We silently refuse to free up the default call_rcu_data structure
647 * because that is where we put any leftover callbacks. Note that
648 * the possibility of self-spawning callbacks makes it impossible
649 * to execute all the callbacks in finite time without putting any
650 * newly spawned callbacks somewhere else. The "somewhere else" of
651 * last resort is the default call_rcu_data structure.
653 * We also silently refuse to free NULL pointers. This simplifies
656 * The caller must wait for a grace-period to pass between return from
657 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
658 * previous call rcu data as argument.
660 * Note: introducing __cds_wfcq_splice_blocking() in this function fixed
661 * a list corruption bug in the 0.7.x series. The equivalent fix
662 * appeared in 0.6.8 for the stable-0.6 branch.
664 void call_rcu_data_free(struct call_rcu_data
*crdp
)
666 if (crdp
== NULL
|| crdp
== default_call_rcu_data
) {
669 if ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_STOPPED
) == 0) {
670 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_STOP
);
671 wake_call_rcu_thread(crdp
);
672 while ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_STOPPED
) == 0)
675 if (!cds_wfcq_empty(&crdp
->cbs_head
, &crdp
->cbs_tail
)) {
676 /* Create default call rcu data if need be */
677 (void) get_default_call_rcu_data();
678 __cds_wfcq_splice_blocking(&default_call_rcu_data
->cbs_head
,
679 &default_call_rcu_data
->cbs_tail
,
680 &crdp
->cbs_head
, &crdp
->cbs_tail
);
681 uatomic_add(&default_call_rcu_data
->qlen
,
682 uatomic_read(&crdp
->qlen
));
683 wake_call_rcu_thread(default_call_rcu_data
);
686 call_rcu_lock(&call_rcu_mutex
);
687 cds_list_del(&crdp
->list
);
688 call_rcu_unlock(&call_rcu_mutex
);
694 * Clean up all the per-CPU call_rcu threads.
696 void free_all_cpu_call_rcu_data(void)
699 struct call_rcu_data
**crdp
;
700 static int warned
= 0;
705 crdp
= malloc(sizeof(*crdp
) * maxcpus
);
708 fprintf(stderr
, "[error] liburcu: unable to allocate per-CPU pointer array\n");
714 for (cpu
= 0; cpu
< maxcpus
; cpu
++) {
715 crdp
[cpu
] = get_cpu_call_rcu_data(cpu
);
716 if (crdp
[cpu
] == NULL
)
718 set_cpu_call_rcu_data(cpu
, NULL
);
721 * Wait for call_rcu sites acting as RCU readers of the
722 * call_rcu_data to become quiescent.
725 for (cpu
= 0; cpu
< maxcpus
; cpu
++) {
726 if (crdp
[cpu
] == NULL
)
728 call_rcu_data_free(crdp
[cpu
]);
734 * Acquire the call_rcu_mutex in order to ensure that the child sees
735 * all of the call_rcu() data structures in a consistent state. Ensure
736 * that all call_rcu threads are in a quiescent state across fork.
737 * Suitable for pthread_atfork() and friends.
739 void call_rcu_before_fork(void)
741 struct call_rcu_data
*crdp
;
743 call_rcu_lock(&call_rcu_mutex
);
745 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
) {
746 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_PAUSE
);
747 cmm_smp_mb__after_uatomic_or();
748 wake_call_rcu_thread(crdp
);
750 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
) {
751 while ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_PAUSED
) == 0)
757 * Clean up call_rcu data structures in the parent of a successful fork()
758 * that is not followed by exec() in the child. Suitable for
759 * pthread_atfork() and friends.
761 void call_rcu_after_fork_parent(void)
763 struct call_rcu_data
*crdp
;
765 cds_list_for_each_entry(crdp
, &call_rcu_data_list
, list
)
766 uatomic_and(&crdp
->flags
, ~URCU_CALL_RCU_PAUSE
);
767 call_rcu_unlock(&call_rcu_mutex
);
771 * Clean up call_rcu data structures in the child of a successful fork()
772 * that is not followed by exec(). Suitable for pthread_atfork() and
775 void call_rcu_after_fork_child(void)
777 struct call_rcu_data
*crdp
, *next
;
779 /* Release the mutex. */
780 call_rcu_unlock(&call_rcu_mutex
);
782 /* Do nothing when call_rcu() has not been used */
783 if (cds_list_empty(&call_rcu_data_list
))
787 * Allocate a new default call_rcu_data structure in order
788 * to get a working call_rcu thread to go with it.
790 default_call_rcu_data
= NULL
;
791 (void)get_default_call_rcu_data();
793 /* Cleanup call_rcu_data pointers before use */
795 free(per_cpu_call_rcu_data
);
796 rcu_set_pointer(&per_cpu_call_rcu_data
, NULL
);
797 URCU_TLS(thread_call_rcu_data
) = NULL
;
800 * Dispose of all of the rest of the call_rcu_data structures.
801 * Leftover call_rcu callbacks will be merged into the new
802 * default call_rcu thread queue.
804 cds_list_for_each_entry_safe(crdp
, next
, &call_rcu_data_list
, list
) {
805 if (crdp
== default_call_rcu_data
)
807 uatomic_set(&crdp
->flags
, URCU_CALL_RCU_STOPPED
);
808 call_rcu_data_free(crdp
);