11a01b4e5SHans Petter Selasky /*-
296fc97c8SStephen Hurd * Copyright (c) 2016 Matthew Macy (mmacy@mattmacy.io)
317777208SHans Petter Selasky * Copyright (c) 2017-2021 Hans Petter Selasky (hselasky@freebsd.org)
41a01b4e5SHans Petter Selasky * All rights reserved.
5*5c92f84bSBjoern A. Zeeb * Copyright (c) 2024 The FreeBSD Foundation
6*5c92f84bSBjoern A. Zeeb *
7*5c92f84bSBjoern A. Zeeb * Portions of this software were developed by Björn Zeeb
8*5c92f84bSBjoern A. Zeeb * under sponsorship from the FreeBSD Foundation.
91a01b4e5SHans Petter Selasky *
101a01b4e5SHans Petter Selasky * Redistribution and use in source and binary forms, with or without
111a01b4e5SHans Petter Selasky * modification, are permitted provided that the following conditions
121a01b4e5SHans Petter Selasky * are met:
131a01b4e5SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright
141a01b4e5SHans Petter Selasky * notice unmodified, this list of conditions, and the following
151a01b4e5SHans Petter Selasky * disclaimer.
161a01b4e5SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright
171a01b4e5SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the
181a01b4e5SHans Petter Selasky * documentation and/or other materials provided with the distribution.
191a01b4e5SHans Petter Selasky *
201a01b4e5SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
211a01b4e5SHans Petter Selasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
221a01b4e5SHans Petter Selasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
231a01b4e5SHans Petter Selasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
241a01b4e5SHans Petter Selasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
251a01b4e5SHans Petter Selasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
261a01b4e5SHans Petter Selasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
271a01b4e5SHans Petter Selasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
281a01b4e5SHans Petter Selasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
291a01b4e5SHans Petter Selasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
301a01b4e5SHans Petter Selasky */
311a01b4e5SHans Petter Selasky
321a01b4e5SHans Petter Selasky #include <sys/types.h>
331a01b4e5SHans Petter Selasky #include <sys/systm.h>
341a01b4e5SHans Petter Selasky #include <sys/malloc.h>
351a01b4e5SHans Petter Selasky #include <sys/kernel.h>
361a01b4e5SHans Petter Selasky #include <sys/lock.h>
371a01b4e5SHans Petter Selasky #include <sys/mutex.h>
381a01b4e5SHans Petter Selasky #include <sys/proc.h>
391a01b4e5SHans Petter Selasky #include <sys/sched.h>
401a01b4e5SHans Petter Selasky #include <sys/smp.h>
411a01b4e5SHans Petter Selasky #include <sys/queue.h>
421a01b4e5SHans Petter Selasky #include <sys/taskqueue.h>
43f3de9af6SHans Petter Selasky #include <sys/kdb.h>
441a01b4e5SHans Petter Selasky
451a01b4e5SHans Petter Selasky #include <ck_epoch.h>
461a01b4e5SHans Petter Selasky
471a01b4e5SHans Petter Selasky #include <linux/rcupdate.h>
48256eb8d5SVladimir Kondratyev #include <linux/sched.h>
491a01b4e5SHans Petter Selasky #include <linux/srcu.h>
501a01b4e5SHans Petter Selasky #include <linux/slab.h>
511a01b4e5SHans Petter Selasky #include <linux/kernel.h>
52f3de9af6SHans Petter Selasky #include <linux/compat.h>
53a2b83b59SVladimir Kondratyev #include <linux/llist.h>
54a2b83b59SVladimir Kondratyev #include <linux/irq_work.h>
551a01b4e5SHans Petter Selasky
56f3de9af6SHans Petter Selasky /*
57f3de9af6SHans Petter Selasky * By defining CONFIG_NO_RCU_SKIP LinuxKPI RCU locks and asserts will
58f3de9af6SHans Petter Selasky * not be skipped during panic().
59f3de9af6SHans Petter Selasky */
60f3de9af6SHans Petter Selasky #ifdef CONFIG_NO_RCU_SKIP
61f3de9af6SHans Petter Selasky #define RCU_SKIP(void) 0
62f3de9af6SHans Petter Selasky #else
63f3de9af6SHans Petter Selasky #define RCU_SKIP(void) unlikely(SCHEDULER_STOPPED() || kdb_active)
64f3de9af6SHans Petter Selasky #endif
651f827dabSHans Petter Selasky
661f827dabSHans Petter Selasky struct callback_head {
67a2b83b59SVladimir Kondratyev union {
681f827dabSHans Petter Selasky STAILQ_ENTRY(callback_head) entry;
69a2b83b59SVladimir Kondratyev struct llist_node node;
70a2b83b59SVladimir Kondratyev };
711f827dabSHans Petter Selasky rcu_callback_t func;
721f827dabSHans Petter Selasky };
731f827dabSHans Petter Selasky
74f3de9af6SHans Petter Selasky struct linux_epoch_head {
75a2b83b59SVladimir Kondratyev struct llist_head cb_head;
76f3de9af6SHans Petter Selasky struct task task;
77f3de9af6SHans Petter Selasky } __aligned(CACHE_LINE_SIZE);
78f3de9af6SHans Petter Selasky
79f3de9af6SHans Petter Selasky struct linux_epoch_record {
801f827dabSHans Petter Selasky ck_epoch_record_t epoch_record;
81f3de9af6SHans Petter Selasky TAILQ_HEAD(, task_struct) ts_head;
82f3de9af6SHans Petter Selasky int cpuid;
836ae24079SHans Petter Selasky int type;
84f3de9af6SHans Petter Selasky } __aligned(CACHE_LINE_SIZE);
851a01b4e5SHans Petter Selasky
861a01b4e5SHans Petter Selasky /*
871a01b4e5SHans Petter Selasky * Verify that "struct rcu_head" is big enough to hold "struct
881a01b4e5SHans Petter Selasky * callback_head". This has been done to avoid having to add special
891a01b4e5SHans Petter Selasky * compile flags for including ck_epoch.h to all clients of the
901a01b4e5SHans Petter Selasky * LinuxKPI.
911a01b4e5SHans Petter Selasky */
92f3de9af6SHans Petter Selasky CTASSERT(sizeof(struct rcu_head) == sizeof(struct callback_head));
931a01b4e5SHans Petter Selasky
941f827dabSHans Petter Selasky /*
9517777208SHans Petter Selasky * Verify that "rcu_section[0]" has the same size as
9617777208SHans Petter Selasky * "ck_epoch_section_t". This has been done to avoid having to add
9717777208SHans Petter Selasky * special compile flags for including ck_epoch.h to all clients of
9817777208SHans Petter Selasky * the LinuxKPI.
9917777208SHans Petter Selasky */
10017777208SHans Petter Selasky CTASSERT(sizeof(((struct task_struct *)0)->rcu_section[0] ==
10117777208SHans Petter Selasky sizeof(ck_epoch_section_t)));
10217777208SHans Petter Selasky
10317777208SHans Petter Selasky /*
1041f827dabSHans Petter Selasky * Verify that "epoch_record" is at beginning of "struct
105f3de9af6SHans Petter Selasky * linux_epoch_record":
1061f827dabSHans Petter Selasky */
107f3de9af6SHans Petter Selasky CTASSERT(offsetof(struct linux_epoch_record, epoch_record) == 0);
1081f827dabSHans Petter Selasky
1096ae24079SHans Petter Selasky CTASSERT(TS_RCU_TYPE_MAX == RCU_TYPE_MAX);
1106ae24079SHans Petter Selasky
111eae5868cSHans Petter Selasky static ck_epoch_t linux_epoch[RCU_TYPE_MAX];
112eae5868cSHans Petter Selasky static struct linux_epoch_head linux_epoch_head[RCU_TYPE_MAX];
113eae5868cSHans Petter Selasky DPCPU_DEFINE_STATIC(struct linux_epoch_record, linux_epoch_record[RCU_TYPE_MAX]);
1141f827dabSHans Petter Selasky
1151f827dabSHans Petter Selasky static void linux_rcu_cleaner_func(void *, int);
1161a01b4e5SHans Petter Selasky
1171a01b4e5SHans Petter Selasky static void
linux_rcu_runtime_init(void * arg __unused)1181a01b4e5SHans Petter Selasky linux_rcu_runtime_init(void *arg __unused)
1191a01b4e5SHans Petter Selasky {
120f3de9af6SHans Petter Selasky struct linux_epoch_head *head;
1211a01b4e5SHans Petter Selasky int i;
122eae5868cSHans Petter Selasky int j;
1231a01b4e5SHans Petter Selasky
124eae5868cSHans Petter Selasky for (j = 0; j != RCU_TYPE_MAX; j++) {
125eae5868cSHans Petter Selasky ck_epoch_init(&linux_epoch[j]);
1261a01b4e5SHans Petter Selasky
127eae5868cSHans Petter Selasky head = &linux_epoch_head[j];
128f3de9af6SHans Petter Selasky
129eae5868cSHans Petter Selasky TASK_INIT(&head->task, 0, linux_rcu_cleaner_func, head);
130a2b83b59SVladimir Kondratyev init_llist_head(&head->cb_head);
131f3de9af6SHans Petter Selasky
1321a01b4e5SHans Petter Selasky CPU_FOREACH(i) {
133f3de9af6SHans Petter Selasky struct linux_epoch_record *record;
1341f827dabSHans Petter Selasky
135eae5868cSHans Petter Selasky record = &DPCPU_ID_GET(i, linux_epoch_record[j]);
1361f827dabSHans Petter Selasky
137f3de9af6SHans Petter Selasky record->cpuid = i;
1386ae24079SHans Petter Selasky record->type = j;
139eae5868cSHans Petter Selasky ck_epoch_register(&linux_epoch[j],
140eae5868cSHans Petter Selasky &record->epoch_record, NULL);
141f3de9af6SHans Petter Selasky TAILQ_INIT(&record->ts_head);
1421a01b4e5SHans Petter Selasky }
1431a01b4e5SHans Petter Selasky }
144eae5868cSHans Petter Selasky }
145d8e073a9SHans Petter Selasky SYSINIT(linux_rcu_runtime, SI_SUB_CPU, SI_ORDER_ANY, linux_rcu_runtime_init, NULL);
1461a01b4e5SHans Petter Selasky
1471a01b4e5SHans Petter Selasky static void
linux_rcu_cleaner_func(void * context,int pending __unused)148eae5868cSHans Petter Selasky linux_rcu_cleaner_func(void *context, int pending __unused)
1491f827dabSHans Petter Selasky {
150a2b83b59SVladimir Kondratyev struct linux_epoch_head *head = context;
1511a01b4e5SHans Petter Selasky struct callback_head *rcu;
152f3de9af6SHans Petter Selasky STAILQ_HEAD(, callback_head) tmp_head;
153a2b83b59SVladimir Kondratyev struct llist_node *node, *next;
154eae5868cSHans Petter Selasky uintptr_t offset;
1551f827dabSHans Petter Selasky
1561f827dabSHans Petter Selasky /* move current callbacks into own queue */
157f3de9af6SHans Petter Selasky STAILQ_INIT(&tmp_head);
158a2b83b59SVladimir Kondratyev llist_for_each_safe(node, next, llist_del_all(&head->cb_head)) {
159a2b83b59SVladimir Kondratyev rcu = container_of(node, struct callback_head, node);
160a2b83b59SVladimir Kondratyev /* re-reverse list to restore chronological order */
161a2b83b59SVladimir Kondratyev STAILQ_INSERT_HEAD(&tmp_head, rcu, entry);
162a2b83b59SVladimir Kondratyev }
1631f827dabSHans Petter Selasky
1641f827dabSHans Petter Selasky /* synchronize */
165eae5868cSHans Petter Selasky linux_synchronize_rcu(head - linux_epoch_head);
1661f827dabSHans Petter Selasky
1671f827dabSHans Petter Selasky /* dispatch all callbacks, if any */
168f3de9af6SHans Petter Selasky while ((rcu = STAILQ_FIRST(&tmp_head)) != NULL) {
169f3de9af6SHans Petter Selasky STAILQ_REMOVE_HEAD(&tmp_head, entry);
1701a01b4e5SHans Petter Selasky
1711a01b4e5SHans Petter Selasky offset = (uintptr_t)rcu->func;
1721a01b4e5SHans Petter Selasky
1731a01b4e5SHans Petter Selasky if (offset < LINUX_KFREE_RCU_OFFSET_MAX)
1741a01b4e5SHans Petter Selasky kfree((char *)rcu - offset);
1751a01b4e5SHans Petter Selasky else
1761a01b4e5SHans Petter Selasky rcu->func((struct rcu_head *)rcu);
1771a01b4e5SHans Petter Selasky }
1781a01b4e5SHans Petter Selasky }
1791a01b4e5SHans Petter Selasky
1801a01b4e5SHans Petter Selasky void
linux_rcu_read_lock(unsigned type)181eae5868cSHans Petter Selasky linux_rcu_read_lock(unsigned type)
1821a01b4e5SHans Petter Selasky {
183f3de9af6SHans Petter Selasky struct linux_epoch_record *record;
184f3de9af6SHans Petter Selasky struct task_struct *ts;
185f3de9af6SHans Petter Selasky
186eae5868cSHans Petter Selasky MPASS(type < RCU_TYPE_MAX);
187eae5868cSHans Petter Selasky
188f3de9af6SHans Petter Selasky if (RCU_SKIP())
189f3de9af6SHans Petter Selasky return;
1901a01b4e5SHans Petter Selasky
19117777208SHans Petter Selasky ts = current;
19217777208SHans Petter Selasky
19317777208SHans Petter Selasky /* assert valid refcount */
19417777208SHans Petter Selasky MPASS(ts->rcu_recurse[type] != INT_MAX);
19517777208SHans Petter Selasky
19617777208SHans Petter Selasky if (++(ts->rcu_recurse[type]) != 1)
19717777208SHans Petter Selasky return;
19817777208SHans Petter Selasky
1991f827dabSHans Petter Selasky /*
2001f827dabSHans Petter Selasky * Pin thread to current CPU so that the unlock code gets the
201f3de9af6SHans Petter Selasky * same per-CPU epoch record:
2021f827dabSHans Petter Selasky */
2031a01b4e5SHans Petter Selasky sched_pin();
2041a01b4e5SHans Petter Selasky
205eae5868cSHans Petter Selasky record = &DPCPU_GET(linux_epoch_record[type]);
2061f827dabSHans Petter Selasky
2071f827dabSHans Petter Selasky /*
2081f827dabSHans Petter Selasky * Use a critical section to prevent recursion inside
2091f827dabSHans Petter Selasky * ck_epoch_begin(). Else this function supports recursion.
2101f827dabSHans Petter Selasky */
2111f827dabSHans Petter Selasky critical_enter();
21217777208SHans Petter Selasky ck_epoch_begin(&record->epoch_record,
21317777208SHans Petter Selasky (ck_epoch_section_t *)&ts->rcu_section[type]);
2146ae24079SHans Petter Selasky TAILQ_INSERT_TAIL(&record->ts_head, ts, rcu_entry[type]);
2151f827dabSHans Petter Selasky critical_exit();
2161a01b4e5SHans Petter Selasky }
2171a01b4e5SHans Petter Selasky
2181a01b4e5SHans Petter Selasky void
linux_rcu_read_unlock(unsigned type)219eae5868cSHans Petter Selasky linux_rcu_read_unlock(unsigned type)
2201a01b4e5SHans Petter Selasky {
221f3de9af6SHans Petter Selasky struct linux_epoch_record *record;
222f3de9af6SHans Petter Selasky struct task_struct *ts;
2231a01b4e5SHans Petter Selasky
224eae5868cSHans Petter Selasky MPASS(type < RCU_TYPE_MAX);
225eae5868cSHans Petter Selasky
226f3de9af6SHans Petter Selasky if (RCU_SKIP())
227f3de9af6SHans Petter Selasky return;
228f3de9af6SHans Petter Selasky
229f3de9af6SHans Petter Selasky ts = current;
2301f827dabSHans Petter Selasky
23117777208SHans Petter Selasky /* assert valid refcount */
23217777208SHans Petter Selasky MPASS(ts->rcu_recurse[type] > 0);
23317777208SHans Petter Selasky
23417777208SHans Petter Selasky if (--(ts->rcu_recurse[type]) != 0)
23517777208SHans Petter Selasky return;
23617777208SHans Petter Selasky
23717777208SHans Petter Selasky record = &DPCPU_GET(linux_epoch_record[type]);
23817777208SHans Petter Selasky
2391f827dabSHans Petter Selasky /*
2401f827dabSHans Petter Selasky * Use a critical section to prevent recursion inside
2411f827dabSHans Petter Selasky * ck_epoch_end(). Else this function supports recursion.
2421f827dabSHans Petter Selasky */
2431f827dabSHans Petter Selasky critical_enter();
24417777208SHans Petter Selasky ck_epoch_end(&record->epoch_record,
24517777208SHans Petter Selasky (ck_epoch_section_t *)&ts->rcu_section[type]);
2466ae24079SHans Petter Selasky TAILQ_REMOVE(&record->ts_head, ts, rcu_entry[type]);
2471f827dabSHans Petter Selasky critical_exit();
2481f827dabSHans Petter Selasky
2491a01b4e5SHans Petter Selasky sched_unpin();
2501a01b4e5SHans Petter Selasky }
2511a01b4e5SHans Petter Selasky
252*5c92f84bSBjoern A. Zeeb bool
linux_rcu_read_lock_held(unsigned type)253*5c92f84bSBjoern A. Zeeb linux_rcu_read_lock_held(unsigned type)
254*5c92f84bSBjoern A. Zeeb {
255*5c92f84bSBjoern A. Zeeb #ifdef INVARINATS
256*5c92f84bSBjoern A. Zeeb struct linux_epoch_record *record __diagused;
257*5c92f84bSBjoern A. Zeeb struct task_struct *ts;
258*5c92f84bSBjoern A. Zeeb
259*5c92f84bSBjoern A. Zeeb MPASS(type < RCU_TYPE_MAX);
260*5c92f84bSBjoern A. Zeeb
261*5c92f84bSBjoern A. Zeeb if (RCU_SKIP())
262*5c92f84bSBjoern A. Zeeb return (false);
263*5c92f84bSBjoern A. Zeeb
264*5c92f84bSBjoern A. Zeeb if (__current_unallocated(curthread))
265*5c92f84bSBjoern A. Zeeb return (false);
266*5c92f84bSBjoern A. Zeeb
267*5c92f84bSBjoern A. Zeeb ts = current;
268*5c92f84bSBjoern A. Zeeb if (ts->rcu_recurse[type] == 0)
269*5c92f84bSBjoern A. Zeeb return (false);
270*5c92f84bSBjoern A. Zeeb
271*5c92f84bSBjoern A. Zeeb MPASS(curthread->td_pinned != 0);
272*5c92f84bSBjoern A. Zeeb MPASS((record = &DPCPU_GET(linux_epoch_record[type])) &&
273*5c92f84bSBjoern A. Zeeb record->epoch_record.active != 0);
274*5c92f84bSBjoern A. Zeeb #endif
275*5c92f84bSBjoern A. Zeeb
276*5c92f84bSBjoern A. Zeeb return (true);
277*5c92f84bSBjoern A. Zeeb }
278*5c92f84bSBjoern A. Zeeb
279f3de9af6SHans Petter Selasky static void
linux_synchronize_rcu_cb(ck_epoch_t * epoch __unused,ck_epoch_record_t * epoch_record,void * arg __unused)280f3de9af6SHans Petter Selasky linux_synchronize_rcu_cb(ck_epoch_t *epoch __unused, ck_epoch_record_t *epoch_record, void *arg __unused)
281f3de9af6SHans Petter Selasky {
282f3de9af6SHans Petter Selasky struct linux_epoch_record *record =
283f3de9af6SHans Petter Selasky container_of(epoch_record, struct linux_epoch_record, epoch_record);
284f3de9af6SHans Petter Selasky struct thread *td = curthread;
285f3de9af6SHans Petter Selasky struct task_struct *ts;
286f3de9af6SHans Petter Selasky
287f3de9af6SHans Petter Selasky /* check if blocked on the current CPU */
288f3de9af6SHans Petter Selasky if (record->cpuid == PCPU_GET(cpuid)) {
289f3de9af6SHans Petter Selasky bool is_sleeping = 0;
290f3de9af6SHans Petter Selasky u_char prio = 0;
291f3de9af6SHans Petter Selasky
292f3de9af6SHans Petter Selasky /*
293f3de9af6SHans Petter Selasky * Find the lowest priority or sleeping thread which
294f3de9af6SHans Petter Selasky * is blocking synchronization on this CPU core. All
295f3de9af6SHans Petter Selasky * the threads in the queue are CPU-pinned and cannot
296f3de9af6SHans Petter Selasky * go anywhere while the current thread is locked.
297f3de9af6SHans Petter Selasky */
2986ae24079SHans Petter Selasky TAILQ_FOREACH(ts, &record->ts_head, rcu_entry[record->type]) {
299f3de9af6SHans Petter Selasky if (ts->task_thread->td_priority > prio)
300f3de9af6SHans Petter Selasky prio = ts->task_thread->td_priority;
301f3de9af6SHans Petter Selasky is_sleeping |= (ts->task_thread->td_inhibitors != 0);
302f3de9af6SHans Petter Selasky }
303f3de9af6SHans Petter Selasky
304f3de9af6SHans Petter Selasky if (is_sleeping) {
305f3de9af6SHans Petter Selasky thread_unlock(td);
306f3de9af6SHans Petter Selasky pause("W", 1);
307f3de9af6SHans Petter Selasky thread_lock(td);
308f3de9af6SHans Petter Selasky } else {
309f3de9af6SHans Petter Selasky /* set new thread priority */
310f3de9af6SHans Petter Selasky sched_prio(td, prio);
311f3de9af6SHans Petter Selasky /* task switch */
312686bcb5cSJeff Roberson mi_switch(SW_VOL | SWT_RELINQUISH);
313cc79ea3aSHans Petter Selasky /*
314cc79ea3aSHans Petter Selasky * It is important the thread lock is dropped
315cc79ea3aSHans Petter Selasky * while yielding to allow other threads to
316cc79ea3aSHans Petter Selasky * acquire the lock pointed to by
317cc79ea3aSHans Petter Selasky * TDQ_LOCKPTR(td). Currently mi_switch() will
318cc79ea3aSHans Petter Selasky * unlock the thread lock before
319cc79ea3aSHans Petter Selasky * returning. Else a deadlock like situation
320cc79ea3aSHans Petter Selasky * might happen.
321cc79ea3aSHans Petter Selasky */
322714ed5b2SHans Petter Selasky thread_lock(td);
323f3de9af6SHans Petter Selasky }
324f3de9af6SHans Petter Selasky } else {
325f3de9af6SHans Petter Selasky /*
326f3de9af6SHans Petter Selasky * To avoid spinning move execution to the other CPU
327f3de9af6SHans Petter Selasky * which is blocking synchronization. Set highest
328f3de9af6SHans Petter Selasky * thread priority so that code gets run. The thread
329f3de9af6SHans Petter Selasky * priority will be restored later.
330f3de9af6SHans Petter Selasky */
331f3de9af6SHans Petter Selasky sched_prio(td, 0);
332f3de9af6SHans Petter Selasky sched_bind(td, record->cpuid);
333f3de9af6SHans Petter Selasky }
334f3de9af6SHans Petter Selasky }
335f3de9af6SHans Petter Selasky
3361a01b4e5SHans Petter Selasky void
linux_synchronize_rcu(unsigned type)337eae5868cSHans Petter Selasky linux_synchronize_rcu(unsigned type)
3381a01b4e5SHans Petter Selasky {
339f3de9af6SHans Petter Selasky struct thread *td;
340f3de9af6SHans Petter Selasky int was_bound;
341f3de9af6SHans Petter Selasky int old_cpu;
342f3de9af6SHans Petter Selasky int old_pinned;
3433f743d78SHans Petter Selasky u_char old_prio;
344f3de9af6SHans Petter Selasky
345eae5868cSHans Petter Selasky MPASS(type < RCU_TYPE_MAX);
346eae5868cSHans Petter Selasky
347f3de9af6SHans Petter Selasky if (RCU_SKIP())
348f3de9af6SHans Petter Selasky return;
349f3de9af6SHans Petter Selasky
350f3de9af6SHans Petter Selasky WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
351f3de9af6SHans Petter Selasky "linux_synchronize_rcu() can sleep");
352f3de9af6SHans Petter Selasky
353f3de9af6SHans Petter Selasky td = curthread;
354fedab1b4SKonstantin Belousov DROP_GIANT();
355f3de9af6SHans Petter Selasky
356f3de9af6SHans Petter Selasky /*
357f3de9af6SHans Petter Selasky * Synchronizing RCU might change the CPU core this function
358f3de9af6SHans Petter Selasky * is running on. Save current values:
359f3de9af6SHans Petter Selasky */
360f3de9af6SHans Petter Selasky thread_lock(td);
361f3de9af6SHans Petter Selasky
362f3de9af6SHans Petter Selasky old_cpu = PCPU_GET(cpuid);
363f3de9af6SHans Petter Selasky old_pinned = td->td_pinned;
3643f743d78SHans Petter Selasky old_prio = td->td_priority;
365f3de9af6SHans Petter Selasky was_bound = sched_is_bound(td);
366ea165254SHans Petter Selasky sched_unbind(td);
367ea165254SHans Petter Selasky td->td_pinned = 0;
368f3de9af6SHans Petter Selasky sched_bind(td, old_cpu);
369f3de9af6SHans Petter Selasky
370eae5868cSHans Petter Selasky ck_epoch_synchronize_wait(&linux_epoch[type],
371f3de9af6SHans Petter Selasky &linux_synchronize_rcu_cb, NULL);
372f3de9af6SHans Petter Selasky
373f3de9af6SHans Petter Selasky /* restore CPU binding, if any */
374f3de9af6SHans Petter Selasky if (was_bound != 0) {
375f3de9af6SHans Petter Selasky sched_bind(td, old_cpu);
376f3de9af6SHans Petter Selasky } else {
377f3de9af6SHans Petter Selasky /* get thread back to initial CPU, if any */
378f3de9af6SHans Petter Selasky if (old_pinned != 0)
379f3de9af6SHans Petter Selasky sched_bind(td, old_cpu);
380f3de9af6SHans Petter Selasky sched_unbind(td);
381f3de9af6SHans Petter Selasky }
382f3de9af6SHans Petter Selasky /* restore pinned after bind */
383f3de9af6SHans Petter Selasky td->td_pinned = old_pinned;
3843f743d78SHans Petter Selasky
3853f743d78SHans Petter Selasky /* restore thread priority */
3863f743d78SHans Petter Selasky sched_prio(td, old_prio);
387f3de9af6SHans Petter Selasky thread_unlock(td);
388f3de9af6SHans Petter Selasky
389f3de9af6SHans Petter Selasky PICKUP_GIANT();
3901a01b4e5SHans Petter Selasky }
3911a01b4e5SHans Petter Selasky
3921a01b4e5SHans Petter Selasky void
linux_rcu_barrier(unsigned type)393eae5868cSHans Petter Selasky linux_rcu_barrier(unsigned type)
3941a01b4e5SHans Petter Selasky {
395f3de9af6SHans Petter Selasky struct linux_epoch_head *head;
3961a01b4e5SHans Petter Selasky
397eae5868cSHans Petter Selasky MPASS(type < RCU_TYPE_MAX);
3981f827dabSHans Petter Selasky
3991ab61a19SVladimir Kondratyev /*
4001ab61a19SVladimir Kondratyev * This function is not obligated to wait for a grace period.
4011ab61a19SVladimir Kondratyev * It only waits for RCU callbacks that have already been posted.
4021ab61a19SVladimir Kondratyev * If there are no RCU callbacks posted, rcu_barrier() can return
4031ab61a19SVladimir Kondratyev * immediately.
4041ab61a19SVladimir Kondratyev */
405eae5868cSHans Petter Selasky head = &linux_epoch_head[type];
4061f827dabSHans Petter Selasky
4071f827dabSHans Petter Selasky /* wait for callbacks to complete */
408a2b83b59SVladimir Kondratyev taskqueue_drain(linux_irq_work_tq, &head->task);
4091a01b4e5SHans Petter Selasky }
4101a01b4e5SHans Petter Selasky
4111a01b4e5SHans Petter Selasky void
linux_call_rcu(unsigned type,struct rcu_head * context,rcu_callback_t func)412eae5868cSHans Petter Selasky linux_call_rcu(unsigned type, struct rcu_head *context, rcu_callback_t func)
4131a01b4e5SHans Petter Selasky {
414eae5868cSHans Petter Selasky struct callback_head *rcu;
415eae5868cSHans Petter Selasky struct linux_epoch_head *head;
416eae5868cSHans Petter Selasky
417eae5868cSHans Petter Selasky MPASS(type < RCU_TYPE_MAX);
418eae5868cSHans Petter Selasky
419eae5868cSHans Petter Selasky rcu = (struct callback_head *)context;
420eae5868cSHans Petter Selasky head = &linux_epoch_head[type];
4211a01b4e5SHans Petter Selasky
4221f827dabSHans Petter Selasky rcu->func = func;
423a2b83b59SVladimir Kondratyev llist_add(&rcu->node, &head->cb_head);
424a2b83b59SVladimir Kondratyev taskqueue_enqueue(linux_irq_work_tq, &head->task);
4251a01b4e5SHans Petter Selasky }
4261a01b4e5SHans Petter Selasky
4271a01b4e5SHans Petter Selasky int
init_srcu_struct(struct srcu_struct * srcu)4281a01b4e5SHans Petter Selasky init_srcu_struct(struct srcu_struct *srcu)
4291a01b4e5SHans Petter Selasky {
4301a01b4e5SHans Petter Selasky return (0);
4311a01b4e5SHans Petter Selasky }
4321a01b4e5SHans Petter Selasky
4331a01b4e5SHans Petter Selasky void
cleanup_srcu_struct(struct srcu_struct * srcu)4341a01b4e5SHans Petter Selasky cleanup_srcu_struct(struct srcu_struct *srcu)
4351a01b4e5SHans Petter Selasky {
4361a01b4e5SHans Petter Selasky }
4371a01b4e5SHans Petter Selasky
4381a01b4e5SHans Petter Selasky int
srcu_read_lock(struct srcu_struct * srcu)4391a01b4e5SHans Petter Selasky srcu_read_lock(struct srcu_struct *srcu)
4401a01b4e5SHans Petter Selasky {
441eae5868cSHans Petter Selasky linux_rcu_read_lock(RCU_TYPE_SLEEPABLE);
4421a01b4e5SHans Petter Selasky return (0);
4431a01b4e5SHans Petter Selasky }
4441a01b4e5SHans Petter Selasky
4451a01b4e5SHans Petter Selasky void
srcu_read_unlock(struct srcu_struct * srcu,int key __unused)4461a01b4e5SHans Petter Selasky srcu_read_unlock(struct srcu_struct *srcu, int key __unused)
4471a01b4e5SHans Petter Selasky {
448eae5868cSHans Petter Selasky linux_rcu_read_unlock(RCU_TYPE_SLEEPABLE);
4491a01b4e5SHans Petter Selasky }
4501a01b4e5SHans Petter Selasky
4511a01b4e5SHans Petter Selasky void
synchronize_srcu(struct srcu_struct * srcu)4521a01b4e5SHans Petter Selasky synchronize_srcu(struct srcu_struct *srcu)
4531a01b4e5SHans Petter Selasky {
454eae5868cSHans Petter Selasky linux_synchronize_rcu(RCU_TYPE_SLEEPABLE);
4551f827dabSHans Petter Selasky }
4561f827dabSHans Petter Selasky
4571f827dabSHans Petter Selasky void
srcu_barrier(struct srcu_struct * srcu)4581f827dabSHans Petter Selasky srcu_barrier(struct srcu_struct *srcu)
4591f827dabSHans Petter Selasky {
460eae5868cSHans Petter Selasky linux_rcu_barrier(RCU_TYPE_SLEEPABLE);
4611a01b4e5SHans Petter Selasky }
462