1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
14 * Use is subject to license terms.
15 */
16 /*
17 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
18 * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved.
19 * Copyright (c) 2014 by Delphix. All rights reserved.
20 */
21
22 #include <sys/sysmacros.h>
23 #include <sys/timer.h>
24 #include <sys/types.h>
25 #include <sys/thread.h>
26 #include <sys/taskq.h>
27 #include <sys/kmem.h>
28 #include <pthread.h>
29
30 static pthread_key_t taskq_tsd;
31 static pthread_once_t taskq_tsd_once = PTHREAD_ONCE_INIT;
32
33 static taskq_t *__system_taskq = NULL;
34 static taskq_t *__system_delay_taskq = NULL;
35
36 taskq_t
_system_taskq(void)37 *_system_taskq(void)
38 {
39 return (__system_taskq);
40 }
41
42 taskq_t
_system_delay_taskq(void)43 *_system_delay_taskq(void)
44 {
45 return (__system_delay_taskq);
46 }
47
48 #define TASKQ_ACTIVE 0x00010000
49
50 static taskq_ent_t *
task_alloc(taskq_t * tq,int tqflags)51 task_alloc(taskq_t *tq, int tqflags)
52 {
53 taskq_ent_t *t;
54 int rv;
55
56 again: if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
57 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
58 tq->tq_freelist = t->tqent_next;
59 } else {
60 if (tq->tq_nalloc >= tq->tq_maxalloc) {
61 if (!(tqflags & KM_SLEEP))
62 return (NULL);
63
64 /*
65 * We don't want to exceed tq_maxalloc, but we can't
66 * wait for other tasks to complete (and thus free up
67 * task structures) without risking deadlock with
68 * the caller. So, we just delay for one second
69 * to throttle the allocation rate. If we have tasks
70 * complete before one second timeout expires then
71 * taskq_ent_free will signal us and we will
72 * immediately retry the allocation.
73 */
74 tq->tq_maxalloc_wait++;
75 rv = cv_timedwait(&tq->tq_maxalloc_cv,
76 &tq->tq_lock, ddi_get_lbolt() + hz);
77 tq->tq_maxalloc_wait--;
78 if (rv > 0)
79 goto again; /* signaled */
80 }
81 mutex_exit(&tq->tq_lock);
82
83 t = kmem_alloc(sizeof (taskq_ent_t), tqflags);
84
85 mutex_enter(&tq->tq_lock);
86 if (t != NULL) {
87 /* Make sure we start without any flags */
88 t->tqent_flags = 0;
89 tq->tq_nalloc++;
90 }
91 }
92 return (t);
93 }
94
95 static void
task_free(taskq_t * tq,taskq_ent_t * t)96 task_free(taskq_t *tq, taskq_ent_t *t)
97 {
98 if (tq->tq_nalloc <= tq->tq_minalloc) {
99 t->tqent_next = tq->tq_freelist;
100 tq->tq_freelist = t;
101 } else {
102 tq->tq_nalloc--;
103 mutex_exit(&tq->tq_lock);
104 kmem_free(t, sizeof (taskq_ent_t));
105 mutex_enter(&tq->tq_lock);
106 }
107
108 if (tq->tq_maxalloc_wait)
109 cv_signal(&tq->tq_maxalloc_cv);
110 }
111
112 taskqid_t
taskq_dispatch(taskq_t * tq,task_func_t func,void * arg,uint_t tqflags)113 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
114 {
115 taskq_ent_t *t;
116
117 mutex_enter(&tq->tq_lock);
118 ASSERT(tq->tq_flags & TASKQ_ACTIVE);
119 if ((t = task_alloc(tq, tqflags)) == NULL) {
120 mutex_exit(&tq->tq_lock);
121 return (0);
122 }
123 if (tqflags & TQ_FRONT) {
124 t->tqent_next = tq->tq_task.tqent_next;
125 t->tqent_prev = &tq->tq_task;
126 } else {
127 t->tqent_next = &tq->tq_task;
128 t->tqent_prev = tq->tq_task.tqent_prev;
129 }
130 t->tqent_next->tqent_prev = t;
131 t->tqent_prev->tqent_next = t;
132 t->tqent_func = func;
133 t->tqent_arg = arg;
134 t->tqent_flags = 0;
135 cv_signal(&tq->tq_dispatch_cv);
136 mutex_exit(&tq->tq_lock);
137 return (1);
138 }
139
140 taskqid_t
taskq_dispatch_delay(taskq_t * tq,task_func_t func,void * arg,uint_t tqflags,clock_t expire_time)141 taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags,
142 clock_t expire_time)
143 {
144 (void) tq, (void) func, (void) arg, (void) tqflags, (void) expire_time;
145 return (0);
146 }
147
148 int
taskq_empty_ent(taskq_ent_t * t)149 taskq_empty_ent(taskq_ent_t *t)
150 {
151 return (t->tqent_next == NULL);
152 }
153
154 void
taskq_init_ent(taskq_ent_t * t)155 taskq_init_ent(taskq_ent_t *t)
156 {
157 t->tqent_next = NULL;
158 t->tqent_prev = NULL;
159 t->tqent_func = NULL;
160 t->tqent_arg = NULL;
161 t->tqent_flags = 0;
162 }
163
164 void
taskq_dispatch_ent(taskq_t * tq,task_func_t func,void * arg,uint_t flags,taskq_ent_t * t)165 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
166 taskq_ent_t *t)
167 {
168 ASSERT(func != NULL);
169
170 /*
171 * Mark it as a prealloc'd task. This is important
172 * to ensure that we don't free it later.
173 */
174 t->tqent_flags |= TQENT_FLAG_PREALLOC;
175 /*
176 * Enqueue the task to the underlying queue.
177 */
178 mutex_enter(&tq->tq_lock);
179
180 if (flags & TQ_FRONT) {
181 t->tqent_next = tq->tq_task.tqent_next;
182 t->tqent_prev = &tq->tq_task;
183 } else {
184 t->tqent_next = &tq->tq_task;
185 t->tqent_prev = tq->tq_task.tqent_prev;
186 }
187 t->tqent_next->tqent_prev = t;
188 t->tqent_prev->tqent_next = t;
189 t->tqent_func = func;
190 t->tqent_arg = arg;
191 cv_signal(&tq->tq_dispatch_cv);
192 mutex_exit(&tq->tq_lock);
193 }
194
195 void
taskq_wait(taskq_t * tq)196 taskq_wait(taskq_t *tq)
197 {
198 mutex_enter(&tq->tq_lock);
199 while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0)
200 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
201 mutex_exit(&tq->tq_lock);
202 }
203
204 void
taskq_wait_id(taskq_t * tq,taskqid_t id)205 taskq_wait_id(taskq_t *tq, taskqid_t id)
206 {
207 (void) id;
208 taskq_wait(tq);
209 }
210
211 void
taskq_wait_outstanding(taskq_t * tq,taskqid_t id)212 taskq_wait_outstanding(taskq_t *tq, taskqid_t id)
213 {
214 (void) id;
215 taskq_wait(tq);
216 }
217
218 static void
taskq_tsd_init(void)219 taskq_tsd_init(void)
220 {
221 VERIFY0(pthread_key_create(&taskq_tsd, NULL));
222 }
223
224 static __attribute__((noreturn)) void
taskq_thread(void * arg)225 taskq_thread(void *arg)
226 {
227 taskq_t *tq = arg;
228 taskq_ent_t *t;
229 boolean_t prealloc;
230
231 pthread_once(&taskq_tsd_once, taskq_tsd_init);
232 VERIFY0(pthread_setspecific(taskq_tsd, tq));
233
234 mutex_enter(&tq->tq_lock);
235 while (tq->tq_flags & TASKQ_ACTIVE) {
236 if ((t = tq->tq_task.tqent_next) == &tq->tq_task) {
237 if (--tq->tq_active == 0)
238 cv_broadcast(&tq->tq_wait_cv);
239 cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
240 tq->tq_active++;
241 continue;
242 }
243 t->tqent_prev->tqent_next = t->tqent_next;
244 t->tqent_next->tqent_prev = t->tqent_prev;
245 t->tqent_next = NULL;
246 t->tqent_prev = NULL;
247 prealloc = t->tqent_flags & TQENT_FLAG_PREALLOC;
248 mutex_exit(&tq->tq_lock);
249
250 rw_enter(&tq->tq_threadlock, RW_READER);
251 t->tqent_func(t->tqent_arg);
252 rw_exit(&tq->tq_threadlock);
253
254 mutex_enter(&tq->tq_lock);
255 if (!prealloc)
256 task_free(tq, t);
257 }
258 tq->tq_nthreads--;
259 cv_broadcast(&tq->tq_wait_cv);
260 mutex_exit(&tq->tq_lock);
261 thread_exit();
262 }
263
264 taskq_t *
taskq_create(const char * name,int nthreads,pri_t pri,int minalloc,int maxalloc,uint_t flags)265 taskq_create(const char *name, int nthreads, pri_t pri,
266 int minalloc, int maxalloc, uint_t flags)
267 {
268 (void) pri;
269 taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
270 int t;
271
272 if (flags & TASKQ_THREADS_CPU_PCT) {
273 int pct;
274 ASSERT3S(nthreads, >=, 0);
275 ASSERT3S(nthreads, <=, 100);
276 pct = MIN(nthreads, 100);
277 pct = MAX(pct, 0);
278
279 nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
280 nthreads = MAX(nthreads, 1); /* need at least 1 thread */
281 } else {
282 ASSERT3S(nthreads, >=, 1);
283 }
284
285 rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
286 mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
287 cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
288 cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
289 cv_init(&tq->tq_maxalloc_cv, NULL, CV_DEFAULT, NULL);
290 (void) strlcpy(tq->tq_name, name, sizeof (tq->tq_name));
291 tq->tq_flags = flags | TASKQ_ACTIVE;
292 tq->tq_active = nthreads;
293 tq->tq_nthreads = nthreads;
294 tq->tq_minalloc = minalloc;
295 tq->tq_maxalloc = maxalloc;
296 tq->tq_task.tqent_next = &tq->tq_task;
297 tq->tq_task.tqent_prev = &tq->tq_task;
298 tq->tq_threadlist = kmem_alloc(nthreads * sizeof (kthread_t *),
299 KM_SLEEP);
300
301 if (flags & TASKQ_PREPOPULATE) {
302 mutex_enter(&tq->tq_lock);
303 while (minalloc-- > 0)
304 task_free(tq, task_alloc(tq, KM_SLEEP));
305 mutex_exit(&tq->tq_lock);
306 }
307
308 for (t = 0; t < nthreads; t++)
309 VERIFY((tq->tq_threadlist[t] = thread_create_named(tq->tq_name,
310 NULL, 0, taskq_thread, tq, 0, &p0, TS_RUN, pri)) != NULL);
311
312 return (tq);
313 }
314
315 void
taskq_destroy(taskq_t * tq)316 taskq_destroy(taskq_t *tq)
317 {
318 int nthreads = tq->tq_nthreads;
319
320 taskq_wait(tq);
321
322 mutex_enter(&tq->tq_lock);
323
324 tq->tq_flags &= ~TASKQ_ACTIVE;
325 cv_broadcast(&tq->tq_dispatch_cv);
326
327 while (tq->tq_nthreads != 0)
328 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
329
330 tq->tq_minalloc = 0;
331 while (tq->tq_nalloc != 0) {
332 ASSERT(tq->tq_freelist != NULL);
333 taskq_ent_t *tqent_nexttq = tq->tq_freelist->tqent_next;
334 task_free(tq, tq->tq_freelist);
335 tq->tq_freelist = tqent_nexttq;
336 }
337
338 mutex_exit(&tq->tq_lock);
339
340 kmem_free(tq->tq_threadlist, nthreads * sizeof (kthread_t *));
341
342 rw_destroy(&tq->tq_threadlock);
343 mutex_destroy(&tq->tq_lock);
344 cv_destroy(&tq->tq_dispatch_cv);
345 cv_destroy(&tq->tq_wait_cv);
346 cv_destroy(&tq->tq_maxalloc_cv);
347
348 kmem_free(tq, sizeof (taskq_t));
349 }
350
351 /*
352 * Create a taskq with a specified number of pool threads. Allocate
353 * and return an array of nthreads kthread_t pointers, one for each
354 * thread in the pool. The array is not ordered and must be freed
355 * by the caller.
356 */
357 taskq_t *
taskq_create_synced(const char * name,int nthreads,pri_t pri,int minalloc,int maxalloc,uint_t flags,kthread_t *** ktpp)358 taskq_create_synced(const char *name, int nthreads, pri_t pri,
359 int minalloc, int maxalloc, uint_t flags, kthread_t ***ktpp)
360 {
361 taskq_t *tq;
362 kthread_t **kthreads = kmem_zalloc(sizeof (*kthreads) * nthreads,
363 KM_SLEEP);
364
365 (void) pri; (void) minalloc; (void) maxalloc;
366
367 flags &= ~(TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT | TASKQ_DC_BATCH);
368
369 tq = taskq_create(name, nthreads, minclsyspri, nthreads, INT_MAX,
370 flags | TASKQ_PREPOPULATE);
371 VERIFY(tq != NULL);
372 VERIFY(tq->tq_nthreads == nthreads);
373
374 for (int i = 0; i < nthreads; i++) {
375 kthreads[i] = tq->tq_threadlist[i];
376 }
377 *ktpp = kthreads;
378 return (tq);
379 }
380
381 int
taskq_member(taskq_t * tq,kthread_t * t)382 taskq_member(taskq_t *tq, kthread_t *t)
383 {
384 int i;
385
386 for (i = 0; i < tq->tq_nthreads; i++)
387 if (tq->tq_threadlist[i] == t)
388 return (1);
389
390 return (0);
391 }
392
393 taskq_t *
taskq_of_curthread(void)394 taskq_of_curthread(void)
395 {
396 return (pthread_getspecific(taskq_tsd));
397 }
398
399 int
taskq_cancel_id(taskq_t * tq,taskqid_t id,boolean_t wait)400 taskq_cancel_id(taskq_t *tq, taskqid_t id, boolean_t wait)
401 {
402 (void) tq, (void) id, (void) wait;
403 return (ENOENT);
404 }
405
406 void
system_taskq_init(void)407 system_taskq_init(void)
408 {
409 __system_taskq = taskq_create("system_taskq", 64, maxclsyspri, 4, 512,
410 TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
411 __system_delay_taskq = taskq_create("delay_taskq", 4, maxclsyspri, 4,
412 512, TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
413 }
414
415 void
system_taskq_fini(void)416 system_taskq_fini(void)
417 {
418 taskq_destroy(__system_taskq);
419 __system_taskq = NULL; /* defensive */
420 taskq_destroy(__system_delay_taskq);
421 __system_delay_taskq = NULL;
422 }
423