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