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