]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/openzfs/module/os/freebsd/spl/spl_taskq.c
Update nvi to 2.2.0-05ed8b9
[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         tsd_set(taskq_tsd, tq);
173 }
174
175 static taskq_t *
176 taskq_create_impl(const char *name, int nthreads, pri_t pri,
177     proc_t *proc __maybe_unused, uint_t flags)
178 {
179         taskq_t *tq;
180
181         if ((flags & TASKQ_THREADS_CPU_PCT) != 0)
182                 nthreads = MAX((mp_ncpus * nthreads) / 100, 1);
183
184         tq = kmem_alloc(sizeof (*tq), KM_SLEEP);
185         tq->tq_queue = taskqueue_create(name, M_WAITOK,
186             taskqueue_thread_enqueue, &tq->tq_queue);
187         taskqueue_set_callback(tq->tq_queue, TASKQUEUE_CALLBACK_TYPE_INIT,
188             taskq_tsd_set, tq);
189         taskqueue_set_callback(tq->tq_queue, TASKQUEUE_CALLBACK_TYPE_SHUTDOWN,
190             taskq_tsd_set, NULL);
191         (void) taskqueue_start_threads_in_proc(&tq->tq_queue, nthreads, pri,
192             proc, "%s", name);
193
194         return ((taskq_t *)tq);
195 }
196
197 taskq_t *
198 taskq_create(const char *name, int nthreads, pri_t pri, int minalloc __unused,
199     int maxalloc __unused, uint_t flags)
200 {
201         return (taskq_create_impl(name, nthreads, pri, system_proc, flags));
202 }
203
204 taskq_t *
205 taskq_create_proc(const char *name, int nthreads, pri_t pri,
206     int minalloc __unused, int maxalloc __unused, proc_t *proc, uint_t flags)
207 {
208         return (taskq_create_impl(name, nthreads, pri, proc, flags));
209 }
210
211 void
212 taskq_destroy(taskq_t *tq)
213 {
214
215         taskqueue_free(tq->tq_queue);
216         kmem_free(tq, sizeof (*tq));
217 }
218
219 int
220 taskq_member(taskq_t *tq, kthread_t *thread)
221 {
222
223         return (taskqueue_member(tq->tq_queue, thread));
224 }
225
226 taskq_t *
227 taskq_of_curthread(void)
228 {
229         return (tsd_get(taskq_tsd));
230 }
231
232 static void
233 taskq_free(taskq_ent_t *task)
234 {
235         taskq_remove(task);
236         if (refcount_release(&task->tqent_rc))
237                 uma_zfree(taskq_zone, task);
238 }
239
240 int
241 taskq_cancel_id(taskq_t *tq, taskqid_t tid)
242 {
243         uint32_t pend;
244         int rc;
245         taskq_ent_t *ent;
246
247         if (tid == 0)
248                 return (0);
249
250         if ((ent = taskq_lookup(tid)) == NULL)
251                 return (0);
252
253         ent->tqent_cancelled = B_TRUE;
254         if (ent->tqent_type == TIMEOUT_TASK) {
255                 rc = taskqueue_cancel_timeout(tq->tq_queue,
256                     &ent->tqent_timeout_task, &pend);
257         } else
258                 rc = taskqueue_cancel(tq->tq_queue, &ent->tqent_task, &pend);
259         if (rc == EBUSY) {
260                 taskqueue_drain(tq->tq_queue, &ent->tqent_task);
261         } else if (pend) {
262                 /*
263                  * Tasks normally free themselves when run, but here the task
264                  * was cancelled so it did not free itself.
265                  */
266                 taskq_free(ent);
267         }
268         /* Free the extra reference we added with taskq_lookup. */
269         taskq_free(ent);
270         return (rc);
271 }
272
273 static void
274 taskq_run(void *arg, int pending __unused)
275 {
276         taskq_ent_t *task = arg;
277
278         if (!task->tqent_cancelled)
279                 task->tqent_func(task->tqent_arg);
280         taskq_free(task);
281 }
282
283 taskqid_t
284 taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg,
285     uint_t flags, clock_t expire_time)
286 {
287         taskq_ent_t *task;
288         taskqid_t tid;
289         clock_t timo;
290         int mflag;
291
292         timo = expire_time - ddi_get_lbolt();
293         if (timo <= 0)
294                 return (taskq_dispatch(tq, func, arg, flags));
295
296         if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP)
297                 mflag = M_WAITOK;
298         else
299                 mflag = M_NOWAIT;
300
301         task = uma_zalloc(taskq_zone, mflag);
302         if (task == NULL)
303                 return (0);
304         task->tqent_func = func;
305         task->tqent_arg = arg;
306         task->tqent_type = TIMEOUT_TASK;
307         task->tqent_cancelled = B_FALSE;
308         refcount_init(&task->tqent_rc, 1);
309         tid = taskq_insert(task);
310         TIMEOUT_TASK_INIT(tq->tq_queue, &task->tqent_timeout_task, 0,
311             taskq_run, task);
312
313         taskqueue_enqueue_timeout(tq->tq_queue, &task->tqent_timeout_task,
314             timo);
315         return (tid);
316 }
317
318 taskqid_t
319 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
320 {
321         taskq_ent_t *task;
322         int mflag, prio;
323         taskqid_t tid;
324
325         if ((flags & (TQ_SLEEP | TQ_NOQUEUE)) == TQ_SLEEP)
326                 mflag = M_WAITOK;
327         else
328                 mflag = M_NOWAIT;
329         /*
330          * If TQ_FRONT is given, we want higher priority for this task, so it
331          * can go at the front of the queue.
332          */
333         prio = !!(flags & TQ_FRONT);
334
335         task = uma_zalloc(taskq_zone, mflag);
336         if (task == NULL)
337                 return (0);
338         refcount_init(&task->tqent_rc, 1);
339         task->tqent_func = func;
340         task->tqent_arg = arg;
341         task->tqent_cancelled = B_FALSE;
342         task->tqent_type = NORMAL_TASK;
343         tid = taskq_insert(task);
344         TASK_INIT(&task->tqent_task, prio, taskq_run, task);
345         taskqueue_enqueue(tq->tq_queue, &task->tqent_task);
346         VERIFY(tid);
347         return (tid);
348 }
349
350 static void
351 taskq_run_ent(void *arg, int pending __unused)
352 {
353         taskq_ent_t *task = arg;
354
355         task->tqent_func(task->tqent_arg);
356 }
357
358 void
359 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint32_t flags,
360     taskq_ent_t *task)
361 {
362         int prio;
363
364         /*
365          * If TQ_FRONT is given, we want higher priority for this task, so it
366          * can go at the front of the queue.
367          */
368         prio = !!(flags & TQ_FRONT);
369         task->tqent_cancelled = B_FALSE;
370         task->tqent_registered = B_FALSE;
371         task->tqent_id = 0;
372         task->tqent_func = func;
373         task->tqent_arg = arg;
374
375         TASK_INIT(&task->tqent_task, prio, taskq_run_ent, task);
376         taskqueue_enqueue(tq->tq_queue, &task->tqent_task);
377 }
378
379 void
380 taskq_wait(taskq_t *tq)
381 {
382         taskqueue_quiesce(tq->tq_queue);
383 }
384
385 void
386 taskq_wait_id(taskq_t *tq, taskqid_t tid)
387 {
388         taskq_ent_t *ent;
389
390         if (tid == 0)
391                 return;
392         if ((ent = taskq_lookup(tid)) == NULL)
393                 return;
394
395         taskqueue_drain(tq->tq_queue, &ent->tqent_task);
396         taskq_free(ent);
397 }
398
399 void
400 taskq_wait_outstanding(taskq_t *tq, taskqid_t id __unused)
401 {
402         taskqueue_drain_all(tq->tq_queue);
403 }
404
405 int
406 taskq_empty_ent(taskq_ent_t *t)
407 {
408         return (t->tqent_task.ta_pending == 0);
409 }