]> git.proxmox.com Git - mirror_spl-debian.git/blame - module/spl/spl-taskq.c
Imported Upstream version 0.6.4.1
[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 8 * This file is part of the SPL, Solaris Porting Layer.
3d6af2dd 9 * For details, see <http://zfsonlinux.org/>.
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>
937879f1 29
9e4fb5c2
LG
30int spl_taskq_thread_bind = 0;
31module_param(spl_taskq_thread_bind, int, 0644);
32MODULE_PARM_DESC(spl_taskq_thread_bind, "Bind taskq thread to CPU by default");
33
e9cb2b4f
BB
34/* Global system-wide dynamic task queue available for all consumers */
35taskq_t *system_taskq;
36EXPORT_SYMBOL(system_taskq);
37
9b51f218
BB
38static int
39task_km_flags(uint_t flags)
40{
41 if (flags & TQ_NOSLEEP)
42 return KM_NOSLEEP;
43
44 if (flags & TQ_PUSHPAGE)
45 return KM_PUSHPAGE;
46
47 return KM_SLEEP;
48}
49
82387586
BB
50/*
51 * NOTE: Must be called with tq->tq_lock held, returns a list_t which
bcd68186 52 * is not attached to the free, work, or pending taskq lists.
f1ca4da6 53 */
046a70c9 54static taskq_ent_t *
bcd68186 55task_alloc(taskq_t *tq, uint_t flags)
56{
472a34ca
BB
57 taskq_ent_t *t;
58 int count = 0;
bcd68186 59
472a34ca
BB
60 ASSERT(tq);
61 ASSERT(spin_is_locked(&tq->tq_lock));
bcd68186 62retry:
472a34ca
BB
63 /* Acquire taskq_ent_t's from free list if available */
64 if (!list_empty(&tq->tq_free_list) && !(flags & TQ_NEW)) {
65 t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
66
67 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
d9acd930
BB
68 ASSERT(!(t->tqent_flags & TQENT_FLAG_CANCEL));
69 ASSERT(!timer_pending(&t->tqent_timer));
472a34ca
BB
70
71 list_del_init(&t->tqent_list);
10946b02 72 return (t);
472a34ca
BB
73 }
74
75 /* Free list is empty and memory allocations are prohibited */
76 if (flags & TQ_NOALLOC)
10946b02 77 return (NULL);
472a34ca
BB
78
79 /* Hit maximum taskq_ent_t pool size */
80 if (tq->tq_nalloc >= tq->tq_maxalloc) {
81 if (flags & TQ_NOSLEEP)
10946b02 82 return (NULL);
472a34ca
BB
83
84 /*
85 * Sleep periodically polling the free list for an available
86 * taskq_ent_t. Dispatching with TQ_SLEEP should always succeed
87 * but we cannot block forever waiting for an taskq_ent_t to
88 * show up in the free list, otherwise a deadlock can happen.
89 *
90 * Therefore, we need to allocate a new task even if the number
91 * of allocated tasks is above tq->tq_maxalloc, but we still
92 * end up delaying the task allocation by one second, thereby
93 * throttling the task dispatch rate.
94 */
95 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
96 schedule_timeout(HZ / 100);
97 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
10946b02
AX
98 if (count < 100) {
99 count++;
100 goto retry;
101 }
472a34ca
BB
102 }
103
104 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
105 t = kmem_alloc(sizeof(taskq_ent_t), task_km_flags(flags));
106 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
107
108 if (t) {
109 taskq_init_ent(t);
110 tq->tq_nalloc++;
111 }
112
10946b02 113 return (t);
bcd68186 114}
115
82387586 116/*
046a70c9 117 * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t
bcd68186 118 * to already be removed from the free, work, or pending taskq lists.
119 */
120static void
046a70c9 121task_free(taskq_t *tq, taskq_ent_t *t)
bcd68186 122{
472a34ca
BB
123 ASSERT(tq);
124 ASSERT(t);
bcd68186 125 ASSERT(spin_is_locked(&tq->tq_lock));
046a70c9 126 ASSERT(list_empty(&t->tqent_list));
d9acd930 127 ASSERT(!timer_pending(&t->tqent_timer));
bcd68186 128
472a34ca
BB
129 kmem_free(t, sizeof(taskq_ent_t));
130 tq->tq_nalloc--;
bcd68186 131}
132
82387586
BB
133/*
134 * NOTE: Must be called with tq->tq_lock held, either destroys the
046a70c9 135 * taskq_ent_t if too many exist or moves it to the free list for later use.
bcd68186 136 */
f1ca4da6 137static void
046a70c9 138task_done(taskq_t *tq, taskq_ent_t *t)
f1ca4da6 139{
bcd68186 140 ASSERT(tq);
141 ASSERT(t);
142 ASSERT(spin_is_locked(&tq->tq_lock));
143
d9acd930
BB
144 /* Wake tasks blocked in taskq_wait_id() */
145 wake_up_all(&t->tqent_waitq);
146
046a70c9 147 list_del_init(&t->tqent_list);
f1ca4da6 148
472a34ca 149 if (tq->tq_nalloc <= tq->tq_minalloc) {
046a70c9
PS
150 t->tqent_id = 0;
151 t->tqent_func = NULL;
152 t->tqent_arg = NULL;
44217f7a 153 t->tqent_flags = 0;
8f2503e0 154
472a34ca 155 list_add_tail(&t->tqent_list, &tq->tq_free_list);
bcd68186 156 } else {
157 task_free(tq, t);
158 }
f1ca4da6 159}
160
82387586 161/*
d9acd930
BB
162 * When a delayed task timer expires remove it from the delay list and
163 * add it to the priority list in order for immediate processing.
bcd68186 164 */
d9acd930
BB
165static void
166task_expire(unsigned long data)
bcd68186 167{
d9acd930
BB
168 taskq_ent_t *w, *t = (taskq_ent_t *)data;
169 taskq_t *tq = t->tqent_taskq;
170 struct list_head *l;
7257ec41
BB
171
172 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
d9acd930
BB
173
174 if (t->tqent_flags & TQENT_FLAG_CANCEL) {
175 ASSERT(list_empty(&t->tqent_list));
176 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
177 return;
178 }
179
180 /*
181 * The priority list must be maintained in strict task id order
182 * from lowest to highest for lowest_id to be easily calculable.
183 */
184 list_del(&t->tqent_list);
185 list_for_each_prev(l, &tq->tq_prio_list) {
186 w = list_entry(l, taskq_ent_t, tqent_list);
187 if (w->tqent_id < t->tqent_id) {
188 list_add(&t->tqent_list, l);
189 break;
190 }
191 }
192 if (l == &tq->tq_prio_list)
193 list_add(&t->tqent_list, &tq->tq_prio_list);
194
7257ec41
BB
195 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
196
d9acd930
BB
197 wake_up(&tq->tq_work_waitq);
198}
199
200/*
201 * Returns the lowest incomplete taskqid_t. The taskqid_t may
202 * be queued on the pending list, on the priority list, on the
203 * delay list, or on the work list currently being handled, but
204 * it is not 100% complete yet.
205 */
206static taskqid_t
207taskq_lowest_id(taskq_t *tq)
208{
209 taskqid_t lowest_id = tq->tq_next_id;
210 taskq_ent_t *t;
211 taskq_thread_t *tqt;
d9acd930
BB
212
213 ASSERT(tq);
214 ASSERT(spin_is_locked(&tq->tq_lock));
215
216 if (!list_empty(&tq->tq_pend_list)) {
217 t = list_entry(tq->tq_pend_list.next, taskq_ent_t, tqent_list);
218 lowest_id = MIN(lowest_id, t->tqent_id);
219 }
220
221 if (!list_empty(&tq->tq_prio_list)) {
222 t = list_entry(tq->tq_prio_list.next, taskq_ent_t, tqent_list);
223 lowest_id = MIN(lowest_id, t->tqent_id);
224 }
225
226 if (!list_empty(&tq->tq_delay_list)) {
227 t = list_entry(tq->tq_delay_list.next, taskq_ent_t, tqent_list);
228 lowest_id = MIN(lowest_id, t->tqent_id);
229 }
230
231 if (!list_empty(&tq->tq_active_list)) {
232 tqt = list_entry(tq->tq_active_list.next, taskq_thread_t,
233 tqt_active_list);
234 ASSERT(tqt->tqt_id != 0);
235 lowest_id = MIN(lowest_id, tqt->tqt_id);
236 }
237
10946b02 238 return (lowest_id);
d9acd930
BB
239}
240
241/*
242 * Insert a task into a list keeping the list sorted by increasing taskqid.
243 */
244static void
245taskq_insert_in_order(taskq_t *tq, taskq_thread_t *tqt)
246{
247 taskq_thread_t *w;
248 struct list_head *l;
249
d9acd930
BB
250 ASSERT(tq);
251 ASSERT(tqt);
252 ASSERT(spin_is_locked(&tq->tq_lock));
253
254 list_for_each_prev(l, &tq->tq_active_list) {
255 w = list_entry(l, taskq_thread_t, tqt_active_list);
256 if (w->tqt_id < tqt->tqt_id) {
257 list_add(&tqt->tqt_active_list, l);
258 break;
259 }
260 }
261 if (l == &tq->tq_active_list)
262 list_add(&tqt->tqt_active_list, &tq->tq_active_list);
d9acd930
BB
263}
264
265/*
266 * Find and return a task from the given list if it exists. The list
267 * must be in lowest to highest task id order.
268 */
269static taskq_ent_t *
270taskq_find_list(taskq_t *tq, struct list_head *lh, taskqid_t id)
271{
272 struct list_head *l;
273 taskq_ent_t *t;
d9acd930
BB
274
275 ASSERT(spin_is_locked(&tq->tq_lock));
276
277 list_for_each(l, lh) {
278 t = list_entry(l, taskq_ent_t, tqent_list);
279
280 if (t->tqent_id == id)
10946b02 281 return (t);
d9acd930
BB
282
283 if (t->tqent_id > id)
284 break;
285 }
286
10946b02 287 return (NULL);
bcd68186 288}
289
d9acd930
BB
290/*
291 * Find an already dispatched task given the task id regardless of what
292 * state it is in. If a task is still pending or executing it will be
293 * returned and 'active' set appropriately. If the task has already
294 * been run then NULL is returned.
295 */
296static taskq_ent_t *
297taskq_find(taskq_t *tq, taskqid_t id, int *active)
298{
299 taskq_thread_t *tqt;
300 struct list_head *l;
301 taskq_ent_t *t;
d9acd930
BB
302
303 ASSERT(spin_is_locked(&tq->tq_lock));
304 *active = 0;
305
306 t = taskq_find_list(tq, &tq->tq_delay_list, id);
307 if (t)
10946b02 308 return (t);
d9acd930
BB
309
310 t = taskq_find_list(tq, &tq->tq_prio_list, id);
311 if (t)
10946b02 312 return (t);
d9acd930
BB
313
314 t = taskq_find_list(tq, &tq->tq_pend_list, id);
315 if (t)
10946b02 316 return (t);
d9acd930
BB
317
318 list_for_each(l, &tq->tq_active_list) {
319 tqt = list_entry(l, taskq_thread_t, tqt_active_list);
320 if (tqt->tqt_id == id) {
321 t = tqt->tqt_task;
322 *active = 1;
10946b02 323 return (t);
d9acd930
BB
324 }
325 }
326
10946b02 327 return (NULL);
d9acd930
BB
328}
329
80093b6f
AX
330static int
331taskq_wait_id_check(taskq_t *tq, taskqid_t id)
f1ca4da6 332{
d9acd930 333 int active = 0;
80093b6f 334 int rc;
bcd68186 335
d9acd930 336 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
80093b6f 337 rc = (taskq_find(tq, id, &active) == NULL);
d9acd930
BB
338 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
339
80093b6f
AX
340 return (rc);
341}
bcd68186 342
80093b6f
AX
343/*
344 * The taskq_wait_id() function blocks until the passed task id completes.
345 * This does not guarantee that all lower task ids have completed.
346 */
347void
348taskq_wait_id(taskq_t *tq, taskqid_t id)
349{
350 wait_event(tq->tq_wait_waitq, taskq_wait_id_check(tq, id));
bcd68186 351}
aed8671c 352EXPORT_SYMBOL(taskq_wait_id);
bcd68186 353
d9acd930
BB
354/*
355 * The taskq_wait() function will block until all previously submitted
356 * tasks have been completed. A previously submitted task is defined as
357 * a task with a lower task id than the current task queue id. Note that
358 * all task id's are assigned monotonically at dispatch time.
359 *
360 * Waiting for all previous tasks to complete is accomplished by tracking
361 * the lowest outstanding task id. As tasks are dispatched they are added
362 * added to the tail of the pending, priority, or delay lists. And as
363 * worker threads become available the tasks are removed from the heads
364 * of these lists and linked to the worker threads. This ensures the
365 * lists are kept in lowest to highest task id order.
366 *
367 * Therefore the lowest outstanding task id can be quickly determined by
368 * checking the head item from all of these lists. This value is stored
369 * with the task queue as the lowest id. It only needs to be recalculated
370 * when either the task with the current lowest id completes or is canceled.
371 *
372 * By blocking until the lowest task id exceeds the current task id when
373 * the function was called we ensure all previous tasks have completed.
374 *
375 * NOTE: When there are multiple worked threads it is possible for larger
376 * task ids to complete before smaller ones. Conversely when the task
377 * queue contains delay tasks with small task ids, you may block for a
378 * considerable length of time waiting for them to expire and execute.
379 */
380static int
381taskq_wait_check(taskq_t *tq, taskqid_t id)
382{
383 int rc;
384
385 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
386 rc = (id < tq->tq_lowest_id);
387 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
388
10946b02 389 return (rc);
d9acd930
BB
390}
391
392void
393taskq_wait_all(taskq_t *tq, taskqid_t id)
394{
395 wait_event(tq->tq_wait_waitq, taskq_wait_check(tq, id));
396}
397EXPORT_SYMBOL(taskq_wait_all);
398
bcd68186 399void
aed8671c 400taskq_wait(taskq_t *tq)
bcd68186 401{
402 taskqid_t id;
10946b02 403
bcd68186 404 ASSERT(tq);
405
7257ec41 406 /* Wait for the largest outstanding taskqid */
749045bb 407 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
7257ec41 408 id = tq->tq_next_id - 1;
749045bb 409 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 410
d9acd930 411 taskq_wait_all(tq, id);
bcd68186 412}
aed8671c 413EXPORT_SYMBOL(taskq_wait);
bcd68186 414
415int
aed8671c 416taskq_member(taskq_t *tq, void *t)
bcd68186 417{
2c02b71b
PS
418 struct list_head *l;
419 taskq_thread_t *tqt;
bcd68186 420
421 ASSERT(tq);
472a34ca 422 ASSERT(t);
bcd68186 423
2c02b71b
PS
424 list_for_each(l, &tq->tq_thread_list) {
425 tqt = list_entry(l, taskq_thread_t, tqt_thread_list);
426 if (tqt->tqt_thread == (struct task_struct *)t)
10946b02 427 return (1);
2c02b71b 428 }
bcd68186 429
10946b02 430 return (0);
bcd68186 431}
aed8671c 432EXPORT_SYMBOL(taskq_member);
bcd68186 433
d9acd930
BB
434/*
435 * Cancel an already dispatched task given the task id. Still pending tasks
436 * will be immediately canceled, and if the task is active the function will
437 * block until it completes. Preallocated tasks which are canceled must be
438 * freed by the caller.
439 */
440int
441taskq_cancel_id(taskq_t *tq, taskqid_t id)
442{
443 taskq_ent_t *t;
444 int active = 0;
445 int rc = ENOENT;
d9acd930
BB
446
447 ASSERT(tq);
448
449 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
450 t = taskq_find(tq, id, &active);
451 if (t && !active) {
452 list_del_init(&t->tqent_list);
453 t->tqent_flags |= TQENT_FLAG_CANCEL;
454
455 /*
456 * When canceling the lowest outstanding task id we
457 * must recalculate the new lowest outstanding id.
458 */
459 if (tq->tq_lowest_id == t->tqent_id) {
460 tq->tq_lowest_id = taskq_lowest_id(tq);
461 ASSERT3S(tq->tq_lowest_id, >, t->tqent_id);
462 }
463
464 /*
465 * The task_expire() function takes the tq->tq_lock so drop
466 * drop the lock before synchronously cancelling the timer.
467 */
468 if (timer_pending(&t->tqent_timer)) {
469 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
470 del_timer_sync(&t->tqent_timer);
471 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
472 }
473
474 if (!(t->tqent_flags & TQENT_FLAG_PREALLOC))
475 task_done(tq, t);
476
477 rc = 0;
478 }
479 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
480
481 if (active) {
482 taskq_wait_id(tq, id);
483 rc = EBUSY;
484 }
485
10946b02 486 return (rc);
d9acd930
BB
487}
488EXPORT_SYMBOL(taskq_cancel_id);
489
bcd68186 490taskqid_t
aed8671c 491taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
bcd68186 492{
472a34ca 493 taskq_ent_t *t;
bcd68186 494 taskqid_t rc = 0;
f1ca4da6 495
472a34ca
BB
496 ASSERT(tq);
497 ASSERT(func);
d05ec4b4 498
472a34ca 499 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
f1ca4da6 500
bcd68186 501 /* Taskq being destroyed and all tasks drained */
502 if (!(tq->tq_flags & TQ_ACTIVE))
10946b02 503 goto out;
f1ca4da6 504
bcd68186 505 /* Do not queue the task unless there is idle thread for it */
506 ASSERT(tq->tq_nactive <= tq->tq_nthreads);
507 if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads))
10946b02 508 goto out;
bcd68186 509
472a34ca 510 if ((t = task_alloc(tq, flags)) == NULL)
10946b02 511 goto out;
f1ca4da6 512
046a70c9 513 spin_lock(&t->tqent_lock);
f0d8bb26
NB
514
515 /* Queue to the priority list instead of the pending list */
516 if (flags & TQ_FRONT)
046a70c9 517 list_add_tail(&t->tqent_list, &tq->tq_prio_list);
f0d8bb26 518 else
046a70c9 519 list_add_tail(&t->tqent_list, &tq->tq_pend_list);
f0d8bb26 520
046a70c9 521 t->tqent_id = rc = tq->tq_next_id;
bcd68186 522 tq->tq_next_id++;
472a34ca
BB
523 t->tqent_func = func;
524 t->tqent_arg = arg;
d9acd930
BB
525 t->tqent_taskq = tq;
526 t->tqent_timer.data = 0;
527 t->tqent_timer.function = NULL;
528 t->tqent_timer.expires = 0;
44217f7a
PS
529
530 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
531
046a70c9 532 spin_unlock(&t->tqent_lock);
0bb43ca2
NB
533
534 wake_up(&tq->tq_work_waitq);
bcd68186 535out:
749045bb 536 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
10946b02 537 return (rc);
f1ca4da6 538}
aed8671c 539EXPORT_SYMBOL(taskq_dispatch);
44217f7a 540
d9acd930
BB
541taskqid_t
542taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg,
543 uint_t flags, clock_t expire_time)
544{
d9acd930 545 taskqid_t rc = 0;
10946b02 546 taskq_ent_t *t;
d9acd930
BB
547
548 ASSERT(tq);
549 ASSERT(func);
550
551 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
552
553 /* Taskq being destroyed and all tasks drained */
554 if (!(tq->tq_flags & TQ_ACTIVE))
10946b02 555 goto out;
d9acd930
BB
556
557 if ((t = task_alloc(tq, flags)) == NULL)
10946b02 558 goto out;
d9acd930
BB
559
560 spin_lock(&t->tqent_lock);
561
562 /* Queue to the delay list for subsequent execution */
563 list_add_tail(&t->tqent_list, &tq->tq_delay_list);
564
565 t->tqent_id = rc = tq->tq_next_id;
566 tq->tq_next_id++;
567 t->tqent_func = func;
568 t->tqent_arg = arg;
569 t->tqent_taskq = tq;
570 t->tqent_timer.data = (unsigned long)t;
571 t->tqent_timer.function = task_expire;
572 t->tqent_timer.expires = (unsigned long)expire_time;
573 add_timer(&t->tqent_timer);
574
575 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
576
577 spin_unlock(&t->tqent_lock);
578out:
579 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
10946b02 580 return (rc);
d9acd930
BB
581}
582EXPORT_SYMBOL(taskq_dispatch_delay);
583
44217f7a 584void
aed8671c 585taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
44217f7a
PS
586 taskq_ent_t *t)
587{
44217f7a
PS
588 ASSERT(tq);
589 ASSERT(func);
590 ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC));
591
592 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
593
594 /* Taskq being destroyed and all tasks drained */
595 if (!(tq->tq_flags & TQ_ACTIVE)) {
596 t->tqent_id = 0;
597 goto out;
598 }
599
600 spin_lock(&t->tqent_lock);
601
602 /*
603 * Mark it as a prealloc'd task. This is important
604 * to ensure that we don't free it later.
605 */
606 t->tqent_flags |= TQENT_FLAG_PREALLOC;
607
608 /* Queue to the priority list instead of the pending list */
609 if (flags & TQ_FRONT)
610 list_add_tail(&t->tqent_list, &tq->tq_prio_list);
611 else
612 list_add_tail(&t->tqent_list, &tq->tq_pend_list);
613
614 t->tqent_id = tq->tq_next_id;
615 tq->tq_next_id++;
616 t->tqent_func = func;
617 t->tqent_arg = arg;
d9acd930 618 t->tqent_taskq = tq;
44217f7a
PS
619
620 spin_unlock(&t->tqent_lock);
621
622 wake_up(&tq->tq_work_waitq);
623out:
0bb43ca2 624 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
44217f7a 625}
aed8671c 626EXPORT_SYMBOL(taskq_dispatch_ent);
44217f7a
PS
627
628int
aed8671c 629taskq_empty_ent(taskq_ent_t *t)
44217f7a
PS
630{
631 return list_empty(&t->tqent_list);
632}
aed8671c 633EXPORT_SYMBOL(taskq_empty_ent);
44217f7a
PS
634
635void
aed8671c 636taskq_init_ent(taskq_ent_t *t)
44217f7a
PS
637{
638 spin_lock_init(&t->tqent_lock);
d9acd930
BB
639 init_waitqueue_head(&t->tqent_waitq);
640 init_timer(&t->tqent_timer);
44217f7a
PS
641 INIT_LIST_HEAD(&t->tqent_list);
642 t->tqent_id = 0;
643 t->tqent_func = NULL;
644 t->tqent_arg = NULL;
645 t->tqent_flags = 0;
d9acd930 646 t->tqent_taskq = NULL;
44217f7a 647}
aed8671c 648EXPORT_SYMBOL(taskq_init_ent);
44217f7a 649
bcd68186 650static int
651taskq_thread(void *args)
652{
472a34ca
BB
653 DECLARE_WAITQUEUE(wait, current);
654 sigset_t blocked;
2c02b71b 655 taskq_thread_t *tqt = args;
472a34ca
BB
656 taskq_t *tq;
657 taskq_ent_t *t;
f0d8bb26 658 struct list_head *pend_list;
bcd68186 659
472a34ca 660 ASSERT(tqt);
2c02b71b 661 tq = tqt->tqt_tq;
472a34ca 662 current->flags |= PF_NOFREEZE;
bcd68186 663
472a34ca
BB
664 sigfillset(&blocked);
665 sigprocmask(SIG_BLOCK, &blocked, NULL);
666 flush_signals(current);
bcd68186 667
472a34ca
BB
668 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
669 tq->tq_nthreads++;
670 wake_up(&tq->tq_wait_waitq);
671 set_current_state(TASK_INTERRUPTIBLE);
bcd68186 672
472a34ca 673 while (!kthread_should_stop()) {
bcd68186 674
f0d8bb26
NB
675 if (list_empty(&tq->tq_pend_list) &&
676 list_empty(&tq->tq_prio_list)) {
3c6ed541 677 add_wait_queue_exclusive(&tq->tq_work_waitq, &wait);
749045bb 678 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 679 schedule();
749045bb 680 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
3c6ed541 681 remove_wait_queue(&tq->tq_work_waitq, &wait);
bcd68186 682 } else {
683 __set_current_state(TASK_RUNNING);
684 }
685
f0d8bb26
NB
686
687 if (!list_empty(&tq->tq_prio_list))
688 pend_list = &tq->tq_prio_list;
689 else if (!list_empty(&tq->tq_pend_list))
690 pend_list = &tq->tq_pend_list;
691 else
692 pend_list = NULL;
693
694 if (pend_list) {
472a34ca
BB
695 t = list_entry(pend_list->next,taskq_ent_t,tqent_list);
696 list_del_init(&t->tqent_list);
8f2503e0 697
44217f7a
PS
698 /* In order to support recursively dispatching a
699 * preallocated taskq_ent_t, tqent_id must be
700 * stored prior to executing tqent_func. */
e7e5f78e 701 tqt->tqt_id = t->tqent_id;
d9acd930 702 tqt->tqt_task = t;
8f2503e0
PS
703
704 /* We must store a copy of the flags prior to
705 * servicing the task (servicing a prealloc'd task
706 * returns the ownership of the tqent back to
707 * the caller of taskq_dispatch). Thus,
708 * tqent_flags _may_ change within the call. */
709 tqt->tqt_flags = t->tqent_flags;
710
2c02b71b 711 taskq_insert_in_order(tq, tqt);
472a34ca 712 tq->tq_nactive++;
749045bb 713 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 714
715 /* Perform the requested task */
472a34ca 716 t->tqent_func(t->tqent_arg);
bcd68186 717
749045bb 718 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
472a34ca 719 tq->tq_nactive--;
2c02b71b 720 list_del_init(&tqt->tqt_active_list);
d9acd930 721 tqt->tqt_task = NULL;
8f2503e0
PS
722
723 /* For prealloc'd tasks, we don't free anything. */
724 if ((tq->tq_flags & TASKQ_DYNAMIC) ||
725 !(tqt->tqt_flags & TQENT_FLAG_PREALLOC))
726 task_done(tq, t);
bcd68186 727
7257ec41
BB
728 /* When the current lowest outstanding taskqid is
729 * done calculate the new lowest outstanding id */
e7e5f78e 730 if (tq->tq_lowest_id == tqt->tqt_id) {
bcd68186 731 tq->tq_lowest_id = taskq_lowest_id(tq);
e7e5f78e 732 ASSERT3S(tq->tq_lowest_id, >, tqt->tqt_id);
bcd68186 733 }
734
e7e5f78e 735 tqt->tqt_id = 0;
8f2503e0 736 tqt->tqt_flags = 0;
472a34ca 737 wake_up_all(&tq->tq_wait_waitq);
bcd68186 738 }
739
740 set_current_state(TASK_INTERRUPTIBLE);
741
472a34ca 742 }
bcd68186 743
744 __set_current_state(TASK_RUNNING);
472a34ca 745 tq->tq_nthreads--;
2c02b71b
PS
746 list_del_init(&tqt->tqt_thread_list);
747 kmem_free(tqt, sizeof(taskq_thread_t));
748
472a34ca 749 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 750
10946b02 751 return (0);
bcd68186 752}
753
f1ca4da6 754taskq_t *
aed8671c 755taskq_create(const char *name, int nthreads, pri_t pri,
472a34ca 756 int minalloc, int maxalloc, uint_t flags)
f1ca4da6 757{
9e4fb5c2 758 static int last_used_cpu = 0;
472a34ca 759 taskq_t *tq;
2c02b71b 760 taskq_thread_t *tqt;
472a34ca 761 int rc = 0, i, j = 0;
bcd68186 762
472a34ca
BB
763 ASSERT(name != NULL);
764 ASSERT(pri <= maxclsyspri);
765 ASSERT(minalloc >= 0);
766 ASSERT(maxalloc <= INT_MAX);
767 ASSERT(!(flags & (TASKQ_CPR_SAFE | TASKQ_DYNAMIC))); /* Unsupported */
bcd68186 768
915404bd
BB
769 /* Scale the number of threads using nthreads as a percentage */
770 if (flags & TASKQ_THREADS_CPU_PCT) {
771 ASSERT(nthreads <= 100);
772 ASSERT(nthreads >= 0);
773 nthreads = MIN(nthreads, 100);
774 nthreads = MAX(nthreads, 0);
775 nthreads = MAX((num_online_cpus() * nthreads) / 100, 1);
776 }
777
472a34ca
BB
778 tq = kmem_alloc(sizeof(*tq), KM_PUSHPAGE);
779 if (tq == NULL)
10946b02 780 return (NULL);
bcd68186 781
472a34ca
BB
782 spin_lock_init(&tq->tq_lock);
783 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
784 INIT_LIST_HEAD(&tq->tq_thread_list);
785 INIT_LIST_HEAD(&tq->tq_active_list);
786 tq->tq_name = name;
787 tq->tq_nactive = 0;
bcd68186 788 tq->tq_nthreads = 0;
472a34ca
BB
789 tq->tq_pri = pri;
790 tq->tq_minalloc = minalloc;
791 tq->tq_maxalloc = maxalloc;
bcd68186 792 tq->tq_nalloc = 0;
472a34ca 793 tq->tq_flags = (flags | TQ_ACTIVE);
bcd68186 794 tq->tq_next_id = 1;
795 tq->tq_lowest_id = 1;
472a34ca
BB
796 INIT_LIST_HEAD(&tq->tq_free_list);
797 INIT_LIST_HEAD(&tq->tq_pend_list);
798 INIT_LIST_HEAD(&tq->tq_prio_list);
d9acd930 799 INIT_LIST_HEAD(&tq->tq_delay_list);
472a34ca
BB
800 init_waitqueue_head(&tq->tq_work_waitq);
801 init_waitqueue_head(&tq->tq_wait_waitq);
bcd68186 802
472a34ca
BB
803 if (flags & TASKQ_PREPOPULATE)
804 for (i = 0; i < minalloc; i++)
805 task_done(tq, task_alloc(tq, TQ_PUSHPAGE | TQ_NEW));
6e605b6e 806
472a34ca 807 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
6e605b6e 808
2c02b71b 809 for (i = 0; i < nthreads; i++) {
9b51f218 810 tqt = kmem_alloc(sizeof(*tqt), KM_PUSHPAGE);
2c02b71b
PS
811 INIT_LIST_HEAD(&tqt->tqt_thread_list);
812 INIT_LIST_HEAD(&tqt->tqt_active_list);
813 tqt->tqt_tq = tq;
e7e5f78e 814 tqt->tqt_id = 0;
2c02b71b 815
9e4fb5c2 816 tqt->tqt_thread = spl_kthread_create(taskq_thread, tqt,
472a34ca 817 "%s/%d", name, i);
2c02b71b
PS
818 if (tqt->tqt_thread) {
819 list_add(&tqt->tqt_thread_list, &tq->tq_thread_list);
9e4fb5c2
LG
820 if (spl_taskq_thread_bind) {
821 last_used_cpu = (last_used_cpu + 1) % num_online_cpus();
822 kthread_bind(tqt->tqt_thread, last_used_cpu);
823 }
2c02b71b
PS
824 set_user_nice(tqt->tqt_thread, PRIO_TO_NICE(pri));
825 wake_up_process(tqt->tqt_thread);
bcd68186 826 j++;
2c02b71b
PS
827 } else {
828 kmem_free(tqt, sizeof(taskq_thread_t));
829 rc = 1;
830 }
831 }
bcd68186 832
472a34ca 833 /* Wait for all threads to be started before potential destroy */
bcd68186 834 wait_event(tq->tq_wait_waitq, tq->tq_nthreads == j);
835
472a34ca 836 if (rc) {
aed8671c 837 taskq_destroy(tq);
472a34ca
BB
838 tq = NULL;
839 }
bcd68186 840
10946b02 841 return (tq);
f1ca4da6 842}
aed8671c 843EXPORT_SYMBOL(taskq_create);
b123971f 844
845void
aed8671c 846taskq_destroy(taskq_t *tq)
b123971f 847{
2c02b71b
PS
848 struct task_struct *thread;
849 taskq_thread_t *tqt;
046a70c9 850 taskq_ent_t *t;
b123971f 851
bcd68186 852 ASSERT(tq);
749045bb 853 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
472a34ca 854 tq->tq_flags &= ~TQ_ACTIVE;
749045bb 855 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 856
857 /* TQ_ACTIVE cleared prevents new tasks being added to pending */
aed8671c 858 taskq_wait(tq);
bcd68186 859
472a34ca 860 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 861
2c02b71b
PS
862 /*
863 * Signal each thread to exit and block until it does. Each thread
864 * is responsible for removing itself from the list and freeing its
865 * taskq_thread_t. This allows for idle threads to opt to remove
866 * themselves from the taskq. They can be recreated as needed.
867 */
868 while (!list_empty(&tq->tq_thread_list)) {
869 tqt = list_entry(tq->tq_thread_list.next,
870 taskq_thread_t, tqt_thread_list);
871 thread = tqt->tqt_thread;
872 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
873
874 kthread_stop(thread);
875
472a34ca 876 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
2c02b71b
PS
877 }
878
472a34ca 879 while (!list_empty(&tq->tq_free_list)) {
046a70c9 880 t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
44217f7a
PS
881
882 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
883
472a34ca
BB
884 list_del_init(&t->tqent_list);
885 task_free(tq, t);
886 }
bcd68186 887
472a34ca
BB
888 ASSERT(tq->tq_nthreads == 0);
889 ASSERT(tq->tq_nalloc == 0);
890 ASSERT(list_empty(&tq->tq_thread_list));
891 ASSERT(list_empty(&tq->tq_active_list));
892 ASSERT(list_empty(&tq->tq_free_list));
893 ASSERT(list_empty(&tq->tq_pend_list));
894 ASSERT(list_empty(&tq->tq_prio_list));
d9acd930 895 ASSERT(list_empty(&tq->tq_delay_list));
bcd68186 896
472a34ca 897 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
2c02b71b 898
472a34ca 899 kmem_free(tq, sizeof(taskq_t));
b123971f 900}
aed8671c 901EXPORT_SYMBOL(taskq_destroy);
e9cb2b4f
BB
902
903int
904spl_taskq_init(void)
905{
f220894e
BB
906 /* Solaris creates a dynamic taskq of up to 64 threads, however in
907 * a Linux environment 1 thread per-core is usually about right */
472a34ca 908 system_taskq = taskq_create("spl_system_taskq", num_online_cpus(),
f220894e 909 minclsyspri, 4, 512, TASKQ_PREPOPULATE);
e9cb2b4f 910 if (system_taskq == NULL)
10946b02 911 return (1);
e9cb2b4f 912
10946b02 913 return (0);
e9cb2b4f
BB
914}
915
916void
917spl_taskq_fini(void)
918{
e9cb2b4f 919 taskq_destroy(system_taskq);
e9cb2b4f 920}