]>
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 /* For prealloc'd tasks, we don't free anything. */
139 if ((!(tq
->tq_flags
& TASKQ_DYNAMIC
)) &&
140 (t
->tqent_flags
& TQENT_FLAG_PREALLOC
))
143 list_del_init(&t
->tqent_list
);
145 if (tq
->tq_nalloc
<= tq
->tq_minalloc
) {
147 t
->tqent_func
= NULL
;
150 list_add_tail(&t
->tqent_list
, &tq
->tq_free_list
);
159 * As tasks are submitted to the task queue they are assigned a
160 * monotonically increasing taskqid and added to the tail of the pending
161 * list. As worker threads become available the tasks are removed from
162 * the head of the pending or priority list, giving preference to the
163 * priority list. The tasks are then added to the work list, preserving
164 * the ordering by taskqid. Finally, as tasks complete they are removed
165 * from the work list. This means that the pending and work lists are
166 * always kept sorted by taskqid. Thus the lowest outstanding
167 * incomplete taskqid can be determined simply by checking the min
168 * taskqid for each head item on the pending, priority, and work list.
169 * This value is stored in tq->tq_lowest_id and only updated to the new
170 * lowest id when the previous lowest id completes. All taskqids lower
171 * than tq->tq_lowest_id must have completed. It is also possible
172 * larger taskqid's have completed because they may be processed in
173 * parallel by several worker threads. However, this is not a problem
174 * because the behavior of taskq_wait_id() is to block until all
175 * previously submitted taskqid's have completed.
177 * XXX: Taskqid_t wrapping is not handled. However, taskqid_t's are
178 * 64-bit values so even if a taskq is processing 2^24 (16,777,216)
179 * taskqid_ts per second it will still take 2^40 seconds, 34,865 years,
180 * before the wrap occurs. I can live with that for now.
183 taskq_wait_check(taskq_t
*tq
, taskqid_t id
)
187 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
188 rc
= (id
< tq
->tq_lowest_id
);
189 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
195 __taskq_wait_id(taskq_t
*tq
, taskqid_t id
)
200 wait_event(tq
->tq_wait_waitq
, taskq_wait_check(tq
, id
));
204 EXPORT_SYMBOL(__taskq_wait_id
);
207 __taskq_wait(taskq_t
*tq
)
213 /* Wait for the largest outstanding taskqid */
214 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
215 id
= tq
->tq_next_id
- 1;
216 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
218 __taskq_wait_id(tq
, id
);
223 EXPORT_SYMBOL(__taskq_wait
);
226 __taskq_member(taskq_t
*tq
, void *t
)
235 list_for_each(l
, &tq
->tq_thread_list
) {
236 tqt
= list_entry(l
, taskq_thread_t
, tqt_thread_list
);
237 if (tqt
->tqt_thread
== (struct task_struct
*)t
)
243 EXPORT_SYMBOL(__taskq_member
);
246 __taskq_dispatch(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
)
255 /* Solaris assumes TQ_SLEEP if not passed explicitly */
256 if (!(flags
& (TQ_SLEEP
| TQ_NOSLEEP
)))
259 if (unlikely(in_atomic() && (flags
& TQ_SLEEP
)))
260 PANIC("May schedule while atomic: %s/0x%08x/%d\n",
261 current
->comm
, preempt_count(), current
->pid
);
263 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
265 /* Taskq being destroyed and all tasks drained */
266 if (!(tq
->tq_flags
& TQ_ACTIVE
))
269 /* Do not queue the task unless there is idle thread for it */
270 ASSERT(tq
->tq_nactive
<= tq
->tq_nthreads
);
271 if ((flags
& TQ_NOQUEUE
) && (tq
->tq_nactive
== tq
->tq_nthreads
))
274 if ((t
= task_alloc(tq
, flags
)) == NULL
)
277 spin_lock(&t
->tqent_lock
);
279 /* Queue to the priority list instead of the pending list */
280 if (flags
& TQ_FRONT
)
281 list_add_tail(&t
->tqent_list
, &tq
->tq_prio_list
);
283 list_add_tail(&t
->tqent_list
, &tq
->tq_pend_list
);
285 t
->tqent_id
= rc
= tq
->tq_next_id
;
287 t
->tqent_func
= func
;
290 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
292 spin_unlock(&t
->tqent_lock
);
294 wake_up(&tq
->tq_work_waitq
);
296 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
299 EXPORT_SYMBOL(__taskq_dispatch
);
302 __taskq_dispatch_ent(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
,
309 ASSERT(!(tq
->tq_flags
& TASKQ_DYNAMIC
));
311 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
313 /* Taskq being destroyed and all tasks drained */
314 if (!(tq
->tq_flags
& TQ_ACTIVE
)) {
319 spin_lock(&t
->tqent_lock
);
322 * Mark it as a prealloc'd task. This is important
323 * to ensure that we don't free it later.
325 t
->tqent_flags
|= TQENT_FLAG_PREALLOC
;
327 /* Queue to the priority list instead of the pending list */
328 if (flags
& TQ_FRONT
)
329 list_add_tail(&t
->tqent_list
, &tq
->tq_prio_list
);
331 list_add_tail(&t
->tqent_list
, &tq
->tq_pend_list
);
333 t
->tqent_id
= tq
->tq_next_id
;
335 t
->tqent_func
= func
;
338 spin_unlock(&t
->tqent_lock
);
340 wake_up(&tq
->tq_work_waitq
);
342 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
345 EXPORT_SYMBOL(__taskq_dispatch_ent
);
348 __taskq_empty_ent(taskq_ent_t
*t
)
350 return list_empty(&t
->tqent_list
);
352 EXPORT_SYMBOL(__taskq_empty_ent
);
355 __taskq_init_ent(taskq_ent_t
*t
)
357 spin_lock_init(&t
->tqent_lock
);
358 INIT_LIST_HEAD(&t
->tqent_list
);
360 t
->tqent_func
= NULL
;
364 EXPORT_SYMBOL(__taskq_init_ent
);
367 * Returns the lowest incomplete taskqid_t. The taskqid_t may
368 * be queued on the pending list, on the priority list, or on
369 * the work list currently being handled, but it is not 100%
373 taskq_lowest_id(taskq_t
*tq
)
375 taskqid_t lowest_id
= tq
->tq_next_id
;
381 ASSERT(spin_is_locked(&tq
->tq_lock
));
383 if (!list_empty(&tq
->tq_pend_list
)) {
384 t
= list_entry(tq
->tq_pend_list
.next
, taskq_ent_t
, tqent_list
);
385 lowest_id
= MIN(lowest_id
, t
->tqent_id
);
388 if (!list_empty(&tq
->tq_prio_list
)) {
389 t
= list_entry(tq
->tq_prio_list
.next
, taskq_ent_t
, tqent_list
);
390 lowest_id
= MIN(lowest_id
, t
->tqent_id
);
393 if (!list_empty(&tq
->tq_active_list
)) {
394 tqt
= list_entry(tq
->tq_active_list
.next
, taskq_thread_t
,
396 ASSERT(tqt
->tqt_ent
!= NULL
);
397 lowest_id
= MIN(lowest_id
, tqt
->tqt_ent
->tqent_id
);
404 * Insert a task into a list keeping the list sorted by increasing
408 taskq_insert_in_order(taskq_t
*tq
, taskq_thread_t
*tqt
)
416 ASSERT(spin_is_locked(&tq
->tq_lock
));
418 list_for_each_prev(l
, &tq
->tq_active_list
) {
419 w
= list_entry(l
, taskq_thread_t
, tqt_active_list
);
420 if (w
->tqt_ent
->tqent_id
< tqt
->tqt_ent
->tqent_id
) {
421 list_add(&tqt
->tqt_active_list
, l
);
425 if (l
== &tq
->tq_active_list
)
426 list_add(&tqt
->tqt_active_list
, &tq
->tq_active_list
);
432 taskq_thread(void *args
)
434 DECLARE_WAITQUEUE(wait
, current
);
437 taskq_thread_t
*tqt
= args
;
440 struct list_head
*pend_list
;
445 current
->flags
|= PF_NOFREEZE
;
447 /* Disable the direct memory reclaim path */
448 if (tq
->tq_flags
& TASKQ_NORECLAIM
)
449 current
->flags
|= PF_MEMALLOC
;
451 sigfillset(&blocked
);
452 sigprocmask(SIG_BLOCK
, &blocked
, NULL
);
453 flush_signals(current
);
455 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
457 wake_up(&tq
->tq_wait_waitq
);
458 set_current_state(TASK_INTERRUPTIBLE
);
460 while (!kthread_should_stop()) {
462 add_wait_queue(&tq
->tq_work_waitq
, &wait
);
463 if (list_empty(&tq
->tq_pend_list
) &&
464 list_empty(&tq
->tq_prio_list
)) {
465 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
467 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
469 __set_current_state(TASK_RUNNING
);
472 remove_wait_queue(&tq
->tq_work_waitq
, &wait
);
474 if (!list_empty(&tq
->tq_prio_list
))
475 pend_list
= &tq
->tq_prio_list
;
476 else if (!list_empty(&tq
->tq_pend_list
))
477 pend_list
= &tq
->tq_pend_list
;
482 t
= list_entry(pend_list
->next
, taskq_ent_t
, tqent_list
);
483 list_del_init(&t
->tqent_list
);
484 /* In order to support recursively dispatching a
485 * preallocated taskq_ent_t, tqent_id must be
486 * stored prior to executing tqent_func. */
489 taskq_insert_in_order(tq
, tqt
);
491 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
493 /* Perform the requested task */
494 t
->tqent_func(t
->tqent_arg
);
496 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
498 list_del_init(&tqt
->tqt_active_list
);
502 /* When the current lowest outstanding taskqid is
503 * done calculate the new lowest outstanding id */
504 if (tq
->tq_lowest_id
== id
) {
505 tq
->tq_lowest_id
= taskq_lowest_id(tq
);
506 ASSERT(tq
->tq_lowest_id
> id
);
509 wake_up_all(&tq
->tq_wait_waitq
);
512 set_current_state(TASK_INTERRUPTIBLE
);
516 __set_current_state(TASK_RUNNING
);
518 list_del_init(&tqt
->tqt_thread_list
);
519 kmem_free(tqt
, sizeof(taskq_thread_t
));
521 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
527 __taskq_create(const char *name
, int nthreads
, pri_t pri
,
528 int minalloc
, int maxalloc
, uint_t flags
)
532 int rc
= 0, i
, j
= 0;
535 ASSERT(name
!= NULL
);
536 ASSERT(pri
<= maxclsyspri
);
537 ASSERT(minalloc
>= 0);
538 ASSERT(maxalloc
<= INT_MAX
);
539 ASSERT(!(flags
& (TASKQ_CPR_SAFE
| TASKQ_DYNAMIC
))); /* Unsupported */
541 /* Scale the number of threads using nthreads as a percentage */
542 if (flags
& TASKQ_THREADS_CPU_PCT
) {
543 ASSERT(nthreads
<= 100);
544 ASSERT(nthreads
>= 0);
545 nthreads
= MIN(nthreads
, 100);
546 nthreads
= MAX(nthreads
, 0);
547 nthreads
= MAX((num_online_cpus() * nthreads
) / 100, 1);
550 tq
= kmem_alloc(sizeof(*tq
), KM_SLEEP
);
554 spin_lock_init(&tq
->tq_lock
);
555 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
556 INIT_LIST_HEAD(&tq
->tq_thread_list
);
557 INIT_LIST_HEAD(&tq
->tq_active_list
);
562 tq
->tq_minalloc
= minalloc
;
563 tq
->tq_maxalloc
= maxalloc
;
565 tq
->tq_flags
= (flags
| TQ_ACTIVE
);
567 tq
->tq_lowest_id
= 1;
568 INIT_LIST_HEAD(&tq
->tq_free_list
);
569 INIT_LIST_HEAD(&tq
->tq_pend_list
);
570 INIT_LIST_HEAD(&tq
->tq_prio_list
);
571 init_waitqueue_head(&tq
->tq_work_waitq
);
572 init_waitqueue_head(&tq
->tq_wait_waitq
);
574 if (flags
& TASKQ_PREPOPULATE
)
575 for (i
= 0; i
< minalloc
; i
++)
576 task_done(tq
, task_alloc(tq
, TQ_SLEEP
| TQ_NEW
));
578 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
580 for (i
= 0; i
< nthreads
; i
++) {
581 tqt
= kmem_alloc(sizeof(*tqt
), KM_SLEEP
);
582 INIT_LIST_HEAD(&tqt
->tqt_thread_list
);
583 INIT_LIST_HEAD(&tqt
->tqt_active_list
);
587 tqt
->tqt_thread
= kthread_create(taskq_thread
, tqt
,
589 if (tqt
->tqt_thread
) {
590 list_add(&tqt
->tqt_thread_list
, &tq
->tq_thread_list
);
591 kthread_bind(tqt
->tqt_thread
, i
% num_online_cpus());
592 set_user_nice(tqt
->tqt_thread
, PRIO_TO_NICE(pri
));
593 wake_up_process(tqt
->tqt_thread
);
596 kmem_free(tqt
, sizeof(taskq_thread_t
));
601 /* Wait for all threads to be started before potential destroy */
602 wait_event(tq
->tq_wait_waitq
, tq
->tq_nthreads
== j
);
611 EXPORT_SYMBOL(__taskq_create
);
614 __taskq_destroy(taskq_t
*tq
)
616 struct task_struct
*thread
;
622 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
623 tq
->tq_flags
&= ~TQ_ACTIVE
;
624 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
626 /* TQ_ACTIVE cleared prevents new tasks being added to pending */
629 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
632 * Signal each thread to exit and block until it does. Each thread
633 * is responsible for removing itself from the list and freeing its
634 * taskq_thread_t. This allows for idle threads to opt to remove
635 * themselves from the taskq. They can be recreated as needed.
637 while (!list_empty(&tq
->tq_thread_list
)) {
638 tqt
= list_entry(tq
->tq_thread_list
.next
,
639 taskq_thread_t
, tqt_thread_list
);
640 thread
= tqt
->tqt_thread
;
641 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
643 kthread_stop(thread
);
645 spin_lock_irqsave(&tq
->tq_lock
, tq
->tq_lock_flags
);
648 while (!list_empty(&tq
->tq_free_list
)) {
649 t
= list_entry(tq
->tq_free_list
.next
, taskq_ent_t
, tqent_list
);
651 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
653 list_del_init(&t
->tqent_list
);
657 ASSERT(tq
->tq_nthreads
== 0);
658 ASSERT(tq
->tq_nalloc
== 0);
659 ASSERT(list_empty(&tq
->tq_thread_list
));
660 ASSERT(list_empty(&tq
->tq_active_list
));
661 ASSERT(list_empty(&tq
->tq_free_list
));
662 ASSERT(list_empty(&tq
->tq_pend_list
));
663 ASSERT(list_empty(&tq
->tq_prio_list
));
665 spin_unlock_irqrestore(&tq
->tq_lock
, tq
->tq_lock_flags
);
667 kmem_free(tq
, sizeof(taskq_t
));
671 EXPORT_SYMBOL(__taskq_destroy
);
678 /* Solaris creates a dynamic taskq of up to 64 threads, however in
679 * a Linux environment 1 thread per-core is usually about right */
680 system_taskq
= taskq_create("spl_system_taskq", num_online_cpus(),
681 minclsyspri
, 4, 512, TASKQ_PREPOPULATE
);
682 if (system_taskq
== NULL
)
692 taskq_destroy(system_taskq
);