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