Commit 1e6c3e8f8fb94a8914a380e02a7e8ad81d47273e

Authored by Linus Torvalds

Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull locking fixes from Ingo Molnar:
 "A liblockdep fix and a mutex_unlock() mutex-debugging fix"

* 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
  mutex: Always clear owner field upon mutex_unlock()
  tools/liblockdep: Fix debug_check thinko in mutex destroy

Showing 2 changed files Inline Diff

kernel/locking/mutex-debug.c
1 /* 1 /*
2 * kernel/mutex-debug.c 2 * kernel/mutex-debug.c
3 * 3 *
4 * Debugging code for mutexes 4 * Debugging code for mutexes
5 * 5 *
6 * Started by Ingo Molnar: 6 * Started by Ingo Molnar:
7 * 7 *
8 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 8 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * 9 *
10 * lock debugging, locking tree, deadlock detection started by: 10 * lock debugging, locking tree, deadlock detection started by:
11 * 11 *
12 * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey 12 * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
13 * Released under the General Public License (GPL). 13 * Released under the General Public License (GPL).
14 */ 14 */
15 #include <linux/mutex.h> 15 #include <linux/mutex.h>
16 #include <linux/delay.h> 16 #include <linux/delay.h>
17 #include <linux/export.h> 17 #include <linux/export.h>
18 #include <linux/poison.h> 18 #include <linux/poison.h>
19 #include <linux/sched.h> 19 #include <linux/sched.h>
20 #include <linux/spinlock.h> 20 #include <linux/spinlock.h>
21 #include <linux/kallsyms.h> 21 #include <linux/kallsyms.h>
22 #include <linux/interrupt.h> 22 #include <linux/interrupt.h>
23 #include <linux/debug_locks.h> 23 #include <linux/debug_locks.h>
24 24
25 #include "mutex-debug.h" 25 #include "mutex-debug.h"
26 26
27 /* 27 /*
28 * Must be called with lock->wait_lock held. 28 * Must be called with lock->wait_lock held.
29 */ 29 */
30 void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter) 30 void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)
31 { 31 {
32 memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter)); 32 memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
33 waiter->magic = waiter; 33 waiter->magic = waiter;
34 INIT_LIST_HEAD(&waiter->list); 34 INIT_LIST_HEAD(&waiter->list);
35 } 35 }
36 36
37 void debug_mutex_wake_waiter(struct mutex *lock, struct mutex_waiter *waiter) 37 void debug_mutex_wake_waiter(struct mutex *lock, struct mutex_waiter *waiter)
38 { 38 {
39 SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&lock->wait_lock)); 39 SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&lock->wait_lock));
40 DEBUG_LOCKS_WARN_ON(list_empty(&lock->wait_list)); 40 DEBUG_LOCKS_WARN_ON(list_empty(&lock->wait_list));
41 DEBUG_LOCKS_WARN_ON(waiter->magic != waiter); 41 DEBUG_LOCKS_WARN_ON(waiter->magic != waiter);
42 DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list)); 42 DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
43 } 43 }
44 44
45 void debug_mutex_free_waiter(struct mutex_waiter *waiter) 45 void debug_mutex_free_waiter(struct mutex_waiter *waiter)
46 { 46 {
47 DEBUG_LOCKS_WARN_ON(!list_empty(&waiter->list)); 47 DEBUG_LOCKS_WARN_ON(!list_empty(&waiter->list));
48 memset(waiter, MUTEX_DEBUG_FREE, sizeof(*waiter)); 48 memset(waiter, MUTEX_DEBUG_FREE, sizeof(*waiter));
49 } 49 }
50 50
51 void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter, 51 void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
52 struct thread_info *ti) 52 struct thread_info *ti)
53 { 53 {
54 SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&lock->wait_lock)); 54 SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&lock->wait_lock));
55 55
56 /* Mark the current thread as blocked on the lock: */ 56 /* Mark the current thread as blocked on the lock: */
57 ti->task->blocked_on = waiter; 57 ti->task->blocked_on = waiter;
58 } 58 }
59 59
60 void mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter, 60 void mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
61 struct thread_info *ti) 61 struct thread_info *ti)
62 { 62 {
63 DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list)); 63 DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
64 DEBUG_LOCKS_WARN_ON(waiter->task != ti->task); 64 DEBUG_LOCKS_WARN_ON(waiter->task != ti->task);
65 DEBUG_LOCKS_WARN_ON(ti->task->blocked_on != waiter); 65 DEBUG_LOCKS_WARN_ON(ti->task->blocked_on != waiter);
66 ti->task->blocked_on = NULL; 66 ti->task->blocked_on = NULL;
67 67
68 list_del_init(&waiter->list); 68 list_del_init(&waiter->list);
69 waiter->task = NULL; 69 waiter->task = NULL;
70 } 70 }
71 71
72 void debug_mutex_unlock(struct mutex *lock) 72 void debug_mutex_unlock(struct mutex *lock)
73 { 73 {
74 if (likely(debug_locks)) { 74 if (likely(debug_locks)) {
75 DEBUG_LOCKS_WARN_ON(lock->magic != lock); 75 DEBUG_LOCKS_WARN_ON(lock->magic != lock);
76 76
77 if (!lock->owner) 77 if (!lock->owner)
78 DEBUG_LOCKS_WARN_ON(!lock->owner); 78 DEBUG_LOCKS_WARN_ON(!lock->owner);
79 else 79 else
80 DEBUG_LOCKS_WARN_ON(lock->owner != current); 80 DEBUG_LOCKS_WARN_ON(lock->owner != current);
81 81
82 DEBUG_LOCKS_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next); 82 DEBUG_LOCKS_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next);
83 mutex_clear_owner(lock);
84 } 83 }
85 84
86 /* 85 /*
87 * __mutex_slowpath_needs_to_unlock() is explicitly 0 for debug 86 * __mutex_slowpath_needs_to_unlock() is explicitly 0 for debug
88 * mutexes so that we can do it here after we've verified state. 87 * mutexes so that we can do it here after we've verified state.
89 */ 88 */
89 mutex_clear_owner(lock);
90 atomic_set(&lock->count, 1); 90 atomic_set(&lock->count, 1);
91 } 91 }
92 92
93 void debug_mutex_init(struct mutex *lock, const char *name, 93 void debug_mutex_init(struct mutex *lock, const char *name,
94 struct lock_class_key *key) 94 struct lock_class_key *key)
95 { 95 {
96 #ifdef CONFIG_DEBUG_LOCK_ALLOC 96 #ifdef CONFIG_DEBUG_LOCK_ALLOC
97 /* 97 /*
98 * Make sure we are not reinitializing a held lock: 98 * Make sure we are not reinitializing a held lock:
99 */ 99 */
100 debug_check_no_locks_freed((void *)lock, sizeof(*lock)); 100 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
101 lockdep_init_map(&lock->dep_map, name, key, 0); 101 lockdep_init_map(&lock->dep_map, name, key, 0);
102 #endif 102 #endif
103 lock->magic = lock; 103 lock->magic = lock;
104 } 104 }
105 105
106 /*** 106 /***
107 * mutex_destroy - mark a mutex unusable 107 * mutex_destroy - mark a mutex unusable
108 * @lock: the mutex to be destroyed 108 * @lock: the mutex to be destroyed
109 * 109 *
110 * This function marks the mutex uninitialized, and any subsequent 110 * This function marks the mutex uninitialized, and any subsequent
111 * use of the mutex is forbidden. The mutex must not be locked when 111 * use of the mutex is forbidden. The mutex must not be locked when
112 * this function is called. 112 * this function is called.
113 */ 113 */
114 void mutex_destroy(struct mutex *lock) 114 void mutex_destroy(struct mutex *lock)
115 { 115 {
116 DEBUG_LOCKS_WARN_ON(mutex_is_locked(lock)); 116 DEBUG_LOCKS_WARN_ON(mutex_is_locked(lock));
117 lock->magic = NULL; 117 lock->magic = NULL;
118 } 118 }
119 119
120 EXPORT_SYMBOL_GPL(mutex_destroy); 120 EXPORT_SYMBOL_GPL(mutex_destroy);
tools/lib/lockdep/preload.c
1 #define _GNU_SOURCE 1 #define _GNU_SOURCE
2 #include <pthread.h> 2 #include <pthread.h>
3 #include <stdio.h> 3 #include <stdio.h>
4 #include <dlfcn.h> 4 #include <dlfcn.h>
5 #include <stdlib.h> 5 #include <stdlib.h>
6 #include <sysexits.h> 6 #include <sysexits.h>
7 #include "include/liblockdep/mutex.h" 7 #include "include/liblockdep/mutex.h"
8 #include "../../../include/linux/rbtree.h" 8 #include "../../../include/linux/rbtree.h"
9 9
10 /** 10 /**
11 * struct lock_lookup - liblockdep's view of a single unique lock 11 * struct lock_lookup - liblockdep's view of a single unique lock
12 * @orig: pointer to the original pthread lock, used for lookups 12 * @orig: pointer to the original pthread lock, used for lookups
13 * @dep_map: lockdep's dep_map structure 13 * @dep_map: lockdep's dep_map structure
14 * @key: lockdep's key structure 14 * @key: lockdep's key structure
15 * @node: rb-tree node used to store the lock in a global tree 15 * @node: rb-tree node used to store the lock in a global tree
16 * @name: a unique name for the lock 16 * @name: a unique name for the lock
17 */ 17 */
18 struct lock_lookup { 18 struct lock_lookup {
19 void *orig; /* Original pthread lock, used for lookups */ 19 void *orig; /* Original pthread lock, used for lookups */
20 struct lockdep_map dep_map; /* Since all locks are dynamic, we need 20 struct lockdep_map dep_map; /* Since all locks are dynamic, we need
21 * a dep_map and a key for each lock */ 21 * a dep_map and a key for each lock */
22 /* 22 /*
23 * Wait, there's no support for key classes? Yup :( 23 * Wait, there's no support for key classes? Yup :(
24 * Most big projects wrap the pthread api with their own calls to 24 * Most big projects wrap the pthread api with their own calls to
25 * be compatible with different locking methods. This means that 25 * be compatible with different locking methods. This means that
26 * "classes" will be brokes since the function that creates all 26 * "classes" will be brokes since the function that creates all
27 * locks will point to a generic locking function instead of the 27 * locks will point to a generic locking function instead of the
28 * actual code that wants to do the locking. 28 * actual code that wants to do the locking.
29 */ 29 */
30 struct lock_class_key key; 30 struct lock_class_key key;
31 struct rb_node node; 31 struct rb_node node;
32 #define LIBLOCKDEP_MAX_LOCK_NAME 22 32 #define LIBLOCKDEP_MAX_LOCK_NAME 22
33 char name[LIBLOCKDEP_MAX_LOCK_NAME]; 33 char name[LIBLOCKDEP_MAX_LOCK_NAME];
34 }; 34 };
35 35
36 /* This is where we store our locks */ 36 /* This is where we store our locks */
37 static struct rb_root locks = RB_ROOT; 37 static struct rb_root locks = RB_ROOT;
38 static pthread_rwlock_t locks_rwlock = PTHREAD_RWLOCK_INITIALIZER; 38 static pthread_rwlock_t locks_rwlock = PTHREAD_RWLOCK_INITIALIZER;
39 39
40 /* pthread mutex API */ 40 /* pthread mutex API */
41 41
42 #ifdef __GLIBC__ 42 #ifdef __GLIBC__
43 extern int __pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); 43 extern int __pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
44 extern int __pthread_mutex_lock(pthread_mutex_t *mutex); 44 extern int __pthread_mutex_lock(pthread_mutex_t *mutex);
45 extern int __pthread_mutex_trylock(pthread_mutex_t *mutex); 45 extern int __pthread_mutex_trylock(pthread_mutex_t *mutex);
46 extern int __pthread_mutex_unlock(pthread_mutex_t *mutex); 46 extern int __pthread_mutex_unlock(pthread_mutex_t *mutex);
47 extern int __pthread_mutex_destroy(pthread_mutex_t *mutex); 47 extern int __pthread_mutex_destroy(pthread_mutex_t *mutex);
48 #else 48 #else
49 #define __pthread_mutex_init NULL 49 #define __pthread_mutex_init NULL
50 #define __pthread_mutex_lock NULL 50 #define __pthread_mutex_lock NULL
51 #define __pthread_mutex_trylock NULL 51 #define __pthread_mutex_trylock NULL
52 #define __pthread_mutex_unlock NULL 52 #define __pthread_mutex_unlock NULL
53 #define __pthread_mutex_destroy NULL 53 #define __pthread_mutex_destroy NULL
54 #endif 54 #endif
55 static int (*ll_pthread_mutex_init)(pthread_mutex_t *mutex, 55 static int (*ll_pthread_mutex_init)(pthread_mutex_t *mutex,
56 const pthread_mutexattr_t *attr) = __pthread_mutex_init; 56 const pthread_mutexattr_t *attr) = __pthread_mutex_init;
57 static int (*ll_pthread_mutex_lock)(pthread_mutex_t *mutex) = __pthread_mutex_lock; 57 static int (*ll_pthread_mutex_lock)(pthread_mutex_t *mutex) = __pthread_mutex_lock;
58 static int (*ll_pthread_mutex_trylock)(pthread_mutex_t *mutex) = __pthread_mutex_trylock; 58 static int (*ll_pthread_mutex_trylock)(pthread_mutex_t *mutex) = __pthread_mutex_trylock;
59 static int (*ll_pthread_mutex_unlock)(pthread_mutex_t *mutex) = __pthread_mutex_unlock; 59 static int (*ll_pthread_mutex_unlock)(pthread_mutex_t *mutex) = __pthread_mutex_unlock;
60 static int (*ll_pthread_mutex_destroy)(pthread_mutex_t *mutex) = __pthread_mutex_destroy; 60 static int (*ll_pthread_mutex_destroy)(pthread_mutex_t *mutex) = __pthread_mutex_destroy;
61 61
62 /* pthread rwlock API */ 62 /* pthread rwlock API */
63 63
64 #ifdef __GLIBC__ 64 #ifdef __GLIBC__
65 extern int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr); 65 extern int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);
66 extern int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock); 66 extern int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
67 extern int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock); 67 extern int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
68 extern int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock); 68 extern int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
69 extern int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock); 69 extern int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
70 extern int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock); 70 extern int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
71 extern int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock); 71 extern int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
72 #else 72 #else
73 #define __pthread_rwlock_init NULL 73 #define __pthread_rwlock_init NULL
74 #define __pthread_rwlock_destroy NULL 74 #define __pthread_rwlock_destroy NULL
75 #define __pthread_rwlock_wrlock NULL 75 #define __pthread_rwlock_wrlock NULL
76 #define __pthread_rwlock_trywrlock NULL 76 #define __pthread_rwlock_trywrlock NULL
77 #define __pthread_rwlock_rdlock NULL 77 #define __pthread_rwlock_rdlock NULL
78 #define __pthread_rwlock_tryrdlock NULL 78 #define __pthread_rwlock_tryrdlock NULL
79 #define __pthread_rwlock_unlock NULL 79 #define __pthread_rwlock_unlock NULL
80 #endif 80 #endif
81 81
82 static int (*ll_pthread_rwlock_init)(pthread_rwlock_t *rwlock, 82 static int (*ll_pthread_rwlock_init)(pthread_rwlock_t *rwlock,
83 const pthread_rwlockattr_t *attr) = __pthread_rwlock_init; 83 const pthread_rwlockattr_t *attr) = __pthread_rwlock_init;
84 static int (*ll_pthread_rwlock_destroy)(pthread_rwlock_t *rwlock) = __pthread_rwlock_destroy; 84 static int (*ll_pthread_rwlock_destroy)(pthread_rwlock_t *rwlock) = __pthread_rwlock_destroy;
85 static int (*ll_pthread_rwlock_rdlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_rdlock; 85 static int (*ll_pthread_rwlock_rdlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_rdlock;
86 static int (*ll_pthread_rwlock_tryrdlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_tryrdlock; 86 static int (*ll_pthread_rwlock_tryrdlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_tryrdlock;
87 static int (*ll_pthread_rwlock_trywrlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_trywrlock; 87 static int (*ll_pthread_rwlock_trywrlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_trywrlock;
88 static int (*ll_pthread_rwlock_wrlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_wrlock; 88 static int (*ll_pthread_rwlock_wrlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_wrlock;
89 static int (*ll_pthread_rwlock_unlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_unlock; 89 static int (*ll_pthread_rwlock_unlock)(pthread_rwlock_t *rwlock) = __pthread_rwlock_unlock;
90 90
91 enum { none, prepare, done, } __init_state; 91 enum { none, prepare, done, } __init_state;
92 static void init_preload(void); 92 static void init_preload(void);
93 static void try_init_preload(void) 93 static void try_init_preload(void)
94 { 94 {
95 if (__init_state != done) 95 if (__init_state != done)
96 init_preload(); 96 init_preload();
97 } 97 }
98 98
99 static struct rb_node **__get_lock_node(void *lock, struct rb_node **parent) 99 static struct rb_node **__get_lock_node(void *lock, struct rb_node **parent)
100 { 100 {
101 struct rb_node **node = &locks.rb_node; 101 struct rb_node **node = &locks.rb_node;
102 struct lock_lookup *l; 102 struct lock_lookup *l;
103 103
104 *parent = NULL; 104 *parent = NULL;
105 105
106 while (*node) { 106 while (*node) {
107 l = rb_entry(*node, struct lock_lookup, node); 107 l = rb_entry(*node, struct lock_lookup, node);
108 108
109 *parent = *node; 109 *parent = *node;
110 if (lock < l->orig) 110 if (lock < l->orig)
111 node = &l->node.rb_left; 111 node = &l->node.rb_left;
112 else if (lock > l->orig) 112 else if (lock > l->orig)
113 node = &l->node.rb_right; 113 node = &l->node.rb_right;
114 else 114 else
115 return node; 115 return node;
116 } 116 }
117 117
118 return node; 118 return node;
119 } 119 }
120 120
121 #ifndef LIBLOCKDEP_STATIC_ENTRIES 121 #ifndef LIBLOCKDEP_STATIC_ENTRIES
122 #define LIBLOCKDEP_STATIC_ENTRIES 1024 122 #define LIBLOCKDEP_STATIC_ENTRIES 1024
123 #endif 123 #endif
124 124
125 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 125 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
126 126
127 static struct lock_lookup __locks[LIBLOCKDEP_STATIC_ENTRIES]; 127 static struct lock_lookup __locks[LIBLOCKDEP_STATIC_ENTRIES];
128 static int __locks_nr; 128 static int __locks_nr;
129 129
130 static inline bool is_static_lock(struct lock_lookup *lock) 130 static inline bool is_static_lock(struct lock_lookup *lock)
131 { 131 {
132 return lock >= __locks && lock < __locks + ARRAY_SIZE(__locks); 132 return lock >= __locks && lock < __locks + ARRAY_SIZE(__locks);
133 } 133 }
134 134
135 static struct lock_lookup *alloc_lock(void) 135 static struct lock_lookup *alloc_lock(void)
136 { 136 {
137 if (__init_state != done) { 137 if (__init_state != done) {
138 /* 138 /*
139 * Some programs attempt to initialize and use locks in their 139 * Some programs attempt to initialize and use locks in their
140 * allocation path. This means that a call to malloc() would 140 * allocation path. This means that a call to malloc() would
141 * result in locks being initialized and locked. 141 * result in locks being initialized and locked.
142 * 142 *
143 * Why is it an issue for us? dlsym() below will try allocating 143 * Why is it an issue for us? dlsym() below will try allocating
144 * to give us the original function. Since this allocation will 144 * to give us the original function. Since this allocation will
145 * result in a locking operations, we have to let pthread deal 145 * result in a locking operations, we have to let pthread deal
146 * with it, but we can't! we don't have the pointer to the 146 * with it, but we can't! we don't have the pointer to the
147 * original API since we're inside dlsym() trying to get it 147 * original API since we're inside dlsym() trying to get it
148 */ 148 */
149 149
150 int idx = __locks_nr++; 150 int idx = __locks_nr++;
151 if (idx >= ARRAY_SIZE(__locks)) { 151 if (idx >= ARRAY_SIZE(__locks)) {
152 fprintf(stderr, 152 fprintf(stderr,
153 "LOCKDEP error: insufficient LIBLOCKDEP_STATIC_ENTRIES\n"); 153 "LOCKDEP error: insufficient LIBLOCKDEP_STATIC_ENTRIES\n");
154 exit(EX_UNAVAILABLE); 154 exit(EX_UNAVAILABLE);
155 } 155 }
156 return __locks + idx; 156 return __locks + idx;
157 } 157 }
158 158
159 return malloc(sizeof(struct lock_lookup)); 159 return malloc(sizeof(struct lock_lookup));
160 } 160 }
161 161
162 static inline void free_lock(struct lock_lookup *lock) 162 static inline void free_lock(struct lock_lookup *lock)
163 { 163 {
164 if (likely(!is_static_lock(lock))) 164 if (likely(!is_static_lock(lock)))
165 free(lock); 165 free(lock);
166 } 166 }
167 167
168 /** 168 /**
169 * __get_lock - find or create a lock instance 169 * __get_lock - find or create a lock instance
170 * @lock: pointer to a pthread lock function 170 * @lock: pointer to a pthread lock function
171 * 171 *
172 * Try to find an existing lock in the rbtree using the provided pointer. If 172 * Try to find an existing lock in the rbtree using the provided pointer. If
173 * one wasn't found - create it. 173 * one wasn't found - create it.
174 */ 174 */
175 static struct lock_lookup *__get_lock(void *lock) 175 static struct lock_lookup *__get_lock(void *lock)
176 { 176 {
177 struct rb_node **node, *parent; 177 struct rb_node **node, *parent;
178 struct lock_lookup *l; 178 struct lock_lookup *l;
179 179
180 ll_pthread_rwlock_rdlock(&locks_rwlock); 180 ll_pthread_rwlock_rdlock(&locks_rwlock);
181 node = __get_lock_node(lock, &parent); 181 node = __get_lock_node(lock, &parent);
182 ll_pthread_rwlock_unlock(&locks_rwlock); 182 ll_pthread_rwlock_unlock(&locks_rwlock);
183 if (*node) { 183 if (*node) {
184 return rb_entry(*node, struct lock_lookup, node); 184 return rb_entry(*node, struct lock_lookup, node);
185 } 185 }
186 186
187 /* We didn't find the lock, let's create it */ 187 /* We didn't find the lock, let's create it */
188 l = alloc_lock(); 188 l = alloc_lock();
189 if (l == NULL) 189 if (l == NULL)
190 return NULL; 190 return NULL;
191 191
192 l->orig = lock; 192 l->orig = lock;
193 /* 193 /*
194 * Currently the name of the lock is the ptr value of the pthread lock, 194 * Currently the name of the lock is the ptr value of the pthread lock,
195 * while not optimal, it makes debugging a bit easier. 195 * while not optimal, it makes debugging a bit easier.
196 * 196 *
197 * TODO: Get the real name of the lock using libdwarf 197 * TODO: Get the real name of the lock using libdwarf
198 */ 198 */
199 sprintf(l->name, "%p", lock); 199 sprintf(l->name, "%p", lock);
200 lockdep_init_map(&l->dep_map, l->name, &l->key, 0); 200 lockdep_init_map(&l->dep_map, l->name, &l->key, 0);
201 201
202 ll_pthread_rwlock_wrlock(&locks_rwlock); 202 ll_pthread_rwlock_wrlock(&locks_rwlock);
203 /* This might have changed since the last time we fetched it */ 203 /* This might have changed since the last time we fetched it */
204 node = __get_lock_node(lock, &parent); 204 node = __get_lock_node(lock, &parent);
205 rb_link_node(&l->node, parent, node); 205 rb_link_node(&l->node, parent, node);
206 rb_insert_color(&l->node, &locks); 206 rb_insert_color(&l->node, &locks);
207 ll_pthread_rwlock_unlock(&locks_rwlock); 207 ll_pthread_rwlock_unlock(&locks_rwlock);
208 208
209 return l; 209 return l;
210 } 210 }
211 211
212 static void __del_lock(struct lock_lookup *lock) 212 static void __del_lock(struct lock_lookup *lock)
213 { 213 {
214 ll_pthread_rwlock_wrlock(&locks_rwlock); 214 ll_pthread_rwlock_wrlock(&locks_rwlock);
215 rb_erase(&lock->node, &locks); 215 rb_erase(&lock->node, &locks);
216 ll_pthread_rwlock_unlock(&locks_rwlock); 216 ll_pthread_rwlock_unlock(&locks_rwlock);
217 free_lock(lock); 217 free_lock(lock);
218 } 218 }
219 219
220 int pthread_mutex_init(pthread_mutex_t *mutex, 220 int pthread_mutex_init(pthread_mutex_t *mutex,
221 const pthread_mutexattr_t *attr) 221 const pthread_mutexattr_t *attr)
222 { 222 {
223 int r; 223 int r;
224 224
225 /* 225 /*
226 * We keep trying to init our preload module because there might be 226 * We keep trying to init our preload module because there might be
227 * code in init sections that tries to touch locks before we are 227 * code in init sections that tries to touch locks before we are
228 * initialized, in that case we'll need to manually call preload 228 * initialized, in that case we'll need to manually call preload
229 * to get us going. 229 * to get us going.
230 * 230 *
231 * Funny enough, kernel's lockdep had the same issue, and used 231 * Funny enough, kernel's lockdep had the same issue, and used
232 * (almost) the same solution. See look_up_lock_class() in 232 * (almost) the same solution. See look_up_lock_class() in
233 * kernel/locking/lockdep.c for details. 233 * kernel/locking/lockdep.c for details.
234 */ 234 */
235 try_init_preload(); 235 try_init_preload();
236 236
237 r = ll_pthread_mutex_init(mutex, attr); 237 r = ll_pthread_mutex_init(mutex, attr);
238 if (r == 0) 238 if (r == 0)
239 /* 239 /*
240 * We do a dummy initialization here so that lockdep could 240 * We do a dummy initialization here so that lockdep could
241 * warn us if something fishy is going on - such as 241 * warn us if something fishy is going on - such as
242 * initializing a held lock. 242 * initializing a held lock.
243 */ 243 */
244 __get_lock(mutex); 244 __get_lock(mutex);
245 245
246 return r; 246 return r;
247 } 247 }
248 248
249 int pthread_mutex_lock(pthread_mutex_t *mutex) 249 int pthread_mutex_lock(pthread_mutex_t *mutex)
250 { 250 {
251 int r; 251 int r;
252 252
253 try_init_preload(); 253 try_init_preload();
254 254
255 lock_acquire(&__get_lock(mutex)->dep_map, 0, 0, 0, 1, NULL, 255 lock_acquire(&__get_lock(mutex)->dep_map, 0, 0, 0, 1, NULL,
256 (unsigned long)_RET_IP_); 256 (unsigned long)_RET_IP_);
257 /* 257 /*
258 * Here's the thing with pthread mutexes: unlike the kernel variant, 258 * Here's the thing with pthread mutexes: unlike the kernel variant,
259 * they can fail. 259 * they can fail.
260 * 260 *
261 * This means that the behaviour here is a bit different from what's 261 * This means that the behaviour here is a bit different from what's
262 * going on in the kernel: there we just tell lockdep that we took the 262 * going on in the kernel: there we just tell lockdep that we took the
263 * lock before actually taking it, but here we must deal with the case 263 * lock before actually taking it, but here we must deal with the case
264 * that locking failed. 264 * that locking failed.
265 * 265 *
266 * To do that we'll "release" the lock if locking failed - this way 266 * To do that we'll "release" the lock if locking failed - this way
267 * we'll get lockdep doing the correct checks when we try to take 267 * we'll get lockdep doing the correct checks when we try to take
268 * the lock, and if that fails - we'll be back to the correct 268 * the lock, and if that fails - we'll be back to the correct
269 * state by releasing it. 269 * state by releasing it.
270 */ 270 */
271 r = ll_pthread_mutex_lock(mutex); 271 r = ll_pthread_mutex_lock(mutex);
272 if (r) 272 if (r)
273 lock_release(&__get_lock(mutex)->dep_map, 0, (unsigned long)_RET_IP_); 273 lock_release(&__get_lock(mutex)->dep_map, 0, (unsigned long)_RET_IP_);
274 274
275 return r; 275 return r;
276 } 276 }
277 277
278 int pthread_mutex_trylock(pthread_mutex_t *mutex) 278 int pthread_mutex_trylock(pthread_mutex_t *mutex)
279 { 279 {
280 int r; 280 int r;
281 281
282 try_init_preload(); 282 try_init_preload();
283 283
284 lock_acquire(&__get_lock(mutex)->dep_map, 0, 1, 0, 1, NULL, (unsigned long)_RET_IP_); 284 lock_acquire(&__get_lock(mutex)->dep_map, 0, 1, 0, 1, NULL, (unsigned long)_RET_IP_);
285 r = ll_pthread_mutex_trylock(mutex); 285 r = ll_pthread_mutex_trylock(mutex);
286 if (r) 286 if (r)
287 lock_release(&__get_lock(mutex)->dep_map, 0, (unsigned long)_RET_IP_); 287 lock_release(&__get_lock(mutex)->dep_map, 0, (unsigned long)_RET_IP_);
288 288
289 return r; 289 return r;
290 } 290 }
291 291
292 int pthread_mutex_unlock(pthread_mutex_t *mutex) 292 int pthread_mutex_unlock(pthread_mutex_t *mutex)
293 { 293 {
294 int r; 294 int r;
295 295
296 try_init_preload(); 296 try_init_preload();
297 297
298 lock_release(&__get_lock(mutex)->dep_map, 0, (unsigned long)_RET_IP_); 298 lock_release(&__get_lock(mutex)->dep_map, 0, (unsigned long)_RET_IP_);
299 /* 299 /*
300 * Just like taking a lock, only in reverse! 300 * Just like taking a lock, only in reverse!
301 * 301 *
302 * If we fail releasing the lock, tell lockdep we're holding it again. 302 * If we fail releasing the lock, tell lockdep we're holding it again.
303 */ 303 */
304 r = ll_pthread_mutex_unlock(mutex); 304 r = ll_pthread_mutex_unlock(mutex);
305 if (r) 305 if (r)
306 lock_acquire(&__get_lock(mutex)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_); 306 lock_acquire(&__get_lock(mutex)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_);
307 307
308 return r; 308 return r;
309 } 309 }
310 310
311 int pthread_mutex_destroy(pthread_mutex_t *mutex) 311 int pthread_mutex_destroy(pthread_mutex_t *mutex)
312 { 312 {
313 try_init_preload(); 313 try_init_preload();
314 314
315 /* 315 /*
316 * Let's see if we're releasing a lock that's held. 316 * Let's see if we're releasing a lock that's held.
317 * 317 *
318 * TODO: Hook into free() and add that check there as well. 318 * TODO: Hook into free() and add that check there as well.
319 */ 319 */
320 debug_check_no_locks_freed(mutex, mutex + sizeof(*mutex)); 320 debug_check_no_locks_freed(mutex, sizeof(*mutex));
321 __del_lock(__get_lock(mutex)); 321 __del_lock(__get_lock(mutex));
322 return ll_pthread_mutex_destroy(mutex); 322 return ll_pthread_mutex_destroy(mutex);
323 } 323 }
324 324
325 /* This is the rwlock part, very similar to what happened with mutex above */ 325 /* This is the rwlock part, very similar to what happened with mutex above */
326 int pthread_rwlock_init(pthread_rwlock_t *rwlock, 326 int pthread_rwlock_init(pthread_rwlock_t *rwlock,
327 const pthread_rwlockattr_t *attr) 327 const pthread_rwlockattr_t *attr)
328 { 328 {
329 int r; 329 int r;
330 330
331 try_init_preload(); 331 try_init_preload();
332 332
333 r = ll_pthread_rwlock_init(rwlock, attr); 333 r = ll_pthread_rwlock_init(rwlock, attr);
334 if (r == 0) 334 if (r == 0)
335 __get_lock(rwlock); 335 __get_lock(rwlock);
336 336
337 return r; 337 return r;
338 } 338 }
339 339
340 int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) 340 int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
341 { 341 {
342 try_init_preload(); 342 try_init_preload();
343 343
344 debug_check_no_locks_freed(rwlock, rwlock + sizeof(*rwlock)); 344 debug_check_no_locks_freed(rwlock, sizeof(*rwlock));
345 __del_lock(__get_lock(rwlock)); 345 __del_lock(__get_lock(rwlock));
346 return ll_pthread_rwlock_destroy(rwlock); 346 return ll_pthread_rwlock_destroy(rwlock);
347 } 347 }
348 348
349 int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) 349 int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
350 { 350 {
351 int r; 351 int r;
352 352
353 init_preload(); 353 init_preload();
354 354
355 lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 2, 1, NULL, (unsigned long)_RET_IP_); 355 lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 2, 1, NULL, (unsigned long)_RET_IP_);
356 r = ll_pthread_rwlock_rdlock(rwlock); 356 r = ll_pthread_rwlock_rdlock(rwlock);
357 if (r) 357 if (r)
358 lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_); 358 lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_);
359 359
360 return r; 360 return r;
361 } 361 }
362 362
363 int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock) 363 int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
364 { 364 {
365 int r; 365 int r;
366 366
367 init_preload(); 367 init_preload();
368 368
369 lock_acquire(&__get_lock(rwlock)->dep_map, 0, 1, 2, 1, NULL, (unsigned long)_RET_IP_); 369 lock_acquire(&__get_lock(rwlock)->dep_map, 0, 1, 2, 1, NULL, (unsigned long)_RET_IP_);
370 r = ll_pthread_rwlock_tryrdlock(rwlock); 370 r = ll_pthread_rwlock_tryrdlock(rwlock);
371 if (r) 371 if (r)
372 lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_); 372 lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_);
373 373
374 return r; 374 return r;
375 } 375 }
376 376
377 int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock) 377 int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
378 { 378 {
379 int r; 379 int r;
380 380
381 init_preload(); 381 init_preload();
382 382
383 lock_acquire(&__get_lock(rwlock)->dep_map, 0, 1, 0, 1, NULL, (unsigned long)_RET_IP_); 383 lock_acquire(&__get_lock(rwlock)->dep_map, 0, 1, 0, 1, NULL, (unsigned long)_RET_IP_);
384 r = ll_pthread_rwlock_trywrlock(rwlock); 384 r = ll_pthread_rwlock_trywrlock(rwlock);
385 if (r) 385 if (r)
386 lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_); 386 lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_);
387 387
388 return r; 388 return r;
389 } 389 }
390 390
391 int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) 391 int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
392 { 392 {
393 int r; 393 int r;
394 394
395 init_preload(); 395 init_preload();
396 396
397 lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_); 397 lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_);
398 r = ll_pthread_rwlock_wrlock(rwlock); 398 r = ll_pthread_rwlock_wrlock(rwlock);
399 if (r) 399 if (r)
400 lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_); 400 lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_);
401 401
402 return r; 402 return r;
403 } 403 }
404 404
405 int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) 405 int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
406 { 406 {
407 int r; 407 int r;
408 408
409 init_preload(); 409 init_preload();
410 410
411 lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_); 411 lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_);
412 r = ll_pthread_rwlock_unlock(rwlock); 412 r = ll_pthread_rwlock_unlock(rwlock);
413 if (r) 413 if (r)
414 lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_); 414 lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_);
415 415
416 return r; 416 return r;
417 } 417 }
418 418
419 __attribute__((constructor)) static void init_preload(void) 419 __attribute__((constructor)) static void init_preload(void)
420 { 420 {
421 if (__init_state == done) 421 if (__init_state == done)
422 return; 422 return;
423 423
424 #ifndef __GLIBC__ 424 #ifndef __GLIBC__
425 __init_state = prepare; 425 __init_state = prepare;
426 426
427 ll_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init"); 427 ll_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init");
428 ll_pthread_mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock"); 428 ll_pthread_mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock");
429 ll_pthread_mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock"); 429 ll_pthread_mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock");
430 ll_pthread_mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock"); 430 ll_pthread_mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
431 ll_pthread_mutex_destroy = dlsym(RTLD_NEXT, "pthread_mutex_destroy"); 431 ll_pthread_mutex_destroy = dlsym(RTLD_NEXT, "pthread_mutex_destroy");
432 432
433 ll_pthread_rwlock_init = dlsym(RTLD_NEXT, "pthread_rwlock_init"); 433 ll_pthread_rwlock_init = dlsym(RTLD_NEXT, "pthread_rwlock_init");
434 ll_pthread_rwlock_destroy = dlsym(RTLD_NEXT, "pthread_rwlock_destroy"); 434 ll_pthread_rwlock_destroy = dlsym(RTLD_NEXT, "pthread_rwlock_destroy");
435 ll_pthread_rwlock_rdlock = dlsym(RTLD_NEXT, "pthread_rwlock_rdlock"); 435 ll_pthread_rwlock_rdlock = dlsym(RTLD_NEXT, "pthread_rwlock_rdlock");
436 ll_pthread_rwlock_tryrdlock = dlsym(RTLD_NEXT, "pthread_rwlock_tryrdlock"); 436 ll_pthread_rwlock_tryrdlock = dlsym(RTLD_NEXT, "pthread_rwlock_tryrdlock");
437 ll_pthread_rwlock_wrlock = dlsym(RTLD_NEXT, "pthread_rwlock_wrlock"); 437 ll_pthread_rwlock_wrlock = dlsym(RTLD_NEXT, "pthread_rwlock_wrlock");
438 ll_pthread_rwlock_trywrlock = dlsym(RTLD_NEXT, "pthread_rwlock_trywrlock"); 438 ll_pthread_rwlock_trywrlock = dlsym(RTLD_NEXT, "pthread_rwlock_trywrlock");
439 ll_pthread_rwlock_unlock = dlsym(RTLD_NEXT, "pthread_rwlock_unlock"); 439 ll_pthread_rwlock_unlock = dlsym(RTLD_NEXT, "pthread_rwlock_unlock");
440 #endif 440 #endif
441 441
442 lockdep_init(); 442 lockdep_init();
443 443
444 __init_state = done; 444 __init_state = done;
445 } 445 }
446 446