]>
Commit | Line | Data |
---|---|---|
1 | /*****************************************************************************\ | |
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>. | |
6 | * UCRL-CODE-235197 | |
7 | * | |
8 | * This file is part of the SPL, Solaris Porting Layer. | |
9 | * For details, see <http://github.com/behlendorf/spl/>. | |
10 | * | |
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 | |
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 | |
22 | * with the SPL. If not, see <http://www.gnu.org/licenses/>. | |
23 | ***************************************************************************** | |
24 | * Solaris Porting Layer (SPL) Task Queue Implementation. | |
25 | \*****************************************************************************/ | |
26 | ||
27 | #include <sys/taskq.h> | |
28 | #include <sys/kmem.h> | |
29 | #include <spl-debug.h> | |
30 | ||
31 | #ifdef SS_DEBUG_SUBSYS | |
32 | #undef SS_DEBUG_SUBSYS | |
33 | #endif | |
34 | ||
35 | #define SS_DEBUG_SUBSYS SS_TASKQ | |
36 | ||
37 | /* Global system-wide dynamic task queue available for all consumers */ | |
38 | taskq_t *system_taskq; | |
39 | EXPORT_SYMBOL(system_taskq); | |
40 | ||
41 | static int | |
42 | task_km_flags(uint_t flags) | |
43 | { | |
44 | if (flags & TQ_NOSLEEP) | |
45 | return KM_NOSLEEP; | |
46 | ||
47 | if (flags & TQ_PUSHPAGE) | |
48 | return KM_PUSHPAGE; | |
49 | ||
50 | return KM_SLEEP; | |
51 | } | |
52 | ||
53 | /* | |
54 | * NOTE: Must be called with tq->tq_lock held, returns a list_t which | |
55 | * is not attached to the free, work, or pending taskq lists. | |
56 | */ | |
57 | static taskq_ent_t * | |
58 | task_alloc(taskq_t *tq, uint_t flags) | |
59 | { | |
60 | taskq_ent_t *t; | |
61 | int count = 0; | |
62 | SENTRY; | |
63 | ||
64 | ASSERT(tq); | |
65 | ASSERT(spin_is_locked(&tq->tq_lock)); | |
66 | retry: | |
67 | /* Acquire taskq_ent_t's from free list if available */ | |
68 | if (!list_empty(&tq->tq_free_list) && !(flags & TQ_NEW)) { | |
69 | t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list); | |
70 | ||
71 | ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC)); | |
72 | ||
73 | list_del_init(&t->tqent_list); | |
74 | SRETURN(t); | |
75 | } | |
76 | ||
77 | /* Free list is empty and memory allocations are prohibited */ | |
78 | if (flags & TQ_NOALLOC) | |
79 | SRETURN(NULL); | |
80 | ||
81 | /* Hit maximum taskq_ent_t pool size */ | |
82 | if (tq->tq_nalloc >= tq->tq_maxalloc) { | |
83 | if (flags & TQ_NOSLEEP) | |
84 | SRETURN(NULL); | |
85 | ||
86 | /* | |
87 | * Sleep periodically polling the free list for an available | |
88 | * taskq_ent_t. Dispatching with TQ_SLEEP should always succeed | |
89 | * but we cannot block forever waiting for an taskq_ent_t to | |
90 | * show up in the free list, otherwise a deadlock can happen. | |
91 | * | |
92 | * Therefore, we need to allocate a new task even if the number | |
93 | * of allocated tasks is above tq->tq_maxalloc, but we still | |
94 | * end up delaying the task allocation by one second, thereby | |
95 | * throttling the task dispatch rate. | |
96 | */ | |
97 | spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags); | |
98 | schedule_timeout(HZ / 100); | |
99 | spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags); | |
100 | if (count < 100) | |
101 | SGOTO(retry, count++); | |
102 | } | |
103 | ||
104 | spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags); | |
105 | t = kmem_alloc(sizeof(taskq_ent_t), task_km_flags(flags)); | |
106 | spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags); | |
107 | ||
108 | if (t) { | |
109 | taskq_init_ent(t); | |
110 | tq->tq_nalloc++; | |
111 | } | |
112 | ||
113 | SRETURN(t); | |
114 | } | |
115 | ||
116 | /* | |
117 | * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t | |
118 | * to already be removed from the free, work, or pending taskq lists. | |
119 | */ | |
120 | static void | |
121 | task_free(taskq_t *tq, taskq_ent_t *t) | |
122 | { | |
123 | SENTRY; | |
124 | ||
125 | ASSERT(tq); | |
126 | ASSERT(t); | |
127 | ASSERT(spin_is_locked(&tq->tq_lock)); | |
128 | ASSERT(list_empty(&t->tqent_list)); | |
129 | ||
130 | kmem_free(t, sizeof(taskq_ent_t)); | |
131 | tq->tq_nalloc--; | |
132 | ||
133 | SEXIT; | |
134 | } | |
135 | ||
136 | /* | |
137 | * NOTE: Must be called with tq->tq_lock held, either destroys the | |
138 | * taskq_ent_t if too many exist or moves it to the free list for later use. | |
139 | */ | |
140 | static void | |
141 | task_done(taskq_t *tq, taskq_ent_t *t) | |
142 | { | |
143 | SENTRY; | |
144 | ASSERT(tq); | |
145 | ASSERT(t); | |
146 | ASSERT(spin_is_locked(&tq->tq_lock)); | |
147 | ||
148 | list_del_init(&t->tqent_list); | |
149 | ||
150 | if (tq->tq_nalloc <= tq->tq_minalloc) { | |
151 | t->tqent_id = 0; | |
152 | t->tqent_func = NULL; | |
153 | t->tqent_arg = NULL; | |
154 | t->tqent_flags = 0; | |
155 | ||
156 | list_add_tail(&t->tqent_list, &tq->tq_free_list); | |
157 | } else { | |
158 | task_free(tq, t); | |
159 | } | |
160 | ||
161 | SEXIT; | |
162 | } | |
163 | ||
164 | /* | |
165 | * As tasks are submitted to the task queue they are assigned a | |
166 | * monotonically increasing taskqid and added to the tail of the pending | |
167 | * list. As worker threads become available the tasks are removed from | |
168 | * the head of the pending or priority list, giving preference to the | |
169 | * priority list. The tasks are then removed from their respective | |
170 | * list, and the taskq_thread servicing the task is added to the active | |
171 | * list, preserving the order using the serviced task's taskqid. | |
172 | * Finally, as tasks complete the taskq_thread servicing the task is | |
173 | * removed from the active list. This means that the pending task and | |
174 | * active taskq_thread lists are always kept sorted by taskqid. Thus the | |
175 | * lowest outstanding incomplete taskqid can be determined simply by | |
176 | * checking the min taskqid for each head item on the pending, priority, | |
177 | * and active taskq_thread list. This value is stored in | |
178 | * tq->tq_lowest_id and only updated to the new lowest id when the | |
179 | * previous lowest id completes. All taskqids lower than | |
180 | * tq->tq_lowest_id must have completed. It is also possible larger | |
181 | * taskqid's have completed because they may be processed in parallel by | |
182 | * several worker threads. However, this is not a problem because the | |
183 | * behavior of taskq_wait_id() is to block until all previously | |
184 | * submitted taskqid's have completed. | |
185 | * | |
186 | * XXX: Taskqid_t wrapping is not handled. However, taskqid_t's are | |
187 | * 64-bit values so even if a taskq is processing 2^24 (16,777,216) | |
188 | * taskqid_ts per second it will still take 2^40 seconds, 34,865 years, | |
189 | * before the wrap occurs. I can live with that for now. | |
190 | */ | |
191 | static int | |
192 | taskq_wait_check(taskq_t *tq, taskqid_t id) | |
193 | { | |
194 | int rc; | |
195 | ||
196 | spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags); | |
197 | rc = (id < tq->tq_lowest_id); | |
198 | spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags); | |
199 | ||
200 | SRETURN(rc); | |
201 | } | |
202 | ||
203 | void | |
204 | __taskq_wait_id(taskq_t *tq, taskqid_t id) | |
205 | { | |
206 | SENTRY; | |
207 | ASSERT(tq); | |
208 | ||
209 | wait_event(tq->tq_wait_waitq, taskq_wait_check(tq, id)); | |
210 | ||
211 | SEXIT; | |
212 | } | |
213 | EXPORT_SYMBOL(__taskq_wait_id); | |
214 | ||
215 | void | |
216 | __taskq_wait(taskq_t *tq) | |
217 | { | |
218 | taskqid_t id; | |
219 | SENTRY; | |
220 | ASSERT(tq); | |
221 | ||
222 | /* Wait for the largest outstanding taskqid */ | |
223 | spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags); | |
224 | id = tq->tq_next_id - 1; | |
225 | spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags); | |
226 | ||
227 | __taskq_wait_id(tq, id); | |
228 | ||
229 | SEXIT; | |
230 | ||
231 | } | |
232 | EXPORT_SYMBOL(__taskq_wait); | |
233 | ||
234 | int | |
235 | __taskq_member(taskq_t *tq, void *t) | |
236 | { | |
237 | struct list_head *l; | |
238 | taskq_thread_t *tqt; | |
239 | SENTRY; | |
240 | ||
241 | ASSERT(tq); | |
242 | ASSERT(t); | |
243 | ||
244 | list_for_each(l, &tq->tq_thread_list) { | |
245 | tqt = list_entry(l, taskq_thread_t, tqt_thread_list); | |
246 | if (tqt->tqt_thread == (struct task_struct *)t) | |
247 | SRETURN(1); | |
248 | } | |
249 | ||
250 | SRETURN(0); | |
251 | } | |
252 | EXPORT_SYMBOL(__taskq_member); | |
253 | ||
254 | taskqid_t | |
255 | __taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags) | |
256 | { | |
257 | taskq_ent_t *t; | |
258 | taskqid_t rc = 0; | |
259 | SENTRY; | |
260 | ||
261 | ASSERT(tq); | |
262 | ASSERT(func); | |
263 | ||
264 | spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags); | |
265 | ||
266 | /* Taskq being destroyed and all tasks drained */ | |
267 | if (!(tq->tq_flags & TQ_ACTIVE)) | |
268 | SGOTO(out, rc = 0); | |
269 | ||
270 | /* Do not queue the task unless there is idle thread for it */ | |
271 | ASSERT(tq->tq_nactive <= tq->tq_nthreads); | |
272 | if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads)) | |
273 | SGOTO(out, rc = 0); | |
274 | ||
275 | if ((t = task_alloc(tq, flags)) == NULL) | |
276 | SGOTO(out, rc = 0); | |
277 | ||
278 | spin_lock(&t->tqent_lock); | |
279 | ||
280 | /* Queue to the priority list instead of the pending list */ | |
281 | if (flags & TQ_FRONT) | |
282 | list_add_tail(&t->tqent_list, &tq->tq_prio_list); | |
283 | else | |
284 | list_add_tail(&t->tqent_list, &tq->tq_pend_list); | |
285 | ||
286 | t->tqent_id = rc = tq->tq_next_id; | |
287 | tq->tq_next_id++; | |
288 | t->tqent_func = func; | |
289 | t->tqent_arg = arg; | |
290 | ||
291 | ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC)); | |
292 | ||
293 | spin_unlock(&t->tqent_lock); | |
294 | ||
295 | wake_up(&tq->tq_work_waitq); | |
296 | out: | |
297 | spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags); | |
298 | SRETURN(rc); | |
299 | } | |
300 | EXPORT_SYMBOL(__taskq_dispatch); | |
301 | ||
302 | void | |
303 | __taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags, | |
304 | taskq_ent_t *t) | |
305 | { | |
306 | SENTRY; | |
307 | ||
308 | ASSERT(tq); | |
309 | ASSERT(func); | |
310 | ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC)); | |
311 | ||
312 | spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags); | |
313 | ||
314 | /* Taskq being destroyed and all tasks drained */ | |
315 | if (!(tq->tq_flags & TQ_ACTIVE)) { | |
316 | t->tqent_id = 0; | |
317 | goto out; | |
318 | } | |
319 | ||
320 | spin_lock(&t->tqent_lock); | |
321 | ||
322 | /* | |
323 | * Mark it as a prealloc'd task. This is important | |
324 | * to ensure that we don't free it later. | |
325 | */ | |
326 | t->tqent_flags |= TQENT_FLAG_PREALLOC; | |
327 | ||
328 | /* Queue to the priority list instead of the pending list */ | |
329 | if (flags & TQ_FRONT) | |
330 | list_add_tail(&t->tqent_list, &tq->tq_prio_list); | |
331 | else | |
332 | list_add_tail(&t->tqent_list, &tq->tq_pend_list); | |
333 | ||
334 | t->tqent_id = tq->tq_next_id; | |
335 | tq->tq_next_id++; | |
336 | t->tqent_func = func; | |
337 | t->tqent_arg = arg; | |
338 | ||
339 | spin_unlock(&t->tqent_lock); | |
340 | ||
341 | wake_up(&tq->tq_work_waitq); | |
342 | out: | |
343 | spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags); | |
344 | SEXIT; | |
345 | } | |
346 | EXPORT_SYMBOL(__taskq_dispatch_ent); | |
347 | ||
348 | int | |
349 | __taskq_empty_ent(taskq_ent_t *t) | |
350 | { | |
351 | return list_empty(&t->tqent_list); | |
352 | } | |
353 | EXPORT_SYMBOL(__taskq_empty_ent); | |
354 | ||
355 | void | |
356 | __taskq_init_ent(taskq_ent_t *t) | |
357 | { | |
358 | spin_lock_init(&t->tqent_lock); | |
359 | INIT_LIST_HEAD(&t->tqent_list); | |
360 | t->tqent_id = 0; | |
361 | t->tqent_func = NULL; | |
362 | t->tqent_arg = NULL; | |
363 | t->tqent_flags = 0; | |
364 | } | |
365 | EXPORT_SYMBOL(__taskq_init_ent); | |
366 | ||
367 | /* | |
368 | * Returns the lowest incomplete taskqid_t. The taskqid_t may | |
369 | * be queued on the pending list, on the priority list, or on | |
370 | * the work list currently being handled, but it is not 100% | |
371 | * complete yet. | |
372 | */ | |
373 | static taskqid_t | |
374 | taskq_lowest_id(taskq_t *tq) | |
375 | { | |
376 | taskqid_t lowest_id = tq->tq_next_id; | |
377 | taskq_ent_t *t; | |
378 | taskq_thread_t *tqt; | |
379 | SENTRY; | |
380 | ||
381 | ASSERT(tq); | |
382 | ASSERT(spin_is_locked(&tq->tq_lock)); | |
383 | ||
384 | if (!list_empty(&tq->tq_pend_list)) { | |
385 | t = list_entry(tq->tq_pend_list.next, taskq_ent_t, tqent_list); | |
386 | lowest_id = MIN(lowest_id, t->tqent_id); | |
387 | } | |
388 | ||
389 | if (!list_empty(&tq->tq_prio_list)) { | |
390 | t = list_entry(tq->tq_prio_list.next, taskq_ent_t, tqent_list); | |
391 | lowest_id = MIN(lowest_id, t->tqent_id); | |
392 | } | |
393 | ||
394 | if (!list_empty(&tq->tq_active_list)) { | |
395 | tqt = list_entry(tq->tq_active_list.next, taskq_thread_t, | |
396 | tqt_active_list); | |
397 | ASSERT(tqt->tqt_id != 0); | |
398 | lowest_id = MIN(lowest_id, tqt->tqt_id); | |
399 | } | |
400 | ||
401 | SRETURN(lowest_id); | |
402 | } | |
403 | ||
404 | /* | |
405 | * Insert a task into a list keeping the list sorted by increasing taskqid. | |
406 | */ | |
407 | static void | |
408 | taskq_insert_in_order(taskq_t *tq, taskq_thread_t *tqt) | |
409 | { | |
410 | taskq_thread_t *w; | |
411 | struct list_head *l; | |
412 | ||
413 | SENTRY; | |
414 | ASSERT(tq); | |
415 | ASSERT(tqt); | |
416 | ASSERT(spin_is_locked(&tq->tq_lock)); | |
417 | ||
418 | list_for_each_prev(l, &tq->tq_active_list) { | |
419 | w = list_entry(l, taskq_thread_t, tqt_active_list); | |
420 | if (w->tqt_id < tqt->tqt_id) { | |
421 | list_add(&tqt->tqt_active_list, l); | |
422 | break; | |
423 | } | |
424 | } | |
425 | if (l == &tq->tq_active_list) | |
426 | list_add(&tqt->tqt_active_list, &tq->tq_active_list); | |
427 | ||
428 | SEXIT; | |
429 | } | |
430 | ||
431 | static int | |
432 | taskq_thread(void *args) | |
433 | { | |
434 | DECLARE_WAITQUEUE(wait, current); | |
435 | sigset_t blocked; | |
436 | taskq_thread_t *tqt = args; | |
437 | taskq_t *tq; | |
438 | taskq_ent_t *t; | |
439 | struct list_head *pend_list; | |
440 | SENTRY; | |
441 | ||
442 | ASSERT(tqt); | |
443 | tq = tqt->tqt_tq; | |
444 | current->flags |= PF_NOFREEZE; | |
445 | ||
446 | sigfillset(&blocked); | |
447 | sigprocmask(SIG_BLOCK, &blocked, NULL); | |
448 | flush_signals(current); | |
449 | ||
450 | spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags); | |
451 | tq->tq_nthreads++; | |
452 | wake_up(&tq->tq_wait_waitq); | |
453 | set_current_state(TASK_INTERRUPTIBLE); | |
454 | ||
455 | while (!kthread_should_stop()) { | |
456 | ||
457 | if (list_empty(&tq->tq_pend_list) && | |
458 | list_empty(&tq->tq_prio_list)) { | |
459 | add_wait_queue_exclusive(&tq->tq_work_waitq, &wait); | |
460 | spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags); | |
461 | schedule(); | |
462 | spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags); | |
463 | remove_wait_queue(&tq->tq_work_waitq, &wait); | |
464 | } else { | |
465 | __set_current_state(TASK_RUNNING); | |
466 | } | |
467 | ||
468 | ||
469 | if (!list_empty(&tq->tq_prio_list)) | |
470 | pend_list = &tq->tq_prio_list; | |
471 | else if (!list_empty(&tq->tq_pend_list)) | |
472 | pend_list = &tq->tq_pend_list; | |
473 | else | |
474 | pend_list = NULL; | |
475 | ||
476 | if (pend_list) { | |
477 | t = list_entry(pend_list->next,taskq_ent_t,tqent_list); | |
478 | list_del_init(&t->tqent_list); | |
479 | ||
480 | /* In order to support recursively dispatching a | |
481 | * preallocated taskq_ent_t, tqent_id must be | |
482 | * stored prior to executing tqent_func. */ | |
483 | tqt->tqt_id = t->tqent_id; | |
484 | ||
485 | /* We must store a copy of the flags prior to | |
486 | * servicing the task (servicing a prealloc'd task | |
487 | * returns the ownership of the tqent back to | |
488 | * the caller of taskq_dispatch). Thus, | |
489 | * tqent_flags _may_ change within the call. */ | |
490 | tqt->tqt_flags = t->tqent_flags; | |
491 | ||
492 | taskq_insert_in_order(tq, tqt); | |
493 | tq->tq_nactive++; | |
494 | spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags); | |
495 | ||
496 | /* Perform the requested task */ | |
497 | t->tqent_func(t->tqent_arg); | |
498 | ||
499 | spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags); | |
500 | tq->tq_nactive--; | |
501 | list_del_init(&tqt->tqt_active_list); | |
502 | ||
503 | /* For prealloc'd tasks, we don't free anything. */ | |
504 | if ((tq->tq_flags & TASKQ_DYNAMIC) || | |
505 | !(tqt->tqt_flags & TQENT_FLAG_PREALLOC)) | |
506 | task_done(tq, t); | |
507 | ||
508 | /* When the current lowest outstanding taskqid is | |
509 | * done calculate the new lowest outstanding id */ | |
510 | if (tq->tq_lowest_id == tqt->tqt_id) { | |
511 | tq->tq_lowest_id = taskq_lowest_id(tq); | |
512 | ASSERT3S(tq->tq_lowest_id, >, tqt->tqt_id); | |
513 | } | |
514 | ||
515 | tqt->tqt_id = 0; | |
516 | tqt->tqt_flags = 0; | |
517 | wake_up_all(&tq->tq_wait_waitq); | |
518 | } | |
519 | ||
520 | set_current_state(TASK_INTERRUPTIBLE); | |
521 | ||
522 | } | |
523 | ||
524 | __set_current_state(TASK_RUNNING); | |
525 | tq->tq_nthreads--; | |
526 | list_del_init(&tqt->tqt_thread_list); | |
527 | kmem_free(tqt, sizeof(taskq_thread_t)); | |
528 | ||
529 | spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags); | |
530 | ||
531 | SRETURN(0); | |
532 | } | |
533 | ||
534 | taskq_t * | |
535 | __taskq_create(const char *name, int nthreads, pri_t pri, | |
536 | int minalloc, int maxalloc, uint_t flags) | |
537 | { | |
538 | taskq_t *tq; | |
539 | taskq_thread_t *tqt; | |
540 | int rc = 0, i, j = 0; | |
541 | SENTRY; | |
542 | ||
543 | ASSERT(name != NULL); | |
544 | ASSERT(pri <= maxclsyspri); | |
545 | ASSERT(minalloc >= 0); | |
546 | ASSERT(maxalloc <= INT_MAX); | |
547 | ASSERT(!(flags & (TASKQ_CPR_SAFE | TASKQ_DYNAMIC))); /* Unsupported */ | |
548 | ||
549 | /* Scale the number of threads using nthreads as a percentage */ | |
550 | if (flags & TASKQ_THREADS_CPU_PCT) { | |
551 | ASSERT(nthreads <= 100); | |
552 | ASSERT(nthreads >= 0); | |
553 | nthreads = MIN(nthreads, 100); | |
554 | nthreads = MAX(nthreads, 0); | |
555 | nthreads = MAX((num_online_cpus() * nthreads) / 100, 1); | |
556 | } | |
557 | ||
558 | tq = kmem_alloc(sizeof(*tq), KM_PUSHPAGE); | |
559 | if (tq == NULL) | |
560 | SRETURN(NULL); | |
561 | ||
562 | spin_lock_init(&tq->tq_lock); | |
563 | spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags); | |
564 | INIT_LIST_HEAD(&tq->tq_thread_list); | |
565 | INIT_LIST_HEAD(&tq->tq_active_list); | |
566 | tq->tq_name = name; | |
567 | tq->tq_nactive = 0; | |
568 | tq->tq_nthreads = 0; | |
569 | tq->tq_pri = pri; | |
570 | tq->tq_minalloc = minalloc; | |
571 | tq->tq_maxalloc = maxalloc; | |
572 | tq->tq_nalloc = 0; | |
573 | tq->tq_flags = (flags | TQ_ACTIVE); | |
574 | tq->tq_next_id = 1; | |
575 | tq->tq_lowest_id = 1; | |
576 | INIT_LIST_HEAD(&tq->tq_free_list); | |
577 | INIT_LIST_HEAD(&tq->tq_pend_list); | |
578 | INIT_LIST_HEAD(&tq->tq_prio_list); | |
579 | init_waitqueue_head(&tq->tq_work_waitq); | |
580 | init_waitqueue_head(&tq->tq_wait_waitq); | |
581 | ||
582 | if (flags & TASKQ_PREPOPULATE) | |
583 | for (i = 0; i < minalloc; i++) | |
584 | task_done(tq, task_alloc(tq, TQ_PUSHPAGE | TQ_NEW)); | |
585 | ||
586 | spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags); | |
587 | ||
588 | for (i = 0; i < nthreads; i++) { | |
589 | tqt = kmem_alloc(sizeof(*tqt), KM_PUSHPAGE); | |
590 | INIT_LIST_HEAD(&tqt->tqt_thread_list); | |
591 | INIT_LIST_HEAD(&tqt->tqt_active_list); | |
592 | tqt->tqt_tq = tq; | |
593 | tqt->tqt_id = 0; | |
594 | ||
595 | tqt->tqt_thread = kthread_create(taskq_thread, tqt, | |
596 | "%s/%d", name, i); | |
597 | if (tqt->tqt_thread) { | |
598 | list_add(&tqt->tqt_thread_list, &tq->tq_thread_list); | |
599 | kthread_bind(tqt->tqt_thread, i % num_online_cpus()); | |
600 | set_user_nice(tqt->tqt_thread, PRIO_TO_NICE(pri)); | |
601 | wake_up_process(tqt->tqt_thread); | |
602 | j++; | |
603 | } else { | |
604 | kmem_free(tqt, sizeof(taskq_thread_t)); | |
605 | rc = 1; | |
606 | } | |
607 | } | |
608 | ||
609 | /* Wait for all threads to be started before potential destroy */ | |
610 | wait_event(tq->tq_wait_waitq, tq->tq_nthreads == j); | |
611 | ||
612 | if (rc) { | |
613 | __taskq_destroy(tq); | |
614 | tq = NULL; | |
615 | } | |
616 | ||
617 | SRETURN(tq); | |
618 | } | |
619 | EXPORT_SYMBOL(__taskq_create); | |
620 | ||
621 | void | |
622 | __taskq_destroy(taskq_t *tq) | |
623 | { | |
624 | struct task_struct *thread; | |
625 | taskq_thread_t *tqt; | |
626 | taskq_ent_t *t; | |
627 | SENTRY; | |
628 | ||
629 | ASSERT(tq); | |
630 | spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags); | |
631 | tq->tq_flags &= ~TQ_ACTIVE; | |
632 | spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags); | |
633 | ||
634 | /* TQ_ACTIVE cleared prevents new tasks being added to pending */ | |
635 | __taskq_wait(tq); | |
636 | ||
637 | spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags); | |
638 | ||
639 | /* | |
640 | * Signal each thread to exit and block until it does. Each thread | |
641 | * is responsible for removing itself from the list and freeing its | |
642 | * taskq_thread_t. This allows for idle threads to opt to remove | |
643 | * themselves from the taskq. They can be recreated as needed. | |
644 | */ | |
645 | while (!list_empty(&tq->tq_thread_list)) { | |
646 | tqt = list_entry(tq->tq_thread_list.next, | |
647 | taskq_thread_t, tqt_thread_list); | |
648 | thread = tqt->tqt_thread; | |
649 | spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags); | |
650 | ||
651 | kthread_stop(thread); | |
652 | ||
653 | spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags); | |
654 | } | |
655 | ||
656 | while (!list_empty(&tq->tq_free_list)) { | |
657 | t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list); | |
658 | ||
659 | ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC)); | |
660 | ||
661 | list_del_init(&t->tqent_list); | |
662 | task_free(tq, t); | |
663 | } | |
664 | ||
665 | ASSERT(tq->tq_nthreads == 0); | |
666 | ASSERT(tq->tq_nalloc == 0); | |
667 | ASSERT(list_empty(&tq->tq_thread_list)); | |
668 | ASSERT(list_empty(&tq->tq_active_list)); | |
669 | ASSERT(list_empty(&tq->tq_free_list)); | |
670 | ASSERT(list_empty(&tq->tq_pend_list)); | |
671 | ASSERT(list_empty(&tq->tq_prio_list)); | |
672 | ||
673 | spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags); | |
674 | ||
675 | kmem_free(tq, sizeof(taskq_t)); | |
676 | ||
677 | SEXIT; | |
678 | } | |
679 | EXPORT_SYMBOL(__taskq_destroy); | |
680 | ||
681 | int | |
682 | spl_taskq_init(void) | |
683 | { | |
684 | SENTRY; | |
685 | ||
686 | /* Solaris creates a dynamic taskq of up to 64 threads, however in | |
687 | * a Linux environment 1 thread per-core is usually about right */ | |
688 | system_taskq = taskq_create("spl_system_taskq", num_online_cpus(), | |
689 | minclsyspri, 4, 512, TASKQ_PREPOPULATE); | |
690 | if (system_taskq == NULL) | |
691 | SRETURN(1); | |
692 | ||
693 | SRETURN(0); | |
694 | } | |
695 | ||
696 | void | |
697 | spl_taskq_fini(void) | |
698 | { | |
699 | SENTRY; | |
700 | taskq_destroy(system_taskq); | |
701 | SEXIT; | |
702 | } |