]> git.proxmox.com Git - mirror_spl.git/blob - module/spl/spl-taskq.c
Add system_delay_taskq for long delay
[mirror_spl.git] / module / spl / spl-taskq.c
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>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
10 *
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
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
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * Solaris Porting Layer (SPL) Task Queue Implementation.
25 */
26
27 #include <sys/taskq.h>
28 #include <sys/kmem.h>
29 #include <sys/tsd.h>
30
31 int spl_taskq_thread_bind = 0;
32 module_param(spl_taskq_thread_bind, int, 0644);
33 MODULE_PARM_DESC(spl_taskq_thread_bind, "Bind taskq thread to CPU by default");
34
35
36 int spl_taskq_thread_dynamic = 1;
37 module_param(spl_taskq_thread_dynamic, int, 0644);
38 MODULE_PARM_DESC(spl_taskq_thread_dynamic, "Allow dynamic taskq threads");
39
40 int spl_taskq_thread_priority = 1;
41 module_param(spl_taskq_thread_priority, int, 0644);
42 MODULE_PARM_DESC(spl_taskq_thread_priority,
43 "Allow non-default priority for taskq threads");
44
45 int spl_taskq_thread_sequential = 4;
46 module_param(spl_taskq_thread_sequential, int, 0644);
47 MODULE_PARM_DESC(spl_taskq_thread_sequential,
48 "Create new taskq threads after N sequential tasks");
49
50 /* Global system-wide dynamic task queue available for all consumers */
51 taskq_t *system_taskq;
52 EXPORT_SYMBOL(system_taskq);
53 /* Global dynamic task queue for long delay */
54 taskq_t *system_delay_taskq;
55 EXPORT_SYMBOL(system_delay_taskq);
56
57 /* Private dedicated taskq for creating new taskq threads on demand. */
58 static taskq_t *dynamic_taskq;
59 static taskq_thread_t *taskq_thread_create(taskq_t *);
60
61 /* List of all taskqs */
62 LIST_HEAD(tq_list);
63 DECLARE_RWSEM(tq_list_sem);
64 static uint_t taskq_tsd;
65
66 static int
67 task_km_flags(uint_t flags)
68 {
69 if (flags & TQ_NOSLEEP)
70 return (KM_NOSLEEP);
71
72 if (flags & TQ_PUSHPAGE)
73 return (KM_PUSHPAGE);
74
75 return (KM_SLEEP);
76 }
77
78 /*
79 * taskq_find_by_name - Find the largest instance number of a named taskq.
80 */
81 static int
82 taskq_find_by_name(const char *name)
83 {
84 struct list_head *tql;
85 taskq_t *tq;
86
87 list_for_each_prev(tql, &tq_list) {
88 tq = list_entry(tql, taskq_t, tq_taskqs);
89 if (strcmp(name, tq->tq_name) == 0)
90 return tq->tq_instance;
91 }
92 return (-1);
93 }
94
95 /*
96 * NOTE: Must be called with tq->tq_lock held, returns a list_t which
97 * is not attached to the free, work, or pending taskq lists.
98 */
99 static taskq_ent_t *
100 task_alloc(taskq_t *tq, uint_t flags, unsigned long *irqflags)
101 {
102 taskq_ent_t *t;
103 int count = 0;
104
105 ASSERT(tq);
106 ASSERT(spin_is_locked(&tq->tq_lock));
107 retry:
108 /* Acquire taskq_ent_t's from free list if available */
109 if (!list_empty(&tq->tq_free_list) && !(flags & TQ_NEW)) {
110 t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
111
112 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
113 ASSERT(!(t->tqent_flags & TQENT_FLAG_CANCEL));
114 ASSERT(!timer_pending(&t->tqent_timer));
115
116 list_del_init(&t->tqent_list);
117 return (t);
118 }
119
120 /* Free list is empty and memory allocations are prohibited */
121 if (flags & TQ_NOALLOC)
122 return (NULL);
123
124 /* Hit maximum taskq_ent_t pool size */
125 if (tq->tq_nalloc >= tq->tq_maxalloc) {
126 if (flags & TQ_NOSLEEP)
127 return (NULL);
128
129 /*
130 * Sleep periodically polling the free list for an available
131 * taskq_ent_t. Dispatching with TQ_SLEEP should always succeed
132 * but we cannot block forever waiting for an taskq_ent_t to
133 * show up in the free list, otherwise a deadlock can happen.
134 *
135 * Therefore, we need to allocate a new task even if the number
136 * of allocated tasks is above tq->tq_maxalloc, but we still
137 * end up delaying the task allocation by one second, thereby
138 * throttling the task dispatch rate.
139 */
140 spin_unlock_irqrestore(&tq->tq_lock, *irqflags);
141 schedule_timeout(HZ / 100);
142 spin_lock_irqsave_nested(&tq->tq_lock, *irqflags,
143 tq->tq_lock_class);
144 if (count < 100) {
145 count++;
146 goto retry;
147 }
148 }
149
150 spin_unlock_irqrestore(&tq->tq_lock, *irqflags);
151 t = kmem_alloc(sizeof (taskq_ent_t), task_km_flags(flags));
152 spin_lock_irqsave_nested(&tq->tq_lock, *irqflags, tq->tq_lock_class);
153
154 if (t) {
155 taskq_init_ent(t);
156 tq->tq_nalloc++;
157 }
158
159 return (t);
160 }
161
162 /*
163 * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t
164 * to already be removed from the free, work, or pending taskq lists.
165 */
166 static void
167 task_free(taskq_t *tq, taskq_ent_t *t)
168 {
169 ASSERT(tq);
170 ASSERT(t);
171 ASSERT(spin_is_locked(&tq->tq_lock));
172 ASSERT(list_empty(&t->tqent_list));
173 ASSERT(!timer_pending(&t->tqent_timer));
174
175 kmem_free(t, sizeof (taskq_ent_t));
176 tq->tq_nalloc--;
177 }
178
179 /*
180 * NOTE: Must be called with tq->tq_lock held, either destroys the
181 * taskq_ent_t if too many exist or moves it to the free list for later use.
182 */
183 static void
184 task_done(taskq_t *tq, taskq_ent_t *t)
185 {
186 ASSERT(tq);
187 ASSERT(t);
188 ASSERT(spin_is_locked(&tq->tq_lock));
189
190 /* Wake tasks blocked in taskq_wait_id() */
191 wake_up_all(&t->tqent_waitq);
192
193 list_del_init(&t->tqent_list);
194
195 if (tq->tq_nalloc <= tq->tq_minalloc) {
196 t->tqent_id = TASKQID_INVALID;
197 t->tqent_func = NULL;
198 t->tqent_arg = NULL;
199 t->tqent_flags = 0;
200
201 list_add_tail(&t->tqent_list, &tq->tq_free_list);
202 } else {
203 task_free(tq, t);
204 }
205 }
206
207 /*
208 * When a delayed task timer expires remove it from the delay list and
209 * add it to the priority list in order for immediate processing.
210 */
211 static void
212 task_expire(unsigned long data)
213 {
214 taskq_ent_t *w, *t = (taskq_ent_t *)data;
215 taskq_t *tq = t->tqent_taskq;
216 struct list_head *l;
217 unsigned long flags;
218
219 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
220
221 if (t->tqent_flags & TQENT_FLAG_CANCEL) {
222 ASSERT(list_empty(&t->tqent_list));
223 spin_unlock_irqrestore(&tq->tq_lock, flags);
224 return;
225 }
226
227 t->tqent_birth = jiffies;
228 /*
229 * The priority list must be maintained in strict task id order
230 * from lowest to highest for lowest_id to be easily calculable.
231 */
232 list_del(&t->tqent_list);
233 list_for_each_prev(l, &tq->tq_prio_list) {
234 w = list_entry(l, taskq_ent_t, tqent_list);
235 if (w->tqent_id < t->tqent_id) {
236 list_add(&t->tqent_list, l);
237 break;
238 }
239 }
240 if (l == &tq->tq_prio_list)
241 list_add(&t->tqent_list, &tq->tq_prio_list);
242
243 spin_unlock_irqrestore(&tq->tq_lock, flags);
244
245 wake_up(&tq->tq_work_waitq);
246 }
247
248 /*
249 * Returns the lowest incomplete taskqid_t. The taskqid_t may
250 * be queued on the pending list, on the priority list, on the
251 * delay list, or on the work list currently being handled, but
252 * it is not 100% complete yet.
253 */
254 static taskqid_t
255 taskq_lowest_id(taskq_t *tq)
256 {
257 taskqid_t lowest_id = tq->tq_next_id;
258 taskq_ent_t *t;
259 taskq_thread_t *tqt;
260
261 ASSERT(tq);
262 ASSERT(spin_is_locked(&tq->tq_lock));
263
264 if (!list_empty(&tq->tq_pend_list)) {
265 t = list_entry(tq->tq_pend_list.next, taskq_ent_t, tqent_list);
266 lowest_id = MIN(lowest_id, t->tqent_id);
267 }
268
269 if (!list_empty(&tq->tq_prio_list)) {
270 t = list_entry(tq->tq_prio_list.next, taskq_ent_t, tqent_list);
271 lowest_id = MIN(lowest_id, t->tqent_id);
272 }
273
274 if (!list_empty(&tq->tq_delay_list)) {
275 t = list_entry(tq->tq_delay_list.next, taskq_ent_t, tqent_list);
276 lowest_id = MIN(lowest_id, t->tqent_id);
277 }
278
279 if (!list_empty(&tq->tq_active_list)) {
280 tqt = list_entry(tq->tq_active_list.next, taskq_thread_t,
281 tqt_active_list);
282 ASSERT(tqt->tqt_id != TASKQID_INVALID);
283 lowest_id = MIN(lowest_id, tqt->tqt_id);
284 }
285
286 return (lowest_id);
287 }
288
289 /*
290 * Insert a task into a list keeping the list sorted by increasing taskqid.
291 */
292 static void
293 taskq_insert_in_order(taskq_t *tq, taskq_thread_t *tqt)
294 {
295 taskq_thread_t *w;
296 struct list_head *l;
297
298 ASSERT(tq);
299 ASSERT(tqt);
300 ASSERT(spin_is_locked(&tq->tq_lock));
301
302 list_for_each_prev(l, &tq->tq_active_list) {
303 w = list_entry(l, taskq_thread_t, tqt_active_list);
304 if (w->tqt_id < tqt->tqt_id) {
305 list_add(&tqt->tqt_active_list, l);
306 break;
307 }
308 }
309 if (l == &tq->tq_active_list)
310 list_add(&tqt->tqt_active_list, &tq->tq_active_list);
311 }
312
313 /*
314 * Find and return a task from the given list if it exists. The list
315 * must be in lowest to highest task id order.
316 */
317 static taskq_ent_t *
318 taskq_find_list(taskq_t *tq, struct list_head *lh, taskqid_t id)
319 {
320 struct list_head *l;
321 taskq_ent_t *t;
322
323 ASSERT(spin_is_locked(&tq->tq_lock));
324
325 list_for_each(l, lh) {
326 t = list_entry(l, taskq_ent_t, tqent_list);
327
328 if (t->tqent_id == id)
329 return (t);
330
331 if (t->tqent_id > id)
332 break;
333 }
334
335 return (NULL);
336 }
337
338 /*
339 * Find an already dispatched task given the task id regardless of what
340 * state it is in. If a task is still pending or executing it will be
341 * returned and 'active' set appropriately. If the task has already
342 * been run then NULL is returned.
343 */
344 static taskq_ent_t *
345 taskq_find(taskq_t *tq, taskqid_t id, int *active)
346 {
347 taskq_thread_t *tqt;
348 struct list_head *l;
349 taskq_ent_t *t;
350
351 ASSERT(spin_is_locked(&tq->tq_lock));
352 *active = 0;
353
354 t = taskq_find_list(tq, &tq->tq_delay_list, id);
355 if (t)
356 return (t);
357
358 t = taskq_find_list(tq, &tq->tq_prio_list, id);
359 if (t)
360 return (t);
361
362 t = taskq_find_list(tq, &tq->tq_pend_list, id);
363 if (t)
364 return (t);
365
366 list_for_each(l, &tq->tq_active_list) {
367 tqt = list_entry(l, taskq_thread_t, tqt_active_list);
368 if (tqt->tqt_id == id) {
369 t = tqt->tqt_task;
370 *active = 1;
371 return (t);
372 }
373 }
374
375 return (NULL);
376 }
377
378 /*
379 * Theory for the taskq_wait_id(), taskq_wait_outstanding(), and
380 * taskq_wait() functions below.
381 *
382 * Taskq waiting is accomplished by tracking the lowest outstanding task
383 * id and the next available task id. As tasks are dispatched they are
384 * added to the tail of the pending, priority, or delay lists. As worker
385 * threads become available the tasks are removed from the heads of these
386 * lists and linked to the worker threads. This ensures the lists are
387 * kept sorted by lowest to highest task id.
388 *
389 * Therefore the lowest outstanding task id can be quickly determined by
390 * checking the head item from all of these lists. This value is stored
391 * with the taskq as the lowest id. It only needs to be recalculated when
392 * either the task with the current lowest id completes or is canceled.
393 *
394 * By blocking until the lowest task id exceeds the passed task id the
395 * taskq_wait_outstanding() function can be easily implemented. Similarly,
396 * by blocking until the lowest task id matches the next task id taskq_wait()
397 * can be implemented.
398 *
399 * Callers should be aware that when there are multiple worked threads it
400 * is possible for larger task ids to complete before smaller ones. Also
401 * when the taskq contains delay tasks with small task ids callers may
402 * block for a considerable length of time waiting for them to expire and
403 * execute.
404 */
405 static int
406 taskq_wait_id_check(taskq_t *tq, taskqid_t id)
407 {
408 int active = 0;
409 int rc;
410 unsigned long flags;
411
412 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
413 rc = (taskq_find(tq, id, &active) == NULL);
414 spin_unlock_irqrestore(&tq->tq_lock, flags);
415
416 return (rc);
417 }
418
419 /*
420 * The taskq_wait_id() function blocks until the passed task id completes.
421 * This does not guarantee that all lower task ids have completed.
422 */
423 void
424 taskq_wait_id(taskq_t *tq, taskqid_t id)
425 {
426 wait_event(tq->tq_wait_waitq, taskq_wait_id_check(tq, id));
427 }
428 EXPORT_SYMBOL(taskq_wait_id);
429
430 static int
431 taskq_wait_outstanding_check(taskq_t *tq, taskqid_t id)
432 {
433 int rc;
434 unsigned long flags;
435
436 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
437 rc = (id < tq->tq_lowest_id);
438 spin_unlock_irqrestore(&tq->tq_lock, flags);
439
440 return (rc);
441 }
442
443 /*
444 * The taskq_wait_outstanding() function will block until all tasks with a
445 * lower taskqid than the passed 'id' have been completed. Note that all
446 * task id's are assigned monotonically at dispatch time. Zero may be
447 * passed for the id to indicate all tasks dispatch up to this point,
448 * but not after, should be waited for.
449 */
450 void
451 taskq_wait_outstanding(taskq_t *tq, taskqid_t id)
452 {
453 id = id ? id : tq->tq_next_id - 1;
454 wait_event(tq->tq_wait_waitq, taskq_wait_outstanding_check(tq, id));
455 }
456 EXPORT_SYMBOL(taskq_wait_outstanding);
457
458 static int
459 taskq_wait_check(taskq_t *tq)
460 {
461 int rc;
462 unsigned long flags;
463
464 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
465 rc = (tq->tq_lowest_id == tq->tq_next_id);
466 spin_unlock_irqrestore(&tq->tq_lock, flags);
467
468 return (rc);
469 }
470
471 /*
472 * The taskq_wait() function will block until the taskq is empty.
473 * This means that if a taskq re-dispatches work to itself taskq_wait()
474 * callers will block indefinitely.
475 */
476 void
477 taskq_wait(taskq_t *tq)
478 {
479 wait_event(tq->tq_wait_waitq, taskq_wait_check(tq));
480 }
481 EXPORT_SYMBOL(taskq_wait);
482
483 int
484 taskq_member(taskq_t *tq, kthread_t *t)
485 {
486 return (tq == (taskq_t *)tsd_get_by_thread(taskq_tsd, t));
487 }
488 EXPORT_SYMBOL(taskq_member);
489
490 /*
491 * Cancel an already dispatched task given the task id. Still pending tasks
492 * will be immediately canceled, and if the task is active the function will
493 * block until it completes. Preallocated tasks which are canceled must be
494 * freed by the caller.
495 */
496 int
497 taskq_cancel_id(taskq_t *tq, taskqid_t id)
498 {
499 taskq_ent_t *t;
500 int active = 0;
501 int rc = ENOENT;
502 unsigned long flags;
503
504 ASSERT(tq);
505
506 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
507 t = taskq_find(tq, id, &active);
508 if (t && !active) {
509 list_del_init(&t->tqent_list);
510 t->tqent_flags |= TQENT_FLAG_CANCEL;
511
512 /*
513 * When canceling the lowest outstanding task id we
514 * must recalculate the new lowest outstanding id.
515 */
516 if (tq->tq_lowest_id == t->tqent_id) {
517 tq->tq_lowest_id = taskq_lowest_id(tq);
518 ASSERT3S(tq->tq_lowest_id, >, t->tqent_id);
519 }
520
521 /*
522 * The task_expire() function takes the tq->tq_lock so drop
523 * drop the lock before synchronously cancelling the timer.
524 */
525 if (timer_pending(&t->tqent_timer)) {
526 spin_unlock_irqrestore(&tq->tq_lock, flags);
527 del_timer_sync(&t->tqent_timer);
528 spin_lock_irqsave_nested(&tq->tq_lock, flags,
529 tq->tq_lock_class);
530 }
531
532 if (!(t->tqent_flags & TQENT_FLAG_PREALLOC))
533 task_done(tq, t);
534
535 rc = 0;
536 }
537 spin_unlock_irqrestore(&tq->tq_lock, flags);
538
539 if (active) {
540 taskq_wait_id(tq, id);
541 rc = EBUSY;
542 }
543
544 return (rc);
545 }
546 EXPORT_SYMBOL(taskq_cancel_id);
547
548 static int taskq_thread_spawn(taskq_t *tq);
549
550 taskqid_t
551 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
552 {
553 taskq_ent_t *t;
554 taskqid_t rc = TASKQID_INVALID;
555 unsigned long irqflags;
556
557 ASSERT(tq);
558 ASSERT(func);
559
560 spin_lock_irqsave_nested(&tq->tq_lock, irqflags, tq->tq_lock_class);
561
562 /* Taskq being destroyed and all tasks drained */
563 if (!(tq->tq_flags & TASKQ_ACTIVE))
564 goto out;
565
566 /* Do not queue the task unless there is idle thread for it */
567 ASSERT(tq->tq_nactive <= tq->tq_nthreads);
568 if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads)) {
569 /* Dynamic taskq may be able to spawn another thread */
570 if (!(tq->tq_flags & TASKQ_DYNAMIC) || taskq_thread_spawn(tq) == 0)
571 goto out;
572 }
573
574 if ((t = task_alloc(tq, flags, &irqflags)) == NULL)
575 goto out;
576
577 spin_lock(&t->tqent_lock);
578
579 /* Queue to the front of the list to enforce TQ_NOQUEUE semantics */
580 if (flags & TQ_NOQUEUE)
581 list_add(&t->tqent_list, &tq->tq_prio_list);
582 /* Queue to the priority list instead of the pending list */
583 else if (flags & TQ_FRONT)
584 list_add_tail(&t->tqent_list, &tq->tq_prio_list);
585 else
586 list_add_tail(&t->tqent_list, &tq->tq_pend_list);
587
588 t->tqent_id = rc = tq->tq_next_id;
589 tq->tq_next_id++;
590 t->tqent_func = func;
591 t->tqent_arg = arg;
592 t->tqent_taskq = tq;
593 t->tqent_timer.data = 0;
594 t->tqent_timer.function = NULL;
595 t->tqent_timer.expires = 0;
596 t->tqent_birth = jiffies;
597
598 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
599
600 spin_unlock(&t->tqent_lock);
601
602 wake_up(&tq->tq_work_waitq);
603 out:
604 /* Spawn additional taskq threads if required. */
605 if (!(flags & TQ_NOQUEUE) && tq->tq_nactive == tq->tq_nthreads)
606 (void) taskq_thread_spawn(tq);
607
608 spin_unlock_irqrestore(&tq->tq_lock, irqflags);
609 return (rc);
610 }
611 EXPORT_SYMBOL(taskq_dispatch);
612
613 taskqid_t
614 taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg,
615 uint_t flags, clock_t expire_time)
616 {
617 taskqid_t rc = TASKQID_INVALID;
618 taskq_ent_t *t;
619 unsigned long irqflags;
620
621 ASSERT(tq);
622 ASSERT(func);
623
624 spin_lock_irqsave_nested(&tq->tq_lock, irqflags, tq->tq_lock_class);
625
626 /* Taskq being destroyed and all tasks drained */
627 if (!(tq->tq_flags & TASKQ_ACTIVE))
628 goto out;
629
630 if ((t = task_alloc(tq, flags, &irqflags)) == NULL)
631 goto out;
632
633 spin_lock(&t->tqent_lock);
634
635 /* Queue to the delay list for subsequent execution */
636 list_add_tail(&t->tqent_list, &tq->tq_delay_list);
637
638 t->tqent_id = rc = tq->tq_next_id;
639 tq->tq_next_id++;
640 t->tqent_func = func;
641 t->tqent_arg = arg;
642 t->tqent_taskq = tq;
643 t->tqent_timer.data = (unsigned long)t;
644 t->tqent_timer.function = task_expire;
645 t->tqent_timer.expires = (unsigned long)expire_time;
646 add_timer(&t->tqent_timer);
647
648 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
649
650 spin_unlock(&t->tqent_lock);
651 out:
652 /* Spawn additional taskq threads if required. */
653 if (tq->tq_nactive == tq->tq_nthreads)
654 (void) taskq_thread_spawn(tq);
655 spin_unlock_irqrestore(&tq->tq_lock, irqflags);
656 return (rc);
657 }
658 EXPORT_SYMBOL(taskq_dispatch_delay);
659
660 void
661 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
662 taskq_ent_t *t)
663 {
664 unsigned long irqflags;
665 ASSERT(tq);
666 ASSERT(func);
667
668 spin_lock_irqsave_nested(&tq->tq_lock, irqflags,
669 tq->tq_lock_class);
670
671 /* Taskq being destroyed and all tasks drained */
672 if (!(tq->tq_flags & TASKQ_ACTIVE)) {
673 t->tqent_id = TASKQID_INVALID;
674 goto out;
675 }
676
677 if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads)) {
678 /* Dynamic taskq may be able to spawn another thread */
679 if (!(tq->tq_flags & TASKQ_DYNAMIC) || taskq_thread_spawn(tq) == 0)
680 goto out2;
681 flags |= TQ_FRONT;
682 }
683
684 spin_lock(&t->tqent_lock);
685
686 /*
687 * Mark it as a prealloc'd task. This is important
688 * to ensure that we don't free it later.
689 */
690 t->tqent_flags |= TQENT_FLAG_PREALLOC;
691
692 /* Queue to the priority list instead of the pending list */
693 if (flags & TQ_FRONT)
694 list_add_tail(&t->tqent_list, &tq->tq_prio_list);
695 else
696 list_add_tail(&t->tqent_list, &tq->tq_pend_list);
697
698 t->tqent_id = tq->tq_next_id;
699 tq->tq_next_id++;
700 t->tqent_func = func;
701 t->tqent_arg = arg;
702 t->tqent_taskq = tq;
703 t->tqent_birth = jiffies;
704
705 spin_unlock(&t->tqent_lock);
706
707 wake_up(&tq->tq_work_waitq);
708 out:
709 /* Spawn additional taskq threads if required. */
710 if (tq->tq_nactive == tq->tq_nthreads)
711 (void) taskq_thread_spawn(tq);
712 out2:
713 spin_unlock_irqrestore(&tq->tq_lock, irqflags);
714 }
715 EXPORT_SYMBOL(taskq_dispatch_ent);
716
717 int
718 taskq_empty_ent(taskq_ent_t *t)
719 {
720 return (list_empty(&t->tqent_list));
721 }
722 EXPORT_SYMBOL(taskq_empty_ent);
723
724 void
725 taskq_init_ent(taskq_ent_t *t)
726 {
727 spin_lock_init(&t->tqent_lock);
728 init_waitqueue_head(&t->tqent_waitq);
729 init_timer(&t->tqent_timer);
730 INIT_LIST_HEAD(&t->tqent_list);
731 t->tqent_id = 0;
732 t->tqent_func = NULL;
733 t->tqent_arg = NULL;
734 t->tqent_flags = 0;
735 t->tqent_taskq = NULL;
736 }
737 EXPORT_SYMBOL(taskq_init_ent);
738
739 /*
740 * Return the next pending task, preference is given to tasks on the
741 * priority list which were dispatched with TQ_FRONT.
742 */
743 static taskq_ent_t *
744 taskq_next_ent(taskq_t *tq)
745 {
746 struct list_head *list;
747
748 ASSERT(spin_is_locked(&tq->tq_lock));
749
750 if (!list_empty(&tq->tq_prio_list))
751 list = &tq->tq_prio_list;
752 else if (!list_empty(&tq->tq_pend_list))
753 list = &tq->tq_pend_list;
754 else
755 return (NULL);
756
757 return (list_entry(list->next, taskq_ent_t, tqent_list));
758 }
759
760 /*
761 * Spawns a new thread for the specified taskq.
762 */
763 static void
764 taskq_thread_spawn_task(void *arg)
765 {
766 taskq_t *tq = (taskq_t *)arg;
767 unsigned long flags;
768
769 if (taskq_thread_create(tq) == NULL) {
770 /* restore spawning count if failed */
771 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
772 tq->tq_nspawn--;
773 spin_unlock_irqrestore(&tq->tq_lock, flags);
774 }
775 }
776
777 /*
778 * Spawn addition threads for dynamic taskqs (TASKQ_DYNAMIC) the current
779 * number of threads is insufficient to handle the pending tasks. These
780 * new threads must be created by the dedicated dynamic_taskq to avoid
781 * deadlocks between thread creation and memory reclaim. The system_taskq
782 * which is also a dynamic taskq cannot be safely used for this.
783 */
784 static int
785 taskq_thread_spawn(taskq_t *tq)
786 {
787 int spawning = 0;
788
789 if (!(tq->tq_flags & TASKQ_DYNAMIC))
790 return (0);
791
792 if ((tq->tq_nthreads + tq->tq_nspawn < tq->tq_maxthreads) &&
793 (tq->tq_flags & TASKQ_ACTIVE)) {
794 spawning = (++tq->tq_nspawn);
795 taskq_dispatch(dynamic_taskq, taskq_thread_spawn_task,
796 tq, TQ_NOSLEEP);
797 }
798
799 return (spawning);
800 }
801
802 /*
803 * Threads in a dynamic taskq should only exit once it has been completely
804 * drained and no other threads are actively servicing tasks. This prevents
805 * threads from being created and destroyed more than is required.
806 *
807 * The first thread is the thread list is treated as the primary thread.
808 * There is nothing special about the primary thread but in order to avoid
809 * all the taskq pids from changing we opt to make it long running.
810 */
811 static int
812 taskq_thread_should_stop(taskq_t *tq, taskq_thread_t *tqt)
813 {
814 ASSERT(spin_is_locked(&tq->tq_lock));
815
816 if (!(tq->tq_flags & TASKQ_DYNAMIC))
817 return (0);
818
819 if (list_first_entry(&(tq->tq_thread_list), taskq_thread_t,
820 tqt_thread_list) == tqt)
821 return (0);
822
823 return
824 ((tq->tq_nspawn == 0) && /* No threads are being spawned */
825 (tq->tq_nactive == 0) && /* No threads are handling tasks */
826 (tq->tq_nthreads > 1) && /* More than 1 thread is running */
827 (!taskq_next_ent(tq)) && /* There are no pending tasks */
828 (spl_taskq_thread_dynamic)); /* Dynamic taskqs are allowed */
829 }
830
831 static int
832 taskq_thread(void *args)
833 {
834 DECLARE_WAITQUEUE(wait, current);
835 sigset_t blocked;
836 taskq_thread_t *tqt = args;
837 taskq_t *tq;
838 taskq_ent_t *t;
839 int seq_tasks = 0;
840 unsigned long flags;
841
842 ASSERT(tqt);
843 ASSERT(tqt->tqt_tq);
844 tq = tqt->tqt_tq;
845 current->flags |= PF_NOFREEZE;
846
847 (void) spl_fstrans_mark();
848
849 sigfillset(&blocked);
850 sigprocmask(SIG_BLOCK, &blocked, NULL);
851 flush_signals(current);
852
853 tsd_set(taskq_tsd, tq);
854 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
855 /*
856 * If we are dynamically spawned, decrease spawning count. Note that
857 * we could be created during taskq_create, in which case we shouldn't
858 * do the decrement. But it's fine because taskq_create will reset
859 * tq_nspawn later.
860 */
861 if (tq->tq_flags & TASKQ_DYNAMIC)
862 tq->tq_nspawn--;
863
864 /* Immediately exit if more threads than allowed were created. */
865 if (tq->tq_nthreads >= tq->tq_maxthreads)
866 goto error;
867
868 tq->tq_nthreads++;
869 list_add_tail(&tqt->tqt_thread_list, &tq->tq_thread_list);
870 wake_up(&tq->tq_wait_waitq);
871 set_current_state(TASK_INTERRUPTIBLE);
872
873 while (!kthread_should_stop()) {
874
875 if (list_empty(&tq->tq_pend_list) &&
876 list_empty(&tq->tq_prio_list)) {
877
878 if (taskq_thread_should_stop(tq, tqt)) {
879 wake_up_all(&tq->tq_wait_waitq);
880 break;
881 }
882
883 add_wait_queue_exclusive(&tq->tq_work_waitq, &wait);
884 spin_unlock_irqrestore(&tq->tq_lock, flags);
885
886 schedule();
887 seq_tasks = 0;
888
889 spin_lock_irqsave_nested(&tq->tq_lock, flags,
890 tq->tq_lock_class);
891 remove_wait_queue(&tq->tq_work_waitq, &wait);
892 } else {
893 __set_current_state(TASK_RUNNING);
894 }
895
896 if ((t = taskq_next_ent(tq)) != NULL) {
897 list_del_init(&t->tqent_list);
898
899 /*
900 * In order to support recursively dispatching a
901 * preallocated taskq_ent_t, tqent_id must be
902 * stored prior to executing tqent_func.
903 */
904 tqt->tqt_id = t->tqent_id;
905 tqt->tqt_task = t;
906
907 /*
908 * We must store a copy of the flags prior to
909 * servicing the task (servicing a prealloc'd task
910 * returns the ownership of the tqent back to
911 * the caller of taskq_dispatch). Thus,
912 * tqent_flags _may_ change within the call.
913 */
914 tqt->tqt_flags = t->tqent_flags;
915
916 taskq_insert_in_order(tq, tqt);
917 tq->tq_nactive++;
918 spin_unlock_irqrestore(&tq->tq_lock, flags);
919
920 /* Perform the requested task */
921 t->tqent_func(t->tqent_arg);
922
923 spin_lock_irqsave_nested(&tq->tq_lock, flags,
924 tq->tq_lock_class);
925 tq->tq_nactive--;
926 list_del_init(&tqt->tqt_active_list);
927 tqt->tqt_task = NULL;
928
929 /* For prealloc'd tasks, we don't free anything. */
930 if (!(tqt->tqt_flags & TQENT_FLAG_PREALLOC))
931 task_done(tq, t);
932
933 /*
934 * When the current lowest outstanding taskqid is
935 * done calculate the new lowest outstanding id
936 */
937 if (tq->tq_lowest_id == tqt->tqt_id) {
938 tq->tq_lowest_id = taskq_lowest_id(tq);
939 ASSERT3S(tq->tq_lowest_id, >, tqt->tqt_id);
940 }
941
942 /* Spawn additional taskq threads if required. */
943 if ((++seq_tasks) > spl_taskq_thread_sequential &&
944 taskq_thread_spawn(tq))
945 seq_tasks = 0;
946
947 tqt->tqt_id = TASKQID_INVALID;
948 tqt->tqt_flags = 0;
949 wake_up_all(&tq->tq_wait_waitq);
950 } else {
951 if (taskq_thread_should_stop(tq, tqt))
952 break;
953 }
954
955 set_current_state(TASK_INTERRUPTIBLE);
956
957 }
958
959 __set_current_state(TASK_RUNNING);
960 tq->tq_nthreads--;
961 list_del_init(&tqt->tqt_thread_list);
962 error:
963 kmem_free(tqt, sizeof (taskq_thread_t));
964 spin_unlock_irqrestore(&tq->tq_lock, flags);
965
966 tsd_set(taskq_tsd, NULL);
967
968 return (0);
969 }
970
971 static taskq_thread_t *
972 taskq_thread_create(taskq_t *tq)
973 {
974 static int last_used_cpu = 0;
975 taskq_thread_t *tqt;
976
977 tqt = kmem_alloc(sizeof (*tqt), KM_PUSHPAGE);
978 INIT_LIST_HEAD(&tqt->tqt_thread_list);
979 INIT_LIST_HEAD(&tqt->tqt_active_list);
980 tqt->tqt_tq = tq;
981 tqt->tqt_id = TASKQID_INVALID;
982
983 tqt->tqt_thread = spl_kthread_create(taskq_thread, tqt,
984 "%s", tq->tq_name);
985 if (tqt->tqt_thread == NULL) {
986 kmem_free(tqt, sizeof (taskq_thread_t));
987 return (NULL);
988 }
989
990 if (spl_taskq_thread_bind) {
991 last_used_cpu = (last_used_cpu + 1) % num_online_cpus();
992 kthread_bind(tqt->tqt_thread, last_used_cpu);
993 }
994
995 if (spl_taskq_thread_priority)
996 set_user_nice(tqt->tqt_thread, PRIO_TO_NICE(tq->tq_pri));
997
998 wake_up_process(tqt->tqt_thread);
999
1000 return (tqt);
1001 }
1002
1003 taskq_t *
1004 taskq_create(const char *name, int nthreads, pri_t pri,
1005 int minalloc, int maxalloc, uint_t flags)
1006 {
1007 taskq_t *tq;
1008 taskq_thread_t *tqt;
1009 int count = 0, rc = 0, i;
1010 unsigned long irqflags;
1011
1012 ASSERT(name != NULL);
1013 ASSERT(minalloc >= 0);
1014 ASSERT(maxalloc <= INT_MAX);
1015 ASSERT(!(flags & (TASKQ_CPR_SAFE))); /* Unsupported */
1016
1017 /* Scale the number of threads using nthreads as a percentage */
1018 if (flags & TASKQ_THREADS_CPU_PCT) {
1019 ASSERT(nthreads <= 100);
1020 ASSERT(nthreads >= 0);
1021 nthreads = MIN(nthreads, 100);
1022 nthreads = MAX(nthreads, 0);
1023 nthreads = MAX((num_online_cpus() * nthreads) / 100, 1);
1024 }
1025
1026 tq = kmem_alloc(sizeof (*tq), KM_PUSHPAGE);
1027 if (tq == NULL)
1028 return (NULL);
1029
1030 spin_lock_init(&tq->tq_lock);
1031 INIT_LIST_HEAD(&tq->tq_thread_list);
1032 INIT_LIST_HEAD(&tq->tq_active_list);
1033 tq->tq_name = strdup(name);
1034 tq->tq_nactive = 0;
1035 tq->tq_nthreads = 0;
1036 tq->tq_nspawn = 0;
1037 tq->tq_maxthreads = nthreads;
1038 tq->tq_pri = pri;
1039 tq->tq_minalloc = minalloc;
1040 tq->tq_maxalloc = maxalloc;
1041 tq->tq_nalloc = 0;
1042 tq->tq_flags = (flags | TASKQ_ACTIVE);
1043 tq->tq_next_id = TASKQID_INITIAL;
1044 tq->tq_lowest_id = TASKQID_INITIAL;
1045 INIT_LIST_HEAD(&tq->tq_free_list);
1046 INIT_LIST_HEAD(&tq->tq_pend_list);
1047 INIT_LIST_HEAD(&tq->tq_prio_list);
1048 INIT_LIST_HEAD(&tq->tq_delay_list);
1049 init_waitqueue_head(&tq->tq_work_waitq);
1050 init_waitqueue_head(&tq->tq_wait_waitq);
1051 tq->tq_lock_class = TQ_LOCK_GENERAL;
1052 INIT_LIST_HEAD(&tq->tq_taskqs);
1053
1054 if (flags & TASKQ_PREPOPULATE) {
1055 spin_lock_irqsave_nested(&tq->tq_lock, irqflags,
1056 tq->tq_lock_class);
1057
1058 for (i = 0; i < minalloc; i++)
1059 task_done(tq, task_alloc(tq, TQ_PUSHPAGE | TQ_NEW,
1060 &irqflags));
1061
1062 spin_unlock_irqrestore(&tq->tq_lock, irqflags);
1063 }
1064
1065 if ((flags & TASKQ_DYNAMIC) && spl_taskq_thread_dynamic)
1066 nthreads = 1;
1067
1068 for (i = 0; i < nthreads; i++) {
1069 tqt = taskq_thread_create(tq);
1070 if (tqt == NULL)
1071 rc = 1;
1072 else
1073 count++;
1074 }
1075
1076 /* Wait for all threads to be started before potential destroy */
1077 wait_event(tq->tq_wait_waitq, tq->tq_nthreads == count);
1078 /*
1079 * taskq_thread might have touched nspawn, but we don't want them to
1080 * because they're not dynamically spawned. So we reset it to 0
1081 */
1082 tq->tq_nspawn = 0;
1083
1084 if (rc) {
1085 taskq_destroy(tq);
1086 tq = NULL;
1087 } else {
1088 down_write(&tq_list_sem);
1089 tq->tq_instance = taskq_find_by_name(name) + 1;
1090 list_add_tail(&tq->tq_taskqs, &tq_list);
1091 up_write(&tq_list_sem);
1092 }
1093
1094 return (tq);
1095 }
1096 EXPORT_SYMBOL(taskq_create);
1097
1098 void
1099 taskq_destroy(taskq_t *tq)
1100 {
1101 struct task_struct *thread;
1102 taskq_thread_t *tqt;
1103 taskq_ent_t *t;
1104 unsigned long flags;
1105
1106 ASSERT(tq);
1107 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
1108 tq->tq_flags &= ~TASKQ_ACTIVE;
1109 spin_unlock_irqrestore(&tq->tq_lock, flags);
1110
1111 /*
1112 * When TASKQ_ACTIVE is clear new tasks may not be added nor may
1113 * new worker threads be spawned for dynamic taskq.
1114 */
1115 if (dynamic_taskq != NULL)
1116 taskq_wait_outstanding(dynamic_taskq, 0);
1117
1118 taskq_wait(tq);
1119
1120 /* remove taskq from global list used by the kstats */
1121 down_write(&tq_list_sem);
1122 list_del(&tq->tq_taskqs);
1123 up_write(&tq_list_sem);
1124
1125 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
1126 /* wait for spawning threads to insert themselves to the list */
1127 while (tq->tq_nspawn) {
1128 spin_unlock_irqrestore(&tq->tq_lock, flags);
1129 schedule_timeout_interruptible(1);
1130 spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
1131 }
1132
1133 /*
1134 * Signal each thread to exit and block until it does. Each thread
1135 * is responsible for removing itself from the list and freeing its
1136 * taskq_thread_t. This allows for idle threads to opt to remove
1137 * themselves from the taskq. They can be recreated as needed.
1138 */
1139 while (!list_empty(&tq->tq_thread_list)) {
1140 tqt = list_entry(tq->tq_thread_list.next,
1141 taskq_thread_t, tqt_thread_list);
1142 thread = tqt->tqt_thread;
1143 spin_unlock_irqrestore(&tq->tq_lock, flags);
1144
1145 kthread_stop(thread);
1146
1147 spin_lock_irqsave_nested(&tq->tq_lock, flags,
1148 tq->tq_lock_class);
1149 }
1150
1151 while (!list_empty(&tq->tq_free_list)) {
1152 t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
1153
1154 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
1155
1156 list_del_init(&t->tqent_list);
1157 task_free(tq, t);
1158 }
1159
1160 ASSERT0(tq->tq_nthreads);
1161 ASSERT0(tq->tq_nalloc);
1162 ASSERT0(tq->tq_nspawn);
1163 ASSERT(list_empty(&tq->tq_thread_list));
1164 ASSERT(list_empty(&tq->tq_active_list));
1165 ASSERT(list_empty(&tq->tq_free_list));
1166 ASSERT(list_empty(&tq->tq_pend_list));
1167 ASSERT(list_empty(&tq->tq_prio_list));
1168 ASSERT(list_empty(&tq->tq_delay_list));
1169
1170 spin_unlock_irqrestore(&tq->tq_lock, flags);
1171
1172 strfree(tq->tq_name);
1173 kmem_free(tq, sizeof (taskq_t));
1174 }
1175 EXPORT_SYMBOL(taskq_destroy);
1176
1177
1178 static unsigned int spl_taskq_kick = 0;
1179
1180 /*
1181 * 2.6.36 API Change
1182 * module_param_cb is introduced to take kernel_param_ops and
1183 * module_param_call is marked as obsolete. Also set and get operations
1184 * were changed to take a 'const struct kernel_param *'.
1185 */
1186 static int
1187 #ifdef module_param_cb
1188 param_set_taskq_kick(const char *val, const struct kernel_param *kp)
1189 #else
1190 param_set_taskq_kick(const char *val, struct kernel_param *kp)
1191 #endif
1192 {
1193 int ret;
1194 taskq_t *tq;
1195 taskq_ent_t *t;
1196 unsigned long flags;
1197
1198 ret = param_set_uint(val, kp);
1199 if (ret < 0 || !spl_taskq_kick)
1200 return (ret);
1201 /* reset value */
1202 spl_taskq_kick = 0;
1203
1204 down_read(&tq_list_sem);
1205 list_for_each_entry(tq, &tq_list, tq_taskqs) {
1206 spin_lock_irqsave_nested(&tq->tq_lock, flags,
1207 tq->tq_lock_class);
1208 /* Check if the first pending is older than 5 seconds */
1209 t = taskq_next_ent(tq);
1210 if (t && time_after(jiffies, t->tqent_birth + 5*HZ)) {
1211 (void) taskq_thread_spawn(tq);
1212 printk(KERN_INFO "spl: Kicked taskq %s/%d\n",
1213 tq->tq_name, tq->tq_instance);
1214 }
1215 spin_unlock_irqrestore(&tq->tq_lock, flags);
1216 }
1217 up_read(&tq_list_sem);
1218 return (ret);
1219 }
1220
1221 #ifdef module_param_cb
1222 static const struct kernel_param_ops param_ops_taskq_kick = {
1223 .set = param_set_taskq_kick,
1224 .get = param_get_uint,
1225 };
1226 module_param_cb(spl_taskq_kick, &param_ops_taskq_kick, &spl_taskq_kick, 0644);
1227 #else
1228 module_param_call(spl_taskq_kick, param_set_taskq_kick, param_get_uint,
1229 &spl_taskq_kick, 0644);
1230 #endif
1231 MODULE_PARM_DESC(spl_taskq_kick,
1232 "Write nonzero to kick stuck taskqs to spawn more threads");
1233
1234 int
1235 spl_taskq_init(void)
1236 {
1237 tsd_create(&taskq_tsd, NULL);
1238
1239 system_taskq = taskq_create("spl_system_taskq", MAX(boot_ncpus, 64),
1240 maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE|TASKQ_DYNAMIC);
1241 if (system_taskq == NULL)
1242 return (1);
1243
1244 system_delay_taskq = taskq_create("spl_delay_taskq", MAX(boot_ncpus, 4),
1245 maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE|TASKQ_DYNAMIC);
1246 if (system_delay_taskq == NULL) {
1247 taskq_destroy(system_taskq);
1248 return (1);
1249 }
1250
1251 dynamic_taskq = taskq_create("spl_dynamic_taskq", 1,
1252 maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE);
1253 if (dynamic_taskq == NULL) {
1254 taskq_destroy(system_taskq);
1255 taskq_destroy(system_delay_taskq);
1256 return (1);
1257 }
1258
1259 /*
1260 * This is used to annotate tq_lock, so
1261 * taskq_dispatch -> taskq_thread_spawn -> taskq_dispatch
1262 * does not trigger a lockdep warning re: possible recursive locking
1263 */
1264 dynamic_taskq->tq_lock_class = TQ_LOCK_DYNAMIC;
1265
1266 return (0);
1267 }
1268
1269 void
1270 spl_taskq_fini(void)
1271 {
1272 taskq_destroy(dynamic_taskq);
1273 dynamic_taskq = NULL;
1274
1275 taskq_destroy(system_delay_taskq);
1276 system_delay_taskq = NULL;
1277
1278 taskq_destroy(system_taskq);
1279 system_taskq = NULL;
1280
1281 tsd_destroy(&taskq_tsd);
1282 }