summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/rtems/rtems-kernel-lockmgr.c
blob: 518f68314ba15775e89a0ed5cb770448b0e5a3f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/**
 * @file
 *
 * @ingroup rtems_bsd_rtems
 *
 * @brief TODO.
 */

/*
 * Copyright (c) 2020 Chris Johns.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <machine/rtems-bsd-kernel-space.h>
#include <machine/rtems-bsd-muteximpl.h>
#include <machine/rtems-bsd-thread.h>

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/lock.h>
#include <sys/lockmgr.h>
#include <sys/mutex.h>
#include <sys/proc.h>

#ifdef DEBUG_LOCKS
#define STACK_PRINT(lk) printf("caller: %p\n", (lk)->lk_stack)
#define STACK_SAVE(lk) (lk)->lk_stack = __builtin_return_address(0)
#define STACK_ZERO(lk) (lk)->lk_stack = NULL
#else
#define STACK_PRINT(lk)
#define STACK_SAVE(lk)
#define STACK_ZERO(lk)
#endif

static void assert_lockmgr(const struct lock_object *lock, int how);
static void lock_lockmgr(struct lock_object *lock, uintptr_t how);
static uintptr_t unlock_lockmgr(struct lock_object *lock);

#define lockmgr_xlocked(lk)    \
	rtems_bsd_mutex_owned( \
	    RTEMS_DECONST(struct lock_object *, &lk->lock_object))
#define lockmgr_disowned(lk)    \
	!rtems_bsd_mutex_owned( \
	    RTEMS_DECONST(struct lock_object *, &lk->lock_object))

struct lock_class lock_class_lockmgr = {
	.lc_name = "lockmgr",
	.lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
	.lc_assert = assert_lockmgr,
#ifdef DDB
	.lc_ddb_show = db_show_lockmgr,
#endif
	.lc_lock = lock_lockmgr,
	.lc_unlock = unlock_lockmgr,
#ifdef KDTRACE_HOOKS
	.lc_owner = owner_lockmgr,
#endif
};

static void
assert_lockmgr(const struct lock_object *lock, int what)
{
	panic("lockmgr locks do not support assertions");
}

static void
lock_lockmgr(struct lock_object *lock, uintptr_t how)
{
	panic("lockmgr locks do not support sleep interlocking : lock");
}

static uintptr_t
unlock_lockmgr(struct lock_object *lock)
{
	panic("lockmgr locks do not support sleep interlocking: unlock");
}

static struct thread *
lockmgr_xholder(const struct lock *lk)
{
	uintptr_t x;
	x = lk->lk_lock;
	if ((x & LK_SHARE))
		return NULL;
	return rtems_bsd_get_thread(lk->lock_object.lo_mtx.queue.Queue.owner);
}

static void
lockmgr_exit(u_int flags, struct lock_object *ilk, int wakeup_swapper)
{
	if (flags & LK_INTERLOCK) {
		struct lock_class *class = LOCK_CLASS(ilk);
		class->lc_unlock(ilk);
	}
}

#ifdef RTEMS_BSD_LOCKMGR_TRACE
static void
lockmgr_trace(const char *label, const char dir, const struct lock *lk)
{
	printf("bsd: lck: %s %c lk=%p (%s) lk_lock=%08x rec=%d mtx=%c/%d\n",
	    label, dir, lk, lk->lock_object.lo_mtx.queue.Queue.name,
	    lk->lk_lock, lk->lk_recurse,
	    lk->lock_object.lo_mtx.queue.Queue.owner != NULL ? 'L' : 'U',
	    lk->lock_object.lo_mtx.nest_level);
}
#else
#define lockmgr_trace(lm, d, lk)
#endif

static int
lockmgr_slock_hard(struct lock *lk, u_int flags, struct lock_object *ilk,
    const char *file, int line)
{
	uintptr_t x;
	lockmgr_trace("slock", 'I', lk);
	rtems_bsd_mutex_lock(&lk->lock_object);
	x = lk->lk_lock;
	atomic_store_rel_ptr(&lk->lk_lock, x + LK_ONE_SHARER);
	lockmgr_trace("slock", 'O', lk);
	LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, lk->lk_recurse, file, line);
	lockmgr_exit(flags, ilk, 0);
	return 0;
}

static int
lockmgr_xlock_hard(struct lock *lk, u_int flags, struct lock_object *ilk,
    const char *file, int line)
{
	int error = 0;
	lockmgr_trace("xlock", 'I', lk);
	if ((flags & LK_NOWAIT) != 0) {
		if (!rtems_bsd_mutex_trylock(&lk->lock_object))
			error = EBUSY;
	} else {
		rtems_bsd_mutex_lock(&lk->lock_object);
	}
	if (error == 0) {
		atomic_store_rel_ptr(&lk->lk_lock, 0);
		lockmgr_trace("xlock", 'O', lk);
	}
	lockmgr_exit(flags, ilk, 0);
	return error;
}

static int
lockmgr_upgrade(struct lock *lk, u_int flags, struct lock_object *ilk,
    const char *file, int line)
{
	uintptr_t x, v;
	int error = 0;
	/*
	 * RTEMS does not support shared locks so locking a shared
	 * lock is locking an exclusive lock. This logic is here to
	 * track the shared vs exclusive logic so the VFS does not get
	 * upset.
	 */
	LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file, line);
	lockmgr_trace("xupgrade", 'I', lk);
	v = lk->lk_lock;
	x = v & ~LK_SHARE;
	atomic_store_rel_ptr(&lk->lk_lock, x);
	lockmgr_trace("xupgrade", 'O', lk);
	lockmgr_exit(flags, ilk, 0);
	return error;
}

static int
lockmgr_downgrade(struct lock *lk, u_int flags, struct lock_object *ilk,
    const char *file, int line)
{
	uintptr_t x;
	LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
	lockmgr_trace("xdowngrade", 'I', lk);
	x = lk->lk_lock;
	x &= LK_ALL_WAITERS;
	atomic_store_rel_ptr(&lk->lk_lock, x);
	lockmgr_trace("xdowngrade", 'O', lk);
	return 0;
}

int
lockmgr_lock_fast_path(struct lock *lk, u_int flags, struct lock_object *ilk,
    const char *file, int line)
{
	uintptr_t x, tid;
	u_int op;
	bool locked;

	if (__predict_false(panicstr != NULL))
		return (0);

	op = flags & LK_TYPE_MASK;
	locked = false;
	switch (op) {
	case LK_SHARED:
		if (!__predict_false(lk->lock_object.lo_flags & LK_NOSHARE))
			return (lockmgr_slock_hard(lk, flags, ilk, file, line));
		/* fall through */
	case LK_EXCLUSIVE:
		return (lockmgr_xlock_hard(lk, flags, ilk, file, line));
		break;
	case LK_UPGRADE:
	case LK_TRYUPGRADE:
		return (lockmgr_upgrade(lk, flags, ilk, file, line));
		break;
	case LK_DOWNGRADE:
		return (lockmgr_downgrade(lk, flags, ilk, file, line));
		break;
	default:
		break;
	}

	panic("unsupported lockmgr op: %d\n", op);
}

static int
lockmgr_sunlock_hard(struct lock *lk, uintptr_t x, u_int flags,
    struct lock_object *ilk, const char *file, int line)
{
	uintptr_t v;
	int error = 0;
	LOCK_LOG_LOCK(
	    "SUNLOCK", &lk->lock_object, 0, lk->lk_recurse, file, line);
	lockmgr_trace("sunlock", 'I', lk);
	x = lk->lk_lock;
	if (x == LK_UNLOCKED)
		panic("sunlock not locked");
	atomic_store_rel_ptr(&lk->lk_lock, x - LK_ONE_SHARER);
	rtems_bsd_mutex_unlock(&lk->lock_object);
	lockmgr_trace("sunlock", 'O', lk);
	lockmgr_exit(flags, ilk, 0);
	return error;
}

static int
lockmgr_xunlock_hard(struct lock *lk, uintptr_t x, u_int flags,
    struct lock_object *ilk, const char *file, int line)
{
	int error = 0;
	LOCK_LOG_LOCK(
	    "XUNLOCK", &lk->lock_object, 0, lk->lk_recurse, file, line);
	lockmgr_trace("xunlock", 'I', lk);
	if (lk->lk_recurse == 0) {
		uintptr_t v, x;
		x = lk->lk_lock;
		if (x != 0)
			x -= LK_ONE_SHARER;
		v = x | LK_SHARE;
		atomic_store_rel_ptr(&lk->lk_lock, v);
	}
	rtems_bsd_mutex_unlock(&lk->lock_object);
	lockmgr_trace("xunlock", 'O', lk);
	lockmgr_exit(flags, ilk, 0);
	return error;
}

int
lockmgr_unlock_fast_path(struct lock *lk, u_int flags, struct lock_object *ilk)
{
	struct lock_class *class;
	uintptr_t x, tid;
	const char *file;
	int line;

	if (__predict_false(panicstr != NULL))
		return (0);

	file = __FILE__;
	line = __LINE__;

	x = lk->lk_lock;
	if (__predict_true(x & LK_SHARE) != 0) {
		return (lockmgr_sunlock_hard(lk, x, flags, ilk, file, line));
	} else {
		return (lockmgr_xunlock_hard(lk, x, flags, ilk, file, line));
	}
}

int
lockstatus(const struct lock *lk)
{
	uintptr_t v, x;
	int ret = 0;

	ret = LK_SHARED;
	x = lk->lk_lock;

	if ((x & LK_SHARE) == 0) {
		v = rtems_bsd_mutex_owned(
		    RTEMS_DECONST(struct lock_object *, &lk->lock_object));
		if (v)
			ret = LK_EXCLUSIVE;
		else
			ret = LK_EXCLOTHER;
	} else if (x == LK_UNLOCKED) {
		ret = 0;
	}

	lockmgr_trace("status", 'O', lk);

	return (ret);
}

void
lockallowrecurse(struct lock *lk)
{
	lk->lock_object.lo_flags |= LO_RECURSABLE;
}

void
lockallowshare(struct lock *lk)
{
	lk->lock_object.lo_flags &= ~LK_NOSHARE;
}

void
lockdisablerecurse(struct lock *lk)
{
	lk->lock_object.lo_flags &= ~LO_RECURSABLE;
}

void
lockdisableshare(struct lock *lk)
{
	lk->lock_object.lo_flags |= LK_NOSHARE;
}

void
lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
{
	int iflags;

	MPASS((flags & ~LK_INIT_MASK) == 0);
	ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
	    ("%s: lockmgr not aligned for %s: %p", __func__, wmesg,
		&lk->lk_lock));

	iflags = LO_SLEEPABLE | LO_UPGRADABLE;
	if (flags & LK_CANRECURSE)
		iflags |= LO_RECURSABLE;
	if ((flags & LK_NODUP) == 0)
		iflags |= LO_DUPOK;
	if (flags & LK_NOPROFILE)
		iflags |= LO_NOPROFILE;
	if ((flags & LK_NOWITNESS) == 0)
		iflags |= LO_WITNESS;
	if (flags & LK_QUIET)
		iflags |= LO_QUIET;
	if (flags & LK_IS_VNODE)
		iflags |= LO_IS_VNODE;
	iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);

	rtems_bsd_mutex_init(
	    &lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);

	lk->lk_lock = LK_UNLOCKED;
	lk->lk_exslpfail = 0;
	lk->lk_timo = timo;
	lk->lk_pri = pri;
	STACK_ZERO(lk);
}

void
lockdestroy(struct lock *lk)
{
	KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
	KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
	KASSERT(lk->lk_exslpfail == 0, ("lockmgr still exclusive waiters"));
	rtems_bsd_mutex_destroy(&lk->lock_object);
}

void
lockmgr_printinfo(const struct lock *lk)
{
	struct thread *td;
	uintptr_t x;

	if (lk->lk_lock == LK_UNLOCKED)
		printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
	else if (lk->lk_lock & LK_SHARE)
		printf("lock type %s: SHARED (count %ju)\n",
		    lk->lock_object.lo_name,
		    (uintmax_t)LK_SHARERS(lk->lk_lock));
	else {
		td = lockmgr_xholder(lk);
		if (td == NULL)
			printf("lock type %s: not owned\n",
			    lk->lock_object.lo_name);
		else
			printf("lock type %s: EXCL by thread %p "
			       "(pid %d, rtems, tid %d)\n",
			    lk->lock_object.lo_name, td, td->td_proc->p_pid,
			    td->td_tid);
	}

	x = lk->lk_lock;
	if (x & LK_EXCLUSIVE_WAITERS)
		printf(" with exclusive waiters pending\n");
	if (x & LK_SHARED_WAITERS)
		printf(" with shared waiters pending\n");
	if (x & LK_EXCLUSIVE_SPINNERS)
		printf(" with exclusive spinners pending\n");

	STACK_PRINT(lk);
}

int
__lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
    const char *wmesg, int prio, int timo, const char *file, int line)
{
	struct lock_class *class;
	uintptr_t x;
	u_int op = (flags & LK_TYPE_MASK);
	int error = 0;

	if (panicstr != NULL)
		return (0);

	class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;

	if (lk->lock_object.lo_flags & LK_NOSHARE) {
		switch (op) {
		case LK_SHARED:
			op = LK_EXCLUSIVE;
			break;
		case LK_UPGRADE:
		case LK_TRYUPGRADE:
		case LK_DOWNGRADE:
			if (flags & LK_INTERLOCK)
				class->lc_unlock(ilk);
			return (0);
		}
	}

	switch (op) {
	case LK_SHARED:
		return (lockmgr_slock_hard(lk, flags, ilk, file, line));
		break;
	case LK_UPGRADE:
	case LK_TRYUPGRADE:
		return (lockmgr_upgrade(lk, flags, ilk, file, line));
		break;
	case LK_EXCLUSIVE:
		return (lockmgr_xlock_hard(lk, flags, ilk, file, line));
		break;
	case LK_DOWNGRADE:
		error = lockmgr_downgrade(lk, flags, ilk, file, line);
		break;
	case LK_RELEASE:
		lockmgr_trace("release", '-', lk);
		x = lk->lk_lock;
		if (__predict_true(x & LK_SHARE) != 0) {
			return (lockmgr_sunlock_hard(
			    lk, x, flags, ilk, file, line));
		} else {
			return (lockmgr_xunlock_hard(
			    lk, x, flags, ilk, file, line));
		}
		break;
	case LK_DRAIN:
		break;
	default:
		if (flags & LK_INTERLOCK)
			class->lc_unlock(ilk);
		panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
	}

	if (flags & LK_INTERLOCK)
		class->lc_unlock(ilk);

	return (error);
}

void
_lockmgr_disown(struct lock *lk, const char *file, int line)
{
}

#ifdef INVARIANT_SUPPORT

#ifndef INVARIANTS
#undef _lockmgr_assert
#endif

void
_lockmgr_assert(const struct lock *lk, int what, const char *file, int line)
{
	int slocked = 0;

	if (panicstr != NULL)
		return;
	switch (what) {
	case KA_SLOCKED:
	case KA_SLOCKED | KA_NOTRECURSED:
	case KA_SLOCKED | KA_RECURSED:
		slocked = 1;
	case KA_LOCKED:
	case KA_LOCKED | KA_NOTRECURSED:
	case KA_LOCKED | KA_RECURSED:
#ifdef WITNESS

		/*
		 * We cannot trust WITNESS if the lock is held in exclusive
		 * mode and a call to lockmgr_disown() happened.
		 * Workaround this skipping the check if the lock is held in
		 * exclusive mode even for the KA_LOCKED case.
		 */
		if (slocked || (lk->lk_lock & LK_SHARE)) {
			witness_assert(&lk->lock_object, what, file, line);
			break;
		}
#endif
		if (lk->lk_lock == LK_UNLOCKED ||
		    ((lk->lk_lock & LK_SHARE) == 0 &&
			(slocked ||
			    (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
			panic("Lock %s not %slocked @ %s:%d\n",
			    lk->lock_object.lo_name, slocked ? "share" : "",
			    file, line);

		if ((lk->lk_lock & LK_SHARE) == 0) {
			if (lockmgr_recursed(lk)) {
				if (what & KA_NOTRECURSED)
					panic("Lock %s recursed @ %s:%d\n",
					    lk->lock_object.lo_name, file,
					    line);
			} else if (what & KA_RECURSED)
				panic("Lock %s not recursed @ %s:%d\n",
				    lk->lock_object.lo_name, file, line);
		}
		break;
	case KA_XLOCKED:
	case KA_XLOCKED | KA_NOTRECURSED:
	case KA_XLOCKED | KA_RECURSED:
		if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
			panic("Lock %s not exclusively locked @ %s:%d\n",
			    lk->lock_object.lo_name, file, line);
		if (lockmgr_recursed(lk)) {
			if (what & KA_NOTRECURSED)
				panic("Lock %s recursed @ %s:%d\n",
				    lk->lock_object.lo_name, file, line);
		} else if (what & KA_RECURSED)
			panic("Lock %s not recursed @ %s:%d\n",
			    lk->lock_object.lo_name, file, line);
		break;
	case KA_UNLOCKED:
		if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
			panic("Lock %s exclusively locked @ %s:%d\n",
			    lk->lock_object.lo_name, file, line);
		break;
	default:
		panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
		    line);
	}
}
#endif