Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Debugging code for mutexes
0003  *
0004  * Started by Ingo Molnar:
0005  *
0006  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
0007  *
0008  * lock debugging, locking tree, deadlock detection started by:
0009  *
0010  *  Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
0011  *  Released under the General Public License (GPL).
0012  */
0013 #include <linux/mutex.h>
0014 #include <linux/delay.h>
0015 #include <linux/export.h>
0016 #include <linux/poison.h>
0017 #include <linux/sched.h>
0018 #include <linux/spinlock.h>
0019 #include <linux/kallsyms.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/debug_locks.h>
0022 
0023 #include "mutex.h"
0024 
0025 /*
0026  * Must be called with lock->wait_lock held.
0027  */
0028 void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)
0029 {
0030     memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
0031     waiter->magic = waiter;
0032     INIT_LIST_HEAD(&waiter->list);
0033     waiter->ww_ctx = MUTEX_POISON_WW_CTX;
0034 }
0035 
0036 void debug_mutex_wake_waiter(struct mutex *lock, struct mutex_waiter *waiter)
0037 {
0038     lockdep_assert_held(&lock->wait_lock);
0039     DEBUG_LOCKS_WARN_ON(list_empty(&lock->wait_list));
0040     DEBUG_LOCKS_WARN_ON(waiter->magic != waiter);
0041     DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
0042 }
0043 
0044 void debug_mutex_free_waiter(struct mutex_waiter *waiter)
0045 {
0046     DEBUG_LOCKS_WARN_ON(!list_empty(&waiter->list));
0047     memset(waiter, MUTEX_DEBUG_FREE, sizeof(*waiter));
0048 }
0049 
0050 void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
0051                 struct task_struct *task)
0052 {
0053     lockdep_assert_held(&lock->wait_lock);
0054 
0055     /* Mark the current thread as blocked on the lock: */
0056     task->blocked_on = waiter;
0057 }
0058 
0059 void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
0060              struct task_struct *task)
0061 {
0062     DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
0063     DEBUG_LOCKS_WARN_ON(waiter->task != task);
0064     DEBUG_LOCKS_WARN_ON(task->blocked_on != waiter);
0065     task->blocked_on = NULL;
0066 
0067     INIT_LIST_HEAD(&waiter->list);
0068     waiter->task = NULL;
0069 }
0070 
0071 void debug_mutex_unlock(struct mutex *lock)
0072 {
0073     if (likely(debug_locks)) {
0074         DEBUG_LOCKS_WARN_ON(lock->magic != lock);
0075         DEBUG_LOCKS_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next);
0076     }
0077 }
0078 
0079 void debug_mutex_init(struct mutex *lock, const char *name,
0080               struct lock_class_key *key)
0081 {
0082 #ifdef CONFIG_DEBUG_LOCK_ALLOC
0083     /*
0084      * Make sure we are not reinitializing a held lock:
0085      */
0086     debug_check_no_locks_freed((void *)lock, sizeof(*lock));
0087     lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_SLEEP);
0088 #endif
0089     lock->magic = lock;
0090 }
0091 
0092 /***
0093  * mutex_destroy - mark a mutex unusable
0094  * @lock: the mutex to be destroyed
0095  *
0096  * This function marks the mutex uninitialized, and any subsequent
0097  * use of the mutex is forbidden. The mutex must not be locked when
0098  * this function is called.
0099  */
0100 void mutex_destroy(struct mutex *lock)
0101 {
0102     DEBUG_LOCKS_WARN_ON(mutex_is_locked(lock));
0103     lock->magic = NULL;
0104 }
0105 
0106 EXPORT_SYMBOL_GPL(mutex_destroy);