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