subr_taskqueue.c revision 215750
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 215750 2010-11-23 14:30:22Z avg $"); 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/lock.h> 37#include <sys/malloc.h> 38#include <sys/mutex.h> 39#include <sys/proc.h> 40#include <sys/sched.h> 41#include <sys/taskqueue.h> 42#include <sys/unistd.h> 43#include <machine/stdarg.h> 44 45static MALLOC_DEFINE(M_TASKQUEUE, "taskqueue", "Task Queues"); 46static void *taskqueue_giant_ih; 47static void *taskqueue_ih; 48 49struct taskqueue_busy { 50 struct task *tb_running; 51 TAILQ_ENTRY(taskqueue_busy) tb_link; 52}; 53 54struct taskqueue { 55 STAILQ_HEAD(, task) tq_queue; 56 taskqueue_enqueue_fn tq_enqueue; 57 void *tq_context; 58 TAILQ_HEAD(, taskqueue_busy) tq_active; 59 struct mtx tq_mutex; 60 struct thread **tq_threads; 61 int tq_tcount; 62 int tq_spin; 63 int tq_flags; 64}; 65 66#define TQ_FLAGS_ACTIVE (1 << 0) 67#define TQ_FLAGS_BLOCKED (1 << 1) 68#define TQ_FLAGS_PENDING (1 << 2) 69 70#define TQ_LOCK(tq) \ 71 do { \ 72 if ((tq)->tq_spin) \ 73 mtx_lock_spin(&(tq)->tq_mutex); \ 74 else \ 75 mtx_lock(&(tq)->tq_mutex); \ 76 } while (0) 77 78#define TQ_UNLOCK(tq) \ 79 do { \ 80 if ((tq)->tq_spin) \ 81 mtx_unlock_spin(&(tq)->tq_mutex); \ 82 else \ 83 mtx_unlock(&(tq)->tq_mutex); \ 84 } while (0) 85 86static __inline int 87TQ_SLEEP(struct taskqueue *tq, void *p, struct mtx *m, int pri, const char *wm, 88 int t) 89{ 90 if (tq->tq_spin) 91 return (msleep_spin(p, m, wm, t)); 92 return (msleep(p, m, pri, wm, t)); 93} 94 95static struct taskqueue * 96_taskqueue_create(const char *name __unused, int mflags, 97 taskqueue_enqueue_fn enqueue, void *context, 98 int mtxflags, const char *mtxname) 99{ 100 struct taskqueue *queue; 101 102 queue = malloc(sizeof(struct taskqueue), M_TASKQUEUE, mflags | M_ZERO); 103 if (!queue) 104 return NULL; 105 106 STAILQ_INIT(&queue->tq_queue); 107 TAILQ_INIT(&queue->tq_active); 108 queue->tq_enqueue = enqueue; 109 queue->tq_context = context; 110 queue->tq_spin = (mtxflags & MTX_SPIN) != 0; 111 queue->tq_flags |= TQ_FLAGS_ACTIVE; 112 mtx_init(&queue->tq_mutex, mtxname, NULL, mtxflags); 113 114 return queue; 115} 116 117struct taskqueue * 118taskqueue_create(const char *name, int mflags, 119 taskqueue_enqueue_fn enqueue, void *context) 120{ 121 return _taskqueue_create(name, mflags, enqueue, context, 122 MTX_DEF, "taskqueue"); 123} 124 125/* 126 * Signal a taskqueue thread to terminate. 127 */ 128static void 129taskqueue_terminate(struct thread **pp, struct taskqueue *tq) 130{ 131 132 while (tq->tq_tcount > 0) { 133 wakeup(tq); 134 TQ_SLEEP(tq, pp, &tq->tq_mutex, PWAIT, "taskqueue_destroy", 0); 135 } 136} 137 138void 139taskqueue_free(struct taskqueue *queue) 140{ 141 142 TQ_LOCK(queue); 143 queue->tq_flags &= ~TQ_FLAGS_ACTIVE; 144 taskqueue_terminate(queue->tq_threads, queue); 145 KASSERT(TAILQ_EMPTY(&queue->tq_active), ("Tasks still running?")); 146 mtx_destroy(&queue->tq_mutex); 147 free(queue->tq_threads, M_TASKQUEUE); 148 free(queue, M_TASKQUEUE); 149} 150 151int 152taskqueue_enqueue(struct taskqueue *queue, struct task *task) 153{ 154 struct task *ins; 155 struct task *prev; 156 157 TQ_LOCK(queue); 158 159 /* 160 * Count multiple enqueues. 161 */ 162 if (task->ta_pending) { 163 task->ta_pending++; 164 TQ_UNLOCK(queue); 165 return 0; 166 } 167 168 /* 169 * Optimise the case when all tasks have the same priority. 170 */ 171 prev = STAILQ_LAST(&queue->tq_queue, task, ta_link); 172 if (!prev || prev->ta_priority >= task->ta_priority) { 173 STAILQ_INSERT_TAIL(&queue->tq_queue, task, ta_link); 174 } else { 175 prev = NULL; 176 for (ins = STAILQ_FIRST(&queue->tq_queue); ins; 177 prev = ins, ins = STAILQ_NEXT(ins, ta_link)) 178 if (ins->ta_priority < task->ta_priority) 179 break; 180 181 if (prev) 182 STAILQ_INSERT_AFTER(&queue->tq_queue, prev, task, ta_link); 183 else 184 STAILQ_INSERT_HEAD(&queue->tq_queue, task, ta_link); 185 } 186 187 task->ta_pending = 1; 188 if ((queue->tq_flags & TQ_FLAGS_BLOCKED) == 0) 189 queue->tq_enqueue(queue->tq_context); 190 else 191 queue->tq_flags |= TQ_FLAGS_PENDING; 192 193 TQ_UNLOCK(queue); 194 195 return 0; 196} 197 198void 199taskqueue_block(struct taskqueue *queue) 200{ 201 202 TQ_LOCK(queue); 203 queue->tq_flags |= TQ_FLAGS_BLOCKED; 204 TQ_UNLOCK(queue); 205} 206 207void 208taskqueue_unblock(struct taskqueue *queue) 209{ 210 211 TQ_LOCK(queue); 212 queue->tq_flags &= ~TQ_FLAGS_BLOCKED; 213 if (queue->tq_flags & TQ_FLAGS_PENDING) { 214 queue->tq_flags &= ~TQ_FLAGS_PENDING; 215 queue->tq_enqueue(queue->tq_context); 216 } 217 TQ_UNLOCK(queue); 218} 219 220static void 221taskqueue_run_locked(struct taskqueue *queue) 222{ 223 struct taskqueue_busy tb; 224 struct task *task; 225 int pending; 226 227 mtx_assert(&queue->tq_mutex, MA_OWNED); 228 tb.tb_running = NULL; 229 TAILQ_INSERT_TAIL(&queue->tq_active, &tb, tb_link); 230 231 while (STAILQ_FIRST(&queue->tq_queue)) { 232 /* 233 * Carefully remove the first task from the queue and 234 * zero its pending count. 235 */ 236 task = STAILQ_FIRST(&queue->tq_queue); 237 STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link); 238 pending = task->ta_pending; 239 task->ta_pending = 0; 240 tb.tb_running = task; 241 TQ_UNLOCK(queue); 242 243 task->ta_func(task->ta_context, pending); 244 245 TQ_LOCK(queue); 246 tb.tb_running = NULL; 247 wakeup(task); 248 } 249 TAILQ_REMOVE(&queue->tq_active, &tb, tb_link); 250} 251 252void 253taskqueue_run(struct taskqueue *queue) 254{ 255 256 TQ_LOCK(queue); 257 taskqueue_run_locked(queue); 258 TQ_UNLOCK(queue); 259} 260 261static int 262task_is_running(struct taskqueue *queue, struct task *task) 263{ 264 struct taskqueue_busy *tb; 265 266 mtx_assert(&queue->tq_mutex, MA_OWNED); 267 TAILQ_FOREACH(tb, &queue->tq_active, tb_link) { 268 if (tb->tb_running == task) 269 return (1); 270 } 271 return (0); 272} 273 274int 275taskqueue_cancel(struct taskqueue *queue, struct task *task, u_int *pendp) 276{ 277 u_int pending; 278 int error; 279 280 TQ_LOCK(queue); 281 if ((pending = task->ta_pending) > 0) 282 STAILQ_REMOVE(&queue->tq_queue, task, task, ta_link); 283 task->ta_pending = 0; 284 error = task_is_running(queue, task) ? EBUSY : 0; 285 TQ_UNLOCK(queue); 286 287 if (pendp != NULL) 288 *pendp = pending; 289 return (error); 290} 291 292void 293taskqueue_drain(struct taskqueue *queue, struct task *task) 294{ 295 296 if (!queue->tq_spin) 297 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); 298 299 TQ_LOCK(queue); 300 while (task->ta_pending != 0 || task_is_running(queue, task)) 301 TQ_SLEEP(queue, task, &queue->tq_mutex, PWAIT, "-", 0); 302 TQ_UNLOCK(queue); 303} 304 305static void 306taskqueue_swi_enqueue(void *context) 307{ 308 swi_sched(taskqueue_ih, 0); 309} 310 311static void 312taskqueue_swi_run(void *dummy) 313{ 314 taskqueue_run(taskqueue_swi); 315} 316 317static void 318taskqueue_swi_giant_enqueue(void *context) 319{ 320 swi_sched(taskqueue_giant_ih, 0); 321} 322 323static void 324taskqueue_swi_giant_run(void *dummy) 325{ 326 taskqueue_run(taskqueue_swi_giant); 327} 328 329int 330taskqueue_start_threads(struct taskqueue **tqp, int count, int pri, 331 const char *name, ...) 332{ 333 va_list ap; 334 struct thread *td; 335 struct taskqueue *tq; 336 int i, error; 337 char ktname[MAXCOMLEN + 1]; 338 339 if (count <= 0) 340 return (EINVAL); 341 342 tq = *tqp; 343 344 va_start(ap, name); 345 vsnprintf(ktname, sizeof(ktname), name, ap); 346 va_end(ap); 347 348 tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE, 349 M_NOWAIT | M_ZERO); 350 if (tq->tq_threads == NULL) { 351 printf("%s: no memory for %s threads\n", __func__, ktname); 352 return (ENOMEM); 353 } 354 355 for (i = 0; i < count; i++) { 356 if (count == 1) 357 error = kthread_add(taskqueue_thread_loop, tqp, NULL, 358 &tq->tq_threads[i], RFSTOPPED, 0, "%s", ktname); 359 else 360 error = kthread_add(taskqueue_thread_loop, tqp, NULL, 361 &tq->tq_threads[i], RFSTOPPED, 0, 362 "%s_%d", ktname, i); 363 if (error) { 364 /* should be ok to continue, taskqueue_free will dtrt */ 365 printf("%s: kthread_add(%s): error %d", __func__, 366 ktname, error); 367 tq->tq_threads[i] = NULL; /* paranoid */ 368 } else 369 tq->tq_tcount++; 370 } 371 for (i = 0; i < count; i++) { 372 if (tq->tq_threads[i] == NULL) 373 continue; 374 td = tq->tq_threads[i]; 375 thread_lock(td); 376 sched_prio(td, pri); 377 sched_add(td, SRQ_BORING); 378 thread_unlock(td); 379 } 380 381 return (0); 382} 383 384void 385taskqueue_thread_loop(void *arg) 386{ 387 struct taskqueue **tqp, *tq; 388 389 tqp = arg; 390 tq = *tqp; 391 TQ_LOCK(tq); 392 while ((tq->tq_flags & TQ_FLAGS_ACTIVE) != 0) { 393 taskqueue_run_locked(tq); 394 /* 395 * Because taskqueue_run() can drop tq_mutex, we need to 396 * check if the TQ_FLAGS_ACTIVE flag wasn't removed in the 397 * meantime, which means we missed a wakeup. 398 */ 399 if ((tq->tq_flags & TQ_FLAGS_ACTIVE) == 0) 400 break; 401 TQ_SLEEP(tq, tq, &tq->tq_mutex, 0, "-", 0); 402 } 403 taskqueue_run_locked(tq); 404 405 /* rendezvous with thread that asked us to terminate */ 406 tq->tq_tcount--; 407 wakeup_one(tq->tq_threads); 408 TQ_UNLOCK(tq); 409 kthread_exit(); 410} 411 412void 413taskqueue_thread_enqueue(void *context) 414{ 415 struct taskqueue **tqp, *tq; 416 417 tqp = context; 418 tq = *tqp; 419 420 mtx_assert(&tq->tq_mutex, MA_OWNED); 421 wakeup_one(tq); 422} 423 424TASKQUEUE_DEFINE(swi, taskqueue_swi_enqueue, NULL, 425 swi_add(NULL, "task queue", taskqueue_swi_run, NULL, SWI_TQ, 426 INTR_MPSAFE, &taskqueue_ih)); 427 428TASKQUEUE_DEFINE(swi_giant, taskqueue_swi_giant_enqueue, NULL, 429 swi_add(NULL, "Giant taskq", taskqueue_swi_giant_run, 430 NULL, SWI_TQ_GIANT, 0, &taskqueue_giant_ih)); 431 432TASKQUEUE_DEFINE_THREAD(thread); 433 434struct taskqueue * 435taskqueue_create_fast(const char *name, int mflags, 436 taskqueue_enqueue_fn enqueue, void *context) 437{ 438 return _taskqueue_create(name, mflags, enqueue, context, 439 MTX_SPIN, "fast_taskqueue"); 440} 441 442/* NB: for backwards compatibility */ 443int 444taskqueue_enqueue_fast(struct taskqueue *queue, struct task *task) 445{ 446 return taskqueue_enqueue(queue, task); 447} 448 449static void *taskqueue_fast_ih; 450 451static void 452taskqueue_fast_enqueue(void *context) 453{ 454 swi_sched(taskqueue_fast_ih, 0); 455} 456 457static void 458taskqueue_fast_run(void *dummy) 459{ 460 taskqueue_run(taskqueue_fast); 461} 462 463TASKQUEUE_FAST_DEFINE(fast, taskqueue_fast_enqueue, NULL, 464 swi_add(NULL, "Fast task queue", taskqueue_fast_run, NULL, 465 SWI_TQ_FAST, INTR_MPSAFE, &taskqueue_fast_ih)); 466 467int 468taskqueue_member(struct taskqueue *queue, struct thread *td) 469{ 470 int i, j, ret = 0; 471 472 TQ_LOCK(queue); 473 for (i = 0, j = 0; ; i++) { 474 if (queue->tq_threads[i] == NULL) 475 continue; 476 if (queue->tq_threads[i] == td) { 477 ret = 1; 478 break; 479 } 480 if (++j >= queue->tq_tcount) 481 break; 482 } 483 TQ_UNLOCK(queue); 484 return (ret); 485} 486