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