2 * Copyright (c) 2017 Hans Petter Selasky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/malloc.h>
32 #include <sys/gtaskqueue.h>
34 #include <sys/sched.h>
36 #include <linux/compiler.h>
37 #include <linux/interrupt.h>
38 #include <linux/compat.h>
40 #define TASKLET_ST_IDLE 0
41 #define TASKLET_ST_BUSY 1
42 #define TASKLET_ST_EXEC 2
43 #define TASKLET_ST_LOOP 3
45 #define TASKLET_ST_CMPSET(ts, old, new) \
46 atomic_cmpset_ptr((volatile uintptr_t *)&(ts)->entry.tqe_prev, old, new)
48 #define TASKLET_ST_SET(ts, new) \
49 WRITE_ONCE(*(volatile uintptr_t *)&(ts)->entry.tqe_prev, new)
51 #define TASKLET_ST_GET(ts) \
52 READ_ONCE(*(volatile uintptr_t *)&(ts)->entry.tqe_prev)
54 struct tasklet_worker {
56 TAILQ_HEAD(, tasklet_struct) head;
57 struct grouptask gtask;
58 } __aligned(CACHE_LINE_SIZE);
60 #define TASKLET_WORKER_LOCK(tw) mtx_lock(&(tw)->mtx)
61 #define TASKLET_WORKER_UNLOCK(tw) mtx_unlock(&(tw)->mtx)
63 static DPCPU_DEFINE(struct tasklet_worker, tasklet_worker);
66 tasklet_handler(void *arg)
68 struct tasklet_worker *tw = (struct tasklet_worker *)arg;
69 struct tasklet_struct *ts;
71 linux_set_current(curthread);
73 TASKLET_WORKER_LOCK(tw);
75 ts = TAILQ_FIRST(&tw->head);
78 TAILQ_REMOVE(&tw->head, ts, entry);
80 TASKLET_WORKER_UNLOCK(tw);
82 /* reset executing state */
83 TASKLET_ST_SET(ts, TASKLET_ST_EXEC);
87 } while (TASKLET_ST_CMPSET(ts, TASKLET_ST_EXEC, TASKLET_ST_IDLE) == 0);
88 TASKLET_WORKER_LOCK(tw);
90 TASKLET_WORKER_UNLOCK(tw);
94 tasklet_subsystem_init(void *arg __unused)
96 struct tasklet_worker *tw;
104 tw = DPCPU_ID_PTR(i, tasklet_worker);
106 mtx_init(&tw->mtx, "linux_tasklet", NULL, MTX_DEF);
107 TAILQ_INIT(&tw->head);
108 GROUPTASK_INIT(&tw->gtask, 0, tasklet_handler, tw);
109 snprintf(buf, sizeof(buf), "softirq%d", i);
110 taskqgroup_attach_cpu(qgroup_softirq, &tw->gtask,
111 "tasklet", i, -1, buf);
114 SYSINIT(linux_tasklet, SI_SUB_TASKQ, SI_ORDER_THIRD, tasklet_subsystem_init, NULL);
117 tasklet_subsystem_uninit(void *arg __unused)
119 struct tasklet_worker *tw;
126 tw = DPCPU_ID_PTR(i, tasklet_worker);
128 taskqgroup_detach(qgroup_softirq, &tw->gtask);
129 mtx_destroy(&tw->mtx);
132 SYSUNINIT(linux_tasklet, SI_SUB_TASKQ, SI_ORDER_THIRD, tasklet_subsystem_uninit, NULL);
135 tasklet_init(struct tasklet_struct *ts,
136 tasklet_func_t *func, unsigned long data)
138 ts->entry.tqe_prev = NULL;
139 ts->entry.tqe_next = NULL;
145 local_bh_enable(void)
151 local_bh_disable(void)
157 tasklet_schedule(struct tasklet_struct *ts)
160 if (TASKLET_ST_CMPSET(ts, TASKLET_ST_EXEC, TASKLET_ST_LOOP)) {
161 /* tasklet_handler() will loop */
162 } else if (TASKLET_ST_CMPSET(ts, TASKLET_ST_IDLE, TASKLET_ST_BUSY)) {
163 struct tasklet_worker *tw;
165 tw = &DPCPU_GET(tasklet_worker);
167 /* tasklet_handler() was not queued */
168 TASKLET_WORKER_LOCK(tw);
169 /* enqueue tasklet */
170 TAILQ_INSERT_TAIL(&tw->head, ts, entry);
171 /* schedule worker */
172 GROUPTASK_ENQUEUE(&tw->gtask);
173 TASKLET_WORKER_UNLOCK(tw);
176 * tasklet_handler() is already executing
178 * If the state is neither EXEC nor IDLE, it is either
179 * LOOP or BUSY. If the state changed between the two
180 * CMPSET's above the only possible transitions by
181 * elimination are LOOP->EXEC and BUSY->EXEC. If a
182 * EXEC->LOOP transition was missed that is not a
183 * problem because the callback function is then
184 * already about to be called again.
190 tasklet_kill(struct tasklet_struct *ts)
193 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "tasklet_kill() can sleep");
195 /* wait until tasklet is no longer busy */
196 while (TASKLET_ST_GET(ts) != TASKLET_ST_IDLE)