1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC. 3eda14cbcSMatt Macy * Copyright (C) 2007 The Regents of the University of California. 4eda14cbcSMatt Macy * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). 5eda14cbcSMatt Macy * Written by Brian Behlendorf <behlendorf1@llnl.gov>. 6eda14cbcSMatt Macy * UCRL-CODE-235197 7eda14cbcSMatt Macy * 8eda14cbcSMatt Macy * This file is part of the SPL, Solaris Porting Layer. 9eda14cbcSMatt Macy * 10eda14cbcSMatt Macy * The SPL is free software; you can redistribute it and/or modify it 11eda14cbcSMatt Macy * under the terms of the GNU General Public License as published by the 12eda14cbcSMatt Macy * Free Software Foundation; either version 2 of the License, or (at your 13eda14cbcSMatt Macy * option) any later version. 14eda14cbcSMatt Macy * 15eda14cbcSMatt Macy * The SPL is distributed in the hope that it will be useful, but WITHOUT 16eda14cbcSMatt Macy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 17eda14cbcSMatt Macy * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 18eda14cbcSMatt Macy * for more details. 19eda14cbcSMatt Macy * 20eda14cbcSMatt Macy * You should have received a copy of the GNU General Public License along 21eda14cbcSMatt Macy * with the SPL. If not, see <http://www.gnu.org/licenses/>. 22eda14cbcSMatt Macy * 23eda14cbcSMatt Macy * Solaris Porting Layer (SPL) Task Queue Implementation. 24eda14cbcSMatt Macy */ 25eda14cbcSMatt Macy 26eda14cbcSMatt Macy #include <sys/timer.h> 27eda14cbcSMatt Macy #include <sys/taskq.h> 28eda14cbcSMatt Macy #include <sys/kmem.h> 29eda14cbcSMatt Macy #include <sys/tsd.h> 30eda14cbcSMatt Macy #include <sys/trace_spl.h> 317877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG 327877fdebSMatt Macy #include <linux/cpuhotplug.h> 337877fdebSMatt Macy #endif 34eda14cbcSMatt Macy 35eda14cbcSMatt Macy int spl_taskq_thread_bind = 0; 36eda14cbcSMatt Macy module_param(spl_taskq_thread_bind, int, 0644); 37eda14cbcSMatt Macy MODULE_PARM_DESC(spl_taskq_thread_bind, "Bind taskq thread to CPU by default"); 38eda14cbcSMatt Macy 39eda14cbcSMatt Macy 40eda14cbcSMatt Macy int spl_taskq_thread_dynamic = 1; 417877fdebSMatt Macy module_param(spl_taskq_thread_dynamic, int, 0444); 42eda14cbcSMatt Macy MODULE_PARM_DESC(spl_taskq_thread_dynamic, "Allow dynamic taskq threads"); 43eda14cbcSMatt Macy 44eda14cbcSMatt Macy int spl_taskq_thread_priority = 1; 45eda14cbcSMatt Macy module_param(spl_taskq_thread_priority, int, 0644); 46eda14cbcSMatt Macy MODULE_PARM_DESC(spl_taskq_thread_priority, 47eda14cbcSMatt Macy "Allow non-default priority for taskq threads"); 48eda14cbcSMatt Macy 49eda14cbcSMatt Macy int spl_taskq_thread_sequential = 4; 50eda14cbcSMatt Macy module_param(spl_taskq_thread_sequential, int, 0644); 51eda14cbcSMatt Macy MODULE_PARM_DESC(spl_taskq_thread_sequential, 52eda14cbcSMatt Macy "Create new taskq threads after N sequential tasks"); 53eda14cbcSMatt Macy 54eda14cbcSMatt Macy /* Global system-wide dynamic task queue available for all consumers */ 55eda14cbcSMatt Macy taskq_t *system_taskq; 56eda14cbcSMatt Macy EXPORT_SYMBOL(system_taskq); 57eda14cbcSMatt Macy /* Global dynamic task queue for long delay */ 58eda14cbcSMatt Macy taskq_t *system_delay_taskq; 59eda14cbcSMatt Macy EXPORT_SYMBOL(system_delay_taskq); 60eda14cbcSMatt Macy 61eda14cbcSMatt Macy /* Private dedicated taskq for creating new taskq threads on demand. */ 62eda14cbcSMatt Macy static taskq_t *dynamic_taskq; 63eda14cbcSMatt Macy static taskq_thread_t *taskq_thread_create(taskq_t *); 64eda14cbcSMatt Macy 657877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG 667877fdebSMatt Macy /* Multi-callback id for cpu hotplugging. */ 677877fdebSMatt Macy static int spl_taskq_cpuhp_state; 687877fdebSMatt Macy #endif 697877fdebSMatt Macy 70eda14cbcSMatt Macy /* List of all taskqs */ 71eda14cbcSMatt Macy LIST_HEAD(tq_list); 72eda14cbcSMatt Macy struct rw_semaphore tq_list_sem; 73eda14cbcSMatt Macy static uint_t taskq_tsd; 74eda14cbcSMatt Macy 75eda14cbcSMatt Macy static int 76eda14cbcSMatt Macy task_km_flags(uint_t flags) 77eda14cbcSMatt Macy { 78eda14cbcSMatt Macy if (flags & TQ_NOSLEEP) 79eda14cbcSMatt Macy return (KM_NOSLEEP); 80eda14cbcSMatt Macy 81eda14cbcSMatt Macy if (flags & TQ_PUSHPAGE) 82eda14cbcSMatt Macy return (KM_PUSHPAGE); 83eda14cbcSMatt Macy 84eda14cbcSMatt Macy return (KM_SLEEP); 85eda14cbcSMatt Macy } 86eda14cbcSMatt Macy 87eda14cbcSMatt Macy /* 88eda14cbcSMatt Macy * taskq_find_by_name - Find the largest instance number of a named taskq. 89eda14cbcSMatt Macy */ 90eda14cbcSMatt Macy static int 91eda14cbcSMatt Macy taskq_find_by_name(const char *name) 92eda14cbcSMatt Macy { 93eda14cbcSMatt Macy struct list_head *tql = NULL; 94eda14cbcSMatt Macy taskq_t *tq; 95eda14cbcSMatt Macy 96eda14cbcSMatt Macy list_for_each_prev(tql, &tq_list) { 97eda14cbcSMatt Macy tq = list_entry(tql, taskq_t, tq_taskqs); 98eda14cbcSMatt Macy if (strcmp(name, tq->tq_name) == 0) 99eda14cbcSMatt Macy return (tq->tq_instance); 100eda14cbcSMatt Macy } 101eda14cbcSMatt Macy return (-1); 102eda14cbcSMatt Macy } 103eda14cbcSMatt Macy 104eda14cbcSMatt Macy /* 105eda14cbcSMatt Macy * NOTE: Must be called with tq->tq_lock held, returns a list_t which 106eda14cbcSMatt Macy * is not attached to the free, work, or pending taskq lists. 107eda14cbcSMatt Macy */ 108eda14cbcSMatt Macy static taskq_ent_t * 109eda14cbcSMatt Macy task_alloc(taskq_t *tq, uint_t flags, unsigned long *irqflags) 110eda14cbcSMatt Macy { 111eda14cbcSMatt Macy taskq_ent_t *t; 112eda14cbcSMatt Macy int count = 0; 113eda14cbcSMatt Macy 114eda14cbcSMatt Macy ASSERT(tq); 115eda14cbcSMatt Macy retry: 116eda14cbcSMatt Macy /* Acquire taskq_ent_t's from free list if available */ 117eda14cbcSMatt Macy if (!list_empty(&tq->tq_free_list) && !(flags & TQ_NEW)) { 118eda14cbcSMatt Macy t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list); 119eda14cbcSMatt Macy 120eda14cbcSMatt Macy ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC)); 121eda14cbcSMatt Macy ASSERT(!(t->tqent_flags & TQENT_FLAG_CANCEL)); 122eda14cbcSMatt Macy ASSERT(!timer_pending(&t->tqent_timer)); 123eda14cbcSMatt Macy 124eda14cbcSMatt Macy list_del_init(&t->tqent_list); 125eda14cbcSMatt Macy return (t); 126eda14cbcSMatt Macy } 127eda14cbcSMatt Macy 128eda14cbcSMatt Macy /* Free list is empty and memory allocations are prohibited */ 129eda14cbcSMatt Macy if (flags & TQ_NOALLOC) 130eda14cbcSMatt Macy return (NULL); 131eda14cbcSMatt Macy 132eda14cbcSMatt Macy /* Hit maximum taskq_ent_t pool size */ 133eda14cbcSMatt Macy if (tq->tq_nalloc >= tq->tq_maxalloc) { 134eda14cbcSMatt Macy if (flags & TQ_NOSLEEP) 135eda14cbcSMatt Macy return (NULL); 136eda14cbcSMatt Macy 137eda14cbcSMatt Macy /* 138eda14cbcSMatt Macy * Sleep periodically polling the free list for an available 139eda14cbcSMatt Macy * taskq_ent_t. Dispatching with TQ_SLEEP should always succeed 140eda14cbcSMatt Macy * but we cannot block forever waiting for an taskq_ent_t to 141eda14cbcSMatt Macy * show up in the free list, otherwise a deadlock can happen. 142eda14cbcSMatt Macy * 143eda14cbcSMatt Macy * Therefore, we need to allocate a new task even if the number 144eda14cbcSMatt Macy * of allocated tasks is above tq->tq_maxalloc, but we still 145eda14cbcSMatt Macy * end up delaying the task allocation by one second, thereby 146eda14cbcSMatt Macy * throttling the task dispatch rate. 147eda14cbcSMatt Macy */ 148eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, *irqflags); 149eda14cbcSMatt Macy schedule_timeout(HZ / 100); 150eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, *irqflags, 151eda14cbcSMatt Macy tq->tq_lock_class); 152eda14cbcSMatt Macy if (count < 100) { 153eda14cbcSMatt Macy count++; 154eda14cbcSMatt Macy goto retry; 155eda14cbcSMatt Macy } 156eda14cbcSMatt Macy } 157eda14cbcSMatt Macy 158eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, *irqflags); 159eda14cbcSMatt Macy t = kmem_alloc(sizeof (taskq_ent_t), task_km_flags(flags)); 160eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, *irqflags, tq->tq_lock_class); 161eda14cbcSMatt Macy 162eda14cbcSMatt Macy if (t) { 163eda14cbcSMatt Macy taskq_init_ent(t); 164eda14cbcSMatt Macy tq->tq_nalloc++; 165eda14cbcSMatt Macy } 166eda14cbcSMatt Macy 167eda14cbcSMatt Macy return (t); 168eda14cbcSMatt Macy } 169eda14cbcSMatt Macy 170eda14cbcSMatt Macy /* 171eda14cbcSMatt Macy * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t 172eda14cbcSMatt Macy * to already be removed from the free, work, or pending taskq lists. 173eda14cbcSMatt Macy */ 174eda14cbcSMatt Macy static void 175eda14cbcSMatt Macy task_free(taskq_t *tq, taskq_ent_t *t) 176eda14cbcSMatt Macy { 177eda14cbcSMatt Macy ASSERT(tq); 178eda14cbcSMatt Macy ASSERT(t); 179eda14cbcSMatt Macy ASSERT(list_empty(&t->tqent_list)); 180eda14cbcSMatt Macy ASSERT(!timer_pending(&t->tqent_timer)); 181eda14cbcSMatt Macy 182eda14cbcSMatt Macy kmem_free(t, sizeof (taskq_ent_t)); 183eda14cbcSMatt Macy tq->tq_nalloc--; 184eda14cbcSMatt Macy } 185eda14cbcSMatt Macy 186eda14cbcSMatt Macy /* 187eda14cbcSMatt Macy * NOTE: Must be called with tq->tq_lock held, either destroys the 188eda14cbcSMatt Macy * taskq_ent_t if too many exist or moves it to the free list for later use. 189eda14cbcSMatt Macy */ 190eda14cbcSMatt Macy static void 191eda14cbcSMatt Macy task_done(taskq_t *tq, taskq_ent_t *t) 192eda14cbcSMatt Macy { 193eda14cbcSMatt Macy ASSERT(tq); 194eda14cbcSMatt Macy ASSERT(t); 195eda14cbcSMatt Macy 196eda14cbcSMatt Macy /* Wake tasks blocked in taskq_wait_id() */ 197eda14cbcSMatt Macy wake_up_all(&t->tqent_waitq); 198eda14cbcSMatt Macy 199eda14cbcSMatt Macy list_del_init(&t->tqent_list); 200eda14cbcSMatt Macy 201eda14cbcSMatt Macy if (tq->tq_nalloc <= tq->tq_minalloc) { 202eda14cbcSMatt Macy t->tqent_id = TASKQID_INVALID; 203eda14cbcSMatt Macy t->tqent_func = NULL; 204eda14cbcSMatt Macy t->tqent_arg = NULL; 205eda14cbcSMatt Macy t->tqent_flags = 0; 206eda14cbcSMatt Macy 207eda14cbcSMatt Macy list_add_tail(&t->tqent_list, &tq->tq_free_list); 208eda14cbcSMatt Macy } else { 209eda14cbcSMatt Macy task_free(tq, t); 210eda14cbcSMatt Macy } 211eda14cbcSMatt Macy } 212eda14cbcSMatt Macy 213eda14cbcSMatt Macy /* 214eda14cbcSMatt Macy * When a delayed task timer expires remove it from the delay list and 215eda14cbcSMatt Macy * add it to the priority list in order for immediate processing. 216eda14cbcSMatt Macy */ 217eda14cbcSMatt Macy static void 218eda14cbcSMatt Macy task_expire_impl(taskq_ent_t *t) 219eda14cbcSMatt Macy { 220eda14cbcSMatt Macy taskq_ent_t *w; 221eda14cbcSMatt Macy taskq_t *tq = t->tqent_taskq; 222eda14cbcSMatt Macy struct list_head *l = NULL; 223eda14cbcSMatt Macy unsigned long flags; 224eda14cbcSMatt Macy 225eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); 226eda14cbcSMatt Macy 227eda14cbcSMatt Macy if (t->tqent_flags & TQENT_FLAG_CANCEL) { 228eda14cbcSMatt Macy ASSERT(list_empty(&t->tqent_list)); 229eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 230eda14cbcSMatt Macy return; 231eda14cbcSMatt Macy } 232eda14cbcSMatt Macy 233eda14cbcSMatt Macy t->tqent_birth = jiffies; 234eda14cbcSMatt Macy DTRACE_PROBE1(taskq_ent__birth, taskq_ent_t *, t); 235eda14cbcSMatt Macy 236eda14cbcSMatt Macy /* 237eda14cbcSMatt Macy * The priority list must be maintained in strict task id order 238eda14cbcSMatt Macy * from lowest to highest for lowest_id to be easily calculable. 239eda14cbcSMatt Macy */ 240eda14cbcSMatt Macy list_del(&t->tqent_list); 241eda14cbcSMatt Macy list_for_each_prev(l, &tq->tq_prio_list) { 242eda14cbcSMatt Macy w = list_entry(l, taskq_ent_t, tqent_list); 243eda14cbcSMatt Macy if (w->tqent_id < t->tqent_id) { 244eda14cbcSMatt Macy list_add(&t->tqent_list, l); 245eda14cbcSMatt Macy break; 246eda14cbcSMatt Macy } 247eda14cbcSMatt Macy } 248eda14cbcSMatt Macy if (l == &tq->tq_prio_list) 249eda14cbcSMatt Macy list_add(&t->tqent_list, &tq->tq_prio_list); 250eda14cbcSMatt Macy 251eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 252eda14cbcSMatt Macy 253eda14cbcSMatt Macy wake_up(&tq->tq_work_waitq); 254eda14cbcSMatt Macy } 255eda14cbcSMatt Macy 256eda14cbcSMatt Macy static void 257eda14cbcSMatt Macy task_expire(spl_timer_list_t tl) 258eda14cbcSMatt Macy { 259eda14cbcSMatt Macy struct timer_list *tmr = (struct timer_list *)tl; 260eda14cbcSMatt Macy taskq_ent_t *t = from_timer(t, tmr, tqent_timer); 261eda14cbcSMatt Macy task_expire_impl(t); 262eda14cbcSMatt Macy } 263eda14cbcSMatt Macy 264eda14cbcSMatt Macy /* 265eda14cbcSMatt Macy * Returns the lowest incomplete taskqid_t. The taskqid_t may 266eda14cbcSMatt Macy * be queued on the pending list, on the priority list, on the 267eda14cbcSMatt Macy * delay list, or on the work list currently being handled, but 268eda14cbcSMatt Macy * it is not 100% complete yet. 269eda14cbcSMatt Macy */ 270eda14cbcSMatt Macy static taskqid_t 271eda14cbcSMatt Macy taskq_lowest_id(taskq_t *tq) 272eda14cbcSMatt Macy { 273eda14cbcSMatt Macy taskqid_t lowest_id = tq->tq_next_id; 274eda14cbcSMatt Macy taskq_ent_t *t; 275eda14cbcSMatt Macy taskq_thread_t *tqt; 276eda14cbcSMatt Macy 277eda14cbcSMatt Macy if (!list_empty(&tq->tq_pend_list)) { 278eda14cbcSMatt Macy t = list_entry(tq->tq_pend_list.next, taskq_ent_t, tqent_list); 279eda14cbcSMatt Macy lowest_id = MIN(lowest_id, t->tqent_id); 280eda14cbcSMatt Macy } 281eda14cbcSMatt Macy 282eda14cbcSMatt Macy if (!list_empty(&tq->tq_prio_list)) { 283eda14cbcSMatt Macy t = list_entry(tq->tq_prio_list.next, taskq_ent_t, tqent_list); 284eda14cbcSMatt Macy lowest_id = MIN(lowest_id, t->tqent_id); 285eda14cbcSMatt Macy } 286eda14cbcSMatt Macy 287eda14cbcSMatt Macy if (!list_empty(&tq->tq_delay_list)) { 288eda14cbcSMatt Macy t = list_entry(tq->tq_delay_list.next, taskq_ent_t, tqent_list); 289eda14cbcSMatt Macy lowest_id = MIN(lowest_id, t->tqent_id); 290eda14cbcSMatt Macy } 291eda14cbcSMatt Macy 292eda14cbcSMatt Macy if (!list_empty(&tq->tq_active_list)) { 293eda14cbcSMatt Macy tqt = list_entry(tq->tq_active_list.next, taskq_thread_t, 294eda14cbcSMatt Macy tqt_active_list); 295eda14cbcSMatt Macy ASSERT(tqt->tqt_id != TASKQID_INVALID); 296eda14cbcSMatt Macy lowest_id = MIN(lowest_id, tqt->tqt_id); 297eda14cbcSMatt Macy } 298eda14cbcSMatt Macy 299eda14cbcSMatt Macy return (lowest_id); 300eda14cbcSMatt Macy } 301eda14cbcSMatt Macy 302eda14cbcSMatt Macy /* 303eda14cbcSMatt Macy * Insert a task into a list keeping the list sorted by increasing taskqid. 304eda14cbcSMatt Macy */ 305eda14cbcSMatt Macy static void 306eda14cbcSMatt Macy taskq_insert_in_order(taskq_t *tq, taskq_thread_t *tqt) 307eda14cbcSMatt Macy { 308eda14cbcSMatt Macy taskq_thread_t *w; 309eda14cbcSMatt Macy struct list_head *l = NULL; 310eda14cbcSMatt Macy 311eda14cbcSMatt Macy ASSERT(tq); 312eda14cbcSMatt Macy ASSERT(tqt); 313eda14cbcSMatt Macy 314eda14cbcSMatt Macy list_for_each_prev(l, &tq->tq_active_list) { 315eda14cbcSMatt Macy w = list_entry(l, taskq_thread_t, tqt_active_list); 316eda14cbcSMatt Macy if (w->tqt_id < tqt->tqt_id) { 317eda14cbcSMatt Macy list_add(&tqt->tqt_active_list, l); 318eda14cbcSMatt Macy break; 319eda14cbcSMatt Macy } 320eda14cbcSMatt Macy } 321eda14cbcSMatt Macy if (l == &tq->tq_active_list) 322eda14cbcSMatt Macy list_add(&tqt->tqt_active_list, &tq->tq_active_list); 323eda14cbcSMatt Macy } 324eda14cbcSMatt Macy 325eda14cbcSMatt Macy /* 326eda14cbcSMatt Macy * Find and return a task from the given list if it exists. The list 327eda14cbcSMatt Macy * must be in lowest to highest task id order. 328eda14cbcSMatt Macy */ 329eda14cbcSMatt Macy static taskq_ent_t * 330eda14cbcSMatt Macy taskq_find_list(taskq_t *tq, struct list_head *lh, taskqid_t id) 331eda14cbcSMatt Macy { 332eda14cbcSMatt Macy struct list_head *l = NULL; 333eda14cbcSMatt Macy taskq_ent_t *t; 334eda14cbcSMatt Macy 335eda14cbcSMatt Macy list_for_each(l, lh) { 336eda14cbcSMatt Macy t = list_entry(l, taskq_ent_t, tqent_list); 337eda14cbcSMatt Macy 338eda14cbcSMatt Macy if (t->tqent_id == id) 339eda14cbcSMatt Macy return (t); 340eda14cbcSMatt Macy 341eda14cbcSMatt Macy if (t->tqent_id > id) 342eda14cbcSMatt Macy break; 343eda14cbcSMatt Macy } 344eda14cbcSMatt Macy 345eda14cbcSMatt Macy return (NULL); 346eda14cbcSMatt Macy } 347eda14cbcSMatt Macy 348eda14cbcSMatt Macy /* 349eda14cbcSMatt Macy * Find an already dispatched task given the task id regardless of what 350eda14cbcSMatt Macy * state it is in. If a task is still pending it will be returned. 351eda14cbcSMatt Macy * If a task is executing, then -EBUSY will be returned instead. 352eda14cbcSMatt Macy * If the task has already been run then NULL is returned. 353eda14cbcSMatt Macy */ 354eda14cbcSMatt Macy static taskq_ent_t * 355eda14cbcSMatt Macy taskq_find(taskq_t *tq, taskqid_t id) 356eda14cbcSMatt Macy { 357eda14cbcSMatt Macy taskq_thread_t *tqt; 358eda14cbcSMatt Macy struct list_head *l = NULL; 359eda14cbcSMatt Macy taskq_ent_t *t; 360eda14cbcSMatt Macy 361eda14cbcSMatt Macy t = taskq_find_list(tq, &tq->tq_delay_list, id); 362eda14cbcSMatt Macy if (t) 363eda14cbcSMatt Macy return (t); 364eda14cbcSMatt Macy 365eda14cbcSMatt Macy t = taskq_find_list(tq, &tq->tq_prio_list, id); 366eda14cbcSMatt Macy if (t) 367eda14cbcSMatt Macy return (t); 368eda14cbcSMatt Macy 369eda14cbcSMatt Macy t = taskq_find_list(tq, &tq->tq_pend_list, id); 370eda14cbcSMatt Macy if (t) 371eda14cbcSMatt Macy return (t); 372eda14cbcSMatt Macy 373eda14cbcSMatt Macy list_for_each(l, &tq->tq_active_list) { 374eda14cbcSMatt Macy tqt = list_entry(l, taskq_thread_t, tqt_active_list); 375eda14cbcSMatt Macy if (tqt->tqt_id == id) { 376eda14cbcSMatt Macy /* 377eda14cbcSMatt Macy * Instead of returning tqt_task, we just return a non 378eda14cbcSMatt Macy * NULL value to prevent misuse, since tqt_task only 379eda14cbcSMatt Macy * has two valid fields. 380eda14cbcSMatt Macy */ 381eda14cbcSMatt Macy return (ERR_PTR(-EBUSY)); 382eda14cbcSMatt Macy } 383eda14cbcSMatt Macy } 384eda14cbcSMatt Macy 385eda14cbcSMatt Macy return (NULL); 386eda14cbcSMatt Macy } 387eda14cbcSMatt Macy 388eda14cbcSMatt Macy /* 389eda14cbcSMatt Macy * Theory for the taskq_wait_id(), taskq_wait_outstanding(), and 390eda14cbcSMatt Macy * taskq_wait() functions below. 391eda14cbcSMatt Macy * 392eda14cbcSMatt Macy * Taskq waiting is accomplished by tracking the lowest outstanding task 393eda14cbcSMatt Macy * id and the next available task id. As tasks are dispatched they are 394eda14cbcSMatt Macy * added to the tail of the pending, priority, or delay lists. As worker 395eda14cbcSMatt Macy * threads become available the tasks are removed from the heads of these 396eda14cbcSMatt Macy * lists and linked to the worker threads. This ensures the lists are 397eda14cbcSMatt Macy * kept sorted by lowest to highest task id. 398eda14cbcSMatt Macy * 399eda14cbcSMatt Macy * Therefore the lowest outstanding task id can be quickly determined by 400eda14cbcSMatt Macy * checking the head item from all of these lists. This value is stored 401eda14cbcSMatt Macy * with the taskq as the lowest id. It only needs to be recalculated when 402eda14cbcSMatt Macy * either the task with the current lowest id completes or is canceled. 403eda14cbcSMatt Macy * 404eda14cbcSMatt Macy * By blocking until the lowest task id exceeds the passed task id the 405eda14cbcSMatt Macy * taskq_wait_outstanding() function can be easily implemented. Similarly, 406eda14cbcSMatt Macy * by blocking until the lowest task id matches the next task id taskq_wait() 407eda14cbcSMatt Macy * can be implemented. 408eda14cbcSMatt Macy * 409eda14cbcSMatt Macy * Callers should be aware that when there are multiple worked threads it 410eda14cbcSMatt Macy * is possible for larger task ids to complete before smaller ones. Also 411eda14cbcSMatt Macy * when the taskq contains delay tasks with small task ids callers may 412eda14cbcSMatt Macy * block for a considerable length of time waiting for them to expire and 413eda14cbcSMatt Macy * execute. 414eda14cbcSMatt Macy */ 415eda14cbcSMatt Macy static int 416eda14cbcSMatt Macy taskq_wait_id_check(taskq_t *tq, taskqid_t id) 417eda14cbcSMatt Macy { 418eda14cbcSMatt Macy int rc; 419eda14cbcSMatt Macy unsigned long flags; 420eda14cbcSMatt Macy 421eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); 422eda14cbcSMatt Macy rc = (taskq_find(tq, id) == NULL); 423eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 424eda14cbcSMatt Macy 425eda14cbcSMatt Macy return (rc); 426eda14cbcSMatt Macy } 427eda14cbcSMatt Macy 428eda14cbcSMatt Macy /* 429eda14cbcSMatt Macy * The taskq_wait_id() function blocks until the passed task id completes. 430eda14cbcSMatt Macy * This does not guarantee that all lower task ids have completed. 431eda14cbcSMatt Macy */ 432eda14cbcSMatt Macy void 433eda14cbcSMatt Macy taskq_wait_id(taskq_t *tq, taskqid_t id) 434eda14cbcSMatt Macy { 435eda14cbcSMatt Macy wait_event(tq->tq_wait_waitq, taskq_wait_id_check(tq, id)); 436eda14cbcSMatt Macy } 437eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_wait_id); 438eda14cbcSMatt Macy 439eda14cbcSMatt Macy static int 440eda14cbcSMatt Macy taskq_wait_outstanding_check(taskq_t *tq, taskqid_t id) 441eda14cbcSMatt Macy { 442eda14cbcSMatt Macy int rc; 443eda14cbcSMatt Macy unsigned long flags; 444eda14cbcSMatt Macy 445eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); 446eda14cbcSMatt Macy rc = (id < tq->tq_lowest_id); 447eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 448eda14cbcSMatt Macy 449eda14cbcSMatt Macy return (rc); 450eda14cbcSMatt Macy } 451eda14cbcSMatt Macy 452eda14cbcSMatt Macy /* 453eda14cbcSMatt Macy * The taskq_wait_outstanding() function will block until all tasks with a 454eda14cbcSMatt Macy * lower taskqid than the passed 'id' have been completed. Note that all 455eda14cbcSMatt Macy * task id's are assigned monotonically at dispatch time. Zero may be 456eda14cbcSMatt Macy * passed for the id to indicate all tasks dispatch up to this point, 457eda14cbcSMatt Macy * but not after, should be waited for. 458eda14cbcSMatt Macy */ 459eda14cbcSMatt Macy void 460eda14cbcSMatt Macy taskq_wait_outstanding(taskq_t *tq, taskqid_t id) 461eda14cbcSMatt Macy { 462eda14cbcSMatt Macy id = id ? id : tq->tq_next_id - 1; 463eda14cbcSMatt Macy wait_event(tq->tq_wait_waitq, taskq_wait_outstanding_check(tq, id)); 464eda14cbcSMatt Macy } 465eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_wait_outstanding); 466eda14cbcSMatt Macy 467eda14cbcSMatt Macy static int 468eda14cbcSMatt Macy taskq_wait_check(taskq_t *tq) 469eda14cbcSMatt Macy { 470eda14cbcSMatt Macy int rc; 471eda14cbcSMatt Macy unsigned long flags; 472eda14cbcSMatt Macy 473eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); 474eda14cbcSMatt Macy rc = (tq->tq_lowest_id == tq->tq_next_id); 475eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 476eda14cbcSMatt Macy 477eda14cbcSMatt Macy return (rc); 478eda14cbcSMatt Macy } 479eda14cbcSMatt Macy 480eda14cbcSMatt Macy /* 481eda14cbcSMatt Macy * The taskq_wait() function will block until the taskq is empty. 482eda14cbcSMatt Macy * This means that if a taskq re-dispatches work to itself taskq_wait() 483eda14cbcSMatt Macy * callers will block indefinitely. 484eda14cbcSMatt Macy */ 485eda14cbcSMatt Macy void 486eda14cbcSMatt Macy taskq_wait(taskq_t *tq) 487eda14cbcSMatt Macy { 488eda14cbcSMatt Macy wait_event(tq->tq_wait_waitq, taskq_wait_check(tq)); 489eda14cbcSMatt Macy } 490eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_wait); 491eda14cbcSMatt Macy 492eda14cbcSMatt Macy int 493eda14cbcSMatt Macy taskq_member(taskq_t *tq, kthread_t *t) 494eda14cbcSMatt Macy { 495eda14cbcSMatt Macy return (tq == (taskq_t *)tsd_get_by_thread(taskq_tsd, t)); 496eda14cbcSMatt Macy } 497eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_member); 498eda14cbcSMatt Macy 499eda14cbcSMatt Macy taskq_t * 500eda14cbcSMatt Macy taskq_of_curthread(void) 501eda14cbcSMatt Macy { 502eda14cbcSMatt Macy return (tsd_get(taskq_tsd)); 503eda14cbcSMatt Macy } 504eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_of_curthread); 505eda14cbcSMatt Macy 506eda14cbcSMatt Macy /* 507eda14cbcSMatt Macy * Cancel an already dispatched task given the task id. Still pending tasks 508eda14cbcSMatt Macy * will be immediately canceled, and if the task is active the function will 509eda14cbcSMatt Macy * block until it completes. Preallocated tasks which are canceled must be 510eda14cbcSMatt Macy * freed by the caller. 511eda14cbcSMatt Macy */ 512eda14cbcSMatt Macy int 513eda14cbcSMatt Macy taskq_cancel_id(taskq_t *tq, taskqid_t id) 514eda14cbcSMatt Macy { 515eda14cbcSMatt Macy taskq_ent_t *t; 516eda14cbcSMatt Macy int rc = ENOENT; 517eda14cbcSMatt Macy unsigned long flags; 518eda14cbcSMatt Macy 519eda14cbcSMatt Macy ASSERT(tq); 520eda14cbcSMatt Macy 521eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); 522eda14cbcSMatt Macy t = taskq_find(tq, id); 523eda14cbcSMatt Macy if (t && t != ERR_PTR(-EBUSY)) { 524eda14cbcSMatt Macy list_del_init(&t->tqent_list); 525eda14cbcSMatt Macy t->tqent_flags |= TQENT_FLAG_CANCEL; 526eda14cbcSMatt Macy 527eda14cbcSMatt Macy /* 528eda14cbcSMatt Macy * When canceling the lowest outstanding task id we 529eda14cbcSMatt Macy * must recalculate the new lowest outstanding id. 530eda14cbcSMatt Macy */ 531eda14cbcSMatt Macy if (tq->tq_lowest_id == t->tqent_id) { 532eda14cbcSMatt Macy tq->tq_lowest_id = taskq_lowest_id(tq); 533eda14cbcSMatt Macy ASSERT3S(tq->tq_lowest_id, >, t->tqent_id); 534eda14cbcSMatt Macy } 535eda14cbcSMatt Macy 536eda14cbcSMatt Macy /* 537eda14cbcSMatt Macy * The task_expire() function takes the tq->tq_lock so drop 538eda14cbcSMatt Macy * drop the lock before synchronously cancelling the timer. 539eda14cbcSMatt Macy */ 540eda14cbcSMatt Macy if (timer_pending(&t->tqent_timer)) { 541eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 542eda14cbcSMatt Macy del_timer_sync(&t->tqent_timer); 543eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, 544eda14cbcSMatt Macy tq->tq_lock_class); 545eda14cbcSMatt Macy } 546eda14cbcSMatt Macy 547eda14cbcSMatt Macy if (!(t->tqent_flags & TQENT_FLAG_PREALLOC)) 548eda14cbcSMatt Macy task_done(tq, t); 549eda14cbcSMatt Macy 550eda14cbcSMatt Macy rc = 0; 551eda14cbcSMatt Macy } 552eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 553eda14cbcSMatt Macy 554eda14cbcSMatt Macy if (t == ERR_PTR(-EBUSY)) { 555eda14cbcSMatt Macy taskq_wait_id(tq, id); 556eda14cbcSMatt Macy rc = EBUSY; 557eda14cbcSMatt Macy } 558eda14cbcSMatt Macy 559eda14cbcSMatt Macy return (rc); 560eda14cbcSMatt Macy } 561eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_cancel_id); 562eda14cbcSMatt Macy 563eda14cbcSMatt Macy static int taskq_thread_spawn(taskq_t *tq); 564eda14cbcSMatt Macy 565eda14cbcSMatt Macy taskqid_t 566eda14cbcSMatt Macy taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags) 567eda14cbcSMatt Macy { 568eda14cbcSMatt Macy taskq_ent_t *t; 569eda14cbcSMatt Macy taskqid_t rc = TASKQID_INVALID; 570eda14cbcSMatt Macy unsigned long irqflags; 571eda14cbcSMatt Macy 572eda14cbcSMatt Macy ASSERT(tq); 573eda14cbcSMatt Macy ASSERT(func); 574eda14cbcSMatt Macy 575eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, irqflags, tq->tq_lock_class); 576eda14cbcSMatt Macy 577eda14cbcSMatt Macy /* Taskq being destroyed and all tasks drained */ 578eda14cbcSMatt Macy if (!(tq->tq_flags & TASKQ_ACTIVE)) 579eda14cbcSMatt Macy goto out; 580eda14cbcSMatt Macy 581eda14cbcSMatt Macy /* Do not queue the task unless there is idle thread for it */ 582eda14cbcSMatt Macy ASSERT(tq->tq_nactive <= tq->tq_nthreads); 583eda14cbcSMatt Macy if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads)) { 584eda14cbcSMatt Macy /* Dynamic taskq may be able to spawn another thread */ 585eda14cbcSMatt Macy if (!(tq->tq_flags & TASKQ_DYNAMIC) || 586eda14cbcSMatt Macy taskq_thread_spawn(tq) == 0) 587eda14cbcSMatt Macy goto out; 588eda14cbcSMatt Macy } 589eda14cbcSMatt Macy 590eda14cbcSMatt Macy if ((t = task_alloc(tq, flags, &irqflags)) == NULL) 591eda14cbcSMatt Macy goto out; 592eda14cbcSMatt Macy 593eda14cbcSMatt Macy spin_lock(&t->tqent_lock); 594eda14cbcSMatt Macy 595eda14cbcSMatt Macy /* Queue to the front of the list to enforce TQ_NOQUEUE semantics */ 596eda14cbcSMatt Macy if (flags & TQ_NOQUEUE) 597eda14cbcSMatt Macy list_add(&t->tqent_list, &tq->tq_prio_list); 598eda14cbcSMatt Macy /* Queue to the priority list instead of the pending list */ 599eda14cbcSMatt Macy else if (flags & TQ_FRONT) 600eda14cbcSMatt Macy list_add_tail(&t->tqent_list, &tq->tq_prio_list); 601eda14cbcSMatt Macy else 602eda14cbcSMatt Macy list_add_tail(&t->tqent_list, &tq->tq_pend_list); 603eda14cbcSMatt Macy 604eda14cbcSMatt Macy t->tqent_id = rc = tq->tq_next_id; 605eda14cbcSMatt Macy tq->tq_next_id++; 606eda14cbcSMatt Macy t->tqent_func = func; 607eda14cbcSMatt Macy t->tqent_arg = arg; 608eda14cbcSMatt Macy t->tqent_taskq = tq; 609eda14cbcSMatt Macy t->tqent_timer.function = NULL; 610eda14cbcSMatt Macy t->tqent_timer.expires = 0; 611eda14cbcSMatt Macy 612eda14cbcSMatt Macy t->tqent_birth = jiffies; 613eda14cbcSMatt Macy DTRACE_PROBE1(taskq_ent__birth, taskq_ent_t *, t); 614eda14cbcSMatt Macy 615eda14cbcSMatt Macy ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC)); 616eda14cbcSMatt Macy 617eda14cbcSMatt Macy spin_unlock(&t->tqent_lock); 618eda14cbcSMatt Macy 619eda14cbcSMatt Macy wake_up(&tq->tq_work_waitq); 620eda14cbcSMatt Macy out: 621eda14cbcSMatt Macy /* Spawn additional taskq threads if required. */ 622eda14cbcSMatt Macy if (!(flags & TQ_NOQUEUE) && tq->tq_nactive == tq->tq_nthreads) 623eda14cbcSMatt Macy (void) taskq_thread_spawn(tq); 624eda14cbcSMatt Macy 625eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, irqflags); 626eda14cbcSMatt Macy return (rc); 627eda14cbcSMatt Macy } 628eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_dispatch); 629eda14cbcSMatt Macy 630eda14cbcSMatt Macy taskqid_t 631eda14cbcSMatt Macy taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg, 632eda14cbcSMatt Macy uint_t flags, clock_t expire_time) 633eda14cbcSMatt Macy { 634eda14cbcSMatt Macy taskqid_t rc = TASKQID_INVALID; 635eda14cbcSMatt Macy taskq_ent_t *t; 636eda14cbcSMatt Macy unsigned long irqflags; 637eda14cbcSMatt Macy 638eda14cbcSMatt Macy ASSERT(tq); 639eda14cbcSMatt Macy ASSERT(func); 640eda14cbcSMatt Macy 641eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, irqflags, tq->tq_lock_class); 642eda14cbcSMatt Macy 643eda14cbcSMatt Macy /* Taskq being destroyed and all tasks drained */ 644eda14cbcSMatt Macy if (!(tq->tq_flags & TASKQ_ACTIVE)) 645eda14cbcSMatt Macy goto out; 646eda14cbcSMatt Macy 647eda14cbcSMatt Macy if ((t = task_alloc(tq, flags, &irqflags)) == NULL) 648eda14cbcSMatt Macy goto out; 649eda14cbcSMatt Macy 650eda14cbcSMatt Macy spin_lock(&t->tqent_lock); 651eda14cbcSMatt Macy 652eda14cbcSMatt Macy /* Queue to the delay list for subsequent execution */ 653eda14cbcSMatt Macy list_add_tail(&t->tqent_list, &tq->tq_delay_list); 654eda14cbcSMatt Macy 655eda14cbcSMatt Macy t->tqent_id = rc = tq->tq_next_id; 656eda14cbcSMatt Macy tq->tq_next_id++; 657eda14cbcSMatt Macy t->tqent_func = func; 658eda14cbcSMatt Macy t->tqent_arg = arg; 659eda14cbcSMatt Macy t->tqent_taskq = tq; 660eda14cbcSMatt Macy t->tqent_timer.function = task_expire; 661eda14cbcSMatt Macy t->tqent_timer.expires = (unsigned long)expire_time; 662eda14cbcSMatt Macy add_timer(&t->tqent_timer); 663eda14cbcSMatt Macy 664eda14cbcSMatt Macy ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC)); 665eda14cbcSMatt Macy 666eda14cbcSMatt Macy spin_unlock(&t->tqent_lock); 667eda14cbcSMatt Macy out: 668eda14cbcSMatt Macy /* Spawn additional taskq threads if required. */ 669eda14cbcSMatt Macy if (tq->tq_nactive == tq->tq_nthreads) 670eda14cbcSMatt Macy (void) taskq_thread_spawn(tq); 671eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, irqflags); 672eda14cbcSMatt Macy return (rc); 673eda14cbcSMatt Macy } 674eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_dispatch_delay); 675eda14cbcSMatt Macy 676eda14cbcSMatt Macy void 677eda14cbcSMatt Macy taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags, 678eda14cbcSMatt Macy taskq_ent_t *t) 679eda14cbcSMatt Macy { 680eda14cbcSMatt Macy unsigned long irqflags; 681eda14cbcSMatt Macy ASSERT(tq); 682eda14cbcSMatt Macy ASSERT(func); 683eda14cbcSMatt Macy 684eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, irqflags, 685eda14cbcSMatt Macy tq->tq_lock_class); 686eda14cbcSMatt Macy 687eda14cbcSMatt Macy /* Taskq being destroyed and all tasks drained */ 688eda14cbcSMatt Macy if (!(tq->tq_flags & TASKQ_ACTIVE)) { 689eda14cbcSMatt Macy t->tqent_id = TASKQID_INVALID; 690eda14cbcSMatt Macy goto out; 691eda14cbcSMatt Macy } 692eda14cbcSMatt Macy 693eda14cbcSMatt Macy if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads)) { 694eda14cbcSMatt Macy /* Dynamic taskq may be able to spawn another thread */ 695eda14cbcSMatt Macy if (!(tq->tq_flags & TASKQ_DYNAMIC) || 696eda14cbcSMatt Macy taskq_thread_spawn(tq) == 0) 697eda14cbcSMatt Macy goto out2; 698eda14cbcSMatt Macy flags |= TQ_FRONT; 699eda14cbcSMatt Macy } 700eda14cbcSMatt Macy 701eda14cbcSMatt Macy spin_lock(&t->tqent_lock); 702eda14cbcSMatt Macy 703eda14cbcSMatt Macy /* 704eda14cbcSMatt Macy * Make sure the entry is not on some other taskq; it is important to 705eda14cbcSMatt Macy * ASSERT() under lock 706eda14cbcSMatt Macy */ 707eda14cbcSMatt Macy ASSERT(taskq_empty_ent(t)); 708eda14cbcSMatt Macy 709eda14cbcSMatt Macy /* 710eda14cbcSMatt Macy * Mark it as a prealloc'd task. This is important 711eda14cbcSMatt Macy * to ensure that we don't free it later. 712eda14cbcSMatt Macy */ 713eda14cbcSMatt Macy t->tqent_flags |= TQENT_FLAG_PREALLOC; 714eda14cbcSMatt Macy 715eda14cbcSMatt Macy /* Queue to the priority list instead of the pending list */ 716eda14cbcSMatt Macy if (flags & TQ_FRONT) 717eda14cbcSMatt Macy list_add_tail(&t->tqent_list, &tq->tq_prio_list); 718eda14cbcSMatt Macy else 719eda14cbcSMatt Macy list_add_tail(&t->tqent_list, &tq->tq_pend_list); 720eda14cbcSMatt Macy 721eda14cbcSMatt Macy t->tqent_id = tq->tq_next_id; 722eda14cbcSMatt Macy tq->tq_next_id++; 723eda14cbcSMatt Macy t->tqent_func = func; 724eda14cbcSMatt Macy t->tqent_arg = arg; 725eda14cbcSMatt Macy t->tqent_taskq = tq; 726eda14cbcSMatt Macy 727eda14cbcSMatt Macy t->tqent_birth = jiffies; 728eda14cbcSMatt Macy DTRACE_PROBE1(taskq_ent__birth, taskq_ent_t *, t); 729eda14cbcSMatt Macy 730eda14cbcSMatt Macy spin_unlock(&t->tqent_lock); 731eda14cbcSMatt Macy 732eda14cbcSMatt Macy wake_up(&tq->tq_work_waitq); 733eda14cbcSMatt Macy out: 734eda14cbcSMatt Macy /* Spawn additional taskq threads if required. */ 735eda14cbcSMatt Macy if (tq->tq_nactive == tq->tq_nthreads) 736eda14cbcSMatt Macy (void) taskq_thread_spawn(tq); 737eda14cbcSMatt Macy out2: 738eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, irqflags); 739eda14cbcSMatt Macy } 740eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_dispatch_ent); 741eda14cbcSMatt Macy 742eda14cbcSMatt Macy int 743eda14cbcSMatt Macy taskq_empty_ent(taskq_ent_t *t) 744eda14cbcSMatt Macy { 745eda14cbcSMatt Macy return (list_empty(&t->tqent_list)); 746eda14cbcSMatt Macy } 747eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_empty_ent); 748eda14cbcSMatt Macy 749eda14cbcSMatt Macy void 750eda14cbcSMatt Macy taskq_init_ent(taskq_ent_t *t) 751eda14cbcSMatt Macy { 752eda14cbcSMatt Macy spin_lock_init(&t->tqent_lock); 753eda14cbcSMatt Macy init_waitqueue_head(&t->tqent_waitq); 754eda14cbcSMatt Macy timer_setup(&t->tqent_timer, NULL, 0); 755eda14cbcSMatt Macy INIT_LIST_HEAD(&t->tqent_list); 756eda14cbcSMatt Macy t->tqent_id = 0; 757eda14cbcSMatt Macy t->tqent_func = NULL; 758eda14cbcSMatt Macy t->tqent_arg = NULL; 759eda14cbcSMatt Macy t->tqent_flags = 0; 760eda14cbcSMatt Macy t->tqent_taskq = NULL; 761eda14cbcSMatt Macy } 762eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_init_ent); 763eda14cbcSMatt Macy 764eda14cbcSMatt Macy /* 765eda14cbcSMatt Macy * Return the next pending task, preference is given to tasks on the 766eda14cbcSMatt Macy * priority list which were dispatched with TQ_FRONT. 767eda14cbcSMatt Macy */ 768eda14cbcSMatt Macy static taskq_ent_t * 769eda14cbcSMatt Macy taskq_next_ent(taskq_t *tq) 770eda14cbcSMatt Macy { 771eda14cbcSMatt Macy struct list_head *list; 772eda14cbcSMatt Macy 773eda14cbcSMatt Macy if (!list_empty(&tq->tq_prio_list)) 774eda14cbcSMatt Macy list = &tq->tq_prio_list; 775eda14cbcSMatt Macy else if (!list_empty(&tq->tq_pend_list)) 776eda14cbcSMatt Macy list = &tq->tq_pend_list; 777eda14cbcSMatt Macy else 778eda14cbcSMatt Macy return (NULL); 779eda14cbcSMatt Macy 780eda14cbcSMatt Macy return (list_entry(list->next, taskq_ent_t, tqent_list)); 781eda14cbcSMatt Macy } 782eda14cbcSMatt Macy 783eda14cbcSMatt Macy /* 784eda14cbcSMatt Macy * Spawns a new thread for the specified taskq. 785eda14cbcSMatt Macy */ 786eda14cbcSMatt Macy static void 787eda14cbcSMatt Macy taskq_thread_spawn_task(void *arg) 788eda14cbcSMatt Macy { 789eda14cbcSMatt Macy taskq_t *tq = (taskq_t *)arg; 790eda14cbcSMatt Macy unsigned long flags; 791eda14cbcSMatt Macy 792eda14cbcSMatt Macy if (taskq_thread_create(tq) == NULL) { 793eda14cbcSMatt Macy /* restore spawning count if failed */ 794eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, 795eda14cbcSMatt Macy tq->tq_lock_class); 796eda14cbcSMatt Macy tq->tq_nspawn--; 797eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 798eda14cbcSMatt Macy } 799eda14cbcSMatt Macy } 800eda14cbcSMatt Macy 801eda14cbcSMatt Macy /* 802eda14cbcSMatt Macy * Spawn addition threads for dynamic taskqs (TASKQ_DYNAMIC) the current 803eda14cbcSMatt Macy * number of threads is insufficient to handle the pending tasks. These 804eda14cbcSMatt Macy * new threads must be created by the dedicated dynamic_taskq to avoid 805eda14cbcSMatt Macy * deadlocks between thread creation and memory reclaim. The system_taskq 806eda14cbcSMatt Macy * which is also a dynamic taskq cannot be safely used for this. 807eda14cbcSMatt Macy */ 808eda14cbcSMatt Macy static int 809eda14cbcSMatt Macy taskq_thread_spawn(taskq_t *tq) 810eda14cbcSMatt Macy { 811eda14cbcSMatt Macy int spawning = 0; 812eda14cbcSMatt Macy 813eda14cbcSMatt Macy if (!(tq->tq_flags & TASKQ_DYNAMIC)) 814eda14cbcSMatt Macy return (0); 815eda14cbcSMatt Macy 816eda14cbcSMatt Macy if ((tq->tq_nthreads + tq->tq_nspawn < tq->tq_maxthreads) && 817eda14cbcSMatt Macy (tq->tq_flags & TASKQ_ACTIVE)) { 818eda14cbcSMatt Macy spawning = (++tq->tq_nspawn); 819eda14cbcSMatt Macy taskq_dispatch(dynamic_taskq, taskq_thread_spawn_task, 820eda14cbcSMatt Macy tq, TQ_NOSLEEP); 821eda14cbcSMatt Macy } 822eda14cbcSMatt Macy 823eda14cbcSMatt Macy return (spawning); 824eda14cbcSMatt Macy } 825eda14cbcSMatt Macy 826eda14cbcSMatt Macy /* 827eda14cbcSMatt Macy * Threads in a dynamic taskq should only exit once it has been completely 828eda14cbcSMatt Macy * drained and no other threads are actively servicing tasks. This prevents 829eda14cbcSMatt Macy * threads from being created and destroyed more than is required. 830eda14cbcSMatt Macy * 831eda14cbcSMatt Macy * The first thread is the thread list is treated as the primary thread. 832eda14cbcSMatt Macy * There is nothing special about the primary thread but in order to avoid 833eda14cbcSMatt Macy * all the taskq pids from changing we opt to make it long running. 834eda14cbcSMatt Macy */ 835eda14cbcSMatt Macy static int 836eda14cbcSMatt Macy taskq_thread_should_stop(taskq_t *tq, taskq_thread_t *tqt) 837eda14cbcSMatt Macy { 838eda14cbcSMatt Macy if (!(tq->tq_flags & TASKQ_DYNAMIC)) 839eda14cbcSMatt Macy return (0); 840eda14cbcSMatt Macy 841eda14cbcSMatt Macy if (list_first_entry(&(tq->tq_thread_list), taskq_thread_t, 842eda14cbcSMatt Macy tqt_thread_list) == tqt) 843eda14cbcSMatt Macy return (0); 844eda14cbcSMatt Macy 845eda14cbcSMatt Macy return 846eda14cbcSMatt Macy ((tq->tq_nspawn == 0) && /* No threads are being spawned */ 847eda14cbcSMatt Macy (tq->tq_nactive == 0) && /* No threads are handling tasks */ 848eda14cbcSMatt Macy (tq->tq_nthreads > 1) && /* More than 1 thread is running */ 849eda14cbcSMatt Macy (!taskq_next_ent(tq)) && /* There are no pending tasks */ 850eda14cbcSMatt Macy (spl_taskq_thread_dynamic)); /* Dynamic taskqs are allowed */ 851eda14cbcSMatt Macy } 852eda14cbcSMatt Macy 853eda14cbcSMatt Macy static int 854eda14cbcSMatt Macy taskq_thread(void *args) 855eda14cbcSMatt Macy { 856eda14cbcSMatt Macy DECLARE_WAITQUEUE(wait, current); 857eda14cbcSMatt Macy sigset_t blocked; 858eda14cbcSMatt Macy taskq_thread_t *tqt = args; 859eda14cbcSMatt Macy taskq_t *tq; 860eda14cbcSMatt Macy taskq_ent_t *t; 861eda14cbcSMatt Macy int seq_tasks = 0; 862eda14cbcSMatt Macy unsigned long flags; 863eda14cbcSMatt Macy taskq_ent_t dup_task = {}; 864eda14cbcSMatt Macy 865eda14cbcSMatt Macy ASSERT(tqt); 866eda14cbcSMatt Macy ASSERT(tqt->tqt_tq); 867eda14cbcSMatt Macy tq = tqt->tqt_tq; 868eda14cbcSMatt Macy current->flags |= PF_NOFREEZE; 869eda14cbcSMatt Macy 870eda14cbcSMatt Macy (void) spl_fstrans_mark(); 871eda14cbcSMatt Macy 872eda14cbcSMatt Macy sigfillset(&blocked); 873eda14cbcSMatt Macy sigprocmask(SIG_BLOCK, &blocked, NULL); 874eda14cbcSMatt Macy flush_signals(current); 875eda14cbcSMatt Macy 876eda14cbcSMatt Macy tsd_set(taskq_tsd, tq); 877eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); 878eda14cbcSMatt Macy /* 879eda14cbcSMatt Macy * If we are dynamically spawned, decrease spawning count. Note that 880eda14cbcSMatt Macy * we could be created during taskq_create, in which case we shouldn't 881eda14cbcSMatt Macy * do the decrement. But it's fine because taskq_create will reset 882eda14cbcSMatt Macy * tq_nspawn later. 883eda14cbcSMatt Macy */ 884eda14cbcSMatt Macy if (tq->tq_flags & TASKQ_DYNAMIC) 885eda14cbcSMatt Macy tq->tq_nspawn--; 886eda14cbcSMatt Macy 887eda14cbcSMatt Macy /* Immediately exit if more threads than allowed were created. */ 888eda14cbcSMatt Macy if (tq->tq_nthreads >= tq->tq_maxthreads) 889eda14cbcSMatt Macy goto error; 890eda14cbcSMatt Macy 891eda14cbcSMatt Macy tq->tq_nthreads++; 892eda14cbcSMatt Macy list_add_tail(&tqt->tqt_thread_list, &tq->tq_thread_list); 893eda14cbcSMatt Macy wake_up(&tq->tq_wait_waitq); 894eda14cbcSMatt Macy set_current_state(TASK_INTERRUPTIBLE); 895eda14cbcSMatt Macy 896eda14cbcSMatt Macy while (!kthread_should_stop()) { 897eda14cbcSMatt Macy 898eda14cbcSMatt Macy if (list_empty(&tq->tq_pend_list) && 899eda14cbcSMatt Macy list_empty(&tq->tq_prio_list)) { 900eda14cbcSMatt Macy 901eda14cbcSMatt Macy if (taskq_thread_should_stop(tq, tqt)) { 902eda14cbcSMatt Macy wake_up_all(&tq->tq_wait_waitq); 903eda14cbcSMatt Macy break; 904eda14cbcSMatt Macy } 905eda14cbcSMatt Macy 906eda14cbcSMatt Macy add_wait_queue_exclusive(&tq->tq_work_waitq, &wait); 907eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 908eda14cbcSMatt Macy 909eda14cbcSMatt Macy schedule(); 910eda14cbcSMatt Macy seq_tasks = 0; 911eda14cbcSMatt Macy 912eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, 913eda14cbcSMatt Macy tq->tq_lock_class); 914eda14cbcSMatt Macy remove_wait_queue(&tq->tq_work_waitq, &wait); 915eda14cbcSMatt Macy } else { 916eda14cbcSMatt Macy __set_current_state(TASK_RUNNING); 917eda14cbcSMatt Macy } 918eda14cbcSMatt Macy 919eda14cbcSMatt Macy if ((t = taskq_next_ent(tq)) != NULL) { 920eda14cbcSMatt Macy list_del_init(&t->tqent_list); 921eda14cbcSMatt Macy 922eda14cbcSMatt Macy /* 923eda14cbcSMatt Macy * A TQENT_FLAG_PREALLOC task may be reused or freed 924eda14cbcSMatt Macy * during the task function call. Store tqent_id and 925eda14cbcSMatt Macy * tqent_flags here. 926eda14cbcSMatt Macy * 927eda14cbcSMatt Macy * Also use an on stack taskq_ent_t for tqt_task 928eda14cbcSMatt Macy * assignment in this case; we want to make sure 929eda14cbcSMatt Macy * to duplicate all fields, so the values are 930eda14cbcSMatt Macy * correct when it's accessed via DTRACE_PROBE*. 931eda14cbcSMatt Macy */ 932eda14cbcSMatt Macy tqt->tqt_id = t->tqent_id; 933eda14cbcSMatt Macy tqt->tqt_flags = t->tqent_flags; 934eda14cbcSMatt Macy 935eda14cbcSMatt Macy if (t->tqent_flags & TQENT_FLAG_PREALLOC) { 936eda14cbcSMatt Macy dup_task = *t; 937eda14cbcSMatt Macy t = &dup_task; 938eda14cbcSMatt Macy } 939eda14cbcSMatt Macy tqt->tqt_task = t; 940eda14cbcSMatt Macy 941eda14cbcSMatt Macy taskq_insert_in_order(tq, tqt); 942eda14cbcSMatt Macy tq->tq_nactive++; 943eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 944eda14cbcSMatt Macy 945eda14cbcSMatt Macy DTRACE_PROBE1(taskq_ent__start, taskq_ent_t *, t); 946eda14cbcSMatt Macy 947eda14cbcSMatt Macy /* Perform the requested task */ 948eda14cbcSMatt Macy t->tqent_func(t->tqent_arg); 949eda14cbcSMatt Macy 950eda14cbcSMatt Macy DTRACE_PROBE1(taskq_ent__finish, taskq_ent_t *, t); 951eda14cbcSMatt Macy 952eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, 953eda14cbcSMatt Macy tq->tq_lock_class); 954eda14cbcSMatt Macy tq->tq_nactive--; 955eda14cbcSMatt Macy list_del_init(&tqt->tqt_active_list); 956eda14cbcSMatt Macy tqt->tqt_task = NULL; 957eda14cbcSMatt Macy 958eda14cbcSMatt Macy /* For prealloc'd tasks, we don't free anything. */ 959eda14cbcSMatt Macy if (!(tqt->tqt_flags & TQENT_FLAG_PREALLOC)) 960eda14cbcSMatt Macy task_done(tq, t); 961eda14cbcSMatt Macy 962eda14cbcSMatt Macy /* 963eda14cbcSMatt Macy * When the current lowest outstanding taskqid is 964eda14cbcSMatt Macy * done calculate the new lowest outstanding id 965eda14cbcSMatt Macy */ 966eda14cbcSMatt Macy if (tq->tq_lowest_id == tqt->tqt_id) { 967eda14cbcSMatt Macy tq->tq_lowest_id = taskq_lowest_id(tq); 968eda14cbcSMatt Macy ASSERT3S(tq->tq_lowest_id, >, tqt->tqt_id); 969eda14cbcSMatt Macy } 970eda14cbcSMatt Macy 971eda14cbcSMatt Macy /* Spawn additional taskq threads if required. */ 972eda14cbcSMatt Macy if ((++seq_tasks) > spl_taskq_thread_sequential && 973eda14cbcSMatt Macy taskq_thread_spawn(tq)) 974eda14cbcSMatt Macy seq_tasks = 0; 975eda14cbcSMatt Macy 976eda14cbcSMatt Macy tqt->tqt_id = TASKQID_INVALID; 977eda14cbcSMatt Macy tqt->tqt_flags = 0; 978eda14cbcSMatt Macy wake_up_all(&tq->tq_wait_waitq); 979eda14cbcSMatt Macy } else { 980eda14cbcSMatt Macy if (taskq_thread_should_stop(tq, tqt)) 981eda14cbcSMatt Macy break; 982eda14cbcSMatt Macy } 983eda14cbcSMatt Macy 984eda14cbcSMatt Macy set_current_state(TASK_INTERRUPTIBLE); 985eda14cbcSMatt Macy 986eda14cbcSMatt Macy } 987eda14cbcSMatt Macy 988eda14cbcSMatt Macy __set_current_state(TASK_RUNNING); 989eda14cbcSMatt Macy tq->tq_nthreads--; 990eda14cbcSMatt Macy list_del_init(&tqt->tqt_thread_list); 991eda14cbcSMatt Macy error: 992eda14cbcSMatt Macy kmem_free(tqt, sizeof (taskq_thread_t)); 993eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 994eda14cbcSMatt Macy 995eda14cbcSMatt Macy tsd_set(taskq_tsd, NULL); 996184c1b94SMartin Matuska thread_exit(); 997eda14cbcSMatt Macy 998eda14cbcSMatt Macy return (0); 999eda14cbcSMatt Macy } 1000eda14cbcSMatt Macy 1001eda14cbcSMatt Macy static taskq_thread_t * 1002eda14cbcSMatt Macy taskq_thread_create(taskq_t *tq) 1003eda14cbcSMatt Macy { 1004eda14cbcSMatt Macy static int last_used_cpu = 0; 1005eda14cbcSMatt Macy taskq_thread_t *tqt; 1006eda14cbcSMatt Macy 1007eda14cbcSMatt Macy tqt = kmem_alloc(sizeof (*tqt), KM_PUSHPAGE); 1008eda14cbcSMatt Macy INIT_LIST_HEAD(&tqt->tqt_thread_list); 1009eda14cbcSMatt Macy INIT_LIST_HEAD(&tqt->tqt_active_list); 1010eda14cbcSMatt Macy tqt->tqt_tq = tq; 1011eda14cbcSMatt Macy tqt->tqt_id = TASKQID_INVALID; 1012eda14cbcSMatt Macy 1013eda14cbcSMatt Macy tqt->tqt_thread = spl_kthread_create(taskq_thread, tqt, 1014eda14cbcSMatt Macy "%s", tq->tq_name); 1015eda14cbcSMatt Macy if (tqt->tqt_thread == NULL) { 1016eda14cbcSMatt Macy kmem_free(tqt, sizeof (taskq_thread_t)); 1017eda14cbcSMatt Macy return (NULL); 1018eda14cbcSMatt Macy } 1019eda14cbcSMatt Macy 1020eda14cbcSMatt Macy if (spl_taskq_thread_bind) { 1021eda14cbcSMatt Macy last_used_cpu = (last_used_cpu + 1) % num_online_cpus(); 1022eda14cbcSMatt Macy kthread_bind(tqt->tqt_thread, last_used_cpu); 1023eda14cbcSMatt Macy } 1024eda14cbcSMatt Macy 1025eda14cbcSMatt Macy if (spl_taskq_thread_priority) 1026eda14cbcSMatt Macy set_user_nice(tqt->tqt_thread, PRIO_TO_NICE(tq->tq_pri)); 1027eda14cbcSMatt Macy 1028eda14cbcSMatt Macy wake_up_process(tqt->tqt_thread); 1029eda14cbcSMatt Macy 1030eda14cbcSMatt Macy return (tqt); 1031eda14cbcSMatt Macy } 1032eda14cbcSMatt Macy 1033eda14cbcSMatt Macy taskq_t * 10347877fdebSMatt Macy taskq_create(const char *name, int threads_arg, pri_t pri, 1035eda14cbcSMatt Macy int minalloc, int maxalloc, uint_t flags) 1036eda14cbcSMatt Macy { 1037eda14cbcSMatt Macy taskq_t *tq; 1038eda14cbcSMatt Macy taskq_thread_t *tqt; 1039eda14cbcSMatt Macy int count = 0, rc = 0, i; 1040eda14cbcSMatt Macy unsigned long irqflags; 10417877fdebSMatt Macy int nthreads = threads_arg; 1042eda14cbcSMatt Macy 1043eda14cbcSMatt Macy ASSERT(name != NULL); 1044eda14cbcSMatt Macy ASSERT(minalloc >= 0); 1045eda14cbcSMatt Macy ASSERT(maxalloc <= INT_MAX); 1046eda14cbcSMatt Macy ASSERT(!(flags & (TASKQ_CPR_SAFE))); /* Unsupported */ 1047eda14cbcSMatt Macy 1048eda14cbcSMatt Macy /* Scale the number of threads using nthreads as a percentage */ 1049eda14cbcSMatt Macy if (flags & TASKQ_THREADS_CPU_PCT) { 1050eda14cbcSMatt Macy ASSERT(nthreads <= 100); 1051eda14cbcSMatt Macy ASSERT(nthreads >= 0); 10527877fdebSMatt Macy nthreads = MIN(threads_arg, 100); 1053eda14cbcSMatt Macy nthreads = MAX(nthreads, 0); 1054eda14cbcSMatt Macy nthreads = MAX((num_online_cpus() * nthreads) /100, 1); 1055eda14cbcSMatt Macy } 1056eda14cbcSMatt Macy 1057eda14cbcSMatt Macy tq = kmem_alloc(sizeof (*tq), KM_PUSHPAGE); 1058eda14cbcSMatt Macy if (tq == NULL) 1059eda14cbcSMatt Macy return (NULL); 1060eda14cbcSMatt Macy 10617877fdebSMatt Macy tq->tq_hp_support = B_FALSE; 10627877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG 10637877fdebSMatt Macy if (flags & TASKQ_THREADS_CPU_PCT) { 10647877fdebSMatt Macy tq->tq_hp_support = B_TRUE; 10657877fdebSMatt Macy if (cpuhp_state_add_instance_nocalls(spl_taskq_cpuhp_state, 10667877fdebSMatt Macy &tq->tq_hp_cb_node) != 0) { 10677877fdebSMatt Macy kmem_free(tq, sizeof (*tq)); 10687877fdebSMatt Macy return (NULL); 10697877fdebSMatt Macy } 10707877fdebSMatt Macy } 10717877fdebSMatt Macy #endif 10727877fdebSMatt Macy 1073eda14cbcSMatt Macy spin_lock_init(&tq->tq_lock); 1074eda14cbcSMatt Macy INIT_LIST_HEAD(&tq->tq_thread_list); 1075eda14cbcSMatt Macy INIT_LIST_HEAD(&tq->tq_active_list); 1076eda14cbcSMatt Macy tq->tq_name = kmem_strdup(name); 1077eda14cbcSMatt Macy tq->tq_nactive = 0; 1078eda14cbcSMatt Macy tq->tq_nthreads = 0; 1079eda14cbcSMatt Macy tq->tq_nspawn = 0; 1080eda14cbcSMatt Macy tq->tq_maxthreads = nthreads; 10817877fdebSMatt Macy tq->tq_cpu_pct = threads_arg; 1082eda14cbcSMatt Macy tq->tq_pri = pri; 1083eda14cbcSMatt Macy tq->tq_minalloc = minalloc; 1084eda14cbcSMatt Macy tq->tq_maxalloc = maxalloc; 1085eda14cbcSMatt Macy tq->tq_nalloc = 0; 1086eda14cbcSMatt Macy tq->tq_flags = (flags | TASKQ_ACTIVE); 1087eda14cbcSMatt Macy tq->tq_next_id = TASKQID_INITIAL; 1088eda14cbcSMatt Macy tq->tq_lowest_id = TASKQID_INITIAL; 1089eda14cbcSMatt Macy INIT_LIST_HEAD(&tq->tq_free_list); 1090eda14cbcSMatt Macy INIT_LIST_HEAD(&tq->tq_pend_list); 1091eda14cbcSMatt Macy INIT_LIST_HEAD(&tq->tq_prio_list); 1092eda14cbcSMatt Macy INIT_LIST_HEAD(&tq->tq_delay_list); 1093eda14cbcSMatt Macy init_waitqueue_head(&tq->tq_work_waitq); 1094eda14cbcSMatt Macy init_waitqueue_head(&tq->tq_wait_waitq); 1095eda14cbcSMatt Macy tq->tq_lock_class = TQ_LOCK_GENERAL; 1096eda14cbcSMatt Macy INIT_LIST_HEAD(&tq->tq_taskqs); 1097eda14cbcSMatt Macy 1098eda14cbcSMatt Macy if (flags & TASKQ_PREPOPULATE) { 1099eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, irqflags, 1100eda14cbcSMatt Macy tq->tq_lock_class); 1101eda14cbcSMatt Macy 1102eda14cbcSMatt Macy for (i = 0; i < minalloc; i++) 1103eda14cbcSMatt Macy task_done(tq, task_alloc(tq, TQ_PUSHPAGE | TQ_NEW, 1104eda14cbcSMatt Macy &irqflags)); 1105eda14cbcSMatt Macy 1106eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, irqflags); 1107eda14cbcSMatt Macy } 1108eda14cbcSMatt Macy 1109eda14cbcSMatt Macy if ((flags & TASKQ_DYNAMIC) && spl_taskq_thread_dynamic) 1110eda14cbcSMatt Macy nthreads = 1; 1111eda14cbcSMatt Macy 1112eda14cbcSMatt Macy for (i = 0; i < nthreads; i++) { 1113eda14cbcSMatt Macy tqt = taskq_thread_create(tq); 1114eda14cbcSMatt Macy if (tqt == NULL) 1115eda14cbcSMatt Macy rc = 1; 1116eda14cbcSMatt Macy else 1117eda14cbcSMatt Macy count++; 1118eda14cbcSMatt Macy } 1119eda14cbcSMatt Macy 1120eda14cbcSMatt Macy /* Wait for all threads to be started before potential destroy */ 1121eda14cbcSMatt Macy wait_event(tq->tq_wait_waitq, tq->tq_nthreads == count); 1122eda14cbcSMatt Macy /* 1123eda14cbcSMatt Macy * taskq_thread might have touched nspawn, but we don't want them to 1124eda14cbcSMatt Macy * because they're not dynamically spawned. So we reset it to 0 1125eda14cbcSMatt Macy */ 1126eda14cbcSMatt Macy tq->tq_nspawn = 0; 1127eda14cbcSMatt Macy 1128eda14cbcSMatt Macy if (rc) { 1129eda14cbcSMatt Macy taskq_destroy(tq); 1130eda14cbcSMatt Macy tq = NULL; 1131eda14cbcSMatt Macy } else { 1132eda14cbcSMatt Macy down_write(&tq_list_sem); 1133eda14cbcSMatt Macy tq->tq_instance = taskq_find_by_name(name) + 1; 1134eda14cbcSMatt Macy list_add_tail(&tq->tq_taskqs, &tq_list); 1135eda14cbcSMatt Macy up_write(&tq_list_sem); 1136eda14cbcSMatt Macy } 1137eda14cbcSMatt Macy 1138eda14cbcSMatt Macy return (tq); 1139eda14cbcSMatt Macy } 1140eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_create); 1141eda14cbcSMatt Macy 1142eda14cbcSMatt Macy void 1143eda14cbcSMatt Macy taskq_destroy(taskq_t *tq) 1144eda14cbcSMatt Macy { 1145eda14cbcSMatt Macy struct task_struct *thread; 1146eda14cbcSMatt Macy taskq_thread_t *tqt; 1147eda14cbcSMatt Macy taskq_ent_t *t; 1148eda14cbcSMatt Macy unsigned long flags; 1149eda14cbcSMatt Macy 1150eda14cbcSMatt Macy ASSERT(tq); 1151eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); 1152eda14cbcSMatt Macy tq->tq_flags &= ~TASKQ_ACTIVE; 1153eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 1154eda14cbcSMatt Macy 11557877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG 11567877fdebSMatt Macy if (tq->tq_hp_support) { 11577877fdebSMatt Macy VERIFY0(cpuhp_state_remove_instance_nocalls( 11587877fdebSMatt Macy spl_taskq_cpuhp_state, &tq->tq_hp_cb_node)); 11597877fdebSMatt Macy } 11607877fdebSMatt Macy #endif 1161eda14cbcSMatt Macy /* 1162eda14cbcSMatt Macy * When TASKQ_ACTIVE is clear new tasks may not be added nor may 1163eda14cbcSMatt Macy * new worker threads be spawned for dynamic taskq. 1164eda14cbcSMatt Macy */ 1165eda14cbcSMatt Macy if (dynamic_taskq != NULL) 1166eda14cbcSMatt Macy taskq_wait_outstanding(dynamic_taskq, 0); 1167eda14cbcSMatt Macy 1168eda14cbcSMatt Macy taskq_wait(tq); 1169eda14cbcSMatt Macy 1170eda14cbcSMatt Macy /* remove taskq from global list used by the kstats */ 1171eda14cbcSMatt Macy down_write(&tq_list_sem); 1172eda14cbcSMatt Macy list_del(&tq->tq_taskqs); 1173eda14cbcSMatt Macy up_write(&tq_list_sem); 1174eda14cbcSMatt Macy 1175eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); 1176eda14cbcSMatt Macy /* wait for spawning threads to insert themselves to the list */ 1177eda14cbcSMatt Macy while (tq->tq_nspawn) { 1178eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 1179eda14cbcSMatt Macy schedule_timeout_interruptible(1); 1180eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, 1181eda14cbcSMatt Macy tq->tq_lock_class); 1182eda14cbcSMatt Macy } 1183eda14cbcSMatt Macy 1184eda14cbcSMatt Macy /* 1185eda14cbcSMatt Macy * Signal each thread to exit and block until it does. Each thread 1186eda14cbcSMatt Macy * is responsible for removing itself from the list and freeing its 1187eda14cbcSMatt Macy * taskq_thread_t. This allows for idle threads to opt to remove 1188eda14cbcSMatt Macy * themselves from the taskq. They can be recreated as needed. 1189eda14cbcSMatt Macy */ 1190eda14cbcSMatt Macy while (!list_empty(&tq->tq_thread_list)) { 1191eda14cbcSMatt Macy tqt = list_entry(tq->tq_thread_list.next, 1192eda14cbcSMatt Macy taskq_thread_t, tqt_thread_list); 1193eda14cbcSMatt Macy thread = tqt->tqt_thread; 1194eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 1195eda14cbcSMatt Macy 1196eda14cbcSMatt Macy kthread_stop(thread); 1197eda14cbcSMatt Macy 1198eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, 1199eda14cbcSMatt Macy tq->tq_lock_class); 1200eda14cbcSMatt Macy } 1201eda14cbcSMatt Macy 1202eda14cbcSMatt Macy while (!list_empty(&tq->tq_free_list)) { 1203eda14cbcSMatt Macy t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list); 1204eda14cbcSMatt Macy 1205eda14cbcSMatt Macy ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC)); 1206eda14cbcSMatt Macy 1207eda14cbcSMatt Macy list_del_init(&t->tqent_list); 1208eda14cbcSMatt Macy task_free(tq, t); 1209eda14cbcSMatt Macy } 1210eda14cbcSMatt Macy 1211eda14cbcSMatt Macy ASSERT0(tq->tq_nthreads); 1212eda14cbcSMatt Macy ASSERT0(tq->tq_nalloc); 1213eda14cbcSMatt Macy ASSERT0(tq->tq_nspawn); 1214eda14cbcSMatt Macy ASSERT(list_empty(&tq->tq_thread_list)); 1215eda14cbcSMatt Macy ASSERT(list_empty(&tq->tq_active_list)); 1216eda14cbcSMatt Macy ASSERT(list_empty(&tq->tq_free_list)); 1217eda14cbcSMatt Macy ASSERT(list_empty(&tq->tq_pend_list)); 1218eda14cbcSMatt Macy ASSERT(list_empty(&tq->tq_prio_list)); 1219eda14cbcSMatt Macy ASSERT(list_empty(&tq->tq_delay_list)); 1220eda14cbcSMatt Macy 1221eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 1222eda14cbcSMatt Macy 1223eda14cbcSMatt Macy kmem_strfree(tq->tq_name); 1224eda14cbcSMatt Macy kmem_free(tq, sizeof (taskq_t)); 1225eda14cbcSMatt Macy } 1226eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_destroy); 1227eda14cbcSMatt Macy 1228eda14cbcSMatt Macy static unsigned int spl_taskq_kick = 0; 1229eda14cbcSMatt Macy 1230eda14cbcSMatt Macy /* 1231eda14cbcSMatt Macy * 2.6.36 API Change 1232eda14cbcSMatt Macy * module_param_cb is introduced to take kernel_param_ops and 1233eda14cbcSMatt Macy * module_param_call is marked as obsolete. Also set and get operations 1234eda14cbcSMatt Macy * were changed to take a 'const struct kernel_param *'. 1235eda14cbcSMatt Macy */ 1236eda14cbcSMatt Macy static int 1237eda14cbcSMatt Macy #ifdef module_param_cb 1238eda14cbcSMatt Macy param_set_taskq_kick(const char *val, const struct kernel_param *kp) 1239eda14cbcSMatt Macy #else 1240eda14cbcSMatt Macy param_set_taskq_kick(const char *val, struct kernel_param *kp) 1241eda14cbcSMatt Macy #endif 1242eda14cbcSMatt Macy { 1243eda14cbcSMatt Macy int ret; 1244eda14cbcSMatt Macy taskq_t *tq = NULL; 1245eda14cbcSMatt Macy taskq_ent_t *t; 1246eda14cbcSMatt Macy unsigned long flags; 1247eda14cbcSMatt Macy 1248eda14cbcSMatt Macy ret = param_set_uint(val, kp); 1249eda14cbcSMatt Macy if (ret < 0 || !spl_taskq_kick) 1250eda14cbcSMatt Macy return (ret); 1251eda14cbcSMatt Macy /* reset value */ 1252eda14cbcSMatt Macy spl_taskq_kick = 0; 1253eda14cbcSMatt Macy 1254eda14cbcSMatt Macy down_read(&tq_list_sem); 1255eda14cbcSMatt Macy list_for_each_entry(tq, &tq_list, tq_taskqs) { 1256eda14cbcSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, 1257eda14cbcSMatt Macy tq->tq_lock_class); 1258eda14cbcSMatt Macy /* Check if the first pending is older than 5 seconds */ 1259eda14cbcSMatt Macy t = taskq_next_ent(tq); 1260eda14cbcSMatt Macy if (t && time_after(jiffies, t->tqent_birth + 5*HZ)) { 1261eda14cbcSMatt Macy (void) taskq_thread_spawn(tq); 1262eda14cbcSMatt Macy printk(KERN_INFO "spl: Kicked taskq %s/%d\n", 1263eda14cbcSMatt Macy tq->tq_name, tq->tq_instance); 1264eda14cbcSMatt Macy } 1265eda14cbcSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 1266eda14cbcSMatt Macy } 1267eda14cbcSMatt Macy up_read(&tq_list_sem); 1268eda14cbcSMatt Macy return (ret); 1269eda14cbcSMatt Macy } 1270eda14cbcSMatt Macy 1271eda14cbcSMatt Macy #ifdef module_param_cb 1272eda14cbcSMatt Macy static const struct kernel_param_ops param_ops_taskq_kick = { 1273eda14cbcSMatt Macy .set = param_set_taskq_kick, 1274eda14cbcSMatt Macy .get = param_get_uint, 1275eda14cbcSMatt Macy }; 1276eda14cbcSMatt Macy module_param_cb(spl_taskq_kick, ¶m_ops_taskq_kick, &spl_taskq_kick, 0644); 1277eda14cbcSMatt Macy #else 1278eda14cbcSMatt Macy module_param_call(spl_taskq_kick, param_set_taskq_kick, param_get_uint, 1279eda14cbcSMatt Macy &spl_taskq_kick, 0644); 1280eda14cbcSMatt Macy #endif 1281eda14cbcSMatt Macy MODULE_PARM_DESC(spl_taskq_kick, 1282eda14cbcSMatt Macy "Write nonzero to kick stuck taskqs to spawn more threads"); 1283eda14cbcSMatt Macy 12847877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG 12857877fdebSMatt Macy /* 12867877fdebSMatt Macy * This callback will be called exactly once for each core that comes online, 12877877fdebSMatt Macy * for each dynamic taskq. We attempt to expand taskqs that have 12887877fdebSMatt Macy * TASKQ_THREADS_CPU_PCT set. We need to redo the percentage calculation every 12897877fdebSMatt Macy * time, to correctly determine whether or not to add a thread. 12907877fdebSMatt Macy */ 12917877fdebSMatt Macy static int 12927877fdebSMatt Macy spl_taskq_expand(unsigned int cpu, struct hlist_node *node) 12937877fdebSMatt Macy { 12947877fdebSMatt Macy taskq_t *tq = list_entry(node, taskq_t, tq_hp_cb_node); 12957877fdebSMatt Macy unsigned long flags; 12967877fdebSMatt Macy int err = 0; 12977877fdebSMatt Macy 12987877fdebSMatt Macy ASSERT(tq); 12997877fdebSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); 13007877fdebSMatt Macy 130181b22a98SMartin Matuska if (!(tq->tq_flags & TASKQ_ACTIVE)) { 130281b22a98SMartin Matuska spin_unlock_irqrestore(&tq->tq_lock, flags); 130381b22a98SMartin Matuska return (err); 130481b22a98SMartin Matuska } 13057877fdebSMatt Macy 13067877fdebSMatt Macy ASSERT(tq->tq_flags & TASKQ_THREADS_CPU_PCT); 13077877fdebSMatt Macy int nthreads = MIN(tq->tq_cpu_pct, 100); 13087877fdebSMatt Macy nthreads = MAX(((num_online_cpus() + 1) * nthreads) / 100, 1); 13097877fdebSMatt Macy tq->tq_maxthreads = nthreads; 13107877fdebSMatt Macy 13117877fdebSMatt Macy if (!((tq->tq_flags & TASKQ_DYNAMIC) && spl_taskq_thread_dynamic) && 13127877fdebSMatt Macy tq->tq_maxthreads > tq->tq_nthreads) { 131381b22a98SMartin Matuska spin_unlock_irqrestore(&tq->tq_lock, flags); 13147877fdebSMatt Macy taskq_thread_t *tqt = taskq_thread_create(tq); 13157877fdebSMatt Macy if (tqt == NULL) 13167877fdebSMatt Macy err = -1; 131781b22a98SMartin Matuska return (err); 13187877fdebSMatt Macy } 13197877fdebSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 13207877fdebSMatt Macy return (err); 13217877fdebSMatt Macy } 13227877fdebSMatt Macy 13237877fdebSMatt Macy /* 13247877fdebSMatt Macy * While we don't support offlining CPUs, it is possible that CPUs will fail 13257877fdebSMatt Macy * to online successfully. We do need to be able to handle this case 13267877fdebSMatt Macy * gracefully. 13277877fdebSMatt Macy */ 13287877fdebSMatt Macy static int 13297877fdebSMatt Macy spl_taskq_prepare_down(unsigned int cpu, struct hlist_node *node) 13307877fdebSMatt Macy { 13317877fdebSMatt Macy taskq_t *tq = list_entry(node, taskq_t, tq_hp_cb_node); 13327877fdebSMatt Macy unsigned long flags; 13337877fdebSMatt Macy 13347877fdebSMatt Macy ASSERT(tq); 13357877fdebSMatt Macy spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class); 13367877fdebSMatt Macy 13377877fdebSMatt Macy if (!(tq->tq_flags & TASKQ_ACTIVE)) 13387877fdebSMatt Macy goto out; 13397877fdebSMatt Macy 13407877fdebSMatt Macy ASSERT(tq->tq_flags & TASKQ_THREADS_CPU_PCT); 13417877fdebSMatt Macy int nthreads = MIN(tq->tq_cpu_pct, 100); 13427877fdebSMatt Macy nthreads = MAX(((num_online_cpus()) * nthreads) / 100, 1); 13437877fdebSMatt Macy tq->tq_maxthreads = nthreads; 13447877fdebSMatt Macy 13457877fdebSMatt Macy if (!((tq->tq_flags & TASKQ_DYNAMIC) && spl_taskq_thread_dynamic) && 13467877fdebSMatt Macy tq->tq_maxthreads < tq->tq_nthreads) { 13477877fdebSMatt Macy ASSERT3U(tq->tq_maxthreads, ==, tq->tq_nthreads - 1); 13487877fdebSMatt Macy taskq_thread_t *tqt = list_entry(tq->tq_thread_list.next, 13497877fdebSMatt Macy taskq_thread_t, tqt_thread_list); 13507877fdebSMatt Macy struct task_struct *thread = tqt->tqt_thread; 13517877fdebSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 13527877fdebSMatt Macy 13537877fdebSMatt Macy kthread_stop(thread); 13547877fdebSMatt Macy 13557877fdebSMatt Macy return (0); 13567877fdebSMatt Macy } 13577877fdebSMatt Macy 13587877fdebSMatt Macy out: 13597877fdebSMatt Macy spin_unlock_irqrestore(&tq->tq_lock, flags); 13607877fdebSMatt Macy return (0); 13617877fdebSMatt Macy } 13627877fdebSMatt Macy #endif 13637877fdebSMatt Macy 1364eda14cbcSMatt Macy int 1365eda14cbcSMatt Macy spl_taskq_init(void) 1366eda14cbcSMatt Macy { 1367eda14cbcSMatt Macy init_rwsem(&tq_list_sem); 1368eda14cbcSMatt Macy tsd_create(&taskq_tsd, NULL); 1369eda14cbcSMatt Macy 13707877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG 13717877fdebSMatt Macy spl_taskq_cpuhp_state = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, 13727877fdebSMatt Macy "fs/spl_taskq:online", spl_taskq_expand, spl_taskq_prepare_down); 13737877fdebSMatt Macy #endif 13747877fdebSMatt Macy 1375eda14cbcSMatt Macy system_taskq = taskq_create("spl_system_taskq", MAX(boot_ncpus, 64), 1376eda14cbcSMatt Macy maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE|TASKQ_DYNAMIC); 1377eda14cbcSMatt Macy if (system_taskq == NULL) 1378eda14cbcSMatt Macy return (1); 1379eda14cbcSMatt Macy 1380eda14cbcSMatt Macy system_delay_taskq = taskq_create("spl_delay_taskq", MAX(boot_ncpus, 4), 1381eda14cbcSMatt Macy maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE|TASKQ_DYNAMIC); 1382eda14cbcSMatt Macy if (system_delay_taskq == NULL) { 13837877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG 13847877fdebSMatt Macy cpuhp_remove_multi_state(spl_taskq_cpuhp_state); 13857877fdebSMatt Macy #endif 1386eda14cbcSMatt Macy taskq_destroy(system_taskq); 1387eda14cbcSMatt Macy return (1); 1388eda14cbcSMatt Macy } 1389eda14cbcSMatt Macy 1390eda14cbcSMatt Macy dynamic_taskq = taskq_create("spl_dynamic_taskq", 1, 1391eda14cbcSMatt Macy maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE); 1392eda14cbcSMatt Macy if (dynamic_taskq == NULL) { 13937877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG 13947877fdebSMatt Macy cpuhp_remove_multi_state(spl_taskq_cpuhp_state); 13957877fdebSMatt Macy #endif 1396eda14cbcSMatt Macy taskq_destroy(system_taskq); 1397eda14cbcSMatt Macy taskq_destroy(system_delay_taskq); 1398eda14cbcSMatt Macy return (1); 1399eda14cbcSMatt Macy } 1400eda14cbcSMatt Macy 1401eda14cbcSMatt Macy /* 1402eda14cbcSMatt Macy * This is used to annotate tq_lock, so 1403eda14cbcSMatt Macy * taskq_dispatch -> taskq_thread_spawn -> taskq_dispatch 1404eda14cbcSMatt Macy * does not trigger a lockdep warning re: possible recursive locking 1405eda14cbcSMatt Macy */ 1406eda14cbcSMatt Macy dynamic_taskq->tq_lock_class = TQ_LOCK_DYNAMIC; 1407eda14cbcSMatt Macy 1408eda14cbcSMatt Macy return (0); 1409eda14cbcSMatt Macy } 1410eda14cbcSMatt Macy 1411eda14cbcSMatt Macy void 1412eda14cbcSMatt Macy spl_taskq_fini(void) 1413eda14cbcSMatt Macy { 1414eda14cbcSMatt Macy taskq_destroy(dynamic_taskq); 1415eda14cbcSMatt Macy dynamic_taskq = NULL; 1416eda14cbcSMatt Macy 1417eda14cbcSMatt Macy taskq_destroy(system_delay_taskq); 1418eda14cbcSMatt Macy system_delay_taskq = NULL; 1419eda14cbcSMatt Macy 1420eda14cbcSMatt Macy taskq_destroy(system_taskq); 1421eda14cbcSMatt Macy system_taskq = NULL; 1422eda14cbcSMatt Macy 1423eda14cbcSMatt Macy tsd_destroy(&taskq_tsd); 14247877fdebSMatt Macy 14257877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG 14267877fdebSMatt Macy cpuhp_remove_multi_state(spl_taskq_cpuhp_state); 14277877fdebSMatt Macy spl_taskq_cpuhp_state = 0; 14287877fdebSMatt Macy #endif 1429eda14cbcSMatt Macy } 1430