2 * Copyright (c) 2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Copyright (c) 2012 Spectra Logic Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/epoch.h>
35 #include <sys/kernel.h>
38 #include <sys/mutex.h>
39 #include <sys/queue.h>
40 #include <sys/taskq.h>
41 #include <sys/taskqueue.h>
42 #include <sys/zfs_context.h>
44 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
45 #include <machine/pcb.h>
50 #if __FreeBSD_version < 1201522
51 #define taskqueue_start_threads_in_proc(tqp, count, pri, proc, name, ...) \
52 taskqueue_start_threads(tqp, count, pri, name, __VA_ARGS__)
55 static uint_t taskq_tsd;
56 static uma_zone_t taskq_zone;
58 taskq_t *system_taskq = NULL;
59 taskq_t *system_delay_taskq = NULL;
60 taskq_t *dynamic_taskq = NULL;
64 extern int uma_align_cache;
66 static MALLOC_DEFINE(M_TASKQ, "taskq", "taskq structures");
68 static CK_LIST_HEAD(tqenthashhead, taskq_ent) *tqenthashtbl;
69 static unsigned long tqenthash;
70 static unsigned long tqenthashlock;
71 static struct sx *tqenthashtbl_lock;
73 static taskqid_t tqidnext;
75 #define TQIDHASH(tqid) (&tqenthashtbl[(tqid) & tqenthash])
76 #define TQIDHASHLOCK(tqid) (&tqenthashtbl_lock[((tqid) & tqenthashlock)])
78 #define TIMEOUT_TASK 1
82 system_taskq_init(void *arg)
86 tsd_create(&taskq_tsd, NULL);
87 tqenthashtbl = hashinit(mp_ncpus * 8, M_TASKQ, &tqenthash);
88 tqenthashlock = (tqenthash + 1) / 8;
89 if (tqenthashlock > 0)
92 malloc(sizeof (*tqenthashtbl_lock) * (tqenthashlock + 1),
93 M_TASKQ, M_WAITOK | M_ZERO);
94 for (i = 0; i < tqenthashlock + 1; i++)
95 sx_init_flags(&tqenthashtbl_lock[i], "tqenthash", SX_DUPOK);
96 taskq_zone = uma_zcreate("taskq_zone", sizeof (taskq_ent_t),
97 NULL, NULL, NULL, NULL,
99 system_taskq = taskq_create("system_taskq", mp_ncpus, minclsyspri,
101 system_delay_taskq = taskq_create("system_delay_taskq", mp_ncpus,
102 minclsyspri, 0, 0, 0);
104 SYSINIT(system_taskq_init, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_init,
108 system_taskq_fini(void *arg)
112 taskq_destroy(system_delay_taskq);
113 taskq_destroy(system_taskq);
114 uma_zdestroy(taskq_zone);
115 tsd_destroy(&taskq_tsd);
116 for (i = 0; i < tqenthashlock + 1; i++)
117 sx_destroy(&tqenthashtbl_lock[i]);
118 for (i = 0; i < tqenthash + 1; i++)
119 VERIFY(CK_LIST_EMPTY(&tqenthashtbl[i]));
120 free(tqenthashtbl_lock, M_TASKQ);
121 free(tqenthashtbl, M_TASKQ);
123 SYSUNINIT(system_taskq_fini, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_fini,
133 * Assume a 64-bit counter will not wrap in practice.
135 tqid = atomic_add_64_nv(&tqidnext, 1);
146 tqid = atomic_add_32_nv(&tqidnext, 1);
147 if (__predict_true(tqid != 0))
156 taskq_lookup(taskqid_t tqid)
158 taskq_ent_t *ent = NULL;
160 sx_xlock(TQIDHASHLOCK(tqid));
161 CK_LIST_FOREACH(ent, TQIDHASH(tqid), tqent_hash) {
162 if (ent->tqent_id == tqid)
166 refcount_acquire(&ent->tqent_rc);
167 sx_xunlock(TQIDHASHLOCK(tqid));
172 taskq_insert(taskq_ent_t *ent)
176 tqid = __taskq_genid();
177 ent->tqent_id = tqid;
178 ent->tqent_registered = B_TRUE;
179 sx_xlock(TQIDHASHLOCK(tqid));
180 CK_LIST_INSERT_HEAD(TQIDHASH(tqid), ent, tqent_hash);
181 sx_xunlock(TQIDHASHLOCK(tqid));
186 taskq_remove(taskq_ent_t *ent)
188 taskqid_t tqid = ent->tqent_id;
190 if (!ent->tqent_registered)
193 sx_xlock(TQIDHASHLOCK(tqid));
194 CK_LIST_REMOVE(ent, tqent_hash);
195 sx_xunlock(TQIDHASHLOCK(tqid));
196 ent->tqent_registered = B_FALSE;
200 taskq_tsd_set(void *context)
202 taskq_t *tq = context;
204 #if defined(__amd64__) || defined(__i386__) || defined(__aarch64__)
205 if (context != NULL && tsd_get(taskq_tsd) == NULL)
206 fpu_kern_thread(FPU_KERN_NORMAL);
208 tsd_set(taskq_tsd, tq);
212 taskq_create_impl(const char *name, int nthreads, pri_t pri,
213 proc_t *proc __maybe_unused, uint_t flags)
217 if ((flags & TASKQ_THREADS_CPU_PCT) != 0)
218 nthreads = MAX((mp_ncpus * nthreads) / 100, 1);
220 tq = kmem_alloc(sizeof (*tq), KM_SLEEP);
221 tq->tq_queue = taskqueue_create(name, M_WAITOK,
222 taskqueue_thread_enqueue, &tq->tq_queue);
223 taskqueue_set_callback(tq->tq_queue, TASKQUEUE_CALLBACK_TYPE_INIT,
225 taskqueue_set_callback(tq->tq_queue, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN,
226 taskq_tsd_set, NULL);
227 (void) taskqueue_start_threads_in_proc(&tq->tq_queue, nthreads, pri,
230 return ((taskq_t *)tq);
234 taskq_create(const char *name, int nthreads, pri_t pri, int minalloc __unused,
235 int maxalloc __unused, uint_t flags)
237 return (taskq_create_impl(name, nthreads, pri, system_proc, flags));
241 taskq_create_proc(const char *name, int nthreads, pri_t pri,
242 int minalloc __unused, int maxalloc __unused, proc_t *proc, uint_t flags)
244 return (taskq_create_impl(name, nthreads, pri, proc, flags));
248 taskq_destroy(taskq_t *tq)
251 taskqueue_free(tq->tq_queue);
252 kmem_free(tq, sizeof (*tq));
256 taskq_member(taskq_t *tq, kthread_t *thread)
259 return (taskqueue_member(tq->tq_queue, thread));
263 taskq_of_curthread(void)
265 return (tsd_get(taskq_tsd));
269 taskq_free(taskq_ent_t *task)
272 if (refcount_release(&task->tqent_rc))
273 uma_zfree(taskq_zone, task);
277 taskq_cancel_id(taskq_t *tq, taskqid_t tid)
286 if ((ent = taskq_lookup(tid)) == NULL)
289 ent->tqent_cancelled = B_TRUE;
290 if (ent->tqent_type == TIMEOUT_TASK) {
291 rc = taskqueue_cancel_timeout(tq->tq_queue,
292 &ent->tqent_timeout_task, &pend);
294 rc = taskqueue_cancel(tq->tq_queue, &ent->tqent_task, &pend);
296 taskqueue_drain(tq->tq_queue, &ent->tqent_task);
299 * Tasks normally free themselves when run, but here the task
300 * was cancelled so it did not free itself.
304 /* Free the extra reference we added with taskq_lookup. */
310 taskq_run(void *arg, int pending __unused)
312 taskq_ent_t *task = arg;
314 if (!task->tqent_cancelled)
315 task->tqent_func(task->tqent_arg);
320 taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg,
321 uint_t flags, clock_t expire_time)
328 timo = expire_time - ddi_get_lbolt();
330 return (taskq_dispatch(tq, func, arg, flags));
332 if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP)
337 task = uma_zalloc(taskq_zone, mflag);
340 task->tqent_func = func;
341 task->tqent_arg = arg;
342 task->tqent_type = TIMEOUT_TASK;
343 task->tqent_cancelled = B_FALSE;
344 refcount_init(&task->tqent_rc, 1);
345 tqid = taskq_insert(task);
346 TIMEOUT_TASK_INIT(tq->tq_queue, &task->tqent_timeout_task, 0,
349 taskqueue_enqueue_timeout(tq->tq_queue, &task->tqent_timeout_task,
355 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
361 if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP)
366 * If TQ_FRONT is given, we want higher priority for this task, so it
367 * can go at the front of the queue.
369 prio = !!(flags & TQ_FRONT);
371 task = uma_zalloc(taskq_zone, mflag);
374 refcount_init(&task->tqent_rc, 1);
375 task->tqent_func = func;
376 task->tqent_arg = arg;
377 task->tqent_cancelled = B_FALSE;
378 task->tqent_type = NORMAL_TASK;
379 tqid = taskq_insert(task);
380 TASK_INIT(&task->tqent_task, prio, taskq_run, task);
381 taskqueue_enqueue(tq->tq_queue, &task->tqent_task);
386 taskq_run_ent(void *arg, int pending __unused)
388 taskq_ent_t *task = arg;
390 task->tqent_func(task->tqent_arg);
394 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint32_t flags,
400 * If TQ_FRONT is given, we want higher priority for this task, so it
401 * can go at the front of the queue.
403 prio = !!(flags & TQ_FRONT);
404 task->tqent_cancelled = B_FALSE;
405 task->tqent_registered = B_FALSE;
407 task->tqent_func = func;
408 task->tqent_arg = arg;
410 TASK_INIT(&task->tqent_task, prio, taskq_run_ent, task);
411 taskqueue_enqueue(tq->tq_queue, &task->tqent_task);
415 taskq_wait(taskq_t *tq)
417 taskqueue_quiesce(tq->tq_queue);
421 taskq_wait_id(taskq_t *tq, taskqid_t tid)
427 if ((ent = taskq_lookup(tid)) == NULL)
430 taskqueue_drain(tq->tq_queue, &ent->tqent_task);
435 taskq_wait_outstanding(taskq_t *tq, taskqid_t id __unused)
437 taskqueue_drain_all(tq->tq_queue);
441 taskq_empty_ent(taskq_ent_t *t)
443 return (t->tqent_task.ta_pending == 0);