xref: /linux-6.15/kernel/locking/mutex-debug.c (revision a321fb90)
1 /*
2  * Debugging code for mutexes
3  *
4  * Started by Ingo Molnar:
5  *
6  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <[email protected]>
7  *
8  * lock debugging, locking tree, deadlock detection started by:
9  *
10  *  Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
11  *  Released under the General Public License (GPL).
12  */
13 #include <linux/mutex.h>
14 #include <linux/delay.h>
15 #include <linux/export.h>
16 #include <linux/poison.h>
17 #include <linux/sched.h>
18 #include <linux/spinlock.h>
19 #include <linux/kallsyms.h>
20 #include <linux/interrupt.h>
21 #include <linux/debug_locks.h>
22 
23 #include "mutex.h"
24 
25 /*
26  * Must be called with lock->wait_lock held.
27  */
28 void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)
29 {
30 	memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
31 	waiter->magic = waiter;
32 	INIT_LIST_HEAD(&waiter->list);
33 }
34 
35 void debug_mutex_wake_waiter(struct mutex *lock, struct mutex_waiter *waiter)
36 {
37 	lockdep_assert_held(&lock->wait_lock);
38 	DEBUG_LOCKS_WARN_ON(list_empty(&lock->wait_list));
39 	DEBUG_LOCKS_WARN_ON(waiter->magic != waiter);
40 	DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
41 }
42 
43 void debug_mutex_free_waiter(struct mutex_waiter *waiter)
44 {
45 	DEBUG_LOCKS_WARN_ON(!list_empty(&waiter->list));
46 	memset(waiter, MUTEX_DEBUG_FREE, sizeof(*waiter));
47 }
48 
49 void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
50 			    struct task_struct *task)
51 {
52 	lockdep_assert_held(&lock->wait_lock);
53 
54 	/* Mark the current thread as blocked on the lock: */
55 	task->blocked_on = waiter;
56 }
57 
58 void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
59 			 struct task_struct *task)
60 {
61 	DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
62 	DEBUG_LOCKS_WARN_ON(waiter->task != task);
63 	DEBUG_LOCKS_WARN_ON(task->blocked_on != waiter);
64 	task->blocked_on = NULL;
65 
66 	INIT_LIST_HEAD(&waiter->list);
67 	waiter->task = NULL;
68 }
69 
70 void debug_mutex_unlock(struct mutex *lock)
71 {
72 	if (likely(debug_locks)) {
73 		DEBUG_LOCKS_WARN_ON(lock->magic != lock);
74 		DEBUG_LOCKS_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next);
75 	}
76 }
77 
78 void debug_mutex_init(struct mutex *lock, const char *name,
79 		      struct lock_class_key *key)
80 {
81 #ifdef CONFIG_DEBUG_LOCK_ALLOC
82 	/*
83 	 * Make sure we are not reinitializing a held lock:
84 	 */
85 	debug_check_no_locks_freed((void *)lock, sizeof(*lock));
86 	lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_SLEEP);
87 #endif
88 	lock->magic = lock;
89 }
90 
91 /***
92  * mutex_destroy - mark a mutex unusable
93  * @lock: the mutex to be destroyed
94  *
95  * This function marks the mutex uninitialized, and any subsequent
96  * use of the mutex is forbidden. The mutex must not be locked when
97  * this function is called.
98  */
99 void mutex_destroy(struct mutex *lock)
100 {
101 	DEBUG_LOCKS_WARN_ON(mutex_is_locked(lock));
102 	lock->magic = NULL;
103 }
104 
105 EXPORT_SYMBOL_GPL(mutex_destroy);
106