2 * Copyright (c) 2020 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Brute-force implementation for linux RCU functions. Just use a delay
36 * line and per-cpu callouts.
38 #include <sys/types.h>
39 #include <sys/errno.h>
40 #include <sys/kernel.h>
41 #include <sys/spinlock.h>
42 #include <sys/spinlock2.h>
43 #include <linux/gfp.h>
44 #include <linux/slab.h>
45 #include <linux/rcupdate.h>
47 #include <machine/atomic.h>
49 typedef struct rcu_elm {
50 enum { RCU_NULL, RCU_CALL, RCU_FREE } type;
52 void (*func)(struct rcu_head *arg);
56 typedef struct rcu_pcpu {
63 struct callout timer_callout;
66 static rcu_pcpu_t *rcupcpu;
69 * Timer callout (pcpu)
74 rcu_pcpu_t *rcu = arg;
79 while (rcu->s != rcu->e) {
80 elm = &rcu->elms[rcu->s & rcu->mask];
81 delta = ticks - elm->ticks; /* 2s compl underflow */
98 if (rcu->s == rcu->e) {
101 callout_reset_bycpu(&rcu->timer_callout, hz / 10, rcu_timer,
109 * Ping timer callout (pcpu).
111 * Must be in critical section.
114 rcu_ping(rcu_pcpu_t *rcu)
116 if (rcu->running == 0) {
118 callout_reset_bycpu(&rcu->timer_callout, hz / 10, rcu_timer,
124 * Expand the rcu array for the current cpu
127 rcu_expand(rcu_pcpu_t *rcu)
136 count = rcu->e - rcu->s; /* note: 2s complement underflow */
137 while (unlikely(count == rcu->size)) {
138 nsize = count ? count * 2 : 16;
139 nelms = kzalloc(nsize * sizeof(*nelms), GFP_KERNEL);
140 kprintf("drm: expand RCU cpu %d to %d\n", mycpuid, nsize);
141 if (likely(count == rcu->size)) {
145 while (n != rcu->e) {
146 nelms[n & nmask] = oelms[n & rcu->mask];
154 if (likely(nelms != NULL))
156 count = rcu->e - rcu->s;
158 KKASSERT(count >= 0 && count < rcu->size);
162 kfree_rcu(void *ptr, void *rcu_head __unused)
167 if (unlikely(rcupcpu == NULL)) {
171 rcu = &rcupcpu[mycpuid];
175 elm = &rcu->elms[rcu->e & rcu->mask];
178 elm->type = RCU_FREE;
187 call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *))
192 if (unlikely(rcupcpu == NULL)) {
196 rcu = &rcupcpu[mycpuid];
200 elm = &rcu->elms[rcu->e & rcu->mask];
203 elm->type = RCU_CALL;
213 init_rcu(void *dummy __unused)
217 rcupcpu = kzalloc(ncpus * sizeof(*rcupcpu), GFP_KERNEL);
218 for (i = 0; i < ncpus; ++i) {
219 callout_init_mp(&rcupcpu[i].timer_callout);
224 SYSINIT(linux_rcu_init, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, init_rcu, NULL);