aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIngo Molnar <mingo@elte.hu>2006-07-03 03:24:55 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2006-07-03 18:27:04 -0400
commitef5d4707b9065c0cf8a69fa3716893f3b75201ba (patch)
tree9ec92f31356bf404486c1b26df9fa40bd784f983
parent8a25d5debff2daee280e83e09d8c25d67c26a972 (diff)
[PATCH] lockdep: prove mutex locking correctness
Use the lock validator framework to prove mutex locking correctness. Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Arjan van de Ven <arjan@linux.intel.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
-rw-r--r--include/linux/mutex-debug.h8
-rw-r--r--include/linux/mutex.h31
-rw-r--r--kernel/mutex-debug.c6
-rw-r--r--kernel/mutex.c28
-rw-r--r--kernel/mutex.h2
5 files changed, 63 insertions, 12 deletions
diff --git a/include/linux/mutex-debug.h b/include/linux/mutex-debug.h
index 70a26091fc73..2537285e1064 100644
--- a/include/linux/mutex-debug.h
+++ b/include/linux/mutex-debug.h
@@ -2,6 +2,7 @@
2#define __LINUX_MUTEX_DEBUG_H 2#define __LINUX_MUTEX_DEBUG_H
3 3
4#include <linux/linkage.h> 4#include <linux/linkage.h>
5#include <linux/lockdep.h>
5 6
6/* 7/*
7 * Mutexes - debugging helpers: 8 * Mutexes - debugging helpers:
@@ -10,7 +11,12 @@
10#define __DEBUG_MUTEX_INITIALIZER(lockname) \ 11#define __DEBUG_MUTEX_INITIALIZER(lockname) \
11 , .magic = &lockname 12 , .magic = &lockname
12 13
13#define mutex_init(sem) __mutex_init(sem, __FILE__":"#sem) 14#define mutex_init(mutex) \
15do { \
16 static struct lock_class_key __key; \
17 \
18 __mutex_init((mutex), #mutex, &__key); \
19} while (0)
14 20
15extern void FASTCALL(mutex_destroy(struct mutex *lock)); 21extern void FASTCALL(mutex_destroy(struct mutex *lock));
16 22
diff --git a/include/linux/mutex.h b/include/linux/mutex.h
index caafecd5e366..27c48daa3183 100644
--- a/include/linux/mutex.h
+++ b/include/linux/mutex.h
@@ -13,6 +13,7 @@
13#include <linux/list.h> 13#include <linux/list.h>
14#include <linux/spinlock_types.h> 14#include <linux/spinlock_types.h>
15#include <linux/linkage.h> 15#include <linux/linkage.h>
16#include <linux/lockdep.h>
16 17
17#include <asm/atomic.h> 18#include <asm/atomic.h>
18 19
@@ -53,6 +54,9 @@ struct mutex {
53 const char *name; 54 const char *name;
54 void *magic; 55 void *magic;
55#endif 56#endif
57#ifdef CONFIG_DEBUG_LOCK_ALLOC
58 struct lockdep_map dep_map;
59#endif
56}; 60};
57 61
58/* 62/*
@@ -72,20 +76,34 @@ struct mutex_waiter {
72# include <linux/mutex-debug.h> 76# include <linux/mutex-debug.h>
73#else 77#else
74# define __DEBUG_MUTEX_INITIALIZER(lockname) 78# define __DEBUG_MUTEX_INITIALIZER(lockname)
75# define mutex_init(mutex) __mutex_init(mutex, NULL) 79# define mutex_init(mutex) \
80do { \
81 static struct lock_class_key __key; \
82 \
83 __mutex_init((mutex), #mutex, &__key); \
84} while (0)
76# define mutex_destroy(mutex) do { } while (0) 85# define mutex_destroy(mutex) do { } while (0)
77#endif 86#endif
78 87
88#ifdef CONFIG_DEBUG_LOCK_ALLOC
89# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
90 , .dep_map = { .name = #lockname }
91#else
92# define __DEP_MAP_MUTEX_INITIALIZER(lockname)
93#endif
94
79#define __MUTEX_INITIALIZER(lockname) \ 95#define __MUTEX_INITIALIZER(lockname) \
80 { .count = ATOMIC_INIT(1) \ 96 { .count = ATOMIC_INIT(1) \
81 , .wait_lock = SPIN_LOCK_UNLOCKED \ 97 , .wait_lock = SPIN_LOCK_UNLOCKED \
82 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \ 98 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
83 __DEBUG_MUTEX_INITIALIZER(lockname) } 99 __DEBUG_MUTEX_INITIALIZER(lockname) \
100 __DEP_MAP_MUTEX_INITIALIZER(lockname) }
84 101
85#define DEFINE_MUTEX(mutexname) \ 102#define DEFINE_MUTEX(mutexname) \
86 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname) 103 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
87 104
88extern void fastcall __mutex_init(struct mutex *lock, const char *name); 105extern void __mutex_init(struct mutex *lock, const char *name,
106 struct lock_class_key *key);
89 107
90/*** 108/***
91 * mutex_is_locked - is the mutex locked 109 * mutex_is_locked - is the mutex locked
@@ -104,6 +122,13 @@ static inline int fastcall mutex_is_locked(struct mutex *lock)
104 */ 122 */
105extern void fastcall mutex_lock(struct mutex *lock); 123extern void fastcall mutex_lock(struct mutex *lock);
106extern int fastcall mutex_lock_interruptible(struct mutex *lock); 124extern int fastcall mutex_lock_interruptible(struct mutex *lock);
125
126#ifdef CONFIG_DEBUG_LOCK_ALLOC
127extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
128#else
129# define mutex_lock_nested(lock, subclass) mutex_lock(lock)
130#endif
131
107/* 132/*
108 * NOTE: mutex_trylock() follows the spin_trylock() convention, 133 * NOTE: mutex_trylock() follows the spin_trylock() convention,
109 * not the down_trylock() convention! 134 * not the down_trylock() convention!
diff --git a/kernel/mutex-debug.c b/kernel/mutex-debug.c
index 5569766a1ea2..e3203c654dda 100644
--- a/kernel/mutex-debug.c
+++ b/kernel/mutex-debug.c
@@ -83,12 +83,16 @@ void debug_mutex_unlock(struct mutex *lock)
83 DEBUG_LOCKS_WARN_ON(lock->owner != current_thread_info()); 83 DEBUG_LOCKS_WARN_ON(lock->owner != current_thread_info());
84} 84}
85 85
86void debug_mutex_init(struct mutex *lock, const char *name) 86void debug_mutex_init(struct mutex *lock, const char *name,
87 struct lock_class_key *key)
87{ 88{
89#ifdef CONFIG_DEBUG_LOCK_ALLOC
88 /* 90 /*
89 * Make sure we are not reinitializing a held lock: 91 * Make sure we are not reinitializing a held lock:
90 */ 92 */
91 debug_check_no_locks_freed((void *)lock, sizeof(*lock)); 93 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
94 lockdep_init_map(&lock->dep_map, name, key);
95#endif
92 lock->owner = NULL; 96 lock->owner = NULL;
93 lock->magic = lock; 97 lock->magic = lock;
94} 98}
diff --git a/kernel/mutex.c b/kernel/mutex.c
index 43a50c18701a..8c71cf72a497 100644
--- a/kernel/mutex.c
+++ b/kernel/mutex.c
@@ -39,13 +39,14 @@
39 * 39 *
40 * It is not allowed to initialize an already locked mutex. 40 * It is not allowed to initialize an already locked mutex.
41 */ 41 */
42__always_inline void fastcall __mutex_init(struct mutex *lock, const char *name) 42void
43__mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
43{ 44{
44 atomic_set(&lock->count, 1); 45 atomic_set(&lock->count, 1);
45 spin_lock_init(&lock->wait_lock); 46 spin_lock_init(&lock->wait_lock);
46 INIT_LIST_HEAD(&lock->wait_list); 47 INIT_LIST_HEAD(&lock->wait_list);
47 48
48 debug_mutex_init(lock, name); 49 debug_mutex_init(lock, name, key);
49} 50}
50 51
51EXPORT_SYMBOL(__mutex_init); 52EXPORT_SYMBOL(__mutex_init);
@@ -131,6 +132,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass)
131 spin_lock_mutex(&lock->wait_lock, flags); 132 spin_lock_mutex(&lock->wait_lock, flags);
132 133
133 debug_mutex_lock_common(lock, &waiter); 134 debug_mutex_lock_common(lock, &waiter);
135 mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
134 debug_mutex_add_waiter(lock, &waiter, task->thread_info); 136 debug_mutex_add_waiter(lock, &waiter, task->thread_info);
135 137
136 /* add waiting tasks to the end of the waitqueue (FIFO): */ 138 /* add waiting tasks to the end of the waitqueue (FIFO): */
@@ -158,6 +160,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass)
158 if (unlikely(state == TASK_INTERRUPTIBLE && 160 if (unlikely(state == TASK_INTERRUPTIBLE &&
159 signal_pending(task))) { 161 signal_pending(task))) {
160 mutex_remove_waiter(lock, &waiter, task->thread_info); 162 mutex_remove_waiter(lock, &waiter, task->thread_info);
163 mutex_release(&lock->dep_map, 1, _RET_IP_);
161 spin_unlock_mutex(&lock->wait_lock, flags); 164 spin_unlock_mutex(&lock->wait_lock, flags);
162 165
163 debug_mutex_free_waiter(&waiter); 166 debug_mutex_free_waiter(&waiter);
@@ -194,16 +197,28 @@ __mutex_lock_slowpath(atomic_t *lock_count)
194 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0); 197 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0);
195} 198}
196 199
200#ifdef CONFIG_DEBUG_LOCK_ALLOC
201void __sched
202mutex_lock_nested(struct mutex *lock, unsigned int subclass)
203{
204 might_sleep();
205 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass);
206}
207
208EXPORT_SYMBOL_GPL(mutex_lock_nested);
209#endif
210
197/* 211/*
198 * Release the lock, slowpath: 212 * Release the lock, slowpath:
199 */ 213 */
200static fastcall inline void 214static fastcall inline void
201__mutex_unlock_common_slowpath(atomic_t *lock_count) 215__mutex_unlock_common_slowpath(atomic_t *lock_count, int nested)
202{ 216{
203 struct mutex *lock = container_of(lock_count, struct mutex, count); 217 struct mutex *lock = container_of(lock_count, struct mutex, count);
204 unsigned long flags; 218 unsigned long flags;
205 219
206 spin_lock_mutex(&lock->wait_lock, flags); 220 spin_lock_mutex(&lock->wait_lock, flags);
221 mutex_release(&lock->dep_map, nested, _RET_IP_);
207 debug_mutex_unlock(lock); 222 debug_mutex_unlock(lock);
208 223
209 /* 224 /*
@@ -236,7 +251,7 @@ __mutex_unlock_common_slowpath(atomic_t *lock_count)
236static fastcall noinline void 251static fastcall noinline void
237__mutex_unlock_slowpath(atomic_t *lock_count) 252__mutex_unlock_slowpath(atomic_t *lock_count)
238{ 253{
239 __mutex_unlock_common_slowpath(lock_count); 254 __mutex_unlock_common_slowpath(lock_count, 1);
240} 255}
241 256
242/* 257/*
@@ -287,9 +302,10 @@ static inline int __mutex_trylock_slowpath(atomic_t *lock_count)
287 spin_lock_mutex(&lock->wait_lock, flags); 302 spin_lock_mutex(&lock->wait_lock, flags);
288 303
289 prev = atomic_xchg(&lock->count, -1); 304 prev = atomic_xchg(&lock->count, -1);
290 if (likely(prev == 1)) 305 if (likely(prev == 1)) {
291 debug_mutex_set_owner(lock, current_thread_info()); 306 debug_mutex_set_owner(lock, current_thread_info());
292 307 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
308 }
293 /* Set it back to 0 if there are no waiters: */ 309 /* Set it back to 0 if there are no waiters: */
294 if (likely(list_empty(&lock->wait_list))) 310 if (likely(list_empty(&lock->wait_list)))
295 atomic_set(&lock->count, 0); 311 atomic_set(&lock->count, 0);
diff --git a/kernel/mutex.h b/kernel/mutex.h
index aeb2d916aa0e..a075dafbb290 100644
--- a/kernel/mutex.h
+++ b/kernel/mutex.h
@@ -22,7 +22,7 @@
22#define debug_mutex_free_waiter(waiter) do { } while (0) 22#define debug_mutex_free_waiter(waiter) do { } while (0)
23#define debug_mutex_add_waiter(lock, waiter, ti) do { } while (0) 23#define debug_mutex_add_waiter(lock, waiter, ti) do { } while (0)
24#define debug_mutex_unlock(lock) do { } while (0) 24#define debug_mutex_unlock(lock) do { } while (0)
25#define debug_mutex_init(lock, name) do { } while (0) 25#define debug_mutex_init(lock, name, key) do { } while (0)
26 26
27static inline void 27static inline void
28debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter) 28debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)