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