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