]>
git.proxmox.com Git - mirror_spl-debian.git/blob - module/spl/spl-taskq.c
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://github.com/behlendorf/spl/>.
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>
29 #include <spl-debug.h>
31 #ifdef SS_DEBUG_SUBSYS
32 #undef SS_DEBUG_SUBSYS
35 #define SS_DEBUG_SUBSYS SS_TASKQ
37 /* Global system-wide dynamic task queue available for all consumers */
38 taskq_t
*system_taskq
;
39 EXPORT_SYMBOL(system_taskq
);
42 * NOTE: Must be called with tq->tq_lock held, returns a list_t which
43 * is not attached to the free, work, or pending taskq lists.
46 task_alloc(taskq_t
*tq
, uint_t flags
)
53 ASSERT(flags
& (TQ_SLEEP
| TQ_NOSLEEP
)); /* One set */
54 ASSERT(!((flags
& TQ_SLEEP
) && (flags
& TQ_NOSLEEP
))); /* Not both */
55 ASSERT(spin_is_locked(&tq
->tq_lock
));
57 /* Acquire taskq_ent_t's from free list if available */
58 if (!list_empty(&tq
->tq_free_list
) && !(flags
& TQ_NEW
)) {
59 t
= list_entry(tq
->tq_free_list
.next
, taskq_ent_t
, tqent_list
);
61 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
63 list_del_init(&t
->tqent_list
);
67 /* Free list is empty and memory allocations are prohibited */
68 if (flags
& TQ_NOALLOC
)
71 /* Hit maximum taskq_ent_t pool size */
72 if (tq
->tq_nalloc
>= tq
->tq_maxalloc
) {
73 if (flags
& TQ_NOSLEEP
)
77 * Sleep periodically polling the free list for an available
78 * taskq_ent_t. Dispatching with TQ_SLEEP should always succeed
79 * but we cannot block forever waiting for an taskq_entq_t to
80 * show up in the free list, otherwise a deadlock can happen.
82 * Therefore, we need to allocate a new task even if the number
83 * of allocated tasks is above tq->tq_maxalloc, but we still
84 * end up delaying the task allocation by one second, thereby
85 * throttling the task dispatch rate.
87 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
88 schedule_timeout(HZ
/ 100);
89 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
91 SGOTO(retry
, count
++);
94 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
95 t
= kmem_alloc(sizeof(taskq_ent_t
), flags
& (TQ_SLEEP
| TQ_NOSLEEP
));
96 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
107 * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t
108 * to already be removed from the free, work, or pending taskq lists.
111 task_free(taskq_t
*tq
, taskq_ent_t
*t
)
117 ASSERT(spin_is_locked(&tq
->tq_lock
));
118 ASSERT(list_empty(&t
->tqent_list
));
120 kmem_free(t
, sizeof(taskq_ent_t
));
127 * NOTE: Must be called with tq->tq_lock held, either destroys the
128 * taskq_ent_t if too many exist or moves it to the free list for later use.
131 task_done(taskq_t
*tq
, taskq_ent_t
*t
)
136 ASSERT(spin_is_locked(&tq
->tq_lock
));
138 list_del_init(&t
->tqent_list
);
140 if (tq
->tq_nalloc
<= tq
->tq_minalloc
) {
142 t
->tqent_func
= NULL
;
146 list_add_tail(&t
->tqent_list
, &tq
->tq_free_list
);
155 * As tasks are submitted to the task queue they are assigned a
156 * monotonically increasing taskqid and added to the tail of the pending
157 * list. As worker threads become available the tasks are removed from
158 * the head of the pending or priority list, giving preference to the
159 * priority list. The tasks are then added to the work list, preserving
160 * the ordering by taskqid. Finally, as tasks complete they are removed
161 * from the work list. This means that the pending and work lists are
162 * always kept sorted by taskqid. Thus the lowest outstanding
163 * incomplete taskqid can be determined simply by checking the min
164 * taskqid for each head item on the pending, priority, and work list.
165 * This value is stored in tq->tq_lowest_id and only updated to the new
166 * lowest id when the previous lowest id completes. All taskqids lower
167 * than tq->tq_lowest_id must have completed. It is also possible
168 * larger taskqid's have completed because they may be processed in
169 * parallel by several worker threads. However, this is not a problem
170 * because the behavior of taskq_wait_id() is to block until all
171 * previously submitted taskqid's have completed.
173 * XXX: Taskqid_t wrapping is not handled. However, taskqid_t's are
174 * 64-bit values so even if a taskq is processing 2^24 (16,777,216)
175 * taskqid_ts per second it will still take 2^40 seconds, 34,865 years,
176 * before the wrap occurs. I can live with that for now.
179 taskq_wait_check(taskq_t
*tq
, taskqid_t id
)
183 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
184 rc
= (id
< tq
->tq_lowest_id
);
185 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
191 __taskq_wait_id(taskq_t
*tq
, taskqid_t id
)
196 wait_event(tq
->tq_wait_waitq
, taskq_wait_check(tq
, id
));
200 EXPORT_SYMBOL(__taskq_wait_id
);
203 __taskq_wait(taskq_t
*tq
)
209 /* Wait for the largest outstanding taskqid */
210 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
211 id
= tq
->tq_next_id
- 1;
212 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
214 __taskq_wait_id(tq
, id
);
219 EXPORT_SYMBOL(__taskq_wait
);
222 __taskq_member(taskq_t
*tq
, void *t
)
231 list_for_each(l
, &tq
->tq_thread_list
) {
232 tqt
= list_entry(l
, taskq_thread_t
, tqt_thread_list
);
233 if (tqt
->tqt_thread
== (struct task_struct
*)t
)
239 EXPORT_SYMBOL(__taskq_member
);
242 __taskq_dispatch(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
)
251 /* Solaris assumes TQ_SLEEP if not passed explicitly */
252 if (!(flags
& (TQ_SLEEP
| TQ_NOSLEEP
)))
255 if (unlikely(in_atomic() && (flags
& TQ_SLEEP
)))
256 PANIC("May schedule while atomic: %s/0x%08x/%d\n",
257 current
->comm
, preempt_count(), current
->pid
);
259 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
261 /* Taskq being destroyed and all tasks drained */
262 if (!(tq
->tq_flags
& TQ_ACTIVE
))
265 /* Do not queue the task unless there is idle thread for it */
266 ASSERT(tq
->tq_nactive
<= tq
->tq_nthreads
);
267 if ((flags
& TQ_NOQUEUE
) && (tq
->tq_nactive
== tq
->tq_nthreads
))
270 if ((t
= task_alloc(tq
, flags
)) == NULL
)
273 spin_lock(&t
->tqent_lock
);
275 /* Queue to the priority list instead of the pending list */
276 if (flags
& TQ_FRONT
)
277 list_add_tail(&t
->tqent_list
, &tq
->tq_prio_list
);
279 list_add_tail(&t
->tqent_list
, &tq
->tq_pend_list
);
281 t
->tqent_id
= rc
= tq
->tq_next_id
;
283 t
->tqent_func
= func
;
286 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
288 spin_unlock(&t
->tqent_lock
);
290 wake_up(&tq
->tq_work_waitq
);
292 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
295 EXPORT_SYMBOL(__taskq_dispatch
);
298 __taskq_dispatch_ent(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
,
305 ASSERT(!(tq
->tq_flags
& TASKQ_DYNAMIC
));
307 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
309 /* Taskq being destroyed and all tasks drained */
310 if (!(tq
->tq_flags
& TQ_ACTIVE
)) {
315 spin_lock(&t
->tqent_lock
);
318 * Mark it as a prealloc'd task. This is important
319 * to ensure that we don't free it later.
321 t
->tqent_flags
|= TQENT_FLAG_PREALLOC
;
323 /* Queue to the priority list instead of the pending list */
324 if (flags
& TQ_FRONT
)
325 list_add_tail(&t
->tqent_list
, &tq
->tq_prio_list
);
327 list_add_tail(&t
->tqent_list
, &tq
->tq_pend_list
);
329 t
->tqent_id
= tq
->tq_next_id
;
331 t
->tqent_func
= func
;
334 spin_unlock(&t
->tqent_lock
);
336 wake_up(&tq
->tq_work_waitq
);
338 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
341 EXPORT_SYMBOL(__taskq_dispatch_ent
);
344 __taskq_empty_ent(taskq_ent_t
*t
)
346 return list_empty(&t
->tqent_list
);
348 EXPORT_SYMBOL(__taskq_empty_ent
);
351 __taskq_init_ent(taskq_ent_t
*t
)
353 spin_lock_init(&t
->tqent_lock
);
354 INIT_LIST_HEAD(&t
->tqent_list
);
356 t
->tqent_func
= NULL
;
360 EXPORT_SYMBOL(__taskq_init_ent
);
363 * Returns the lowest incomplete taskqid_t. The taskqid_t may
364 * be queued on the pending list, on the priority list, or on
365 * the work list currently being handled, but it is not 100%
369 taskq_lowest_id(taskq_t
*tq
)
371 taskqid_t lowest_id
= tq
->tq_next_id
;
377 ASSERT(spin_is_locked(&tq
->tq_lock
));
379 if (!list_empty(&tq
->tq_pend_list
)) {
380 t
= list_entry(tq
->tq_pend_list
.next
, taskq_ent_t
, tqent_list
);
381 lowest_id
= MIN(lowest_id
, t
->tqent_id
);
384 if (!list_empty(&tq
->tq_prio_list
)) {
385 t
= list_entry(tq
->tq_prio_list
.next
, taskq_ent_t
, tqent_list
);
386 lowest_id
= MIN(lowest_id
, t
->tqent_id
);
389 if (!list_empty(&tq
->tq_active_list
)) {
390 tqt
= list_entry(tq
->tq_active_list
.next
, taskq_thread_t
,
392 ASSERT(tqt
->tqt_id
!= 0);
393 lowest_id
= MIN(lowest_id
, tqt
->tqt_id
);
400 * Insert a task into a list keeping the list sorted by increasing
404 taskq_insert_in_order(taskq_t
*tq
, taskq_thread_t
*tqt
)
412 ASSERT(spin_is_locked(&tq
->tq_lock
));
414 list_for_each_prev(l
, &tq
->tq_active_list
) {
415 w
= list_entry(l
, taskq_thread_t
, tqt_active_list
);
416 if (w
->tqt_id
< tqt
->tqt_id
) {
417 list_add(&tqt
->tqt_active_list
, l
);
421 if (l
== &tq
->tq_active_list
)
422 list_add(&tqt
->tqt_active_list
, &tq
->tq_active_list
);
428 taskq_thread(void *args
)
430 DECLARE_WAITQUEUE(wait
, current
);
432 taskq_thread_t
*tqt
= args
;
435 struct list_head
*pend_list
;
440 current
->flags
|= PF_NOFREEZE
;
442 /* Disable the direct memory reclaim path */
443 if (tq
->tq_flags
& TASKQ_NORECLAIM
)
444 current
->flags
|= PF_MEMALLOC
;
446 sigfillset(&blocked
);
447 sigprocmask(SIG_BLOCK
, &blocked
, NULL
);
448 flush_signals(current
);
450 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
452 wake_up(&tq
->tq_wait_waitq
);
453 set_current_state(TASK_INTERRUPTIBLE
);
455 while (!kthread_should_stop()) {
457 add_wait_queue(&tq
->tq_work_waitq
, &wait
);
458 if (list_empty(&tq
->tq_pend_list
) &&
459 list_empty(&tq
->tq_prio_list
)) {
460 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
462 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
464 __set_current_state(TASK_RUNNING
);
467 remove_wait_queue(&tq
->tq_work_waitq
, &wait
);
469 if (!list_empty(&tq
->tq_prio_list
))
470 pend_list
= &tq
->tq_prio_list
;
471 else if (!list_empty(&tq
->tq_pend_list
))
472 pend_list
= &tq
->tq_pend_list
;
477 t
= list_entry(pend_list
->next
, taskq_ent_t
, tqent_list
);
478 list_del_init(&t
->tqent_list
);
480 /* In order to support recursively dispatching a
481 * preallocated taskq_ent_t, tqent_id must be
482 * stored prior to executing tqent_func. */
483 tqt
->tqt_id
= t
->tqent_id
;
485 /* We must store a copy of the flags prior to
486 * servicing the task (servicing a prealloc'd task
487 * returns the ownership of the tqent back to
488 * the caller of taskq_dispatch). Thus,
489 * tqent_flags _may_ change within the call. */
490 tqt
->tqt_flags
= t
->tqent_flags
;
492 taskq_insert_in_order(tq
, tqt
);
494 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
496 /* Perform the requested task */
497 t
->tqent_func(t
->tqent_arg
);
499 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
501 list_del_init(&tqt
->tqt_active_list
);
503 /* For prealloc'd tasks, we don't free anything. */
504 if ((tq
->tq_flags
& TASKQ_DYNAMIC
) ||
505 !(tqt
->tqt_flags
& TQENT_FLAG_PREALLOC
))
508 /* When the current lowest outstanding taskqid is
509 * done calculate the new lowest outstanding id */
510 if (tq
->tq_lowest_id
== tqt
->tqt_id
) {
511 tq
->tq_lowest_id
= taskq_lowest_id(tq
);
512 ASSERT3S(tq
->tq_lowest_id
, >, tqt
->tqt_id
);
517 wake_up_all(&tq
->tq_wait_waitq
);
520 set_current_state(TASK_INTERRUPTIBLE
);
524 __set_current_state(TASK_RUNNING
);
526 list_del_init(&tqt
->tqt_thread_list
);
527 kmem_free(tqt
, sizeof(taskq_thread_t
));
529 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
535 __taskq_create(const char *name
, int nthreads
, pri_t pri
,
536 int minalloc
, int maxalloc
, uint_t flags
)
540 int rc
= 0, i
, j
= 0;
543 ASSERT(name
!= NULL
);
544 ASSERT(pri
<= maxclsyspri
);
545 ASSERT(minalloc
>= 0);
546 ASSERT(maxalloc
<= INT_MAX
);
547 ASSERT(!(flags
& (TASKQ_CPR_SAFE
| TASKQ_DYNAMIC
))); /* Unsupported */
549 /* Scale the number of threads using nthreads as a percentage */
550 if (flags
& TASKQ_THREADS_CPU_PCT
) {
551 ASSERT(nthreads
<= 100);
552 ASSERT(nthreads
>= 0);
553 nthreads
= MIN(nthreads
, 100);
554 nthreads
= MAX(nthreads
, 0);
555 nthreads
= MAX((num_online_cpus() * nthreads
) / 100, 1);
558 tq
= kmem_alloc(sizeof(*tq
), KM_SLEEP
);
562 spin_lock_init(&tq
->tq_lock
);
563 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
564 INIT_LIST_HEAD(&tq
->tq_thread_list
);
565 INIT_LIST_HEAD(&tq
->tq_active_list
);
570 tq
->tq_minalloc
= minalloc
;
571 tq
->tq_maxalloc
= maxalloc
;
573 tq
->tq_flags
= (flags
| TQ_ACTIVE
);
575 tq
->tq_lowest_id
= 1;
576 INIT_LIST_HEAD(&tq
->tq_free_list
);
577 INIT_LIST_HEAD(&tq
->tq_pend_list
);
578 INIT_LIST_HEAD(&tq
->tq_prio_list
);
579 init_waitqueue_head(&tq
->tq_work_waitq
);
580 init_waitqueue_head(&tq
->tq_wait_waitq
);
582 if (flags
& TASKQ_PREPOPULATE
)
583 for (i
= 0; i
< minalloc
; i
++)
584 task_done(tq
, task_alloc(tq
, TQ_SLEEP
| TQ_NEW
));
586 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
588 for (i
= 0; i
< nthreads
; i
++) {
589 tqt
= kmem_alloc(sizeof(*tqt
), KM_SLEEP
);
590 INIT_LIST_HEAD(&tqt
->tqt_thread_list
);
591 INIT_LIST_HEAD(&tqt
->tqt_active_list
);
595 tqt
->tqt_thread
= kthread_create(taskq_thread
, tqt
,
597 if (tqt
->tqt_thread
) {
598 list_add(&tqt
->tqt_thread_list
, &tq
->tq_thread_list
);
599 kthread_bind(tqt
->tqt_thread
, i
% num_online_cpus());
600 set_user_nice(tqt
->tqt_thread
, PRIO_TO_NICE(pri
));
601 wake_up_process(tqt
->tqt_thread
);
604 kmem_free(tqt
, sizeof(taskq_thread_t
));
609 /* Wait for all threads to be started before potential destroy */
610 wait_event(tq
->tq_wait_waitq
, tq
->tq_nthreads
== j
);
619 EXPORT_SYMBOL(__taskq_create
);
622 __taskq_destroy(taskq_t
*tq
)
624 struct task_struct
*thread
;
630 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
631 tq
->tq_flags
&= ~TQ_ACTIVE
;
632 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
634 /* TQ_ACTIVE cleared prevents new tasks being added to pending */
637 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
640 * Signal each thread to exit and block until it does. Each thread
641 * is responsible for removing itself from the list and freeing its
642 * taskq_thread_t. This allows for idle threads to opt to remove
643 * themselves from the taskq. They can be recreated as needed.
645 while (!list_empty(&tq
->tq_thread_list
)) {
646 tqt
= list_entry(tq
->tq_thread_list
.next
,
647 taskq_thread_t
, tqt_thread_list
);
648 thread
= tqt
->tqt_thread
;
649 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
651 kthread_stop(thread
);
653 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
656 while (!list_empty(&tq
->tq_free_list
)) {
657 t
= list_entry(tq
->tq_free_list
.next
, taskq_ent_t
, tqent_list
);
659 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
661 list_del_init(&t
->tqent_list
);
665 ASSERT(tq
->tq_nthreads
== 0);
666 ASSERT(tq
->tq_nalloc
== 0);
667 ASSERT(list_empty(&tq
->tq_thread_list
));
668 ASSERT(list_empty(&tq
->tq_active_list
));
669 ASSERT(list_empty(&tq
->tq_free_list
));
670 ASSERT(list_empty(&tq
->tq_pend_list
));
671 ASSERT(list_empty(&tq
->tq_prio_list
));
673 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
675 kmem_free(tq
, sizeof(taskq_t
));
679 EXPORT_SYMBOL(__taskq_destroy
);
686 /* Solaris creates a dynamic taskq of up to 64 threads, however in
687 * a Linux environment 1 thread per-core is usually about right */
688 system_taskq
= taskq_create("spl_system_taskq", num_online_cpus(),
689 minclsyspri
, 4, 512, TASKQ_PREPOPULATE
);
690 if (system_taskq
== NULL
)
700 taskq_destroy(system_taskq
);