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_priority
= 1;
40 module_param(spl_taskq_thread_priority
, int, 0644);
41 MODULE_PARM_DESC(spl_taskq_thread_priority
,
42 "Allow non-default priority for taskq threads");
44 int spl_taskq_thread_sequential
= 4;
45 module_param(spl_taskq_thread_sequential
, int, 0644);
46 MODULE_PARM_DESC(spl_taskq_thread_sequential
,
47 "Create new taskq threads after N sequential tasks");
49 /* Global system-wide dynamic task queue available for all consumers */
50 taskq_t
*system_taskq
;
51 EXPORT_SYMBOL(system_taskq
);
53 /* Private dedicated taskq for creating new taskq threads on demand. */
54 static taskq_t
*dynamic_taskq
;
55 static taskq_thread_t
*taskq_thread_create(taskq_t
*);
58 task_km_flags(uint_t flags
)
60 if (flags
& TQ_NOSLEEP
)
63 if (flags
& TQ_PUSHPAGE
)
70 * NOTE: Must be called with tq->tq_lock held, returns a list_t which
71 * is not attached to the free, work, or pending taskq lists.
74 task_alloc(taskq_t
*tq
, uint_t flags
)
80 ASSERT(spin_is_locked(&tq
->tq_lock
));
82 /* Acquire taskq_ent_t's from free list if available */
83 if (!list_empty(&tq
->tq_free_list
) && !(flags
& TQ_NEW
)) {
84 t
= list_entry(tq
->tq_free_list
.next
, taskq_ent_t
, tqent_list
);
86 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
87 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_CANCEL
));
88 ASSERT(!timer_pending(&t
->tqent_timer
));
90 list_del_init(&t
->tqent_list
);
94 /* Free list is empty and memory allocations are prohibited */
95 if (flags
& TQ_NOALLOC
)
98 /* Hit maximum taskq_ent_t pool size */
99 if (tq
->tq_nalloc
>= tq
->tq_maxalloc
) {
100 if (flags
& TQ_NOSLEEP
)
104 * Sleep periodically polling the free list for an available
105 * taskq_ent_t. Dispatching with TQ_SLEEP should always succeed
106 * but we cannot block forever waiting for an taskq_ent_t to
107 * show up in the free list, otherwise a deadlock can happen.
109 * Therefore, we need to allocate a new task even if the number
110 * of allocated tasks is above tq->tq_maxalloc, but we still
111 * end up delaying the task allocation by one second, thereby
112 * throttling the task dispatch rate.
114 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
115 schedule_timeout(HZ
/ 100);
116 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
123 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
124 t
= kmem_alloc(sizeof(taskq_ent_t
), task_km_flags(flags
));
125 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
136 * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t
137 * to already be removed from the free, work, or pending taskq lists.
140 task_free(taskq_t
*tq
, taskq_ent_t
*t
)
144 ASSERT(spin_is_locked(&tq
->tq_lock
));
145 ASSERT(list_empty(&t
->tqent_list
));
146 ASSERT(!timer_pending(&t
->tqent_timer
));
148 kmem_free(t
, sizeof(taskq_ent_t
));
153 * NOTE: Must be called with tq->tq_lock held, either destroys the
154 * taskq_ent_t if too many exist or moves it to the free list for later use.
157 task_done(taskq_t
*tq
, taskq_ent_t
*t
)
161 ASSERT(spin_is_locked(&tq
->tq_lock
));
163 /* Wake tasks blocked in taskq_wait_id() */
164 wake_up_all(&t
->tqent_waitq
);
166 list_del_init(&t
->tqent_list
);
168 if (tq
->tq_nalloc
<= tq
->tq_minalloc
) {
170 t
->tqent_func
= NULL
;
174 list_add_tail(&t
->tqent_list
, &tq
->tq_free_list
);
181 * When a delayed task timer expires remove it from the delay list and
182 * add it to the priority list in order for immediate processing.
185 task_expire(unsigned long data
)
187 taskq_ent_t
*w
, *t
= (taskq_ent_t
*)data
;
188 taskq_t
*tq
= t
->tqent_taskq
;
191 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
193 if (t
->tqent_flags
& TQENT_FLAG_CANCEL
) {
194 ASSERT(list_empty(&t
->tqent_list
));
195 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
200 * The priority list must be maintained in strict task id order
201 * from lowest to highest for lowest_id to be easily calculable.
203 list_del(&t
->tqent_list
);
204 list_for_each_prev(l
, &tq
->tq_prio_list
) {
205 w
= list_entry(l
, taskq_ent_t
, tqent_list
);
206 if (w
->tqent_id
< t
->tqent_id
) {
207 list_add(&t
->tqent_list
, l
);
211 if (l
== &tq
->tq_prio_list
)
212 list_add(&t
->tqent_list
, &tq
->tq_prio_list
);
214 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
216 wake_up(&tq
->tq_work_waitq
);
220 * Returns the lowest incomplete taskqid_t. The taskqid_t may
221 * be queued on the pending list, on the priority list, on the
222 * delay list, or on the work list currently being handled, but
223 * it is not 100% complete yet.
226 taskq_lowest_id(taskq_t
*tq
)
228 taskqid_t lowest_id
= tq
->tq_next_id
;
233 ASSERT(spin_is_locked(&tq
->tq_lock
));
235 if (!list_empty(&tq
->tq_pend_list
)) {
236 t
= list_entry(tq
->tq_pend_list
.next
, taskq_ent_t
, tqent_list
);
237 lowest_id
= MIN(lowest_id
, t
->tqent_id
);
240 if (!list_empty(&tq
->tq_prio_list
)) {
241 t
= list_entry(tq
->tq_prio_list
.next
, taskq_ent_t
, tqent_list
);
242 lowest_id
= MIN(lowest_id
, t
->tqent_id
);
245 if (!list_empty(&tq
->tq_delay_list
)) {
246 t
= list_entry(tq
->tq_delay_list
.next
, taskq_ent_t
, tqent_list
);
247 lowest_id
= MIN(lowest_id
, t
->tqent_id
);
250 if (!list_empty(&tq
->tq_active_list
)) {
251 tqt
= list_entry(tq
->tq_active_list
.next
, taskq_thread_t
,
253 ASSERT(tqt
->tqt_id
!= 0);
254 lowest_id
= MIN(lowest_id
, tqt
->tqt_id
);
261 * Insert a task into a list keeping the list sorted by increasing taskqid.
264 taskq_insert_in_order(taskq_t
*tq
, taskq_thread_t
*tqt
)
271 ASSERT(spin_is_locked(&tq
->tq_lock
));
273 list_for_each_prev(l
, &tq
->tq_active_list
) {
274 w
= list_entry(l
, taskq_thread_t
, tqt_active_list
);
275 if (w
->tqt_id
< tqt
->tqt_id
) {
276 list_add(&tqt
->tqt_active_list
, l
);
280 if (l
== &tq
->tq_active_list
)
281 list_add(&tqt
->tqt_active_list
, &tq
->tq_active_list
);
285 * Find and return a task from the given list if it exists. The list
286 * must be in lowest to highest task id order.
289 taskq_find_list(taskq_t
*tq
, struct list_head
*lh
, taskqid_t id
)
294 ASSERT(spin_is_locked(&tq
->tq_lock
));
296 list_for_each(l
, lh
) {
297 t
= list_entry(l
, taskq_ent_t
, tqent_list
);
299 if (t
->tqent_id
== id
)
302 if (t
->tqent_id
> id
)
310 * Find an already dispatched task given the task id regardless of what
311 * state it is in. If a task is still pending or executing it will be
312 * returned and 'active' set appropriately. If the task has already
313 * been run then NULL is returned.
316 taskq_find(taskq_t
*tq
, taskqid_t id
, int *active
)
322 ASSERT(spin_is_locked(&tq
->tq_lock
));
325 t
= taskq_find_list(tq
, &tq
->tq_delay_list
, id
);
329 t
= taskq_find_list(tq
, &tq
->tq_prio_list
, id
);
333 t
= taskq_find_list(tq
, &tq
->tq_pend_list
, id
);
337 list_for_each(l
, &tq
->tq_active_list
) {
338 tqt
= list_entry(l
, taskq_thread_t
, tqt_active_list
);
339 if (tqt
->tqt_id
== id
) {
350 * Theory for the taskq_wait_id(), taskq_wait_outstanding(), and
351 * taskq_wait() functions below.
353 * Taskq waiting is accomplished by tracking the lowest outstanding task
354 * id and the next available task id. As tasks are dispatched they are
355 * added to the tail of the pending, priority, or delay lists. As worker
356 * threads become available the tasks are removed from the heads of these
357 * lists and linked to the worker threads. This ensures the lists are
358 * kept sorted by lowest to highest task id.
360 * Therefore the lowest outstanding task id can be quickly determined by
361 * checking the head item from all of these lists. This value is stored
362 * with the taskq as the lowest id. It only needs to be recalculated when
363 * either the task with the current lowest id completes or is canceled.
365 * By blocking until the lowest task id exceeds the passed task id the
366 * taskq_wait_outstanding() function can be easily implemented. Similarly,
367 * by blocking until the lowest task id matches the next task id taskq_wait()
368 * can be implemented.
370 * Callers should be aware that when there are multiple worked threads it
371 * is possible for larger task ids to complete before smaller ones. Also
372 * when the taskq contains delay tasks with small task ids callers may
373 * block for a considerable length of time waiting for them to expire and
377 taskq_wait_id_check(taskq_t
*tq
, taskqid_t id
)
382 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
383 rc
= (taskq_find(tq
, id
, &active
) == NULL
);
384 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
390 * The taskq_wait_id() function blocks until the passed task id completes.
391 * This does not guarantee that all lower task ids have completed.
394 taskq_wait_id(taskq_t
*tq
, taskqid_t id
)
396 wait_event(tq
->tq_wait_waitq
, taskq_wait_id_check(tq
, id
));
398 EXPORT_SYMBOL(taskq_wait_id
);
401 taskq_wait_outstanding_check(taskq_t
*tq
, taskqid_t id
)
405 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
406 rc
= (id
< tq
->tq_lowest_id
);
407 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
413 * The taskq_wait_outstanding() function will block until all tasks with a
414 * lower taskqid than the passed 'id' have been completed. Note that all
415 * task id's are assigned monotonically at dispatch time. Zero may be
416 * passed for the id to indicate all tasks dispatch up to this point,
417 * but not after, should be waited for.
420 taskq_wait_outstanding(taskq_t
*tq
, taskqid_t id
)
422 wait_event(tq
->tq_wait_waitq
,
423 taskq_wait_outstanding_check(tq
, id
? id
: tq
->tq_next_id
- 1));
425 EXPORT_SYMBOL(taskq_wait_outstanding
);
428 taskq_wait_check(taskq_t
*tq
)
432 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
433 rc
= (tq
->tq_lowest_id
== tq
->tq_next_id
);
434 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
440 * The taskq_wait() function will block until the taskq is empty.
441 * This means that if a taskq re-dispatches work to itself taskq_wait()
442 * callers will block indefinitely.
445 taskq_wait(taskq_t
*tq
)
447 wait_event(tq
->tq_wait_waitq
, taskq_wait_check(tq
));
449 EXPORT_SYMBOL(taskq_wait
);
452 taskq_member_impl(taskq_t
*tq
, void *t
)
460 ASSERT(spin_is_locked(&tq
->tq_lock
));
462 list_for_each(l
, &tq
->tq_thread_list
) {
463 tqt
= list_entry(l
, taskq_thread_t
, tqt_thread_list
);
464 if (tqt
->tqt_thread
== (struct task_struct
*)t
) {
473 taskq_member(taskq_t
*tq
, void *t
)
477 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
478 found
= taskq_member_impl(tq
, t
);
479 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
483 EXPORT_SYMBOL(taskq_member
);
486 * Cancel an already dispatched task given the task id. Still pending tasks
487 * will be immediately canceled, and if the task is active the function will
488 * block until it completes. Preallocated tasks which are canceled must be
489 * freed by the caller.
492 taskq_cancel_id(taskq_t
*tq
, taskqid_t id
)
500 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
501 t
= taskq_find(tq
, id
, &active
);
503 list_del_init(&t
->tqent_list
);
504 t
->tqent_flags
|= TQENT_FLAG_CANCEL
;
507 * When canceling the lowest outstanding task id we
508 * must recalculate the new lowest outstanding id.
510 if (tq
->tq_lowest_id
== t
->tqent_id
) {
511 tq
->tq_lowest_id
= taskq_lowest_id(tq
);
512 ASSERT3S(tq
->tq_lowest_id
, >, t
->tqent_id
);
516 * The task_expire() function takes the tq->tq_lock so drop
517 * drop the lock before synchronously cancelling the timer.
519 if (timer_pending(&t
->tqent_timer
)) {
520 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
521 del_timer_sync(&t
->tqent_timer
);
522 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
525 if (!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
))
530 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
533 taskq_wait_id(tq
, id
);
539 EXPORT_SYMBOL(taskq_cancel_id
);
541 static int taskq_thread_spawn(taskq_t
*tq
, int seq_tasks
);
544 taskq_dispatch(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
)
552 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
554 /* Taskq being destroyed and all tasks drained */
555 if (!(tq
->tq_flags
& TASKQ_ACTIVE
))
558 /* Do not queue the task unless there is idle thread for it */
559 ASSERT(tq
->tq_nactive
<= tq
->tq_nthreads
);
560 if ((flags
& TQ_NOQUEUE
) && (tq
->tq_nactive
== tq
->tq_nthreads
))
563 if ((t
= task_alloc(tq
, flags
)) == NULL
)
566 spin_lock(&t
->tqent_lock
);
568 /* Queue to the priority list instead of the pending list */
569 if (flags
& TQ_FRONT
)
570 list_add_tail(&t
->tqent_list
, &tq
->tq_prio_list
);
572 list_add_tail(&t
->tqent_list
, &tq
->tq_pend_list
);
574 t
->tqent_id
= rc
= tq
->tq_next_id
;
576 t
->tqent_func
= func
;
579 t
->tqent_timer
.data
= 0;
580 t
->tqent_timer
.function
= NULL
;
581 t
->tqent_timer
.expires
= 0;
583 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
585 spin_unlock(&t
->tqent_lock
);
587 wake_up(&tq
->tq_work_waitq
);
589 /* Spawn additional taskq threads if required. */
590 if (tq
->tq_nactive
== tq
->tq_nthreads
&&
591 taskq_member_impl(tq
, current
))
592 (void) taskq_thread_spawn(tq
, spl_taskq_thread_sequential
+ 1);
594 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
597 EXPORT_SYMBOL(taskq_dispatch
);
600 taskq_dispatch_delay(taskq_t
*tq
, task_func_t func
, void *arg
,
601 uint_t flags
, clock_t expire_time
)
609 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
611 /* Taskq being destroyed and all tasks drained */
612 if (!(tq
->tq_flags
& TASKQ_ACTIVE
))
615 if ((t
= task_alloc(tq
, flags
)) == NULL
)
618 spin_lock(&t
->tqent_lock
);
620 /* Queue to the delay list for subsequent execution */
621 list_add_tail(&t
->tqent_list
, &tq
->tq_delay_list
);
623 t
->tqent_id
= rc
= tq
->tq_next_id
;
625 t
->tqent_func
= func
;
628 t
->tqent_timer
.data
= (unsigned long)t
;
629 t
->tqent_timer
.function
= task_expire
;
630 t
->tqent_timer
.expires
= (unsigned long)expire_time
;
631 add_timer(&t
->tqent_timer
);
633 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
635 spin_unlock(&t
->tqent_lock
);
637 /* Spawn additional taskq threads if required. */
638 if (tq
->tq_nactive
== tq
->tq_nthreads
&&
639 taskq_member_impl(tq
, current
))
640 (void) taskq_thread_spawn(tq
, spl_taskq_thread_sequential
+ 1);
641 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
644 EXPORT_SYMBOL(taskq_dispatch_delay
);
647 taskq_dispatch_ent(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
,
653 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
655 /* Taskq being destroyed and all tasks drained */
656 if (!(tq
->tq_flags
& TASKQ_ACTIVE
)) {
661 spin_lock(&t
->tqent_lock
);
664 * Mark it as a prealloc'd task. This is important
665 * to ensure that we don't free it later.
667 t
->tqent_flags
|= TQENT_FLAG_PREALLOC
;
669 /* Queue to the priority list instead of the pending list */
670 if (flags
& TQ_FRONT
)
671 list_add_tail(&t
->tqent_list
, &tq
->tq_prio_list
);
673 list_add_tail(&t
->tqent_list
, &tq
->tq_pend_list
);
675 t
->tqent_id
= tq
->tq_next_id
;
677 t
->tqent_func
= func
;
681 spin_unlock(&t
->tqent_lock
);
683 wake_up(&tq
->tq_work_waitq
);
685 /* Spawn additional taskq threads if required. */
686 if (tq
->tq_nactive
== tq
->tq_nthreads
&&
687 taskq_member_impl(tq
, current
))
688 (void) taskq_thread_spawn(tq
, spl_taskq_thread_sequential
+ 1);
689 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
691 EXPORT_SYMBOL(taskq_dispatch_ent
);
694 taskq_empty_ent(taskq_ent_t
*t
)
696 return list_empty(&t
->tqent_list
);
698 EXPORT_SYMBOL(taskq_empty_ent
);
701 taskq_init_ent(taskq_ent_t
*t
)
703 spin_lock_init(&t
->tqent_lock
);
704 init_waitqueue_head(&t
->tqent_waitq
);
705 init_timer(&t
->tqent_timer
);
706 INIT_LIST_HEAD(&t
->tqent_list
);
708 t
->tqent_func
= NULL
;
711 t
->tqent_taskq
= NULL
;
713 EXPORT_SYMBOL(taskq_init_ent
);
716 * Return the next pending task, preference is given to tasks on the
717 * priority list which were dispatched with TQ_FRONT.
720 taskq_next_ent(taskq_t
*tq
)
722 struct list_head
*list
;
724 ASSERT(spin_is_locked(&tq
->tq_lock
));
726 if (!list_empty(&tq
->tq_prio_list
))
727 list
= &tq
->tq_prio_list
;
728 else if (!list_empty(&tq
->tq_pend_list
))
729 list
= &tq
->tq_pend_list
;
733 return (list_entry(list
->next
, taskq_ent_t
, tqent_list
));
737 * Spawns a new thread for the specified taskq.
740 taskq_thread_spawn_task(void *arg
)
742 taskq_t
*tq
= (taskq_t
*)arg
;
744 (void) taskq_thread_create(tq
);
746 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
748 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
752 * Spawn addition threads for dynamic taskqs (TASKQ_DYNMAIC) the current
753 * number of threads is insufficient to handle the pending tasks. These
754 * new threads must be created by the dedicated dynamic_taskq to avoid
755 * deadlocks between thread creation and memory reclaim. The system_taskq
756 * which is also a dynamic taskq cannot be safely used for this.
759 taskq_thread_spawn(taskq_t
*tq
, int seq_tasks
)
763 if (!(tq
->tq_flags
& TASKQ_DYNAMIC
))
766 if ((seq_tasks
> spl_taskq_thread_sequential
) &&
767 (tq
->tq_nthreads
+ tq
->tq_nspawn
< tq
->tq_maxthreads
) &&
768 (tq
->tq_flags
& TASKQ_ACTIVE
)) {
769 spawning
= (++tq
->tq_nspawn
);
770 taskq_dispatch(dynamic_taskq
, taskq_thread_spawn_task
,
778 * Threads in a dynamic taskq should only exit once it has been completely
779 * drained and no other threads are actively servicing tasks. This prevents
780 * threads from being created and destroyed more than is required.
782 * The first thread is the thread list is treated as the primary thread.
783 * There is nothing special about the primary thread but in order to avoid
784 * all the taskq pids from changing we opt to make it long running.
787 taskq_thread_should_stop(taskq_t
*tq
, taskq_thread_t
*tqt
)
789 ASSERT(spin_is_locked(&tq
->tq_lock
));
791 if (!(tq
->tq_flags
& TASKQ_DYNAMIC
))
794 if (list_first_entry(&(tq
->tq_thread_list
), taskq_thread_t
,
795 tqt_thread_list
) == tqt
)
799 ((tq
->tq_nspawn
== 0) && /* No threads are being spawned */
800 (tq
->tq_nactive
== 0) && /* No threads are handling tasks */
801 (tq
->tq_nthreads
> 1) && /* More than 1 thread is running */
802 (!taskq_next_ent(tq
)) && /* There are no pending tasks */
803 (spl_taskq_thread_dynamic
));/* Dynamic taskqs are allowed */
807 taskq_thread(void *args
)
809 DECLARE_WAITQUEUE(wait
, current
);
811 taskq_thread_t
*tqt
= args
;
818 current
->flags
|= PF_NOFREEZE
;
820 #if defined(PF_MEMALLOC_NOIO)
821 (void) memalloc_noio_save();
824 sigfillset(&blocked
);
825 sigprocmask(SIG_BLOCK
, &blocked
, NULL
);
826 flush_signals(current
);
828 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
830 /* Immediately exit if more threads than allowed were created. */
831 if (tq
->tq_nthreads
>= tq
->tq_maxthreads
)
835 list_add_tail(&tqt
->tqt_thread_list
, &tq
->tq_thread_list
);
836 wake_up(&tq
->tq_wait_waitq
);
837 set_current_state(TASK_INTERRUPTIBLE
);
839 while (!kthread_should_stop()) {
841 if (list_empty(&tq
->tq_pend_list
) &&
842 list_empty(&tq
->tq_prio_list
)) {
844 if (taskq_thread_should_stop(tq
, tqt
)) {
845 wake_up_all(&tq
->tq_wait_waitq
);
849 add_wait_queue_exclusive(&tq
->tq_work_waitq
, &wait
);
850 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
855 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
856 remove_wait_queue(&tq
->tq_work_waitq
, &wait
);
858 __set_current_state(TASK_RUNNING
);
861 if ((t
= taskq_next_ent(tq
)) != NULL
) {
862 list_del_init(&t
->tqent_list
);
864 /* In order to support recursively dispatching a
865 * preallocated taskq_ent_t, tqent_id must be
866 * stored prior to executing tqent_func. */
867 tqt
->tqt_id
= t
->tqent_id
;
870 /* We must store a copy of the flags prior to
871 * servicing the task (servicing a prealloc'd task
872 * returns the ownership of the tqent back to
873 * the caller of taskq_dispatch). Thus,
874 * tqent_flags _may_ change within the call. */
875 tqt
->tqt_flags
= t
->tqent_flags
;
877 taskq_insert_in_order(tq
, tqt
);
879 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
881 /* Perform the requested task */
882 t
->tqent_func(t
->tqent_arg
);
884 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
886 list_del_init(&tqt
->tqt_active_list
);
887 tqt
->tqt_task
= NULL
;
889 /* For prealloc'd tasks, we don't free anything. */
890 if (!(tqt
->tqt_flags
& TQENT_FLAG_PREALLOC
))
893 /* When the current lowest outstanding taskqid is
894 * done calculate the new lowest outstanding id */
895 if (tq
->tq_lowest_id
== tqt
->tqt_id
) {
896 tq
->tq_lowest_id
= taskq_lowest_id(tq
);
897 ASSERT3S(tq
->tq_lowest_id
, >, tqt
->tqt_id
);
900 /* Spawn additional taskq threads if required. */
901 if (taskq_thread_spawn(tq
, ++seq_tasks
))
906 wake_up_all(&tq
->tq_wait_waitq
);
908 if (taskq_thread_should_stop(tq
, tqt
))
912 set_current_state(TASK_INTERRUPTIBLE
);
916 __set_current_state(TASK_RUNNING
);
918 list_del_init(&tqt
->tqt_thread_list
);
920 kmem_free(tqt
, sizeof (taskq_thread_t
));
921 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
926 static taskq_thread_t
*
927 taskq_thread_create(taskq_t
*tq
)
929 static int last_used_cpu
= 0;
932 tqt
= kmem_alloc(sizeof (*tqt
), KM_PUSHPAGE
);
933 INIT_LIST_HEAD(&tqt
->tqt_thread_list
);
934 INIT_LIST_HEAD(&tqt
->tqt_active_list
);
938 tqt
->tqt_thread
= spl_kthread_create(taskq_thread
, tqt
,
940 if (tqt
->tqt_thread
== NULL
) {
941 kmem_free(tqt
, sizeof (taskq_thread_t
));
945 if (spl_taskq_thread_bind
) {
946 last_used_cpu
= (last_used_cpu
+ 1) % num_online_cpus();
947 kthread_bind(tqt
->tqt_thread
, last_used_cpu
);
950 if (spl_taskq_thread_priority
)
951 set_user_nice(tqt
->tqt_thread
, PRIO_TO_NICE(tq
->tq_pri
));
953 wake_up_process(tqt
->tqt_thread
);
959 taskq_create(const char *name
, int nthreads
, pri_t pri
,
960 int minalloc
, int maxalloc
, uint_t flags
)
964 int count
= 0, rc
= 0, i
;
966 ASSERT(name
!= NULL
);
967 ASSERT(minalloc
>= 0);
968 ASSERT(maxalloc
<= INT_MAX
);
969 ASSERT(!(flags
& (TASKQ_CPR_SAFE
))); /* Unsupported */
971 /* Scale the number of threads using nthreads as a percentage */
972 if (flags
& TASKQ_THREADS_CPU_PCT
) {
973 ASSERT(nthreads
<= 100);
974 ASSERT(nthreads
>= 0);
975 nthreads
= MIN(nthreads
, 100);
976 nthreads
= MAX(nthreads
, 0);
977 nthreads
= MAX((num_online_cpus() * nthreads
) / 100, 1);
980 tq
= kmem_alloc(sizeof (*tq
), KM_PUSHPAGE
);
984 spin_lock_init(&tq
->tq_lock
);
985 INIT_LIST_HEAD(&tq
->tq_thread_list
);
986 INIT_LIST_HEAD(&tq
->tq_active_list
);
987 tq
->tq_name
= strdup(name
);
991 tq
->tq_maxthreads
= nthreads
;
993 tq
->tq_minalloc
= minalloc
;
994 tq
->tq_maxalloc
= maxalloc
;
996 tq
->tq_flags
= (flags
| TASKQ_ACTIVE
);
998 tq
->tq_lowest_id
= 1;
999 INIT_LIST_HEAD(&tq
->tq_free_list
);
1000 INIT_LIST_HEAD(&tq
->tq_pend_list
);
1001 INIT_LIST_HEAD(&tq
->tq_prio_list
);
1002 INIT_LIST_HEAD(&tq
->tq_delay_list
);
1003 init_waitqueue_head(&tq
->tq_work_waitq
);
1004 init_waitqueue_head(&tq
->tq_wait_waitq
);
1006 if (flags
& TASKQ_PREPOPULATE
) {
1007 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
1009 for (i
= 0; i
< minalloc
; i
++)
1010 task_done(tq
, task_alloc(tq
, TQ_PUSHPAGE
| TQ_NEW
));
1012 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
1015 if ((flags
& TASKQ_DYNAMIC
) && spl_taskq_thread_dynamic
)
1018 for (i
= 0; i
< nthreads
; i
++) {
1019 tqt
= taskq_thread_create(tq
);
1026 /* Wait for all threads to be started before potential destroy */
1027 wait_event(tq
->tq_wait_waitq
, tq
->tq_nthreads
== count
);
1036 EXPORT_SYMBOL(taskq_create
);
1039 taskq_destroy(taskq_t
*tq
)
1041 struct task_struct
*thread
;
1042 taskq_thread_t
*tqt
;
1046 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
1047 tq
->tq_flags
&= ~TASKQ_ACTIVE
;
1048 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
1051 * When TASKQ_ACTIVE is clear new tasks may not be added nor may
1052 * new worker threads be spawned for dynamic taskq.
1054 if (dynamic_taskq
!= NULL
)
1055 taskq_wait_outstanding(dynamic_taskq
, 0);
1059 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
1062 * Signal each thread to exit and block until it does. Each thread
1063 * is responsible for removing itself from the list and freeing its
1064 * taskq_thread_t. This allows for idle threads to opt to remove
1065 * themselves from the taskq. They can be recreated as needed.
1067 while (!list_empty(&tq
->tq_thread_list
)) {
1068 tqt
= list_entry(tq
->tq_thread_list
.next
,
1069 taskq_thread_t
, tqt_thread_list
);
1070 thread
= tqt
->tqt_thread
;
1071 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
1073 kthread_stop(thread
);
1075 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
1078 while (!list_empty(&tq
->tq_free_list
)) {
1079 t
= list_entry(tq
->tq_free_list
.next
, taskq_ent_t
, tqent_list
);
1081 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
1083 list_del_init(&t
->tqent_list
);
1087 ASSERT0(tq
->tq_nthreads
);
1088 ASSERT0(tq
->tq_nalloc
);
1089 ASSERT0(tq
->tq_nspawn
);
1090 ASSERT(list_empty(&tq
->tq_thread_list
));
1091 ASSERT(list_empty(&tq
->tq_active_list
));
1092 ASSERT(list_empty(&tq
->tq_free_list
));
1093 ASSERT(list_empty(&tq
->tq_pend_list
));
1094 ASSERT(list_empty(&tq
->tq_prio_list
));
1095 ASSERT(list_empty(&tq
->tq_delay_list
));
1097 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
1099 strfree(tq
->tq_name
);
1100 kmem_free(tq
, sizeof (taskq_t
));
1102 EXPORT_SYMBOL(taskq_destroy
);
1105 spl_taskq_init(void)
1107 system_taskq
= taskq_create("spl_system_taskq", MAX(boot_ncpus
, 64),
1108 maxclsyspri
, boot_ncpus
, INT_MAX
, TASKQ_PREPOPULATE
|TASKQ_DYNAMIC
);
1109 if (system_taskq
== NULL
)
1112 dynamic_taskq
= taskq_create("spl_dynamic_taskq", 1,
1113 maxclsyspri
, boot_ncpus
, INT_MAX
, TASKQ_PREPOPULATE
);
1114 if (dynamic_taskq
== NULL
) {
1115 taskq_destroy(system_taskq
);
1123 spl_taskq_fini(void)
1125 taskq_destroy(dynamic_taskq
);
1126 dynamic_taskq
= NULL
;
1128 taskq_destroy(system_taskq
);
1129 system_taskq
= NULL
;