xref: /illumos-gate/usr/src/uts/common/disp/disp_lock.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
5*9d68b18eSck  * Common Development and Distribution License (the "License").
6*9d68b18eSck  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*9d68b18eSck  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
297c478bd9Sstevel@tonic-gate #include <sys/systm.h>
307c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
317c478bd9Sstevel@tonic-gate #include <sys/debug.h>
327c478bd9Sstevel@tonic-gate #include <sys/inline.h>
337c478bd9Sstevel@tonic-gate #include <sys/disp.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
367c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
377c478bd9Sstevel@tonic-gate #include <sys/lockstat.h>
387c478bd9Sstevel@tonic-gate #include <sys/spl.h>
397c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
407c478bd9Sstevel@tonic-gate #include <sys/cpu.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * We check CPU_ON_INTR(CPU) when exiting a disp lock, rather than when
447c478bd9Sstevel@tonic-gate  * entering it, for a purely pragmatic reason: when exiting a disp lock
457c478bd9Sstevel@tonic-gate  * we know that we must be at PIL 10, and thus not preemptible; therefore
467c478bd9Sstevel@tonic-gate  * we can safely load the CPU pointer without worrying about it changing.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate static void
disp_onintr_panic(void)497c478bd9Sstevel@tonic-gate disp_onintr_panic(void)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	panic("dispatcher invoked from high-level interrupt handler");
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* ARGSUSED */
557c478bd9Sstevel@tonic-gate void
disp_lock_init(disp_lock_t * lp,char * name)567c478bd9Sstevel@tonic-gate disp_lock_init(disp_lock_t *lp, char *name)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	DISP_LOCK_INIT(lp);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /* ARGSUSED */
627c478bd9Sstevel@tonic-gate void
disp_lock_destroy(disp_lock_t * lp)637c478bd9Sstevel@tonic-gate disp_lock_destroy(disp_lock_t *lp)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	DISP_LOCK_DESTROY(lp);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate void
disp_lock_enter_high(disp_lock_t * lp)697c478bd9Sstevel@tonic-gate disp_lock_enter_high(disp_lock_t *lp)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	lock_set(lp);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate void
disp_lock_exit_high(disp_lock_t * lp)757c478bd9Sstevel@tonic-gate disp_lock_exit_high(disp_lock_t *lp)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	if (CPU_ON_INTR(CPU) != 0)
787c478bd9Sstevel@tonic-gate 		disp_onintr_panic();
797c478bd9Sstevel@tonic-gate 	ASSERT(DISP_LOCK_HELD(lp));
807c478bd9Sstevel@tonic-gate 	lock_clear(lp);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate void
disp_lock_enter(disp_lock_t * lp)847c478bd9Sstevel@tonic-gate disp_lock_enter(disp_lock_t *lp)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	lock_set_spl(lp, ipltospl(DISP_LEVEL), &curthread->t_oldspl);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate void
disp_lock_exit(disp_lock_t * lp)907c478bd9Sstevel@tonic-gate disp_lock_exit(disp_lock_t *lp)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	if (CPU_ON_INTR(CPU) != 0)
937c478bd9Sstevel@tonic-gate 		disp_onintr_panic();
947c478bd9Sstevel@tonic-gate 	ASSERT(DISP_LOCK_HELD(lp));
957c478bd9Sstevel@tonic-gate 	if (CPU->cpu_kprunrun) {
967c478bd9Sstevel@tonic-gate 		lock_clear_splx(lp, curthread->t_oldspl);
977c478bd9Sstevel@tonic-gate 		kpreempt(KPREEMPT_SYNC);
987c478bd9Sstevel@tonic-gate 	} else {
997c478bd9Sstevel@tonic-gate 		lock_clear_splx(lp, curthread->t_oldspl);
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate void
disp_lock_exit_nopreempt(disp_lock_t * lp)1047c478bd9Sstevel@tonic-gate disp_lock_exit_nopreempt(disp_lock_t *lp)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	if (CPU_ON_INTR(CPU) != 0)
1077c478bd9Sstevel@tonic-gate 		disp_onintr_panic();
1087c478bd9Sstevel@tonic-gate 	ASSERT(DISP_LOCK_HELD(lp));
1097c478bd9Sstevel@tonic-gate 	lock_clear_splx(lp, curthread->t_oldspl);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * Thread_lock() - get the correct dispatcher lock for the thread.
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate void
thread_lock(kthread_id_t t)1167c478bd9Sstevel@tonic-gate thread_lock(kthread_id_t t)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	int s = splhigh();
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if (CPU_ON_INTR(CPU) != 0)
1217c478bd9Sstevel@tonic-gate 		disp_onintr_panic();
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	for (;;) {
1247c478bd9Sstevel@tonic-gate 		lock_t *volatile *tlpp = &t->t_lockp;
1257c478bd9Sstevel@tonic-gate 		lock_t *lp = *tlpp;
1267c478bd9Sstevel@tonic-gate 		if (lock_try(lp)) {
1277c478bd9Sstevel@tonic-gate 			if (lp == *tlpp) {
1287c478bd9Sstevel@tonic-gate 				curthread->t_oldspl = (ushort_t)s;
1297c478bd9Sstevel@tonic-gate 				return;
1307c478bd9Sstevel@tonic-gate 			}
1317c478bd9Sstevel@tonic-gate 			lock_clear(lp);
1327c478bd9Sstevel@tonic-gate 		} else {
133*9d68b18eSck 			hrtime_t spin_time =
134*9d68b18eSck 			    LOCKSTAT_START_TIME(LS_THREAD_LOCK_SPIN);
1357c478bd9Sstevel@tonic-gate 			/*
1367c478bd9Sstevel@tonic-gate 			 * Lower spl and spin on lock with non-atomic load
1377c478bd9Sstevel@tonic-gate 			 * to avoid cache activity.  Spin until the lock
1387c478bd9Sstevel@tonic-gate 			 * becomes available or spontaneously changes.
1397c478bd9Sstevel@tonic-gate 			 */
1407c478bd9Sstevel@tonic-gate 			splx(s);
1417c478bd9Sstevel@tonic-gate 			while (lp == *tlpp && LOCK_HELD(lp)) {
1427c478bd9Sstevel@tonic-gate 				if (panicstr) {
1437c478bd9Sstevel@tonic-gate 					curthread->t_oldspl = splhigh();
1447c478bd9Sstevel@tonic-gate 					return;
1457c478bd9Sstevel@tonic-gate 				}
1467c478bd9Sstevel@tonic-gate 				SMT_PAUSE();
1477c478bd9Sstevel@tonic-gate 			}
148*9d68b18eSck 
149*9d68b18eSck 			LOCKSTAT_RECORD_TIME(LS_THREAD_LOCK_SPIN,
150*9d68b18eSck 			    lp, spin_time);
1517c478bd9Sstevel@tonic-gate 			s = splhigh();
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * Thread_lock_high() - get the correct dispatcher lock for the thread.
1587c478bd9Sstevel@tonic-gate  *	This version is called when already at high spl.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate void
thread_lock_high(kthread_id_t t)1617c478bd9Sstevel@tonic-gate thread_lock_high(kthread_id_t t)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	if (CPU_ON_INTR(CPU) != 0)
1647c478bd9Sstevel@tonic-gate 		disp_onintr_panic();
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	for (;;) {
1677c478bd9Sstevel@tonic-gate 		lock_t *volatile *tlpp = &t->t_lockp;
1687c478bd9Sstevel@tonic-gate 		lock_t *lp = *tlpp;
1697c478bd9Sstevel@tonic-gate 		if (lock_try(lp)) {
1707c478bd9Sstevel@tonic-gate 			if (lp == *tlpp)
1717c478bd9Sstevel@tonic-gate 				return;
1727c478bd9Sstevel@tonic-gate 			lock_clear(lp);
1737c478bd9Sstevel@tonic-gate 		} else {
174*9d68b18eSck 			hrtime_t spin_time =
175*9d68b18eSck 			    LOCKSTAT_START_TIME(LS_THREAD_LOCK_HIGH_SPIN);
1767c478bd9Sstevel@tonic-gate 			while (lp == *tlpp && LOCK_HELD(lp)) {
1777c478bd9Sstevel@tonic-gate 				if (panicstr)
1787c478bd9Sstevel@tonic-gate 					return;
1797c478bd9Sstevel@tonic-gate 				SMT_PAUSE();
1807c478bd9Sstevel@tonic-gate 			}
181*9d68b18eSck 			LOCKSTAT_RECORD_TIME(LS_THREAD_LOCK_HIGH_SPIN,
182*9d68b18eSck 			    lp, spin_time);
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * Called by THREAD_TRANSITION macro to change the thread state to
1897c478bd9Sstevel@tonic-gate  * the intermediate state-in-transititon state.
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate void
thread_transition(kthread_id_t t)1927c478bd9Sstevel@tonic-gate thread_transition(kthread_id_t t)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	disp_lock_t	*lp;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1977c478bd9Sstevel@tonic-gate 	ASSERT(t->t_lockp != &transition_lock);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	lp = t->t_lockp;
2007c478bd9Sstevel@tonic-gate 	t->t_lockp = &transition_lock;
2017c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(lp);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate  * Put thread in stop state, and set the lock pointer to the stop_lock.
2067c478bd9Sstevel@tonic-gate  * This effectively drops the lock on the thread, since the stop_lock
2077c478bd9Sstevel@tonic-gate  * isn't held.
2087c478bd9Sstevel@tonic-gate  * Eventually, stop_lock could be hashed if there is too much contention.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate void
thread_stop(kthread_id_t t)2117c478bd9Sstevel@tonic-gate thread_stop(kthread_id_t t)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	disp_lock_t	*lp;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
2167c478bd9Sstevel@tonic-gate 	ASSERT(t->t_lockp != &stop_lock);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	lp = t->t_lockp;
2197c478bd9Sstevel@tonic-gate 	t->t_state = TS_STOPPED;
2207c478bd9Sstevel@tonic-gate 	/*
2217c478bd9Sstevel@tonic-gate 	 * Ensure that t_state reaches global visibility before t_lockp
2227c478bd9Sstevel@tonic-gate 	 */
2237c478bd9Sstevel@tonic-gate 	membar_producer();
2247c478bd9Sstevel@tonic-gate 	t->t_lockp = &stop_lock;
2257c478bd9Sstevel@tonic-gate 	disp_lock_exit(lp);
2267c478bd9Sstevel@tonic-gate }
227