xref: /titanic_44/usr/src/uts/common/os/mutex.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Big Theory Statement for mutual exclusion locking primitives.
30  *
31  * A mutex serializes multiple threads so that only one thread
32  * (the "owner" of the mutex) is active at a time.  See mutex(9F)
33  * for a full description of the interfaces and programming model.
34  * The rest of this comment describes the implementation.
35  *
36  * Mutexes come in two flavors: adaptive and spin.  mutex_init(9F)
37  * determines the type based solely on the iblock cookie (PIL) argument.
38  * PIL > LOCK_LEVEL implies a spin lock; everything else is adaptive.
39  *
40  * Spin mutexes block interrupts and spin until the lock becomes available.
41  * A thread may not sleep, or call any function that might sleep, while
42  * holding a spin mutex.  With few exceptions, spin mutexes should only
43  * be used to synchronize with interrupt handlers.
44  *
45  * Adaptive mutexes (the default type) spin if the owner is running on
46  * another CPU and block otherwise.  This policy is based on the assumption
47  * that mutex hold times are typically short enough that the time spent
48  * spinning is less than the time it takes to block.  If you need mutual
49  * exclusion semantics with long hold times, consider an rwlock(9F) as
50  * RW_WRITER.  Better still, reconsider the algorithm: if it requires
51  * mutual exclusion for long periods of time, it's probably not scalable.
52  *
53  * Adaptive mutexes are overwhelmingly more common than spin mutexes,
54  * so mutex_enter() assumes that the lock is adaptive.  We get away
55  * with this by structuring mutexes so that an attempt to acquire a
56  * spin mutex as adaptive always fails.  When mutex_enter() fails
57  * it punts to mutex_vector_enter(), which does all the hard stuff.
58  *
59  * mutex_vector_enter() first checks the type.  If it's spin mutex,
60  * we just call lock_set_spl() and return.  If it's an adaptive mutex,
61  * we check to see what the owner is doing.  If the owner is running,
62  * we spin until the lock becomes available; if not, we mark the lock
63  * as having waiters and block.
64  *
65  * Blocking on a mutex is surprisingly delicate dance because, for speed,
66  * mutex_exit() doesn't use an atomic instruction.  Thus we have to work
67  * a little harder in the (rarely-executed) blocking path to make sure
68  * we don't block on a mutex that's just been released -- otherwise we
69  * might never be woken up.
70  *
71  * The logic for synchronizing mutex_vector_enter() with mutex_exit()
72  * in the face of preemption and relaxed memory ordering is as follows:
73  *
74  * (1) Preemption in the middle of mutex_exit() must cause mutex_exit()
75  *     to restart.  Each platform must enforce this by checking the
76  *     interrupted PC in the interrupt handler (or on return from trap --
77  *     whichever is more convenient for the platform).  If the PC
78  *     lies within the critical region of mutex_exit(), the interrupt
79  *     handler must reset the PC back to the beginning of mutex_exit().
80  *     The critical region consists of all instructions up to, but not
81  *     including, the store that clears the lock (which, of course,
82  *     must never be executed twice.)
83  *
84  *     This ensures that the owner will always check for waiters after
85  *     resuming from a previous preemption.
86  *
87  * (2) A thread resuming in mutex_exit() does (at least) the following:
88  *
89  *	when resuming:	set CPU_THREAD = owner
90  *			membar #StoreLoad
91  *
92  *	in mutex_exit:	check waiters bit; do wakeup if set
93  *			membar #LoadStore|#StoreStore
94  *			clear owner
95  *			(at this point, other threads may or may not grab
96  *			the lock, and we may or may not reacquire it)
97  *
98  *	when blocking:	membar #StoreStore (due to disp_lock_enter())
99  *			set CPU_THREAD = (possibly) someone else
100  *
101  * (3) A thread blocking in mutex_vector_enter() does the following:
102  *
103  *			set waiters bit
104  *			membar #StoreLoad (via membar_enter())
105  *			check CPU_THREAD for each CPU; abort if owner running
106  *			membar #LoadLoad (via membar_consumer())
107  *			check owner and waiters bit; abort if either changed
108  *			block
109  *
110  * Thus the global memory orderings for (2) and (3) are as follows:
111  *
112  * (2M) mutex_exit() memory order:
113  *
114  *			STORE	CPU_THREAD = owner
115  *			LOAD	waiters bit
116  *			STORE	owner = NULL
117  *			STORE	CPU_THREAD = (possibly) someone else
118  *
119  * (3M) mutex_vector_enter() memory order:
120  *
121  *			STORE	waiters bit = 1
122  *			LOAD	CPU_THREAD for each CPU
123  *			LOAD	owner and waiters bit
124  *
125  * It has been verified by exhaustive simulation that all possible global
126  * memory orderings of (2M) interleaved with (3M) result in correct
127  * behavior.  Moreover, these ordering constraints are minimal: changing
128  * the ordering of anything in (2M) or (3M) breaks the algorithm, creating
129  * windows for missed wakeups.  Note: the possibility that other threads
130  * may grab the lock after the owner drops it can be factored out of the
131  * memory ordering analysis because mutex_vector_enter() won't block
132  * if the lock isn't still owned by the same thread.
133  *
134  * The only requirements of code outside the mutex implementation are
135  * (1) mutex_exit() preemption fixup in interrupt handlers or trap return,
136  * and (2) a membar #StoreLoad after setting CPU_THREAD in resume().
137  * Note: idle threads cannot grab adaptive locks (since they cannot block),
138  * so the membar may be safely omitted when resuming an idle thread.
139  *
140  * When a mutex has waiters, mutex_vector_exit() has several options:
141  *
142  * (1) Choose a waiter and make that thread the owner before waking it;
143  *     this is known as "direct handoff" of ownership.
144  *
145  * (2) Drop the lock and wake one waiter.
146  *
147  * (3) Drop the lock, clear the waiters bit, and wake all waiters.
148  *
149  * In many ways (1) is the cleanest solution, but if a lock is moderately
150  * contended it defeats the adaptive spin logic.  If we make some other
151  * thread the owner, but he's not ONPROC yet, then all other threads on
152  * other cpus that try to get the lock will conclude that the owner is
153  * blocked, so they'll block too.  And so on -- it escalates quickly,
154  * with every thread taking the blocking path rather than the spin path.
155  * Thus, direct handoff is *not* a good idea for adaptive mutexes.
156  *
157  * Option (2) is the next most natural-seeming option, but it has several
158  * annoying properties.  If there's more than one waiter, we must preserve
159  * the waiters bit on an unheld lock.  On cas-capable platforms, where
160  * the waiters bit is part of the lock word, this means that both 0x0
161  * and 0x1 represent unheld locks, so we have to cas against *both*.
162  * Priority inheritance also gets more complicated, because a lock can
163  * have waiters but no owner to whom priority can be willed.  So while
164  * it is possible to make option (2) work, it's surprisingly vile.
165  *
166  * Option (3), the least-intuitive at first glance, is what we actually do.
167  * It has the advantage that because you always wake all waiters, you
168  * never have to preserve the waiters bit.  Waking all waiters seems like
169  * begging for a thundering herd problem, but consider: under option (2),
170  * every thread that grabs and drops the lock will wake one waiter -- so
171  * if the lock is fairly active, all waiters will be awakened very quickly
172  * anyway.  Moreover, this is how adaptive locks are *supposed* to work.
173  * The blocking case is rare; the more common case (by 3-4 orders of
174  * magnitude) is that one or more threads spin waiting to get the lock.
175  * Only direct handoff can prevent the thundering herd problem, but as
176  * mentioned earlier, that would tend to defeat the adaptive spin logic.
177  * In practice, option (3) works well because the blocking case is rare.
178  */
179 
180 /*
181  * delayed lock retry with exponential delay for spin locks
182  *
183  * It is noted above that for both the spin locks and the adaptive locks,
184  * spinning is the dominate mode of operation.  So long as there is only
185  * one thread waiting on a lock, the naive spin loop works very well in
186  * cache based architectures.  The lock data structure is pulled into the
187  * cache of the processor with the waiting/spinning thread and no further
188  * memory traffic is generated until the lock is released.  Unfortunately,
189  * once two or more threads are waiting on a lock, the naive spin has
190  * the property of generating maximum memory traffic from each spinning
191  * thread as the spinning threads contend for the lock data structure.
192  *
193  * By executing a delay loop before retrying a lock, a waiting thread
194  * can reduce its memory traffic by a large factor, depending on the
195  * size of the delay loop.  A large delay loop greatly reduced the memory
196  * traffic, but has the drawback of having a period of time when
197  * no thread is attempting to gain the lock even though several threads
198  * might be waiting.  A small delay loop has the drawback of not
199  * much reduction in memory traffic, but reduces the potential idle time.
200  * The theory of the exponential delay code is to start with a short
201  * delay loop and double the waiting time on each iteration, up to
202  * a preselected maximum.  The BACKOFF_BASE provides the equivalent
203  * of 2 to 3 memory references delay for US-III+ and US-IV architectures.
204  * The BACKOFF_CAP is the equivalent of 50 to 100 memory references of
205  * time (less than 12 microseconds for a 1000 MHz system).
206  *
207  * To determine appropriate BACKOFF_BASE and BACKOFF_CAP values,
208  * studies on US-III+ and US-IV systems using 1 to 66 threads were
209  * done.  A range of possible values were studied.
210  * Performance differences below 10 threads were not large.  For
211  * systems with more threads, substantial increases in total lock
212  * throughput was observed with the given values.  For cases where
213  * more than 20 threads were waiting on the same lock, lock throughput
214  * increased by a factor of 5 or more using the backoff algorithm.
215  */
216 
217 #include <sys/param.h>
218 #include <sys/time.h>
219 #include <sys/cpuvar.h>
220 #include <sys/thread.h>
221 #include <sys/debug.h>
222 #include <sys/cmn_err.h>
223 #include <sys/sobject.h>
224 #include <sys/turnstile.h>
225 #include <sys/systm.h>
226 #include <sys/mutex_impl.h>
227 #include <sys/spl.h>
228 #include <sys/lockstat.h>
229 #include <sys/atomic.h>
230 #include <sys/cpu.h>
231 #include <sys/stack.h>
232 
233 #define	BACKOFF_BASE	50
234 #define	BACKOFF_CAP 	1600
235 
236 /*
237  * The sobj_ops vector exports a set of functions needed when a thread
238  * is asleep on a synchronization object of this type.
239  */
240 static sobj_ops_t mutex_sobj_ops = {
241 	SOBJ_MUTEX, mutex_owner, turnstile_stay_asleep, turnstile_change_pri
242 };
243 
244 /*
245  * If the system panics on a mutex, save the address of the offending
246  * mutex in panic_mutex_addr, and save the contents in panic_mutex.
247  */
248 static mutex_impl_t panic_mutex;
249 static mutex_impl_t *panic_mutex_addr;
250 
251 static void
252 mutex_panic(char *msg, mutex_impl_t *lp)
253 {
254 	if (panicstr)
255 		return;
256 
257 	if (casptr(&panic_mutex_addr, NULL, lp) == NULL)
258 		panic_mutex = *lp;
259 
260 	panic("%s, lp=%p owner=%p thread=%p",
261 	    msg, lp, MUTEX_OWNER(&panic_mutex), curthread);
262 }
263 
264 /*
265  * mutex_vector_enter() is called from the assembly mutex_enter() routine
266  * if the lock is held or is not of type MUTEX_ADAPTIVE.
267  */
268 void
269 mutex_vector_enter(mutex_impl_t *lp)
270 {
271 	kthread_id_t	owner;
272 	hrtime_t	sleep_time = 0;	/* how long we slept */
273 	uint_t		spin_count = 0;	/* how many times we spun */
274 	cpu_t 		*cpup, *last_cpu;
275 	extern cpu_t	*cpu_list;
276 	turnstile_t	*ts;
277 	volatile mutex_impl_t *vlp = (volatile mutex_impl_t *)lp;
278 	int		backoff;	/* current backoff */
279 	int		backctr;	/* ctr for backoff */
280 	int		sleep_count = 0;
281 
282 	ASSERT_STACK_ALIGNED();
283 
284 	if (MUTEX_TYPE_SPIN(lp)) {
285 		lock_set_spl(&lp->m_spin.m_spinlock, lp->m_spin.m_minspl,
286 		    &lp->m_spin.m_oldspl);
287 		return;
288 	}
289 
290 	if (!MUTEX_TYPE_ADAPTIVE(lp)) {
291 		mutex_panic("mutex_enter: bad mutex", lp);
292 		return;
293 	}
294 
295 	/*
296 	 * Adaptive mutexes must not be acquired from above LOCK_LEVEL.
297 	 * We can migrate after loading CPU but before checking CPU_ON_INTR,
298 	 * so we must verify by disabling preemption and loading CPU again.
299 	 */
300 	cpup = CPU;
301 	if (CPU_ON_INTR(cpup) && !panicstr) {
302 		kpreempt_disable();
303 		if (CPU_ON_INTR(CPU))
304 			mutex_panic("mutex_enter: adaptive at high PIL", lp);
305 		kpreempt_enable();
306 	}
307 
308 	CPU_STATS_ADDQ(cpup, sys, mutex_adenters, 1);
309 
310 	backoff = BACKOFF_BASE;
311 
312 	for (;;) {
313 spin:
314 		spin_count++;
315 		/*
316 		 * Add an exponential backoff delay before trying again
317 		 * to touch the mutex data structure.
318 		 * the spin_count test and call to nulldev are to prevent
319 		 * the compiler optimizer from eliminating the delay loop.
320 		 */
321 		for (backctr = backoff; backctr; backctr--) {
322 			if (!spin_count) (void) nulldev();
323 		};    /* delay */
324 		backoff = backoff << 1;			/* double it */
325 		if (backoff > BACKOFF_CAP) {
326 			backoff = BACKOFF_CAP;
327 		}
328 
329 		SMT_PAUSE();
330 
331 		if (panicstr)
332 			return;
333 
334 		if ((owner = MUTEX_OWNER(vlp)) == NULL) {
335 			if (mutex_adaptive_tryenter(lp))
336 				break;
337 			continue;
338 		}
339 
340 		if (owner == curthread)
341 			mutex_panic("recursive mutex_enter", lp);
342 
343 		/*
344 		 * If lock is held but owner is not yet set, spin.
345 		 * (Only relevant for platforms that don't have cas.)
346 		 */
347 		if (owner == MUTEX_NO_OWNER)
348 			continue;
349 
350 		/*
351 		 * When searching the other CPUs, start with the one where
352 		 * we last saw the owner thread.  If owner is running, spin.
353 		 *
354 		 * We must disable preemption at this point to guarantee
355 		 * that the list doesn't change while we traverse it
356 		 * without the cpu_lock mutex.  While preemption is
357 		 * disabled, we must revalidate our cached cpu pointer.
358 		 */
359 		kpreempt_disable();
360 		if (cpup->cpu_next == NULL)
361 			cpup = cpu_list;
362 		last_cpu = cpup;	/* mark end of search */
363 		do {
364 			if (cpup->cpu_thread == owner) {
365 				kpreempt_enable();
366 				goto spin;
367 			}
368 		} while ((cpup = cpup->cpu_next) != last_cpu);
369 		kpreempt_enable();
370 
371 		/*
372 		 * The owner appears not to be running, so block.
373 		 * See the Big Theory Statement for memory ordering issues.
374 		 */
375 		ts = turnstile_lookup(lp);
376 		MUTEX_SET_WAITERS(lp);
377 		membar_enter();
378 
379 		/*
380 		 * Recheck whether owner is running after waiters bit hits
381 		 * global visibility (above).  If owner is running, spin.
382 		 *
383 		 * Since we are at ipl DISP_LEVEL, kernel preemption is
384 		 * disabled, however we still need to revalidate our cached
385 		 * cpu pointer to make sure the cpu hasn't been deleted.
386 		 */
387 		if (cpup->cpu_next == NULL)
388 			last_cpu = cpup = cpu_list;
389 		do {
390 			if (cpup->cpu_thread == owner) {
391 				turnstile_exit(lp);
392 				goto spin;
393 			}
394 		} while ((cpup = cpup->cpu_next) != last_cpu);
395 		membar_consumer();
396 
397 		/*
398 		 * If owner and waiters bit are unchanged, block.
399 		 */
400 		if (MUTEX_OWNER(vlp) == owner && MUTEX_HAS_WAITERS(vlp)) {
401 			sleep_time -= gethrtime();
402 			(void) turnstile_block(ts, TS_WRITER_Q, lp,
403 			    &mutex_sobj_ops, NULL, NULL);
404 			sleep_time += gethrtime();
405 			sleep_count++;
406 		} else {
407 			turnstile_exit(lp);
408 		}
409 	}
410 
411 	ASSERT(MUTEX_OWNER(lp) == curthread);
412 
413 	if (sleep_time != 0) {
414 		/*
415 		 * Note, sleep time is the sum of all the sleeping we
416 		 * did.
417 		 */
418 		LOCKSTAT_RECORD(LS_MUTEX_ENTER_BLOCK, lp, sleep_time);
419 	}
420 
421 	/*
422 	 * We do not count a sleep as a spin.
423 	 */
424 	if (spin_count > sleep_count)
425 		LOCKSTAT_RECORD(LS_MUTEX_ENTER_SPIN, lp,
426 		    spin_count - sleep_count);
427 
428 	LOCKSTAT_RECORD0(LS_MUTEX_ENTER_ACQUIRE, lp);
429 }
430 
431 /*
432  * mutex_vector_tryenter() is called from the assembly mutex_tryenter()
433  * routine if the lock is held or is not of type MUTEX_ADAPTIVE.
434  */
435 int
436 mutex_vector_tryenter(mutex_impl_t *lp)
437 {
438 	int s;
439 
440 	if (MUTEX_TYPE_ADAPTIVE(lp))
441 		return (0);		/* we already tried in assembly */
442 
443 	if (!MUTEX_TYPE_SPIN(lp)) {
444 		mutex_panic("mutex_tryenter: bad mutex", lp);
445 		return (0);
446 	}
447 
448 	s = splr(lp->m_spin.m_minspl);
449 	if (lock_try(&lp->m_spin.m_spinlock)) {
450 		lp->m_spin.m_oldspl = (ushort_t)s;
451 		return (1);
452 	}
453 	splx(s);
454 	return (0);
455 }
456 
457 /*
458  * mutex_vector_exit() is called from mutex_exit() if the lock is not
459  * adaptive, has waiters, or is not owned by the current thread (panic).
460  */
461 void
462 mutex_vector_exit(mutex_impl_t *lp)
463 {
464 	turnstile_t *ts;
465 
466 	if (MUTEX_TYPE_SPIN(lp)) {
467 		lock_clear_splx(&lp->m_spin.m_spinlock, lp->m_spin.m_oldspl);
468 		return;
469 	}
470 
471 	if (MUTEX_OWNER(lp) != curthread) {
472 		mutex_panic("mutex_exit: not owner", lp);
473 		return;
474 	}
475 
476 	ts = turnstile_lookup(lp);
477 	MUTEX_CLEAR_LOCK_AND_WAITERS(lp);
478 	if (ts == NULL)
479 		turnstile_exit(lp);
480 	else
481 		turnstile_wakeup(ts, TS_WRITER_Q, ts->ts_waiters, NULL);
482 	LOCKSTAT_RECORD0(LS_MUTEX_EXIT_RELEASE, lp);
483 }
484 
485 int
486 mutex_owned(kmutex_t *mp)
487 {
488 	mutex_impl_t *lp = (mutex_impl_t *)mp;
489 
490 	if (panicstr)
491 		return (1);
492 
493 	if (MUTEX_TYPE_ADAPTIVE(lp))
494 		return (MUTEX_OWNER(lp) == curthread);
495 	return (LOCK_HELD(&lp->m_spin.m_spinlock));
496 }
497 
498 kthread_t *
499 mutex_owner(kmutex_t *mp)
500 {
501 	mutex_impl_t *lp = (mutex_impl_t *)mp;
502 	kthread_id_t t;
503 
504 	if (MUTEX_TYPE_ADAPTIVE(lp) && (t = MUTEX_OWNER(lp)) != MUTEX_NO_OWNER)
505 		return (t);
506 	return (NULL);
507 }
508 
509 /*
510  * The iblock cookie 'ibc' is the spl level associated with the lock;
511  * this alone determines whether the lock will be ADAPTIVE or SPIN.
512  *
513  * Adaptive mutexes created in zeroed memory do not need to call
514  * mutex_init() as their allocation in this fashion guarantees
515  * their initialization.
516  *   eg adaptive mutexes created as static within the BSS or allocated
517  *      by kmem_zalloc().
518  */
519 /* ARGSUSED */
520 void
521 mutex_init(kmutex_t *mp, char *name, kmutex_type_t type, void *ibc)
522 {
523 	mutex_impl_t *lp = (mutex_impl_t *)mp;
524 
525 	ASSERT(ibc < (void *)KERNELBASE);	/* see 1215173 */
526 
527 	if ((intptr_t)ibc > ipltospl(LOCK_LEVEL) && ibc < (void *)KERNELBASE) {
528 		ASSERT(type != MUTEX_ADAPTIVE && type != MUTEX_DEFAULT);
529 		MUTEX_SET_TYPE(lp, MUTEX_SPIN);
530 		LOCK_INIT_CLEAR(&lp->m_spin.m_spinlock);
531 		LOCK_INIT_HELD(&lp->m_spin.m_dummylock);
532 		lp->m_spin.m_minspl = (int)(intptr_t)ibc;
533 	} else {
534 		ASSERT(type != MUTEX_SPIN);
535 		MUTEX_SET_TYPE(lp, MUTEX_ADAPTIVE);
536 		MUTEX_CLEAR_LOCK_AND_WAITERS(lp);
537 	}
538 }
539 
540 void
541 mutex_destroy(kmutex_t *mp)
542 {
543 	mutex_impl_t *lp = (mutex_impl_t *)mp;
544 
545 	if (lp->m_owner == 0 && !MUTEX_HAS_WAITERS(lp)) {
546 		MUTEX_DESTROY(lp);
547 	} else if (MUTEX_TYPE_SPIN(lp)) {
548 		LOCKSTAT_RECORD0(LS_MUTEX_DESTROY_RELEASE, lp);
549 		MUTEX_DESTROY(lp);
550 	} else if (MUTEX_TYPE_ADAPTIVE(lp)) {
551 		LOCKSTAT_RECORD0(LS_MUTEX_DESTROY_RELEASE, lp);
552 		if (MUTEX_OWNER(lp) != curthread)
553 			mutex_panic("mutex_destroy: not owner", lp);
554 		if (MUTEX_HAS_WAITERS(lp)) {
555 			turnstile_t *ts = turnstile_lookup(lp);
556 			turnstile_exit(lp);
557 			if (ts != NULL)
558 				mutex_panic("mutex_destroy: has waiters", lp);
559 		}
560 		MUTEX_DESTROY(lp);
561 	} else {
562 		mutex_panic("mutex_destroy: bad mutex", lp);
563 	}
564 }
565 
566 /*
567  * Simple C support for the cases where spin locks miss on the first try.
568  */
569 void
570 lock_set_spin(lock_t *lp)
571 {
572 	int spin_count = 1;
573 	int backoff;	/* current backoff */
574 	int backctr;	/* ctr for backoff */
575 
576 	if (panicstr)
577 		return;
578 
579 	if (ncpus == 1)
580 		panic("lock_set: %p lock held and only one CPU", lp);
581 
582 	backoff = BACKOFF_BASE;
583 	while (LOCK_HELD(lp) || !lock_spin_try(lp)) {
584 		if (panicstr)
585 			return;
586 		spin_count++;
587 		/*
588 		 * Add an exponential backoff delay before trying again
589 		 * to touch the mutex data structure.
590 		 * the spin_count test and call to nulldev are to prevent
591 		 * the compiler optimizer from eliminating the delay loop.
592 		 */
593 		for (backctr = backoff; backctr; backctr--) {	/* delay */
594 			if (!spin_count) (void) nulldev();
595 		}
596 
597 		backoff = backoff << 1;		/* double it */
598 		if (backoff > BACKOFF_CAP) {
599 			backoff = BACKOFF_CAP;
600 		}
601 		SMT_PAUSE();
602 	}
603 
604 	if (spin_count) {
605 		LOCKSTAT_RECORD(LS_LOCK_SET_SPIN, lp, spin_count);
606 	}
607 
608 	LOCKSTAT_RECORD0(LS_LOCK_SET_ACQUIRE, lp);
609 }
610 
611 void
612 lock_set_spl_spin(lock_t *lp, int new_pil, ushort_t *old_pil_addr, int old_pil)
613 {
614 	int spin_count = 1;
615 	int backoff;	/* current backoff */
616 	int backctr;	/* ctr for backoff */
617 
618 	if (panicstr)
619 		return;
620 
621 	if (ncpus == 1)
622 		panic("lock_set_spl: %p lock held and only one CPU", lp);
623 
624 	ASSERT(new_pil > LOCK_LEVEL);
625 
626 	backoff = BACKOFF_BASE;
627 	do {
628 		splx(old_pil);
629 		while (LOCK_HELD(lp)) {
630 			if (panicstr) {
631 				*old_pil_addr = (ushort_t)splr(new_pil);
632 				return;
633 			}
634 			spin_count++;
635 			/*
636 			 * Add an exponential backoff delay before trying again
637 			 * to touch the mutex data structure.
638 			 * spin_count test and call to nulldev are to prevent
639 			 * compiler optimizer from eliminating the delay loop.
640 			 */
641 			for (backctr = backoff; backctr; backctr--) {
642 				if (!spin_count) (void) nulldev();
643 			}
644 			backoff = backoff << 1;		/* double it */
645 			if (backoff > BACKOFF_CAP) {
646 				backoff = BACKOFF_CAP;
647 			}
648 
649 			SMT_PAUSE();
650 		}
651 		old_pil = splr(new_pil);
652 	} while (!lock_spin_try(lp));
653 
654 	*old_pil_addr = (ushort_t)old_pil;
655 
656 	if (spin_count) {
657 		LOCKSTAT_RECORD(LS_LOCK_SET_SPL_SPIN, lp, spin_count);
658 	}
659 
660 	LOCKSTAT_RECORD(LS_LOCK_SET_SPL_ACQUIRE, lp, spin_count);
661 }
662