]>
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 BB |
6 | * UCRL-CODE-235197 |
7 | * | |
716154c5 | 8 | * This file is part of the SPL, Solaris Porting Layer. |
715f6251 | 9 | * |
716154c5 BB |
10 | * The SPL is free software; you can redistribute it and/or modify it |
11 | * under the terms of the GNU General Public License as published by the | |
12 | * Free Software Foundation; either version 2 of the License, or (at your | |
13 | * option) any later version. | |
14 | * | |
15 | * The SPL is distributed in the hope that it will be useful, but WITHOUT | |
715f6251 BB |
16 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 | * for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License along | |
716154c5 | 21 | * with the SPL. If not, see <http://www.gnu.org/licenses/>. |
2c4332cf | 22 | * |
716154c5 | 23 | * Solaris Porting Layer (SPL) Task Queue Implementation. |
2c4332cf | 24 | */ |
715f6251 | 25 | |
8b8b44d0 | 26 | #include <sys/timer.h> |
f4b37741 | 27 | #include <sys/taskq.h> |
3d061e9d | 28 | #include <sys/kmem.h> |
16522ac2 | 29 | #include <sys/tsd.h> |
ae38e009 | 30 | #include <sys/trace_spl.h> |
937879f1 | 31 | |
703371d8 AV |
32 | int spl_taskq_thread_bind = 0; |
33 | module_param(spl_taskq_thread_bind, int, 0644); | |
34 | MODULE_PARM_DESC(spl_taskq_thread_bind, "Bind taskq thread to CPU by default"); | |
35 | ||
f7a973d9 BB |
36 | |
37 | int spl_taskq_thread_dynamic = 1; | |
38 | module_param(spl_taskq_thread_dynamic, int, 0644); | |
39 | MODULE_PARM_DESC(spl_taskq_thread_dynamic, "Allow dynamic taskq threads"); | |
40 | ||
62aa81a5 BB |
41 | int spl_taskq_thread_priority = 1; |
42 | module_param(spl_taskq_thread_priority, int, 0644); | |
43 | MODULE_PARM_DESC(spl_taskq_thread_priority, | |
2c4332cf | 44 | "Allow non-default priority for taskq threads"); |
62aa81a5 | 45 | |
f7a973d9 BB |
46 | int spl_taskq_thread_sequential = 4; |
47 | module_param(spl_taskq_thread_sequential, int, 0644); | |
48 | MODULE_PARM_DESC(spl_taskq_thread_sequential, | |
2c4332cf | 49 | "Create new taskq threads after N sequential tasks"); |
f7a973d9 | 50 | |
e9cb2b4f BB |
51 | /* Global system-wide dynamic task queue available for all consumers */ |
52 | taskq_t *system_taskq; | |
53 | EXPORT_SYMBOL(system_taskq); | |
f200b836 CC |
54 | /* Global dynamic task queue for long delay */ |
55 | taskq_t *system_delay_taskq; | |
56 | EXPORT_SYMBOL(system_delay_taskq); | |
e9cb2b4f | 57 | |
f7a973d9 BB |
58 | /* Private dedicated taskq for creating new taskq threads on demand. */ |
59 | static taskq_t *dynamic_taskq; | |
60 | static taskq_thread_t *taskq_thread_create(taskq_t *); | |
61 | ||
200366f2 TC |
62 | /* List of all taskqs */ |
63 | LIST_HEAD(tq_list); | |
93ce2b4c | 64 | struct rw_semaphore tq_list_sem; |
16522ac2 | 65 | static uint_t taskq_tsd; |
200366f2 | 66 | |
9b51f218 BB |
67 | static int |
68 | task_km_flags(uint_t flags) | |
69 | { | |
70 | if (flags & TQ_NOSLEEP) | |
2c4332cf | 71 | return (KM_NOSLEEP); |
9b51f218 BB |
72 | |
73 | if (flags & TQ_PUSHPAGE) | |
2c4332cf | 74 | return (KM_PUSHPAGE); |
9b51f218 | 75 | |
2c4332cf | 76 | return (KM_SLEEP); |
9b51f218 BB |
77 | } |
78 | ||
200366f2 TC |
79 | /* |
80 | * taskq_find_by_name - Find the largest instance number of a named taskq. | |
81 | */ | |
82 | static int | |
83 | taskq_find_by_name(const char *name) | |
84 | { | |
7cf1fe63 | 85 | struct list_head *tql = NULL; |
200366f2 TC |
86 | taskq_t *tq; |
87 | ||
88 | list_for_each_prev(tql, &tq_list) { | |
89 | tq = list_entry(tql, taskq_t, tq_taskqs); | |
90 | if (strcmp(name, tq->tq_name) == 0) | |
5461eefe | 91 | return (tq->tq_instance); |
200366f2 TC |
92 | } |
93 | return (-1); | |
94 | } | |
95 | ||
82387586 BB |
96 | /* |
97 | * NOTE: Must be called with tq->tq_lock held, returns a list_t which | |
bcd68186 | 98 | * is not attached to the free, work, or pending taskq lists. |
f1ca4da6 | 99 | */ |
046a70c9 | 100 | static taskq_ent_t * |
066b89e6 | 101 | task_alloc(taskq_t *tq, uint_t flags, unsigned long *irqflags) |
bcd68186 | 102 | { |
472a34ca BB |
103 | taskq_ent_t *t; |
104 | int count = 0; | |
bcd68186 | 105 | |
472a34ca | 106 | ASSERT(tq); |
bcd68186 | 107 | retry: |
472a34ca BB |
108 | /* Acquire taskq_ent_t's from free list if available */ |
109 | if (!list_empty(&tq->tq_free_list) && !(flags & TQ_NEW)) { | |
110 | t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list); | |
111 | ||
112 | ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC)); | |
d9acd930 BB |
113 | ASSERT(!(t->tqent_flags & TQENT_FLAG_CANCEL)); |
114 | ASSERT(!timer_pending(&t->tqent_timer)); | |
472a34ca BB |
115 | |
116 | list_del_init(&t->tqent_list); | |
8d9a23e8 | 117 | return (t); |
472a34ca BB |
118 | } |
119 | ||
120 | /* Free list is empty and memory allocations are prohibited */ | |
121 | if (flags & TQ_NOALLOC) | |
8d9a23e8 | 122 | return (NULL); |
472a34ca BB |
123 | |
124 | /* Hit maximum taskq_ent_t pool size */ | |
125 | if (tq->tq_nalloc >= tq->tq_maxalloc) { | |
126 | if (flags & TQ_NOSLEEP) | |
8d9a23e8 | 127 | return (NULL); |
472a34ca BB |
128 | |
129 | /* | |
130 | * Sleep periodically polling the free list for an available | |
131 | * taskq_ent_t. Dispatching with TQ_SLEEP should always succeed | |
132 | * but we cannot block forever waiting for an taskq_ent_t to | |
133 | * show up in the free list, otherwise a deadlock can happen. | |
134 | * | |
135 | * Therefore, we need to allocate a new task even if the number | |
136 | * of allocated tasks is above tq->tq_maxalloc, but we still | |
137 | * end up delaying the task allocation by one second, thereby | |
138 | * throttling the task dispatch rate. | |
139 | */ | |
066b89e6 | 140 | spin_unlock_irqrestore(&tq->tq_lock, *irqflags); |
472a34ca | 141 | schedule_timeout(HZ / 100); |
066b89e6 | 142 | spin_lock_irqsave_nested(&tq->tq_lock, *irqflags, |
326172d8 | 143 | tq->tq_lock_class); |
8d9a23e8 BB |
144 | if (count < 100) { |
145 | count++; | |
146 | goto retry; | |
147 | } | |
472a34ca BB |
148 | } |
149 | ||
066b89e6 | 150 | spin_unlock_irqrestore(&tq->tq_lock, *irqflags); |
2c4332cf | 151 | t = kmem_alloc(sizeof (taskq_ent_t), task_km_flags(flags)); |
066b89e6 | 152 | spin_lock_irqsave_nested(&tq->tq_lock, *irqflags, tq->tq_lock_class); |
472a34ca BB |
153 | |
154 | if (t) { | |
155 | taskq_init_ent(t); | |
156 | tq->tq_nalloc++; | |
157 | } | |
158 | ||
8d9a23e8 | 159 | return (t); |
bcd68186 BB |
160 | } |
161 | ||
82387586 | 162 | /* |
046a70c9 | 163 | * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t |
bcd68186 BB |
164 | * to already be removed from the free, work, or pending taskq lists. |
165 | */ | |
166 | static void | |
046a70c9 | 167 | task_free(taskq_t *tq, taskq_ent_t *t) |
bcd68186 | 168 | { |
472a34ca BB |
169 | ASSERT(tq); |
170 | ASSERT(t); | |
046a70c9 | 171 | ASSERT(list_empty(&t->tqent_list)); |
d9acd930 | 172 | ASSERT(!timer_pending(&t->tqent_timer)); |
bcd68186 | 173 | |
2c4332cf | 174 | kmem_free(t, sizeof (taskq_ent_t)); |
472a34ca | 175 | tq->tq_nalloc--; |
bcd68186 BB |
176 | } |
177 | ||
82387586 BB |
178 | /* |
179 | * NOTE: Must be called with tq->tq_lock held, either destroys the | |
046a70c9 | 180 | * taskq_ent_t if too many exist or moves it to the free list for later use. |
bcd68186 | 181 | */ |
f1ca4da6 | 182 | static void |
046a70c9 | 183 | task_done(taskq_t *tq, taskq_ent_t *t) |
f1ca4da6 | 184 | { |
bcd68186 BB |
185 | ASSERT(tq); |
186 | ASSERT(t); | |
bcd68186 | 187 | |
d9acd930 BB |
188 | /* Wake tasks blocked in taskq_wait_id() */ |
189 | wake_up_all(&t->tqent_waitq); | |
190 | ||
046a70c9 | 191 | list_del_init(&t->tqent_list); |
f1ca4da6 | 192 | |
472a34ca | 193 | if (tq->tq_nalloc <= tq->tq_minalloc) { |
cbba7146 | 194 | t->tqent_id = TASKQID_INVALID; |
046a70c9 PS |
195 | t->tqent_func = NULL; |
196 | t->tqent_arg = NULL; | |
44217f7a | 197 | t->tqent_flags = 0; |
8f2503e0 | 198 | |
472a34ca | 199 | list_add_tail(&t->tqent_list, &tq->tq_free_list); |
bcd68186 BB |
200 | } else { |
201 | task_free(tq, t); | |
202 | } | |
f1ca4da6 BB |
203 | } |
204 | ||
82387586 | 205 | /* |
d9acd930 BB |
206 | * When a delayed task timer expires remove it from the delay list and |
207 | * add it to the priority list in order for immediate processing. | |
bcd68186 | 208 | */ |
d9acd930 | 209 | static void |
c9821f1c | 210 | task_expire_impl(taskq_ent_t *t) |
bcd68186 | 211 | { |
c9821f1c | 212 | taskq_ent_t *w; |
d9acd930 | 213 | taskq_t *tq = t->tqent_taskq; |
7cf1fe63 | 214 | struct list_head *l = NULL; |
066b89e6 | 215 | unsigned long flags; |
7257ec41 | 216 | |
066b89e6 | 217 | spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); |
d9acd930 BB |
218 | |
219 | if (t->tqent_flags & TQENT_FLAG_CANCEL) { | |
220 | ASSERT(list_empty(&t->tqent_list)); | |
066b89e6 | 221 | spin_unlock_irqrestore(&tq->tq_lock, flags); |
d9acd930 BB |
222 | return; |
223 | } | |
224 | ||
8f3b403a | 225 | t->tqent_birth = jiffies; |
ae38e009 PS |
226 | DTRACE_PROBE1(taskq_ent__birth, taskq_ent_t *, t); |
227 | ||
d9acd930 BB |
228 | /* |
229 | * The priority list must be maintained in strict task id order | |
230 | * from lowest to highest for lowest_id to be easily calculable. | |
231 | */ | |
232 | list_del(&t->tqent_list); | |
233 | list_for_each_prev(l, &tq->tq_prio_list) { | |
234 | w = list_entry(l, taskq_ent_t, tqent_list); | |
235 | if (w->tqent_id < t->tqent_id) { | |
236 | list_add(&t->tqent_list, l); | |
237 | break; | |
238 | } | |
239 | } | |
240 | if (l == &tq->tq_prio_list) | |
241 | list_add(&t->tqent_list, &tq->tq_prio_list); | |
242 | ||
066b89e6 | 243 | spin_unlock_irqrestore(&tq->tq_lock, flags); |
7257ec41 | 244 | |
d9acd930 BB |
245 | wake_up(&tq->tq_work_waitq); |
246 | } | |
247 | ||
c9821f1c | 248 | static void |
8b8b44d0 | 249 | task_expire(spl_timer_list_t tl) |
c9821f1c | 250 | { |
8b8b44d0 RK |
251 | struct timer_list *tmr = (struct timer_list *)tl; |
252 | taskq_ent_t *t = from_timer(t, tmr, tqent_timer); | |
c9821f1c TH |
253 | task_expire_impl(t); |
254 | } | |
c9821f1c | 255 | |
d9acd930 BB |
256 | /* |
257 | * Returns the lowest incomplete taskqid_t. The taskqid_t may | |
258 | * be queued on the pending list, on the priority list, on the | |
259 | * delay list, or on the work list currently being handled, but | |
260 | * it is not 100% complete yet. | |
261 | */ | |
262 | static taskqid_t | |
263 | taskq_lowest_id(taskq_t *tq) | |
264 | { | |
265 | taskqid_t lowest_id = tq->tq_next_id; | |
266 | taskq_ent_t *t; | |
267 | taskq_thread_t *tqt; | |
d9acd930 BB |
268 | |
269 | ASSERT(tq); | |
d9acd930 BB |
270 | |
271 | if (!list_empty(&tq->tq_pend_list)) { | |
272 | t = list_entry(tq->tq_pend_list.next, taskq_ent_t, tqent_list); | |
273 | lowest_id = MIN(lowest_id, t->tqent_id); | |
274 | } | |
275 | ||
276 | if (!list_empty(&tq->tq_prio_list)) { | |
277 | t = list_entry(tq->tq_prio_list.next, taskq_ent_t, tqent_list); | |
278 | lowest_id = MIN(lowest_id, t->tqent_id); | |
279 | } | |
280 | ||
281 | if (!list_empty(&tq->tq_delay_list)) { | |
282 | t = list_entry(tq->tq_delay_list.next, taskq_ent_t, tqent_list); | |
283 | lowest_id = MIN(lowest_id, t->tqent_id); | |
284 | } | |
285 | ||
286 | if (!list_empty(&tq->tq_active_list)) { | |
287 | tqt = list_entry(tq->tq_active_list.next, taskq_thread_t, | |
288 | tqt_active_list); | |
cbba7146 | 289 | ASSERT(tqt->tqt_id != TASKQID_INVALID); |
d9acd930 BB |
290 | lowest_id = MIN(lowest_id, tqt->tqt_id); |
291 | } | |
292 | ||
8d9a23e8 | 293 | return (lowest_id); |
d9acd930 BB |
294 | } |
295 | ||
296 | /* | |
297 | * Insert a task into a list keeping the list sorted by increasing taskqid. | |
298 | */ | |
299 | static void | |
300 | taskq_insert_in_order(taskq_t *tq, taskq_thread_t *tqt) | |
301 | { | |
302 | taskq_thread_t *w; | |
7cf1fe63 | 303 | struct list_head *l = NULL; |
d9acd930 | 304 | |
d9acd930 BB |
305 | ASSERT(tq); |
306 | ASSERT(tqt); | |
d9acd930 BB |
307 | |
308 | list_for_each_prev(l, &tq->tq_active_list) { | |
309 | w = list_entry(l, taskq_thread_t, tqt_active_list); | |
310 | if (w->tqt_id < tqt->tqt_id) { | |
311 | list_add(&tqt->tqt_active_list, l); | |
312 | break; | |
313 | } | |
314 | } | |
315 | if (l == &tq->tq_active_list) | |
316 | list_add(&tqt->tqt_active_list, &tq->tq_active_list); | |
d9acd930 BB |
317 | } |
318 | ||
319 | /* | |
320 | * Find and return a task from the given list if it exists. The list | |
321 | * must be in lowest to highest task id order. | |
322 | */ | |
323 | static taskq_ent_t * | |
324 | taskq_find_list(taskq_t *tq, struct list_head *lh, taskqid_t id) | |
325 | { | |
7cf1fe63 | 326 | struct list_head *l = NULL; |
d9acd930 | 327 | taskq_ent_t *t; |
d9acd930 | 328 | |
d9acd930 BB |
329 | list_for_each(l, lh) { |
330 | t = list_entry(l, taskq_ent_t, tqent_list); | |
331 | ||
332 | if (t->tqent_id == id) | |
8d9a23e8 | 333 | return (t); |
d9acd930 BB |
334 | |
335 | if (t->tqent_id > id) | |
336 | break; | |
337 | } | |
338 | ||
8d9a23e8 | 339 | return (NULL); |
bcd68186 BB |
340 | } |
341 | ||
d9acd930 BB |
342 | /* |
343 | * Find an already dispatched task given the task id regardless of what | |
cce83ba0 CC |
344 | * state it is in. If a task is still pending it will be returned. |
345 | * If a task is executing, then -EBUSY will be returned instead. | |
346 | * If the task has already been run then NULL is returned. | |
d9acd930 BB |
347 | */ |
348 | static taskq_ent_t * | |
cce83ba0 | 349 | taskq_find(taskq_t *tq, taskqid_t id) |
d9acd930 BB |
350 | { |
351 | taskq_thread_t *tqt; | |
7cf1fe63 | 352 | struct list_head *l = NULL; |
d9acd930 | 353 | taskq_ent_t *t; |
d9acd930 | 354 | |
d9acd930 BB |
355 | t = taskq_find_list(tq, &tq->tq_delay_list, id); |
356 | if (t) | |
8d9a23e8 | 357 | return (t); |
d9acd930 BB |
358 | |
359 | t = taskq_find_list(tq, &tq->tq_prio_list, id); | |
360 | if (t) | |
8d9a23e8 | 361 | return (t); |
d9acd930 BB |
362 | |
363 | t = taskq_find_list(tq, &tq->tq_pend_list, id); | |
364 | if (t) | |
8d9a23e8 | 365 | return (t); |
d9acd930 BB |
366 | |
367 | list_for_each(l, &tq->tq_active_list) { | |
368 | tqt = list_entry(l, taskq_thread_t, tqt_active_list); | |
369 | if (tqt->tqt_id == id) { | |
cce83ba0 CC |
370 | /* |
371 | * Instead of returning tqt_task, we just return a non | |
372 | * NULL value to prevent misuse, since tqt_task only | |
373 | * has two valid fields. | |
374 | */ | |
375 | return (ERR_PTR(-EBUSY)); | |
d9acd930 BB |
376 | } |
377 | } | |
378 | ||
8d9a23e8 | 379 | return (NULL); |
d9acd930 BB |
380 | } |
381 | ||
a876b030 CD |
382 | /* |
383 | * Theory for the taskq_wait_id(), taskq_wait_outstanding(), and | |
384 | * taskq_wait() functions below. | |
385 | * | |
386 | * Taskq waiting is accomplished by tracking the lowest outstanding task | |
387 | * id and the next available task id. As tasks are dispatched they are | |
388 | * added to the tail of the pending, priority, or delay lists. As worker | |
389 | * threads become available the tasks are removed from the heads of these | |
390 | * lists and linked to the worker threads. This ensures the lists are | |
391 | * kept sorted by lowest to highest task id. | |
392 | * | |
393 | * Therefore the lowest outstanding task id can be quickly determined by | |
394 | * checking the head item from all of these lists. This value is stored | |
395 | * with the taskq as the lowest id. It only needs to be recalculated when | |
396 | * either the task with the current lowest id completes or is canceled. | |
397 | * | |
398 | * By blocking until the lowest task id exceeds the passed task id the | |
399 | * taskq_wait_outstanding() function can be easily implemented. Similarly, | |
400 | * by blocking until the lowest task id matches the next task id taskq_wait() | |
401 | * can be implemented. | |
402 | * | |
403 | * Callers should be aware that when there are multiple worked threads it | |
404 | * is possible for larger task ids to complete before smaller ones. Also | |
405 | * when the taskq contains delay tasks with small task ids callers may | |
406 | * block for a considerable length of time waiting for them to expire and | |
407 | * execute. | |
408 | */ | |
99c452bb BB |
409 | static int |
410 | taskq_wait_id_check(taskq_t *tq, taskqid_t id) | |
f1ca4da6 | 411 | { |
99c452bb | 412 | int rc; |
066b89e6 | 413 | unsigned long flags; |
bcd68186 | 414 | |
066b89e6 | 415 | spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); |
cce83ba0 | 416 | rc = (taskq_find(tq, id) == NULL); |
066b89e6 | 417 | spin_unlock_irqrestore(&tq->tq_lock, flags); |
d9acd930 | 418 | |
99c452bb BB |
419 | return (rc); |
420 | } | |
bcd68186 | 421 | |
99c452bb BB |
422 | /* |
423 | * The taskq_wait_id() function blocks until the passed task id completes. | |
424 | * This does not guarantee that all lower task ids have completed. | |
425 | */ | |
426 | void | |
427 | taskq_wait_id(taskq_t *tq, taskqid_t id) | |
428 | { | |
429 | wait_event(tq->tq_wait_waitq, taskq_wait_id_check(tq, id)); | |
bcd68186 | 430 | } |
aed8671c | 431 | EXPORT_SYMBOL(taskq_wait_id); |
bcd68186 | 432 | |
d9acd930 | 433 | static int |
a876b030 | 434 | taskq_wait_outstanding_check(taskq_t *tq, taskqid_t id) |
d9acd930 BB |
435 | { |
436 | int rc; | |
066b89e6 | 437 | unsigned long flags; |
d9acd930 | 438 | |
066b89e6 | 439 | spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); |
d9acd930 | 440 | rc = (id < tq->tq_lowest_id); |
066b89e6 | 441 | spin_unlock_irqrestore(&tq->tq_lock, flags); |
d9acd930 | 442 | |
8d9a23e8 | 443 | return (rc); |
d9acd930 BB |
444 | } |
445 | ||
a876b030 CD |
446 | /* |
447 | * The taskq_wait_outstanding() function will block until all tasks with a | |
448 | * lower taskqid than the passed 'id' have been completed. Note that all | |
449 | * task id's are assigned monotonically at dispatch time. Zero may be | |
450 | * passed for the id to indicate all tasks dispatch up to this point, | |
451 | * but not after, should be waited for. | |
452 | */ | |
d9acd930 | 453 | void |
a876b030 | 454 | taskq_wait_outstanding(taskq_t *tq, taskqid_t id) |
d9acd930 | 455 | { |
b3a22a0a CC |
456 | id = id ? id : tq->tq_next_id - 1; |
457 | wait_event(tq->tq_wait_waitq, taskq_wait_outstanding_check(tq, id)); | |
d9acd930 | 458 | } |
a876b030 | 459 | EXPORT_SYMBOL(taskq_wait_outstanding); |
d9acd930 | 460 | |
a876b030 CD |
461 | static int |
462 | taskq_wait_check(taskq_t *tq) | |
bcd68186 | 463 | { |
a876b030 | 464 | int rc; |
066b89e6 | 465 | unsigned long flags; |
bcd68186 | 466 | |
066b89e6 | 467 | spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); |
a876b030 | 468 | rc = (tq->tq_lowest_id == tq->tq_next_id); |
066b89e6 | 469 | spin_unlock_irqrestore(&tq->tq_lock, flags); |
bcd68186 | 470 | |
a876b030 CD |
471 | return (rc); |
472 | } | |
473 | ||
474 | /* | |
475 | * The taskq_wait() function will block until the taskq is empty. | |
476 | * This means that if a taskq re-dispatches work to itself taskq_wait() | |
477 | * callers will block indefinitely. | |
478 | */ | |
479 | void | |
480 | taskq_wait(taskq_t *tq) | |
481 | { | |
482 | wait_event(tq->tq_wait_waitq, taskq_wait_check(tq)); | |
bcd68186 | 483 | } |
aed8671c | 484 | EXPORT_SYMBOL(taskq_wait); |
bcd68186 | 485 | |
c5a8b1e1 | 486 | int |
16522ac2 | 487 | taskq_member(taskq_t *tq, kthread_t *t) |
c5a8b1e1 | 488 | { |
16522ac2 | 489 | return (tq == (taskq_t *)tsd_get_by_thread(taskq_tsd, t)); |
c5a8b1e1 BB |
490 | } |
491 | EXPORT_SYMBOL(taskq_member); | |
492 | ||
b3212d2f MA |
493 | taskq_t * |
494 | taskq_of_curthread(void) | |
495 | { | |
496 | return (tsd_get(taskq_tsd)); | |
497 | } | |
498 | EXPORT_SYMBOL(taskq_of_curthread); | |
499 | ||
d9acd930 BB |
500 | /* |
501 | * Cancel an already dispatched task given the task id. Still pending tasks | |
502 | * will be immediately canceled, and if the task is active the function will | |
503 | * block until it completes. Preallocated tasks which are canceled must be | |
504 | * freed by the caller. | |
505 | */ | |
506 | int | |
507 | taskq_cancel_id(taskq_t *tq, taskqid_t id) | |
508 | { | |
509 | taskq_ent_t *t; | |
d9acd930 | 510 | int rc = ENOENT; |
066b89e6 | 511 | unsigned long flags; |
d9acd930 BB |
512 | |
513 | ASSERT(tq); | |
514 | ||
066b89e6 | 515 | spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); |
cce83ba0 CC |
516 | t = taskq_find(tq, id); |
517 | if (t && t != ERR_PTR(-EBUSY)) { | |
d9acd930 BB |
518 | list_del_init(&t->tqent_list); |
519 | t->tqent_flags |= TQENT_FLAG_CANCEL; | |
520 | ||
521 | /* | |
522 | * When canceling the lowest outstanding task id we | |
523 | * must recalculate the new lowest outstanding id. | |
524 | */ | |
525 | if (tq->tq_lowest_id == t->tqent_id) { | |
526 | tq->tq_lowest_id = taskq_lowest_id(tq); | |
527 | ASSERT3S(tq->tq_lowest_id, >, t->tqent_id); | |
528 | } | |
529 | ||
530 | /* | |
531 | * The task_expire() function takes the tq->tq_lock so drop | |
532 | * drop the lock before synchronously cancelling the timer. | |
533 | */ | |
534 | if (timer_pending(&t->tqent_timer)) { | |
066b89e6 | 535 | spin_unlock_irqrestore(&tq->tq_lock, flags); |
d9acd930 | 536 | del_timer_sync(&t->tqent_timer); |
066b89e6 CC |
537 | spin_lock_irqsave_nested(&tq->tq_lock, flags, |
538 | tq->tq_lock_class); | |
d9acd930 BB |
539 | } |
540 | ||
541 | if (!(t->tqent_flags & TQENT_FLAG_PREALLOC)) | |
542 | task_done(tq, t); | |
543 | ||
544 | rc = 0; | |
545 | } | |
066b89e6 | 546 | spin_unlock_irqrestore(&tq->tq_lock, flags); |
d9acd930 | 547 | |
cce83ba0 | 548 | if (t == ERR_PTR(-EBUSY)) { |
d9acd930 BB |
549 | taskq_wait_id(tq, id); |
550 | rc = EBUSY; | |
551 | } | |
552 | ||
8d9a23e8 | 553 | return (rc); |
d9acd930 BB |
554 | } |
555 | EXPORT_SYMBOL(taskq_cancel_id); | |
556 | ||
f5f2b87d | 557 | static int taskq_thread_spawn(taskq_t *tq); |
a64e5575 | 558 | |
bcd68186 | 559 | taskqid_t |
aed8671c | 560 | taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags) |
bcd68186 | 561 | { |
472a34ca | 562 | taskq_ent_t *t; |
cbba7146 | 563 | taskqid_t rc = TASKQID_INVALID; |
066b89e6 | 564 | unsigned long irqflags; |
f1ca4da6 | 565 | |
472a34ca BB |
566 | ASSERT(tq); |
567 | ASSERT(func); | |
d05ec4b4 | 568 | |
066b89e6 | 569 | spin_lock_irqsave_nested(&tq->tq_lock, irqflags, tq->tq_lock_class); |
f1ca4da6 | 570 | |
bcd68186 | 571 | /* Taskq being destroyed and all tasks drained */ |
f7a973d9 | 572 | if (!(tq->tq_flags & TASKQ_ACTIVE)) |
8d9a23e8 | 573 | goto out; |
f1ca4da6 | 574 | |
bcd68186 BB |
575 | /* Do not queue the task unless there is idle thread for it */ |
576 | ASSERT(tq->tq_nactive <= tq->tq_nthreads); | |
7bb5d92d TC |
577 | if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads)) { |
578 | /* Dynamic taskq may be able to spawn another thread */ | |
5461eefe BB |
579 | if (!(tq->tq_flags & TASKQ_DYNAMIC) || |
580 | taskq_thread_spawn(tq) == 0) | |
7bb5d92d TC |
581 | goto out; |
582 | } | |
bcd68186 | 583 | |
066b89e6 | 584 | if ((t = task_alloc(tq, flags, &irqflags)) == NULL) |
8d9a23e8 | 585 | goto out; |
f1ca4da6 | 586 | |
046a70c9 | 587 | spin_lock(&t->tqent_lock); |
f0d8bb26 | 588 | |
7bb5d92d TC |
589 | /* Queue to the front of the list to enforce TQ_NOQUEUE semantics */ |
590 | if (flags & TQ_NOQUEUE) | |
591 | list_add(&t->tqent_list, &tq->tq_prio_list); | |
f0d8bb26 | 592 | /* Queue to the priority list instead of the pending list */ |
7bb5d92d | 593 | else if (flags & TQ_FRONT) |
046a70c9 | 594 | list_add_tail(&t->tqent_list, &tq->tq_prio_list); |
f0d8bb26 | 595 | else |
046a70c9 | 596 | list_add_tail(&t->tqent_list, &tq->tq_pend_list); |
f0d8bb26 | 597 | |
046a70c9 | 598 | t->tqent_id = rc = tq->tq_next_id; |
bcd68186 | 599 | tq->tq_next_id++; |
472a34ca BB |
600 | t->tqent_func = func; |
601 | t->tqent_arg = arg; | |
d9acd930 | 602 | t->tqent_taskq = tq; |
d9acd930 BB |
603 | t->tqent_timer.function = NULL; |
604 | t->tqent_timer.expires = 0; | |
ae38e009 | 605 | |
8f3b403a | 606 | t->tqent_birth = jiffies; |
ae38e009 | 607 | DTRACE_PROBE1(taskq_ent__birth, taskq_ent_t *, t); |
44217f7a PS |
608 | |
609 | ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC)); | |
610 | ||
046a70c9 | 611 | spin_unlock(&t->tqent_lock); |
0bb43ca2 NB |
612 | |
613 | wake_up(&tq->tq_work_waitq); | |
bcd68186 | 614 | out: |
a64e5575 | 615 | /* Spawn additional taskq threads if required. */ |
7bb5d92d | 616 | if (!(flags & TQ_NOQUEUE) && tq->tq_nactive == tq->tq_nthreads) |
f5f2b87d | 617 | (void) taskq_thread_spawn(tq); |
a64e5575 | 618 | |
066b89e6 | 619 | spin_unlock_irqrestore(&tq->tq_lock, irqflags); |
8d9a23e8 | 620 | return (rc); |
f1ca4da6 | 621 | } |
aed8671c | 622 | EXPORT_SYMBOL(taskq_dispatch); |
44217f7a | 623 | |
d9acd930 BB |
624 | taskqid_t |
625 | taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg, | |
626 | uint_t flags, clock_t expire_time) | |
627 | { | |
cbba7146 | 628 | taskqid_t rc = TASKQID_INVALID; |
8d9a23e8 | 629 | taskq_ent_t *t; |
066b89e6 | 630 | unsigned long irqflags; |
d9acd930 BB |
631 | |
632 | ASSERT(tq); | |
633 | ASSERT(func); | |
634 | ||
066b89e6 | 635 | spin_lock_irqsave_nested(&tq->tq_lock, irqflags, tq->tq_lock_class); |
d9acd930 BB |
636 | |
637 | /* Taskq being destroyed and all tasks drained */ | |
f7a973d9 | 638 | if (!(tq->tq_flags & TASKQ_ACTIVE)) |
8d9a23e8 | 639 | goto out; |
d9acd930 | 640 | |
066b89e6 | 641 | if ((t = task_alloc(tq, flags, &irqflags)) == NULL) |
8d9a23e8 | 642 | goto out; |
d9acd930 BB |
643 | |
644 | spin_lock(&t->tqent_lock); | |
645 | ||
646 | /* Queue to the delay list for subsequent execution */ | |
647 | list_add_tail(&t->tqent_list, &tq->tq_delay_list); | |
648 | ||
649 | t->tqent_id = rc = tq->tq_next_id; | |
650 | tq->tq_next_id++; | |
651 | t->tqent_func = func; | |
652 | t->tqent_arg = arg; | |
653 | t->tqent_taskq = tq; | |
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 */ | |
5461eefe BB |
689 | if (!(tq->tq_flags & TASKQ_DYNAMIC) || |
690 | taskq_thread_spawn(tq) == 0) | |
7bb5d92d TC |
691 | goto out2; |
692 | flags |= TQ_FRONT; | |
693 | } | |
694 | ||
44217f7a PS |
695 | spin_lock(&t->tqent_lock); |
696 | ||
9243b0fb BP |
697 | /* |
698 | * Make sure the entry is not on some other taskq; it is important to | |
699 | * ASSERT() under lock | |
700 | */ | |
701 | ASSERT(taskq_empty_ent(t)); | |
702 | ||
44217f7a PS |
703 | /* |
704 | * Mark it as a prealloc'd task. This is important | |
705 | * to ensure that we don't free it later. | |
706 | */ | |
707 | t->tqent_flags |= TQENT_FLAG_PREALLOC; | |
708 | ||
709 | /* Queue to the priority list instead of the pending list */ | |
710 | if (flags & TQ_FRONT) | |
711 | list_add_tail(&t->tqent_list, &tq->tq_prio_list); | |
712 | else | |
713 | list_add_tail(&t->tqent_list, &tq->tq_pend_list); | |
714 | ||
715 | t->tqent_id = tq->tq_next_id; | |
716 | tq->tq_next_id++; | |
717 | t->tqent_func = func; | |
718 | t->tqent_arg = arg; | |
d9acd930 | 719 | t->tqent_taskq = tq; |
ae38e009 | 720 | |
8f3b403a | 721 | t->tqent_birth = jiffies; |
ae38e009 | 722 | DTRACE_PROBE1(taskq_ent__birth, taskq_ent_t *, t); |
44217f7a PS |
723 | |
724 | spin_unlock(&t->tqent_lock); | |
725 | ||
726 | wake_up(&tq->tq_work_waitq); | |
727 | out: | |
a64e5575 | 728 | /* Spawn additional taskq threads if required. */ |
f5f2b87d | 729 | if (tq->tq_nactive == tq->tq_nthreads) |
730 | (void) taskq_thread_spawn(tq); | |
7bb5d92d | 731 | out2: |
066b89e6 | 732 | spin_unlock_irqrestore(&tq->tq_lock, irqflags); |
44217f7a | 733 | } |
aed8671c | 734 | EXPORT_SYMBOL(taskq_dispatch_ent); |
44217f7a PS |
735 | |
736 | int | |
aed8671c | 737 | taskq_empty_ent(taskq_ent_t *t) |
44217f7a | 738 | { |
2c4332cf | 739 | return (list_empty(&t->tqent_list)); |
44217f7a | 740 | } |
aed8671c | 741 | EXPORT_SYMBOL(taskq_empty_ent); |
44217f7a PS |
742 | |
743 | void | |
aed8671c | 744 | taskq_init_ent(taskq_ent_t *t) |
44217f7a PS |
745 | { |
746 | spin_lock_init(&t->tqent_lock); | |
d9acd930 | 747 | init_waitqueue_head(&t->tqent_waitq); |
c9821f1c | 748 | timer_setup(&t->tqent_timer, NULL, 0); |
44217f7a PS |
749 | INIT_LIST_HEAD(&t->tqent_list); |
750 | t->tqent_id = 0; | |
751 | t->tqent_func = NULL; | |
752 | t->tqent_arg = NULL; | |
753 | t->tqent_flags = 0; | |
d9acd930 | 754 | t->tqent_taskq = NULL; |
44217f7a | 755 | } |
aed8671c | 756 | EXPORT_SYMBOL(taskq_init_ent); |
44217f7a | 757 | |
f7a973d9 BB |
758 | /* |
759 | * Return the next pending task, preference is given to tasks on the | |
760 | * priority list which were dispatched with TQ_FRONT. | |
761 | */ | |
762 | static taskq_ent_t * | |
763 | taskq_next_ent(taskq_t *tq) | |
764 | { | |
765 | struct list_head *list; | |
766 | ||
f7a973d9 BB |
767 | if (!list_empty(&tq->tq_prio_list)) |
768 | list = &tq->tq_prio_list; | |
769 | else if (!list_empty(&tq->tq_pend_list)) | |
770 | list = &tq->tq_pend_list; | |
771 | else | |
772 | return (NULL); | |
773 | ||
774 | return (list_entry(list->next, taskq_ent_t, tqent_list)); | |
775 | } | |
776 | ||
777 | /* | |
778 | * Spawns a new thread for the specified taskq. | |
779 | */ | |
780 | static void | |
781 | taskq_thread_spawn_task(void *arg) | |
782 | { | |
783 | taskq_t *tq = (taskq_t *)arg; | |
066b89e6 | 784 | unsigned long flags; |
f7a973d9 | 785 | |
5ce028b0 CC |
786 | if (taskq_thread_create(tq) == NULL) { |
787 | /* restore spawning count if failed */ | |
5461eefe BB |
788 | spin_lock_irqsave_nested(&tq->tq_lock, flags, |
789 | tq->tq_lock_class); | |
5ce028b0 CC |
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 BB |
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 BB |
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 | |
ae38e009 PS |
922 | * assignment in this case; we want to make sure |
923 | * to duplicate all fields, so the values are | |
924 | * correct when it's accessed via DTRACE_PROBE*. | |
2c4332cf | 925 | */ |
e7e5f78e | 926 | tqt->tqt_id = t->tqent_id; |
8f2503e0 PS |
927 | tqt->tqt_flags = t->tqent_flags; |
928 | ||
cce83ba0 | 929 | if (t->tqent_flags & TQENT_FLAG_PREALLOC) { |
ae38e009 | 930 | dup_task = *t; |
cce83ba0 CC |
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 | |
ae38e009 PS |
939 | DTRACE_PROBE1(taskq_ent__start, taskq_ent_t *, t); |
940 | ||
bcd68186 | 941 | /* Perform the requested task */ |
472a34ca | 942 | t->tqent_func(t->tqent_arg); |
bcd68186 | 943 | |
ae38e009 PS |
944 | DTRACE_PROBE1(taskq_ent__finish, taskq_ent_t *, t); |
945 | ||
066b89e6 CC |
946 | spin_lock_irqsave_nested(&tq->tq_lock, flags, |
947 | tq->tq_lock_class); | |
472a34ca | 948 | tq->tq_nactive--; |
2c02b71b | 949 | list_del_init(&tqt->tqt_active_list); |
d9acd930 | 950 | tqt->tqt_task = NULL; |
8f2503e0 PS |
951 | |
952 | /* For prealloc'd tasks, we don't free anything. */ | |
f7a973d9 | 953 | if (!(tqt->tqt_flags & TQENT_FLAG_PREALLOC)) |
8f2503e0 | 954 | task_done(tq, t); |
bcd68186 | 955 | |
2c4332cf BB |
956 | /* |
957 | * When the current lowest outstanding taskqid is | |
958 | * done calculate the new lowest outstanding id | |
959 | */ | |
e7e5f78e | 960 | if (tq->tq_lowest_id == tqt->tqt_id) { |
bcd68186 | 961 | tq->tq_lowest_id = taskq_lowest_id(tq); |
e7e5f78e | 962 | ASSERT3S(tq->tq_lowest_id, >, tqt->tqt_id); |
bcd68186 BB |
963 | } |
964 | ||
f7a973d9 | 965 | /* Spawn additional taskq threads if required. */ |
f5f2b87d | 966 | if ((++seq_tasks) > spl_taskq_thread_sequential && |
967 | taskq_thread_spawn(tq)) | |
f7a973d9 BB |
968 | seq_tasks = 0; |
969 | ||
cbba7146 | 970 | tqt->tqt_id = TASKQID_INVALID; |
8f2503e0 | 971 | tqt->tqt_flags = 0; |
472a34ca | 972 | wake_up_all(&tq->tq_wait_waitq); |
f7a973d9 BB |
973 | } else { |
974 | if (taskq_thread_should_stop(tq, tqt)) | |
975 | break; | |
bcd68186 BB |
976 | } |
977 | ||
978 | set_current_state(TASK_INTERRUPTIBLE); | |
979 | ||
472a34ca | 980 | } |
bcd68186 BB |
981 | |
982 | __set_current_state(TASK_RUNNING); | |
472a34ca | 983 | tq->tq_nthreads--; |
2c02b71b | 984 | list_del_init(&tqt->tqt_thread_list); |
f7a973d9 BB |
985 | error: |
986 | kmem_free(tqt, sizeof (taskq_thread_t)); | |
066b89e6 | 987 | spin_unlock_irqrestore(&tq->tq_lock, flags); |
bcd68186 | 988 | |
16522ac2 CC |
989 | tsd_set(taskq_tsd, NULL); |
990 | ||
8d9a23e8 | 991 | return (0); |
bcd68186 BB |
992 | } |
993 | ||
f7a973d9 BB |
994 | static taskq_thread_t * |
995 | taskq_thread_create(taskq_t *tq) | |
996 | { | |
997 | static int last_used_cpu = 0; | |
998 | taskq_thread_t *tqt; | |
999 | ||
1000 | tqt = kmem_alloc(sizeof (*tqt), KM_PUSHPAGE); | |
1001 | INIT_LIST_HEAD(&tqt->tqt_thread_list); | |
1002 | INIT_LIST_HEAD(&tqt->tqt_active_list); | |
1003 | tqt->tqt_tq = tq; | |
cbba7146 | 1004 | tqt->tqt_id = TASKQID_INVALID; |
f7a973d9 BB |
1005 | |
1006 | tqt->tqt_thread = spl_kthread_create(taskq_thread, tqt, | |
1007 | "%s", tq->tq_name); | |
1008 | if (tqt->tqt_thread == NULL) { | |
1009 | kmem_free(tqt, sizeof (taskq_thread_t)); | |
1010 | return (NULL); | |
1011 | } | |
1012 | ||
1013 | if (spl_taskq_thread_bind) { | |
1014 | last_used_cpu = (last_used_cpu + 1) % num_online_cpus(); | |
1015 | kthread_bind(tqt->tqt_thread, last_used_cpu); | |
1016 | } | |
1017 | ||
62aa81a5 BB |
1018 | if (spl_taskq_thread_priority) |
1019 | set_user_nice(tqt->tqt_thread, PRIO_TO_NICE(tq->tq_pri)); | |
1020 | ||
f7a973d9 BB |
1021 | wake_up_process(tqt->tqt_thread); |
1022 | ||
1023 | return (tqt); | |
1024 | } | |
1025 | ||
f1ca4da6 | 1026 | taskq_t * |
aed8671c | 1027 | taskq_create(const char *name, int nthreads, pri_t pri, |
472a34ca | 1028 | int minalloc, int maxalloc, uint_t flags) |
f1ca4da6 | 1029 | { |
472a34ca | 1030 | taskq_t *tq; |
2c02b71b | 1031 | taskq_thread_t *tqt; |
f7a973d9 | 1032 | int count = 0, rc = 0, i; |
066b89e6 | 1033 | unsigned long irqflags; |
bcd68186 | 1034 | |
472a34ca | 1035 | ASSERT(name != NULL); |
472a34ca BB |
1036 | ASSERT(minalloc >= 0); |
1037 | ASSERT(maxalloc <= INT_MAX); | |
f7a973d9 | 1038 | ASSERT(!(flags & (TASKQ_CPR_SAFE))); /* Unsupported */ |
bcd68186 | 1039 | |
915404bd BB |
1040 | /* Scale the number of threads using nthreads as a percentage */ |
1041 | if (flags & TASKQ_THREADS_CPU_PCT) { | |
1042 | ASSERT(nthreads <= 100); | |
1043 | ASSERT(nthreads >= 0); | |
1044 | nthreads = MIN(nthreads, 100); | |
1045 | nthreads = MAX(nthreads, 0); | |
1046 | nthreads = MAX((num_online_cpus() * nthreads) / 100, 1); | |
1047 | } | |
1048 | ||
f7a973d9 | 1049 | tq = kmem_alloc(sizeof (*tq), KM_PUSHPAGE); |
472a34ca | 1050 | if (tq == NULL) |
8d9a23e8 | 1051 | return (NULL); |
bcd68186 | 1052 | |
472a34ca | 1053 | spin_lock_init(&tq->tq_lock); |
472a34ca BB |
1054 | INIT_LIST_HEAD(&tq->tq_thread_list); |
1055 | INIT_LIST_HEAD(&tq->tq_active_list); | |
e4f5fa12 | 1056 | tq->tq_name = kmem_strdup(name); |
2c4332cf BB |
1057 | tq->tq_nactive = 0; |
1058 | tq->tq_nthreads = 0; | |
1059 | tq->tq_nspawn = 0; | |
f7a973d9 | 1060 | tq->tq_maxthreads = nthreads; |
2c4332cf BB |
1061 | tq->tq_pri = pri; |
1062 | tq->tq_minalloc = minalloc; | |
1063 | tq->tq_maxalloc = maxalloc; | |
1064 | tq->tq_nalloc = 0; | |
1065 | tq->tq_flags = (flags | TASKQ_ACTIVE); | |
cbba7146 BB |
1066 | tq->tq_next_id = TASKQID_INITIAL; |
1067 | tq->tq_lowest_id = TASKQID_INITIAL; | |
472a34ca BB |
1068 | INIT_LIST_HEAD(&tq->tq_free_list); |
1069 | INIT_LIST_HEAD(&tq->tq_pend_list); | |
1070 | INIT_LIST_HEAD(&tq->tq_prio_list); | |
d9acd930 | 1071 | INIT_LIST_HEAD(&tq->tq_delay_list); |
472a34ca BB |
1072 | init_waitqueue_head(&tq->tq_work_waitq); |
1073 | init_waitqueue_head(&tq->tq_wait_waitq); | |
326172d8 | 1074 | tq->tq_lock_class = TQ_LOCK_GENERAL; |
200366f2 | 1075 | INIT_LIST_HEAD(&tq->tq_taskqs); |
bcd68186 | 1076 | |
f7a973d9 | 1077 | if (flags & TASKQ_PREPOPULATE) { |
066b89e6 | 1078 | spin_lock_irqsave_nested(&tq->tq_lock, irqflags, |
326172d8 | 1079 | tq->tq_lock_class); |
f7a973d9 | 1080 | |
472a34ca | 1081 | for (i = 0; i < minalloc; i++) |
066b89e6 CC |
1082 | task_done(tq, task_alloc(tq, TQ_PUSHPAGE | TQ_NEW, |
1083 | &irqflags)); | |
6e605b6e | 1084 | |
066b89e6 | 1085 | spin_unlock_irqrestore(&tq->tq_lock, irqflags); |
f7a973d9 BB |
1086 | } |
1087 | ||
1088 | if ((flags & TASKQ_DYNAMIC) && spl_taskq_thread_dynamic) | |
1089 | nthreads = 1; | |
6e605b6e | 1090 | |
2c02b71b | 1091 | for (i = 0; i < nthreads; i++) { |
f7a973d9 BB |
1092 | tqt = taskq_thread_create(tq); |
1093 | if (tqt == NULL) | |
2c02b71b | 1094 | rc = 1; |
f7a973d9 BB |
1095 | else |
1096 | count++; | |
2c02b71b | 1097 | } |
bcd68186 | 1098 | |
472a34ca | 1099 | /* Wait for all threads to be started before potential destroy */ |
f7a973d9 | 1100 | wait_event(tq->tq_wait_waitq, tq->tq_nthreads == count); |
5ce028b0 CC |
1101 | /* |
1102 | * taskq_thread might have touched nspawn, but we don't want them to | |
1103 | * because they're not dynamically spawned. So we reset it to 0 | |
1104 | */ | |
1105 | tq->tq_nspawn = 0; | |
bcd68186 | 1106 | |
472a34ca | 1107 | if (rc) { |
aed8671c | 1108 | taskq_destroy(tq); |
472a34ca | 1109 | tq = NULL; |
200366f2 TC |
1110 | } else { |
1111 | down_write(&tq_list_sem); | |
1112 | tq->tq_instance = taskq_find_by_name(name) + 1; | |
1113 | list_add_tail(&tq->tq_taskqs, &tq_list); | |
1114 | up_write(&tq_list_sem); | |
472a34ca | 1115 | } |
bcd68186 | 1116 | |
8d9a23e8 | 1117 | return (tq); |
f1ca4da6 | 1118 | } |
aed8671c | 1119 | EXPORT_SYMBOL(taskq_create); |
b123971f BB |
1120 | |
1121 | void | |
aed8671c | 1122 | taskq_destroy(taskq_t *tq) |
b123971f | 1123 | { |
2c02b71b PS |
1124 | struct task_struct *thread; |
1125 | taskq_thread_t *tqt; | |
046a70c9 | 1126 | taskq_ent_t *t; |
066b89e6 | 1127 | unsigned long flags; |
b123971f | 1128 | |
bcd68186 | 1129 | ASSERT(tq); |
066b89e6 | 1130 | spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); |
f7a973d9 | 1131 | tq->tq_flags &= ~TASKQ_ACTIVE; |
066b89e6 | 1132 | spin_unlock_irqrestore(&tq->tq_lock, flags); |
bcd68186 | 1133 | |
f7a973d9 BB |
1134 | /* |
1135 | * When TASKQ_ACTIVE is clear new tasks may not be added nor may | |
1136 | * new worker threads be spawned for dynamic taskq. | |
1137 | */ | |
1138 | if (dynamic_taskq != NULL) | |
1139 | taskq_wait_outstanding(dynamic_taskq, 0); | |
1140 | ||
aed8671c | 1141 | taskq_wait(tq); |
bcd68186 | 1142 | |
200366f2 TC |
1143 | /* remove taskq from global list used by the kstats */ |
1144 | down_write(&tq_list_sem); | |
1145 | list_del(&tq->tq_taskqs); | |
1146 | up_write(&tq_list_sem); | |
1147 | ||
066b89e6 | 1148 | spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); |
5ce028b0 CC |
1149 | /* wait for spawning threads to insert themselves to the list */ |
1150 | while (tq->tq_nspawn) { | |
1151 | spin_unlock_irqrestore(&tq->tq_lock, flags); | |
1152 | schedule_timeout_interruptible(1); | |
5461eefe BB |
1153 | spin_lock_irqsave_nested(&tq->tq_lock, flags, |
1154 | tq->tq_lock_class); | |
5ce028b0 | 1155 | } |
bcd68186 | 1156 | |
2c02b71b PS |
1157 | /* |
1158 | * Signal each thread to exit and block until it does. Each thread | |
1159 | * is responsible for removing itself from the list and freeing its | |
1160 | * taskq_thread_t. This allows for idle threads to opt to remove | |
1161 | * themselves from the taskq. They can be recreated as needed. | |
1162 | */ | |
1163 | while (!list_empty(&tq->tq_thread_list)) { | |
1164 | tqt = list_entry(tq->tq_thread_list.next, | |
f7a973d9 | 1165 | taskq_thread_t, tqt_thread_list); |
2c02b71b | 1166 | thread = tqt->tqt_thread; |
066b89e6 | 1167 | spin_unlock_irqrestore(&tq->tq_lock, flags); |
2c02b71b PS |
1168 | |
1169 | kthread_stop(thread); | |
1170 | ||
066b89e6 | 1171 | spin_lock_irqsave_nested(&tq->tq_lock, flags, |
326172d8 | 1172 | tq->tq_lock_class); |
2c02b71b PS |
1173 | } |
1174 | ||
472a34ca | 1175 | while (!list_empty(&tq->tq_free_list)) { |
046a70c9 | 1176 | t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list); |
44217f7a PS |
1177 | |
1178 | ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC)); | |
1179 | ||
472a34ca BB |
1180 | list_del_init(&t->tqent_list); |
1181 | task_free(tq, t); | |
1182 | } | |
bcd68186 | 1183 | |
f7a973d9 BB |
1184 | ASSERT0(tq->tq_nthreads); |
1185 | ASSERT0(tq->tq_nalloc); | |
1186 | ASSERT0(tq->tq_nspawn); | |
472a34ca BB |
1187 | ASSERT(list_empty(&tq->tq_thread_list)); |
1188 | ASSERT(list_empty(&tq->tq_active_list)); | |
1189 | ASSERT(list_empty(&tq->tq_free_list)); | |
1190 | ASSERT(list_empty(&tq->tq_pend_list)); | |
1191 | ASSERT(list_empty(&tq->tq_prio_list)); | |
d9acd930 | 1192 | ASSERT(list_empty(&tq->tq_delay_list)); |
bcd68186 | 1193 | |
066b89e6 | 1194 | spin_unlock_irqrestore(&tq->tq_lock, flags); |
2c02b71b | 1195 | |
e4f5fa12 | 1196 | kmem_strfree(tq->tq_name); |
f7a973d9 | 1197 | kmem_free(tq, sizeof (taskq_t)); |
b123971f | 1198 | } |
aed8671c | 1199 | EXPORT_SYMBOL(taskq_destroy); |
e9cb2b4f | 1200 | |
8f3b403a CC |
1201 | |
1202 | static unsigned int spl_taskq_kick = 0; | |
1203 | ||
1204 | /* | |
1205 | * 2.6.36 API Change | |
1206 | * module_param_cb is introduced to take kernel_param_ops and | |
1207 | * module_param_call is marked as obsolete. Also set and get operations | |
1208 | * were changed to take a 'const struct kernel_param *'. | |
1209 | */ | |
1210 | static int | |
1211 | #ifdef module_param_cb | |
1212 | param_set_taskq_kick(const char *val, const struct kernel_param *kp) | |
1213 | #else | |
1214 | param_set_taskq_kick(const char *val, struct kernel_param *kp) | |
1215 | #endif | |
1216 | { | |
1217 | int ret; | |
7cf1fe63 | 1218 | taskq_t *tq = NULL; |
8f3b403a CC |
1219 | taskq_ent_t *t; |
1220 | unsigned long flags; | |
1221 | ||
1222 | ret = param_set_uint(val, kp); | |
1223 | if (ret < 0 || !spl_taskq_kick) | |
1224 | return (ret); | |
1225 | /* reset value */ | |
1226 | spl_taskq_kick = 0; | |
1227 | ||
1228 | down_read(&tq_list_sem); | |
1229 | list_for_each_entry(tq, &tq_list, tq_taskqs) { | |
1230 | spin_lock_irqsave_nested(&tq->tq_lock, flags, | |
1231 | tq->tq_lock_class); | |
1232 | /* Check if the first pending is older than 5 seconds */ | |
1233 | t = taskq_next_ent(tq); | |
1234 | if (t && time_after(jiffies, t->tqent_birth + 5*HZ)) { | |
1235 | (void) taskq_thread_spawn(tq); | |
1236 | printk(KERN_INFO "spl: Kicked taskq %s/%d\n", | |
1237 | tq->tq_name, tq->tq_instance); | |
1238 | } | |
1239 | spin_unlock_irqrestore(&tq->tq_lock, flags); | |
1240 | } | |
1241 | up_read(&tq_list_sem); | |
1242 | return (ret); | |
1243 | } | |
1244 | ||
1245 | #ifdef module_param_cb | |
1246 | static const struct kernel_param_ops param_ops_taskq_kick = { | |
5461eefe BB |
1247 | .set = param_set_taskq_kick, |
1248 | .get = param_get_uint, | |
8f3b403a CC |
1249 | }; |
1250 | module_param_cb(spl_taskq_kick, ¶m_ops_taskq_kick, &spl_taskq_kick, 0644); | |
1251 | #else | |
1252 | module_param_call(spl_taskq_kick, param_set_taskq_kick, param_get_uint, | |
5461eefe | 1253 | &spl_taskq_kick, 0644); |
8f3b403a CC |
1254 | #endif |
1255 | MODULE_PARM_DESC(spl_taskq_kick, | |
5461eefe | 1256 | "Write nonzero to kick stuck taskqs to spawn more threads"); |
8f3b403a | 1257 | |
e9cb2b4f BB |
1258 | int |
1259 | spl_taskq_init(void) | |
1260 | { | |
93ce2b4c | 1261 | init_rwsem(&tq_list_sem); |
16522ac2 CC |
1262 | tsd_create(&taskq_tsd, NULL); |
1263 | ||
3c82160f | 1264 | system_taskq = taskq_create("spl_system_taskq", MAX(boot_ncpus, 64), |
9dc5ffbe | 1265 | maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE|TASKQ_DYNAMIC); |
e9cb2b4f | 1266 | if (system_taskq == NULL) |
8d9a23e8 | 1267 | return (1); |
e9cb2b4f | 1268 | |
f200b836 CC |
1269 | system_delay_taskq = taskq_create("spl_delay_taskq", MAX(boot_ncpus, 4), |
1270 | maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE|TASKQ_DYNAMIC); | |
1271 | if (system_delay_taskq == NULL) { | |
1272 | taskq_destroy(system_taskq); | |
1273 | return (1); | |
1274 | } | |
1275 | ||
f7a973d9 | 1276 | dynamic_taskq = taskq_create("spl_dynamic_taskq", 1, |
9dc5ffbe | 1277 | maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE); |
f7a973d9 BB |
1278 | if (dynamic_taskq == NULL) { |
1279 | taskq_destroy(system_taskq); | |
f200b836 | 1280 | taskq_destroy(system_delay_taskq); |
f7a973d9 BB |
1281 | return (1); |
1282 | } | |
1283 | ||
2c4332cf BB |
1284 | /* |
1285 | * This is used to annotate tq_lock, so | |
1286 | * taskq_dispatch -> taskq_thread_spawn -> taskq_dispatch | |
326172d8 OF |
1287 | * does not trigger a lockdep warning re: possible recursive locking |
1288 | */ | |
1289 | dynamic_taskq->tq_lock_class = TQ_LOCK_DYNAMIC; | |
1290 | ||
8d9a23e8 | 1291 | return (0); |
e9cb2b4f BB |
1292 | } |
1293 | ||
1294 | void | |
1295 | spl_taskq_fini(void) | |
1296 | { | |
f7a973d9 BB |
1297 | taskq_destroy(dynamic_taskq); |
1298 | dynamic_taskq = NULL; | |
1299 | ||
f200b836 CC |
1300 | taskq_destroy(system_delay_taskq); |
1301 | system_delay_taskq = NULL; | |
1302 | ||
e9cb2b4f | 1303 | taskq_destroy(system_taskq); |
f7a973d9 | 1304 | system_taskq = NULL; |
16522ac2 CC |
1305 | |
1306 | tsd_destroy(&taskq_tsd); | |
e9cb2b4f | 1307 | } |