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/types.h>
33 #include <sys/param.h>
34 #include <sys/kernel.h>
37 #include <sys/mutex.h>
38 #include <sys/queue.h>
39 #include <sys/taskqueue.h>
40 #include <sys/taskq.h>
41 #include <sys/zfs_context.h>
43 #include <sys/epoch.h>
47 #if __FreeBSD_version < 1201522
48 #define taskqueue_start_threads_in_proc(tqp, count, pri, proc, name, ...) \
49 taskqueue_start_threads(tqp, count, pri, name, __VA_ARGS__)
52 static uint_t taskq_tsd;
53 static uma_zone_t taskq_zone;
55 taskq_t *system_taskq = NULL;
56 taskq_t *system_delay_taskq = NULL;
57 taskq_t *dynamic_taskq = NULL;
61 extern int uma_align_cache;
63 static MALLOC_DEFINE(M_TASKQ, "taskq", "taskq structures");
65 static CK_LIST_HEAD(tqenthashhead, taskq_ent) *tqenthashtbl;
66 static unsigned long tqenthash;
67 static unsigned long tqenthashlock;
68 static struct sx *tqenthashtbl_lock;
70 static uint32_t tqidnext = 1;
72 #define TQIDHASH(tqid) (&tqenthashtbl[(tqid) & tqenthash])
73 #define TQIDHASHLOCK(tqid) (&tqenthashtbl_lock[((tqid) & tqenthashlock)])
75 #define TIMEOUT_TASK 1
79 system_taskq_init(void *arg)
83 tsd_create(&taskq_tsd, NULL);
84 tqenthashtbl = hashinit(mp_ncpus * 8, M_TASKQ, &tqenthash);
85 tqenthashlock = (tqenthash + 1) / 8;
86 if (tqenthashlock > 0)
89 malloc(sizeof (*tqenthashtbl_lock) * (tqenthashlock + 1),
90 M_TASKQ, M_WAITOK | M_ZERO);
91 for (i = 0; i < tqenthashlock + 1; i++)
92 sx_init_flags(&tqenthashtbl_lock[i], "tqenthash", SX_DUPOK);
94 taskq_zone = uma_zcreate("taskq_zone", sizeof (taskq_ent_t),
95 NULL, NULL, NULL, NULL,
97 system_taskq = taskq_create("system_taskq", mp_ncpus, minclsyspri,
99 system_delay_taskq = taskq_create("system_delay_taskq", mp_ncpus,
100 minclsyspri, 0, 0, 0);
102 SYSINIT(system_taskq_init, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_init,
106 system_taskq_fini(void *arg)
110 taskq_destroy(system_delay_taskq);
111 taskq_destroy(system_taskq);
112 uma_zdestroy(taskq_zone);
113 tsd_destroy(&taskq_tsd);
114 for (i = 0; i < tqenthashlock + 1; i++)
115 sx_destroy(&tqenthashtbl_lock[i]);
116 for (i = 0; i < tqenthash + 1; i++)
117 VERIFY(CK_LIST_EMPTY(&tqenthashtbl[i]));
118 free(tqenthashtbl_lock, M_TASKQ);
119 free(tqenthashtbl, M_TASKQ);
121 SYSUNINIT(system_taskq_fini, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_fini,
125 taskq_lookup(taskqid_t tqid)
127 taskq_ent_t *ent = NULL;
129 sx_xlock(TQIDHASHLOCK(tqid));
130 CK_LIST_FOREACH(ent, TQIDHASH(tqid), tqent_hash) {
131 if (ent->tqent_id == tqid)
135 refcount_acquire(&ent->tqent_rc);
136 sx_xunlock(TQIDHASHLOCK(tqid));
141 taskq_insert(taskq_ent_t *ent)
143 taskqid_t tqid = atomic_fetchadd_int(&tqidnext, 1);
145 ent->tqent_id = tqid;
146 ent->tqent_registered = B_TRUE;
147 sx_xlock(TQIDHASHLOCK(tqid));
148 CK_LIST_INSERT_HEAD(TQIDHASH(tqid), ent, tqent_hash);
149 sx_xunlock(TQIDHASHLOCK(tqid));
154 taskq_remove(taskq_ent_t *ent)
156 taskqid_t tqid = ent->tqent_id;
158 if (!ent->tqent_registered)
161 sx_xlock(TQIDHASHLOCK(tqid));
162 CK_LIST_REMOVE(ent, tqent_hash);
163 sx_xunlock(TQIDHASHLOCK(tqid));
164 ent->tqent_registered = B_FALSE;
168 taskq_tsd_set(void *context)
170 taskq_t *tq = context;
172 #if defined(__amd64__) || defined(__aarch64__)
173 if (context != NULL && tsd_get(taskq_tsd) == NULL)
174 fpu_kern_thread(FPU_KERN_NORMAL);
176 tsd_set(taskq_tsd, tq);
180 taskq_create_impl(const char *name, int nthreads, pri_t pri,
181 proc_t *proc __maybe_unused, uint_t flags)
185 if ((flags & TASKQ_THREADS_CPU_PCT) != 0)
186 nthreads = MAX((mp_ncpus * nthreads) / 100, 1);
188 tq = kmem_alloc(sizeof (*tq), KM_SLEEP);
189 tq->tq_queue = taskqueue_create(name, M_WAITOK,
190 taskqueue_thread_enqueue, &tq->tq_queue);
191 taskqueue_set_callback(tq->tq_queue, TASKQUEUE_CALLBACK_TYPE_INIT,
193 taskqueue_set_callback(tq->tq_queue, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN,
194 taskq_tsd_set, NULL);
195 (void) taskqueue_start_threads_in_proc(&tq->tq_queue, nthreads, pri,
198 return ((taskq_t *)tq);
202 taskq_create(const char *name, int nthreads, pri_t pri, int minalloc __unused,
203 int maxalloc __unused, uint_t flags)
205 return (taskq_create_impl(name, nthreads, pri, system_proc, flags));
209 taskq_create_proc(const char *name, int nthreads, pri_t pri,
210 int minalloc __unused, int maxalloc __unused, proc_t *proc, uint_t flags)
212 return (taskq_create_impl(name, nthreads, pri, proc, flags));
216 taskq_destroy(taskq_t *tq)
219 taskqueue_free(tq->tq_queue);
220 kmem_free(tq, sizeof (*tq));
224 taskq_member(taskq_t *tq, kthread_t *thread)
227 return (taskqueue_member(tq->tq_queue, thread));
231 taskq_of_curthread(void)
233 return (tsd_get(taskq_tsd));
237 taskq_free(taskq_ent_t *task)
240 if (refcount_release(&task->tqent_rc))
241 uma_zfree(taskq_zone, task);
245 taskq_cancel_id(taskq_t *tq, taskqid_t tid)
254 if ((ent = taskq_lookup(tid)) == NULL)
257 ent->tqent_cancelled = B_TRUE;
258 if (ent->tqent_type == TIMEOUT_TASK) {
259 rc = taskqueue_cancel_timeout(tq->tq_queue,
260 &ent->tqent_timeout_task, &pend);
262 rc = taskqueue_cancel(tq->tq_queue, &ent->tqent_task, &pend);
264 taskqueue_drain(tq->tq_queue, &ent->tqent_task);
267 * Tasks normally free themselves when run, but here the task
268 * was cancelled so it did not free itself.
272 /* Free the extra reference we added with taskq_lookup. */
278 taskq_run(void *arg, int pending __unused)
280 taskq_ent_t *task = arg;
282 if (!task->tqent_cancelled)
283 task->tqent_func(task->tqent_arg);
288 taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg,
289 uint_t flags, clock_t expire_time)
296 timo = expire_time - ddi_get_lbolt();
298 return (taskq_dispatch(tq, func, arg, flags));
300 if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP)
305 task = uma_zalloc(taskq_zone, mflag);
308 task->tqent_func = func;
309 task->tqent_arg = arg;
310 task->tqent_type = TIMEOUT_TASK;
311 task->tqent_cancelled = B_FALSE;
312 refcount_init(&task->tqent_rc, 1);
313 tid = taskq_insert(task);
314 TIMEOUT_TASK_INIT(tq->tq_queue, &task->tqent_timeout_task, 0,
317 taskqueue_enqueue_timeout(tq->tq_queue, &task->tqent_timeout_task,
323 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
329 if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP)
334 * If TQ_FRONT is given, we want higher priority for this task, so it
335 * can go at the front of the queue.
337 prio = !!(flags & TQ_FRONT);
339 task = uma_zalloc(taskq_zone, mflag);
342 refcount_init(&task->tqent_rc, 1);
343 task->tqent_func = func;
344 task->tqent_arg = arg;
345 task->tqent_cancelled = B_FALSE;
346 task->tqent_type = NORMAL_TASK;
347 tid = taskq_insert(task);
348 TASK_INIT(&task->tqent_task, prio, taskq_run, task);
349 taskqueue_enqueue(tq->tq_queue, &task->tqent_task);
355 taskq_run_ent(void *arg, int pending __unused)
357 taskq_ent_t *task = arg;
359 task->tqent_func(task->tqent_arg);
363 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint32_t flags,
369 * If TQ_FRONT is given, we want higher priority for this task, so it
370 * can go at the front of the queue.
372 prio = !!(flags & TQ_FRONT);
373 task->tqent_cancelled = B_FALSE;
374 task->tqent_registered = B_FALSE;
376 task->tqent_func = func;
377 task->tqent_arg = arg;
379 TASK_INIT(&task->tqent_task, prio, taskq_run_ent, task);
380 taskqueue_enqueue(tq->tq_queue, &task->tqent_task);
384 taskq_wait(taskq_t *tq)
386 taskqueue_quiesce(tq->tq_queue);
390 taskq_wait_id(taskq_t *tq, taskqid_t tid)
396 if ((ent = taskq_lookup(tid)) == NULL)
399 taskqueue_drain(tq->tq_queue, &ent->tqent_task);
404 taskq_wait_outstanding(taskq_t *tq, taskqid_t id __unused)
406 taskqueue_drain_all(tq->tq_queue);
410 taskq_empty_ent(taskq_ent_t *t)
412 return (t->tqent_task.ta_pending == 0);