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