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