Skip to content

Commit ef5d470

Browse files
Ingo MolnarLinus Torvalds
authored andcommitted
[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>
1 parent 8a25d5d commit ef5d470

5 files changed

Lines changed: 63 additions & 12 deletions

File tree

‎include/linux/mutex-debug.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define __LINUX_MUTEX_DEBUG_H
33

44
#include <linux/linkage.h>
5+
#include <linux/lockdep.h>
56

67
/*
78
* Mutexes - debugging helpers:
@@ -10,7 +11,12 @@
1011
#define __DEBUG_MUTEX_INITIALIZER(lockname) \
1112
, .magic = &lockname
1213

13-
#define mutex_init(sem) __mutex_init(sem, __FILE__":"#sem)
14+
#define mutex_init(mutex) \
15+
do { \
16+
static struct lock_class_key __key; \
17+
\
18+
__mutex_init((mutex), #mutex, &__key); \
19+
} while (0)
1420

1521
extern void FASTCALL(mutex_destroy(struct mutex *lock));
1622

‎include/linux/mutex.h‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/list.h>
1414
#include <linux/spinlock_types.h>
1515
#include <linux/linkage.h>
16+
#include <linux/lockdep.h>
1617

1718
#include <asm/atomic.h>
1819

@@ -53,6 +54,9 @@ struct mutex {
5354
const char *name;
5455
void *magic;
5556
#endif
57+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
58+
struct lockdep_map dep_map;
59+
#endif
5660
};
5761

5862
/*
@@ -72,20 +76,34 @@ struct mutex_waiter {
7276
# include <linux/mutex-debug.h>
7377
#else
7478
# define __DEBUG_MUTEX_INITIALIZER(lockname)
75-
# define mutex_init(mutex) __mutex_init(mutex, NULL)
79+
# define mutex_init(mutex) \
80+
do { \
81+
static struct lock_class_key __key; \
82+
\
83+
__mutex_init((mutex), #mutex, &__key); \
84+
} while (0)
7685
# define mutex_destroy(mutex) do { } while (0)
7786
#endif
7887

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+
7995
#define __MUTEX_INITIALIZER(lockname) \
8096
{ .count = ATOMIC_INIT(1) \
8197
, .wait_lock = SPIN_LOCK_UNLOCKED \
8298
, .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
83-
__DEBUG_MUTEX_INITIALIZER(lockname) }
99+
__DEBUG_MUTEX_INITIALIZER(lockname) \
100+
__DEP_MAP_MUTEX_INITIALIZER(lockname) }
84101

85102
#define DEFINE_MUTEX(mutexname) \
86103
struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
87104

88-
extern void fastcall __mutex_init(struct mutex *lock, const char *name);
105+
extern void __mutex_init(struct mutex *lock, const char *name,
106+
struct lock_class_key *key);
89107

90108
/***
91109
* mutex_is_locked - is the mutex locked
@@ -104,6 +122,13 @@ static inline int fastcall mutex_is_locked(struct mutex *lock)
104122
*/
105123
extern void fastcall mutex_lock(struct mutex *lock);
106124
extern int fastcall mutex_lock_interruptible(struct mutex *lock);
125+
126+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
127+
extern 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+
107132
/*
108133
* NOTE: mutex_trylock() follows the spin_trylock() convention,
109134
* not the down_trylock() convention!

‎kernel/mutex-debug.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,16 @@ void debug_mutex_unlock(struct mutex *lock)
8383
DEBUG_LOCKS_WARN_ON(lock->owner != current_thread_info());
8484
}
8585

86-
void debug_mutex_init(struct mutex *lock, const char *name)
86+
void debug_mutex_init(struct mutex *lock, const char *name,
87+
struct lock_class_key *key)
8788
{
89+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
8890
/*
8991
* Make sure we are not reinitializing a held lock:
9092
*/
9193
debug_check_no_locks_freed((void *)lock, sizeof(*lock));
94+
lockdep_init_map(&lock->dep_map, name, key);
95+
#endif
9296
lock->owner = NULL;
9397
lock->magic = lock;
9498
}

‎kernel/mutex.c‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@
3939
*
4040
* It is not allowed to initialize an already locked mutex.
4141
*/
42-
__always_inline void fastcall __mutex_init(struct mutex *lock, const char *name)
42+
void
43+
__mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
4344
{
4445
atomic_set(&lock->count, 1);
4546
spin_lock_init(&lock->wait_lock);
4647
INIT_LIST_HEAD(&lock->wait_list);
4748

48-
debug_mutex_init(lock, name);
49+
debug_mutex_init(lock, name, key);
4950
}
5051

5152
EXPORT_SYMBOL(__mutex_init);
@@ -131,6 +132,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass)
131132
spin_lock_mutex(&lock->wait_lock, flags);
132133

133134
debug_mutex_lock_common(lock, &waiter);
135+
mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
134136
debug_mutex_add_waiter(lock, &waiter, task->thread_info);
135137

136138
/* 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)
158160
if (unlikely(state == TASK_INTERRUPTIBLE &&
159161
signal_pending(task))) {
160162
mutex_remove_waiter(lock, &waiter, task->thread_info);
163+
mutex_release(&lock->dep_map, 1, _RET_IP_);
161164
spin_unlock_mutex(&lock->wait_lock, flags);
162165

163166
debug_mutex_free_waiter(&waiter);
@@ -194,16 +197,28 @@ __mutex_lock_slowpath(atomic_t *lock_count)
194197
__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0);
195198
}
196199

200+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
201+
void __sched
202+
mutex_lock_nested(struct mutex *lock, unsigned int subclass)
203+
{
204+
might_sleep();
205+
__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass);
206+
}
207+
208+
EXPORT_SYMBOL_GPL(mutex_lock_nested);
209+
#endif
210+
197211
/*
198212
* Release the lock, slowpath:
199213
*/
200214
static fastcall inline void
201-
__mutex_unlock_common_slowpath(atomic_t *lock_count)
215+
__mutex_unlock_common_slowpath(atomic_t *lock_count, int nested)
202216
{
203217
struct mutex *lock = container_of(lock_count, struct mutex, count);
204218
unsigned long flags;
205219

206220
spin_lock_mutex(&lock->wait_lock, flags);
221+
mutex_release(&lock->dep_map, nested, _RET_IP_);
207222
debug_mutex_unlock(lock);
208223

209224
/*
@@ -236,7 +251,7 @@ __mutex_unlock_common_slowpath(atomic_t *lock_count)
236251
static fastcall noinline void
237252
__mutex_unlock_slowpath(atomic_t *lock_count)
238253
{
239-
__mutex_unlock_common_slowpath(lock_count);
254+
__mutex_unlock_common_slowpath(lock_count, 1);
240255
}
241256

242257
/*
@@ -287,9 +302,10 @@ static inline int __mutex_trylock_slowpath(atomic_t *lock_count)
287302
spin_lock_mutex(&lock->wait_lock, flags);
288303

289304
prev = atomic_xchg(&lock->count, -1);
290-
if (likely(prev == 1))
305+
if (likely(prev == 1)) {
291306
debug_mutex_set_owner(lock, current_thread_info());
292-
307+
mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
308+
}
293309
/* Set it back to 0 if there are no waiters: */
294310
if (likely(list_empty(&lock->wait_list)))
295311
atomic_set(&lock->count, 0);

‎kernel/mutex.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#define debug_mutex_free_waiter(waiter) do { } while (0)
2323
#define debug_mutex_add_waiter(lock, waiter, ti) do { } while (0)
2424
#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)
2626

2727
static inline void
2828
debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)

0 commit comments

Comments
 (0)