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 static void taskqueue_fast_enqueue(void *); 50 static void taskqueue_swi_enqueue(void *); 51 static void taskqueue_swi_giant_enqueue(void *); 52 53 struct taskqueue_busy { 54 struct task *tb_running; 55 TAILQ_ENTRY(taskqueue_busy) tb_link; 56 }; 57 58 struct 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 97 void 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 109 static __inline int 110 TQ_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 118 static 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 145 struct taskqueue * 146 taskqueue_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 153 void 154 taskqueue_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 */ 173 static void 174 taskqueue_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 183 void 184 taskqueue_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 197 static int 198 taskqueue_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 with lock released. */ 241 return (0); 242 } 243 int 244 taskqueue_enqueue(struct taskqueue *queue, struct task *task) 245 { 246 int res; 247 248 TQ_LOCK(queue); 249 res = taskqueue_enqueue_locked(queue, task); 250 /* The lock is released inside. */ 251 252 return (res); 253 } 254 255 static void 256 taskqueue_timeout_func(void *arg) 257 { 258 struct taskqueue *queue; 259 struct timeout_task *timeout_task; 260 261 timeout_task = arg; 262 queue = timeout_task->q; 263 KASSERT((timeout_task->f & DT_CALLOUT_ARMED) != 0, ("Stray timeout")); 264 timeout_task->f &= ~DT_CALLOUT_ARMED; 265 queue->tq_callouts--; 266 taskqueue_enqueue_locked(timeout_task->q, &timeout_task->t); 267 /* The lock is released inside. */ 268 } 269 270 int 271 taskqueue_enqueue_timeout(struct taskqueue *queue, 272 struct timeout_task *timeout_task, int ticks) 273 { 274 int res; 275 276 TQ_LOCK(queue); 277 KASSERT(timeout_task->q == NULL || timeout_task->q == queue, 278 ("Migrated queue")); 279 KASSERT(!queue->tq_spin, ("Timeout for spin-queue")); 280 timeout_task->q = queue; 281 res = timeout_task->t.ta_pending; 282 if (ticks == 0) { 283 taskqueue_enqueue_locked(queue, &timeout_task->t); 284 /* The lock is released inside. */ 285 } else { 286 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) { 287 res++; 288 } else { 289 queue->tq_callouts++; 290 timeout_task->f |= DT_CALLOUT_ARMED; 291 if (ticks < 0) 292 ticks = -ticks; /* Ignore overflow. */ 293 } 294 if (ticks > 0) { 295 callout_reset(&timeout_task->c, ticks, 296 taskqueue_timeout_func, timeout_task); 297 } 298 TQ_UNLOCK(queue); 299 } 300 return (res); 301 } 302 303 void 304 taskqueue_block(struct taskqueue *queue) 305 { 306 307 TQ_LOCK(queue); 308 queue->tq_flags |= TQ_FLAGS_BLOCKED; 309 TQ_UNLOCK(queue); 310 } 311 312 void 313 taskqueue_unblock(struct taskqueue *queue) 314 { 315 316 TQ_LOCK(queue); 317 queue->tq_flags &= ~TQ_FLAGS_BLOCKED; 318 if (!STAILQ_EMPTY(&queue->tq_queue)) 319 queue->tq_enqueue(queue->tq_context); 320 TQ_UNLOCK(queue); 321 } 322 323 static void 324 taskqueue_run_locked(struct taskqueue *queue) 325 { 326 struct taskqueue_busy tb; 327 struct task *task; 328 int pending; 329 330 TQ_ASSERT_LOCKED(queue); 331 tb.tb_running = NULL; 332 TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link); 333 334 while (STAILQ_FIRST(&queue->tq_queue)) { 335 /* 336 * Carefully remove the first task from the queue and 337 * zero its pending count. 338 */ 339 task = STAILQ_FIRST(&queue->tq_queue); 340 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link); 341 pending = task->ta_pending; 342 task->ta_pending = 0; 343 tb.tb_running = task; 344 TQ_UNLOCK(queue); 345 346 task->ta_func(task->ta_context, pending); 347 348 TQ_LOCK(queue); 349 tb.tb_running = NULL; 350 wakeup(task); 351 } 352 TAILQ_REMOVE(&queue->tq_active, &tb, tb_link); 353 } 354 355 void 356 taskqueue_run(struct taskqueue *queue) 357 { 358 359 TQ_LOCK(queue); 360 taskqueue_run_locked(queue); 361 TQ_UNLOCK(queue); 362 } 363 364 static int 365 task_is_running(struct taskqueue *queue, struct task *task) 366 { 367 struct taskqueue_busy *tb; 368 369 TQ_ASSERT_LOCKED(queue); 370 TAILQ_FOREACH(tb, &queue->tq_active, tb_link) { 371 if (tb->tb_running == task) 372 return (1); 373 } 374 return (0); 375 } 376 377 static int 378 taskqueue_cancel_locked(struct taskqueue *queue, struct task *task, 379 u_int *pendp) 380 { 381 382 if (task->ta_pending > 0) 383 STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link); 384 if (pendp != NULL) 385 *pendp = task->ta_pending; 386 task->ta_pending = 0; 387 return (task_is_running(queue, task) ? EBUSY : 0); 388 } 389 390 int 391 taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp) 392 { 393 u_int pending; 394 int error; 395 396 TQ_LOCK(queue); 397 pending = task->ta_pending; 398 error = taskqueue_cancel_locked(queue, task, pendp); 399 TQ_UNLOCK(queue); 400 401 return (error); 402 } 403 404 int 405 taskqueue_cancel_timeout(struct taskqueue *queue, 406 struct timeout_task *timeout_task, u_int *pendp) 407 { 408 u_int pending, pending1; 409 int error; 410 411 TQ_LOCK(queue); 412 pending = !!callout_stop(&timeout_task->c); 413 error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1); 414 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) { 415 timeout_task->f &= ~DT_CALLOUT_ARMED; 416 queue->tq_callouts--; 417 } 418 TQ_UNLOCK(queue); 419 420 if (pendp != NULL) 421 *pendp = pending + pending1; 422 return (error); 423 } 424 425 void 426 taskqueue_drain(struct taskqueue *queue, struct task *task) 427 { 428 429 if (!queue->tq_spin) 430 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); 431 432 TQ_LOCK(queue); 433 while (task->ta_pending != 0 || task_is_running(queue, task)) 434 TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0); 435 TQ_UNLOCK(queue); 436 } 437 438 void 439 taskqueue_drain_timeout(struct taskqueue *queue, 440 struct timeout_task *timeout_task) 441 { 442 443 callout_drain(&timeout_task->c); 444 taskqueue_drain(queue, &timeout_task->t); 445 } 446 447 static void 448 taskqueue_swi_enqueue(void *context) 449 { 450 swi_sched(taskqueue_ih, 0); 451 } 452 453 static void 454 taskqueue_swi_run(void *dummy) 455 { 456 taskqueue_run(taskqueue_swi); 457 } 458 459 static void 460 taskqueue_swi_giant_enqueue(void *context) 461 { 462 swi_sched(taskqueue_giant_ih, 0); 463 } 464 465 static void 466 taskqueue_swi_giant_run(void *dummy) 467 { 468 taskqueue_run(taskqueue_swi_giant); 469 } 470 471 int 472 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri, 473 const char *name, ...) 474 { 475 va_list ap; 476 struct thread *td; 477 struct taskqueue *tq; 478 int i, error; 479 char ktname[MAXCOMLEN + 1]; 480 481 if (count <= 0) 482 return (EINVAL); 483 484 tq = *tqp; 485 486 va_start(ap, name); 487 vsnprintf(ktname, sizeof(ktname), name, ap); 488 va_end(ap); 489 490 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE, 491 M_NOWAIT | M_ZERO); 492 if (tq->tq_threads == NULL) { 493 printf("%s: no memory for %s threads\n", __func__, ktname); 494 return (ENOMEM); 495 } 496 497 for (i = 0; i < count; i++) { 498 if (count == 1) 499 error = kthread_add(taskqueue_thread_loop, tqp, NULL, 500 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname); 501 else 502 error = kthread_add(taskqueue_thread_loop, tqp, NULL, 503 &tq->tq_threads[i], RFSTOPPED, 0, 504 "%s_%d", ktname, i); 505 if (error) { 506 /* should be ok to continue, taskqueue_free will dtrt */ 507 printf("%s: kthread_add(%s): error %d", __func__, 508 ktname, error); 509 tq->tq_threads[i] = NULL; /* paranoid */ 510 } else 511 tq->tq_tcount++; 512 } 513 for (i = 0; i < count; i++) { 514 if (tq->tq_threads[i] == NULL) 515 continue; 516 td = tq->tq_threads[i]; 517 thread_lock(td); 518 sched_prio(td, pri); 519 sched_add(td, SRQ_BORING); 520 thread_unlock(td); 521 } 522 523 return (0); 524 } 525 526 static inline void 527 taskqueue_run_callback(struct taskqueue *tq, 528 enum taskqueue_callback_type cb_type) 529 { 530 taskqueue_callback_fn tq_callback; 531 532 TQ_ASSERT_UNLOCKED(tq); 533 tq_callback = tq->tq_callbacks[cb_type]; 534 if (tq_callback != NULL) 535 tq_callback(tq->tq_cb_contexts[cb_type]); 536 } 537 538 void 539 taskqueue_thread_loop(void *arg) 540 { 541 struct taskqueue **tqp, *tq; 542 543 tqp = arg; 544 tq = *tqp; 545 taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT); 546 TQ_LOCK(tq); 547 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) { 548 taskqueue_run_locked(tq); 549 /* 550 * Because taskqueue_run() can drop tq_mutex, we need to 551 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the 552 * meantime, which means we missed a wakeup. 553 */ 554 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0) 555 break; 556 TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0); 557 } 558 taskqueue_run_locked(tq); 559 560 /* 561 * This thread is on its way out, so just drop the lock temporarily 562 * in order to call the shutdown callback. This allows the callback 563 * to look at the taskqueue, even just before it dies. 564 */ 565 TQ_UNLOCK(tq); 566 taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN); 567 TQ_LOCK(tq); 568 569 /* rendezvous with thread that asked us to terminate */ 570 tq->tq_tcount--; 571 wakeup_one(tq->tq_threads); 572 TQ_UNLOCK(tq); 573 kthread_exit(); 574 } 575 576 void 577 taskqueue_thread_enqueue(void *context) 578 { 579 struct taskqueue **tqp, *tq; 580 581 tqp = context; 582 tq = *tqp; 583 584 wakeup_one(tq); 585 } 586 587 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL, 588 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ, 589 INTR_MPSAFE, &taskqueue_ih)); 590 591 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL, 592 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run, 593 NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih)); 594 595 TASKQUEUE_DEFINE_THREAD(thread); 596 597 struct taskqueue * 598 taskqueue_create_fast(const char *name, int mflags, 599 taskqueue_enqueue_fn enqueue, void *context) 600 { 601 return _taskqueue_create(name, mflags, enqueue, context, 602 MTX_SPIN, "fast_taskqueue"); 603 } 604 605 /* NB: for backwards compatibility */ 606 int 607 taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task) 608 { 609 return taskqueue_enqueue(queue, task); 610 } 611 612 static void *taskqueue_fast_ih; 613 614 static void 615 taskqueue_fast_enqueue(void *context) 616 { 617 swi_sched(taskqueue_fast_ih, 0); 618 } 619 620 static void 621 taskqueue_fast_run(void *dummy) 622 { 623 taskqueue_run(taskqueue_fast); 624 } 625 626 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL, 627 swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL, 628 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih)); 629 630 int 631 taskqueue_member(struct taskqueue *queue, struct thread *td) 632 { 633 int i, j, ret = 0; 634 635 for (i = 0, j = 0; ; i++) { 636 if (queue->tq_threads[i] == NULL) 637 continue; 638 if (queue->tq_threads[i] == td) { 639 ret = 1; 640 break; 641 } 642 if (++j >= queue->tq_tcount) 643 break; 644 } 645 return (ret); 646 } 647