xref: /illumos-gate/usr/src/uts/common/os/serializer.c (revision 2d6eb4a5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Kernel protection serializers: general purpose synchronization mechanism.
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * Serializers provide a simple way to serialize access to some resource. They
317c478bd9Sstevel@tonic-gate  * can be used as an alternative to locks or STREAMS perimeters. They scale
327c478bd9Sstevel@tonic-gate  * much better than STREAMS outer serializers.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * Serializer is an abstraction that guarantees that all functions executed
357c478bd9Sstevel@tonic-gate  * within the serializer are serialized: they are executed in the order they
367c478bd9Sstevel@tonic-gate  * entered serializer one at a time.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * INTERFACES:
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * serializer_t *serializer_create(flags);
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  *	Create a serializer. The flags may be either SER_SLEEP or SER_NOSLEEP
437c478bd9Sstevel@tonic-gate  *	which are the same as KM_SLEEP and KM_NOSLEEP respectively.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * serializer_enter(serializer, proc, mblk, arg);
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  *	Execute 'proc(mblk, arg)' within the serializer.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * serializer_wait(serializer);
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *	Wait for pending serializer jobs to complete. This function should never
527c478bd9Sstevel@tonic-gate  *	be called within the serializer or it will deadlock.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * serializer_destroy(serializer);
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  *	Destroy serializer.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * Serializers export three DTrace SDT probes:
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  *	serializer-enqueue(serializer, mblk, arg, proc)
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  *		The probe triggers when serializer is busy and the request is
637c478bd9Sstevel@tonic-gate  *		queued.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  *	serializer-exec-start(serializer, mblk, arg, proc)
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  *		The probe triggers before the request is executed
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  *	serializer-exec-end(serializer, mblk, arg, proc)
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  *		The probe triggers after the request is executed
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * IMPLEMENTATION.
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * Serializer consists of a "owner" and a list of queued jobs. The first thread
777c478bd9Sstevel@tonic-gate  * entering serializer sets the owner and executes its job directly without
787c478bd9Sstevel@tonic-gate  * context switch. Then it processes jobs which may have been enqueued while it
797c478bd9Sstevel@tonic-gate  * was executing a job and drops the owner, leaving the serializer empty.  Any
807c478bd9Sstevel@tonic-gate  * thread entering an owned serializer enqueues its job and returns immediately.
817c478bd9Sstevel@tonic-gate  *
827c478bd9Sstevel@tonic-gate  * Serializer data structure holds several fields used for debugging only. They
837c478bd9Sstevel@tonic-gate  * are not relevant for the proper serializer functioning.
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  * When new requests arrive faster then they are processed it is possible that a
867c478bd9Sstevel@tonic-gate  * thread that started processing serializer will continue doing so for a long
877c478bd9Sstevel@tonic-gate  * time. To avoid such pathological behavior the amount of requests drained by
887c478bd9Sstevel@tonic-gate  * serializer_enter() is limited by `serializer_credit' value. After the credit
897c478bd9Sstevel@tonic-gate  * is expired serializer_enter() schedules a taskq request to continue draining.
907c478bd9Sstevel@tonic-gate  * The taskq thread draining is not limited by serializer_credit. Note that it
917c478bd9Sstevel@tonic-gate  * is possible that another serializer_enter() will drain the serializer before
927c478bd9Sstevel@tonic-gate  * a taskq thread will get to it.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #include <sys/types.h>
967c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
977c478bd9Sstevel@tonic-gate #include <sys/thread.h>
987c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
997c478bd9Sstevel@tonic-gate #include <sys/systm.h>
1007c478bd9Sstevel@tonic-gate #include <sys/debug.h>
1017c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
1027c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
1037c478bd9Sstevel@tonic-gate #include <sys/serializer.h>
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #define	SERIALIZER_NAMELEN 31
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Serializer abstraction.
1097c478bd9Sstevel@tonic-gate  * Fields marked (D) are used for debugging purposes only.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate struct serializer_s {
1127c478bd9Sstevel@tonic-gate 	kmutex_t	ser_lock;	/* Protects state and the list */
1137c478bd9Sstevel@tonic-gate 	kthread_t	*ser_owner;	/* Thread executing serializer */
1147c478bd9Sstevel@tonic-gate 	ushort_t	ser_taskq;	/* Serializer scheduled for taskq */
1157c478bd9Sstevel@tonic-gate 	kcondvar_t	ser_cv;		/* For serializer-wait */
1167c478bd9Sstevel@tonic-gate 	uint_t		ser_count;	/* # of queued requests (D) */
1177c478bd9Sstevel@tonic-gate 	mblk_t		*ser_first;	/* First message in the queue */
1187c478bd9Sstevel@tonic-gate 	mblk_t		*ser_last;	/* Last message in the queue */
1197c478bd9Sstevel@tonic-gate 	srproc_t	*ser_proc;	/* Currently executing proc (D) */
1207c478bd9Sstevel@tonic-gate 	mblk_t		*ser_curr;	/* Currently executing msg (D) */
1217c478bd9Sstevel@tonic-gate 	void		*ser_arg;	/* Currently executing arg (D) */
1227c478bd9Sstevel@tonic-gate };
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static kmem_cache_t *serializer_cache;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * How many drains are allowed before we switch to taskq processing.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate #define	SERIALIZER_CREDIT 10
1307c478bd9Sstevel@tonic-gate static int serializer_credit = SERIALIZER_CREDIT;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /* Statistics for debugging */
1337c478bd9Sstevel@tonic-gate static int perim_context_swtch = 0;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static int serializer_constructor(void *, void *, int);
1367c478bd9Sstevel@tonic-gate static void serializer_destructor(void *, void *);
1377c478bd9Sstevel@tonic-gate static void serializer_exec(serializer_t *, srproc_t, mblk_t *, void *);
1387c478bd9Sstevel@tonic-gate static void serializer_enqueue(serializer_t *, srproc_t, mblk_t *, void *);
1397c478bd9Sstevel@tonic-gate static void serializer_drain(serializer_t *, int);
1407c478bd9Sstevel@tonic-gate static void serializer_drain_completely(serializer_t *);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * SERIALIZER Implementation.
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * Record debugging information and execute single request.
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate static void
serializer_exec(serializer_t * s,srproc_t proc,mblk_t * mp,void * arg)1507c478bd9Sstevel@tonic-gate serializer_exec(serializer_t *s, srproc_t proc, mblk_t *mp, void *arg)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&s->ser_lock));
1537c478bd9Sstevel@tonic-gate 	ASSERT(s->ser_owner == curthread);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	ASSERT(proc != NULL);
1567c478bd9Sstevel@tonic-gate 	ASSERT(mp != NULL);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	s->ser_curr = mp;
1597c478bd9Sstevel@tonic-gate 	s->ser_arg = arg;
1607c478bd9Sstevel@tonic-gate 	s->ser_proc = proc;
1617c478bd9Sstevel@tonic-gate 	proc(mp, arg);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * Enqueue a single request on serializer.
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate static void
serializer_enqueue(serializer_t * s,srproc_t proc,mblk_t * mp,void * arg)1687c478bd9Sstevel@tonic-gate serializer_enqueue(serializer_t *s, srproc_t proc, mblk_t *mp, void *arg)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&s->ser_lock));
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	DTRACE_PROBE4(serializer__enqueue, serializer_t *, s,
1737c478bd9Sstevel@tonic-gate 	    mblk_t *, mp, void *, arg, srproc_t, proc);
1747c478bd9Sstevel@tonic-gate 	s->ser_count++;
1757c478bd9Sstevel@tonic-gate 	mp->b_queue = (queue_t *)proc;
1767c478bd9Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)arg;
1777c478bd9Sstevel@tonic-gate 	if (s->ser_last != NULL)
1787c478bd9Sstevel@tonic-gate 		s->ser_last->b_next = mp;
1797c478bd9Sstevel@tonic-gate 	else
1807c478bd9Sstevel@tonic-gate 		s->ser_first = mp;
1817c478bd9Sstevel@tonic-gate 	s->ser_last = mp;
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * Drain serializer, limiting drain to `credit' requests at most.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate static void
serializer_drain(serializer_t * s,int credit)1887c478bd9Sstevel@tonic-gate serializer_drain(serializer_t *s, int credit)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	mblk_t *mp = s->ser_first;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&s->ser_lock));
1937c478bd9Sstevel@tonic-gate 	ASSERT(s->ser_owner == curthread);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	for (; mp != NULL && credit-- != 0; mp = s->ser_first) {
1967c478bd9Sstevel@tonic-gate 		srproc_t *proc = (srproc_t *)mp->b_queue;
1977c478bd9Sstevel@tonic-gate 		void *arg = mp->b_prev;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 		if ((s->ser_first = s->ser_first->b_next) == NULL) {
2007c478bd9Sstevel@tonic-gate 			s->ser_last = NULL;
2017c478bd9Sstevel@tonic-gate 		} else {
2027c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 		ASSERT(s->ser_count != 0);
2057c478bd9Sstevel@tonic-gate 		s->ser_count--;
2067c478bd9Sstevel@tonic-gate 		mp->b_queue = NULL;
2077c478bd9Sstevel@tonic-gate 		mp->b_prev = NULL;
2087c478bd9Sstevel@tonic-gate 		mutex_exit(&s->ser_lock);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		DTRACE_PROBE4(serializer__exec__start, serializer_t *, s,
2117c478bd9Sstevel@tonic-gate 		    mblk_t *, mp, void *, arg, srproc_t, proc);
2127c478bd9Sstevel@tonic-gate 		serializer_exec(s, proc, mp, arg);
2137c478bd9Sstevel@tonic-gate 		DTRACE_PROBE4(serializer__exec__end, serializer_t *, s,
2147c478bd9Sstevel@tonic-gate 		    mblk_t *, mp, void *, arg, srproc_t, proc);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		mutex_enter(&s->ser_lock);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * Drain serializer completely if serializer is free.
2227c478bd9Sstevel@tonic-gate  */
2237c478bd9Sstevel@tonic-gate static void
serializer_drain_completely(serializer_t * s)2247c478bd9Sstevel@tonic-gate serializer_drain_completely(serializer_t *s)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	mutex_enter(&s->ser_lock);
2277c478bd9Sstevel@tonic-gate 	ASSERT(s->ser_taskq);
2287c478bd9Sstevel@tonic-gate 	if (s->ser_owner == NULL) {
2297c478bd9Sstevel@tonic-gate 		s->ser_owner = curthread;
2307c478bd9Sstevel@tonic-gate 		while (s->ser_first != NULL)
2317c478bd9Sstevel@tonic-gate 			serializer_drain(s, INT_MAX);
2327c478bd9Sstevel@tonic-gate 		s->ser_owner = NULL;
2337c478bd9Sstevel@tonic-gate 		s->ser_curr = NULL;
2347c478bd9Sstevel@tonic-gate 		s->ser_proc = NULL;
2357c478bd9Sstevel@tonic-gate 		s->ser_arg = NULL;
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 	s->ser_taskq = B_FALSE;
2387c478bd9Sstevel@tonic-gate 	/*
2397c478bd9Sstevel@tonic-gate 	 * Wake up serializer_wait().
2407c478bd9Sstevel@tonic-gate 	 */
2417c478bd9Sstevel@tonic-gate 	cv_signal(&s->ser_cv);
2427c478bd9Sstevel@tonic-gate 	mutex_exit(&s->ser_lock);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate  * Call proc(mp, arg) within serializer.
2477c478bd9Sstevel@tonic-gate  *
2487c478bd9Sstevel@tonic-gate  * If serializer is empty and not owned, proc(mp, arg) is called right
2497c478bd9Sstevel@tonic-gate  * away. Otherwise the request is queued.
2507c478bd9Sstevel@tonic-gate  */
2517c478bd9Sstevel@tonic-gate void
serializer_enter(serializer_t * s,srproc_t proc,mblk_t * mp,void * arg)2527c478bd9Sstevel@tonic-gate serializer_enter(serializer_t *s, srproc_t proc, mblk_t *mp, void *arg)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	ASSERT(proc != NULL);
2557c478bd9Sstevel@tonic-gate 	ASSERT(mp != NULL);
2567c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL);
2577c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_prev == NULL);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&s->ser_lock));
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	mutex_enter(&s->ser_lock);
2627c478bd9Sstevel@tonic-gate 	if (s->ser_owner != NULL) {
2637c478bd9Sstevel@tonic-gate 		/*
2647c478bd9Sstevel@tonic-gate 		 * Serializer is owned. Enqueue and return.
2657c478bd9Sstevel@tonic-gate 		 */
2667c478bd9Sstevel@tonic-gate 		serializer_enqueue(s, proc, mp, arg);
2677c478bd9Sstevel@tonic-gate 	} else {
268*fc8ae2ecSToomas Soome 		taskqid_t tid = TASKQID_INVALID;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		/*
2717c478bd9Sstevel@tonic-gate 		 * If the request list is empty, can process right away,
2727c478bd9Sstevel@tonic-gate 		 * otherwise enqueue and process.
2737c478bd9Sstevel@tonic-gate 		 */
2747c478bd9Sstevel@tonic-gate 		s->ser_owner = curthread;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		if (s->ser_first != NULL) {
2777c478bd9Sstevel@tonic-gate 			ASSERT(s->ser_count != 0);
2787c478bd9Sstevel@tonic-gate 			serializer_enqueue(s, proc, mp, arg);
2797c478bd9Sstevel@tonic-gate 		} else {
2807c478bd9Sstevel@tonic-gate 			ASSERT(s->ser_count == 0);
2817c478bd9Sstevel@tonic-gate 			mutex_exit(&s->ser_lock);
2827c478bd9Sstevel@tonic-gate 			/*
2837c478bd9Sstevel@tonic-gate 			 * Execute request
2847c478bd9Sstevel@tonic-gate 			 */
2857c478bd9Sstevel@tonic-gate 			DTRACE_PROBE4(serializer__exec__start,
2867c478bd9Sstevel@tonic-gate 			    serializer_t *, s, mblk_t *, mp,
2877c478bd9Sstevel@tonic-gate 			    void *, arg, srproc_t, proc);
2887c478bd9Sstevel@tonic-gate 			serializer_exec(s, proc, mp, arg);
2897c478bd9Sstevel@tonic-gate 			DTRACE_PROBE4(serializer__exec__end,
2907c478bd9Sstevel@tonic-gate 			    serializer_t *, s, mblk_t *, mp,
2917c478bd9Sstevel@tonic-gate 			    void *, arg, srproc_t, proc);
2927c478bd9Sstevel@tonic-gate 			mutex_enter(&s->ser_lock);
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 		/*
2967c478bd9Sstevel@tonic-gate 		 * Drain whatever has arrived in the meantime.
2977c478bd9Sstevel@tonic-gate 		 * If we spend too much time draining, continue draining by the
2987c478bd9Sstevel@tonic-gate 		 * taskq thread.
2997c478bd9Sstevel@tonic-gate 		 */
3007c478bd9Sstevel@tonic-gate 		while ((s->ser_first != NULL) && (tid == 0)) {
3017c478bd9Sstevel@tonic-gate 			serializer_drain(s, serializer_credit);
3027c478bd9Sstevel@tonic-gate 			if (s->ser_first != NULL) {
3037c478bd9Sstevel@tonic-gate 				perim_context_swtch++;
3047c478bd9Sstevel@tonic-gate 				/*
3057c478bd9Sstevel@tonic-gate 				 * If there is a taskq pending for this
3067c478bd9Sstevel@tonic-gate 				 * serializer, no need to schedule a new one.
3077c478bd9Sstevel@tonic-gate 				 */
3087c478bd9Sstevel@tonic-gate 				if (s->ser_taskq) {
3097c478bd9Sstevel@tonic-gate 					break;
3107c478bd9Sstevel@tonic-gate 				} else {
3117c478bd9Sstevel@tonic-gate 					tid = taskq_dispatch(system_taskq,
3127c478bd9Sstevel@tonic-gate 					    (task_func_t *)
3137c478bd9Sstevel@tonic-gate 					    serializer_drain_completely,
3147c478bd9Sstevel@tonic-gate 					    s, TQ_NOSLEEP | TQ_NOQUEUE);
315*fc8ae2ecSToomas Soome 					if (tid != TASKQID_INVALID)
3167c478bd9Sstevel@tonic-gate 						s->ser_taskq = B_TRUE;
3177c478bd9Sstevel@tonic-gate 				}
3187c478bd9Sstevel@tonic-gate 			}
3197c478bd9Sstevel@tonic-gate 		}
3207c478bd9Sstevel@tonic-gate 		s->ser_owner = NULL;
3217c478bd9Sstevel@tonic-gate 		s->ser_curr = NULL;
3227c478bd9Sstevel@tonic-gate 		s->ser_proc = NULL;
3237c478bd9Sstevel@tonic-gate 		s->ser_arg = NULL;
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 	/*
3267c478bd9Sstevel@tonic-gate 	 * Wakeup serializer_wait().
3277c478bd9Sstevel@tonic-gate 	 */
3287c478bd9Sstevel@tonic-gate 	cv_signal(&s->ser_cv);
3297c478bd9Sstevel@tonic-gate 	mutex_exit(&s->ser_lock);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate  * Wait for pending serializer jobs to complete. This function should never be
3347c478bd9Sstevel@tonic-gate  * called within the serializer or it will deadlock.
3357c478bd9Sstevel@tonic-gate  */
3367c478bd9Sstevel@tonic-gate void
serializer_wait(serializer_t * s)3377c478bd9Sstevel@tonic-gate serializer_wait(serializer_t *s)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	mutex_enter(&s->ser_lock);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	ASSERT(s->ser_owner != curthread);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	while ((s->ser_owner != NULL) || s->ser_taskq || (s->ser_first != NULL))
3447c478bd9Sstevel@tonic-gate 		cv_wait(&s->ser_cv, &s->ser_lock);
3457c478bd9Sstevel@tonic-gate 	ASSERT((s->ser_first == NULL) && (s->ser_last == NULL));
3467c478bd9Sstevel@tonic-gate 	/*
3477c478bd9Sstevel@tonic-gate 	 * Wakeup other potential waiters.
3487c478bd9Sstevel@tonic-gate 	 */
3497c478bd9Sstevel@tonic-gate 	cv_signal(&s->ser_cv);
3507c478bd9Sstevel@tonic-gate 	mutex_exit(&s->ser_lock);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate  * Create a new serializer.
3557c478bd9Sstevel@tonic-gate  */
3567c478bd9Sstevel@tonic-gate serializer_t *
serializer_create(int flags)3577c478bd9Sstevel@tonic-gate serializer_create(int flags)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	return (kmem_cache_alloc(serializer_cache, flags));
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * Wait for all pending entries to drain and then destroy serializer.
3647c478bd9Sstevel@tonic-gate  */
3657c478bd9Sstevel@tonic-gate void
serializer_destroy(serializer_t * s)3667c478bd9Sstevel@tonic-gate serializer_destroy(serializer_t *s)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate 	serializer_wait(s);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	ASSERT(s->ser_owner == NULL);
3717c478bd9Sstevel@tonic-gate 	ASSERT(s->ser_taskq == 0);
3727c478bd9Sstevel@tonic-gate 	ASSERT(s->ser_count == 0);
3737c478bd9Sstevel@tonic-gate 	ASSERT(s->ser_first == NULL);
3747c478bd9Sstevel@tonic-gate 	ASSERT(s->ser_last == NULL);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	kmem_cache_free(serializer_cache, s);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3807c478bd9Sstevel@tonic-gate static int
serializer_constructor(void * buf,void * cdrarg,int kmflags)3817c478bd9Sstevel@tonic-gate serializer_constructor(void *buf, void *cdrarg, int kmflags)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	serializer_t *s = buf;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	mutex_init(&s->ser_lock, NULL, MUTEX_DEFAULT, NULL);
3867c478bd9Sstevel@tonic-gate 	cv_init(&s->ser_cv, NULL, CV_DEFAULT, NULL);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	s->ser_taskq = 0;
3897c478bd9Sstevel@tonic-gate 	s->ser_count = 0;
3907c478bd9Sstevel@tonic-gate 	s->ser_first = s->ser_last = s->ser_curr = NULL;
3917c478bd9Sstevel@tonic-gate 	s->ser_proc = NULL;
3927c478bd9Sstevel@tonic-gate 	s->ser_arg = NULL;
3937c478bd9Sstevel@tonic-gate 	s->ser_owner = NULL;
3947c478bd9Sstevel@tonic-gate 	return (0);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3987c478bd9Sstevel@tonic-gate static void
serializer_destructor(void * buf,void * cdrarg)3997c478bd9Sstevel@tonic-gate serializer_destructor(void *buf, void *cdrarg)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	serializer_t *s = buf;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	mutex_destroy(&s->ser_lock);
4047c478bd9Sstevel@tonic-gate 	cv_destroy(&s->ser_cv);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate void
serializer_init(void)4087c478bd9Sstevel@tonic-gate serializer_init(void)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	serializer_cache = kmem_cache_create("serializer_cache",
4117c478bd9Sstevel@tonic-gate 	    sizeof (serializer_t), 0, serializer_constructor,
4127c478bd9Sstevel@tonic-gate 	    serializer_destructor, NULL, NULL, NULL, 0);
4137c478bd9Sstevel@tonic-gate }
414