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