xref: /freebsd/sys/kern/subr_taskqueue.c (revision 262e143bd46171a6415a5b28af260a5efa2a3db8)
1 /*-
2  * Copyright (c) 2000 Doug Rabson
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, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/interrupt.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/proc.h>
40 #include <sys/taskqueue.h>
41 #include <sys/unistd.h>
42 
43 static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
44 static void	*taskqueue_giant_ih;
45 static void	*taskqueue_ih;
46 static STAILQ_HEAD(taskqueue_list, taskqueue) taskqueue_queues;
47 static struct mtx taskqueue_queues_mutex;
48 
49 struct taskqueue {
50 	STAILQ_ENTRY(taskqueue)	tq_link;
51 	STAILQ_HEAD(, task)	tq_queue;
52 	const char		*tq_name;
53 	taskqueue_enqueue_fn	tq_enqueue;
54 	void			*tq_context;
55 	struct task		*tq_running;
56 	struct mtx		tq_mutex;
57 	struct proc		**tq_pproc;
58 };
59 
60 static void	init_taskqueue_list(void *data);
61 
62 static void
63 init_taskqueue_list(void *data __unused)
64 {
65 
66 	mtx_init(&taskqueue_queues_mutex, "taskqueue list", NULL, MTX_DEF);
67 	STAILQ_INIT(&taskqueue_queues);
68 }
69 SYSINIT(taskqueue_list, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_taskqueue_list,
70     NULL);
71 
72 struct taskqueue *
73 taskqueue_create(const char *name, int mflags,
74 		 taskqueue_enqueue_fn enqueue, void *context,
75 		 struct proc **pp)
76 {
77 	struct taskqueue *queue;
78 
79 	queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
80 	if (!queue)
81 		return 0;
82 
83 	STAILQ_INIT(&queue->tq_queue);
84 	queue->tq_name = name;
85 	queue->tq_enqueue = enqueue;
86 	queue->tq_context = context;
87 	queue->tq_pproc = pp;
88 	mtx_init(&queue->tq_mutex, "taskqueue", NULL, MTX_DEF);
89 
90 	mtx_lock(&taskqueue_queues_mutex);
91 	STAILQ_INSERT_TAIL(&taskqueue_queues, queue, tq_link);
92 	mtx_unlock(&taskqueue_queues_mutex);
93 
94 	return queue;
95 }
96 
97 /*
98  * Signal a taskqueue thread to terminate.
99  */
100 static void
101 taskqueue_terminate(struct proc **pp, struct taskqueue *tq)
102 {
103 	struct proc *p;
104 
105 	p = *pp;
106 	*pp = NULL;
107 	if (p) {
108 		wakeup_one(tq);
109 		PROC_LOCK(p);		   /* NB: insure we don't miss wakeup */
110 		mtx_unlock(&tq->tq_mutex); /* let taskqueue thread run */
111 		msleep(p, &p->p_mtx, PWAIT, "taskqueue_destroy", 0);
112 		PROC_UNLOCK(p);
113 		mtx_lock(&tq->tq_mutex);
114 	}
115 }
116 
117 void
118 taskqueue_free(struct taskqueue *queue)
119 {
120 
121 	mtx_lock(&taskqueue_queues_mutex);
122 	STAILQ_REMOVE(&taskqueue_queues, queue, taskqueue, tq_link);
123 	mtx_unlock(&taskqueue_queues_mutex);
124 
125 	mtx_lock(&queue->tq_mutex);
126 	taskqueue_run(queue);
127 	taskqueue_terminate(queue->tq_pproc, queue);
128 	mtx_destroy(&queue->tq_mutex);
129 	free(queue, M_TASKQUEUE);
130 }
131 
132 /*
133  * Returns with the taskqueue locked.
134  */
135 struct taskqueue *
136 taskqueue_find(const char *name)
137 {
138 	struct taskqueue *queue;
139 
140 	mtx_lock(&taskqueue_queues_mutex);
141 	STAILQ_FOREACH(queue, &taskqueue_queues, tq_link) {
142 		if (strcmp(queue->tq_name, name) == 0) {
143 			mtx_lock(&queue->tq_mutex);
144 			mtx_unlock(&taskqueue_queues_mutex);
145 			return queue;
146 		}
147 	}
148 	mtx_unlock(&taskqueue_queues_mutex);
149 	return NULL;
150 }
151 
152 int
153 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
154 {
155 	struct task *ins;
156 	struct task *prev;
157 
158 	mtx_lock(&queue->tq_mutex);
159 
160 	/*
161 	 * Count multiple enqueues.
162 	 */
163 	if (task->ta_pending) {
164 		task->ta_pending++;
165 		mtx_unlock(&queue->tq_mutex);
166 		return 0;
167 	}
168 
169 	/*
170 	 * Optimise the case when all tasks have the same priority.
171 	 */
172 	prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
173 	if (!prev || prev->ta_priority >= task->ta_priority) {
174 		STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
175 	} else {
176 		prev = 0;
177 		for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
178 		     prev = ins, ins = STAILQ_NEXT(ins, ta_link))
179 			if (ins->ta_priority < task->ta_priority)
180 				break;
181 
182 		if (prev)
183 			STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
184 		else
185 			STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
186 	}
187 
188 	task->ta_pending = 1;
189 	queue->tq_enqueue(queue->tq_context);
190 
191 	mtx_unlock(&queue->tq_mutex);
192 
193 	return 0;
194 }
195 
196 void
197 taskqueue_run(struct taskqueue *queue)
198 {
199 	struct task *task;
200 	int owned, pending;
201 
202 	owned = mtx_owned(&queue->tq_mutex);
203 	if (!owned)
204 		mtx_lock(&queue->tq_mutex);
205 	while (STAILQ_FIRST(&queue->tq_queue)) {
206 		/*
207 		 * Carefully remove the first task from the queue and
208 		 * zero its pending count.
209 		 */
210 		task = STAILQ_FIRST(&queue->tq_queue);
211 		STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
212 		pending = task->ta_pending;
213 		task->ta_pending = 0;
214 		queue->tq_running = task;
215 		mtx_unlock(&queue->tq_mutex);
216 
217 		task->ta_func(task->ta_context, pending);
218 
219 		mtx_lock(&queue->tq_mutex);
220 		queue->tq_running = NULL;
221 		wakeup(task);
222 	}
223 
224 	/*
225 	 * For compatibility, unlock on return if the queue was not locked
226 	 * on entry, although this opens a race window.
227 	 */
228 	if (!owned)
229 		mtx_unlock(&queue->tq_mutex);
230 }
231 
232 void
233 taskqueue_drain(struct taskqueue *queue, struct task *task)
234 {
235 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "taskqueue_drain");
236 
237 	mtx_lock(&queue->tq_mutex);
238 	while (task->ta_pending != 0 || task == queue->tq_running)
239 		msleep(task, &queue->tq_mutex, PWAIT, "-", 0);
240 	mtx_unlock(&queue->tq_mutex);
241 }
242 
243 static void
244 taskqueue_swi_enqueue(void *context)
245 {
246 	swi_sched(taskqueue_ih, 0);
247 }
248 
249 static void
250 taskqueue_swi_run(void *dummy)
251 {
252 	taskqueue_run(taskqueue_swi);
253 }
254 
255 static void
256 taskqueue_swi_giant_enqueue(void *context)
257 {
258 	swi_sched(taskqueue_giant_ih, 0);
259 }
260 
261 static void
262 taskqueue_swi_giant_run(void *dummy)
263 {
264 	taskqueue_run(taskqueue_swi_giant);
265 }
266 
267 void
268 taskqueue_thread_loop(void *arg)
269 {
270 	struct taskqueue **tqp, *tq;
271 
272 	tqp = arg;
273 	tq = *tqp;
274 	mtx_lock(&tq->tq_mutex);
275 	do {
276 		taskqueue_run(tq);
277 		msleep(tq, &tq->tq_mutex, PWAIT, "-", 0);
278 	} while (*tq->tq_pproc != NULL);
279 
280 	/* rendezvous with thread that asked us to terminate */
281 	wakeup_one(tq);
282 	mtx_unlock(&tq->tq_mutex);
283 	kthread_exit(0);
284 }
285 
286 void
287 taskqueue_thread_enqueue(void *context)
288 {
289 	struct taskqueue **tqp, *tq;
290 
291 	tqp = context;
292 	tq = *tqp;
293 
294 	mtx_assert(&tq->tq_mutex, MA_OWNED);
295 	wakeup_one(tq);
296 }
297 
298 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, 0,
299 		 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
300 		     INTR_MPSAFE, &taskqueue_ih));
301 
302 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, 0,
303 		 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
304 		     NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
305 
306 TASKQUEUE_DEFINE_THREAD(thread);
307 
308 int
309 taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
310 {
311 	struct task *ins;
312 	struct task *prev;
313 
314 	mtx_lock_spin(&queue->tq_mutex);
315 
316 	/*
317 	 * Count multiple enqueues.
318 	 */
319 	if (task->ta_pending) {
320 		task->ta_pending++;
321 		mtx_unlock_spin(&queue->tq_mutex);
322 		return 0;
323 	}
324 
325 	/*
326 	 * Optimise the case when all tasks have the same priority.
327 	 */
328 	prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
329 	if (!prev || prev->ta_priority >= task->ta_priority) {
330 		STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
331 	} else {
332 		prev = 0;
333 		for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
334 		     prev = ins, ins = STAILQ_NEXT(ins, ta_link))
335 			if (ins->ta_priority < task->ta_priority)
336 				break;
337 
338 		if (prev)
339 			STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
340 		else
341 			STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
342 	}
343 
344 	task->ta_pending = 1;
345 	queue->tq_enqueue(queue->tq_context);
346 
347 	mtx_unlock_spin(&queue->tq_mutex);
348 
349 	return 0;
350 }
351 
352 static void
353 taskqueue_run_fast(struct taskqueue *queue)
354 {
355 	struct task *task;
356 	int pending;
357 
358 	mtx_lock_spin(&queue->tq_mutex);
359 	while (STAILQ_FIRST(&queue->tq_queue)) {
360 		/*
361 		 * Carefully remove the first task from the queue and
362 		 * zero its pending count.
363 		 */
364 		task = STAILQ_FIRST(&queue->tq_queue);
365 		STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
366 		pending = task->ta_pending;
367 		task->ta_pending = 0;
368 		mtx_unlock_spin(&queue->tq_mutex);
369 
370 		task->ta_func(task->ta_context, pending);
371 
372 		mtx_lock_spin(&queue->tq_mutex);
373 	}
374 	mtx_unlock_spin(&queue->tq_mutex);
375 }
376 
377 struct taskqueue *taskqueue_fast;
378 static void	*taskqueue_fast_ih;
379 
380 static void
381 taskqueue_fast_schedule(void *context)
382 {
383 	swi_sched(taskqueue_fast_ih, 0);
384 }
385 
386 static void
387 taskqueue_fast_run(void *dummy)
388 {
389 	taskqueue_run_fast(taskqueue_fast);
390 }
391 
392 static void
393 taskqueue_define_fast(void *arg)
394 {
395 
396 	taskqueue_fast = malloc(sizeof(struct taskqueue), M_TASKQUEUE,
397 	    M_NOWAIT | M_ZERO);
398 	if (!taskqueue_fast) {
399 		printf("%s: Unable to allocate fast task queue!\n", __func__);
400 		return;
401 	}
402 
403 	STAILQ_INIT(&taskqueue_fast->tq_queue);
404 	taskqueue_fast->tq_name = "fast";
405 	taskqueue_fast->tq_enqueue = taskqueue_fast_schedule;
406 	mtx_init(&taskqueue_fast->tq_mutex, "taskqueue_fast", NULL, MTX_SPIN);
407 
408 	mtx_lock(&taskqueue_queues_mutex);
409 	STAILQ_INSERT_TAIL(&taskqueue_queues, taskqueue_fast, tq_link);
410 	mtx_unlock(&taskqueue_queues_mutex);
411 
412 	swi_add(NULL, "Fast taskq", taskqueue_fast_run,
413 		NULL, SWI_TQ_FAST, 0, &taskqueue_fast_ih);
414 }
415 SYSINIT(taskqueue_fast, SI_SUB_CONFIGURE, SI_ORDER_SECOND,
416     taskqueue_define_fast, NULL);
417