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 | ||
23 | #include <stdio.h> | |
24 | #include <pthread.h> | |
25 | #include <signal.h> | |
26 | #include <assert.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #include <errno.h> | |
30 | #include <poll.h> | |
31 | #include <sys/time.h> | |
32 | #include <syscall.h> | |
33 | #include <unistd.h> | |
34 | ||
35 | #include "config.h" | |
36 | #include "urcu/wfqueue.h" | |
37 | #include "urcu-call-rcu.h" | |
38 | #include "urcu-pointer.h" | |
39 | ||
40 | /* Data structure that identifies a call_rcu thread. */ | |
41 | ||
42 | struct call_rcu_data { | |
43 | struct cds_wfq_queue cbs; | |
44 | unsigned long flags; | |
45 | pthread_mutex_t mtx; | |
46 | pthread_cond_t cond; | |
47 | unsigned long qlen; | |
48 | pthread_t tid; | |
49 | } __attribute__((aligned(CAA_CACHE_LINE_SIZE))); | |
50 | ||
51 | /* Link a thread using call_rcu() to its call_rcu thread. */ | |
52 | ||
53 | static __thread struct call_rcu_data *thread_call_rcu_data; | |
54 | ||
55 | /* Guard call_rcu thread creation. */ | |
56 | ||
57 | static pthread_mutex_t call_rcu_mutex = PTHREAD_MUTEX_INITIALIZER; | |
58 | ||
59 | /* If a given thread does not have its own call_rcu thread, this is default. */ | |
60 | ||
61 | static struct call_rcu_data *default_call_rcu_data; | |
62 | ||
63 | extern void synchronize_rcu(void); | |
64 | ||
65 | /* | |
66 | * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are | |
67 | * available, then we can have call_rcu threads assigned to individual | |
68 | * CPUs rather than only to specific threads. | |
69 | */ | |
70 | ||
71 | #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) | |
72 | ||
73 | /* | |
74 | * Pointer to array of pointers to per-CPU call_rcu_data structures | |
75 | * and # CPUs. | |
76 | */ | |
77 | ||
78 | static struct call_rcu_data **per_cpu_call_rcu_data; | |
79 | static long maxcpus; | |
80 | ||
81 | /* Allocate the array if it has not already been allocated. */ | |
82 | ||
83 | static void alloc_cpu_call_rcu_data(void) | |
84 | { | |
85 | struct call_rcu_data **p; | |
86 | static int warned = 0; | |
87 | ||
88 | if (maxcpus != 0) | |
89 | return; | |
90 | maxcpus = sysconf(_SC_NPROCESSORS_CONF); | |
91 | if (maxcpus <= 0) { | |
92 | return; | |
93 | } | |
94 | p = malloc(maxcpus * sizeof(*per_cpu_call_rcu_data)); | |
95 | if (p != NULL) { | |
96 | memset(p, '\0', maxcpus * sizeof(*per_cpu_call_rcu_data)); | |
97 | per_cpu_call_rcu_data = p; | |
98 | } else { | |
99 | if (!warned) { | |
100 | fprintf(stderr, "[error] liburcu: unable to allocate per-CPU pointer array\n"); | |
101 | } | |
102 | warned = 1; | |
103 | } | |
104 | } | |
105 | ||
106 | #else /* #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */ | |
107 | ||
108 | static const struct call_rcu_data **per_cpu_call_rcu_data = NULL; | |
109 | static const long maxcpus = -1; | |
110 | ||
111 | static void alloc_cpu_call_rcu_data(void) | |
112 | { | |
113 | } | |
114 | ||
115 | static int sched_getcpu(void) | |
116 | { | |
117 | return -1; | |
118 | } | |
119 | ||
120 | #endif /* #else #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */ | |
121 | ||
122 | /* Acquire the specified pthread mutex. */ | |
123 | ||
124 | static void call_rcu_lock(pthread_mutex_t *pmp) | |
125 | { | |
126 | if (pthread_mutex_lock(pmp) != 0) { | |
127 | perror("pthread_mutex_lock"); | |
128 | exit(-1); | |
129 | } | |
130 | } | |
131 | ||
132 | /* Release the specified pthread mutex. */ | |
133 | ||
134 | static void call_rcu_unlock(pthread_mutex_t *pmp) | |
135 | { | |
136 | if (pthread_mutex_unlock(pmp) != 0) { | |
137 | perror("pthread_mutex_unlock"); | |
138 | exit(-1); | |
139 | } | |
140 | } | |
141 | ||
142 | /* This is the code run by each call_rcu thread. */ | |
143 | ||
144 | static void *call_rcu_thread(void *arg) | |
145 | { | |
146 | unsigned long cbcount; | |
147 | struct cds_wfq_node *cbs; | |
148 | struct cds_wfq_node **cbs_tail; | |
149 | struct call_rcu_data *crdp = (struct call_rcu_data *)arg; | |
150 | struct rcu_head *rhp; | |
151 | ||
152 | thread_call_rcu_data = crdp; | |
153 | for (;;) { | |
154 | if (&crdp->cbs.head != _CMM_LOAD_SHARED(crdp->cbs.tail)) { | |
155 | while ((cbs = _CMM_LOAD_SHARED(crdp->cbs.head)) == NULL) | |
156 | poll(NULL, 0, 1); | |
157 | _CMM_STORE_SHARED(crdp->cbs.head, NULL); | |
158 | cbs_tail = (struct cds_wfq_node **) | |
159 | uatomic_xchg(&crdp->cbs.tail, &crdp->cbs.head); | |
160 | synchronize_rcu(); | |
161 | cbcount = 0; | |
162 | do { | |
163 | while (cbs->next == NULL && | |
164 | &cbs->next != cbs_tail) | |
165 | poll(NULL, 0, 1); | |
166 | if (cbs == &crdp->cbs.dummy) { | |
167 | cbs = cbs->next; | |
168 | continue; | |
169 | } | |
170 | rhp = (struct rcu_head *)cbs; | |
171 | cbs = cbs->next; | |
172 | rhp->func(rhp); | |
173 | cbcount++; | |
174 | } while (cbs != NULL); | |
175 | uatomic_sub(&crdp->qlen, cbcount); | |
176 | } | |
177 | if (crdp->flags & URCU_CALL_RCU_RT) | |
178 | poll(NULL, 0, 10); | |
179 | else { | |
180 | call_rcu_lock(&crdp->mtx); | |
181 | _CMM_STORE_SHARED(crdp->flags, | |
182 | crdp->flags & ~URCU_CALL_RCU_RUNNING); | |
183 | if (&crdp->cbs.head == | |
184 | _CMM_LOAD_SHARED(crdp->cbs.tail) && | |
185 | pthread_cond_wait(&crdp->cond, &crdp->mtx) != 0) { | |
186 | perror("pthread_cond_wait"); | |
187 | exit(-1); | |
188 | } | |
189 | _CMM_STORE_SHARED(crdp->flags, | |
190 | crdp->flags | URCU_CALL_RCU_RUNNING); | |
191 | poll(NULL, 0, 10); | |
192 | call_rcu_unlock(&crdp->mtx); | |
193 | } | |
194 | } | |
195 | return NULL; /* NOTREACHED */ | |
196 | } | |
197 | ||
198 | /* | |
199 | * Create both a call_rcu thread and the corresponding call_rcu_data | |
200 | * structure, linking the structure in as specified. | |
201 | */ | |
202 | ||
203 | void call_rcu_data_init(struct call_rcu_data **crdpp, unsigned long flags) | |
204 | { | |
205 | struct call_rcu_data *crdp; | |
206 | ||
207 | crdp = malloc(sizeof(*crdp)); | |
208 | if (crdp == NULL) { | |
209 | fprintf(stderr, "Out of memory.\n"); | |
210 | exit(-1); | |
211 | } | |
212 | memset(crdp, '\0', sizeof(*crdp)); | |
213 | cds_wfq_init(&crdp->cbs); | |
214 | crdp->qlen = 0; | |
215 | if (pthread_mutex_init(&crdp->mtx, NULL) != 0) { | |
216 | perror("pthread_mutex_init"); | |
217 | exit(-1); | |
218 | } | |
219 | if (pthread_cond_init(&crdp->cond, NULL) != 0) { | |
220 | perror("pthread_cond_init"); | |
221 | exit(-1); | |
222 | } | |
223 | crdp->flags = flags | URCU_CALL_RCU_RUNNING; | |
224 | cmm_smp_mb(); /* Structure initialized before pointer is planted. */ | |
225 | *crdpp = crdp; | |
226 | if (pthread_create(&crdp->tid, NULL, call_rcu_thread, crdp) != 0) { | |
227 | perror("pthread_create"); | |
228 | exit(-1); | |
229 | } | |
230 | } | |
231 | ||
232 | /* | |
233 | * Return a pointer to the call_rcu_data structure for the specified | |
234 | * CPU, returning NULL if there is none. We cannot automatically | |
235 | * created it because the platform we are running on might not define | |
236 | * sched_getcpu(). | |
237 | */ | |
238 | ||
239 | struct call_rcu_data *get_cpu_call_rcu_data(int cpu) | |
240 | { | |
241 | static int warned = 0; | |
242 | ||
243 | if (per_cpu_call_rcu_data == NULL) | |
244 | return NULL; | |
245 | if (!warned && maxcpus > 0 && (cpu < 0 || maxcpus <= cpu)) { | |
246 | fprintf(stderr, "[error] liburcu: get CPU # out of range\n"); | |
247 | warned = 1; | |
248 | } | |
249 | if (cpu < 0 || maxcpus <= cpu) | |
250 | return NULL; | |
251 | return per_cpu_call_rcu_data[cpu]; | |
252 | } | |
253 | ||
254 | /* | |
255 | * Return the tid corresponding to the call_rcu thread whose | |
256 | * call_rcu_data structure is specified. | |
257 | */ | |
258 | ||
259 | pthread_t get_call_rcu_thread(struct call_rcu_data *crdp) | |
260 | { | |
261 | return crdp->tid; | |
262 | } | |
263 | ||
264 | /* | |
265 | * Create a call_rcu_data structure (with thread) and return a pointer. | |
266 | */ | |
267 | ||
268 | struct call_rcu_data *create_call_rcu_data(unsigned long flags) | |
269 | { | |
270 | struct call_rcu_data *crdp; | |
271 | ||
272 | call_rcu_data_init(&crdp, flags); | |
273 | return crdp; | |
274 | } | |
275 | ||
276 | /* | |
277 | * Set the specified CPU to use the specified call_rcu_data structure. | |
278 | */ | |
279 | ||
280 | int set_cpu_call_rcu_data(int cpu, struct call_rcu_data *crdp) | |
281 | { | |
282 | int warned = 0; | |
283 | ||
284 | call_rcu_lock(&call_rcu_mutex); | |
285 | if (cpu < 0 || maxcpus <= cpu) { | |
286 | if (!warned) { | |
287 | fprintf(stderr, "[error] liburcu: set CPU # out of range\n"); | |
288 | warned = 1; | |
289 | } | |
290 | call_rcu_unlock(&call_rcu_mutex); | |
291 | errno = EINVAL; | |
292 | return -EINVAL; | |
293 | } | |
294 | alloc_cpu_call_rcu_data(); | |
295 | call_rcu_unlock(&call_rcu_mutex); | |
296 | if (per_cpu_call_rcu_data == NULL) { | |
297 | errno = ENOMEM; | |
298 | return -ENOMEM; | |
299 | } | |
300 | per_cpu_call_rcu_data[cpu] = crdp; | |
301 | return 0; | |
302 | } | |
303 | ||
304 | /* | |
305 | * Return a pointer to the default call_rcu_data structure, creating | |
306 | * one if need be. Because we never free call_rcu_data structures, | |
307 | * we don't need to be in an RCU read-side critical section. | |
308 | */ | |
309 | ||
310 | struct call_rcu_data *get_default_call_rcu_data(void) | |
311 | { | |
312 | if (default_call_rcu_data != NULL) | |
313 | return rcu_dereference(default_call_rcu_data); | |
314 | call_rcu_lock(&call_rcu_mutex); | |
315 | if (default_call_rcu_data != NULL) { | |
316 | call_rcu_unlock(&call_rcu_mutex); | |
317 | return default_call_rcu_data; | |
318 | } | |
319 | call_rcu_data_init(&default_call_rcu_data, 0); | |
320 | call_rcu_unlock(&call_rcu_mutex); | |
321 | return default_call_rcu_data; | |
322 | } | |
323 | ||
324 | /* | |
325 | * Return the call_rcu_data structure that applies to the currently | |
326 | * running thread. Any call_rcu_data structure assigned specifically | |
327 | * to this thread has first priority, followed by any call_rcu_data | |
328 | * structure assigned to the CPU on which the thread is running, | |
329 | * followed by the default call_rcu_data structure. If there is not | |
330 | * yet a default call_rcu_data structure, one will be created. | |
331 | */ | |
332 | struct call_rcu_data *get_call_rcu_data(void) | |
333 | { | |
334 | int curcpu; | |
335 | static int warned = 0; | |
336 | ||
337 | if (thread_call_rcu_data != NULL) | |
338 | return thread_call_rcu_data; | |
339 | if (maxcpus <= 0) | |
340 | return get_default_call_rcu_data(); | |
341 | curcpu = sched_getcpu(); | |
342 | if (!warned && (curcpu < 0 || maxcpus <= curcpu)) { | |
343 | fprintf(stderr, "[error] liburcu: gcrd CPU # out of range\n"); | |
344 | warned = 1; | |
345 | } | |
346 | if (curcpu >= 0 && maxcpus > curcpu && | |
347 | per_cpu_call_rcu_data != NULL && | |
348 | per_cpu_call_rcu_data[curcpu] != NULL) | |
349 | return per_cpu_call_rcu_data[curcpu]; | |
350 | return get_default_call_rcu_data(); | |
351 | } | |
352 | ||
353 | /* | |
354 | * Return a pointer to this task's call_rcu_data if there is one. | |
355 | */ | |
356 | ||
357 | struct call_rcu_data *get_thread_call_rcu_data(void) | |
358 | { | |
359 | return thread_call_rcu_data; | |
360 | } | |
361 | ||
362 | /* | |
363 | * Set this task's call_rcu_data structure as specified, regardless | |
364 | * of whether or not this task already had one. (This allows switching | |
365 | * to and from real-time call_rcu threads, for example.) | |
366 | */ | |
367 | ||
368 | void set_thread_call_rcu_data(struct call_rcu_data *crdp) | |
369 | { | |
370 | thread_call_rcu_data = crdp; | |
371 | } | |
372 | ||
373 | /* | |
374 | * Create a separate call_rcu thread for each CPU. This does not | |
375 | * replace a pre-existing call_rcu thread -- use the set_cpu_call_rcu_data() | |
376 | * function if you want that behavior. | |
377 | */ | |
378 | ||
379 | int create_all_cpu_call_rcu_data(unsigned long flags) | |
380 | { | |
381 | int i; | |
382 | struct call_rcu_data *crdp; | |
383 | int ret; | |
384 | ||
385 | call_rcu_lock(&call_rcu_mutex); | |
386 | alloc_cpu_call_rcu_data(); | |
387 | call_rcu_unlock(&call_rcu_mutex); | |
388 | if (maxcpus <= 0) { | |
389 | errno = EINVAL; | |
390 | return -EINVAL; | |
391 | } | |
392 | if (per_cpu_call_rcu_data == NULL) { | |
393 | errno = ENOMEM; | |
394 | return -ENOMEM; | |
395 | } | |
396 | for (i = 0; i < maxcpus; i++) { | |
397 | call_rcu_lock(&call_rcu_mutex); | |
398 | if (get_cpu_call_rcu_data(i)) { | |
399 | call_rcu_unlock(&call_rcu_mutex); | |
400 | continue; | |
401 | } | |
402 | crdp = create_call_rcu_data(flags); | |
403 | if (crdp == NULL) { | |
404 | call_rcu_unlock(&call_rcu_mutex); | |
405 | errno = ENOMEM; | |
406 | return -ENOMEM; | |
407 | } | |
408 | call_rcu_unlock(&call_rcu_mutex); | |
409 | if ((ret = set_cpu_call_rcu_data(i, crdp)) != 0) { | |
410 | /* FIXME: Leaks crdp for now. */ | |
411 | return ret; /* Can happen on race. */ | |
412 | } | |
413 | } | |
414 | return 0; | |
415 | } | |
416 | ||
417 | /* | |
418 | * Schedule a function to be invoked after a following grace period. | |
419 | * This is the only function that must be called -- the others are | |
420 | * only present to allow applications to tune their use of RCU for | |
421 | * maximum performance. | |
422 | * | |
423 | * Note that unless a call_rcu thread has not already been created, | |
424 | * the first invocation of call_rcu() will create one. So, if you | |
425 | * need the first invocation of call_rcu() to be fast, make sure | |
426 | * to create a call_rcu thread first. One way to accomplish this is | |
427 | * "get_call_rcu_data();", and another is create_all_cpu_call_rcu_data(). | |
428 | */ | |
429 | ||
430 | void call_rcu(struct rcu_head *head, | |
431 | void (*func)(struct rcu_head *head)) | |
432 | { | |
433 | struct call_rcu_data *crdp; | |
434 | ||
435 | cds_wfq_node_init(&head->next); | |
436 | head->func = func; | |
437 | crdp = get_call_rcu_data(); | |
438 | cds_wfq_enqueue(&crdp->cbs, &head->next); | |
439 | uatomic_inc(&crdp->qlen); | |
440 | if (!(_CMM_LOAD_SHARED(crdp->flags) & URCU_CALL_RCU_RT)) { | |
441 | call_rcu_lock(&crdp->mtx); | |
442 | if (!(_CMM_LOAD_SHARED(crdp->flags) & URCU_CALL_RCU_RUNNING)) { | |
443 | if (pthread_cond_signal(&crdp->cond) != 0) { | |
444 | perror("pthread_cond_signal"); | |
445 | exit(-1); | |
446 | } | |
447 | } | |
448 | call_rcu_unlock(&crdp->mtx); | |
449 | } | |
450 | } |