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