uatomic/x86: Remove redundant memory barriers
[urcu.git] / src / urcu-qsbr.c
CommitLineData
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
9f1621ca 6/*
7ac06cef 7 * Userspace RCU QSBR library
9f1621ca 8 *
9f1621ca
MD
9 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
10 */
11
e37faee1 12#define URCU_NO_COMPAT_IDENTIFIERS
71c811bf 13#define _LGPL_SOURCE
9f1621ca
MD
14#include <stdio.h>
15#include <pthread.h>
16#include <signal.h>
9f1621ca 17#include <stdlib.h>
6d841bc2 18#include <stdint.h>
9f1621ca
MD
19#include <string.h>
20#include <errno.h>
21#include <poll.h>
22
601922a8 23#include <urcu/annotate.h>
01477510 24#include <urcu/assert.h>
4477a870
MD
25#include <urcu/wfcqueue.h>
26#include <urcu/map/urcu-qsbr.h>
727f819d 27#define BUILD_QSBR_LIB
4477a870
MD
28#include <urcu/static/urcu-qsbr.h>
29#include <urcu/pointer.h>
30#include <urcu/tls-compat.h>
71c811bf 31
4a6d7378 32#include "urcu-die.h"
cba82d7b 33#include "urcu-wait.h"
ce28e67a 34#include "urcu-utils.h"
4a6d7378 35
4477a870 36#define URCU_API_MAP
9f1621ca 37/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
71c811bf 38#undef _LGPL_SOURCE
4477a870 39#include <urcu/urcu-qsbr.h>
71c811bf 40#define _LGPL_SOURCE
9f1621ca 41
4477a870 42void __attribute__((destructor)) urcu_qsbr_exit(void);
90f72b8c 43static void urcu_call_rcu_exit(void);
f6d18c64 44
731ccb96
MD
45/*
46 * rcu_gp_lock ensures mutual exclusion between threads calling
47 * synchronize_rcu().
48 */
6abb4bd5 49static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
731ccb96
MD
50/*
51 * rcu_registry_lock ensures mutual exclusion between threads
52 * registering and unregistering themselves to/from the registry, and
53 * with threads reading that registry from synchronize_rcu(). However,
54 * this lock is not held all the way through the completion of awaiting
55 * for the grace period. It is sporadically released between iterations
56 * on the registry.
57 * rcu_registry_lock may nest inside rcu_gp_lock.
58 */
59static pthread_mutex_t rcu_registry_lock = PTHREAD_MUTEX_INITIALIZER;
4477a870 60struct urcu_gp urcu_qsbr_gp = { .ctr = URCU_QSBR_GP_ONLINE };
9f1621ca 61
408f6d92
PB
62/*
63 * Active attempts to check for reader Q.S. before calling futex().
64 */
65#define RCU_QS_ACTIVE_ATTEMPTS 100
66
9f1621ca
MD
67/*
68 * Written to only by each individual reader. Read by both the reader and the
69 * writers.
70 */
4477a870 71DEFINE_URCU_TLS(struct urcu_qsbr_reader, urcu_qsbr_reader);
9f1621ca 72
16aa9ee8 73static CDS_LIST_HEAD(registry);
9f1621ca 74
6362f68f 75/*
bf6822a6 76 * Queue keeping threads awaiting to wait for a grace period. Contains
6362f68f
MD
77 * struct gp_waiters_thread objects.
78 */
bf6822a6 79static DEFINE_URCU_WAIT_QUEUE(gp_waiters);
6362f68f 80
6abb4bd5 81static void mutex_lock(pthread_mutex_t *mutex)
9f1621ca
MD
82{
83 int ret;
84
85#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 86 ret = pthread_mutex_lock(mutex);
4a6d7378
MD
87 if (ret)
88 urcu_die(ret);
9f1621ca 89#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 90 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
4a6d7378
MD
91 if (ret != EBUSY && ret != EINTR)
92 urcu_die(ret);
9f1621ca
MD
93 poll(NULL,0,10);
94 }
95#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
96}
97
6abb4bd5 98static void mutex_unlock(pthread_mutex_t *mutex)
9f1621ca
MD
99{
100 int ret;
101
6abb4bd5 102 ret = pthread_mutex_unlock(mutex);
4a6d7378
MD
103 if (ret)
104 urcu_die(ret);
9f1621ca
MD
105}
106
bc6c15bb
MD
107/*
108 * synchronize_rcu() waiting. Single thread.
109 */
4d703340 110static void wait_gp(void)
bc6c15bb 111{
4d703340 112 /* Read reader_gp before read futex */
5481ddb3 113 cmm_smp_rmb();
4974ad5f
MD
114 while (uatomic_read(&urcu_qsbr_gp.futex) == -1) {
115 if (!futex_noasync(&urcu_qsbr_gp.futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
116 /*
117 * Prior queued wakeups queued by unrelated code
118 * using the same address can cause futex wait to
119 * return 0 even through the futex value is still
120 * -1 (spurious wakeups). Check the value again
121 * in user-space to validate whether it really
122 * differs from -1.
123 */
124 continue;
125 }
b0a841b4 126 switch (errno) {
4974ad5f 127 case EAGAIN:
b0a841b4
MD
128 /* Value already changed. */
129 return;
130 case EINTR:
131 /* Retry if interrupted by signal. */
4974ad5f 132 break; /* Get out of switch. Check again. */
b0a841b4
MD
133 default:
134 /* Unexpected error. */
135 urcu_die(errno);
136 }
137 }
bc6c15bb
MD
138}
139
731ccb96
MD
140/*
141 * Always called with rcu_registry lock held. Releases this lock between
142 * iterations and grabs it again. Holds the lock when it returns.
143 */
708d89f0
MD
144static void wait_for_readers(struct cds_list_head *input_readers,
145 struct cds_list_head *cur_snap_readers,
601922a8
OD
146 struct cds_list_head *qsreaders,
147 cmm_annotate_t *group)
9f1621ca 148{
9340c38d 149 unsigned int wait_loops = 0;
4477a870 150 struct urcu_qsbr_reader *index, *tmp;
9f1621ca 151
9f1621ca 152 /*
4477a870 153 * Wait for each thread URCU_TLS(urcu_qsbr_reader).ctr to either
f6b42f9c 154 * indicate quiescence (offline), or for them to observe the
4477a870 155 * current urcu_qsbr_gp.ctr value.
9f1621ca 156 */
4d703340 157 for (;;) {
5e81fed7
MD
158 if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
159 wait_loops++;
83a2c421 160 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
4477a870 161 uatomic_set(&urcu_qsbr_gp.futex, -1);
83a2c421
PB
162 /*
163 * Write futex before write waiting (the other side
164 * reads them in the opposite order).
165 */
166 cmm_smp_wmb();
708d89f0 167 cds_list_for_each_entry(index, input_readers, node) {
83a2c421
PB
168 _CMM_STORE_SHARED(index->waiting, 1);
169 }
4d703340 170 /* Write futex before read reader_gp */
5481ddb3 171 cmm_smp_mb();
4d703340 172 }
708d89f0 173 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
601922a8 174 switch (urcu_qsbr_reader_state(&index->ctr, group)) {
4477a870 175 case URCU_READER_ACTIVE_CURRENT:
708d89f0
MD
176 if (cur_snap_readers) {
177 cds_list_move(&index->node,
178 cur_snap_readers);
179 break;
180 }
181 /* Fall-through */
4477a870 182 case URCU_READER_INACTIVE:
708d89f0
MD
183 cds_list_move(&index->node, qsreaders);
184 break;
4477a870 185 case URCU_READER_ACTIVE_OLD:
708d89f0
MD
186 /*
187 * Old snapshot. Leaving node in
188 * input_readers will make us busy-loop
189 * until the snapshot becomes current or
190 * the reader becomes inactive.
191 */
192 break;
193 }
4d703340 194 }
bc6c15bb 195
708d89f0 196 if (cds_list_empty(input_readers)) {
83a2c421 197 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
4d703340 198 /* Read reader_gp before write futex */
601922a8 199 uatomic_store(&urcu_qsbr_gp.futex, 0, CMM_RELEASE);
4d703340
MD
200 }
201 break;
202 } else {
731ccb96
MD
203 /* Temporarily unlock the registry lock. */
204 mutex_unlock(&rcu_registry_lock);
83a2c421 205 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
4d703340 206 wait_gp();
bc6c15bb 207 } else {
9f1621ca 208#ifndef HAS_INCOHERENT_CACHES
06f22bdb 209 caa_cpu_relax();
9f1621ca 210#else /* #ifndef HAS_INCOHERENT_CACHES */
5481ddb3 211 cmm_smp_mb();
9f1621ca 212#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
bc6c15bb 213 }
731ccb96
MD
214 /* Re-lock the registry lock before the next loop. */
215 mutex_lock(&rcu_registry_lock);
bc6c15bb 216 }
9f1621ca
MD
217 }
218}
219
47d2f29e
MD
220/*
221 * Using a two-subphases algorithm for architectures with smaller than 64-bit
222 * long-size to ensure we do not encounter an overflow bug.
223 */
224
b39e1761 225#if (CAA_BITS_PER_LONG < 64)
4477a870 226void urcu_qsbr_synchronize_rcu(void)
47d2f29e 227{
601922a8
OD
228 cmm_annotate_define(acquire_group);
229 cmm_annotate_define(release_group);
708d89f0
MD
230 CDS_LIST_HEAD(cur_snap_readers);
231 CDS_LIST_HEAD(qsreaders);
bc49c323 232 unsigned long was_online;
bf6822a6
MD
233 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
234 struct urcu_waiters waiters;
bc49c323 235
4477a870 236 was_online = urcu_qsbr_read_ongoing();
bc49c323 237
47d2f29e 238 /* All threads should read qparity before accessing data structure
27b940e7
PB
239 * where new ptr points to. In the "then" case, rcu_thread_offline
240 * includes a memory barrier.
241 *
bc49c323 242 * Mark the writer thread offline to make sure we don't wait for
5e77fc1f
PM
243 * our own quiescent state. This allows using synchronize_rcu()
244 * in threads registered as readers.
bc49c323 245 */
27b940e7 246 if (was_online)
4477a870 247 urcu_qsbr_thread_offline();
27b940e7
PB
248 else
249 cmm_smp_mb();
601922a8 250 cmm_annotate_group_mb_release(&release_group);
bc49c323 251
6362f68f 252 /*
bf6822a6 253 * Add ourself to gp_waiters queue of threads awaiting to wait
6362f68f 254 * for a grace period. Proceed to perform the grace period only
bf6822a6 255 * if we are the first thread added into the queue.
6362f68f 256 */
bf6822a6
MD
257 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
258 /* Not first in queue: will be awakened by another thread. */
259 urcu_adaptative_busy_wait(&wait);
6362f68f
MD
260 goto gp_end;
261 }
bf6822a6
MD
262 /* We won't need to wake ourself up */
263 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
6362f68f 264
6abb4bd5 265 mutex_lock(&rcu_gp_lock);
47d2f29e 266
6362f68f 267 /*
bf6822a6 268 * Move all waiters into our local queue.
6362f68f 269 */
bf6822a6 270 urcu_move_waiters(&waiters, &gp_waiters);
6362f68f 271
731ccb96
MD
272 mutex_lock(&rcu_registry_lock);
273
16aa9ee8 274 if (cds_list_empty(&registry))
2dfb8b5e 275 goto out;
47d2f29e
MD
276
277 /*
f6b42f9c 278 * Wait for readers to observe original parity or be quiescent.
731ccb96 279 * wait_for_readers() can release and grab again rcu_registry_lock
f99c6e92 280 * internally.
47d2f29e 281 */
601922a8 282 wait_for_readers(&registry, &cur_snap_readers, &qsreaders, &acquire_group);
47d2f29e
MD
283
284 /*
f6b42f9c 285 * Must finish waiting for quiescent state for original parity
4477a870 286 * before committing next urcu_qsbr_gp.ctr update to memory. Failure
f6b42f9c 287 * to do so could result in the writer waiting forever while new
5e77fc1f 288 * readers are always accessing data (no progress). Enforce
4477a870
MD
289 * compiler-order of load URCU_TLS(urcu_qsbr_reader).ctr before store
290 * to urcu_qsbr_gp.ctr.
47d2f29e 291 */
5481ddb3 292 cmm_barrier();
47d2f29e 293
47d2f29e 294 /*
5481ddb3 295 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
2dfb8b5e
MD
296 * model easier to understand. It does not have a big performance impact
297 * anyway, given this is the write-side.
47d2f29e 298 */
5481ddb3 299 cmm_smp_mb();
47d2f29e 300
f6b42f9c 301 /* Switch parity: 0 -> 1, 1 -> 0 */
601922a8
OD
302 cmm_annotate_group_mem_release(&release_group, &urcu_qsbr_gp.ctr);
303 uatomic_store(&urcu_qsbr_gp.ctr, urcu_qsbr_gp.ctr ^ URCU_QSBR_GP_CTR, CMM_RELAXED);
f6b42f9c 304
47d2f29e 305 /*
4477a870 306 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
f6b42f9c
MD
307 * quiescent state. Failure to do so could result in the writer
308 * waiting forever while new readers are always accessing data
4477a870
MD
309 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
310 * before load URCU_TLS(urcu_qsbr_reader).ctr.
47d2f29e 311 */
f6b42f9c
MD
312 cmm_barrier();
313
314 /*
315 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
316 * model easier to understand. It does not have a big performance impact
317 * anyway, given this is the write-side.
318 */
319 cmm_smp_mb();
320
321 /*
322 * Wait for readers to observe new parity or be quiescent.
731ccb96 323 * wait_for_readers() can release and grab again rcu_registry_lock
f99c6e92 324 * internally.
f6b42f9c 325 */
601922a8 326 wait_for_readers(&cur_snap_readers, NULL, &qsreaders, &acquire_group);
708d89f0
MD
327
328 /*
329 * Put quiescent reader list back into registry.
330 */
331 cds_list_splice(&qsreaders, &registry);
2dfb8b5e 332out:
731ccb96 333 mutex_unlock(&rcu_registry_lock);
6abb4bd5 334 mutex_unlock(&rcu_gp_lock);
bf6822a6 335 urcu_wake_all_waiters(&waiters);
6362f68f 336gp_end:
bc49c323
MD
337 /*
338 * Finish waiting for reader threads before letting the old ptr being
47d2f29e
MD
339 * freed.
340 */
601922a8
OD
341 cmm_annotate_group_mb_acquire(&acquire_group);
342
bc49c323 343 if (was_online)
4477a870 344 urcu_qsbr_thread_online();
27b940e7
PB
345 else
346 cmm_smp_mb();
47d2f29e 347}
b39e1761 348#else /* !(CAA_BITS_PER_LONG < 64) */
4477a870 349void urcu_qsbr_synchronize_rcu(void)
9f1621ca 350{
601922a8
OD
351 cmm_annotate_define(acquire_group);
352 cmm_annotate_define(release_group);
708d89f0 353 CDS_LIST_HEAD(qsreaders);
f0f7dbdd 354 unsigned long was_online;
bf6822a6
MD
355 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
356 struct urcu_waiters waiters;
ff2f67a0 357
4477a870 358 was_online = urcu_qsbr_read_ongoing();
ff2f67a0
MD
359
360 /*
361 * Mark the writer thread offline to make sure we don't wait for
5e77fc1f
PM
362 * our own quiescent state. This allows using synchronize_rcu()
363 * in threads registered as readers.
ff2f67a0 364 */
27b940e7 365 if (was_online)
4477a870 366 urcu_qsbr_thread_offline();
27b940e7
PB
367 else
368 cmm_smp_mb();
601922a8 369 cmm_annotate_group_mb_release(&release_group);
ff2f67a0 370
6362f68f 371 /*
bf6822a6 372 * Add ourself to gp_waiters queue of threads awaiting to wait
6362f68f 373 * for a grace period. Proceed to perform the grace period only
bf6822a6 374 * if we are the first thread added into the queue.
6362f68f 375 */
bf6822a6
MD
376 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
377 /* Not first in queue: will be awakened by another thread. */
378 urcu_adaptative_busy_wait(&wait);
6362f68f
MD
379 goto gp_end;
380 }
bf6822a6
MD
381 /* We won't need to wake ourself up */
382 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
6362f68f 383
6abb4bd5 384 mutex_lock(&rcu_gp_lock);
6362f68f
MD
385
386 /*
bf6822a6 387 * Move all waiters into our local queue.
6362f68f 388 */
bf6822a6 389 urcu_move_waiters(&waiters, &gp_waiters);
6362f68f 390
731ccb96
MD
391 mutex_lock(&rcu_registry_lock);
392
16aa9ee8 393 if (cds_list_empty(&registry))
2dfb8b5e 394 goto out;
f6b42f9c
MD
395
396 /* Increment current G.P. */
601922a8
OD
397 cmm_annotate_group_mem_release(&release_group, &urcu_qsbr_gp.ctr);
398 uatomic_store(&urcu_qsbr_gp.ctr, urcu_qsbr_gp.ctr + URCU_QSBR_GP_CTR, CMM_RELAXED);
f6b42f9c
MD
399
400 /*
4477a870 401 * Must commit urcu_qsbr_gp.ctr update to memory before waiting for
f6b42f9c
MD
402 * quiescent state. Failure to do so could result in the writer
403 * waiting forever while new readers are always accessing data
4477a870
MD
404 * (no progress). Enforce compiler-order of store to urcu_qsbr_gp.ctr
405 * before load URCU_TLS(urcu_qsbr_reader).ctr.
f6b42f9c
MD
406 */
407 cmm_barrier();
408
409 /*
410 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
411 * model easier to understand. It does not have a big performance impact
412 * anyway, given this is the write-side.
413 */
414 cmm_smp_mb();
415
416 /*
417 * Wait for readers to observe new count of be quiescent.
731ccb96 418 * wait_for_readers() can release and grab again rcu_registry_lock
f99c6e92 419 * internally.
f6b42f9c 420 */
601922a8 421 wait_for_readers(&registry, NULL, &qsreaders, &acquire_group);
708d89f0
MD
422
423 /*
424 * Put quiescent reader list back into registry.
425 */
426 cds_list_splice(&qsreaders, &registry);
2dfb8b5e 427out:
731ccb96 428 mutex_unlock(&rcu_registry_lock);
6abb4bd5 429 mutex_unlock(&rcu_gp_lock);
bf6822a6 430 urcu_wake_all_waiters(&waiters);
6362f68f 431gp_end:
ff2f67a0 432 if (was_online)
4477a870 433 urcu_qsbr_thread_online();
27b940e7
PB
434 else
435 cmm_smp_mb();
601922a8
OD
436
437 cmm_annotate_group_mb_acquire(&acquire_group);
9f1621ca 438}
b39e1761 439#endif /* !(CAA_BITS_PER_LONG < 64) */
9f1621ca
MD
440
441/*
442 * library wrappers to be used by non-LGPL compatible source code.
443 */
444
4477a870 445void urcu_qsbr_read_lock(void)
9f1621ca 446{
4477a870 447 _urcu_qsbr_read_lock();
9f1621ca
MD
448}
449
4477a870 450void urcu_qsbr_read_unlock(void)
9f1621ca 451{
4477a870 452 _urcu_qsbr_read_unlock();
9f1621ca
MD
453}
454
4477a870 455int urcu_qsbr_read_ongoing(void)
882f3357 456{
4477a870 457 return _urcu_qsbr_read_ongoing();
882f3357 458}
4477a870 459void rcu_read_ongoing_qsbr();
882f3357 460
4477a870 461void urcu_qsbr_quiescent_state(void)
7ac06cef 462{
4477a870 463 _urcu_qsbr_quiescent_state();
7ac06cef 464}
4477a870 465void rcu_quiescent_state_qsbr();
7ac06cef 466
4477a870 467void urcu_qsbr_thread_offline(void)
7ac06cef 468{
4477a870 469 _urcu_qsbr_thread_offline();
7ac06cef 470}
4477a870 471void rcu_thread_offline_qsbr();
7ac06cef 472
4477a870 473void urcu_qsbr_thread_online(void)
7ac06cef 474{
4477a870 475 _urcu_qsbr_thread_online();
7ac06cef
MD
476}
477
4477a870 478void urcu_qsbr_register_thread(void)
9f1621ca 479{
4477a870 480 URCU_TLS(urcu_qsbr_reader).tid = pthread_self();
01477510 481 urcu_posix_assert(URCU_TLS(urcu_qsbr_reader).ctr == 0);
4f8e3380 482
731ccb96 483 mutex_lock(&rcu_registry_lock);
01477510 484 urcu_posix_assert(!URCU_TLS(urcu_qsbr_reader).registered);
4477a870
MD
485 URCU_TLS(urcu_qsbr_reader).registered = 1;
486 cds_list_add(&URCU_TLS(urcu_qsbr_reader).node, &registry);
731ccb96 487 mutex_unlock(&rcu_registry_lock);
4477a870 488 _urcu_qsbr_thread_online();
9f1621ca
MD
489}
490
4477a870 491void urcu_qsbr_unregister_thread(void)
9f1621ca 492{
76f3022f
MD
493 /*
494 * We have to make the thread offline otherwise we end up dealocking
495 * with a waiting writer.
496 */
4477a870 497 _urcu_qsbr_thread_offline();
01477510 498 urcu_posix_assert(URCU_TLS(urcu_qsbr_reader).registered);
4477a870 499 URCU_TLS(urcu_qsbr_reader).registered = 0;
731ccb96 500 mutex_lock(&rcu_registry_lock);
4477a870 501 cds_list_del(&URCU_TLS(urcu_qsbr_reader).node);
731ccb96 502 mutex_unlock(&rcu_registry_lock);
9f1621ca 503}
f6d18c64 504
4477a870 505void urcu_qsbr_exit(void)
f6d18c64 506{
01cadde4
MD
507 /*
508 * Assertion disabled because call_rcu threads are now rcu
509 * readers, and left running at exit.
01477510 510 * urcu_posix_assert(cds_list_empty(&registry));
01cadde4 511 */
90f72b8c 512 urcu_call_rcu_exit();
f6d18c64 513}
5e77fc1f 514
5e6b23a6 515DEFINE_RCU_FLAVOR(rcu_flavor);
541d828d 516
5e77fc1f 517#include "urcu-call-rcu-impl.h"
0376e7b2 518#include "urcu-defer-impl.h"
111bda8f 519#include "urcu-poll-impl.h"
This page took 0.077156 seconds and 5 git commands to generate.