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 | ||
c1d2c60b | 23 | #define _GNU_SOURCE |
5161f31e | 24 | #define _LGPL_SOURCE |
b57aee66 PM |
25 | #include <stdio.h> |
26 | #include <pthread.h> | |
27 | #include <signal.h> | |
28 | #include <assert.h> | |
29 | #include <stdlib.h> | |
6d841bc2 | 30 | #include <stdint.h> |
b57aee66 PM |
31 | #include <string.h> |
32 | #include <errno.h> | |
33 | #include <poll.h> | |
34 | #include <sys/time.h> | |
b57aee66 | 35 | #include <unistd.h> |
c1d2c60b | 36 | #include <sched.h> |
b57aee66 PM |
37 | |
38 | #include "config.h" | |
5161f31e | 39 | #include "urcu/wfcqueue.h" |
b57aee66 PM |
40 | #include "urcu-call-rcu.h" |
41 | #include "urcu-pointer.h" | |
3c24913f | 42 | #include "urcu/list.h" |
41849996 | 43 | #include "urcu/futex.h" |
bd252a04 | 44 | #include "urcu/tls-compat.h" |
4a6d7378 | 45 | #include "urcu-die.h" |
b57aee66 PM |
46 | |
47 | /* Data structure that identifies a call_rcu thread. */ | |
48 | ||
49 | struct call_rcu_data { | |
5161f31e MD |
50 | /* |
51 | * Align the tail on cache line size to eliminate false-sharing | |
b9f893b6 MD |
52 | * with head. Small note, however: the "qlen" field, kept for |
53 | * debugging, will cause false-sharing between enqueue and | |
54 | * dequeue. | |
5161f31e | 55 | */ |
b9f893b6 | 56 | struct cds_wfcq_tail cbs_tail; |
5161f31e MD |
57 | /* Alignment on cache line size will add padding here */ |
58 | ||
b9f893b6 | 59 | struct cds_wfcq_head __attribute__((aligned(CAA_CACHE_LINE_SIZE))) cbs_head; |
b57aee66 | 60 | unsigned long flags; |
6d841bc2 | 61 | int32_t futex; |
73987721 | 62 | unsigned long qlen; /* maintained for debugging. */ |
b57aee66 | 63 | pthread_t tid; |
c1d2c60b | 64 | int cpu_affinity; |
3c24913f | 65 | struct cds_list_head list; |
b57aee66 PM |
66 | } __attribute__((aligned(CAA_CACHE_LINE_SIZE))); |
67 | ||
3c24913f PM |
68 | /* |
69 | * List of all call_rcu_data structures to keep valgrind happy. | |
70 | * Protected by call_rcu_mutex. | |
71 | */ | |
72 | ||
73 | CDS_LIST_HEAD(call_rcu_data_list); | |
74 | ||
b57aee66 PM |
75 | /* Link a thread using call_rcu() to its call_rcu thread. */ |
76 | ||
bd252a04 | 77 | static DEFINE_URCU_TLS(struct call_rcu_data *, thread_call_rcu_data); |
b57aee66 PM |
78 | |
79 | /* Guard call_rcu thread creation. */ | |
80 | ||
81 | static pthread_mutex_t call_rcu_mutex = PTHREAD_MUTEX_INITIALIZER; | |
82 | ||
83 | /* If a given thread does not have its own call_rcu thread, this is default. */ | |
84 | ||
85 | static struct call_rcu_data *default_call_rcu_data; | |
86 | ||
b57aee66 PM |
87 | /* |
88 | * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are | |
89 | * available, then we can have call_rcu threads assigned to individual | |
90 | * CPUs rather than only to specific threads. | |
91 | */ | |
92 | ||
93 | #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) | |
94 | ||
95 | /* | |
96 | * Pointer to array of pointers to per-CPU call_rcu_data structures | |
618b2595 MD |
97 | * and # CPUs. per_cpu_call_rcu_data is a RCU-protected pointer to an |
98 | * array of RCU-protected pointers to call_rcu_data. call_rcu acts as a | |
99 | * RCU read-side and reads per_cpu_call_rcu_data and the per-cpu pointer | |
100 | * without mutex. The call_rcu_mutex protects updates. | |
b57aee66 PM |
101 | */ |
102 | ||
103 | static struct call_rcu_data **per_cpu_call_rcu_data; | |
104 | static long maxcpus; | |
105 | ||
60af049d LJ |
106 | static void maxcpus_reset(void) |
107 | { | |
108 | maxcpus = 0; | |
109 | } | |
110 | ||
b57aee66 PM |
111 | /* Allocate the array if it has not already been allocated. */ |
112 | ||
113 | static void alloc_cpu_call_rcu_data(void) | |
114 | { | |
115 | struct call_rcu_data **p; | |
116 | static int warned = 0; | |
117 | ||
118 | if (maxcpus != 0) | |
119 | return; | |
120 | maxcpus = sysconf(_SC_NPROCESSORS_CONF); | |
121 | if (maxcpus <= 0) { | |
122 | return; | |
123 | } | |
124 | p = malloc(maxcpus * sizeof(*per_cpu_call_rcu_data)); | |
125 | if (p != NULL) { | |
126 | memset(p, '\0', maxcpus * sizeof(*per_cpu_call_rcu_data)); | |
618b2595 | 127 | rcu_set_pointer(&per_cpu_call_rcu_data, p); |
b57aee66 PM |
128 | } else { |
129 | if (!warned) { | |
130 | fprintf(stderr, "[error] liburcu: unable to allocate per-CPU pointer array\n"); | |
131 | } | |
132 | warned = 1; | |
133 | } | |
134 | } | |
135 | ||
136 | #else /* #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */ | |
137 | ||
f9437098 MD |
138 | /* |
139 | * per_cpu_call_rcu_data should be constant, but some functions below, used both | |
140 | * for cases where cpu number is available and not available, assume it it not | |
141 | * constant. | |
142 | */ | |
143 | static struct call_rcu_data **per_cpu_call_rcu_data = NULL; | |
b57aee66 PM |
144 | static const long maxcpus = -1; |
145 | ||
60af049d LJ |
146 | static void maxcpus_reset(void) |
147 | { | |
148 | } | |
149 | ||
b57aee66 PM |
150 | static void alloc_cpu_call_rcu_data(void) |
151 | { | |
152 | } | |
153 | ||
154 | static int sched_getcpu(void) | |
155 | { | |
156 | return -1; | |
157 | } | |
158 | ||
159 | #endif /* #else #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */ | |
160 | ||
161 | /* Acquire the specified pthread mutex. */ | |
162 | ||
163 | static void call_rcu_lock(pthread_mutex_t *pmp) | |
164 | { | |
4a6d7378 MD |
165 | int ret; |
166 | ||
167 | ret = pthread_mutex_lock(pmp); | |
168 | if (ret) | |
169 | urcu_die(ret); | |
b57aee66 PM |
170 | } |
171 | ||
172 | /* Release the specified pthread mutex. */ | |
173 | ||
174 | static void call_rcu_unlock(pthread_mutex_t *pmp) | |
175 | { | |
4a6d7378 MD |
176 | int ret; |
177 | ||
178 | ret = pthread_mutex_unlock(pmp); | |
179 | if (ret) | |
180 | urcu_die(ret); | |
b57aee66 PM |
181 | } |
182 | ||
c1d2c60b MD |
183 | #if HAVE_SCHED_SETAFFINITY |
184 | static | |
185 | int set_thread_cpu_affinity(struct call_rcu_data *crdp) | |
186 | { | |
187 | cpu_set_t mask; | |
188 | ||
189 | if (crdp->cpu_affinity < 0) | |
190 | return 0; | |
191 | ||
192 | CPU_ZERO(&mask); | |
193 | CPU_SET(crdp->cpu_affinity, &mask); | |
194 | #if SCHED_SETAFFINITY_ARGS == 2 | |
195 | return sched_setaffinity(0, &mask); | |
196 | #else | |
197 | return sched_setaffinity(0, sizeof(mask), &mask); | |
198 | #endif | |
199 | } | |
200 | #else | |
201 | static | |
202 | int set_thread_cpu_affinity(struct call_rcu_data *crdp) | |
203 | { | |
204 | return 0; | |
205 | } | |
206 | #endif | |
207 | ||
03fe58b3 MD |
208 | static void call_rcu_wait(struct call_rcu_data *crdp) |
209 | { | |
210 | /* Read call_rcu list before read futex */ | |
211 | cmm_smp_mb(); | |
212 | if (uatomic_read(&crdp->futex) == -1) | |
213 | futex_async(&crdp->futex, FUTEX_WAIT, -1, | |
214 | NULL, NULL, 0); | |
215 | } | |
216 | ||
217 | static void call_rcu_wake_up(struct call_rcu_data *crdp) | |
218 | { | |
219 | /* Write to call_rcu list before reading/writing futex */ | |
220 | cmm_smp_mb(); | |
a0b7f7ea | 221 | if (caa_unlikely(uatomic_read(&crdp->futex) == -1)) { |
03fe58b3 MD |
222 | uatomic_set(&crdp->futex, 0); |
223 | futex_async(&crdp->futex, FUTEX_WAKE, 1, | |
224 | NULL, NULL, 0); | |
225 | } | |
226 | } | |
227 | ||
b57aee66 PM |
228 | /* This is the code run by each call_rcu thread. */ |
229 | ||
230 | static void *call_rcu_thread(void *arg) | |
231 | { | |
232 | unsigned long cbcount; | |
5161f31e | 233 | struct call_rcu_data *crdp = (struct call_rcu_data *) arg; |
2870aa1e | 234 | int rt = !!(uatomic_read(&crdp->flags) & URCU_CALL_RCU_RT); |
4a6d7378 | 235 | int ret; |
b57aee66 | 236 | |
4a6d7378 MD |
237 | ret = set_thread_cpu_affinity(crdp); |
238 | if (ret) | |
239 | urcu_die(errno); | |
c1d2c60b | 240 | |
765f3ead MD |
241 | /* |
242 | * If callbacks take a read-side lock, we need to be registered. | |
243 | */ | |
244 | rcu_register_thread(); | |
245 | ||
bd252a04 | 246 | URCU_TLS(thread_call_rcu_data) = crdp; |
bc94ca9b MD |
247 | if (!rt) { |
248 | uatomic_dec(&crdp->futex); | |
249 | /* Decrement futex before reading call_rcu list */ | |
250 | cmm_smp_mb(); | |
251 | } | |
b57aee66 | 252 | for (;;) { |
5161f31e MD |
253 | struct cds_wfcq_head cbs_tmp_head; |
254 | struct cds_wfcq_tail cbs_tmp_tail; | |
255 | struct cds_wfcq_node *cbs, *cbs_tmp_n; | |
256 | ||
257 | cds_wfcq_init(&cbs_tmp_head, &cbs_tmp_tail); | |
258 | __cds_wfcq_splice_blocking(&cbs_tmp_head, &cbs_tmp_tail, | |
259 | &crdp->cbs_head, &crdp->cbs_tail); | |
260 | if (!cds_wfcq_empty(&cbs_tmp_head, &cbs_tmp_tail)) { | |
b57aee66 PM |
261 | synchronize_rcu(); |
262 | cbcount = 0; | |
5161f31e MD |
263 | __cds_wfcq_for_each_blocking_safe(&cbs_tmp_head, |
264 | &cbs_tmp_tail, cbs, cbs_tmp_n) { | |
265 | struct rcu_head *rhp; | |
266 | ||
267 | rhp = caa_container_of(cbs, | |
268 | struct rcu_head, next); | |
b57aee66 PM |
269 | rhp->func(rhp); |
270 | cbcount++; | |
5161f31e | 271 | } |
b57aee66 PM |
272 | uatomic_sub(&crdp->qlen, cbcount); |
273 | } | |
bc94ca9b MD |
274 | if (uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOP) |
275 | break; | |
765f3ead | 276 | rcu_thread_offline(); |
bc94ca9b | 277 | if (!rt) { |
5161f31e MD |
278 | if (cds_wfcq_empty(&crdp->cbs_head, |
279 | &crdp->cbs_tail)) { | |
bc94ca9b MD |
280 | call_rcu_wait(crdp); |
281 | poll(NULL, 0, 10); | |
282 | uatomic_dec(&crdp->futex); | |
c768e45e | 283 | /* |
bc94ca9b MD |
284 | * Decrement futex before reading |
285 | * call_rcu list. | |
c768e45e MD |
286 | */ |
287 | cmm_smp_mb(); | |
ccbac24d MD |
288 | } else { |
289 | poll(NULL, 0, 10); | |
c768e45e | 290 | } |
bc94ca9b MD |
291 | } else { |
292 | poll(NULL, 0, 10); | |
b57aee66 | 293 | } |
765f3ead | 294 | rcu_thread_online(); |
bc94ca9b MD |
295 | } |
296 | if (!rt) { | |
297 | /* | |
298 | * Read call_rcu list before write futex. | |
299 | */ | |
300 | cmm_smp_mb(); | |
301 | uatomic_set(&crdp->futex, 0); | |
b57aee66 | 302 | } |
2870aa1e | 303 | uatomic_or(&crdp->flags, URCU_CALL_RCU_STOPPED); |
765f3ead | 304 | rcu_unregister_thread(); |
7106ddf8 | 305 | return NULL; |
b57aee66 PM |
306 | } |
307 | ||
308 | /* | |
309 | * Create both a call_rcu thread and the corresponding call_rcu_data | |
3c24913f PM |
310 | * structure, linking the structure in as specified. Caller must hold |
311 | * call_rcu_mutex. | |
b57aee66 PM |
312 | */ |
313 | ||
3c24913f | 314 | static void call_rcu_data_init(struct call_rcu_data **crdpp, |
c1d2c60b MD |
315 | unsigned long flags, |
316 | int cpu_affinity) | |
b57aee66 PM |
317 | { |
318 | struct call_rcu_data *crdp; | |
4a6d7378 | 319 | int ret; |
b57aee66 PM |
320 | |
321 | crdp = malloc(sizeof(*crdp)); | |
4a6d7378 MD |
322 | if (crdp == NULL) |
323 | urcu_die(errno); | |
b57aee66 | 324 | memset(crdp, '\0', sizeof(*crdp)); |
5161f31e | 325 | cds_wfcq_init(&crdp->cbs_head, &crdp->cbs_tail); |
b57aee66 | 326 | crdp->qlen = 0; |
263e3cf9 MD |
327 | crdp->futex = 0; |
328 | crdp->flags = flags; | |
3c24913f | 329 | cds_list_add(&crdp->list, &call_rcu_data_list); |
c1d2c60b | 330 | crdp->cpu_affinity = cpu_affinity; |
b57aee66 PM |
331 | cmm_smp_mb(); /* Structure initialized before pointer is planted. */ |
332 | *crdpp = crdp; | |
4a6d7378 MD |
333 | ret = pthread_create(&crdp->tid, NULL, call_rcu_thread, crdp); |
334 | if (ret) | |
335 | urcu_die(ret); | |
b57aee66 PM |
336 | } |
337 | ||
338 | /* | |
339 | * Return a pointer to the call_rcu_data structure for the specified | |
340 | * CPU, returning NULL if there is none. We cannot automatically | |
341 | * created it because the platform we are running on might not define | |
342 | * sched_getcpu(). | |
618b2595 MD |
343 | * |
344 | * The call to this function and use of the returned call_rcu_data | |
345 | * should be protected by RCU read-side lock. | |
b57aee66 PM |
346 | */ |
347 | ||
348 | struct call_rcu_data *get_cpu_call_rcu_data(int cpu) | |
349 | { | |
350 | static int warned = 0; | |
618b2595 | 351 | struct call_rcu_data **pcpu_crdp; |
b57aee66 | 352 | |
618b2595 MD |
353 | pcpu_crdp = rcu_dereference(per_cpu_call_rcu_data); |
354 | if (pcpu_crdp == NULL) | |
b57aee66 PM |
355 | return NULL; |
356 | if (!warned && maxcpus > 0 && (cpu < 0 || maxcpus <= cpu)) { | |
357 | fprintf(stderr, "[error] liburcu: get CPU # out of range\n"); | |
358 | warned = 1; | |
359 | } | |
360 | if (cpu < 0 || maxcpus <= cpu) | |
361 | return NULL; | |
618b2595 | 362 | return rcu_dereference(pcpu_crdp[cpu]); |
b57aee66 PM |
363 | } |
364 | ||
365 | /* | |
366 | * Return the tid corresponding to the call_rcu thread whose | |
367 | * call_rcu_data structure is specified. | |
368 | */ | |
369 | ||
370 | pthread_t get_call_rcu_thread(struct call_rcu_data *crdp) | |
371 | { | |
372 | return crdp->tid; | |
373 | } | |
374 | ||
375 | /* | |
376 | * Create a call_rcu_data structure (with thread) and return a pointer. | |
377 | */ | |
378 | ||
c1d2c60b MD |
379 | static struct call_rcu_data *__create_call_rcu_data(unsigned long flags, |
380 | int cpu_affinity) | |
b57aee66 PM |
381 | { |
382 | struct call_rcu_data *crdp; | |
383 | ||
c1d2c60b | 384 | call_rcu_data_init(&crdp, flags, cpu_affinity); |
b57aee66 PM |
385 | return crdp; |
386 | } | |
387 | ||
c1d2c60b MD |
388 | struct call_rcu_data *create_call_rcu_data(unsigned long flags, |
389 | int cpu_affinity) | |
3c24913f PM |
390 | { |
391 | struct call_rcu_data *crdp; | |
392 | ||
393 | call_rcu_lock(&call_rcu_mutex); | |
c1d2c60b | 394 | crdp = __create_call_rcu_data(flags, cpu_affinity); |
3c24913f PM |
395 | call_rcu_unlock(&call_rcu_mutex); |
396 | return crdp; | |
397 | } | |
398 | ||
b57aee66 PM |
399 | /* |
400 | * Set the specified CPU to use the specified call_rcu_data structure. | |
7106ddf8 PM |
401 | * |
402 | * Use NULL to remove a CPU's call_rcu_data structure, but it is | |
403 | * the caller's responsibility to dispose of the removed structure. | |
404 | * Use get_cpu_call_rcu_data() to obtain a pointer to the old structure | |
405 | * (prior to NULLing it out, of course). | |
f9da0936 MD |
406 | * |
407 | * The caller must wait for a grace-period to pass between return from | |
408 | * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the | |
409 | * previous call rcu data as argument. | |
b57aee66 PM |
410 | */ |
411 | ||
412 | int set_cpu_call_rcu_data(int cpu, struct call_rcu_data *crdp) | |
413 | { | |
dcfc8165 | 414 | static int warned = 0; |
b57aee66 PM |
415 | |
416 | call_rcu_lock(&call_rcu_mutex); | |
f3776786 | 417 | alloc_cpu_call_rcu_data(); |
b57aee66 PM |
418 | if (cpu < 0 || maxcpus <= cpu) { |
419 | if (!warned) { | |
420 | fprintf(stderr, "[error] liburcu: set CPU # out of range\n"); | |
421 | warned = 1; | |
422 | } | |
423 | call_rcu_unlock(&call_rcu_mutex); | |
424 | errno = EINVAL; | |
425 | return -EINVAL; | |
426 | } | |
53a55535 | 427 | |
b57aee66 | 428 | if (per_cpu_call_rcu_data == NULL) { |
3670fef2 | 429 | call_rcu_unlock(&call_rcu_mutex); |
b57aee66 PM |
430 | errno = ENOMEM; |
431 | return -ENOMEM; | |
432 | } | |
53a55535 LJ |
433 | |
434 | if (per_cpu_call_rcu_data[cpu] != NULL && crdp != NULL) { | |
435 | call_rcu_unlock(&call_rcu_mutex); | |
436 | errno = EEXIST; | |
437 | return -EEXIST; | |
438 | } | |
439 | ||
618b2595 | 440 | rcu_set_pointer(&per_cpu_call_rcu_data[cpu], crdp); |
3670fef2 | 441 | call_rcu_unlock(&call_rcu_mutex); |
b57aee66 PM |
442 | return 0; |
443 | } | |
444 | ||
445 | /* | |
446 | * Return a pointer to the default call_rcu_data structure, creating | |
447 | * one if need be. Because we never free call_rcu_data structures, | |
448 | * we don't need to be in an RCU read-side critical section. | |
449 | */ | |
450 | ||
451 | struct call_rcu_data *get_default_call_rcu_data(void) | |
452 | { | |
453 | if (default_call_rcu_data != NULL) | |
454 | return rcu_dereference(default_call_rcu_data); | |
455 | call_rcu_lock(&call_rcu_mutex); | |
456 | if (default_call_rcu_data != NULL) { | |
457 | call_rcu_unlock(&call_rcu_mutex); | |
458 | return default_call_rcu_data; | |
459 | } | |
c1d2c60b | 460 | call_rcu_data_init(&default_call_rcu_data, 0, -1); |
b57aee66 PM |
461 | call_rcu_unlock(&call_rcu_mutex); |
462 | return default_call_rcu_data; | |
463 | } | |
464 | ||
465 | /* | |
466 | * Return the call_rcu_data structure that applies to the currently | |
467 | * running thread. Any call_rcu_data structure assigned specifically | |
468 | * to this thread has first priority, followed by any call_rcu_data | |
469 | * structure assigned to the CPU on which the thread is running, | |
470 | * followed by the default call_rcu_data structure. If there is not | |
471 | * yet a default call_rcu_data structure, one will be created. | |
618b2595 MD |
472 | * |
473 | * Calls to this function and use of the returned call_rcu_data should | |
474 | * be protected by RCU read-side lock. | |
b57aee66 PM |
475 | */ |
476 | struct call_rcu_data *get_call_rcu_data(void) | |
477 | { | |
9744f3bb | 478 | struct call_rcu_data *crd; |
b57aee66 | 479 | |
bd252a04 MD |
480 | if (URCU_TLS(thread_call_rcu_data) != NULL) |
481 | return URCU_TLS(thread_call_rcu_data); | |
9744f3bb LJ |
482 | |
483 | if (maxcpus > 0) { | |
484 | crd = get_cpu_call_rcu_data(sched_getcpu()); | |
485 | if (crd) | |
486 | return crd; | |
b57aee66 | 487 | } |
9744f3bb | 488 | |
b57aee66 PM |
489 | return get_default_call_rcu_data(); |
490 | } | |
491 | ||
492 | /* | |
493 | * Return a pointer to this task's call_rcu_data if there is one. | |
494 | */ | |
495 | ||
496 | struct call_rcu_data *get_thread_call_rcu_data(void) | |
497 | { | |
bd252a04 | 498 | return URCU_TLS(thread_call_rcu_data); |
b57aee66 PM |
499 | } |
500 | ||
501 | /* | |
502 | * Set this task's call_rcu_data structure as specified, regardless | |
503 | * of whether or not this task already had one. (This allows switching | |
504 | * to and from real-time call_rcu threads, for example.) | |
7106ddf8 PM |
505 | * |
506 | * Use NULL to remove a thread's call_rcu_data structure, but it is | |
507 | * the caller's responsibility to dispose of the removed structure. | |
508 | * Use get_thread_call_rcu_data() to obtain a pointer to the old structure | |
509 | * (prior to NULLing it out, of course). | |
b57aee66 PM |
510 | */ |
511 | ||
512 | void set_thread_call_rcu_data(struct call_rcu_data *crdp) | |
513 | { | |
bd252a04 | 514 | URCU_TLS(thread_call_rcu_data) = crdp; |
b57aee66 PM |
515 | } |
516 | ||
517 | /* | |
518 | * Create a separate call_rcu thread for each CPU. This does not | |
519 | * replace a pre-existing call_rcu thread -- use the set_cpu_call_rcu_data() | |
0938c541 MD |
520 | * function if you want that behavior. Should be paired with |
521 | * free_all_cpu_call_rcu_data() to teardown these call_rcu worker | |
522 | * threads. | |
b57aee66 PM |
523 | */ |
524 | ||
525 | int create_all_cpu_call_rcu_data(unsigned long flags) | |
526 | { | |
527 | int i; | |
528 | struct call_rcu_data *crdp; | |
529 | int ret; | |
530 | ||
531 | call_rcu_lock(&call_rcu_mutex); | |
532 | alloc_cpu_call_rcu_data(); | |
533 | call_rcu_unlock(&call_rcu_mutex); | |
534 | if (maxcpus <= 0) { | |
535 | errno = EINVAL; | |
536 | return -EINVAL; | |
537 | } | |
538 | if (per_cpu_call_rcu_data == NULL) { | |
539 | errno = ENOMEM; | |
540 | return -ENOMEM; | |
541 | } | |
542 | for (i = 0; i < maxcpus; i++) { | |
543 | call_rcu_lock(&call_rcu_mutex); | |
544 | if (get_cpu_call_rcu_data(i)) { | |
545 | call_rcu_unlock(&call_rcu_mutex); | |
546 | continue; | |
547 | } | |
c1d2c60b | 548 | crdp = __create_call_rcu_data(flags, i); |
b57aee66 PM |
549 | if (crdp == NULL) { |
550 | call_rcu_unlock(&call_rcu_mutex); | |
551 | errno = ENOMEM; | |
552 | return -ENOMEM; | |
553 | } | |
554 | call_rcu_unlock(&call_rcu_mutex); | |
555 | if ((ret = set_cpu_call_rcu_data(i, crdp)) != 0) { | |
356c8794 LJ |
556 | call_rcu_data_free(crdp); |
557 | ||
558 | /* it has been created by other thread */ | |
559 | if (ret == -EEXIST) | |
560 | continue; | |
561 | ||
562 | return ret; | |
b57aee66 PM |
563 | } |
564 | } | |
565 | return 0; | |
566 | } | |
567 | ||
7106ddf8 PM |
568 | /* |
569 | * Wake up the call_rcu thread corresponding to the specified | |
570 | * call_rcu_data structure. | |
571 | */ | |
572 | static void wake_call_rcu_thread(struct call_rcu_data *crdp) | |
573 | { | |
263e3cf9 MD |
574 | if (!(_CMM_LOAD_SHARED(crdp->flags) & URCU_CALL_RCU_RT)) |
575 | call_rcu_wake_up(crdp); | |
7106ddf8 PM |
576 | } |
577 | ||
b57aee66 PM |
578 | /* |
579 | * Schedule a function to be invoked after a following grace period. | |
580 | * This is the only function that must be called -- the others are | |
581 | * only present to allow applications to tune their use of RCU for | |
582 | * maximum performance. | |
583 | * | |
584 | * Note that unless a call_rcu thread has not already been created, | |
585 | * the first invocation of call_rcu() will create one. So, if you | |
586 | * need the first invocation of call_rcu() to be fast, make sure | |
587 | * to create a call_rcu thread first. One way to accomplish this is | |
588 | * "get_call_rcu_data();", and another is create_all_cpu_call_rcu_data(). | |
618b2595 MD |
589 | * |
590 | * call_rcu must be called by registered RCU read-side threads. | |
b57aee66 PM |
591 | */ |
592 | ||
593 | void call_rcu(struct rcu_head *head, | |
594 | void (*func)(struct rcu_head *head)) | |
595 | { | |
596 | struct call_rcu_data *crdp; | |
597 | ||
5161f31e | 598 | cds_wfcq_node_init(&head->next); |
b57aee66 | 599 | head->func = func; |
618b2595 MD |
600 | /* Holding rcu read-side lock across use of per-cpu crdp */ |
601 | rcu_read_lock(); | |
b57aee66 | 602 | crdp = get_call_rcu_data(); |
5161f31e | 603 | cds_wfcq_enqueue(&crdp->cbs_head, &crdp->cbs_tail, &head->next); |
b57aee66 | 604 | uatomic_inc(&crdp->qlen); |
7106ddf8 | 605 | wake_call_rcu_thread(crdp); |
618b2595 | 606 | rcu_read_unlock(); |
7106ddf8 PM |
607 | } |
608 | ||
609 | /* | |
610 | * Free up the specified call_rcu_data structure, terminating the | |
611 | * associated call_rcu thread. The caller must have previously | |
612 | * removed the call_rcu_data structure from per-thread or per-CPU | |
613 | * usage. For example, set_cpu_call_rcu_data(cpu, NULL) for per-CPU | |
614 | * call_rcu_data structures or set_thread_call_rcu_data(NULL) for | |
615 | * per-thread call_rcu_data structures. | |
616 | * | |
617 | * We silently refuse to free up the default call_rcu_data structure | |
618 | * because that is where we put any leftover callbacks. Note that | |
619 | * the possibility of self-spawning callbacks makes it impossible | |
620 | * to execute all the callbacks in finite time without putting any | |
621 | * newly spawned callbacks somewhere else. The "somewhere else" of | |
622 | * last resort is the default call_rcu_data structure. | |
623 | * | |
624 | * We also silently refuse to free NULL pointers. This simplifies | |
625 | * the calling code. | |
f9da0936 MD |
626 | * |
627 | * The caller must wait for a grace-period to pass between return from | |
628 | * set_cpu_call_rcu_data() and call to call_rcu_data_free() passing the | |
629 | * previous call rcu data as argument. | |
7106ddf8 PM |
630 | */ |
631 | void call_rcu_data_free(struct call_rcu_data *crdp) | |
632 | { | |
7106ddf8 PM |
633 | if (crdp == NULL || crdp == default_call_rcu_data) { |
634 | return; | |
635 | } | |
2870aa1e PB |
636 | if ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0) { |
637 | uatomic_or(&crdp->flags, URCU_CALL_RCU_STOP); | |
7106ddf8 | 638 | wake_call_rcu_thread(crdp); |
2870aa1e | 639 | while ((uatomic_read(&crdp->flags) & URCU_CALL_RCU_STOPPED) == 0) |
7106ddf8 PM |
640 | poll(NULL, 0, 1); |
641 | } | |
5161f31e | 642 | if (!cds_wfcq_empty(&crdp->cbs_head, &crdp->cbs_tail)) { |
698d0778 MD |
643 | /* Create default call rcu data if need be */ |
644 | (void) get_default_call_rcu_data(); | |
5161f31e MD |
645 | __cds_wfcq_splice_blocking(&default_call_rcu_data->cbs_head, |
646 | &default_call_rcu_data->cbs_tail, | |
647 | &crdp->cbs_head, &crdp->cbs_tail); | |
7106ddf8 PM |
648 | uatomic_add(&default_call_rcu_data->qlen, |
649 | uatomic_read(&crdp->qlen)); | |
1e92aa15 | 650 | wake_call_rcu_thread(default_call_rcu_data); |
7106ddf8 | 651 | } |
59dc9e9d | 652 | |
b75dffe6 | 653 | call_rcu_lock(&call_rcu_mutex); |
59dc9e9d | 654 | cds_list_del(&crdp->list); |
b75dffe6 LJ |
655 | call_rcu_unlock(&call_rcu_mutex); |
656 | ||
59dc9e9d | 657 | free(crdp); |
7106ddf8 PM |
658 | } |
659 | ||
660 | /* | |
661 | * Clean up all the per-CPU call_rcu threads. | |
662 | */ | |
663 | void free_all_cpu_call_rcu_data(void) | |
664 | { | |
665 | int cpu; | |
618b2595 MD |
666 | struct call_rcu_data **crdp; |
667 | static int warned = 0; | |
7106ddf8 PM |
668 | |
669 | if (maxcpus <= 0) | |
670 | return; | |
618b2595 MD |
671 | |
672 | crdp = malloc(sizeof(*crdp) * maxcpus); | |
673 | if (!crdp) { | |
674 | if (!warned) { | |
675 | fprintf(stderr, "[error] liburcu: unable to allocate per-CPU pointer array\n"); | |
676 | } | |
677 | warned = 1; | |
d31150b4 | 678 | return; |
618b2595 MD |
679 | } |
680 | ||
7106ddf8 | 681 | for (cpu = 0; cpu < maxcpus; cpu++) { |
618b2595 MD |
682 | crdp[cpu] = get_cpu_call_rcu_data(cpu); |
683 | if (crdp[cpu] == NULL) | |
7106ddf8 PM |
684 | continue; |
685 | set_cpu_call_rcu_data(cpu, NULL); | |
7106ddf8 | 686 | } |
618b2595 MD |
687 | /* |
688 | * Wait for call_rcu sites acting as RCU readers of the | |
689 | * call_rcu_data to become quiescent. | |
690 | */ | |
691 | synchronize_rcu(); | |
692 | for (cpu = 0; cpu < maxcpus; cpu++) { | |
693 | if (crdp[cpu] == NULL) | |
694 | continue; | |
695 | call_rcu_data_free(crdp[cpu]); | |
696 | } | |
697 | free(crdp); | |
7106ddf8 PM |
698 | } |
699 | ||
81ad2e19 PM |
700 | /* |
701 | * Acquire the call_rcu_mutex in order to ensure that the child sees | |
702 | * all of the call_rcu() data structures in a consistent state. | |
703 | * Suitable for pthread_atfork() and friends. | |
704 | */ | |
705 | void call_rcu_before_fork(void) | |
706 | { | |
707 | call_rcu_lock(&call_rcu_mutex); | |
708 | } | |
709 | ||
710 | /* | |
711 | * Clean up call_rcu data structures in the parent of a successful fork() | |
712 | * that is not followed by exec() in the child. Suitable for | |
713 | * pthread_atfork() and friends. | |
714 | */ | |
715 | void call_rcu_after_fork_parent(void) | |
716 | { | |
717 | call_rcu_unlock(&call_rcu_mutex); | |
718 | } | |
719 | ||
7106ddf8 PM |
720 | /* |
721 | * Clean up call_rcu data structures in the child of a successful fork() | |
81ad2e19 PM |
722 | * that is not followed by exec(). Suitable for pthread_atfork() and |
723 | * friends. | |
7106ddf8 PM |
724 | */ |
725 | void call_rcu_after_fork_child(void) | |
726 | { | |
077ff173 | 727 | struct call_rcu_data *crdp, *next; |
7106ddf8 | 728 | |
81ad2e19 PM |
729 | /* Release the mutex. */ |
730 | call_rcu_unlock(&call_rcu_mutex); | |
731 | ||
ad1b9909 LJ |
732 | /* Do nothing when call_rcu() has not been used */ |
733 | if (cds_list_empty(&call_rcu_data_list)) | |
734 | return; | |
735 | ||
7106ddf8 PM |
736 | /* |
737 | * Allocate a new default call_rcu_data structure in order | |
738 | * to get a working call_rcu thread to go with it. | |
739 | */ | |
740 | default_call_rcu_data = NULL; | |
741 | (void)get_default_call_rcu_data(); | |
742 | ||
60af049d LJ |
743 | /* Cleanup call_rcu_data pointers before use */ |
744 | maxcpus_reset(); | |
745 | free(per_cpu_call_rcu_data); | |
618b2595 | 746 | rcu_set_pointer(&per_cpu_call_rcu_data, NULL); |
bd252a04 | 747 | URCU_TLS(thread_call_rcu_data) = NULL; |
60af049d | 748 | |
7106ddf8 | 749 | /* Dispose of all of the rest of the call_rcu_data structures. */ |
077ff173 | 750 | cds_list_for_each_entry_safe(crdp, next, &call_rcu_data_list, list) { |
7106ddf8 | 751 | if (crdp == default_call_rcu_data) |
077ff173 | 752 | continue; |
2870aa1e | 753 | uatomic_set(&crdp->flags, URCU_CALL_RCU_STOPPED); |
7106ddf8 | 754 | call_rcu_data_free(crdp); |
b57aee66 PM |
755 | } |
756 | } |