linux_rcu.c revision 1.1 1 1.1 riastrad /* $NetBSD: linux_rcu.c,v 1.1 2021/12/19 01:33:17 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.1 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_rcu.c,v 1.1 2021/12/19 01:33:17 riastradh Exp $");
34 1.1 riastrad
35 1.1 riastrad #include <sys/param.h>
36 1.1 riastrad #include <sys/types.h>
37 1.1 riastrad #include <sys/condvar.h>
38 1.1 riastrad #include <sys/cpu.h>
39 1.1 riastrad #include <sys/kthread.h>
40 1.1 riastrad #include <sys/mutex.h>
41 1.1 riastrad #include <sys/sdt.h>
42 1.1 riastrad #include <sys/xcall.h>
43 1.1 riastrad
44 1.1 riastrad #include <linux/rcupdate.h>
45 1.1 riastrad #include <linux/slab.h>
46 1.1 riastrad
47 1.1 riastrad SDT_PROBE_DEFINE0(sdt, linux, rcu, synchronize__start);
48 1.1 riastrad SDT_PROBE_DEFINE1(sdt, linux, rcu, synchronize__cpu, "unsigned"/*cpu*/);
49 1.1 riastrad SDT_PROBE_DEFINE0(sdt, linux, rcu, synchronize__done);
50 1.1 riastrad SDT_PROBE_DEFINE0(sdt, linux, rcu, barrier__start);
51 1.1 riastrad SDT_PROBE_DEFINE0(sdt, linux, rcu, barrier__done);
52 1.1 riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, call__queue,
53 1.1 riastrad "struct rcu_head *"/*head*/, "void (*)(struct rcu_head *)"/*callback*/);
54 1.1 riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, call__run,
55 1.1 riastrad "struct rcu_head *"/*head*/, "void (*)(struct rcu_head *)"/*callback*/);
56 1.1 riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, call__done,
57 1.1 riastrad "struct rcu_head *"/*head*/, "void (*)(struct rcu_head *)"/*callback*/);
58 1.1 riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, kfree__queue,
59 1.1 riastrad "struct rcu_head *"/*head*/, "void *"/*obj*/);
60 1.1 riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, kfree__free,
61 1.1 riastrad "struct rcu_head *"/*head*/, "void *"/*obj*/);
62 1.1 riastrad SDT_PROBE_DEFINE2(sdt, linux, rcu, kfree__done,
63 1.1 riastrad "struct rcu_head *"/*head*/, "void *"/*obj*/);
64 1.1 riastrad
65 1.1 riastrad static struct {
66 1.1 riastrad kmutex_t lock;
67 1.1 riastrad kcondvar_t cv;
68 1.1 riastrad struct rcu_head *first_callback;
69 1.1 riastrad struct rcu_head *first_kfree;
70 1.1 riastrad struct lwp *lwp;
71 1.1 riastrad uint64_t gen;
72 1.1 riastrad bool dying;
73 1.1 riastrad } gc __cacheline_aligned;
74 1.1 riastrad
75 1.1 riastrad static void
76 1.1 riastrad synchronize_rcu_xc(void *a, void *b)
77 1.1 riastrad {
78 1.1 riastrad
79 1.1 riastrad SDT_PROBE1(sdt, linux, rcu, synchronize__cpu, cpu_index(curcpu()));
80 1.1 riastrad }
81 1.1 riastrad
82 1.1 riastrad /*
83 1.1 riastrad * synchronize_rcu()
84 1.1 riastrad *
85 1.1 riastrad * Wait for any pending RCU read section on every CPU to complete
86 1.1 riastrad * by triggering on every CPU activity that is blocked by an RCU
87 1.1 riastrad * read section.
88 1.1 riastrad */
89 1.1 riastrad void
90 1.1 riastrad synchronize_rcu(void)
91 1.1 riastrad {
92 1.1 riastrad
93 1.1 riastrad SDT_PROBE0(sdt, linux, rcu, synchronize__start);
94 1.1 riastrad xc_wait(xc_broadcast(0, &synchronize_rcu_xc, NULL, NULL));
95 1.1 riastrad SDT_PROBE0(sdt, linux, rcu, synchronize__done);
96 1.1 riastrad }
97 1.1 riastrad
98 1.1 riastrad /*
99 1.1 riastrad * rcu_barrier()
100 1.1 riastrad *
101 1.1 riastrad * Wait for all pending RCU callbacks to complete.
102 1.1 riastrad *
103 1.1 riastrad * Does not imply, and is not implied by, synchronize_rcu.
104 1.1 riastrad */
105 1.1 riastrad void
106 1.1 riastrad rcu_barrier(void)
107 1.1 riastrad {
108 1.1 riastrad uint64_t gen;
109 1.1 riastrad
110 1.1 riastrad SDT_PROBE0(sdt, linux, rcu, barrier__start);
111 1.1 riastrad mutex_enter(&gc.lock);
112 1.1 riastrad if (gc.first_callback != NULL || gc.first_kfree != NULL) {
113 1.1 riastrad gen = gc.gen;
114 1.1 riastrad do {
115 1.1 riastrad cv_wait(&gc.cv, &gc.lock);
116 1.1 riastrad } while (gc.gen == gen);
117 1.1 riastrad }
118 1.1 riastrad mutex_exit(&gc.lock);
119 1.1 riastrad SDT_PROBE0(sdt, linux, rcu, barrier__done);
120 1.1 riastrad }
121 1.1 riastrad
122 1.1 riastrad /*
123 1.1 riastrad * call_rcu(head, callback)
124 1.1 riastrad *
125 1.1 riastrad * Arrange to call callback(head) after any pending RCU read
126 1.1 riastrad * sections on every CPU is complete. Return immediately.
127 1.1 riastrad */
128 1.1 riastrad void
129 1.1 riastrad call_rcu(struct rcu_head *head, void (*callback)(struct rcu_head *))
130 1.1 riastrad {
131 1.1 riastrad
132 1.1 riastrad head->rcuh_u.callback = callback;
133 1.1 riastrad
134 1.1 riastrad mutex_enter(&gc.lock);
135 1.1 riastrad head->rcuh_next = gc.first_callback;
136 1.1 riastrad gc.first_callback = head;
137 1.1 riastrad cv_broadcast(&gc.cv);
138 1.1 riastrad SDT_PROBE2(sdt, linux, rcu, call__queue, head, callback);
139 1.1 riastrad mutex_exit(&gc.lock);
140 1.1 riastrad }
141 1.1 riastrad
142 1.1 riastrad /*
143 1.1 riastrad * _kfree_rcu(head, obj)
144 1.1 riastrad *
145 1.1 riastrad * kfree_rcu helper: schedule kfree(obj) using head for storage.
146 1.1 riastrad */
147 1.1 riastrad void
148 1.1 riastrad _kfree_rcu(struct rcu_head *head, void *obj)
149 1.1 riastrad {
150 1.1 riastrad
151 1.1 riastrad head->rcuh_u.obj = obj;
152 1.1 riastrad
153 1.1 riastrad mutex_enter(&gc.lock);
154 1.1 riastrad head->rcuh_next = gc.first_kfree;
155 1.1 riastrad gc.first_kfree = head;
156 1.1 riastrad cv_broadcast(&gc.cv);
157 1.1 riastrad SDT_PROBE2(sdt, linux, rcu, kfree__queue, head, obj);
158 1.1 riastrad mutex_exit(&gc.lock);
159 1.1 riastrad }
160 1.1 riastrad
161 1.1 riastrad static void
162 1.1 riastrad gc_thread(void *cookie)
163 1.1 riastrad {
164 1.1 riastrad struct rcu_head *head_callback, *head_kfree, *head, *next;
165 1.1 riastrad
166 1.1 riastrad mutex_enter(&gc.lock);
167 1.1 riastrad for (;;) {
168 1.1 riastrad /* Start with no work. */
169 1.1 riastrad bool work = false;
170 1.1 riastrad
171 1.1 riastrad /* Grab the list of callbacks. */
172 1.1 riastrad if ((head_callback = gc.first_callback) != NULL) {
173 1.1 riastrad gc.first_callback = NULL;
174 1.1 riastrad work = true;
175 1.1 riastrad }
176 1.1 riastrad
177 1.1 riastrad /* Grab the list of objects to kfree. */
178 1.1 riastrad if ((head_kfree = gc.first_kfree) != NULL) {
179 1.1 riastrad gc.first_kfree = NULL;
180 1.1 riastrad work = true;
181 1.1 riastrad }
182 1.1 riastrad
183 1.1 riastrad /*
184 1.1 riastrad * If no work, then either stop, if we're dying, or
185 1.1 riastrad * wait for work, if not.
186 1.1 riastrad */
187 1.1 riastrad if (!work) {
188 1.1 riastrad if (gc.dying)
189 1.1 riastrad break;
190 1.1 riastrad cv_wait(&gc.cv, &gc.lock);
191 1.1 riastrad continue;
192 1.1 riastrad }
193 1.1 riastrad
194 1.1 riastrad /* We have work to do. Drop the lock to do it. */
195 1.1 riastrad mutex_exit(&gc.lock);
196 1.1 riastrad
197 1.1 riastrad /* Wait for activity on all CPUs. */
198 1.1 riastrad synchronize_rcu();
199 1.1 riastrad
200 1.1 riastrad /* Call the callbacks. */
201 1.1 riastrad for (head = head_callback; head != NULL; head = next) {
202 1.1 riastrad void (*callback)(struct rcu_head *) =
203 1.1 riastrad head->rcuh_u.callback;
204 1.1 riastrad next = head->rcuh_next;
205 1.1 riastrad SDT_PROBE2(sdt, linux, rcu, call__run,
206 1.1 riastrad head, callback);
207 1.1 riastrad (*callback)(head);
208 1.1 riastrad /*
209 1.1 riastrad * Can't dereference head or invoke
210 1.1 riastrad * callback after this point.
211 1.1 riastrad */
212 1.1 riastrad SDT_PROBE2(sdt, linux, rcu, call__done,
213 1.1 riastrad head, callback);
214 1.1 riastrad }
215 1.1 riastrad
216 1.1 riastrad /* Free the objects to kfree. */
217 1.1 riastrad for (head = head_kfree; head != NULL; head = next) {
218 1.1 riastrad void *obj = head->rcuh_u.obj;
219 1.1 riastrad next = head->rcuh_next;
220 1.1 riastrad SDT_PROBE2(sdt, linux, rcu, kfree__free, head, obj);
221 1.1 riastrad kfree(obj);
222 1.1 riastrad /* Can't dereference head or obj after this point. */
223 1.1 riastrad SDT_PROBE2(sdt, linux, rcu, kfree__done, head, obj);
224 1.1 riastrad }
225 1.1 riastrad
226 1.1 riastrad /* Return to the lock. */
227 1.1 riastrad mutex_enter(&gc.lock);
228 1.1 riastrad
229 1.1 riastrad /* Finished a batch of work. Notify rcu_barrier. */
230 1.1 riastrad gc.gen++;
231 1.1 riastrad cv_broadcast(&gc.cv);
232 1.1 riastrad }
233 1.1 riastrad KASSERT(gc.first_callback == NULL);
234 1.1 riastrad KASSERT(gc.first_kfree == NULL);
235 1.1 riastrad mutex_exit(&gc.lock);
236 1.1 riastrad
237 1.1 riastrad kthread_exit(0);
238 1.1 riastrad }
239 1.1 riastrad
240 1.1 riastrad int
241 1.1 riastrad linux_rcu_gc_init(void)
242 1.1 riastrad {
243 1.1 riastrad int error;
244 1.1 riastrad
245 1.1 riastrad mutex_init(&gc.lock, MUTEX_DEFAULT, IPL_VM);
246 1.1 riastrad cv_init(&gc.cv, "lnxrcugc");
247 1.1 riastrad gc.first_callback = NULL;
248 1.1 riastrad gc.first_kfree = NULL;
249 1.1 riastrad gc.gen = 0;
250 1.1 riastrad gc.dying = false;
251 1.1 riastrad
252 1.1 riastrad error = kthread_create(PRI_NONE,
253 1.1 riastrad KTHREAD_MPSAFE|KTHREAD_TS|KTHREAD_MUSTJOIN, NULL, &gc_thread, NULL,
254 1.1 riastrad &gc.lwp, "lnxrcugc");
255 1.1 riastrad if (error)
256 1.1 riastrad goto fail;
257 1.1 riastrad
258 1.1 riastrad /* Success! */
259 1.1 riastrad return 0;
260 1.1 riastrad
261 1.1 riastrad fail: cv_destroy(&gc.cv);
262 1.1 riastrad mutex_destroy(&gc.lock);
263 1.1 riastrad return error;
264 1.1 riastrad }
265 1.1 riastrad
266 1.1 riastrad void
267 1.1 riastrad linux_rcu_gc_fini(void)
268 1.1 riastrad {
269 1.1 riastrad
270 1.1 riastrad mutex_enter(&gc.lock);
271 1.1 riastrad gc.dying = true;
272 1.1 riastrad cv_broadcast(&gc.cv);
273 1.1 riastrad mutex_exit(&gc.lock);
274 1.1 riastrad
275 1.1 riastrad kthread_join(gc.lwp);
276 1.1 riastrad gc.lwp = NULL;
277 1.1 riastrad KASSERT(gc.first_callback == NULL);
278 1.1 riastrad KASSERT(gc.first_kfree == NULL);
279 1.1 riastrad cv_destroy(&gc.cv);
280 1.1 riastrad mutex_destroy(&gc.lock);
281 1.1 riastrad }
282