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