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 
35e92ffd9bSMartin Matuska static 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 
39e2257b31SMartin Matuska static uint_t spl_taskq_thread_timeout_ms = 5000;
407b5e6873SMartin Matuska /* BEGIN CSTYLED */
417b5e6873SMartin Matuska module_param(spl_taskq_thread_timeout_ms, uint, 0644);
427b5e6873SMartin Matuska /* END CSTYLED */
437b5e6873SMartin Matuska MODULE_PARM_DESC(spl_taskq_thread_timeout_ms,
44e2257b31SMartin Matuska 	"Minimum idle threads exit interval for dynamic taskqs");
45eda14cbcSMatt Macy 
46e92ffd9bSMartin Matuska static int spl_taskq_thread_dynamic = 1;
477877fdebSMatt Macy module_param(spl_taskq_thread_dynamic, int, 0444);
48eda14cbcSMatt Macy MODULE_PARM_DESC(spl_taskq_thread_dynamic, "Allow dynamic taskq threads");
49eda14cbcSMatt Macy 
50e92ffd9bSMartin Matuska static int spl_taskq_thread_priority = 1;
51eda14cbcSMatt Macy module_param(spl_taskq_thread_priority, int, 0644);
52eda14cbcSMatt Macy MODULE_PARM_DESC(spl_taskq_thread_priority,
53eda14cbcSMatt Macy 	"Allow non-default priority for taskq threads");
54eda14cbcSMatt Macy 
55be181ee2SMartin Matuska static uint_t spl_taskq_thread_sequential = 4;
56be181ee2SMartin Matuska /* BEGIN CSTYLED */
57be181ee2SMartin Matuska module_param(spl_taskq_thread_sequential, uint, 0644);
58be181ee2SMartin Matuska /* END CSTYLED */
59eda14cbcSMatt Macy MODULE_PARM_DESC(spl_taskq_thread_sequential,
60eda14cbcSMatt Macy 	"Create new taskq threads after N sequential tasks");
61eda14cbcSMatt Macy 
62681ce946SMartin Matuska /*
63681ce946SMartin Matuska  * Global system-wide dynamic task queue available for all consumers. This
64681ce946SMartin Matuska  * taskq is not intended for long-running tasks; instead, a dedicated taskq
65681ce946SMartin Matuska  * should be created.
66681ce946SMartin Matuska  */
67eda14cbcSMatt Macy taskq_t *system_taskq;
68eda14cbcSMatt Macy EXPORT_SYMBOL(system_taskq);
69eda14cbcSMatt Macy /* Global dynamic task queue for long delay */
70eda14cbcSMatt Macy taskq_t *system_delay_taskq;
71eda14cbcSMatt Macy EXPORT_SYMBOL(system_delay_taskq);
72eda14cbcSMatt Macy 
73eda14cbcSMatt Macy /* Private dedicated taskq for creating new taskq threads on demand. */
74eda14cbcSMatt Macy static taskq_t *dynamic_taskq;
75eda14cbcSMatt Macy static taskq_thread_t *taskq_thread_create(taskq_t *);
76eda14cbcSMatt Macy 
777877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG
787877fdebSMatt Macy /* Multi-callback id for cpu hotplugging. */
797877fdebSMatt Macy static int spl_taskq_cpuhp_state;
807877fdebSMatt Macy #endif
817877fdebSMatt Macy 
82eda14cbcSMatt Macy /* List of all taskqs */
83eda14cbcSMatt Macy LIST_HEAD(tq_list);
84eda14cbcSMatt Macy struct rw_semaphore tq_list_sem;
85eda14cbcSMatt Macy static uint_t taskq_tsd;
86eda14cbcSMatt Macy 
87eda14cbcSMatt Macy static int
task_km_flags(uint_t flags)88eda14cbcSMatt Macy task_km_flags(uint_t flags)
89eda14cbcSMatt Macy {
90eda14cbcSMatt Macy 	if (flags & TQ_NOSLEEP)
91eda14cbcSMatt Macy 		return (KM_NOSLEEP);
92eda14cbcSMatt Macy 
93eda14cbcSMatt Macy 	if (flags & TQ_PUSHPAGE)
94eda14cbcSMatt Macy 		return (KM_PUSHPAGE);
95eda14cbcSMatt Macy 
96eda14cbcSMatt Macy 	return (KM_SLEEP);
97eda14cbcSMatt Macy }
98eda14cbcSMatt Macy 
99eda14cbcSMatt Macy /*
100eda14cbcSMatt Macy  * taskq_find_by_name - Find the largest instance number of a named taskq.
101eda14cbcSMatt Macy  */
102eda14cbcSMatt Macy static int
taskq_find_by_name(const char * name)103eda14cbcSMatt Macy taskq_find_by_name(const char *name)
104eda14cbcSMatt Macy {
105eda14cbcSMatt Macy 	struct list_head *tql = NULL;
106eda14cbcSMatt Macy 	taskq_t *tq;
107eda14cbcSMatt Macy 
108eda14cbcSMatt Macy 	list_for_each_prev(tql, &tq_list) {
109eda14cbcSMatt Macy 		tq = list_entry(tql, taskq_t, tq_taskqs);
110eda14cbcSMatt Macy 		if (strcmp(name, tq->tq_name) == 0)
111eda14cbcSMatt Macy 			return (tq->tq_instance);
112eda14cbcSMatt Macy 	}
113eda14cbcSMatt Macy 	return (-1);
114eda14cbcSMatt Macy }
115eda14cbcSMatt Macy 
116eda14cbcSMatt Macy /*
117eda14cbcSMatt Macy  * NOTE: Must be called with tq->tq_lock held, returns a list_t which
118eda14cbcSMatt Macy  * is not attached to the free, work, or pending taskq lists.
119eda14cbcSMatt Macy  */
120eda14cbcSMatt Macy static taskq_ent_t *
task_alloc(taskq_t * tq,uint_t flags,unsigned long * irqflags)121eda14cbcSMatt Macy task_alloc(taskq_t *tq, uint_t flags, unsigned long *irqflags)
122eda14cbcSMatt Macy {
123eda14cbcSMatt Macy 	taskq_ent_t *t;
124eda14cbcSMatt Macy 	int count = 0;
125eda14cbcSMatt Macy 
126eda14cbcSMatt Macy 	ASSERT(tq);
127eda14cbcSMatt Macy retry:
128eda14cbcSMatt Macy 	/* Acquire taskq_ent_t's from free list if available */
129eda14cbcSMatt Macy 	if (!list_empty(&tq->tq_free_list) && !(flags & TQ_NEW)) {
130eda14cbcSMatt Macy 		t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
131eda14cbcSMatt Macy 
132eda14cbcSMatt Macy 		ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
133eda14cbcSMatt Macy 		ASSERT(!(t->tqent_flags & TQENT_FLAG_CANCEL));
134eda14cbcSMatt Macy 		ASSERT(!timer_pending(&t->tqent_timer));
135eda14cbcSMatt Macy 
136eda14cbcSMatt Macy 		list_del_init(&t->tqent_list);
137eda14cbcSMatt Macy 		return (t);
138eda14cbcSMatt Macy 	}
139eda14cbcSMatt Macy 
140eda14cbcSMatt Macy 	/* Free list is empty and memory allocations are prohibited */
141eda14cbcSMatt Macy 	if (flags & TQ_NOALLOC)
142eda14cbcSMatt Macy 		return (NULL);
143eda14cbcSMatt Macy 
144eda14cbcSMatt Macy 	/* Hit maximum taskq_ent_t pool size */
145eda14cbcSMatt Macy 	if (tq->tq_nalloc >= tq->tq_maxalloc) {
146eda14cbcSMatt Macy 		if (flags & TQ_NOSLEEP)
147eda14cbcSMatt Macy 			return (NULL);
148eda14cbcSMatt Macy 
149eda14cbcSMatt Macy 		/*
150eda14cbcSMatt Macy 		 * Sleep periodically polling the free list for an available
151eda14cbcSMatt Macy 		 * taskq_ent_t. Dispatching with TQ_SLEEP should always succeed
152eda14cbcSMatt Macy 		 * but we cannot block forever waiting for an taskq_ent_t to
153eda14cbcSMatt Macy 		 * show up in the free list, otherwise a deadlock can happen.
154eda14cbcSMatt Macy 		 *
155eda14cbcSMatt Macy 		 * Therefore, we need to allocate a new task even if the number
156eda14cbcSMatt Macy 		 * of allocated tasks is above tq->tq_maxalloc, but we still
157eda14cbcSMatt Macy 		 * end up delaying the task allocation by one second, thereby
158eda14cbcSMatt Macy 		 * throttling the task dispatch rate.
159eda14cbcSMatt Macy 		 */
160eda14cbcSMatt Macy 		spin_unlock_irqrestore(&tq->tq_lock, *irqflags);
161eda14cbcSMatt Macy 		schedule_timeout(HZ / 100);
162eda14cbcSMatt Macy 		spin_lock_irqsave_nested(&tq->tq_lock, *irqflags,
163eda14cbcSMatt Macy 		    tq->tq_lock_class);
164eda14cbcSMatt Macy 		if (count < 100) {
165eda14cbcSMatt Macy 			count++;
166eda14cbcSMatt Macy 			goto retry;
167eda14cbcSMatt Macy 		}
168eda14cbcSMatt Macy 	}
169eda14cbcSMatt Macy 
170eda14cbcSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, *irqflags);
171eda14cbcSMatt Macy 	t = kmem_alloc(sizeof (taskq_ent_t), task_km_flags(flags));
172eda14cbcSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, *irqflags, tq->tq_lock_class);
173eda14cbcSMatt Macy 
174eda14cbcSMatt Macy 	if (t) {
175eda14cbcSMatt Macy 		taskq_init_ent(t);
176eda14cbcSMatt Macy 		tq->tq_nalloc++;
177eda14cbcSMatt Macy 	}
178eda14cbcSMatt Macy 
179eda14cbcSMatt Macy 	return (t);
180eda14cbcSMatt Macy }
181eda14cbcSMatt Macy 
182eda14cbcSMatt Macy /*
183eda14cbcSMatt Macy  * NOTE: Must be called with tq->tq_lock held, expects the taskq_ent_t
184eda14cbcSMatt Macy  * to already be removed from the free, work, or pending taskq lists.
185eda14cbcSMatt Macy  */
186eda14cbcSMatt Macy static void
task_free(taskq_t * tq,taskq_ent_t * t)187eda14cbcSMatt Macy task_free(taskq_t *tq, taskq_ent_t *t)
188eda14cbcSMatt Macy {
189eda14cbcSMatt Macy 	ASSERT(tq);
190eda14cbcSMatt Macy 	ASSERT(t);
191eda14cbcSMatt Macy 	ASSERT(list_empty(&t->tqent_list));
192eda14cbcSMatt Macy 	ASSERT(!timer_pending(&t->tqent_timer));
193eda14cbcSMatt Macy 
194eda14cbcSMatt Macy 	kmem_free(t, sizeof (taskq_ent_t));
195eda14cbcSMatt Macy 	tq->tq_nalloc--;
196eda14cbcSMatt Macy }
197eda14cbcSMatt Macy 
198eda14cbcSMatt Macy /*
199eda14cbcSMatt Macy  * NOTE: Must be called with tq->tq_lock held, either destroys the
200eda14cbcSMatt Macy  * taskq_ent_t if too many exist or moves it to the free list for later use.
201eda14cbcSMatt Macy  */
202eda14cbcSMatt Macy static void
task_done(taskq_t * tq,taskq_ent_t * t)203eda14cbcSMatt Macy task_done(taskq_t *tq, taskq_ent_t *t)
204eda14cbcSMatt Macy {
205eda14cbcSMatt Macy 	ASSERT(tq);
206eda14cbcSMatt Macy 	ASSERT(t);
207eda14cbcSMatt Macy 
208eda14cbcSMatt Macy 	/* Wake tasks blocked in taskq_wait_id() */
209eda14cbcSMatt Macy 	wake_up_all(&t->tqent_waitq);
210eda14cbcSMatt Macy 
211eda14cbcSMatt Macy 	list_del_init(&t->tqent_list);
212eda14cbcSMatt Macy 
213eda14cbcSMatt Macy 	if (tq->tq_nalloc <= tq->tq_minalloc) {
214eda14cbcSMatt Macy 		t->tqent_id = TASKQID_INVALID;
215eda14cbcSMatt Macy 		t->tqent_func = NULL;
216eda14cbcSMatt Macy 		t->tqent_arg = NULL;
217eda14cbcSMatt Macy 		t->tqent_flags = 0;
218eda14cbcSMatt Macy 
219eda14cbcSMatt Macy 		list_add_tail(&t->tqent_list, &tq->tq_free_list);
220eda14cbcSMatt Macy 	} else {
221eda14cbcSMatt Macy 		task_free(tq, t);
222eda14cbcSMatt Macy 	}
223eda14cbcSMatt Macy }
224eda14cbcSMatt Macy 
225eda14cbcSMatt Macy /*
226eda14cbcSMatt Macy  * When a delayed task timer expires remove it from the delay list and
227eda14cbcSMatt Macy  * add it to the priority list in order for immediate processing.
228eda14cbcSMatt Macy  */
229eda14cbcSMatt Macy static void
task_expire_impl(taskq_ent_t * t)230eda14cbcSMatt Macy task_expire_impl(taskq_ent_t *t)
231eda14cbcSMatt Macy {
232eda14cbcSMatt Macy 	taskq_ent_t *w;
233eda14cbcSMatt Macy 	taskq_t *tq = t->tqent_taskq;
234eda14cbcSMatt Macy 	struct list_head *l = NULL;
235eda14cbcSMatt Macy 	unsigned long flags;
236eda14cbcSMatt Macy 
237eda14cbcSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
238eda14cbcSMatt Macy 
239eda14cbcSMatt Macy 	if (t->tqent_flags & TQENT_FLAG_CANCEL) {
240eda14cbcSMatt Macy 		ASSERT(list_empty(&t->tqent_list));
241eda14cbcSMatt Macy 		spin_unlock_irqrestore(&tq->tq_lock, flags);
242eda14cbcSMatt Macy 		return;
243eda14cbcSMatt Macy 	}
244eda14cbcSMatt Macy 
245eda14cbcSMatt Macy 	t->tqent_birth = jiffies;
246eda14cbcSMatt Macy 	DTRACE_PROBE1(taskq_ent__birth, taskq_ent_t *, t);
247eda14cbcSMatt Macy 
248eda14cbcSMatt Macy 	/*
249eda14cbcSMatt Macy 	 * The priority list must be maintained in strict task id order
250eda14cbcSMatt Macy 	 * from lowest to highest for lowest_id to be easily calculable.
251eda14cbcSMatt Macy 	 */
252eda14cbcSMatt Macy 	list_del(&t->tqent_list);
253eda14cbcSMatt Macy 	list_for_each_prev(l, &tq->tq_prio_list) {
254eda14cbcSMatt Macy 		w = list_entry(l, taskq_ent_t, tqent_list);
255eda14cbcSMatt Macy 		if (w->tqent_id < t->tqent_id) {
256eda14cbcSMatt Macy 			list_add(&t->tqent_list, l);
257eda14cbcSMatt Macy 			break;
258eda14cbcSMatt Macy 		}
259eda14cbcSMatt Macy 	}
260eda14cbcSMatt Macy 	if (l == &tq->tq_prio_list)
261eda14cbcSMatt Macy 		list_add(&t->tqent_list, &tq->tq_prio_list);
262eda14cbcSMatt Macy 
263eda14cbcSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, flags);
264eda14cbcSMatt Macy 
265eda14cbcSMatt Macy 	wake_up(&tq->tq_work_waitq);
266eda14cbcSMatt Macy }
267eda14cbcSMatt Macy 
268eda14cbcSMatt Macy static void
task_expire(spl_timer_list_t tl)269eda14cbcSMatt Macy task_expire(spl_timer_list_t tl)
270eda14cbcSMatt Macy {
271eda14cbcSMatt Macy 	struct timer_list *tmr = (struct timer_list *)tl;
272eda14cbcSMatt Macy 	taskq_ent_t *t = from_timer(t, tmr, tqent_timer);
273eda14cbcSMatt Macy 	task_expire_impl(t);
274eda14cbcSMatt Macy }
275eda14cbcSMatt Macy 
276eda14cbcSMatt Macy /*
277eda14cbcSMatt Macy  * Returns the lowest incomplete taskqid_t.  The taskqid_t may
278eda14cbcSMatt Macy  * be queued on the pending list, on the priority list, on the
279eda14cbcSMatt Macy  * delay list, or on the work list currently being handled, but
280eda14cbcSMatt Macy  * it is not 100% complete yet.
281eda14cbcSMatt Macy  */
282eda14cbcSMatt Macy static taskqid_t
taskq_lowest_id(taskq_t * tq)283eda14cbcSMatt Macy taskq_lowest_id(taskq_t *tq)
284eda14cbcSMatt Macy {
285eda14cbcSMatt Macy 	taskqid_t lowest_id = tq->tq_next_id;
286eda14cbcSMatt Macy 	taskq_ent_t *t;
287eda14cbcSMatt Macy 	taskq_thread_t *tqt;
288eda14cbcSMatt Macy 
289eda14cbcSMatt Macy 	if (!list_empty(&tq->tq_pend_list)) {
290eda14cbcSMatt Macy 		t = list_entry(tq->tq_pend_list.next, taskq_ent_t, tqent_list);
291eda14cbcSMatt Macy 		lowest_id = MIN(lowest_id, t->tqent_id);
292eda14cbcSMatt Macy 	}
293eda14cbcSMatt Macy 
294eda14cbcSMatt Macy 	if (!list_empty(&tq->tq_prio_list)) {
295eda14cbcSMatt Macy 		t = list_entry(tq->tq_prio_list.next, taskq_ent_t, tqent_list);
296eda14cbcSMatt Macy 		lowest_id = MIN(lowest_id, t->tqent_id);
297eda14cbcSMatt Macy 	}
298eda14cbcSMatt Macy 
299eda14cbcSMatt Macy 	if (!list_empty(&tq->tq_delay_list)) {
300eda14cbcSMatt Macy 		t = list_entry(tq->tq_delay_list.next, taskq_ent_t, tqent_list);
301eda14cbcSMatt Macy 		lowest_id = MIN(lowest_id, t->tqent_id);
302eda14cbcSMatt Macy 	}
303eda14cbcSMatt Macy 
304eda14cbcSMatt Macy 	if (!list_empty(&tq->tq_active_list)) {
305eda14cbcSMatt Macy 		tqt = list_entry(tq->tq_active_list.next, taskq_thread_t,
306eda14cbcSMatt Macy 		    tqt_active_list);
307eda14cbcSMatt Macy 		ASSERT(tqt->tqt_id != TASKQID_INVALID);
308eda14cbcSMatt Macy 		lowest_id = MIN(lowest_id, tqt->tqt_id);
309eda14cbcSMatt Macy 	}
310eda14cbcSMatt Macy 
311eda14cbcSMatt Macy 	return (lowest_id);
312eda14cbcSMatt Macy }
313eda14cbcSMatt Macy 
314eda14cbcSMatt Macy /*
315eda14cbcSMatt Macy  * Insert a task into a list keeping the list sorted by increasing taskqid.
316eda14cbcSMatt Macy  */
317eda14cbcSMatt Macy static void
taskq_insert_in_order(taskq_t * tq,taskq_thread_t * tqt)318eda14cbcSMatt Macy taskq_insert_in_order(taskq_t *tq, taskq_thread_t *tqt)
319eda14cbcSMatt Macy {
320eda14cbcSMatt Macy 	taskq_thread_t *w;
321eda14cbcSMatt Macy 	struct list_head *l = NULL;
322eda14cbcSMatt Macy 
323eda14cbcSMatt Macy 	ASSERT(tq);
324eda14cbcSMatt Macy 	ASSERT(tqt);
325eda14cbcSMatt Macy 
326eda14cbcSMatt Macy 	list_for_each_prev(l, &tq->tq_active_list) {
327eda14cbcSMatt Macy 		w = list_entry(l, taskq_thread_t, tqt_active_list);
328eda14cbcSMatt Macy 		if (w->tqt_id < tqt->tqt_id) {
329eda14cbcSMatt Macy 			list_add(&tqt->tqt_active_list, l);
330eda14cbcSMatt Macy 			break;
331eda14cbcSMatt Macy 		}
332eda14cbcSMatt Macy 	}
333eda14cbcSMatt Macy 	if (l == &tq->tq_active_list)
334eda14cbcSMatt Macy 		list_add(&tqt->tqt_active_list, &tq->tq_active_list);
335eda14cbcSMatt Macy }
336eda14cbcSMatt Macy 
337eda14cbcSMatt Macy /*
338eda14cbcSMatt Macy  * Find and return a task from the given list if it exists.  The list
339eda14cbcSMatt Macy  * must be in lowest to highest task id order.
340eda14cbcSMatt Macy  */
341eda14cbcSMatt Macy static taskq_ent_t *
taskq_find_list(taskq_t * tq,struct list_head * lh,taskqid_t id)342eda14cbcSMatt Macy taskq_find_list(taskq_t *tq, struct list_head *lh, taskqid_t id)
343eda14cbcSMatt Macy {
344eda14cbcSMatt Macy 	struct list_head *l = NULL;
345eda14cbcSMatt Macy 	taskq_ent_t *t;
346eda14cbcSMatt Macy 
347eda14cbcSMatt Macy 	list_for_each(l, lh) {
348eda14cbcSMatt Macy 		t = list_entry(l, taskq_ent_t, tqent_list);
349eda14cbcSMatt Macy 
350eda14cbcSMatt Macy 		if (t->tqent_id == id)
351eda14cbcSMatt Macy 			return (t);
352eda14cbcSMatt Macy 
353eda14cbcSMatt Macy 		if (t->tqent_id > id)
354eda14cbcSMatt Macy 			break;
355eda14cbcSMatt Macy 	}
356eda14cbcSMatt Macy 
357eda14cbcSMatt Macy 	return (NULL);
358eda14cbcSMatt Macy }
359eda14cbcSMatt Macy 
360eda14cbcSMatt Macy /*
361eda14cbcSMatt Macy  * Find an already dispatched task given the task id regardless of what
362eda14cbcSMatt Macy  * state it is in.  If a task is still pending it will be returned.
363eda14cbcSMatt Macy  * If a task is executing, then -EBUSY will be returned instead.
364eda14cbcSMatt Macy  * If the task has already been run then NULL is returned.
365eda14cbcSMatt Macy  */
366eda14cbcSMatt Macy static taskq_ent_t *
taskq_find(taskq_t * tq,taskqid_t id)367eda14cbcSMatt Macy taskq_find(taskq_t *tq, taskqid_t id)
368eda14cbcSMatt Macy {
369eda14cbcSMatt Macy 	taskq_thread_t *tqt;
370eda14cbcSMatt Macy 	struct list_head *l = NULL;
371eda14cbcSMatt Macy 	taskq_ent_t *t;
372eda14cbcSMatt Macy 
373eda14cbcSMatt Macy 	t = taskq_find_list(tq, &tq->tq_delay_list, id);
374eda14cbcSMatt Macy 	if (t)
375eda14cbcSMatt Macy 		return (t);
376eda14cbcSMatt Macy 
377eda14cbcSMatt Macy 	t = taskq_find_list(tq, &tq->tq_prio_list, id);
378eda14cbcSMatt Macy 	if (t)
379eda14cbcSMatt Macy 		return (t);
380eda14cbcSMatt Macy 
381eda14cbcSMatt Macy 	t = taskq_find_list(tq, &tq->tq_pend_list, id);
382eda14cbcSMatt Macy 	if (t)
383eda14cbcSMatt Macy 		return (t);
384eda14cbcSMatt Macy 
385eda14cbcSMatt Macy 	list_for_each(l, &tq->tq_active_list) {
386eda14cbcSMatt Macy 		tqt = list_entry(l, taskq_thread_t, tqt_active_list);
387eda14cbcSMatt Macy 		if (tqt->tqt_id == id) {
388eda14cbcSMatt Macy 			/*
389eda14cbcSMatt Macy 			 * Instead of returning tqt_task, we just return a non
390eda14cbcSMatt Macy 			 * NULL value to prevent misuse, since tqt_task only
391eda14cbcSMatt Macy 			 * has two valid fields.
392eda14cbcSMatt Macy 			 */
393eda14cbcSMatt Macy 			return (ERR_PTR(-EBUSY));
394eda14cbcSMatt Macy 		}
395eda14cbcSMatt Macy 	}
396eda14cbcSMatt Macy 
397eda14cbcSMatt Macy 	return (NULL);
398eda14cbcSMatt Macy }
399eda14cbcSMatt Macy 
400eda14cbcSMatt Macy /*
401eda14cbcSMatt Macy  * Theory for the taskq_wait_id(), taskq_wait_outstanding(), and
402eda14cbcSMatt Macy  * taskq_wait() functions below.
403eda14cbcSMatt Macy  *
404eda14cbcSMatt Macy  * Taskq waiting is accomplished by tracking the lowest outstanding task
405eda14cbcSMatt Macy  * id and the next available task id.  As tasks are dispatched they are
406eda14cbcSMatt Macy  * added to the tail of the pending, priority, or delay lists.  As worker
407eda14cbcSMatt Macy  * threads become available the tasks are removed from the heads of these
408eda14cbcSMatt Macy  * lists and linked to the worker threads.  This ensures the lists are
409eda14cbcSMatt Macy  * kept sorted by lowest to highest task id.
410eda14cbcSMatt Macy  *
411eda14cbcSMatt Macy  * Therefore the lowest outstanding task id can be quickly determined by
412eda14cbcSMatt Macy  * checking the head item from all of these lists.  This value is stored
413eda14cbcSMatt Macy  * with the taskq as the lowest id.  It only needs to be recalculated when
414eda14cbcSMatt Macy  * either the task with the current lowest id completes or is canceled.
415eda14cbcSMatt Macy  *
416eda14cbcSMatt Macy  * By blocking until the lowest task id exceeds the passed task id the
417eda14cbcSMatt Macy  * taskq_wait_outstanding() function can be easily implemented.  Similarly,
418eda14cbcSMatt Macy  * by blocking until the lowest task id matches the next task id taskq_wait()
419eda14cbcSMatt Macy  * can be implemented.
420eda14cbcSMatt Macy  *
421eda14cbcSMatt Macy  * Callers should be aware that when there are multiple worked threads it
422eda14cbcSMatt Macy  * is possible for larger task ids to complete before smaller ones.  Also
423eda14cbcSMatt Macy  * when the taskq contains delay tasks with small task ids callers may
424eda14cbcSMatt Macy  * block for a considerable length of time waiting for them to expire and
425eda14cbcSMatt Macy  * execute.
426eda14cbcSMatt Macy  */
427eda14cbcSMatt Macy static int
taskq_wait_id_check(taskq_t * tq,taskqid_t id)428eda14cbcSMatt Macy taskq_wait_id_check(taskq_t *tq, taskqid_t id)
429eda14cbcSMatt Macy {
430eda14cbcSMatt Macy 	int rc;
431eda14cbcSMatt Macy 	unsigned long flags;
432eda14cbcSMatt Macy 
433eda14cbcSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
434eda14cbcSMatt Macy 	rc = (taskq_find(tq, id) == NULL);
435eda14cbcSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, flags);
436eda14cbcSMatt Macy 
437eda14cbcSMatt Macy 	return (rc);
438eda14cbcSMatt Macy }
439eda14cbcSMatt Macy 
440eda14cbcSMatt Macy /*
441eda14cbcSMatt Macy  * The taskq_wait_id() function blocks until the passed task id completes.
442eda14cbcSMatt Macy  * This does not guarantee that all lower task ids have completed.
443eda14cbcSMatt Macy  */
444eda14cbcSMatt Macy void
taskq_wait_id(taskq_t * tq,taskqid_t id)445eda14cbcSMatt Macy taskq_wait_id(taskq_t *tq, taskqid_t id)
446eda14cbcSMatt Macy {
447eda14cbcSMatt Macy 	wait_event(tq->tq_wait_waitq, taskq_wait_id_check(tq, id));
448eda14cbcSMatt Macy }
449eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_wait_id);
450eda14cbcSMatt Macy 
451eda14cbcSMatt Macy static int
taskq_wait_outstanding_check(taskq_t * tq,taskqid_t id)452eda14cbcSMatt Macy taskq_wait_outstanding_check(taskq_t *tq, taskqid_t id)
453eda14cbcSMatt Macy {
454eda14cbcSMatt Macy 	int rc;
455eda14cbcSMatt Macy 	unsigned long flags;
456eda14cbcSMatt Macy 
457eda14cbcSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
458eda14cbcSMatt Macy 	rc = (id < tq->tq_lowest_id);
459eda14cbcSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, flags);
460eda14cbcSMatt Macy 
461eda14cbcSMatt Macy 	return (rc);
462eda14cbcSMatt Macy }
463eda14cbcSMatt Macy 
464eda14cbcSMatt Macy /*
465eda14cbcSMatt Macy  * The taskq_wait_outstanding() function will block until all tasks with a
466eda14cbcSMatt Macy  * lower taskqid than the passed 'id' have been completed.  Note that all
467eda14cbcSMatt Macy  * task id's are assigned monotonically at dispatch time.  Zero may be
468eda14cbcSMatt Macy  * passed for the id to indicate all tasks dispatch up to this point,
469eda14cbcSMatt Macy  * but not after, should be waited for.
470eda14cbcSMatt Macy  */
471eda14cbcSMatt Macy void
taskq_wait_outstanding(taskq_t * tq,taskqid_t id)472eda14cbcSMatt Macy taskq_wait_outstanding(taskq_t *tq, taskqid_t id)
473eda14cbcSMatt Macy {
474eda14cbcSMatt Macy 	id = id ? id : tq->tq_next_id - 1;
475eda14cbcSMatt Macy 	wait_event(tq->tq_wait_waitq, taskq_wait_outstanding_check(tq, id));
476eda14cbcSMatt Macy }
477eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_wait_outstanding);
478eda14cbcSMatt Macy 
479eda14cbcSMatt Macy static int
taskq_wait_check(taskq_t * tq)480eda14cbcSMatt Macy taskq_wait_check(taskq_t *tq)
481eda14cbcSMatt Macy {
482eda14cbcSMatt Macy 	int rc;
483eda14cbcSMatt Macy 	unsigned long flags;
484eda14cbcSMatt Macy 
485eda14cbcSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
486eda14cbcSMatt Macy 	rc = (tq->tq_lowest_id == tq->tq_next_id);
487eda14cbcSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, flags);
488eda14cbcSMatt Macy 
489eda14cbcSMatt Macy 	return (rc);
490eda14cbcSMatt Macy }
491eda14cbcSMatt Macy 
492eda14cbcSMatt Macy /*
493eda14cbcSMatt Macy  * The taskq_wait() function will block until the taskq is empty.
494eda14cbcSMatt Macy  * This means that if a taskq re-dispatches work to itself taskq_wait()
495eda14cbcSMatt Macy  * callers will block indefinitely.
496eda14cbcSMatt Macy  */
497eda14cbcSMatt Macy void
taskq_wait(taskq_t * tq)498eda14cbcSMatt Macy taskq_wait(taskq_t *tq)
499eda14cbcSMatt Macy {
500eda14cbcSMatt Macy 	wait_event(tq->tq_wait_waitq, taskq_wait_check(tq));
501eda14cbcSMatt Macy }
502eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_wait);
503eda14cbcSMatt Macy 
504eda14cbcSMatt Macy int
taskq_member(taskq_t * tq,kthread_t * t)505eda14cbcSMatt Macy taskq_member(taskq_t *tq, kthread_t *t)
506eda14cbcSMatt Macy {
507eda14cbcSMatt Macy 	return (tq == (taskq_t *)tsd_get_by_thread(taskq_tsd, t));
508eda14cbcSMatt Macy }
509eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_member);
510eda14cbcSMatt Macy 
511eda14cbcSMatt Macy taskq_t *
taskq_of_curthread(void)512eda14cbcSMatt Macy taskq_of_curthread(void)
513eda14cbcSMatt Macy {
514eda14cbcSMatt Macy 	return (tsd_get(taskq_tsd));
515eda14cbcSMatt Macy }
516eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_of_curthread);
517eda14cbcSMatt Macy 
518eda14cbcSMatt Macy /*
519eda14cbcSMatt Macy  * Cancel an already dispatched task given the task id.  Still pending tasks
520eda14cbcSMatt Macy  * will be immediately canceled, and if the task is active the function will
521eda14cbcSMatt Macy  * block until it completes.  Preallocated tasks which are canceled must be
522eda14cbcSMatt Macy  * freed by the caller.
523eda14cbcSMatt Macy  */
524eda14cbcSMatt Macy int
taskq_cancel_id(taskq_t * tq,taskqid_t id)525eda14cbcSMatt Macy taskq_cancel_id(taskq_t *tq, taskqid_t id)
526eda14cbcSMatt Macy {
527eda14cbcSMatt Macy 	taskq_ent_t *t;
528eda14cbcSMatt Macy 	int rc = ENOENT;
529eda14cbcSMatt Macy 	unsigned long flags;
530eda14cbcSMatt Macy 
531eda14cbcSMatt Macy 	ASSERT(tq);
532eda14cbcSMatt Macy 
533eda14cbcSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
534eda14cbcSMatt Macy 	t = taskq_find(tq, id);
535eda14cbcSMatt Macy 	if (t && t != ERR_PTR(-EBUSY)) {
536eda14cbcSMatt Macy 		list_del_init(&t->tqent_list);
537eda14cbcSMatt Macy 		t->tqent_flags |= TQENT_FLAG_CANCEL;
538eda14cbcSMatt Macy 
539eda14cbcSMatt Macy 		/*
540eda14cbcSMatt Macy 		 * When canceling the lowest outstanding task id we
541eda14cbcSMatt Macy 		 * must recalculate the new lowest outstanding id.
542eda14cbcSMatt Macy 		 */
543eda14cbcSMatt Macy 		if (tq->tq_lowest_id == t->tqent_id) {
544eda14cbcSMatt Macy 			tq->tq_lowest_id = taskq_lowest_id(tq);
545eda14cbcSMatt Macy 			ASSERT3S(tq->tq_lowest_id, >, t->tqent_id);
546eda14cbcSMatt Macy 		}
547eda14cbcSMatt Macy 
548eda14cbcSMatt Macy 		/*
549eda14cbcSMatt Macy 		 * The task_expire() function takes the tq->tq_lock so drop
550eda14cbcSMatt Macy 		 * drop the lock before synchronously cancelling the timer.
551eda14cbcSMatt Macy 		 */
552eda14cbcSMatt Macy 		if (timer_pending(&t->tqent_timer)) {
553eda14cbcSMatt Macy 			spin_unlock_irqrestore(&tq->tq_lock, flags);
554eda14cbcSMatt Macy 			del_timer_sync(&t->tqent_timer);
555eda14cbcSMatt Macy 			spin_lock_irqsave_nested(&tq->tq_lock, flags,
556eda14cbcSMatt Macy 			    tq->tq_lock_class);
557eda14cbcSMatt Macy 		}
558eda14cbcSMatt Macy 
559eda14cbcSMatt Macy 		if (!(t->tqent_flags & TQENT_FLAG_PREALLOC))
560eda14cbcSMatt Macy 			task_done(tq, t);
561eda14cbcSMatt Macy 
562eda14cbcSMatt Macy 		rc = 0;
563eda14cbcSMatt Macy 	}
564eda14cbcSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, flags);
565eda14cbcSMatt Macy 
566eda14cbcSMatt Macy 	if (t == ERR_PTR(-EBUSY)) {
567eda14cbcSMatt Macy 		taskq_wait_id(tq, id);
568eda14cbcSMatt Macy 		rc = EBUSY;
569eda14cbcSMatt Macy 	}
570eda14cbcSMatt Macy 
571eda14cbcSMatt Macy 	return (rc);
572eda14cbcSMatt Macy }
573eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_cancel_id);
574eda14cbcSMatt Macy 
575eda14cbcSMatt Macy static int taskq_thread_spawn(taskq_t *tq);
576eda14cbcSMatt Macy 
577eda14cbcSMatt Macy taskqid_t
taskq_dispatch(taskq_t * tq,task_func_t func,void * arg,uint_t flags)578eda14cbcSMatt Macy taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
579eda14cbcSMatt Macy {
580eda14cbcSMatt Macy 	taskq_ent_t *t;
581eda14cbcSMatt Macy 	taskqid_t rc = TASKQID_INVALID;
582eda14cbcSMatt Macy 	unsigned long irqflags;
583eda14cbcSMatt Macy 
584eda14cbcSMatt Macy 	ASSERT(tq);
585eda14cbcSMatt Macy 	ASSERT(func);
586eda14cbcSMatt Macy 
587eda14cbcSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, irqflags, tq->tq_lock_class);
588eda14cbcSMatt Macy 
589eda14cbcSMatt Macy 	/* Taskq being destroyed and all tasks drained */
590eda14cbcSMatt Macy 	if (!(tq->tq_flags & TASKQ_ACTIVE))
591eda14cbcSMatt Macy 		goto out;
592eda14cbcSMatt Macy 
593eda14cbcSMatt Macy 	/* Do not queue the task unless there is idle thread for it */
594eda14cbcSMatt Macy 	ASSERT(tq->tq_nactive <= tq->tq_nthreads);
595eda14cbcSMatt Macy 	if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads)) {
596eda14cbcSMatt Macy 		/* Dynamic taskq may be able to spawn another thread */
597e2257b31SMartin Matuska 		if (taskq_thread_spawn(tq) == 0)
598eda14cbcSMatt Macy 			goto out;
599eda14cbcSMatt Macy 	}
600eda14cbcSMatt Macy 
601eda14cbcSMatt Macy 	if ((t = task_alloc(tq, flags, &irqflags)) == NULL)
602eda14cbcSMatt Macy 		goto out;
603eda14cbcSMatt Macy 
604eda14cbcSMatt Macy 	spin_lock(&t->tqent_lock);
605eda14cbcSMatt Macy 
606eda14cbcSMatt Macy 	/* Queue to the front of the list to enforce TQ_NOQUEUE semantics */
607eda14cbcSMatt Macy 	if (flags & TQ_NOQUEUE)
608eda14cbcSMatt Macy 		list_add(&t->tqent_list, &tq->tq_prio_list);
609eda14cbcSMatt Macy 	/* Queue to the priority list instead of the pending list */
610eda14cbcSMatt Macy 	else if (flags & TQ_FRONT)
611eda14cbcSMatt Macy 		list_add_tail(&t->tqent_list, &tq->tq_prio_list);
612eda14cbcSMatt Macy 	else
613eda14cbcSMatt Macy 		list_add_tail(&t->tqent_list, &tq->tq_pend_list);
614eda14cbcSMatt Macy 
615eda14cbcSMatt Macy 	t->tqent_id = rc = tq->tq_next_id;
616eda14cbcSMatt Macy 	tq->tq_next_id++;
617eda14cbcSMatt Macy 	t->tqent_func = func;
618eda14cbcSMatt Macy 	t->tqent_arg = arg;
619eda14cbcSMatt Macy 	t->tqent_taskq = tq;
620eda14cbcSMatt Macy 	t->tqent_timer.function = NULL;
621eda14cbcSMatt Macy 	t->tqent_timer.expires = 0;
622eda14cbcSMatt Macy 
623eda14cbcSMatt Macy 	t->tqent_birth = jiffies;
624eda14cbcSMatt Macy 	DTRACE_PROBE1(taskq_ent__birth, taskq_ent_t *, t);
625eda14cbcSMatt Macy 
626eda14cbcSMatt Macy 	ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
627eda14cbcSMatt Macy 
628eda14cbcSMatt Macy 	spin_unlock(&t->tqent_lock);
629eda14cbcSMatt Macy 
630eda14cbcSMatt Macy 	wake_up(&tq->tq_work_waitq);
631e2257b31SMartin Matuska 
632eda14cbcSMatt Macy 	/* Spawn additional taskq threads if required. */
633eda14cbcSMatt Macy 	if (!(flags & TQ_NOQUEUE) && tq->tq_nactive == tq->tq_nthreads)
634eda14cbcSMatt Macy 		(void) taskq_thread_spawn(tq);
635e2257b31SMartin Matuska out:
636eda14cbcSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, irqflags);
637eda14cbcSMatt Macy 	return (rc);
638eda14cbcSMatt Macy }
639eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_dispatch);
640eda14cbcSMatt Macy 
641eda14cbcSMatt Macy taskqid_t
taskq_dispatch_delay(taskq_t * tq,task_func_t func,void * arg,uint_t flags,clock_t expire_time)642eda14cbcSMatt Macy taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg,
643eda14cbcSMatt Macy     uint_t flags, clock_t expire_time)
644eda14cbcSMatt Macy {
645eda14cbcSMatt Macy 	taskqid_t rc = TASKQID_INVALID;
646eda14cbcSMatt Macy 	taskq_ent_t *t;
647eda14cbcSMatt Macy 	unsigned long irqflags;
648eda14cbcSMatt Macy 
649eda14cbcSMatt Macy 	ASSERT(tq);
650eda14cbcSMatt Macy 	ASSERT(func);
651eda14cbcSMatt Macy 
652eda14cbcSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, irqflags, tq->tq_lock_class);
653eda14cbcSMatt Macy 
654eda14cbcSMatt Macy 	/* Taskq being destroyed and all tasks drained */
655eda14cbcSMatt Macy 	if (!(tq->tq_flags & TASKQ_ACTIVE))
656eda14cbcSMatt Macy 		goto out;
657eda14cbcSMatt Macy 
658eda14cbcSMatt Macy 	if ((t = task_alloc(tq, flags, &irqflags)) == NULL)
659eda14cbcSMatt Macy 		goto out;
660eda14cbcSMatt Macy 
661eda14cbcSMatt Macy 	spin_lock(&t->tqent_lock);
662eda14cbcSMatt Macy 
663eda14cbcSMatt Macy 	/* Queue to the delay list for subsequent execution */
664eda14cbcSMatt Macy 	list_add_tail(&t->tqent_list, &tq->tq_delay_list);
665eda14cbcSMatt Macy 
666eda14cbcSMatt Macy 	t->tqent_id = rc = tq->tq_next_id;
667eda14cbcSMatt Macy 	tq->tq_next_id++;
668eda14cbcSMatt Macy 	t->tqent_func = func;
669eda14cbcSMatt Macy 	t->tqent_arg = arg;
670eda14cbcSMatt Macy 	t->tqent_taskq = tq;
671eda14cbcSMatt Macy 	t->tqent_timer.function = task_expire;
672eda14cbcSMatt Macy 	t->tqent_timer.expires = (unsigned long)expire_time;
673eda14cbcSMatt Macy 	add_timer(&t->tqent_timer);
674eda14cbcSMatt Macy 
675eda14cbcSMatt Macy 	ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
676eda14cbcSMatt Macy 
677eda14cbcSMatt Macy 	spin_unlock(&t->tqent_lock);
678e2257b31SMartin Matuska 
679eda14cbcSMatt Macy 	/* Spawn additional taskq threads if required. */
680eda14cbcSMatt Macy 	if (tq->tq_nactive == tq->tq_nthreads)
681eda14cbcSMatt Macy 		(void) taskq_thread_spawn(tq);
682e2257b31SMartin Matuska out:
683eda14cbcSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, irqflags);
684eda14cbcSMatt Macy 	return (rc);
685eda14cbcSMatt Macy }
686eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_dispatch_delay);
687eda14cbcSMatt Macy 
688eda14cbcSMatt Macy void
taskq_dispatch_ent(taskq_t * tq,task_func_t func,void * arg,uint_t flags,taskq_ent_t * t)689eda14cbcSMatt Macy taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
690eda14cbcSMatt Macy     taskq_ent_t *t)
691eda14cbcSMatt Macy {
692eda14cbcSMatt Macy 	unsigned long irqflags;
693eda14cbcSMatt Macy 	ASSERT(tq);
694eda14cbcSMatt Macy 	ASSERT(func);
695eda14cbcSMatt Macy 
696eda14cbcSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, irqflags,
697eda14cbcSMatt Macy 	    tq->tq_lock_class);
698eda14cbcSMatt Macy 
699eda14cbcSMatt Macy 	/* Taskq being destroyed and all tasks drained */
700eda14cbcSMatt Macy 	if (!(tq->tq_flags & TASKQ_ACTIVE)) {
701eda14cbcSMatt Macy 		t->tqent_id = TASKQID_INVALID;
702eda14cbcSMatt Macy 		goto out;
703eda14cbcSMatt Macy 	}
704eda14cbcSMatt Macy 
705eda14cbcSMatt Macy 	if ((flags & TQ_NOQUEUE) && (tq->tq_nactive == tq->tq_nthreads)) {
706eda14cbcSMatt Macy 		/* Dynamic taskq may be able to spawn another thread */
707e2257b31SMartin Matuska 		if (taskq_thread_spawn(tq) == 0)
708e2257b31SMartin Matuska 			goto out;
709eda14cbcSMatt Macy 		flags |= TQ_FRONT;
710eda14cbcSMatt Macy 	}
711eda14cbcSMatt Macy 
712eda14cbcSMatt Macy 	spin_lock(&t->tqent_lock);
713eda14cbcSMatt Macy 
714eda14cbcSMatt Macy 	/*
715eda14cbcSMatt Macy 	 * Make sure the entry is not on some other taskq; it is important to
716eda14cbcSMatt Macy 	 * ASSERT() under lock
717eda14cbcSMatt Macy 	 */
718eda14cbcSMatt Macy 	ASSERT(taskq_empty_ent(t));
719eda14cbcSMatt Macy 
720eda14cbcSMatt Macy 	/*
721eda14cbcSMatt Macy 	 * Mark it as a prealloc'd task.  This is important
722eda14cbcSMatt Macy 	 * to ensure that we don't free it later.
723eda14cbcSMatt Macy 	 */
724eda14cbcSMatt Macy 	t->tqent_flags |= TQENT_FLAG_PREALLOC;
725eda14cbcSMatt Macy 
726eda14cbcSMatt Macy 	/* Queue to the priority list instead of the pending list */
727eda14cbcSMatt Macy 	if (flags & TQ_FRONT)
728eda14cbcSMatt Macy 		list_add_tail(&t->tqent_list, &tq->tq_prio_list);
729eda14cbcSMatt Macy 	else
730eda14cbcSMatt Macy 		list_add_tail(&t->tqent_list, &tq->tq_pend_list);
731eda14cbcSMatt Macy 
732eda14cbcSMatt Macy 	t->tqent_id = tq->tq_next_id;
733eda14cbcSMatt Macy 	tq->tq_next_id++;
734eda14cbcSMatt Macy 	t->tqent_func = func;
735eda14cbcSMatt Macy 	t->tqent_arg = arg;
736eda14cbcSMatt Macy 	t->tqent_taskq = tq;
737eda14cbcSMatt Macy 
738eda14cbcSMatt Macy 	t->tqent_birth = jiffies;
739eda14cbcSMatt Macy 	DTRACE_PROBE1(taskq_ent__birth, taskq_ent_t *, t);
740eda14cbcSMatt Macy 
741eda14cbcSMatt Macy 	spin_unlock(&t->tqent_lock);
742eda14cbcSMatt Macy 
743eda14cbcSMatt Macy 	wake_up(&tq->tq_work_waitq);
744e2257b31SMartin Matuska 
745eda14cbcSMatt Macy 	/* Spawn additional taskq threads if required. */
746eda14cbcSMatt Macy 	if (tq->tq_nactive == tq->tq_nthreads)
747eda14cbcSMatt Macy 		(void) taskq_thread_spawn(tq);
748e2257b31SMartin Matuska out:
749eda14cbcSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, irqflags);
750eda14cbcSMatt Macy }
751eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_dispatch_ent);
752eda14cbcSMatt Macy 
753eda14cbcSMatt Macy int
taskq_empty_ent(taskq_ent_t * t)754eda14cbcSMatt Macy taskq_empty_ent(taskq_ent_t *t)
755eda14cbcSMatt Macy {
756eda14cbcSMatt Macy 	return (list_empty(&t->tqent_list));
757eda14cbcSMatt Macy }
758eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_empty_ent);
759eda14cbcSMatt Macy 
760eda14cbcSMatt Macy void
taskq_init_ent(taskq_ent_t * t)761eda14cbcSMatt Macy taskq_init_ent(taskq_ent_t *t)
762eda14cbcSMatt Macy {
763eda14cbcSMatt Macy 	spin_lock_init(&t->tqent_lock);
764eda14cbcSMatt Macy 	init_waitqueue_head(&t->tqent_waitq);
765eda14cbcSMatt Macy 	timer_setup(&t->tqent_timer, NULL, 0);
766eda14cbcSMatt Macy 	INIT_LIST_HEAD(&t->tqent_list);
767eda14cbcSMatt Macy 	t->tqent_id = 0;
768eda14cbcSMatt Macy 	t->tqent_func = NULL;
769eda14cbcSMatt Macy 	t->tqent_arg = NULL;
770eda14cbcSMatt Macy 	t->tqent_flags = 0;
771eda14cbcSMatt Macy 	t->tqent_taskq = NULL;
772eda14cbcSMatt Macy }
773eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_init_ent);
774eda14cbcSMatt Macy 
775eda14cbcSMatt Macy /*
776eda14cbcSMatt Macy  * Return the next pending task, preference is given to tasks on the
777eda14cbcSMatt Macy  * priority list which were dispatched with TQ_FRONT.
778eda14cbcSMatt Macy  */
779eda14cbcSMatt Macy static taskq_ent_t *
taskq_next_ent(taskq_t * tq)780eda14cbcSMatt Macy taskq_next_ent(taskq_t *tq)
781eda14cbcSMatt Macy {
782eda14cbcSMatt Macy 	struct list_head *list;
783eda14cbcSMatt Macy 
784eda14cbcSMatt Macy 	if (!list_empty(&tq->tq_prio_list))
785eda14cbcSMatt Macy 		list = &tq->tq_prio_list;
786eda14cbcSMatt Macy 	else if (!list_empty(&tq->tq_pend_list))
787eda14cbcSMatt Macy 		list = &tq->tq_pend_list;
788eda14cbcSMatt Macy 	else
789eda14cbcSMatt Macy 		return (NULL);
790eda14cbcSMatt Macy 
791eda14cbcSMatt Macy 	return (list_entry(list->next, taskq_ent_t, tqent_list));
792eda14cbcSMatt Macy }
793eda14cbcSMatt Macy 
794eda14cbcSMatt Macy /*
795eda14cbcSMatt Macy  * Spawns a new thread for the specified taskq.
796eda14cbcSMatt Macy  */
797eda14cbcSMatt Macy static void
taskq_thread_spawn_task(void * arg)798eda14cbcSMatt Macy taskq_thread_spawn_task(void *arg)
799eda14cbcSMatt Macy {
800eda14cbcSMatt Macy 	taskq_t *tq = (taskq_t *)arg;
801eda14cbcSMatt Macy 	unsigned long flags;
802eda14cbcSMatt Macy 
803eda14cbcSMatt Macy 	if (taskq_thread_create(tq) == NULL) {
804eda14cbcSMatt Macy 		/* restore spawning count if failed */
805eda14cbcSMatt Macy 		spin_lock_irqsave_nested(&tq->tq_lock, flags,
806eda14cbcSMatt Macy 		    tq->tq_lock_class);
807eda14cbcSMatt Macy 		tq->tq_nspawn--;
808eda14cbcSMatt Macy 		spin_unlock_irqrestore(&tq->tq_lock, flags);
809eda14cbcSMatt Macy 	}
810eda14cbcSMatt Macy }
811eda14cbcSMatt Macy 
812eda14cbcSMatt Macy /*
813eda14cbcSMatt Macy  * Spawn addition threads for dynamic taskqs (TASKQ_DYNAMIC) the current
814eda14cbcSMatt Macy  * number of threads is insufficient to handle the pending tasks.  These
815eda14cbcSMatt Macy  * new threads must be created by the dedicated dynamic_taskq to avoid
816eda14cbcSMatt Macy  * deadlocks between thread creation and memory reclaim.  The system_taskq
817eda14cbcSMatt Macy  * which is also a dynamic taskq cannot be safely used for this.
818eda14cbcSMatt Macy  */
819eda14cbcSMatt Macy static int
taskq_thread_spawn(taskq_t * tq)820eda14cbcSMatt Macy taskq_thread_spawn(taskq_t *tq)
821eda14cbcSMatt Macy {
822eda14cbcSMatt Macy 	int spawning = 0;
823eda14cbcSMatt Macy 
824eda14cbcSMatt Macy 	if (!(tq->tq_flags & TASKQ_DYNAMIC))
825eda14cbcSMatt Macy 		return (0);
826eda14cbcSMatt Macy 
827e2257b31SMartin Matuska 	tq->lastspawnstop = jiffies;
828eda14cbcSMatt Macy 	if ((tq->tq_nthreads + tq->tq_nspawn < tq->tq_maxthreads) &&
829eda14cbcSMatt Macy 	    (tq->tq_flags & TASKQ_ACTIVE)) {
830eda14cbcSMatt Macy 		spawning = (++tq->tq_nspawn);
831eda14cbcSMatt Macy 		taskq_dispatch(dynamic_taskq, taskq_thread_spawn_task,
832eda14cbcSMatt Macy 		    tq, TQ_NOSLEEP);
833eda14cbcSMatt Macy 	}
834eda14cbcSMatt Macy 
835eda14cbcSMatt Macy 	return (spawning);
836eda14cbcSMatt Macy }
837eda14cbcSMatt Macy 
838eda14cbcSMatt Macy /*
839e2257b31SMartin Matuska  * Threads in a dynamic taskq may exit once there is no more work to do.
840e2257b31SMartin Matuska  * To prevent threads from being created and destroyed too often limit
841e2257b31SMartin Matuska  * the exit rate to one per spl_taskq_thread_timeout_ms.
842eda14cbcSMatt Macy  *
843eda14cbcSMatt Macy  * The first thread is the thread list is treated as the primary thread.
844eda14cbcSMatt Macy  * There is nothing special about the primary thread but in order to avoid
845eda14cbcSMatt Macy  * all the taskq pids from changing we opt to make it long running.
846eda14cbcSMatt Macy  */
847eda14cbcSMatt Macy static int
taskq_thread_should_stop(taskq_t * tq,taskq_thread_t * tqt)848eda14cbcSMatt Macy taskq_thread_should_stop(taskq_t *tq, taskq_thread_t *tqt)
849eda14cbcSMatt Macy {
850e2257b31SMartin Matuska 	ASSERT(!taskq_next_ent(tq));
851e2257b31SMartin Matuska 	if (!(tq->tq_flags & TASKQ_DYNAMIC) || !spl_taskq_thread_dynamic)
852eda14cbcSMatt Macy 		return (0);
853e2257b31SMartin Matuska 	if (!(tq->tq_flags & TASKQ_ACTIVE))
854e2257b31SMartin Matuska 		return (1);
855eda14cbcSMatt Macy 	if (list_first_entry(&(tq->tq_thread_list), taskq_thread_t,
856eda14cbcSMatt Macy 	    tqt_thread_list) == tqt)
857eda14cbcSMatt Macy 		return (0);
858e2257b31SMartin Matuska 	ASSERT3U(tq->tq_nthreads, >, 1);
859e2257b31SMartin Matuska 	if (tq->tq_nspawn != 0)
860e2257b31SMartin Matuska 		return (0);
861e2257b31SMartin Matuska 	if (time_before(jiffies, tq->lastspawnstop +
8627b5e6873SMartin Matuska 	    msecs_to_jiffies(spl_taskq_thread_timeout_ms)))
863e2257b31SMartin Matuska 		return (0);
864e2257b31SMartin Matuska 	tq->lastspawnstop = jiffies;
8657b5e6873SMartin Matuska 	return (1);
866eda14cbcSMatt Macy }
867eda14cbcSMatt Macy 
868eda14cbcSMatt Macy static int
taskq_thread(void * args)869eda14cbcSMatt Macy taskq_thread(void *args)
870eda14cbcSMatt Macy {
871eda14cbcSMatt Macy 	DECLARE_WAITQUEUE(wait, current);
872eda14cbcSMatt Macy 	sigset_t blocked;
873eda14cbcSMatt Macy 	taskq_thread_t *tqt = args;
874eda14cbcSMatt Macy 	taskq_t *tq;
875eda14cbcSMatt Macy 	taskq_ent_t *t;
876eda14cbcSMatt Macy 	int seq_tasks = 0;
877eda14cbcSMatt Macy 	unsigned long flags;
878eda14cbcSMatt Macy 	taskq_ent_t dup_task = {};
879eda14cbcSMatt Macy 
880eda14cbcSMatt Macy 	ASSERT(tqt);
881eda14cbcSMatt Macy 	ASSERT(tqt->tqt_tq);
882eda14cbcSMatt Macy 	tq = tqt->tqt_tq;
883eda14cbcSMatt Macy 	current->flags |= PF_NOFREEZE;
884eda14cbcSMatt Macy 
885eda14cbcSMatt Macy 	(void) spl_fstrans_mark();
886eda14cbcSMatt Macy 
887eda14cbcSMatt Macy 	sigfillset(&blocked);
888eda14cbcSMatt Macy 	sigprocmask(SIG_BLOCK, &blocked, NULL);
889eda14cbcSMatt Macy 	flush_signals(current);
890eda14cbcSMatt Macy 
891eda14cbcSMatt Macy 	tsd_set(taskq_tsd, tq);
892eda14cbcSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
893eda14cbcSMatt Macy 	/*
894eda14cbcSMatt Macy 	 * If we are dynamically spawned, decrease spawning count. Note that
895eda14cbcSMatt Macy 	 * we could be created during taskq_create, in which case we shouldn't
896eda14cbcSMatt Macy 	 * do the decrement. But it's fine because taskq_create will reset
897eda14cbcSMatt Macy 	 * tq_nspawn later.
898eda14cbcSMatt Macy 	 */
899eda14cbcSMatt Macy 	if (tq->tq_flags & TASKQ_DYNAMIC)
900eda14cbcSMatt Macy 		tq->tq_nspawn--;
901eda14cbcSMatt Macy 
902eda14cbcSMatt Macy 	/* Immediately exit if more threads than allowed were created. */
903eda14cbcSMatt Macy 	if (tq->tq_nthreads >= tq->tq_maxthreads)
904eda14cbcSMatt Macy 		goto error;
905eda14cbcSMatt Macy 
906eda14cbcSMatt Macy 	tq->tq_nthreads++;
907eda14cbcSMatt Macy 	list_add_tail(&tqt->tqt_thread_list, &tq->tq_thread_list);
908eda14cbcSMatt Macy 	wake_up(&tq->tq_wait_waitq);
909eda14cbcSMatt Macy 	set_current_state(TASK_INTERRUPTIBLE);
910eda14cbcSMatt Macy 
911eda14cbcSMatt Macy 	while (!kthread_should_stop()) {
912eda14cbcSMatt Macy 
913eda14cbcSMatt Macy 		if (list_empty(&tq->tq_pend_list) &&
914eda14cbcSMatt Macy 		    list_empty(&tq->tq_prio_list)) {
915eda14cbcSMatt Macy 
916e2257b31SMartin Matuska 			if (taskq_thread_should_stop(tq, tqt))
917eda14cbcSMatt Macy 				break;
918eda14cbcSMatt Macy 
919eda14cbcSMatt Macy 			add_wait_queue_exclusive(&tq->tq_work_waitq, &wait);
920eda14cbcSMatt Macy 			spin_unlock_irqrestore(&tq->tq_lock, flags);
921eda14cbcSMatt Macy 
922eda14cbcSMatt Macy 			schedule();
923eda14cbcSMatt Macy 			seq_tasks = 0;
924eda14cbcSMatt Macy 
925eda14cbcSMatt Macy 			spin_lock_irqsave_nested(&tq->tq_lock, flags,
926eda14cbcSMatt Macy 			    tq->tq_lock_class);
927eda14cbcSMatt Macy 			remove_wait_queue(&tq->tq_work_waitq, &wait);
928eda14cbcSMatt Macy 		} else {
929eda14cbcSMatt Macy 			__set_current_state(TASK_RUNNING);
930eda14cbcSMatt Macy 		}
931eda14cbcSMatt Macy 
932eda14cbcSMatt Macy 		if ((t = taskq_next_ent(tq)) != NULL) {
933eda14cbcSMatt Macy 			list_del_init(&t->tqent_list);
934eda14cbcSMatt Macy 
935eda14cbcSMatt Macy 			/*
936eda14cbcSMatt Macy 			 * A TQENT_FLAG_PREALLOC task may be reused or freed
937eda14cbcSMatt Macy 			 * during the task function call. Store tqent_id and
938eda14cbcSMatt Macy 			 * tqent_flags here.
939eda14cbcSMatt Macy 			 *
940eda14cbcSMatt Macy 			 * Also use an on stack taskq_ent_t for tqt_task
941eda14cbcSMatt Macy 			 * assignment in this case; we want to make sure
942eda14cbcSMatt Macy 			 * to duplicate all fields, so the values are
943eda14cbcSMatt Macy 			 * correct when it's accessed via DTRACE_PROBE*.
944eda14cbcSMatt Macy 			 */
945eda14cbcSMatt Macy 			tqt->tqt_id = t->tqent_id;
946eda14cbcSMatt Macy 			tqt->tqt_flags = t->tqent_flags;
947eda14cbcSMatt Macy 
948eda14cbcSMatt Macy 			if (t->tqent_flags & TQENT_FLAG_PREALLOC) {
949eda14cbcSMatt Macy 				dup_task = *t;
950eda14cbcSMatt Macy 				t = &dup_task;
951eda14cbcSMatt Macy 			}
952eda14cbcSMatt Macy 			tqt->tqt_task = t;
953eda14cbcSMatt Macy 
954eda14cbcSMatt Macy 			taskq_insert_in_order(tq, tqt);
955eda14cbcSMatt Macy 			tq->tq_nactive++;
956eda14cbcSMatt Macy 			spin_unlock_irqrestore(&tq->tq_lock, flags);
957eda14cbcSMatt Macy 
958eda14cbcSMatt Macy 			DTRACE_PROBE1(taskq_ent__start, taskq_ent_t *, t);
959eda14cbcSMatt Macy 
960eda14cbcSMatt Macy 			/* Perform the requested task */
961eda14cbcSMatt Macy 			t->tqent_func(t->tqent_arg);
962eda14cbcSMatt Macy 
963eda14cbcSMatt Macy 			DTRACE_PROBE1(taskq_ent__finish, taskq_ent_t *, t);
964eda14cbcSMatt Macy 
965eda14cbcSMatt Macy 			spin_lock_irqsave_nested(&tq->tq_lock, flags,
966eda14cbcSMatt Macy 			    tq->tq_lock_class);
967eda14cbcSMatt Macy 			tq->tq_nactive--;
968eda14cbcSMatt Macy 			list_del_init(&tqt->tqt_active_list);
969eda14cbcSMatt Macy 			tqt->tqt_task = NULL;
970eda14cbcSMatt Macy 
971eda14cbcSMatt Macy 			/* For prealloc'd tasks, we don't free anything. */
972eda14cbcSMatt Macy 			if (!(tqt->tqt_flags & TQENT_FLAG_PREALLOC))
973eda14cbcSMatt Macy 				task_done(tq, t);
974eda14cbcSMatt Macy 
975eda14cbcSMatt Macy 			/*
976eda14cbcSMatt Macy 			 * When the current lowest outstanding taskqid is
977eda14cbcSMatt Macy 			 * done calculate the new lowest outstanding id
978eda14cbcSMatt Macy 			 */
979eda14cbcSMatt Macy 			if (tq->tq_lowest_id == tqt->tqt_id) {
980eda14cbcSMatt Macy 				tq->tq_lowest_id = taskq_lowest_id(tq);
981eda14cbcSMatt Macy 				ASSERT3S(tq->tq_lowest_id, >, tqt->tqt_id);
982eda14cbcSMatt Macy 			}
983eda14cbcSMatt Macy 
984eda14cbcSMatt Macy 			/* Spawn additional taskq threads if required. */
985eda14cbcSMatt Macy 			if ((++seq_tasks) > spl_taskq_thread_sequential &&
986eda14cbcSMatt Macy 			    taskq_thread_spawn(tq))
987eda14cbcSMatt Macy 				seq_tasks = 0;
988eda14cbcSMatt Macy 
989eda14cbcSMatt Macy 			tqt->tqt_id = TASKQID_INVALID;
990eda14cbcSMatt Macy 			tqt->tqt_flags = 0;
991eda14cbcSMatt Macy 			wake_up_all(&tq->tq_wait_waitq);
992eda14cbcSMatt Macy 		}
993eda14cbcSMatt Macy 
994eda14cbcSMatt Macy 		set_current_state(TASK_INTERRUPTIBLE);
995eda14cbcSMatt Macy 
996eda14cbcSMatt Macy 	}
997eda14cbcSMatt Macy 
998eda14cbcSMatt Macy 	__set_current_state(TASK_RUNNING);
999eda14cbcSMatt Macy 	tq->tq_nthreads--;
1000eda14cbcSMatt Macy 	list_del_init(&tqt->tqt_thread_list);
1001eda14cbcSMatt Macy error:
1002eda14cbcSMatt Macy 	kmem_free(tqt, sizeof (taskq_thread_t));
1003eda14cbcSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, flags);
1004eda14cbcSMatt Macy 
1005eda14cbcSMatt Macy 	tsd_set(taskq_tsd, NULL);
1006184c1b94SMartin Matuska 	thread_exit();
1007eda14cbcSMatt Macy 
1008eda14cbcSMatt Macy 	return (0);
1009eda14cbcSMatt Macy }
1010eda14cbcSMatt Macy 
1011eda14cbcSMatt Macy static taskq_thread_t *
taskq_thread_create(taskq_t * tq)1012eda14cbcSMatt Macy taskq_thread_create(taskq_t *tq)
1013eda14cbcSMatt Macy {
1014eda14cbcSMatt Macy 	static int last_used_cpu = 0;
1015eda14cbcSMatt Macy 	taskq_thread_t *tqt;
1016eda14cbcSMatt Macy 
1017eda14cbcSMatt Macy 	tqt = kmem_alloc(sizeof (*tqt), KM_PUSHPAGE);
1018eda14cbcSMatt Macy 	INIT_LIST_HEAD(&tqt->tqt_thread_list);
1019eda14cbcSMatt Macy 	INIT_LIST_HEAD(&tqt->tqt_active_list);
1020eda14cbcSMatt Macy 	tqt->tqt_tq = tq;
1021eda14cbcSMatt Macy 	tqt->tqt_id = TASKQID_INVALID;
1022eda14cbcSMatt Macy 
1023eda14cbcSMatt Macy 	tqt->tqt_thread = spl_kthread_create(taskq_thread, tqt,
1024eda14cbcSMatt Macy 	    "%s", tq->tq_name);
1025eda14cbcSMatt Macy 	if (tqt->tqt_thread == NULL) {
1026eda14cbcSMatt Macy 		kmem_free(tqt, sizeof (taskq_thread_t));
1027eda14cbcSMatt Macy 		return (NULL);
1028eda14cbcSMatt Macy 	}
1029eda14cbcSMatt Macy 
1030eda14cbcSMatt Macy 	if (spl_taskq_thread_bind) {
1031eda14cbcSMatt Macy 		last_used_cpu = (last_used_cpu + 1) % num_online_cpus();
1032eda14cbcSMatt Macy 		kthread_bind(tqt->tqt_thread, last_used_cpu);
1033eda14cbcSMatt Macy 	}
1034eda14cbcSMatt Macy 
1035eda14cbcSMatt Macy 	if (spl_taskq_thread_priority)
1036eda14cbcSMatt Macy 		set_user_nice(tqt->tqt_thread, PRIO_TO_NICE(tq->tq_pri));
1037eda14cbcSMatt Macy 
1038eda14cbcSMatt Macy 	wake_up_process(tqt->tqt_thread);
1039eda14cbcSMatt Macy 
1040eda14cbcSMatt Macy 	return (tqt);
1041eda14cbcSMatt Macy }
1042eda14cbcSMatt Macy 
1043eda14cbcSMatt Macy taskq_t *
taskq_create(const char * name,int threads_arg,pri_t pri,int minalloc,int maxalloc,uint_t flags)10447877fdebSMatt Macy taskq_create(const char *name, int threads_arg, pri_t pri,
1045eda14cbcSMatt Macy     int minalloc, int maxalloc, uint_t flags)
1046eda14cbcSMatt Macy {
1047eda14cbcSMatt Macy 	taskq_t *tq;
1048eda14cbcSMatt Macy 	taskq_thread_t *tqt;
1049eda14cbcSMatt Macy 	int count = 0, rc = 0, i;
1050eda14cbcSMatt Macy 	unsigned long irqflags;
10517877fdebSMatt Macy 	int nthreads = threads_arg;
1052eda14cbcSMatt Macy 
1053eda14cbcSMatt Macy 	ASSERT(name != NULL);
1054eda14cbcSMatt Macy 	ASSERT(minalloc >= 0);
1055eda14cbcSMatt Macy 	ASSERT(!(flags & (TASKQ_CPR_SAFE))); /* Unsupported */
1056eda14cbcSMatt Macy 
1057eda14cbcSMatt Macy 	/* Scale the number of threads using nthreads as a percentage */
1058eda14cbcSMatt Macy 	if (flags & TASKQ_THREADS_CPU_PCT) {
1059eda14cbcSMatt Macy 		ASSERT(nthreads <= 100);
1060eda14cbcSMatt Macy 		ASSERT(nthreads >= 0);
10617877fdebSMatt Macy 		nthreads = MIN(threads_arg, 100);
1062eda14cbcSMatt Macy 		nthreads = MAX(nthreads, 0);
1063eda14cbcSMatt Macy 		nthreads = MAX((num_online_cpus() * nthreads) /100, 1);
1064eda14cbcSMatt Macy 	}
1065eda14cbcSMatt Macy 
1066eda14cbcSMatt Macy 	tq = kmem_alloc(sizeof (*tq), KM_PUSHPAGE);
1067eda14cbcSMatt Macy 	if (tq == NULL)
1068eda14cbcSMatt Macy 		return (NULL);
1069eda14cbcSMatt Macy 
10707877fdebSMatt Macy 	tq->tq_hp_support = B_FALSE;
10717877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG
10727877fdebSMatt Macy 	if (flags & TASKQ_THREADS_CPU_PCT) {
10737877fdebSMatt Macy 		tq->tq_hp_support = B_TRUE;
10747877fdebSMatt Macy 		if (cpuhp_state_add_instance_nocalls(spl_taskq_cpuhp_state,
10757877fdebSMatt Macy 		    &tq->tq_hp_cb_node) != 0) {
10767877fdebSMatt Macy 			kmem_free(tq, sizeof (*tq));
10777877fdebSMatt Macy 			return (NULL);
10787877fdebSMatt Macy 		}
10797877fdebSMatt Macy 	}
10807877fdebSMatt Macy #endif
10817877fdebSMatt Macy 
1082eda14cbcSMatt Macy 	spin_lock_init(&tq->tq_lock);
1083eda14cbcSMatt Macy 	INIT_LIST_HEAD(&tq->tq_thread_list);
1084eda14cbcSMatt Macy 	INIT_LIST_HEAD(&tq->tq_active_list);
1085eda14cbcSMatt Macy 	tq->tq_name = kmem_strdup(name);
1086eda14cbcSMatt Macy 	tq->tq_nactive = 0;
1087eda14cbcSMatt Macy 	tq->tq_nthreads = 0;
1088eda14cbcSMatt Macy 	tq->tq_nspawn = 0;
1089eda14cbcSMatt Macy 	tq->tq_maxthreads = nthreads;
10907877fdebSMatt Macy 	tq->tq_cpu_pct = threads_arg;
1091eda14cbcSMatt Macy 	tq->tq_pri = pri;
1092eda14cbcSMatt Macy 	tq->tq_minalloc = minalloc;
1093eda14cbcSMatt Macy 	tq->tq_maxalloc = maxalloc;
1094eda14cbcSMatt Macy 	tq->tq_nalloc = 0;
1095eda14cbcSMatt Macy 	tq->tq_flags = (flags | TASKQ_ACTIVE);
1096eda14cbcSMatt Macy 	tq->tq_next_id = TASKQID_INITIAL;
1097eda14cbcSMatt Macy 	tq->tq_lowest_id = TASKQID_INITIAL;
1098e2257b31SMartin Matuska 	tq->lastspawnstop = jiffies;
1099eda14cbcSMatt Macy 	INIT_LIST_HEAD(&tq->tq_free_list);
1100eda14cbcSMatt Macy 	INIT_LIST_HEAD(&tq->tq_pend_list);
1101eda14cbcSMatt Macy 	INIT_LIST_HEAD(&tq->tq_prio_list);
1102eda14cbcSMatt Macy 	INIT_LIST_HEAD(&tq->tq_delay_list);
1103eda14cbcSMatt Macy 	init_waitqueue_head(&tq->tq_work_waitq);
1104eda14cbcSMatt Macy 	init_waitqueue_head(&tq->tq_wait_waitq);
1105eda14cbcSMatt Macy 	tq->tq_lock_class = TQ_LOCK_GENERAL;
1106eda14cbcSMatt Macy 	INIT_LIST_HEAD(&tq->tq_taskqs);
1107eda14cbcSMatt Macy 
1108eda14cbcSMatt Macy 	if (flags & TASKQ_PREPOPULATE) {
1109eda14cbcSMatt Macy 		spin_lock_irqsave_nested(&tq->tq_lock, irqflags,
1110eda14cbcSMatt Macy 		    tq->tq_lock_class);
1111eda14cbcSMatt Macy 
1112eda14cbcSMatt Macy 		for (i = 0; i < minalloc; i++)
1113eda14cbcSMatt Macy 			task_done(tq, task_alloc(tq, TQ_PUSHPAGE | TQ_NEW,
1114eda14cbcSMatt Macy 			    &irqflags));
1115eda14cbcSMatt Macy 
1116eda14cbcSMatt Macy 		spin_unlock_irqrestore(&tq->tq_lock, irqflags);
1117eda14cbcSMatt Macy 	}
1118eda14cbcSMatt Macy 
1119eda14cbcSMatt Macy 	if ((flags & TASKQ_DYNAMIC) && spl_taskq_thread_dynamic)
1120eda14cbcSMatt Macy 		nthreads = 1;
1121eda14cbcSMatt Macy 
1122eda14cbcSMatt Macy 	for (i = 0; i < nthreads; i++) {
1123eda14cbcSMatt Macy 		tqt = taskq_thread_create(tq);
1124eda14cbcSMatt Macy 		if (tqt == NULL)
1125eda14cbcSMatt Macy 			rc = 1;
1126eda14cbcSMatt Macy 		else
1127eda14cbcSMatt Macy 			count++;
1128eda14cbcSMatt Macy 	}
1129eda14cbcSMatt Macy 
1130eda14cbcSMatt Macy 	/* Wait for all threads to be started before potential destroy */
1131eda14cbcSMatt Macy 	wait_event(tq->tq_wait_waitq, tq->tq_nthreads == count);
1132eda14cbcSMatt Macy 	/*
1133eda14cbcSMatt Macy 	 * taskq_thread might have touched nspawn, but we don't want them to
1134eda14cbcSMatt Macy 	 * because they're not dynamically spawned. So we reset it to 0
1135eda14cbcSMatt Macy 	 */
1136eda14cbcSMatt Macy 	tq->tq_nspawn = 0;
1137eda14cbcSMatt Macy 
1138eda14cbcSMatt Macy 	if (rc) {
1139eda14cbcSMatt Macy 		taskq_destroy(tq);
1140eda14cbcSMatt Macy 		tq = NULL;
1141eda14cbcSMatt Macy 	} else {
1142eda14cbcSMatt Macy 		down_write(&tq_list_sem);
1143eda14cbcSMatt Macy 		tq->tq_instance = taskq_find_by_name(name) + 1;
1144eda14cbcSMatt Macy 		list_add_tail(&tq->tq_taskqs, &tq_list);
1145eda14cbcSMatt Macy 		up_write(&tq_list_sem);
1146eda14cbcSMatt Macy 	}
1147eda14cbcSMatt Macy 
1148eda14cbcSMatt Macy 	return (tq);
1149eda14cbcSMatt Macy }
1150eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_create);
1151eda14cbcSMatt Macy 
1152eda14cbcSMatt Macy void
taskq_destroy(taskq_t * tq)1153eda14cbcSMatt Macy taskq_destroy(taskq_t *tq)
1154eda14cbcSMatt Macy {
1155eda14cbcSMatt Macy 	struct task_struct *thread;
1156eda14cbcSMatt Macy 	taskq_thread_t *tqt;
1157eda14cbcSMatt Macy 	taskq_ent_t *t;
1158eda14cbcSMatt Macy 	unsigned long flags;
1159eda14cbcSMatt Macy 
1160eda14cbcSMatt Macy 	ASSERT(tq);
1161eda14cbcSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
1162eda14cbcSMatt Macy 	tq->tq_flags &= ~TASKQ_ACTIVE;
1163eda14cbcSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, flags);
1164eda14cbcSMatt Macy 
11657877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG
11667877fdebSMatt Macy 	if (tq->tq_hp_support) {
11677877fdebSMatt Macy 		VERIFY0(cpuhp_state_remove_instance_nocalls(
11687877fdebSMatt Macy 		    spl_taskq_cpuhp_state, &tq->tq_hp_cb_node));
11697877fdebSMatt Macy 	}
11707877fdebSMatt Macy #endif
1171eda14cbcSMatt Macy 	/*
1172eda14cbcSMatt Macy 	 * When TASKQ_ACTIVE is clear new tasks may not be added nor may
1173eda14cbcSMatt Macy 	 * new worker threads be spawned for dynamic taskq.
1174eda14cbcSMatt Macy 	 */
1175eda14cbcSMatt Macy 	if (dynamic_taskq != NULL)
1176eda14cbcSMatt Macy 		taskq_wait_outstanding(dynamic_taskq, 0);
1177eda14cbcSMatt Macy 
1178eda14cbcSMatt Macy 	taskq_wait(tq);
1179eda14cbcSMatt Macy 
1180eda14cbcSMatt Macy 	/* remove taskq from global list used by the kstats */
1181eda14cbcSMatt Macy 	down_write(&tq_list_sem);
1182eda14cbcSMatt Macy 	list_del(&tq->tq_taskqs);
1183eda14cbcSMatt Macy 	up_write(&tq_list_sem);
1184eda14cbcSMatt Macy 
1185eda14cbcSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
1186eda14cbcSMatt Macy 	/* wait for spawning threads to insert themselves to the list */
1187eda14cbcSMatt Macy 	while (tq->tq_nspawn) {
1188eda14cbcSMatt Macy 		spin_unlock_irqrestore(&tq->tq_lock, flags);
1189eda14cbcSMatt Macy 		schedule_timeout_interruptible(1);
1190eda14cbcSMatt Macy 		spin_lock_irqsave_nested(&tq->tq_lock, flags,
1191eda14cbcSMatt Macy 		    tq->tq_lock_class);
1192eda14cbcSMatt Macy 	}
1193eda14cbcSMatt Macy 
1194eda14cbcSMatt Macy 	/*
1195eda14cbcSMatt Macy 	 * Signal each thread to exit and block until it does.  Each thread
1196eda14cbcSMatt Macy 	 * is responsible for removing itself from the list and freeing its
1197eda14cbcSMatt Macy 	 * taskq_thread_t.  This allows for idle threads to opt to remove
1198eda14cbcSMatt Macy 	 * themselves from the taskq.  They can be recreated as needed.
1199eda14cbcSMatt Macy 	 */
1200eda14cbcSMatt Macy 	while (!list_empty(&tq->tq_thread_list)) {
1201eda14cbcSMatt Macy 		tqt = list_entry(tq->tq_thread_list.next,
1202eda14cbcSMatt Macy 		    taskq_thread_t, tqt_thread_list);
1203eda14cbcSMatt Macy 		thread = tqt->tqt_thread;
1204eda14cbcSMatt Macy 		spin_unlock_irqrestore(&tq->tq_lock, flags);
1205eda14cbcSMatt Macy 
1206eda14cbcSMatt Macy 		kthread_stop(thread);
1207eda14cbcSMatt Macy 
1208eda14cbcSMatt Macy 		spin_lock_irqsave_nested(&tq->tq_lock, flags,
1209eda14cbcSMatt Macy 		    tq->tq_lock_class);
1210eda14cbcSMatt Macy 	}
1211eda14cbcSMatt Macy 
1212eda14cbcSMatt Macy 	while (!list_empty(&tq->tq_free_list)) {
1213eda14cbcSMatt Macy 		t = list_entry(tq->tq_free_list.next, taskq_ent_t, tqent_list);
1214eda14cbcSMatt Macy 
1215eda14cbcSMatt Macy 		ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
1216eda14cbcSMatt Macy 
1217eda14cbcSMatt Macy 		list_del_init(&t->tqent_list);
1218eda14cbcSMatt Macy 		task_free(tq, t);
1219eda14cbcSMatt Macy 	}
1220eda14cbcSMatt Macy 
1221eda14cbcSMatt Macy 	ASSERT0(tq->tq_nthreads);
1222eda14cbcSMatt Macy 	ASSERT0(tq->tq_nalloc);
1223eda14cbcSMatt Macy 	ASSERT0(tq->tq_nspawn);
1224eda14cbcSMatt Macy 	ASSERT(list_empty(&tq->tq_thread_list));
1225eda14cbcSMatt Macy 	ASSERT(list_empty(&tq->tq_active_list));
1226eda14cbcSMatt Macy 	ASSERT(list_empty(&tq->tq_free_list));
1227eda14cbcSMatt Macy 	ASSERT(list_empty(&tq->tq_pend_list));
1228eda14cbcSMatt Macy 	ASSERT(list_empty(&tq->tq_prio_list));
1229eda14cbcSMatt Macy 	ASSERT(list_empty(&tq->tq_delay_list));
1230eda14cbcSMatt Macy 
1231eda14cbcSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, flags);
1232eda14cbcSMatt Macy 
1233eda14cbcSMatt Macy 	kmem_strfree(tq->tq_name);
1234eda14cbcSMatt Macy 	kmem_free(tq, sizeof (taskq_t));
1235eda14cbcSMatt Macy }
1236eda14cbcSMatt Macy EXPORT_SYMBOL(taskq_destroy);
1237eda14cbcSMatt Macy 
123814c2e0a0SMartin Matuska /*
123914c2e0a0SMartin Matuska  * Create a taskq with a specified number of pool threads. Allocate
124014c2e0a0SMartin Matuska  * and return an array of nthreads kthread_t pointers, one for each
124114c2e0a0SMartin Matuska  * thread in the pool. The array is not ordered and must be freed
124214c2e0a0SMartin Matuska  * by the caller.
124314c2e0a0SMartin Matuska  */
124414c2e0a0SMartin Matuska taskq_t *
taskq_create_synced(const char * name,int nthreads,pri_t pri,int minalloc,int maxalloc,uint_t flags,kthread_t *** ktpp)124514c2e0a0SMartin Matuska taskq_create_synced(const char *name, int nthreads, pri_t pri,
124614c2e0a0SMartin Matuska     int minalloc, int maxalloc, uint_t flags, kthread_t ***ktpp)
124714c2e0a0SMartin Matuska {
124814c2e0a0SMartin Matuska 	taskq_t *tq;
124914c2e0a0SMartin Matuska 	taskq_thread_t *tqt;
125014c2e0a0SMartin Matuska 	int i = 0;
125114c2e0a0SMartin Matuska 	kthread_t **kthreads = kmem_zalloc(sizeof (*kthreads) * nthreads,
125214c2e0a0SMartin Matuska 	    KM_SLEEP);
125314c2e0a0SMartin Matuska 
125414c2e0a0SMartin Matuska 	flags &= ~(TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT | TASKQ_DC_BATCH);
125514c2e0a0SMartin Matuska 
125614c2e0a0SMartin Matuska 	/* taskq_create spawns all the threads before returning */
125714c2e0a0SMartin Matuska 	tq = taskq_create(name, nthreads, minclsyspri, nthreads, INT_MAX,
125814c2e0a0SMartin Matuska 	    flags | TASKQ_PREPOPULATE);
125914c2e0a0SMartin Matuska 	VERIFY(tq != NULL);
126014c2e0a0SMartin Matuska 	VERIFY(tq->tq_nthreads == nthreads);
126114c2e0a0SMartin Matuska 
126214c2e0a0SMartin Matuska 	list_for_each_entry(tqt, &tq->tq_thread_list, tqt_thread_list) {
126314c2e0a0SMartin Matuska 		kthreads[i] = tqt->tqt_thread;
126414c2e0a0SMartin Matuska 		i++;
126514c2e0a0SMartin Matuska 	}
126614c2e0a0SMartin Matuska 
126714c2e0a0SMartin Matuska 	ASSERT3S(i, ==, nthreads);
126814c2e0a0SMartin Matuska 	*ktpp = kthreads;
126914c2e0a0SMartin Matuska 
127014c2e0a0SMartin Matuska 	return (tq);
127114c2e0a0SMartin Matuska }
127214c2e0a0SMartin Matuska EXPORT_SYMBOL(taskq_create_synced);
127314c2e0a0SMartin Matuska 
1274eda14cbcSMatt Macy static unsigned int spl_taskq_kick = 0;
1275eda14cbcSMatt Macy 
1276eda14cbcSMatt Macy /*
1277eda14cbcSMatt Macy  * 2.6.36 API Change
1278eda14cbcSMatt Macy  * module_param_cb is introduced to take kernel_param_ops and
1279eda14cbcSMatt Macy  * module_param_call is marked as obsolete. Also set and get operations
1280eda14cbcSMatt Macy  * were changed to take a 'const struct kernel_param *'.
1281eda14cbcSMatt Macy  */
1282eda14cbcSMatt Macy static int
1283eda14cbcSMatt Macy #ifdef module_param_cb
param_set_taskq_kick(const char * val,const struct kernel_param * kp)1284eda14cbcSMatt Macy param_set_taskq_kick(const char *val, const struct kernel_param *kp)
1285eda14cbcSMatt Macy #else
1286eda14cbcSMatt Macy param_set_taskq_kick(const char *val, struct kernel_param *kp)
1287eda14cbcSMatt Macy #endif
1288eda14cbcSMatt Macy {
1289eda14cbcSMatt Macy 	int ret;
1290eda14cbcSMatt Macy 	taskq_t *tq = NULL;
1291eda14cbcSMatt Macy 	taskq_ent_t *t;
1292eda14cbcSMatt Macy 	unsigned long flags;
1293eda14cbcSMatt Macy 
1294eda14cbcSMatt Macy 	ret = param_set_uint(val, kp);
1295eda14cbcSMatt Macy 	if (ret < 0 || !spl_taskq_kick)
1296eda14cbcSMatt Macy 		return (ret);
1297eda14cbcSMatt Macy 	/* reset value */
1298eda14cbcSMatt Macy 	spl_taskq_kick = 0;
1299eda14cbcSMatt Macy 
1300eda14cbcSMatt Macy 	down_read(&tq_list_sem);
1301eda14cbcSMatt Macy 	list_for_each_entry(tq, &tq_list, tq_taskqs) {
1302eda14cbcSMatt Macy 		spin_lock_irqsave_nested(&tq->tq_lock, flags,
1303eda14cbcSMatt Macy 		    tq->tq_lock_class);
1304eda14cbcSMatt Macy 		/* Check if the first pending is older than 5 seconds */
1305eda14cbcSMatt Macy 		t = taskq_next_ent(tq);
1306eda14cbcSMatt Macy 		if (t && time_after(jiffies, t->tqent_birth + 5*HZ)) {
1307eda14cbcSMatt Macy 			(void) taskq_thread_spawn(tq);
1308eda14cbcSMatt Macy 			printk(KERN_INFO "spl: Kicked taskq %s/%d\n",
1309eda14cbcSMatt Macy 			    tq->tq_name, tq->tq_instance);
1310eda14cbcSMatt Macy 		}
1311eda14cbcSMatt Macy 		spin_unlock_irqrestore(&tq->tq_lock, flags);
1312eda14cbcSMatt Macy 	}
1313eda14cbcSMatt Macy 	up_read(&tq_list_sem);
1314eda14cbcSMatt Macy 	return (ret);
1315eda14cbcSMatt Macy }
1316eda14cbcSMatt Macy 
1317eda14cbcSMatt Macy #ifdef module_param_cb
1318eda14cbcSMatt Macy static const struct kernel_param_ops param_ops_taskq_kick = {
1319eda14cbcSMatt Macy 	.set = param_set_taskq_kick,
1320eda14cbcSMatt Macy 	.get = param_get_uint,
1321eda14cbcSMatt Macy };
1322eda14cbcSMatt Macy module_param_cb(spl_taskq_kick, &param_ops_taskq_kick, &spl_taskq_kick, 0644);
1323eda14cbcSMatt Macy #else
1324eda14cbcSMatt Macy module_param_call(spl_taskq_kick, param_set_taskq_kick, param_get_uint,
1325eda14cbcSMatt Macy 	&spl_taskq_kick, 0644);
1326eda14cbcSMatt Macy #endif
1327eda14cbcSMatt Macy MODULE_PARM_DESC(spl_taskq_kick,
1328eda14cbcSMatt Macy 	"Write nonzero to kick stuck taskqs to spawn more threads");
1329eda14cbcSMatt Macy 
13307877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG
13317877fdebSMatt Macy /*
13327877fdebSMatt Macy  * This callback will be called exactly once for each core that comes online,
13337877fdebSMatt Macy  * for each dynamic taskq. We attempt to expand taskqs that have
13347877fdebSMatt Macy  * TASKQ_THREADS_CPU_PCT set. We need to redo the percentage calculation every
13357877fdebSMatt Macy  * time, to correctly determine whether or not to add a thread.
13367877fdebSMatt Macy  */
13377877fdebSMatt Macy static int
spl_taskq_expand(unsigned int cpu,struct hlist_node * node)13387877fdebSMatt Macy spl_taskq_expand(unsigned int cpu, struct hlist_node *node)
13397877fdebSMatt Macy {
13407877fdebSMatt Macy 	taskq_t *tq = list_entry(node, taskq_t, tq_hp_cb_node);
13417877fdebSMatt Macy 	unsigned long flags;
13427877fdebSMatt Macy 	int err = 0;
13437877fdebSMatt Macy 
13447877fdebSMatt Macy 	ASSERT(tq);
13457877fdebSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
13467877fdebSMatt Macy 
134781b22a98SMartin Matuska 	if (!(tq->tq_flags & TASKQ_ACTIVE)) {
134881b22a98SMartin Matuska 		spin_unlock_irqrestore(&tq->tq_lock, flags);
134981b22a98SMartin Matuska 		return (err);
135081b22a98SMartin Matuska 	}
13517877fdebSMatt Macy 
13527877fdebSMatt Macy 	ASSERT(tq->tq_flags & TASKQ_THREADS_CPU_PCT);
13537877fdebSMatt Macy 	int nthreads = MIN(tq->tq_cpu_pct, 100);
13547877fdebSMatt Macy 	nthreads = MAX(((num_online_cpus() + 1) * nthreads) / 100, 1);
13557877fdebSMatt Macy 	tq->tq_maxthreads = nthreads;
13567877fdebSMatt Macy 
13577877fdebSMatt Macy 	if (!((tq->tq_flags & TASKQ_DYNAMIC) && spl_taskq_thread_dynamic) &&
13587877fdebSMatt Macy 	    tq->tq_maxthreads > tq->tq_nthreads) {
135981b22a98SMartin Matuska 		spin_unlock_irqrestore(&tq->tq_lock, flags);
13607877fdebSMatt Macy 		taskq_thread_t *tqt = taskq_thread_create(tq);
13617877fdebSMatt Macy 		if (tqt == NULL)
13627877fdebSMatt Macy 			err = -1;
136381b22a98SMartin Matuska 		return (err);
13647877fdebSMatt Macy 	}
13657877fdebSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, flags);
13667877fdebSMatt Macy 	return (err);
13677877fdebSMatt Macy }
13687877fdebSMatt Macy 
13697877fdebSMatt Macy /*
13707877fdebSMatt Macy  * While we don't support offlining CPUs, it is possible that CPUs will fail
13717877fdebSMatt Macy  * to online successfully. We do need to be able to handle this case
13727877fdebSMatt Macy  * gracefully.
13737877fdebSMatt Macy  */
13747877fdebSMatt Macy static int
spl_taskq_prepare_down(unsigned int cpu,struct hlist_node * node)13757877fdebSMatt Macy spl_taskq_prepare_down(unsigned int cpu, struct hlist_node *node)
13767877fdebSMatt Macy {
13777877fdebSMatt Macy 	taskq_t *tq = list_entry(node, taskq_t, tq_hp_cb_node);
13787877fdebSMatt Macy 	unsigned long flags;
13797877fdebSMatt Macy 
13807877fdebSMatt Macy 	ASSERT(tq);
13817877fdebSMatt Macy 	spin_lock_irqsave_nested(&tq->tq_lock, flags, tq->tq_lock_class);
13827877fdebSMatt Macy 
13837877fdebSMatt Macy 	if (!(tq->tq_flags & TASKQ_ACTIVE))
13847877fdebSMatt Macy 		goto out;
13857877fdebSMatt Macy 
13867877fdebSMatt Macy 	ASSERT(tq->tq_flags & TASKQ_THREADS_CPU_PCT);
13877877fdebSMatt Macy 	int nthreads = MIN(tq->tq_cpu_pct, 100);
13887877fdebSMatt Macy 	nthreads = MAX(((num_online_cpus()) * nthreads) / 100, 1);
13897877fdebSMatt Macy 	tq->tq_maxthreads = nthreads;
13907877fdebSMatt Macy 
13917877fdebSMatt Macy 	if (!((tq->tq_flags & TASKQ_DYNAMIC) && spl_taskq_thread_dynamic) &&
13927877fdebSMatt Macy 	    tq->tq_maxthreads < tq->tq_nthreads) {
13937877fdebSMatt Macy 		ASSERT3U(tq->tq_maxthreads, ==, tq->tq_nthreads - 1);
13947877fdebSMatt Macy 		taskq_thread_t *tqt = list_entry(tq->tq_thread_list.next,
13957877fdebSMatt Macy 		    taskq_thread_t, tqt_thread_list);
13967877fdebSMatt Macy 		struct task_struct *thread = tqt->tqt_thread;
13977877fdebSMatt Macy 		spin_unlock_irqrestore(&tq->tq_lock, flags);
13987877fdebSMatt Macy 
13997877fdebSMatt Macy 		kthread_stop(thread);
14007877fdebSMatt Macy 
14017877fdebSMatt Macy 		return (0);
14027877fdebSMatt Macy 	}
14037877fdebSMatt Macy 
14047877fdebSMatt Macy out:
14057877fdebSMatt Macy 	spin_unlock_irqrestore(&tq->tq_lock, flags);
14067877fdebSMatt Macy 	return (0);
14077877fdebSMatt Macy }
14087877fdebSMatt Macy #endif
14097877fdebSMatt Macy 
1410eda14cbcSMatt Macy int
spl_taskq_init(void)1411eda14cbcSMatt Macy spl_taskq_init(void)
1412eda14cbcSMatt Macy {
1413eda14cbcSMatt Macy 	init_rwsem(&tq_list_sem);
1414eda14cbcSMatt Macy 	tsd_create(&taskq_tsd, NULL);
1415eda14cbcSMatt Macy 
14167877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG
14177877fdebSMatt Macy 	spl_taskq_cpuhp_state = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
14187877fdebSMatt Macy 	    "fs/spl_taskq:online", spl_taskq_expand, spl_taskq_prepare_down);
14197877fdebSMatt Macy #endif
14207877fdebSMatt Macy 
1421eda14cbcSMatt Macy 	system_taskq = taskq_create("spl_system_taskq", MAX(boot_ncpus, 64),
1422eda14cbcSMatt Macy 	    maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE|TASKQ_DYNAMIC);
1423eda14cbcSMatt Macy 	if (system_taskq == NULL)
1424c7046f76SMartin Matuska 		return (-ENOMEM);
1425eda14cbcSMatt Macy 
1426eda14cbcSMatt Macy 	system_delay_taskq = taskq_create("spl_delay_taskq", MAX(boot_ncpus, 4),
1427eda14cbcSMatt Macy 	    maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE|TASKQ_DYNAMIC);
1428eda14cbcSMatt Macy 	if (system_delay_taskq == NULL) {
14297877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG
14307877fdebSMatt Macy 		cpuhp_remove_multi_state(spl_taskq_cpuhp_state);
14317877fdebSMatt Macy #endif
1432eda14cbcSMatt Macy 		taskq_destroy(system_taskq);
1433c7046f76SMartin Matuska 		return (-ENOMEM);
1434eda14cbcSMatt Macy 	}
1435eda14cbcSMatt Macy 
1436eda14cbcSMatt Macy 	dynamic_taskq = taskq_create("spl_dynamic_taskq", 1,
1437eda14cbcSMatt Macy 	    maxclsyspri, boot_ncpus, INT_MAX, TASKQ_PREPOPULATE);
1438eda14cbcSMatt Macy 	if (dynamic_taskq == NULL) {
14397877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG
14407877fdebSMatt Macy 		cpuhp_remove_multi_state(spl_taskq_cpuhp_state);
14417877fdebSMatt Macy #endif
1442eda14cbcSMatt Macy 		taskq_destroy(system_taskq);
1443eda14cbcSMatt Macy 		taskq_destroy(system_delay_taskq);
1444c7046f76SMartin Matuska 		return (-ENOMEM);
1445eda14cbcSMatt Macy 	}
1446eda14cbcSMatt Macy 
1447eda14cbcSMatt Macy 	/*
1448eda14cbcSMatt Macy 	 * This is used to annotate tq_lock, so
1449eda14cbcSMatt Macy 	 *   taskq_dispatch -> taskq_thread_spawn -> taskq_dispatch
1450eda14cbcSMatt Macy 	 * does not trigger a lockdep warning re: possible recursive locking
1451eda14cbcSMatt Macy 	 */
1452eda14cbcSMatt Macy 	dynamic_taskq->tq_lock_class = TQ_LOCK_DYNAMIC;
1453eda14cbcSMatt Macy 
1454eda14cbcSMatt Macy 	return (0);
1455eda14cbcSMatt Macy }
1456eda14cbcSMatt Macy 
1457eda14cbcSMatt Macy void
spl_taskq_fini(void)1458eda14cbcSMatt Macy spl_taskq_fini(void)
1459eda14cbcSMatt Macy {
1460eda14cbcSMatt Macy 	taskq_destroy(dynamic_taskq);
1461eda14cbcSMatt Macy 	dynamic_taskq = NULL;
1462eda14cbcSMatt Macy 
1463eda14cbcSMatt Macy 	taskq_destroy(system_delay_taskq);
1464eda14cbcSMatt Macy 	system_delay_taskq = NULL;
1465eda14cbcSMatt Macy 
1466eda14cbcSMatt Macy 	taskq_destroy(system_taskq);
1467eda14cbcSMatt Macy 	system_taskq = NULL;
1468eda14cbcSMatt Macy 
1469eda14cbcSMatt Macy 	tsd_destroy(&taskq_tsd);
14707877fdebSMatt Macy 
14717877fdebSMatt Macy #ifdef HAVE_CPU_HOTPLUG
14727877fdebSMatt Macy 	cpuhp_remove_multi_state(spl_taskq_cpuhp_state);
14737877fdebSMatt Macy 	spl_taskq_cpuhp_state = 0;
14747877fdebSMatt Macy #endif
1475eda14cbcSMatt Macy }
1476