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