protect writing to per_cpu_call_rcu_data[*]
[urcu.git] / 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
0617bf4c 26#define _GNU_SOURCE
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>
37#include <sys/mman.h>
38
71c811bf 39#include "urcu/wfqueue.h"
57760d44 40#include "urcu/map/urcu-bp.h"
af7c2dbe 41#include "urcu/static/urcu-bp.h"
71c811bf 42
fdee2e6d 43/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
71c811bf 44#undef _LGPL_SOURCE
fdee2e6d 45#include "urcu-bp.h"
71c811bf 46#define _LGPL_SOURCE
fdee2e6d 47
4c1ae2ea
MD
48#ifndef MAP_ANONYMOUS
49#define MAP_ANONYMOUS MAP_ANON
50#endif
51
45a4872f
MD
52#ifndef __linux__
53
54#define MREMAP_MAYMOVE 1
55#define MREMAP_FIXED 2
56
57/*
58 * mremap wrapper for non-Linux systems. Maps a RW, anonymous private mapping.
59 * This is not generic.
60*/
61void *mremap(void *old_address, size_t old_size, size_t new_size, int flags)
62{
dc745ef6 63 void *new_address;
45a4872f
MD
64
65 assert(flags & MREMAP_MAYMOVE);
66 assert(!(flags & MREMAP_FIXED));
67 new_address = mmap(old_address, new_size,
68 PROT_READ | PROT_WRITE,
69 MAP_ANONYMOUS | MAP_PRIVATE,
70 -1, 0);
71 if (new_address == MAP_FAILED)
72 return MAP_FAILED;
73 if (old_address) {
74 memcpy(new_address, old_address, old_size);
75 munmap(old_address, old_size);
76 }
77 return new_address;
78}
79#endif
80
fdee2e6d
MD
81/* Sleep delay in us */
82#define RCU_SLEEP_DELAY 1000
83#define ARENA_INIT_ALLOC 16
84
b7b6a8f5
PB
85/*
86 * Active attempts to check for reader Q.S. before calling sleep().
87 */
88#define RCU_QS_ACTIVE_ATTEMPTS 100
89
02be5561 90void __attribute__((destructor)) rcu_bp_exit(void);
fdee2e6d 91
6abb4bd5 92static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
fdee2e6d
MD
93
94#ifdef DEBUG_YIELD
95unsigned int yield_active;
96unsigned int __thread rand_yield;
97#endif
98
99/*
100 * Global grace period counter.
02be5561 101 * Contains the current RCU_GP_CTR_PHASE.
fdee2e6d
MD
102 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
103 * Written to only by writer with mutex taken. Read by both writer and readers.
104 */
02be5561 105long rcu_gp_ctr = RCU_GP_COUNT;
fdee2e6d
MD
106
107/*
108 * Pointer to registry elements. Written to only by each individual reader. Read
109 * by both the reader and the writers.
110 */
02be5561 111struct rcu_reader __thread *rcu_reader;
fdee2e6d 112
16aa9ee8 113static CDS_LIST_HEAD(registry);
fdee2e6d
MD
114
115struct registry_arena {
116 void *p;
117 size_t len;
118 size_t used;
119};
120
121static struct registry_arena registry_arena;
122
4cf1675f
MD
123/* Saved fork signal mask, protected by rcu_gp_lock */
124static sigset_t saved_fork_signal_mask;
125
fdee2e6d
MD
126static void rcu_gc_registry(void);
127
6abb4bd5 128static void mutex_lock(pthread_mutex_t *mutex)
fdee2e6d
MD
129{
130 int ret;
131
132#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 133 ret = pthread_mutex_lock(mutex);
fdee2e6d
MD
134 if (ret) {
135 perror("Error in pthread mutex lock");
136 exit(-1);
137 }
138#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 139 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
fdee2e6d
MD
140 if (ret != EBUSY && ret != EINTR) {
141 printf("ret = %d, errno = %d\n", ret, errno);
142 perror("Error in pthread mutex lock");
143 exit(-1);
144 }
fdee2e6d
MD
145 poll(NULL,0,10);
146 }
147#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
148}
149
6abb4bd5 150static void mutex_unlock(pthread_mutex_t *mutex)
fdee2e6d
MD
151{
152 int ret;
153
6abb4bd5 154 ret = pthread_mutex_unlock(mutex);
fdee2e6d
MD
155 if (ret) {
156 perror("Error in pthread mutex unlock");
157 exit(-1);
158 }
159}
160
2dfb8b5e 161void update_counter_and_wait(void)
fdee2e6d 162{
16aa9ee8 163 CDS_LIST_HEAD(qsreaders);
fdee2e6d 164 int wait_loops = 0;
02be5561 165 struct rcu_reader *index, *tmp;
fdee2e6d 166
32c15e4e 167 /* Switch parity: 0 -> 1, 1 -> 0 */
6cf3827c 168 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
2dfb8b5e
MD
169
170 /*
171 * Must commit qparity update to memory before waiting for other parity
172 * quiescent state. Failure to do so could result in the writer waiting
173 * forever while new readers are always accessing data (no progress).
6cf3827c 174 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
2dfb8b5e
MD
175 */
176
177 /*
5481ddb3 178 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
2dfb8b5e
MD
179 * model easier to understand. It does not have a big performance impact
180 * anyway, given this is the write-side.
181 */
5481ddb3 182 cmm_smp_mb();
2dfb8b5e 183
fdee2e6d 184 /*
02be5561 185 * Wait for each thread rcu_reader.ctr count to become 0.
fdee2e6d
MD
186 */
187 for (;;) {
188 wait_loops++;
16aa9ee8 189 cds_list_for_each_entry_safe(index, tmp, &registry, node) {
fdee2e6d 190 if (!rcu_old_gp_ongoing(&index->ctr))
16aa9ee8 191 cds_list_move(&index->node, &qsreaders);
fdee2e6d
MD
192 }
193
16aa9ee8 194 if (cds_list_empty(&registry)) {
fdee2e6d
MD
195 break;
196 } else {
197 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS)
198 usleep(RCU_SLEEP_DELAY);
199 else
06f22bdb 200 caa_cpu_relax();
fdee2e6d
MD
201 }
202 }
203 /* put back the reader list in the registry */
16aa9ee8 204 cds_list_splice(&qsreaders, &registry);
fdee2e6d
MD
205}
206
207void synchronize_rcu(void)
208{
209 sigset_t newmask, oldmask;
210 int ret;
211
212 ret = sigemptyset(&newmask);
213 assert(!ret);
214 ret = pthread_sigmask(SIG_SETMASK, &newmask, &oldmask);
215 assert(!ret);
216
6abb4bd5 217 mutex_lock(&rcu_gp_lock);
fdee2e6d 218
16aa9ee8 219 if (cds_list_empty(&registry))
2dfb8b5e 220 goto out;
fdee2e6d
MD
221
222 /* All threads should read qparity before accessing data structure
2dfb8b5e 223 * where new ptr points to. */
fdee2e6d 224 /* Write new ptr before changing the qparity */
5481ddb3 225 cmm_smp_mb();
fdee2e6d 226
2dfb8b5e
MD
227 /* Remove old registry elements */
228 rcu_gc_registry();
fdee2e6d
MD
229
230 /*
231 * Wait for previous parity to be empty of readers.
232 */
2dfb8b5e 233 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
fdee2e6d
MD
234
235 /*
5481ddb3 236 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
fdee2e6d
MD
237 * model easier to understand. It does not have a big performance impact
238 * anyway, given this is the write-side.
239 */
5481ddb3 240 cmm_smp_mb();
fdee2e6d 241
fdee2e6d 242 /*
2dfb8b5e 243 * Wait for previous parity to be empty of readers.
fdee2e6d 244 */
2dfb8b5e 245 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
fdee2e6d
MD
246
247 /*
2dfb8b5e
MD
248 * Finish waiting for reader threads before letting the old ptr being
249 * freed.
fdee2e6d 250 */
5481ddb3 251 cmm_smp_mb();
2dfb8b5e 252out:
6abb4bd5 253 mutex_unlock(&rcu_gp_lock);
fdee2e6d
MD
254 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
255 assert(!ret);
256}
257
258/*
259 * library wrappers to be used by non-LGPL compatible source code.
260 */
261
262void rcu_read_lock(void)
263{
264 _rcu_read_lock();
265}
266
267void rcu_read_unlock(void)
268{
269 _rcu_read_unlock();
270}
271
272/*
273 * only grow for now.
274 */
275static void resize_arena(struct registry_arena *arena, size_t len)
276{
277 void *new_arena;
278
0617bf4c
MD
279 if (!arena->p)
280 new_arena = mmap(arena->p, len,
281 PROT_READ | PROT_WRITE,
282 MAP_ANONYMOUS | MAP_PRIVATE,
283 -1, 0);
284 else
285 new_arena = mremap(arena->p, arena->len,
286 len, MREMAP_MAYMOVE);
287 assert(new_arena != MAP_FAILED);
288
fdee2e6d
MD
289 /*
290 * re-used the same region ?
291 */
292 if (new_arena == arena->p)
293 return;
294
fdee2e6d
MD
295 bzero(new_arena + arena->len, len - arena->len);
296 arena->p = new_arena;
297}
298
299/* Called with signals off and mutex locked */
300static void add_thread(void)
301{
02be5561 302 struct rcu_reader *rcu_reader_reg;
fdee2e6d
MD
303
304 if (registry_arena.len
02be5561 305 < registry_arena.used + sizeof(struct rcu_reader))
fdee2e6d 306 resize_arena(&registry_arena,
2f8a5ae7 307 caa_max(registry_arena.len << 1, ARENA_INIT_ALLOC));
fdee2e6d
MD
308 /*
309 * Find a free spot.
310 */
02be5561
MD
311 for (rcu_reader_reg = registry_arena.p;
312 (void *)rcu_reader_reg < registry_arena.p + registry_arena.len;
313 rcu_reader_reg++) {
314 if (!rcu_reader_reg->alloc)
fdee2e6d
MD
315 break;
316 }
02be5561
MD
317 rcu_reader_reg->alloc = 1;
318 registry_arena.used += sizeof(struct rcu_reader);
fdee2e6d
MD
319
320 /* Add to registry */
02be5561
MD
321 rcu_reader_reg->tid = pthread_self();
322 assert(rcu_reader_reg->ctr == 0);
16aa9ee8 323 cds_list_add(&rcu_reader_reg->node, &registry);
02be5561 324 rcu_reader = rcu_reader_reg;
fdee2e6d
MD
325}
326
327/* Called with signals off and mutex locked */
328static void rcu_gc_registry(void)
329{
02be5561 330 struct rcu_reader *rcu_reader_reg;
fdee2e6d
MD
331 pthread_t tid;
332 int ret;
333
02be5561
MD
334 for (rcu_reader_reg = registry_arena.p;
335 (void *)rcu_reader_reg < registry_arena.p + registry_arena.len;
336 rcu_reader_reg++) {
337 if (!rcu_reader_reg->alloc)
fdee2e6d 338 continue;
02be5561 339 tid = rcu_reader_reg->tid;
fdee2e6d
MD
340 ret = pthread_kill(tid, 0);
341 assert(ret != EINVAL);
342 if (ret == ESRCH) {
16aa9ee8 343 cds_list_del(&rcu_reader_reg->node);
79266051 344 rcu_reader_reg->ctr = 0;
02be5561
MD
345 rcu_reader_reg->alloc = 0;
346 registry_arena.used -= sizeof(struct rcu_reader);
fdee2e6d
MD
347 }
348 }
349}
350
351/* Disable signals, take mutex, add to registry */
352void rcu_bp_register(void)
353{
354 sigset_t newmask, oldmask;
355 int ret;
356
357 ret = sigemptyset(&newmask);
358 assert(!ret);
359 ret = pthread_sigmask(SIG_SETMASK, &newmask, &oldmask);
360 assert(!ret);
361
362 /*
363 * Check if a signal concurrently registered our thread since
364 * the check in rcu_read_lock(). */
02be5561 365 if (rcu_reader)
fdee2e6d
MD
366 goto end;
367
6abb4bd5 368 mutex_lock(&rcu_gp_lock);
fdee2e6d 369 add_thread();
6abb4bd5 370 mutex_unlock(&rcu_gp_lock);
fdee2e6d
MD
371end:
372 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
373 assert(!ret);
374}
375
9380711a 376void rcu_bp_exit(void)
fdee2e6d 377{
9380711a
MD
378 if (registry_arena.p)
379 munmap(registry_arena.p, registry_arena.len);
fdee2e6d 380}
4cf1675f
MD
381
382/*
383 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
384 * a concurrent thread executing with this same lock held. This ensures that the
385 * registry is in a coherent state in the child.
386 */
387void rcu_bp_before_fork(void)
388{
389 sigset_t newmask, oldmask;
390 int ret;
391
392 ret = sigemptyset(&newmask);
393 assert(!ret);
394 ret = pthread_sigmask(SIG_SETMASK, &newmask, &oldmask);
395 assert(!ret);
396 mutex_lock(&rcu_gp_lock);
397 saved_fork_signal_mask = oldmask;
398}
399
400void rcu_bp_after_fork_parent(void)
401{
402 sigset_t oldmask;
403 int ret;
404
405 oldmask = saved_fork_signal_mask;
406 mutex_unlock(&rcu_gp_lock);
407 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
408 assert(!ret);
409}
410
411void rcu_bp_after_fork_child(void)
412{
413 sigset_t oldmask;
414 int ret;
415
416 rcu_gc_registry();
417 oldmask = saved_fork_signal_mask;
418 mutex_unlock(&rcu_gp_lock);
419 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
420 assert(!ret);
421}
5e77fc1f
PM
422
423#include "urcu-call-rcu-impl.h"
0376e7b2 424#include "urcu-defer-impl.h"
This page took 0.043676 seconds and 4 git commands to generate.