xref: /freebsd/sys/kern/kern_sx.c (revision 4e5e677bc09b04779ca78ed11e8dcb5e8bca692b)
16281b30aSJason Evans /*
26281b30aSJason Evans  * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.  All rights reserved.
36281b30aSJason Evans  *
46281b30aSJason Evans  * Redistribution and use in source and binary forms, with or without
56281b30aSJason Evans  * modification, are permitted provided that the following conditions
66281b30aSJason Evans  * are met:
76281b30aSJason Evans  * 1. Redistributions of source code must retain the above copyright
86281b30aSJason Evans  *    notice(s), this list of conditions and the following disclaimer as
96281b30aSJason Evans  *    the first lines of this file unmodified other than the possible
106281b30aSJason Evans  *    addition of one or more copyright notices.
116281b30aSJason Evans  * 2. Redistributions in binary form must reproduce the above copyright
126281b30aSJason Evans  *    notice(s), this list of conditions and the following disclaimer in the
136281b30aSJason Evans  *    documentation and/or other materials provided with the distribution.
146281b30aSJason Evans  *
156281b30aSJason Evans  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
166281b30aSJason Evans  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
176281b30aSJason Evans  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
186281b30aSJason Evans  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
196281b30aSJason Evans  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
206281b30aSJason Evans  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
216281b30aSJason Evans  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
226281b30aSJason Evans  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
236281b30aSJason Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
246281b30aSJason Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
256281b30aSJason Evans  * DAMAGE.
266281b30aSJason Evans  *
276281b30aSJason Evans  * $FreeBSD$
286281b30aSJason Evans  */
296281b30aSJason Evans 
306281b30aSJason Evans /*
316281b30aSJason Evans  * Shared/exclusive locks.  This implementation assures deterministic lock
326281b30aSJason Evans  * granting behavior, so that slocks and xlocks are interleaved.
336281b30aSJason Evans  *
346281b30aSJason Evans  * Priority propagation will not generally raise the priority of lock holders,
356281b30aSJason Evans  * so should not be relied upon in combination with sx locks.
366281b30aSJason Evans  */
376281b30aSJason Evans 
386281b30aSJason Evans #include <sys/param.h>
396281b30aSJason Evans #include <sys/systm.h>
406281b30aSJason Evans #include <sys/ktr.h>
416281b30aSJason Evans #include <sys/condvar.h>
4219284646SJohn Baldwin #include <sys/lock.h>
436281b30aSJason Evans #include <sys/mutex.h>
446281b30aSJason Evans #include <sys/sx.h>
456281b30aSJason Evans 
4619284646SJohn Baldwin struct lock_class lock_class_sx = {
4719284646SJohn Baldwin 	"sx",
48b0b7cb50SJohn Baldwin 	LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE
4919284646SJohn Baldwin };
5019284646SJohn Baldwin 
516281b30aSJason Evans void
526281b30aSJason Evans sx_init(struct sx *sx, const char *description)
536281b30aSJason Evans {
5419284646SJohn Baldwin 	struct lock_object *lock;
556281b30aSJason Evans 
5619284646SJohn Baldwin 	lock = &sx->sx_object;
577ada5876SJohn Baldwin 	KASSERT((lock->lo_flags & LO_INITIALIZED) == 0,
587ada5876SJohn Baldwin 	    ("sx lock %s %p already initialized", description, sx));
597ada5876SJohn Baldwin 	bzero(sx, sizeof(*sx));
6019284646SJohn Baldwin 	lock->lo_class = &lock_class_sx;
6119284646SJohn Baldwin 	lock->lo_name = description;
62b0b7cb50SJohn Baldwin 	lock->lo_flags = LO_WITNESS | LO_RECURSABLE | LO_SLEEPABLE |
63b0b7cb50SJohn Baldwin 	    LO_UPGRADABLE;
6419284646SJohn Baldwin 	mtx_init(&sx->sx_lock, "sx backing lock",
6519284646SJohn Baldwin 	    MTX_DEF | MTX_NOWITNESS | MTX_QUIET);
666281b30aSJason Evans 	sx->sx_cnt = 0;
676281b30aSJason Evans 	cv_init(&sx->sx_shrd_cv, description);
686281b30aSJason Evans 	sx->sx_shrd_wcnt = 0;
696281b30aSJason Evans 	cv_init(&sx->sx_excl_cv, description);
706281b30aSJason Evans 	sx->sx_excl_wcnt = 0;
71af761449SBosko Milekic 	sx->sx_xholder = NULL;
7219284646SJohn Baldwin 
7319284646SJohn Baldwin 	LOCK_LOG_INIT(lock, 0);
7419284646SJohn Baldwin 
7519284646SJohn Baldwin 	WITNESS_INIT(lock);
766281b30aSJason Evans }
776281b30aSJason Evans 
786281b30aSJason Evans void
796281b30aSJason Evans sx_destroy(struct sx *sx)
806281b30aSJason Evans {
816281b30aSJason Evans 
8219284646SJohn Baldwin 	LOCK_LOG_DESTROY(&sx->sx_object, 0);
8319284646SJohn Baldwin 
846281b30aSJason Evans 	KASSERT((sx->sx_cnt == 0 && sx->sx_shrd_wcnt == 0 && sx->sx_excl_wcnt ==
8519284646SJohn Baldwin 	    0), ("%s (%s): holders or waiters\n", __FUNCTION__,
8619284646SJohn Baldwin 	    sx->sx_object.lo_name));
876281b30aSJason Evans 
886281b30aSJason Evans 	mtx_destroy(&sx->sx_lock);
896281b30aSJason Evans 	cv_destroy(&sx->sx_shrd_cv);
906281b30aSJason Evans 	cv_destroy(&sx->sx_excl_cv);
9119284646SJohn Baldwin 
9219284646SJohn Baldwin 	WITNESS_DESTROY(&sx->sx_object);
936281b30aSJason Evans }
946281b30aSJason Evans 
956281b30aSJason Evans void
9619284646SJohn Baldwin _sx_slock(struct sx *sx, const char *file, int line)
976281b30aSJason Evans {
986281b30aSJason Evans 
996281b30aSJason Evans 	mtx_lock(&sx->sx_lock);
100b40ce416SJulian Elischer 	KASSERT(sx->sx_xholder != curthread,
1015f36700aSJohn Baldwin 	    ("%s (%s): slock while xlock is held @ %s:%d\n", __FUNCTION__,
1025f36700aSJohn Baldwin 	    sx->sx_object.lo_name, file, line));
1036281b30aSJason Evans 
1046281b30aSJason Evans 	/*
1056281b30aSJason Evans 	 * Loop in case we lose the race for lock acquisition.
1066281b30aSJason Evans 	 */
1076281b30aSJason Evans 	while (sx->sx_cnt < 0) {
1086281b30aSJason Evans 		sx->sx_shrd_wcnt++;
1096281b30aSJason Evans 		cv_wait(&sx->sx_shrd_cv, &sx->sx_lock);
1106281b30aSJason Evans 		sx->sx_shrd_wcnt--;
1116281b30aSJason Evans 	}
1126281b30aSJason Evans 
1136281b30aSJason Evans 	/* Acquire a shared lock. */
1146281b30aSJason Evans 	sx->sx_cnt++;
1156281b30aSJason Evans 
11619284646SJohn Baldwin 	LOCK_LOG_LOCK("SLOCK", &sx->sx_object, 0, 0, file, line);
11719284646SJohn Baldwin 	WITNESS_LOCK(&sx->sx_object, 0, file, line);
11819284646SJohn Baldwin 
1196281b30aSJason Evans 	mtx_unlock(&sx->sx_lock);
1206281b30aSJason Evans }
1216281b30aSJason Evans 
1225f36700aSJohn Baldwin int
1235f36700aSJohn Baldwin _sx_try_slock(struct sx *sx, const char *file, int line)
1245f36700aSJohn Baldwin {
1255f36700aSJohn Baldwin 
1265f36700aSJohn Baldwin 	mtx_lock(&sx->sx_lock);
1275f36700aSJohn Baldwin 	if (sx->sx_cnt >= 0) {
1285f36700aSJohn Baldwin 		sx->sx_cnt++;
1295f36700aSJohn Baldwin 		LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 1, file, line);
1305f36700aSJohn Baldwin 		WITNESS_LOCK(&sx->sx_object, LOP_TRYLOCK, file, line);
1315f36700aSJohn Baldwin 		mtx_unlock(&sx->sx_lock);
1325f36700aSJohn Baldwin 		return (1);
1335f36700aSJohn Baldwin 	} else {
1345f36700aSJohn Baldwin 		LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 0, file, line);
1355f36700aSJohn Baldwin 		mtx_unlock(&sx->sx_lock);
1365f36700aSJohn Baldwin 		return (0);
1375f36700aSJohn Baldwin 	}
1385f36700aSJohn Baldwin }
1395f36700aSJohn Baldwin 
1406281b30aSJason Evans void
14119284646SJohn Baldwin _sx_xlock(struct sx *sx, const char *file, int line)
1426281b30aSJason Evans {
1436281b30aSJason Evans 
1446281b30aSJason Evans 	mtx_lock(&sx->sx_lock);
1456281b30aSJason Evans 
146af761449SBosko Milekic 	/*
147af761449SBosko Milekic 	 * With sx locks, we're absolutely not permitted to recurse on
148af761449SBosko Milekic 	 * xlocks, as it is fatal (deadlock). Normally, recursion is handled
149af761449SBosko Milekic 	 * by WITNESS, but as it is not semantically correct to hold the
150af761449SBosko Milekic 	 * xlock while in here, we consider it API abuse and put it under
151af761449SBosko Milekic 	 * INVARIANTS.
152af761449SBosko Milekic 	 */
153b40ce416SJulian Elischer 	KASSERT(sx->sx_xholder != curthread,
15419284646SJohn Baldwin 	    ("%s (%s): xlock already held @ %s:%d", __FUNCTION__,
15519284646SJohn Baldwin 	    sx->sx_object.lo_name, file, line));
156af761449SBosko Milekic 
1576281b30aSJason Evans 	/* Loop in case we lose the race for lock acquisition. */
1586281b30aSJason Evans 	while (sx->sx_cnt != 0) {
1596281b30aSJason Evans 		sx->sx_excl_wcnt++;
1606281b30aSJason Evans 		cv_wait(&sx->sx_excl_cv, &sx->sx_lock);
1616281b30aSJason Evans 		sx->sx_excl_wcnt--;
1626281b30aSJason Evans 	}
1636281b30aSJason Evans 
164af761449SBosko Milekic 	MPASS(sx->sx_cnt == 0);
165af761449SBosko Milekic 
1666281b30aSJason Evans 	/* Acquire an exclusive lock. */
1676281b30aSJason Evans 	sx->sx_cnt--;
168b40ce416SJulian Elischer 	sx->sx_xholder = curthread;
1696281b30aSJason Evans 
17019284646SJohn Baldwin 	LOCK_LOG_LOCK("XLOCK", &sx->sx_object, 0, 0, file, line);
1712d96f0b1SJohn Baldwin 	WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
17219284646SJohn Baldwin 
1736281b30aSJason Evans 	mtx_unlock(&sx->sx_lock);
1746281b30aSJason Evans }
1756281b30aSJason Evans 
1765f36700aSJohn Baldwin int
1775f36700aSJohn Baldwin _sx_try_xlock(struct sx *sx, const char *file, int line)
1785f36700aSJohn Baldwin {
1795f36700aSJohn Baldwin 
1805f36700aSJohn Baldwin 	mtx_lock(&sx->sx_lock);
1815f36700aSJohn Baldwin 	if (sx->sx_cnt == 0) {
1825f36700aSJohn Baldwin 		sx->sx_cnt--;
183b40ce416SJulian Elischer 		sx->sx_xholder = curthread;
1845f36700aSJohn Baldwin 		LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 1, file, line);
1855f36700aSJohn Baldwin 		WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK, file,
1865f36700aSJohn Baldwin 		    line);
1875f36700aSJohn Baldwin 		mtx_unlock(&sx->sx_lock);
1885f36700aSJohn Baldwin 		return (1);
1895f36700aSJohn Baldwin 	} else {
1905f36700aSJohn Baldwin 		LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 0, file, line);
1915f36700aSJohn Baldwin 		mtx_unlock(&sx->sx_lock);
1925f36700aSJohn Baldwin 		return (0);
1935f36700aSJohn Baldwin 	}
1945f36700aSJohn Baldwin }
1955f36700aSJohn Baldwin 
1966281b30aSJason Evans void
19719284646SJohn Baldwin _sx_sunlock(struct sx *sx, const char *file, int line)
1986281b30aSJason Evans {
1996281b30aSJason Evans 
2004e5e677bSJohn Baldwin 	_sx_assert(sx, SX_SLOCKED, file, line);
2016281b30aSJason Evans 	mtx_lock(&sx->sx_lock);
2026281b30aSJason Evans 
20319284646SJohn Baldwin 	WITNESS_UNLOCK(&sx->sx_object, 0, file, line);
20419284646SJohn Baldwin 
2056281b30aSJason Evans 	/* Release. */
2066281b30aSJason Evans 	sx->sx_cnt--;
2076281b30aSJason Evans 
2086281b30aSJason Evans 	/*
2096281b30aSJason Evans 	 * If we just released the last shared lock, wake any waiters up, giving
2106281b30aSJason Evans 	 * exclusive lockers precedence.  In order to make sure that exclusive
2116281b30aSJason Evans 	 * lockers won't be blocked forever, don't wake shared lock waiters if
2126281b30aSJason Evans 	 * there are exclusive lock waiters.
2136281b30aSJason Evans 	 */
2146281b30aSJason Evans 	if (sx->sx_excl_wcnt > 0) {
2156281b30aSJason Evans 		if (sx->sx_cnt == 0)
2166281b30aSJason Evans 			cv_signal(&sx->sx_excl_cv);
2176281b30aSJason Evans 	} else if (sx->sx_shrd_wcnt > 0)
2186281b30aSJason Evans 		cv_broadcast(&sx->sx_shrd_cv);
2196281b30aSJason Evans 
22019284646SJohn Baldwin 	LOCK_LOG_LOCK("SUNLOCK", &sx->sx_object, 0, 0, file, line);
22119284646SJohn Baldwin 
2226281b30aSJason Evans 	mtx_unlock(&sx->sx_lock);
2236281b30aSJason Evans }
2246281b30aSJason Evans 
2256281b30aSJason Evans void
22619284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line)
2276281b30aSJason Evans {
2286281b30aSJason Evans 
2294e5e677bSJohn Baldwin 	_sx_assert(sx, SX_XLOCKED, file, line);
2306281b30aSJason Evans 	mtx_lock(&sx->sx_lock);
231af761449SBosko Milekic 	MPASS(sx->sx_cnt == -1);
2326281b30aSJason Evans 
2332d96f0b1SJohn Baldwin 	WITNESS_UNLOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
23419284646SJohn Baldwin 
2356281b30aSJason Evans 	/* Release. */
2366281b30aSJason Evans 	sx->sx_cnt++;
237af761449SBosko Milekic 	sx->sx_xholder = NULL;
2386281b30aSJason Evans 
2396281b30aSJason Evans 	/*
2406281b30aSJason Evans 	 * Wake up waiters if there are any.  Give precedence to slock waiters.
2416281b30aSJason Evans 	 */
2426281b30aSJason Evans 	if (sx->sx_shrd_wcnt > 0)
2436281b30aSJason Evans 		cv_broadcast(&sx->sx_shrd_cv);
2446281b30aSJason Evans 	else if (sx->sx_excl_wcnt > 0)
2456281b30aSJason Evans 		cv_signal(&sx->sx_excl_cv);
2466281b30aSJason Evans 
24719284646SJohn Baldwin 	LOCK_LOG_LOCK("XUNLOCK", &sx->sx_object, 0, 0, file, line);
24819284646SJohn Baldwin 
2496281b30aSJason Evans 	mtx_unlock(&sx->sx_lock);
2506281b30aSJason Evans }
251d55229b7SJason Evans 
252d55229b7SJason Evans int
253d55229b7SJason Evans _sx_try_upgrade(struct sx *sx, const char *file, int line)
254d55229b7SJason Evans {
255d55229b7SJason Evans 
2564e5e677bSJohn Baldwin 	_sx_assert(sx, SX_SLOCKED, file, line);
257d55229b7SJason Evans 	mtx_lock(&sx->sx_lock);
258d55229b7SJason Evans 
259d55229b7SJason Evans 	if (sx->sx_cnt == 1) {
260d55229b7SJason Evans 		sx->sx_cnt = -1;
261b40ce416SJulian Elischer 		sx->sx_xholder = curthread;
262d55229b7SJason Evans 
263d55229b7SJason Evans 		LOCK_LOG_TRY("XUPGRADE", &sx->sx_object, 0, 1, file, line);
264b0b7cb50SJohn Baldwin 		WITNESS_UPGRADE(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
265b0b7cb50SJohn Baldwin 		    file, line);
266d55229b7SJason Evans 
267d55229b7SJason Evans 		mtx_unlock(&sx->sx_lock);
268d55229b7SJason Evans 		return (1);
269d55229b7SJason Evans 	} else {
270d55229b7SJason Evans 		LOCK_LOG_TRY("XUPGRADE", &sx->sx_object, 0, 0, file, line);
271d55229b7SJason Evans 		mtx_unlock(&sx->sx_lock);
272d55229b7SJason Evans 		return (0);
273d55229b7SJason Evans 	}
274d55229b7SJason Evans }
275d55229b7SJason Evans 
276d55229b7SJason Evans void
277d55229b7SJason Evans _sx_downgrade(struct sx *sx, const char *file, int line)
278d55229b7SJason Evans {
279d55229b7SJason Evans 
2804e5e677bSJohn Baldwin 	_sx_assert(sx, SX_XLOCKED, file, line);
281d55229b7SJason Evans 	mtx_lock(&sx->sx_lock);
282d55229b7SJason Evans 	MPASS(sx->sx_cnt == -1);
283d55229b7SJason Evans 
284b0b7cb50SJohn Baldwin 	WITNESS_DOWNGRADE(&sx->sx_object, 0, file, line);
285d55229b7SJason Evans 
286d55229b7SJason Evans 	sx->sx_cnt = 1;
287e2870579SJohn Baldwin 	sx->sx_xholder = NULL;
288d55229b7SJason Evans         if (sx->sx_shrd_wcnt > 0)
289d55229b7SJason Evans                 cv_broadcast(&sx->sx_shrd_cv);
290d55229b7SJason Evans 
291d55229b7SJason Evans 	LOCK_LOG_LOCK("XDOWNGRADE", &sx->sx_object, 0, 0, file, line);
292d55229b7SJason Evans 
293d55229b7SJason Evans 	mtx_unlock(&sx->sx_lock);
294d55229b7SJason Evans }
2954e5e677bSJohn Baldwin 
2964e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT
2974e5e677bSJohn Baldwin /*
2984e5e677bSJohn Baldwin  * In the non-WITNESS case, sx_assert() can only detect that at least
2994e5e677bSJohn Baldwin  * *some* thread owns an slock, but it cannot guarantee that *this*
3004e5e677bSJohn Baldwin  * thread owns an slock.
3014e5e677bSJohn Baldwin  */
3024e5e677bSJohn Baldwin void
3034e5e677bSJohn Baldwin _sx_assert(struct sx *sx, int what, const char *file, int line)
3044e5e677bSJohn Baldwin {
3054e5e677bSJohn Baldwin 
3064e5e677bSJohn Baldwin 	switch (what) {
3074e5e677bSJohn Baldwin 	case SX_LOCKED:
3084e5e677bSJohn Baldwin 	case SX_SLOCKED:
3094e5e677bSJohn Baldwin #ifdef WITNESS
3104e5e677bSJohn Baldwin 		witness_assert(&sx->sx_object, what, file, line);
3114e5e677bSJohn Baldwin #else
3124e5e677bSJohn Baldwin 		mtx_lock(&sx->sx_lock);
3134e5e677bSJohn Baldwin 		if (sx->sx_cnt <= 0 &&
3144e5e677bSJohn Baldwin 		    (what == SX_SLOCKED || sx->sx_xholder == curthread))
3154e5e677bSJohn Baldwin 			printf("Lock %s not %slocked @ %s:%d",
3164e5e677bSJohn Baldwin 			    sx->sx_object.lo_name, (what == SX_SLOCKED) ?
3174e5e677bSJohn Baldwin 			    "share " : "", file, line);
3184e5e677bSJohn Baldwin 		mtx_unlock(&sx->sx_lock);
3194e5e677bSJohn Baldwin #endif
3204e5e677bSJohn Baldwin 		break;
3214e5e677bSJohn Baldwin 	case SX_XLOCKED:
3224e5e677bSJohn Baldwin 		mtx_lock(&sx->sx_lock);
3234e5e677bSJohn Baldwin 		if (sx->sx_xholder != curthread)
3244e5e677bSJohn Baldwin 			printf("Lock %s not exclusively locked @ %s:%d",
3254e5e677bSJohn Baldwin 			    sx->sx_object.lo_name, file, line);
3264e5e677bSJohn Baldwin 		mtx_unlock(&sx->sx_lock);
3274e5e677bSJohn Baldwin 		break;
3284e5e677bSJohn Baldwin 	default:
3294e5e677bSJohn Baldwin 		panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
3304e5e677bSJohn Baldwin 		    line);
3314e5e677bSJohn Baldwin 	}
3324e5e677bSJohn Baldwin }
3334e5e677bSJohn Baldwin #endif	/* INVARIANT_SUPPORT */
334