xref: /freebsd/sys/kern/kern_sx.c (revision e4e9813eb92cd7c4d4b819a8fbed5cbd3d92f5d8)
1 /*-
2  * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice(s), this list of conditions and the following disclaimer as
9  *    the first lines of this file unmodified other than the possible
10  *    addition of one or more copyright notices.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice(s), this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25  * DAMAGE.
26  */
27 
28 /*
29  * Shared/exclusive locks.  This implementation assures deterministic lock
30  * granting behavior, so that slocks and xlocks are interleaved.
31  *
32  * Priority propagation will not generally raise the priority of lock holders,
33  * so should not be relied upon in combination with sx locks.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include "opt_ddb.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/ktr.h>
44 #include <sys/linker_set.h>
45 #include <sys/condvar.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/proc.h>
49 #include <sys/sx.h>
50 
51 #include <ddb/ddb.h>
52 
53 #ifdef DDB
54 static void	db_show_sx(struct lock_object *lock);
55 #endif
56 
57 struct lock_class lock_class_sx = {
58 	"sx",
59 	LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
60 #ifdef DDB
61 	db_show_sx
62 #endif
63 };
64 
65 #ifndef INVARIANTS
66 #define	_sx_assert(sx, what, file, line)
67 #endif
68 
69 void
70 sx_sysinit(void *arg)
71 {
72 	struct sx_args *sargs = arg;
73 
74 	sx_init(sargs->sa_sx, sargs->sa_desc);
75 }
76 
77 void
78 sx_init(struct sx *sx, const char *description)
79 {
80 
81 	sx->sx_lock = mtx_pool_find(mtxpool_lockbuilder, sx);
82 	sx->sx_cnt = 0;
83 	cv_init(&sx->sx_shrd_cv, description);
84 	sx->sx_shrd_wcnt = 0;
85 	cv_init(&sx->sx_excl_cv, description);
86 	sx->sx_excl_wcnt = 0;
87 	sx->sx_xholder = NULL;
88 	lock_init(&sx->sx_object, &lock_class_sx, description, NULL,
89 	    LO_WITNESS | LO_RECURSABLE | LO_SLEEPABLE | LO_UPGRADABLE);
90 }
91 
92 void
93 sx_destroy(struct sx *sx)
94 {
95 
96 	KASSERT((sx->sx_cnt == 0 && sx->sx_shrd_wcnt == 0 && sx->sx_excl_wcnt ==
97 	    0), ("%s (%s): holders or waiters\n", __func__,
98 	    sx->sx_object.lo_name));
99 
100 	sx->sx_lock = NULL;
101 	cv_destroy(&sx->sx_shrd_cv);
102 	cv_destroy(&sx->sx_excl_cv);
103 
104 	lock_destroy(&sx->sx_object);
105 }
106 
107 void
108 _sx_slock(struct sx *sx, const char *file, int line)
109 {
110 
111 	mtx_lock(sx->sx_lock);
112 	KASSERT(sx->sx_xholder != curthread,
113 	    ("%s (%s): slock while xlock is held @ %s:%d\n", __func__,
114 	    sx->sx_object.lo_name, file, line));
115 	WITNESS_CHECKORDER(&sx->sx_object, LOP_NEWORDER, file, line);
116 
117 	/*
118 	 * Loop in case we lose the race for lock acquisition.
119 	 */
120 	while (sx->sx_cnt < 0) {
121 		sx->sx_shrd_wcnt++;
122 		cv_wait(&sx->sx_shrd_cv, sx->sx_lock);
123 		sx->sx_shrd_wcnt--;
124 	}
125 
126 	/* Acquire a shared lock. */
127 	sx->sx_cnt++;
128 
129 	LOCK_LOG_LOCK("SLOCK", &sx->sx_object, 0, 0, file, line);
130 	WITNESS_LOCK(&sx->sx_object, 0, file, line);
131 	curthread->td_locks++;
132 
133 	mtx_unlock(sx->sx_lock);
134 }
135 
136 int
137 _sx_try_slock(struct sx *sx, const char *file, int line)
138 {
139 
140 	mtx_lock(sx->sx_lock);
141 	if (sx->sx_cnt >= 0) {
142 		sx->sx_cnt++;
143 		LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 1, file, line);
144 		WITNESS_LOCK(&sx->sx_object, LOP_TRYLOCK, file, line);
145 		curthread->td_locks++;
146 		mtx_unlock(sx->sx_lock);
147 		return (1);
148 	} else {
149 		LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 0, file, line);
150 		mtx_unlock(sx->sx_lock);
151 		return (0);
152 	}
153 }
154 
155 void
156 _sx_xlock(struct sx *sx, const char *file, int line)
157 {
158 
159 	mtx_lock(sx->sx_lock);
160 
161 	/*
162 	 * With sx locks, we're absolutely not permitted to recurse on
163 	 * xlocks, as it is fatal (deadlock). Normally, recursion is handled
164 	 * by WITNESS, but as it is not semantically correct to hold the
165 	 * xlock while in here, we consider it API abuse and put it under
166 	 * INVARIANTS.
167 	 */
168 	KASSERT(sx->sx_xholder != curthread,
169 	    ("%s (%s): xlock already held @ %s:%d", __func__,
170 	    sx->sx_object.lo_name, file, line));
171 	WITNESS_CHECKORDER(&sx->sx_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
172 	    line);
173 
174 	/* Loop in case we lose the race for lock acquisition. */
175 	while (sx->sx_cnt != 0) {
176 		sx->sx_excl_wcnt++;
177 		cv_wait(&sx->sx_excl_cv, sx->sx_lock);
178 		sx->sx_excl_wcnt--;
179 	}
180 
181 	MPASS(sx->sx_cnt == 0);
182 
183 	/* Acquire an exclusive lock. */
184 	sx->sx_cnt--;
185 	sx->sx_xholder = curthread;
186 
187 	LOCK_LOG_LOCK("XLOCK", &sx->sx_object, 0, 0, file, line);
188 	WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
189 	curthread->td_locks++;
190 
191 	mtx_unlock(sx->sx_lock);
192 }
193 
194 int
195 _sx_try_xlock(struct sx *sx, const char *file, int line)
196 {
197 
198 	mtx_lock(sx->sx_lock);
199 	if (sx->sx_cnt == 0) {
200 		sx->sx_cnt--;
201 		sx->sx_xholder = curthread;
202 		LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 1, file, line);
203 		WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK, file,
204 		    line);
205 		curthread->td_locks++;
206 		mtx_unlock(sx->sx_lock);
207 		return (1);
208 	} else {
209 		LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 0, file, line);
210 		mtx_unlock(sx->sx_lock);
211 		return (0);
212 	}
213 }
214 
215 void
216 _sx_sunlock(struct sx *sx, const char *file, int line)
217 {
218 
219 	_sx_assert(sx, SX_SLOCKED, file, line);
220 	mtx_lock(sx->sx_lock);
221 
222 	curthread->td_locks--;
223 	WITNESS_UNLOCK(&sx->sx_object, 0, file, line);
224 
225 	/* Release. */
226 	sx->sx_cnt--;
227 
228 	/*
229 	 * If we just released the last shared lock, wake any waiters up, giving
230 	 * exclusive lockers precedence.  In order to make sure that exclusive
231 	 * lockers won't be blocked forever, don't wake shared lock waiters if
232 	 * there are exclusive lock waiters.
233 	 */
234 	if (sx->sx_excl_wcnt > 0) {
235 		if (sx->sx_cnt == 0)
236 			cv_signal(&sx->sx_excl_cv);
237 	} else if (sx->sx_shrd_wcnt > 0)
238 		cv_broadcast(&sx->sx_shrd_cv);
239 
240 	LOCK_LOG_LOCK("SUNLOCK", &sx->sx_object, 0, 0, file, line);
241 
242 	mtx_unlock(sx->sx_lock);
243 }
244 
245 void
246 _sx_xunlock(struct sx *sx, const char *file, int line)
247 {
248 
249 	_sx_assert(sx, SX_XLOCKED, file, line);
250 	mtx_lock(sx->sx_lock);
251 	MPASS(sx->sx_cnt == -1);
252 
253 	curthread->td_locks--;
254 	WITNESS_UNLOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
255 
256 	/* Release. */
257 	sx->sx_cnt++;
258 	sx->sx_xholder = NULL;
259 
260 	/*
261 	 * Wake up waiters if there are any.  Give precedence to slock waiters.
262 	 */
263 	if (sx->sx_shrd_wcnt > 0)
264 		cv_broadcast(&sx->sx_shrd_cv);
265 	else if (sx->sx_excl_wcnt > 0)
266 		cv_signal(&sx->sx_excl_cv);
267 
268 	LOCK_LOG_LOCK("XUNLOCK", &sx->sx_object, 0, 0, file, line);
269 
270 	mtx_unlock(sx->sx_lock);
271 }
272 
273 int
274 _sx_try_upgrade(struct sx *sx, const char *file, int line)
275 {
276 
277 	_sx_assert(sx, SX_SLOCKED, file, line);
278 	mtx_lock(sx->sx_lock);
279 
280 	if (sx->sx_cnt == 1) {
281 		sx->sx_cnt = -1;
282 		sx->sx_xholder = curthread;
283 
284 		LOCK_LOG_TRY("XUPGRADE", &sx->sx_object, 0, 1, file, line);
285 		WITNESS_UPGRADE(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
286 		    file, line);
287 
288 		mtx_unlock(sx->sx_lock);
289 		return (1);
290 	} else {
291 		LOCK_LOG_TRY("XUPGRADE", &sx->sx_object, 0, 0, file, line);
292 		mtx_unlock(sx->sx_lock);
293 		return (0);
294 	}
295 }
296 
297 void
298 _sx_downgrade(struct sx *sx, const char *file, int line)
299 {
300 
301 	_sx_assert(sx, SX_XLOCKED, file, line);
302 	mtx_lock(sx->sx_lock);
303 	MPASS(sx->sx_cnt == -1);
304 
305 	WITNESS_DOWNGRADE(&sx->sx_object, 0, file, line);
306 
307 	sx->sx_cnt = 1;
308 	sx->sx_xholder = NULL;
309         if (sx->sx_shrd_wcnt > 0)
310                 cv_broadcast(&sx->sx_shrd_cv);
311 
312 	LOCK_LOG_LOCK("XDOWNGRADE", &sx->sx_object, 0, 0, file, line);
313 
314 	mtx_unlock(sx->sx_lock);
315 }
316 
317 #ifdef INVARIANT_SUPPORT
318 #ifndef INVARIANTS
319 #undef	_sx_assert
320 #endif
321 
322 /*
323  * In the non-WITNESS case, sx_assert() can only detect that at least
324  * *some* thread owns an slock, but it cannot guarantee that *this*
325  * thread owns an slock.
326  */
327 void
328 _sx_assert(struct sx *sx, int what, const char *file, int line)
329 {
330 
331 	if (panicstr != NULL)
332 		return;
333 	switch (what) {
334 	case SX_LOCKED:
335 	case SX_SLOCKED:
336 #ifdef WITNESS
337 		witness_assert(&sx->sx_object, what, file, line);
338 #else
339 		mtx_lock(sx->sx_lock);
340 		if (sx->sx_cnt <= 0 &&
341 		    (what == SX_SLOCKED || sx->sx_xholder != curthread))
342 			panic("Lock %s not %slocked @ %s:%d\n",
343 			    sx->sx_object.lo_name, (what == SX_SLOCKED) ?
344 			    "share " : "", file, line);
345 		mtx_unlock(sx->sx_lock);
346 #endif
347 		break;
348 	case SX_XLOCKED:
349 		mtx_lock(sx->sx_lock);
350 		if (sx->sx_xholder != curthread)
351 			panic("Lock %s not exclusively locked @ %s:%d\n",
352 			    sx->sx_object.lo_name, file, line);
353 		mtx_unlock(sx->sx_lock);
354 		break;
355 	case SX_UNLOCKED:
356 #ifdef WITNESS
357 		witness_assert(&sx->sx_object, what, file, line);
358 #else
359 		/*
360 		 * We are able to check only exclusive lock here,
361 		 * we cannot assert that *this* thread owns slock.
362 		 */
363 		mtx_lock(sx->sx_lock);
364 		if (sx->sx_xholder == curthread)
365 			panic("Lock %s exclusively locked @ %s:%d\n",
366 			    sx->sx_object.lo_name, file, line);
367 		mtx_unlock(sx->sx_lock);
368 #endif
369 		break;
370 	default:
371 		panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
372 		    line);
373 	}
374 }
375 #endif	/* INVARIANT_SUPPORT */
376 
377 #ifdef DDB
378 void
379 db_show_sx(struct lock_object *lock)
380 {
381 	struct thread *td;
382 	struct sx *sx;
383 
384 	sx = (struct sx *)lock;
385 
386 	db_printf(" state: ");
387 	if (sx->sx_cnt < 0) {
388 		td = sx->sx_xholder;
389 		db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
390 		    td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
391 	} else if (sx->sx_cnt > 0)
392 		db_printf("SLOCK: %d locks\n", sx->sx_cnt);
393 	else
394 		db_printf("UNLOCKED\n");
395 	db_printf(" waiters: %d shared, %d exclusive\n", sx->sx_shrd_wcnt,
396 	    sx->sx_excl_wcnt);
397 }
398 #endif
399