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