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 * Align the tail on cache line size to eliminate false-sharing
54 struct cds_wfcq_tail
__attribute__((aligned(CAA_CACHE_LINE_SIZE
))) cbs_tail
;
55 /* Alignment on cache line size will add padding here */
57 struct cds_wfcq_head cbs_head
;
60 unsigned long qlen
; /* maintained for debugging. */
63 struct cds_list_head list
;
64 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
67 * List of all call_rcu_data structures to keep valgrind happy.
68 * Protected by call_rcu_mutex.
71 CDS_LIST_HEAD(call_rcu_data_list
);
73 /* Link a thread using call_rcu() to its call_rcu thread. */
75 static DEFINE_URCU_TLS(struct call_rcu_data
*, thread_call_rcu_data
);
77 /* Guard call_rcu thread creation. */
79 static pthread_mutex_t call_rcu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
81 /* If a given thread does not have its own call_rcu thread, this is default. */
83 static struct call_rcu_data
*default_call_rcu_data
;
86 * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
87 * available, then we can have call_rcu threads assigned to individual
88 * CPUs rather than only to specific threads.
91 #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
94 * Pointer to array of pointers to per-CPU call_rcu_data structures
95 * and # CPUs. per_cpu_call_rcu_data is a RCU-protected pointer to an
96 * array of RCU-protected pointers to call_rcu_data. call_rcu acts as a
97 * RCU read-side and reads per_cpu_call_rcu_data and the per-cpu pointer
98 * without mutex. The call_rcu_mutex protects updates.
101 static struct call_rcu_data
**per_cpu_call_rcu_data
;
104 static void maxcpus_reset(void)
109 /* Allocate the array if it has not already been allocated. */
111 static void alloc_cpu_call_rcu_data(void)
113 struct call_rcu_data
**p
;
114 static int warned
= 0;
118 maxcpus
= sysconf(_SC_NPROCESSORS_CONF
);
122 p
= malloc(maxcpus
* sizeof(*per_cpu_call_rcu_data
));
124 memset(p
, '\0', maxcpus
* sizeof(*per_cpu_call_rcu_data
));
125 rcu_set_pointer(&per_cpu_call_rcu_data
, p
);
128 fprintf(stderr
, "[error] liburcu: unable to allocate per-CPU pointer array\n");
134 #else /* #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
137 * per_cpu_call_rcu_data should be constant, but some functions below, used both
138 * for cases where cpu number is available and not available, assume it it not
141 static struct call_rcu_data
**per_cpu_call_rcu_data
= NULL
;
142 static const long maxcpus
= -1;
144 static void maxcpus_reset(void)
148 static void alloc_cpu_call_rcu_data(void)
152 static int sched_getcpu(void)
157 #endif /* #else #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
159 /* Acquire the specified pthread mutex. */
161 static void call_rcu_lock(pthread_mutex_t
*pmp
)
165 ret
= pthread_mutex_lock(pmp
);
170 /* Release the specified pthread mutex. */
172 static void call_rcu_unlock(pthread_mutex_t
*pmp
)
176 ret
= pthread_mutex_unlock(pmp
);
181 #if HAVE_SCHED_SETAFFINITY
183 int set_thread_cpu_affinity(struct call_rcu_data
*crdp
)
187 if (crdp
->cpu_affinity
< 0)
191 CPU_SET(crdp
->cpu_affinity
, &mask
);
192 #if SCHED_SETAFFINITY_ARGS == 2
193 return sched_setaffinity(0, &mask
);
195 return sched_setaffinity(0, sizeof(mask
), &mask
);
200 int set_thread_cpu_affinity(struct call_rcu_data
*crdp
)
206 static void call_rcu_wait(struct call_rcu_data
*crdp
)
208 /* Read call_rcu list before read futex */
210 if (uatomic_read(&crdp
->futex
) == -1)
211 futex_async(&crdp
->futex
, FUTEX_WAIT
, -1,
215 static void call_rcu_wake_up(struct call_rcu_data
*crdp
)
217 /* Write to call_rcu list before reading/writing futex */
219 if (caa_unlikely(uatomic_read(&crdp
->futex
) == -1)) {
220 uatomic_set(&crdp
->futex
, 0);
221 futex_async(&crdp
->futex
, FUTEX_WAKE
, 1,
226 /* This is the code run by each call_rcu thread. */
228 static void *call_rcu_thread(void *arg
)
230 unsigned long cbcount
;
231 struct call_rcu_data
*crdp
= (struct call_rcu_data
*) arg
;
232 int rt
= !!(uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_RT
);
235 ret
= set_thread_cpu_affinity(crdp
);
240 * If callbacks take a read-side lock, we need to be registered.
242 rcu_register_thread();
244 URCU_TLS(thread_call_rcu_data
) = crdp
;
246 uatomic_dec(&crdp
->futex
);
247 /* Decrement futex before reading call_rcu list */
251 struct cds_wfcq_head cbs_tmp_head
;
252 struct cds_wfcq_tail cbs_tmp_tail
;
253 struct cds_wfcq_node
*cbs
, *cbs_tmp_n
;
255 cds_wfcq_init(&cbs_tmp_head
, &cbs_tmp_tail
);
256 __cds_wfcq_splice_blocking(&cbs_tmp_head
, &cbs_tmp_tail
,
257 &crdp
->cbs_head
, &crdp
->cbs_tail
);
258 if (!cds_wfcq_empty(&cbs_tmp_head
, &cbs_tmp_tail
)) {
261 __cds_wfcq_for_each_blocking_safe(&cbs_tmp_head
,
262 &cbs_tmp_tail
, cbs
, cbs_tmp_n
) {
263 struct rcu_head
*rhp
;
265 rhp
= caa_container_of(cbs
,
266 struct rcu_head
, next
);
270 uatomic_sub(&crdp
->qlen
, cbcount
);
272 if (uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_STOP
)
274 rcu_thread_offline();
276 if (cds_wfcq_empty(&crdp
->cbs_head
,
280 uatomic_dec(&crdp
->futex
);
282 * Decrement futex before reading
296 * Read call_rcu list before write futex.
299 uatomic_set(&crdp
->futex
, 0);
301 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_STOPPED
);
302 rcu_unregister_thread();
307 * Create both a call_rcu thread and the corresponding call_rcu_data
308 * structure, linking the structure in as specified. Caller must hold
312 static void call_rcu_data_init(struct call_rcu_data
**crdpp
,
316 struct call_rcu_data
*crdp
;
319 crdp
= malloc(sizeof(*crdp
));
322 memset(crdp
, '\0', sizeof(*crdp
));
323 cds_wfcq_init(&crdp
->cbs_head
, &crdp
->cbs_tail
);
327 cds_list_add(&crdp
->list
, &call_rcu_data_list
);
328 crdp
->cpu_affinity
= cpu_affinity
;
329 cmm_smp_mb(); /* Structure initialized before pointer is planted. */
331 ret
= pthread_create(&crdp
->tid
, NULL
, call_rcu_thread
, crdp
);
337 * Return a pointer to the call_rcu_data structure for the specified
338 * CPU, returning NULL if there is none. We cannot automatically
339 * created it because the platform we are running on might not define
342 * The call to this function and use of the returned call_rcu_data
343 * should be protected by RCU read-side lock.
346 struct call_rcu_data
*get_cpu_call_rcu_data(int cpu
)
348 static int warned
= 0;
349 struct call_rcu_data
**pcpu_crdp
;
351 pcpu_crdp
= rcu_dereference(per_cpu_call_rcu_data
);
352 if (pcpu_crdp
== NULL
)
354 if (!warned
&& maxcpus
> 0 && (cpu
< 0 || maxcpus
<= cpu
)) {
355 fprintf(stderr
, "[error] liburcu: get CPU # out of range\n");
358 if (cpu
< 0 || maxcpus
<= cpu
)
360 return rcu_dereference(pcpu_crdp
[cpu
]);
364 * Return the tid corresponding to the call_rcu thread whose
365 * call_rcu_data structure is specified.
368 pthread_t
get_call_rcu_thread(struct call_rcu_data
*crdp
)
374 * Create a call_rcu_data structure (with thread) and return a pointer.
377 static struct call_rcu_data
*__create_call_rcu_data(unsigned long flags
,
380 struct call_rcu_data
*crdp
;
382 call_rcu_data_init(&crdp
, flags
, cpu_affinity
);
386 struct call_rcu_data
*create_call_rcu_data(unsigned long flags
,
389 struct call_rcu_data
*crdp
;
391 call_rcu_lock(&call_rcu_mutex
);
392 crdp
= __create_call_rcu_data(flags
, cpu_affinity
);
393 call_rcu_unlock(&call_rcu_mutex
);
398 * Set the specified CPU to use the specified call_rcu_data structure.
400 * Use NULL to remove a CPU's call_rcu_data structure, but it is
401 * the caller's responsibility to dispose of the removed structure.
402 * Use get_cpu_call_rcu_data() to obtain a pointer to the old structure
403 * (prior to NULLing it out, of course).
405 * The caller must wait for a grace-period to pass between return from
406 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
407 * previous call rcu data as argument.
410 int set_cpu_call_rcu_data(int cpu
, struct call_rcu_data
*crdp
)
412 static int warned
= 0;
414 call_rcu_lock(&call_rcu_mutex
);
415 alloc_cpu_call_rcu_data();
416 if (cpu
< 0 || maxcpus
<= cpu
) {
418 fprintf(stderr
, "[error] liburcu: set CPU # out of range\n");
421 call_rcu_unlock(&call_rcu_mutex
);
426 if (per_cpu_call_rcu_data
== NULL
) {
427 call_rcu_unlock(&call_rcu_mutex
);
432 if (per_cpu_call_rcu_data
[cpu
] != NULL
&& crdp
!= NULL
) {
433 call_rcu_unlock(&call_rcu_mutex
);
438 rcu_set_pointer(&per_cpu_call_rcu_data
[cpu
], crdp
);
439 call_rcu_unlock(&call_rcu_mutex
);
444 * Return a pointer to the default call_rcu_data structure, creating
445 * one if need be. Because we never free call_rcu_data structures,
446 * we don't need to be in an RCU read-side critical section.
449 struct call_rcu_data
*get_default_call_rcu_data(void)
451 if (default_call_rcu_data
!= NULL
)
452 return rcu_dereference(default_call_rcu_data
);
453 call_rcu_lock(&call_rcu_mutex
);
454 if (default_call_rcu_data
!= NULL
) {
455 call_rcu_unlock(&call_rcu_mutex
);
456 return default_call_rcu_data
;
458 call_rcu_data_init(&default_call_rcu_data
, 0, -1);
459 call_rcu_unlock(&call_rcu_mutex
);
460 return default_call_rcu_data
;
464 * Return the call_rcu_data structure that applies to the currently
465 * running thread. Any call_rcu_data structure assigned specifically
466 * to this thread has first priority, followed by any call_rcu_data
467 * structure assigned to the CPU on which the thread is running,
468 * followed by the default call_rcu_data structure. If there is not
469 * yet a default call_rcu_data structure, one will be created.
471 * Calls to this function and use of the returned call_rcu_data should
472 * be protected by RCU read-side lock.
474 struct call_rcu_data
*get_call_rcu_data(void)
476 struct call_rcu_data
*crd
;
478 if (URCU_TLS(thread_call_rcu_data
) != NULL
)
479 return URCU_TLS(thread_call_rcu_data
);
482 crd
= get_cpu_call_rcu_data(sched_getcpu());
487 return get_default_call_rcu_data();
491 * Return a pointer to this task's call_rcu_data if there is one.
494 struct call_rcu_data
*get_thread_call_rcu_data(void)
496 return URCU_TLS(thread_call_rcu_data
);
500 * Set this task's call_rcu_data structure as specified, regardless
501 * of whether or not this task already had one. (This allows switching
502 * to and from real-time call_rcu threads, for example.)
504 * Use NULL to remove a thread's call_rcu_data structure, but it is
505 * the caller's responsibility to dispose of the removed structure.
506 * Use get_thread_call_rcu_data() to obtain a pointer to the old structure
507 * (prior to NULLing it out, of course).
510 void set_thread_call_rcu_data(struct call_rcu_data
*crdp
)
512 URCU_TLS(thread_call_rcu_data
) = crdp
;
516 * Create a separate call_rcu thread for each CPU. This does not
517 * replace a pre-existing call_rcu thread -- use the set_cpu_call_rcu_data()
518 * function if you want that behavior. Should be paired with
519 * free_all_cpu_call_rcu_data() to teardown these call_rcu worker
523 int create_all_cpu_call_rcu_data(unsigned long flags
)
526 struct call_rcu_data
*crdp
;
529 call_rcu_lock(&call_rcu_mutex
);
530 alloc_cpu_call_rcu_data();
531 call_rcu_unlock(&call_rcu_mutex
);
536 if (per_cpu_call_rcu_data
== NULL
) {
540 for (i
= 0; i
< maxcpus
; i
++) {
541 call_rcu_lock(&call_rcu_mutex
);
542 if (get_cpu_call_rcu_data(i
)) {
543 call_rcu_unlock(&call_rcu_mutex
);
546 crdp
= __create_call_rcu_data(flags
, i
);
548 call_rcu_unlock(&call_rcu_mutex
);
552 call_rcu_unlock(&call_rcu_mutex
);
553 if ((ret
= set_cpu_call_rcu_data(i
, crdp
)) != 0) {
554 call_rcu_data_free(crdp
);
556 /* it has been created by other thread */
567 * Wake up the call_rcu thread corresponding to the specified
568 * call_rcu_data structure.
570 static void wake_call_rcu_thread(struct call_rcu_data
*crdp
)
572 if (!(_CMM_LOAD_SHARED(crdp
->flags
) & URCU_CALL_RCU_RT
))
573 call_rcu_wake_up(crdp
);
577 * Schedule a function to be invoked after a following grace period.
578 * This is the only function that must be called -- the others are
579 * only present to allow applications to tune their use of RCU for
580 * maximum performance.
582 * Note that unless a call_rcu thread has not already been created,
583 * the first invocation of call_rcu() will create one. So, if you
584 * need the first invocation of call_rcu() to be fast, make sure
585 * to create a call_rcu thread first. One way to accomplish this is
586 * "get_call_rcu_data();", and another is create_all_cpu_call_rcu_data().
588 * call_rcu must be called by registered RCU read-side threads.
591 void call_rcu(struct rcu_head
*head
,
592 void (*func
)(struct rcu_head
*head
))
594 struct call_rcu_data
*crdp
;
596 cds_wfcq_node_init(&head
->next
);
598 /* Holding rcu read-side lock across use of per-cpu crdp */
600 crdp
= get_call_rcu_data();
601 cds_wfcq_enqueue(&crdp
->cbs_head
, &crdp
->cbs_tail
, &head
->next
);
602 uatomic_inc(&crdp
->qlen
);
603 wake_call_rcu_thread(crdp
);
608 * Free up the specified call_rcu_data structure, terminating the
609 * associated call_rcu thread. The caller must have previously
610 * removed the call_rcu_data structure from per-thread or per-CPU
611 * usage. For example, set_cpu_call_rcu_data(cpu, NULL) for per-CPU
612 * call_rcu_data structures or set_thread_call_rcu_data(NULL) for
613 * per-thread call_rcu_data structures.
615 * We silently refuse to free up the default call_rcu_data structure
616 * because that is where we put any leftover callbacks. Note that
617 * the possibility of self-spawning callbacks makes it impossible
618 * to execute all the callbacks in finite time without putting any
619 * newly spawned callbacks somewhere else. The "somewhere else" of
620 * last resort is the default call_rcu_data structure.
622 * We also silently refuse to free NULL pointers. This simplifies
625 * The caller must wait for a grace-period to pass between return from
626 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
627 * previous call rcu data as argument.
629 void call_rcu_data_free(struct call_rcu_data
*crdp
)
631 if (crdp
== NULL
|| crdp
== default_call_rcu_data
) {
634 if ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_STOPPED
) == 0) {
635 uatomic_or(&crdp
->flags
, URCU_CALL_RCU_STOP
);
636 wake_call_rcu_thread(crdp
);
637 while ((uatomic_read(&crdp
->flags
) & URCU_CALL_RCU_STOPPED
) == 0)
640 if (!cds_wfcq_empty(&crdp
->cbs_head
, &crdp
->cbs_tail
)) {
641 /* Create default call rcu data if need be */
642 (void) get_default_call_rcu_data();
643 __cds_wfcq_splice_blocking(&default_call_rcu_data
->cbs_head
,
644 &default_call_rcu_data
->cbs_tail
,
645 &crdp
->cbs_head
, &crdp
->cbs_tail
);
646 uatomic_add(&default_call_rcu_data
->qlen
,
647 uatomic_read(&crdp
->qlen
));
648 wake_call_rcu_thread(default_call_rcu_data
);
651 call_rcu_lock(&call_rcu_mutex
);
652 cds_list_del(&crdp
->list
);
653 call_rcu_unlock(&call_rcu_mutex
);
659 * Clean up all the per-CPU call_rcu threads.
661 void free_all_cpu_call_rcu_data(void)
664 struct call_rcu_data
**crdp
;
665 static int warned
= 0;
670 crdp
= malloc(sizeof(*crdp
) * maxcpus
);
673 fprintf(stderr
, "[error] liburcu: unable to allocate per-CPU pointer array\n");
679 for (cpu
= 0; cpu
< maxcpus
; cpu
++) {
680 crdp
[cpu
] = get_cpu_call_rcu_data(cpu
);
681 if (crdp
[cpu
] == NULL
)
683 set_cpu_call_rcu_data(cpu
, NULL
);
686 * Wait for call_rcu sites acting as RCU readers of the
687 * call_rcu_data to become quiescent.
690 for (cpu
= 0; cpu
< maxcpus
; cpu
++) {
691 if (crdp
[cpu
] == NULL
)
693 call_rcu_data_free(crdp
[cpu
]);
699 * Acquire the call_rcu_mutex in order to ensure that the child sees
700 * all of the call_rcu() data structures in a consistent state.
701 * Suitable for pthread_atfork() and friends.
703 void call_rcu_before_fork(void)
705 call_rcu_lock(&call_rcu_mutex
);
709 * Clean up call_rcu data structures in the parent of a successful fork()
710 * that is not followed by exec() in the child. Suitable for
711 * pthread_atfork() and friends.
713 void call_rcu_after_fork_parent(void)
715 call_rcu_unlock(&call_rcu_mutex
);
719 * Clean up call_rcu data structures in the child of a successful fork()
720 * that is not followed by exec(). Suitable for pthread_atfork() and
723 void call_rcu_after_fork_child(void)
725 struct call_rcu_data
*crdp
, *next
;
727 /* Release the mutex. */
728 call_rcu_unlock(&call_rcu_mutex
);
730 /* Do nothing when call_rcu() has not been used */
731 if (cds_list_empty(&call_rcu_data_list
))
735 * Allocate a new default call_rcu_data structure in order
736 * to get a working call_rcu thread to go with it.
738 default_call_rcu_data
= NULL
;
739 (void)get_default_call_rcu_data();
741 /* Cleanup call_rcu_data pointers before use */
743 free(per_cpu_call_rcu_data
);
744 rcu_set_pointer(&per_cpu_call_rcu_data
, NULL
);
745 URCU_TLS(thread_call_rcu_data
) = NULL
;
747 /* Dispose of all of the rest of the call_rcu_data structures. */
748 cds_list_for_each_entry_safe(crdp
, next
, &call_rcu_data_list
, list
) {
749 if (crdp
== default_call_rcu_data
)
751 uatomic_set(&crdp
->flags
, URCU_CALL_RCU_STOPPED
);
752 call_rcu_data_free(crdp
);