10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52205Sdv142724 * Common Development and Distribution License (the "License"). 62205Sdv142724 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 225834Spt157919 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * Big Theory Statement for mutual exclusion locking primitives. 300Sstevel@tonic-gate * 310Sstevel@tonic-gate * A mutex serializes multiple threads so that only one thread 320Sstevel@tonic-gate * (the "owner" of the mutex) is active at a time. See mutex(9F) 330Sstevel@tonic-gate * for a full description of the interfaces and programming model. 340Sstevel@tonic-gate * The rest of this comment describes the implementation. 350Sstevel@tonic-gate * 360Sstevel@tonic-gate * Mutexes come in two flavors: adaptive and spin. mutex_init(9F) 370Sstevel@tonic-gate * determines the type based solely on the iblock cookie (PIL) argument. 380Sstevel@tonic-gate * PIL > LOCK_LEVEL implies a spin lock; everything else is adaptive. 390Sstevel@tonic-gate * 400Sstevel@tonic-gate * Spin mutexes block interrupts and spin until the lock becomes available. 410Sstevel@tonic-gate * A thread may not sleep, or call any function that might sleep, while 420Sstevel@tonic-gate * holding a spin mutex. With few exceptions, spin mutexes should only 430Sstevel@tonic-gate * be used to synchronize with interrupt handlers. 440Sstevel@tonic-gate * 450Sstevel@tonic-gate * Adaptive mutexes (the default type) spin if the owner is running on 460Sstevel@tonic-gate * another CPU and block otherwise. This policy is based on the assumption 470Sstevel@tonic-gate * that mutex hold times are typically short enough that the time spent 480Sstevel@tonic-gate * spinning is less than the time it takes to block. If you need mutual 490Sstevel@tonic-gate * exclusion semantics with long hold times, consider an rwlock(9F) as 500Sstevel@tonic-gate * RW_WRITER. Better still, reconsider the algorithm: if it requires 510Sstevel@tonic-gate * mutual exclusion for long periods of time, it's probably not scalable. 520Sstevel@tonic-gate * 530Sstevel@tonic-gate * Adaptive mutexes are overwhelmingly more common than spin mutexes, 540Sstevel@tonic-gate * so mutex_enter() assumes that the lock is adaptive. We get away 550Sstevel@tonic-gate * with this by structuring mutexes so that an attempt to acquire a 560Sstevel@tonic-gate * spin mutex as adaptive always fails. When mutex_enter() fails 570Sstevel@tonic-gate * it punts to mutex_vector_enter(), which does all the hard stuff. 580Sstevel@tonic-gate * 590Sstevel@tonic-gate * mutex_vector_enter() first checks the type. If it's spin mutex, 600Sstevel@tonic-gate * we just call lock_set_spl() and return. If it's an adaptive mutex, 610Sstevel@tonic-gate * we check to see what the owner is doing. If the owner is running, 620Sstevel@tonic-gate * we spin until the lock becomes available; if not, we mark the lock 630Sstevel@tonic-gate * as having waiters and block. 640Sstevel@tonic-gate * 650Sstevel@tonic-gate * Blocking on a mutex is surprisingly delicate dance because, for speed, 660Sstevel@tonic-gate * mutex_exit() doesn't use an atomic instruction. Thus we have to work 670Sstevel@tonic-gate * a little harder in the (rarely-executed) blocking path to make sure 680Sstevel@tonic-gate * we don't block on a mutex that's just been released -- otherwise we 690Sstevel@tonic-gate * might never be woken up. 700Sstevel@tonic-gate * 710Sstevel@tonic-gate * The logic for synchronizing mutex_vector_enter() with mutex_exit() 720Sstevel@tonic-gate * in the face of preemption and relaxed memory ordering is as follows: 730Sstevel@tonic-gate * 740Sstevel@tonic-gate * (1) Preemption in the middle of mutex_exit() must cause mutex_exit() 750Sstevel@tonic-gate * to restart. Each platform must enforce this by checking the 760Sstevel@tonic-gate * interrupted PC in the interrupt handler (or on return from trap -- 770Sstevel@tonic-gate * whichever is more convenient for the platform). If the PC 780Sstevel@tonic-gate * lies within the critical region of mutex_exit(), the interrupt 790Sstevel@tonic-gate * handler must reset the PC back to the beginning of mutex_exit(). 800Sstevel@tonic-gate * The critical region consists of all instructions up to, but not 810Sstevel@tonic-gate * including, the store that clears the lock (which, of course, 820Sstevel@tonic-gate * must never be executed twice.) 830Sstevel@tonic-gate * 840Sstevel@tonic-gate * This ensures that the owner will always check for waiters after 850Sstevel@tonic-gate * resuming from a previous preemption. 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * (2) A thread resuming in mutex_exit() does (at least) the following: 880Sstevel@tonic-gate * 890Sstevel@tonic-gate * when resuming: set CPU_THREAD = owner 900Sstevel@tonic-gate * membar #StoreLoad 910Sstevel@tonic-gate * 920Sstevel@tonic-gate * in mutex_exit: check waiters bit; do wakeup if set 930Sstevel@tonic-gate * membar #LoadStore|#StoreStore 940Sstevel@tonic-gate * clear owner 950Sstevel@tonic-gate * (at this point, other threads may or may not grab 960Sstevel@tonic-gate * the lock, and we may or may not reacquire it) 970Sstevel@tonic-gate * 980Sstevel@tonic-gate * when blocking: membar #StoreStore (due to disp_lock_enter()) 990Sstevel@tonic-gate * set CPU_THREAD = (possibly) someone else 1000Sstevel@tonic-gate * 1010Sstevel@tonic-gate * (3) A thread blocking in mutex_vector_enter() does the following: 1020Sstevel@tonic-gate * 1030Sstevel@tonic-gate * set waiters bit 1040Sstevel@tonic-gate * membar #StoreLoad (via membar_enter()) 1055834Spt157919 * check CPU_THREAD for owner's t_cpu 1065834Spt157919 * continue if owner running 1070Sstevel@tonic-gate * membar #LoadLoad (via membar_consumer()) 1080Sstevel@tonic-gate * check owner and waiters bit; abort if either changed 1090Sstevel@tonic-gate * block 1100Sstevel@tonic-gate * 1110Sstevel@tonic-gate * Thus the global memory orderings for (2) and (3) are as follows: 1120Sstevel@tonic-gate * 1130Sstevel@tonic-gate * (2M) mutex_exit() memory order: 1140Sstevel@tonic-gate * 1150Sstevel@tonic-gate * STORE CPU_THREAD = owner 1160Sstevel@tonic-gate * LOAD waiters bit 1170Sstevel@tonic-gate * STORE owner = NULL 1180Sstevel@tonic-gate * STORE CPU_THREAD = (possibly) someone else 1190Sstevel@tonic-gate * 1200Sstevel@tonic-gate * (3M) mutex_vector_enter() memory order: 1210Sstevel@tonic-gate * 1220Sstevel@tonic-gate * STORE waiters bit = 1 1230Sstevel@tonic-gate * LOAD CPU_THREAD for each CPU 1240Sstevel@tonic-gate * LOAD owner and waiters bit 1250Sstevel@tonic-gate * 1260Sstevel@tonic-gate * It has been verified by exhaustive simulation that all possible global 1270Sstevel@tonic-gate * memory orderings of (2M) interleaved with (3M) result in correct 1280Sstevel@tonic-gate * behavior. Moreover, these ordering constraints are minimal: changing 1290Sstevel@tonic-gate * the ordering of anything in (2M) or (3M) breaks the algorithm, creating 1300Sstevel@tonic-gate * windows for missed wakeups. Note: the possibility that other threads 1310Sstevel@tonic-gate * may grab the lock after the owner drops it can be factored out of the 1320Sstevel@tonic-gate * memory ordering analysis because mutex_vector_enter() won't block 1330Sstevel@tonic-gate * if the lock isn't still owned by the same thread. 1340Sstevel@tonic-gate * 1350Sstevel@tonic-gate * The only requirements of code outside the mutex implementation are 1360Sstevel@tonic-gate * (1) mutex_exit() preemption fixup in interrupt handlers or trap return, 1375834Spt157919 * (2) a membar #StoreLoad after setting CPU_THREAD in resume(), 1385834Spt157919 * (3) mutex_owner_running() preemption fixup in interrupt handlers 1395834Spt157919 * or trap returns. 1400Sstevel@tonic-gate * Note: idle threads cannot grab adaptive locks (since they cannot block), 1410Sstevel@tonic-gate * so the membar may be safely omitted when resuming an idle thread. 1420Sstevel@tonic-gate * 1430Sstevel@tonic-gate * When a mutex has waiters, mutex_vector_exit() has several options: 1440Sstevel@tonic-gate * 1450Sstevel@tonic-gate * (1) Choose a waiter and make that thread the owner before waking it; 1460Sstevel@tonic-gate * this is known as "direct handoff" of ownership. 1470Sstevel@tonic-gate * 1480Sstevel@tonic-gate * (2) Drop the lock and wake one waiter. 1490Sstevel@tonic-gate * 1500Sstevel@tonic-gate * (3) Drop the lock, clear the waiters bit, and wake all waiters. 1510Sstevel@tonic-gate * 1520Sstevel@tonic-gate * In many ways (1) is the cleanest solution, but if a lock is moderately 1530Sstevel@tonic-gate * contended it defeats the adaptive spin logic. If we make some other 1540Sstevel@tonic-gate * thread the owner, but he's not ONPROC yet, then all other threads on 1550Sstevel@tonic-gate * other cpus that try to get the lock will conclude that the owner is 1560Sstevel@tonic-gate * blocked, so they'll block too. And so on -- it escalates quickly, 1570Sstevel@tonic-gate * with every thread taking the blocking path rather than the spin path. 1580Sstevel@tonic-gate * Thus, direct handoff is *not* a good idea for adaptive mutexes. 1590Sstevel@tonic-gate * 1600Sstevel@tonic-gate * Option (2) is the next most natural-seeming option, but it has several 1610Sstevel@tonic-gate * annoying properties. If there's more than one waiter, we must preserve 1620Sstevel@tonic-gate * the waiters bit on an unheld lock. On cas-capable platforms, where 1630Sstevel@tonic-gate * the waiters bit is part of the lock word, this means that both 0x0 1640Sstevel@tonic-gate * and 0x1 represent unheld locks, so we have to cas against *both*. 1650Sstevel@tonic-gate * Priority inheritance also gets more complicated, because a lock can 1660Sstevel@tonic-gate * have waiters but no owner to whom priority can be willed. So while 1670Sstevel@tonic-gate * it is possible to make option (2) work, it's surprisingly vile. 1680Sstevel@tonic-gate * 1690Sstevel@tonic-gate * Option (3), the least-intuitive at first glance, is what we actually do. 1700Sstevel@tonic-gate * It has the advantage that because you always wake all waiters, you 1710Sstevel@tonic-gate * never have to preserve the waiters bit. Waking all waiters seems like 1720Sstevel@tonic-gate * begging for a thundering herd problem, but consider: under option (2), 1730Sstevel@tonic-gate * every thread that grabs and drops the lock will wake one waiter -- so 1740Sstevel@tonic-gate * if the lock is fairly active, all waiters will be awakened very quickly 1750Sstevel@tonic-gate * anyway. Moreover, this is how adaptive locks are *supposed* to work. 1760Sstevel@tonic-gate * The blocking case is rare; the more common case (by 3-4 orders of 1770Sstevel@tonic-gate * magnitude) is that one or more threads spin waiting to get the lock. 1780Sstevel@tonic-gate * Only direct handoff can prevent the thundering herd problem, but as 1790Sstevel@tonic-gate * mentioned earlier, that would tend to defeat the adaptive spin logic. 1800Sstevel@tonic-gate * In practice, option (3) works well because the blocking case is rare. 1810Sstevel@tonic-gate */ 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate /* 1840Sstevel@tonic-gate * delayed lock retry with exponential delay for spin locks 1850Sstevel@tonic-gate * 1860Sstevel@tonic-gate * It is noted above that for both the spin locks and the adaptive locks, 1870Sstevel@tonic-gate * spinning is the dominate mode of operation. So long as there is only 1880Sstevel@tonic-gate * one thread waiting on a lock, the naive spin loop works very well in 1890Sstevel@tonic-gate * cache based architectures. The lock data structure is pulled into the 1900Sstevel@tonic-gate * cache of the processor with the waiting/spinning thread and no further 1910Sstevel@tonic-gate * memory traffic is generated until the lock is released. Unfortunately, 1920Sstevel@tonic-gate * once two or more threads are waiting on a lock, the naive spin has 1930Sstevel@tonic-gate * the property of generating maximum memory traffic from each spinning 1940Sstevel@tonic-gate * thread as the spinning threads contend for the lock data structure. 1950Sstevel@tonic-gate * 1960Sstevel@tonic-gate * By executing a delay loop before retrying a lock, a waiting thread 1970Sstevel@tonic-gate * can reduce its memory traffic by a large factor, depending on the 1980Sstevel@tonic-gate * size of the delay loop. A large delay loop greatly reduced the memory 1990Sstevel@tonic-gate * traffic, but has the drawback of having a period of time when 2000Sstevel@tonic-gate * no thread is attempting to gain the lock even though several threads 2010Sstevel@tonic-gate * might be waiting. A small delay loop has the drawback of not 2020Sstevel@tonic-gate * much reduction in memory traffic, but reduces the potential idle time. 2030Sstevel@tonic-gate * The theory of the exponential delay code is to start with a short 2040Sstevel@tonic-gate * delay loop and double the waiting time on each iteration, up to 2055834Spt157919 * a preselected maximum. 2060Sstevel@tonic-gate */ 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate #include <sys/param.h> 2090Sstevel@tonic-gate #include <sys/time.h> 2100Sstevel@tonic-gate #include <sys/cpuvar.h> 2110Sstevel@tonic-gate #include <sys/thread.h> 2120Sstevel@tonic-gate #include <sys/debug.h> 2130Sstevel@tonic-gate #include <sys/cmn_err.h> 2140Sstevel@tonic-gate #include <sys/sobject.h> 2150Sstevel@tonic-gate #include <sys/turnstile.h> 2160Sstevel@tonic-gate #include <sys/systm.h> 2170Sstevel@tonic-gate #include <sys/mutex_impl.h> 2180Sstevel@tonic-gate #include <sys/spl.h> 2190Sstevel@tonic-gate #include <sys/lockstat.h> 2200Sstevel@tonic-gate #include <sys/atomic.h> 2210Sstevel@tonic-gate #include <sys/cpu.h> 2220Sstevel@tonic-gate #include <sys/stack.h> 2235084Sjohnlev #include <sys/archsystm.h> 2245834Spt157919 #include <sys/machsystm.h> 2255834Spt157919 #include <sys/x_call.h> 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate /* 2280Sstevel@tonic-gate * The sobj_ops vector exports a set of functions needed when a thread 2290Sstevel@tonic-gate * is asleep on a synchronization object of this type. 2300Sstevel@tonic-gate */ 2310Sstevel@tonic-gate static sobj_ops_t mutex_sobj_ops = { 2320Sstevel@tonic-gate SOBJ_MUTEX, mutex_owner, turnstile_stay_asleep, turnstile_change_pri 2330Sstevel@tonic-gate }; 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate /* 2360Sstevel@tonic-gate * If the system panics on a mutex, save the address of the offending 2370Sstevel@tonic-gate * mutex in panic_mutex_addr, and save the contents in panic_mutex. 2380Sstevel@tonic-gate */ 2390Sstevel@tonic-gate static mutex_impl_t panic_mutex; 2400Sstevel@tonic-gate static mutex_impl_t *panic_mutex_addr; 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate static void 2430Sstevel@tonic-gate mutex_panic(char *msg, mutex_impl_t *lp) 2440Sstevel@tonic-gate { 2450Sstevel@tonic-gate if (panicstr) 2460Sstevel@tonic-gate return; 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate if (casptr(&panic_mutex_addr, NULL, lp) == NULL) 2490Sstevel@tonic-gate panic_mutex = *lp; 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate panic("%s, lp=%p owner=%p thread=%p", 2520Sstevel@tonic-gate msg, lp, MUTEX_OWNER(&panic_mutex), curthread); 2530Sstevel@tonic-gate } 2540Sstevel@tonic-gate 2555834Spt157919 /* "tunables" for per-platform backoff constants. */ 2565834Spt157919 uint_t mutex_backoff_cap = 0; 2575834Spt157919 ushort_t mutex_backoff_base = MUTEX_BACKOFF_BASE; 2585834Spt157919 ushort_t mutex_cap_factor = MUTEX_CAP_FACTOR; 2595834Spt157919 uchar_t mutex_backoff_shift = MUTEX_BACKOFF_SHIFT; 2605834Spt157919 2615834Spt157919 void 2625834Spt157919 mutex_sync(void) 2635834Spt157919 { 2645834Spt157919 MUTEX_SYNC(); 2655834Spt157919 } 2665834Spt157919 2675834Spt157919 /* calculate the backoff interval */ 268*6138Ssvemuri uint_t 2695834Spt157919 default_lock_backoff(uint_t backoff) 2705834Spt157919 { 2715834Spt157919 uint_t cap; /* backoff cap calculated */ 2725834Spt157919 2735834Spt157919 if (backoff == 0) { 2745834Spt157919 backoff = mutex_backoff_base; 2755834Spt157919 /* first call just sets the base */ 2765834Spt157919 return (backoff); 2775834Spt157919 } 2785834Spt157919 2795834Spt157919 /* set cap */ 2805834Spt157919 if (mutex_backoff_cap == 0) { 2815834Spt157919 /* 2825834Spt157919 * For a contended lock, in the worst case a load + cas may 2835834Spt157919 * be queued at the controller for each contending CPU. 2845834Spt157919 * Therefore, to avoid queueing, the accesses for all CPUS must 2855834Spt157919 * be spread out in time over an interval of (ncpu * 2865834Spt157919 * cap-factor). Maximum backoff is set to this value, and 2875834Spt157919 * actual backoff is a random number from 0 to the current max. 2885834Spt157919 */ 2895834Spt157919 cap = ncpus_online * mutex_cap_factor; 2905834Spt157919 } else { 2915834Spt157919 cap = mutex_backoff_cap; 2925834Spt157919 } 2935834Spt157919 2945834Spt157919 /* calculate new backoff value */ 2955834Spt157919 backoff <<= mutex_backoff_shift; /* increase backoff */ 2965834Spt157919 if (backoff > cap) { 2975834Spt157919 if (cap < mutex_backoff_base) 2985834Spt157919 backoff = mutex_backoff_base; 2995834Spt157919 else 3005834Spt157919 backoff = cap; 3015834Spt157919 } 3025834Spt157919 3035834Spt157919 return (backoff); 3045834Spt157919 } 3055834Spt157919 3065834Spt157919 /* 3075834Spt157919 * default delay function for mutexes. 3085834Spt157919 */ 309*6138Ssvemuri void 3105834Spt157919 default_lock_delay(uint_t backoff) 3115834Spt157919 { 3125834Spt157919 ulong_t rnd; /* random factor */ 3135834Spt157919 uint_t cur_backoff; /* calculated backoff */ 3145834Spt157919 uint_t backctr; 3155834Spt157919 3165834Spt157919 /* 3175834Spt157919 * Modify backoff by a random amount to avoid lockstep, and to 3185834Spt157919 * make it probable that some thread gets a small backoff, and 3195834Spt157919 * re-checks quickly 3205834Spt157919 */ 3215834Spt157919 rnd = (((long)curthread >> PTR24_LSB) ^ (long)MUTEX_GETTICK()); 3225834Spt157919 cur_backoff = (uint_t)(rnd % (backoff - mutex_backoff_base + 1)) + 3235834Spt157919 mutex_backoff_base; 3245834Spt157919 3255834Spt157919 /* 3265834Spt157919 * Delay before trying 3275834Spt157919 * to touch the mutex data structure. 3285834Spt157919 */ 3295834Spt157919 for (backctr = cur_backoff; backctr; backctr--) { 3305834Spt157919 MUTEX_DELAY(); 3315834Spt157919 }; 3325834Spt157919 } 3335834Spt157919 3345834Spt157919 uint_t (*mutex_lock_backoff)(uint_t) = default_lock_backoff; 3355834Spt157919 void (*mutex_lock_delay)(uint_t) = default_lock_delay; 3365834Spt157919 void (*mutex_delay)(void) = mutex_delay_default; 3375834Spt157919 3380Sstevel@tonic-gate /* 3390Sstevel@tonic-gate * mutex_vector_enter() is called from the assembly mutex_enter() routine 3400Sstevel@tonic-gate * if the lock is held or is not of type MUTEX_ADAPTIVE. 3410Sstevel@tonic-gate */ 3420Sstevel@tonic-gate void 3430Sstevel@tonic-gate mutex_vector_enter(mutex_impl_t *lp) 3440Sstevel@tonic-gate { 3450Sstevel@tonic-gate kthread_id_t owner; 3465834Spt157919 kthread_id_t lastowner = MUTEX_NO_OWNER; /* track owner changes */ 3470Sstevel@tonic-gate hrtime_t sleep_time = 0; /* how long we slept */ 3486103Sck142721 hrtime_t spin_time = 0; /* how long we spun */ 3495834Spt157919 cpu_t *cpup; 3500Sstevel@tonic-gate turnstile_t *ts; 3510Sstevel@tonic-gate volatile mutex_impl_t *vlp = (volatile mutex_impl_t *)lp; 3525834Spt157919 uint_t backoff = 0; /* current backoff */ 3535834Spt157919 int changecnt = 0; /* count of owner changes */ 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate ASSERT_STACK_ALIGNED(); 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate if (MUTEX_TYPE_SPIN(lp)) { 3580Sstevel@tonic-gate lock_set_spl(&lp->m_spin.m_spinlock, lp->m_spin.m_minspl, 3590Sstevel@tonic-gate &lp->m_spin.m_oldspl); 3600Sstevel@tonic-gate return; 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate if (!MUTEX_TYPE_ADAPTIVE(lp)) { 3640Sstevel@tonic-gate mutex_panic("mutex_enter: bad mutex", lp); 3650Sstevel@tonic-gate return; 3660Sstevel@tonic-gate } 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate /* 3690Sstevel@tonic-gate * Adaptive mutexes must not be acquired from above LOCK_LEVEL. 3700Sstevel@tonic-gate * We can migrate after loading CPU but before checking CPU_ON_INTR, 3710Sstevel@tonic-gate * so we must verify by disabling preemption and loading CPU again. 3720Sstevel@tonic-gate */ 3730Sstevel@tonic-gate cpup = CPU; 3740Sstevel@tonic-gate if (CPU_ON_INTR(cpup) && !panicstr) { 3750Sstevel@tonic-gate kpreempt_disable(); 3760Sstevel@tonic-gate if (CPU_ON_INTR(CPU)) 3770Sstevel@tonic-gate mutex_panic("mutex_enter: adaptive at high PIL", lp); 3780Sstevel@tonic-gate kpreempt_enable(); 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, sys, mutex_adenters, 1); 3820Sstevel@tonic-gate 3836103Sck142721 spin_time = LOCKSTAT_START_TIME(LS_MUTEX_ENTER_SPIN); 3846103Sck142721 3855834Spt157919 backoff = mutex_lock_backoff(0); /* set base backoff */ 3860Sstevel@tonic-gate for (;;) { 3875834Spt157919 mutex_lock_delay(backoff); /* backoff delay */ 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate if (panicstr) 3900Sstevel@tonic-gate return; 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate if ((owner = MUTEX_OWNER(vlp)) == NULL) { 3935834Spt157919 if (mutex_adaptive_tryenter(lp)) { 3940Sstevel@tonic-gate break; 3955834Spt157919 } 3965834Spt157919 /* increase backoff only on failed attempt. */ 3975834Spt157919 backoff = mutex_lock_backoff(backoff); 3985834Spt157919 changecnt++; 3990Sstevel@tonic-gate continue; 4005834Spt157919 } else if (lastowner != owner) { 4015834Spt157919 lastowner = owner; 4025834Spt157919 backoff = mutex_lock_backoff(backoff); 4035834Spt157919 changecnt++; 4045834Spt157919 } 4055834Spt157919 4065834Spt157919 if (changecnt >= ncpus_online) { 4075834Spt157919 backoff = mutex_lock_backoff(0); 4085834Spt157919 changecnt = 0; 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate if (owner == curthread) 4120Sstevel@tonic-gate mutex_panic("recursive mutex_enter", lp); 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate /* 4150Sstevel@tonic-gate * If lock is held but owner is not yet set, spin. 4160Sstevel@tonic-gate * (Only relevant for platforms that don't have cas.) 4170Sstevel@tonic-gate */ 4180Sstevel@tonic-gate if (owner == MUTEX_NO_OWNER) 4190Sstevel@tonic-gate continue; 4200Sstevel@tonic-gate 4215834Spt157919 if (mutex_owner_running(lp) != NULL) { 4225834Spt157919 continue; 4235834Spt157919 } 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate /* 4260Sstevel@tonic-gate * The owner appears not to be running, so block. 4270Sstevel@tonic-gate * See the Big Theory Statement for memory ordering issues. 4280Sstevel@tonic-gate */ 4290Sstevel@tonic-gate ts = turnstile_lookup(lp); 4300Sstevel@tonic-gate MUTEX_SET_WAITERS(lp); 4310Sstevel@tonic-gate membar_enter(); 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate /* 4340Sstevel@tonic-gate * Recheck whether owner is running after waiters bit hits 4350Sstevel@tonic-gate * global visibility (above). If owner is running, spin. 4360Sstevel@tonic-gate */ 4375834Spt157919 if (mutex_owner_running(lp) != NULL) { 4385834Spt157919 turnstile_exit(lp); 4395834Spt157919 continue; 4405834Spt157919 } 4410Sstevel@tonic-gate membar_consumer(); 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate /* 4440Sstevel@tonic-gate * If owner and waiters bit are unchanged, block. 4450Sstevel@tonic-gate */ 4460Sstevel@tonic-gate if (MUTEX_OWNER(vlp) == owner && MUTEX_HAS_WAITERS(vlp)) { 4470Sstevel@tonic-gate sleep_time -= gethrtime(); 4480Sstevel@tonic-gate (void) turnstile_block(ts, TS_WRITER_Q, lp, 4490Sstevel@tonic-gate &mutex_sobj_ops, NULL, NULL); 4500Sstevel@tonic-gate sleep_time += gethrtime(); 4515834Spt157919 /* reset backoff after turnstile */ 4525834Spt157919 backoff = mutex_lock_backoff(0); 4530Sstevel@tonic-gate } else { 4540Sstevel@tonic-gate turnstile_exit(lp); 4550Sstevel@tonic-gate } 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate ASSERT(MUTEX_OWNER(lp) == curthread); 4590Sstevel@tonic-gate 4602205Sdv142724 if (sleep_time != 0) { 4612205Sdv142724 /* 4622205Sdv142724 * Note, sleep time is the sum of all the sleeping we 4632205Sdv142724 * did. 4642205Sdv142724 */ 4650Sstevel@tonic-gate LOCKSTAT_RECORD(LS_MUTEX_ENTER_BLOCK, lp, sleep_time); 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate 4686103Sck142721 /* record spin time, don't count sleep time */ 4696103Sck142721 if (spin_time != 0) { 4706103Sck142721 LOCKSTAT_RECORD_TIME(LS_MUTEX_ENTER_SPIN, lp, 4716103Sck142721 spin_time + sleep_time); 4725834Spt157919 } 4732205Sdv142724 4740Sstevel@tonic-gate LOCKSTAT_RECORD0(LS_MUTEX_ENTER_ACQUIRE, lp); 4750Sstevel@tonic-gate } 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate /* 4780Sstevel@tonic-gate * mutex_vector_tryenter() is called from the assembly mutex_tryenter() 4790Sstevel@tonic-gate * routine if the lock is held or is not of type MUTEX_ADAPTIVE. 4800Sstevel@tonic-gate */ 4810Sstevel@tonic-gate int 4820Sstevel@tonic-gate mutex_vector_tryenter(mutex_impl_t *lp) 4830Sstevel@tonic-gate { 4840Sstevel@tonic-gate int s; 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate if (MUTEX_TYPE_ADAPTIVE(lp)) 4870Sstevel@tonic-gate return (0); /* we already tried in assembly */ 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate if (!MUTEX_TYPE_SPIN(lp)) { 4900Sstevel@tonic-gate mutex_panic("mutex_tryenter: bad mutex", lp); 4910Sstevel@tonic-gate return (0); 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate s = splr(lp->m_spin.m_minspl); 4950Sstevel@tonic-gate if (lock_try(&lp->m_spin.m_spinlock)) { 4960Sstevel@tonic-gate lp->m_spin.m_oldspl = (ushort_t)s; 4970Sstevel@tonic-gate return (1); 4980Sstevel@tonic-gate } 4990Sstevel@tonic-gate splx(s); 5000Sstevel@tonic-gate return (0); 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate /* 5040Sstevel@tonic-gate * mutex_vector_exit() is called from mutex_exit() if the lock is not 5050Sstevel@tonic-gate * adaptive, has waiters, or is not owned by the current thread (panic). 5060Sstevel@tonic-gate */ 5070Sstevel@tonic-gate void 5080Sstevel@tonic-gate mutex_vector_exit(mutex_impl_t *lp) 5090Sstevel@tonic-gate { 5100Sstevel@tonic-gate turnstile_t *ts; 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate if (MUTEX_TYPE_SPIN(lp)) { 5130Sstevel@tonic-gate lock_clear_splx(&lp->m_spin.m_spinlock, lp->m_spin.m_oldspl); 5140Sstevel@tonic-gate return; 5150Sstevel@tonic-gate } 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate if (MUTEX_OWNER(lp) != curthread) { 5180Sstevel@tonic-gate mutex_panic("mutex_exit: not owner", lp); 5190Sstevel@tonic-gate return; 5200Sstevel@tonic-gate } 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate ts = turnstile_lookup(lp); 5230Sstevel@tonic-gate MUTEX_CLEAR_LOCK_AND_WAITERS(lp); 5240Sstevel@tonic-gate if (ts == NULL) 5250Sstevel@tonic-gate turnstile_exit(lp); 5260Sstevel@tonic-gate else 5270Sstevel@tonic-gate turnstile_wakeup(ts, TS_WRITER_Q, ts->ts_waiters, NULL); 5280Sstevel@tonic-gate LOCKSTAT_RECORD0(LS_MUTEX_EXIT_RELEASE, lp); 5290Sstevel@tonic-gate } 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate int 5320Sstevel@tonic-gate mutex_owned(kmutex_t *mp) 5330Sstevel@tonic-gate { 5340Sstevel@tonic-gate mutex_impl_t *lp = (mutex_impl_t *)mp; 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate if (panicstr) 5370Sstevel@tonic-gate return (1); 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate if (MUTEX_TYPE_ADAPTIVE(lp)) 5400Sstevel@tonic-gate return (MUTEX_OWNER(lp) == curthread); 5410Sstevel@tonic-gate return (LOCK_HELD(&lp->m_spin.m_spinlock)); 5420Sstevel@tonic-gate } 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate kthread_t * 5450Sstevel@tonic-gate mutex_owner(kmutex_t *mp) 5460Sstevel@tonic-gate { 5470Sstevel@tonic-gate mutex_impl_t *lp = (mutex_impl_t *)mp; 5480Sstevel@tonic-gate kthread_id_t t; 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate if (MUTEX_TYPE_ADAPTIVE(lp) && (t = MUTEX_OWNER(lp)) != MUTEX_NO_OWNER) 5510Sstevel@tonic-gate return (t); 5520Sstevel@tonic-gate return (NULL); 5530Sstevel@tonic-gate } 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate /* 5560Sstevel@tonic-gate * The iblock cookie 'ibc' is the spl level associated with the lock; 5570Sstevel@tonic-gate * this alone determines whether the lock will be ADAPTIVE or SPIN. 5580Sstevel@tonic-gate * 5590Sstevel@tonic-gate * Adaptive mutexes created in zeroed memory do not need to call 5600Sstevel@tonic-gate * mutex_init() as their allocation in this fashion guarantees 5610Sstevel@tonic-gate * their initialization. 5620Sstevel@tonic-gate * eg adaptive mutexes created as static within the BSS or allocated 5630Sstevel@tonic-gate * by kmem_zalloc(). 5640Sstevel@tonic-gate */ 5650Sstevel@tonic-gate /* ARGSUSED */ 5660Sstevel@tonic-gate void 5670Sstevel@tonic-gate mutex_init(kmutex_t *mp, char *name, kmutex_type_t type, void *ibc) 5680Sstevel@tonic-gate { 5690Sstevel@tonic-gate mutex_impl_t *lp = (mutex_impl_t *)mp; 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate ASSERT(ibc < (void *)KERNELBASE); /* see 1215173 */ 5720Sstevel@tonic-gate 5730Sstevel@tonic-gate if ((intptr_t)ibc > ipltospl(LOCK_LEVEL) && ibc < (void *)KERNELBASE) { 5740Sstevel@tonic-gate ASSERT(type != MUTEX_ADAPTIVE && type != MUTEX_DEFAULT); 5750Sstevel@tonic-gate MUTEX_SET_TYPE(lp, MUTEX_SPIN); 5760Sstevel@tonic-gate LOCK_INIT_CLEAR(&lp->m_spin.m_spinlock); 5770Sstevel@tonic-gate LOCK_INIT_HELD(&lp->m_spin.m_dummylock); 5780Sstevel@tonic-gate lp->m_spin.m_minspl = (int)(intptr_t)ibc; 5790Sstevel@tonic-gate } else { 5800Sstevel@tonic-gate ASSERT(type != MUTEX_SPIN); 5810Sstevel@tonic-gate MUTEX_SET_TYPE(lp, MUTEX_ADAPTIVE); 5820Sstevel@tonic-gate MUTEX_CLEAR_LOCK_AND_WAITERS(lp); 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate } 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate void 5870Sstevel@tonic-gate mutex_destroy(kmutex_t *mp) 5880Sstevel@tonic-gate { 5890Sstevel@tonic-gate mutex_impl_t *lp = (mutex_impl_t *)mp; 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate if (lp->m_owner == 0 && !MUTEX_HAS_WAITERS(lp)) { 5920Sstevel@tonic-gate MUTEX_DESTROY(lp); 5930Sstevel@tonic-gate } else if (MUTEX_TYPE_SPIN(lp)) { 5940Sstevel@tonic-gate LOCKSTAT_RECORD0(LS_MUTEX_DESTROY_RELEASE, lp); 5950Sstevel@tonic-gate MUTEX_DESTROY(lp); 5960Sstevel@tonic-gate } else if (MUTEX_TYPE_ADAPTIVE(lp)) { 5970Sstevel@tonic-gate LOCKSTAT_RECORD0(LS_MUTEX_DESTROY_RELEASE, lp); 5980Sstevel@tonic-gate if (MUTEX_OWNER(lp) != curthread) 5990Sstevel@tonic-gate mutex_panic("mutex_destroy: not owner", lp); 6000Sstevel@tonic-gate if (MUTEX_HAS_WAITERS(lp)) { 6010Sstevel@tonic-gate turnstile_t *ts = turnstile_lookup(lp); 6020Sstevel@tonic-gate turnstile_exit(lp); 6030Sstevel@tonic-gate if (ts != NULL) 6040Sstevel@tonic-gate mutex_panic("mutex_destroy: has waiters", lp); 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate MUTEX_DESTROY(lp); 6070Sstevel@tonic-gate } else { 6080Sstevel@tonic-gate mutex_panic("mutex_destroy: bad mutex", lp); 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate /* 6130Sstevel@tonic-gate * Simple C support for the cases where spin locks miss on the first try. 6140Sstevel@tonic-gate */ 6150Sstevel@tonic-gate void 6160Sstevel@tonic-gate lock_set_spin(lock_t *lp) 6170Sstevel@tonic-gate { 6185834Spt157919 int loop_count = 0; 6195834Spt157919 uint_t backoff = 0; /* current backoff */ 6206103Sck142721 hrtime_t spin_time = 0; /* how long we spun */ 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate if (panicstr) 6230Sstevel@tonic-gate return; 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate if (ncpus == 1) 6260Sstevel@tonic-gate panic("lock_set: %p lock held and only one CPU", lp); 6270Sstevel@tonic-gate 6286103Sck142721 spin_time = LOCKSTAT_START_TIME(LS_LOCK_SET_SPIN); 6296103Sck142721 6300Sstevel@tonic-gate while (LOCK_HELD(lp) || !lock_spin_try(lp)) { 6310Sstevel@tonic-gate if (panicstr) 6320Sstevel@tonic-gate return; 6335834Spt157919 loop_count++; 6345834Spt157919 6355834Spt157919 if (ncpus_online == loop_count) { 6365834Spt157919 backoff = mutex_lock_backoff(0); 6375834Spt157919 loop_count = 0; 6383914Spm145316 } else { 6395834Spt157919 backoff = mutex_lock_backoff(backoff); 6400Sstevel@tonic-gate } 6415834Spt157919 mutex_lock_delay(backoff); 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate 6446103Sck142721 LOCKSTAT_RECORD_TIME(LS_LOCK_SET_SPIN, lp, spin_time); 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate LOCKSTAT_RECORD0(LS_LOCK_SET_ACQUIRE, lp); 6470Sstevel@tonic-gate } 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate void 6500Sstevel@tonic-gate lock_set_spl_spin(lock_t *lp, int new_pil, ushort_t *old_pil_addr, int old_pil) 6510Sstevel@tonic-gate { 6525834Spt157919 int loop_count = 0; 6535834Spt157919 uint_t backoff = 0; /* current backoff */ 6546103Sck142721 hrtime_t spin_time = 0; /* how long we spun */ 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate if (panicstr) 6570Sstevel@tonic-gate return; 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate if (ncpus == 1) 6600Sstevel@tonic-gate panic("lock_set_spl: %p lock held and only one CPU", lp); 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate ASSERT(new_pil > LOCK_LEVEL); 6630Sstevel@tonic-gate 6646103Sck142721 spin_time = LOCKSTAT_START_TIME(LS_LOCK_SET_SPL_SPIN); 6656103Sck142721 6660Sstevel@tonic-gate do { 6670Sstevel@tonic-gate splx(old_pil); 6680Sstevel@tonic-gate while (LOCK_HELD(lp)) { 6695834Spt157919 loop_count++; 6705834Spt157919 6710Sstevel@tonic-gate if (panicstr) { 6720Sstevel@tonic-gate *old_pil_addr = (ushort_t)splr(new_pil); 6730Sstevel@tonic-gate return; 6740Sstevel@tonic-gate } 6755834Spt157919 if (ncpus_online == loop_count) { 6765834Spt157919 backoff = mutex_lock_backoff(0); 6775834Spt157919 loop_count = 0; 6783914Spm145316 } else { 6795834Spt157919 backoff = mutex_lock_backoff(backoff); 6800Sstevel@tonic-gate } 6815834Spt157919 mutex_lock_delay(backoff); 6820Sstevel@tonic-gate } 6830Sstevel@tonic-gate old_pil = splr(new_pil); 6840Sstevel@tonic-gate } while (!lock_spin_try(lp)); 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate *old_pil_addr = (ushort_t)old_pil; 6870Sstevel@tonic-gate 6886103Sck142721 LOCKSTAT_RECORD_TIME(LS_LOCK_SET_SPL_SPIN, lp, spin_time); 6890Sstevel@tonic-gate 6906103Sck142721 LOCKSTAT_RECORD0(LS_LOCK_SET_SPL_ACQUIRE, lp); 6910Sstevel@tonic-gate } 692