summaryrefslogtreecommitdiffstats
path: root/kernel/rtmutex-debug.c
blob: 4aa8a2c9f45341935827b904b1aa27b74b5fc8c8 (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
/*
 * RT-Mutexes: blocking mutual exclusion locks with PI support
 *
 * started by Ingo Molnar and Thomas Gleixner:
 *
 *  Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
 *  Copyright (C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
 *
 * This code is based on the rt.c implementation in the preempt-rt tree.
 * Portions of said code are
 *
 *  Copyright (C) 2004  LynuxWorks, Inc., Igor Manyilov, Bill Huey
 *  Copyright (C) 2006  Esben Nielsen
 *  Copyright (C) 2006  Kihon Technologies Inc.,
 *			Steven Rostedt <rostedt@goodmis.org>
 *
 * See rt.c in preempt-rt for proper credits and further information
 */
#include <linux/config.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/kallsyms.h>
#include <linux/syscalls.h>
#include <linux/interrupt.h>
#include <linux/plist.h>
#include <linux/fs.h>

#include "rtmutex_common.h"

#ifdef CONFIG_DEBUG_RT_MUTEXES
# include "rtmutex-debug.h"
#else
# include "rtmutex.h"
#endif

# define TRACE_WARN_ON(x)			WARN_ON(x)
# define TRACE_BUG_ON(x)			BUG_ON(x)

# define TRACE_OFF()						\
do {								\
	if (rt_trace_on) {					\
		rt_trace_on = 0;				\
		console_verbose();				\
		if (spin_is_locked(&current->pi_lock))		\
			spin_unlock(&current->pi_lock);		\
		if (spin_is_locked(&current->held_list_lock))	\
			spin_unlock(&current->held_list_lock);	\
	}							\
} while (0)

# define TRACE_OFF_NOLOCK()					\
do {								\
	if (rt_trace_on) {					\
		rt_trace_on = 0;				\
		console_verbose();				\
	}							\
} while (0)

# define TRACE_BUG_LOCKED()			\
do {						\
	TRACE_OFF();				\
	BUG();					\
} while (0)

# define TRACE_WARN_ON_LOCKED(c)		\
do {						\
	if (unlikely(c)) {			\
		TRACE_OFF();			\
		WARN_ON(1);			\
	}					\
} while (0)

# define TRACE_BUG_ON_LOCKED(c)			\
do {						\
	if (unlikely(c))			\
		TRACE_BUG_LOCKED();		\
} while (0)

#ifdef CONFIG_SMP
# define SMP_TRACE_BUG_ON_LOCKED(c)	TRACE_BUG_ON_LOCKED(c)
#else
# define SMP_TRACE_BUG_ON_LOCKED(c)	do { } while (0)
#endif

/*
 * deadlock detection flag. We turn it off when we detect
 * the first problem because we dont want to recurse back
 * into the tracing code when doing error printk or
 * executing a BUG():
 */
int rt_trace_on = 1;

void deadlock_trace_off(void)
{
	rt_trace_on = 0;
}

static void printk_task(task_t *p)
{
	if (p)
		printk("%16s:%5d [%p, %3d]", p->comm, p->pid, p, p->prio);
	else
		printk("<none>");
}

static void printk_task_short(task_t *p)
{
	if (p)
		printk("%s/%d [%p, %3d]", p->comm, p->pid, p, p->prio);
	else
		printk("<none>");
}

static void printk_lock(struct rt_mutex *lock, int print_owner)
{
	if (lock->name)
		printk(" [%p] {%s}\n",
			lock, lock->name);
	else
		printk(" [%p] {%s:%d}\n",
			lock, lock->file, lock->line);

	if (print_owner && rt_mutex_owner(lock)) {
		printk(".. ->owner: %p\n", lock->owner);
		printk(".. held by:  ");
		printk_task(rt_mutex_owner(lock));
		printk("\n");
	}
	if (rt_mutex_owner(lock)) {
		printk("... acquired at:               ");
		print_symbol("%s\n", lock->acquire_ip);
	}
}

static void printk_waiter(struct rt_mutex_waiter *w)
{
	printk("-------------------------\n");
	printk("| waiter struct %p:\n", w);
	printk("| w->list_entry: [DP:%p/%p|SP:%p/%p|PRI:%d]\n",
	       w->list_entry.plist.prio_list.prev, w->list_entry.plist.prio_list.next,
	       w->list_entry.plist.node_list.prev, w->list_entry.plist.node_list.next,
	       w->list_entry.prio);
	printk("| w->pi_list_entry: [DP:%p/%p|SP:%p/%p|PRI:%d]\n",
	       w->pi_list_entry.plist.prio_list.prev, w->pi_list_entry.plist.prio_list.next,
	       w->pi_list_entry.plist.node_list.prev, w->pi_list_entry.plist.node_list.next,
	       w->pi_list_entry.prio);
	printk("\n| lock:\n");
	printk_lock(w->lock, 1);
	printk("| w->ti->task:\n");
	printk_task(w->task);
	printk("| blocked at:  ");
	print_symbol("%s\n", w->ip);
	printk("-------------------------\n");
}

static void show_task_locks(task_t *p)
{
	switch (p->state) {
	case TASK_RUNNING:		printk("R"); break;
	case TASK_INTERRUPTIBLE:	printk("S"); break;
	case TASK_UNINTERRUPTIBLE:	printk("D"); break;
	case TASK_STOPPED:		printk("T"); break;
	case EXIT_ZOMBIE:		printk("Z"); break;
	case EXIT_DEAD:			printk("X"); break;
	default:			printk("?"); break;
	}
	printk_task(p);
	if (p->pi_blocked_on) {
		struct rt_mutex *lock = p->pi_blocked_on->lock;

		printk(" blocked on:");
		printk_lock(lock, 1);
	} else
		printk(" (not blocked)\n");
}

void rt_mutex_show_held_locks(task_t *task, int verbose)
{
	struct list_head *curr, *cursor = NULL;
	struct rt_mutex *lock;
	task_t *t;
	unsigned long flags;
	int count = 0;

	if (!rt_trace_on)
		return;

	if (verbose) {
		printk("------------------------------\n");
		printk("| showing all locks held by: |  (");
		printk_task_short(task);
		printk("):\n");
		printk("------------------------------\n");
	}

next:
	spin_lock_irqsave(&task->held_list_lock, flags);
	list_for_each(curr, &task->held_list_head) {
		if (cursor && curr != cursor)
			continue;
		lock = list_entry(curr, struct rt_mutex, held_list_entry);
		t = rt_mutex_owner(lock);
		WARN_ON(t != task);
		count++;
		cursor = curr->next;
		spin_unlock_irqrestore(&task->held_list_lock, flags);

		printk("\n#%03d:            ", count);
		printk_lock(lock, 0);
		goto next;
	}
	spin_unlock_irqrestore(&task->held_list_lock, flags);

	printk("\n");
}

void rt_mutex_show_all_locks(void)
{
	task_t *g, *p;
	int count = 10;
	int unlock = 1;

	printk("\n");
	printk("----------------------\n");
	printk("| showing all tasks: |\n");
	printk("----------------------\n");

	/*
	 * Here we try to get the tasklist_lock as hard as possible,
	 * if not successful after 2 seconds we ignore it (but keep
	 * trying). This is to enable a debug printout even if a
	 * tasklist_lock-holding task deadlocks or crashes.
	 */
retry:
	if (!read_trylock(&tasklist_lock)) {
		if (count == 10)
			printk("hm, tasklist_lock locked, retrying... ");
		if (count) {
			count--;
			printk(" #%d", 10-count);
			mdelay(200);
			goto retry;
		}
		printk(" ignoring it.\n");
		unlock = 0;
	}
	if (count != 10)
		printk(" locked it.\n");

	do_each_thread(g, p) {
		show_task_locks(p);
		if (!unlock)
			if (read_trylock(&tasklist_lock))
				unlock = 1;
	} while_each_thread(g, p);

	printk("\n");

	printk("-----------------------------------------\n");
	printk("| showing all locks held in the system: |\n");
	printk("-----------------------------------------\n");

	do_each_thread(g, p) {
		rt_mutex_show_held_locks(p, 0);
		if (!unlock)
			if (read_trylock(&tasklist_lock))
				unlock = 1;
	} while_each_thread(g, p);


	printk("=============================================\n\n");

	if (unlock)
		read_unlock(&tasklist_lock);
}

void rt_mutex_debug_check_no_locks_held(task_t *task)
{
	struct rt_mutex_waiter *w;
	struct list_head *curr;
	struct rt_mutex *lock;

	if (!rt_trace_on)
		return;
	if (!rt_prio(task->normal_prio) && rt_prio(task->prio)) {
		printk("BUG: PI priority boost leaked!\n");
		printk_task(task);
		printk("\n");
	}
	if (list_empty(&task->held_list_head))
		return;

	spin_lock(&task->pi_lock);
	plist_for_each_entry(w, &task->pi_waiters, pi_list_entry) {
		TRACE_OFF();

		printk("hm, PI interest held at exit time? Task:\n");
		printk_task(task);
		printk_waiter(w);
		return;
	}
	spin_unlock(&task->pi_lock);

	list_for_each(curr, &task->held_list_head) {
		lock = list_entry(curr, struct rt_mutex, held_list_entry);

		printk("BUG: %s/%d, lock held at task exit time!\n",
		       task->comm, task->pid);
		printk_lock(lock, 1);
		if (rt_mutex_owner(lock) != task)
			printk("exiting task is not even the owner??\n");
	}
}

int rt_mutex_debug_check_no_locks_freed(const void *from, unsigned long len)
{
	const void *to = from + len;
	struct list_head *curr;
	struct rt_mutex *lock;
	unsigned long flags;
	void *lock_addr;

	if (!rt_trace_on)
		return 0;

	spin_lock_irqsave(&current->held_list_lock, flags);
	list_for_each(curr, &current->held_list_head) {
		lock = list_entry(curr, struct rt_mutex, held_list_entry);
		lock_addr = lock;
		if (lock_addr < from || lock_addr >= to)
			continue;
		TRACE_OFF();

		printk("BUG: %s/%d, active lock [%p(%p-%p)] freed!\n",
			current->comm, current->pid, lock, from, to);
		dump_stack();
		printk_lock(lock, 1);
		if (rt_mutex_owner(lock) != current)
			printk("freeing task is not even the owner??\n");
		return 1;
	}
	spin_unlock_irqrestore(&current->held_list_lock, flags);

	return 0;
}

void rt_mutex_debug_task_free(struct task_struct *task)
{
	WARN_ON(!plist_head_empty(&task->pi_waiters));
	WARN_ON(task->pi_blocked_on);
}

/*
 * We fill out the fields in the waiter to store the information about
 * the deadlock. We print when we return. act_waiter can be NULL in
 * case of a remove waiter operation.
 */
void debug_rt_mutex_deadlock(int detect, struct rt_mutex_waiter *act_waiter,
			     struct rt_mutex *lock)
{
	struct task_struct *task;

	if (!rt_trace_on || detect || !act_waiter)
		return;

	task = rt_mutex_owner(act_waiter->lock);
	if (task && task != current) {
		act_waiter->deadlock_task_pid = task->pid;
		act_waiter->deadlock_lock = lock;
	}
}

void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter)
{
	struct task_struct *task;

	if (!waiter->deadlock_lock || !rt_trace_on)
		return;

	task = find_task_by_pid(waiter->deadlock_task_pid);
	if (!task)
		return;

	TRACE_OFF_NOLOCK();

	printk("\n============================================\n");
	printk(  "[ BUG: circular locking deadlock detected! ]\n");
	printk(  "--------------------------------------------\n");
	printk("%s/%d is deadlocking current task %s/%d\n\n",
	       task->comm, task->pid, current->comm, current->pid);

	printk("\n1) %s/%d is trying to acquire this lock:\n",
	       current->comm, current->pid);
	printk_lock(waiter->lock, 1);

	printk("... trying at:                 ");
	print_symbol("%s\n", waiter->ip);

	printk("\n2) %s/%d is blocked on this lock:\n", task->comm, task->pid);
	printk_lock(waiter->deadlock_lock, 1);

	rt_mutex_show_held_locks(current, 1);
	rt_mutex_show_held_locks(task, 1);

	printk("\n%s/%d's [blocked] stackdump:\n\n", task->comm, task->pid);
	show_stack(task, NULL);
	printk("\n%s/%d's [current] stackdump:\n\n",
	       current->comm, current->pid);
	dump_stack();
	rt_mutex_show_all_locks();
	printk("[ turning off deadlock detection."
	       "Please report this trace. ]\n\n");
	local_irq_disable();
}

void debug_rt_mutex_lock(struct rt_mutex *lock __IP_DECL__)
{
	unsigned long flags;

	if (rt_trace_on) {
		TRACE_WARN_ON_LOCKED(!list_empty(&lock->held_list_entry));

		spin_lock_irqsave(&current->held_list_lock, flags);
		list_add_tail(&lock->held_list_entry, &current->held_list_head);
		spin_unlock_irqrestore(&current->held_list_lock, flags);

		lock->acquire_ip = ip;
	}
}

void debug_rt_mutex_unlock(struct rt_mutex *lock)
{
	unsigned long flags;

	if (rt_trace_on) {
		TRACE_WARN_ON_LOCKED(rt_mutex_owner(lock) != current);
		TRACE_WARN_ON_LOCKED(list_empty(&lock->held_list_entry));

		spin_lock_irqsave(&current->held_list_lock, flags);
		list_del_init(&lock->held_list_entry);
		spin_unlock_irqrestore(&current->held_list_lock, flags);
	}
}

void debug_rt_mutex_proxy_lock(struct rt_mutex *lock,
			       struct task_struct *powner __IP_DECL__)
{
	unsigned long flags;

	if (rt_trace_on) {
		TRACE_WARN_ON_LOCKED(!list_empty(&lock->held_list_entry));

		spin_lock_irqsave(&powner->held_list_lock, flags);
		list_add_tail(&lock->held_list_entry, &powner->held_list_head);
		spin_unlock_irqrestore(&powner->held_list_lock, flags);

		lock->acquire_ip = ip;
	}
}

void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
{
	unsigned long flags;

	if (rt_trace_on) {
		struct task_struct *owner = rt_mutex_owner(lock);

		TRACE_WARN_ON_LOCKED(!owner);
		TRACE_WARN_ON_LOCKED(list_empty(&lock->held_list_entry));

		spin_lock_irqsave(&owner->held_list_lock, flags);
		list_del_init(&lock->held_list_entry);
		spin_unlock_irqrestore(&owner->held_list_lock, flags);
	}
}

void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
{
	memset(waiter, 0x11, sizeof(*waiter));
	plist_node_init(&waiter->list_entry, MAX_PRIO);
	plist_node_init(&waiter->pi_list_entry, MAX_PRIO);
}

void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
{
	TRACE_WARN_ON(!plist_node_empty(&waiter->list_entry));
	TRACE_WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
	TRACE_WARN_ON(waiter->task);
	memset(waiter, 0x22, sizeof(*waiter));
}

void debug_rt_mutex_init(struct rt_mutex *lock, const char *name)
{
	void *addr = lock;

	if (rt_trace_on) {
		rt_mutex_debug_check_no_locks_freed(addr,
						    sizeof(struct rt_mutex));
		INIT_LIST_HEAD(&lock->held_list_entry);
		lock->name = name;
	}
}

void rt_mutex_deadlock_account_lock(struct rt_mutex *lock, task_t *task)
{
}

void rt_mutex_deadlock_account_unlock(struct task_struct *task)
{
}

OpenPOWER on IntegriCloud