Fix: compat_futex_noasync race condition
[urcu.git] / urcu-qsbr.c
CommitLineData
9f1621ca 1/*
7ac06cef 2 * urcu-qsbr.c
9f1621ca 3 *
7ac06cef 4 * Userspace RCU QSBR library
9f1621ca 5 *
6982d6d7 6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9f1621ca
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
c1d2c60b 26#define _GNU_SOURCE
71c811bf 27#define _LGPL_SOURCE
9f1621ca
MD
28#include <stdio.h>
29#include <pthread.h>
30#include <signal.h>
31#include <assert.h>
32#include <stdlib.h>
6d841bc2 33#include <stdint.h>
9f1621ca
MD
34#include <string.h>
35#include <errno.h>
36#include <poll.h>
37
d73fb81f 38#include "urcu/wfcqueue.h"
57760d44 39#include "urcu/map/urcu-qsbr.h"
727f819d 40#define BUILD_QSBR_LIB
af7c2dbe 41#include "urcu/static/urcu-qsbr.h"
618b2595 42#include "urcu-pointer.h"
bd252a04 43#include "urcu/tls-compat.h"
71c811bf 44
4a6d7378 45#include "urcu-die.h"
cba82d7b 46#include "urcu-wait.h"
4a6d7378 47
9f1621ca 48/* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
71c811bf 49#undef _LGPL_SOURCE
7ac06cef 50#include "urcu-qsbr.h"
71c811bf 51#define _LGPL_SOURCE
9f1621ca 52
f6d18c64
MD
53void __attribute__((destructor)) rcu_exit(void);
54
6abb4bd5 55static pthread_mutex_t rcu_gp_lock = PTHREAD_MUTEX_INITIALIZER;
4de0cd31 56struct rcu_gp rcu_gp = { .ctr = RCU_GP_ONLINE };
9f1621ca 57
408f6d92
PB
58/*
59 * Active attempts to check for reader Q.S. before calling futex().
60 */
61#define RCU_QS_ACTIVE_ATTEMPTS 100
62
9f1621ca
MD
63/*
64 * Written to only by each individual reader. Read by both the reader and the
65 * writers.
66 */
bd252a04 67DEFINE_URCU_TLS(struct rcu_reader, rcu_reader);
9f1621ca 68
16aa9ee8 69static CDS_LIST_HEAD(registry);
9f1621ca 70
6362f68f 71/*
bf6822a6 72 * Queue keeping threads awaiting to wait for a grace period. Contains
6362f68f
MD
73 * struct gp_waiters_thread objects.
74 */
bf6822a6 75static DEFINE_URCU_WAIT_QUEUE(gp_waiters);
6362f68f 76
6abb4bd5 77static void mutex_lock(pthread_mutex_t *mutex)
9f1621ca
MD
78{
79 int ret;
80
81#ifndef DISTRUST_SIGNALS_EXTREME
6abb4bd5 82 ret = pthread_mutex_lock(mutex);
4a6d7378
MD
83 if (ret)
84 urcu_die(ret);
9f1621ca 85#else /* #ifndef DISTRUST_SIGNALS_EXTREME */
6abb4bd5 86 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
4a6d7378
MD
87 if (ret != EBUSY && ret != EINTR)
88 urcu_die(ret);
9f1621ca
MD
89 poll(NULL,0,10);
90 }
91#endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
92}
93
6abb4bd5 94static void mutex_unlock(pthread_mutex_t *mutex)
9f1621ca
MD
95{
96 int ret;
97
6abb4bd5 98 ret = pthread_mutex_unlock(mutex);
4a6d7378
MD
99 if (ret)
100 urcu_die(ret);
9f1621ca
MD
101}
102
bc6c15bb
MD
103/*
104 * synchronize_rcu() waiting. Single thread.
105 */
4d703340 106static void wait_gp(void)
bc6c15bb 107{
4d703340 108 /* Read reader_gp before read futex */
5481ddb3 109 cmm_smp_rmb();
15302e28
LJ
110 if (uatomic_read(&rcu_gp.futex) == -1)
111 futex_noasync(&rcu_gp.futex, FUTEX_WAIT, -1,
4d703340 112 NULL, NULL, 0);
bc6c15bb
MD
113}
114
708d89f0
MD
115static void wait_for_readers(struct cds_list_head *input_readers,
116 struct cds_list_head *cur_snap_readers,
117 struct cds_list_head *qsreaders)
9f1621ca 118{
9340c38d 119 unsigned int wait_loops = 0;
02be5561 120 struct rcu_reader *index, *tmp;
9f1621ca 121
9f1621ca 122 /*
f6b42f9c
MD
123 * Wait for each thread URCU_TLS(rcu_reader).ctr to either
124 * indicate quiescence (offline), or for them to observe the
15302e28 125 * current rcu_gp.ctr value.
9f1621ca 126 */
4d703340 127 for (;;) {
5e81fed7
MD
128 if (wait_loops < RCU_QS_ACTIVE_ATTEMPTS)
129 wait_loops++;
83a2c421 130 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
15302e28 131 uatomic_set(&rcu_gp.futex, -1);
83a2c421
PB
132 /*
133 * Write futex before write waiting (the other side
134 * reads them in the opposite order).
135 */
136 cmm_smp_wmb();
708d89f0 137 cds_list_for_each_entry(index, input_readers, node) {
83a2c421
PB
138 _CMM_STORE_SHARED(index->waiting, 1);
139 }
4d703340 140 /* Write futex before read reader_gp */
5481ddb3 141 cmm_smp_mb();
4d703340 142 }
708d89f0
MD
143 cds_list_for_each_entry_safe(index, tmp, input_readers, node) {
144 switch (rcu_reader_state(&index->ctr)) {
145 case RCU_READER_ACTIVE_CURRENT:
146 if (cur_snap_readers) {
147 cds_list_move(&index->node,
148 cur_snap_readers);
149 break;
150 }
151 /* Fall-through */
152 case RCU_READER_INACTIVE:
153 cds_list_move(&index->node, qsreaders);
154 break;
155 case RCU_READER_ACTIVE_OLD:
156 /*
157 * Old snapshot. Leaving node in
158 * input_readers will make us busy-loop
159 * until the snapshot becomes current or
160 * the reader becomes inactive.
161 */
162 break;
163 }
4d703340 164 }
bc6c15bb 165
708d89f0 166 if (cds_list_empty(input_readers)) {
83a2c421 167 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
4d703340 168 /* Read reader_gp before write futex */
5481ddb3 169 cmm_smp_mb();
15302e28 170 uatomic_set(&rcu_gp.futex, 0);
4d703340
MD
171 }
172 break;
173 } else {
83a2c421 174 if (wait_loops >= RCU_QS_ACTIVE_ATTEMPTS) {
4d703340 175 wait_gp();
bc6c15bb 176 } else {
9f1621ca 177#ifndef HAS_INCOHERENT_CACHES
06f22bdb 178 caa_cpu_relax();
9f1621ca 179#else /* #ifndef HAS_INCOHERENT_CACHES */
5481ddb3 180 cmm_smp_mb();
9f1621ca 181#endif /* #else #ifndef HAS_INCOHERENT_CACHES */
bc6c15bb
MD
182 }
183 }
9f1621ca
MD
184 }
185}
186
47d2f29e
MD
187/*
188 * Using a two-subphases algorithm for architectures with smaller than 64-bit
189 * long-size to ensure we do not encounter an overflow bug.
190 */
191
b39e1761 192#if (CAA_BITS_PER_LONG < 64)
47d2f29e
MD
193void synchronize_rcu(void)
194{
708d89f0
MD
195 CDS_LIST_HEAD(cur_snap_readers);
196 CDS_LIST_HEAD(qsreaders);
bc49c323 197 unsigned long was_online;
bf6822a6
MD
198 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
199 struct urcu_waiters waiters;
bc49c323 200
882f3357 201 was_online = rcu_read_ongoing();
bc49c323 202
47d2f29e 203 /* All threads should read qparity before accessing data structure
27b940e7
PB
204 * where new ptr points to. In the "then" case, rcu_thread_offline
205 * includes a memory barrier.
206 *
bc49c323 207 * Mark the writer thread offline to make sure we don't wait for
5e77fc1f
PM
208 * our own quiescent state. This allows using synchronize_rcu()
209 * in threads registered as readers.
bc49c323 210 */
27b940e7
PB
211 if (was_online)
212 rcu_thread_offline();
213 else
214 cmm_smp_mb();
bc49c323 215
6362f68f 216 /*
bf6822a6 217 * Add ourself to gp_waiters queue of threads awaiting to wait
6362f68f 218 * for a grace period. Proceed to perform the grace period only
bf6822a6 219 * if we are the first thread added into the queue.
6362f68f 220 */
bf6822a6
MD
221 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
222 /* Not first in queue: will be awakened by another thread. */
223 urcu_adaptative_busy_wait(&wait);
6362f68f
MD
224 goto gp_end;
225 }
bf6822a6
MD
226 /* We won't need to wake ourself up */
227 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
6362f68f 228
6abb4bd5 229 mutex_lock(&rcu_gp_lock);
47d2f29e 230
6362f68f 231 /*
bf6822a6 232 * Move all waiters into our local queue.
6362f68f 233 */
bf6822a6 234 urcu_move_waiters(&waiters, &gp_waiters);
6362f68f 235
16aa9ee8 236 if (cds_list_empty(&registry))
2dfb8b5e 237 goto out;
47d2f29e
MD
238
239 /*
f6b42f9c 240 * Wait for readers to observe original parity or be quiescent.
47d2f29e 241 */
708d89f0 242 wait_for_readers(&registry, &cur_snap_readers, &qsreaders);
47d2f29e
MD
243
244 /*
f6b42f9c 245 * Must finish waiting for quiescent state for original parity
15302e28 246 * before committing next rcu_gp.ctr update to memory. Failure
f6b42f9c 247 * to do so could result in the writer waiting forever while new
5e77fc1f 248 * readers are always accessing data (no progress). Enforce
f6b42f9c 249 * compiler-order of load URCU_TLS(rcu_reader).ctr before store
15302e28 250 * to rcu_gp.ctr.
47d2f29e 251 */
5481ddb3 252 cmm_barrier();
47d2f29e 253
47d2f29e 254 /*
5481ddb3 255 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
2dfb8b5e
MD
256 * model easier to understand. It does not have a big performance impact
257 * anyway, given this is the write-side.
47d2f29e 258 */
5481ddb3 259 cmm_smp_mb();
47d2f29e 260
f6b42f9c 261 /* Switch parity: 0 -> 1, 1 -> 0 */
15302e28 262 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr ^ RCU_GP_CTR);
f6b42f9c 263
47d2f29e 264 /*
15302e28 265 * Must commit rcu_gp.ctr update to memory before waiting for
f6b42f9c
MD
266 * quiescent state. Failure to do so could result in the writer
267 * waiting forever while new readers are always accessing data
15302e28 268 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
f6b42f9c 269 * before load URCU_TLS(rcu_reader).ctr.
47d2f29e 270 */
f6b42f9c
MD
271 cmm_barrier();
272
273 /*
274 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
275 * model easier to understand. It does not have a big performance impact
276 * anyway, given this is the write-side.
277 */
278 cmm_smp_mb();
279
280 /*
281 * Wait for readers to observe new parity or be quiescent.
282 */
708d89f0
MD
283 wait_for_readers(&cur_snap_readers, NULL, &qsreaders);
284
285 /*
286 * Put quiescent reader list back into registry.
287 */
288 cds_list_splice(&qsreaders, &registry);
2dfb8b5e 289out:
6abb4bd5 290 mutex_unlock(&rcu_gp_lock);
bf6822a6 291 urcu_wake_all_waiters(&waiters);
6362f68f 292gp_end:
bc49c323
MD
293 /*
294 * Finish waiting for reader threads before letting the old ptr being
47d2f29e
MD
295 * freed.
296 */
bc49c323 297 if (was_online)
27b940e7
PB
298 rcu_thread_online();
299 else
300 cmm_smp_mb();
47d2f29e 301}
b39e1761 302#else /* !(CAA_BITS_PER_LONG < 64) */
9f1621ca
MD
303void synchronize_rcu(void)
304{
708d89f0 305 CDS_LIST_HEAD(qsreaders);
f0f7dbdd 306 unsigned long was_online;
bf6822a6
MD
307 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
308 struct urcu_waiters waiters;
ff2f67a0 309
882f3357 310 was_online = rcu_read_ongoing();
ff2f67a0
MD
311
312 /*
313 * Mark the writer thread offline to make sure we don't wait for
5e77fc1f
PM
314 * our own quiescent state. This allows using synchronize_rcu()
315 * in threads registered as readers.
ff2f67a0 316 */
27b940e7
PB
317 if (was_online)
318 rcu_thread_offline();
319 else
320 cmm_smp_mb();
ff2f67a0 321
6362f68f 322 /*
bf6822a6 323 * Add ourself to gp_waiters queue of threads awaiting to wait
6362f68f 324 * for a grace period. Proceed to perform the grace period only
bf6822a6 325 * if we are the first thread added into the queue.
6362f68f 326 */
bf6822a6
MD
327 if (urcu_wait_add(&gp_waiters, &wait) != 0) {
328 /* Not first in queue: will be awakened by another thread. */
329 urcu_adaptative_busy_wait(&wait);
6362f68f
MD
330 goto gp_end;
331 }
bf6822a6
MD
332 /* We won't need to wake ourself up */
333 urcu_wait_set_state(&wait, URCU_WAIT_RUNNING);
6362f68f 334
6abb4bd5 335 mutex_lock(&rcu_gp_lock);
6362f68f
MD
336
337 /*
bf6822a6 338 * Move all waiters into our local queue.
6362f68f 339 */
bf6822a6 340 urcu_move_waiters(&waiters, &gp_waiters);
6362f68f 341
16aa9ee8 342 if (cds_list_empty(&registry))
2dfb8b5e 343 goto out;
f6b42f9c
MD
344
345 /* Increment current G.P. */
15302e28 346 CMM_STORE_SHARED(rcu_gp.ctr, rcu_gp.ctr + RCU_GP_CTR);
f6b42f9c
MD
347
348 /*
15302e28 349 * Must commit rcu_gp.ctr update to memory before waiting for
f6b42f9c
MD
350 * quiescent state. Failure to do so could result in the writer
351 * waiting forever while new readers are always accessing data
15302e28 352 * (no progress). Enforce compiler-order of store to rcu_gp.ctr
f6b42f9c
MD
353 * before load URCU_TLS(rcu_reader).ctr.
354 */
355 cmm_barrier();
356
357 /*
358 * Adding a cmm_smp_mb() which is _not_ formally required, but makes the
359 * model easier to understand. It does not have a big performance impact
360 * anyway, given this is the write-side.
361 */
362 cmm_smp_mb();
363
364 /*
365 * Wait for readers to observe new count of be quiescent.
366 */
708d89f0
MD
367 wait_for_readers(&registry, NULL, &qsreaders);
368
369 /*
370 * Put quiescent reader list back into registry.
371 */
372 cds_list_splice(&qsreaders, &registry);
2dfb8b5e 373out:
6abb4bd5 374 mutex_unlock(&rcu_gp_lock);
bf6822a6 375 urcu_wake_all_waiters(&waiters);
6362f68f 376gp_end:
ff2f67a0 377 if (was_online)
27b940e7
PB
378 rcu_thread_online();
379 else
380 cmm_smp_mb();
9f1621ca 381}
b39e1761 382#endif /* !(CAA_BITS_PER_LONG < 64) */
9f1621ca
MD
383
384/*
385 * library wrappers to be used by non-LGPL compatible source code.
386 */
387
388void rcu_read_lock(void)
389{
390 _rcu_read_lock();
391}
392
393void rcu_read_unlock(void)
394{
395 _rcu_read_unlock();
396}
397
882f3357
MD
398int rcu_read_ongoing(void)
399{
400 return _rcu_read_ongoing();
401}
402
7ac06cef
MD
403void rcu_quiescent_state(void)
404{
405 _rcu_quiescent_state();
406}
407
408void rcu_thread_offline(void)
409{
410 _rcu_thread_offline();
411}
412
413void rcu_thread_online(void)
414{
415 _rcu_thread_online();
416}
417
9f1621ca
MD
418void rcu_register_thread(void)
419{
bd252a04
MD
420 URCU_TLS(rcu_reader).tid = pthread_self();
421 assert(URCU_TLS(rcu_reader).ctr == 0);
4f8e3380 422
6abb4bd5 423 mutex_lock(&rcu_gp_lock);
bd252a04 424 cds_list_add(&URCU_TLS(rcu_reader).node, &registry);
6abb4bd5 425 mutex_unlock(&rcu_gp_lock);
5f373c84 426 _rcu_thread_online();
9f1621ca
MD
427}
428
429void rcu_unregister_thread(void)
430{
76f3022f
MD
431 /*
432 * We have to make the thread offline otherwise we end up dealocking
433 * with a waiting writer.
434 */
435 _rcu_thread_offline();
6abb4bd5 436 mutex_lock(&rcu_gp_lock);
bd252a04 437 cds_list_del(&URCU_TLS(rcu_reader).node);
6abb4bd5 438 mutex_unlock(&rcu_gp_lock);
9f1621ca 439}
f6d18c64
MD
440
441void rcu_exit(void)
442{
01cadde4
MD
443 /*
444 * Assertion disabled because call_rcu threads are now rcu
445 * readers, and left running at exit.
446 * assert(cds_list_empty(&registry));
447 */
f6d18c64 448}
5e77fc1f 449
5e6b23a6 450DEFINE_RCU_FLAVOR(rcu_flavor);
541d828d 451
5e77fc1f 452#include "urcu-call-rcu-impl.h"
0376e7b2 453#include "urcu-defer-impl.h"
This page took 0.054723 seconds and 4 git commands to generate.