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/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/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);
55 TASKQGROUP_DEFINE(softirq, mp_ncpus, 1);
57 struct gtaskqueue_busy {
58 struct gtask *tb_running;
59 TAILQ_ENTRY(gtaskqueue_busy) tb_link;
62 static struct gtask * const TB_DRAIN_WAITER = (struct gtask *)0x1;
65 STAILQ_HEAD(, gtask) tq_queue;
66 gtaskqueue_enqueue_fn tq_enqueue;
69 TAILQ_HEAD(, gtaskqueue_busy) tq_active;
71 struct thread **tq_threads;
76 taskqueue_callback_fn tq_callbacks[TASKQUEUE_NUM_CALLBACKS];
77 void *tq_cb_contexts[TASKQUEUE_NUM_CALLBACKS];
80 #define TQ_FLAGS_ACTIVE (1 << 0)
81 #define TQ_FLAGS_BLOCKED (1 << 1)
82 #define TQ_FLAGS_UNLOCKED_ENQUEUE (1 << 2)
84 #define DT_CALLOUT_ARMED (1 << 0)
89 mtx_lock_spin(&(tq)->tq_mutex); \
91 mtx_lock(&(tq)->tq_mutex); \
93 #define TQ_ASSERT_LOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_OWNED)
95 #define TQ_UNLOCK(tq) \
98 mtx_unlock_spin(&(tq)->tq_mutex); \
100 mtx_unlock(&(tq)->tq_mutex); \
102 #define TQ_ASSERT_UNLOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_NOTOWNED)
106 gtask_dump(struct gtask *gtask)
108 printf("gtask: %p ta_flags=%x ta_priority=%d ta_func=%p ta_context=%p\n",
109 gtask, gtask->ta_flags, gtask->ta_priority, gtask->ta_func, gtask->ta_context);
114 TQ_SLEEP(struct gtaskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
118 return (msleep_spin(p, m, wm, t));
119 return (msleep(p, m, pri, wm, t));
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 TAILQ_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);
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, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0);
171 gtaskqueue_free(struct gtaskqueue *queue)
175 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
176 gtaskqueue_terminate(queue->tq_threads, queue);
177 KASSERT(TAILQ_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 grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *gtask)
191 panic("queue == NULL");
195 if (gtask->ta_flags & TASK_ENQUEUED) {
199 STAILQ_INSERT_TAIL(&queue->tq_queue, gtask, ta_link);
200 gtask->ta_flags |= TASK_ENQUEUED;
202 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
203 queue->tq_enqueue(queue->tq_context);
208 gtaskqueue_task_nop_fn(void *context)
213 * Block until all currently queued tasks in this taskqueue
214 * have begun execution. Tasks queued during execution of
215 * this function are ignored.
218 gtaskqueue_drain_tq_queue(struct gtaskqueue *queue)
220 struct gtask t_barrier;
222 if (STAILQ_EMPTY(&queue->tq_queue))
226 * Enqueue our barrier after all current tasks, but with
227 * the highest priority so that newly queued tasks cannot
228 * pass it. Because of the high priority, we can not use
229 * taskqueue_enqueue_locked directly (which drops the lock
230 * anyway) so just insert it at tail while we have the
233 GTASK_INIT(&t_barrier, 0, USHRT_MAX, gtaskqueue_task_nop_fn, &t_barrier);
234 STAILQ_INSERT_TAIL(&queue->tq_queue, &t_barrier, ta_link);
235 t_barrier.ta_flags |= TASK_ENQUEUED;
238 * Once the barrier has executed, all previously queued tasks
239 * have completed or are currently executing.
241 while (t_barrier.ta_flags & TASK_ENQUEUED)
242 TQ_SLEEP(queue, &t_barrier, &queue->tq_mutex, PWAIT, "-", 0);
246 * Block until all currently executing tasks for this taskqueue
247 * complete. Tasks that begin execution during the execution
248 * of this function are ignored.
251 gtaskqueue_drain_tq_active(struct gtaskqueue *queue)
253 struct gtaskqueue_busy tb_marker, *tb_first;
255 if (TAILQ_EMPTY(&queue->tq_active))
258 /* Block taskq_terminate().*/
259 queue->tq_callouts++;
262 * Wait for all currently executing taskqueue threads
265 tb_marker.tb_running = TB_DRAIN_WAITER;
266 TAILQ_INSERT_TAIL(&queue->tq_active, &tb_marker, tb_link);
267 while (TAILQ_FIRST(&queue->tq_active) != &tb_marker)
268 TQ_SLEEP(queue, &tb_marker, &queue->tq_mutex, PWAIT, "-", 0);
269 TAILQ_REMOVE(&queue->tq_active, &tb_marker, tb_link);
272 * Wakeup any other drain waiter that happened to queue up
273 * without any intervening active thread.
275 tb_first = TAILQ_FIRST(&queue->tq_active);
276 if (tb_first != NULL && tb_first->tb_running == TB_DRAIN_WAITER)
279 /* Release taskqueue_terminate(). */
280 queue->tq_callouts--;
281 if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0)
282 wakeup_one(queue->tq_threads);
286 gtaskqueue_block(struct gtaskqueue *queue)
290 queue->tq_flags |= TQ_FLAGS_BLOCKED;
295 gtaskqueue_unblock(struct gtaskqueue *queue)
299 queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
300 if (!STAILQ_EMPTY(&queue->tq_queue))
301 queue->tq_enqueue(queue->tq_context);
306 gtaskqueue_run_locked(struct gtaskqueue *queue)
308 struct gtaskqueue_busy tb;
309 struct gtaskqueue_busy *tb_first;
312 KASSERT(queue != NULL, ("tq is NULL"));
313 TQ_ASSERT_LOCKED(queue);
314 tb.tb_running = NULL;
316 while (STAILQ_FIRST(&queue->tq_queue)) {
317 TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link);
320 * Carefully remove the first task from the queue and
321 * clear its TASK_ENQUEUED flag
323 gtask = STAILQ_FIRST(&queue->tq_queue);
324 KASSERT(gtask != NULL, ("task is NULL"));
325 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
326 gtask->ta_flags &= ~TASK_ENQUEUED;
327 tb.tb_running = gtask;
330 KASSERT(gtask->ta_func != NULL, ("task->ta_func is NULL"));
331 gtask->ta_func(gtask->ta_context);
334 tb.tb_running = NULL;
337 TAILQ_REMOVE(&queue->tq_active, &tb, tb_link);
338 tb_first = TAILQ_FIRST(&queue->tq_active);
339 if (tb_first != NULL &&
340 tb_first->tb_running == TB_DRAIN_WAITER)
346 task_is_running(struct gtaskqueue *queue, struct gtask *gtask)
348 struct gtaskqueue_busy *tb;
350 TQ_ASSERT_LOCKED(queue);
351 TAILQ_FOREACH(tb, &queue->tq_active, tb_link) {
352 if (tb->tb_running == gtask)
359 gtaskqueue_cancel_locked(struct gtaskqueue *queue, struct gtask *gtask)
362 if (gtask->ta_flags & TASK_ENQUEUED)
363 STAILQ_REMOVE(&queue->tq_queue, gtask, gtask, ta_link);
364 gtask->ta_flags &= ~TASK_ENQUEUED;
365 return (task_is_running(queue, gtask) ? EBUSY : 0);
369 gtaskqueue_cancel(struct gtaskqueue *queue, struct gtask *gtask)
374 error = gtaskqueue_cancel_locked(queue, gtask);
381 gtaskqueue_drain(struct gtaskqueue *queue, struct gtask *gtask)
385 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
388 while ((gtask->ta_flags & TASK_ENQUEUED) || task_is_running(queue, gtask))
389 TQ_SLEEP(queue, gtask, &queue->tq_mutex, PWAIT, "-", 0);
394 gtaskqueue_drain_all(struct gtaskqueue *queue)
398 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
401 gtaskqueue_drain_tq_queue(queue);
402 gtaskqueue_drain_tq_active(queue);
407 _gtaskqueue_start_threads(struct gtaskqueue **tqp, int count, int pri,
408 cpuset_t *mask, const char *name, va_list ap)
410 char ktname[MAXCOMLEN + 1];
412 struct gtaskqueue *tq;
418 vsnprintf(ktname, sizeof(ktname), name, ap);
421 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_GTASKQUEUE,
423 if (tq->tq_threads == NULL) {
424 printf("%s: no memory for %s threads\n", __func__, ktname);
428 for (i = 0; i < count; i++) {
430 error = kthread_add(gtaskqueue_thread_loop, tqp, NULL,
431 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
433 error = kthread_add(gtaskqueue_thread_loop, tqp, NULL,
434 &tq->tq_threads[i], RFSTOPPED, 0,
437 /* should be ok to continue, taskqueue_free will dtrt */
438 printf("%s: kthread_add(%s): error %d", __func__,
440 tq->tq_threads[i] = NULL; /* paranoid */
444 for (i = 0; i < count; i++) {
445 if (tq->tq_threads[i] == NULL)
447 td = tq->tq_threads[i];
449 error = cpuset_setthread(td->td_tid, mask);
451 * Failing to pin is rarely an actual fatal error;
452 * it'll just affect performance.
455 printf("%s: curthread=%llu: can't pin; "
458 (unsigned long long) td->td_tid,
463 sched_add(td, SRQ_BORING);
471 gtaskqueue_start_threads(struct gtaskqueue **tqp, int count, int pri,
472 const char *name, ...)
478 error = _gtaskqueue_start_threads(tqp, count, pri, NULL, name, ap);
484 gtaskqueue_run_callback(struct gtaskqueue *tq,
485 enum taskqueue_callback_type cb_type)
487 taskqueue_callback_fn tq_callback;
489 TQ_ASSERT_UNLOCKED(tq);
490 tq_callback = tq->tq_callbacks[cb_type];
491 if (tq_callback != NULL)
492 tq_callback(tq->tq_cb_contexts[cb_type]);
496 gtaskqueue_thread_loop(void *arg)
498 struct gtaskqueue **tqp, *tq;
502 gtaskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT);
504 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
506 gtaskqueue_run_locked(tq);
508 * Because taskqueue_run() can drop tq_mutex, we need to
509 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
510 * meantime, which means we missed a wakeup.
512 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
514 TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
516 gtaskqueue_run_locked(tq);
518 * This thread is on its way out, so just drop the lock temporarily
519 * in order to call the shutdown callback. This allows the callback
520 * to look at the taskqueue, even just before it dies.
523 gtaskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN);
526 /* rendezvous with thread that asked us to terminate */
528 wakeup_one(tq->tq_threads);
534 gtaskqueue_thread_enqueue(void *context)
536 struct gtaskqueue **tqp, *tq;
544 static struct gtaskqueue *
545 gtaskqueue_create_fast(const char *name, int mflags,
546 taskqueue_enqueue_fn enqueue, void *context)
548 return _gtaskqueue_create(name, mflags, enqueue, context,
549 MTX_SPIN, "fast_taskqueue");
553 struct taskqgroup_cpu {
554 LIST_HEAD(, grouptask) tgc_tasks;
555 struct gtaskqueue *tgc_taskq;
561 struct taskqgroup_cpu tqg_queue[MAXCPU];
569 struct taskq_bind_task {
570 struct gtask bt_task;
575 taskqgroup_cpu_create(struct taskqgroup *qgroup, int idx, int cpu)
577 struct taskqgroup_cpu *qcpu;
579 qcpu = &qgroup->tqg_queue[idx];
580 LIST_INIT(&qcpu->tgc_tasks);
581 qcpu->tgc_taskq = gtaskqueue_create_fast(NULL, M_WAITOK,
582 taskqueue_thread_enqueue, &qcpu->tgc_taskq);
583 gtaskqueue_start_threads(&qcpu->tgc_taskq, 1, PI_SOFT,
584 "%s_%d", qgroup->tqg_name, idx);
589 taskqgroup_cpu_remove(struct taskqgroup *qgroup, int idx)
592 gtaskqueue_free(qgroup->tqg_queue[idx].tgc_taskq);
596 * Find the taskq with least # of tasks that doesn't currently have any
597 * other queues from the uniq identifier.
600 taskqgroup_find(struct taskqgroup *qgroup, void *uniq)
606 mtx_assert(&qgroup->tqg_lock, MA_OWNED);
607 if (qgroup->tqg_cnt == 0)
612 * Two passes; First scan for a queue with the least tasks that
613 * does not already service this uniq id. If that fails simply find
614 * the queue with the least total tasks;
616 for (strict = 1; mincnt == INT_MAX; strict = 0) {
617 for (i = 0; i < qgroup->tqg_cnt; i++) {
618 if (qgroup->tqg_queue[i].tgc_cnt > mincnt)
622 &qgroup->tqg_queue[i].tgc_tasks, gt_list)
623 if (n->gt_uniq == uniq)
628 mincnt = qgroup->tqg_queue[i].tgc_cnt;
633 panic("taskqgroup_find: Failed to pick a qid.");
639 * smp_started is unusable since it is not set for UP kernels or even for
640 * SMP kernels when there is 1 CPU. This is usually handled by adding a
641 * (mp_ncpus == 1) test, but that would be broken here since we need to
642 * to synchronize with the SI_SUB_SMP ordering. Even in the pure SMP case
643 * smp_started only gives a fuzzy ordering relative to SI_SUB_SMP.
645 * So maintain our own flag. It must be set after all CPUs are started
646 * and before SI_SUB_SMP:SI_ORDER_ANY so that the SYSINIT for delayed
647 * adjustment is properly delayed. SI_ORDER_FOURTH is clearly before
648 * SI_ORDER_ANY and unclearly after the CPUs are started. It would be
649 * simpler for adjustment to pass a flag indicating if it is delayed.
652 static int tqg_smp_started;
655 tqg_record_smp_started(void *arg)
660 SYSINIT(tqg_record_smp_started, SI_SUB_SMP, SI_ORDER_FOURTH,
661 tqg_record_smp_started, NULL);
664 taskqgroup_attach(struct taskqgroup *qgroup, struct grouptask *gtask,
665 void *uniq, int irq, char *name)
670 gtask->gt_uniq = uniq;
671 snprintf(gtask->gt_name, GROUPTASK_NAMELEN, "%s", name ? name : "grouptask");
674 mtx_lock(&qgroup->tqg_lock);
675 qid = taskqgroup_find(qgroup, uniq);
676 qgroup->tqg_queue[qid].tgc_cnt++;
677 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
678 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
679 if (irq != -1 && tqg_smp_started) {
680 gtask->gt_cpu = qgroup->tqg_queue[qid].tgc_cpu;
682 CPU_SET(qgroup->tqg_queue[qid].tgc_cpu, &mask);
683 mtx_unlock(&qgroup->tqg_lock);
684 error = intr_setaffinity(irq, CPU_WHICH_IRQ, &mask);
686 printf("%s: setaffinity failed for %s: %d\n", __func__, gtask->gt_name, error);
688 mtx_unlock(&qgroup->tqg_lock);
692 taskqgroup_attach_deferred(struct taskqgroup *qgroup, struct grouptask *gtask)
697 mtx_lock(&qgroup->tqg_lock);
698 qid = taskqgroup_find(qgroup, gtask->gt_uniq);
699 cpu = qgroup->tqg_queue[qid].tgc_cpu;
700 if (gtask->gt_irq != -1) {
701 mtx_unlock(&qgroup->tqg_lock);
705 error = intr_setaffinity(gtask->gt_irq, CPU_WHICH_IRQ, &mask);
706 mtx_lock(&qgroup->tqg_lock);
708 printf("%s: %s setaffinity failed: %d\n", __func__, gtask->gt_name, error);
711 qgroup->tqg_queue[qid].tgc_cnt++;
713 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask,
715 MPASS(qgroup->tqg_queue[qid].tgc_taskq != NULL);
716 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
717 mtx_unlock(&qgroup->tqg_lock);
721 taskqgroup_attach_cpu(struct taskqgroup *qgroup, struct grouptask *gtask,
722 void *uniq, int cpu, int irq, char *name)
728 gtask->gt_uniq = uniq;
729 snprintf(gtask->gt_name, GROUPTASK_NAMELEN, "%s", name ? name : "grouptask");
732 mtx_lock(&qgroup->tqg_lock);
733 if (tqg_smp_started) {
734 for (i = 0; i < qgroup->tqg_cnt; i++)
735 if (qgroup->tqg_queue[i].tgc_cpu == cpu) {
740 mtx_unlock(&qgroup->tqg_lock);
741 printf("%s: qid not found for %s cpu=%d\n", __func__, gtask->gt_name, cpu);
746 qgroup->tqg_queue[qid].tgc_cnt++;
747 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
748 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
749 cpu = qgroup->tqg_queue[qid].tgc_cpu;
750 mtx_unlock(&qgroup->tqg_lock);
754 if (irq != -1 && tqg_smp_started) {
755 error = intr_setaffinity(irq, CPU_WHICH_IRQ, &mask);
757 printf("%s: setaffinity failed: %d\n", __func__, error);
763 taskqgroup_attach_cpu_deferred(struct taskqgroup *qgroup, struct grouptask *gtask)
766 int i, qid, irq, cpu, error;
771 MPASS(tqg_smp_started);
772 mtx_lock(&qgroup->tqg_lock);
773 for (i = 0; i < qgroup->tqg_cnt; i++)
774 if (qgroup->tqg_queue[i].tgc_cpu == cpu) {
779 mtx_unlock(&qgroup->tqg_lock);
780 printf("%s: qid not found for %s cpu=%d\n", __func__, gtask->gt_name, cpu);
783 qgroup->tqg_queue[qid].tgc_cnt++;
784 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
785 MPASS(qgroup->tqg_queue[qid].tgc_taskq != NULL);
786 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
787 mtx_unlock(&qgroup->tqg_lock);
793 error = intr_setaffinity(irq, CPU_WHICH_IRQ, &mask);
795 printf("%s: setaffinity failed: %d\n", __func__, error);
801 taskqgroup_detach(struct taskqgroup *qgroup, struct grouptask *gtask)
805 mtx_lock(&qgroup->tqg_lock);
806 for (i = 0; i < qgroup->tqg_cnt; i++)
807 if (qgroup->tqg_queue[i].tgc_taskq == gtask->gt_taskqueue)
809 if (i == qgroup->tqg_cnt)
810 panic("taskqgroup_detach: task %s not in group\n", gtask->gt_name);
811 qgroup->tqg_queue[i].tgc_cnt--;
812 LIST_REMOVE(gtask, gt_list);
813 mtx_unlock(&qgroup->tqg_lock);
814 gtask->gt_taskqueue = NULL;
818 taskqgroup_binder(void *ctx)
820 struct taskq_bind_task *gtask = (struct taskq_bind_task *)ctx;
825 CPU_SET(gtask->bt_cpuid, &mask);
826 error = cpuset_setthread(curthread->td_tid, &mask);
827 thread_lock(curthread);
828 sched_bind(curthread, gtask->bt_cpuid);
829 thread_unlock(curthread);
832 printf("%s: setaffinity failed: %d\n", __func__,
834 free(gtask, M_DEVBUF);
838 taskqgroup_bind(struct taskqgroup *qgroup)
840 struct taskq_bind_task *gtask;
844 * Bind taskqueue threads to specific CPUs, if they have been assigned
847 if (qgroup->tqg_cnt == 1)
850 for (i = 0; i < qgroup->tqg_cnt; i++) {
851 gtask = malloc(sizeof (*gtask), M_DEVBUF, M_WAITOK);
852 GTASK_INIT(>ask->bt_task, 0, 0, taskqgroup_binder, gtask);
853 gtask->bt_cpuid = qgroup->tqg_queue[i].tgc_cpu;
854 grouptaskqueue_enqueue(qgroup->tqg_queue[i].tgc_taskq,
860 _taskqgroup_adjust(struct taskqgroup *qgroup, int cnt, int stride)
862 LIST_HEAD(, grouptask) gtask_head = LIST_HEAD_INITIALIZER(NULL);
863 struct grouptask *gtask;
864 int i, k, old_cnt, old_cpu, cpu;
866 mtx_assert(&qgroup->tqg_lock, MA_OWNED);
868 if (cnt < 1 || cnt * stride > mp_ncpus || !tqg_smp_started) {
869 printf("%s: failed cnt: %d stride: %d "
870 "mp_ncpus: %d tqg_smp_started: %d\n",
871 __func__, cnt, stride, mp_ncpus, tqg_smp_started);
874 if (qgroup->tqg_adjusting) {
875 printf("%s failed: adjusting\n", __func__);
878 qgroup->tqg_adjusting = 1;
879 old_cnt = qgroup->tqg_cnt;
882 old_cpu = qgroup->tqg_queue[old_cnt].tgc_cpu;
883 mtx_unlock(&qgroup->tqg_lock);
885 * Set up queue for tasks added before boot.
888 LIST_SWAP(>ask_head, &qgroup->tqg_queue[0].tgc_tasks,
890 qgroup->tqg_queue[0].tgc_cnt = 0;
894 * If new taskq threads have been added.
897 for (i = old_cnt; i < cnt; i++) {
898 taskqgroup_cpu_create(qgroup, i, cpu);
900 for (k = 0; k < stride; k++)
903 mtx_lock(&qgroup->tqg_lock);
904 qgroup->tqg_cnt = cnt;
905 qgroup->tqg_stride = stride;
908 * Adjust drivers to use new taskqs.
910 for (i = 0; i < old_cnt; i++) {
911 while ((gtask = LIST_FIRST(&qgroup->tqg_queue[i].tgc_tasks))) {
912 LIST_REMOVE(gtask, gt_list);
913 qgroup->tqg_queue[i].tgc_cnt--;
914 LIST_INSERT_HEAD(>ask_head, gtask, gt_list);
917 mtx_unlock(&qgroup->tqg_lock);
919 while ((gtask = LIST_FIRST(>ask_head))) {
920 LIST_REMOVE(gtask, gt_list);
921 if (gtask->gt_cpu == -1)
922 taskqgroup_attach_deferred(qgroup, gtask);
923 else if (taskqgroup_attach_cpu_deferred(qgroup, gtask))
924 taskqgroup_attach_deferred(qgroup, gtask);
928 mtx_lock(&qgroup->tqg_lock);
929 for (i = 0; i < qgroup->tqg_cnt; i++) {
930 MPASS(qgroup->tqg_queue[i].tgc_taskq != NULL);
931 LIST_FOREACH(gtask, &qgroup->tqg_queue[i].tgc_tasks, gt_list)
932 MPASS(gtask->gt_taskqueue != NULL);
934 mtx_unlock(&qgroup->tqg_lock);
937 * If taskq thread count has been reduced.
939 for (i = cnt; i < old_cnt; i++)
940 taskqgroup_cpu_remove(qgroup, i);
942 taskqgroup_bind(qgroup);
944 mtx_lock(&qgroup->tqg_lock);
945 qgroup->tqg_adjusting = 0;
951 taskqgroup_adjust(struct taskqgroup *qgroup, int cnt, int stride)
955 mtx_lock(&qgroup->tqg_lock);
956 error = _taskqgroup_adjust(qgroup, cnt, stride);
957 mtx_unlock(&qgroup->tqg_lock);
963 taskqgroup_create(char *name)
965 struct taskqgroup *qgroup;
967 qgroup = malloc(sizeof(*qgroup), M_GTASKQUEUE, M_WAITOK | M_ZERO);
968 mtx_init(&qgroup->tqg_lock, "taskqgroup", NULL, MTX_DEF);
969 qgroup->tqg_name = name;
970 LIST_INIT(&qgroup->tqg_queue[0].tgc_tasks);
976 taskqgroup_destroy(struct taskqgroup *qgroup)