101768b42SPeter Zijlstra /* 201768b42SPeter Zijlstra * Debugging code for mutexes 301768b42SPeter Zijlstra * 401768b42SPeter Zijlstra * Started by Ingo Molnar: 501768b42SPeter Zijlstra * 601768b42SPeter Zijlstra * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 701768b42SPeter Zijlstra * 801768b42SPeter Zijlstra * lock debugging, locking tree, deadlock detection started by: 901768b42SPeter Zijlstra * 1001768b42SPeter Zijlstra * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey 1101768b42SPeter Zijlstra * Released under the General Public License (GPL). 1201768b42SPeter Zijlstra */ 1301768b42SPeter Zijlstra #include <linux/mutex.h> 1401768b42SPeter Zijlstra #include <linux/delay.h> 1501768b42SPeter Zijlstra #include <linux/export.h> 1601768b42SPeter Zijlstra #include <linux/poison.h> 1701768b42SPeter Zijlstra #include <linux/sched.h> 1801768b42SPeter Zijlstra #include <linux/spinlock.h> 1901768b42SPeter Zijlstra #include <linux/kallsyms.h> 2001768b42SPeter Zijlstra #include <linux/interrupt.h> 2101768b42SPeter Zijlstra #include <linux/debug_locks.h> 2201768b42SPeter Zijlstra 23a321fb90SThomas Gleixner #include "mutex.h" 2401768b42SPeter Zijlstra 2501768b42SPeter Zijlstra /* 2601768b42SPeter Zijlstra * Must be called with lock->wait_lock held. 2701768b42SPeter Zijlstra */ 2801768b42SPeter Zijlstra void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter) 2901768b42SPeter Zijlstra { 3001768b42SPeter Zijlstra memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter)); 3101768b42SPeter Zijlstra waiter->magic = waiter; 3201768b42SPeter Zijlstra INIT_LIST_HEAD(&waiter->list); 33*c0afb0ffSPeter Zijlstra waiter->ww_ctx = MUTEX_POISON_WW_CTX; 3401768b42SPeter Zijlstra } 3501768b42SPeter Zijlstra 3601768b42SPeter Zijlstra void debug_mutex_wake_waiter(struct mutex *lock, struct mutex_waiter *waiter) 3701768b42SPeter Zijlstra { 3804547728SLance Roy lockdep_assert_held(&lock->wait_lock); 3901768b42SPeter Zijlstra DEBUG_LOCKS_WARN_ON(list_empty(&lock->wait_list)); 4001768b42SPeter Zijlstra DEBUG_LOCKS_WARN_ON(waiter->magic != waiter); 4101768b42SPeter Zijlstra DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list)); 4201768b42SPeter Zijlstra } 4301768b42SPeter Zijlstra 4401768b42SPeter Zijlstra void debug_mutex_free_waiter(struct mutex_waiter *waiter) 4501768b42SPeter Zijlstra { 4601768b42SPeter Zijlstra DEBUG_LOCKS_WARN_ON(!list_empty(&waiter->list)); 4701768b42SPeter Zijlstra memset(waiter, MUTEX_DEBUG_FREE, sizeof(*waiter)); 4801768b42SPeter Zijlstra } 4901768b42SPeter Zijlstra 5001768b42SPeter Zijlstra void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter, 516720a305SLinus Torvalds struct task_struct *task) 5201768b42SPeter Zijlstra { 5304547728SLance Roy lockdep_assert_held(&lock->wait_lock); 5401768b42SPeter Zijlstra 5501768b42SPeter Zijlstra /* Mark the current thread as blocked on the lock: */ 566720a305SLinus Torvalds task->blocked_on = waiter; 5701768b42SPeter Zijlstra } 5801768b42SPeter Zijlstra 593a010c49SZqiang void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter, 606720a305SLinus Torvalds struct task_struct *task) 6101768b42SPeter Zijlstra { 6201768b42SPeter Zijlstra DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list)); 636720a305SLinus Torvalds DEBUG_LOCKS_WARN_ON(waiter->task != task); 646720a305SLinus Torvalds DEBUG_LOCKS_WARN_ON(task->blocked_on != waiter); 656720a305SLinus Torvalds task->blocked_on = NULL; 6601768b42SPeter Zijlstra 673a010c49SZqiang INIT_LIST_HEAD(&waiter->list); 6801768b42SPeter Zijlstra waiter->task = NULL; 6901768b42SPeter Zijlstra } 7001768b42SPeter Zijlstra 7101768b42SPeter Zijlstra void debug_mutex_unlock(struct mutex *lock) 7201768b42SPeter Zijlstra { 73a227960fSPeter Zijlstra if (likely(debug_locks)) { 7401768b42SPeter Zijlstra DEBUG_LOCKS_WARN_ON(lock->magic != lock); 7501768b42SPeter Zijlstra DEBUG_LOCKS_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next); 76a227960fSPeter Zijlstra } 7701768b42SPeter Zijlstra } 7801768b42SPeter Zijlstra 7901768b42SPeter Zijlstra void debug_mutex_init(struct mutex *lock, const char *name, 8001768b42SPeter Zijlstra struct lock_class_key *key) 8101768b42SPeter Zijlstra { 8201768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 8301768b42SPeter Zijlstra /* 8401768b42SPeter Zijlstra * Make sure we are not reinitializing a held lock: 8501768b42SPeter Zijlstra */ 8601768b42SPeter Zijlstra debug_check_no_locks_freed((void *)lock, sizeof(*lock)); 87de8f5e4fSPeter Zijlstra lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_SLEEP); 8801768b42SPeter Zijlstra #endif 8901768b42SPeter Zijlstra lock->magic = lock; 9001768b42SPeter Zijlstra } 9101768b42SPeter Zijlstra 9201768b42SPeter Zijlstra /*** 9301768b42SPeter Zijlstra * mutex_destroy - mark a mutex unusable 9401768b42SPeter Zijlstra * @lock: the mutex to be destroyed 9501768b42SPeter Zijlstra * 9601768b42SPeter Zijlstra * This function marks the mutex uninitialized, and any subsequent 9701768b42SPeter Zijlstra * use of the mutex is forbidden. The mutex must not be locked when 9801768b42SPeter Zijlstra * this function is called. 9901768b42SPeter Zijlstra */ 10001768b42SPeter Zijlstra void mutex_destroy(struct mutex *lock) 10101768b42SPeter Zijlstra { 10201768b42SPeter Zijlstra DEBUG_LOCKS_WARN_ON(mutex_is_locked(lock)); 10301768b42SPeter Zijlstra lock->magic = NULL; 10401768b42SPeter Zijlstra } 10501768b42SPeter Zijlstra 10601768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_destroy); 107