Fix: join worker thread in call_rcu_data_free
[urcu.git] / src / urcu-call-rcu-impl.h
CommitLineData
b57aee66
PM
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
5161f31e 23#define _LGPL_SOURCE
b57aee66
PM
24#include <stdio.h>
25#include <pthread.h>
26#include <signal.h>
b57aee66 27#include <stdlib.h>
6d841bc2 28#include <stdint.h>
b57aee66
PM
29#include <string.h>
30#include <errno.h>
31#include <poll.h>
32#include <sys/time.h>
b57aee66 33#include <unistd.h>
c1d2c60b 34#include <sched.h>
b57aee66 35
a47dd11c 36#include "compat-getcpu.h"
01477510 37#include <urcu/assert.h>
4477a870
MD
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>
4a6d7378 45#include "urcu-die.h"
4477a870 46#include "urcu-utils.h"
5cfe81b7 47#include "compat-smp.h"
b57aee66 48
8e3690db
MD
49#define SET_AFFINITY_CHECK_PERIOD (1U << 8) /* 256 */
50#define SET_AFFINITY_CHECK_PERIOD_MASK (SET_AFFINITY_CHECK_PERIOD - 1)
51
b57aee66
PM
52/* Data structure that identifies a call_rcu thread. */
53
54struct call_rcu_data {
5161f31e 55 /*
0b8ab7df
MD
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.
5161f31e 61 */
b9f893b6 62 struct cds_wfcq_tail cbs_tail;
0b8ab7df 63 struct cds_wfcq_head cbs_head;
b57aee66 64 unsigned long flags;
6d841bc2 65 int32_t futex;
73987721 66 unsigned long qlen; /* maintained for debugging. */
b57aee66 67 pthread_t tid;
c1d2c60b 68 int cpu_affinity;
8e3690db 69 unsigned long gp_count;
3c24913f 70 struct cds_list_head list;
b57aee66
PM
71} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
72
b7f721d9
MD
73struct call_rcu_completion {
74 int barrier_count;
75 int32_t futex;
81dd9134 76 struct urcu_ref ref;
b7f721d9
MD
77};
78
79struct call_rcu_completion_work {
80 struct rcu_head head;
81 struct call_rcu_completion *completion;
82};
83
1cf55ba4
MD
84enum crdf_flags {
85 CRDF_FLAG_JOIN_THREAD = (1 << 0),
86};
87
3c24913f
PM
88/*
89 * List of all call_rcu_data structures to keep valgrind happy.
90 * Protected by call_rcu_mutex.
91 */
92
bab44e28 93static CDS_LIST_HEAD(call_rcu_data_list);
3c24913f 94
b57aee66
PM
95/* Link a thread using call_rcu() to its call_rcu thread. */
96
2f661865 97static DEFINE_URCU_TLS(struct call_rcu_data *, thread_call_rcu_data);
b57aee66 98
e85451a1
MD
99/*
100 * Guard call_rcu thread creation and atfork handlers.
101 */
b57aee66
PM
102static 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
106static struct call_rcu_data *default_call_rcu_data;
107
d0ec0ed2 108static struct urcu_atfork *registered_rculfhash_atfork;
d0ec0ed2 109
b57aee66
PM
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
a47dd11c 116#if defined(HAVE_SYSCONF) && (defined(HAVE_SCHED_GETCPU) || defined(HAVE_GETCPUID))
b57aee66
PM
117
118/*
119 * Pointer to array of pointers to per-CPU call_rcu_data structures
618b2595
MD
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.
b57aee66
PM
124 */
125
126static struct call_rcu_data **per_cpu_call_rcu_data;
5cfe81b7 127static long cpus_array_len;
b57aee66 128
5cfe81b7 129static void cpus_array_len_reset(void)
60af049d 130{
5cfe81b7 131 cpus_array_len = 0;
60af049d
LJ
132}
133
b57aee66
PM
134/* Allocate the array if it has not already been allocated. */
135
136static void alloc_cpu_call_rcu_data(void)
137{
138 struct call_rcu_data **p;
139 static int warned = 0;
140
5cfe81b7 141 if (cpus_array_len != 0)
b57aee66 142 return;
5cfe81b7
MJ
143 cpus_array_len = get_possible_cpus_array_len();
144 if (cpus_array_len <= 0) {
b57aee66
PM
145 return;
146 }
5cfe81b7 147 p = malloc(cpus_array_len * sizeof(*per_cpu_call_rcu_data));
b57aee66 148 if (p != NULL) {
5cfe81b7 149 memset(p, '\0', cpus_array_len * sizeof(*per_cpu_call_rcu_data));
618b2595 150 rcu_set_pointer(&per_cpu_call_rcu_data, p);
b57aee66
PM
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
63b495d8 159#else /* #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */
b57aee66 160
f9437098
MD
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 */
166static struct call_rcu_data **per_cpu_call_rcu_data = NULL;
5cfe81b7 167static const long cpus_array_len = -1;
b57aee66 168
5cfe81b7 169static void cpus_array_len_reset(void)
60af049d
LJ
170{
171}
172
b57aee66
PM
173static void alloc_cpu_call_rcu_data(void)
174{
175}
176
63b495d8 177#endif /* #else #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */
b57aee66
PM
178
179/* Acquire the specified pthread mutex. */
180
181static void call_rcu_lock(pthread_mutex_t *pmp)
182{
4a6d7378
MD
183 int ret;
184
185 ret = pthread_mutex_lock(pmp);
186 if (ret)
187 urcu_die(ret);
b57aee66
PM
188}
189
190/* Release the specified pthread mutex. */
191
192static void call_rcu_unlock(pthread_mutex_t *pmp)
193{
4a6d7378
MD
194 int ret;
195
196 ret = pthread_mutex_unlock(pmp);
197 if (ret)
198 urcu_die(ret);
b57aee66
PM
199}
200
8e3690db
MD
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 */
2388c075 206#ifdef HAVE_SCHED_SETAFFINITY
c1d2c60b
MD
207static
208int set_thread_cpu_affinity(struct call_rcu_data *crdp)
209{
210 cpu_set_t mask;
8e3690db 211 int ret;
c1d2c60b
MD
212
213 if (crdp->cpu_affinity < 0)
214 return 0;
8e3690db
MD
215 if (++crdp->gp_count & SET_AFFINITY_CHECK_PERIOD_MASK)
216 return 0;
217 if (urcu_sched_getcpu() == crdp->cpu_affinity)
218 return 0;
c1d2c60b
MD
219
220 CPU_ZERO(&mask);
221 CPU_SET(crdp->cpu_affinity, &mask);
8e3690db 222 ret = sched_setaffinity(0, sizeof(mask), &mask);
0614a2e6 223
8e3690db
MD
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;
c1d2c60b
MD
234}
235#else
236static
a142df4e 237int set_thread_cpu_affinity(struct call_rcu_data *crdp __attribute__((unused)))
c1d2c60b
MD
238{
239 return 0;
240}
241#endif
242
03fe58b3
MD
243static void call_rcu_wait(struct call_rcu_data *crdp)
244{
245 /* Read call_rcu list before read futex */
246 cmm_smp_mb();
98d705dd
MD
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 }
b0a841b4 259 switch (errno) {
98d705dd 260 case EAGAIN:
b0a841b4
MD
261 /* Value already changed. */
262 return;
263 case EINTR:
264 /* Retry if interrupted by signal. */
98d705dd 265 break; /* Get out of switch. Check again. */
b0a841b4
MD
266 default:
267 /* Unexpected error. */
268 urcu_die(errno);
269 }
270 }
03fe58b3
MD
271}
272
273static 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();
a0b7f7ea 277 if (caa_unlikely(uatomic_read(&crdp->futex) == -1)) {
03fe58b3 278 uatomic_set(&crdp->futex, 0);
b0a841b4
MD
279 if (futex_async(&crdp->futex, FUTEX_WAKE, 1,
280 NULL, NULL, 0) < 0)
281 urcu_die(errno);
03fe58b3
MD
282 }
283}
284
b7f721d9
MD
285static void call_rcu_completion_wait(struct call_rcu_completion *completion)
286{
287 /* Read completion barrier count before read futex */
288 cmm_smp_mb();
98d705dd
MD
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 }
b0a841b4 301 switch (errno) {
98d705dd 302 case EAGAIN:
b0a841b4
MD
303 /* Value already changed. */
304 return;
305 case EINTR:
306 /* Retry if interrupted by signal. */
98d705dd 307 break; /* Get out of switch. Check again. */
b0a841b4
MD
308 default:
309 /* Unexpected error. */
310 urcu_die(errno);
311 }
312 }
b7f721d9
MD
313}
314
315static 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);
b0a841b4
MD
321 if (futex_async(&completion->futex, FUTEX_WAKE, 1,
322 NULL, NULL, 0) < 0)
323 urcu_die(errno);
b7f721d9
MD
324 }
325}
326
b57aee66
PM
327/* This is the code run by each call_rcu thread. */
328
329static void *call_rcu_thread(void *arg)
330{
331 unsigned long cbcount;
5161f31e 332 struct call_rcu_data *crdp = (struct call_rcu_data *) arg;
2870aa1e 333 int rt = !!(uatomic_read(&crdp->flags) & URCU_CALL_RCU_RT);
b57aee66 334
8e3690db 335 if (set_thread_cpu_affinity(crdp))
4a6d7378 336 urcu_die(errno);
c1d2c60b 337
765f3ead
MD
338 /*
339 * If callbacks take a read-side lock, we need to be registered.
340 */
341 rcu_register_thread();
342
bd252a04 343 URCU_TLS(thread_call_rcu_data) = crdp;
bc94ca9b
MD
344 if (!rt) {
345 uatomic_dec(&crdp->futex);
346 /* Decrement futex before reading call_rcu list */
347 cmm_smp_mb();
348 }
b57aee66 349 for (;;) {
5161f31e
MD
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;
ae25b7e2 353 enum cds_wfcq_ret splice_ret;
5161f31e 354
8e3690db
MD
355 if (set_thread_cpu_affinity(crdp))
356 urcu_die(errno);
357
e85451a1
MD
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)
109105b7 369 (void) poll(NULL, 0, 1);
fc236e5e
KF
370 uatomic_and(&crdp->flags, ~URCU_CALL_RCU_PAUSED);
371 cmm_smp_mb__after_uatomic_and();
e85451a1
MD
372 rcu_register_thread();
373 }
374
5161f31e 375 cds_wfcq_init(&cbs_tmp_head, &cbs_tmp_tail);
ae25b7e2
MD
376 splice_ret = __cds_wfcq_splice_blocking(&cbs_tmp_head,
377 &cbs_tmp_tail, &crdp->cbs_head, &crdp->cbs_tail);
01477510
FD
378 urcu_posix_assert(splice_ret != CDS_WFCQ_RET_WOULDBLOCK);
379 urcu_posix_assert(splice_ret != CDS_WFCQ_RET_DEST_NON_EMPTY);
ae25b7e2 380 if (splice_ret != CDS_WFCQ_RET_SRC_EMPTY) {
b57aee66
PM
381 synchronize_rcu();
382 cbcount = 0;
5161f31e
MD
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);
b57aee66
PM
389 rhp->func(rhp);
390 cbcount++;
5161f31e 391 }
b57aee66
PM
392 uatomic_sub(&crdp->qlen, cbcount);
393 }
bc94ca9b
MD
394 if (uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOP)
395 break;
765f3ead 396 rcu_thread_offline();
bc94ca9b 397 if (!rt) {
5161f31e
MD
398 if (cds_wfcq_empty(&crdp->cbs_head,
399 &crdp->cbs_tail)) {
bc94ca9b 400 call_rcu_wait(crdp);
109105b7 401 (void) poll(NULL, 0, 10);
bc94ca9b 402 uatomic_dec(&crdp->futex);
c768e45e 403 /*
bc94ca9b
MD
404 * Decrement futex before reading
405 * call_rcu list.
c768e45e
MD
406 */
407 cmm_smp_mb();
ccbac24d 408 } else {
109105b7 409 (void) poll(NULL, 0, 10);
c768e45e 410 }
bc94ca9b 411 } else {
109105b7 412 (void) poll(NULL, 0, 10);
b57aee66 413 }
765f3ead 414 rcu_thread_online();
bc94ca9b
MD
415 }
416 if (!rt) {
417 /*
418 * Read call_rcu list before write futex.
419 */
420 cmm_smp_mb();
421 uatomic_set(&crdp->futex, 0);
b57aee66 422 }
2870aa1e 423 uatomic_or(&crdp->flags, URCU_CALL_RCU_STOPPED);
765f3ead 424 rcu_unregister_thread();
7106ddf8 425 return NULL;
b57aee66
PM
426}
427
428/*
429 * Create both a call_rcu thread and the corresponding call_rcu_data
3c24913f
PM
430 * structure, linking the structure in as specified. Caller must hold
431 * call_rcu_mutex.
b57aee66
PM
432 */
433
3c24913f 434static void call_rcu_data_init(struct call_rcu_data **crdpp,
c1d2c60b
MD
435 unsigned long flags,
436 int cpu_affinity)
b57aee66
PM
437{
438 struct call_rcu_data *crdp;
4a6d7378 439 int ret;
ea3a28a3 440 sigset_t newmask, oldmask;
b57aee66
PM
441
442 crdp = malloc(sizeof(*crdp));
4a6d7378
MD
443 if (crdp == NULL)
444 urcu_die(errno);
b57aee66 445 memset(crdp, '\0', sizeof(*crdp));
5161f31e 446 cds_wfcq_init(&crdp->cbs_head, &crdp->cbs_tail);
b57aee66 447 crdp->qlen = 0;
263e3cf9
MD
448 crdp->futex = 0;
449 crdp->flags = flags;
3c24913f 450 cds_list_add(&crdp->list, &call_rcu_data_list);
c1d2c60b 451 crdp->cpu_affinity = cpu_affinity;
8e3690db 452 crdp->gp_count = 0;
b57aee66
PM
453 cmm_smp_mb(); /* Structure initialized before pointer is planted. */
454 *crdpp = crdp;
ea3a28a3
MD
455
456 ret = sigfillset(&newmask);
457 urcu_posix_assert(!ret);
458 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
459 urcu_posix_assert(!ret);
460
4a6d7378
MD
461 ret = pthread_create(&crdp->tid, NULL, call_rcu_thread, crdp);
462 if (ret)
463 urcu_die(ret);
ea3a28a3
MD
464
465 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
466 urcu_posix_assert(!ret);
b57aee66
PM
467}
468
469/*
470 * Return a pointer to the call_rcu_data structure for the specified
471 * CPU, returning NULL if there is none. We cannot automatically
472 * created it because the platform we are running on might not define
63b495d8 473 * urcu_sched_getcpu().
618b2595
MD
474 *
475 * The call to this function and use of the returned call_rcu_data
476 * should be protected by RCU read-side lock.
b57aee66
PM
477 */
478
479struct call_rcu_data *get_cpu_call_rcu_data(int cpu)
480{
481 static int warned = 0;
618b2595 482 struct call_rcu_data **pcpu_crdp;
b57aee66 483
618b2595
MD
484 pcpu_crdp = rcu_dereference(per_cpu_call_rcu_data);
485 if (pcpu_crdp == NULL)
b57aee66 486 return NULL;
5cfe81b7 487 if (!warned && cpus_array_len > 0 && (cpu < 0 || cpus_array_len <= cpu)) {
b57aee66
PM
488 fprintf(stderr, "[error] liburcu: get CPU # out of range\n");
489 warned = 1;
490 }
5cfe81b7 491 if (cpu < 0 || cpus_array_len <= cpu)
b57aee66 492 return NULL;
618b2595 493 return rcu_dereference(pcpu_crdp[cpu]);
b57aee66
PM
494}
495
496/*
497 * Return the tid corresponding to the call_rcu thread whose
498 * call_rcu_data structure is specified.
499 */
500
501pthread_t get_call_rcu_thread(struct call_rcu_data *crdp)
502{
503 return crdp->tid;
504}
505
506/*
507 * Create a call_rcu_data structure (with thread) and return a pointer.
508 */
509
c1d2c60b
MD
510static struct call_rcu_data *__create_call_rcu_data(unsigned long flags,
511 int cpu_affinity)
b57aee66
PM
512{
513 struct call_rcu_data *crdp;
514
c1d2c60b 515 call_rcu_data_init(&crdp, flags, cpu_affinity);
b57aee66
PM
516 return crdp;
517}
518
c1d2c60b
MD
519struct call_rcu_data *create_call_rcu_data(unsigned long flags,
520 int cpu_affinity)
3c24913f
PM
521{
522 struct call_rcu_data *crdp;
523
524 call_rcu_lock(&call_rcu_mutex);
c1d2c60b 525 crdp = __create_call_rcu_data(flags, cpu_affinity);
3c24913f
PM
526 call_rcu_unlock(&call_rcu_mutex);
527 return crdp;
528}
529
b57aee66
PM
530/*
531 * Set the specified CPU to use the specified call_rcu_data structure.
7106ddf8
PM
532 *
533 * Use NULL to remove a CPU's call_rcu_data structure, but it is
534 * the caller's responsibility to dispose of the removed structure.
535 * Use get_cpu_call_rcu_data() to obtain a pointer to the old structure
536 * (prior to NULLing it out, of course).
f9da0936
MD
537 *
538 * The caller must wait for a grace-period to pass between return from
539 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
540 * previous call rcu data as argument.
b57aee66
PM
541 */
542
543int set_cpu_call_rcu_data(int cpu, struct call_rcu_data *crdp)
544{
dcfc8165 545 static int warned = 0;
b57aee66
PM
546
547 call_rcu_lock(&call_rcu_mutex);
f3776786 548 alloc_cpu_call_rcu_data();
5cfe81b7 549 if (cpu < 0 || cpus_array_len <= cpu) {
b57aee66
PM
550 if (!warned) {
551 fprintf(stderr, "[error] liburcu: set CPU # out of range\n");
552 warned = 1;
553 }
554 call_rcu_unlock(&call_rcu_mutex);
555 errno = EINVAL;
556 return -EINVAL;
557 }
53a55535 558
b57aee66 559 if (per_cpu_call_rcu_data == NULL) {
3670fef2 560 call_rcu_unlock(&call_rcu_mutex);
b57aee66
PM
561 errno = ENOMEM;
562 return -ENOMEM;
563 }
53a55535
LJ
564
565 if (per_cpu_call_rcu_data[cpu] != NULL && crdp != NULL) {
566 call_rcu_unlock(&call_rcu_mutex);
567 errno = EEXIST;
568 return -EEXIST;
569 }
570
618b2595 571 rcu_set_pointer(&per_cpu_call_rcu_data[cpu], crdp);
3670fef2 572 call_rcu_unlock(&call_rcu_mutex);
b57aee66
PM
573 return 0;
574}
575
576/*
577 * Return a pointer to the default call_rcu_data structure, creating
578 * one if need be. Because we never free call_rcu_data structures,
579 * we don't need to be in an RCU read-side critical section.
580 */
581
582struct call_rcu_data *get_default_call_rcu_data(void)
583{
584 if (default_call_rcu_data != NULL)
585 return rcu_dereference(default_call_rcu_data);
586 call_rcu_lock(&call_rcu_mutex);
587 if (default_call_rcu_data != NULL) {
588 call_rcu_unlock(&call_rcu_mutex);
589 return default_call_rcu_data;
590 }
c1d2c60b 591 call_rcu_data_init(&default_call_rcu_data, 0, -1);
b57aee66
PM
592 call_rcu_unlock(&call_rcu_mutex);
593 return default_call_rcu_data;
594}
595
596/*
597 * Return the call_rcu_data structure that applies to the currently
598 * running thread. Any call_rcu_data structure assigned specifically
599 * to this thread has first priority, followed by any call_rcu_data
600 * structure assigned to the CPU on which the thread is running,
601 * followed by the default call_rcu_data structure. If there is not
602 * yet a default call_rcu_data structure, one will be created.
618b2595
MD
603 *
604 * Calls to this function and use of the returned call_rcu_data should
605 * be protected by RCU read-side lock.
b57aee66
PM
606 */
607struct call_rcu_data *get_call_rcu_data(void)
608{
9744f3bb 609 struct call_rcu_data *crd;
b57aee66 610
bd252a04
MD
611 if (URCU_TLS(thread_call_rcu_data) != NULL)
612 return URCU_TLS(thread_call_rcu_data);
9744f3bb 613
5cfe81b7 614 if (cpus_array_len > 0) {
63b495d8 615 crd = get_cpu_call_rcu_data(urcu_sched_getcpu());
9744f3bb
LJ
616 if (crd)
617 return crd;
b57aee66 618 }
9744f3bb 619
b57aee66
PM
620 return get_default_call_rcu_data();
621}
622
623/*
624 * Return a pointer to this task's call_rcu_data if there is one.
625 */
626
627struct call_rcu_data *get_thread_call_rcu_data(void)
628{
bd252a04 629 return URCU_TLS(thread_call_rcu_data);
b57aee66
PM
630}
631
632/*
633 * Set this task's call_rcu_data structure as specified, regardless
634 * of whether or not this task already had one. (This allows switching
635 * to and from real-time call_rcu threads, for example.)
7106ddf8
PM
636 *
637 * Use NULL to remove a thread's call_rcu_data structure, but it is
638 * the caller's responsibility to dispose of the removed structure.
639 * Use get_thread_call_rcu_data() to obtain a pointer to the old structure
640 * (prior to NULLing it out, of course).
b57aee66
PM
641 */
642
643void set_thread_call_rcu_data(struct call_rcu_data *crdp)
644{
bd252a04 645 URCU_TLS(thread_call_rcu_data) = crdp;
b57aee66
PM
646}
647
648/*
649 * Create a separate call_rcu thread for each CPU. This does not
650 * replace a pre-existing call_rcu thread -- use the set_cpu_call_rcu_data()
0938c541
MD
651 * function if you want that behavior. Should be paired with
652 * free_all_cpu_call_rcu_data() to teardown these call_rcu worker
653 * threads.
b57aee66
PM
654 */
655
656int create_all_cpu_call_rcu_data(unsigned long flags)
657{
658 int i;
659 struct call_rcu_data *crdp;
660 int ret;
661
662 call_rcu_lock(&call_rcu_mutex);
663 alloc_cpu_call_rcu_data();
664 call_rcu_unlock(&call_rcu_mutex);
5cfe81b7 665 if (cpus_array_len <= 0) {
b57aee66
PM
666 errno = EINVAL;
667 return -EINVAL;
668 }
669 if (per_cpu_call_rcu_data == NULL) {
670 errno = ENOMEM;
671 return -ENOMEM;
672 }
5cfe81b7 673 for (i = 0; i < cpus_array_len; i++) {
b57aee66
PM
674 call_rcu_lock(&call_rcu_mutex);
675 if (get_cpu_call_rcu_data(i)) {
676 call_rcu_unlock(&call_rcu_mutex);
677 continue;
678 }
c1d2c60b 679 crdp = __create_call_rcu_data(flags, i);
b57aee66
PM
680 if (crdp == NULL) {
681 call_rcu_unlock(&call_rcu_mutex);
682 errno = ENOMEM;
683 return -ENOMEM;
684 }
685 call_rcu_unlock(&call_rcu_mutex);
686 if ((ret = set_cpu_call_rcu_data(i, crdp)) != 0) {
356c8794
LJ
687 call_rcu_data_free(crdp);
688
689 /* it has been created by other thread */
690 if (ret == -EEXIST)
691 continue;
692
693 return ret;
b57aee66
PM
694 }
695 }
696 return 0;
697}
698
7106ddf8
PM
699/*
700 * Wake up the call_rcu thread corresponding to the specified
701 * call_rcu_data structure.
702 */
703static void wake_call_rcu_thread(struct call_rcu_data *crdp)
704{
263e3cf9
MD
705 if (!(_CMM_LOAD_SHARED(crdp->flags) & URCU_CALL_RCU_RT))
706 call_rcu_wake_up(crdp);
7106ddf8
PM
707}
708
b7f721d9
MD
709static void _call_rcu(struct rcu_head *head,
710 void (*func)(struct rcu_head *head),
711 struct call_rcu_data *crdp)
712{
713 cds_wfcq_node_init(&head->next);
714 head->func = func;
715 cds_wfcq_enqueue(&crdp->cbs_head, &crdp->cbs_tail, &head->next);
716 uatomic_inc(&crdp->qlen);
717 wake_call_rcu_thread(crdp);
718}
719
b57aee66
PM
720/*
721 * Schedule a function to be invoked after a following grace period.
722 * This is the only function that must be called -- the others are
723 * only present to allow applications to tune their use of RCU for
724 * maximum performance.
725 *
726 * Note that unless a call_rcu thread has not already been created,
727 * the first invocation of call_rcu() will create one. So, if you
728 * need the first invocation of call_rcu() to be fast, make sure
729 * to create a call_rcu thread first. One way to accomplish this is
730 * "get_call_rcu_data();", and another is create_all_cpu_call_rcu_data().
618b2595
MD
731 *
732 * call_rcu must be called by registered RCU read-side threads.
b57aee66 733 */
b57aee66
PM
734void call_rcu(struct rcu_head *head,
735 void (*func)(struct rcu_head *head))
736{
737 struct call_rcu_data *crdp;
738
618b2595 739 /* Holding rcu read-side lock across use of per-cpu crdp */
ffb4aaa7 740 _rcu_read_lock();
b57aee66 741 crdp = get_call_rcu_data();
b7f721d9 742 _call_rcu(head, func, crdp);
ffb4aaa7 743 _rcu_read_unlock();
7106ddf8
PM
744}
745
746/*
747 * Free up the specified call_rcu_data structure, terminating the
748 * associated call_rcu thread. The caller must have previously
749 * removed the call_rcu_data structure from per-thread or per-CPU
750 * usage. For example, set_cpu_call_rcu_data(cpu, NULL) for per-CPU
751 * call_rcu_data structures or set_thread_call_rcu_data(NULL) for
752 * per-thread call_rcu_data structures.
753 *
754 * We silently refuse to free up the default call_rcu_data structure
755 * because that is where we put any leftover callbacks. Note that
756 * the possibility of self-spawning callbacks makes it impossible
757 * to execute all the callbacks in finite time without putting any
758 * newly spawned callbacks somewhere else. The "somewhere else" of
759 * last resort is the default call_rcu_data structure.
760 *
761 * We also silently refuse to free NULL pointers. This simplifies
762 * the calling code.
f9da0936
MD
763 *
764 * The caller must wait for a grace-period to pass between return from
765 * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the
766 * previous call rcu data as argument.
03e5118f
MD
767 *
768 * Note: introducing __cds_wfcq_splice_blocking() in this function fixed
769 * a list corruption bug in the 0.7.x series. The equivalent fix
770 * appeared in 0.6.8 for the stable-0.6 branch.
7106ddf8 771 */
1cf55ba4
MD
772static
773void _call_rcu_data_free(struct call_rcu_data *crdp, unsigned int flags)
7106ddf8 774{
7106ddf8
PM
775 if (crdp == NULL || crdp == default_call_rcu_data) {
776 return;
777 }
2870aa1e
PB
778 if ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0) {
779 uatomic_or(&crdp->flags, URCU_CALL_RCU_STOP);
7106ddf8 780 wake_call_rcu_thread(crdp);
2870aa1e 781 while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0)
109105b7 782 (void) poll(NULL, 0, 1);
7106ddf8 783 }
51b98c3c 784 call_rcu_lock(&call_rcu_mutex);
5161f31e 785 if (!cds_wfcq_empty(&crdp->cbs_head, &crdp->cbs_tail)) {
51b98c3c
PM
786 call_rcu_unlock(&call_rcu_mutex);
787 /* Create default call rcu data if need be. */
788 /* CBs queued here will be handed to the default list. */
698d0778 789 (void) get_default_call_rcu_data();
51b98c3c 790 call_rcu_lock(&call_rcu_mutex);
5161f31e
MD
791 __cds_wfcq_splice_blocking(&default_call_rcu_data->cbs_head,
792 &default_call_rcu_data->cbs_tail,
793 &crdp->cbs_head, &crdp->cbs_tail);
7106ddf8
PM
794 uatomic_add(&default_call_rcu_data->qlen,
795 uatomic_read(&crdp->qlen));
1e92aa15 796 wake_call_rcu_thread(default_call_rcu_data);
7106ddf8 797 }
59dc9e9d
LJ
798
799 cds_list_del(&crdp->list);
b75dffe6
LJ
800 call_rcu_unlock(&call_rcu_mutex);
801
1cf55ba4
MD
802 if (flags & CRDF_FLAG_JOIN_THREAD) {
803 int ret;
804
805 ret = pthread_join(get_call_rcu_thread(crdp), NULL);
806 if (ret)
807 urcu_die(ret);
808 }
59dc9e9d 809 free(crdp);
7106ddf8
PM
810}
811
1cf55ba4
MD
812void call_rcu_data_free(struct call_rcu_data *crdp)
813{
814 _call_rcu_data_free(crdp, CRDF_FLAG_JOIN_THREAD);
815}
816
7106ddf8
PM
817/*
818 * Clean up all the per-CPU call_rcu threads.
819 */
820void free_all_cpu_call_rcu_data(void)
821{
822 int cpu;
618b2595
MD
823 struct call_rcu_data **crdp;
824 static int warned = 0;
7106ddf8 825
5cfe81b7 826 if (cpus_array_len <= 0)
7106ddf8 827 return;
618b2595 828
5cfe81b7 829 crdp = malloc(sizeof(*crdp) * cpus_array_len);
618b2595
MD
830 if (!crdp) {
831 if (!warned) {
832 fprintf(stderr, "[error] liburcu: unable to allocate per-CPU pointer array\n");
833 }
834 warned = 1;
d31150b4 835 return;
618b2595
MD
836 }
837
5cfe81b7 838 for (cpu = 0; cpu < cpus_array_len; cpu++) {
618b2595
MD
839 crdp[cpu] = get_cpu_call_rcu_data(cpu);
840 if (crdp[cpu] == NULL)
7106ddf8
PM
841 continue;
842 set_cpu_call_rcu_data(cpu, NULL);
7106ddf8 843 }
618b2595
MD
844 /*
845 * Wait for call_rcu sites acting as RCU readers of the
846 * call_rcu_data to become quiescent.
847 */
848 synchronize_rcu();
5cfe81b7 849 for (cpu = 0; cpu < cpus_array_len; cpu++) {
618b2595
MD
850 if (crdp[cpu] == NULL)
851 continue;
852 call_rcu_data_free(crdp[cpu]);
853 }
854 free(crdp);
7106ddf8
PM
855}
856
81dd9134
KF
857static
858void free_completion(struct urcu_ref *ref)
859{
860 struct call_rcu_completion *completion;
861
862 completion = caa_container_of(ref, struct call_rcu_completion, ref);
863 free(completion);
864}
865
b7f721d9
MD
866static
867void _rcu_barrier_complete(struct rcu_head *head)
868{
869 struct call_rcu_completion_work *work;
870 struct call_rcu_completion *completion;
871
872 work = caa_container_of(head, struct call_rcu_completion_work, head);
873 completion = work->completion;
81dd9134
KF
874 if (!uatomic_sub_return(&completion->barrier_count, 1))
875 call_rcu_completion_wake_up(completion);
876 urcu_ref_put(&completion->ref, free_completion);
b7f721d9
MD
877 free(work);
878}
879
880/*
881 * Wait for all in-flight call_rcu callbacks to complete execution.
882 */
883void rcu_barrier(void)
884{
885 struct call_rcu_data *crdp;
81dd9134 886 struct call_rcu_completion *completion;
63f0fc6d 887 int count = 0;
b7f721d9
MD
888 int was_online;
889
890 /* Put in offline state in QSBR. */
ffb4aaa7 891 was_online = _rcu_read_ongoing();
b7f721d9
MD
892 if (was_online)
893 rcu_thread_offline();
894 /*
895 * Calling a rcu_barrier() within a RCU read-side critical
896 * section is an error.
897 */
ffb4aaa7 898 if (_rcu_read_ongoing()) {
b7f721d9
MD
899 static int warned = 0;
900
901 if (!warned) {
902 fprintf(stderr, "[error] liburcu: rcu_barrier() called from within RCU read-side critical section.\n");
903 }
904 warned = 1;
905 goto online;
906 }
907
81dd9134
KF
908 completion = calloc(sizeof(*completion), 1);
909 if (!completion)
910 urcu_die(errno);
911
b7f721d9
MD
912 call_rcu_lock(&call_rcu_mutex);
913 cds_list_for_each_entry(crdp, &call_rcu_data_list, list)
914 count++;
915
81dd9134
KF
916 /* Referenced by rcu_barrier() and each call_rcu thread. */
917 urcu_ref_set(&completion->ref, count + 1);
918 completion->barrier_count = count;
b7f721d9
MD
919
920 cds_list_for_each_entry(crdp, &call_rcu_data_list, list) {
921 struct call_rcu_completion_work *work;
922
923 work = calloc(sizeof(*work), 1);
63f0fc6d
MD
924 if (!work)
925 urcu_die(errno);
81dd9134 926 work->completion = completion;
b7f721d9 927 _call_rcu(&work->head, _rcu_barrier_complete, crdp);
b7f721d9
MD
928 }
929 call_rcu_unlock(&call_rcu_mutex);
930
b7f721d9
MD
931 /* Wait for them */
932 for (;;) {
81dd9134 933 uatomic_dec(&completion->futex);
b7f721d9
MD
934 /* Decrement futex before reading barrier_count */
935 cmm_smp_mb();
81dd9134 936 if (!uatomic_read(&completion->barrier_count))
b7f721d9 937 break;
81dd9134 938 call_rcu_completion_wait(completion);
b7f721d9 939 }
81dd9134
KF
940
941 urcu_ref_put(&completion->ref, free_completion);
942
b7f721d9
MD
943online:
944 if (was_online)
945 rcu_thread_online();
946}
947
81ad2e19
PM
948/*
949 * Acquire the call_rcu_mutex in order to ensure that the child sees
e85451a1
MD
950 * all of the call_rcu() data structures in a consistent state. Ensure
951 * that all call_rcu threads are in a quiescent state across fork.
81ad2e19
PM
952 * Suitable for pthread_atfork() and friends.
953 */
954void call_rcu_before_fork(void)
955{
e85451a1 956 struct call_rcu_data *crdp;
d0ec0ed2 957 struct urcu_atfork *atfork;
e85451a1 958
81ad2e19 959 call_rcu_lock(&call_rcu_mutex);
e85451a1 960
d0ec0ed2
MD
961 atfork = registered_rculfhash_atfork;
962 if (atfork)
963 atfork->before_fork(atfork->priv);
964
e85451a1
MD
965 cds_list_for_each_entry(crdp, &call_rcu_data_list, list) {
966 uatomic_or(&crdp->flags, URCU_CALL_RCU_PAUSE);
967 cmm_smp_mb__after_uatomic_or();
968 wake_call_rcu_thread(crdp);
969 }
970 cds_list_for_each_entry(crdp, &call_rcu_data_list, list) {
971 while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_PAUSED) == 0)
109105b7 972 (void) poll(NULL, 0, 1);
e85451a1 973 }
81ad2e19
PM
974}
975
976/*
977 * Clean up call_rcu data structures in the parent of a successful fork()
978 * that is not followed by exec() in the child. Suitable for
979 * pthread_atfork() and friends.
980 */
981void call_rcu_after_fork_parent(void)
982{
e85451a1 983 struct call_rcu_data *crdp;
d0ec0ed2 984 struct urcu_atfork *atfork;
e85451a1
MD
985
986 cds_list_for_each_entry(crdp, &call_rcu_data_list, list)
987 uatomic_and(&crdp->flags, ~URCU_CALL_RCU_PAUSE);
fc236e5e
KF
988 cds_list_for_each_entry(crdp, &call_rcu_data_list, list) {
989 while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_PAUSED) != 0)
109105b7 990 (void) poll(NULL, 0, 1);
fc236e5e 991 }
d0ec0ed2
MD
992 atfork = registered_rculfhash_atfork;
993 if (atfork)
994 atfork->after_fork_parent(atfork->priv);
81ad2e19
PM
995 call_rcu_unlock(&call_rcu_mutex);
996}
997
7106ddf8
PM
998/*
999 * Clean up call_rcu data structures in the child of a successful fork()
81ad2e19
PM
1000 * that is not followed by exec(). Suitable for pthread_atfork() and
1001 * friends.
7106ddf8
PM
1002 */
1003void call_rcu_after_fork_child(void)
1004{
077ff173 1005 struct call_rcu_data *crdp, *next;
d0ec0ed2 1006 struct urcu_atfork *atfork;
7106ddf8 1007
81ad2e19
PM
1008 /* Release the mutex. */
1009 call_rcu_unlock(&call_rcu_mutex);
1010
d0ec0ed2
MD
1011 atfork = registered_rculfhash_atfork;
1012 if (atfork)
1013 atfork->after_fork_child(atfork->priv);
1014
ad1b9909
LJ
1015 /* Do nothing when call_rcu() has not been used */
1016 if (cds_list_empty(&call_rcu_data_list))
1017 return;
1018
7106ddf8
PM
1019 /*
1020 * Allocate a new default call_rcu_data structure in order
1021 * to get a working call_rcu thread to go with it.
1022 */
1023 default_call_rcu_data = NULL;
1024 (void)get_default_call_rcu_data();
1025
60af049d 1026 /* Cleanup call_rcu_data pointers before use */
5cfe81b7 1027 cpus_array_len_reset();
60af049d 1028 free(per_cpu_call_rcu_data);
618b2595 1029 rcu_set_pointer(&per_cpu_call_rcu_data, NULL);
bd252a04 1030 URCU_TLS(thread_call_rcu_data) = NULL;
60af049d 1031
e85451a1
MD
1032 /*
1033 * Dispose of all of the rest of the call_rcu_data structures.
1034 * Leftover call_rcu callbacks will be merged into the new
1035 * default call_rcu thread queue.
1036 */
077ff173 1037 cds_list_for_each_entry_safe(crdp, next, &call_rcu_data_list, list) {
7106ddf8 1038 if (crdp == default_call_rcu_data)
077ff173 1039 continue;
2870aa1e 1040 uatomic_set(&crdp->flags, URCU_CALL_RCU_STOPPED);
1cf55ba4
MD
1041 /*
1042 * Do not join the thread because it does not exist in
1043 * the child.
1044 */
1045 _call_rcu_data_free(crdp, 0);
b57aee66
PM
1046 }
1047}
d0ec0ed2
MD
1048
1049void urcu_register_rculfhash_atfork(struct urcu_atfork *atfork)
1050{
b047e7a7
MD
1051 if (CMM_LOAD_SHARED(registered_rculfhash_atfork))
1052 return;
d0ec0ed2 1053 call_rcu_lock(&call_rcu_mutex);
b047e7a7
MD
1054 if (!registered_rculfhash_atfork)
1055 registered_rculfhash_atfork = atfork;
d0ec0ed2
MD
1056 call_rcu_unlock(&call_rcu_mutex);
1057}
1058
b047e7a7
MD
1059/*
1060 * This unregistration function is deprecated, meant only for internal
1061 * use by rculfhash.
1062 */
1063__attribute__((noreturn))
70469b43 1064void urcu_unregister_rculfhash_atfork(struct urcu_atfork *atfork __attribute__((unused)))
d0ec0ed2 1065{
b047e7a7 1066 urcu_die(EPERM);
d0ec0ed2 1067}
This page took 0.114886 seconds and 4 git commands to generate.