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 2004 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 protection serializers: general purpose synchronization mechanism.
31*7c478bd9Sstevel@tonic-gate *
32*7c478bd9Sstevel@tonic-gate * Serializers provide a simple way to serialize access to some resource. They
33*7c478bd9Sstevel@tonic-gate * can be used as an alternative to locks or STREAMS perimeters. They scale
34*7c478bd9Sstevel@tonic-gate * much better than STREAMS outer serializers.
35*7c478bd9Sstevel@tonic-gate *
36*7c478bd9Sstevel@tonic-gate * Serializer is an abstraction that guarantees that all functions executed
37*7c478bd9Sstevel@tonic-gate * within the serializer are serialized: they are executed in the order they
38*7c478bd9Sstevel@tonic-gate * entered serializer one at a time.
39*7c478bd9Sstevel@tonic-gate *
40*7c478bd9Sstevel@tonic-gate * INTERFACES:
41*7c478bd9Sstevel@tonic-gate *
42*7c478bd9Sstevel@tonic-gate * serializer_t *serializer_create(flags);
43*7c478bd9Sstevel@tonic-gate *
44*7c478bd9Sstevel@tonic-gate * Create a serializer. The flags may be either SER_SLEEP or SER_NOSLEEP
45*7c478bd9Sstevel@tonic-gate * which are the same as KM_SLEEP and KM_NOSLEEP respectively.
46*7c478bd9Sstevel@tonic-gate *
47*7c478bd9Sstevel@tonic-gate * serializer_enter(serializer, proc, mblk, arg);
48*7c478bd9Sstevel@tonic-gate *
49*7c478bd9Sstevel@tonic-gate * Execute 'proc(mblk, arg)' within the serializer.
50*7c478bd9Sstevel@tonic-gate *
51*7c478bd9Sstevel@tonic-gate * serializer_wait(serializer);
52*7c478bd9Sstevel@tonic-gate *
53*7c478bd9Sstevel@tonic-gate * Wait for pending serializer jobs to complete. This function should never
54*7c478bd9Sstevel@tonic-gate * be called within the serializer or it will deadlock.
55*7c478bd9Sstevel@tonic-gate *
56*7c478bd9Sstevel@tonic-gate * serializer_destroy(serializer);
57*7c478bd9Sstevel@tonic-gate *
58*7c478bd9Sstevel@tonic-gate * Destroy serializer.
59*7c478bd9Sstevel@tonic-gate *
60*7c478bd9Sstevel@tonic-gate * Serializers export three DTrace SDT probes:
61*7c478bd9Sstevel@tonic-gate *
62*7c478bd9Sstevel@tonic-gate * serializer-enqueue(serializer, mblk, arg, proc)
63*7c478bd9Sstevel@tonic-gate *
64*7c478bd9Sstevel@tonic-gate * The probe triggers when serializer is busy and the request is
65*7c478bd9Sstevel@tonic-gate * queued.
66*7c478bd9Sstevel@tonic-gate *
67*7c478bd9Sstevel@tonic-gate * serializer-exec-start(serializer, mblk, arg, proc)
68*7c478bd9Sstevel@tonic-gate *
69*7c478bd9Sstevel@tonic-gate * The probe triggers before the request is executed
70*7c478bd9Sstevel@tonic-gate *
71*7c478bd9Sstevel@tonic-gate * serializer-exec-end(serializer, mblk, arg, proc)
72*7c478bd9Sstevel@tonic-gate *
73*7c478bd9Sstevel@tonic-gate * The probe triggers after the request is executed
74*7c478bd9Sstevel@tonic-gate *
75*7c478bd9Sstevel@tonic-gate *
76*7c478bd9Sstevel@tonic-gate * IMPLEMENTATION.
77*7c478bd9Sstevel@tonic-gate *
78*7c478bd9Sstevel@tonic-gate * Serializer consists of a "owner" and a list of queued jobs. The first thread
79*7c478bd9Sstevel@tonic-gate * entering serializer sets the owner and executes its job directly without
80*7c478bd9Sstevel@tonic-gate * context switch. Then it processes jobs which may have been enqueued while it
81*7c478bd9Sstevel@tonic-gate * was executing a job and drops the owner, leaving the serializer empty. Any
82*7c478bd9Sstevel@tonic-gate * thread entering an owned serializer enqueues its job and returns immediately.
83*7c478bd9Sstevel@tonic-gate *
84*7c478bd9Sstevel@tonic-gate * Serializer data structure holds several fields used for debugging only. They
85*7c478bd9Sstevel@tonic-gate * are not relevant for the proper serializer functioning.
86*7c478bd9Sstevel@tonic-gate *
87*7c478bd9Sstevel@tonic-gate * When new requests arrive faster then they are processed it is possible that a
88*7c478bd9Sstevel@tonic-gate * thread that started processing serializer will continue doing so for a long
89*7c478bd9Sstevel@tonic-gate * time. To avoid such pathological behavior the amount of requests drained by
90*7c478bd9Sstevel@tonic-gate * serializer_enter() is limited by `serializer_credit' value. After the credit
91*7c478bd9Sstevel@tonic-gate * is expired serializer_enter() schedules a taskq request to continue draining.
92*7c478bd9Sstevel@tonic-gate * The taskq thread draining is not limited by serializer_credit. Note that it
93*7c478bd9Sstevel@tonic-gate * is possible that another serializer_enter() will drain the serializer before
94*7c478bd9Sstevel@tonic-gate * a taskq thread will get to it.
95*7c478bd9Sstevel@tonic-gate */
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
98*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
99*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
100*7c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
101*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
102*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
103*7c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
104*7c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
105*7c478bd9Sstevel@tonic-gate #include <sys/serializer.h>
106*7c478bd9Sstevel@tonic-gate
107*7c478bd9Sstevel@tonic-gate #define SERIALIZER_NAMELEN 31
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate /*
110*7c478bd9Sstevel@tonic-gate * Serializer abstraction.
111*7c478bd9Sstevel@tonic-gate * Fields marked (D) are used for debugging purposes only.
112*7c478bd9Sstevel@tonic-gate */
113*7c478bd9Sstevel@tonic-gate struct serializer_s {
114*7c478bd9Sstevel@tonic-gate kmutex_t ser_lock; /* Protects state and the list */
115*7c478bd9Sstevel@tonic-gate kthread_t *ser_owner; /* Thread executing serializer */
116*7c478bd9Sstevel@tonic-gate ushort_t ser_taskq; /* Serializer scheduled for taskq */
117*7c478bd9Sstevel@tonic-gate kcondvar_t ser_cv; /* For serializer-wait */
118*7c478bd9Sstevel@tonic-gate uint_t ser_count; /* # of queued requests (D) */
119*7c478bd9Sstevel@tonic-gate mblk_t *ser_first; /* First message in the queue */
120*7c478bd9Sstevel@tonic-gate mblk_t *ser_last; /* Last message in the queue */
121*7c478bd9Sstevel@tonic-gate srproc_t *ser_proc; /* Currently executing proc (D) */
122*7c478bd9Sstevel@tonic-gate mblk_t *ser_curr; /* Currently executing msg (D) */
123*7c478bd9Sstevel@tonic-gate void *ser_arg; /* Currently executing arg (D) */
124*7c478bd9Sstevel@tonic-gate };
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate static kmem_cache_t *serializer_cache;
127*7c478bd9Sstevel@tonic-gate
128*7c478bd9Sstevel@tonic-gate /*
129*7c478bd9Sstevel@tonic-gate * How many drains are allowed before we switch to taskq processing.
130*7c478bd9Sstevel@tonic-gate */
131*7c478bd9Sstevel@tonic-gate #define SERIALIZER_CREDIT 10
132*7c478bd9Sstevel@tonic-gate static int serializer_credit = SERIALIZER_CREDIT;
133*7c478bd9Sstevel@tonic-gate
134*7c478bd9Sstevel@tonic-gate /* Statistics for debugging */
135*7c478bd9Sstevel@tonic-gate static int perim_context_swtch = 0;
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate static int serializer_constructor(void *, void *, int);
138*7c478bd9Sstevel@tonic-gate static void serializer_destructor(void *, void *);
139*7c478bd9Sstevel@tonic-gate static void serializer_exec(serializer_t *, srproc_t, mblk_t *, void *);
140*7c478bd9Sstevel@tonic-gate static void serializer_enqueue(serializer_t *, srproc_t, mblk_t *, void *);
141*7c478bd9Sstevel@tonic-gate static void serializer_drain(serializer_t *, int);
142*7c478bd9Sstevel@tonic-gate static void serializer_drain_completely(serializer_t *);
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate /*
145*7c478bd9Sstevel@tonic-gate * SERIALIZER Implementation.
146*7c478bd9Sstevel@tonic-gate */
147*7c478bd9Sstevel@tonic-gate
148*7c478bd9Sstevel@tonic-gate /*
149*7c478bd9Sstevel@tonic-gate * Record debugging information and execute single request.
150*7c478bd9Sstevel@tonic-gate */
151*7c478bd9Sstevel@tonic-gate static void
serializer_exec(serializer_t * s,srproc_t proc,mblk_t * mp,void * arg)152*7c478bd9Sstevel@tonic-gate serializer_exec(serializer_t *s, srproc_t proc, mblk_t *mp, void *arg)
153*7c478bd9Sstevel@tonic-gate {
154*7c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&s->ser_lock));
155*7c478bd9Sstevel@tonic-gate ASSERT(s->ser_owner == curthread);
156*7c478bd9Sstevel@tonic-gate
157*7c478bd9Sstevel@tonic-gate ASSERT(proc != NULL);
158*7c478bd9Sstevel@tonic-gate ASSERT(mp != NULL);
159*7c478bd9Sstevel@tonic-gate
160*7c478bd9Sstevel@tonic-gate s->ser_curr = mp;
161*7c478bd9Sstevel@tonic-gate s->ser_arg = arg;
162*7c478bd9Sstevel@tonic-gate s->ser_proc = proc;
163*7c478bd9Sstevel@tonic-gate proc(mp, arg);
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate
166*7c478bd9Sstevel@tonic-gate /*
167*7c478bd9Sstevel@tonic-gate * Enqueue a single request on serializer.
168*7c478bd9Sstevel@tonic-gate */
169*7c478bd9Sstevel@tonic-gate static void
serializer_enqueue(serializer_t * s,srproc_t proc,mblk_t * mp,void * arg)170*7c478bd9Sstevel@tonic-gate serializer_enqueue(serializer_t *s, srproc_t proc, mblk_t *mp, void *arg)
171*7c478bd9Sstevel@tonic-gate {
172*7c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&s->ser_lock));
173*7c478bd9Sstevel@tonic-gate
174*7c478bd9Sstevel@tonic-gate DTRACE_PROBE4(serializer__enqueue, serializer_t *, s,
175*7c478bd9Sstevel@tonic-gate mblk_t *, mp, void *, arg, srproc_t, proc);
176*7c478bd9Sstevel@tonic-gate s->ser_count++;
177*7c478bd9Sstevel@tonic-gate mp->b_queue = (queue_t *)proc;
178*7c478bd9Sstevel@tonic-gate mp->b_prev = (mblk_t *)arg;
179*7c478bd9Sstevel@tonic-gate if (s->ser_last != NULL)
180*7c478bd9Sstevel@tonic-gate s->ser_last->b_next = mp;
181*7c478bd9Sstevel@tonic-gate else
182*7c478bd9Sstevel@tonic-gate s->ser_first = mp;
183*7c478bd9Sstevel@tonic-gate s->ser_last = mp;
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate
186*7c478bd9Sstevel@tonic-gate /*
187*7c478bd9Sstevel@tonic-gate * Drain serializer, limiting drain to `credit' requests at most.
188*7c478bd9Sstevel@tonic-gate */
189*7c478bd9Sstevel@tonic-gate static void
serializer_drain(serializer_t * s,int credit)190*7c478bd9Sstevel@tonic-gate serializer_drain(serializer_t *s, int credit)
191*7c478bd9Sstevel@tonic-gate {
192*7c478bd9Sstevel@tonic-gate mblk_t *mp = s->ser_first;
193*7c478bd9Sstevel@tonic-gate
194*7c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&s->ser_lock));
195*7c478bd9Sstevel@tonic-gate ASSERT(s->ser_owner == curthread);
196*7c478bd9Sstevel@tonic-gate
197*7c478bd9Sstevel@tonic-gate for (; mp != NULL && credit-- != 0; mp = s->ser_first) {
198*7c478bd9Sstevel@tonic-gate srproc_t *proc = (srproc_t *)mp->b_queue;
199*7c478bd9Sstevel@tonic-gate void *arg = mp->b_prev;
200*7c478bd9Sstevel@tonic-gate
201*7c478bd9Sstevel@tonic-gate if ((s->ser_first = s->ser_first->b_next) == NULL) {
202*7c478bd9Sstevel@tonic-gate s->ser_last = NULL;
203*7c478bd9Sstevel@tonic-gate } else {
204*7c478bd9Sstevel@tonic-gate mp->b_next = NULL;
205*7c478bd9Sstevel@tonic-gate }
206*7c478bd9Sstevel@tonic-gate ASSERT(s->ser_count != 0);
207*7c478bd9Sstevel@tonic-gate s->ser_count--;
208*7c478bd9Sstevel@tonic-gate mp->b_queue = NULL;
209*7c478bd9Sstevel@tonic-gate mp->b_prev = NULL;
210*7c478bd9Sstevel@tonic-gate mutex_exit(&s->ser_lock);
211*7c478bd9Sstevel@tonic-gate
212*7c478bd9Sstevel@tonic-gate DTRACE_PROBE4(serializer__exec__start, serializer_t *, s,
213*7c478bd9Sstevel@tonic-gate mblk_t *, mp, void *, arg, srproc_t, proc);
214*7c478bd9Sstevel@tonic-gate serializer_exec(s, proc, mp, arg);
215*7c478bd9Sstevel@tonic-gate DTRACE_PROBE4(serializer__exec__end, serializer_t *, s,
216*7c478bd9Sstevel@tonic-gate mblk_t *, mp, void *, arg, srproc_t, proc);
217*7c478bd9Sstevel@tonic-gate
218*7c478bd9Sstevel@tonic-gate mutex_enter(&s->ser_lock);
219*7c478bd9Sstevel@tonic-gate }
220*7c478bd9Sstevel@tonic-gate }
221*7c478bd9Sstevel@tonic-gate
222*7c478bd9Sstevel@tonic-gate /*
223*7c478bd9Sstevel@tonic-gate * Drain serializer completely if serializer is free.
224*7c478bd9Sstevel@tonic-gate */
225*7c478bd9Sstevel@tonic-gate static void
serializer_drain_completely(serializer_t * s)226*7c478bd9Sstevel@tonic-gate serializer_drain_completely(serializer_t *s)
227*7c478bd9Sstevel@tonic-gate {
228*7c478bd9Sstevel@tonic-gate mutex_enter(&s->ser_lock);
229*7c478bd9Sstevel@tonic-gate ASSERT(s->ser_taskq);
230*7c478bd9Sstevel@tonic-gate if (s->ser_owner == NULL) {
231*7c478bd9Sstevel@tonic-gate s->ser_owner = curthread;
232*7c478bd9Sstevel@tonic-gate while (s->ser_first != NULL)
233*7c478bd9Sstevel@tonic-gate serializer_drain(s, INT_MAX);
234*7c478bd9Sstevel@tonic-gate s->ser_owner = NULL;
235*7c478bd9Sstevel@tonic-gate s->ser_curr = NULL;
236*7c478bd9Sstevel@tonic-gate s->ser_proc = NULL;
237*7c478bd9Sstevel@tonic-gate s->ser_arg = NULL;
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate s->ser_taskq = B_FALSE;
240*7c478bd9Sstevel@tonic-gate /*
241*7c478bd9Sstevel@tonic-gate * Wake up serializer_wait().
242*7c478bd9Sstevel@tonic-gate */
243*7c478bd9Sstevel@tonic-gate cv_signal(&s->ser_cv);
244*7c478bd9Sstevel@tonic-gate mutex_exit(&s->ser_lock);
245*7c478bd9Sstevel@tonic-gate }
246*7c478bd9Sstevel@tonic-gate
247*7c478bd9Sstevel@tonic-gate /*
248*7c478bd9Sstevel@tonic-gate * Call proc(mp, arg) within serializer.
249*7c478bd9Sstevel@tonic-gate *
250*7c478bd9Sstevel@tonic-gate * If serializer is empty and not owned, proc(mp, arg) is called right
251*7c478bd9Sstevel@tonic-gate * away. Otherwise the request is queued.
252*7c478bd9Sstevel@tonic-gate */
253*7c478bd9Sstevel@tonic-gate void
serializer_enter(serializer_t * s,srproc_t proc,mblk_t * mp,void * arg)254*7c478bd9Sstevel@tonic-gate serializer_enter(serializer_t *s, srproc_t proc, mblk_t *mp, void *arg)
255*7c478bd9Sstevel@tonic-gate {
256*7c478bd9Sstevel@tonic-gate ASSERT(proc != NULL);
257*7c478bd9Sstevel@tonic-gate ASSERT(mp != NULL);
258*7c478bd9Sstevel@tonic-gate ASSERT(mp->b_next == NULL);
259*7c478bd9Sstevel@tonic-gate ASSERT(mp->b_prev == NULL);
260*7c478bd9Sstevel@tonic-gate
261*7c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&s->ser_lock));
262*7c478bd9Sstevel@tonic-gate
263*7c478bd9Sstevel@tonic-gate mutex_enter(&s->ser_lock);
264*7c478bd9Sstevel@tonic-gate if (s->ser_owner != NULL) {
265*7c478bd9Sstevel@tonic-gate /*
266*7c478bd9Sstevel@tonic-gate * Serializer is owned. Enqueue and return.
267*7c478bd9Sstevel@tonic-gate */
268*7c478bd9Sstevel@tonic-gate serializer_enqueue(s, proc, mp, arg);
269*7c478bd9Sstevel@tonic-gate } else {
270*7c478bd9Sstevel@tonic-gate taskqid_t tid = 0;
271*7c478bd9Sstevel@tonic-gate
272*7c478bd9Sstevel@tonic-gate /*
273*7c478bd9Sstevel@tonic-gate * If the request list is empty, can process right away,
274*7c478bd9Sstevel@tonic-gate * otherwise enqueue and process.
275*7c478bd9Sstevel@tonic-gate */
276*7c478bd9Sstevel@tonic-gate s->ser_owner = curthread;
277*7c478bd9Sstevel@tonic-gate
278*7c478bd9Sstevel@tonic-gate if (s->ser_first != NULL) {
279*7c478bd9Sstevel@tonic-gate ASSERT(s->ser_count != 0);
280*7c478bd9Sstevel@tonic-gate serializer_enqueue(s, proc, mp, arg);
281*7c478bd9Sstevel@tonic-gate } else {
282*7c478bd9Sstevel@tonic-gate ASSERT(s->ser_count == 0);
283*7c478bd9Sstevel@tonic-gate mutex_exit(&s->ser_lock);
284*7c478bd9Sstevel@tonic-gate /*
285*7c478bd9Sstevel@tonic-gate * Execute request
286*7c478bd9Sstevel@tonic-gate */
287*7c478bd9Sstevel@tonic-gate DTRACE_PROBE4(serializer__exec__start,
288*7c478bd9Sstevel@tonic-gate serializer_t *, s, mblk_t *, mp,
289*7c478bd9Sstevel@tonic-gate void *, arg, srproc_t, proc);
290*7c478bd9Sstevel@tonic-gate serializer_exec(s, proc, mp, arg);
291*7c478bd9Sstevel@tonic-gate DTRACE_PROBE4(serializer__exec__end,
292*7c478bd9Sstevel@tonic-gate serializer_t *, s, mblk_t *, mp,
293*7c478bd9Sstevel@tonic-gate void *, arg, srproc_t, proc);
294*7c478bd9Sstevel@tonic-gate mutex_enter(&s->ser_lock);
295*7c478bd9Sstevel@tonic-gate }
296*7c478bd9Sstevel@tonic-gate
297*7c478bd9Sstevel@tonic-gate /*
298*7c478bd9Sstevel@tonic-gate * Drain whatever has arrived in the meantime.
299*7c478bd9Sstevel@tonic-gate * If we spend too much time draining, continue draining by the
300*7c478bd9Sstevel@tonic-gate * taskq thread.
301*7c478bd9Sstevel@tonic-gate */
302*7c478bd9Sstevel@tonic-gate while ((s->ser_first != NULL) && (tid == 0)) {
303*7c478bd9Sstevel@tonic-gate serializer_drain(s, serializer_credit);
304*7c478bd9Sstevel@tonic-gate if (s->ser_first != NULL) {
305*7c478bd9Sstevel@tonic-gate perim_context_swtch++;
306*7c478bd9Sstevel@tonic-gate /*
307*7c478bd9Sstevel@tonic-gate * If there is a taskq pending for this
308*7c478bd9Sstevel@tonic-gate * serializer, no need to schedule a new one.
309*7c478bd9Sstevel@tonic-gate */
310*7c478bd9Sstevel@tonic-gate if (s->ser_taskq) {
311*7c478bd9Sstevel@tonic-gate break;
312*7c478bd9Sstevel@tonic-gate } else {
313*7c478bd9Sstevel@tonic-gate tid = taskq_dispatch(system_taskq,
314*7c478bd9Sstevel@tonic-gate (task_func_t *)
315*7c478bd9Sstevel@tonic-gate serializer_drain_completely,
316*7c478bd9Sstevel@tonic-gate s, TQ_NOSLEEP | TQ_NOQUEUE);
317*7c478bd9Sstevel@tonic-gate if (tid != 0)
318*7c478bd9Sstevel@tonic-gate s->ser_taskq = B_TRUE;
319*7c478bd9Sstevel@tonic-gate }
320*7c478bd9Sstevel@tonic-gate }
321*7c478bd9Sstevel@tonic-gate }
322*7c478bd9Sstevel@tonic-gate s->ser_owner = NULL;
323*7c478bd9Sstevel@tonic-gate s->ser_curr = NULL;
324*7c478bd9Sstevel@tonic-gate s->ser_proc = NULL;
325*7c478bd9Sstevel@tonic-gate s->ser_arg = NULL;
326*7c478bd9Sstevel@tonic-gate }
327*7c478bd9Sstevel@tonic-gate /*
328*7c478bd9Sstevel@tonic-gate * Wakeup serializer_wait().
329*7c478bd9Sstevel@tonic-gate */
330*7c478bd9Sstevel@tonic-gate cv_signal(&s->ser_cv);
331*7c478bd9Sstevel@tonic-gate mutex_exit(&s->ser_lock);
332*7c478bd9Sstevel@tonic-gate }
333*7c478bd9Sstevel@tonic-gate
334*7c478bd9Sstevel@tonic-gate /*
335*7c478bd9Sstevel@tonic-gate * Wait for pending serializer jobs to complete. This function should never be
336*7c478bd9Sstevel@tonic-gate * called within the serializer or it will deadlock.
337*7c478bd9Sstevel@tonic-gate */
338*7c478bd9Sstevel@tonic-gate void
serializer_wait(serializer_t * s)339*7c478bd9Sstevel@tonic-gate serializer_wait(serializer_t *s)
340*7c478bd9Sstevel@tonic-gate {
341*7c478bd9Sstevel@tonic-gate mutex_enter(&s->ser_lock);
342*7c478bd9Sstevel@tonic-gate
343*7c478bd9Sstevel@tonic-gate ASSERT(s->ser_owner != curthread);
344*7c478bd9Sstevel@tonic-gate
345*7c478bd9Sstevel@tonic-gate while ((s->ser_owner != NULL) || s->ser_taskq || (s->ser_first != NULL))
346*7c478bd9Sstevel@tonic-gate cv_wait(&s->ser_cv, &s->ser_lock);
347*7c478bd9Sstevel@tonic-gate ASSERT((s->ser_first == NULL) && (s->ser_last == NULL));
348*7c478bd9Sstevel@tonic-gate /*
349*7c478bd9Sstevel@tonic-gate * Wakeup other potential waiters.
350*7c478bd9Sstevel@tonic-gate */
351*7c478bd9Sstevel@tonic-gate cv_signal(&s->ser_cv);
352*7c478bd9Sstevel@tonic-gate mutex_exit(&s->ser_lock);
353*7c478bd9Sstevel@tonic-gate }
354*7c478bd9Sstevel@tonic-gate
355*7c478bd9Sstevel@tonic-gate /*
356*7c478bd9Sstevel@tonic-gate * Create a new serializer.
357*7c478bd9Sstevel@tonic-gate */
358*7c478bd9Sstevel@tonic-gate serializer_t *
serializer_create(int flags)359*7c478bd9Sstevel@tonic-gate serializer_create(int flags)
360*7c478bd9Sstevel@tonic-gate {
361*7c478bd9Sstevel@tonic-gate return (kmem_cache_alloc(serializer_cache, flags));
362*7c478bd9Sstevel@tonic-gate }
363*7c478bd9Sstevel@tonic-gate
364*7c478bd9Sstevel@tonic-gate /*
365*7c478bd9Sstevel@tonic-gate * Wait for all pending entries to drain and then destroy serializer.
366*7c478bd9Sstevel@tonic-gate */
367*7c478bd9Sstevel@tonic-gate void
serializer_destroy(serializer_t * s)368*7c478bd9Sstevel@tonic-gate serializer_destroy(serializer_t *s)
369*7c478bd9Sstevel@tonic-gate {
370*7c478bd9Sstevel@tonic-gate serializer_wait(s);
371*7c478bd9Sstevel@tonic-gate
372*7c478bd9Sstevel@tonic-gate ASSERT(s->ser_owner == NULL);
373*7c478bd9Sstevel@tonic-gate ASSERT(s->ser_taskq == 0);
374*7c478bd9Sstevel@tonic-gate ASSERT(s->ser_count == 0);
375*7c478bd9Sstevel@tonic-gate ASSERT(s->ser_first == NULL);
376*7c478bd9Sstevel@tonic-gate ASSERT(s->ser_last == NULL);
377*7c478bd9Sstevel@tonic-gate
378*7c478bd9Sstevel@tonic-gate kmem_cache_free(serializer_cache, s);
379*7c478bd9Sstevel@tonic-gate }
380*7c478bd9Sstevel@tonic-gate
381*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
382*7c478bd9Sstevel@tonic-gate static int
serializer_constructor(void * buf,void * cdrarg,int kmflags)383*7c478bd9Sstevel@tonic-gate serializer_constructor(void *buf, void *cdrarg, int kmflags)
384*7c478bd9Sstevel@tonic-gate {
385*7c478bd9Sstevel@tonic-gate serializer_t *s = buf;
386*7c478bd9Sstevel@tonic-gate
387*7c478bd9Sstevel@tonic-gate mutex_init(&s->ser_lock, NULL, MUTEX_DEFAULT, NULL);
388*7c478bd9Sstevel@tonic-gate cv_init(&s->ser_cv, NULL, CV_DEFAULT, NULL);
389*7c478bd9Sstevel@tonic-gate
390*7c478bd9Sstevel@tonic-gate s->ser_taskq = 0;
391*7c478bd9Sstevel@tonic-gate s->ser_count = 0;
392*7c478bd9Sstevel@tonic-gate s->ser_first = s->ser_last = s->ser_curr = NULL;
393*7c478bd9Sstevel@tonic-gate s->ser_proc = NULL;
394*7c478bd9Sstevel@tonic-gate s->ser_arg = NULL;
395*7c478bd9Sstevel@tonic-gate s->ser_owner = NULL;
396*7c478bd9Sstevel@tonic-gate return (0);
397*7c478bd9Sstevel@tonic-gate }
398*7c478bd9Sstevel@tonic-gate
399*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
400*7c478bd9Sstevel@tonic-gate static void
serializer_destructor(void * buf,void * cdrarg)401*7c478bd9Sstevel@tonic-gate serializer_destructor(void *buf, void *cdrarg)
402*7c478bd9Sstevel@tonic-gate {
403*7c478bd9Sstevel@tonic-gate serializer_t *s = buf;
404*7c478bd9Sstevel@tonic-gate
405*7c478bd9Sstevel@tonic-gate mutex_destroy(&s->ser_lock);
406*7c478bd9Sstevel@tonic-gate cv_destroy(&s->ser_cv);
407*7c478bd9Sstevel@tonic-gate }
408*7c478bd9Sstevel@tonic-gate
409*7c478bd9Sstevel@tonic-gate void
serializer_init(void)410*7c478bd9Sstevel@tonic-gate serializer_init(void)
411*7c478bd9Sstevel@tonic-gate {
412*7c478bd9Sstevel@tonic-gate serializer_cache = kmem_cache_create("serializer_cache",
413*7c478bd9Sstevel@tonic-gate sizeof (serializer_t), 0, serializer_constructor,
414*7c478bd9Sstevel@tonic-gate serializer_destructor, NULL, NULL, NULL, 0);
415*7c478bd9Sstevel@tonic-gate }
416