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>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
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.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Task Queue Implementation.
25 \*****************************************************************************/
27 #include <sys/taskq.h>
30 int spl_taskq_thread_bind
= 0;
31 module_param(spl_taskq_thread_bind
, int, 0644);
32 MODULE_PARM_DESC(spl_taskq_thread_bind
, "Bind taskq thread to CPU by default");
35 int spl_taskq_thread_dynamic
= 1;
36 module_param(spl_taskq_thread_dynamic
, int, 0644);
37 MODULE_PARM_DESC(spl_taskq_thread_dynamic
, "Allow dynamic taskq threads");
39 int spl_taskq_thread_sequential
= 4;
40 module_param(spl_taskq_thread_sequential
, int, 0644);
41 MODULE_PARM_DESC(spl_taskq_thread_sequential
,
42 "Create new taskq threads after N sequential tasks");
44 /* Global system-wide dynamic task queue available for all consumers */
45 taskq_t
*system_taskq
;
46 EXPORT_SYMBOL(system_taskq
);
48 /* Private dedicated taskq for creating new taskq threads on demand. */
49 static taskq_t
*dynamic_taskq
;
50 static taskq_thread_t
*taskq_thread_create(taskq_t
*);
53 task_km_flags(uint_t flags
)
55 if (flags
& TQ_NOSLEEP
)
58 if (flags
& TQ_PUSHPAGE
)
65 * NOTE: Must be called with tq->tq_lock held, returns a list_t which
66 * is not attached to the free, work, or pending taskq lists.
69 task_alloc(taskq_t
*tq
, uint_t flags
)
75 ASSERT(spin_is_locked(&tq
->tq_lock
));
77 /* Acquire taskq_ent_t's from free list if available */
78 if (!list_empty(&tq
->tq_free_list
) && !(flags
& TQ_NEW
)) {
79 t
= list_entry(tq
->tq_free_list
.next
, taskq_ent_t
, tqent_list
);
81 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
82 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_CANCEL
));
83 ASSERT(!timer_pending(&t
->tqent_timer
));
85 list_del_init(&t
->tqent_list
);
89 /* Free list is empty and memory allocations are prohibited */
90 if (flags
& TQ_NOALLOC
)
93 /* Hit maximum taskq_ent_t pool size */
94 if (tq
->tq_nalloc
>= tq
->tq_maxalloc
) {
95 if (flags
& TQ_NOSLEEP
)
99 * Sleep periodically polling the free list for an available
100 * taskq_ent_t. Dispatching with TQ_SLEEP should always succeed
101 * but we cannot block forever waiting for an taskq_ent_t to
102 * show up in the free list, otherwise a deadlock can happen.
104 * Therefore, we need to allocate a new task even if the number
105 * of allocated tasks is above tq->tq_maxalloc, but we still
106 * end up delaying the task allocation by one second, thereby
107 * throttling the task dispatch rate.
109 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
110 schedule_timeout(HZ
/ 100);
111 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
118 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
119 t
= kmem_alloc(sizeof(taskq_ent_t
), task_km_flags(flags
));
120 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
131 * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t
132 * to already be removed from the free, work, or pending taskq lists.
135 task_free(taskq_t
*tq
, taskq_ent_t
*t
)
139 ASSERT(spin_is_locked(&tq
->tq_lock
));
140 ASSERT(list_empty(&t
->tqent_list
));
141 ASSERT(!timer_pending(&t
->tqent_timer
));
143 kmem_free(t
, sizeof(taskq_ent_t
));
148 * NOTE: Must be called with tq->tq_lock held, either destroys the
149 * taskq_ent_t if too many exist or moves it to the free list for later use.
152 task_done(taskq_t
*tq
, taskq_ent_t
*t
)
156 ASSERT(spin_is_locked(&tq
->tq_lock
));
158 /* Wake tasks blocked in taskq_wait_id() */
159 wake_up_all(&t
->tqent_waitq
);
161 list_del_init(&t
->tqent_list
);
163 if (tq
->tq_nalloc
<= tq
->tq_minalloc
) {
165 t
->tqent_func
= NULL
;
169 list_add_tail(&t
->tqent_list
, &tq
->tq_free_list
);
176 * When a delayed task timer expires remove it from the delay list and
177 * add it to the priority list in order for immediate processing.
180 task_expire(unsigned long data
)
182 taskq_ent_t
*w
, *t
= (taskq_ent_t
*)data
;
183 taskq_t
*tq
= t
->tqent_taskq
;
186 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
188 if (t
->tqent_flags
& TQENT_FLAG_CANCEL
) {
189 ASSERT(list_empty(&t
->tqent_list
));
190 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
195 * The priority list must be maintained in strict task id order
196 * from lowest to highest for lowest_id to be easily calculable.
198 list_del(&t
->tqent_list
);
199 list_for_each_prev(l
, &tq
->tq_prio_list
) {
200 w
= list_entry(l
, taskq_ent_t
, tqent_list
);
201 if (w
->tqent_id
< t
->tqent_id
) {
202 list_add(&t
->tqent_list
, l
);
206 if (l
== &tq
->tq_prio_list
)
207 list_add(&t
->tqent_list
, &tq
->tq_prio_list
);
209 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
211 wake_up(&tq
->tq_work_waitq
);
215 * Returns the lowest incomplete taskqid_t. The taskqid_t may
216 * be queued on the pending list, on the priority list, on the
217 * delay list, or on the work list currently being handled, but
218 * it is not 100% complete yet.
221 taskq_lowest_id(taskq_t
*tq
)
223 taskqid_t lowest_id
= tq
->tq_next_id
;
228 ASSERT(spin_is_locked(&tq
->tq_lock
));
230 if (!list_empty(&tq
->tq_pend_list
)) {
231 t
= list_entry(tq
->tq_pend_list
.next
, taskq_ent_t
, tqent_list
);
232 lowest_id
= MIN(lowest_id
, t
->tqent_id
);
235 if (!list_empty(&tq
->tq_prio_list
)) {
236 t
= list_entry(tq
->tq_prio_list
.next
, taskq_ent_t
, tqent_list
);
237 lowest_id
= MIN(lowest_id
, t
->tqent_id
);
240 if (!list_empty(&tq
->tq_delay_list
)) {
241 t
= list_entry(tq
->tq_delay_list
.next
, taskq_ent_t
, tqent_list
);
242 lowest_id
= MIN(lowest_id
, t
->tqent_id
);
245 if (!list_empty(&tq
->tq_active_list
)) {
246 tqt
= list_entry(tq
->tq_active_list
.next
, taskq_thread_t
,
248 ASSERT(tqt
->tqt_id
!= 0);
249 lowest_id
= MIN(lowest_id
, tqt
->tqt_id
);
256 * Insert a task into a list keeping the list sorted by increasing taskqid.
259 taskq_insert_in_order(taskq_t
*tq
, taskq_thread_t
*tqt
)
266 ASSERT(spin_is_locked(&tq
->tq_lock
));
268 list_for_each_prev(l
, &tq
->tq_active_list
) {
269 w
= list_entry(l
, taskq_thread_t
, tqt_active_list
);
270 if (w
->tqt_id
< tqt
->tqt_id
) {
271 list_add(&tqt
->tqt_active_list
, l
);
275 if (l
== &tq
->tq_active_list
)
276 list_add(&tqt
->tqt_active_list
, &tq
->tq_active_list
);
280 * Find and return a task from the given list if it exists. The list
281 * must be in lowest to highest task id order.
284 taskq_find_list(taskq_t
*tq
, struct list_head
*lh
, taskqid_t id
)
289 ASSERT(spin_is_locked(&tq
->tq_lock
));
291 list_for_each(l
, lh
) {
292 t
= list_entry(l
, taskq_ent_t
, tqent_list
);
294 if (t
->tqent_id
== id
)
297 if (t
->tqent_id
> id
)
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.
311 taskq_find(taskq_t
*tq
, taskqid_t id
, int *active
)
317 ASSERT(spin_is_locked(&tq
->tq_lock
));
320 t
= taskq_find_list(tq
, &tq
->tq_delay_list
, id
);
324 t
= taskq_find_list(tq
, &tq
->tq_prio_list
, id
);
328 t
= taskq_find_list(tq
, &tq
->tq_pend_list
, id
);
332 list_for_each(l
, &tq
->tq_active_list
) {
333 tqt
= list_entry(l
, taskq_thread_t
, tqt_active_list
);
334 if (tqt
->tqt_id
== id
) {
345 * Theory for the taskq_wait_id(), taskq_wait_outstanding(), and
346 * taskq_wait() functions below.
348 * Taskq waiting is accomplished by tracking the lowest outstanding task
349 * id and the next available task id. As tasks are dispatched they are
350 * added to the tail of the pending, priority, or delay lists. As worker
351 * threads become available the tasks are removed from the heads of these
352 * lists and linked to the worker threads. This ensures the lists are
353 * kept sorted by lowest to highest task id.
355 * Therefore the lowest outstanding task id can be quickly determined by
356 * checking the head item from all of these lists. This value is stored
357 * with the taskq as the lowest id. It only needs to be recalculated when
358 * either the task with the current lowest id completes or is canceled.
360 * By blocking until the lowest task id exceeds the passed task id the
361 * taskq_wait_outstanding() function can be easily implemented. Similarly,
362 * by blocking until the lowest task id matches the next task id taskq_wait()
363 * can be implemented.
365 * Callers should be aware that when there are multiple worked threads it
366 * is possible for larger task ids to complete before smaller ones. Also
367 * when the taskq contains delay tasks with small task ids callers may
368 * block for a considerable length of time waiting for them to expire and
372 taskq_wait_id_check(taskq_t
*tq
, taskqid_t id
)
377 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
378 rc
= (taskq_find(tq
, id
, &active
) == NULL
);
379 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
385 * The taskq_wait_id() function blocks until the passed task id completes.
386 * This does not guarantee that all lower task ids have completed.
389 taskq_wait_id(taskq_t
*tq
, taskqid_t id
)
391 wait_event(tq
->tq_wait_waitq
, taskq_wait_id_check(tq
, id
));
393 EXPORT_SYMBOL(taskq_wait_id
);
396 taskq_wait_outstanding_check(taskq_t
*tq
, taskqid_t id
)
400 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
401 rc
= (id
< tq
->tq_lowest_id
);
402 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
408 * The taskq_wait_outstanding() function will block until all tasks with a
409 * lower taskqid than the passed 'id' have been completed. Note that all
410 * task id's are assigned monotonically at dispatch time. Zero may be
411 * passed for the id to indicate all tasks dispatch up to this point,
412 * but not after, should be waited for.
415 taskq_wait_outstanding(taskq_t
*tq
, taskqid_t id
)
417 wait_event(tq
->tq_wait_waitq
,
418 taskq_wait_outstanding_check(tq
, id
? id
: tq
->tq_next_id
- 1));
420 EXPORT_SYMBOL(taskq_wait_outstanding
);
423 taskq_wait_check(taskq_t
*tq
)
427 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
428 rc
= (tq
->tq_lowest_id
== tq
->tq_next_id
);
429 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
435 * The taskq_wait() function will block until the taskq is empty.
436 * This means that if a taskq re-dispatches work to itself taskq_wait()
437 * callers will block indefinitely.
440 taskq_wait(taskq_t
*tq
)
442 wait_event(tq
->tq_wait_waitq
, taskq_wait_check(tq
));
444 EXPORT_SYMBOL(taskq_wait
);
447 taskq_member(taskq_t
*tq
, void *t
)
456 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
457 list_for_each(l
, &tq
->tq_thread_list
) {
458 tqt
= list_entry(l
, taskq_thread_t
, tqt_thread_list
);
459 if (tqt
->tqt_thread
== (struct task_struct
*)t
) {
464 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
468 EXPORT_SYMBOL(taskq_member
);
471 * Cancel an already dispatched task given the task id. Still pending tasks
472 * will be immediately canceled, and if the task is active the function will
473 * block until it completes. Preallocated tasks which are canceled must be
474 * freed by the caller.
477 taskq_cancel_id(taskq_t
*tq
, taskqid_t id
)
485 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
486 t
= taskq_find(tq
, id
, &active
);
488 list_del_init(&t
->tqent_list
);
489 t
->tqent_flags
|= TQENT_FLAG_CANCEL
;
492 * When canceling the lowest outstanding task id we
493 * must recalculate the new lowest outstanding id.
495 if (tq
->tq_lowest_id
== t
->tqent_id
) {
496 tq
->tq_lowest_id
= taskq_lowest_id(tq
);
497 ASSERT3S(tq
->tq_lowest_id
, >, t
->tqent_id
);
501 * The task_expire() function takes the tq->tq_lock so drop
502 * drop the lock before synchronously cancelling the timer.
504 if (timer_pending(&t
->tqent_timer
)) {
505 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
506 del_timer_sync(&t
->tqent_timer
);
507 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
510 if (!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
))
515 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
518 taskq_wait_id(tq
, id
);
524 EXPORT_SYMBOL(taskq_cancel_id
);
527 taskq_dispatch(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
)
535 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
537 /* Taskq being destroyed and all tasks drained */
538 if (!(tq
->tq_flags
& TASKQ_ACTIVE
))
541 /* Do not queue the task unless there is idle thread for it */
542 ASSERT(tq
->tq_nactive
<= tq
->tq_nthreads
);
543 if ((flags
& TQ_NOQUEUE
) && (tq
->tq_nactive
== tq
->tq_nthreads
))
546 if ((t
= task_alloc(tq
, flags
)) == NULL
)
549 spin_lock(&t
->tqent_lock
);
551 /* Queue to the priority list instead of the pending list */
552 if (flags
& TQ_FRONT
)
553 list_add_tail(&t
->tqent_list
, &tq
->tq_prio_list
);
555 list_add_tail(&t
->tqent_list
, &tq
->tq_pend_list
);
557 t
->tqent_id
= rc
= tq
->tq_next_id
;
559 t
->tqent_func
= func
;
562 t
->tqent_timer
.data
= 0;
563 t
->tqent_timer
.function
= NULL
;
564 t
->tqent_timer
.expires
= 0;
566 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
568 spin_unlock(&t
->tqent_lock
);
570 wake_up(&tq
->tq_work_waitq
);
572 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
575 EXPORT_SYMBOL(taskq_dispatch
);
578 taskq_dispatch_delay(taskq_t
*tq
, task_func_t func
, void *arg
,
579 uint_t flags
, clock_t expire_time
)
587 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
589 /* Taskq being destroyed and all tasks drained */
590 if (!(tq
->tq_flags
& TASKQ_ACTIVE
))
593 if ((t
= task_alloc(tq
, flags
)) == NULL
)
596 spin_lock(&t
->tqent_lock
);
598 /* Queue to the delay list for subsequent execution */
599 list_add_tail(&t
->tqent_list
, &tq
->tq_delay_list
);
601 t
->tqent_id
= rc
= tq
->tq_next_id
;
603 t
->tqent_func
= func
;
606 t
->tqent_timer
.data
= (unsigned long)t
;
607 t
->tqent_timer
.function
= task_expire
;
608 t
->tqent_timer
.expires
= (unsigned long)expire_time
;
609 add_timer(&t
->tqent_timer
);
611 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
613 spin_unlock(&t
->tqent_lock
);
615 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
618 EXPORT_SYMBOL(taskq_dispatch_delay
);
621 taskq_dispatch_ent(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
,
627 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
629 /* Taskq being destroyed and all tasks drained */
630 if (!(tq
->tq_flags
& TASKQ_ACTIVE
)) {
635 spin_lock(&t
->tqent_lock
);
638 * Mark it as a prealloc'd task. This is important
639 * to ensure that we don't free it later.
641 t
->tqent_flags
|= TQENT_FLAG_PREALLOC
;
643 /* Queue to the priority list instead of the pending list */
644 if (flags
& TQ_FRONT
)
645 list_add_tail(&t
->tqent_list
, &tq
->tq_prio_list
);
647 list_add_tail(&t
->tqent_list
, &tq
->tq_pend_list
);
649 t
->tqent_id
= tq
->tq_next_id
;
651 t
->tqent_func
= func
;
655 spin_unlock(&t
->tqent_lock
);
657 wake_up(&tq
->tq_work_waitq
);
659 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
661 EXPORT_SYMBOL(taskq_dispatch_ent
);
664 taskq_empty_ent(taskq_ent_t
*t
)
666 return list_empty(&t
->tqent_list
);
668 EXPORT_SYMBOL(taskq_empty_ent
);
671 taskq_init_ent(taskq_ent_t
*t
)
673 spin_lock_init(&t
->tqent_lock
);
674 init_waitqueue_head(&t
->tqent_waitq
);
675 init_timer(&t
->tqent_timer
);
676 INIT_LIST_HEAD(&t
->tqent_list
);
678 t
->tqent_func
= NULL
;
681 t
->tqent_taskq
= NULL
;
683 EXPORT_SYMBOL(taskq_init_ent
);
686 * Return the next pending task, preference is given to tasks on the
687 * priority list which were dispatched with TQ_FRONT.
690 taskq_next_ent(taskq_t
*tq
)
692 struct list_head
*list
;
694 ASSERT(spin_is_locked(&tq
->tq_lock
));
696 if (!list_empty(&tq
->tq_prio_list
))
697 list
= &tq
->tq_prio_list
;
698 else if (!list_empty(&tq
->tq_pend_list
))
699 list
= &tq
->tq_pend_list
;
703 return (list_entry(list
->next
, taskq_ent_t
, tqent_list
));
707 * Spawns a new thread for the specified taskq.
710 taskq_thread_spawn_task(void *arg
)
712 taskq_t
*tq
= (taskq_t
*)arg
;
714 (void) taskq_thread_create(tq
);
716 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
718 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
722 * Spawn addition threads for dynamic taskqs (TASKQ_DYNMAIC) the current
723 * number of threads is insufficient to handle the pending tasks. These
724 * new threads must be created by the dedicated dynamic_taskq to avoid
725 * deadlocks between thread creation and memory reclaim. The system_taskq
726 * which is also a dynamic taskq cannot be safely used for this.
729 taskq_thread_spawn(taskq_t
*tq
, int seq_tasks
)
733 if (!(tq
->tq_flags
& TASKQ_DYNAMIC
))
736 if ((seq_tasks
> spl_taskq_thread_sequential
) &&
737 (tq
->tq_nthreads
+ tq
->tq_nspawn
< tq
->tq_maxthreads
) &&
738 (tq
->tq_flags
& TASKQ_ACTIVE
)) {
739 spawning
= (++tq
->tq_nspawn
);
740 taskq_dispatch(dynamic_taskq
, taskq_thread_spawn_task
,
748 * Threads in a dynamic taskq should only exit once it has been completely
749 * drained and no other threads are actively servicing tasks. This prevents
750 * threads from being created and destroyed more than is required.
752 * The first thread is the thread list is treated as the primary thread.
753 * There is nothing special about the primary thread but in order to avoid
754 * all the taskq pids from changing we opt to make it long running.
757 taskq_thread_should_stop(taskq_t
*tq
, taskq_thread_t
*tqt
)
759 ASSERT(spin_is_locked(&tq
->tq_lock
));
761 if (!(tq
->tq_flags
& TASKQ_DYNAMIC
))
764 if (list_first_entry(&(tq
->tq_thread_list
), taskq_thread_t
,
765 tqt_thread_list
) == tqt
)
769 ((tq
->tq_nspawn
== 0) && /* No threads are being spawned */
770 (tq
->tq_nactive
== 0) && /* No threads are handling tasks */
771 (tq
->tq_nthreads
> 1) && /* More than 1 thread is running */
772 (!taskq_next_ent(tq
)) && /* There are no pending tasks */
773 (spl_taskq_thread_dynamic
));/* Dynamic taskqs are allowed */
777 taskq_thread(void *args
)
779 DECLARE_WAITQUEUE(wait
, current
);
781 taskq_thread_t
*tqt
= args
;
788 current
->flags
|= PF_NOFREEZE
;
790 sigfillset(&blocked
);
791 sigprocmask(SIG_BLOCK
, &blocked
, NULL
);
792 flush_signals(current
);
794 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
796 /* Immediately exit if more threads than allowed were created. */
797 if (tq
->tq_nthreads
>= tq
->tq_maxthreads
)
801 list_add_tail(&tqt
->tqt_thread_list
, &tq
->tq_thread_list
);
802 wake_up(&tq
->tq_wait_waitq
);
803 set_current_state(TASK_INTERRUPTIBLE
);
805 while (!kthread_should_stop()) {
807 if (list_empty(&tq
->tq_pend_list
) &&
808 list_empty(&tq
->tq_prio_list
)) {
810 if (taskq_thread_should_stop(tq
, tqt
)) {
811 wake_up_all(&tq
->tq_wait_waitq
);
815 add_wait_queue_exclusive(&tq
->tq_work_waitq
, &wait
);
816 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
821 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
822 remove_wait_queue(&tq
->tq_work_waitq
, &wait
);
824 __set_current_state(TASK_RUNNING
);
827 if ((t
= taskq_next_ent(tq
)) != NULL
) {
828 list_del_init(&t
->tqent_list
);
830 /* In order to support recursively dispatching a
831 * preallocated taskq_ent_t, tqent_id must be
832 * stored prior to executing tqent_func. */
833 tqt
->tqt_id
= t
->tqent_id
;
836 /* We must store a copy of the flags prior to
837 * servicing the task (servicing a prealloc'd task
838 * returns the ownership of the tqent back to
839 * the caller of taskq_dispatch). Thus,
840 * tqent_flags _may_ change within the call. */
841 tqt
->tqt_flags
= t
->tqent_flags
;
843 taskq_insert_in_order(tq
, tqt
);
845 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
847 /* Perform the requested task */
848 t
->tqent_func(t
->tqent_arg
);
850 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
852 list_del_init(&tqt
->tqt_active_list
);
853 tqt
->tqt_task
= NULL
;
855 /* For prealloc'd tasks, we don't free anything. */
856 if (!(tqt
->tqt_flags
& TQENT_FLAG_PREALLOC
))
859 /* When the current lowest outstanding taskqid is
860 * done calculate the new lowest outstanding id */
861 if (tq
->tq_lowest_id
== tqt
->tqt_id
) {
862 tq
->tq_lowest_id
= taskq_lowest_id(tq
);
863 ASSERT3S(tq
->tq_lowest_id
, >, tqt
->tqt_id
);
866 /* Spawn additional taskq threads if required. */
867 if (taskq_thread_spawn(tq
, ++seq_tasks
))
872 wake_up_all(&tq
->tq_wait_waitq
);
874 if (taskq_thread_should_stop(tq
, tqt
))
878 set_current_state(TASK_INTERRUPTIBLE
);
882 __set_current_state(TASK_RUNNING
);
884 list_del_init(&tqt
->tqt_thread_list
);
886 kmem_free(tqt
, sizeof (taskq_thread_t
));
887 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
892 static taskq_thread_t
*
893 taskq_thread_create(taskq_t
*tq
)
895 static int last_used_cpu
= 0;
898 tqt
= kmem_alloc(sizeof (*tqt
), KM_PUSHPAGE
);
899 INIT_LIST_HEAD(&tqt
->tqt_thread_list
);
900 INIT_LIST_HEAD(&tqt
->tqt_active_list
);
904 tqt
->tqt_thread
= spl_kthread_create(taskq_thread
, tqt
,
906 if (tqt
->tqt_thread
== NULL
) {
907 kmem_free(tqt
, sizeof (taskq_thread_t
));
911 if (spl_taskq_thread_bind
) {
912 last_used_cpu
= (last_used_cpu
+ 1) % num_online_cpus();
913 kthread_bind(tqt
->tqt_thread
, last_used_cpu
);
916 set_user_nice(tqt
->tqt_thread
, PRIO_TO_NICE(tq
->tq_pri
));
917 wake_up_process(tqt
->tqt_thread
);
923 taskq_create(const char *name
, int nthreads
, pri_t pri
,
924 int minalloc
, int maxalloc
, uint_t flags
)
928 int count
= 0, rc
= 0, i
;
930 ASSERT(name
!= NULL
);
931 ASSERT(pri
<= maxclsyspri
);
932 ASSERT(minalloc
>= 0);
933 ASSERT(maxalloc
<= INT_MAX
);
934 ASSERT(!(flags
& (TASKQ_CPR_SAFE
))); /* Unsupported */
936 /* Scale the number of threads using nthreads as a percentage */
937 if (flags
& TASKQ_THREADS_CPU_PCT
) {
938 ASSERT(nthreads
<= 100);
939 ASSERT(nthreads
>= 0);
940 nthreads
= MIN(nthreads
, 100);
941 nthreads
= MAX(nthreads
, 0);
942 nthreads
= MAX((num_online_cpus() * nthreads
) / 100, 1);
945 tq
= kmem_alloc(sizeof (*tq
), KM_PUSHPAGE
);
949 spin_lock_init(&tq
->tq_lock
);
950 INIT_LIST_HEAD(&tq
->tq_thread_list
);
951 INIT_LIST_HEAD(&tq
->tq_active_list
);
952 tq
->tq_name
= strdup(name
);
956 tq
->tq_maxthreads
= nthreads
;
958 tq
->tq_minalloc
= minalloc
;
959 tq
->tq_maxalloc
= maxalloc
;
961 tq
->tq_flags
= (flags
| TASKQ_ACTIVE
);
963 tq
->tq_lowest_id
= 1;
964 INIT_LIST_HEAD(&tq
->tq_free_list
);
965 INIT_LIST_HEAD(&tq
->tq_pend_list
);
966 INIT_LIST_HEAD(&tq
->tq_prio_list
);
967 INIT_LIST_HEAD(&tq
->tq_delay_list
);
968 init_waitqueue_head(&tq
->tq_work_waitq
);
969 init_waitqueue_head(&tq
->tq_wait_waitq
);
971 if (flags
& TASKQ_PREPOPULATE
) {
972 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
974 for (i
= 0; i
< minalloc
; i
++)
975 task_done(tq
, task_alloc(tq
, TQ_PUSHPAGE
| TQ_NEW
));
977 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
980 if ((flags
& TASKQ_DYNAMIC
) && spl_taskq_thread_dynamic
)
983 for (i
= 0; i
< nthreads
; i
++) {
984 tqt
= taskq_thread_create(tq
);
991 /* Wait for all threads to be started before potential destroy */
992 wait_event(tq
->tq_wait_waitq
, tq
->tq_nthreads
== count
);
1001 EXPORT_SYMBOL(taskq_create
);
1004 taskq_destroy(taskq_t
*tq
)
1006 struct task_struct
*thread
;
1007 taskq_thread_t
*tqt
;
1011 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
1012 tq
->tq_flags
&= ~TASKQ_ACTIVE
;
1013 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
1016 * When TASKQ_ACTIVE is clear new tasks may not be added nor may
1017 * new worker threads be spawned for dynamic taskq.
1019 if (dynamic_taskq
!= NULL
)
1020 taskq_wait_outstanding(dynamic_taskq
, 0);
1024 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
1027 * Signal each thread to exit and block until it does. Each thread
1028 * is responsible for removing itself from the list and freeing its
1029 * taskq_thread_t. This allows for idle threads to opt to remove
1030 * themselves from the taskq. They can be recreated as needed.
1032 while (!list_empty(&tq
->tq_thread_list
)) {
1033 tqt
= list_entry(tq
->tq_thread_list
.next
,
1034 taskq_thread_t
, tqt_thread_list
);
1035 thread
= tqt
->tqt_thread
;
1036 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
1038 kthread_stop(thread
);
1040 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
1043 while (!list_empty(&tq
->tq_free_list
)) {
1044 t
= list_entry(tq
->tq_free_list
.next
, taskq_ent_t
, tqent_list
);
1046 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
1048 list_del_init(&t
->tqent_list
);
1052 ASSERT0(tq
->tq_nthreads
);
1053 ASSERT0(tq
->tq_nalloc
);
1054 ASSERT0(tq
->tq_nspawn
);
1055 ASSERT(list_empty(&tq
->tq_thread_list
));
1056 ASSERT(list_empty(&tq
->tq_active_list
));
1057 ASSERT(list_empty(&tq
->tq_free_list
));
1058 ASSERT(list_empty(&tq
->tq_pend_list
));
1059 ASSERT(list_empty(&tq
->tq_prio_list
));
1060 ASSERT(list_empty(&tq
->tq_delay_list
));
1062 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
1064 strfree(tq
->tq_name
);
1065 kmem_free(tq
, sizeof (taskq_t
));
1067 EXPORT_SYMBOL(taskq_destroy
);
1070 spl_taskq_init(void)
1072 system_taskq
= taskq_create("spl_system_taskq", MAX(boot_ncpus
, 64),
1073 minclsyspri
, boot_ncpus
, INT_MAX
, TASKQ_PREPOPULATE
|TASKQ_DYNAMIC
);
1074 if (system_taskq
== NULL
)
1077 dynamic_taskq
= taskq_create("spl_dynamic_taskq", 1,
1078 minclsyspri
, boot_ncpus
, INT_MAX
, TASKQ_PREPOPULATE
);
1079 if (dynamic_taskq
== NULL
) {
1080 taskq_destroy(system_taskq
);
1088 spl_taskq_fini(void)
1090 taskq_destroy(dynamic_taskq
);
1091 dynamic_taskq
= NULL
;
1093 taskq_destroy(system_taskq
);
1094 system_taskq
= NULL
;