2 * Copyright (c) 2000 Doug Rabson
3 * Copyright (c) 2014 Jeff Roberson
4 * Copyright (c) 2016 Matthew Macy
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 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/cpuset.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/libkern.h>
37 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
42 #include <sys/epoch.h>
43 #include <sys/sched.h>
45 #include <sys/gtaskqueue.h>
46 #include <sys/unistd.h>
47 #include <machine/stdarg.h>
49 static MALLOC_DEFINE(M_GTASKQUEUE, "gtaskqueue", "Group Task Queues");
50 static void gtaskqueue_thread_enqueue(void *);
51 static void gtaskqueue_thread_loop(void *arg);
52 static int task_is_running(struct gtaskqueue *queue, struct gtask *gtask);
53 static void gtaskqueue_drain_locked(struct gtaskqueue *queue, struct gtask *gtask);
55 TASKQGROUP_DEFINE(softirq, mp_ncpus, 1);
57 struct gtaskqueue_busy {
58 struct gtask *tb_running;
60 LIST_ENTRY(gtaskqueue_busy) tb_link;
63 typedef void (*gtaskqueue_enqueue_fn)(void *context);
66 STAILQ_HEAD(, gtask) tq_queue;
67 LIST_HEAD(, gtaskqueue_busy) tq_active;
70 struct mtx_padalign tq_mutex;
71 gtaskqueue_enqueue_fn tq_enqueue;
74 struct thread **tq_threads;
78 taskqueue_callback_fn tq_callbacks[TASKQUEUE_NUM_CALLBACKS];
79 void *tq_cb_contexts[TASKQUEUE_NUM_CALLBACKS];
82 #define TQ_FLAGS_ACTIVE (1 << 0)
83 #define TQ_FLAGS_BLOCKED (1 << 1)
84 #define TQ_FLAGS_UNLOCKED_ENQUEUE (1 << 2)
86 #define DT_CALLOUT_ARMED (1 << 0)
91 mtx_lock_spin(&(tq)->tq_mutex); \
93 mtx_lock(&(tq)->tq_mutex); \
95 #define TQ_ASSERT_LOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_OWNED)
97 #define TQ_UNLOCK(tq) \
100 mtx_unlock_spin(&(tq)->tq_mutex); \
102 mtx_unlock(&(tq)->tq_mutex); \
104 #define TQ_ASSERT_UNLOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_NOTOWNED)
108 gtask_dump(struct gtask *gtask)
110 printf("gtask: %p ta_flags=%x ta_priority=%d ta_func=%p ta_context=%p\n",
111 gtask, gtask->ta_flags, gtask->ta_priority, gtask->ta_func, gtask->ta_context);
116 TQ_SLEEP(struct gtaskqueue *tq, void *p, const char *wm)
119 return (msleep_spin(p, (struct mtx *)&tq->tq_mutex, wm, 0));
120 return (msleep(p, &tq->tq_mutex, 0, wm, 0));
123 static struct gtaskqueue *
124 _gtaskqueue_create(const char *name, int mflags,
125 taskqueue_enqueue_fn enqueue, void *context,
126 int mtxflags, const char *mtxname __unused)
128 struct gtaskqueue *queue;
131 tq_name = malloc(TASKQUEUE_NAMELEN, M_GTASKQUEUE, mflags | M_ZERO);
135 snprintf(tq_name, TASKQUEUE_NAMELEN, "%s", (name) ? name : "taskqueue");
137 queue = malloc(sizeof(struct gtaskqueue), M_GTASKQUEUE, mflags | M_ZERO);
139 free(tq_name, M_GTASKQUEUE);
143 STAILQ_INIT(&queue->tq_queue);
144 LIST_INIT(&queue->tq_active);
145 queue->tq_enqueue = enqueue;
146 queue->tq_context = context;
147 queue->tq_name = tq_name;
148 queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
149 queue->tq_flags |= TQ_FLAGS_ACTIVE;
150 if (enqueue == gtaskqueue_thread_enqueue)
151 queue->tq_flags |= TQ_FLAGS_UNLOCKED_ENQUEUE;
152 mtx_init(&queue->tq_mutex, tq_name, NULL, mtxflags);
158 * Signal a taskqueue thread to terminate.
161 gtaskqueue_terminate(struct thread **pp, struct gtaskqueue *tq)
164 while (tq->tq_tcount > 0 || tq->tq_callouts > 0) {
166 TQ_SLEEP(tq, pp, "gtq_destroy");
171 gtaskqueue_free(struct gtaskqueue *queue)
175 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
176 gtaskqueue_terminate(queue->tq_threads, queue);
177 KASSERT(LIST_EMPTY(&queue->tq_active), ("Tasks still running?"));
178 KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks"));
179 mtx_destroy(&queue->tq_mutex);
180 free(queue->tq_threads, M_GTASKQUEUE);
181 free(queue->tq_name, M_GTASKQUEUE);
182 free(queue, M_GTASKQUEUE);
186 * Wait for all to complete, then prevent it from being enqueued
189 grouptask_block(struct grouptask *grouptask)
191 struct gtaskqueue *queue = grouptask->gt_taskqueue;
192 struct gtask *gtask = &grouptask->gt_task;
197 panic("queue == NULL");
201 gtask->ta_flags |= TASK_NOENQUEUE;
202 gtaskqueue_drain_locked(queue, gtask);
207 grouptask_unblock(struct grouptask *grouptask)
209 struct gtaskqueue *queue = grouptask->gt_taskqueue;
210 struct gtask *gtask = &grouptask->gt_task;
215 panic("queue == NULL");
219 gtask->ta_flags &= ~TASK_NOENQUEUE;
224 grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *gtask)
229 panic("queue == NULL");
233 if (gtask->ta_flags & TASK_ENQUEUED) {
237 if (gtask->ta_flags & TASK_NOENQUEUE) {
241 STAILQ_INSERT_TAIL(&queue->tq_queue, gtask, ta_link);
242 gtask->ta_flags |= TASK_ENQUEUED;
244 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
245 queue->tq_enqueue(queue->tq_context);
250 gtaskqueue_task_nop_fn(void *context)
255 * Block until all currently queued tasks in this taskqueue
256 * have begun execution. Tasks queued during execution of
257 * this function are ignored.
260 gtaskqueue_drain_tq_queue(struct gtaskqueue *queue)
262 struct gtask t_barrier;
264 if (STAILQ_EMPTY(&queue->tq_queue))
268 * Enqueue our barrier after all current tasks, but with
269 * the highest priority so that newly queued tasks cannot
270 * pass it. Because of the high priority, we can not use
271 * taskqueue_enqueue_locked directly (which drops the lock
272 * anyway) so just insert it at tail while we have the
275 GTASK_INIT(&t_barrier, 0, USHRT_MAX, gtaskqueue_task_nop_fn, &t_barrier);
276 STAILQ_INSERT_TAIL(&queue->tq_queue, &t_barrier, ta_link);
277 t_barrier.ta_flags |= TASK_ENQUEUED;
280 * Once the barrier has executed, all previously queued tasks
281 * have completed or are currently executing.
283 while (t_barrier.ta_flags & TASK_ENQUEUED)
284 TQ_SLEEP(queue, &t_barrier, "gtq_qdrain");
288 * Block until all currently executing tasks for this taskqueue
289 * complete. Tasks that begin execution during the execution
290 * of this function are ignored.
293 gtaskqueue_drain_tq_active(struct gtaskqueue *queue)
295 struct gtaskqueue_busy *tb;
298 if (LIST_EMPTY(&queue->tq_active))
301 /* Block taskq_terminate().*/
302 queue->tq_callouts++;
304 /* Wait for any active task with sequence from the past. */
307 LIST_FOREACH(tb, &queue->tq_active, tb_link) {
308 if ((int)(tb->tb_seq - seq) <= 0) {
309 TQ_SLEEP(queue, tb->tb_running, "gtq_adrain");
314 /* Release taskqueue_terminate(). */
315 queue->tq_callouts--;
316 if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0)
317 wakeup_one(queue->tq_threads);
321 gtaskqueue_block(struct gtaskqueue *queue)
325 queue->tq_flags |= TQ_FLAGS_BLOCKED;
330 gtaskqueue_unblock(struct gtaskqueue *queue)
334 queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
335 if (!STAILQ_EMPTY(&queue->tq_queue))
336 queue->tq_enqueue(queue->tq_context);
341 gtaskqueue_run_locked(struct gtaskqueue *queue)
343 struct epoch_tracker et;
344 struct gtaskqueue_busy tb;
348 KASSERT(queue != NULL, ("tq is NULL"));
349 TQ_ASSERT_LOCKED(queue);
350 tb.tb_running = NULL;
351 LIST_INSERT_HEAD(&queue->tq_active, &tb, tb_link);
352 in_net_epoch = false;
354 while ((gtask = STAILQ_FIRST(&queue->tq_queue)) != NULL) {
355 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
356 gtask->ta_flags &= ~TASK_ENQUEUED;
357 tb.tb_running = gtask;
358 tb.tb_seq = ++queue->tq_seq;
361 KASSERT(gtask->ta_func != NULL, ("task->ta_func is NULL"));
362 if (!in_net_epoch && TASK_IS_NET(gtask)) {
365 } else if (in_net_epoch && !TASK_IS_NET(gtask)) {
367 in_net_epoch = false;
369 gtask->ta_func(gtask->ta_context);
376 LIST_REMOVE(&tb, tb_link);
380 task_is_running(struct gtaskqueue *queue, struct gtask *gtask)
382 struct gtaskqueue_busy *tb;
384 TQ_ASSERT_LOCKED(queue);
385 LIST_FOREACH(tb, &queue->tq_active, tb_link) {
386 if (tb->tb_running == gtask)
393 gtaskqueue_cancel_locked(struct gtaskqueue *queue, struct gtask *gtask)
396 if (gtask->ta_flags & TASK_ENQUEUED)
397 STAILQ_REMOVE(&queue->tq_queue, gtask, gtask, ta_link);
398 gtask->ta_flags &= ~TASK_ENQUEUED;
399 return (task_is_running(queue, gtask) ? EBUSY : 0);
403 gtaskqueue_cancel(struct gtaskqueue *queue, struct gtask *gtask)
408 error = gtaskqueue_cancel_locked(queue, gtask);
415 gtaskqueue_drain_locked(struct gtaskqueue *queue, struct gtask *gtask)
417 while ((gtask->ta_flags & TASK_ENQUEUED) || task_is_running(queue, gtask))
418 TQ_SLEEP(queue, gtask, "gtq_drain");
422 gtaskqueue_drain(struct gtaskqueue *queue, struct gtask *gtask)
426 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
429 gtaskqueue_drain_locked(queue, gtask);
434 gtaskqueue_drain_all(struct gtaskqueue *queue)
438 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
441 gtaskqueue_drain_tq_queue(queue);
442 gtaskqueue_drain_tq_active(queue);
447 _gtaskqueue_start_threads(struct gtaskqueue **tqp, int count, int pri,
448 cpuset_t *mask, const char *name, va_list ap)
450 char ktname[MAXCOMLEN + 1];
452 struct gtaskqueue *tq;
458 vsnprintf(ktname, sizeof(ktname), name, ap);
461 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_GTASKQUEUE,
463 if (tq->tq_threads == NULL) {
464 printf("%s: no memory for %s threads\n", __func__, ktname);
468 for (i = 0; i < count; i++) {
470 error = kthread_add(gtaskqueue_thread_loop, tqp, NULL,
471 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
473 error = kthread_add(gtaskqueue_thread_loop, tqp, NULL,
474 &tq->tq_threads[i], RFSTOPPED, 0,
477 /* should be ok to continue, taskqueue_free will dtrt */
478 printf("%s: kthread_add(%s): error %d", __func__,
480 tq->tq_threads[i] = NULL; /* paranoid */
484 for (i = 0; i < count; i++) {
485 if (tq->tq_threads[i] == NULL)
487 td = tq->tq_threads[i];
489 error = cpuset_setthread(td->td_tid, mask);
491 * Failing to pin is rarely an actual fatal error;
492 * it'll just affect performance.
495 printf("%s: curthread=%llu: can't pin; "
498 (unsigned long long) td->td_tid,
503 sched_add(td, SRQ_BORING);
510 gtaskqueue_start_threads(struct gtaskqueue **tqp, int count, int pri,
511 const char *name, ...)
517 error = _gtaskqueue_start_threads(tqp, count, pri, NULL, name, ap);
523 gtaskqueue_run_callback(struct gtaskqueue *tq,
524 enum taskqueue_callback_type cb_type)
526 taskqueue_callback_fn tq_callback;
528 TQ_ASSERT_UNLOCKED(tq);
529 tq_callback = tq->tq_callbacks[cb_type];
530 if (tq_callback != NULL)
531 tq_callback(tq->tq_cb_contexts[cb_type]);
535 gtaskqueue_thread_loop(void *arg)
537 struct gtaskqueue **tqp, *tq;
541 gtaskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT);
543 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
545 gtaskqueue_run_locked(tq);
547 * Because taskqueue_run() can drop tq_mutex, we need to
548 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
549 * meantime, which means we missed a wakeup.
551 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
553 TQ_SLEEP(tq, tq, "-");
555 gtaskqueue_run_locked(tq);
557 * This thread is on its way out, so just drop the lock temporarily
558 * in order to call the shutdown callback. This allows the callback
559 * to look at the taskqueue, even just before it dies.
562 gtaskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN);
565 /* rendezvous with thread that asked us to terminate */
567 wakeup_one(tq->tq_threads);
573 gtaskqueue_thread_enqueue(void *context)
575 struct gtaskqueue **tqp, *tq;
582 static struct gtaskqueue *
583 gtaskqueue_create_fast(const char *name, int mflags,
584 taskqueue_enqueue_fn enqueue, void *context)
586 return _gtaskqueue_create(name, mflags, enqueue, context,
587 MTX_SPIN, "fast_taskqueue");
590 struct taskqgroup_cpu {
591 LIST_HEAD(, grouptask) tgc_tasks;
592 struct gtaskqueue *tgc_taskq;
598 struct taskqgroup_cpu tqg_queue[MAXCPU];
600 const char * tqg_name;
604 struct taskq_bind_task {
605 struct gtask bt_task;
610 taskqgroup_cpu_create(struct taskqgroup *qgroup, int idx, int cpu)
612 struct taskqgroup_cpu *qcpu;
614 qcpu = &qgroup->tqg_queue[idx];
615 LIST_INIT(&qcpu->tgc_tasks);
616 qcpu->tgc_taskq = gtaskqueue_create_fast(NULL, M_WAITOK,
617 gtaskqueue_thread_enqueue, &qcpu->tgc_taskq);
618 gtaskqueue_start_threads(&qcpu->tgc_taskq, 1, PI_SOFT,
619 "%s_%d", qgroup->tqg_name, idx);
624 * Find the taskq with least # of tasks that doesn't currently have any
625 * other queues from the uniq identifier.
628 taskqgroup_find(struct taskqgroup *qgroup, void *uniq)
634 mtx_assert(&qgroup->tqg_lock, MA_OWNED);
635 KASSERT(qgroup->tqg_cnt != 0,
636 ("qgroup %s has no queues", qgroup->tqg_name));
639 * Two passes: first scan for a queue with the least tasks that
640 * does not already service this uniq id. If that fails simply find
641 * the queue with the least total tasks.
643 for (idx = -1, mincnt = INT_MAX, strict = 1; mincnt == INT_MAX;
645 for (i = 0; i < qgroup->tqg_cnt; i++) {
646 if (qgroup->tqg_queue[i].tgc_cnt > mincnt)
649 LIST_FOREACH(n, &qgroup->tqg_queue[i].tgc_tasks,
651 if (n->gt_uniq == uniq)
656 mincnt = qgroup->tqg_queue[i].tgc_cnt;
661 panic("%s: failed to pick a qid.", __func__);
667 taskqgroup_attach(struct taskqgroup *qgroup, struct grouptask *gtask,
668 void *uniq, device_t dev, struct resource *irq, const char *name)
672 KASSERT(qgroup->tqg_cnt > 0,
673 ("qgroup %s has no queues", qgroup->tqg_name));
675 gtask->gt_uniq = uniq;
676 snprintf(gtask->gt_name, GROUPTASK_NAMELEN, "%s", name ? name : "grouptask");
680 mtx_lock(&qgroup->tqg_lock);
681 qid = taskqgroup_find(qgroup, uniq);
682 qgroup->tqg_queue[qid].tgc_cnt++;
683 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
684 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
685 if (dev != NULL && irq != NULL) {
686 cpu = qgroup->tqg_queue[qid].tgc_cpu;
688 mtx_unlock(&qgroup->tqg_lock);
689 error = bus_bind_intr(dev, irq, cpu);
691 printf("%s: binding interrupt failed for %s: %d\n",
692 __func__, gtask->gt_name, error);
694 mtx_unlock(&qgroup->tqg_lock);
698 taskqgroup_attach_cpu(struct taskqgroup *qgroup, struct grouptask *gtask,
699 void *uniq, int cpu, device_t dev, struct resource *irq, const char *name)
703 gtask->gt_uniq = uniq;
704 snprintf(gtask->gt_name, GROUPTASK_NAMELEN, "%s", name ? name : "grouptask");
708 mtx_lock(&qgroup->tqg_lock);
709 for (i = 0, qid = -1; i < qgroup->tqg_cnt; i++)
710 if (qgroup->tqg_queue[i].tgc_cpu == cpu) {
715 mtx_unlock(&qgroup->tqg_lock);
716 printf("%s: qid not found for %s cpu=%d\n", __func__, gtask->gt_name, cpu);
719 qgroup->tqg_queue[qid].tgc_cnt++;
720 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
721 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
722 cpu = qgroup->tqg_queue[qid].tgc_cpu;
723 mtx_unlock(&qgroup->tqg_lock);
725 if (dev != NULL && irq != NULL) {
726 error = bus_bind_intr(dev, irq, cpu);
728 printf("%s: binding interrupt failed for %s: %d\n",
729 __func__, gtask->gt_name, error);
735 taskqgroup_detach(struct taskqgroup *qgroup, struct grouptask *gtask)
739 grouptask_block(gtask);
740 mtx_lock(&qgroup->tqg_lock);
741 for (i = 0; i < qgroup->tqg_cnt; i++)
742 if (qgroup->tqg_queue[i].tgc_taskq == gtask->gt_taskqueue)
744 if (i == qgroup->tqg_cnt)
745 panic("%s: task %s not in group", __func__, gtask->gt_name);
746 qgroup->tqg_queue[i].tgc_cnt--;
747 LIST_REMOVE(gtask, gt_list);
748 mtx_unlock(&qgroup->tqg_lock);
749 gtask->gt_taskqueue = NULL;
750 gtask->gt_task.ta_flags &= ~TASK_NOENQUEUE;
754 taskqgroup_binder(void *ctx)
756 struct taskq_bind_task *gtask;
762 CPU_SET(gtask->bt_cpuid, &mask);
763 error = cpuset_setthread(curthread->td_tid, &mask);
764 thread_lock(curthread);
765 sched_bind(curthread, gtask->bt_cpuid);
766 thread_unlock(curthread);
769 printf("%s: binding curthread failed: %d\n", __func__, error);
770 free(gtask, M_DEVBUF);
774 taskqgroup_bind(struct taskqgroup *qgroup)
776 struct taskq_bind_task *gtask;
780 * Bind taskqueue threads to specific CPUs, if they have been assigned
783 if (qgroup->tqg_cnt == 1)
786 for (i = 0; i < qgroup->tqg_cnt; i++) {
787 gtask = malloc(sizeof(*gtask), M_DEVBUF, M_WAITOK);
788 GTASK_INIT(>ask->bt_task, 0, 0, taskqgroup_binder, gtask);
789 gtask->bt_cpuid = qgroup->tqg_queue[i].tgc_cpu;
790 grouptaskqueue_enqueue(qgroup->tqg_queue[i].tgc_taskq,
796 taskqgroup_create(const char *name, int cnt, int stride)
798 struct taskqgroup *qgroup;
801 qgroup = malloc(sizeof(*qgroup), M_GTASKQUEUE, M_WAITOK | M_ZERO);
802 mtx_init(&qgroup->tqg_lock, "taskqgroup", NULL, MTX_DEF);
803 qgroup->tqg_name = name;
804 qgroup->tqg_cnt = cnt;
806 for (cpu = i = 0; i < cnt; i++) {
807 taskqgroup_cpu_create(qgroup, i, cpu);
808 for (j = 0; j < stride; j++)
815 taskqgroup_destroy(struct taskqgroup *qgroup)
820 taskqgroup_drain_all(struct taskqgroup *tqg)
822 struct gtaskqueue *q;
824 for (int i = 0; i < mp_ncpus; i++) {
825 q = tqg->tqg_queue[i].tgc_taskq;
828 gtaskqueue_drain_all(q);