xref: /titanic_53/usr/src/uts/common/os/taskq.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Kernel task queues: general-purpose asynchronous task scheduling.
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * A common problem in kernel programming is the need to schedule tasks
33*7c478bd9Sstevel@tonic-gate  * to be performed later, by another thread. There are several reasons
34*7c478bd9Sstevel@tonic-gate  * you may want or need to do this:
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * (1) The task isn't time-critical, but your current code path is.
37*7c478bd9Sstevel@tonic-gate  *
38*7c478bd9Sstevel@tonic-gate  * (2) The task may require grabbing locks that you already hold.
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  * (3) The task may need to block (e.g. to wait for memory), but you
41*7c478bd9Sstevel@tonic-gate  *     cannot block in your current context.
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  * (4) Your code path can't complete because of some condition, but you can't
44*7c478bd9Sstevel@tonic-gate  *     sleep or fail, so you queue the task for later execution when condition
45*7c478bd9Sstevel@tonic-gate  *     disappears.
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  * (5) You just want a simple way to launch multiple tasks in parallel.
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  * Task queues provide such a facility. In its simplest form (used when
50*7c478bd9Sstevel@tonic-gate  * performance is not a critical consideration) a task queue consists of a
51*7c478bd9Sstevel@tonic-gate  * single list of tasks, together with one or more threads to service the
52*7c478bd9Sstevel@tonic-gate  * list. There are some cases when this simple queue is not sufficient:
53*7c478bd9Sstevel@tonic-gate  *
54*7c478bd9Sstevel@tonic-gate  * (1) The task queues are very hot and there is a need to avoid data and lock
55*7c478bd9Sstevel@tonic-gate  *	contention over global resources.
56*7c478bd9Sstevel@tonic-gate  *
57*7c478bd9Sstevel@tonic-gate  * (2) Some tasks may depend on other tasks to complete, so they can't be put in
58*7c478bd9Sstevel@tonic-gate  *	the same list managed by the same thread.
59*7c478bd9Sstevel@tonic-gate  *
60*7c478bd9Sstevel@tonic-gate  * (3) Some tasks may block for a long time, and this should not block other
61*7c478bd9Sstevel@tonic-gate  * 	tasks in the queue.
62*7c478bd9Sstevel@tonic-gate  *
63*7c478bd9Sstevel@tonic-gate  * To provide useful service in such cases we define a "dynamic task queue"
64*7c478bd9Sstevel@tonic-gate  * which has an individual thread for each of the tasks. These threads are
65*7c478bd9Sstevel@tonic-gate  * dynamically created as they are needed and destroyed when they are not in
66*7c478bd9Sstevel@tonic-gate  * use. The API for managing task pools is the same as for managing task queues
67*7c478bd9Sstevel@tonic-gate  * with the exception of a taskq creation flag TASKQ_DYNAMIC which tells that
68*7c478bd9Sstevel@tonic-gate  * dynamic task pool behavior is desired.
69*7c478bd9Sstevel@tonic-gate  *
70*7c478bd9Sstevel@tonic-gate  * Dynamic task queues may also place tasks in the normal queue (called "backing
71*7c478bd9Sstevel@tonic-gate  * queue") when task pool runs out of resources. Users of task queues may
72*7c478bd9Sstevel@tonic-gate  * disallow such queued scheduling by specifying TQ_NOQUEUE in the dispatch
73*7c478bd9Sstevel@tonic-gate  * flags.
74*7c478bd9Sstevel@tonic-gate  *
75*7c478bd9Sstevel@tonic-gate  * The backing task queue is also used for scheduling internal tasks needed for
76*7c478bd9Sstevel@tonic-gate  * dynamic task queue maintenance.
77*7c478bd9Sstevel@tonic-gate  *
78*7c478bd9Sstevel@tonic-gate  * INTERFACES:
79*7c478bd9Sstevel@tonic-gate  *
80*7c478bd9Sstevel@tonic-gate  * taskq_t *taskq_create(name, nthreads, pri_t pri, minalloc, maxall, flags);
81*7c478bd9Sstevel@tonic-gate  *
82*7c478bd9Sstevel@tonic-gate  *	Create a taskq with specified properties.
83*7c478bd9Sstevel@tonic-gate  *	Possible 'flags':
84*7c478bd9Sstevel@tonic-gate  *
85*7c478bd9Sstevel@tonic-gate  *	  TASKQ_DYNAMIC: Create task pool for task management. If this flag is
86*7c478bd9Sstevel@tonic-gate  * 		specified, 'nthreads' specifies the maximum number of threads in
87*7c478bd9Sstevel@tonic-gate  *		the task queue. Task execution order for dynamic task queues is
88*7c478bd9Sstevel@tonic-gate  *		not predictable.
89*7c478bd9Sstevel@tonic-gate  *
90*7c478bd9Sstevel@tonic-gate  *		If this flag is not specified (default case) a
91*7c478bd9Sstevel@tonic-gate  * 		single-list task queue is created with 'nthreads' threads
92*7c478bd9Sstevel@tonic-gate  * 		servicing it. Entries in this queue are managed by
93*7c478bd9Sstevel@tonic-gate  * 		taskq_ent_alloc() and taskq_ent_free() which try to keep the
94*7c478bd9Sstevel@tonic-gate  * 		task population between 'minalloc' and 'maxalloc', but the
95*7c478bd9Sstevel@tonic-gate  *		latter limit is only advisory for TQ_SLEEP dispatches and the
96*7c478bd9Sstevel@tonic-gate  *		former limit is only advisory for TQ_NOALLOC dispatches. If
97*7c478bd9Sstevel@tonic-gate  *		TASKQ_PREPOPULATE is set in 'flags', the taskq will be
98*7c478bd9Sstevel@tonic-gate  *		prepopulated with 'minalloc' task structures.
99*7c478bd9Sstevel@tonic-gate  *
100*7c478bd9Sstevel@tonic-gate  *		Since non-DYNAMIC taskqs are queues, tasks are guaranteed to be
101*7c478bd9Sstevel@tonic-gate  *		executed in the order they are scheduled if nthreads == 1.
102*7c478bd9Sstevel@tonic-gate  *		If nthreads > 1, task execution order is not predictable.
103*7c478bd9Sstevel@tonic-gate  *
104*7c478bd9Sstevel@tonic-gate  *	  TASKQ_PREPOPULATE: Prepopulate task queue with threads.
105*7c478bd9Sstevel@tonic-gate  *		Also prepopulate the task queue with 'minalloc' task structures.
106*7c478bd9Sstevel@tonic-gate  *
107*7c478bd9Sstevel@tonic-gate  *	  TASKQ_CPR_SAFE: This flag specifies that users of the task queue will
108*7c478bd9Sstevel@tonic-gate  * 		use their own protocol for handling CPR issues. This flag is not
109*7c478bd9Sstevel@tonic-gate  *		supported for DYNAMIC task queues.
110*7c478bd9Sstevel@tonic-gate  *
111*7c478bd9Sstevel@tonic-gate  *	The 'pri' field specifies the default priority for the threads that
112*7c478bd9Sstevel@tonic-gate  *	service all scheduled tasks.
113*7c478bd9Sstevel@tonic-gate  *
114*7c478bd9Sstevel@tonic-gate  * void taskq_destroy(tap):
115*7c478bd9Sstevel@tonic-gate  *
116*7c478bd9Sstevel@tonic-gate  *	Waits for any scheduled tasks to complete, then destroys the taskq.
117*7c478bd9Sstevel@tonic-gate  *	Caller should guarantee that no new tasks are scheduled in the closing
118*7c478bd9Sstevel@tonic-gate  *	taskq.
119*7c478bd9Sstevel@tonic-gate  *
120*7c478bd9Sstevel@tonic-gate  * taskqid_t taskq_dispatch(tq, func, arg, flags):
121*7c478bd9Sstevel@tonic-gate  *
122*7c478bd9Sstevel@tonic-gate  *	Dispatches the task "func(arg)" to taskq. The 'flags' indicates whether
123*7c478bd9Sstevel@tonic-gate  *	the caller is willing to block for memory.  The function returns an
124*7c478bd9Sstevel@tonic-gate  *	opaque value which is zero iff dispatch fails.  If flags is TQ_NOSLEEP
125*7c478bd9Sstevel@tonic-gate  *	or TQ_NOALLOC and the task can't be dispatched, taskq_dispatch() fails
126*7c478bd9Sstevel@tonic-gate  *	and returns (taskqid_t)0.
127*7c478bd9Sstevel@tonic-gate  *
128*7c478bd9Sstevel@tonic-gate  *	ASSUMES: func != NULL.
129*7c478bd9Sstevel@tonic-gate  *
130*7c478bd9Sstevel@tonic-gate  *	Possible flags:
131*7c478bd9Sstevel@tonic-gate  *	  TQ_NOSLEEP: Do not wait for resources; may fail.
132*7c478bd9Sstevel@tonic-gate  *
133*7c478bd9Sstevel@tonic-gate  *	  TQ_NOALLOC: Do not allocate memory; may fail.  May only be used with
134*7c478bd9Sstevel@tonic-gate  *		non-dynamic task queues.
135*7c478bd9Sstevel@tonic-gate  *
136*7c478bd9Sstevel@tonic-gate  *	  TQ_NOQUEUE: Do not enqueue a task if it can't dispatch it due to
137*7c478bd9Sstevel@tonic-gate  *		lack of available resources and fail. If this flag is not
138*7c478bd9Sstevel@tonic-gate  * 		set, and the task pool is exhausted, the task may be scheduled
139*7c478bd9Sstevel@tonic-gate  *		in the backing queue. This flag may ONLY be used with dynamic
140*7c478bd9Sstevel@tonic-gate  *		task queues.
141*7c478bd9Sstevel@tonic-gate  *
142*7c478bd9Sstevel@tonic-gate  *		NOTE: This flag should always be used when a task queue is used
143*7c478bd9Sstevel@tonic-gate  *		for tasks that may depend on each other for completion.
144*7c478bd9Sstevel@tonic-gate  *		Enqueueing dependent tasks may create deadlocks.
145*7c478bd9Sstevel@tonic-gate  *
146*7c478bd9Sstevel@tonic-gate  *	  TQ_SLEEP:   May block waiting for resources. May still fail for
147*7c478bd9Sstevel@tonic-gate  * 		dynamic task queues if TQ_NOQUEUE is also specified, otherwise
148*7c478bd9Sstevel@tonic-gate  *		always succeed.
149*7c478bd9Sstevel@tonic-gate  *
150*7c478bd9Sstevel@tonic-gate  *	NOTE: Dynamic task queues are much more likely to fail in
151*7c478bd9Sstevel@tonic-gate  *		taskq_dispatch() (especially if TQ_NOQUEUE was specified), so it
152*7c478bd9Sstevel@tonic-gate  *		is important to have backup strategies handling such failures.
153*7c478bd9Sstevel@tonic-gate  *
154*7c478bd9Sstevel@tonic-gate  * void taskq_wait(tq):
155*7c478bd9Sstevel@tonic-gate  *
156*7c478bd9Sstevel@tonic-gate  *	Waits for all previously scheduled tasks to complete.
157*7c478bd9Sstevel@tonic-gate  *
158*7c478bd9Sstevel@tonic-gate  *	NOTE: It does not stop any new task dispatches.
159*7c478bd9Sstevel@tonic-gate  *	      Do NOT call taskq_wait() from a task: it will cause deadlock.
160*7c478bd9Sstevel@tonic-gate  *
161*7c478bd9Sstevel@tonic-gate  * void taskq_suspend(tq)
162*7c478bd9Sstevel@tonic-gate  *
163*7c478bd9Sstevel@tonic-gate  *	Suspend all task execution. Tasks already scheduled for a dynamic task
164*7c478bd9Sstevel@tonic-gate  *	queue will still be executed, but all new scheduled tasks will be
165*7c478bd9Sstevel@tonic-gate  *	suspended until taskq_resume() is called.
166*7c478bd9Sstevel@tonic-gate  *
167*7c478bd9Sstevel@tonic-gate  * int  taskq_suspended(tq)
168*7c478bd9Sstevel@tonic-gate  *
169*7c478bd9Sstevel@tonic-gate  *	Returns 1 if taskq is suspended and 0 otherwise. It is intended to
170*7c478bd9Sstevel@tonic-gate  *	ASSERT that the task queue is suspended.
171*7c478bd9Sstevel@tonic-gate  *
172*7c478bd9Sstevel@tonic-gate  * void taskq_resume(tq)
173*7c478bd9Sstevel@tonic-gate  *
174*7c478bd9Sstevel@tonic-gate  *	Resume task queue execution.
175*7c478bd9Sstevel@tonic-gate  *
176*7c478bd9Sstevel@tonic-gate  * int  taskq_member(tq, thread)
177*7c478bd9Sstevel@tonic-gate  *
178*7c478bd9Sstevel@tonic-gate  *	Returns 1 if 'thread' belongs to taskq 'tq' and 0 otherwise. The
179*7c478bd9Sstevel@tonic-gate  *	intended use is to ASSERT that a given function is called in taskq
180*7c478bd9Sstevel@tonic-gate  *	context only.
181*7c478bd9Sstevel@tonic-gate  *
182*7c478bd9Sstevel@tonic-gate  * system_taskq
183*7c478bd9Sstevel@tonic-gate  *
184*7c478bd9Sstevel@tonic-gate  *	Global system-wide dynamic task queue for common uses. It may be used by
185*7c478bd9Sstevel@tonic-gate  *	any subsystem that needs to schedule tasks and does not need to manage
186*7c478bd9Sstevel@tonic-gate  *	its own task queues. It is initialized quite early during system boot.
187*7c478bd9Sstevel@tonic-gate  *
188*7c478bd9Sstevel@tonic-gate  * IMPLEMENTATION.
189*7c478bd9Sstevel@tonic-gate  *
190*7c478bd9Sstevel@tonic-gate  * This is schematic representation of the task queue structures.
191*7c478bd9Sstevel@tonic-gate  *
192*7c478bd9Sstevel@tonic-gate  *   taskq:
193*7c478bd9Sstevel@tonic-gate  *   +-------------+
194*7c478bd9Sstevel@tonic-gate  *   |tq_lock      | +---< taskq_ent_free()
195*7c478bd9Sstevel@tonic-gate  *   +-------------+ |
196*7c478bd9Sstevel@tonic-gate  *   |...          | | tqent:                  tqent:
197*7c478bd9Sstevel@tonic-gate  *   +-------------+ | +------------+          +------------+
198*7c478bd9Sstevel@tonic-gate  *   | tq_freelist |-->| tqent_next |--> ... ->| tqent_next |
199*7c478bd9Sstevel@tonic-gate  *   +-------------+   +------------+          +------------+
200*7c478bd9Sstevel@tonic-gate  *   |...          |   | ...        |          | ...        |
201*7c478bd9Sstevel@tonic-gate  *   +-------------+   +------------+          +------------+
202*7c478bd9Sstevel@tonic-gate  *   | tq_task     |    |
203*7c478bd9Sstevel@tonic-gate  *   |             |    +-------------->taskq_ent_alloc()
204*7c478bd9Sstevel@tonic-gate  * +--------------------------------------------------------------------------+
205*7c478bd9Sstevel@tonic-gate  * | |                     |            tqent                   tqent         |
206*7c478bd9Sstevel@tonic-gate  * | +---------------------+     +--> +------------+     +--> +------------+  |
207*7c478bd9Sstevel@tonic-gate  * | | ...		   |     |    | func, arg  |     |    | func, arg  |  |
208*7c478bd9Sstevel@tonic-gate  * +>+---------------------+ <---|-+  +------------+ <---|-+  +------------+  |
209*7c478bd9Sstevel@tonic-gate  *   | tq_taskq.tqent_next | ----+ |  | tqent_next | --->+ |  | tqent_next |--+
210*7c478bd9Sstevel@tonic-gate  *   +---------------------+	   |  +------------+     ^ |  +------------+
211*7c478bd9Sstevel@tonic-gate  * +-| tq_task.tqent_prev  |	   +--| tqent_prev |     | +--| tqent_prev |  ^
212*7c478bd9Sstevel@tonic-gate  * | +---------------------+	      +------------+     |    +------------+  |
213*7c478bd9Sstevel@tonic-gate  * | |...		   |	      | ...        |     |    | ...        |  |
214*7c478bd9Sstevel@tonic-gate  * | +---------------------+	      +------------+     |    +------------+  |
215*7c478bd9Sstevel@tonic-gate  * |                                      ^              |                    |
216*7c478bd9Sstevel@tonic-gate  * |                                      |              |                    |
217*7c478bd9Sstevel@tonic-gate  * +--------------------------------------+--------------+       TQ_APPEND() -+
218*7c478bd9Sstevel@tonic-gate  *   |             |                      |
219*7c478bd9Sstevel@tonic-gate  *   |...          |   taskq_thread()-----+
220*7c478bd9Sstevel@tonic-gate  *   +-------------+
221*7c478bd9Sstevel@tonic-gate  *   | tq_buckets  |--+-------> [ NULL ] (for regular task queues)
222*7c478bd9Sstevel@tonic-gate  *   +-------------+  |
223*7c478bd9Sstevel@tonic-gate  *                    |   DYNAMIC TASK QUEUES:
224*7c478bd9Sstevel@tonic-gate  *                    |
225*7c478bd9Sstevel@tonic-gate  *                    +-> taskq_bucket[nCPU]       	taskq_bucket_dispatch()
226*7c478bd9Sstevel@tonic-gate  *                        +-------------------+                    ^
227*7c478bd9Sstevel@tonic-gate  *                   +--->| tqbucket_lock     |                    |
228*7c478bd9Sstevel@tonic-gate  *                   |    +-------------------+   +--------+      +--------+
229*7c478bd9Sstevel@tonic-gate  *                   |    | tqbucket_freelist |-->| tqent  |-->...| tqent  | ^
230*7c478bd9Sstevel@tonic-gate  *                   |    +-------------------+<--+--------+<--...+--------+ |
231*7c478bd9Sstevel@tonic-gate  *                   |    | ...               |   | thread |      | thread | |
232*7c478bd9Sstevel@tonic-gate  *                   |    +-------------------+   +--------+      +--------+ |
233*7c478bd9Sstevel@tonic-gate  *                   |    +-------------------+                              |
234*7c478bd9Sstevel@tonic-gate  * taskq_dispatch()--+--->| tqbucket_lock     |             TQ_APPEND()------+
235*7c478bd9Sstevel@tonic-gate  *      TQ_HASH()    |    +-------------------+   +--------+      +--------+
236*7c478bd9Sstevel@tonic-gate  *                   |    | tqbucket_freelist |-->| tqent  |-->...| tqent  |
237*7c478bd9Sstevel@tonic-gate  *                   |    +-------------------+<--+--------+<--...+--------+
238*7c478bd9Sstevel@tonic-gate  *                   |    | ...               |   | thread |      | thread |
239*7c478bd9Sstevel@tonic-gate  *                   |    +-------------------+   +--------+      +--------+
240*7c478bd9Sstevel@tonic-gate  *		     +---> 	...
241*7c478bd9Sstevel@tonic-gate  *
242*7c478bd9Sstevel@tonic-gate  *
243*7c478bd9Sstevel@tonic-gate  * Task queues use tq_task field to link new entry in the queue. The queue is a
244*7c478bd9Sstevel@tonic-gate  * circular doubly-linked list. Entries are put in the end of the list with
245*7c478bd9Sstevel@tonic-gate  * TQ_APPEND() and processed from the front of the list by taskq_thread() in
246*7c478bd9Sstevel@tonic-gate  * FIFO order. Task queue entries are cached in the free list managed by
247*7c478bd9Sstevel@tonic-gate  * taskq_ent_alloc() and taskq_ent_free() functions.
248*7c478bd9Sstevel@tonic-gate  *
249*7c478bd9Sstevel@tonic-gate  *	All threads used by task queues mark t_taskq field of the thread to
250*7c478bd9Sstevel@tonic-gate  *	point to the task queue.
251*7c478bd9Sstevel@tonic-gate  *
252*7c478bd9Sstevel@tonic-gate  * Dynamic Task Queues Implementation.
253*7c478bd9Sstevel@tonic-gate  *
254*7c478bd9Sstevel@tonic-gate  * For a dynamic task queues there is a 1-to-1 mapping between a thread and
255*7c478bd9Sstevel@tonic-gate  * taskq_ent_structure. Each entry is serviced by its own thread and each thread
256*7c478bd9Sstevel@tonic-gate  * is controlled by a single entry.
257*7c478bd9Sstevel@tonic-gate  *
258*7c478bd9Sstevel@tonic-gate  * Entries are distributed over a set of buckets. To avoid using modulo
259*7c478bd9Sstevel@tonic-gate  * arithmetics the number of buckets is 2^n and is determined as the nearest
260*7c478bd9Sstevel@tonic-gate  * power of two roundown of the number of CPUs in the system. Tunable
261*7c478bd9Sstevel@tonic-gate  * variable 'taskq_maxbuckets' limits the maximum number of buckets. Each entry
262*7c478bd9Sstevel@tonic-gate  * is attached to a bucket for its lifetime and can't migrate to other buckets.
263*7c478bd9Sstevel@tonic-gate  *
264*7c478bd9Sstevel@tonic-gate  * Entries that have scheduled tasks are not placed in any list. The dispatch
265*7c478bd9Sstevel@tonic-gate  * function sets their "func" and "arg" fields and signals the corresponding
266*7c478bd9Sstevel@tonic-gate  * thread to execute the task. Once the thread executes the task it clears the
267*7c478bd9Sstevel@tonic-gate  * "func" field and places an entry on the bucket cache of free entries pointed
268*7c478bd9Sstevel@tonic-gate  * by "tqbucket_freelist" field. ALL entries on the free list should have "func"
269*7c478bd9Sstevel@tonic-gate  * field equal to NULL. The free list is a circular doubly-linked list identical
270*7c478bd9Sstevel@tonic-gate  * in structure to the tq_task list above, but entries are taken from it in LIFO
271*7c478bd9Sstevel@tonic-gate  * order - the last freed entry is the first to be allocated. The
272*7c478bd9Sstevel@tonic-gate  * taskq_bucket_dispatch() function gets the most recently used entry from the
273*7c478bd9Sstevel@tonic-gate  * free list, sets its "func" and "arg" fields and signals a worker thread.
274*7c478bd9Sstevel@tonic-gate  *
275*7c478bd9Sstevel@tonic-gate  * After executing each task a per-entry thread taskq_d_thread() places its
276*7c478bd9Sstevel@tonic-gate  * entry on the bucket free list and goes to a timed sleep. If it wakes up
277*7c478bd9Sstevel@tonic-gate  * without getting new task it removes the entry from the free list and destroys
278*7c478bd9Sstevel@tonic-gate  * itself. The thread sleep time is controlled by a tunable variable
279*7c478bd9Sstevel@tonic-gate  * `taskq_thread_timeout'.
280*7c478bd9Sstevel@tonic-gate  *
281*7c478bd9Sstevel@tonic-gate  * There is various statistics kept in the bucket which allows for later
282*7c478bd9Sstevel@tonic-gate  * analysis of taskq usage patterns. Also, a global copy of taskq creation and
283*7c478bd9Sstevel@tonic-gate  * death statistics is kept in the global taskq data structure. Since thread
284*7c478bd9Sstevel@tonic-gate  * creation and death happen rarely, updating such global data does not present
285*7c478bd9Sstevel@tonic-gate  * a performance problem.
286*7c478bd9Sstevel@tonic-gate  *
287*7c478bd9Sstevel@tonic-gate  * NOTE: Threads are not bound to any CPU and there is absolutely no association
288*7c478bd9Sstevel@tonic-gate  *       between the bucket and actual thread CPU, so buckets are used only to
289*7c478bd9Sstevel@tonic-gate  *	 split resources and reduce resource contention. Having threads attached
290*7c478bd9Sstevel@tonic-gate  *	 to the CPU denoted by a bucket may reduce number of times the job
291*7c478bd9Sstevel@tonic-gate  *	 switches between CPUs.
292*7c478bd9Sstevel@tonic-gate  *
293*7c478bd9Sstevel@tonic-gate  *	 Current algorithm creates a thread whenever a bucket has no free
294*7c478bd9Sstevel@tonic-gate  *	 entries. It would be nice to know how many threads are in the running
295*7c478bd9Sstevel@tonic-gate  *	 state and don't create threads if all CPUs are busy with existing
296*7c478bd9Sstevel@tonic-gate  *	 tasks, but it is unclear how such strategy can be implemented.
297*7c478bd9Sstevel@tonic-gate  *
298*7c478bd9Sstevel@tonic-gate  *	 Currently buckets are created statically as an array attached to task
299*7c478bd9Sstevel@tonic-gate  *	 queue. On some system with nCPUs < max_ncpus it may waste system
300*7c478bd9Sstevel@tonic-gate  *	 memory. One solution may be allocation of buckets when they are first
301*7c478bd9Sstevel@tonic-gate  *	 touched, but it is not clear how useful it is.
302*7c478bd9Sstevel@tonic-gate  *
303*7c478bd9Sstevel@tonic-gate  * SUSPEND/RESUME implementation.
304*7c478bd9Sstevel@tonic-gate  *
305*7c478bd9Sstevel@tonic-gate  *	Before executing a task taskq_thread() (executing non-dynamic task
306*7c478bd9Sstevel@tonic-gate  *	queues) obtains taskq's thread lock as a reader. The taskq_suspend()
307*7c478bd9Sstevel@tonic-gate  *	function gets the same lock as a writer blocking all non-dynamic task
308*7c478bd9Sstevel@tonic-gate  *	execution. The taskq_resume() function releases the lock allowing
309*7c478bd9Sstevel@tonic-gate  *	taskq_thread to continue execution.
310*7c478bd9Sstevel@tonic-gate  *
311*7c478bd9Sstevel@tonic-gate  *	For dynamic task queues, each bucket is marked as TQBUCKET_SUSPEND by
312*7c478bd9Sstevel@tonic-gate  *	taskq_suspend() function. After that taskq_bucket_dispatch() always
313*7c478bd9Sstevel@tonic-gate  *	fails, so that taskq_dispatch() will either enqueue tasks for a
314*7c478bd9Sstevel@tonic-gate  *	suspended backing queue or fail if TQ_NOQUEUE is specified in dispatch
315*7c478bd9Sstevel@tonic-gate  *	flags.
316*7c478bd9Sstevel@tonic-gate  *
317*7c478bd9Sstevel@tonic-gate  *	NOTE: taskq_suspend() does not immediately block any tasks already
318*7c478bd9Sstevel@tonic-gate  *	      scheduled for dynamic task queues. It only suspends new tasks
319*7c478bd9Sstevel@tonic-gate  *	      scheduled after taskq_suspend() was called.
320*7c478bd9Sstevel@tonic-gate  *
321*7c478bd9Sstevel@tonic-gate  *	taskq_member() function works by comparing a thread t_taskq pointer with
322*7c478bd9Sstevel@tonic-gate  *	the passed thread pointer.
323*7c478bd9Sstevel@tonic-gate  *
324*7c478bd9Sstevel@tonic-gate  * LOCKS and LOCK Hierarchy:
325*7c478bd9Sstevel@tonic-gate  *
326*7c478bd9Sstevel@tonic-gate  *   There are two locks used in task queues.
327*7c478bd9Sstevel@tonic-gate  *
328*7c478bd9Sstevel@tonic-gate  *   1) Task queue structure has a lock, protecting global task queue state.
329*7c478bd9Sstevel@tonic-gate  *
330*7c478bd9Sstevel@tonic-gate  *   2) Each per-CPU bucket has a lock for bucket management.
331*7c478bd9Sstevel@tonic-gate  *
332*7c478bd9Sstevel@tonic-gate  *   If both locks are needed, task queue lock should be taken only after bucket
333*7c478bd9Sstevel@tonic-gate  *   lock.
334*7c478bd9Sstevel@tonic-gate  *
335*7c478bd9Sstevel@tonic-gate  * DEBUG FACILITIES.
336*7c478bd9Sstevel@tonic-gate  *
337*7c478bd9Sstevel@tonic-gate  * For DEBUG kernels it is possible to induce random failures to
338*7c478bd9Sstevel@tonic-gate  * taskq_dispatch() function when it is given TQ_NOSLEEP argument. The value of
339*7c478bd9Sstevel@tonic-gate  * taskq_dmtbf and taskq_smtbf tunables control the mean time between induced
340*7c478bd9Sstevel@tonic-gate  * failures for dynamic and static task queues respectively.
341*7c478bd9Sstevel@tonic-gate  *
342*7c478bd9Sstevel@tonic-gate  * Setting TASKQ_STATISTIC to 0 will disable per-bucket statistics.
343*7c478bd9Sstevel@tonic-gate  *
344*7c478bd9Sstevel@tonic-gate  * TUNABLES
345*7c478bd9Sstevel@tonic-gate  *
346*7c478bd9Sstevel@tonic-gate  *	system_taskq_size	- Size of the global system_taskq.
347*7c478bd9Sstevel@tonic-gate  *				  This value is multiplied by nCPUs to determine
348*7c478bd9Sstevel@tonic-gate  *				  actual size.
349*7c478bd9Sstevel@tonic-gate  *				  Default value: 64
350*7c478bd9Sstevel@tonic-gate  *
351*7c478bd9Sstevel@tonic-gate  *	taskq_thread_timeout	- Maximum idle time for taskq_d_thread()
352*7c478bd9Sstevel@tonic-gate  *				  Default value: 5 minutes
353*7c478bd9Sstevel@tonic-gate  *
354*7c478bd9Sstevel@tonic-gate  *	taskq_maxbuckets	- Maximum number of buckets in any task queue
355*7c478bd9Sstevel@tonic-gate  *				  Default value: 128
356*7c478bd9Sstevel@tonic-gate  *
357*7c478bd9Sstevel@tonic-gate  *	taskq_search_depth	- Maximum # of buckets searched for a free entry
358*7c478bd9Sstevel@tonic-gate  *				  Default value: 4
359*7c478bd9Sstevel@tonic-gate  *
360*7c478bd9Sstevel@tonic-gate  *	taskq_dmtbf		- Mean time between induced dispatch failures
361*7c478bd9Sstevel@tonic-gate  *				  for dynamic task queues.
362*7c478bd9Sstevel@tonic-gate  *				  Default value: UINT_MAX (no induced failures)
363*7c478bd9Sstevel@tonic-gate  *
364*7c478bd9Sstevel@tonic-gate  *	taskq_smtbf		- Mean time between induced dispatch failures
365*7c478bd9Sstevel@tonic-gate  *				  for static task queues.
366*7c478bd9Sstevel@tonic-gate  *				  Default value: UINT_MAX (no induced failures)
367*7c478bd9Sstevel@tonic-gate  *
368*7c478bd9Sstevel@tonic-gate  * CONDITIONAL compilation.
369*7c478bd9Sstevel@tonic-gate  *
370*7c478bd9Sstevel@tonic-gate  *    TASKQ_STATISTIC	- If set will enable bucket statistic (default).
371*7c478bd9Sstevel@tonic-gate  *
372*7c478bd9Sstevel@tonic-gate  */
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate #include <sys/taskq_impl.h>
375*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
376*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
377*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
378*7c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
379*7c478bd9Sstevel@tonic-gate #include <sys/callb.h>
380*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
381*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
382*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
383*7c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>	/* For throttlefree */
384*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
385*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
386*7c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate static kmem_cache_t *taskq_ent_cache, *taskq_cache;
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate /*
391*7c478bd9Sstevel@tonic-gate  * Pseudo instance numbers for taskqs without explicitely provided instance.
392*7c478bd9Sstevel@tonic-gate  */
393*7c478bd9Sstevel@tonic-gate static vmem_t *taskq_id_arena;
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate /* Global system task queue for common use */
396*7c478bd9Sstevel@tonic-gate taskq_t	*system_taskq;
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate /*
399*7c478bd9Sstevel@tonic-gate  * Maxmimum number of entries in global system taskq is
400*7c478bd9Sstevel@tonic-gate  * 	system_taskq_size * max_ncpus
401*7c478bd9Sstevel@tonic-gate  */
402*7c478bd9Sstevel@tonic-gate #define	SYSTEM_TASKQ_SIZE 64
403*7c478bd9Sstevel@tonic-gate int system_taskq_size = SYSTEM_TASKQ_SIZE;
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate /*
406*7c478bd9Sstevel@tonic-gate  * Dynamic task queue threads that don't get any work within
407*7c478bd9Sstevel@tonic-gate  * taskq_thread_timeout destroy themselves
408*7c478bd9Sstevel@tonic-gate  */
409*7c478bd9Sstevel@tonic-gate #define	TASKQ_THREAD_TIMEOUT (60 * 5)
410*7c478bd9Sstevel@tonic-gate int taskq_thread_timeout = TASKQ_THREAD_TIMEOUT;
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate #define	TASKQ_MAXBUCKETS 128
413*7c478bd9Sstevel@tonic-gate int taskq_maxbuckets = TASKQ_MAXBUCKETS;
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate /*
416*7c478bd9Sstevel@tonic-gate  * When a bucket has no available entries another buckets are tried.
417*7c478bd9Sstevel@tonic-gate  * taskq_search_depth parameter limits the amount of buckets that we search
418*7c478bd9Sstevel@tonic-gate  * before failing. This is mostly useful in systems with many CPUs where we may
419*7c478bd9Sstevel@tonic-gate  * spend too much time scanning busy buckets.
420*7c478bd9Sstevel@tonic-gate  */
421*7c478bd9Sstevel@tonic-gate #define	TASKQ_SEARCH_DEPTH 4
422*7c478bd9Sstevel@tonic-gate int taskq_search_depth = TASKQ_SEARCH_DEPTH;
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate /*
425*7c478bd9Sstevel@tonic-gate  * Hashing function: mix various bits of x. May be pretty much anything.
426*7c478bd9Sstevel@tonic-gate  */
427*7c478bd9Sstevel@tonic-gate #define	TQ_HASH(x) ((x) ^ ((x) >> 11) ^ ((x) >> 17) ^ ((x) ^ 27))
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate /*
430*7c478bd9Sstevel@tonic-gate  * We do not create any new threads when the system is low on memory and start
431*7c478bd9Sstevel@tonic-gate  * throttling memory allocations. The following macro tries to estimate such
432*7c478bd9Sstevel@tonic-gate  * condition.
433*7c478bd9Sstevel@tonic-gate  */
434*7c478bd9Sstevel@tonic-gate #define	ENOUGH_MEMORY() (freemem > throttlefree)
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate /*
437*7c478bd9Sstevel@tonic-gate  * Static functions.
438*7c478bd9Sstevel@tonic-gate  */
439*7c478bd9Sstevel@tonic-gate static taskq_t	*taskq_create_common(const char *, int, int, pri_t, int,
440*7c478bd9Sstevel@tonic-gate     int, uint_t);
441*7c478bd9Sstevel@tonic-gate static void taskq_thread(void *);
442*7c478bd9Sstevel@tonic-gate static void taskq_d_thread(taskq_ent_t *);
443*7c478bd9Sstevel@tonic-gate static void taskq_bucket_extend(void *);
444*7c478bd9Sstevel@tonic-gate static int  taskq_constructor(void *, void *, int);
445*7c478bd9Sstevel@tonic-gate static void taskq_destructor(void *, void *);
446*7c478bd9Sstevel@tonic-gate static int  taskq_ent_constructor(void *, void *, int);
447*7c478bd9Sstevel@tonic-gate static void taskq_ent_destructor(void *, void *);
448*7c478bd9Sstevel@tonic-gate static taskq_ent_t *taskq_ent_alloc(taskq_t *, int);
449*7c478bd9Sstevel@tonic-gate static void taskq_ent_free(taskq_t *, taskq_ent_t *);
450*7c478bd9Sstevel@tonic-gate static taskq_ent_t *taskq_bucket_dispatch(taskq_bucket_t *, task_func_t,
451*7c478bd9Sstevel@tonic-gate     void *);
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate /*
454*7c478bd9Sstevel@tonic-gate  * Task queues kstats.
455*7c478bd9Sstevel@tonic-gate  */
456*7c478bd9Sstevel@tonic-gate struct taskq_kstat {
457*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tq_tasks;
458*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tq_executed;
459*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tq_maxtasks;
460*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tq_totaltime;
461*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tq_nalloc;
462*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tq_nactive;
463*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tq_pri;
464*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tq_nthreads;
465*7c478bd9Sstevel@tonic-gate } taskq_kstat = {
466*7c478bd9Sstevel@tonic-gate 	{ "tasks",		KSTAT_DATA_UINT64 },
467*7c478bd9Sstevel@tonic-gate 	{ "executed",		KSTAT_DATA_UINT64 },
468*7c478bd9Sstevel@tonic-gate 	{ "maxtasks",		KSTAT_DATA_UINT64 },
469*7c478bd9Sstevel@tonic-gate 	{ "totaltime",		KSTAT_DATA_UINT64 },
470*7c478bd9Sstevel@tonic-gate 	{ "nactive",		KSTAT_DATA_UINT64 },
471*7c478bd9Sstevel@tonic-gate 	{ "nalloc",		KSTAT_DATA_UINT64 },
472*7c478bd9Sstevel@tonic-gate 	{ "priority",		KSTAT_DATA_UINT64 },
473*7c478bd9Sstevel@tonic-gate 	{ "threads",		KSTAT_DATA_UINT64 },
474*7c478bd9Sstevel@tonic-gate };
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate struct taskq_d_kstat {
477*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_pri;
478*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_btasks;
479*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_bexecuted;
480*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_bmaxtasks;
481*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_bnalloc;
482*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_bnactive;
483*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_btotaltime;
484*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_hits;
485*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_misses;
486*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_overflows;
487*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_tcreates;
488*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_tdeaths;
489*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_maxthreads;
490*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_nomem;
491*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_disptcreates;
492*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_totaltime;
493*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_nalloc;
494*7c478bd9Sstevel@tonic-gate 	kstat_named_t	tqd_nfree;
495*7c478bd9Sstevel@tonic-gate } taskq_d_kstat = {
496*7c478bd9Sstevel@tonic-gate 	{ "priority",		KSTAT_DATA_UINT64 },
497*7c478bd9Sstevel@tonic-gate 	{ "btasks",		KSTAT_DATA_UINT64 },
498*7c478bd9Sstevel@tonic-gate 	{ "bexecuted",		KSTAT_DATA_UINT64 },
499*7c478bd9Sstevel@tonic-gate 	{ "bmaxtasks",		KSTAT_DATA_UINT64 },
500*7c478bd9Sstevel@tonic-gate 	{ "bnalloc",		KSTAT_DATA_UINT64 },
501*7c478bd9Sstevel@tonic-gate 	{ "bnactive",		KSTAT_DATA_UINT64 },
502*7c478bd9Sstevel@tonic-gate 	{ "btotaltime",		KSTAT_DATA_UINT64 },
503*7c478bd9Sstevel@tonic-gate 	{ "hits",		KSTAT_DATA_UINT64 },
504*7c478bd9Sstevel@tonic-gate 	{ "misses",		KSTAT_DATA_UINT64 },
505*7c478bd9Sstevel@tonic-gate 	{ "overflows",		KSTAT_DATA_UINT64 },
506*7c478bd9Sstevel@tonic-gate 	{ "tcreates",		KSTAT_DATA_UINT64 },
507*7c478bd9Sstevel@tonic-gate 	{ "tdeaths",		KSTAT_DATA_UINT64 },
508*7c478bd9Sstevel@tonic-gate 	{ "maxthreads",		KSTAT_DATA_UINT64 },
509*7c478bd9Sstevel@tonic-gate 	{ "nomem",		KSTAT_DATA_UINT64 },
510*7c478bd9Sstevel@tonic-gate 	{ "disptcreates",	KSTAT_DATA_UINT64 },
511*7c478bd9Sstevel@tonic-gate 	{ "totaltime",		KSTAT_DATA_UINT64 },
512*7c478bd9Sstevel@tonic-gate 	{ "nalloc",		KSTAT_DATA_UINT64 },
513*7c478bd9Sstevel@tonic-gate 	{ "nfree",		KSTAT_DATA_UINT64 },
514*7c478bd9Sstevel@tonic-gate };
515*7c478bd9Sstevel@tonic-gate 
516*7c478bd9Sstevel@tonic-gate static kmutex_t taskq_kstat_lock;
517*7c478bd9Sstevel@tonic-gate static kmutex_t taskq_d_kstat_lock;
518*7c478bd9Sstevel@tonic-gate static int taskq_kstat_update(kstat_t *, int);
519*7c478bd9Sstevel@tonic-gate static int taskq_d_kstat_update(kstat_t *, int);
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate /*
523*7c478bd9Sstevel@tonic-gate  * Collect per-bucket statistic when TASKQ_STATISTIC is defined.
524*7c478bd9Sstevel@tonic-gate  */
525*7c478bd9Sstevel@tonic-gate #define	TASKQ_STATISTIC 1
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate #if TASKQ_STATISTIC
528*7c478bd9Sstevel@tonic-gate #define	TQ_STAT(b, x)	b->tqbucket_stat.x++
529*7c478bd9Sstevel@tonic-gate #else
530*7c478bd9Sstevel@tonic-gate #define	TQ_STAT(b, x)
531*7c478bd9Sstevel@tonic-gate #endif
532*7c478bd9Sstevel@tonic-gate 
533*7c478bd9Sstevel@tonic-gate /*
534*7c478bd9Sstevel@tonic-gate  * Random fault injection.
535*7c478bd9Sstevel@tonic-gate  */
536*7c478bd9Sstevel@tonic-gate uint_t taskq_random;
537*7c478bd9Sstevel@tonic-gate uint_t taskq_dmtbf = UINT_MAX;    /* mean time between injected failures */
538*7c478bd9Sstevel@tonic-gate uint_t taskq_smtbf = UINT_MAX;    /* mean time between injected failures */
539*7c478bd9Sstevel@tonic-gate 
540*7c478bd9Sstevel@tonic-gate /*
541*7c478bd9Sstevel@tonic-gate  * TQ_NOSLEEP dispatches on dynamic task queues are always allowed to fail.
542*7c478bd9Sstevel@tonic-gate  *
543*7c478bd9Sstevel@tonic-gate  * TQ_NOSLEEP dispatches on static task queues can't arbitrarily fail because
544*7c478bd9Sstevel@tonic-gate  * they could prepopulate the cache and make sure that they do not use more
545*7c478bd9Sstevel@tonic-gate  * then minalloc entries.  So, fault injection in this case insures that
546*7c478bd9Sstevel@tonic-gate  * either TASKQ_PREPOPULATE is not set or there are more entries allocated
547*7c478bd9Sstevel@tonic-gate  * than is specified by minalloc.  TQ_NOALLOC dispatches are always allowed
548*7c478bd9Sstevel@tonic-gate  * to fail, but for simplicity we treat them identically to TQ_NOSLEEP
549*7c478bd9Sstevel@tonic-gate  * dispatches.
550*7c478bd9Sstevel@tonic-gate  */
551*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
552*7c478bd9Sstevel@tonic-gate #define	TASKQ_D_RANDOM_DISPATCH_FAILURE(tq, flag)		\
553*7c478bd9Sstevel@tonic-gate 	taskq_random = (taskq_random * 2416 + 374441) % 1771875;\
554*7c478bd9Sstevel@tonic-gate 	if ((flag & TQ_NOSLEEP) &&				\
555*7c478bd9Sstevel@tonic-gate 	    taskq_random < 1771875 / taskq_dmtbf) {		\
556*7c478bd9Sstevel@tonic-gate 		return (NULL);					\
557*7c478bd9Sstevel@tonic-gate 	}
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate #define	TASKQ_S_RANDOM_DISPATCH_FAILURE(tq, flag)		\
560*7c478bd9Sstevel@tonic-gate 	taskq_random = (taskq_random * 2416 + 374441) % 1771875;\
561*7c478bd9Sstevel@tonic-gate 	if ((flag & (TQ_NOSLEEP | TQ_NOALLOC)) &&		\
562*7c478bd9Sstevel@tonic-gate 	    (!(tq->tq_flags & TASKQ_PREPOPULATE) ||		\
563*7c478bd9Sstevel@tonic-gate 	    (tq->tq_nalloc > tq->tq_minalloc)) &&		\
564*7c478bd9Sstevel@tonic-gate 	    (taskq_random < (1771875 / taskq_smtbf))) {		\
565*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);			\
566*7c478bd9Sstevel@tonic-gate 		return (NULL);					\
567*7c478bd9Sstevel@tonic-gate 	}
568*7c478bd9Sstevel@tonic-gate #else
569*7c478bd9Sstevel@tonic-gate #define	TASKQ_S_RANDOM_DISPATCH_FAILURE(tq, flag)
570*7c478bd9Sstevel@tonic-gate #define	TASKQ_D_RANDOM_DISPATCH_FAILURE(tq, flag)
571*7c478bd9Sstevel@tonic-gate #endif
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate #define	IS_EMPTY(l) (((l).tqent_prev == (l).tqent_next) &&	\
574*7c478bd9Sstevel@tonic-gate 	((l).tqent_prev == &(l)))
575*7c478bd9Sstevel@tonic-gate 
576*7c478bd9Sstevel@tonic-gate /*
577*7c478bd9Sstevel@tonic-gate  * Append `tqe' in the end of the doubly-linked list denoted by l.
578*7c478bd9Sstevel@tonic-gate  */
579*7c478bd9Sstevel@tonic-gate #define	TQ_APPEND(l, tqe) {					\
580*7c478bd9Sstevel@tonic-gate 	tqe->tqent_next = &l;					\
581*7c478bd9Sstevel@tonic-gate 	tqe->tqent_prev = l.tqent_prev;				\
582*7c478bd9Sstevel@tonic-gate 	tqe->tqent_next->tqent_prev = tqe;			\
583*7c478bd9Sstevel@tonic-gate 	tqe->tqent_prev->tqent_next = tqe;			\
584*7c478bd9Sstevel@tonic-gate }
585*7c478bd9Sstevel@tonic-gate 
586*7c478bd9Sstevel@tonic-gate /*
587*7c478bd9Sstevel@tonic-gate  * Schedule a task specified by func and arg into the task queue entry tqe.
588*7c478bd9Sstevel@tonic-gate  */
589*7c478bd9Sstevel@tonic-gate #define	TQ_ENQUEUE(tq, tqe, func, arg) {			\
590*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tq->tq_lock));			\
591*7c478bd9Sstevel@tonic-gate 	TQ_APPEND(tq->tq_task, tqe);				\
592*7c478bd9Sstevel@tonic-gate 	tqe->tqent_func = (func);				\
593*7c478bd9Sstevel@tonic-gate 	tqe->tqent_arg = (arg);					\
594*7c478bd9Sstevel@tonic-gate 	tq->tq_tasks++;						\
595*7c478bd9Sstevel@tonic-gate 	if (tq->tq_tasks - tq->tq_executed > tq->tq_maxtasks)	\
596*7c478bd9Sstevel@tonic-gate 		tq->tq_maxtasks = tq->tq_tasks - tq->tq_executed;	\
597*7c478bd9Sstevel@tonic-gate 	cv_signal(&tq->tq_dispatch_cv);				\
598*7c478bd9Sstevel@tonic-gate 	DTRACE_PROBE2(taskq__enqueue, taskq_t *, tq, taskq_ent_t *, tqe); \
599*7c478bd9Sstevel@tonic-gate }
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate /*
602*7c478bd9Sstevel@tonic-gate  * Do-nothing task which may be used to prepopulate thread caches.
603*7c478bd9Sstevel@tonic-gate  */
604*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
605*7c478bd9Sstevel@tonic-gate void
606*7c478bd9Sstevel@tonic-gate nulltask(void *unused)
607*7c478bd9Sstevel@tonic-gate {
608*7c478bd9Sstevel@tonic-gate }
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate 
611*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
612*7c478bd9Sstevel@tonic-gate static int
613*7c478bd9Sstevel@tonic-gate taskq_constructor(void *buf, void *cdrarg, int kmflags)
614*7c478bd9Sstevel@tonic-gate {
615*7c478bd9Sstevel@tonic-gate 	taskq_t *tq = buf;
616*7c478bd9Sstevel@tonic-gate 
617*7c478bd9Sstevel@tonic-gate 	bzero(tq, sizeof (taskq_t));
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 	mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
620*7c478bd9Sstevel@tonic-gate 	rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
621*7c478bd9Sstevel@tonic-gate 	cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
622*7c478bd9Sstevel@tonic-gate 	cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
623*7c478bd9Sstevel@tonic-gate 
624*7c478bd9Sstevel@tonic-gate 	tq->tq_task.tqent_next = &tq->tq_task;
625*7c478bd9Sstevel@tonic-gate 	tq->tq_task.tqent_prev = &tq->tq_task;
626*7c478bd9Sstevel@tonic-gate 
627*7c478bd9Sstevel@tonic-gate 	return (0);
628*7c478bd9Sstevel@tonic-gate }
629*7c478bd9Sstevel@tonic-gate 
630*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
631*7c478bd9Sstevel@tonic-gate static void
632*7c478bd9Sstevel@tonic-gate taskq_destructor(void *buf, void *cdrarg)
633*7c478bd9Sstevel@tonic-gate {
634*7c478bd9Sstevel@tonic-gate 	taskq_t *tq = buf;
635*7c478bd9Sstevel@tonic-gate 
636*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&tq->tq_lock);
637*7c478bd9Sstevel@tonic-gate 	rw_destroy(&tq->tq_threadlock);
638*7c478bd9Sstevel@tonic-gate 	cv_destroy(&tq->tq_dispatch_cv);
639*7c478bd9Sstevel@tonic-gate 	cv_destroy(&tq->tq_wait_cv);
640*7c478bd9Sstevel@tonic-gate }
641*7c478bd9Sstevel@tonic-gate 
642*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
643*7c478bd9Sstevel@tonic-gate static int
644*7c478bd9Sstevel@tonic-gate taskq_ent_constructor(void *buf, void *cdrarg, int kmflags)
645*7c478bd9Sstevel@tonic-gate {
646*7c478bd9Sstevel@tonic-gate 	taskq_ent_t *tqe = buf;
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate 	tqe->tqent_thread = NULL;
649*7c478bd9Sstevel@tonic-gate 	cv_init(&tqe->tqent_cv, NULL, CV_DEFAULT, NULL);
650*7c478bd9Sstevel@tonic-gate 
651*7c478bd9Sstevel@tonic-gate 	return (0);
652*7c478bd9Sstevel@tonic-gate }
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
655*7c478bd9Sstevel@tonic-gate static void
656*7c478bd9Sstevel@tonic-gate taskq_ent_destructor(void *buf, void *cdrarg)
657*7c478bd9Sstevel@tonic-gate {
658*7c478bd9Sstevel@tonic-gate 	taskq_ent_t *tqe = buf;
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate 	ASSERT(tqe->tqent_thread == NULL);
661*7c478bd9Sstevel@tonic-gate 	cv_destroy(&tqe->tqent_cv);
662*7c478bd9Sstevel@tonic-gate }
663*7c478bd9Sstevel@tonic-gate 
664*7c478bd9Sstevel@tonic-gate void
665*7c478bd9Sstevel@tonic-gate taskq_init(void)
666*7c478bd9Sstevel@tonic-gate {
667*7c478bd9Sstevel@tonic-gate 	taskq_ent_cache = kmem_cache_create("taskq_ent_cache",
668*7c478bd9Sstevel@tonic-gate 	    sizeof (taskq_ent_t), 0, taskq_ent_constructor,
669*7c478bd9Sstevel@tonic-gate 	    taskq_ent_destructor, NULL, NULL, NULL, 0);
670*7c478bd9Sstevel@tonic-gate 	taskq_cache = kmem_cache_create("taskq_cache", sizeof (taskq_t),
671*7c478bd9Sstevel@tonic-gate 	    0, taskq_constructor, taskq_destructor, NULL, NULL, NULL, 0);
672*7c478bd9Sstevel@tonic-gate 	taskq_id_arena = vmem_create("taskq_id_arena",
673*7c478bd9Sstevel@tonic-gate 	    (void *)1, INT32_MAX, 1, NULL, NULL, NULL, 0,
674*7c478bd9Sstevel@tonic-gate 	    VM_SLEEP | VMC_IDENTIFIER);
675*7c478bd9Sstevel@tonic-gate }
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate /*
678*7c478bd9Sstevel@tonic-gate  * Create global system dynamic task queue.
679*7c478bd9Sstevel@tonic-gate  */
680*7c478bd9Sstevel@tonic-gate void
681*7c478bd9Sstevel@tonic-gate system_taskq_init(void)
682*7c478bd9Sstevel@tonic-gate {
683*7c478bd9Sstevel@tonic-gate 	system_taskq = taskq_create_common("system_taskq", 0,
684*7c478bd9Sstevel@tonic-gate 	    system_taskq_size * max_ncpus, minclsyspri, 4, 512,
685*7c478bd9Sstevel@tonic-gate 	    TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
686*7c478bd9Sstevel@tonic-gate }
687*7c478bd9Sstevel@tonic-gate 
688*7c478bd9Sstevel@tonic-gate /*
689*7c478bd9Sstevel@tonic-gate  * taskq_ent_alloc()
690*7c478bd9Sstevel@tonic-gate  *
691*7c478bd9Sstevel@tonic-gate  * Allocates a new taskq_ent_t structure either from the free list or from the
692*7c478bd9Sstevel@tonic-gate  * cache. Returns NULL if it can't be allocated.
693*7c478bd9Sstevel@tonic-gate  *
694*7c478bd9Sstevel@tonic-gate  * Assumes: tq->tq_lock is held.
695*7c478bd9Sstevel@tonic-gate  */
696*7c478bd9Sstevel@tonic-gate static taskq_ent_t *
697*7c478bd9Sstevel@tonic-gate taskq_ent_alloc(taskq_t *tq, int flags)
698*7c478bd9Sstevel@tonic-gate {
699*7c478bd9Sstevel@tonic-gate 	int kmflags = (flags & TQ_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP;
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate 	taskq_ent_t *tqe;
702*7c478bd9Sstevel@tonic-gate 
703*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tq->tq_lock));
704*7c478bd9Sstevel@tonic-gate 
705*7c478bd9Sstevel@tonic-gate 	/*
706*7c478bd9Sstevel@tonic-gate 	 * TQ_NOALLOC allocations are allowed to use the freelist, even if
707*7c478bd9Sstevel@tonic-gate 	 * we are below tq_minalloc.
708*7c478bd9Sstevel@tonic-gate 	 */
709*7c478bd9Sstevel@tonic-gate 	if ((tqe = tq->tq_freelist) != NULL &&
710*7c478bd9Sstevel@tonic-gate 	    ((flags & TQ_NOALLOC) || tq->tq_nalloc >= tq->tq_minalloc)) {
711*7c478bd9Sstevel@tonic-gate 		tq->tq_freelist = tqe->tqent_next;
712*7c478bd9Sstevel@tonic-gate 	} else {
713*7c478bd9Sstevel@tonic-gate 		if (flags & TQ_NOALLOC)
714*7c478bd9Sstevel@tonic-gate 			return (NULL);
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);
717*7c478bd9Sstevel@tonic-gate 		if (tq->tq_nalloc >= tq->tq_maxalloc) {
718*7c478bd9Sstevel@tonic-gate 			if (kmflags & KM_NOSLEEP) {
719*7c478bd9Sstevel@tonic-gate 				mutex_enter(&tq->tq_lock);
720*7c478bd9Sstevel@tonic-gate 				return (NULL);
721*7c478bd9Sstevel@tonic-gate 			}
722*7c478bd9Sstevel@tonic-gate 			/*
723*7c478bd9Sstevel@tonic-gate 			 * We don't want to exceed tq_maxalloc, but we can't
724*7c478bd9Sstevel@tonic-gate 			 * wait for other tasks to complete (and thus free up
725*7c478bd9Sstevel@tonic-gate 			 * task structures) without risking deadlock with
726*7c478bd9Sstevel@tonic-gate 			 * the caller.  So, we just delay for one second
727*7c478bd9Sstevel@tonic-gate 			 * to throttle the allocation rate.
728*7c478bd9Sstevel@tonic-gate 			 */
729*7c478bd9Sstevel@tonic-gate 			delay(hz);
730*7c478bd9Sstevel@tonic-gate 		}
731*7c478bd9Sstevel@tonic-gate 		tqe = kmem_cache_alloc(taskq_ent_cache, kmflags);
732*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tq->tq_lock);
733*7c478bd9Sstevel@tonic-gate 		if (tqe != NULL)
734*7c478bd9Sstevel@tonic-gate 			tq->tq_nalloc++;
735*7c478bd9Sstevel@tonic-gate 	}
736*7c478bd9Sstevel@tonic-gate 	return (tqe);
737*7c478bd9Sstevel@tonic-gate }
738*7c478bd9Sstevel@tonic-gate 
739*7c478bd9Sstevel@tonic-gate /*
740*7c478bd9Sstevel@tonic-gate  * taskq_ent_free()
741*7c478bd9Sstevel@tonic-gate  *
742*7c478bd9Sstevel@tonic-gate  * Free taskq_ent_t structure by either putting it on the free list or freeing
743*7c478bd9Sstevel@tonic-gate  * it to the cache.
744*7c478bd9Sstevel@tonic-gate  *
745*7c478bd9Sstevel@tonic-gate  * Assumes: tq->tq_lock is held.
746*7c478bd9Sstevel@tonic-gate  */
747*7c478bd9Sstevel@tonic-gate static void
748*7c478bd9Sstevel@tonic-gate taskq_ent_free(taskq_t *tq, taskq_ent_t *tqe)
749*7c478bd9Sstevel@tonic-gate {
750*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&tq->tq_lock));
751*7c478bd9Sstevel@tonic-gate 
752*7c478bd9Sstevel@tonic-gate 	if (tq->tq_nalloc <= tq->tq_minalloc) {
753*7c478bd9Sstevel@tonic-gate 		tqe->tqent_next = tq->tq_freelist;
754*7c478bd9Sstevel@tonic-gate 		tq->tq_freelist = tqe;
755*7c478bd9Sstevel@tonic-gate 	} else {
756*7c478bd9Sstevel@tonic-gate 		tq->tq_nalloc--;
757*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);
758*7c478bd9Sstevel@tonic-gate 		kmem_cache_free(taskq_ent_cache, tqe);
759*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tq->tq_lock);
760*7c478bd9Sstevel@tonic-gate 	}
761*7c478bd9Sstevel@tonic-gate }
762*7c478bd9Sstevel@tonic-gate 
763*7c478bd9Sstevel@tonic-gate /*
764*7c478bd9Sstevel@tonic-gate  * Dispatch a task "func(arg)" to a free entry of bucket b.
765*7c478bd9Sstevel@tonic-gate  *
766*7c478bd9Sstevel@tonic-gate  * Assumes: no bucket locks is held.
767*7c478bd9Sstevel@tonic-gate  *
768*7c478bd9Sstevel@tonic-gate  * Returns: a pointer to an entry if dispatch was successful.
769*7c478bd9Sstevel@tonic-gate  *	    NULL if there are no free entries or if the bucket is suspended.
770*7c478bd9Sstevel@tonic-gate  */
771*7c478bd9Sstevel@tonic-gate static taskq_ent_t *
772*7c478bd9Sstevel@tonic-gate taskq_bucket_dispatch(taskq_bucket_t *b, task_func_t func, void *arg)
773*7c478bd9Sstevel@tonic-gate {
774*7c478bd9Sstevel@tonic-gate 	taskq_ent_t *tqe;
775*7c478bd9Sstevel@tonic-gate 
776*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&b->tqbucket_lock));
777*7c478bd9Sstevel@tonic-gate 	ASSERT(func != NULL);
778*7c478bd9Sstevel@tonic-gate 
779*7c478bd9Sstevel@tonic-gate 	mutex_enter(&b->tqbucket_lock);
780*7c478bd9Sstevel@tonic-gate 
781*7c478bd9Sstevel@tonic-gate 	ASSERT(b->tqbucket_nfree != 0 || IS_EMPTY(b->tqbucket_freelist));
782*7c478bd9Sstevel@tonic-gate 	ASSERT(b->tqbucket_nfree == 0 || !IS_EMPTY(b->tqbucket_freelist));
783*7c478bd9Sstevel@tonic-gate 
784*7c478bd9Sstevel@tonic-gate 	/*
785*7c478bd9Sstevel@tonic-gate 	 * Get en entry from the freelist if there is one.
786*7c478bd9Sstevel@tonic-gate 	 * Schedule task into the entry.
787*7c478bd9Sstevel@tonic-gate 	 */
788*7c478bd9Sstevel@tonic-gate 	if ((b->tqbucket_nfree != 0) &&
789*7c478bd9Sstevel@tonic-gate 	    !(b->tqbucket_flags & TQBUCKET_SUSPEND)) {
790*7c478bd9Sstevel@tonic-gate 		tqe = b->tqbucket_freelist.tqent_prev;
791*7c478bd9Sstevel@tonic-gate 
792*7c478bd9Sstevel@tonic-gate 		ASSERT(tqe != &b->tqbucket_freelist);
793*7c478bd9Sstevel@tonic-gate 		ASSERT(tqe->tqent_thread != NULL);
794*7c478bd9Sstevel@tonic-gate 
795*7c478bd9Sstevel@tonic-gate 		tqe->tqent_prev->tqent_next = tqe->tqent_next;
796*7c478bd9Sstevel@tonic-gate 		tqe->tqent_next->tqent_prev = tqe->tqent_prev;
797*7c478bd9Sstevel@tonic-gate 		b->tqbucket_nalloc++;
798*7c478bd9Sstevel@tonic-gate 		b->tqbucket_nfree--;
799*7c478bd9Sstevel@tonic-gate 		tqe->tqent_func = func;
800*7c478bd9Sstevel@tonic-gate 		tqe->tqent_arg = arg;
801*7c478bd9Sstevel@tonic-gate 		TQ_STAT(b, tqs_hits);
802*7c478bd9Sstevel@tonic-gate 		cv_signal(&tqe->tqent_cv);
803*7c478bd9Sstevel@tonic-gate 		DTRACE_PROBE2(taskq__d__enqueue, taskq_bucket_t *, b,
804*7c478bd9Sstevel@tonic-gate 		    taskq_ent_t *, tqe);
805*7c478bd9Sstevel@tonic-gate 	} else {
806*7c478bd9Sstevel@tonic-gate 		tqe = NULL;
807*7c478bd9Sstevel@tonic-gate 		TQ_STAT(b, tqs_misses);
808*7c478bd9Sstevel@tonic-gate 	}
809*7c478bd9Sstevel@tonic-gate 	mutex_exit(&b->tqbucket_lock);
810*7c478bd9Sstevel@tonic-gate 	return (tqe);
811*7c478bd9Sstevel@tonic-gate }
812*7c478bd9Sstevel@tonic-gate 
813*7c478bd9Sstevel@tonic-gate /*
814*7c478bd9Sstevel@tonic-gate  * Dispatch a task.
815*7c478bd9Sstevel@tonic-gate  *
816*7c478bd9Sstevel@tonic-gate  * Assumes: func != NULL
817*7c478bd9Sstevel@tonic-gate  *
818*7c478bd9Sstevel@tonic-gate  * Returns: NULL if dispatch failed.
819*7c478bd9Sstevel@tonic-gate  *	    non-NULL if task dispatched successfully.
820*7c478bd9Sstevel@tonic-gate  *	    Actual return value is the pointer to taskq entry that was used to
821*7c478bd9Sstevel@tonic-gate  *	    dispatch a task. This is useful for debugging.
822*7c478bd9Sstevel@tonic-gate  */
823*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
824*7c478bd9Sstevel@tonic-gate taskqid_t
825*7c478bd9Sstevel@tonic-gate taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t flags)
826*7c478bd9Sstevel@tonic-gate {
827*7c478bd9Sstevel@tonic-gate 	taskq_bucket_t *bucket = NULL;	/* Which bucket needs extension */
828*7c478bd9Sstevel@tonic-gate 	taskq_ent_t *tqe = NULL;
829*7c478bd9Sstevel@tonic-gate 	taskq_ent_t *tqe1;
830*7c478bd9Sstevel@tonic-gate 	uint_t bsize;
831*7c478bd9Sstevel@tonic-gate 
832*7c478bd9Sstevel@tonic-gate 	ASSERT(tq != NULL);
833*7c478bd9Sstevel@tonic-gate 	ASSERT(func != NULL);
834*7c478bd9Sstevel@tonic-gate 
835*7c478bd9Sstevel@tonic-gate 	if (!(tq->tq_flags & TASKQ_DYNAMIC)) {
836*7c478bd9Sstevel@tonic-gate 		/*
837*7c478bd9Sstevel@tonic-gate 		 * TQ_NOQUEUE flag can't be used with non-dynamic task queues.
838*7c478bd9Sstevel@tonic-gate 		 */
839*7c478bd9Sstevel@tonic-gate 		ASSERT(! (flags & TQ_NOQUEUE));
840*7c478bd9Sstevel@tonic-gate 		/*
841*7c478bd9Sstevel@tonic-gate 		 * Enqueue the task to the underlying queue.
842*7c478bd9Sstevel@tonic-gate 		 */
843*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tq->tq_lock);
844*7c478bd9Sstevel@tonic-gate 
845*7c478bd9Sstevel@tonic-gate 		TASKQ_S_RANDOM_DISPATCH_FAILURE(tq, flags);
846*7c478bd9Sstevel@tonic-gate 
847*7c478bd9Sstevel@tonic-gate 		if ((tqe = taskq_ent_alloc(tq, flags)) == NULL) {
848*7c478bd9Sstevel@tonic-gate 			mutex_exit(&tq->tq_lock);
849*7c478bd9Sstevel@tonic-gate 			return (NULL);
850*7c478bd9Sstevel@tonic-gate 		}
851*7c478bd9Sstevel@tonic-gate 		TQ_ENQUEUE(tq, tqe, func, arg);
852*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);
853*7c478bd9Sstevel@tonic-gate 		return ((taskqid_t)tqe);
854*7c478bd9Sstevel@tonic-gate 	}
855*7c478bd9Sstevel@tonic-gate 
856*7c478bd9Sstevel@tonic-gate 	/*
857*7c478bd9Sstevel@tonic-gate 	 * Dynamic taskq dispatching.
858*7c478bd9Sstevel@tonic-gate 	 */
859*7c478bd9Sstevel@tonic-gate 	ASSERT(!(flags & TQ_NOALLOC));
860*7c478bd9Sstevel@tonic-gate 	TASKQ_D_RANDOM_DISPATCH_FAILURE(tq, flags);
861*7c478bd9Sstevel@tonic-gate 
862*7c478bd9Sstevel@tonic-gate 	bsize = tq->tq_nbuckets;
863*7c478bd9Sstevel@tonic-gate 
864*7c478bd9Sstevel@tonic-gate 	if (bsize == 1) {
865*7c478bd9Sstevel@tonic-gate 		/*
866*7c478bd9Sstevel@tonic-gate 		 * In a single-CPU case there is only one bucket, so get
867*7c478bd9Sstevel@tonic-gate 		 * entry directly from there.
868*7c478bd9Sstevel@tonic-gate 		 */
869*7c478bd9Sstevel@tonic-gate 		if ((tqe = taskq_bucket_dispatch(tq->tq_buckets, func, arg))
870*7c478bd9Sstevel@tonic-gate 		    != NULL)
871*7c478bd9Sstevel@tonic-gate 			return ((taskqid_t)tqe);	/* Fastpath */
872*7c478bd9Sstevel@tonic-gate 		bucket = tq->tq_buckets;
873*7c478bd9Sstevel@tonic-gate 	} else {
874*7c478bd9Sstevel@tonic-gate 		int loopcount;
875*7c478bd9Sstevel@tonic-gate 		taskq_bucket_t *b;
876*7c478bd9Sstevel@tonic-gate 		uintptr_t h = ((uintptr_t)CPU + (uintptr_t)arg) >> 3;
877*7c478bd9Sstevel@tonic-gate 
878*7c478bd9Sstevel@tonic-gate 		h = TQ_HASH(h);
879*7c478bd9Sstevel@tonic-gate 
880*7c478bd9Sstevel@tonic-gate 		/*
881*7c478bd9Sstevel@tonic-gate 		 * The 'bucket' points to the original bucket that we hit. If we
882*7c478bd9Sstevel@tonic-gate 		 * can't allocate from it, we search other buckets, but only
883*7c478bd9Sstevel@tonic-gate 		 * extend this one.
884*7c478bd9Sstevel@tonic-gate 		 */
885*7c478bd9Sstevel@tonic-gate 		b = &tq->tq_buckets[h & (bsize - 1)];
886*7c478bd9Sstevel@tonic-gate 		ASSERT(b->tqbucket_taskq == tq);	/* Sanity check */
887*7c478bd9Sstevel@tonic-gate 
888*7c478bd9Sstevel@tonic-gate 		/*
889*7c478bd9Sstevel@tonic-gate 		 * Do a quick check before grabbing the lock. If the bucket does
890*7c478bd9Sstevel@tonic-gate 		 * not have free entries now, chances are very small that it
891*7c478bd9Sstevel@tonic-gate 		 * will after we take the lock, so we just skip it.
892*7c478bd9Sstevel@tonic-gate 		 */
893*7c478bd9Sstevel@tonic-gate 		if (b->tqbucket_nfree != 0) {
894*7c478bd9Sstevel@tonic-gate 			if ((tqe = taskq_bucket_dispatch(b, func, arg)) != NULL)
895*7c478bd9Sstevel@tonic-gate 				return ((taskqid_t)tqe);	/* Fastpath */
896*7c478bd9Sstevel@tonic-gate 		} else {
897*7c478bd9Sstevel@tonic-gate 			TQ_STAT(b, tqs_misses);
898*7c478bd9Sstevel@tonic-gate 		}
899*7c478bd9Sstevel@tonic-gate 
900*7c478bd9Sstevel@tonic-gate 		bucket = b;
901*7c478bd9Sstevel@tonic-gate 		loopcount = MIN(taskq_search_depth, bsize);
902*7c478bd9Sstevel@tonic-gate 		/*
903*7c478bd9Sstevel@tonic-gate 		 * If bucket dispatch failed, search loopcount number of buckets
904*7c478bd9Sstevel@tonic-gate 		 * before we give up and fail.
905*7c478bd9Sstevel@tonic-gate 		 */
906*7c478bd9Sstevel@tonic-gate 		do {
907*7c478bd9Sstevel@tonic-gate 			b = &tq->tq_buckets[++h & (bsize - 1)];
908*7c478bd9Sstevel@tonic-gate 			ASSERT(b->tqbucket_taskq == tq);  /* Sanity check */
909*7c478bd9Sstevel@tonic-gate 			loopcount--;
910*7c478bd9Sstevel@tonic-gate 
911*7c478bd9Sstevel@tonic-gate 			if (b->tqbucket_nfree != 0) {
912*7c478bd9Sstevel@tonic-gate 				tqe = taskq_bucket_dispatch(b, func, arg);
913*7c478bd9Sstevel@tonic-gate 			} else {
914*7c478bd9Sstevel@tonic-gate 				TQ_STAT(b, tqs_misses);
915*7c478bd9Sstevel@tonic-gate 			}
916*7c478bd9Sstevel@tonic-gate 		} while ((tqe == NULL) && (loopcount > 0));
917*7c478bd9Sstevel@tonic-gate 	}
918*7c478bd9Sstevel@tonic-gate 
919*7c478bd9Sstevel@tonic-gate 	/*
920*7c478bd9Sstevel@tonic-gate 	 * At this point we either scheduled a task and (tqe != NULL) or failed
921*7c478bd9Sstevel@tonic-gate 	 * (tqe == NULL). Try to recover from fails.
922*7c478bd9Sstevel@tonic-gate 	 */
923*7c478bd9Sstevel@tonic-gate 
924*7c478bd9Sstevel@tonic-gate 	/*
925*7c478bd9Sstevel@tonic-gate 	 * For KM_SLEEP dispatches, try to extend the bucket and retry dispatch.
926*7c478bd9Sstevel@tonic-gate 	 */
927*7c478bd9Sstevel@tonic-gate 	if ((tqe == NULL) && !(flags & TQ_NOSLEEP)) {
928*7c478bd9Sstevel@tonic-gate 		/*
929*7c478bd9Sstevel@tonic-gate 		 * taskq_bucket_extend() may fail to do anything, but this is
930*7c478bd9Sstevel@tonic-gate 		 * fine - we deal with it later. If the bucket was successfully
931*7c478bd9Sstevel@tonic-gate 		 * extended, there is a good chance that taskq_bucket_dispatch()
932*7c478bd9Sstevel@tonic-gate 		 * will get this new entry, unless someone is racing with us and
933*7c478bd9Sstevel@tonic-gate 		 * stealing the new entry from under our nose.
934*7c478bd9Sstevel@tonic-gate 		 * taskq_bucket_extend() may sleep.
935*7c478bd9Sstevel@tonic-gate 		 */
936*7c478bd9Sstevel@tonic-gate 		taskq_bucket_extend(bucket);
937*7c478bd9Sstevel@tonic-gate 		TQ_STAT(bucket, tqs_disptcreates);
938*7c478bd9Sstevel@tonic-gate 		if ((tqe = taskq_bucket_dispatch(bucket, func, arg)) != NULL)
939*7c478bd9Sstevel@tonic-gate 			return ((taskqid_t)tqe);
940*7c478bd9Sstevel@tonic-gate 	}
941*7c478bd9Sstevel@tonic-gate 
942*7c478bd9Sstevel@tonic-gate 	ASSERT(bucket != NULL);
943*7c478bd9Sstevel@tonic-gate 	/*
944*7c478bd9Sstevel@tonic-gate 	 * Since there are not enough free entries in the bucket, extend it
945*7c478bd9Sstevel@tonic-gate 	 * in the background using backing queue.
946*7c478bd9Sstevel@tonic-gate 	 */
947*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
948*7c478bd9Sstevel@tonic-gate 	if ((tqe1 = taskq_ent_alloc(tq, TQ_NOSLEEP)) != NULL) {
949*7c478bd9Sstevel@tonic-gate 		TQ_ENQUEUE(tq, tqe1, taskq_bucket_extend,
950*7c478bd9Sstevel@tonic-gate 		    bucket);
951*7c478bd9Sstevel@tonic-gate 	} else {
952*7c478bd9Sstevel@tonic-gate 		TQ_STAT(bucket, tqs_nomem);
953*7c478bd9Sstevel@tonic-gate 	}
954*7c478bd9Sstevel@tonic-gate 
955*7c478bd9Sstevel@tonic-gate 	/*
956*7c478bd9Sstevel@tonic-gate 	 * Dispatch failed and we can't find an entry to schedule a task.
957*7c478bd9Sstevel@tonic-gate 	 * Revert to the backing queue unless TQ_NOQUEUE was asked.
958*7c478bd9Sstevel@tonic-gate 	 */
959*7c478bd9Sstevel@tonic-gate 	if ((tqe == NULL) && !(flags & TQ_NOQUEUE)) {
960*7c478bd9Sstevel@tonic-gate 		if ((tqe = taskq_ent_alloc(tq, flags)) != NULL) {
961*7c478bd9Sstevel@tonic-gate 			TQ_ENQUEUE(tq, tqe, func, arg);
962*7c478bd9Sstevel@tonic-gate 		} else {
963*7c478bd9Sstevel@tonic-gate 			TQ_STAT(bucket, tqs_nomem);
964*7c478bd9Sstevel@tonic-gate 		}
965*7c478bd9Sstevel@tonic-gate 	}
966*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tq->tq_lock);
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 	return ((taskqid_t)tqe);
969*7c478bd9Sstevel@tonic-gate }
970*7c478bd9Sstevel@tonic-gate 
971*7c478bd9Sstevel@tonic-gate /*
972*7c478bd9Sstevel@tonic-gate  * Wait for all pending tasks to complete.
973*7c478bd9Sstevel@tonic-gate  * Calling taskq_wait from a task will cause deadlock.
974*7c478bd9Sstevel@tonic-gate  */
975*7c478bd9Sstevel@tonic-gate void
976*7c478bd9Sstevel@tonic-gate taskq_wait(taskq_t *tq)
977*7c478bd9Sstevel@tonic-gate {
978*7c478bd9Sstevel@tonic-gate 	ASSERT(tq != curthread->t_taskq);
979*7c478bd9Sstevel@tonic-gate 
980*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
981*7c478bd9Sstevel@tonic-gate 	while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0)
982*7c478bd9Sstevel@tonic-gate 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
983*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tq->tq_lock);
984*7c478bd9Sstevel@tonic-gate 
985*7c478bd9Sstevel@tonic-gate 	if (tq->tq_flags & TASKQ_DYNAMIC) {
986*7c478bd9Sstevel@tonic-gate 		taskq_bucket_t *b = tq->tq_buckets;
987*7c478bd9Sstevel@tonic-gate 		int bid = 0;
988*7c478bd9Sstevel@tonic-gate 		for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) {
989*7c478bd9Sstevel@tonic-gate 			mutex_enter(&b->tqbucket_lock);
990*7c478bd9Sstevel@tonic-gate 			while (b->tqbucket_nalloc > 0)
991*7c478bd9Sstevel@tonic-gate 				cv_wait(&b->tqbucket_cv, &b->tqbucket_lock);
992*7c478bd9Sstevel@tonic-gate 			mutex_exit(&b->tqbucket_lock);
993*7c478bd9Sstevel@tonic-gate 		}
994*7c478bd9Sstevel@tonic-gate 	}
995*7c478bd9Sstevel@tonic-gate }
996*7c478bd9Sstevel@tonic-gate 
997*7c478bd9Sstevel@tonic-gate /*
998*7c478bd9Sstevel@tonic-gate  * Suspend execution of tasks.
999*7c478bd9Sstevel@tonic-gate  *
1000*7c478bd9Sstevel@tonic-gate  * Tasks in the queue part will be suspended immediately upon return from this
1001*7c478bd9Sstevel@tonic-gate  * function. Pending tasks in the dynamic part will continue to execute, but all
1002*7c478bd9Sstevel@tonic-gate  * new tasks will  be suspended.
1003*7c478bd9Sstevel@tonic-gate  */
1004*7c478bd9Sstevel@tonic-gate void
1005*7c478bd9Sstevel@tonic-gate taskq_suspend(taskq_t *tq)
1006*7c478bd9Sstevel@tonic-gate {
1007*7c478bd9Sstevel@tonic-gate 	rw_enter(&tq->tq_threadlock, RW_WRITER);
1008*7c478bd9Sstevel@tonic-gate 
1009*7c478bd9Sstevel@tonic-gate 	if (tq->tq_flags & TASKQ_DYNAMIC) {
1010*7c478bd9Sstevel@tonic-gate 		taskq_bucket_t *b = tq->tq_buckets;
1011*7c478bd9Sstevel@tonic-gate 		int bid = 0;
1012*7c478bd9Sstevel@tonic-gate 		for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) {
1013*7c478bd9Sstevel@tonic-gate 			mutex_enter(&b->tqbucket_lock);
1014*7c478bd9Sstevel@tonic-gate 			b->tqbucket_flags |= TQBUCKET_SUSPEND;
1015*7c478bd9Sstevel@tonic-gate 			mutex_exit(&b->tqbucket_lock);
1016*7c478bd9Sstevel@tonic-gate 		}
1017*7c478bd9Sstevel@tonic-gate 	}
1018*7c478bd9Sstevel@tonic-gate 	/*
1019*7c478bd9Sstevel@tonic-gate 	 * Mark task queue as being suspended. Needed for taskq_suspended().
1020*7c478bd9Sstevel@tonic-gate 	 */
1021*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
1022*7c478bd9Sstevel@tonic-gate 	ASSERT(!(tq->tq_flags & TASKQ_SUSPENDED));
1023*7c478bd9Sstevel@tonic-gate 	tq->tq_flags |= TASKQ_SUSPENDED;
1024*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tq->tq_lock);
1025*7c478bd9Sstevel@tonic-gate }
1026*7c478bd9Sstevel@tonic-gate 
1027*7c478bd9Sstevel@tonic-gate /*
1028*7c478bd9Sstevel@tonic-gate  * returns: 1 if tq is suspended, 0 otherwise.
1029*7c478bd9Sstevel@tonic-gate  */
1030*7c478bd9Sstevel@tonic-gate int
1031*7c478bd9Sstevel@tonic-gate taskq_suspended(taskq_t *tq)
1032*7c478bd9Sstevel@tonic-gate {
1033*7c478bd9Sstevel@tonic-gate 	return ((tq->tq_flags & TASKQ_SUSPENDED) != 0);
1034*7c478bd9Sstevel@tonic-gate }
1035*7c478bd9Sstevel@tonic-gate 
1036*7c478bd9Sstevel@tonic-gate /*
1037*7c478bd9Sstevel@tonic-gate  * Resume taskq execution.
1038*7c478bd9Sstevel@tonic-gate  */
1039*7c478bd9Sstevel@tonic-gate void
1040*7c478bd9Sstevel@tonic-gate taskq_resume(taskq_t *tq)
1041*7c478bd9Sstevel@tonic-gate {
1042*7c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&tq->tq_threadlock));
1043*7c478bd9Sstevel@tonic-gate 
1044*7c478bd9Sstevel@tonic-gate 	if (tq->tq_flags & TASKQ_DYNAMIC) {
1045*7c478bd9Sstevel@tonic-gate 		taskq_bucket_t *b = tq->tq_buckets;
1046*7c478bd9Sstevel@tonic-gate 		int bid = 0;
1047*7c478bd9Sstevel@tonic-gate 		for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) {
1048*7c478bd9Sstevel@tonic-gate 			mutex_enter(&b->tqbucket_lock);
1049*7c478bd9Sstevel@tonic-gate 			b->tqbucket_flags &= ~TQBUCKET_SUSPEND;
1050*7c478bd9Sstevel@tonic-gate 			mutex_exit(&b->tqbucket_lock);
1051*7c478bd9Sstevel@tonic-gate 		}
1052*7c478bd9Sstevel@tonic-gate 	}
1053*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
1054*7c478bd9Sstevel@tonic-gate 	ASSERT(tq->tq_flags & TASKQ_SUSPENDED);
1055*7c478bd9Sstevel@tonic-gate 	tq->tq_flags &= ~TASKQ_SUSPENDED;
1056*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tq->tq_lock);
1057*7c478bd9Sstevel@tonic-gate 
1058*7c478bd9Sstevel@tonic-gate 	rw_exit(&tq->tq_threadlock);
1059*7c478bd9Sstevel@tonic-gate }
1060*7c478bd9Sstevel@tonic-gate 
1061*7c478bd9Sstevel@tonic-gate int
1062*7c478bd9Sstevel@tonic-gate taskq_member(taskq_t *tq, kthread_t *thread)
1063*7c478bd9Sstevel@tonic-gate {
1064*7c478bd9Sstevel@tonic-gate 	return (thread->t_taskq == tq);
1065*7c478bd9Sstevel@tonic-gate }
1066*7c478bd9Sstevel@tonic-gate 
1067*7c478bd9Sstevel@tonic-gate /*
1068*7c478bd9Sstevel@tonic-gate  * Worker thread for processing task queue.
1069*7c478bd9Sstevel@tonic-gate  */
1070*7c478bd9Sstevel@tonic-gate static void
1071*7c478bd9Sstevel@tonic-gate taskq_thread(void *arg)
1072*7c478bd9Sstevel@tonic-gate {
1073*7c478bd9Sstevel@tonic-gate 	taskq_t *tq = arg;
1074*7c478bd9Sstevel@tonic-gate 	taskq_ent_t *tqe;
1075*7c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
1076*7c478bd9Sstevel@tonic-gate 	hrtime_t start, end;
1077*7c478bd9Sstevel@tonic-gate 
1078*7c478bd9Sstevel@tonic-gate 	if (tq->tq_flags & TASKQ_CPR_SAFE) {
1079*7c478bd9Sstevel@tonic-gate 		CALLB_CPR_INIT_SAFE(curthread, tq->tq_name);
1080*7c478bd9Sstevel@tonic-gate 	} else {
1081*7c478bd9Sstevel@tonic-gate 		CALLB_CPR_INIT(&cprinfo, &tq->tq_lock, callb_generic_cpr,
1082*7c478bd9Sstevel@tonic-gate 		    tq->tq_name);
1083*7c478bd9Sstevel@tonic-gate 	}
1084*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
1085*7c478bd9Sstevel@tonic-gate 	while (tq->tq_flags & TASKQ_ACTIVE) {
1086*7c478bd9Sstevel@tonic-gate 		if ((tqe = tq->tq_task.tqent_next) == &tq->tq_task) {
1087*7c478bd9Sstevel@tonic-gate 			if (--tq->tq_active == 0)
1088*7c478bd9Sstevel@tonic-gate 				cv_broadcast(&tq->tq_wait_cv);
1089*7c478bd9Sstevel@tonic-gate 			if (tq->tq_flags & TASKQ_CPR_SAFE) {
1090*7c478bd9Sstevel@tonic-gate 				cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
1091*7c478bd9Sstevel@tonic-gate 			} else {
1092*7c478bd9Sstevel@tonic-gate 				CALLB_CPR_SAFE_BEGIN(&cprinfo);
1093*7c478bd9Sstevel@tonic-gate 				cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
1094*7c478bd9Sstevel@tonic-gate 				CALLB_CPR_SAFE_END(&cprinfo, &tq->tq_lock);
1095*7c478bd9Sstevel@tonic-gate 			}
1096*7c478bd9Sstevel@tonic-gate 			tq->tq_active++;
1097*7c478bd9Sstevel@tonic-gate 			continue;
1098*7c478bd9Sstevel@tonic-gate 		}
1099*7c478bd9Sstevel@tonic-gate 		tqe->tqent_prev->tqent_next = tqe->tqent_next;
1100*7c478bd9Sstevel@tonic-gate 		tqe->tqent_next->tqent_prev = tqe->tqent_prev;
1101*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);
1102*7c478bd9Sstevel@tonic-gate 
1103*7c478bd9Sstevel@tonic-gate 		rw_enter(&tq->tq_threadlock, RW_READER);
1104*7c478bd9Sstevel@tonic-gate 		start = gethrtime();
1105*7c478bd9Sstevel@tonic-gate 		DTRACE_PROBE2(taskq__exec__start, taskq_t *, tq,
1106*7c478bd9Sstevel@tonic-gate 		    taskq_ent_t *, tqe);
1107*7c478bd9Sstevel@tonic-gate 		tqe->tqent_func(tqe->tqent_arg);
1108*7c478bd9Sstevel@tonic-gate 		DTRACE_PROBE2(taskq__exec__end, taskq_t *, tq,
1109*7c478bd9Sstevel@tonic-gate 		    taskq_ent_t *, tqe);
1110*7c478bd9Sstevel@tonic-gate 		end = gethrtime();
1111*7c478bd9Sstevel@tonic-gate 		rw_exit(&tq->tq_threadlock);
1112*7c478bd9Sstevel@tonic-gate 
1113*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tq->tq_lock);
1114*7c478bd9Sstevel@tonic-gate 		tq->tq_totaltime += end - start;
1115*7c478bd9Sstevel@tonic-gate 		tq->tq_executed++;
1116*7c478bd9Sstevel@tonic-gate 
1117*7c478bd9Sstevel@tonic-gate 		taskq_ent_free(tq, tqe);
1118*7c478bd9Sstevel@tonic-gate 	}
1119*7c478bd9Sstevel@tonic-gate 	tq->tq_nthreads--;
1120*7c478bd9Sstevel@tonic-gate 	cv_broadcast(&tq->tq_wait_cv);
1121*7c478bd9Sstevel@tonic-gate 	ASSERT(!(tq->tq_flags & TASKQ_CPR_SAFE));
1122*7c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
1123*7c478bd9Sstevel@tonic-gate 	thread_exit();
1124*7c478bd9Sstevel@tonic-gate }
1125*7c478bd9Sstevel@tonic-gate 
1126*7c478bd9Sstevel@tonic-gate /*
1127*7c478bd9Sstevel@tonic-gate  * Worker per-entry thread for dynamic dispatches.
1128*7c478bd9Sstevel@tonic-gate  */
1129*7c478bd9Sstevel@tonic-gate static void
1130*7c478bd9Sstevel@tonic-gate taskq_d_thread(taskq_ent_t *tqe)
1131*7c478bd9Sstevel@tonic-gate {
1132*7c478bd9Sstevel@tonic-gate 	taskq_bucket_t	*bucket = tqe->tqent_bucket;
1133*7c478bd9Sstevel@tonic-gate 	taskq_t		*tq = bucket->tqbucket_taskq;
1134*7c478bd9Sstevel@tonic-gate 	kmutex_t	*lock = &bucket->tqbucket_lock;
1135*7c478bd9Sstevel@tonic-gate 	kcondvar_t	*cv = &tqe->tqent_cv;
1136*7c478bd9Sstevel@tonic-gate 	callb_cpr_t	cprinfo;
1137*7c478bd9Sstevel@tonic-gate 	clock_t		w;
1138*7c478bd9Sstevel@tonic-gate 
1139*7c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, lock, callb_generic_cpr, tq->tq_name);
1140*7c478bd9Sstevel@tonic-gate 
1141*7c478bd9Sstevel@tonic-gate 	mutex_enter(lock);
1142*7c478bd9Sstevel@tonic-gate 
1143*7c478bd9Sstevel@tonic-gate 	for (;;) {
1144*7c478bd9Sstevel@tonic-gate 		/*
1145*7c478bd9Sstevel@tonic-gate 		 * If a task is scheduled (func != NULL), execute it, otherwise
1146*7c478bd9Sstevel@tonic-gate 		 * sleep, waiting for a job.
1147*7c478bd9Sstevel@tonic-gate 		 */
1148*7c478bd9Sstevel@tonic-gate 		if (tqe->tqent_func != NULL) {
1149*7c478bd9Sstevel@tonic-gate 			hrtime_t	start;
1150*7c478bd9Sstevel@tonic-gate 			hrtime_t	end;
1151*7c478bd9Sstevel@tonic-gate 
1152*7c478bd9Sstevel@tonic-gate 			ASSERT(bucket->tqbucket_nalloc > 0);
1153*7c478bd9Sstevel@tonic-gate 
1154*7c478bd9Sstevel@tonic-gate 			/*
1155*7c478bd9Sstevel@tonic-gate 			 * It is possible to free the entry right away before
1156*7c478bd9Sstevel@tonic-gate 			 * actually executing the task so that subsequent
1157*7c478bd9Sstevel@tonic-gate 			 * dispatches may immediately reuse it. But this,
1158*7c478bd9Sstevel@tonic-gate 			 * effectively, creates a two-length queue in the entry
1159*7c478bd9Sstevel@tonic-gate 			 * and may lead to a deadlock if the execution of the
1160*7c478bd9Sstevel@tonic-gate 			 * current task depends on the execution of the next
1161*7c478bd9Sstevel@tonic-gate 			 * scheduled task. So, we keep the entry busy until the
1162*7c478bd9Sstevel@tonic-gate 			 * task is processed.
1163*7c478bd9Sstevel@tonic-gate 			 */
1164*7c478bd9Sstevel@tonic-gate 
1165*7c478bd9Sstevel@tonic-gate 			mutex_exit(lock);
1166*7c478bd9Sstevel@tonic-gate 			start = gethrtime();
1167*7c478bd9Sstevel@tonic-gate 			DTRACE_PROBE3(taskq__d__exec__start, taskq_t *, tq,
1168*7c478bd9Sstevel@tonic-gate 			    taskq_bucket_t *, bucket, taskq_ent_t *, tqe);
1169*7c478bd9Sstevel@tonic-gate 			tqe->tqent_func(tqe->tqent_arg);
1170*7c478bd9Sstevel@tonic-gate 			DTRACE_PROBE3(taskq__d__exec__end, taskq_t *, tq,
1171*7c478bd9Sstevel@tonic-gate 			    taskq_bucket_t *, bucket, taskq_ent_t *, tqe);
1172*7c478bd9Sstevel@tonic-gate 			end = gethrtime();
1173*7c478bd9Sstevel@tonic-gate 			mutex_enter(lock);
1174*7c478bd9Sstevel@tonic-gate 			bucket->tqbucket_totaltime += end - start;
1175*7c478bd9Sstevel@tonic-gate 
1176*7c478bd9Sstevel@tonic-gate 			/*
1177*7c478bd9Sstevel@tonic-gate 			 * Return the entry to the bucket free list.
1178*7c478bd9Sstevel@tonic-gate 			 */
1179*7c478bd9Sstevel@tonic-gate 			tqe->tqent_func = NULL;
1180*7c478bd9Sstevel@tonic-gate 			TQ_APPEND(bucket->tqbucket_freelist, tqe);
1181*7c478bd9Sstevel@tonic-gate 			bucket->tqbucket_nalloc--;
1182*7c478bd9Sstevel@tonic-gate 			bucket->tqbucket_nfree++;
1183*7c478bd9Sstevel@tonic-gate 			ASSERT(!IS_EMPTY(bucket->tqbucket_freelist));
1184*7c478bd9Sstevel@tonic-gate 			/*
1185*7c478bd9Sstevel@tonic-gate 			 * taskq_wait() waits for nalloc to drop to zero on
1186*7c478bd9Sstevel@tonic-gate 			 * tqbucket_cv.
1187*7c478bd9Sstevel@tonic-gate 			 */
1188*7c478bd9Sstevel@tonic-gate 			cv_signal(&bucket->tqbucket_cv);
1189*7c478bd9Sstevel@tonic-gate 		}
1190*7c478bd9Sstevel@tonic-gate 
1191*7c478bd9Sstevel@tonic-gate 		/*
1192*7c478bd9Sstevel@tonic-gate 		 * At this point the entry must be in the bucket free list -
1193*7c478bd9Sstevel@tonic-gate 		 * either because it was there initially or because it just
1194*7c478bd9Sstevel@tonic-gate 		 * finished executing a task and put itself on the free list.
1195*7c478bd9Sstevel@tonic-gate 		 */
1196*7c478bd9Sstevel@tonic-gate 		ASSERT(bucket->tqbucket_nfree > 0);
1197*7c478bd9Sstevel@tonic-gate 		/*
1198*7c478bd9Sstevel@tonic-gate 		 * Go to sleep unless we are closing.
1199*7c478bd9Sstevel@tonic-gate 		 * If a thread is sleeping too long, it dies.
1200*7c478bd9Sstevel@tonic-gate 		 */
1201*7c478bd9Sstevel@tonic-gate 		if (! (bucket->tqbucket_flags & TQBUCKET_CLOSE)) {
1202*7c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
1203*7c478bd9Sstevel@tonic-gate 			w = cv_timedwait(cv, lock, lbolt +
1204*7c478bd9Sstevel@tonic-gate 			    taskq_thread_timeout * hz);
1205*7c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, lock);
1206*7c478bd9Sstevel@tonic-gate 		}
1207*7c478bd9Sstevel@tonic-gate 
1208*7c478bd9Sstevel@tonic-gate 		/*
1209*7c478bd9Sstevel@tonic-gate 		 * At this point we may be in two different states:
1210*7c478bd9Sstevel@tonic-gate 		 *
1211*7c478bd9Sstevel@tonic-gate 		 * (1) tqent_func is set which means that a new task is
1212*7c478bd9Sstevel@tonic-gate 		 *	dispatched and we need to execute it.
1213*7c478bd9Sstevel@tonic-gate 		 *
1214*7c478bd9Sstevel@tonic-gate 		 * (2) Thread is sleeping for too long or we are closing. In
1215*7c478bd9Sstevel@tonic-gate 		 *	both cases destroy the thread and the entry.
1216*7c478bd9Sstevel@tonic-gate 		 */
1217*7c478bd9Sstevel@tonic-gate 
1218*7c478bd9Sstevel@tonic-gate 		/* If func is NULL we should be on the freelist. */
1219*7c478bd9Sstevel@tonic-gate 		ASSERT((tqe->tqent_func != NULL) ||
1220*7c478bd9Sstevel@tonic-gate 		    (bucket->tqbucket_nfree > 0));
1221*7c478bd9Sstevel@tonic-gate 		/* If func is non-NULL we should be allocated */
1222*7c478bd9Sstevel@tonic-gate 		ASSERT((tqe->tqent_func == NULL) ||
1223*7c478bd9Sstevel@tonic-gate 		    (bucket->tqbucket_nalloc > 0));
1224*7c478bd9Sstevel@tonic-gate 
1225*7c478bd9Sstevel@tonic-gate 		/* Check freelist consistency */
1226*7c478bd9Sstevel@tonic-gate 		ASSERT((bucket->tqbucket_nfree > 0) ||
1227*7c478bd9Sstevel@tonic-gate 		    IS_EMPTY(bucket->tqbucket_freelist));
1228*7c478bd9Sstevel@tonic-gate 		ASSERT((bucket->tqbucket_nfree == 0) ||
1229*7c478bd9Sstevel@tonic-gate 		    !IS_EMPTY(bucket->tqbucket_freelist));
1230*7c478bd9Sstevel@tonic-gate 
1231*7c478bd9Sstevel@tonic-gate 		if ((tqe->tqent_func == NULL) &&
1232*7c478bd9Sstevel@tonic-gate 		    ((w == -1) || (bucket->tqbucket_flags & TQBUCKET_CLOSE))) {
1233*7c478bd9Sstevel@tonic-gate 			/*
1234*7c478bd9Sstevel@tonic-gate 			 * This thread is sleeping for too long or we are
1235*7c478bd9Sstevel@tonic-gate 			 * closing - time to die.
1236*7c478bd9Sstevel@tonic-gate 			 * Thread creation/destruction happens rarely,
1237*7c478bd9Sstevel@tonic-gate 			 * so grabbing the lock is not a big performance issue.
1238*7c478bd9Sstevel@tonic-gate 			 * The bucket lock is dropped by CALLB_CPR_EXIT().
1239*7c478bd9Sstevel@tonic-gate 			 */
1240*7c478bd9Sstevel@tonic-gate 
1241*7c478bd9Sstevel@tonic-gate 			/* Remove the entry from the free list. */
1242*7c478bd9Sstevel@tonic-gate 			tqe->tqent_prev->tqent_next = tqe->tqent_next;
1243*7c478bd9Sstevel@tonic-gate 			tqe->tqent_next->tqent_prev = tqe->tqent_prev;
1244*7c478bd9Sstevel@tonic-gate 			ASSERT(bucket->tqbucket_nfree > 0);
1245*7c478bd9Sstevel@tonic-gate 			bucket->tqbucket_nfree--;
1246*7c478bd9Sstevel@tonic-gate 
1247*7c478bd9Sstevel@tonic-gate 			TQ_STAT(bucket, tqs_tdeaths);
1248*7c478bd9Sstevel@tonic-gate 			cv_signal(&bucket->tqbucket_cv);
1249*7c478bd9Sstevel@tonic-gate 			tqe->tqent_thread = NULL;
1250*7c478bd9Sstevel@tonic-gate 			mutex_enter(&tq->tq_lock);
1251*7c478bd9Sstevel@tonic-gate 			tq->tq_tdeaths++;
1252*7c478bd9Sstevel@tonic-gate 			mutex_exit(&tq->tq_lock);
1253*7c478bd9Sstevel@tonic-gate 			CALLB_CPR_EXIT(&cprinfo);
1254*7c478bd9Sstevel@tonic-gate 			kmem_cache_free(taskq_ent_cache, tqe);
1255*7c478bd9Sstevel@tonic-gate 			thread_exit();
1256*7c478bd9Sstevel@tonic-gate 		}
1257*7c478bd9Sstevel@tonic-gate 	}
1258*7c478bd9Sstevel@tonic-gate }
1259*7c478bd9Sstevel@tonic-gate 
1260*7c478bd9Sstevel@tonic-gate 
1261*7c478bd9Sstevel@tonic-gate /*
1262*7c478bd9Sstevel@tonic-gate  * Taskq creation. May sleep for memory.
1263*7c478bd9Sstevel@tonic-gate  * Always use automatically generated instances to avoid kstat name space
1264*7c478bd9Sstevel@tonic-gate  * collisions.
1265*7c478bd9Sstevel@tonic-gate  */
1266*7c478bd9Sstevel@tonic-gate 
1267*7c478bd9Sstevel@tonic-gate taskq_t *
1268*7c478bd9Sstevel@tonic-gate taskq_create(const char *name, int nthreads, pri_t pri, int minalloc,
1269*7c478bd9Sstevel@tonic-gate     int maxalloc, uint_t flags)
1270*7c478bd9Sstevel@tonic-gate {
1271*7c478bd9Sstevel@tonic-gate 	return taskq_create_common(name, 0, nthreads, pri, minalloc,
1272*7c478bd9Sstevel@tonic-gate 	    maxalloc, flags | TASKQ_NOINSTANCE);
1273*7c478bd9Sstevel@tonic-gate }
1274*7c478bd9Sstevel@tonic-gate 
1275*7c478bd9Sstevel@tonic-gate /*
1276*7c478bd9Sstevel@tonic-gate  * Create an instance of task queue. It is legal to create task queues with the
1277*7c478bd9Sstevel@tonic-gate  * same name and different instances.
1278*7c478bd9Sstevel@tonic-gate  *
1279*7c478bd9Sstevel@tonic-gate  * taskq_create_instance is used by ddi_taskq_create() where it gets the
1280*7c478bd9Sstevel@tonic-gate  * instance from ddi_get_instance(). In some cases the instance is not
1281*7c478bd9Sstevel@tonic-gate  * initialized and is set to -1. This case is handled as if no instance was
1282*7c478bd9Sstevel@tonic-gate  * passed at all.
1283*7c478bd9Sstevel@tonic-gate  */
1284*7c478bd9Sstevel@tonic-gate taskq_t *
1285*7c478bd9Sstevel@tonic-gate taskq_create_instance(const char *name, int instance, int nthreads, pri_t pri,
1286*7c478bd9Sstevel@tonic-gate     int minalloc, int maxalloc, uint_t flags)
1287*7c478bd9Sstevel@tonic-gate {
1288*7c478bd9Sstevel@tonic-gate 	ASSERT((instance >= 0) || (instance == -1));
1289*7c478bd9Sstevel@tonic-gate 
1290*7c478bd9Sstevel@tonic-gate 	if (instance < 0) {
1291*7c478bd9Sstevel@tonic-gate 		flags |= TASKQ_NOINSTANCE;
1292*7c478bd9Sstevel@tonic-gate 	}
1293*7c478bd9Sstevel@tonic-gate 
1294*7c478bd9Sstevel@tonic-gate 	return (taskq_create_common(name, instance, nthreads,
1295*7c478bd9Sstevel@tonic-gate 	    pri, minalloc, maxalloc, flags));
1296*7c478bd9Sstevel@tonic-gate }
1297*7c478bd9Sstevel@tonic-gate 
1298*7c478bd9Sstevel@tonic-gate static taskq_t *
1299*7c478bd9Sstevel@tonic-gate taskq_create_common(const char *name, int instance, int nthreads, pri_t pri,
1300*7c478bd9Sstevel@tonic-gate     int minalloc, int maxalloc, uint_t flags)
1301*7c478bd9Sstevel@tonic-gate {
1302*7c478bd9Sstevel@tonic-gate 	taskq_t *tq = kmem_cache_alloc(taskq_cache, KM_SLEEP);
1303*7c478bd9Sstevel@tonic-gate 	uint_t ncpus = ((boot_max_ncpus == -1) ? max_ncpus : boot_max_ncpus);
1304*7c478bd9Sstevel@tonic-gate 	uint_t bsize;	/* # of buckets - always power of 2 */
1305*7c478bd9Sstevel@tonic-gate 
1306*7c478bd9Sstevel@tonic-gate 	/*
1307*7c478bd9Sstevel@tonic-gate 	 * TASKQ_CPR_SAFE and TASKQ_DYNAMIC flags are mutually exclusive.
1308*7c478bd9Sstevel@tonic-gate 	 */
1309*7c478bd9Sstevel@tonic-gate 	ASSERT((flags & (TASKQ_DYNAMIC | TASKQ_CPR_SAFE)) !=
1310*7c478bd9Sstevel@tonic-gate 	    ((TASKQ_DYNAMIC | TASKQ_CPR_SAFE)));
1311*7c478bd9Sstevel@tonic-gate 
1312*7c478bd9Sstevel@tonic-gate 	ASSERT(tq->tq_buckets == NULL);
1313*7c478bd9Sstevel@tonic-gate 
1314*7c478bd9Sstevel@tonic-gate 	bsize = 1 << (highbit(ncpus) - 1);
1315*7c478bd9Sstevel@tonic-gate 	ASSERT(bsize >= 1);
1316*7c478bd9Sstevel@tonic-gate 	bsize = MIN(bsize, taskq_maxbuckets);
1317*7c478bd9Sstevel@tonic-gate 
1318*7c478bd9Sstevel@tonic-gate 	tq->tq_maxsize = nthreads;
1319*7c478bd9Sstevel@tonic-gate 
1320*7c478bd9Sstevel@tonic-gate 	/* For non-dynamic task queues use just one backup thread */
1321*7c478bd9Sstevel@tonic-gate 	if (flags & TASKQ_DYNAMIC)
1322*7c478bd9Sstevel@tonic-gate 		nthreads = 1;
1323*7c478bd9Sstevel@tonic-gate 
1324*7c478bd9Sstevel@tonic-gate 	(void) strncpy(tq->tq_name, name, TASKQ_NAMELEN + 1);
1325*7c478bd9Sstevel@tonic-gate 	tq->tq_name[TASKQ_NAMELEN] = '\0';
1326*7c478bd9Sstevel@tonic-gate 	/* Make sure the name conforms to the rules for C indentifiers */
1327*7c478bd9Sstevel@tonic-gate 	strident_canon(tq->tq_name, TASKQ_NAMELEN);
1328*7c478bd9Sstevel@tonic-gate 
1329*7c478bd9Sstevel@tonic-gate 	tq->tq_flags = flags | TASKQ_ACTIVE;
1330*7c478bd9Sstevel@tonic-gate 	tq->tq_active = nthreads;
1331*7c478bd9Sstevel@tonic-gate 	tq->tq_instance = instance;
1332*7c478bd9Sstevel@tonic-gate 	tq->tq_nthreads = nthreads;
1333*7c478bd9Sstevel@tonic-gate 	tq->tq_minalloc = minalloc;
1334*7c478bd9Sstevel@tonic-gate 	tq->tq_maxalloc = maxalloc;
1335*7c478bd9Sstevel@tonic-gate 	tq->tq_nbuckets = bsize;
1336*7c478bd9Sstevel@tonic-gate 	tq->tq_pri = pri;
1337*7c478bd9Sstevel@tonic-gate 
1338*7c478bd9Sstevel@tonic-gate 	if (flags & TASKQ_PREPOPULATE) {
1339*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tq->tq_lock);
1340*7c478bd9Sstevel@tonic-gate 		while (minalloc-- > 0)
1341*7c478bd9Sstevel@tonic-gate 			taskq_ent_free(tq, taskq_ent_alloc(tq, TQ_SLEEP));
1342*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);
1343*7c478bd9Sstevel@tonic-gate 	}
1344*7c478bd9Sstevel@tonic-gate 
1345*7c478bd9Sstevel@tonic-gate 	if (nthreads == 1) {
1346*7c478bd9Sstevel@tonic-gate 		tq->tq_thread = thread_create(NULL, 0, taskq_thread, tq,
1347*7c478bd9Sstevel@tonic-gate 		    0, &p0, TS_RUN, pri);
1348*7c478bd9Sstevel@tonic-gate 		/*
1349*7c478bd9Sstevel@tonic-gate 		 * No need to take thread_lock to change the field: no one can
1350*7c478bd9Sstevel@tonic-gate 		 * reference it at this point.
1351*7c478bd9Sstevel@tonic-gate 		 */
1352*7c478bd9Sstevel@tonic-gate 		tq->tq_thread->t_taskq = tq;
1353*7c478bd9Sstevel@tonic-gate 	} else {
1354*7c478bd9Sstevel@tonic-gate 		kthread_t **tpp = kmem_alloc(sizeof (kthread_t *) * nthreads,
1355*7c478bd9Sstevel@tonic-gate 		    KM_SLEEP);
1356*7c478bd9Sstevel@tonic-gate 
1357*7c478bd9Sstevel@tonic-gate 		tq->tq_threadlist = tpp;
1358*7c478bd9Sstevel@tonic-gate 
1359*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tq->tq_lock);
1360*7c478bd9Sstevel@tonic-gate 		while (nthreads-- > 0) {
1361*7c478bd9Sstevel@tonic-gate 			*tpp = thread_create(NULL, 0, taskq_thread, tq,
1362*7c478bd9Sstevel@tonic-gate 			    0, &p0, TS_RUN, pri);
1363*7c478bd9Sstevel@tonic-gate 			(*tpp)->t_taskq = tq;
1364*7c478bd9Sstevel@tonic-gate 			tpp++;
1365*7c478bd9Sstevel@tonic-gate 		}
1366*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);
1367*7c478bd9Sstevel@tonic-gate 	}
1368*7c478bd9Sstevel@tonic-gate 
1369*7c478bd9Sstevel@tonic-gate 	if (flags & TASKQ_DYNAMIC) {
1370*7c478bd9Sstevel@tonic-gate 		taskq_bucket_t *bucket = kmem_zalloc(sizeof (taskq_bucket_t) *
1371*7c478bd9Sstevel@tonic-gate 		    bsize, KM_SLEEP);
1372*7c478bd9Sstevel@tonic-gate 		int b_id;
1373*7c478bd9Sstevel@tonic-gate 
1374*7c478bd9Sstevel@tonic-gate 		tq->tq_buckets = bucket;
1375*7c478bd9Sstevel@tonic-gate 
1376*7c478bd9Sstevel@tonic-gate 		/* Initialize each bucket */
1377*7c478bd9Sstevel@tonic-gate 		for (b_id = 0; b_id < bsize; b_id++, bucket++) {
1378*7c478bd9Sstevel@tonic-gate 			mutex_init(&bucket->tqbucket_lock, NULL, MUTEX_DEFAULT,
1379*7c478bd9Sstevel@tonic-gate 			    NULL);
1380*7c478bd9Sstevel@tonic-gate 			cv_init(&bucket->tqbucket_cv, NULL, CV_DEFAULT, NULL);
1381*7c478bd9Sstevel@tonic-gate 			bucket->tqbucket_taskq = tq;
1382*7c478bd9Sstevel@tonic-gate 			bucket->tqbucket_freelist.tqent_next =
1383*7c478bd9Sstevel@tonic-gate 			    bucket->tqbucket_freelist.tqent_prev =
1384*7c478bd9Sstevel@tonic-gate 			    &bucket->tqbucket_freelist;
1385*7c478bd9Sstevel@tonic-gate 			if (flags & TASKQ_PREPOPULATE)
1386*7c478bd9Sstevel@tonic-gate 				taskq_bucket_extend(bucket);
1387*7c478bd9Sstevel@tonic-gate 		}
1388*7c478bd9Sstevel@tonic-gate 	}
1389*7c478bd9Sstevel@tonic-gate 
1390*7c478bd9Sstevel@tonic-gate 	/*
1391*7c478bd9Sstevel@tonic-gate 	 * Install kstats.
1392*7c478bd9Sstevel@tonic-gate 	 * We have two cases:
1393*7c478bd9Sstevel@tonic-gate 	 *   1) Instance is provided to taskq_create_instance(). In this case it
1394*7c478bd9Sstevel@tonic-gate 	 * 	should be >= 0 and we use it.
1395*7c478bd9Sstevel@tonic-gate 	 *
1396*7c478bd9Sstevel@tonic-gate 	 *   2) Instance is not provided and is automatically generated
1397*7c478bd9Sstevel@tonic-gate 	 */
1398*7c478bd9Sstevel@tonic-gate 	if (flags & TASKQ_NOINSTANCE) {
1399*7c478bd9Sstevel@tonic-gate 		instance = tq->tq_instance =
1400*7c478bd9Sstevel@tonic-gate 		    (int)(uintptr_t)vmem_alloc(taskq_id_arena, 1, VM_SLEEP);
1401*7c478bd9Sstevel@tonic-gate 	}
1402*7c478bd9Sstevel@tonic-gate 
1403*7c478bd9Sstevel@tonic-gate 	if (flags & TASKQ_DYNAMIC) {
1404*7c478bd9Sstevel@tonic-gate 		if ((tq->tq_kstat = kstat_create("unix", instance,
1405*7c478bd9Sstevel@tonic-gate 			tq->tq_name, "taskq_d", KSTAT_TYPE_NAMED,
1406*7c478bd9Sstevel@tonic-gate 			sizeof (taskq_d_kstat) / sizeof (kstat_named_t),
1407*7c478bd9Sstevel@tonic-gate 			KSTAT_FLAG_VIRTUAL)) != NULL) {
1408*7c478bd9Sstevel@tonic-gate 			tq->tq_kstat->ks_lock = &taskq_d_kstat_lock;
1409*7c478bd9Sstevel@tonic-gate 			tq->tq_kstat->ks_data = &taskq_d_kstat;
1410*7c478bd9Sstevel@tonic-gate 			tq->tq_kstat->ks_update = taskq_d_kstat_update;
1411*7c478bd9Sstevel@tonic-gate 			tq->tq_kstat->ks_private = tq;
1412*7c478bd9Sstevel@tonic-gate 			kstat_install(tq->tq_kstat);
1413*7c478bd9Sstevel@tonic-gate 		}
1414*7c478bd9Sstevel@tonic-gate 	} else {
1415*7c478bd9Sstevel@tonic-gate 		if ((tq->tq_kstat = kstat_create("unix", instance, tq->tq_name,
1416*7c478bd9Sstevel@tonic-gate 			"taskq", KSTAT_TYPE_NAMED,
1417*7c478bd9Sstevel@tonic-gate 			sizeof (taskq_kstat) / sizeof (kstat_named_t),
1418*7c478bd9Sstevel@tonic-gate 			KSTAT_FLAG_VIRTUAL)) != NULL) {
1419*7c478bd9Sstevel@tonic-gate 			tq->tq_kstat->ks_lock = &taskq_kstat_lock;
1420*7c478bd9Sstevel@tonic-gate 			tq->tq_kstat->ks_data = &taskq_kstat;
1421*7c478bd9Sstevel@tonic-gate 			tq->tq_kstat->ks_update = taskq_kstat_update;
1422*7c478bd9Sstevel@tonic-gate 			tq->tq_kstat->ks_private = tq;
1423*7c478bd9Sstevel@tonic-gate 			kstat_install(tq->tq_kstat);
1424*7c478bd9Sstevel@tonic-gate 		}
1425*7c478bd9Sstevel@tonic-gate 	}
1426*7c478bd9Sstevel@tonic-gate 
1427*7c478bd9Sstevel@tonic-gate 	return (tq);
1428*7c478bd9Sstevel@tonic-gate }
1429*7c478bd9Sstevel@tonic-gate 
1430*7c478bd9Sstevel@tonic-gate /*
1431*7c478bd9Sstevel@tonic-gate  * taskq_destroy().
1432*7c478bd9Sstevel@tonic-gate  *
1433*7c478bd9Sstevel@tonic-gate  * Assumes: by the time taskq_destroy is called no one will use this task queue
1434*7c478bd9Sstevel@tonic-gate  * in any way and no one will try to dispatch entries in it.
1435*7c478bd9Sstevel@tonic-gate  */
1436*7c478bd9Sstevel@tonic-gate void
1437*7c478bd9Sstevel@tonic-gate taskq_destroy(taskq_t *tq)
1438*7c478bd9Sstevel@tonic-gate {
1439*7c478bd9Sstevel@tonic-gate 	taskq_bucket_t *b = tq->tq_buckets;
1440*7c478bd9Sstevel@tonic-gate 	int bid = 0;
1441*7c478bd9Sstevel@tonic-gate 
1442*7c478bd9Sstevel@tonic-gate 	ASSERT(! (tq->tq_flags & TASKQ_CPR_SAFE));
1443*7c478bd9Sstevel@tonic-gate 
1444*7c478bd9Sstevel@tonic-gate 	/*
1445*7c478bd9Sstevel@tonic-gate 	 * Destroy kstats.
1446*7c478bd9Sstevel@tonic-gate 	 */
1447*7c478bd9Sstevel@tonic-gate 	if (tq->tq_kstat != NULL) {
1448*7c478bd9Sstevel@tonic-gate 		kstat_delete(tq->tq_kstat);
1449*7c478bd9Sstevel@tonic-gate 		tq->tq_kstat = NULL;
1450*7c478bd9Sstevel@tonic-gate 	}
1451*7c478bd9Sstevel@tonic-gate 
1452*7c478bd9Sstevel@tonic-gate 	/*
1453*7c478bd9Sstevel@tonic-gate 	 * Destroy instance if needed.
1454*7c478bd9Sstevel@tonic-gate 	 */
1455*7c478bd9Sstevel@tonic-gate 	if (tq->tq_flags & TASKQ_NOINSTANCE) {
1456*7c478bd9Sstevel@tonic-gate 		vmem_free(taskq_id_arena, (void *)(uintptr_t)(tq->tq_instance),
1457*7c478bd9Sstevel@tonic-gate 		    1);
1458*7c478bd9Sstevel@tonic-gate 		tq->tq_instance = 0;
1459*7c478bd9Sstevel@tonic-gate 	}
1460*7c478bd9Sstevel@tonic-gate 
1461*7c478bd9Sstevel@tonic-gate 	/*
1462*7c478bd9Sstevel@tonic-gate 	 * Wait for any pending entries to complete.
1463*7c478bd9Sstevel@tonic-gate 	 */
1464*7c478bd9Sstevel@tonic-gate 	taskq_wait(tq);
1465*7c478bd9Sstevel@tonic-gate 
1466*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
1467*7c478bd9Sstevel@tonic-gate 	ASSERT((tq->tq_task.tqent_next == &tq->tq_task) &&
1468*7c478bd9Sstevel@tonic-gate 	    (tq->tq_active == 0));
1469*7c478bd9Sstevel@tonic-gate 
1470*7c478bd9Sstevel@tonic-gate 	if ((tq->tq_nthreads > 1) && (tq->tq_threadlist != NULL))
1471*7c478bd9Sstevel@tonic-gate 		kmem_free(tq->tq_threadlist, sizeof (kthread_t *) *
1472*7c478bd9Sstevel@tonic-gate 		    tq->tq_nthreads);
1473*7c478bd9Sstevel@tonic-gate 
1474*7c478bd9Sstevel@tonic-gate 	tq->tq_flags &= ~TASKQ_ACTIVE;
1475*7c478bd9Sstevel@tonic-gate 	cv_broadcast(&tq->tq_dispatch_cv);
1476*7c478bd9Sstevel@tonic-gate 	while (tq->tq_nthreads != 0)
1477*7c478bd9Sstevel@tonic-gate 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
1478*7c478bd9Sstevel@tonic-gate 
1479*7c478bd9Sstevel@tonic-gate 	tq->tq_minalloc = 0;
1480*7c478bd9Sstevel@tonic-gate 	while (tq->tq_nalloc != 0)
1481*7c478bd9Sstevel@tonic-gate 		taskq_ent_free(tq, taskq_ent_alloc(tq, TQ_SLEEP));
1482*7c478bd9Sstevel@tonic-gate 
1483*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tq->tq_lock);
1484*7c478bd9Sstevel@tonic-gate 
1485*7c478bd9Sstevel@tonic-gate 	/*
1486*7c478bd9Sstevel@tonic-gate 	 * Mark each bucket as closing and wakeup all sleeping threads.
1487*7c478bd9Sstevel@tonic-gate 	 */
1488*7c478bd9Sstevel@tonic-gate 	for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) {
1489*7c478bd9Sstevel@tonic-gate 		taskq_ent_t *tqe;
1490*7c478bd9Sstevel@tonic-gate 
1491*7c478bd9Sstevel@tonic-gate 		mutex_enter(&b->tqbucket_lock);
1492*7c478bd9Sstevel@tonic-gate 
1493*7c478bd9Sstevel@tonic-gate 		b->tqbucket_flags |= TQBUCKET_CLOSE;
1494*7c478bd9Sstevel@tonic-gate 		/* Wakeup all sleeping threads */
1495*7c478bd9Sstevel@tonic-gate 
1496*7c478bd9Sstevel@tonic-gate 		for (tqe = b->tqbucket_freelist.tqent_next;
1497*7c478bd9Sstevel@tonic-gate 		    tqe != &b->tqbucket_freelist; tqe = tqe->tqent_next)
1498*7c478bd9Sstevel@tonic-gate 			cv_signal(&tqe->tqent_cv);
1499*7c478bd9Sstevel@tonic-gate 
1500*7c478bd9Sstevel@tonic-gate 		ASSERT(b->tqbucket_nalloc == 0);
1501*7c478bd9Sstevel@tonic-gate 
1502*7c478bd9Sstevel@tonic-gate 		/*
1503*7c478bd9Sstevel@tonic-gate 		 * At this point we waited for all pending jobs to complete (in
1504*7c478bd9Sstevel@tonic-gate 		 * both the task queue and the bucket and no new jobs should
1505*7c478bd9Sstevel@tonic-gate 		 * arrive. Wait for all threads to die.
1506*7c478bd9Sstevel@tonic-gate 		 */
1507*7c478bd9Sstevel@tonic-gate 		while (b->tqbucket_nfree > 0)
1508*7c478bd9Sstevel@tonic-gate 			cv_wait(&b->tqbucket_cv, &b->tqbucket_lock);
1509*7c478bd9Sstevel@tonic-gate 		mutex_exit(&b->tqbucket_lock);
1510*7c478bd9Sstevel@tonic-gate 		mutex_destroy(&b->tqbucket_lock);
1511*7c478bd9Sstevel@tonic-gate 		cv_destroy(&b->tqbucket_cv);
1512*7c478bd9Sstevel@tonic-gate 	}
1513*7c478bd9Sstevel@tonic-gate 
1514*7c478bd9Sstevel@tonic-gate 	if (tq->tq_buckets != NULL) {
1515*7c478bd9Sstevel@tonic-gate 		ASSERT(tq->tq_flags & TASKQ_DYNAMIC);
1516*7c478bd9Sstevel@tonic-gate 		kmem_free(tq->tq_buckets,
1517*7c478bd9Sstevel@tonic-gate 		    sizeof (taskq_bucket_t) * tq->tq_nbuckets);
1518*7c478bd9Sstevel@tonic-gate 
1519*7c478bd9Sstevel@tonic-gate 		/* Cleanup fields before returning tq to the cache */
1520*7c478bd9Sstevel@tonic-gate 		tq->tq_buckets = NULL;
1521*7c478bd9Sstevel@tonic-gate 		tq->tq_tcreates = 0;
1522*7c478bd9Sstevel@tonic-gate 		tq->tq_tdeaths = 0;
1523*7c478bd9Sstevel@tonic-gate 	} else {
1524*7c478bd9Sstevel@tonic-gate 		ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC));
1525*7c478bd9Sstevel@tonic-gate 	}
1526*7c478bd9Sstevel@tonic-gate 
1527*7c478bd9Sstevel@tonic-gate 	tq->tq_totaltime = 0;
1528*7c478bd9Sstevel@tonic-gate 	tq->tq_tasks = 0;
1529*7c478bd9Sstevel@tonic-gate 	tq->tq_maxtasks = 0;
1530*7c478bd9Sstevel@tonic-gate 	tq->tq_executed = 0;
1531*7c478bd9Sstevel@tonic-gate 	kmem_cache_free(taskq_cache, tq);
1532*7c478bd9Sstevel@tonic-gate }
1533*7c478bd9Sstevel@tonic-gate 
1534*7c478bd9Sstevel@tonic-gate /*
1535*7c478bd9Sstevel@tonic-gate  * Extend a bucket with a new entry on the free list and attach a worker thread
1536*7c478bd9Sstevel@tonic-gate  * to it.
1537*7c478bd9Sstevel@tonic-gate  *
1538*7c478bd9Sstevel@tonic-gate  * Argument: pointer to the bucket.
1539*7c478bd9Sstevel@tonic-gate  *
1540*7c478bd9Sstevel@tonic-gate  * This function may quietly fail. It is only used by taskq_dispatch() which
1541*7c478bd9Sstevel@tonic-gate  * handles such failures properly.
1542*7c478bd9Sstevel@tonic-gate  */
1543*7c478bd9Sstevel@tonic-gate static void
1544*7c478bd9Sstevel@tonic-gate taskq_bucket_extend(void *arg)
1545*7c478bd9Sstevel@tonic-gate {
1546*7c478bd9Sstevel@tonic-gate 	taskq_ent_t *tqe;
1547*7c478bd9Sstevel@tonic-gate 	taskq_bucket_t *b = (taskq_bucket_t *)arg;
1548*7c478bd9Sstevel@tonic-gate 	taskq_t *tq = b->tqbucket_taskq;
1549*7c478bd9Sstevel@tonic-gate 	int nthreads;
1550*7c478bd9Sstevel@tonic-gate 
1551*7c478bd9Sstevel@tonic-gate 	if (! ENOUGH_MEMORY()) {
1552*7c478bd9Sstevel@tonic-gate 		TQ_STAT(b, tqs_nomem);
1553*7c478bd9Sstevel@tonic-gate 		return;
1554*7c478bd9Sstevel@tonic-gate 	}
1555*7c478bd9Sstevel@tonic-gate 
1556*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tq->tq_lock);
1557*7c478bd9Sstevel@tonic-gate 
1558*7c478bd9Sstevel@tonic-gate 	/*
1559*7c478bd9Sstevel@tonic-gate 	 * Observe global taskq limits on the number of threads.
1560*7c478bd9Sstevel@tonic-gate 	 */
1561*7c478bd9Sstevel@tonic-gate 	if (tq->tq_tcreates++ - tq->tq_tdeaths > tq->tq_maxsize) {
1562*7c478bd9Sstevel@tonic-gate 		tq->tq_tcreates--;
1563*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);
1564*7c478bd9Sstevel@tonic-gate 		return;
1565*7c478bd9Sstevel@tonic-gate 	}
1566*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tq->tq_lock);
1567*7c478bd9Sstevel@tonic-gate 
1568*7c478bd9Sstevel@tonic-gate 	tqe = kmem_cache_alloc(taskq_ent_cache, KM_NOSLEEP);
1569*7c478bd9Sstevel@tonic-gate 
1570*7c478bd9Sstevel@tonic-gate 	if (tqe == NULL) {
1571*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tq->tq_lock);
1572*7c478bd9Sstevel@tonic-gate 		TQ_STAT(b, tqs_nomem);
1573*7c478bd9Sstevel@tonic-gate 		tq->tq_tcreates--;
1574*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tq->tq_lock);
1575*7c478bd9Sstevel@tonic-gate 		return;
1576*7c478bd9Sstevel@tonic-gate 	}
1577*7c478bd9Sstevel@tonic-gate 
1578*7c478bd9Sstevel@tonic-gate 	ASSERT(tqe->tqent_thread == NULL);
1579*7c478bd9Sstevel@tonic-gate 
1580*7c478bd9Sstevel@tonic-gate 	tqe->tqent_bucket = b;
1581*7c478bd9Sstevel@tonic-gate 
1582*7c478bd9Sstevel@tonic-gate 	/*
1583*7c478bd9Sstevel@tonic-gate 	 * Create a thread in a TS_STOPPED state first. If it is successfully
1584*7c478bd9Sstevel@tonic-gate 	 * created, place the entry on the free list and start the thread.
1585*7c478bd9Sstevel@tonic-gate 	 */
1586*7c478bd9Sstevel@tonic-gate 	tqe->tqent_thread = thread_create(NULL, 0, taskq_d_thread, tqe,
1587*7c478bd9Sstevel@tonic-gate 	    0, &p0, TS_STOPPED, tq->tq_pri);
1588*7c478bd9Sstevel@tonic-gate 
1589*7c478bd9Sstevel@tonic-gate 	/*
1590*7c478bd9Sstevel@tonic-gate 	 * Once the entry is ready, link it to the the bucket free list.
1591*7c478bd9Sstevel@tonic-gate 	 */
1592*7c478bd9Sstevel@tonic-gate 	mutex_enter(&b->tqbucket_lock);
1593*7c478bd9Sstevel@tonic-gate 	tqe->tqent_func = NULL;
1594*7c478bd9Sstevel@tonic-gate 	TQ_APPEND(b->tqbucket_freelist, tqe);
1595*7c478bd9Sstevel@tonic-gate 	b->tqbucket_nfree++;
1596*7c478bd9Sstevel@tonic-gate 	TQ_STAT(b, tqs_tcreates);
1597*7c478bd9Sstevel@tonic-gate 
1598*7c478bd9Sstevel@tonic-gate #if TASKQ_STATISTIC
1599*7c478bd9Sstevel@tonic-gate 	nthreads = b->tqbucket_stat.tqs_tcreates -
1600*7c478bd9Sstevel@tonic-gate 	    b->tqbucket_stat.tqs_tdeaths;
1601*7c478bd9Sstevel@tonic-gate 	b->tqbucket_stat.tqs_maxthreads = MAX(nthreads,
1602*7c478bd9Sstevel@tonic-gate 	    b->tqbucket_stat.tqs_maxthreads);
1603*7c478bd9Sstevel@tonic-gate #endif
1604*7c478bd9Sstevel@tonic-gate 
1605*7c478bd9Sstevel@tonic-gate 	mutex_exit(&b->tqbucket_lock);
1606*7c478bd9Sstevel@tonic-gate 	/*
1607*7c478bd9Sstevel@tonic-gate 	 * Start the stopped thread.
1608*7c478bd9Sstevel@tonic-gate 	 */
1609*7c478bd9Sstevel@tonic-gate 	thread_lock(tqe->tqent_thread);
1610*7c478bd9Sstevel@tonic-gate 	tqe->tqent_thread->t_taskq = tq;
1611*7c478bd9Sstevel@tonic-gate 	tqe->tqent_thread->t_schedflag |= TS_ALLSTART;
1612*7c478bd9Sstevel@tonic-gate 	setrun_locked(tqe->tqent_thread);
1613*7c478bd9Sstevel@tonic-gate 	thread_unlock(tqe->tqent_thread);
1614*7c478bd9Sstevel@tonic-gate }
1615*7c478bd9Sstevel@tonic-gate 
1616*7c478bd9Sstevel@tonic-gate static int
1617*7c478bd9Sstevel@tonic-gate taskq_kstat_update(kstat_t *ksp, int rw)
1618*7c478bd9Sstevel@tonic-gate {
1619*7c478bd9Sstevel@tonic-gate 	struct taskq_kstat *tqsp = &taskq_kstat;
1620*7c478bd9Sstevel@tonic-gate 	taskq_t *tq = ksp->ks_private;
1621*7c478bd9Sstevel@tonic-gate 
1622*7c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
1623*7c478bd9Sstevel@tonic-gate 		return (EACCES);
1624*7c478bd9Sstevel@tonic-gate 
1625*7c478bd9Sstevel@tonic-gate 	tqsp->tq_tasks.value.ui64 = tq->tq_tasks;
1626*7c478bd9Sstevel@tonic-gate 	tqsp->tq_executed.value.ui64 = tq->tq_executed;
1627*7c478bd9Sstevel@tonic-gate 	tqsp->tq_maxtasks.value.ui64 = tq->tq_maxtasks;
1628*7c478bd9Sstevel@tonic-gate 	tqsp->tq_totaltime.value.ui64 = tq->tq_totaltime;
1629*7c478bd9Sstevel@tonic-gate 	tqsp->tq_nactive.value.ui64 = tq->tq_active;
1630*7c478bd9Sstevel@tonic-gate 	tqsp->tq_nalloc.value.ui64 = tq->tq_nalloc;
1631*7c478bd9Sstevel@tonic-gate 	tqsp->tq_pri.value.ui64 = tq->tq_pri;
1632*7c478bd9Sstevel@tonic-gate 	tqsp->tq_nthreads.value.ui64 = tq->tq_nthreads;
1633*7c478bd9Sstevel@tonic-gate 	return (0);
1634*7c478bd9Sstevel@tonic-gate }
1635*7c478bd9Sstevel@tonic-gate 
1636*7c478bd9Sstevel@tonic-gate static int
1637*7c478bd9Sstevel@tonic-gate taskq_d_kstat_update(kstat_t *ksp, int rw)
1638*7c478bd9Sstevel@tonic-gate {
1639*7c478bd9Sstevel@tonic-gate 	struct taskq_d_kstat *tqsp = &taskq_d_kstat;
1640*7c478bd9Sstevel@tonic-gate 	taskq_t *tq = ksp->ks_private;
1641*7c478bd9Sstevel@tonic-gate 	taskq_bucket_t *b = tq->tq_buckets;
1642*7c478bd9Sstevel@tonic-gate 	int bid = 0;
1643*7c478bd9Sstevel@tonic-gate 
1644*7c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
1645*7c478bd9Sstevel@tonic-gate 		return (EACCES);
1646*7c478bd9Sstevel@tonic-gate 
1647*7c478bd9Sstevel@tonic-gate 	ASSERT(tq->tq_flags & TASKQ_DYNAMIC);
1648*7c478bd9Sstevel@tonic-gate 
1649*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_btasks.value.ui64 = tq->tq_tasks;
1650*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_bexecuted.value.ui64 = tq->tq_executed;
1651*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_bmaxtasks.value.ui64 = tq->tq_maxtasks;
1652*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_bnalloc.value.ui64 = tq->tq_nalloc;
1653*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_bnactive.value.ui64 = tq->tq_active;
1654*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_btotaltime.value.ui64 = tq->tq_totaltime;
1655*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_pri.value.ui64 = tq->tq_pri;
1656*7c478bd9Sstevel@tonic-gate 
1657*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_hits.value.ui64 = 0;
1658*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_misses.value.ui64 = 0;
1659*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_overflows.value.ui64 = 0;
1660*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_tcreates.value.ui64 = 0;
1661*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_tdeaths.value.ui64 = 0;
1662*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_maxthreads.value.ui64 = 0;
1663*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_nomem.value.ui64 = 0;
1664*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_disptcreates.value.ui64 = 0;
1665*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_totaltime.value.ui64 = 0;
1666*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_nalloc.value.ui64 = 0;
1667*7c478bd9Sstevel@tonic-gate 	tqsp->tqd_nfree.value.ui64 = 0;
1668*7c478bd9Sstevel@tonic-gate 
1669*7c478bd9Sstevel@tonic-gate 	for (; (b != NULL) && (bid < tq->tq_nbuckets); b++, bid++) {
1670*7c478bd9Sstevel@tonic-gate 		tqsp->tqd_hits.value.ui64 += b->tqbucket_stat.tqs_hits;
1671*7c478bd9Sstevel@tonic-gate 		tqsp->tqd_misses.value.ui64 += b->tqbucket_stat.tqs_misses;
1672*7c478bd9Sstevel@tonic-gate 		tqsp->tqd_overflows.value.ui64 += b->tqbucket_stat.tqs_overflow;
1673*7c478bd9Sstevel@tonic-gate 		tqsp->tqd_tcreates.value.ui64 += b->tqbucket_stat.tqs_tcreates;
1674*7c478bd9Sstevel@tonic-gate 		tqsp->tqd_tdeaths.value.ui64 += b->tqbucket_stat.tqs_tdeaths;
1675*7c478bd9Sstevel@tonic-gate 		tqsp->tqd_maxthreads.value.ui64 +=
1676*7c478bd9Sstevel@tonic-gate 		    b->tqbucket_stat.tqs_maxthreads;
1677*7c478bd9Sstevel@tonic-gate 		tqsp->tqd_nomem.value.ui64 += b->tqbucket_stat.tqs_nomem;
1678*7c478bd9Sstevel@tonic-gate 		tqsp->tqd_disptcreates.value.ui64 +=
1679*7c478bd9Sstevel@tonic-gate 		    b->tqbucket_stat.tqs_disptcreates;
1680*7c478bd9Sstevel@tonic-gate 		tqsp->tqd_totaltime.value.ui64 += b->tqbucket_totaltime;
1681*7c478bd9Sstevel@tonic-gate 		tqsp->tqd_nalloc.value.ui64 += b->tqbucket_nalloc;
1682*7c478bd9Sstevel@tonic-gate 		tqsp->tqd_nfree.value.ui64 += b->tqbucket_nfree;
1683*7c478bd9Sstevel@tonic-gate 	}
1684*7c478bd9Sstevel@tonic-gate 	return (0);
1685*7c478bd9Sstevel@tonic-gate }
1686