Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * kernel/lockdep.c |
| 3 | * |
| 4 | * Runtime locking correctness validator |
| 5 | * |
| 6 | * Started by Ingo Molnar: |
| 7 | * |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 8 | * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
| 9 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 10 | * |
| 11 | * this code maps all the lock dependencies as they occur in a live kernel |
| 12 | * and will warn about the following classes of locking bugs: |
| 13 | * |
| 14 | * - lock inversion scenarios |
| 15 | * - circular lock dependencies |
| 16 | * - hardirq/softirq safe/unsafe locking bugs |
| 17 | * |
| 18 | * Bugs are reported even if the current locking scenario does not cause |
| 19 | * any deadlock at this point. |
| 20 | * |
| 21 | * I.e. if anytime in the past two locks were taken in a different order, |
| 22 | * even if it happened for another task, even if those were different |
| 23 | * locks (but of the same class as this lock), this code will detect it. |
| 24 | * |
| 25 | * Thanks to Arjan van de Ven for coming up with the initial idea of |
| 26 | * mapping lock dependencies runtime. |
| 27 | */ |
Steven Rostedt | a5e2588 | 2008-12-02 15:34:05 -0500 | [diff] [blame] | 28 | #define DISABLE_BRANCH_PROFILING |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 29 | #include <linux/mutex.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/delay.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/proc_fs.h> |
| 34 | #include <linux/seq_file.h> |
| 35 | #include <linux/spinlock.h> |
| 36 | #include <linux/kallsyms.h> |
| 37 | #include <linux/interrupt.h> |
| 38 | #include <linux/stacktrace.h> |
| 39 | #include <linux/debug_locks.h> |
| 40 | #include <linux/irqflags.h> |
Dave Jones | 99de055 | 2006-09-29 02:00:10 -0700 | [diff] [blame] | 41 | #include <linux/utsname.h> |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 42 | #include <linux/hash.h> |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 43 | #include <linux/ftrace.h> |
Peter Zijlstra | b4b136f | 2009-01-29 14:50:36 +0100 | [diff] [blame] | 44 | #include <linux/stringify.h> |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 45 | |
| 46 | #include <asm/sections.h> |
| 47 | |
| 48 | #include "lockdep_internals.h" |
| 49 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 50 | #ifdef CONFIG_PROVE_LOCKING |
| 51 | int prove_locking = 1; |
| 52 | module_param(prove_locking, int, 0644); |
| 53 | #else |
| 54 | #define prove_locking 0 |
| 55 | #endif |
| 56 | |
| 57 | #ifdef CONFIG_LOCK_STAT |
| 58 | int lock_stat = 1; |
| 59 | module_param(lock_stat, int, 0644); |
| 60 | #else |
| 61 | #define lock_stat 0 |
| 62 | #endif |
| 63 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 64 | /* |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 65 | * lockdep_lock: protects the lockdep graph, the hashes and the |
| 66 | * class/list/hash allocators. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 67 | * |
| 68 | * This is one of the rare exceptions where it's justified |
| 69 | * to use a raw spinlock - we really dont want the spinlock |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 70 | * code to recurse back into the lockdep code... |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 71 | */ |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 72 | static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED; |
| 73 | |
| 74 | static int graph_lock(void) |
| 75 | { |
| 76 | __raw_spin_lock(&lockdep_lock); |
| 77 | /* |
| 78 | * Make sure that if another CPU detected a bug while |
| 79 | * walking the graph we dont change it (while the other |
| 80 | * CPU is busy printing out stuff with the graph lock |
| 81 | * dropped already) |
| 82 | */ |
| 83 | if (!debug_locks) { |
| 84 | __raw_spin_unlock(&lockdep_lock); |
| 85 | return 0; |
| 86 | } |
Steven Rostedt | bb065af | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 87 | /* prevent any recursions within lockdep from causing deadlocks */ |
| 88 | current->lockdep_recursion++; |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | static inline int graph_unlock(void) |
| 93 | { |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 94 | if (debug_locks && !__raw_spin_is_locked(&lockdep_lock)) |
| 95 | return DEBUG_LOCKS_WARN_ON(1); |
| 96 | |
Steven Rostedt | bb065af | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 97 | current->lockdep_recursion--; |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 98 | __raw_spin_unlock(&lockdep_lock); |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Turn lock debugging off and return with 0 if it was off already, |
| 104 | * and also release the graph lock: |
| 105 | */ |
| 106 | static inline int debug_locks_off_graph_unlock(void) |
| 107 | { |
| 108 | int ret = debug_locks_off(); |
| 109 | |
| 110 | __raw_spin_unlock(&lockdep_lock); |
| 111 | |
| 112 | return ret; |
| 113 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 114 | |
| 115 | static int lockdep_initialized; |
| 116 | |
| 117 | unsigned long nr_list_entries; |
| 118 | static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES]; |
| 119 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 120 | /* |
| 121 | * All data structures here are protected by the global debug_lock. |
| 122 | * |
| 123 | * Mutex key structs only get allocated, once during bootup, and never |
| 124 | * get freed - this significantly simplifies the debugging code. |
| 125 | */ |
| 126 | unsigned long nr_lock_classes; |
| 127 | static struct lock_class lock_classes[MAX_LOCKDEP_KEYS]; |
| 128 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 129 | static inline struct lock_class *hlock_class(struct held_lock *hlock) |
| 130 | { |
| 131 | if (!hlock->class_idx) { |
| 132 | DEBUG_LOCKS_WARN_ON(1); |
| 133 | return NULL; |
| 134 | } |
| 135 | return lock_classes + hlock->class_idx - 1; |
| 136 | } |
| 137 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 138 | #ifdef CONFIG_LOCK_STAT |
| 139 | static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats); |
| 140 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 141 | static int lock_point(unsigned long points[], unsigned long ip) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 142 | { |
| 143 | int i; |
| 144 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 145 | for (i = 0; i < LOCKSTAT_POINTS; i++) { |
| 146 | if (points[i] == 0) { |
| 147 | points[i] = ip; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 148 | break; |
| 149 | } |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 150 | if (points[i] == ip) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | |
| 154 | return i; |
| 155 | } |
| 156 | |
| 157 | static void lock_time_inc(struct lock_time *lt, s64 time) |
| 158 | { |
| 159 | if (time > lt->max) |
| 160 | lt->max = time; |
| 161 | |
| 162 | if (time < lt->min || !lt->min) |
| 163 | lt->min = time; |
| 164 | |
| 165 | lt->total += time; |
| 166 | lt->nr++; |
| 167 | } |
| 168 | |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 169 | static inline void lock_time_add(struct lock_time *src, struct lock_time *dst) |
| 170 | { |
| 171 | dst->min += src->min; |
| 172 | dst->max += src->max; |
| 173 | dst->total += src->total; |
| 174 | dst->nr += src->nr; |
| 175 | } |
| 176 | |
| 177 | struct lock_class_stats lock_stats(struct lock_class *class) |
| 178 | { |
| 179 | struct lock_class_stats stats; |
| 180 | int cpu, i; |
| 181 | |
| 182 | memset(&stats, 0, sizeof(struct lock_class_stats)); |
| 183 | for_each_possible_cpu(cpu) { |
| 184 | struct lock_class_stats *pcs = |
| 185 | &per_cpu(lock_stats, cpu)[class - lock_classes]; |
| 186 | |
| 187 | for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++) |
| 188 | stats.contention_point[i] += pcs->contention_point[i]; |
| 189 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 190 | for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++) |
| 191 | stats.contending_point[i] += pcs->contending_point[i]; |
| 192 | |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 193 | lock_time_add(&pcs->read_waittime, &stats.read_waittime); |
| 194 | lock_time_add(&pcs->write_waittime, &stats.write_waittime); |
| 195 | |
| 196 | lock_time_add(&pcs->read_holdtime, &stats.read_holdtime); |
| 197 | lock_time_add(&pcs->write_holdtime, &stats.write_holdtime); |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 198 | |
| 199 | for (i = 0; i < ARRAY_SIZE(stats.bounces); i++) |
| 200 | stats.bounces[i] += pcs->bounces[i]; |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | return stats; |
| 204 | } |
| 205 | |
| 206 | void clear_lock_stats(struct lock_class *class) |
| 207 | { |
| 208 | int cpu; |
| 209 | |
| 210 | for_each_possible_cpu(cpu) { |
| 211 | struct lock_class_stats *cpu_stats = |
| 212 | &per_cpu(lock_stats, cpu)[class - lock_classes]; |
| 213 | |
| 214 | memset(cpu_stats, 0, sizeof(struct lock_class_stats)); |
| 215 | } |
| 216 | memset(class->contention_point, 0, sizeof(class->contention_point)); |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 217 | memset(class->contending_point, 0, sizeof(class->contending_point)); |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 220 | static struct lock_class_stats *get_lock_stats(struct lock_class *class) |
| 221 | { |
| 222 | return &get_cpu_var(lock_stats)[class - lock_classes]; |
| 223 | } |
| 224 | |
| 225 | static void put_lock_stats(struct lock_class_stats *stats) |
| 226 | { |
| 227 | put_cpu_var(lock_stats); |
| 228 | } |
| 229 | |
| 230 | static void lock_release_holdtime(struct held_lock *hlock) |
| 231 | { |
| 232 | struct lock_class_stats *stats; |
| 233 | s64 holdtime; |
| 234 | |
| 235 | if (!lock_stat) |
| 236 | return; |
| 237 | |
| 238 | holdtime = sched_clock() - hlock->holdtime_stamp; |
| 239 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 240 | stats = get_lock_stats(hlock_class(hlock)); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 241 | if (hlock->read) |
| 242 | lock_time_inc(&stats->read_holdtime, holdtime); |
| 243 | else |
| 244 | lock_time_inc(&stats->write_holdtime, holdtime); |
| 245 | put_lock_stats(stats); |
| 246 | } |
| 247 | #else |
| 248 | static inline void lock_release_holdtime(struct held_lock *hlock) |
| 249 | { |
| 250 | } |
| 251 | #endif |
| 252 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 253 | /* |
| 254 | * We keep a global list of all lock classes. The list only grows, |
| 255 | * never shrinks. The list is only accessed with the lockdep |
| 256 | * spinlock lock held. |
| 257 | */ |
| 258 | LIST_HEAD(all_lock_classes); |
| 259 | |
| 260 | /* |
| 261 | * The lockdep classes are in a hash-table as well, for fast lookup: |
| 262 | */ |
| 263 | #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1) |
| 264 | #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS) |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 265 | #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 266 | #define classhashentry(key) (classhash_table + __classhashfn((key))) |
| 267 | |
| 268 | static struct list_head classhash_table[CLASSHASH_SIZE]; |
| 269 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 270 | /* |
| 271 | * We put the lock dependency chains into a hash-table as well, to cache |
| 272 | * their existence: |
| 273 | */ |
| 274 | #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1) |
| 275 | #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS) |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 276 | #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 277 | #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain))) |
| 278 | |
| 279 | static struct list_head chainhash_table[CHAINHASH_SIZE]; |
| 280 | |
| 281 | /* |
| 282 | * The hash key of the lock dependency chains is a hash itself too: |
| 283 | * it's a hash of all locks taken up to that lock, including that lock. |
| 284 | * It's a 64-bit hash, because it's important for the keys to be |
| 285 | * unique. |
| 286 | */ |
| 287 | #define iterate_chain_key(key1, key2) \ |
Ingo Molnar | 03cbc35 | 2006-09-29 02:01:46 -0700 | [diff] [blame] | 288 | (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \ |
| 289 | ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 290 | (key2)) |
| 291 | |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 292 | void lockdep_off(void) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 293 | { |
| 294 | current->lockdep_recursion++; |
| 295 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 296 | EXPORT_SYMBOL(lockdep_off); |
| 297 | |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 298 | void lockdep_on(void) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 299 | { |
| 300 | current->lockdep_recursion--; |
| 301 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 302 | EXPORT_SYMBOL(lockdep_on); |
| 303 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 304 | /* |
| 305 | * Debugging switches: |
| 306 | */ |
| 307 | |
| 308 | #define VERBOSE 0 |
Ingo Molnar | 33e94e9 | 2006-12-13 00:34:41 -0800 | [diff] [blame] | 309 | #define VERY_VERBOSE 0 |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 310 | |
| 311 | #if VERBOSE |
| 312 | # define HARDIRQ_VERBOSE 1 |
| 313 | # define SOFTIRQ_VERBOSE 1 |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 314 | # define RECLAIM_VERBOSE 1 |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 315 | #else |
| 316 | # define HARDIRQ_VERBOSE 0 |
| 317 | # define SOFTIRQ_VERBOSE 0 |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 318 | # define RECLAIM_VERBOSE 0 |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 319 | #endif |
| 320 | |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 321 | #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 322 | /* |
| 323 | * Quick filtering for interesting events: |
| 324 | */ |
| 325 | static int class_filter(struct lock_class *class) |
| 326 | { |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 327 | #if 0 |
| 328 | /* Example */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 329 | if (class->name_version == 1 && |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 330 | !strcmp(class->name, "lockname")) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 331 | return 1; |
| 332 | if (class->name_version == 1 && |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 333 | !strcmp(class->name, "&struct->lockfield")) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 334 | return 1; |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 335 | #endif |
Ingo Molnar | a664089 | 2006-12-13 00:34:39 -0800 | [diff] [blame] | 336 | /* Filter everything else. 1 would be to allow everything else */ |
| 337 | return 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 338 | } |
| 339 | #endif |
| 340 | |
| 341 | static int verbose(struct lock_class *class) |
| 342 | { |
| 343 | #if VERBOSE |
| 344 | return class_filter(class); |
| 345 | #endif |
| 346 | return 0; |
| 347 | } |
| 348 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 349 | /* |
| 350 | * Stack-trace: tightly packed array of stack backtrace |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 351 | * addresses. Protected by the graph_lock. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 352 | */ |
| 353 | unsigned long nr_stack_trace_entries; |
| 354 | static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES]; |
| 355 | |
| 356 | static int save_trace(struct stack_trace *trace) |
| 357 | { |
| 358 | trace->nr_entries = 0; |
| 359 | trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries; |
| 360 | trace->entries = stack_trace + nr_stack_trace_entries; |
| 361 | |
Andi Kleen | 5a1b399 | 2006-09-26 10:52:34 +0200 | [diff] [blame] | 362 | trace->skip = 3; |
Andi Kleen | 5a1b399 | 2006-09-26 10:52:34 +0200 | [diff] [blame] | 363 | |
Christoph Hellwig | ab1b6f0 | 2007-05-08 00:23:29 -0700 | [diff] [blame] | 364 | save_stack_trace(trace); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 365 | |
| 366 | trace->max_entries = trace->nr_entries; |
| 367 | |
| 368 | nr_stack_trace_entries += trace->nr_entries; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 369 | |
| 370 | if (nr_stack_trace_entries == MAX_STACK_TRACE_ENTRIES) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 371 | if (!debug_locks_off_graph_unlock()) |
| 372 | return 0; |
| 373 | |
| 374 | printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n"); |
| 375 | printk("turning off the locking correctness validator.\n"); |
| 376 | dump_stack(); |
| 377 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | return 1; |
| 382 | } |
| 383 | |
| 384 | unsigned int nr_hardirq_chains; |
| 385 | unsigned int nr_softirq_chains; |
| 386 | unsigned int nr_process_chains; |
| 387 | unsigned int max_lockdep_depth; |
| 388 | unsigned int max_recursion_depth; |
| 389 | |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 390 | static unsigned int lockdep_dependency_gen_id; |
| 391 | |
| 392 | static bool lockdep_dependency_visit(struct lock_class *source, |
| 393 | unsigned int depth) |
| 394 | { |
| 395 | if (!depth) |
| 396 | lockdep_dependency_gen_id++; |
| 397 | if (source->dep_gen_id == lockdep_dependency_gen_id) |
| 398 | return true; |
| 399 | source->dep_gen_id = lockdep_dependency_gen_id; |
| 400 | return false; |
| 401 | } |
| 402 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 403 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 404 | /* |
| 405 | * We cannot printk in early bootup code. Not even early_printk() |
| 406 | * might work. So we mark any initialization errors and printk |
| 407 | * about it later on, in lockdep_info(). |
| 408 | */ |
| 409 | static int lockdep_init_error; |
Johannes Berg | c71063c | 2007-07-19 01:49:02 -0700 | [diff] [blame] | 410 | static unsigned long lockdep_init_trace_data[20]; |
| 411 | static struct stack_trace lockdep_init_trace = { |
| 412 | .max_entries = ARRAY_SIZE(lockdep_init_trace_data), |
| 413 | .entries = lockdep_init_trace_data, |
| 414 | }; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 415 | |
| 416 | /* |
| 417 | * Various lockdep statistics: |
| 418 | */ |
| 419 | atomic_t chain_lookup_hits; |
| 420 | atomic_t chain_lookup_misses; |
| 421 | atomic_t hardirqs_on_events; |
| 422 | atomic_t hardirqs_off_events; |
| 423 | atomic_t redundant_hardirqs_on; |
| 424 | atomic_t redundant_hardirqs_off; |
| 425 | atomic_t softirqs_on_events; |
| 426 | atomic_t softirqs_off_events; |
| 427 | atomic_t redundant_softirqs_on; |
| 428 | atomic_t redundant_softirqs_off; |
| 429 | atomic_t nr_unused_locks; |
| 430 | atomic_t nr_cyclic_checks; |
| 431 | atomic_t nr_cyclic_check_recursions; |
| 432 | atomic_t nr_find_usage_forwards_checks; |
| 433 | atomic_t nr_find_usage_forwards_recursions; |
| 434 | atomic_t nr_find_usage_backwards_checks; |
| 435 | atomic_t nr_find_usage_backwards_recursions; |
| 436 | # define debug_atomic_inc(ptr) atomic_inc(ptr) |
| 437 | # define debug_atomic_dec(ptr) atomic_dec(ptr) |
| 438 | # define debug_atomic_read(ptr) atomic_read(ptr) |
| 439 | #else |
| 440 | # define debug_atomic_inc(ptr) do { } while (0) |
| 441 | # define debug_atomic_dec(ptr) do { } while (0) |
| 442 | # define debug_atomic_read(ptr) 0 |
| 443 | #endif |
| 444 | |
| 445 | /* |
| 446 | * Locking printouts: |
| 447 | */ |
| 448 | |
Peter Zijlstra | fabe9c4 | 2009-01-22 14:51:01 +0100 | [diff] [blame] | 449 | #define __USAGE(__STATE) \ |
Peter Zijlstra | b4b136f | 2009-01-29 14:50:36 +0100 | [diff] [blame] | 450 | [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \ |
| 451 | [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \ |
| 452 | [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\ |
| 453 | [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R", |
Peter Zijlstra | fabe9c4 | 2009-01-22 14:51:01 +0100 | [diff] [blame] | 454 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 455 | static const char *usage_str[] = |
| 456 | { |
Peter Zijlstra | fabe9c4 | 2009-01-22 14:51:01 +0100 | [diff] [blame] | 457 | #define LOCKDEP_STATE(__STATE) __USAGE(__STATE) |
| 458 | #include "lockdep_states.h" |
| 459 | #undef LOCKDEP_STATE |
| 460 | [LOCK_USED] = "INITIAL USE", |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | const char * __get_key_name(struct lockdep_subclass_key *key, char *str) |
| 464 | { |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 465 | return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Peter Zijlstra | 3ff176c | 2009-01-22 17:40:42 +0100 | [diff] [blame] | 468 | static inline unsigned long lock_flag(enum lock_usage_bit bit) |
| 469 | { |
| 470 | return 1UL << bit; |
| 471 | } |
| 472 | |
| 473 | static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit) |
| 474 | { |
| 475 | char c = '.'; |
| 476 | |
| 477 | if (class->usage_mask & lock_flag(bit + 2)) |
| 478 | c = '+'; |
| 479 | if (class->usage_mask & lock_flag(bit)) { |
| 480 | c = '-'; |
| 481 | if (class->usage_mask & lock_flag(bit + 2)) |
| 482 | c = '?'; |
| 483 | } |
| 484 | |
| 485 | return c; |
| 486 | } |
| 487 | |
Peter Zijlstra | f510b23 | 2009-01-22 17:53:47 +0100 | [diff] [blame] | 488 | void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS]) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 489 | { |
Peter Zijlstra | f510b23 | 2009-01-22 17:53:47 +0100 | [diff] [blame] | 490 | int i = 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 491 | |
Peter Zijlstra | f510b23 | 2009-01-22 17:53:47 +0100 | [diff] [blame] | 492 | #define LOCKDEP_STATE(__STATE) \ |
| 493 | usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \ |
| 494 | usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ); |
| 495 | #include "lockdep_states.h" |
| 496 | #undef LOCKDEP_STATE |
| 497 | |
| 498 | usage[i] = '\0'; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static void print_lock_name(struct lock_class *class) |
| 502 | { |
Peter Zijlstra | f510b23 | 2009-01-22 17:53:47 +0100 | [diff] [blame] | 503 | char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS]; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 504 | const char *name; |
| 505 | |
Peter Zijlstra | f510b23 | 2009-01-22 17:53:47 +0100 | [diff] [blame] | 506 | get_usage_chars(class, usage); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 507 | |
| 508 | name = class->name; |
| 509 | if (!name) { |
| 510 | name = __get_key_name(class->key, str); |
| 511 | printk(" (%s", name); |
| 512 | } else { |
| 513 | printk(" (%s", name); |
| 514 | if (class->name_version > 1) |
| 515 | printk("#%d", class->name_version); |
| 516 | if (class->subclass) |
| 517 | printk("/%d", class->subclass); |
| 518 | } |
Peter Zijlstra | f510b23 | 2009-01-22 17:53:47 +0100 | [diff] [blame] | 519 | printk("){%s}", usage); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | static void print_lockdep_cache(struct lockdep_map *lock) |
| 523 | { |
| 524 | const char *name; |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 525 | char str[KSYM_NAME_LEN]; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 526 | |
| 527 | name = lock->name; |
| 528 | if (!name) |
| 529 | name = __get_key_name(lock->key->subkeys, str); |
| 530 | |
| 531 | printk("%s", name); |
| 532 | } |
| 533 | |
| 534 | static void print_lock(struct held_lock *hlock) |
| 535 | { |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 536 | print_lock_name(hlock_class(hlock)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 537 | printk(", at: "); |
| 538 | print_ip_sym(hlock->acquire_ip); |
| 539 | } |
| 540 | |
| 541 | static void lockdep_print_held_locks(struct task_struct *curr) |
| 542 | { |
| 543 | int i, depth = curr->lockdep_depth; |
| 544 | |
| 545 | if (!depth) { |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 546 | printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 547 | return; |
| 548 | } |
| 549 | printk("%d lock%s held by %s/%d:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 550 | depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 551 | |
| 552 | for (i = 0; i < depth; i++) { |
| 553 | printk(" #%d: ", i); |
| 554 | print_lock(curr->held_locks + i); |
| 555 | } |
| 556 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 557 | |
| 558 | static void print_lock_class_header(struct lock_class *class, int depth) |
| 559 | { |
| 560 | int bit; |
| 561 | |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 562 | printk("%*s->", depth, ""); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 563 | print_lock_name(class); |
| 564 | printk(" ops: %lu", class->ops); |
| 565 | printk(" {\n"); |
| 566 | |
| 567 | for (bit = 0; bit < LOCK_USAGE_STATES; bit++) { |
| 568 | if (class->usage_mask & (1 << bit)) { |
| 569 | int len = depth; |
| 570 | |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 571 | len += printk("%*s %s", depth, "", usage_str[bit]); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 572 | len += printk(" at:\n"); |
| 573 | print_stack_trace(class->usage_traces + bit, len); |
| 574 | } |
| 575 | } |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 576 | printk("%*s }\n", depth, ""); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 577 | |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 578 | printk("%*s ... key at: ",depth,""); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 579 | print_ip_sym((unsigned long)class->key); |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * printk all lock dependencies starting at <entry>: |
| 584 | */ |
Ingo Molnar | 7807faf | 2008-11-25 08:44:24 +0100 | [diff] [blame] | 585 | static void __used |
| 586 | print_lock_dependencies(struct lock_class *class, int depth) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 587 | { |
| 588 | struct lock_list *entry; |
| 589 | |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 590 | if (lockdep_dependency_visit(class, depth)) |
| 591 | return; |
| 592 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 593 | if (DEBUG_LOCKS_WARN_ON(depth >= 20)) |
| 594 | return; |
| 595 | |
| 596 | print_lock_class_header(class, depth); |
| 597 | |
| 598 | list_for_each_entry(entry, &class->locks_after, entry) { |
Jarek Poplawski | b23984d | 2006-12-06 20:36:23 -0800 | [diff] [blame] | 599 | if (DEBUG_LOCKS_WARN_ON(!entry->class)) |
| 600 | return; |
| 601 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 602 | print_lock_dependencies(entry->class, depth + 1); |
| 603 | |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 604 | printk("%*s ... acquired at:\n",depth,""); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 605 | print_stack_trace(&entry->trace, 2); |
| 606 | printk("\n"); |
| 607 | } |
| 608 | } |
| 609 | |
Dave Jones | 99de055 | 2006-09-29 02:00:10 -0700 | [diff] [blame] | 610 | static void print_kernel_version(void) |
| 611 | { |
Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 612 | printk("%s %.*s\n", init_utsname()->release, |
| 613 | (int)strcspn(init_utsname()->version, " "), |
| 614 | init_utsname()->version); |
Dave Jones | 99de055 | 2006-09-29 02:00:10 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 617 | static int very_verbose(struct lock_class *class) |
| 618 | { |
| 619 | #if VERY_VERBOSE |
| 620 | return class_filter(class); |
| 621 | #endif |
| 622 | return 0; |
| 623 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 624 | |
| 625 | /* |
| 626 | * Is this the address of a static object: |
| 627 | */ |
| 628 | static int static_obj(void *obj) |
| 629 | { |
| 630 | unsigned long start = (unsigned long) &_stext, |
| 631 | end = (unsigned long) &_end, |
| 632 | addr = (unsigned long) obj; |
| 633 | #ifdef CONFIG_SMP |
| 634 | int i; |
| 635 | #endif |
| 636 | |
| 637 | /* |
| 638 | * static variable? |
| 639 | */ |
| 640 | if ((addr >= start) && (addr < end)) |
| 641 | return 1; |
| 642 | |
| 643 | #ifdef CONFIG_SMP |
| 644 | /* |
| 645 | * percpu var? |
| 646 | */ |
| 647 | for_each_possible_cpu(i) { |
| 648 | start = (unsigned long) &__per_cpu_start + per_cpu_offset(i); |
Ingo Molnar | 1ff5683 | 2006-11-17 19:57:22 +0100 | [diff] [blame] | 649 | end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM |
| 650 | + per_cpu_offset(i); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 651 | |
| 652 | if ((addr >= start) && (addr < end)) |
| 653 | return 1; |
| 654 | } |
| 655 | #endif |
| 656 | |
| 657 | /* |
| 658 | * module var? |
| 659 | */ |
| 660 | return is_module_address(addr); |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * To make lock name printouts unique, we calculate a unique |
| 665 | * class->name_version generation counter: |
| 666 | */ |
| 667 | static int count_matching_names(struct lock_class *new_class) |
| 668 | { |
| 669 | struct lock_class *class; |
| 670 | int count = 0; |
| 671 | |
| 672 | if (!new_class->name) |
| 673 | return 0; |
| 674 | |
| 675 | list_for_each_entry(class, &all_lock_classes, lock_entry) { |
| 676 | if (new_class->key - new_class->subclass == class->key) |
| 677 | return class->name_version; |
| 678 | if (class->name && !strcmp(class->name, new_class->name)) |
| 679 | count = max(count, class->name_version); |
| 680 | } |
| 681 | |
| 682 | return count + 1; |
| 683 | } |
| 684 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 685 | /* |
| 686 | * Register a lock's class in the hash-table, if the class is not present |
| 687 | * yet. Otherwise we look it up. We cache the result in the lock object |
| 688 | * itself, so actual lookup of the hash should be once per lock object. |
| 689 | */ |
| 690 | static inline struct lock_class * |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 691 | look_up_lock_class(struct lockdep_map *lock, unsigned int subclass) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 692 | { |
| 693 | struct lockdep_subclass_key *key; |
| 694 | struct list_head *hash_head; |
| 695 | struct lock_class *class; |
| 696 | |
| 697 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 698 | /* |
| 699 | * If the architecture calls into lockdep before initializing |
| 700 | * the hashes then we'll warn about it later. (we cannot printk |
| 701 | * right now) |
| 702 | */ |
| 703 | if (unlikely(!lockdep_initialized)) { |
| 704 | lockdep_init(); |
| 705 | lockdep_init_error = 1; |
Johannes Berg | c71063c | 2007-07-19 01:49:02 -0700 | [diff] [blame] | 706 | save_stack_trace(&lockdep_init_trace); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 707 | } |
| 708 | #endif |
| 709 | |
| 710 | /* |
| 711 | * Static locks do not have their class-keys yet - for them the key |
| 712 | * is the lock object itself: |
| 713 | */ |
| 714 | if (unlikely(!lock->key)) |
| 715 | lock->key = (void *)lock; |
| 716 | |
| 717 | /* |
| 718 | * NOTE: the class-key must be unique. For dynamic locks, a static |
| 719 | * lock_class_key variable is passed in through the mutex_init() |
| 720 | * (or spin_lock_init()) call - which acts as the key. For static |
| 721 | * locks we use the lock object itself as the key. |
| 722 | */ |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 723 | BUILD_BUG_ON(sizeof(struct lock_class_key) > |
| 724 | sizeof(struct lockdep_map)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 725 | |
| 726 | key = lock->key->subkeys + subclass; |
| 727 | |
| 728 | hash_head = classhashentry(key); |
| 729 | |
| 730 | /* |
| 731 | * We can walk the hash lockfree, because the hash only |
| 732 | * grows, and we are careful when adding entries to the end: |
| 733 | */ |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 734 | list_for_each_entry(class, hash_head, hash_entry) { |
| 735 | if (class->key == key) { |
| 736 | WARN_ON_ONCE(class->name != lock->name); |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 737 | return class; |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 738 | } |
| 739 | } |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 740 | |
| 741 | return NULL; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * Register a lock's class in the hash-table, if the class is not present |
| 746 | * yet. Otherwise we look it up. We cache the result in the lock object |
| 747 | * itself, so actual lookup of the hash should be once per lock object. |
| 748 | */ |
| 749 | static inline struct lock_class * |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 750 | register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force) |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 751 | { |
| 752 | struct lockdep_subclass_key *key; |
| 753 | struct list_head *hash_head; |
| 754 | struct lock_class *class; |
Ingo Molnar | 70e45067 | 2006-12-06 20:40:50 -0800 | [diff] [blame] | 755 | unsigned long flags; |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 756 | |
| 757 | class = look_up_lock_class(lock, subclass); |
| 758 | if (likely(class)) |
| 759 | return class; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 760 | |
| 761 | /* |
| 762 | * Debug-check: all keys must be persistent! |
| 763 | */ |
| 764 | if (!static_obj(lock->key)) { |
| 765 | debug_locks_off(); |
| 766 | printk("INFO: trying to register non-static key.\n"); |
| 767 | printk("the code is fine but needs lockdep annotation.\n"); |
| 768 | printk("turning off the locking correctness validator.\n"); |
| 769 | dump_stack(); |
| 770 | |
| 771 | return NULL; |
| 772 | } |
| 773 | |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 774 | key = lock->key->subkeys + subclass; |
| 775 | hash_head = classhashentry(key); |
| 776 | |
Ingo Molnar | 70e45067 | 2006-12-06 20:40:50 -0800 | [diff] [blame] | 777 | raw_local_irq_save(flags); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 778 | if (!graph_lock()) { |
| 779 | raw_local_irq_restore(flags); |
| 780 | return NULL; |
| 781 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 782 | /* |
| 783 | * We have to do the hash-walk again, to avoid races |
| 784 | * with another CPU: |
| 785 | */ |
| 786 | list_for_each_entry(class, hash_head, hash_entry) |
| 787 | if (class->key == key) |
| 788 | goto out_unlock_set; |
| 789 | /* |
| 790 | * Allocate a new key from the static array, and add it to |
| 791 | * the hash: |
| 792 | */ |
| 793 | if (nr_lock_classes >= MAX_LOCKDEP_KEYS) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 794 | if (!debug_locks_off_graph_unlock()) { |
| 795 | raw_local_irq_restore(flags); |
| 796 | return NULL; |
| 797 | } |
Ingo Molnar | 70e45067 | 2006-12-06 20:40:50 -0800 | [diff] [blame] | 798 | raw_local_irq_restore(flags); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 799 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 800 | printk("BUG: MAX_LOCKDEP_KEYS too low!\n"); |
| 801 | printk("turning off the locking correctness validator.\n"); |
| 802 | return NULL; |
| 803 | } |
| 804 | class = lock_classes + nr_lock_classes++; |
| 805 | debug_atomic_inc(&nr_unused_locks); |
| 806 | class->key = key; |
| 807 | class->name = lock->name; |
| 808 | class->subclass = subclass; |
| 809 | INIT_LIST_HEAD(&class->lock_entry); |
| 810 | INIT_LIST_HEAD(&class->locks_before); |
| 811 | INIT_LIST_HEAD(&class->locks_after); |
| 812 | class->name_version = count_matching_names(class); |
| 813 | /* |
| 814 | * We use RCU's safe list-add method to make |
| 815 | * parallel walking of the hash-list safe: |
| 816 | */ |
| 817 | list_add_tail_rcu(&class->hash_entry, hash_head); |
Dale Farnsworth | 1481197 | 2008-02-25 23:03:02 +0100 | [diff] [blame] | 818 | /* |
| 819 | * Add it to the global list of classes: |
| 820 | */ |
| 821 | list_add_tail_rcu(&class->lock_entry, &all_lock_classes); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 822 | |
| 823 | if (verbose(class)) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 824 | graph_unlock(); |
Ingo Molnar | 70e45067 | 2006-12-06 20:40:50 -0800 | [diff] [blame] | 825 | raw_local_irq_restore(flags); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 826 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 827 | printk("\nnew class %p: %s", class->key, class->name); |
| 828 | if (class->name_version > 1) |
| 829 | printk("#%d", class->name_version); |
| 830 | printk("\n"); |
| 831 | dump_stack(); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 832 | |
Ingo Molnar | 70e45067 | 2006-12-06 20:40:50 -0800 | [diff] [blame] | 833 | raw_local_irq_save(flags); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 834 | if (!graph_lock()) { |
| 835 | raw_local_irq_restore(flags); |
| 836 | return NULL; |
| 837 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 838 | } |
| 839 | out_unlock_set: |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 840 | graph_unlock(); |
Ingo Molnar | 70e45067 | 2006-12-06 20:40:50 -0800 | [diff] [blame] | 841 | raw_local_irq_restore(flags); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 842 | |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 843 | if (!subclass || force) |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 844 | lock->class_cache = class; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 845 | |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 846 | if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass)) |
| 847 | return NULL; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 848 | |
| 849 | return class; |
| 850 | } |
| 851 | |
Peter Zijlstra | ca58abc | 2007-07-19 01:48:53 -0700 | [diff] [blame] | 852 | #ifdef CONFIG_PROVE_LOCKING |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 853 | /* |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 854 | * Allocate a lockdep entry. (assumes the graph_lock held, returns |
| 855 | * with NULL on failure) |
| 856 | */ |
| 857 | static struct lock_list *alloc_list_entry(void) |
| 858 | { |
| 859 | if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) { |
| 860 | if (!debug_locks_off_graph_unlock()) |
| 861 | return NULL; |
| 862 | |
| 863 | printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n"); |
| 864 | printk("turning off the locking correctness validator.\n"); |
| 865 | return NULL; |
| 866 | } |
| 867 | return list_entries + nr_list_entries++; |
| 868 | } |
| 869 | |
| 870 | /* |
| 871 | * Add a new dependency to the head of the list: |
| 872 | */ |
| 873 | static int add_lock_to_list(struct lock_class *class, struct lock_class *this, |
| 874 | struct list_head *head, unsigned long ip, int distance) |
| 875 | { |
| 876 | struct lock_list *entry; |
| 877 | /* |
| 878 | * Lock not present yet - get a new dependency struct and |
| 879 | * add it to the list: |
| 880 | */ |
| 881 | entry = alloc_list_entry(); |
| 882 | if (!entry) |
| 883 | return 0; |
| 884 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 885 | if (!save_trace(&entry->trace)) |
| 886 | return 0; |
| 887 | |
Zhu Yi | 7487017 | 2008-08-27 14:33:00 +0800 | [diff] [blame] | 888 | entry->class = this; |
| 889 | entry->distance = distance; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 890 | /* |
| 891 | * Since we never remove from the dependency list, the list can |
| 892 | * be walked lockless by other CPUs, it's only allocation |
| 893 | * that must be protected by the spinlock. But this also means |
| 894 | * we must make new entries visible only once writes to the |
| 895 | * entry become visible - hence the RCU op: |
| 896 | */ |
| 897 | list_add_tail_rcu(&entry->entry, head); |
| 898 | |
| 899 | return 1; |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * Recursive, forwards-direction lock-dependency checking, used for |
| 904 | * both noncyclic checking and for hardirq-unsafe/softirq-unsafe |
| 905 | * checking. |
| 906 | * |
| 907 | * (to keep the stackframe of the recursive functions small we |
| 908 | * use these global variables, and we also mark various helper |
| 909 | * functions as noinline.) |
| 910 | */ |
| 911 | static struct held_lock *check_source, *check_target; |
| 912 | |
| 913 | /* |
| 914 | * Print a dependency chain entry (this is only done when a deadlock |
| 915 | * has been detected): |
| 916 | */ |
| 917 | static noinline int |
| 918 | print_circular_bug_entry(struct lock_list *target, unsigned int depth) |
| 919 | { |
| 920 | if (debug_locks_silent) |
| 921 | return 0; |
| 922 | printk("\n-> #%u", depth); |
| 923 | print_lock_name(target->class); |
| 924 | printk(":\n"); |
| 925 | print_stack_trace(&target->trace, 6); |
| 926 | |
| 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * When a circular dependency is detected, print the |
| 932 | * header first: |
| 933 | */ |
| 934 | static noinline int |
| 935 | print_circular_bug_header(struct lock_list *entry, unsigned int depth) |
| 936 | { |
| 937 | struct task_struct *curr = current; |
| 938 | |
| 939 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
| 940 | return 0; |
| 941 | |
| 942 | printk("\n=======================================================\n"); |
| 943 | printk( "[ INFO: possible circular locking dependency detected ]\n"); |
| 944 | print_kernel_version(); |
| 945 | printk( "-------------------------------------------------------\n"); |
| 946 | printk("%s/%d is trying to acquire lock:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 947 | curr->comm, task_pid_nr(curr)); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 948 | print_lock(check_source); |
| 949 | printk("\nbut task is already holding lock:\n"); |
| 950 | print_lock(check_target); |
| 951 | printk("\nwhich lock already depends on the new lock.\n\n"); |
| 952 | printk("\nthe existing dependency chain (in reverse order) is:\n"); |
| 953 | |
| 954 | print_circular_bug_entry(entry, depth); |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | static noinline int print_circular_bug_tail(void) |
| 960 | { |
| 961 | struct task_struct *curr = current; |
| 962 | struct lock_list this; |
| 963 | |
| 964 | if (debug_locks_silent) |
| 965 | return 0; |
| 966 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 967 | this.class = hlock_class(check_source); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 968 | if (!save_trace(&this.trace)) |
| 969 | return 0; |
| 970 | |
| 971 | print_circular_bug_entry(&this, 0); |
| 972 | |
| 973 | printk("\nother info that might help us debug this:\n\n"); |
| 974 | lockdep_print_held_locks(curr); |
| 975 | |
| 976 | printk("\nstack backtrace:\n"); |
| 977 | dump_stack(); |
| 978 | |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | #define RECURSION_LIMIT 40 |
| 983 | |
| 984 | static int noinline print_infinite_recursion_bug(void) |
| 985 | { |
| 986 | if (!debug_locks_off_graph_unlock()) |
| 987 | return 0; |
| 988 | |
| 989 | WARN_ON(1); |
| 990 | |
| 991 | return 0; |
| 992 | } |
| 993 | |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 994 | unsigned long __lockdep_count_forward_deps(struct lock_class *class, |
| 995 | unsigned int depth) |
| 996 | { |
| 997 | struct lock_list *entry; |
| 998 | unsigned long ret = 1; |
| 999 | |
| 1000 | if (lockdep_dependency_visit(class, depth)) |
| 1001 | return 0; |
| 1002 | |
| 1003 | /* |
| 1004 | * Recurse this class's dependency list: |
| 1005 | */ |
| 1006 | list_for_each_entry(entry, &class->locks_after, entry) |
| 1007 | ret += __lockdep_count_forward_deps(entry->class, depth + 1); |
| 1008 | |
| 1009 | return ret; |
| 1010 | } |
| 1011 | |
| 1012 | unsigned long lockdep_count_forward_deps(struct lock_class *class) |
| 1013 | { |
| 1014 | unsigned long ret, flags; |
| 1015 | |
| 1016 | local_irq_save(flags); |
| 1017 | __raw_spin_lock(&lockdep_lock); |
| 1018 | ret = __lockdep_count_forward_deps(class, 0); |
| 1019 | __raw_spin_unlock(&lockdep_lock); |
| 1020 | local_irq_restore(flags); |
| 1021 | |
| 1022 | return ret; |
| 1023 | } |
| 1024 | |
| 1025 | unsigned long __lockdep_count_backward_deps(struct lock_class *class, |
| 1026 | unsigned int depth) |
| 1027 | { |
| 1028 | struct lock_list *entry; |
| 1029 | unsigned long ret = 1; |
| 1030 | |
| 1031 | if (lockdep_dependency_visit(class, depth)) |
| 1032 | return 0; |
| 1033 | /* |
| 1034 | * Recurse this class's dependency list: |
| 1035 | */ |
| 1036 | list_for_each_entry(entry, &class->locks_before, entry) |
| 1037 | ret += __lockdep_count_backward_deps(entry->class, depth + 1); |
| 1038 | |
| 1039 | return ret; |
| 1040 | } |
| 1041 | |
| 1042 | unsigned long lockdep_count_backward_deps(struct lock_class *class) |
| 1043 | { |
| 1044 | unsigned long ret, flags; |
| 1045 | |
| 1046 | local_irq_save(flags); |
| 1047 | __raw_spin_lock(&lockdep_lock); |
| 1048 | ret = __lockdep_count_backward_deps(class, 0); |
| 1049 | __raw_spin_unlock(&lockdep_lock); |
| 1050 | local_irq_restore(flags); |
| 1051 | |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1055 | /* |
| 1056 | * Prove that the dependency graph starting at <entry> can not |
| 1057 | * lead to <target>. Print an error and return 0 if it does. |
| 1058 | */ |
| 1059 | static noinline int |
| 1060 | check_noncircular(struct lock_class *source, unsigned int depth) |
| 1061 | { |
| 1062 | struct lock_list *entry; |
| 1063 | |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1064 | if (lockdep_dependency_visit(source, depth)) |
| 1065 | return 1; |
| 1066 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1067 | debug_atomic_inc(&nr_cyclic_check_recursions); |
| 1068 | if (depth > max_recursion_depth) |
| 1069 | max_recursion_depth = depth; |
| 1070 | if (depth >= RECURSION_LIMIT) |
| 1071 | return print_infinite_recursion_bug(); |
| 1072 | /* |
| 1073 | * Check this lock's dependency list: |
| 1074 | */ |
| 1075 | list_for_each_entry(entry, &source->locks_after, entry) { |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1076 | if (entry->class == hlock_class(check_target)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1077 | return print_circular_bug_header(entry, depth+1); |
| 1078 | debug_atomic_inc(&nr_cyclic_checks); |
| 1079 | if (!check_noncircular(entry->class, depth+1)) |
| 1080 | return print_circular_bug_entry(entry, depth+1); |
| 1081 | } |
| 1082 | return 1; |
| 1083 | } |
| 1084 | |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1085 | #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1086 | /* |
| 1087 | * Forwards and backwards subgraph searching, for the purposes of |
| 1088 | * proving that two subgraphs can be connected by a new dependency |
| 1089 | * without creating any illegal irq-safe -> irq-unsafe lock dependency. |
| 1090 | */ |
| 1091 | static enum lock_usage_bit find_usage_bit; |
| 1092 | static struct lock_class *forwards_match, *backwards_match; |
| 1093 | |
| 1094 | /* |
| 1095 | * Find a node in the forwards-direction dependency sub-graph starting |
| 1096 | * at <source> that matches <find_usage_bit>. |
| 1097 | * |
| 1098 | * Return 2 if such a node exists in the subgraph, and put that node |
| 1099 | * into <forwards_match>. |
| 1100 | * |
| 1101 | * Return 1 otherwise and keep <forwards_match> unchanged. |
| 1102 | * Return 0 on error. |
| 1103 | */ |
| 1104 | static noinline int |
| 1105 | find_usage_forwards(struct lock_class *source, unsigned int depth) |
| 1106 | { |
| 1107 | struct lock_list *entry; |
| 1108 | int ret; |
| 1109 | |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1110 | if (lockdep_dependency_visit(source, depth)) |
| 1111 | return 1; |
| 1112 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1113 | if (depth > max_recursion_depth) |
| 1114 | max_recursion_depth = depth; |
| 1115 | if (depth >= RECURSION_LIMIT) |
| 1116 | return print_infinite_recursion_bug(); |
| 1117 | |
| 1118 | debug_atomic_inc(&nr_find_usage_forwards_checks); |
| 1119 | if (source->usage_mask & (1 << find_usage_bit)) { |
| 1120 | forwards_match = source; |
| 1121 | return 2; |
| 1122 | } |
| 1123 | |
| 1124 | /* |
| 1125 | * Check this lock's dependency list: |
| 1126 | */ |
| 1127 | list_for_each_entry(entry, &source->locks_after, entry) { |
| 1128 | debug_atomic_inc(&nr_find_usage_forwards_recursions); |
| 1129 | ret = find_usage_forwards(entry->class, depth+1); |
| 1130 | if (ret == 2 || ret == 0) |
| 1131 | return ret; |
| 1132 | } |
| 1133 | return 1; |
| 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * Find a node in the backwards-direction dependency sub-graph starting |
| 1138 | * at <source> that matches <find_usage_bit>. |
| 1139 | * |
| 1140 | * Return 2 if such a node exists in the subgraph, and put that node |
| 1141 | * into <backwards_match>. |
| 1142 | * |
| 1143 | * Return 1 otherwise and keep <backwards_match> unchanged. |
| 1144 | * Return 0 on error. |
| 1145 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 1146 | static noinline int |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1147 | find_usage_backwards(struct lock_class *source, unsigned int depth) |
| 1148 | { |
| 1149 | struct lock_list *entry; |
| 1150 | int ret; |
| 1151 | |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1152 | if (lockdep_dependency_visit(source, depth)) |
| 1153 | return 1; |
| 1154 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1155 | if (!__raw_spin_is_locked(&lockdep_lock)) |
| 1156 | return DEBUG_LOCKS_WARN_ON(1); |
| 1157 | |
| 1158 | if (depth > max_recursion_depth) |
| 1159 | max_recursion_depth = depth; |
| 1160 | if (depth >= RECURSION_LIMIT) |
| 1161 | return print_infinite_recursion_bug(); |
| 1162 | |
| 1163 | debug_atomic_inc(&nr_find_usage_backwards_checks); |
| 1164 | if (source->usage_mask & (1 << find_usage_bit)) { |
| 1165 | backwards_match = source; |
| 1166 | return 2; |
| 1167 | } |
| 1168 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1169 | if (!source && debug_locks_off_graph_unlock()) { |
| 1170 | WARN_ON(1); |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1174 | /* |
| 1175 | * Check this lock's dependency list: |
| 1176 | */ |
| 1177 | list_for_each_entry(entry, &source->locks_before, entry) { |
| 1178 | debug_atomic_inc(&nr_find_usage_backwards_recursions); |
| 1179 | ret = find_usage_backwards(entry->class, depth+1); |
| 1180 | if (ret == 2 || ret == 0) |
| 1181 | return ret; |
| 1182 | } |
| 1183 | return 1; |
| 1184 | } |
| 1185 | |
| 1186 | static int |
| 1187 | print_bad_irq_dependency(struct task_struct *curr, |
| 1188 | struct held_lock *prev, |
| 1189 | struct held_lock *next, |
| 1190 | enum lock_usage_bit bit1, |
| 1191 | enum lock_usage_bit bit2, |
| 1192 | const char *irqclass) |
| 1193 | { |
| 1194 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
| 1195 | return 0; |
| 1196 | |
| 1197 | printk("\n======================================================\n"); |
| 1198 | printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n", |
| 1199 | irqclass, irqclass); |
| 1200 | print_kernel_version(); |
| 1201 | printk( "------------------------------------------------------\n"); |
| 1202 | printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 1203 | curr->comm, task_pid_nr(curr), |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1204 | curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT, |
| 1205 | curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT, |
| 1206 | curr->hardirqs_enabled, |
| 1207 | curr->softirqs_enabled); |
| 1208 | print_lock(next); |
| 1209 | |
| 1210 | printk("\nand this task is already holding:\n"); |
| 1211 | print_lock(prev); |
| 1212 | printk("which would create a new lock dependency:\n"); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1213 | print_lock_name(hlock_class(prev)); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1214 | printk(" ->"); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1215 | print_lock_name(hlock_class(next)); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1216 | printk("\n"); |
| 1217 | |
| 1218 | printk("\nbut this new dependency connects a %s-irq-safe lock:\n", |
| 1219 | irqclass); |
| 1220 | print_lock_name(backwards_match); |
| 1221 | printk("\n... which became %s-irq-safe at:\n", irqclass); |
| 1222 | |
| 1223 | print_stack_trace(backwards_match->usage_traces + bit1, 1); |
| 1224 | |
| 1225 | printk("\nto a %s-irq-unsafe lock:\n", irqclass); |
| 1226 | print_lock_name(forwards_match); |
| 1227 | printk("\n... which became %s-irq-unsafe at:\n", irqclass); |
| 1228 | printk("..."); |
| 1229 | |
| 1230 | print_stack_trace(forwards_match->usage_traces + bit2, 1); |
| 1231 | |
| 1232 | printk("\nother info that might help us debug this:\n\n"); |
| 1233 | lockdep_print_held_locks(curr); |
| 1234 | |
| 1235 | printk("\nthe %s-irq-safe lock's dependencies:\n", irqclass); |
| 1236 | print_lock_dependencies(backwards_match, 0); |
| 1237 | |
| 1238 | printk("\nthe %s-irq-unsafe lock's dependencies:\n", irqclass); |
| 1239 | print_lock_dependencies(forwards_match, 0); |
| 1240 | |
| 1241 | printk("\nstack backtrace:\n"); |
| 1242 | dump_stack(); |
| 1243 | |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | static int |
| 1248 | check_usage(struct task_struct *curr, struct held_lock *prev, |
| 1249 | struct held_lock *next, enum lock_usage_bit bit_backwards, |
| 1250 | enum lock_usage_bit bit_forwards, const char *irqclass) |
| 1251 | { |
| 1252 | int ret; |
| 1253 | |
| 1254 | find_usage_bit = bit_backwards; |
| 1255 | /* fills in <backwards_match> */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1256 | ret = find_usage_backwards(hlock_class(prev), 0); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1257 | if (!ret || ret == 1) |
| 1258 | return ret; |
| 1259 | |
| 1260 | find_usage_bit = bit_forwards; |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1261 | ret = find_usage_forwards(hlock_class(next), 0); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1262 | if (!ret || ret == 1) |
| 1263 | return ret; |
| 1264 | /* ret == 2 */ |
| 1265 | return print_bad_irq_dependency(curr, prev, next, |
| 1266 | bit_backwards, bit_forwards, irqclass); |
| 1267 | } |
| 1268 | |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1269 | static const char *state_names[] = { |
| 1270 | #define LOCKDEP_STATE(__STATE) \ |
Peter Zijlstra | b4b136f | 2009-01-29 14:50:36 +0100 | [diff] [blame] | 1271 | __stringify(__STATE), |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1272 | #include "lockdep_states.h" |
| 1273 | #undef LOCKDEP_STATE |
| 1274 | }; |
| 1275 | |
| 1276 | static const char *state_rnames[] = { |
| 1277 | #define LOCKDEP_STATE(__STATE) \ |
Peter Zijlstra | b4b136f | 2009-01-29 14:50:36 +0100 | [diff] [blame] | 1278 | __stringify(__STATE)"-READ", |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1279 | #include "lockdep_states.h" |
| 1280 | #undef LOCKDEP_STATE |
| 1281 | }; |
| 1282 | |
| 1283 | static inline const char *state_name(enum lock_usage_bit bit) |
| 1284 | { |
| 1285 | return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2]; |
| 1286 | } |
| 1287 | |
| 1288 | static int exclusive_bit(int new_bit) |
| 1289 | { |
| 1290 | /* |
| 1291 | * USED_IN |
| 1292 | * USED_IN_READ |
| 1293 | * ENABLED |
| 1294 | * ENABLED_READ |
| 1295 | * |
| 1296 | * bit 0 - write/read |
| 1297 | * bit 1 - used_in/enabled |
| 1298 | * bit 2+ state |
| 1299 | */ |
| 1300 | |
| 1301 | int state = new_bit & ~3; |
| 1302 | int dir = new_bit & 2; |
| 1303 | |
| 1304 | /* |
| 1305 | * keep state, bit flip the direction and strip read. |
| 1306 | */ |
| 1307 | return state | (dir ^ 2); |
| 1308 | } |
| 1309 | |
| 1310 | static int check_irq_usage(struct task_struct *curr, struct held_lock *prev, |
| 1311 | struct held_lock *next, enum lock_usage_bit bit) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1312 | { |
| 1313 | /* |
| 1314 | * Prove that the new dependency does not connect a hardirq-safe |
| 1315 | * lock with a hardirq-unsafe lock - to achieve this we search |
| 1316 | * the backwards-subgraph starting at <prev>, and the |
| 1317 | * forwards-subgraph starting at <next>: |
| 1318 | */ |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1319 | if (!check_usage(curr, prev, next, bit, |
| 1320 | exclusive_bit(bit), state_name(bit))) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1321 | return 0; |
| 1322 | |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1323 | bit++; /* _READ */ |
| 1324 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1325 | /* |
| 1326 | * Prove that the new dependency does not connect a hardirq-safe-read |
| 1327 | * lock with a hardirq-unsafe lock - to achieve this we search |
| 1328 | * the backwards-subgraph starting at <prev>, and the |
| 1329 | * forwards-subgraph starting at <next>: |
| 1330 | */ |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1331 | if (!check_usage(curr, prev, next, bit, |
| 1332 | exclusive_bit(bit), state_name(bit))) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1333 | return 0; |
| 1334 | |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1335 | return 1; |
| 1336 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1337 | |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1338 | static int |
| 1339 | check_prev_add_irq(struct task_struct *curr, struct held_lock *prev, |
| 1340 | struct held_lock *next) |
| 1341 | { |
| 1342 | #define LOCKDEP_STATE(__STATE) \ |
| 1343 | if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \ |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 1344 | return 0; |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1345 | #include "lockdep_states.h" |
| 1346 | #undef LOCKDEP_STATE |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 1347 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1348 | return 1; |
| 1349 | } |
| 1350 | |
| 1351 | static void inc_chains(void) |
| 1352 | { |
| 1353 | if (current->hardirq_context) |
| 1354 | nr_hardirq_chains++; |
| 1355 | else { |
| 1356 | if (current->softirq_context) |
| 1357 | nr_softirq_chains++; |
| 1358 | else |
| 1359 | nr_process_chains++; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | #else |
| 1364 | |
| 1365 | static inline int |
| 1366 | check_prev_add_irq(struct task_struct *curr, struct held_lock *prev, |
| 1367 | struct held_lock *next) |
| 1368 | { |
| 1369 | return 1; |
| 1370 | } |
| 1371 | |
| 1372 | static inline void inc_chains(void) |
| 1373 | { |
| 1374 | nr_process_chains++; |
| 1375 | } |
| 1376 | |
| 1377 | #endif |
| 1378 | |
| 1379 | static int |
| 1380 | print_deadlock_bug(struct task_struct *curr, struct held_lock *prev, |
| 1381 | struct held_lock *next) |
| 1382 | { |
| 1383 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
| 1384 | return 0; |
| 1385 | |
| 1386 | printk("\n=============================================\n"); |
| 1387 | printk( "[ INFO: possible recursive locking detected ]\n"); |
| 1388 | print_kernel_version(); |
| 1389 | printk( "---------------------------------------------\n"); |
| 1390 | printk("%s/%d is trying to acquire lock:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 1391 | curr->comm, task_pid_nr(curr)); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1392 | print_lock(next); |
| 1393 | printk("\nbut task is already holding lock:\n"); |
| 1394 | print_lock(prev); |
| 1395 | |
| 1396 | printk("\nother info that might help us debug this:\n"); |
| 1397 | lockdep_print_held_locks(curr); |
| 1398 | |
| 1399 | printk("\nstack backtrace:\n"); |
| 1400 | dump_stack(); |
| 1401 | |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
| 1405 | /* |
| 1406 | * Check whether we are holding such a class already. |
| 1407 | * |
| 1408 | * (Note that this has to be done separately, because the graph cannot |
| 1409 | * detect such classes of deadlocks.) |
| 1410 | * |
| 1411 | * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read |
| 1412 | */ |
| 1413 | static int |
| 1414 | check_deadlock(struct task_struct *curr, struct held_lock *next, |
| 1415 | struct lockdep_map *next_instance, int read) |
| 1416 | { |
| 1417 | struct held_lock *prev; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 1418 | struct held_lock *nest = NULL; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1419 | int i; |
| 1420 | |
| 1421 | for (i = 0; i < curr->lockdep_depth; i++) { |
| 1422 | prev = curr->held_locks + i; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 1423 | |
| 1424 | if (prev->instance == next->nest_lock) |
| 1425 | nest = prev; |
| 1426 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1427 | if (hlock_class(prev) != hlock_class(next)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1428 | continue; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 1429 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1430 | /* |
| 1431 | * Allow read-after-read recursion of the same |
| 1432 | * lock class (i.e. read_lock(lock)+read_lock(lock)): |
| 1433 | */ |
| 1434 | if ((read == 2) && prev->read) |
| 1435 | return 2; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 1436 | |
| 1437 | /* |
| 1438 | * We're holding the nest_lock, which serializes this lock's |
| 1439 | * nesting behaviour. |
| 1440 | */ |
| 1441 | if (nest) |
| 1442 | return 2; |
| 1443 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1444 | return print_deadlock_bug(curr, prev, next); |
| 1445 | } |
| 1446 | return 1; |
| 1447 | } |
| 1448 | |
| 1449 | /* |
| 1450 | * There was a chain-cache miss, and we are about to add a new dependency |
| 1451 | * to a previous lock. We recursively validate the following rules: |
| 1452 | * |
| 1453 | * - would the adding of the <prev> -> <next> dependency create a |
| 1454 | * circular dependency in the graph? [== circular deadlock] |
| 1455 | * |
| 1456 | * - does the new prev->next dependency connect any hardirq-safe lock |
| 1457 | * (in the full backwards-subgraph starting at <prev>) with any |
| 1458 | * hardirq-unsafe lock (in the full forwards-subgraph starting at |
| 1459 | * <next>)? [== illegal lock inversion with hardirq contexts] |
| 1460 | * |
| 1461 | * - does the new prev->next dependency connect any softirq-safe lock |
| 1462 | * (in the full backwards-subgraph starting at <prev>) with any |
| 1463 | * softirq-unsafe lock (in the full forwards-subgraph starting at |
| 1464 | * <next>)? [== illegal lock inversion with softirq contexts] |
| 1465 | * |
| 1466 | * any of these scenarios could lead to a deadlock. |
| 1467 | * |
| 1468 | * Then if all the validations pass, we add the forwards and backwards |
| 1469 | * dependency. |
| 1470 | */ |
| 1471 | static int |
| 1472 | check_prev_add(struct task_struct *curr, struct held_lock *prev, |
| 1473 | struct held_lock *next, int distance) |
| 1474 | { |
| 1475 | struct lock_list *entry; |
| 1476 | int ret; |
| 1477 | |
| 1478 | /* |
| 1479 | * Prove that the new <prev> -> <next> dependency would not |
| 1480 | * create a circular dependency in the graph. (We do this by |
| 1481 | * forward-recursing into the graph starting at <next>, and |
| 1482 | * checking whether we can reach <prev>.) |
| 1483 | * |
| 1484 | * We are using global variables to control the recursion, to |
| 1485 | * keep the stackframe size of the recursive functions low: |
| 1486 | */ |
| 1487 | check_source = next; |
| 1488 | check_target = prev; |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1489 | if (!(check_noncircular(hlock_class(next), 0))) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1490 | return print_circular_bug_tail(); |
| 1491 | |
| 1492 | if (!check_prev_add_irq(curr, prev, next)) |
| 1493 | return 0; |
| 1494 | |
| 1495 | /* |
| 1496 | * For recursive read-locks we do all the dependency checks, |
| 1497 | * but we dont store read-triggered dependencies (only |
| 1498 | * write-triggered dependencies). This ensures that only the |
| 1499 | * write-side dependencies matter, and that if for example a |
| 1500 | * write-lock never takes any other locks, then the reads are |
| 1501 | * equivalent to a NOP. |
| 1502 | */ |
| 1503 | if (next->read == 2 || prev->read == 2) |
| 1504 | return 1; |
| 1505 | /* |
| 1506 | * Is the <prev> -> <next> dependency already present? |
| 1507 | * |
| 1508 | * (this may occur even though this is a new chain: consider |
| 1509 | * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3 |
| 1510 | * chains - the second one will be new, but L1 already has |
| 1511 | * L2 added to its dependency list, due to the first chain.) |
| 1512 | */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1513 | list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) { |
| 1514 | if (entry->class == hlock_class(next)) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1515 | if (distance == 1) |
| 1516 | entry->distance = 1; |
| 1517 | return 2; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | * Ok, all validations passed, add the new lock |
| 1523 | * to the previous lock's dependency list: |
| 1524 | */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1525 | ret = add_lock_to_list(hlock_class(prev), hlock_class(next), |
| 1526 | &hlock_class(prev)->locks_after, |
| 1527 | next->acquire_ip, distance); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1528 | |
| 1529 | if (!ret) |
| 1530 | return 0; |
| 1531 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1532 | ret = add_lock_to_list(hlock_class(next), hlock_class(prev), |
| 1533 | &hlock_class(next)->locks_before, |
| 1534 | next->acquire_ip, distance); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1535 | if (!ret) |
| 1536 | return 0; |
| 1537 | |
| 1538 | /* |
| 1539 | * Debugging printouts: |
| 1540 | */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1541 | if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1542 | graph_unlock(); |
| 1543 | printk("\n new dependency: "); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1544 | print_lock_name(hlock_class(prev)); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1545 | printk(" => "); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1546 | print_lock_name(hlock_class(next)); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1547 | printk("\n"); |
| 1548 | dump_stack(); |
| 1549 | return graph_lock(); |
| 1550 | } |
| 1551 | return 1; |
| 1552 | } |
| 1553 | |
| 1554 | /* |
| 1555 | * Add the dependency to all directly-previous locks that are 'relevant'. |
| 1556 | * The ones that are relevant are (in increasing distance from curr): |
| 1557 | * all consecutive trylock entries and the final non-trylock entry - or |
| 1558 | * the end of this context's lock-chain - whichever comes first. |
| 1559 | */ |
| 1560 | static int |
| 1561 | check_prevs_add(struct task_struct *curr, struct held_lock *next) |
| 1562 | { |
| 1563 | int depth = curr->lockdep_depth; |
| 1564 | struct held_lock *hlock; |
| 1565 | |
| 1566 | /* |
| 1567 | * Debugging checks. |
| 1568 | * |
| 1569 | * Depth must not be zero for a non-head lock: |
| 1570 | */ |
| 1571 | if (!depth) |
| 1572 | goto out_bug; |
| 1573 | /* |
| 1574 | * At least two relevant locks must exist for this |
| 1575 | * to be a head: |
| 1576 | */ |
| 1577 | if (curr->held_locks[depth].irq_context != |
| 1578 | curr->held_locks[depth-1].irq_context) |
| 1579 | goto out_bug; |
| 1580 | |
| 1581 | for (;;) { |
| 1582 | int distance = curr->lockdep_depth - depth + 1; |
| 1583 | hlock = curr->held_locks + depth-1; |
| 1584 | /* |
| 1585 | * Only non-recursive-read entries get new dependencies |
| 1586 | * added: |
| 1587 | */ |
| 1588 | if (hlock->read != 2) { |
| 1589 | if (!check_prev_add(curr, hlock, next, distance)) |
| 1590 | return 0; |
| 1591 | /* |
| 1592 | * Stop after the first non-trylock entry, |
| 1593 | * as non-trylock entries have added their |
| 1594 | * own direct dependencies already, so this |
| 1595 | * lock is connected to them indirectly: |
| 1596 | */ |
| 1597 | if (!hlock->trylock) |
| 1598 | break; |
| 1599 | } |
| 1600 | depth--; |
| 1601 | /* |
| 1602 | * End of lock-stack? |
| 1603 | */ |
| 1604 | if (!depth) |
| 1605 | break; |
| 1606 | /* |
| 1607 | * Stop the search if we cross into another context: |
| 1608 | */ |
| 1609 | if (curr->held_locks[depth].irq_context != |
| 1610 | curr->held_locks[depth-1].irq_context) |
| 1611 | break; |
| 1612 | } |
| 1613 | return 1; |
| 1614 | out_bug: |
| 1615 | if (!debug_locks_off_graph_unlock()) |
| 1616 | return 0; |
| 1617 | |
| 1618 | WARN_ON(1); |
| 1619 | |
| 1620 | return 0; |
| 1621 | } |
| 1622 | |
| 1623 | unsigned long nr_lock_chains; |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 1624 | struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS]; |
Huang, Ying | cd1a28e | 2008-06-23 11:20:54 +0800 | [diff] [blame] | 1625 | int nr_chain_hlocks; |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 1626 | static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS]; |
| 1627 | |
| 1628 | struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i) |
| 1629 | { |
| 1630 | return lock_classes + chain_hlocks[chain->base + i]; |
| 1631 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1632 | |
| 1633 | /* |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1634 | * Look up a dependency chain. If the key is not present yet then |
Jarek Poplawski | 9e860d0 | 2007-05-08 00:30:12 -0700 | [diff] [blame] | 1635 | * add it and return 1 - in this case the new dependency chain is |
| 1636 | * validated. If the key is already hashed, return 0. |
| 1637 | * (On return with 1 graph_lock is held.) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1638 | */ |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 1639 | static inline int lookup_chain_cache(struct task_struct *curr, |
| 1640 | struct held_lock *hlock, |
| 1641 | u64 chain_key) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1642 | { |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1643 | struct lock_class *class = hlock_class(hlock); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1644 | struct list_head *hash_head = chainhashentry(chain_key); |
| 1645 | struct lock_chain *chain; |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 1646 | struct held_lock *hlock_curr, *hlock_next; |
Huang, Ying | cd1a28e | 2008-06-23 11:20:54 +0800 | [diff] [blame] | 1647 | int i, j, n, cn; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1648 | |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 1649 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 1650 | return 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1651 | /* |
| 1652 | * We can walk it lock-free, because entries only get added |
| 1653 | * to the hash: |
| 1654 | */ |
| 1655 | list_for_each_entry(chain, hash_head, entry) { |
| 1656 | if (chain->chain_key == chain_key) { |
| 1657 | cache_hit: |
| 1658 | debug_atomic_inc(&chain_lookup_hits); |
Ingo Molnar | 81fc685 | 2006-12-13 00:34:40 -0800 | [diff] [blame] | 1659 | if (very_verbose(class)) |
Andrew Morton | 755cd90 | 2006-12-29 16:49:14 -0800 | [diff] [blame] | 1660 | printk("\nhash chain already cached, key: " |
| 1661 | "%016Lx tail class: [%p] %s\n", |
| 1662 | (unsigned long long)chain_key, |
| 1663 | class->key, class->name); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1664 | return 0; |
| 1665 | } |
| 1666 | } |
Ingo Molnar | 81fc685 | 2006-12-13 00:34:40 -0800 | [diff] [blame] | 1667 | if (very_verbose(class)) |
Andrew Morton | 755cd90 | 2006-12-29 16:49:14 -0800 | [diff] [blame] | 1668 | printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n", |
| 1669 | (unsigned long long)chain_key, class->key, class->name); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1670 | /* |
| 1671 | * Allocate a new chain entry from the static array, and add |
| 1672 | * it to the hash: |
| 1673 | */ |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1674 | if (!graph_lock()) |
| 1675 | return 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1676 | /* |
| 1677 | * We have to walk the chain again locked - to avoid duplicates: |
| 1678 | */ |
| 1679 | list_for_each_entry(chain, hash_head, entry) { |
| 1680 | if (chain->chain_key == chain_key) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1681 | graph_unlock(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1682 | goto cache_hit; |
| 1683 | } |
| 1684 | } |
| 1685 | if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1686 | if (!debug_locks_off_graph_unlock()) |
| 1687 | return 0; |
| 1688 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1689 | printk("BUG: MAX_LOCKDEP_CHAINS too low!\n"); |
| 1690 | printk("turning off the locking correctness validator.\n"); |
| 1691 | return 0; |
| 1692 | } |
| 1693 | chain = lock_chains + nr_lock_chains++; |
| 1694 | chain->chain_key = chain_key; |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 1695 | chain->irq_context = hlock->irq_context; |
| 1696 | /* Find the first held_lock of current chain */ |
| 1697 | hlock_next = hlock; |
| 1698 | for (i = curr->lockdep_depth - 1; i >= 0; i--) { |
| 1699 | hlock_curr = curr->held_locks + i; |
| 1700 | if (hlock_curr->irq_context != hlock_next->irq_context) |
| 1701 | break; |
| 1702 | hlock_next = hlock; |
| 1703 | } |
| 1704 | i++; |
| 1705 | chain->depth = curr->lockdep_depth + 1 - i; |
Huang, Ying | cd1a28e | 2008-06-23 11:20:54 +0800 | [diff] [blame] | 1706 | cn = nr_chain_hlocks; |
| 1707 | while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) { |
| 1708 | n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth); |
| 1709 | if (n == cn) |
| 1710 | break; |
| 1711 | cn = n; |
| 1712 | } |
| 1713 | if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) { |
| 1714 | chain->base = cn; |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 1715 | for (j = 0; j < chain->depth - 1; j++, i++) { |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1716 | int lock_id = curr->held_locks[i].class_idx - 1; |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 1717 | chain_hlocks[chain->base + j] = lock_id; |
| 1718 | } |
| 1719 | chain_hlocks[chain->base + j] = class - lock_classes; |
| 1720 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1721 | list_add_tail_rcu(&chain->entry, hash_head); |
| 1722 | debug_atomic_inc(&chain_lookup_misses); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1723 | inc_chains(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1724 | |
| 1725 | return 1; |
| 1726 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1727 | |
| 1728 | static int validate_chain(struct task_struct *curr, struct lockdep_map *lock, |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 1729 | struct held_lock *hlock, int chain_head, u64 chain_key) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1730 | { |
| 1731 | /* |
| 1732 | * Trylock needs to maintain the stack of held locks, but it |
| 1733 | * does not add new dependencies, because trylock can be done |
| 1734 | * in any order. |
| 1735 | * |
| 1736 | * We look up the chain_key and do the O(N^2) check and update of |
| 1737 | * the dependencies only if this is a new dependency chain. |
| 1738 | * (If lookup_chain_cache() returns with 1 it acquires |
| 1739 | * graph_lock for us) |
| 1740 | */ |
| 1741 | if (!hlock->trylock && (hlock->check == 2) && |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 1742 | lookup_chain_cache(curr, hlock, chain_key)) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1743 | /* |
| 1744 | * Check whether last held lock: |
| 1745 | * |
| 1746 | * - is irq-safe, if this lock is irq-unsafe |
| 1747 | * - is softirq-safe, if this lock is hardirq-unsafe |
| 1748 | * |
| 1749 | * And check whether the new lock's dependency graph |
| 1750 | * could lead back to the previous lock. |
| 1751 | * |
| 1752 | * any of these scenarios could lead to a deadlock. If |
| 1753 | * All validations |
| 1754 | */ |
| 1755 | int ret = check_deadlock(curr, hlock, lock, hlock->read); |
| 1756 | |
| 1757 | if (!ret) |
| 1758 | return 0; |
| 1759 | /* |
| 1760 | * Mark recursive read, as we jump over it when |
| 1761 | * building dependencies (just like we jump over |
| 1762 | * trylock entries): |
| 1763 | */ |
| 1764 | if (ret == 2) |
| 1765 | hlock->read = 2; |
| 1766 | /* |
| 1767 | * Add dependency only if this lock is not the head |
| 1768 | * of the chain, and if it's not a secondary read-lock: |
| 1769 | */ |
| 1770 | if (!chain_head && ret != 2) |
| 1771 | if (!check_prevs_add(curr, hlock)) |
| 1772 | return 0; |
| 1773 | graph_unlock(); |
| 1774 | } else |
| 1775 | /* after lookup_chain_cache(): */ |
| 1776 | if (unlikely(!debug_locks)) |
| 1777 | return 0; |
| 1778 | |
| 1779 | return 1; |
| 1780 | } |
| 1781 | #else |
| 1782 | static inline int validate_chain(struct task_struct *curr, |
| 1783 | struct lockdep_map *lock, struct held_lock *hlock, |
Gregory Haskins | 3aa416b | 2007-10-11 22:11:11 +0200 | [diff] [blame] | 1784 | int chain_head, u64 chain_key) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1785 | { |
| 1786 | return 1; |
| 1787 | } |
Peter Zijlstra | ca58abc | 2007-07-19 01:48:53 -0700 | [diff] [blame] | 1788 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1789 | |
| 1790 | /* |
| 1791 | * We are building curr_chain_key incrementally, so double-check |
| 1792 | * it from scratch, to make sure that it's done correctly: |
| 1793 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 1794 | static void check_chain_key(struct task_struct *curr) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1795 | { |
| 1796 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 1797 | struct held_lock *hlock, *prev_hlock = NULL; |
| 1798 | unsigned int i, id; |
| 1799 | u64 chain_key = 0; |
| 1800 | |
| 1801 | for (i = 0; i < curr->lockdep_depth; i++) { |
| 1802 | hlock = curr->held_locks + i; |
| 1803 | if (chain_key != hlock->prev_chain_key) { |
| 1804 | debug_locks_off(); |
Arjan van de Ven | 2df8b1d | 2008-07-30 12:43:11 -0700 | [diff] [blame] | 1805 | WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n", |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1806 | curr->lockdep_depth, i, |
| 1807 | (unsigned long long)chain_key, |
| 1808 | (unsigned long long)hlock->prev_chain_key); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1809 | return; |
| 1810 | } |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1811 | id = hlock->class_idx - 1; |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 1812 | if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS)) |
| 1813 | return; |
| 1814 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1815 | if (prev_hlock && (prev_hlock->irq_context != |
| 1816 | hlock->irq_context)) |
| 1817 | chain_key = 0; |
| 1818 | chain_key = iterate_chain_key(chain_key, id); |
| 1819 | prev_hlock = hlock; |
| 1820 | } |
| 1821 | if (chain_key != curr->curr_chain_key) { |
| 1822 | debug_locks_off(); |
Arjan van de Ven | 2df8b1d | 2008-07-30 12:43:11 -0700 | [diff] [blame] | 1823 | WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n", |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1824 | curr->lockdep_depth, i, |
| 1825 | (unsigned long long)chain_key, |
| 1826 | (unsigned long long)curr->curr_chain_key); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1827 | } |
| 1828 | #endif |
| 1829 | } |
| 1830 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1831 | static int |
| 1832 | print_usage_bug(struct task_struct *curr, struct held_lock *this, |
| 1833 | enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit) |
| 1834 | { |
| 1835 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
| 1836 | return 0; |
| 1837 | |
| 1838 | printk("\n=================================\n"); |
| 1839 | printk( "[ INFO: inconsistent lock state ]\n"); |
| 1840 | print_kernel_version(); |
| 1841 | printk( "---------------------------------\n"); |
| 1842 | |
| 1843 | printk("inconsistent {%s} -> {%s} usage.\n", |
| 1844 | usage_str[prev_bit], usage_str[new_bit]); |
| 1845 | |
| 1846 | printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 1847 | curr->comm, task_pid_nr(curr), |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1848 | trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT, |
| 1849 | trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT, |
| 1850 | trace_hardirqs_enabled(curr), |
| 1851 | trace_softirqs_enabled(curr)); |
| 1852 | print_lock(this); |
| 1853 | |
| 1854 | printk("{%s} state was registered at:\n", usage_str[prev_bit]); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1855 | print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1856 | |
| 1857 | print_irqtrace_events(curr); |
| 1858 | printk("\nother info that might help us debug this:\n"); |
| 1859 | lockdep_print_held_locks(curr); |
| 1860 | |
| 1861 | printk("\nstack backtrace:\n"); |
| 1862 | dump_stack(); |
| 1863 | |
| 1864 | return 0; |
| 1865 | } |
| 1866 | |
| 1867 | /* |
| 1868 | * Print out an error if an invalid bit is set: |
| 1869 | */ |
| 1870 | static inline int |
| 1871 | valid_state(struct task_struct *curr, struct held_lock *this, |
| 1872 | enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit) |
| 1873 | { |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1874 | if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1875 | return print_usage_bug(curr, this, bad_bit, new_bit); |
| 1876 | return 1; |
| 1877 | } |
| 1878 | |
| 1879 | static int mark_lock(struct task_struct *curr, struct held_lock *this, |
| 1880 | enum lock_usage_bit new_bit); |
| 1881 | |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1882 | #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1883 | |
| 1884 | /* |
| 1885 | * print irq inversion bug: |
| 1886 | */ |
| 1887 | static int |
| 1888 | print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other, |
| 1889 | struct held_lock *this, int forwards, |
| 1890 | const char *irqclass) |
| 1891 | { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1892 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1893 | return 0; |
| 1894 | |
| 1895 | printk("\n=========================================================\n"); |
| 1896 | printk( "[ INFO: possible irq lock inversion dependency detected ]\n"); |
Dave Jones | 99de055 | 2006-09-29 02:00:10 -0700 | [diff] [blame] | 1897 | print_kernel_version(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1898 | printk( "---------------------------------------------------------\n"); |
| 1899 | printk("%s/%d just changed the state of lock:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 1900 | curr->comm, task_pid_nr(curr)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1901 | print_lock(this); |
| 1902 | if (forwards) |
Peter Zijlstra | 26575e2 | 2009-03-04 14:53:24 +0100 | [diff] [blame^] | 1903 | printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1904 | else |
Peter Zijlstra | 26575e2 | 2009-03-04 14:53:24 +0100 | [diff] [blame^] | 1905 | printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1906 | print_lock_name(other); |
| 1907 | printk("\n\nand interrupts could create inverse lock ordering between them.\n\n"); |
| 1908 | |
| 1909 | printk("\nother info that might help us debug this:\n"); |
| 1910 | lockdep_print_held_locks(curr); |
| 1911 | |
| 1912 | printk("\nthe first lock's dependencies:\n"); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1913 | print_lock_dependencies(hlock_class(this), 0); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1914 | |
| 1915 | printk("\nthe second lock's dependencies:\n"); |
| 1916 | print_lock_dependencies(other, 0); |
| 1917 | |
| 1918 | printk("\nstack backtrace:\n"); |
| 1919 | dump_stack(); |
| 1920 | |
| 1921 | return 0; |
| 1922 | } |
| 1923 | |
| 1924 | /* |
| 1925 | * Prove that in the forwards-direction subgraph starting at <this> |
| 1926 | * there is no lock matching <mask>: |
| 1927 | */ |
| 1928 | static int |
| 1929 | check_usage_forwards(struct task_struct *curr, struct held_lock *this, |
| 1930 | enum lock_usage_bit bit, const char *irqclass) |
| 1931 | { |
| 1932 | int ret; |
| 1933 | |
| 1934 | find_usage_bit = bit; |
| 1935 | /* fills in <forwards_match> */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1936 | ret = find_usage_forwards(hlock_class(this), 0); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1937 | if (!ret || ret == 1) |
| 1938 | return ret; |
| 1939 | |
| 1940 | return print_irq_inversion_bug(curr, forwards_match, this, 1, irqclass); |
| 1941 | } |
| 1942 | |
| 1943 | /* |
| 1944 | * Prove that in the backwards-direction subgraph starting at <this> |
| 1945 | * there is no lock matching <mask>: |
| 1946 | */ |
| 1947 | static int |
| 1948 | check_usage_backwards(struct task_struct *curr, struct held_lock *this, |
| 1949 | enum lock_usage_bit bit, const char *irqclass) |
| 1950 | { |
| 1951 | int ret; |
| 1952 | |
| 1953 | find_usage_bit = bit; |
| 1954 | /* fills in <backwards_match> */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1955 | ret = find_usage_backwards(hlock_class(this), 0); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1956 | if (!ret || ret == 1) |
| 1957 | return ret; |
| 1958 | |
| 1959 | return print_irq_inversion_bug(curr, backwards_match, this, 0, irqclass); |
| 1960 | } |
| 1961 | |
Ingo Molnar | 3117df0 | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1962 | void print_irqtrace_events(struct task_struct *curr) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1963 | { |
| 1964 | printk("irq event stamp: %u\n", curr->irq_events); |
| 1965 | printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event); |
| 1966 | print_ip_sym(curr->hardirq_enable_ip); |
| 1967 | printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event); |
| 1968 | print_ip_sym(curr->hardirq_disable_ip); |
| 1969 | printk("softirqs last enabled at (%u): ", curr->softirq_enable_event); |
| 1970 | print_ip_sym(curr->softirq_enable_ip); |
| 1971 | printk("softirqs last disabled at (%u): ", curr->softirq_disable_event); |
| 1972 | print_ip_sym(curr->softirq_disable_ip); |
| 1973 | } |
| 1974 | |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 1975 | static int HARDIRQ_verbose(struct lock_class *class) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1976 | { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1977 | #if HARDIRQ_VERBOSE |
| 1978 | return class_filter(class); |
| 1979 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1980 | return 0; |
| 1981 | } |
| 1982 | |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 1983 | static int SOFTIRQ_verbose(struct lock_class *class) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1984 | { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1985 | #if SOFTIRQ_VERBOSE |
| 1986 | return class_filter(class); |
| 1987 | #endif |
| 1988 | return 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 1991 | static int RECLAIM_FS_verbose(struct lock_class *class) |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 1992 | { |
| 1993 | #if RECLAIM_VERBOSE |
| 1994 | return class_filter(class); |
| 1995 | #endif |
| 1996 | return 0; |
| 1997 | } |
| 1998 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1999 | #define STRICT_READ_CHECKS 1 |
| 2000 | |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 2001 | static int (*state_verbose_f[])(struct lock_class *class) = { |
| 2002 | #define LOCKDEP_STATE(__STATE) \ |
| 2003 | __STATE##_verbose, |
| 2004 | #include "lockdep_states.h" |
| 2005 | #undef LOCKDEP_STATE |
| 2006 | }; |
| 2007 | |
| 2008 | static inline int state_verbose(enum lock_usage_bit bit, |
| 2009 | struct lock_class *class) |
| 2010 | { |
| 2011 | return state_verbose_f[bit >> 2](class); |
| 2012 | } |
| 2013 | |
Peter Zijlstra | 42c50d5 | 2009-01-22 16:58:16 +0100 | [diff] [blame] | 2014 | typedef int (*check_usage_f)(struct task_struct *, struct held_lock *, |
| 2015 | enum lock_usage_bit bit, const char *name); |
| 2016 | |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 2017 | static int |
Peter Zijlstra | 1c21f14 | 2009-03-04 13:51:13 +0100 | [diff] [blame] | 2018 | mark_lock_irq(struct task_struct *curr, struct held_lock *this, |
| 2019 | enum lock_usage_bit new_bit) |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 2020 | { |
Peter Zijlstra | f989209 | 2009-01-22 16:09:59 +0100 | [diff] [blame] | 2021 | int excl_bit = exclusive_bit(new_bit); |
Peter Zijlstra | 9d3651a | 2009-01-22 17:18:32 +0100 | [diff] [blame] | 2022 | int read = new_bit & 1; |
Peter Zijlstra | 42c50d5 | 2009-01-22 16:58:16 +0100 | [diff] [blame] | 2023 | int dir = new_bit & 2; |
| 2024 | |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 2025 | /* |
| 2026 | * mark USED_IN has to look forwards -- to ensure no dependency |
| 2027 | * has ENABLED state, which would allow recursion deadlocks. |
| 2028 | * |
| 2029 | * mark ENABLED has to look backwards -- to ensure no dependee |
| 2030 | * has USED_IN state, which, again, would allow recursion deadlocks. |
| 2031 | */ |
Peter Zijlstra | 42c50d5 | 2009-01-22 16:58:16 +0100 | [diff] [blame] | 2032 | check_usage_f usage = dir ? |
| 2033 | check_usage_backwards : check_usage_forwards; |
Peter Zijlstra | f989209 | 2009-01-22 16:09:59 +0100 | [diff] [blame] | 2034 | |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 2035 | /* |
| 2036 | * Validate that this particular lock does not have conflicting |
| 2037 | * usage states. |
| 2038 | */ |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 2039 | if (!valid_state(curr, this, new_bit, excl_bit)) |
| 2040 | return 0; |
Peter Zijlstra | 9d3651a | 2009-01-22 17:18:32 +0100 | [diff] [blame] | 2041 | |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 2042 | /* |
| 2043 | * Validate that the lock dependencies don't have conflicting usage |
| 2044 | * states. |
| 2045 | */ |
| 2046 | if ((!read || !dir || STRICT_READ_CHECKS) && |
Peter Zijlstra | 1c21f14 | 2009-03-04 13:51:13 +0100 | [diff] [blame] | 2047 | !usage(curr, this, excl_bit, state_name(new_bit & ~1))) |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 2048 | return 0; |
Peter Zijlstra | 780e820 | 2009-01-22 16:51:29 +0100 | [diff] [blame] | 2049 | |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 2050 | /* |
| 2051 | * Check for read in write conflicts |
| 2052 | */ |
| 2053 | if (!read) { |
| 2054 | if (!valid_state(curr, this, new_bit, excl_bit + 1)) |
| 2055 | return 0; |
| 2056 | |
| 2057 | if (STRICT_READ_CHECKS && |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 2058 | !usage(curr, this, excl_bit + 1, |
| 2059 | state_name(new_bit + 1))) |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 2060 | return 0; |
| 2061 | } |
Peter Zijlstra | 780e820 | 2009-01-22 16:51:29 +0100 | [diff] [blame] | 2062 | |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 2063 | if (state_verbose(new_bit, hlock_class(this))) |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 2064 | return 2; |
| 2065 | |
| 2066 | return 1; |
| 2067 | } |
| 2068 | |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 2069 | enum mark_type { |
Peter Zijlstra | 36bfb9b | 2009-01-22 14:12:41 +0100 | [diff] [blame] | 2070 | #define LOCKDEP_STATE(__STATE) __STATE, |
| 2071 | #include "lockdep_states.h" |
| 2072 | #undef LOCKDEP_STATE |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 2073 | }; |
| 2074 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2075 | /* |
| 2076 | * Mark all held locks with a usage bit: |
| 2077 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 2078 | static int |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 2079 | mark_held_locks(struct task_struct *curr, enum mark_type mark) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2080 | { |
| 2081 | enum lock_usage_bit usage_bit; |
| 2082 | struct held_lock *hlock; |
| 2083 | int i; |
| 2084 | |
| 2085 | for (i = 0; i < curr->lockdep_depth; i++) { |
| 2086 | hlock = curr->held_locks + i; |
| 2087 | |
Peter Zijlstra | cf2ad4d | 2009-01-27 13:58:08 +0100 | [diff] [blame] | 2088 | usage_bit = 2 + (mark << 2); /* ENABLED */ |
| 2089 | if (hlock->read) |
| 2090 | usage_bit += 1; /* READ */ |
| 2091 | |
| 2092 | BUG_ON(usage_bit >= LOCK_USAGE_STATES); |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 2093 | |
Jarek Poplawski | 4ff773bb | 2007-05-08 00:31:00 -0700 | [diff] [blame] | 2094 | if (!mark_lock(curr, hlock, usage_bit)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2095 | return 0; |
| 2096 | } |
| 2097 | |
| 2098 | return 1; |
| 2099 | } |
| 2100 | |
| 2101 | /* |
| 2102 | * Debugging helper: via this flag we know that we are in |
| 2103 | * 'early bootup code', and will warn about any invalid irqs-on event: |
| 2104 | */ |
| 2105 | static int early_boot_irqs_enabled; |
| 2106 | |
| 2107 | void early_boot_irqs_off(void) |
| 2108 | { |
| 2109 | early_boot_irqs_enabled = 0; |
| 2110 | } |
| 2111 | |
| 2112 | void early_boot_irqs_on(void) |
| 2113 | { |
| 2114 | early_boot_irqs_enabled = 1; |
| 2115 | } |
| 2116 | |
| 2117 | /* |
| 2118 | * Hardirqs will be enabled: |
| 2119 | */ |
Heiko Carstens | 6afe40b | 2008-10-28 11:14:58 +0100 | [diff] [blame] | 2120 | void trace_hardirqs_on_caller(unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2121 | { |
| 2122 | struct task_struct *curr = current; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2123 | |
Heiko Carstens | 6afe40b | 2008-10-28 11:14:58 +0100 | [diff] [blame] | 2124 | time_hardirqs_on(CALLER_ADDR0, ip); |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2125 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2126 | if (unlikely(!debug_locks || current->lockdep_recursion)) |
| 2127 | return; |
| 2128 | |
| 2129 | if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled))) |
| 2130 | return; |
| 2131 | |
| 2132 | if (unlikely(curr->hardirqs_enabled)) { |
| 2133 | debug_atomic_inc(&redundant_hardirqs_on); |
| 2134 | return; |
| 2135 | } |
| 2136 | /* we'll do an OFF -> ON transition: */ |
| 2137 | curr->hardirqs_enabled = 1; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2138 | |
| 2139 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 2140 | return; |
| 2141 | if (DEBUG_LOCKS_WARN_ON(current->hardirq_context)) |
| 2142 | return; |
| 2143 | /* |
| 2144 | * We are going to turn hardirqs on, so set the |
| 2145 | * usage bit for all held locks: |
| 2146 | */ |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 2147 | if (!mark_held_locks(curr, HARDIRQ)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2148 | return; |
| 2149 | /* |
| 2150 | * If we have softirqs enabled, then set the usage |
| 2151 | * bit for all held locks. (disabled hardirqs prevented |
| 2152 | * this bit from being set before) |
| 2153 | */ |
| 2154 | if (curr->softirqs_enabled) |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 2155 | if (!mark_held_locks(curr, SOFTIRQ)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2156 | return; |
| 2157 | |
| 2158 | curr->hardirq_enable_ip = ip; |
| 2159 | curr->hardirq_enable_event = ++curr->irq_events; |
| 2160 | debug_atomic_inc(&hardirqs_on_events); |
| 2161 | } |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2162 | EXPORT_SYMBOL(trace_hardirqs_on_caller); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2163 | |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 2164 | void trace_hardirqs_on(void) |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2165 | { |
| 2166 | trace_hardirqs_on_caller(CALLER_ADDR0); |
| 2167 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2168 | EXPORT_SYMBOL(trace_hardirqs_on); |
| 2169 | |
| 2170 | /* |
| 2171 | * Hardirqs were disabled: |
| 2172 | */ |
Heiko Carstens | 6afe40b | 2008-10-28 11:14:58 +0100 | [diff] [blame] | 2173 | void trace_hardirqs_off_caller(unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2174 | { |
| 2175 | struct task_struct *curr = current; |
| 2176 | |
Heiko Carstens | 6afe40b | 2008-10-28 11:14:58 +0100 | [diff] [blame] | 2177 | time_hardirqs_off(CALLER_ADDR0, ip); |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2178 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2179 | if (unlikely(!debug_locks || current->lockdep_recursion)) |
| 2180 | return; |
| 2181 | |
| 2182 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 2183 | return; |
| 2184 | |
| 2185 | if (curr->hardirqs_enabled) { |
| 2186 | /* |
| 2187 | * We have done an ON -> OFF transition: |
| 2188 | */ |
| 2189 | curr->hardirqs_enabled = 0; |
Heiko Carstens | 6afe40b | 2008-10-28 11:14:58 +0100 | [diff] [blame] | 2190 | curr->hardirq_disable_ip = ip; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2191 | curr->hardirq_disable_event = ++curr->irq_events; |
| 2192 | debug_atomic_inc(&hardirqs_off_events); |
| 2193 | } else |
| 2194 | debug_atomic_inc(&redundant_hardirqs_off); |
| 2195 | } |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2196 | EXPORT_SYMBOL(trace_hardirqs_off_caller); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2197 | |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 2198 | void trace_hardirqs_off(void) |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2199 | { |
| 2200 | trace_hardirqs_off_caller(CALLER_ADDR0); |
| 2201 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2202 | EXPORT_SYMBOL(trace_hardirqs_off); |
| 2203 | |
| 2204 | /* |
| 2205 | * Softirqs will be enabled: |
| 2206 | */ |
| 2207 | void trace_softirqs_on(unsigned long ip) |
| 2208 | { |
| 2209 | struct task_struct *curr = current; |
| 2210 | |
| 2211 | if (unlikely(!debug_locks)) |
| 2212 | return; |
| 2213 | |
| 2214 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 2215 | return; |
| 2216 | |
| 2217 | if (curr->softirqs_enabled) { |
| 2218 | debug_atomic_inc(&redundant_softirqs_on); |
| 2219 | return; |
| 2220 | } |
| 2221 | |
| 2222 | /* |
| 2223 | * We'll do an OFF -> ON transition: |
| 2224 | */ |
| 2225 | curr->softirqs_enabled = 1; |
| 2226 | curr->softirq_enable_ip = ip; |
| 2227 | curr->softirq_enable_event = ++curr->irq_events; |
| 2228 | debug_atomic_inc(&softirqs_on_events); |
| 2229 | /* |
| 2230 | * We are going to turn softirqs on, so set the |
| 2231 | * usage bit for all held locks, if hardirqs are |
| 2232 | * enabled too: |
| 2233 | */ |
| 2234 | if (curr->hardirqs_enabled) |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 2235 | mark_held_locks(curr, SOFTIRQ); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | /* |
| 2239 | * Softirqs were disabled: |
| 2240 | */ |
| 2241 | void trace_softirqs_off(unsigned long ip) |
| 2242 | { |
| 2243 | struct task_struct *curr = current; |
| 2244 | |
| 2245 | if (unlikely(!debug_locks)) |
| 2246 | return; |
| 2247 | |
| 2248 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 2249 | return; |
| 2250 | |
| 2251 | if (curr->softirqs_enabled) { |
| 2252 | /* |
| 2253 | * We have done an ON -> OFF transition: |
| 2254 | */ |
| 2255 | curr->softirqs_enabled = 0; |
| 2256 | curr->softirq_disable_ip = ip; |
| 2257 | curr->softirq_disable_event = ++curr->irq_events; |
| 2258 | debug_atomic_inc(&softirqs_off_events); |
| 2259 | DEBUG_LOCKS_WARN_ON(!softirq_count()); |
| 2260 | } else |
| 2261 | debug_atomic_inc(&redundant_softirqs_off); |
| 2262 | } |
| 2263 | |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 2264 | void lockdep_trace_alloc(gfp_t gfp_mask) |
| 2265 | { |
| 2266 | struct task_struct *curr = current; |
| 2267 | |
| 2268 | if (unlikely(!debug_locks)) |
| 2269 | return; |
| 2270 | |
| 2271 | /* no reclaim without waiting on it */ |
| 2272 | if (!(gfp_mask & __GFP_WAIT)) |
| 2273 | return; |
| 2274 | |
| 2275 | /* this guy won't enter reclaim */ |
| 2276 | if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC)) |
| 2277 | return; |
| 2278 | |
| 2279 | /* We're only interested __GFP_FS allocations for now */ |
| 2280 | if (!(gfp_mask & __GFP_FS)) |
| 2281 | return; |
| 2282 | |
| 2283 | if (DEBUG_LOCKS_WARN_ON(irqs_disabled())) |
| 2284 | return; |
| 2285 | |
| 2286 | mark_held_locks(curr, RECLAIM_FS); |
| 2287 | } |
| 2288 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2289 | static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock) |
| 2290 | { |
| 2291 | /* |
| 2292 | * If non-trylock use in a hardirq or softirq context, then |
| 2293 | * mark the lock as used in these contexts: |
| 2294 | */ |
| 2295 | if (!hlock->trylock) { |
| 2296 | if (hlock->read) { |
| 2297 | if (curr->hardirq_context) |
| 2298 | if (!mark_lock(curr, hlock, |
| 2299 | LOCK_USED_IN_HARDIRQ_READ)) |
| 2300 | return 0; |
| 2301 | if (curr->softirq_context) |
| 2302 | if (!mark_lock(curr, hlock, |
| 2303 | LOCK_USED_IN_SOFTIRQ_READ)) |
| 2304 | return 0; |
| 2305 | } else { |
| 2306 | if (curr->hardirq_context) |
| 2307 | if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ)) |
| 2308 | return 0; |
| 2309 | if (curr->softirq_context) |
| 2310 | if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ)) |
| 2311 | return 0; |
| 2312 | } |
| 2313 | } |
| 2314 | if (!hlock->hardirqs_off) { |
| 2315 | if (hlock->read) { |
| 2316 | if (!mark_lock(curr, hlock, |
Peter Zijlstra | 4fc95e8 | 2009-01-22 13:10:52 +0100 | [diff] [blame] | 2317 | LOCK_ENABLED_HARDIRQ_READ)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2318 | return 0; |
| 2319 | if (curr->softirqs_enabled) |
| 2320 | if (!mark_lock(curr, hlock, |
Peter Zijlstra | 4fc95e8 | 2009-01-22 13:10:52 +0100 | [diff] [blame] | 2321 | LOCK_ENABLED_SOFTIRQ_READ)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2322 | return 0; |
| 2323 | } else { |
| 2324 | if (!mark_lock(curr, hlock, |
Peter Zijlstra | 4fc95e8 | 2009-01-22 13:10:52 +0100 | [diff] [blame] | 2325 | LOCK_ENABLED_HARDIRQ)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2326 | return 0; |
| 2327 | if (curr->softirqs_enabled) |
| 2328 | if (!mark_lock(curr, hlock, |
Peter Zijlstra | 4fc95e8 | 2009-01-22 13:10:52 +0100 | [diff] [blame] | 2329 | LOCK_ENABLED_SOFTIRQ)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2330 | return 0; |
| 2331 | } |
| 2332 | } |
| 2333 | |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 2334 | /* |
| 2335 | * We reuse the irq context infrastructure more broadly as a general |
| 2336 | * context checking code. This tests GFP_FS recursion (a lock taken |
| 2337 | * during reclaim for a GFP_FS allocation is held over a GFP_FS |
| 2338 | * allocation). |
| 2339 | */ |
| 2340 | if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) { |
| 2341 | if (hlock->read) { |
| 2342 | if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ)) |
| 2343 | return 0; |
| 2344 | } else { |
| 2345 | if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS)) |
| 2346 | return 0; |
| 2347 | } |
| 2348 | } |
| 2349 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2350 | return 1; |
| 2351 | } |
| 2352 | |
| 2353 | static int separate_irq_context(struct task_struct *curr, |
| 2354 | struct held_lock *hlock) |
| 2355 | { |
| 2356 | unsigned int depth = curr->lockdep_depth; |
| 2357 | |
| 2358 | /* |
| 2359 | * Keep track of points where we cross into an interrupt context: |
| 2360 | */ |
| 2361 | hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) + |
| 2362 | curr->softirq_context; |
| 2363 | if (depth) { |
| 2364 | struct held_lock *prev_hlock; |
| 2365 | |
| 2366 | prev_hlock = curr->held_locks + depth-1; |
| 2367 | /* |
| 2368 | * If we cross into another context, reset the |
| 2369 | * hash key (this also prevents the checking and the |
| 2370 | * adding of the dependency to 'prev'): |
| 2371 | */ |
| 2372 | if (prev_hlock->irq_context != hlock->irq_context) |
| 2373 | return 1; |
| 2374 | } |
| 2375 | return 0; |
| 2376 | } |
| 2377 | |
| 2378 | #else |
| 2379 | |
| 2380 | static inline |
| 2381 | int mark_lock_irq(struct task_struct *curr, struct held_lock *this, |
| 2382 | enum lock_usage_bit new_bit) |
| 2383 | { |
| 2384 | WARN_ON(1); |
| 2385 | return 1; |
| 2386 | } |
| 2387 | |
| 2388 | static inline int mark_irqflags(struct task_struct *curr, |
| 2389 | struct held_lock *hlock) |
| 2390 | { |
| 2391 | return 1; |
| 2392 | } |
| 2393 | |
| 2394 | static inline int separate_irq_context(struct task_struct *curr, |
| 2395 | struct held_lock *hlock) |
| 2396 | { |
| 2397 | return 0; |
| 2398 | } |
| 2399 | |
Peter Zijlstra | 868a23a | 2009-02-15 00:25:21 +0100 | [diff] [blame] | 2400 | void lockdep_trace_alloc(gfp_t gfp_mask) |
| 2401 | { |
| 2402 | } |
| 2403 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2404 | #endif |
| 2405 | |
| 2406 | /* |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2407 | * Mark a lock with a usage bit, and validate the state transition: |
| 2408 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 2409 | static int mark_lock(struct task_struct *curr, struct held_lock *this, |
Steven Rostedt | 0764d23 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 2410 | enum lock_usage_bit new_bit) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2411 | { |
| 2412 | unsigned int new_mask = 1 << new_bit, ret = 1; |
| 2413 | |
| 2414 | /* |
| 2415 | * If already set then do not dirty the cacheline, |
| 2416 | * nor do any checks: |
| 2417 | */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2418 | if (likely(hlock_class(this)->usage_mask & new_mask)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2419 | return 1; |
| 2420 | |
| 2421 | if (!graph_lock()) |
| 2422 | return 0; |
| 2423 | /* |
| 2424 | * Make sure we didnt race: |
| 2425 | */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2426 | if (unlikely(hlock_class(this)->usage_mask & new_mask)) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2427 | graph_unlock(); |
| 2428 | return 1; |
| 2429 | } |
| 2430 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2431 | hlock_class(this)->usage_mask |= new_mask; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2432 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2433 | if (!save_trace(hlock_class(this)->usage_traces + new_bit)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2434 | return 0; |
| 2435 | |
| 2436 | switch (new_bit) { |
Peter Zijlstra | 5346417 | 2009-01-22 14:15:53 +0100 | [diff] [blame] | 2437 | #define LOCKDEP_STATE(__STATE) \ |
| 2438 | case LOCK_USED_IN_##__STATE: \ |
| 2439 | case LOCK_USED_IN_##__STATE##_READ: \ |
| 2440 | case LOCK_ENABLED_##__STATE: \ |
| 2441 | case LOCK_ENABLED_##__STATE##_READ: |
| 2442 | #include "lockdep_states.h" |
| 2443 | #undef LOCKDEP_STATE |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2444 | ret = mark_lock_irq(curr, this, new_bit); |
| 2445 | if (!ret) |
| 2446 | return 0; |
| 2447 | break; |
| 2448 | case LOCK_USED: |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2449 | debug_atomic_dec(&nr_unused_locks); |
| 2450 | break; |
| 2451 | default: |
| 2452 | if (!debug_locks_off_graph_unlock()) |
| 2453 | return 0; |
| 2454 | WARN_ON(1); |
| 2455 | return 0; |
| 2456 | } |
| 2457 | |
| 2458 | graph_unlock(); |
| 2459 | |
| 2460 | /* |
| 2461 | * We must printk outside of the graph_lock: |
| 2462 | */ |
| 2463 | if (ret == 2) { |
| 2464 | printk("\nmarked lock as {%s}:\n", usage_str[new_bit]); |
| 2465 | print_lock(this); |
| 2466 | print_irqtrace_events(curr); |
| 2467 | dump_stack(); |
| 2468 | } |
| 2469 | |
| 2470 | return ret; |
| 2471 | } |
| 2472 | |
| 2473 | /* |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2474 | * Initialize a lock instance's lock-class mapping info: |
| 2475 | */ |
| 2476 | void lockdep_init_map(struct lockdep_map *lock, const char *name, |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 2477 | struct lock_class_key *key, int subclass) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2478 | { |
| 2479 | if (unlikely(!debug_locks)) |
| 2480 | return; |
| 2481 | |
| 2482 | if (DEBUG_LOCKS_WARN_ON(!key)) |
| 2483 | return; |
| 2484 | if (DEBUG_LOCKS_WARN_ON(!name)) |
| 2485 | return; |
| 2486 | /* |
| 2487 | * Sanity check, the lock-class key must be persistent: |
| 2488 | */ |
| 2489 | if (!static_obj(key)) { |
| 2490 | printk("BUG: key %p not in .data!\n", key); |
| 2491 | DEBUG_LOCKS_WARN_ON(1); |
| 2492 | return; |
| 2493 | } |
| 2494 | lock->name = name; |
| 2495 | lock->key = key; |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 2496 | lock->class_cache = NULL; |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 2497 | #ifdef CONFIG_LOCK_STAT |
| 2498 | lock->cpu = raw_smp_processor_id(); |
| 2499 | #endif |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 2500 | if (subclass) |
| 2501 | register_lock_class(lock, subclass, 1); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2502 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2503 | EXPORT_SYMBOL_GPL(lockdep_init_map); |
| 2504 | |
| 2505 | /* |
| 2506 | * This gets called for every mutex_lock*()/spin_lock*() operation. |
| 2507 | * We maintain the dependency maps and validate the locking attempt: |
| 2508 | */ |
| 2509 | static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, |
| 2510 | int trylock, int read, int check, int hardirqs_off, |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 2511 | struct lockdep_map *nest_lock, unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2512 | { |
| 2513 | struct task_struct *curr = current; |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 2514 | struct lock_class *class = NULL; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2515 | struct held_lock *hlock; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2516 | unsigned int depth, id; |
| 2517 | int chain_head = 0; |
| 2518 | u64 chain_key; |
| 2519 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 2520 | if (!prove_locking) |
| 2521 | check = 1; |
| 2522 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2523 | if (unlikely(!debug_locks)) |
| 2524 | return 0; |
| 2525 | |
| 2526 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 2527 | return 0; |
| 2528 | |
| 2529 | if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) { |
| 2530 | debug_locks_off(); |
| 2531 | printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n"); |
| 2532 | printk("turning off the locking correctness validator.\n"); |
| 2533 | return 0; |
| 2534 | } |
| 2535 | |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 2536 | if (!subclass) |
| 2537 | class = lock->class_cache; |
| 2538 | /* |
| 2539 | * Not cached yet or subclass? |
| 2540 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2541 | if (unlikely(!class)) { |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 2542 | class = register_lock_class(lock, subclass, 0); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2543 | if (!class) |
| 2544 | return 0; |
| 2545 | } |
| 2546 | debug_atomic_inc((atomic_t *)&class->ops); |
| 2547 | if (very_verbose(class)) { |
| 2548 | printk("\nacquire class [%p] %s", class->key, class->name); |
| 2549 | if (class->name_version > 1) |
| 2550 | printk("#%d", class->name_version); |
| 2551 | printk("\n"); |
| 2552 | dump_stack(); |
| 2553 | } |
| 2554 | |
| 2555 | /* |
| 2556 | * Add the lock to the list of currently held locks. |
| 2557 | * (we dont increase the depth just yet, up until the |
| 2558 | * dependency checks are done) |
| 2559 | */ |
| 2560 | depth = curr->lockdep_depth; |
| 2561 | if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH)) |
| 2562 | return 0; |
| 2563 | |
| 2564 | hlock = curr->held_locks + depth; |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2565 | if (DEBUG_LOCKS_WARN_ON(!class)) |
| 2566 | return 0; |
| 2567 | hlock->class_idx = class - lock_classes + 1; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2568 | hlock->acquire_ip = ip; |
| 2569 | hlock->instance = lock; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 2570 | hlock->nest_lock = nest_lock; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2571 | hlock->trylock = trylock; |
| 2572 | hlock->read = read; |
| 2573 | hlock->check = check; |
Dmitry Baryshkov | 6951b12 | 2008-08-18 04:26:37 +0400 | [diff] [blame] | 2574 | hlock->hardirqs_off = !!hardirqs_off; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 2575 | #ifdef CONFIG_LOCK_STAT |
| 2576 | hlock->waittime_stamp = 0; |
| 2577 | hlock->holdtime_stamp = sched_clock(); |
| 2578 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2579 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2580 | if (check == 2 && !mark_irqflags(curr, hlock)) |
| 2581 | return 0; |
| 2582 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2583 | /* mark it as used: */ |
Jarek Poplawski | 4ff773bb | 2007-05-08 00:31:00 -0700 | [diff] [blame] | 2584 | if (!mark_lock(curr, hlock, LOCK_USED)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2585 | return 0; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2586 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2587 | /* |
Gautham R Shenoy | 17aacfb9 | 2007-10-28 20:47:01 +0100 | [diff] [blame] | 2588 | * Calculate the chain hash: it's the combined hash of all the |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2589 | * lock keys along the dependency chain. We save the hash value |
| 2590 | * at every step so that we can get the current hash easily |
| 2591 | * after unlock. The chain hash is then used to cache dependency |
| 2592 | * results. |
| 2593 | * |
| 2594 | * The 'key ID' is what is the most compact key value to drive |
| 2595 | * the hash, not class->key. |
| 2596 | */ |
| 2597 | id = class - lock_classes; |
| 2598 | if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS)) |
| 2599 | return 0; |
| 2600 | |
| 2601 | chain_key = curr->curr_chain_key; |
| 2602 | if (!depth) { |
| 2603 | if (DEBUG_LOCKS_WARN_ON(chain_key != 0)) |
| 2604 | return 0; |
| 2605 | chain_head = 1; |
| 2606 | } |
| 2607 | |
| 2608 | hlock->prev_chain_key = chain_key; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2609 | if (separate_irq_context(curr, hlock)) { |
| 2610 | chain_key = 0; |
| 2611 | chain_head = 1; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2612 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2613 | chain_key = iterate_chain_key(chain_key, id); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2614 | |
Gregory Haskins | 3aa416b | 2007-10-11 22:11:11 +0200 | [diff] [blame] | 2615 | if (!validate_chain(curr, lock, hlock, chain_head, chain_key)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2616 | return 0; |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 2617 | |
Gregory Haskins | 3aa416b | 2007-10-11 22:11:11 +0200 | [diff] [blame] | 2618 | curr->curr_chain_key = chain_key; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2619 | curr->lockdep_depth++; |
| 2620 | check_chain_key(curr); |
Jarek Poplawski | 60e114d | 2007-02-20 13:58:00 -0800 | [diff] [blame] | 2621 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 2622 | if (unlikely(!debug_locks)) |
| 2623 | return 0; |
| 2624 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2625 | if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) { |
| 2626 | debug_locks_off(); |
| 2627 | printk("BUG: MAX_LOCK_DEPTH too low!\n"); |
| 2628 | printk("turning off the locking correctness validator.\n"); |
| 2629 | return 0; |
| 2630 | } |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 2631 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2632 | if (unlikely(curr->lockdep_depth > max_lockdep_depth)) |
| 2633 | max_lockdep_depth = curr->lockdep_depth; |
| 2634 | |
| 2635 | return 1; |
| 2636 | } |
| 2637 | |
| 2638 | static int |
| 2639 | print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock, |
| 2640 | unsigned long ip) |
| 2641 | { |
| 2642 | if (!debug_locks_off()) |
| 2643 | return 0; |
| 2644 | if (debug_locks_silent) |
| 2645 | return 0; |
| 2646 | |
| 2647 | printk("\n=====================================\n"); |
| 2648 | printk( "[ BUG: bad unlock balance detected! ]\n"); |
| 2649 | printk( "-------------------------------------\n"); |
| 2650 | printk("%s/%d is trying to release lock (", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 2651 | curr->comm, task_pid_nr(curr)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2652 | print_lockdep_cache(lock); |
| 2653 | printk(") at:\n"); |
| 2654 | print_ip_sym(ip); |
| 2655 | printk("but there are no more locks to release!\n"); |
| 2656 | printk("\nother info that might help us debug this:\n"); |
| 2657 | lockdep_print_held_locks(curr); |
| 2658 | |
| 2659 | printk("\nstack backtrace:\n"); |
| 2660 | dump_stack(); |
| 2661 | |
| 2662 | return 0; |
| 2663 | } |
| 2664 | |
| 2665 | /* |
| 2666 | * Common debugging checks for both nested and non-nested unlock: |
| 2667 | */ |
| 2668 | static int check_unlock(struct task_struct *curr, struct lockdep_map *lock, |
| 2669 | unsigned long ip) |
| 2670 | { |
| 2671 | if (unlikely(!debug_locks)) |
| 2672 | return 0; |
| 2673 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 2674 | return 0; |
| 2675 | |
| 2676 | if (curr->lockdep_depth <= 0) |
| 2677 | return print_unlock_inbalance_bug(curr, lock, ip); |
| 2678 | |
| 2679 | return 1; |
| 2680 | } |
| 2681 | |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 2682 | static int |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 2683 | __lock_set_class(struct lockdep_map *lock, const char *name, |
| 2684 | struct lock_class_key *key, unsigned int subclass, |
| 2685 | unsigned long ip) |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 2686 | { |
| 2687 | struct task_struct *curr = current; |
| 2688 | struct held_lock *hlock, *prev_hlock; |
| 2689 | struct lock_class *class; |
| 2690 | unsigned int depth; |
| 2691 | int i; |
| 2692 | |
| 2693 | depth = curr->lockdep_depth; |
| 2694 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
| 2695 | return 0; |
| 2696 | |
| 2697 | prev_hlock = NULL; |
| 2698 | for (i = depth-1; i >= 0; i--) { |
| 2699 | hlock = curr->held_locks + i; |
| 2700 | /* |
| 2701 | * We must not cross into another context: |
| 2702 | */ |
| 2703 | if (prev_hlock && prev_hlock->irq_context != hlock->irq_context) |
| 2704 | break; |
| 2705 | if (hlock->instance == lock) |
| 2706 | goto found_it; |
| 2707 | prev_hlock = hlock; |
| 2708 | } |
| 2709 | return print_unlock_inbalance_bug(curr, lock, ip); |
| 2710 | |
| 2711 | found_it: |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 2712 | lockdep_init_map(lock, name, key, 0); |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 2713 | class = register_lock_class(lock, subclass, 0); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2714 | hlock->class_idx = class - lock_classes + 1; |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 2715 | |
| 2716 | curr->lockdep_depth = i; |
| 2717 | curr->curr_chain_key = hlock->prev_chain_key; |
| 2718 | |
| 2719 | for (; i < depth; i++) { |
| 2720 | hlock = curr->held_locks + i; |
| 2721 | if (!__lock_acquire(hlock->instance, |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2722 | hlock_class(hlock)->subclass, hlock->trylock, |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 2723 | hlock->read, hlock->check, hlock->hardirqs_off, |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 2724 | hlock->nest_lock, hlock->acquire_ip)) |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 2725 | return 0; |
| 2726 | } |
| 2727 | |
| 2728 | if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth)) |
| 2729 | return 0; |
| 2730 | return 1; |
| 2731 | } |
| 2732 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2733 | /* |
| 2734 | * Remove the lock to the list of currently held locks in a |
| 2735 | * potentially non-nested (out of order) manner. This is a |
| 2736 | * relatively rare operation, as all the unlock APIs default |
| 2737 | * to nested mode (which uses lock_release()): |
| 2738 | */ |
| 2739 | static int |
| 2740 | lock_release_non_nested(struct task_struct *curr, |
| 2741 | struct lockdep_map *lock, unsigned long ip) |
| 2742 | { |
| 2743 | struct held_lock *hlock, *prev_hlock; |
| 2744 | unsigned int depth; |
| 2745 | int i; |
| 2746 | |
| 2747 | /* |
| 2748 | * Check whether the lock exists in the current stack |
| 2749 | * of held locks: |
| 2750 | */ |
| 2751 | depth = curr->lockdep_depth; |
| 2752 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
| 2753 | return 0; |
| 2754 | |
| 2755 | prev_hlock = NULL; |
| 2756 | for (i = depth-1; i >= 0; i--) { |
| 2757 | hlock = curr->held_locks + i; |
| 2758 | /* |
| 2759 | * We must not cross into another context: |
| 2760 | */ |
| 2761 | if (prev_hlock && prev_hlock->irq_context != hlock->irq_context) |
| 2762 | break; |
| 2763 | if (hlock->instance == lock) |
| 2764 | goto found_it; |
| 2765 | prev_hlock = hlock; |
| 2766 | } |
| 2767 | return print_unlock_inbalance_bug(curr, lock, ip); |
| 2768 | |
| 2769 | found_it: |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 2770 | lock_release_holdtime(hlock); |
| 2771 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2772 | /* |
| 2773 | * We have the right lock to unlock, 'hlock' points to it. |
| 2774 | * Now we remove it from the stack, and add back the other |
| 2775 | * entries (if any), recalculating the hash along the way: |
| 2776 | */ |
| 2777 | curr->lockdep_depth = i; |
| 2778 | curr->curr_chain_key = hlock->prev_chain_key; |
| 2779 | |
| 2780 | for (i++; i < depth; i++) { |
| 2781 | hlock = curr->held_locks + i; |
| 2782 | if (!__lock_acquire(hlock->instance, |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2783 | hlock_class(hlock)->subclass, hlock->trylock, |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2784 | hlock->read, hlock->check, hlock->hardirqs_off, |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 2785 | hlock->nest_lock, hlock->acquire_ip)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2786 | return 0; |
| 2787 | } |
| 2788 | |
| 2789 | if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1)) |
| 2790 | return 0; |
| 2791 | return 1; |
| 2792 | } |
| 2793 | |
| 2794 | /* |
| 2795 | * Remove the lock to the list of currently held locks - this gets |
| 2796 | * called on mutex_unlock()/spin_unlock*() (or on a failed |
| 2797 | * mutex_lock_interruptible()). This is done for unlocks that nest |
| 2798 | * perfectly. (i.e. the current top of the lock-stack is unlocked) |
| 2799 | */ |
| 2800 | static int lock_release_nested(struct task_struct *curr, |
| 2801 | struct lockdep_map *lock, unsigned long ip) |
| 2802 | { |
| 2803 | struct held_lock *hlock; |
| 2804 | unsigned int depth; |
| 2805 | |
| 2806 | /* |
| 2807 | * Pop off the top of the lock stack: |
| 2808 | */ |
| 2809 | depth = curr->lockdep_depth - 1; |
| 2810 | hlock = curr->held_locks + depth; |
| 2811 | |
| 2812 | /* |
| 2813 | * Is the unlock non-nested: |
| 2814 | */ |
| 2815 | if (hlock->instance != lock) |
| 2816 | return lock_release_non_nested(curr, lock, ip); |
| 2817 | curr->lockdep_depth--; |
| 2818 | |
| 2819 | if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0))) |
| 2820 | return 0; |
| 2821 | |
| 2822 | curr->curr_chain_key = hlock->prev_chain_key; |
| 2823 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 2824 | lock_release_holdtime(hlock); |
| 2825 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2826 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 2827 | hlock->prev_chain_key = 0; |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2828 | hlock->class_idx = 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2829 | hlock->acquire_ip = 0; |
| 2830 | hlock->irq_context = 0; |
| 2831 | #endif |
| 2832 | return 1; |
| 2833 | } |
| 2834 | |
| 2835 | /* |
| 2836 | * Remove the lock to the list of currently held locks - this gets |
| 2837 | * called on mutex_unlock()/spin_unlock*() (or on a failed |
| 2838 | * mutex_lock_interruptible()). This is done for unlocks that nest |
| 2839 | * perfectly. (i.e. the current top of the lock-stack is unlocked) |
| 2840 | */ |
| 2841 | static void |
| 2842 | __lock_release(struct lockdep_map *lock, int nested, unsigned long ip) |
| 2843 | { |
| 2844 | struct task_struct *curr = current; |
| 2845 | |
| 2846 | if (!check_unlock(curr, lock, ip)) |
| 2847 | return; |
| 2848 | |
| 2849 | if (nested) { |
| 2850 | if (!lock_release_nested(curr, lock, ip)) |
| 2851 | return; |
| 2852 | } else { |
| 2853 | if (!lock_release_non_nested(curr, lock, ip)) |
| 2854 | return; |
| 2855 | } |
| 2856 | |
| 2857 | check_chain_key(curr); |
| 2858 | } |
| 2859 | |
| 2860 | /* |
| 2861 | * Check whether we follow the irq-flags state precisely: |
| 2862 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 2863 | static void check_flags(unsigned long flags) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2864 | { |
Ingo Molnar | 992860e | 2008-07-14 10:28:38 +0200 | [diff] [blame] | 2865 | #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \ |
| 2866 | defined(CONFIG_TRACE_IRQFLAGS) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2867 | if (!debug_locks) |
| 2868 | return; |
| 2869 | |
Ingo Molnar | 5f9fa8a | 2007-12-07 19:02:47 +0100 | [diff] [blame] | 2870 | if (irqs_disabled_flags(flags)) { |
| 2871 | if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) { |
| 2872 | printk("possible reason: unannotated irqs-off.\n"); |
| 2873 | } |
| 2874 | } else { |
| 2875 | if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) { |
| 2876 | printk("possible reason: unannotated irqs-on.\n"); |
| 2877 | } |
| 2878 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2879 | |
| 2880 | /* |
| 2881 | * We dont accurately track softirq state in e.g. |
| 2882 | * hardirq contexts (such as on 4KSTACKS), so only |
| 2883 | * check if not in hardirq contexts: |
| 2884 | */ |
| 2885 | if (!hardirq_count()) { |
| 2886 | if (softirq_count()) |
| 2887 | DEBUG_LOCKS_WARN_ON(current->softirqs_enabled); |
| 2888 | else |
| 2889 | DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled); |
| 2890 | } |
| 2891 | |
| 2892 | if (!debug_locks) |
| 2893 | print_irqtrace_events(current); |
| 2894 | #endif |
| 2895 | } |
| 2896 | |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 2897 | void lock_set_class(struct lockdep_map *lock, const char *name, |
| 2898 | struct lock_class_key *key, unsigned int subclass, |
| 2899 | unsigned long ip) |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 2900 | { |
| 2901 | unsigned long flags; |
| 2902 | |
| 2903 | if (unlikely(current->lockdep_recursion)) |
| 2904 | return; |
| 2905 | |
| 2906 | raw_local_irq_save(flags); |
| 2907 | current->lockdep_recursion = 1; |
| 2908 | check_flags(flags); |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 2909 | if (__lock_set_class(lock, name, key, subclass, ip)) |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 2910 | check_chain_key(current); |
| 2911 | current->lockdep_recursion = 0; |
| 2912 | raw_local_irq_restore(flags); |
| 2913 | } |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 2914 | EXPORT_SYMBOL_GPL(lock_set_class); |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 2915 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2916 | /* |
| 2917 | * We are not always called with irqs disabled - do that here, |
| 2918 | * and also avoid lockdep recursion: |
| 2919 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 2920 | void lock_acquire(struct lockdep_map *lock, unsigned int subclass, |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 2921 | int trylock, int read, int check, |
| 2922 | struct lockdep_map *nest_lock, unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2923 | { |
| 2924 | unsigned long flags; |
| 2925 | |
| 2926 | if (unlikely(current->lockdep_recursion)) |
| 2927 | return; |
| 2928 | |
| 2929 | raw_local_irq_save(flags); |
| 2930 | check_flags(flags); |
| 2931 | |
| 2932 | current->lockdep_recursion = 1; |
| 2933 | __lock_acquire(lock, subclass, trylock, read, check, |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 2934 | irqs_disabled_flags(flags), nest_lock, ip); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2935 | current->lockdep_recursion = 0; |
| 2936 | raw_local_irq_restore(flags); |
| 2937 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2938 | EXPORT_SYMBOL_GPL(lock_acquire); |
| 2939 | |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 2940 | void lock_release(struct lockdep_map *lock, int nested, |
Steven Rostedt | 0764d23 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 2941 | unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2942 | { |
| 2943 | unsigned long flags; |
| 2944 | |
| 2945 | if (unlikely(current->lockdep_recursion)) |
| 2946 | return; |
| 2947 | |
| 2948 | raw_local_irq_save(flags); |
| 2949 | check_flags(flags); |
| 2950 | current->lockdep_recursion = 1; |
| 2951 | __lock_release(lock, nested, ip); |
| 2952 | current->lockdep_recursion = 0; |
| 2953 | raw_local_irq_restore(flags); |
| 2954 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2955 | EXPORT_SYMBOL_GPL(lock_release); |
| 2956 | |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 2957 | void lockdep_set_current_reclaim_state(gfp_t gfp_mask) |
| 2958 | { |
| 2959 | current->lockdep_reclaim_gfp = gfp_mask; |
| 2960 | } |
| 2961 | |
| 2962 | void lockdep_clear_current_reclaim_state(void) |
| 2963 | { |
| 2964 | current->lockdep_reclaim_gfp = 0; |
| 2965 | } |
| 2966 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 2967 | #ifdef CONFIG_LOCK_STAT |
| 2968 | static int |
| 2969 | print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock, |
| 2970 | unsigned long ip) |
| 2971 | { |
| 2972 | if (!debug_locks_off()) |
| 2973 | return 0; |
| 2974 | if (debug_locks_silent) |
| 2975 | return 0; |
| 2976 | |
| 2977 | printk("\n=================================\n"); |
| 2978 | printk( "[ BUG: bad contention detected! ]\n"); |
| 2979 | printk( "---------------------------------\n"); |
| 2980 | printk("%s/%d is trying to contend lock (", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 2981 | curr->comm, task_pid_nr(curr)); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 2982 | print_lockdep_cache(lock); |
| 2983 | printk(") at:\n"); |
| 2984 | print_ip_sym(ip); |
| 2985 | printk("but there are no locks held!\n"); |
| 2986 | printk("\nother info that might help us debug this:\n"); |
| 2987 | lockdep_print_held_locks(curr); |
| 2988 | |
| 2989 | printk("\nstack backtrace:\n"); |
| 2990 | dump_stack(); |
| 2991 | |
| 2992 | return 0; |
| 2993 | } |
| 2994 | |
| 2995 | static void |
| 2996 | __lock_contended(struct lockdep_map *lock, unsigned long ip) |
| 2997 | { |
| 2998 | struct task_struct *curr = current; |
| 2999 | struct held_lock *hlock, *prev_hlock; |
| 3000 | struct lock_class_stats *stats; |
| 3001 | unsigned int depth; |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 3002 | int i, contention_point, contending_point; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 3003 | |
| 3004 | depth = curr->lockdep_depth; |
| 3005 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
| 3006 | return; |
| 3007 | |
| 3008 | prev_hlock = NULL; |
| 3009 | for (i = depth-1; i >= 0; i--) { |
| 3010 | hlock = curr->held_locks + i; |
| 3011 | /* |
| 3012 | * We must not cross into another context: |
| 3013 | */ |
| 3014 | if (prev_hlock && prev_hlock->irq_context != hlock->irq_context) |
| 3015 | break; |
| 3016 | if (hlock->instance == lock) |
| 3017 | goto found_it; |
| 3018 | prev_hlock = hlock; |
| 3019 | } |
| 3020 | print_lock_contention_bug(curr, lock, ip); |
| 3021 | return; |
| 3022 | |
| 3023 | found_it: |
| 3024 | hlock->waittime_stamp = sched_clock(); |
| 3025 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 3026 | contention_point = lock_point(hlock_class(hlock)->contention_point, ip); |
| 3027 | contending_point = lock_point(hlock_class(hlock)->contending_point, |
| 3028 | lock->ip); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 3029 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 3030 | stats = get_lock_stats(hlock_class(hlock)); |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 3031 | if (contention_point < LOCKSTAT_POINTS) |
| 3032 | stats->contention_point[contention_point]++; |
| 3033 | if (contending_point < LOCKSTAT_POINTS) |
| 3034 | stats->contending_point[contending_point]++; |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 3035 | if (lock->cpu != smp_processor_id()) |
| 3036 | stats->bounces[bounce_contended + !!hlock->read]++; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 3037 | put_lock_stats(stats); |
| 3038 | } |
| 3039 | |
| 3040 | static void |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 3041 | __lock_acquired(struct lockdep_map *lock, unsigned long ip) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 3042 | { |
| 3043 | struct task_struct *curr = current; |
| 3044 | struct held_lock *hlock, *prev_hlock; |
| 3045 | struct lock_class_stats *stats; |
| 3046 | unsigned int depth; |
| 3047 | u64 now; |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 3048 | s64 waittime = 0; |
| 3049 | int i, cpu; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 3050 | |
| 3051 | depth = curr->lockdep_depth; |
| 3052 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
| 3053 | return; |
| 3054 | |
| 3055 | prev_hlock = NULL; |
| 3056 | for (i = depth-1; i >= 0; i--) { |
| 3057 | hlock = curr->held_locks + i; |
| 3058 | /* |
| 3059 | * We must not cross into another context: |
| 3060 | */ |
| 3061 | if (prev_hlock && prev_hlock->irq_context != hlock->irq_context) |
| 3062 | break; |
| 3063 | if (hlock->instance == lock) |
| 3064 | goto found_it; |
| 3065 | prev_hlock = hlock; |
| 3066 | } |
| 3067 | print_lock_contention_bug(curr, lock, _RET_IP_); |
| 3068 | return; |
| 3069 | |
| 3070 | found_it: |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 3071 | cpu = smp_processor_id(); |
| 3072 | if (hlock->waittime_stamp) { |
| 3073 | now = sched_clock(); |
| 3074 | waittime = now - hlock->waittime_stamp; |
| 3075 | hlock->holdtime_stamp = now; |
| 3076 | } |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 3077 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 3078 | stats = get_lock_stats(hlock_class(hlock)); |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 3079 | if (waittime) { |
| 3080 | if (hlock->read) |
| 3081 | lock_time_inc(&stats->read_waittime, waittime); |
| 3082 | else |
| 3083 | lock_time_inc(&stats->write_waittime, waittime); |
| 3084 | } |
| 3085 | if (lock->cpu != cpu) |
| 3086 | stats->bounces[bounce_acquired + !!hlock->read]++; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 3087 | put_lock_stats(stats); |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 3088 | |
| 3089 | lock->cpu = cpu; |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 3090 | lock->ip = ip; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | void lock_contended(struct lockdep_map *lock, unsigned long ip) |
| 3094 | { |
| 3095 | unsigned long flags; |
| 3096 | |
| 3097 | if (unlikely(!lock_stat)) |
| 3098 | return; |
| 3099 | |
| 3100 | if (unlikely(current->lockdep_recursion)) |
| 3101 | return; |
| 3102 | |
| 3103 | raw_local_irq_save(flags); |
| 3104 | check_flags(flags); |
| 3105 | current->lockdep_recursion = 1; |
| 3106 | __lock_contended(lock, ip); |
| 3107 | current->lockdep_recursion = 0; |
| 3108 | raw_local_irq_restore(flags); |
| 3109 | } |
| 3110 | EXPORT_SYMBOL_GPL(lock_contended); |
| 3111 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 3112 | void lock_acquired(struct lockdep_map *lock, unsigned long ip) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 3113 | { |
| 3114 | unsigned long flags; |
| 3115 | |
| 3116 | if (unlikely(!lock_stat)) |
| 3117 | return; |
| 3118 | |
| 3119 | if (unlikely(current->lockdep_recursion)) |
| 3120 | return; |
| 3121 | |
| 3122 | raw_local_irq_save(flags); |
| 3123 | check_flags(flags); |
| 3124 | current->lockdep_recursion = 1; |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 3125 | __lock_acquired(lock, ip); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 3126 | current->lockdep_recursion = 0; |
| 3127 | raw_local_irq_restore(flags); |
| 3128 | } |
| 3129 | EXPORT_SYMBOL_GPL(lock_acquired); |
| 3130 | #endif |
| 3131 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3132 | /* |
| 3133 | * Used by the testsuite, sanitize the validator state |
| 3134 | * after a simulated failure: |
| 3135 | */ |
| 3136 | |
| 3137 | void lockdep_reset(void) |
| 3138 | { |
| 3139 | unsigned long flags; |
Ingo Molnar | 23d95a0 | 2006-12-13 00:34:40 -0800 | [diff] [blame] | 3140 | int i; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3141 | |
| 3142 | raw_local_irq_save(flags); |
| 3143 | current->curr_chain_key = 0; |
| 3144 | current->lockdep_depth = 0; |
| 3145 | current->lockdep_recursion = 0; |
| 3146 | memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock)); |
| 3147 | nr_hardirq_chains = 0; |
| 3148 | nr_softirq_chains = 0; |
| 3149 | nr_process_chains = 0; |
| 3150 | debug_locks = 1; |
Ingo Molnar | 23d95a0 | 2006-12-13 00:34:40 -0800 | [diff] [blame] | 3151 | for (i = 0; i < CHAINHASH_SIZE; i++) |
| 3152 | INIT_LIST_HEAD(chainhash_table + i); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3153 | raw_local_irq_restore(flags); |
| 3154 | } |
| 3155 | |
| 3156 | static void zap_class(struct lock_class *class) |
| 3157 | { |
| 3158 | int i; |
| 3159 | |
| 3160 | /* |
| 3161 | * Remove all dependencies this lock is |
| 3162 | * involved in: |
| 3163 | */ |
| 3164 | for (i = 0; i < nr_list_entries; i++) { |
| 3165 | if (list_entries[i].class == class) |
| 3166 | list_del_rcu(&list_entries[i].entry); |
| 3167 | } |
| 3168 | /* |
| 3169 | * Unhash the class and remove it from the all_lock_classes list: |
| 3170 | */ |
| 3171 | list_del_rcu(&class->hash_entry); |
| 3172 | list_del_rcu(&class->lock_entry); |
| 3173 | |
Rabin Vincent | 8bfe029 | 2008-08-11 09:30:26 +0200 | [diff] [blame] | 3174 | class->key = NULL; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3175 | } |
| 3176 | |
Arjan van de Ven | fabe874 | 2008-01-24 07:00:45 +0100 | [diff] [blame] | 3177 | static inline int within(const void *addr, void *start, unsigned long size) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3178 | { |
| 3179 | return addr >= start && addr < start + size; |
| 3180 | } |
| 3181 | |
| 3182 | void lockdep_free_key_range(void *start, unsigned long size) |
| 3183 | { |
| 3184 | struct lock_class *class, *next; |
| 3185 | struct list_head *head; |
| 3186 | unsigned long flags; |
| 3187 | int i; |
Nick Piggin | 5a26db5 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 3188 | int locked; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3189 | |
| 3190 | raw_local_irq_save(flags); |
Nick Piggin | 5a26db5 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 3191 | locked = graph_lock(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3192 | |
| 3193 | /* |
| 3194 | * Unhash all classes that were created by this module: |
| 3195 | */ |
| 3196 | for (i = 0; i < CLASSHASH_SIZE; i++) { |
| 3197 | head = classhash_table + i; |
| 3198 | if (list_empty(head)) |
| 3199 | continue; |
Arjan van de Ven | fabe874 | 2008-01-24 07:00:45 +0100 | [diff] [blame] | 3200 | list_for_each_entry_safe(class, next, head, hash_entry) { |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3201 | if (within(class->key, start, size)) |
| 3202 | zap_class(class); |
Arjan van de Ven | fabe874 | 2008-01-24 07:00:45 +0100 | [diff] [blame] | 3203 | else if (within(class->name, start, size)) |
| 3204 | zap_class(class); |
| 3205 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3206 | } |
| 3207 | |
Nick Piggin | 5a26db5 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 3208 | if (locked) |
| 3209 | graph_unlock(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3210 | raw_local_irq_restore(flags); |
| 3211 | } |
| 3212 | |
| 3213 | void lockdep_reset_lock(struct lockdep_map *lock) |
| 3214 | { |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 3215 | struct lock_class *class, *next; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3216 | struct list_head *head; |
| 3217 | unsigned long flags; |
| 3218 | int i, j; |
Nick Piggin | 5a26db5 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 3219 | int locked; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3220 | |
| 3221 | raw_local_irq_save(flags); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3222 | |
| 3223 | /* |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 3224 | * Remove all classes this lock might have: |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3225 | */ |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 3226 | for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) { |
| 3227 | /* |
| 3228 | * If the class exists we look it up and zap it: |
| 3229 | */ |
| 3230 | class = look_up_lock_class(lock, j); |
| 3231 | if (class) |
| 3232 | zap_class(class); |
| 3233 | } |
| 3234 | /* |
| 3235 | * Debug check: in the end all mapped classes should |
| 3236 | * be gone. |
| 3237 | */ |
Nick Piggin | 5a26db5 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 3238 | locked = graph_lock(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3239 | for (i = 0; i < CLASSHASH_SIZE; i++) { |
| 3240 | head = classhash_table + i; |
| 3241 | if (list_empty(head)) |
| 3242 | continue; |
| 3243 | list_for_each_entry_safe(class, next, head, hash_entry) { |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 3244 | if (unlikely(class == lock->class_cache)) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 3245 | if (debug_locks_off_graph_unlock()) |
| 3246 | WARN_ON(1); |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 3247 | goto out_restore; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3248 | } |
| 3249 | } |
| 3250 | } |
Nick Piggin | 5a26db5 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 3251 | if (locked) |
| 3252 | graph_unlock(); |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 3253 | |
| 3254 | out_restore: |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3255 | raw_local_irq_restore(flags); |
| 3256 | } |
| 3257 | |
Sam Ravnborg | 1499993 | 2007-02-28 20:12:31 -0800 | [diff] [blame] | 3258 | void lockdep_init(void) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3259 | { |
| 3260 | int i; |
| 3261 | |
| 3262 | /* |
| 3263 | * Some architectures have their own start_kernel() |
| 3264 | * code which calls lockdep_init(), while we also |
| 3265 | * call lockdep_init() from the start_kernel() itself, |
| 3266 | * and we want to initialize the hashes only once: |
| 3267 | */ |
| 3268 | if (lockdep_initialized) |
| 3269 | return; |
| 3270 | |
| 3271 | for (i = 0; i < CLASSHASH_SIZE; i++) |
| 3272 | INIT_LIST_HEAD(classhash_table + i); |
| 3273 | |
| 3274 | for (i = 0; i < CHAINHASH_SIZE; i++) |
| 3275 | INIT_LIST_HEAD(chainhash_table + i); |
| 3276 | |
| 3277 | lockdep_initialized = 1; |
| 3278 | } |
| 3279 | |
| 3280 | void __init lockdep_info(void) |
| 3281 | { |
| 3282 | printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n"); |
| 3283 | |
Li Zefan | b0788ca | 2008-11-21 15:57:32 +0800 | [diff] [blame] | 3284 | printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3285 | printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH); |
| 3286 | printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS); |
Li Zefan | b0788ca | 2008-11-21 15:57:32 +0800 | [diff] [blame] | 3287 | printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3288 | printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES); |
| 3289 | printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS); |
| 3290 | printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE); |
| 3291 | |
| 3292 | printk(" memory used by lock dependency info: %lu kB\n", |
| 3293 | (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS + |
| 3294 | sizeof(struct list_head) * CLASSHASH_SIZE + |
| 3295 | sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES + |
| 3296 | sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS + |
| 3297 | sizeof(struct list_head) * CHAINHASH_SIZE) / 1024); |
| 3298 | |
| 3299 | printk(" per task-struct memory footprint: %lu bytes\n", |
| 3300 | sizeof(struct held_lock) * MAX_LOCK_DEPTH); |
| 3301 | |
| 3302 | #ifdef CONFIG_DEBUG_LOCKDEP |
Johannes Berg | c71063c | 2007-07-19 01:49:02 -0700 | [diff] [blame] | 3303 | if (lockdep_init_error) { |
| 3304 | printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n"); |
| 3305 | printk("Call stack leading to lockdep invocation was:\n"); |
| 3306 | print_stack_trace(&lockdep_init_trace, 0); |
| 3307 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3308 | #endif |
| 3309 | } |
| 3310 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3311 | static void |
| 3312 | print_freed_lock_bug(struct task_struct *curr, const void *mem_from, |
Arjan van de Ven | 55794a4 | 2006-07-10 04:44:03 -0700 | [diff] [blame] | 3313 | const void *mem_to, struct held_lock *hlock) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3314 | { |
| 3315 | if (!debug_locks_off()) |
| 3316 | return; |
| 3317 | if (debug_locks_silent) |
| 3318 | return; |
| 3319 | |
| 3320 | printk("\n=========================\n"); |
| 3321 | printk( "[ BUG: held lock freed! ]\n"); |
| 3322 | printk( "-------------------------\n"); |
| 3323 | printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 3324 | curr->comm, task_pid_nr(curr), mem_from, mem_to-1); |
Arjan van de Ven | 55794a4 | 2006-07-10 04:44:03 -0700 | [diff] [blame] | 3325 | print_lock(hlock); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3326 | lockdep_print_held_locks(curr); |
| 3327 | |
| 3328 | printk("\nstack backtrace:\n"); |
| 3329 | dump_stack(); |
| 3330 | } |
| 3331 | |
Oleg Nesterov | 5456178 | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3332 | static inline int not_in_range(const void* mem_from, unsigned long mem_len, |
| 3333 | const void* lock_from, unsigned long lock_len) |
| 3334 | { |
| 3335 | return lock_from + lock_len <= mem_from || |
| 3336 | mem_from + mem_len <= lock_from; |
| 3337 | } |
| 3338 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3339 | /* |
| 3340 | * Called when kernel memory is freed (or unmapped), or if a lock |
| 3341 | * is destroyed or reinitialized - this code checks whether there is |
| 3342 | * any held lock in the memory range of <from> to <to>: |
| 3343 | */ |
| 3344 | void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len) |
| 3345 | { |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3346 | struct task_struct *curr = current; |
| 3347 | struct held_lock *hlock; |
| 3348 | unsigned long flags; |
| 3349 | int i; |
| 3350 | |
| 3351 | if (unlikely(!debug_locks)) |
| 3352 | return; |
| 3353 | |
| 3354 | local_irq_save(flags); |
| 3355 | for (i = 0; i < curr->lockdep_depth; i++) { |
| 3356 | hlock = curr->held_locks + i; |
| 3357 | |
Oleg Nesterov | 5456178 | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3358 | if (not_in_range(mem_from, mem_len, hlock->instance, |
| 3359 | sizeof(*hlock->instance))) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3360 | continue; |
| 3361 | |
Oleg Nesterov | 5456178 | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3362 | print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3363 | break; |
| 3364 | } |
| 3365 | local_irq_restore(flags); |
| 3366 | } |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 3367 | EXPORT_SYMBOL_GPL(debug_check_no_locks_freed); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3368 | |
| 3369 | static void print_held_locks_bug(struct task_struct *curr) |
| 3370 | { |
| 3371 | if (!debug_locks_off()) |
| 3372 | return; |
| 3373 | if (debug_locks_silent) |
| 3374 | return; |
| 3375 | |
| 3376 | printk("\n=====================================\n"); |
| 3377 | printk( "[ BUG: lock held at task exit time! ]\n"); |
| 3378 | printk( "-------------------------------------\n"); |
| 3379 | printk("%s/%d is exiting with locks still held!\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 3380 | curr->comm, task_pid_nr(curr)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3381 | lockdep_print_held_locks(curr); |
| 3382 | |
| 3383 | printk("\nstack backtrace:\n"); |
| 3384 | dump_stack(); |
| 3385 | } |
| 3386 | |
| 3387 | void debug_check_no_locks_held(struct task_struct *task) |
| 3388 | { |
| 3389 | if (unlikely(task->lockdep_depth > 0)) |
| 3390 | print_held_locks_bug(task); |
| 3391 | } |
| 3392 | |
| 3393 | void debug_show_all_locks(void) |
| 3394 | { |
| 3395 | struct task_struct *g, *p; |
| 3396 | int count = 10; |
| 3397 | int unlock = 1; |
| 3398 | |
Jarek Poplawski | 9c35dd7 | 2007-03-22 00:11:28 -0800 | [diff] [blame] | 3399 | if (unlikely(!debug_locks)) { |
| 3400 | printk("INFO: lockdep is turned off.\n"); |
| 3401 | return; |
| 3402 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3403 | printk("\nShowing all locks held in the system:\n"); |
| 3404 | |
| 3405 | /* |
| 3406 | * Here we try to get the tasklist_lock as hard as possible, |
| 3407 | * if not successful after 2 seconds we ignore it (but keep |
| 3408 | * trying). This is to enable a debug printout even if a |
| 3409 | * tasklist_lock-holding task deadlocks or crashes. |
| 3410 | */ |
| 3411 | retry: |
| 3412 | if (!read_trylock(&tasklist_lock)) { |
| 3413 | if (count == 10) |
| 3414 | printk("hm, tasklist_lock locked, retrying... "); |
| 3415 | if (count) { |
| 3416 | count--; |
| 3417 | printk(" #%d", 10-count); |
| 3418 | mdelay(200); |
| 3419 | goto retry; |
| 3420 | } |
| 3421 | printk(" ignoring it.\n"); |
| 3422 | unlock = 0; |
qinghuang feng | 46fec7a | 2008-10-28 17:24:28 +0800 | [diff] [blame] | 3423 | } else { |
| 3424 | if (count != 10) |
| 3425 | printk(KERN_CONT " locked it.\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3426 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3427 | |
| 3428 | do_each_thread(g, p) { |
Ingo Molnar | 8568487 | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 3429 | /* |
| 3430 | * It's not reliable to print a task's held locks |
| 3431 | * if it's not sleeping (or if it's not the current |
| 3432 | * task): |
| 3433 | */ |
| 3434 | if (p->state == TASK_RUNNING && p != current) |
| 3435 | continue; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3436 | if (p->lockdep_depth) |
| 3437 | lockdep_print_held_locks(p); |
| 3438 | if (!unlock) |
| 3439 | if (read_trylock(&tasklist_lock)) |
| 3440 | unlock = 1; |
| 3441 | } while_each_thread(g, p); |
| 3442 | |
| 3443 | printk("\n"); |
| 3444 | printk("=============================================\n\n"); |
| 3445 | |
| 3446 | if (unlock) |
| 3447 | read_unlock(&tasklist_lock); |
| 3448 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3449 | EXPORT_SYMBOL_GPL(debug_show_all_locks); |
| 3450 | |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 3451 | /* |
| 3452 | * Careful: only use this function if you are sure that |
| 3453 | * the task cannot run in parallel! |
| 3454 | */ |
| 3455 | void __debug_show_held_locks(struct task_struct *task) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3456 | { |
Jarek Poplawski | 9c35dd7 | 2007-03-22 00:11:28 -0800 | [diff] [blame] | 3457 | if (unlikely(!debug_locks)) { |
| 3458 | printk("INFO: lockdep is turned off.\n"); |
| 3459 | return; |
| 3460 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3461 | lockdep_print_held_locks(task); |
| 3462 | } |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 3463 | EXPORT_SYMBOL_GPL(__debug_show_held_locks); |
| 3464 | |
| 3465 | void debug_show_held_locks(struct task_struct *task) |
| 3466 | { |
| 3467 | __debug_show_held_locks(task); |
| 3468 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3469 | EXPORT_SYMBOL_GPL(debug_show_held_locks); |
Peter Zijlstra | b351d16 | 2007-10-11 22:11:12 +0200 | [diff] [blame] | 3470 | |
| 3471 | void lockdep_sys_exit(void) |
| 3472 | { |
| 3473 | struct task_struct *curr = current; |
| 3474 | |
| 3475 | if (unlikely(curr->lockdep_depth)) { |
| 3476 | if (!debug_locks_off()) |
| 3477 | return; |
| 3478 | printk("\n================================================\n"); |
| 3479 | printk( "[ BUG: lock held when returning to user space! ]\n"); |
| 3480 | printk( "------------------------------------------------\n"); |
| 3481 | printk("%s/%d is leaving the kernel with locks still held!\n", |
| 3482 | curr->comm, curr->pid); |
| 3483 | lockdep_print_held_locks(curr); |
| 3484 | } |
| 3485 | } |