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