]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/openzfs/module/os/freebsd/spl/spl_taskq.c
OpenZFS: MFV 2.0-rc3-gfc5966
[FreeBSD/FreeBSD.git] / sys / contrib / openzfs / module / os / freebsd / spl / spl_taskq.c
1 /*
2  * Copyright (c) 2009 Pawel Jakub Dawidek <pjd@FreeBSD.org>
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/types.h>
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/kmem.h>
36 #include <sys/lock.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>
42 #include <sys/ck.h>
43 #include <sys/epoch.h>
44
45 #include <vm/uma.h>
46
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__)
50 #endif
51
52 static uint_t taskq_tsd;
53 static uma_zone_t taskq_zone;
54
55 taskq_t *system_taskq = NULL;
56 taskq_t *system_delay_taskq = NULL;
57 taskq_t *dynamic_taskq = NULL;
58
59 proc_t *system_proc;
60
61 extern int uma_align_cache;
62
63 static MALLOC_DEFINE(M_TASKQ, "taskq", "taskq structures");
64
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;
69
70 static uint32_t tqidnext = 1;
71
72 #define TQIDHASH(tqid) (&tqenthashtbl[(tqid) & tqenthash])
73 #define TQIDHASHLOCK(tqid) (&tqenthashtbl_lock[((tqid) & tqenthashlock)])
74
75 #define TIMEOUT_TASK 1
76 #define NORMAL_TASK 2
77
78 static void
79 system_taskq_init(void *arg)
80 {
81         int i;
82
83         tsd_create(&taskq_tsd, NULL);
84         tqenthashtbl = hashinit(mp_ncpus * 8, M_TASKQ, &tqenthash);
85         tqenthashlock = (tqenthash + 1) / 8;
86         if (tqenthashlock > 0)
87                 tqenthashlock--;
88         tqenthashtbl_lock =
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);
93         tqidnext = 1;
94         taskq_zone = uma_zcreate("taskq_zone", sizeof (taskq_ent_t),
95             NULL, NULL, NULL, NULL,
96             UMA_ALIGN_CACHE, 0);
97         system_taskq = taskq_create("system_taskq", mp_ncpus, minclsyspri,
98             0, 0, 0);
99         system_delay_taskq = taskq_create("system_delay_taskq", mp_ncpus,
100             minclsyspri, 0, 0, 0);
101 }
102 SYSINIT(system_taskq_init, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_init,
103     NULL);
104
105 static void
106 system_taskq_fini(void *arg)
107 {
108         int i;
109
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);
120 }
121 SYSUNINIT(system_taskq_fini, SI_SUB_CONFIGURE, SI_ORDER_ANY, system_taskq_fini,
122     NULL);
123
124 static taskq_ent_t *
125 taskq_lookup(taskqid_t tqid)
126 {
127         taskq_ent_t *ent = NULL;
128
129         sx_xlock(TQIDHASHLOCK(tqid));
130         CK_LIST_FOREACH(ent, TQIDHASH(tqid), tqent_hash) {
131                 if (ent->tqent_id == tqid)
132                         break;
133         }
134         if (ent != NULL)
135                 refcount_acquire(&ent->tqent_rc);
136         sx_xunlock(TQIDHASHLOCK(tqid));
137         return (ent);
138 }
139
140 static taskqid_t
141 taskq_insert(taskq_ent_t *ent)
142 {
143         taskqid_t tqid = atomic_fetchadd_int(&tqidnext, 1);
144
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));
150         return (tqid);
151 }
152
153 static void
154 taskq_remove(taskq_ent_t *ent)
155 {
156         taskqid_t tqid = ent->tqent_id;
157
158         if (!ent->tqent_registered)
159                 return;
160
161         sx_xlock(TQIDHASHLOCK(tqid));
162         CK_LIST_REMOVE(ent, tqent_hash);
163         sx_xunlock(TQIDHASHLOCK(tqid));
164         ent->tqent_registered = B_FALSE;
165 }
166
167 static void
168 taskq_tsd_set(void *context)
169 {
170         taskq_t *tq = context;
171
172 #if defined(__amd64__) || defined(__i386__) || defined(__aarch64__)
173         if (context != NULL && tsd_get(taskq_tsd) == NULL)
174                 fpu_kern_thread(FPU_KERN_NORMAL);
175 #endif
176         tsd_set(taskq_tsd, tq);
177 }
178
179 static taskq_t *
180 taskq_create_impl(const char *name, int nthreads, pri_t pri,
181     proc_t *proc __maybe_unused, uint_t flags)
182 {
183         taskq_t *tq;
184
185         if ((flags & TASKQ_THREADS_CPU_PCT) != 0)
186                 nthreads = MAX((mp_ncpus * nthreads) / 100, 1);
187
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,
192             taskq_tsd_set, tq);
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,
196             proc, "%s", name);
197
198         return ((taskq_t *)tq);
199 }
200
201 taskq_t *
202 taskq_create(const char *name, int nthreads, pri_t pri, int minalloc __unused,
203     int maxalloc __unused, uint_t flags)
204 {
205         return (taskq_create_impl(name, nthreads, pri, system_proc, flags));
206 }
207
208 taskq_t *
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)
211 {
212         return (taskq_create_impl(name, nthreads, pri, proc, flags));
213 }
214
215 void
216 taskq_destroy(taskq_t *tq)
217 {
218
219         taskqueue_free(tq->tq_queue);
220         kmem_free(tq, sizeof (*tq));
221 }
222
223 int
224 taskq_member(taskq_t *tq, kthread_t *thread)
225 {
226
227         return (taskqueue_member(tq->tq_queue, thread));
228 }
229
230 taskq_t *
231 taskq_of_curthread(void)
232 {
233         return (tsd_get(taskq_tsd));
234 }
235
236 static void
237 taskq_free(taskq_ent_t *task)
238 {
239         taskq_remove(task);
240         if (refcount_release(&task->tqent_rc))
241                 uma_zfree(taskq_zone, task);
242 }
243
244 int
245 taskq_cancel_id(taskq_t *tq, taskqid_t tid)
246 {
247         uint32_t pend;
248         int rc;
249         taskq_ent_t *ent;
250
251         if (tid == 0)
252                 return (0);
253
254         if ((ent = taskq_lookup(tid)) == NULL)
255                 return (0);
256
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);
261         } else
262                 rc = taskqueue_cancel(tq->tq_queue, &ent->tqent_task, &pend);
263         if (rc == EBUSY) {
264                 taskqueue_drain(tq->tq_queue, &ent->tqent_task);
265         } else if (pend) {
266                 /*
267                  * Tasks normally free themselves when run, but here the task
268                  * was cancelled so it did not free itself.
269                  */
270                 taskq_free(ent);
271         }
272         /* Free the extra reference we added with taskq_lookup. */
273         taskq_free(ent);
274         return (rc);
275 }
276
277 static void
278 taskq_run(void *arg, int pending __unused)
279 {
280         taskq_ent_t *task = arg;
281
282         if (!task->tqent_cancelled)
283                 task->tqent_func(task->tqent_arg);
284         taskq_free(task);
285 }
286
287 taskqid_t
288 taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg,
289     uint_t flags, clock_t expire_time)
290 {
291         taskq_ent_t *task;
292         taskqid_t tid;
293         clock_t timo;
294         int mflag;
295
296         timo = expire_time - ddi_get_lbolt();
297         if (timo <= 0)
298                 return (taskq_dispatch(tq, func, arg, flags));
299
300         if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP)
301                 mflag = M_WAITOK;
302         else
303                 mflag = M_NOWAIT;
304
305         task = uma_zalloc(taskq_zone, mflag);
306         if (task == NULL)
307                 return (0);
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,
315             taskq_run, task);
316
317         taskqueue_enqueue_timeout(tq->tq_queue, &task->tqent_timeout_task,
318             timo);
319         return (tid);
320 }
321
322 taskqid_t
323 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
324 {
325         taskq_ent_t *task;
326         int mflag, prio;
327         taskqid_t tid;
328
329         if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP)
330                 mflag = M_WAITOK;
331         else
332                 mflag = M_NOWAIT;
333         /*
334          * If TQ_FRONT is given, we want higher priority for this task, so it
335          * can go at the front of the queue.
336          */
337         prio = !!(flags & TQ_FRONT);
338
339         task = uma_zalloc(taskq_zone, mflag);
340         if (task == NULL)
341                 return (0);
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);
350         VERIFY(tid);
351         return (tid);
352 }
353
354 static void
355 taskq_run_ent(void *arg, int pending __unused)
356 {
357         taskq_ent_t *task = arg;
358
359         task->tqent_func(task->tqent_arg);
360 }
361
362 void
363 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint32_t flags,
364     taskq_ent_t *task)
365 {
366         int prio;
367
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         task->tqent_cancelled = B_FALSE;
374         task->tqent_registered = B_FALSE;
375         task->tqent_id = 0;
376         task->tqent_func = func;
377         task->tqent_arg = arg;
378
379         TASK_INIT(&task->tqent_task, prio, taskq_run_ent, task);
380         taskqueue_enqueue(tq->tq_queue, &task->tqent_task);
381 }
382
383 void
384 taskq_wait(taskq_t *tq)
385 {
386         taskqueue_quiesce(tq->tq_queue);
387 }
388
389 void
390 taskq_wait_id(taskq_t *tq, taskqid_t tid)
391 {
392         taskq_ent_t *ent;
393
394         if (tid == 0)
395                 return;
396         if ((ent = taskq_lookup(tid)) == NULL)
397                 return;
398
399         taskqueue_drain(tq->tq_queue, &ent->tqent_task);
400         taskq_free(ent);
401 }
402
403 void
404 taskq_wait_outstanding(taskq_t *tq, taskqid_t id __unused)
405 {
406         taskqueue_drain_all(tq->tq_queue);
407 }
408
409 int
410 taskq_empty_ent(taskq_ent_t *t)
411 {
412         return (t->tqent_task.ta_pending == 0);
413 }