2010-05-18 02:18:00 +04:00
|
|
|
/*****************************************************************************\
|
|
|
|
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
|
|
|
|
* Copyright (C) 2007 The Regents of the University of California.
|
|
|
|
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
|
|
|
|
* Written by Brian Behlendorf <behlendorf1@llnl.gov>.
|
2008-05-26 08:38:26 +04:00
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
2010-05-18 02:18:00 +04:00
|
|
|
* This file is part of the SPL, Solaris Porting Layer.
|
|
|
|
* For details, see <http://github.com/behlendorf/spl/>.
|
2008-05-26 08:38:26 +04:00
|
|
|
*
|
2010-05-18 02:18:00 +04:00
|
|
|
* The SPL 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.
|
|
|
|
*
|
|
|
|
* The SPL is distributed in the hope that it will be useful, but WITHOUT
|
2008-05-26 08:38:26 +04:00
|
|
|
* 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
|
2010-05-18 02:18:00 +04:00
|
|
|
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*****************************************************************************
|
|
|
|
* Solaris Porting Layer (SPL) Task Queue Implementation.
|
|
|
|
\*****************************************************************************/
|
2008-05-26 08:38:26 +04:00
|
|
|
|
2008-03-01 03:45:59 +03:00
|
|
|
#include <sys/taskq.h>
|
2008-08-12 02:13:47 +04:00
|
|
|
#include <sys/kmem.h>
|
2010-07-20 01:16:05 +04:00
|
|
|
#include <spl-debug.h>
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
#ifdef SS_DEBUG_SUBSYS
|
|
|
|
#undef SS_DEBUG_SUBSYS
|
2008-04-21 21:29:47 +04:00
|
|
|
#endif
|
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
#define SS_DEBUG_SUBSYS SS_TASKQ
|
2008-04-21 21:29:47 +04:00
|
|
|
|
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);
|
|
|
|
|
2011-11-12 03:06:35 +04:00
|
|
|
typedef struct taskq_ent {
|
|
|
|
spinlock_t tqent_lock;
|
|
|
|
struct list_head tqent_list;
|
|
|
|
taskqid_t tqent_id;
|
|
|
|
task_func_t *tqent_func;
|
|
|
|
void *tqent_arg;
|
|
|
|
} taskq_ent_t;
|
2008-08-12 02:13:47 +04:00
|
|
|
|
2010-01-05 02:52:26 +03:00
|
|
|
/*
|
|
|
|
* NOTE: Must be called with tq->tq_lock held, returns a list_t which
|
2008-04-26 02:10:47 +04:00
|
|
|
* is not attached to the free, work, or pending taskq lists.
|
2008-02-26 23:36:04 +03:00
|
|
|
*/
|
2011-11-12 03:06:35 +04:00
|
|
|
static taskq_ent_t *
|
2008-04-26 02:10:47 +04:00
|
|
|
task_alloc(taskq_t *tq, uint_t flags)
|
|
|
|
{
|
2011-11-12 03:06:35 +04:00
|
|
|
taskq_ent_t *t;
|
2008-04-26 02:10:47 +04:00
|
|
|
int count = 0;
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
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:
|
2011-11-12 03:06:35 +04:00
|
|
|
/* Acquire taskq_ent_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)) {
|
2011-11-12 03:06:35 +04:00
|
|
|
t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
|
|
|
|
list_del_init(&t->tqent_list);
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(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)
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(NULL);
|
2008-04-26 02:10:47 +04:00
|
|
|
|
2011-11-12 03:06:35 +04:00
|
|
|
/* Hit maximum taskq_ent_t pool size */
|
2008-04-26 02:10:47 +04:00
|
|
|
if (tq->tq_nalloc >= tq->tq_maxalloc) {
|
|
|
|
if (flags & TQ_NOSLEEP)
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(NULL);
|
2008-04-26 02:10:47 +04:00
|
|
|
|
2010-08-02 13:24:01 +04:00
|
|
|
/*
|
|
|
|
* Sleep periodically polling the free list for an available
|
2011-11-12 03:06:35 +04:00
|
|
|
* taskq_ent_t. Dispatching with TQ_SLEEP should always succeed
|
|
|
|
* but we cannot block forever waiting for an taskq_entq_t to
|
2010-08-02 13:24:01 +04:00
|
|
|
* show up in the free list, otherwise a deadlock can happen.
|
|
|
|
*
|
|
|
|
* Therefore, we need to allocate a new task even if the number
|
|
|
|
* of allocated tasks is above tq->tq_maxalloc, but we still
|
|
|
|
* end up delaying the task allocation by one second, thereby
|
|
|
|
* throttling the task dispatch rate.
|
|
|
|
*/
|
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
schedule_timeout(HZ / 100);
|
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
if (count < 100)
|
|
|
|
SGOTO(retry, count++);
|
2008-04-26 02:10:47 +04:00
|
|
|
}
|
|
|
|
|
2010-08-02 13:24:01 +04:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2011-11-12 03:06:35 +04:00
|
|
|
t = kmem_alloc(sizeof(taskq_ent_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
|
|
|
|
2010-08-02 13:24:01 +04:00
|
|
|
if (t) {
|
2011-11-12 03:06:35 +04:00
|
|
|
spin_lock_init(&t->tqent_lock);
|
|
|
|
INIT_LIST_HEAD(&t->tqent_list);
|
|
|
|
t->tqent_id = 0;
|
|
|
|
t->tqent_func = NULL;
|
|
|
|
t->tqent_arg = NULL;
|
2010-08-02 13:24:01 +04:00
|
|
|
tq->tq_nalloc++;
|
|
|
|
}
|
2008-04-26 02:10:47 +04:00
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(t);
|
2008-04-26 02:10:47 +04:00
|
|
|
}
|
|
|
|
|
2010-01-05 02:52:26 +03:00
|
|
|
/*
|
2011-11-12 03:06:35 +04:00
|
|
|
* NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t
|
2008-04-26 02:10:47 +04:00
|
|
|
* to already be removed from the free, work, or pending taskq lists.
|
|
|
|
*/
|
|
|
|
static void
|
2011-11-12 03:06:35 +04:00
|
|
|
task_free(taskq_t *tq, taskq_ent_t *t)
|
2008-04-26 02:10:47 +04:00
|
|
|
{
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(t);
|
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
2011-11-12 03:06:35 +04:00
|
|
|
ASSERT(list_empty(&t->tqent_list));
|
2008-04-26 02:10:47 +04:00
|
|
|
|
2011-11-12 03:06:35 +04:00
|
|
|
kmem_free(t, sizeof(taskq_ent_t));
|
2008-04-26 02:10:47 +04:00
|
|
|
tq->tq_nalloc--;
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SEXIT;
|
2008-04-26 02:10:47 +04:00
|
|
|
}
|
|
|
|
|
2010-01-05 02:52:26 +03:00
|
|
|
/*
|
|
|
|
* NOTE: Must be called with tq->tq_lock held, either destroys the
|
2011-11-12 03:06:35 +04:00
|
|
|
* taskq_ent_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
|
2011-11-12 03:06:35 +04:00
|
|
|
task_done(taskq_t *tq, taskq_ent_t *t)
|
2008-02-26 23:36:04 +03:00
|
|
|
{
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
2008-04-26 02:10:47 +04:00
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(t);
|
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
|
|
|
|
2011-11-12 03:06:35 +04:00
|
|
|
list_del_init(&t->tqent_list);
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
if (tq->tq_nalloc <= tq->tq_minalloc) {
|
2011-11-12 03:06:35 +04:00
|
|
|
t->tqent_id = 0;
|
|
|
|
t->tqent_func = NULL;
|
|
|
|
t->tqent_arg = NULL;
|
|
|
|
list_add_tail(&t->tqent_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
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SEXIT;
|
2008-02-26 23:36:04 +03:00
|
|
|
}
|
|
|
|
|
2010-01-05 02:52:26 +03:00
|
|
|
/*
|
|
|
|
* As tasks are submitted to the task queue they are assigned a
|
2010-07-01 21:07:51 +04:00
|
|
|
* monotonically increasing taskqid and added to the tail of the pending
|
|
|
|
* list. As worker threads become available the tasks are removed from
|
|
|
|
* the head of the pending or priority list, giving preference to the
|
|
|
|
* priority list. The tasks are then added to the work list, preserving
|
|
|
|
* the ordering by taskqid. Finally, as tasks complete they are removed
|
|
|
|
* from the work list. This means that the pending and work lists are
|
|
|
|
* always kept sorted by taskqid. Thus the lowest outstanding
|
2010-01-05 02:52:26 +03:00
|
|
|
* incomplete taskqid can be determined simply by checking the min
|
2010-07-01 21:07:51 +04:00
|
|
|
* taskqid for each head item on the pending, priority, and work list.
|
|
|
|
* This value is stored in tq->tq_lowest_id and only updated to the new
|
|
|
|
* lowest id when the previous lowest id completes. All taskqids lower
|
|
|
|
* than tq->tq_lowest_id must have completed. It is also possible
|
|
|
|
* larger taskqid's have completed because they may be processed in
|
|
|
|
* parallel by several worker threads. However, this is not a problem
|
|
|
|
* because the behavior of taskq_wait_id() is to block until all
|
|
|
|
* previously submitted taskqid's have completed.
|
2010-01-05 02:52:26 +03:00
|
|
|
*
|
|
|
|
* XXX: Taskqid_t wrapping is not handled. However, taskqid_t's are
|
|
|
|
* 64-bit values so even if a taskq is processing 2^24 (16,777,216)
|
|
|
|
* taskqid_ts per second it will still take 2^40 seconds, 34,865 years,
|
|
|
|
* before the wrap occurs. I can live with that for now.
|
2008-04-26 02:10:47 +04:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(rc);
|
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
|
|
|
{
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
2008-04-26 02:10:47 +04:00
|
|
|
ASSERT(tq);
|
|
|
|
|
|
|
|
wait_event(tq->tq_wait_waitq, taskq_wait_check(tq, id));
|
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SEXIT;
|
2008-04-26 02:10:47 +04:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__taskq_wait_id);
|
|
|
|
|
|
|
|
void
|
|
|
|
__taskq_wait(taskq_t *tq)
|
|
|
|
{
|
|
|
|
taskqid_t id;
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
2008-04-26 02:10:47 +04:00
|
|
|
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);
|
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SEXIT;
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__taskq_wait);
|
|
|
|
|
|
|
|
int
|
|
|
|
__taskq_member(taskq_t *tq, void *t)
|
|
|
|
{
|
|
|
|
int i;
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(t);
|
|
|
|
|
|
|
|
for (i = 0; i < tq->tq_nthreads; i++)
|
|
|
|
if (tq->tq_threads[i] == (struct task_struct *)t)
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(1);
|
2008-04-26 02:10:47 +04:00
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(0);
|
2008-04-26 02:10:47 +04:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(__taskq_member);
|
|
|
|
|
|
|
|
taskqid_t
|
|
|
|
__taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
|
|
|
|
{
|
2011-11-12 03:06:35 +04:00
|
|
|
taskq_ent_t *t;
|
2008-04-26 02:10:47 +04:00
|
|
|
taskqid_t rc = 0;
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2008-04-21 21:29:47 +04:00
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(func);
|
2010-04-24 01:39:47 +04:00
|
|
|
|
|
|
|
/* Solaris assumes TQ_SLEEP if not passed explicitly */
|
|
|
|
if (!(flags & (TQ_SLEEP | TQ_NOSLEEP)))
|
|
|
|
flags |= TQ_SLEEP;
|
|
|
|
|
2010-07-20 01:16:05 +04:00
|
|
|
if (unlikely(in_atomic() && (flags & TQ_SLEEP)))
|
|
|
|
PANIC("May schedule while atomic: %s/0x%08x/%d\n",
|
|
|
|
current->comm, preempt_count(), current->pid);
|
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))
|
2010-07-20 22:55:37 +04:00
|
|
|
SGOTO(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))
|
2010-07-20 22:55:37 +04:00
|
|
|
SGOTO(out, rc = 0);
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
if ((t = task_alloc(tq, flags)) == NULL)
|
2010-07-20 22:55:37 +04:00
|
|
|
SGOTO(out, rc = 0);
|
2008-02-26 23:36:04 +03:00
|
|
|
|
2011-11-12 03:06:35 +04:00
|
|
|
spin_lock(&t->tqent_lock);
|
2010-07-01 21:07:51 +04:00
|
|
|
|
|
|
|
/* Queue to the priority list instead of the pending list */
|
|
|
|
if (flags & TQ_FRONT)
|
2011-11-12 03:06:35 +04:00
|
|
|
list_add_tail(&t->tqent_list, &tq->tq_prio_list);
|
2010-07-01 21:07:51 +04:00
|
|
|
else
|
2011-11-12 03:06:35 +04:00
|
|
|
list_add_tail(&t->tqent_list, &tq->tq_pend_list);
|
2010-07-01 21:07:51 +04:00
|
|
|
|
2011-11-12 03:06:35 +04:00
|
|
|
t->tqent_id = rc = tq->tq_next_id;
|
2008-04-26 02:10:47 +04:00
|
|
|
tq->tq_next_id++;
|
2011-11-12 03:06:35 +04:00
|
|
|
t->tqent_func = func;
|
|
|
|
t->tqent_arg = arg;
|
|
|
|
spin_unlock(&t->tqent_lock);
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
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);
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(rc);
|
2008-02-26 23:36:04 +03:00
|
|
|
}
|
2008-02-27 22:09:51 +03:00
|
|
|
EXPORT_SYMBOL(__taskq_dispatch);
|
2010-01-05 02:52:26 +03:00
|
|
|
/*
|
|
|
|
* Returns the lowest incomplete taskqid_t. The taskqid_t may
|
2010-07-01 21:07:51 +04:00
|
|
|
* be queued on the pending list, on the priority list, or on
|
|
|
|
* the work list currently being handled, but it is not 100%
|
|
|
|
* complete yet.
|
2010-01-05 02:52:26 +03:00
|
|
|
*/
|
2008-04-26 02:10:47 +04:00
|
|
|
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;
|
2011-11-12 03:06:35 +04:00
|
|
|
taskq_ent_t *t;
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
|
|
|
|
2010-01-05 02:52:26 +03:00
|
|
|
if (!list_empty(&tq->tq_pend_list)) {
|
2011-11-12 03:06:35 +04:00
|
|
|
t = list_entry(tq->tq_pend_list.next, taskq_ent_t, tqent_list);
|
|
|
|
lowest_id = MIN(lowest_id, t->tqent_id);
|
2010-01-05 02:52:26 +03:00
|
|
|
}
|
2008-04-26 02:10:47 +04:00
|
|
|
|
2010-07-01 21:07:51 +04:00
|
|
|
if (!list_empty(&tq->tq_prio_list)) {
|
2011-11-12 03:06:35 +04:00
|
|
|
t = list_entry(tq->tq_prio_list.next, taskq_ent_t, tqent_list);
|
|
|
|
lowest_id = MIN(lowest_id, t->tqent_id);
|
2010-07-01 21:07:51 +04:00
|
|
|
}
|
|
|
|
|
2010-01-05 02:52:26 +03:00
|
|
|
if (!list_empty(&tq->tq_work_list)) {
|
2011-11-12 03:06:35 +04:00
|
|
|
t = list_entry(tq->tq_work_list.next, taskq_ent_t, tqent_list);
|
|
|
|
lowest_id = MIN(lowest_id, t->tqent_id);
|
2010-01-05 02:52:26 +03:00
|
|
|
}
|
2008-04-26 02:10:47 +04:00
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(lowest_id);
|
2008-04-26 02:10:47 +04:00
|
|
|
}
|
|
|
|
|
2010-07-01 21:07:51 +04:00
|
|
|
/*
|
|
|
|
* Insert a task into a list keeping the list sorted by increasing
|
|
|
|
* taskqid.
|
|
|
|
*/
|
|
|
|
static void
|
2011-11-12 03:06:35 +04:00
|
|
|
taskq_insert_in_order(taskq_t *tq, taskq_ent_t *t)
|
2010-07-01 21:07:51 +04:00
|
|
|
{
|
2011-11-12 03:06:35 +04:00
|
|
|
taskq_ent_t *w;
|
2010-07-01 21:07:51 +04:00
|
|
|
struct list_head *l;
|
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
2010-07-01 21:07:51 +04:00
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(t);
|
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
|
|
|
|
|
|
|
list_for_each_prev(l, &tq->tq_work_list) {
|
2011-11-12 03:06:35 +04:00
|
|
|
w = list_entry(l, taskq_ent_t, tqent_list);
|
|
|
|
if (w->tqent_id < t->tqent_id) {
|
|
|
|
list_add(&t->tqent_list, l);
|
2010-07-01 21:07:51 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (l == &tq->tq_work_list)
|
2011-11-12 03:06:35 +04:00
|
|
|
list_add(&t->tqent_list, &tq->tq_work_list);
|
2010-07-01 21:07:51 +04:00
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SEXIT;
|
2010-07-01 21:07:51 +04:00
|
|
|
}
|
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
static int
|
|
|
|
taskq_thread(void *args)
|
|
|
|
{
|
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
|
|
|
sigset_t blocked;
|
|
|
|
taskqid_t id;
|
|
|
|
taskq_t *tq = args;
|
2011-11-12 03:06:35 +04:00
|
|
|
taskq_ent_t *t;
|
2010-07-01 21:07:51 +04:00
|
|
|
struct list_head *pend_list;
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
ASSERT(tq);
|
|
|
|
current->flags |= PF_NOFREEZE;
|
|
|
|
|
2011-05-07 02:21:58 +04:00
|
|
|
/* Disable the direct memory reclaim path */
|
|
|
|
if (tq->tq_flags & TASKQ_NORECLAIM)
|
|
|
|
current->flags |= PF_MEMALLOC;
|
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
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);
|
2010-07-01 21:07:51 +04:00
|
|
|
if (list_empty(&tq->tq_pend_list) &&
|
|
|
|
list_empty(&tq->tq_prio_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);
|
2010-07-01 21:07:51 +04:00
|
|
|
|
|
|
|
if (!list_empty(&tq->tq_prio_list))
|
|
|
|
pend_list = &tq->tq_prio_list;
|
|
|
|
else if (!list_empty(&tq->tq_pend_list))
|
|
|
|
pend_list = &tq->tq_pend_list;
|
|
|
|
else
|
|
|
|
pend_list = NULL;
|
|
|
|
|
|
|
|
if (pend_list) {
|
2011-11-12 03:06:35 +04:00
|
|
|
t = list_entry(pend_list->next, taskq_ent_t, tqent_list);
|
|
|
|
list_del_init(&t->tqent_list);
|
2010-07-01 21:07:51 +04:00
|
|
|
taskq_insert_in_order(tq, t);
|
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 */
|
2011-11-12 03:06:35 +04:00
|
|
|
t->tqent_func(t->tqent_arg);
|
2008-04-26 02:10:47 +04:00
|
|
|
|
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--;
|
2011-11-12 03:06:35 +04:00
|
|
|
id = t->tqent_id;
|
2008-04-26 02:10:47 +04:00
|
|
|
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
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(0);
|
2008-04-26 02:10:47 +04:00
|
|
|
}
|
|
|
|
|
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;
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
ASSERT(name != NULL);
|
|
|
|
ASSERT(pri <= maxclsyspri);
|
|
|
|
ASSERT(minalloc >= 0);
|
|
|
|
ASSERT(maxalloc <= INT_MAX);
|
|
|
|
ASSERT(!(flags & (TASKQ_CPR_SAFE | TASKQ_DYNAMIC))); /* Unsupported */
|
|
|
|
|
2009-07-09 21:07:52 +04:00
|
|
|
/* Scale the number of threads using nthreads as a percentage */
|
|
|
|
if (flags & TASKQ_THREADS_CPU_PCT) {
|
|
|
|
ASSERT(nthreads <= 100);
|
|
|
|
ASSERT(nthreads >= 0);
|
|
|
|
nthreads = MIN(nthreads, 100);
|
|
|
|
nthreads = MAX(nthreads, 0);
|
|
|
|
nthreads = MAX((num_online_cpus() * nthreads) / 100, 1);
|
|
|
|
}
|
|
|
|
|
2008-04-26 02:10:47 +04:00
|
|
|
tq = kmem_alloc(sizeof(*tq), KM_SLEEP);
|
|
|
|
if (tq == NULL)
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(NULL);
|
2008-04-26 02:10:47 +04:00
|
|
|
|
|
|
|
tq->tq_threads = kmem_alloc(nthreads * sizeof(t), KM_SLEEP);
|
|
|
|
if (tq->tq_threads == NULL) {
|
|
|
|
kmem_free(tq, sizeof(*tq));
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(NULL);
|
2008-04-26 02:10:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2010-07-01 21:07:51 +04:00
|
|
|
INIT_LIST_HEAD(&tq->tq_prio_list);
|
2008-04-26 02:10:47 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(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)
|
|
|
|
{
|
2011-11-12 03:06:35 +04:00
|
|
|
taskq_ent_t *t;
|
2008-04-26 02:10:47 +04:00
|
|
|
int i, nthreads;
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
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)) {
|
2011-11-12 03:06:35 +04:00
|
|
|
t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
|
|
|
|
list_del_init(&t->tqent_list);
|
2008-04-26 02:10:47 +04:00
|
|
|
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));
|
2010-07-01 21:07:51 +04:00
|
|
|
ASSERT(list_empty(&tq->tq_prio_list));
|
2008-04-26 02:10:47 +04:00
|
|
|
|
2008-11-03 23:21:08 +03:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2011-11-12 03:06:35 +04:00
|
|
|
kmem_free(tq->tq_threads, nthreads * sizeof(taskq_ent_t *));
|
2008-04-26 02:10:47 +04:00
|
|
|
kmem_free(tq, sizeof(taskq_t));
|
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SEXIT;
|
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)
|
|
|
|
{
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
2009-01-06 02:08:03 +03:00
|
|
|
|
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)
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(1);
|
2009-01-06 02:08:03 +03:00
|
|
|
|
2010-07-20 22:55:37 +04:00
|
|
|
SRETURN(0);
|
2009-01-06 02:08:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
spl_taskq_fini(void)
|
|
|
|
{
|
2010-07-20 22:55:37 +04:00
|
|
|
SENTRY;
|
2009-01-06 02:08:03 +03:00
|
|
|
taskq_destroy(system_taskq);
|
2010-07-20 22:55:37 +04:00
|
|
|
SEXIT;
|
2009-01-06 02:08:03 +03:00
|
|
|
}
|