linux_rcu.c revision 1.7 1 1.7 riastrad /* $NetBSD: linux_rcu.c,v 1.7 2021/12/19 12:40:11 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #include <sys/cdefs.h>
33 1.7 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_rcu.c,v 1.7 2021/12/19 12:40:11 riastradh Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/param.h>
36 1.1 riastrad #include <sys/types.h>
37 1.5 riastrad
38 1.1 riastrad #include <sys/condvar.h>
39 1.1 riastrad #include <sys/cpu.h>
40 1.1 riastrad #include <sys/kthread.h>
41 1.5 riastrad #include <sys/lockdebug.h>
42 1.1 riastrad #include <sys/mutex.h>
43 1.1 riastrad #include <sys/sdt.h>
44 1.1 riastrad #include <sys/xcall.h>
45 1.1 riastrad
46 1.1 riastrad #include <linux/rcupdate.h>
47 1.1 riastrad #include <linux/slab.h>
48 1.1 riastrad
49 1.1 riastrad SDT_PROBE_DEFINE0(sdt, linux, rcu, synchronize__start);
50 1.1 riastrad SDT_PROBE_DEFINE1(sdt, linux, rcu, synchronize__cpu, "unsigned"/*cpu*/);
51 1.1 riastrad SDT_PROBE_DEFINE0(sdt, linux, rcu, synchronize__done);
52 1.1 riastrad SDT_PROBE_DEFINE0(sdt, linux, rcu, barrier__start);
53 1.1 riastrad SDT_PROBE_DEFINE0(sdt, linux, rcu, barrier__done);
54 1.1 riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, call__queue,
55 1.1 riastrad "struct rcu_head *"/*head*/, "void (*)(struct rcu_head *)"/*callback*/);
56 1.1 riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, call__run,
57 1.1 riastrad "struct rcu_head *"/*head*/, "void (*)(struct rcu_head *)"/*callback*/);
58 1.1 riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, call__done,
59 1.1 riastrad "struct rcu_head *"/*head*/, "void (*)(struct rcu_head *)"/*callback*/);
60 1.1 riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, kfree__queue,
61 1.1 riastrad "struct rcu_head *"/*head*/, "void *"/*obj*/);
62 1.1 riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, kfree__free,
63 1.1 riastrad "struct rcu_head *"/*head*/, "void *"/*obj*/);
64 1.1 riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, kfree__done,
65 1.1 riastrad "struct rcu_head *"/*head*/, "void *"/*obj*/);
66 1.1 riastrad
67 1.1 riastrad static struct {
68 1.1 riastrad kmutex_t lock;
69 1.1 riastrad kcondvar_t cv;
70 1.1 riastrad struct rcu_head *first_callback;
71 1.1 riastrad struct rcu_head *first_kfree;
72 1.1 riastrad struct lwp *lwp;
73 1.1 riastrad uint64_t gen;
74 1.7 riastrad bool running;
75 1.1 riastrad bool dying;
76 1.1 riastrad } gc __cacheline_aligned;
77 1.1 riastrad
78 1.1 riastrad static void
79 1.1 riastrad synchronize_rcu_xc(void *a, void *b)
80 1.1 riastrad {
81 1.1 riastrad
82 1.1 riastrad SDT_PROBE1(sdt, linux, rcu, synchronize__cpu, cpu_index(curcpu()));
83 1.1 riastrad }
84 1.1 riastrad
85 1.1 riastrad /*
86 1.1 riastrad * synchronize_rcu()
87 1.1 riastrad *
88 1.1 riastrad * Wait for any pending RCU read section on every CPU to complete
89 1.1 riastrad * by triggering on every CPU activity that is blocked by an RCU
90 1.1 riastrad * read section.
91 1.4 riastrad *
92 1.4 riastrad * May sleep. (Practically guaranteed to sleep!)
93 1.1 riastrad */
94 1.1 riastrad void
95 1.1 riastrad synchronize_rcu(void)
96 1.1 riastrad {
97 1.1 riastrad
98 1.1 riastrad SDT_PROBE0(sdt, linux, rcu, synchronize__start);
99 1.1 riastrad xc_wait(xc_broadcast(0, &synchronize_rcu_xc, NULL, NULL));
100 1.1 riastrad SDT_PROBE0(sdt, linux, rcu, synchronize__done);
101 1.1 riastrad }
102 1.1 riastrad
103 1.1 riastrad /*
104 1.4 riastrad * synchronize_rcu_expedited()
105 1.4 riastrad *
106 1.4 riastrad * Wait for any pending RCU read section on every CPU to complete
107 1.4 riastrad * by triggering on every CPU activity that is blocked by an RCU
108 1.4 riastrad * read section. Try to get an answer faster than
109 1.4 riastrad * synchronize_rcu, at the cost of more activity triggered on
110 1.4 riastrad * other CPUs.
111 1.4 riastrad *
112 1.4 riastrad * May sleep. (Practically guaranteed to sleep!)
113 1.4 riastrad */
114 1.4 riastrad void
115 1.4 riastrad synchronize_rcu_expedited(void)
116 1.4 riastrad {
117 1.4 riastrad
118 1.4 riastrad synchronize_rcu();
119 1.4 riastrad }
120 1.4 riastrad
121 1.4 riastrad /*
122 1.3 riastrad * cookie = get_state_synchronize_rcu(), cond_synchronize_rcu(cookie)
123 1.3 riastrad *
124 1.3 riastrad * Optimization for synchronize_rcu -- skip if it has already
125 1.3 riastrad * happened between get_state_synchronize_rcu and
126 1.3 riastrad * cond_synchronize_rcu. get_state_synchronize_rcu implies a full
127 1.3 riastrad * SMP memory barrier (membar_sync).
128 1.3 riastrad */
129 1.3 riastrad unsigned long
130 1.3 riastrad get_state_synchronize_rcu(void)
131 1.3 riastrad {
132 1.3 riastrad
133 1.3 riastrad membar_sync();
134 1.3 riastrad return 0;
135 1.3 riastrad }
136 1.3 riastrad
137 1.3 riastrad void
138 1.3 riastrad cond_synchronize_rcu(unsigned long cookie)
139 1.3 riastrad {
140 1.3 riastrad
141 1.3 riastrad synchronize_rcu();
142 1.3 riastrad }
143 1.3 riastrad
144 1.3 riastrad /*
145 1.1 riastrad * rcu_barrier()
146 1.1 riastrad *
147 1.1 riastrad * Wait for all pending RCU callbacks to complete.
148 1.1 riastrad *
149 1.1 riastrad * Does not imply, and is not implied by, synchronize_rcu.
150 1.1 riastrad */
151 1.1 riastrad void
152 1.1 riastrad rcu_barrier(void)
153 1.1 riastrad {
154 1.1 riastrad uint64_t gen;
155 1.1 riastrad
156 1.7 riastrad /*
157 1.7 riastrad * If the GC isn't running anything yet, then all callbacks of
158 1.7 riastrad * interest are queued, and it suffices to wait for the GC to
159 1.7 riastrad * advance one generation number.
160 1.7 riastrad *
161 1.7 riastrad * If the GC is already running, however, and there are any
162 1.7 riastrad * callbacks of interest queued but not in the GC's current
163 1.7 riastrad * batch of work, then when the advances the generation number
164 1.7 riastrad * it will not have completed the queued callbacks. So we have
165 1.7 riastrad * to wait for one more generation -- or until the GC has
166 1.7 riastrad * stopped running because there's no work left.
167 1.7 riastrad */
168 1.7 riastrad
169 1.1 riastrad SDT_PROBE0(sdt, linux, rcu, barrier__start);
170 1.1 riastrad mutex_enter(&gc.lock);
171 1.7 riastrad gen = gc.gen;
172 1.7 riastrad if (gc.running)
173 1.7 riastrad gen++;
174 1.7 riastrad while (gc.running || gc.first_callback || gc.first_kfree) {
175 1.7 riastrad cv_wait(&gc.cv, &gc.lock);
176 1.7 riastrad if (gc.gen > gen)
177 1.7 riastrad break;
178 1.1 riastrad }
179 1.1 riastrad mutex_exit(&gc.lock);
180 1.1 riastrad SDT_PROBE0(sdt, linux, rcu, barrier__done);
181 1.1 riastrad }
182 1.1 riastrad
183 1.1 riastrad /*
184 1.1 riastrad * call_rcu(head, callback)
185 1.1 riastrad *
186 1.1 riastrad * Arrange to call callback(head) after any pending RCU read
187 1.1 riastrad * sections on every CPU is complete. Return immediately.
188 1.1 riastrad */
189 1.1 riastrad void
190 1.1 riastrad call_rcu(struct rcu_head *head, void (*callback)(struct rcu_head *))
191 1.1 riastrad {
192 1.1 riastrad
193 1.1 riastrad head->rcuh_u.callback = callback;
194 1.1 riastrad
195 1.1 riastrad mutex_enter(&gc.lock);
196 1.1 riastrad head->rcuh_next = gc.first_callback;
197 1.1 riastrad gc.first_callback = head;
198 1.1 riastrad cv_broadcast(&gc.cv);
199 1.1 riastrad SDT_PROBE2(sdt, linux, rcu, call__queue, head, callback);
200 1.1 riastrad mutex_exit(&gc.lock);
201 1.1 riastrad }
202 1.1 riastrad
203 1.1 riastrad /*
204 1.1 riastrad * _kfree_rcu(head, obj)
205 1.1 riastrad *
206 1.1 riastrad * kfree_rcu helper: schedule kfree(obj) using head for storage.
207 1.1 riastrad */
208 1.1 riastrad void
209 1.1 riastrad _kfree_rcu(struct rcu_head *head, void *obj)
210 1.1 riastrad {
211 1.1 riastrad
212 1.5 riastrad LOCKDEBUG_MEM_CHECK(obj, ((struct linux_malloc *)obj - 1)->lm_size);
213 1.5 riastrad
214 1.1 riastrad head->rcuh_u.obj = obj;
215 1.1 riastrad
216 1.1 riastrad mutex_enter(&gc.lock);
217 1.1 riastrad head->rcuh_next = gc.first_kfree;
218 1.1 riastrad gc.first_kfree = head;
219 1.1 riastrad cv_broadcast(&gc.cv);
220 1.1 riastrad SDT_PROBE2(sdt, linux, rcu, kfree__queue, head, obj);
221 1.1 riastrad mutex_exit(&gc.lock);
222 1.1 riastrad }
223 1.1 riastrad
224 1.1 riastrad static void
225 1.1 riastrad gc_thread(void *cookie)
226 1.1 riastrad {
227 1.1 riastrad struct rcu_head *head_callback, *head_kfree, *head, *next;
228 1.1 riastrad
229 1.1 riastrad mutex_enter(&gc.lock);
230 1.1 riastrad for (;;) {
231 1.1 riastrad /* Start with no work. */
232 1.1 riastrad bool work = false;
233 1.1 riastrad
234 1.1 riastrad /* Grab the list of callbacks. */
235 1.1 riastrad if ((head_callback = gc.first_callback) != NULL) {
236 1.1 riastrad gc.first_callback = NULL;
237 1.1 riastrad work = true;
238 1.1 riastrad }
239 1.1 riastrad
240 1.1 riastrad /* Grab the list of objects to kfree. */
241 1.1 riastrad if ((head_kfree = gc.first_kfree) != NULL) {
242 1.1 riastrad gc.first_kfree = NULL;
243 1.1 riastrad work = true;
244 1.1 riastrad }
245 1.1 riastrad
246 1.1 riastrad /*
247 1.1 riastrad * If no work, then either stop, if we're dying, or
248 1.1 riastrad * wait for work, if not.
249 1.1 riastrad */
250 1.1 riastrad if (!work) {
251 1.1 riastrad if (gc.dying)
252 1.1 riastrad break;
253 1.1 riastrad cv_wait(&gc.cv, &gc.lock);
254 1.1 riastrad continue;
255 1.1 riastrad }
256 1.1 riastrad
257 1.7 riastrad /*
258 1.7 riastrad * We have work to do. Drop the lock to do it, and
259 1.7 riastrad * notify rcu_barrier that we're still doing it.
260 1.7 riastrad */
261 1.7 riastrad gc.running = true;
262 1.1 riastrad mutex_exit(&gc.lock);
263 1.1 riastrad
264 1.1 riastrad /* Wait for activity on all CPUs. */
265 1.1 riastrad synchronize_rcu();
266 1.1 riastrad
267 1.1 riastrad /* Call the callbacks. */
268 1.1 riastrad for (head = head_callback; head != NULL; head = next) {
269 1.1 riastrad void (*callback)(struct rcu_head *) =
270 1.1 riastrad head->rcuh_u.callback;
271 1.1 riastrad next = head->rcuh_next;
272 1.1 riastrad SDT_PROBE2(sdt, linux, rcu, call__run,
273 1.1 riastrad head, callback);
274 1.1 riastrad (*callback)(head);
275 1.1 riastrad /*
276 1.1 riastrad * Can't dereference head or invoke
277 1.1 riastrad * callback after this point.
278 1.1 riastrad */
279 1.1 riastrad SDT_PROBE2(sdt, linux, rcu, call__done,
280 1.1 riastrad head, callback);
281 1.1 riastrad }
282 1.1 riastrad
283 1.1 riastrad /* Free the objects to kfree. */
284 1.1 riastrad for (head = head_kfree; head != NULL; head = next) {
285 1.1 riastrad void *obj = head->rcuh_u.obj;
286 1.1 riastrad next = head->rcuh_next;
287 1.1 riastrad SDT_PROBE2(sdt, linux, rcu, kfree__free, head, obj);
288 1.1 riastrad kfree(obj);
289 1.1 riastrad /* Can't dereference head or obj after this point. */
290 1.1 riastrad SDT_PROBE2(sdt, linux, rcu, kfree__done, head, obj);
291 1.1 riastrad }
292 1.1 riastrad
293 1.1 riastrad /* Return to the lock. */
294 1.1 riastrad mutex_enter(&gc.lock);
295 1.1 riastrad
296 1.1 riastrad /* Finished a batch of work. Notify rcu_barrier. */
297 1.1 riastrad gc.gen++;
298 1.7 riastrad gc.running = false;
299 1.1 riastrad cv_broadcast(&gc.cv);
300 1.6 riastrad
301 1.6 riastrad /*
302 1.6 riastrad * Limit ourselves to one batch per tick, in an attempt
303 1.6 riastrad * to make the batches larger.
304 1.6 riastrad *
305 1.6 riastrad * XXX We should maybe also limit the size of each
306 1.6 riastrad * batch.
307 1.6 riastrad */
308 1.6 riastrad (void)kpause("lxrcubat", /*intr*/false, /*timo*/1, &gc.lock);
309 1.1 riastrad }
310 1.1 riastrad KASSERT(gc.first_callback == NULL);
311 1.1 riastrad KASSERT(gc.first_kfree == NULL);
312 1.1 riastrad mutex_exit(&gc.lock);
313 1.1 riastrad
314 1.1 riastrad kthread_exit(0);
315 1.1 riastrad }
316 1.1 riastrad
317 1.2 riastrad void
318 1.2 riastrad init_rcu_head(struct rcu_head *head)
319 1.2 riastrad {
320 1.2 riastrad }
321 1.2 riastrad
322 1.2 riastrad void
323 1.2 riastrad destroy_rcu_head(struct rcu_head *head)
324 1.2 riastrad {
325 1.2 riastrad }
326 1.2 riastrad
327 1.1 riastrad int
328 1.1 riastrad linux_rcu_gc_init(void)
329 1.1 riastrad {
330 1.1 riastrad int error;
331 1.1 riastrad
332 1.1 riastrad mutex_init(&gc.lock, MUTEX_DEFAULT, IPL_VM);
333 1.1 riastrad cv_init(&gc.cv, "lnxrcugc");
334 1.1 riastrad gc.first_callback = NULL;
335 1.1 riastrad gc.first_kfree = NULL;
336 1.1 riastrad gc.gen = 0;
337 1.1 riastrad gc.dying = false;
338 1.1 riastrad
339 1.1 riastrad error = kthread_create(PRI_NONE,
340 1.1 riastrad KTHREAD_MPSAFE|KTHREAD_TS|KTHREAD_MUSTJOIN, NULL, &gc_thread, NULL,
341 1.1 riastrad &gc.lwp, "lnxrcugc");
342 1.1 riastrad if (error)
343 1.1 riastrad goto fail;
344 1.1 riastrad
345 1.1 riastrad /* Success! */
346 1.1 riastrad return 0;
347 1.1 riastrad
348 1.1 riastrad fail: cv_destroy(&gc.cv);
349 1.1 riastrad mutex_destroy(&gc.lock);
350 1.1 riastrad return error;
351 1.1 riastrad }
352 1.1 riastrad
353 1.1 riastrad void
354 1.1 riastrad linux_rcu_gc_fini(void)
355 1.1 riastrad {
356 1.1 riastrad
357 1.1 riastrad mutex_enter(&gc.lock);
358 1.1 riastrad gc.dying = true;
359 1.1 riastrad cv_broadcast(&gc.cv);
360 1.1 riastrad mutex_exit(&gc.lock);
361 1.1 riastrad
362 1.1 riastrad kthread_join(gc.lwp);
363 1.1 riastrad gc.lwp = NULL;
364 1.1 riastrad KASSERT(gc.first_callback == NULL);
365 1.1 riastrad KASSERT(gc.first_kfree == NULL);
366 1.1 riastrad cv_destroy(&gc.cv);
367 1.1 riastrad mutex_destroy(&gc.lock);
368 1.1 riastrad }
369