]> git.proxmox.com Git - mirror_spl.git/blob - module/spl/spl-taskq.c
951298d9fa7fc74f7bbff0a7742b7174ed77ed2f
[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
30 int spl_taskq_thread_bind = 0;
31 module_param(spl_taskq_thread_bind, int, 0644);
32 MODULE_PARM_DESC(spl_taskq_thread_bind, "Bind taskq thread to CPU by default");
33
34 /* Global system-wide dynamic task queue available for all consumers */
35 taskq_t *system_taskq;
36 EXPORT_SYMBOL(system_taskq);
37
38 static int
39 task_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
50 /*
51 * NOTE: Must be called with tq->tq_lock held, returns a list_t which
52 * is not attached to the free, work, or pending taskq lists.
53 */
54 static taskq_ent_t *
55 task_alloc(taskq_t *tq, uint_t flags)
56 {
57 taskq_ent_t *t;
58 int count = 0;
59
60 ASSERT(tq);
61 ASSERT(spin_is_locked(&tq->tq_lock));
62 retry:
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));
68 ASSERT(!(t->tqent_flags & TQENT_FLAG_CANCEL));
69 ASSERT(!timer_pending(&t->tqent_timer));
70
71 list_del_init(&t->tqent_list);
72 return (t);
73 }
74
75 /* Free list is empty and memory allocations are prohibited */
76 if (flags & TQ_NOALLOC)
77 return (NULL);
78
79 /* Hit maximum taskq_ent_t pool size */
80 if (tq->tq_nalloc >= tq->tq_maxalloc) {
81 if (flags & TQ_NOSLEEP)
82 return (NULL);
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);
98 if (count < 100) {
99 count++;
100 goto retry;
101 }
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
113 return (t);
114 }
115
116 /*
117 * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t
118 * to already be removed from the free, work, or pending taskq lists.
119 */
120 static void
121 task_free(taskq_t *tq, taskq_ent_t *t)
122 {
123 ASSERT(tq);
124 ASSERT(t);
125 ASSERT(spin_is_locked(&tq->tq_lock));
126 ASSERT(list_empty(&t->tqent_list));
127 ASSERT(!timer_pending(&t->tqent_timer));
128
129 kmem_free(t, sizeof(taskq_ent_t));
130 tq->tq_nalloc--;
131 }
132
133 /*
134 * NOTE: Must be called with tq->tq_lock held, either destroys the
135 * taskq_ent_t if too many exist or moves it to the free list for later use.
136 */
137 static void
138 task_done(taskq_t *tq, taskq_ent_t *t)
139 {
140 ASSERT(tq);
141 ASSERT(t);
142 ASSERT(spin_is_locked(&tq->tq_lock));
143
144 /* Wake tasks blocked in taskq_wait_id() */
145 wake_up_all(&t->tqent_waitq);
146
147 list_del_init(&t->tqent_list);
148
149 if (tq->tq_nalloc <= tq->tq_minalloc) {
150 t->tqent_id = 0;
151 t->tqent_func = NULL;
152 t->tqent_arg = NULL;
153 t->tqent_flags = 0;
154
155 list_add_tail(&t->tqent_list, &tq->tq_free_list);
156 } else {
157 task_free(tq, t);
158 }
159 }
160
161 /*
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.
164 */
165 static void
166 task_expire(unsigned long data)
167 {
168 taskq_ent_t *w, *t = (taskq_ent_t *)data;
169 taskq_t *tq = t->tqent_taskq;
170 struct list_head *l;
171
172 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
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
195 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
196
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 */
206 static taskqid_t
207 taskq_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;
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
238 return (lowest_id);
239 }
240
241 /*
242 * Insert a task into a list keeping the list sorted by increasing taskqid.
243 */
244 static void
245 taskq_insert_in_order(taskq_t *tq, taskq_thread_t *tqt)
246 {
247 taskq_thread_t *w;
248 struct list_head *l;
249
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);
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 */
269 static taskq_ent_t *
270 taskq_find_list(taskq_t *tq, struct list_head *lh, taskqid_t id)
271 {
272 struct list_head *l;
273 taskq_ent_t *t;
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)
281 return (t);
282
283 if (t->tqent_id > id)
284 break;
285 }
286
287 return (NULL);
288 }
289
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 */
296 static taskq_ent_t *
297 taskq_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;
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)
308 return (t);
309
310 t = taskq_find_list(tq, &tq->tq_prio_list, id);
311 if (t)
312 return (t);
313
314 t = taskq_find_list(tq, &tq->tq_pend_list, id);
315 if (t)
316 return (t);
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;
323 return (t);
324 }
325 }
326
327 return (NULL);
328 }
329
330 static int
331 taskq_wait_id_check(taskq_t *tq, taskqid_t id)
332 {
333 int active = 0;
334 int rc;
335
336 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
337 rc = (taskq_find(tq, id, &active) == NULL);
338 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
339
340 return (rc);
341 }
342
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 */
347 void
348 taskq_wait_id(taskq_t *tq, taskqid_t id)
349 {
350 wait_event(tq->tq_wait_waitq, taskq_wait_id_check(tq, id));
351 }
352 EXPORT_SYMBOL(taskq_wait_id);
353
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 */
380 static int
381 taskq_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
389 return (rc);
390 }
391
392 void
393 taskq_wait_all(taskq_t *tq, taskqid_t id)
394 {
395 wait_event(tq->tq_wait_waitq, taskq_wait_check(tq, id));
396 }
397 EXPORT_SYMBOL(taskq_wait_all);
398
399 void
400 taskq_wait(taskq_t *tq)
401 {
402 taskqid_t id;
403
404 ASSERT(tq);
405
406 /* Wait for the largest outstanding taskqid */
407 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
408 id = tq->tq_next_id - 1;
409 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
410
411 taskq_wait_all(tq, id);
412 }
413 EXPORT_SYMBOL(taskq_wait);
414
415 int
416 taskq_member(taskq_t *tq, void *t)
417 {
418 struct list_head *l;
419 taskq_thread_t *tqt;
420
421 ASSERT(tq);
422 ASSERT(t);
423
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)
427 return (1);
428 }
429
430 return (0);
431 }
432 EXPORT_SYMBOL(taskq_member);
433
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 */
440 int
441 taskq_cancel_id(taskq_t *tq, taskqid_t id)
442 {
443 taskq_ent_t *t;
444 int active = 0;
445 int rc = ENOENT;
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
486 return (rc);
487 }
488 EXPORT_SYMBOL(taskq_cancel_id);
489
490 taskqid_t
491 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
492 {
493 taskq_ent_t *t;
494 taskqid_t rc = 0;
495
496 ASSERT(tq);
497 ASSERT(func);
498
499 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
500
501 /* Taskq being destroyed and all tasks drained */
502 if (!(tq->tq_flags & TQ_ACTIVE))
503 goto out;
504
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))
508 goto out;
509
510 if ((t = task_alloc(tq, flags)) == NULL)
511 goto out;
512
513 spin_lock(&t->tqent_lock);
514
515 /* Queue to the priority list instead of the pending list */
516 if (flags & TQ_FRONT)
517 list_add_tail(&t->tqent_list, &tq->tq_prio_list);
518 else
519 list_add_tail(&t->tqent_list, &tq->tq_pend_list);
520
521 t->tqent_id = rc = tq->tq_next_id;
522 tq->tq_next_id++;
523 t->tqent_func = func;
524 t->tqent_arg = arg;
525 t->tqent_taskq = tq;
526 t->tqent_timer.data = 0;
527 t->tqent_timer.function = NULL;
528 t->tqent_timer.expires = 0;
529
530 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
531
532 spin_unlock(&t->tqent_lock);
533
534 wake_up(&tq->tq_work_waitq);
535 out:
536 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
537 return (rc);
538 }
539 EXPORT_SYMBOL(taskq_dispatch);
540
541 taskqid_t
542 taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg,
543 uint_t flags, clock_t expire_time)
544 {
545 taskqid_t rc = 0;
546 taskq_ent_t *t;
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))
555 goto out;
556
557 if ((t = task_alloc(tq, flags)) == NULL)
558 goto out;
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);
578 out:
579 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
580 return (rc);
581 }
582 EXPORT_SYMBOL(taskq_dispatch_delay);
583
584 void
585 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
586 taskq_ent_t *t)
587 {
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;
618 t->tqent_taskq = tq;
619
620 spin_unlock(&t->tqent_lock);
621
622 wake_up(&tq->tq_work_waitq);
623 out:
624 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
625 }
626 EXPORT_SYMBOL(taskq_dispatch_ent);
627
628 int
629 taskq_empty_ent(taskq_ent_t *t)
630 {
631 return list_empty(&t->tqent_list);
632 }
633 EXPORT_SYMBOL(taskq_empty_ent);
634
635 void
636 taskq_init_ent(taskq_ent_t *t)
637 {
638 spin_lock_init(&t->tqent_lock);
639 init_waitqueue_head(&t->tqent_waitq);
640 init_timer(&t->tqent_timer);
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;
646 t->tqent_taskq = NULL;
647 }
648 EXPORT_SYMBOL(taskq_init_ent);
649
650 static int
651 taskq_thread(void *args)
652 {
653 DECLARE_WAITQUEUE(wait, current);
654 sigset_t blocked;
655 taskq_thread_t *tqt = args;
656 taskq_t *tq;
657 taskq_ent_t *t;
658 struct list_head *pend_list;
659
660 ASSERT(tqt);
661 tq = tqt->tqt_tq;
662 current->flags |= PF_NOFREEZE;
663
664 sigfillset(&blocked);
665 sigprocmask(SIG_BLOCK, &blocked, NULL);
666 flush_signals(current);
667
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);
672
673 while (!kthread_should_stop()) {
674
675 if (list_empty(&tq->tq_pend_list) &&
676 list_empty(&tq->tq_prio_list)) {
677 add_wait_queue_exclusive(&tq->tq_work_waitq, &wait);
678 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
679 schedule();
680 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
681 remove_wait_queue(&tq->tq_work_waitq, &wait);
682 } else {
683 __set_current_state(TASK_RUNNING);
684 }
685
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) {
695 t = list_entry(pend_list->next,taskq_ent_t,tqent_list);
696 list_del_init(&t->tqent_list);
697
698 /* In order to support recursively dispatching a
699 * preallocated taskq_ent_t, tqent_id must be
700 * stored prior to executing tqent_func. */
701 tqt->tqt_id = t->tqent_id;
702 tqt->tqt_task = t;
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
711 taskq_insert_in_order(tq, tqt);
712 tq->tq_nactive++;
713 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
714
715 /* Perform the requested task */
716 t->tqent_func(t->tqent_arg);
717
718 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
719 tq->tq_nactive--;
720 list_del_init(&tqt->tqt_active_list);
721 tqt->tqt_task = NULL;
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);
727
728 /* When the current lowest outstanding taskqid is
729 * done calculate the new lowest outstanding id */
730 if (tq->tq_lowest_id == tqt->tqt_id) {
731 tq->tq_lowest_id = taskq_lowest_id(tq);
732 ASSERT3S(tq->tq_lowest_id, >, tqt->tqt_id);
733 }
734
735 tqt->tqt_id = 0;
736 tqt->tqt_flags = 0;
737 wake_up_all(&tq->tq_wait_waitq);
738 }
739
740 set_current_state(TASK_INTERRUPTIBLE);
741
742 }
743
744 __set_current_state(TASK_RUNNING);
745 tq->tq_nthreads--;
746 list_del_init(&tqt->tqt_thread_list);
747 kmem_free(tqt, sizeof(taskq_thread_t));
748
749 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
750
751 return (0);
752 }
753
754 taskq_t *
755 taskq_create(const char *name, int nthreads, pri_t pri,
756 int minalloc, int maxalloc, uint_t flags)
757 {
758 static int last_used_cpu = 0;
759 taskq_t *tq;
760 taskq_thread_t *tqt;
761 int rc = 0, i, j = 0;
762
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 */
768
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
778 tq = kmem_alloc(sizeof(*tq), KM_PUSHPAGE);
779 if (tq == NULL)
780 return (NULL);
781
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;
788 tq->tq_nthreads = 0;
789 tq->tq_pri = pri;
790 tq->tq_minalloc = minalloc;
791 tq->tq_maxalloc = maxalloc;
792 tq->tq_nalloc = 0;
793 tq->tq_flags = (flags | TQ_ACTIVE);
794 tq->tq_next_id = 1;
795 tq->tq_lowest_id = 1;
796 INIT_LIST_HEAD(&tq->tq_free_list);
797 INIT_LIST_HEAD(&tq->tq_pend_list);
798 INIT_LIST_HEAD(&tq->tq_prio_list);
799 INIT_LIST_HEAD(&tq->tq_delay_list);
800 init_waitqueue_head(&tq->tq_work_waitq);
801 init_waitqueue_head(&tq->tq_wait_waitq);
802
803 if (flags & TASKQ_PREPOPULATE)
804 for (i = 0; i < minalloc; i++)
805 task_done(tq, task_alloc(tq, TQ_PUSHPAGE | TQ_NEW));
806
807 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
808
809 for (i = 0; i < nthreads; i++) {
810 tqt = kmem_alloc(sizeof(*tqt), KM_PUSHPAGE);
811 INIT_LIST_HEAD(&tqt->tqt_thread_list);
812 INIT_LIST_HEAD(&tqt->tqt_active_list);
813 tqt->tqt_tq = tq;
814 tqt->tqt_id = 0;
815
816 tqt->tqt_thread = spl_kthread_create(taskq_thread, tqt,
817 "%s/%d", name, i);
818 if (tqt->tqt_thread) {
819 list_add(&tqt->tqt_thread_list, &tq->tq_thread_list);
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 }
824 set_user_nice(tqt->tqt_thread, PRIO_TO_NICE(pri));
825 wake_up_process(tqt->tqt_thread);
826 j++;
827 } else {
828 kmem_free(tqt, sizeof(taskq_thread_t));
829 rc = 1;
830 }
831 }
832
833 /* Wait for all threads to be started before potential destroy */
834 wait_event(tq->tq_wait_waitq, tq->tq_nthreads == j);
835
836 if (rc) {
837 taskq_destroy(tq);
838 tq = NULL;
839 }
840
841 return (tq);
842 }
843 EXPORT_SYMBOL(taskq_create);
844
845 void
846 taskq_destroy(taskq_t *tq)
847 {
848 struct task_struct *thread;
849 taskq_thread_t *tqt;
850 taskq_ent_t *t;
851
852 ASSERT(tq);
853 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
854 tq->tq_flags &= ~TQ_ACTIVE;
855 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
856
857 /* TQ_ACTIVE cleared prevents new tasks being added to pending */
858 taskq_wait(tq);
859
860 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
861
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
876 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
877 }
878
879 while (!list_empty(&tq->tq_free_list)) {
880 t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
881
882 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
883
884 list_del_init(&t->tqent_list);
885 task_free(tq, t);
886 }
887
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));
895 ASSERT(list_empty(&tq->tq_delay_list));
896
897 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
898
899 kmem_free(tq, sizeof(taskq_t));
900 }
901 EXPORT_SYMBOL(taskq_destroy);
902
903 int
904 spl_taskq_init(void)
905 {
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 */
908 system_taskq = taskq_create("spl_system_taskq", num_online_cpus(),
909 minclsyspri, 4, 512, TASKQ_PREPOPULATE);
910 if (system_taskq == NULL)
911 return (1);
912
913 return (0);
914 }
915
916 void
917 spl_taskq_fini(void)
918 {
919 taskq_destroy(system_taskq);
920 }