1 /*-
2 * Copyright (c) 2017 Hans Petter Selasky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
10 * disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/types.h>
31 #include <sys/malloc.h>
32 #include <sys/gtaskqueue.h>
33 #include <sys/proc.h>
34 #include <sys/sched.h>
35
36 #include <linux/compiler.h>
37 #include <linux/interrupt.h>
38 #include <linux/compat.h>
39
40 #define TASKLET_ST_IDLE 0
41 #define TASKLET_ST_BUSY 1
42 #define TASKLET_ST_EXEC 2
43 #define TASKLET_ST_LOOP 3
44
45 #define TASKLET_ST_CMPSET(ts, old, new) \
46 atomic_cmpset_int((volatile u_int *)&(ts)->tasklet_state, old, new)
47
48 #define TASKLET_ST_SET(ts, new) \
49 WRITE_ONCE(*(volatile u_int *)&(ts)->tasklet_state, new)
50
51 #define TASKLET_ST_GET(ts) \
52 READ_ONCE(*(volatile u_int *)&(ts)->tasklet_state)
53
54 struct tasklet_worker {
55 struct mtx mtx;
56 TAILQ_HEAD(tasklet_list, tasklet_struct) head;
57 struct grouptask gtask;
58 } __aligned(CACHE_LINE_SIZE);
59
60 #define TASKLET_WORKER_LOCK(tw) mtx_lock(&(tw)->mtx)
61 #define TASKLET_WORKER_UNLOCK(tw) mtx_unlock(&(tw)->mtx)
62
63 DPCPU_DEFINE_STATIC(struct tasklet_worker, tasklet_worker);
64
65 static void
tasklet_handler(void * arg)66 tasklet_handler(void *arg)
67 {
68 struct tasklet_worker *tw = (struct tasklet_worker *)arg;
69 struct tasklet_struct *ts;
70 struct tasklet_struct *last;
71
72 linux_set_current(curthread);
73
74 TASKLET_WORKER_LOCK(tw);
75 last = TAILQ_LAST(&tw->head, tasklet_list);
76 while (1) {
77 ts = TAILQ_FIRST(&tw->head);
78 if (ts == NULL)
79 break;
80 TAILQ_REMOVE(&tw->head, ts, entry);
81
82 if (!atomic_read(&ts->count)) {
83 TASKLET_WORKER_UNLOCK(tw);
84 do {
85 /* reset executing state */
86 TASKLET_ST_SET(ts, TASKLET_ST_EXEC);
87
88 ts->func(ts->data);
89
90 } while (TASKLET_ST_CMPSET(ts, TASKLET_ST_EXEC,
91 TASKLET_ST_IDLE) == 0);
92 TASKLET_WORKER_LOCK(tw);
93 } else {
94 TAILQ_INSERT_TAIL(&tw->head, ts, entry);
95 }
96 if (ts == last)
97 break;
98 }
99 TASKLET_WORKER_UNLOCK(tw);
100 }
101
102 static void
tasklet_subsystem_init(void * arg __unused)103 tasklet_subsystem_init(void *arg __unused)
104 {
105 struct tasklet_worker *tw;
106 char buf[32];
107 int i;
108
109 CPU_FOREACH(i) {
110 if (CPU_ABSENT(i))
111 continue;
112
113 tw = DPCPU_ID_PTR(i, tasklet_worker);
114
115 mtx_init(&tw->mtx, "linux_tasklet", NULL, MTX_DEF);
116 TAILQ_INIT(&tw->head);
117 GROUPTASK_INIT(&tw->gtask, 0, tasklet_handler, tw);
118 snprintf(buf, sizeof(buf), "softirq%d", i);
119 taskqgroup_attach_cpu(qgroup_softirq, &tw->gtask,
120 "tasklet", i, NULL, NULL, buf);
121 }
122 }
123 SYSINIT(linux_tasklet, SI_SUB_TASKQ, SI_ORDER_THIRD, tasklet_subsystem_init, NULL);
124
125 static void
tasklet_subsystem_uninit(void * arg __unused)126 tasklet_subsystem_uninit(void *arg __unused)
127 {
128 struct tasklet_worker *tw;
129 int i;
130
131 taskqgroup_drain_all(qgroup_softirq);
132
133 CPU_FOREACH(i) {
134 if (CPU_ABSENT(i))
135 continue;
136
137 tw = DPCPU_ID_PTR(i, tasklet_worker);
138
139 taskqgroup_detach(qgroup_softirq, &tw->gtask);
140 mtx_destroy(&tw->mtx);
141 }
142 }
143 SYSUNINIT(linux_tasklet, SI_SUB_TASKQ, SI_ORDER_THIRD, tasklet_subsystem_uninit, NULL);
144
145 void
tasklet_init(struct tasklet_struct * ts,tasklet_func_t * func,unsigned long data)146 tasklet_init(struct tasklet_struct *ts,
147 tasklet_func_t *func, unsigned long data)
148 {
149 ts->entry.tqe_prev = NULL;
150 ts->entry.tqe_next = NULL;
151 ts->func = func;
152 ts->data = data;
153 atomic_set_int(&ts->tasklet_state, TASKLET_ST_IDLE);
154 atomic_set(&ts->count, 0);
155 }
156
157 void
local_bh_enable(void)158 local_bh_enable(void)
159 {
160 sched_unpin();
161 }
162
163 void
local_bh_disable(void)164 local_bh_disable(void)
165 {
166 sched_pin();
167 }
168
169 void
tasklet_schedule(struct tasklet_struct * ts)170 tasklet_schedule(struct tasklet_struct *ts)
171 {
172
173 /* tasklet is paused */
174 if (atomic_read(&ts->count))
175 return;
176
177 if (TASKLET_ST_CMPSET(ts, TASKLET_ST_EXEC, TASKLET_ST_LOOP)) {
178 /* tasklet_handler() will loop */
179 } else if (TASKLET_ST_CMPSET(ts, TASKLET_ST_IDLE, TASKLET_ST_BUSY)) {
180 struct tasklet_worker *tw;
181
182 tw = &DPCPU_GET(tasklet_worker);
183
184 /* tasklet_handler() was not queued */
185 TASKLET_WORKER_LOCK(tw);
186 /* enqueue tasklet */
187 TAILQ_INSERT_TAIL(&tw->head, ts, entry);
188 /* schedule worker */
189 GROUPTASK_ENQUEUE(&tw->gtask);
190 TASKLET_WORKER_UNLOCK(tw);
191 } else {
192 /*
193 * tasklet_handler() is already executing
194 *
195 * If the state is neither EXEC nor IDLE, it is either
196 * LOOP or BUSY. If the state changed between the two
197 * CMPSET's above the only possible transitions by
198 * elimination are LOOP->EXEC and BUSY->EXEC. If a
199 * EXEC->LOOP transition was missed that is not a
200 * problem because the callback function is then
201 * already about to be called again.
202 */
203 }
204 }
205
206 void
tasklet_kill(struct tasklet_struct * ts)207 tasklet_kill(struct tasklet_struct *ts)
208 {
209
210 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "tasklet_kill() can sleep");
211
212 /* wait until tasklet is no longer busy */
213 while (TASKLET_ST_GET(ts) != TASKLET_ST_IDLE)
214 pause("W", 1);
215 }
216
217 void
tasklet_enable(struct tasklet_struct * ts)218 tasklet_enable(struct tasklet_struct *ts)
219 {
220
221 atomic_dec(&ts->count);
222 }
223
224 void
tasklet_disable(struct tasklet_struct * ts)225 tasklet_disable(struct tasklet_struct *ts)
226 {
227
228 atomic_inc(&ts->count);
229 tasklet_unlock_wait(ts);
230 }
231
232 void
tasklet_disable_nosync(struct tasklet_struct * ts)233 tasklet_disable_nosync(struct tasklet_struct *ts)
234 {
235 atomic_inc(&ts->count);
236 barrier();
237 }
238
239 int
tasklet_trylock(struct tasklet_struct * ts)240 tasklet_trylock(struct tasklet_struct *ts)
241 {
242
243 return (TASKLET_ST_CMPSET(ts, TASKLET_ST_IDLE, TASKLET_ST_BUSY));
244 }
245
246 void
tasklet_unlock(struct tasklet_struct * ts)247 tasklet_unlock(struct tasklet_struct *ts)
248 {
249
250 TASKLET_ST_SET(ts, TASKLET_ST_IDLE);
251 }
252
253 void
tasklet_unlock_wait(struct tasklet_struct * ts)254 tasklet_unlock_wait(struct tasklet_struct *ts)
255 {
256
257 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "tasklet_kill() can sleep");
258
259 /* wait until tasklet is no longer busy */
260 while (TASKLET_ST_GET(ts) != TASKLET_ST_IDLE)
261 pause("W", 1);
262 }
263