Commit | Line | Data |
---|---|---|
b257a10b MD |
1 | /* |
2 | * urcu.c | |
3 | * | |
4 | * Userspace RCU library | |
5 | * | |
af02d47e MD |
6 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
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 |
27b012e2 MD |
27 | #include <stdio.h> |
28 | #include <pthread.h> | |
29 | #include <signal.h> | |
30 | #include <assert.h> | |
f69f195a MD |
31 | #include <stdlib.h> |
32 | #include <string.h> | |
09a9f986 | 33 | #include <errno.h> |
e8043c1b | 34 | #include <poll.h> |
27b012e2 | 35 | |
121a5d44 MD |
36 | #include "urcu-static.h" |
37 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ | |
27b012e2 MD |
38 | #include "urcu.h" |
39 | ||
fdf01eed | 40 | #ifdef RCU_MEMBARRIER |
834a45ba | 41 | static int init_done; |
fdf01eed | 42 | int has_sys_membarrier; |
834a45ba | 43 | |
02be5561 | 44 | void __attribute__((constructor)) rcu_init(void); |
fdf01eed MD |
45 | #endif |
46 | ||
47 | #ifdef RCU_MB | |
02be5561 | 48 | void rcu_init(void) |
e90a6e9c MD |
49 | { |
50 | } | |
51 | #endif | |
8a5fb4c9 | 52 | |
fdf01eed MD |
53 | #ifdef RCU_SIGNAL |
54 | static int init_done; | |
55 | ||
56 | void __attribute__((constructor)) rcu_init(void); | |
57 | void __attribute__((destructor)) rcu_exit(void); | |
58 | #endif | |
59 | ||
02be5561 | 60 | static pthread_mutex_t rcu_mutex = PTHREAD_MUTEX_INITIALIZER; |
27b012e2 | 61 | |
bc6c15bb MD |
62 | int gp_futex; |
63 | ||
128166c9 MD |
64 | /* |
65 | * Global grace period counter. | |
02be5561 | 66 | * Contains the current RCU_GP_CTR_PHASE. |
afb8f2c9 | 67 | * Also has a RCU_GP_COUNT of 1, to accelerate the reader fast path. |
b0d5e790 | 68 | * Written to only by writer with mutex taken. Read by both writer and readers. |
128166c9 | 69 | */ |
02be5561 | 70 | long rcu_gp_ctr = RCU_GP_COUNT; |
27b012e2 | 71 | |
b0d5e790 MD |
72 | /* |
73 | * Written to only by each individual reader. Read by both the reader and the | |
74 | * writers. | |
75 | */ | |
02be5561 | 76 | struct rcu_reader __thread rcu_reader; |
27b012e2 | 77 | |
cf380c2f | 78 | #ifdef DEBUG_YIELD |
9d335088 MD |
79 | unsigned int yield_active; |
80 | unsigned int __thread rand_yield; | |
cf380c2f MD |
81 | #endif |
82 | ||
e3b0cef0 | 83 | static LIST_HEAD(registry); |
27b012e2 | 84 | |
02be5561 | 85 | static void internal_rcu_lock(void) |
41718ff9 MD |
86 | { |
87 | int ret; | |
09a9f986 PM |
88 | |
89 | #ifndef DISTRUST_SIGNALS_EXTREME | |
02be5561 | 90 | ret = pthread_mutex_lock(&rcu_mutex); |
41718ff9 MD |
91 | if (ret) { |
92 | perror("Error in pthread mutex lock"); | |
93 | exit(-1); | |
94 | } | |
09a9f986 | 95 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ |
02be5561 | 96 | while ((ret = pthread_mutex_trylock(&rcu_mutex)) != 0) { |
09a9f986 PM |
97 | if (ret != EBUSY && ret != EINTR) { |
98 | printf("ret = %d, errno = %d\n", ret, errno); | |
99 | perror("Error in pthread mutex lock"); | |
100 | exit(-1); | |
101 | } | |
02be5561 | 102 | if (rcu_reader.need_mb) { |
09a9f986 | 103 | smp_mb(); |
02be5561 | 104 | rcu_reader.need_mb = 0; |
09a9f986 PM |
105 | smp_mb(); |
106 | } | |
107 | poll(NULL,0,10); | |
108 | } | |
109 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ | |
41718ff9 MD |
110 | } |
111 | ||
02be5561 | 112 | static void internal_rcu_unlock(void) |
41718ff9 MD |
113 | { |
114 | int ret; | |
115 | ||
02be5561 | 116 | ret = pthread_mutex_unlock(&rcu_mutex); |
41718ff9 MD |
117 | if (ret) { |
118 | perror("Error in pthread mutex unlock"); | |
119 | exit(-1); | |
120 | } | |
121 | } | |
122 | ||
27b012e2 | 123 | /* |
02be5561 | 124 | * called with rcu_mutex held. |
27b012e2 | 125 | */ |
02be5561 | 126 | static void switch_next_rcu_qparity(void) |
27b012e2 | 127 | { |
02be5561 | 128 | STORE_SHARED(rcu_gp_ctr, rcu_gp_ctr ^ RCU_GP_CTR_PHASE); |
27b012e2 MD |
129 | } |
130 | ||
fdf01eed MD |
131 | #ifdef RCU_MEMBARRIER |
132 | static void smp_mb_heavy(void) | |
133 | { | |
134 | if (likely(has_sys_membarrier)) | |
f0708810 | 135 | membarrier(MEMBARRIER_EXPEDITED); |
fdf01eed MD |
136 | else |
137 | smp_mb(); | |
138 | } | |
139 | #endif | |
140 | ||
02be5561 | 141 | #ifdef RCU_MB |
fdf01eed | 142 | static void smp_mb_heavy(void) |
40e140c9 MD |
143 | { |
144 | smp_mb(); | |
145 | } | |
fdf01eed MD |
146 | #endif |
147 | ||
148 | #ifdef RCU_SIGNAL | |
78ff9419 | 149 | static void force_mb_all_readers(void) |
27b012e2 | 150 | { |
02be5561 | 151 | struct rcu_reader *index; |
e3b0cef0 | 152 | |
27b012e2 | 153 | /* |
b715b99e | 154 | * Ask for each threads to execute a smp_mb() so we can consider the |
27b012e2 MD |
155 | * compiler barriers around rcu read lock as real memory barriers. |
156 | */ | |
e3b0cef0 | 157 | if (list_empty(®istry)) |
27b012e2 | 158 | return; |
3a86deba MD |
159 | /* |
160 | * pthread_kill has a smp_mb(). But beware, we assume it performs | |
157dca95 MD |
161 | * a cache flush on architectures with non-coherent cache. Let's play |
162 | * safe and don't assume anything : we use smp_mc() to make sure the | |
163 | * cache flush is enforced. | |
3a86deba | 164 | */ |
e3b0cef0 MD |
165 | list_for_each_entry(index, ®istry, head) { |
166 | index->need_mb = 1; | |
09a9f986 | 167 | smp_mc(); /* write need_mb before sending the signal */ |
02be5561 | 168 | pthread_kill(index->tid, SIGRCU); |
09a9f986 | 169 | } |
27b012e2 MD |
170 | /* |
171 | * Wait for sighandler (and thus mb()) to execute on every thread. | |
09a9f986 PM |
172 | * |
173 | * Note that the pthread_kill() will never be executed on systems | |
174 | * that correctly deliver signals in a timely manner. However, it | |
175 | * is not uncommon for kernels to have bugs that can result in | |
176 | * lost or unduly delayed signals. | |
177 | * | |
178 | * If you are seeing the below pthread_kill() executing much at | |
179 | * all, we suggest testing the underlying kernel and filing the | |
180 | * relevant bug report. For Linux kernels, we recommend getting | |
181 | * the Linux Test Project (LTP). | |
27b012e2 | 182 | */ |
e3b0cef0 MD |
183 | list_for_each_entry(index, ®istry, head) { |
184 | while (index->need_mb) { | |
02be5561 | 185 | pthread_kill(index->tid, SIGRCU); |
09a9f986 PM |
186 | poll(NULL, 0, 1); |
187 | } | |
188 | } | |
189 | smp_mb(); /* read ->need_mb before ending the barrier */ | |
27b012e2 | 190 | } |
9d7e3f89 | 191 | |
fdf01eed | 192 | static void smp_mb_heavy(void) |
9d7e3f89 MD |
193 | { |
194 | force_mb_all_readers(); | |
195 | } | |
fdf01eed | 196 | #endif /* #ifdef RCU_SIGNAL */ |
27b012e2 | 197 | |
bc6c15bb MD |
198 | /* |
199 | * synchronize_rcu() waiting. Single thread. | |
200 | */ | |
cfe78e25 | 201 | static void wait_gp(void) |
bc6c15bb | 202 | { |
cfe78e25 | 203 | /* Read reader_gp before read futex */ |
9d7e3f89 | 204 | smp_mb_heavy(); |
cfe78e25 | 205 | if (uatomic_read(&gp_futex) == -1) |
0854ccff | 206 | futex_async(&gp_futex, FUTEX_WAIT, -1, |
cfe78e25 | 207 | NULL, NULL, 0); |
bc6c15bb MD |
208 | } |
209 | ||
1430ee0b | 210 | void wait_for_quiescent_state(void) |
27b012e2 | 211 | { |
cfe78e25 MD |
212 | LIST_HEAD(qsreaders); |
213 | int wait_loops = 0; | |
02be5561 | 214 | struct rcu_reader *index, *tmp; |
27b012e2 | 215 | |
e3b0cef0 | 216 | if (list_empty(®istry)) |
27b012e2 | 217 | return; |
40e140c9 | 218 | /* |
02be5561 | 219 | * Wait for each thread rcu_reader.ctr count to become 0. |
27b012e2 | 220 | */ |
cfe78e25 MD |
221 | for (;;) { |
222 | wait_loops++; | |
223 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) { | |
224 | uatomic_dec(&gp_futex); | |
225 | /* Write futex before read reader_gp */ | |
9d7e3f89 | 226 | smp_mb_heavy(); |
cfe78e25 MD |
227 | } |
228 | ||
23758cc9 | 229 | list_for_each_entry_safe(index, tmp, ®istry, head) { |
cfe78e25 MD |
230 | if (!rcu_old_gp_ongoing(&index->ctr)) |
231 | list_move(&index->head, &qsreaders); | |
232 | } | |
233 | ||
e8043c1b | 234 | #ifndef HAS_INCOHERENT_CACHES |
cfe78e25 MD |
235 | if (list_empty(®istry)) { |
236 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) { | |
237 | /* Read reader_gp before write futex */ | |
9d7e3f89 | 238 | smp_mb_heavy(); |
cfe78e25 | 239 | uatomic_set(&gp_futex, 0); |
bc6c15bb | 240 | } |
cfe78e25 MD |
241 | break; |
242 | } else { | |
243 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) | |
244 | wait_gp(); | |
245 | else | |
246 | cpu_relax(); | |
bc6c15bb | 247 | } |
e8043c1b | 248 | #else /* #ifndef HAS_INCOHERENT_CACHES */ |
27b012e2 | 249 | /* |
40e140c9 | 250 | * BUSY-LOOP. Force the reader thread to commit its |
02be5561 | 251 | * rcu_reader.ctr update to memory if we wait for too long. |
27b012e2 | 252 | */ |
cfe78e25 MD |
253 | if (list_empty(®istry)) { |
254 | if (wait_loops == RCU_QS_ACTIVE_ATTEMPTS) { | |
255 | /* Read reader_gp before write futex */ | |
9d7e3f89 | 256 | smp_mb_heavy(); |
cfe78e25 MD |
257 | uatomic_set(&gp_futex, 0); |
258 | } | |
259 | break; | |
260 | } else { | |
261 | switch (wait_loops) { | |
bc6c15bb | 262 | case RCU_QS_ACTIVE_ATTEMPTS: |
cfe78e25 MD |
263 | wait_gp(); |
264 | break; /* only escape switch */ | |
bc6c15bb | 265 | case KICK_READER_LOOPS: |
9d7e3f89 | 266 | smp_mb_heavy(); |
40e140c9 | 267 | wait_loops = 0; |
cfe78e25 | 268 | break; /* only escape switch */ |
bc6c15bb | 269 | default: |
3b55dbf4 | 270 | cpu_relax(); |
40e140c9 MD |
271 | } |
272 | } | |
e8043c1b | 273 | #endif /* #else #ifndef HAS_INCOHERENT_CACHES */ |
27b012e2 | 274 | } |
cfe78e25 | 275 | /* put back the reader list in the registry */ |
23758cc9 | 276 | list_splice(&qsreaders, ®istry); |
27b012e2 MD |
277 | } |
278 | ||
9598a481 | 279 | void synchronize_rcu(void) |
2bc59bd7 | 280 | { |
02be5561 | 281 | internal_rcu_lock(); |
135530fd | 282 | |
9598a481 | 283 | /* All threads should read qparity before accessing data structure |
02be5561 | 284 | * where new ptr points to. Must be done within internal_rcu_lock |
135530fd | 285 | * because it iterates on reader threads.*/ |
9598a481 | 286 | /* Write new ptr before changing the qparity */ |
9d7e3f89 | 287 | smp_mb_heavy(); |
9598a481 | 288 | |
02be5561 | 289 | switch_next_rcu_qparity(); /* 0 -> 1 */ |
2bc59bd7 PM |
290 | |
291 | /* | |
9598a481 MD |
292 | * Must commit qparity update to memory before waiting for parity |
293 | * 0 quiescent state. Failure to do so could result in the writer | |
294 | * waiting forever while new readers are always accessing data (no | |
295 | * progress). | |
b0d5e790 | 296 | * Ensured by STORE_SHARED and LOAD_SHARED. |
2bc59bd7 | 297 | */ |
2bc59bd7 | 298 | |
67c2d80b | 299 | /* |
5dba80f9 MD |
300 | * Adding a smp_mb() which is _not_ formally required, but makes the |
301 | * model easier to understand. It does not have a big performance impact | |
302 | * anyway, given this is the write-side. | |
67c2d80b | 303 | */ |
5dba80f9 | 304 | smp_mb(); |
67c2d80b | 305 | |
9598a481 MD |
306 | /* |
307 | * Wait for previous parity to be empty of readers. | |
308 | */ | |
309 | wait_for_quiescent_state(); /* Wait readers in parity 0 */ | |
9598a481 MD |
310 | |
311 | /* | |
312 | * Must finish waiting for quiescent state for parity 0 before | |
313 | * committing qparity update to memory. Failure to do so could result in | |
314 | * the writer waiting forever while new readers are always accessing | |
315 | * data (no progress). | |
b0d5e790 | 316 | * Ensured by STORE_SHARED and LOAD_SHARED. |
9598a481 | 317 | */ |
9598a481 | 318 | |
5dba80f9 MD |
319 | /* |
320 | * Adding a smp_mb() which is _not_ formally required, but makes the | |
321 | * model easier to understand. It does not have a big performance impact | |
322 | * anyway, given this is the write-side. | |
323 | */ | |
324 | smp_mb(); | |
67c2d80b | 325 | |
02be5561 | 326 | switch_next_rcu_qparity(); /* 1 -> 0 */ |
9598a481 MD |
327 | |
328 | /* | |
329 | * Must commit qparity update to memory before waiting for parity | |
330 | * 1 quiescent state. Failure to do so could result in the writer | |
331 | * waiting forever while new readers are always accessing data (no | |
332 | * progress). | |
b0d5e790 | 333 | * Ensured by STORE_SHARED and LOAD_SHARED. |
9598a481 | 334 | */ |
9598a481 | 335 | |
5dba80f9 MD |
336 | /* |
337 | * Adding a smp_mb() which is _not_ formally required, but makes the | |
338 | * model easier to understand. It does not have a big performance impact | |
339 | * anyway, given this is the write-side. | |
340 | */ | |
341 | smp_mb(); | |
67c2d80b | 342 | |
9598a481 MD |
343 | /* |
344 | * Wait for previous parity to be empty of readers. | |
345 | */ | |
346 | wait_for_quiescent_state(); /* Wait readers in parity 1 */ | |
9598a481 | 347 | |
9598a481 | 348 | /* Finish waiting for reader threads before letting the old ptr being |
02be5561 | 349 | * freed. Must be done within internal_rcu_lock because it iterates on |
135530fd | 350 | * reader threads. */ |
9d7e3f89 | 351 | smp_mb_heavy(); |
135530fd | 352 | |
02be5561 | 353 | internal_rcu_unlock(); |
2bc59bd7 PM |
354 | } |
355 | ||
121a5d44 MD |
356 | /* |
357 | * library wrappers to be used by non-LGPL compatible source code. | |
358 | */ | |
359 | ||
360 | void rcu_read_lock(void) | |
361 | { | |
362 | _rcu_read_lock(); | |
363 | } | |
364 | ||
365 | void rcu_read_unlock(void) | |
366 | { | |
367 | _rcu_read_unlock(); | |
368 | } | |
369 | ||
121a5d44 | 370 | void rcu_register_thread(void) |
27b012e2 | 371 | { |
02be5561 MD |
372 | rcu_reader.tid = pthread_self(); |
373 | assert(rcu_reader.need_mb == 0); | |
374 | assert(rcu_reader.ctr == 0); | |
375 | ||
376 | internal_rcu_lock(); | |
377 | rcu_init(); /* In case gcc does not support constructor attribute */ | |
378 | list_add(&rcu_reader.head, ®istry); | |
379 | internal_rcu_unlock(); | |
27b012e2 MD |
380 | } |
381 | ||
121a5d44 | 382 | void rcu_unregister_thread(void) |
27b012e2 | 383 | { |
02be5561 MD |
384 | internal_rcu_lock(); |
385 | list_del(&rcu_reader.head); | |
386 | internal_rcu_unlock(); | |
27b012e2 MD |
387 | } |
388 | ||
fdf01eed MD |
389 | #ifdef RCU_MEMBARRIER |
390 | void rcu_init(void) | |
391 | { | |
392 | if (init_done) | |
393 | return; | |
394 | init_done = 1; | |
cf5271ee | 395 | if (!membarrier(MEMBARRIER_EXPEDITED | MEMBARRIER_QUERY)) |
fdf01eed MD |
396 | has_sys_membarrier = 1; |
397 | } | |
398 | #endif | |
399 | ||
400 | #ifdef RCU_SIGNAL | |
02be5561 | 401 | static void sigrcu_handler(int signo, siginfo_t *siginfo, void *context) |
27b012e2 | 402 | { |
40e140c9 MD |
403 | /* |
404 | * Executing this smp_mb() is the only purpose of this signal handler. | |
405 | * It punctually promotes barrier() into smp_mb() on every thread it is | |
406 | * executed on. | |
407 | */ | |
b715b99e | 408 | smp_mb(); |
02be5561 | 409 | rcu_reader.need_mb = 0; |
09a9f986 | 410 | smp_mb(); |
27b012e2 MD |
411 | } |
412 | ||
8a5fb4c9 | 413 | /* |
02be5561 | 414 | * rcu_init constructor. Called when the library is linked, but also when |
8a5fb4c9 MD |
415 | * reader threads are calling rcu_register_thread(). |
416 | * Should only be called by a single thread at a given time. This is ensured by | |
02be5561 | 417 | * holing the internal_rcu_lock() from rcu_register_thread() or by running at |
8a5fb4c9 MD |
418 | * library load time, which should not be executed by multiple threads nor |
419 | * concurrently with rcu_register_thread() anyway. | |
420 | */ | |
02be5561 | 421 | void rcu_init(void) |
27b012e2 MD |
422 | { |
423 | struct sigaction act; | |
424 | int ret; | |
425 | ||
8a5fb4c9 MD |
426 | if (init_done) |
427 | return; | |
428 | init_done = 1; | |
429 | ||
02be5561 | 430 | act.sa_sigaction = sigrcu_handler; |
dd052bd3 | 431 | act.sa_flags = SA_SIGINFO | SA_RESTART; |
c297c21c | 432 | sigemptyset(&act.sa_mask); |
02be5561 | 433 | ret = sigaction(SIGRCU, &act, NULL); |
f69f195a MD |
434 | if (ret) { |
435 | perror("Error in sigaction"); | |
27b012e2 MD |
436 | exit(-1); |
437 | } | |
438 | } | |
439 | ||
02be5561 | 440 | void rcu_exit(void) |
27b012e2 MD |
441 | { |
442 | struct sigaction act; | |
443 | int ret; | |
444 | ||
02be5561 | 445 | ret = sigaction(SIGRCU, NULL, &act); |
f69f195a MD |
446 | if (ret) { |
447 | perror("Error in sigaction"); | |
27b012e2 MD |
448 | exit(-1); |
449 | } | |
02be5561 | 450 | assert(act.sa_sigaction == sigrcu_handler); |
e3b0cef0 | 451 | assert(list_empty(®istry)); |
27b012e2 | 452 | } |
fdf01eed | 453 | #endif /* #ifdef RCU_SIGNAL */ |