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