2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Doug Rabson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/cpuset.h>
36 #include <sys/interrupt.h>
37 #include <sys/kernel.h>
38 #include <sys/kthread.h>
39 #include <sys/libkern.h>
40 #include <sys/limits.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
45 #include <sys/epoch.h>
46 #include <sys/sched.h>
48 #include <sys/taskqueue.h>
49 #include <sys/unistd.h>
50 #include <machine/stdarg.h>
52 static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
53 static void *taskqueue_giant_ih;
54 static void *taskqueue_ih;
55 static void taskqueue_fast_enqueue(void *);
56 static void taskqueue_swi_enqueue(void *);
57 static void taskqueue_swi_giant_enqueue(void *);
59 struct taskqueue_busy {
60 struct task *tb_running;
62 LIST_ENTRY(taskqueue_busy) tb_link;
66 STAILQ_HEAD(, task) tq_queue;
67 LIST_HEAD(, taskqueue_busy) tq_active;
71 struct mtx_padalign tq_mutex;
72 taskqueue_enqueue_fn tq_enqueue;
75 struct thread **tq_threads;
79 taskqueue_callback_fn tq_callbacks[TASKQUEUE_NUM_CALLBACKS];
80 void *tq_cb_contexts[TASKQUEUE_NUM_CALLBACKS];
83 #define TQ_FLAGS_ACTIVE (1 << 0)
84 #define TQ_FLAGS_BLOCKED (1 << 1)
85 #define TQ_FLAGS_UNLOCKED_ENQUEUE (1 << 2)
87 #define DT_CALLOUT_ARMED (1 << 0)
88 #define DT_DRAIN_IN_PROGRESS (1 << 1)
93 mtx_lock_spin(&(tq)->tq_mutex); \
95 mtx_lock(&(tq)->tq_mutex); \
97 #define TQ_ASSERT_LOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_OWNED)
99 #define TQ_UNLOCK(tq) \
102 mtx_unlock_spin(&(tq)->tq_mutex); \
104 mtx_unlock(&(tq)->tq_mutex); \
106 #define TQ_ASSERT_UNLOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_NOTOWNED)
109 _timeout_task_init(struct taskqueue *queue, struct timeout_task *timeout_task,
110 int priority, task_fn_t func, void *context)
113 TASK_INIT(&timeout_task->t, priority, func, context);
114 callout_init_mtx(&timeout_task->c, &queue->tq_mutex,
115 CALLOUT_RETURNUNLOCKED);
116 timeout_task->q = queue;
121 TQ_SLEEP(struct taskqueue *tq, void *p, const char *wm)
124 return (msleep_spin(p, (struct mtx *)&tq->tq_mutex, wm, 0));
125 return (msleep(p, &tq->tq_mutex, 0, wm, 0));
128 static struct taskqueue *
129 _taskqueue_create(const char *name, int mflags,
130 taskqueue_enqueue_fn enqueue, void *context,
131 int mtxflags, const char *mtxname __unused)
133 struct taskqueue *queue;
136 tq_name = malloc(TASKQUEUE_NAMELEN, M_TASKQUEUE, mflags | M_ZERO);
140 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
142 free(tq_name, M_TASKQUEUE);
146 snprintf(tq_name, TASKQUEUE_NAMELEN, "%s", (name) ? name : "taskqueue");
148 STAILQ_INIT(&queue->tq_queue);
149 LIST_INIT(&queue->tq_active);
150 queue->tq_enqueue = enqueue;
151 queue->tq_context = context;
152 queue->tq_name = tq_name;
153 queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
154 queue->tq_flags |= TQ_FLAGS_ACTIVE;
155 if (enqueue == taskqueue_fast_enqueue ||
156 enqueue == taskqueue_swi_enqueue ||
157 enqueue == taskqueue_swi_giant_enqueue ||
158 enqueue == taskqueue_thread_enqueue)
159 queue->tq_flags |= TQ_FLAGS_UNLOCKED_ENQUEUE;
160 mtx_init(&queue->tq_mutex, tq_name, NULL, mtxflags);
166 taskqueue_create(const char *name, int mflags,
167 taskqueue_enqueue_fn enqueue, void *context)
170 return _taskqueue_create(name, mflags, enqueue, context,
175 taskqueue_set_callback(struct taskqueue *queue,
176 enum taskqueue_callback_type cb_type, taskqueue_callback_fn callback,
180 KASSERT(((cb_type >= TASKQUEUE_CALLBACK_TYPE_MIN) &&
181 (cb_type <= TASKQUEUE_CALLBACK_TYPE_MAX)),
182 ("Callback type %d not valid, must be %d-%d", cb_type,
183 TASKQUEUE_CALLBACK_TYPE_MIN, TASKQUEUE_CALLBACK_TYPE_MAX));
184 KASSERT((queue->tq_callbacks[cb_type] == NULL),
185 ("Re-initialization of taskqueue callback?"));
187 queue->tq_callbacks[cb_type] = callback;
188 queue->tq_cb_contexts[cb_type] = context;
192 * Signal a taskqueue thread to terminate.
195 taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
198 while (tq->tq_tcount > 0 || tq->tq_callouts > 0) {
200 TQ_SLEEP(tq, pp, "tq_destroy");
205 taskqueue_free(struct taskqueue *queue)
209 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
210 taskqueue_terminate(queue->tq_threads, queue);
211 KASSERT(LIST_EMPTY(&queue->tq_active), ("Tasks still running?"));
212 KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks"));
213 mtx_destroy(&queue->tq_mutex);
214 free(queue->tq_threads, M_TASKQUEUE);
215 free(queue->tq_name, M_TASKQUEUE);
216 free(queue, M_TASKQUEUE);
220 taskqueue_enqueue_locked(struct taskqueue *queue, struct task *task)
225 KASSERT(task->ta_func != NULL, ("enqueueing task with NULL func"));
227 * Count multiple enqueues.
229 if (task->ta_pending) {
230 if (task->ta_pending < USHRT_MAX)
237 * Optimise cases when all tasks use small set of priorities.
238 * In case of only one priority we always insert at the end.
239 * In case of two tq_hint typically gives the insertion point.
240 * In case of more then two tq_hint should halve the search.
242 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
243 if (!prev || prev->ta_priority >= task->ta_priority) {
244 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
246 prev = queue->tq_hint;
247 if (prev && prev->ta_priority >= task->ta_priority) {
248 ins = STAILQ_NEXT(prev, ta_link);
251 ins = STAILQ_FIRST(&queue->tq_queue);
253 for (; ins; prev = ins, ins = STAILQ_NEXT(ins, ta_link))
254 if (ins->ta_priority < task->ta_priority)
258 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
259 queue->tq_hint = task;
261 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
264 task->ta_pending = 1;
265 if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) != 0)
267 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
268 queue->tq_enqueue(queue->tq_context);
269 if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) == 0)
272 /* Return with lock released. */
277 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
282 res = taskqueue_enqueue_locked(queue, task);
283 /* The lock is released inside. */
289 taskqueue_timeout_func(void *arg)
291 struct taskqueue *queue;
292 struct timeout_task *timeout_task;
295 queue = timeout_task->q;
296 KASSERT((timeout_task->f & DT_CALLOUT_ARMED) != 0, ("Stray timeout"));
297 timeout_task->f &= ~DT_CALLOUT_ARMED;
298 queue->tq_callouts--;
299 taskqueue_enqueue_locked(timeout_task->q, &timeout_task->t);
300 /* The lock is released inside. */
304 taskqueue_enqueue_timeout_sbt(struct taskqueue *queue,
305 struct timeout_task *timeout_task, sbintime_t sbt, sbintime_t pr, int flags)
310 KASSERT(timeout_task->q == NULL || timeout_task->q == queue,
312 KASSERT(!queue->tq_spin, ("Timeout for spin-queue"));
313 timeout_task->q = queue;
314 res = timeout_task->t.ta_pending;
315 if (timeout_task->f & DT_DRAIN_IN_PROGRESS) {
319 } else if (sbt == 0) {
320 taskqueue_enqueue_locked(queue, &timeout_task->t);
321 /* The lock is released inside. */
323 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
326 queue->tq_callouts++;
327 timeout_task->f |= DT_CALLOUT_ARMED;
329 sbt = -sbt; /* Ignore overflow. */
332 callout_reset_sbt(&timeout_task->c, sbt, pr,
333 taskqueue_timeout_func, timeout_task, flags);
341 taskqueue_enqueue_timeout(struct taskqueue *queue,
342 struct timeout_task *ttask, int ticks)
345 return (taskqueue_enqueue_timeout_sbt(queue, ttask, ticks * tick_sbt,
350 taskqueue_task_nop_fn(void *context, int pending)
355 * Block until all currently queued tasks in this taskqueue
356 * have begun execution. Tasks queued during execution of
357 * this function are ignored.
360 taskqueue_drain_tq_queue(struct taskqueue *queue)
362 struct task t_barrier;
364 if (STAILQ_EMPTY(&queue->tq_queue))
368 * Enqueue our barrier after all current tasks, but with
369 * the highest priority so that newly queued tasks cannot
370 * pass it. Because of the high priority, we can not use
371 * taskqueue_enqueue_locked directly (which drops the lock
372 * anyway) so just insert it at tail while we have the
375 TASK_INIT(&t_barrier, UCHAR_MAX, taskqueue_task_nop_fn, &t_barrier);
376 STAILQ_INSERT_TAIL(&queue->tq_queue, &t_barrier, ta_link);
377 queue->tq_hint = &t_barrier;
378 t_barrier.ta_pending = 1;
381 * Once the barrier has executed, all previously queued tasks
382 * have completed or are currently executing.
384 while (t_barrier.ta_pending != 0)
385 TQ_SLEEP(queue, &t_barrier, "tq_qdrain");
390 * Block until all currently executing tasks for this taskqueue
391 * complete. Tasks that begin execution during the execution
392 * of this function are ignored.
395 taskqueue_drain_tq_active(struct taskqueue *queue)
397 struct taskqueue_busy *tb;
400 if (LIST_EMPTY(&queue->tq_active))
403 /* Block taskq_terminate().*/
404 queue->tq_callouts++;
406 /* Wait for any active task with sequence from the past. */
409 LIST_FOREACH(tb, &queue->tq_active, tb_link) {
410 if ((int)(tb->tb_seq - seq) <= 0) {
411 TQ_SLEEP(queue, tb->tb_running, "tq_adrain");
416 /* Release taskqueue_terminate(). */
417 queue->tq_callouts--;
418 if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0)
419 wakeup_one(queue->tq_threads);
424 taskqueue_block(struct taskqueue *queue)
428 queue->tq_flags |= TQ_FLAGS_BLOCKED;
433 taskqueue_unblock(struct taskqueue *queue)
437 queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
438 if (!STAILQ_EMPTY(&queue->tq_queue))
439 queue->tq_enqueue(queue->tq_context);
444 taskqueue_run_locked(struct taskqueue *queue)
446 struct epoch_tracker et;
447 struct taskqueue_busy tb;
452 KASSERT(queue != NULL, ("tq is NULL"));
453 TQ_ASSERT_LOCKED(queue);
454 tb.tb_running = NULL;
455 LIST_INSERT_HEAD(&queue->tq_active, &tb, tb_link);
456 in_net_epoch = false;
458 while ((task = STAILQ_FIRST(&queue->tq_queue)) != NULL) {
459 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
460 if (queue->tq_hint == task)
461 queue->tq_hint = NULL;
462 pending = task->ta_pending;
463 task->ta_pending = 0;
464 tb.tb_running = task;
465 tb.tb_seq = ++queue->tq_seq;
468 KASSERT(task->ta_func != NULL, ("task->ta_func is NULL"));
469 if (!in_net_epoch && TASK_IS_NET(task)) {
472 } else if (in_net_epoch && !TASK_IS_NET(task)) {
474 in_net_epoch = false;
476 task->ta_func(task->ta_context, pending);
483 LIST_REMOVE(&tb, tb_link);
487 taskqueue_run(struct taskqueue *queue)
491 taskqueue_run_locked(queue);
496 task_is_running(struct taskqueue *queue, struct task *task)
498 struct taskqueue_busy *tb;
500 TQ_ASSERT_LOCKED(queue);
501 LIST_FOREACH(tb, &queue->tq_active, tb_link) {
502 if (tb->tb_running == task)
509 * Only use this function in single threaded contexts. It returns
510 * non-zero if the given task is either pending or running. Else the
511 * task is idle and can be queued again or freed.
514 taskqueue_poll_is_busy(struct taskqueue *queue, struct task *task)
519 retval = task->ta_pending > 0 || task_is_running(queue, task);
526 taskqueue_cancel_locked(struct taskqueue *queue, struct task *task,
530 if (task->ta_pending > 0) {
531 STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link);
532 if (queue->tq_hint == task)
533 queue->tq_hint = NULL;
536 *pendp = task->ta_pending;
537 task->ta_pending = 0;
538 return (task_is_running(queue, task) ? EBUSY : 0);
542 taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp)
547 error = taskqueue_cancel_locked(queue, task, pendp);
554 taskqueue_cancel_timeout(struct taskqueue *queue,
555 struct timeout_task *timeout_task, u_int *pendp)
557 u_int pending, pending1;
561 pending = !!(callout_stop(&timeout_task->c) > 0);
562 error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1);
563 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
564 timeout_task->f &= ~DT_CALLOUT_ARMED;
565 queue->tq_callouts--;
570 *pendp = pending + pending1;
575 taskqueue_drain(struct taskqueue *queue, struct task *task)
579 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
582 while (task->ta_pending != 0 || task_is_running(queue, task))
583 TQ_SLEEP(queue, task, "tq_drain");
588 taskqueue_drain_all(struct taskqueue *queue)
592 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
595 (void)taskqueue_drain_tq_queue(queue);
596 (void)taskqueue_drain_tq_active(queue);
601 taskqueue_drain_timeout(struct taskqueue *queue,
602 struct timeout_task *timeout_task)
606 * Set flag to prevent timer from re-starting during drain:
609 KASSERT((timeout_task->f & DT_DRAIN_IN_PROGRESS) == 0,
610 ("Drain already in progress"));
611 timeout_task->f |= DT_DRAIN_IN_PROGRESS;
614 callout_drain(&timeout_task->c);
615 taskqueue_drain(queue, &timeout_task->t);
618 * Clear flag to allow timer to re-start:
621 timeout_task->f &= ~DT_DRAIN_IN_PROGRESS;
626 taskqueue_quiesce(struct taskqueue *queue)
632 ret = taskqueue_drain_tq_queue(queue);
634 ret = taskqueue_drain_tq_active(queue);
640 taskqueue_swi_enqueue(void *context)
642 swi_sched(taskqueue_ih, 0);
646 taskqueue_swi_run(void *dummy)
648 taskqueue_run(taskqueue_swi);
652 taskqueue_swi_giant_enqueue(void *context)
654 swi_sched(taskqueue_giant_ih, 0);
658 taskqueue_swi_giant_run(void *dummy)
660 taskqueue_run(taskqueue_swi_giant);
664 _taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
665 cpuset_t *mask, struct proc *p, const char *name, va_list ap)
667 char ktname[MAXCOMLEN + 1];
669 struct taskqueue *tq;
675 vsnprintf(ktname, sizeof(ktname), name, ap);
678 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
680 if (tq->tq_threads == NULL) {
681 printf("%s: no memory for %s threads\n", __func__, ktname);
685 for (i = 0; i < count; i++) {
687 error = kthread_add(taskqueue_thread_loop, tqp, p,
688 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
690 error = kthread_add(taskqueue_thread_loop, tqp, p,
691 &tq->tq_threads[i], RFSTOPPED, 0,
694 /* should be ok to continue, taskqueue_free will dtrt */
695 printf("%s: kthread_add(%s): error %d", __func__,
697 tq->tq_threads[i] = NULL; /* paranoid */
701 if (tq->tq_tcount == 0) {
702 free(tq->tq_threads, M_TASKQUEUE);
703 tq->tq_threads = NULL;
706 for (i = 0; i < count; i++) {
707 if (tq->tq_threads[i] == NULL)
709 td = tq->tq_threads[i];
711 error = cpuset_setthread(td->td_tid, mask);
713 * Failing to pin is rarely an actual fatal error;
714 * it'll just affect performance.
717 printf("%s: curthread=%llu: can't pin; "
720 (unsigned long long) td->td_tid,
725 sched_add(td, SRQ_BORING);
732 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
733 const char *name, ...)
739 error = _taskqueue_start_threads(tqp, count, pri, NULL, NULL, name, ap);
745 taskqueue_start_threads_in_proc(struct taskqueue **tqp, int count, int pri,
746 struct proc *proc, const char *name, ...)
752 error = _taskqueue_start_threads(tqp, count, pri, NULL, proc, name, ap);
758 taskqueue_start_threads_cpuset(struct taskqueue **tqp, int count, int pri,
759 cpuset_t *mask, const char *name, ...)
765 error = _taskqueue_start_threads(tqp, count, pri, mask, NULL, name, ap);
771 taskqueue_run_callback(struct taskqueue *tq,
772 enum taskqueue_callback_type cb_type)
774 taskqueue_callback_fn tq_callback;
776 TQ_ASSERT_UNLOCKED(tq);
777 tq_callback = tq->tq_callbacks[cb_type];
778 if (tq_callback != NULL)
779 tq_callback(tq->tq_cb_contexts[cb_type]);
783 taskqueue_thread_loop(void *arg)
785 struct taskqueue **tqp, *tq;
789 taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT);
791 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
793 taskqueue_run_locked(tq);
795 * Because taskqueue_run() can drop tq_mutex, we need to
796 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
797 * meantime, which means we missed a wakeup.
799 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
801 TQ_SLEEP(tq, tq, "-");
803 taskqueue_run_locked(tq);
805 * This thread is on its way out, so just drop the lock temporarily
806 * in order to call the shutdown callback. This allows the callback
807 * to look at the taskqueue, even just before it dies.
810 taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN);
813 /* rendezvous with thread that asked us to terminate */
815 wakeup_one(tq->tq_threads);
821 taskqueue_thread_enqueue(void *context)
823 struct taskqueue **tqp, *tq;
830 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL,
831 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
832 INTR_MPSAFE, &taskqueue_ih));
834 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL,
835 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
836 NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
838 TASKQUEUE_DEFINE_THREAD(thread);
841 taskqueue_create_fast(const char *name, int mflags,
842 taskqueue_enqueue_fn enqueue, void *context)
844 return _taskqueue_create(name, mflags, enqueue, context,
845 MTX_SPIN, "fast_taskqueue");
848 static void *taskqueue_fast_ih;
851 taskqueue_fast_enqueue(void *context)
853 swi_sched(taskqueue_fast_ih, 0);
857 taskqueue_fast_run(void *dummy)
859 taskqueue_run(taskqueue_fast);
862 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL,
863 swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL,
864 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
867 taskqueue_member(struct taskqueue *queue, struct thread *td)
871 for (i = 0, j = 0; ; i++) {
872 if (queue->tq_threads[i] == NULL)
874 if (queue->tq_threads[i] == td) {
878 if (++j >= queue->tq_tcount)