Commit | Line | Data |
---|---|---|
b57aee66 PM |
1 | /* |
2 | * urcu-call-rcu.c | |
3 | * | |
4 | * Userspace RCU library - batch memory reclamation with kernel API | |
5 | * | |
6 | * Copyright (c) 2010 Paul E. McKenney <paulmck@linux.vnet.ibm.com> | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; either | |
11 | * version 2.1 of the License, or (at your option) any later version. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
5161f31e | 23 | #define _LGPL_SOURCE |
b57aee66 PM |
24 | #include <stdio.h> |
25 | #include <pthread.h> | |
26 | #include <signal.h> | |
27 | #include <assert.h> | |
28 | #include <stdlib.h> | |
6d841bc2 | 29 | #include <stdint.h> |
b57aee66 PM |
30 | #include <string.h> |
31 | #include <errno.h> | |
32 | #include <poll.h> | |
33 | #include <sys/time.h> | |
b57aee66 | 34 | #include <unistd.h> |
c1d2c60b | 35 | #include <sched.h> |
b57aee66 | 36 | |
a47dd11c | 37 | #include "compat-getcpu.h" |
4477a870 MD |
38 | #include <urcu/wfcqueue.h> |
39 | #include <urcu/call-rcu.h> | |
40 | #include <urcu/pointer.h> | |
41 | #include <urcu/list.h> | |
42 | #include <urcu/futex.h> | |
43 | #include <urcu/tls-compat.h> | |
44 | #include <urcu/ref.h> | |
4a6d7378 | 45 | #include "urcu-die.h" |
4477a870 | 46 | #include "urcu-utils.h" |
01730852 | 47 | #include "compat-smp.h" |
b57aee66 | 48 | |
8e3690db MD |
49 | #define SET_AFFINITY_CHECK_PERIOD (1U << 8) /* 256 */ |
50 | #define SET_AFFINITY_CHECK_PERIOD_MASK (SET_AFFINITY_CHECK_PERIOD - 1) | |
51 | ||
b57aee66 PM |
52 | /* Data structure that identifies a call_rcu thread. */ |
53 | ||
54 | struct call_rcu_data { | |
5161f31e | 55 | /* |
0b8ab7df MD |
56 | * We do not align head on a different cache-line than tail |
57 | * mainly because call_rcu callback-invocation threads use | |
58 | * batching ("splice") to get an entire list of callbacks, which | |
59 | * effectively empties the queue, and requires to touch the tail | |
60 | * anyway. | |
5161f31e | 61 | */ |
b9f893b6 | 62 | struct cds_wfcq_tail cbs_tail; |
0b8ab7df | 63 | struct cds_wfcq_head cbs_head; |
b57aee66 | 64 | unsigned long flags; |
6d841bc2 | 65 | int32_t futex; |
73987721 | 66 | unsigned long qlen; /* maintained for debugging. */ |
b57aee66 | 67 | pthread_t tid; |
c1d2c60b | 68 | int cpu_affinity; |
8e3690db | 69 | unsigned long gp_count; |
3c24913f | 70 | struct cds_list_head list; |
b57aee66 PM |
71 | } __attribute__((aligned(CAA_CACHE_LINE_SIZE))); |
72 | ||
b7f721d9 MD |
73 | struct call_rcu_completion { |
74 | int barrier_count; | |
75 | int32_t futex; | |
81dd9134 | 76 | struct urcu_ref ref; |
b7f721d9 MD |
77 | }; |
78 | ||
79 | struct call_rcu_completion_work { | |
80 | struct rcu_head head; | |
81 | struct call_rcu_completion *completion; | |
82 | }; | |
83 | ||
232cf6ac MD |
84 | enum crdf_flags { |
85 | CRDF_FLAG_JOIN_THREAD = (1 << 0), | |
86 | }; | |
87 | ||
3c24913f PM |
88 | /* |
89 | * List of all call_rcu_data structures to keep valgrind happy. | |
90 | * Protected by call_rcu_mutex. | |
91 | */ | |
92 | ||
bab44e28 | 93 | static CDS_LIST_HEAD(call_rcu_data_list); |
3c24913f | 94 | |
b57aee66 PM |
95 | /* Link a thread using call_rcu() to its call_rcu thread. */ |
96 | ||
2f661865 | 97 | static DEFINE_URCU_TLS(struct call_rcu_data *, thread_call_rcu_data); |
b57aee66 | 98 | |
e85451a1 MD |
99 | /* |
100 | * Guard call_rcu thread creation and atfork handlers. | |
101 | */ | |
b57aee66 PM |
102 | static pthread_mutex_t call_rcu_mutex = PTHREAD_MUTEX_INITIALIZER; |
103 | ||
104 | /* If a given thread does not have its own call_rcu thread, this is default. */ | |
105 | ||
106 | static struct call_rcu_data *default_call_rcu_data; | |
107 | ||
d0ec0ed2 | 108 | static struct urcu_atfork *registered_rculfhash_atfork; |
d0ec0ed2 | 109 | |
b57aee66 PM |
110 | /* |
111 | * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are | |
112 | * available, then we can have call_rcu threads assigned to individual | |
113 | * CPUs rather than only to specific threads. | |
114 | */ | |
115 | ||
a47dd11c | 116 | #if defined(HAVE_SYSCONF) && (defined(HAVE_SCHED_GETCPU) || defined(HAVE_GETCPUID)) |
b57aee66 PM |
117 | |
118 | /* | |
119 | * Pointer to array of pointers to per-CPU call_rcu_data structures | |
618b2595 MD |
120 | * and # CPUs. per_cpu_call_rcu_data is a RCU-protected pointer to an |
121 | * array of RCU-protected pointers to call_rcu_data. call_rcu acts as a | |
122 | * RCU read-side and reads per_cpu_call_rcu_data and the per-cpu pointer | |
123 | * without mutex. The call_rcu_mutex protects updates. | |
b57aee66 PM |
124 | */ |
125 | ||
126 | static struct call_rcu_data **per_cpu_call_rcu_data; | |
01730852 | 127 | static long cpus_array_len; |
b57aee66 | 128 | |
01730852 | 129 | static void cpus_array_len_reset(void) |
60af049d | 130 | { |
01730852 | 131 | cpus_array_len = 0; |
60af049d LJ |
132 | } |
133 | ||
b57aee66 PM |
134 | /* Allocate the array if it has not already been allocated. */ |
135 | ||
136 | static void alloc_cpu_call_rcu_data(void) | |
137 | { | |
138 | struct call_rcu_data **p; | |
139 | static int warned = 0; | |
140 | ||
01730852 | 141 | if (cpus_array_len != 0) |
b57aee66 | 142 | return; |
01730852 MJ |
143 | cpus_array_len = get_possible_cpus_array_len(); |
144 | if (cpus_array_len <= 0) { | |
b57aee66 PM |
145 | return; |
146 | } | |
01730852 | 147 | p = malloc(cpus_array_len * sizeof(*per_cpu_call_rcu_data)); |
b57aee66 | 148 | if (p != NULL) { |
01730852 | 149 | memset(p, '\0', cpus_array_len * sizeof(*per_cpu_call_rcu_data)); |
618b2595 | 150 | rcu_set_pointer(&per_cpu_call_rcu_data, p); |
b57aee66 PM |
151 | } else { |
152 | if (!warned) { | |
153 | fprintf(stderr, "[error] liburcu: unable to allocate per-CPU pointer array\n"); | |
154 | } | |
155 | warned = 1; | |
156 | } | |
157 | } | |
158 | ||
63b495d8 | 159 | #else /* #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */ |
b57aee66 | 160 | |
f9437098 MD |
161 | /* |
162 | * per_cpu_call_rcu_data should be constant, but some functions below, used both | |
163 | * for cases where cpu number is available and not available, assume it it not | |
164 | * constant. | |
165 | */ | |
166 | static struct call_rcu_data **per_cpu_call_rcu_data = NULL; | |
01730852 | 167 | static const long cpus_array_len = -1; |
b57aee66 | 168 | |
01730852 | 169 | static void cpus_array_len_reset(void) |
60af049d LJ |
170 | { |
171 | } | |
172 | ||
b57aee66 PM |
173 | static void alloc_cpu_call_rcu_data(void) |
174 | { | |
175 | } | |
176 | ||
63b495d8 | 177 | #endif /* #else #if defined(HAVE_SYSCONF) && defined(HAVE_SCHED_GETCPU) */ |
b57aee66 PM |
178 | |
179 | /* Acquire the specified pthread mutex. */ | |
180 | ||
181 | static void call_rcu_lock(pthread_mutex_t *pmp) | |
182 | { | |
4a6d7378 MD |
183 | int ret; |
184 | ||
185 | ret = pthread_mutex_lock(pmp); | |
186 | if (ret) | |
187 | urcu_die(ret); | |
b57aee66 PM |
188 | } |
189 | ||
190 | /* Release the specified pthread mutex. */ | |
191 | ||
192 | static void call_rcu_unlock(pthread_mutex_t *pmp) | |
193 | { | |
4a6d7378 MD |
194 | int ret; |
195 | ||
196 | ret = pthread_mutex_unlock(pmp); | |
197 | if (ret) | |
198 | urcu_die(ret); | |
b57aee66 PM |
199 | } |
200 | ||
8e3690db MD |
201 | /* |
202 | * Periodically retry setting CPU affinity if we migrate. | |
203 | * Losing affinity can be caused by CPU hotunplug/hotplug, or by | |
204 | * cpuset(7). | |
205 | */ | |
a9e31859 | 206 | #ifdef HAVE_SCHED_SETAFFINITY |
c1d2c60b MD |
207 | static |
208 | int set_thread_cpu_affinity(struct call_rcu_data *crdp) | |
209 | { | |
210 | cpu_set_t mask; | |
8e3690db | 211 | int ret; |
c1d2c60b MD |
212 | |
213 | if (crdp->cpu_affinity < 0) | |
214 | return 0; | |
8e3690db MD |
215 | if (++crdp->gp_count & SET_AFFINITY_CHECK_PERIOD_MASK) |
216 | return 0; | |
217 | if (urcu_sched_getcpu() == crdp->cpu_affinity) | |
218 | return 0; | |
c1d2c60b MD |
219 | |
220 | CPU_ZERO(&mask); | |
221 | CPU_SET(crdp->cpu_affinity, &mask); | |
222 | #if SCHED_SETAFFINITY_ARGS == 2 | |
8e3690db | 223 | ret = sched_setaffinity(0, &mask); |
c1d2c60b | 224 | #else |
8e3690db | 225 | ret = sched_setaffinity(0, sizeof(mask), &mask); |
c1d2c60b | 226 | #endif |
8e3690db MD |
227 | /* |
228 | * EINVAL is fine: can be caused by hotunplugged CPUs, or by | |
229 | * cpuset(7). This is why we should always retry if we detect | |
230 | * migration. | |
231 | */ | |
232 | if (ret && errno == EINVAL) { | |
233 | ret = 0; | |
234 | errno = 0; | |
235 | } | |
236 | return ret; | |
c1d2c60b MD |
237 | } |
238 | #else | |
239 | static | |
0d657320 | 240 | int set_thread_cpu_affinity(struct call_rcu_data *crdp __attribute__((unused))) |
c1d2c60b MD |
241 | { |
242 | return 0; | |
243 | } | |
244 | #endif | |
245 | ||
03fe58b3 MD |
246 | static void call_rcu_wait(struct call_rcu_data *crdp) |
247 | { | |
248 | /* Read call_rcu list before read futex */ | |
249 | cmm_smp_mb(); | |
d60603e8 MD |
250 | while (uatomic_read(&crdp->futex) == -1) { |
251 | if (!futex_async(&crdp->futex, FUTEX_WAIT, -1, NULL, NULL, 0)) { | |
252 | /* | |
253 | * Prior queued wakeups queued by unrelated code | |
254 | * using the same address can cause futex wait to | |
255 | * return 0 even through the futex value is still | |
256 | * -1 (spurious wakeups). Check the value again | |
257 | * in user-space to validate whether it really | |
258 | * differs from -1. | |
259 | */ | |
260 | continue; | |
261 | } | |
b0a841b4 | 262 | switch (errno) { |
d60603e8 | 263 | case EAGAIN: |
b0a841b4 MD |
264 | /* Value already changed. */ |
265 | return; | |
266 | case EINTR: | |
267 | /* Retry if interrupted by signal. */ | |
d60603e8 | 268 | break; /* Get out of switch. Check again. */ |
b0a841b4 MD |
269 | default: |
270 | /* Unexpected error. */ | |
271 | urcu_die(errno); | |
272 | } | |
273 | } | |
03fe58b3 MD |
274 | } |
275 | ||
276 | static void call_rcu_wake_up(struct call_rcu_data *crdp) | |
277 | { | |
278 | /* Write to call_rcu list before reading/writing futex */ | |
279 | cmm_smp_mb(); | |
a0b7f7ea | 280 | if (caa_unlikely(uatomic_read(&crdp->futex) == -1)) { |
03fe58b3 | 281 | uatomic_set(&crdp->futex, 0); |
b0a841b4 MD |
282 | if (futex_async(&crdp->futex, FUTEX_WAKE, 1, |
283 | NULL, NULL, 0) < 0) | |
284 | urcu_die(errno); | |
03fe58b3 MD |
285 | } |
286 | } | |
287 | ||
b7f721d9 MD |
288 | static void call_rcu_completion_wait(struct call_rcu_completion *completion) |
289 | { | |
290 | /* Read completion barrier count before read futex */ | |
291 | cmm_smp_mb(); | |
d60603e8 MD |
292 | while (uatomic_read(&completion->futex) == -1) { |
293 | if (!futex_async(&completion->futex, FUTEX_WAIT, -1, NULL, NULL, 0)) { | |
294 | /* | |
295 | * Prior queued wakeups queued by unrelated code | |
296 | * using the same address can cause futex wait to | |
297 | * return 0 even through the futex value is still | |
298 | * -1 (spurious wakeups). Check the value again | |
299 | * in user-space to validate whether it really | |
300 | * differs from -1. | |
301 | */ | |
302 | continue; | |
303 | } | |
b0a841b4 | 304 | switch (errno) { |
d60603e8 | 305 | case EAGAIN: |
b0a841b4 MD |
306 | /* Value already changed. */ |
307 | return; | |
308 | case EINTR: | |
309 | /* Retry if interrupted by signal. */ | |
d60603e8 | 310 | break; /* Get out of switch. Check again. */ |
b0a841b4 MD |
311 | default: |
312 | /* Unexpected error. */ | |
313 | urcu_die(errno); | |
314 | } | |
315 | } | |
b7f721d9 MD |
316 | } |
317 | ||
318 | static void call_rcu_completion_wake_up(struct call_rcu_completion *completion) | |
319 | { | |
320 | /* Write to completion barrier count before reading/writing futex */ | |
321 | cmm_smp_mb(); | |
322 | if (caa_unlikely(uatomic_read(&completion->futex) == -1)) { | |
323 | uatomic_set(&completion->futex, 0); | |
b0a841b4 MD |
324 | if (futex_async(&completion->futex, FUTEX_WAKE, 1, |
325 | NULL, NULL, 0) < 0) | |
326 | urcu_die(errno); | |
b7f721d9 MD |
327 | } |
328 | } | |
329 | ||
b57aee66 PM |
330 | /* This is the code run by each call_rcu thread. */ |
331 | ||
332 | static void *call_rcu_thread(void *arg) | |
333 | { | |
334 | unsigned long cbcount; | |
5161f31e | 335 | struct call_rcu_data *crdp = (struct call_rcu_data *) arg; |
2870aa1e | 336 | int rt = !!(uatomic_read(&crdp->flags) & URCU_CALL_RCU_RT); |
b57aee66 | 337 | |
8e3690db | 338 | if (set_thread_cpu_affinity(crdp)) |
4a6d7378 | 339 | urcu_die(errno); |
c1d2c60b | 340 | |
765f3ead MD |
341 | /* |
342 | * If callbacks take a read-side lock, we need to be registered. | |
343 | */ | |
344 | rcu_register_thread(); | |
345 | ||
bd252a04 | 346 | URCU_TLS(thread_call_rcu_data) = crdp; |
bc94ca9b MD |
347 | if (!rt) { |
348 | uatomic_dec(&crdp->futex); | |
349 | /* Decrement futex before reading call_rcu list */ | |
350 | cmm_smp_mb(); | |
351 | } | |
b57aee66 | 352 | for (;;) { |
5161f31e MD |
353 | struct cds_wfcq_head cbs_tmp_head; |
354 | struct cds_wfcq_tail cbs_tmp_tail; | |
355 | struct cds_wfcq_node *cbs, *cbs_tmp_n; | |
ae25b7e2 | 356 | enum cds_wfcq_ret splice_ret; |
5161f31e | 357 | |
8e3690db MD |
358 | if (set_thread_cpu_affinity(crdp)) |
359 | urcu_die(errno); | |
360 | ||
e85451a1 MD |
361 | if (uatomic_read(&crdp->flags) & URCU_CALL_RCU_PAUSE) { |
362 | /* | |
363 | * Pause requested. Become quiescent: remove | |
364 | * ourself from all global lists, and don't | |
365 | * process any callback. The callback lists may | |
366 | * still be non-empty though. | |
367 | */ | |
368 | rcu_unregister_thread(); | |
369 | cmm_smp_mb__before_uatomic_or(); | |
370 | uatomic_or(&crdp->flags, URCU_CALL_RCU_PAUSED); | |
371 | while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_PAUSE) != 0) | |
109105b7 | 372 | (void) poll(NULL, 0, 1); |
fc236e5e KF |
373 | uatomic_and(&crdp->flags, ~URCU_CALL_RCU_PAUSED); |
374 | cmm_smp_mb__after_uatomic_and(); | |
e85451a1 MD |
375 | rcu_register_thread(); |
376 | } | |
377 | ||
5161f31e | 378 | cds_wfcq_init(&cbs_tmp_head, &cbs_tmp_tail); |
ae25b7e2 MD |
379 | splice_ret = __cds_wfcq_splice_blocking(&cbs_tmp_head, |
380 | &cbs_tmp_tail, &crdp->cbs_head, &crdp->cbs_tail); | |
381 | assert(splice_ret != CDS_WFCQ_RET_WOULDBLOCK); | |
382 | assert(splice_ret != CDS_WFCQ_RET_DEST_NON_EMPTY); | |
383 | if (splice_ret != CDS_WFCQ_RET_SRC_EMPTY) { | |
b57aee66 PM |
384 | synchronize_rcu(); |
385 | cbcount = 0; | |
5161f31e MD |
386 | __cds_wfcq_for_each_blocking_safe(&cbs_tmp_head, |
387 | &cbs_tmp_tail, cbs, cbs_tmp_n) { | |
388 | struct rcu_head *rhp; | |
389 | ||
390 | rhp = caa_container_of(cbs, | |
391 | struct rcu_head, next); | |
b57aee66 PM |
392 | rhp->func(rhp); |
393 | cbcount++; | |
5161f31e | 394 | } |
b57aee66 PM |
395 | uatomic_sub(&crdp->qlen, cbcount); |
396 | } | |
bc94ca9b MD |
397 | if (uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOP) |
398 | break; | |
765f3ead | 399 | rcu_thread_offline(); |
bc94ca9b | 400 | if (!rt) { |
5161f31e MD |
401 | if (cds_wfcq_empty(&crdp->cbs_head, |
402 | &crdp->cbs_tail)) { | |
bc94ca9b | 403 | call_rcu_wait(crdp); |
109105b7 | 404 | (void) poll(NULL, 0, 10); |
bc94ca9b | 405 | uatomic_dec(&crdp->futex); |
c768e45e | 406 | /* |
bc94ca9b MD |
407 | * Decrement futex before reading |
408 | * call_rcu list. | |
c768e45e MD |
409 | */ |
410 | cmm_smp_mb(); | |
ccbac24d | 411 | } else { |
109105b7 | 412 | (void) poll(NULL, 0, 10); |
c768e45e | 413 | } |
bc94ca9b | 414 | } else { |
109105b7 | 415 | (void) poll(NULL, 0, 10); |
b57aee66 | 416 | } |
765f3ead | 417 | rcu_thread_online(); |
bc94ca9b MD |
418 | } |
419 | if (!rt) { | |
420 | /* | |
421 | * Read call_rcu list before write futex. | |
422 | */ | |
423 | cmm_smp_mb(); | |
424 | uatomic_set(&crdp->futex, 0); | |
b57aee66 | 425 | } |
2870aa1e | 426 | uatomic_or(&crdp->flags, URCU_CALL_RCU_STOPPED); |
765f3ead | 427 | rcu_unregister_thread(); |
7106ddf8 | 428 | return NULL; |
b57aee66 PM |
429 | } |
430 | ||
431 | /* | |
432 | * Create both a call_rcu thread and the corresponding call_rcu_data | |
3c24913f PM |
433 | * structure, linking the structure in as specified. Caller must hold |
434 | * call_rcu_mutex. | |
b57aee66 PM |
435 | */ |
436 | ||
3c24913f | 437 | static void call_rcu_data_init(struct call_rcu_data **crdpp, |
c1d2c60b MD |
438 | unsigned long flags, |
439 | int cpu_affinity) | |
b57aee66 PM |
440 | { |
441 | struct call_rcu_data *crdp; | |
4a6d7378 | 442 | int ret; |
b57aee66 PM |
443 | |
444 | crdp = malloc(sizeof(*crdp)); | |
4a6d7378 MD |
445 | if (crdp == NULL) |
446 | urcu_die(errno); | |
b57aee66 | 447 | memset(crdp, '\0', sizeof(*crdp)); |
5161f31e | 448 | cds_wfcq_init(&crdp->cbs_head, &crdp->cbs_tail); |
b57aee66 | 449 | crdp->qlen = 0; |
263e3cf9 MD |
450 | crdp->futex = 0; |
451 | crdp->flags = flags; | |
3c24913f | 452 | cds_list_add(&crdp->list, &call_rcu_data_list); |
c1d2c60b | 453 | crdp->cpu_affinity = cpu_affinity; |
8e3690db | 454 | crdp->gp_count = 0; |
457eeeee MD |
455 | rcu_set_pointer(crdpp, crdp); |
456 | ||
4a6d7378 MD |
457 | ret = pthread_create(&crdp->tid, NULL, call_rcu_thread, crdp); |
458 | if (ret) | |
459 | urcu_die(ret); | |
b57aee66 PM |
460 | } |
461 | ||
462 | /* | |
463 | * Return a pointer to the call_rcu_data structure for the specified | |
464 | * CPU, returning NULL if there is none. We cannot automatically | |
465 | * created it because the platform we are running on might not define | |
63b495d8 | 466 | * urcu_sched_getcpu(). |
618b2595 MD |
467 | * |
468 | * The call to this function and use of the returned call_rcu_data | |
469 | * should be protected by RCU read-side lock. | |
b57aee66 PM |
470 | */ |
471 | ||
472 | struct call_rcu_data *get_cpu_call_rcu_data(int cpu) | |
473 | { | |
474 | static int warned = 0; | |
618b2595 | 475 | struct call_rcu_data **pcpu_crdp; |
b57aee66 | 476 | |
618b2595 MD |
477 | pcpu_crdp = rcu_dereference(per_cpu_call_rcu_data); |
478 | if (pcpu_crdp == NULL) | |
b57aee66 | 479 | return NULL; |
01730852 | 480 | if (!warned && cpus_array_len > 0 && (cpu < 0 || cpus_array_len <= cpu)) { |
b57aee66 PM |
481 | fprintf(stderr, "[error] liburcu: get CPU # out of range\n"); |
482 | warned = 1; | |
483 | } | |
01730852 | 484 | if (cpu < 0 || cpus_array_len <= cpu) |
b57aee66 | 485 | return NULL; |
618b2595 | 486 | return rcu_dereference(pcpu_crdp[cpu]); |
b57aee66 | 487 | } |
ce28e67a | 488 | URCU_ATTR_ALIAS(urcu_stringify(get_cpu_call_rcu_data)) |
4477a870 | 489 | struct call_rcu_data *alias_get_cpu_call_rcu_data(); |
b57aee66 PM |
490 | |
491 | /* | |
492 | * Return the tid corresponding to the call_rcu thread whose | |
493 | * call_rcu_data structure is specified. | |
494 | */ | |
495 | ||
496 | pthread_t get_call_rcu_thread(struct call_rcu_data *crdp) | |
497 | { | |
498 | return crdp->tid; | |
499 | } | |
ce28e67a | 500 | URCU_ATTR_ALIAS(urcu_stringify(get_call_rcu_thread)) |
4477a870 | 501 | pthread_t alias_get_call_rcu_thread(); |
b57aee66 PM |
502 | |
503 | /* | |
504 | * Create a call_rcu_data structure (with thread) and return a pointer. | |
505 | */ | |
506 | ||
c1d2c60b MD |
507 | static struct call_rcu_data *__create_call_rcu_data(unsigned long flags, |
508 | int cpu_affinity) | |
b57aee66 PM |
509 | { |
510 | struct call_rcu_data *crdp; | |
511 | ||
c1d2c60b | 512 | call_rcu_data_init(&crdp, flags, cpu_affinity); |
b57aee66 PM |
513 | return crdp; |
514 | } | |
515 | ||
ce28e67a | 516 | URCU_ATTR_ALIAS(urcu_stringify(create_call_rcu_data)) |
4477a870 | 517 | struct call_rcu_data *alias_create_call_rcu_data(); |
c1d2c60b MD |
518 | struct call_rcu_data *create_call_rcu_data(unsigned long flags, |
519 | int cpu_affinity) | |
3c24913f PM |
520 | { |
521 | struct call_rcu_data *crdp; | |
522 | ||
523 | call_rcu_lock(&call_rcu_mutex); | |
c1d2c60b | 524 | crdp = __create_call_rcu_data(flags, cpu_affinity); |
3c24913f PM |
525 | call_rcu_unlock(&call_rcu_mutex); |
526 | return crdp; | |
527 | } | |
528 | ||
b57aee66 PM |
529 | /* |
530 | * Set the specified CPU to use the specified call_rcu_data structure. | |
7106ddf8 PM |
531 | * |
532 | * Use NULL to remove a CPU's call_rcu_data structure, but it is | |
533 | * the caller's responsibility to dispose of the removed structure. | |
534 | * Use get_cpu_call_rcu_data() to obtain a pointer to the old structure | |
535 | * (prior to NULLing it out, of course). | |
f9da0936 MD |
536 | * |
537 | * The caller must wait for a grace-period to pass between return from | |
538 | * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the | |
539 | * previous call rcu data as argument. | |
b57aee66 PM |
540 | */ |
541 | ||
542 | int set_cpu_call_rcu_data(int cpu, struct call_rcu_data *crdp) | |
543 | { | |
dcfc8165 | 544 | static int warned = 0; |
b57aee66 PM |
545 | |
546 | call_rcu_lock(&call_rcu_mutex); | |
f3776786 | 547 | alloc_cpu_call_rcu_data(); |
01730852 | 548 | if (cpu < 0 || cpus_array_len <= cpu) { |
b57aee66 PM |
549 | if (!warned) { |
550 | fprintf(stderr, "[error] liburcu: set CPU # out of range\n"); | |
551 | warned = 1; | |
552 | } | |
553 | call_rcu_unlock(&call_rcu_mutex); | |
554 | errno = EINVAL; | |
555 | return -EINVAL; | |
556 | } | |
53a55535 | 557 | |
b57aee66 | 558 | if (per_cpu_call_rcu_data == NULL) { |
3670fef2 | 559 | call_rcu_unlock(&call_rcu_mutex); |
b57aee66 PM |
560 | errno = ENOMEM; |
561 | return -ENOMEM; | |
562 | } | |
53a55535 LJ |
563 | |
564 | if (per_cpu_call_rcu_data[cpu] != NULL && crdp != NULL) { | |
565 | call_rcu_unlock(&call_rcu_mutex); | |
566 | errno = EEXIST; | |
567 | return -EEXIST; | |
568 | } | |
569 | ||
618b2595 | 570 | rcu_set_pointer(&per_cpu_call_rcu_data[cpu], crdp); |
3670fef2 | 571 | call_rcu_unlock(&call_rcu_mutex); |
b57aee66 PM |
572 | return 0; |
573 | } | |
ce28e67a | 574 | URCU_ATTR_ALIAS(urcu_stringify(set_cpu_call_rcu_data)) |
4477a870 | 575 | int alias_set_cpu_call_rcu_data(); |
b57aee66 PM |
576 | |
577 | /* | |
578 | * Return a pointer to the default call_rcu_data structure, creating | |
457eeeee MD |
579 | * one if need be. |
580 | * | |
581 | * The call to this function with intent to use the returned | |
582 | * call_rcu_data should be protected by RCU read-side lock. | |
b57aee66 PM |
583 | */ |
584 | ||
585 | struct call_rcu_data *get_default_call_rcu_data(void) | |
586 | { | |
457eeeee MD |
587 | struct call_rcu_data *crdp; |
588 | ||
589 | crdp = rcu_dereference(default_call_rcu_data); | |
590 | if (crdp != NULL) | |
591 | return crdp; | |
592 | ||
b57aee66 | 593 | call_rcu_lock(&call_rcu_mutex); |
457eeeee MD |
594 | if (default_call_rcu_data == NULL) |
595 | call_rcu_data_init(&default_call_rcu_data, 0, -1); | |
596 | crdp = default_call_rcu_data; | |
b57aee66 | 597 | call_rcu_unlock(&call_rcu_mutex); |
457eeeee MD |
598 | |
599 | return crdp; | |
b57aee66 | 600 | } |
ce28e67a | 601 | URCU_ATTR_ALIAS(urcu_stringify(get_default_call_rcu_data)) |
4477a870 | 602 | struct call_rcu_data *alias_get_default_call_rcu_data(); |
b57aee66 PM |
603 | |
604 | /* | |
605 | * Return the call_rcu_data structure that applies to the currently | |
606 | * running thread. Any call_rcu_data structure assigned specifically | |
607 | * to this thread has first priority, followed by any call_rcu_data | |
608 | * structure assigned to the CPU on which the thread is running, | |
609 | * followed by the default call_rcu_data structure. If there is not | |
610 | * yet a default call_rcu_data structure, one will be created. | |
618b2595 MD |
611 | * |
612 | * Calls to this function and use of the returned call_rcu_data should | |
613 | * be protected by RCU read-side lock. | |
b57aee66 PM |
614 | */ |
615 | struct call_rcu_data *get_call_rcu_data(void) | |
616 | { | |
9744f3bb | 617 | struct call_rcu_data *crd; |
b57aee66 | 618 | |
bd252a04 MD |
619 | if (URCU_TLS(thread_call_rcu_data) != NULL) |
620 | return URCU_TLS(thread_call_rcu_data); | |
9744f3bb | 621 | |
01730852 | 622 | if (cpus_array_len > 0) { |
63b495d8 | 623 | crd = get_cpu_call_rcu_data(urcu_sched_getcpu()); |
9744f3bb LJ |
624 | if (crd) |
625 | return crd; | |
b57aee66 | 626 | } |
9744f3bb | 627 | |
b57aee66 PM |
628 | return get_default_call_rcu_data(); |
629 | } | |
ce28e67a | 630 | URCU_ATTR_ALIAS(urcu_stringify(get_call_rcu_data)) |
4477a870 | 631 | struct call_rcu_data *alias_get_call_rcu_data(); |
b57aee66 PM |
632 | |
633 | /* | |
634 | * Return a pointer to this task's call_rcu_data if there is one. | |
635 | */ | |
636 | ||
637 | struct call_rcu_data *get_thread_call_rcu_data(void) | |
638 | { | |
bd252a04 | 639 | return URCU_TLS(thread_call_rcu_data); |
b57aee66 | 640 | } |
ce28e67a | 641 | URCU_ATTR_ALIAS(urcu_stringify(get_thread_call_rcu_data)) |
4477a870 | 642 | struct call_rcu_data *alias_get_thread_call_rcu_data(); |
b57aee66 PM |
643 | |
644 | /* | |
645 | * Set this task's call_rcu_data structure as specified, regardless | |
646 | * of whether or not this task already had one. (This allows switching | |
647 | * to and from real-time call_rcu threads, for example.) | |
7106ddf8 PM |
648 | * |
649 | * Use NULL to remove a thread's call_rcu_data structure, but it is | |
650 | * the caller's responsibility to dispose of the removed structure. | |
651 | * Use get_thread_call_rcu_data() to obtain a pointer to the old structure | |
652 | * (prior to NULLing it out, of course). | |
b57aee66 PM |
653 | */ |
654 | ||
655 | void set_thread_call_rcu_data(struct call_rcu_data *crdp) | |
656 | { | |
bd252a04 | 657 | URCU_TLS(thread_call_rcu_data) = crdp; |
b57aee66 | 658 | } |
ce28e67a | 659 | URCU_ATTR_ALIAS(urcu_stringify(set_thread_call_rcu_data)) |
4477a870 | 660 | void alias_set_thread_call_rcu_data(); |
b57aee66 PM |
661 | |
662 | /* | |
663 | * Create a separate call_rcu thread for each CPU. This does not | |
664 | * replace a pre-existing call_rcu thread -- use the set_cpu_call_rcu_data() | |
0938c541 MD |
665 | * function if you want that behavior. Should be paired with |
666 | * free_all_cpu_call_rcu_data() to teardown these call_rcu worker | |
667 | * threads. | |
b57aee66 PM |
668 | */ |
669 | ||
670 | int create_all_cpu_call_rcu_data(unsigned long flags) | |
671 | { | |
672 | int i; | |
673 | struct call_rcu_data *crdp; | |
674 | int ret; | |
675 | ||
676 | call_rcu_lock(&call_rcu_mutex); | |
677 | alloc_cpu_call_rcu_data(); | |
678 | call_rcu_unlock(&call_rcu_mutex); | |
01730852 | 679 | if (cpus_array_len <= 0) { |
b57aee66 PM |
680 | errno = EINVAL; |
681 | return -EINVAL; | |
682 | } | |
683 | if (per_cpu_call_rcu_data == NULL) { | |
684 | errno = ENOMEM; | |
685 | return -ENOMEM; | |
686 | } | |
01730852 | 687 | for (i = 0; i < cpus_array_len; i++) { |
b57aee66 PM |
688 | call_rcu_lock(&call_rcu_mutex); |
689 | if (get_cpu_call_rcu_data(i)) { | |
690 | call_rcu_unlock(&call_rcu_mutex); | |
691 | continue; | |
692 | } | |
c1d2c60b | 693 | crdp = __create_call_rcu_data(flags, i); |
b57aee66 PM |
694 | if (crdp == NULL) { |
695 | call_rcu_unlock(&call_rcu_mutex); | |
696 | errno = ENOMEM; | |
697 | return -ENOMEM; | |
698 | } | |
699 | call_rcu_unlock(&call_rcu_mutex); | |
700 | if ((ret = set_cpu_call_rcu_data(i, crdp)) != 0) { | |
356c8794 LJ |
701 | call_rcu_data_free(crdp); |
702 | ||
703 | /* it has been created by other thread */ | |
704 | if (ret == -EEXIST) | |
705 | continue; | |
706 | ||
707 | return ret; | |
b57aee66 PM |
708 | } |
709 | } | |
710 | return 0; | |
711 | } | |
ce28e67a | 712 | URCU_ATTR_ALIAS(urcu_stringify(create_all_cpu_call_rcu_data)) |
4477a870 | 713 | int alias_create_all_cpu_call_rcu_data(); |
b57aee66 | 714 | |
7106ddf8 PM |
715 | /* |
716 | * Wake up the call_rcu thread corresponding to the specified | |
717 | * call_rcu_data structure. | |
718 | */ | |
719 | static void wake_call_rcu_thread(struct call_rcu_data *crdp) | |
720 | { | |
263e3cf9 MD |
721 | if (!(_CMM_LOAD_SHARED(crdp->flags) & URCU_CALL_RCU_RT)) |
722 | call_rcu_wake_up(crdp); | |
7106ddf8 PM |
723 | } |
724 | ||
b7f721d9 MD |
725 | static void _call_rcu(struct rcu_head *head, |
726 | void (*func)(struct rcu_head *head), | |
727 | struct call_rcu_data *crdp) | |
728 | { | |
729 | cds_wfcq_node_init(&head->next); | |
730 | head->func = func; | |
731 | cds_wfcq_enqueue(&crdp->cbs_head, &crdp->cbs_tail, &head->next); | |
732 | uatomic_inc(&crdp->qlen); | |
733 | wake_call_rcu_thread(crdp); | |
734 | } | |
735 | ||
b57aee66 PM |
736 | /* |
737 | * Schedule a function to be invoked after a following grace period. | |
738 | * This is the only function that must be called -- the others are | |
739 | * only present to allow applications to tune their use of RCU for | |
740 | * maximum performance. | |
741 | * | |
742 | * Note that unless a call_rcu thread has not already been created, | |
743 | * the first invocation of call_rcu() will create one. So, if you | |
744 | * need the first invocation of call_rcu() to be fast, make sure | |
745 | * to create a call_rcu thread first. One way to accomplish this is | |
746 | * "get_call_rcu_data();", and another is create_all_cpu_call_rcu_data(). | |
618b2595 MD |
747 | * |
748 | * call_rcu must be called by registered RCU read-side threads. | |
b57aee66 | 749 | */ |
b57aee66 PM |
750 | void call_rcu(struct rcu_head *head, |
751 | void (*func)(struct rcu_head *head)) | |
752 | { | |
753 | struct call_rcu_data *crdp; | |
754 | ||
618b2595 | 755 | /* Holding rcu read-side lock across use of per-cpu crdp */ |
ffb4aaa7 | 756 | _rcu_read_lock(); |
b57aee66 | 757 | crdp = get_call_rcu_data(); |
b7f721d9 | 758 | _call_rcu(head, func, crdp); |
ffb4aaa7 | 759 | _rcu_read_unlock(); |
7106ddf8 | 760 | } |
ce28e67a | 761 | URCU_ATTR_ALIAS(urcu_stringify(call_rcu)) void alias_call_rcu(); |
7106ddf8 PM |
762 | |
763 | /* | |
764 | * Free up the specified call_rcu_data structure, terminating the | |
765 | * associated call_rcu thread. The caller must have previously | |
766 | * removed the call_rcu_data structure from per-thread or per-CPU | |
767 | * usage. For example, set_cpu_call_rcu_data(cpu, NULL) for per-CPU | |
768 | * call_rcu_data structures or set_thread_call_rcu_data(NULL) for | |
769 | * per-thread call_rcu_data structures. | |
770 | * | |
771 | * We silently refuse to free up the default call_rcu_data structure | |
772 | * because that is where we put any leftover callbacks. Note that | |
773 | * the possibility of self-spawning callbacks makes it impossible | |
774 | * to execute all the callbacks in finite time without putting any | |
775 | * newly spawned callbacks somewhere else. The "somewhere else" of | |
776 | * last resort is the default call_rcu_data structure. | |
777 | * | |
778 | * We also silently refuse to free NULL pointers. This simplifies | |
779 | * the calling code. | |
f9da0936 MD |
780 | * |
781 | * The caller must wait for a grace-period to pass between return from | |
782 | * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the | |
783 | * previous call rcu data as argument. | |
03e5118f MD |
784 | * |
785 | * Note: introducing __cds_wfcq_splice_blocking() in this function fixed | |
786 | * a list corruption bug in the 0.7.x series. The equivalent fix | |
787 | * appeared in 0.6.8 for the stable-0.6 branch. | |
7106ddf8 | 788 | */ |
232cf6ac MD |
789 | static |
790 | void _call_rcu_data_free(struct call_rcu_data *crdp, unsigned int flags) | |
7106ddf8 | 791 | { |
7106ddf8 PM |
792 | if (crdp == NULL || crdp == default_call_rcu_data) { |
793 | return; | |
794 | } | |
2870aa1e PB |
795 | if ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0) { |
796 | uatomic_or(&crdp->flags, URCU_CALL_RCU_STOP); | |
7106ddf8 | 797 | wake_call_rcu_thread(crdp); |
2870aa1e | 798 | while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0) |
109105b7 | 799 | (void) poll(NULL, 0, 1); |
7106ddf8 | 800 | } |
9049d56a | 801 | call_rcu_lock(&call_rcu_mutex); |
5161f31e | 802 | if (!cds_wfcq_empty(&crdp->cbs_head, &crdp->cbs_tail)) { |
9049d56a PM |
803 | call_rcu_unlock(&call_rcu_mutex); |
804 | /* Create default call rcu data if need be. */ | |
805 | /* CBs queued here will be handed to the default list. */ | |
698d0778 | 806 | (void) get_default_call_rcu_data(); |
9049d56a | 807 | call_rcu_lock(&call_rcu_mutex); |
5161f31e MD |
808 | __cds_wfcq_splice_blocking(&default_call_rcu_data->cbs_head, |
809 | &default_call_rcu_data->cbs_tail, | |
810 | &crdp->cbs_head, &crdp->cbs_tail); | |
7106ddf8 PM |
811 | uatomic_add(&default_call_rcu_data->qlen, |
812 | uatomic_read(&crdp->qlen)); | |
1e92aa15 | 813 | wake_call_rcu_thread(default_call_rcu_data); |
7106ddf8 | 814 | } |
59dc9e9d LJ |
815 | |
816 | cds_list_del(&crdp->list); | |
b75dffe6 LJ |
817 | call_rcu_unlock(&call_rcu_mutex); |
818 | ||
232cf6ac MD |
819 | if (flags & CRDF_FLAG_JOIN_THREAD) { |
820 | int ret; | |
821 | ||
822 | ret = pthread_join(get_call_rcu_thread(crdp), NULL); | |
823 | if (ret) | |
824 | urcu_die(ret); | |
825 | } | |
59dc9e9d | 826 | free(crdp); |
7106ddf8 | 827 | } |
ce28e67a | 828 | URCU_ATTR_ALIAS(urcu_stringify(call_rcu_data_free)) |
4477a870 | 829 | void alias_call_rcu_data_free(); |
7106ddf8 | 830 | |
232cf6ac MD |
831 | void call_rcu_data_free(struct call_rcu_data *crdp) |
832 | { | |
833 | _call_rcu_data_free(crdp, CRDF_FLAG_JOIN_THREAD); | |
834 | } | |
835 | ||
7106ddf8 PM |
836 | /* |
837 | * Clean up all the per-CPU call_rcu threads. | |
838 | */ | |
839 | void free_all_cpu_call_rcu_data(void) | |
840 | { | |
841 | int cpu; | |
618b2595 MD |
842 | struct call_rcu_data **crdp; |
843 | static int warned = 0; | |
7106ddf8 | 844 | |
01730852 | 845 | if (cpus_array_len <= 0) |
7106ddf8 | 846 | return; |
618b2595 | 847 | |
01730852 | 848 | crdp = malloc(sizeof(*crdp) * cpus_array_len); |
618b2595 MD |
849 | if (!crdp) { |
850 | if (!warned) { | |
851 | fprintf(stderr, "[error] liburcu: unable to allocate per-CPU pointer array\n"); | |
852 | } | |
853 | warned = 1; | |
d31150b4 | 854 | return; |
618b2595 MD |
855 | } |
856 | ||
01730852 | 857 | for (cpu = 0; cpu < cpus_array_len; cpu++) { |
618b2595 MD |
858 | crdp[cpu] = get_cpu_call_rcu_data(cpu); |
859 | if (crdp[cpu] == NULL) | |
7106ddf8 PM |
860 | continue; |
861 | set_cpu_call_rcu_data(cpu, NULL); | |
7106ddf8 | 862 | } |
618b2595 MD |
863 | /* |
864 | * Wait for call_rcu sites acting as RCU readers of the | |
865 | * call_rcu_data to become quiescent. | |
866 | */ | |
867 | synchronize_rcu(); | |
01730852 | 868 | for (cpu = 0; cpu < cpus_array_len; cpu++) { |
618b2595 MD |
869 | if (crdp[cpu] == NULL) |
870 | continue; | |
871 | call_rcu_data_free(crdp[cpu]); | |
872 | } | |
873 | free(crdp); | |
7106ddf8 | 874 | } |
4477a870 MD |
875 | #ifdef RCU_QSBR |
876 | /* ABI6 has a non-namespaced free_all_cpu_call_rcu_data for qsbr */ | |
877 | #undef free_all_cpu_call_rcu_data | |
ce28e67a | 878 | URCU_ATTR_ALIAS("urcu_qsbr_free_all_cpu_call_rcu_data") |
4477a870 MD |
879 | void free_all_cpu_call_rcu_data(); |
880 | #define free_all_cpu_call_rcu_data urcu_qsbr_free_all_cpu_call_rcu_data | |
881 | #else | |
ce28e67a | 882 | URCU_ATTR_ALIAS(urcu_stringify(free_all_cpu_call_rcu_data)) |
4477a870 MD |
883 | void alias_free_all_cpu_call_rcu_data(); |
884 | #endif | |
7106ddf8 | 885 | |
81dd9134 KF |
886 | static |
887 | void free_completion(struct urcu_ref *ref) | |
888 | { | |
889 | struct call_rcu_completion *completion; | |
890 | ||
891 | completion = caa_container_of(ref, struct call_rcu_completion, ref); | |
892 | free(completion); | |
893 | } | |
894 | ||
b7f721d9 MD |
895 | static |
896 | void _rcu_barrier_complete(struct rcu_head *head) | |
897 | { | |
898 | struct call_rcu_completion_work *work; | |
899 | struct call_rcu_completion *completion; | |
900 | ||
901 | work = caa_container_of(head, struct call_rcu_completion_work, head); | |
902 | completion = work->completion; | |
81dd9134 KF |
903 | if (!uatomic_sub_return(&completion->barrier_count, 1)) |
904 | call_rcu_completion_wake_up(completion); | |
905 | urcu_ref_put(&completion->ref, free_completion); | |
b7f721d9 MD |
906 | free(work); |
907 | } | |
908 | ||
909 | /* | |
910 | * Wait for all in-flight call_rcu callbacks to complete execution. | |
911 | */ | |
912 | void rcu_barrier(void) | |
913 | { | |
914 | struct call_rcu_data *crdp; | |
81dd9134 | 915 | struct call_rcu_completion *completion; |
63f0fc6d | 916 | int count = 0; |
b7f721d9 MD |
917 | int was_online; |
918 | ||
919 | /* Put in offline state in QSBR. */ | |
ffb4aaa7 | 920 | was_online = _rcu_read_ongoing(); |
b7f721d9 MD |
921 | if (was_online) |
922 | rcu_thread_offline(); | |
923 | /* | |
924 | * Calling a rcu_barrier() within a RCU read-side critical | |
925 | * section is an error. | |
926 | */ | |
ffb4aaa7 | 927 | if (_rcu_read_ongoing()) { |
b7f721d9 MD |
928 | static int warned = 0; |
929 | ||
930 | if (!warned) { | |
931 | fprintf(stderr, "[error] liburcu: rcu_barrier() called from within RCU read-side critical section.\n"); | |
932 | } | |
933 | warned = 1; | |
934 | goto online; | |
935 | } | |
936 | ||
81dd9134 KF |
937 | completion = calloc(sizeof(*completion), 1); |
938 | if (!completion) | |
939 | urcu_die(errno); | |
940 | ||
b7f721d9 MD |
941 | call_rcu_lock(&call_rcu_mutex); |
942 | cds_list_for_each_entry(crdp, &call_rcu_data_list, list) | |
943 | count++; | |
944 | ||
81dd9134 KF |
945 | /* Referenced by rcu_barrier() and each call_rcu thread. */ |
946 | urcu_ref_set(&completion->ref, count + 1); | |
947 | completion->barrier_count = count; | |
b7f721d9 MD |
948 | |
949 | cds_list_for_each_entry(crdp, &call_rcu_data_list, list) { | |
950 | struct call_rcu_completion_work *work; | |
951 | ||
952 | work = calloc(sizeof(*work), 1); | |
63f0fc6d MD |
953 | if (!work) |
954 | urcu_die(errno); | |
81dd9134 | 955 | work->completion = completion; |
b7f721d9 | 956 | _call_rcu(&work->head, _rcu_barrier_complete, crdp); |
b7f721d9 MD |
957 | } |
958 | call_rcu_unlock(&call_rcu_mutex); | |
959 | ||
b7f721d9 MD |
960 | /* Wait for them */ |
961 | for (;;) { | |
81dd9134 | 962 | uatomic_dec(&completion->futex); |
b7f721d9 MD |
963 | /* Decrement futex before reading barrier_count */ |
964 | cmm_smp_mb(); | |
81dd9134 | 965 | if (!uatomic_read(&completion->barrier_count)) |
b7f721d9 | 966 | break; |
81dd9134 | 967 | call_rcu_completion_wait(completion); |
b7f721d9 | 968 | } |
81dd9134 KF |
969 | |
970 | urcu_ref_put(&completion->ref, free_completion); | |
971 | ||
b7f721d9 MD |
972 | online: |
973 | if (was_online) | |
974 | rcu_thread_online(); | |
975 | } | |
ce28e67a | 976 | URCU_ATTR_ALIAS(urcu_stringify(rcu_barrier)) |
4477a870 | 977 | void alias_rcu_barrier(); |
b7f721d9 | 978 | |
81ad2e19 PM |
979 | /* |
980 | * Acquire the call_rcu_mutex in order to ensure that the child sees | |
e85451a1 MD |
981 | * all of the call_rcu() data structures in a consistent state. Ensure |
982 | * that all call_rcu threads are in a quiescent state across fork. | |
81ad2e19 PM |
983 | * Suitable for pthread_atfork() and friends. |
984 | */ | |
985 | void call_rcu_before_fork(void) | |
986 | { | |
e85451a1 | 987 | struct call_rcu_data *crdp; |
d0ec0ed2 | 988 | struct urcu_atfork *atfork; |
e85451a1 | 989 | |
81ad2e19 | 990 | call_rcu_lock(&call_rcu_mutex); |
e85451a1 | 991 | |
d0ec0ed2 MD |
992 | atfork = registered_rculfhash_atfork; |
993 | if (atfork) | |
994 | atfork->before_fork(atfork->priv); | |
995 | ||
e85451a1 MD |
996 | cds_list_for_each_entry(crdp, &call_rcu_data_list, list) { |
997 | uatomic_or(&crdp->flags, URCU_CALL_RCU_PAUSE); | |
998 | cmm_smp_mb__after_uatomic_or(); | |
999 | wake_call_rcu_thread(crdp); | |
1000 | } | |
1001 | cds_list_for_each_entry(crdp, &call_rcu_data_list, list) { | |
1002 | while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_PAUSED) == 0) | |
109105b7 | 1003 | (void) poll(NULL, 0, 1); |
e85451a1 | 1004 | } |
81ad2e19 | 1005 | } |
ce28e67a | 1006 | URCU_ATTR_ALIAS(urcu_stringify(call_rcu_before_fork)) |
4477a870 | 1007 | void alias_call_rcu_before_fork(); |
81ad2e19 PM |
1008 | |
1009 | /* | |
1010 | * Clean up call_rcu data structures in the parent of a successful fork() | |
1011 | * that is not followed by exec() in the child. Suitable for | |
1012 | * pthread_atfork() and friends. | |
1013 | */ | |
1014 | void call_rcu_after_fork_parent(void) | |
1015 | { | |
e85451a1 | 1016 | struct call_rcu_data *crdp; |
d0ec0ed2 | 1017 | struct urcu_atfork *atfork; |
e85451a1 MD |
1018 | |
1019 | cds_list_for_each_entry(crdp, &call_rcu_data_list, list) | |
1020 | uatomic_and(&crdp->flags, ~URCU_CALL_RCU_PAUSE); | |
fc236e5e KF |
1021 | cds_list_for_each_entry(crdp, &call_rcu_data_list, list) { |
1022 | while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_PAUSED) != 0) | |
109105b7 | 1023 | (void) poll(NULL, 0, 1); |
fc236e5e | 1024 | } |
d0ec0ed2 MD |
1025 | atfork = registered_rculfhash_atfork; |
1026 | if (atfork) | |
1027 | atfork->after_fork_parent(atfork->priv); | |
81ad2e19 PM |
1028 | call_rcu_unlock(&call_rcu_mutex); |
1029 | } | |
ce28e67a | 1030 | URCU_ATTR_ALIAS(urcu_stringify(call_rcu_after_fork_parent)) |
4477a870 | 1031 | void alias_call_rcu_after_fork_parent(); |
81ad2e19 | 1032 | |
7106ddf8 PM |
1033 | /* |
1034 | * Clean up call_rcu data structures in the child of a successful fork() | |
81ad2e19 PM |
1035 | * that is not followed by exec(). Suitable for pthread_atfork() and |
1036 | * friends. | |
7106ddf8 PM |
1037 | */ |
1038 | void call_rcu_after_fork_child(void) | |
1039 | { | |
077ff173 | 1040 | struct call_rcu_data *crdp, *next; |
d0ec0ed2 | 1041 | struct urcu_atfork *atfork; |
7106ddf8 | 1042 | |
81ad2e19 PM |
1043 | /* Release the mutex. */ |
1044 | call_rcu_unlock(&call_rcu_mutex); | |
1045 | ||
d0ec0ed2 MD |
1046 | atfork = registered_rculfhash_atfork; |
1047 | if (atfork) | |
1048 | atfork->after_fork_child(atfork->priv); | |
1049 | ||
ad1b9909 LJ |
1050 | /* Do nothing when call_rcu() has not been used */ |
1051 | if (cds_list_empty(&call_rcu_data_list)) | |
1052 | return; | |
1053 | ||
7106ddf8 PM |
1054 | /* |
1055 | * Allocate a new default call_rcu_data structure in order | |
1056 | * to get a working call_rcu thread to go with it. | |
1057 | */ | |
1058 | default_call_rcu_data = NULL; | |
1059 | (void)get_default_call_rcu_data(); | |
1060 | ||
60af049d | 1061 | /* Cleanup call_rcu_data pointers before use */ |
01730852 | 1062 | cpus_array_len_reset(); |
60af049d | 1063 | free(per_cpu_call_rcu_data); |
618b2595 | 1064 | rcu_set_pointer(&per_cpu_call_rcu_data, NULL); |
bd252a04 | 1065 | URCU_TLS(thread_call_rcu_data) = NULL; |
60af049d | 1066 | |
e85451a1 MD |
1067 | /* |
1068 | * Dispose of all of the rest of the call_rcu_data structures. | |
1069 | * Leftover call_rcu callbacks will be merged into the new | |
1070 | * default call_rcu thread queue. | |
1071 | */ | |
077ff173 | 1072 | cds_list_for_each_entry_safe(crdp, next, &call_rcu_data_list, list) { |
7106ddf8 | 1073 | if (crdp == default_call_rcu_data) |
077ff173 | 1074 | continue; |
2870aa1e | 1075 | uatomic_set(&crdp->flags, URCU_CALL_RCU_STOPPED); |
232cf6ac MD |
1076 | /* |
1077 | * Do not join the thread because it does not exist in | |
1078 | * the child. | |
1079 | */ | |
1080 | _call_rcu_data_free(crdp, 0); | |
b57aee66 PM |
1081 | } |
1082 | } | |
ce28e67a | 1083 | URCU_ATTR_ALIAS(urcu_stringify(call_rcu_after_fork_child)) |
4477a870 | 1084 | void alias_call_rcu_after_fork_child(); |
d0ec0ed2 MD |
1085 | |
1086 | void urcu_register_rculfhash_atfork(struct urcu_atfork *atfork) | |
1087 | { | |
19ec56ca MD |
1088 | if (CMM_LOAD_SHARED(registered_rculfhash_atfork)) |
1089 | return; | |
d0ec0ed2 | 1090 | call_rcu_lock(&call_rcu_mutex); |
19ec56ca MD |
1091 | if (!registered_rculfhash_atfork) |
1092 | registered_rculfhash_atfork = atfork; | |
d0ec0ed2 MD |
1093 | call_rcu_unlock(&call_rcu_mutex); |
1094 | } | |
ce28e67a | 1095 | URCU_ATTR_ALIAS(urcu_stringify(urcu_register_rculfhash_atfork)) |
4477a870 | 1096 | void alias_urcu_register_rculfhash_atfork(); |
d0ec0ed2 | 1097 | |
19ec56ca MD |
1098 | /* |
1099 | * This unregistration function is deprecated, meant only for internal | |
1100 | * use by rculfhash. | |
1101 | */ | |
1102 | __attribute__((noreturn)) | |
17a8f206 | 1103 | void urcu_unregister_rculfhash_atfork(struct urcu_atfork *atfork __attribute__((unused))) |
d0ec0ed2 | 1104 | { |
19ec56ca | 1105 | urcu_die(EPERM); |
d0ec0ed2 | 1106 | } |
457eeeee | 1107 | |
ce28e67a | 1108 | URCU_ATTR_ALIAS(urcu_stringify(urcu_unregister_rculfhash_atfork)) |
19ec56ca | 1109 | __attribute__((noreturn)) |
4477a870 | 1110 | void alias_urcu_unregister_rculfhash_atfork(); |
457eeeee MD |
1111 | |
1112 | /* | |
1113 | * Teardown the default call_rcu worker thread if there are no queued | |
1114 | * callbacks on process exit. This prevents leaking memory. | |
1115 | * | |
1116 | * Here is how an application can ensure graceful teardown of this | |
1117 | * worker thread: | |
1118 | * | |
1119 | * - An application queuing call_rcu callbacks should invoke | |
1120 | * rcu_barrier() before it exits. | |
1121 | * - When chaining call_rcu callbacks, the number of calls to | |
1122 | * rcu_barrier() on application exit must match at least the maximum | |
1123 | * number of chained callbacks. | |
1124 | * - If an application chains callbacks endlessly, it would have to be | |
1125 | * modified to stop chaining callbacks when it detects an application | |
1126 | * exit (e.g. with a flag), and wait for quiescence with rcu_barrier() | |
1127 | * after setting that flag. | |
1128 | * - The statements above apply to a library which queues call_rcu | |
1129 | * callbacks, only it needs to invoke rcu_barrier in its library | |
1130 | * destructor. | |
1131 | * | |
1132 | * Note that this function does not presume it is being called when the | |
1133 | * application is single-threaded even though this is invoked from a | |
1134 | * destructor: this function synchronizes against concurrent calls to | |
1135 | * get_default_call_rcu_data(). | |
1136 | */ | |
1137 | static void urcu_call_rcu_exit(void) | |
1138 | { | |
1139 | struct call_rcu_data *crdp; | |
1140 | bool teardown = true; | |
1141 | ||
1142 | if (default_call_rcu_data == NULL) | |
1143 | return; | |
1144 | call_rcu_lock(&call_rcu_mutex); | |
1145 | /* | |
1146 | * If the application leaves callbacks in the default call_rcu | |
1147 | * worker queue, keep the default worker in place. | |
1148 | */ | |
1149 | crdp = default_call_rcu_data; | |
1150 | if (!crdp) { | |
1151 | teardown = false; | |
1152 | goto unlock; | |
1153 | } | |
1154 | if (!cds_wfcq_empty(&crdp->cbs_head, &crdp->cbs_tail)) { | |
1155 | teardown = false; | |
1156 | goto unlock; | |
1157 | } | |
1158 | rcu_set_pointer(&default_call_rcu_data, NULL); | |
1159 | unlock: | |
1160 | call_rcu_unlock(&call_rcu_mutex); | |
1161 | if (teardown) { | |
1162 | synchronize_rcu(); | |
1163 | call_rcu_data_free(crdp); | |
1164 | } | |
1165 | } |