1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 /*
  26  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  27  * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
  28  * Copyright (c) 2014 by Delphix. All rights reserved.
  29  * Copyright (c) 2017, Joyent, Inc.
  30  */
  31 
  32 #include <sys/zfs_context.h>
  33 
  34 int taskq_now;
  35 taskq_t *system_taskq;
  36 
  37 #define TASKQ_ACTIVE    0x00010000
  38 #define TASKQ_NAMELEN   31
  39 
  40 struct taskq {
  41         char            tq_name[TASKQ_NAMELEN + 1];
  42         kmutex_t        tq_lock;
  43         krwlock_t       tq_threadlock;
  44         kcondvar_t      tq_dispatch_cv;
  45         kcondvar_t      tq_wait_cv;
  46         thread_t        *tq_threadlist;
  47         int             tq_flags;
  48         int             tq_active;
  49         int             tq_nthreads;
  50         int             tq_nalloc;
  51         int             tq_minalloc;
  52         int             tq_maxalloc;
  53         kcondvar_t      tq_maxalloc_cv;
  54         int             tq_maxalloc_wait;
  55         taskq_ent_t     *tq_freelist;
  56         taskq_ent_t     tq_task;
  57 };
  58 
  59 static taskq_ent_t *
  60 task_alloc(taskq_t *tq, int tqflags)
  61 {
  62         taskq_ent_t *t;
  63         int rv;
  64 
  65 again:  if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
  66                 tq->tq_freelist = t->tqent_next;
  67         } else {
  68                 if (tq->tq_nalloc >= tq->tq_maxalloc) {
  69                         if (!(tqflags & KM_SLEEP))
  70                                 return (NULL);
  71 
  72                         /*
  73                          * We don't want to exceed tq_maxalloc, but we can't
  74                          * wait for other tasks to complete (and thus free up
  75                          * task structures) without risking deadlock with
  76                          * the caller.  So, we just delay for one second
  77                          * to throttle the allocation rate. If we have tasks
  78                          * complete before one second timeout expires then
  79                          * taskq_ent_free will signal us and we will
  80                          * immediately retry the allocation.
  81                          */
  82                         tq->tq_maxalloc_wait++;
  83                         rv = cv_timedwait(&tq->tq_maxalloc_cv,
  84                             &tq->tq_lock, ddi_get_lbolt() + hz);
  85                         tq->tq_maxalloc_wait--;
  86                         if (rv > 0)
  87                                 goto again;             /* signaled */
  88                 }
  89                 mutex_exit(&tq->tq_lock);
  90 
  91                 t = kmem_alloc(sizeof (taskq_ent_t), tqflags);
  92 
  93                 mutex_enter(&tq->tq_lock);
  94                 if (t != NULL)
  95                         tq->tq_nalloc++;
  96         }
  97         return (t);
  98 }
  99 
 100 static void
 101 task_free(taskq_t *tq, taskq_ent_t *t)
 102 {
 103         if (tq->tq_nalloc <= tq->tq_minalloc) {
 104                 t->tqent_next = tq->tq_freelist;
 105                 tq->tq_freelist = t;
 106         } else {
 107                 tq->tq_nalloc--;
 108                 mutex_exit(&tq->tq_lock);
 109                 kmem_free(t, sizeof (taskq_ent_t));
 110                 mutex_enter(&tq->tq_lock);
 111         }
 112 
 113         if (tq->tq_maxalloc_wait)
 114                 cv_signal(&tq->tq_maxalloc_cv);
 115 }
 116 
 117 taskqid_t
 118 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
 119 {
 120         taskq_ent_t *t;
 121 
 122         if (taskq_now) {
 123                 func(arg);
 124                 return (1);
 125         }
 126 
 127         mutex_enter(&tq->tq_lock);
 128         ASSERT(tq->tq_flags & TASKQ_ACTIVE);
 129         if ((t = task_alloc(tq, tqflags)) == NULL) {
 130                 mutex_exit(&tq->tq_lock);
 131                 return (0);
 132         }
 133         if (tqflags & TQ_FRONT) {
 134                 t->tqent_next = tq->tq_task.tqent_next;
 135                 t->tqent_prev = &tq->tq_task;
 136         } else {
 137                 t->tqent_next = &tq->tq_task;
 138                 t->tqent_prev = tq->tq_task.tqent_prev;
 139         }
 140         t->tqent_next->tqent_prev = t;
 141         t->tqent_prev->tqent_next = t;
 142         t->tqent_func = func;
 143         t->tqent_arg = arg;
 144         t->tqent_flags = 0;
 145         cv_signal(&tq->tq_dispatch_cv);
 146         mutex_exit(&tq->tq_lock);
 147         return (1);
 148 }
 149 
 150 void
 151 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
 152     taskq_ent_t *t)
 153 {
 154         ASSERT(func != NULL);
 155         ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC));
 156 
 157         /*
 158          * Mark it as a prealloc'd task.  This is important
 159          * to ensure that we don't free it later.
 160          */
 161         t->tqent_flags |= TQENT_FLAG_PREALLOC;
 162         /*
 163          * Enqueue the task to the underlying queue.
 164          */
 165         mutex_enter(&tq->tq_lock);
 166 
 167         if (flags & TQ_FRONT) {
 168                 t->tqent_next = tq->tq_task.tqent_next;
 169                 t->tqent_prev = &tq->tq_task;
 170         } else {
 171                 t->tqent_next = &tq->tq_task;
 172                 t->tqent_prev = tq->tq_task.tqent_prev;
 173         }
 174         t->tqent_next->tqent_prev = t;
 175         t->tqent_prev->tqent_next = t;
 176         t->tqent_func = func;
 177         t->tqent_arg = arg;
 178         cv_signal(&tq->tq_dispatch_cv);
 179         mutex_exit(&tq->tq_lock);
 180 }
 181 
 182 boolean_t
 183 taskq_empty(taskq_t *tq)
 184 {
 185         boolean_t rv;
 186 
 187         mutex_enter(&tq->tq_lock);
 188         rv = (tq->tq_task.tqent_next == &tq->tq_task) && (tq->tq_active == 0);
 189         mutex_exit(&tq->tq_lock);
 190 
 191         return (rv);
 192 }
 193 
 194 void
 195 taskq_wait(taskq_t *tq)
 196 {
 197         mutex_enter(&tq->tq_lock);
 198         while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0)
 199                 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
 200         mutex_exit(&tq->tq_lock);
 201 }
 202 
 203 static void *
 204 taskq_thread(void *arg)
 205 {
 206         taskq_t *tq = arg;
 207         taskq_ent_t *t;
 208         boolean_t prealloc;
 209 
 210         mutex_enter(&tq->tq_lock);
 211         while (tq->tq_flags & TASKQ_ACTIVE) {
 212                 if ((t = tq->tq_task.tqent_next) == &tq->tq_task) {
 213                         if (--tq->tq_active == 0)
 214                                 cv_broadcast(&tq->tq_wait_cv);
 215                         cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
 216                         tq->tq_active++;
 217                         continue;
 218                 }
 219                 t->tqent_prev->tqent_next = t->tqent_next;
 220                 t->tqent_next->tqent_prev = t->tqent_prev;
 221                 t->tqent_next = NULL;
 222                 t->tqent_prev = NULL;
 223                 prealloc = t->tqent_flags & TQENT_FLAG_PREALLOC;
 224                 mutex_exit(&tq->tq_lock);
 225 
 226                 rw_enter(&tq->tq_threadlock, RW_READER);
 227                 t->tqent_func(t->tqent_arg);
 228                 rw_exit(&tq->tq_threadlock);
 229 
 230                 mutex_enter(&tq->tq_lock);
 231                 if (!prealloc)
 232                         task_free(tq, t);
 233         }
 234         tq->tq_nthreads--;
 235         cv_broadcast(&tq->tq_wait_cv);
 236         mutex_exit(&tq->tq_lock);
 237         return (NULL);
 238 }
 239 
 240 /*ARGSUSED*/
 241 taskq_t *
 242 taskq_create(const char *name, int nthreads, pri_t pri,
 243         int minalloc, int maxalloc, uint_t flags)
 244 {
 245         taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
 246         int t;
 247 
 248         if (flags & TASKQ_THREADS_CPU_PCT) {
 249                 int pct;
 250                 ASSERT3S(nthreads, >=, 0);
 251                 ASSERT3S(nthreads, <=, 100);
 252                 pct = MIN(nthreads, 100);
 253                 pct = MAX(pct, 0);
 254 
 255                 nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
 256                 nthreads = MAX(nthreads, 1);    /* need at least 1 thread */
 257         } else {
 258                 ASSERT3S(nthreads, >=, 1);
 259         }
 260 
 261         rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
 262         mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
 263         cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
 264         cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
 265         cv_init(&tq->tq_maxalloc_cv, NULL, CV_DEFAULT, NULL);
 266         (void) strncpy(tq->tq_name, name, TASKQ_NAMELEN + 1);
 267         tq->tq_flags = flags | TASKQ_ACTIVE;
 268         tq->tq_active = nthreads;
 269         tq->tq_nthreads = nthreads;
 270         tq->tq_minalloc = minalloc;
 271         tq->tq_maxalloc = maxalloc;
 272         tq->tq_task.tqent_next = &tq->tq_task;
 273         tq->tq_task.tqent_prev = &tq->tq_task;
 274         tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
 275 
 276         if (flags & TASKQ_PREPOPULATE) {
 277                 mutex_enter(&tq->tq_lock);
 278                 while (minalloc-- > 0)
 279                         task_free(tq, task_alloc(tq, KM_SLEEP));
 280                 mutex_exit(&tq->tq_lock);
 281         }
 282 
 283         for (t = 0; t < nthreads; t++)
 284                 (void) thr_create(0, 0, taskq_thread,
 285                     tq, THR_BOUND, &tq->tq_threadlist[t]);
 286 
 287         return (tq);
 288 }
 289 
 290 void
 291 taskq_destroy(taskq_t *tq)
 292 {
 293         int t;
 294         int nthreads = tq->tq_nthreads;
 295 
 296         taskq_wait(tq);
 297 
 298         mutex_enter(&tq->tq_lock);
 299 
 300         tq->tq_flags &= ~TASKQ_ACTIVE;
 301         cv_broadcast(&tq->tq_dispatch_cv);
 302 
 303         while (tq->tq_nthreads != 0)
 304                 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
 305 
 306         tq->tq_minalloc = 0;
 307         while (tq->tq_nalloc != 0) {
 308                 ASSERT(tq->tq_freelist != NULL);
 309                 task_free(tq, task_alloc(tq, KM_SLEEP));
 310         }
 311 
 312         mutex_exit(&tq->tq_lock);
 313 
 314         for (t = 0; t < nthreads; t++)
 315                 (void) thr_join(tq->tq_threadlist[t], NULL, NULL);
 316 
 317         kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
 318 
 319         rw_destroy(&tq->tq_threadlock);
 320         mutex_destroy(&tq->tq_lock);
 321         cv_destroy(&tq->tq_dispatch_cv);
 322         cv_destroy(&tq->tq_wait_cv);
 323         cv_destroy(&tq->tq_maxalloc_cv);
 324 
 325         kmem_free(tq, sizeof (taskq_t));
 326 }
 327 
 328 int
 329 taskq_member(taskq_t *tq, void *t)
 330 {
 331         int i;
 332 
 333         if (taskq_now)
 334                 return (1);
 335 
 336         for (i = 0; i < tq->tq_nthreads; i++)
 337                 if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
 338                         return (1);
 339 
 340         return (0);
 341 }
 342 
 343 void
 344 system_taskq_init(void)
 345 {
 346         system_taskq = taskq_create("system_taskq", 64, minclsyspri, 4, 512,
 347             TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
 348 }
 349 
 350 void
 351 system_taskq_fini(void)
 352 {
 353         taskq_destroy(system_taskq);
 354         system_taskq = NULL; /* defensive */
 355 }