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 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/cpuset.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
38 #include <sys/libkern.h>
39 #include <sys/limits.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
44 #include <sys/epoch.h>
45 #include <sys/sched.h>
47 #include <sys/gtaskqueue.h>
48 #include <sys/unistd.h>
49 #include <machine/stdarg.h>
51 static MALLOC_DEFINE(M_GTASKQUEUE, "gtaskqueue", "Group Task Queues");
52 static void gtaskqueue_thread_enqueue(void *);
53 static void gtaskqueue_thread_loop(void *arg);
54 static int task_is_running(struct gtaskqueue *queue, struct gtask *gtask);
55 static void gtaskqueue_drain_locked(struct gtaskqueue *queue, struct gtask *gtask);
57 TASKQGROUP_DEFINE(softirq, mp_ncpus, 1);
59 struct gtaskqueue_busy {
60 struct gtask *tb_running;
62 LIST_ENTRY(gtaskqueue_busy) tb_link;
65 typedef void (*gtaskqueue_enqueue_fn)(void *context);
68 STAILQ_HEAD(, gtask) tq_queue;
69 LIST_HEAD(, gtaskqueue_busy) tq_active;
72 struct mtx_padalign tq_mutex;
73 gtaskqueue_enqueue_fn tq_enqueue;
76 struct thread **tq_threads;
80 taskqueue_callback_fn tq_callbacks[TASKQUEUE_NUM_CALLBACKS];
81 void *tq_cb_contexts[TASKQUEUE_NUM_CALLBACKS];
84 #define TQ_FLAGS_ACTIVE (1 << 0)
85 #define TQ_FLAGS_BLOCKED (1 << 1)
86 #define TQ_FLAGS_UNLOCKED_ENQUEUE (1 << 2)
88 #define DT_CALLOUT_ARMED (1 << 0)
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)
110 gtask_dump(struct gtask *gtask)
112 printf("gtask: %p ta_flags=%x ta_priority=%d ta_func=%p ta_context=%p\n",
113 gtask, gtask->ta_flags, gtask->ta_priority, gtask->ta_func, gtask->ta_context);
118 TQ_SLEEP(struct gtaskqueue *tq, void *p, const char *wm)
121 return (msleep_spin(p, (struct mtx *)&tq->tq_mutex, wm, 0));
122 return (msleep(p, &tq->tq_mutex, 0, wm, 0));
125 static struct gtaskqueue *
126 _gtaskqueue_create(const char *name, int mflags,
127 taskqueue_enqueue_fn enqueue, void *context,
128 int mtxflags, const char *mtxname __unused)
130 struct gtaskqueue *queue;
133 tq_name = malloc(TASKQUEUE_NAMELEN, M_GTASKQUEUE, mflags | M_ZERO);
137 snprintf(tq_name, TASKQUEUE_NAMELEN, "%s", (name) ? name : "taskqueue");
139 queue = malloc(sizeof(struct gtaskqueue), M_GTASKQUEUE, mflags | M_ZERO);
141 free(tq_name, M_GTASKQUEUE);
145 STAILQ_INIT(&queue->tq_queue);
146 LIST_INIT(&queue->tq_active);
147 queue->tq_enqueue = enqueue;
148 queue->tq_context = context;
149 queue->tq_name = tq_name;
150 queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
151 queue->tq_flags |= TQ_FLAGS_ACTIVE;
152 if (enqueue == gtaskqueue_thread_enqueue)
153 queue->tq_flags |= TQ_FLAGS_UNLOCKED_ENQUEUE;
154 mtx_init(&queue->tq_mutex, tq_name, NULL, mtxflags);
160 * Signal a taskqueue thread to terminate.
163 gtaskqueue_terminate(struct thread **pp, struct gtaskqueue *tq)
166 while (tq->tq_tcount > 0 || tq->tq_callouts > 0) {
168 TQ_SLEEP(tq, pp, "gtq_destroy");
173 gtaskqueue_free(struct gtaskqueue *queue)
177 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
178 gtaskqueue_terminate(queue->tq_threads, queue);
179 KASSERT(LIST_EMPTY(&queue->tq_active), ("Tasks still running?"));
180 KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks"));
181 mtx_destroy(&queue->tq_mutex);
182 free(queue->tq_threads, M_GTASKQUEUE);
183 free(queue->tq_name, M_GTASKQUEUE);
184 free(queue, M_GTASKQUEUE);
188 * Wait for all to complete, then prevent it from being enqueued
191 grouptask_block(struct grouptask *grouptask)
193 struct gtaskqueue *queue = grouptask->gt_taskqueue;
194 struct gtask *gtask = &grouptask->gt_task;
199 panic("queue == NULL");
203 gtask->ta_flags |= TASK_NOENQUEUE;
204 gtaskqueue_drain_locked(queue, gtask);
209 grouptask_unblock(struct grouptask *grouptask)
211 struct gtaskqueue *queue = grouptask->gt_taskqueue;
212 struct gtask *gtask = &grouptask->gt_task;
217 panic("queue == NULL");
221 gtask->ta_flags &= ~TASK_NOENQUEUE;
226 grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *gtask)
231 panic("queue == NULL");
235 if (gtask->ta_flags & TASK_ENQUEUED) {
239 if (gtask->ta_flags & TASK_NOENQUEUE) {
243 STAILQ_INSERT_TAIL(&queue->tq_queue, gtask, ta_link);
244 gtask->ta_flags |= TASK_ENQUEUED;
246 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
247 queue->tq_enqueue(queue->tq_context);
252 gtaskqueue_task_nop_fn(void *context)
257 * Block until all currently queued tasks in this taskqueue
258 * have begun execution. Tasks queued during execution of
259 * this function are ignored.
262 gtaskqueue_drain_tq_queue(struct gtaskqueue *queue)
264 struct gtask t_barrier;
266 if (STAILQ_EMPTY(&queue->tq_queue))
270 * Enqueue our barrier after all current tasks, but with
271 * the highest priority so that newly queued tasks cannot
272 * pass it. Because of the high priority, we can not use
273 * taskqueue_enqueue_locked directly (which drops the lock
274 * anyway) so just insert it at tail while we have the
277 GTASK_INIT(&t_barrier, 0, USHRT_MAX, gtaskqueue_task_nop_fn, &t_barrier);
278 STAILQ_INSERT_TAIL(&queue->tq_queue, &t_barrier, ta_link);
279 t_barrier.ta_flags |= TASK_ENQUEUED;
282 * Once the barrier has executed, all previously queued tasks
283 * have completed or are currently executing.
285 while (t_barrier.ta_flags & TASK_ENQUEUED)
286 TQ_SLEEP(queue, &t_barrier, "gtq_qdrain");
290 * Block until all currently executing tasks for this taskqueue
291 * complete. Tasks that begin execution during the execution
292 * of this function are ignored.
295 gtaskqueue_drain_tq_active(struct gtaskqueue *queue)
297 struct gtaskqueue_busy *tb;
300 if (LIST_EMPTY(&queue->tq_active))
303 /* Block taskq_terminate().*/
304 queue->tq_callouts++;
306 /* Wait for any active task with sequence from the past. */
309 LIST_FOREACH(tb, &queue->tq_active, tb_link) {
310 if ((int)(tb->tb_seq - seq) <= 0) {
311 TQ_SLEEP(queue, tb->tb_running, "gtq_adrain");
316 /* Release taskqueue_terminate(). */
317 queue->tq_callouts--;
318 if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0)
319 wakeup_one(queue->tq_threads);
323 gtaskqueue_block(struct gtaskqueue *queue)
327 queue->tq_flags |= TQ_FLAGS_BLOCKED;
332 gtaskqueue_unblock(struct gtaskqueue *queue)
336 queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
337 if (!STAILQ_EMPTY(&queue->tq_queue))
338 queue->tq_enqueue(queue->tq_context);
343 gtaskqueue_run_locked(struct gtaskqueue *queue)
345 struct epoch_tracker et;
346 struct gtaskqueue_busy tb;
350 KASSERT(queue != NULL, ("tq is NULL"));
351 TQ_ASSERT_LOCKED(queue);
352 tb.tb_running = NULL;
353 LIST_INSERT_HEAD(&queue->tq_active, &tb, tb_link);
354 in_net_epoch = false;
356 while ((gtask = STAILQ_FIRST(&queue->tq_queue)) != NULL) {
357 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
358 gtask->ta_flags &= ~TASK_ENQUEUED;
359 tb.tb_running = gtask;
360 tb.tb_seq = ++queue->tq_seq;
363 KASSERT(gtask->ta_func != NULL, ("task->ta_func is NULL"));
364 if (!in_net_epoch && TASK_IS_NET(gtask)) {
367 } else if (in_net_epoch && !TASK_IS_NET(gtask)) {
369 in_net_epoch = false;
371 gtask->ta_func(gtask->ta_context);
378 LIST_REMOVE(&tb, tb_link);
382 task_is_running(struct gtaskqueue *queue, struct gtask *gtask)
384 struct gtaskqueue_busy *tb;
386 TQ_ASSERT_LOCKED(queue);
387 LIST_FOREACH(tb, &queue->tq_active, tb_link) {
388 if (tb->tb_running == gtask)
395 gtaskqueue_cancel_locked(struct gtaskqueue *queue, struct gtask *gtask)
398 if (gtask->ta_flags & TASK_ENQUEUED)
399 STAILQ_REMOVE(&queue->tq_queue, gtask, gtask, ta_link);
400 gtask->ta_flags &= ~TASK_ENQUEUED;
401 return (task_is_running(queue, gtask) ? EBUSY : 0);
405 gtaskqueue_cancel(struct gtaskqueue *queue, struct gtask *gtask)
410 error = gtaskqueue_cancel_locked(queue, gtask);
417 gtaskqueue_drain_locked(struct gtaskqueue *queue, struct gtask *gtask)
419 while ((gtask->ta_flags & TASK_ENQUEUED) || task_is_running(queue, gtask))
420 TQ_SLEEP(queue, gtask, "gtq_drain");
424 gtaskqueue_drain(struct gtaskqueue *queue, struct gtask *gtask)
428 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
431 gtaskqueue_drain_locked(queue, gtask);
436 gtaskqueue_drain_all(struct gtaskqueue *queue)
440 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
443 gtaskqueue_drain_tq_queue(queue);
444 gtaskqueue_drain_tq_active(queue);
449 _gtaskqueue_start_threads(struct gtaskqueue **tqp, int count, int pri,
450 cpuset_t *mask, const char *name, va_list ap)
452 char ktname[MAXCOMLEN + 1];
454 struct gtaskqueue *tq;
460 vsnprintf(ktname, sizeof(ktname), name, ap);
463 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_GTASKQUEUE,
465 if (tq->tq_threads == NULL) {
466 printf("%s: no memory for %s threads\n", __func__, ktname);
470 for (i = 0; i < count; i++) {
472 error = kthread_add(gtaskqueue_thread_loop, tqp, NULL,
473 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
475 error = kthread_add(gtaskqueue_thread_loop, tqp, NULL,
476 &tq->tq_threads[i], RFSTOPPED, 0,
479 /* should be ok to continue, taskqueue_free will dtrt */
480 printf("%s: kthread_add(%s): error %d", __func__,
482 tq->tq_threads[i] = NULL; /* paranoid */
486 for (i = 0; i < count; i++) {
487 if (tq->tq_threads[i] == NULL)
489 td = tq->tq_threads[i];
491 error = cpuset_setthread(td->td_tid, mask);
493 * Failing to pin is rarely an actual fatal error;
494 * it'll just affect performance.
497 printf("%s: curthread=%llu: can't pin; "
500 (unsigned long long) td->td_tid,
505 sched_add(td, SRQ_BORING);
512 gtaskqueue_start_threads(struct gtaskqueue **tqp, int count, int pri,
513 const char *name, ...)
519 error = _gtaskqueue_start_threads(tqp, count, pri, NULL, name, ap);
525 gtaskqueue_run_callback(struct gtaskqueue *tq,
526 enum taskqueue_callback_type cb_type)
528 taskqueue_callback_fn tq_callback;
530 TQ_ASSERT_UNLOCKED(tq);
531 tq_callback = tq->tq_callbacks[cb_type];
532 if (tq_callback != NULL)
533 tq_callback(tq->tq_cb_contexts[cb_type]);
537 gtaskqueue_thread_loop(void *arg)
539 struct gtaskqueue **tqp, *tq;
543 gtaskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT);
545 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
547 gtaskqueue_run_locked(tq);
549 * Because taskqueue_run() can drop tq_mutex, we need to
550 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
551 * meantime, which means we missed a wakeup.
553 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
555 TQ_SLEEP(tq, tq, "-");
557 gtaskqueue_run_locked(tq);
559 * This thread is on its way out, so just drop the lock temporarily
560 * in order to call the shutdown callback. This allows the callback
561 * to look at the taskqueue, even just before it dies.
564 gtaskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN);
567 /* rendezvous with thread that asked us to terminate */
569 wakeup_one(tq->tq_threads);
575 gtaskqueue_thread_enqueue(void *context)
577 struct gtaskqueue **tqp, *tq;
584 static struct gtaskqueue *
585 gtaskqueue_create_fast(const char *name, int mflags,
586 taskqueue_enqueue_fn enqueue, void *context)
588 return _gtaskqueue_create(name, mflags, enqueue, context,
589 MTX_SPIN, "fast_taskqueue");
592 struct taskqgroup_cpu {
593 LIST_HEAD(, grouptask) tgc_tasks;
594 struct gtaskqueue *tgc_taskq;
600 struct taskqgroup_cpu tqg_queue[MAXCPU];
602 const char * tqg_name;
606 struct taskq_bind_task {
607 struct gtask bt_task;
612 taskqgroup_cpu_create(struct taskqgroup *qgroup, int idx, int cpu)
614 struct taskqgroup_cpu *qcpu;
616 qcpu = &qgroup->tqg_queue[idx];
617 LIST_INIT(&qcpu->tgc_tasks);
618 qcpu->tgc_taskq = gtaskqueue_create_fast(NULL, M_WAITOK,
619 taskqueue_thread_enqueue, &qcpu->tgc_taskq);
620 gtaskqueue_start_threads(&qcpu->tgc_taskq, 1, PI_SOFT,
621 "%s_%d", qgroup->tqg_name, idx);
626 * Find the taskq with least # of tasks that doesn't currently have any
627 * other queues from the uniq identifier.
630 taskqgroup_find(struct taskqgroup *qgroup, void *uniq)
636 mtx_assert(&qgroup->tqg_lock, MA_OWNED);
637 KASSERT(qgroup->tqg_cnt != 0,
638 ("qgroup %s has no queues", qgroup->tqg_name));
641 * Two passes: first scan for a queue with the least tasks that
642 * does not already service this uniq id. If that fails simply find
643 * the queue with the least total tasks.
645 for (idx = -1, mincnt = INT_MAX, strict = 1; mincnt == INT_MAX;
647 for (i = 0; i < qgroup->tqg_cnt; i++) {
648 if (qgroup->tqg_queue[i].tgc_cnt > mincnt)
651 LIST_FOREACH(n, &qgroup->tqg_queue[i].tgc_tasks,
653 if (n->gt_uniq == uniq)
658 mincnt = qgroup->tqg_queue[i].tgc_cnt;
663 panic("%s: failed to pick a qid.", __func__);
669 taskqgroup_attach(struct taskqgroup *qgroup, struct grouptask *gtask,
670 void *uniq, device_t dev, struct resource *irq, const char *name)
674 KASSERT(qgroup->tqg_cnt > 0,
675 ("qgroup %s has no queues", qgroup->tqg_name));
677 gtask->gt_uniq = uniq;
678 snprintf(gtask->gt_name, GROUPTASK_NAMELEN, "%s", name ? name : "grouptask");
682 mtx_lock(&qgroup->tqg_lock);
683 qid = taskqgroup_find(qgroup, uniq);
684 qgroup->tqg_queue[qid].tgc_cnt++;
685 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
686 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
687 if (dev != NULL && irq != NULL) {
688 cpu = qgroup->tqg_queue[qid].tgc_cpu;
690 mtx_unlock(&qgroup->tqg_lock);
691 error = bus_bind_intr(dev, irq, cpu);
693 printf("%s: binding interrupt failed for %s: %d\n",
694 __func__, gtask->gt_name, error);
696 mtx_unlock(&qgroup->tqg_lock);
700 taskqgroup_attach_cpu(struct taskqgroup *qgroup, struct grouptask *gtask,
701 void *uniq, int cpu, device_t dev, struct resource *irq, const char *name)
705 gtask->gt_uniq = uniq;
706 snprintf(gtask->gt_name, GROUPTASK_NAMELEN, "%s", name ? name : "grouptask");
710 mtx_lock(&qgroup->tqg_lock);
711 for (i = 0, qid = -1; i < qgroup->tqg_cnt; i++)
712 if (qgroup->tqg_queue[i].tgc_cpu == cpu) {
717 mtx_unlock(&qgroup->tqg_lock);
718 printf("%s: qid not found for %s cpu=%d\n", __func__, gtask->gt_name, cpu);
721 qgroup->tqg_queue[qid].tgc_cnt++;
722 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
723 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
724 cpu = qgroup->tqg_queue[qid].tgc_cpu;
725 mtx_unlock(&qgroup->tqg_lock);
727 if (dev != NULL && irq != NULL) {
728 error = bus_bind_intr(dev, irq, cpu);
730 printf("%s: binding interrupt failed for %s: %d\n",
731 __func__, gtask->gt_name, error);
737 taskqgroup_detach(struct taskqgroup *qgroup, struct grouptask *gtask)
741 grouptask_block(gtask);
742 mtx_lock(&qgroup->tqg_lock);
743 for (i = 0; i < qgroup->tqg_cnt; i++)
744 if (qgroup->tqg_queue[i].tgc_taskq == gtask->gt_taskqueue)
746 if (i == qgroup->tqg_cnt)
747 panic("%s: task %s not in group", __func__, gtask->gt_name);
748 qgroup->tqg_queue[i].tgc_cnt--;
749 LIST_REMOVE(gtask, gt_list);
750 mtx_unlock(&qgroup->tqg_lock);
751 gtask->gt_taskqueue = NULL;
752 gtask->gt_task.ta_flags &= ~TASK_NOENQUEUE;
756 taskqgroup_binder(void *ctx)
758 struct taskq_bind_task *gtask;
764 CPU_SET(gtask->bt_cpuid, &mask);
765 error = cpuset_setthread(curthread->td_tid, &mask);
766 thread_lock(curthread);
767 sched_bind(curthread, gtask->bt_cpuid);
768 thread_unlock(curthread);
771 printf("%s: binding curthread failed: %d\n", __func__, error);
772 free(gtask, M_DEVBUF);
776 taskqgroup_bind(struct taskqgroup *qgroup)
778 struct taskq_bind_task *gtask;
782 * Bind taskqueue threads to specific CPUs, if they have been assigned
785 if (qgroup->tqg_cnt == 1)
788 for (i = 0; i < qgroup->tqg_cnt; i++) {
789 gtask = malloc(sizeof(*gtask), M_DEVBUF, M_WAITOK);
790 GTASK_INIT(>ask->bt_task, 0, 0, taskqgroup_binder, gtask);
791 gtask->bt_cpuid = qgroup->tqg_queue[i].tgc_cpu;
792 grouptaskqueue_enqueue(qgroup->tqg_queue[i].tgc_taskq,
798 taskqgroup_create(const char *name, int cnt, int stride)
800 struct taskqgroup *qgroup;
803 qgroup = malloc(sizeof(*qgroup), M_GTASKQUEUE, M_WAITOK | M_ZERO);
804 mtx_init(&qgroup->tqg_lock, "taskqgroup", NULL, MTX_DEF);
805 qgroup->tqg_name = name;
806 qgroup->tqg_cnt = cnt;
808 for (cpu = i = 0; i < cnt; i++) {
809 taskqgroup_cpu_create(qgroup, i, cpu);
810 for (j = 0; j < stride; j++)
817 taskqgroup_destroy(struct taskqgroup *qgroup)