xref: /freebsd/sys/kern/subr_taskqueue.c (revision a3cf0ef5a295c885c895fabfd56470c0d1db322d)
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/sched.h>
41 #include <sys/taskqueue.h>
42 #include <sys/unistd.h>
43 #include <machine/stdarg.h>
44 
45 static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
46 static void	*taskqueue_giant_ih;
47 static void	*taskqueue_ih;
48 
49 struct taskqueue_busy {
50 	struct task	*tb_running;
51 	TAILQ_ENTRY(taskqueue_busy) tb_link;
52 };
53 
54 struct taskqueue {
55 	STAILQ_HEAD(, task)	tq_queue;
56 	const char		*tq_name;
57 	taskqueue_enqueue_fn	tq_enqueue;
58 	void			*tq_context;
59 	TAILQ_HEAD(, taskqueue_busy) tq_active;
60 	struct mtx		tq_mutex;
61 	struct thread		**tq_threads;
62 	int			tq_tcount;
63 	int			tq_spin;
64 	int			tq_flags;
65 };
66 
67 #define	TQ_FLAGS_ACTIVE		(1 << 0)
68 #define	TQ_FLAGS_BLOCKED	(1 << 1)
69 #define	TQ_FLAGS_PENDING	(1 << 2)
70 
71 #define	TQ_LOCK(tq)							\
72 	do {								\
73 		if ((tq)->tq_spin)					\
74 			mtx_lock_spin(&(tq)->tq_mutex);			\
75 		else							\
76 			mtx_lock(&(tq)->tq_mutex);			\
77 	} while (0)
78 
79 #define	TQ_UNLOCK(tq)							\
80 	do {								\
81 		if ((tq)->tq_spin)					\
82 			mtx_unlock_spin(&(tq)->tq_mutex);		\
83 		else							\
84 			mtx_unlock(&(tq)->tq_mutex);			\
85 	} while (0)
86 
87 static __inline int
88 TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
89     int t)
90 {
91 	if (tq->tq_spin)
92 		return (msleep_spin(p, m, wm, t));
93 	return (msleep(p, m, pri, wm, t));
94 }
95 
96 static struct taskqueue *
97 _taskqueue_create(const char *name, int mflags,
98 		 taskqueue_enqueue_fn enqueue, void *context,
99 		 int mtxflags, const char *mtxname)
100 {
101 	struct taskqueue *queue;
102 
103 	queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
104 	if (!queue)
105 		return NULL;
106 
107 	STAILQ_INIT(&queue->tq_queue);
108 	TAILQ_INIT(&queue->tq_active);
109 	queue->tq_name = name;
110 	queue->tq_enqueue = enqueue;
111 	queue->tq_context = context;
112 	queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
113 	queue->tq_flags |= TQ_FLAGS_ACTIVE;
114 	mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags);
115 
116 	return queue;
117 }
118 
119 struct taskqueue *
120 taskqueue_create(const char *name, int mflags,
121 		 taskqueue_enqueue_fn enqueue, void *context)
122 {
123 	return _taskqueue_create(name, mflags, enqueue, context,
124 			MTX_DEF, "taskqueue");
125 }
126 
127 /*
128  * Signal a taskqueue thread to terminate.
129  */
130 static void
131 taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
132 {
133 
134 	while (tq->tq_tcount > 0) {
135 		wakeup(tq);
136 		TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0);
137 	}
138 }
139 
140 void
141 taskqueue_free(struct taskqueue *queue)
142 {
143 
144 	TQ_LOCK(queue);
145 	queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
146 	taskqueue_terminate(queue->tq_threads, queue);
147 	KASSERT(TAILQ_EMPTY(&queue->tq_active), ("Tasks still running?"));
148 	mtx_destroy(&queue->tq_mutex);
149 	free(queue->tq_threads, M_TASKQUEUE);
150 	free(queue, M_TASKQUEUE);
151 }
152 
153 int
154 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
155 {
156 	struct task *ins;
157 	struct task *prev;
158 
159 	TQ_LOCK(queue);
160 
161 	/*
162 	 * Count multiple enqueues.
163 	 */
164 	if (task->ta_pending) {
165 		task->ta_pending++;
166 		TQ_UNLOCK(queue);
167 		return 0;
168 	}
169 
170 	/*
171 	 * Optimise the case when all tasks have the same priority.
172 	 */
173 	prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
174 	if (!prev || prev->ta_priority >= task->ta_priority) {
175 		STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
176 	} else {
177 		prev = NULL;
178 		for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
179 		     prev = ins, ins = STAILQ_NEXT(ins, ta_link))
180 			if (ins->ta_priority < task->ta_priority)
181 				break;
182 
183 		if (prev)
184 			STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
185 		else
186 			STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
187 	}
188 
189 	task->ta_pending = 1;
190 	if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
191 		queue->tq_enqueue(queue->tq_context);
192 	else
193 		queue->tq_flags |= TQ_FLAGS_PENDING;
194 
195 	TQ_UNLOCK(queue);
196 
197 	return 0;
198 }
199 
200 void
201 taskqueue_block(struct taskqueue *queue)
202 {
203 
204 	TQ_LOCK(queue);
205 	queue->tq_flags |= TQ_FLAGS_BLOCKED;
206 	TQ_UNLOCK(queue);
207 }
208 
209 void
210 taskqueue_unblock(struct taskqueue *queue)
211 {
212 
213 	TQ_LOCK(queue);
214 	queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
215 	if (queue->tq_flags & TQ_FLAGS_PENDING) {
216 		queue->tq_flags &= ~TQ_FLAGS_PENDING;
217 		queue->tq_enqueue(queue->tq_context);
218 	}
219 	TQ_UNLOCK(queue);
220 }
221 
222 static void
223 taskqueue_run_locked(struct taskqueue *queue)
224 {
225 	struct taskqueue_busy tb;
226 	struct task *task;
227 	int pending;
228 
229 	mtx_assert(&queue->tq_mutex, MA_OWNED);
230 	tb.tb_running = NULL;
231 	TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link);
232 
233 	while (STAILQ_FIRST(&queue->tq_queue)) {
234 		/*
235 		 * Carefully remove the first task from the queue and
236 		 * zero its pending count.
237 		 */
238 		task = STAILQ_FIRST(&queue->tq_queue);
239 		STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
240 		pending = task->ta_pending;
241 		task->ta_pending = 0;
242 		tb.tb_running = task;
243 		TQ_UNLOCK(queue);
244 
245 		task->ta_func(task->ta_context, pending);
246 
247 		TQ_LOCK(queue);
248 		tb.tb_running = NULL;
249 		wakeup(task);
250 	}
251 	TAILQ_REMOVE(&queue->tq_active, &tb, tb_link);
252 }
253 
254 void
255 taskqueue_run(struct taskqueue *queue)
256 {
257 
258 	TQ_LOCK(queue);
259 	taskqueue_run_locked(queue);
260 	TQ_UNLOCK(queue);
261 }
262 
263 static int
264 task_is_running(struct taskqueue *queue, struct task *task)
265 {
266 	struct taskqueue_busy *tb;
267 
268 	mtx_assert(&queue->tq_mutex, MA_OWNED);
269 	TAILQ_FOREACH(tb, &queue->tq_active, tb_link) {
270 		if (tb->tb_running == task)
271 			return (1);
272 	}
273 	return (0);
274 }
275 
276 int
277 taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp)
278 {
279 	u_int pending;
280 	int error;
281 
282 	TQ_LOCK(queue);
283 	if ((pending = task->ta_pending) > 0)
284 		STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link);
285 	task->ta_pending = 0;
286 	error = task_is_running(queue, task) ? EBUSY : 0;
287 	TQ_UNLOCK(queue);
288 
289 	if (pendp != NULL)
290 		*pendp = pending;
291 	return (error);
292 }
293 
294 void
295 taskqueue_drain(struct taskqueue *queue, struct task *task)
296 {
297 
298 	if (!queue->tq_spin)
299 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
300 
301 	TQ_LOCK(queue);
302 	while (task->ta_pending != 0 || task_is_running(queue, task))
303 		TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0);
304 	TQ_UNLOCK(queue);
305 }
306 
307 static void
308 taskqueue_swi_enqueue(void *context)
309 {
310 	swi_sched(taskqueue_ih, 0);
311 }
312 
313 static void
314 taskqueue_swi_run(void *dummy)
315 {
316 	taskqueue_run(taskqueue_swi);
317 }
318 
319 static void
320 taskqueue_swi_giant_enqueue(void *context)
321 {
322 	swi_sched(taskqueue_giant_ih, 0);
323 }
324 
325 static void
326 taskqueue_swi_giant_run(void *dummy)
327 {
328 	taskqueue_run(taskqueue_swi_giant);
329 }
330 
331 int
332 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
333 			const char *name, ...)
334 {
335 	va_list ap;
336 	struct thread *td;
337 	struct taskqueue *tq;
338 	int i, error;
339 	char ktname[MAXCOMLEN + 1];
340 
341 	if (count <= 0)
342 		return (EINVAL);
343 
344 	tq = *tqp;
345 
346 	va_start(ap, name);
347 	vsnprintf(ktname, sizeof(ktname), name, ap);
348 	va_end(ap);
349 
350 	tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
351 	    M_NOWAIT | M_ZERO);
352 	if (tq->tq_threads == NULL) {
353 		printf("%s: no memory for %s threads\n", __func__, ktname);
354 		return (ENOMEM);
355 	}
356 
357 	for (i = 0; i < count; i++) {
358 		if (count == 1)
359 			error = kthread_add(taskqueue_thread_loop, tqp, NULL,
360 			    &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
361 		else
362 			error = kthread_add(taskqueue_thread_loop, tqp, NULL,
363 			    &tq->tq_threads[i], RFSTOPPED, 0,
364 			    "%s_%d", ktname, i);
365 		if (error) {
366 			/* should be ok to continue, taskqueue_free will dtrt */
367 			printf("%s: kthread_add(%s): error %d", __func__,
368 			    ktname, error);
369 			tq->tq_threads[i] = NULL;		/* paranoid */
370 		} else
371 			tq->tq_tcount++;
372 	}
373 	for (i = 0; i < count; i++) {
374 		if (tq->tq_threads[i] == NULL)
375 			continue;
376 		td = tq->tq_threads[i];
377 		thread_lock(td);
378 		sched_prio(td, pri);
379 		sched_add(td, SRQ_BORING);
380 		thread_unlock(td);
381 	}
382 
383 	return (0);
384 }
385 
386 void
387 taskqueue_thread_loop(void *arg)
388 {
389 	struct taskqueue **tqp, *tq;
390 
391 	tqp = arg;
392 	tq = *tqp;
393 	TQ_LOCK(tq);
394 	while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
395 		taskqueue_run_locked(tq);
396 		/*
397 		 * Because taskqueue_run() can drop tq_mutex, we need to
398 		 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
399 		 * meantime, which means we missed a wakeup.
400 		 */
401 		if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
402 			break;
403 		TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
404 	}
405 	taskqueue_run_locked(tq);
406 
407 	/* rendezvous with thread that asked us to terminate */
408 	tq->tq_tcount--;
409 	wakeup_one(tq->tq_threads);
410 	TQ_UNLOCK(tq);
411 	kthread_exit();
412 }
413 
414 void
415 taskqueue_thread_enqueue(void *context)
416 {
417 	struct taskqueue **tqp, *tq;
418 
419 	tqp = context;
420 	tq = *tqp;
421 
422 	mtx_assert(&tq->tq_mutex, MA_OWNED);
423 	wakeup_one(tq);
424 }
425 
426 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL,
427 		 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
428 		     INTR_MPSAFE, &taskqueue_ih));
429 
430 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL,
431 		 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
432 		     NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
433 
434 TASKQUEUE_DEFINE_THREAD(thread);
435 
436 struct taskqueue *
437 taskqueue_create_fast(const char *name, int mflags,
438 		 taskqueue_enqueue_fn enqueue, void *context)
439 {
440 	return _taskqueue_create(name, mflags, enqueue, context,
441 			MTX_SPIN, "fast_taskqueue");
442 }
443 
444 /* NB: for backwards compatibility */
445 int
446 taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
447 {
448 	return taskqueue_enqueue(queue, task);
449 }
450 
451 static void	*taskqueue_fast_ih;
452 
453 static void
454 taskqueue_fast_enqueue(void *context)
455 {
456 	swi_sched(taskqueue_fast_ih, 0);
457 }
458 
459 static void
460 taskqueue_fast_run(void *dummy)
461 {
462 	taskqueue_run(taskqueue_fast);
463 }
464 
465 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL,
466 	swi_add(NULL, "Fast task queue", taskqueue_fast_run, NULL,
467 	SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
468 
469 int
470 taskqueue_member(struct taskqueue *queue, struct thread *td)
471 {
472 	int i, j, ret = 0;
473 
474 	TQ_LOCK(queue);
475 	for (i = 0, j = 0; ; i++) {
476 		if (queue->tq_threads[i] == NULL)
477 			continue;
478 		if (queue->tq_threads[i] == td) {
479 			ret = 1;
480 			break;
481 		}
482 		if (++j >= queue->tq_tcount)
483 			break;
484 	}
485 	TQ_UNLOCK(queue);
486 	return (ret);
487 }
488