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_ptr((volatile uintptr_t *)&(ts)->entry.tqe_prev, old, new)
47 
48 #define	TASKLET_ST_SET(ts, new)	\
49 	WRITE_ONCE(*(volatile uintptr_t *)&(ts)->entry.tqe_prev, new)
50 
51 #define	TASKLET_ST_GET(ts) \
52 	READ_ONCE(*(volatile uintptr_t *)&(ts)->entry.tqe_prev)
53 
54 struct tasklet_worker {
55 	struct mtx mtx;
56 	TAILQ_HEAD(, 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 static DPCPU_DEFINE(struct tasklet_worker, tasklet_worker);
64 
65 static void
66 tasklet_handler(void *arg)
67 {
68 	struct tasklet_worker *tw = (struct tasklet_worker *)arg;
69 	struct tasklet_struct *ts;
70 
71 	linux_set_current(curthread);
72 
73 	TASKLET_WORKER_LOCK(tw);
74 	while (1) {
75 		ts = TAILQ_FIRST(&tw->head);
76 		if (ts == NULL)
77 			break;
78 		TAILQ_REMOVE(&tw->head, ts, entry);
79 
80 		TASKLET_WORKER_UNLOCK(tw);
81 		do {
82 			/* reset executing state */
83 			TASKLET_ST_SET(ts, TASKLET_ST_EXEC);
84 
85 			ts->func(ts->data);
86 
87 		} while (TASKLET_ST_CMPSET(ts, TASKLET_ST_EXEC, TASKLET_ST_IDLE) == 0);
88 		TASKLET_WORKER_LOCK(tw);
89 	}
90 	TASKLET_WORKER_UNLOCK(tw);
91 }
92 
93 static void
94 tasklet_subsystem_init(void *arg __unused)
95 {
96 	struct tasklet_worker *tw;
97 	char buf[32];
98 	int i;
99 
100 	CPU_FOREACH(i) {
101 		if (CPU_ABSENT(i))
102 			continue;
103 
104 		tw = DPCPU_ID_PTR(i, tasklet_worker);
105 
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);
112        }
113 }
114 SYSINIT(linux_tasklet, SI_SUB_TASKQ, SI_ORDER_THIRD, tasklet_subsystem_init, NULL);
115 
116 static void
117 tasklet_subsystem_uninit(void *arg __unused)
118 {
119 	struct tasklet_worker *tw;
120 	int i;
121 
122 	CPU_FOREACH(i) {
123 		if (CPU_ABSENT(i))
124 			continue;
125 
126 		tw = DPCPU_ID_PTR(i, tasklet_worker);
127 
128 		taskqgroup_detach(qgroup_softirq, &tw->gtask);
129 		mtx_destroy(&tw->mtx);
130 	}
131 }
132 SYSUNINIT(linux_tasklet, SI_SUB_TASKQ, SI_ORDER_THIRD, tasklet_subsystem_uninit, NULL);
133 
134 void
135 tasklet_init(struct tasklet_struct *ts,
136     tasklet_func_t *func, unsigned long data)
137 {
138 	ts->entry.tqe_prev = NULL;
139 	ts->entry.tqe_next = NULL;
140 	ts->func = func;
141 	ts->data = data;
142 }
143 
144 void
145 local_bh_enable(void)
146 {
147 	sched_unpin();
148 }
149 
150 void
151 local_bh_disable(void)
152 {
153 	sched_pin();
154 }
155 
156 void
157 tasklet_schedule(struct tasklet_struct *ts)
158 {
159 
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;
164 
165 		tw = &DPCPU_GET(tasklet_worker);
166 
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);
174 	} else {
175 		/*
176 		 * tasklet_handler() is already executing
177 		 *
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.
185 		 */
186 	}
187 }
188 
189 void
190 tasklet_kill(struct tasklet_struct *ts)
191 {
192 
193 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "tasklet_kill() can sleep");
194 
195 	/* wait until tasklet is no longer busy */
196 	while (TASKLET_ST_GET(ts) != TASKLET_ST_IDLE)
197 		pause("W", 1);
198 }
199