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/param.h>
30 #include <sys/systm.h>
32 #include <sys/cpuset.h>
33 #include <sys/kernel.h>
34 #include <sys/kthread.h>
35 #include <sys/libkern.h>
36 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
41 #include <sys/epoch.h>
42 #include <sys/sched.h>
44 #include <sys/gtaskqueue.h>
45 #include <sys/unistd.h>
46 #include <machine/stdarg.h>
48 static MALLOC_DEFINE(M_GTASKQUEUE, "gtaskqueue", "Group Task Queues");
49 static void gtaskqueue_thread_enqueue(void *);
50 static void gtaskqueue_thread_loop(void *arg);
51 static int task_is_running(struct gtaskqueue *queue, struct gtask *gtask);
52 static void gtaskqueue_drain_locked(struct gtaskqueue *queue, struct gtask *gtask);
54 TASKQGROUP_DEFINE(softirq, mp_ncpus, 1);
56 struct gtaskqueue_busy {
57 struct gtask *tb_running;
59 LIST_ENTRY(gtaskqueue_busy) tb_link;
62 typedef void (*gtaskqueue_enqueue_fn)(void *context);
65 STAILQ_HEAD(, gtask) tq_queue;
66 LIST_HEAD(, gtaskqueue_busy) tq_active;
69 struct mtx_padalign tq_mutex;
70 gtaskqueue_enqueue_fn tq_enqueue;
73 struct thread **tq_threads;
77 taskqueue_callback_fn tq_callbacks[TASKQUEUE_NUM_CALLBACKS];
78 void *tq_cb_contexts[TASKQUEUE_NUM_CALLBACKS];
81 #define TQ_FLAGS_ACTIVE (1 << 0)
82 #define TQ_FLAGS_BLOCKED (1 << 1)
83 #define TQ_FLAGS_UNLOCKED_ENQUEUE (1 << 2)
85 #define DT_CALLOUT_ARMED (1 << 0)
90 mtx_lock_spin(&(tq)->tq_mutex); \
92 mtx_lock(&(tq)->tq_mutex); \
94 #define TQ_ASSERT_LOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_OWNED)
96 #define TQ_UNLOCK(tq) \
99 mtx_unlock_spin(&(tq)->tq_mutex); \
101 mtx_unlock(&(tq)->tq_mutex); \
103 #define TQ_ASSERT_UNLOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_NOTOWNED)
107 gtask_dump(struct gtask *gtask)
109 printf("gtask: %p ta_flags=%x ta_priority=%d ta_func=%p ta_context=%p\n",
110 gtask, gtask->ta_flags, gtask->ta_priority, gtask->ta_func, gtask->ta_context);
115 TQ_SLEEP(struct gtaskqueue *tq, void *p, const char *wm)
118 return (msleep_spin(p, (struct mtx *)&tq->tq_mutex, wm, 0));
119 return (msleep(p, &tq->tq_mutex, 0, wm, 0));
122 static struct gtaskqueue *
123 _gtaskqueue_create(const char *name, int mflags,
124 taskqueue_enqueue_fn enqueue, void *context,
125 int mtxflags, const char *mtxname __unused)
127 struct gtaskqueue *queue;
130 tq_name = malloc(TASKQUEUE_NAMELEN, M_GTASKQUEUE, mflags | M_ZERO);
134 snprintf(tq_name, TASKQUEUE_NAMELEN, "%s", (name) ? name : "taskqueue");
136 queue = malloc(sizeof(struct gtaskqueue), M_GTASKQUEUE, mflags | M_ZERO);
138 free(tq_name, M_GTASKQUEUE);
142 STAILQ_INIT(&queue->tq_queue);
143 LIST_INIT(&queue->tq_active);
144 queue->tq_enqueue = enqueue;
145 queue->tq_context = context;
146 queue->tq_name = tq_name;
147 queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
148 queue->tq_flags |= TQ_FLAGS_ACTIVE;
149 if (enqueue == gtaskqueue_thread_enqueue)
150 queue->tq_flags |= TQ_FLAGS_UNLOCKED_ENQUEUE;
151 mtx_init(&queue->tq_mutex, tq_name, NULL, mtxflags);
157 * Signal a taskqueue thread to terminate.
160 gtaskqueue_terminate(struct thread **pp, struct gtaskqueue *tq)
163 while (tq->tq_tcount > 0 || tq->tq_callouts > 0) {
165 TQ_SLEEP(tq, pp, "gtq_destroy");
170 gtaskqueue_free(struct gtaskqueue *queue)
174 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
175 gtaskqueue_terminate(queue->tq_threads, queue);
176 KASSERT(LIST_EMPTY(&queue->tq_active), ("Tasks still running?"));
177 KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks"));
178 mtx_destroy(&queue->tq_mutex);
179 free(queue->tq_threads, M_GTASKQUEUE);
180 free(queue->tq_name, M_GTASKQUEUE);
181 free(queue, M_GTASKQUEUE);
185 * Wait for all to complete, then prevent it from being enqueued
188 grouptask_block(struct grouptask *grouptask)
190 struct gtaskqueue *queue = grouptask->gt_taskqueue;
191 struct gtask *gtask = &grouptask->gt_task;
196 panic("queue == NULL");
200 gtask->ta_flags |= TASK_NOENQUEUE;
201 gtaskqueue_drain_locked(queue, gtask);
206 grouptask_unblock(struct grouptask *grouptask)
208 struct gtaskqueue *queue = grouptask->gt_taskqueue;
209 struct gtask *gtask = &grouptask->gt_task;
214 panic("queue == NULL");
218 gtask->ta_flags &= ~TASK_NOENQUEUE;
223 grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *gtask)
228 panic("queue == NULL");
232 if (gtask->ta_flags & TASK_ENQUEUED) {
236 if (gtask->ta_flags & TASK_NOENQUEUE) {
240 STAILQ_INSERT_TAIL(&queue->tq_queue, gtask, ta_link);
241 gtask->ta_flags |= TASK_ENQUEUED;
243 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
244 queue->tq_enqueue(queue->tq_context);
249 gtaskqueue_task_nop_fn(void *context)
254 * Block until all currently queued tasks in this taskqueue
255 * have begun execution. Tasks queued during execution of
256 * this function are ignored.
259 gtaskqueue_drain_tq_queue(struct gtaskqueue *queue)
261 struct gtask t_barrier;
263 if (STAILQ_EMPTY(&queue->tq_queue))
267 * Enqueue our barrier after all current tasks, but with
268 * the highest priority so that newly queued tasks cannot
269 * pass it. Because of the high priority, we can not use
270 * taskqueue_enqueue_locked directly (which drops the lock
271 * anyway) so just insert it at tail while we have the
274 GTASK_INIT(&t_barrier, 0, USHRT_MAX, gtaskqueue_task_nop_fn, &t_barrier);
275 STAILQ_INSERT_TAIL(&queue->tq_queue, &t_barrier, ta_link);
276 t_barrier.ta_flags |= TASK_ENQUEUED;
279 * Once the barrier has executed, all previously queued tasks
280 * have completed or are currently executing.
282 while (t_barrier.ta_flags & TASK_ENQUEUED)
283 TQ_SLEEP(queue, &t_barrier, "gtq_qdrain");
287 * Block until all currently executing tasks for this taskqueue
288 * complete. Tasks that begin execution during the execution
289 * of this function are ignored.
292 gtaskqueue_drain_tq_active(struct gtaskqueue *queue)
294 struct gtaskqueue_busy *tb;
297 if (LIST_EMPTY(&queue->tq_active))
300 /* Block taskq_terminate().*/
301 queue->tq_callouts++;
303 /* Wait for any active task with sequence from the past. */
306 LIST_FOREACH(tb, &queue->tq_active, tb_link) {
307 if ((int)(tb->tb_seq - seq) <= 0) {
308 TQ_SLEEP(queue, tb->tb_running, "gtq_adrain");
313 /* Release taskqueue_terminate(). */
314 queue->tq_callouts--;
315 if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0)
316 wakeup_one(queue->tq_threads);
320 gtaskqueue_block(struct gtaskqueue *queue)
324 queue->tq_flags |= TQ_FLAGS_BLOCKED;
329 gtaskqueue_unblock(struct gtaskqueue *queue)
333 queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
334 if (!STAILQ_EMPTY(&queue->tq_queue))
335 queue->tq_enqueue(queue->tq_context);
340 gtaskqueue_run_locked(struct gtaskqueue *queue)
342 struct epoch_tracker et;
343 struct gtaskqueue_busy tb;
347 KASSERT(queue != NULL, ("tq is NULL"));
348 TQ_ASSERT_LOCKED(queue);
349 tb.tb_running = NULL;
350 LIST_INSERT_HEAD(&queue->tq_active, &tb, tb_link);
351 in_net_epoch = false;
353 while ((gtask = STAILQ_FIRST(&queue->tq_queue)) != NULL) {
354 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
355 gtask->ta_flags &= ~TASK_ENQUEUED;
356 tb.tb_running = gtask;
357 tb.tb_seq = ++queue->tq_seq;
360 KASSERT(gtask->ta_func != NULL, ("task->ta_func is NULL"));
361 if (!in_net_epoch && TASK_IS_NET(gtask)) {
364 } else if (in_net_epoch && !TASK_IS_NET(gtask)) {
366 in_net_epoch = false;
368 gtask->ta_func(gtask->ta_context);
375 LIST_REMOVE(&tb, tb_link);
379 task_is_running(struct gtaskqueue *queue, struct gtask *gtask)
381 struct gtaskqueue_busy *tb;
383 TQ_ASSERT_LOCKED(queue);
384 LIST_FOREACH(tb, &queue->tq_active, tb_link) {
385 if (tb->tb_running == gtask)
392 gtaskqueue_cancel_locked(struct gtaskqueue *queue, struct gtask *gtask)
395 if (gtask->ta_flags & TASK_ENQUEUED)
396 STAILQ_REMOVE(&queue->tq_queue, gtask, gtask, ta_link);
397 gtask->ta_flags &= ~TASK_ENQUEUED;
398 return (task_is_running(queue, gtask) ? EBUSY : 0);
402 gtaskqueue_cancel(struct gtaskqueue *queue, struct gtask *gtask)
407 error = gtaskqueue_cancel_locked(queue, gtask);
414 gtaskqueue_drain_locked(struct gtaskqueue *queue, struct gtask *gtask)
416 while ((gtask->ta_flags & TASK_ENQUEUED) || task_is_running(queue, gtask))
417 TQ_SLEEP(queue, gtask, "gtq_drain");
421 gtaskqueue_drain(struct gtaskqueue *queue, struct gtask *gtask)
425 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
428 gtaskqueue_drain_locked(queue, gtask);
433 gtaskqueue_drain_all(struct gtaskqueue *queue)
437 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
440 gtaskqueue_drain_tq_queue(queue);
441 gtaskqueue_drain_tq_active(queue);
446 _gtaskqueue_start_threads(struct gtaskqueue **tqp, int count, int pri,
447 cpuset_t *mask, const char *name, va_list ap)
449 char ktname[MAXCOMLEN + 1];
451 struct gtaskqueue *tq;
457 vsnprintf(ktname, sizeof(ktname), name, ap);
460 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_GTASKQUEUE,
462 if (tq->tq_threads == NULL) {
463 printf("%s: no memory for %s threads\n", __func__, ktname);
467 for (i = 0; i < count; i++) {
469 error = kthread_add(gtaskqueue_thread_loop, tqp, NULL,
470 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
472 error = kthread_add(gtaskqueue_thread_loop, tqp, NULL,
473 &tq->tq_threads[i], RFSTOPPED, 0,
476 /* should be ok to continue, taskqueue_free will dtrt */
477 printf("%s: kthread_add(%s): error %d", __func__,
479 tq->tq_threads[i] = NULL; /* paranoid */
483 for (i = 0; i < count; i++) {
484 if (tq->tq_threads[i] == NULL)
486 td = tq->tq_threads[i];
488 error = cpuset_setthread(td->td_tid, mask);
490 * Failing to pin is rarely an actual fatal error;
491 * it'll just affect performance.
494 printf("%s: curthread=%llu: can't pin; "
497 (unsigned long long) td->td_tid,
502 sched_add(td, SRQ_BORING);
509 gtaskqueue_start_threads(struct gtaskqueue **tqp, int count, int pri,
510 const char *name, ...)
516 error = _gtaskqueue_start_threads(tqp, count, pri, NULL, name, ap);
522 gtaskqueue_run_callback(struct gtaskqueue *tq,
523 enum taskqueue_callback_type cb_type)
525 taskqueue_callback_fn tq_callback;
527 TQ_ASSERT_UNLOCKED(tq);
528 tq_callback = tq->tq_callbacks[cb_type];
529 if (tq_callback != NULL)
530 tq_callback(tq->tq_cb_contexts[cb_type]);
534 gtaskqueue_thread_loop(void *arg)
536 struct gtaskqueue **tqp, *tq;
540 gtaskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT);
542 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
544 gtaskqueue_run_locked(tq);
546 * Because taskqueue_run() can drop tq_mutex, we need to
547 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
548 * meantime, which means we missed a wakeup.
550 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
552 TQ_SLEEP(tq, tq, "-");
554 gtaskqueue_run_locked(tq);
556 * This thread is on its way out, so just drop the lock temporarily
557 * in order to call the shutdown callback. This allows the callback
558 * to look at the taskqueue, even just before it dies.
561 gtaskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN);
564 /* rendezvous with thread that asked us to terminate */
566 wakeup_one(tq->tq_threads);
572 gtaskqueue_thread_enqueue(void *context)
574 struct gtaskqueue **tqp, *tq;
581 static struct gtaskqueue *
582 gtaskqueue_create_fast(const char *name, int mflags,
583 taskqueue_enqueue_fn enqueue, void *context)
585 return _gtaskqueue_create(name, mflags, enqueue, context,
586 MTX_SPIN, "fast_taskqueue");
589 struct taskqgroup_cpu {
590 LIST_HEAD(, grouptask) tgc_tasks;
591 struct gtaskqueue *tgc_taskq;
597 struct taskqgroup_cpu tqg_queue[MAXCPU];
599 const char * tqg_name;
603 struct taskq_bind_task {
604 struct gtask bt_task;
609 taskqgroup_cpu_create(struct taskqgroup *qgroup, int idx, int cpu)
611 struct taskqgroup_cpu *qcpu;
613 qcpu = &qgroup->tqg_queue[idx];
614 LIST_INIT(&qcpu->tgc_tasks);
615 qcpu->tgc_taskq = gtaskqueue_create_fast(NULL, M_WAITOK,
616 gtaskqueue_thread_enqueue, &qcpu->tgc_taskq);
617 gtaskqueue_start_threads(&qcpu->tgc_taskq, 1, PI_SOFT,
618 "%s_%d", qgroup->tqg_name, idx);
623 * Find the taskq with least # of tasks that doesn't currently have any
624 * other queues from the uniq identifier.
627 taskqgroup_find(struct taskqgroup *qgroup, void *uniq)
633 mtx_assert(&qgroup->tqg_lock, MA_OWNED);
634 KASSERT(qgroup->tqg_cnt != 0,
635 ("qgroup %s has no queues", qgroup->tqg_name));
638 * Two passes: first scan for a queue with the least tasks that
639 * does not already service this uniq id. If that fails simply find
640 * the queue with the least total tasks.
642 for (idx = -1, mincnt = INT_MAX, strict = 1; mincnt == INT_MAX;
644 for (i = 0; i < qgroup->tqg_cnt; i++) {
645 if (qgroup->tqg_queue[i].tgc_cnt > mincnt)
648 LIST_FOREACH(n, &qgroup->tqg_queue[i].tgc_tasks,
650 if (n->gt_uniq == uniq)
655 mincnt = qgroup->tqg_queue[i].tgc_cnt;
660 panic("%s: failed to pick a qid.", __func__);
666 taskqgroup_attach(struct taskqgroup *qgroup, struct grouptask *gtask,
667 void *uniq, device_t dev, struct resource *irq, const char *name)
671 KASSERT(qgroup->tqg_cnt > 0,
672 ("qgroup %s has no queues", qgroup->tqg_name));
674 gtask->gt_uniq = uniq;
675 snprintf(gtask->gt_name, GROUPTASK_NAMELEN, "%s", name ? name : "grouptask");
679 mtx_lock(&qgroup->tqg_lock);
680 qid = taskqgroup_find(qgroup, uniq);
681 qgroup->tqg_queue[qid].tgc_cnt++;
682 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
683 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
684 if (dev != NULL && irq != NULL) {
685 cpu = qgroup->tqg_queue[qid].tgc_cpu;
687 mtx_unlock(&qgroup->tqg_lock);
688 error = bus_bind_intr(dev, irq, cpu);
690 printf("%s: binding interrupt failed for %s: %d\n",
691 __func__, gtask->gt_name, error);
693 mtx_unlock(&qgroup->tqg_lock);
697 taskqgroup_attach_cpu(struct taskqgroup *qgroup, struct grouptask *gtask,
698 void *uniq, int cpu, device_t dev, struct resource *irq, const char *name)
702 gtask->gt_uniq = uniq;
703 snprintf(gtask->gt_name, GROUPTASK_NAMELEN, "%s", name ? name : "grouptask");
707 mtx_lock(&qgroup->tqg_lock);
708 for (i = 0, qid = -1; i < qgroup->tqg_cnt; i++)
709 if (qgroup->tqg_queue[i].tgc_cpu == cpu) {
714 mtx_unlock(&qgroup->tqg_lock);
715 printf("%s: qid not found for %s cpu=%d\n", __func__, gtask->gt_name, cpu);
718 qgroup->tqg_queue[qid].tgc_cnt++;
719 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
720 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
721 cpu = qgroup->tqg_queue[qid].tgc_cpu;
722 mtx_unlock(&qgroup->tqg_lock);
724 if (dev != NULL && irq != NULL) {
725 error = bus_bind_intr(dev, irq, cpu);
727 printf("%s: binding interrupt failed for %s: %d\n",
728 __func__, gtask->gt_name, error);
734 taskqgroup_detach(struct taskqgroup *qgroup, struct grouptask *gtask)
738 grouptask_block(gtask);
739 mtx_lock(&qgroup->tqg_lock);
740 for (i = 0; i < qgroup->tqg_cnt; i++)
741 if (qgroup->tqg_queue[i].tgc_taskq == gtask->gt_taskqueue)
743 if (i == qgroup->tqg_cnt)
744 panic("%s: task %s not in group", __func__, gtask->gt_name);
745 qgroup->tqg_queue[i].tgc_cnt--;
746 LIST_REMOVE(gtask, gt_list);
747 mtx_unlock(&qgroup->tqg_lock);
748 gtask->gt_taskqueue = NULL;
749 gtask->gt_task.ta_flags &= ~TASK_NOENQUEUE;
753 taskqgroup_binder(void *ctx)
755 struct taskq_bind_task *gtask;
761 CPU_SET(gtask->bt_cpuid, &mask);
762 error = cpuset_setthread(curthread->td_tid, &mask);
763 thread_lock(curthread);
764 sched_bind(curthread, gtask->bt_cpuid);
765 thread_unlock(curthread);
768 printf("%s: binding curthread failed: %d\n", __func__, error);
769 free(gtask, M_DEVBUF);
773 taskqgroup_bind(struct taskqgroup *qgroup)
775 struct taskq_bind_task *gtask;
779 * Bind taskqueue threads to specific CPUs, if they have been assigned
782 if (qgroup->tqg_cnt == 1)
785 for (i = 0; i < qgroup->tqg_cnt; i++) {
786 gtask = malloc(sizeof(*gtask), M_DEVBUF, M_WAITOK);
787 GTASK_INIT(>ask->bt_task, 0, 0, taskqgroup_binder, gtask);
788 gtask->bt_cpuid = qgroup->tqg_queue[i].tgc_cpu;
789 grouptaskqueue_enqueue(qgroup->tqg_queue[i].tgc_taskq,
795 taskqgroup_create(const char *name, int cnt, int stride)
797 struct taskqgroup *qgroup;
800 qgroup = malloc(sizeof(*qgroup), M_GTASKQUEUE, M_WAITOK | M_ZERO);
801 mtx_init(&qgroup->tqg_lock, "taskqgroup", NULL, MTX_DEF);
802 qgroup->tqg_name = name;
803 qgroup->tqg_cnt = cnt;
805 for (cpu = i = 0; i < cnt; i++) {
806 taskqgroup_cpu_create(qgroup, i, cpu);
807 for (j = 0; j < stride; j++)
814 taskqgroup_destroy(struct taskqgroup *qgroup)
819 taskqgroup_drain_all(struct taskqgroup *tqg)
821 struct gtaskqueue *q;
823 for (int i = 0; i < mp_ncpus; i++) {
824 q = tqg->tqg_queue[i].tgc_taskq;
827 gtaskqueue_drain_all(q);