Fix: urcu-bp: Bulletproof RCU arena resize bug
[userspace-rcu.git] / urcu-bp.c
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/wfqueue.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__
57 static
58 void *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. Maps a RW, anonymous private mapping.
70 * This is not generic.
71 */
72 static
73 void *mremap_wrapper(void *old_address, size_t old_size,
74 size_t new_size, int flags)
75 {
76 void *new_address;
77
78 assert(flags & MREMAP_MAYMOVE);
79 assert(!(flags & MREMAP_FIXED));
80 new_address = mmap(old_address, new_size,
81 PROT_READ | PROT_WRITE,
82 MAP_ANONYMOUS | MAP_PRIVATE,
83 -1, 0);
84 if (new_address == MAP_FAILED)
85 return MAP_FAILED;
86 if (old_address) {
87 memcpy(new_address, old_address, old_size);
88 munmap(old_address, old_size);
89 }
90 return new_address;
91 }
92 #endif
93
94 /* Sleep delay in us */
95 #define RCU_SLEEP_DELAY 1000
96 #define ARENA_INIT_ALLOC 16
97
98 /*
99 * Active attempts to check for reader Q.S. before calling sleep().
100 */
101 #define RCU_QS_ACTIVE_ATTEMPTS 100
102
103 void __attribute__((destructor)) rcu_bp_exit(void);
104
105 static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
106
107 #ifdef DEBUG_YIELD
108 unsigned int yield_active;
109 DEFINE_URCU_TLS(unsigned int, rand_yield);
110 #endif
111
112 /*
113 * Global grace period counter.
114 * Contains the current RCU_GP_CTR_PHASE.
115 * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path.
116 * Written to only by writer with mutex taken. Read by both writer and readers.
117 */
118 long rcu_gp_ctr = RCU_GP_COUNT;
119
120 /*
121 * Pointer to registry elements. Written to only by each individual reader. Read
122 * by both the reader and the writers.
123 */
124 DEFINE_URCU_TLS(struct rcu_reader *, rcu_reader);
125
126 static CDS_LIST_HEAD(registry);
127
128 struct registry_arena {
129 void *p;
130 size_t len;
131 size_t used;
132 };
133
134 static struct registry_arena registry_arena;
135
136 /* Saved fork signal mask, protected by rcu_gp_lock */
137 static sigset_t saved_fork_signal_mask;
138
139 static void rcu_gc_registry(void);
140
141 static void mutex_lock(pthread_mutex_t *mutex)
142 {
143 int ret;
144
145 #ifndef DISTRUST_SIGNALS_EXTREME
146 ret = pthread_mutex_lock(mutex);
147 if (ret)
148 urcu_die(ret);
149 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
150 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
151 if (ret != EBUSY && ret != EINTR)
152 urcu_die(ret);
153 poll(NULL,0,10);
154 }
155 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
156 }
157
158 static void mutex_unlock(pthread_mutex_t *mutex)
159 {
160 int ret;
161
162 ret = pthread_mutex_unlock(mutex);
163 if (ret)
164 urcu_die(ret);
165 }
166
167 void update_counter_and_wait(void)
168 {
169 CDS_LIST_HEAD(qsreaders);
170 int wait_loops = 0;
171 struct rcu_reader *index, *tmp;
172
173 /* Switch parity: 0 -> 1, 1 -> 0 */
174 CMM_STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE);
175
176 /*
177 * Must commit qparity update to memory before waiting for other parity
178 * quiescent state. Failure to do so could result in the writer waiting
179 * forever while new readers are always accessing data (no progress).
180 * Ensured by CMM_STORE_SHARED and CMM_LOAD_SHARED.
181 */
182
183 /*
184 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
185 * model easier to understand. It does not have a big performance impact
186 * anyway, given this is the write-side.
187 */
188 cmm_smp_mb();
189
190 /*
191 * Wait for each thread rcu_reader.ctr count to become 0.
192 */
193 for (;;) {
194 wait_loops++;
195 cds_list_for_each_entry_safe(index, tmp, &registry, node) {
196 if (!rcu_old_gp_ongoing(&index->ctr))
197 cds_list_move(&index->node, &qsreaders);
198 }
199
200 if (cds_list_empty(&registry)) {
201 break;
202 } else {
203 if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS)
204 usleep(RCU_SLEEP_DELAY);
205 else
206 caa_cpu_relax();
207 }
208 }
209 /* put back the reader list in the registry */
210 cds_list_splice(&qsreaders, &registry);
211 }
212
213 void synchronize_rcu(void)
214 {
215 sigset_t newmask, oldmask;
216 int ret;
217
218 ret = sigfillset(&newmask);
219 assert(!ret);
220 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
221 assert(!ret);
222
223 mutex_lock(&rcu_gp_lock);
224
225 if (cds_list_empty(&registry))
226 goto out;
227
228 /* All threads should read qparity before accessing data structure
229 * where new ptr points to. */
230 /* Write new ptr before changing the qparity */
231 cmm_smp_mb();
232
233 /* Remove old registry elements */
234 rcu_gc_registry();
235
236 /*
237 * Wait for previous parity to be empty of readers.
238 */
239 update_counter_and_wait(); /* 0 -> 1, wait readers in parity 0 */
240
241 /*
242 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
243 * model easier to understand. It does not have a big performance impact
244 * anyway, given this is the write-side.
245 */
246 cmm_smp_mb();
247
248 /*
249 * Wait for previous parity to be empty of readers.
250 */
251 update_counter_and_wait(); /* 1 -> 0, wait readers in parity 1 */
252
253 /*
254 * Finish waiting for reader threads before letting the old ptr being
255 * freed.
256 */
257 cmm_smp_mb();
258 out:
259 mutex_unlock(&rcu_gp_lock);
260 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
261 assert(!ret);
262 }
263
264 /*
265 * library wrappers to be used by non-LGPL compatible source code.
266 */
267
268 void rcu_read_lock(void)
269 {
270 _rcu_read_lock();
271 }
272
273 void rcu_read_unlock(void)
274 {
275 _rcu_read_unlock();
276 }
277
278 /*
279 * only grow for now.
280 */
281 static void resize_arena(struct registry_arena *arena, size_t len)
282 {
283 void *new_p;
284 size_t old_len;
285
286 old_len = arena->len;
287
288 if (!arena->p)
289 new_p = mmap(arena->p, len,
290 PROT_READ | PROT_WRITE,
291 MAP_ANONYMOUS | MAP_PRIVATE,
292 -1, 0);
293 else
294 new_p = mremap_wrapper(arena->p, old_len,
295 len, MREMAP_MAYMOVE);
296 assert(new_p != MAP_FAILED);
297
298 /*
299 * Zero the newly allocated memory. Since mmap() does not
300 * clearly specify if memory is zeroed or not (although it is
301 * very likely that it is), be extra careful by not expecting
302 * the new range to be zeroed by mremap.
303 */
304 bzero(new_p + old_len, len - old_len);
305
306 /*
307 * If we did not re-use the same region, we need to update the
308 * arena pointer.
309 */
310 if (new_p != arena->p)
311 arena->p = new_p;
312
313 arena->len = len;
314 }
315
316 /* Called with signals off and mutex locked */
317 static void add_thread(void)
318 {
319 struct rcu_reader *rcu_reader_reg;
320
321 if (registry_arena.len
322 < registry_arena.used + sizeof(struct rcu_reader))
323 resize_arena(&registry_arena,
324 caa_max(registry_arena.len << 1, ARENA_INIT_ALLOC));
325 /*
326 * Find a free spot.
327 */
328 for (rcu_reader_reg = registry_arena.p;
329 (void *)rcu_reader_reg < registry_arena.p + registry_arena.len;
330 rcu_reader_reg++) {
331 if (!rcu_reader_reg->alloc)
332 break;
333 }
334 rcu_reader_reg->alloc = 1;
335 registry_arena.used += sizeof(struct rcu_reader);
336
337 /* Add to registry */
338 rcu_reader_reg->tid = pthread_self();
339 assert(rcu_reader_reg->ctr == 0);
340 cds_list_add(&rcu_reader_reg->node, &registry);
341 URCU_TLS(rcu_reader) = rcu_reader_reg;
342 }
343
344 /* Called with signals off and mutex locked */
345 static void rcu_gc_registry(void)
346 {
347 struct rcu_reader *rcu_reader_reg;
348 pthread_t tid;
349 int ret;
350
351 for (rcu_reader_reg = registry_arena.p;
352 (void *)rcu_reader_reg < registry_arena.p + registry_arena.len;
353 rcu_reader_reg++) {
354 if (!rcu_reader_reg->alloc)
355 continue;
356 tid = rcu_reader_reg->tid;
357 ret = pthread_kill(tid, 0);
358 assert(ret != EINVAL);
359 if (ret == ESRCH) {
360 cds_list_del(&rcu_reader_reg->node);
361 rcu_reader_reg->ctr = 0;
362 rcu_reader_reg->alloc = 0;
363 registry_arena.used -= sizeof(struct rcu_reader);
364 }
365 }
366 }
367
368 /* Disable signals, take mutex, add to registry */
369 void rcu_bp_register(void)
370 {
371 sigset_t newmask, oldmask;
372 int ret;
373
374 ret = sigfillset(&newmask);
375 assert(!ret);
376 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
377 assert(!ret);
378
379 /*
380 * Check if a signal concurrently registered our thread since
381 * the check in rcu_read_lock(). */
382 if (URCU_TLS(rcu_reader))
383 goto end;
384
385 mutex_lock(&rcu_gp_lock);
386 add_thread();
387 mutex_unlock(&rcu_gp_lock);
388 end:
389 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
390 assert(!ret);
391 }
392
393 void rcu_bp_exit(void)
394 {
395 if (registry_arena.p)
396 munmap(registry_arena.p, registry_arena.len);
397 }
398
399 /*
400 * Holding the rcu_gp_lock across fork will make sure we fork() don't race with
401 * a concurrent thread executing with this same lock held. This ensures that the
402 * registry is in a coherent state in the child.
403 */
404 void rcu_bp_before_fork(void)
405 {
406 sigset_t newmask, oldmask;
407 int ret;
408
409 ret = sigfillset(&newmask);
410 assert(!ret);
411 ret = pthread_sigmask(SIG_BLOCK, &newmask, &oldmask);
412 assert(!ret);
413 mutex_lock(&rcu_gp_lock);
414 saved_fork_signal_mask = oldmask;
415 }
416
417 void rcu_bp_after_fork_parent(void)
418 {
419 sigset_t oldmask;
420 int ret;
421
422 oldmask = saved_fork_signal_mask;
423 mutex_unlock(&rcu_gp_lock);
424 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
425 assert(!ret);
426 }
427
428 void rcu_bp_after_fork_child(void)
429 {
430 sigset_t oldmask;
431 int ret;
432
433 rcu_gc_registry();
434 oldmask = saved_fork_signal_mask;
435 mutex_unlock(&rcu_gp_lock);
436 ret = pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
437 assert(!ret);
438 }
439
440 void *rcu_dereference_sym_bp(void *p)
441 {
442 return _rcu_dereference(p);
443 }
444
445 void *rcu_set_pointer_sym_bp(void **p, void *v)
446 {
447 cmm_wmb();
448 uatomic_set(p, v);
449 return v;
450 }
451
452 void *rcu_xchg_pointer_sym_bp(void **p, void *v)
453 {
454 cmm_wmb();
455 return uatomic_xchg(p, v);
456 }
457
458 void *rcu_cmpxchg_pointer_sym_bp(void **p, void *old, void *_new)
459 {
460 cmm_wmb();
461 return uatomic_cmpxchg(p, old, _new);
462 }
463
464 DEFINE_RCU_FLAVOR(rcu_flavor);
465
466 #include "urcu-call-rcu-impl.h"
467 #include "urcu-defer-impl.h"
This page took 0.038375 seconds and 4 git commands to generate.