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