]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_taskqueue.c
Disconnect the OPENSTACK target from the CLOUDWARE list.
[FreeBSD/FreeBSD.git] / sys / kern / subr_taskqueue.c
1 /*-
2  * Copyright (c) 2000 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/cpuset.h>
34 #include <sys/interrupt.h>
35 #include <sys/kernel.h>
36 #include <sys/kthread.h>
37 #include <sys/libkern.h>
38 #include <sys/limits.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/sched.h>
44 #include <sys/smp.h>
45 #include <sys/taskqueue.h>
46 #include <sys/unistd.h>
47 #include <machine/stdarg.h>
48
49 static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
50 static void     *taskqueue_giant_ih;
51 static void     *taskqueue_ih;
52 static void      taskqueue_fast_enqueue(void *);
53 static void      taskqueue_swi_enqueue(void *);
54 static void      taskqueue_swi_giant_enqueue(void *);
55
56 struct taskqueue_busy {
57         struct task     *tb_running;
58         TAILQ_ENTRY(taskqueue_busy) tb_link;
59 };
60
61 struct task * const TB_DRAIN_WAITER = (struct task *)0x1;
62
63 struct taskqueue {
64         STAILQ_HEAD(, task)     tq_queue;
65         taskqueue_enqueue_fn    tq_enqueue;
66         void                    *tq_context;
67         char                    *tq_name;
68         TAILQ_HEAD(, taskqueue_busy) tq_active;
69         struct mtx              tq_mutex;
70         struct thread           **tq_threads;
71         int                     tq_tcount;
72         int                     tq_spin;
73         int                     tq_flags;
74         int                     tq_callouts;
75         taskqueue_callback_fn   tq_callbacks[TASKQUEUE_NUM_CALLBACKS];
76         void                    *tq_cb_contexts[TASKQUEUE_NUM_CALLBACKS];
77 };
78
79 #define TQ_FLAGS_ACTIVE         (1 << 0)
80 #define TQ_FLAGS_BLOCKED        (1 << 1)
81 #define TQ_FLAGS_UNLOCKED_ENQUEUE       (1 << 2)
82
83 #define DT_CALLOUT_ARMED        (1 << 0)
84
85 #define TQ_LOCK(tq)                                                     \
86         do {                                                            \
87                 if ((tq)->tq_spin)                                      \
88                         mtx_lock_spin(&(tq)->tq_mutex);                 \
89                 else                                                    \
90                         mtx_lock(&(tq)->tq_mutex);                      \
91         } while (0)
92 #define TQ_ASSERT_LOCKED(tq)    mtx_assert(&(tq)->tq_mutex, MA_OWNED)
93
94 #define TQ_UNLOCK(tq)                                                   \
95         do {                                                            \
96                 if ((tq)->tq_spin)                                      \
97                         mtx_unlock_spin(&(tq)->tq_mutex);               \
98                 else                                                    \
99                         mtx_unlock(&(tq)->tq_mutex);                    \
100         } while (0)
101 #define TQ_ASSERT_UNLOCKED(tq)  mtx_assert(&(tq)->tq_mutex, MA_NOTOWNED)
102
103 void
104 _timeout_task_init(struct taskqueue *queue, struct timeout_task *timeout_task,
105     int priority, task_fn_t func, void *context)
106 {
107
108         TASK_INIT(&timeout_task->t, priority, func, context);
109         callout_init_mtx(&timeout_task->c, &queue->tq_mutex,
110             CALLOUT_RETURNUNLOCKED);
111         timeout_task->q = queue;
112         timeout_task->f = 0;
113 }
114
115 static __inline int
116 TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
117     int t)
118 {
119         if (tq->tq_spin)
120                 return (msleep_spin(p, m, wm, t));
121         return (msleep(p, m, pri, wm, t));
122 }
123
124 static struct taskqueue *
125 _taskqueue_create(const char *name, int mflags,
126                  taskqueue_enqueue_fn enqueue, void *context,
127                  int mtxflags, const char *mtxname __unused)
128 {
129         struct taskqueue *queue;
130         char *tq_name;
131
132         tq_name = malloc(TASKQUEUE_NAMELEN, M_TASKQUEUE, mflags | M_ZERO);
133         if (!tq_name)
134                 return (NULL);
135
136         snprintf(tq_name, TASKQUEUE_NAMELEN, "%s", (name) ? name : "taskqueue");
137
138         queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
139         if (!queue)
140                 return (NULL);
141
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 == taskqueue_fast_enqueue ||
150             enqueue == taskqueue_swi_enqueue ||
151             enqueue == taskqueue_swi_giant_enqueue ||
152             enqueue == taskqueue_thread_enqueue)
153                 queue->tq_flags |= TQ_FLAGS_UNLOCKED_ENQUEUE;
154         mtx_init(&queue->tq_mutex, tq_name, NULL, mtxflags);
155
156         return (queue);
157 }
158
159 struct taskqueue *
160 taskqueue_create(const char *name, int mflags,
161                  taskqueue_enqueue_fn enqueue, void *context)
162 {
163
164         return _taskqueue_create(name, mflags, enqueue, context,
165                         MTX_DEF, name);
166 }
167
168 void
169 taskqueue_set_callback(struct taskqueue *queue,
170     enum taskqueue_callback_type cb_type, taskqueue_callback_fn callback,
171     void *context)
172 {
173
174         KASSERT(((cb_type >= TASKQUEUE_CALLBACK_TYPE_MIN) &&
175             (cb_type <= TASKQUEUE_CALLBACK_TYPE_MAX)),
176             ("Callback type %d not valid, must be %d-%d", cb_type,
177             TASKQUEUE_CALLBACK_TYPE_MIN, TASKQUEUE_CALLBACK_TYPE_MAX));
178         KASSERT((queue->tq_callbacks[cb_type] == NULL),
179             ("Re-initialization of taskqueue callback?"));
180
181         queue->tq_callbacks[cb_type] = callback;
182         queue->tq_cb_contexts[cb_type] = context;
183 }
184
185 /*
186  * Signal a taskqueue thread to terminate.
187  */
188 static void
189 taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
190 {
191
192         while (tq->tq_tcount > 0 || tq->tq_callouts > 0) {
193                 wakeup(tq);
194                 TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0);
195         }
196 }
197
198 void
199 taskqueue_free(struct taskqueue *queue)
200 {
201
202         TQ_LOCK(queue);
203         queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
204         taskqueue_terminate(queue->tq_threads, queue);
205         KASSERT(TAILQ_EMPTY(&queue->tq_active), ("Tasks still running?"));
206         KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks"));
207         mtx_destroy(&queue->tq_mutex);
208         free(queue->tq_threads, M_TASKQUEUE);
209         free(queue->tq_name, M_TASKQUEUE);
210         free(queue, M_TASKQUEUE);
211 }
212
213 static int
214 taskqueue_enqueue_locked(struct taskqueue *queue, struct task *task)
215 {
216         struct task *ins;
217         struct task *prev;
218
219         KASSERT(task->ta_func != NULL, ("enqueueing task with NULL func"));
220         /*
221          * Count multiple enqueues.
222          */
223         if (task->ta_pending) {
224                 if (task->ta_pending < USHRT_MAX)
225                         task->ta_pending++;
226                 TQ_UNLOCK(queue);
227                 return (0);
228         }
229
230         /*
231          * Optimise the case when all tasks have the same priority.
232          */
233         prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
234         if (!prev || prev->ta_priority >= task->ta_priority) {
235                 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
236         } else {
237                 prev = NULL;
238                 for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
239                      prev = ins, ins = STAILQ_NEXT(ins, ta_link))
240                         if (ins->ta_priority < task->ta_priority)
241                                 break;
242
243                 if (prev)
244                         STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
245                 else
246                         STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
247         }
248
249         task->ta_pending = 1;
250         if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) != 0)
251                 TQ_UNLOCK(queue);
252         if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
253                 queue->tq_enqueue(queue->tq_context);
254         if ((queue->tq_flags & TQ_FLAGS_UNLOCKED_ENQUEUE) == 0)
255                 TQ_UNLOCK(queue);
256
257         /* Return with lock released. */
258         return (0);
259 }
260
261 int
262 grouptaskqueue_enqueue(struct taskqueue *queue, struct task *task)
263 {
264         TQ_LOCK(queue);
265         if (task->ta_pending) {
266                 TQ_UNLOCK(queue);
267                 return (0);
268         }
269         STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
270         task->ta_pending = 1;
271         TQ_UNLOCK(queue);
272         if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
273                 queue->tq_enqueue(queue->tq_context);
274         return (0);
275 }
276
277 int
278 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
279 {
280         int res;
281
282         TQ_LOCK(queue);
283         res = taskqueue_enqueue_locked(queue, task);
284         /* The lock is released inside. */
285
286         return (res);
287 }
288
289 static void
290 taskqueue_timeout_func(void *arg)
291 {
292         struct taskqueue *queue;
293         struct timeout_task *timeout_task;
294
295         timeout_task = arg;
296         queue = timeout_task->q;
297         KASSERT((timeout_task->f & DT_CALLOUT_ARMED) != 0, ("Stray timeout"));
298         timeout_task->f &= ~DT_CALLOUT_ARMED;
299         queue->tq_callouts--;
300         taskqueue_enqueue_locked(timeout_task->q, &timeout_task->t);
301         /* The lock is released inside. */
302 }
303
304 int
305 taskqueue_enqueue_timeout(struct taskqueue *queue,
306     struct timeout_task *timeout_task, int ticks)
307 {
308         int res;
309
310         TQ_LOCK(queue);
311         KASSERT(timeout_task->q == NULL || timeout_task->q == queue,
312             ("Migrated queue"));
313         KASSERT(!queue->tq_spin, ("Timeout for spin-queue"));
314         timeout_task->q = queue;
315         res = timeout_task->t.ta_pending;
316         if (ticks == 0) {
317                 taskqueue_enqueue_locked(queue, &timeout_task->t);
318                 /* The lock is released inside. */
319         } else {
320                 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
321                         res++;
322                 } else {
323                         queue->tq_callouts++;
324                         timeout_task->f |= DT_CALLOUT_ARMED;
325                         if (ticks < 0)
326                                 ticks = -ticks; /* Ignore overflow. */
327                 }
328                 if (ticks > 0) {
329                         callout_reset(&timeout_task->c, ticks,
330                             taskqueue_timeout_func, timeout_task);
331                 }
332                 TQ_UNLOCK(queue);
333         }
334         return (res);
335 }
336
337 static void
338 taskqueue_task_nop_fn(void *context, int pending)
339 {
340 }
341
342 /*
343  * Block until all currently queued tasks in this taskqueue
344  * have begun execution.  Tasks queued during execution of
345  * this function are ignored.
346  */
347 static void
348 taskqueue_drain_tq_queue(struct taskqueue *queue)
349 {
350         struct task t_barrier;
351
352         if (STAILQ_EMPTY(&queue->tq_queue))
353                 return;
354
355         /*
356          * Enqueue our barrier after all current tasks, but with
357          * the highest priority so that newly queued tasks cannot
358          * pass it.  Because of the high priority, we can not use
359          * taskqueue_enqueue_locked directly (which drops the lock
360          * anyway) so just insert it at tail while we have the
361          * queue lock.
362          */
363         TASK_INIT(&t_barrier, USHRT_MAX, taskqueue_task_nop_fn, &t_barrier);
364         STAILQ_INSERT_TAIL(&queue->tq_queue, &t_barrier, ta_link);
365         t_barrier.ta_pending = 1;
366
367         /*
368          * Once the barrier has executed, all previously queued tasks
369          * have completed or are currently executing.
370          */
371         while (t_barrier.ta_pending != 0)
372                 TQ_SLEEP(queue, &t_barrier, &queue->tq_mutex, PWAIT, "-", 0);
373 }
374
375 /*
376  * Block until all currently executing tasks for this taskqueue
377  * complete.  Tasks that begin execution during the execution
378  * of this function are ignored.
379  */
380 static void
381 taskqueue_drain_tq_active(struct taskqueue *queue)
382 {
383         struct taskqueue_busy tb_marker, *tb_first;
384
385         if (TAILQ_EMPTY(&queue->tq_active))
386                 return;
387
388         /* Block taskq_terminate().*/
389         queue->tq_callouts++;
390
391         /*
392          * Wait for all currently executing taskqueue threads
393          * to go idle.
394          */
395         tb_marker.tb_running = TB_DRAIN_WAITER;
396         TAILQ_INSERT_TAIL(&queue->tq_active, &tb_marker, tb_link);
397         while (TAILQ_FIRST(&queue->tq_active) != &tb_marker)
398                 TQ_SLEEP(queue, &tb_marker, &queue->tq_mutex, PWAIT, "-", 0);
399         TAILQ_REMOVE(&queue->tq_active, &tb_marker, tb_link);
400
401         /*
402          * Wakeup any other drain waiter that happened to queue up
403          * without any intervening active thread.
404          */
405         tb_first = TAILQ_FIRST(&queue->tq_active);
406         if (tb_first != NULL && tb_first->tb_running == TB_DRAIN_WAITER)
407                 wakeup(tb_first);
408
409         /* Release taskqueue_terminate(). */
410         queue->tq_callouts--;
411         if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0)
412                 wakeup_one(queue->tq_threads);
413 }
414
415 void
416 taskqueue_block(struct taskqueue *queue)
417 {
418
419         TQ_LOCK(queue);
420         queue->tq_flags |= TQ_FLAGS_BLOCKED;
421         TQ_UNLOCK(queue);
422 }
423
424 void
425 taskqueue_unblock(struct taskqueue *queue)
426 {
427
428         TQ_LOCK(queue);
429         queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
430         if (!STAILQ_EMPTY(&queue->tq_queue))
431                 queue->tq_enqueue(queue->tq_context);
432         TQ_UNLOCK(queue);
433 }
434
435 static void
436 taskqueue_run_locked(struct taskqueue *queue)
437 {
438         struct taskqueue_busy tb;
439         struct taskqueue_busy *tb_first;
440         struct task *task;
441         int pending;
442
443         KASSERT(queue != NULL, ("tq is NULL"));
444         TQ_ASSERT_LOCKED(queue);
445         tb.tb_running = NULL;
446
447         while (STAILQ_FIRST(&queue->tq_queue)) {
448                 TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link);
449
450                 /*
451                  * Carefully remove the first task from the queue and
452                  * zero its pending count.
453                  */
454                 task = STAILQ_FIRST(&queue->tq_queue);
455                 KASSERT(task != NULL, ("task is NULL"));
456                 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
457                 pending = task->ta_pending;
458                 task->ta_pending = 0;
459                 tb.tb_running = task;
460                 TQ_UNLOCK(queue);
461
462                 KASSERT(task->ta_func != NULL, ("task->ta_func is NULL"));
463                 task->ta_func(task->ta_context, pending);
464
465                 TQ_LOCK(queue);
466                 tb.tb_running = NULL;
467                 wakeup(task);
468
469                 TAILQ_REMOVE(&queue->tq_active, &tb, tb_link);
470                 tb_first = TAILQ_FIRST(&queue->tq_active);
471                 if (tb_first != NULL &&
472                     tb_first->tb_running == TB_DRAIN_WAITER)
473                         wakeup(tb_first);
474         }
475 }
476
477 void
478 taskqueue_run(struct taskqueue *queue)
479 {
480
481         TQ_LOCK(queue);
482         taskqueue_run_locked(queue);
483         TQ_UNLOCK(queue);
484 }
485
486 static int
487 task_is_running(struct taskqueue *queue, struct task *task)
488 {
489         struct taskqueue_busy *tb;
490
491         TQ_ASSERT_LOCKED(queue);
492         TAILQ_FOREACH(tb, &queue->tq_active, tb_link) {
493                 if (tb->tb_running == task)
494                         return (1);
495         }
496         return (0);
497 }
498
499 static int
500 taskqueue_cancel_locked(struct taskqueue *queue, struct task *task,
501     u_int *pendp)
502 {
503
504         if (task->ta_pending > 0)
505                 STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link);
506         if (pendp != NULL)
507                 *pendp = task->ta_pending;
508         task->ta_pending = 0;
509         return (task_is_running(queue, task) ? EBUSY : 0);
510 }
511
512 int
513 taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp)
514 {
515         int error;
516
517         TQ_LOCK(queue);
518         error = taskqueue_cancel_locked(queue, task, pendp);
519         TQ_UNLOCK(queue);
520
521         return (error);
522 }
523
524 int
525 taskqueue_cancel_timeout(struct taskqueue *queue,
526     struct timeout_task *timeout_task, u_int *pendp)
527 {
528         u_int pending, pending1;
529         int error;
530
531         TQ_LOCK(queue);
532         pending = !!(callout_stop(&timeout_task->c) > 0);
533         error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1);
534         if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
535                 timeout_task->f &= ~DT_CALLOUT_ARMED;
536                 queue->tq_callouts--;
537         }
538         TQ_UNLOCK(queue);
539
540         if (pendp != NULL)
541                 *pendp = pending + pending1;
542         return (error);
543 }
544
545 void
546 taskqueue_drain(struct taskqueue *queue, struct task *task)
547 {
548
549         if (!queue->tq_spin)
550                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
551
552         TQ_LOCK(queue);
553         while (task->ta_pending != 0 || task_is_running(queue, task))
554                 TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0);
555         TQ_UNLOCK(queue);
556 }
557
558 void
559 taskqueue_drain_all(struct taskqueue *queue)
560 {
561
562         if (!queue->tq_spin)
563                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
564
565         TQ_LOCK(queue);
566         taskqueue_drain_tq_queue(queue);
567         taskqueue_drain_tq_active(queue);
568         TQ_UNLOCK(queue);
569 }
570
571 void
572 taskqueue_drain_timeout(struct taskqueue *queue,
573     struct timeout_task *timeout_task)
574 {
575
576         callout_drain(&timeout_task->c);
577         taskqueue_drain(queue, &timeout_task->t);
578 }
579
580 static void
581 taskqueue_swi_enqueue(void *context)
582 {
583         swi_sched(taskqueue_ih, 0);
584 }
585
586 static void
587 taskqueue_swi_run(void *dummy)
588 {
589         taskqueue_run(taskqueue_swi);
590 }
591
592 static void
593 taskqueue_swi_giant_enqueue(void *context)
594 {
595         swi_sched(taskqueue_giant_ih, 0);
596 }
597
598 static void
599 taskqueue_swi_giant_run(void *dummy)
600 {
601         taskqueue_run(taskqueue_swi_giant);
602 }
603
604 static int
605 _taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
606     cpuset_t *mask, const char *name, va_list ap)
607 {
608         char ktname[MAXCOMLEN + 1];
609         struct thread *td;
610         struct taskqueue *tq;
611         int i, error;
612
613         if (count <= 0)
614                 return (EINVAL);
615
616         vsnprintf(ktname, sizeof(ktname), name, ap);
617         tq = *tqp;
618
619         tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
620             M_NOWAIT | M_ZERO);
621         if (tq->tq_threads == NULL) {
622                 printf("%s: no memory for %s threads\n", __func__, ktname);
623                 return (ENOMEM);
624         }
625
626         for (i = 0; i < count; i++) {
627                 if (count == 1)
628                         error = kthread_add(taskqueue_thread_loop, tqp, NULL,
629                             &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
630                 else
631                         error = kthread_add(taskqueue_thread_loop, tqp, NULL,
632                             &tq->tq_threads[i], RFSTOPPED, 0,
633                             "%s_%d", ktname, i);
634                 if (error) {
635                         /* should be ok to continue, taskqueue_free will dtrt */
636                         printf("%s: kthread_add(%s): error %d", __func__,
637                             ktname, error);
638                         tq->tq_threads[i] = NULL;               /* paranoid */
639                 } else
640                         tq->tq_tcount++;
641         }
642         for (i = 0; i < count; i++) {
643                 if (tq->tq_threads[i] == NULL)
644                         continue;
645                 td = tq->tq_threads[i];
646                 if (mask) {
647                         error = cpuset_setthread(td->td_tid, mask);
648                         /*
649                          * Failing to pin is rarely an actual fatal error;
650                          * it'll just affect performance.
651                          */
652                         if (error)
653                                 printf("%s: curthread=%llu: can't pin; "
654                                     "error=%d\n",
655                                     __func__,
656                                     (unsigned long long) td->td_tid,
657                                     error);
658                 }
659                 thread_lock(td);
660                 sched_prio(td, pri);
661                 sched_add(td, SRQ_BORING);
662                 thread_unlock(td);
663         }
664
665         return (0);
666 }
667
668 int
669 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
670     const char *name, ...)
671 {
672         va_list ap;
673         int error;
674
675         va_start(ap, name);
676         error = _taskqueue_start_threads(tqp, count, pri, NULL, name, ap);
677         va_end(ap);
678         return (error);
679 }
680
681 int
682 taskqueue_start_threads_cpuset(struct taskqueue **tqp, int count, int pri,
683     cpuset_t *mask, const char *name, ...)
684 {
685         va_list ap;
686         int error;
687
688         va_start(ap, name);
689         error = _taskqueue_start_threads(tqp, count, pri, mask, name, ap);
690         va_end(ap);
691         return (error);
692 }
693
694 static inline void
695 taskqueue_run_callback(struct taskqueue *tq,
696     enum taskqueue_callback_type cb_type)
697 {
698         taskqueue_callback_fn tq_callback;
699
700         TQ_ASSERT_UNLOCKED(tq);
701         tq_callback = tq->tq_callbacks[cb_type];
702         if (tq_callback != NULL)
703                 tq_callback(tq->tq_cb_contexts[cb_type]);
704 }
705
706 void
707 taskqueue_thread_loop(void *arg)
708 {
709         struct taskqueue **tqp, *tq;
710
711         tqp = arg;
712         tq = *tqp;
713         taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT);
714         TQ_LOCK(tq);
715         while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
716                 /* XXX ? */
717                 taskqueue_run_locked(tq);
718                 /*
719                  * Because taskqueue_run() can drop tq_mutex, we need to
720                  * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
721                  * meantime, which means we missed a wakeup.
722                  */
723                 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
724                         break;
725                 TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
726         }
727         taskqueue_run_locked(tq);
728         /*
729          * This thread is on its way out, so just drop the lock temporarily
730          * in order to call the shutdown callback.  This allows the callback
731          * to look at the taskqueue, even just before it dies.
732          */
733         TQ_UNLOCK(tq);
734         taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN);
735         TQ_LOCK(tq);
736
737         /* rendezvous with thread that asked us to terminate */
738         tq->tq_tcount--;
739         wakeup_one(tq->tq_threads);
740         TQ_UNLOCK(tq);
741         kthread_exit();
742 }
743
744 void
745 taskqueue_thread_enqueue(void *context)
746 {
747         struct taskqueue **tqp, *tq;
748
749         tqp = context;
750         tq = *tqp;
751         wakeup_one(tq);
752 }
753
754 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL,
755                  swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
756                      INTR_MPSAFE, &taskqueue_ih));
757
758 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL,
759                  swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
760                      NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih));
761
762 TASKQUEUE_DEFINE_THREAD(thread);
763
764 struct taskqueue *
765 taskqueue_create_fast(const char *name, int mflags,
766                  taskqueue_enqueue_fn enqueue, void *context)
767 {
768         return _taskqueue_create(name, mflags, enqueue, context,
769                         MTX_SPIN, "fast_taskqueue");
770 }
771
772 static void     *taskqueue_fast_ih;
773
774 static void
775 taskqueue_fast_enqueue(void *context)
776 {
777         swi_sched(taskqueue_fast_ih, 0);
778 }
779
780 static void
781 taskqueue_fast_run(void *dummy)
782 {
783         taskqueue_run(taskqueue_fast);
784 }
785
786 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL,
787         swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL,
788         SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
789
790 int
791 taskqueue_member(struct taskqueue *queue, struct thread *td)
792 {
793         int i, j, ret = 0;
794
795         for (i = 0, j = 0; ; i++) {
796                 if (queue->tq_threads[i] == NULL)
797                         continue;
798                 if (queue->tq_threads[i] == td) {
799                         ret = 1;
800                         break;
801                 }
802                 if (++j >= queue->tq_tcount)
803                         break;
804         }
805         return (ret);
806 }
807
808 struct taskqgroup_cpu {
809         LIST_HEAD(, grouptask)  tgc_tasks;
810         struct taskqueue        *tgc_taskq;
811         int     tgc_cnt;
812         int     tgc_cpu;
813 };
814
815 struct taskqgroup {
816         struct taskqgroup_cpu tqg_queue[MAXCPU];
817         struct mtx      tqg_lock;
818         char *          tqg_name;
819         int             tqg_adjusting;
820         int             tqg_stride;
821         int             tqg_cnt;
822 };
823
824 struct taskq_bind_task {
825         struct task bt_task;
826         int     bt_cpuid;
827 };
828
829 static void
830 taskqgroup_cpu_create(struct taskqgroup *qgroup, int idx)
831 {
832         struct taskqgroup_cpu *qcpu;
833
834         qcpu = &qgroup->tqg_queue[idx];
835         LIST_INIT(&qcpu->tgc_tasks);
836         qcpu->tgc_taskq = taskqueue_create_fast(NULL, M_WAITOK,
837             taskqueue_thread_enqueue, &qcpu->tgc_taskq);
838         taskqueue_start_threads(&qcpu->tgc_taskq, 1, PI_SOFT,
839             "%s_%d", qgroup->tqg_name, idx);
840         qcpu->tgc_cpu = idx * qgroup->tqg_stride;
841 }
842
843 static void
844 taskqgroup_cpu_remove(struct taskqgroup *qgroup, int idx)
845 {
846
847         taskqueue_free(qgroup->tqg_queue[idx].tgc_taskq);
848 }
849
850 /*
851  * Find the taskq with least # of tasks that doesn't currently have any
852  * other queues from the uniq identifier.
853  */
854 static int
855 taskqgroup_find(struct taskqgroup *qgroup, void *uniq)
856 {
857         struct grouptask *n;
858         int i, idx, mincnt;
859         int strict;
860
861         mtx_assert(&qgroup->tqg_lock, MA_OWNED);
862         if (qgroup->tqg_cnt == 0)
863                 return (0);
864         idx = -1;
865         mincnt = INT_MAX;
866         /*
867          * Two passes;  First scan for a queue with the least tasks that
868          * does not already service this uniq id.  If that fails simply find
869          * the queue with the least total tasks;
870          */
871         for (strict = 1; mincnt == INT_MAX; strict = 0) {
872                 for (i = 0; i < qgroup->tqg_cnt; i++) {
873                         if (qgroup->tqg_queue[i].tgc_cnt > mincnt)
874                                 continue;
875                         if (strict) {
876                                 LIST_FOREACH(n,
877                                     &qgroup->tqg_queue[i].tgc_tasks, gt_list)
878                                         if (n->gt_uniq == uniq)
879                                                 break;
880                                 if (n != NULL)
881                                         continue;
882                         }
883                         mincnt = qgroup->tqg_queue[i].tgc_cnt;
884                         idx = i;
885                 }
886         }
887         if (idx == -1)
888                 panic("taskqgroup_find: Failed to pick a qid.");
889
890         return (idx);
891 }
892
893 void
894 taskqgroup_attach(struct taskqgroup *qgroup, struct grouptask *gtask,
895     void *uniq, int irq, char *name)
896 {
897         cpuset_t mask;
898         int qid;
899
900         gtask->gt_uniq = uniq;
901         gtask->gt_name = name;
902         gtask->gt_irq = irq;
903         gtask->gt_cpu = -1;
904         mtx_lock(&qgroup->tqg_lock);
905         qid = taskqgroup_find(qgroup, uniq);
906         qgroup->tqg_queue[qid].tgc_cnt++;
907         LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
908         gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
909         if (irq != -1 && smp_started) {
910                 CPU_ZERO(&mask);
911                 CPU_SET(qgroup->tqg_queue[qid].tgc_cpu, &mask);
912                 mtx_unlock(&qgroup->tqg_lock);
913                 intr_setaffinity(irq, &mask);
914         } else
915                 mtx_unlock(&qgroup->tqg_lock);
916 }
917
918 int
919 taskqgroup_attach_cpu(struct taskqgroup *qgroup, struct grouptask *gtask,
920         void *uniq, int cpu, int irq, char *name)
921 {
922         cpuset_t mask;
923         int i, qid;
924
925         qid = -1;
926         gtask->gt_uniq = uniq;
927         gtask->gt_name = name;
928         gtask->gt_irq = irq;
929         gtask->gt_cpu = cpu;
930         mtx_lock(&qgroup->tqg_lock);
931         if (smp_started) {
932                 for (i = 0; i < qgroup->tqg_cnt; i++)
933                         if (qgroup->tqg_queue[i].tgc_cpu == cpu) {
934                                 qid = i;
935                                 break;
936                         }
937                 if (qid == -1) {
938                         mtx_unlock(&qgroup->tqg_lock);
939                         return (EINVAL);
940                 }
941         } else
942                 qid = 0;
943         qgroup->tqg_queue[qid].tgc_cnt++;
944         LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list);
945         gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
946         if (irq != -1 && smp_started) {
947                 CPU_ZERO(&mask);
948                 CPU_SET(qgroup->tqg_queue[qid].tgc_cpu, &mask);
949                 mtx_unlock(&qgroup->tqg_lock);
950                 intr_setaffinity(irq, &mask);
951         } else
952                 mtx_unlock(&qgroup->tqg_lock);
953         return (0);
954 }
955
956 void
957 taskqgroup_detach(struct taskqgroup *qgroup, struct grouptask *gtask)
958 {
959         int i;
960
961         mtx_lock(&qgroup->tqg_lock);
962         for (i = 0; i < qgroup->tqg_cnt; i++)
963                 if (qgroup->tqg_queue[i].tgc_taskq == gtask->gt_taskqueue)
964                         break;
965         if (i == qgroup->tqg_cnt)
966                 panic("taskqgroup_detach: task not in group\n");
967         qgroup->tqg_queue[i].tgc_cnt--;
968         LIST_REMOVE(gtask, gt_list);
969         mtx_unlock(&qgroup->tqg_lock);
970         gtask->gt_taskqueue = NULL;
971 }
972
973 static void
974 taskqgroup_binder(void *ctx, int pending)
975 {
976         struct taskq_bind_task *task = (struct taskq_bind_task *)ctx;
977         cpuset_t mask;
978         int error;
979
980         CPU_ZERO(&mask);
981         CPU_SET(task->bt_cpuid, &mask);
982         error = cpuset_setthread(curthread->td_tid, &mask);
983         thread_lock(curthread);
984         sched_bind(curthread, task->bt_cpuid);
985         thread_unlock(curthread);
986
987         if (error)
988                 printf("taskqgroup_binder: setaffinity failed: %d\n",
989                     error);
990         free(task, M_DEVBUF);
991 }
992
993 static void
994 taskqgroup_bind(struct taskqgroup *qgroup)
995 {
996         struct taskq_bind_task *task;
997         int i;
998
999         /*
1000          * Bind taskqueue threads to specific CPUs, if they have been assigned
1001          * one.
1002          */
1003         for (i = 0; i < qgroup->tqg_cnt; i++) {
1004                 task = malloc(sizeof (*task), M_DEVBUF, M_NOWAIT);
1005                 TASK_INIT(&task->bt_task, 0, taskqgroup_binder, task);
1006                 task->bt_cpuid = qgroup->tqg_queue[i].tgc_cpu;
1007                 taskqueue_enqueue(qgroup->tqg_queue[i].tgc_taskq,
1008                     &task->bt_task);
1009         }
1010 }
1011
1012 static int
1013 _taskqgroup_adjust(struct taskqgroup *qgroup, int cnt, int stride)
1014 {
1015         LIST_HEAD(, grouptask) gtask_head = LIST_HEAD_INITIALIZER(NULL);
1016         cpuset_t mask;
1017         struct grouptask *gtask;
1018         int i, old_cnt, qid;
1019
1020         mtx_assert(&qgroup->tqg_lock, MA_OWNED);
1021
1022         if (cnt < 1 || cnt * stride > mp_ncpus || !smp_started) {
1023                 printf("taskqgroup_adjust failed cnt: %d stride: %d mp_ncpus: %d smp_started: %d\n",
1024                            cnt, stride, mp_ncpus, smp_started);
1025                 return (EINVAL);
1026         }
1027         if (qgroup->tqg_adjusting) {
1028                 printf("taskqgroup_adjust failed: adjusting\n");
1029                 return (EBUSY);
1030         }
1031         qgroup->tqg_adjusting = 1;
1032         old_cnt = qgroup->tqg_cnt;
1033         mtx_unlock(&qgroup->tqg_lock);
1034         /*
1035          * Set up queue for tasks added before boot.
1036          */
1037         if (old_cnt == 0) {
1038                 LIST_SWAP(&gtask_head, &qgroup->tqg_queue[0].tgc_tasks,
1039                     grouptask, gt_list);
1040                 qgroup->tqg_queue[0].tgc_cnt = 0;
1041         }
1042
1043         /*
1044          * If new taskq threads have been added.
1045          */
1046         for (i = old_cnt; i < cnt; i++)
1047                 taskqgroup_cpu_create(qgroup, i);
1048         mtx_lock(&qgroup->tqg_lock);
1049         qgroup->tqg_cnt = cnt;
1050         qgroup->tqg_stride = stride;
1051
1052         /*
1053          * Adjust drivers to use new taskqs.
1054          */
1055         for (i = 0; i < old_cnt; i++) {
1056                 while ((gtask = LIST_FIRST(&qgroup->tqg_queue[i].tgc_tasks))) {
1057                         LIST_REMOVE(gtask, gt_list);
1058                         qgroup->tqg_queue[i].tgc_cnt--;
1059                         LIST_INSERT_HEAD(&gtask_head, gtask, gt_list);
1060                 }
1061         }
1062
1063         while ((gtask = LIST_FIRST(&gtask_head))) {
1064                 LIST_REMOVE(gtask, gt_list);
1065                 if (gtask->gt_cpu == -1)
1066                         qid = taskqgroup_find(qgroup, gtask->gt_uniq);
1067                 else {
1068                         for (i = 0; i < qgroup->tqg_cnt; i++)
1069                                 if (qgroup->tqg_queue[i].tgc_cpu == gtask->gt_cpu) {
1070                                         qid = i;
1071                                         break;
1072                                 }
1073                 }
1074                 qgroup->tqg_queue[qid].tgc_cnt++;
1075                 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask,
1076                     gt_list);
1077                 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq;
1078         }
1079         /*
1080          * Set new CPU and IRQ affinity
1081          */
1082         for (i = 0; i < cnt; i++) {
1083                 qgroup->tqg_queue[i].tgc_cpu = i * qgroup->tqg_stride;
1084                 CPU_ZERO(&mask);
1085                 CPU_SET(qgroup->tqg_queue[i].tgc_cpu, &mask);
1086                 LIST_FOREACH(gtask, &qgroup->tqg_queue[i].tgc_tasks, gt_list) {
1087                         if (gtask->gt_irq == -1)
1088                                 continue;
1089                         intr_setaffinity(gtask->gt_irq, &mask);
1090                 }
1091         }
1092         mtx_unlock(&qgroup->tqg_lock);
1093
1094         /*
1095          * If taskq thread count has been reduced.
1096          */
1097         for (i = cnt; i < old_cnt; i++)
1098                 taskqgroup_cpu_remove(qgroup, i);
1099
1100         mtx_lock(&qgroup->tqg_lock);
1101         qgroup->tqg_adjusting = 0;
1102
1103         taskqgroup_bind(qgroup);
1104
1105         return (0);
1106 }
1107
1108 int
1109 taskqgroup_adjust(struct taskqgroup *qgroup, int cpu, int stride)
1110 {
1111         int error;
1112
1113         mtx_lock(&qgroup->tqg_lock);
1114         error = _taskqgroup_adjust(qgroup, cpu, stride);
1115         mtx_unlock(&qgroup->tqg_lock);
1116
1117         return (error);
1118 }
1119
1120 struct taskqgroup *
1121 taskqgroup_create(char *name)
1122 {
1123         struct taskqgroup *qgroup;
1124
1125         qgroup = malloc(sizeof(*qgroup), M_TASKQUEUE, M_WAITOK | M_ZERO);
1126         mtx_init(&qgroup->tqg_lock, "taskqgroup", NULL, MTX_DEF);
1127         qgroup->tqg_name = name;
1128         LIST_INIT(&qgroup->tqg_queue[0].tgc_tasks);
1129
1130         return (qgroup);
1131 }
1132
1133 void
1134 taskqgroup_destroy(struct taskqgroup *qgroup)
1135 {
1136
1137 }