]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/kern/subr_taskqueue.c
MFC r306441 and r306634:
[FreeBSD/stable/9.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/interrupt.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/sched.h>
42 #include <sys/taskqueue.h>
43 #include <sys/unistd.h>
44 #include <machine/stdarg.h>
45
46 static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues");
47 static void     *taskqueue_giant_ih;
48 static void     *taskqueue_ih;
49
50 struct taskqueue_busy {
51         struct task     *tb_running;
52         TAILQ_ENTRY(taskqueue_busy) tb_link;
53 };
54
55 struct taskqueue {
56         STAILQ_HEAD(, task)     tq_queue;
57         taskqueue_enqueue_fn    tq_enqueue;
58         void                    *tq_context;
59         TAILQ_HEAD(, taskqueue_busy) tq_active;
60         struct mtx              tq_mutex;
61         struct thread           **tq_threads;
62         int                     tq_tcount;
63         int                     tq_spin;
64         int                     tq_flags;
65         int                     tq_callouts;
66 };
67
68 #define TQ_FLAGS_ACTIVE         (1 << 0)
69 #define TQ_FLAGS_BLOCKED        (1 << 1)
70 #define TQ_FLAGS_PENDING        (1 << 2)
71
72 #define DT_CALLOUT_ARMED        (1 << 0)
73 #define DT_DRAIN_IN_PROGRESS    (1 << 1)
74
75 #define TQ_LOCK(tq)                                                     \
76         do {                                                            \
77                 if ((tq)->tq_spin)                                      \
78                         mtx_lock_spin(&(tq)->tq_mutex);                 \
79                 else                                                    \
80                         mtx_lock(&(tq)->tq_mutex);                      \
81         } while (0)
82
83 #define TQ_UNLOCK(tq)                                                   \
84         do {                                                            \
85                 if ((tq)->tq_spin)                                      \
86                         mtx_unlock_spin(&(tq)->tq_mutex);               \
87                 else                                                    \
88                         mtx_unlock(&(tq)->tq_mutex);                    \
89         } while (0)
90
91 void
92 _timeout_task_init(struct taskqueue *queue, struct timeout_task *timeout_task,
93     int priority, task_fn_t func, void *context)
94 {
95
96         TASK_INIT(&timeout_task->t, priority, func, context);
97         callout_init_mtx(&timeout_task->c, &queue->tq_mutex, 0);
98         timeout_task->q = queue;
99         timeout_task->f = 0;
100 }
101
102 static __inline int
103 TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm,
104     int t)
105 {
106         if (tq->tq_spin)
107                 return (msleep_spin(p, m, wm, t));
108         return (msleep(p, m, pri, wm, t));
109 }
110
111 static struct taskqueue *
112 _taskqueue_create(const char *name __unused, int mflags,
113                  taskqueue_enqueue_fn enqueue, void *context,
114                  int mtxflags, const char *mtxname)
115 {
116         struct taskqueue *queue;
117
118         queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO);
119         if (!queue)
120                 return NULL;
121
122         STAILQ_INIT(&queue->tq_queue);
123         TAILQ_INIT(&queue->tq_active);
124         queue->tq_enqueue = enqueue;
125         queue->tq_context = context;
126         queue->tq_spin = (mtxflags & MTX_SPIN) != 0;
127         queue->tq_flags |= TQ_FLAGS_ACTIVE;
128         mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags);
129
130         return queue;
131 }
132
133 struct taskqueue *
134 taskqueue_create(const char *name, int mflags,
135                  taskqueue_enqueue_fn enqueue, void *context)
136 {
137         return _taskqueue_create(name, mflags, enqueue, context,
138                         MTX_DEF, "taskqueue");
139 }
140
141 /*
142  * Signal a taskqueue thread to terminate.
143  */
144 static void
145 taskqueue_terminate(struct thread **pp, struct taskqueue *tq)
146 {
147
148         while (tq->tq_tcount > 0 || tq->tq_callouts > 0) {
149                 wakeup(tq);
150                 TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0);
151         }
152 }
153
154 void
155 taskqueue_free(struct taskqueue *queue)
156 {
157
158         TQ_LOCK(queue);
159         queue->tq_flags &= ~TQ_FLAGS_ACTIVE;
160         taskqueue_terminate(queue->tq_threads, queue);
161         KASSERT(TAILQ_EMPTY(&queue->tq_active), ("Tasks still running?"));
162         KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks"));
163         mtx_destroy(&queue->tq_mutex);
164         free(queue->tq_threads, M_TASKQUEUE);
165         free(queue, M_TASKQUEUE);
166 }
167
168 static int
169 taskqueue_enqueue_locked(struct taskqueue *queue, struct task *task)
170 {
171         struct task *ins;
172         struct task *prev;
173
174         /*
175          * Count multiple enqueues.
176          */
177         if (task->ta_pending) {
178                 if (task->ta_pending < USHRT_MAX)
179                         task->ta_pending++;
180                 return (0);
181         }
182
183         /*
184          * Optimise the case when all tasks have the same priority.
185          */
186         prev = STAILQ_LAST(&queue->tq_queue, task, ta_link);
187         if (!prev || prev->ta_priority >= task->ta_priority) {
188                 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link);
189         } else {
190                 prev = NULL;
191                 for (ins = STAILQ_FIRST(&queue->tq_queue); ins;
192                      prev = ins, ins = STAILQ_NEXT(ins, ta_link))
193                         if (ins->ta_priority < task->ta_priority)
194                                 break;
195
196                 if (prev)
197                         STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link);
198                 else
199                         STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link);
200         }
201
202         task->ta_pending = 1;
203         if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0)
204                 queue->tq_enqueue(queue->tq_context);
205         else
206                 queue->tq_flags |= TQ_FLAGS_PENDING;
207
208         return (0);
209 }
210 int
211 taskqueue_enqueue(struct taskqueue *queue, struct task *task)
212 {
213         int res;
214
215         TQ_LOCK(queue);
216         res = taskqueue_enqueue_locked(queue, task);
217         TQ_UNLOCK(queue);
218
219         return (res);
220 }
221
222 static void
223 taskqueue_timeout_func(void *arg)
224 {
225         struct taskqueue *queue;
226         struct timeout_task *timeout_task;
227
228         timeout_task = arg;
229         queue = timeout_task->q;
230         KASSERT((timeout_task->f & DT_CALLOUT_ARMED) != 0, ("Stray timeout"));
231         timeout_task->f &= ~DT_CALLOUT_ARMED;
232         queue->tq_callouts--;
233         taskqueue_enqueue_locked(timeout_task->q, &timeout_task->t);
234 }
235
236 int
237 taskqueue_enqueue_timeout(struct taskqueue *queue,
238     struct timeout_task *timeout_task, int ticks)
239 {
240         int res;
241
242         TQ_LOCK(queue);
243         KASSERT(timeout_task->q == NULL || timeout_task->q == queue,
244             ("Migrated queue"));
245         KASSERT(!queue->tq_spin, ("Timeout for spin-queue"));
246         timeout_task->q = queue;
247         res = timeout_task->t.ta_pending;
248         if (timeout_task->f & DT_DRAIN_IN_PROGRESS) {
249                 /* Do nothing */
250                 TQ_UNLOCK(queue);
251                 res = -1;
252         } else if (ticks == 0) {
253                 taskqueue_enqueue_locked(queue, &timeout_task->t);
254         } else {
255                 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
256                         res++;
257                 } else {
258                         queue->tq_callouts++;
259                         timeout_task->f |= DT_CALLOUT_ARMED;
260                         if (ticks < 0)
261                                 ticks = -ticks; /* Ignore overflow. */
262                 }
263                 if (ticks > 0) {
264                         callout_reset(&timeout_task->c, ticks,
265                             taskqueue_timeout_func, timeout_task);
266                 }
267         }
268         TQ_UNLOCK(queue);
269         return (res);
270 }
271
272 static void
273 taskqueue_drain_running(struct taskqueue *queue)
274 {
275
276         while (!TAILQ_EMPTY(&queue->tq_active))
277                 TQ_SLEEP(queue, &queue->tq_active, &queue->tq_mutex,
278                     PWAIT, "-", 0);
279 }
280
281 void
282 taskqueue_block(struct taskqueue *queue)
283 {
284
285         TQ_LOCK(queue);
286         queue->tq_flags |= TQ_FLAGS_BLOCKED;
287         TQ_UNLOCK(queue);
288 }
289
290 void
291 taskqueue_unblock(struct taskqueue *queue)
292 {
293
294         TQ_LOCK(queue);
295         queue->tq_flags &= ~TQ_FLAGS_BLOCKED;
296         if (queue->tq_flags & TQ_FLAGS_PENDING) {
297                 queue->tq_flags &= ~TQ_FLAGS_PENDING;
298                 queue->tq_enqueue(queue->tq_context);
299         }
300         TQ_UNLOCK(queue);
301 }
302
303 static void
304 taskqueue_run_locked(struct taskqueue *queue)
305 {
306         struct taskqueue_busy tb;
307         struct task *task;
308         int pending;
309
310         mtx_assert(&queue->tq_mutex, MA_OWNED);
311         tb.tb_running = NULL;
312         TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link);
313
314         while (STAILQ_FIRST(&queue->tq_queue)) {
315                 /*
316                  * Carefully remove the first task from the queue and
317                  * zero its pending count.
318                  */
319                 task = STAILQ_FIRST(&queue->tq_queue);
320                 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link);
321                 pending = task->ta_pending;
322                 task->ta_pending = 0;
323                 tb.tb_running = task;
324                 TQ_UNLOCK(queue);
325
326                 task->ta_func(task->ta_context, pending);
327
328                 TQ_LOCK(queue);
329                 tb.tb_running = NULL;
330                 wakeup(task);
331         }
332         TAILQ_REMOVE(&queue->tq_active, &tb, tb_link);
333         if (TAILQ_EMPTY(&queue->tq_active))
334                 wakeup(&queue->tq_active);
335 }
336
337 void
338 taskqueue_run(struct taskqueue *queue)
339 {
340
341         TQ_LOCK(queue);
342         taskqueue_run_locked(queue);
343         TQ_UNLOCK(queue);
344 }
345
346 static int
347 task_is_running(struct taskqueue *queue, struct task *task)
348 {
349         struct taskqueue_busy *tb;
350
351         mtx_assert(&queue->tq_mutex, MA_OWNED);
352         TAILQ_FOREACH(tb, &queue->tq_active, tb_link) {
353                 if (tb->tb_running == task)
354                         return (1);
355         }
356         return (0);
357 }
358
359 static int
360 taskqueue_cancel_locked(struct taskqueue *queue, struct task *task,
361     u_int *pendp)
362 {
363
364         if (task->ta_pending > 0)
365                 STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link);
366         if (pendp != NULL)
367                 *pendp = task->ta_pending;
368         task->ta_pending = 0;
369         return (task_is_running(queue, task) ? EBUSY : 0);
370 }
371
372 int
373 taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp)
374 {
375         int error;
376
377         TQ_LOCK(queue);
378         error = taskqueue_cancel_locked(queue, task, pendp);
379         TQ_UNLOCK(queue);
380
381         return (error);
382 }
383
384 int
385 taskqueue_cancel_timeout(struct taskqueue *queue,
386     struct timeout_task *timeout_task, u_int *pendp)
387 {
388         u_int pending, pending1;
389         int error;
390
391         TQ_LOCK(queue);
392         pending = !!callout_stop(&timeout_task->c);
393         error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1);
394         if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) {
395                 timeout_task->f &= ~DT_CALLOUT_ARMED;
396                 queue->tq_callouts--;
397         }
398         TQ_UNLOCK(queue);
399
400         if (pendp != NULL)
401                 *pendp = pending + pending1;
402         return (error);
403 }
404
405 void
406 taskqueue_drain(struct taskqueue *queue, struct task *task)
407 {
408
409         if (!queue->tq_spin)
410                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
411
412         TQ_LOCK(queue);
413         while (task->ta_pending != 0 || task_is_running(queue, task))
414                 TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0);
415         TQ_UNLOCK(queue);
416 }
417
418 void
419 taskqueue_drain_all(struct taskqueue *queue)
420 {
421         struct task *task;
422
423         if (!queue->tq_spin)
424                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
425
426         TQ_LOCK(queue);
427         task = STAILQ_LAST(&queue->tq_queue, task, ta_link);
428         if (task != NULL)
429                 while (task->ta_pending != 0)
430                         TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0);
431         taskqueue_drain_running(queue);
432         KASSERT(STAILQ_EMPTY(&queue->tq_queue),
433             ("taskqueue queue is not empty after draining"));
434         TQ_UNLOCK(queue);
435 }
436
437 void
438 taskqueue_drain_timeout(struct taskqueue *queue,
439     struct timeout_task *timeout_task)
440 {
441
442         /*
443          * Set flag to prevent timer from re-starting during drain:
444          */
445         TQ_LOCK(queue);
446         KASSERT((timeout_task->f & DT_DRAIN_IN_PROGRESS) == 0,
447             ("Drain already in progress"));
448         timeout_task->f |= DT_DRAIN_IN_PROGRESS;
449         TQ_UNLOCK(queue);
450
451         callout_drain(&timeout_task->c);
452         taskqueue_drain(queue, &timeout_task->t);
453
454         /*
455          * Clear flag to allow timer to re-start:
456          */
457         TQ_LOCK(queue);
458         timeout_task->f &= ~DT_DRAIN_IN_PROGRESS;
459         TQ_UNLOCK(queue);
460 }
461
462 static void
463 taskqueue_swi_enqueue(void *context)
464 {
465         swi_sched(taskqueue_ih, 0);
466 }
467
468 static void
469 taskqueue_swi_run(void *dummy)
470 {
471         taskqueue_run(taskqueue_swi);
472 }
473
474 static void
475 taskqueue_swi_giant_enqueue(void *context)
476 {
477         swi_sched(taskqueue_giant_ih, 0);
478 }
479
480 static void
481 taskqueue_swi_giant_run(void *dummy)
482 {
483         taskqueue_run(taskqueue_swi_giant);
484 }
485
486 int
487 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
488                         const char *name, ...)
489 {
490         va_list ap;
491         struct thread *td;
492         struct taskqueue *tq;
493         int i, error;
494         char ktname[MAXCOMLEN + 1];
495
496         if (count <= 0)
497                 return (EINVAL);
498
499         tq = *tqp;
500
501         va_start(ap, name);
502         vsnprintf(ktname, sizeof(ktname), name, ap);
503         va_end(ap);
504
505         tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
506             M_NOWAIT | M_ZERO);
507         if (tq->tq_threads == NULL) {
508                 printf("%s: no memory for %s threads\n", __func__, ktname);
509                 return (ENOMEM);
510         }
511
512         for (i = 0; i < count; i++) {
513                 if (count == 1)
514                         error = kthread_add(taskqueue_thread_loop, tqp, NULL,
515                             &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname);
516                 else
517                         error = kthread_add(taskqueue_thread_loop, tqp, NULL,
518                             &tq->tq_threads[i], RFSTOPPED, 0,
519                             "%s_%d", ktname, i);
520                 if (error) {
521                         /* should be ok to continue, taskqueue_free will dtrt */
522                         printf("%s: kthread_add(%s): error %d", __func__,
523                             ktname, error);
524                         tq->tq_threads[i] = NULL;               /* paranoid */
525                 } else
526                         tq->tq_tcount++;
527         }
528         for (i = 0; i < count; i++) {
529                 if (tq->tq_threads[i] == NULL)
530                         continue;
531                 td = tq->tq_threads[i];
532                 thread_lock(td);
533                 sched_prio(td, pri);
534                 sched_add(td, SRQ_BORING);
535                 thread_unlock(td);
536         }
537
538         return (0);
539 }
540
541 void
542 taskqueue_thread_loop(void *arg)
543 {
544         struct taskqueue **tqp, *tq;
545
546         tqp = arg;
547         tq = *tqp;
548         TQ_LOCK(tq);
549         while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) {
550                 taskqueue_run_locked(tq);
551                 /*
552                  * Because taskqueue_run() can drop tq_mutex, we need to
553                  * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the
554                  * meantime, which means we missed a wakeup.
555                  */
556                 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0)
557                         break;
558                 TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0);
559         }
560         taskqueue_run_locked(tq);
561
562         /* rendezvous with thread that asked us to terminate */
563         tq->tq_tcount--;
564         wakeup_one(tq->tq_threads);
565         TQ_UNLOCK(tq);
566         kthread_exit();
567 }
568
569 void
570 taskqueue_thread_enqueue(void *context)
571 {
572         struct taskqueue **tqp, *tq;
573
574         tqp = context;
575         tq = *tqp;
576
577         mtx_assert(&tq->tq_mutex, MA_OWNED);
578         wakeup_one(tq);
579 }
580
581 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL,
582                  swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ,
583                      INTR_MPSAFE, &taskqueue_ih)); 
584
585 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL,
586                  swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run,
587                      NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih)); 
588
589 TASKQUEUE_DEFINE_THREAD(thread);
590
591 struct taskqueue *
592 taskqueue_create_fast(const char *name, int mflags,
593                  taskqueue_enqueue_fn enqueue, void *context)
594 {
595         return _taskqueue_create(name, mflags, enqueue, context,
596                         MTX_SPIN, "fast_taskqueue");
597 }
598
599 /* NB: for backwards compatibility */
600 int
601 taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task)
602 {
603         return taskqueue_enqueue(queue, task);
604 }
605
606 static void     *taskqueue_fast_ih;
607
608 static void
609 taskqueue_fast_enqueue(void *context)
610 {
611         swi_sched(taskqueue_fast_ih, 0);
612 }
613
614 static void
615 taskqueue_fast_run(void *dummy)
616 {
617         taskqueue_run(taskqueue_fast);
618 }
619
620 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL,
621         swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL,
622         SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih));
623
624 int
625 taskqueue_member(struct taskqueue *queue, struct thread *td)
626 {
627         int i, j, ret = 0;
628
629         for (i = 0, j = 0; ; i++) {
630                 if (queue->tq_threads[i] == NULL)
631                         continue;
632                 if (queue->tq_threads[i] == td) {
633                         ret = 1;
634                         break;
635                 }
636                 if (++j >= queue->tq_tcount)
637                         break;
638         }
639         return (ret);
640 }