2 * Copyright (c) 2016 Matthew Macy (mmacy@mattmacy.io)
3 * Copyright (c) 2017 Hans Petter Selasky (hselasky@freebsd.org)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
36 #include <sys/mutex.h>
38 #include <sys/sched.h>
40 #include <sys/queue.h>
41 #include <sys/taskqueue.h>
46 #include <linux/rcupdate.h>
47 #include <linux/srcu.h>
48 #include <linux/slab.h>
49 #include <linux/kernel.h>
50 #include <linux/compat.h>
53 * By defining CONFIG_NO_RCU_SKIP LinuxKPI RCU locks and asserts will
54 * not be skipped during panic().
56 #ifdef CONFIG_NO_RCU_SKIP
57 #define RCU_SKIP(void) 0
59 #define RCU_SKIP(void) unlikely(SCHEDULER_STOPPED() || kdb_active)
62 struct callback_head {
63 STAILQ_ENTRY(callback_head) entry;
67 struct linux_epoch_head {
68 STAILQ_HEAD(, callback_head) cb_head;
71 } __aligned(CACHE_LINE_SIZE);
73 struct linux_epoch_record {
74 ck_epoch_record_t epoch_record;
75 TAILQ_HEAD(, task_struct) ts_head;
77 } __aligned(CACHE_LINE_SIZE);
80 * Verify that "struct rcu_head" is big enough to hold "struct
81 * callback_head". This has been done to avoid having to add special
82 * compile flags for including ck_epoch.h to all clients of the
85 CTASSERT(sizeof(struct rcu_head) == sizeof(struct callback_head));
88 * Verify that "epoch_record" is at beginning of "struct
89 * linux_epoch_record":
91 CTASSERT(offsetof(struct linux_epoch_record, epoch_record) == 0);
93 static ck_epoch_t linux_epoch;
94 static struct linux_epoch_head linux_epoch_head;
95 DPCPU_DEFINE_STATIC(struct linux_epoch_record, linux_epoch_record);
97 static void linux_rcu_cleaner_func(void *, int);
100 linux_rcu_runtime_init(void *arg __unused)
102 struct linux_epoch_head *head;
105 ck_epoch_init(&linux_epoch);
107 head = &linux_epoch_head;
109 mtx_init(&head->lock, "LRCU-HEAD", NULL, MTX_DEF);
110 TASK_INIT(&head->task, 0, linux_rcu_cleaner_func, NULL);
111 STAILQ_INIT(&head->cb_head);
114 struct linux_epoch_record *record;
116 record = &DPCPU_ID_GET(i, linux_epoch_record);
119 ck_epoch_register(&linux_epoch, &record->epoch_record, NULL);
120 TAILQ_INIT(&record->ts_head);
123 SYSINIT(linux_rcu_runtime, SI_SUB_CPU, SI_ORDER_ANY, linux_rcu_runtime_init, NULL);
126 linux_rcu_runtime_uninit(void *arg __unused)
128 struct linux_epoch_head *head;
130 head = &linux_epoch_head;
132 /* destroy head lock */
133 mtx_destroy(&head->lock);
135 SYSUNINIT(linux_rcu_runtime, SI_SUB_LOCK, SI_ORDER_SECOND, linux_rcu_runtime_uninit, NULL);
138 linux_rcu_cleaner_func(void *context __unused, int pending __unused)
140 struct linux_epoch_head *head;
141 struct callback_head *rcu;
142 STAILQ_HEAD(, callback_head) tmp_head;
144 linux_set_current(curthread);
146 head = &linux_epoch_head;
148 /* move current callbacks into own queue */
149 mtx_lock(&head->lock);
150 STAILQ_INIT(&tmp_head);
151 STAILQ_CONCAT(&tmp_head, &head->cb_head);
152 mtx_unlock(&head->lock);
155 linux_synchronize_rcu();
157 /* dispatch all callbacks, if any */
158 while ((rcu = STAILQ_FIRST(&tmp_head)) != NULL) {
161 STAILQ_REMOVE_HEAD(&tmp_head, entry);
163 offset = (uintptr_t)rcu->func;
165 if (offset < LINUX_KFREE_RCU_OFFSET_MAX)
166 kfree((char *)rcu - offset);
168 rcu->func((struct rcu_head *)rcu);
173 linux_rcu_read_lock(void)
175 struct linux_epoch_record *record;
176 struct task_struct *ts;
182 * Pin thread to current CPU so that the unlock code gets the
183 * same per-CPU epoch record:
187 record = &DPCPU_GET(linux_epoch_record);
191 * Use a critical section to prevent recursion inside
192 * ck_epoch_begin(). Else this function supports recursion.
195 ck_epoch_begin(&record->epoch_record, NULL);
197 if (ts->rcu_recurse == 1)
198 TAILQ_INSERT_TAIL(&record->ts_head, ts, rcu_entry);
203 linux_rcu_read_unlock(void)
205 struct linux_epoch_record *record;
206 struct task_struct *ts;
211 record = &DPCPU_GET(linux_epoch_record);
215 * Use a critical section to prevent recursion inside
216 * ck_epoch_end(). Else this function supports recursion.
219 ck_epoch_end(&record->epoch_record, NULL);
221 if (ts->rcu_recurse == 0)
222 TAILQ_REMOVE(&record->ts_head, ts, rcu_entry);
229 linux_synchronize_rcu_cb(ck_epoch_t *epoch __unused, ck_epoch_record_t *epoch_record, void *arg __unused)
231 struct linux_epoch_record *record =
232 container_of(epoch_record, struct linux_epoch_record, epoch_record);
233 struct thread *td = curthread;
234 struct task_struct *ts;
236 /* check if blocked on the current CPU */
237 if (record->cpuid == PCPU_GET(cpuid)) {
238 bool is_sleeping = 0;
242 * Find the lowest priority or sleeping thread which
243 * is blocking synchronization on this CPU core. All
244 * the threads in the queue are CPU-pinned and cannot
245 * go anywhere while the current thread is locked.
247 TAILQ_FOREACH(ts, &record->ts_head, rcu_entry) {
248 if (ts->task_thread->td_priority > prio)
249 prio = ts->task_thread->td_priority;
250 is_sleeping |= (ts->task_thread->td_inhibitors != 0);
258 /* set new thread priority */
259 sched_prio(td, prio);
261 mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
264 * Release the thread lock while yielding to
265 * allow other threads to acquire the lock
266 * pointed to by TDQ_LOCKPTR(td). Else a
267 * deadlock like situation might happen.
274 * To avoid spinning move execution to the other CPU
275 * which is blocking synchronization. Set highest
276 * thread priority so that code gets run. The thread
277 * priority will be restored later.
280 sched_bind(td, record->cpuid);
285 linux_synchronize_rcu(void)
296 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
297 "linux_synchronize_rcu() can sleep");
302 * Synchronizing RCU might change the CPU core this function
303 * is running on. Save current values:
309 old_cpu = PCPU_GET(cpuid);
310 old_pinned = td->td_pinned;
311 old_prio = td->td_priority;
312 was_bound = sched_is_bound(td);
315 sched_bind(td, old_cpu);
317 ck_epoch_synchronize_wait(&linux_epoch,
318 &linux_synchronize_rcu_cb, NULL);
320 /* restore CPU binding, if any */
321 if (was_bound != 0) {
322 sched_bind(td, old_cpu);
324 /* get thread back to initial CPU, if any */
326 sched_bind(td, old_cpu);
329 /* restore pinned after bind */
330 td->td_pinned = old_pinned;
332 /* restore thread priority */
333 sched_prio(td, old_prio);
340 linux_rcu_barrier(void)
342 struct linux_epoch_head *head;
344 linux_synchronize_rcu();
346 head = &linux_epoch_head;
348 /* wait for callbacks to complete */
349 taskqueue_drain(taskqueue_fast, &head->task);
353 linux_call_rcu(struct rcu_head *context, rcu_callback_t func)
355 struct callback_head *rcu = (struct callback_head *)context;
356 struct linux_epoch_head *head = &linux_epoch_head;
358 mtx_lock(&head->lock);
360 STAILQ_INSERT_TAIL(&head->cb_head, rcu, entry);
361 taskqueue_enqueue(taskqueue_fast, &head->task);
362 mtx_unlock(&head->lock);
366 init_srcu_struct(struct srcu_struct *srcu)
372 cleanup_srcu_struct(struct srcu_struct *srcu)
377 srcu_read_lock(struct srcu_struct *srcu)
379 linux_rcu_read_lock();
384 srcu_read_unlock(struct srcu_struct *srcu, int key __unused)
386 linux_rcu_read_unlock();
390 synchronize_srcu(struct srcu_struct *srcu)
392 linux_synchronize_rcu();
396 srcu_barrier(struct srcu_struct *srcu)