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 /* 22*5834Spt157919 * 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()) 105*5834Spt157919 * check CPU_THREAD for owner's t_cpu 106*5834Spt157919 * 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, 137*5834Spt157919 * (2) a membar #StoreLoad after setting CPU_THREAD in resume(), 138*5834Spt157919 * (3) mutex_owner_running() preemption fixup in interrupt handlers 139*5834Spt157919 * 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 205*5834Spt157919 * 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> 224*5834Spt157919 #include <sys/machsystm.h> 225*5834Spt157919 #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 255*5834Spt157919 /* "tunables" for per-platform backoff constants. */ 256*5834Spt157919 uint_t mutex_backoff_cap = 0; 257*5834Spt157919 ushort_t mutex_backoff_base = MUTEX_BACKOFF_BASE; 258*5834Spt157919 ushort_t mutex_cap_factor = MUTEX_CAP_FACTOR; 259*5834Spt157919 uchar_t mutex_backoff_shift = MUTEX_BACKOFF_SHIFT; 260*5834Spt157919 261*5834Spt157919 void 262*5834Spt157919 mutex_sync(void) 263*5834Spt157919 { 264*5834Spt157919 MUTEX_SYNC(); 265*5834Spt157919 } 266*5834Spt157919 267*5834Spt157919 /* calculate the backoff interval */ 268*5834Spt157919 static uint_t 269*5834Spt157919 default_lock_backoff(uint_t backoff) 270*5834Spt157919 { 271*5834Spt157919 uint_t cap; /* backoff cap calculated */ 272*5834Spt157919 273*5834Spt157919 if (backoff == 0) { 274*5834Spt157919 backoff = mutex_backoff_base; 275*5834Spt157919 /* first call just sets the base */ 276*5834Spt157919 return (backoff); 277*5834Spt157919 } 278*5834Spt157919 279*5834Spt157919 /* set cap */ 280*5834Spt157919 if (mutex_backoff_cap == 0) { 281*5834Spt157919 /* 282*5834Spt157919 * For a contended lock, in the worst case a load + cas may 283*5834Spt157919 * be queued at the controller for each contending CPU. 284*5834Spt157919 * Therefore, to avoid queueing, the accesses for all CPUS must 285*5834Spt157919 * be spread out in time over an interval of (ncpu * 286*5834Spt157919 * cap-factor). Maximum backoff is set to this value, and 287*5834Spt157919 * actual backoff is a random number from 0 to the current max. 288*5834Spt157919 */ 289*5834Spt157919 cap = ncpus_online * mutex_cap_factor; 290*5834Spt157919 } else { 291*5834Spt157919 cap = mutex_backoff_cap; 292*5834Spt157919 } 293*5834Spt157919 294*5834Spt157919 /* calculate new backoff value */ 295*5834Spt157919 backoff <<= mutex_backoff_shift; /* increase backoff */ 296*5834Spt157919 if (backoff > cap) { 297*5834Spt157919 if (cap < mutex_backoff_base) 298*5834Spt157919 backoff = mutex_backoff_base; 299*5834Spt157919 else 300*5834Spt157919 backoff = cap; 301*5834Spt157919 } 302*5834Spt157919 303*5834Spt157919 return (backoff); 304*5834Spt157919 } 305*5834Spt157919 306*5834Spt157919 /* 307*5834Spt157919 * default delay function for mutexes. 308*5834Spt157919 */ 309*5834Spt157919 static void 310*5834Spt157919 default_lock_delay(uint_t backoff) 311*5834Spt157919 { 312*5834Spt157919 ulong_t rnd; /* random factor */ 313*5834Spt157919 uint_t cur_backoff; /* calculated backoff */ 314*5834Spt157919 uint_t backctr; 315*5834Spt157919 316*5834Spt157919 /* 317*5834Spt157919 * Modify backoff by a random amount to avoid lockstep, and to 318*5834Spt157919 * make it probable that some thread gets a small backoff, and 319*5834Spt157919 * re-checks quickly 320*5834Spt157919 */ 321*5834Spt157919 rnd = (((long)curthread >> PTR24_LSB) ^ (long)MUTEX_GETTICK()); 322*5834Spt157919 cur_backoff = (uint_t)(rnd % (backoff - mutex_backoff_base + 1)) + 323*5834Spt157919 mutex_backoff_base; 324*5834Spt157919 325*5834Spt157919 /* 326*5834Spt157919 * Delay before trying 327*5834Spt157919 * to touch the mutex data structure. 328*5834Spt157919 */ 329*5834Spt157919 for (backctr = cur_backoff; backctr; backctr--) { 330*5834Spt157919 MUTEX_DELAY(); 331*5834Spt157919 }; 332*5834Spt157919 } 333*5834Spt157919 334*5834Spt157919 uint_t (*mutex_lock_backoff)(uint_t) = default_lock_backoff; 335*5834Spt157919 void (*mutex_lock_delay)(uint_t) = default_lock_delay; 336*5834Spt157919 void (*mutex_delay)(void) = mutex_delay_default; 337*5834Spt157919 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; 346*5834Spt157919 kthread_id_t lastowner = MUTEX_NO_OWNER; /* track owner changes */ 3470Sstevel@tonic-gate hrtime_t sleep_time = 0; /* how long we slept */ 3480Sstevel@tonic-gate uint_t spin_count = 0; /* how many times we spun */ 349*5834Spt157919 cpu_t *cpup; 3500Sstevel@tonic-gate turnstile_t *ts; 3510Sstevel@tonic-gate volatile mutex_impl_t *vlp = (volatile mutex_impl_t *)lp; 352*5834Spt157919 uint_t backoff = 0; /* current backoff */ 3532205Sdv142724 int sleep_count = 0; 354*5834Spt157919 int changecnt = 0; /* count of owner changes */ 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate ASSERT_STACK_ALIGNED(); 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate if (MUTEX_TYPE_SPIN(lp)) { 3590Sstevel@tonic-gate lock_set_spl(&lp->m_spin.m_spinlock, lp->m_spin.m_minspl, 3600Sstevel@tonic-gate &lp->m_spin.m_oldspl); 3610Sstevel@tonic-gate return; 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate if (!MUTEX_TYPE_ADAPTIVE(lp)) { 3650Sstevel@tonic-gate mutex_panic("mutex_enter: bad mutex", lp); 3660Sstevel@tonic-gate return; 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate /* 3700Sstevel@tonic-gate * Adaptive mutexes must not be acquired from above LOCK_LEVEL. 3710Sstevel@tonic-gate * We can migrate after loading CPU but before checking CPU_ON_INTR, 3720Sstevel@tonic-gate * so we must verify by disabling preemption and loading CPU again. 3730Sstevel@tonic-gate */ 3740Sstevel@tonic-gate cpup = CPU; 3750Sstevel@tonic-gate if (CPU_ON_INTR(cpup) && !panicstr) { 3760Sstevel@tonic-gate kpreempt_disable(); 3770Sstevel@tonic-gate if (CPU_ON_INTR(CPU)) 3780Sstevel@tonic-gate mutex_panic("mutex_enter: adaptive at high PIL", lp); 3790Sstevel@tonic-gate kpreempt_enable(); 3800Sstevel@tonic-gate } 3810Sstevel@tonic-gate 3820Sstevel@tonic-gate CPU_STATS_ADDQ(cpup, sys, mutex_adenters, 1); 3830Sstevel@tonic-gate 384*5834Spt157919 backoff = mutex_lock_backoff(0); /* set base backoff */ 3850Sstevel@tonic-gate for (;;) { 3860Sstevel@tonic-gate spin_count++; 387*5834Spt157919 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) { 393*5834Spt157919 if (mutex_adaptive_tryenter(lp)) { 3940Sstevel@tonic-gate break; 395*5834Spt157919 } 396*5834Spt157919 /* increase backoff only on failed attempt. */ 397*5834Spt157919 backoff = mutex_lock_backoff(backoff); 398*5834Spt157919 changecnt++; 3990Sstevel@tonic-gate continue; 400*5834Spt157919 } else if (lastowner != owner) { 401*5834Spt157919 lastowner = owner; 402*5834Spt157919 backoff = mutex_lock_backoff(backoff); 403*5834Spt157919 changecnt++; 404*5834Spt157919 } 405*5834Spt157919 406*5834Spt157919 if (changecnt >= ncpus_online) { 407*5834Spt157919 backoff = mutex_lock_backoff(0); 408*5834Spt157919 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 421*5834Spt157919 if (mutex_owner_running(lp) != NULL) { 422*5834Spt157919 continue; 423*5834Spt157919 } 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 */ 437*5834Spt157919 if (mutex_owner_running(lp) != NULL) { 438*5834Spt157919 turnstile_exit(lp); 439*5834Spt157919 continue; 440*5834Spt157919 } 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(); 4512205Sdv142724 sleep_count++; 452*5834Spt157919 /* reset backoff after turnstile */ 453*5834Spt157919 backoff = mutex_lock_backoff(0); 4540Sstevel@tonic-gate } else { 4550Sstevel@tonic-gate turnstile_exit(lp); 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate ASSERT(MUTEX_OWNER(lp) == curthread); 4600Sstevel@tonic-gate 4612205Sdv142724 if (sleep_time != 0) { 4622205Sdv142724 /* 4632205Sdv142724 * Note, sleep time is the sum of all the sleeping we 4642205Sdv142724 * did. 4652205Sdv142724 */ 4660Sstevel@tonic-gate LOCKSTAT_RECORD(LS_MUTEX_ENTER_BLOCK, lp, sleep_time); 4670Sstevel@tonic-gate } 4680Sstevel@tonic-gate 4692205Sdv142724 /* 4702205Sdv142724 * We do not count a sleep as a spin. 4712205Sdv142724 */ 472*5834Spt157919 if (spin_count > sleep_count) { 4732205Sdv142724 LOCKSTAT_RECORD(LS_MUTEX_ENTER_SPIN, lp, 4742205Sdv142724 spin_count - sleep_count); 475*5834Spt157919 } 4762205Sdv142724 4770Sstevel@tonic-gate LOCKSTAT_RECORD0(LS_MUTEX_ENTER_ACQUIRE, lp); 4780Sstevel@tonic-gate } 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate /* 4810Sstevel@tonic-gate * mutex_vector_tryenter() is called from the assembly mutex_tryenter() 4820Sstevel@tonic-gate * routine if the lock is held or is not of type MUTEX_ADAPTIVE. 4830Sstevel@tonic-gate */ 4840Sstevel@tonic-gate int 4850Sstevel@tonic-gate mutex_vector_tryenter(mutex_impl_t *lp) 4860Sstevel@tonic-gate { 4870Sstevel@tonic-gate int s; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate if (MUTEX_TYPE_ADAPTIVE(lp)) 4900Sstevel@tonic-gate return (0); /* we already tried in assembly */ 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate if (!MUTEX_TYPE_SPIN(lp)) { 4930Sstevel@tonic-gate mutex_panic("mutex_tryenter: bad mutex", lp); 4940Sstevel@tonic-gate return (0); 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate s = splr(lp->m_spin.m_minspl); 4980Sstevel@tonic-gate if (lock_try(&lp->m_spin.m_spinlock)) { 4990Sstevel@tonic-gate lp->m_spin.m_oldspl = (ushort_t)s; 5000Sstevel@tonic-gate return (1); 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate splx(s); 5030Sstevel@tonic-gate return (0); 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate /* 5070Sstevel@tonic-gate * mutex_vector_exit() is called from mutex_exit() if the lock is not 5080Sstevel@tonic-gate * adaptive, has waiters, or is not owned by the current thread (panic). 5090Sstevel@tonic-gate */ 5100Sstevel@tonic-gate void 5110Sstevel@tonic-gate mutex_vector_exit(mutex_impl_t *lp) 5120Sstevel@tonic-gate { 5130Sstevel@tonic-gate turnstile_t *ts; 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate if (MUTEX_TYPE_SPIN(lp)) { 5160Sstevel@tonic-gate lock_clear_splx(&lp->m_spin.m_spinlock, lp->m_spin.m_oldspl); 5170Sstevel@tonic-gate return; 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate if (MUTEX_OWNER(lp) != curthread) { 5210Sstevel@tonic-gate mutex_panic("mutex_exit: not owner", lp); 5220Sstevel@tonic-gate return; 5230Sstevel@tonic-gate } 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate ts = turnstile_lookup(lp); 5260Sstevel@tonic-gate MUTEX_CLEAR_LOCK_AND_WAITERS(lp); 5270Sstevel@tonic-gate if (ts == NULL) 5280Sstevel@tonic-gate turnstile_exit(lp); 5290Sstevel@tonic-gate else 5300Sstevel@tonic-gate turnstile_wakeup(ts, TS_WRITER_Q, ts->ts_waiters, NULL); 5310Sstevel@tonic-gate LOCKSTAT_RECORD0(LS_MUTEX_EXIT_RELEASE, lp); 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate int 5350Sstevel@tonic-gate mutex_owned(kmutex_t *mp) 5360Sstevel@tonic-gate { 5370Sstevel@tonic-gate mutex_impl_t *lp = (mutex_impl_t *)mp; 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate if (panicstr) 5400Sstevel@tonic-gate return (1); 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate if (MUTEX_TYPE_ADAPTIVE(lp)) 5430Sstevel@tonic-gate return (MUTEX_OWNER(lp) == curthread); 5440Sstevel@tonic-gate return (LOCK_HELD(&lp->m_spin.m_spinlock)); 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate kthread_t * 5480Sstevel@tonic-gate mutex_owner(kmutex_t *mp) 5490Sstevel@tonic-gate { 5500Sstevel@tonic-gate mutex_impl_t *lp = (mutex_impl_t *)mp; 5510Sstevel@tonic-gate kthread_id_t t; 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate if (MUTEX_TYPE_ADAPTIVE(lp) && (t = MUTEX_OWNER(lp)) != MUTEX_NO_OWNER) 5540Sstevel@tonic-gate return (t); 5550Sstevel@tonic-gate return (NULL); 5560Sstevel@tonic-gate } 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate /* 5590Sstevel@tonic-gate * The iblock cookie 'ibc' is the spl level associated with the lock; 5600Sstevel@tonic-gate * this alone determines whether the lock will be ADAPTIVE or SPIN. 5610Sstevel@tonic-gate * 5620Sstevel@tonic-gate * Adaptive mutexes created in zeroed memory do not need to call 5630Sstevel@tonic-gate * mutex_init() as their allocation in this fashion guarantees 5640Sstevel@tonic-gate * their initialization. 5650Sstevel@tonic-gate * eg adaptive mutexes created as static within the BSS or allocated 5660Sstevel@tonic-gate * by kmem_zalloc(). 5670Sstevel@tonic-gate */ 5680Sstevel@tonic-gate /* ARGSUSED */ 5690Sstevel@tonic-gate void 5700Sstevel@tonic-gate mutex_init(kmutex_t *mp, char *name, kmutex_type_t type, void *ibc) 5710Sstevel@tonic-gate { 5720Sstevel@tonic-gate mutex_impl_t *lp = (mutex_impl_t *)mp; 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate ASSERT(ibc < (void *)KERNELBASE); /* see 1215173 */ 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate if ((intptr_t)ibc > ipltospl(LOCK_LEVEL) && ibc < (void *)KERNELBASE) { 5770Sstevel@tonic-gate ASSERT(type != MUTEX_ADAPTIVE && type != MUTEX_DEFAULT); 5780Sstevel@tonic-gate MUTEX_SET_TYPE(lp, MUTEX_SPIN); 5790Sstevel@tonic-gate LOCK_INIT_CLEAR(&lp->m_spin.m_spinlock); 5800Sstevel@tonic-gate LOCK_INIT_HELD(&lp->m_spin.m_dummylock); 5810Sstevel@tonic-gate lp->m_spin.m_minspl = (int)(intptr_t)ibc; 5820Sstevel@tonic-gate } else { 5830Sstevel@tonic-gate ASSERT(type != MUTEX_SPIN); 5840Sstevel@tonic-gate MUTEX_SET_TYPE(lp, MUTEX_ADAPTIVE); 5850Sstevel@tonic-gate MUTEX_CLEAR_LOCK_AND_WAITERS(lp); 5860Sstevel@tonic-gate } 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate void 5900Sstevel@tonic-gate mutex_destroy(kmutex_t *mp) 5910Sstevel@tonic-gate { 5920Sstevel@tonic-gate mutex_impl_t *lp = (mutex_impl_t *)mp; 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate if (lp->m_owner == 0 && !MUTEX_HAS_WAITERS(lp)) { 5950Sstevel@tonic-gate MUTEX_DESTROY(lp); 5960Sstevel@tonic-gate } else if (MUTEX_TYPE_SPIN(lp)) { 5970Sstevel@tonic-gate LOCKSTAT_RECORD0(LS_MUTEX_DESTROY_RELEASE, lp); 5980Sstevel@tonic-gate MUTEX_DESTROY(lp); 5990Sstevel@tonic-gate } else if (MUTEX_TYPE_ADAPTIVE(lp)) { 6000Sstevel@tonic-gate LOCKSTAT_RECORD0(LS_MUTEX_DESTROY_RELEASE, lp); 6010Sstevel@tonic-gate if (MUTEX_OWNER(lp) != curthread) 6020Sstevel@tonic-gate mutex_panic("mutex_destroy: not owner", lp); 6030Sstevel@tonic-gate if (MUTEX_HAS_WAITERS(lp)) { 6040Sstevel@tonic-gate turnstile_t *ts = turnstile_lookup(lp); 6050Sstevel@tonic-gate turnstile_exit(lp); 6060Sstevel@tonic-gate if (ts != NULL) 6070Sstevel@tonic-gate mutex_panic("mutex_destroy: has waiters", lp); 6080Sstevel@tonic-gate } 6090Sstevel@tonic-gate MUTEX_DESTROY(lp); 6100Sstevel@tonic-gate } else { 6110Sstevel@tonic-gate mutex_panic("mutex_destroy: bad mutex", lp); 6120Sstevel@tonic-gate } 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate /* 6160Sstevel@tonic-gate * Simple C support for the cases where spin locks miss on the first try. 6170Sstevel@tonic-gate */ 6180Sstevel@tonic-gate void 6190Sstevel@tonic-gate lock_set_spin(lock_t *lp) 6200Sstevel@tonic-gate { 6210Sstevel@tonic-gate int spin_count = 1; 622*5834Spt157919 int loop_count = 0; 623*5834Spt157919 uint_t backoff = 0; /* current backoff */ 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate if (panicstr) 6260Sstevel@tonic-gate return; 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate if (ncpus == 1) 6290Sstevel@tonic-gate panic("lock_set: %p lock held and only one CPU", lp); 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate while (LOCK_HELD(lp) || !lock_spin_try(lp)) { 6320Sstevel@tonic-gate if (panicstr) 6330Sstevel@tonic-gate return; 6340Sstevel@tonic-gate spin_count++; 635*5834Spt157919 loop_count++; 636*5834Spt157919 637*5834Spt157919 if (ncpus_online == loop_count) { 638*5834Spt157919 backoff = mutex_lock_backoff(0); 639*5834Spt157919 loop_count = 0; 6403914Spm145316 } else { 641*5834Spt157919 backoff = mutex_lock_backoff(backoff); 6420Sstevel@tonic-gate } 643*5834Spt157919 mutex_lock_delay(backoff); 6440Sstevel@tonic-gate } 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate if (spin_count) { 6470Sstevel@tonic-gate LOCKSTAT_RECORD(LS_LOCK_SET_SPIN, lp, spin_count); 6480Sstevel@tonic-gate } 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate LOCKSTAT_RECORD0(LS_LOCK_SET_ACQUIRE, lp); 6510Sstevel@tonic-gate } 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate void 6540Sstevel@tonic-gate lock_set_spl_spin(lock_t *lp, int new_pil, ushort_t *old_pil_addr, int old_pil) 6550Sstevel@tonic-gate { 6560Sstevel@tonic-gate int spin_count = 1; 657*5834Spt157919 int loop_count = 0; 658*5834Spt157919 uint_t backoff = 0; /* current backoff */ 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate if (panicstr) 6610Sstevel@tonic-gate return; 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate if (ncpus == 1) 6640Sstevel@tonic-gate panic("lock_set_spl: %p lock held and only one CPU", lp); 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate ASSERT(new_pil > LOCK_LEVEL); 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate do { 6690Sstevel@tonic-gate splx(old_pil); 6700Sstevel@tonic-gate while (LOCK_HELD(lp)) { 671*5834Spt157919 spin_count++; 672*5834Spt157919 loop_count++; 673*5834Spt157919 6740Sstevel@tonic-gate if (panicstr) { 6750Sstevel@tonic-gate *old_pil_addr = (ushort_t)splr(new_pil); 6760Sstevel@tonic-gate return; 6770Sstevel@tonic-gate } 678*5834Spt157919 if (ncpus_online == loop_count) { 679*5834Spt157919 backoff = mutex_lock_backoff(0); 680*5834Spt157919 loop_count = 0; 6813914Spm145316 } else { 682*5834Spt157919 backoff = mutex_lock_backoff(backoff); 6830Sstevel@tonic-gate } 684*5834Spt157919 mutex_lock_delay(backoff); 6850Sstevel@tonic-gate } 6860Sstevel@tonic-gate old_pil = splr(new_pil); 6870Sstevel@tonic-gate } while (!lock_spin_try(lp)); 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate *old_pil_addr = (ushort_t)old_pil; 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate if (spin_count) { 6920Sstevel@tonic-gate LOCKSTAT_RECORD(LS_LOCK_SET_SPL_SPIN, lp, spin_count); 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate LOCKSTAT_RECORD(LS_LOCK_SET_SPL_ACQUIRE, lp, spin_count); 6960Sstevel@tonic-gate } 697