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