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