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