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