xref: /freebsd/sys/kern/subr_taskqueue.c (revision b2db760808f74bb53c232900091c9da801ebbfcc)
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 {
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 
66 static void taskqueue_run(struct taskqueue *, struct task **);
67 
68 static __inline void
69 TQ_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 
77 static __inline void
78 TQ_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 
86 static __inline int
87 TQ_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 
95 static 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 
117 struct taskqueue *
118 taskqueue_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  */
128 static void
129 taskqueue_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 
138 void
139 taskqueue_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 
151 int
152 taskqueue_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 
198 void
199 taskqueue_block(struct taskqueue *queue)
200 {
201 
202 	TQ_LOCK(queue);
203 	queue->tq_flags |= TQ_FLAGS_BLOCKED;
204 	TQ_UNLOCK(queue);
205 }
206 
207 void
208 taskqueue_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 
220 static void
221 taskqueue_run(struct taskqueue *queue, struct task **tpp)
222 {
223 	struct task *task;
224 	int pending;
225 
226 	mtx_assert(&queue->tq_mutex, MA_OWNED);
227 	while (STAILQ_FIRST(&queue->tq_queue)) {
228 		/*
229 		 * Carefully remove the first task from the queue and
230 		 * zero its pending count.
231 		 */
232 		task = STAILQ_FIRST(&queue->tq_queue);
233 		STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
234 		pending = task->ta_pending;
235 		task->ta_pending = 0;
236 		task->ta_running = tpp;
237 		*tpp = task;
238 		TQ_UNLOCK(queue);
239 
240 		task->ta_func(task->ta_context, pending);
241 
242 		TQ_LOCK(queue);
243 		*tpp = NULL;
244 		wakeup(task);
245 	}
246 }
247 
248 void
249 taskqueue_drain(struct taskqueue *queue, struct task *task)
250 {
251 
252 	if (!queue->tq_spin)
253 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
254 
255 	TQ_LOCK(queue);
256 	while (task->ta_pending != 0 ||
257 	    (task->ta_running != NULL && task == *task->ta_running)) {
258 		TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0);
259 	}
260 	TQ_UNLOCK(queue);
261 }
262 
263 static void
264 taskqueue_swi_enqueue(void *context)
265 {
266 	swi_sched(taskqueue_ih, 0);
267 }
268 
269 static void
270 taskqueue_swi_run(void *dummy)
271 {
272 	TQ_LOCK(taskqueue_swi);
273 	taskqueue_run(taskqueue_swi, &taskqueue_swi->tq_running);
274 	TQ_UNLOCK(taskqueue_swi);
275 }
276 
277 static void
278 taskqueue_swi_giant_enqueue(void *context)
279 {
280 	swi_sched(taskqueue_giant_ih, 0);
281 }
282 
283 static void
284 taskqueue_swi_giant_run(void *dummy)
285 {
286 	TQ_LOCK(taskqueue_swi_giant);
287 	taskqueue_run(taskqueue_swi_giant, &taskqueue_swi_giant->tq_running);
288 	TQ_UNLOCK(taskqueue_swi_giant);
289 }
290 
291 int
292 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
293 			const char *name, ...)
294 {
295 	va_list ap;
296 	struct thread *td;
297 	struct taskqueue *tq;
298 	int i, error;
299 	char ktname[MAXCOMLEN + 1];
300 
301 	if (count <= 0)
302 		return (EINVAL);
303 
304 	tq = *tqp;
305 
306 	va_start(ap, name);
307 	vsnprintf(ktname, sizeof(ktname), name, ap);
308 	va_end(ap);
309 
310 	tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
311 	    M_NOWAIT | M_ZERO);
312 	if (tq->tq_threads == NULL) {
313 		printf("%s: no memory for %s threads\n", __func__, ktname);
314 		return (ENOMEM);
315 	}
316 
317 	for (i = 0; i < count; i++) {
318 		if (count == 1)
319 			error = kthread_add(taskqueue_thread_loop, tqp, NULL,
320 			    &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
321 		else
322 			error = kthread_add(taskqueue_thread_loop, tqp, NULL,
323 			    &tq->tq_threads[i], RFSTOPPED, 0,
324 			    "%s_%d", ktname, i);
325 		if (error) {
326 			/* should be ok to continue, taskqueue_free will dtrt */
327 			printf("%s: kthread_add(%s): error %d", __func__,
328 			    ktname, error);
329 			tq->tq_threads[i] = NULL;		/* paranoid */
330 		} else
331 			tq->tq_tcount++;
332 	}
333 	for (i = 0; i < count; i++) {
334 		if (tq->tq_threads[i] == NULL)
335 			continue;
336 		td = tq->tq_threads[i];
337 		thread_lock(td);
338 		sched_prio(td, pri);
339 		sched_add(td, SRQ_BORING);
340 		thread_unlock(td);
341 	}
342 
343 	return (0);
344 }
345 
346 void
347 taskqueue_thread_loop(void *arg)
348 {
349 	struct taskqueue **tqp, *tq;
350 	struct task *running;
351 
352 	/*
353 	 * The kernel stack space is globaly addressable, and it would
354 	 * be an error to ask whether a task is running after the
355 	 * taskqueue has been released.  So it is safe to have the
356 	 * task point back to an address in the taskqueue's stack to
357 	 * determine if the task is running.
358 	 */
359 	running = NULL;
360 
361 	tqp = arg;
362 	tq = *tqp;
363 	TQ_LOCK(tq);
364 	while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
365 		taskqueue_run(tq, &running);
366 		/*
367 		 * Because taskqueue_run() can drop tq_mutex, we need to
368 		 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
369 		 * meantime, which means we missed a wakeup.
370 		 */
371 		if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
372 			break;
373 		TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
374 	}
375 
376 	/* rendezvous with thread that asked us to terminate */
377 	tq->tq_tcount--;
378 	wakeup_one(tq->tq_threads);
379 	TQ_UNLOCK(tq);
380 	kthread_exit();
381 }
382 
383 void
384 taskqueue_thread_enqueue(void *context)
385 {
386 	struct taskqueue **tqp, *tq;
387 
388 	tqp = context;
389 	tq = *tqp;
390 
391 	mtx_assert(&tq->tq_mutex, MA_OWNED);
392 	wakeup_one(tq);
393 }
394 
395 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL,
396 		 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
397 		     INTR_MPSAFE, &taskqueue_ih));
398 
399 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL,
400 		 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
401 		     NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
402 
403 TASKQUEUE_DEFINE_THREAD(thread);
404 
405 struct taskqueue *
406 taskqueue_create_fast(const char *name, int mflags,
407 		 taskqueue_enqueue_fn enqueue, void *context)
408 {
409 	return _taskqueue_create(name, mflags, enqueue, context,
410 			MTX_SPIN, "fast_taskqueue");
411 }
412 
413 /* NB: for backwards compatibility */
414 int
415 taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
416 {
417 	return taskqueue_enqueue(queue, task);
418 }
419 
420 static void	*taskqueue_fast_ih;
421 
422 static void
423 taskqueue_fast_enqueue(void *context)
424 {
425 	swi_sched(taskqueue_fast_ih, 0);
426 }
427 
428 static void
429 taskqueue_fast_run(void *dummy)
430 {
431 	TQ_LOCK(taskqueue_fast);
432 	taskqueue_run(taskqueue_fast, &taskqueue_fast->tq_running);
433 	TQ_UNLOCK(taskqueue_fast);
434 }
435 
436 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL,
437 	swi_add(NULL, "Fast task queue", taskqueue_fast_run, NULL,
438 	SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
439 
440 int
441 taskqueue_member(struct taskqueue *queue, struct thread *td)
442 {
443 	int i, j, ret = 0;
444 
445 	TQ_LOCK(queue);
446 	for (i = 0, j = 0; ; i++) {
447 		if (queue->tq_threads[i] == NULL)
448 			continue;
449 		if (queue->tq_threads[i] == td) {
450 			ret = 1;
451 			break;
452 		}
453 		if (++j >= queue->tq_tcount)
454 			break;
455 	}
456 	TQ_UNLOCK(queue);
457 	return (ret);
458 }
459