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