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, "taskqueue", "Task Queues");
52 static void gtaskqueue_thread_enqueue(void *);
53 static void gtaskqueue_thread_loop(void *arg);
55 struct gtaskqueue_busy {
56 struct gtask *tb_running;
57 TAILQ_ENTRY(gtaskqueue_busy) tb_link;
60 static struct gtask * const TB_DRAIN_WAITER = (struct gtask *)0x1;
63 STAILQ_HEAD(, gtask) tq_queue;
64 gtaskqueue_enqueue_fn tq_enqueue;
67 TAILQ_HEAD(, gtaskqueue_busy) tq_active;
69 struct thread **tq_threads;
74 taskqueue_callback_fn tq_callbacks[TASKQUEUE_NUM_CALLBACKS];
75 void *tq_cb_contexts[TASKQUEUE_NUM_CALLBACKS];
78 #define TQ_FLAGS_ACTIVE (1 << 0)
79 #define TQ_FLAGS_BLOCKED (1 << 1)
80 #define TQ_FLAGS_UNLOCKED_ENQUEUE (1 << 2)
82 #define DT_CALLOUT_ARMED (1 << 0)
87 mtx_lock_spin(&(tq)->tq_mutex); \
89 mtx_lock(&(tq)->tq_mutex); \
91 #define TQ_ASSERT_LOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_OWNED)
93 #define TQ_UNLOCK(tq) \
96 mtx_unlock_spin(&(tq)->tq_mutex); \
98 mtx_unlock(&(tq)->tq_mutex); \
100 #define TQ_ASSERT_UNLOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_NOTOWNED)
103 TQ_SLEEP(struct gtaskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
107 return (msleep_spin(p, m, wm, t));
108 return (msleep(p, m, pri, wm, t));
111 static struct gtaskqueue *
112 _gtaskqueue_create(const char *name, int mflags,
113 taskqueue_enqueue_fn enqueue, void *context,
114 int mtxflags, const char *mtxname __unused)
116 struct gtaskqueue *queue;
119 tq_name = malloc(TASKQUEUE_NAMELEN, M_GTASKQUEUE, mflags | M_ZERO);
123 snprintf(tq_name, TASKQUEUE_NAMELEN, "%s", (name) ? name : "taskqueue");
125 queue = malloc(sizeof(struct gtaskqueue), M_GTASKQUEUE, mflags | M_ZERO);
129 STAILQ_INIT(&queue->tq_queue);
130 TAILQ_INIT(&queue->tq_active);
131 queue->tq_enqueue = enqueue;
132 queue->tq_context = context;
133 queue->tq_name = tq_name;
134 queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
135 queue->tq_flags |= TQ_FLAGS_ACTIVE;
136 if (enqueue == gtaskqueue_thread_enqueue)
137 queue->tq_flags |= TQ_FLAGS_UNLOCKED_ENQUEUE;
138 mtx_init(&queue->tq_mutex, tq_name, NULL, mtxflags);
145 * Signal a taskqueue thread to terminate.
148 gtaskqueue_terminate(struct thread **pp, struct gtaskqueue *tq)
151 while (tq->tq_tcount > 0 || tq->tq_callouts > 0) {
153 TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0);
158 gtaskqueue_free(struct gtaskqueue *queue)
162 queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
163 gtaskqueue_terminate(queue->tq_threads, queue);
164 KASSERT(TAILQ_EMPTY(&queue->tq_active), ("Tasks still running?"));
165 KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks"));
166 mtx_destroy(&queue->tq_mutex);
167 free(queue->tq_threads, M_GTASKQUEUE);
168 free(queue->tq_name, M_GTASKQUEUE);
169 free(queue, M_GTASKQUEUE);
173 grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *gtask)
176 if (gtask->ta_flags & TASK_ENQUEUED) {
180 STAILQ_INSERT_TAIL(&queue->tq_queue, gtask, ta_link);
181 gtask->ta_flags |= TASK_ENQUEUED;
183 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
184 queue->tq_enqueue(queue->tq_context);
189 gtaskqueue_task_nop_fn(void *context)
194 * Block until all currently queued tasks in this taskqueue
195 * have begun execution. Tasks queued during execution of
196 * this function are ignored.
199 gtaskqueue_drain_tq_queue(struct gtaskqueue *queue)
201 struct gtask t_barrier;
203 if (STAILQ_EMPTY(&queue->tq_queue))
207 * Enqueue our barrier after all current tasks, but with
208 * the highest priority so that newly queued tasks cannot
209 * pass it. Because of the high priority, we can not use
210 * taskqueue_enqueue_locked directly (which drops the lock
211 * anyway) so just insert it at tail while we have the
214 GTASK_INIT(&t_barrier, 0, USHRT_MAX, gtaskqueue_task_nop_fn, &t_barrier);
215 STAILQ_INSERT_TAIL(&queue->tq_queue, &t_barrier, ta_link);
216 t_barrier.ta_flags |= TASK_ENQUEUED;
219 * Once the barrier has executed, all previously queued tasks
220 * have completed or are currently executing.
222 while (t_barrier.ta_flags & TASK_ENQUEUED)
223 TQ_SLEEP(queue, &t_barrier, &queue->tq_mutex, PWAIT, "-", 0);
227 * Block until all currently executing tasks for this taskqueue
228 * complete. Tasks that begin execution during the execution
229 * of this function are ignored.
232 gtaskqueue_drain_tq_active(struct gtaskqueue *queue)
234 struct gtaskqueue_busy tb_marker, *tb_first;
236 if (TAILQ_EMPTY(&queue->tq_active))
239 /* Block taskq_terminate().*/
240 queue->tq_callouts++;
243 * Wait for all currently executing taskqueue threads
246 tb_marker.tb_running = TB_DRAIN_WAITER;
247 TAILQ_INSERT_TAIL(&queue->tq_active, &tb_marker, tb_link);
248 while (TAILQ_FIRST(&queue->tq_active) != &tb_marker)
249 TQ_SLEEP(queue, &tb_marker, &queue->tq_mutex, PWAIT, "-", 0);
250 TAILQ_REMOVE(&queue->tq_active, &tb_marker, tb_link);
253 * Wakeup any other drain waiter that happened to queue up
254 * without any intervening active thread.
256 tb_first = TAILQ_FIRST(&queue->tq_active);
257 if (tb_first != NULL && tb_first->tb_running == TB_DRAIN_WAITER)
260 /* Release taskqueue_terminate(). */
261 queue->tq_callouts--;
262 if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0)
263 wakeup_one(queue->tq_threads);
267 gtaskqueue_block(struct gtaskqueue *queue)
271 queue->tq_flags |= TQ_FLAGS_BLOCKED;
276 gtaskqueue_unblock(struct gtaskqueue *queue)
280 queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
281 if (!STAILQ_EMPTY(&queue->tq_queue))
282 queue->tq_enqueue(queue->tq_context);
287 gtaskqueue_run_locked(struct gtaskqueue *queue)
289 struct gtaskqueue_busy tb;
290 struct gtaskqueue_busy *tb_first;
293 KASSERT(queue != NULL, ("tq is NULL"));
294 TQ_ASSERT_LOCKED(queue);
295 tb.tb_running = NULL;
297 while (STAILQ_FIRST(&queue->tq_queue)) {
298 TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link);
301 * Carefully remove the first task from the queue and
302 * clear its TASK_ENQUEUED flag
304 gtask = STAILQ_FIRST(&queue->tq_queue);
305 KASSERT(gtask != NULL, ("task is NULL"));
306 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
307 gtask->ta_flags &= ~TASK_ENQUEUED;
308 tb.tb_running = gtask;
311 KASSERT(gtask->ta_func != NULL, ("task->ta_func is NULL"));
312 gtask->ta_func(gtask->ta_context);
315 tb.tb_running = NULL;
318 TAILQ_REMOVE(&queue->tq_active, &tb, tb_link);
319 tb_first = TAILQ_FIRST(&queue->tq_active);
320 if (tb_first != NULL &&
321 tb_first->tb_running == TB_DRAIN_WAITER)
327 task_is_running(struct gtaskqueue *queue, struct gtask *gtask)
329 struct gtaskqueue_busy *tb;
331 TQ_ASSERT_LOCKED(queue);
332 TAILQ_FOREACH(tb, &queue->tq_active, tb_link) {
333 if (tb->tb_running == gtask)
340 gtaskqueue_cancel_locked(struct gtaskqueue *queue, struct gtask *gtask)
343 if (gtask->ta_flags & TASK_ENQUEUED)
344 STAILQ_REMOVE(&queue->tq_queue, gtask, gtask, ta_link);
345 gtask->ta_flags &= ~TASK_ENQUEUED;
346 return (task_is_running(queue, gtask) ? EBUSY : 0);
350 gtaskqueue_cancel(struct gtaskqueue *queue, struct gtask *gtask)
355 error = gtaskqueue_cancel_locked(queue, gtask);
362 gtaskqueue_drain(struct gtaskqueue *queue, struct gtask *gtask)
366 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
369 while ((gtask->ta_flags & TASK_ENQUEUED) || task_is_running(queue, gtask))
370 TQ_SLEEP(queue, gtask, &queue->tq_mutex, PWAIT, "-", 0);
375 gtaskqueue_drain_all(struct gtaskqueue *queue)
379 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
382 gtaskqueue_drain_tq_queue(queue);
383 gtaskqueue_drain_tq_active(queue);
388 _gtaskqueue_start_threads(struct gtaskqueue **tqp, int count, int pri,
389 cpuset_t *mask, const char *name, va_list ap)
391 char ktname[MAXCOMLEN + 1];
393 struct gtaskqueue *tq;
399 vsnprintf(ktname, sizeof(ktname), name, ap);
402 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_GTASKQUEUE,
404 if (tq->tq_threads == NULL) {
405 printf("%s: no memory for %s threads\n", __func__, ktname);
409 for (i = 0; i < count; i++) {
411 error = kthread_add(gtaskqueue_thread_loop, tqp, NULL,
412 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
414 error = kthread_add(gtaskqueue_thread_loop, tqp, NULL,
415 &tq->tq_threads[i], RFSTOPPED, 0,
418 /* should be ok to continue, taskqueue_free will dtrt */
419 printf("%s: kthread_add(%s): error %d", __func__,
421 tq->tq_threads[i] = NULL; /* paranoid */
425 for (i = 0; i < count; i++) {
426 if (tq->tq_threads[i] == NULL)
428 td = tq->tq_threads[i];
430 error = cpuset_setthread(td->td_tid, mask);
432 * Failing to pin is rarely an actual fatal error;
433 * it'll just affect performance.
436 printf("%s: curthread=%llu: can't pin; "
439 (unsigned long long) td->td_tid,
444 sched_add(td, SRQ_BORING);
452 gtaskqueue_start_threads(struct gtaskqueue **tqp, int count, int pri,
453 const char *name, ...)
459 error = _gtaskqueue_start_threads(tqp, count, pri, NULL, name, ap);
465 gtaskqueue_run_callback(struct gtaskqueue *tq,
466 enum taskqueue_callback_type cb_type)
468 taskqueue_callback_fn tq_callback;
470 TQ_ASSERT_UNLOCKED(tq);
471 tq_callback = tq->tq_callbacks[cb_type];
472 if (tq_callback != NULL)
473 tq_callback(tq->tq_cb_contexts[cb_type]);
477 gtaskqueue_thread_loop(void *arg)
479 struct gtaskqueue **tqp, *tq;
483 gtaskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT);
485 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
487 gtaskqueue_run_locked(tq);
489 * Because taskqueue_run() can drop tq_mutex, we need to
490 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
491 * meantime, which means we missed a wakeup.
493 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
495 TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
497 gtaskqueue_run_locked(tq);
499 * This thread is on its way out, so just drop the lock temporarily
500 * in order to call the shutdown callback. This allows the callback
501 * to look at the taskqueue, even just before it dies.
504 gtaskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN);
507 /* rendezvous with thread that asked us to terminate */
509 wakeup_one(tq->tq_threads);
515 gtaskqueue_thread_enqueue(void *context)
517 struct gtaskqueue **tqp, *tq;
525 static struct gtaskqueue *
526 gtaskqueue_create_fast(const char *name, int mflags,
527 taskqueue_enqueue_fn enqueue, void *context)
529 return _gtaskqueue_create(name, mflags, enqueue, context,
530 MTX_SPIN, "fast_taskqueue");
534 struct taskqgroup_cpu {
535 LIST_HEAD(, grouptask) tgc_tasks;
536 struct gtaskqueue *tgc_taskq;
542 struct taskqgroup_cpu tqg_queue[MAXCPU];
550 struct taskq_bind_task {
551 struct gtask bt_task;
556 taskqgroup_cpu_create(struct taskqgroup *qgroup, int idx, int cpu)
558 struct taskqgroup_cpu *qcpu;
560 qcpu = &qgroup->tqg_queue[idx];
561 LIST_INIT(&qcpu->tgc_tasks);
562 qcpu->tgc_taskq = gtaskqueue_create_fast(NULL, M_WAITOK,
563 taskqueue_thread_enqueue, &qcpu->tgc_taskq);
564 gtaskqueue_start_threads(&qcpu->tgc_taskq, 1, PI_SOFT,
565 "%s_%d", qgroup->tqg_name, idx);
570 taskqgroup_cpu_remove(struct taskqgroup *qgroup, int idx)
573 gtaskqueue_free(qgroup->tqg_queue[idx].tgc_taskq);
577 * Find the taskq with least # of tasks that doesn't currently have any
578 * other queues from the uniq identifier.
581 taskqgroup_find(struct taskqgroup *qgroup, void *uniq)
587 mtx_assert(&qgroup->tqg_lock, MA_OWNED);
588 if (qgroup->tqg_cnt == 0)
593 * Two passes; First scan for a queue with the least tasks that
594 * does not already service this uniq id. If that fails simply find
595 * the queue with the least total tasks;
597 for (strict = 1; mincnt == INT_MAX; strict = 0) {
598 for (i = 0; i < qgroup->tqg_cnt; i++) {
599 if (qgroup->tqg_queue[i].tgc_cnt > mincnt)
603 &qgroup->tqg_queue[i].tgc_tasks, gt_list)
604 if (n->gt_uniq == uniq)
609 mincnt = qgroup->tqg_queue[i].tgc_cnt;
614 panic("taskqgroup_find: Failed to pick a qid.");
620 taskqgroup_attach(struct taskqgroup *qgroup, struct grouptask *gtask,
621 void *uniq, int irq, char *name)
626 gtask->gt_uniq = uniq;
627 gtask->gt_name = name;
630 mtx_lock(&qgroup->tqg_lock);
631 qid = taskqgroup_find(qgroup, uniq);
632 qgroup->tqg_queue[qid].tgc_cnt++;
633 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
634 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
635 if (irq != -1 && smp_started) {
636 gtask->gt_cpu = qgroup->tqg_queue[qid].tgc_cpu;
638 CPU_SET(qgroup->tqg_queue[qid].tgc_cpu, &mask);
639 mtx_unlock(&qgroup->tqg_lock);
640 intr_setaffinity(irq, &mask);
642 mtx_unlock(&qgroup->tqg_lock);
646 taskqgroup_attach_deferred(struct taskqgroup *qgroup, struct grouptask *gtask)
651 mtx_lock(&qgroup->tqg_lock);
652 qid = taskqgroup_find(qgroup, gtask->gt_uniq);
653 cpu = qgroup->tqg_queue[qid].tgc_cpu;
654 if (gtask->gt_irq != -1) {
655 mtx_unlock(&qgroup->tqg_lock);
659 intr_setaffinity(gtask->gt_irq, &mask);
661 mtx_lock(&qgroup->tqg_lock);
663 qgroup->tqg_queue[qid].tgc_cnt++;
665 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask,
667 MPASS(qgroup->tqg_queue[qid].tgc_taskq != NULL);
668 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
669 mtx_unlock(&qgroup->tqg_lock);
673 taskqgroup_attach_cpu(struct taskqgroup *qgroup, struct grouptask *gtask,
674 void *uniq, int cpu, int irq, char *name)
680 gtask->gt_uniq = uniq;
681 gtask->gt_name = name;
684 mtx_lock(&qgroup->tqg_lock);
686 for (i = 0; i < qgroup->tqg_cnt; i++)
687 if (qgroup->tqg_queue[i].tgc_cpu == cpu) {
692 mtx_unlock(&qgroup->tqg_lock);
697 qgroup->tqg_queue[qid].tgc_cnt++;
698 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
699 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
700 cpu = qgroup->tqg_queue[qid].tgc_cpu;
701 mtx_unlock(&qgroup->tqg_lock);
705 if (irq != -1 && smp_started)
706 intr_setaffinity(irq, &mask);
711 taskqgroup_attach_cpu_deferred(struct taskqgroup *qgroup, struct grouptask *gtask)
714 int i, qid, irq, cpu;
720 mtx_lock(&qgroup->tqg_lock);
721 for (i = 0; i < qgroup->tqg_cnt; i++)
722 if (qgroup->tqg_queue[i].tgc_cpu == cpu) {
727 mtx_unlock(&qgroup->tqg_lock);
730 qgroup->tqg_queue[qid].tgc_cnt++;
731 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
732 MPASS(qgroup->tqg_queue[qid].tgc_taskq != NULL);
733 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
734 mtx_unlock(&qgroup->tqg_lock);
740 intr_setaffinity(irq, &mask);
745 taskqgroup_detach(struct taskqgroup *qgroup, struct grouptask *gtask)
749 mtx_lock(&qgroup->tqg_lock);
750 for (i = 0; i < qgroup->tqg_cnt; i++)
751 if (qgroup->tqg_queue[i].tgc_taskq == gtask->gt_taskqueue)
753 if (i == qgroup->tqg_cnt)
754 panic("taskqgroup_detach: task not in group\n");
755 qgroup->tqg_queue[i].tgc_cnt--;
756 LIST_REMOVE(gtask, gt_list);
757 mtx_unlock(&qgroup->tqg_lock);
758 gtask->gt_taskqueue = NULL;
762 taskqgroup_binder(void *ctx)
764 struct taskq_bind_task *gtask = (struct taskq_bind_task *)ctx;
769 CPU_SET(gtask->bt_cpuid, &mask);
770 error = cpuset_setthread(curthread->td_tid, &mask);
771 thread_lock(curthread);
772 sched_bind(curthread, gtask->bt_cpuid);
773 thread_unlock(curthread);
776 printf("taskqgroup_binder: setaffinity failed: %d\n",
778 free(gtask, M_DEVBUF);
782 taskqgroup_bind(struct taskqgroup *qgroup)
784 struct taskq_bind_task *gtask;
788 * Bind taskqueue threads to specific CPUs, if they have been assigned
791 if (qgroup->tqg_cnt == 1)
794 for (i = 0; i < qgroup->tqg_cnt; i++) {
795 gtask = malloc(sizeof (*gtask), M_DEVBUF, M_WAITOK);
796 GTASK_INIT(>ask->bt_task, 0, 0, taskqgroup_binder, gtask);
797 gtask->bt_cpuid = qgroup->tqg_queue[i].tgc_cpu;
798 grouptaskqueue_enqueue(qgroup->tqg_queue[i].tgc_taskq,
804 _taskqgroup_adjust(struct taskqgroup *qgroup, int cnt, int stride)
806 LIST_HEAD(, grouptask) gtask_head = LIST_HEAD_INITIALIZER(NULL);
807 struct grouptask *gtask;
808 int i, k, old_cnt, old_cpu, cpu;
810 mtx_assert(&qgroup->tqg_lock, MA_OWNED);
812 if (cnt < 1 || cnt * stride > mp_ncpus || !smp_started) {
813 printf("taskqgroup_adjust failed cnt: %d stride: %d mp_ncpus: %d smp_started: %d\n",
814 cnt, stride, mp_ncpus, smp_started);
817 if (qgroup->tqg_adjusting) {
818 printf("taskqgroup_adjust failed: adjusting\n");
821 qgroup->tqg_adjusting = 1;
822 old_cnt = qgroup->tqg_cnt;
825 old_cpu = qgroup->tqg_queue[old_cnt].tgc_cpu;
826 mtx_unlock(&qgroup->tqg_lock);
828 * Set up queue for tasks added before boot.
831 LIST_SWAP(>ask_head, &qgroup->tqg_queue[0].tgc_tasks,
833 qgroup->tqg_queue[0].tgc_cnt = 0;
837 * If new taskq threads have been added.
840 for (i = old_cnt; i < cnt; i++) {
841 taskqgroup_cpu_create(qgroup, i, cpu);
843 for (k = 0; k < stride; k++)
846 mtx_lock(&qgroup->tqg_lock);
847 qgroup->tqg_cnt = cnt;
848 qgroup->tqg_stride = stride;
851 * Adjust drivers to use new taskqs.
853 for (i = 0; i < old_cnt; i++) {
854 while ((gtask = LIST_FIRST(&qgroup->tqg_queue[i].tgc_tasks))) {
855 LIST_REMOVE(gtask, gt_list);
856 qgroup->tqg_queue[i].tgc_cnt--;
857 LIST_INSERT_HEAD(>ask_head, gtask, gt_list);
860 mtx_unlock(&qgroup->tqg_lock);
862 while ((gtask = LIST_FIRST(>ask_head))) {
863 LIST_REMOVE(gtask, gt_list);
864 if (gtask->gt_cpu == -1)
865 taskqgroup_attach_deferred(qgroup, gtask);
866 else if (taskqgroup_attach_cpu_deferred(qgroup, gtask))
867 taskqgroup_attach_deferred(qgroup, gtask);
871 mtx_lock(&qgroup->tqg_lock);
872 for (i = 0; i < qgroup->tqg_cnt; i++) {
873 MPASS(qgroup->tqg_queue[i].tgc_taskq != NULL);
874 LIST_FOREACH(gtask, &qgroup->tqg_queue[i].tgc_tasks, gt_list)
875 MPASS(gtask->gt_taskqueue != NULL);
877 mtx_unlock(&qgroup->tqg_lock);
880 * If taskq thread count has been reduced.
882 for (i = cnt; i < old_cnt; i++)
883 taskqgroup_cpu_remove(qgroup, i);
885 taskqgroup_bind(qgroup);
887 mtx_lock(&qgroup->tqg_lock);
888 qgroup->tqg_adjusting = 0;
894 taskqgroup_adjust(struct taskqgroup *qgroup, int cnt, int stride)
898 mtx_lock(&qgroup->tqg_lock);
899 error = _taskqgroup_adjust(qgroup, cnt, stride);
900 mtx_unlock(&qgroup->tqg_lock);
906 taskqgroup_create(char *name)
908 struct taskqgroup *qgroup;
910 qgroup = malloc(sizeof(*qgroup), M_GTASKQUEUE, M_WAITOK | M_ZERO);
911 mtx_init(&qgroup->tqg_lock, "taskqgroup", NULL, MTX_DEF);
912 qgroup->tqg_name = name;
913 LIST_INIT(&qgroup->tqg_queue[0].tgc_tasks);
919 taskqgroup_destroy(struct taskqgroup *qgroup)