Fix: format string signedness
[urcu.git] / urcu-bp.c
... / ...
CommitLineData
1/*
2 * urcu-bp.c
3 *
4 * Userspace RCU library, "bulletproof" version.
5 *
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
26#define _GNU_SOURCE
27#define _LGPL_SOURCE
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>
37#include <sys/mman.h>
38
39#include "urcu/wfcqueue.h"
40#include "urcu/map/urcu-bp.h"
41#include "urcu/static/urcu-bp.h"
42#include "urcu-pointer.h"
43#include "urcu/tls-compat.h"
44
45#include "urcu-die.h"
46
47/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
48#undef _LGPL_SOURCE
49#include "urcu-bp.h"
50#define _LGPL_SOURCE
51
52#ifndef MAP_ANONYMOUS
53#define MAP_ANONYMOUS MAP_ANON
54#endif
55
56#ifdef __linux__
57static
58void *mremap_wrapper(void *old_address, size_t old_size,
59 size_t new_size, int flags)
60{
61 return mremap(old_address, old_size, new_size, flags);
62}
63#else
64
65#define MREMAP_MAYMOVE 1
66#define MREMAP_FIXED 2
67
68/*
69 * mremap wrapper for non-Linux systems not allowing MAYMOVE.
70 * This is not generic.
71*/
72static
73void *mremap_wrapper(void *old_address, size_t old_size,
74 size_t new_size, int flags)
75{
76 assert(!(flags & MREMAP_MAYMOVE));
77
78 return MAP_FAILED;
79}
80#endif
81
82/* Sleep delay in ms */
83#define RCU_SLEEP_DELAY_MS 10
84#define INIT_NR_THREADS 8
85#define ARENA_INIT_ALLOC \
86 sizeof(struct registry_chunk) \
87 + INIT_NR_THREADS * sizeof(struct rcu_reader)
88
89/*
90 * Active attempts to check for reader Q.S. before calling sleep().
91 */
92#define RCU_QS_ACTIVE_ATTEMPTS 100
93
94static
95int rcu_bp_refcount;
96
97static
98void __attribute__((constructor)) rcu_bp_init(void);
99static
100void __attribute__((destructor)) _rcu_bp_exit(void);
101
102/*
103 * rcu_gp_lock ensures mutual exclusion between threads calling
104 * synchronize_rcu().
105 */
106static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
107/*
108 * rcu_registry_lock ensures mutual exclusion between threads
109 * registering and unregistering themselves to/from the registry, and
110 * with threads reading that registry from synchronize_rcu(). However,
111 * this lock is not held all the way through the completion of awaiting
112 * for the grace period. It is sporadically released between iterations
113 * on the registry.
114 * rcu_registry_lock may nest inside rcu_gp_lock.
115 */
116static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER;
117
118static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
119static int initialized;
120
121static pthread_key_t urcu_bp_key;
122
123#ifdef DEBUG_YIELD
124unsigned int rcu_yield_active;
125__DEFINE_URCU_TLS_GLOBAL(unsigned int, rcu_rand_yield);
126#endif
127
128struct rcu_gp rcu_gp = { .ctr = RCU_GP_COUNT };
129
130/*
131 * Pointer to registry elements. Written to only by each individual reader. Read
132 * by both the reader and the writers.
133 */
134__DEFINE_URCU_TLS_GLOBAL(struct rcu_reader *, rcu_reader);
135
136static CDS_LIST_HEAD(registry);
137
138struct registry_chunk {
139 size_t data_len; /* data length */
140 size_t used; /* amount of data used */
141 struct cds_list_head node; /* chunk_list node */
142 char data[];
143};
144
145struct registry_arena {
146 struct cds_list_head chunk_list;
147};
148
149static struct registry_arena registry_arena = {
150 .chunk_list = CDS_LIST_HEAD_INIT(registry_arena.chunk_list),
151};
152
153/* Saved fork signal mask, protected by rcu_gp_lock */
154static sigset_t saved_fork_signal_mask;
155
156static void mutex_lock(pthread_mutex_t *mutex)
157{
158 int ret;
159
160#ifndef DISTRUST_SIGNALS_EXTREME
161 ret = pthread_mutex_lock(mutex);
162 if (ret)
163 urcu_die(ret);
164#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
165 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
166 if (ret != EBUSY && ret != EINTR)
167 urcu_die(ret);
168 poll(NULL,0,10);
169 }
170#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
171}
172
173static void mutex_unlock(pthread_mutex_t *mutex)
174{
175 int ret;
176
177 ret = pthread_mutex_unlock(mutex);
178 if (ret)
179 urcu_die(ret);
180}
181
182/*
183 * Always called with rcu_registry lock held. Releases this lock between
184 * iterations and grabs it again. Holds the lock when it returns.
185 */
186static void wait_for_readers(struct cds_list_head *input_readers,
187 struct cds_list_head *cur_snap_readers,
188 struct cds_list_head *qsreaders)
189{
190 unsigned int wait_loops = 0;
191 struct rcu_reader *index, *tmp;
192
193 /*
194 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
195 * indicate quiescence (not nested), or observe the current
196 * rcu_gp.ctr value.
197 */
198 for (;;) {
199 if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
200 wait_loops++;
201
202 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
203 switch (rcu_reader_state(&index->ctr)) {
204 case RCU_READER_ACTIVE_CURRENT:
205 if (cur_snap_readers) {
206 cds_list_move(&index->node,
207 cur_snap_readers);
208 break;
209 }
210 /* Fall-through */
211 case RCU_READER_INACTIVE:
212 cds_list_move(&index->node, qsreaders);
213 break;
214 case RCU_READER_ACTIVE_OLD:
215 /*
216 * Old snapshot. Leaving node in
217 * input_readers will make us busy-loop
218 * until the snapshot becomes current or
219 * the reader becomes inactive.
220 */
221 break;
222 }
223 }
224
225 if (cds_list_empty(input_readers)) {
226 break;
227 } else {
228 /* Temporarily unlock the registry lock. */
229 mutex_unlock(&rcu_registry_lock);
230 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS)
231 (void) poll(NULL, 0, RCU_SLEEP_DELAY_MS);
232 else
233 caa_cpu_relax();
234 /* Re-lock the registry lock before the next loop. */
235 mutex_lock(&rcu_registry_lock);
236 }
237 }
238}
239
240void synchronize_rcu(void)
241{
242 CDS_LIST_HEAD(cur_snap_readers);
243 CDS_LIST_HEAD(qsreaders);
244 sigset_t newmask, oldmask;
245 int ret;
246
247 ret = sigfillset(&newmask);
248 assert(!ret);
249 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
250 assert(!ret);
251
252 mutex_lock(&rcu_gp_lock);
253
254 mutex_lock(&rcu_registry_lock);
255
256 if (cds_list_empty(&registry))
257 goto out;
258
259 /* All threads should read qparity before accessing data structure
260 * where new ptr points to. */
261 /* Write new ptr before changing the qparity */
262 cmm_smp_mb();
263
264 /*
265 * Wait for readers to observe original parity or be quiescent.
266 * wait_for_readers() can release and grab again rcu_registry_lock
267 * interally.
268 */
269 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
270
271 /*
272 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
273 * model easier to understand. It does not have a big performance impact
274 * anyway, given this is the write-side.
275 */
276 cmm_smp_mb();
277
278 /* Switch parity: 0 -> 1, 1 -> 0 */
279 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ RCU_GP_CTR_PHASE);
280
281 /*
282 * Must commit qparity update to memory before waiting for other parity
283 * quiescent state. Failure to do so could result in the writer waiting
284 * forever while new readers are always accessing data (no progress).
285 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
286 */
287
288 /*
289 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
290 * model easier to understand. It does not have a big performance impact
291 * anyway, given this is the write-side.
292 */
293 cmm_smp_mb();
294
295 /*
296 * Wait for readers to observe new parity or be quiescent.
297 * wait_for_readers() can release and grab again rcu_registry_lock
298 * interally.
299 */
300 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
301
302 /*
303 * Put quiescent reader list back into registry.
304 */
305 cds_list_splice(&qsreaders, &registry);
306
307 /*
308 * Finish waiting for reader threads before letting the old ptr being
309 * freed.
310 */
311 cmm_smp_mb();
312out:
313 mutex_unlock(&rcu_registry_lock);
314 mutex_unlock(&rcu_gp_lock);
315 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
316 assert(!ret);
317}
318
319/*
320 * library wrappers to be used by non-LGPL compatible source code.
321 */
322
323void rcu_read_lock(void)
324{
325 _rcu_read_lock();
326}
327
328void rcu_read_unlock(void)
329{
330 _rcu_read_unlock();
331}
332
333int rcu_read_ongoing(void)
334{
335 return _rcu_read_ongoing();
336}
337
338/*
339 * Only grow for now. If empty, allocate a ARENA_INIT_ALLOC sized chunk.
340 * Else, try expanding the last chunk. If this fails, allocate a new
341 * chunk twice as big as the last chunk.
342 * Memory used by chunks _never_ moves. A chunk could theoretically be
343 * freed when all "used" slots are released, but we don't do it at this
344 * point.
345 */
346static
347void expand_arena(struct registry_arena *arena)
348{
349 struct registry_chunk *new_chunk, *last_chunk;
350 size_t old_chunk_len, new_chunk_len;
351
352 /* No chunk. */
353 if (cds_list_empty(&arena->chunk_list)) {
354 assert(ARENA_INIT_ALLOC >=
355 sizeof(struct registry_chunk)
356 + sizeof(struct rcu_reader));
357 new_chunk_len = ARENA_INIT_ALLOC;
358 new_chunk = mmap(NULL, new_chunk_len,
359 PROT_READ | PROT_WRITE,
360 MAP_ANONYMOUS | MAP_PRIVATE,
361 -1, 0);
362 if (new_chunk == MAP_FAILED)
363 abort();
364 bzero(new_chunk, new_chunk_len);
365 new_chunk->data_len =
366 new_chunk_len - sizeof(struct registry_chunk);
367 cds_list_add_tail(&new_chunk->node, &arena->chunk_list);
368 return; /* We're done. */
369 }
370
371 /* Try expanding last chunk. */
372 last_chunk = cds_list_entry(arena->chunk_list.prev,
373 struct registry_chunk, node);
374 old_chunk_len =
375 last_chunk->data_len + sizeof(struct registry_chunk);
376 new_chunk_len = old_chunk_len << 1;
377
378 /* Don't allow memory mapping to move, just expand. */
379 new_chunk = mremap_wrapper(last_chunk, old_chunk_len,
380 new_chunk_len, 0);
381 if (new_chunk != MAP_FAILED) {
382 /* Should not have moved. */
383 assert(new_chunk == last_chunk);
384 bzero((char *) last_chunk + old_chunk_len,
385 new_chunk_len - old_chunk_len);
386 last_chunk->data_len =
387 new_chunk_len - sizeof(struct registry_chunk);
388 return; /* We're done. */
389 }
390
391 /* Remap did not succeed, we need to add a new chunk. */
392 new_chunk = mmap(NULL, new_chunk_len,
393 PROT_READ | PROT_WRITE,
394 MAP_ANONYMOUS | MAP_PRIVATE,
395 -1, 0);
396 if (new_chunk == MAP_FAILED)
397 abort();
398 bzero(new_chunk, new_chunk_len);
399 new_chunk->data_len =
400 new_chunk_len - sizeof(struct registry_chunk);
401 cds_list_add_tail(&new_chunk->node, &arena->chunk_list);
402}
403
404static
405struct rcu_reader *arena_alloc(struct registry_arena *arena)
406{
407 struct registry_chunk *chunk;
408 struct rcu_reader *rcu_reader_reg;
409 int expand_done = 0; /* Only allow to expand once per alloc */
410 size_t len = sizeof(struct rcu_reader);
411
412retry:
413 cds_list_for_each_entry(chunk, &arena->chunk_list, node) {
414 if (chunk->data_len - chunk->used < len)
415 continue;
416 /* Find spot */
417 for (rcu_reader_reg = (struct rcu_reader *) &chunk->data[0];
418 rcu_reader_reg < (struct rcu_reader *) &chunk->data[chunk->data_len];
419 rcu_reader_reg++) {
420 if (!rcu_reader_reg->alloc) {
421 rcu_reader_reg->alloc = 1;
422 chunk->used += len;
423 return rcu_reader_reg;
424 }
425 }
426 }
427
428 if (!expand_done) {
429 expand_arena(arena);
430 expand_done = 1;
431 goto retry;
432 }
433
434 return NULL;
435}
436
437/* Called with signals off and mutex locked */
438static
439void add_thread(void)
440{
441 struct rcu_reader *rcu_reader_reg;
442 int ret;
443
444 rcu_reader_reg = arena_alloc(&registry_arena);
445 if (!rcu_reader_reg)
446 abort();
447 ret = pthread_setspecific(urcu_bp_key, rcu_reader_reg);
448 if (ret)
449 abort();
450
451 /* Add to registry */
452 rcu_reader_reg->tid = pthread_self();
453 assert(rcu_reader_reg->ctr == 0);
454 cds_list_add(&rcu_reader_reg->node, &registry);
455 /*
456 * Reader threads are pointing to the reader registry. This is
457 * why its memory should never be relocated.
458 */
459 URCU_TLS(rcu_reader) = rcu_reader_reg;
460}
461
462/* Called with mutex locked */
463static
464void cleanup_thread(struct registry_chunk *chunk,
465 struct rcu_reader *rcu_reader_reg)
466{
467 rcu_reader_reg->ctr = 0;
468 cds_list_del(&rcu_reader_reg->node);
469 rcu_reader_reg->tid = 0;
470 rcu_reader_reg->alloc = 0;
471 chunk->used -= sizeof(struct rcu_reader);
472}
473
474static
475struct registry_chunk *find_chunk(struct rcu_reader *rcu_reader_reg)
476{
477 struct registry_chunk *chunk;
478
479 cds_list_for_each_entry(chunk, &registry_arena.chunk_list, node) {
480 if (rcu_reader_reg < (struct rcu_reader *) &chunk->data[0])
481 continue;
482 if (rcu_reader_reg >= (struct rcu_reader *) &chunk->data[chunk->data_len])
483 continue;
484 return chunk;
485 }
486 return NULL;
487}
488
489/* Called with signals off and mutex locked */
490static
491void remove_thread(struct rcu_reader *rcu_reader_reg)
492{
493 cleanup_thread(find_chunk(rcu_reader_reg), rcu_reader_reg);
494 URCU_TLS(rcu_reader) = NULL;
495}
496
497/* Disable signals, take mutex, add to registry */
498void rcu_bp_register(void)
499{
500 sigset_t newmask, oldmask;
501 int ret;
502
503 ret = sigfillset(&newmask);
504 if (ret)
505 abort();
506 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
507 if (ret)
508 abort();
509
510 /*
511 * Check if a signal concurrently registered our thread since
512 * the check in rcu_read_lock().
513 */
514 if (URCU_TLS(rcu_reader))
515 goto end;
516
517 /*
518 * Take care of early registration before urcu_bp constructor.
519 */
520 rcu_bp_init();
521
522 mutex_lock(&rcu_registry_lock);
523 add_thread();
524 mutex_unlock(&rcu_registry_lock);
525end:
526 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
527 if (ret)
528 abort();
529}
530
531/* Disable signals, take mutex, remove from registry */
532static
533void rcu_bp_unregister(struct rcu_reader *rcu_reader_reg)
534{
535 sigset_t newmask, oldmask;
536 int ret;
537
538 ret = sigfillset(&newmask);
539 if (ret)
540 abort();
541 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
542 if (ret)
543 abort();
544
545 mutex_lock(&rcu_registry_lock);
546 remove_thread(rcu_reader_reg);
547 mutex_unlock(&rcu_registry_lock);
548 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
549 if (ret)
550 abort();
551 _rcu_bp_exit();
552}
553
554/*
555 * Remove thread from the registry when it exits, and flag it as
556 * destroyed so garbage collection can take care of it.
557 */
558static
559void urcu_bp_thread_exit_notifier(void *rcu_key)
560{
561 rcu_bp_unregister(rcu_key);
562}
563
564static
565void rcu_bp_init(void)
566{
567 mutex_lock(&init_lock);
568 if (!rcu_bp_refcount++) {
569 int ret;
570
571 ret = pthread_key_create(&urcu_bp_key,
572 urcu_bp_thread_exit_notifier);
573 if (ret)
574 abort();
575 initialized = 1;
576 }
577 mutex_unlock(&init_lock);
578}
579
580static
581void _rcu_bp_exit(void)
582{
583 mutex_lock(&init_lock);
584 if (!--rcu_bp_refcount) {
585 struct registry_chunk *chunk, *tmp;
586 int ret;
587
588 cds_list_for_each_entry_safe(chunk, tmp,
589 &registry_arena.chunk_list, node) {
590 munmap(chunk, chunk->data_len
591 + sizeof(struct registry_chunk));
592 }
593 ret = pthread_key_delete(urcu_bp_key);
594 if (ret)
595 abort();
596 }
597 mutex_unlock(&init_lock);
598}
599
600/*
601 * Keep ABI compability within stable versions. This has never been
602 * exposed through a header, but needs to stay in the .so until the
603 * soname is bumped.
604 */
605void rcu_bp_exit(void)
606{
607}
608
609/*
610 * Holding the rcu_gp_lock and rcu_registry_lock across fork will make
611 * sure we fork() don't race with a concurrent thread executing with
612 * any of those locks held. This ensures that the registry and data
613 * protected by rcu_gp_lock are in a coherent state in the child.
614 */
615void rcu_bp_before_fork(void)
616{
617 sigset_t newmask, oldmask;
618 int ret;
619
620 ret = sigfillset(&newmask);
621 assert(!ret);
622 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
623 assert(!ret);
624 mutex_lock(&rcu_gp_lock);
625 mutex_lock(&rcu_registry_lock);
626 saved_fork_signal_mask = oldmask;
627}
628
629void rcu_bp_after_fork_parent(void)
630{
631 sigset_t oldmask;
632 int ret;
633
634 oldmask = saved_fork_signal_mask;
635 mutex_unlock(&rcu_registry_lock);
636 mutex_unlock(&rcu_gp_lock);
637 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
638 assert(!ret);
639}
640
641/*
642 * Prune all entries from registry except our own thread. Fits the Linux
643 * fork behavior. Called with rcu_gp_lock and rcu_registry_lock held.
644 */
645static
646void urcu_bp_prune_registry(void)
647{
648 struct registry_chunk *chunk;
649 struct rcu_reader *rcu_reader_reg;
650
651 cds_list_for_each_entry(chunk, &registry_arena.chunk_list, node) {
652 for (rcu_reader_reg = (struct rcu_reader *) &chunk->data[0];
653 rcu_reader_reg < (struct rcu_reader *) &chunk->data[chunk->data_len];
654 rcu_reader_reg++) {
655 if (!rcu_reader_reg->alloc)
656 continue;
657 if (rcu_reader_reg->tid == pthread_self())
658 continue;
659 cleanup_thread(chunk, rcu_reader_reg);
660 }
661 }
662}
663
664void rcu_bp_after_fork_child(void)
665{
666 sigset_t oldmask;
667 int ret;
668
669 urcu_bp_prune_registry();
670 oldmask = saved_fork_signal_mask;
671 mutex_unlock(&rcu_registry_lock);
672 mutex_unlock(&rcu_gp_lock);
673 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
674 assert(!ret);
675}
676
677void *rcu_dereference_sym_bp(void *p)
678{
679 return _rcu_dereference(p);
680}
681
682void *rcu_set_pointer_sym_bp(void **p, void *v)
683{
684 cmm_wmb();
685 uatomic_set(p, v);
686 return v;
687}
688
689void *rcu_xchg_pointer_sym_bp(void **p, void *v)
690{
691 cmm_wmb();
692 return uatomic_xchg(p, v);
693}
694
695void *rcu_cmpxchg_pointer_sym_bp(void **p, void *old, void *_new)
696{
697 cmm_wmb();
698 return uatomic_cmpxchg(p, old, _new);
699}
700
701DEFINE_RCU_FLAVOR(rcu_flavor);
702
703#include "urcu-call-rcu-impl.h"
704#include "urcu-defer-impl.h"
This page took 0.024786 seconds and 4 git commands to generate.