blob: c7710e4092efb3d927a71058bea91cf5c6d48e1a [file] [log] [blame]
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001/*
2 * kernel/lockdep.c
3 *
4 * Runtime locking correctness validator
5 *
6 * Started by Ingo Molnar:
7 *
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -07008 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
Peter Zijlstra90eec102015-11-16 11:08:45 +01009 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070010 *
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
13 *
14 * - lock inversion scenarios
15 * - circular lock dependencies
16 * - hardirq/softirq safe/unsafe locking bugs
17 *
18 * Bugs are reported even if the current locking scenario does not cause
19 * any deadlock at this point.
20 *
21 * I.e. if anytime in the past two locks were taken in a different order,
22 * even if it happened for another task, even if those were different
23 * locks (but of the same class as this lock), this code will detect it.
24 *
25 * Thanks to Arjan van de Ven for coming up with the initial idea of
26 * mapping lock dependencies runtime.
27 */
Steven Rostedta5e25882008-12-02 15:34:05 -050028#define DISABLE_BRANCH_PROFILING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070029#include <linux/mutex.h>
30#include <linux/sched.h>
31#include <linux/delay.h>
32#include <linux/module.h>
33#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
35#include <linux/spinlock.h>
36#include <linux/kallsyms.h>
37#include <linux/interrupt.h>
38#include <linux/stacktrace.h>
39#include <linux/debug_locks.h>
40#include <linux/irqflags.h>
Dave Jones99de0552006-09-29 02:00:10 -070041#include <linux/utsname.h>
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -070042#include <linux/hash.h>
Steven Rostedt81d68a92008-05-12 21:20:42 +020043#include <linux/ftrace.h>
Peter Zijlstrab4b136f2009-01-29 14:50:36 +010044#include <linux/stringify.h>
Ming Leid588e462009-07-16 15:44:29 +020045#include <linux/bitops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/gfp.h>
Yong Zhangd3d03d42011-11-09 16:04:51 +080047#include <linux/kmemcheck.h>
Peter Zijlstraaf012962009-07-16 15:44:29 +020048
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070049#include <asm/sections.h>
50
51#include "lockdep_internals.h"
52
Steven Rostedta8d154b2009-04-10 09:36:00 -040053#define CREATE_TRACE_POINTS
Frederic Weisbecker67178762009-11-13 10:06:34 +010054#include <trace/events/lock.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040055
Peter Zijlstraf20786f2007-07-19 01:48:56 -070056#ifdef CONFIG_PROVE_LOCKING
57int prove_locking = 1;
58module_param(prove_locking, int, 0644);
59#else
60#define prove_locking 0
61#endif
62
63#ifdef CONFIG_LOCK_STAT
64int lock_stat = 1;
65module_param(lock_stat, int, 0644);
66#else
67#define lock_stat 0
68#endif
69
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070070/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080071 * lockdep_lock: protects the lockdep graph, the hashes and the
72 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070073 *
74 * This is one of the rare exceptions where it's justified
75 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080076 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070077 */
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010078static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Ingo Molnar74c383f2006-12-13 00:34:43 -080079
80static int graph_lock(void)
81{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010082 arch_spin_lock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080083 /*
84 * Make sure that if another CPU detected a bug while
85 * walking the graph we dont change it (while the other
86 * CPU is busy printing out stuff with the graph lock
87 * dropped already)
88 */
89 if (!debug_locks) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010090 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080091 return 0;
92 }
Steven Rostedtbb065af2008-05-12 21:21:00 +020093 /* prevent any recursions within lockdep from causing deadlocks */
94 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -080095 return 1;
96}
97
98static inline int graph_unlock(void)
99{
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200100 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
101 /*
102 * The lockdep graph lock isn't locked while we expect it to
103 * be, we're confused now, bye!
104 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800105 return DEBUG_LOCKS_WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200106 }
Jarek Poplawski381a2292007-02-10 01:44:58 -0800107
Steven Rostedtbb065af2008-05-12 21:21:00 +0200108 current->lockdep_recursion--;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100109 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800110 return 0;
111}
112
113/*
114 * Turn lock debugging off and return with 0 if it was off already,
115 * and also release the graph lock:
116 */
117static inline int debug_locks_off_graph_unlock(void)
118{
119 int ret = debug_locks_off();
120
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100121 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800122
123 return ret;
124}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700125
126static int lockdep_initialized;
127
128unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200129static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700130
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700131/*
132 * All data structures here are protected by the global debug_lock.
133 *
134 * Mutex key structs only get allocated, once during bootup, and never
135 * get freed - this significantly simplifies the debugging code.
136 */
137unsigned long nr_lock_classes;
138static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
139
Dave Jonesf82b2172008-08-11 09:30:23 +0200140static inline struct lock_class *hlock_class(struct held_lock *hlock)
141{
142 if (!hlock->class_idx) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200143 /*
144 * Someone passed in garbage, we give up.
145 */
Dave Jonesf82b2172008-08-11 09:30:23 +0200146 DEBUG_LOCKS_WARN_ON(1);
147 return NULL;
148 }
149 return lock_classes + hlock->class_idx - 1;
150}
151
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700152#ifdef CONFIG_LOCK_STAT
Tejun Heo1871e522009-10-29 22:34:13 +0900153static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS],
154 cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700155
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200156static inline u64 lockstat_clock(void)
157{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200158 return local_clock();
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200159}
160
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200161static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700162{
163 int i;
164
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200165 for (i = 0; i < LOCKSTAT_POINTS; i++) {
166 if (points[i] == 0) {
167 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700168 break;
169 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200170 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700171 break;
172 }
173
174 return i;
175}
176
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200177static void lock_time_inc(struct lock_time *lt, u64 time)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700178{
179 if (time > lt->max)
180 lt->max = time;
181
Frank Rowand109d71c2009-11-19 13:42:06 -0800182 if (time < lt->min || !lt->nr)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700183 lt->min = time;
184
185 lt->total += time;
186 lt->nr++;
187}
188
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700189static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
190{
Frank Rowand109d71c2009-11-19 13:42:06 -0800191 if (!src->nr)
192 return;
193
194 if (src->max > dst->max)
195 dst->max = src->max;
196
197 if (src->min < dst->min || !dst->nr)
198 dst->min = src->min;
199
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700200 dst->total += src->total;
201 dst->nr += src->nr;
202}
203
204struct lock_class_stats lock_stats(struct lock_class *class)
205{
206 struct lock_class_stats stats;
207 int cpu, i;
208
209 memset(&stats, 0, sizeof(struct lock_class_stats));
210 for_each_possible_cpu(cpu) {
211 struct lock_class_stats *pcs =
Tejun Heo1871e522009-10-29 22:34:13 +0900212 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700213
214 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
215 stats.contention_point[i] += pcs->contention_point[i];
216
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200217 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
218 stats.contending_point[i] += pcs->contending_point[i];
219
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700220 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
221 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
222
223 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
224 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700225
226 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
227 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700228 }
229
230 return stats;
231}
232
233void clear_lock_stats(struct lock_class *class)
234{
235 int cpu;
236
237 for_each_possible_cpu(cpu) {
238 struct lock_class_stats *cpu_stats =
Tejun Heo1871e522009-10-29 22:34:13 +0900239 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700240
241 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
242 }
243 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200244 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700245}
246
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700247static struct lock_class_stats *get_lock_stats(struct lock_class *class)
248{
Tejun Heo1871e522009-10-29 22:34:13 +0900249 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700250}
251
252static void put_lock_stats(struct lock_class_stats *stats)
253{
Tejun Heo1871e522009-10-29 22:34:13 +0900254 put_cpu_var(cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700255}
256
257static void lock_release_holdtime(struct held_lock *hlock)
258{
259 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200260 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700261
262 if (!lock_stat)
263 return;
264
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200265 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700266
Dave Jonesf82b2172008-08-11 09:30:23 +0200267 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700268 if (hlock->read)
269 lock_time_inc(&stats->read_holdtime, holdtime);
270 else
271 lock_time_inc(&stats->write_holdtime, holdtime);
272 put_lock_stats(stats);
273}
274#else
275static inline void lock_release_holdtime(struct held_lock *hlock)
276{
277}
278#endif
279
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700280/*
281 * We keep a global list of all lock classes. The list only grows,
282 * never shrinks. The list is only accessed with the lockdep
283 * spinlock lock held.
284 */
285LIST_HEAD(all_lock_classes);
286
287/*
288 * The lockdep classes are in a hash-table as well, for fast lookup:
289 */
290#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
291#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700292#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700293#define classhashentry(key) (classhash_table + __classhashfn((key)))
294
295static struct list_head classhash_table[CLASSHASH_SIZE];
296
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700297/*
298 * We put the lock dependency chains into a hash-table as well, to cache
299 * their existence:
300 */
301#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
302#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700303#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700304#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
305
306static struct list_head chainhash_table[CHAINHASH_SIZE];
307
308/*
309 * The hash key of the lock dependency chains is a hash itself too:
310 * it's a hash of all locks taken up to that lock, including that lock.
311 * It's a 64-bit hash, because it's important for the keys to be
312 * unique.
313 */
314#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700315 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
316 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700317 (key2))
318
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200319void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700320{
321 current->lockdep_recursion++;
322}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700323EXPORT_SYMBOL(lockdep_off);
324
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200325void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700326{
327 current->lockdep_recursion--;
328}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700329EXPORT_SYMBOL(lockdep_on);
330
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700331/*
332 * Debugging switches:
333 */
334
335#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800336#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700337
338#if VERBOSE
339# define HARDIRQ_VERBOSE 1
340# define SOFTIRQ_VERBOSE 1
Nick Piggincf40bd12009-01-21 08:12:39 +0100341# define RECLAIM_VERBOSE 1
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700342#else
343# define HARDIRQ_VERBOSE 0
344# define SOFTIRQ_VERBOSE 0
Nick Piggincf40bd12009-01-21 08:12:39 +0100345# define RECLAIM_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700346#endif
347
Nick Piggincf40bd12009-01-21 08:12:39 +0100348#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700349/*
350 * Quick filtering for interesting events:
351 */
352static int class_filter(struct lock_class *class)
353{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700354#if 0
355 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700356 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700357 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700358 return 1;
359 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700360 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700361 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700362#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800363 /* Filter everything else. 1 would be to allow everything else */
364 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700365}
366#endif
367
368static int verbose(struct lock_class *class)
369{
370#if VERBOSE
371 return class_filter(class);
372#endif
373 return 0;
374}
375
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700376/*
377 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800378 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700379 */
380unsigned long nr_stack_trace_entries;
381static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
382
Dave Jones2c522832013-04-25 13:40:02 -0400383static void print_lockdep_off(const char *bug_msg)
384{
385 printk(KERN_DEBUG "%s\n", bug_msg);
386 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200387#ifdef CONFIG_LOCK_STAT
Dave Jones2c522832013-04-25 13:40:02 -0400388 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200389#endif
Dave Jones2c522832013-04-25 13:40:02 -0400390}
391
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700392static int save_trace(struct stack_trace *trace)
393{
394 trace->nr_entries = 0;
395 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
396 trace->entries = stack_trace + nr_stack_trace_entries;
397
Andi Kleen5a1b3992006-09-26 10:52:34 +0200398 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200399
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700400 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700401
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200402 /*
403 * Some daft arches put -1 at the end to indicate its a full trace.
404 *
405 * <rant> this is buggy anyway, since it takes a whole extra entry so a
406 * complete trace that maxes out the entries provided will be reported
407 * as incomplete, friggin useless </rant>
408 */
Luck, Tonyea5b41f2009-12-09 14:29:36 -0800409 if (trace->nr_entries != 0 &&
410 trace->entries[trace->nr_entries-1] == ULONG_MAX)
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200411 trace->nr_entries--;
412
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700413 trace->max_entries = trace->nr_entries;
414
415 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700416
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200417 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800418 if (!debug_locks_off_graph_unlock())
419 return 0;
420
Dave Jones2c522832013-04-25 13:40:02 -0400421 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
Ingo Molnar74c383f2006-12-13 00:34:43 -0800422 dump_stack();
423
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700424 return 0;
425 }
426
427 return 1;
428}
429
430unsigned int nr_hardirq_chains;
431unsigned int nr_softirq_chains;
432unsigned int nr_process_chains;
433unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700434
435#ifdef CONFIG_DEBUG_LOCKDEP
436/*
437 * We cannot printk in early bootup code. Not even early_printk()
438 * might work. So we mark any initialization errors and printk
439 * about it later on, in lockdep_info().
440 */
441static int lockdep_init_error;
Ming Lei81140ac2011-11-17 13:34:32 +0800442static const char *lock_init_error;
Johannes Bergc71063c2007-07-19 01:49:02 -0700443static unsigned long lockdep_init_trace_data[20];
444static struct stack_trace lockdep_init_trace = {
445 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
446 .entries = lockdep_init_trace_data,
447};
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700448
449/*
450 * Various lockdep statistics:
451 */
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200452DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700453#endif
454
455/*
456 * Locking printouts:
457 */
458
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100459#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100460 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
461 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
462 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
463 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100464
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700465static const char *usage_str[] =
466{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100467#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
468#include "lockdep_states.h"
469#undef LOCKDEP_STATE
470 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700471};
472
473const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
474{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700475 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700476}
477
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100478static inline unsigned long lock_flag(enum lock_usage_bit bit)
479{
480 return 1UL << bit;
481}
482
483static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
484{
485 char c = '.';
486
487 if (class->usage_mask & lock_flag(bit + 2))
488 c = '+';
489 if (class->usage_mask & lock_flag(bit)) {
490 c = '-';
491 if (class->usage_mask & lock_flag(bit + 2))
492 c = '?';
493 }
494
495 return c;
496}
497
Peter Zijlstraf510b232009-01-22 17:53:47 +0100498void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700499{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100500 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700501
Peter Zijlstraf510b232009-01-22 17:53:47 +0100502#define LOCKDEP_STATE(__STATE) \
503 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
504 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
505#include "lockdep_states.h"
506#undef LOCKDEP_STATE
507
508 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700509}
510
Steven Rostedte5e78d02011-11-02 20:24:16 -0400511static void __print_lock_name(struct lock_class *class)
Steven Rostedt3003eba2011-04-20 21:41:54 -0400512{
513 char str[KSYM_NAME_LEN];
514 const char *name;
515
516 name = class->name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700517 if (!name) {
518 name = __get_key_name(class->key, str);
Steven Rostedte5e78d02011-11-02 20:24:16 -0400519 printk("%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700520 } else {
Steven Rostedte5e78d02011-11-02 20:24:16 -0400521 printk("%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700522 if (class->name_version > 1)
523 printk("#%d", class->name_version);
524 if (class->subclass)
525 printk("/%d", class->subclass);
526 }
Steven Rostedte5e78d02011-11-02 20:24:16 -0400527}
528
529static void print_lock_name(struct lock_class *class)
530{
531 char usage[LOCK_USAGE_CHARS];
532
533 get_usage_chars(class, usage);
534
535 printk(" (");
536 __print_lock_name(class);
Peter Zijlstraf510b232009-01-22 17:53:47 +0100537 printk("){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700538}
539
540static void print_lockdep_cache(struct lockdep_map *lock)
541{
542 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700543 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700544
545 name = lock->name;
546 if (!name)
547 name = __get_key_name(lock->key->subkeys, str);
548
549 printk("%s", name);
550}
551
552static void print_lock(struct held_lock *hlock)
553{
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200554 /*
555 * We can be called locklessly through debug_show_all_locks() so be
556 * extra careful, the hlock might have been released and cleared.
557 */
558 unsigned int class_idx = hlock->class_idx;
559
560 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
561 barrier();
562
563 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
564 printk("<RELEASED>\n");
565 return;
566 }
567
568 print_lock_name(lock_classes + class_idx - 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700569 printk(", at: ");
570 print_ip_sym(hlock->acquire_ip);
571}
572
573static void lockdep_print_held_locks(struct task_struct *curr)
574{
575 int i, depth = curr->lockdep_depth;
576
577 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700578 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700579 return;
580 }
581 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700582 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700583
584 for (i = 0; i < depth; i++) {
585 printk(" #%d: ", i);
586 print_lock(curr->held_locks + i);
587 }
588}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700589
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100590static void print_kernel_ident(void)
Dave Jones99de0552006-09-29 02:00:10 -0700591{
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100592 printk("%s %.*s %s\n", init_utsname()->release,
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700593 (int)strcspn(init_utsname()->version, " "),
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100594 init_utsname()->version,
595 print_tainted());
Dave Jones99de0552006-09-29 02:00:10 -0700596}
597
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700598static int very_verbose(struct lock_class *class)
599{
600#if VERY_VERBOSE
601 return class_filter(class);
602#endif
603 return 0;
604}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700605
606/*
607 * Is this the address of a static object:
608 */
Sasha Levin8dce7a92013-06-13 18:41:16 -0400609#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700610static int static_obj(void *obj)
611{
612 unsigned long start = (unsigned long) &_stext,
613 end = (unsigned long) &_end,
614 addr = (unsigned long) obj;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700615
616 /*
617 * static variable?
618 */
619 if ((addr >= start) && (addr < end))
620 return 1;
621
Mike Frysinger2a9ad182009-09-22 16:44:16 -0700622 if (arch_is_kernel_data(addr))
623 return 1;
624
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700625 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900626 * in-kernel percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700627 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900628 if (is_kernel_percpu_address(addr))
629 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700630
631 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900632 * module static or percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700633 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900634 return is_module_address(addr) || is_module_percpu_address(addr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700635}
Sasha Levin8dce7a92013-06-13 18:41:16 -0400636#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700637
638/*
639 * To make lock name printouts unique, we calculate a unique
640 * class->name_version generation counter:
641 */
642static int count_matching_names(struct lock_class *new_class)
643{
644 struct lock_class *class;
645 int count = 0;
646
647 if (!new_class->name)
648 return 0;
649
Peter Zijlstra35a93932015-02-26 16:23:11 +0100650 list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700651 if (new_class->key - new_class->subclass == class->key)
652 return class->name_version;
653 if (class->name && !strcmp(class->name, new_class->name))
654 count = max(count, class->name_version);
655 }
656
657 return count + 1;
658}
659
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700660/*
661 * Register a lock's class in the hash-table, if the class is not present
662 * yet. Otherwise we look it up. We cache the result in the lock object
663 * itself, so actual lookup of the hash should be once per lock object.
664 */
665static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700666look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700667{
668 struct lockdep_subclass_key *key;
669 struct list_head *hash_head;
670 struct lock_class *class;
671
672#ifdef CONFIG_DEBUG_LOCKDEP
673 /*
674 * If the architecture calls into lockdep before initializing
675 * the hashes then we'll warn about it later. (we cannot printk
676 * right now)
677 */
678 if (unlikely(!lockdep_initialized)) {
679 lockdep_init();
680 lockdep_init_error = 1;
Ming Lei81140ac2011-11-17 13:34:32 +0800681 lock_init_error = lock->name;
Johannes Bergc71063c2007-07-19 01:49:02 -0700682 save_stack_trace(&lockdep_init_trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700683 }
684#endif
685
Hitoshi Mitake4ba053c2010-10-13 17:30:26 +0900686 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
687 debug_locks_off();
688 printk(KERN_ERR
689 "BUG: looking up invalid subclass: %u\n", subclass);
690 printk(KERN_ERR
691 "turning off the locking correctness validator.\n");
692 dump_stack();
693 return NULL;
694 }
695
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700696 /*
697 * Static locks do not have their class-keys yet - for them the key
698 * is the lock object itself:
699 */
700 if (unlikely(!lock->key))
701 lock->key = (void *)lock;
702
703 /*
704 * NOTE: the class-key must be unique. For dynamic locks, a static
705 * lock_class_key variable is passed in through the mutex_init()
706 * (or spin_lock_init()) call - which acts as the key. For static
707 * locks we use the lock object itself as the key.
708 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700709 BUILD_BUG_ON(sizeof(struct lock_class_key) >
710 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700711
712 key = lock->key->subkeys + subclass;
713
714 hash_head = classhashentry(key);
715
716 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +0100717 * We do an RCU walk of the hash, see lockdep_free_key_range().
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700718 */
Peter Zijlstra35a93932015-02-26 16:23:11 +0100719 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
720 return NULL;
721
722 list_for_each_entry_rcu(class, hash_head, hash_entry) {
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700723 if (class->key == key) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200724 /*
725 * Huh! same key, different name? Did someone trample
726 * on some memory? We're most confused.
727 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700728 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700729 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700730 }
731 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700732
733 return NULL;
734}
735
736/*
737 * Register a lock's class in the hash-table, if the class is not present
738 * yet. Otherwise we look it up. We cache the result in the lock object
739 * itself, so actual lookup of the hash should be once per lock object.
740 */
741static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400742register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700743{
744 struct lockdep_subclass_key *key;
745 struct list_head *hash_head;
746 struct lock_class *class;
Peter Zijlstra35a93932015-02-26 16:23:11 +0100747
748 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
Ingo Molnard6d897c2006-07-10 04:44:04 -0700749
750 class = look_up_lock_class(lock, subclass);
751 if (likely(class))
Yong Zhang87cdee72011-11-09 16:07:14 +0800752 goto out_set_class_cache;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700753
754 /*
755 * Debug-check: all keys must be persistent!
756 */
757 if (!static_obj(lock->key)) {
758 debug_locks_off();
759 printk("INFO: trying to register non-static key.\n");
760 printk("the code is fine but needs lockdep annotation.\n");
761 printk("turning off the locking correctness validator.\n");
762 dump_stack();
763
764 return NULL;
765 }
766
Ingo Molnard6d897c2006-07-10 04:44:04 -0700767 key = lock->key->subkeys + subclass;
768 hash_head = classhashentry(key);
769
Ingo Molnar74c383f2006-12-13 00:34:43 -0800770 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800771 return NULL;
772 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700773 /*
774 * We have to do the hash-walk again, to avoid races
775 * with another CPU:
776 */
Peter Zijlstra35a93932015-02-26 16:23:11 +0100777 list_for_each_entry_rcu(class, hash_head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700778 if (class->key == key)
779 goto out_unlock_set;
Peter Zijlstra35a93932015-02-26 16:23:11 +0100780 }
781
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700782 /*
783 * Allocate a new key from the static array, and add it to
784 * the hash:
785 */
786 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800787 if (!debug_locks_off_graph_unlock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800788 return NULL;
789 }
Ingo Molnar74c383f2006-12-13 00:34:43 -0800790
Dave Jones2c522832013-04-25 13:40:02 -0400791 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100792 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700793 return NULL;
794 }
795 class = lock_classes + nr_lock_classes++;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200796 debug_atomic_inc(nr_unused_locks);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700797 class->key = key;
798 class->name = lock->name;
799 class->subclass = subclass;
800 INIT_LIST_HEAD(&class->lock_entry);
801 INIT_LIST_HEAD(&class->locks_before);
802 INIT_LIST_HEAD(&class->locks_after);
803 class->name_version = count_matching_names(class);
804 /*
805 * We use RCU's safe list-add method to make
806 * parallel walking of the hash-list safe:
807 */
808 list_add_tail_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100809 /*
810 * Add it to the global list of classes:
811 */
812 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700813
814 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800815 graph_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800816
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700817 printk("\nnew class %p: %s", class->key, class->name);
818 if (class->name_version > 1)
819 printk("#%d", class->name_version);
820 printk("\n");
821 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800822
Ingo Molnar74c383f2006-12-13 00:34:43 -0800823 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800824 return NULL;
825 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700826 }
827out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800828 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700829
Yong Zhang87cdee72011-11-09 16:07:14 +0800830out_set_class_cache:
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400831 if (!subclass || force)
Hitoshi Mitake62016252010-10-05 18:01:51 +0900832 lock->class_cache[0] = class;
833 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
834 lock->class_cache[subclass] = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700835
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200836 /*
837 * Hash collision, did we smoke some? We found a class with a matching
838 * hash but the subclass -- which is hashed in -- didn't match.
839 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800840 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
841 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700842
843 return class;
844}
845
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700846#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700847/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700848 * Allocate a lockdep entry. (assumes the graph_lock held, returns
849 * with NULL on failure)
850 */
851static struct lock_list *alloc_list_entry(void)
852{
853 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
854 if (!debug_locks_off_graph_unlock())
855 return NULL;
856
Dave Jones2c522832013-04-25 13:40:02 -0400857 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100858 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700859 return NULL;
860 }
861 return list_entries + nr_list_entries++;
862}
863
864/*
865 * Add a new dependency to the head of the list:
866 */
867static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
Yong Zhang4726f2a2010-05-04 14:16:48 +0800868 struct list_head *head, unsigned long ip,
869 int distance, struct stack_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -0700870{
871 struct lock_list *entry;
872 /*
873 * Lock not present yet - get a new dependency struct and
874 * add it to the list:
875 */
876 entry = alloc_list_entry();
877 if (!entry)
878 return 0;
879
Zhu Yi74870172008-08-27 14:33:00 +0800880 entry->class = this;
881 entry->distance = distance;
Yong Zhang4726f2a2010-05-04 14:16:48 +0800882 entry->trace = *trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700883 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +0100884 * Both allocation and removal are done under the graph lock; but
885 * iteration is under RCU-sched; see look_up_lock_class() and
886 * lockdep_free_key_range().
Peter Zijlstra8e182572007-07-19 01:48:54 -0700887 */
888 list_add_tail_rcu(&entry->entry, head);
889
890 return 1;
891}
892
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200893/*
894 * For good efficiency of modular, we use power of 2
895 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200896#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
897#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
898
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200899/*
900 * The circular_queue and helpers is used to implement the
Peter Zijlstraaf012962009-07-16 15:44:29 +0200901 * breadth-first search(BFS)algorithem, by which we can build
902 * the shortest path from the next lock to be acquired to the
903 * previous held lock if there is a circular between them.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200904 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200905struct circular_queue {
906 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
907 unsigned int front, rear;
908};
909
910static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200911
Ming Lei12f3dfd2009-07-16 15:44:29 +0200912unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200913
Ming Leie351b662009-07-22 22:48:09 +0800914static unsigned int lockdep_dependency_gen_id;
915
Peter Zijlstraaf012962009-07-16 15:44:29 +0200916static inline void __cq_init(struct circular_queue *cq)
917{
918 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +0800919 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200920}
921
922static inline int __cq_empty(struct circular_queue *cq)
923{
924 return (cq->front == cq->rear);
925}
926
927static inline int __cq_full(struct circular_queue *cq)
928{
929 return ((cq->rear + 1) & CQ_MASK) == cq->front;
930}
931
932static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
933{
934 if (__cq_full(cq))
935 return -1;
936
937 cq->element[cq->rear] = elem;
938 cq->rear = (cq->rear + 1) & CQ_MASK;
939 return 0;
940}
941
942static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
943{
944 if (__cq_empty(cq))
945 return -1;
946
947 *elem = cq->element[cq->front];
948 cq->front = (cq->front + 1) & CQ_MASK;
949 return 0;
950}
951
952static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
953{
954 return (cq->rear - cq->front) & CQ_MASK;
955}
956
957static inline void mark_lock_accessed(struct lock_list *lock,
958 struct lock_list *parent)
959{
960 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200961
Peter Zijlstraaf012962009-07-16 15:44:29 +0200962 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200963 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200964 lock->parent = parent;
Ming Leie351b662009-07-22 22:48:09 +0800965 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200966}
967
968static inline unsigned long lock_accessed(struct lock_list *lock)
969{
970 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200971
Peter Zijlstraaf012962009-07-16 15:44:29 +0200972 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200973 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Ming Leie351b662009-07-22 22:48:09 +0800974 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200975}
976
977static inline struct lock_list *get_lock_parent(struct lock_list *child)
978{
979 return child->parent;
980}
981
982static inline int get_lock_depth(struct lock_list *child)
983{
984 int depth = 0;
985 struct lock_list *parent;
986
987 while ((parent = get_lock_parent(child))) {
988 child = parent;
989 depth++;
990 }
991 return depth;
992}
993
Ming Lei9e2d5512009-07-16 15:44:29 +0200994static int __bfs(struct lock_list *source_entry,
Peter Zijlstraaf012962009-07-16 15:44:29 +0200995 void *data,
996 int (*match)(struct lock_list *entry, void *data),
997 struct lock_list **target_entry,
998 int forward)
Ming Leic94aa5c2009-07-16 15:44:29 +0200999{
1000 struct lock_list *entry;
Ming Leid588e462009-07-16 15:44:29 +02001001 struct list_head *head;
Ming Leic94aa5c2009-07-16 15:44:29 +02001002 struct circular_queue *cq = &lock_cq;
1003 int ret = 1;
1004
Ming Lei9e2d5512009-07-16 15:44:29 +02001005 if (match(source_entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001006 *target_entry = source_entry;
1007 ret = 0;
1008 goto exit;
1009 }
1010
Ming Leid588e462009-07-16 15:44:29 +02001011 if (forward)
1012 head = &source_entry->class->locks_after;
1013 else
1014 head = &source_entry->class->locks_before;
1015
1016 if (list_empty(head))
1017 goto exit;
1018
1019 __cq_init(cq);
Ming Leic94aa5c2009-07-16 15:44:29 +02001020 __cq_enqueue(cq, (unsigned long)source_entry);
1021
1022 while (!__cq_empty(cq)) {
1023 struct lock_list *lock;
Ming Leic94aa5c2009-07-16 15:44:29 +02001024
1025 __cq_dequeue(cq, (unsigned long *)&lock);
1026
1027 if (!lock->class) {
1028 ret = -2;
1029 goto exit;
1030 }
1031
1032 if (forward)
1033 head = &lock->class->locks_after;
1034 else
1035 head = &lock->class->locks_before;
1036
Peter Zijlstra35a93932015-02-26 16:23:11 +01001037 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1038
1039 list_for_each_entry_rcu(entry, head, entry) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001040 if (!lock_accessed(entry)) {
Ming Lei12f3dfd2009-07-16 15:44:29 +02001041 unsigned int cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001042 mark_lock_accessed(entry, lock);
Ming Lei9e2d5512009-07-16 15:44:29 +02001043 if (match(entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001044 *target_entry = entry;
1045 ret = 0;
1046 goto exit;
1047 }
1048
1049 if (__cq_enqueue(cq, (unsigned long)entry)) {
1050 ret = -1;
1051 goto exit;
1052 }
Ming Lei12f3dfd2009-07-16 15:44:29 +02001053 cq_depth = __cq_get_elem_count(cq);
1054 if (max_bfs_queue_depth < cq_depth)
1055 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001056 }
1057 }
1058 }
1059exit:
1060 return ret;
1061}
1062
Ming Leid7aaba12009-07-16 15:44:29 +02001063static inline int __bfs_forwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001064 void *data,
1065 int (*match)(struct lock_list *entry, void *data),
1066 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001067{
Ming Lei9e2d5512009-07-16 15:44:29 +02001068 return __bfs(src_entry, data, match, target_entry, 1);
Ming Leic94aa5c2009-07-16 15:44:29 +02001069
1070}
1071
Ming Leid7aaba12009-07-16 15:44:29 +02001072static inline int __bfs_backwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001073 void *data,
1074 int (*match)(struct lock_list *entry, void *data),
1075 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001076{
Ming Lei9e2d5512009-07-16 15:44:29 +02001077 return __bfs(src_entry, data, match, target_entry, 0);
Ming Leic94aa5c2009-07-16 15:44:29 +02001078
1079}
1080
Peter Zijlstra8e182572007-07-19 01:48:54 -07001081/*
1082 * Recursive, forwards-direction lock-dependency checking, used for
1083 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1084 * checking.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001085 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001086
1087/*
1088 * Print a dependency chain entry (this is only done when a deadlock
1089 * has been detected):
1090 */
1091static noinline int
Ming Lei24208ca2009-07-16 15:44:29 +02001092print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001093{
1094 if (debug_locks_silent)
1095 return 0;
1096 printk("\n-> #%u", depth);
1097 print_lock_name(target->class);
1098 printk(":\n");
1099 print_stack_trace(&target->trace, 6);
1100
1101 return 0;
1102}
1103
Steven Rostedtf4185812011-04-20 21:41:55 -04001104static void
1105print_circular_lock_scenario(struct held_lock *src,
1106 struct held_lock *tgt,
1107 struct lock_list *prt)
1108{
1109 struct lock_class *source = hlock_class(src);
1110 struct lock_class *target = hlock_class(tgt);
1111 struct lock_class *parent = prt->class;
1112
1113 /*
1114 * A direct locking problem where unsafe_class lock is taken
1115 * directly by safe_class lock, then all we need to show
1116 * is the deadlock scenario, as it is obvious that the
1117 * unsafe lock is taken under the safe lock.
1118 *
1119 * But if there is a chain instead, where the safe lock takes
1120 * an intermediate lock (middle_class) where this lock is
1121 * not the same as the safe lock, then the lock chain is
1122 * used to describe the problem. Otherwise we would need
1123 * to show a different CPU case for each link in the chain
1124 * from the safe_class lock to the unsafe_class lock.
1125 */
1126 if (parent != source) {
1127 printk("Chain exists of:\n ");
1128 __print_lock_name(source);
1129 printk(" --> ");
1130 __print_lock_name(parent);
1131 printk(" --> ");
1132 __print_lock_name(target);
1133 printk("\n\n");
1134 }
1135
1136 printk(" Possible unsafe locking scenario:\n\n");
1137 printk(" CPU0 CPU1\n");
1138 printk(" ---- ----\n");
1139 printk(" lock(");
1140 __print_lock_name(target);
1141 printk(");\n");
1142 printk(" lock(");
1143 __print_lock_name(parent);
1144 printk(");\n");
1145 printk(" lock(");
1146 __print_lock_name(target);
1147 printk(");\n");
1148 printk(" lock(");
1149 __print_lock_name(source);
1150 printk(");\n");
1151 printk("\n *** DEADLOCK ***\n\n");
1152}
1153
Peter Zijlstra8e182572007-07-19 01:48:54 -07001154/*
1155 * When a circular dependency is detected, print the
1156 * header first:
1157 */
1158static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001159print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1160 struct held_lock *check_src,
1161 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001162{
1163 struct task_struct *curr = current;
1164
Ming Leic94aa5c2009-07-16 15:44:29 +02001165 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001166 return 0;
1167
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001168 printk("\n");
1169 printk("======================================================\n");
1170 printk("[ INFO: possible circular locking dependency detected ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001171 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001172 printk("-------------------------------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001173 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001174 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001175 print_lock(check_src);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001176 printk("\nbut task is already holding lock:\n");
Ming Leidb0002a2009-07-16 15:44:29 +02001177 print_lock(check_tgt);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001178 printk("\nwhich lock already depends on the new lock.\n\n");
1179 printk("\nthe existing dependency chain (in reverse order) is:\n");
1180
1181 print_circular_bug_entry(entry, depth);
1182
1183 return 0;
1184}
1185
Ming Lei9e2d5512009-07-16 15:44:29 +02001186static inline int class_equal(struct lock_list *entry, void *data)
1187{
1188 return entry->class == data;
1189}
1190
Ming Leidb0002a2009-07-16 15:44:29 +02001191static noinline int print_circular_bug(struct lock_list *this,
1192 struct lock_list *target,
1193 struct held_lock *check_src,
1194 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001195{
1196 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001197 struct lock_list *parent;
Steven Rostedtf4185812011-04-20 21:41:55 -04001198 struct lock_list *first_parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001199 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001200
Ming Leic94aa5c2009-07-16 15:44:29 +02001201 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001202 return 0;
1203
Ming Leidb0002a2009-07-16 15:44:29 +02001204 if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001205 return 0;
1206
Ming Leic94aa5c2009-07-16 15:44:29 +02001207 depth = get_lock_depth(target);
1208
Ming Leidb0002a2009-07-16 15:44:29 +02001209 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001210
1211 parent = get_lock_parent(target);
Steven Rostedtf4185812011-04-20 21:41:55 -04001212 first_parent = parent;
Ming Leic94aa5c2009-07-16 15:44:29 +02001213
1214 while (parent) {
1215 print_circular_bug_entry(parent, --depth);
1216 parent = get_lock_parent(parent);
1217 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001218
1219 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001220 print_circular_lock_scenario(check_src, check_tgt,
1221 first_parent);
1222
Peter Zijlstra8e182572007-07-19 01:48:54 -07001223 lockdep_print_held_locks(curr);
1224
1225 printk("\nstack backtrace:\n");
1226 dump_stack();
1227
1228 return 0;
1229}
1230
Ming Leidb0002a2009-07-16 15:44:29 +02001231static noinline int print_bfs_bug(int ret)
1232{
1233 if (!debug_locks_off_graph_unlock())
1234 return 0;
1235
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001236 /*
1237 * Breadth-first-search failed, graph got corrupted?
1238 */
Ming Leidb0002a2009-07-16 15:44:29 +02001239 WARN(1, "lockdep bfs error:%d\n", ret);
1240
1241 return 0;
1242}
1243
Ming Leief681022009-07-16 15:44:29 +02001244static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001245{
Ming Leief681022009-07-16 15:44:29 +02001246 (*(unsigned long *)data)++;
1247 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001248}
1249
Fengguang Wu5216d532013-11-09 00:55:35 +08001250static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
Ming Leief681022009-07-16 15:44:29 +02001251{
1252 unsigned long count = 0;
1253 struct lock_list *uninitialized_var(target_entry);
1254
1255 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1256
1257 return count;
1258}
David Miller419ca3f2008-07-29 21:45:03 -07001259unsigned long lockdep_count_forward_deps(struct lock_class *class)
1260{
1261 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001262 struct lock_list this;
1263
1264 this.parent = NULL;
1265 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001266
1267 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001268 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001269 ret = __lockdep_count_forward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001270 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001271 local_irq_restore(flags);
1272
1273 return ret;
1274}
1275
Fengguang Wu5216d532013-11-09 00:55:35 +08001276static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001277{
Ming Leief681022009-07-16 15:44:29 +02001278 unsigned long count = 0;
1279 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001280
Ming Leief681022009-07-16 15:44:29 +02001281 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001282
Ming Leief681022009-07-16 15:44:29 +02001283 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001284}
1285
1286unsigned long lockdep_count_backward_deps(struct lock_class *class)
1287{
1288 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001289 struct lock_list this;
1290
1291 this.parent = NULL;
1292 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001293
1294 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001295 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001296 ret = __lockdep_count_backward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001297 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001298 local_irq_restore(flags);
1299
1300 return ret;
1301}
1302
Peter Zijlstra8e182572007-07-19 01:48:54 -07001303/*
1304 * Prove that the dependency graph starting at <entry> can not
1305 * lead to <target>. Print an error and return 0 if it does.
1306 */
1307static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001308check_noncircular(struct lock_list *root, struct lock_class *target,
1309 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001310{
Ming Leidb0002a2009-07-16 15:44:29 +02001311 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001312
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001313 debug_atomic_inc(nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001314
Ming Leid7aaba12009-07-16 15:44:29 +02001315 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001316
1317 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001318}
1319
Steven Rostedt81d68a92008-05-12 21:20:42 +02001320#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001321/*
1322 * Forwards and backwards subgraph searching, for the purposes of
1323 * proving that two subgraphs can be connected by a new dependency
1324 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1325 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001326
Ming Leid7aaba12009-07-16 15:44:29 +02001327static inline int usage_match(struct lock_list *entry, void *bit)
1328{
1329 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1330}
1331
1332
1333
Peter Zijlstra8e182572007-07-19 01:48:54 -07001334/*
1335 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001336 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001337 *
Ming Leid7aaba12009-07-16 15:44:29 +02001338 * Return 0 if such a node exists in the subgraph, and put that node
1339 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001340 *
Ming Leid7aaba12009-07-16 15:44:29 +02001341 * Return 1 otherwise and keep *@target_entry unchanged.
1342 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001343 */
Ming Leid7aaba12009-07-16 15:44:29 +02001344static int
1345find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1346 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001347{
Ming Leid7aaba12009-07-16 15:44:29 +02001348 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001349
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001350 debug_atomic_inc(nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001351
Ming Leid7aaba12009-07-16 15:44:29 +02001352 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1353
1354 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001355}
1356
1357/*
1358 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001359 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001360 *
Ming Leid7aaba12009-07-16 15:44:29 +02001361 * Return 0 if such a node exists in the subgraph, and put that node
1362 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001363 *
Ming Leid7aaba12009-07-16 15:44:29 +02001364 * Return 1 otherwise and keep *@target_entry unchanged.
1365 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001366 */
Ming Leid7aaba12009-07-16 15:44:29 +02001367static int
1368find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1369 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001370{
Ming Leid7aaba12009-07-16 15:44:29 +02001371 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001372
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001373 debug_atomic_inc(nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001374
Ming Leid7aaba12009-07-16 15:44:29 +02001375 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001376
Ming Leid7aaba12009-07-16 15:44:29 +02001377 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001378}
1379
Peter Zijlstraaf012962009-07-16 15:44:29 +02001380static void print_lock_class_header(struct lock_class *class, int depth)
1381{
1382 int bit;
1383
1384 printk("%*s->", depth, "");
1385 print_lock_name(class);
1386 printk(" ops: %lu", class->ops);
1387 printk(" {\n");
1388
1389 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1390 if (class->usage_mask & (1 << bit)) {
1391 int len = depth;
1392
1393 len += printk("%*s %s", depth, "", usage_str[bit]);
1394 len += printk(" at:\n");
1395 print_stack_trace(class->usage_traces + bit, len);
1396 }
1397 }
1398 printk("%*s }\n", depth, "");
1399
1400 printk("%*s ... key at: ",depth,"");
1401 print_ip_sym((unsigned long)class->key);
1402}
1403
1404/*
1405 * printk the shortest lock dependencies from @start to @end in reverse order:
1406 */
1407static void __used
1408print_shortest_lock_dependencies(struct lock_list *leaf,
1409 struct lock_list *root)
1410{
1411 struct lock_list *entry = leaf;
1412 int depth;
1413
1414 /*compute depth from generated tree by BFS*/
1415 depth = get_lock_depth(leaf);
1416
1417 do {
1418 print_lock_class_header(entry->class, depth);
1419 printk("%*s ... acquired at:\n", depth, "");
1420 print_stack_trace(&entry->trace, 2);
1421 printk("\n");
1422
1423 if (depth == 0 && (entry != root)) {
Steven Rostedt6be8c392011-04-20 21:41:58 -04001424 printk("lockdep:%s bad path found in chain graph\n", __func__);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001425 break;
1426 }
1427
1428 entry = get_lock_parent(entry);
1429 depth--;
1430 } while (entry && (depth >= 0));
1431
1432 return;
1433}
Ming Leid7aaba12009-07-16 15:44:29 +02001434
Steven Rostedt3003eba2011-04-20 21:41:54 -04001435static void
1436print_irq_lock_scenario(struct lock_list *safe_entry,
1437 struct lock_list *unsafe_entry,
Steven Rostedtdad3d742011-04-20 21:41:57 -04001438 struct lock_class *prev_class,
1439 struct lock_class *next_class)
Steven Rostedt3003eba2011-04-20 21:41:54 -04001440{
1441 struct lock_class *safe_class = safe_entry->class;
1442 struct lock_class *unsafe_class = unsafe_entry->class;
Steven Rostedtdad3d742011-04-20 21:41:57 -04001443 struct lock_class *middle_class = prev_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001444
1445 if (middle_class == safe_class)
Steven Rostedtdad3d742011-04-20 21:41:57 -04001446 middle_class = next_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001447
1448 /*
1449 * A direct locking problem where unsafe_class lock is taken
1450 * directly by safe_class lock, then all we need to show
1451 * is the deadlock scenario, as it is obvious that the
1452 * unsafe lock is taken under the safe lock.
1453 *
1454 * But if there is a chain instead, where the safe lock takes
1455 * an intermediate lock (middle_class) where this lock is
1456 * not the same as the safe lock, then the lock chain is
1457 * used to describe the problem. Otherwise we would need
1458 * to show a different CPU case for each link in the chain
1459 * from the safe_class lock to the unsafe_class lock.
1460 */
1461 if (middle_class != unsafe_class) {
1462 printk("Chain exists of:\n ");
1463 __print_lock_name(safe_class);
1464 printk(" --> ");
1465 __print_lock_name(middle_class);
1466 printk(" --> ");
1467 __print_lock_name(unsafe_class);
1468 printk("\n\n");
1469 }
1470
1471 printk(" Possible interrupt unsafe locking scenario:\n\n");
1472 printk(" CPU0 CPU1\n");
1473 printk(" ---- ----\n");
1474 printk(" lock(");
1475 __print_lock_name(unsafe_class);
1476 printk(");\n");
1477 printk(" local_irq_disable();\n");
1478 printk(" lock(");
1479 __print_lock_name(safe_class);
1480 printk(");\n");
1481 printk(" lock(");
1482 __print_lock_name(middle_class);
1483 printk(");\n");
1484 printk(" <Interrupt>\n");
1485 printk(" lock(");
1486 __print_lock_name(safe_class);
1487 printk(");\n");
1488 printk("\n *** DEADLOCK ***\n\n");
1489}
1490
Peter Zijlstra8e182572007-07-19 01:48:54 -07001491static int
1492print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001493 struct lock_list *prev_root,
1494 struct lock_list *next_root,
1495 struct lock_list *backwards_entry,
1496 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001497 struct held_lock *prev,
1498 struct held_lock *next,
1499 enum lock_usage_bit bit1,
1500 enum lock_usage_bit bit2,
1501 const char *irqclass)
1502{
1503 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1504 return 0;
1505
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001506 printk("\n");
1507 printk("======================================================\n");
1508 printk("[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07001509 irqclass, irqclass);
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001510 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001511 printk("------------------------------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001512 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001513 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001514 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1515 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1516 curr->hardirqs_enabled,
1517 curr->softirqs_enabled);
1518 print_lock(next);
1519
1520 printk("\nand this task is already holding:\n");
1521 print_lock(prev);
1522 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001523 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001524 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001525 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001526 printk("\n");
1527
1528 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1529 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001530 print_lock_name(backwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001531 printk("\n... which became %s-irq-safe at:\n", irqclass);
1532
Ming Lei24208ca2009-07-16 15:44:29 +02001533 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001534
1535 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001536 print_lock_name(forwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001537 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1538 printk("...");
1539
Ming Lei24208ca2009-07-16 15:44:29 +02001540 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001541
1542 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04001543 print_irq_lock_scenario(backwards_entry, forwards_entry,
1544 hlock_class(prev), hlock_class(next));
Steven Rostedt3003eba2011-04-20 21:41:54 -04001545
Peter Zijlstra8e182572007-07-19 01:48:54 -07001546 lockdep_print_held_locks(curr);
1547
Ming Lei24208ca2009-07-16 15:44:29 +02001548 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1549 printk(" and the holding lock:\n");
1550 if (!save_trace(&prev_root->trace))
1551 return 0;
1552 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001553
Ming Lei24208ca2009-07-16 15:44:29 +02001554 printk("\nthe dependencies between the lock to be acquired");
1555 printk(" and %s-irq-unsafe lock:\n", irqclass);
1556 if (!save_trace(&next_root->trace))
1557 return 0;
1558 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001559
1560 printk("\nstack backtrace:\n");
1561 dump_stack();
1562
1563 return 0;
1564}
1565
1566static int
1567check_usage(struct task_struct *curr, struct held_lock *prev,
1568 struct held_lock *next, enum lock_usage_bit bit_backwards,
1569 enum lock_usage_bit bit_forwards, const char *irqclass)
1570{
1571 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001572 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001573 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001574 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001575
Ming Leid7aaba12009-07-16 15:44:29 +02001576 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001577
Ming Leid7aaba12009-07-16 15:44:29 +02001578 this.class = hlock_class(prev);
1579 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001580 if (ret < 0)
1581 return print_bfs_bug(ret);
1582 if (ret == 1)
1583 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001584
Ming Lei24208ca2009-07-16 15:44:29 +02001585 that.parent = NULL;
1586 that.class = hlock_class(next);
1587 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001588 if (ret < 0)
1589 return print_bfs_bug(ret);
1590 if (ret == 1)
1591 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001592
Ming Lei24208ca2009-07-16 15:44:29 +02001593 return print_bad_irq_dependency(curr, &this, &that,
1594 target_entry, target_entry1,
1595 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001596 bit_backwards, bit_forwards, irqclass);
1597}
1598
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001599static const char *state_names[] = {
1600#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001601 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001602#include "lockdep_states.h"
1603#undef LOCKDEP_STATE
1604};
1605
1606static const char *state_rnames[] = {
1607#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001608 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001609#include "lockdep_states.h"
1610#undef LOCKDEP_STATE
1611};
1612
1613static inline const char *state_name(enum lock_usage_bit bit)
1614{
1615 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1616}
1617
1618static int exclusive_bit(int new_bit)
1619{
1620 /*
1621 * USED_IN
1622 * USED_IN_READ
1623 * ENABLED
1624 * ENABLED_READ
1625 *
1626 * bit 0 - write/read
1627 * bit 1 - used_in/enabled
1628 * bit 2+ state
1629 */
1630
1631 int state = new_bit & ~3;
1632 int dir = new_bit & 2;
1633
1634 /*
1635 * keep state, bit flip the direction and strip read.
1636 */
1637 return state | (dir ^ 2);
1638}
1639
1640static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1641 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001642{
1643 /*
1644 * Prove that the new dependency does not connect a hardirq-safe
1645 * lock with a hardirq-unsafe lock - to achieve this we search
1646 * the backwards-subgraph starting at <prev>, and the
1647 * forwards-subgraph starting at <next>:
1648 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001649 if (!check_usage(curr, prev, next, bit,
1650 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001651 return 0;
1652
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001653 bit++; /* _READ */
1654
Peter Zijlstra8e182572007-07-19 01:48:54 -07001655 /*
1656 * Prove that the new dependency does not connect a hardirq-safe-read
1657 * lock with a hardirq-unsafe lock - to achieve this we search
1658 * the backwards-subgraph starting at <prev>, and the
1659 * forwards-subgraph starting at <next>:
1660 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001661 if (!check_usage(curr, prev, next, bit,
1662 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001663 return 0;
1664
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001665 return 1;
1666}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001667
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001668static int
1669check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1670 struct held_lock *next)
1671{
1672#define LOCKDEP_STATE(__STATE) \
1673 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001674 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001675#include "lockdep_states.h"
1676#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001677
Peter Zijlstra8e182572007-07-19 01:48:54 -07001678 return 1;
1679}
1680
1681static void inc_chains(void)
1682{
1683 if (current->hardirq_context)
1684 nr_hardirq_chains++;
1685 else {
1686 if (current->softirq_context)
1687 nr_softirq_chains++;
1688 else
1689 nr_process_chains++;
1690 }
1691}
1692
1693#else
1694
1695static inline int
1696check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1697 struct held_lock *next)
1698{
1699 return 1;
1700}
1701
1702static inline void inc_chains(void)
1703{
1704 nr_process_chains++;
1705}
1706
1707#endif
1708
Steven Rostedt48702ec2011-04-20 21:41:56 -04001709static void
1710print_deadlock_scenario(struct held_lock *nxt,
1711 struct held_lock *prv)
1712{
1713 struct lock_class *next = hlock_class(nxt);
1714 struct lock_class *prev = hlock_class(prv);
1715
1716 printk(" Possible unsafe locking scenario:\n\n");
1717 printk(" CPU0\n");
1718 printk(" ----\n");
1719 printk(" lock(");
1720 __print_lock_name(prev);
1721 printk(");\n");
1722 printk(" lock(");
1723 __print_lock_name(next);
1724 printk(");\n");
1725 printk("\n *** DEADLOCK ***\n\n");
1726 printk(" May be due to missing lock nesting notation\n\n");
1727}
1728
Peter Zijlstra8e182572007-07-19 01:48:54 -07001729static int
1730print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1731 struct held_lock *next)
1732{
1733 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1734 return 0;
1735
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001736 printk("\n");
1737 printk("=============================================\n");
1738 printk("[ INFO: possible recursive locking detected ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001739 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001740 printk("---------------------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001741 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001742 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001743 print_lock(next);
1744 printk("\nbut task is already holding lock:\n");
1745 print_lock(prev);
1746
1747 printk("\nother info that might help us debug this:\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001748 print_deadlock_scenario(next, prev);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001749 lockdep_print_held_locks(curr);
1750
1751 printk("\nstack backtrace:\n");
1752 dump_stack();
1753
1754 return 0;
1755}
1756
1757/*
1758 * Check whether we are holding such a class already.
1759 *
1760 * (Note that this has to be done separately, because the graph cannot
1761 * detect such classes of deadlocks.)
1762 *
1763 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1764 */
1765static int
1766check_deadlock(struct task_struct *curr, struct held_lock *next,
1767 struct lockdep_map *next_instance, int read)
1768{
1769 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001770 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001771 int i;
1772
1773 for (i = 0; i < curr->lockdep_depth; i++) {
1774 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001775
1776 if (prev->instance == next->nest_lock)
1777 nest = prev;
1778
Dave Jonesf82b2172008-08-11 09:30:23 +02001779 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001780 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001781
Peter Zijlstra8e182572007-07-19 01:48:54 -07001782 /*
1783 * Allow read-after-read recursion of the same
1784 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1785 */
1786 if ((read == 2) && prev->read)
1787 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001788
1789 /*
1790 * We're holding the nest_lock, which serializes this lock's
1791 * nesting behaviour.
1792 */
1793 if (nest)
1794 return 2;
1795
Peter Zijlstra8e182572007-07-19 01:48:54 -07001796 return print_deadlock_bug(curr, prev, next);
1797 }
1798 return 1;
1799}
1800
1801/*
1802 * There was a chain-cache miss, and we are about to add a new dependency
1803 * to a previous lock. We recursively validate the following rules:
1804 *
1805 * - would the adding of the <prev> -> <next> dependency create a
1806 * circular dependency in the graph? [== circular deadlock]
1807 *
1808 * - does the new prev->next dependency connect any hardirq-safe lock
1809 * (in the full backwards-subgraph starting at <prev>) with any
1810 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1811 * <next>)? [== illegal lock inversion with hardirq contexts]
1812 *
1813 * - does the new prev->next dependency connect any softirq-safe lock
1814 * (in the full backwards-subgraph starting at <prev>) with any
1815 * softirq-unsafe lock (in the full forwards-subgraph starting at
1816 * <next>)? [== illegal lock inversion with softirq contexts]
1817 *
1818 * any of these scenarios could lead to a deadlock.
1819 *
1820 * Then if all the validations pass, we add the forwards and backwards
1821 * dependency.
1822 */
1823static int
1824check_prev_add(struct task_struct *curr, struct held_lock *prev,
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001825 struct held_lock *next, int distance, int *stack_saved)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001826{
1827 struct lock_list *entry;
1828 int ret;
Ming Leidb0002a2009-07-16 15:44:29 +02001829 struct lock_list this;
1830 struct lock_list *uninitialized_var(target_entry);
Yong Zhang4726f2a2010-05-04 14:16:48 +08001831 /*
1832 * Static variable, serialized by the graph_lock().
1833 *
1834 * We use this static variable to save the stack trace in case
1835 * we call into this function multiple times due to encountering
1836 * trylocks in the held lock stack.
1837 */
1838 static struct stack_trace trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001839
1840 /*
1841 * Prove that the new <prev> -> <next> dependency would not
1842 * create a circular dependency in the graph. (We do this by
1843 * forward-recursing into the graph starting at <next>, and
1844 * checking whether we can reach <prev>.)
1845 *
1846 * We are using global variables to control the recursion, to
1847 * keep the stackframe size of the recursive functions low:
1848 */
Ming Leidb0002a2009-07-16 15:44:29 +02001849 this.class = hlock_class(next);
1850 this.parent = NULL;
1851 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1852 if (unlikely(!ret))
1853 return print_circular_bug(&this, target_entry, next, prev);
1854 else if (unlikely(ret < 0))
1855 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02001856
Peter Zijlstra8e182572007-07-19 01:48:54 -07001857 if (!check_prev_add_irq(curr, prev, next))
1858 return 0;
1859
1860 /*
1861 * For recursive read-locks we do all the dependency checks,
1862 * but we dont store read-triggered dependencies (only
1863 * write-triggered dependencies). This ensures that only the
1864 * write-side dependencies matter, and that if for example a
1865 * write-lock never takes any other locks, then the reads are
1866 * equivalent to a NOP.
1867 */
1868 if (next->read == 2 || prev->read == 2)
1869 return 1;
1870 /*
1871 * Is the <prev> -> <next> dependency already present?
1872 *
1873 * (this may occur even though this is a new chain: consider
1874 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1875 * chains - the second one will be new, but L1 already has
1876 * L2 added to its dependency list, due to the first chain.)
1877 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001878 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1879 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001880 if (distance == 1)
1881 entry->distance = 1;
1882 return 2;
1883 }
1884 }
1885
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001886 if (!*stack_saved) {
1887 if (!save_trace(&trace))
1888 return 0;
1889 *stack_saved = 1;
1890 }
Yong Zhang4726f2a2010-05-04 14:16:48 +08001891
Peter Zijlstra8e182572007-07-19 01:48:54 -07001892 /*
1893 * Ok, all validations passed, add the new lock
1894 * to the previous lock's dependency list:
1895 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001896 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1897 &hlock_class(prev)->locks_after,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001898 next->acquire_ip, distance, &trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001899
1900 if (!ret)
1901 return 0;
1902
Dave Jonesf82b2172008-08-11 09:30:23 +02001903 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1904 &hlock_class(next)->locks_before,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001905 next->acquire_ip, distance, &trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001906 if (!ret)
1907 return 0;
1908
1909 /*
1910 * Debugging printouts:
1911 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001912 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001913 /* We drop graph lock, so another thread can overwrite trace. */
1914 *stack_saved = 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001915 graph_unlock();
1916 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001917 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001918 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001919 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001920 printk("\n");
1921 dump_stack();
1922 return graph_lock();
1923 }
1924 return 1;
1925}
1926
1927/*
1928 * Add the dependency to all directly-previous locks that are 'relevant'.
1929 * The ones that are relevant are (in increasing distance from curr):
1930 * all consecutive trylock entries and the final non-trylock entry - or
1931 * the end of this context's lock-chain - whichever comes first.
1932 */
1933static int
1934check_prevs_add(struct task_struct *curr, struct held_lock *next)
1935{
1936 int depth = curr->lockdep_depth;
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001937 int stack_saved = 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001938 struct held_lock *hlock;
1939
1940 /*
1941 * Debugging checks.
1942 *
1943 * Depth must not be zero for a non-head lock:
1944 */
1945 if (!depth)
1946 goto out_bug;
1947 /*
1948 * At least two relevant locks must exist for this
1949 * to be a head:
1950 */
1951 if (curr->held_locks[depth].irq_context !=
1952 curr->held_locks[depth-1].irq_context)
1953 goto out_bug;
1954
1955 for (;;) {
1956 int distance = curr->lockdep_depth - depth + 1;
Oleg Nesterov1b5ff812014-01-20 19:20:10 +01001957 hlock = curr->held_locks + depth - 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001958 /*
1959 * Only non-recursive-read entries get new dependencies
1960 * added:
1961 */
Oleg Nesterov1b5ff812014-01-20 19:20:10 +01001962 if (hlock->read != 2 && hlock->check) {
Yong Zhang4726f2a2010-05-04 14:16:48 +08001963 if (!check_prev_add(curr, hlock, next,
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001964 distance, &stack_saved))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001965 return 0;
1966 /*
1967 * Stop after the first non-trylock entry,
1968 * as non-trylock entries have added their
1969 * own direct dependencies already, so this
1970 * lock is connected to them indirectly:
1971 */
1972 if (!hlock->trylock)
1973 break;
1974 }
1975 depth--;
1976 /*
1977 * End of lock-stack?
1978 */
1979 if (!depth)
1980 break;
1981 /*
1982 * Stop the search if we cross into another context:
1983 */
1984 if (curr->held_locks[depth].irq_context !=
1985 curr->held_locks[depth-1].irq_context)
1986 break;
1987 }
1988 return 1;
1989out_bug:
1990 if (!debug_locks_off_graph_unlock())
1991 return 0;
1992
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001993 /*
1994 * Clearly we all shouldn't be here, but since we made it we
1995 * can reliable say we messed up our state. See the above two
1996 * gotos for reasons why we could possibly end up here.
1997 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001998 WARN_ON(1);
1999
2000 return 0;
2001}
2002
2003unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08002004struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08002005int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08002006static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
2007
2008struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
2009{
2010 return lock_classes + chain_hlocks[chain->base + i];
2011}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002012
2013/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002014 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07002015 * add it and return 1 - in this case the new dependency chain is
2016 * validated. If the key is already hashed, return 0.
2017 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002018 */
Huang, Ying443cd502008-06-20 16:39:21 +08002019static inline int lookup_chain_cache(struct task_struct *curr,
2020 struct held_lock *hlock,
2021 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002022{
Dave Jonesf82b2172008-08-11 09:30:23 +02002023 struct lock_class *class = hlock_class(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002024 struct list_head *hash_head = chainhashentry(chain_key);
2025 struct lock_chain *chain;
Hong Zhiguobfaf4af2013-04-04 15:01:21 +08002026 struct held_lock *hlock_curr;
Steven Rostedte0944ee2011-04-20 21:42:00 -04002027 int i, j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002028
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002029 /*
2030 * We might need to take the graph lock, ensure we've got IRQs
2031 * disabled to make this an IRQ-safe lock.. for recursion reasons
2032 * lockdep won't complain about its own locking errors.
2033 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08002034 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2035 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002036 /*
2037 * We can walk it lock-free, because entries only get added
2038 * to the hash:
2039 */
Peter Zijlstra35a93932015-02-26 16:23:11 +01002040 list_for_each_entry_rcu(chain, hash_head, entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002041 if (chain->chain_key == chain_key) {
2042cache_hit:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002043 debug_atomic_inc(chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08002044 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08002045 printk("\nhash chain already cached, key: "
2046 "%016Lx tail class: [%p] %s\n",
2047 (unsigned long long)chain_key,
2048 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002049 return 0;
2050 }
2051 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08002052 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08002053 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2054 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002055 /*
2056 * Allocate a new chain entry from the static array, and add
2057 * it to the hash:
2058 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08002059 if (!graph_lock())
2060 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002061 /*
2062 * We have to walk the chain again locked - to avoid duplicates:
2063 */
2064 list_for_each_entry(chain, hash_head, entry) {
2065 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002066 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002067 goto cache_hit;
2068 }
2069 }
2070 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002071 if (!debug_locks_off_graph_unlock())
2072 return 0;
2073
Dave Jones2c522832013-04-25 13:40:02 -04002074 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002075 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002076 return 0;
2077 }
2078 chain = lock_chains + nr_lock_chains++;
2079 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08002080 chain->irq_context = hlock->irq_context;
2081 /* Find the first held_lock of current chain */
Huang, Ying443cd502008-06-20 16:39:21 +08002082 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2083 hlock_curr = curr->held_locks + i;
Hong Zhiguobfaf4af2013-04-04 15:01:21 +08002084 if (hlock_curr->irq_context != hlock->irq_context)
Huang, Ying443cd502008-06-20 16:39:21 +08002085 break;
Huang, Ying443cd502008-06-20 16:39:21 +08002086 }
2087 i++;
2088 chain->depth = curr->lockdep_depth + 1 - i;
Steven Rostedte0944ee2011-04-20 21:42:00 -04002089 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2090 chain->base = nr_chain_hlocks;
2091 nr_chain_hlocks += chain->depth;
Huang, Ying443cd502008-06-20 16:39:21 +08002092 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02002093 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08002094 chain_hlocks[chain->base + j] = lock_id;
2095 }
2096 chain_hlocks[chain->base + j] = class - lock_classes;
2097 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002098 list_add_tail_rcu(&chain->entry, hash_head);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002099 debug_atomic_inc(chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002100 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002101
2102 return 1;
2103}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002104
2105static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07002106 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002107{
2108 /*
2109 * Trylock needs to maintain the stack of held locks, but it
2110 * does not add new dependencies, because trylock can be done
2111 * in any order.
2112 *
2113 * We look up the chain_key and do the O(N^2) check and update of
2114 * the dependencies only if this is a new dependency chain.
2115 * (If lookup_chain_cache() returns with 1 it acquires
2116 * graph_lock for us)
2117 */
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01002118 if (!hlock->trylock && hlock->check &&
Huang, Ying443cd502008-06-20 16:39:21 +08002119 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002120 /*
2121 * Check whether last held lock:
2122 *
2123 * - is irq-safe, if this lock is irq-unsafe
2124 * - is softirq-safe, if this lock is hardirq-unsafe
2125 *
2126 * And check whether the new lock's dependency graph
2127 * could lead back to the previous lock.
2128 *
2129 * any of these scenarios could lead to a deadlock. If
2130 * All validations
2131 */
2132 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2133
2134 if (!ret)
2135 return 0;
2136 /*
2137 * Mark recursive read, as we jump over it when
2138 * building dependencies (just like we jump over
2139 * trylock entries):
2140 */
2141 if (ret == 2)
2142 hlock->read = 2;
2143 /*
2144 * Add dependency only if this lock is not the head
2145 * of the chain, and if it's not a secondary read-lock:
2146 */
2147 if (!chain_head && ret != 2)
2148 if (!check_prevs_add(curr, hlock))
2149 return 0;
2150 graph_unlock();
2151 } else
2152 /* after lookup_chain_cache(): */
2153 if (unlikely(!debug_locks))
2154 return 0;
2155
2156 return 1;
2157}
2158#else
2159static inline int validate_chain(struct task_struct *curr,
2160 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002161 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002162{
2163 return 1;
2164}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07002165#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002166
2167/*
2168 * We are building curr_chain_key incrementally, so double-check
2169 * it from scratch, to make sure that it's done correctly:
2170 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002171static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002172{
2173#ifdef CONFIG_DEBUG_LOCKDEP
2174 struct held_lock *hlock, *prev_hlock = NULL;
2175 unsigned int i, id;
2176 u64 chain_key = 0;
2177
2178 for (i = 0; i < curr->lockdep_depth; i++) {
2179 hlock = curr->held_locks + i;
2180 if (chain_key != hlock->prev_chain_key) {
2181 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002182 /*
2183 * We got mighty confused, our chain keys don't match
2184 * with what we expect, someone trample on our task state?
2185 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002186 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002187 curr->lockdep_depth, i,
2188 (unsigned long long)chain_key,
2189 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002190 return;
2191 }
Dave Jonesf82b2172008-08-11 09:30:23 +02002192 id = hlock->class_idx - 1;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002193 /*
2194 * Whoops ran out of static storage again?
2195 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08002196 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2197 return;
2198
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002199 if (prev_hlock && (prev_hlock->irq_context !=
2200 hlock->irq_context))
2201 chain_key = 0;
2202 chain_key = iterate_chain_key(chain_key, id);
2203 prev_hlock = hlock;
2204 }
2205 if (chain_key != curr->curr_chain_key) {
2206 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002207 /*
2208 * More smoking hash instead of calculating it, damn see these
2209 * numbers float.. I bet that a pink elephant stepped on my memory.
2210 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002211 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002212 curr->lockdep_depth, i,
2213 (unsigned long long)chain_key,
2214 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002215 }
2216#endif
2217}
2218
Steven Rostedt282b5c22011-04-20 21:41:59 -04002219static void
2220print_usage_bug_scenario(struct held_lock *lock)
2221{
2222 struct lock_class *class = hlock_class(lock);
2223
2224 printk(" Possible unsafe locking scenario:\n\n");
2225 printk(" CPU0\n");
2226 printk(" ----\n");
2227 printk(" lock(");
2228 __print_lock_name(class);
2229 printk(");\n");
2230 printk(" <Interrupt>\n");
2231 printk(" lock(");
2232 __print_lock_name(class);
2233 printk(");\n");
2234 printk("\n *** DEADLOCK ***\n\n");
2235}
2236
Peter Zijlstra8e182572007-07-19 01:48:54 -07002237static int
2238print_usage_bug(struct task_struct *curr, struct held_lock *this,
2239 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2240{
2241 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2242 return 0;
2243
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002244 printk("\n");
2245 printk("=================================\n");
2246 printk("[ INFO: inconsistent lock state ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002247 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002248 printk("---------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002249
2250 printk("inconsistent {%s} -> {%s} usage.\n",
2251 usage_str[prev_bit], usage_str[new_bit]);
2252
2253 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002254 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002255 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2256 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2257 trace_hardirqs_enabled(curr),
2258 trace_softirqs_enabled(curr));
2259 print_lock(this);
2260
2261 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002262 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002263
2264 print_irqtrace_events(curr);
2265 printk("\nother info that might help us debug this:\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002266 print_usage_bug_scenario(this);
2267
Peter Zijlstra8e182572007-07-19 01:48:54 -07002268 lockdep_print_held_locks(curr);
2269
2270 printk("\nstack backtrace:\n");
2271 dump_stack();
2272
2273 return 0;
2274}
2275
2276/*
2277 * Print out an error if an invalid bit is set:
2278 */
2279static inline int
2280valid_state(struct task_struct *curr, struct held_lock *this,
2281 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2282{
Dave Jonesf82b2172008-08-11 09:30:23 +02002283 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002284 return print_usage_bug(curr, this, bad_bit, new_bit);
2285 return 1;
2286}
2287
2288static int mark_lock(struct task_struct *curr, struct held_lock *this,
2289 enum lock_usage_bit new_bit);
2290
Steven Rostedt81d68a92008-05-12 21:20:42 +02002291#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002292
2293/*
2294 * print irq inversion bug:
2295 */
2296static int
Ming Lei24208ca2009-07-16 15:44:29 +02002297print_irq_inversion_bug(struct task_struct *curr,
2298 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002299 struct held_lock *this, int forwards,
2300 const char *irqclass)
2301{
Steven Rostedtdad3d742011-04-20 21:41:57 -04002302 struct lock_list *entry = other;
2303 struct lock_list *middle = NULL;
2304 int depth;
2305
Ingo Molnar74c383f2006-12-13 00:34:43 -08002306 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002307 return 0;
2308
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002309 printk("\n");
2310 printk("=========================================================\n");
2311 printk("[ INFO: possible irq lock inversion dependency detected ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002312 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002313 printk("---------------------------------------------------------\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002314 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002315 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002316 print_lock(this);
2317 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01002318 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002319 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01002320 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002321 print_lock_name(other->class);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002322 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2323
2324 printk("\nother info that might help us debug this:\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04002325
2326 /* Find a middle lock (if one exists) */
2327 depth = get_lock_depth(other);
2328 do {
2329 if (depth == 0 && (entry != root)) {
2330 printk("lockdep:%s bad path found in chain graph\n", __func__);
2331 break;
2332 }
2333 middle = entry;
2334 entry = get_lock_parent(entry);
2335 depth--;
2336 } while (entry && entry != root && (depth >= 0));
2337 if (forwards)
2338 print_irq_lock_scenario(root, other,
2339 middle ? middle->class : root->class, other->class);
2340 else
2341 print_irq_lock_scenario(other, root,
2342 middle ? middle->class : other->class, root->class);
2343
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002344 lockdep_print_held_locks(curr);
2345
Ming Lei24208ca2009-07-16 15:44:29 +02002346 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2347 if (!save_trace(&root->trace))
2348 return 0;
2349 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002350
2351 printk("\nstack backtrace:\n");
2352 dump_stack();
2353
2354 return 0;
2355}
2356
2357/*
2358 * Prove that in the forwards-direction subgraph starting at <this>
2359 * there is no lock matching <mask>:
2360 */
2361static int
2362check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2363 enum lock_usage_bit bit, const char *irqclass)
2364{
2365 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002366 struct lock_list root;
2367 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002368
Ming Leid7aaba12009-07-16 15:44:29 +02002369 root.parent = NULL;
2370 root.class = hlock_class(this);
2371 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002372 if (ret < 0)
2373 return print_bfs_bug(ret);
2374 if (ret == 1)
2375 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002376
Ming Lei24208ca2009-07-16 15:44:29 +02002377 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002378 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002379}
2380
2381/*
2382 * Prove that in the backwards-direction subgraph starting at <this>
2383 * there is no lock matching <mask>:
2384 */
2385static int
2386check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2387 enum lock_usage_bit bit, const char *irqclass)
2388{
2389 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002390 struct lock_list root;
2391 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002392
Ming Leid7aaba12009-07-16 15:44:29 +02002393 root.parent = NULL;
2394 root.class = hlock_class(this);
2395 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002396 if (ret < 0)
2397 return print_bfs_bug(ret);
2398 if (ret == 1)
2399 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002400
Ming Lei24208ca2009-07-16 15:44:29 +02002401 return print_irq_inversion_bug(curr, &root, target_entry,
Oleg Nesterov48d50672010-01-26 19:16:41 +01002402 this, 0, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002403}
2404
Ingo Molnar3117df02006-12-13 00:34:43 -08002405void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002406{
2407 printk("irq event stamp: %u\n", curr->irq_events);
2408 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2409 print_ip_sym(curr->hardirq_enable_ip);
2410 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2411 print_ip_sym(curr->hardirq_disable_ip);
2412 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2413 print_ip_sym(curr->softirq_enable_ip);
2414 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2415 print_ip_sym(curr->softirq_disable_ip);
2416}
2417
Peter Zijlstracd953022009-01-22 16:38:21 +01002418static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002419{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002420#if HARDIRQ_VERBOSE
2421 return class_filter(class);
2422#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002423 return 0;
2424}
2425
Peter Zijlstracd953022009-01-22 16:38:21 +01002426static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002427{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002428#if SOFTIRQ_VERBOSE
2429 return class_filter(class);
2430#endif
2431 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002432}
2433
Peter Zijlstracd953022009-01-22 16:38:21 +01002434static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01002435{
2436#if RECLAIM_VERBOSE
2437 return class_filter(class);
2438#endif
2439 return 0;
2440}
2441
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002442#define STRICT_READ_CHECKS 1
2443
Peter Zijlstracd953022009-01-22 16:38:21 +01002444static int (*state_verbose_f[])(struct lock_class *class) = {
2445#define LOCKDEP_STATE(__STATE) \
2446 __STATE##_verbose,
2447#include "lockdep_states.h"
2448#undef LOCKDEP_STATE
2449};
2450
2451static inline int state_verbose(enum lock_usage_bit bit,
2452 struct lock_class *class)
2453{
2454 return state_verbose_f[bit >> 2](class);
2455}
2456
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002457typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2458 enum lock_usage_bit bit, const char *name);
2459
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002460static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002461mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2462 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002463{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002464 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002465 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002466 int dir = new_bit & 2;
2467
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002468 /*
2469 * mark USED_IN has to look forwards -- to ensure no dependency
2470 * has ENABLED state, which would allow recursion deadlocks.
2471 *
2472 * mark ENABLED has to look backwards -- to ensure no dependee
2473 * has USED_IN state, which, again, would allow recursion deadlocks.
2474 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002475 check_usage_f usage = dir ?
2476 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002477
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002478 /*
2479 * Validate that this particular lock does not have conflicting
2480 * usage states.
2481 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002482 if (!valid_state(curr, this, new_bit, excl_bit))
2483 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002484
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002485 /*
2486 * Validate that the lock dependencies don't have conflicting usage
2487 * states.
2488 */
2489 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002490 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002491 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002492
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002493 /*
2494 * Check for read in write conflicts
2495 */
2496 if (!read) {
2497 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2498 return 0;
2499
2500 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002501 !usage(curr, this, excl_bit + 1,
2502 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002503 return 0;
2504 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002505
Peter Zijlstracd953022009-01-22 16:38:21 +01002506 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002507 return 2;
2508
2509 return 1;
2510}
2511
Nick Piggincf40bd12009-01-21 08:12:39 +01002512enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002513#define LOCKDEP_STATE(__STATE) __STATE,
2514#include "lockdep_states.h"
2515#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002516};
2517
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002518/*
2519 * Mark all held locks with a usage bit:
2520 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002521static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002522mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002523{
2524 enum lock_usage_bit usage_bit;
2525 struct held_lock *hlock;
2526 int i;
2527
2528 for (i = 0; i < curr->lockdep_depth; i++) {
2529 hlock = curr->held_locks + i;
2530
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002531 usage_bit = 2 + (mark << 2); /* ENABLED */
2532 if (hlock->read)
2533 usage_bit += 1; /* READ */
2534
2535 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002536
Oleg Nesterov34d0ed52014-01-20 19:20:13 +01002537 if (!hlock->check)
Peter Zijlstraefbe2ee2011-07-07 11:39:45 +02002538 continue;
2539
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002540 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002541 return 0;
2542 }
2543
2544 return 1;
2545}
2546
2547/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002548 * Hardirqs will be enabled:
2549 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002550static void __trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002551{
2552 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002553
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002554 /* we'll do an OFF -> ON transition: */
2555 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002556
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002557 /*
2558 * We are going to turn hardirqs on, so set the
2559 * usage bit for all held locks:
2560 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002561 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002562 return;
2563 /*
2564 * If we have softirqs enabled, then set the usage
2565 * bit for all held locks. (disabled hardirqs prevented
2566 * this bit from being set before)
2567 */
2568 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002569 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002570 return;
2571
2572 curr->hardirq_enable_ip = ip;
2573 curr->hardirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002574 debug_atomic_inc(hardirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002575}
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002576
Andi Kleenb35f8302014-02-08 08:52:02 +01002577__visible void trace_hardirqs_on_caller(unsigned long ip)
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002578{
2579 time_hardirqs_on(CALLER_ADDR0, ip);
2580
2581 if (unlikely(!debug_locks || current->lockdep_recursion))
2582 return;
2583
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002584 if (unlikely(current->hardirqs_enabled)) {
2585 /*
2586 * Neither irq nor preemption are disabled here
2587 * so this is racy by nature but losing one hit
2588 * in a stat is not a big deal.
2589 */
2590 __debug_atomic_inc(redundant_hardirqs_on);
2591 return;
2592 }
2593
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002594 /*
2595 * We're enabling irqs and according to our state above irqs weren't
2596 * already enabled, yet we find the hardware thinks they are in fact
2597 * enabled.. someone messed up their IRQ state tracing.
2598 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002599 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2600 return;
2601
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002602 /*
2603 * See the fine text that goes along with this variable definition.
2604 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002605 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2606 return;
2607
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002608 /*
2609 * Can't allow enabling interrupts while in an interrupt handler,
2610 * that's general bad form and such. Recursion, limited stack etc..
2611 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002612 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2613 return;
2614
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002615 current->lockdep_recursion = 1;
2616 __trace_hardirqs_on_caller(ip);
2617 current->lockdep_recursion = 0;
2618}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002619EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002620
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002621void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002622{
2623 trace_hardirqs_on_caller(CALLER_ADDR0);
2624}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002625EXPORT_SYMBOL(trace_hardirqs_on);
2626
2627/*
2628 * Hardirqs were disabled:
2629 */
Andi Kleenb35f8302014-02-08 08:52:02 +01002630__visible void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002631{
2632 struct task_struct *curr = current;
2633
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002634 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002635
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002636 if (unlikely(!debug_locks || current->lockdep_recursion))
2637 return;
2638
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002639 /*
2640 * So we're supposed to get called after you mask local IRQs, but for
2641 * some reason the hardware doesn't quite think you did a proper job.
2642 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002643 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2644 return;
2645
2646 if (curr->hardirqs_enabled) {
2647 /*
2648 * We have done an ON -> OFF transition:
2649 */
2650 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002651 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002652 curr->hardirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002653 debug_atomic_inc(hardirqs_off_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002654 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002655 debug_atomic_inc(redundant_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002656}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002657EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002658
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002659void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002660{
2661 trace_hardirqs_off_caller(CALLER_ADDR0);
2662}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002663EXPORT_SYMBOL(trace_hardirqs_off);
2664
2665/*
2666 * Softirqs will be enabled:
2667 */
2668void trace_softirqs_on(unsigned long ip)
2669{
2670 struct task_struct *curr = current;
2671
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002672 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002673 return;
2674
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002675 /*
2676 * We fancy IRQs being disabled here, see softirq.c, avoids
2677 * funny state and nesting things.
2678 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002679 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2680 return;
2681
2682 if (curr->softirqs_enabled) {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002683 debug_atomic_inc(redundant_softirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002684 return;
2685 }
2686
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002687 current->lockdep_recursion = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002688 /*
2689 * We'll do an OFF -> ON transition:
2690 */
2691 curr->softirqs_enabled = 1;
2692 curr->softirq_enable_ip = ip;
2693 curr->softirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002694 debug_atomic_inc(softirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002695 /*
2696 * We are going to turn softirqs on, so set the
2697 * usage bit for all held locks, if hardirqs are
2698 * enabled too:
2699 */
2700 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002701 mark_held_locks(curr, SOFTIRQ);
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002702 current->lockdep_recursion = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002703}
2704
2705/*
2706 * Softirqs were disabled:
2707 */
2708void trace_softirqs_off(unsigned long ip)
2709{
2710 struct task_struct *curr = current;
2711
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002712 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002713 return;
2714
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002715 /*
2716 * We fancy IRQs being disabled here, see softirq.c
2717 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002718 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2719 return;
2720
2721 if (curr->softirqs_enabled) {
2722 /*
2723 * We have done an ON -> OFF transition:
2724 */
2725 curr->softirqs_enabled = 0;
2726 curr->softirq_disable_ip = ip;
2727 curr->softirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002728 debug_atomic_inc(softirqs_off_events);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002729 /*
2730 * Whoops, we wanted softirqs off, so why aren't they?
2731 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002732 DEBUG_LOCKS_WARN_ON(!softirq_count());
2733 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002734 debug_atomic_inc(redundant_softirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002735}
2736
Peter Zijlstra2f850182009-03-20 11:13:20 +01002737static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
Nick Piggincf40bd12009-01-21 08:12:39 +01002738{
2739 struct task_struct *curr = current;
2740
2741 if (unlikely(!debug_locks))
2742 return;
2743
2744 /* no reclaim without waiting on it */
Mel Gormand0164ad2015-11-06 16:28:21 -08002745 if (!(gfp_mask & __GFP_DIRECT_RECLAIM))
Nick Piggincf40bd12009-01-21 08:12:39 +01002746 return;
2747
2748 /* this guy won't enter reclaim */
2749 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2750 return;
2751
2752 /* We're only interested __GFP_FS allocations for now */
2753 if (!(gfp_mask & __GFP_FS))
2754 return;
2755
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002756 /*
2757 * Oi! Can't be having __GFP_FS allocations with IRQs disabled.
2758 */
Peter Zijlstra2f850182009-03-20 11:13:20 +01002759 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
Nick Piggincf40bd12009-01-21 08:12:39 +01002760 return;
2761
2762 mark_held_locks(curr, RECLAIM_FS);
2763}
2764
Peter Zijlstra2f850182009-03-20 11:13:20 +01002765static void check_flags(unsigned long flags);
2766
2767void lockdep_trace_alloc(gfp_t gfp_mask)
2768{
2769 unsigned long flags;
2770
2771 if (unlikely(current->lockdep_recursion))
2772 return;
2773
2774 raw_local_irq_save(flags);
2775 check_flags(flags);
2776 current->lockdep_recursion = 1;
2777 __lockdep_trace_alloc(gfp_mask, flags);
2778 current->lockdep_recursion = 0;
2779 raw_local_irq_restore(flags);
2780}
2781
Peter Zijlstra8e182572007-07-19 01:48:54 -07002782static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2783{
2784 /*
2785 * If non-trylock use in a hardirq or softirq context, then
2786 * mark the lock as used in these contexts:
2787 */
2788 if (!hlock->trylock) {
2789 if (hlock->read) {
2790 if (curr->hardirq_context)
2791 if (!mark_lock(curr, hlock,
2792 LOCK_USED_IN_HARDIRQ_READ))
2793 return 0;
2794 if (curr->softirq_context)
2795 if (!mark_lock(curr, hlock,
2796 LOCK_USED_IN_SOFTIRQ_READ))
2797 return 0;
2798 } else {
2799 if (curr->hardirq_context)
2800 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2801 return 0;
2802 if (curr->softirq_context)
2803 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2804 return 0;
2805 }
2806 }
2807 if (!hlock->hardirqs_off) {
2808 if (hlock->read) {
2809 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002810 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002811 return 0;
2812 if (curr->softirqs_enabled)
2813 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002814 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002815 return 0;
2816 } else {
2817 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002818 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002819 return 0;
2820 if (curr->softirqs_enabled)
2821 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002822 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002823 return 0;
2824 }
2825 }
2826
Nick Piggincf40bd12009-01-21 08:12:39 +01002827 /*
2828 * We reuse the irq context infrastructure more broadly as a general
2829 * context checking code. This tests GFP_FS recursion (a lock taken
2830 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2831 * allocation).
2832 */
2833 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2834 if (hlock->read) {
2835 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2836 return 0;
2837 } else {
2838 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2839 return 0;
2840 }
2841 }
2842
Peter Zijlstra8e182572007-07-19 01:48:54 -07002843 return 1;
2844}
2845
2846static int separate_irq_context(struct task_struct *curr,
2847 struct held_lock *hlock)
2848{
2849 unsigned int depth = curr->lockdep_depth;
2850
2851 /*
2852 * Keep track of points where we cross into an interrupt context:
2853 */
2854 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2855 curr->softirq_context;
2856 if (depth) {
2857 struct held_lock *prev_hlock;
2858
2859 prev_hlock = curr->held_locks + depth-1;
2860 /*
2861 * If we cross into another context, reset the
2862 * hash key (this also prevents the checking and the
2863 * adding of the dependency to 'prev'):
2864 */
2865 if (prev_hlock->irq_context != hlock->irq_context)
2866 return 1;
2867 }
2868 return 0;
2869}
2870
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002871#else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002872
2873static inline
2874int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2875 enum lock_usage_bit new_bit)
2876{
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002877 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002878 return 1;
2879}
2880
2881static inline int mark_irqflags(struct task_struct *curr,
2882 struct held_lock *hlock)
2883{
2884 return 1;
2885}
2886
2887static inline int separate_irq_context(struct task_struct *curr,
2888 struct held_lock *hlock)
2889{
2890 return 0;
2891}
2892
Peter Zijlstra868a23a2009-02-15 00:25:21 +01002893void lockdep_trace_alloc(gfp_t gfp_mask)
2894{
2895}
2896
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002897#endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002898
2899/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002900 * Mark a lock with a usage bit, and validate the state transition:
2901 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002902static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002903 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002904{
2905 unsigned int new_mask = 1 << new_bit, ret = 1;
2906
2907 /*
2908 * If already set then do not dirty the cacheline,
2909 * nor do any checks:
2910 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002911 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002912 return 1;
2913
2914 if (!graph_lock())
2915 return 0;
2916 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002917 * Make sure we didn't race:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002918 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002919 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002920 graph_unlock();
2921 return 1;
2922 }
2923
Dave Jonesf82b2172008-08-11 09:30:23 +02002924 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002925
Dave Jonesf82b2172008-08-11 09:30:23 +02002926 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002927 return 0;
2928
2929 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002930#define LOCKDEP_STATE(__STATE) \
2931 case LOCK_USED_IN_##__STATE: \
2932 case LOCK_USED_IN_##__STATE##_READ: \
2933 case LOCK_ENABLED_##__STATE: \
2934 case LOCK_ENABLED_##__STATE##_READ:
2935#include "lockdep_states.h"
2936#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002937 ret = mark_lock_irq(curr, this, new_bit);
2938 if (!ret)
2939 return 0;
2940 break;
2941 case LOCK_USED:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002942 debug_atomic_dec(nr_unused_locks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002943 break;
2944 default:
2945 if (!debug_locks_off_graph_unlock())
2946 return 0;
2947 WARN_ON(1);
2948 return 0;
2949 }
2950
2951 graph_unlock();
2952
2953 /*
2954 * We must printk outside of the graph_lock:
2955 */
2956 if (ret == 2) {
2957 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2958 print_lock(this);
2959 print_irqtrace_events(curr);
2960 dump_stack();
2961 }
2962
2963 return ret;
2964}
2965
2966/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002967 * Initialize a lock instance's lock-class mapping info:
2968 */
2969void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002970 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002971{
Yong Zhangd3d03d42011-11-09 16:04:51 +08002972 int i;
2973
2974 kmemcheck_mark_initialized(lock, sizeof(*lock));
2975
2976 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
2977 lock->class_cache[i] = NULL;
Hitoshi Mitake62016252010-10-05 18:01:51 +09002978
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002979#ifdef CONFIG_LOCK_STAT
2980 lock->cpu = raw_smp_processor_id();
2981#endif
2982
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002983 /*
2984 * Can't be having no nameless bastards around this place!
2985 */
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002986 if (DEBUG_LOCKS_WARN_ON(!name)) {
2987 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002988 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002989 }
2990
2991 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002992
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002993 /*
2994 * No key, no joy, we need to hash something.
2995 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002996 if (DEBUG_LOCKS_WARN_ON(!key))
2997 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002998 /*
2999 * Sanity check, the lock-class key must be persistent:
3000 */
3001 if (!static_obj(key)) {
3002 printk("BUG: key %p not in .data!\n", key);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003003 /*
3004 * What it says above ^^^^^, I suggest you read it.
3005 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003006 DEBUG_LOCKS_WARN_ON(1);
3007 return;
3008 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003009 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003010
3011 if (unlikely(!debug_locks))
3012 return;
3013
Peter Zijlstra35a93932015-02-26 16:23:11 +01003014 if (subclass) {
3015 unsigned long flags;
3016
3017 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3018 return;
3019
3020 raw_local_irq_save(flags);
3021 current->lockdep_recursion = 1;
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003022 register_lock_class(lock, subclass, 1);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003023 current->lockdep_recursion = 0;
3024 raw_local_irq_restore(flags);
3025 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003026}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003027EXPORT_SYMBOL_GPL(lockdep_init_map);
3028
Peter Zijlstra1704f472010-03-19 01:37:42 +01003029struct lock_class_key __lockdep_no_validate__;
Kent Overstreetea6749c2012-12-27 22:21:58 -08003030EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003031
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003032static int
3033print_lock_nested_lock_not_held(struct task_struct *curr,
3034 struct held_lock *hlock,
3035 unsigned long ip)
3036{
3037 if (!debug_locks_off())
3038 return 0;
3039 if (debug_locks_silent)
3040 return 0;
3041
3042 printk("\n");
3043 printk("==================================\n");
3044 printk("[ BUG: Nested lock was not taken ]\n");
3045 print_kernel_ident();
3046 printk("----------------------------------\n");
3047
3048 printk("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
3049 print_lock(hlock);
3050
3051 printk("\nbut this task is not holding:\n");
3052 printk("%s\n", hlock->nest_lock->name);
3053
3054 printk("\nstack backtrace:\n");
3055 dump_stack();
3056
3057 printk("\nother info that might help us debug this:\n");
3058 lockdep_print_held_locks(curr);
3059
3060 printk("\nstack backtrace:\n");
3061 dump_stack();
3062
3063 return 0;
3064}
3065
3066static int __lock_is_held(struct lockdep_map *lock);
3067
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003068/*
3069 * This gets called for every mutex_lock*()/spin_lock*() operation.
3070 * We maintain the dependency maps and validate the locking attempt:
3071 */
3072static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3073 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003074 struct lockdep_map *nest_lock, unsigned long ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003075 int references, int pin_count)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003076{
3077 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07003078 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003079 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003080 unsigned int depth, id;
3081 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003082 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003083 u64 chain_key;
3084
3085 if (unlikely(!debug_locks))
3086 return 0;
3087
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003088 /*
3089 * Lockdep should run with IRQs disabled, otherwise we could
3090 * get an interrupt which would want to take locks, which would
3091 * end up in lockdep and have you got a head-ache already?
3092 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003093 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3094 return 0;
3095
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003096 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3097 check = 0;
Peter Zijlstra1704f472010-03-19 01:37:42 +01003098
Hitoshi Mitake62016252010-10-05 18:01:51 +09003099 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3100 class = lock->class_cache[subclass];
Ingo Molnard6d897c2006-07-10 04:44:04 -07003101 /*
Hitoshi Mitake62016252010-10-05 18:01:51 +09003102 * Not cached?
Ingo Molnard6d897c2006-07-10 04:44:04 -07003103 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003104 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003105 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003106 if (!class)
3107 return 0;
3108 }
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003109 atomic_inc((atomic_t *)&class->ops);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003110 if (very_verbose(class)) {
3111 printk("\nacquire class [%p] %s", class->key, class->name);
3112 if (class->name_version > 1)
3113 printk("#%d", class->name_version);
3114 printk("\n");
3115 dump_stack();
3116 }
3117
3118 /*
3119 * Add the lock to the list of currently held locks.
3120 * (we dont increase the depth just yet, up until the
3121 * dependency checks are done)
3122 */
3123 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003124 /*
3125 * Ran out of static storage for our per-task lock stack again have we?
3126 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003127 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3128 return 0;
3129
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003130 class_idx = class - lock_classes + 1;
3131
3132 if (depth) {
3133 hlock = curr->held_locks + depth - 1;
3134 if (hlock->class_idx == class_idx && nest_lock) {
3135 if (hlock->references)
3136 hlock->references++;
3137 else
3138 hlock->references = 2;
3139
3140 return 1;
3141 }
3142 }
3143
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003144 hlock = curr->held_locks + depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003145 /*
3146 * Plain impossible, we just registered it and checked it weren't no
3147 * NULL like.. I bet this mushroom I ate was good!
3148 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003149 if (DEBUG_LOCKS_WARN_ON(!class))
3150 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003151 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003152 hlock->acquire_ip = ip;
3153 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003154 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003155 hlock->trylock = trylock;
3156 hlock->read = read;
3157 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04003158 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003159 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003160#ifdef CONFIG_LOCK_STAT
3161 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003162 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003163#endif
Peter Zijlstra21199f22015-09-16 16:10:40 +02003164 hlock->pin_count = pin_count;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003165
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003166 if (check && !mark_irqflags(curr, hlock))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003167 return 0;
3168
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003169 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07003170 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003171 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003172
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003173 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01003174 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003175 * lock keys along the dependency chain. We save the hash value
3176 * at every step so that we can get the current hash easily
3177 * after unlock. The chain hash is then used to cache dependency
3178 * results.
3179 *
3180 * The 'key ID' is what is the most compact key value to drive
3181 * the hash, not class->key.
3182 */
3183 id = class - lock_classes;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003184 /*
3185 * Whoops, we did it again.. ran straight out of our static allocation.
3186 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003187 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
3188 return 0;
3189
3190 chain_key = curr->curr_chain_key;
3191 if (!depth) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003192 /*
3193 * How can we have a chain hash when we ain't got no keys?!
3194 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003195 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3196 return 0;
3197 chain_head = 1;
3198 }
3199
3200 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003201 if (separate_irq_context(curr, hlock)) {
3202 chain_key = 0;
3203 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003204 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003205 chain_key = iterate_chain_key(chain_key, id);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003206
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003207 if (nest_lock && !__lock_is_held(nest_lock))
3208 return print_lock_nested_lock_not_held(curr, hlock, ip);
3209
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003210 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003211 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08003212
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003213 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003214 curr->lockdep_depth++;
3215 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08003216#ifdef CONFIG_DEBUG_LOCKDEP
3217 if (unlikely(!debug_locks))
3218 return 0;
3219#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003220 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3221 debug_locks_off();
Dave Jones2c522832013-04-25 13:40:02 -04003222 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3223 printk(KERN_DEBUG "depth: %i max: %lu!\n",
Ben Greearc0540602013-02-06 10:56:19 -08003224 curr->lockdep_depth, MAX_LOCK_DEPTH);
Ben Greearc0540602013-02-06 10:56:19 -08003225
3226 lockdep_print_held_locks(current);
3227 debug_show_all_locks();
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01003228 dump_stack();
Ben Greearc0540602013-02-06 10:56:19 -08003229
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003230 return 0;
3231 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08003232
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003233 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3234 max_lockdep_depth = curr->lockdep_depth;
3235
3236 return 1;
3237}
3238
3239static int
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303240print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003241 unsigned long ip)
3242{
3243 if (!debug_locks_off())
3244 return 0;
3245 if (debug_locks_silent)
3246 return 0;
3247
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003248 printk("\n");
3249 printk("=====================================\n");
3250 printk("[ BUG: bad unlock balance detected! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003251 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003252 printk("-------------------------------------\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003253 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003254 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003255 print_lockdep_cache(lock);
3256 printk(") at:\n");
3257 print_ip_sym(ip);
3258 printk("but there are no more locks to release!\n");
3259 printk("\nother info that might help us debug this:\n");
3260 lockdep_print_held_locks(curr);
3261
3262 printk("\nstack backtrace:\n");
3263 dump_stack();
3264
3265 return 0;
3266}
3267
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003268static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3269{
3270 if (hlock->instance == lock)
3271 return 1;
3272
3273 if (hlock->references) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09003274 struct lock_class *class = lock->class_cache[0];
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003275
3276 if (!class)
3277 class = look_up_lock_class(lock, 0);
3278
Peter Zijlstra80e04012011-08-05 14:26:17 +02003279 /*
3280 * If look_up_lock_class() failed to find a class, we're trying
3281 * to test if we hold a lock that has never yet been acquired.
3282 * Clearly if the lock hasn't been acquired _ever_, we're not
3283 * holding it either, so report failure.
3284 */
3285 if (!class)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003286 return 0;
3287
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003288 /*
3289 * References, but not a lock we're actually ref-counting?
3290 * State got messed up, follow the sites that change ->references
3291 * and try to make sense of it.
3292 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003293 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3294 return 0;
3295
3296 if (hlock->class_idx == class - lock_classes + 1)
3297 return 1;
3298 }
3299
3300 return 0;
3301}
3302
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003303static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003304__lock_set_class(struct lockdep_map *lock, const char *name,
3305 struct lock_class_key *key, unsigned int subclass,
3306 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003307{
3308 struct task_struct *curr = current;
3309 struct held_lock *hlock, *prev_hlock;
3310 struct lock_class *class;
3311 unsigned int depth;
3312 int i;
3313
3314 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003315 /*
3316 * This function is about (re)setting the class of a held lock,
3317 * yet we're not actually holding any locks. Naughty user!
3318 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003319 if (DEBUG_LOCKS_WARN_ON(!depth))
3320 return 0;
3321
3322 prev_hlock = NULL;
3323 for (i = depth-1; i >= 0; i--) {
3324 hlock = curr->held_locks + i;
3325 /*
3326 * We must not cross into another context:
3327 */
3328 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3329 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003330 if (match_held_lock(hlock, lock))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003331 goto found_it;
3332 prev_hlock = hlock;
3333 }
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303334 return print_unlock_imbalance_bug(curr, lock, ip);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003335
3336found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003337 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003338 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02003339 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003340
3341 curr->lockdep_depth = i;
3342 curr->curr_chain_key = hlock->prev_chain_key;
3343
3344 for (; i < depth; i++) {
3345 hlock = curr->held_locks + i;
3346 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003347 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003348 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003349 hlock->nest_lock, hlock->acquire_ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003350 hlock->references, hlock->pin_count))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003351 return 0;
3352 }
3353
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003354 /*
3355 * I took it apart and put it back together again, except now I have
3356 * these 'spare' parts.. where shall I put them.
3357 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003358 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3359 return 0;
3360 return 1;
3361}
3362
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003363/*
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003364 * Remove the lock to the list of currently held locks - this gets
3365 * called on mutex_unlock()/spin_unlock*() (or on a failed
3366 * mutex_lock_interruptible()).
3367 *
3368 * @nested is an hysterical artifact, needs a tree wide cleanup.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003369 */
3370static int
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003371__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003372{
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003373 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003374 struct held_lock *hlock, *prev_hlock;
3375 unsigned int depth;
3376 int i;
3377
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003378 if (unlikely(!debug_locks))
3379 return 0;
3380
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003381 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003382 /*
3383 * So we're all set to release this lock.. wait what lock? We don't
3384 * own any locks, you've been drinking again?
3385 */
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003386 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3387 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003388
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003389 /*
3390 * Check whether the lock exists in the current stack
3391 * of held locks:
3392 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003393 prev_hlock = NULL;
3394 for (i = depth-1; i >= 0; i--) {
3395 hlock = curr->held_locks + i;
3396 /*
3397 * We must not cross into another context:
3398 */
3399 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3400 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003401 if (match_held_lock(hlock, lock))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003402 goto found_it;
3403 prev_hlock = hlock;
3404 }
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303405 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003406
3407found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003408 if (hlock->instance == lock)
3409 lock_release_holdtime(hlock);
3410
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003411 WARN(hlock->pin_count, "releasing a pinned lock\n");
3412
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003413 if (hlock->references) {
3414 hlock->references--;
3415 if (hlock->references) {
3416 /*
3417 * We had, and after removing one, still have
3418 * references, the current lock stack is still
3419 * valid. We're done!
3420 */
3421 return 1;
3422 }
3423 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003424
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003425 /*
3426 * We have the right lock to unlock, 'hlock' points to it.
3427 * Now we remove it from the stack, and add back the other
3428 * entries (if any), recalculating the hash along the way:
3429 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003430
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003431 curr->lockdep_depth = i;
3432 curr->curr_chain_key = hlock->prev_chain_key;
3433
3434 for (i++; i < depth; i++) {
3435 hlock = curr->held_locks + i;
3436 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003437 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003438 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003439 hlock->nest_lock, hlock->acquire_ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003440 hlock->references, hlock->pin_count))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003441 return 0;
3442 }
3443
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003444 /*
3445 * We had N bottles of beer on the wall, we drank one, but now
3446 * there's not N-1 bottles of beer left on the wall...
3447 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003448 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3449 return 0;
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003450
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003451 return 1;
3452}
3453
Peter Zijlstraf607c662009-07-20 19:16:29 +02003454static int __lock_is_held(struct lockdep_map *lock)
3455{
3456 struct task_struct *curr = current;
3457 int i;
3458
3459 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003460 struct held_lock *hlock = curr->held_locks + i;
3461
3462 if (match_held_lock(hlock, lock))
Peter Zijlstraf607c662009-07-20 19:16:29 +02003463 return 1;
3464 }
3465
3466 return 0;
3467}
3468
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003469static void __lock_pin_lock(struct lockdep_map *lock)
3470{
3471 struct task_struct *curr = current;
3472 int i;
3473
3474 if (unlikely(!debug_locks))
3475 return;
3476
3477 for (i = 0; i < curr->lockdep_depth; i++) {
3478 struct held_lock *hlock = curr->held_locks + i;
3479
3480 if (match_held_lock(hlock, lock)) {
3481 hlock->pin_count++;
3482 return;
3483 }
3484 }
3485
3486 WARN(1, "pinning an unheld lock\n");
3487}
3488
3489static void __lock_unpin_lock(struct lockdep_map *lock)
3490{
3491 struct task_struct *curr = current;
3492 int i;
3493
3494 if (unlikely(!debug_locks))
3495 return;
3496
3497 for (i = 0; i < curr->lockdep_depth; i++) {
3498 struct held_lock *hlock = curr->held_locks + i;
3499
3500 if (match_held_lock(hlock, lock)) {
3501 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
3502 return;
3503
3504 hlock->pin_count--;
3505 return;
3506 }
3507 }
3508
3509 WARN(1, "unpinning an unheld lock\n");
3510}
3511
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003512/*
3513 * Check whether we follow the irq-flags state precisely:
3514 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003515static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003516{
Ingo Molnar992860e2008-07-14 10:28:38 +02003517#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3518 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003519 if (!debug_locks)
3520 return;
3521
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01003522 if (irqs_disabled_flags(flags)) {
3523 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3524 printk("possible reason: unannotated irqs-off.\n");
3525 }
3526 } else {
3527 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3528 printk("possible reason: unannotated irqs-on.\n");
3529 }
3530 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003531
3532 /*
3533 * We dont accurately track softirq state in e.g.
3534 * hardirq contexts (such as on 4KSTACKS), so only
3535 * check if not in hardirq contexts:
3536 */
3537 if (!hardirq_count()) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003538 if (softirq_count()) {
3539 /* like the above, but with softirqs */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003540 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003541 } else {
3542 /* lick the above, does it taste good? */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003543 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003544 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003545 }
3546
3547 if (!debug_locks)
3548 print_irqtrace_events(current);
3549#endif
3550}
3551
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003552void lock_set_class(struct lockdep_map *lock, const char *name,
3553 struct lock_class_key *key, unsigned int subclass,
3554 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003555{
3556 unsigned long flags;
3557
3558 if (unlikely(current->lockdep_recursion))
3559 return;
3560
3561 raw_local_irq_save(flags);
3562 current->lockdep_recursion = 1;
3563 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003564 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003565 check_chain_key(current);
3566 current->lockdep_recursion = 0;
3567 raw_local_irq_restore(flags);
3568}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003569EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003570
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003571/*
3572 * We are not always called with irqs disabled - do that here,
3573 * and also avoid lockdep recursion:
3574 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003575void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003576 int trylock, int read, int check,
3577 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003578{
3579 unsigned long flags;
3580
3581 if (unlikely(current->lockdep_recursion))
3582 return;
3583
3584 raw_local_irq_save(flags);
3585 check_flags(flags);
3586
3587 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003588 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003589 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003590 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003591 current->lockdep_recursion = 0;
3592 raw_local_irq_restore(flags);
3593}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003594EXPORT_SYMBOL_GPL(lock_acquire);
3595
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003596void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003597 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003598{
3599 unsigned long flags;
3600
3601 if (unlikely(current->lockdep_recursion))
3602 return;
3603
3604 raw_local_irq_save(flags);
3605 check_flags(flags);
3606 current->lockdep_recursion = 1;
Frederic Weisbecker93135432010-05-08 06:24:25 +02003607 trace_lock_release(lock, ip);
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003608 if (__lock_release(lock, nested, ip))
3609 check_chain_key(current);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003610 current->lockdep_recursion = 0;
3611 raw_local_irq_restore(flags);
3612}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003613EXPORT_SYMBOL_GPL(lock_release);
3614
Peter Zijlstraf607c662009-07-20 19:16:29 +02003615int lock_is_held(struct lockdep_map *lock)
3616{
3617 unsigned long flags;
3618 int ret = 0;
3619
3620 if (unlikely(current->lockdep_recursion))
Peter Zijlstraf2513cd2011-06-06 12:32:43 +02003621 return 1; /* avoid false negative lockdep_assert_held() */
Peter Zijlstraf607c662009-07-20 19:16:29 +02003622
3623 raw_local_irq_save(flags);
3624 check_flags(flags);
3625
3626 current->lockdep_recursion = 1;
3627 ret = __lock_is_held(lock);
3628 current->lockdep_recursion = 0;
3629 raw_local_irq_restore(flags);
3630
3631 return ret;
3632}
3633EXPORT_SYMBOL_GPL(lock_is_held);
3634
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003635void lock_pin_lock(struct lockdep_map *lock)
3636{
3637 unsigned long flags;
3638
3639 if (unlikely(current->lockdep_recursion))
3640 return;
3641
3642 raw_local_irq_save(flags);
3643 check_flags(flags);
3644
3645 current->lockdep_recursion = 1;
3646 __lock_pin_lock(lock);
3647 current->lockdep_recursion = 0;
3648 raw_local_irq_restore(flags);
3649}
3650EXPORT_SYMBOL_GPL(lock_pin_lock);
3651
3652void lock_unpin_lock(struct lockdep_map *lock)
3653{
3654 unsigned long flags;
3655
3656 if (unlikely(current->lockdep_recursion))
3657 return;
3658
3659 raw_local_irq_save(flags);
3660 check_flags(flags);
3661
3662 current->lockdep_recursion = 1;
3663 __lock_unpin_lock(lock);
3664 current->lockdep_recursion = 0;
3665 raw_local_irq_restore(flags);
3666}
3667EXPORT_SYMBOL_GPL(lock_unpin_lock);
3668
Nick Piggincf40bd12009-01-21 08:12:39 +01003669void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3670{
3671 current->lockdep_reclaim_gfp = gfp_mask;
3672}
3673
3674void lockdep_clear_current_reclaim_state(void)
3675{
3676 current->lockdep_reclaim_gfp = 0;
3677}
3678
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003679#ifdef CONFIG_LOCK_STAT
3680static int
3681print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3682 unsigned long ip)
3683{
3684 if (!debug_locks_off())
3685 return 0;
3686 if (debug_locks_silent)
3687 return 0;
3688
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003689 printk("\n");
3690 printk("=================================\n");
3691 printk("[ BUG: bad contention detected! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003692 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003693 printk("---------------------------------\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003694 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003695 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003696 print_lockdep_cache(lock);
3697 printk(") at:\n");
3698 print_ip_sym(ip);
3699 printk("but there are no locks held!\n");
3700 printk("\nother info that might help us debug this:\n");
3701 lockdep_print_held_locks(curr);
3702
3703 printk("\nstack backtrace:\n");
3704 dump_stack();
3705
3706 return 0;
3707}
3708
3709static void
3710__lock_contended(struct lockdep_map *lock, unsigned long ip)
3711{
3712 struct task_struct *curr = current;
3713 struct held_lock *hlock, *prev_hlock;
3714 struct lock_class_stats *stats;
3715 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003716 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003717
3718 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003719 /*
3720 * Whee, we contended on this lock, except it seems we're not
3721 * actually trying to acquire anything much at all..
3722 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003723 if (DEBUG_LOCKS_WARN_ON(!depth))
3724 return;
3725
3726 prev_hlock = NULL;
3727 for (i = depth-1; i >= 0; i--) {
3728 hlock = curr->held_locks + i;
3729 /*
3730 * We must not cross into another context:
3731 */
3732 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3733 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003734 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003735 goto found_it;
3736 prev_hlock = hlock;
3737 }
3738 print_lock_contention_bug(curr, lock, ip);
3739 return;
3740
3741found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003742 if (hlock->instance != lock)
3743 return;
3744
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003745 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003746
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003747 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3748 contending_point = lock_point(hlock_class(hlock)->contending_point,
3749 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003750
Dave Jonesf82b2172008-08-11 09:30:23 +02003751 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003752 if (contention_point < LOCKSTAT_POINTS)
3753 stats->contention_point[contention_point]++;
3754 if (contending_point < LOCKSTAT_POINTS)
3755 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003756 if (lock->cpu != smp_processor_id())
3757 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003758 put_lock_stats(stats);
3759}
3760
3761static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003762__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003763{
3764 struct task_struct *curr = current;
3765 struct held_lock *hlock, *prev_hlock;
3766 struct lock_class_stats *stats;
3767 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003768 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07003769 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003770
3771 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003772 /*
3773 * Yay, we acquired ownership of this lock we didn't try to
3774 * acquire, how the heck did that happen?
3775 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003776 if (DEBUG_LOCKS_WARN_ON(!depth))
3777 return;
3778
3779 prev_hlock = NULL;
3780 for (i = depth-1; i >= 0; i--) {
3781 hlock = curr->held_locks + i;
3782 /*
3783 * We must not cross into another context:
3784 */
3785 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3786 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003787 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003788 goto found_it;
3789 prev_hlock = hlock;
3790 }
3791 print_lock_contention_bug(curr, lock, _RET_IP_);
3792 return;
3793
3794found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003795 if (hlock->instance != lock)
3796 return;
3797
Peter Zijlstra96645672007-07-19 01:49:00 -07003798 cpu = smp_processor_id();
3799 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003800 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07003801 waittime = now - hlock->waittime_stamp;
3802 hlock->holdtime_stamp = now;
3803 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003804
Frederic Weisbecker883a2a32010-05-08 06:16:11 +02003805 trace_lock_acquired(lock, ip);
Frederic Weisbecker20625012009-04-06 01:49:33 +02003806
Dave Jonesf82b2172008-08-11 09:30:23 +02003807 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003808 if (waittime) {
3809 if (hlock->read)
3810 lock_time_inc(&stats->read_waittime, waittime);
3811 else
3812 lock_time_inc(&stats->write_waittime, waittime);
3813 }
3814 if (lock->cpu != cpu)
3815 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003816 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003817
3818 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003819 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003820}
3821
3822void lock_contended(struct lockdep_map *lock, unsigned long ip)
3823{
3824 unsigned long flags;
3825
3826 if (unlikely(!lock_stat))
3827 return;
3828
3829 if (unlikely(current->lockdep_recursion))
3830 return;
3831
3832 raw_local_irq_save(flags);
3833 check_flags(flags);
3834 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003835 trace_lock_contended(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003836 __lock_contended(lock, ip);
3837 current->lockdep_recursion = 0;
3838 raw_local_irq_restore(flags);
3839}
3840EXPORT_SYMBOL_GPL(lock_contended);
3841
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003842void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003843{
3844 unsigned long flags;
3845
3846 if (unlikely(!lock_stat))
3847 return;
3848
3849 if (unlikely(current->lockdep_recursion))
3850 return;
3851
3852 raw_local_irq_save(flags);
3853 check_flags(flags);
3854 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003855 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003856 current->lockdep_recursion = 0;
3857 raw_local_irq_restore(flags);
3858}
3859EXPORT_SYMBOL_GPL(lock_acquired);
3860#endif
3861
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003862/*
3863 * Used by the testsuite, sanitize the validator state
3864 * after a simulated failure:
3865 */
3866
3867void lockdep_reset(void)
3868{
3869 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003870 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003871
3872 raw_local_irq_save(flags);
3873 current->curr_chain_key = 0;
3874 current->lockdep_depth = 0;
3875 current->lockdep_recursion = 0;
3876 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3877 nr_hardirq_chains = 0;
3878 nr_softirq_chains = 0;
3879 nr_process_chains = 0;
3880 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003881 for (i = 0; i < CHAINHASH_SIZE; i++)
3882 INIT_LIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003883 raw_local_irq_restore(flags);
3884}
3885
3886static void zap_class(struct lock_class *class)
3887{
3888 int i;
3889
3890 /*
3891 * Remove all dependencies this lock is
3892 * involved in:
3893 */
3894 for (i = 0; i < nr_list_entries; i++) {
3895 if (list_entries[i].class == class)
3896 list_del_rcu(&list_entries[i].entry);
3897 }
3898 /*
3899 * Unhash the class and remove it from the all_lock_classes list:
3900 */
3901 list_del_rcu(&class->hash_entry);
3902 list_del_rcu(&class->lock_entry);
3903
Peter Zijlstracee34d82015-06-02 12:50:13 +02003904 RCU_INIT_POINTER(class->key, NULL);
3905 RCU_INIT_POINTER(class->name, NULL);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003906}
3907
Arjan van de Venfabe8742008-01-24 07:00:45 +01003908static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003909{
3910 return addr >= start && addr < start + size;
3911}
3912
Peter Zijlstra35a93932015-02-26 16:23:11 +01003913/*
3914 * Used in module.c to remove lock classes from memory that is going to be
3915 * freed; and possibly re-used by other modules.
3916 *
3917 * We will have had one sync_sched() before getting here, so we're guaranteed
3918 * nobody will look up these exact classes -- they're properly dead but still
3919 * allocated.
3920 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003921void lockdep_free_key_range(void *start, unsigned long size)
3922{
Peter Zijlstra35a93932015-02-26 16:23:11 +01003923 struct lock_class *class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003924 struct list_head *head;
3925 unsigned long flags;
3926 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003927 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003928
3929 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003930 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003931
3932 /*
3933 * Unhash all classes that were created by this module:
3934 */
3935 for (i = 0; i < CLASSHASH_SIZE; i++) {
3936 head = classhash_table + i;
3937 if (list_empty(head))
3938 continue;
Peter Zijlstra35a93932015-02-26 16:23:11 +01003939 list_for_each_entry_rcu(class, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003940 if (within(class->key, start, size))
3941 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003942 else if (within(class->name, start, size))
3943 zap_class(class);
3944 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003945 }
3946
Nick Piggin5a26db52008-01-16 09:51:58 +01003947 if (locked)
3948 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003949 raw_local_irq_restore(flags);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003950
3951 /*
3952 * Wait for any possible iterators from look_up_lock_class() to pass
3953 * before continuing to free the memory they refer to.
3954 *
3955 * sync_sched() is sufficient because the read-side is IRQ disable.
3956 */
3957 synchronize_sched();
3958
3959 /*
3960 * XXX at this point we could return the resources to the pool;
3961 * instead we leak them. We would need to change to bitmap allocators
3962 * instead of the linear allocators we have now.
3963 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003964}
3965
3966void lockdep_reset_lock(struct lockdep_map *lock)
3967{
Peter Zijlstra35a93932015-02-26 16:23:11 +01003968 struct lock_class *class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003969 struct list_head *head;
3970 unsigned long flags;
3971 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003972 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003973
3974 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003975
3976 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003977 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003978 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003979 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3980 /*
3981 * If the class exists we look it up and zap it:
3982 */
3983 class = look_up_lock_class(lock, j);
3984 if (class)
3985 zap_class(class);
3986 }
3987 /*
3988 * Debug check: in the end all mapped classes should
3989 * be gone.
3990 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003991 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003992 for (i = 0; i < CLASSHASH_SIZE; i++) {
3993 head = classhash_table + i;
3994 if (list_empty(head))
3995 continue;
Peter Zijlstra35a93932015-02-26 16:23:11 +01003996 list_for_each_entry_rcu(class, head, hash_entry) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09003997 int match = 0;
3998
3999 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
4000 match |= class == lock->class_cache[j];
4001
4002 if (unlikely(match)) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004003 if (debug_locks_off_graph_unlock()) {
4004 /*
4005 * We all just reset everything, how did it match?
4006 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08004007 WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004008 }
Ingo Molnard6d897c2006-07-10 04:44:04 -07004009 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004010 }
4011 }
4012 }
Nick Piggin5a26db52008-01-16 09:51:58 +01004013 if (locked)
4014 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07004015
4016out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004017 raw_local_irq_restore(flags);
4018}
4019
Sam Ravnborg14999932007-02-28 20:12:31 -08004020void lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004021{
4022 int i;
4023
4024 /*
4025 * Some architectures have their own start_kernel()
4026 * code which calls lockdep_init(), while we also
4027 * call lockdep_init() from the start_kernel() itself,
4028 * and we want to initialize the hashes only once:
4029 */
4030 if (lockdep_initialized)
4031 return;
4032
4033 for (i = 0; i < CLASSHASH_SIZE; i++)
4034 INIT_LIST_HEAD(classhash_table + i);
4035
4036 for (i = 0; i < CHAINHASH_SIZE; i++)
4037 INIT_LIST_HEAD(chainhash_table + i);
4038
4039 lockdep_initialized = 1;
4040}
4041
4042void __init lockdep_info(void)
4043{
4044 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4045
Li Zefanb0788ca2008-11-21 15:57:32 +08004046 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004047 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4048 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08004049 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004050 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4051 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4052 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4053
4054 printk(" memory used by lock dependency info: %lu kB\n",
4055 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
4056 sizeof(struct list_head) * CLASSHASH_SIZE +
4057 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
4058 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei90629202009-08-02 21:43:36 +08004059 sizeof(struct list_head) * CHAINHASH_SIZE
Ming Lei4dd861d2009-07-16 15:44:29 +02004060#ifdef CONFIG_PROVE_LOCKING
Ming Leie351b662009-07-22 22:48:09 +08004061 + sizeof(struct circular_queue)
Ming Lei4dd861d2009-07-16 15:44:29 +02004062#endif
Ming Lei90629202009-08-02 21:43:36 +08004063 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02004064 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004065
4066 printk(" per task-struct memory footprint: %lu bytes\n",
4067 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
4068
4069#ifdef CONFIG_DEBUG_LOCKDEP
Johannes Bergc71063c2007-07-19 01:49:02 -07004070 if (lockdep_init_error) {
Borislav Petkov92ae1832015-06-02 15:38:27 +02004071 printk("WARNING: lockdep init error: lock '%s' was acquired before lockdep_init().\n", lock_init_error);
Johannes Bergc71063c2007-07-19 01:49:02 -07004072 printk("Call stack leading to lockdep invocation was:\n");
4073 print_stack_trace(&lockdep_init_trace, 0);
4074 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004075#endif
4076}
4077
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004078static void
4079print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07004080 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004081{
4082 if (!debug_locks_off())
4083 return;
4084 if (debug_locks_silent)
4085 return;
4086
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004087 printk("\n");
4088 printk("=========================\n");
4089 printk("[ BUG: held lock freed! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004090 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004091 printk("-------------------------\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004092 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004093 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07004094 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004095 lockdep_print_held_locks(curr);
4096
4097 printk("\nstack backtrace:\n");
4098 dump_stack();
4099}
4100
Oleg Nesterov54561782007-12-05 15:46:09 +01004101static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4102 const void* lock_from, unsigned long lock_len)
4103{
4104 return lock_from + lock_len <= mem_from ||
4105 mem_from + mem_len <= lock_from;
4106}
4107
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004108/*
4109 * Called when kernel memory is freed (or unmapped), or if a lock
4110 * is destroyed or reinitialized - this code checks whether there is
4111 * any held lock in the memory range of <from> to <to>:
4112 */
4113void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4114{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004115 struct task_struct *curr = current;
4116 struct held_lock *hlock;
4117 unsigned long flags;
4118 int i;
4119
4120 if (unlikely(!debug_locks))
4121 return;
4122
4123 local_irq_save(flags);
4124 for (i = 0; i < curr->lockdep_depth; i++) {
4125 hlock = curr->held_locks + i;
4126
Oleg Nesterov54561782007-12-05 15:46:09 +01004127 if (not_in_range(mem_from, mem_len, hlock->instance,
4128 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004129 continue;
4130
Oleg Nesterov54561782007-12-05 15:46:09 +01004131 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004132 break;
4133 }
4134 local_irq_restore(flags);
4135}
Peter Zijlstraed075362006-12-06 20:35:24 -08004136EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004137
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004138static void print_held_locks_bug(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004139{
4140 if (!debug_locks_off())
4141 return;
4142 if (debug_locks_silent)
4143 return;
4144
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004145 printk("\n");
4146 printk("=====================================\n");
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004147 printk("[ BUG: %s/%d still has locks held! ]\n",
4148 current->comm, task_pid_nr(current));
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004149 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004150 printk("-------------------------------------\n");
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004151 lockdep_print_held_locks(current);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004152 printk("\nstack backtrace:\n");
4153 dump_stack();
4154}
4155
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004156void debug_check_no_locks_held(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004157{
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004158 if (unlikely(current->lockdep_depth > 0))
4159 print_held_locks_bug();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004160}
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004161EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004162
Sasha Levin8dce7a92013-06-13 18:41:16 -04004163#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004164void debug_show_all_locks(void)
4165{
4166 struct task_struct *g, *p;
4167 int count = 10;
4168 int unlock = 1;
4169
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004170 if (unlikely(!debug_locks)) {
4171 printk("INFO: lockdep is turned off.\n");
4172 return;
4173 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004174 printk("\nShowing all locks held in the system:\n");
4175
4176 /*
4177 * Here we try to get the tasklist_lock as hard as possible,
4178 * if not successful after 2 seconds we ignore it (but keep
4179 * trying). This is to enable a debug printout even if a
4180 * tasklist_lock-holding task deadlocks or crashes.
4181 */
4182retry:
4183 if (!read_trylock(&tasklist_lock)) {
4184 if (count == 10)
4185 printk("hm, tasklist_lock locked, retrying... ");
4186 if (count) {
4187 count--;
4188 printk(" #%d", 10-count);
4189 mdelay(200);
4190 goto retry;
4191 }
4192 printk(" ignoring it.\n");
4193 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08004194 } else {
4195 if (count != 10)
4196 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004197 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004198
4199 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01004200 /*
4201 * It's not reliable to print a task's held locks
4202 * if it's not sleeping (or if it's not the current
4203 * task):
4204 */
4205 if (p->state == TASK_RUNNING && p != current)
4206 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004207 if (p->lockdep_depth)
4208 lockdep_print_held_locks(p);
4209 if (!unlock)
4210 if (read_trylock(&tasklist_lock))
4211 unlock = 1;
4212 } while_each_thread(g, p);
4213
4214 printk("\n");
4215 printk("=============================================\n\n");
4216
4217 if (unlock)
4218 read_unlock(&tasklist_lock);
4219}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004220EXPORT_SYMBOL_GPL(debug_show_all_locks);
Sasha Levin8dce7a92013-06-13 18:41:16 -04004221#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004222
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004223/*
4224 * Careful: only use this function if you are sure that
4225 * the task cannot run in parallel!
4226 */
John Kacurf1b499f2010-08-05 17:10:53 +02004227void debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004228{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004229 if (unlikely(!debug_locks)) {
4230 printk("INFO: lockdep is turned off.\n");
4231 return;
4232 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004233 lockdep_print_held_locks(task);
4234}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004235EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02004236
Andi Kleen722a9f92014-05-02 00:44:38 +02004237asmlinkage __visible void lockdep_sys_exit(void)
Peter Zijlstrab351d162007-10-11 22:11:12 +02004238{
4239 struct task_struct *curr = current;
4240
4241 if (unlikely(curr->lockdep_depth)) {
4242 if (!debug_locks_off())
4243 return;
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004244 printk("\n");
4245 printk("================================================\n");
4246 printk("[ BUG: lock held when returning to user space! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004247 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004248 printk("------------------------------------------------\n");
Peter Zijlstrab351d162007-10-11 22:11:12 +02004249 printk("%s/%d is leaving the kernel with locks still held!\n",
4250 curr->comm, curr->pid);
4251 lockdep_print_held_locks(curr);
4252 }
4253}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004254
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004255void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004256{
4257 struct task_struct *curr = current;
4258
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004259#ifndef CONFIG_PROVE_RCU_REPEATEDLY
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004260 if (!debug_locks_off())
4261 return;
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004262#endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4263 /* Note: the following can be executed concurrently, so be careful. */
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004264 printk("\n");
4265 printk("===============================\n");
4266 printk("[ INFO: suspicious RCU usage. ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004267 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004268 printk("-------------------------------\n");
4269 printk("%s:%d %s!\n", file, line, s);
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004270 printk("\nother info that might help us debug this:\n\n");
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004271 printk("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4272 !rcu_lockdep_current_cpu_online()
4273 ? "RCU used illegally from offline CPU!\n"
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004274 : !rcu_is_watching()
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004275 ? "RCU used illegally from idle CPU!\n"
4276 : "",
4277 rcu_scheduler_active, debug_locks);
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004278
4279 /*
4280 * If a CPU is in the RCU-free window in idle (ie: in the section
4281 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4282 * considers that CPU to be in an "extended quiescent state",
4283 * which means that RCU will be completely ignoring that CPU.
4284 * Therefore, rcu_read_lock() and friends have absolutely no
4285 * effect on a CPU running in that state. In other words, even if
4286 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4287 * delete data structures out from under it. RCU really has no
4288 * choice here: we need to keep an RCU-free window in idle where
4289 * the CPU may possibly enter into low power mode. This way we can
4290 * notice an extended quiescent state to other CPUs that started a grace
4291 * period. Otherwise we would delay any grace period as long as we run
4292 * in the idle task.
4293 *
4294 * So complain bitterly if someone does call rcu_read_lock(),
4295 * rcu_read_lock_bh() and so on from extended quiescent states.
4296 */
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004297 if (!rcu_is_watching())
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004298 printk("RCU used illegally from extended quiescent state!\n");
4299
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004300 lockdep_print_held_locks(curr);
4301 printk("\nstack backtrace:\n");
4302 dump_stack();
4303}
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004304EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);