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 int error; 394 395 TQ_LOCK(queue); 396 error = taskqueue_cancel_locked(queue, task, pendp); 397 TQ_UNLOCK(queue); 398 399 return (error); 400 } 401 402 int 403 taskqueue_cancel_timeout(struct taskqueue *queue, 404 struct timeout_task *timeout_task, u_int *pendp) 405 { 406 u_int pending, pending1; 407 int error; 408 409 TQ_LOCK(queue); 410 pending = !!callout_stop(&timeout_task->c); 411 error = taskqueue_cancel_locked(queue, &timeout_task->t, &pending1); 412 if ((timeout_task->f & DT_CALLOUT_ARMED) != 0) { 413 timeout_task->f &= ~DT_CALLOUT_ARMED; 414 queue->tq_callouts--; 415 } 416 TQ_UNLOCK(queue); 417 418 if (pendp != NULL) 419 *pendp = pending + pending1; 420 return (error); 421 } 422 423 void 424 taskqueue_drain(struct taskqueue *queue, struct task *task) 425 { 426 427 if (!queue->tq_spin) 428 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); 429 430 TQ_LOCK(queue); 431 while (task->ta_pending != 0 || task_is_running(queue, task)) 432 TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0); 433 TQ_UNLOCK(queue); 434 } 435 436 void 437 taskqueue_drain_timeout(struct taskqueue *queue, 438 struct timeout_task *timeout_task) 439 { 440 441 callout_drain(&timeout_task->c); 442 taskqueue_drain(queue, &timeout_task->t); 443 } 444 445 static void 446 taskqueue_swi_enqueue(void *context) 447 { 448 swi_sched(taskqueue_ih, 0); 449 } 450 451 static void 452 taskqueue_swi_run(void *dummy) 453 { 454 taskqueue_run(taskqueue_swi); 455 } 456 457 static void 458 taskqueue_swi_giant_enqueue(void *context) 459 { 460 swi_sched(taskqueue_giant_ih, 0); 461 } 462 463 static void 464 taskqueue_swi_giant_run(void *dummy) 465 { 466 taskqueue_run(taskqueue_swi_giant); 467 } 468 469 int 470 taskqueue_start_threads(struct taskqueue **tqp, int count, int pri, 471 const char *name, ...) 472 { 473 va_list ap; 474 struct thread *td; 475 struct taskqueue *tq; 476 int i, error; 477 char ktname[MAXCOMLEN + 1]; 478 479 if (count <= 0) 480 return (EINVAL); 481 482 tq = *tqp; 483 484 va_start(ap, name); 485 vsnprintf(ktname, sizeof(ktname), name, ap); 486 va_end(ap); 487 488 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE, 489 M_NOWAIT | M_ZERO); 490 if (tq->tq_threads == NULL) { 491 printf("%s: no memory for %s threads\n", __func__, ktname); 492 return (ENOMEM); 493 } 494 495 for (i = 0; i < count; i++) { 496 if (count == 1) 497 error = kthread_add(taskqueue_thread_loop, tqp, NULL, 498 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname); 499 else 500 error = kthread_add(taskqueue_thread_loop, tqp, NULL, 501 &tq->tq_threads[i], RFSTOPPED, 0, 502 "%s_%d", ktname, i); 503 if (error) { 504 /* should be ok to continue, taskqueue_free will dtrt */ 505 printf("%s: kthread_add(%s): error %d", __func__, 506 ktname, error); 507 tq->tq_threads[i] = NULL; /* paranoid */ 508 } else 509 tq->tq_tcount++; 510 } 511 for (i = 0; i < count; i++) { 512 if (tq->tq_threads[i] == NULL) 513 continue; 514 td = tq->tq_threads[i]; 515 thread_lock(td); 516 sched_prio(td, pri); 517 sched_add(td, SRQ_BORING); 518 thread_unlock(td); 519 } 520 521 return (0); 522 } 523 524 static inline void 525 taskqueue_run_callback(struct taskqueue *tq, 526 enum taskqueue_callback_type cb_type) 527 { 528 taskqueue_callback_fn tq_callback; 529 530 TQ_ASSERT_UNLOCKED(tq); 531 tq_callback = tq->tq_callbacks[cb_type]; 532 if (tq_callback != NULL) 533 tq_callback(tq->tq_cb_contexts[cb_type]); 534 } 535 536 void 537 taskqueue_thread_loop(void *arg) 538 { 539 struct taskqueue **tqp, *tq; 540 541 tqp = arg; 542 tq = *tqp; 543 taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT); 544 TQ_LOCK(tq); 545 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) { 546 taskqueue_run_locked(tq); 547 /* 548 * Because taskqueue_run() can drop tq_mutex, we need to 549 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the 550 * meantime, which means we missed a wakeup. 551 */ 552 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0) 553 break; 554 TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0); 555 } 556 taskqueue_run_locked(tq); 557 558 /* 559 * This thread is on its way out, so just drop the lock temporarily 560 * in order to call the shutdown callback. This allows the callback 561 * to look at the taskqueue, even just before it dies. 562 */ 563 TQ_UNLOCK(tq); 564 taskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN); 565 TQ_LOCK(tq); 566 567 /* rendezvous with thread that asked us to terminate */ 568 tq->tq_tcount--; 569 wakeup_one(tq->tq_threads); 570 TQ_UNLOCK(tq); 571 kthread_exit(); 572 } 573 574 void 575 taskqueue_thread_enqueue(void *context) 576 { 577 struct taskqueue **tqp, *tq; 578 579 tqp = context; 580 tq = *tqp; 581 582 wakeup_one(tq); 583 } 584 585 TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL, 586 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ, 587 INTR_MPSAFE, &taskqueue_ih)); 588 589 TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL, 590 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run, 591 NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih)); 592 593 TASKQUEUE_DEFINE_THREAD(thread); 594 595 struct taskqueue * 596 taskqueue_create_fast(const char *name, int mflags, 597 taskqueue_enqueue_fn enqueue, void *context) 598 { 599 return _taskqueue_create(name, mflags, enqueue, context, 600 MTX_SPIN, "fast_taskqueue"); 601 } 602 603 /* NB: for backwards compatibility */ 604 int 605 taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task) 606 { 607 return taskqueue_enqueue(queue, task); 608 } 609 610 static void *taskqueue_fast_ih; 611 612 static void 613 taskqueue_fast_enqueue(void *context) 614 { 615 swi_sched(taskqueue_fast_ih, 0); 616 } 617 618 static void 619 taskqueue_fast_run(void *dummy) 620 { 621 taskqueue_run(taskqueue_fast); 622 } 623 624 TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL, 625 swi_add(NULL, "fast taskq", taskqueue_fast_run, NULL, 626 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih)); 627 628 int 629 taskqueue_member(struct taskqueue *queue, struct thread *td) 630 { 631 int i, j, ret = 0; 632 633 for (i = 0, j = 0; ; i++) { 634 if (queue->tq_threads[i] == NULL) 635 continue; 636 if (queue->tq_threads[i] == td) { 637 ret = 1; 638 break; 639 } 640 if (++j >= queue->tq_tcount) 641 break; 642 } 643 return (ret); 644 } 645