]> git.proxmox.com Git - mirror_spl-debian.git/blame - module/spl/spl-taskq.c
Fix rpm dependencies
[mirror_spl-debian.git] / module / spl / spl-taskq.c
CommitLineData
716154c5
BB
1/*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
715f6251 6 * UCRL-CODE-235197
7 *
716154c5
BB
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://github.com/behlendorf/spl/>.
715f6251 10 *
716154c5
BB
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
715f6251 17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
716154c5
BB
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Task Queue Implementation.
25\*****************************************************************************/
715f6251 26
f4b37741 27#include <sys/taskq.h>
3d061e9d 28#include <sys/kmem.h>
55abb092 29#include <spl-debug.h>
f1ca4da6 30
b17edc10
BB
31#ifdef SS_DEBUG_SUBSYS
32#undef SS_DEBUG_SUBSYS
937879f1 33#endif
34
b17edc10 35#define SS_DEBUG_SUBSYS SS_TASKQ
937879f1 36
e9cb2b4f
BB
37/* Global system-wide dynamic task queue available for all consumers */
38taskq_t *system_taskq;
39EXPORT_SYMBOL(system_taskq);
40
82387586
BB
41/*
42 * NOTE: Must be called with tq->tq_lock held, returns a list_t which
bcd68186 43 * is not attached to the free, work, or pending taskq lists.
f1ca4da6 44 */
046a70c9 45static taskq_ent_t *
bcd68186 46task_alloc(taskq_t *tq, uint_t flags)
47{
046a70c9 48 taskq_ent_t *t;
bcd68186 49 int count = 0;
b17edc10 50 SENTRY;
bcd68186 51
52 ASSERT(tq);
53 ASSERT(flags & (TQ_SLEEP | TQ_NOSLEEP)); /* One set */
54 ASSERT(!((flags & TQ_SLEEP) && (flags & TQ_NOSLEEP))); /* Not both */
3d061e9d 55 ASSERT(spin_is_locked(&tq->tq_lock));
bcd68186 56retry:
046a70c9 57 /* Acquire taskq_ent_t's from free list if available */
bcd68186 58 if (!list_empty(&tq->tq_free_list) && !(flags & TQ_NEW)) {
046a70c9 59 t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
44217f7a
PS
60
61 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
62
046a70c9 63 list_del_init(&t->tqent_list);
b17edc10 64 SRETURN(t);
bcd68186 65 }
66
7257ec41 67 /* Free list is empty and memory allocations are prohibited */
bcd68186 68 if (flags & TQ_NOALLOC)
b17edc10 69 SRETURN(NULL);
bcd68186 70
046a70c9 71 /* Hit maximum taskq_ent_t pool size */
bcd68186 72 if (tq->tq_nalloc >= tq->tq_maxalloc) {
73 if (flags & TQ_NOSLEEP)
b17edc10 74 SRETURN(NULL);
bcd68186 75
26f7245c
RC
76 /*
77 * Sleep periodically polling the free list for an available
046a70c9
PS
78 * taskq_ent_t. Dispatching with TQ_SLEEP should always succeed
79 * but we cannot block forever waiting for an taskq_entq_t to
26f7245c
RC
80 * show up in the free list, otherwise a deadlock can happen.
81 *
82 * Therefore, we need to allocate a new task even if the number
83 * of allocated tasks is above tq->tq_maxalloc, but we still
84 * end up delaying the task allocation by one second, thereby
85 * throttling the task dispatch rate.
86 */
87 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
88 schedule_timeout(HZ / 100);
89 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
90 if (count < 100)
91 SGOTO(retry, count++);
bcd68186 92 }
93
26f7245c 94 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
046a70c9 95 t = kmem_alloc(sizeof(taskq_ent_t), flags & (TQ_SLEEP | TQ_NOSLEEP));
749045bb 96 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 97
26f7245c 98 if (t) {
44217f7a 99 taskq_init_ent(t);
26f7245c
RC
100 tq->tq_nalloc++;
101 }
bcd68186 102
b17edc10 103 SRETURN(t);
bcd68186 104}
105
82387586 106/*
046a70c9 107 * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t
bcd68186 108 * to already be removed from the free, work, or pending taskq lists.
109 */
110static void
046a70c9 111task_free(taskq_t *tq, taskq_ent_t *t)
bcd68186 112{
b17edc10 113 SENTRY;
bcd68186 114
115 ASSERT(tq);
116 ASSERT(t);
117 ASSERT(spin_is_locked(&tq->tq_lock));
046a70c9 118 ASSERT(list_empty(&t->tqent_list));
bcd68186 119
046a70c9 120 kmem_free(t, sizeof(taskq_ent_t));
bcd68186 121 tq->tq_nalloc--;
f1ca4da6 122
b17edc10 123 SEXIT;
bcd68186 124}
125
82387586
BB
126/*
127 * NOTE: Must be called with tq->tq_lock held, either destroys the
046a70c9 128 * taskq_ent_t if too many exist or moves it to the free list for later use.
bcd68186 129 */
f1ca4da6 130static void
046a70c9 131task_done(taskq_t *tq, taskq_ent_t *t)
f1ca4da6 132{
b17edc10 133 SENTRY;
bcd68186 134 ASSERT(tq);
135 ASSERT(t);
136 ASSERT(spin_is_locked(&tq->tq_lock));
137
046a70c9 138 list_del_init(&t->tqent_list);
f1ca4da6 139
bcd68186 140 if (tq->tq_nalloc <= tq->tq_minalloc) {
046a70c9
PS
141 t->tqent_id = 0;
142 t->tqent_func = NULL;
143 t->tqent_arg = NULL;
44217f7a 144 t->tqent_flags = 0;
8f2503e0 145
046a70c9 146 list_add_tail(&t->tqent_list, &tq->tq_free_list);
bcd68186 147 } else {
148 task_free(tq, t);
149 }
f1ca4da6 150
b17edc10 151 SEXIT;
f1ca4da6 152}
153
82387586
BB
154/*
155 * As tasks are submitted to the task queue they are assigned a
f0d8bb26
NB
156 * monotonically increasing taskqid and added to the tail of the pending
157 * list. As worker threads become available the tasks are removed from
158 * the head of the pending or priority list, giving preference to the
159 * priority list. The tasks are then added to the work list, preserving
160 * the ordering by taskqid. Finally, as tasks complete they are removed
161 * from the work list. This means that the pending and work lists are
162 * always kept sorted by taskqid. Thus the lowest outstanding
82387586 163 * incomplete taskqid can be determined simply by checking the min
f0d8bb26
NB
164 * taskqid for each head item on the pending, priority, and work list.
165 * This value is stored in tq->tq_lowest_id and only updated to the new
166 * lowest id when the previous lowest id completes. All taskqids lower
167 * than tq->tq_lowest_id must have completed. It is also possible
168 * larger taskqid's have completed because they may be processed in
169 * parallel by several worker threads. However, this is not a problem
170 * because the behavior of taskq_wait_id() is to block until all
171 * previously submitted taskqid's have completed.
82387586
BB
172 *
173 * XXX: Taskqid_t wrapping is not handled. However, taskqid_t's are
174 * 64-bit values so even if a taskq is processing 2^24 (16,777,216)
175 * taskqid_ts per second it will still take 2^40 seconds, 34,865 years,
176 * before the wrap occurs. I can live with that for now.
bcd68186 177 */
178static int
179taskq_wait_check(taskq_t *tq, taskqid_t id)
180{
7257ec41
BB
181 int rc;
182
183 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
184 rc = (id < tq->tq_lowest_id);
185 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
186
b17edc10 187 SRETURN(rc);
bcd68186 188}
189
bcd68186 190void
191__taskq_wait_id(taskq_t *tq, taskqid_t id)
f1ca4da6 192{
b17edc10 193 SENTRY;
bcd68186 194 ASSERT(tq);
195
196 wait_event(tq->tq_wait_waitq, taskq_wait_check(tq, id));
197
b17edc10 198 SEXIT;
bcd68186 199}
200EXPORT_SYMBOL(__taskq_wait_id);
201
202void
203__taskq_wait(taskq_t *tq)
204{
205 taskqid_t id;
b17edc10 206 SENTRY;
bcd68186 207 ASSERT(tq);
208
7257ec41 209 /* Wait for the largest outstanding taskqid */
749045bb 210 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
7257ec41 211 id = tq->tq_next_id - 1;
749045bb 212 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 213
214 __taskq_wait_id(tq, id);
215
b17edc10 216 SEXIT;
bcd68186 217
218}
219EXPORT_SYMBOL(__taskq_wait);
220
221int
222__taskq_member(taskq_t *tq, void *t)
223{
2c02b71b
PS
224 struct list_head *l;
225 taskq_thread_t *tqt;
b17edc10 226 SENTRY;
bcd68186 227
228 ASSERT(tq);
229 ASSERT(t);
230
2c02b71b
PS
231 list_for_each(l, &tq->tq_thread_list) {
232 tqt = list_entry(l, taskq_thread_t, tqt_thread_list);
233 if (tqt->tqt_thread == (struct task_struct *)t)
234 SRETURN(1);
235 }
bcd68186 236
b17edc10 237 SRETURN(0);
bcd68186 238}
239EXPORT_SYMBOL(__taskq_member);
240
241taskqid_t
242__taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
243{
046a70c9 244 taskq_ent_t *t;
bcd68186 245 taskqid_t rc = 0;
b17edc10 246 SENTRY;
f1ca4da6 247
937879f1 248 ASSERT(tq);
249 ASSERT(func);
d05ec4b4
BB
250
251 /* Solaris assumes TQ_SLEEP if not passed explicitly */
252 if (!(flags & (TQ_SLEEP | TQ_NOSLEEP)))
253 flags |= TQ_SLEEP;
254
55abb092
BB
255 if (unlikely(in_atomic() && (flags & TQ_SLEEP)))
256 PANIC("May schedule while atomic: %s/0x%08x/%d\n",
257 current->comm, preempt_count(), current->pid);
f1ca4da6 258
749045bb 259 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
f1ca4da6 260
bcd68186 261 /* Taskq being destroyed and all tasks drained */
262 if (!(tq->tq_flags & TQ_ACTIVE))
b17edc10 263 SGOTO(out, rc = 0);
f1ca4da6 264
bcd68186 265 /* Do not queue the task unless there is idle thread for it */
266 ASSERT(tq->tq_nactive <= tq->tq_nthreads);
267 if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads))
b17edc10 268 SGOTO(out, rc = 0);
bcd68186 269
270 if ((t = task_alloc(tq, flags)) == NULL)
b17edc10 271 SGOTO(out, rc = 0);
f1ca4da6 272
046a70c9 273 spin_lock(&t->tqent_lock);
f0d8bb26
NB
274
275 /* Queue to the priority list instead of the pending list */
276 if (flags & TQ_FRONT)
046a70c9 277 list_add_tail(&t->tqent_list, &tq->tq_prio_list);
f0d8bb26 278 else
046a70c9 279 list_add_tail(&t->tqent_list, &tq->tq_pend_list);
f0d8bb26 280
046a70c9 281 t->tqent_id = rc = tq->tq_next_id;
bcd68186 282 tq->tq_next_id++;
046a70c9
PS
283 t->tqent_func = func;
284 t->tqent_arg = arg;
44217f7a
PS
285
286 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
287
046a70c9 288 spin_unlock(&t->tqent_lock);
bcd68186 289out:
749045bb 290 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
ec2b4104
NB
291 if (rc > 0)
292 wake_up(&tq->tq_work_waitq);
293
b17edc10 294 SRETURN(rc);
f1ca4da6 295}
f1b59d26 296EXPORT_SYMBOL(__taskq_dispatch);
44217f7a
PS
297
298void
299__taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
300 taskq_ent_t *t)
301{
302 SENTRY;
303
304 ASSERT(tq);
305 ASSERT(func);
306 ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC));
307
308 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
309
310 /* Taskq being destroyed and all tasks drained */
311 if (!(tq->tq_flags & TQ_ACTIVE)) {
312 t->tqent_id = 0;
ec2b4104 313 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
44217f7a
PS
314 goto out;
315 }
316
317 spin_lock(&t->tqent_lock);
318
319 /*
320 * Mark it as a prealloc'd task. This is important
321 * to ensure that we don't free it later.
322 */
323 t->tqent_flags |= TQENT_FLAG_PREALLOC;
324
325 /* Queue to the priority list instead of the pending list */
326 if (flags & TQ_FRONT)
327 list_add_tail(&t->tqent_list, &tq->tq_prio_list);
328 else
329 list_add_tail(&t->tqent_list, &tq->tq_pend_list);
330
331 t->tqent_id = tq->tq_next_id;
332 tq->tq_next_id++;
333 t->tqent_func = func;
334 t->tqent_arg = arg;
335
336 spin_unlock(&t->tqent_lock);
ec2b4104 337 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
44217f7a
PS
338
339 wake_up(&tq->tq_work_waitq);
340out:
44217f7a
PS
341 SEXIT;
342}
343EXPORT_SYMBOL(__taskq_dispatch_ent);
344
345int
346__taskq_empty_ent(taskq_ent_t *t)
347{
348 return list_empty(&t->tqent_list);
349}
350EXPORT_SYMBOL(__taskq_empty_ent);
351
352void
353__taskq_init_ent(taskq_ent_t *t)
354{
355 spin_lock_init(&t->tqent_lock);
356 INIT_LIST_HEAD(&t->tqent_list);
357 t->tqent_id = 0;
358 t->tqent_func = NULL;
359 t->tqent_arg = NULL;
360 t->tqent_flags = 0;
361}
362EXPORT_SYMBOL(__taskq_init_ent);
363
82387586
BB
364/*
365 * Returns the lowest incomplete taskqid_t. The taskqid_t may
f0d8bb26
NB
366 * be queued on the pending list, on the priority list, or on
367 * the work list currently being handled, but it is not 100%
368 * complete yet.
82387586 369 */
bcd68186 370static taskqid_t
371taskq_lowest_id(taskq_t *tq)
372{
7257ec41 373 taskqid_t lowest_id = tq->tq_next_id;
046a70c9 374 taskq_ent_t *t;
2c02b71b 375 taskq_thread_t *tqt;
b17edc10 376 SENTRY;
bcd68186 377
378 ASSERT(tq);
379 ASSERT(spin_is_locked(&tq->tq_lock));
380
82387586 381 if (!list_empty(&tq->tq_pend_list)) {
046a70c9
PS
382 t = list_entry(tq->tq_pend_list.next, taskq_ent_t, tqent_list);
383 lowest_id = MIN(lowest_id, t->tqent_id);
82387586 384 }
bcd68186 385
f0d8bb26 386 if (!list_empty(&tq->tq_prio_list)) {
046a70c9
PS
387 t = list_entry(tq->tq_prio_list.next, taskq_ent_t, tqent_list);
388 lowest_id = MIN(lowest_id, t->tqent_id);
f0d8bb26
NB
389 }
390
2c02b71b
PS
391 if (!list_empty(&tq->tq_active_list)) {
392 tqt = list_entry(tq->tq_active_list.next, taskq_thread_t,
393 tqt_active_list);
e7e5f78e
PS
394 ASSERT(tqt->tqt_id != 0);
395 lowest_id = MIN(lowest_id, tqt->tqt_id);
82387586 396 }
bcd68186 397
b17edc10 398 SRETURN(lowest_id);
bcd68186 399}
400
f0d8bb26
NB
401/*
402 * Insert a task into a list keeping the list sorted by increasing
403 * taskqid.
404 */
405static void
2c02b71b 406taskq_insert_in_order(taskq_t *tq, taskq_thread_t *tqt)
f0d8bb26 407{
2c02b71b 408 taskq_thread_t *w;
f0d8bb26
NB
409 struct list_head *l;
410
b17edc10 411 SENTRY;
f0d8bb26 412 ASSERT(tq);
2c02b71b 413 ASSERT(tqt);
f0d8bb26
NB
414 ASSERT(spin_is_locked(&tq->tq_lock));
415
2c02b71b
PS
416 list_for_each_prev(l, &tq->tq_active_list) {
417 w = list_entry(l, taskq_thread_t, tqt_active_list);
e7e5f78e 418 if (w->tqt_id < tqt->tqt_id) {
2c02b71b 419 list_add(&tqt->tqt_active_list, l);
f0d8bb26
NB
420 break;
421 }
422 }
2c02b71b
PS
423 if (l == &tq->tq_active_list)
424 list_add(&tqt->tqt_active_list, &tq->tq_active_list);
f0d8bb26 425
b17edc10 426 SEXIT;
f0d8bb26
NB
427}
428
bcd68186 429static int
430taskq_thread(void *args)
431{
432 DECLARE_WAITQUEUE(wait, current);
433 sigset_t blocked;
2c02b71b
PS
434 taskq_thread_t *tqt = args;
435 taskq_t *tq;
046a70c9 436 taskq_ent_t *t;
f0d8bb26 437 struct list_head *pend_list;
b17edc10 438 SENTRY;
bcd68186 439
2c02b71b
PS
440 ASSERT(tqt);
441 tq = tqt->tqt_tq;
bcd68186 442 current->flags |= PF_NOFREEZE;
443
372c2572
BB
444 /* Disable the direct memory reclaim path */
445 if (tq->tq_flags & TASKQ_NORECLAIM)
446 current->flags |= PF_MEMALLOC;
447
bcd68186 448 sigfillset(&blocked);
449 sigprocmask(SIG_BLOCK, &blocked, NULL);
450 flush_signals(current);
451
749045bb 452 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 453 tq->tq_nthreads++;
454 wake_up(&tq->tq_wait_waitq);
455 set_current_state(TASK_INTERRUPTIBLE);
456
457 while (!kthread_should_stop()) {
458
f0d8bb26
NB
459 if (list_empty(&tq->tq_pend_list) &&
460 list_empty(&tq->tq_prio_list)) {
749045bb 461 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
ec2b4104 462 add_wait_queue_exclusive(&tq->tq_work_waitq, &wait);
bcd68186 463 schedule();
ec2b4104 464 remove_wait_queue(&tq->tq_work_waitq, &wait);
749045bb 465 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 466 } else {
467 __set_current_state(TASK_RUNNING);
468 }
469
f0d8bb26
NB
470
471 if (!list_empty(&tq->tq_prio_list))
472 pend_list = &tq->tq_prio_list;
473 else if (!list_empty(&tq->tq_pend_list))
474 pend_list = &tq->tq_pend_list;
475 else
476 pend_list = NULL;
477
478 if (pend_list) {
046a70c9
PS
479 t = list_entry(pend_list->next, taskq_ent_t, tqent_list);
480 list_del_init(&t->tqent_list);
8f2503e0 481
44217f7a
PS
482 /* In order to support recursively dispatching a
483 * preallocated taskq_ent_t, tqent_id must be
484 * stored prior to executing tqent_func. */
e7e5f78e 485 tqt->tqt_id = t->tqent_id;
8f2503e0
PS
486
487 /* We must store a copy of the flags prior to
488 * servicing the task (servicing a prealloc'd task
489 * returns the ownership of the tqent back to
490 * the caller of taskq_dispatch). Thus,
491 * tqent_flags _may_ change within the call. */
492 tqt->tqt_flags = t->tqent_flags;
493
2c02b71b 494 taskq_insert_in_order(tq, tqt);
bcd68186 495 tq->tq_nactive++;
749045bb 496 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 497
498 /* Perform the requested task */
046a70c9 499 t->tqent_func(t->tqent_arg);
bcd68186 500
749045bb 501 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 502 tq->tq_nactive--;
2c02b71b 503 list_del_init(&tqt->tqt_active_list);
8f2503e0
PS
504
505 /* For prealloc'd tasks, we don't free anything. */
506 if ((tq->tq_flags & TASKQ_DYNAMIC) ||
507 !(tqt->tqt_flags & TQENT_FLAG_PREALLOC))
508 task_done(tq, t);
bcd68186 509
7257ec41
BB
510 /* When the current lowest outstanding taskqid is
511 * done calculate the new lowest outstanding id */
e7e5f78e 512 if (tq->tq_lowest_id == tqt->tqt_id) {
bcd68186 513 tq->tq_lowest_id = taskq_lowest_id(tq);
e7e5f78e 514 ASSERT3S(tq->tq_lowest_id, >, tqt->tqt_id);
bcd68186 515 }
516
e7e5f78e 517 tqt->tqt_id = 0;
8f2503e0 518 tqt->tqt_flags = 0;
bcd68186 519 wake_up_all(&tq->tq_wait_waitq);
520 }
521
522 set_current_state(TASK_INTERRUPTIBLE);
523
524 }
525
526 __set_current_state(TASK_RUNNING);
527 tq->tq_nthreads--;
2c02b71b
PS
528 list_del_init(&tqt->tqt_thread_list);
529 kmem_free(tqt, sizeof(taskq_thread_t));
530
749045bb 531 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 532
b17edc10 533 SRETURN(0);
bcd68186 534}
535
f1ca4da6 536taskq_t *
537__taskq_create(const char *name, int nthreads, pri_t pri,
538 int minalloc, int maxalloc, uint_t flags)
539{
bcd68186 540 taskq_t *tq;
2c02b71b 541 taskq_thread_t *tqt;
bcd68186 542 int rc = 0, i, j = 0;
b17edc10 543 SENTRY;
bcd68186 544
545 ASSERT(name != NULL);
546 ASSERT(pri <= maxclsyspri);
547 ASSERT(minalloc >= 0);
548 ASSERT(maxalloc <= INT_MAX);
549 ASSERT(!(flags & (TASKQ_CPR_SAFE | TASKQ_DYNAMIC))); /* Unsupported */
550
915404bd
BB
551 /* Scale the number of threads using nthreads as a percentage */
552 if (flags & TASKQ_THREADS_CPU_PCT) {
553 ASSERT(nthreads <= 100);
554 ASSERT(nthreads >= 0);
555 nthreads = MIN(nthreads, 100);
556 nthreads = MAX(nthreads, 0);
557 nthreads = MAX((num_online_cpus() * nthreads) / 100, 1);
558 }
559
bcd68186 560 tq = kmem_alloc(sizeof(*tq), KM_SLEEP);
561 if (tq == NULL)
b17edc10 562 SRETURN(NULL);
bcd68186 563
bcd68186 564 spin_lock_init(&tq->tq_lock);
749045bb 565 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
2c02b71b
PS
566 INIT_LIST_HEAD(&tq->tq_thread_list);
567 INIT_LIST_HEAD(&tq->tq_active_list);
bcd68186 568 tq->tq_name = name;
569 tq->tq_nactive = 0;
570 tq->tq_nthreads = 0;
571 tq->tq_pri = pri;
572 tq->tq_minalloc = minalloc;
573 tq->tq_maxalloc = maxalloc;
574 tq->tq_nalloc = 0;
575 tq->tq_flags = (flags | TQ_ACTIVE);
576 tq->tq_next_id = 1;
577 tq->tq_lowest_id = 1;
578 INIT_LIST_HEAD(&tq->tq_free_list);
bcd68186 579 INIT_LIST_HEAD(&tq->tq_pend_list);
f0d8bb26 580 INIT_LIST_HEAD(&tq->tq_prio_list);
bcd68186 581 init_waitqueue_head(&tq->tq_work_waitq);
582 init_waitqueue_head(&tq->tq_wait_waitq);
583
584 if (flags & TASKQ_PREPOPULATE)
585 for (i = 0; i < minalloc; i++)
586 task_done(tq, task_alloc(tq, TQ_SLEEP | TQ_NEW));
6e605b6e 587
749045bb 588 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
6e605b6e 589
2c02b71b
PS
590 for (i = 0; i < nthreads; i++) {
591 tqt = kmem_alloc(sizeof(*tqt), KM_SLEEP);
592 INIT_LIST_HEAD(&tqt->tqt_thread_list);
593 INIT_LIST_HEAD(&tqt->tqt_active_list);
594 tqt->tqt_tq = tq;
e7e5f78e 595 tqt->tqt_id = 0;
2c02b71b
PS
596
597 tqt->tqt_thread = kthread_create(taskq_thread, tqt,
598 "%s/%d", name, i);
599 if (tqt->tqt_thread) {
600 list_add(&tqt->tqt_thread_list, &tq->tq_thread_list);
601 kthread_bind(tqt->tqt_thread, i % num_online_cpus());
602 set_user_nice(tqt->tqt_thread, PRIO_TO_NICE(pri));
603 wake_up_process(tqt->tqt_thread);
bcd68186 604 j++;
2c02b71b
PS
605 } else {
606 kmem_free(tqt, sizeof(taskq_thread_t));
607 rc = 1;
608 }
609 }
bcd68186 610
611 /* Wait for all threads to be started before potential destroy */
612 wait_event(tq->tq_wait_waitq, tq->tq_nthreads == j);
613
614 if (rc) {
615 __taskq_destroy(tq);
616 tq = NULL;
617 }
618
b17edc10 619 SRETURN(tq);
f1ca4da6 620}
f1b59d26 621EXPORT_SYMBOL(__taskq_create);
b123971f 622
623void
624__taskq_destroy(taskq_t *tq)
625{
2c02b71b
PS
626 struct task_struct *thread;
627 taskq_thread_t *tqt;
046a70c9 628 taskq_ent_t *t;
b17edc10 629 SENTRY;
b123971f 630
bcd68186 631 ASSERT(tq);
749045bb 632 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 633 tq->tq_flags &= ~TQ_ACTIVE;
749045bb 634 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 635
636 /* TQ_ACTIVE cleared prevents new tasks being added to pending */
637 __taskq_wait(tq);
638
749045bb 639 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 640
2c02b71b
PS
641 /*
642 * Signal each thread to exit and block until it does. Each thread
643 * is responsible for removing itself from the list and freeing its
644 * taskq_thread_t. This allows for idle threads to opt to remove
645 * themselves from the taskq. They can be recreated as needed.
646 */
647 while (!list_empty(&tq->tq_thread_list)) {
648 tqt = list_entry(tq->tq_thread_list.next,
649 taskq_thread_t, tqt_thread_list);
650 thread = tqt->tqt_thread;
651 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
652
653 kthread_stop(thread);
654
655 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
656 }
657
bcd68186 658 while (!list_empty(&tq->tq_free_list)) {
046a70c9 659 t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
44217f7a
PS
660
661 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
662
046a70c9 663 list_del_init(&t->tqent_list);
bcd68186 664 task_free(tq, t);
665 }
666
667 ASSERT(tq->tq_nthreads == 0);
668 ASSERT(tq->tq_nalloc == 0);
2c02b71b
PS
669 ASSERT(list_empty(&tq->tq_thread_list));
670 ASSERT(list_empty(&tq->tq_active_list));
bcd68186 671 ASSERT(list_empty(&tq->tq_free_list));
bcd68186 672 ASSERT(list_empty(&tq->tq_pend_list));
f0d8bb26 673 ASSERT(list_empty(&tq->tq_prio_list));
bcd68186 674
749045bb 675 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
2c02b71b 676
bcd68186 677 kmem_free(tq, sizeof(taskq_t));
678
b17edc10 679 SEXIT;
b123971f 680}
bcd68186 681EXPORT_SYMBOL(__taskq_destroy);
e9cb2b4f
BB
682
683int
684spl_taskq_init(void)
685{
b17edc10 686 SENTRY;
e9cb2b4f 687
f220894e
BB
688 /* Solaris creates a dynamic taskq of up to 64 threads, however in
689 * a Linux environment 1 thread per-core is usually about right */
690 system_taskq = taskq_create("spl_system_taskq", num_online_cpus(),
691 minclsyspri, 4, 512, TASKQ_PREPOPULATE);
e9cb2b4f 692 if (system_taskq == NULL)
b17edc10 693 SRETURN(1);
e9cb2b4f 694
b17edc10 695 SRETURN(0);
e9cb2b4f
BB
696}
697
698void
699spl_taskq_fini(void)
700{
b17edc10 701 SENTRY;
e9cb2b4f 702 taskq_destroy(system_taskq);
b17edc10 703 SEXIT;
e9cb2b4f 704}