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