1 /*- 2 * Copyright (c) 2000 Doug Rabson 3 * Copyright (c) 2014 Jeff Roberson 4 * Copyright (c) 2016 Matthew Macy 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/cpuset.h> 34 #include <sys/kernel.h> 35 #include <sys/kthread.h> 36 #include <sys/libkern.h> 37 #include <sys/limits.h> 38 #include <sys/lock.h> 39 #include <sys/malloc.h> 40 #include <sys/mutex.h> 41 #include <sys/proc.h> 42 #include <sys/epoch.h> 43 #include <sys/sched.h> 44 #include <sys/smp.h> 45 #include <sys/gtaskqueue.h> 46 #include <sys/unistd.h> 47 #include <machine/stdarg.h> 48 49 static MALLOC_DEFINE(M_GTASKQUEUE, "gtaskqueue", "Group Task Queues"); 50 static void gtaskqueue_thread_enqueue(void *); 51 static void gtaskqueue_thread_loop(void *arg); 52 static int task_is_running(struct gtaskqueue *queue, struct gtask *gtask); 53 static void gtaskqueue_drain_locked(struct gtaskqueue *queue, struct gtask *gtask); 54 55 TASKQGROUP_DEFINE(softirq, mp_ncpus, 1); 56 57 struct gtaskqueue_busy { 58 struct gtask *tb_running; 59 u_int tb_seq; 60 LIST_ENTRY(gtaskqueue_busy) tb_link; 61 }; 62 63 typedef void (*gtaskqueue_enqueue_fn)(void *context); 64 65 struct gtaskqueue { 66 STAILQ_HEAD(, gtask) tq_queue; 67 LIST_HEAD(, gtaskqueue_busy) tq_active; 68 u_int tq_seq; 69 int tq_callouts; 70 struct mtx_padalign tq_mutex; 71 gtaskqueue_enqueue_fn tq_enqueue; 72 void *tq_context; 73 char *tq_name; 74 struct thread **tq_threads; 75 int tq_tcount; 76 int tq_spin; 77 int tq_flags; 78 taskqueue_callback_fn tq_callbacks[TASKQUEUE_NUM_CALLBACKS]; 79 void *tq_cb_contexts[TASKQUEUE_NUM_CALLBACKS]; 80 }; 81 82 #define TQ_FLAGS_ACTIVE (1 << 0) 83 #define TQ_FLAGS_BLOCKED (1 << 1) 84 #define TQ_FLAGS_UNLOCKED_ENQUEUE (1 << 2) 85 86 #define DT_CALLOUT_ARMED (1 << 0) 87 88 #define TQ_LOCK(tq) \ 89 do { \ 90 if ((tq)->tq_spin) \ 91 mtx_lock_spin(&(tq)->tq_mutex); \ 92 else \ 93 mtx_lock(&(tq)->tq_mutex); \ 94 } while (0) 95 #define TQ_ASSERT_LOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_OWNED) 96 97 #define TQ_UNLOCK(tq) \ 98 do { \ 99 if ((tq)->tq_spin) \ 100 mtx_unlock_spin(&(tq)->tq_mutex); \ 101 else \ 102 mtx_unlock(&(tq)->tq_mutex); \ 103 } while (0) 104 #define TQ_ASSERT_UNLOCKED(tq) mtx_assert(&(tq)->tq_mutex, MA_NOTOWNED) 105 106 #ifdef INVARIANTS 107 static void 108 gtask_dump(struct gtask *gtask) 109 { 110 printf("gtask: %p ta_flags=%x ta_priority=%d ta_func=%p ta_context=%p\n", 111 gtask, gtask->ta_flags, gtask->ta_priority, gtask->ta_func, gtask->ta_context); 112 } 113 #endif 114 115 static __inline int 116 TQ_SLEEP(struct gtaskqueue *tq, void *p, const char *wm) 117 { 118 if (tq->tq_spin) 119 return (msleep_spin(p, (struct mtx *)&tq->tq_mutex, wm, 0)); 120 return (msleep(p, &tq->tq_mutex, 0, wm, 0)); 121 } 122 123 static struct gtaskqueue * 124 _gtaskqueue_create(const char *name, int mflags, 125 taskqueue_enqueue_fn enqueue, void *context, 126 int mtxflags, const char *mtxname __unused) 127 { 128 struct gtaskqueue *queue; 129 char *tq_name; 130 131 tq_name = malloc(TASKQUEUE_NAMELEN, M_GTASKQUEUE, mflags | M_ZERO); 132 if (!tq_name) 133 return (NULL); 134 135 snprintf(tq_name, TASKQUEUE_NAMELEN, "%s", (name) ? name : "taskqueue"); 136 137 queue = malloc(sizeof(struct gtaskqueue), M_GTASKQUEUE, mflags | M_ZERO); 138 if (!queue) { 139 free(tq_name, M_GTASKQUEUE); 140 return (NULL); 141 } 142 143 STAILQ_INIT(&queue->tq_queue); 144 LIST_INIT(&queue->tq_active); 145 queue->tq_enqueue = enqueue; 146 queue->tq_context = context; 147 queue->tq_name = tq_name; 148 queue->tq_spin = (mtxflags & MTX_SPIN) != 0; 149 queue->tq_flags |= TQ_FLAGS_ACTIVE; 150 if (enqueue == gtaskqueue_thread_enqueue) 151 queue->tq_flags |= TQ_FLAGS_UNLOCKED_ENQUEUE; 152 mtx_init(&queue->tq_mutex, tq_name, NULL, mtxflags); 153 154 return (queue); 155 } 156 157 /* 158 * Signal a taskqueue thread to terminate. 159 */ 160 static void 161 gtaskqueue_terminate(struct thread **pp, struct gtaskqueue *tq) 162 { 163 164 while (tq->tq_tcount > 0 || tq->tq_callouts > 0) { 165 wakeup(tq); 166 TQ_SLEEP(tq, pp, "gtq_destroy"); 167 } 168 } 169 170 static void __unused 171 gtaskqueue_free(struct gtaskqueue *queue) 172 { 173 174 TQ_LOCK(queue); 175 queue->tq_flags &= ~TQ_FLAGS_ACTIVE; 176 gtaskqueue_terminate(queue->tq_threads, queue); 177 KASSERT(LIST_EMPTY(&queue->tq_active), ("Tasks still running?")); 178 KASSERT(queue->tq_callouts == 0, ("Armed timeout tasks")); 179 mtx_destroy(&queue->tq_mutex); 180 free(queue->tq_threads, M_GTASKQUEUE); 181 free(queue->tq_name, M_GTASKQUEUE); 182 free(queue, M_GTASKQUEUE); 183 } 184 185 /* 186 * Wait for all to complete, then prevent it from being enqueued 187 */ 188 void 189 grouptask_block(struct grouptask *grouptask) 190 { 191 struct gtaskqueue *queue = grouptask->gt_taskqueue; 192 struct gtask *gtask = &grouptask->gt_task; 193 194 #ifdef INVARIANTS 195 if (queue == NULL) { 196 gtask_dump(gtask); 197 panic("queue == NULL"); 198 } 199 #endif 200 TQ_LOCK(queue); 201 gtask->ta_flags |= TASK_NOENQUEUE; 202 gtaskqueue_drain_locked(queue, gtask); 203 TQ_UNLOCK(queue); 204 } 205 206 void 207 grouptask_unblock(struct grouptask *grouptask) 208 { 209 struct gtaskqueue *queue = grouptask->gt_taskqueue; 210 struct gtask *gtask = &grouptask->gt_task; 211 212 #ifdef INVARIANTS 213 if (queue == NULL) { 214 gtask_dump(gtask); 215 panic("queue == NULL"); 216 } 217 #endif 218 TQ_LOCK(queue); 219 gtask->ta_flags &= ~TASK_NOENQUEUE; 220 TQ_UNLOCK(queue); 221 } 222 223 int 224 grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *gtask) 225 { 226 #ifdef INVARIANTS 227 if (queue == NULL) { 228 gtask_dump(gtask); 229 panic("queue == NULL"); 230 } 231 #endif 232 TQ_LOCK(queue); 233 if (gtask->ta_flags & TASK_ENQUEUED) { 234 TQ_UNLOCK(queue); 235 return (0); 236 } 237 if (gtask->ta_flags & TASK_NOENQUEUE) { 238 TQ_UNLOCK(queue); 239 return (EAGAIN); 240 } 241 STAILQ_INSERT_TAIL(&queue->tq_queue, gtask, ta_link); 242 gtask->ta_flags |= TASK_ENQUEUED; 243 TQ_UNLOCK(queue); 244 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0) 245 queue->tq_enqueue(queue->tq_context); 246 return (0); 247 } 248 249 static void 250 gtaskqueue_task_nop_fn(void *context) 251 { 252 } 253 254 /* 255 * Block until all currently queued tasks in this taskqueue 256 * have begun execution. Tasks queued during execution of 257 * this function are ignored. 258 */ 259 static void 260 gtaskqueue_drain_tq_queue(struct gtaskqueue *queue) 261 { 262 struct gtask t_barrier; 263 264 if (STAILQ_EMPTY(&queue->tq_queue)) 265 return; 266 267 /* 268 * Enqueue our barrier after all current tasks, but with 269 * the highest priority so that newly queued tasks cannot 270 * pass it. Because of the high priority, we can not use 271 * taskqueue_enqueue_locked directly (which drops the lock 272 * anyway) so just insert it at tail while we have the 273 * queue lock. 274 */ 275 GTASK_INIT(&t_barrier, 0, USHRT_MAX, gtaskqueue_task_nop_fn, &t_barrier); 276 STAILQ_INSERT_TAIL(&queue->tq_queue, &t_barrier, ta_link); 277 t_barrier.ta_flags |= TASK_ENQUEUED; 278 279 /* 280 * Once the barrier has executed, all previously queued tasks 281 * have completed or are currently executing. 282 */ 283 while (t_barrier.ta_flags & TASK_ENQUEUED) 284 TQ_SLEEP(queue, &t_barrier, "gtq_qdrain"); 285 } 286 287 /* 288 * Block until all currently executing tasks for this taskqueue 289 * complete. Tasks that begin execution during the execution 290 * of this function are ignored. 291 */ 292 static void 293 gtaskqueue_drain_tq_active(struct gtaskqueue *queue) 294 { 295 struct gtaskqueue_busy *tb; 296 u_int seq; 297 298 if (LIST_EMPTY(&queue->tq_active)) 299 return; 300 301 /* Block taskq_terminate().*/ 302 queue->tq_callouts++; 303 304 /* Wait for any active task with sequence from the past. */ 305 seq = queue->tq_seq; 306 restart: 307 LIST_FOREACH(tb, &queue->tq_active, tb_link) { 308 if ((int)(tb->tb_seq - seq) <= 0) { 309 TQ_SLEEP(queue, tb->tb_running, "gtq_adrain"); 310 goto restart; 311 } 312 } 313 314 /* Release taskqueue_terminate(). */ 315 queue->tq_callouts--; 316 if ((queue->tq_flags & TQ_FLAGS_ACTIVE) == 0) 317 wakeup_one(queue->tq_threads); 318 } 319 320 void 321 gtaskqueue_block(struct gtaskqueue *queue) 322 { 323 324 TQ_LOCK(queue); 325 queue->tq_flags |= TQ_FLAGS_BLOCKED; 326 TQ_UNLOCK(queue); 327 } 328 329 void 330 gtaskqueue_unblock(struct gtaskqueue *queue) 331 { 332 333 TQ_LOCK(queue); 334 queue->tq_flags &= ~TQ_FLAGS_BLOCKED; 335 if (!STAILQ_EMPTY(&queue->tq_queue)) 336 queue->tq_enqueue(queue->tq_context); 337 TQ_UNLOCK(queue); 338 } 339 340 static void 341 gtaskqueue_run_locked(struct gtaskqueue *queue) 342 { 343 struct epoch_tracker et; 344 struct gtaskqueue_busy tb; 345 struct gtask *gtask; 346 bool in_net_epoch; 347 348 KASSERT(queue != NULL, ("tq is NULL")); 349 TQ_ASSERT_LOCKED(queue); 350 tb.tb_running = NULL; 351 LIST_INSERT_HEAD(&queue->tq_active, &tb, tb_link); 352 in_net_epoch = false; 353 354 while ((gtask = STAILQ_FIRST(&queue->tq_queue)) != NULL) { 355 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link); 356 gtask->ta_flags &= ~TASK_ENQUEUED; 357 tb.tb_running = gtask; 358 tb.tb_seq = ++queue->tq_seq; 359 TQ_UNLOCK(queue); 360 361 KASSERT(gtask->ta_func != NULL, ("task->ta_func is NULL")); 362 if (!in_net_epoch && TASK_IS_NET(gtask)) { 363 in_net_epoch = true; 364 NET_EPOCH_ENTER(et); 365 } else if (in_net_epoch && !TASK_IS_NET(gtask)) { 366 NET_EPOCH_EXIT(et); 367 in_net_epoch = false; 368 } 369 gtask->ta_func(gtask->ta_context); 370 371 TQ_LOCK(queue); 372 wakeup(gtask); 373 } 374 if (in_net_epoch) 375 NET_EPOCH_EXIT(et); 376 LIST_REMOVE(&tb, tb_link); 377 } 378 379 static int 380 task_is_running(struct gtaskqueue *queue, struct gtask *gtask) 381 { 382 struct gtaskqueue_busy *tb; 383 384 TQ_ASSERT_LOCKED(queue); 385 LIST_FOREACH(tb, &queue->tq_active, tb_link) { 386 if (tb->tb_running == gtask) 387 return (1); 388 } 389 return (0); 390 } 391 392 static int 393 gtaskqueue_cancel_locked(struct gtaskqueue *queue, struct gtask *gtask) 394 { 395 396 if (gtask->ta_flags & TASK_ENQUEUED) 397 STAILQ_REMOVE(&queue->tq_queue, gtask, gtask, ta_link); 398 gtask->ta_flags &= ~TASK_ENQUEUED; 399 return (task_is_running(queue, gtask) ? EBUSY : 0); 400 } 401 402 int 403 gtaskqueue_cancel(struct gtaskqueue *queue, struct gtask *gtask) 404 { 405 int error; 406 407 TQ_LOCK(queue); 408 error = gtaskqueue_cancel_locked(queue, gtask); 409 TQ_UNLOCK(queue); 410 411 return (error); 412 } 413 414 static void 415 gtaskqueue_drain_locked(struct gtaskqueue *queue, struct gtask *gtask) 416 { 417 while ((gtask->ta_flags & TASK_ENQUEUED) || task_is_running(queue, gtask)) 418 TQ_SLEEP(queue, gtask, "gtq_drain"); 419 } 420 421 void 422 gtaskqueue_drain(struct gtaskqueue *queue, struct gtask *gtask) 423 { 424 425 if (!queue->tq_spin) 426 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); 427 428 TQ_LOCK(queue); 429 gtaskqueue_drain_locked(queue, gtask); 430 TQ_UNLOCK(queue); 431 } 432 433 void 434 gtaskqueue_drain_all(struct gtaskqueue *queue) 435 { 436 437 if (!queue->tq_spin) 438 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); 439 440 TQ_LOCK(queue); 441 gtaskqueue_drain_tq_queue(queue); 442 gtaskqueue_drain_tq_active(queue); 443 TQ_UNLOCK(queue); 444 } 445 446 static int 447 _gtaskqueue_start_threads(struct gtaskqueue **tqp, int count, int pri, 448 cpuset_t *mask, const char *name, va_list ap) 449 { 450 char ktname[MAXCOMLEN + 1]; 451 struct thread *td; 452 struct gtaskqueue *tq; 453 int i, error; 454 455 if (count <= 0) 456 return (EINVAL); 457 458 vsnprintf(ktname, sizeof(ktname), name, ap); 459 tq = *tqp; 460 461 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_GTASKQUEUE, 462 M_NOWAIT | M_ZERO); 463 if (tq->tq_threads == NULL) { 464 printf("%s: no memory for %s threads\n", __func__, ktname); 465 return (ENOMEM); 466 } 467 468 for (i = 0; i < count; i++) { 469 if (count == 1) 470 error = kthread_add(gtaskqueue_thread_loop, tqp, NULL, 471 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname); 472 else 473 error = kthread_add(gtaskqueue_thread_loop, tqp, NULL, 474 &tq->tq_threads[i], RFSTOPPED, 0, 475 "%s_%d", ktname, i); 476 if (error) { 477 /* should be ok to continue, taskqueue_free will dtrt */ 478 printf("%s: kthread_add(%s): error %d", __func__, 479 ktname, error); 480 tq->tq_threads[i] = NULL; /* paranoid */ 481 } else 482 tq->tq_tcount++; 483 } 484 for (i = 0; i < count; i++) { 485 if (tq->tq_threads[i] == NULL) 486 continue; 487 td = tq->tq_threads[i]; 488 if (mask) { 489 error = cpuset_setthread(td->td_tid, mask); 490 /* 491 * Failing to pin is rarely an actual fatal error; 492 * it'll just affect performance. 493 */ 494 if (error) 495 printf("%s: curthread=%llu: can't pin; " 496 "error=%d\n", 497 __func__, 498 (unsigned long long) td->td_tid, 499 error); 500 } 501 thread_lock(td); 502 sched_prio(td, pri); 503 sched_add(td, SRQ_BORING); 504 } 505 506 return (0); 507 } 508 509 static int 510 gtaskqueue_start_threads(struct gtaskqueue **tqp, int count, int pri, 511 const char *name, ...) 512 { 513 va_list ap; 514 int error; 515 516 va_start(ap, name); 517 error = _gtaskqueue_start_threads(tqp, count, pri, NULL, name, ap); 518 va_end(ap); 519 return (error); 520 } 521 522 static inline void 523 gtaskqueue_run_callback(struct gtaskqueue *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 534 static void 535 gtaskqueue_thread_loop(void *arg) 536 { 537 struct gtaskqueue **tqp, *tq; 538 539 tqp = arg; 540 tq = *tqp; 541 gtaskqueue_run_callback(tq, TASKQUEUE_CALLBACK_TYPE_INIT); 542 TQ_LOCK(tq); 543 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) { 544 /* XXX ? */ 545 gtaskqueue_run_locked(tq); 546 /* 547 * Because taskqueue_run() can drop tq_mutex, we need to 548 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the 549 * meantime, which means we missed a wakeup. 550 */ 551 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0) 552 break; 553 TQ_SLEEP(tq, tq, "-"); 554 } 555 gtaskqueue_run_locked(tq); 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 gtaskqueue_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 572 static void 573 gtaskqueue_thread_enqueue(void *context) 574 { 575 struct gtaskqueue **tqp, *tq; 576 577 tqp = context; 578 tq = *tqp; 579 wakeup_any(tq); 580 } 581 582 static struct gtaskqueue * 583 gtaskqueue_create_fast(const char *name, int mflags, 584 taskqueue_enqueue_fn enqueue, void *context) 585 { 586 return _gtaskqueue_create(name, mflags, enqueue, context, 587 MTX_SPIN, "fast_taskqueue"); 588 } 589 590 struct taskqgroup_cpu { 591 LIST_HEAD(, grouptask) tgc_tasks; 592 struct gtaskqueue *tgc_taskq; 593 int tgc_cnt; 594 int tgc_cpu; 595 }; 596 597 struct taskqgroup { 598 struct taskqgroup_cpu tqg_queue[MAXCPU]; 599 struct mtx tqg_lock; 600 const char * tqg_name; 601 int tqg_cnt; 602 }; 603 604 struct taskq_bind_task { 605 struct gtask bt_task; 606 int bt_cpuid; 607 }; 608 609 static void 610 taskqgroup_cpu_create(struct taskqgroup *qgroup, int idx, int cpu) 611 { 612 struct taskqgroup_cpu *qcpu; 613 614 qcpu = &qgroup->tqg_queue[idx]; 615 LIST_INIT(&qcpu->tgc_tasks); 616 qcpu->tgc_taskq = gtaskqueue_create_fast(NULL, M_WAITOK, 617 taskqueue_thread_enqueue, &qcpu->tgc_taskq); 618 gtaskqueue_start_threads(&qcpu->tgc_taskq, 1, PI_SOFT, 619 "%s_%d", qgroup->tqg_name, idx); 620 qcpu->tgc_cpu = cpu; 621 } 622 623 /* 624 * Find the taskq with least # of tasks that doesn't currently have any 625 * other queues from the uniq identifier. 626 */ 627 static int 628 taskqgroup_find(struct taskqgroup *qgroup, void *uniq) 629 { 630 struct grouptask *n; 631 int i, idx, mincnt; 632 int strict; 633 634 mtx_assert(&qgroup->tqg_lock, MA_OWNED); 635 KASSERT(qgroup->tqg_cnt != 0, 636 ("qgroup %s has no queues", qgroup->tqg_name)); 637 638 /* 639 * Two passes: first scan for a queue with the least tasks that 640 * does not already service this uniq id. If that fails simply find 641 * the queue with the least total tasks. 642 */ 643 for (idx = -1, mincnt = INT_MAX, strict = 1; mincnt == INT_MAX; 644 strict = 0) { 645 for (i = 0; i < qgroup->tqg_cnt; i++) { 646 if (qgroup->tqg_queue[i].tgc_cnt > mincnt) 647 continue; 648 if (strict) { 649 LIST_FOREACH(n, &qgroup->tqg_queue[i].tgc_tasks, 650 gt_list) 651 if (n->gt_uniq == uniq) 652 break; 653 if (n != NULL) 654 continue; 655 } 656 mincnt = qgroup->tqg_queue[i].tgc_cnt; 657 idx = i; 658 } 659 } 660 if (idx == -1) 661 panic("%s: failed to pick a qid.", __func__); 662 663 return (idx); 664 } 665 666 void 667 taskqgroup_attach(struct taskqgroup *qgroup, struct grouptask *gtask, 668 void *uniq, device_t dev, struct resource *irq, const char *name) 669 { 670 int cpu, qid, error; 671 672 KASSERT(qgroup->tqg_cnt > 0, 673 ("qgroup %s has no queues", qgroup->tqg_name)); 674 675 gtask->gt_uniq = uniq; 676 snprintf(gtask->gt_name, GROUPTASK_NAMELEN, "%s", name ? name : "grouptask"); 677 gtask->gt_dev = dev; 678 gtask->gt_irq = irq; 679 gtask->gt_cpu = -1; 680 mtx_lock(&qgroup->tqg_lock); 681 qid = taskqgroup_find(qgroup, uniq); 682 qgroup->tqg_queue[qid].tgc_cnt++; 683 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list); 684 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq; 685 if (dev != NULL && irq != NULL) { 686 cpu = qgroup->tqg_queue[qid].tgc_cpu; 687 gtask->gt_cpu = cpu; 688 mtx_unlock(&qgroup->tqg_lock); 689 error = bus_bind_intr(dev, irq, cpu); 690 if (error) 691 printf("%s: binding interrupt failed for %s: %d\n", 692 __func__, gtask->gt_name, error); 693 } else 694 mtx_unlock(&qgroup->tqg_lock); 695 } 696 697 int 698 taskqgroup_attach_cpu(struct taskqgroup *qgroup, struct grouptask *gtask, 699 void *uniq, int cpu, device_t dev, struct resource *irq, const char *name) 700 { 701 int i, qid, error; 702 703 gtask->gt_uniq = uniq; 704 snprintf(gtask->gt_name, GROUPTASK_NAMELEN, "%s", name ? name : "grouptask"); 705 gtask->gt_dev = dev; 706 gtask->gt_irq = irq; 707 gtask->gt_cpu = cpu; 708 mtx_lock(&qgroup->tqg_lock); 709 for (i = 0, qid = -1; i < qgroup->tqg_cnt; i++) 710 if (qgroup->tqg_queue[i].tgc_cpu == cpu) { 711 qid = i; 712 break; 713 } 714 if (qid == -1) { 715 mtx_unlock(&qgroup->tqg_lock); 716 printf("%s: qid not found for %s cpu=%d\n", __func__, gtask->gt_name, cpu); 717 return (EINVAL); 718 } 719 qgroup->tqg_queue[qid].tgc_cnt++; 720 LIST_INSERT_HEAD(&qgroup->tqg_queue[qid].tgc_tasks, gtask, gt_list); 721 gtask->gt_taskqueue = qgroup->tqg_queue[qid].tgc_taskq; 722 cpu = qgroup->tqg_queue[qid].tgc_cpu; 723 mtx_unlock(&qgroup->tqg_lock); 724 725 if (dev != NULL && irq != NULL) { 726 error = bus_bind_intr(dev, irq, cpu); 727 if (error) 728 printf("%s: binding interrupt failed for %s: %d\n", 729 __func__, gtask->gt_name, error); 730 } 731 return (0); 732 } 733 734 void 735 taskqgroup_detach(struct taskqgroup *qgroup, struct grouptask *gtask) 736 { 737 int i; 738 739 grouptask_block(gtask); 740 mtx_lock(&qgroup->tqg_lock); 741 for (i = 0; i < qgroup->tqg_cnt; i++) 742 if (qgroup->tqg_queue[i].tgc_taskq == gtask->gt_taskqueue) 743 break; 744 if (i == qgroup->tqg_cnt) 745 panic("%s: task %s not in group", __func__, gtask->gt_name); 746 qgroup->tqg_queue[i].tgc_cnt--; 747 LIST_REMOVE(gtask, gt_list); 748 mtx_unlock(&qgroup->tqg_lock); 749 gtask->gt_taskqueue = NULL; 750 gtask->gt_task.ta_flags &= ~TASK_NOENQUEUE; 751 } 752 753 static void 754 taskqgroup_binder(void *ctx) 755 { 756 struct taskq_bind_task *gtask; 757 cpuset_t mask; 758 int error; 759 760 gtask = ctx; 761 CPU_ZERO(&mask); 762 CPU_SET(gtask->bt_cpuid, &mask); 763 error = cpuset_setthread(curthread->td_tid, &mask); 764 thread_lock(curthread); 765 sched_bind(curthread, gtask->bt_cpuid); 766 thread_unlock(curthread); 767 768 if (error) 769 printf("%s: binding curthread failed: %d\n", __func__, error); 770 free(gtask, M_DEVBUF); 771 } 772 773 void 774 taskqgroup_bind(struct taskqgroup *qgroup) 775 { 776 struct taskq_bind_task *gtask; 777 int i; 778 779 /* 780 * Bind taskqueue threads to specific CPUs, if they have been assigned 781 * one. 782 */ 783 if (qgroup->tqg_cnt == 1) 784 return; 785 786 for (i = 0; i < qgroup->tqg_cnt; i++) { 787 gtask = malloc(sizeof(*gtask), M_DEVBUF, M_WAITOK); 788 GTASK_INIT(>ask->bt_task, 0, 0, taskqgroup_binder, gtask); 789 gtask->bt_cpuid = qgroup->tqg_queue[i].tgc_cpu; 790 grouptaskqueue_enqueue(qgroup->tqg_queue[i].tgc_taskq, 791 >ask->bt_task); 792 } 793 } 794 795 struct taskqgroup * 796 taskqgroup_create(const char *name, int cnt, int stride) 797 { 798 struct taskqgroup *qgroup; 799 int cpu, i, j; 800 801 qgroup = malloc(sizeof(*qgroup), M_GTASKQUEUE, M_WAITOK | M_ZERO); 802 mtx_init(&qgroup->tqg_lock, "taskqgroup", NULL, MTX_DEF); 803 qgroup->tqg_name = name; 804 qgroup->tqg_cnt = cnt; 805 806 for (cpu = i = 0; i < cnt; i++) { 807 taskqgroup_cpu_create(qgroup, i, cpu); 808 for (j = 0; j < stride; j++) 809 cpu = CPU_NEXT(cpu); 810 } 811 return (qgroup); 812 } 813 814 void 815 taskqgroup_destroy(struct taskqgroup *qgroup) 816 { 817 } 818 819 void 820 taskqgroup_drain_all(struct taskqgroup *tqg) 821 { 822 struct gtaskqueue *q; 823 824 for (int i = 0; i < mp_ncpus; i++) { 825 q = tqg->tqg_queue[i].tgc_taskq; 826 if (q == NULL) 827 continue; 828 gtaskqueue_drain_all(q); 829 } 830 } 831