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