]> git.proxmox.com Git - mirror_spl.git/blame - module/spl/spl-taskq.c
Refresh links to web site
[mirror_spl.git] / module / spl / spl-taskq.c
CommitLineData
716154c5
BB
1/*****************************************************************************\
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
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Task Queue Implementation.
25\*****************************************************************************/
715f6251 26
f4b37741 27#include <sys/taskq.h>
3d061e9d 28#include <sys/kmem.h>
55abb092 29#include <spl-debug.h>
f1ca4da6 30
b17edc10
BB
31#ifdef SS_DEBUG_SUBSYS
32#undef SS_DEBUG_SUBSYS
937879f1 33#endif
34
b17edc10 35#define SS_DEBUG_SUBSYS SS_TASKQ
937879f1 36
e9cb2b4f
BB
37/* Global system-wide dynamic task queue available for all consumers */
38taskq_t *system_taskq;
39EXPORT_SYMBOL(system_taskq);
40
9b51f218
BB
41static int
42task_km_flags(uint_t flags)
43{
44 if (flags & TQ_NOSLEEP)
45 return KM_NOSLEEP;
46
47 if (flags & TQ_PUSHPAGE)
48 return KM_PUSHPAGE;
49
50 return KM_SLEEP;
51}
52
82387586
BB
53/*
54 * NOTE: Must be called with tq->tq_lock held, returns a list_t which
bcd68186 55 * is not attached to the free, work, or pending taskq lists.
f1ca4da6 56 */
046a70c9 57static taskq_ent_t *
bcd68186 58task_alloc(taskq_t *tq, uint_t flags)
59{
472a34ca
BB
60 taskq_ent_t *t;
61 int count = 0;
62 SENTRY;
bcd68186 63
472a34ca
BB
64 ASSERT(tq);
65 ASSERT(spin_is_locked(&tq->tq_lock));
bcd68186 66retry:
472a34ca
BB
67 /* Acquire taskq_ent_t's from free list if available */
68 if (!list_empty(&tq->tq_free_list) && !(flags & TQ_NEW)) {
69 t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
70
71 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
d9acd930
BB
72 ASSERT(!(t->tqent_flags & TQENT_FLAG_CANCEL));
73 ASSERT(!timer_pending(&t->tqent_timer));
472a34ca
BB
74
75 list_del_init(&t->tqent_list);
76 SRETURN(t);
77 }
78
79 /* Free list is empty and memory allocations are prohibited */
80 if (flags & TQ_NOALLOC)
81 SRETURN(NULL);
82
83 /* Hit maximum taskq_ent_t pool size */
84 if (tq->tq_nalloc >= tq->tq_maxalloc) {
85 if (flags & TQ_NOSLEEP)
86 SRETURN(NULL);
87
88 /*
89 * Sleep periodically polling the free list for an available
90 * taskq_ent_t. Dispatching with TQ_SLEEP should always succeed
91 * but we cannot block forever waiting for an taskq_ent_t to
92 * show up in the free list, otherwise a deadlock can happen.
93 *
94 * Therefore, we need to allocate a new task even if the number
95 * of allocated tasks is above tq->tq_maxalloc, but we still
96 * end up delaying the task allocation by one second, thereby
97 * throttling the task dispatch rate.
98 */
99 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
100 schedule_timeout(HZ / 100);
101 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
102 if (count < 100)
103 SGOTO(retry, count++);
104 }
105
106 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
107 t = kmem_alloc(sizeof(taskq_ent_t), task_km_flags(flags));
108 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
109
110 if (t) {
111 taskq_init_ent(t);
112 tq->tq_nalloc++;
113 }
114
115 SRETURN(t);
bcd68186 116}
117
82387586 118/*
046a70c9 119 * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t
bcd68186 120 * to already be removed from the free, work, or pending taskq lists.
121 */
122static void
046a70c9 123task_free(taskq_t *tq, taskq_ent_t *t)
bcd68186 124{
472a34ca 125 SENTRY;
bcd68186 126
472a34ca
BB
127 ASSERT(tq);
128 ASSERT(t);
bcd68186 129 ASSERT(spin_is_locked(&tq->tq_lock));
046a70c9 130 ASSERT(list_empty(&t->tqent_list));
d9acd930 131 ASSERT(!timer_pending(&t->tqent_timer));
bcd68186 132
472a34ca
BB
133 kmem_free(t, sizeof(taskq_ent_t));
134 tq->tq_nalloc--;
f1ca4da6 135
b17edc10 136 SEXIT;
bcd68186 137}
138
82387586
BB
139/*
140 * NOTE: Must be called with tq->tq_lock held, either destroys the
046a70c9 141 * taskq_ent_t if too many exist or moves it to the free list for later use.
bcd68186 142 */
f1ca4da6 143static void
046a70c9 144task_done(taskq_t *tq, taskq_ent_t *t)
f1ca4da6 145{
b17edc10 146 SENTRY;
bcd68186 147 ASSERT(tq);
148 ASSERT(t);
149 ASSERT(spin_is_locked(&tq->tq_lock));
150
d9acd930
BB
151 /* Wake tasks blocked in taskq_wait_id() */
152 wake_up_all(&t->tqent_waitq);
153
046a70c9 154 list_del_init(&t->tqent_list);
f1ca4da6 155
472a34ca 156 if (tq->tq_nalloc <= tq->tq_minalloc) {
046a70c9
PS
157 t->tqent_id = 0;
158 t->tqent_func = NULL;
159 t->tqent_arg = NULL;
44217f7a 160 t->tqent_flags = 0;
8f2503e0 161
472a34ca 162 list_add_tail(&t->tqent_list, &tq->tq_free_list);
bcd68186 163 } else {
164 task_free(tq, t);
165 }
f1ca4da6 166
472a34ca 167 SEXIT;
f1ca4da6 168}
169
82387586 170/*
d9acd930
BB
171 * When a delayed task timer expires remove it from the delay list and
172 * add it to the priority list in order for immediate processing.
bcd68186 173 */
d9acd930
BB
174static void
175task_expire(unsigned long data)
bcd68186 176{
d9acd930
BB
177 taskq_ent_t *w, *t = (taskq_ent_t *)data;
178 taskq_t *tq = t->tqent_taskq;
179 struct list_head *l;
7257ec41
BB
180
181 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
d9acd930
BB
182
183 if (t->tqent_flags & TQENT_FLAG_CANCEL) {
184 ASSERT(list_empty(&t->tqent_list));
185 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
186 return;
187 }
188
189 /*
190 * The priority list must be maintained in strict task id order
191 * from lowest to highest for lowest_id to be easily calculable.
192 */
193 list_del(&t->tqent_list);
194 list_for_each_prev(l, &tq->tq_prio_list) {
195 w = list_entry(l, taskq_ent_t, tqent_list);
196 if (w->tqent_id < t->tqent_id) {
197 list_add(&t->tqent_list, l);
198 break;
199 }
200 }
201 if (l == &tq->tq_prio_list)
202 list_add(&t->tqent_list, &tq->tq_prio_list);
203
7257ec41
BB
204 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
205
d9acd930
BB
206 wake_up(&tq->tq_work_waitq);
207}
208
209/*
210 * Returns the lowest incomplete taskqid_t. The taskqid_t may
211 * be queued on the pending list, on the priority list, on the
212 * delay list, or on the work list currently being handled, but
213 * it is not 100% complete yet.
214 */
215static taskqid_t
216taskq_lowest_id(taskq_t *tq)
217{
218 taskqid_t lowest_id = tq->tq_next_id;
219 taskq_ent_t *t;
220 taskq_thread_t *tqt;
221 SENTRY;
222
223 ASSERT(tq);
224 ASSERT(spin_is_locked(&tq->tq_lock));
225
226 if (!list_empty(&tq->tq_pend_list)) {
227 t = list_entry(tq->tq_pend_list.next, taskq_ent_t, tqent_list);
228 lowest_id = MIN(lowest_id, t->tqent_id);
229 }
230
231 if (!list_empty(&tq->tq_prio_list)) {
232 t = list_entry(tq->tq_prio_list.next, taskq_ent_t, tqent_list);
233 lowest_id = MIN(lowest_id, t->tqent_id);
234 }
235
236 if (!list_empty(&tq->tq_delay_list)) {
237 t = list_entry(tq->tq_delay_list.next, taskq_ent_t, tqent_list);
238 lowest_id = MIN(lowest_id, t->tqent_id);
239 }
240
241 if (!list_empty(&tq->tq_active_list)) {
242 tqt = list_entry(tq->tq_active_list.next, taskq_thread_t,
243 tqt_active_list);
244 ASSERT(tqt->tqt_id != 0);
245 lowest_id = MIN(lowest_id, tqt->tqt_id);
246 }
247
248 SRETURN(lowest_id);
249}
250
251/*
252 * Insert a task into a list keeping the list sorted by increasing taskqid.
253 */
254static void
255taskq_insert_in_order(taskq_t *tq, taskq_thread_t *tqt)
256{
257 taskq_thread_t *w;
258 struct list_head *l;
259
260 SENTRY;
261 ASSERT(tq);
262 ASSERT(tqt);
263 ASSERT(spin_is_locked(&tq->tq_lock));
264
265 list_for_each_prev(l, &tq->tq_active_list) {
266 w = list_entry(l, taskq_thread_t, tqt_active_list);
267 if (w->tqt_id < tqt->tqt_id) {
268 list_add(&tqt->tqt_active_list, l);
269 break;
270 }
271 }
272 if (l == &tq->tq_active_list)
273 list_add(&tqt->tqt_active_list, &tq->tq_active_list);
274
275 SEXIT;
276}
277
278/*
279 * Find and return a task from the given list if it exists. The list
280 * must be in lowest to highest task id order.
281 */
282static taskq_ent_t *
283taskq_find_list(taskq_t *tq, struct list_head *lh, taskqid_t id)
284{
285 struct list_head *l;
286 taskq_ent_t *t;
287 SENTRY;
288
289 ASSERT(spin_is_locked(&tq->tq_lock));
290
291 list_for_each(l, lh) {
292 t = list_entry(l, taskq_ent_t, tqent_list);
293
294 if (t->tqent_id == id)
295 SRETURN(t);
296
297 if (t->tqent_id > id)
298 break;
299 }
300
301 SRETURN(NULL);
bcd68186 302}
303
d9acd930
BB
304/*
305 * Find an already dispatched task given the task id regardless of what
306 * state it is in. If a task is still pending or executing it will be
307 * returned and 'active' set appropriately. If the task has already
308 * been run then NULL is returned.
309 */
310static taskq_ent_t *
311taskq_find(taskq_t *tq, taskqid_t id, int *active)
312{
313 taskq_thread_t *tqt;
314 struct list_head *l;
315 taskq_ent_t *t;
316 SENTRY;
317
318 ASSERT(spin_is_locked(&tq->tq_lock));
319 *active = 0;
320
321 t = taskq_find_list(tq, &tq->tq_delay_list, id);
322 if (t)
323 SRETURN(t);
324
325 t = taskq_find_list(tq, &tq->tq_prio_list, id);
326 if (t)
327 SRETURN(t);
328
329 t = taskq_find_list(tq, &tq->tq_pend_list, id);
330 if (t)
331 SRETURN(t);
332
333 list_for_each(l, &tq->tq_active_list) {
334 tqt = list_entry(l, taskq_thread_t, tqt_active_list);
335 if (tqt->tqt_id == id) {
336 t = tqt->tqt_task;
337 *active = 1;
338 SRETURN(t);
339 }
340 }
341
342 SRETURN(NULL);
343}
344
345/*
346 * The taskq_wait_id() function blocks until the passed task id completes.
347 * This does not guarantee that all lower task id's have completed.
348 */
bcd68186 349void
aed8671c 350taskq_wait_id(taskq_t *tq, taskqid_t id)
f1ca4da6 351{
d9acd930
BB
352 DEFINE_WAIT(wait);
353 taskq_ent_t *t;
354 int active = 0;
b17edc10 355 SENTRY;
d9acd930 356
bcd68186 357 ASSERT(tq);
d9acd930 358 ASSERT(id > 0);
bcd68186 359
d9acd930
BB
360 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
361 t = taskq_find(tq, id, &active);
362 if (t)
363 prepare_to_wait(&t->tqent_waitq, &wait, TASK_UNINTERRUPTIBLE);
364 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
365
366 /*
367 * We rely on the kernels autoremove_wake_function() function to
368 * remove us from the wait queue in the context of wake_up().
369 * Once woken the taskq_ent_t pointer must never be accessed.
370 */
371 if (t) {
372 t = NULL;
373 schedule();
374 __set_current_state(TASK_RUNNING);
375 }
bcd68186 376
b17edc10 377 SEXIT;
bcd68186 378}
aed8671c 379EXPORT_SYMBOL(taskq_wait_id);
bcd68186 380
d9acd930
BB
381/*
382 * The taskq_wait() function will block until all previously submitted
383 * tasks have been completed. A previously submitted task is defined as
384 * a task with a lower task id than the current task queue id. Note that
385 * all task id's are assigned monotonically at dispatch time.
386 *
387 * Waiting for all previous tasks to complete is accomplished by tracking
388 * the lowest outstanding task id. As tasks are dispatched they are added
389 * added to the tail of the pending, priority, or delay lists. And as
390 * worker threads become available the tasks are removed from the heads
391 * of these lists and linked to the worker threads. This ensures the
392 * lists are kept in lowest to highest task id order.
393 *
394 * Therefore the lowest outstanding task id can be quickly determined by
395 * checking the head item from all of these lists. This value is stored
396 * with the task queue as the lowest id. It only needs to be recalculated
397 * when either the task with the current lowest id completes or is canceled.
398 *
399 * By blocking until the lowest task id exceeds the current task id when
400 * the function was called we ensure all previous tasks have completed.
401 *
402 * NOTE: When there are multiple worked threads it is possible for larger
403 * task ids to complete before smaller ones. Conversely when the task
404 * queue contains delay tasks with small task ids, you may block for a
405 * considerable length of time waiting for them to expire and execute.
406 */
407static int
408taskq_wait_check(taskq_t *tq, taskqid_t id)
409{
410 int rc;
411
412 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
413 rc = (id < tq->tq_lowest_id);
414 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
415
416 SRETURN(rc);
417}
418
419void
420taskq_wait_all(taskq_t *tq, taskqid_t id)
421{
422 wait_event(tq->tq_wait_waitq, taskq_wait_check(tq, id));
423}
424EXPORT_SYMBOL(taskq_wait_all);
425
bcd68186 426void
aed8671c 427taskq_wait(taskq_t *tq)
bcd68186 428{
429 taskqid_t id;
b17edc10 430 SENTRY;
bcd68186 431 ASSERT(tq);
432
7257ec41 433 /* Wait for the largest outstanding taskqid */
749045bb 434 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
7257ec41 435 id = tq->tq_next_id - 1;
749045bb 436 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 437
d9acd930 438 taskq_wait_all(tq, id);
bcd68186 439
b17edc10 440 SEXIT;
bcd68186 441
442}
aed8671c 443EXPORT_SYMBOL(taskq_wait);
bcd68186 444
445int
aed8671c 446taskq_member(taskq_t *tq, void *t)
bcd68186 447{
2c02b71b
PS
448 struct list_head *l;
449 taskq_thread_t *tqt;
472a34ca 450 SENTRY;
bcd68186 451
452 ASSERT(tq);
472a34ca 453 ASSERT(t);
bcd68186 454
2c02b71b
PS
455 list_for_each(l, &tq->tq_thread_list) {
456 tqt = list_entry(l, taskq_thread_t, tqt_thread_list);
457 if (tqt->tqt_thread == (struct task_struct *)t)
458 SRETURN(1);
459 }
bcd68186 460
472a34ca 461 SRETURN(0);
bcd68186 462}
aed8671c 463EXPORT_SYMBOL(taskq_member);
bcd68186 464
d9acd930
BB
465/*
466 * Cancel an already dispatched task given the task id. Still pending tasks
467 * will be immediately canceled, and if the task is active the function will
468 * block until it completes. Preallocated tasks which are canceled must be
469 * freed by the caller.
470 */
471int
472taskq_cancel_id(taskq_t *tq, taskqid_t id)
473{
474 taskq_ent_t *t;
475 int active = 0;
476 int rc = ENOENT;
477 SENTRY;
478
479 ASSERT(tq);
480
481 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
482 t = taskq_find(tq, id, &active);
483 if (t && !active) {
484 list_del_init(&t->tqent_list);
485 t->tqent_flags |= TQENT_FLAG_CANCEL;
486
487 /*
488 * When canceling the lowest outstanding task id we
489 * must recalculate the new lowest outstanding id.
490 */
491 if (tq->tq_lowest_id == t->tqent_id) {
492 tq->tq_lowest_id = taskq_lowest_id(tq);
493 ASSERT3S(tq->tq_lowest_id, >, t->tqent_id);
494 }
495
496 /*
497 * The task_expire() function takes the tq->tq_lock so drop
498 * drop the lock before synchronously cancelling the timer.
499 */
500 if (timer_pending(&t->tqent_timer)) {
501 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
502 del_timer_sync(&t->tqent_timer);
503 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
504 }
505
506 if (!(t->tqent_flags & TQENT_FLAG_PREALLOC))
507 task_done(tq, t);
508
509 rc = 0;
510 }
511 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
512
513 if (active) {
514 taskq_wait_id(tq, id);
515 rc = EBUSY;
516 }
517
518 SRETURN(rc);
519}
520EXPORT_SYMBOL(taskq_cancel_id);
521
bcd68186 522taskqid_t
aed8671c 523taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
bcd68186 524{
472a34ca 525 taskq_ent_t *t;
bcd68186 526 taskqid_t rc = 0;
472a34ca 527 SENTRY;
f1ca4da6 528
472a34ca
BB
529 ASSERT(tq);
530 ASSERT(func);
d05ec4b4 531
472a34ca 532 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
f1ca4da6 533
bcd68186 534 /* Taskq being destroyed and all tasks drained */
535 if (!(tq->tq_flags & TQ_ACTIVE))
b17edc10 536 SGOTO(out, rc = 0);
f1ca4da6 537
bcd68186 538 /* Do not queue the task unless there is idle thread for it */
539 ASSERT(tq->tq_nactive <= tq->tq_nthreads);
540 if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads))
b17edc10 541 SGOTO(out, rc = 0);
bcd68186 542
472a34ca 543 if ((t = task_alloc(tq, flags)) == NULL)
b17edc10 544 SGOTO(out, rc = 0);
f1ca4da6 545
046a70c9 546 spin_lock(&t->tqent_lock);
f0d8bb26
NB
547
548 /* Queue to the priority list instead of the pending list */
549 if (flags & TQ_FRONT)
046a70c9 550 list_add_tail(&t->tqent_list, &tq->tq_prio_list);
f0d8bb26 551 else
046a70c9 552 list_add_tail(&t->tqent_list, &tq->tq_pend_list);
f0d8bb26 553
046a70c9 554 t->tqent_id = rc = tq->tq_next_id;
bcd68186 555 tq->tq_next_id++;
472a34ca
BB
556 t->tqent_func = func;
557 t->tqent_arg = arg;
d9acd930
BB
558 t->tqent_taskq = tq;
559 t->tqent_timer.data = 0;
560 t->tqent_timer.function = NULL;
561 t->tqent_timer.expires = 0;
44217f7a
PS
562
563 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
564
046a70c9 565 spin_unlock(&t->tqent_lock);
0bb43ca2
NB
566
567 wake_up(&tq->tq_work_waitq);
bcd68186 568out:
749045bb 569 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
b17edc10 570 SRETURN(rc);
f1ca4da6 571}
aed8671c 572EXPORT_SYMBOL(taskq_dispatch);
44217f7a 573
d9acd930
BB
574taskqid_t
575taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg,
576 uint_t flags, clock_t expire_time)
577{
578 taskq_ent_t *t;
579 taskqid_t rc = 0;
580 SENTRY;
581
582 ASSERT(tq);
583 ASSERT(func);
584
585 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
586
587 /* Taskq being destroyed and all tasks drained */
588 if (!(tq->tq_flags & TQ_ACTIVE))
589 SGOTO(out, rc = 0);
590
591 if ((t = task_alloc(tq, flags)) == NULL)
592 SGOTO(out, rc = 0);
593
594 spin_lock(&t->tqent_lock);
595
596 /* Queue to the delay list for subsequent execution */
597 list_add_tail(&t->tqent_list, &tq->tq_delay_list);
598
599 t->tqent_id = rc = tq->tq_next_id;
600 tq->tq_next_id++;
601 t->tqent_func = func;
602 t->tqent_arg = arg;
603 t->tqent_taskq = tq;
604 t->tqent_timer.data = (unsigned long)t;
605 t->tqent_timer.function = task_expire;
606 t->tqent_timer.expires = (unsigned long)expire_time;
607 add_timer(&t->tqent_timer);
608
609 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
610
611 spin_unlock(&t->tqent_lock);
612out:
613 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
614 SRETURN(rc);
615}
616EXPORT_SYMBOL(taskq_dispatch_delay);
617
44217f7a 618void
aed8671c 619taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
44217f7a
PS
620 taskq_ent_t *t)
621{
622 SENTRY;
623
624 ASSERT(tq);
625 ASSERT(func);
626 ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC));
627
628 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
629
630 /* Taskq being destroyed and all tasks drained */
631 if (!(tq->tq_flags & TQ_ACTIVE)) {
632 t->tqent_id = 0;
633 goto out;
634 }
635
636 spin_lock(&t->tqent_lock);
637
638 /*
639 * Mark it as a prealloc'd task. This is important
640 * to ensure that we don't free it later.
641 */
642 t->tqent_flags |= TQENT_FLAG_PREALLOC;
643
644 /* Queue to the priority list instead of the pending list */
645 if (flags & TQ_FRONT)
646 list_add_tail(&t->tqent_list, &tq->tq_prio_list);
647 else
648 list_add_tail(&t->tqent_list, &tq->tq_pend_list);
649
650 t->tqent_id = tq->tq_next_id;
651 tq->tq_next_id++;
652 t->tqent_func = func;
653 t->tqent_arg = arg;
d9acd930 654 t->tqent_taskq = tq;
44217f7a
PS
655
656 spin_unlock(&t->tqent_lock);
657
658 wake_up(&tq->tq_work_waitq);
659out:
0bb43ca2 660 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
44217f7a
PS
661 SEXIT;
662}
aed8671c 663EXPORT_SYMBOL(taskq_dispatch_ent);
44217f7a
PS
664
665int
aed8671c 666taskq_empty_ent(taskq_ent_t *t)
44217f7a
PS
667{
668 return list_empty(&t->tqent_list);
669}
aed8671c 670EXPORT_SYMBOL(taskq_empty_ent);
44217f7a
PS
671
672void
aed8671c 673taskq_init_ent(taskq_ent_t *t)
44217f7a
PS
674{
675 spin_lock_init(&t->tqent_lock);
d9acd930
BB
676 init_waitqueue_head(&t->tqent_waitq);
677 init_timer(&t->tqent_timer);
44217f7a
PS
678 INIT_LIST_HEAD(&t->tqent_list);
679 t->tqent_id = 0;
680 t->tqent_func = NULL;
681 t->tqent_arg = NULL;
682 t->tqent_flags = 0;
d9acd930 683 t->tqent_taskq = NULL;
44217f7a 684}
aed8671c 685EXPORT_SYMBOL(taskq_init_ent);
44217f7a 686
bcd68186 687static int
688taskq_thread(void *args)
689{
472a34ca
BB
690 DECLARE_WAITQUEUE(wait, current);
691 sigset_t blocked;
2c02b71b 692 taskq_thread_t *tqt = args;
472a34ca
BB
693 taskq_t *tq;
694 taskq_ent_t *t;
f0d8bb26 695 struct list_head *pend_list;
b17edc10 696 SENTRY;
bcd68186 697
472a34ca 698 ASSERT(tqt);
2c02b71b 699 tq = tqt->tqt_tq;
472a34ca 700 current->flags |= PF_NOFREEZE;
bcd68186 701
472a34ca
BB
702 sigfillset(&blocked);
703 sigprocmask(SIG_BLOCK, &blocked, NULL);
704 flush_signals(current);
bcd68186 705
472a34ca
BB
706 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
707 tq->tq_nthreads++;
708 wake_up(&tq->tq_wait_waitq);
709 set_current_state(TASK_INTERRUPTIBLE);
bcd68186 710
472a34ca 711 while (!kthread_should_stop()) {
bcd68186 712
f0d8bb26
NB
713 if (list_empty(&tq->tq_pend_list) &&
714 list_empty(&tq->tq_prio_list)) {
3c6ed541 715 add_wait_queue_exclusive(&tq->tq_work_waitq, &wait);
749045bb 716 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 717 schedule();
749045bb 718 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
3c6ed541 719 remove_wait_queue(&tq->tq_work_waitq, &wait);
bcd68186 720 } else {
721 __set_current_state(TASK_RUNNING);
722 }
723
f0d8bb26
NB
724
725 if (!list_empty(&tq->tq_prio_list))
726 pend_list = &tq->tq_prio_list;
727 else if (!list_empty(&tq->tq_pend_list))
728 pend_list = &tq->tq_pend_list;
729 else
730 pend_list = NULL;
731
732 if (pend_list) {
472a34ca
BB
733 t = list_entry(pend_list->next,taskq_ent_t,tqent_list);
734 list_del_init(&t->tqent_list);
8f2503e0 735
44217f7a
PS
736 /* In order to support recursively dispatching a
737 * preallocated taskq_ent_t, tqent_id must be
738 * stored prior to executing tqent_func. */
e7e5f78e 739 tqt->tqt_id = t->tqent_id;
d9acd930 740 tqt->tqt_task = t;
8f2503e0
PS
741
742 /* We must store a copy of the flags prior to
743 * servicing the task (servicing a prealloc'd task
744 * returns the ownership of the tqent back to
745 * the caller of taskq_dispatch). Thus,
746 * tqent_flags _may_ change within the call. */
747 tqt->tqt_flags = t->tqent_flags;
748
2c02b71b 749 taskq_insert_in_order(tq, tqt);
472a34ca 750 tq->tq_nactive++;
749045bb 751 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 752
753 /* Perform the requested task */
472a34ca 754 t->tqent_func(t->tqent_arg);
bcd68186 755
749045bb 756 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
472a34ca 757 tq->tq_nactive--;
2c02b71b 758 list_del_init(&tqt->tqt_active_list);
d9acd930 759 tqt->tqt_task = NULL;
8f2503e0
PS
760
761 /* For prealloc'd tasks, we don't free anything. */
762 if ((tq->tq_flags & TASKQ_DYNAMIC) ||
763 !(tqt->tqt_flags & TQENT_FLAG_PREALLOC))
764 task_done(tq, t);
bcd68186 765
7257ec41
BB
766 /* When the current lowest outstanding taskqid is
767 * done calculate the new lowest outstanding id */
e7e5f78e 768 if (tq->tq_lowest_id == tqt->tqt_id) {
bcd68186 769 tq->tq_lowest_id = taskq_lowest_id(tq);
e7e5f78e 770 ASSERT3S(tq->tq_lowest_id, >, tqt->tqt_id);
bcd68186 771 }
772
e7e5f78e 773 tqt->tqt_id = 0;
8f2503e0 774 tqt->tqt_flags = 0;
472a34ca 775 wake_up_all(&tq->tq_wait_waitq);
bcd68186 776 }
777
778 set_current_state(TASK_INTERRUPTIBLE);
779
472a34ca 780 }
bcd68186 781
782 __set_current_state(TASK_RUNNING);
472a34ca 783 tq->tq_nthreads--;
2c02b71b
PS
784 list_del_init(&tqt->tqt_thread_list);
785 kmem_free(tqt, sizeof(taskq_thread_t));
786
472a34ca 787 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 788
b17edc10 789 SRETURN(0);
bcd68186 790}
791
f1ca4da6 792taskq_t *
aed8671c 793taskq_create(const char *name, int nthreads, pri_t pri,
472a34ca 794 int minalloc, int maxalloc, uint_t flags)
f1ca4da6 795{
472a34ca 796 taskq_t *tq;
2c02b71b 797 taskq_thread_t *tqt;
472a34ca
BB
798 int rc = 0, i, j = 0;
799 SENTRY;
bcd68186 800
472a34ca
BB
801 ASSERT(name != NULL);
802 ASSERT(pri <= maxclsyspri);
803 ASSERT(minalloc >= 0);
804 ASSERT(maxalloc <= INT_MAX);
805 ASSERT(!(flags & (TASKQ_CPR_SAFE | TASKQ_DYNAMIC))); /* Unsupported */
bcd68186 806
915404bd
BB
807 /* Scale the number of threads using nthreads as a percentage */
808 if (flags & TASKQ_THREADS_CPU_PCT) {
809 ASSERT(nthreads <= 100);
810 ASSERT(nthreads >= 0);
811 nthreads = MIN(nthreads, 100);
812 nthreads = MAX(nthreads, 0);
813 nthreads = MAX((num_online_cpus() * nthreads) / 100, 1);
814 }
815
472a34ca
BB
816 tq = kmem_alloc(sizeof(*tq), KM_PUSHPAGE);
817 if (tq == NULL)
818 SRETURN(NULL);
bcd68186 819
472a34ca
BB
820 spin_lock_init(&tq->tq_lock);
821 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
822 INIT_LIST_HEAD(&tq->tq_thread_list);
823 INIT_LIST_HEAD(&tq->tq_active_list);
824 tq->tq_name = name;
825 tq->tq_nactive = 0;
bcd68186 826 tq->tq_nthreads = 0;
472a34ca
BB
827 tq->tq_pri = pri;
828 tq->tq_minalloc = minalloc;
829 tq->tq_maxalloc = maxalloc;
bcd68186 830 tq->tq_nalloc = 0;
472a34ca 831 tq->tq_flags = (flags | TQ_ACTIVE);
bcd68186 832 tq->tq_next_id = 1;
833 tq->tq_lowest_id = 1;
472a34ca
BB
834 INIT_LIST_HEAD(&tq->tq_free_list);
835 INIT_LIST_HEAD(&tq->tq_pend_list);
836 INIT_LIST_HEAD(&tq->tq_prio_list);
d9acd930 837 INIT_LIST_HEAD(&tq->tq_delay_list);
472a34ca
BB
838 init_waitqueue_head(&tq->tq_work_waitq);
839 init_waitqueue_head(&tq->tq_wait_waitq);
bcd68186 840
472a34ca
BB
841 if (flags & TASKQ_PREPOPULATE)
842 for (i = 0; i < minalloc; i++)
843 task_done(tq, task_alloc(tq, TQ_PUSHPAGE | TQ_NEW));
6e605b6e 844
472a34ca 845 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
6e605b6e 846
2c02b71b 847 for (i = 0; i < nthreads; i++) {
9b51f218 848 tqt = kmem_alloc(sizeof(*tqt), KM_PUSHPAGE);
2c02b71b
PS
849 INIT_LIST_HEAD(&tqt->tqt_thread_list);
850 INIT_LIST_HEAD(&tqt->tqt_active_list);
851 tqt->tqt_tq = tq;
e7e5f78e 852 tqt->tqt_id = 0;
2c02b71b
PS
853
854 tqt->tqt_thread = kthread_create(taskq_thread, tqt,
472a34ca 855 "%s/%d", name, i);
2c02b71b
PS
856 if (tqt->tqt_thread) {
857 list_add(&tqt->tqt_thread_list, &tq->tq_thread_list);
858 kthread_bind(tqt->tqt_thread, i % num_online_cpus());
859 set_user_nice(tqt->tqt_thread, PRIO_TO_NICE(pri));
860 wake_up_process(tqt->tqt_thread);
bcd68186 861 j++;
2c02b71b
PS
862 } else {
863 kmem_free(tqt, sizeof(taskq_thread_t));
864 rc = 1;
865 }
866 }
bcd68186 867
472a34ca 868 /* Wait for all threads to be started before potential destroy */
bcd68186 869 wait_event(tq->tq_wait_waitq, tq->tq_nthreads == j);
870
472a34ca 871 if (rc) {
aed8671c 872 taskq_destroy(tq);
472a34ca
BB
873 tq = NULL;
874 }
bcd68186 875
472a34ca 876 SRETURN(tq);
f1ca4da6 877}
aed8671c 878EXPORT_SYMBOL(taskq_create);
b123971f 879
880void
aed8671c 881taskq_destroy(taskq_t *tq)
b123971f 882{
2c02b71b
PS
883 struct task_struct *thread;
884 taskq_thread_t *tqt;
046a70c9 885 taskq_ent_t *t;
b17edc10 886 SENTRY;
b123971f 887
bcd68186 888 ASSERT(tq);
749045bb 889 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
472a34ca 890 tq->tq_flags &= ~TQ_ACTIVE;
749045bb 891 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 892
893 /* TQ_ACTIVE cleared prevents new tasks being added to pending */
aed8671c 894 taskq_wait(tq);
bcd68186 895
472a34ca 896 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 897
2c02b71b
PS
898 /*
899 * Signal each thread to exit and block until it does. Each thread
900 * is responsible for removing itself from the list and freeing its
901 * taskq_thread_t. This allows for idle threads to opt to remove
902 * themselves from the taskq. They can be recreated as needed.
903 */
904 while (!list_empty(&tq->tq_thread_list)) {
905 tqt = list_entry(tq->tq_thread_list.next,
906 taskq_thread_t, tqt_thread_list);
907 thread = tqt->tqt_thread;
908 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
909
910 kthread_stop(thread);
911
472a34ca 912 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
2c02b71b
PS
913 }
914
472a34ca 915 while (!list_empty(&tq->tq_free_list)) {
046a70c9 916 t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
44217f7a
PS
917
918 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
919
472a34ca
BB
920 list_del_init(&t->tqent_list);
921 task_free(tq, t);
922 }
bcd68186 923
472a34ca
BB
924 ASSERT(tq->tq_nthreads == 0);
925 ASSERT(tq->tq_nalloc == 0);
926 ASSERT(list_empty(&tq->tq_thread_list));
927 ASSERT(list_empty(&tq->tq_active_list));
928 ASSERT(list_empty(&tq->tq_free_list));
929 ASSERT(list_empty(&tq->tq_pend_list));
930 ASSERT(list_empty(&tq->tq_prio_list));
d9acd930 931 ASSERT(list_empty(&tq->tq_delay_list));
bcd68186 932
472a34ca 933 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
2c02b71b 934
472a34ca 935 kmem_free(tq, sizeof(taskq_t));
bcd68186 936
b17edc10 937 SEXIT;
b123971f 938}
aed8671c 939EXPORT_SYMBOL(taskq_destroy);
e9cb2b4f
BB
940
941int
942spl_taskq_init(void)
943{
472a34ca 944 SENTRY;
e9cb2b4f 945
f220894e
BB
946 /* Solaris creates a dynamic taskq of up to 64 threads, however in
947 * a Linux environment 1 thread per-core is usually about right */
472a34ca 948 system_taskq = taskq_create("spl_system_taskq", num_online_cpus(),
f220894e 949 minclsyspri, 4, 512, TASKQ_PREPOPULATE);
e9cb2b4f 950 if (system_taskq == NULL)
b17edc10 951 SRETURN(1);
e9cb2b4f 952
472a34ca 953 SRETURN(0);
e9cb2b4f
BB
954}
955
956void
957spl_taskq_fini(void)
958{
472a34ca 959 SENTRY;
e9cb2b4f 960 taskq_destroy(system_taskq);
472a34ca 961 SEXIT;
e9cb2b4f 962}