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