blob: bc8abb8549d20d38638ab6e1a10d1107f1fe39af [file] [log] [blame]
Ingo Molnar408894e2006-01-09 15:59:20 -08001/*
Ingo Molnar408894e2006-01-09 15:59:20 -08002 * Debugging code for mutexes
3 *
4 * Started by Ingo Molnar:
5 *
6 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
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>
Ingo Molnar408894e2006-01-09 15:59:20 -080014#include <linux/delay.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040015#include <linux/export.h>
Randy Dunlapa7807a32006-06-27 02:53:54 -070016#include <linux/poison.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040017#include <linux/sched.h>
Ingo Molnar408894e2006-01-09 15:59:20 -080018#include <linux/spinlock.h>
19#include <linux/kallsyms.h>
20#include <linux/interrupt.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070021#include <linux/debug_locks.h>
Ingo Molnar408894e2006-01-09 15:59:20 -080022
Thomas Gleixnera321fb92021-08-17 16:17:38 +020023#include "mutex.h"
Ingo Molnar408894e2006-01-09 15:59:20 -080024
25/*
Ingo Molnar408894e2006-01-09 15:59:20 -080026 * Must be called with lock->wait_lock held.
27 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070028void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)
Ingo Molnar408894e2006-01-09 15:59:20 -080029{
Randy Dunlapa7807a32006-06-27 02:53:54 -070030 memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
Ingo Molnar408894e2006-01-09 15:59:20 -080031 waiter->magic = waiter;
32 INIT_LIST_HEAD(&waiter->list);
Peter Zijlstrac0afb0f2021-08-15 23:28:39 +020033 waiter->ww_ctx = MUTEX_POISON_WW_CTX;
Ingo Molnar408894e2006-01-09 15:59:20 -080034}
35
36void debug_mutex_wake_waiter(struct mutex *lock, struct mutex_waiter *waiter)
37{
Lance Roy04547722018-10-04 23:45:46 -070038 lockdep_assert_held(&lock->wait_lock);
Ingo Molnar9e7f4d42006-07-03 00:24:30 -070039 DEBUG_LOCKS_WARN_ON(list_empty(&lock->wait_list));
40 DEBUG_LOCKS_WARN_ON(waiter->magic != waiter);
41 DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
Ingo Molnar408894e2006-01-09 15:59:20 -080042}
43
44void debug_mutex_free_waiter(struct mutex_waiter *waiter)
45{
Ingo Molnar9e7f4d42006-07-03 00:24:30 -070046 DEBUG_LOCKS_WARN_ON(!list_empty(&waiter->list));
Randy Dunlapa7807a32006-06-27 02:53:54 -070047 memset(waiter, MUTEX_DEBUG_FREE, sizeof(*waiter));
Ingo Molnar408894e2006-01-09 15:59:20 -080048}
49
50void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
Linus Torvalds6720a302016-06-23 12:11:17 -070051 struct task_struct *task)
Ingo Molnar408894e2006-01-09 15:59:20 -080052{
Lance Roy04547722018-10-04 23:45:46 -070053 lockdep_assert_held(&lock->wait_lock);
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070054
Ingo Molnar408894e2006-01-09 15:59:20 -080055 /* Mark the current thread as blocked on the lock: */
Linus Torvalds6720a302016-06-23 12:11:17 -070056 task->blocked_on = waiter;
Ingo Molnar408894e2006-01-09 15:59:20 -080057}
58
Zqiang3a010c42021-05-17 11:40:05 +080059void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
Linus Torvalds6720a302016-06-23 12:11:17 -070060 struct task_struct *task)
Ingo Molnar408894e2006-01-09 15:59:20 -080061{
Ingo Molnar9e7f4d42006-07-03 00:24:30 -070062 DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
Linus Torvalds6720a302016-06-23 12:11:17 -070063 DEBUG_LOCKS_WARN_ON(waiter->task != task);
64 DEBUG_LOCKS_WARN_ON(task->blocked_on != waiter);
65 task->blocked_on = NULL;
Ingo Molnar408894e2006-01-09 15:59:20 -080066
Zqiang3a010c42021-05-17 11:40:05 +080067 INIT_LIST_HEAD(&waiter->list);
Ingo Molnar408894e2006-01-09 15:59:20 -080068 waiter->task = NULL;
69}
70
71void debug_mutex_unlock(struct mutex *lock)
72{
Peter Zijlstraa2279602014-04-10 16:15:59 +020073 if (likely(debug_locks)) {
74 DEBUG_LOCKS_WARN_ON(lock->magic != lock);
Peter Zijlstraa2279602014-04-10 16:15:59 +020075 DEBUG_LOCKS_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next);
Peter Zijlstraa2279602014-04-10 16:15:59 +020076 }
Ingo Molnar408894e2006-01-09 15:59:20 -080077}
78
Ingo Molnaref5d4702006-07-03 00:24:55 -070079void debug_mutex_init(struct mutex *lock, const char *name,
80 struct lock_class_key *key)
Ingo Molnar408894e2006-01-09 15:59:20 -080081{
Ingo Molnaref5d4702006-07-03 00:24:55 -070082#ifdef CONFIG_DEBUG_LOCK_ALLOC
Ingo Molnar408894e2006-01-09 15:59:20 -080083 /*
84 * Make sure we are not reinitializing a held lock:
85 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070086 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
Peter Zijlstrade8f5e42020-03-21 12:26:01 +010087 lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_SLEEP);
Ingo Molnaref5d4702006-07-03 00:24:55 -070088#endif
Ingo Molnar408894e2006-01-09 15:59:20 -080089 lock->magic = lock;
90}
91
92/***
93 * mutex_destroy - mark a mutex unusable
94 * @lock: the mutex to be destroyed
95 *
96 * This function marks the mutex uninitialized, and any subsequent
97 * use of the mutex is forbidden. The mutex must not be locked when
98 * this function is called.
99 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800100void mutex_destroy(struct mutex *lock)
Ingo Molnar408894e2006-01-09 15:59:20 -0800101{
Ingo Molnar9e7f4d42006-07-03 00:24:30 -0700102 DEBUG_LOCKS_WARN_ON(mutex_is_locked(lock));
Ingo Molnar408894e2006-01-09 15:59:20 -0800103 lock->magic = NULL;
104}
105
106EXPORT_SYMBOL_GPL(mutex_destroy);