summaryrefslogtreecommitdiff
path: root/ww_mutex.c
blob: 6858949d70036d66cc94979b4852a16ea81ab38a (plain)
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
580
581
582
583
/*
 * Wound/Wait Mutexes: blocking mutual exclusion locks with deadlock avoidance
 *
 * Original mutex implementation started by Ingo Molnar:
 *
 *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
 *
 * Wound/wait implementation:
 *  Copyright (C) 2013 Canonical Ltd.
 *
 * Wound/ wait drop-in, actual wound-wait semantics and batching:
 *  Copyright (C) 2016-2018 VMWare Inc.
 */

#include "ww_mutex.h"
#include <linux/sched/signal.h>
#include <linux/sched/debug.h>

#ifndef WW_BUILTIN

#undef EXPORT_SYMBOL_GPL
#define EXPORT_SYMBOL_GPL(_a)
#define MUTEX_FLAGS		0x07

#ifndef CONFIG_DEBUG_MUTEXES
#define debug_ww_mutex_add_waiter(_a, _b, _c)
#define debug_ww_mutex_wake_waiter(_a, _b)
#define debug_ww_mutex_lock_common(_a, _b)
#define mutex_remove_waiter(__lock, __waiter, __task) \
	__list_del((__waiter)->list.prev, (__waiter)->list.next)
#else
static void debug_ww_mutex_add_waiter(struct ww_mutex *ww,
				      struct mutex_waiter *waiter,
				      struct task_struct *task)
{
	SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&ww->my_class->lock));
	task->blocked_on = waiter;
}

static void debug_ww_mutex_wake_waiter(struct ww_mutex *ww,
				       struct mutex_waiter *waiter)
{
	struct mutex *lock = &ww->base;

	SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&ww->my_class->lock));
	DEBUG_LOCKS_WARN_ON(list_empty(&lock->wait_list));
	DEBUG_LOCKS_WARN_ON(waiter->magic != waiter);
	DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
}

static void debug_ww_mutex_lock_common(struct ww_mutex *ww,
				       struct mutex_waiter *waiter)
{
	memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
	waiter->magic = waiter;
	INIT_LIST_HEAD(&waiter->list);
}

static void mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
				struct task_struct *task)
{
	DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
	DEBUG_LOCKS_WARN_ON(waiter->task != task);
	DEBUG_LOCKS_WARN_ON(task->blocked_on != waiter);
	task->blocked_on = NULL;

	list_del_init(&waiter->list);
	waiter->task = NULL;
}
#endif


/**
 * ww_acquire_class_lock - Lock the global class spinlock unless batching
 *
 * @ww: The ww mutex.
 * @ww_ctx: The acquire context.
 *
 * Take the global class spinlock unless we're batching in which case the
 * global class spinlock was taken during batch begin.
 */
static void ww_acquire_class_lock(struct ww_mutex *ww,
				  struct ww_acquire_ctx *ww_ctx)
{
	if (!ww_ctx || !ww_ctx->batched)
		spin_lock(&ww->my_class->lock);
}


/**
 * ww_acquire_class_unlock - Unlock the global class spinlock unless batching
 *
 * @ww: The ww mutex.
 * @ww_ctx: The acquire context.
 *
 * Free the global class spinlock unless we're batching in which case the
 * global class spinlock is freed at batch end.
 */
static void ww_acquire_class_unlock(struct ww_mutex *ww,
				    struct ww_acquire_ctx *ww_ctx)
{
	if (!ww_ctx || !ww_ctx->batched)
		spin_unlock(&ww->my_class->lock);
}

static bool __mutex_waiter_is_first(struct mutex *lock,
				    struct mutex_waiter *waiter)
{
	return list_first_entry(&lock->wait_list, struct mutex_waiter, list) ==
		waiter;
}


/**
 * __ww_mutex_trylock - Trylock a ww_mutex
 *
 * @ww: The mutex to trylock
 * @ww_ctx: The acquire_ctx to register as locker or NULL.
 * @waiter: The waiter if a waiter is trying to lock, or NULL.
 * Return: true if lock succeeded. false otherwise.
 */
static bool __ww_mutex_trylock(struct ww_mutex *ww,
			       struct ww_acquire_ctx *ww_ctx,
			       struct mutex_waiter *waiter)
{
	struct mutex *lock = &ww->base;

	lockdep_assert_held(&ww->my_class->lock);

	if (atomic_long_read(&lock->owner))
		return false;

	/*
	 * No lock stealing for now. If there are waiters, only the first
	 * waiter is allowed to lock.
	 */
	if (!list_empty(&lock->wait_list) &&
	    !__mutex_waiter_is_first(lock, waiter))
		return false;

	atomic_long_set(&lock->owner, (unsigned long) current);
	ww->ctx = ww_ctx;
	if (ww_ctx)
		ww_ctx->acquired++;

	return true;
}

static bool
__ww_ctx_stamp_after(struct ww_acquire_ctx *a, struct ww_acquire_ctx *b)
{
	return a->stamp - b->stamp <= LONG_MAX &&
	       (a->stamp != b->stamp || a > b);
}

static struct task_struct *__owner_task(unsigned long owner)
{
	return (struct task_struct *)(owner & ~MUTEX_FLAGS);
}


/**
 * ww_mutex_waiter_backoff - Whether to backoff if younger
 *
 * @us: Our acquire_ctx.
 * @other: other waiter or lock owner acquire_ctx.
 * Return: Whether to back off for another waiter or lock owner if we're
 * younger than the lock owner or other waiter.
 */
static bool ww_mutex_waiter_backoff(struct ww_acquire_ctx *us,
				    struct ww_acquire_ctx *other)
{
  return (us->my_class->is_wait_die || us->wounded) && us->acquired > 0 &&
	  !other->done_acquire;
}


/**
 * ww_mutex_backoff - Whether to backoff
 *
 * @us: Our acquire_ctx.
 * @other: other waiter or lock owner acquire_ctx.
 * Return: Whether to back off for another waiter or lock owner.
 */
static bool ww_mutex_backoff(struct ww_acquire_ctx *us,
			     struct ww_acquire_ctx *other)
{
	return other && ww_mutex_waiter_backoff(us, other) &&
		us != other && __ww_ctx_stamp_after(us, other);
}


/**
 * ww_mutex_lock_backoff - Check backoff for lock owner and all other waiters.
 *
 * @us: Our acquire_ctx.
 * @ww: The ww_mutex considered.
 * Return: Whether to back off for any other waiters or lock owner.
 */
static bool ww_mutex_lock_backoff(struct ww_acquire_ctx *us,
				  struct ww_mutex *ww)
{
	struct mutex_waiter *cur;

	if (!us)
		return false;

	/*
	 * Wounded contexts lazy-preempt before first wounded lock wait, so that
	 * we have a lock to wait on after backoff.
	 */
	if (us->wounded)
		return true;

	/* Backoff for lock owner? */
	if (ww_mutex_backoff(us, ww->ctx))
		return true;

	/* Backoff for other waiters? */
	list_for_each_entry(cur, &ww->base.wait_list, list) {
		if (ww_mutex_backoff(us, cur->ww_ctx))
			return true;
	}

	return false;
}

static int
__ww_mutex_add_waiter(struct mutex_waiter *waiter,
		      struct ww_mutex *ww,
		      struct ww_acquire_ctx *ww_ctx)
{
	struct mutex *lock = &ww->base;
	struct mutex_waiter *cur;
	struct list_head *pos;
	bool is_wait_die = ww->my_class->is_wait_die;

	waiter->task = current;
	waiter->ww_ctx = ww_ctx;

	if (!ww_ctx) {
		list_add_tail(&waiter->list, &lock->wait_list);
		return 0;
	}

	/*
	 * Add the waiter before the first waiter with a higher stamp.
	 * Waiters without a context are skipped to avoid starving
	 * them.
	 */
	pos = &lock->wait_list;
	list_for_each_entry_reverse(cur, &lock->wait_list, list) {
		if (!cur->ww_ctx)
			continue;

		/* Early backoff for other waiter */
		if (__ww_ctx_stamp_after(ww_ctx, cur->ww_ctx)) {
			if (ww_mutex_waiter_backoff(ww_ctx, cur->ww_ctx))
				return -EDEADLK;

			break;
		}

		pos = &cur->list;

		/*
		 * Other waiter needs to backoff for us.
		 * Wake up the waiter so that it gets a chance to back
		 * off.
		 */
		if (ww_mutex_waiter_backoff(cur->ww_ctx, ww_ctx)) {
			debug_ww_mutex_wake_waiter(ww, cur);
			wake_up_process(cur->task);
		}
	}

	list_add_tail(&waiter->list, pos);

	/* Need to wound the lock owner? */
	if (!is_wait_die && ww->ctx && __ww_ctx_stamp_after(ww->ctx, ww_ctx) &&
	    ww_ctx->acquired > 0){
		ww->ctx->wounded = true;

		/*
		 * Wake up the lock owner in case it's sleeping on
		 * another ww_mutex..
		 */
		wake_up_process(__owner_task(atomic_long_read(&lock->owner)));
	}

	return 0;
}


/**
 * ww_mutex_wake_first_waiter - Wake first waiter if any.
 *
 * @ww: The ww_mutex on which to wake first waiter.
 */
static void ww_mutex_wake_first_waiter(struct ww_mutex *ww)
{
	struct mutex_waiter *cur;
	struct mutex *lock = &ww->base;

	if (!list_empty(&lock->wait_list)) {
		cur = list_first_entry(&lock->wait_list, struct mutex_waiter,
				       list);
		debug_ww_mutex_wake_waiter(ww, cur);
		wake_up_process(cur->task);
	}
}

static int __ww_mutex_lock(struct ww_mutex *ww, long state,
			   unsigned int subclass,
			   struct lockdep_map *nest_lock,
			   unsigned long ip,
			   struct ww_acquire_ctx *ww_ctx)
{
	struct mutex_waiter waiter;
	int ret = 0;

	lockdep_assert_held(&ww->my_class->lock);

	if (ww_ctx) {
		/*
		 * If we've backed off when wounded, there are no more
		 * acquired locks, and we can clear the wounded flag.
		 */
		if (ww_ctx->acquired == 0)
			ww_ctx->wounded = false;

		if (ww->ctx == ww_ctx) {
			ret = -EALREADY;
			goto err_early_backoff;
		}
	}

	if (__ww_mutex_trylock(ww, ww_ctx, &waiter))
		goto skip_wait;

	debug_ww_mutex_lock_common(ww, &waiter);
	debug_ww_mutex_add_waiter(ww, &waiter, current);

	lock_contended(&ww->base.dep_map, ip);

	ret = __ww_mutex_add_waiter(&waiter, ww, ww_ctx);
	if (ret)
		goto err_early_backoff;

	set_current_state(state);
	for (;;) {
		if (__ww_mutex_trylock(ww, ww_ctx, &waiter))
			break;

		if (unlikely(signal_pending_state(state, current))) {
			ret = -EINTR;
			goto err;
		}

		if (ww_mutex_lock_backoff(ww_ctx, ww)) {
			ret = -EDEADLK;
			goto err;
		}

		spin_unlock(&ww->my_class->lock);
		schedule();
		spin_lock(&ww->my_class->lock);
		set_current_state(state);
	}
	set_current_state(TASK_RUNNING);
	mutex_remove_waiter(&ww->base, &waiter, current);
skip_wait:
	lock_acquired(&ww->base.dep_map, ip);
	return 0;

err:
	set_current_state(TASK_RUNNING);
	mutex_remove_waiter(&ww->base, &waiter, current);
	ww_mutex_wake_first_waiter(ww);
err_early_backoff:
	lock_release(&ww->base.dep_map, !!ww_ctx, ip);
	return ret;
}

static void __ww_mutex_unlock(struct ww_mutex *ww, unsigned long ip)
{
	struct mutex *lock = &ww->base;

	lockdep_assert_held(&ww->my_class->lock);
	lock_release(&lock->dep_map, !!ww->ctx, ip);
	if (ww->ctx)
		ww->ctx->acquired--;
	ww->ctx = NULL;
	atomic_long_set(&lock->owner, 0);
	ww_mutex_wake_first_waiter(ww);
}

#ifdef CONFIG_DEBUG_LOCK_ALLOC
static int ww_mutex_deadlock_injection(struct ww_mutex *ww,
				       struct ww_acquire_ctx *ww_ctx)
{
#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
	unsigned tmp;

	if (ww_ctx->deadlock_inject_countdown-- == 0) {
		tmp = ww_ctx->deadlock_inject_interval;
		if (tmp > UINT_MAX/4)
			tmp = UINT_MAX;
		else
			tmp = tmp*2 + tmp + tmp/2;

		ww_ctx->deadlock_inject_interval = tmp;
		ww_ctx->deadlock_inject_countdown = tmp;

		ww_mutex_unlock(ww);

		return -EDEADLK;
	}

	return 0;
#endif /* CONFIG_DEBUG_WW_MUTEX_SLOWPATH */
}

/**
 * ww_class_lock_annotate - lockdep annotation at locking time.
 *
 * @ww: The ww_mutex
 * @ww_ctx: The acquire ctx or NULL
 * @ip: The caller stack
 */
static void  ww_class_lock_annotate(struct ww_mutex *ww,
					   struct ww_acquire_ctx *ww_ctx,
					   unsigned long ip)
{
	if (!ww_ctx || !ww_ctx->batched) {
		/* Annotate wait lock before the spinlock. */
		lock_acquire(&ww->base.dep_map, 0, 0, 0, 1,
			     ww_ctx ? &ww_ctx->dep_map : NULL, ip);
	} else {
		/*
		 * OK, We'd like to annotate ww trylocks under the class
		 * spinlock, but since each trylock becomes a lockdep level,
		 * we'd quickly run out of levels. And we can't annotate
		 * nested trylocks, since apparently lockdep can't cope
		 * with that. So cheat lockdep and fake a class spinlock
		 * release and annotate a wating ww lock...
		 */
		lock_release(&ww->my_class->lock.dep_map, 0, ip);
		lock_acquire(&ww->base.dep_map, 0, 0, 0, 1,
			     &ww_ctx->dep_map, ip);
		lock_acquire(&ww->my_class->lock.dep_map, 0, 1, 0, 1, NULL, ip);
	}
}

int
ww_mutex_lock(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
{
	int ret;

	ww_class_lock_annotate(ww, ww_ctx, _RET_IP_);
	ww_acquire_class_lock(ww, ww_ctx);
	ret =  __ww_mutex_lock(ww, TASK_UNINTERRUPTIBLE,
			       0, ww_ctx ? &ww_ctx->dep_map : NULL, _RET_IP_,
			       ww_ctx);
	ww_acquire_class_unlock(ww, ww_ctx);
	if (!ret && ww_ctx && ww_ctx->acquired > 1)
		return ww_mutex_deadlock_injection(ww, ww_ctx);

	return ret;
}
EXPORT_SYMBOL_GPL(ww_mutex_lock);


int
ww_mutex_lock_interruptible(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
{
	int ret;

	ww_class_lock_annotate(ww, ww_ctx, _RET_IP_);
	ww_acquire_class_lock(ww, ww_ctx);
	ret = __ww_mutex_lock(ww, TASK_INTERRUPTIBLE,
			      0, ww_ctx ? &ww_ctx->dep_map : NULL, _RET_IP_,
			      ww_ctx);
	ww_acquire_class_unlock(ww, ww_ctx);
	if (!ret && ww_ctx && ww_ctx->acquired > 1)
		return ww_mutex_deadlock_injection(ww, ww_ctx);

	return ret;
}
EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible);

#else /* CONFIG_DEBUG_LOCK_ALLOC */

int
ww_mutex_lock(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
{
	int ret;

	ww_acquire_class_lock(ww, ww_ctx);
	ret = __ww_mutex_lock(ww, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_, ww_ctx);
	ww_acquire_class_unlock(ww, ww_ctx);

	return ret;
}
EXPORT_SYMBOL_GPL(ww_mutex_lock);


int
ww_mutex_lock_interruptible(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
{
	int ret;

	ww_acquire_class_lock(ww, ww_ctx);
	ret = __ww_mutex_lock(ww, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_, ww_ctx);
	ww_acquire_class_unlock(ww, ww_ctx);

	return ret;
}
EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible);

#endif  /* CONFIG_DEBUG_LOCK_ALLOC */

/**
 * ww_mutex_unlock_batched - Replacement to be used for batched unlock
 *
 * @ww: The mutex to unlock
 */
void
ww_mutex_unlock_batched(struct ww_mutex *ww)
{

	__ww_mutex_unlock(ww, _RET_IP_);
}
EXPORT_SYMBOL_GPL(ww_mutex_unlock_batched);

void
ww_mutex_unlock(struct ww_mutex *ww)
{
	struct ww_acquire_ctx *ww_ctx = ww->ctx;

	ww_acquire_class_lock(ww, ww_ctx);
	__ww_mutex_unlock(ww, _RET_IP_);
	ww_acquire_class_unlock(ww, ww_ctx);
}
EXPORT_SYMBOL_GPL(ww_mutex_unlock);

#ifdef WW_BATCHING
void  ww_acquire_batch_begin(struct ww_acquire_ctx *ww_ctx)
{
#ifdef CONFIG_DEBUG_MUTEXES
	WARN_ON(ww_ctx->batched);
#endif
	spin_lock(&ww_ctx->my_class->lock);
	ww_ctx->batched = true;
}
EXPORT_SYMBOL_GPL(ww_acquire_batch_begin);

void  ww_acquire_batch_end(struct ww_acquire_ctx *ww_ctx)
{
#ifdef CONFIG_DEBUG_MUTEXES
	WARN_ON(!ww_ctx->batched);
#endif
	ww_ctx->batched = false;
	spin_unlock(&ww_ctx->my_class->lock);
}
EXPORT_SYMBOL_GPL(ww_acquire_batch_end);
#endif

int  ww_mutex_trylock(struct ww_mutex *ww)
{
	bool locked;

        spin_lock(&ww->my_class->lock);
	locked = __ww_mutex_trylock(ww, NULL, NULL);
	spin_unlock(&ww->my_class->lock);
	if (locked)
		lock_acquire(&ww->base.dep_map, 0, 1, 0, 1, NULL, _RET_IP_);

	return locked;
}
EXPORT_SYMBOL_GPL(ww_mutex_trylock);

#endif