kern_sx.c revision 154484
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: head/sys/kern/kern_sx.c 154484 2006-01-17 16:55:17Z jhb $");
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
54static void	db_show_sx(struct lock_object *lock);
55#endif
56
57struct 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
69void
70sx_sysinit(void *arg)
71{
72	struct sx_args *sargs = arg;
73
74	sx_init(sargs->sa_sx, sargs->sa_desc);
75}
76
77void
78sx_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
92void
93sx_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
107void
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
132	mtx_unlock(sx->sx_lock);
133}
134
135int
136_sx_try_slock(struct sx *sx, const char *file, int line)
137{
138
139	mtx_lock(sx->sx_lock);
140	if (sx->sx_cnt >= 0) {
141		sx->sx_cnt++;
142		LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 1, file, line);
143		WITNESS_LOCK(&sx->sx_object, LOP_TRYLOCK, file, line);
144		mtx_unlock(sx->sx_lock);
145		return (1);
146	} else {
147		LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 0, file, line);
148		mtx_unlock(sx->sx_lock);
149		return (0);
150	}
151}
152
153void
154_sx_xlock(struct sx *sx, const char *file, int line)
155{
156
157	mtx_lock(sx->sx_lock);
158
159	/*
160	 * With sx locks, we're absolutely not permitted to recurse on
161	 * xlocks, as it is fatal (deadlock). Normally, recursion is handled
162	 * by WITNESS, but as it is not semantically correct to hold the
163	 * xlock while in here, we consider it API abuse and put it under
164	 * INVARIANTS.
165	 */
166	KASSERT(sx->sx_xholder != curthread,
167	    ("%s (%s): xlock already held @ %s:%d", __func__,
168	    sx->sx_object.lo_name, file, line));
169	WITNESS_CHECKORDER(&sx->sx_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
170	    line);
171
172	/* Loop in case we lose the race for lock acquisition. */
173	while (sx->sx_cnt != 0) {
174		sx->sx_excl_wcnt++;
175		cv_wait(&sx->sx_excl_cv, sx->sx_lock);
176		sx->sx_excl_wcnt--;
177	}
178
179	MPASS(sx->sx_cnt == 0);
180
181	/* Acquire an exclusive lock. */
182	sx->sx_cnt--;
183	sx->sx_xholder = curthread;
184
185	LOCK_LOG_LOCK("XLOCK", &sx->sx_object, 0, 0, file, line);
186	WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
187
188	mtx_unlock(sx->sx_lock);
189}
190
191int
192_sx_try_xlock(struct sx *sx, const char *file, int line)
193{
194
195	mtx_lock(sx->sx_lock);
196	if (sx->sx_cnt == 0) {
197		sx->sx_cnt--;
198		sx->sx_xholder = curthread;
199		LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 1, file, line);
200		WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK, file,
201		    line);
202		mtx_unlock(sx->sx_lock);
203		return (1);
204	} else {
205		LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 0, file, line);
206		mtx_unlock(sx->sx_lock);
207		return (0);
208	}
209}
210
211void
212_sx_sunlock(struct sx *sx, const char *file, int line)
213{
214
215	_sx_assert(sx, SX_SLOCKED, file, line);
216	mtx_lock(sx->sx_lock);
217
218	WITNESS_UNLOCK(&sx->sx_object, 0, file, line);
219
220	/* Release. */
221	sx->sx_cnt--;
222
223	/*
224	 * If we just released the last shared lock, wake any waiters up, giving
225	 * exclusive lockers precedence.  In order to make sure that exclusive
226	 * lockers won't be blocked forever, don't wake shared lock waiters if
227	 * there are exclusive lock waiters.
228	 */
229	if (sx->sx_excl_wcnt > 0) {
230		if (sx->sx_cnt == 0)
231			cv_signal(&sx->sx_excl_cv);
232	} else if (sx->sx_shrd_wcnt > 0)
233		cv_broadcast(&sx->sx_shrd_cv);
234
235	LOCK_LOG_LOCK("SUNLOCK", &sx->sx_object, 0, 0, file, line);
236
237	mtx_unlock(sx->sx_lock);
238}
239
240void
241_sx_xunlock(struct sx *sx, const char *file, int line)
242{
243
244	_sx_assert(sx, SX_XLOCKED, file, line);
245	mtx_lock(sx->sx_lock);
246	MPASS(sx->sx_cnt == -1);
247
248	WITNESS_UNLOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
249
250	/* Release. */
251	sx->sx_cnt++;
252	sx->sx_xholder = NULL;
253
254	/*
255	 * Wake up waiters if there are any.  Give precedence to slock waiters.
256	 */
257	if (sx->sx_shrd_wcnt > 0)
258		cv_broadcast(&sx->sx_shrd_cv);
259	else if (sx->sx_excl_wcnt > 0)
260		cv_signal(&sx->sx_excl_cv);
261
262	LOCK_LOG_LOCK("XUNLOCK", &sx->sx_object, 0, 0, file, line);
263
264	mtx_unlock(sx->sx_lock);
265}
266
267int
268_sx_try_upgrade(struct sx *sx, const char *file, int line)
269{
270
271	_sx_assert(sx, SX_SLOCKED, file, line);
272	mtx_lock(sx->sx_lock);
273
274	if (sx->sx_cnt == 1) {
275		sx->sx_cnt = -1;
276		sx->sx_xholder = curthread;
277
278		LOCK_LOG_TRY("XUPGRADE", &sx->sx_object, 0, 1, file, line);
279		WITNESS_UPGRADE(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
280		    file, line);
281
282		mtx_unlock(sx->sx_lock);
283		return (1);
284	} else {
285		LOCK_LOG_TRY("XUPGRADE", &sx->sx_object, 0, 0, file, line);
286		mtx_unlock(sx->sx_lock);
287		return (0);
288	}
289}
290
291void
292_sx_downgrade(struct sx *sx, const char *file, int line)
293{
294
295	_sx_assert(sx, SX_XLOCKED, file, line);
296	mtx_lock(sx->sx_lock);
297	MPASS(sx->sx_cnt == -1);
298
299	WITNESS_DOWNGRADE(&sx->sx_object, 0, file, line);
300
301	sx->sx_cnt = 1;
302	sx->sx_xholder = NULL;
303        if (sx->sx_shrd_wcnt > 0)
304                cv_broadcast(&sx->sx_shrd_cv);
305
306	LOCK_LOG_LOCK("XDOWNGRADE", &sx->sx_object, 0, 0, file, line);
307
308	mtx_unlock(sx->sx_lock);
309}
310
311#ifdef INVARIANT_SUPPORT
312#ifndef INVARIANTS
313#undef	_sx_assert
314#endif
315
316/*
317 * In the non-WITNESS case, sx_assert() can only detect that at least
318 * *some* thread owns an slock, but it cannot guarantee that *this*
319 * thread owns an slock.
320 */
321void
322_sx_assert(struct sx *sx, int what, const char *file, int line)
323{
324
325	if (panicstr != NULL)
326		return;
327	switch (what) {
328	case SX_LOCKED:
329	case SX_SLOCKED:
330#ifdef WITNESS
331		witness_assert(&sx->sx_object, what, file, line);
332#else
333		mtx_lock(sx->sx_lock);
334		if (sx->sx_cnt <= 0 &&
335		    (what == SX_SLOCKED || sx->sx_xholder != curthread))
336			panic("Lock %s not %slocked @ %s:%d\n",
337			    sx->sx_object.lo_name, (what == SX_SLOCKED) ?
338			    "share " : "", file, line);
339		mtx_unlock(sx->sx_lock);
340#endif
341		break;
342	case SX_XLOCKED:
343		mtx_lock(sx->sx_lock);
344		if (sx->sx_xholder != curthread)
345			panic("Lock %s not exclusively locked @ %s:%d\n",
346			    sx->sx_object.lo_name, file, line);
347		mtx_unlock(sx->sx_lock);
348		break;
349	case SX_UNLOCKED:
350#ifdef WITNESS
351		witness_assert(&sx->sx_object, what, file, line);
352#else
353		/*
354		 * We are able to check only exclusive lock here,
355		 * we cannot assert that *this* thread owns slock.
356		 */
357		mtx_lock(sx->sx_lock);
358		if (sx->sx_xholder == curthread)
359			panic("Lock %s exclusively locked @ %s:%d\n",
360			    sx->sx_object.lo_name, file, line);
361		mtx_unlock(sx->sx_lock);
362#endif
363		break;
364	default:
365		panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
366		    line);
367	}
368}
369#endif	/* INVARIANT_SUPPORT */
370
371#ifdef DDB
372void
373db_show_sx(struct lock_object *lock)
374{
375	struct thread *td;
376	struct sx *sx;
377
378	sx = (struct sx *)lock;
379
380	db_printf(" state: ");
381	if (sx->sx_cnt < 0) {
382		td = sx->sx_xholder;
383		db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
384		    td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
385	} else if (sx->sx_cnt > 0)
386		db_printf("SLOCK: %d locks\n", sx->sx_cnt);
387	else
388		db_printf("UNLOCKED\n");
389	db_printf(" waiters: %d shared, %d exclusive\n", sx->sx_shrd_wcnt,
390	    sx->sx_excl_wcnt);
391}
392#endif
393