xref: /titanic_50/usr/src/uts/common/disp/disp_lock.c (revision 9d68b18ee78271952aa02af7a316bfb825f35d97)
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*9d68b18eSck142721  * Common Development and Distribution License (the "License").
6*9d68b18eSck142721  * 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*9d68b18eSck142721  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
337c478bd9Sstevel@tonic-gate #include <sys/debug.h>
347c478bd9Sstevel@tonic-gate #include <sys/inline.h>
357c478bd9Sstevel@tonic-gate #include <sys/disp.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
387c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
397c478bd9Sstevel@tonic-gate #include <sys/lockstat.h>
407c478bd9Sstevel@tonic-gate #include <sys/spl.h>
417c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
427c478bd9Sstevel@tonic-gate #include <sys/cpu.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * We check CPU_ON_INTR(CPU) when exiting a disp lock, rather than when
467c478bd9Sstevel@tonic-gate  * entering it, for a purely pragmatic reason: when exiting a disp lock
477c478bd9Sstevel@tonic-gate  * we know that we must be at PIL 10, and thus not preemptible; therefore
487c478bd9Sstevel@tonic-gate  * we can safely load the CPU pointer without worrying about it changing.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate static void
disp_onintr_panic(void)517c478bd9Sstevel@tonic-gate disp_onintr_panic(void)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	panic("dispatcher invoked from high-level interrupt handler");
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* ARGSUSED */
577c478bd9Sstevel@tonic-gate void
disp_lock_init(disp_lock_t * lp,char * name)587c478bd9Sstevel@tonic-gate disp_lock_init(disp_lock_t *lp, char *name)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	DISP_LOCK_INIT(lp);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* ARGSUSED */
647c478bd9Sstevel@tonic-gate void
disp_lock_destroy(disp_lock_t * lp)657c478bd9Sstevel@tonic-gate disp_lock_destroy(disp_lock_t *lp)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	DISP_LOCK_DESTROY(lp);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate void
disp_lock_enter_high(disp_lock_t * lp)717c478bd9Sstevel@tonic-gate disp_lock_enter_high(disp_lock_t *lp)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	lock_set(lp);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate void
disp_lock_exit_high(disp_lock_t * lp)777c478bd9Sstevel@tonic-gate disp_lock_exit_high(disp_lock_t *lp)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	if (CPU_ON_INTR(CPU) != 0)
807c478bd9Sstevel@tonic-gate 		disp_onintr_panic();
817c478bd9Sstevel@tonic-gate 	ASSERT(DISP_LOCK_HELD(lp));
827c478bd9Sstevel@tonic-gate 	lock_clear(lp);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate void
disp_lock_enter(disp_lock_t * lp)867c478bd9Sstevel@tonic-gate disp_lock_enter(disp_lock_t *lp)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	lock_set_spl(lp, ipltospl(DISP_LEVEL), &curthread->t_oldspl);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate void
disp_lock_exit(disp_lock_t * lp)927c478bd9Sstevel@tonic-gate disp_lock_exit(disp_lock_t *lp)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	if (CPU_ON_INTR(CPU) != 0)
957c478bd9Sstevel@tonic-gate 		disp_onintr_panic();
967c478bd9Sstevel@tonic-gate 	ASSERT(DISP_LOCK_HELD(lp));
977c478bd9Sstevel@tonic-gate 	if (CPU->cpu_kprunrun) {
987c478bd9Sstevel@tonic-gate 		lock_clear_splx(lp, curthread->t_oldspl);
997c478bd9Sstevel@tonic-gate 		kpreempt(KPREEMPT_SYNC);
1007c478bd9Sstevel@tonic-gate 	} else {
1017c478bd9Sstevel@tonic-gate 		lock_clear_splx(lp, curthread->t_oldspl);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate void
disp_lock_exit_nopreempt(disp_lock_t * lp)1067c478bd9Sstevel@tonic-gate disp_lock_exit_nopreempt(disp_lock_t *lp)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	if (CPU_ON_INTR(CPU) != 0)
1097c478bd9Sstevel@tonic-gate 		disp_onintr_panic();
1107c478bd9Sstevel@tonic-gate 	ASSERT(DISP_LOCK_HELD(lp));
1117c478bd9Sstevel@tonic-gate 	lock_clear_splx(lp, curthread->t_oldspl);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * Thread_lock() - get the correct dispatcher lock for the thread.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate void
thread_lock(kthread_id_t t)1187c478bd9Sstevel@tonic-gate thread_lock(kthread_id_t t)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	int s = splhigh();
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (CPU_ON_INTR(CPU) != 0)
1237c478bd9Sstevel@tonic-gate 		disp_onintr_panic();
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	for (;;) {
1267c478bd9Sstevel@tonic-gate 		lock_t *volatile *tlpp = &t->t_lockp;
1277c478bd9Sstevel@tonic-gate 		lock_t *lp = *tlpp;
1287c478bd9Sstevel@tonic-gate 		if (lock_try(lp)) {
1297c478bd9Sstevel@tonic-gate 			if (lp == *tlpp) {
1307c478bd9Sstevel@tonic-gate 				curthread->t_oldspl = (ushort_t)s;
1317c478bd9Sstevel@tonic-gate 				return;
1327c478bd9Sstevel@tonic-gate 			}
1337c478bd9Sstevel@tonic-gate 			lock_clear(lp);
1347c478bd9Sstevel@tonic-gate 		} else {
135*9d68b18eSck142721 			hrtime_t spin_time =
136*9d68b18eSck142721 			    LOCKSTAT_START_TIME(LS_THREAD_LOCK_SPIN);
1377c478bd9Sstevel@tonic-gate 			/*
1387c478bd9Sstevel@tonic-gate 			 * Lower spl and spin on lock with non-atomic load
1397c478bd9Sstevel@tonic-gate 			 * to avoid cache activity.  Spin until the lock
1407c478bd9Sstevel@tonic-gate 			 * becomes available or spontaneously changes.
1417c478bd9Sstevel@tonic-gate 			 */
1427c478bd9Sstevel@tonic-gate 			splx(s);
1437c478bd9Sstevel@tonic-gate 			while (lp == *tlpp && LOCK_HELD(lp)) {
1447c478bd9Sstevel@tonic-gate 				if (panicstr) {
1457c478bd9Sstevel@tonic-gate 					curthread->t_oldspl = splhigh();
1467c478bd9Sstevel@tonic-gate 					return;
1477c478bd9Sstevel@tonic-gate 				}
1487c478bd9Sstevel@tonic-gate 				SMT_PAUSE();
1497c478bd9Sstevel@tonic-gate 			}
150*9d68b18eSck142721 
151*9d68b18eSck142721 			LOCKSTAT_RECORD_TIME(LS_THREAD_LOCK_SPIN,
152*9d68b18eSck142721 			    lp, spin_time);
1537c478bd9Sstevel@tonic-gate 			s = splhigh();
1547c478bd9Sstevel@tonic-gate 		}
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * Thread_lock_high() - get the correct dispatcher lock for the thread.
1607c478bd9Sstevel@tonic-gate  *	This version is called when already at high spl.
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate void
thread_lock_high(kthread_id_t t)1637c478bd9Sstevel@tonic-gate thread_lock_high(kthread_id_t t)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	if (CPU_ON_INTR(CPU) != 0)
1667c478bd9Sstevel@tonic-gate 		disp_onintr_panic();
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	for (;;) {
1697c478bd9Sstevel@tonic-gate 		lock_t *volatile *tlpp = &t->t_lockp;
1707c478bd9Sstevel@tonic-gate 		lock_t *lp = *tlpp;
1717c478bd9Sstevel@tonic-gate 		if (lock_try(lp)) {
1727c478bd9Sstevel@tonic-gate 			if (lp == *tlpp)
1737c478bd9Sstevel@tonic-gate 				return;
1747c478bd9Sstevel@tonic-gate 			lock_clear(lp);
1757c478bd9Sstevel@tonic-gate 		} else {
176*9d68b18eSck142721 			hrtime_t spin_time =
177*9d68b18eSck142721 			    LOCKSTAT_START_TIME(LS_THREAD_LOCK_HIGH_SPIN);
1787c478bd9Sstevel@tonic-gate 			while (lp == *tlpp && LOCK_HELD(lp)) {
1797c478bd9Sstevel@tonic-gate 				if (panicstr)
1807c478bd9Sstevel@tonic-gate 					return;
1817c478bd9Sstevel@tonic-gate 				SMT_PAUSE();
1827c478bd9Sstevel@tonic-gate 			}
183*9d68b18eSck142721 			LOCKSTAT_RECORD_TIME(LS_THREAD_LOCK_HIGH_SPIN,
184*9d68b18eSck142721 			    lp, spin_time);
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * Called by THREAD_TRANSITION macro to change the thread state to
1917c478bd9Sstevel@tonic-gate  * the intermediate state-in-transititon state.
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate void
thread_transition(kthread_id_t t)1947c478bd9Sstevel@tonic-gate thread_transition(kthread_id_t t)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	disp_lock_t	*lp;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
1997c478bd9Sstevel@tonic-gate 	ASSERT(t->t_lockp != &transition_lock);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	lp = t->t_lockp;
2027c478bd9Sstevel@tonic-gate 	t->t_lockp = &transition_lock;
2037c478bd9Sstevel@tonic-gate 	disp_lock_exit_high(lp);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * Put thread in stop state, and set the lock pointer to the stop_lock.
2087c478bd9Sstevel@tonic-gate  * This effectively drops the lock on the thread, since the stop_lock
2097c478bd9Sstevel@tonic-gate  * isn't held.
2107c478bd9Sstevel@tonic-gate  * Eventually, stop_lock could be hashed if there is too much contention.
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate void
thread_stop(kthread_id_t t)2137c478bd9Sstevel@tonic-gate thread_stop(kthread_id_t t)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	disp_lock_t	*lp;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
2187c478bd9Sstevel@tonic-gate 	ASSERT(t->t_lockp != &stop_lock);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	lp = t->t_lockp;
2217c478bd9Sstevel@tonic-gate 	t->t_state = TS_STOPPED;
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * Ensure that t_state reaches global visibility before t_lockp
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 	membar_producer();
2267c478bd9Sstevel@tonic-gate 	t->t_lockp = &stop_lock;
2277c478bd9Sstevel@tonic-gate 	disp_lock_exit(lp);
2287c478bd9Sstevel@tonic-gate }
229