2008-05-26 08:38:26 +04:00
|
|
|
/*
|
|
|
|
* This file is part of the SPL: Solaris Porting Layer.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Lawrence Livermore National Security, LLC.
|
|
|
|
* Produced at Lawrence Livermore National Laboratory
|
|
|
|
* Written by:
|
|
|
|
* Brian Behlendorf <behlendorf1@llnl.gov>,
|
|
|
|
* Herb Wartens <wartens2@llnl.gov>,
|
|
|
|
* Jim Garlick <garlick@llnl.gov>
|
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2008-03-01 03:45:59 +03:00
|
|
|
#include <sys/taskq.h>
|
2008-08-12 02:13:47 +04:00
|
|
|
#include <sys/kmem.h>
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-04-21 21:29:47 +04:00
|
|
|
#ifdef DEBUG_SUBSYSTEM
|
|
|
|
#undef DEBUG_SUBSYSTEM
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DEBUG_SUBSYSTEM S_TASKQ
|
|
|
|
|
2009-01-06 02:08:03 +03:00
|
|
|
/* Global system-wide dynamic task queue available for all consumers */
|
|
|
|
taskq_t *system_taskq;
|
|
|
|
EXPORT_SYMBOL(system_taskq);
|
|
|
|
|
2008-08-12 02:13:47 +04:00
|
|
|
typedef struct spl_task {
|
|
|
|
spinlock_t t_lock;
|
|
|
|
struct list_head t_list;
|
|
|
|
taskqid_t t_id;
|
|
|
|
task_func_t *t_func;
|
|
|
|
void *t_arg;
|
|
|
|
} spl_task_t;
|
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
/* NOTE: Must be called with tq->tq_lock held, returns a list_t which
|
|
|
|
* is not attached to the free, work, or pending taskq lists.
|
2008-02-26 23:36:04 +03:00
|
|
|
*/
|
2008-08-12 02:13:47 +04:00
|
|
|
static spl_task_t *
|
2008-04-26 02:10:47 +04:00
|
|
|
task_alloc(taskq_t *tq, uint_t flags)
|
|
|
|
{
|
2008-08-12 02:13:47 +04:00
|
|
|
spl_task_t *t;
|
2008-04-26 02:10:47 +04:00
|
|
|
int count = 0;
|
|
|
|
ENTRY;
|
|
|
|
|
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(flags & (TQ_SLEEP | TQ_NOSLEEP)); /* One set */
|
|
|
|
ASSERT(!((flags & TQ_SLEEP) && (flags & TQ_NOSLEEP))); /* Not both */
|
2008-08-12 02:13:47 +04:00
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
2008-04-26 02:10:47 +04:00
|
|
|
retry:
|
2009-03-16 01:13:49 +03:00
|
|
|
/* Acquire spl_task_t's from free list if available */
|
2008-04-26 02:10:47 +04:00
|
|
|
if (!list_empty(&tq->tq_free_list) && !(flags & TQ_NEW)) {
|
2008-08-12 02:13:47 +04:00
|
|
|
t = list_entry(tq->tq_free_list.next, spl_task_t, t_list);
|
|
|
|
list_del_init(&t->t_list);
|
|
|
|
RETURN(t);
|
2008-04-26 02:10:47 +04:00
|
|
|
}
|
|
|
|
|
2009-03-16 01:13:49 +03:00
|
|
|
/* Free list is empty and memory allocations are prohibited */
|
2008-04-26 02:10:47 +04:00
|
|
|
if (flags & TQ_NOALLOC)
|
|
|
|
RETURN(NULL);
|
|
|
|
|
2008-08-12 02:13:47 +04:00
|
|
|
/* Hit maximum spl_task_t pool size */
|
2008-04-26 02:10:47 +04:00
|
|
|
if (tq->tq_nalloc >= tq->tq_maxalloc) {
|
|
|
|
if (flags & TQ_NOSLEEP)
|
|
|
|
RETURN(NULL);
|
|
|
|
|
|
|
|
/* Sleep periodically polling the free list for an available
|
2008-08-12 02:13:47 +04:00
|
|
|
* spl_task_t. If a full second passes and we have not found
|
2008-04-26 02:10:47 +04:00
|
|
|
* one gives up and return a NULL to the caller. */
|
|
|
|
if (flags & TQ_SLEEP) {
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
schedule_timeout(HZ / 100);
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
if (count < 100)
|
|
|
|
GOTO(retry, count++);
|
|
|
|
|
|
|
|
RETURN(NULL);
|
|
|
|
}
|
|
|
|
|
2009-03-16 01:13:49 +03:00
|
|
|
/* Unreachable, TQ_SLEEP or TQ_NOSLEEP */
|
2008-04-26 02:10:47 +04:00
|
|
|
SBUG();
|
|
|
|
}
|
|
|
|
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-08-12 02:13:47 +04:00
|
|
|
t = kmem_alloc(sizeof(spl_task_t), flags & (TQ_SLEEP | TQ_NOSLEEP));
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
if (t) {
|
|
|
|
spin_lock_init(&t->t_lock);
|
|
|
|
INIT_LIST_HEAD(&t->t_list);
|
|
|
|
t->t_id = 0;
|
|
|
|
t->t_func = NULL;
|
|
|
|
t->t_arg = NULL;
|
|
|
|
tq->tq_nalloc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN(t);
|
|
|
|
}
|
|
|
|
|
2009-03-16 01:13:49 +03:00
|
|
|
/* NOTE: Must be called with tq->tq_lock held, expects the spl_task_t
|
2008-04-26 02:10:47 +04:00
|
|
|
* to already be removed from the free, work, or pending taskq lists.
|
|
|
|
*/
|
|
|
|
static void
|
2008-08-12 02:13:47 +04:00
|
|
|
task_free(taskq_t *tq, spl_task_t *t)
|
2008-04-26 02:10:47 +04:00
|
|
|
{
|
|
|
|
ENTRY;
|
|
|
|
|
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(t);
|
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
|
|
|
ASSERT(list_empty(&t->t_list));
|
|
|
|
|
2008-08-12 02:13:47 +04:00
|
|
|
kmem_free(t, sizeof(spl_task_t));
|
2008-04-26 02:10:47 +04:00
|
|
|
tq->tq_nalloc--;
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
EXIT;
|
|
|
|
}
|
|
|
|
|
2009-03-16 01:13:49 +03:00
|
|
|
/* NOTE: Must be called with tq->tq_lock held, either destroys the
|
2008-08-12 02:13:47 +04:00
|
|
|
* spl_task_t if too many exist or moves it to the free list for later use.
|
2008-04-26 02:10:47 +04:00
|
|
|
*/
|
2008-02-26 23:36:04 +03:00
|
|
|
static void
|
2008-08-12 02:13:47 +04:00
|
|
|
task_done(taskq_t *tq, spl_task_t *t)
|
2008-02-26 23:36:04 +03:00
|
|
|
{
|
2008-04-26 02:10:47 +04:00
|
|
|
ENTRY;
|
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(t);
|
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
|
|
|
|
|
|
|
list_del_init(&t->t_list);
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
if (tq->tq_nalloc <= tq->tq_minalloc) {
|
|
|
|
t->t_id = 0;
|
|
|
|
t->t_func = NULL;
|
|
|
|
t->t_arg = NULL;
|
2008-05-06 00:18:49 +04:00
|
|
|
list_add_tail(&t->t_list, &tq->tq_free_list);
|
2008-04-26 02:10:47 +04:00
|
|
|
} else {
|
|
|
|
task_free(tq, t);
|
|
|
|
}
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
EXIT;
|
2008-02-26 23:36:04 +03:00
|
|
|
}
|
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
/* Taskqid's are handed out in a monotonically increasing fashion per
|
2009-03-16 01:13:49 +03:00
|
|
|
* taskq_t. We don't handle taskqid wrapping yet, but fortunately it is
|
2008-04-26 02:10:47 +04:00
|
|
|
* a 64-bit value so this is probably never going to happen. The lowest
|
|
|
|
* pending taskqid is stored in the taskq_t to make it easy for any
|
|
|
|
* taskq_wait()'ers to know if the tasks they're waiting for have
|
|
|
|
* completed. Unfortunately, tq_task_lowest is kept up to date is
|
|
|
|
* a pretty brain dead way, something more clever should be done.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
taskq_wait_check(taskq_t *tq, taskqid_t id)
|
|
|
|
{
|
2009-03-16 01:13:49 +03:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
rc = (id < tq->tq_lowest_id);
|
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
|
|
|
|
RETURN(rc);
|
2008-04-26 02:10:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Expected to wait for all previously scheduled tasks to complete. We do
|
|
|
|
* not need to wait for tasked scheduled after this call to complete. In
|
2009-03-16 01:13:49 +03:00
|
|
|
* other words we do not need to drain the entire taskq. */
|
2008-04-26 02:10:47 +04:00
|
|
|
void
|
|
|
|
__taskq_wait_id(taskq_t *tq, taskqid_t id)
|
2008-02-26 23:36:04 +03:00
|
|
|
{
|
2008-04-21 21:29:47 +04:00
|
|
|
ENTRY;
|
2008-04-26 02:10:47 +04:00
|
|
|
ASSERT(tq);
|
|
|
|
|
|
|
|
wait_event(tq->tq_wait_waitq, taskq_wait_check(tq, id));
|
|
|
|
|
|
|
|
EXIT;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__taskq_wait_id);
|
|
|
|
|
|
|
|
void
|
|
|
|
__taskq_wait(taskq_t *tq)
|
|
|
|
{
|
|
|
|
taskqid_t id;
|
|
|
|
ENTRY;
|
|
|
|
ASSERT(tq);
|
|
|
|
|
2009-03-16 01:13:49 +03:00
|
|
|
/* Wait for the largest outstanding taskqid */
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2009-03-16 01:13:49 +03:00
|
|
|
id = tq->tq_next_id - 1;
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
__taskq_wait_id(tq, id);
|
|
|
|
|
|
|
|
EXIT;
|
|
|
|
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__taskq_wait);
|
|
|
|
|
|
|
|
int
|
|
|
|
__taskq_member(taskq_t *tq, void *t)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
ENTRY;
|
|
|
|
|
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(t);
|
|
|
|
|
|
|
|
for (i = 0; i < tq->tq_nthreads; i++)
|
|
|
|
if (tq->tq_threads[i] == (struct task_struct *)t)
|
|
|
|
RETURN(1);
|
|
|
|
|
|
|
|
RETURN(0);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__taskq_member);
|
|
|
|
|
|
|
|
taskqid_t
|
|
|
|
__taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
|
|
|
|
{
|
2008-08-12 02:13:47 +04:00
|
|
|
spl_task_t *t;
|
2008-04-26 02:10:47 +04:00
|
|
|
taskqid_t rc = 0;
|
|
|
|
ENTRY;
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-04-21 21:29:47 +04:00
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(func);
|
2008-04-26 02:10:47 +04:00
|
|
|
if (unlikely(in_atomic() && (flags & TQ_SLEEP))) {
|
|
|
|
CERROR("May schedule while atomic: %s/0x%08x/%d\n",
|
|
|
|
current->comm, preempt_count(), current->pid);
|
|
|
|
SBUG();
|
|
|
|
}
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
/* Taskq being destroyed and all tasks drained */
|
|
|
|
if (!(tq->tq_flags & TQ_ACTIVE))
|
|
|
|
GOTO(out, rc = 0);
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
/* Do not queue the task unless there is idle thread for it */
|
|
|
|
ASSERT(tq->tq_nactive <= tq->tq_nthreads);
|
|
|
|
if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads))
|
|
|
|
GOTO(out, rc = 0);
|
|
|
|
|
|
|
|
if ((t = task_alloc(tq, flags)) == NULL)
|
|
|
|
GOTO(out, rc = 0);
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
spin_lock(&t->t_lock);
|
2008-05-06 00:18:49 +04:00
|
|
|
list_add_tail(&t->t_list, &tq->tq_pend_list);
|
2008-04-26 02:10:47 +04:00
|
|
|
t->t_id = rc = tq->tq_next_id;
|
|
|
|
tq->tq_next_id++;
|
|
|
|
t->t_func = func;
|
|
|
|
t->t_arg = arg;
|
|
|
|
spin_unlock(&t->t_lock);
|
|
|
|
|
|
|
|
wake_up(&tq->tq_work_waitq);
|
|
|
|
out:
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
RETURN(rc);
|
2008-02-26 23:36:04 +03:00
|
|
|
}
|
2008-02-27 22:09:51 +03:00
|
|
|
EXPORT_SYMBOL(__taskq_dispatch);
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
/* NOTE: Must be called with tq->tq_lock held */
|
|
|
|
static taskqid_t
|
|
|
|
taskq_lowest_id(taskq_t *tq)
|
|
|
|
{
|
2009-03-16 01:13:49 +03:00
|
|
|
taskqid_t lowest_id = tq->tq_next_id;
|
2008-08-12 02:13:47 +04:00
|
|
|
spl_task_t *t;
|
2008-04-26 02:10:47 +04:00
|
|
|
ENTRY;
|
|
|
|
|
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
|
|
|
|
|
|
|
list_for_each_entry(t, &tq->tq_pend_list, t_list)
|
|
|
|
if (t->t_id < lowest_id)
|
|
|
|
lowest_id = t->t_id;
|
|
|
|
|
|
|
|
list_for_each_entry(t, &tq->tq_work_list, t_list)
|
|
|
|
if (t->t_id < lowest_id)
|
|
|
|
lowest_id = t->t_id;
|
|
|
|
|
|
|
|
RETURN(lowest_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
taskq_thread(void *args)
|
|
|
|
{
|
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
|
|
|
sigset_t blocked;
|
|
|
|
taskqid_t id;
|
|
|
|
taskq_t *tq = args;
|
2008-08-12 02:13:47 +04:00
|
|
|
spl_task_t *t;
|
2008-04-26 02:10:47 +04:00
|
|
|
ENTRY;
|
|
|
|
|
|
|
|
ASSERT(tq);
|
|
|
|
current->flags |= PF_NOFREEZE;
|
|
|
|
|
|
|
|
sigfillset(&blocked);
|
|
|
|
sigprocmask(SIG_BLOCK, &blocked, NULL);
|
|
|
|
flush_signals(current);
|
|
|
|
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
tq->tq_nthreads++;
|
|
|
|
wake_up(&tq->tq_wait_waitq);
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
|
|
|
|
while (!kthread_should_stop()) {
|
|
|
|
|
|
|
|
add_wait_queue(&tq->tq_work_waitq, &wait);
|
|
|
|
if (list_empty(&tq->tq_pend_list)) {
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
schedule();
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
} else {
|
|
|
|
__set_current_state(TASK_RUNNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_wait_queue(&tq->tq_work_waitq, &wait);
|
|
|
|
if (!list_empty(&tq->tq_pend_list)) {
|
2009-03-16 01:13:49 +03:00
|
|
|
t = list_entry(tq->tq_pend_list.next,spl_task_t,t_list);
|
2008-04-26 02:10:47 +04:00
|
|
|
list_del_init(&t->t_list);
|
2008-05-06 00:18:49 +04:00
|
|
|
list_add_tail(&t->t_list, &tq->tq_work_list);
|
2008-04-26 02:10:47 +04:00
|
|
|
tq->tq_nactive++;
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
/* Perform the requested task */
|
|
|
|
t->t_func(t->t_arg);
|
|
|
|
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
tq->tq_nactive--;
|
|
|
|
id = t->t_id;
|
|
|
|
task_done(tq, t);
|
|
|
|
|
2009-03-16 01:13:49 +03:00
|
|
|
/* When the current lowest outstanding taskqid is
|
|
|
|
* done calculate the new lowest outstanding id */
|
2008-04-26 02:10:47 +04:00
|
|
|
if (tq->tq_lowest_id == id) {
|
|
|
|
tq->tq_lowest_id = taskq_lowest_id(tq);
|
|
|
|
ASSERT(tq->tq_lowest_id > id);
|
|
|
|
}
|
|
|
|
|
|
|
|
wake_up_all(&tq->tq_wait_waitq);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
__set_current_state(TASK_RUNNING);
|
|
|
|
tq->tq_nthreads--;
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
RETURN(0);
|
|
|
|
}
|
|
|
|
|
2008-02-26 23:36:04 +03:00
|
|
|
taskq_t *
|
|
|
|
__taskq_create(const char *name, int nthreads, pri_t pri,
|
|
|
|
int minalloc, int maxalloc, uint_t flags)
|
|
|
|
{
|
2008-04-26 02:10:47 +04:00
|
|
|
taskq_t *tq;
|
|
|
|
struct task_struct *t;
|
|
|
|
int rc = 0, i, j = 0;
|
|
|
|
ENTRY;
|
|
|
|
|
|
|
|
ASSERT(name != NULL);
|
|
|
|
ASSERT(pri <= maxclsyspri);
|
|
|
|
ASSERT(minalloc >= 0);
|
|
|
|
ASSERT(maxalloc <= INT_MAX);
|
|
|
|
ASSERT(!(flags & (TASKQ_CPR_SAFE | TASKQ_DYNAMIC))); /* Unsupported */
|
|
|
|
|
|
|
|
tq = kmem_alloc(sizeof(*tq), KM_SLEEP);
|
|
|
|
if (tq == NULL)
|
|
|
|
RETURN(NULL);
|
|
|
|
|
|
|
|
tq->tq_threads = kmem_alloc(nthreads * sizeof(t), KM_SLEEP);
|
|
|
|
if (tq->tq_threads == NULL) {
|
|
|
|
kmem_free(tq, sizeof(*tq));
|
|
|
|
RETURN(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock_init(&tq->tq_lock);
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
tq->tq_name = name;
|
|
|
|
tq->tq_nactive = 0;
|
|
|
|
tq->tq_nthreads = 0;
|
|
|
|
tq->tq_pri = pri;
|
|
|
|
tq->tq_minalloc = minalloc;
|
|
|
|
tq->tq_maxalloc = maxalloc;
|
|
|
|
tq->tq_nalloc = 0;
|
|
|
|
tq->tq_flags = (flags | TQ_ACTIVE);
|
|
|
|
tq->tq_next_id = 1;
|
|
|
|
tq->tq_lowest_id = 1;
|
|
|
|
INIT_LIST_HEAD(&tq->tq_free_list);
|
|
|
|
INIT_LIST_HEAD(&tq->tq_work_list);
|
|
|
|
INIT_LIST_HEAD(&tq->tq_pend_list);
|
|
|
|
init_waitqueue_head(&tq->tq_work_waitq);
|
|
|
|
init_waitqueue_head(&tq->tq_wait_waitq);
|
|
|
|
|
|
|
|
if (flags & TASKQ_PREPOPULATE)
|
|
|
|
for (i = 0; i < minalloc; i++)
|
|
|
|
task_done(tq, task_alloc(tq, TQ_SLEEP | TQ_NEW));
|
2008-04-24 01:19:47 +04:00
|
|
|
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-24 01:19:47 +04:00
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
for (i = 0; i < nthreads; i++) {
|
|
|
|
t = kthread_create(taskq_thread, tq, "%s/%d", name, i);
|
|
|
|
if (t) {
|
|
|
|
tq->tq_threads[i] = t;
|
|
|
|
kthread_bind(t, i % num_online_cpus());
|
|
|
|
set_user_nice(t, PRIO_TO_NICE(pri));
|
|
|
|
wake_up_process(t);
|
|
|
|
j++;
|
|
|
|
} else {
|
|
|
|
tq->tq_threads[i] = NULL;
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for all threads to be started before potential destroy */
|
|
|
|
wait_event(tq->tq_wait_waitq, tq->tq_nthreads == j);
|
|
|
|
|
|
|
|
if (rc) {
|
|
|
|
__taskq_destroy(tq);
|
|
|
|
tq = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN(tq);
|
2008-02-26 23:36:04 +03:00
|
|
|
}
|
2008-02-27 22:09:51 +03:00
|
|
|
EXPORT_SYMBOL(__taskq_create);
|
2008-03-11 05:08:57 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
__taskq_destroy(taskq_t *tq)
|
|
|
|
{
|
2008-08-12 02:13:47 +04:00
|
|
|
spl_task_t *t;
|
2008-04-26 02:10:47 +04:00
|
|
|
int i, nthreads;
|
2008-04-21 21:29:47 +04:00
|
|
|
ENTRY;
|
2008-03-11 05:08:57 +03:00
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
ASSERT(tq);
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
tq->tq_flags &= ~TQ_ACTIVE;
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
/* TQ_ACTIVE cleared prevents new tasks being added to pending */
|
|
|
|
__taskq_wait(tq);
|
|
|
|
|
|
|
|
nthreads = tq->tq_nthreads;
|
|
|
|
for (i = 0; i < nthreads; i++)
|
|
|
|
if (tq->tq_threads[i])
|
|
|
|
kthread_stop(tq->tq_threads[i]);
|
|
|
|
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
while (!list_empty(&tq->tq_free_list)) {
|
2008-08-12 02:13:47 +04:00
|
|
|
t = list_entry(tq->tq_free_list.next, spl_task_t, t_list);
|
2008-04-26 02:10:47 +04:00
|
|
|
list_del_init(&t->t_list);
|
|
|
|
task_free(tq, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(tq->tq_nthreads == 0);
|
|
|
|
ASSERT(tq->tq_nalloc == 0);
|
|
|
|
ASSERT(list_empty(&tq->tq_free_list));
|
|
|
|
ASSERT(list_empty(&tq->tq_work_list));
|
|
|
|
ASSERT(list_empty(&tq->tq_pend_list));
|
|
|
|
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-08-12 02:13:47 +04:00
|
|
|
kmem_free(tq->tq_threads, nthreads * sizeof(spl_task_t *));
|
2008-04-26 02:10:47 +04:00
|
|
|
kmem_free(tq, sizeof(taskq_t));
|
|
|
|
|
2008-04-21 21:29:47 +04:00
|
|
|
EXIT;
|
2008-03-11 05:08:57 +03:00
|
|
|
}
|
2008-04-26 02:10:47 +04:00
|
|
|
EXPORT_SYMBOL(__taskq_destroy);
|
2009-01-06 02:08:03 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
spl_taskq_init(void)
|
|
|
|
{
|
|
|
|
ENTRY;
|
|
|
|
|
2009-02-02 19:53:53 +03:00
|
|
|
/* Solaris creates a dynamic taskq of up to 64 threads, however in
|
|
|
|
* a Linux environment 1 thread per-core is usually about right */
|
|
|
|
system_taskq = taskq_create("spl_system_taskq", num_online_cpus(),
|
|
|
|
minclsyspri, 4, 512, TASKQ_PREPOPULATE);
|
2009-01-06 02:08:03 +03:00
|
|
|
if (system_taskq == NULL)
|
|
|
|
RETURN(1);
|
|
|
|
|
|
|
|
RETURN(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
spl_taskq_fini(void)
|
|
|
|
{
|
|
|
|
ENTRY;
|
|
|
|
taskq_destroy(system_taskq);
|
|
|
|
EXIT;
|
|
|
|
}
|