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