]> git.proxmox.com Git - mirror_spl.git/blame - module/spl/spl-taskq.c
Add 3 missing typedefs.
[mirror_spl.git] / module / spl / spl-taskq.c
CommitLineData
715f6251 1/*
2 * This file is part of the SPL: Solaris Porting Layer.
3 *
4 * Copyright (c) 2008 Lawrence Livermore National Security, LLC.
5 * Produced at Lawrence Livermore National Laboratory
6 * Written by:
7 * Brian Behlendorf <behlendorf1@llnl.gov>,
8 * Herb Wartens <wartens2@llnl.gov>,
9 * Jim Garlick <garlick@llnl.gov>
10 * UCRL-CODE-235197
11 *
12 * This is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
f4b37741 27#include <sys/taskq.h>
3d061e9d 28#include <sys/kmem.h>
f1ca4da6 29
937879f1 30#ifdef DEBUG_SUBSYSTEM
31#undef DEBUG_SUBSYSTEM
32#endif
33
34#define DEBUG_SUBSYSTEM S_TASKQ
35
e9cb2b4f
BB
36/* Global system-wide dynamic task queue available for all consumers */
37taskq_t *system_taskq;
38EXPORT_SYMBOL(system_taskq);
39
3d061e9d 40typedef struct spl_task {
41 spinlock_t t_lock;
42 struct list_head t_list;
43 taskqid_t t_id;
44 task_func_t *t_func;
45 void *t_arg;
46} spl_task_t;
47
82387586
BB
48/*
49 * NOTE: Must be called with tq->tq_lock held, returns a list_t which
bcd68186 50 * is not attached to the free, work, or pending taskq lists.
f1ca4da6 51 */
3d061e9d 52static spl_task_t *
bcd68186 53task_alloc(taskq_t *tq, uint_t flags)
54{
3d061e9d 55 spl_task_t *t;
bcd68186 56 int count = 0;
57 ENTRY;
58
59 ASSERT(tq);
60 ASSERT(flags & (TQ_SLEEP | TQ_NOSLEEP)); /* One set */
61 ASSERT(!((flags & TQ_SLEEP) && (flags & TQ_NOSLEEP))); /* Not both */
3d061e9d 62 ASSERT(spin_is_locked(&tq->tq_lock));
bcd68186 63retry:
7257ec41 64 /* Acquire spl_task_t's from free list if available */
bcd68186 65 if (!list_empty(&tq->tq_free_list) && !(flags & TQ_NEW)) {
3d061e9d 66 t = list_entry(tq->tq_free_list.next, spl_task_t, t_list);
67 list_del_init(&t->t_list);
68 RETURN(t);
bcd68186 69 }
70
7257ec41 71 /* Free list is empty and memory allocations are prohibited */
bcd68186 72 if (flags & TQ_NOALLOC)
73 RETURN(NULL);
74
3d061e9d 75 /* Hit maximum spl_task_t pool size */
bcd68186 76 if (tq->tq_nalloc >= tq->tq_maxalloc) {
77 if (flags & TQ_NOSLEEP)
78 RETURN(NULL);
79
80 /* Sleep periodically polling the free list for an available
3d061e9d 81 * spl_task_t. If a full second passes and we have not found
bcd68186 82 * one gives up and return a NULL to the caller. */
83 if (flags & TQ_SLEEP) {
749045bb 84 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 85 schedule_timeout(HZ / 100);
749045bb 86 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 87 if (count < 100)
88 GOTO(retry, count++);
89
90 RETURN(NULL);
91 }
92
7257ec41 93 /* Unreachable, TQ_SLEEP or TQ_NOSLEEP */
bcd68186 94 SBUG();
95 }
96
749045bb 97 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
3d061e9d 98 t = kmem_alloc(sizeof(spl_task_t), flags & (TQ_SLEEP | TQ_NOSLEEP));
749045bb 99 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 100
101 if (t) {
102 spin_lock_init(&t->t_lock);
103 INIT_LIST_HEAD(&t->t_list);
104 t->t_id = 0;
105 t->t_func = NULL;
106 t->t_arg = NULL;
107 tq->tq_nalloc++;
108 }
109
110 RETURN(t);
111}
112
82387586
BB
113/*
114 * NOTE: Must be called with tq->tq_lock held, expects the spl_task_t
bcd68186 115 * to already be removed from the free, work, or pending taskq lists.
116 */
117static void
3d061e9d 118task_free(taskq_t *tq, spl_task_t *t)
bcd68186 119{
120 ENTRY;
121
122 ASSERT(tq);
123 ASSERT(t);
124 ASSERT(spin_is_locked(&tq->tq_lock));
125 ASSERT(list_empty(&t->t_list));
126
3d061e9d 127 kmem_free(t, sizeof(spl_task_t));
bcd68186 128 tq->tq_nalloc--;
f1ca4da6 129
bcd68186 130 EXIT;
131}
132
82387586
BB
133/*
134 * NOTE: Must be called with tq->tq_lock held, either destroys the
3d061e9d 135 * spl_task_t if too many exist or moves it to the free list for later use.
bcd68186 136 */
f1ca4da6 137static void
3d061e9d 138task_done(taskq_t *tq, spl_task_t *t)
f1ca4da6 139{
bcd68186 140 ENTRY;
141 ASSERT(tq);
142 ASSERT(t);
143 ASSERT(spin_is_locked(&tq->tq_lock));
144
145 list_del_init(&t->t_list);
f1ca4da6 146
bcd68186 147 if (tq->tq_nalloc <= tq->tq_minalloc) {
148 t->t_id = 0;
149 t->t_func = NULL;
150 t->t_arg = NULL;
9ab1ac14 151 list_add_tail(&t->t_list, &tq->tq_free_list);
bcd68186 152 } else {
153 task_free(tq, t);
154 }
f1ca4da6 155
bcd68186 156 EXIT;
f1ca4da6 157}
158
82387586
BB
159/*
160 * As tasks are submitted to the task queue they are assigned a
161 * monotonically increasing taskqid and added to the tail of the
162 * pending list. As worker threads become available the tasks are
163 * removed from the head of the pending list and added to the tail
164 * of the work list. Finally, as tasks complete they are removed
165 * from the work list. This means that the pending and work lists
166 * are always kept sorted by taskqid. Thus the lowest outstanding
167 * incomplete taskqid can be determined simply by checking the min
168 * taskqid for each head item on the pending and work list. This
169 * value is stored in tq->tq_lowest_id and only updated to the new
170 * lowest id when the previous lowest id completes. All taskqids
171 * lower than tq->tq_lowest_id must have completed. It is also
172 * possible larger taskqid's have completed because they may be
173 * processed in parallel by several worker threads. However, this
174 * is not a problem because the behavior of taskq_wait_id() is to
175 * block until all previously submitted taskqid's have completed.
176 *
177 * XXX: Taskqid_t wrapping is not handled. However, taskqid_t's are
178 * 64-bit values so even if a taskq is processing 2^24 (16,777,216)
179 * taskqid_ts per second it will still take 2^40 seconds, 34,865 years,
180 * before the wrap occurs. I can live with that for now.
bcd68186 181 */
182static int
183taskq_wait_check(taskq_t *tq, taskqid_t id)
184{
7257ec41
BB
185 int rc;
186
187 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
188 rc = (id < tq->tq_lowest_id);
189 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
190
191 RETURN(rc);
bcd68186 192}
193
bcd68186 194void
195__taskq_wait_id(taskq_t *tq, taskqid_t id)
f1ca4da6 196{
937879f1 197 ENTRY;
bcd68186 198 ASSERT(tq);
199
200 wait_event(tq->tq_wait_waitq, taskq_wait_check(tq, id));
201
202 EXIT;
203}
204EXPORT_SYMBOL(__taskq_wait_id);
205
206void
207__taskq_wait(taskq_t *tq)
208{
209 taskqid_t id;
210 ENTRY;
211 ASSERT(tq);
212
7257ec41 213 /* Wait for the largest outstanding taskqid */
749045bb 214 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
7257ec41 215 id = tq->tq_next_id - 1;
749045bb 216 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 217
218 __taskq_wait_id(tq, id);
219
220 EXIT;
221
222}
223EXPORT_SYMBOL(__taskq_wait);
224
225int
226__taskq_member(taskq_t *tq, void *t)
227{
228 int i;
229 ENTRY;
230
231 ASSERT(tq);
232 ASSERT(t);
233
234 for (i = 0; i < tq->tq_nthreads; i++)
235 if (tq->tq_threads[i] == (struct task_struct *)t)
236 RETURN(1);
237
238 RETURN(0);
239}
240EXPORT_SYMBOL(__taskq_member);
241
242taskqid_t
243__taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
244{
3d061e9d 245 spl_task_t *t;
bcd68186 246 taskqid_t rc = 0;
247 ENTRY;
f1ca4da6 248
937879f1 249 ASSERT(tq);
250 ASSERT(func);
d05ec4b4
BB
251
252 /* Solaris assumes TQ_SLEEP if not passed explicitly */
253 if (!(flags & (TQ_SLEEP | TQ_NOSLEEP)))
254 flags |= TQ_SLEEP;
255
bcd68186 256 if (unlikely(in_atomic() && (flags & TQ_SLEEP))) {
257 CERROR("May schedule while atomic: %s/0x%08x/%d\n",
258 current->comm, preempt_count(), current->pid);
259 SBUG();
260 }
f1ca4da6 261
749045bb 262 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
f1ca4da6 263
bcd68186 264 /* Taskq being destroyed and all tasks drained */
265 if (!(tq->tq_flags & TQ_ACTIVE))
266 GOTO(out, rc = 0);
f1ca4da6 267
bcd68186 268 /* Do not queue the task unless there is idle thread for it */
269 ASSERT(tq->tq_nactive <= tq->tq_nthreads);
270 if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads))
271 GOTO(out, rc = 0);
272
273 if ((t = task_alloc(tq, flags)) == NULL)
274 GOTO(out, rc = 0);
f1ca4da6 275
bcd68186 276 spin_lock(&t->t_lock);
9ab1ac14 277 list_add_tail(&t->t_list, &tq->tq_pend_list);
bcd68186 278 t->t_id = rc = tq->tq_next_id;
279 tq->tq_next_id++;
280 t->t_func = func;
281 t->t_arg = arg;
282 spin_unlock(&t->t_lock);
283
284 wake_up(&tq->tq_work_waitq);
285out:
749045bb 286 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 287 RETURN(rc);
f1ca4da6 288}
f1b59d26 289EXPORT_SYMBOL(__taskq_dispatch);
f1ca4da6 290
82387586
BB
291/*
292 * Returns the lowest incomplete taskqid_t. The taskqid_t may
293 * be queued on the pending list or may be on the work list
294 * currently being handled, but it is not 100% complete yet.
295 */
bcd68186 296static taskqid_t
297taskq_lowest_id(taskq_t *tq)
298{
7257ec41 299 taskqid_t lowest_id = tq->tq_next_id;
3d061e9d 300 spl_task_t *t;
bcd68186 301 ENTRY;
302
303 ASSERT(tq);
304 ASSERT(spin_is_locked(&tq->tq_lock));
305
82387586
BB
306 if (!list_empty(&tq->tq_pend_list)) {
307 t = list_entry(tq->tq_pend_list.next, spl_task_t, t_list);
308 lowest_id = MIN(lowest_id, t->t_id);
309 }
bcd68186 310
82387586
BB
311 if (!list_empty(&tq->tq_work_list)) {
312 t = list_entry(tq->tq_work_list.next, spl_task_t, t_list);
313 lowest_id = MIN(lowest_id, t->t_id);
314 }
bcd68186 315
316 RETURN(lowest_id);
317}
318
319static int
320taskq_thread(void *args)
321{
322 DECLARE_WAITQUEUE(wait, current);
323 sigset_t blocked;
324 taskqid_t id;
325 taskq_t *tq = args;
3d061e9d 326 spl_task_t *t;
bcd68186 327 ENTRY;
328
329 ASSERT(tq);
330 current->flags |= PF_NOFREEZE;
331
332 sigfillset(&blocked);
333 sigprocmask(SIG_BLOCK, &blocked, NULL);
334 flush_signals(current);
335
749045bb 336 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 337 tq->tq_nthreads++;
338 wake_up(&tq->tq_wait_waitq);
339 set_current_state(TASK_INTERRUPTIBLE);
340
341 while (!kthread_should_stop()) {
342
343 add_wait_queue(&tq->tq_work_waitq, &wait);
344 if (list_empty(&tq->tq_pend_list)) {
749045bb 345 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 346 schedule();
749045bb 347 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 348 } else {
349 __set_current_state(TASK_RUNNING);
350 }
351
352 remove_wait_queue(&tq->tq_work_waitq, &wait);
353 if (!list_empty(&tq->tq_pend_list)) {
7257ec41 354 t = list_entry(tq->tq_pend_list.next,spl_task_t,t_list);
bcd68186 355 list_del_init(&t->t_list);
9ab1ac14 356 list_add_tail(&t->t_list, &tq->tq_work_list);
bcd68186 357 tq->tq_nactive++;
749045bb 358 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 359
360 /* Perform the requested task */
361 t->t_func(t->t_arg);
362
749045bb 363 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 364 tq->tq_nactive--;
365 id = t->t_id;
366 task_done(tq, t);
367
7257ec41
BB
368 /* When the current lowest outstanding taskqid is
369 * done calculate the new lowest outstanding id */
bcd68186 370 if (tq->tq_lowest_id == id) {
371 tq->tq_lowest_id = taskq_lowest_id(tq);
372 ASSERT(tq->tq_lowest_id > id);
373 }
374
375 wake_up_all(&tq->tq_wait_waitq);
376 }
377
378 set_current_state(TASK_INTERRUPTIBLE);
379
380 }
381
382 __set_current_state(TASK_RUNNING);
383 tq->tq_nthreads--;
749045bb 384 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 385
386 RETURN(0);
387}
388
f1ca4da6 389taskq_t *
390__taskq_create(const char *name, int nthreads, pri_t pri,
391 int minalloc, int maxalloc, uint_t flags)
392{
bcd68186 393 taskq_t *tq;
394 struct task_struct *t;
395 int rc = 0, i, j = 0;
396 ENTRY;
397
398 ASSERT(name != NULL);
399 ASSERT(pri <= maxclsyspri);
400 ASSERT(minalloc >= 0);
401 ASSERT(maxalloc <= INT_MAX);
402 ASSERT(!(flags & (TASKQ_CPR_SAFE | TASKQ_DYNAMIC))); /* Unsupported */
403
915404bd
BB
404 /* Scale the number of threads using nthreads as a percentage */
405 if (flags & TASKQ_THREADS_CPU_PCT) {
406 ASSERT(nthreads <= 100);
407 ASSERT(nthreads >= 0);
408 nthreads = MIN(nthreads, 100);
409 nthreads = MAX(nthreads, 0);
410 nthreads = MAX((num_online_cpus() * nthreads) / 100, 1);
411 }
412
bcd68186 413 tq = kmem_alloc(sizeof(*tq), KM_SLEEP);
414 if (tq == NULL)
415 RETURN(NULL);
416
417 tq->tq_threads = kmem_alloc(nthreads * sizeof(t), KM_SLEEP);
418 if (tq->tq_threads == NULL) {
419 kmem_free(tq, sizeof(*tq));
420 RETURN(NULL);
421 }
422
423 spin_lock_init(&tq->tq_lock);
749045bb 424 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 425 tq->tq_name = name;
426 tq->tq_nactive = 0;
427 tq->tq_nthreads = 0;
428 tq->tq_pri = pri;
429 tq->tq_minalloc = minalloc;
430 tq->tq_maxalloc = maxalloc;
431 tq->tq_nalloc = 0;
432 tq->tq_flags = (flags | TQ_ACTIVE);
433 tq->tq_next_id = 1;
434 tq->tq_lowest_id = 1;
435 INIT_LIST_HEAD(&tq->tq_free_list);
436 INIT_LIST_HEAD(&tq->tq_work_list);
437 INIT_LIST_HEAD(&tq->tq_pend_list);
438 init_waitqueue_head(&tq->tq_work_waitq);
439 init_waitqueue_head(&tq->tq_wait_waitq);
440
441 if (flags & TASKQ_PREPOPULATE)
442 for (i = 0; i < minalloc; i++)
443 task_done(tq, task_alloc(tq, TQ_SLEEP | TQ_NEW));
6e605b6e 444
749045bb 445 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
6e605b6e 446
bcd68186 447 for (i = 0; i < nthreads; i++) {
448 t = kthread_create(taskq_thread, tq, "%s/%d", name, i);
449 if (t) {
450 tq->tq_threads[i] = t;
451 kthread_bind(t, i % num_online_cpus());
452 set_user_nice(t, PRIO_TO_NICE(pri));
453 wake_up_process(t);
454 j++;
455 } else {
456 tq->tq_threads[i] = NULL;
457 rc = 1;
458 }
459 }
460
461 /* Wait for all threads to be started before potential destroy */
462 wait_event(tq->tq_wait_waitq, tq->tq_nthreads == j);
463
464 if (rc) {
465 __taskq_destroy(tq);
466 tq = NULL;
467 }
468
469 RETURN(tq);
f1ca4da6 470}
f1b59d26 471EXPORT_SYMBOL(__taskq_create);
b123971f 472
473void
474__taskq_destroy(taskq_t *tq)
475{
3d061e9d 476 spl_task_t *t;
bcd68186 477 int i, nthreads;
937879f1 478 ENTRY;
b123971f 479
bcd68186 480 ASSERT(tq);
749045bb 481 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 482 tq->tq_flags &= ~TQ_ACTIVE;
749045bb 483 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 484
485 /* TQ_ACTIVE cleared prevents new tasks being added to pending */
486 __taskq_wait(tq);
487
488 nthreads = tq->tq_nthreads;
489 for (i = 0; i < nthreads; i++)
490 if (tq->tq_threads[i])
491 kthread_stop(tq->tq_threads[i]);
492
749045bb 493 spin_lock_irqsave(&tq->tq_lock, tq->tq_lock_flags);
bcd68186 494
495 while (!list_empty(&tq->tq_free_list)) {
3d061e9d 496 t = list_entry(tq->tq_free_list.next, spl_task_t, t_list);
bcd68186 497 list_del_init(&t->t_list);
498 task_free(tq, t);
499 }
500
501 ASSERT(tq->tq_nthreads == 0);
502 ASSERT(tq->tq_nalloc == 0);
503 ASSERT(list_empty(&tq->tq_free_list));
504 ASSERT(list_empty(&tq->tq_work_list));
505 ASSERT(list_empty(&tq->tq_pend_list));
506
749045bb 507 spin_unlock_irqrestore(&tq->tq_lock, tq->tq_lock_flags);
3d061e9d 508 kmem_free(tq->tq_threads, nthreads * sizeof(spl_task_t *));
bcd68186 509 kmem_free(tq, sizeof(taskq_t));
510
937879f1 511 EXIT;
b123971f 512}
bcd68186 513EXPORT_SYMBOL(__taskq_destroy);
e9cb2b4f
BB
514
515int
516spl_taskq_init(void)
517{
518 ENTRY;
519
f220894e
BB
520 /* Solaris creates a dynamic taskq of up to 64 threads, however in
521 * a Linux environment 1 thread per-core is usually about right */
522 system_taskq = taskq_create("spl_system_taskq", num_online_cpus(),
523 minclsyspri, 4, 512, TASKQ_PREPOPULATE);
e9cb2b4f
BB
524 if (system_taskq == NULL)
525 RETURN(1);
526
527 RETURN(0);
528}
529
530void
531spl_taskq_fini(void)
532{
533 ENTRY;
534 taskq_destroy(system_taskq);
535 EXIT;
536}