Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2018, Linaro Limited
 */

#include <assert.h>
#include <config.h>
#include <kernel/lockdep.h>
#include <kernel/unwind.h>
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>
#include <util.h>

/* lockdep_node::flags values */
/* Flags used for depth-first topological sorting */
#define LOCKDEP_NODE_TEMP_MARK		BIT(0)
#define LOCKDEP_NODE_PERM_MARK		BIT(1)
/* Flag used during breadth-first search (print shortest cycle) */
#define LOCKDEP_NODE_BFS_VISITED	BIT(2)

/* Find node in graph or add it */
static struct lockdep_node *lockdep_add_to_graph(
				struct lockdep_node_head *graph,
				uintptr_t lock_id)
{
	struct lockdep_node *node = NULL;

	assert(graph);
	TAILQ_FOREACH(node, graph, link)
		if (node->lock_id == lock_id)
			return node;

	node = calloc(1, sizeof(*node));
	if (!node)
		return NULL;

	node->lock_id = lock_id;
	STAILQ_INIT(&node->edges);
	TAILQ_INSERT_TAIL(graph, node, link);

	return node;
}

static vaddr_t *dup_call_stack(vaddr_t *stack)
{
	vaddr_t *nstack = NULL;
	int n = 0;

	if (!stack)
		return NULL;

	while (stack[n])
		n++;

	nstack = malloc((n + 1) * sizeof(vaddr_t));
	if (!nstack)
		return NULL;

	memcpy(nstack, stack, (n + 1) * sizeof(vaddr_t));

	return nstack;
}

static void lockdep_print_call_stack(vaddr_t *stack)
{
	vaddr_t *p = NULL;

	if (!IS_ENABLED(CFG_LOCKDEP_RECORD_STACK))
		return;

	EMSG_RAW("Call stack:");
	for (p = stack; p && *p; p++)
		EMSG_RAW(" %#" PRIxPTR, *p);
}

static TEE_Result lockdep_add_edge(struct lockdep_node *from,
				   struct lockdep_node *to,
				   vaddr_t *call_stack_from,
				   vaddr_t *call_stack_to,
				   uintptr_t thread_id)
{
	struct lockdep_edge *edge = NULL;

	STAILQ_FOREACH(edge, &from->edges, link)
		if (edge->to == to)
			return TEE_SUCCESS;

	edge = calloc(1, sizeof(*edge));
	if (!edge)
		return TEE_ERROR_OUT_OF_MEMORY;
	edge->to = to;
	edge->call_stack_from = dup_call_stack(call_stack_from);
	edge->call_stack_to = dup_call_stack(call_stack_to);
	edge->thread_id = thread_id;
	STAILQ_INSERT_TAIL(&from->edges, edge, link);

	return TEE_SUCCESS;
}

struct lockdep_bfs {
	struct lockdep_node *node;
	uintptr_t *path;
	int pathlen;
	TAILQ_ENTRY(lockdep_bfs) link;
};

TAILQ_HEAD(lockdep_bfs_head, lockdep_bfs);

static void lockdep_bfs_queue_delete(struct lockdep_bfs_head *queue)
{
	struct lockdep_bfs *cur = NULL;
	struct lockdep_bfs *next = NULL;

	TAILQ_FOREACH_SAFE(cur, queue, link, next) {
		TAILQ_REMOVE(queue, cur, link);
		free(cur->path);
		free(cur);
	}
}

/*
 * Print shortest cycle in @graph that contains @node.
 * This function performs an iterative breadth-first search starting from @node,
 * and stops when it reaches @node again. In each node we're tracking the path
 * from the start node.
 */
static uintptr_t *lockdep_graph_get_shortest_cycle(struct lockdep_node *node)
{
	struct lockdep_bfs_head queue;
	struct lockdep_bfs *qe = NULL;
	uintptr_t *ret = NULL;

	TAILQ_INIT(&queue);
	node->flags |= LOCKDEP_NODE_BFS_VISITED;

	qe = calloc(1, sizeof(*qe));
	if (!qe)
		goto out;
	qe->node = node;
	qe->path = malloc(sizeof(uintptr_t));
	if (!qe->path)
		goto out;
	qe->path[0] = node->lock_id;
	qe->pathlen = 1;
	TAILQ_INSERT_TAIL(&queue, qe, link);

	while (!TAILQ_EMPTY(&queue)) {
		qe = TAILQ_FIRST(&queue);

		struct lockdep_node *n = qe->node;

		TAILQ_REMOVE(&queue, qe, link);

		struct lockdep_edge *e = NULL;

		STAILQ_FOREACH(e, &n->edges, link) {
			if (e->to->lock_id == node->lock_id) {
				uintptr_t *tmp = NULL;
				size_t nlen = qe->pathlen + 1;

				/*
				 * Cycle found. Terminate cycle path with NULL
				 * and return it.
				 */
				tmp = realloc(qe->path,
					      nlen * sizeof(uintptr_t));
				if (!tmp) {
					EMSG("Out of memory");
					free(qe->path);
					ret = NULL;
					goto out;
				}
				qe->path = tmp;
				qe->path[nlen - 1] = 0;
				ret = qe->path;
				goto out;
			}

			if (!(e->to->flags & LOCKDEP_NODE_BFS_VISITED)) {
				e->to->flags |= LOCKDEP_NODE_BFS_VISITED;

				size_t nlen = qe->pathlen + 1;
				struct lockdep_bfs *nqe = calloc(1,
								 sizeof(*nqe));

				if (!nqe)
					goto out;
				nqe->node = e->to;
				nqe->path = malloc(nlen * sizeof(uintptr_t));
				if (!nqe->path)
					goto out;
				nqe->pathlen = nlen;
				memcpy(nqe->path, qe->path,
				       qe->pathlen * sizeof(uintptr_t));
				nqe->path[nlen - 1] = e->to->lock_id;
				TAILQ_INSERT_TAIL(&queue, nqe, link);
			}
		}
		free(qe->path);
		free(qe);
		qe = NULL;
	}

out:
	free(qe);
	lockdep_bfs_queue_delete(&queue);
	return ret;
}

static TEE_Result lockdep_visit(struct lockdep_node *node)
{
	if (node->flags & LOCKDEP_NODE_PERM_MARK)
		return TEE_SUCCESS;

	if (node->flags & LOCKDEP_NODE_TEMP_MARK)
		return TEE_ERROR_BAD_STATE;	/* Not a DAG! */

	node->flags |= LOCKDEP_NODE_TEMP_MARK;

	struct lockdep_edge *e;

	STAILQ_FOREACH(e, &node->edges, link) {
		TEE_Result res = lockdep_visit(e->to);

		if (res)
			return res;
	}

	node->flags |= LOCKDEP_NODE_PERM_MARK;
	return TEE_SUCCESS;
}

static TEE_Result lockdep_graph_sort(struct lockdep_node_head *graph)
{
	struct lockdep_node *node = NULL;

	TAILQ_FOREACH(node, graph, link) {
		if (!node->flags) {
			/* Unmarked node */
			TEE_Result res = lockdep_visit(node);

			if (res)
				return res;
		}
	}

	TAILQ_FOREACH(node, graph, link)
		node->flags = 0;

	return TEE_SUCCESS;
}

static struct lockdep_edge *lockdep_find_edge(struct lockdep_node_head *graph,
					      uintptr_t from, uintptr_t to)
{
	struct lockdep_node *node = NULL;
	struct lockdep_edge *edge = NULL;

	TAILQ_FOREACH(node, graph, link)
		if (node->lock_id == from)
			STAILQ_FOREACH(edge, &node->edges, link)
				if (edge->to->lock_id == to)
					return edge;
	return NULL;
}

static void lockdep_print_edge_info(uintptr_t from __maybe_unused,
				    struct lockdep_edge *edge)
{
	uintptr_t __maybe_unused to = edge->to->lock_id;
	const char __maybe_unused *at_msg = "";
	const char __maybe_unused *acq_msg = "";

	if (IS_ENABLED(CFG_LOCKDEP_RECORD_STACK)) {
		at_msg = " at:";
		acq_msg = " acquired at:";
	}

	EMSG_RAW("-> Thread %#" PRIxPTR " acquired lock %#" PRIxPTR "%s",
		 edge->thread_id, to, at_msg);
	lockdep_print_call_stack(edge->call_stack_to);
	EMSG_RAW("...while holding lock %#" PRIxPTR "%s",
		 from, acq_msg);
	lockdep_print_call_stack(edge->call_stack_from);
}

/*
 * Find cycle containing @node in the lock graph, then print full debug
 * information about each edge (thread that acquired the locks and call stacks)
 */
static void lockdep_print_cycle_info(struct lockdep_node_head *graph,
				     struct lockdep_node *node)
{
	struct lockdep_edge *edge = NULL;
	uintptr_t *cycle = NULL;
	uintptr_t *p = NULL;
	uintptr_t from = 0;
	uintptr_t to = 0;

	cycle = lockdep_graph_get_shortest_cycle(node);
	assert(cycle && cycle[0]);
	EMSG_RAW("-> Shortest cycle:");
	for (p = cycle; *p; p++)
		EMSG_RAW(" Lock %#" PRIxPTR, *p);
	for (p = cycle; ; p++) {
		if (!*p) {
			assert(p != cycle);
			from = to;
			to = cycle[0];
			edge = lockdep_find_edge(graph, from, to);
			lockdep_print_edge_info(from, edge);
			break;
		}
		if (p != cycle)
			from = to;
		to = *p;
		if (p != cycle) {
			edge = lockdep_find_edge(graph, from, to);
			lockdep_print_edge_info(from, edge);
		}
	}
	free(cycle);
}

static vaddr_t *lockdep_get_kernel_stack(void)
{
	if (IS_ENABLED(CFG_LOCKDEP_RECORD_STACK))
		return unw_get_kernel_stack();

	return NULL;
}

TEE_Result __lockdep_lock_acquire(struct lockdep_node_head *graph,
				  struct lockdep_lock_head *owned,
				  uintptr_t id)
{
	struct lockdep_node *node = lockdep_add_to_graph(graph, id);

	if (!node)
		return TEE_ERROR_OUT_OF_MEMORY;

	struct lockdep_lock *lock = NULL;
	vaddr_t *acq_stack = lockdep_get_kernel_stack();

	TAILQ_FOREACH(lock, owned, link) {
		TEE_Result res = lockdep_add_edge(lock->node, node,
						  lock->call_stack,
						  acq_stack,
						  (uintptr_t)owned);

		if (res)
			return res;
	}

	TEE_Result res = lockdep_graph_sort(graph);

	if (res) {
		EMSG_RAW("Potential deadlock detected!");
		EMSG_RAW("When trying to acquire lock %#" PRIxPTR, id);
		lockdep_print_cycle_info(graph, node);
		return res;
	}

	lock = calloc(1, sizeof(*lock));
	if (!lock)
		return TEE_ERROR_OUT_OF_MEMORY;

	lock->node = node;
	lock->call_stack = acq_stack;
	TAILQ_INSERT_TAIL(owned, lock, link);

	return TEE_SUCCESS;
}

/*
 * Call this when it is known that the thread has been able to acquire the lock.
 * Similar to __lockdep_lock_acquire(), but since the operation is non-blocking,
 * no dependency to currently owned locks are created.
 */
TEE_Result __lockdep_lock_tryacquire(struct lockdep_node_head *graph,
				     struct lockdep_lock_head *owned,
				     uintptr_t id)
{
	struct lockdep_node *node = lockdep_add_to_graph(graph, id);

	if (!node)
		return TEE_ERROR_OUT_OF_MEMORY;

	struct lockdep_lock *lock = NULL;
	vaddr_t *acq_stack = lockdep_get_kernel_stack();

	lock = calloc(1, sizeof(*lock));
	if (!lock)
		return TEE_ERROR_OUT_OF_MEMORY;

	lock->node = node;
	lock->call_stack = acq_stack;
	TAILQ_INSERT_TAIL(owned, lock, link);

	return TEE_SUCCESS;
}

TEE_Result __lockdep_lock_release(struct lockdep_lock_head *owned, uintptr_t id)
{
	struct lockdep_lock *lock = NULL;

	TAILQ_FOREACH_REVERSE(lock, owned, lockdep_lock_head, link) {
		if (lock->node->lock_id == id) {
			TAILQ_REMOVE(owned, lock, link);
			free(lock->call_stack);
			free(lock);
			return TEE_SUCCESS;
		}
	}

	EMSG_RAW("Thread %p does not own lock %#" PRIxPTR, (void *)owned, id);
	return TEE_ERROR_ITEM_NOT_FOUND;
}

static void lockdep_free_edge(struct lockdep_edge *edge)
{
	free(edge->call_stack_from);
	free(edge->call_stack_to);
	free(edge);
}

static void lockdep_node_delete(struct lockdep_node *node)
{
	struct lockdep_edge *edge = NULL;
	struct lockdep_edge *next = NULL;

	STAILQ_FOREACH_SAFE(edge, &node->edges, link, next)
		lockdep_free_edge(edge);

	free(node);
}

void lockdep_graph_delete(struct lockdep_node_head *graph)
{
	struct lockdep_node *node = NULL;
	struct lockdep_node *next = NULL;

	TAILQ_FOREACH_SAFE(node, graph, link, next) {
		TAILQ_REMOVE(graph, node, link);
		lockdep_node_delete(node);
	}
}

void lockdep_queue_delete(struct lockdep_lock_head *owned)
{
	struct lockdep_lock *lock = NULL;
	struct lockdep_lock *next = NULL;

	TAILQ_FOREACH_SAFE(lock, owned, link, next) {
		TAILQ_REMOVE(owned, lock, link);
		free(lock);
	}
}

static void lockdep_node_destroy(struct lockdep_node_head *graph,
				 struct lockdep_node *node)
{
	struct lockdep_edge *edge = NULL;
	struct lockdep_edge *next = NULL;
	struct lockdep_node *from = NULL;

	TAILQ_REMOVE(graph, node, link);

	/*
	 * Loop over all nodes in the graph to remove all edges with the
	 * node to remove in the "to" field.
	 */
	TAILQ_FOREACH(from, graph, link) {
		edge = STAILQ_FIRST(&from->edges);
		while (edge && edge->to == node) {
			STAILQ_REMOVE_HEAD(&from->edges, link);
			lockdep_free_edge(edge);
			edge = STAILQ_FIRST(&from->edges);
		}

		if (!edge)
			continue;

		next = STAILQ_NEXT(edge, link);
		while (next) {
			if (next->to == node) {
				STAILQ_REMOVE_AFTER(&from->edges, edge, link);
				lockdep_free_edge(next);
			} else {
				edge = next;
			}
			next = STAILQ_NEXT(edge, link);
		}
	}

	STAILQ_FOREACH_SAFE(edge, &node->edges, link, next)
		lockdep_free_edge(edge);

	free(node);
}

void lockdep_lock_destroy(struct lockdep_node_head *graph, uintptr_t lock_id)
{
	struct lockdep_node *node = NULL;

	assert(graph);
	TAILQ_FOREACH(node, graph, link) {
		if (node->lock_id == lock_id) {
			lockdep_node_destroy(graph, node);
			break;
		}
	}
}