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