1 /*
2  * Copyright (c) 2009 Pawel Jakub Dawidek <[email protected]>
3  * All rights reserved.
4  *
5  * Copyright (c) 2012 Spectra Logic Corporation.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/kmem.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/queue.h>
38 #include <sys/taskq.h>
39 #include <sys/taskqueue.h>
40 #include <sys/zfs_context.h>
41 
42 #if defined(__i386__) || defined(__amd64__) || defined(__aarch64__)
43 #include <machine/pcb.h>
44 #endif
45 
46 #include <vm/uma.h>
47 
48 #if __FreeBSD_version < 1201522
49 #define	taskqueue_start_threads_in_proc(tqp, count, pri, proc, name, ...) \
50     taskqueue_start_threads(tqp, count, pri, name, __VA_ARGS__)
51 #endif
52 
53 static uint_t taskq_tsd;
54 static uma_zone_t taskq_zone;
55 
56 /*
57  * Global system-wide dynamic task queue available for all consumers. This
58  * taskq is not intended for long-running tasks; instead, a dedicated taskq
59  * should be created.
60  */
61 taskq_t *system_taskq = NULL;
62 taskq_t *system_delay_taskq = NULL;
63 taskq_t *dynamic_taskq = NULL;
64 
65 proc_t *system_proc;
66 
67 static MALLOC_DEFINE(M_TASKQ, "taskq", "taskq structures");
68 
69 static LIST_HEAD(tqenthashhead, taskq_ent) *tqenthashtbl;
70 static unsigned long tqenthash;
71 static unsigned long tqenthashlock;
72 static struct sx *tqenthashtbl_lock;
73 
74 static taskqid_t tqidnext;
75 
76 #define	TQIDHASH(tqid) (&tqenthashtbl[(tqid) & tqenthash])
77 #define	TQIDHASHLOCK(tqid) (&tqenthashtbl_lock[((tqid) & tqenthashlock)])
78 
79 #define	NORMAL_TASK 0
80 #define	TIMEOUT_TASK 1
81 
82 static void
system_taskq_init(void * arg)83 system_taskq_init(void *arg)
84 {
85 	int i;
86 
87 	tsd_create(&taskq_tsd, NULL);
88 	tqenthashtbl = hashinit(mp_ncpus * 8, M_TASKQ, &tqenthash);
89 	tqenthashlock = (tqenthash + 1) / 8;
90 	if (tqenthashlock > 0)
91 		tqenthashlock--;
92 	tqenthashtbl_lock =
93 	    malloc(sizeof (*tqenthashtbl_lock) * (tqenthashlock + 1),
94 	    M_TASKQ, M_WAITOK | M_ZERO);
95 	for (i = 0; i < tqenthashlock + 1; i++)
96 		sx_init_flags(&tqenthashtbl_lock[i], "tqenthash", SX_DUPOK);
97 	taskq_zone = uma_zcreate("taskq_zone", sizeof (taskq_ent_t),
98 	    NULL, NULL, NULL, NULL,
99 	    UMA_ALIGN_CACHE, 0);
100 	system_taskq = taskq_create("system_taskq", mp_ncpus, minclsyspri,
101 	    0, 0, 0);
102 	system_delay_taskq = taskq_create("system_delay_taskq", mp_ncpus,
103 	    minclsyspri, 0, 0, 0);
104 }
105 SYSINIT(system_taskq_init, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_init,
106     NULL);
107 
108 static void
system_taskq_fini(void * arg)109 system_taskq_fini(void *arg)
110 {
111 	int i;
112 
113 	taskq_destroy(system_delay_taskq);
114 	taskq_destroy(system_taskq);
115 	uma_zdestroy(taskq_zone);
116 	tsd_destroy(&taskq_tsd);
117 	for (i = 0; i < tqenthashlock + 1; i++)
118 		sx_destroy(&tqenthashtbl_lock[i]);
119 	for (i = 0; i < tqenthash + 1; i++)
120 		VERIFY(LIST_EMPTY(&tqenthashtbl[i]));
121 	free(tqenthashtbl_lock, M_TASKQ);
122 	free(tqenthashtbl, M_TASKQ);
123 }
124 SYSUNINIT(system_taskq_fini, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_fini,
125     NULL);
126 
127 #ifdef __LP64__
128 static taskqid_t
__taskq_genid(void)129 __taskq_genid(void)
130 {
131 	taskqid_t tqid;
132 
133 	/*
134 	 * Assume a 64-bit counter will not wrap in practice.
135 	 */
136 	tqid = atomic_add_64_nv(&tqidnext, 1);
137 	VERIFY(tqid);
138 	return (tqid);
139 }
140 #else
141 static taskqid_t
__taskq_genid(void)142 __taskq_genid(void)
143 {
144 	taskqid_t tqid;
145 
146 	for (;;) {
147 		tqid = atomic_add_32_nv(&tqidnext, 1);
148 		if (__predict_true(tqid != 0))
149 			break;
150 	}
151 	VERIFY(tqid);
152 	return (tqid);
153 }
154 #endif
155 
156 static taskq_ent_t *
taskq_lookup(taskqid_t tqid)157 taskq_lookup(taskqid_t tqid)
158 {
159 	taskq_ent_t *ent = NULL;
160 
161 	if (tqid == 0)
162 		return (NULL);
163 	sx_slock(TQIDHASHLOCK(tqid));
164 	LIST_FOREACH(ent, TQIDHASH(tqid), tqent_hash) {
165 		if (ent->tqent_id == tqid)
166 			break;
167 	}
168 	if (ent != NULL)
169 		refcount_acquire(&ent->tqent_rc);
170 	sx_sunlock(TQIDHASHLOCK(tqid));
171 	return (ent);
172 }
173 
174 static taskqid_t
taskq_insert(taskq_ent_t * ent)175 taskq_insert(taskq_ent_t *ent)
176 {
177 	taskqid_t tqid = __taskq_genid();
178 
179 	ent->tqent_id = tqid;
180 	sx_xlock(TQIDHASHLOCK(tqid));
181 	LIST_INSERT_HEAD(TQIDHASH(tqid), ent, tqent_hash);
182 	sx_xunlock(TQIDHASHLOCK(tqid));
183 	return (tqid);
184 }
185 
186 static void
taskq_remove(taskq_ent_t * ent)187 taskq_remove(taskq_ent_t *ent)
188 {
189 	taskqid_t tqid = ent->tqent_id;
190 
191 	if (tqid == 0)
192 		return;
193 	sx_xlock(TQIDHASHLOCK(tqid));
194 	if (ent->tqent_id != 0) {
195 		LIST_REMOVE(ent, tqent_hash);
196 		ent->tqent_id = 0;
197 	}
198 	sx_xunlock(TQIDHASHLOCK(tqid));
199 }
200 
201 static void
taskq_tsd_set(void * context)202 taskq_tsd_set(void *context)
203 {
204 	taskq_t *tq = context;
205 
206 #if defined(__amd64__) || defined(__aarch64__)
207 	if (context != NULL && tsd_get(taskq_tsd) == NULL)
208 		fpu_kern_thread(FPU_KERN_NORMAL);
209 #endif
210 	tsd_set(taskq_tsd, tq);
211 }
212 
213 static taskq_t *
taskq_create_impl(const char * name,int nthreads,pri_t pri,proc_t * proc __maybe_unused,uint_t flags)214 taskq_create_impl(const char *name, int nthreads, pri_t pri,
215     proc_t *proc __maybe_unused, uint_t flags)
216 {
217 	taskq_t *tq;
218 
219 	if ((flags & TASKQ_THREADS_CPU_PCT) != 0)
220 		nthreads = MAX((mp_ncpus * nthreads) / 100, 1);
221 
222 	tq = kmem_alloc(sizeof (*tq), KM_SLEEP);
223 	tq->tq_queue = taskqueue_create(name, M_WAITOK,
224 	    taskqueue_thread_enqueue, &tq->tq_queue);
225 	taskqueue_set_callback(tq->tq_queue, TASKQUEUE_CALLBACK_TYPE_INIT,
226 	    taskq_tsd_set, tq);
227 	taskqueue_set_callback(tq->tq_queue, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN,
228 	    taskq_tsd_set, NULL);
229 	(void) taskqueue_start_threads_in_proc(&tq->tq_queue, nthreads, pri,
230 	    proc, "%s", name);
231 
232 	return ((taskq_t *)tq);
233 }
234 
235 taskq_t *
taskq_create(const char * name,int nthreads,pri_t pri,int minalloc __unused,int maxalloc __unused,uint_t flags)236 taskq_create(const char *name, int nthreads, pri_t pri, int minalloc __unused,
237     int maxalloc __unused, uint_t flags)
238 {
239 	return (taskq_create_impl(name, nthreads, pri, system_proc, flags));
240 }
241 
242 taskq_t *
taskq_create_proc(const char * name,int nthreads,pri_t pri,int minalloc __unused,int maxalloc __unused,proc_t * proc,uint_t flags)243 taskq_create_proc(const char *name, int nthreads, pri_t pri,
244     int minalloc __unused, int maxalloc __unused, proc_t *proc, uint_t flags)
245 {
246 	return (taskq_create_impl(name, nthreads, pri, proc, flags));
247 }
248 
249 void
taskq_destroy(taskq_t * tq)250 taskq_destroy(taskq_t *tq)
251 {
252 
253 	taskqueue_free(tq->tq_queue);
254 	kmem_free(tq, sizeof (*tq));
255 }
256 
257 int
taskq_member(taskq_t * tq,kthread_t * thread)258 taskq_member(taskq_t *tq, kthread_t *thread)
259 {
260 
261 	return (taskqueue_member(tq->tq_queue, thread));
262 }
263 
264 taskq_t *
taskq_of_curthread(void)265 taskq_of_curthread(void)
266 {
267 	return (tsd_get(taskq_tsd));
268 }
269 
270 static void
taskq_free(taskq_ent_t * task)271 taskq_free(taskq_ent_t *task)
272 {
273 	taskq_remove(task);
274 	if (refcount_release(&task->tqent_rc))
275 		uma_zfree(taskq_zone, task);
276 }
277 
278 int
taskq_cancel_id(taskq_t * tq,taskqid_t tid)279 taskq_cancel_id(taskq_t *tq, taskqid_t tid)
280 {
281 	uint32_t pend;
282 	int rc;
283 	taskq_ent_t *ent;
284 
285 	if ((ent = taskq_lookup(tid)) == NULL)
286 		return (0);
287 
288 	if (ent->tqent_type == NORMAL_TASK) {
289 		rc = taskqueue_cancel(tq->tq_queue, &ent->tqent_task, &pend);
290 		if (rc == EBUSY)
291 			taskqueue_drain(tq->tq_queue, &ent->tqent_task);
292 	} else {
293 		rc = taskqueue_cancel_timeout(tq->tq_queue,
294 		    &ent->tqent_timeout_task, &pend);
295 		if (rc == EBUSY) {
296 			taskqueue_drain_timeout(tq->tq_queue,
297 			    &ent->tqent_timeout_task);
298 		}
299 	}
300 	if (pend) {
301 		/*
302 		 * Tasks normally free themselves when run, but here the task
303 		 * was cancelled so it did not free itself.
304 		 */
305 		taskq_free(ent);
306 	}
307 	/* Free the extra reference we added with taskq_lookup. */
308 	taskq_free(ent);
309 	return (rc);
310 }
311 
312 static void
taskq_run(void * arg,int pending)313 taskq_run(void *arg, int pending)
314 {
315 	taskq_ent_t *task = arg;
316 
317 	if (pending == 0)
318 		return;
319 	task->tqent_func(task->tqent_arg);
320 	taskq_free(task);
321 }
322 
323 taskqid_t
taskq_dispatch_delay(taskq_t * tq,task_func_t func,void * arg,uint_t flags,clock_t expire_time)324 taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg,
325     uint_t flags, clock_t expire_time)
326 {
327 	taskq_ent_t *task;
328 	taskqid_t tqid;
329 	clock_t timo;
330 	int mflag;
331 
332 	timo = expire_time - ddi_get_lbolt();
333 	if (timo <= 0)
334 		return (taskq_dispatch(tq, func, arg, flags));
335 
336 	if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP)
337 		mflag = M_WAITOK;
338 	else
339 		mflag = M_NOWAIT;
340 
341 	task = uma_zalloc(taskq_zone, mflag);
342 	if (task == NULL)
343 		return (0);
344 	task->tqent_func = func;
345 	task->tqent_arg = arg;
346 	task->tqent_type = TIMEOUT_TASK;
347 	refcount_init(&task->tqent_rc, 1);
348 	tqid = taskq_insert(task);
349 	TIMEOUT_TASK_INIT(tq->tq_queue, &task->tqent_timeout_task, 0,
350 	    taskq_run, task);
351 
352 	taskqueue_enqueue_timeout(tq->tq_queue, &task->tqent_timeout_task,
353 	    timo);
354 	return (tqid);
355 }
356 
357 taskqid_t
taskq_dispatch(taskq_t * tq,task_func_t func,void * arg,uint_t flags)358 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
359 {
360 	taskq_ent_t *task;
361 	int mflag, prio;
362 	taskqid_t tqid;
363 
364 	if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP)
365 		mflag = M_WAITOK;
366 	else
367 		mflag = M_NOWAIT;
368 	/*
369 	 * If TQ_FRONT is given, we want higher priority for this task, so it
370 	 * can go at the front of the queue.
371 	 */
372 	prio = !!(flags & TQ_FRONT);
373 
374 	task = uma_zalloc(taskq_zone, mflag);
375 	if (task == NULL)
376 		return (0);
377 	refcount_init(&task->tqent_rc, 1);
378 	task->tqent_func = func;
379 	task->tqent_arg = arg;
380 	task->tqent_type = NORMAL_TASK;
381 	tqid = taskq_insert(task);
382 	TASK_INIT(&task->tqent_task, prio, taskq_run, task);
383 	taskqueue_enqueue(tq->tq_queue, &task->tqent_task);
384 	return (tqid);
385 }
386 
387 static void
taskq_run_ent(void * arg,int pending)388 taskq_run_ent(void *arg, int pending)
389 {
390 	taskq_ent_t *task = arg;
391 
392 	if (pending == 0)
393 		return;
394 	task->tqent_func(task->tqent_arg);
395 }
396 
397 void
taskq_dispatch_ent(taskq_t * tq,task_func_t func,void * arg,uint32_t flags,taskq_ent_t * task)398 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint32_t flags,
399     taskq_ent_t *task)
400 {
401 	int prio;
402 
403 	/*
404 	 * If TQ_FRONT is given, we want higher priority for this task, so it
405 	 * can go at the front of the queue.
406 	 */
407 	prio = !!(flags & TQ_FRONT);
408 	task->tqent_id = 0;
409 	task->tqent_func = func;
410 	task->tqent_arg = arg;
411 
412 	TASK_INIT(&task->tqent_task, prio, taskq_run_ent, task);
413 	taskqueue_enqueue(tq->tq_queue, &task->tqent_task);
414 }
415 
416 void
taskq_wait(taskq_t * tq)417 taskq_wait(taskq_t *tq)
418 {
419 	taskqueue_quiesce(tq->tq_queue);
420 }
421 
422 void
taskq_wait_id(taskq_t * tq,taskqid_t tid)423 taskq_wait_id(taskq_t *tq, taskqid_t tid)
424 {
425 	taskq_ent_t *ent;
426 
427 	if ((ent = taskq_lookup(tid)) == NULL)
428 		return;
429 
430 	if (ent->tqent_type == NORMAL_TASK)
431 		taskqueue_drain(tq->tq_queue, &ent->tqent_task);
432 	else
433 		taskqueue_drain_timeout(tq->tq_queue, &ent->tqent_timeout_task);
434 	taskq_free(ent);
435 }
436 
437 void
taskq_wait_outstanding(taskq_t * tq,taskqid_t id __unused)438 taskq_wait_outstanding(taskq_t *tq, taskqid_t id __unused)
439 {
440 	taskqueue_drain_all(tq->tq_queue);
441 }
442 
443 int
taskq_empty_ent(taskq_ent_t * t)444 taskq_empty_ent(taskq_ent_t *t)
445 {
446 	return (t->tqent_task.ta_pending == 0);
447 }
448