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