Commit | Line | Data |
---|---|---|
fbb9ce95 IM |
1 | /* |
2 | * kernel/lockdep.c | |
3 | * | |
4 | * Runtime locking correctness validator | |
5 | * | |
6 | * Started by Ingo Molnar: | |
7 | * | |
4b32d0a4 | 8 | * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
90eec103 | 9 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra |
fbb9ce95 IM |
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 | */ | |
a5e25883 | 28 | #define DISABLE_BRANCH_PROFILING |
fbb9ce95 IM |
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> | |
99de055a | 41 | #include <linux/utsname.h> |
4b32d0a4 | 42 | #include <linux/hash.h> |
81d68a96 | 43 | #include <linux/ftrace.h> |
b4b136f4 | 44 | #include <linux/stringify.h> |
d588e461 | 45 | #include <linux/bitops.h> |
5a0e3ad6 | 46 | #include <linux/gfp.h> |
d3d03d4f | 47 | #include <linux/kmemcheck.h> |
e7904a28 | 48 | #include <linux/random.h> |
af012961 | 49 | |
fbb9ce95 IM |
50 | #include <asm/sections.h> |
51 | ||
52 | #include "lockdep_internals.h" | |
53 | ||
a8d154b0 | 54 | #define CREATE_TRACE_POINTS |
67178767 | 55 | #include <trace/events/lock.h> |
a8d154b0 | 56 | |
f20786ff PZ |
57 | #ifdef CONFIG_PROVE_LOCKING |
58 | int prove_locking = 1; | |
59 | module_param(prove_locking, int, 0644); | |
60 | #else | |
61 | #define prove_locking 0 | |
62 | #endif | |
63 | ||
64 | #ifdef CONFIG_LOCK_STAT | |
65 | int lock_stat = 1; | |
66 | module_param(lock_stat, int, 0644); | |
67 | #else | |
68 | #define lock_stat 0 | |
69 | #endif | |
70 | ||
fbb9ce95 | 71 | /* |
74c383f1 IM |
72 | * lockdep_lock: protects the lockdep graph, the hashes and the |
73 | * class/list/hash allocators. | |
fbb9ce95 IM |
74 | * |
75 | * This is one of the rare exceptions where it's justified | |
76 | * to use a raw spinlock - we really dont want the spinlock | |
74c383f1 | 77 | * code to recurse back into the lockdep code... |
fbb9ce95 | 78 | */ |
edc35bd7 | 79 | static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
74c383f1 IM |
80 | |
81 | static int graph_lock(void) | |
82 | { | |
0199c4e6 | 83 | arch_spin_lock(&lockdep_lock); |
74c383f1 IM |
84 | /* |
85 | * Make sure that if another CPU detected a bug while | |
86 | * walking the graph we dont change it (while the other | |
87 | * CPU is busy printing out stuff with the graph lock | |
88 | * dropped already) | |
89 | */ | |
90 | if (!debug_locks) { | |
0199c4e6 | 91 | arch_spin_unlock(&lockdep_lock); |
74c383f1 IM |
92 | return 0; |
93 | } | |
bb065afb SR |
94 | /* prevent any recursions within lockdep from causing deadlocks */ |
95 | current->lockdep_recursion++; | |
74c383f1 IM |
96 | return 1; |
97 | } | |
98 | ||
99 | static inline int graph_unlock(void) | |
100 | { | |
0119fee4 PZ |
101 | if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) { |
102 | /* | |
103 | * The lockdep graph lock isn't locked while we expect it to | |
104 | * be, we're confused now, bye! | |
105 | */ | |
381a2292 | 106 | return DEBUG_LOCKS_WARN_ON(1); |
0119fee4 | 107 | } |
381a2292 | 108 | |
bb065afb | 109 | current->lockdep_recursion--; |
0199c4e6 | 110 | arch_spin_unlock(&lockdep_lock); |
74c383f1 IM |
111 | return 0; |
112 | } | |
113 | ||
114 | /* | |
115 | * Turn lock debugging off and return with 0 if it was off already, | |
116 | * and also release the graph lock: | |
117 | */ | |
118 | static inline int debug_locks_off_graph_unlock(void) | |
119 | { | |
120 | int ret = debug_locks_off(); | |
121 | ||
0199c4e6 | 122 | arch_spin_unlock(&lockdep_lock); |
74c383f1 IM |
123 | |
124 | return ret; | |
125 | } | |
fbb9ce95 | 126 | |
fbb9ce95 | 127 | unsigned long nr_list_entries; |
af012961 | 128 | static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES]; |
fbb9ce95 | 129 | |
fbb9ce95 IM |
130 | /* |
131 | * All data structures here are protected by the global debug_lock. | |
132 | * | |
133 | * Mutex key structs only get allocated, once during bootup, and never | |
134 | * get freed - this significantly simplifies the debugging code. | |
135 | */ | |
136 | unsigned long nr_lock_classes; | |
137 | static struct lock_class lock_classes[MAX_LOCKDEP_KEYS]; | |
138 | ||
f82b217e DJ |
139 | static inline struct lock_class *hlock_class(struct held_lock *hlock) |
140 | { | |
141 | if (!hlock->class_idx) { | |
0119fee4 PZ |
142 | /* |
143 | * Someone passed in garbage, we give up. | |
144 | */ | |
f82b217e DJ |
145 | DEBUG_LOCKS_WARN_ON(1); |
146 | return NULL; | |
147 | } | |
148 | return lock_classes + hlock->class_idx - 1; | |
149 | } | |
150 | ||
f20786ff | 151 | #ifdef CONFIG_LOCK_STAT |
25528213 | 152 | static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats); |
f20786ff | 153 | |
3365e779 PZ |
154 | static inline u64 lockstat_clock(void) |
155 | { | |
c676329a | 156 | return local_clock(); |
3365e779 PZ |
157 | } |
158 | ||
c7e78cff | 159 | static int lock_point(unsigned long points[], unsigned long ip) |
f20786ff PZ |
160 | { |
161 | int i; | |
162 | ||
c7e78cff PZ |
163 | for (i = 0; i < LOCKSTAT_POINTS; i++) { |
164 | if (points[i] == 0) { | |
165 | points[i] = ip; | |
f20786ff PZ |
166 | break; |
167 | } | |
c7e78cff | 168 | if (points[i] == ip) |
f20786ff PZ |
169 | break; |
170 | } | |
171 | ||
172 | return i; | |
173 | } | |
174 | ||
3365e779 | 175 | static void lock_time_inc(struct lock_time *lt, u64 time) |
f20786ff PZ |
176 | { |
177 | if (time > lt->max) | |
178 | lt->max = time; | |
179 | ||
109d71c6 | 180 | if (time < lt->min || !lt->nr) |
f20786ff PZ |
181 | lt->min = time; |
182 | ||
183 | lt->total += time; | |
184 | lt->nr++; | |
185 | } | |
186 | ||
c46261de PZ |
187 | static inline void lock_time_add(struct lock_time *src, struct lock_time *dst) |
188 | { | |
109d71c6 FR |
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 | ||
c46261de PZ |
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 = | |
1871e52c | 210 | &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; |
c46261de PZ |
211 | |
212 | for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++) | |
213 | stats.contention_point[i] += pcs->contention_point[i]; | |
214 | ||
c7e78cff PZ |
215 | for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++) |
216 | stats.contending_point[i] += pcs->contending_point[i]; | |
217 | ||
c46261de PZ |
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); | |
96645678 PZ |
223 | |
224 | for (i = 0; i < ARRAY_SIZE(stats.bounces); i++) | |
225 | stats.bounces[i] += pcs->bounces[i]; | |
c46261de PZ |
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 = | |
1871e52c | 237 | &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; |
c46261de PZ |
238 | |
239 | memset(cpu_stats, 0, sizeof(struct lock_class_stats)); | |
240 | } | |
241 | memset(class->contention_point, 0, sizeof(class->contention_point)); | |
c7e78cff | 242 | memset(class->contending_point, 0, sizeof(class->contending_point)); |
c46261de PZ |
243 | } |
244 | ||
f20786ff PZ |
245 | static struct lock_class_stats *get_lock_stats(struct lock_class *class) |
246 | { | |
1871e52c | 247 | return &get_cpu_var(cpu_lock_stats)[class - lock_classes]; |
f20786ff PZ |
248 | } |
249 | ||
250 | static void put_lock_stats(struct lock_class_stats *stats) | |
251 | { | |
1871e52c | 252 | put_cpu_var(cpu_lock_stats); |
f20786ff PZ |
253 | } |
254 | ||
255 | static void lock_release_holdtime(struct held_lock *hlock) | |
256 | { | |
257 | struct lock_class_stats *stats; | |
3365e779 | 258 | u64 holdtime; |
f20786ff PZ |
259 | |
260 | if (!lock_stat) | |
261 | return; | |
262 | ||
3365e779 | 263 | holdtime = lockstat_clock() - hlock->holdtime_stamp; |
f20786ff | 264 | |
f82b217e | 265 | stats = get_lock_stats(hlock_class(hlock)); |
f20786ff PZ |
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 | ||
fbb9ce95 IM |
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) | |
4b32d0a4 | 290 | #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS) |
fbb9ce95 IM |
291 | #define classhashentry(key) (classhash_table + __classhashfn((key))) |
292 | ||
a63f38cc | 293 | static struct hlist_head classhash_table[CLASSHASH_SIZE]; |
fbb9ce95 | 294 | |
fbb9ce95 IM |
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) | |
4b32d0a4 | 301 | #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS) |
fbb9ce95 IM |
302 | #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain))) |
303 | ||
a63f38cc | 304 | static struct hlist_head chainhash_table[CHAINHASH_SIZE]; |
fbb9ce95 IM |
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) \ | |
03cbc358 IM |
313 | (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \ |
314 | ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \ | |
fbb9ce95 IM |
315 | (key2)) |
316 | ||
1d09daa5 | 317 | void lockdep_off(void) |
fbb9ce95 IM |
318 | { |
319 | current->lockdep_recursion++; | |
320 | } | |
fbb9ce95 IM |
321 | EXPORT_SYMBOL(lockdep_off); |
322 | ||
1d09daa5 | 323 | void lockdep_on(void) |
fbb9ce95 IM |
324 | { |
325 | current->lockdep_recursion--; | |
326 | } | |
fbb9ce95 IM |
327 | EXPORT_SYMBOL(lockdep_on); |
328 | ||
fbb9ce95 IM |
329 | /* |
330 | * Debugging switches: | |
331 | */ | |
332 | ||
333 | #define VERBOSE 0 | |
33e94e96 | 334 | #define VERY_VERBOSE 0 |
fbb9ce95 IM |
335 | |
336 | #if VERBOSE | |
337 | # define HARDIRQ_VERBOSE 1 | |
338 | # define SOFTIRQ_VERBOSE 1 | |
cf40bd16 | 339 | # define RECLAIM_VERBOSE 1 |
fbb9ce95 IM |
340 | #else |
341 | # define HARDIRQ_VERBOSE 0 | |
342 | # define SOFTIRQ_VERBOSE 0 | |
cf40bd16 | 343 | # define RECLAIM_VERBOSE 0 |
fbb9ce95 IM |
344 | #endif |
345 | ||
cf40bd16 | 346 | #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE |
fbb9ce95 IM |
347 | /* |
348 | * Quick filtering for interesting events: | |
349 | */ | |
350 | static int class_filter(struct lock_class *class) | |
351 | { | |
f9829cce AK |
352 | #if 0 |
353 | /* Example */ | |
fbb9ce95 | 354 | if (class->name_version == 1 && |
f9829cce | 355 | !strcmp(class->name, "lockname")) |
fbb9ce95 IM |
356 | return 1; |
357 | if (class->name_version == 1 && | |
f9829cce | 358 | !strcmp(class->name, "&struct->lockfield")) |
fbb9ce95 | 359 | return 1; |
f9829cce | 360 | #endif |
a6640897 IM |
361 | /* Filter everything else. 1 would be to allow everything else */ |
362 | return 0; | |
fbb9ce95 IM |
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 | ||
fbb9ce95 IM |
374 | /* |
375 | * Stack-trace: tightly packed array of stack backtrace | |
74c383f1 | 376 | * addresses. Protected by the graph_lock. |
fbb9ce95 IM |
377 | */ |
378 | unsigned long nr_stack_trace_entries; | |
379 | static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES]; | |
380 | ||
2c522836 DJ |
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"); | |
acf59377 | 385 | #ifdef CONFIG_LOCK_STAT |
2c522836 | 386 | printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n"); |
acf59377 | 387 | #endif |
2c522836 DJ |
388 | } |
389 | ||
fbb9ce95 IM |
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 | ||
5a1b3999 | 396 | trace->skip = 3; |
5a1b3999 | 397 | |
ab1b6f03 | 398 | save_stack_trace(trace); |
fbb9ce95 | 399 | |
4f84f433 PZ |
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 | */ | |
ea5b41f9 LT |
407 | if (trace->nr_entries != 0 && |
408 | trace->entries[trace->nr_entries-1] == ULONG_MAX) | |
4f84f433 PZ |
409 | trace->nr_entries--; |
410 | ||
fbb9ce95 IM |
411 | trace->max_entries = trace->nr_entries; |
412 | ||
413 | nr_stack_trace_entries += trace->nr_entries; | |
fbb9ce95 | 414 | |
4f84f433 | 415 | if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) { |
74c383f1 IM |
416 | if (!debug_locks_off_graph_unlock()) |
417 | return 0; | |
418 | ||
2c522836 | 419 | print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!"); |
74c383f1 IM |
420 | dump_stack(); |
421 | ||
fbb9ce95 IM |
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; | |
fbb9ce95 IM |
432 | |
433 | #ifdef CONFIG_DEBUG_LOCKDEP | |
fbb9ce95 IM |
434 | /* |
435 | * Various lockdep statistics: | |
436 | */ | |
bd6d29c2 | 437 | DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats); |
fbb9ce95 IM |
438 | #endif |
439 | ||
440 | /* | |
441 | * Locking printouts: | |
442 | */ | |
443 | ||
fabe9c42 | 444 | #define __USAGE(__STATE) \ |
b4b136f4 PZ |
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", | |
fabe9c42 | 449 | |
fbb9ce95 IM |
450 | static const char *usage_str[] = |
451 | { | |
fabe9c42 PZ |
452 | #define LOCKDEP_STATE(__STATE) __USAGE(__STATE) |
453 | #include "lockdep_states.h" | |
454 | #undef LOCKDEP_STATE | |
455 | [LOCK_USED] = "INITIAL USE", | |
fbb9ce95 IM |
456 | }; |
457 | ||
458 | const char * __get_key_name(struct lockdep_subclass_key *key, char *str) | |
459 | { | |
ffb45122 | 460 | return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str); |
fbb9ce95 IM |
461 | } |
462 | ||
3ff176ca | 463 | static inline unsigned long lock_flag(enum lock_usage_bit bit) |
fbb9ce95 | 464 | { |
3ff176ca PZ |
465 | return 1UL << bit; |
466 | } | |
fbb9ce95 | 467 | |
3ff176ca PZ |
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 = '?'; | |
fbb9ce95 IM |
478 | } |
479 | ||
3ff176ca PZ |
480 | return c; |
481 | } | |
cf40bd16 | 482 | |
f510b233 | 483 | void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS]) |
3ff176ca | 484 | { |
f510b233 | 485 | int i = 0; |
cf40bd16 | 486 | |
f510b233 PZ |
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'; | |
fbb9ce95 IM |
494 | } |
495 | ||
e5e78d08 | 496 | static void __print_lock_name(struct lock_class *class) |
3003eba3 SR |
497 | { |
498 | char str[KSYM_NAME_LEN]; | |
499 | const char *name; | |
500 | ||
fbb9ce95 IM |
501 | name = class->name; |
502 | if (!name) { | |
503 | name = __get_key_name(class->key, str); | |
e5e78d08 | 504 | printk("%s", name); |
fbb9ce95 | 505 | } else { |
e5e78d08 | 506 | printk("%s", name); |
fbb9ce95 IM |
507 | if (class->name_version > 1) |
508 | printk("#%d", class->name_version); | |
509 | if (class->subclass) | |
510 | printk("/%d", class->subclass); | |
511 | } | |
e5e78d08 SR |
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); | |
f510b233 | 522 | printk("){%s}", usage); |
fbb9ce95 IM |
523 | } |
524 | ||
525 | static void print_lockdep_cache(struct lockdep_map *lock) | |
526 | { | |
527 | const char *name; | |
9281acea | 528 | char str[KSYM_NAME_LEN]; |
fbb9ce95 IM |
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 | { | |
d7bc3197 PZ |
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); | |
fbb9ce95 IM |
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) { | |
ba25f9dc | 563 | printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr)); |
fbb9ce95 IM |
564 | return; |
565 | } | |
566 | printk("%d lock%s held by %s/%d:\n", | |
ba25f9dc | 567 | depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr)); |
fbb9ce95 IM |
568 | |
569 | for (i = 0; i < depth; i++) { | |
570 | printk(" #%d: ", i); | |
571 | print_lock(curr->held_locks + i); | |
572 | } | |
573 | } | |
fbb9ce95 | 574 | |
fbdc4b9a | 575 | static void print_kernel_ident(void) |
8e18257d | 576 | { |
fbdc4b9a | 577 | printk("%s %.*s %s\n", init_utsname()->release, |
8e18257d | 578 | (int)strcspn(init_utsname()->version, " "), |
fbdc4b9a BH |
579 | init_utsname()->version, |
580 | print_tainted()); | |
8e18257d PZ |
581 | } |
582 | ||
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 | } | |
590 | ||
fbb9ce95 | 591 | /* |
8e18257d | 592 | * Is this the address of a static object: |
fbb9ce95 | 593 | */ |
8dce7a9a | 594 | #ifdef __KERNEL__ |
8e18257d | 595 | static int static_obj(void *obj) |
fbb9ce95 | 596 | { |
8e18257d PZ |
597 | unsigned long start = (unsigned long) &_stext, |
598 | end = (unsigned long) &_end, | |
599 | addr = (unsigned long) obj; | |
8e18257d | 600 | |
fbb9ce95 | 601 | /* |
8e18257d | 602 | * static variable? |
fbb9ce95 | 603 | */ |
8e18257d PZ |
604 | if ((addr >= start) && (addr < end)) |
605 | return 1; | |
fbb9ce95 | 606 | |
2a9ad18d MF |
607 | if (arch_is_kernel_data(addr)) |
608 | return 1; | |
609 | ||
fbb9ce95 | 610 | /* |
10fad5e4 | 611 | * in-kernel percpu var? |
fbb9ce95 | 612 | */ |
10fad5e4 TH |
613 | if (is_kernel_percpu_address(addr)) |
614 | return 1; | |
fbb9ce95 | 615 | |
8e18257d | 616 | /* |
10fad5e4 | 617 | * module static or percpu var? |
8e18257d | 618 | */ |
10fad5e4 | 619 | return is_module_address(addr) || is_module_percpu_address(addr); |
99de055a | 620 | } |
8dce7a9a | 621 | #endif |
99de055a | 622 | |
fbb9ce95 | 623 | /* |
8e18257d PZ |
624 | * To make lock name printouts unique, we calculate a unique |
625 | * class->name_version generation counter: | |
fbb9ce95 | 626 | */ |
8e18257d | 627 | static int count_matching_names(struct lock_class *new_class) |
fbb9ce95 | 628 | { |
8e18257d PZ |
629 | struct lock_class *class; |
630 | int count = 0; | |
fbb9ce95 | 631 | |
8e18257d | 632 | if (!new_class->name) |
fbb9ce95 IM |
633 | return 0; |
634 | ||
35a9393c | 635 | list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) { |
8e18257d PZ |
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 | } | |
fbb9ce95 | 641 | |
8e18257d | 642 | return count + 1; |
fbb9ce95 IM |
643 | } |
644 | ||
8e18257d PZ |
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 * | |
651 | look_up_lock_class(struct lockdep_map *lock, unsigned int subclass) | |
fbb9ce95 | 652 | { |
8e18257d | 653 | struct lockdep_subclass_key *key; |
a63f38cc | 654 | struct hlist_head *hash_head; |
8e18257d | 655 | struct lock_class *class; |
fbb9ce95 | 656 | |
4ba053c0 HM |
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 | ||
8e18257d PZ |
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; | |
fbb9ce95 | 673 | |
8e18257d PZ |
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 | */ | |
4b32d0a4 PZ |
680 | BUILD_BUG_ON(sizeof(struct lock_class_key) > |
681 | sizeof(struct lockdep_map)); | |
fbb9ce95 | 682 | |
8e18257d | 683 | key = lock->key->subkeys + subclass; |
ca268c69 | 684 | |
8e18257d | 685 | hash_head = classhashentry(key); |
74c383f1 | 686 | |
8e18257d | 687 | /* |
35a9393c | 688 | * We do an RCU walk of the hash, see lockdep_free_key_range(). |
8e18257d | 689 | */ |
35a9393c PZ |
690 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
691 | return NULL; | |
692 | ||
a63f38cc | 693 | hlist_for_each_entry_rcu(class, hash_head, hash_entry) { |
4b32d0a4 | 694 | if (class->key == key) { |
0119fee4 PZ |
695 | /* |
696 | * Huh! same key, different name? Did someone trample | |
697 | * on some memory? We're most confused. | |
698 | */ | |
4b32d0a4 | 699 | WARN_ON_ONCE(class->name != lock->name); |
8e18257d | 700 | return class; |
4b32d0a4 PZ |
701 | } |
702 | } | |
fbb9ce95 | 703 | |
8e18257d | 704 | return NULL; |
fbb9ce95 IM |
705 | } |
706 | ||
707 | /* | |
8e18257d PZ |
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. | |
fbb9ce95 | 711 | */ |
c003ed92 | 712 | static struct lock_class * |
8e18257d | 713 | register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force) |
fbb9ce95 | 714 | { |
8e18257d | 715 | struct lockdep_subclass_key *key; |
a63f38cc | 716 | struct hlist_head *hash_head; |
8e18257d | 717 | struct lock_class *class; |
35a9393c PZ |
718 | |
719 | DEBUG_LOCKS_WARN_ON(!irqs_disabled()); | |
8e18257d PZ |
720 | |
721 | class = look_up_lock_class(lock, subclass); | |
722 | if (likely(class)) | |
87cdee71 | 723 | goto out_set_class_cache; |
8e18257d PZ |
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 | ||
738 | key = lock->key->subkeys + subclass; | |
739 | hash_head = classhashentry(key); | |
740 | ||
8e18257d | 741 | if (!graph_lock()) { |
8e18257d PZ |
742 | return NULL; |
743 | } | |
744 | /* | |
745 | * We have to do the hash-walk again, to avoid races | |
746 | * with another CPU: | |
747 | */ | |
a63f38cc | 748 | hlist_for_each_entry_rcu(class, hash_head, hash_entry) { |
8e18257d PZ |
749 | if (class->key == key) |
750 | goto out_unlock_set; | |
35a9393c PZ |
751 | } |
752 | ||
8e18257d PZ |
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) { | |
758 | if (!debug_locks_off_graph_unlock()) { | |
8e18257d PZ |
759 | return NULL; |
760 | } | |
8e18257d | 761 | |
2c522836 | 762 | print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!"); |
eedeeabd | 763 | dump_stack(); |
8e18257d PZ |
764 | return NULL; |
765 | } | |
766 | class = lock_classes + nr_lock_classes++; | |
bd6d29c2 | 767 | debug_atomic_inc(nr_unused_locks); |
8e18257d PZ |
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 | */ | |
a63f38cc | 779 | hlist_add_head_rcu(&class->hash_entry, hash_head); |
1481197b DF |
780 | /* |
781 | * Add it to the global list of classes: | |
782 | */ | |
783 | list_add_tail_rcu(&class->lock_entry, &all_lock_classes); | |
8e18257d PZ |
784 | |
785 | if (verbose(class)) { | |
786 | graph_unlock(); | |
8e18257d PZ |
787 | |
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(); | |
793 | ||
8e18257d | 794 | if (!graph_lock()) { |
8e18257d PZ |
795 | return NULL; |
796 | } | |
797 | } | |
798 | out_unlock_set: | |
799 | graph_unlock(); | |
8e18257d | 800 | |
87cdee71 | 801 | out_set_class_cache: |
8e18257d | 802 | if (!subclass || force) |
62016250 HM |
803 | lock->class_cache[0] = class; |
804 | else if (subclass < NR_LOCKDEP_CACHING_CLASSES) | |
805 | lock->class_cache[subclass] = class; | |
8e18257d | 806 | |
0119fee4 PZ |
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 | */ | |
8e18257d PZ |
811 | if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass)) |
812 | return NULL; | |
813 | ||
814 | return class; | |
815 | } | |
816 | ||
817 | #ifdef CONFIG_PROVE_LOCKING | |
818 | /* | |
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 | ||
2c522836 | 828 | print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!"); |
eedeeabd | 829 | dump_stack(); |
8e18257d PZ |
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, | |
4726f2a6 YZ |
839 | struct list_head *head, unsigned long ip, |
840 | int distance, struct stack_trace *trace) | |
8e18257d PZ |
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 | ||
74870172 ZY |
851 | entry->class = this; |
852 | entry->distance = distance; | |
4726f2a6 | 853 | entry->trace = *trace; |
8e18257d | 854 | /* |
35a9393c PZ |
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(). | |
8e18257d PZ |
858 | */ |
859 | list_add_tail_rcu(&entry->entry, head); | |
860 | ||
861 | return 1; | |
862 | } | |
863 | ||
98c33edd PZ |
864 | /* |
865 | * For good efficiency of modular, we use power of 2 | |
866 | */ | |
af012961 PZ |
867 | #define MAX_CIRCULAR_QUEUE_SIZE 4096UL |
868 | #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1) | |
869 | ||
98c33edd PZ |
870 | /* |
871 | * The circular_queue and helpers is used to implement the | |
af012961 PZ |
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. | |
98c33edd | 875 | */ |
af012961 PZ |
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; | |
af012961 | 882 | |
12f3dfd0 | 883 | unsigned int max_bfs_queue_depth; |
af012961 | 884 | |
e351b660 ML |
885 | static unsigned int lockdep_dependency_gen_id; |
886 | ||
af012961 PZ |
887 | static inline void __cq_init(struct circular_queue *cq) |
888 | { | |
889 | cq->front = cq->rear = 0; | |
e351b660 | 890 | lockdep_dependency_gen_id++; |
af012961 PZ |
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; | |
98c33edd | 932 | |
af012961 | 933 | nr = lock - list_entries; |
0119fee4 | 934 | WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */ |
af012961 | 935 | lock->parent = parent; |
e351b660 | 936 | lock->class->dep_gen_id = lockdep_dependency_gen_id; |
af012961 PZ |
937 | } |
938 | ||
939 | static inline unsigned long lock_accessed(struct lock_list *lock) | |
940 | { | |
941 | unsigned long nr; | |
98c33edd | 942 | |
af012961 | 943 | nr = lock - list_entries; |
0119fee4 | 944 | WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */ |
e351b660 | 945 | return lock->class->dep_gen_id == lockdep_dependency_gen_id; |
af012961 PZ |
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 | ||
9e2d551e | 965 | static int __bfs(struct lock_list *source_entry, |
af012961 PZ |
966 | void *data, |
967 | int (*match)(struct lock_list *entry, void *data), | |
968 | struct lock_list **target_entry, | |
969 | int forward) | |
c94aa5ca ML |
970 | { |
971 | struct lock_list *entry; | |
d588e461 | 972 | struct list_head *head; |
c94aa5ca ML |
973 | struct circular_queue *cq = &lock_cq; |
974 | int ret = 1; | |
975 | ||
9e2d551e | 976 | if (match(source_entry, data)) { |
c94aa5ca ML |
977 | *target_entry = source_entry; |
978 | ret = 0; | |
979 | goto exit; | |
980 | } | |
981 | ||
d588e461 ML |
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); | |
c94aa5ca ML |
991 | __cq_enqueue(cq, (unsigned long)source_entry); |
992 | ||
993 | while (!__cq_empty(cq)) { | |
994 | struct lock_list *lock; | |
c94aa5ca ML |
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 | ||
35a9393c PZ |
1008 | DEBUG_LOCKS_WARN_ON(!irqs_disabled()); |
1009 | ||
1010 | list_for_each_entry_rcu(entry, head, entry) { | |
c94aa5ca | 1011 | if (!lock_accessed(entry)) { |
12f3dfd0 | 1012 | unsigned int cq_depth; |
c94aa5ca | 1013 | mark_lock_accessed(entry, lock); |
9e2d551e | 1014 | if (match(entry, data)) { |
c94aa5ca ML |
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 | } | |
12f3dfd0 ML |
1024 | cq_depth = __cq_get_elem_count(cq); |
1025 | if (max_bfs_queue_depth < cq_depth) | |
1026 | max_bfs_queue_depth = cq_depth; | |
c94aa5ca ML |
1027 | } |
1028 | } | |
1029 | } | |
1030 | exit: | |
1031 | return ret; | |
1032 | } | |
1033 | ||
d7aaba14 | 1034 | static inline int __bfs_forwards(struct lock_list *src_entry, |
9e2d551e ML |
1035 | void *data, |
1036 | int (*match)(struct lock_list *entry, void *data), | |
1037 | struct lock_list **target_entry) | |
c94aa5ca | 1038 | { |
9e2d551e | 1039 | return __bfs(src_entry, data, match, target_entry, 1); |
c94aa5ca ML |
1040 | |
1041 | } | |
1042 | ||
d7aaba14 | 1043 | static inline int __bfs_backwards(struct lock_list *src_entry, |
9e2d551e ML |
1044 | void *data, |
1045 | int (*match)(struct lock_list *entry, void *data), | |
1046 | struct lock_list **target_entry) | |
c94aa5ca | 1047 | { |
9e2d551e | 1048 | return __bfs(src_entry, data, match, target_entry, 0); |
c94aa5ca ML |
1049 | |
1050 | } | |
1051 | ||
8e18257d PZ |
1052 | /* |
1053 | * Recursive, forwards-direction lock-dependency checking, used for | |
1054 | * both noncyclic checking and for hardirq-unsafe/softirq-unsafe | |
1055 | * checking. | |
8e18257d | 1056 | */ |
8e18257d PZ |
1057 | |
1058 | /* | |
1059 | * Print a dependency chain entry (this is only done when a deadlock | |
1060 | * has been detected): | |
1061 | */ | |
1062 | static noinline int | |
24208ca7 | 1063 | print_circular_bug_entry(struct lock_list *target, int depth) |
8e18257d PZ |
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 | ||
f4185812 SR |
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 | ||
8e18257d PZ |
1125 | /* |
1126 | * When a circular dependency is detected, print the | |
1127 | * header first: | |
1128 | */ | |
1129 | static noinline int | |
db0002a3 ML |
1130 | print_circular_bug_header(struct lock_list *entry, unsigned int depth, |
1131 | struct held_lock *check_src, | |
1132 | struct held_lock *check_tgt) | |
8e18257d PZ |
1133 | { |
1134 | struct task_struct *curr = current; | |
1135 | ||
c94aa5ca | 1136 | if (debug_locks_silent) |
8e18257d PZ |
1137 | return 0; |
1138 | ||
b3fbab05 PM |
1139 | printk("\n"); |
1140 | printk("======================================================\n"); | |
1141 | printk("[ INFO: possible circular locking dependency detected ]\n"); | |
fbdc4b9a | 1142 | print_kernel_ident(); |
b3fbab05 | 1143 | printk("-------------------------------------------------------\n"); |
8e18257d | 1144 | printk("%s/%d is trying to acquire lock:\n", |
ba25f9dc | 1145 | curr->comm, task_pid_nr(curr)); |
db0002a3 | 1146 | print_lock(check_src); |
8e18257d | 1147 | printk("\nbut task is already holding lock:\n"); |
db0002a3 | 1148 | print_lock(check_tgt); |
8e18257d PZ |
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 | ||
9e2d551e ML |
1157 | static inline int class_equal(struct lock_list *entry, void *data) |
1158 | { | |
1159 | return entry->class == data; | |
1160 | } | |
1161 | ||
db0002a3 ML |
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) | |
8e18257d PZ |
1166 | { |
1167 | struct task_struct *curr = current; | |
c94aa5ca | 1168 | struct lock_list *parent; |
f4185812 | 1169 | struct lock_list *first_parent; |
24208ca7 | 1170 | int depth; |
8e18257d | 1171 | |
c94aa5ca | 1172 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
8e18257d PZ |
1173 | return 0; |
1174 | ||
db0002a3 | 1175 | if (!save_trace(&this->trace)) |
8e18257d PZ |
1176 | return 0; |
1177 | ||
c94aa5ca ML |
1178 | depth = get_lock_depth(target); |
1179 | ||
db0002a3 | 1180 | print_circular_bug_header(target, depth, check_src, check_tgt); |
c94aa5ca ML |
1181 | |
1182 | parent = get_lock_parent(target); | |
f4185812 | 1183 | first_parent = parent; |
c94aa5ca ML |
1184 | |
1185 | while (parent) { | |
1186 | print_circular_bug_entry(parent, --depth); | |
1187 | parent = get_lock_parent(parent); | |
1188 | } | |
8e18257d PZ |
1189 | |
1190 | printk("\nother info that might help us debug this:\n\n"); | |
f4185812 SR |
1191 | print_circular_lock_scenario(check_src, check_tgt, |
1192 | first_parent); | |
1193 | ||
8e18257d PZ |
1194 | lockdep_print_held_locks(curr); |
1195 | ||
1196 | printk("\nstack backtrace:\n"); | |
1197 | dump_stack(); | |
1198 | ||
1199 | return 0; | |
1200 | } | |
1201 | ||
db0002a3 ML |
1202 | static noinline int print_bfs_bug(int ret) |
1203 | { | |
1204 | if (!debug_locks_off_graph_unlock()) | |
1205 | return 0; | |
1206 | ||
0119fee4 PZ |
1207 | /* |
1208 | * Breadth-first-search failed, graph got corrupted? | |
1209 | */ | |
db0002a3 ML |
1210 | WARN(1, "lockdep bfs error:%d\n", ret); |
1211 | ||
1212 | return 0; | |
1213 | } | |
1214 | ||
ef681026 | 1215 | static int noop_count(struct lock_list *entry, void *data) |
419ca3f1 | 1216 | { |
ef681026 ML |
1217 | (*(unsigned long *)data)++; |
1218 | return 0; | |
1219 | } | |
419ca3f1 | 1220 | |
5216d530 | 1221 | static unsigned long __lockdep_count_forward_deps(struct lock_list *this) |
ef681026 ML |
1222 | { |
1223 | unsigned long count = 0; | |
1224 | struct lock_list *uninitialized_var(target_entry); | |
419ca3f1 | 1225 | |
ef681026 | 1226 | __bfs_forwards(this, (void *)&count, noop_count, &target_entry); |
419ca3f1 | 1227 | |
ef681026 | 1228 | return count; |
419ca3f1 | 1229 | } |
419ca3f1 DM |
1230 | unsigned long lockdep_count_forward_deps(struct lock_class *class) |
1231 | { | |
1232 | unsigned long ret, flags; | |
ef681026 ML |
1233 | struct lock_list this; |
1234 | ||
1235 | this.parent = NULL; | |
1236 | this.class = class; | |
419ca3f1 DM |
1237 | |
1238 | local_irq_save(flags); | |
0199c4e6 | 1239 | arch_spin_lock(&lockdep_lock); |
ef681026 | 1240 | ret = __lockdep_count_forward_deps(&this); |
0199c4e6 | 1241 | arch_spin_unlock(&lockdep_lock); |
419ca3f1 DM |
1242 | local_irq_restore(flags); |
1243 | ||
1244 | return ret; | |
1245 | } | |
1246 | ||
5216d530 | 1247 | static unsigned long __lockdep_count_backward_deps(struct lock_list *this) |
419ca3f1 | 1248 | { |
ef681026 ML |
1249 | unsigned long count = 0; |
1250 | struct lock_list *uninitialized_var(target_entry); | |
419ca3f1 | 1251 | |
ef681026 | 1252 | __bfs_backwards(this, (void *)&count, noop_count, &target_entry); |
419ca3f1 | 1253 | |
ef681026 | 1254 | return count; |
419ca3f1 DM |
1255 | } |
1256 | ||
1257 | unsigned long lockdep_count_backward_deps(struct lock_class *class) | |
1258 | { | |
1259 | unsigned long ret, flags; | |
ef681026 ML |
1260 | struct lock_list this; |
1261 | ||
1262 | this.parent = NULL; | |
1263 | this.class = class; | |
419ca3f1 DM |
1264 | |
1265 | local_irq_save(flags); | |
0199c4e6 | 1266 | arch_spin_lock(&lockdep_lock); |
ef681026 | 1267 | ret = __lockdep_count_backward_deps(&this); |
0199c4e6 | 1268 | arch_spin_unlock(&lockdep_lock); |
419ca3f1 DM |
1269 | local_irq_restore(flags); |
1270 | ||
1271 | return ret; | |
1272 | } | |
1273 | ||
8e18257d PZ |
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 | |
db0002a3 ML |
1279 | check_noncircular(struct lock_list *root, struct lock_class *target, |
1280 | struct lock_list **target_entry) | |
8e18257d | 1281 | { |
db0002a3 | 1282 | int result; |
8e18257d | 1283 | |
bd6d29c2 | 1284 | debug_atomic_inc(nr_cyclic_checks); |
419ca3f1 | 1285 | |
d7aaba14 | 1286 | result = __bfs_forwards(root, target, class_equal, target_entry); |
fbb9ce95 | 1287 | |
db0002a3 ML |
1288 | return result; |
1289 | } | |
c94aa5ca | 1290 | |
81d68a96 | 1291 | #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) |
fbb9ce95 IM |
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 | */ | |
fbb9ce95 | 1297 | |
d7aaba14 ML |
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 | ||
fbb9ce95 IM |
1305 | /* |
1306 | * Find a node in the forwards-direction dependency sub-graph starting | |
d7aaba14 | 1307 | * at @root->class that matches @bit. |
fbb9ce95 | 1308 | * |
d7aaba14 ML |
1309 | * Return 0 if such a node exists in the subgraph, and put that node |
1310 | * into *@target_entry. | |
fbb9ce95 | 1311 | * |
d7aaba14 ML |
1312 | * Return 1 otherwise and keep *@target_entry unchanged. |
1313 | * Return <0 on error. | |
fbb9ce95 | 1314 | */ |
d7aaba14 ML |
1315 | static int |
1316 | find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit, | |
1317 | struct lock_list **target_entry) | |
fbb9ce95 | 1318 | { |
d7aaba14 | 1319 | int result; |
fbb9ce95 | 1320 | |
bd6d29c2 | 1321 | debug_atomic_inc(nr_find_usage_forwards_checks); |
fbb9ce95 | 1322 | |
d7aaba14 ML |
1323 | result = __bfs_forwards(root, (void *)bit, usage_match, target_entry); |
1324 | ||
1325 | return result; | |
fbb9ce95 IM |
1326 | } |
1327 | ||
1328 | /* | |
1329 | * Find a node in the backwards-direction dependency sub-graph starting | |
d7aaba14 | 1330 | * at @root->class that matches @bit. |
fbb9ce95 | 1331 | * |
d7aaba14 ML |
1332 | * Return 0 if such a node exists in the subgraph, and put that node |
1333 | * into *@target_entry. | |
fbb9ce95 | 1334 | * |
d7aaba14 ML |
1335 | * Return 1 otherwise and keep *@target_entry unchanged. |
1336 | * Return <0 on error. | |
fbb9ce95 | 1337 | */ |
d7aaba14 ML |
1338 | static int |
1339 | find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit, | |
1340 | struct lock_list **target_entry) | |
fbb9ce95 | 1341 | { |
d7aaba14 | 1342 | int result; |
fbb9ce95 | 1343 | |
bd6d29c2 | 1344 | debug_atomic_inc(nr_find_usage_backwards_checks); |
fbb9ce95 | 1345 | |
d7aaba14 | 1346 | result = __bfs_backwards(root, (void *)bit, usage_match, target_entry); |
f82b217e | 1347 | |
d7aaba14 | 1348 | return result; |
fbb9ce95 IM |
1349 | } |
1350 | ||
af012961 PZ |
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)) { | |
6be8c393 | 1395 | printk("lockdep:%s bad path found in chain graph\n", __func__); |
af012961 PZ |
1396 | break; |
1397 | } | |
1398 | ||
1399 | entry = get_lock_parent(entry); | |
1400 | depth--; | |
1401 | } while (entry && (depth >= 0)); | |
1402 | ||
1403 | return; | |
1404 | } | |
d7aaba14 | 1405 | |
3003eba3 SR |
1406 | static void |
1407 | print_irq_lock_scenario(struct lock_list *safe_entry, | |
1408 | struct lock_list *unsafe_entry, | |
dad3d743 SR |
1409 | struct lock_class *prev_class, |
1410 | struct lock_class *next_class) | |
3003eba3 SR |
1411 | { |
1412 | struct lock_class *safe_class = safe_entry->class; | |
1413 | struct lock_class *unsafe_class = unsafe_entry->class; | |
dad3d743 | 1414 | struct lock_class *middle_class = prev_class; |
3003eba3 SR |
1415 | |
1416 | if (middle_class == safe_class) | |
dad3d743 | 1417 | middle_class = next_class; |
3003eba3 SR |
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 | ||
fbb9ce95 IM |
1462 | static int |
1463 | print_bad_irq_dependency(struct task_struct *curr, | |
24208ca7 ML |
1464 | struct lock_list *prev_root, |
1465 | struct lock_list *next_root, | |
1466 | struct lock_list *backwards_entry, | |
1467 | struct lock_list *forwards_entry, | |
fbb9ce95 IM |
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 | { | |
74c383f1 | 1474 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
fbb9ce95 IM |
1475 | return 0; |
1476 | ||
b3fbab05 PM |
1477 | printk("\n"); |
1478 | printk("======================================================\n"); | |
1479 | printk("[ INFO: %s-safe -> %s-unsafe lock order detected ]\n", | |
fbb9ce95 | 1480 | irqclass, irqclass); |
fbdc4b9a | 1481 | print_kernel_ident(); |
b3fbab05 | 1482 | printk("------------------------------------------------------\n"); |
fbb9ce95 | 1483 | printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n", |
ba25f9dc | 1484 | curr->comm, task_pid_nr(curr), |
fbb9ce95 IM |
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"); | |
f82b217e | 1494 | print_lock_name(hlock_class(prev)); |
fbb9ce95 | 1495 | printk(" ->"); |
f82b217e | 1496 | print_lock_name(hlock_class(next)); |
fbb9ce95 IM |
1497 | printk("\n"); |
1498 | ||
1499 | printk("\nbut this new dependency connects a %s-irq-safe lock:\n", | |
1500 | irqclass); | |
24208ca7 | 1501 | print_lock_name(backwards_entry->class); |
fbb9ce95 IM |
1502 | printk("\n... which became %s-irq-safe at:\n", irqclass); |
1503 | ||
24208ca7 | 1504 | print_stack_trace(backwards_entry->class->usage_traces + bit1, 1); |
fbb9ce95 IM |
1505 | |
1506 | printk("\nto a %s-irq-unsafe lock:\n", irqclass); | |
24208ca7 | 1507 | print_lock_name(forwards_entry->class); |
fbb9ce95 IM |
1508 | printk("\n... which became %s-irq-unsafe at:\n", irqclass); |
1509 | printk("..."); | |
1510 | ||
24208ca7 | 1511 | print_stack_trace(forwards_entry->class->usage_traces + bit2, 1); |
fbb9ce95 IM |
1512 | |
1513 | printk("\nother info that might help us debug this:\n\n"); | |
dad3d743 SR |
1514 | print_irq_lock_scenario(backwards_entry, forwards_entry, |
1515 | hlock_class(prev), hlock_class(next)); | |
3003eba3 | 1516 | |
fbb9ce95 IM |
1517 | lockdep_print_held_locks(curr); |
1518 | ||
24208ca7 ML |
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); | |
fbb9ce95 | 1524 | |
24208ca7 ML |
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); | |
fbb9ce95 IM |
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; | |
24208ca7 | 1543 | struct lock_list this, that; |
d7aaba14 | 1544 | struct lock_list *uninitialized_var(target_entry); |
24208ca7 | 1545 | struct lock_list *uninitialized_var(target_entry1); |
d7aaba14 ML |
1546 | |
1547 | this.parent = NULL; | |
1548 | ||
1549 | this.class = hlock_class(prev); | |
1550 | ret = find_usage_backwards(&this, bit_backwards, &target_entry); | |
af012961 PZ |
1551 | if (ret < 0) |
1552 | return print_bfs_bug(ret); | |
1553 | if (ret == 1) | |
1554 | return ret; | |
d7aaba14 | 1555 | |
24208ca7 ML |
1556 | that.parent = NULL; |
1557 | that.class = hlock_class(next); | |
1558 | ret = find_usage_forwards(&that, bit_forwards, &target_entry1); | |
af012961 PZ |
1559 | if (ret < 0) |
1560 | return print_bfs_bug(ret); | |
1561 | if (ret == 1) | |
1562 | return ret; | |
fbb9ce95 | 1563 | |
24208ca7 ML |
1564 | return print_bad_irq_dependency(curr, &this, &that, |
1565 | target_entry, target_entry1, | |
1566 | prev, next, | |
fbb9ce95 IM |
1567 | bit_backwards, bit_forwards, irqclass); |
1568 | } | |
1569 | ||
4f367d8a PZ |
1570 | static const char *state_names[] = { |
1571 | #define LOCKDEP_STATE(__STATE) \ | |
b4b136f4 | 1572 | __stringify(__STATE), |
4f367d8a PZ |
1573 | #include "lockdep_states.h" |
1574 | #undef LOCKDEP_STATE | |
1575 | }; | |
1576 | ||
1577 | static const char *state_rnames[] = { | |
1578 | #define LOCKDEP_STATE(__STATE) \ | |
b4b136f4 | 1579 | __stringify(__STATE)"-READ", |
4f367d8a PZ |
1580 | #include "lockdep_states.h" |
1581 | #undef LOCKDEP_STATE | |
1582 | }; | |
1583 | ||
1584 | static inline const char *state_name(enum lock_usage_bit bit) | |
8e18257d | 1585 | { |
4f367d8a PZ |
1586 | return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2]; |
1587 | } | |
8e18257d | 1588 | |
4f367d8a PZ |
1589 | static int exclusive_bit(int new_bit) |
1590 | { | |
8e18257d | 1591 | /* |
4f367d8a PZ |
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 | |
8e18257d | 1600 | */ |
4f367d8a PZ |
1601 | |
1602 | int state = new_bit & ~3; | |
1603 | int dir = new_bit & 2; | |
8e18257d PZ |
1604 | |
1605 | /* | |
4f367d8a | 1606 | * keep state, bit flip the direction and strip read. |
8e18257d | 1607 | */ |
4f367d8a PZ |
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) | |
1613 | { | |
8e18257d | 1614 | /* |
4f367d8a PZ |
1615 | * Prove that the new dependency does not connect a hardirq-safe |
1616 | * lock with a hardirq-unsafe lock - to achieve this we search | |
8e18257d PZ |
1617 | * the backwards-subgraph starting at <prev>, and the |
1618 | * forwards-subgraph starting at <next>: | |
1619 | */ | |
4f367d8a PZ |
1620 | if (!check_usage(curr, prev, next, bit, |
1621 | exclusive_bit(bit), state_name(bit))) | |
8e18257d PZ |
1622 | return 0; |
1623 | ||
4f367d8a PZ |
1624 | bit++; /* _READ */ |
1625 | ||
cf40bd16 | 1626 | /* |
4f367d8a PZ |
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 | |
cf40bd16 NP |
1629 | * the backwards-subgraph starting at <prev>, and the |
1630 | * forwards-subgraph starting at <next>: | |
1631 | */ | |
4f367d8a PZ |
1632 | if (!check_usage(curr, prev, next, bit, |
1633 | exclusive_bit(bit), state_name(bit))) | |
cf40bd16 NP |
1634 | return 0; |
1635 | ||
4f367d8a PZ |
1636 | return 1; |
1637 | } | |
1638 | ||
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)) \ | |
cf40bd16 | 1645 | return 0; |
4f367d8a PZ |
1646 | #include "lockdep_states.h" |
1647 | #undef LOCKDEP_STATE | |
cf40bd16 | 1648 | |
8e18257d PZ |
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 | ||
fbb9ce95 IM |
1678 | #endif |
1679 | ||
48702ecf SR |
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 | ||
fbb9ce95 IM |
1700 | static int |
1701 | print_deadlock_bug(struct task_struct *curr, struct held_lock *prev, | |
1702 | struct held_lock *next) | |
1703 | { | |
74c383f1 | 1704 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
fbb9ce95 IM |
1705 | return 0; |
1706 | ||
b3fbab05 PM |
1707 | printk("\n"); |
1708 | printk("=============================================\n"); | |
1709 | printk("[ INFO: possible recursive locking detected ]\n"); | |
fbdc4b9a | 1710 | print_kernel_ident(); |
b3fbab05 | 1711 | printk("---------------------------------------------\n"); |
fbb9ce95 | 1712 | printk("%s/%d is trying to acquire lock:\n", |
ba25f9dc | 1713 | curr->comm, task_pid_nr(curr)); |
fbb9ce95 IM |
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"); | |
48702ecf | 1719 | print_deadlock_scenario(next, prev); |
fbb9ce95 IM |
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; | |
7531e2f3 | 1741 | struct held_lock *nest = NULL; |
fbb9ce95 IM |
1742 | int i; |
1743 | ||
1744 | for (i = 0; i < curr->lockdep_depth; i++) { | |
1745 | prev = curr->held_locks + i; | |
7531e2f3 PZ |
1746 | |
1747 | if (prev->instance == next->nest_lock) | |
1748 | nest = prev; | |
1749 | ||
f82b217e | 1750 | if (hlock_class(prev) != hlock_class(next)) |
fbb9ce95 | 1751 | continue; |
7531e2f3 | 1752 | |
fbb9ce95 IM |
1753 | /* |
1754 | * Allow read-after-read recursion of the same | |
6c9076ec | 1755 | * lock class (i.e. read_lock(lock)+read_lock(lock)): |
fbb9ce95 | 1756 | */ |
6c9076ec | 1757 | if ((read == 2) && prev->read) |
fbb9ce95 | 1758 | return 2; |
7531e2f3 PZ |
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 | ||
fbb9ce95 IM |
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, | |
8a5fd564 | 1796 | struct held_lock *next, int distance, int *stack_saved) |
fbb9ce95 IM |
1797 | { |
1798 | struct lock_list *entry; | |
1799 | int ret; | |
db0002a3 ML |
1800 | struct lock_list this; |
1801 | struct lock_list *uninitialized_var(target_entry); | |
4726f2a6 YZ |
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; | |
fbb9ce95 IM |
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 | */ | |
db0002a3 ML |
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); | |
c94aa5ca | 1827 | |
8e18257d | 1828 | if (!check_prev_add_irq(curr, prev, next)) |
fbb9ce95 IM |
1829 | return 0; |
1830 | ||
fbb9ce95 IM |
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 | */ | |
f82b217e DJ |
1849 | list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) { |
1850 | if (entry->class == hlock_class(next)) { | |
068135e6 JB |
1851 | if (distance == 1) |
1852 | entry->distance = 1; | |
fbb9ce95 | 1853 | return 2; |
068135e6 | 1854 | } |
fbb9ce95 IM |
1855 | } |
1856 | ||
8a5fd564 DV |
1857 | if (!*stack_saved) { |
1858 | if (!save_trace(&trace)) | |
1859 | return 0; | |
1860 | *stack_saved = 1; | |
1861 | } | |
4726f2a6 | 1862 | |
fbb9ce95 IM |
1863 | /* |
1864 | * Ok, all validations passed, add the new lock | |
1865 | * to the previous lock's dependency list: | |
1866 | */ | |
f82b217e DJ |
1867 | ret = add_lock_to_list(hlock_class(prev), hlock_class(next), |
1868 | &hlock_class(prev)->locks_after, | |
4726f2a6 | 1869 | next->acquire_ip, distance, &trace); |
068135e6 | 1870 | |
fbb9ce95 IM |
1871 | if (!ret) |
1872 | return 0; | |
910b1b2e | 1873 | |
f82b217e DJ |
1874 | ret = add_lock_to_list(hlock_class(next), hlock_class(prev), |
1875 | &hlock_class(next)->locks_before, | |
4726f2a6 | 1876 | next->acquire_ip, distance, &trace); |
910b1b2e JP |
1877 | if (!ret) |
1878 | return 0; | |
fbb9ce95 IM |
1879 | |
1880 | /* | |
8e18257d PZ |
1881 | * Debugging printouts: |
1882 | */ | |
f82b217e | 1883 | if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) { |
8a5fd564 DV |
1884 | /* We drop graph lock, so another thread can overwrite trace. */ |
1885 | *stack_saved = 0; | |
8e18257d PZ |
1886 | graph_unlock(); |
1887 | printk("\n new dependency: "); | |
f82b217e | 1888 | print_lock_name(hlock_class(prev)); |
8e18257d | 1889 | printk(" => "); |
f82b217e | 1890 | print_lock_name(hlock_class(next)); |
8e18257d | 1891 | printk("\n"); |
fbb9ce95 | 1892 | dump_stack(); |
8e18257d | 1893 | return graph_lock(); |
fbb9ce95 | 1894 | } |
8e18257d PZ |
1895 | return 1; |
1896 | } | |
fbb9ce95 | 1897 | |
8e18257d PZ |
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; | |
8a5fd564 | 1908 | int stack_saved = 0; |
8e18257d | 1909 | struct held_lock *hlock; |
d6d897ce | 1910 | |
fbb9ce95 | 1911 | /* |
8e18257d PZ |
1912 | * Debugging checks. |
1913 | * | |
1914 | * Depth must not be zero for a non-head lock: | |
fbb9ce95 | 1915 | */ |
8e18257d PZ |
1916 | if (!depth) |
1917 | goto out_bug; | |
fbb9ce95 | 1918 | /* |
8e18257d PZ |
1919 | * At least two relevant locks must exist for this |
1920 | * to be a head: | |
fbb9ce95 | 1921 | */ |
8e18257d PZ |
1922 | if (curr->held_locks[depth].irq_context != |
1923 | curr->held_locks[depth-1].irq_context) | |
1924 | goto out_bug; | |
74c383f1 | 1925 | |
8e18257d PZ |
1926 | for (;;) { |
1927 | int distance = curr->lockdep_depth - depth + 1; | |
1b5ff816 | 1928 | hlock = curr->held_locks + depth - 1; |
8e18257d PZ |
1929 | /* |
1930 | * Only non-recursive-read entries get new dependencies | |
1931 | * added: | |
1932 | */ | |
1b5ff816 | 1933 | if (hlock->read != 2 && hlock->check) { |
4726f2a6 | 1934 | if (!check_prev_add(curr, hlock, next, |
8a5fd564 | 1935 | distance, &stack_saved)) |
8e18257d PZ |
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; | |
74c383f1 | 1945 | } |
8e18257d PZ |
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; | |
fbb9ce95 | 1958 | } |
8e18257d PZ |
1959 | return 1; |
1960 | out_bug: | |
1961 | if (!debug_locks_off_graph_unlock()) | |
1962 | return 0; | |
fbb9ce95 | 1963 | |
0119fee4 PZ |
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 | */ | |
8e18257d | 1969 | WARN_ON(1); |
fbb9ce95 | 1970 | |
8e18257d | 1971 | return 0; |
fbb9ce95 IM |
1972 | } |
1973 | ||
8e18257d | 1974 | unsigned long nr_lock_chains; |
443cd507 | 1975 | struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS]; |
cd1a28e8 | 1976 | int nr_chain_hlocks; |
443cd507 HY |
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 | } | |
8e18257d | 1983 | |
9e4e7554 IM |
1984 | /* |
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 | ||
5c8a010c | 2003 | #ifdef CONFIG_DEBUG_LOCKDEP |
39e2e173 AAF |
2004 | /* |
2005 | * Returns the next chain_key iteration | |
2006 | */ | |
2007 | static u64 print_chain_key_iteration(int class_idx, u64 chain_key) | |
2008 | { | |
2009 | u64 new_chain_key = iterate_chain_key(chain_key, class_idx); | |
2010 | ||
2011 | printk(" class_idx:%d -> chain_key:%016Lx", | |
2012 | class_idx, | |
2013 | (unsigned long long)new_chain_key); | |
2014 | return new_chain_key; | |
2015 | } | |
2016 | ||
2017 | static void | |
2018 | print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next) | |
2019 | { | |
2020 | struct held_lock *hlock; | |
2021 | u64 chain_key = 0; | |
2022 | int depth = curr->lockdep_depth; | |
2023 | int i; | |
2024 | ||
2025 | printk("depth: %u\n", depth + 1); | |
2026 | for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) { | |
2027 | hlock = curr->held_locks + i; | |
2028 | chain_key = print_chain_key_iteration(hlock->class_idx, chain_key); | |
2029 | ||
2030 | print_lock(hlock); | |
2031 | } | |
2032 | ||
2033 | print_chain_key_iteration(hlock_next->class_idx, chain_key); | |
2034 | print_lock(hlock_next); | |
2035 | } | |
2036 | ||
2037 | static void print_chain_keys_chain(struct lock_chain *chain) | |
2038 | { | |
2039 | int i; | |
2040 | u64 chain_key = 0; | |
2041 | int class_id; | |
2042 | ||
2043 | printk("depth: %u\n", chain->depth); | |
2044 | for (i = 0; i < chain->depth; i++) { | |
2045 | class_id = chain_hlocks[chain->base + i]; | |
2046 | chain_key = print_chain_key_iteration(class_id + 1, chain_key); | |
2047 | ||
2048 | print_lock_name(lock_classes + class_id); | |
2049 | printk("\n"); | |
2050 | } | |
2051 | } | |
2052 | ||
2053 | static void print_collision(struct task_struct *curr, | |
2054 | struct held_lock *hlock_next, | |
2055 | struct lock_chain *chain) | |
2056 | { | |
2057 | printk("\n"); | |
2058 | printk("======================\n"); | |
2059 | printk("[chain_key collision ]\n"); | |
2060 | print_kernel_ident(); | |
2061 | printk("----------------------\n"); | |
2062 | printk("%s/%d: ", current->comm, task_pid_nr(current)); | |
2063 | printk("Hash chain already cached but the contents don't match!\n"); | |
2064 | ||
2065 | printk("Held locks:"); | |
2066 | print_chain_keys_held_locks(curr, hlock_next); | |
2067 | ||
2068 | printk("Locks in cached chain:"); | |
2069 | print_chain_keys_chain(chain); | |
2070 | ||
2071 | printk("\nstack backtrace:\n"); | |
2072 | dump_stack(); | |
2073 | } | |
5c8a010c | 2074 | #endif |
39e2e173 | 2075 | |
9e4e7554 IM |
2076 | /* |
2077 | * Checks whether the chain and the current held locks are consistent | |
2078 | * in depth and also in content. If they are not it most likely means | |
2079 | * that there was a collision during the calculation of the chain_key. | |
2080 | * Returns: 0 not passed, 1 passed | |
2081 | */ | |
2082 | static int check_no_collision(struct task_struct *curr, | |
2083 | struct held_lock *hlock, | |
2084 | struct lock_chain *chain) | |
2085 | { | |
2086 | #ifdef CONFIG_DEBUG_LOCKDEP | |
2087 | int i, j, id; | |
2088 | ||
2089 | i = get_first_held_lock(curr, hlock); | |
2090 | ||
39e2e173 AAF |
2091 | if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) { |
2092 | print_collision(curr, hlock, chain); | |
9e4e7554 | 2093 | return 0; |
39e2e173 | 2094 | } |
9e4e7554 IM |
2095 | |
2096 | for (j = 0; j < chain->depth - 1; j++, i++) { | |
2097 | id = curr->held_locks[i].class_idx - 1; | |
2098 | ||
39e2e173 AAF |
2099 | if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) { |
2100 | print_collision(curr, hlock, chain); | |
9e4e7554 | 2101 | return 0; |
39e2e173 | 2102 | } |
9e4e7554 IM |
2103 | } |
2104 | #endif | |
2105 | return 1; | |
2106 | } | |
2107 | ||
fbb9ce95 IM |
2108 | /* |
2109 | * Look up a dependency chain. If the key is not present yet then | |
9e860d00 JP |
2110 | * add it and return 1 - in this case the new dependency chain is |
2111 | * validated. If the key is already hashed, return 0. | |
2112 | * (On return with 1 graph_lock is held.) | |
fbb9ce95 | 2113 | */ |
443cd507 HY |
2114 | static inline int lookup_chain_cache(struct task_struct *curr, |
2115 | struct held_lock *hlock, | |
2116 | u64 chain_key) | |
fbb9ce95 | 2117 | { |
f82b217e | 2118 | struct lock_class *class = hlock_class(hlock); |
a63f38cc | 2119 | struct hlist_head *hash_head = chainhashentry(chain_key); |
fbb9ce95 | 2120 | struct lock_chain *chain; |
e0944ee6 | 2121 | int i, j; |
fbb9ce95 | 2122 | |
0119fee4 PZ |
2123 | /* |
2124 | * We might need to take the graph lock, ensure we've got IRQs | |
2125 | * disabled to make this an IRQ-safe lock.. for recursion reasons | |
2126 | * lockdep won't complain about its own locking errors. | |
2127 | */ | |
381a2292 JP |
2128 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
2129 | return 0; | |
fbb9ce95 IM |
2130 | /* |
2131 | * We can walk it lock-free, because entries only get added | |
2132 | * to the hash: | |
2133 | */ | |
a63f38cc | 2134 | hlist_for_each_entry_rcu(chain, hash_head, entry) { |
fbb9ce95 IM |
2135 | if (chain->chain_key == chain_key) { |
2136 | cache_hit: | |
bd6d29c2 | 2137 | debug_atomic_inc(chain_lookup_hits); |
9e4e7554 IM |
2138 | if (!check_no_collision(curr, hlock, chain)) |
2139 | return 0; | |
2140 | ||
81fc685a | 2141 | if (very_verbose(class)) |
755cd900 AM |
2142 | printk("\nhash chain already cached, key: " |
2143 | "%016Lx tail class: [%p] %s\n", | |
2144 | (unsigned long long)chain_key, | |
2145 | class->key, class->name); | |
fbb9ce95 IM |
2146 | return 0; |
2147 | } | |
2148 | } | |
81fc685a | 2149 | if (very_verbose(class)) |
755cd900 AM |
2150 | printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n", |
2151 | (unsigned long long)chain_key, class->key, class->name); | |
fbb9ce95 IM |
2152 | /* |
2153 | * Allocate a new chain entry from the static array, and add | |
2154 | * it to the hash: | |
2155 | */ | |
74c383f1 IM |
2156 | if (!graph_lock()) |
2157 | return 0; | |
fbb9ce95 IM |
2158 | /* |
2159 | * We have to walk the chain again locked - to avoid duplicates: | |
2160 | */ | |
a63f38cc | 2161 | hlist_for_each_entry(chain, hash_head, entry) { |
fbb9ce95 | 2162 | if (chain->chain_key == chain_key) { |
74c383f1 | 2163 | graph_unlock(); |
fbb9ce95 IM |
2164 | goto cache_hit; |
2165 | } | |
2166 | } | |
2167 | if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) { | |
74c383f1 IM |
2168 | if (!debug_locks_off_graph_unlock()) |
2169 | return 0; | |
2170 | ||
2c522836 | 2171 | print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!"); |
eedeeabd | 2172 | dump_stack(); |
fbb9ce95 IM |
2173 | return 0; |
2174 | } | |
2175 | chain = lock_chains + nr_lock_chains++; | |
2176 | chain->chain_key = chain_key; | |
443cd507 | 2177 | chain->irq_context = hlock->irq_context; |
9e4e7554 | 2178 | i = get_first_held_lock(curr, hlock); |
443cd507 | 2179 | chain->depth = curr->lockdep_depth + 1 - i; |
75dd602a PZ |
2180 | |
2181 | BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks)); | |
2182 | BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks)); | |
2183 | BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes)); | |
2184 | ||
e0944ee6 SR |
2185 | if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) { |
2186 | chain->base = nr_chain_hlocks; | |
443cd507 | 2187 | for (j = 0; j < chain->depth - 1; j++, i++) { |
f82b217e | 2188 | int lock_id = curr->held_locks[i].class_idx - 1; |
443cd507 HY |
2189 | chain_hlocks[chain->base + j] = lock_id; |
2190 | } | |
2191 | chain_hlocks[chain->base + j] = class - lock_classes; | |
2192 | } | |
75dd602a PZ |
2193 | |
2194 | if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS) | |
2195 | nr_chain_hlocks += chain->depth; | |
2196 | ||
2197 | #ifdef CONFIG_DEBUG_LOCKDEP | |
2198 | /* | |
2199 | * Important for check_no_collision(). | |
2200 | */ | |
2201 | if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) { | |
2202 | if (debug_locks_off_graph_unlock()) | |
2203 | return 0; | |
2204 | ||
2205 | print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!"); | |
2206 | dump_stack(); | |
2207 | return 0; | |
2208 | } | |
2209 | #endif | |
2210 | ||
a63f38cc | 2211 | hlist_add_head_rcu(&chain->entry, hash_head); |
bd6d29c2 | 2212 | debug_atomic_inc(chain_lookup_misses); |
8e18257d PZ |
2213 | inc_chains(); |
2214 | ||
2215 | return 1; | |
2216 | } | |
2217 | ||
2218 | static int validate_chain(struct task_struct *curr, struct lockdep_map *lock, | |
4e6045f1 | 2219 | struct held_lock *hlock, int chain_head, u64 chain_key) |
8e18257d PZ |
2220 | { |
2221 | /* | |
2222 | * Trylock needs to maintain the stack of held locks, but it | |
2223 | * does not add new dependencies, because trylock can be done | |
2224 | * in any order. | |
2225 | * | |
2226 | * We look up the chain_key and do the O(N^2) check and update of | |
2227 | * the dependencies only if this is a new dependency chain. | |
2228 | * (If lookup_chain_cache() returns with 1 it acquires | |
2229 | * graph_lock for us) | |
2230 | */ | |
fb9edbe9 | 2231 | if (!hlock->trylock && hlock->check && |
443cd507 | 2232 | lookup_chain_cache(curr, hlock, chain_key)) { |
8e18257d PZ |
2233 | /* |
2234 | * Check whether last held lock: | |
2235 | * | |
2236 | * - is irq-safe, if this lock is irq-unsafe | |
2237 | * - is softirq-safe, if this lock is hardirq-unsafe | |
2238 | * | |
2239 | * And check whether the new lock's dependency graph | |
2240 | * could lead back to the previous lock. | |
2241 | * | |
2242 | * any of these scenarios could lead to a deadlock. If | |
2243 | * All validations | |
2244 | */ | |
2245 | int ret = check_deadlock(curr, hlock, lock, hlock->read); | |
2246 | ||
2247 | if (!ret) | |
2248 | return 0; | |
2249 | /* | |
2250 | * Mark recursive read, as we jump over it when | |
2251 | * building dependencies (just like we jump over | |
2252 | * trylock entries): | |
2253 | */ | |
2254 | if (ret == 2) | |
2255 | hlock->read = 2; | |
2256 | /* | |
2257 | * Add dependency only if this lock is not the head | |
2258 | * of the chain, and if it's not a secondary read-lock: | |
2259 | */ | |
2260 | if (!chain_head && ret != 2) | |
2261 | if (!check_prevs_add(curr, hlock)) | |
2262 | return 0; | |
2263 | graph_unlock(); | |
2264 | } else | |
2265 | /* after lookup_chain_cache(): */ | |
2266 | if (unlikely(!debug_locks)) | |
2267 | return 0; | |
fbb9ce95 IM |
2268 | |
2269 | return 1; | |
2270 | } | |
8e18257d PZ |
2271 | #else |
2272 | static inline int validate_chain(struct task_struct *curr, | |
2273 | struct lockdep_map *lock, struct held_lock *hlock, | |
3aa416b0 | 2274 | int chain_head, u64 chain_key) |
8e18257d PZ |
2275 | { |
2276 | return 1; | |
2277 | } | |
ca58abcb | 2278 | #endif |
fbb9ce95 IM |
2279 | |
2280 | /* | |
2281 | * We are building curr_chain_key incrementally, so double-check | |
2282 | * it from scratch, to make sure that it's done correctly: | |
2283 | */ | |
1d09daa5 | 2284 | static void check_chain_key(struct task_struct *curr) |
fbb9ce95 IM |
2285 | { |
2286 | #ifdef CONFIG_DEBUG_LOCKDEP | |
2287 | struct held_lock *hlock, *prev_hlock = NULL; | |
5f18ab5c | 2288 | unsigned int i; |
fbb9ce95 IM |
2289 | u64 chain_key = 0; |
2290 | ||
2291 | for (i = 0; i < curr->lockdep_depth; i++) { | |
2292 | hlock = curr->held_locks + i; | |
2293 | if (chain_key != hlock->prev_chain_key) { | |
2294 | debug_locks_off(); | |
0119fee4 PZ |
2295 | /* |
2296 | * We got mighty confused, our chain keys don't match | |
2297 | * with what we expect, someone trample on our task state? | |
2298 | */ | |
2df8b1d6 | 2299 | WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n", |
fbb9ce95 IM |
2300 | curr->lockdep_depth, i, |
2301 | (unsigned long long)chain_key, | |
2302 | (unsigned long long)hlock->prev_chain_key); | |
fbb9ce95 IM |
2303 | return; |
2304 | } | |
0119fee4 PZ |
2305 | /* |
2306 | * Whoops ran out of static storage again? | |
2307 | */ | |
5f18ab5c | 2308 | if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS)) |
381a2292 JP |
2309 | return; |
2310 | ||
fbb9ce95 IM |
2311 | if (prev_hlock && (prev_hlock->irq_context != |
2312 | hlock->irq_context)) | |
2313 | chain_key = 0; | |
5f18ab5c | 2314 | chain_key = iterate_chain_key(chain_key, hlock->class_idx); |
fbb9ce95 IM |
2315 | prev_hlock = hlock; |
2316 | } | |
2317 | if (chain_key != curr->curr_chain_key) { | |
2318 | debug_locks_off(); | |
0119fee4 PZ |
2319 | /* |
2320 | * More smoking hash instead of calculating it, damn see these | |
2321 | * numbers float.. I bet that a pink elephant stepped on my memory. | |
2322 | */ | |
2df8b1d6 | 2323 | WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n", |
fbb9ce95 IM |
2324 | curr->lockdep_depth, i, |
2325 | (unsigned long long)chain_key, | |
2326 | (unsigned long long)curr->curr_chain_key); | |
fbb9ce95 IM |
2327 | } |
2328 | #endif | |
2329 | } | |
2330 | ||
282b5c2f SR |
2331 | static void |
2332 | print_usage_bug_scenario(struct held_lock *lock) | |
2333 | { | |
2334 | struct lock_class *class = hlock_class(lock); | |
2335 | ||
2336 | printk(" Possible unsafe locking scenario:\n\n"); | |
2337 | printk(" CPU0\n"); | |
2338 | printk(" ----\n"); | |
2339 | printk(" lock("); | |
2340 | __print_lock_name(class); | |
2341 | printk(");\n"); | |
2342 | printk(" <Interrupt>\n"); | |
2343 | printk(" lock("); | |
2344 | __print_lock_name(class); | |
2345 | printk(");\n"); | |
2346 | printk("\n *** DEADLOCK ***\n\n"); | |
2347 | } | |
2348 | ||
8e18257d PZ |
2349 | static int |
2350 | print_usage_bug(struct task_struct *curr, struct held_lock *this, | |
2351 | enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit) | |
2352 | { | |
2353 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) | |
2354 | return 0; | |
2355 | ||
b3fbab05 PM |
2356 | printk("\n"); |
2357 | printk("=================================\n"); | |
2358 | printk("[ INFO: inconsistent lock state ]\n"); | |
fbdc4b9a | 2359 | print_kernel_ident(); |
b3fbab05 | 2360 | printk("---------------------------------\n"); |
8e18257d PZ |
2361 | |
2362 | printk("inconsistent {%s} -> {%s} usage.\n", | |
2363 | usage_str[prev_bit], usage_str[new_bit]); | |
2364 | ||
2365 | printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n", | |
ba25f9dc | 2366 | curr->comm, task_pid_nr(curr), |
8e18257d PZ |
2367 | trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT, |
2368 | trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT, | |
2369 | trace_hardirqs_enabled(curr), | |
2370 | trace_softirqs_enabled(curr)); | |
2371 | print_lock(this); | |
2372 | ||
2373 | printk("{%s} state was registered at:\n", usage_str[prev_bit]); | |
f82b217e | 2374 | print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1); |
8e18257d PZ |
2375 | |
2376 | print_irqtrace_events(curr); | |
2377 | printk("\nother info that might help us debug this:\n"); | |
282b5c2f SR |
2378 | print_usage_bug_scenario(this); |
2379 | ||
8e18257d PZ |
2380 | lockdep_print_held_locks(curr); |
2381 | ||
2382 | printk("\nstack backtrace:\n"); | |
2383 | dump_stack(); | |
2384 | ||
2385 | return 0; | |
2386 | } | |
2387 | ||
2388 | /* | |
2389 | * Print out an error if an invalid bit is set: | |
2390 | */ | |
2391 | static inline int | |
2392 | valid_state(struct task_struct *curr, struct held_lock *this, | |
2393 | enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit) | |
2394 | { | |
f82b217e | 2395 | if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) |
8e18257d PZ |
2396 | return print_usage_bug(curr, this, bad_bit, new_bit); |
2397 | return 1; | |
2398 | } | |
2399 | ||
2400 | static int mark_lock(struct task_struct *curr, struct held_lock *this, | |
2401 | enum lock_usage_bit new_bit); | |
2402 | ||
81d68a96 | 2403 | #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) |
fbb9ce95 IM |
2404 | |
2405 | /* | |
2406 | * print irq inversion bug: | |
2407 | */ | |
2408 | static int | |
24208ca7 ML |
2409 | print_irq_inversion_bug(struct task_struct *curr, |
2410 | struct lock_list *root, struct lock_list *other, | |
fbb9ce95 IM |
2411 | struct held_lock *this, int forwards, |
2412 | const char *irqclass) | |
2413 | { | |
dad3d743 SR |
2414 | struct lock_list *entry = other; |
2415 | struct lock_list *middle = NULL; | |
2416 | int depth; | |
2417 | ||
74c383f1 | 2418 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
fbb9ce95 IM |
2419 | return 0; |
2420 | ||
b3fbab05 PM |
2421 | printk("\n"); |
2422 | printk("=========================================================\n"); | |
2423 | printk("[ INFO: possible irq lock inversion dependency detected ]\n"); | |
fbdc4b9a | 2424 | print_kernel_ident(); |
b3fbab05 | 2425 | printk("---------------------------------------------------------\n"); |
fbb9ce95 | 2426 | printk("%s/%d just changed the state of lock:\n", |
ba25f9dc | 2427 | curr->comm, task_pid_nr(curr)); |
fbb9ce95 IM |
2428 | print_lock(this); |
2429 | if (forwards) | |
26575e28 | 2430 | printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass); |
fbb9ce95 | 2431 | else |
26575e28 | 2432 | printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass); |
24208ca7 | 2433 | print_lock_name(other->class); |
fbb9ce95 IM |
2434 | printk("\n\nand interrupts could create inverse lock ordering between them.\n\n"); |
2435 | ||
2436 | printk("\nother info that might help us debug this:\n"); | |
dad3d743 SR |
2437 | |
2438 | /* Find a middle lock (if one exists) */ | |
2439 | depth = get_lock_depth(other); | |
2440 | do { | |
2441 | if (depth == 0 && (entry != root)) { | |
2442 | printk("lockdep:%s bad path found in chain graph\n", __func__); | |
2443 | break; | |
2444 | } | |
2445 | middle = entry; | |
2446 | entry = get_lock_parent(entry); | |
2447 | depth--; | |
2448 | } while (entry && entry != root && (depth >= 0)); | |
2449 | if (forwards) | |
2450 | print_irq_lock_scenario(root, other, | |
2451 | middle ? middle->class : root->class, other->class); | |
2452 | else | |
2453 | print_irq_lock_scenario(other, root, | |
2454 | middle ? middle->class : other->class, root->class); | |
2455 | ||
fbb9ce95 IM |
2456 | lockdep_print_held_locks(curr); |
2457 | ||
24208ca7 ML |
2458 | printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n"); |
2459 | if (!save_trace(&root->trace)) | |
2460 | return 0; | |
2461 | print_shortest_lock_dependencies(other, root); | |
fbb9ce95 IM |
2462 | |
2463 | printk("\nstack backtrace:\n"); | |
2464 | dump_stack(); | |
2465 | ||
2466 | return 0; | |
2467 | } | |
2468 | ||
2469 | /* | |
2470 | * Prove that in the forwards-direction subgraph starting at <this> | |
2471 | * there is no lock matching <mask>: | |
2472 | */ | |
2473 | static int | |
2474 | check_usage_forwards(struct task_struct *curr, struct held_lock *this, | |
2475 | enum lock_usage_bit bit, const char *irqclass) | |
2476 | { | |
2477 | int ret; | |
d7aaba14 ML |
2478 | struct lock_list root; |
2479 | struct lock_list *uninitialized_var(target_entry); | |
fbb9ce95 | 2480 | |
d7aaba14 ML |
2481 | root.parent = NULL; |
2482 | root.class = hlock_class(this); | |
2483 | ret = find_usage_forwards(&root, bit, &target_entry); | |
af012961 PZ |
2484 | if (ret < 0) |
2485 | return print_bfs_bug(ret); | |
2486 | if (ret == 1) | |
2487 | return ret; | |
fbb9ce95 | 2488 | |
24208ca7 | 2489 | return print_irq_inversion_bug(curr, &root, target_entry, |
d7aaba14 | 2490 | this, 1, irqclass); |
fbb9ce95 IM |
2491 | } |
2492 | ||
2493 | /* | |
2494 | * Prove that in the backwards-direction subgraph starting at <this> | |
2495 | * there is no lock matching <mask>: | |
2496 | */ | |
2497 | static int | |
2498 | check_usage_backwards(struct task_struct *curr, struct held_lock *this, | |
2499 | enum lock_usage_bit bit, const char *irqclass) | |
2500 | { | |
2501 | int ret; | |
d7aaba14 ML |
2502 | struct lock_list root; |
2503 | struct lock_list *uninitialized_var(target_entry); | |
fbb9ce95 | 2504 | |
d7aaba14 ML |
2505 | root.parent = NULL; |
2506 | root.class = hlock_class(this); | |
2507 | ret = find_usage_backwards(&root, bit, &target_entry); | |
af012961 PZ |
2508 | if (ret < 0) |
2509 | return print_bfs_bug(ret); | |
2510 | if (ret == 1) | |
2511 | return ret; | |
fbb9ce95 | 2512 | |
24208ca7 | 2513 | return print_irq_inversion_bug(curr, &root, target_entry, |
48d50674 | 2514 | this, 0, irqclass); |
fbb9ce95 IM |
2515 | } |
2516 | ||
3117df04 | 2517 | void print_irqtrace_events(struct task_struct *curr) |
fbb9ce95 IM |
2518 | { |
2519 | printk("irq event stamp: %u\n", curr->irq_events); | |
2520 | printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event); | |
2521 | print_ip_sym(curr->hardirq_enable_ip); | |
2522 | printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event); | |
2523 | print_ip_sym(curr->hardirq_disable_ip); | |
2524 | printk("softirqs last enabled at (%u): ", curr->softirq_enable_event); | |
2525 | print_ip_sym(curr->softirq_enable_ip); | |
2526 | printk("softirqs last disabled at (%u): ", curr->softirq_disable_event); | |
2527 | print_ip_sym(curr->softirq_disable_ip); | |
2528 | } | |
2529 | ||
cd95302d | 2530 | static int HARDIRQ_verbose(struct lock_class *class) |
fbb9ce95 | 2531 | { |
8e18257d PZ |
2532 | #if HARDIRQ_VERBOSE |
2533 | return class_filter(class); | |
2534 | #endif | |
fbb9ce95 IM |
2535 | return 0; |
2536 | } | |
2537 | ||
cd95302d | 2538 | static int SOFTIRQ_verbose(struct lock_class *class) |
fbb9ce95 | 2539 | { |
8e18257d PZ |
2540 | #if SOFTIRQ_VERBOSE |
2541 | return class_filter(class); | |
2542 | #endif | |
2543 | return 0; | |
fbb9ce95 IM |
2544 | } |
2545 | ||
cd95302d | 2546 | static int RECLAIM_FS_verbose(struct lock_class *class) |
cf40bd16 NP |
2547 | { |
2548 | #if RECLAIM_VERBOSE | |
2549 | return class_filter(class); | |
2550 | #endif | |
2551 | return 0; | |
2552 | } | |
2553 | ||
fbb9ce95 IM |
2554 | #define STRICT_READ_CHECKS 1 |
2555 | ||
cd95302d PZ |
2556 | static int (*state_verbose_f[])(struct lock_class *class) = { |
2557 | #define LOCKDEP_STATE(__STATE) \ | |
2558 | __STATE##_verbose, | |
2559 | #include "lockdep_states.h" | |
2560 | #undef LOCKDEP_STATE | |
2561 | }; | |
2562 | ||
2563 | static inline int state_verbose(enum lock_usage_bit bit, | |
2564 | struct lock_class *class) | |
2565 | { | |
2566 | return state_verbose_f[bit >> 2](class); | |
2567 | } | |
2568 | ||
42c50d54 PZ |
2569 | typedef int (*check_usage_f)(struct task_struct *, struct held_lock *, |
2570 | enum lock_usage_bit bit, const char *name); | |
2571 | ||
6a6904d3 | 2572 | static int |
1c21f14e PZ |
2573 | mark_lock_irq(struct task_struct *curr, struct held_lock *this, |
2574 | enum lock_usage_bit new_bit) | |
6a6904d3 | 2575 | { |
f989209e | 2576 | int excl_bit = exclusive_bit(new_bit); |
9d3651a2 | 2577 | int read = new_bit & 1; |
42c50d54 PZ |
2578 | int dir = new_bit & 2; |
2579 | ||
38aa2714 PZ |
2580 | /* |
2581 | * mark USED_IN has to look forwards -- to ensure no dependency | |
2582 | * has ENABLED state, which would allow recursion deadlocks. | |
2583 | * | |
2584 | * mark ENABLED has to look backwards -- to ensure no dependee | |
2585 | * has USED_IN state, which, again, would allow recursion deadlocks. | |
2586 | */ | |
42c50d54 PZ |
2587 | check_usage_f usage = dir ? |
2588 | check_usage_backwards : check_usage_forwards; | |
f989209e | 2589 | |
38aa2714 PZ |
2590 | /* |
2591 | * Validate that this particular lock does not have conflicting | |
2592 | * usage states. | |
2593 | */ | |
6a6904d3 PZ |
2594 | if (!valid_state(curr, this, new_bit, excl_bit)) |
2595 | return 0; | |
42c50d54 | 2596 | |
38aa2714 PZ |
2597 | /* |
2598 | * Validate that the lock dependencies don't have conflicting usage | |
2599 | * states. | |
2600 | */ | |
2601 | if ((!read || !dir || STRICT_READ_CHECKS) && | |
1c21f14e | 2602 | !usage(curr, this, excl_bit, state_name(new_bit & ~1))) |
6a6904d3 | 2603 | return 0; |
780e820b | 2604 | |
38aa2714 PZ |
2605 | /* |
2606 | * Check for read in write conflicts | |
2607 | */ | |
2608 | if (!read) { | |
2609 | if (!valid_state(curr, this, new_bit, excl_bit + 1)) | |
2610 | return 0; | |
2611 | ||
2612 | if (STRICT_READ_CHECKS && | |
4f367d8a PZ |
2613 | !usage(curr, this, excl_bit + 1, |
2614 | state_name(new_bit + 1))) | |
38aa2714 PZ |
2615 | return 0; |
2616 | } | |
780e820b | 2617 | |
cd95302d | 2618 | if (state_verbose(new_bit, hlock_class(this))) |
6a6904d3 PZ |
2619 | return 2; |
2620 | ||
2621 | return 1; | |
2622 | } | |
2623 | ||
cf40bd16 | 2624 | enum mark_type { |
36bfb9bb PZ |
2625 | #define LOCKDEP_STATE(__STATE) __STATE, |
2626 | #include "lockdep_states.h" | |
2627 | #undef LOCKDEP_STATE | |
cf40bd16 NP |
2628 | }; |
2629 | ||
fbb9ce95 IM |
2630 | /* |
2631 | * Mark all held locks with a usage bit: | |
2632 | */ | |
1d09daa5 | 2633 | static int |
cf40bd16 | 2634 | mark_held_locks(struct task_struct *curr, enum mark_type mark) |
fbb9ce95 IM |
2635 | { |
2636 | enum lock_usage_bit usage_bit; | |
2637 | struct held_lock *hlock; | |
2638 | int i; | |
2639 | ||
2640 | for (i = 0; i < curr->lockdep_depth; i++) { | |
2641 | hlock = curr->held_locks + i; | |
2642 | ||
cf2ad4d1 PZ |
2643 | usage_bit = 2 + (mark << 2); /* ENABLED */ |
2644 | if (hlock->read) | |
2645 | usage_bit += 1; /* READ */ | |
2646 | ||
2647 | BUG_ON(usage_bit >= LOCK_USAGE_STATES); | |
cf40bd16 | 2648 | |
34d0ed5e | 2649 | if (!hlock->check) |
efbe2eee PZ |
2650 | continue; |
2651 | ||
4ff773bb | 2652 | if (!mark_lock(curr, hlock, usage_bit)) |
fbb9ce95 IM |
2653 | return 0; |
2654 | } | |
2655 | ||
2656 | return 1; | |
2657 | } | |
2658 | ||
fbb9ce95 IM |
2659 | /* |
2660 | * Hardirqs will be enabled: | |
2661 | */ | |
dd4e5d3a | 2662 | static void __trace_hardirqs_on_caller(unsigned long ip) |
fbb9ce95 IM |
2663 | { |
2664 | struct task_struct *curr = current; | |
fbb9ce95 | 2665 | |
fbb9ce95 IM |
2666 | /* we'll do an OFF -> ON transition: */ |
2667 | curr->hardirqs_enabled = 1; | |
fbb9ce95 | 2668 | |
fbb9ce95 IM |
2669 | /* |
2670 | * We are going to turn hardirqs on, so set the | |
2671 | * usage bit for all held locks: | |
2672 | */ | |
cf40bd16 | 2673 | if (!mark_held_locks(curr, HARDIRQ)) |
fbb9ce95 IM |
2674 | return; |
2675 | /* | |
2676 | * If we have softirqs enabled, then set the usage | |
2677 | * bit for all held locks. (disabled hardirqs prevented | |
2678 | * this bit from being set before) | |
2679 | */ | |
2680 | if (curr->softirqs_enabled) | |
cf40bd16 | 2681 | if (!mark_held_locks(curr, SOFTIRQ)) |
fbb9ce95 IM |
2682 | return; |
2683 | ||
8e18257d PZ |
2684 | curr->hardirq_enable_ip = ip; |
2685 | curr->hardirq_enable_event = ++curr->irq_events; | |
bd6d29c2 | 2686 | debug_atomic_inc(hardirqs_on_events); |
8e18257d | 2687 | } |
dd4e5d3a | 2688 | |
b35f8305 | 2689 | __visible void trace_hardirqs_on_caller(unsigned long ip) |
dd4e5d3a PZ |
2690 | { |
2691 | time_hardirqs_on(CALLER_ADDR0, ip); | |
2692 | ||
2693 | if (unlikely(!debug_locks || current->lockdep_recursion)) | |
2694 | return; | |
2695 | ||
7d36b26b PZ |
2696 | if (unlikely(current->hardirqs_enabled)) { |
2697 | /* | |
2698 | * Neither irq nor preemption are disabled here | |
2699 | * so this is racy by nature but losing one hit | |
2700 | * in a stat is not a big deal. | |
2701 | */ | |
2702 | __debug_atomic_inc(redundant_hardirqs_on); | |
2703 | return; | |
2704 | } | |
2705 | ||
0119fee4 PZ |
2706 | /* |
2707 | * We're enabling irqs and according to our state above irqs weren't | |
2708 | * already enabled, yet we find the hardware thinks they are in fact | |
2709 | * enabled.. someone messed up their IRQ state tracing. | |
2710 | */ | |
dd4e5d3a PZ |
2711 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
2712 | return; | |
2713 | ||
0119fee4 PZ |
2714 | /* |
2715 | * See the fine text that goes along with this variable definition. | |
2716 | */ | |
7d36b26b PZ |
2717 | if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled))) |
2718 | return; | |
2719 | ||
0119fee4 PZ |
2720 | /* |
2721 | * Can't allow enabling interrupts while in an interrupt handler, | |
2722 | * that's general bad form and such. Recursion, limited stack etc.. | |
2723 | */ | |
7d36b26b PZ |
2724 | if (DEBUG_LOCKS_WARN_ON(current->hardirq_context)) |
2725 | return; | |
2726 | ||
dd4e5d3a PZ |
2727 | current->lockdep_recursion = 1; |
2728 | __trace_hardirqs_on_caller(ip); | |
2729 | current->lockdep_recursion = 0; | |
2730 | } | |
81d68a96 | 2731 | EXPORT_SYMBOL(trace_hardirqs_on_caller); |
8e18257d | 2732 | |
1d09daa5 | 2733 | void trace_hardirqs_on(void) |
81d68a96 SR |
2734 | { |
2735 | trace_hardirqs_on_caller(CALLER_ADDR0); | |
2736 | } | |
8e18257d PZ |
2737 | EXPORT_SYMBOL(trace_hardirqs_on); |
2738 | ||
2739 | /* | |
2740 | * Hardirqs were disabled: | |
2741 | */ | |
b35f8305 | 2742 | __visible void trace_hardirqs_off_caller(unsigned long ip) |
8e18257d PZ |
2743 | { |
2744 | struct task_struct *curr = current; | |
2745 | ||
6afe40b4 | 2746 | time_hardirqs_off(CALLER_ADDR0, ip); |
81d68a96 | 2747 | |
8e18257d PZ |
2748 | if (unlikely(!debug_locks || current->lockdep_recursion)) |
2749 | return; | |
2750 | ||
0119fee4 PZ |
2751 | /* |
2752 | * So we're supposed to get called after you mask local IRQs, but for | |
2753 | * some reason the hardware doesn't quite think you did a proper job. | |
2754 | */ | |
8e18257d PZ |
2755 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
2756 | return; | |
2757 | ||
2758 | if (curr->hardirqs_enabled) { | |
2759 | /* | |
2760 | * We have done an ON -> OFF transition: | |
2761 | */ | |
2762 | curr->hardirqs_enabled = 0; | |
6afe40b4 | 2763 | curr->hardirq_disable_ip = ip; |
8e18257d | 2764 | curr->hardirq_disable_event = ++curr->irq_events; |
bd6d29c2 | 2765 | debug_atomic_inc(hardirqs_off_events); |
8e18257d | 2766 | } else |
bd6d29c2 | 2767 | debug_atomic_inc(redundant_hardirqs_off); |
8e18257d | 2768 | } |
81d68a96 | 2769 | EXPORT_SYMBOL(trace_hardirqs_off_caller); |
8e18257d | 2770 | |
1d09daa5 | 2771 | void trace_hardirqs_off(void) |
81d68a96 SR |
2772 | { |
2773 | trace_hardirqs_off_caller(CALLER_ADDR0); | |
2774 | } | |
8e18257d PZ |
2775 | EXPORT_SYMBOL(trace_hardirqs_off); |
2776 | ||
2777 | /* | |
2778 | * Softirqs will be enabled: | |
2779 | */ | |
2780 | void trace_softirqs_on(unsigned long ip) | |
2781 | { | |
2782 | struct task_struct *curr = current; | |
2783 | ||
dd4e5d3a | 2784 | if (unlikely(!debug_locks || current->lockdep_recursion)) |
8e18257d PZ |
2785 | return; |
2786 | ||
0119fee4 PZ |
2787 | /* |
2788 | * We fancy IRQs being disabled here, see softirq.c, avoids | |
2789 | * funny state and nesting things. | |
2790 | */ | |
8e18257d PZ |
2791 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
2792 | return; | |
2793 | ||
2794 | if (curr->softirqs_enabled) { | |
bd6d29c2 | 2795 | debug_atomic_inc(redundant_softirqs_on); |
8e18257d PZ |
2796 | return; |
2797 | } | |
2798 | ||
dd4e5d3a | 2799 | current->lockdep_recursion = 1; |
8e18257d PZ |
2800 | /* |
2801 | * We'll do an OFF -> ON transition: | |
2802 | */ | |
2803 | curr->softirqs_enabled = 1; | |
2804 | curr->softirq_enable_ip = ip; | |
2805 | curr->softirq_enable_event = ++curr->irq_events; | |
bd6d29c2 | 2806 | debug_atomic_inc(softirqs_on_events); |
8e18257d PZ |
2807 | /* |
2808 | * We are going to turn softirqs on, so set the | |
2809 | * usage bit for all held locks, if hardirqs are | |
2810 | * enabled too: | |
2811 | */ | |
2812 | if (curr->hardirqs_enabled) | |
cf40bd16 | 2813 | mark_held_locks(curr, SOFTIRQ); |
dd4e5d3a | 2814 | current->lockdep_recursion = 0; |
8e18257d PZ |
2815 | } |
2816 | ||
2817 | /* | |
2818 | * Softirqs were disabled: | |
2819 | */ | |
2820 | void trace_softirqs_off(unsigned long ip) | |
2821 | { | |
2822 | struct task_struct *curr = current; | |
2823 | ||
dd4e5d3a | 2824 | if (unlikely(!debug_locks || current->lockdep_recursion)) |
8e18257d PZ |
2825 | return; |
2826 | ||
0119fee4 PZ |
2827 | /* |
2828 | * We fancy IRQs being disabled here, see softirq.c | |
2829 | */ | |
8e18257d PZ |
2830 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
2831 | return; | |
2832 | ||
2833 | if (curr->softirqs_enabled) { | |
2834 | /* | |
2835 | * We have done an ON -> OFF transition: | |
2836 | */ | |
2837 | curr->softirqs_enabled = 0; | |
2838 | curr->softirq_disable_ip = ip; | |
2839 | curr->softirq_disable_event = ++curr->irq_events; | |
bd6d29c2 | 2840 | debug_atomic_inc(softirqs_off_events); |
0119fee4 PZ |
2841 | /* |
2842 | * Whoops, we wanted softirqs off, so why aren't they? | |
2843 | */ | |
8e18257d PZ |
2844 | DEBUG_LOCKS_WARN_ON(!softirq_count()); |
2845 | } else | |
bd6d29c2 | 2846 | debug_atomic_inc(redundant_softirqs_off); |
8e18257d PZ |
2847 | } |
2848 | ||
2f850181 | 2849 | static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags) |
cf40bd16 NP |
2850 | { |
2851 | struct task_struct *curr = current; | |
2852 | ||
2853 | if (unlikely(!debug_locks)) | |
2854 | return; | |
2855 | ||
2856 | /* no reclaim without waiting on it */ | |
d0164adc | 2857 | if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) |
cf40bd16 NP |
2858 | return; |
2859 | ||
2860 | /* this guy won't enter reclaim */ | |
2861 | if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC)) | |
2862 | return; | |
2863 | ||
2864 | /* We're only interested __GFP_FS allocations for now */ | |
2865 | if (!(gfp_mask & __GFP_FS)) | |
2866 | return; | |
2867 | ||
0119fee4 PZ |
2868 | /* |
2869 | * Oi! Can't be having __GFP_FS allocations with IRQs disabled. | |
2870 | */ | |
2f850181 | 2871 | if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags))) |
cf40bd16 NP |
2872 | return; |
2873 | ||
2874 | mark_held_locks(curr, RECLAIM_FS); | |
2875 | } | |
2876 | ||
2f850181 PZ |
2877 | static void check_flags(unsigned long flags); |
2878 | ||
2879 | void lockdep_trace_alloc(gfp_t gfp_mask) | |
2880 | { | |
2881 | unsigned long flags; | |
2882 | ||
2883 | if (unlikely(current->lockdep_recursion)) | |
2884 | return; | |
2885 | ||
2886 | raw_local_irq_save(flags); | |
2887 | check_flags(flags); | |
2888 | current->lockdep_recursion = 1; | |
2889 | __lockdep_trace_alloc(gfp_mask, flags); | |
2890 | current->lockdep_recursion = 0; | |
2891 | raw_local_irq_restore(flags); | |
2892 | } | |
2893 | ||
8e18257d PZ |
2894 | static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock) |
2895 | { | |
2896 | /* | |
2897 | * If non-trylock use in a hardirq or softirq context, then | |
2898 | * mark the lock as used in these contexts: | |
2899 | */ | |
2900 | if (!hlock->trylock) { | |
2901 | if (hlock->read) { | |
2902 | if (curr->hardirq_context) | |
2903 | if (!mark_lock(curr, hlock, | |
2904 | LOCK_USED_IN_HARDIRQ_READ)) | |
2905 | return 0; | |
2906 | if (curr->softirq_context) | |
2907 | if (!mark_lock(curr, hlock, | |
2908 | LOCK_USED_IN_SOFTIRQ_READ)) | |
2909 | return 0; | |
2910 | } else { | |
2911 | if (curr->hardirq_context) | |
2912 | if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ)) | |
2913 | return 0; | |
2914 | if (curr->softirq_context) | |
2915 | if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ)) | |
2916 | return 0; | |
2917 | } | |
2918 | } | |
2919 | if (!hlock->hardirqs_off) { | |
2920 | if (hlock->read) { | |
2921 | if (!mark_lock(curr, hlock, | |
4fc95e86 | 2922 | LOCK_ENABLED_HARDIRQ_READ)) |
8e18257d PZ |
2923 | return 0; |
2924 | if (curr->softirqs_enabled) | |
2925 | if (!mark_lock(curr, hlock, | |
4fc95e86 | 2926 | LOCK_ENABLED_SOFTIRQ_READ)) |
8e18257d PZ |
2927 | return 0; |
2928 | } else { | |
2929 | if (!mark_lock(curr, hlock, | |
4fc95e86 | 2930 | LOCK_ENABLED_HARDIRQ)) |
8e18257d PZ |
2931 | return 0; |
2932 | if (curr->softirqs_enabled) | |
2933 | if (!mark_lock(curr, hlock, | |
4fc95e86 | 2934 | LOCK_ENABLED_SOFTIRQ)) |
8e18257d PZ |
2935 | return 0; |
2936 | } | |
2937 | } | |
2938 | ||
cf40bd16 NP |
2939 | /* |
2940 | * We reuse the irq context infrastructure more broadly as a general | |
2941 | * context checking code. This tests GFP_FS recursion (a lock taken | |
2942 | * during reclaim for a GFP_FS allocation is held over a GFP_FS | |
2943 | * allocation). | |
2944 | */ | |
2945 | if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) { | |
2946 | if (hlock->read) { | |
2947 | if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ)) | |
2948 | return 0; | |
2949 | } else { | |
2950 | if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS)) | |
2951 | return 0; | |
2952 | } | |
2953 | } | |
2954 | ||
8e18257d PZ |
2955 | return 1; |
2956 | } | |
2957 | ||
c2469756 BF |
2958 | static inline unsigned int task_irq_context(struct task_struct *task) |
2959 | { | |
2960 | return 2 * !!task->hardirq_context + !!task->softirq_context; | |
2961 | } | |
2962 | ||
8e18257d PZ |
2963 | static int separate_irq_context(struct task_struct *curr, |
2964 | struct held_lock *hlock) | |
2965 | { | |
2966 | unsigned int depth = curr->lockdep_depth; | |
2967 | ||
2968 | /* | |
2969 | * Keep track of points where we cross into an interrupt context: | |
2970 | */ | |
8e18257d PZ |
2971 | if (depth) { |
2972 | struct held_lock *prev_hlock; | |
2973 | ||
2974 | prev_hlock = curr->held_locks + depth-1; | |
2975 | /* | |
2976 | * If we cross into another context, reset the | |
2977 | * hash key (this also prevents the checking and the | |
2978 | * adding of the dependency to 'prev'): | |
2979 | */ | |
2980 | if (prev_hlock->irq_context != hlock->irq_context) | |
2981 | return 1; | |
2982 | } | |
2983 | return 0; | |
fbb9ce95 IM |
2984 | } |
2985 | ||
0119fee4 | 2986 | #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */ |
fbb9ce95 | 2987 | |
8e18257d PZ |
2988 | static inline |
2989 | int mark_lock_irq(struct task_struct *curr, struct held_lock *this, | |
2990 | enum lock_usage_bit new_bit) | |
fbb9ce95 | 2991 | { |
0119fee4 | 2992 | WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */ |
8e18257d PZ |
2993 | return 1; |
2994 | } | |
fbb9ce95 | 2995 | |
8e18257d PZ |
2996 | static inline int mark_irqflags(struct task_struct *curr, |
2997 | struct held_lock *hlock) | |
2998 | { | |
2999 | return 1; | |
3000 | } | |
fbb9ce95 | 3001 | |
c2469756 BF |
3002 | static inline unsigned int task_irq_context(struct task_struct *task) |
3003 | { | |
3004 | return 0; | |
3005 | } | |
3006 | ||
8e18257d PZ |
3007 | static inline int separate_irq_context(struct task_struct *curr, |
3008 | struct held_lock *hlock) | |
3009 | { | |
3010 | return 0; | |
fbb9ce95 IM |
3011 | } |
3012 | ||
868a23a8 PZ |
3013 | void lockdep_trace_alloc(gfp_t gfp_mask) |
3014 | { | |
3015 | } | |
3016 | ||
0119fee4 | 3017 | #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */ |
fbb9ce95 IM |
3018 | |
3019 | /* | |
8e18257d | 3020 | * Mark a lock with a usage bit, and validate the state transition: |
fbb9ce95 | 3021 | */ |
1d09daa5 | 3022 | static int mark_lock(struct task_struct *curr, struct held_lock *this, |
0764d23c | 3023 | enum lock_usage_bit new_bit) |
fbb9ce95 | 3024 | { |
8e18257d | 3025 | unsigned int new_mask = 1 << new_bit, ret = 1; |
fbb9ce95 IM |
3026 | |
3027 | /* | |
8e18257d PZ |
3028 | * If already set then do not dirty the cacheline, |
3029 | * nor do any checks: | |
fbb9ce95 | 3030 | */ |
f82b217e | 3031 | if (likely(hlock_class(this)->usage_mask & new_mask)) |
8e18257d PZ |
3032 | return 1; |
3033 | ||
3034 | if (!graph_lock()) | |
3035 | return 0; | |
fbb9ce95 | 3036 | /* |
25985edc | 3037 | * Make sure we didn't race: |
fbb9ce95 | 3038 | */ |
f82b217e | 3039 | if (unlikely(hlock_class(this)->usage_mask & new_mask)) { |
8e18257d PZ |
3040 | graph_unlock(); |
3041 | return 1; | |
3042 | } | |
fbb9ce95 | 3043 | |
f82b217e | 3044 | hlock_class(this)->usage_mask |= new_mask; |
fbb9ce95 | 3045 | |
f82b217e | 3046 | if (!save_trace(hlock_class(this)->usage_traces + new_bit)) |
8e18257d | 3047 | return 0; |
fbb9ce95 | 3048 | |
8e18257d | 3049 | switch (new_bit) { |
5346417e PZ |
3050 | #define LOCKDEP_STATE(__STATE) \ |
3051 | case LOCK_USED_IN_##__STATE: \ | |
3052 | case LOCK_USED_IN_##__STATE##_READ: \ | |
3053 | case LOCK_ENABLED_##__STATE: \ | |
3054 | case LOCK_ENABLED_##__STATE##_READ: | |
3055 | #include "lockdep_states.h" | |
3056 | #undef LOCKDEP_STATE | |
8e18257d PZ |
3057 | ret = mark_lock_irq(curr, this, new_bit); |
3058 | if (!ret) | |
3059 | return 0; | |
3060 | break; | |
3061 | case LOCK_USED: | |
bd6d29c2 | 3062 | debug_atomic_dec(nr_unused_locks); |
8e18257d PZ |
3063 | break; |
3064 | default: | |
3065 | if (!debug_locks_off_graph_unlock()) | |
3066 | return 0; | |
3067 | WARN_ON(1); | |
3068 | return 0; | |
3069 | } | |
fbb9ce95 | 3070 | |
8e18257d PZ |
3071 | graph_unlock(); |
3072 | ||
3073 | /* | |
3074 | * We must printk outside of the graph_lock: | |
3075 | */ | |
3076 | if (ret == 2) { | |
3077 | printk("\nmarked lock as {%s}:\n", usage_str[new_bit]); | |
3078 | print_lock(this); | |
3079 | print_irqtrace_events(curr); | |
3080 | dump_stack(); | |
3081 | } | |
3082 | ||
3083 | return ret; | |
3084 | } | |
fbb9ce95 IM |
3085 | |
3086 | /* | |
3087 | * Initialize a lock instance's lock-class mapping info: | |
3088 | */ | |
3089 | void lockdep_init_map(struct lockdep_map *lock, const char *name, | |
4dfbb9d8 | 3090 | struct lock_class_key *key, int subclass) |
fbb9ce95 | 3091 | { |
d3d03d4f YZ |
3092 | int i; |
3093 | ||
3094 | kmemcheck_mark_initialized(lock, sizeof(*lock)); | |
3095 | ||
3096 | for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++) | |
3097 | lock->class_cache[i] = NULL; | |
62016250 | 3098 | |
c8a25005 PZ |
3099 | #ifdef CONFIG_LOCK_STAT |
3100 | lock->cpu = raw_smp_processor_id(); | |
3101 | #endif | |
3102 | ||
0119fee4 PZ |
3103 | /* |
3104 | * Can't be having no nameless bastards around this place! | |
3105 | */ | |
c8a25005 PZ |
3106 | if (DEBUG_LOCKS_WARN_ON(!name)) { |
3107 | lock->name = "NULL"; | |
fbb9ce95 | 3108 | return; |
c8a25005 PZ |
3109 | } |
3110 | ||
3111 | lock->name = name; | |
fbb9ce95 | 3112 | |
0119fee4 PZ |
3113 | /* |
3114 | * No key, no joy, we need to hash something. | |
3115 | */ | |
fbb9ce95 IM |
3116 | if (DEBUG_LOCKS_WARN_ON(!key)) |
3117 | return; | |
fbb9ce95 IM |
3118 | /* |
3119 | * Sanity check, the lock-class key must be persistent: | |
3120 | */ | |
3121 | if (!static_obj(key)) { | |
3122 | printk("BUG: key %p not in .data!\n", key); | |
0119fee4 PZ |
3123 | /* |
3124 | * What it says above ^^^^^, I suggest you read it. | |
3125 | */ | |
fbb9ce95 IM |
3126 | DEBUG_LOCKS_WARN_ON(1); |
3127 | return; | |
3128 | } | |
fbb9ce95 | 3129 | lock->key = key; |
c8a25005 PZ |
3130 | |
3131 | if (unlikely(!debug_locks)) | |
3132 | return; | |
3133 | ||
35a9393c PZ |
3134 | if (subclass) { |
3135 | unsigned long flags; | |
3136 | ||
3137 | if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion)) | |
3138 | return; | |
3139 | ||
3140 | raw_local_irq_save(flags); | |
3141 | current->lockdep_recursion = 1; | |
4dfbb9d8 | 3142 | register_lock_class(lock, subclass, 1); |
35a9393c PZ |
3143 | current->lockdep_recursion = 0; |
3144 | raw_local_irq_restore(flags); | |
3145 | } | |
fbb9ce95 | 3146 | } |
fbb9ce95 IM |
3147 | EXPORT_SYMBOL_GPL(lockdep_init_map); |
3148 | ||
1704f47b | 3149 | struct lock_class_key __lockdep_no_validate__; |
ea6749c7 | 3150 | EXPORT_SYMBOL_GPL(__lockdep_no_validate__); |
1704f47b | 3151 | |
d0945950 ML |
3152 | static int |
3153 | print_lock_nested_lock_not_held(struct task_struct *curr, | |
3154 | struct held_lock *hlock, | |
3155 | unsigned long ip) | |
3156 | { | |
3157 | if (!debug_locks_off()) | |
3158 | return 0; | |
3159 | if (debug_locks_silent) | |
3160 | return 0; | |
3161 | ||
3162 | printk("\n"); | |
3163 | printk("==================================\n"); | |
3164 | printk("[ BUG: Nested lock was not taken ]\n"); | |
3165 | print_kernel_ident(); | |
3166 | printk("----------------------------------\n"); | |
3167 | ||
3168 | printk("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr)); | |
3169 | print_lock(hlock); | |
3170 | ||
3171 | printk("\nbut this task is not holding:\n"); | |
3172 | printk("%s\n", hlock->nest_lock->name); | |
3173 | ||
3174 | printk("\nstack backtrace:\n"); | |
3175 | dump_stack(); | |
3176 | ||
3177 | printk("\nother info that might help us debug this:\n"); | |
3178 | lockdep_print_held_locks(curr); | |
3179 | ||
3180 | printk("\nstack backtrace:\n"); | |
3181 | dump_stack(); | |
3182 | ||
3183 | return 0; | |
3184 | } | |
3185 | ||
3186 | static int __lock_is_held(struct lockdep_map *lock); | |
3187 | ||
fbb9ce95 IM |
3188 | /* |
3189 | * This gets called for every mutex_lock*()/spin_lock*() operation. | |
3190 | * We maintain the dependency maps and validate the locking attempt: | |
3191 | */ | |
3192 | static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, | |
3193 | int trylock, int read, int check, int hardirqs_off, | |
bb97a91e | 3194 | struct lockdep_map *nest_lock, unsigned long ip, |
21199f27 | 3195 | int references, int pin_count) |
fbb9ce95 IM |
3196 | { |
3197 | struct task_struct *curr = current; | |
d6d897ce | 3198 | struct lock_class *class = NULL; |
fbb9ce95 | 3199 | struct held_lock *hlock; |
5f18ab5c | 3200 | unsigned int depth; |
fbb9ce95 | 3201 | int chain_head = 0; |
bb97a91e | 3202 | int class_idx; |
fbb9ce95 IM |
3203 | u64 chain_key; |
3204 | ||
3205 | if (unlikely(!debug_locks)) | |
3206 | return 0; | |
3207 | ||
0119fee4 PZ |
3208 | /* |
3209 | * Lockdep should run with IRQs disabled, otherwise we could | |
3210 | * get an interrupt which would want to take locks, which would | |
3211 | * end up in lockdep and have you got a head-ache already? | |
3212 | */ | |
fbb9ce95 IM |
3213 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
3214 | return 0; | |
3215 | ||
fb9edbe9 ON |
3216 | if (!prove_locking || lock->key == &__lockdep_no_validate__) |
3217 | check = 0; | |
1704f47b | 3218 | |
62016250 HM |
3219 | if (subclass < NR_LOCKDEP_CACHING_CLASSES) |
3220 | class = lock->class_cache[subclass]; | |
d6d897ce | 3221 | /* |
62016250 | 3222 | * Not cached? |
d6d897ce | 3223 | */ |
fbb9ce95 | 3224 | if (unlikely(!class)) { |
4dfbb9d8 | 3225 | class = register_lock_class(lock, subclass, 0); |
fbb9ce95 IM |
3226 | if (!class) |
3227 | return 0; | |
3228 | } | |
bd6d29c2 | 3229 | atomic_inc((atomic_t *)&class->ops); |
fbb9ce95 IM |
3230 | if (very_verbose(class)) { |
3231 | printk("\nacquire class [%p] %s", class->key, class->name); | |
3232 | if (class->name_version > 1) | |
3233 | printk("#%d", class->name_version); | |
3234 | printk("\n"); | |
3235 | dump_stack(); | |
3236 | } | |
3237 | ||
3238 | /* | |
3239 | * Add the lock to the list of currently held locks. | |
3240 | * (we dont increase the depth just yet, up until the | |
3241 | * dependency checks are done) | |
3242 | */ | |
3243 | depth = curr->lockdep_depth; | |
0119fee4 PZ |
3244 | /* |
3245 | * Ran out of static storage for our per-task lock stack again have we? | |
3246 | */ | |
fbb9ce95 IM |
3247 | if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH)) |
3248 | return 0; | |
3249 | ||
bb97a91e PZ |
3250 | class_idx = class - lock_classes + 1; |
3251 | ||
3252 | if (depth) { | |
3253 | hlock = curr->held_locks + depth - 1; | |
3254 | if (hlock->class_idx == class_idx && nest_lock) { | |
3255 | if (hlock->references) | |
3256 | hlock->references++; | |
3257 | else | |
3258 | hlock->references = 2; | |
3259 | ||
3260 | return 1; | |
3261 | } | |
3262 | } | |
3263 | ||
fbb9ce95 | 3264 | hlock = curr->held_locks + depth; |
0119fee4 PZ |
3265 | /* |
3266 | * Plain impossible, we just registered it and checked it weren't no | |
3267 | * NULL like.. I bet this mushroom I ate was good! | |
3268 | */ | |
f82b217e DJ |
3269 | if (DEBUG_LOCKS_WARN_ON(!class)) |
3270 | return 0; | |
bb97a91e | 3271 | hlock->class_idx = class_idx; |
fbb9ce95 IM |
3272 | hlock->acquire_ip = ip; |
3273 | hlock->instance = lock; | |
7531e2f3 | 3274 | hlock->nest_lock = nest_lock; |
c2469756 | 3275 | hlock->irq_context = task_irq_context(curr); |
fbb9ce95 IM |
3276 | hlock->trylock = trylock; |
3277 | hlock->read = read; | |
3278 | hlock->check = check; | |
6951b12a | 3279 | hlock->hardirqs_off = !!hardirqs_off; |
bb97a91e | 3280 | hlock->references = references; |
f20786ff PZ |
3281 | #ifdef CONFIG_LOCK_STAT |
3282 | hlock->waittime_stamp = 0; | |
3365e779 | 3283 | hlock->holdtime_stamp = lockstat_clock(); |
f20786ff | 3284 | #endif |
21199f27 | 3285 | hlock->pin_count = pin_count; |
fbb9ce95 | 3286 | |
fb9edbe9 | 3287 | if (check && !mark_irqflags(curr, hlock)) |
8e18257d PZ |
3288 | return 0; |
3289 | ||
fbb9ce95 | 3290 | /* mark it as used: */ |
4ff773bb | 3291 | if (!mark_lock(curr, hlock, LOCK_USED)) |
fbb9ce95 | 3292 | return 0; |
8e18257d | 3293 | |
fbb9ce95 | 3294 | /* |
17aacfb9 | 3295 | * Calculate the chain hash: it's the combined hash of all the |
fbb9ce95 IM |
3296 | * lock keys along the dependency chain. We save the hash value |
3297 | * at every step so that we can get the current hash easily | |
3298 | * after unlock. The chain hash is then used to cache dependency | |
3299 | * results. | |
3300 | * | |
3301 | * The 'key ID' is what is the most compact key value to drive | |
3302 | * the hash, not class->key. | |
3303 | */ | |
0119fee4 PZ |
3304 | /* |
3305 | * Whoops, we did it again.. ran straight out of our static allocation. | |
3306 | */ | |
5f18ab5c | 3307 | if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS)) |
fbb9ce95 IM |
3308 | return 0; |
3309 | ||
3310 | chain_key = curr->curr_chain_key; | |
3311 | if (!depth) { | |
0119fee4 PZ |
3312 | /* |
3313 | * How can we have a chain hash when we ain't got no keys?! | |
3314 | */ | |
fbb9ce95 IM |
3315 | if (DEBUG_LOCKS_WARN_ON(chain_key != 0)) |
3316 | return 0; | |
3317 | chain_head = 1; | |
3318 | } | |
3319 | ||
3320 | hlock->prev_chain_key = chain_key; | |
8e18257d PZ |
3321 | if (separate_irq_context(curr, hlock)) { |
3322 | chain_key = 0; | |
3323 | chain_head = 1; | |
fbb9ce95 | 3324 | } |
5f18ab5c | 3325 | chain_key = iterate_chain_key(chain_key, class_idx); |
fbb9ce95 | 3326 | |
d0945950 ML |
3327 | if (nest_lock && !__lock_is_held(nest_lock)) |
3328 | return print_lock_nested_lock_not_held(curr, hlock, ip); | |
3329 | ||
3aa416b0 | 3330 | if (!validate_chain(curr, lock, hlock, chain_head, chain_key)) |
8e18257d | 3331 | return 0; |
381a2292 | 3332 | |
3aa416b0 | 3333 | curr->curr_chain_key = chain_key; |
fbb9ce95 IM |
3334 | curr->lockdep_depth++; |
3335 | check_chain_key(curr); | |
60e114d1 JP |
3336 | #ifdef CONFIG_DEBUG_LOCKDEP |
3337 | if (unlikely(!debug_locks)) | |
3338 | return 0; | |
3339 | #endif | |
fbb9ce95 IM |
3340 | if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) { |
3341 | debug_locks_off(); | |
2c522836 DJ |
3342 | print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!"); |
3343 | printk(KERN_DEBUG "depth: %i max: %lu!\n", | |
c0540606 | 3344 | curr->lockdep_depth, MAX_LOCK_DEPTH); |
c0540606 BG |
3345 | |
3346 | lockdep_print_held_locks(current); | |
3347 | debug_show_all_locks(); | |
eedeeabd | 3348 | dump_stack(); |
c0540606 | 3349 | |
fbb9ce95 IM |
3350 | return 0; |
3351 | } | |
381a2292 | 3352 | |
fbb9ce95 IM |
3353 | if (unlikely(curr->lockdep_depth > max_lockdep_depth)) |
3354 | max_lockdep_depth = curr->lockdep_depth; | |
3355 | ||
3356 | return 1; | |
3357 | } | |
3358 | ||
3359 | static int | |
f86f7554 | 3360 | print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock, |
fbb9ce95 IM |
3361 | unsigned long ip) |
3362 | { | |
3363 | if (!debug_locks_off()) | |
3364 | return 0; | |
3365 | if (debug_locks_silent) | |
3366 | return 0; | |
3367 | ||
b3fbab05 PM |
3368 | printk("\n"); |
3369 | printk("=====================================\n"); | |
3370 | printk("[ BUG: bad unlock balance detected! ]\n"); | |
fbdc4b9a | 3371 | print_kernel_ident(); |
b3fbab05 | 3372 | printk("-------------------------------------\n"); |
fbb9ce95 | 3373 | printk("%s/%d is trying to release lock (", |
ba25f9dc | 3374 | curr->comm, task_pid_nr(curr)); |
fbb9ce95 IM |
3375 | print_lockdep_cache(lock); |
3376 | printk(") at:\n"); | |
3377 | print_ip_sym(ip); | |
3378 | printk("but there are no more locks to release!\n"); | |
3379 | printk("\nother info that might help us debug this:\n"); | |
3380 | lockdep_print_held_locks(curr); | |
3381 | ||
3382 | printk("\nstack backtrace:\n"); | |
3383 | dump_stack(); | |
3384 | ||
3385 | return 0; | |
3386 | } | |
3387 | ||
bb97a91e PZ |
3388 | static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock) |
3389 | { | |
3390 | if (hlock->instance == lock) | |
3391 | return 1; | |
3392 | ||
3393 | if (hlock->references) { | |
62016250 | 3394 | struct lock_class *class = lock->class_cache[0]; |
bb97a91e PZ |
3395 | |
3396 | if (!class) | |
3397 | class = look_up_lock_class(lock, 0); | |
3398 | ||
80e0401e PZ |
3399 | /* |
3400 | * If look_up_lock_class() failed to find a class, we're trying | |
3401 | * to test if we hold a lock that has never yet been acquired. | |
3402 | * Clearly if the lock hasn't been acquired _ever_, we're not | |
3403 | * holding it either, so report failure. | |
3404 | */ | |
3405 | if (!class) | |
bb97a91e PZ |
3406 | return 0; |
3407 | ||
0119fee4 PZ |
3408 | /* |
3409 | * References, but not a lock we're actually ref-counting? | |
3410 | * State got messed up, follow the sites that change ->references | |
3411 | * and try to make sense of it. | |
3412 | */ | |
bb97a91e PZ |
3413 | if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock)) |
3414 | return 0; | |
3415 | ||
3416 | if (hlock->class_idx == class - lock_classes + 1) | |
3417 | return 1; | |
3418 | } | |
3419 | ||
3420 | return 0; | |
3421 | } | |
3422 | ||
64aa348e | 3423 | static int |
00ef9f73 PZ |
3424 | __lock_set_class(struct lockdep_map *lock, const char *name, |
3425 | struct lock_class_key *key, unsigned int subclass, | |
3426 | unsigned long ip) | |
64aa348e PZ |
3427 | { |
3428 | struct task_struct *curr = current; | |
3429 | struct held_lock *hlock, *prev_hlock; | |
3430 | struct lock_class *class; | |
3431 | unsigned int depth; | |
3432 | int i; | |
3433 | ||
3434 | depth = curr->lockdep_depth; | |
0119fee4 PZ |
3435 | /* |
3436 | * This function is about (re)setting the class of a held lock, | |
3437 | * yet we're not actually holding any locks. Naughty user! | |
3438 | */ | |
64aa348e PZ |
3439 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
3440 | return 0; | |
3441 | ||
3442 | prev_hlock = NULL; | |
3443 | for (i = depth-1; i >= 0; i--) { | |
3444 | hlock = curr->held_locks + i; | |
3445 | /* | |
3446 | * We must not cross into another context: | |
3447 | */ | |
3448 | if (prev_hlock && prev_hlock->irq_context != hlock->irq_context) | |
3449 | break; | |
bb97a91e | 3450 | if (match_held_lock(hlock, lock)) |
64aa348e PZ |
3451 | goto found_it; |
3452 | prev_hlock = hlock; | |
3453 | } | |
f86f7554 | 3454 | return print_unlock_imbalance_bug(curr, lock, ip); |
64aa348e PZ |
3455 | |
3456 | found_it: | |
00ef9f73 | 3457 | lockdep_init_map(lock, name, key, 0); |
64aa348e | 3458 | class = register_lock_class(lock, subclass, 0); |
f82b217e | 3459 | hlock->class_idx = class - lock_classes + 1; |
64aa348e PZ |
3460 | |
3461 | curr->lockdep_depth = i; | |
3462 | curr->curr_chain_key = hlock->prev_chain_key; | |
3463 | ||
3464 | for (; i < depth; i++) { | |
3465 | hlock = curr->held_locks + i; | |
3466 | if (!__lock_acquire(hlock->instance, | |
f82b217e | 3467 | hlock_class(hlock)->subclass, hlock->trylock, |
64aa348e | 3468 | hlock->read, hlock->check, hlock->hardirqs_off, |
bb97a91e | 3469 | hlock->nest_lock, hlock->acquire_ip, |
21199f27 | 3470 | hlock->references, hlock->pin_count)) |
64aa348e PZ |
3471 | return 0; |
3472 | } | |
3473 | ||
0119fee4 PZ |
3474 | /* |
3475 | * I took it apart and put it back together again, except now I have | |
3476 | * these 'spare' parts.. where shall I put them. | |
3477 | */ | |
64aa348e PZ |
3478 | if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth)) |
3479 | return 0; | |
3480 | return 1; | |
3481 | } | |
3482 | ||
fbb9ce95 | 3483 | /* |
e0f56fd7 PZ |
3484 | * Remove the lock to the list of currently held locks - this gets |
3485 | * called on mutex_unlock()/spin_unlock*() (or on a failed | |
3486 | * mutex_lock_interruptible()). | |
3487 | * | |
3488 | * @nested is an hysterical artifact, needs a tree wide cleanup. | |
fbb9ce95 IM |
3489 | */ |
3490 | static int | |
e0f56fd7 | 3491 | __lock_release(struct lockdep_map *lock, int nested, unsigned long ip) |
fbb9ce95 | 3492 | { |
e0f56fd7 | 3493 | struct task_struct *curr = current; |
fbb9ce95 IM |
3494 | struct held_lock *hlock, *prev_hlock; |
3495 | unsigned int depth; | |
3496 | int i; | |
3497 | ||
e0f56fd7 PZ |
3498 | if (unlikely(!debug_locks)) |
3499 | return 0; | |
3500 | ||
fbb9ce95 | 3501 | depth = curr->lockdep_depth; |
0119fee4 PZ |
3502 | /* |
3503 | * So we're all set to release this lock.. wait what lock? We don't | |
3504 | * own any locks, you've been drinking again? | |
3505 | */ | |
e0f56fd7 PZ |
3506 | if (DEBUG_LOCKS_WARN_ON(depth <= 0)) |
3507 | return print_unlock_imbalance_bug(curr, lock, ip); | |
fbb9ce95 | 3508 | |
e0f56fd7 PZ |
3509 | /* |
3510 | * Check whether the lock exists in the current stack | |
3511 | * of held locks: | |
3512 | */ | |
fbb9ce95 IM |
3513 | prev_hlock = NULL; |
3514 | for (i = depth-1; i >= 0; i--) { | |
3515 | hlock = curr->held_locks + i; | |
3516 | /* | |
3517 | * We must not cross into another context: | |
3518 | */ | |
3519 | if (prev_hlock && prev_hlock->irq_context != hlock->irq_context) | |
3520 | break; | |
bb97a91e | 3521 | if (match_held_lock(hlock, lock)) |
fbb9ce95 IM |
3522 | goto found_it; |
3523 | prev_hlock = hlock; | |
3524 | } | |
f86f7554 | 3525 | return print_unlock_imbalance_bug(curr, lock, ip); |
fbb9ce95 IM |
3526 | |
3527 | found_it: | |
bb97a91e PZ |
3528 | if (hlock->instance == lock) |
3529 | lock_release_holdtime(hlock); | |
3530 | ||
a24fc60d PZ |
3531 | WARN(hlock->pin_count, "releasing a pinned lock\n"); |
3532 | ||
bb97a91e PZ |
3533 | if (hlock->references) { |
3534 | hlock->references--; | |
3535 | if (hlock->references) { | |
3536 | /* | |
3537 | * We had, and after removing one, still have | |
3538 | * references, the current lock stack is still | |
3539 | * valid. We're done! | |
3540 | */ | |
3541 | return 1; | |
3542 | } | |
3543 | } | |
f20786ff | 3544 | |
fbb9ce95 IM |
3545 | /* |
3546 | * We have the right lock to unlock, 'hlock' points to it. | |
3547 | * Now we remove it from the stack, and add back the other | |
3548 | * entries (if any), recalculating the hash along the way: | |
3549 | */ | |
bb97a91e | 3550 | |
fbb9ce95 IM |
3551 | curr->lockdep_depth = i; |
3552 | curr->curr_chain_key = hlock->prev_chain_key; | |
3553 | ||
3554 | for (i++; i < depth; i++) { | |
3555 | hlock = curr->held_locks + i; | |
3556 | if (!__lock_acquire(hlock->instance, | |
f82b217e | 3557 | hlock_class(hlock)->subclass, hlock->trylock, |
fbb9ce95 | 3558 | hlock->read, hlock->check, hlock->hardirqs_off, |
bb97a91e | 3559 | hlock->nest_lock, hlock->acquire_ip, |
21199f27 | 3560 | hlock->references, hlock->pin_count)) |
fbb9ce95 IM |
3561 | return 0; |
3562 | } | |
3563 | ||
0119fee4 PZ |
3564 | /* |
3565 | * We had N bottles of beer on the wall, we drank one, but now | |
3566 | * there's not N-1 bottles of beer left on the wall... | |
3567 | */ | |
fbb9ce95 IM |
3568 | if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1)) |
3569 | return 0; | |
f20786ff | 3570 | |
fbb9ce95 IM |
3571 | return 1; |
3572 | } | |
3573 | ||
f607c668 | 3574 | static int __lock_is_held(struct lockdep_map *lock) |
fbb9ce95 | 3575 | { |
f607c668 PZ |
3576 | struct task_struct *curr = current; |
3577 | int i; | |
fbb9ce95 | 3578 | |
f607c668 | 3579 | for (i = 0; i < curr->lockdep_depth; i++) { |
bb97a91e | 3580 | struct held_lock *hlock = curr->held_locks + i; |
fbb9ce95 | 3581 | |
bb97a91e | 3582 | if (match_held_lock(hlock, lock)) |
f607c668 PZ |
3583 | return 1; |
3584 | } | |
f20786ff | 3585 | |
f607c668 | 3586 | return 0; |
fbb9ce95 IM |
3587 | } |
3588 | ||
e7904a28 PZ |
3589 | static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock) |
3590 | { | |
3591 | struct pin_cookie cookie = NIL_COOKIE; | |
3592 | struct task_struct *curr = current; | |
3593 | int i; | |
3594 | ||
3595 | if (unlikely(!debug_locks)) | |
3596 | return cookie; | |
3597 | ||
3598 | for (i = 0; i < curr->lockdep_depth; i++) { | |
3599 | struct held_lock *hlock = curr->held_locks + i; | |
3600 | ||
3601 | if (match_held_lock(hlock, lock)) { | |
3602 | /* | |
3603 | * Grab 16bits of randomness; this is sufficient to not | |
3604 | * be guessable and still allows some pin nesting in | |
3605 | * our u32 pin_count. | |
3606 | */ | |
3607 | cookie.val = 1 + (prandom_u32() >> 16); | |
3608 | hlock->pin_count += cookie.val; | |
3609 | return cookie; | |
3610 | } | |
3611 | } | |
3612 | ||
3613 | WARN(1, "pinning an unheld lock\n"); | |
3614 | return cookie; | |
3615 | } | |
3616 | ||
3617 | static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie) | |
fbb9ce95 IM |
3618 | { |
3619 | struct task_struct *curr = current; | |
a24fc60d | 3620 | int i; |
fbb9ce95 | 3621 | |
a24fc60d | 3622 | if (unlikely(!debug_locks)) |
fbb9ce95 IM |
3623 | return; |
3624 | ||
a24fc60d PZ |
3625 | for (i = 0; i < curr->lockdep_depth; i++) { |
3626 | struct held_lock *hlock = curr->held_locks + i; | |
3627 | ||
3628 | if (match_held_lock(hlock, lock)) { | |
e7904a28 | 3629 | hlock->pin_count += cookie.val; |
fbb9ce95 | 3630 | return; |
a24fc60d | 3631 | } |
fbb9ce95 IM |
3632 | } |
3633 | ||
a24fc60d | 3634 | WARN(1, "pinning an unheld lock\n"); |
fbb9ce95 IM |
3635 | } |
3636 | ||
e7904a28 | 3637 | static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie) |
f607c668 PZ |
3638 | { |
3639 | struct task_struct *curr = current; | |
3640 | int i; | |
3641 | ||
a24fc60d PZ |
3642 | if (unlikely(!debug_locks)) |
3643 | return; | |
3644 | ||
f607c668 | 3645 | for (i = 0; i < curr->lockdep_depth; i++) { |
bb97a91e PZ |
3646 | struct held_lock *hlock = curr->held_locks + i; |
3647 | ||
a24fc60d PZ |
3648 | if (match_held_lock(hlock, lock)) { |
3649 | if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n")) | |
3650 | return; | |
3651 | ||
e7904a28 PZ |
3652 | hlock->pin_count -= cookie.val; |
3653 | ||
3654 | if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n")) | |
3655 | hlock->pin_count = 0; | |
3656 | ||
a24fc60d PZ |
3657 | return; |
3658 | } | |
f607c668 PZ |
3659 | } |
3660 | ||
a24fc60d | 3661 | WARN(1, "unpinning an unheld lock\n"); |
f607c668 PZ |
3662 | } |
3663 | ||
fbb9ce95 IM |
3664 | /* |
3665 | * Check whether we follow the irq-flags state precisely: | |
3666 | */ | |
1d09daa5 | 3667 | static void check_flags(unsigned long flags) |
fbb9ce95 | 3668 | { |
992860e9 IM |
3669 | #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \ |
3670 | defined(CONFIG_TRACE_IRQFLAGS) | |
fbb9ce95 IM |
3671 | if (!debug_locks) |
3672 | return; | |
3673 | ||
5f9fa8a6 IM |
3674 | if (irqs_disabled_flags(flags)) { |
3675 | if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) { | |
3676 | printk("possible reason: unannotated irqs-off.\n"); | |
3677 | } | |
3678 | } else { | |
3679 | if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) { | |
3680 | printk("possible reason: unannotated irqs-on.\n"); | |
3681 | } | |
3682 | } | |
fbb9ce95 IM |
3683 | |
3684 | /* | |
3685 | * We dont accurately track softirq state in e.g. | |
3686 | * hardirq contexts (such as on 4KSTACKS), so only | |
3687 | * check if not in hardirq contexts: | |
3688 | */ | |
3689 | if (!hardirq_count()) { | |
0119fee4 PZ |
3690 | if (softirq_count()) { |
3691 | /* like the above, but with softirqs */ | |
fbb9ce95 | 3692 | DEBUG_LOCKS_WARN_ON(current->softirqs_enabled); |
0119fee4 PZ |
3693 | } else { |
3694 | /* lick the above, does it taste good? */ | |
fbb9ce95 | 3695 | DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled); |
0119fee4 | 3696 | } |
fbb9ce95 IM |
3697 | } |
3698 | ||
3699 | if (!debug_locks) | |
3700 | print_irqtrace_events(current); | |
3701 | #endif | |
3702 | } | |
3703 | ||
00ef9f73 PZ |
3704 | void lock_set_class(struct lockdep_map *lock, const char *name, |
3705 | struct lock_class_key *key, unsigned int subclass, | |
3706 | unsigned long ip) | |
64aa348e PZ |
3707 | { |
3708 | unsigned long flags; | |
3709 | ||
3710 | if (unlikely(current->lockdep_recursion)) | |
3711 | return; | |
3712 | ||
3713 | raw_local_irq_save(flags); | |
3714 | current->lockdep_recursion = 1; | |
3715 | check_flags(flags); | |
00ef9f73 | 3716 | if (__lock_set_class(lock, name, key, subclass, ip)) |
64aa348e PZ |
3717 | check_chain_key(current); |
3718 | current->lockdep_recursion = 0; | |
3719 | raw_local_irq_restore(flags); | |
3720 | } | |
00ef9f73 | 3721 | EXPORT_SYMBOL_GPL(lock_set_class); |
64aa348e | 3722 | |
fbb9ce95 IM |
3723 | /* |
3724 | * We are not always called with irqs disabled - do that here, | |
3725 | * and also avoid lockdep recursion: | |
3726 | */ | |
1d09daa5 | 3727 | void lock_acquire(struct lockdep_map *lock, unsigned int subclass, |
7531e2f3 PZ |
3728 | int trylock, int read, int check, |
3729 | struct lockdep_map *nest_lock, unsigned long ip) | |
fbb9ce95 IM |
3730 | { |
3731 | unsigned long flags; | |
3732 | ||
3733 | if (unlikely(current->lockdep_recursion)) | |
3734 | return; | |
3735 | ||
3736 | raw_local_irq_save(flags); | |
3737 | check_flags(flags); | |
3738 | ||
3739 | current->lockdep_recursion = 1; | |
db2c4c77 | 3740 | trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip); |
fbb9ce95 | 3741 | __lock_acquire(lock, subclass, trylock, read, check, |
21199f27 | 3742 | irqs_disabled_flags(flags), nest_lock, ip, 0, 0); |
fbb9ce95 IM |
3743 | current->lockdep_recursion = 0; |
3744 | raw_local_irq_restore(flags); | |
3745 | } | |
fbb9ce95 IM |
3746 | EXPORT_SYMBOL_GPL(lock_acquire); |
3747 | ||
1d09daa5 | 3748 | void lock_release(struct lockdep_map *lock, int nested, |
0764d23c | 3749 | unsigned long ip) |
fbb9ce95 IM |
3750 | { |
3751 | unsigned long flags; | |
3752 | ||
3753 | if (unlikely(current->lockdep_recursion)) | |
3754 | return; | |
3755 | ||
3756 | raw_local_irq_save(flags); | |
3757 | check_flags(flags); | |
3758 | current->lockdep_recursion = 1; | |
93135439 | 3759 | trace_lock_release(lock, ip); |
e0f56fd7 PZ |
3760 | if (__lock_release(lock, nested, ip)) |
3761 | check_chain_key(current); | |
fbb9ce95 IM |
3762 | current->lockdep_recursion = 0; |
3763 | raw_local_irq_restore(flags); | |
3764 | } | |
fbb9ce95 IM |
3765 | EXPORT_SYMBOL_GPL(lock_release); |
3766 | ||
f607c668 PZ |
3767 | int lock_is_held(struct lockdep_map *lock) |
3768 | { | |
3769 | unsigned long flags; | |
3770 | int ret = 0; | |
3771 | ||
3772 | if (unlikely(current->lockdep_recursion)) | |
f2513cde | 3773 | return 1; /* avoid false negative lockdep_assert_held() */ |
f607c668 PZ |
3774 | |
3775 | raw_local_irq_save(flags); | |
3776 | check_flags(flags); | |
3777 | ||
3778 | current->lockdep_recursion = 1; | |
3779 | ret = __lock_is_held(lock); | |
3780 | current->lockdep_recursion = 0; | |
3781 | raw_local_irq_restore(flags); | |
3782 | ||
3783 | return ret; | |
3784 | } | |
3785 | EXPORT_SYMBOL_GPL(lock_is_held); | |
3786 | ||
e7904a28 | 3787 | struct pin_cookie lock_pin_lock(struct lockdep_map *lock) |
a24fc60d | 3788 | { |
e7904a28 | 3789 | struct pin_cookie cookie = NIL_COOKIE; |
a24fc60d PZ |
3790 | unsigned long flags; |
3791 | ||
3792 | if (unlikely(current->lockdep_recursion)) | |
e7904a28 | 3793 | return cookie; |
a24fc60d PZ |
3794 | |
3795 | raw_local_irq_save(flags); | |
3796 | check_flags(flags); | |
3797 | ||
3798 | current->lockdep_recursion = 1; | |
e7904a28 | 3799 | cookie = __lock_pin_lock(lock); |
a24fc60d PZ |
3800 | current->lockdep_recursion = 0; |
3801 | raw_local_irq_restore(flags); | |
e7904a28 PZ |
3802 | |
3803 | return cookie; | |
a24fc60d PZ |
3804 | } |
3805 | EXPORT_SYMBOL_GPL(lock_pin_lock); | |
3806 | ||
e7904a28 PZ |
3807 | void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie) |
3808 | { | |
3809 | unsigned long flags; | |
3810 | ||
3811 | if (unlikely(current->lockdep_recursion)) | |
3812 | return; | |
3813 | ||
3814 | raw_local_irq_save(flags); | |
3815 | check_flags(flags); | |
3816 | ||
3817 | current->lockdep_recursion = 1; | |
3818 | __lock_repin_lock(lock, cookie); | |
3819 | current->lockdep_recursion = 0; | |
3820 | raw_local_irq_restore(flags); | |
3821 | } | |
3822 | EXPORT_SYMBOL_GPL(lock_repin_lock); | |
3823 | ||
3824 | void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie) | |
a24fc60d PZ |
3825 | { |
3826 | unsigned long flags; | |
3827 | ||
3828 | if (unlikely(current->lockdep_recursion)) | |
3829 | return; | |
3830 | ||
3831 | raw_local_irq_save(flags); | |
3832 | check_flags(flags); | |
3833 | ||
3834 | current->lockdep_recursion = 1; | |
e7904a28 | 3835 | __lock_unpin_lock(lock, cookie); |
a24fc60d PZ |
3836 | current->lockdep_recursion = 0; |
3837 | raw_local_irq_restore(flags); | |
3838 | } | |
3839 | EXPORT_SYMBOL_GPL(lock_unpin_lock); | |
3840 | ||
cf40bd16 NP |
3841 | void lockdep_set_current_reclaim_state(gfp_t gfp_mask) |
3842 | { | |
3843 | current->lockdep_reclaim_gfp = gfp_mask; | |
3844 | } | |
3845 | ||
3846 | void lockdep_clear_current_reclaim_state(void) | |
3847 | { | |
3848 | current->lockdep_reclaim_gfp = 0; | |
3849 | } | |
3850 | ||
f20786ff PZ |
3851 | #ifdef CONFIG_LOCK_STAT |
3852 | static int | |
3853 | print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock, | |
3854 | unsigned long ip) | |
3855 | { | |
3856 | if (!debug_locks_off()) | |
3857 | return 0; | |
3858 | if (debug_locks_silent) | |
3859 | return 0; | |
3860 | ||
b3fbab05 PM |
3861 | printk("\n"); |
3862 | printk("=================================\n"); | |
3863 | printk("[ BUG: bad contention detected! ]\n"); | |
fbdc4b9a | 3864 | print_kernel_ident(); |
b3fbab05 | 3865 | printk("---------------------------------\n"); |
f20786ff | 3866 | printk("%s/%d is trying to contend lock (", |
ba25f9dc | 3867 | curr->comm, task_pid_nr(curr)); |
f20786ff PZ |
3868 | print_lockdep_cache(lock); |
3869 | printk(") at:\n"); | |
3870 | print_ip_sym(ip); | |
3871 | printk("but there are no locks held!\n"); | |
3872 | printk("\nother info that might help us debug this:\n"); | |
3873 | lockdep_print_held_locks(curr); | |
3874 | ||
3875 | printk("\nstack backtrace:\n"); | |
3876 | dump_stack(); | |
3877 | ||
3878 | return 0; | |
3879 | } | |
3880 | ||
3881 | static void | |
3882 | __lock_contended(struct lockdep_map *lock, unsigned long ip) | |
3883 | { | |
3884 | struct task_struct *curr = current; | |
3885 | struct held_lock *hlock, *prev_hlock; | |
3886 | struct lock_class_stats *stats; | |
3887 | unsigned int depth; | |
c7e78cff | 3888 | int i, contention_point, contending_point; |
f20786ff PZ |
3889 | |
3890 | depth = curr->lockdep_depth; | |
0119fee4 PZ |
3891 | /* |
3892 | * Whee, we contended on this lock, except it seems we're not | |
3893 | * actually trying to acquire anything much at all.. | |
3894 | */ | |
f20786ff PZ |
3895 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
3896 | return; | |
3897 | ||
3898 | prev_hlock = NULL; | |
3899 | for (i = depth-1; i >= 0; i--) { | |
3900 | hlock = curr->held_locks + i; | |
3901 | /* | |
3902 | * We must not cross into another context: | |
3903 | */ | |
3904 | if (prev_hlock && prev_hlock->irq_context != hlock->irq_context) | |
3905 | break; | |
bb97a91e | 3906 | if (match_held_lock(hlock, lock)) |
f20786ff PZ |
3907 | goto found_it; |
3908 | prev_hlock = hlock; | |
3909 | } | |
3910 | print_lock_contention_bug(curr, lock, ip); | |
3911 | return; | |
3912 | ||
3913 | found_it: | |
bb97a91e PZ |
3914 | if (hlock->instance != lock) |
3915 | return; | |
3916 | ||
3365e779 | 3917 | hlock->waittime_stamp = lockstat_clock(); |
f20786ff | 3918 | |
c7e78cff PZ |
3919 | contention_point = lock_point(hlock_class(hlock)->contention_point, ip); |
3920 | contending_point = lock_point(hlock_class(hlock)->contending_point, | |
3921 | lock->ip); | |
f20786ff | 3922 | |
f82b217e | 3923 | stats = get_lock_stats(hlock_class(hlock)); |
c7e78cff PZ |
3924 | if (contention_point < LOCKSTAT_POINTS) |
3925 | stats->contention_point[contention_point]++; | |
3926 | if (contending_point < LOCKSTAT_POINTS) | |
3927 | stats->contending_point[contending_point]++; | |
96645678 PZ |
3928 | if (lock->cpu != smp_processor_id()) |
3929 | stats->bounces[bounce_contended + !!hlock->read]++; | |
f20786ff PZ |
3930 | put_lock_stats(stats); |
3931 | } | |
3932 | ||
3933 | static void | |
c7e78cff | 3934 | __lock_acquired(struct lockdep_map *lock, unsigned long ip) |
f20786ff PZ |
3935 | { |
3936 | struct task_struct *curr = current; | |
3937 | struct held_lock *hlock, *prev_hlock; | |
3938 | struct lock_class_stats *stats; | |
3939 | unsigned int depth; | |
3365e779 | 3940 | u64 now, waittime = 0; |
96645678 | 3941 | int i, cpu; |
f20786ff PZ |
3942 | |
3943 | depth = curr->lockdep_depth; | |
0119fee4 PZ |
3944 | /* |
3945 | * Yay, we acquired ownership of this lock we didn't try to | |
3946 | * acquire, how the heck did that happen? | |
3947 | */ | |
f20786ff PZ |
3948 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
3949 | return; | |
3950 | ||
3951 | prev_hlock = NULL; | |
3952 | for (i = depth-1; i >= 0; i--) { | |
3953 | hlock = curr->held_locks + i; | |
3954 | /* | |
3955 | * We must not cross into another context: | |
3956 | */ | |
3957 | if (prev_hlock && prev_hlock->irq_context != hlock->irq_context) | |
3958 | break; | |
bb97a91e | 3959 | if (match_held_lock(hlock, lock)) |
f20786ff PZ |
3960 | goto found_it; |
3961 | prev_hlock = hlock; | |
3962 | } | |
3963 | print_lock_contention_bug(curr, lock, _RET_IP_); | |
3964 | return; | |
3965 | ||
3966 | found_it: | |
bb97a91e PZ |
3967 | if (hlock->instance != lock) |
3968 | return; | |
3969 | ||
96645678 PZ |
3970 | cpu = smp_processor_id(); |
3971 | if (hlock->waittime_stamp) { | |
3365e779 | 3972 | now = lockstat_clock(); |
96645678 PZ |
3973 | waittime = now - hlock->waittime_stamp; |
3974 | hlock->holdtime_stamp = now; | |
3975 | } | |
f20786ff | 3976 | |
883a2a31 | 3977 | trace_lock_acquired(lock, ip); |
2062501a | 3978 | |
f82b217e | 3979 | stats = get_lock_stats(hlock_class(hlock)); |
96645678 PZ |
3980 | if (waittime) { |
3981 | if (hlock->read) | |
3982 | lock_time_inc(&stats->read_waittime, waittime); | |
3983 | else | |
3984 | lock_time_inc(&stats->write_waittime, waittime); | |
3985 | } | |
3986 | if (lock->cpu != cpu) | |
3987 | stats->bounces[bounce_acquired + !!hlock->read]++; | |
f20786ff | 3988 | put_lock_stats(stats); |
96645678 PZ |
3989 | |
3990 | lock->cpu = cpu; | |
c7e78cff | 3991 | lock->ip = ip; |
f20786ff PZ |
3992 | } |
3993 | ||
3994 | void lock_contended(struct lockdep_map *lock, unsigned long ip) | |
3995 | { | |
3996 | unsigned long flags; | |
3997 | ||
3998 | if (unlikely(!lock_stat)) | |
3999 | return; | |
4000 | ||
4001 | if (unlikely(current->lockdep_recursion)) | |
4002 | return; | |
4003 | ||
4004 | raw_local_irq_save(flags); | |
4005 | check_flags(flags); | |
4006 | current->lockdep_recursion = 1; | |
db2c4c77 | 4007 | trace_lock_contended(lock, ip); |
f20786ff PZ |
4008 | __lock_contended(lock, ip); |
4009 | current->lockdep_recursion = 0; | |
4010 | raw_local_irq_restore(flags); | |
4011 | } | |
4012 | EXPORT_SYMBOL_GPL(lock_contended); | |
4013 | ||
c7e78cff | 4014 | void lock_acquired(struct lockdep_map *lock, unsigned long ip) |
f20786ff PZ |
4015 | { |
4016 | unsigned long flags; | |
4017 | ||
4018 | if (unlikely(!lock_stat)) | |
4019 | return; | |
4020 | ||
4021 | if (unlikely(current->lockdep_recursion)) | |
4022 | return; | |
4023 | ||
4024 | raw_local_irq_save(flags); | |
4025 | check_flags(flags); | |
4026 | current->lockdep_recursion = 1; | |
c7e78cff | 4027 | __lock_acquired(lock, ip); |
f20786ff PZ |
4028 | current->lockdep_recursion = 0; |
4029 | raw_local_irq_restore(flags); | |
4030 | } | |
4031 | EXPORT_SYMBOL_GPL(lock_acquired); | |
4032 | #endif | |
4033 | ||
fbb9ce95 IM |
4034 | /* |
4035 | * Used by the testsuite, sanitize the validator state | |
4036 | * after a simulated failure: | |
4037 | */ | |
4038 | ||
4039 | void lockdep_reset(void) | |
4040 | { | |
4041 | unsigned long flags; | |
23d95a03 | 4042 | int i; |
fbb9ce95 IM |
4043 | |
4044 | raw_local_irq_save(flags); | |
4045 | current->curr_chain_key = 0; | |
4046 | current->lockdep_depth = 0; | |
4047 | current->lockdep_recursion = 0; | |
4048 | memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock)); | |
4049 | nr_hardirq_chains = 0; | |
4050 | nr_softirq_chains = 0; | |
4051 | nr_process_chains = 0; | |
4052 | debug_locks = 1; | |
23d95a03 | 4053 | for (i = 0; i < CHAINHASH_SIZE; i++) |
a63f38cc | 4054 | INIT_HLIST_HEAD(chainhash_table + i); |
fbb9ce95 IM |
4055 | raw_local_irq_restore(flags); |
4056 | } | |
4057 | ||
4058 | static void zap_class(struct lock_class *class) | |
4059 | { | |
4060 | int i; | |
4061 | ||
4062 | /* | |
4063 | * Remove all dependencies this lock is | |
4064 | * involved in: | |
4065 | */ | |
4066 | for (i = 0; i < nr_list_entries; i++) { | |
4067 | if (list_entries[i].class == class) | |
4068 | list_del_rcu(&list_entries[i].entry); | |
4069 | } | |
4070 | /* | |
4071 | * Unhash the class and remove it from the all_lock_classes list: | |
4072 | */ | |
a63f38cc | 4073 | hlist_del_rcu(&class->hash_entry); |
fbb9ce95 IM |
4074 | list_del_rcu(&class->lock_entry); |
4075 | ||
cee34d88 PZ |
4076 | RCU_INIT_POINTER(class->key, NULL); |
4077 | RCU_INIT_POINTER(class->name, NULL); | |
fbb9ce95 IM |
4078 | } |
4079 | ||
fabe874a | 4080 | static inline int within(const void *addr, void *start, unsigned long size) |
fbb9ce95 IM |
4081 | { |
4082 | return addr >= start && addr < start + size; | |
4083 | } | |
4084 | ||
35a9393c PZ |
4085 | /* |
4086 | * Used in module.c to remove lock classes from memory that is going to be | |
4087 | * freed; and possibly re-used by other modules. | |
4088 | * | |
4089 | * We will have had one sync_sched() before getting here, so we're guaranteed | |
4090 | * nobody will look up these exact classes -- they're properly dead but still | |
4091 | * allocated. | |
4092 | */ | |
fbb9ce95 IM |
4093 | void lockdep_free_key_range(void *start, unsigned long size) |
4094 | { | |
35a9393c | 4095 | struct lock_class *class; |
a63f38cc | 4096 | struct hlist_head *head; |
fbb9ce95 IM |
4097 | unsigned long flags; |
4098 | int i; | |
5a26db5b | 4099 | int locked; |
fbb9ce95 IM |
4100 | |
4101 | raw_local_irq_save(flags); | |
5a26db5b | 4102 | locked = graph_lock(); |
fbb9ce95 IM |
4103 | |
4104 | /* | |
4105 | * Unhash all classes that were created by this module: | |
4106 | */ | |
4107 | for (i = 0; i < CLASSHASH_SIZE; i++) { | |
4108 | head = classhash_table + i; | |
a63f38cc | 4109 | hlist_for_each_entry_rcu(class, head, hash_entry) { |
fbb9ce95 IM |
4110 | if (within(class->key, start, size)) |
4111 | zap_class(class); | |
fabe874a AV |
4112 | else if (within(class->name, start, size)) |
4113 | zap_class(class); | |
4114 | } | |
fbb9ce95 IM |
4115 | } |
4116 | ||
5a26db5b NP |
4117 | if (locked) |
4118 | graph_unlock(); | |
fbb9ce95 | 4119 | raw_local_irq_restore(flags); |
35a9393c PZ |
4120 | |
4121 | /* | |
4122 | * Wait for any possible iterators from look_up_lock_class() to pass | |
4123 | * before continuing to free the memory they refer to. | |
4124 | * | |
4125 | * sync_sched() is sufficient because the read-side is IRQ disable. | |
4126 | */ | |
4127 | synchronize_sched(); | |
4128 | ||
4129 | /* | |
4130 | * XXX at this point we could return the resources to the pool; | |
4131 | * instead we leak them. We would need to change to bitmap allocators | |
4132 | * instead of the linear allocators we have now. | |
4133 | */ | |
fbb9ce95 IM |
4134 | } |
4135 | ||
4136 | void lockdep_reset_lock(struct lockdep_map *lock) | |
4137 | { | |
35a9393c | 4138 | struct lock_class *class; |
a63f38cc | 4139 | struct hlist_head *head; |
fbb9ce95 IM |
4140 | unsigned long flags; |
4141 | int i, j; | |
5a26db5b | 4142 | int locked; |
fbb9ce95 IM |
4143 | |
4144 | raw_local_irq_save(flags); | |
fbb9ce95 IM |
4145 | |
4146 | /* | |
d6d897ce IM |
4147 | * Remove all classes this lock might have: |
4148 | */ | |
4149 | for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) { | |
4150 | /* | |
4151 | * If the class exists we look it up and zap it: | |
4152 | */ | |
4153 | class = look_up_lock_class(lock, j); | |
4154 | if (class) | |
4155 | zap_class(class); | |
4156 | } | |
4157 | /* | |
4158 | * Debug check: in the end all mapped classes should | |
4159 | * be gone. | |
fbb9ce95 | 4160 | */ |
5a26db5b | 4161 | locked = graph_lock(); |
fbb9ce95 IM |
4162 | for (i = 0; i < CLASSHASH_SIZE; i++) { |
4163 | head = classhash_table + i; | |
a63f38cc | 4164 | hlist_for_each_entry_rcu(class, head, hash_entry) { |
62016250 HM |
4165 | int match = 0; |
4166 | ||
4167 | for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++) | |
4168 | match |= class == lock->class_cache[j]; | |
4169 | ||
4170 | if (unlikely(match)) { | |
0119fee4 PZ |
4171 | if (debug_locks_off_graph_unlock()) { |
4172 | /* | |
4173 | * We all just reset everything, how did it match? | |
4174 | */ | |
74c383f1 | 4175 | WARN_ON(1); |
0119fee4 | 4176 | } |
d6d897ce | 4177 | goto out_restore; |
fbb9ce95 IM |
4178 | } |
4179 | } | |
4180 | } | |
5a26db5b NP |
4181 | if (locked) |
4182 | graph_unlock(); | |
d6d897ce IM |
4183 | |
4184 | out_restore: | |
fbb9ce95 IM |
4185 | raw_local_irq_restore(flags); |
4186 | } | |
4187 | ||
fbb9ce95 IM |
4188 | void __init lockdep_info(void) |
4189 | { | |
4190 | printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n"); | |
4191 | ||
b0788caf | 4192 | printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES); |
fbb9ce95 IM |
4193 | printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH); |
4194 | printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS); | |
b0788caf | 4195 | printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE); |
fbb9ce95 IM |
4196 | printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES); |
4197 | printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS); | |
4198 | printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE); | |
4199 | ||
4200 | printk(" memory used by lock dependency info: %lu kB\n", | |
4201 | (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS + | |
4202 | sizeof(struct list_head) * CLASSHASH_SIZE + | |
4203 | sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES + | |
4204 | sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS + | |
90629209 | 4205 | sizeof(struct list_head) * CHAINHASH_SIZE |
4dd861d6 | 4206 | #ifdef CONFIG_PROVE_LOCKING |
e351b660 | 4207 | + sizeof(struct circular_queue) |
4dd861d6 | 4208 | #endif |
90629209 | 4209 | ) / 1024 |
4dd861d6 | 4210 | ); |
fbb9ce95 IM |
4211 | |
4212 | printk(" per task-struct memory footprint: %lu bytes\n", | |
4213 | sizeof(struct held_lock) * MAX_LOCK_DEPTH); | |
fbb9ce95 IM |
4214 | } |
4215 | ||
fbb9ce95 IM |
4216 | static void |
4217 | print_freed_lock_bug(struct task_struct *curr, const void *mem_from, | |
55794a41 | 4218 | const void *mem_to, struct held_lock *hlock) |
fbb9ce95 IM |
4219 | { |
4220 | if (!debug_locks_off()) | |
4221 | return; | |
4222 | if (debug_locks_silent) | |
4223 | return; | |
4224 | ||
b3fbab05 PM |
4225 | printk("\n"); |
4226 | printk("=========================\n"); | |
4227 | printk("[ BUG: held lock freed! ]\n"); | |
fbdc4b9a | 4228 | print_kernel_ident(); |
b3fbab05 | 4229 | printk("-------------------------\n"); |
fbb9ce95 | 4230 | printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n", |
ba25f9dc | 4231 | curr->comm, task_pid_nr(curr), mem_from, mem_to-1); |
55794a41 | 4232 | print_lock(hlock); |
fbb9ce95 IM |
4233 | lockdep_print_held_locks(curr); |
4234 | ||
4235 | printk("\nstack backtrace:\n"); | |
4236 | dump_stack(); | |
4237 | } | |
4238 | ||
54561783 ON |
4239 | static inline int not_in_range(const void* mem_from, unsigned long mem_len, |
4240 | const void* lock_from, unsigned long lock_len) | |
4241 | { | |
4242 | return lock_from + lock_len <= mem_from || | |
4243 | mem_from + mem_len <= lock_from; | |
4244 | } | |
4245 | ||
fbb9ce95 IM |
4246 | /* |
4247 | * Called when kernel memory is freed (or unmapped), or if a lock | |
4248 | * is destroyed or reinitialized - this code checks whether there is | |
4249 | * any held lock in the memory range of <from> to <to>: | |
4250 | */ | |
4251 | void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len) | |
4252 | { | |
fbb9ce95 IM |
4253 | struct task_struct *curr = current; |
4254 | struct held_lock *hlock; | |
4255 | unsigned long flags; | |
4256 | int i; | |
4257 | ||
4258 | if (unlikely(!debug_locks)) | |
4259 | return; | |
4260 | ||
4261 | local_irq_save(flags); | |
4262 | for (i = 0; i < curr->lockdep_depth; i++) { | |
4263 | hlock = curr->held_locks + i; | |
4264 | ||
54561783 ON |
4265 | if (not_in_range(mem_from, mem_len, hlock->instance, |
4266 | sizeof(*hlock->instance))) | |
fbb9ce95 IM |
4267 | continue; |
4268 | ||
54561783 | 4269 | print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock); |
fbb9ce95 IM |
4270 | break; |
4271 | } | |
4272 | local_irq_restore(flags); | |
4273 | } | |
ed07536e | 4274 | EXPORT_SYMBOL_GPL(debug_check_no_locks_freed); |
fbb9ce95 | 4275 | |
1b1d2fb4 | 4276 | static void print_held_locks_bug(void) |
fbb9ce95 IM |
4277 | { |
4278 | if (!debug_locks_off()) | |
4279 | return; | |
4280 | if (debug_locks_silent) | |
4281 | return; | |
4282 | ||
b3fbab05 PM |
4283 | printk("\n"); |
4284 | printk("=====================================\n"); | |
1b1d2fb4 CC |
4285 | printk("[ BUG: %s/%d still has locks held! ]\n", |
4286 | current->comm, task_pid_nr(current)); | |
fbdc4b9a | 4287 | print_kernel_ident(); |
b3fbab05 | 4288 | printk("-------------------------------------\n"); |
1b1d2fb4 | 4289 | lockdep_print_held_locks(current); |
fbb9ce95 IM |
4290 | printk("\nstack backtrace:\n"); |
4291 | dump_stack(); | |
4292 | } | |
4293 | ||
1b1d2fb4 | 4294 | void debug_check_no_locks_held(void) |
fbb9ce95 | 4295 | { |
1b1d2fb4 CC |
4296 | if (unlikely(current->lockdep_depth > 0)) |
4297 | print_held_locks_bug(); | |
fbb9ce95 | 4298 | } |
1b1d2fb4 | 4299 | EXPORT_SYMBOL_GPL(debug_check_no_locks_held); |
fbb9ce95 | 4300 | |
8dce7a9a | 4301 | #ifdef __KERNEL__ |
fbb9ce95 IM |
4302 | void debug_show_all_locks(void) |
4303 | { | |
4304 | struct task_struct *g, *p; | |
4305 | int count = 10; | |
4306 | int unlock = 1; | |
4307 | ||
9c35dd7f JP |
4308 | if (unlikely(!debug_locks)) { |
4309 | printk("INFO: lockdep is turned off.\n"); | |
4310 | return; | |
4311 | } | |
fbb9ce95 IM |
4312 | printk("\nShowing all locks held in the system:\n"); |
4313 | ||
4314 | /* | |
4315 | * Here we try to get the tasklist_lock as hard as possible, | |
4316 | * if not successful after 2 seconds we ignore it (but keep | |
4317 | * trying). This is to enable a debug printout even if a | |
4318 | * tasklist_lock-holding task deadlocks or crashes. | |
4319 | */ | |
4320 | retry: | |
4321 | if (!read_trylock(&tasklist_lock)) { | |
4322 | if (count == 10) | |
4323 | printk("hm, tasklist_lock locked, retrying... "); | |
4324 | if (count) { | |
4325 | count--; | |
4326 | printk(" #%d", 10-count); | |
4327 | mdelay(200); | |
4328 | goto retry; | |
4329 | } | |
4330 | printk(" ignoring it.\n"); | |
4331 | unlock = 0; | |
46fec7ac | 4332 | } else { |
4333 | if (count != 10) | |
4334 | printk(KERN_CONT " locked it.\n"); | |
fbb9ce95 | 4335 | } |
fbb9ce95 IM |
4336 | |
4337 | do_each_thread(g, p) { | |
85684873 IM |
4338 | /* |
4339 | * It's not reliable to print a task's held locks | |
4340 | * if it's not sleeping (or if it's not the current | |
4341 | * task): | |
4342 | */ | |
4343 | if (p->state == TASK_RUNNING && p != current) | |
4344 | continue; | |
fbb9ce95 IM |
4345 | if (p->lockdep_depth) |
4346 | lockdep_print_held_locks(p); | |
4347 | if (!unlock) | |
4348 | if (read_trylock(&tasklist_lock)) | |
4349 | unlock = 1; | |
4350 | } while_each_thread(g, p); | |
4351 | ||
4352 | printk("\n"); | |
4353 | printk("=============================================\n\n"); | |
4354 | ||
4355 | if (unlock) | |
4356 | read_unlock(&tasklist_lock); | |
4357 | } | |
fbb9ce95 | 4358 | EXPORT_SYMBOL_GPL(debug_show_all_locks); |
8dce7a9a | 4359 | #endif |
fbb9ce95 | 4360 | |
82a1fcb9 IM |
4361 | /* |
4362 | * Careful: only use this function if you are sure that | |
4363 | * the task cannot run in parallel! | |
4364 | */ | |
f1b499f0 | 4365 | void debug_show_held_locks(struct task_struct *task) |
fbb9ce95 | 4366 | { |
9c35dd7f JP |
4367 | if (unlikely(!debug_locks)) { |
4368 | printk("INFO: lockdep is turned off.\n"); | |
4369 | return; | |
4370 | } | |
fbb9ce95 IM |
4371 | lockdep_print_held_locks(task); |
4372 | } | |
fbb9ce95 | 4373 | EXPORT_SYMBOL_GPL(debug_show_held_locks); |
b351d164 | 4374 | |
722a9f92 | 4375 | asmlinkage __visible void lockdep_sys_exit(void) |
b351d164 PZ |
4376 | { |
4377 | struct task_struct *curr = current; | |
4378 | ||
4379 | if (unlikely(curr->lockdep_depth)) { | |
4380 | if (!debug_locks_off()) | |
4381 | return; | |
b3fbab05 PM |
4382 | printk("\n"); |
4383 | printk("================================================\n"); | |
4384 | printk("[ BUG: lock held when returning to user space! ]\n"); | |
fbdc4b9a | 4385 | print_kernel_ident(); |
b3fbab05 | 4386 | printk("------------------------------------------------\n"); |
b351d164 PZ |
4387 | printk("%s/%d is leaving the kernel with locks still held!\n", |
4388 | curr->comm, curr->pid); | |
4389 | lockdep_print_held_locks(curr); | |
4390 | } | |
4391 | } | |
0632eb3d | 4392 | |
b3fbab05 | 4393 | void lockdep_rcu_suspicious(const char *file, const int line, const char *s) |
0632eb3d PM |
4394 | { |
4395 | struct task_struct *curr = current; | |
4396 | ||
2b3fc35f | 4397 | #ifndef CONFIG_PROVE_RCU_REPEATEDLY |
0632eb3d PM |
4398 | if (!debug_locks_off()) |
4399 | return; | |
2b3fc35f LJ |
4400 | #endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */ |
4401 | /* Note: the following can be executed concurrently, so be careful. */ | |
b3fbab05 PM |
4402 | printk("\n"); |
4403 | printk("===============================\n"); | |
4404 | printk("[ INFO: suspicious RCU usage. ]\n"); | |
fbdc4b9a | 4405 | print_kernel_ident(); |
b3fbab05 PM |
4406 | printk("-------------------------------\n"); |
4407 | printk("%s:%d %s!\n", file, line, s); | |
0632eb3d | 4408 | printk("\nother info that might help us debug this:\n\n"); |
c5fdcec9 PM |
4409 | printk("\n%srcu_scheduler_active = %d, debug_locks = %d\n", |
4410 | !rcu_lockdep_current_cpu_online() | |
4411 | ? "RCU used illegally from offline CPU!\n" | |
5c173eb8 | 4412 | : !rcu_is_watching() |
c5fdcec9 PM |
4413 | ? "RCU used illegally from idle CPU!\n" |
4414 | : "", | |
4415 | rcu_scheduler_active, debug_locks); | |
0464e937 FW |
4416 | |
4417 | /* | |
4418 | * If a CPU is in the RCU-free window in idle (ie: in the section | |
4419 | * between rcu_idle_enter() and rcu_idle_exit(), then RCU | |
4420 | * considers that CPU to be in an "extended quiescent state", | |
4421 | * which means that RCU will be completely ignoring that CPU. | |
4422 | * Therefore, rcu_read_lock() and friends have absolutely no | |
4423 | * effect on a CPU running in that state. In other words, even if | |
4424 | * such an RCU-idle CPU has called rcu_read_lock(), RCU might well | |
4425 | * delete data structures out from under it. RCU really has no | |
4426 | * choice here: we need to keep an RCU-free window in idle where | |
4427 | * the CPU may possibly enter into low power mode. This way we can | |
4428 | * notice an extended quiescent state to other CPUs that started a grace | |
4429 | * period. Otherwise we would delay any grace period as long as we run | |
4430 | * in the idle task. | |
4431 | * | |
4432 | * So complain bitterly if someone does call rcu_read_lock(), | |
4433 | * rcu_read_lock_bh() and so on from extended quiescent states. | |
4434 | */ | |
5c173eb8 | 4435 | if (!rcu_is_watching()) |
0464e937 FW |
4436 | printk("RCU used illegally from extended quiescent state!\n"); |
4437 | ||
0632eb3d PM |
4438 | lockdep_print_held_locks(curr); |
4439 | printk("\nstack backtrace:\n"); | |
4440 | dump_stack(); | |
4441 | } | |
b3fbab05 | 4442 | EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious); |