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 #include <sys/types.h>
29 #include <sys/malloc.h>
30 #include <sys/gtaskqueue.h>
31 #include <sys/proc.h>
32 #include <sys/sched.h>
33
34 #include <linux/compiler.h>
35 #include <linux/interrupt.h>
36 #include <linux/compat.h>
37
38 #define TASKLET_ST_IDLE 0
39 #define TASKLET_ST_BUSY 1
40 #define TASKLET_ST_EXEC 2
41 #define TASKLET_ST_LOOP 3
42
43 #define TASKLET_ST_CMPSET(ts, old, new) \
44 atomic_cmpset_int((volatile u_int *)&(ts)->tasklet_state, old, new)
45
46 #define TASKLET_ST_SET(ts, new) \
47 WRITE_ONCE(*(volatile u_int *)&(ts)->tasklet_state, new)
48
49 #define TASKLET_ST_GET(ts) \
50 READ_ONCE(*(volatile u_int *)&(ts)->tasklet_state)
51
52 struct tasklet_worker {
53 struct mtx mtx;
54 TAILQ_HEAD(tasklet_list, tasklet_struct) head;
55 struct grouptask gtask;
56 } __aligned(CACHE_LINE_SIZE);
57
58 #define TASKLET_WORKER_LOCK(tw) mtx_lock(&(tw)->mtx)
59 #define TASKLET_WORKER_UNLOCK(tw) mtx_unlock(&(tw)->mtx)
60
61 DPCPU_DEFINE_STATIC(struct tasklet_worker, tasklet_worker);
62
63 static void
tasklet_handler(void * arg)64 tasklet_handler(void *arg)
65 {
66 struct tasklet_worker *tw = (struct tasklet_worker *)arg;
67 struct tasklet_struct *ts;
68 struct tasklet_struct *last;
69
70 linux_set_current(curthread);
71
72 TASKLET_WORKER_LOCK(tw);
73 last = TAILQ_LAST(&tw->head, tasklet_list);
74 while (1) {
75 ts = TAILQ_FIRST(&tw->head);
76 if (ts == NULL)
77 break;
78 TAILQ_REMOVE(&tw->head, ts, entry);
79
80 if (!atomic_read(&ts->count)) {
81 TASKLET_WORKER_UNLOCK(tw);
82 do {
83 /* reset executing state */
84 TASKLET_ST_SET(ts, TASKLET_ST_EXEC);
85
86 ts->func(ts->data);
87
88 } while (TASKLET_ST_CMPSET(ts, TASKLET_ST_EXEC,
89 TASKLET_ST_IDLE) == 0);
90 TASKLET_WORKER_LOCK(tw);
91 } else {
92 TAILQ_INSERT_TAIL(&tw->head, ts, entry);
93 }
94 if (ts == last)
95 break;
96 }
97 TASKLET_WORKER_UNLOCK(tw);
98 }
99
100 static void
tasklet_subsystem_init(void * arg __unused)101 tasklet_subsystem_init(void *arg __unused)
102 {
103 struct tasklet_worker *tw;
104 char buf[32];
105 int i;
106
107 CPU_FOREACH(i) {
108 if (CPU_ABSENT(i))
109 continue;
110
111 tw = DPCPU_ID_PTR(i, tasklet_worker);
112
113 mtx_init(&tw->mtx, "linux_tasklet", NULL, MTX_DEF);
114 TAILQ_INIT(&tw->head);
115 GROUPTASK_INIT(&tw->gtask, 0, tasklet_handler, tw);
116 snprintf(buf, sizeof(buf), "softirq%d", i);
117 taskqgroup_attach_cpu(qgroup_softirq, &tw->gtask,
118 "tasklet", i, NULL, NULL, buf);
119 }
120 }
121 SYSINIT(linux_tasklet, SI_SUB_TASKQ, SI_ORDER_THIRD, tasklet_subsystem_init, NULL);
122
123 static void
tasklet_subsystem_uninit(void * arg __unused)124 tasklet_subsystem_uninit(void *arg __unused)
125 {
126 struct tasklet_worker *tw;
127 int i;
128
129 taskqgroup_drain_all(qgroup_softirq);
130
131 CPU_FOREACH(i) {
132 if (CPU_ABSENT(i))
133 continue;
134
135 tw = DPCPU_ID_PTR(i, tasklet_worker);
136
137 taskqgroup_detach(qgroup_softirq, &tw->gtask);
138 mtx_destroy(&tw->mtx);
139 }
140 }
141 SYSUNINIT(linux_tasklet, SI_SUB_TASKQ, SI_ORDER_THIRD, tasklet_subsystem_uninit, NULL);
142
143 void
tasklet_init(struct tasklet_struct * ts,tasklet_func_t * func,unsigned long data)144 tasklet_init(struct tasklet_struct *ts,
145 tasklet_func_t *func, unsigned long data)
146 {
147 ts->entry.tqe_prev = NULL;
148 ts->entry.tqe_next = NULL;
149 ts->func = func;
150 ts->data = data;
151 atomic_set_int(&ts->tasklet_state, TASKLET_ST_IDLE);
152 atomic_set(&ts->count, 0);
153 }
154
155 void
local_bh_enable(void)156 local_bh_enable(void)
157 {
158 sched_unpin();
159 }
160
161 void
local_bh_disable(void)162 local_bh_disable(void)
163 {
164 sched_pin();
165 }
166
167 void
tasklet_schedule(struct tasklet_struct * ts)168 tasklet_schedule(struct tasklet_struct *ts)
169 {
170
171 /* tasklet is paused */
172 if (atomic_read(&ts->count))
173 return;
174
175 if (TASKLET_ST_CMPSET(ts, TASKLET_ST_EXEC, TASKLET_ST_LOOP)) {
176 /* tasklet_handler() will loop */
177 } else if (TASKLET_ST_CMPSET(ts, TASKLET_ST_IDLE, TASKLET_ST_BUSY)) {
178 struct tasklet_worker *tw;
179
180 tw = &DPCPU_GET(tasklet_worker);
181
182 /* tasklet_handler() was not queued */
183 TASKLET_WORKER_LOCK(tw);
184 /* enqueue tasklet */
185 TAILQ_INSERT_TAIL(&tw->head, ts, entry);
186 /* schedule worker */
187 GROUPTASK_ENQUEUE(&tw->gtask);
188 TASKLET_WORKER_UNLOCK(tw);
189 } else {
190 /*
191 * tasklet_handler() is already executing
192 *
193 * If the state is neither EXEC nor IDLE, it is either
194 * LOOP or BUSY. If the state changed between the two
195 * CMPSET's above the only possible transitions by
196 * elimination are LOOP->EXEC and BUSY->EXEC. If a
197 * EXEC->LOOP transition was missed that is not a
198 * problem because the callback function is then
199 * already about to be called again.
200 */
201 }
202 }
203
204 void
tasklet_kill(struct tasklet_struct * ts)205 tasklet_kill(struct tasklet_struct *ts)
206 {
207
208 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "tasklet_kill() can sleep");
209
210 /* wait until tasklet is no longer busy */
211 while (TASKLET_ST_GET(ts) != TASKLET_ST_IDLE)
212 pause("W", 1);
213 }
214
215 void
tasklet_enable(struct tasklet_struct * ts)216 tasklet_enable(struct tasklet_struct *ts)
217 {
218
219 atomic_dec(&ts->count);
220 }
221
222 void
tasklet_disable(struct tasklet_struct * ts)223 tasklet_disable(struct tasklet_struct *ts)
224 {
225
226 atomic_inc(&ts->count);
227 tasklet_unlock_wait(ts);
228 }
229
230 void
tasklet_disable_nosync(struct tasklet_struct * ts)231 tasklet_disable_nosync(struct tasklet_struct *ts)
232 {
233 atomic_inc(&ts->count);
234 barrier();
235 }
236
237 int
tasklet_trylock(struct tasklet_struct * ts)238 tasklet_trylock(struct tasklet_struct *ts)
239 {
240
241 return (TASKLET_ST_CMPSET(ts, TASKLET_ST_IDLE, TASKLET_ST_BUSY));
242 }
243
244 void
tasklet_unlock(struct tasklet_struct * ts)245 tasklet_unlock(struct tasklet_struct *ts)
246 {
247
248 TASKLET_ST_SET(ts, TASKLET_ST_IDLE);
249 }
250
251 void
tasklet_unlock_wait(struct tasklet_struct * ts)252 tasklet_unlock_wait(struct tasklet_struct *ts)
253 {
254
255 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "tasklet_kill() can sleep");
256
257 /* wait until tasklet is no longer busy */
258 while (TASKLET_ST_GET(ts) != TASKLET_ST_IDLE)
259 pause("W", 1);
260 }
261