]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
fbb9ce95 IM |
2 | /* |
3 | * kernel/lockdep.c | |
4 | * | |
5 | * Runtime locking correctness validator | |
6 | * | |
7 | * Started by Ingo Molnar: | |
8 | * | |
4b32d0a4 | 9 | * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <[email protected]> |
90eec103 | 10 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra |
fbb9ce95 IM |
11 | * |
12 | * this code maps all the lock dependencies as they occur in a live kernel | |
13 | * and will warn about the following classes of locking bugs: | |
14 | * | |
15 | * - lock inversion scenarios | |
16 | * - circular lock dependencies | |
17 | * - hardirq/softirq safe/unsafe locking bugs | |
18 | * | |
19 | * Bugs are reported even if the current locking scenario does not cause | |
20 | * any deadlock at this point. | |
21 | * | |
22 | * I.e. if anytime in the past two locks were taken in a different order, | |
23 | * even if it happened for another task, even if those were different | |
24 | * locks (but of the same class as this lock), this code will detect it. | |
25 | * | |
26 | * Thanks to Arjan van de Ven for coming up with the initial idea of | |
27 | * mapping lock dependencies runtime. | |
28 | */ | |
a5e25883 | 29 | #define DISABLE_BRANCH_PROFILING |
fbb9ce95 IM |
30 | #include <linux/mutex.h> |
31 | #include <linux/sched.h> | |
e6017571 | 32 | #include <linux/sched/clock.h> |
29930025 | 33 | #include <linux/sched/task.h> |
6d7225f0 | 34 | #include <linux/sched/mm.h> |
fbb9ce95 IM |
35 | #include <linux/delay.h> |
36 | #include <linux/module.h> | |
37 | #include <linux/proc_fs.h> | |
38 | #include <linux/seq_file.h> | |
39 | #include <linux/spinlock.h> | |
40 | #include <linux/kallsyms.h> | |
41 | #include <linux/interrupt.h> | |
42 | #include <linux/stacktrace.h> | |
43 | #include <linux/debug_locks.h> | |
44 | #include <linux/irqflags.h> | |
99de055a | 45 | #include <linux/utsname.h> |
4b32d0a4 | 46 | #include <linux/hash.h> |
81d68a96 | 47 | #include <linux/ftrace.h> |
b4b136f4 | 48 | #include <linux/stringify.h> |
ace35a7a | 49 | #include <linux/bitmap.h> |
d588e461 | 50 | #include <linux/bitops.h> |
5a0e3ad6 | 51 | #include <linux/gfp.h> |
e7904a28 | 52 | #include <linux/random.h> |
dfaaf3fa | 53 | #include <linux/jhash.h> |
88f1c87d | 54 | #include <linux/nmi.h> |
a0b0fd53 | 55 | #include <linux/rcupdate.h> |
2f43c602 | 56 | #include <linux/kprobes.h> |
af012961 | 57 | |
fbb9ce95 IM |
58 | #include <asm/sections.h> |
59 | ||
60 | #include "lockdep_internals.h" | |
61 | ||
a8d154b0 | 62 | #define CREATE_TRACE_POINTS |
67178767 | 63 | #include <trace/events/lock.h> |
a8d154b0 | 64 | |
f20786ff PZ |
65 | #ifdef CONFIG_PROVE_LOCKING |
66 | int prove_locking = 1; | |
67 | module_param(prove_locking, int, 0644); | |
68 | #else | |
69 | #define prove_locking 0 | |
70 | #endif | |
71 | ||
72 | #ifdef CONFIG_LOCK_STAT | |
73 | int lock_stat = 1; | |
74 | module_param(lock_stat, int, 0644); | |
75 | #else | |
76 | #define lock_stat 0 | |
77 | #endif | |
78 | ||
4d004099 PZ |
79 | DEFINE_PER_CPU(unsigned int, lockdep_recursion); |
80 | EXPORT_PER_CPU_SYMBOL_GPL(lockdep_recursion); | |
81 | ||
0afda3a8 | 82 | static __always_inline bool lockdep_enabled(void) |
4d004099 PZ |
83 | { |
84 | if (!debug_locks) | |
85 | return false; | |
86 | ||
d48e3850 | 87 | if (this_cpu_read(lockdep_recursion)) |
4d004099 PZ |
88 | return false; |
89 | ||
90 | if (current->lockdep_recursion) | |
91 | return false; | |
92 | ||
93 | return true; | |
94 | } | |
95 | ||
fbb9ce95 | 96 | /* |
74c383f1 IM |
97 | * lockdep_lock: protects the lockdep graph, the hashes and the |
98 | * class/list/hash allocators. | |
fbb9ce95 IM |
99 | * |
100 | * This is one of the rare exceptions where it's justified | |
101 | * to use a raw spinlock - we really dont want the spinlock | |
74c383f1 | 102 | * code to recurse back into the lockdep code... |
fbb9ce95 | 103 | */ |
248efb21 PZ |
104 | static arch_spinlock_t __lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
105 | static struct task_struct *__owner; | |
106 | ||
107 | static inline void lockdep_lock(void) | |
108 | { | |
109 | DEBUG_LOCKS_WARN_ON(!irqs_disabled()); | |
110 | ||
43be4388 | 111 | __this_cpu_inc(lockdep_recursion); |
248efb21 PZ |
112 | arch_spin_lock(&__lock); |
113 | __owner = current; | |
248efb21 PZ |
114 | } |
115 | ||
116 | static inline void lockdep_unlock(void) | |
117 | { | |
43be4388 BF |
118 | DEBUG_LOCKS_WARN_ON(!irqs_disabled()); |
119 | ||
248efb21 PZ |
120 | if (debug_locks && DEBUG_LOCKS_WARN_ON(__owner != current)) |
121 | return; | |
122 | ||
248efb21 PZ |
123 | __owner = NULL; |
124 | arch_spin_unlock(&__lock); | |
43be4388 | 125 | __this_cpu_dec(lockdep_recursion); |
248efb21 PZ |
126 | } |
127 | ||
128 | static inline bool lockdep_assert_locked(void) | |
129 | { | |
130 | return DEBUG_LOCKS_WARN_ON(__owner != current); | |
131 | } | |
132 | ||
cdc84d79 | 133 | static struct task_struct *lockdep_selftest_task_struct; |
74c383f1 | 134 | |
248efb21 | 135 | |
74c383f1 IM |
136 | static int graph_lock(void) |
137 | { | |
248efb21 | 138 | lockdep_lock(); |
74c383f1 IM |
139 | /* |
140 | * Make sure that if another CPU detected a bug while | |
141 | * walking the graph we dont change it (while the other | |
142 | * CPU is busy printing out stuff with the graph lock | |
143 | * dropped already) | |
144 | */ | |
145 | if (!debug_locks) { | |
248efb21 | 146 | lockdep_unlock(); |
74c383f1 IM |
147 | return 0; |
148 | } | |
149 | return 1; | |
150 | } | |
151 | ||
248efb21 | 152 | static inline void graph_unlock(void) |
74c383f1 | 153 | { |
248efb21 | 154 | lockdep_unlock(); |
74c383f1 IM |
155 | } |
156 | ||
157 | /* | |
158 | * Turn lock debugging off and return with 0 if it was off already, | |
159 | * and also release the graph lock: | |
160 | */ | |
161 | static inline int debug_locks_off_graph_unlock(void) | |
162 | { | |
163 | int ret = debug_locks_off(); | |
164 | ||
248efb21 | 165 | lockdep_unlock(); |
74c383f1 IM |
166 | |
167 | return ret; | |
168 | } | |
fbb9ce95 | 169 | |
fbb9ce95 | 170 | unsigned long nr_list_entries; |
af012961 | 171 | static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES]; |
ace35a7a | 172 | static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES); |
fbb9ce95 | 173 | |
fbb9ce95 IM |
174 | /* |
175 | * All data structures here are protected by the global debug_lock. | |
176 | * | |
a0b0fd53 BVA |
177 | * nr_lock_classes is the number of elements of lock_classes[] that is |
178 | * in use. | |
fbb9ce95 | 179 | */ |
108c1485 BVA |
180 | #define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1) |
181 | #define KEYHASH_SIZE (1UL << KEYHASH_BITS) | |
182 | static struct hlist_head lock_keys_hash[KEYHASH_SIZE]; | |
fbb9ce95 | 183 | unsigned long nr_lock_classes; |
1d44bcb4 | 184 | unsigned long nr_zapped_classes; |
1431a5d2 BVA |
185 | #ifndef CONFIG_DEBUG_LOCKDEP |
186 | static | |
187 | #endif | |
8ca2b56c | 188 | struct lock_class lock_classes[MAX_LOCKDEP_KEYS]; |
01bb6f0a | 189 | static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS); |
fbb9ce95 | 190 | |
f82b217e DJ |
191 | static inline struct lock_class *hlock_class(struct held_lock *hlock) |
192 | { | |
01bb6f0a YD |
193 | unsigned int class_idx = hlock->class_idx; |
194 | ||
195 | /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfield */ | |
196 | barrier(); | |
197 | ||
198 | if (!test_bit(class_idx, lock_classes_in_use)) { | |
0119fee4 PZ |
199 | /* |
200 | * Someone passed in garbage, we give up. | |
201 | */ | |
f82b217e DJ |
202 | DEBUG_LOCKS_WARN_ON(1); |
203 | return NULL; | |
204 | } | |
01bb6f0a YD |
205 | |
206 | /* | |
207 | * At this point, if the passed hlock->class_idx is still garbage, | |
208 | * we just have to live with it | |
209 | */ | |
210 | return lock_classes + class_idx; | |
f82b217e DJ |
211 | } |
212 | ||
f20786ff | 213 | #ifdef CONFIG_LOCK_STAT |
25528213 | 214 | static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats); |
f20786ff | 215 | |
3365e779 PZ |
216 | static inline u64 lockstat_clock(void) |
217 | { | |
c676329a | 218 | return local_clock(); |
3365e779 PZ |
219 | } |
220 | ||
c7e78cff | 221 | static int lock_point(unsigned long points[], unsigned long ip) |
f20786ff PZ |
222 | { |
223 | int i; | |
224 | ||
c7e78cff PZ |
225 | for (i = 0; i < LOCKSTAT_POINTS; i++) { |
226 | if (points[i] == 0) { | |
227 | points[i] = ip; | |
f20786ff PZ |
228 | break; |
229 | } | |
c7e78cff | 230 | if (points[i] == ip) |
f20786ff PZ |
231 | break; |
232 | } | |
233 | ||
234 | return i; | |
235 | } | |
236 | ||
3365e779 | 237 | static void lock_time_inc(struct lock_time *lt, u64 time) |
f20786ff PZ |
238 | { |
239 | if (time > lt->max) | |
240 | lt->max = time; | |
241 | ||
109d71c6 | 242 | if (time < lt->min || !lt->nr) |
f20786ff PZ |
243 | lt->min = time; |
244 | ||
245 | lt->total += time; | |
246 | lt->nr++; | |
247 | } | |
248 | ||
c46261de PZ |
249 | static inline void lock_time_add(struct lock_time *src, struct lock_time *dst) |
250 | { | |
109d71c6 FR |
251 | if (!src->nr) |
252 | return; | |
253 | ||
254 | if (src->max > dst->max) | |
255 | dst->max = src->max; | |
256 | ||
257 | if (src->min < dst->min || !dst->nr) | |
258 | dst->min = src->min; | |
259 | ||
c46261de PZ |
260 | dst->total += src->total; |
261 | dst->nr += src->nr; | |
262 | } | |
263 | ||
264 | struct lock_class_stats lock_stats(struct lock_class *class) | |
265 | { | |
266 | struct lock_class_stats stats; | |
267 | int cpu, i; | |
268 | ||
269 | memset(&stats, 0, sizeof(struct lock_class_stats)); | |
270 | for_each_possible_cpu(cpu) { | |
271 | struct lock_class_stats *pcs = | |
1871e52c | 272 | &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; |
c46261de PZ |
273 | |
274 | for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++) | |
275 | stats.contention_point[i] += pcs->contention_point[i]; | |
276 | ||
c7e78cff PZ |
277 | for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++) |
278 | stats.contending_point[i] += pcs->contending_point[i]; | |
279 | ||
c46261de PZ |
280 | lock_time_add(&pcs->read_waittime, &stats.read_waittime); |
281 | lock_time_add(&pcs->write_waittime, &stats.write_waittime); | |
282 | ||
283 | lock_time_add(&pcs->read_holdtime, &stats.read_holdtime); | |
284 | lock_time_add(&pcs->write_holdtime, &stats.write_holdtime); | |
96645678 PZ |
285 | |
286 | for (i = 0; i < ARRAY_SIZE(stats.bounces); i++) | |
287 | stats.bounces[i] += pcs->bounces[i]; | |
c46261de PZ |
288 | } |
289 | ||
290 | return stats; | |
291 | } | |
292 | ||
293 | void clear_lock_stats(struct lock_class *class) | |
294 | { | |
295 | int cpu; | |
296 | ||
297 | for_each_possible_cpu(cpu) { | |
298 | struct lock_class_stats *cpu_stats = | |
1871e52c | 299 | &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; |
c46261de PZ |
300 | |
301 | memset(cpu_stats, 0, sizeof(struct lock_class_stats)); | |
302 | } | |
303 | memset(class->contention_point, 0, sizeof(class->contention_point)); | |
c7e78cff | 304 | memset(class->contending_point, 0, sizeof(class->contending_point)); |
c46261de PZ |
305 | } |
306 | ||
f20786ff PZ |
307 | static struct lock_class_stats *get_lock_stats(struct lock_class *class) |
308 | { | |
01f38497 | 309 | return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes]; |
f20786ff PZ |
310 | } |
311 | ||
312 | static void lock_release_holdtime(struct held_lock *hlock) | |
313 | { | |
314 | struct lock_class_stats *stats; | |
3365e779 | 315 | u64 holdtime; |
f20786ff PZ |
316 | |
317 | if (!lock_stat) | |
318 | return; | |
319 | ||
3365e779 | 320 | holdtime = lockstat_clock() - hlock->holdtime_stamp; |
f20786ff | 321 | |
f82b217e | 322 | stats = get_lock_stats(hlock_class(hlock)); |
f20786ff PZ |
323 | if (hlock->read) |
324 | lock_time_inc(&stats->read_holdtime, holdtime); | |
325 | else | |
326 | lock_time_inc(&stats->write_holdtime, holdtime); | |
f20786ff PZ |
327 | } |
328 | #else | |
329 | static inline void lock_release_holdtime(struct held_lock *hlock) | |
330 | { | |
331 | } | |
332 | #endif | |
333 | ||
fbb9ce95 | 334 | /* |
a0b0fd53 BVA |
335 | * We keep a global list of all lock classes. The list is only accessed with |
336 | * the lockdep spinlock lock held. free_lock_classes is a list with free | |
337 | * elements. These elements are linked together by the lock_entry member in | |
338 | * struct lock_class. | |
fbb9ce95 IM |
339 | */ |
340 | LIST_HEAD(all_lock_classes); | |
a0b0fd53 BVA |
341 | static LIST_HEAD(free_lock_classes); |
342 | ||
343 | /** | |
344 | * struct pending_free - information about data structures about to be freed | |
345 | * @zapped: Head of a list with struct lock_class elements. | |
de4643a7 BVA |
346 | * @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements |
347 | * are about to be freed. | |
a0b0fd53 BVA |
348 | */ |
349 | struct pending_free { | |
350 | struct list_head zapped; | |
de4643a7 | 351 | DECLARE_BITMAP(lock_chains_being_freed, MAX_LOCKDEP_CHAINS); |
a0b0fd53 BVA |
352 | }; |
353 | ||
354 | /** | |
355 | * struct delayed_free - data structures used for delayed freeing | |
356 | * | |
357 | * A data structure for delayed freeing of data structures that may be | |
358 | * accessed by RCU readers at the time these were freed. | |
359 | * | |
360 | * @rcu_head: Used to schedule an RCU callback for freeing data structures. | |
361 | * @index: Index of @pf to which freed data structures are added. | |
362 | * @scheduled: Whether or not an RCU callback has been scheduled. | |
363 | * @pf: Array with information about data structures about to be freed. | |
364 | */ | |
365 | static struct delayed_free { | |
366 | struct rcu_head rcu_head; | |
367 | int index; | |
368 | int scheduled; | |
369 | struct pending_free pf[2]; | |
370 | } delayed_free; | |
fbb9ce95 IM |
371 | |
372 | /* | |
373 | * The lockdep classes are in a hash-table as well, for fast lookup: | |
374 | */ | |
375 | #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1) | |
376 | #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS) | |
4b32d0a4 | 377 | #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS) |
fbb9ce95 IM |
378 | #define classhashentry(key) (classhash_table + __classhashfn((key))) |
379 | ||
a63f38cc | 380 | static struct hlist_head classhash_table[CLASSHASH_SIZE]; |
fbb9ce95 | 381 | |
fbb9ce95 IM |
382 | /* |
383 | * We put the lock dependency chains into a hash-table as well, to cache | |
384 | * their existence: | |
385 | */ | |
386 | #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1) | |
387 | #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS) | |
4b32d0a4 | 388 | #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS) |
fbb9ce95 IM |
389 | #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain))) |
390 | ||
a63f38cc | 391 | static struct hlist_head chainhash_table[CHAINHASH_SIZE]; |
fbb9ce95 | 392 | |
f611e8cf BF |
393 | /* |
394 | * the id of held_lock | |
395 | */ | |
396 | static inline u16 hlock_id(struct held_lock *hlock) | |
397 | { | |
398 | BUILD_BUG_ON(MAX_LOCKDEP_KEYS_BITS + 2 > 16); | |
399 | ||
400 | return (hlock->class_idx | (hlock->read << MAX_LOCKDEP_KEYS_BITS)); | |
401 | } | |
402 | ||
403 | static inline unsigned int chain_hlock_class_idx(u16 hlock_id) | |
404 | { | |
405 | return hlock_id & (MAX_LOCKDEP_KEYS - 1); | |
406 | } | |
407 | ||
fbb9ce95 IM |
408 | /* |
409 | * The hash key of the lock dependency chains is a hash itself too: | |
410 | * it's a hash of all locks taken up to that lock, including that lock. | |
411 | * It's a 64-bit hash, because it's important for the keys to be | |
412 | * unique. | |
413 | */ | |
dfaaf3fa PZ |
414 | static inline u64 iterate_chain_key(u64 key, u32 idx) |
415 | { | |
416 | u32 k0 = key, k1 = key >> 32; | |
417 | ||
418 | __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */ | |
419 | ||
420 | return k0 | (u64)k1 << 32; | |
421 | } | |
fbb9ce95 | 422 | |
e196e479 YD |
423 | void lockdep_init_task(struct task_struct *task) |
424 | { | |
425 | task->lockdep_depth = 0; /* no locks held yet */ | |
f6ec8829 | 426 | task->curr_chain_key = INITIAL_CHAIN_KEY; |
e196e479 YD |
427 | task->lockdep_recursion = 0; |
428 | } | |
429 | ||
4d004099 PZ |
430 | static __always_inline void lockdep_recursion_inc(void) |
431 | { | |
432 | __this_cpu_inc(lockdep_recursion); | |
433 | } | |
434 | ||
6eebad1a | 435 | static __always_inline void lockdep_recursion_finish(void) |
10476e63 | 436 | { |
4d004099 PZ |
437 | if (WARN_ON_ONCE(__this_cpu_dec_return(lockdep_recursion))) |
438 | __this_cpu_write(lockdep_recursion, 0); | |
10476e63 PZ |
439 | } |
440 | ||
cdc84d79 BVA |
441 | void lockdep_set_selftest_task(struct task_struct *task) |
442 | { | |
443 | lockdep_selftest_task_struct = task; | |
444 | } | |
445 | ||
fbb9ce95 IM |
446 | /* |
447 | * Debugging switches: | |
448 | */ | |
449 | ||
450 | #define VERBOSE 0 | |
33e94e96 | 451 | #define VERY_VERBOSE 0 |
fbb9ce95 IM |
452 | |
453 | #if VERBOSE | |
454 | # define HARDIRQ_VERBOSE 1 | |
455 | # define SOFTIRQ_VERBOSE 1 | |
456 | #else | |
457 | # define HARDIRQ_VERBOSE 0 | |
458 | # define SOFTIRQ_VERBOSE 0 | |
459 | #endif | |
460 | ||
d92a8cfc | 461 | #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE |
fbb9ce95 IM |
462 | /* |
463 | * Quick filtering for interesting events: | |
464 | */ | |
465 | static int class_filter(struct lock_class *class) | |
466 | { | |
f9829cce AK |
467 | #if 0 |
468 | /* Example */ | |
fbb9ce95 | 469 | if (class->name_version == 1 && |
f9829cce | 470 | !strcmp(class->name, "lockname")) |
fbb9ce95 IM |
471 | return 1; |
472 | if (class->name_version == 1 && | |
f9829cce | 473 | !strcmp(class->name, "&struct->lockfield")) |
fbb9ce95 | 474 | return 1; |
f9829cce | 475 | #endif |
a6640897 IM |
476 | /* Filter everything else. 1 would be to allow everything else */ |
477 | return 0; | |
fbb9ce95 IM |
478 | } |
479 | #endif | |
480 | ||
481 | static int verbose(struct lock_class *class) | |
482 | { | |
483 | #if VERBOSE | |
484 | return class_filter(class); | |
485 | #endif | |
486 | return 0; | |
487 | } | |
488 | ||
2c522836 DJ |
489 | static void print_lockdep_off(const char *bug_msg) |
490 | { | |
491 | printk(KERN_DEBUG "%s\n", bug_msg); | |
492 | printk(KERN_DEBUG "turning off the locking correctness validator.\n"); | |
acf59377 | 493 | #ifdef CONFIG_LOCK_STAT |
2c522836 | 494 | printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n"); |
acf59377 | 495 | #endif |
2c522836 DJ |
496 | } |
497 | ||
886532ae AB |
498 | unsigned long nr_stack_trace_entries; |
499 | ||
30a35f79 | 500 | #ifdef CONFIG_PROVE_LOCKING |
12593b74 BVA |
501 | /** |
502 | * struct lock_trace - single stack backtrace | |
503 | * @hash_entry: Entry in a stack_trace_hash[] list. | |
504 | * @hash: jhash() of @entries. | |
505 | * @nr_entries: Number of entries in @entries. | |
506 | * @entries: Actual stack backtrace. | |
507 | */ | |
508 | struct lock_trace { | |
509 | struct hlist_node hash_entry; | |
510 | u32 hash; | |
511 | u32 nr_entries; | |
db78538c | 512 | unsigned long entries[] __aligned(sizeof(unsigned long)); |
12593b74 BVA |
513 | }; |
514 | #define LOCK_TRACE_SIZE_IN_LONGS \ | |
515 | (sizeof(struct lock_trace) / sizeof(unsigned long)) | |
886532ae | 516 | /* |
12593b74 | 517 | * Stack-trace: sequence of lock_trace structures. Protected by the graph_lock. |
886532ae AB |
518 | */ |
519 | static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES]; | |
12593b74 BVA |
520 | static struct hlist_head stack_trace_hash[STACK_TRACE_HASH_SIZE]; |
521 | ||
522 | static bool traces_identical(struct lock_trace *t1, struct lock_trace *t2) | |
523 | { | |
524 | return t1->hash == t2->hash && t1->nr_entries == t2->nr_entries && | |
525 | memcmp(t1->entries, t2->entries, | |
526 | t1->nr_entries * sizeof(t1->entries[0])) == 0; | |
527 | } | |
886532ae | 528 | |
12593b74 | 529 | static struct lock_trace *save_trace(void) |
fbb9ce95 | 530 | { |
12593b74 BVA |
531 | struct lock_trace *trace, *t2; |
532 | struct hlist_head *hash_head; | |
533 | u32 hash; | |
d91f3057 | 534 | int max_entries; |
fbb9ce95 | 535 | |
12593b74 BVA |
536 | BUILD_BUG_ON_NOT_POWER_OF_2(STACK_TRACE_HASH_SIZE); |
537 | BUILD_BUG_ON(LOCK_TRACE_SIZE_IN_LONGS >= MAX_STACK_TRACE_ENTRIES); | |
538 | ||
539 | trace = (struct lock_trace *)(stack_trace + nr_stack_trace_entries); | |
540 | max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries - | |
541 | LOCK_TRACE_SIZE_IN_LONGS; | |
fbb9ce95 | 542 | |
d91f3057 | 543 | if (max_entries <= 0) { |
74c383f1 | 544 | if (!debug_locks_off_graph_unlock()) |
12593b74 | 545 | return NULL; |
74c383f1 | 546 | |
2c522836 | 547 | print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!"); |
74c383f1 IM |
548 | dump_stack(); |
549 | ||
12593b74 | 550 | return NULL; |
fbb9ce95 | 551 | } |
d91f3057 | 552 | trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3); |
fbb9ce95 | 553 | |
12593b74 BVA |
554 | hash = jhash(trace->entries, trace->nr_entries * |
555 | sizeof(trace->entries[0]), 0); | |
556 | trace->hash = hash; | |
557 | hash_head = stack_trace_hash + (hash & (STACK_TRACE_HASH_SIZE - 1)); | |
558 | hlist_for_each_entry(t2, hash_head, hash_entry) { | |
559 | if (traces_identical(trace, t2)) | |
560 | return t2; | |
561 | } | |
562 | nr_stack_trace_entries += LOCK_TRACE_SIZE_IN_LONGS + trace->nr_entries; | |
563 | hlist_add_head(&trace->hash_entry, hash_head); | |
564 | ||
565 | return trace; | |
fbb9ce95 | 566 | } |
8c779229 BVA |
567 | |
568 | /* Return the number of stack traces in the stack_trace[] array. */ | |
569 | u64 lockdep_stack_trace_count(void) | |
570 | { | |
571 | struct lock_trace *trace; | |
572 | u64 c = 0; | |
573 | int i; | |
574 | ||
575 | for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++) { | |
576 | hlist_for_each_entry(trace, &stack_trace_hash[i], hash_entry) { | |
577 | c++; | |
578 | } | |
579 | } | |
580 | ||
581 | return c; | |
582 | } | |
583 | ||
584 | /* Return the number of stack hash chains that have at least one stack trace. */ | |
585 | u64 lockdep_stack_hash_count(void) | |
586 | { | |
587 | u64 c = 0; | |
588 | int i; | |
589 | ||
590 | for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++) | |
591 | if (!hlist_empty(&stack_trace_hash[i])) | |
592 | c++; | |
593 | ||
594 | return c; | |
fbb9ce95 | 595 | } |
886532ae | 596 | #endif |
fbb9ce95 IM |
597 | |
598 | unsigned int nr_hardirq_chains; | |
599 | unsigned int nr_softirq_chains; | |
600 | unsigned int nr_process_chains; | |
601 | unsigned int max_lockdep_depth; | |
fbb9ce95 IM |
602 | |
603 | #ifdef CONFIG_DEBUG_LOCKDEP | |
fbb9ce95 IM |
604 | /* |
605 | * Various lockdep statistics: | |
606 | */ | |
bd6d29c2 | 607 | DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats); |
fbb9ce95 IM |
608 | #endif |
609 | ||
30a35f79 | 610 | #ifdef CONFIG_PROVE_LOCKING |
fbb9ce95 IM |
611 | /* |
612 | * Locking printouts: | |
613 | */ | |
614 | ||
fabe9c42 | 615 | #define __USAGE(__STATE) \ |
b4b136f4 PZ |
616 | [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \ |
617 | [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \ | |
618 | [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\ | |
619 | [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R", | |
fabe9c42 | 620 | |
fbb9ce95 IM |
621 | static const char *usage_str[] = |
622 | { | |
fabe9c42 PZ |
623 | #define LOCKDEP_STATE(__STATE) __USAGE(__STATE) |
624 | #include "lockdep_states.h" | |
625 | #undef LOCKDEP_STATE | |
626 | [LOCK_USED] = "INITIAL USE", | |
2bb8945b PZ |
627 | [LOCK_USED_READ] = "INITIAL READ USE", |
628 | /* abused as string storage for verify_lock_unused() */ | |
f6f48e18 | 629 | [LOCK_USAGE_STATES] = "IN-NMI", |
fbb9ce95 | 630 | }; |
886532ae | 631 | #endif |
fbb9ce95 | 632 | |
364f6afc | 633 | const char *__get_key_name(const struct lockdep_subclass_key *key, char *str) |
fbb9ce95 | 634 | { |
ffb45122 | 635 | return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str); |
fbb9ce95 IM |
636 | } |
637 | ||
3ff176ca | 638 | static inline unsigned long lock_flag(enum lock_usage_bit bit) |
fbb9ce95 | 639 | { |
3ff176ca PZ |
640 | return 1UL << bit; |
641 | } | |
fbb9ce95 | 642 | |
3ff176ca PZ |
643 | static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit) |
644 | { | |
c52478f4 YD |
645 | /* |
646 | * The usage character defaults to '.' (i.e., irqs disabled and not in | |
647 | * irq context), which is the safest usage category. | |
648 | */ | |
3ff176ca PZ |
649 | char c = '.'; |
650 | ||
c52478f4 YD |
651 | /* |
652 | * The order of the following usage checks matters, which will | |
653 | * result in the outcome character as follows: | |
654 | * | |
655 | * - '+': irq is enabled and not in irq context | |
656 | * - '-': in irq context and irq is disabled | |
657 | * - '?': in irq context and irq is enabled | |
658 | */ | |
659 | if (class->usage_mask & lock_flag(bit + LOCK_USAGE_DIR_MASK)) { | |
3ff176ca | 660 | c = '+'; |
c52478f4 | 661 | if (class->usage_mask & lock_flag(bit)) |
3ff176ca | 662 | c = '?'; |
c52478f4 YD |
663 | } else if (class->usage_mask & lock_flag(bit)) |
664 | c = '-'; | |
fbb9ce95 | 665 | |
3ff176ca PZ |
666 | return c; |
667 | } | |
cf40bd16 | 668 | |
f510b233 | 669 | void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS]) |
3ff176ca | 670 | { |
f510b233 | 671 | int i = 0; |
cf40bd16 | 672 | |
f510b233 PZ |
673 | #define LOCKDEP_STATE(__STATE) \ |
674 | usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \ | |
675 | usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ); | |
676 | #include "lockdep_states.h" | |
677 | #undef LOCKDEP_STATE | |
678 | ||
679 | usage[i] = '\0'; | |
fbb9ce95 IM |
680 | } |
681 | ||
e5e78d08 | 682 | static void __print_lock_name(struct lock_class *class) |
3003eba3 SR |
683 | { |
684 | char str[KSYM_NAME_LEN]; | |
685 | const char *name; | |
686 | ||
fbb9ce95 IM |
687 | name = class->name; |
688 | if (!name) { | |
689 | name = __get_key_name(class->key, str); | |
f943fe0f | 690 | printk(KERN_CONT "%s", name); |
fbb9ce95 | 691 | } else { |
f943fe0f | 692 | printk(KERN_CONT "%s", name); |
fbb9ce95 | 693 | if (class->name_version > 1) |
f943fe0f | 694 | printk(KERN_CONT "#%d", class->name_version); |
fbb9ce95 | 695 | if (class->subclass) |
f943fe0f | 696 | printk(KERN_CONT "/%d", class->subclass); |
fbb9ce95 | 697 | } |
e5e78d08 SR |
698 | } |
699 | ||
700 | static void print_lock_name(struct lock_class *class) | |
701 | { | |
702 | char usage[LOCK_USAGE_CHARS]; | |
703 | ||
704 | get_usage_chars(class, usage); | |
705 | ||
f943fe0f | 706 | printk(KERN_CONT " ("); |
e5e78d08 | 707 | __print_lock_name(class); |
de8f5e4f PZ |
708 | printk(KERN_CONT "){%s}-{%hd:%hd}", usage, |
709 | class->wait_type_outer ?: class->wait_type_inner, | |
710 | class->wait_type_inner); | |
fbb9ce95 IM |
711 | } |
712 | ||
713 | static void print_lockdep_cache(struct lockdep_map *lock) | |
714 | { | |
715 | const char *name; | |
9281acea | 716 | char str[KSYM_NAME_LEN]; |
fbb9ce95 IM |
717 | |
718 | name = lock->name; | |
719 | if (!name) | |
720 | name = __get_key_name(lock->key->subkeys, str); | |
721 | ||
f943fe0f | 722 | printk(KERN_CONT "%s", name); |
fbb9ce95 IM |
723 | } |
724 | ||
725 | static void print_lock(struct held_lock *hlock) | |
726 | { | |
d7bc3197 PZ |
727 | /* |
728 | * We can be called locklessly through debug_show_all_locks() so be | |
729 | * extra careful, the hlock might have been released and cleared. | |
01bb6f0a YD |
730 | * |
731 | * If this indeed happens, lets pretend it does not hurt to continue | |
732 | * to print the lock unless the hlock class_idx does not point to a | |
733 | * registered class. The rationale here is: since we don't attempt | |
734 | * to distinguish whether we are in this situation, if it just | |
735 | * happened we can't count on class_idx to tell either. | |
d7bc3197 | 736 | */ |
01bb6f0a | 737 | struct lock_class *lock = hlock_class(hlock); |
d7bc3197 | 738 | |
01bb6f0a | 739 | if (!lock) { |
f943fe0f | 740 | printk(KERN_CONT "<RELEASED>\n"); |
d7bc3197 PZ |
741 | return; |
742 | } | |
743 | ||
519248f3 | 744 | printk(KERN_CONT "%px", hlock->instance); |
01bb6f0a | 745 | print_lock_name(lock); |
b3c39758 | 746 | printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip); |
fbb9ce95 IM |
747 | } |
748 | ||
8cc05c71 | 749 | static void lockdep_print_held_locks(struct task_struct *p) |
fbb9ce95 | 750 | { |
8cc05c71 | 751 | int i, depth = READ_ONCE(p->lockdep_depth); |
fbb9ce95 | 752 | |
8cc05c71 TH |
753 | if (!depth) |
754 | printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p)); | |
755 | else | |
756 | printk("%d lock%s held by %s/%d:\n", depth, | |
757 | depth > 1 ? "s" : "", p->comm, task_pid_nr(p)); | |
758 | /* | |
759 | * It's not reliable to print a task's held locks if it's not sleeping | |
760 | * and it's not the current task. | |
761 | */ | |
762 | if (p->state == TASK_RUNNING && p != current) | |
fbb9ce95 | 763 | return; |
fbb9ce95 IM |
764 | for (i = 0; i < depth; i++) { |
765 | printk(" #%d: ", i); | |
8cc05c71 | 766 | print_lock(p->held_locks + i); |
fbb9ce95 IM |
767 | } |
768 | } | |
fbb9ce95 | 769 | |
fbdc4b9a | 770 | static void print_kernel_ident(void) |
8e18257d | 771 | { |
fbdc4b9a | 772 | printk("%s %.*s %s\n", init_utsname()->release, |
8e18257d | 773 | (int)strcspn(init_utsname()->version, " "), |
fbdc4b9a BH |
774 | init_utsname()->version, |
775 | print_tainted()); | |
8e18257d PZ |
776 | } |
777 | ||
778 | static int very_verbose(struct lock_class *class) | |
779 | { | |
780 | #if VERY_VERBOSE | |
781 | return class_filter(class); | |
782 | #endif | |
783 | return 0; | |
784 | } | |
785 | ||
fbb9ce95 | 786 | /* |
8e18257d | 787 | * Is this the address of a static object: |
fbb9ce95 | 788 | */ |
8dce7a9a | 789 | #ifdef __KERNEL__ |
108c1485 | 790 | static int static_obj(const void *obj) |
fbb9ce95 | 791 | { |
8e18257d PZ |
792 | unsigned long start = (unsigned long) &_stext, |
793 | end = (unsigned long) &_end, | |
794 | addr = (unsigned long) obj; | |
8e18257d | 795 | |
7a5da02d GS |
796 | if (arch_is_kernel_initmem_freed(addr)) |
797 | return 0; | |
798 | ||
fbb9ce95 | 799 | /* |
8e18257d | 800 | * static variable? |
fbb9ce95 | 801 | */ |
8e18257d PZ |
802 | if ((addr >= start) && (addr < end)) |
803 | return 1; | |
fbb9ce95 | 804 | |
2a9ad18d MF |
805 | if (arch_is_kernel_data(addr)) |
806 | return 1; | |
807 | ||
fbb9ce95 | 808 | /* |
10fad5e4 | 809 | * in-kernel percpu var? |
fbb9ce95 | 810 | */ |
10fad5e4 TH |
811 | if (is_kernel_percpu_address(addr)) |
812 | return 1; | |
fbb9ce95 | 813 | |
8e18257d | 814 | /* |
10fad5e4 | 815 | * module static or percpu var? |
8e18257d | 816 | */ |
10fad5e4 | 817 | return is_module_address(addr) || is_module_percpu_address(addr); |
99de055a | 818 | } |
8dce7a9a | 819 | #endif |
99de055a | 820 | |
fbb9ce95 | 821 | /* |
8e18257d | 822 | * To make lock name printouts unique, we calculate a unique |
fe27b0de BVA |
823 | * class->name_version generation counter. The caller must hold the graph |
824 | * lock. | |
fbb9ce95 | 825 | */ |
8e18257d | 826 | static int count_matching_names(struct lock_class *new_class) |
fbb9ce95 | 827 | { |
8e18257d PZ |
828 | struct lock_class *class; |
829 | int count = 0; | |
fbb9ce95 | 830 | |
8e18257d | 831 | if (!new_class->name) |
fbb9ce95 IM |
832 | return 0; |
833 | ||
fe27b0de | 834 | list_for_each_entry(class, &all_lock_classes, lock_entry) { |
8e18257d PZ |
835 | if (new_class->key - new_class->subclass == class->key) |
836 | return class->name_version; | |
837 | if (class->name && !strcmp(class->name, new_class->name)) | |
838 | count = max(count, class->name_version); | |
839 | } | |
fbb9ce95 | 840 | |
8e18257d | 841 | return count + 1; |
fbb9ce95 IM |
842 | } |
843 | ||
f6f48e18 | 844 | /* used from NMI context -- must be lockless */ |
6eebad1a | 845 | static __always_inline struct lock_class * |
08f36ff6 | 846 | look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass) |
fbb9ce95 | 847 | { |
8e18257d | 848 | struct lockdep_subclass_key *key; |
a63f38cc | 849 | struct hlist_head *hash_head; |
8e18257d | 850 | struct lock_class *class; |
fbb9ce95 | 851 | |
4ba053c0 HM |
852 | if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) { |
853 | debug_locks_off(); | |
854 | printk(KERN_ERR | |
855 | "BUG: looking up invalid subclass: %u\n", subclass); | |
856 | printk(KERN_ERR | |
857 | "turning off the locking correctness validator.\n"); | |
858 | dump_stack(); | |
859 | return NULL; | |
860 | } | |
861 | ||
8e18257d | 862 | /* |
64f29d1b MW |
863 | * If it is not initialised then it has never been locked, |
864 | * so it won't be present in the hash table. | |
8e18257d | 865 | */ |
64f29d1b MW |
866 | if (unlikely(!lock->key)) |
867 | return NULL; | |
fbb9ce95 | 868 | |
8e18257d PZ |
869 | /* |
870 | * NOTE: the class-key must be unique. For dynamic locks, a static | |
871 | * lock_class_key variable is passed in through the mutex_init() | |
872 | * (or spin_lock_init()) call - which acts as the key. For static | |
873 | * locks we use the lock object itself as the key. | |
874 | */ | |
4b32d0a4 PZ |
875 | BUILD_BUG_ON(sizeof(struct lock_class_key) > |
876 | sizeof(struct lockdep_map)); | |
fbb9ce95 | 877 | |
8e18257d | 878 | key = lock->key->subkeys + subclass; |
ca268c69 | 879 | |
8e18257d | 880 | hash_head = classhashentry(key); |
74c383f1 | 881 | |
8e18257d | 882 | /* |
35a9393c | 883 | * We do an RCU walk of the hash, see lockdep_free_key_range(). |
8e18257d | 884 | */ |
35a9393c PZ |
885 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
886 | return NULL; | |
887 | ||
a63f38cc | 888 | hlist_for_each_entry_rcu(class, hash_head, hash_entry) { |
4b32d0a4 | 889 | if (class->key == key) { |
0119fee4 PZ |
890 | /* |
891 | * Huh! same key, different name? Did someone trample | |
892 | * on some memory? We're most confused. | |
893 | */ | |
97831546 SAS |
894 | WARN_ON_ONCE(class->name != lock->name && |
895 | lock->key != &__lockdep_no_validate__); | |
8e18257d | 896 | return class; |
4b32d0a4 PZ |
897 | } |
898 | } | |
fbb9ce95 | 899 | |
64f29d1b MW |
900 | return NULL; |
901 | } | |
902 | ||
903 | /* | |
904 | * Static locks do not have their class-keys yet - for them the key is | |
905 | * the lock object itself. If the lock is in the per cpu area, the | |
906 | * canonical address of the lock (per cpu offset removed) is used. | |
907 | */ | |
908 | static bool assign_lock_key(struct lockdep_map *lock) | |
909 | { | |
910 | unsigned long can_addr, addr = (unsigned long)lock; | |
911 | ||
4bf50862 BVA |
912 | #ifdef __KERNEL__ |
913 | /* | |
914 | * lockdep_free_key_range() assumes that struct lock_class_key | |
915 | * objects do not overlap. Since we use the address of lock | |
916 | * objects as class key for static objects, check whether the | |
917 | * size of lock_class_key objects does not exceed the size of | |
918 | * the smallest lock object. | |
919 | */ | |
920 | BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(raw_spinlock_t)); | |
921 | #endif | |
922 | ||
64f29d1b MW |
923 | if (__is_kernel_percpu_address(addr, &can_addr)) |
924 | lock->key = (void *)can_addr; | |
925 | else if (__is_module_percpu_address(addr, &can_addr)) | |
926 | lock->key = (void *)can_addr; | |
927 | else if (static_obj(lock)) | |
928 | lock->key = (void *)lock; | |
929 | else { | |
930 | /* Debug-check: all keys must be persistent! */ | |
931 | debug_locks_off(); | |
932 | pr_err("INFO: trying to register non-static key.\n"); | |
933 | pr_err("the code is fine but needs lockdep annotation.\n"); | |
934 | pr_err("turning off the locking correctness validator.\n"); | |
935 | dump_stack(); | |
936 | return false; | |
937 | } | |
938 | ||
939 | return true; | |
fbb9ce95 IM |
940 | } |
941 | ||
72dcd505 PZ |
942 | #ifdef CONFIG_DEBUG_LOCKDEP |
943 | ||
b526b2e3 BVA |
944 | /* Check whether element @e occurs in list @h */ |
945 | static bool in_list(struct list_head *e, struct list_head *h) | |
946 | { | |
947 | struct list_head *f; | |
948 | ||
949 | list_for_each(f, h) { | |
950 | if (e == f) | |
951 | return true; | |
952 | } | |
953 | ||
954 | return false; | |
955 | } | |
956 | ||
957 | /* | |
958 | * Check whether entry @e occurs in any of the locks_after or locks_before | |
959 | * lists. | |
960 | */ | |
961 | static bool in_any_class_list(struct list_head *e) | |
962 | { | |
963 | struct lock_class *class; | |
964 | int i; | |
965 | ||
966 | for (i = 0; i < ARRAY_SIZE(lock_classes); i++) { | |
967 | class = &lock_classes[i]; | |
968 | if (in_list(e, &class->locks_after) || | |
969 | in_list(e, &class->locks_before)) | |
970 | return true; | |
971 | } | |
972 | return false; | |
973 | } | |
974 | ||
975 | static bool class_lock_list_valid(struct lock_class *c, struct list_head *h) | |
976 | { | |
977 | struct lock_list *e; | |
978 | ||
979 | list_for_each_entry(e, h, entry) { | |
980 | if (e->links_to != c) { | |
981 | printk(KERN_INFO "class %s: mismatch for lock entry %ld; class %s <> %s", | |
982 | c->name ? : "(?)", | |
983 | (unsigned long)(e - list_entries), | |
984 | e->links_to && e->links_to->name ? | |
985 | e->links_to->name : "(?)", | |
986 | e->class && e->class->name ? e->class->name : | |
987 | "(?)"); | |
988 | return false; | |
989 | } | |
990 | } | |
991 | return true; | |
992 | } | |
993 | ||
3fe7522f AB |
994 | #ifdef CONFIG_PROVE_LOCKING |
995 | static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS]; | |
996 | #endif | |
b526b2e3 BVA |
997 | |
998 | static bool check_lock_chain_key(struct lock_chain *chain) | |
999 | { | |
1000 | #ifdef CONFIG_PROVE_LOCKING | |
f6ec8829 | 1001 | u64 chain_key = INITIAL_CHAIN_KEY; |
b526b2e3 BVA |
1002 | int i; |
1003 | ||
1004 | for (i = chain->base; i < chain->base + chain->depth; i++) | |
01bb6f0a | 1005 | chain_key = iterate_chain_key(chain_key, chain_hlocks[i]); |
b526b2e3 BVA |
1006 | /* |
1007 | * The 'unsigned long long' casts avoid that a compiler warning | |
1008 | * is reported when building tools/lib/lockdep. | |
1009 | */ | |
72dcd505 | 1010 | if (chain->chain_key != chain_key) { |
b526b2e3 BVA |
1011 | printk(KERN_INFO "chain %lld: key %#llx <> %#llx\n", |
1012 | (unsigned long long)(chain - lock_chains), | |
1013 | (unsigned long long)chain->chain_key, | |
1014 | (unsigned long long)chain_key); | |
72dcd505 PZ |
1015 | return false; |
1016 | } | |
b526b2e3 | 1017 | #endif |
72dcd505 | 1018 | return true; |
b526b2e3 BVA |
1019 | } |
1020 | ||
1021 | static bool in_any_zapped_class_list(struct lock_class *class) | |
1022 | { | |
1023 | struct pending_free *pf; | |
1024 | int i; | |
1025 | ||
72dcd505 | 1026 | for (i = 0, pf = delayed_free.pf; i < ARRAY_SIZE(delayed_free.pf); i++, pf++) { |
b526b2e3 BVA |
1027 | if (in_list(&class->lock_entry, &pf->zapped)) |
1028 | return true; | |
72dcd505 | 1029 | } |
b526b2e3 BVA |
1030 | |
1031 | return false; | |
1032 | } | |
1033 | ||
72dcd505 | 1034 | static bool __check_data_structures(void) |
b526b2e3 BVA |
1035 | { |
1036 | struct lock_class *class; | |
1037 | struct lock_chain *chain; | |
1038 | struct hlist_head *head; | |
1039 | struct lock_list *e; | |
1040 | int i; | |
1041 | ||
1042 | /* Check whether all classes occur in a lock list. */ | |
1043 | for (i = 0; i < ARRAY_SIZE(lock_classes); i++) { | |
1044 | class = &lock_classes[i]; | |
1045 | if (!in_list(&class->lock_entry, &all_lock_classes) && | |
1046 | !in_list(&class->lock_entry, &free_lock_classes) && | |
1047 | !in_any_zapped_class_list(class)) { | |
1048 | printk(KERN_INFO "class %px/%s is not in any class list\n", | |
1049 | class, class->name ? : "(?)"); | |
1050 | return false; | |
b526b2e3 BVA |
1051 | } |
1052 | } | |
1053 | ||
1054 | /* Check whether all classes have valid lock lists. */ | |
1055 | for (i = 0; i < ARRAY_SIZE(lock_classes); i++) { | |
1056 | class = &lock_classes[i]; | |
1057 | if (!class_lock_list_valid(class, &class->locks_before)) | |
1058 | return false; | |
1059 | if (!class_lock_list_valid(class, &class->locks_after)) | |
1060 | return false; | |
1061 | } | |
1062 | ||
1063 | /* Check the chain_key of all lock chains. */ | |
1064 | for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) { | |
1065 | head = chainhash_table + i; | |
1066 | hlist_for_each_entry_rcu(chain, head, entry) { | |
1067 | if (!check_lock_chain_key(chain)) | |
1068 | return false; | |
1069 | } | |
1070 | } | |
1071 | ||
1072 | /* | |
1073 | * Check whether all list entries that are in use occur in a class | |
1074 | * lock list. | |
1075 | */ | |
1076 | for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) { | |
1077 | e = list_entries + i; | |
1078 | if (!in_any_class_list(&e->entry)) { | |
1079 | printk(KERN_INFO "list entry %d is not in any class list; class %s <> %s\n", | |
1080 | (unsigned int)(e - list_entries), | |
1081 | e->class->name ? : "(?)", | |
1082 | e->links_to->name ? : "(?)"); | |
1083 | return false; | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | /* | |
1088 | * Check whether all list entries that are not in use do not occur in | |
1089 | * a class lock list. | |
1090 | */ | |
1091 | for_each_clear_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) { | |
1092 | e = list_entries + i; | |
1093 | if (in_any_class_list(&e->entry)) { | |
1094 | printk(KERN_INFO "list entry %d occurs in a class list; class %s <> %s\n", | |
1095 | (unsigned int)(e - list_entries), | |
1096 | e->class && e->class->name ? e->class->name : | |
1097 | "(?)", | |
1098 | e->links_to && e->links_to->name ? | |
1099 | e->links_to->name : "(?)"); | |
1100 | return false; | |
1101 | } | |
1102 | } | |
1103 | ||
1104 | return true; | |
1105 | } | |
1106 | ||
72dcd505 PZ |
1107 | int check_consistency = 0; |
1108 | module_param(check_consistency, int, 0644); | |
1109 | ||
1110 | static void check_data_structures(void) | |
1111 | { | |
1112 | static bool once = false; | |
1113 | ||
1114 | if (check_consistency && !once) { | |
1115 | if (!__check_data_structures()) { | |
1116 | once = true; | |
1117 | WARN_ON(once); | |
1118 | } | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | #else /* CONFIG_DEBUG_LOCKDEP */ | |
1123 | ||
1124 | static inline void check_data_structures(void) { } | |
1125 | ||
1126 | #endif /* CONFIG_DEBUG_LOCKDEP */ | |
1127 | ||
810507fe WL |
1128 | static void init_chain_block_buckets(void); |
1129 | ||
feb0a386 | 1130 | /* |
a0b0fd53 BVA |
1131 | * Initialize the lock_classes[] array elements, the free_lock_classes list |
1132 | * and also the delayed_free structure. | |
feb0a386 BVA |
1133 | */ |
1134 | static void init_data_structures_once(void) | |
1135 | { | |
810507fe | 1136 | static bool __read_mostly ds_initialized, rcu_head_initialized; |
feb0a386 BVA |
1137 | int i; |
1138 | ||
0126574f | 1139 | if (likely(rcu_head_initialized)) |
feb0a386 BVA |
1140 | return; |
1141 | ||
0126574f BVA |
1142 | if (system_state >= SYSTEM_SCHEDULING) { |
1143 | init_rcu_head(&delayed_free.rcu_head); | |
1144 | rcu_head_initialized = true; | |
1145 | } | |
1146 | ||
1147 | if (ds_initialized) | |
1148 | return; | |
1149 | ||
1150 | ds_initialized = true; | |
feb0a386 | 1151 | |
a0b0fd53 BVA |
1152 | INIT_LIST_HEAD(&delayed_free.pf[0].zapped); |
1153 | INIT_LIST_HEAD(&delayed_free.pf[1].zapped); | |
1154 | ||
feb0a386 | 1155 | for (i = 0; i < ARRAY_SIZE(lock_classes); i++) { |
a0b0fd53 | 1156 | list_add_tail(&lock_classes[i].lock_entry, &free_lock_classes); |
feb0a386 BVA |
1157 | INIT_LIST_HEAD(&lock_classes[i].locks_after); |
1158 | INIT_LIST_HEAD(&lock_classes[i].locks_before); | |
1159 | } | |
810507fe | 1160 | init_chain_block_buckets(); |
feb0a386 BVA |
1161 | } |
1162 | ||
108c1485 BVA |
1163 | static inline struct hlist_head *keyhashentry(const struct lock_class_key *key) |
1164 | { | |
1165 | unsigned long hash = hash_long((uintptr_t)key, KEYHASH_BITS); | |
1166 | ||
1167 | return lock_keys_hash + hash; | |
1168 | } | |
1169 | ||
1170 | /* Register a dynamically allocated key. */ | |
1171 | void lockdep_register_key(struct lock_class_key *key) | |
1172 | { | |
1173 | struct hlist_head *hash_head; | |
1174 | struct lock_class_key *k; | |
1175 | unsigned long flags; | |
1176 | ||
1177 | if (WARN_ON_ONCE(static_obj(key))) | |
1178 | return; | |
1179 | hash_head = keyhashentry(key); | |
1180 | ||
1181 | raw_local_irq_save(flags); | |
1182 | if (!graph_lock()) | |
1183 | goto restore_irqs; | |
1184 | hlist_for_each_entry_rcu(k, hash_head, hash_entry) { | |
1185 | if (WARN_ON_ONCE(k == key)) | |
1186 | goto out_unlock; | |
1187 | } | |
1188 | hlist_add_head_rcu(&key->hash_entry, hash_head); | |
1189 | out_unlock: | |
1190 | graph_unlock(); | |
1191 | restore_irqs: | |
1192 | raw_local_irq_restore(flags); | |
1193 | } | |
1194 | EXPORT_SYMBOL_GPL(lockdep_register_key); | |
1195 | ||
1196 | /* Check whether a key has been registered as a dynamic key. */ | |
1197 | static bool is_dynamic_key(const struct lock_class_key *key) | |
1198 | { | |
1199 | struct hlist_head *hash_head; | |
1200 | struct lock_class_key *k; | |
1201 | bool found = false; | |
1202 | ||
1203 | if (WARN_ON_ONCE(static_obj(key))) | |
1204 | return false; | |
1205 | ||
1206 | /* | |
1207 | * If lock debugging is disabled lock_keys_hash[] may contain | |
1208 | * pointers to memory that has already been freed. Avoid triggering | |
1209 | * a use-after-free in that case by returning early. | |
1210 | */ | |
1211 | if (!debug_locks) | |
1212 | return true; | |
1213 | ||
1214 | hash_head = keyhashentry(key); | |
1215 | ||
1216 | rcu_read_lock(); | |
1217 | hlist_for_each_entry_rcu(k, hash_head, hash_entry) { | |
1218 | if (k == key) { | |
1219 | found = true; | |
1220 | break; | |
1221 | } | |
1222 | } | |
1223 | rcu_read_unlock(); | |
1224 | ||
1225 | return found; | |
1226 | } | |
1227 | ||
fbb9ce95 | 1228 | /* |
8e18257d PZ |
1229 | * Register a lock's class in the hash-table, if the class is not present |
1230 | * yet. Otherwise we look it up. We cache the result in the lock object | |
1231 | * itself, so actual lookup of the hash should be once per lock object. | |
fbb9ce95 | 1232 | */ |
c003ed92 | 1233 | static struct lock_class * |
8e18257d | 1234 | register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force) |
fbb9ce95 | 1235 | { |
8e18257d | 1236 | struct lockdep_subclass_key *key; |
a63f38cc | 1237 | struct hlist_head *hash_head; |
8e18257d | 1238 | struct lock_class *class; |
35a9393c PZ |
1239 | |
1240 | DEBUG_LOCKS_WARN_ON(!irqs_disabled()); | |
8e18257d PZ |
1241 | |
1242 | class = look_up_lock_class(lock, subclass); | |
64f29d1b | 1243 | if (likely(class)) |
87cdee71 | 1244 | goto out_set_class_cache; |
8e18257d | 1245 | |
64f29d1b MW |
1246 | if (!lock->key) { |
1247 | if (!assign_lock_key(lock)) | |
1248 | return NULL; | |
108c1485 | 1249 | } else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) { |
8e18257d PZ |
1250 | return NULL; |
1251 | } | |
1252 | ||
1253 | key = lock->key->subkeys + subclass; | |
1254 | hash_head = classhashentry(key); | |
1255 | ||
8e18257d | 1256 | if (!graph_lock()) { |
8e18257d PZ |
1257 | return NULL; |
1258 | } | |
1259 | /* | |
1260 | * We have to do the hash-walk again, to avoid races | |
1261 | * with another CPU: | |
1262 | */ | |
a63f38cc | 1263 | hlist_for_each_entry_rcu(class, hash_head, hash_entry) { |
8e18257d PZ |
1264 | if (class->key == key) |
1265 | goto out_unlock_set; | |
35a9393c PZ |
1266 | } |
1267 | ||
feb0a386 BVA |
1268 | init_data_structures_once(); |
1269 | ||
a0b0fd53 BVA |
1270 | /* Allocate a new lock class and add it to the hash. */ |
1271 | class = list_first_entry_or_null(&free_lock_classes, typeof(*class), | |
1272 | lock_entry); | |
1273 | if (!class) { | |
8e18257d | 1274 | if (!debug_locks_off_graph_unlock()) { |
8e18257d PZ |
1275 | return NULL; |
1276 | } | |
8e18257d | 1277 | |
2c522836 | 1278 | print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!"); |
eedeeabd | 1279 | dump_stack(); |
8e18257d PZ |
1280 | return NULL; |
1281 | } | |
a0b0fd53 | 1282 | nr_lock_classes++; |
01bb6f0a | 1283 | __set_bit(class - lock_classes, lock_classes_in_use); |
bd6d29c2 | 1284 | debug_atomic_inc(nr_unused_locks); |
8e18257d PZ |
1285 | class->key = key; |
1286 | class->name = lock->name; | |
1287 | class->subclass = subclass; | |
feb0a386 BVA |
1288 | WARN_ON_ONCE(!list_empty(&class->locks_before)); |
1289 | WARN_ON_ONCE(!list_empty(&class->locks_after)); | |
8e18257d | 1290 | class->name_version = count_matching_names(class); |
de8f5e4f PZ |
1291 | class->wait_type_inner = lock->wait_type_inner; |
1292 | class->wait_type_outer = lock->wait_type_outer; | |
8e18257d PZ |
1293 | /* |
1294 | * We use RCU's safe list-add method to make | |
1295 | * parallel walking of the hash-list safe: | |
1296 | */ | |
a63f38cc | 1297 | hlist_add_head_rcu(&class->hash_entry, hash_head); |
1481197b | 1298 | /* |
a0b0fd53 BVA |
1299 | * Remove the class from the free list and add it to the global list |
1300 | * of classes. | |
1481197b | 1301 | */ |
a0b0fd53 | 1302 | list_move_tail(&class->lock_entry, &all_lock_classes); |
8e18257d PZ |
1303 | |
1304 | if (verbose(class)) { | |
1305 | graph_unlock(); | |
8e18257d | 1306 | |
04860d48 | 1307 | printk("\nnew class %px: %s", class->key, class->name); |
8e18257d | 1308 | if (class->name_version > 1) |
f943fe0f DV |
1309 | printk(KERN_CONT "#%d", class->name_version); |
1310 | printk(KERN_CONT "\n"); | |
8e18257d PZ |
1311 | dump_stack(); |
1312 | ||
8e18257d | 1313 | if (!graph_lock()) { |
8e18257d PZ |
1314 | return NULL; |
1315 | } | |
1316 | } | |
1317 | out_unlock_set: | |
1318 | graph_unlock(); | |
8e18257d | 1319 | |
87cdee71 | 1320 | out_set_class_cache: |
8e18257d | 1321 | if (!subclass || force) |
62016250 HM |
1322 | lock->class_cache[0] = class; |
1323 | else if (subclass < NR_LOCKDEP_CACHING_CLASSES) | |
1324 | lock->class_cache[subclass] = class; | |
8e18257d | 1325 | |
0119fee4 PZ |
1326 | /* |
1327 | * Hash collision, did we smoke some? We found a class with a matching | |
1328 | * hash but the subclass -- which is hashed in -- didn't match. | |
1329 | */ | |
8e18257d PZ |
1330 | if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass)) |
1331 | return NULL; | |
1332 | ||
1333 | return class; | |
1334 | } | |
1335 | ||
1336 | #ifdef CONFIG_PROVE_LOCKING | |
1337 | /* | |
1338 | * Allocate a lockdep entry. (assumes the graph_lock held, returns | |
1339 | * with NULL on failure) | |
1340 | */ | |
1341 | static struct lock_list *alloc_list_entry(void) | |
1342 | { | |
ace35a7a BVA |
1343 | int idx = find_first_zero_bit(list_entries_in_use, |
1344 | ARRAY_SIZE(list_entries)); | |
1345 | ||
1346 | if (idx >= ARRAY_SIZE(list_entries)) { | |
8e18257d PZ |
1347 | if (!debug_locks_off_graph_unlock()) |
1348 | return NULL; | |
1349 | ||
2c522836 | 1350 | print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!"); |
eedeeabd | 1351 | dump_stack(); |
8e18257d PZ |
1352 | return NULL; |
1353 | } | |
ace35a7a BVA |
1354 | nr_list_entries++; |
1355 | __set_bit(idx, list_entries_in_use); | |
1356 | return list_entries + idx; | |
8e18257d PZ |
1357 | } |
1358 | ||
1359 | /* | |
1360 | * Add a new dependency to the head of the list: | |
1361 | */ | |
86cffb80 BVA |
1362 | static int add_lock_to_list(struct lock_class *this, |
1363 | struct lock_class *links_to, struct list_head *head, | |
3454a36d | 1364 | unsigned long ip, u16 distance, u8 dep, |
12593b74 | 1365 | const struct lock_trace *trace) |
8e18257d PZ |
1366 | { |
1367 | struct lock_list *entry; | |
1368 | /* | |
1369 | * Lock not present yet - get a new dependency struct and | |
1370 | * add it to the list: | |
1371 | */ | |
1372 | entry = alloc_list_entry(); | |
1373 | if (!entry) | |
1374 | return 0; | |
1375 | ||
74870172 | 1376 | entry->class = this; |
86cffb80 | 1377 | entry->links_to = links_to; |
3454a36d | 1378 | entry->dep = dep; |
74870172 | 1379 | entry->distance = distance; |
12593b74 | 1380 | entry->trace = trace; |
8e18257d | 1381 | /* |
35a9393c PZ |
1382 | * Both allocation and removal are done under the graph lock; but |
1383 | * iteration is under RCU-sched; see look_up_lock_class() and | |
1384 | * lockdep_free_key_range(). | |
8e18257d PZ |
1385 | */ |
1386 | list_add_tail_rcu(&entry->entry, head); | |
1387 | ||
1388 | return 1; | |
1389 | } | |
1390 | ||
98c33edd PZ |
1391 | /* |
1392 | * For good efficiency of modular, we use power of 2 | |
1393 | */ | |
af012961 PZ |
1394 | #define MAX_CIRCULAR_QUEUE_SIZE 4096UL |
1395 | #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1) | |
1396 | ||
98c33edd | 1397 | /* |
aa480771 YD |
1398 | * The circular_queue and helpers are used to implement graph |
1399 | * breadth-first search (BFS) algorithm, by which we can determine | |
1400 | * whether there is a path from a lock to another. In deadlock checks, | |
1401 | * a path from the next lock to be acquired to a previous held lock | |
1402 | * indicates that adding the <prev> -> <next> lock dependency will | |
1403 | * produce a circle in the graph. Breadth-first search instead of | |
1404 | * depth-first search is used in order to find the shortest (circular) | |
1405 | * path. | |
98c33edd | 1406 | */ |
af012961 | 1407 | struct circular_queue { |
aa480771 | 1408 | struct lock_list *element[MAX_CIRCULAR_QUEUE_SIZE]; |
af012961 PZ |
1409 | unsigned int front, rear; |
1410 | }; | |
1411 | ||
1412 | static struct circular_queue lock_cq; | |
af012961 | 1413 | |
12f3dfd0 | 1414 | unsigned int max_bfs_queue_depth; |
af012961 | 1415 | |
e351b660 ML |
1416 | static unsigned int lockdep_dependency_gen_id; |
1417 | ||
af012961 PZ |
1418 | static inline void __cq_init(struct circular_queue *cq) |
1419 | { | |
1420 | cq->front = cq->rear = 0; | |
e351b660 | 1421 | lockdep_dependency_gen_id++; |
af012961 PZ |
1422 | } |
1423 | ||
1424 | static inline int __cq_empty(struct circular_queue *cq) | |
1425 | { | |
1426 | return (cq->front == cq->rear); | |
1427 | } | |
1428 | ||
1429 | static inline int __cq_full(struct circular_queue *cq) | |
1430 | { | |
1431 | return ((cq->rear + 1) & CQ_MASK) == cq->front; | |
1432 | } | |
1433 | ||
aa480771 | 1434 | static inline int __cq_enqueue(struct circular_queue *cq, struct lock_list *elem) |
af012961 PZ |
1435 | { |
1436 | if (__cq_full(cq)) | |
1437 | return -1; | |
1438 | ||
1439 | cq->element[cq->rear] = elem; | |
1440 | cq->rear = (cq->rear + 1) & CQ_MASK; | |
1441 | return 0; | |
1442 | } | |
1443 | ||
c1661325 YD |
1444 | /* |
1445 | * Dequeue an element from the circular_queue, return a lock_list if | |
1446 | * the queue is not empty, or NULL if otherwise. | |
1447 | */ | |
1448 | static inline struct lock_list * __cq_dequeue(struct circular_queue *cq) | |
af012961 | 1449 | { |
c1661325 YD |
1450 | struct lock_list * lock; |
1451 | ||
af012961 | 1452 | if (__cq_empty(cq)) |
c1661325 | 1453 | return NULL; |
af012961 | 1454 | |
c1661325 | 1455 | lock = cq->element[cq->front]; |
af012961 | 1456 | cq->front = (cq->front + 1) & CQ_MASK; |
c1661325 YD |
1457 | |
1458 | return lock; | |
af012961 PZ |
1459 | } |
1460 | ||
1461 | static inline unsigned int __cq_get_elem_count(struct circular_queue *cq) | |
1462 | { | |
1463 | return (cq->rear - cq->front) & CQ_MASK; | |
1464 | } | |
1465 | ||
d563bc6e | 1466 | static inline void mark_lock_accessed(struct lock_list *lock) |
af012961 | 1467 | { |
d563bc6e BF |
1468 | lock->class->dep_gen_id = lockdep_dependency_gen_id; |
1469 | } | |
98c33edd | 1470 | |
d563bc6e BF |
1471 | static inline void visit_lock_entry(struct lock_list *lock, |
1472 | struct lock_list *parent) | |
1473 | { | |
af012961 | 1474 | lock->parent = parent; |
af012961 PZ |
1475 | } |
1476 | ||
1477 | static inline unsigned long lock_accessed(struct lock_list *lock) | |
1478 | { | |
e351b660 | 1479 | return lock->class->dep_gen_id == lockdep_dependency_gen_id; |
af012961 PZ |
1480 | } |
1481 | ||
1482 | static inline struct lock_list *get_lock_parent(struct lock_list *child) | |
1483 | { | |
1484 | return child->parent; | |
1485 | } | |
1486 | ||
1487 | static inline int get_lock_depth(struct lock_list *child) | |
1488 | { | |
1489 | int depth = 0; | |
1490 | struct lock_list *parent; | |
1491 | ||
1492 | while ((parent = get_lock_parent(child))) { | |
1493 | child = parent; | |
1494 | depth++; | |
1495 | } | |
1496 | return depth; | |
1497 | } | |
1498 | ||
77a80692 YD |
1499 | /* |
1500 | * Return the forward or backward dependency list. | |
1501 | * | |
1502 | * @lock: the lock_list to get its class's dependency list | |
1503 | * @offset: the offset to struct lock_class to determine whether it is | |
1504 | * locks_after or locks_before | |
1505 | */ | |
1506 | static inline struct list_head *get_dep_list(struct lock_list *lock, int offset) | |
1507 | { | |
1508 | void *lock_class = lock->class; | |
1509 | ||
1510 | return lock_class + offset; | |
1511 | } | |
b11be024 BF |
1512 | /* |
1513 | * Return values of a bfs search: | |
1514 | * | |
1515 | * BFS_E* indicates an error | |
1516 | * BFS_R* indicates a result (match or not) | |
1517 | * | |
1518 | * BFS_EINVALIDNODE: Find a invalid node in the graph. | |
1519 | * | |
1520 | * BFS_EQUEUEFULL: The queue is full while doing the bfs. | |
1521 | * | |
1522 | * BFS_RMATCH: Find the matched node in the graph, and put that node into | |
1523 | * *@target_entry. | |
1524 | * | |
1525 | * BFS_RNOMATCH: Haven't found the matched node and keep *@target_entry | |
1526 | * _unchanged_. | |
1527 | */ | |
1528 | enum bfs_result { | |
1529 | BFS_EINVALIDNODE = -2, | |
1530 | BFS_EQUEUEFULL = -1, | |
1531 | BFS_RMATCH = 0, | |
1532 | BFS_RNOMATCH = 1, | |
1533 | }; | |
1534 | ||
1535 | /* | |
1536 | * bfs_result < 0 means error | |
1537 | */ | |
1538 | static inline bool bfs_error(enum bfs_result res) | |
1539 | { | |
1540 | return res < 0; | |
1541 | } | |
77a80692 | 1542 | |
3454a36d BF |
1543 | /* |
1544 | * DEP_*_BIT in lock_list::dep | |
1545 | * | |
1546 | * For dependency @prev -> @next: | |
1547 | * | |
1548 | * SR: @prev is shared reader (->read != 0) and @next is recursive reader | |
1549 | * (->read == 2) | |
1550 | * ER: @prev is exclusive locker (->read == 0) and @next is recursive reader | |
1551 | * SN: @prev is shared reader and @next is non-recursive locker (->read != 2) | |
1552 | * EN: @prev is exclusive locker and @next is non-recursive locker | |
1553 | * | |
1554 | * Note that we define the value of DEP_*_BITs so that: | |
1555 | * bit0 is prev->read == 0 | |
1556 | * bit1 is next->read != 2 | |
1557 | */ | |
1558 | #define DEP_SR_BIT (0 + (0 << 1)) /* 0 */ | |
1559 | #define DEP_ER_BIT (1 + (0 << 1)) /* 1 */ | |
1560 | #define DEP_SN_BIT (0 + (1 << 1)) /* 2 */ | |
1561 | #define DEP_EN_BIT (1 + (1 << 1)) /* 3 */ | |
1562 | ||
1563 | #define DEP_SR_MASK (1U << (DEP_SR_BIT)) | |
1564 | #define DEP_ER_MASK (1U << (DEP_ER_BIT)) | |
1565 | #define DEP_SN_MASK (1U << (DEP_SN_BIT)) | |
1566 | #define DEP_EN_MASK (1U << (DEP_EN_BIT)) | |
1567 | ||
1568 | static inline unsigned int | |
1569 | __calc_dep_bit(struct held_lock *prev, struct held_lock *next) | |
1570 | { | |
1571 | return (prev->read == 0) + ((next->read != 2) << 1); | |
1572 | } | |
1573 | ||
1574 | static inline u8 calc_dep(struct held_lock *prev, struct held_lock *next) | |
1575 | { | |
1576 | return 1U << __calc_dep_bit(prev, next); | |
1577 | } | |
1578 | ||
1579 | /* | |
1580 | * calculate the dep_bit for backwards edges. We care about whether @prev is | |
1581 | * shared and whether @next is recursive. | |
1582 | */ | |
1583 | static inline unsigned int | |
1584 | __calc_dep_bitb(struct held_lock *prev, struct held_lock *next) | |
1585 | { | |
1586 | return (next->read != 2) + ((prev->read == 0) << 1); | |
1587 | } | |
1588 | ||
1589 | static inline u8 calc_depb(struct held_lock *prev, struct held_lock *next) | |
1590 | { | |
1591 | return 1U << __calc_dep_bitb(prev, next); | |
1592 | } | |
1593 | ||
154f185e | 1594 | /* |
6971c0f3 BF |
1595 | * Initialize a lock_list entry @lock belonging to @class as the root for a BFS |
1596 | * search. | |
1597 | */ | |
1598 | static inline void __bfs_init_root(struct lock_list *lock, | |
1599 | struct lock_class *class) | |
1600 | { | |
1601 | lock->class = class; | |
1602 | lock->parent = NULL; | |
1603 | lock->only_xr = 0; | |
1604 | } | |
1605 | ||
1606 | /* | |
1607 | * Initialize a lock_list entry @lock based on a lock acquisition @hlock as the | |
1608 | * root for a BFS search. | |
1609 | * | |
1610 | * ->only_xr of the initial lock node is set to @hlock->read == 2, to make sure | |
1611 | * that <prev> -> @hlock and @hlock -> <whatever __bfs() found> is not -(*R)-> | |
1612 | * and -(S*)->. | |
1613 | */ | |
1614 | static inline void bfs_init_root(struct lock_list *lock, | |
1615 | struct held_lock *hlock) | |
1616 | { | |
1617 | __bfs_init_root(lock, hlock_class(hlock)); | |
1618 | lock->only_xr = (hlock->read == 2); | |
1619 | } | |
1620 | ||
1621 | /* | |
1622 | * Similar to bfs_init_root() but initialize the root for backwards BFS. | |
1623 | * | |
1624 | * ->only_xr of the initial lock node is set to @hlock->read != 0, to make sure | |
1625 | * that <next> -> @hlock and @hlock -> <whatever backwards BFS found> is not | |
1626 | * -(*S)-> and -(R*)-> (reverse order of -(*R)-> and -(S*)->). | |
1627 | */ | |
1628 | static inline void bfs_init_rootb(struct lock_list *lock, | |
1629 | struct held_lock *hlock) | |
1630 | { | |
1631 | __bfs_init_root(lock, hlock_class(hlock)); | |
1632 | lock->only_xr = (hlock->read != 0); | |
1633 | } | |
1634 | ||
6d1823cc BF |
1635 | static inline struct lock_list *__bfs_next(struct lock_list *lock, int offset) |
1636 | { | |
1637 | if (!lock || !lock->parent) | |
1638 | return NULL; | |
1639 | ||
1640 | return list_next_or_null_rcu(get_dep_list(lock->parent, offset), | |
1641 | &lock->entry, struct lock_list, entry); | |
1642 | } | |
1643 | ||
6971c0f3 BF |
1644 | /* |
1645 | * Breadth-First Search to find a strong path in the dependency graph. | |
1646 | * | |
1647 | * @source_entry: the source of the path we are searching for. | |
1648 | * @data: data used for the second parameter of @match function | |
1649 | * @match: match function for the search | |
1650 | * @target_entry: pointer to the target of a matched path | |
1651 | * @offset: the offset to struct lock_class to determine whether it is | |
1652 | * locks_after or locks_before | |
1653 | * | |
1654 | * We may have multiple edges (considering different kinds of dependencies, | |
1655 | * e.g. ER and SN) between two nodes in the dependency graph. But | |
1656 | * only the strong dependency path in the graph is relevant to deadlocks. A | |
1657 | * strong dependency path is a dependency path that doesn't have two adjacent | |
1658 | * dependencies as -(*R)-> -(S*)->, please see: | |
1659 | * | |
1660 | * Documentation/locking/lockdep-design.rst | |
1661 | * | |
1662 | * for more explanation of the definition of strong dependency paths | |
1663 | * | |
1664 | * In __bfs(), we only traverse in the strong dependency path: | |
1665 | * | |
1666 | * In lock_list::only_xr, we record whether the previous dependency only | |
1667 | * has -(*R)-> in the search, and if it does (prev only has -(*R)->), we | |
1668 | * filter out any -(S*)-> in the current dependency and after that, the | |
1669 | * ->only_xr is set according to whether we only have -(*R)-> left. | |
154f185e | 1670 | */ |
b11be024 BF |
1671 | static enum bfs_result __bfs(struct lock_list *source_entry, |
1672 | void *data, | |
61775ed2 | 1673 | bool (*match)(struct lock_list *entry, void *data), |
b11be024 BF |
1674 | struct lock_list **target_entry, |
1675 | int offset) | |
c94aa5ca | 1676 | { |
6d1823cc BF |
1677 | struct circular_queue *cq = &lock_cq; |
1678 | struct lock_list *lock = NULL; | |
c94aa5ca | 1679 | struct lock_list *entry; |
d588e461 | 1680 | struct list_head *head; |
6d1823cc BF |
1681 | unsigned int cq_depth; |
1682 | bool first; | |
c94aa5ca | 1683 | |
248efb21 PZ |
1684 | lockdep_assert_locked(); |
1685 | ||
d588e461 | 1686 | __cq_init(cq); |
aa480771 | 1687 | __cq_enqueue(cq, source_entry); |
c94aa5ca | 1688 | |
6d1823cc BF |
1689 | while ((lock = __bfs_next(lock, offset)) || (lock = __cq_dequeue(cq))) { |
1690 | if (!lock->class) | |
1691 | return BFS_EINVALIDNODE; | |
c94aa5ca | 1692 | |
d563bc6e | 1693 | /* |
6d1823cc BF |
1694 | * Step 1: check whether we already finish on this one. |
1695 | * | |
d563bc6e BF |
1696 | * If we have visited all the dependencies from this @lock to |
1697 | * others (iow, if we have visited all lock_list entries in | |
1698 | * @lock->class->locks_{after,before}) we skip, otherwise go | |
1699 | * and visit all the dependencies in the list and mark this | |
1700 | * list accessed. | |
1701 | */ | |
1702 | if (lock_accessed(lock)) | |
1703 | continue; | |
1704 | else | |
1705 | mark_lock_accessed(lock); | |
1706 | ||
6d1823cc BF |
1707 | /* |
1708 | * Step 2: check whether prev dependency and this form a strong | |
1709 | * dependency path. | |
1710 | */ | |
1711 | if (lock->parent) { /* Parent exists, check prev dependency */ | |
1712 | u8 dep = lock->dep; | |
1713 | bool prev_only_xr = lock->parent->only_xr; | |
6971c0f3 BF |
1714 | |
1715 | /* | |
1716 | * Mask out all -(S*)-> if we only have *R in previous | |
1717 | * step, because -(*R)-> -(S*)-> don't make up a strong | |
1718 | * dependency. | |
1719 | */ | |
1720 | if (prev_only_xr) | |
1721 | dep &= ~(DEP_SR_MASK | DEP_SN_MASK); | |
1722 | ||
1723 | /* If nothing left, we skip */ | |
1724 | if (!dep) | |
1725 | continue; | |
1726 | ||
1727 | /* If there are only -(*R)-> left, set that for the next step */ | |
6d1823cc BF |
1728 | lock->only_xr = !(dep & (DEP_SN_MASK | DEP_EN_MASK)); |
1729 | } | |
1730 | ||
1731 | /* | |
1732 | * Step 3: we haven't visited this and there is a strong | |
1733 | * dependency path to this, so check with @match. | |
1734 | */ | |
1735 | if (match(lock, data)) { | |
1736 | *target_entry = lock; | |
1737 | return BFS_RMATCH; | |
1738 | } | |
d563bc6e | 1739 | |
6d1823cc BF |
1740 | /* |
1741 | * Step 4: if not match, expand the path by adding the | |
1742 | * forward or backwards dependencis in the search | |
1743 | * | |
1744 | */ | |
1745 | first = true; | |
1746 | head = get_dep_list(lock, offset); | |
1747 | list_for_each_entry_rcu(entry, head, entry) { | |
d563bc6e | 1748 | visit_lock_entry(entry, lock); |
6d1823cc BF |
1749 | |
1750 | /* | |
1751 | * Note we only enqueue the first of the list into the | |
1752 | * queue, because we can always find a sibling | |
1753 | * dependency from one (see __bfs_next()), as a result | |
1754 | * the space of queue is saved. | |
1755 | */ | |
1756 | if (!first) | |
1757 | continue; | |
1758 | ||
1759 | first = false; | |
1760 | ||
1761 | if (__cq_enqueue(cq, entry)) | |
1762 | return BFS_EQUEUEFULL; | |
1763 | ||
d563bc6e BF |
1764 | cq_depth = __cq_get_elem_count(cq); |
1765 | if (max_bfs_queue_depth < cq_depth) | |
1766 | max_bfs_queue_depth = cq_depth; | |
c94aa5ca ML |
1767 | } |
1768 | } | |
6d1823cc BF |
1769 | |
1770 | return BFS_RNOMATCH; | |
c94aa5ca ML |
1771 | } |
1772 | ||
b11be024 BF |
1773 | static inline enum bfs_result |
1774 | __bfs_forwards(struct lock_list *src_entry, | |
1775 | void *data, | |
61775ed2 | 1776 | bool (*match)(struct lock_list *entry, void *data), |
b11be024 | 1777 | struct lock_list **target_entry) |
c94aa5ca | 1778 | { |
77a80692 YD |
1779 | return __bfs(src_entry, data, match, target_entry, |
1780 | offsetof(struct lock_class, locks_after)); | |
c94aa5ca ML |
1781 | |
1782 | } | |
1783 | ||
b11be024 BF |
1784 | static inline enum bfs_result |
1785 | __bfs_backwards(struct lock_list *src_entry, | |
1786 | void *data, | |
61775ed2 | 1787 | bool (*match)(struct lock_list *entry, void *data), |
b11be024 | 1788 | struct lock_list **target_entry) |
c94aa5ca | 1789 | { |
77a80692 YD |
1790 | return __bfs(src_entry, data, match, target_entry, |
1791 | offsetof(struct lock_class, locks_before)); | |
c94aa5ca ML |
1792 | |
1793 | } | |
1794 | ||
12593b74 BVA |
1795 | static void print_lock_trace(const struct lock_trace *trace, |
1796 | unsigned int spaces) | |
c120bce7 | 1797 | { |
12593b74 | 1798 | stack_trace_print(trace->entries, trace->nr_entries, spaces); |
c120bce7 TG |
1799 | } |
1800 | ||
8e18257d PZ |
1801 | /* |
1802 | * Print a dependency chain entry (this is only done when a deadlock | |
1803 | * has been detected): | |
1804 | */ | |
f7c1c6b3 | 1805 | static noinline void |
24208ca7 | 1806 | print_circular_bug_entry(struct lock_list *target, int depth) |
8e18257d PZ |
1807 | { |
1808 | if (debug_locks_silent) | |
f7c1c6b3 | 1809 | return; |
8e18257d PZ |
1810 | printk("\n-> #%u", depth); |
1811 | print_lock_name(target->class); | |
f943fe0f | 1812 | printk(KERN_CONT ":\n"); |
12593b74 | 1813 | print_lock_trace(target->trace, 6); |
8e18257d PZ |
1814 | } |
1815 | ||
f4185812 SR |
1816 | static void |
1817 | print_circular_lock_scenario(struct held_lock *src, | |
1818 | struct held_lock *tgt, | |
1819 | struct lock_list *prt) | |
1820 | { | |
1821 | struct lock_class *source = hlock_class(src); | |
1822 | struct lock_class *target = hlock_class(tgt); | |
1823 | struct lock_class *parent = prt->class; | |
1824 | ||
1825 | /* | |
1826 | * A direct locking problem where unsafe_class lock is taken | |
1827 | * directly by safe_class lock, then all we need to show | |
1828 | * is the deadlock scenario, as it is obvious that the | |
1829 | * unsafe lock is taken under the safe lock. | |
1830 | * | |
1831 | * But if there is a chain instead, where the safe lock takes | |
1832 | * an intermediate lock (middle_class) where this lock is | |
1833 | * not the same as the safe lock, then the lock chain is | |
1834 | * used to describe the problem. Otherwise we would need | |
1835 | * to show a different CPU case for each link in the chain | |
1836 | * from the safe_class lock to the unsafe_class lock. | |
1837 | */ | |
1838 | if (parent != source) { | |
1839 | printk("Chain exists of:\n "); | |
1840 | __print_lock_name(source); | |
f943fe0f | 1841 | printk(KERN_CONT " --> "); |
f4185812 | 1842 | __print_lock_name(parent); |
f943fe0f | 1843 | printk(KERN_CONT " --> "); |
f4185812 | 1844 | __print_lock_name(target); |
f943fe0f | 1845 | printk(KERN_CONT "\n\n"); |
f4185812 SR |
1846 | } |
1847 | ||
e966eaee IM |
1848 | printk(" Possible unsafe locking scenario:\n\n"); |
1849 | printk(" CPU0 CPU1\n"); | |
1850 | printk(" ---- ----\n"); | |
1851 | printk(" lock("); | |
1852 | __print_lock_name(target); | |
1853 | printk(KERN_CONT ");\n"); | |
1854 | printk(" lock("); | |
1855 | __print_lock_name(parent); | |
1856 | printk(KERN_CONT ");\n"); | |
1857 | printk(" lock("); | |
1858 | __print_lock_name(target); | |
1859 | printk(KERN_CONT ");\n"); | |
1860 | printk(" lock("); | |
1861 | __print_lock_name(source); | |
1862 | printk(KERN_CONT ");\n"); | |
1863 | printk("\n *** DEADLOCK ***\n\n"); | |
f4185812 SR |
1864 | } |
1865 | ||
8e18257d PZ |
1866 | /* |
1867 | * When a circular dependency is detected, print the | |
1868 | * header first: | |
1869 | */ | |
f7c1c6b3 | 1870 | static noinline void |
db0002a3 ML |
1871 | print_circular_bug_header(struct lock_list *entry, unsigned int depth, |
1872 | struct held_lock *check_src, | |
1873 | struct held_lock *check_tgt) | |
8e18257d PZ |
1874 | { |
1875 | struct task_struct *curr = current; | |
1876 | ||
c94aa5ca | 1877 | if (debug_locks_silent) |
f7c1c6b3 | 1878 | return; |
8e18257d | 1879 | |
681fbec8 | 1880 | pr_warn("\n"); |
a5dd63ef PM |
1881 | pr_warn("======================================================\n"); |
1882 | pr_warn("WARNING: possible circular locking dependency detected\n"); | |
fbdc4b9a | 1883 | print_kernel_ident(); |
a5dd63ef | 1884 | pr_warn("------------------------------------------------------\n"); |
681fbec8 | 1885 | pr_warn("%s/%d is trying to acquire lock:\n", |
ba25f9dc | 1886 | curr->comm, task_pid_nr(curr)); |
db0002a3 | 1887 | print_lock(check_src); |
383a4bc8 | 1888 | |
e966eaee | 1889 | pr_warn("\nbut task is already holding lock:\n"); |
383a4bc8 | 1890 | |
db0002a3 | 1891 | print_lock(check_tgt); |
681fbec8 PM |
1892 | pr_warn("\nwhich lock already depends on the new lock.\n\n"); |
1893 | pr_warn("\nthe existing dependency chain (in reverse order) is:\n"); | |
8e18257d PZ |
1894 | |
1895 | print_circular_bug_entry(entry, depth); | |
8e18257d PZ |
1896 | } |
1897 | ||
68e30567 BF |
1898 | /* |
1899 | * We are about to add A -> B into the dependency graph, and in __bfs() a | |
1900 | * strong dependency path A -> .. -> B is found: hlock_class equals | |
1901 | * entry->class. | |
1902 | * | |
1903 | * If A -> .. -> B can replace A -> B in any __bfs() search (means the former | |
1904 | * is _stronger_ than or equal to the latter), we consider A -> B as redundant. | |
1905 | * For example if A -> .. -> B is -(EN)-> (i.e. A -(E*)-> .. -(*N)-> B), and A | |
1906 | * -> B is -(ER)-> or -(EN)->, then we don't need to add A -> B into the | |
1907 | * dependency graph, as any strong path ..-> A -> B ->.. we can get with | |
1908 | * having dependency A -> B, we could already get a equivalent path ..-> A -> | |
1909 | * .. -> B -> .. with A -> .. -> B. Therefore A -> B is reduntant. | |
1910 | * | |
1911 | * We need to make sure both the start and the end of A -> .. -> B is not | |
1912 | * weaker than A -> B. For the start part, please see the comment in | |
1913 | * check_redundant(). For the end part, we need: | |
1914 | * | |
1915 | * Either | |
1916 | * | |
1917 | * a) A -> B is -(*R)-> (everything is not weaker than that) | |
1918 | * | |
1919 | * or | |
1920 | * | |
1921 | * b) A -> .. -> B is -(*N)-> (nothing is stronger than this) | |
1922 | * | |
1923 | */ | |
1924 | static inline bool hlock_equal(struct lock_list *entry, void *data) | |
9e2d551e | 1925 | { |
68e30567 BF |
1926 | struct held_lock *hlock = (struct held_lock *)data; |
1927 | ||
1928 | return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */ | |
1929 | (hlock->read == 2 || /* A -> B is -(*R)-> */ | |
1930 | !entry->only_xr); /* A -> .. -> B is -(*N)-> */ | |
9e2d551e ML |
1931 | } |
1932 | ||
9de0c9bb BF |
1933 | /* |
1934 | * We are about to add B -> A into the dependency graph, and in __bfs() a | |
1935 | * strong dependency path A -> .. -> B is found: hlock_class equals | |
1936 | * entry->class. | |
1937 | * | |
1938 | * We will have a deadlock case (conflict) if A -> .. -> B -> A is a strong | |
1939 | * dependency cycle, that means: | |
1940 | * | |
1941 | * Either | |
1942 | * | |
1943 | * a) B -> A is -(E*)-> | |
1944 | * | |
1945 | * or | |
1946 | * | |
1947 | * b) A -> .. -> B is -(*N)-> (i.e. A -> .. -(*N)-> B) | |
1948 | * | |
1949 | * as then we don't have -(*R)-> -(S*)-> in the cycle. | |
1950 | */ | |
1951 | static inline bool hlock_conflict(struct lock_list *entry, void *data) | |
1952 | { | |
1953 | struct held_lock *hlock = (struct held_lock *)data; | |
1954 | ||
1955 | return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */ | |
1956 | (hlock->read == 0 || /* B -> A is -(E*)-> */ | |
1957 | !entry->only_xr); /* A -> .. -> B is -(*N)-> */ | |
1958 | } | |
1959 | ||
f7c1c6b3 | 1960 | static noinline void print_circular_bug(struct lock_list *this, |
9de0c9bb BF |
1961 | struct lock_list *target, |
1962 | struct held_lock *check_src, | |
1963 | struct held_lock *check_tgt) | |
8e18257d PZ |
1964 | { |
1965 | struct task_struct *curr = current; | |
c94aa5ca | 1966 | struct lock_list *parent; |
f4185812 | 1967 | struct lock_list *first_parent; |
24208ca7 | 1968 | int depth; |
8e18257d | 1969 | |
c94aa5ca | 1970 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
f7c1c6b3 | 1971 | return; |
8e18257d | 1972 | |
12593b74 BVA |
1973 | this->trace = save_trace(); |
1974 | if (!this->trace) | |
f7c1c6b3 | 1975 | return; |
8e18257d | 1976 | |
c94aa5ca ML |
1977 | depth = get_lock_depth(target); |
1978 | ||
db0002a3 | 1979 | print_circular_bug_header(target, depth, check_src, check_tgt); |
c94aa5ca ML |
1980 | |
1981 | parent = get_lock_parent(target); | |
f4185812 | 1982 | first_parent = parent; |
c94aa5ca ML |
1983 | |
1984 | while (parent) { | |
1985 | print_circular_bug_entry(parent, --depth); | |
1986 | parent = get_lock_parent(parent); | |
1987 | } | |
8e18257d PZ |
1988 | |
1989 | printk("\nother info that might help us debug this:\n\n"); | |
f4185812 SR |
1990 | print_circular_lock_scenario(check_src, check_tgt, |
1991 | first_parent); | |
1992 | ||
8e18257d PZ |
1993 | lockdep_print_held_locks(curr); |
1994 | ||
1995 | printk("\nstack backtrace:\n"); | |
1996 | dump_stack(); | |
8e18257d PZ |
1997 | } |
1998 | ||
f7c1c6b3 | 1999 | static noinline void print_bfs_bug(int ret) |
db0002a3 ML |
2000 | { |
2001 | if (!debug_locks_off_graph_unlock()) | |
f7c1c6b3 | 2002 | return; |
db0002a3 | 2003 | |
0119fee4 PZ |
2004 | /* |
2005 | * Breadth-first-search failed, graph got corrupted? | |
2006 | */ | |
db0002a3 | 2007 | WARN(1, "lockdep bfs error:%d\n", ret); |
db0002a3 ML |
2008 | } |
2009 | ||
61775ed2 | 2010 | static bool noop_count(struct lock_list *entry, void *data) |
419ca3f1 | 2011 | { |
ef681026 | 2012 | (*(unsigned long *)data)++; |
61775ed2 | 2013 | return false; |
ef681026 | 2014 | } |
419ca3f1 | 2015 | |
5216d530 | 2016 | static unsigned long __lockdep_count_forward_deps(struct lock_list *this) |
ef681026 ML |
2017 | { |
2018 | unsigned long count = 0; | |
3f649ab7 | 2019 | struct lock_list *target_entry; |
419ca3f1 | 2020 | |
ef681026 | 2021 | __bfs_forwards(this, (void *)&count, noop_count, &target_entry); |
419ca3f1 | 2022 | |
ef681026 | 2023 | return count; |
419ca3f1 | 2024 | } |
419ca3f1 DM |
2025 | unsigned long lockdep_count_forward_deps(struct lock_class *class) |
2026 | { | |
2027 | unsigned long ret, flags; | |
ef681026 ML |
2028 | struct lock_list this; |
2029 | ||
6971c0f3 | 2030 | __bfs_init_root(&this, class); |
419ca3f1 | 2031 | |
fcc784be | 2032 | raw_local_irq_save(flags); |
248efb21 | 2033 | lockdep_lock(); |
ef681026 | 2034 | ret = __lockdep_count_forward_deps(&this); |
248efb21 | 2035 | lockdep_unlock(); |
fcc784be | 2036 | raw_local_irq_restore(flags); |
419ca3f1 DM |
2037 | |
2038 | return ret; | |
2039 | } | |
2040 | ||
5216d530 | 2041 | static unsigned long __lockdep_count_backward_deps(struct lock_list *this) |
419ca3f1 | 2042 | { |
ef681026 | 2043 | unsigned long count = 0; |
3f649ab7 | 2044 | struct lock_list *target_entry; |
419ca3f1 | 2045 | |
ef681026 | 2046 | __bfs_backwards(this, (void *)&count, noop_count, &target_entry); |
419ca3f1 | 2047 | |
ef681026 | 2048 | return count; |
419ca3f1 DM |
2049 | } |
2050 | ||
2051 | unsigned long lockdep_count_backward_deps(struct lock_class *class) | |
2052 | { | |
2053 | unsigned long ret, flags; | |
ef681026 ML |
2054 | struct lock_list this; |
2055 | ||
6971c0f3 | 2056 | __bfs_init_root(&this, class); |
419ca3f1 | 2057 | |
fcc784be | 2058 | raw_local_irq_save(flags); |
248efb21 | 2059 | lockdep_lock(); |
ef681026 | 2060 | ret = __lockdep_count_backward_deps(&this); |
248efb21 | 2061 | lockdep_unlock(); |
fcc784be | 2062 | raw_local_irq_restore(flags); |
419ca3f1 DM |
2063 | |
2064 | return ret; | |
2065 | } | |
2066 | ||
8e18257d | 2067 | /* |
8c2c2b44 | 2068 | * Check that the dependency graph starting at <src> can lead to |
b11be024 | 2069 | * <target> or not. |
8e18257d | 2070 | */ |
b11be024 | 2071 | static noinline enum bfs_result |
9de0c9bb BF |
2072 | check_path(struct held_lock *target, struct lock_list *src_entry, |
2073 | bool (*match)(struct lock_list *entry, void *data), | |
8c2c2b44 | 2074 | struct lock_list **target_entry) |
8e18257d | 2075 | { |
b11be024 | 2076 | enum bfs_result ret; |
8c2c2b44 | 2077 | |
9de0c9bb | 2078 | ret = __bfs_forwards(src_entry, target, match, target_entry); |
8c2c2b44 | 2079 | |
b11be024 | 2080 | if (unlikely(bfs_error(ret))) |
8c2c2b44 YD |
2081 | print_bfs_bug(ret); |
2082 | ||
2083 | return ret; | |
2084 | } | |
2085 | ||
2086 | /* | |
2087 | * Prove that the dependency graph starting at <src> can not | |
2088 | * lead to <target>. If it can, there is a circle when adding | |
2089 | * <target> -> <src> dependency. | |
2090 | * | |
b11be024 | 2091 | * Print an error and return BFS_RMATCH if it does. |
8c2c2b44 | 2092 | */ |
b11be024 | 2093 | static noinline enum bfs_result |
8c2c2b44 | 2094 | check_noncircular(struct held_lock *src, struct held_lock *target, |
12593b74 | 2095 | struct lock_trace **const trace) |
8c2c2b44 | 2096 | { |
b11be024 | 2097 | enum bfs_result ret; |
3f649ab7 | 2098 | struct lock_list *target_entry; |
6971c0f3 BF |
2099 | struct lock_list src_entry; |
2100 | ||
2101 | bfs_init_root(&src_entry, src); | |
8e18257d | 2102 | |
bd6d29c2 | 2103 | debug_atomic_inc(nr_cyclic_checks); |
419ca3f1 | 2104 | |
9de0c9bb | 2105 | ret = check_path(target, &src_entry, hlock_conflict, &target_entry); |
fbb9ce95 | 2106 | |
b11be024 | 2107 | if (unlikely(ret == BFS_RMATCH)) { |
12593b74 | 2108 | if (!*trace) { |
8c2c2b44 YD |
2109 | /* |
2110 | * If save_trace fails here, the printing might | |
2111 | * trigger a WARN but because of the !nr_entries it | |
2112 | * should not do bad things. | |
2113 | */ | |
12593b74 | 2114 | *trace = save_trace(); |
8c2c2b44 YD |
2115 | } |
2116 | ||
2117 | print_circular_bug(&src_entry, target_entry, src, target); | |
2118 | } | |
2119 | ||
2120 | return ret; | |
db0002a3 | 2121 | } |
c94aa5ca | 2122 | |
68e9dc29 | 2123 | #ifdef CONFIG_LOCKDEP_SMALL |
8c2c2b44 YD |
2124 | /* |
2125 | * Check that the dependency graph starting at <src> can lead to | |
2126 | * <target> or not. If it can, <src> -> <target> dependency is already | |
2127 | * in the graph. | |
2128 | * | |
b11be024 BF |
2129 | * Return BFS_RMATCH if it does, or BFS_RMATCH if it does not, return BFS_E* if |
2130 | * any error appears in the bfs search. | |
8c2c2b44 | 2131 | */ |
b11be024 | 2132 | static noinline enum bfs_result |
8c2c2b44 | 2133 | check_redundant(struct held_lock *src, struct held_lock *target) |
ae813308 | 2134 | { |
b11be024 | 2135 | enum bfs_result ret; |
3f649ab7 | 2136 | struct lock_list *target_entry; |
6971c0f3 BF |
2137 | struct lock_list src_entry; |
2138 | ||
2139 | bfs_init_root(&src_entry, src); | |
68e30567 BF |
2140 | /* |
2141 | * Special setup for check_redundant(). | |
2142 | * | |
2143 | * To report redundant, we need to find a strong dependency path that | |
2144 | * is equal to or stronger than <src> -> <target>. So if <src> is E, | |
2145 | * we need to let __bfs() only search for a path starting at a -(E*)->, | |
2146 | * we achieve this by setting the initial node's ->only_xr to true in | |
2147 | * that case. And if <prev> is S, we set initial ->only_xr to false | |
2148 | * because both -(S*)-> (equal) and -(E*)-> (stronger) are redundant. | |
2149 | */ | |
2150 | src_entry.only_xr = src->read == 0; | |
ae813308 PZ |
2151 | |
2152 | debug_atomic_inc(nr_redundant_checks); | |
2153 | ||
68e30567 | 2154 | ret = check_path(target, &src_entry, hlock_equal, &target_entry); |
ae813308 | 2155 | |
b11be024 | 2156 | if (ret == BFS_RMATCH) |
8c2c2b44 | 2157 | debug_atomic_inc(nr_redundant); |
8c2c2b44 YD |
2158 | |
2159 | return ret; | |
ae813308 | 2160 | } |
68e9dc29 | 2161 | #endif |
ae813308 | 2162 | |
e7a38f63 | 2163 | #ifdef CONFIG_TRACE_IRQFLAGS |
948f8376 | 2164 | |
f08e3888 BF |
2165 | /* |
2166 | * Forwards and backwards subgraph searching, for the purposes of | |
2167 | * proving that two subgraphs can be connected by a new dependency | |
2168 | * without creating any illegal irq-safe -> irq-unsafe lock dependency. | |
2169 | * | |
2170 | * A irq safe->unsafe deadlock happens with the following conditions: | |
2171 | * | |
2172 | * 1) We have a strong dependency path A -> ... -> B | |
2173 | * | |
2174 | * 2) and we have ENABLED_IRQ usage of B and USED_IN_IRQ usage of A, therefore | |
2175 | * irq can create a new dependency B -> A (consider the case that a holder | |
2176 | * of B gets interrupted by an irq whose handler will try to acquire A). | |
2177 | * | |
2178 | * 3) the dependency circle A -> ... -> B -> A we get from 1) and 2) is a | |
2179 | * strong circle: | |
2180 | * | |
2181 | * For the usage bits of B: | |
2182 | * a) if A -> B is -(*N)->, then B -> A could be any type, so any | |
2183 | * ENABLED_IRQ usage suffices. | |
2184 | * b) if A -> B is -(*R)->, then B -> A must be -(E*)->, so only | |
2185 | * ENABLED_IRQ_*_READ usage suffices. | |
2186 | * | |
2187 | * For the usage bits of A: | |
2188 | * c) if A -> B is -(E*)->, then B -> A could be any type, so any | |
2189 | * USED_IN_IRQ usage suffices. | |
2190 | * d) if A -> B is -(S*)->, then B -> A must be -(*N)->, so only | |
2191 | * USED_IN_IRQ_*_READ usage suffices. | |
2192 | */ | |
2193 | ||
2194 | /* | |
2195 | * There is a strong dependency path in the dependency graph: A -> B, and now | |
2196 | * we need to decide which usage bit of A should be accumulated to detect | |
2197 | * safe->unsafe bugs. | |
2198 | * | |
2199 | * Note that usage_accumulate() is used in backwards search, so ->only_xr | |
2200 | * stands for whether A -> B only has -(S*)-> (in this case ->only_xr is true). | |
2201 | * | |
2202 | * As above, if only_xr is false, which means A -> B has -(E*)-> dependency | |
2203 | * path, any usage of A should be considered. Otherwise, we should only | |
2204 | * consider _READ usage. | |
2205 | */ | |
61775ed2 | 2206 | static inline bool usage_accumulate(struct lock_list *entry, void *mask) |
948f8376 | 2207 | { |
f08e3888 BF |
2208 | if (!entry->only_xr) |
2209 | *(unsigned long *)mask |= entry->class->usage_mask; | |
2210 | else /* Mask out _READ usage bits */ | |
2211 | *(unsigned long *)mask |= (entry->class->usage_mask & LOCKF_IRQ); | |
948f8376 | 2212 | |
61775ed2 | 2213 | return false; |
948f8376 FW |
2214 | } |
2215 | ||
fbb9ce95 | 2216 | /* |
f08e3888 BF |
2217 | * There is a strong dependency path in the dependency graph: A -> B, and now |
2218 | * we need to decide which usage bit of B conflicts with the usage bits of A, | |
2219 | * i.e. which usage bit of B may introduce safe->unsafe deadlocks. | |
2220 | * | |
2221 | * As above, if only_xr is false, which means A -> B has -(*N)-> dependency | |
2222 | * path, any usage of B should be considered. Otherwise, we should only | |
2223 | * consider _READ usage. | |
fbb9ce95 | 2224 | */ |
61775ed2 | 2225 | static inline bool usage_match(struct lock_list *entry, void *mask) |
d7aaba14 | 2226 | { |
f08e3888 BF |
2227 | if (!entry->only_xr) |
2228 | return !!(entry->class->usage_mask & *(unsigned long *)mask); | |
2229 | else /* Mask out _READ usage bits */ | |
2230 | return !!((entry->class->usage_mask & LOCKF_IRQ) & *(unsigned long *)mask); | |
d7aaba14 ML |
2231 | } |
2232 | ||
fbb9ce95 IM |
2233 | /* |
2234 | * Find a node in the forwards-direction dependency sub-graph starting | |
d7aaba14 | 2235 | * at @root->class that matches @bit. |
fbb9ce95 | 2236 | * |
b11be024 | 2237 | * Return BFS_MATCH if such a node exists in the subgraph, and put that node |
d7aaba14 | 2238 | * into *@target_entry. |
fbb9ce95 | 2239 | */ |
b11be024 | 2240 | static enum bfs_result |
627f364d | 2241 | find_usage_forwards(struct lock_list *root, unsigned long usage_mask, |
d7aaba14 | 2242 | struct lock_list **target_entry) |
fbb9ce95 | 2243 | { |
b11be024 | 2244 | enum bfs_result result; |
fbb9ce95 | 2245 | |
bd6d29c2 | 2246 | debug_atomic_inc(nr_find_usage_forwards_checks); |
fbb9ce95 | 2247 | |
627f364d | 2248 | result = __bfs_forwards(root, &usage_mask, usage_match, target_entry); |
d7aaba14 ML |
2249 | |
2250 | return result; | |
fbb9ce95 IM |
2251 | } |
2252 | ||
2253 | /* | |
2254 | * Find a node in the backwards-direction dependency sub-graph starting | |
d7aaba14 | 2255 | * at @root->class that matches @bit. |
fbb9ce95 | 2256 | */ |
b11be024 | 2257 | static enum bfs_result |
627f364d | 2258 | find_usage_backwards(struct lock_list *root, unsigned long usage_mask, |
d7aaba14 | 2259 | struct lock_list **target_entry) |
fbb9ce95 | 2260 | { |
b11be024 | 2261 | enum bfs_result result; |
fbb9ce95 | 2262 | |
bd6d29c2 | 2263 | debug_atomic_inc(nr_find_usage_backwards_checks); |
fbb9ce95 | 2264 | |
627f364d | 2265 | result = __bfs_backwards(root, &usage_mask, usage_match, target_entry); |
f82b217e | 2266 | |
d7aaba14 | 2267 | return result; |
fbb9ce95 IM |
2268 | } |
2269 | ||
af012961 PZ |
2270 | static void print_lock_class_header(struct lock_class *class, int depth) |
2271 | { | |
2272 | int bit; | |
2273 | ||
2274 | printk("%*s->", depth, ""); | |
2275 | print_lock_name(class); | |
8ca2b56c WL |
2276 | #ifdef CONFIG_DEBUG_LOCKDEP |
2277 | printk(KERN_CONT " ops: %lu", debug_class_ops_read(class)); | |
2278 | #endif | |
f943fe0f | 2279 | printk(KERN_CONT " {\n"); |
af012961 | 2280 | |
2bb8945b | 2281 | for (bit = 0; bit < LOCK_TRACE_STATES; bit++) { |
af012961 PZ |
2282 | if (class->usage_mask & (1 << bit)) { |
2283 | int len = depth; | |
2284 | ||
2285 | len += printk("%*s %s", depth, "", usage_str[bit]); | |
f943fe0f | 2286 | len += printk(KERN_CONT " at:\n"); |
12593b74 | 2287 | print_lock_trace(class->usage_traces[bit], len); |
af012961 PZ |
2288 | } |
2289 | } | |
2290 | printk("%*s }\n", depth, ""); | |
2291 | ||
04860d48 | 2292 | printk("%*s ... key at: [<%px>] %pS\n", |
f943fe0f | 2293 | depth, "", class->key, class->key); |
af012961 PZ |
2294 | } |
2295 | ||
2296 | /* | |
2297 | * printk the shortest lock dependencies from @start to @end in reverse order: | |
2298 | */ | |
2299 | static void __used | |
2300 | print_shortest_lock_dependencies(struct lock_list *leaf, | |
f7c1c6b3 | 2301 | struct lock_list *root) |
af012961 PZ |
2302 | { |
2303 | struct lock_list *entry = leaf; | |
2304 | int depth; | |
2305 | ||
2306 | /*compute depth from generated tree by BFS*/ | |
2307 | depth = get_lock_depth(leaf); | |
2308 | ||
2309 | do { | |
2310 | print_lock_class_header(entry->class, depth); | |
2311 | printk("%*s ... acquired at:\n", depth, ""); | |
12593b74 | 2312 | print_lock_trace(entry->trace, 2); |
af012961 PZ |
2313 | printk("\n"); |
2314 | ||
2315 | if (depth == 0 && (entry != root)) { | |
6be8c393 | 2316 | printk("lockdep:%s bad path found in chain graph\n", __func__); |
af012961 PZ |
2317 | break; |
2318 | } | |
2319 | ||
2320 | entry = get_lock_parent(entry); | |
2321 | depth--; | |
2322 | } while (entry && (depth >= 0)); | |
af012961 | 2323 | } |
d7aaba14 | 2324 | |
3003eba3 SR |
2325 | static void |
2326 | print_irq_lock_scenario(struct lock_list *safe_entry, | |
2327 | struct lock_list *unsafe_entry, | |
dad3d743 SR |
2328 | struct lock_class *prev_class, |
2329 | struct lock_class *next_class) | |
3003eba3 SR |
2330 | { |
2331 | struct lock_class *safe_class = safe_entry->class; | |
2332 | struct lock_class *unsafe_class = unsafe_entry->class; | |
dad3d743 | 2333 | struct lock_class *middle_class = prev_class; |
3003eba3 SR |
2334 | |
2335 | if (middle_class == safe_class) | |
dad3d743 | 2336 | middle_class = next_class; |
3003eba3 SR |
2337 | |
2338 | /* | |
2339 | * A direct locking problem where unsafe_class lock is taken | |
2340 | * directly by safe_class lock, then all we need to show | |
2341 | * is the deadlock scenario, as it is obvious that the | |
2342 | * unsafe lock is taken under the safe lock. | |
2343 | * | |
2344 | * But if there is a chain instead, where the safe lock takes | |
2345 | * an intermediate lock (middle_class) where this lock is | |
2346 | * not the same as the safe lock, then the lock chain is | |
2347 | * used to describe the problem. Otherwise we would need | |
2348 | * to show a different CPU case for each link in the chain | |
2349 | * from the safe_class lock to the unsafe_class lock. | |
2350 | */ | |
2351 | if (middle_class != unsafe_class) { | |
2352 | printk("Chain exists of:\n "); | |
2353 | __print_lock_name(safe_class); | |
f943fe0f | 2354 | printk(KERN_CONT " --> "); |
3003eba3 | 2355 | __print_lock_name(middle_class); |
f943fe0f | 2356 | printk(KERN_CONT " --> "); |
3003eba3 | 2357 | __print_lock_name(unsafe_class); |
f943fe0f | 2358 | printk(KERN_CONT "\n\n"); |
3003eba3 SR |
2359 | } |
2360 | ||
2361 | printk(" Possible interrupt unsafe locking scenario:\n\n"); | |
2362 | printk(" CPU0 CPU1\n"); | |
2363 | printk(" ---- ----\n"); | |
2364 | printk(" lock("); | |
2365 | __print_lock_name(unsafe_class); | |
f943fe0f | 2366 | printk(KERN_CONT ");\n"); |
3003eba3 SR |
2367 | printk(" local_irq_disable();\n"); |
2368 | printk(" lock("); | |
2369 | __print_lock_name(safe_class); | |
f943fe0f | 2370 | printk(KERN_CONT ");\n"); |
3003eba3 SR |
2371 | printk(" lock("); |
2372 | __print_lock_name(middle_class); | |
f943fe0f | 2373 | printk(KERN_CONT ");\n"); |
3003eba3 SR |
2374 | printk(" <Interrupt>\n"); |
2375 | printk(" lock("); | |
2376 | __print_lock_name(safe_class); | |
f943fe0f | 2377 | printk(KERN_CONT ");\n"); |
3003eba3 SR |
2378 | printk("\n *** DEADLOCK ***\n\n"); |
2379 | } | |
2380 | ||
f7c1c6b3 | 2381 | static void |
fbb9ce95 | 2382 | print_bad_irq_dependency(struct task_struct *curr, |
24208ca7 ML |
2383 | struct lock_list *prev_root, |
2384 | struct lock_list *next_root, | |
2385 | struct lock_list *backwards_entry, | |
2386 | struct lock_list *forwards_entry, | |
fbb9ce95 IM |
2387 | struct held_lock *prev, |
2388 | struct held_lock *next, | |
2389 | enum lock_usage_bit bit1, | |
2390 | enum lock_usage_bit bit2, | |
2391 | const char *irqclass) | |
2392 | { | |
74c383f1 | 2393 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
f7c1c6b3 | 2394 | return; |
fbb9ce95 | 2395 | |
681fbec8 | 2396 | pr_warn("\n"); |
a5dd63ef PM |
2397 | pr_warn("=====================================================\n"); |
2398 | pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n", | |
fbb9ce95 | 2399 | irqclass, irqclass); |
fbdc4b9a | 2400 | print_kernel_ident(); |
a5dd63ef | 2401 | pr_warn("-----------------------------------------------------\n"); |
681fbec8 | 2402 | pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n", |
ba25f9dc | 2403 | curr->comm, task_pid_nr(curr), |
f9ad4a5f | 2404 | lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT, |
fbb9ce95 | 2405 | curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT, |
f9ad4a5f | 2406 | lockdep_hardirqs_enabled(), |
fbb9ce95 IM |
2407 | curr->softirqs_enabled); |
2408 | print_lock(next); | |
2409 | ||
681fbec8 | 2410 | pr_warn("\nand this task is already holding:\n"); |
fbb9ce95 | 2411 | print_lock(prev); |
681fbec8 | 2412 | pr_warn("which would create a new lock dependency:\n"); |
f82b217e | 2413 | print_lock_name(hlock_class(prev)); |
681fbec8 | 2414 | pr_cont(" ->"); |
f82b217e | 2415 | print_lock_name(hlock_class(next)); |
681fbec8 | 2416 | pr_cont("\n"); |
fbb9ce95 | 2417 | |
681fbec8 | 2418 | pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n", |
fbb9ce95 | 2419 | irqclass); |
24208ca7 | 2420 | print_lock_name(backwards_entry->class); |
681fbec8 | 2421 | pr_warn("\n... which became %s-irq-safe at:\n", irqclass); |
fbb9ce95 | 2422 | |
12593b74 | 2423 | print_lock_trace(backwards_entry->class->usage_traces[bit1], 1); |
fbb9ce95 | 2424 | |
681fbec8 | 2425 | pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass); |
24208ca7 | 2426 | print_lock_name(forwards_entry->class); |
681fbec8 PM |
2427 | pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass); |
2428 | pr_warn("..."); | |
fbb9ce95 | 2429 | |
12593b74 | 2430 | print_lock_trace(forwards_entry->class->usage_traces[bit2], 1); |
fbb9ce95 | 2431 | |
681fbec8 | 2432 | pr_warn("\nother info that might help us debug this:\n\n"); |
dad3d743 SR |
2433 | print_irq_lock_scenario(backwards_entry, forwards_entry, |
2434 | hlock_class(prev), hlock_class(next)); | |
3003eba3 | 2435 | |
fbb9ce95 IM |
2436 | lockdep_print_held_locks(curr); |
2437 | ||
681fbec8 | 2438 | pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass); |
12593b74 BVA |
2439 | prev_root->trace = save_trace(); |
2440 | if (!prev_root->trace) | |
f7c1c6b3 | 2441 | return; |
24208ca7 | 2442 | print_shortest_lock_dependencies(backwards_entry, prev_root); |
fbb9ce95 | 2443 | |
681fbec8 PM |
2444 | pr_warn("\nthe dependencies between the lock to be acquired"); |
2445 | pr_warn(" and %s-irq-unsafe lock:\n", irqclass); | |
12593b74 BVA |
2446 | next_root->trace = save_trace(); |
2447 | if (!next_root->trace) | |
f7c1c6b3 | 2448 | return; |
24208ca7 | 2449 | print_shortest_lock_dependencies(forwards_entry, next_root); |
fbb9ce95 | 2450 | |
681fbec8 | 2451 | pr_warn("\nstack backtrace:\n"); |
fbb9ce95 | 2452 | dump_stack(); |
fbb9ce95 IM |
2453 | } |
2454 | ||
4f367d8a PZ |
2455 | static const char *state_names[] = { |
2456 | #define LOCKDEP_STATE(__STATE) \ | |
b4b136f4 | 2457 | __stringify(__STATE), |
4f367d8a PZ |
2458 | #include "lockdep_states.h" |
2459 | #undef LOCKDEP_STATE | |
2460 | }; | |
2461 | ||
2462 | static const char *state_rnames[] = { | |
2463 | #define LOCKDEP_STATE(__STATE) \ | |
b4b136f4 | 2464 | __stringify(__STATE)"-READ", |
4f367d8a PZ |
2465 | #include "lockdep_states.h" |
2466 | #undef LOCKDEP_STATE | |
2467 | }; | |
2468 | ||
2469 | static inline const char *state_name(enum lock_usage_bit bit) | |
8e18257d | 2470 | { |
c902a1e8 FW |
2471 | if (bit & LOCK_USAGE_READ_MASK) |
2472 | return state_rnames[bit >> LOCK_USAGE_DIR_MASK]; | |
2473 | else | |
2474 | return state_names[bit >> LOCK_USAGE_DIR_MASK]; | |
4f367d8a | 2475 | } |
8e18257d | 2476 | |
948f8376 FW |
2477 | /* |
2478 | * The bit number is encoded like: | |
2479 | * | |
2480 | * bit0: 0 exclusive, 1 read lock | |
2481 | * bit1: 0 used in irq, 1 irq enabled | |
2482 | * bit2-n: state | |
2483 | */ | |
4f367d8a PZ |
2484 | static int exclusive_bit(int new_bit) |
2485 | { | |
bba2a8f1 FW |
2486 | int state = new_bit & LOCK_USAGE_STATE_MASK; |
2487 | int dir = new_bit & LOCK_USAGE_DIR_MASK; | |
8e18257d PZ |
2488 | |
2489 | /* | |
4f367d8a | 2490 | * keep state, bit flip the direction and strip read. |
8e18257d | 2491 | */ |
bba2a8f1 | 2492 | return state | (dir ^ LOCK_USAGE_DIR_MASK); |
4f367d8a PZ |
2493 | } |
2494 | ||
948f8376 FW |
2495 | /* |
2496 | * Observe that when given a bitmask where each bitnr is encoded as above, a | |
2497 | * right shift of the mask transforms the individual bitnrs as -1 and | |
2498 | * conversely, a left shift transforms into +1 for the individual bitnrs. | |
2499 | * | |
2500 | * So for all bits whose number have LOCK_ENABLED_* set (bitnr1 == 1), we can | |
2501 | * create the mask with those bit numbers using LOCK_USED_IN_* (bitnr1 == 0) | |
2502 | * instead by subtracting the bit number by 2, or shifting the mask right by 2. | |
2503 | * | |
2504 | * Similarly, bitnr1 == 0 becomes bitnr1 == 1 by adding 2, or shifting left 2. | |
2505 | * | |
2506 | * So split the mask (note that LOCKF_ENABLED_IRQ_ALL|LOCKF_USED_IN_IRQ_ALL is | |
2507 | * all bits set) and recompose with bitnr1 flipped. | |
2508 | */ | |
2509 | static unsigned long invert_dir_mask(unsigned long mask) | |
2510 | { | |
2511 | unsigned long excl = 0; | |
2512 | ||
2513 | /* Invert dir */ | |
2514 | excl |= (mask & LOCKF_ENABLED_IRQ_ALL) >> LOCK_USAGE_DIR_MASK; | |
2515 | excl |= (mask & LOCKF_USED_IN_IRQ_ALL) << LOCK_USAGE_DIR_MASK; | |
2516 | ||
2517 | return excl; | |
2518 | } | |
2519 | ||
2520 | /* | |
f08e3888 BF |
2521 | * Note that a LOCK_ENABLED_IRQ_*_READ usage and a LOCK_USED_IN_IRQ_*_READ |
2522 | * usage may cause deadlock too, for example: | |
2523 | * | |
2524 | * P1 P2 | |
2525 | * <irq disabled> | |
2526 | * write_lock(l1); <irq enabled> | |
2527 | * read_lock(l2); | |
2528 | * write_lock(l2); | |
2529 | * <in irq> | |
2530 | * read_lock(l1); | |
2531 | * | |
2532 | * , in above case, l1 will be marked as LOCK_USED_IN_IRQ_HARDIRQ_READ and l2 | |
2533 | * will marked as LOCK_ENABLE_IRQ_HARDIRQ_READ, and this is a possible | |
2534 | * deadlock. | |
2535 | * | |
2536 | * In fact, all of the following cases may cause deadlocks: | |
2537 | * | |
2538 | * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_* | |
2539 | * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_* | |
2540 | * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*_READ | |
2541 | * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*_READ | |
2542 | * | |
2543 | * As a result, to calculate the "exclusive mask", first we invert the | |
2544 | * direction (USED_IN/ENABLED) of the original mask, and 1) for all bits with | |
2545 | * bitnr0 set (LOCK_*_READ), add those with bitnr0 cleared (LOCK_*). 2) for all | |
2546 | * bits with bitnr0 cleared (LOCK_*_READ), add those with bitnr0 set (LOCK_*). | |
948f8376 FW |
2547 | */ |
2548 | static unsigned long exclusive_mask(unsigned long mask) | |
2549 | { | |
2550 | unsigned long excl = invert_dir_mask(mask); | |
2551 | ||
948f8376 | 2552 | excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK; |
f08e3888 | 2553 | excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK; |
948f8376 FW |
2554 | |
2555 | return excl; | |
2556 | } | |
2557 | ||
2558 | /* | |
2559 | * Retrieve the _possible_ original mask to which @mask is | |
2560 | * exclusive. Ie: this is the opposite of exclusive_mask(). | |
2561 | * Note that 2 possible original bits can match an exclusive | |
2562 | * bit: one has LOCK_USAGE_READ_MASK set, the other has it | |
2563 | * cleared. So both are returned for each exclusive bit. | |
2564 | */ | |
2565 | static unsigned long original_mask(unsigned long mask) | |
2566 | { | |
2567 | unsigned long excl = invert_dir_mask(mask); | |
2568 | ||
2569 | /* Include read in existing usages */ | |
f08e3888 | 2570 | excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK; |
948f8376 FW |
2571 | excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK; |
2572 | ||
2573 | return excl; | |
2574 | } | |
2575 | ||
2576 | /* | |
2577 | * Find the first pair of bit match between an original | |
2578 | * usage mask and an exclusive usage mask. | |
2579 | */ | |
2580 | static int find_exclusive_match(unsigned long mask, | |
2581 | unsigned long excl_mask, | |
2582 | enum lock_usage_bit *bitp, | |
2583 | enum lock_usage_bit *excl_bitp) | |
2584 | { | |
f08e3888 | 2585 | int bit, excl, excl_read; |
948f8376 FW |
2586 | |
2587 | for_each_set_bit(bit, &mask, LOCK_USED) { | |
f08e3888 BF |
2588 | /* |
2589 | * exclusive_bit() strips the read bit, however, | |
2590 | * LOCK_ENABLED_IRQ_*_READ may cause deadlocks too, so we need | |
2591 | * to search excl | LOCK_USAGE_READ_MASK as well. | |
2592 | */ | |
948f8376 | 2593 | excl = exclusive_bit(bit); |
f08e3888 | 2594 | excl_read = excl | LOCK_USAGE_READ_MASK; |
948f8376 FW |
2595 | if (excl_mask & lock_flag(excl)) { |
2596 | *bitp = bit; | |
2597 | *excl_bitp = excl; | |
2598 | return 0; | |
f08e3888 BF |
2599 | } else if (excl_mask & lock_flag(excl_read)) { |
2600 | *bitp = bit; | |
2601 | *excl_bitp = excl_read; | |
2602 | return 0; | |
948f8376 FW |
2603 | } |
2604 | } | |
2605 | return -1; | |
2606 | } | |
2607 | ||
2608 | /* | |
2609 | * Prove that the new dependency does not connect a hardirq-safe(-read) | |
2610 | * lock with a hardirq-unsafe lock - to achieve this we search | |
2611 | * the backwards-subgraph starting at <prev>, and the | |
2612 | * forwards-subgraph starting at <next>: | |
2613 | */ | |
4f367d8a | 2614 | static int check_irq_usage(struct task_struct *curr, struct held_lock *prev, |
948f8376 | 2615 | struct held_lock *next) |
4f367d8a | 2616 | { |
948f8376 FW |
2617 | unsigned long usage_mask = 0, forward_mask, backward_mask; |
2618 | enum lock_usage_bit forward_bit = 0, backward_bit = 0; | |
3f649ab7 KC |
2619 | struct lock_list *target_entry1; |
2620 | struct lock_list *target_entry; | |
948f8376 | 2621 | struct lock_list this, that; |
b11be024 | 2622 | enum bfs_result ret; |
948f8376 | 2623 | |
8e18257d | 2624 | /* |
948f8376 FW |
2625 | * Step 1: gather all hard/soft IRQs usages backward in an |
2626 | * accumulated usage mask. | |
8e18257d | 2627 | */ |
f08e3888 | 2628 | bfs_init_rootb(&this, prev); |
8e18257d | 2629 | |
948f8376 | 2630 | ret = __bfs_backwards(&this, &usage_mask, usage_accumulate, NULL); |
b11be024 | 2631 | if (bfs_error(ret)) { |
f7c1c6b3 YD |
2632 | print_bfs_bug(ret); |
2633 | return 0; | |
2634 | } | |
8e18257d | 2635 | |
948f8376 FW |
2636 | usage_mask &= LOCKF_USED_IN_IRQ_ALL; |
2637 | if (!usage_mask) | |
2638 | return 1; | |
4f367d8a | 2639 | |
cf40bd16 | 2640 | /* |
948f8376 FW |
2641 | * Step 2: find exclusive uses forward that match the previous |
2642 | * backward accumulated mask. | |
cf40bd16 | 2643 | */ |
948f8376 | 2644 | forward_mask = exclusive_mask(usage_mask); |
cf40bd16 | 2645 | |
f08e3888 | 2646 | bfs_init_root(&that, next); |
4f367d8a | 2647 | |
948f8376 | 2648 | ret = find_usage_forwards(&that, forward_mask, &target_entry1); |
b11be024 | 2649 | if (bfs_error(ret)) { |
f7c1c6b3 YD |
2650 | print_bfs_bug(ret); |
2651 | return 0; | |
2652 | } | |
b11be024 BF |
2653 | if (ret == BFS_RNOMATCH) |
2654 | return 1; | |
cf40bd16 | 2655 | |
948f8376 FW |
2656 | /* |
2657 | * Step 3: we found a bad match! Now retrieve a lock from the backward | |
2658 | * list whose usage mask matches the exclusive usage mask from the | |
2659 | * lock found on the forward list. | |
2660 | */ | |
2661 | backward_mask = original_mask(target_entry1->class->usage_mask); | |
2662 | ||
2663 | ret = find_usage_backwards(&this, backward_mask, &target_entry); | |
b11be024 | 2664 | if (bfs_error(ret)) { |
f7c1c6b3 YD |
2665 | print_bfs_bug(ret); |
2666 | return 0; | |
2667 | } | |
b11be024 | 2668 | if (DEBUG_LOCKS_WARN_ON(ret == BFS_RNOMATCH)) |
948f8376 FW |
2669 | return 1; |
2670 | ||
2671 | /* | |
2672 | * Step 4: narrow down to a pair of incompatible usage bits | |
2673 | * and report it. | |
2674 | */ | |
2675 | ret = find_exclusive_match(target_entry->class->usage_mask, | |
2676 | target_entry1->class->usage_mask, | |
2677 | &backward_bit, &forward_bit); | |
2678 | if (DEBUG_LOCKS_WARN_ON(ret == -1)) | |
2679 | return 1; | |
2680 | ||
f7c1c6b3 YD |
2681 | print_bad_irq_dependency(curr, &this, &that, |
2682 | target_entry, target_entry1, | |
2683 | prev, next, | |
2684 | backward_bit, forward_bit, | |
2685 | state_name(backward_bit)); | |
2686 | ||
2687 | return 0; | |
8e18257d PZ |
2688 | } |
2689 | ||
8e18257d PZ |
2690 | #else |
2691 | ||
948f8376 FW |
2692 | static inline int check_irq_usage(struct task_struct *curr, |
2693 | struct held_lock *prev, struct held_lock *next) | |
8e18257d PZ |
2694 | { |
2695 | return 1; | |
2696 | } | |
b3b9c187 | 2697 | #endif /* CONFIG_TRACE_IRQFLAGS */ |
8e18257d | 2698 | |
b3b9c187 | 2699 | static void inc_chains(int irq_context) |
8e18257d | 2700 | { |
b3b9c187 WL |
2701 | if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT) |
2702 | nr_hardirq_chains++; | |
2703 | else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT) | |
2704 | nr_softirq_chains++; | |
2705 | else | |
2706 | nr_process_chains++; | |
8e18257d PZ |
2707 | } |
2708 | ||
b3b9c187 WL |
2709 | static void dec_chains(int irq_context) |
2710 | { | |
2711 | if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT) | |
2712 | nr_hardirq_chains--; | |
2713 | else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT) | |
2714 | nr_softirq_chains--; | |
2715 | else | |
2716 | nr_process_chains--; | |
2717 | } | |
fbb9ce95 | 2718 | |
48702ecf | 2719 | static void |
f7c1c6b3 | 2720 | print_deadlock_scenario(struct held_lock *nxt, struct held_lock *prv) |
48702ecf SR |
2721 | { |
2722 | struct lock_class *next = hlock_class(nxt); | |
2723 | struct lock_class *prev = hlock_class(prv); | |
2724 | ||
2725 | printk(" Possible unsafe locking scenario:\n\n"); | |
2726 | printk(" CPU0\n"); | |
2727 | printk(" ----\n"); | |
2728 | printk(" lock("); | |
2729 | __print_lock_name(prev); | |
f943fe0f | 2730 | printk(KERN_CONT ");\n"); |
48702ecf SR |
2731 | printk(" lock("); |
2732 | __print_lock_name(next); | |
f943fe0f | 2733 | printk(KERN_CONT ");\n"); |
48702ecf SR |
2734 | printk("\n *** DEADLOCK ***\n\n"); |
2735 | printk(" May be due to missing lock nesting notation\n\n"); | |
2736 | } | |
2737 | ||
f7c1c6b3 | 2738 | static void |
fbb9ce95 IM |
2739 | print_deadlock_bug(struct task_struct *curr, struct held_lock *prev, |
2740 | struct held_lock *next) | |
2741 | { | |
74c383f1 | 2742 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
f7c1c6b3 | 2743 | return; |
fbb9ce95 | 2744 | |
681fbec8 | 2745 | pr_warn("\n"); |
a5dd63ef PM |
2746 | pr_warn("============================================\n"); |
2747 | pr_warn("WARNING: possible recursive locking detected\n"); | |
fbdc4b9a | 2748 | print_kernel_ident(); |
a5dd63ef | 2749 | pr_warn("--------------------------------------------\n"); |
681fbec8 | 2750 | pr_warn("%s/%d is trying to acquire lock:\n", |
ba25f9dc | 2751 | curr->comm, task_pid_nr(curr)); |
fbb9ce95 | 2752 | print_lock(next); |
681fbec8 | 2753 | pr_warn("\nbut task is already holding lock:\n"); |
fbb9ce95 IM |
2754 | print_lock(prev); |
2755 | ||
681fbec8 | 2756 | pr_warn("\nother info that might help us debug this:\n"); |
48702ecf | 2757 | print_deadlock_scenario(next, prev); |
fbb9ce95 IM |
2758 | lockdep_print_held_locks(curr); |
2759 | ||
681fbec8 | 2760 | pr_warn("\nstack backtrace:\n"); |
fbb9ce95 | 2761 | dump_stack(); |
fbb9ce95 IM |
2762 | } |
2763 | ||
2764 | /* | |
2765 | * Check whether we are holding such a class already. | |
2766 | * | |
2767 | * (Note that this has to be done separately, because the graph cannot | |
2768 | * detect such classes of deadlocks.) | |
2769 | * | |
d61fc96a BF |
2770 | * Returns: 0 on deadlock detected, 1 on OK, 2 if another lock with the same |
2771 | * lock class is held but nest_lock is also held, i.e. we rely on the | |
2772 | * nest_lock to avoid the deadlock. | |
fbb9ce95 IM |
2773 | */ |
2774 | static int | |
4609c4f9 | 2775 | check_deadlock(struct task_struct *curr, struct held_lock *next) |
fbb9ce95 IM |
2776 | { |
2777 | struct held_lock *prev; | |
7531e2f3 | 2778 | struct held_lock *nest = NULL; |
fbb9ce95 IM |
2779 | int i; |
2780 | ||
2781 | for (i = 0; i < curr->lockdep_depth; i++) { | |
2782 | prev = curr->held_locks + i; | |
7531e2f3 PZ |
2783 | |
2784 | if (prev->instance == next->nest_lock) | |
2785 | nest = prev; | |
2786 | ||
f82b217e | 2787 | if (hlock_class(prev) != hlock_class(next)) |
fbb9ce95 | 2788 | continue; |
7531e2f3 | 2789 | |
fbb9ce95 IM |
2790 | /* |
2791 | * Allow read-after-read recursion of the same | |
6c9076ec | 2792 | * lock class (i.e. read_lock(lock)+read_lock(lock)): |
fbb9ce95 | 2793 | */ |
4609c4f9 | 2794 | if ((next->read == 2) && prev->read) |
d61fc96a | 2795 | continue; |
7531e2f3 PZ |
2796 | |
2797 | /* | |
2798 | * We're holding the nest_lock, which serializes this lock's | |
2799 | * nesting behaviour. | |
2800 | */ | |
2801 | if (nest) | |
2802 | return 2; | |
2803 | ||
f7c1c6b3 YD |
2804 | print_deadlock_bug(curr, prev, next); |
2805 | return 0; | |
fbb9ce95 IM |
2806 | } |
2807 | return 1; | |
2808 | } | |
2809 | ||
2810 | /* | |
2811 | * There was a chain-cache miss, and we are about to add a new dependency | |
154f185e | 2812 | * to a previous lock. We validate the following rules: |
fbb9ce95 IM |
2813 | * |
2814 | * - would the adding of the <prev> -> <next> dependency create a | |
2815 | * circular dependency in the graph? [== circular deadlock] | |
2816 | * | |
2817 | * - does the new prev->next dependency connect any hardirq-safe lock | |
2818 | * (in the full backwards-subgraph starting at <prev>) with any | |
2819 | * hardirq-unsafe lock (in the full forwards-subgraph starting at | |
2820 | * <next>)? [== illegal lock inversion with hardirq contexts] | |
2821 | * | |
2822 | * - does the new prev->next dependency connect any softirq-safe lock | |
2823 | * (in the full backwards-subgraph starting at <prev>) with any | |
2824 | * softirq-unsafe lock (in the full forwards-subgraph starting at | |
2825 | * <next>)? [== illegal lock inversion with softirq contexts] | |
2826 | * | |
2827 | * any of these scenarios could lead to a deadlock. | |
2828 | * | |
2829 | * Then if all the validations pass, we add the forwards and backwards | |
2830 | * dependency. | |
2831 | */ | |
2832 | static int | |
2833 | check_prev_add(struct task_struct *curr, struct held_lock *prev, | |
bd76eca1 | 2834 | struct held_lock *next, u16 distance, |
12593b74 | 2835 | struct lock_trace **const trace) |
fbb9ce95 IM |
2836 | { |
2837 | struct lock_list *entry; | |
b11be024 | 2838 | enum bfs_result ret; |
fbb9ce95 | 2839 | |
a0b0fd53 BVA |
2840 | if (!hlock_class(prev)->key || !hlock_class(next)->key) { |
2841 | /* | |
2842 | * The warning statements below may trigger a use-after-free | |
2843 | * of the class name. It is better to trigger a use-after free | |
2844 | * and to have the class name most of the time instead of not | |
2845 | * having the class name available. | |
2846 | */ | |
2847 | WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key, | |
2848 | "Detected use-after-free of lock class %px/%s\n", | |
2849 | hlock_class(prev), | |
2850 | hlock_class(prev)->name); | |
2851 | WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key, | |
2852 | "Detected use-after-free of lock class %px/%s\n", | |
2853 | hlock_class(next), | |
2854 | hlock_class(next)->name); | |
2855 | return 2; | |
2856 | } | |
2857 | ||
fbb9ce95 IM |
2858 | /* |
2859 | * Prove that the new <prev> -> <next> dependency would not | |
2860 | * create a circular dependency in the graph. (We do this by | |
154f185e YD |
2861 | * a breadth-first search into the graph starting at <next>, |
2862 | * and check whether we can reach <prev>.) | |
fbb9ce95 | 2863 | * |
154f185e YD |
2864 | * The search is limited by the size of the circular queue (i.e., |
2865 | * MAX_CIRCULAR_QUEUE_SIZE) which keeps track of a breadth of nodes | |
2866 | * in the graph whose neighbours are to be checked. | |
fbb9ce95 | 2867 | */ |
8c2c2b44 | 2868 | ret = check_noncircular(next, prev, trace); |
b11be024 | 2869 | if (unlikely(bfs_error(ret) || ret == BFS_RMATCH)) |
f7c1c6b3 | 2870 | return 0; |
c94aa5ca | 2871 | |
948f8376 | 2872 | if (!check_irq_usage(curr, prev, next)) |
fbb9ce95 IM |
2873 | return 0; |
2874 | ||
fbb9ce95 IM |
2875 | /* |
2876 | * Is the <prev> -> <next> dependency already present? | |
2877 | * | |
2878 | * (this may occur even though this is a new chain: consider | |
2879 | * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3 | |
2880 | * chains - the second one will be new, but L1 already has | |
2881 | * L2 added to its dependency list, due to the first chain.) | |
2882 | */ | |
f82b217e DJ |
2883 | list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) { |
2884 | if (entry->class == hlock_class(next)) { | |
068135e6 JB |
2885 | if (distance == 1) |
2886 | entry->distance = 1; | |
3454a36d BF |
2887 | entry->dep |= calc_dep(prev, next); |
2888 | ||
2889 | /* | |
2890 | * Also, update the reverse dependency in @next's | |
2891 | * ->locks_before list. | |
2892 | * | |
2893 | * Here we reuse @entry as the cursor, which is fine | |
2894 | * because we won't go to the next iteration of the | |
2895 | * outer loop: | |
2896 | * | |
2897 | * For normal cases, we return in the inner loop. | |
2898 | * | |
2899 | * If we fail to return, we have inconsistency, i.e. | |
2900 | * <prev>::locks_after contains <next> while | |
2901 | * <next>::locks_before doesn't contain <prev>. In | |
2902 | * that case, we return after the inner and indicate | |
2903 | * something is wrong. | |
2904 | */ | |
2905 | list_for_each_entry(entry, &hlock_class(next)->locks_before, entry) { | |
2906 | if (entry->class == hlock_class(prev)) { | |
2907 | if (distance == 1) | |
2908 | entry->distance = 1; | |
2909 | entry->dep |= calc_depb(prev, next); | |
2910 | return 1; | |
2911 | } | |
2912 | } | |
2913 | ||
2914 | /* <prev> is not found in <next>::locks_before */ | |
2915 | return 0; | |
068135e6 | 2916 | } |
fbb9ce95 IM |
2917 | } |
2918 | ||
68e9dc29 | 2919 | #ifdef CONFIG_LOCKDEP_SMALL |
ae813308 PZ |
2920 | /* |
2921 | * Is the <prev> -> <next> link redundant? | |
2922 | */ | |
8c2c2b44 | 2923 | ret = check_redundant(prev, next); |
b11be024 BF |
2924 | if (bfs_error(ret)) |
2925 | return 0; | |
2926 | else if (ret == BFS_RMATCH) | |
2927 | return 2; | |
68e9dc29 | 2928 | #endif |
ae813308 | 2929 | |
12593b74 BVA |
2930 | if (!*trace) { |
2931 | *trace = save_trace(); | |
2932 | if (!*trace) | |
2933 | return 0; | |
2934 | } | |
4726f2a6 | 2935 | |
fbb9ce95 IM |
2936 | /* |
2937 | * Ok, all validations passed, add the new lock | |
2938 | * to the previous lock's dependency list: | |
2939 | */ | |
86cffb80 | 2940 | ret = add_lock_to_list(hlock_class(next), hlock_class(prev), |
f82b217e | 2941 | &hlock_class(prev)->locks_after, |
3454a36d BF |
2942 | next->acquire_ip, distance, |
2943 | calc_dep(prev, next), | |
2944 | *trace); | |
068135e6 | 2945 | |
fbb9ce95 IM |
2946 | if (!ret) |
2947 | return 0; | |
910b1b2e | 2948 | |
86cffb80 | 2949 | ret = add_lock_to_list(hlock_class(prev), hlock_class(next), |
f82b217e | 2950 | &hlock_class(next)->locks_before, |
3454a36d BF |
2951 | next->acquire_ip, distance, |
2952 | calc_depb(prev, next), | |
2953 | *trace); | |
910b1b2e JP |
2954 | if (!ret) |
2955 | return 0; | |
fbb9ce95 | 2956 | |
70911fdc | 2957 | return 2; |
8e18257d | 2958 | } |
fbb9ce95 | 2959 | |
8e18257d PZ |
2960 | /* |
2961 | * Add the dependency to all directly-previous locks that are 'relevant'. | |
2962 | * The ones that are relevant are (in increasing distance from curr): | |
2963 | * all consecutive trylock entries and the final non-trylock entry - or | |
2964 | * the end of this context's lock-chain - whichever comes first. | |
2965 | */ | |
2966 | static int | |
2967 | check_prevs_add(struct task_struct *curr, struct held_lock *next) | |
2968 | { | |
12593b74 | 2969 | struct lock_trace *trace = NULL; |
8e18257d PZ |
2970 | int depth = curr->lockdep_depth; |
2971 | struct held_lock *hlock; | |
d6d897ce | 2972 | |
fbb9ce95 | 2973 | /* |
8e18257d PZ |
2974 | * Debugging checks. |
2975 | * | |
2976 | * Depth must not be zero for a non-head lock: | |
fbb9ce95 | 2977 | */ |
8e18257d PZ |
2978 | if (!depth) |
2979 | goto out_bug; | |
fbb9ce95 | 2980 | /* |
8e18257d PZ |
2981 | * At least two relevant locks must exist for this |
2982 | * to be a head: | |
fbb9ce95 | 2983 | */ |
8e18257d PZ |
2984 | if (curr->held_locks[depth].irq_context != |
2985 | curr->held_locks[depth-1].irq_context) | |
2986 | goto out_bug; | |
74c383f1 | 2987 | |
8e18257d | 2988 | for (;;) { |
bd76eca1 | 2989 | u16 distance = curr->lockdep_depth - depth + 1; |
1b5ff816 | 2990 | hlock = curr->held_locks + depth - 1; |
e966eaee | 2991 | |
621c9dac BF |
2992 | if (hlock->check) { |
2993 | int ret = check_prev_add(curr, hlock, next, distance, &trace); | |
e966eaee IM |
2994 | if (!ret) |
2995 | return 0; | |
2996 | ||
ce07a941 | 2997 | /* |
e966eaee IM |
2998 | * Stop after the first non-trylock entry, |
2999 | * as non-trylock entries have added their | |
3000 | * own direct dependencies already, so this | |
3001 | * lock is connected to them indirectly: | |
ce07a941 | 3002 | */ |
e966eaee IM |
3003 | if (!hlock->trylock) |
3004 | break; | |
74c383f1 | 3005 | } |
e966eaee | 3006 | |
8e18257d PZ |
3007 | depth--; |
3008 | /* | |
3009 | * End of lock-stack? | |
3010 | */ | |
3011 | if (!depth) | |
3012 | break; | |
3013 | /* | |
3014 | * Stop the search if we cross into another context: | |
3015 | */ | |
3016 | if (curr->held_locks[depth].irq_context != | |
3017 | curr->held_locks[depth-1].irq_context) | |
3018 | break; | |
fbb9ce95 | 3019 | } |
8e18257d PZ |
3020 | return 1; |
3021 | out_bug: | |
3022 | if (!debug_locks_off_graph_unlock()) | |
3023 | return 0; | |
fbb9ce95 | 3024 | |
0119fee4 PZ |
3025 | /* |
3026 | * Clearly we all shouldn't be here, but since we made it we | |
3027 | * can reliable say we messed up our state. See the above two | |
3028 | * gotos for reasons why we could possibly end up here. | |
3029 | */ | |
8e18257d | 3030 | WARN_ON(1); |
fbb9ce95 | 3031 | |
8e18257d | 3032 | return 0; |
fbb9ce95 IM |
3033 | } |
3034 | ||
443cd507 | 3035 | struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS]; |
de4643a7 | 3036 | static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS); |
443cd507 | 3037 | static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS]; |
797b82eb | 3038 | unsigned long nr_zapped_lock_chains; |
810507fe WL |
3039 | unsigned int nr_free_chain_hlocks; /* Free chain_hlocks in buckets */ |
3040 | unsigned int nr_lost_chain_hlocks; /* Lost chain_hlocks */ | |
3041 | unsigned int nr_large_chain_blocks; /* size > MAX_CHAIN_BUCKETS */ | |
3042 | ||
3043 | /* | |
3044 | * The first 2 chain_hlocks entries in the chain block in the bucket | |
3045 | * list contains the following meta data: | |
3046 | * | |
3047 | * entry[0]: | |
3048 | * Bit 15 - always set to 1 (it is not a class index) | |
3049 | * Bits 0-14 - upper 15 bits of the next block index | |
3050 | * entry[1] - lower 16 bits of next block index | |
3051 | * | |
3052 | * A next block index of all 1 bits means it is the end of the list. | |
3053 | * | |
3054 | * On the unsized bucket (bucket-0), the 3rd and 4th entries contain | |
3055 | * the chain block size: | |
3056 | * | |
3057 | * entry[2] - upper 16 bits of the chain block size | |
3058 | * entry[3] - lower 16 bits of the chain block size | |
3059 | */ | |
3060 | #define MAX_CHAIN_BUCKETS 16 | |
3061 | #define CHAIN_BLK_FLAG (1U << 15) | |
3062 | #define CHAIN_BLK_LIST_END 0xFFFFU | |
3063 | ||
3064 | static int chain_block_buckets[MAX_CHAIN_BUCKETS]; | |
3065 | ||
3066 | static inline int size_to_bucket(int size) | |
3067 | { | |
3068 | if (size > MAX_CHAIN_BUCKETS) | |
3069 | return 0; | |
3070 | ||
3071 | return size - 1; | |
3072 | } | |
3073 | ||
3074 | /* | |
3075 | * Iterate all the chain blocks in a bucket. | |
3076 | */ | |
3077 | #define for_each_chain_block(bucket, prev, curr) \ | |
3078 | for ((prev) = -1, (curr) = chain_block_buckets[bucket]; \ | |
3079 | (curr) >= 0; \ | |
3080 | (prev) = (curr), (curr) = chain_block_next(curr)) | |
3081 | ||
3082 | /* | |
3083 | * next block or -1 | |
3084 | */ | |
3085 | static inline int chain_block_next(int offset) | |
3086 | { | |
3087 | int next = chain_hlocks[offset]; | |
3088 | ||
3089 | WARN_ON_ONCE(!(next & CHAIN_BLK_FLAG)); | |
3090 | ||
3091 | if (next == CHAIN_BLK_LIST_END) | |
3092 | return -1; | |
3093 | ||
3094 | next &= ~CHAIN_BLK_FLAG; | |
3095 | next <<= 16; | |
3096 | next |= chain_hlocks[offset + 1]; | |
3097 | ||
3098 | return next; | |
3099 | } | |
3100 | ||
3101 | /* | |
3102 | * bucket-0 only | |
3103 | */ | |
3104 | static inline int chain_block_size(int offset) | |
3105 | { | |
3106 | return (chain_hlocks[offset + 2] << 16) | chain_hlocks[offset + 3]; | |
3107 | } | |
3108 | ||
3109 | static inline void init_chain_block(int offset, int next, int bucket, int size) | |
3110 | { | |
3111 | chain_hlocks[offset] = (next >> 16) | CHAIN_BLK_FLAG; | |
3112 | chain_hlocks[offset + 1] = (u16)next; | |
3113 | ||
3114 | if (size && !bucket) { | |
3115 | chain_hlocks[offset + 2] = size >> 16; | |
3116 | chain_hlocks[offset + 3] = (u16)size; | |
3117 | } | |
3118 | } | |
3119 | ||
3120 | static inline void add_chain_block(int offset, int size) | |
3121 | { | |
3122 | int bucket = size_to_bucket(size); | |
3123 | int next = chain_block_buckets[bucket]; | |
3124 | int prev, curr; | |
3125 | ||
3126 | if (unlikely(size < 2)) { | |
3127 | /* | |
3128 | * We can't store single entries on the freelist. Leak them. | |
3129 | * | |
3130 | * One possible way out would be to uniquely mark them, other | |
3131 | * than with CHAIN_BLK_FLAG, such that we can recover them when | |
3132 | * the block before it is re-added. | |
3133 | */ | |
3134 | if (size) | |
3135 | nr_lost_chain_hlocks++; | |
3136 | return; | |
3137 | } | |
3138 | ||
3139 | nr_free_chain_hlocks += size; | |
3140 | if (!bucket) { | |
3141 | nr_large_chain_blocks++; | |
3142 | ||
3143 | /* | |
3144 | * Variable sized, sort large to small. | |
3145 | */ | |
3146 | for_each_chain_block(0, prev, curr) { | |
3147 | if (size >= chain_block_size(curr)) | |
3148 | break; | |
3149 | } | |
3150 | init_chain_block(offset, curr, 0, size); | |
3151 | if (prev < 0) | |
3152 | chain_block_buckets[0] = offset; | |
3153 | else | |
3154 | init_chain_block(prev, offset, 0, 0); | |
3155 | return; | |
3156 | } | |
3157 | /* | |
3158 | * Fixed size, add to head. | |
3159 | */ | |
3160 | init_chain_block(offset, next, bucket, size); | |
3161 | chain_block_buckets[bucket] = offset; | |
3162 | } | |
3163 | ||
3164 | /* | |
3165 | * Only the first block in the list can be deleted. | |
3166 | * | |
3167 | * For the variable size bucket[0], the first block (the largest one) is | |
3168 | * returned, broken up and put back into the pool. So if a chain block of | |
3169 | * length > MAX_CHAIN_BUCKETS is ever used and zapped, it will just be | |
3170 | * queued up after the primordial chain block and never be used until the | |
3171 | * hlock entries in the primordial chain block is almost used up. That | |
3172 | * causes fragmentation and reduce allocation efficiency. That can be | |
3173 | * monitored by looking at the "large chain blocks" number in lockdep_stats. | |
3174 | */ | |
3175 | static inline void del_chain_block(int bucket, int size, int next) | |
3176 | { | |
3177 | nr_free_chain_hlocks -= size; | |
3178 | chain_block_buckets[bucket] = next; | |
3179 | ||
3180 | if (!bucket) | |
3181 | nr_large_chain_blocks--; | |
3182 | } | |
3183 | ||
3184 | static void init_chain_block_buckets(void) | |
3185 | { | |
3186 | int i; | |
3187 | ||
3188 | for (i = 0; i < MAX_CHAIN_BUCKETS; i++) | |
3189 | chain_block_buckets[i] = -1; | |
3190 | ||
3191 | add_chain_block(0, ARRAY_SIZE(chain_hlocks)); | |
3192 | } | |
3193 | ||
3194 | /* | |
3195 | * Return offset of a chain block of the right size or -1 if not found. | |
3196 | * | |
3197 | * Fairly simple worst-fit allocator with the addition of a number of size | |
3198 | * specific free lists. | |
3199 | */ | |
3200 | static int alloc_chain_hlocks(int req) | |
3201 | { | |
3202 | int bucket, curr, size; | |
3203 | ||
3204 | /* | |
3205 | * We rely on the MSB to act as an escape bit to denote freelist | |
3206 | * pointers. Make sure this bit isn't set in 'normal' class_idx usage. | |
3207 | */ | |
3208 | BUILD_BUG_ON((MAX_LOCKDEP_KEYS-1) & CHAIN_BLK_FLAG); | |
3209 | ||
3210 | init_data_structures_once(); | |
3211 | ||
3212 | if (nr_free_chain_hlocks < req) | |
3213 | return -1; | |
3214 | ||
3215 | /* | |
3216 | * We require a minimum of 2 (u16) entries to encode a freelist | |
3217 | * 'pointer'. | |
3218 | */ | |
3219 | req = max(req, 2); | |
3220 | bucket = size_to_bucket(req); | |
3221 | curr = chain_block_buckets[bucket]; | |
3222 | ||
3223 | if (bucket) { | |
3224 | if (curr >= 0) { | |
3225 | del_chain_block(bucket, req, chain_block_next(curr)); | |
3226 | return curr; | |
3227 | } | |
3228 | /* Try bucket 0 */ | |
3229 | curr = chain_block_buckets[0]; | |
3230 | } | |
3231 | ||
3232 | /* | |
3233 | * The variable sized freelist is sorted by size; the first entry is | |
3234 | * the largest. Use it if it fits. | |
3235 | */ | |
3236 | if (curr >= 0) { | |
3237 | size = chain_block_size(curr); | |
3238 | if (likely(size >= req)) { | |
3239 | del_chain_block(0, size, chain_block_next(curr)); | |
3240 | add_chain_block(curr + req, size - req); | |
3241 | return curr; | |
3242 | } | |
3243 | } | |
3244 | ||
3245 | /* | |
3246 | * Last resort, split a block in a larger sized bucket. | |
3247 | */ | |
3248 | for (size = MAX_CHAIN_BUCKETS; size > req; size--) { | |
3249 | bucket = size_to_bucket(size); | |
3250 | curr = chain_block_buckets[bucket]; | |
3251 | if (curr < 0) | |
3252 | continue; | |
3253 | ||
3254 | del_chain_block(bucket, size, chain_block_next(curr)); | |
3255 | add_chain_block(curr + req, size - req); | |
3256 | return curr; | |
3257 | } | |
3258 | ||
3259 | return -1; | |
3260 | } | |
3261 | ||
3262 | static inline void free_chain_hlocks(int base, int size) | |
3263 | { | |
3264 | add_chain_block(base, max(size, 2)); | |
3265 | } | |
443cd507 YH |
3266 | |
3267 | struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i) | |
3268 | { | |
f611e8cf BF |
3269 | u16 chain_hlock = chain_hlocks[chain->base + i]; |
3270 | unsigned int class_idx = chain_hlock_class_idx(chain_hlock); | |
3271 | ||
3272 | return lock_classes + class_idx - 1; | |
443cd507 | 3273 | } |
8e18257d | 3274 | |
9e4e7554 IM |
3275 | /* |
3276 | * Returns the index of the first held_lock of the current chain | |
3277 | */ | |
3278 | static inline int get_first_held_lock(struct task_struct *curr, | |
3279 | struct held_lock *hlock) | |
3280 | { | |
3281 | int i; | |
3282 | struct held_lock *hlock_curr; | |
3283 | ||
3284 | for (i = curr->lockdep_depth - 1; i >= 0; i--) { | |
3285 | hlock_curr = curr->held_locks + i; | |
3286 | if (hlock_curr->irq_context != hlock->irq_context) | |
3287 | break; | |
3288 | ||
3289 | } | |
3290 | ||
3291 | return ++i; | |
3292 | } | |
3293 | ||
5c8a010c | 3294 | #ifdef CONFIG_DEBUG_LOCKDEP |
39e2e173 AAF |
3295 | /* |
3296 | * Returns the next chain_key iteration | |
3297 | */ | |
f611e8cf | 3298 | static u64 print_chain_key_iteration(u16 hlock_id, u64 chain_key) |
39e2e173 | 3299 | { |
f611e8cf | 3300 | u64 new_chain_key = iterate_chain_key(chain_key, hlock_id); |
39e2e173 | 3301 | |
f611e8cf BF |
3302 | printk(" hlock_id:%d -> chain_key:%016Lx", |
3303 | (unsigned int)hlock_id, | |
39e2e173 AAF |
3304 | (unsigned long long)new_chain_key); |
3305 | return new_chain_key; | |
3306 | } | |
3307 | ||
3308 | static void | |
3309 | print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next) | |
3310 | { | |
3311 | struct held_lock *hlock; | |
f6ec8829 | 3312 | u64 chain_key = INITIAL_CHAIN_KEY; |
39e2e173 | 3313 | int depth = curr->lockdep_depth; |
834494b2 | 3314 | int i = get_first_held_lock(curr, hlock_next); |
39e2e173 | 3315 | |
834494b2 YD |
3316 | printk("depth: %u (irq_context %u)\n", depth - i + 1, |
3317 | hlock_next->irq_context); | |
3318 | for (; i < depth; i++) { | |
39e2e173 | 3319 | hlock = curr->held_locks + i; |
f611e8cf | 3320 | chain_key = print_chain_key_iteration(hlock_id(hlock), chain_key); |
39e2e173 AAF |
3321 | |
3322 | print_lock(hlock); | |
3323 | } | |
3324 | ||
f611e8cf | 3325 | print_chain_key_iteration(hlock_id(hlock_next), chain_key); |
39e2e173 AAF |
3326 | print_lock(hlock_next); |
3327 | } | |
3328 | ||
3329 | static void print_chain_keys_chain(struct lock_chain *chain) | |
3330 | { | |
3331 | int i; | |
f6ec8829 | 3332 | u64 chain_key = INITIAL_CHAIN_KEY; |
f611e8cf | 3333 | u16 hlock_id; |
39e2e173 AAF |
3334 | |
3335 | printk("depth: %u\n", chain->depth); | |
3336 | for (i = 0; i < chain->depth; i++) { | |
f611e8cf BF |
3337 | hlock_id = chain_hlocks[chain->base + i]; |
3338 | chain_key = print_chain_key_iteration(hlock_id, chain_key); | |
39e2e173 | 3339 | |
f611e8cf | 3340 | print_lock_name(lock_classes + chain_hlock_class_idx(hlock_id) - 1); |
39e2e173 AAF |
3341 | printk("\n"); |
3342 | } | |
3343 | } | |
3344 | ||
3345 | static void print_collision(struct task_struct *curr, | |
3346 | struct held_lock *hlock_next, | |
3347 | struct lock_chain *chain) | |
3348 | { | |
681fbec8 | 3349 | pr_warn("\n"); |
a5dd63ef PM |
3350 | pr_warn("============================\n"); |
3351 | pr_warn("WARNING: chain_key collision\n"); | |
39e2e173 | 3352 | print_kernel_ident(); |
a5dd63ef | 3353 | pr_warn("----------------------------\n"); |
681fbec8 PM |
3354 | pr_warn("%s/%d: ", current->comm, task_pid_nr(current)); |
3355 | pr_warn("Hash chain already cached but the contents don't match!\n"); | |
39e2e173 | 3356 | |
681fbec8 | 3357 | pr_warn("Held locks:"); |
39e2e173 AAF |
3358 | print_chain_keys_held_locks(curr, hlock_next); |
3359 | ||
681fbec8 | 3360 | pr_warn("Locks in cached chain:"); |
39e2e173 AAF |
3361 | print_chain_keys_chain(chain); |
3362 | ||
681fbec8 | 3363 | pr_warn("\nstack backtrace:\n"); |
39e2e173 AAF |
3364 | dump_stack(); |
3365 | } | |
5c8a010c | 3366 | #endif |
39e2e173 | 3367 | |
9e4e7554 IM |
3368 | /* |
3369 | * Checks whether the chain and the current held locks are consistent | |
3370 | * in depth and also in content. If they are not it most likely means | |
3371 | * that there was a collision during the calculation of the chain_key. | |
3372 | * Returns: 0 not passed, 1 passed | |
3373 | */ | |
3374 | static int check_no_collision(struct task_struct *curr, | |
3375 | struct held_lock *hlock, | |
3376 | struct lock_chain *chain) | |
3377 | { | |
3378 | #ifdef CONFIG_DEBUG_LOCKDEP | |
3379 | int i, j, id; | |
3380 | ||
3381 | i = get_first_held_lock(curr, hlock); | |
3382 | ||
39e2e173 AAF |
3383 | if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) { |
3384 | print_collision(curr, hlock, chain); | |
9e4e7554 | 3385 | return 0; |
39e2e173 | 3386 | } |
9e4e7554 IM |
3387 | |
3388 | for (j = 0; j < chain->depth - 1; j++, i++) { | |
f611e8cf | 3389 | id = hlock_id(&curr->held_locks[i]); |
9e4e7554 | 3390 | |
39e2e173 AAF |
3391 | if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) { |
3392 | print_collision(curr, hlock, chain); | |
9e4e7554 | 3393 | return 0; |
39e2e173 | 3394 | } |
9e4e7554 IM |
3395 | } |
3396 | #endif | |
3397 | return 1; | |
3398 | } | |
3399 | ||
2212684a BVA |
3400 | /* |
3401 | * Given an index that is >= -1, return the index of the next lock chain. | |
3402 | * Return -2 if there is no next lock chain. | |
3403 | */ | |
3404 | long lockdep_next_lockchain(long i) | |
3405 | { | |
de4643a7 BVA |
3406 | i = find_next_bit(lock_chains_in_use, ARRAY_SIZE(lock_chains), i + 1); |
3407 | return i < ARRAY_SIZE(lock_chains) ? i : -2; | |
2212684a BVA |
3408 | } |
3409 | ||
3410 | unsigned long lock_chain_count(void) | |
3411 | { | |
de4643a7 BVA |
3412 | return bitmap_weight(lock_chains_in_use, ARRAY_SIZE(lock_chains)); |
3413 | } | |
3414 | ||
3415 | /* Must be called with the graph lock held. */ | |
3416 | static struct lock_chain *alloc_lock_chain(void) | |
3417 | { | |
3418 | int idx = find_first_zero_bit(lock_chains_in_use, | |
3419 | ARRAY_SIZE(lock_chains)); | |
3420 | ||
3421 | if (unlikely(idx >= ARRAY_SIZE(lock_chains))) | |
3422 | return NULL; | |
3423 | __set_bit(idx, lock_chains_in_use); | |
3424 | return lock_chains + idx; | |
2212684a BVA |
3425 | } |
3426 | ||
fbb9ce95 | 3427 | /* |
545c23f2 BP |
3428 | * Adds a dependency chain into chain hashtable. And must be called with |
3429 | * graph_lock held. | |
3430 | * | |
3431 | * Return 0 if fail, and graph_lock is released. | |
3432 | * Return 1 if succeed, with graph_lock held. | |
fbb9ce95 | 3433 | */ |
545c23f2 BP |
3434 | static inline int add_chain_cache(struct task_struct *curr, |
3435 | struct held_lock *hlock, | |
3436 | u64 chain_key) | |
fbb9ce95 | 3437 | { |
a63f38cc | 3438 | struct hlist_head *hash_head = chainhashentry(chain_key); |
fbb9ce95 | 3439 | struct lock_chain *chain; |
e0944ee6 | 3440 | int i, j; |
fbb9ce95 | 3441 | |
0119fee4 | 3442 | /* |
527af3ea | 3443 | * The caller must hold the graph lock, ensure we've got IRQs |
0119fee4 PZ |
3444 | * disabled to make this an IRQ-safe lock.. for recursion reasons |
3445 | * lockdep won't complain about its own locking errors. | |
3446 | */ | |
248efb21 | 3447 | if (lockdep_assert_locked()) |
381a2292 | 3448 | return 0; |
9e4e7554 | 3449 | |
de4643a7 BVA |
3450 | chain = alloc_lock_chain(); |
3451 | if (!chain) { | |
74c383f1 IM |
3452 | if (!debug_locks_off_graph_unlock()) |
3453 | return 0; | |
3454 | ||
2c522836 | 3455 | print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!"); |
eedeeabd | 3456 | dump_stack(); |
fbb9ce95 IM |
3457 | return 0; |
3458 | } | |
fbb9ce95 | 3459 | chain->chain_key = chain_key; |
443cd507 | 3460 | chain->irq_context = hlock->irq_context; |
9e4e7554 | 3461 | i = get_first_held_lock(curr, hlock); |
443cd507 | 3462 | chain->depth = curr->lockdep_depth + 1 - i; |
75dd602a PZ |
3463 | |
3464 | BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks)); | |
3465 | BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks)); | |
3466 | BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes)); | |
3467 | ||
810507fe WL |
3468 | j = alloc_chain_hlocks(chain->depth); |
3469 | if (j < 0) { | |
f9af456a | 3470 | if (!debug_locks_off_graph_unlock()) |
75dd602a PZ |
3471 | return 0; |
3472 | ||
3473 | print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!"); | |
3474 | dump_stack(); | |
3475 | return 0; | |
3476 | } | |
75dd602a | 3477 | |
810507fe WL |
3478 | chain->base = j; |
3479 | for (j = 0; j < chain->depth - 1; j++, i++) { | |
f611e8cf | 3480 | int lock_id = hlock_id(curr->held_locks + i); |
810507fe WL |
3481 | |
3482 | chain_hlocks[chain->base + j] = lock_id; | |
3483 | } | |
f611e8cf | 3484 | chain_hlocks[chain->base + j] = hlock_id(hlock); |
a63f38cc | 3485 | hlist_add_head_rcu(&chain->entry, hash_head); |
bd6d29c2 | 3486 | debug_atomic_inc(chain_lookup_misses); |
b3b9c187 | 3487 | inc_chains(chain->irq_context); |
8e18257d PZ |
3488 | |
3489 | return 1; | |
3490 | } | |
3491 | ||
545c23f2 | 3492 | /* |
a0b0fd53 BVA |
3493 | * Look up a dependency chain. Must be called with either the graph lock or |
3494 | * the RCU read lock held. | |
545c23f2 BP |
3495 | */ |
3496 | static inline struct lock_chain *lookup_chain_cache(u64 chain_key) | |
3497 | { | |
3498 | struct hlist_head *hash_head = chainhashentry(chain_key); | |
3499 | struct lock_chain *chain; | |
3500 | ||
545c23f2 | 3501 | hlist_for_each_entry_rcu(chain, hash_head, entry) { |
a0b0fd53 | 3502 | if (READ_ONCE(chain->chain_key) == chain_key) { |
545c23f2 BP |
3503 | debug_atomic_inc(chain_lookup_hits); |
3504 | return chain; | |
3505 | } | |
3506 | } | |
3507 | return NULL; | |
3508 | } | |
3509 | ||
3510 | /* | |
3511 | * If the key is not present yet in dependency chain cache then | |
3512 | * add it and return 1 - in this case the new dependency chain is | |
3513 | * validated. If the key is already hashed, return 0. | |
3514 | * (On return with 1 graph_lock is held.) | |
3515 | */ | |
3516 | static inline int lookup_chain_cache_add(struct task_struct *curr, | |
3517 | struct held_lock *hlock, | |
3518 | u64 chain_key) | |
3519 | { | |
3520 | struct lock_class *class = hlock_class(hlock); | |
3521 | struct lock_chain *chain = lookup_chain_cache(chain_key); | |
3522 | ||
3523 | if (chain) { | |
3524 | cache_hit: | |
3525 | if (!check_no_collision(curr, hlock, chain)) | |
3526 | return 0; | |
3527 | ||
3528 | if (very_verbose(class)) { | |
3529 | printk("\nhash chain already cached, key: " | |
04860d48 | 3530 | "%016Lx tail class: [%px] %s\n", |
545c23f2 BP |
3531 | (unsigned long long)chain_key, |
3532 | class->key, class->name); | |
3533 | } | |
3534 | ||
3535 | return 0; | |
3536 | } | |
3537 | ||
3538 | if (very_verbose(class)) { | |
04860d48 | 3539 | printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n", |
545c23f2 BP |
3540 | (unsigned long long)chain_key, class->key, class->name); |
3541 | } | |
3542 | ||
3543 | if (!graph_lock()) | |
3544 | return 0; | |
3545 | ||
3546 | /* | |
3547 | * We have to walk the chain again locked - to avoid duplicates: | |
3548 | */ | |
3549 | chain = lookup_chain_cache(chain_key); | |
3550 | if (chain) { | |
3551 | graph_unlock(); | |
3552 | goto cache_hit; | |
3553 | } | |
3554 | ||
3555 | if (!add_chain_cache(curr, hlock, chain_key)) | |
3556 | return 0; | |
3557 | ||
3558 | return 1; | |
3559 | } | |
3560 | ||
0b9fc8ec YD |
3561 | static int validate_chain(struct task_struct *curr, |
3562 | struct held_lock *hlock, | |
3563 | int chain_head, u64 chain_key) | |
8e18257d PZ |
3564 | { |
3565 | /* | |
3566 | * Trylock needs to maintain the stack of held locks, but it | |
3567 | * does not add new dependencies, because trylock can be done | |
3568 | * in any order. | |
3569 | * | |
3570 | * We look up the chain_key and do the O(N^2) check and update of | |
3571 | * the dependencies only if this is a new dependency chain. | |
545c23f2 | 3572 | * (If lookup_chain_cache_add() return with 1 it acquires |
8e18257d PZ |
3573 | * graph_lock for us) |
3574 | */ | |
fb9edbe9 | 3575 | if (!hlock->trylock && hlock->check && |
545c23f2 | 3576 | lookup_chain_cache_add(curr, hlock, chain_key)) { |
8e18257d PZ |
3577 | /* |
3578 | * Check whether last held lock: | |
3579 | * | |
3580 | * - is irq-safe, if this lock is irq-unsafe | |
3581 | * - is softirq-safe, if this lock is hardirq-unsafe | |
3582 | * | |
3583 | * And check whether the new lock's dependency graph | |
31a490e5 | 3584 | * could lead back to the previous lock: |
8e18257d | 3585 | * |
31a490e5 YD |
3586 | * - within the current held-lock stack |
3587 | * - across our accumulated lock dependency records | |
3588 | * | |
3589 | * any of these scenarios could lead to a deadlock. | |
3590 | */ | |
3591 | /* | |
3592 | * The simple case: does the current hold the same lock | |
3593 | * already? | |
8e18257d | 3594 | */ |
4609c4f9 | 3595 | int ret = check_deadlock(curr, hlock); |
8e18257d PZ |
3596 | |
3597 | if (!ret) | |
3598 | return 0; | |
8e18257d PZ |
3599 | /* |
3600 | * Add dependency only if this lock is not the head | |
d61fc96a BF |
3601 | * of the chain, and if the new lock introduces no more |
3602 | * lock dependency (because we already hold a lock with the | |
3603 | * same lock class) nor deadlock (because the nest_lock | |
3604 | * serializes nesting locks), see the comments for | |
3605 | * check_deadlock(). | |
8e18257d | 3606 | */ |
545c23f2 | 3607 | if (!chain_head && ret != 2) { |
8e18257d PZ |
3608 | if (!check_prevs_add(curr, hlock)) |
3609 | return 0; | |
545c23f2 BP |
3610 | } |
3611 | ||
8e18257d | 3612 | graph_unlock(); |
545c23f2 BP |
3613 | } else { |
3614 | /* after lookup_chain_cache_add(): */ | |
8e18257d PZ |
3615 | if (unlikely(!debug_locks)) |
3616 | return 0; | |
545c23f2 | 3617 | } |
fbb9ce95 IM |
3618 | |
3619 | return 1; | |
3620 | } | |
8e18257d PZ |
3621 | #else |
3622 | static inline int validate_chain(struct task_struct *curr, | |
0b9fc8ec YD |
3623 | struct held_lock *hlock, |
3624 | int chain_head, u64 chain_key) | |
8e18257d PZ |
3625 | { |
3626 | return 1; | |
3627 | } | |
810507fe WL |
3628 | |
3629 | static void init_chain_block_buckets(void) { } | |
e7a38f63 | 3630 | #endif /* CONFIG_PROVE_LOCKING */ |
fbb9ce95 IM |
3631 | |
3632 | /* | |
3633 | * We are building curr_chain_key incrementally, so double-check | |
3634 | * it from scratch, to make sure that it's done correctly: | |
3635 | */ | |
1d09daa5 | 3636 | static void check_chain_key(struct task_struct *curr) |
fbb9ce95 IM |
3637 | { |
3638 | #ifdef CONFIG_DEBUG_LOCKDEP | |
3639 | struct held_lock *hlock, *prev_hlock = NULL; | |
5f18ab5c | 3640 | unsigned int i; |
f6ec8829 | 3641 | u64 chain_key = INITIAL_CHAIN_KEY; |
fbb9ce95 IM |
3642 | |
3643 | for (i = 0; i < curr->lockdep_depth; i++) { | |
3644 | hlock = curr->held_locks + i; | |
3645 | if (chain_key != hlock->prev_chain_key) { | |
3646 | debug_locks_off(); | |
0119fee4 PZ |
3647 | /* |
3648 | * We got mighty confused, our chain keys don't match | |
3649 | * with what we expect, someone trample on our task state? | |
3650 | */ | |
2df8b1d6 | 3651 | WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n", |
fbb9ce95 IM |
3652 | curr->lockdep_depth, i, |
3653 | (unsigned long long)chain_key, | |
3654 | (unsigned long long)hlock->prev_chain_key); | |
fbb9ce95 IM |
3655 | return; |
3656 | } | |
01bb6f0a | 3657 | |
0119fee4 | 3658 | /* |
01bb6f0a YD |
3659 | * hlock->class_idx can't go beyond MAX_LOCKDEP_KEYS, but is |
3660 | * it registered lock class index? | |
0119fee4 | 3661 | */ |
01bb6f0a | 3662 | if (DEBUG_LOCKS_WARN_ON(!test_bit(hlock->class_idx, lock_classes_in_use))) |
381a2292 JP |
3663 | return; |
3664 | ||
fbb9ce95 IM |
3665 | if (prev_hlock && (prev_hlock->irq_context != |
3666 | hlock->irq_context)) | |
f6ec8829 | 3667 | chain_key = INITIAL_CHAIN_KEY; |
f611e8cf | 3668 | chain_key = iterate_chain_key(chain_key, hlock_id(hlock)); |
fbb9ce95 IM |
3669 | prev_hlock = hlock; |
3670 | } | |
3671 | if (chain_key != curr->curr_chain_key) { | |
3672 | debug_locks_off(); | |
0119fee4 PZ |
3673 | /* |
3674 | * More smoking hash instead of calculating it, damn see these | |
3675 | * numbers float.. I bet that a pink elephant stepped on my memory. | |
3676 | */ | |
2df8b1d6 | 3677 | WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n", |
fbb9ce95 IM |
3678 | curr->lockdep_depth, i, |
3679 | (unsigned long long)chain_key, | |
3680 | (unsigned long long)curr->curr_chain_key); | |
fbb9ce95 IM |
3681 | } |
3682 | #endif | |
3683 | } | |
3684 | ||
30a35f79 | 3685 | #ifdef CONFIG_PROVE_LOCKING |
0d2cc3b3 FW |
3686 | static int mark_lock(struct task_struct *curr, struct held_lock *this, |
3687 | enum lock_usage_bit new_bit); | |
3688 | ||
f7c1c6b3 | 3689 | static void print_usage_bug_scenario(struct held_lock *lock) |
282b5c2f SR |
3690 | { |
3691 | struct lock_class *class = hlock_class(lock); | |
3692 | ||
3693 | printk(" Possible unsafe locking scenario:\n\n"); | |
3694 | printk(" CPU0\n"); | |
3695 | printk(" ----\n"); | |
3696 | printk(" lock("); | |
3697 | __print_lock_name(class); | |
f943fe0f | 3698 | printk(KERN_CONT ");\n"); |
282b5c2f SR |
3699 | printk(" <Interrupt>\n"); |
3700 | printk(" lock("); | |
3701 | __print_lock_name(class); | |
f943fe0f | 3702 | printk(KERN_CONT ");\n"); |
282b5c2f SR |
3703 | printk("\n *** DEADLOCK ***\n\n"); |
3704 | } | |
3705 | ||
f7c1c6b3 | 3706 | static void |
8e18257d PZ |
3707 | print_usage_bug(struct task_struct *curr, struct held_lock *this, |
3708 | enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit) | |
3709 | { | |
3710 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) | |
f7c1c6b3 | 3711 | return; |
8e18257d | 3712 | |
681fbec8 | 3713 | pr_warn("\n"); |
a5dd63ef PM |
3714 | pr_warn("================================\n"); |
3715 | pr_warn("WARNING: inconsistent lock state\n"); | |
fbdc4b9a | 3716 | print_kernel_ident(); |
a5dd63ef | 3717 | pr_warn("--------------------------------\n"); |
8e18257d | 3718 | |
681fbec8 | 3719 | pr_warn("inconsistent {%s} -> {%s} usage.\n", |
8e18257d PZ |
3720 | usage_str[prev_bit], usage_str[new_bit]); |
3721 | ||
681fbec8 | 3722 | pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n", |
ba25f9dc | 3723 | curr->comm, task_pid_nr(curr), |
f9ad4a5f | 3724 | lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT, |
ef996916 | 3725 | lockdep_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT, |
f9ad4a5f | 3726 | lockdep_hardirqs_enabled(), |
ef996916 | 3727 | lockdep_softirqs_enabled(curr)); |
8e18257d PZ |
3728 | print_lock(this); |
3729 | ||
681fbec8 | 3730 | pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]); |
12593b74 | 3731 | print_lock_trace(hlock_class(this)->usage_traces[prev_bit], 1); |
8e18257d PZ |
3732 | |
3733 | print_irqtrace_events(curr); | |
681fbec8 | 3734 | pr_warn("\nother info that might help us debug this:\n"); |
282b5c2f SR |
3735 | print_usage_bug_scenario(this); |
3736 | ||
8e18257d PZ |
3737 | lockdep_print_held_locks(curr); |
3738 | ||
681fbec8 | 3739 | pr_warn("\nstack backtrace:\n"); |
8e18257d | 3740 | dump_stack(); |
8e18257d PZ |
3741 | } |
3742 | ||
3743 | /* | |
3744 | * Print out an error if an invalid bit is set: | |
3745 | */ | |
3746 | static inline int | |
3747 | valid_state(struct task_struct *curr, struct held_lock *this, | |
3748 | enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit) | |
3749 | { | |
f7c1c6b3 YD |
3750 | if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) { |
3751 | print_usage_bug(curr, this, bad_bit, new_bit); | |
3752 | return 0; | |
3753 | } | |
8e18257d PZ |
3754 | return 1; |
3755 | } | |
3756 | ||
fbb9ce95 IM |
3757 | |
3758 | /* | |
3759 | * print irq inversion bug: | |
3760 | */ | |
f7c1c6b3 | 3761 | static void |
24208ca7 ML |
3762 | print_irq_inversion_bug(struct task_struct *curr, |
3763 | struct lock_list *root, struct lock_list *other, | |
fbb9ce95 IM |
3764 | struct held_lock *this, int forwards, |
3765 | const char *irqclass) | |
3766 | { | |
dad3d743 SR |
3767 | struct lock_list *entry = other; |
3768 | struct lock_list *middle = NULL; | |
3769 | int depth; | |
3770 | ||
74c383f1 | 3771 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
f7c1c6b3 | 3772 | return; |
fbb9ce95 | 3773 | |
681fbec8 | 3774 | pr_warn("\n"); |
a5dd63ef PM |
3775 | pr_warn("========================================================\n"); |
3776 | pr_warn("WARNING: possible irq lock inversion dependency detected\n"); | |
fbdc4b9a | 3777 | print_kernel_ident(); |
a5dd63ef | 3778 | pr_warn("--------------------------------------------------------\n"); |
681fbec8 | 3779 | pr_warn("%s/%d just changed the state of lock:\n", |
ba25f9dc | 3780 | curr->comm, task_pid_nr(curr)); |
fbb9ce95 IM |
3781 | print_lock(this); |
3782 | if (forwards) | |
681fbec8 | 3783 | pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass); |
fbb9ce95 | 3784 | else |
681fbec8 | 3785 | pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass); |
24208ca7 | 3786 | print_lock_name(other->class); |
681fbec8 | 3787 | pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n"); |
fbb9ce95 | 3788 | |
681fbec8 | 3789 | pr_warn("\nother info that might help us debug this:\n"); |
dad3d743 SR |
3790 | |
3791 | /* Find a middle lock (if one exists) */ | |
3792 | depth = get_lock_depth(other); | |
3793 | do { | |
3794 | if (depth == 0 && (entry != root)) { | |
681fbec8 | 3795 | pr_warn("lockdep:%s bad path found in chain graph\n", __func__); |
dad3d743 SR |
3796 | break; |
3797 | } | |
3798 | middle = entry; | |
3799 | entry = get_lock_parent(entry); | |
3800 | depth--; | |
3801 | } while (entry && entry != root && (depth >= 0)); | |
3802 | if (forwards) | |
3803 | print_irq_lock_scenario(root, other, | |
3804 | middle ? middle->class : root->class, other->class); | |
3805 | else | |
3806 | print_irq_lock_scenario(other, root, | |
3807 | middle ? middle->class : other->class, root->class); | |
3808 | ||
fbb9ce95 IM |
3809 | lockdep_print_held_locks(curr); |
3810 | ||
681fbec8 | 3811 | pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n"); |
12593b74 BVA |
3812 | root->trace = save_trace(); |
3813 | if (!root->trace) | |
f7c1c6b3 | 3814 | return; |
24208ca7 | 3815 | print_shortest_lock_dependencies(other, root); |
fbb9ce95 | 3816 | |
681fbec8 | 3817 | pr_warn("\nstack backtrace:\n"); |
fbb9ce95 | 3818 | dump_stack(); |
fbb9ce95 IM |
3819 | } |
3820 | ||
3821 | /* | |
3822 | * Prove that in the forwards-direction subgraph starting at <this> | |
3823 | * there is no lock matching <mask>: | |
3824 | */ | |
3825 | static int | |
3826 | check_usage_forwards(struct task_struct *curr, struct held_lock *this, | |
f08e3888 | 3827 | enum lock_usage_bit bit) |
fbb9ce95 | 3828 | { |
b11be024 | 3829 | enum bfs_result ret; |
d7aaba14 | 3830 | struct lock_list root; |
3f649ab7 | 3831 | struct lock_list *target_entry; |
f08e3888 BF |
3832 | enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK; |
3833 | unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit); | |
fbb9ce95 | 3834 | |
6971c0f3 | 3835 | bfs_init_root(&root, this); |
f08e3888 | 3836 | ret = find_usage_forwards(&root, usage_mask, &target_entry); |
b11be024 | 3837 | if (bfs_error(ret)) { |
f7c1c6b3 YD |
3838 | print_bfs_bug(ret); |
3839 | return 0; | |
3840 | } | |
b11be024 BF |
3841 | if (ret == BFS_RNOMATCH) |
3842 | return 1; | |
fbb9ce95 | 3843 | |
f08e3888 BF |
3844 | /* Check whether write or read usage is the match */ |
3845 | if (target_entry->class->usage_mask & lock_flag(bit)) { | |
3846 | print_irq_inversion_bug(curr, &root, target_entry, | |
3847 | this, 1, state_name(bit)); | |
3848 | } else { | |
3849 | print_irq_inversion_bug(curr, &root, target_entry, | |
3850 | this, 1, state_name(read_bit)); | |
3851 | } | |
3852 | ||
f7c1c6b3 | 3853 | return 0; |
fbb9ce95 IM |
3854 | } |
3855 | ||
3856 | /* | |
3857 | * Prove that in the backwards-direction subgraph starting at <this> | |
3858 | * there is no lock matching <mask>: | |
3859 | */ | |
3860 | static int | |
3861 | check_usage_backwards(struct task_struct *curr, struct held_lock *this, | |
f08e3888 | 3862 | enum lock_usage_bit bit) |
fbb9ce95 | 3863 | { |
b11be024 | 3864 | enum bfs_result ret; |
d7aaba14 | 3865 | struct lock_list root; |
3f649ab7 | 3866 | struct lock_list *target_entry; |
f08e3888 BF |
3867 | enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK; |
3868 | unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit); | |
fbb9ce95 | 3869 | |
6971c0f3 | 3870 | bfs_init_rootb(&root, this); |
f08e3888 | 3871 | ret = find_usage_backwards(&root, usage_mask, &target_entry); |
b11be024 | 3872 | if (bfs_error(ret)) { |
f7c1c6b3 YD |
3873 | print_bfs_bug(ret); |
3874 | return 0; | |
3875 | } | |
b11be024 BF |
3876 | if (ret == BFS_RNOMATCH) |
3877 | return 1; | |
fbb9ce95 | 3878 | |
f08e3888 BF |
3879 | /* Check whether write or read usage is the match */ |
3880 | if (target_entry->class->usage_mask & lock_flag(bit)) { | |
3881 | print_irq_inversion_bug(curr, &root, target_entry, | |
3882 | this, 0, state_name(bit)); | |
3883 | } else { | |
3884 | print_irq_inversion_bug(curr, &root, target_entry, | |
3885 | this, 0, state_name(read_bit)); | |
3886 | } | |
3887 | ||
f7c1c6b3 | 3888 | return 0; |
fbb9ce95 IM |
3889 | } |
3890 | ||
3117df04 | 3891 | void print_irqtrace_events(struct task_struct *curr) |
fbb9ce95 | 3892 | { |
0584df9c ME |
3893 | const struct irqtrace_events *trace = &curr->irqtrace; |
3894 | ||
3895 | printk("irq event stamp: %u\n", trace->irq_events); | |
04860d48 | 3896 | printk("hardirqs last enabled at (%u): [<%px>] %pS\n", |
0584df9c ME |
3897 | trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip, |
3898 | (void *)trace->hardirq_enable_ip); | |
04860d48 | 3899 | printk("hardirqs last disabled at (%u): [<%px>] %pS\n", |
0584df9c ME |
3900 | trace->hardirq_disable_event, (void *)trace->hardirq_disable_ip, |
3901 | (void *)trace->hardirq_disable_ip); | |
04860d48 | 3902 | printk("softirqs last enabled at (%u): [<%px>] %pS\n", |
0584df9c ME |
3903 | trace->softirq_enable_event, (void *)trace->softirq_enable_ip, |
3904 | (void *)trace->softirq_enable_ip); | |
04860d48 | 3905 | printk("softirqs last disabled at (%u): [<%px>] %pS\n", |
0584df9c ME |
3906 | trace->softirq_disable_event, (void *)trace->softirq_disable_ip, |
3907 | (void *)trace->softirq_disable_ip); | |
fbb9ce95 IM |
3908 | } |
3909 | ||
cd95302d | 3910 | static int HARDIRQ_verbose(struct lock_class *class) |
fbb9ce95 | 3911 | { |
8e18257d PZ |
3912 | #if HARDIRQ_VERBOSE |
3913 | return class_filter(class); | |
3914 | #endif | |
fbb9ce95 IM |
3915 | return 0; |
3916 | } | |
3917 | ||
cd95302d | 3918 | static int SOFTIRQ_verbose(struct lock_class *class) |
fbb9ce95 | 3919 | { |
8e18257d PZ |
3920 | #if SOFTIRQ_VERBOSE |
3921 | return class_filter(class); | |
3922 | #endif | |
3923 | return 0; | |
fbb9ce95 IM |
3924 | } |
3925 | ||
cd95302d PZ |
3926 | static int (*state_verbose_f[])(struct lock_class *class) = { |
3927 | #define LOCKDEP_STATE(__STATE) \ | |
3928 | __STATE##_verbose, | |
3929 | #include "lockdep_states.h" | |
3930 | #undef LOCKDEP_STATE | |
3931 | }; | |
3932 | ||
3933 | static inline int state_verbose(enum lock_usage_bit bit, | |
3934 | struct lock_class *class) | |
3935 | { | |
c902a1e8 | 3936 | return state_verbose_f[bit >> LOCK_USAGE_DIR_MASK](class); |
cd95302d PZ |
3937 | } |
3938 | ||
42c50d54 PZ |
3939 | typedef int (*check_usage_f)(struct task_struct *, struct held_lock *, |
3940 | enum lock_usage_bit bit, const char *name); | |
3941 | ||
6a6904d3 | 3942 | static int |
1c21f14e PZ |
3943 | mark_lock_irq(struct task_struct *curr, struct held_lock *this, |
3944 | enum lock_usage_bit new_bit) | |
6a6904d3 | 3945 | { |
f989209e | 3946 | int excl_bit = exclusive_bit(new_bit); |
bba2a8f1 FW |
3947 | int read = new_bit & LOCK_USAGE_READ_MASK; |
3948 | int dir = new_bit & LOCK_USAGE_DIR_MASK; | |
42c50d54 | 3949 | |
38aa2714 PZ |
3950 | /* |
3951 | * Validate that this particular lock does not have conflicting | |
3952 | * usage states. | |
3953 | */ | |
6a6904d3 PZ |
3954 | if (!valid_state(curr, this, new_bit, excl_bit)) |
3955 | return 0; | |
42c50d54 | 3956 | |
38aa2714 | 3957 | /* |
f08e3888 | 3958 | * Check for read in write conflicts |
38aa2714 | 3959 | */ |
f08e3888 BF |
3960 | if (!read && !valid_state(curr, this, new_bit, |
3961 | excl_bit + LOCK_USAGE_READ_MASK)) | |
6a6904d3 | 3962 | return 0; |
780e820b | 3963 | |
f08e3888 | 3964 | |
38aa2714 | 3965 | /* |
f08e3888 BF |
3966 | * Validate that the lock dependencies don't have conflicting usage |
3967 | * states. | |
38aa2714 | 3968 | */ |
f08e3888 BF |
3969 | if (dir) { |
3970 | /* | |
3971 | * mark ENABLED has to look backwards -- to ensure no dependee | |
3972 | * has USED_IN state, which, again, would allow recursion deadlocks. | |
3973 | */ | |
3974 | if (!check_usage_backwards(curr, this, excl_bit)) | |
38aa2714 | 3975 | return 0; |
f08e3888 BF |
3976 | } else { |
3977 | /* | |
3978 | * mark USED_IN has to look forwards -- to ensure no dependency | |
3979 | * has ENABLED state, which would allow recursion deadlocks. | |
3980 | */ | |
3981 | if (!check_usage_forwards(curr, this, excl_bit)) | |
38aa2714 PZ |
3982 | return 0; |
3983 | } | |
780e820b | 3984 | |
cd95302d | 3985 | if (state_verbose(new_bit, hlock_class(this))) |
6a6904d3 PZ |
3986 | return 2; |
3987 | ||
3988 | return 1; | |
3989 | } | |
3990 | ||
fbb9ce95 IM |
3991 | /* |
3992 | * Mark all held locks with a usage bit: | |
3993 | */ | |
1d09daa5 | 3994 | static int |
436a49ae | 3995 | mark_held_locks(struct task_struct *curr, enum lock_usage_bit base_bit) |
fbb9ce95 | 3996 | { |
fbb9ce95 IM |
3997 | struct held_lock *hlock; |
3998 | int i; | |
3999 | ||
4000 | for (i = 0; i < curr->lockdep_depth; i++) { | |
436a49ae | 4001 | enum lock_usage_bit hlock_bit = base_bit; |
fbb9ce95 IM |
4002 | hlock = curr->held_locks + i; |
4003 | ||
cf2ad4d1 | 4004 | if (hlock->read) |
bba2a8f1 | 4005 | hlock_bit += LOCK_USAGE_READ_MASK; |
cf2ad4d1 | 4006 | |
436a49ae | 4007 | BUG_ON(hlock_bit >= LOCK_USAGE_STATES); |
cf40bd16 | 4008 | |
34d0ed5e | 4009 | if (!hlock->check) |
efbe2eee PZ |
4010 | continue; |
4011 | ||
436a49ae | 4012 | if (!mark_lock(curr, hlock, hlock_bit)) |
fbb9ce95 IM |
4013 | return 0; |
4014 | } | |
4015 | ||
4016 | return 1; | |
4017 | } | |
4018 | ||
fbb9ce95 IM |
4019 | /* |
4020 | * Hardirqs will be enabled: | |
4021 | */ | |
c86e9b98 | 4022 | static void __trace_hardirqs_on_caller(void) |
fbb9ce95 IM |
4023 | { |
4024 | struct task_struct *curr = current; | |
fbb9ce95 | 4025 | |
fbb9ce95 IM |
4026 | /* |
4027 | * We are going to turn hardirqs on, so set the | |
4028 | * usage bit for all held locks: | |
4029 | */ | |
436a49ae | 4030 | if (!mark_held_locks(curr, LOCK_ENABLED_HARDIRQ)) |
fbb9ce95 IM |
4031 | return; |
4032 | /* | |
4033 | * If we have softirqs enabled, then set the usage | |
4034 | * bit for all held locks. (disabled hardirqs prevented | |
4035 | * this bit from being set before) | |
4036 | */ | |
4037 | if (curr->softirqs_enabled) | |
c86e9b98 | 4038 | mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ); |
8e18257d | 4039 | } |
dd4e5d3a | 4040 | |
c86e9b98 PZ |
4041 | /** |
4042 | * lockdep_hardirqs_on_prepare - Prepare for enabling interrupts | |
4043 | * @ip: Caller address | |
4044 | * | |
4045 | * Invoked before a possible transition to RCU idle from exit to user or | |
4046 | * guest mode. This ensures that all RCU operations are done before RCU | |
4047 | * stops watching. After the RCU transition lockdep_hardirqs_on() has to be | |
4048 | * invoked to set the final state. | |
4049 | */ | |
4050 | void lockdep_hardirqs_on_prepare(unsigned long ip) | |
dd4e5d3a | 4051 | { |
859d069e PZ |
4052 | if (unlikely(!debug_locks)) |
4053 | return; | |
4054 | ||
4055 | /* | |
4056 | * NMIs do not (and cannot) track lock dependencies, nothing to do. | |
4057 | */ | |
4058 | if (unlikely(in_nmi())) | |
4059 | return; | |
4060 | ||
f8e48a3d | 4061 | if (unlikely(this_cpu_read(lockdep_recursion))) |
dd4e5d3a PZ |
4062 | return; |
4063 | ||
f9ad4a5f | 4064 | if (unlikely(lockdep_hardirqs_enabled())) { |
7d36b26b PZ |
4065 | /* |
4066 | * Neither irq nor preemption are disabled here | |
4067 | * so this is racy by nature but losing one hit | |
4068 | * in a stat is not a big deal. | |
4069 | */ | |
4070 | __debug_atomic_inc(redundant_hardirqs_on); | |
4071 | return; | |
4072 | } | |
4073 | ||
0119fee4 PZ |
4074 | /* |
4075 | * We're enabling irqs and according to our state above irqs weren't | |
4076 | * already enabled, yet we find the hardware thinks they are in fact | |
4077 | * enabled.. someone messed up their IRQ state tracing. | |
4078 | */ | |
dd4e5d3a PZ |
4079 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
4080 | return; | |
4081 | ||
0119fee4 PZ |
4082 | /* |
4083 | * See the fine text that goes along with this variable definition. | |
4084 | */ | |
d671002b | 4085 | if (DEBUG_LOCKS_WARN_ON(early_boot_irqs_disabled)) |
7d36b26b PZ |
4086 | return; |
4087 | ||
0119fee4 PZ |
4088 | /* |
4089 | * Can't allow enabling interrupts while in an interrupt handler, | |
4090 | * that's general bad form and such. Recursion, limited stack etc.. | |
4091 | */ | |
f9ad4a5f | 4092 | if (DEBUG_LOCKS_WARN_ON(lockdep_hardirq_context())) |
7d36b26b PZ |
4093 | return; |
4094 | ||
c86e9b98 PZ |
4095 | current->hardirq_chain_key = current->curr_chain_key; |
4096 | ||
4d004099 | 4097 | lockdep_recursion_inc(); |
c86e9b98 | 4098 | __trace_hardirqs_on_caller(); |
10476e63 | 4099 | lockdep_recursion_finish(); |
dd4e5d3a | 4100 | } |
c86e9b98 PZ |
4101 | EXPORT_SYMBOL_GPL(lockdep_hardirqs_on_prepare); |
4102 | ||
4103 | void noinstr lockdep_hardirqs_on(unsigned long ip) | |
4104 | { | |
0584df9c | 4105 | struct irqtrace_events *trace = ¤t->irqtrace; |
c86e9b98 | 4106 | |
859d069e PZ |
4107 | if (unlikely(!debug_locks)) |
4108 | return; | |
4109 | ||
4110 | /* | |
4111 | * NMIs can happen in the middle of local_irq_{en,dis}able() where the | |
4112 | * tracking state and hardware state are out of sync. | |
4113 | * | |
4114 | * NMIs must save lockdep_hardirqs_enabled() to restore IRQ state from, | |
4115 | * and not rely on hardware state like normal interrupts. | |
4116 | */ | |
4117 | if (unlikely(in_nmi())) { | |
ed004953 | 4118 | if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI)) |
4119 | return; | |
4120 | ||
859d069e PZ |
4121 | /* |
4122 | * Skip: | |
4123 | * - recursion check, because NMI can hit lockdep; | |
4124 | * - hardware state check, because above; | |
4125 | * - chain_key check, see lockdep_hardirqs_on_prepare(). | |
4126 | */ | |
4127 | goto skip_checks; | |
4128 | } | |
c86e9b98 | 4129 | |
f8e48a3d | 4130 | if (unlikely(this_cpu_read(lockdep_recursion))) |
c86e9b98 PZ |
4131 | return; |
4132 | ||
f9ad4a5f | 4133 | if (lockdep_hardirqs_enabled()) { |
c86e9b98 PZ |
4134 | /* |
4135 | * Neither irq nor preemption are disabled here | |
4136 | * so this is racy by nature but losing one hit | |
4137 | * in a stat is not a big deal. | |
4138 | */ | |
4139 | __debug_atomic_inc(redundant_hardirqs_on); | |
4140 | return; | |
4141 | } | |
4142 | ||
4143 | /* | |
4144 | * We're enabling irqs and according to our state above irqs weren't | |
4145 | * already enabled, yet we find the hardware thinks they are in fact | |
4146 | * enabled.. someone messed up their IRQ state tracing. | |
4147 | */ | |
4148 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) | |
4149 | return; | |
4150 | ||
4151 | /* | |
4152 | * Ensure the lock stack remained unchanged between | |
4153 | * lockdep_hardirqs_on_prepare() and lockdep_hardirqs_on(). | |
4154 | */ | |
4155 | DEBUG_LOCKS_WARN_ON(current->hardirq_chain_key != | |
4156 | current->curr_chain_key); | |
4157 | ||
859d069e | 4158 | skip_checks: |
c86e9b98 | 4159 | /* we'll do an OFF -> ON transition: */ |
fddf9055 | 4160 | __this_cpu_write(hardirqs_enabled, 1); |
0584df9c ME |
4161 | trace->hardirq_enable_ip = ip; |
4162 | trace->hardirq_enable_event = ++trace->irq_events; | |
c86e9b98 PZ |
4163 | debug_atomic_inc(hardirqs_on_events); |
4164 | } | |
4165 | EXPORT_SYMBOL_GPL(lockdep_hardirqs_on); | |
8e18257d PZ |
4166 | |
4167 | /* | |
4168 | * Hardirqs were disabled: | |
4169 | */ | |
c86e9b98 | 4170 | void noinstr lockdep_hardirqs_off(unsigned long ip) |
8e18257d | 4171 | { |
859d069e PZ |
4172 | if (unlikely(!debug_locks)) |
4173 | return; | |
8e18257d | 4174 | |
859d069e PZ |
4175 | /* |
4176 | * Matching lockdep_hardirqs_on(), allow NMIs in the middle of lockdep; | |
4177 | * they will restore the software state. This ensures the software | |
4178 | * state is consistent inside NMIs as well. | |
4179 | */ | |
ed004953 | 4180 | if (in_nmi()) { |
4181 | if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI)) | |
4182 | return; | |
4d004099 | 4183 | } else if (__this_cpu_read(lockdep_recursion)) |
8e18257d PZ |
4184 | return; |
4185 | ||
0119fee4 PZ |
4186 | /* |
4187 | * So we're supposed to get called after you mask local IRQs, but for | |
4188 | * some reason the hardware doesn't quite think you did a proper job. | |
4189 | */ | |
8e18257d PZ |
4190 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
4191 | return; | |
4192 | ||
f9ad4a5f | 4193 | if (lockdep_hardirqs_enabled()) { |
0584df9c ME |
4194 | struct irqtrace_events *trace = ¤t->irqtrace; |
4195 | ||
8e18257d PZ |
4196 | /* |
4197 | * We have done an ON -> OFF transition: | |
4198 | */ | |
fddf9055 | 4199 | __this_cpu_write(hardirqs_enabled, 0); |
0584df9c ME |
4200 | trace->hardirq_disable_ip = ip; |
4201 | trace->hardirq_disable_event = ++trace->irq_events; | |
bd6d29c2 | 4202 | debug_atomic_inc(hardirqs_off_events); |
c86e9b98 | 4203 | } else { |
bd6d29c2 | 4204 | debug_atomic_inc(redundant_hardirqs_off); |
c86e9b98 | 4205 | } |
8e18257d | 4206 | } |
c86e9b98 | 4207 | EXPORT_SYMBOL_GPL(lockdep_hardirqs_off); |
8e18257d PZ |
4208 | |
4209 | /* | |
4210 | * Softirqs will be enabled: | |
4211 | */ | |
0d38453c | 4212 | void lockdep_softirqs_on(unsigned long ip) |
8e18257d | 4213 | { |
0584df9c | 4214 | struct irqtrace_events *trace = ¤t->irqtrace; |
8e18257d | 4215 | |
4d004099 | 4216 | if (unlikely(!lockdep_enabled())) |
8e18257d PZ |
4217 | return; |
4218 | ||
0119fee4 PZ |
4219 | /* |
4220 | * We fancy IRQs being disabled here, see softirq.c, avoids | |
4221 | * funny state and nesting things. | |
4222 | */ | |
8e18257d PZ |
4223 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
4224 | return; | |
4225 | ||
0584df9c | 4226 | if (current->softirqs_enabled) { |
bd6d29c2 | 4227 | debug_atomic_inc(redundant_softirqs_on); |
8e18257d PZ |
4228 | return; |
4229 | } | |
4230 | ||
4d004099 | 4231 | lockdep_recursion_inc(); |
8e18257d PZ |
4232 | /* |
4233 | * We'll do an OFF -> ON transition: | |
4234 | */ | |
0584df9c ME |
4235 | current->softirqs_enabled = 1; |
4236 | trace->softirq_enable_ip = ip; | |
4237 | trace->softirq_enable_event = ++trace->irq_events; | |
bd6d29c2 | 4238 | debug_atomic_inc(softirqs_on_events); |
8e18257d PZ |
4239 | /* |
4240 | * We are going to turn softirqs on, so set the | |
4241 | * usage bit for all held locks, if hardirqs are | |
4242 | * enabled too: | |
4243 | */ | |
f9ad4a5f | 4244 | if (lockdep_hardirqs_enabled()) |
0584df9c | 4245 | mark_held_locks(current, LOCK_ENABLED_SOFTIRQ); |
10476e63 | 4246 | lockdep_recursion_finish(); |
8e18257d PZ |
4247 | } |
4248 | ||
4249 | /* | |
4250 | * Softirqs were disabled: | |
4251 | */ | |
0d38453c | 4252 | void lockdep_softirqs_off(unsigned long ip) |
8e18257d | 4253 | { |
4d004099 | 4254 | if (unlikely(!lockdep_enabled())) |
8e18257d PZ |
4255 | return; |
4256 | ||
0119fee4 PZ |
4257 | /* |
4258 | * We fancy IRQs being disabled here, see softirq.c | |
4259 | */ | |
8e18257d PZ |
4260 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
4261 | return; | |
4262 | ||
0584df9c ME |
4263 | if (current->softirqs_enabled) { |
4264 | struct irqtrace_events *trace = ¤t->irqtrace; | |
4265 | ||
8e18257d PZ |
4266 | /* |
4267 | * We have done an ON -> OFF transition: | |
4268 | */ | |
0584df9c ME |
4269 | current->softirqs_enabled = 0; |
4270 | trace->softirq_disable_ip = ip; | |
4271 | trace->softirq_disable_event = ++trace->irq_events; | |
bd6d29c2 | 4272 | debug_atomic_inc(softirqs_off_events); |
0119fee4 PZ |
4273 | /* |
4274 | * Whoops, we wanted softirqs off, so why aren't they? | |
4275 | */ | |
8e18257d PZ |
4276 | DEBUG_LOCKS_WARN_ON(!softirq_count()); |
4277 | } else | |
bd6d29c2 | 4278 | debug_atomic_inc(redundant_softirqs_off); |
8e18257d PZ |
4279 | } |
4280 | ||
09180651 YD |
4281 | static int |
4282 | mark_usage(struct task_struct *curr, struct held_lock *hlock, int check) | |
8e18257d | 4283 | { |
09180651 YD |
4284 | if (!check) |
4285 | goto lock_used; | |
4286 | ||
8e18257d PZ |
4287 | /* |
4288 | * If non-trylock use in a hardirq or softirq context, then | |
4289 | * mark the lock as used in these contexts: | |
4290 | */ | |
4291 | if (!hlock->trylock) { | |
4292 | if (hlock->read) { | |
f9ad4a5f | 4293 | if (lockdep_hardirq_context()) |
8e18257d PZ |
4294 | if (!mark_lock(curr, hlock, |
4295 | LOCK_USED_IN_HARDIRQ_READ)) | |
4296 | return 0; | |
4297 | if (curr->softirq_context) | |
4298 | if (!mark_lock(curr, hlock, | |
4299 | LOCK_USED_IN_SOFTIRQ_READ)) | |
4300 | return 0; | |
4301 | } else { | |
f9ad4a5f | 4302 | if (lockdep_hardirq_context()) |
8e18257d PZ |
4303 | if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ)) |
4304 | return 0; | |
4305 | if (curr->softirq_context) | |
4306 | if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ)) | |
4307 | return 0; | |
4308 | } | |
4309 | } | |
4310 | if (!hlock->hardirqs_off) { | |
4311 | if (hlock->read) { | |
4312 | if (!mark_lock(curr, hlock, | |
4fc95e86 | 4313 | LOCK_ENABLED_HARDIRQ_READ)) |
8e18257d PZ |
4314 | return 0; |
4315 | if (curr->softirqs_enabled) | |
4316 | if (!mark_lock(curr, hlock, | |
4fc95e86 | 4317 | LOCK_ENABLED_SOFTIRQ_READ)) |
8e18257d PZ |
4318 | return 0; |
4319 | } else { | |
4320 | if (!mark_lock(curr, hlock, | |
4fc95e86 | 4321 | LOCK_ENABLED_HARDIRQ)) |
8e18257d PZ |
4322 | return 0; |
4323 | if (curr->softirqs_enabled) | |
4324 | if (!mark_lock(curr, hlock, | |
4fc95e86 | 4325 | LOCK_ENABLED_SOFTIRQ)) |
8e18257d PZ |
4326 | return 0; |
4327 | } | |
4328 | } | |
4329 | ||
09180651 YD |
4330 | lock_used: |
4331 | /* mark it as used: */ | |
4332 | if (!mark_lock(curr, hlock, LOCK_USED)) | |
4333 | return 0; | |
4334 | ||
8e18257d PZ |
4335 | return 1; |
4336 | } | |
4337 | ||
c2469756 BF |
4338 | static inline unsigned int task_irq_context(struct task_struct *task) |
4339 | { | |
f9ad4a5f | 4340 | return LOCK_CHAIN_HARDIRQ_CONTEXT * !!lockdep_hardirq_context() + |
b3b9c187 | 4341 | LOCK_CHAIN_SOFTIRQ_CONTEXT * !!task->softirq_context; |
c2469756 BF |
4342 | } |
4343 | ||
8e18257d PZ |
4344 | static int separate_irq_context(struct task_struct *curr, |
4345 | struct held_lock *hlock) | |
4346 | { | |
4347 | unsigned int depth = curr->lockdep_depth; | |
4348 | ||
4349 | /* | |
4350 | * Keep track of points where we cross into an interrupt context: | |
4351 | */ | |
8e18257d PZ |
4352 | if (depth) { |
4353 | struct held_lock *prev_hlock; | |
4354 | ||
4355 | prev_hlock = curr->held_locks + depth-1; | |
4356 | /* | |
4357 | * If we cross into another context, reset the | |
4358 | * hash key (this also prevents the checking and the | |
4359 | * adding of the dependency to 'prev'): | |
4360 | */ | |
4361 | if (prev_hlock->irq_context != hlock->irq_context) | |
4362 | return 1; | |
4363 | } | |
4364 | return 0; | |
fbb9ce95 IM |
4365 | } |
4366 | ||
fbb9ce95 | 4367 | /* |
8e18257d | 4368 | * Mark a lock with a usage bit, and validate the state transition: |
fbb9ce95 | 4369 | */ |
1d09daa5 | 4370 | static int mark_lock(struct task_struct *curr, struct held_lock *this, |
0764d23c | 4371 | enum lock_usage_bit new_bit) |
fbb9ce95 | 4372 | { |
2bb8945b | 4373 | unsigned int new_mask, ret = 1; |
fbb9ce95 | 4374 | |
4d56330d YD |
4375 | if (new_bit >= LOCK_USAGE_STATES) { |
4376 | DEBUG_LOCKS_WARN_ON(1); | |
4377 | return 0; | |
4378 | } | |
4379 | ||
23870f12 | 4380 | if (new_bit == LOCK_USED && this->read) |
4381 | new_bit = LOCK_USED_READ; | |
4382 | ||
4383 | new_mask = 1 << new_bit; | |
4384 | ||
fbb9ce95 | 4385 | /* |
8e18257d PZ |
4386 | * If already set then do not dirty the cacheline, |
4387 | * nor do any checks: | |
fbb9ce95 | 4388 | */ |
f82b217e | 4389 | if (likely(hlock_class(this)->usage_mask & new_mask)) |
8e18257d PZ |
4390 | return 1; |
4391 | ||
4392 | if (!graph_lock()) | |
4393 | return 0; | |
fbb9ce95 | 4394 | /* |
25985edc | 4395 | * Make sure we didn't race: |
fbb9ce95 | 4396 | */ |
23870f12 | 4397 | if (unlikely(hlock_class(this)->usage_mask & new_mask)) |
4398 | goto unlock; | |
fbb9ce95 | 4399 | |
1a393408 PZ |
4400 | if (!hlock_class(this)->usage_mask) |
4401 | debug_atomic_dec(nr_unused_locks); | |
4402 | ||
f82b217e | 4403 | hlock_class(this)->usage_mask |= new_mask; |
fbb9ce95 | 4404 | |
2bb8945b PZ |
4405 | if (new_bit < LOCK_TRACE_STATES) { |
4406 | if (!(hlock_class(this)->usage_traces[new_bit] = save_trace())) | |
4407 | return 0; | |
23870f12 | 4408 | } |
fbb9ce95 | 4409 | |
1a393408 | 4410 | if (new_bit < LOCK_USED) { |
2bb8945b PZ |
4411 | ret = mark_lock_irq(curr, this, new_bit); |
4412 | if (!ret) | |
4413 | return 0; | |
8e18257d | 4414 | } |
fbb9ce95 | 4415 | |
23870f12 | 4416 | unlock: |
8e18257d PZ |
4417 | graph_unlock(); |
4418 | ||
4419 | /* | |
4420 | * We must printk outside of the graph_lock: | |
4421 | */ | |
4422 | if (ret == 2) { | |
4423 | printk("\nmarked lock as {%s}:\n", usage_str[new_bit]); | |
4424 | print_lock(this); | |
4425 | print_irqtrace_events(curr); | |
4426 | dump_stack(); | |
4427 | } | |
4428 | ||
4429 | return ret; | |
4430 | } | |
fbb9ce95 | 4431 | |
9a019db0 PZ |
4432 | static inline short task_wait_context(struct task_struct *curr) |
4433 | { | |
4434 | /* | |
4435 | * Set appropriate wait type for the context; for IRQs we have to take | |
4436 | * into account force_irqthread as that is implied by PREEMPT_RT. | |
4437 | */ | |
f9ad4a5f | 4438 | if (lockdep_hardirq_context()) { |
9a019db0 PZ |
4439 | /* |
4440 | * Check if force_irqthreads will run us threaded. | |
4441 | */ | |
4442 | if (curr->hardirq_threaded || curr->irq_config) | |
4443 | return LD_WAIT_CONFIG; | |
4444 | ||
4445 | return LD_WAIT_SPIN; | |
4446 | } else if (curr->softirq_context) { | |
4447 | /* | |
4448 | * Softirqs are always threaded. | |
4449 | */ | |
4450 | return LD_WAIT_CONFIG; | |
4451 | } | |
4452 | ||
4453 | return LD_WAIT_MAX; | |
4454 | } | |
4455 | ||
de8f5e4f PZ |
4456 | static int |
4457 | print_lock_invalid_wait_context(struct task_struct *curr, | |
4458 | struct held_lock *hlock) | |
4459 | { | |
9a019db0 PZ |
4460 | short curr_inner; |
4461 | ||
de8f5e4f PZ |
4462 | if (!debug_locks_off()) |
4463 | return 0; | |
4464 | if (debug_locks_silent) | |
4465 | return 0; | |
4466 | ||
4467 | pr_warn("\n"); | |
4468 | pr_warn("=============================\n"); | |
4469 | pr_warn("[ BUG: Invalid wait context ]\n"); | |
4470 | print_kernel_ident(); | |
4471 | pr_warn("-----------------------------\n"); | |
4472 | ||
4473 | pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr)); | |
4474 | print_lock(hlock); | |
4475 | ||
4476 | pr_warn("other info that might help us debug this:\n"); | |
9a019db0 PZ |
4477 | |
4478 | curr_inner = task_wait_context(curr); | |
4479 | pr_warn("context-{%d:%d}\n", curr_inner, curr_inner); | |
4480 | ||
de8f5e4f PZ |
4481 | lockdep_print_held_locks(curr); |
4482 | ||
4483 | pr_warn("stack backtrace:\n"); | |
4484 | dump_stack(); | |
4485 | ||
4486 | return 0; | |
4487 | } | |
4488 | ||
4489 | /* | |
4490 | * Verify the wait_type context. | |
4491 | * | |
4492 | * This check validates we takes locks in the right wait-type order; that is it | |
4493 | * ensures that we do not take mutexes inside spinlocks and do not attempt to | |
4494 | * acquire spinlocks inside raw_spinlocks and the sort. | |
4495 | * | |
4496 | * The entire thing is slightly more complex because of RCU, RCU is a lock that | |
4497 | * can be taken from (pretty much) any context but also has constraints. | |
4498 | * However when taken in a stricter environment the RCU lock does not loosen | |
4499 | * the constraints. | |
4500 | * | |
4501 | * Therefore we must look for the strictest environment in the lock stack and | |
4502 | * compare that to the lock we're trying to acquire. | |
4503 | */ | |
4504 | static int check_wait_context(struct task_struct *curr, struct held_lock *next) | |
4505 | { | |
4506 | short next_inner = hlock_class(next)->wait_type_inner; | |
4507 | short next_outer = hlock_class(next)->wait_type_outer; | |
4508 | short curr_inner; | |
4509 | int depth; | |
4510 | ||
4511 | if (!curr->lockdep_depth || !next_inner || next->trylock) | |
4512 | return 0; | |
4513 | ||
4514 | if (!next_outer) | |
4515 | next_outer = next_inner; | |
4516 | ||
4517 | /* | |
4518 | * Find start of current irq_context.. | |
4519 | */ | |
4520 | for (depth = curr->lockdep_depth - 1; depth >= 0; depth--) { | |
4521 | struct held_lock *prev = curr->held_locks + depth; | |
4522 | if (prev->irq_context != next->irq_context) | |
4523 | break; | |
4524 | } | |
4525 | depth++; | |
4526 | ||
9a019db0 | 4527 | curr_inner = task_wait_context(curr); |
de8f5e4f PZ |
4528 | |
4529 | for (; depth < curr->lockdep_depth; depth++) { | |
4530 | struct held_lock *prev = curr->held_locks + depth; | |
4531 | short prev_inner = hlock_class(prev)->wait_type_inner; | |
4532 | ||
4533 | if (prev_inner) { | |
4534 | /* | |
4535 | * We can have a bigger inner than a previous one | |
4536 | * when outer is smaller than inner, as with RCU. | |
4537 | * | |
4538 | * Also due to trylocks. | |
4539 | */ | |
4540 | curr_inner = min(curr_inner, prev_inner); | |
4541 | } | |
4542 | } | |
4543 | ||
4544 | if (next_outer > curr_inner) | |
4545 | return print_lock_invalid_wait_context(curr, next); | |
4546 | ||
4547 | return 0; | |
4548 | } | |
4549 | ||
30a35f79 | 4550 | #else /* CONFIG_PROVE_LOCKING */ |
886532ae AB |
4551 | |
4552 | static inline int | |
4553 | mark_usage(struct task_struct *curr, struct held_lock *hlock, int check) | |
4554 | { | |
4555 | return 1; | |
4556 | } | |
4557 | ||
4558 | static inline unsigned int task_irq_context(struct task_struct *task) | |
4559 | { | |
4560 | return 0; | |
4561 | } | |
4562 | ||
4563 | static inline int separate_irq_context(struct task_struct *curr, | |
4564 | struct held_lock *hlock) | |
4565 | { | |
4566 | return 0; | |
4567 | } | |
4568 | ||
de8f5e4f PZ |
4569 | static inline int check_wait_context(struct task_struct *curr, |
4570 | struct held_lock *next) | |
4571 | { | |
4572 | return 0; | |
4573 | } | |
4574 | ||
30a35f79 | 4575 | #endif /* CONFIG_PROVE_LOCKING */ |
886532ae | 4576 | |
fbb9ce95 IM |
4577 | /* |
4578 | * Initialize a lock instance's lock-class mapping info: | |
4579 | */ | |
de8f5e4f PZ |
4580 | void lockdep_init_map_waits(struct lockdep_map *lock, const char *name, |
4581 | struct lock_class_key *key, int subclass, | |
4582 | short inner, short outer) | |
fbb9ce95 | 4583 | { |
d3d03d4f YZ |
4584 | int i; |
4585 | ||
d3d03d4f YZ |
4586 | for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++) |
4587 | lock->class_cache[i] = NULL; | |
62016250 | 4588 | |
c8a25005 PZ |
4589 | #ifdef CONFIG_LOCK_STAT |
4590 | lock->cpu = raw_smp_processor_id(); | |
4591 | #endif | |
4592 | ||
0119fee4 PZ |
4593 | /* |
4594 | * Can't be having no nameless bastards around this place! | |
4595 | */ | |
c8a25005 PZ |
4596 | if (DEBUG_LOCKS_WARN_ON(!name)) { |
4597 | lock->name = "NULL"; | |
fbb9ce95 | 4598 | return; |
c8a25005 PZ |
4599 | } |
4600 | ||
4601 | lock->name = name; | |
fbb9ce95 | 4602 | |
de8f5e4f PZ |
4603 | lock->wait_type_outer = outer; |
4604 | lock->wait_type_inner = inner; | |
4605 | ||
0119fee4 PZ |
4606 | /* |
4607 | * No key, no joy, we need to hash something. | |
4608 | */ | |
fbb9ce95 IM |
4609 | if (DEBUG_LOCKS_WARN_ON(!key)) |
4610 | return; | |
fbb9ce95 | 4611 | /* |
108c1485 BVA |
4612 | * Sanity check, the lock-class key must either have been allocated |
4613 | * statically or must have been registered as a dynamic key. | |
fbb9ce95 | 4614 | */ |
108c1485 BVA |
4615 | if (!static_obj(key) && !is_dynamic_key(key)) { |
4616 | if (debug_locks) | |
4617 | printk(KERN_ERR "BUG: key %px has not been registered!\n", key); | |
fbb9ce95 IM |
4618 | DEBUG_LOCKS_WARN_ON(1); |
4619 | return; | |
4620 | } | |
fbb9ce95 | 4621 | lock->key = key; |
c8a25005 PZ |
4622 | |
4623 | if (unlikely(!debug_locks)) | |
4624 | return; | |
4625 | ||
35a9393c PZ |
4626 | if (subclass) { |
4627 | unsigned long flags; | |
4628 | ||
4d004099 | 4629 | if (DEBUG_LOCKS_WARN_ON(!lockdep_enabled())) |
35a9393c PZ |
4630 | return; |
4631 | ||
4632 | raw_local_irq_save(flags); | |
4d004099 | 4633 | lockdep_recursion_inc(); |
4dfbb9d8 | 4634 | register_lock_class(lock, subclass, 1); |
10476e63 | 4635 | lockdep_recursion_finish(); |
35a9393c PZ |
4636 | raw_local_irq_restore(flags); |
4637 | } | |
fbb9ce95 | 4638 | } |
de8f5e4f | 4639 | EXPORT_SYMBOL_GPL(lockdep_init_map_waits); |
fbb9ce95 | 4640 | |
1704f47b | 4641 | struct lock_class_key __lockdep_no_validate__; |
ea6749c7 | 4642 | EXPORT_SYMBOL_GPL(__lockdep_no_validate__); |
1704f47b | 4643 | |
f7c1c6b3 | 4644 | static void |
d0945950 ML |
4645 | print_lock_nested_lock_not_held(struct task_struct *curr, |
4646 | struct held_lock *hlock, | |
4647 | unsigned long ip) | |
4648 | { | |
4649 | if (!debug_locks_off()) | |
f7c1c6b3 | 4650 | return; |
d0945950 | 4651 | if (debug_locks_silent) |
f7c1c6b3 | 4652 | return; |
d0945950 | 4653 | |
681fbec8 | 4654 | pr_warn("\n"); |
a5dd63ef PM |
4655 | pr_warn("==================================\n"); |
4656 | pr_warn("WARNING: Nested lock was not taken\n"); | |
d0945950 | 4657 | print_kernel_ident(); |
a5dd63ef | 4658 | pr_warn("----------------------------------\n"); |
d0945950 | 4659 | |
681fbec8 | 4660 | pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr)); |
d0945950 ML |
4661 | print_lock(hlock); |
4662 | ||
681fbec8 PM |
4663 | pr_warn("\nbut this task is not holding:\n"); |
4664 | pr_warn("%s\n", hlock->nest_lock->name); | |
d0945950 | 4665 | |
681fbec8 | 4666 | pr_warn("\nstack backtrace:\n"); |
d0945950 ML |
4667 | dump_stack(); |
4668 | ||
681fbec8 | 4669 | pr_warn("\nother info that might help us debug this:\n"); |
d0945950 ML |
4670 | lockdep_print_held_locks(curr); |
4671 | ||
681fbec8 | 4672 | pr_warn("\nstack backtrace:\n"); |
d0945950 | 4673 | dump_stack(); |
d0945950 ML |
4674 | } |
4675 | ||
08f36ff6 | 4676 | static int __lock_is_held(const struct lockdep_map *lock, int read); |
d0945950 | 4677 | |
fbb9ce95 IM |
4678 | /* |
4679 | * This gets called for every mutex_lock*()/spin_lock*() operation. | |
4680 | * We maintain the dependency maps and validate the locking attempt: | |
8ee10862 WL |
4681 | * |
4682 | * The callers must make sure that IRQs are disabled before calling it, | |
4683 | * otherwise we could get an interrupt which would want to take locks, | |
4684 | * which would end up in lockdep again. | |
fbb9ce95 IM |
4685 | */ |
4686 | static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, | |
4687 | int trylock, int read, int check, int hardirqs_off, | |
bb97a91e | 4688 | struct lockdep_map *nest_lock, unsigned long ip, |
21199f27 | 4689 | int references, int pin_count) |
fbb9ce95 IM |
4690 | { |
4691 | struct task_struct *curr = current; | |
d6d897ce | 4692 | struct lock_class *class = NULL; |
fbb9ce95 | 4693 | struct held_lock *hlock; |
5f18ab5c | 4694 | unsigned int depth; |
fbb9ce95 | 4695 | int chain_head = 0; |
bb97a91e | 4696 | int class_idx; |
fbb9ce95 IM |
4697 | u64 chain_key; |
4698 | ||
4699 | if (unlikely(!debug_locks)) | |
4700 | return 0; | |
4701 | ||
fb9edbe9 ON |
4702 | if (!prove_locking || lock->key == &__lockdep_no_validate__) |
4703 | check = 0; | |
1704f47b | 4704 | |
62016250 HM |
4705 | if (subclass < NR_LOCKDEP_CACHING_CLASSES) |
4706 | class = lock->class_cache[subclass]; | |
d6d897ce | 4707 | /* |
62016250 | 4708 | * Not cached? |
d6d897ce | 4709 | */ |
fbb9ce95 | 4710 | if (unlikely(!class)) { |
4dfbb9d8 | 4711 | class = register_lock_class(lock, subclass, 0); |
fbb9ce95 IM |
4712 | if (!class) |
4713 | return 0; | |
4714 | } | |
8ca2b56c WL |
4715 | |
4716 | debug_class_ops_inc(class); | |
4717 | ||
fbb9ce95 | 4718 | if (very_verbose(class)) { |
04860d48 | 4719 | printk("\nacquire class [%px] %s", class->key, class->name); |
fbb9ce95 | 4720 | if (class->name_version > 1) |
f943fe0f DV |
4721 | printk(KERN_CONT "#%d", class->name_version); |
4722 | printk(KERN_CONT "\n"); | |
fbb9ce95 IM |
4723 | dump_stack(); |
4724 | } | |
4725 | ||
4726 | /* | |
4727 | * Add the lock to the list of currently held locks. | |
4728 | * (we dont increase the depth just yet, up until the | |
4729 | * dependency checks are done) | |
4730 | */ | |
4731 | depth = curr->lockdep_depth; | |
0119fee4 PZ |
4732 | /* |
4733 | * Ran out of static storage for our per-task lock stack again have we? | |
4734 | */ | |
fbb9ce95 IM |
4735 | if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH)) |
4736 | return 0; | |
4737 | ||
01bb6f0a | 4738 | class_idx = class - lock_classes; |
bb97a91e | 4739 | |
de8f5e4f | 4740 | if (depth) { /* we're holding locks */ |
bb97a91e PZ |
4741 | hlock = curr->held_locks + depth - 1; |
4742 | if (hlock->class_idx == class_idx && nest_lock) { | |
d9349850 ID |
4743 | if (!references) |
4744 | references++; | |
7fb4a2ce | 4745 | |
d9349850 | 4746 | if (!hlock->references) |
bb97a91e | 4747 | hlock->references++; |
d9349850 ID |
4748 | |
4749 | hlock->references += references; | |
4750 | ||
4751 | /* Overflow */ | |
4752 | if (DEBUG_LOCKS_WARN_ON(hlock->references < references)) | |
4753 | return 0; | |
bb97a91e | 4754 | |
8c8889d8 | 4755 | return 2; |
bb97a91e PZ |
4756 | } |
4757 | } | |
4758 | ||
fbb9ce95 | 4759 | hlock = curr->held_locks + depth; |
0119fee4 PZ |
4760 | /* |
4761 | * Plain impossible, we just registered it and checked it weren't no | |
4762 | * NULL like.. I bet this mushroom I ate was good! | |
4763 | */ | |
f82b217e DJ |
4764 | if (DEBUG_LOCKS_WARN_ON(!class)) |
4765 | return 0; | |
bb97a91e | 4766 | hlock->class_idx = class_idx; |
fbb9ce95 IM |
4767 | hlock->acquire_ip = ip; |
4768 | hlock->instance = lock; | |
7531e2f3 | 4769 | hlock->nest_lock = nest_lock; |
c2469756 | 4770 | hlock->irq_context = task_irq_context(curr); |
fbb9ce95 IM |
4771 | hlock->trylock = trylock; |
4772 | hlock->read = read; | |
4773 | hlock->check = check; | |
6951b12a | 4774 | hlock->hardirqs_off = !!hardirqs_off; |
bb97a91e | 4775 | hlock->references = references; |
f20786ff PZ |
4776 | #ifdef CONFIG_LOCK_STAT |
4777 | hlock->waittime_stamp = 0; | |
3365e779 | 4778 | hlock->holdtime_stamp = lockstat_clock(); |
f20786ff | 4779 | #endif |
21199f27 | 4780 | hlock->pin_count = pin_count; |
fbb9ce95 | 4781 | |
de8f5e4f PZ |
4782 | if (check_wait_context(curr, hlock)) |
4783 | return 0; | |
4784 | ||
09180651 YD |
4785 | /* Initialize the lock usage bit */ |
4786 | if (!mark_usage(curr, hlock, check)) | |
fbb9ce95 | 4787 | return 0; |
8e18257d | 4788 | |
fbb9ce95 | 4789 | /* |
17aacfb9 | 4790 | * Calculate the chain hash: it's the combined hash of all the |
fbb9ce95 IM |
4791 | * lock keys along the dependency chain. We save the hash value |
4792 | * at every step so that we can get the current hash easily | |
4793 | * after unlock. The chain hash is then used to cache dependency | |
4794 | * results. | |
4795 | * | |
4796 | * The 'key ID' is what is the most compact key value to drive | |
4797 | * the hash, not class->key. | |
4798 | */ | |
0119fee4 | 4799 | /* |
01bb6f0a | 4800 | * Whoops, we did it again.. class_idx is invalid. |
0119fee4 | 4801 | */ |
01bb6f0a | 4802 | if (DEBUG_LOCKS_WARN_ON(!test_bit(class_idx, lock_classes_in_use))) |
fbb9ce95 IM |
4803 | return 0; |
4804 | ||
4805 | chain_key = curr->curr_chain_key; | |
4806 | if (!depth) { | |
0119fee4 PZ |
4807 | /* |
4808 | * How can we have a chain hash when we ain't got no keys?! | |
4809 | */ | |
f6ec8829 | 4810 | if (DEBUG_LOCKS_WARN_ON(chain_key != INITIAL_CHAIN_KEY)) |
fbb9ce95 IM |
4811 | return 0; |
4812 | chain_head = 1; | |
4813 | } | |
4814 | ||
4815 | hlock->prev_chain_key = chain_key; | |
8e18257d | 4816 | if (separate_irq_context(curr, hlock)) { |
f6ec8829 | 4817 | chain_key = INITIAL_CHAIN_KEY; |
8e18257d | 4818 | chain_head = 1; |
fbb9ce95 | 4819 | } |
f611e8cf | 4820 | chain_key = iterate_chain_key(chain_key, hlock_id(hlock)); |
fbb9ce95 | 4821 | |
f7c1c6b3 YD |
4822 | if (nest_lock && !__lock_is_held(nest_lock, -1)) { |
4823 | print_lock_nested_lock_not_held(curr, hlock, ip); | |
4824 | return 0; | |
4825 | } | |
d0945950 | 4826 | |
a0b0fd53 BVA |
4827 | if (!debug_locks_silent) { |
4828 | WARN_ON_ONCE(depth && !hlock_class(hlock - 1)->key); | |
4829 | WARN_ON_ONCE(!hlock_class(hlock)->key); | |
4830 | } | |
4831 | ||
0b9fc8ec | 4832 | if (!validate_chain(curr, hlock, chain_head, chain_key)) |
8e18257d | 4833 | return 0; |
381a2292 | 4834 | |
3aa416b0 | 4835 | curr->curr_chain_key = chain_key; |
fbb9ce95 IM |
4836 | curr->lockdep_depth++; |
4837 | check_chain_key(curr); | |
60e114d1 JP |
4838 | #ifdef CONFIG_DEBUG_LOCKDEP |
4839 | if (unlikely(!debug_locks)) | |
4840 | return 0; | |
4841 | #endif | |
fbb9ce95 IM |
4842 | if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) { |
4843 | debug_locks_off(); | |
2c522836 DJ |
4844 | print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!"); |
4845 | printk(KERN_DEBUG "depth: %i max: %lu!\n", | |
c0540606 | 4846 | curr->lockdep_depth, MAX_LOCK_DEPTH); |
c0540606 BG |
4847 | |
4848 | lockdep_print_held_locks(current); | |
4849 | debug_show_all_locks(); | |
eedeeabd | 4850 | dump_stack(); |
c0540606 | 4851 | |
fbb9ce95 IM |
4852 | return 0; |
4853 | } | |
381a2292 | 4854 | |
fbb9ce95 IM |
4855 | if (unlikely(curr->lockdep_depth > max_lockdep_depth)) |
4856 | max_lockdep_depth = curr->lockdep_depth; | |
4857 | ||
4858 | return 1; | |
4859 | } | |
4860 | ||
f7c1c6b3 YD |
4861 | static void print_unlock_imbalance_bug(struct task_struct *curr, |
4862 | struct lockdep_map *lock, | |
4863 | unsigned long ip) | |
fbb9ce95 IM |
4864 | { |
4865 | if (!debug_locks_off()) | |
f7c1c6b3 | 4866 | return; |
fbb9ce95 | 4867 | if (debug_locks_silent) |
f7c1c6b3 | 4868 | return; |
fbb9ce95 | 4869 | |
681fbec8 | 4870 | pr_warn("\n"); |
a5dd63ef PM |
4871 | pr_warn("=====================================\n"); |
4872 | pr_warn("WARNING: bad unlock balance detected!\n"); | |
fbdc4b9a | 4873 | print_kernel_ident(); |
a5dd63ef | 4874 | pr_warn("-------------------------------------\n"); |
681fbec8 | 4875 | pr_warn("%s/%d is trying to release lock (", |
ba25f9dc | 4876 | curr->comm, task_pid_nr(curr)); |
fbb9ce95 | 4877 | print_lockdep_cache(lock); |
681fbec8 | 4878 | pr_cont(") at:\n"); |
2062a4e8 | 4879 | print_ip_sym(KERN_WARNING, ip); |
681fbec8 PM |
4880 | pr_warn("but there are no more locks to release!\n"); |
4881 | pr_warn("\nother info that might help us debug this:\n"); | |
fbb9ce95 IM |
4882 | lockdep_print_held_locks(curr); |
4883 | ||
681fbec8 | 4884 | pr_warn("\nstack backtrace:\n"); |
fbb9ce95 | 4885 | dump_stack(); |
fbb9ce95 IM |
4886 | } |
4887 | ||
c86e9b98 PZ |
4888 | static noinstr int match_held_lock(const struct held_lock *hlock, |
4889 | const struct lockdep_map *lock) | |
bb97a91e PZ |
4890 | { |
4891 | if (hlock->instance == lock) | |
4892 | return 1; | |
4893 | ||
4894 | if (hlock->references) { | |
08f36ff6 | 4895 | const struct lock_class *class = lock->class_cache[0]; |
bb97a91e PZ |
4896 | |
4897 | if (!class) | |
4898 | class = look_up_lock_class(lock, 0); | |
4899 | ||
80e0401e PZ |
4900 | /* |
4901 | * If look_up_lock_class() failed to find a class, we're trying | |
4902 | * to test if we hold a lock that has never yet been acquired. | |
4903 | * Clearly if the lock hasn't been acquired _ever_, we're not | |
4904 | * holding it either, so report failure. | |
4905 | */ | |
64f29d1b | 4906 | if (!class) |
bb97a91e PZ |
4907 | return 0; |
4908 | ||
0119fee4 PZ |
4909 | /* |
4910 | * References, but not a lock we're actually ref-counting? | |
4911 | * State got messed up, follow the sites that change ->references | |
4912 | * and try to make sense of it. | |
4913 | */ | |
bb97a91e PZ |
4914 | if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock)) |
4915 | return 0; | |
4916 | ||
01bb6f0a | 4917 | if (hlock->class_idx == class - lock_classes) |
bb97a91e PZ |
4918 | return 1; |
4919 | } | |
4920 | ||
4921 | return 0; | |
4922 | } | |
4923 | ||
41c2c5b8 O |
4924 | /* @depth must not be zero */ |
4925 | static struct held_lock *find_held_lock(struct task_struct *curr, | |
4926 | struct lockdep_map *lock, | |
4927 | unsigned int depth, int *idx) | |
4928 | { | |
4929 | struct held_lock *ret, *hlock, *prev_hlock; | |
4930 | int i; | |
4931 | ||
4932 | i = depth - 1; | |
4933 | hlock = curr->held_locks + i; | |
4934 | ret = hlock; | |
4935 | if (match_held_lock(hlock, lock)) | |
4936 | goto out; | |
4937 | ||
4938 | ret = NULL; | |
4939 | for (i--, prev_hlock = hlock--; | |
4940 | i >= 0; | |
4941 | i--, prev_hlock = hlock--) { | |
4942 | /* | |
4943 | * We must not cross into another context: | |
4944 | */ | |
4945 | if (prev_hlock->irq_context != hlock->irq_context) { | |
4946 | ret = NULL; | |
4947 | break; | |
4948 | } | |
4949 | if (match_held_lock(hlock, lock)) { | |
4950 | ret = hlock; | |
4951 | break; | |
4952 | } | |
4953 | } | |
4954 | ||
4955 | out: | |
4956 | *idx = i; | |
4957 | return ret; | |
4958 | } | |
4959 | ||
e969970b | 4960 | static int reacquire_held_locks(struct task_struct *curr, unsigned int depth, |
8c8889d8 | 4961 | int idx, unsigned int *merged) |
e969970b O |
4962 | { |
4963 | struct held_lock *hlock; | |
8c8889d8 | 4964 | int first_idx = idx; |
e969970b | 4965 | |
8ee10862 WL |
4966 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
4967 | return 0; | |
4968 | ||
e969970b | 4969 | for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) { |
8c8889d8 | 4970 | switch (__lock_acquire(hlock->instance, |
e969970b O |
4971 | hlock_class(hlock)->subclass, |
4972 | hlock->trylock, | |
4973 | hlock->read, hlock->check, | |
4974 | hlock->hardirqs_off, | |
4975 | hlock->nest_lock, hlock->acquire_ip, | |
8c8889d8 ID |
4976 | hlock->references, hlock->pin_count)) { |
4977 | case 0: | |
e969970b | 4978 | return 1; |
8c8889d8 ID |
4979 | case 1: |
4980 | break; | |
4981 | case 2: | |
4982 | *merged += (idx == first_idx); | |
4983 | break; | |
4984 | default: | |
4985 | WARN_ON(1); | |
4986 | return 0; | |
4987 | } | |
e969970b O |
4988 | } |
4989 | return 0; | |
4990 | } | |
4991 | ||
64aa348e | 4992 | static int |
00ef9f73 PZ |
4993 | __lock_set_class(struct lockdep_map *lock, const char *name, |
4994 | struct lock_class_key *key, unsigned int subclass, | |
4995 | unsigned long ip) | |
64aa348e PZ |
4996 | { |
4997 | struct task_struct *curr = current; | |
8c8889d8 | 4998 | unsigned int depth, merged = 0; |
41c2c5b8 | 4999 | struct held_lock *hlock; |
64aa348e | 5000 | struct lock_class *class; |
64aa348e PZ |
5001 | int i; |
5002 | ||
513e1073 WL |
5003 | if (unlikely(!debug_locks)) |
5004 | return 0; | |
5005 | ||
64aa348e | 5006 | depth = curr->lockdep_depth; |
0119fee4 PZ |
5007 | /* |
5008 | * This function is about (re)setting the class of a held lock, | |
5009 | * yet we're not actually holding any locks. Naughty user! | |
5010 | */ | |
64aa348e PZ |
5011 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
5012 | return 0; | |
5013 | ||
41c2c5b8 | 5014 | hlock = find_held_lock(curr, lock, depth, &i); |
f7c1c6b3 YD |
5015 | if (!hlock) { |
5016 | print_unlock_imbalance_bug(curr, lock, ip); | |
5017 | return 0; | |
5018 | } | |
64aa348e | 5019 | |
de8f5e4f PZ |
5020 | lockdep_init_map_waits(lock, name, key, 0, |
5021 | lock->wait_type_inner, | |
5022 | lock->wait_type_outer); | |
64aa348e | 5023 | class = register_lock_class(lock, subclass, 0); |
01bb6f0a | 5024 | hlock->class_idx = class - lock_classes; |
64aa348e PZ |
5025 | |
5026 | curr->lockdep_depth = i; | |
5027 | curr->curr_chain_key = hlock->prev_chain_key; | |
5028 | ||
8c8889d8 | 5029 | if (reacquire_held_locks(curr, depth, i, &merged)) |
e969970b | 5030 | return 0; |
64aa348e | 5031 | |
0119fee4 PZ |
5032 | /* |
5033 | * I took it apart and put it back together again, except now I have | |
5034 | * these 'spare' parts.. where shall I put them. | |
5035 | */ | |
8c8889d8 | 5036 | if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged)) |
64aa348e PZ |
5037 | return 0; |
5038 | return 1; | |
5039 | } | |
5040 | ||
6419c4af O |
5041 | static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip) |
5042 | { | |
5043 | struct task_struct *curr = current; | |
8c8889d8 | 5044 | unsigned int depth, merged = 0; |
6419c4af | 5045 | struct held_lock *hlock; |
6419c4af O |
5046 | int i; |
5047 | ||
71492580 WL |
5048 | if (unlikely(!debug_locks)) |
5049 | return 0; | |
5050 | ||
6419c4af O |
5051 | depth = curr->lockdep_depth; |
5052 | /* | |
5053 | * This function is about (re)setting the class of a held lock, | |
5054 | * yet we're not actually holding any locks. Naughty user! | |
5055 | */ | |
5056 | if (DEBUG_LOCKS_WARN_ON(!depth)) | |
5057 | return 0; | |
5058 | ||
5059 | hlock = find_held_lock(curr, lock, depth, &i); | |
f7c1c6b3 YD |
5060 | if (!hlock) { |
5061 | print_unlock_imbalance_bug(curr, lock, ip); | |
5062 | return 0; | |
5063 | } | |
6419c4af O |
5064 | |
5065 | curr->lockdep_depth = i; | |
5066 | curr->curr_chain_key = hlock->prev_chain_key; | |
5067 | ||
5068 | WARN(hlock->read, "downgrading a read lock"); | |
5069 | hlock->read = 1; | |
5070 | hlock->acquire_ip = ip; | |
5071 | ||
8c8889d8 ID |
5072 | if (reacquire_held_locks(curr, depth, i, &merged)) |
5073 | return 0; | |
5074 | ||
5075 | /* Merging can't happen with unchanged classes.. */ | |
5076 | if (DEBUG_LOCKS_WARN_ON(merged)) | |
6419c4af O |
5077 | return 0; |
5078 | ||
5079 | /* | |
5080 | * I took it apart and put it back together again, except now I have | |
5081 | * these 'spare' parts.. where shall I put them. | |
5082 | */ | |
5083 | if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth)) | |
5084 | return 0; | |
8c8889d8 | 5085 | |
6419c4af O |
5086 | return 1; |
5087 | } | |
5088 | ||
fbb9ce95 | 5089 | /* |
c759bc47 | 5090 | * Remove the lock from the list of currently held locks - this gets |
e0f56fd7 PZ |
5091 | * called on mutex_unlock()/spin_unlock*() (or on a failed |
5092 | * mutex_lock_interruptible()). | |
fbb9ce95 IM |
5093 | */ |
5094 | static int | |
b4adfe8e | 5095 | __lock_release(struct lockdep_map *lock, unsigned long ip) |
fbb9ce95 | 5096 | { |
e0f56fd7 | 5097 | struct task_struct *curr = current; |
8c8889d8 | 5098 | unsigned int depth, merged = 1; |
41c2c5b8 | 5099 | struct held_lock *hlock; |
e966eaee | 5100 | int i; |
fbb9ce95 | 5101 | |
e0f56fd7 PZ |
5102 | if (unlikely(!debug_locks)) |
5103 | return 0; | |
5104 | ||
fbb9ce95 | 5105 | depth = curr->lockdep_depth; |
0119fee4 PZ |
5106 | /* |
5107 | * So we're all set to release this lock.. wait what lock? We don't | |
5108 | * own any locks, you've been drinking again? | |
5109 | */ | |
dd471efe | 5110 | if (depth <= 0) { |
f7c1c6b3 YD |
5111 | print_unlock_imbalance_bug(curr, lock, ip); |
5112 | return 0; | |
5113 | } | |
fbb9ce95 | 5114 | |
e0f56fd7 PZ |
5115 | /* |
5116 | * Check whether the lock exists in the current stack | |
5117 | * of held locks: | |
5118 | */ | |
41c2c5b8 | 5119 | hlock = find_held_lock(curr, lock, depth, &i); |
f7c1c6b3 YD |
5120 | if (!hlock) { |
5121 | print_unlock_imbalance_bug(curr, lock, ip); | |
5122 | return 0; | |
5123 | } | |
fbb9ce95 | 5124 | |
bb97a91e PZ |
5125 | if (hlock->instance == lock) |
5126 | lock_release_holdtime(hlock); | |
5127 | ||
a24fc60d PZ |
5128 | WARN(hlock->pin_count, "releasing a pinned lock\n"); |
5129 | ||
bb97a91e PZ |
5130 | if (hlock->references) { |
5131 | hlock->references--; | |
5132 | if (hlock->references) { | |
5133 | /* | |
5134 | * We had, and after removing one, still have | |
5135 | * references, the current lock stack is still | |
5136 | * valid. We're done! | |
5137 | */ | |
5138 | return 1; | |
5139 | } | |
5140 | } | |
f20786ff | 5141 | |
fbb9ce95 IM |
5142 | /* |
5143 | * We have the right lock to unlock, 'hlock' points to it. | |
5144 | * Now we remove it from the stack, and add back the other | |
5145 | * entries (if any), recalculating the hash along the way: | |
5146 | */ | |
bb97a91e | 5147 | |
fbb9ce95 IM |
5148 | curr->lockdep_depth = i; |
5149 | curr->curr_chain_key = hlock->prev_chain_key; | |
5150 | ||
ce52a18d WL |
5151 | /* |
5152 | * The most likely case is when the unlock is on the innermost | |
5153 | * lock. In this case, we are done! | |
5154 | */ | |
5155 | if (i == depth-1) | |
5156 | return 1; | |
5157 | ||
8c8889d8 | 5158 | if (reacquire_held_locks(curr, depth, i + 1, &merged)) |
e969970b | 5159 | return 0; |
fbb9ce95 | 5160 | |
0119fee4 PZ |
5161 | /* |
5162 | * We had N bottles of beer on the wall, we drank one, but now | |
5163 | * there's not N-1 bottles of beer left on the wall... | |
8c8889d8 | 5164 | * Pouring two of the bottles together is acceptable. |
0119fee4 | 5165 | */ |
8c8889d8 | 5166 | DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged); |
f20786ff | 5167 | |
ce52a18d WL |
5168 | /* |
5169 | * Since reacquire_held_locks() would have called check_chain_key() | |
5170 | * indirectly via __lock_acquire(), we don't need to do it again | |
5171 | * on return. | |
5172 | */ | |
5173 | return 0; | |
fbb9ce95 IM |
5174 | } |
5175 | ||
c86e9b98 | 5176 | static __always_inline |
2f43c602 | 5177 | int __lock_is_held(const struct lockdep_map *lock, int read) |
fbb9ce95 | 5178 | { |
f607c668 PZ |
5179 | struct task_struct *curr = current; |
5180 | int i; | |
fbb9ce95 | 5181 | |
f607c668 | 5182 | for (i = 0; i < curr->lockdep_depth; i++) { |
bb97a91e | 5183 | struct held_lock *hlock = curr->held_locks + i; |
fbb9ce95 | 5184 | |
f8319483 PZ |
5185 | if (match_held_lock(hlock, lock)) { |
5186 | if (read == -1 || hlock->read == read) | |
5187 | return 1; | |
5188 | ||
5189 | return 0; | |
5190 | } | |
f607c668 | 5191 | } |
f20786ff | 5192 | |
f607c668 | 5193 | return 0; |
fbb9ce95 IM |
5194 | } |
5195 | ||
e7904a28 PZ |
5196 | static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock) |
5197 | { | |
5198 | struct pin_cookie cookie = NIL_COOKIE; | |
5199 | struct task_struct *curr = current; | |
5200 | int i; | |
5201 | ||
5202 | if (unlikely(!debug_locks)) | |
5203 | return cookie; | |
5204 | ||
5205 | for (i = 0; i < curr->lockdep_depth; i++) { | |
5206 | struct held_lock *hlock = curr->held_locks + i; | |
5207 | ||
5208 | if (match_held_lock(hlock, lock)) { | |
5209 | /* | |
5210 | * Grab 16bits of randomness; this is sufficient to not | |
5211 | * be guessable and still allows some pin nesting in | |
5212 | * our u32 pin_count. | |
5213 | */ | |
5214 | cookie.val = 1 + (prandom_u32() >> 16); | |
5215 | hlock->pin_count += cookie.val; | |
5216 | return cookie; | |
5217 | } | |
5218 | } | |
5219 | ||
5220 | WARN(1, "pinning an unheld lock\n"); | |
5221 | return cookie; | |
5222 | } | |
5223 | ||
5224 | static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie) | |
fbb9ce95 IM |
5225 | { |
5226 | struct task_struct *curr = current; | |
a24fc60d | 5227 | int i; |
fbb9ce95 | 5228 | |
a24fc60d | 5229 | if (unlikely(!debug_locks)) |
fbb9ce95 IM |
5230 | return; |
5231 | ||
a24fc60d PZ |
5232 | for (i = 0; i < curr->lockdep_depth; i++) { |
5233 | struct held_lock *hlock = curr->held_locks + i; | |
5234 | ||
5235 | if (match_held_lock(hlock, lock)) { | |
e7904a28 | 5236 | hlock->pin_count += cookie.val; |
fbb9ce95 | 5237 | return; |
a24fc60d | 5238 | } |
fbb9ce95 IM |
5239 | } |
5240 | ||
a24fc60d | 5241 | WARN(1, "pinning an unheld lock\n"); |
fbb9ce95 IM |
5242 | } |
5243 | ||
e7904a28 | 5244 | static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie) |
f607c668 PZ |
5245 | { |
5246 | struct task_struct *curr = current; | |
5247 | int i; | |
5248 | ||
a24fc60d PZ |
5249 | if (unlikely(!debug_locks)) |
5250 | return; | |
5251 | ||
f607c668 | 5252 | for (i = 0; i < curr->lockdep_depth; i++) { |
bb97a91e PZ |
5253 | struct held_lock *hlock = curr->held_locks + i; |
5254 | ||
a24fc60d PZ |
5255 | if (match_held_lock(hlock, lock)) { |
5256 | if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n")) | |
5257 | return; | |
5258 | ||
e7904a28 PZ |
5259 | hlock->pin_count -= cookie.val; |
5260 | ||
5261 | if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n")) | |
5262 | hlock->pin_count = 0; | |
5263 | ||
a24fc60d PZ |
5264 | return; |
5265 | } | |
f607c668 PZ |
5266 | } |
5267 | ||
a24fc60d | 5268 | WARN(1, "unpinning an unheld lock\n"); |
f607c668 PZ |
5269 | } |
5270 | ||
fbb9ce95 IM |
5271 | /* |
5272 | * Check whether we follow the irq-flags state precisely: | |
5273 | */ | |
1d09daa5 | 5274 | static void check_flags(unsigned long flags) |
fbb9ce95 | 5275 | { |
30a35f79 | 5276 | #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) |
fbb9ce95 IM |
5277 | if (!debug_locks) |
5278 | return; | |
5279 | ||
5f9fa8a6 | 5280 | if (irqs_disabled_flags(flags)) { |
f9ad4a5f | 5281 | if (DEBUG_LOCKS_WARN_ON(lockdep_hardirqs_enabled())) { |
5f9fa8a6 IM |
5282 | printk("possible reason: unannotated irqs-off.\n"); |
5283 | } | |
5284 | } else { | |
f9ad4a5f | 5285 | if (DEBUG_LOCKS_WARN_ON(!lockdep_hardirqs_enabled())) { |
5f9fa8a6 IM |
5286 | printk("possible reason: unannotated irqs-on.\n"); |
5287 | } | |
5288 | } | |
fbb9ce95 IM |
5289 | |
5290 | /* | |
5291 | * We dont accurately track softirq state in e.g. | |
5292 | * hardirq contexts (such as on 4KSTACKS), so only | |
5293 | * check if not in hardirq contexts: | |
5294 | */ | |
5295 | if (!hardirq_count()) { | |
0119fee4 PZ |
5296 | if (softirq_count()) { |
5297 | /* like the above, but with softirqs */ | |
fbb9ce95 | 5298 | DEBUG_LOCKS_WARN_ON(current->softirqs_enabled); |
0119fee4 PZ |
5299 | } else { |
5300 | /* lick the above, does it taste good? */ | |
fbb9ce95 | 5301 | DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled); |
0119fee4 | 5302 | } |
fbb9ce95 IM |
5303 | } |
5304 | ||
5305 | if (!debug_locks) | |
5306 | print_irqtrace_events(current); | |
5307 | #endif | |
5308 | } | |
5309 | ||
00ef9f73 PZ |
5310 | void lock_set_class(struct lockdep_map *lock, const char *name, |
5311 | struct lock_class_key *key, unsigned int subclass, | |
5312 | unsigned long ip) | |
64aa348e PZ |
5313 | { |
5314 | unsigned long flags; | |
5315 | ||
4d004099 | 5316 | if (unlikely(!lockdep_enabled())) |
64aa348e PZ |
5317 | return; |
5318 | ||
5319 | raw_local_irq_save(flags); | |
4d004099 | 5320 | lockdep_recursion_inc(); |
64aa348e | 5321 | check_flags(flags); |
00ef9f73 | 5322 | if (__lock_set_class(lock, name, key, subclass, ip)) |
64aa348e | 5323 | check_chain_key(current); |
10476e63 | 5324 | lockdep_recursion_finish(); |
64aa348e PZ |
5325 | raw_local_irq_restore(flags); |
5326 | } | |
00ef9f73 | 5327 | EXPORT_SYMBOL_GPL(lock_set_class); |
64aa348e | 5328 | |
6419c4af O |
5329 | void lock_downgrade(struct lockdep_map *lock, unsigned long ip) |
5330 | { | |
5331 | unsigned long flags; | |
5332 | ||
4d004099 | 5333 | if (unlikely(!lockdep_enabled())) |
6419c4af O |
5334 | return; |
5335 | ||
5336 | raw_local_irq_save(flags); | |
4d004099 | 5337 | lockdep_recursion_inc(); |
6419c4af O |
5338 | check_flags(flags); |
5339 | if (__lock_downgrade(lock, ip)) | |
5340 | check_chain_key(current); | |
10476e63 | 5341 | lockdep_recursion_finish(); |
6419c4af O |
5342 | raw_local_irq_restore(flags); |
5343 | } | |
5344 | EXPORT_SYMBOL_GPL(lock_downgrade); | |
5345 | ||
f6f48e18 PZ |
5346 | /* NMI context !!! */ |
5347 | static void verify_lock_unused(struct lockdep_map *lock, struct held_lock *hlock, int subclass) | |
5348 | { | |
5349 | #ifdef CONFIG_PROVE_LOCKING | |
5350 | struct lock_class *class = look_up_lock_class(lock, subclass); | |
23870f12 | 5351 | unsigned long mask = LOCKF_USED; |
f6f48e18 PZ |
5352 | |
5353 | /* if it doesn't have a class (yet), it certainly hasn't been used yet */ | |
5354 | if (!class) | |
5355 | return; | |
5356 | ||
23870f12 | 5357 | /* |
5358 | * READ locks only conflict with USED, such that if we only ever use | |
5359 | * READ locks, there is no deadlock possible -- RCU. | |
5360 | */ | |
5361 | if (!hlock->read) | |
5362 | mask |= LOCKF_USED_READ; | |
5363 | ||
5364 | if (!(class->usage_mask & mask)) | |
f6f48e18 PZ |
5365 | return; |
5366 | ||
5367 | hlock->class_idx = class - lock_classes; | |
5368 | ||
5369 | print_usage_bug(current, hlock, LOCK_USED, LOCK_USAGE_STATES); | |
5370 | #endif | |
5371 | } | |
5372 | ||
5373 | static bool lockdep_nmi(void) | |
5374 | { | |
4d004099 | 5375 | if (raw_cpu_read(lockdep_recursion)) |
f6f48e18 PZ |
5376 | return false; |
5377 | ||
5378 | if (!in_nmi()) | |
5379 | return false; | |
5380 | ||
5381 | return true; | |
5382 | } | |
5383 | ||
e9181886 BF |
5384 | /* |
5385 | * read_lock() is recursive if: | |
5386 | * 1. We force lockdep think this way in selftests or | |
5387 | * 2. The implementation is not queued read/write lock or | |
5388 | * 3. The locker is at an in_interrupt() context. | |
5389 | */ | |
5390 | bool read_lock_is_recursive(void) | |
5391 | { | |
5392 | return force_read_lock_recursive || | |
5393 | !IS_ENABLED(CONFIG_QUEUED_RWLOCKS) || | |
5394 | in_interrupt(); | |
5395 | } | |
5396 | EXPORT_SYMBOL_GPL(read_lock_is_recursive); | |
5397 | ||
fbb9ce95 IM |
5398 | /* |
5399 | * We are not always called with irqs disabled - do that here, | |
5400 | * and also avoid lockdep recursion: | |
5401 | */ | |
1d09daa5 | 5402 | void lock_acquire(struct lockdep_map *lock, unsigned int subclass, |
7531e2f3 PZ |
5403 | int trylock, int read, int check, |
5404 | struct lockdep_map *nest_lock, unsigned long ip) | |
fbb9ce95 IM |
5405 | { |
5406 | unsigned long flags; | |
5407 | ||
eb1f0023 PZ |
5408 | trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip); |
5409 | ||
4d004099 PZ |
5410 | if (!debug_locks) |
5411 | return; | |
5412 | ||
5413 | if (unlikely(!lockdep_enabled())) { | |
f6f48e18 PZ |
5414 | /* XXX allow trylock from NMI ?!? */ |
5415 | if (lockdep_nmi() && !trylock) { | |
5416 | struct held_lock hlock; | |
5417 | ||
5418 | hlock.acquire_ip = ip; | |
5419 | hlock.instance = lock; | |
5420 | hlock.nest_lock = nest_lock; | |
5421 | hlock.irq_context = 2; // XXX | |
5422 | hlock.trylock = trylock; | |
5423 | hlock.read = read; | |
5424 | hlock.check = check; | |
5425 | hlock.hardirqs_off = true; | |
5426 | hlock.references = 0; | |
5427 | ||
5428 | verify_lock_unused(lock, &hlock, subclass); | |
5429 | } | |
fbb9ce95 | 5430 | return; |
f6f48e18 | 5431 | } |
fbb9ce95 IM |
5432 | |
5433 | raw_local_irq_save(flags); | |
5434 | check_flags(flags); | |
5435 | ||
4d004099 | 5436 | lockdep_recursion_inc(); |
fbb9ce95 | 5437 | __lock_acquire(lock, subclass, trylock, read, check, |
21199f27 | 5438 | irqs_disabled_flags(flags), nest_lock, ip, 0, 0); |
10476e63 | 5439 | lockdep_recursion_finish(); |
fbb9ce95 IM |
5440 | raw_local_irq_restore(flags); |
5441 | } | |
fbb9ce95 IM |
5442 | EXPORT_SYMBOL_GPL(lock_acquire); |
5443 | ||
5facae4f | 5444 | void lock_release(struct lockdep_map *lock, unsigned long ip) |
fbb9ce95 IM |
5445 | { |
5446 | unsigned long flags; | |
5447 | ||
eb1f0023 PZ |
5448 | trace_lock_release(lock, ip); |
5449 | ||
4d004099 | 5450 | if (unlikely(!lockdep_enabled())) |
fbb9ce95 IM |
5451 | return; |
5452 | ||
5453 | raw_local_irq_save(flags); | |
5454 | check_flags(flags); | |
eb1f0023 | 5455 | |
4d004099 | 5456 | lockdep_recursion_inc(); |
b4adfe8e | 5457 | if (__lock_release(lock, ip)) |
e0f56fd7 | 5458 | check_chain_key(current); |
10476e63 | 5459 | lockdep_recursion_finish(); |
fbb9ce95 IM |
5460 | raw_local_irq_restore(flags); |
5461 | } | |
fbb9ce95 IM |
5462 | EXPORT_SYMBOL_GPL(lock_release); |
5463 | ||
c86e9b98 | 5464 | noinstr int lock_is_held_type(const struct lockdep_map *lock, int read) |
f607c668 PZ |
5465 | { |
5466 | unsigned long flags; | |
5467 | int ret = 0; | |
5468 | ||
4d004099 | 5469 | if (unlikely(!lockdep_enabled())) |
f2513cde | 5470 | return 1; /* avoid false negative lockdep_assert_held() */ |
f607c668 PZ |
5471 | |
5472 | raw_local_irq_save(flags); | |
5473 | check_flags(flags); | |
5474 | ||
4d004099 | 5475 | lockdep_recursion_inc(); |
f8319483 | 5476 | ret = __lock_is_held(lock, read); |
10476e63 | 5477 | lockdep_recursion_finish(); |
f607c668 PZ |
5478 | raw_local_irq_restore(flags); |
5479 | ||
5480 | return ret; | |
5481 | } | |
f8319483 | 5482 | EXPORT_SYMBOL_GPL(lock_is_held_type); |
2f43c602 | 5483 | NOKPROBE_SYMBOL(lock_is_held_type); |
f607c668 | 5484 | |
e7904a28 | 5485 | struct pin_cookie lock_pin_lock(struct lockdep_map *lock) |
a24fc60d | 5486 | { |
e7904a28 | 5487 | struct pin_cookie cookie = NIL_COOKIE; |
a24fc60d PZ |
5488 | unsigned long flags; |
5489 | ||
4d004099 | 5490 | if (unlikely(!lockdep_enabled())) |
e7904a28 | 5491 | return cookie; |
a24fc60d PZ |
5492 | |
5493 | raw_local_irq_save(flags); | |
5494 | check_flags(flags); | |
5495 | ||
4d004099 | 5496 | lockdep_recursion_inc(); |
e7904a28 | 5497 | cookie = __lock_pin_lock(lock); |
10476e63 | 5498 | lockdep_recursion_finish(); |
a24fc60d | 5499 | raw_local_irq_restore(flags); |
e7904a28 PZ |
5500 | |
5501 | return cookie; | |
a24fc60d PZ |
5502 | } |
5503 | EXPORT_SYMBOL_GPL(lock_pin_lock); | |
5504 | ||
e7904a28 PZ |
5505 | void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie) |
5506 | { | |
5507 | unsigned long flags; | |
5508 | ||
4d004099 | 5509 | if (unlikely(!lockdep_enabled())) |
e7904a28 PZ |
5510 | return; |
5511 | ||
5512 | raw_local_irq_save(flags); | |
5513 | check_flags(flags); | |
5514 | ||
4d004099 | 5515 | lockdep_recursion_inc(); |
e7904a28 | 5516 | __lock_repin_lock(lock, cookie); |
10476e63 | 5517 | lockdep_recursion_finish(); |
e7904a28 PZ |
5518 | raw_local_irq_restore(flags); |
5519 | } | |
5520 | EXPORT_SYMBOL_GPL(lock_repin_lock); | |
5521 | ||
5522 | void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie) | |
a24fc60d PZ |
5523 | { |
5524 | unsigned long flags; | |
5525 | ||
4d004099 | 5526 | if (unlikely(!lockdep_enabled())) |
a24fc60d PZ |
5527 | return; |
5528 | ||
5529 | raw_local_irq_save(flags); | |
5530 | check_flags(flags); | |
5531 | ||
4d004099 | 5532 | lockdep_recursion_inc(); |
e7904a28 | 5533 | __lock_unpin_lock(lock, cookie); |
10476e63 | 5534 | lockdep_recursion_finish(); |
a24fc60d PZ |
5535 | raw_local_irq_restore(flags); |
5536 | } | |
5537 | EXPORT_SYMBOL_GPL(lock_unpin_lock); | |
5538 | ||
f20786ff | 5539 | #ifdef CONFIG_LOCK_STAT |
f7c1c6b3 YD |
5540 | static void print_lock_contention_bug(struct task_struct *curr, |
5541 | struct lockdep_map *lock, | |
5542 | unsigned long ip) | |
f20786ff PZ |
5543 | { |
5544 | if (!debug_locks_off()) | |
f7c1c6b3 | 5545 | return; |
f20786ff | 5546 | if (debug_locks_silent) |
f7c1c6b3 | 5547 | return; |
f20786ff | 5548 | |
681fbec8 | 5549 | pr_warn("\n"); |
a5dd63ef PM |
5550 | pr_warn("=================================\n"); |
5551 | pr_warn("WARNING: bad contention detected!\n"); | |
fbdc4b9a | 5552 | print_kernel_ident(); |
a5dd63ef | 5553 | pr_warn("---------------------------------\n"); |
681fbec8 | 5554 | pr_warn("%s/%d is trying to contend lock (", |
ba25f9dc | 5555 | curr->comm, task_pid_nr(curr)); |
f20786ff | 5556 | print_lockdep_cache(lock); |
681fbec8 | 5557 | pr_cont(") at:\n"); |
2062a4e8 | 5558 | print_ip_sym(KERN_WARNING, ip); |
681fbec8 PM |
5559 | pr_warn("but there are no locks held!\n"); |
5560 | pr_warn("\nother info that might help us debug this:\n"); | |
f20786ff PZ |
5561 | lockdep_print_held_locks(curr); |
5562 | ||
681fbec8 | 5563 | pr_warn("\nstack backtrace:\n"); |
f20786ff | 5564 | dump_stack(); |
f20786ff PZ |
5565 | } |
5566 | ||
5567 | static void | |
5568 | __lock_contended(struct lockdep_map *lock, unsigned long ip) | |
5569 | { | |
5570 | struct task_struct *curr = current; | |
41c2c5b8 | 5571 | struct held_lock *hlock; |
f20786ff PZ |
5572 | struct lock_class_stats *stats; |
5573 | unsigned int depth; | |
c7e78cff | 5574 | int i, contention_point, contending_point; |
f20786ff PZ |
5575 | |
5576 | depth = curr->lockdep_depth; | |
0119fee4 PZ |
5577 | /* |
5578 | * Whee, we contended on this lock, except it seems we're not | |
5579 | * actually trying to acquire anything much at all.. | |
5580 | */ | |
f20786ff PZ |
5581 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
5582 | return; | |
5583 | ||
41c2c5b8 O |
5584 | hlock = find_held_lock(curr, lock, depth, &i); |
5585 | if (!hlock) { | |
5586 | print_lock_contention_bug(curr, lock, ip); | |
5587 | return; | |
f20786ff | 5588 | } |
f20786ff | 5589 | |
bb97a91e PZ |
5590 | if (hlock->instance != lock) |
5591 | return; | |
5592 | ||
3365e779 | 5593 | hlock->waittime_stamp = lockstat_clock(); |
f20786ff | 5594 | |
c7e78cff PZ |
5595 | contention_point = lock_point(hlock_class(hlock)->contention_point, ip); |
5596 | contending_point = lock_point(hlock_class(hlock)->contending_point, | |
5597 | lock->ip); | |
f20786ff | 5598 | |
f82b217e | 5599 | stats = get_lock_stats(hlock_class(hlock)); |
c7e78cff PZ |
5600 | if (contention_point < LOCKSTAT_POINTS) |
5601 | stats->contention_point[contention_point]++; | |
5602 | if (contending_point < LOCKSTAT_POINTS) | |
5603 | stats->contending_point[contending_point]++; | |
96645678 PZ |
5604 | if (lock->cpu != smp_processor_id()) |
5605 | stats->bounces[bounce_contended + !!hlock->read]++; | |
f20786ff PZ |
5606 | } |
5607 | ||
5608 | static void | |
c7e78cff | 5609 | __lock_acquired(struct lockdep_map *lock, unsigned long ip) |
f20786ff PZ |
5610 | { |
5611 | struct task_struct *curr = current; | |
41c2c5b8 | 5612 | struct held_lock *hlock; |
f20786ff PZ |
5613 | struct lock_class_stats *stats; |
5614 | unsigned int depth; | |
3365e779 | 5615 | u64 now, waittime = 0; |
96645678 | 5616 | int i, cpu; |
f20786ff PZ |
5617 | |
5618 | depth = curr->lockdep_depth; | |
0119fee4 PZ |
5619 | /* |
5620 | * Yay, we acquired ownership of this lock we didn't try to | |
5621 | * acquire, how the heck did that happen? | |
5622 | */ | |
f20786ff PZ |
5623 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
5624 | return; | |
5625 | ||
41c2c5b8 O |
5626 | hlock = find_held_lock(curr, lock, depth, &i); |
5627 | if (!hlock) { | |
5628 | print_lock_contention_bug(curr, lock, _RET_IP_); | |
5629 | return; | |
f20786ff | 5630 | } |
f20786ff | 5631 | |
bb97a91e PZ |
5632 | if (hlock->instance != lock) |
5633 | return; | |
5634 | ||
96645678 PZ |
5635 | cpu = smp_processor_id(); |
5636 | if (hlock->waittime_stamp) { | |
3365e779 | 5637 | now = lockstat_clock(); |
96645678 PZ |
5638 | waittime = now - hlock->waittime_stamp; |
5639 | hlock->holdtime_stamp = now; | |
5640 | } | |
f20786ff | 5641 | |
f82b217e | 5642 | stats = get_lock_stats(hlock_class(hlock)); |
96645678 PZ |
5643 | if (waittime) { |
5644 | if (hlock->read) | |
5645 | lock_time_inc(&stats->read_waittime, waittime); | |
5646 | else | |
5647 | lock_time_inc(&stats->write_waittime, waittime); | |
5648 | } | |
5649 | if (lock->cpu != cpu) | |
5650 | stats->bounces[bounce_acquired + !!hlock->read]++; | |
96645678 PZ |
5651 | |
5652 | lock->cpu = cpu; | |
c7e78cff | 5653 | lock->ip = ip; |
f20786ff PZ |
5654 | } |
5655 | ||
5656 | void lock_contended(struct lockdep_map *lock, unsigned long ip) | |
5657 | { | |
5658 | unsigned long flags; | |
5659 | ||
eb1f0023 PZ |
5660 | trace_lock_acquired(lock, ip); |
5661 | ||
4d004099 | 5662 | if (unlikely(!lock_stat || !lockdep_enabled())) |
f20786ff PZ |
5663 | return; |
5664 | ||
5665 | raw_local_irq_save(flags); | |
5666 | check_flags(flags); | |
4d004099 | 5667 | lockdep_recursion_inc(); |
f20786ff | 5668 | __lock_contended(lock, ip); |
10476e63 | 5669 | lockdep_recursion_finish(); |
f20786ff PZ |
5670 | raw_local_irq_restore(flags); |
5671 | } | |
5672 | EXPORT_SYMBOL_GPL(lock_contended); | |
5673 | ||
c7e78cff | 5674 | void lock_acquired(struct lockdep_map *lock, unsigned long ip) |
f20786ff PZ |
5675 | { |
5676 | unsigned long flags; | |
5677 | ||
eb1f0023 PZ |
5678 | trace_lock_contended(lock, ip); |
5679 | ||
4d004099 | 5680 | if (unlikely(!lock_stat || !lockdep_enabled())) |
f20786ff PZ |
5681 | return; |
5682 | ||
5683 | raw_local_irq_save(flags); | |
5684 | check_flags(flags); | |
4d004099 | 5685 | lockdep_recursion_inc(); |
c7e78cff | 5686 | __lock_acquired(lock, ip); |
10476e63 | 5687 | lockdep_recursion_finish(); |
f20786ff PZ |
5688 | raw_local_irq_restore(flags); |
5689 | } | |
5690 | EXPORT_SYMBOL_GPL(lock_acquired); | |
5691 | #endif | |
5692 | ||
fbb9ce95 IM |
5693 | /* |
5694 | * Used by the testsuite, sanitize the validator state | |
5695 | * after a simulated failure: | |
5696 | */ | |
5697 | ||
5698 | void lockdep_reset(void) | |
5699 | { | |
5700 | unsigned long flags; | |
23d95a03 | 5701 | int i; |
fbb9ce95 IM |
5702 | |
5703 | raw_local_irq_save(flags); | |
e196e479 | 5704 | lockdep_init_task(current); |
fbb9ce95 IM |
5705 | memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock)); |
5706 | nr_hardirq_chains = 0; | |
5707 | nr_softirq_chains = 0; | |
5708 | nr_process_chains = 0; | |
5709 | debug_locks = 1; | |
23d95a03 | 5710 | for (i = 0; i < CHAINHASH_SIZE; i++) |
a63f38cc | 5711 | INIT_HLIST_HEAD(chainhash_table + i); |
fbb9ce95 IM |
5712 | raw_local_irq_restore(flags); |
5713 | } | |
5714 | ||
a0b0fd53 | 5715 | /* Remove a class from a lock chain. Must be called with the graph lock held. */ |
de4643a7 BVA |
5716 | static void remove_class_from_lock_chain(struct pending_free *pf, |
5717 | struct lock_chain *chain, | |
a0b0fd53 BVA |
5718 | struct lock_class *class) |
5719 | { | |
5720 | #ifdef CONFIG_PROVE_LOCKING | |
a0b0fd53 BVA |
5721 | int i; |
5722 | ||
5723 | for (i = chain->base; i < chain->base + chain->depth; i++) { | |
f611e8cf | 5724 | if (chain_hlock_class_idx(chain_hlocks[i]) != class - lock_classes) |
a0b0fd53 | 5725 | continue; |
a0b0fd53 BVA |
5726 | /* |
5727 | * Each lock class occurs at most once in a lock chain so once | |
5728 | * we found a match we can break out of this loop. | |
5729 | */ | |
836bd74b | 5730 | goto free_lock_chain; |
a0b0fd53 BVA |
5731 | } |
5732 | /* Since the chain has not been modified, return. */ | |
5733 | return; | |
5734 | ||
836bd74b | 5735 | free_lock_chain: |
810507fe | 5736 | free_chain_hlocks(chain->base, chain->depth); |
a0b0fd53 | 5737 | /* Overwrite the chain key for concurrent RCU readers. */ |
836bd74b | 5738 | WRITE_ONCE(chain->chain_key, INITIAL_CHAIN_KEY); |
b3b9c187 WL |
5739 | dec_chains(chain->irq_context); |
5740 | ||
a0b0fd53 BVA |
5741 | /* |
5742 | * Note: calling hlist_del_rcu() from inside a | |
5743 | * hlist_for_each_entry_rcu() loop is safe. | |
5744 | */ | |
5745 | hlist_del_rcu(&chain->entry); | |
de4643a7 | 5746 | __set_bit(chain - lock_chains, pf->lock_chains_being_freed); |
797b82eb | 5747 | nr_zapped_lock_chains++; |
a0b0fd53 BVA |
5748 | #endif |
5749 | } | |
5750 | ||
5751 | /* Must be called with the graph lock held. */ | |
de4643a7 BVA |
5752 | static void remove_class_from_lock_chains(struct pending_free *pf, |
5753 | struct lock_class *class) | |
a0b0fd53 BVA |
5754 | { |
5755 | struct lock_chain *chain; | |
5756 | struct hlist_head *head; | |
5757 | int i; | |
5758 | ||
5759 | for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) { | |
5760 | head = chainhash_table + i; | |
5761 | hlist_for_each_entry_rcu(chain, head, entry) { | |
de4643a7 | 5762 | remove_class_from_lock_chain(pf, chain, class); |
a0b0fd53 BVA |
5763 | } |
5764 | } | |
5765 | } | |
5766 | ||
786fa29e BVA |
5767 | /* |
5768 | * Remove all references to a lock class. The caller must hold the graph lock. | |
5769 | */ | |
a0b0fd53 | 5770 | static void zap_class(struct pending_free *pf, struct lock_class *class) |
fbb9ce95 | 5771 | { |
86cffb80 | 5772 | struct lock_list *entry; |
fbb9ce95 IM |
5773 | int i; |
5774 | ||
a0b0fd53 BVA |
5775 | WARN_ON_ONCE(!class->key); |
5776 | ||
fbb9ce95 IM |
5777 | /* |
5778 | * Remove all dependencies this lock is | |
5779 | * involved in: | |
5780 | */ | |
ace35a7a BVA |
5781 | for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) { |
5782 | entry = list_entries + i; | |
86cffb80 BVA |
5783 | if (entry->class != class && entry->links_to != class) |
5784 | continue; | |
ace35a7a BVA |
5785 | __clear_bit(i, list_entries_in_use); |
5786 | nr_list_entries--; | |
86cffb80 | 5787 | list_del_rcu(&entry->entry); |
fbb9ce95 | 5788 | } |
a0b0fd53 BVA |
5789 | if (list_empty(&class->locks_after) && |
5790 | list_empty(&class->locks_before)) { | |
5791 | list_move_tail(&class->lock_entry, &pf->zapped); | |
5792 | hlist_del_rcu(&class->hash_entry); | |
5793 | WRITE_ONCE(class->key, NULL); | |
5794 | WRITE_ONCE(class->name, NULL); | |
5795 | nr_lock_classes--; | |
01bb6f0a | 5796 | __clear_bit(class - lock_classes, lock_classes_in_use); |
a0b0fd53 BVA |
5797 | } else { |
5798 | WARN_ONCE(true, "%s() failed for class %s\n", __func__, | |
5799 | class->name); | |
5800 | } | |
fbb9ce95 | 5801 | |
de4643a7 | 5802 | remove_class_from_lock_chains(pf, class); |
1d44bcb4 | 5803 | nr_zapped_classes++; |
a0b0fd53 BVA |
5804 | } |
5805 | ||
5806 | static void reinit_class(struct lock_class *class) | |
5807 | { | |
5808 | void *const p = class; | |
5809 | const unsigned int offset = offsetof(struct lock_class, key); | |
5810 | ||
5811 | WARN_ON_ONCE(!class->lock_entry.next); | |
5812 | WARN_ON_ONCE(!list_empty(&class->locks_after)); | |
5813 | WARN_ON_ONCE(!list_empty(&class->locks_before)); | |
5814 | memset(p + offset, 0, sizeof(*class) - offset); | |
5815 | WARN_ON_ONCE(!class->lock_entry.next); | |
5816 | WARN_ON_ONCE(!list_empty(&class->locks_after)); | |
5817 | WARN_ON_ONCE(!list_empty(&class->locks_before)); | |
fbb9ce95 IM |
5818 | } |
5819 | ||
fabe874a | 5820 | static inline int within(const void *addr, void *start, unsigned long size) |
fbb9ce95 IM |
5821 | { |
5822 | return addr >= start && addr < start + size; | |
5823 | } | |
5824 | ||
a0b0fd53 BVA |
5825 | static bool inside_selftest(void) |
5826 | { | |
5827 | return current == lockdep_selftest_task_struct; | |
5828 | } | |
5829 | ||
5830 | /* The caller must hold the graph lock. */ | |
5831 | static struct pending_free *get_pending_free(void) | |
5832 | { | |
5833 | return delayed_free.pf + delayed_free.index; | |
5834 | } | |
5835 | ||
5836 | static void free_zapped_rcu(struct rcu_head *cb); | |
5837 | ||
5838 | /* | |
5839 | * Schedule an RCU callback if no RCU callback is pending. Must be called with | |
5840 | * the graph lock held. | |
5841 | */ | |
5842 | static void call_rcu_zapped(struct pending_free *pf) | |
5843 | { | |
5844 | WARN_ON_ONCE(inside_selftest()); | |
5845 | ||
5846 | if (list_empty(&pf->zapped)) | |
5847 | return; | |
5848 | ||
5849 | if (delayed_free.scheduled) | |
5850 | return; | |
5851 | ||
5852 | delayed_free.scheduled = true; | |
5853 | ||
5854 | WARN_ON_ONCE(delayed_free.pf + delayed_free.index != pf); | |
5855 | delayed_free.index ^= 1; | |
5856 | ||
5857 | call_rcu(&delayed_free.rcu_head, free_zapped_rcu); | |
5858 | } | |
5859 | ||
5860 | /* The caller must hold the graph lock. May be called from RCU context. */ | |
5861 | static void __free_zapped_classes(struct pending_free *pf) | |
5862 | { | |
5863 | struct lock_class *class; | |
5864 | ||
72dcd505 | 5865 | check_data_structures(); |
b526b2e3 | 5866 | |
a0b0fd53 BVA |
5867 | list_for_each_entry(class, &pf->zapped, lock_entry) |
5868 | reinit_class(class); | |
5869 | ||
5870 | list_splice_init(&pf->zapped, &free_lock_classes); | |
de4643a7 BVA |
5871 | |
5872 | #ifdef CONFIG_PROVE_LOCKING | |
5873 | bitmap_andnot(lock_chains_in_use, lock_chains_in_use, | |
5874 | pf->lock_chains_being_freed, ARRAY_SIZE(lock_chains)); | |
5875 | bitmap_clear(pf->lock_chains_being_freed, 0, ARRAY_SIZE(lock_chains)); | |
5876 | #endif | |
a0b0fd53 BVA |
5877 | } |
5878 | ||
5879 | static void free_zapped_rcu(struct rcu_head *ch) | |
5880 | { | |
5881 | struct pending_free *pf; | |
5882 | unsigned long flags; | |
5883 | ||
5884 | if (WARN_ON_ONCE(ch != &delayed_free.rcu_head)) | |
5885 | return; | |
5886 | ||
5887 | raw_local_irq_save(flags); | |
248efb21 | 5888 | lockdep_lock(); |
a0b0fd53 BVA |
5889 | |
5890 | /* closed head */ | |
5891 | pf = delayed_free.pf + (delayed_free.index ^ 1); | |
5892 | __free_zapped_classes(pf); | |
5893 | delayed_free.scheduled = false; | |
5894 | ||
5895 | /* | |
5896 | * If there's anything on the open list, close and start a new callback. | |
5897 | */ | |
5898 | call_rcu_zapped(delayed_free.pf + delayed_free.index); | |
5899 | ||
248efb21 | 5900 | lockdep_unlock(); |
a0b0fd53 BVA |
5901 | raw_local_irq_restore(flags); |
5902 | } | |
5903 | ||
5904 | /* | |
5905 | * Remove all lock classes from the class hash table and from the | |
5906 | * all_lock_classes list whose key or name is in the address range [start, | |
5907 | * start + size). Move these lock classes to the zapped_classes list. Must | |
5908 | * be called with the graph lock held. | |
5909 | */ | |
5910 | static void __lockdep_free_key_range(struct pending_free *pf, void *start, | |
5911 | unsigned long size) | |
956f3563 BVA |
5912 | { |
5913 | struct lock_class *class; | |
5914 | struct hlist_head *head; | |
5915 | int i; | |
5916 | ||
5917 | /* Unhash all classes that were created by a module. */ | |
5918 | for (i = 0; i < CLASSHASH_SIZE; i++) { | |
5919 | head = classhash_table + i; | |
5920 | hlist_for_each_entry_rcu(class, head, hash_entry) { | |
5921 | if (!within(class->key, start, size) && | |
5922 | !within(class->name, start, size)) | |
5923 | continue; | |
a0b0fd53 | 5924 | zap_class(pf, class); |
956f3563 BVA |
5925 | } |
5926 | } | |
5927 | } | |
5928 | ||
35a9393c PZ |
5929 | /* |
5930 | * Used in module.c to remove lock classes from memory that is going to be | |
5931 | * freed; and possibly re-used by other modules. | |
5932 | * | |
29fc33fb BVA |
5933 | * We will have had one synchronize_rcu() before getting here, so we're |
5934 | * guaranteed nobody will look up these exact classes -- they're properly dead | |
5935 | * but still allocated. | |
35a9393c | 5936 | */ |
a0b0fd53 | 5937 | static void lockdep_free_key_range_reg(void *start, unsigned long size) |
fbb9ce95 | 5938 | { |
a0b0fd53 | 5939 | struct pending_free *pf; |
fbb9ce95 | 5940 | unsigned long flags; |
fbb9ce95 | 5941 | |
feb0a386 BVA |
5942 | init_data_structures_once(); |
5943 | ||
fbb9ce95 | 5944 | raw_local_irq_save(flags); |
248efb21 | 5945 | lockdep_lock(); |
a0b0fd53 BVA |
5946 | pf = get_pending_free(); |
5947 | __lockdep_free_key_range(pf, start, size); | |
5948 | call_rcu_zapped(pf); | |
248efb21 | 5949 | lockdep_unlock(); |
fbb9ce95 | 5950 | raw_local_irq_restore(flags); |
35a9393c PZ |
5951 | |
5952 | /* | |
5953 | * Wait for any possible iterators from look_up_lock_class() to pass | |
5954 | * before continuing to free the memory they refer to. | |
35a9393c | 5955 | */ |
51959d85 | 5956 | synchronize_rcu(); |
a0b0fd53 | 5957 | } |
35a9393c | 5958 | |
a0b0fd53 BVA |
5959 | /* |
5960 | * Free all lockdep keys in the range [start, start+size). Does not sleep. | |
5961 | * Ignores debug_locks. Must only be used by the lockdep selftests. | |
5962 | */ | |
5963 | static void lockdep_free_key_range_imm(void *start, unsigned long size) | |
5964 | { | |
5965 | struct pending_free *pf = delayed_free.pf; | |
5966 | unsigned long flags; | |
5967 | ||
5968 | init_data_structures_once(); | |
5969 | ||
5970 | raw_local_irq_save(flags); | |
248efb21 | 5971 | lockdep_lock(); |
a0b0fd53 BVA |
5972 | __lockdep_free_key_range(pf, start, size); |
5973 | __free_zapped_classes(pf); | |
248efb21 | 5974 | lockdep_unlock(); |
a0b0fd53 BVA |
5975 | raw_local_irq_restore(flags); |
5976 | } | |
5977 | ||
5978 | void lockdep_free_key_range(void *start, unsigned long size) | |
5979 | { | |
5980 | init_data_structures_once(); | |
5981 | ||
5982 | if (inside_selftest()) | |
5983 | lockdep_free_key_range_imm(start, size); | |
5984 | else | |
5985 | lockdep_free_key_range_reg(start, size); | |
fbb9ce95 IM |
5986 | } |
5987 | ||
2904d9fa BVA |
5988 | /* |
5989 | * Check whether any element of the @lock->class_cache[] array refers to a | |
5990 | * registered lock class. The caller must hold either the graph lock or the | |
5991 | * RCU read lock. | |
5992 | */ | |
5993 | static bool lock_class_cache_is_registered(struct lockdep_map *lock) | |
fbb9ce95 | 5994 | { |
35a9393c | 5995 | struct lock_class *class; |
a63f38cc | 5996 | struct hlist_head *head; |
fbb9ce95 | 5997 | int i, j; |
2904d9fa BVA |
5998 | |
5999 | for (i = 0; i < CLASSHASH_SIZE; i++) { | |
6000 | head = classhash_table + i; | |
6001 | hlist_for_each_entry_rcu(class, head, hash_entry) { | |
6002 | for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++) | |
6003 | if (lock->class_cache[j] == class) | |
6004 | return true; | |
6005 | } | |
6006 | } | |
6007 | return false; | |
6008 | } | |
6009 | ||
956f3563 | 6010 | /* The caller must hold the graph lock. Does not sleep. */ |
a0b0fd53 BVA |
6011 | static void __lockdep_reset_lock(struct pending_free *pf, |
6012 | struct lockdep_map *lock) | |
2904d9fa BVA |
6013 | { |
6014 | struct lock_class *class; | |
956f3563 | 6015 | int j; |
fbb9ce95 IM |
6016 | |
6017 | /* | |
d6d897ce IM |
6018 | * Remove all classes this lock might have: |
6019 | */ | |
6020 | for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) { | |
6021 | /* | |
6022 | * If the class exists we look it up and zap it: | |
6023 | */ | |
6024 | class = look_up_lock_class(lock, j); | |
64f29d1b | 6025 | if (class) |
a0b0fd53 | 6026 | zap_class(pf, class); |
d6d897ce IM |
6027 | } |
6028 | /* | |
6029 | * Debug check: in the end all mapped classes should | |
6030 | * be gone. | |
fbb9ce95 | 6031 | */ |
956f3563 BVA |
6032 | if (WARN_ON_ONCE(lock_class_cache_is_registered(lock))) |
6033 | debug_locks_off(); | |
6034 | } | |
6035 | ||
a0b0fd53 BVA |
6036 | /* |
6037 | * Remove all information lockdep has about a lock if debug_locks == 1. Free | |
6038 | * released data structures from RCU context. | |
6039 | */ | |
6040 | static void lockdep_reset_lock_reg(struct lockdep_map *lock) | |
956f3563 | 6041 | { |
a0b0fd53 | 6042 | struct pending_free *pf; |
956f3563 BVA |
6043 | unsigned long flags; |
6044 | int locked; | |
6045 | ||
956f3563 BVA |
6046 | raw_local_irq_save(flags); |
6047 | locked = graph_lock(); | |
a0b0fd53 BVA |
6048 | if (!locked) |
6049 | goto out_irq; | |
6050 | ||
6051 | pf = get_pending_free(); | |
6052 | __lockdep_reset_lock(pf, lock); | |
6053 | call_rcu_zapped(pf); | |
6054 | ||
6055 | graph_unlock(); | |
6056 | out_irq: | |
6057 | raw_local_irq_restore(flags); | |
6058 | } | |
6059 | ||
6060 | /* | |
6061 | * Reset a lock. Does not sleep. Ignores debug_locks. Must only be used by the | |
6062 | * lockdep selftests. | |
6063 | */ | |
6064 | static void lockdep_reset_lock_imm(struct lockdep_map *lock) | |
6065 | { | |
6066 | struct pending_free *pf = delayed_free.pf; | |
6067 | unsigned long flags; | |
6068 | ||
6069 | raw_local_irq_save(flags); | |
248efb21 | 6070 | lockdep_lock(); |
a0b0fd53 BVA |
6071 | __lockdep_reset_lock(pf, lock); |
6072 | __free_zapped_classes(pf); | |
248efb21 | 6073 | lockdep_unlock(); |
fbb9ce95 IM |
6074 | raw_local_irq_restore(flags); |
6075 | } | |
6076 | ||
a0b0fd53 BVA |
6077 | void lockdep_reset_lock(struct lockdep_map *lock) |
6078 | { | |
6079 | init_data_structures_once(); | |
6080 | ||
6081 | if (inside_selftest()) | |
6082 | lockdep_reset_lock_imm(lock); | |
6083 | else | |
6084 | lockdep_reset_lock_reg(lock); | |
6085 | } | |
6086 | ||
108c1485 BVA |
6087 | /* Unregister a dynamically allocated key. */ |
6088 | void lockdep_unregister_key(struct lock_class_key *key) | |
6089 | { | |
6090 | struct hlist_head *hash_head = keyhashentry(key); | |
6091 | struct lock_class_key *k; | |
6092 | struct pending_free *pf; | |
6093 | unsigned long flags; | |
6094 | bool found = false; | |
6095 | ||
6096 | might_sleep(); | |
6097 | ||
6098 | if (WARN_ON_ONCE(static_obj(key))) | |
6099 | return; | |
6100 | ||
6101 | raw_local_irq_save(flags); | |
8b39adbe BVA |
6102 | if (!graph_lock()) |
6103 | goto out_irq; | |
6104 | ||
108c1485 BVA |
6105 | pf = get_pending_free(); |
6106 | hlist_for_each_entry_rcu(k, hash_head, hash_entry) { | |
6107 | if (k == key) { | |
6108 | hlist_del_rcu(&k->hash_entry); | |
6109 | found = true; | |
6110 | break; | |
6111 | } | |
6112 | } | |
6113 | WARN_ON_ONCE(!found); | |
6114 | __lockdep_free_key_range(pf, key, 1); | |
6115 | call_rcu_zapped(pf); | |
8b39adbe BVA |
6116 | graph_unlock(); |
6117 | out_irq: | |
108c1485 BVA |
6118 | raw_local_irq_restore(flags); |
6119 | ||
6120 | /* Wait until is_dynamic_key() has finished accessing k->hash_entry. */ | |
6121 | synchronize_rcu(); | |
6122 | } | |
6123 | EXPORT_SYMBOL_GPL(lockdep_unregister_key); | |
6124 | ||
c3bc8fd6 | 6125 | void __init lockdep_init(void) |
fbb9ce95 IM |
6126 | { |
6127 | printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n"); | |
6128 | ||
b0788caf | 6129 | printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES); |
fbb9ce95 IM |
6130 | printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH); |
6131 | printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS); | |
b0788caf | 6132 | printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE); |
fbb9ce95 IM |
6133 | printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES); |
6134 | printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS); | |
6135 | printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE); | |
6136 | ||
09d75ecb | 6137 | printk(" memory used by lock dependency info: %zu kB\n", |
7ff8517e | 6138 | (sizeof(lock_classes) + |
01bb6f0a | 6139 | sizeof(lock_classes_in_use) + |
7ff8517e BVA |
6140 | sizeof(classhash_table) + |
6141 | sizeof(list_entries) + | |
ace35a7a | 6142 | sizeof(list_entries_in_use) + |
a0b0fd53 BVA |
6143 | sizeof(chainhash_table) + |
6144 | sizeof(delayed_free) | |
4dd861d6 | 6145 | #ifdef CONFIG_PROVE_LOCKING |
7ff8517e | 6146 | + sizeof(lock_cq) |
15ea86b5 | 6147 | + sizeof(lock_chains) |
de4643a7 | 6148 | + sizeof(lock_chains_in_use) |
15ea86b5 | 6149 | + sizeof(chain_hlocks) |
4dd861d6 | 6150 | #endif |
90629209 | 6151 | ) / 1024 |
4dd861d6 | 6152 | ); |
fbb9ce95 | 6153 | |
12593b74 BVA |
6154 | #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) |
6155 | printk(" memory used for stack traces: %zu kB\n", | |
6156 | (sizeof(stack_trace) + sizeof(stack_trace_hash)) / 1024 | |
6157 | ); | |
6158 | #endif | |
6159 | ||
09d75ecb | 6160 | printk(" per task-struct memory footprint: %zu bytes\n", |
7ff8517e | 6161 | sizeof(((struct task_struct *)NULL)->held_locks)); |
fbb9ce95 IM |
6162 | } |
6163 | ||
fbb9ce95 IM |
6164 | static void |
6165 | print_freed_lock_bug(struct task_struct *curr, const void *mem_from, | |
55794a41 | 6166 | const void *mem_to, struct held_lock *hlock) |
fbb9ce95 IM |
6167 | { |
6168 | if (!debug_locks_off()) | |
6169 | return; | |
6170 | if (debug_locks_silent) | |
6171 | return; | |
6172 | ||
681fbec8 | 6173 | pr_warn("\n"); |
a5dd63ef PM |
6174 | pr_warn("=========================\n"); |
6175 | pr_warn("WARNING: held lock freed!\n"); | |
fbdc4b9a | 6176 | print_kernel_ident(); |
a5dd63ef | 6177 | pr_warn("-------------------------\n"); |
04860d48 | 6178 | pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n", |
ba25f9dc | 6179 | curr->comm, task_pid_nr(curr), mem_from, mem_to-1); |
55794a41 | 6180 | print_lock(hlock); |
fbb9ce95 IM |
6181 | lockdep_print_held_locks(curr); |
6182 | ||
681fbec8 | 6183 | pr_warn("\nstack backtrace:\n"); |
fbb9ce95 IM |
6184 | dump_stack(); |
6185 | } | |
6186 | ||
54561783 ON |
6187 | static inline int not_in_range(const void* mem_from, unsigned long mem_len, |
6188 | const void* lock_from, unsigned long lock_len) | |
6189 | { | |
6190 | return lock_from + lock_len <= mem_from || | |
6191 | mem_from + mem_len <= lock_from; | |
6192 | } | |
6193 | ||
fbb9ce95 IM |
6194 | /* |
6195 | * Called when kernel memory is freed (or unmapped), or if a lock | |
6196 | * is destroyed or reinitialized - this code checks whether there is | |
6197 | * any held lock in the memory range of <from> to <to>: | |
6198 | */ | |
6199 | void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len) | |
6200 | { | |
fbb9ce95 IM |
6201 | struct task_struct *curr = current; |
6202 | struct held_lock *hlock; | |
6203 | unsigned long flags; | |
6204 | int i; | |
6205 | ||
6206 | if (unlikely(!debug_locks)) | |
6207 | return; | |
6208 | ||
fcc784be | 6209 | raw_local_irq_save(flags); |
fbb9ce95 IM |
6210 | for (i = 0; i < curr->lockdep_depth; i++) { |
6211 | hlock = curr->held_locks + i; | |
6212 | ||
54561783 ON |
6213 | if (not_in_range(mem_from, mem_len, hlock->instance, |
6214 | sizeof(*hlock->instance))) | |
fbb9ce95 IM |
6215 | continue; |
6216 | ||
54561783 | 6217 | print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock); |
fbb9ce95 IM |
6218 | break; |
6219 | } | |
fcc784be | 6220 | raw_local_irq_restore(flags); |
fbb9ce95 | 6221 | } |
ed07536e | 6222 | EXPORT_SYMBOL_GPL(debug_check_no_locks_freed); |
fbb9ce95 | 6223 | |
1b1d2fb4 | 6224 | static void print_held_locks_bug(void) |
fbb9ce95 IM |
6225 | { |
6226 | if (!debug_locks_off()) | |
6227 | return; | |
6228 | if (debug_locks_silent) | |
6229 | return; | |
6230 | ||
681fbec8 | 6231 | pr_warn("\n"); |
a5dd63ef PM |
6232 | pr_warn("====================================\n"); |
6233 | pr_warn("WARNING: %s/%d still has locks held!\n", | |
1b1d2fb4 | 6234 | current->comm, task_pid_nr(current)); |
fbdc4b9a | 6235 | print_kernel_ident(); |
a5dd63ef | 6236 | pr_warn("------------------------------------\n"); |
1b1d2fb4 | 6237 | lockdep_print_held_locks(current); |
681fbec8 | 6238 | pr_warn("\nstack backtrace:\n"); |
fbb9ce95 IM |
6239 | dump_stack(); |
6240 | } | |
6241 | ||
1b1d2fb4 | 6242 | void debug_check_no_locks_held(void) |
fbb9ce95 | 6243 | { |
1b1d2fb4 CC |
6244 | if (unlikely(current->lockdep_depth > 0)) |
6245 | print_held_locks_bug(); | |
fbb9ce95 | 6246 | } |
1b1d2fb4 | 6247 | EXPORT_SYMBOL_GPL(debug_check_no_locks_held); |
fbb9ce95 | 6248 | |
8dce7a9a | 6249 | #ifdef __KERNEL__ |
fbb9ce95 IM |
6250 | void debug_show_all_locks(void) |
6251 | { | |
6252 | struct task_struct *g, *p; | |
fbb9ce95 | 6253 | |
9c35dd7f | 6254 | if (unlikely(!debug_locks)) { |
681fbec8 | 6255 | pr_warn("INFO: lockdep is turned off.\n"); |
9c35dd7f JP |
6256 | return; |
6257 | } | |
681fbec8 | 6258 | pr_warn("\nShowing all locks held in the system:\n"); |
fbb9ce95 | 6259 | |
0f736a52 TH |
6260 | rcu_read_lock(); |
6261 | for_each_process_thread(g, p) { | |
0f736a52 TH |
6262 | if (!p->lockdep_depth) |
6263 | continue; | |
6264 | lockdep_print_held_locks(p); | |
88f1c87d | 6265 | touch_nmi_watchdog(); |
0f736a52 TH |
6266 | touch_all_softlockup_watchdogs(); |
6267 | } | |
6268 | rcu_read_unlock(); | |
fbb9ce95 | 6269 | |
681fbec8 | 6270 | pr_warn("\n"); |
a5dd63ef | 6271 | pr_warn("=============================================\n\n"); |
fbb9ce95 | 6272 | } |
fbb9ce95 | 6273 | EXPORT_SYMBOL_GPL(debug_show_all_locks); |
8dce7a9a | 6274 | #endif |
fbb9ce95 | 6275 | |
82a1fcb9 IM |
6276 | /* |
6277 | * Careful: only use this function if you are sure that | |
6278 | * the task cannot run in parallel! | |
6279 | */ | |
f1b499f0 | 6280 | void debug_show_held_locks(struct task_struct *task) |
fbb9ce95 | 6281 | { |
9c35dd7f JP |
6282 | if (unlikely(!debug_locks)) { |
6283 | printk("INFO: lockdep is turned off.\n"); | |
6284 | return; | |
6285 | } | |
fbb9ce95 IM |
6286 | lockdep_print_held_locks(task); |
6287 | } | |
fbb9ce95 | 6288 | EXPORT_SYMBOL_GPL(debug_show_held_locks); |
b351d164 | 6289 | |
722a9f92 | 6290 | asmlinkage __visible void lockdep_sys_exit(void) |
b351d164 PZ |
6291 | { |
6292 | struct task_struct *curr = current; | |
6293 | ||
6294 | if (unlikely(curr->lockdep_depth)) { | |
6295 | if (!debug_locks_off()) | |
6296 | return; | |
681fbec8 | 6297 | pr_warn("\n"); |
a5dd63ef PM |
6298 | pr_warn("================================================\n"); |
6299 | pr_warn("WARNING: lock held when returning to user space!\n"); | |
fbdc4b9a | 6300 | print_kernel_ident(); |
a5dd63ef | 6301 | pr_warn("------------------------------------------------\n"); |
681fbec8 | 6302 | pr_warn("%s/%d is leaving the kernel with locks still held!\n", |
b351d164 PZ |
6303 | curr->comm, curr->pid); |
6304 | lockdep_print_held_locks(curr); | |
6305 | } | |
b09be676 BP |
6306 | |
6307 | /* | |
6308 | * The lock history for each syscall should be independent. So wipe the | |
6309 | * slate clean on return to userspace. | |
6310 | */ | |
f52be570 | 6311 | lockdep_invariant_state(false); |
b351d164 | 6312 | } |
0632eb3d | 6313 | |
b3fbab05 | 6314 | void lockdep_rcu_suspicious(const char *file, const int line, const char *s) |
0632eb3d PM |
6315 | { |
6316 | struct task_struct *curr = current; | |
6317 | ||
2b3fc35f | 6318 | /* Note: the following can be executed concurrently, so be careful. */ |
681fbec8 | 6319 | pr_warn("\n"); |
a5dd63ef PM |
6320 | pr_warn("=============================\n"); |
6321 | pr_warn("WARNING: suspicious RCU usage\n"); | |
fbdc4b9a | 6322 | print_kernel_ident(); |
a5dd63ef | 6323 | pr_warn("-----------------------------\n"); |
681fbec8 PM |
6324 | pr_warn("%s:%d %s!\n", file, line, s); |
6325 | pr_warn("\nother info that might help us debug this:\n\n"); | |
6326 | pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n", | |
c5fdcec9 PM |
6327 | !rcu_lockdep_current_cpu_online() |
6328 | ? "RCU used illegally from offline CPU!\n" | |
d29e0b26 | 6329 | : "", |
c5fdcec9 | 6330 | rcu_scheduler_active, debug_locks); |
0464e937 FW |
6331 | |
6332 | /* | |
6333 | * If a CPU is in the RCU-free window in idle (ie: in the section | |
6334 | * between rcu_idle_enter() and rcu_idle_exit(), then RCU | |
6335 | * considers that CPU to be in an "extended quiescent state", | |
6336 | * which means that RCU will be completely ignoring that CPU. | |
6337 | * Therefore, rcu_read_lock() and friends have absolutely no | |
6338 | * effect on a CPU running in that state. In other words, even if | |
6339 | * such an RCU-idle CPU has called rcu_read_lock(), RCU might well | |
6340 | * delete data structures out from under it. RCU really has no | |
6341 | * choice here: we need to keep an RCU-free window in idle where | |
6342 | * the CPU may possibly enter into low power mode. This way we can | |
6343 | * notice an extended quiescent state to other CPUs that started a grace | |
6344 | * period. Otherwise we would delay any grace period as long as we run | |
6345 | * in the idle task. | |
6346 | * | |
6347 | * So complain bitterly if someone does call rcu_read_lock(), | |
6348 | * rcu_read_lock_bh() and so on from extended quiescent states. | |
6349 | */ | |
5c173eb8 | 6350 | if (!rcu_is_watching()) |
681fbec8 | 6351 | pr_warn("RCU used illegally from extended quiescent state!\n"); |
0464e937 | 6352 | |
0632eb3d | 6353 | lockdep_print_held_locks(curr); |
681fbec8 | 6354 | pr_warn("\nstack backtrace:\n"); |
0632eb3d PM |
6355 | dump_stack(); |
6356 | } | |
b3fbab05 | 6357 | EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious); |