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