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