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/>.
24 * Solaris Porting Layer (SPL) Task Queue Implementation.
27 #include <sys/taskq.h>
31 int spl_taskq_thread_bind
= 0;
32 module_param(spl_taskq_thread_bind
, int, 0644);
33 MODULE_PARM_DESC(spl_taskq_thread_bind
, "Bind taskq thread to CPU by default");
36 int spl_taskq_thread_dynamic
= 1;
37 module_param(spl_taskq_thread_dynamic
, int, 0644);
38 MODULE_PARM_DESC(spl_taskq_thread_dynamic
, "Allow dynamic taskq threads");
40 int spl_taskq_thread_priority
= 1;
41 module_param(spl_taskq_thread_priority
, int, 0644);
42 MODULE_PARM_DESC(spl_taskq_thread_priority
,
43 "Allow non-default priority for taskq threads");
45 int spl_taskq_thread_sequential
= 4;
46 module_param(spl_taskq_thread_sequential
, int, 0644);
47 MODULE_PARM_DESC(spl_taskq_thread_sequential
,
48 "Create new taskq threads after N sequential tasks");
50 /* Global system-wide dynamic task queue available for all consumers */
51 taskq_t
*system_taskq
;
52 EXPORT_SYMBOL(system_taskq
);
54 /* Private dedicated taskq for creating new taskq threads on demand. */
55 static taskq_t
*dynamic_taskq
;
56 static taskq_thread_t
*taskq_thread_create(taskq_t
*);
58 /* List of all taskqs */
60 DECLARE_RWSEM(tq_list_sem
);
61 static uint_t taskq_tsd
;
64 task_km_flags(uint_t flags
)
66 if (flags
& TQ_NOSLEEP
)
69 if (flags
& TQ_PUSHPAGE
)
76 * taskq_find_by_name - Find the largest instance number of a named taskq.
79 taskq_find_by_name(const char *name
)
81 struct list_head
*tql
;
84 list_for_each_prev(tql
, &tq_list
) {
85 tq
= list_entry(tql
, taskq_t
, tq_taskqs
);
86 if (strcmp(name
, tq
->tq_name
) == 0)
87 return tq
->tq_instance
;
93 * NOTE: Must be called with tq->tq_lock held, returns a list_t which
94 * is not attached to the free, work, or pending taskq lists.
97 task_alloc(taskq_t
*tq
, uint_t flags
, unsigned long *irqflags
)
103 ASSERT(spin_is_locked(&tq
->tq_lock
));
105 /* Acquire taskq_ent_t's from free list if available */
106 if (!list_empty(&tq
->tq_free_list
) && !(flags
& TQ_NEW
)) {
107 t
= list_entry(tq
->tq_free_list
.next
, taskq_ent_t
, tqent_list
);
109 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
110 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_CANCEL
));
111 ASSERT(!timer_pending(&t
->tqent_timer
));
113 list_del_init(&t
->tqent_list
);
117 /* Free list is empty and memory allocations are prohibited */
118 if (flags
& TQ_NOALLOC
)
121 /* Hit maximum taskq_ent_t pool size */
122 if (tq
->tq_nalloc
>= tq
->tq_maxalloc
) {
123 if (flags
& TQ_NOSLEEP
)
127 * Sleep periodically polling the free list for an available
128 * taskq_ent_t. Dispatching with TQ_SLEEP should always succeed
129 * but we cannot block forever waiting for an taskq_ent_t to
130 * show up in the free list, otherwise a deadlock can happen.
132 * Therefore, we need to allocate a new task even if the number
133 * of allocated tasks is above tq->tq_maxalloc, but we still
134 * end up delaying the task allocation by one second, thereby
135 * throttling the task dispatch rate.
137 spin_unlock_irqrestore(&tq
->tq_lock
, *irqflags
);
138 schedule_timeout(HZ
/ 100);
139 spin_lock_irqsave_nested(&tq
->tq_lock
, *irqflags
,
147 spin_unlock_irqrestore(&tq
->tq_lock
, *irqflags
);
148 t
= kmem_alloc(sizeof (taskq_ent_t
), task_km_flags(flags
));
149 spin_lock_irqsave_nested(&tq
->tq_lock
, *irqflags
, tq
->tq_lock_class
);
160 * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t
161 * to already be removed from the free, work, or pending taskq lists.
164 task_free(taskq_t
*tq
, taskq_ent_t
*t
)
168 ASSERT(spin_is_locked(&tq
->tq_lock
));
169 ASSERT(list_empty(&t
->tqent_list
));
170 ASSERT(!timer_pending(&t
->tqent_timer
));
172 kmem_free(t
, sizeof (taskq_ent_t
));
177 * NOTE: Must be called with tq->tq_lock held, either destroys the
178 * taskq_ent_t if too many exist or moves it to the free list for later use.
181 task_done(taskq_t
*tq
, taskq_ent_t
*t
)
185 ASSERT(spin_is_locked(&tq
->tq_lock
));
187 /* Wake tasks blocked in taskq_wait_id() */
188 wake_up_all(&t
->tqent_waitq
);
190 list_del_init(&t
->tqent_list
);
192 if (tq
->tq_nalloc
<= tq
->tq_minalloc
) {
194 t
->tqent_func
= NULL
;
198 list_add_tail(&t
->tqent_list
, &tq
->tq_free_list
);
205 * When a delayed task timer expires remove it from the delay list and
206 * add it to the priority list in order for immediate processing.
209 task_expire(unsigned long data
)
211 taskq_ent_t
*w
, *t
= (taskq_ent_t
*)data
;
212 taskq_t
*tq
= t
->tqent_taskq
;
216 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
, tq
->tq_lock_class
);
218 if (t
->tqent_flags
& TQENT_FLAG_CANCEL
) {
219 ASSERT(list_empty(&t
->tqent_list
));
220 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
224 t
->tqent_birth
= jiffies
;
226 * The priority list must be maintained in strict task id order
227 * from lowest to highest for lowest_id to be easily calculable.
229 list_del(&t
->tqent_list
);
230 list_for_each_prev(l
, &tq
->tq_prio_list
) {
231 w
= list_entry(l
, taskq_ent_t
, tqent_list
);
232 if (w
->tqent_id
< t
->tqent_id
) {
233 list_add(&t
->tqent_list
, l
);
237 if (l
== &tq
->tq_prio_list
)
238 list_add(&t
->tqent_list
, &tq
->tq_prio_list
);
240 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
242 wake_up(&tq
->tq_work_waitq
);
246 * Returns the lowest incomplete taskqid_t. The taskqid_t may
247 * be queued on the pending list, on the priority list, on the
248 * delay list, or on the work list currently being handled, but
249 * it is not 100% complete yet.
252 taskq_lowest_id(taskq_t
*tq
)
254 taskqid_t lowest_id
= tq
->tq_next_id
;
259 ASSERT(spin_is_locked(&tq
->tq_lock
));
261 if (!list_empty(&tq
->tq_pend_list
)) {
262 t
= list_entry(tq
->tq_pend_list
.next
, taskq_ent_t
, tqent_list
);
263 lowest_id
= MIN(lowest_id
, t
->tqent_id
);
266 if (!list_empty(&tq
->tq_prio_list
)) {
267 t
= list_entry(tq
->tq_prio_list
.next
, taskq_ent_t
, tqent_list
);
268 lowest_id
= MIN(lowest_id
, t
->tqent_id
);
271 if (!list_empty(&tq
->tq_delay_list
)) {
272 t
= list_entry(tq
->tq_delay_list
.next
, taskq_ent_t
, tqent_list
);
273 lowest_id
= MIN(lowest_id
, t
->tqent_id
);
276 if (!list_empty(&tq
->tq_active_list
)) {
277 tqt
= list_entry(tq
->tq_active_list
.next
, taskq_thread_t
,
279 ASSERT(tqt
->tqt_id
!= 0);
280 lowest_id
= MIN(lowest_id
, tqt
->tqt_id
);
287 * Insert a task into a list keeping the list sorted by increasing taskqid.
290 taskq_insert_in_order(taskq_t
*tq
, taskq_thread_t
*tqt
)
297 ASSERT(spin_is_locked(&tq
->tq_lock
));
299 list_for_each_prev(l
, &tq
->tq_active_list
) {
300 w
= list_entry(l
, taskq_thread_t
, tqt_active_list
);
301 if (w
->tqt_id
< tqt
->tqt_id
) {
302 list_add(&tqt
->tqt_active_list
, l
);
306 if (l
== &tq
->tq_active_list
)
307 list_add(&tqt
->tqt_active_list
, &tq
->tq_active_list
);
311 * Find and return a task from the given list if it exists. The list
312 * must be in lowest to highest task id order.
315 taskq_find_list(taskq_t
*tq
, struct list_head
*lh
, taskqid_t id
)
320 ASSERT(spin_is_locked(&tq
->tq_lock
));
322 list_for_each(l
, lh
) {
323 t
= list_entry(l
, taskq_ent_t
, tqent_list
);
325 if (t
->tqent_id
== id
)
328 if (t
->tqent_id
> id
)
336 * Find an already dispatched task given the task id regardless of what
337 * state it is in. If a task is still pending or executing it will be
338 * returned and 'active' set appropriately. If the task has already
339 * been run then NULL is returned.
342 taskq_find(taskq_t
*tq
, taskqid_t id
, int *active
)
348 ASSERT(spin_is_locked(&tq
->tq_lock
));
351 t
= taskq_find_list(tq
, &tq
->tq_delay_list
, id
);
355 t
= taskq_find_list(tq
, &tq
->tq_prio_list
, id
);
359 t
= taskq_find_list(tq
, &tq
->tq_pend_list
, id
);
363 list_for_each(l
, &tq
->tq_active_list
) {
364 tqt
= list_entry(l
, taskq_thread_t
, tqt_active_list
);
365 if (tqt
->tqt_id
== id
) {
376 * Theory for the taskq_wait_id(), taskq_wait_outstanding(), and
377 * taskq_wait() functions below.
379 * Taskq waiting is accomplished by tracking the lowest outstanding task
380 * id and the next available task id. As tasks are dispatched they are
381 * added to the tail of the pending, priority, or delay lists. As worker
382 * threads become available the tasks are removed from the heads of these
383 * lists and linked to the worker threads. This ensures the lists are
384 * kept sorted by lowest to highest task id.
386 * Therefore the lowest outstanding task id can be quickly determined by
387 * checking the head item from all of these lists. This value is stored
388 * with the taskq as the lowest id. It only needs to be recalculated when
389 * either the task with the current lowest id completes or is canceled.
391 * By blocking until the lowest task id exceeds the passed task id the
392 * taskq_wait_outstanding() function can be easily implemented. Similarly,
393 * by blocking until the lowest task id matches the next task id taskq_wait()
394 * can be implemented.
396 * Callers should be aware that when there are multiple worked threads it
397 * is possible for larger task ids to complete before smaller ones. Also
398 * when the taskq contains delay tasks with small task ids callers may
399 * block for a considerable length of time waiting for them to expire and
403 taskq_wait_id_check(taskq_t
*tq
, taskqid_t id
)
409 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
, tq
->tq_lock_class
);
410 rc
= (taskq_find(tq
, id
, &active
) == NULL
);
411 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
417 * The taskq_wait_id() function blocks until the passed task id completes.
418 * This does not guarantee that all lower task ids have completed.
421 taskq_wait_id(taskq_t
*tq
, taskqid_t id
)
423 wait_event(tq
->tq_wait_waitq
, taskq_wait_id_check(tq
, id
));
425 EXPORT_SYMBOL(taskq_wait_id
);
428 taskq_wait_outstanding_check(taskq_t
*tq
, taskqid_t id
)
433 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
, tq
->tq_lock_class
);
434 rc
= (id
< tq
->tq_lowest_id
);
435 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
441 * The taskq_wait_outstanding() function will block until all tasks with a
442 * lower taskqid than the passed 'id' have been completed. Note that all
443 * task id's are assigned monotonically at dispatch time. Zero may be
444 * passed for the id to indicate all tasks dispatch up to this point,
445 * but not after, should be waited for.
448 taskq_wait_outstanding(taskq_t
*tq
, taskqid_t id
)
450 wait_event(tq
->tq_wait_waitq
,
451 taskq_wait_outstanding_check(tq
, id
? id
: tq
->tq_next_id
- 1));
453 EXPORT_SYMBOL(taskq_wait_outstanding
);
456 taskq_wait_check(taskq_t
*tq
)
461 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
, tq
->tq_lock_class
);
462 rc
= (tq
->tq_lowest_id
== tq
->tq_next_id
);
463 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
469 * The taskq_wait() function will block until the taskq is empty.
470 * This means that if a taskq re-dispatches work to itself taskq_wait()
471 * callers will block indefinitely.
474 taskq_wait(taskq_t
*tq
)
476 wait_event(tq
->tq_wait_waitq
, taskq_wait_check(tq
));
478 EXPORT_SYMBOL(taskq_wait
);
481 taskq_member(taskq_t
*tq
, kthread_t
*t
)
483 return (tq
== (taskq_t
*)tsd_get_by_thread(taskq_tsd
, t
));
485 EXPORT_SYMBOL(taskq_member
);
488 * Cancel an already dispatched task given the task id. Still pending tasks
489 * will be immediately canceled, and if the task is active the function will
490 * block until it completes. Preallocated tasks which are canceled must be
491 * freed by the caller.
494 taskq_cancel_id(taskq_t
*tq
, taskqid_t id
)
503 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
, tq
->tq_lock_class
);
504 t
= taskq_find(tq
, id
, &active
);
506 list_del_init(&t
->tqent_list
);
507 t
->tqent_flags
|= TQENT_FLAG_CANCEL
;
510 * When canceling the lowest outstanding task id we
511 * must recalculate the new lowest outstanding id.
513 if (tq
->tq_lowest_id
== t
->tqent_id
) {
514 tq
->tq_lowest_id
= taskq_lowest_id(tq
);
515 ASSERT3S(tq
->tq_lowest_id
, >, t
->tqent_id
);
519 * The task_expire() function takes the tq->tq_lock so drop
520 * drop the lock before synchronously cancelling the timer.
522 if (timer_pending(&t
->tqent_timer
)) {
523 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
524 del_timer_sync(&t
->tqent_timer
);
525 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
,
529 if (!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
))
534 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
537 taskq_wait_id(tq
, id
);
543 EXPORT_SYMBOL(taskq_cancel_id
);
545 static int taskq_thread_spawn(taskq_t
*tq
);
548 taskq_dispatch(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
)
552 unsigned long irqflags
;
557 spin_lock_irqsave_nested(&tq
->tq_lock
, irqflags
, tq
->tq_lock_class
);
559 /* Taskq being destroyed and all tasks drained */
560 if (!(tq
->tq_flags
& TASKQ_ACTIVE
))
563 /* Do not queue the task unless there is idle thread for it */
564 ASSERT(tq
->tq_nactive
<= tq
->tq_nthreads
);
565 if ((flags
& TQ_NOQUEUE
) && (tq
->tq_nactive
== tq
->tq_nthreads
))
568 if ((t
= task_alloc(tq
, flags
, &irqflags
)) == NULL
)
571 spin_lock(&t
->tqent_lock
);
573 /* Queue to the priority list instead of the pending list */
574 if (flags
& TQ_FRONT
)
575 list_add_tail(&t
->tqent_list
, &tq
->tq_prio_list
);
577 list_add_tail(&t
->tqent_list
, &tq
->tq_pend_list
);
579 t
->tqent_id
= rc
= tq
->tq_next_id
;
581 t
->tqent_func
= func
;
584 t
->tqent_timer
.data
= 0;
585 t
->tqent_timer
.function
= NULL
;
586 t
->tqent_timer
.expires
= 0;
587 t
->tqent_birth
= jiffies
;
589 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
591 spin_unlock(&t
->tqent_lock
);
593 wake_up(&tq
->tq_work_waitq
);
595 /* Spawn additional taskq threads if required. */
596 if (tq
->tq_nactive
== tq
->tq_nthreads
)
597 (void) taskq_thread_spawn(tq
);
599 spin_unlock_irqrestore(&tq
->tq_lock
, irqflags
);
602 EXPORT_SYMBOL(taskq_dispatch
);
605 taskq_dispatch_delay(taskq_t
*tq
, task_func_t func
, void *arg
,
606 uint_t flags
, clock_t expire_time
)
610 unsigned long irqflags
;
615 spin_lock_irqsave_nested(&tq
->tq_lock
, irqflags
, tq
->tq_lock_class
);
617 /* Taskq being destroyed and all tasks drained */
618 if (!(tq
->tq_flags
& TASKQ_ACTIVE
))
621 if ((t
= task_alloc(tq
, flags
, &irqflags
)) == NULL
)
624 spin_lock(&t
->tqent_lock
);
626 /* Queue to the delay list for subsequent execution */
627 list_add_tail(&t
->tqent_list
, &tq
->tq_delay_list
);
629 t
->tqent_id
= rc
= tq
->tq_next_id
;
631 t
->tqent_func
= func
;
634 t
->tqent_timer
.data
= (unsigned long)t
;
635 t
->tqent_timer
.function
= task_expire
;
636 t
->tqent_timer
.expires
= (unsigned long)expire_time
;
637 add_timer(&t
->tqent_timer
);
639 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
641 spin_unlock(&t
->tqent_lock
);
643 /* Spawn additional taskq threads if required. */
644 if (tq
->tq_nactive
== tq
->tq_nthreads
)
645 (void) taskq_thread_spawn(tq
);
646 spin_unlock_irqrestore(&tq
->tq_lock
, irqflags
);
649 EXPORT_SYMBOL(taskq_dispatch_delay
);
652 taskq_dispatch_ent(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
,
655 unsigned long irqflags
;
659 spin_lock_irqsave_nested(&tq
->tq_lock
, irqflags
,
662 /* Taskq being destroyed and all tasks drained */
663 if (!(tq
->tq_flags
& TASKQ_ACTIVE
)) {
668 spin_lock(&t
->tqent_lock
);
671 * Mark it as a prealloc'd task. This is important
672 * to ensure that we don't free it later.
674 t
->tqent_flags
|= TQENT_FLAG_PREALLOC
;
676 /* Queue to the priority list instead of the pending list */
677 if (flags
& TQ_FRONT
)
678 list_add_tail(&t
->tqent_list
, &tq
->tq_prio_list
);
680 list_add_tail(&t
->tqent_list
, &tq
->tq_pend_list
);
682 t
->tqent_id
= tq
->tq_next_id
;
684 t
->tqent_func
= func
;
687 t
->tqent_birth
= jiffies
;
689 spin_unlock(&t
->tqent_lock
);
691 wake_up(&tq
->tq_work_waitq
);
693 /* Spawn additional taskq threads if required. */
694 if (tq
->tq_nactive
== tq
->tq_nthreads
)
695 (void) taskq_thread_spawn(tq
);
696 spin_unlock_irqrestore(&tq
->tq_lock
, irqflags
);
698 EXPORT_SYMBOL(taskq_dispatch_ent
);
701 taskq_empty_ent(taskq_ent_t
*t
)
703 return (list_empty(&t
->tqent_list
));
705 EXPORT_SYMBOL(taskq_empty_ent
);
708 taskq_init_ent(taskq_ent_t
*t
)
710 spin_lock_init(&t
->tqent_lock
);
711 init_waitqueue_head(&t
->tqent_waitq
);
712 init_timer(&t
->tqent_timer
);
713 INIT_LIST_HEAD(&t
->tqent_list
);
715 t
->tqent_func
= NULL
;
718 t
->tqent_taskq
= NULL
;
720 EXPORT_SYMBOL(taskq_init_ent
);
723 * Return the next pending task, preference is given to tasks on the
724 * priority list which were dispatched with TQ_FRONT.
727 taskq_next_ent(taskq_t
*tq
)
729 struct list_head
*list
;
731 ASSERT(spin_is_locked(&tq
->tq_lock
));
733 if (!list_empty(&tq
->tq_prio_list
))
734 list
= &tq
->tq_prio_list
;
735 else if (!list_empty(&tq
->tq_pend_list
))
736 list
= &tq
->tq_pend_list
;
740 return (list_entry(list
->next
, taskq_ent_t
, tqent_list
));
744 * Spawns a new thread for the specified taskq.
747 taskq_thread_spawn_task(void *arg
)
749 taskq_t
*tq
= (taskq_t
*)arg
;
752 (void) taskq_thread_create(tq
);
754 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
, tq
->tq_lock_class
);
756 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
760 * Spawn addition threads for dynamic taskqs (TASKQ_DYNAMIC) the current
761 * number of threads is insufficient to handle the pending tasks. These
762 * new threads must be created by the dedicated dynamic_taskq to avoid
763 * deadlocks between thread creation and memory reclaim. The system_taskq
764 * which is also a dynamic taskq cannot be safely used for this.
767 taskq_thread_spawn(taskq_t
*tq
)
771 if (!(tq
->tq_flags
& TASKQ_DYNAMIC
))
774 if ((tq
->tq_nthreads
+ tq
->tq_nspawn
< tq
->tq_maxthreads
) &&
775 (tq
->tq_flags
& TASKQ_ACTIVE
)) {
776 spawning
= (++tq
->tq_nspawn
);
777 taskq_dispatch(dynamic_taskq
, taskq_thread_spawn_task
,
785 * Threads in a dynamic taskq should only exit once it has been completely
786 * drained and no other threads are actively servicing tasks. This prevents
787 * threads from being created and destroyed more than is required.
789 * The first thread is the thread list is treated as the primary thread.
790 * There is nothing special about the primary thread but in order to avoid
791 * all the taskq pids from changing we opt to make it long running.
794 taskq_thread_should_stop(taskq_t
*tq
, taskq_thread_t
*tqt
)
796 ASSERT(spin_is_locked(&tq
->tq_lock
));
798 if (!(tq
->tq_flags
& TASKQ_DYNAMIC
))
801 if (list_first_entry(&(tq
->tq_thread_list
), taskq_thread_t
,
802 tqt_thread_list
) == tqt
)
806 ((tq
->tq_nspawn
== 0) && /* No threads are being spawned */
807 (tq
->tq_nactive
== 0) && /* No threads are handling tasks */
808 (tq
->tq_nthreads
> 1) && /* More than 1 thread is running */
809 (!taskq_next_ent(tq
)) && /* There are no pending tasks */
810 (spl_taskq_thread_dynamic
)); /* Dynamic taskqs are allowed */
814 taskq_thread(void *args
)
816 DECLARE_WAITQUEUE(wait
, current
);
818 taskq_thread_t
*tqt
= args
;
827 current
->flags
|= PF_NOFREEZE
;
829 (void) spl_fstrans_mark();
831 sigfillset(&blocked
);
832 sigprocmask(SIG_BLOCK
, &blocked
, NULL
);
833 flush_signals(current
);
835 tsd_set(taskq_tsd
, tq
);
836 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
, tq
->tq_lock_class
);
838 /* Immediately exit if more threads than allowed were created. */
839 if (tq
->tq_nthreads
>= tq
->tq_maxthreads
)
843 list_add_tail(&tqt
->tqt_thread_list
, &tq
->tq_thread_list
);
844 wake_up(&tq
->tq_wait_waitq
);
845 set_current_state(TASK_INTERRUPTIBLE
);
847 while (!kthread_should_stop()) {
849 if (list_empty(&tq
->tq_pend_list
) &&
850 list_empty(&tq
->tq_prio_list
)) {
852 if (taskq_thread_should_stop(tq
, tqt
)) {
853 wake_up_all(&tq
->tq_wait_waitq
);
857 add_wait_queue_exclusive(&tq
->tq_work_waitq
, &wait
);
858 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
863 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
,
865 remove_wait_queue(&tq
->tq_work_waitq
, &wait
);
867 __set_current_state(TASK_RUNNING
);
870 if ((t
= taskq_next_ent(tq
)) != NULL
) {
871 list_del_init(&t
->tqent_list
);
874 * In order to support recursively dispatching a
875 * preallocated taskq_ent_t, tqent_id must be
876 * stored prior to executing tqent_func.
878 tqt
->tqt_id
= t
->tqent_id
;
882 * We must store a copy of the flags prior to
883 * servicing the task (servicing a prealloc'd task
884 * returns the ownership of the tqent back to
885 * the caller of taskq_dispatch). Thus,
886 * tqent_flags _may_ change within the call.
888 tqt
->tqt_flags
= t
->tqent_flags
;
890 taskq_insert_in_order(tq
, tqt
);
892 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
894 /* Perform the requested task */
895 t
->tqent_func(t
->tqent_arg
);
897 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
,
900 list_del_init(&tqt
->tqt_active_list
);
901 tqt
->tqt_task
= NULL
;
903 /* For prealloc'd tasks, we don't free anything. */
904 if (!(tqt
->tqt_flags
& TQENT_FLAG_PREALLOC
))
908 * When the current lowest outstanding taskqid is
909 * done calculate the new lowest outstanding id
911 if (tq
->tq_lowest_id
== tqt
->tqt_id
) {
912 tq
->tq_lowest_id
= taskq_lowest_id(tq
);
913 ASSERT3S(tq
->tq_lowest_id
, >, tqt
->tqt_id
);
916 /* Spawn additional taskq threads if required. */
917 if ((++seq_tasks
) > spl_taskq_thread_sequential
&&
918 taskq_thread_spawn(tq
))
923 wake_up_all(&tq
->tq_wait_waitq
);
925 if (taskq_thread_should_stop(tq
, tqt
))
929 set_current_state(TASK_INTERRUPTIBLE
);
933 __set_current_state(TASK_RUNNING
);
935 list_del_init(&tqt
->tqt_thread_list
);
937 kmem_free(tqt
, sizeof (taskq_thread_t
));
938 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
940 tsd_set(taskq_tsd
, NULL
);
945 static taskq_thread_t
*
946 taskq_thread_create(taskq_t
*tq
)
948 static int last_used_cpu
= 0;
951 tqt
= kmem_alloc(sizeof (*tqt
), KM_PUSHPAGE
);
952 INIT_LIST_HEAD(&tqt
->tqt_thread_list
);
953 INIT_LIST_HEAD(&tqt
->tqt_active_list
);
957 tqt
->tqt_thread
= spl_kthread_create(taskq_thread
, tqt
,
959 if (tqt
->tqt_thread
== NULL
) {
960 kmem_free(tqt
, sizeof (taskq_thread_t
));
964 if (spl_taskq_thread_bind
) {
965 last_used_cpu
= (last_used_cpu
+ 1) % num_online_cpus();
966 kthread_bind(tqt
->tqt_thread
, last_used_cpu
);
969 if (spl_taskq_thread_priority
)
970 set_user_nice(tqt
->tqt_thread
, PRIO_TO_NICE(tq
->tq_pri
));
972 wake_up_process(tqt
->tqt_thread
);
978 taskq_create(const char *name
, int nthreads
, pri_t pri
,
979 int minalloc
, int maxalloc
, uint_t flags
)
983 int count
= 0, rc
= 0, i
;
984 unsigned long irqflags
;
986 ASSERT(name
!= NULL
);
987 ASSERT(minalloc
>= 0);
988 ASSERT(maxalloc
<= INT_MAX
);
989 ASSERT(!(flags
& (TASKQ_CPR_SAFE
))); /* Unsupported */
991 /* Scale the number of threads using nthreads as a percentage */
992 if (flags
& TASKQ_THREADS_CPU_PCT
) {
993 ASSERT(nthreads
<= 100);
994 ASSERT(nthreads
>= 0);
995 nthreads
= MIN(nthreads
, 100);
996 nthreads
= MAX(nthreads
, 0);
997 nthreads
= MAX((num_online_cpus() * nthreads
) / 100, 1);
1000 tq
= kmem_alloc(sizeof (*tq
), KM_PUSHPAGE
);
1004 spin_lock_init(&tq
->tq_lock
);
1005 INIT_LIST_HEAD(&tq
->tq_thread_list
);
1006 INIT_LIST_HEAD(&tq
->tq_active_list
);
1007 tq
->tq_name
= strdup(name
);
1009 tq
->tq_nthreads
= 0;
1011 tq
->tq_maxthreads
= nthreads
;
1013 tq
->tq_minalloc
= minalloc
;
1014 tq
->tq_maxalloc
= maxalloc
;
1016 tq
->tq_flags
= (flags
| TASKQ_ACTIVE
);
1018 tq
->tq_lowest_id
= 1;
1019 INIT_LIST_HEAD(&tq
->tq_free_list
);
1020 INIT_LIST_HEAD(&tq
->tq_pend_list
);
1021 INIT_LIST_HEAD(&tq
->tq_prio_list
);
1022 INIT_LIST_HEAD(&tq
->tq_delay_list
);
1023 init_waitqueue_head(&tq
->tq_work_waitq
);
1024 init_waitqueue_head(&tq
->tq_wait_waitq
);
1025 tq
->tq_lock_class
= TQ_LOCK_GENERAL
;
1026 INIT_LIST_HEAD(&tq
->tq_taskqs
);
1028 if (flags
& TASKQ_PREPOPULATE
) {
1029 spin_lock_irqsave_nested(&tq
->tq_lock
, irqflags
,
1032 for (i
= 0; i
< minalloc
; i
++)
1033 task_done(tq
, task_alloc(tq
, TQ_PUSHPAGE
| TQ_NEW
,
1036 spin_unlock_irqrestore(&tq
->tq_lock
, irqflags
);
1039 if ((flags
& TASKQ_DYNAMIC
) && spl_taskq_thread_dynamic
)
1042 for (i
= 0; i
< nthreads
; i
++) {
1043 tqt
= taskq_thread_create(tq
);
1050 /* Wait for all threads to be started before potential destroy */
1051 wait_event(tq
->tq_wait_waitq
, tq
->tq_nthreads
== count
);
1057 down_write(&tq_list_sem
);
1058 tq
->tq_instance
= taskq_find_by_name(name
) + 1;
1059 list_add_tail(&tq
->tq_taskqs
, &tq_list
);
1060 up_write(&tq_list_sem
);
1065 EXPORT_SYMBOL(taskq_create
);
1068 taskq_destroy(taskq_t
*tq
)
1070 struct task_struct
*thread
;
1071 taskq_thread_t
*tqt
;
1073 unsigned long flags
;
1076 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
, tq
->tq_lock_class
);
1077 tq
->tq_flags
&= ~TASKQ_ACTIVE
;
1078 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
1081 * When TASKQ_ACTIVE is clear new tasks may not be added nor may
1082 * new worker threads be spawned for dynamic taskq.
1084 if (dynamic_taskq
!= NULL
)
1085 taskq_wait_outstanding(dynamic_taskq
, 0);
1089 /* remove taskq from global list used by the kstats */
1090 down_write(&tq_list_sem
);
1091 list_del(&tq
->tq_taskqs
);
1092 up_write(&tq_list_sem
);
1094 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
, tq
->tq_lock_class
);
1097 * Signal each thread to exit and block until it does. Each thread
1098 * is responsible for removing itself from the list and freeing its
1099 * taskq_thread_t. This allows for idle threads to opt to remove
1100 * themselves from the taskq. They can be recreated as needed.
1102 while (!list_empty(&tq
->tq_thread_list
)) {
1103 tqt
= list_entry(tq
->tq_thread_list
.next
,
1104 taskq_thread_t
, tqt_thread_list
);
1105 thread
= tqt
->tqt_thread
;
1106 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
1108 kthread_stop(thread
);
1110 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
,
1114 while (!list_empty(&tq
->tq_free_list
)) {
1115 t
= list_entry(tq
->tq_free_list
.next
, taskq_ent_t
, tqent_list
);
1117 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
1119 list_del_init(&t
->tqent_list
);
1123 ASSERT0(tq
->tq_nthreads
);
1124 ASSERT0(tq
->tq_nalloc
);
1125 ASSERT0(tq
->tq_nspawn
);
1126 ASSERT(list_empty(&tq
->tq_thread_list
));
1127 ASSERT(list_empty(&tq
->tq_active_list
));
1128 ASSERT(list_empty(&tq
->tq_free_list
));
1129 ASSERT(list_empty(&tq
->tq_pend_list
));
1130 ASSERT(list_empty(&tq
->tq_prio_list
));
1131 ASSERT(list_empty(&tq
->tq_delay_list
));
1133 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
1135 strfree(tq
->tq_name
);
1136 kmem_free(tq
, sizeof (taskq_t
));
1138 EXPORT_SYMBOL(taskq_destroy
);
1141 static unsigned int spl_taskq_kick
= 0;
1145 * module_param_cb is introduced to take kernel_param_ops and
1146 * module_param_call is marked as obsolete. Also set and get operations
1147 * were changed to take a 'const struct kernel_param *'.
1150 #ifdef module_param_cb
1151 param_set_taskq_kick(const char *val
, const struct kernel_param
*kp
)
1153 param_set_taskq_kick(const char *val
, struct kernel_param
*kp
)
1159 unsigned long flags
;
1161 ret
= param_set_uint(val
, kp
);
1162 if (ret
< 0 || !spl_taskq_kick
)
1167 down_read(&tq_list_sem
);
1168 list_for_each_entry(tq
, &tq_list
, tq_taskqs
) {
1169 spin_lock_irqsave_nested(&tq
->tq_lock
, flags
,
1171 /* Check if the first pending is older than 5 seconds */
1172 t
= taskq_next_ent(tq
);
1173 if (t
&& time_after(jiffies
, t
->tqent_birth
+ 5*HZ
)) {
1174 (void) taskq_thread_spawn(tq
);
1175 printk(KERN_INFO
"spl: Kicked taskq %s/%d\n",
1176 tq
->tq_name
, tq
->tq_instance
);
1178 spin_unlock_irqrestore(&tq
->tq_lock
, flags
);
1180 up_read(&tq_list_sem
);
1184 #ifdef module_param_cb
1185 static const struct kernel_param_ops param_ops_taskq_kick
= {
1186 .set
= param_set_taskq_kick
,
1187 .get
= param_get_uint
,
1189 module_param_cb(spl_taskq_kick
, ¶m_ops_taskq_kick
, &spl_taskq_kick
, 0644);
1191 module_param_call(spl_taskq_kick
, param_set_taskq_kick
, param_get_uint
,
1192 &spl_taskq_kick
, 0644);
1194 MODULE_PARM_DESC(spl_taskq_kick
,
1195 "Write nonzero to kick stuck taskqs to spawn more threads");
1198 spl_taskq_init(void)
1200 tsd_create(&taskq_tsd
, NULL
);
1202 system_taskq
= taskq_create("spl_system_taskq", MAX(boot_ncpus
, 64),
1203 maxclsyspri
, boot_ncpus
, INT_MAX
, TASKQ_PREPOPULATE
|TASKQ_DYNAMIC
);
1204 if (system_taskq
== NULL
)
1207 dynamic_taskq
= taskq_create("spl_dynamic_taskq", 1,
1208 maxclsyspri
, boot_ncpus
, INT_MAX
, TASKQ_PREPOPULATE
);
1209 if (dynamic_taskq
== NULL
) {
1210 taskq_destroy(system_taskq
);
1215 * This is used to annotate tq_lock, so
1216 * taskq_dispatch -> taskq_thread_spawn -> taskq_dispatch
1217 * does not trigger a lockdep warning re: possible recursive locking
1219 dynamic_taskq
->tq_lock_class
= TQ_LOCK_DYNAMIC
;
1225 spl_taskq_fini(void)
1227 taskq_destroy(dynamic_taskq
);
1228 dynamic_taskq
= NULL
;
1230 taskq_destroy(system_taskq
);
1231 system_taskq
= NULL
;
1233 tsd_destroy(&taskq_tsd
);