2010-05-17 22:18:00 +00: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 04:38:26 +00:00
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* This file is part of the SPL, Solaris Porting Layer.
|
2013-03-05 01:26:55 +00:00
|
|
|
* For details, see <http://zfsonlinux.org/>.
|
2008-05-26 04:38:26 +00:00
|
|
|
*
|
2010-05-17 22:18:00 +00: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 04:38:26 +00: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-17 22:18:00 +00:00
|
|
|
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*****************************************************************************
|
|
|
|
* Solaris Porting Layer (SPL) Task Queue Implementation.
|
|
|
|
\*****************************************************************************/
|
2008-05-26 04:38:26 +00:00
|
|
|
|
2008-03-01 00:45:59 +00:00
|
|
|
#include <sys/taskq.h>
|
2008-08-11 22:13:47 +00:00
|
|
|
#include <sys/kmem.h>
|
2010-07-19 21:16:05 +00:00
|
|
|
#include <spl-debug.h>
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2010-07-20 18:55:37 +00:00
|
|
|
#ifdef SS_DEBUG_SUBSYS
|
|
|
|
#undef SS_DEBUG_SUBSYS
|
2008-04-21 17:29:47 +00:00
|
|
|
#endif
|
|
|
|
|
2010-07-20 18:55:37 +00:00
|
|
|
#define SS_DEBUG_SUBSYS SS_TASKQ
|
2008-04-21 17:29:47 +00:00
|
|
|
|
2009-01-05 23:08:03 +00:00
|
|
|
/* Global system-wide dynamic task queue available for all consumers */
|
|
|
|
taskq_t *system_taskq;
|
|
|
|
EXPORT_SYMBOL(system_taskq);
|
|
|
|
|
2012-09-12 18:31:39 +00:00
|
|
|
static int
|
|
|
|
task_km_flags(uint_t flags)
|
|
|
|
{
|
|
|
|
if (flags & TQ_NOSLEEP)
|
|
|
|
return KM_NOSLEEP;
|
|
|
|
|
|
|
|
if (flags & TQ_PUSHPAGE)
|
|
|
|
return KM_PUSHPAGE;
|
|
|
|
|
|
|
|
return KM_SLEEP;
|
|
|
|
}
|
|
|
|
|
2010-01-04 23:52:26 +00:00
|
|
|
/*
|
|
|
|
* NOTE: Must be called with tq->tq_lock held, returns a list_t which
|
2008-04-25 22:10:47 +00:00
|
|
|
* is not attached to the free, work, or pending taskq lists.
|
2008-02-26 20:36:04 +00:00
|
|
|
*/
|
2011-11-11 23:06:35 +00:00
|
|
|
static taskq_ent_t *
|
2008-04-25 22:10:47 +00:00
|
|
|
task_alloc(taskq_t *tq, uint_t flags)
|
|
|
|
{
|
2012-12-06 20:57:42 +00:00
|
|
|
taskq_ent_t *t;
|
|
|
|
int count = 0;
|
|
|
|
SENTRY;
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
2008-04-25 22:10:47 +00:00
|
|
|
retry:
|
2012-12-06 20:57:42 +00:00
|
|
|
/* Acquire taskq_ent_t's from free list if available */
|
|
|
|
if (!list_empty(&tq->tq_free_list) && !(flags & TQ_NEW)) {
|
|
|
|
t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
|
|
|
|
|
|
|
|
ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
|
2012-12-06 20:38:19 +00:00
|
|
|
ASSERT(!(t->tqent_flags & TQENT_FLAG_CANCEL));
|
|
|
|
ASSERT(!timer_pending(&t->tqent_timer));
|
2012-12-06 20:57:42 +00:00
|
|
|
|
|
|
|
list_del_init(&t->tqent_list);
|
|
|
|
SRETURN(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free list is empty and memory allocations are prohibited */
|
|
|
|
if (flags & TQ_NOALLOC)
|
|
|
|
SRETURN(NULL);
|
|
|
|
|
|
|
|
/* Hit maximum taskq_ent_t pool size */
|
|
|
|
if (tq->tq_nalloc >= tq->tq_maxalloc) {
|
|
|
|
if (flags & TQ_NOSLEEP)
|
|
|
|
SRETURN(NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sleep periodically polling the free list for an available
|
|
|
|
* taskq_ent_t. Dispatching with TQ_SLEEP should always succeed
|
|
|
|
* but we cannot block forever waiting for an taskq_ent_t to
|
|
|
|
* 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++);
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
t = kmem_alloc(sizeof(taskq_ent_t), task_km_flags(flags));
|
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
|
|
|
|
if (t) {
|
|
|
|
taskq_init_ent(t);
|
|
|
|
tq->tq_nalloc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
SRETURN(t);
|
2008-04-25 22:10:47 +00:00
|
|
|
}
|
|
|
|
|
2010-01-04 23:52:26 +00:00
|
|
|
/*
|
2011-11-11 23:06:35 +00:00
|
|
|
* NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t
|
2008-04-25 22:10:47 +00:00
|
|
|
* to already be removed from the free, work, or pending taskq lists.
|
|
|
|
*/
|
|
|
|
static void
|
2011-11-11 23:06:35 +00:00
|
|
|
task_free(taskq_t *tq, taskq_ent_t *t)
|
2008-04-25 22:10:47 +00:00
|
|
|
{
|
2012-12-06 20:57:42 +00:00
|
|
|
SENTRY;
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(t);
|
2008-04-25 22:10:47 +00:00
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
2011-11-11 23:06:35 +00:00
|
|
|
ASSERT(list_empty(&t->tqent_list));
|
2012-12-06 20:38:19 +00:00
|
|
|
ASSERT(!timer_pending(&t->tqent_timer));
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
kmem_free(t, sizeof(taskq_ent_t));
|
|
|
|
tq->tq_nalloc--;
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2010-07-20 18:55:37 +00:00
|
|
|
SEXIT;
|
2008-04-25 22:10:47 +00:00
|
|
|
}
|
|
|
|
|
2010-01-04 23:52:26 +00:00
|
|
|
/*
|
|
|
|
* NOTE: Must be called with tq->tq_lock held, either destroys the
|
2011-11-11 23:06:35 +00:00
|
|
|
* taskq_ent_t if too many exist or moves it to the free list for later use.
|
2008-04-25 22:10:47 +00:00
|
|
|
*/
|
2008-02-26 20:36:04 +00:00
|
|
|
static void
|
2011-11-11 23:06:35 +00:00
|
|
|
task_done(taskq_t *tq, taskq_ent_t *t)
|
2008-02-26 20:36:04 +00:00
|
|
|
{
|
2010-07-20 18:55:37 +00:00
|
|
|
SENTRY;
|
2008-04-25 22:10:47 +00:00
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(t);
|
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
|
|
|
|
2012-12-06 20:38:19 +00:00
|
|
|
/* Wake tasks blocked in taskq_wait_id() */
|
|
|
|
wake_up_all(&t->tqent_waitq);
|
|
|
|
|
2011-11-11 23:06:35 +00:00
|
|
|
list_del_init(&t->tqent_list);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
if (tq->tq_nalloc <= tq->tq_minalloc) {
|
2011-11-11 23:06:35 +00:00
|
|
|
t->tqent_id = 0;
|
|
|
|
t->tqent_func = NULL;
|
|
|
|
t->tqent_arg = NULL;
|
2011-12-06 18:04:51 +00:00
|
|
|
t->tqent_flags = 0;
|
Store copy of tqent_flags prior to servicing task
A preallocated taskq_ent_t's tqent_flags must be checked prior to
servicing the taskq_ent_t. Once a preallocated taskq entry is serviced,
the ownership of the entry is handed back to the caller of
taskq_dispatch, thus the entry's contents can potentially be mangled.
In particular, this is a problem in the case where a preallocated taskq
entry is serviced, and the caller clears it's tqent_flags field. Thus,
when the function returns and task_done is called, it looks as though
the entry is **not** a preallocated task (when in fact it **is** a
preallocated task).
In this situation, task_done will place the preallocated taskq_ent_t
structure onto the taskq_t's free list. This is a **huge** mistake. If
the taskq_ent_t is then freed by the caller of taskq_dispatch, the
taskq_t's free list will hold a pointer to garbage data. Even worse, if
nothing has over written the freed memory before the pointer is
dereferenced, it may still look as though it points to a valid list_head
belonging to a taskq_ent_t structure.
Thus, the task entry's flags are now copied prior to servicing the task.
This copy is then checked to see if it is a preallocated task, and
determine if the entry needs to be passed down to the task_done
function.
Signed-off-by: Prakash Surya <surya1@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #71
2011-12-16 22:57:31 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
list_add_tail(&t->tqent_list, &tq->tq_free_list);
|
2008-04-25 22:10:47 +00:00
|
|
|
} else {
|
|
|
|
task_free(tq, t);
|
|
|
|
}
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
SEXIT;
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
2010-01-04 23:52:26 +00:00
|
|
|
/*
|
2012-12-06 20:38:19 +00:00
|
|
|
* When a delayed task timer expires remove it from the delay list and
|
|
|
|
* add it to the priority list in order for immediate processing.
|
2008-04-25 22:10:47 +00:00
|
|
|
*/
|
2012-12-06 20:38:19 +00:00
|
|
|
static void
|
|
|
|
task_expire(unsigned long data)
|
2008-04-25 22:10:47 +00:00
|
|
|
{
|
2012-12-06 20:38:19 +00:00
|
|
|
taskq_ent_t *w, *t = (taskq_ent_t *)data;
|
|
|
|
taskq_t *tq = t->tqent_taskq;
|
|
|
|
struct list_head *l;
|
2009-03-15 22:13:49 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2012-12-06 20:38:19 +00:00
|
|
|
|
|
|
|
if (t->tqent_flags & TQENT_FLAG_CANCEL) {
|
|
|
|
ASSERT(list_empty(&t->tqent_list));
|
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The priority list must be maintained in strict task id order
|
|
|
|
* from lowest to highest for lowest_id to be easily calculable.
|
|
|
|
*/
|
|
|
|
list_del(&t->tqent_list);
|
|
|
|
list_for_each_prev(l, &tq->tq_prio_list) {
|
|
|
|
w = list_entry(l, taskq_ent_t, tqent_list);
|
|
|
|
if (w->tqent_id < t->tqent_id) {
|
|
|
|
list_add(&t->tqent_list, l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (l == &tq->tq_prio_list)
|
|
|
|
list_add(&t->tqent_list, &tq->tq_prio_list);
|
|
|
|
|
2009-03-15 22:13:49 +00:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
|
2012-12-06 20:38:19 +00:00
|
|
|
wake_up(&tq->tq_work_waitq);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the lowest incomplete taskqid_t. The taskqid_t may
|
|
|
|
* be queued on the pending list, on the priority list, on the
|
|
|
|
* delay list, or on the work list currently being handled, but
|
|
|
|
* it is not 100% complete yet.
|
|
|
|
*/
|
|
|
|
static taskqid_t
|
|
|
|
taskq_lowest_id(taskq_t *tq)
|
|
|
|
{
|
|
|
|
taskqid_t lowest_id = tq->tq_next_id;
|
|
|
|
taskq_ent_t *t;
|
|
|
|
taskq_thread_t *tqt;
|
|
|
|
SENTRY;
|
|
|
|
|
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
|
|
|
|
|
|
|
if (!list_empty(&tq->tq_pend_list)) {
|
|
|
|
t = list_entry(tq->tq_pend_list.next, taskq_ent_t, tqent_list);
|
|
|
|
lowest_id = MIN(lowest_id, t->tqent_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!list_empty(&tq->tq_prio_list)) {
|
|
|
|
t = list_entry(tq->tq_prio_list.next, taskq_ent_t, tqent_list);
|
|
|
|
lowest_id = MIN(lowest_id, t->tqent_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!list_empty(&tq->tq_delay_list)) {
|
|
|
|
t = list_entry(tq->tq_delay_list.next, taskq_ent_t, tqent_list);
|
|
|
|
lowest_id = MIN(lowest_id, t->tqent_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!list_empty(&tq->tq_active_list)) {
|
|
|
|
tqt = list_entry(tq->tq_active_list.next, taskq_thread_t,
|
|
|
|
tqt_active_list);
|
|
|
|
ASSERT(tqt->tqt_id != 0);
|
|
|
|
lowest_id = MIN(lowest_id, tqt->tqt_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
SRETURN(lowest_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert a task into a list keeping the list sorted by increasing taskqid.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
taskq_insert_in_order(taskq_t *tq, taskq_thread_t *tqt)
|
|
|
|
{
|
|
|
|
taskq_thread_t *w;
|
|
|
|
struct list_head *l;
|
|
|
|
|
|
|
|
SENTRY;
|
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(tqt);
|
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
|
|
|
|
|
|
|
list_for_each_prev(l, &tq->tq_active_list) {
|
|
|
|
w = list_entry(l, taskq_thread_t, tqt_active_list);
|
|
|
|
if (w->tqt_id < tqt->tqt_id) {
|
|
|
|
list_add(&tqt->tqt_active_list, l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (l == &tq->tq_active_list)
|
|
|
|
list_add(&tqt->tqt_active_list, &tq->tq_active_list);
|
|
|
|
|
|
|
|
SEXIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find and return a task from the given list if it exists. The list
|
|
|
|
* must be in lowest to highest task id order.
|
|
|
|
*/
|
|
|
|
static taskq_ent_t *
|
|
|
|
taskq_find_list(taskq_t *tq, struct list_head *lh, taskqid_t id)
|
|
|
|
{
|
|
|
|
struct list_head *l;
|
|
|
|
taskq_ent_t *t;
|
|
|
|
SENTRY;
|
|
|
|
|
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
|
|
|
|
|
|
|
list_for_each(l, lh) {
|
|
|
|
t = list_entry(l, taskq_ent_t, tqent_list);
|
|
|
|
|
|
|
|
if (t->tqent_id == id)
|
|
|
|
SRETURN(t);
|
|
|
|
|
|
|
|
if (t->tqent_id > id)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SRETURN(NULL);
|
2008-04-25 22:10:47 +00:00
|
|
|
}
|
|
|
|
|
2012-12-06 20:38:19 +00:00
|
|
|
/*
|
|
|
|
* Find an already dispatched task given the task id regardless of what
|
|
|
|
* state it is in. If a task is still pending or executing it will be
|
|
|
|
* returned and 'active' set appropriately. If the task has already
|
|
|
|
* been run then NULL is returned.
|
|
|
|
*/
|
|
|
|
static taskq_ent_t *
|
|
|
|
taskq_find(taskq_t *tq, taskqid_t id, int *active)
|
|
|
|
{
|
|
|
|
taskq_thread_t *tqt;
|
|
|
|
struct list_head *l;
|
|
|
|
taskq_ent_t *t;
|
|
|
|
SENTRY;
|
|
|
|
|
|
|
|
ASSERT(spin_is_locked(&tq->tq_lock));
|
|
|
|
*active = 0;
|
|
|
|
|
|
|
|
t = taskq_find_list(tq, &tq->tq_delay_list, id);
|
|
|
|
if (t)
|
|
|
|
SRETURN(t);
|
|
|
|
|
|
|
|
t = taskq_find_list(tq, &tq->tq_prio_list, id);
|
|
|
|
if (t)
|
|
|
|
SRETURN(t);
|
|
|
|
|
|
|
|
t = taskq_find_list(tq, &tq->tq_pend_list, id);
|
|
|
|
if (t)
|
|
|
|
SRETURN(t);
|
|
|
|
|
|
|
|
list_for_each(l, &tq->tq_active_list) {
|
|
|
|
tqt = list_entry(l, taskq_thread_t, tqt_active_list);
|
|
|
|
if (tqt->tqt_id == id) {
|
|
|
|
t = tqt->tqt_task;
|
|
|
|
*active = 1;
|
|
|
|
SRETURN(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SRETURN(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The taskq_wait_id() function blocks until the passed task id completes.
|
|
|
|
* This does not guarantee that all lower task id's have completed.
|
|
|
|
*/
|
2008-04-25 22:10:47 +00:00
|
|
|
void
|
2012-12-06 21:04:27 +00:00
|
|
|
taskq_wait_id(taskq_t *tq, taskqid_t id)
|
2008-02-26 20:36:04 +00:00
|
|
|
{
|
2012-12-06 20:38:19 +00:00
|
|
|
DEFINE_WAIT(wait);
|
|
|
|
taskq_ent_t *t;
|
|
|
|
int active = 0;
|
2010-07-20 18:55:37 +00:00
|
|
|
SENTRY;
|
2012-12-06 20:38:19 +00:00
|
|
|
|
2008-04-25 22:10:47 +00:00
|
|
|
ASSERT(tq);
|
2012-12-06 20:38:19 +00:00
|
|
|
ASSERT(id > 0);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:38:19 +00:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
t = taskq_find(tq, id, &active);
|
|
|
|
if (t)
|
|
|
|
prepare_to_wait(&t->tqent_waitq, &wait, TASK_UNINTERRUPTIBLE);
|
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We rely on the kernels autoremove_wake_function() function to
|
|
|
|
* remove us from the wait queue in the context of wake_up().
|
|
|
|
* Once woken the taskq_ent_t pointer must never be accessed.
|
|
|
|
*/
|
|
|
|
if (t) {
|
|
|
|
t = NULL;
|
|
|
|
schedule();
|
|
|
|
__set_current_state(TASK_RUNNING);
|
|
|
|
}
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2010-07-20 18:55:37 +00:00
|
|
|
SEXIT;
|
2008-04-25 22:10:47 +00:00
|
|
|
}
|
2012-12-06 21:04:27 +00:00
|
|
|
EXPORT_SYMBOL(taskq_wait_id);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:38:19 +00:00
|
|
|
/*
|
|
|
|
* The taskq_wait() function will block until all previously submitted
|
|
|
|
* tasks have been completed. A previously submitted task is defined as
|
|
|
|
* a task with a lower task id than the current task queue id. Note that
|
|
|
|
* all task id's are assigned monotonically at dispatch time.
|
|
|
|
*
|
|
|
|
* Waiting for all previous tasks to complete is accomplished by tracking
|
|
|
|
* the lowest outstanding task id. As tasks are dispatched they are added
|
|
|
|
* added to the tail of the pending, priority, or delay lists. And as
|
|
|
|
* worker threads become available the tasks are removed from the heads
|
|
|
|
* of these lists and linked to the worker threads. This ensures the
|
|
|
|
* lists are kept in lowest to highest task id order.
|
|
|
|
*
|
|
|
|
* Therefore the lowest outstanding task id can be quickly determined by
|
|
|
|
* checking the head item from all of these lists. This value is stored
|
|
|
|
* with the task queue as the lowest id. It only needs to be recalculated
|
|
|
|
* when either the task with the current lowest id completes or is canceled.
|
|
|
|
*
|
|
|
|
* By blocking until the lowest task id exceeds the current task id when
|
|
|
|
* the function was called we ensure all previous tasks have completed.
|
|
|
|
*
|
|
|
|
* NOTE: When there are multiple worked threads it is possible for larger
|
|
|
|
* task ids to complete before smaller ones. Conversely when the task
|
|
|
|
* queue contains delay tasks with small task ids, you may block for a
|
|
|
|
* considerable length of time waiting for them to expire and execute.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
taskq_wait_check(taskq_t *tq, taskqid_t id)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
SRETURN(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
taskq_wait_all(taskq_t *tq, taskqid_t id)
|
|
|
|
{
|
|
|
|
wait_event(tq->tq_wait_waitq, taskq_wait_check(tq, id));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(taskq_wait_all);
|
|
|
|
|
2008-04-25 22:10:47 +00:00
|
|
|
void
|
2012-12-06 21:04:27 +00:00
|
|
|
taskq_wait(taskq_t *tq)
|
2008-04-25 22:10:47 +00:00
|
|
|
{
|
|
|
|
taskqid_t id;
|
2010-07-20 18:55:37 +00:00
|
|
|
SENTRY;
|
2008-04-25 22:10:47 +00:00
|
|
|
ASSERT(tq);
|
|
|
|
|
2009-03-15 22:13:49 +00:00
|
|
|
/* Wait for the largest outstanding taskqid */
|
2008-11-03 20:21:08 +00:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2009-03-15 22:13:49 +00:00
|
|
|
id = tq->tq_next_id - 1;
|
2008-11-03 20:21:08 +00:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:38:19 +00:00
|
|
|
taskq_wait_all(tq, id);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2010-07-20 18:55:37 +00:00
|
|
|
SEXIT;
|
2008-04-25 22:10:47 +00:00
|
|
|
|
|
|
|
}
|
2012-12-06 21:04:27 +00:00
|
|
|
EXPORT_SYMBOL(taskq_wait);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
|
|
|
int
|
2012-12-06 21:04:27 +00:00
|
|
|
taskq_member(taskq_t *tq, void *t)
|
2008-04-25 22:10:47 +00:00
|
|
|
{
|
2011-12-06 01:32:48 +00:00
|
|
|
struct list_head *l;
|
|
|
|
taskq_thread_t *tqt;
|
2012-12-06 20:57:42 +00:00
|
|
|
SENTRY;
|
2008-04-25 22:10:47 +00:00
|
|
|
|
|
|
|
ASSERT(tq);
|
2012-12-06 20:57:42 +00:00
|
|
|
ASSERT(t);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2011-12-06 01:32:48 +00:00
|
|
|
list_for_each(l, &tq->tq_thread_list) {
|
|
|
|
tqt = list_entry(l, taskq_thread_t, tqt_thread_list);
|
|
|
|
if (tqt->tqt_thread == (struct task_struct *)t)
|
|
|
|
SRETURN(1);
|
|
|
|
}
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
SRETURN(0);
|
2008-04-25 22:10:47 +00:00
|
|
|
}
|
2012-12-06 21:04:27 +00:00
|
|
|
EXPORT_SYMBOL(taskq_member);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:38:19 +00:00
|
|
|
/*
|
|
|
|
* Cancel an already dispatched task given the task id. Still pending tasks
|
|
|
|
* will be immediately canceled, and if the task is active the function will
|
|
|
|
* block until it completes. Preallocated tasks which are canceled must be
|
|
|
|
* freed by the caller.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
taskq_cancel_id(taskq_t *tq, taskqid_t id)
|
|
|
|
{
|
|
|
|
taskq_ent_t *t;
|
|
|
|
int active = 0;
|
|
|
|
int rc = ENOENT;
|
|
|
|
SENTRY;
|
|
|
|
|
|
|
|
ASSERT(tq);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
t = taskq_find(tq, id, &active);
|
|
|
|
if (t && !active) {
|
|
|
|
list_del_init(&t->tqent_list);
|
|
|
|
t->tqent_flags |= TQENT_FLAG_CANCEL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When canceling the lowest outstanding task id we
|
|
|
|
* must recalculate the new lowest outstanding id.
|
|
|
|
*/
|
|
|
|
if (tq->tq_lowest_id == t->tqent_id) {
|
|
|
|
tq->tq_lowest_id = taskq_lowest_id(tq);
|
|
|
|
ASSERT3S(tq->tq_lowest_id, >, t->tqent_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The task_expire() function takes the tq->tq_lock so drop
|
|
|
|
* drop the lock before synchronously cancelling the timer.
|
|
|
|
*/
|
|
|
|
if (timer_pending(&t->tqent_timer)) {
|
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
del_timer_sync(&t->tqent_timer);
|
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(t->tqent_flags & TQENT_FLAG_PREALLOC))
|
|
|
|
task_done(tq, t);
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
|
|
|
|
if (active) {
|
|
|
|
taskq_wait_id(tq, id);
|
|
|
|
rc = EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
SRETURN(rc);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(taskq_cancel_id);
|
|
|
|
|
2008-04-25 22:10:47 +00:00
|
|
|
taskqid_t
|
2012-12-06 21:04:27 +00:00
|
|
|
taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
|
2008-04-25 22:10:47 +00:00
|
|
|
{
|
2012-12-06 20:57:42 +00:00
|
|
|
taskq_ent_t *t;
|
2008-04-25 22:10:47 +00:00
|
|
|
taskqid_t rc = 0;
|
2012-12-06 20:57:42 +00:00
|
|
|
SENTRY;
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(func);
|
2010-04-23 21:39:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-04-25 22:10:47 +00:00
|
|
|
/* Taskq being destroyed and all tasks drained */
|
|
|
|
if (!(tq->tq_flags & TQ_ACTIVE))
|
2010-07-20 18:55:37 +00:00
|
|
|
SGOTO(out, rc = 0);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-04-25 22:10:47 +00: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 18:55:37 +00:00
|
|
|
SGOTO(out, rc = 0);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
if ((t = task_alloc(tq, flags)) == NULL)
|
2010-07-20 18:55:37 +00:00
|
|
|
SGOTO(out, rc = 0);
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2011-11-11 23:06:35 +00:00
|
|
|
spin_lock(&t->tqent_lock);
|
2010-07-01 17:07:51 +00:00
|
|
|
|
|
|
|
/* Queue to the priority list instead of the pending list */
|
|
|
|
if (flags & TQ_FRONT)
|
2011-11-11 23:06:35 +00:00
|
|
|
list_add_tail(&t->tqent_list, &tq->tq_prio_list);
|
2010-07-01 17:07:51 +00:00
|
|
|
else
|
2011-11-11 23:06:35 +00:00
|
|
|
list_add_tail(&t->tqent_list, &tq->tq_pend_list);
|
2010-07-01 17:07:51 +00:00
|
|
|
|
2011-11-11 23:06:35 +00:00
|
|
|
t->tqent_id = rc = tq->tq_next_id;
|
2008-04-25 22:10:47 +00:00
|
|
|
tq->tq_next_id++;
|
2012-12-06 20:57:42 +00:00
|
|
|
t->tqent_func = func;
|
|
|
|
t->tqent_arg = arg;
|
2012-12-06 20:38:19 +00:00
|
|
|
t->tqent_taskq = tq;
|
|
|
|
t->tqent_timer.data = 0;
|
|
|
|
t->tqent_timer.function = NULL;
|
|
|
|
t->tqent_timer.expires = 0;
|
2011-12-06 18:04:51 +00:00
|
|
|
|
|
|
|
ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
|
|
|
|
|
2011-11-11 23:06:35 +00:00
|
|
|
spin_unlock(&t->tqent_lock);
|
2012-01-19 19:36:27 +00:00
|
|
|
|
|
|
|
wake_up(&tq->tq_work_waitq);
|
2008-04-25 22:10:47 +00:00
|
|
|
out:
|
2008-11-03 20:21:08 +00:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2010-07-20 18:55:37 +00:00
|
|
|
SRETURN(rc);
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
2012-12-06 21:04:27 +00:00
|
|
|
EXPORT_SYMBOL(taskq_dispatch);
|
2011-12-06 18:04:51 +00:00
|
|
|
|
2012-12-06 20:38:19 +00:00
|
|
|
taskqid_t
|
|
|
|
taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg,
|
|
|
|
uint_t flags, clock_t expire_time)
|
|
|
|
{
|
|
|
|
taskq_ent_t *t;
|
|
|
|
taskqid_t rc = 0;
|
|
|
|
SENTRY;
|
|
|
|
|
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(func);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
|
|
|
|
/* Taskq being destroyed and all tasks drained */
|
|
|
|
if (!(tq->tq_flags & TQ_ACTIVE))
|
|
|
|
SGOTO(out, rc = 0);
|
|
|
|
|
|
|
|
if ((t = task_alloc(tq, flags)) == NULL)
|
|
|
|
SGOTO(out, rc = 0);
|
|
|
|
|
|
|
|
spin_lock(&t->tqent_lock);
|
|
|
|
|
|
|
|
/* Queue to the delay list for subsequent execution */
|
|
|
|
list_add_tail(&t->tqent_list, &tq->tq_delay_list);
|
|
|
|
|
|
|
|
t->tqent_id = rc = tq->tq_next_id;
|
|
|
|
tq->tq_next_id++;
|
|
|
|
t->tqent_func = func;
|
|
|
|
t->tqent_arg = arg;
|
|
|
|
t->tqent_taskq = tq;
|
|
|
|
t->tqent_timer.data = (unsigned long)t;
|
|
|
|
t->tqent_timer.function = task_expire;
|
|
|
|
t->tqent_timer.expires = (unsigned long)expire_time;
|
|
|
|
add_timer(&t->tqent_timer);
|
|
|
|
|
|
|
|
ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
|
|
|
|
|
|
|
|
spin_unlock(&t->tqent_lock);
|
|
|
|
out:
|
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
SRETURN(rc);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(taskq_dispatch_delay);
|
|
|
|
|
2011-12-06 18:04:51 +00:00
|
|
|
void
|
2012-12-06 21:04:27 +00:00
|
|
|
taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
|
2011-12-06 18:04:51 +00:00
|
|
|
taskq_ent_t *t)
|
|
|
|
{
|
|
|
|
SENTRY;
|
|
|
|
|
|
|
|
ASSERT(tq);
|
|
|
|
ASSERT(func);
|
|
|
|
ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC));
|
|
|
|
|
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
|
|
|
|
/* Taskq being destroyed and all tasks drained */
|
|
|
|
if (!(tq->tq_flags & TQ_ACTIVE)) {
|
|
|
|
t->tqent_id = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_lock(&t->tqent_lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark it as a prealloc'd task. This is important
|
|
|
|
* to ensure that we don't free it later.
|
|
|
|
*/
|
|
|
|
t->tqent_flags |= TQENT_FLAG_PREALLOC;
|
|
|
|
|
|
|
|
/* Queue to the priority list instead of the pending list */
|
|
|
|
if (flags & TQ_FRONT)
|
|
|
|
list_add_tail(&t->tqent_list, &tq->tq_prio_list);
|
|
|
|
else
|
|
|
|
list_add_tail(&t->tqent_list, &tq->tq_pend_list);
|
|
|
|
|
|
|
|
t->tqent_id = tq->tq_next_id;
|
|
|
|
tq->tq_next_id++;
|
|
|
|
t->tqent_func = func;
|
|
|
|
t->tqent_arg = arg;
|
2012-12-06 20:38:19 +00:00
|
|
|
t->tqent_taskq = tq;
|
2011-12-06 18:04:51 +00:00
|
|
|
|
|
|
|
spin_unlock(&t->tqent_lock);
|
|
|
|
|
|
|
|
wake_up(&tq->tq_work_waitq);
|
|
|
|
out:
|
2012-01-19 19:36:27 +00:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2011-12-06 18:04:51 +00:00
|
|
|
SEXIT;
|
|
|
|
}
|
2012-12-06 21:04:27 +00:00
|
|
|
EXPORT_SYMBOL(taskq_dispatch_ent);
|
2011-12-06 18:04:51 +00:00
|
|
|
|
|
|
|
int
|
2012-12-06 21:04:27 +00:00
|
|
|
taskq_empty_ent(taskq_ent_t *t)
|
2011-12-06 18:04:51 +00:00
|
|
|
{
|
|
|
|
return list_empty(&t->tqent_list);
|
|
|
|
}
|
2012-12-06 21:04:27 +00:00
|
|
|
EXPORT_SYMBOL(taskq_empty_ent);
|
2011-12-06 18:04:51 +00:00
|
|
|
|
|
|
|
void
|
2012-12-06 21:04:27 +00:00
|
|
|
taskq_init_ent(taskq_ent_t *t)
|
2011-12-06 18:04:51 +00:00
|
|
|
{
|
|
|
|
spin_lock_init(&t->tqent_lock);
|
2012-12-06 20:38:19 +00:00
|
|
|
init_waitqueue_head(&t->tqent_waitq);
|
|
|
|
init_timer(&t->tqent_timer);
|
2011-12-06 18:04:51 +00:00
|
|
|
INIT_LIST_HEAD(&t->tqent_list);
|
|
|
|
t->tqent_id = 0;
|
|
|
|
t->tqent_func = NULL;
|
|
|
|
t->tqent_arg = NULL;
|
|
|
|
t->tqent_flags = 0;
|
2012-12-06 20:38:19 +00:00
|
|
|
t->tqent_taskq = NULL;
|
2011-12-06 18:04:51 +00:00
|
|
|
}
|
2012-12-06 21:04:27 +00:00
|
|
|
EXPORT_SYMBOL(taskq_init_ent);
|
2011-12-06 18:04:51 +00:00
|
|
|
|
2008-04-25 22:10:47 +00:00
|
|
|
static int
|
|
|
|
taskq_thread(void *args)
|
|
|
|
{
|
2012-12-06 20:57:42 +00:00
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
|
|
|
sigset_t blocked;
|
2011-12-06 01:32:48 +00:00
|
|
|
taskq_thread_t *tqt = args;
|
2012-12-06 20:57:42 +00:00
|
|
|
taskq_t *tq;
|
|
|
|
taskq_ent_t *t;
|
2010-07-01 17:07:51 +00:00
|
|
|
struct list_head *pend_list;
|
2010-07-20 18:55:37 +00:00
|
|
|
SENTRY;
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
ASSERT(tqt);
|
2011-12-06 01:32:48 +00:00
|
|
|
tq = tqt->tqt_tq;
|
2012-12-06 20:57:42 +00:00
|
|
|
current->flags |= PF_NOFREEZE;
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
sigfillset(&blocked);
|
|
|
|
sigprocmask(SIG_BLOCK, &blocked, NULL);
|
|
|
|
flush_signals(current);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
tq->tq_nthreads++;
|
|
|
|
wake_up(&tq->tq_wait_waitq);
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
while (!kthread_should_stop()) {
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2010-07-01 17:07:51 +00:00
|
|
|
if (list_empty(&tq->tq_pend_list) &&
|
|
|
|
list_empty(&tq->tq_prio_list)) {
|
2012-01-19 18:33:19 +00:00
|
|
|
add_wait_queue_exclusive(&tq->tq_work_waitq, &wait);
|
2008-11-03 20:21:08 +00:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-25 22:10:47 +00:00
|
|
|
schedule();
|
2008-11-03 20:21:08 +00:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2012-01-19 18:33:19 +00:00
|
|
|
remove_wait_queue(&tq->tq_work_waitq, &wait);
|
2008-04-25 22:10:47 +00:00
|
|
|
} else {
|
|
|
|
__set_current_state(TASK_RUNNING);
|
|
|
|
}
|
|
|
|
|
2010-07-01 17:07:51 +00: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) {
|
2012-12-06 20:57:42 +00:00
|
|
|
t = list_entry(pend_list->next,taskq_ent_t,tqent_list);
|
|
|
|
list_del_init(&t->tqent_list);
|
Store copy of tqent_flags prior to servicing task
A preallocated taskq_ent_t's tqent_flags must be checked prior to
servicing the taskq_ent_t. Once a preallocated taskq entry is serviced,
the ownership of the entry is handed back to the caller of
taskq_dispatch, thus the entry's contents can potentially be mangled.
In particular, this is a problem in the case where a preallocated taskq
entry is serviced, and the caller clears it's tqent_flags field. Thus,
when the function returns and task_done is called, it looks as though
the entry is **not** a preallocated task (when in fact it **is** a
preallocated task).
In this situation, task_done will place the preallocated taskq_ent_t
structure onto the taskq_t's free list. This is a **huge** mistake. If
the taskq_ent_t is then freed by the caller of taskq_dispatch, the
taskq_t's free list will hold a pointer to garbage data. Even worse, if
nothing has over written the freed memory before the pointer is
dereferenced, it may still look as though it points to a valid list_head
belonging to a taskq_ent_t structure.
Thus, the task entry's flags are now copied prior to servicing the task.
This copy is then checked to see if it is a preallocated task, and
determine if the entry needs to be passed down to the task_done
function.
Signed-off-by: Prakash Surya <surya1@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #71
2011-12-16 22:57:31 +00:00
|
|
|
|
2011-12-06 18:04:51 +00:00
|
|
|
/* In order to support recursively dispatching a
|
|
|
|
* preallocated taskq_ent_t, tqent_id must be
|
|
|
|
* stored prior to executing tqent_func. */
|
Swap taskq_ent_t with taskqid_t in taskq_thread_t
The taskq_t's active thread list is sorted based on its
tqt_ent->tqent_id field. The list is kept sorted solely by inserting
new taskq_thread_t's in their correct sorted location; no other
means is used. This means that once inserted, if a taskq_thread_t's
tqt_ent->tqent_id field changes, the list runs the risk of no
longer being sorted.
Prior to the introduction of the taskq_dispatch_prealloc() interface,
this was not a problem as a taskq_ent_t actively being serviced under
the old interface should always have a static tqent_id field. Thus,
once the taskq_thread_t is added to the taskq_t's active thread list,
the taskq_thread_t's tqt_ent->tqent_id field would remain constant.
Now, this is no longer the case. Currently, if using the
taskq_dispatch_prealloc() interface, any given taskq_ent_t actively
being serviced _may_ have its tqent_id value incremented. This happens
when the preallocated taskq_ent_t structure is recursively dispatched.
Thus, a taskq_thread_t could potentially have its tqt_ent->tqent_id
field silently modified from under its feet. If this were to happen
to a taskq_thread_t on a taskq_t's active thread list, this would
compromise the integrity of the order of the list (as the list
_may_ no longer be sorted).
To get around this, the taskq_thread_t's taskq_ent_t pointer was
replaced with its own static copy of the tqent_id. So, as a taskq_ent_t
is pulled off of the taskq_t's pending list, a static copy of its
tqent_id is made and this copy is used to sort the active thread
list. Using a static copy is key in ensuring the integrity of the
order of the active thread list. Even if the underlying taskq_ent_t
is recursively dispatched (as has its tqent_id modified), this
static copy stored inside the taskq_thread_t will remain constant.
Signed-off-by: Prakash Surya <surya1@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #71
2011-12-16 17:44:31 +00:00
|
|
|
tqt->tqt_id = t->tqent_id;
|
2012-12-06 20:38:19 +00:00
|
|
|
tqt->tqt_task = t;
|
Store copy of tqent_flags prior to servicing task
A preallocated taskq_ent_t's tqent_flags must be checked prior to
servicing the taskq_ent_t. Once a preallocated taskq entry is serviced,
the ownership of the entry is handed back to the caller of
taskq_dispatch, thus the entry's contents can potentially be mangled.
In particular, this is a problem in the case where a preallocated taskq
entry is serviced, and the caller clears it's tqent_flags field. Thus,
when the function returns and task_done is called, it looks as though
the entry is **not** a preallocated task (when in fact it **is** a
preallocated task).
In this situation, task_done will place the preallocated taskq_ent_t
structure onto the taskq_t's free list. This is a **huge** mistake. If
the taskq_ent_t is then freed by the caller of taskq_dispatch, the
taskq_t's free list will hold a pointer to garbage data. Even worse, if
nothing has over written the freed memory before the pointer is
dereferenced, it may still look as though it points to a valid list_head
belonging to a taskq_ent_t structure.
Thus, the task entry's flags are now copied prior to servicing the task.
This copy is then checked to see if it is a preallocated task, and
determine if the entry needs to be passed down to the task_done
function.
Signed-off-by: Prakash Surya <surya1@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #71
2011-12-16 22:57:31 +00:00
|
|
|
|
|
|
|
/* We must store a copy of the flags prior to
|
|
|
|
* servicing the task (servicing a prealloc'd task
|
|
|
|
* returns the ownership of the tqent back to
|
|
|
|
* the caller of taskq_dispatch). Thus,
|
|
|
|
* tqent_flags _may_ change within the call. */
|
|
|
|
tqt->tqt_flags = t->tqent_flags;
|
|
|
|
|
2011-12-06 01:32:48 +00:00
|
|
|
taskq_insert_in_order(tq, tqt);
|
2012-12-06 20:57:42 +00:00
|
|
|
tq->tq_nactive++;
|
2008-11-03 20:21:08 +00:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
|
|
|
/* Perform the requested task */
|
2012-12-06 20:57:42 +00:00
|
|
|
t->tqent_func(t->tqent_arg);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2008-11-03 20:21:08 +00:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2012-12-06 20:57:42 +00:00
|
|
|
tq->tq_nactive--;
|
2011-12-06 01:32:48 +00:00
|
|
|
list_del_init(&tqt->tqt_active_list);
|
2012-12-06 20:38:19 +00:00
|
|
|
tqt->tqt_task = NULL;
|
Store copy of tqent_flags prior to servicing task
A preallocated taskq_ent_t's tqent_flags must be checked prior to
servicing the taskq_ent_t. Once a preallocated taskq entry is serviced,
the ownership of the entry is handed back to the caller of
taskq_dispatch, thus the entry's contents can potentially be mangled.
In particular, this is a problem in the case where a preallocated taskq
entry is serviced, and the caller clears it's tqent_flags field. Thus,
when the function returns and task_done is called, it looks as though
the entry is **not** a preallocated task (when in fact it **is** a
preallocated task).
In this situation, task_done will place the preallocated taskq_ent_t
structure onto the taskq_t's free list. This is a **huge** mistake. If
the taskq_ent_t is then freed by the caller of taskq_dispatch, the
taskq_t's free list will hold a pointer to garbage data. Even worse, if
nothing has over written the freed memory before the pointer is
dereferenced, it may still look as though it points to a valid list_head
belonging to a taskq_ent_t structure.
Thus, the task entry's flags are now copied prior to servicing the task.
This copy is then checked to see if it is a preallocated task, and
determine if the entry needs to be passed down to the task_done
function.
Signed-off-by: Prakash Surya <surya1@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #71
2011-12-16 22:57:31 +00:00
|
|
|
|
|
|
|
/* For prealloc'd tasks, we don't free anything. */
|
|
|
|
if ((tq->tq_flags & TASKQ_DYNAMIC) ||
|
|
|
|
!(tqt->tqt_flags & TQENT_FLAG_PREALLOC))
|
|
|
|
task_done(tq, t);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2009-03-15 22:13:49 +00:00
|
|
|
/* When the current lowest outstanding taskqid is
|
|
|
|
* done calculate the new lowest outstanding id */
|
Swap taskq_ent_t with taskqid_t in taskq_thread_t
The taskq_t's active thread list is sorted based on its
tqt_ent->tqent_id field. The list is kept sorted solely by inserting
new taskq_thread_t's in their correct sorted location; no other
means is used. This means that once inserted, if a taskq_thread_t's
tqt_ent->tqent_id field changes, the list runs the risk of no
longer being sorted.
Prior to the introduction of the taskq_dispatch_prealloc() interface,
this was not a problem as a taskq_ent_t actively being serviced under
the old interface should always have a static tqent_id field. Thus,
once the taskq_thread_t is added to the taskq_t's active thread list,
the taskq_thread_t's tqt_ent->tqent_id field would remain constant.
Now, this is no longer the case. Currently, if using the
taskq_dispatch_prealloc() interface, any given taskq_ent_t actively
being serviced _may_ have its tqent_id value incremented. This happens
when the preallocated taskq_ent_t structure is recursively dispatched.
Thus, a taskq_thread_t could potentially have its tqt_ent->tqent_id
field silently modified from under its feet. If this were to happen
to a taskq_thread_t on a taskq_t's active thread list, this would
compromise the integrity of the order of the list (as the list
_may_ no longer be sorted).
To get around this, the taskq_thread_t's taskq_ent_t pointer was
replaced with its own static copy of the tqent_id. So, as a taskq_ent_t
is pulled off of the taskq_t's pending list, a static copy of its
tqent_id is made and this copy is used to sort the active thread
list. Using a static copy is key in ensuring the integrity of the
order of the active thread list. Even if the underlying taskq_ent_t
is recursively dispatched (as has its tqent_id modified), this
static copy stored inside the taskq_thread_t will remain constant.
Signed-off-by: Prakash Surya <surya1@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #71
2011-12-16 17:44:31 +00:00
|
|
|
if (tq->tq_lowest_id == tqt->tqt_id) {
|
2008-04-25 22:10:47 +00:00
|
|
|
tq->tq_lowest_id = taskq_lowest_id(tq);
|
Swap taskq_ent_t with taskqid_t in taskq_thread_t
The taskq_t's active thread list is sorted based on its
tqt_ent->tqent_id field. The list is kept sorted solely by inserting
new taskq_thread_t's in their correct sorted location; no other
means is used. This means that once inserted, if a taskq_thread_t's
tqt_ent->tqent_id field changes, the list runs the risk of no
longer being sorted.
Prior to the introduction of the taskq_dispatch_prealloc() interface,
this was not a problem as a taskq_ent_t actively being serviced under
the old interface should always have a static tqent_id field. Thus,
once the taskq_thread_t is added to the taskq_t's active thread list,
the taskq_thread_t's tqt_ent->tqent_id field would remain constant.
Now, this is no longer the case. Currently, if using the
taskq_dispatch_prealloc() interface, any given taskq_ent_t actively
being serviced _may_ have its tqent_id value incremented. This happens
when the preallocated taskq_ent_t structure is recursively dispatched.
Thus, a taskq_thread_t could potentially have its tqt_ent->tqent_id
field silently modified from under its feet. If this were to happen
to a taskq_thread_t on a taskq_t's active thread list, this would
compromise the integrity of the order of the list (as the list
_may_ no longer be sorted).
To get around this, the taskq_thread_t's taskq_ent_t pointer was
replaced with its own static copy of the tqent_id. So, as a taskq_ent_t
is pulled off of the taskq_t's pending list, a static copy of its
tqent_id is made and this copy is used to sort the active thread
list. Using a static copy is key in ensuring the integrity of the
order of the active thread list. Even if the underlying taskq_ent_t
is recursively dispatched (as has its tqent_id modified), this
static copy stored inside the taskq_thread_t will remain constant.
Signed-off-by: Prakash Surya <surya1@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #71
2011-12-16 17:44:31 +00:00
|
|
|
ASSERT3S(tq->tq_lowest_id, >, tqt->tqt_id);
|
2008-04-25 22:10:47 +00:00
|
|
|
}
|
|
|
|
|
Swap taskq_ent_t with taskqid_t in taskq_thread_t
The taskq_t's active thread list is sorted based on its
tqt_ent->tqent_id field. The list is kept sorted solely by inserting
new taskq_thread_t's in their correct sorted location; no other
means is used. This means that once inserted, if a taskq_thread_t's
tqt_ent->tqent_id field changes, the list runs the risk of no
longer being sorted.
Prior to the introduction of the taskq_dispatch_prealloc() interface,
this was not a problem as a taskq_ent_t actively being serviced under
the old interface should always have a static tqent_id field. Thus,
once the taskq_thread_t is added to the taskq_t's active thread list,
the taskq_thread_t's tqt_ent->tqent_id field would remain constant.
Now, this is no longer the case. Currently, if using the
taskq_dispatch_prealloc() interface, any given taskq_ent_t actively
being serviced _may_ have its tqent_id value incremented. This happens
when the preallocated taskq_ent_t structure is recursively dispatched.
Thus, a taskq_thread_t could potentially have its tqt_ent->tqent_id
field silently modified from under its feet. If this were to happen
to a taskq_thread_t on a taskq_t's active thread list, this would
compromise the integrity of the order of the list (as the list
_may_ no longer be sorted).
To get around this, the taskq_thread_t's taskq_ent_t pointer was
replaced with its own static copy of the tqent_id. So, as a taskq_ent_t
is pulled off of the taskq_t's pending list, a static copy of its
tqent_id is made and this copy is used to sort the active thread
list. Using a static copy is key in ensuring the integrity of the
order of the active thread list. Even if the underlying taskq_ent_t
is recursively dispatched (as has its tqent_id modified), this
static copy stored inside the taskq_thread_t will remain constant.
Signed-off-by: Prakash Surya <surya1@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #71
2011-12-16 17:44:31 +00:00
|
|
|
tqt->tqt_id = 0;
|
Store copy of tqent_flags prior to servicing task
A preallocated taskq_ent_t's tqent_flags must be checked prior to
servicing the taskq_ent_t. Once a preallocated taskq entry is serviced,
the ownership of the entry is handed back to the caller of
taskq_dispatch, thus the entry's contents can potentially be mangled.
In particular, this is a problem in the case where a preallocated taskq
entry is serviced, and the caller clears it's tqent_flags field. Thus,
when the function returns and task_done is called, it looks as though
the entry is **not** a preallocated task (when in fact it **is** a
preallocated task).
In this situation, task_done will place the preallocated taskq_ent_t
structure onto the taskq_t's free list. This is a **huge** mistake. If
the taskq_ent_t is then freed by the caller of taskq_dispatch, the
taskq_t's free list will hold a pointer to garbage data. Even worse, if
nothing has over written the freed memory before the pointer is
dereferenced, it may still look as though it points to a valid list_head
belonging to a taskq_ent_t structure.
Thus, the task entry's flags are now copied prior to servicing the task.
This copy is then checked to see if it is a preallocated task, and
determine if the entry needs to be passed down to the task_done
function.
Signed-off-by: Prakash Surya <surya1@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #71
2011-12-16 22:57:31 +00:00
|
|
|
tqt->tqt_flags = 0;
|
2012-12-06 20:57:42 +00:00
|
|
|
wake_up_all(&tq->tq_wait_waitq);
|
2008-04-25 22:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
}
|
2008-04-25 22:10:47 +00:00
|
|
|
|
|
|
|
__set_current_state(TASK_RUNNING);
|
2012-12-06 20:57:42 +00:00
|
|
|
tq->tq_nthreads--;
|
2011-12-06 01:32:48 +00:00
|
|
|
list_del_init(&tqt->tqt_thread_list);
|
|
|
|
kmem_free(tqt, sizeof(taskq_thread_t));
|
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2010-07-20 18:55:37 +00:00
|
|
|
SRETURN(0);
|
2008-04-25 22:10:47 +00:00
|
|
|
}
|
|
|
|
|
2008-02-26 20:36:04 +00:00
|
|
|
taskq_t *
|
2012-12-06 21:04:27 +00:00
|
|
|
taskq_create(const char *name, int nthreads, pri_t pri,
|
2012-12-06 20:57:42 +00:00
|
|
|
int minalloc, int maxalloc, uint_t flags)
|
2008-02-26 20:36:04 +00:00
|
|
|
{
|
2012-12-06 20:57:42 +00:00
|
|
|
taskq_t *tq;
|
2011-12-06 01:32:48 +00:00
|
|
|
taskq_thread_t *tqt;
|
2012-12-06 20:57:42 +00:00
|
|
|
int rc = 0, i, j = 0;
|
|
|
|
SENTRY;
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
ASSERT(name != NULL);
|
|
|
|
ASSERT(pri <= maxclsyspri);
|
|
|
|
ASSERT(minalloc >= 0);
|
|
|
|
ASSERT(maxalloc <= INT_MAX);
|
|
|
|
ASSERT(!(flags & (TASKQ_CPR_SAFE | TASKQ_DYNAMIC))); /* Unsupported */
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2009-07-09 17:07:52 +00: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);
|
|
|
|
}
|
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
tq = kmem_alloc(sizeof(*tq), KM_PUSHPAGE);
|
|
|
|
if (tq == NULL)
|
|
|
|
SRETURN(NULL);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
spin_lock_init(&tq->tq_lock);
|
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
INIT_LIST_HEAD(&tq->tq_thread_list);
|
|
|
|
INIT_LIST_HEAD(&tq->tq_active_list);
|
|
|
|
tq->tq_name = name;
|
|
|
|
tq->tq_nactive = 0;
|
2008-04-25 22:10:47 +00:00
|
|
|
tq->tq_nthreads = 0;
|
2012-12-06 20:57:42 +00:00
|
|
|
tq->tq_pri = pri;
|
|
|
|
tq->tq_minalloc = minalloc;
|
|
|
|
tq->tq_maxalloc = maxalloc;
|
2008-04-25 22:10:47 +00:00
|
|
|
tq->tq_nalloc = 0;
|
2012-12-06 20:57:42 +00:00
|
|
|
tq->tq_flags = (flags | TQ_ACTIVE);
|
2008-04-25 22:10:47 +00:00
|
|
|
tq->tq_next_id = 1;
|
|
|
|
tq->tq_lowest_id = 1;
|
2012-12-06 20:57:42 +00:00
|
|
|
INIT_LIST_HEAD(&tq->tq_free_list);
|
|
|
|
INIT_LIST_HEAD(&tq->tq_pend_list);
|
|
|
|
INIT_LIST_HEAD(&tq->tq_prio_list);
|
2012-12-06 20:38:19 +00:00
|
|
|
INIT_LIST_HEAD(&tq->tq_delay_list);
|
2012-12-06 20:57:42 +00:00
|
|
|
init_waitqueue_head(&tq->tq_work_waitq);
|
|
|
|
init_waitqueue_head(&tq->tq_wait_waitq);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
if (flags & TASKQ_PREPOPULATE)
|
|
|
|
for (i = 0; i < minalloc; i++)
|
|
|
|
task_done(tq, task_alloc(tq, TQ_PUSHPAGE | TQ_NEW));
|
2008-04-23 21:19:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-23 21:19:47 +00:00
|
|
|
|
2011-12-06 01:32:48 +00:00
|
|
|
for (i = 0; i < nthreads; i++) {
|
2012-09-12 18:31:39 +00:00
|
|
|
tqt = kmem_alloc(sizeof(*tqt), KM_PUSHPAGE);
|
2011-12-06 01:32:48 +00:00
|
|
|
INIT_LIST_HEAD(&tqt->tqt_thread_list);
|
|
|
|
INIT_LIST_HEAD(&tqt->tqt_active_list);
|
|
|
|
tqt->tqt_tq = tq;
|
Swap taskq_ent_t with taskqid_t in taskq_thread_t
The taskq_t's active thread list is sorted based on its
tqt_ent->tqent_id field. The list is kept sorted solely by inserting
new taskq_thread_t's in their correct sorted location; no other
means is used. This means that once inserted, if a taskq_thread_t's
tqt_ent->tqent_id field changes, the list runs the risk of no
longer being sorted.
Prior to the introduction of the taskq_dispatch_prealloc() interface,
this was not a problem as a taskq_ent_t actively being serviced under
the old interface should always have a static tqent_id field. Thus,
once the taskq_thread_t is added to the taskq_t's active thread list,
the taskq_thread_t's tqt_ent->tqent_id field would remain constant.
Now, this is no longer the case. Currently, if using the
taskq_dispatch_prealloc() interface, any given taskq_ent_t actively
being serviced _may_ have its tqent_id value incremented. This happens
when the preallocated taskq_ent_t structure is recursively dispatched.
Thus, a taskq_thread_t could potentially have its tqt_ent->tqent_id
field silently modified from under its feet. If this were to happen
to a taskq_thread_t on a taskq_t's active thread list, this would
compromise the integrity of the order of the list (as the list
_may_ no longer be sorted).
To get around this, the taskq_thread_t's taskq_ent_t pointer was
replaced with its own static copy of the tqent_id. So, as a taskq_ent_t
is pulled off of the taskq_t's pending list, a static copy of its
tqent_id is made and this copy is used to sort the active thread
list. Using a static copy is key in ensuring the integrity of the
order of the active thread list. Even if the underlying taskq_ent_t
is recursively dispatched (as has its tqent_id modified), this
static copy stored inside the taskq_thread_t will remain constant.
Signed-off-by: Prakash Surya <surya1@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #71
2011-12-16 17:44:31 +00:00
|
|
|
tqt->tqt_id = 0;
|
2011-12-06 01:32:48 +00:00
|
|
|
|
|
|
|
tqt->tqt_thread = kthread_create(taskq_thread, tqt,
|
2012-12-06 20:57:42 +00:00
|
|
|
"%s/%d", name, i);
|
2011-12-06 01:32:48 +00:00
|
|
|
if (tqt->tqt_thread) {
|
|
|
|
list_add(&tqt->tqt_thread_list, &tq->tq_thread_list);
|
|
|
|
kthread_bind(tqt->tqt_thread, i % num_online_cpus());
|
|
|
|
set_user_nice(tqt->tqt_thread, PRIO_TO_NICE(pri));
|
|
|
|
wake_up_process(tqt->tqt_thread);
|
2008-04-25 22:10:47 +00:00
|
|
|
j++;
|
2011-12-06 01:32:48 +00:00
|
|
|
} else {
|
|
|
|
kmem_free(tqt, sizeof(taskq_thread_t));
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
}
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
/* Wait for all threads to be started before potential destroy */
|
2008-04-25 22:10:47 +00:00
|
|
|
wait_event(tq->tq_wait_waitq, tq->tq_nthreads == j);
|
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
if (rc) {
|
2012-12-06 21:04:27 +00:00
|
|
|
taskq_destroy(tq);
|
2012-12-06 20:57:42 +00:00
|
|
|
tq = NULL;
|
|
|
|
}
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
SRETURN(tq);
|
2008-02-26 20:36:04 +00:00
|
|
|
}
|
2012-12-06 21:04:27 +00:00
|
|
|
EXPORT_SYMBOL(taskq_create);
|
2008-03-11 02:08:57 +00:00
|
|
|
|
|
|
|
void
|
2012-12-06 21:04:27 +00:00
|
|
|
taskq_destroy(taskq_t *tq)
|
2008-03-11 02:08:57 +00:00
|
|
|
{
|
2011-12-06 01:32:48 +00:00
|
|
|
struct task_struct *thread;
|
|
|
|
taskq_thread_t *tqt;
|
2011-11-11 23:06:35 +00:00
|
|
|
taskq_ent_t *t;
|
2010-07-20 18:55:37 +00:00
|
|
|
SENTRY;
|
2008-03-11 02:08:57 +00:00
|
|
|
|
2008-04-25 22:10:47 +00:00
|
|
|
ASSERT(tq);
|
2008-11-03 20:21:08 +00:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2012-12-06 20:57:42 +00:00
|
|
|
tq->tq_flags &= ~TQ_ACTIVE;
|
2008-11-03 20:21:08 +00:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
|
|
|
/* TQ_ACTIVE cleared prevents new tasks being added to pending */
|
2012-12-06 21:04:27 +00:00
|
|
|
taskq_wait(tq);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2011-12-06 01:32:48 +00:00
|
|
|
/*
|
|
|
|
* Signal each thread to exit and block until it does. Each thread
|
|
|
|
* is responsible for removing itself from the list and freeing its
|
|
|
|
* taskq_thread_t. This allows for idle threads to opt to remove
|
|
|
|
* themselves from the taskq. They can be recreated as needed.
|
|
|
|
*/
|
|
|
|
while (!list_empty(&tq->tq_thread_list)) {
|
|
|
|
tqt = list_entry(tq->tq_thread_list.next,
|
|
|
|
taskq_thread_t, tqt_thread_list);
|
|
|
|
thread = tqt->tqt_thread;
|
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
|
|
|
|
|
|
|
kthread_stop(thread);
|
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
|
2011-12-06 01:32:48 +00:00
|
|
|
}
|
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
while (!list_empty(&tq->tq_free_list)) {
|
2011-11-11 23:06:35 +00:00
|
|
|
t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
|
2011-12-06 18:04:51 +00:00
|
|
|
|
|
|
|
ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
|
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
list_del_init(&t->tqent_list);
|
|
|
|
task_free(tq, t);
|
|
|
|
}
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
ASSERT(tq->tq_nthreads == 0);
|
|
|
|
ASSERT(tq->tq_nalloc == 0);
|
|
|
|
ASSERT(list_empty(&tq->tq_thread_list));
|
|
|
|
ASSERT(list_empty(&tq->tq_active_list));
|
|
|
|
ASSERT(list_empty(&tq->tq_free_list));
|
|
|
|
ASSERT(list_empty(&tq->tq_pend_list));
|
|
|
|
ASSERT(list_empty(&tq->tq_prio_list));
|
2012-12-06 20:38:19 +00:00
|
|
|
ASSERT(list_empty(&tq->tq_delay_list));
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
|
2011-12-06 01:32:48 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
kmem_free(tq, sizeof(taskq_t));
|
2008-04-25 22:10:47 +00:00
|
|
|
|
2010-07-20 18:55:37 +00:00
|
|
|
SEXIT;
|
2008-03-11 02:08:57 +00:00
|
|
|
}
|
2012-12-06 21:04:27 +00:00
|
|
|
EXPORT_SYMBOL(taskq_destroy);
|
2009-01-05 23:08:03 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
spl_taskq_init(void)
|
|
|
|
{
|
2012-12-06 20:57:42 +00:00
|
|
|
SENTRY;
|
2009-01-05 23:08:03 +00:00
|
|
|
|
2009-02-02 16:53:53 +00:00
|
|
|
/* Solaris creates a dynamic taskq of up to 64 threads, however in
|
|
|
|
* a Linux environment 1 thread per-core is usually about right */
|
2012-12-06 20:57:42 +00:00
|
|
|
system_taskq = taskq_create("spl_system_taskq", num_online_cpus(),
|
2009-02-02 16:53:53 +00:00
|
|
|
minclsyspri, 4, 512, TASKQ_PREPOPULATE);
|
2009-01-05 23:08:03 +00:00
|
|
|
if (system_taskq == NULL)
|
2010-07-20 18:55:37 +00:00
|
|
|
SRETURN(1);
|
2009-01-05 23:08:03 +00:00
|
|
|
|
2012-12-06 20:57:42 +00:00
|
|
|
SRETURN(0);
|
2009-01-05 23:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
spl_taskq_fini(void)
|
|
|
|
{
|
2012-12-06 20:57:42 +00:00
|
|
|
SENTRY;
|
2009-01-05 23:08:03 +00:00
|
|
|
taskq_destroy(system_taskq);
|
2012-12-06 20:57:42 +00:00
|
|
|
SEXIT;
|
2009-01-05 23:08:03 +00:00
|
|
|
}
|