]> git.proxmox.com Git - mirror_spl.git/blame - module/spl/spl-taskq.c
Provide kstat for taskqs
[mirror_spl.git] / module / spl / spl-taskq.c
CommitLineData
2c4332cf 1/*
716154c5
BB
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
715f6251 6 * UCRL-CODE-235197
7 *
716154c5 8 * This file is part of the SPL, Solaris Porting Layer.
3d6af2dd 9 * For details, see <http://zfsonlinux.org/>.
715f6251 10 *
716154c5
BB
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251 17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
716154c5 22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
2c4332cf 23 *
716154c5 24 * Solaris Porting Layer (SPL) Task Queue Implementation.
2c4332cf 25 */
715f6251 26
f4b37741 27#include <sys/taskq.h>
3d061e9d 28#include <sys/kmem.h>
937879f1 29
703371d8
AV
30int spl_taskq_thread_bind = 0;
31module_param(spl_taskq_thread_bind, int, 0644);
32MODULE_PARM_DESC(spl_taskq_thread_bind, "Bind taskq thread to CPU by default");
33
f7a973d9
BB
34
35int spl_taskq_thread_dynamic = 1;
36module_param(spl_taskq_thread_dynamic, int, 0644);
37MODULE_PARM_DESC(spl_taskq_thread_dynamic, "Allow dynamic taskq threads");
38
62aa81a5
BB
39int spl_taskq_thread_priority = 1;
40module_param(spl_taskq_thread_priority, int, 0644);
41MODULE_PARM_DESC(spl_taskq_thread_priority,
2c4332cf 42 "Allow non-default priority for taskq threads");
62aa81a5 43
f7a973d9
BB
44int spl_taskq_thread_sequential = 4;
45module_param(spl_taskq_thread_sequential, int, 0644);
46MODULE_PARM_DESC(spl_taskq_thread_sequential,
2c4332cf 47 "Create new taskq threads after N sequential tasks");
f7a973d9 48
e9cb2b4f
BB
49/* Global system-wide dynamic task queue available for all consumers */
50taskq_t *system_taskq;
51EXPORT_SYMBOL(system_taskq);
52
f7a973d9
BB
53/* Private dedicated taskq for creating new taskq threads on demand. */
54static taskq_t *dynamic_taskq;
55static taskq_thread_t *taskq_thread_create(taskq_t *);
56
200366f2
TC
57/* List of all taskqs */
58LIST_HEAD(tq_list);
59DECLARE_RWSEM(tq_list_sem);
60
9b51f218
BB
61static int
62task_km_flags(uint_t flags)
63{
64 if (flags & TQ_NOSLEEP)
2c4332cf 65 return (KM_NOSLEEP);
9b51f218
BB
66
67 if (flags & TQ_PUSHPAGE)
2c4332cf 68 return (KM_PUSHPAGE);
9b51f218 69
2c4332cf 70 return (KM_SLEEP);
9b51f218
BB
71}
72
200366f2
TC
73/*
74 * taskq_find_by_name - Find the largest instance number of a named taskq.
75 */
76static int
77taskq_find_by_name(const char *name)
78{
79 struct list_head *tql;
80 taskq_t *tq;
81
82 list_for_each_prev(tql, &tq_list) {
83 tq = list_entry(tql, taskq_t, tq_taskqs);
84 if (strcmp(name, tq->tq_name) == 0)
85 return tq->tq_instance;
86 }
87 return (-1);
88}
89
82387586
BB
90/*
91 * NOTE: Must be called with tq->tq_lock held, returns a list_t which
bcd68186 92 * is not attached to the free, work, or pending taskq lists.
f1ca4da6 93 */
046a70c9 94static taskq_ent_t *
066b89e6 95task_alloc(taskq_t *tq, uint_t flags, unsigned long *irqflags)
bcd68186 96{
472a34ca
BB
97 taskq_ent_t *t;
98 int count = 0;
bcd68186 99
472a34ca
BB
100 ASSERT(tq);
101 ASSERT(spin_is_locked(&tq->tq_lock));
bcd68186 102retry:
472a34ca
BB
103 /* Acquire taskq_ent_t's from free list if available */
104 if (!list_empty(&tq->tq_free_list) && !(flags & TQ_NEW)) {
105 t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
106
107 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
d9acd930
BB
108 ASSERT(!(t->tqent_flags & TQENT_FLAG_CANCEL));
109 ASSERT(!timer_pending(&t->tqent_timer));
472a34ca
BB
110
111 list_del_init(&t->tqent_list);
8d9a23e8 112 return (t);
472a34ca
BB
113 }
114
115 /* Free list is empty and memory allocations are prohibited */
116 if (flags & TQ_NOALLOC)
8d9a23e8 117 return (NULL);
472a34ca
BB
118
119 /* Hit maximum taskq_ent_t pool size */
120 if (tq->tq_nalloc >= tq->tq_maxalloc) {
121 if (flags & TQ_NOSLEEP)
8d9a23e8 122 return (NULL);
472a34ca
BB
123
124 /*
125 * Sleep periodically polling the free list for an available
126 * taskq_ent_t. Dispatching with TQ_SLEEP should always succeed
127 * but we cannot block forever waiting for an taskq_ent_t to
128 * show up in the free list, otherwise a deadlock can happen.
129 *
130 * Therefore, we need to allocate a new task even if the number
131 * of allocated tasks is above tq->tq_maxalloc, but we still
132 * end up delaying the task allocation by one second, thereby
133 * throttling the task dispatch rate.
134 */
066b89e6 135 spin_unlock_irqrestore(&tq->tq_lock, *irqflags);
472a34ca 136 schedule_timeout(HZ / 100);
066b89e6 137 spin_lock_irqsave_nested(&tq->tq_lock, *irqflags,
326172d8 138 tq->tq_lock_class);
8d9a23e8
BB
139 if (count < 100) {
140 count++;
141 goto retry;
142 }
472a34ca
BB
143 }
144
066b89e6 145 spin_unlock_irqrestore(&tq->tq_lock, *irqflags);
2c4332cf 146 t = kmem_alloc(sizeof (taskq_ent_t), task_km_flags(flags));
066b89e6 147 spin_lock_irqsave_nested(&tq->tq_lock, *irqflags, tq->tq_lock_class);
472a34ca
BB
148
149 if (t) {
150 taskq_init_ent(t);
151 tq->tq_nalloc++;
152 }
153
8d9a23e8 154 return (t);
bcd68186 155}
156
82387586 157/*
046a70c9 158 * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t
bcd68186 159 * to already be removed from the free, work, or pending taskq lists.
160 */
161static void
046a70c9 162task_free(taskq_t *tq, taskq_ent_t *t)
bcd68186 163{
472a34ca
BB
164 ASSERT(tq);
165 ASSERT(t);
bcd68186 166 ASSERT(spin_is_locked(&tq->tq_lock));
046a70c9 167 ASSERT(list_empty(&t->tqent_list));
d9acd930 168 ASSERT(!timer_pending(&t->tqent_timer));
bcd68186 169
2c4332cf 170 kmem_free(t, sizeof (taskq_ent_t));
472a34ca 171 tq->tq_nalloc--;
bcd68186 172}
173
82387586
BB
174/*
175 * NOTE: Must be called with tq->tq_lock held, either destroys the
046a70c9 176 * taskq_ent_t if too many exist or moves it to the free list for later use.
bcd68186 177 */
f1ca4da6 178static void
046a70c9 179task_done(taskq_t *tq, taskq_ent_t *t)
f1ca4da6 180{
bcd68186 181 ASSERT(tq);
182 ASSERT(t);
183 ASSERT(spin_is_locked(&tq->tq_lock));
184
d9acd930
BB
185 /* Wake tasks blocked in taskq_wait_id() */
186 wake_up_all(&t->tqent_waitq);
187
046a70c9 188 list_del_init(&t->tqent_list);
f1ca4da6 189
472a34ca 190 if (tq->tq_nalloc <= tq->tq_minalloc) {
046a70c9
PS
191 t->tqent_id = 0;
192 t->tqent_func = NULL;
193 t->tqent_arg = NULL;
44217f7a 194 t->tqent_flags = 0;
8f2503e0 195
472a34ca 196 list_add_tail(&t->tqent_list, &tq->tq_free_list);
bcd68186 197 } else {
198 task_free(tq, t);
199 }
f1ca4da6 200}
201
82387586 202/*
d9acd930
BB
203 * When a delayed task timer expires remove it from the delay list and
204 * add it to the priority list in order for immediate processing.
bcd68186 205 */
d9acd930
BB
206static void
207task_expire(unsigned long data)
bcd68186 208{
d9acd930
BB
209 taskq_ent_t *w, *t = (taskq_ent_t *)data;
210 taskq_t *tq = t->tqent_taskq;
211 struct list_head *l;
066b89e6 212 unsigned long flags;
7257ec41 213
066b89e6 214 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
d9acd930
BB
215
216 if (t->tqent_flags & TQENT_FLAG_CANCEL) {
217 ASSERT(list_empty(&t->tqent_list));
066b89e6 218 spin_unlock_irqrestore(&tq->tq_lock, flags);
d9acd930
BB
219 return;
220 }
221
222 /*
223 * The priority list must be maintained in strict task id order
224 * from lowest to highest for lowest_id to be easily calculable.
225 */
226 list_del(&t->tqent_list);
227 list_for_each_prev(l, &tq->tq_prio_list) {
228 w = list_entry(l, taskq_ent_t, tqent_list);
229 if (w->tqent_id < t->tqent_id) {
230 list_add(&t->tqent_list, l);
231 break;
232 }
233 }
234 if (l == &tq->tq_prio_list)
235 list_add(&t->tqent_list, &tq->tq_prio_list);
236
066b89e6 237 spin_unlock_irqrestore(&tq->tq_lock, flags);
7257ec41 238
d9acd930
BB
239 wake_up(&tq->tq_work_waitq);
240}
241
242/*
243 * Returns the lowest incomplete taskqid_t. The taskqid_t may
244 * be queued on the pending list, on the priority list, on the
245 * delay list, or on the work list currently being handled, but
246 * it is not 100% complete yet.
247 */
248static taskqid_t
249taskq_lowest_id(taskq_t *tq)
250{
251 taskqid_t lowest_id = tq->tq_next_id;
252 taskq_ent_t *t;
253 taskq_thread_t *tqt;
d9acd930
BB
254
255 ASSERT(tq);
256 ASSERT(spin_is_locked(&tq->tq_lock));
257
258 if (!list_empty(&tq->tq_pend_list)) {
259 t = list_entry(tq->tq_pend_list.next, taskq_ent_t, tqent_list);
260 lowest_id = MIN(lowest_id, t->tqent_id);
261 }
262
263 if (!list_empty(&tq->tq_prio_list)) {
264 t = list_entry(tq->tq_prio_list.next, taskq_ent_t, tqent_list);
265 lowest_id = MIN(lowest_id, t->tqent_id);
266 }
267
268 if (!list_empty(&tq->tq_delay_list)) {
269 t = list_entry(tq->tq_delay_list.next, taskq_ent_t, tqent_list);
270 lowest_id = MIN(lowest_id, t->tqent_id);
271 }
272
273 if (!list_empty(&tq->tq_active_list)) {
274 tqt = list_entry(tq->tq_active_list.next, taskq_thread_t,
275 tqt_active_list);
276 ASSERT(tqt->tqt_id != 0);
277 lowest_id = MIN(lowest_id, tqt->tqt_id);
278 }
279
8d9a23e8 280 return (lowest_id);
d9acd930
BB
281}
282
283/*
284 * Insert a task into a list keeping the list sorted by increasing taskqid.
285 */
286static void
287taskq_insert_in_order(taskq_t *tq, taskq_thread_t *tqt)
288{
289 taskq_thread_t *w;
290 struct list_head *l;
291
d9acd930
BB
292 ASSERT(tq);
293 ASSERT(tqt);
294 ASSERT(spin_is_locked(&tq->tq_lock));
295
296 list_for_each_prev(l, &tq->tq_active_list) {
297 w = list_entry(l, taskq_thread_t, tqt_active_list);
298 if (w->tqt_id < tqt->tqt_id) {
299 list_add(&tqt->tqt_active_list, l);
300 break;
301 }
302 }
303 if (l == &tq->tq_active_list)
304 list_add(&tqt->tqt_active_list, &tq->tq_active_list);
d9acd930
BB
305}
306
307/*
308 * Find and return a task from the given list if it exists. The list
309 * must be in lowest to highest task id order.
310 */
311static taskq_ent_t *
312taskq_find_list(taskq_t *tq, struct list_head *lh, taskqid_t id)
313{
314 struct list_head *l;
315 taskq_ent_t *t;
d9acd930
BB
316
317 ASSERT(spin_is_locked(&tq->tq_lock));
318
319 list_for_each(l, lh) {
320 t = list_entry(l, taskq_ent_t, tqent_list);
321
322 if (t->tqent_id == id)
8d9a23e8 323 return (t);
d9acd930
BB
324
325 if (t->tqent_id > id)
326 break;
327 }
328
8d9a23e8 329 return (NULL);
bcd68186 330}
331
d9acd930
BB
332/*
333 * Find an already dispatched task given the task id regardless of what
334 * state it is in. If a task is still pending or executing it will be
335 * returned and 'active' set appropriately. If the task has already
336 * been run then NULL is returned.
337 */
338static taskq_ent_t *
339taskq_find(taskq_t *tq, taskqid_t id, int *active)
340{
341 taskq_thread_t *tqt;
342 struct list_head *l;
343 taskq_ent_t *t;
d9acd930
BB
344
345 ASSERT(spin_is_locked(&tq->tq_lock));
346 *active = 0;
347
348 t = taskq_find_list(tq, &tq->tq_delay_list, id);
349 if (t)
8d9a23e8 350 return (t);
d9acd930
BB
351
352 t = taskq_find_list(tq, &tq->tq_prio_list, id);
353 if (t)
8d9a23e8 354 return (t);
d9acd930
BB
355
356 t = taskq_find_list(tq, &tq->tq_pend_list, id);
357 if (t)
8d9a23e8 358 return (t);
d9acd930
BB
359
360 list_for_each(l, &tq->tq_active_list) {
361 tqt = list_entry(l, taskq_thread_t, tqt_active_list);
362 if (tqt->tqt_id == id) {
363 t = tqt->tqt_task;
364 *active = 1;
8d9a23e8 365 return (t);
d9acd930
BB
366 }
367 }
368
8d9a23e8 369 return (NULL);
d9acd930
BB
370}
371
a876b030
CD
372/*
373 * Theory for the taskq_wait_id(), taskq_wait_outstanding(), and
374 * taskq_wait() functions below.
375 *
376 * Taskq waiting is accomplished by tracking the lowest outstanding task
377 * id and the next available task id. As tasks are dispatched they are
378 * added to the tail of the pending, priority, or delay lists. As worker
379 * threads become available the tasks are removed from the heads of these
380 * lists and linked to the worker threads. This ensures the lists are
381 * kept sorted by lowest to highest task id.
382 *
383 * Therefore the lowest outstanding task id can be quickly determined by
384 * checking the head item from all of these lists. This value is stored
385 * with the taskq as the lowest id. It only needs to be recalculated when
386 * either the task with the current lowest id completes or is canceled.
387 *
388 * By blocking until the lowest task id exceeds the passed task id the
389 * taskq_wait_outstanding() function can be easily implemented. Similarly,
390 * by blocking until the lowest task id matches the next task id taskq_wait()
391 * can be implemented.
392 *
393 * Callers should be aware that when there are multiple worked threads it
394 * is possible for larger task ids to complete before smaller ones. Also
395 * when the taskq contains delay tasks with small task ids callers may
396 * block for a considerable length of time waiting for them to expire and
397 * execute.
398 */
99c452bb
BB
399static int
400taskq_wait_id_check(taskq_t *tq, taskqid_t id)
f1ca4da6 401{
d9acd930 402 int active = 0;
99c452bb 403 int rc;
066b89e6 404 unsigned long flags;
bcd68186 405
066b89e6 406 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
99c452bb 407 rc = (taskq_find(tq, id, &active) == NULL);
066b89e6 408 spin_unlock_irqrestore(&tq->tq_lock, flags);
d9acd930 409
99c452bb
BB
410 return (rc);
411}
bcd68186 412
99c452bb
BB
413/*
414 * The taskq_wait_id() function blocks until the passed task id completes.
415 * This does not guarantee that all lower task ids have completed.
416 */
417void
418taskq_wait_id(taskq_t *tq, taskqid_t id)
419{
420 wait_event(tq->tq_wait_waitq, taskq_wait_id_check(tq, id));
bcd68186 421}
aed8671c 422EXPORT_SYMBOL(taskq_wait_id);
bcd68186 423
d9acd930 424static int
a876b030 425taskq_wait_outstanding_check(taskq_t *tq, taskqid_t id)
d9acd930
BB
426{
427 int rc;
066b89e6 428 unsigned long flags;
d9acd930 429
066b89e6 430 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
d9acd930 431 rc = (id < tq->tq_lowest_id);
066b89e6 432 spin_unlock_irqrestore(&tq->tq_lock, flags);
d9acd930 433
8d9a23e8 434 return (rc);
d9acd930
BB
435}
436
a876b030
CD
437/*
438 * The taskq_wait_outstanding() function will block until all tasks with a
439 * lower taskqid than the passed 'id' have been completed. Note that all
440 * task id's are assigned monotonically at dispatch time. Zero may be
441 * passed for the id to indicate all tasks dispatch up to this point,
442 * but not after, should be waited for.
443 */
d9acd930 444void
a876b030 445taskq_wait_outstanding(taskq_t *tq, taskqid_t id)
d9acd930 446{
a876b030
CD
447 wait_event(tq->tq_wait_waitq,
448 taskq_wait_outstanding_check(tq, id ? id : tq->tq_next_id - 1));
d9acd930 449}
a876b030 450EXPORT_SYMBOL(taskq_wait_outstanding);
d9acd930 451
a876b030
CD
452static int
453taskq_wait_check(taskq_t *tq)
bcd68186 454{
a876b030 455 int rc;
066b89e6 456 unsigned long flags;
bcd68186 457
066b89e6 458 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
a876b030 459 rc = (tq->tq_lowest_id == tq->tq_next_id);
066b89e6 460 spin_unlock_irqrestore(&tq->tq_lock, flags);
bcd68186 461
a876b030
CD
462 return (rc);
463}
464
465/*
466 * The taskq_wait() function will block until the taskq is empty.
467 * This means that if a taskq re-dispatches work to itself taskq_wait()
468 * callers will block indefinitely.
469 */
470void
471taskq_wait(taskq_t *tq)
472{
473 wait_event(tq->tq_wait_waitq, taskq_wait_check(tq));
bcd68186 474}
aed8671c 475EXPORT_SYMBOL(taskq_wait);
bcd68186 476
c5a8b1e1
BB
477static int
478taskq_member_impl(taskq_t *tq, void *t)
479{
480 struct list_head *l;
481 taskq_thread_t *tqt;
482 int found = 0;
483
484 ASSERT(tq);
485 ASSERT(t);
486 ASSERT(spin_is_locked(&tq->tq_lock));
487
488 list_for_each(l, &tq->tq_thread_list) {
489 tqt = list_entry(l, taskq_thread_t, tqt_thread_list);
490 if (tqt->tqt_thread == (struct task_struct *)t) {
491 found = 1;
492 break;
493 }
494 }
495 return (found);
496}
497
498int
499taskq_member(taskq_t *tq, void *t)
500{
501 int found;
066b89e6 502 unsigned long flags;
c5a8b1e1 503
066b89e6 504 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
c5a8b1e1 505 found = taskq_member_impl(tq, t);
066b89e6 506 spin_unlock_irqrestore(&tq->tq_lock, flags);
c5a8b1e1
BB
507
508 return (found);
509}
510EXPORT_SYMBOL(taskq_member);
511
d9acd930
BB
512/*
513 * Cancel an already dispatched task given the task id. Still pending tasks
514 * will be immediately canceled, and if the task is active the function will
515 * block until it completes. Preallocated tasks which are canceled must be
516 * freed by the caller.
517 */
518int
519taskq_cancel_id(taskq_t *tq, taskqid_t id)
520{
521 taskq_ent_t *t;
522 int active = 0;
523 int rc = ENOENT;
066b89e6 524 unsigned long flags;
d9acd930
BB
525
526 ASSERT(tq);
527
066b89e6 528 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
d9acd930
BB
529 t = taskq_find(tq, id, &active);
530 if (t && !active) {
531 list_del_init(&t->tqent_list);
532 t->tqent_flags |= TQENT_FLAG_CANCEL;
533
534 /*
535 * When canceling the lowest outstanding task id we
536 * must recalculate the new lowest outstanding id.
537 */
538 if (tq->tq_lowest_id == t->tqent_id) {
539 tq->tq_lowest_id = taskq_lowest_id(tq);
540 ASSERT3S(tq->tq_lowest_id, >, t->tqent_id);
541 }
542
543 /*
544 * The task_expire() function takes the tq->tq_lock so drop
545 * drop the lock before synchronously cancelling the timer.
546 */
547 if (timer_pending(&t->tqent_timer)) {
066b89e6 548 spin_unlock_irqrestore(&tq->tq_lock, flags);
d9acd930 549 del_timer_sync(&t->tqent_timer);
066b89e6
CC
550 spin_lock_irqsave_nested(&tq->tq_lock, flags,
551 tq->tq_lock_class);
d9acd930
BB
552 }
553
554 if (!(t->tqent_flags & TQENT_FLAG_PREALLOC))
555 task_done(tq, t);
556
557 rc = 0;
558 }
066b89e6 559 spin_unlock_irqrestore(&tq->tq_lock, flags);
d9acd930
BB
560
561 if (active) {
562 taskq_wait_id(tq, id);
563 rc = EBUSY;
564 }
565
8d9a23e8 566 return (rc);
d9acd930
BB
567}
568EXPORT_SYMBOL(taskq_cancel_id);
569
f5f2b87d 570static int taskq_thread_spawn(taskq_t *tq);
a64e5575 571
bcd68186 572taskqid_t
aed8671c 573taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
bcd68186 574{
472a34ca 575 taskq_ent_t *t;
bcd68186 576 taskqid_t rc = 0;
066b89e6 577 unsigned long irqflags;
f1ca4da6 578
472a34ca
BB
579 ASSERT(tq);
580 ASSERT(func);
d05ec4b4 581
066b89e6 582 spin_lock_irqsave_nested(&tq->tq_lock, irqflags, tq->tq_lock_class);
f1ca4da6 583
bcd68186 584 /* Taskq being destroyed and all tasks drained */
f7a973d9 585 if (!(tq->tq_flags & TASKQ_ACTIVE))
8d9a23e8 586 goto out;
f1ca4da6 587
bcd68186 588 /* Do not queue the task unless there is idle thread for it */
589 ASSERT(tq->tq_nactive <= tq->tq_nthreads);
590 if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads))
8d9a23e8 591 goto out;
bcd68186 592
066b89e6 593 if ((t = task_alloc(tq, flags, &irqflags)) == NULL)
8d9a23e8 594 goto out;
f1ca4da6 595
046a70c9 596 spin_lock(&t->tqent_lock);
f0d8bb26
NB
597
598 /* Queue to the priority list instead of the pending list */
599 if (flags & TQ_FRONT)
046a70c9 600 list_add_tail(&t->tqent_list, &tq->tq_prio_list);
f0d8bb26 601 else
046a70c9 602 list_add_tail(&t->tqent_list, &tq->tq_pend_list);
f0d8bb26 603
046a70c9 604 t->tqent_id = rc = tq->tq_next_id;
bcd68186 605 tq->tq_next_id++;
472a34ca
BB
606 t->tqent_func = func;
607 t->tqent_arg = arg;
d9acd930
BB
608 t->tqent_taskq = tq;
609 t->tqent_timer.data = 0;
610 t->tqent_timer.function = NULL;
611 t->tqent_timer.expires = 0;
44217f7a
PS
612
613 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
614
046a70c9 615 spin_unlock(&t->tqent_lock);
0bb43ca2
NB
616
617 wake_up(&tq->tq_work_waitq);
bcd68186 618out:
a64e5575 619 /* Spawn additional taskq threads if required. */
f5f2b87d 620 if (tq->tq_nactive == tq->tq_nthreads)
621 (void) taskq_thread_spawn(tq);
a64e5575 622
066b89e6 623 spin_unlock_irqrestore(&tq->tq_lock, irqflags);
8d9a23e8 624 return (rc);
f1ca4da6 625}
aed8671c 626EXPORT_SYMBOL(taskq_dispatch);
44217f7a 627
d9acd930
BB
628taskqid_t
629taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg,
630 uint_t flags, clock_t expire_time)
631{
d9acd930 632 taskqid_t rc = 0;
8d9a23e8 633 taskq_ent_t *t;
066b89e6 634 unsigned long irqflags;
d9acd930
BB
635
636 ASSERT(tq);
637 ASSERT(func);
638
066b89e6 639 spin_lock_irqsave_nested(&tq->tq_lock, irqflags, tq->tq_lock_class);
d9acd930
BB
640
641 /* Taskq being destroyed and all tasks drained */
f7a973d9 642 if (!(tq->tq_flags & TASKQ_ACTIVE))
8d9a23e8 643 goto out;
d9acd930 644
066b89e6 645 if ((t = task_alloc(tq, flags, &irqflags)) == NULL)
8d9a23e8 646 goto out;
d9acd930
BB
647
648 spin_lock(&t->tqent_lock);
649
650 /* Queue to the delay list for subsequent execution */
651 list_add_tail(&t->tqent_list, &tq->tq_delay_list);
652
653 t->tqent_id = rc = tq->tq_next_id;
654 tq->tq_next_id++;
655 t->tqent_func = func;
656 t->tqent_arg = arg;
657 t->tqent_taskq = tq;
658 t->tqent_timer.data = (unsigned long)t;
659 t->tqent_timer.function = task_expire;
660 t->tqent_timer.expires = (unsigned long)expire_time;
661 add_timer(&t->tqent_timer);
662
663 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
664
665 spin_unlock(&t->tqent_lock);
666out:
a64e5575 667 /* Spawn additional taskq threads if required. */
f5f2b87d 668 if (tq->tq_nactive == tq->tq_nthreads)
669 (void) taskq_thread_spawn(tq);
066b89e6 670 spin_unlock_irqrestore(&tq->tq_lock, irqflags);
8d9a23e8 671 return (rc);
d9acd930
BB
672}
673EXPORT_SYMBOL(taskq_dispatch_delay);
674
44217f7a 675void
aed8671c 676taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
2c4332cf 677 taskq_ent_t *t)
44217f7a 678{
066b89e6 679 unsigned long irqflags;
44217f7a
PS
680 ASSERT(tq);
681 ASSERT(func);
44217f7a 682
066b89e6 683 spin_lock_irqsave_nested(&tq->tq_lock, irqflags,
326172d8 684 tq->tq_lock_class);
44217f7a
PS
685
686 /* Taskq being destroyed and all tasks drained */
f7a973d9 687 if (!(tq->tq_flags & TASKQ_ACTIVE)) {
44217f7a
PS
688 t->tqent_id = 0;
689 goto out;
690 }
691
692 spin_lock(&t->tqent_lock);
693
694 /*
695 * Mark it as a prealloc'd task. This is important
696 * to ensure that we don't free it later.
697 */
698 t->tqent_flags |= TQENT_FLAG_PREALLOC;
699
700 /* Queue to the priority list instead of the pending list */
701 if (flags & TQ_FRONT)
702 list_add_tail(&t->tqent_list, &tq->tq_prio_list);
703 else
704 list_add_tail(&t->tqent_list, &tq->tq_pend_list);
705
706 t->tqent_id = tq->tq_next_id;
707 tq->tq_next_id++;
708 t->tqent_func = func;
709 t->tqent_arg = arg;
d9acd930 710 t->tqent_taskq = tq;
44217f7a
PS
711
712 spin_unlock(&t->tqent_lock);
713
714 wake_up(&tq->tq_work_waitq);
715out:
a64e5575 716 /* Spawn additional taskq threads if required. */
f5f2b87d 717 if (tq->tq_nactive == tq->tq_nthreads)
718 (void) taskq_thread_spawn(tq);
066b89e6 719 spin_unlock_irqrestore(&tq->tq_lock, irqflags);
44217f7a 720}
aed8671c 721EXPORT_SYMBOL(taskq_dispatch_ent);
44217f7a
PS
722
723int
aed8671c 724taskq_empty_ent(taskq_ent_t *t)
44217f7a 725{
2c4332cf 726 return (list_empty(&t->tqent_list));
44217f7a 727}
aed8671c 728EXPORT_SYMBOL(taskq_empty_ent);
44217f7a
PS
729
730void
aed8671c 731taskq_init_ent(taskq_ent_t *t)
44217f7a
PS
732{
733 spin_lock_init(&t->tqent_lock);
d9acd930
BB
734 init_waitqueue_head(&t->tqent_waitq);
735 init_timer(&t->tqent_timer);
44217f7a
PS
736 INIT_LIST_HEAD(&t->tqent_list);
737 t->tqent_id = 0;
738 t->tqent_func = NULL;
739 t->tqent_arg = NULL;
740 t->tqent_flags = 0;
d9acd930 741 t->tqent_taskq = NULL;
44217f7a 742}
aed8671c 743EXPORT_SYMBOL(taskq_init_ent);
44217f7a 744
f7a973d9
BB
745/*
746 * Return the next pending task, preference is given to tasks on the
747 * priority list which were dispatched with TQ_FRONT.
748 */
749static taskq_ent_t *
750taskq_next_ent(taskq_t *tq)
751{
752 struct list_head *list;
753
754 ASSERT(spin_is_locked(&tq->tq_lock));
755
756 if (!list_empty(&tq->tq_prio_list))
757 list = &tq->tq_prio_list;
758 else if (!list_empty(&tq->tq_pend_list))
759 list = &tq->tq_pend_list;
760 else
761 return (NULL);
762
763 return (list_entry(list->next, taskq_ent_t, tqent_list));
764}
765
766/*
767 * Spawns a new thread for the specified taskq.
768 */
769static void
770taskq_thread_spawn_task(void *arg)
771{
772 taskq_t *tq = (taskq_t *)arg;
066b89e6 773 unsigned long flags;
f7a973d9
BB
774
775 (void) taskq_thread_create(tq);
776
066b89e6 777 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
f7a973d9 778 tq->tq_nspawn--;
066b89e6 779 spin_unlock_irqrestore(&tq->tq_lock, flags);
f7a973d9
BB
780}
781
782/*
326172d8 783 * Spawn addition threads for dynamic taskqs (TASKQ_DYNAMIC) the current
f7a973d9
BB
784 * number of threads is insufficient to handle the pending tasks. These
785 * new threads must be created by the dedicated dynamic_taskq to avoid
786 * deadlocks between thread creation and memory reclaim. The system_taskq
787 * which is also a dynamic taskq cannot be safely used for this.
788 */
789static int
f5f2b87d 790taskq_thread_spawn(taskq_t *tq)
f7a973d9
BB
791{
792 int spawning = 0;
793
794 if (!(tq->tq_flags & TASKQ_DYNAMIC))
795 return (0);
796
f5f2b87d 797 if ((tq->tq_nthreads + tq->tq_nspawn < tq->tq_maxthreads) &&
f7a973d9
BB
798 (tq->tq_flags & TASKQ_ACTIVE)) {
799 spawning = (++tq->tq_nspawn);
800 taskq_dispatch(dynamic_taskq, taskq_thread_spawn_task,
801 tq, TQ_NOSLEEP);
802 }
803
804 return (spawning);
805}
806
807/*
808 * Threads in a dynamic taskq should only exit once it has been completely
809 * drained and no other threads are actively servicing tasks. This prevents
810 * threads from being created and destroyed more than is required.
811 *
812 * The first thread is the thread list is treated as the primary thread.
813 * There is nothing special about the primary thread but in order to avoid
814 * all the taskq pids from changing we opt to make it long running.
815 */
816static int
817taskq_thread_should_stop(taskq_t *tq, taskq_thread_t *tqt)
818{
819 ASSERT(spin_is_locked(&tq->tq_lock));
820
821 if (!(tq->tq_flags & TASKQ_DYNAMIC))
822 return (0);
823
824 if (list_first_entry(&(tq->tq_thread_list), taskq_thread_t,
825 tqt_thread_list) == tqt)
826 return (0);
827
828 return
829 ((tq->tq_nspawn == 0) && /* No threads are being spawned */
830 (tq->tq_nactive == 0) && /* No threads are handling tasks */
831 (tq->tq_nthreads > 1) && /* More than 1 thread is running */
832 (!taskq_next_ent(tq)) && /* There are no pending tasks */
2c4332cf 833 (spl_taskq_thread_dynamic)); /* Dynamic taskqs are allowed */
f7a973d9
BB
834}
835
bcd68186 836static int
837taskq_thread(void *args)
838{
472a34ca
BB
839 DECLARE_WAITQUEUE(wait, current);
840 sigset_t blocked;
2c02b71b 841 taskq_thread_t *tqt = args;
472a34ca
BB
842 taskq_t *tq;
843 taskq_ent_t *t;
f7a973d9 844 int seq_tasks = 0;
066b89e6 845 unsigned long flags;
bcd68186 846
472a34ca 847 ASSERT(tqt);
326172d8 848 ASSERT(tqt->tqt_tq);
2c02b71b 849 tq = tqt->tqt_tq;
472a34ca 850 current->flags |= PF_NOFREEZE;
bcd68186 851
2c4332cf 852#if defined(PF_MEMALLOC_NOIO)
d4bf6d84 853 (void) memalloc_noio_save();
2c4332cf 854#endif
d4bf6d84 855
472a34ca
BB
856 sigfillset(&blocked);
857 sigprocmask(SIG_BLOCK, &blocked, NULL);
858 flush_signals(current);
bcd68186 859
066b89e6 860 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
f7a973d9
BB
861
862 /* Immediately exit if more threads than allowed were created. */
863 if (tq->tq_nthreads >= tq->tq_maxthreads)
864 goto error;
865
472a34ca 866 tq->tq_nthreads++;
f7a973d9 867 list_add_tail(&tqt->tqt_thread_list, &tq->tq_thread_list);
472a34ca
BB
868 wake_up(&tq->tq_wait_waitq);
869 set_current_state(TASK_INTERRUPTIBLE);
bcd68186 870
472a34ca 871 while (!kthread_should_stop()) {
bcd68186 872
f0d8bb26
NB
873 if (list_empty(&tq->tq_pend_list) &&
874 list_empty(&tq->tq_prio_list)) {
f7a973d9
BB
875
876 if (taskq_thread_should_stop(tq, tqt)) {
877 wake_up_all(&tq->tq_wait_waitq);
878 break;
879 }
880
3c6ed541 881 add_wait_queue_exclusive(&tq->tq_work_waitq, &wait);
066b89e6 882 spin_unlock_irqrestore(&tq->tq_lock, flags);
f7a973d9 883
bcd68186 884 schedule();
f7a973d9
BB
885 seq_tasks = 0;
886
066b89e6
CC
887 spin_lock_irqsave_nested(&tq->tq_lock, flags,
888 tq->tq_lock_class);
3c6ed541 889 remove_wait_queue(&tq->tq_work_waitq, &wait);
bcd68186 890 } else {
891 __set_current_state(TASK_RUNNING);
892 }
893
f7a973d9 894 if ((t = taskq_next_ent(tq)) != NULL) {
472a34ca 895 list_del_init(&t->tqent_list);
8f2503e0 896
2c4332cf
BB
897 /*
898 * In order to support recursively dispatching a
44217f7a 899 * preallocated taskq_ent_t, tqent_id must be
2c4332cf
BB
900 * stored prior to executing tqent_func.
901 */
e7e5f78e 902 tqt->tqt_id = t->tqent_id;
d9acd930 903 tqt->tqt_task = t;
8f2503e0 904
2c4332cf
BB
905 /*
906 * We must store a copy of the flags prior to
8f2503e0
PS
907 * servicing the task (servicing a prealloc'd task
908 * returns the ownership of the tqent back to
909 * the caller of taskq_dispatch). Thus,
2c4332cf
BB
910 * tqent_flags _may_ change within the call.
911 */
8f2503e0
PS
912 tqt->tqt_flags = t->tqent_flags;
913
2c02b71b 914 taskq_insert_in_order(tq, tqt);
472a34ca 915 tq->tq_nactive++;
066b89e6 916 spin_unlock_irqrestore(&tq->tq_lock, flags);
bcd68186 917
918 /* Perform the requested task */
472a34ca 919 t->tqent_func(t->tqent_arg);
bcd68186 920
066b89e6
CC
921 spin_lock_irqsave_nested(&tq->tq_lock, flags,
922 tq->tq_lock_class);
472a34ca 923 tq->tq_nactive--;
2c02b71b 924 list_del_init(&tqt->tqt_active_list);
d9acd930 925 tqt->tqt_task = NULL;
8f2503e0
PS
926
927 /* For prealloc'd tasks, we don't free anything. */
f7a973d9 928 if (!(tqt->tqt_flags & TQENT_FLAG_PREALLOC))
8f2503e0 929 task_done(tq, t);
bcd68186 930
2c4332cf
BB
931 /*
932 * When the current lowest outstanding taskqid is
933 * done calculate the new lowest outstanding id
934 */
e7e5f78e 935 if (tq->tq_lowest_id == tqt->tqt_id) {
bcd68186 936 tq->tq_lowest_id = taskq_lowest_id(tq);
e7e5f78e 937 ASSERT3S(tq->tq_lowest_id, >, tqt->tqt_id);
bcd68186 938 }
939
f7a973d9 940 /* Spawn additional taskq threads if required. */
f5f2b87d 941 if ((++seq_tasks) > spl_taskq_thread_sequential &&
942 taskq_thread_spawn(tq))
f7a973d9
BB
943 seq_tasks = 0;
944
e7e5f78e 945 tqt->tqt_id = 0;
8f2503e0 946 tqt->tqt_flags = 0;
472a34ca 947 wake_up_all(&tq->tq_wait_waitq);
f7a973d9
BB
948 } else {
949 if (taskq_thread_should_stop(tq, tqt))
950 break;
bcd68186 951 }
952
953 set_current_state(TASK_INTERRUPTIBLE);
954
472a34ca 955 }
bcd68186 956
957 __set_current_state(TASK_RUNNING);
472a34ca 958 tq->tq_nthreads--;
2c02b71b 959 list_del_init(&tqt->tqt_thread_list);
f7a973d9
BB
960error:
961 kmem_free(tqt, sizeof (taskq_thread_t));
066b89e6 962 spin_unlock_irqrestore(&tq->tq_lock, flags);
bcd68186 963
8d9a23e8 964 return (0);
bcd68186 965}
966
f7a973d9
BB
967static taskq_thread_t *
968taskq_thread_create(taskq_t *tq)
969{
970 static int last_used_cpu = 0;
971 taskq_thread_t *tqt;
972
973 tqt = kmem_alloc(sizeof (*tqt), KM_PUSHPAGE);
974 INIT_LIST_HEAD(&tqt->tqt_thread_list);
975 INIT_LIST_HEAD(&tqt->tqt_active_list);
976 tqt->tqt_tq = tq;
977 tqt->tqt_id = 0;
978
979 tqt->tqt_thread = spl_kthread_create(taskq_thread, tqt,
980 "%s", tq->tq_name);
981 if (tqt->tqt_thread == NULL) {
982 kmem_free(tqt, sizeof (taskq_thread_t));
983 return (NULL);
984 }
985
986 if (spl_taskq_thread_bind) {
987 last_used_cpu = (last_used_cpu + 1) % num_online_cpus();
988 kthread_bind(tqt->tqt_thread, last_used_cpu);
989 }
990
62aa81a5
BB
991 if (spl_taskq_thread_priority)
992 set_user_nice(tqt->tqt_thread, PRIO_TO_NICE(tq->tq_pri));
993
f7a973d9
BB
994 wake_up_process(tqt->tqt_thread);
995
996 return (tqt);
997}
998
f1ca4da6 999taskq_t *
aed8671c 1000taskq_create(const char *name, int nthreads, pri_t pri,
472a34ca 1001 int minalloc, int maxalloc, uint_t flags)
f1ca4da6 1002{
472a34ca 1003 taskq_t *tq;
2c02b71b 1004 taskq_thread_t *tqt;
f7a973d9 1005 int count = 0, rc = 0, i;
066b89e6 1006 unsigned long irqflags;
bcd68186 1007
472a34ca 1008 ASSERT(name != NULL);
472a34ca
BB
1009 ASSERT(minalloc >= 0);
1010 ASSERT(maxalloc <= INT_MAX);
f7a973d9 1011 ASSERT(!(flags & (TASKQ_CPR_SAFE))); /* Unsupported */
bcd68186 1012
915404bd
BB
1013 /* Scale the number of threads using nthreads as a percentage */
1014 if (flags & TASKQ_THREADS_CPU_PCT) {
1015 ASSERT(nthreads <= 100);
1016 ASSERT(nthreads >= 0);
1017 nthreads = MIN(nthreads, 100);
1018 nthreads = MAX(nthreads, 0);
1019 nthreads = MAX((num_online_cpus() * nthreads) / 100, 1);
1020 }
1021
f7a973d9 1022 tq = kmem_alloc(sizeof (*tq), KM_PUSHPAGE);
472a34ca 1023 if (tq == NULL)
8d9a23e8 1024 return (NULL);
bcd68186 1025
472a34ca 1026 spin_lock_init(&tq->tq_lock);
472a34ca
BB
1027 INIT_LIST_HEAD(&tq->tq_thread_list);
1028 INIT_LIST_HEAD(&tq->tq_active_list);
2c4332cf
BB
1029 tq->tq_name = strdup(name);
1030 tq->tq_nactive = 0;
1031 tq->tq_nthreads = 0;
1032 tq->tq_nspawn = 0;
f7a973d9 1033 tq->tq_maxthreads = nthreads;
2c4332cf
BB
1034 tq->tq_pri = pri;
1035 tq->tq_minalloc = minalloc;
1036 tq->tq_maxalloc = maxalloc;
1037 tq->tq_nalloc = 0;
1038 tq->tq_flags = (flags | TASKQ_ACTIVE);
1039 tq->tq_next_id = 1;
1040 tq->tq_lowest_id = 1;
472a34ca
BB
1041 INIT_LIST_HEAD(&tq->tq_free_list);
1042 INIT_LIST_HEAD(&tq->tq_pend_list);
1043 INIT_LIST_HEAD(&tq->tq_prio_list);
d9acd930 1044 INIT_LIST_HEAD(&tq->tq_delay_list);
472a34ca
BB
1045 init_waitqueue_head(&tq->tq_work_waitq);
1046 init_waitqueue_head(&tq->tq_wait_waitq);
326172d8 1047 tq->tq_lock_class = TQ_LOCK_GENERAL;
200366f2 1048 INIT_LIST_HEAD(&tq->tq_taskqs);
bcd68186 1049
f7a973d9 1050 if (flags & TASKQ_PREPOPULATE) {
066b89e6 1051 spin_lock_irqsave_nested(&tq->tq_lock, irqflags,
326172d8 1052 tq->tq_lock_class);
f7a973d9 1053
472a34ca 1054 for (i = 0; i < minalloc; i++)
066b89e6
CC
1055 task_done(tq, task_alloc(tq, TQ_PUSHPAGE | TQ_NEW,
1056 &irqflags));
6e605b6e 1057
066b89e6 1058 spin_unlock_irqrestore(&tq->tq_lock, irqflags);
f7a973d9
BB
1059 }
1060
1061 if ((flags & TASKQ_DYNAMIC) && spl_taskq_thread_dynamic)
1062 nthreads = 1;
6e605b6e 1063
2c02b71b 1064 for (i = 0; i < nthreads; i++) {
f7a973d9
BB
1065 tqt = taskq_thread_create(tq);
1066 if (tqt == NULL)
2c02b71b 1067 rc = 1;
f7a973d9
BB
1068 else
1069 count++;
2c02b71b 1070 }
bcd68186 1071
472a34ca 1072 /* Wait for all threads to be started before potential destroy */
f7a973d9 1073 wait_event(tq->tq_wait_waitq, tq->tq_nthreads == count);
bcd68186 1074
472a34ca 1075 if (rc) {
aed8671c 1076 taskq_destroy(tq);
472a34ca 1077 tq = NULL;
200366f2
TC
1078 } else {
1079 down_write(&tq_list_sem);
1080 tq->tq_instance = taskq_find_by_name(name) + 1;
1081 list_add_tail(&tq->tq_taskqs, &tq_list);
1082 up_write(&tq_list_sem);
472a34ca 1083 }
bcd68186 1084
8d9a23e8 1085 return (tq);
f1ca4da6 1086}
aed8671c 1087EXPORT_SYMBOL(taskq_create);
b123971f 1088
1089void
aed8671c 1090taskq_destroy(taskq_t *tq)
b123971f 1091{
2c02b71b
PS
1092 struct task_struct *thread;
1093 taskq_thread_t *tqt;
046a70c9 1094 taskq_ent_t *t;
066b89e6 1095 unsigned long flags;
b123971f 1096
bcd68186 1097 ASSERT(tq);
066b89e6 1098 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
f7a973d9 1099 tq->tq_flags &= ~TASKQ_ACTIVE;
066b89e6 1100 spin_unlock_irqrestore(&tq->tq_lock, flags);
bcd68186 1101
f7a973d9
BB
1102 /*
1103 * When TASKQ_ACTIVE is clear new tasks may not be added nor may
1104 * new worker threads be spawned for dynamic taskq.
1105 */
1106 if (dynamic_taskq != NULL)
1107 taskq_wait_outstanding(dynamic_taskq, 0);
1108
aed8671c 1109 taskq_wait(tq);
bcd68186 1110
200366f2
TC
1111 /* remove taskq from global list used by the kstats */
1112 down_write(&tq_list_sem);
1113 list_del(&tq->tq_taskqs);
1114 up_write(&tq_list_sem);
1115
066b89e6 1116 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
bcd68186 1117
2c02b71b
PS
1118 /*
1119 * Signal each thread to exit and block until it does. Each thread
1120 * is responsible for removing itself from the list and freeing its
1121 * taskq_thread_t. This allows for idle threads to opt to remove
1122 * themselves from the taskq. They can be recreated as needed.
1123 */
1124 while (!list_empty(&tq->tq_thread_list)) {
1125 tqt = list_entry(tq->tq_thread_list.next,
f7a973d9 1126 taskq_thread_t, tqt_thread_list);
2c02b71b 1127 thread = tqt->tqt_thread;
066b89e6 1128 spin_unlock_irqrestore(&tq->tq_lock, flags);
2c02b71b
PS
1129
1130 kthread_stop(thread);
1131
066b89e6 1132 spin_lock_irqsave_nested(&tq->tq_lock, flags,
326172d8 1133 tq->tq_lock_class);
2c02b71b
PS
1134 }
1135
472a34ca 1136 while (!list_empty(&tq->tq_free_list)) {
046a70c9 1137 t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
44217f7a
PS
1138
1139 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
1140
472a34ca
BB
1141 list_del_init(&t->tqent_list);
1142 task_free(tq, t);
1143 }
bcd68186 1144
f7a973d9
BB
1145 ASSERT0(tq->tq_nthreads);
1146 ASSERT0(tq->tq_nalloc);
1147 ASSERT0(tq->tq_nspawn);
472a34ca
BB
1148 ASSERT(list_empty(&tq->tq_thread_list));
1149 ASSERT(list_empty(&tq->tq_active_list));
1150 ASSERT(list_empty(&tq->tq_free_list));
1151 ASSERT(list_empty(&tq->tq_pend_list));
1152 ASSERT(list_empty(&tq->tq_prio_list));
d9acd930 1153 ASSERT(list_empty(&tq->tq_delay_list));
bcd68186 1154
066b89e6 1155 spin_unlock_irqrestore(&tq->tq_lock, flags);
2c02b71b 1156
f7a973d9
BB
1157 strfree(tq->tq_name);
1158 kmem_free(tq, sizeof (taskq_t));
b123971f 1159}
aed8671c 1160EXPORT_SYMBOL(taskq_destroy);
e9cb2b4f
BB
1161
1162int
1163spl_taskq_init(void)
1164{
3c82160f 1165 system_taskq = taskq_create("spl_system_taskq", MAX(boot_ncpus, 64),
9dc5ffbe 1166 maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE|TASKQ_DYNAMIC);
e9cb2b4f 1167 if (system_taskq == NULL)
8d9a23e8 1168 return (1);
e9cb2b4f 1169
f7a973d9 1170 dynamic_taskq = taskq_create("spl_dynamic_taskq", 1,
9dc5ffbe 1171 maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE);
f7a973d9
BB
1172 if (dynamic_taskq == NULL) {
1173 taskq_destroy(system_taskq);
1174 return (1);
1175 }
1176
2c4332cf
BB
1177 /*
1178 * This is used to annotate tq_lock, so
1179 * taskq_dispatch -> taskq_thread_spawn -> taskq_dispatch
326172d8
OF
1180 * does not trigger a lockdep warning re: possible recursive locking
1181 */
1182 dynamic_taskq->tq_lock_class = TQ_LOCK_DYNAMIC;
1183
8d9a23e8 1184 return (0);
e9cb2b4f
BB
1185}
1186
1187void
1188spl_taskq_fini(void)
1189{
f7a973d9
BB
1190 taskq_destroy(dynamic_taskq);
1191 dynamic_taskq = NULL;
1192
e9cb2b4f 1193 taskq_destroy(system_taskq);
f7a973d9 1194 system_taskq = NULL;
e9cb2b4f 1195}