Fix: call_rcu: teardown default call_rcu worker on application exit
[urcu.git] / src / urcu-bp.c
CommitLineData
fdee2e6d
MD
1/*
2 * urcu-bp.c
3 *
4 * Userspace RCU library, "bulletproof" version.
5 *
6982d6d7 6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fdee2e6d
MD
7 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
24 */
25
e37faee1 26#define URCU_NO_COMPAT_IDENTIFIERS
71c811bf 27#define _LGPL_SOURCE
fdee2e6d
MD
28#include <stdio.h>
29#include <pthread.h>
30#include <signal.h>
31#include <assert.h>
32#include <stdlib.h>
33#include <string.h>
34#include <errno.h>
35#include <poll.h>
36#include <unistd.h>
3745305b 37#include <stdbool.h>
fdee2e6d
MD
38#include <sys/mman.h>
39
c6bc6503 40#include <urcu/config.h>
4477a870
MD
41#include <urcu/arch.h>
42#include <urcu/wfcqueue.h>
43#include <urcu/map/urcu-bp.h>
44#include <urcu/static/urcu-bp.h>
45#include <urcu/pointer.h>
46#include <urcu/tls-compat.h>
71c811bf 47
4a6d7378 48#include "urcu-die.h"
ce28e67a 49#include "urcu-utils.h"
4a6d7378 50
4477a870 51#define URCU_API_MAP
fdee2e6d 52/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
71c811bf 53#undef _LGPL_SOURCE
4477a870 54#include <urcu/urcu-bp.h>
71c811bf 55#define _LGPL_SOURCE
fdee2e6d 56
4c1ae2ea
MD
57#ifndef MAP_ANONYMOUS
58#define MAP_ANONYMOUS MAP_ANON
59#endif
60
c7eaf61c
MD
61#ifdef __linux__
62static
63void *mremap_wrapper(void *old_address, size_t old_size,
64 size_t new_size, int flags)
65{
66 return mremap(old_address, old_size, new_size, flags);
67}
68#else
45a4872f
MD
69
70#define MREMAP_MAYMOVE 1
71#define MREMAP_FIXED 2
72
73/*
95b94246 74 * mremap wrapper for non-Linux systems not allowing MAYMOVE.
45a4872f
MD
75 * This is not generic.
76*/
c7eaf61c 77static
0d657320
MJ
78void *mremap_wrapper(void *old_address __attribute__((unused)),
79 size_t old_size __attribute__((unused)),
80 size_t new_size __attribute__((unused)),
81 int flags)
45a4872f 82{
95b94246
MD
83 assert(!(flags & MREMAP_MAYMOVE));
84
85 return MAP_FAILED;
45a4872f
MD
86}
87#endif
88
9340c38d
MD
89/* Sleep delay in ms */
90#define RCU_SLEEP_DELAY_MS 10
95b94246
MD
91#define INIT_NR_THREADS 8
92#define ARENA_INIT_ALLOC \
93 sizeof(struct registry_chunk) \
4477a870 94 + INIT_NR_THREADS * sizeof(struct urcu_bp_reader)
fdee2e6d 95
b7b6a8f5
PB
96/*
97 * Active attempts to check for reader Q.S. before calling sleep().
98 */
99#define RCU_QS_ACTIVE_ATTEMPTS 100
100
76d6a951 101static
4477a870 102int urcu_bp_refcount;
76d6a951 103
999991c6
MD
104/* If the headers do not support membarrier system call, fall back smp_mb. */
105#ifdef __NR_membarrier
106# define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__)
f541831e
MD
107#else
108# define membarrier(...) -ENOSYS
109#endif
110
111enum membarrier_cmd {
3745305b
MD
112 MEMBARRIER_CMD_QUERY = 0,
113 MEMBARRIER_CMD_SHARED = (1 << 0),
114 /* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */
115 /* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */
116 MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 << 3),
117 MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED = (1 << 4),
f541831e
MD
118};
119
c1be8fb9 120static
4477a870 121void __attribute__((constructor)) _urcu_bp_init(void);
c1be8fb9 122static
4477a870 123void __attribute__((destructor)) urcu_bp_exit(void);
457eeeee 124static void urcu_call_rcu_exit(void);
fdee2e6d 125
d8d9a340 126#ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER
f541831e 127int urcu_bp_has_sys_membarrier;
d8d9a340 128#endif
f541831e 129
731ccb96
MD
130/*
131 * rcu_gp_lock ensures mutual exclusion between threads calling
132 * synchronize_rcu().
133 */
6abb4bd5 134static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
731ccb96
MD
135/*
136 * rcu_registry_lock ensures mutual exclusion between threads
137 * registering and unregistering themselves to/from the registry, and
138 * with threads reading that registry from synchronize_rcu(). However,
139 * this lock is not held all the way through the completion of awaiting
140 * for the grace period. It is sporadically released between iterations
141 * on the registry.
142 * rcu_registry_lock may nest inside rcu_gp_lock.
143 */
144static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER;
fdee2e6d 145
c1be8fb9
MD
146static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
147static int initialized;
148
149static pthread_key_t urcu_bp_key;
150
4477a870 151struct urcu_bp_gp urcu_bp_gp = { .ctr = URCU_BP_GP_COUNT };
ce28e67a 152URCU_ATTR_ALIAS("urcu_bp_gp") extern struct urcu_bp_gp rcu_gp_bp;
fdee2e6d
MD
153
154/*
155 * Pointer to registry elements. Written to only by each individual reader. Read
156 * by both the reader and the writers.
157 */
4477a870 158DEFINE_URCU_TLS(struct urcu_bp_reader *, urcu_bp_reader);
99bfa9e9 159DEFINE_URCU_TLS_ALIAS(struct urcu_bp_reader *, urcu_bp_reader, rcu_reader_bp);
fdee2e6d 160
16aa9ee8 161static CDS_LIST_HEAD(registry);
fdee2e6d 162
95b94246
MD
163struct registry_chunk {
164 size_t data_len; /* data length */
c1be8fb9 165 size_t used; /* amount of data used */
95b94246
MD
166 struct cds_list_head node; /* chunk_list node */
167 char data[];
168};
169
fdee2e6d 170struct registry_arena {
95b94246 171 struct cds_list_head chunk_list;
fdee2e6d
MD
172};
173
95b94246
MD
174static struct registry_arena registry_arena = {
175 .chunk_list = CDS_LIST_HEAD_INIT(registry_arena.chunk_list),
176};
fdee2e6d 177
4cf1675f
MD
178/* Saved fork signal mask, protected by rcu_gp_lock */
179static sigset_t saved_fork_signal_mask;
180
6abb4bd5 181static void mutex_lock(pthread_mutex_t *mutex)
fdee2e6d
MD
182{
183 int ret;
184
185#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 186 ret = pthread_mutex_lock(mutex);
4a6d7378
MD
187 if (ret)
188 urcu_die(ret);
fdee2e6d 189#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 190 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
4a6d7378
MD
191 if (ret != EBUSY && ret != EINTR)
192 urcu_die(ret);
fdee2e6d
MD
193 poll(NULL,0,10);
194 }
195#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
196}
197
6abb4bd5 198static void mutex_unlock(pthread_mutex_t *mutex)
fdee2e6d
MD
199{
200 int ret;
201
6abb4bd5 202 ret = pthread_mutex_unlock(mutex);
4a6d7378
MD
203 if (ret)
204 urcu_die(ret);
fdee2e6d
MD
205}
206
f541831e
MD
207static void smp_mb_master(void)
208{
3745305b
MD
209 if (caa_likely(urcu_bp_has_sys_membarrier)) {
210 if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0))
211 urcu_die(errno);
212 } else {
f541831e 213 cmm_smp_mb();
3745305b 214 }
f541831e
MD
215}
216
731ccb96
MD
217/*
218 * Always called with rcu_registry lock held. Releases this lock between
219 * iterations and grabs it again. Holds the lock when it returns.
220 */
52c75091
MD
221static void wait_for_readers(struct cds_list_head *input_readers,
222 struct cds_list_head *cur_snap_readers,
223 struct cds_list_head *qsreaders)
fdee2e6d 224{
9340c38d 225 unsigned int wait_loops = 0;
4477a870 226 struct urcu_bp_reader *index, *tmp;
fdee2e6d 227
fdee2e6d 228 /*
4477a870 229 * Wait for each thread URCU_TLS(urcu_bp_reader).ctr to either
dd61d077 230 * indicate quiescence (not nested), or observe the current
c13c2e55 231 * rcu_gp.ctr value.
fdee2e6d
MD
232 */
233 for (;;) {
9340c38d
MD
234 if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
235 wait_loops++;
236
52c75091 237 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
4477a870
MD
238 switch (urcu_bp_reader_state(&index->ctr)) {
239 case URCU_BP_READER_ACTIVE_CURRENT:
52c75091
MD
240 if (cur_snap_readers) {
241 cds_list_move(&index->node,
242 cur_snap_readers);
243 break;
244 }
245 /* Fall-through */
4477a870 246 case URCU_BP_READER_INACTIVE:
52c75091
MD
247 cds_list_move(&index->node, qsreaders);
248 break;
4477a870 249 case URCU_BP_READER_ACTIVE_OLD:
52c75091
MD
250 /*
251 * Old snapshot. Leaving node in
252 * input_readers will make us busy-loop
253 * until the snapshot becomes current or
254 * the reader becomes inactive.
255 */
256 break;
257 }
fdee2e6d
MD
258 }
259
52c75091 260 if (cds_list_empty(input_readers)) {
fdee2e6d
MD
261 break;
262 } else {
731ccb96
MD
263 /* Temporarily unlock the registry lock. */
264 mutex_unlock(&rcu_registry_lock);
9340c38d
MD
265 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS)
266 (void) poll(NULL, 0, RCU_SLEEP_DELAY_MS);
fdee2e6d 267 else
06f22bdb 268 caa_cpu_relax();
731ccb96
MD
269 /* Re-lock the registry lock before the next loop. */
270 mutex_lock(&rcu_registry_lock);
fdee2e6d
MD
271 }
272 }
fdee2e6d
MD
273}
274
4477a870 275void urcu_bp_synchronize_rcu(void)
fdee2e6d 276{
52c75091
MD
277 CDS_LIST_HEAD(cur_snap_readers);
278 CDS_LIST_HEAD(qsreaders);
fdee2e6d
MD
279 sigset_t newmask, oldmask;
280 int ret;
281
6ed4b2e6 282 ret = sigfillset(&newmask);
fdee2e6d 283 assert(!ret);
6ed4b2e6 284 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
fdee2e6d
MD
285 assert(!ret);
286
6abb4bd5 287 mutex_lock(&rcu_gp_lock);
fdee2e6d 288
731ccb96
MD
289 mutex_lock(&rcu_registry_lock);
290
16aa9ee8 291 if (cds_list_empty(&registry))
2dfb8b5e 292 goto out;
fdee2e6d
MD
293
294 /* All threads should read qparity before accessing data structure
2dfb8b5e 295 * where new ptr points to. */
fdee2e6d 296 /* Write new ptr before changing the qparity */
f541831e 297 smp_mb_master();
fdee2e6d 298
fdee2e6d 299 /*
dd61d077 300 * Wait for readers to observe original parity or be quiescent.
731ccb96
MD
301 * wait_for_readers() can release and grab again rcu_registry_lock
302 * interally.
dd61d077 303 */
52c75091 304 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
dd61d077
MD
305
306 /*
307 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
308 * model easier to understand. It does not have a big performance impact
309 * anyway, given this is the write-side.
310 */
311 cmm_smp_mb();
312
313 /* Switch parity: 0 -> 1, 1 -> 0 */
4477a870 314 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ URCU_BP_GP_CTR_PHASE);
dd61d077
MD
315
316 /*
317 * Must commit qparity update to memory before waiting for other parity
318 * quiescent state. Failure to do so could result in the writer waiting
319 * forever while new readers are always accessing data (no progress).
320 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
fdee2e6d 321 */
fdee2e6d
MD
322
323 /*
5481ddb3 324 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
fdee2e6d
MD
325 * model easier to understand. It does not have a big performance impact
326 * anyway, given this is the write-side.
327 */
5481ddb3 328 cmm_smp_mb();
fdee2e6d 329
fdee2e6d 330 /*
dd61d077 331 * Wait for readers to observe new parity or be quiescent.
731ccb96
MD
332 * wait_for_readers() can release and grab again rcu_registry_lock
333 * interally.
fdee2e6d 334 */
52c75091
MD
335 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
336
337 /*
338 * Put quiescent reader list back into registry.
339 */
340 cds_list_splice(&qsreaders, &registry);
fdee2e6d
MD
341
342 /*
2dfb8b5e
MD
343 * Finish waiting for reader threads before letting the old ptr being
344 * freed.
fdee2e6d 345 */
f541831e 346 smp_mb_master();
2dfb8b5e 347out:
731ccb96 348 mutex_unlock(&rcu_registry_lock);
6abb4bd5 349 mutex_unlock(&rcu_gp_lock);
fdee2e6d
MD
350 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
351 assert(!ret);
352}
ce28e67a 353URCU_ATTR_ALIAS("urcu_bp_synchronize_rcu") void synchronize_rcu_bp();
fdee2e6d
MD
354
355/*
356 * library wrappers to be used by non-LGPL compatible source code.
357 */
358
4477a870 359void urcu_bp_read_lock(void)
fdee2e6d 360{
4477a870 361 _urcu_bp_read_lock();
fdee2e6d 362}
ce28e67a 363URCU_ATTR_ALIAS("urcu_bp_read_lock") void rcu_read_lock_bp();
fdee2e6d 364
4477a870 365void urcu_bp_read_unlock(void)
fdee2e6d 366{
4477a870 367 _urcu_bp_read_unlock();
fdee2e6d 368}
ce28e67a 369URCU_ATTR_ALIAS("urcu_bp_read_unlock") void rcu_read_unlock_bp();
fdee2e6d 370
4477a870 371int urcu_bp_read_ongoing(void)
882f3357 372{
4477a870 373 return _urcu_bp_read_ongoing();
882f3357 374}
ce28e67a 375URCU_ATTR_ALIAS("urcu_bp_read_ongoing") int rcu_read_ongoing_bp();
882f3357 376
fdee2e6d 377/*
95b94246
MD
378 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
379 * Else, try expanding the last chunk. If this fails, allocate a new
380 * chunk twice as big as the last chunk.
381 * Memory used by chunks _never_ moves. A chunk could theoretically be
382 * freed when all "used" slots are released, but we don't do it at this
383 * point.
fdee2e6d 384 */
95b94246
MD
385static
386void expand_arena(struct registry_arena *arena)
fdee2e6d 387{
95b94246
MD
388 struct registry_chunk *new_chunk, *last_chunk;
389 size_t old_chunk_len, new_chunk_len;
390
391 /* No chunk. */
392 if (cds_list_empty(&arena->chunk_list)) {
393 assert(ARENA_INIT_ALLOC >=
394 sizeof(struct registry_chunk)
395 + sizeof(struct rcu_reader));
396 new_chunk_len = ARENA_INIT_ALLOC;
5592d049
MJ
397 new_chunk = (struct registry_chunk *) mmap(NULL,
398 new_chunk_len,
9d8612b7
MD
399 PROT_READ | PROT_WRITE,
400 MAP_ANONYMOUS | MAP_PRIVATE,
401 -1, 0);
95b94246
MD
402 if (new_chunk == MAP_FAILED)
403 abort();
d3ac5bb7 404 memset(new_chunk, 0, new_chunk_len);
95b94246
MD
405 new_chunk->data_len =
406 new_chunk_len - sizeof(struct registry_chunk);
407 cds_list_add_tail(&new_chunk->node, &arena->chunk_list);
408 return; /* We're done. */
409 }
9d8612b7 410
95b94246
MD
411 /* Try expanding last chunk. */
412 last_chunk = cds_list_entry(arena->chunk_list.prev,
413 struct registry_chunk, node);
414 old_chunk_len =
415 last_chunk->data_len + sizeof(struct registry_chunk);
416 new_chunk_len = old_chunk_len << 1;
417
418 /* Don't allow memory mapping to move, just expand. */
419 new_chunk = mremap_wrapper(last_chunk, old_chunk_len,
420 new_chunk_len, 0);
421 if (new_chunk != MAP_FAILED) {
422 /* Should not have moved. */
423 assert(new_chunk == last_chunk);
d3ac5bb7 424 memset((char *) last_chunk + old_chunk_len, 0,
95b94246
MD
425 new_chunk_len - old_chunk_len);
426 last_chunk->data_len =
427 new_chunk_len - sizeof(struct registry_chunk);
428 return; /* We're done. */
429 }
0617bf4c 430
95b94246 431 /* Remap did not succeed, we need to add a new chunk. */
5592d049
MJ
432 new_chunk = (struct registry_chunk *) mmap(NULL,
433 new_chunk_len,
95b94246
MD
434 PROT_READ | PROT_WRITE,
435 MAP_ANONYMOUS | MAP_PRIVATE,
436 -1, 0);
437 if (new_chunk == MAP_FAILED)
438 abort();
d3ac5bb7 439 memset(new_chunk, 0, new_chunk_len);
95b94246
MD
440 new_chunk->data_len =
441 new_chunk_len - sizeof(struct registry_chunk);
442 cds_list_add_tail(&new_chunk->node, &arena->chunk_list);
443}
fdee2e6d 444
95b94246
MD
445static
446struct rcu_reader *arena_alloc(struct registry_arena *arena)
447{
448 struct registry_chunk *chunk;
449 struct rcu_reader *rcu_reader_reg;
450 int expand_done = 0; /* Only allow to expand once per alloc */
451 size_t len = sizeof(struct rcu_reader);
452
453retry:
454 cds_list_for_each_entry(chunk, &arena->chunk_list, node) {
455 if (chunk->data_len - chunk->used < len)
456 continue;
457 /* Find spot */
458 for (rcu_reader_reg = (struct rcu_reader *) &chunk->data[0];
459 rcu_reader_reg < (struct rcu_reader *) &chunk->data[chunk->data_len];
460 rcu_reader_reg++) {
461 if (!rcu_reader_reg->alloc) {
462 rcu_reader_reg->alloc = 1;
463 chunk->used += len;
464 return rcu_reader_reg;
465 }
466 }
467 }
468
469 if (!expand_done) {
470 expand_arena(arena);
471 expand_done = 1;
472 goto retry;
473 }
474
475 return NULL;
fdee2e6d
MD
476}
477
478/* Called with signals off and mutex locked */
95b94246
MD
479static
480void add_thread(void)
fdee2e6d 481{
02be5561 482 struct rcu_reader *rcu_reader_reg;
c1be8fb9 483 int ret;
fdee2e6d 484
95b94246
MD
485 rcu_reader_reg = arena_alloc(&registry_arena);
486 if (!rcu_reader_reg)
487 abort();
c1be8fb9
MD
488 ret = pthread_setspecific(urcu_bp_key, rcu_reader_reg);
489 if (ret)
490 abort();
fdee2e6d
MD
491
492 /* Add to registry */
02be5561
MD
493 rcu_reader_reg->tid = pthread_self();
494 assert(rcu_reader_reg->ctr == 0);
16aa9ee8 495 cds_list_add(&rcu_reader_reg->node, &registry);
95b94246
MD
496 /*
497 * Reader threads are pointing to the reader registry. This is
498 * why its memory should never be relocated.
499 */
4477a870 500 URCU_TLS(urcu_bp_reader) = rcu_reader_reg;
fdee2e6d
MD
501}
502
c1be8fb9
MD
503/* Called with mutex locked */
504static
505void cleanup_thread(struct registry_chunk *chunk,
506 struct rcu_reader *rcu_reader_reg)
507{
508 rcu_reader_reg->ctr = 0;
509 cds_list_del(&rcu_reader_reg->node);
510 rcu_reader_reg->tid = 0;
511 rcu_reader_reg->alloc = 0;
512 chunk->used -= sizeof(struct rcu_reader);
513}
514
515static
516struct registry_chunk *find_chunk(struct rcu_reader *rcu_reader_reg)
fdee2e6d 517{
95b94246 518 struct registry_chunk *chunk;
fdee2e6d 519
95b94246 520 cds_list_for_each_entry(chunk, &registry_arena.chunk_list, node) {
c1be8fb9
MD
521 if (rcu_reader_reg < (struct rcu_reader *) &chunk->data[0])
522 continue;
523 if (rcu_reader_reg >= (struct rcu_reader *) &chunk->data[chunk->data_len])
524 continue;
525 return chunk;
526 }
527 return NULL;
528}
95b94246 529
c1be8fb9
MD
530/* Called with signals off and mutex locked */
531static
76d6a951 532void remove_thread(struct rcu_reader *rcu_reader_reg)
c1be8fb9 533{
c1be8fb9 534 cleanup_thread(find_chunk(rcu_reader_reg), rcu_reader_reg);
4477a870 535 URCU_TLS(urcu_bp_reader) = NULL;
fdee2e6d
MD
536}
537
538/* Disable signals, take mutex, add to registry */
4477a870 539void urcu_bp_register(void)
fdee2e6d
MD
540{
541 sigset_t newmask, oldmask;
542 int ret;
543
6ed4b2e6 544 ret = sigfillset(&newmask);
c1be8fb9
MD
545 if (ret)
546 abort();
6ed4b2e6 547 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
c1be8fb9
MD
548 if (ret)
549 abort();
fdee2e6d
MD
550
551 /*
552 * Check if a signal concurrently registered our thread since
c1be8fb9
MD
553 * the check in rcu_read_lock().
554 */
4477a870 555 if (URCU_TLS(urcu_bp_reader))
fdee2e6d
MD
556 goto end;
557
c1be8fb9
MD
558 /*
559 * Take care of early registration before urcu_bp constructor.
560 */
4477a870 561 _urcu_bp_init();
c1be8fb9 562
731ccb96 563 mutex_lock(&rcu_registry_lock);
fdee2e6d 564 add_thread();
731ccb96 565 mutex_unlock(&rcu_registry_lock);
fdee2e6d
MD
566end:
567 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
c1be8fb9
MD
568 if (ret)
569 abort();
570}
ce28e67a 571URCU_ATTR_ALIAS("urcu_bp_register") void rcu_bp_register();
c1be8fb9 572
5b46e39d
MD
573void urcu_bp_register_thread(void)
574{
575 if (caa_unlikely(!URCU_TLS(urcu_bp_reader)))
576 urcu_bp_register(); /* If not yet registered. */
577}
578
c1be8fb9
MD
579/* Disable signals, take mutex, remove from registry */
580static
4477a870 581void urcu_bp_unregister(struct rcu_reader *rcu_reader_reg)
c1be8fb9
MD
582{
583 sigset_t newmask, oldmask;
584 int ret;
585
586 ret = sigfillset(&newmask);
587 if (ret)
588 abort();
589 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
590 if (ret)
591 abort();
592
731ccb96 593 mutex_lock(&rcu_registry_lock);
76d6a951 594 remove_thread(rcu_reader_reg);
731ccb96 595 mutex_unlock(&rcu_registry_lock);
c1be8fb9
MD
596 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
597 if (ret)
598 abort();
4477a870 599 urcu_bp_exit();
c1be8fb9
MD
600}
601
602/*
603 * Remove thread from the registry when it exits, and flag it as
604 * destroyed so garbage collection can take care of it.
605 */
606static
607void urcu_bp_thread_exit_notifier(void *rcu_key)
608{
4477a870 609 urcu_bp_unregister(rcu_key);
c1be8fb9
MD
610}
611
d8d9a340
MD
612#ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER
613static
4477a870 614void urcu_bp_sys_membarrier_status(bool available)
d8d9a340
MD
615{
616 if (!available)
617 abort();
618}
619#else
620static
4477a870 621void urcu_bp_sys_membarrier_status(bool available)
d8d9a340 622{
3745305b
MD
623 if (!available)
624 return;
625 urcu_bp_has_sys_membarrier = 1;
d8d9a340
MD
626}
627#endif
628
3745305b 629static
4477a870 630void urcu_bp_sys_membarrier_init(void)
3745305b
MD
631{
632 bool available = false;
633 int mask;
634
635 mask = membarrier(MEMBARRIER_CMD_QUERY, 0);
636 if (mask >= 0) {
637 if (mask & MEMBARRIER_CMD_PRIVATE_EXPEDITED) {
638 if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0))
639 urcu_die(errno);
640 available = true;
641 }
642 }
4477a870 643 urcu_bp_sys_membarrier_status(available);
3745305b
MD
644}
645
c1be8fb9 646static
4477a870 647void _urcu_bp_init(void)
c1be8fb9
MD
648{
649 mutex_lock(&init_lock);
4477a870 650 if (!urcu_bp_refcount++) {
c1be8fb9
MD
651 int ret;
652
653 ret = pthread_key_create(&urcu_bp_key,
654 urcu_bp_thread_exit_notifier);
655 if (ret)
656 abort();
4477a870 657 urcu_bp_sys_membarrier_init();
c1be8fb9
MD
658 initialized = 1;
659 }
660 mutex_unlock(&init_lock);
fdee2e6d
MD
661}
662
c1be8fb9 663static
4477a870 664void urcu_bp_exit(void)
fdee2e6d 665{
457eeeee
MD
666 urcu_call_rcu_exit();
667
76d6a951 668 mutex_lock(&init_lock);
4477a870 669 if (!--urcu_bp_refcount) {
76d6a951
MD
670 struct registry_chunk *chunk, *tmp;
671 int ret;
95b94246 672
76d6a951
MD
673 cds_list_for_each_entry_safe(chunk, tmp,
674 &registry_arena.chunk_list, node) {
5592d049 675 munmap((void *) chunk, chunk->data_len
76d6a951
MD
676 + sizeof(struct registry_chunk));
677 }
7937ae1c 678 CDS_INIT_LIST_HEAD(&registry_arena.chunk_list);
76d6a951
MD
679 ret = pthread_key_delete(urcu_bp_key);
680 if (ret)
681 abort();
95b94246 682 }
76d6a951 683 mutex_unlock(&init_lock);
fdee2e6d 684}
4cf1675f
MD
685
686/*
731ccb96
MD
687 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
688 * sure we fork() don't race with a concurrent thread executing with
689 * any of those locks held. This ensures that the registry and data
690 * protected by rcu_gp_lock are in a coherent state in the child.
4cf1675f 691 */
4477a870 692void urcu_bp_before_fork(void)
4cf1675f
MD
693{
694 sigset_t newmask, oldmask;
695 int ret;
696
6ed4b2e6 697 ret = sigfillset(&newmask);
4cf1675f 698 assert(!ret);
6ed4b2e6 699 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
4cf1675f
MD
700 assert(!ret);
701 mutex_lock(&rcu_gp_lock);
731ccb96 702 mutex_lock(&rcu_registry_lock);
4cf1675f
MD
703 saved_fork_signal_mask = oldmask;
704}
ce28e67a 705URCU_ATTR_ALIAS("urcu_bp_before_fork") void rcu_bp_before_fork();
4cf1675f 706
4477a870 707void urcu_bp_after_fork_parent(void)
4cf1675f
MD
708{
709 sigset_t oldmask;
710 int ret;
711
712 oldmask = saved_fork_signal_mask;
731ccb96 713 mutex_unlock(&rcu_registry_lock);
4cf1675f
MD
714 mutex_unlock(&rcu_gp_lock);
715 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
716 assert(!ret);
717}
ce28e67a 718URCU_ATTR_ALIAS("urcu_bp_after_fork_parent")
4477a870 719void rcu_bp_after_fork_parent(void);
4cf1675f 720
c1be8fb9
MD
721/*
722 * Prune all entries from registry except our own thread. Fits the Linux
731ccb96 723 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
c1be8fb9
MD
724 */
725static
726void urcu_bp_prune_registry(void)
727{
728 struct registry_chunk *chunk;
4477a870 729 struct urcu_bp_reader *rcu_reader_reg;
c1be8fb9
MD
730
731 cds_list_for_each_entry(chunk, &registry_arena.chunk_list, node) {
4477a870
MD
732 for (rcu_reader_reg = (struct urcu_bp_reader *) &chunk->data[0];
733 rcu_reader_reg < (struct urcu_bp_reader *) &chunk->data[chunk->data_len];
c1be8fb9
MD
734 rcu_reader_reg++) {
735 if (!rcu_reader_reg->alloc)
736 continue;
737 if (rcu_reader_reg->tid == pthread_self())
738 continue;
739 cleanup_thread(chunk, rcu_reader_reg);
740 }
741 }
742}
743
4477a870 744void urcu_bp_after_fork_child(void)
4cf1675f
MD
745{
746 sigset_t oldmask;
747 int ret;
748
c1be8fb9 749 urcu_bp_prune_registry();
4cf1675f 750 oldmask = saved_fork_signal_mask;
731ccb96 751 mutex_unlock(&rcu_registry_lock);
4cf1675f
MD
752 mutex_unlock(&rcu_gp_lock);
753 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
754 assert(!ret);
755}
ce28e67a 756URCU_ATTR_ALIAS("urcu_bp_after_fork_child")
4477a870 757void rcu_bp_after_fork_child(void);
5e77fc1f 758
4477a870 759void *urcu_bp_dereference_sym(void *p)
9b7981bb
MD
760{
761 return _rcu_dereference(p);
762}
ce28e67a 763URCU_ATTR_ALIAS("urcu_bp_dereference_sym")
4477a870 764void *rcu_dereference_sym_bp();
9b7981bb 765
4477a870 766void *urcu_bp_set_pointer_sym(void **p, void *v)
5efd3cd2
MD
767{
768 cmm_wmb();
424d4ed5
MD
769 uatomic_set(p, v);
770 return v;
5efd3cd2 771}
ce28e67a 772URCU_ATTR_ALIAS("urcu_bp_set_pointer_sym")
4477a870 773void *rcu_set_pointer_sym_bp();
5efd3cd2 774
4477a870 775void *urcu_bp_xchg_pointer_sym(void **p, void *v)
5efd3cd2
MD
776{
777 cmm_wmb();
778 return uatomic_xchg(p, v);
779}
ce28e67a 780URCU_ATTR_ALIAS("urcu_bp_xchg_pointer_sym")
4477a870 781void *rcu_xchg_pointer_sym_bp();
5efd3cd2 782
4477a870 783void *urcu_bp_cmpxchg_pointer_sym(void **p, void *old, void *_new)
5efd3cd2
MD
784{
785 cmm_wmb();
786 return uatomic_cmpxchg(p, old, _new);
787}
ce28e67a 788URCU_ATTR_ALIAS("urcu_bp_cmpxchg_pointer_sym")
4477a870 789void *rcu_cmpxchg_pointer_sym_bp();
5efd3cd2 790
5e6b23a6 791DEFINE_RCU_FLAVOR(rcu_flavor);
4477a870 792DEFINE_RCU_FLAVOR_ALIAS(rcu_flavor, alias_rcu_flavor);
541d828d 793
5e77fc1f 794#include "urcu-call-rcu-impl.h"
0376e7b2 795#include "urcu-defer-impl.h"
This page took 0.083154 seconds and 4 git commands to generate.