Commit | Line | Data |
---|---|---|
b257a10b MD |
1 | /* |
2 | * urcu.c | |
3 | * | |
4 | * Userspace RCU library | |
5 | * | |
6982d6d7 | 6 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
af02d47e | 7 | * Copyright (c) 2009 Paul E. McKenney, IBM Corporation. |
b257a10b | 8 | * |
af02d47e MD |
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 | |
54843abc PM |
22 | * |
23 | * IBM's contributions to this file may be relicensed under LGPLv2 or later. | |
b257a10b MD |
24 | */ |
25 | ||
fdf01eed | 26 | #define _BSD_SOURCE |
71c811bf | 27 | #define _LGPL_SOURCE |
82d50e1a | 28 | #define _DEFAULT_SOURCE |
27b012e2 MD |
29 | #include <stdio.h> |
30 | #include <pthread.h> | |
31 | #include <signal.h> | |
32 | #include <assert.h> | |
f69f195a | 33 | #include <stdlib.h> |
6d841bc2 | 34 | #include <stdint.h> |
f69f195a | 35 | #include <string.h> |
09a9f986 | 36 | #include <errno.h> |
c0bb9f69 | 37 | #include <stdbool.h> |
e8043c1b | 38 | #include <poll.h> |
27b012e2 | 39 | |
999991c6 | 40 | #include "urcu/arch.h" |
d73fb81f | 41 | #include "urcu/wfcqueue.h" |
57760d44 | 42 | #include "urcu/map/urcu.h" |
af7c2dbe | 43 | #include "urcu/static/urcu.h" |
618b2595 | 44 | #include "urcu-pointer.h" |
bd252a04 | 45 | #include "urcu/tls-compat.h" |
71c811bf | 46 | |
4a6d7378 | 47 | #include "urcu-die.h" |
5bffdd5d | 48 | #include "urcu-wait.h" |
4a6d7378 | 49 | |
121a5d44 | 50 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ |
71c811bf | 51 | #undef _LGPL_SOURCE |
27b012e2 | 52 | #include "urcu.h" |
71c811bf | 53 | #define _LGPL_SOURCE |
27b012e2 | 54 | |
3a71751e PB |
55 | /* |
56 | * If a reader is really non-cooperative and refuses to commit its | |
57 | * rcu_active_readers count to memory (there is no barrier in the reader | |
9340c38d | 58 | * per-se), kick it after 10 loops waiting for it. |
3a71751e | 59 | */ |
9340c38d | 60 | #define KICK_READER_LOOPS 10 |
3a71751e PB |
61 | |
62 | /* | |
63 | * Active attempts to check for reader Q.S. before calling futex(). | |
64 | */ | |
65 | #define RCU_QS_ACTIVE_ATTEMPTS 100 | |
66 | ||
999991c6 MD |
67 | /* If the headers do not support membarrier system call, fall back on RCU_MB */ |
68 | #ifdef __NR_membarrier | |
69 | # define membarrier(...) syscall(__NR_membarrier, __VA_ARGS__) | |
553b7eb9 MD |
70 | #else |
71 | # define membarrier(...) -ENOSYS | |
72 | #endif | |
73 | ||
64f469e6 | 74 | enum membarrier_cmd { |
c0bb9f69 MD |
75 | MEMBARRIER_CMD_QUERY = 0, |
76 | MEMBARRIER_CMD_SHARED = (1 << 0), | |
77 | /* reserved for MEMBARRIER_CMD_SHARED_EXPEDITED (1 << 1) */ | |
78 | /* reserved for MEMBARRIER_CMD_PRIVATE (1 << 2) */ | |
79 | MEMBARRIER_CMD_PRIVATE_EXPEDITED = (1 << 3), | |
80 | MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED = (1 << 4), | |
64f469e6 | 81 | }; |
553b7eb9 | 82 | |
fdf01eed | 83 | #ifdef RCU_MEMBARRIER |
834a45ba | 84 | static int init_done; |
c0bb9f69 MD |
85 | static int has_sys_membarrier_private_expedited; |
86 | ||
d8d9a340 MD |
87 | #ifndef CONFIG_RCU_FORCE_SYS_MEMBARRIER |
88 | int rcu_has_sys_membarrier_memb; | |
89 | #endif | |
834a45ba | 90 | |
02be5561 | 91 | void __attribute__((constructor)) rcu_init(void); |
fdf01eed MD |
92 | #endif |
93 | ||
94 | #ifdef RCU_MB | |
02be5561 | 95 | void rcu_init(void) |
e90a6e9c MD |
96 | { |
97 | } | |
98 | #endif | |
8a5fb4c9 | 99 | |
fdf01eed MD |
100 | #ifdef RCU_SIGNAL |
101 | static int init_done; | |
102 | ||
103 | void __attribute__((constructor)) rcu_init(void); | |
104 | void __attribute__((destructor)) rcu_exit(void); | |
105 | #endif | |
106 | ||
731ccb96 MD |
107 | /* |
108 | * rcu_gp_lock ensures mutual exclusion between threads calling | |
109 | * synchronize_rcu(). | |
110 | */ | |
6abb4bd5 | 111 | static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER; |
731ccb96 MD |
112 | /* |
113 | * rcu_registry_lock ensures mutual exclusion between threads | |
114 | * registering and unregistering themselves to/from the registry, and | |
115 | * with threads reading that registry from synchronize_rcu(). However, | |
116 | * this lock is not held all the way through the completion of awaiting | |
117 | * for the grace period. It is sporadically released between iterations | |
118 | * on the registry. | |
119 | * rcu_registry_lock may nest inside rcu_gp_lock. | |
120 | */ | |
121 | static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER; | |
4de0cd31 | 122 | struct rcu_gp rcu_gp = { .ctr = RCU_GP_COUNT }; |
27b012e2 | 123 | |
b0d5e790 MD |
124 | /* |
125 | * Written to only by each individual reader. Read by both the reader and the | |
126 | * writers. | |
127 | */ | |
2f661865 | 128 | DEFINE_URCU_TLS(struct rcu_reader, rcu_reader); |
27b012e2 | 129 | |
16aa9ee8 | 130 | static CDS_LIST_HEAD(registry); |
27b012e2 | 131 | |
5bffdd5d MD |
132 | /* |
133 | * Queue keeping threads awaiting to wait for a grace period. Contains | |
134 | * struct gp_waiters_thread objects. | |
135 | */ | |
136 | static DEFINE_URCU_WAIT_QUEUE(gp_waiters); | |
137 | ||
6abb4bd5 | 138 | static void mutex_lock(pthread_mutex_t *mutex) |
41718ff9 MD |
139 | { |
140 | int ret; | |
09a9f986 PM |
141 | |
142 | #ifndef DISTRUST_SIGNALS_EXTREME | |
6abb4bd5 | 143 | ret = pthread_mutex_lock(mutex); |
4a6d7378 MD |
144 | if (ret) |
145 | urcu_die(ret); | |
09a9f986 | 146 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ |
6abb4bd5 | 147 | while ((ret = pthread_mutex_trylock(mutex)) != 0) { |
4a6d7378 MD |
148 | if (ret != EBUSY && ret != EINTR) |
149 | urcu_die(ret); | |
bd252a04 | 150 | if (CMM_LOAD_SHARED(URCU_TLS(rcu_reader).need_mb)) { |
5481ddb3 | 151 | cmm_smp_mb(); |
bd252a04 | 152 | _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0); |
5481ddb3 | 153 | cmm_smp_mb(); |
09a9f986 | 154 | } |
8999a9ee | 155 | (void) poll(NULL, 0, 10); |
09a9f986 PM |
156 | } |
157 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ | |
41718ff9 MD |
158 | } |
159 | ||
6abb4bd5 | 160 | static void mutex_unlock(pthread_mutex_t *mutex) |
41718ff9 MD |
161 | { |
162 | int ret; | |
163 | ||
6abb4bd5 | 164 | ret = pthread_mutex_unlock(mutex); |
4a6d7378 MD |
165 | if (ret) |
166 | urcu_die(ret); | |
41718ff9 MD |
167 | } |
168 | ||
fdf01eed | 169 | #ifdef RCU_MEMBARRIER |
a4922ed9 | 170 | static void smp_mb_master(void) |
fdf01eed | 171 | { |
c0bb9f69 MD |
172 | if (caa_likely(rcu_has_sys_membarrier_memb)) { |
173 | if (membarrier(has_sys_membarrier_private_expedited ? | |
174 | MEMBARRIER_CMD_PRIVATE_EXPEDITED : | |
175 | MEMBARRIER_CMD_SHARED, 0)) | |
176 | urcu_die(errno); | |
177 | } else { | |
5481ddb3 | 178 | cmm_smp_mb(); |
c0bb9f69 | 179 | } |
fdf01eed MD |
180 | } |
181 | #endif | |
182 | ||
02be5561 | 183 | #ifdef RCU_MB |
a4922ed9 | 184 | static void smp_mb_master(void) |
40e140c9 | 185 | { |
5481ddb3 | 186 | cmm_smp_mb(); |
40e140c9 | 187 | } |
fdf01eed MD |
188 | #endif |
189 | ||
190 | #ifdef RCU_SIGNAL | |
78ff9419 | 191 | static void force_mb_all_readers(void) |
27b012e2 | 192 | { |
02be5561 | 193 | struct rcu_reader *index; |
e3b0cef0 | 194 | |
27b012e2 | 195 | /* |
5481ddb3 | 196 | * Ask for each threads to execute a cmm_smp_mb() so we can consider the |
27b012e2 MD |
197 | * compiler barriers around rcu read lock as real memory barriers. |
198 | */ | |
16aa9ee8 | 199 | if (cds_list_empty(®istry)) |
27b012e2 | 200 | return; |
3a86deba | 201 | /* |
5481ddb3 | 202 | * pthread_kill has a cmm_smp_mb(). But beware, we assume it performs |
157dca95 | 203 | * a cache flush on architectures with non-coherent cache. Let's play |
5481ddb3 | 204 | * safe and don't assume anything : we use cmm_smp_mc() to make sure the |
157dca95 | 205 | * cache flush is enforced. |
3a86deba | 206 | */ |
16aa9ee8 | 207 | cds_list_for_each_entry(index, ®istry, node) { |
6cf3827c | 208 | CMM_STORE_SHARED(index->need_mb, 1); |
02be5561 | 209 | pthread_kill(index->tid, SIGRCU); |
09a9f986 | 210 | } |
27b012e2 MD |
211 | /* |
212 | * Wait for sighandler (and thus mb()) to execute on every thread. | |
09a9f986 PM |
213 | * |
214 | * Note that the pthread_kill() will never be executed on systems | |
215 | * that correctly deliver signals in a timely manner. However, it | |
216 | * is not uncommon for kernels to have bugs that can result in | |
217 | * lost or unduly delayed signals. | |
218 | * | |
219 | * If you are seeing the below pthread_kill() executing much at | |
220 | * all, we suggest testing the underlying kernel and filing the | |
221 | * relevant bug report. For Linux kernels, we recommend getting | |
222 | * the Linux Test Project (LTP). | |
27b012e2 | 223 | */ |
16aa9ee8 | 224 | cds_list_for_each_entry(index, ®istry, node) { |
6cf3827c | 225 | while (CMM_LOAD_SHARED(index->need_mb)) { |
02be5561 | 226 | pthread_kill(index->tid, SIGRCU); |
8999a9ee | 227 | (void) poll(NULL, 0, 1); |
09a9f986 PM |
228 | } |
229 | } | |
5481ddb3 | 230 | cmm_smp_mb(); /* read ->need_mb before ending the barrier */ |
27b012e2 | 231 | } |
9d7e3f89 | 232 | |
a4922ed9 | 233 | static void smp_mb_master(void) |
9d7e3f89 MD |
234 | { |
235 | force_mb_all_readers(); | |
236 | } | |
fdf01eed | 237 | #endif /* #ifdef RCU_SIGNAL */ |
27b012e2 | 238 | |
bc6c15bb MD |
239 | /* |
240 | * synchronize_rcu() waiting. Single thread. | |
fca9fb96 MD |
241 | * Always called with rcu_registry lock held. Releases this lock and |
242 | * grabs it again. Holds the lock when it returns. | |
bc6c15bb | 243 | */ |
cfe78e25 | 244 | static void wait_gp(void) |
bc6c15bb | 245 | { |
fca9fb96 MD |
246 | /* |
247 | * Read reader_gp before read futex. smp_mb_master() needs to | |
248 | * be called with the rcu registry lock held in RCU_SIGNAL | |
249 | * flavor. | |
250 | */ | |
a4922ed9 | 251 | smp_mb_master(); |
fca9fb96 MD |
252 | /* Temporarily unlock the registry lock. */ |
253 | mutex_unlock(&rcu_registry_lock); | |
b0a841b4 | 254 | if (uatomic_read(&rcu_gp.futex) != -1) |
fca9fb96 | 255 | goto end; |
b0a841b4 MD |
256 | while (futex_async(&rcu_gp.futex, FUTEX_WAIT, -1, |
257 | NULL, NULL, 0)) { | |
258 | switch (errno) { | |
259 | case EWOULDBLOCK: | |
260 | /* Value already changed. */ | |
fca9fb96 | 261 | goto end; |
b0a841b4 MD |
262 | case EINTR: |
263 | /* Retry if interrupted by signal. */ | |
264 | break; /* Get out of switch. */ | |
265 | default: | |
266 | /* Unexpected error. */ | |
267 | urcu_die(errno); | |
268 | } | |
269 | } | |
fca9fb96 MD |
270 | end: |
271 | /* | |
272 | * Re-lock the registry lock before the next loop. | |
273 | */ | |
274 | mutex_lock(&rcu_registry_lock); | |
bc6c15bb MD |
275 | } |
276 | ||
731ccb96 MD |
277 | /* |
278 | * Always called with rcu_registry lock held. Releases this lock between | |
279 | * iterations and grabs it again. Holds the lock when it returns. | |
280 | */ | |
fd189fa5 MD |
281 | static void wait_for_readers(struct cds_list_head *input_readers, |
282 | struct cds_list_head *cur_snap_readers, | |
283 | struct cds_list_head *qsreaders) | |
27b012e2 | 284 | { |
9340c38d | 285 | unsigned int wait_loops = 0; |
02be5561 | 286 | struct rcu_reader *index, *tmp; |
9340c38d MD |
287 | #ifdef HAS_INCOHERENT_CACHES |
288 | unsigned int wait_gp_loops = 0; | |
289 | #endif /* HAS_INCOHERENT_CACHES */ | |
27b012e2 | 290 | |
40e140c9 | 291 | /* |
c9488684 MD |
292 | * Wait for each thread URCU_TLS(rcu_reader).ctr to either |
293 | * indicate quiescence (not nested), or observe the current | |
ed1b099e | 294 | * rcu_gp.ctr value. |
27b012e2 | 295 | */ |
cfe78e25 | 296 | for (;;) { |
5e81fed7 MD |
297 | if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS) |
298 | wait_loops++; | |
9340c38d | 299 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
ed1b099e | 300 | uatomic_dec(&rcu_gp.futex); |
cfe78e25 | 301 | /* Write futex before read reader_gp */ |
a4922ed9 | 302 | smp_mb_master(); |
cfe78e25 MD |
303 | } |
304 | ||
fd189fa5 MD |
305 | cds_list_for_each_entry_safe(index, tmp, input_readers, node) { |
306 | switch (rcu_reader_state(&index->ctr)) { | |
307 | case RCU_READER_ACTIVE_CURRENT: | |
308 | if (cur_snap_readers) { | |
309 | cds_list_move(&index->node, | |
310 | cur_snap_readers); | |
311 | break; | |
312 | } | |
313 | /* Fall-through */ | |
314 | case RCU_READER_INACTIVE: | |
315 | cds_list_move(&index->node, qsreaders); | |
316 | break; | |
317 | case RCU_READER_ACTIVE_OLD: | |
318 | /* | |
319 | * Old snapshot. Leaving node in | |
320 | * input_readers will make us busy-loop | |
321 | * until the snapshot becomes current or | |
322 | * the reader becomes inactive. | |
323 | */ | |
324 | break; | |
325 | } | |
cfe78e25 MD |
326 | } |
327 | ||
e8043c1b | 328 | #ifndef HAS_INCOHERENT_CACHES |
fd189fa5 | 329 | if (cds_list_empty(input_readers)) { |
9340c38d | 330 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
cfe78e25 | 331 | /* Read reader_gp before write futex */ |
a4922ed9 | 332 | smp_mb_master(); |
ed1b099e | 333 | uatomic_set(&rcu_gp.futex, 0); |
bc6c15bb | 334 | } |
cfe78e25 MD |
335 | break; |
336 | } else { | |
fca9fb96 MD |
337 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
338 | /* wait_gp unlocks/locks registry lock. */ | |
cfe78e25 | 339 | wait_gp(); |
fca9fb96 MD |
340 | } else { |
341 | /* Temporarily unlock the registry lock. */ | |
342 | mutex_unlock(&rcu_registry_lock); | |
06f22bdb | 343 | caa_cpu_relax(); |
fca9fb96 MD |
344 | /* |
345 | * Re-lock the registry lock before the | |
346 | * next loop. | |
347 | */ | |
348 | mutex_lock(&rcu_registry_lock); | |
349 | } | |
bc6c15bb | 350 | } |
e8043c1b | 351 | #else /* #ifndef HAS_INCOHERENT_CACHES */ |
27b012e2 | 352 | /* |
40e140c9 | 353 | * BUSY-LOOP. Force the reader thread to commit its |
bd252a04 MD |
354 | * URCU_TLS(rcu_reader).ctr update to memory if we wait |
355 | * for too long. | |
27b012e2 | 356 | */ |
fd189fa5 | 357 | if (cds_list_empty(input_readers)) { |
9340c38d | 358 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { |
cfe78e25 | 359 | /* Read reader_gp before write futex */ |
a4922ed9 | 360 | smp_mb_master(); |
ed1b099e | 361 | uatomic_set(&rcu_gp.futex, 0); |
cfe78e25 MD |
362 | } |
363 | break; | |
364 | } else { | |
9340c38d | 365 | if (wait_gp_loops == KICK_READER_LOOPS) { |
a4922ed9 | 366 | smp_mb_master(); |
9340c38d MD |
367 | wait_gp_loops = 0; |
368 | } | |
369 | if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) { | |
fca9fb96 | 370 | /* wait_gp unlocks/locks registry lock. */ |
9340c38d MD |
371 | wait_gp(); |
372 | wait_gp_loops++; | |
373 | } else { | |
fca9fb96 MD |
374 | /* Temporarily unlock the registry lock. */ |
375 | mutex_unlock(&rcu_registry_lock); | |
06f22bdb | 376 | caa_cpu_relax(); |
fca9fb96 MD |
377 | /* |
378 | * Re-lock the registry lock before the | |
379 | * next loop. | |
380 | */ | |
381 | mutex_lock(&rcu_registry_lock); | |
40e140c9 MD |
382 | } |
383 | } | |
e8043c1b | 384 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ |
27b012e2 | 385 | } |
27b012e2 MD |
386 | } |
387 | ||
9598a481 | 388 | void synchronize_rcu(void) |
2bc59bd7 | 389 | { |
fd189fa5 MD |
390 | CDS_LIST_HEAD(cur_snap_readers); |
391 | CDS_LIST_HEAD(qsreaders); | |
5bffdd5d MD |
392 | DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING); |
393 | struct urcu_waiters waiters; | |
394 | ||
395 | /* | |
396 | * Add ourself to gp_waiters queue of threads awaiting to wait | |
397 | * for a grace period. Proceed to perform the grace period only | |
398 | * if we are the first thread added into the queue. | |
399 | * The implicit memory barrier before urcu_wait_add() | |
400 | * orders prior memory accesses of threads put into the wait | |
401 | * queue before their insertion into the wait queue. | |
402 | */ | |
403 | if (urcu_wait_add(&gp_waiters, &wait) != 0) { | |
404 | /* Not first in queue: will be awakened by another thread. */ | |
405 | urcu_adaptative_busy_wait(&wait); | |
406 | /* Order following memory accesses after grace period. */ | |
407 | cmm_smp_mb(); | |
408 | return; | |
409 | } | |
410 | /* We won't need to wake ourself up */ | |
411 | urcu_wait_set_state(&wait, URCU_WAIT_RUNNING); | |
fd189fa5 | 412 | |
6abb4bd5 | 413 | mutex_lock(&rcu_gp_lock); |
135530fd | 414 | |
5bffdd5d MD |
415 | /* |
416 | * Move all waiters into our local queue. | |
417 | */ | |
418 | urcu_move_waiters(&waiters, &gp_waiters); | |
419 | ||
731ccb96 MD |
420 | mutex_lock(&rcu_registry_lock); |
421 | ||
16aa9ee8 | 422 | if (cds_list_empty(®istry)) |
2dfb8b5e MD |
423 | goto out; |
424 | ||
731ccb96 MD |
425 | /* |
426 | * All threads should read qparity before accessing data structure | |
427 | * where new ptr points to. Must be done within rcu_registry_lock | |
428 | * because it iterates on reader threads. | |
429 | */ | |
9598a481 | 430 | /* Write new ptr before changing the qparity */ |
a4922ed9 | 431 | smp_mb_master(); |
9598a481 | 432 | |
9598a481 | 433 | /* |
c9488684 | 434 | * Wait for readers to observe original parity or be quiescent. |
731ccb96 MD |
435 | * wait_for_readers() can release and grab again rcu_registry_lock |
436 | * interally. | |
9598a481 | 437 | */ |
fd189fa5 | 438 | wait_for_readers(®istry, &cur_snap_readers, &qsreaders); |
9598a481 MD |
439 | |
440 | /* | |
c9488684 | 441 | * Must finish waiting for quiescent state for original parity before |
ed1b099e | 442 | * committing next rcu_gp.ctr update to memory. Failure to do so could |
d40fde2c MD |
443 | * result in the writer waiting forever while new readers are always |
444 | * accessing data (no progress). Enforce compiler-order of load | |
ed1b099e | 445 | * URCU_TLS(rcu_reader).ctr before store to rcu_gp.ctr. |
9598a481 | 446 | */ |
5481ddb3 | 447 | cmm_barrier(); |
9598a481 | 448 | |
5dba80f9 | 449 | /* |
5481ddb3 | 450 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the |
5dba80f9 MD |
451 | * model easier to understand. It does not have a big performance impact |
452 | * anyway, given this is the write-side. | |
453 | */ | |
5481ddb3 | 454 | cmm_smp_mb(); |
67c2d80b | 455 | |
c9488684 | 456 | /* Switch parity: 0 -> 1, 1 -> 0 */ |
ed1b099e | 457 | CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ RCU_GP_CTR_PHASE); |
c9488684 MD |
458 | |
459 | /* | |
ed1b099e | 460 | * Must commit rcu_gp.ctr update to memory before waiting for quiescent |
c9488684 MD |
461 | * state. Failure to do so could result in the writer waiting forever |
462 | * while new readers are always accessing data (no progress). Enforce | |
ed1b099e | 463 | * compiler-order of store to rcu_gp.ctr before load rcu_reader ctr. |
c9488684 MD |
464 | */ |
465 | cmm_barrier(); | |
466 | ||
467 | /* | |
468 | * | |
469 | * Adding a cmm_smp_mb() which is _not_ formally required, but makes the | |
470 | * model easier to understand. It does not have a big performance impact | |
471 | * anyway, given this is the write-side. | |
472 | */ | |
473 | cmm_smp_mb(); | |
474 | ||
9598a481 | 475 | /* |
c9488684 | 476 | * Wait for readers to observe new parity or be quiescent. |
731ccb96 MD |
477 | * wait_for_readers() can release and grab again rcu_registry_lock |
478 | * interally. | |
9598a481 | 479 | */ |
fd189fa5 MD |
480 | wait_for_readers(&cur_snap_readers, NULL, &qsreaders); |
481 | ||
482 | /* | |
483 | * Put quiescent reader list back into registry. | |
484 | */ | |
485 | cds_list_splice(&qsreaders, ®istry); | |
9598a481 | 486 | |
731ccb96 MD |
487 | /* |
488 | * Finish waiting for reader threads before letting the old ptr | |
489 | * being freed. Must be done within rcu_registry_lock because it | |
490 | * iterates on reader threads. | |
491 | */ | |
a4922ed9 | 492 | smp_mb_master(); |
2dfb8b5e | 493 | out: |
731ccb96 | 494 | mutex_unlock(&rcu_registry_lock); |
6abb4bd5 | 495 | mutex_unlock(&rcu_gp_lock); |
5bffdd5d MD |
496 | |
497 | /* | |
498 | * Wakeup waiters only after we have completed the grace period | |
499 | * and have ensured the memory barriers at the end of the grace | |
500 | * period have been issued. | |
501 | */ | |
502 | urcu_wake_all_waiters(&waiters); | |
2bc59bd7 PM |
503 | } |
504 | ||
121a5d44 MD |
505 | /* |
506 | * library wrappers to be used by non-LGPL compatible source code. | |
507 | */ | |
508 | ||
509 | void rcu_read_lock(void) | |
510 | { | |
511 | _rcu_read_lock(); | |
512 | } | |
513 | ||
514 | void rcu_read_unlock(void) | |
515 | { | |
516 | _rcu_read_unlock(); | |
517 | } | |
518 | ||
882f3357 MD |
519 | int rcu_read_ongoing(void) |
520 | { | |
521 | return _rcu_read_ongoing(); | |
522 | } | |
523 | ||
121a5d44 | 524 | void rcu_register_thread(void) |
27b012e2 | 525 | { |
bd252a04 MD |
526 | URCU_TLS(rcu_reader).tid = pthread_self(); |
527 | assert(URCU_TLS(rcu_reader).need_mb == 0); | |
528 | assert(!(URCU_TLS(rcu_reader).ctr & RCU_GP_CTR_NEST_MASK)); | |
02be5561 | 529 | |
731ccb96 | 530 | mutex_lock(&rcu_registry_lock); |
a77f7d82 MD |
531 | assert(!URCU_TLS(rcu_reader).registered); |
532 | URCU_TLS(rcu_reader).registered = 1; | |
02be5561 | 533 | rcu_init(); /* In case gcc does not support constructor attribute */ |
bd252a04 | 534 | cds_list_add(&URCU_TLS(rcu_reader).node, ®istry); |
731ccb96 | 535 | mutex_unlock(&rcu_registry_lock); |
27b012e2 MD |
536 | } |
537 | ||
121a5d44 | 538 | void rcu_unregister_thread(void) |
27b012e2 | 539 | { |
731ccb96 | 540 | mutex_lock(&rcu_registry_lock); |
a77f7d82 MD |
541 | assert(URCU_TLS(rcu_reader).registered); |
542 | URCU_TLS(rcu_reader).registered = 0; | |
bd252a04 | 543 | cds_list_del(&URCU_TLS(rcu_reader).node); |
731ccb96 | 544 | mutex_unlock(&rcu_registry_lock); |
27b012e2 MD |
545 | } |
546 | ||
fdf01eed | 547 | #ifdef RCU_MEMBARRIER |
d8d9a340 MD |
548 | |
549 | #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER | |
550 | static | |
c0bb9f69 | 551 | void rcu_sys_membarrier_status(bool available) |
d8d9a340 MD |
552 | { |
553 | if (!available) | |
554 | abort(); | |
555 | } | |
556 | #else | |
557 | static | |
c0bb9f69 | 558 | void rcu_sys_membarrier_status(bool available) |
d8d9a340 | 559 | { |
c0bb9f69 MD |
560 | if (!available) |
561 | return; | |
562 | rcu_has_sys_membarrier_memb = 1; | |
d8d9a340 MD |
563 | } |
564 | #endif | |
565 | ||
c0bb9f69 MD |
566 | static |
567 | void rcu_sys_membarrier_init(void) | |
fdf01eed | 568 | { |
c0bb9f69 MD |
569 | bool available = false; |
570 | int mask; | |
571 | ||
572 | mask = membarrier(MEMBARRIER_CMD_QUERY, 0); | |
573 | if (mask >= 0) { | |
574 | if (mask & MEMBARRIER_CMD_PRIVATE_EXPEDITED) { | |
575 | if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0)) | |
576 | urcu_die(errno); | |
577 | has_sys_membarrier_private_expedited = 1; | |
578 | available = true; | |
579 | } else if (mask & MEMBARRIER_CMD_SHARED) { | |
580 | available = true; | |
581 | } | |
582 | } | |
583 | rcu_sys_membarrier_status(available); | |
584 | } | |
64f469e6 | 585 | |
c0bb9f69 MD |
586 | void rcu_init(void) |
587 | { | |
fdf01eed MD |
588 | if (init_done) |
589 | return; | |
590 | init_done = 1; | |
c0bb9f69 | 591 | rcu_sys_membarrier_init(); |
fdf01eed MD |
592 | } |
593 | #endif | |
594 | ||
595 | #ifdef RCU_SIGNAL | |
02be5561 | 596 | static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context) |
27b012e2 | 597 | { |
40e140c9 | 598 | /* |
5481ddb3 DG |
599 | * Executing this cmm_smp_mb() is the only purpose of this signal handler. |
600 | * It punctually promotes cmm_barrier() into cmm_smp_mb() on every thread it is | |
40e140c9 MD |
601 | * executed on. |
602 | */ | |
5481ddb3 | 603 | cmm_smp_mb(); |
bd252a04 | 604 | _CMM_STORE_SHARED(URCU_TLS(rcu_reader).need_mb, 0); |
5481ddb3 | 605 | cmm_smp_mb(); |
27b012e2 MD |
606 | } |
607 | ||
8a5fb4c9 | 608 | /* |
02be5561 | 609 | * rcu_init constructor. Called when the library is linked, but also when |
8a5fb4c9 MD |
610 | * reader threads are calling rcu_register_thread(). |
611 | * Should only be called by a single thread at a given time. This is ensured by | |
731ccb96 MD |
612 | * holing the rcu_registry_lock from rcu_register_thread() or by running |
613 | * at library load time, which should not be executed by multiple | |
614 | * threads nor concurrently with rcu_register_thread() anyway. | |
8a5fb4c9 | 615 | */ |
02be5561 | 616 | void rcu_init(void) |
27b012e2 MD |
617 | { |
618 | struct sigaction act; | |
619 | int ret; | |
620 | ||
8a5fb4c9 MD |
621 | if (init_done) |
622 | return; | |
623 | init_done = 1; | |
624 | ||
02be5561 | 625 | act.sa_sigaction = sigrcu_handler; |
dd052bd3 | 626 | act.sa_flags = SA_SIGINFO | SA_RESTART; |
c297c21c | 627 | sigemptyset(&act.sa_mask); |
02be5561 | 628 | ret = sigaction(SIGRCU, &act, NULL); |
4a6d7378 MD |
629 | if (ret) |
630 | urcu_die(errno); | |
27b012e2 MD |
631 | } |
632 | ||
02be5561 | 633 | void rcu_exit(void) |
27b012e2 | 634 | { |
71210954 MD |
635 | /* |
636 | * Don't unregister the SIGRCU signal handler anymore, because | |
637 | * call_rcu threads could still be using it shortly before the | |
638 | * application exits. | |
639 | * Assertion disabled because call_rcu threads are now rcu | |
640 | * readers, and left running at exit. | |
641 | * assert(cds_list_empty(®istry)); | |
642 | */ | |
27b012e2 | 643 | } |
5e77fc1f | 644 | |
fdf01eed | 645 | #endif /* #ifdef RCU_SIGNAL */ |
5e77fc1f | 646 | |
5e6b23a6 | 647 | DEFINE_RCU_FLAVOR(rcu_flavor); |
541d828d | 648 | |
5e77fc1f | 649 | #include "urcu-call-rcu-impl.h" |
0376e7b2 | 650 | #include "urcu-defer-impl.h" |