blob: 6f446eb3c7427a80886468581d2c3fe63232db01 [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
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700126unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200127static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700128
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700129/*
130 * All data structures here are protected by the global debug_lock.
131 *
132 * Mutex key structs only get allocated, once during bootup, and never
133 * get freed - this significantly simplifies the debugging code.
134 */
135unsigned long nr_lock_classes;
136static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
137
Dave Jonesf82b2172008-08-11 09:30:23 +0200138static inline struct lock_class *hlock_class(struct held_lock *hlock)
139{
140 if (!hlock->class_idx) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200141 /*
142 * Someone passed in garbage, we give up.
143 */
Dave Jonesf82b2172008-08-11 09:30:23 +0200144 DEBUG_LOCKS_WARN_ON(1);
145 return NULL;
146 }
147 return lock_classes + hlock->class_idx - 1;
148}
149
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700150#ifdef CONFIG_LOCK_STAT
Tejun Heo1871e522009-10-29 22:34:13 +0900151static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS],
152 cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700153
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200154static inline u64 lockstat_clock(void)
155{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200156 return local_clock();
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200157}
158
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200159static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700160{
161 int i;
162
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200163 for (i = 0; i < LOCKSTAT_POINTS; i++) {
164 if (points[i] == 0) {
165 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700166 break;
167 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200168 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700169 break;
170 }
171
172 return i;
173}
174
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200175static void lock_time_inc(struct lock_time *lt, u64 time)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700176{
177 if (time > lt->max)
178 lt->max = time;
179
Frank Rowand109d71c2009-11-19 13:42:06 -0800180 if (time < lt->min || !lt->nr)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700181 lt->min = time;
182
183 lt->total += time;
184 lt->nr++;
185}
186
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700187static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
188{
Frank Rowand109d71c2009-11-19 13:42:06 -0800189 if (!src->nr)
190 return;
191
192 if (src->max > dst->max)
193 dst->max = src->max;
194
195 if (src->min < dst->min || !dst->nr)
196 dst->min = src->min;
197
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700198 dst->total += src->total;
199 dst->nr += src->nr;
200}
201
202struct lock_class_stats lock_stats(struct lock_class *class)
203{
204 struct lock_class_stats stats;
205 int cpu, i;
206
207 memset(&stats, 0, sizeof(struct lock_class_stats));
208 for_each_possible_cpu(cpu) {
209 struct lock_class_stats *pcs =
Tejun Heo1871e522009-10-29 22:34:13 +0900210 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700211
212 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
213 stats.contention_point[i] += pcs->contention_point[i];
214
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200215 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
216 stats.contending_point[i] += pcs->contending_point[i];
217
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700218 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
219 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
220
221 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
222 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700223
224 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
225 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700226 }
227
228 return stats;
229}
230
231void clear_lock_stats(struct lock_class *class)
232{
233 int cpu;
234
235 for_each_possible_cpu(cpu) {
236 struct lock_class_stats *cpu_stats =
Tejun Heo1871e522009-10-29 22:34:13 +0900237 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700238
239 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
240 }
241 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200242 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700243}
244
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700245static struct lock_class_stats *get_lock_stats(struct lock_class *class)
246{
Tejun Heo1871e522009-10-29 22:34:13 +0900247 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700248}
249
250static void put_lock_stats(struct lock_class_stats *stats)
251{
Tejun Heo1871e522009-10-29 22:34:13 +0900252 put_cpu_var(cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700253}
254
255static void lock_release_holdtime(struct held_lock *hlock)
256{
257 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200258 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700259
260 if (!lock_stat)
261 return;
262
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200263 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700264
Dave Jonesf82b2172008-08-11 09:30:23 +0200265 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700266 if (hlock->read)
267 lock_time_inc(&stats->read_holdtime, holdtime);
268 else
269 lock_time_inc(&stats->write_holdtime, holdtime);
270 put_lock_stats(stats);
271}
272#else
273static inline void lock_release_holdtime(struct held_lock *hlock)
274{
275}
276#endif
277
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700278/*
279 * We keep a global list of all lock classes. The list only grows,
280 * never shrinks. The list is only accessed with the lockdep
281 * spinlock lock held.
282 */
283LIST_HEAD(all_lock_classes);
284
285/*
286 * The lockdep classes are in a hash-table as well, for fast lookup:
287 */
288#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
289#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700290#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700291#define classhashentry(key) (classhash_table + __classhashfn((key)))
292
Andrew Mortona63f38c2016-02-03 13:44:12 -0800293static struct hlist_head classhash_table[CLASSHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700294
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700295/*
296 * We put the lock dependency chains into a hash-table as well, to cache
297 * their existence:
298 */
299#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
300#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700301#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700302#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
303
Andrew Mortona63f38c2016-02-03 13:44:12 -0800304static struct hlist_head chainhash_table[CHAINHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700305
306/*
307 * The hash key of the lock dependency chains is a hash itself too:
308 * it's a hash of all locks taken up to that lock, including that lock.
309 * It's a 64-bit hash, because it's important for the keys to be
310 * unique.
311 */
312#define iterate_chain_key(key1, key2) \
Ingo Molnar03cbc352006-09-29 02:01:46 -0700313 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
314 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700315 (key2))
316
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200317void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700318{
319 current->lockdep_recursion++;
320}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700321EXPORT_SYMBOL(lockdep_off);
322
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200323void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700324{
325 current->lockdep_recursion--;
326}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700327EXPORT_SYMBOL(lockdep_on);
328
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700329/*
330 * Debugging switches:
331 */
332
333#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800334#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700335
336#if VERBOSE
337# define HARDIRQ_VERBOSE 1
338# define SOFTIRQ_VERBOSE 1
Nick Piggincf40bd12009-01-21 08:12:39 +0100339# define RECLAIM_VERBOSE 1
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700340#else
341# define HARDIRQ_VERBOSE 0
342# define SOFTIRQ_VERBOSE 0
Nick Piggincf40bd12009-01-21 08:12:39 +0100343# define RECLAIM_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700344#endif
345
Nick Piggincf40bd12009-01-21 08:12:39 +0100346#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700347/*
348 * Quick filtering for interesting events:
349 */
350static int class_filter(struct lock_class *class)
351{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700352#if 0
353 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700354 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700355 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700356 return 1;
357 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700358 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700359 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700360#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800361 /* Filter everything else. 1 would be to allow everything else */
362 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700363}
364#endif
365
366static int verbose(struct lock_class *class)
367{
368#if VERBOSE
369 return class_filter(class);
370#endif
371 return 0;
372}
373
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700374/*
375 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800376 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700377 */
378unsigned long nr_stack_trace_entries;
379static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
380
Dave Jones2c522832013-04-25 13:40:02 -0400381static void print_lockdep_off(const char *bug_msg)
382{
383 printk(KERN_DEBUG "%s\n", bug_msg);
384 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200385#ifdef CONFIG_LOCK_STAT
Dave Jones2c522832013-04-25 13:40:02 -0400386 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200387#endif
Dave Jones2c522832013-04-25 13:40:02 -0400388}
389
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700390static int save_trace(struct stack_trace *trace)
391{
392 trace->nr_entries = 0;
393 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
394 trace->entries = stack_trace + nr_stack_trace_entries;
395
Andi Kleen5a1b3992006-09-26 10:52:34 +0200396 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200397
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700398 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700399
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200400 /*
401 * Some daft arches put -1 at the end to indicate its a full trace.
402 *
403 * <rant> this is buggy anyway, since it takes a whole extra entry so a
404 * complete trace that maxes out the entries provided will be reported
405 * as incomplete, friggin useless </rant>
406 */
Luck, Tonyea5b41f2009-12-09 14:29:36 -0800407 if (trace->nr_entries != 0 &&
408 trace->entries[trace->nr_entries-1] == ULONG_MAX)
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200409 trace->nr_entries--;
410
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700411 trace->max_entries = trace->nr_entries;
412
413 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700414
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200415 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800416 if (!debug_locks_off_graph_unlock())
417 return 0;
418
Dave Jones2c522832013-04-25 13:40:02 -0400419 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
Ingo Molnar74c383f2006-12-13 00:34:43 -0800420 dump_stack();
421
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700422 return 0;
423 }
424
425 return 1;
426}
427
428unsigned int nr_hardirq_chains;
429unsigned int nr_softirq_chains;
430unsigned int nr_process_chains;
431unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700432
433#ifdef CONFIG_DEBUG_LOCKDEP
434/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700435 * Various lockdep statistics:
436 */
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200437DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700438#endif
439
440/*
441 * Locking printouts:
442 */
443
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100444#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100445 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
446 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
447 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
448 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100449
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700450static const char *usage_str[] =
451{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100452#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
453#include "lockdep_states.h"
454#undef LOCKDEP_STATE
455 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700456};
457
458const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
459{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700460 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700461}
462
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100463static inline unsigned long lock_flag(enum lock_usage_bit bit)
464{
465 return 1UL << bit;
466}
467
468static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
469{
470 char c = '.';
471
472 if (class->usage_mask & lock_flag(bit + 2))
473 c = '+';
474 if (class->usage_mask & lock_flag(bit)) {
475 c = '-';
476 if (class->usage_mask & lock_flag(bit + 2))
477 c = '?';
478 }
479
480 return c;
481}
482
Peter Zijlstraf510b232009-01-22 17:53:47 +0100483void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700484{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100485 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700486
Peter Zijlstraf510b232009-01-22 17:53:47 +0100487#define LOCKDEP_STATE(__STATE) \
488 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
489 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
490#include "lockdep_states.h"
491#undef LOCKDEP_STATE
492
493 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700494}
495
Steven Rostedte5e78d02011-11-02 20:24:16 -0400496static void __print_lock_name(struct lock_class *class)
Steven Rostedt3003eba2011-04-20 21:41:54 -0400497{
498 char str[KSYM_NAME_LEN];
499 const char *name;
500
501 name = class->name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700502 if (!name) {
503 name = __get_key_name(class->key, str);
Steven Rostedte5e78d02011-11-02 20:24:16 -0400504 printk("%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700505 } else {
Steven Rostedte5e78d02011-11-02 20:24:16 -0400506 printk("%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700507 if (class->name_version > 1)
508 printk("#%d", class->name_version);
509 if (class->subclass)
510 printk("/%d", class->subclass);
511 }
Steven Rostedte5e78d02011-11-02 20:24:16 -0400512}
513
514static void print_lock_name(struct lock_class *class)
515{
516 char usage[LOCK_USAGE_CHARS];
517
518 get_usage_chars(class, usage);
519
520 printk(" (");
521 __print_lock_name(class);
Peter Zijlstraf510b232009-01-22 17:53:47 +0100522 printk("){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700523}
524
525static void print_lockdep_cache(struct lockdep_map *lock)
526{
527 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700528 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700529
530 name = lock->name;
531 if (!name)
532 name = __get_key_name(lock->key->subkeys, str);
533
534 printk("%s", name);
535}
536
537static void print_lock(struct held_lock *hlock)
538{
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200539 /*
540 * We can be called locklessly through debug_show_all_locks() so be
541 * extra careful, the hlock might have been released and cleared.
542 */
543 unsigned int class_idx = hlock->class_idx;
544
545 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
546 barrier();
547
548 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
549 printk("<RELEASED>\n");
550 return;
551 }
552
553 print_lock_name(lock_classes + class_idx - 1);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700554 printk(", at: ");
555 print_ip_sym(hlock->acquire_ip);
556}
557
558static void lockdep_print_held_locks(struct task_struct *curr)
559{
560 int i, depth = curr->lockdep_depth;
561
562 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700563 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700564 return;
565 }
566 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700567 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700568
569 for (i = 0; i < depth; i++) {
570 printk(" #%d: ", i);
571 print_lock(curr->held_locks + i);
572 }
573}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700574
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100575static void print_kernel_ident(void)
Dave Jones99de0552006-09-29 02:00:10 -0700576{
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100577 printk("%s %.*s %s\n", init_utsname()->release,
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700578 (int)strcspn(init_utsname()->version, " "),
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100579 init_utsname()->version,
580 print_tainted());
Dave Jones99de0552006-09-29 02:00:10 -0700581}
582
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700583static int very_verbose(struct lock_class *class)
584{
585#if VERY_VERBOSE
586 return class_filter(class);
587#endif
588 return 0;
589}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700590
591/*
592 * Is this the address of a static object:
593 */
Sasha Levin8dce7a92013-06-13 18:41:16 -0400594#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700595static int static_obj(void *obj)
596{
597 unsigned long start = (unsigned long) &_stext,
598 end = (unsigned long) &_end,
599 addr = (unsigned long) obj;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700600
601 /*
602 * static variable?
603 */
604 if ((addr >= start) && (addr < end))
605 return 1;
606
Mike Frysinger2a9ad182009-09-22 16:44:16 -0700607 if (arch_is_kernel_data(addr))
608 return 1;
609
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700610 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900611 * in-kernel percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700612 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900613 if (is_kernel_percpu_address(addr))
614 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700615
616 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900617 * module static or percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700618 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900619 return is_module_address(addr) || is_module_percpu_address(addr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700620}
Sasha Levin8dce7a92013-06-13 18:41:16 -0400621#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700622
623/*
624 * To make lock name printouts unique, we calculate a unique
625 * class->name_version generation counter:
626 */
627static int count_matching_names(struct lock_class *new_class)
628{
629 struct lock_class *class;
630 int count = 0;
631
632 if (!new_class->name)
633 return 0;
634
Peter Zijlstra35a93932015-02-26 16:23:11 +0100635 list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700636 if (new_class->key - new_class->subclass == class->key)
637 return class->name_version;
638 if (class->name && !strcmp(class->name, new_class->name))
639 count = max(count, class->name_version);
640 }
641
642 return count + 1;
643}
644
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700645/*
646 * Register a lock's class in the hash-table, if the class is not present
647 * yet. Otherwise we look it up. We cache the result in the lock object
648 * itself, so actual lookup of the hash should be once per lock object.
649 */
650static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700651look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700652{
653 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -0800654 struct hlist_head *hash_head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700655 struct lock_class *class;
656
Hitoshi Mitake4ba053c2010-10-13 17:30:26 +0900657 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
658 debug_locks_off();
659 printk(KERN_ERR
660 "BUG: looking up invalid subclass: %u\n", subclass);
661 printk(KERN_ERR
662 "turning off the locking correctness validator.\n");
663 dump_stack();
664 return NULL;
665 }
666
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700667 /*
668 * Static locks do not have their class-keys yet - for them the key
669 * is the lock object itself:
670 */
671 if (unlikely(!lock->key))
672 lock->key = (void *)lock;
673
674 /*
675 * NOTE: the class-key must be unique. For dynamic locks, a static
676 * lock_class_key variable is passed in through the mutex_init()
677 * (or spin_lock_init()) call - which acts as the key. For static
678 * locks we use the lock object itself as the key.
679 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700680 BUILD_BUG_ON(sizeof(struct lock_class_key) >
681 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700682
683 key = lock->key->subkeys + subclass;
684
685 hash_head = classhashentry(key);
686
687 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +0100688 * We do an RCU walk of the hash, see lockdep_free_key_range().
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700689 */
Peter Zijlstra35a93932015-02-26 16:23:11 +0100690 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
691 return NULL;
692
Andrew Mortona63f38c2016-02-03 13:44:12 -0800693 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700694 if (class->key == key) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200695 /*
696 * Huh! same key, different name? Did someone trample
697 * on some memory? We're most confused.
698 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700699 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700700 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700701 }
702 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700703
704 return NULL;
705}
706
707/*
708 * Register a lock's class in the hash-table, if the class is not present
709 * yet. Otherwise we look it up. We cache the result in the lock object
710 * itself, so actual lookup of the hash should be once per lock object.
711 */
712static inline struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400713register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700714{
715 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -0800716 struct hlist_head *hash_head;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700717 struct lock_class *class;
Peter Zijlstra35a93932015-02-26 16:23:11 +0100718
719 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
Ingo Molnard6d897c2006-07-10 04:44:04 -0700720
721 class = look_up_lock_class(lock, subclass);
722 if (likely(class))
Yong Zhang87cdee72011-11-09 16:07:14 +0800723 goto out_set_class_cache;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700724
725 /*
726 * Debug-check: all keys must be persistent!
727 */
728 if (!static_obj(lock->key)) {
729 debug_locks_off();
730 printk("INFO: trying to register non-static key.\n");
731 printk("the code is fine but needs lockdep annotation.\n");
732 printk("turning off the locking correctness validator.\n");
733 dump_stack();
734
735 return NULL;
736 }
737
Ingo Molnard6d897c2006-07-10 04:44:04 -0700738 key = lock->key->subkeys + subclass;
739 hash_head = classhashentry(key);
740
Ingo Molnar74c383f2006-12-13 00:34:43 -0800741 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800742 return NULL;
743 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700744 /*
745 * We have to do the hash-walk again, to avoid races
746 * with another CPU:
747 */
Andrew Mortona63f38c2016-02-03 13:44:12 -0800748 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700749 if (class->key == key)
750 goto out_unlock_set;
Peter Zijlstra35a93932015-02-26 16:23:11 +0100751 }
752
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700753 /*
754 * Allocate a new key from the static array, and add it to
755 * the hash:
756 */
757 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800758 if (!debug_locks_off_graph_unlock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800759 return NULL;
760 }
Ingo Molnar74c383f2006-12-13 00:34:43 -0800761
Dave Jones2c522832013-04-25 13:40:02 -0400762 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100763 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700764 return NULL;
765 }
766 class = lock_classes + nr_lock_classes++;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200767 debug_atomic_inc(nr_unused_locks);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700768 class->key = key;
769 class->name = lock->name;
770 class->subclass = subclass;
771 INIT_LIST_HEAD(&class->lock_entry);
772 INIT_LIST_HEAD(&class->locks_before);
773 INIT_LIST_HEAD(&class->locks_after);
774 class->name_version = count_matching_names(class);
775 /*
776 * We use RCU's safe list-add method to make
777 * parallel walking of the hash-list safe:
778 */
Andrew Mortona63f38c2016-02-03 13:44:12 -0800779 hlist_add_head_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100780 /*
781 * Add it to the global list of classes:
782 */
783 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700784
785 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800786 graph_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800787
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700788 printk("\nnew class %p: %s", class->key, class->name);
789 if (class->name_version > 1)
790 printk("#%d", class->name_version);
791 printk("\n");
792 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800793
Ingo Molnar74c383f2006-12-13 00:34:43 -0800794 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800795 return NULL;
796 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700797 }
798out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800799 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700800
Yong Zhang87cdee72011-11-09 16:07:14 +0800801out_set_class_cache:
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400802 if (!subclass || force)
Hitoshi Mitake62016252010-10-05 18:01:51 +0900803 lock->class_cache[0] = class;
804 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
805 lock->class_cache[subclass] = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700806
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200807 /*
808 * Hash collision, did we smoke some? We found a class with a matching
809 * hash but the subclass -- which is hashed in -- didn't match.
810 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800811 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
812 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700813
814 return class;
815}
816
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700817#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700818/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700819 * Allocate a lockdep entry. (assumes the graph_lock held, returns
820 * with NULL on failure)
821 */
822static struct lock_list *alloc_list_entry(void)
823{
824 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
825 if (!debug_locks_off_graph_unlock())
826 return NULL;
827
Dave Jones2c522832013-04-25 13:40:02 -0400828 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100829 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700830 return NULL;
831 }
832 return list_entries + nr_list_entries++;
833}
834
835/*
836 * Add a new dependency to the head of the list:
837 */
838static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
Yong Zhang4726f2a2010-05-04 14:16:48 +0800839 struct list_head *head, unsigned long ip,
840 int distance, struct stack_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -0700841{
842 struct lock_list *entry;
843 /*
844 * Lock not present yet - get a new dependency struct and
845 * add it to the list:
846 */
847 entry = alloc_list_entry();
848 if (!entry)
849 return 0;
850
Zhu Yi74870172008-08-27 14:33:00 +0800851 entry->class = this;
852 entry->distance = distance;
Yong Zhang4726f2a2010-05-04 14:16:48 +0800853 entry->trace = *trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700854 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +0100855 * Both allocation and removal are done under the graph lock; but
856 * iteration is under RCU-sched; see look_up_lock_class() and
857 * lockdep_free_key_range().
Peter Zijlstra8e182572007-07-19 01:48:54 -0700858 */
859 list_add_tail_rcu(&entry->entry, head);
860
861 return 1;
862}
863
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200864/*
865 * For good efficiency of modular, we use power of 2
866 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200867#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
868#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
869
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200870/*
871 * The circular_queue and helpers is used to implement the
Peter Zijlstraaf012962009-07-16 15:44:29 +0200872 * breadth-first search(BFS)algorithem, by which we can build
873 * the shortest path from the next lock to be acquired to the
874 * previous held lock if there is a circular between them.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200875 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200876struct circular_queue {
877 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
878 unsigned int front, rear;
879};
880
881static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200882
Ming Lei12f3dfd2009-07-16 15:44:29 +0200883unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200884
Ming Leie351b662009-07-22 22:48:09 +0800885static unsigned int lockdep_dependency_gen_id;
886
Peter Zijlstraaf012962009-07-16 15:44:29 +0200887static inline void __cq_init(struct circular_queue *cq)
888{
889 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +0800890 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200891}
892
893static inline int __cq_empty(struct circular_queue *cq)
894{
895 return (cq->front == cq->rear);
896}
897
898static inline int __cq_full(struct circular_queue *cq)
899{
900 return ((cq->rear + 1) & CQ_MASK) == cq->front;
901}
902
903static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
904{
905 if (__cq_full(cq))
906 return -1;
907
908 cq->element[cq->rear] = elem;
909 cq->rear = (cq->rear + 1) & CQ_MASK;
910 return 0;
911}
912
913static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
914{
915 if (__cq_empty(cq))
916 return -1;
917
918 *elem = cq->element[cq->front];
919 cq->front = (cq->front + 1) & CQ_MASK;
920 return 0;
921}
922
923static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
924{
925 return (cq->rear - cq->front) & CQ_MASK;
926}
927
928static inline void mark_lock_accessed(struct lock_list *lock,
929 struct lock_list *parent)
930{
931 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200932
Peter Zijlstraaf012962009-07-16 15:44:29 +0200933 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200934 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200935 lock->parent = parent;
Ming Leie351b662009-07-22 22:48:09 +0800936 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200937}
938
939static inline unsigned long lock_accessed(struct lock_list *lock)
940{
941 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200942
Peter Zijlstraaf012962009-07-16 15:44:29 +0200943 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200944 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Ming Leie351b662009-07-22 22:48:09 +0800945 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200946}
947
948static inline struct lock_list *get_lock_parent(struct lock_list *child)
949{
950 return child->parent;
951}
952
953static inline int get_lock_depth(struct lock_list *child)
954{
955 int depth = 0;
956 struct lock_list *parent;
957
958 while ((parent = get_lock_parent(child))) {
959 child = parent;
960 depth++;
961 }
962 return depth;
963}
964
Ming Lei9e2d5512009-07-16 15:44:29 +0200965static int __bfs(struct lock_list *source_entry,
Peter Zijlstraaf012962009-07-16 15:44:29 +0200966 void *data,
967 int (*match)(struct lock_list *entry, void *data),
968 struct lock_list **target_entry,
969 int forward)
Ming Leic94aa5c2009-07-16 15:44:29 +0200970{
971 struct lock_list *entry;
Ming Leid588e462009-07-16 15:44:29 +0200972 struct list_head *head;
Ming Leic94aa5c2009-07-16 15:44:29 +0200973 struct circular_queue *cq = &lock_cq;
974 int ret = 1;
975
Ming Lei9e2d5512009-07-16 15:44:29 +0200976 if (match(source_entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +0200977 *target_entry = source_entry;
978 ret = 0;
979 goto exit;
980 }
981
Ming Leid588e462009-07-16 15:44:29 +0200982 if (forward)
983 head = &source_entry->class->locks_after;
984 else
985 head = &source_entry->class->locks_before;
986
987 if (list_empty(head))
988 goto exit;
989
990 __cq_init(cq);
Ming Leic94aa5c2009-07-16 15:44:29 +0200991 __cq_enqueue(cq, (unsigned long)source_entry);
992
993 while (!__cq_empty(cq)) {
994 struct lock_list *lock;
Ming Leic94aa5c2009-07-16 15:44:29 +0200995
996 __cq_dequeue(cq, (unsigned long *)&lock);
997
998 if (!lock->class) {
999 ret = -2;
1000 goto exit;
1001 }
1002
1003 if (forward)
1004 head = &lock->class->locks_after;
1005 else
1006 head = &lock->class->locks_before;
1007
Peter Zijlstra35a93932015-02-26 16:23:11 +01001008 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1009
1010 list_for_each_entry_rcu(entry, head, entry) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001011 if (!lock_accessed(entry)) {
Ming Lei12f3dfd2009-07-16 15:44:29 +02001012 unsigned int cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001013 mark_lock_accessed(entry, lock);
Ming Lei9e2d5512009-07-16 15:44:29 +02001014 if (match(entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001015 *target_entry = entry;
1016 ret = 0;
1017 goto exit;
1018 }
1019
1020 if (__cq_enqueue(cq, (unsigned long)entry)) {
1021 ret = -1;
1022 goto exit;
1023 }
Ming Lei12f3dfd2009-07-16 15:44:29 +02001024 cq_depth = __cq_get_elem_count(cq);
1025 if (max_bfs_queue_depth < cq_depth)
1026 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001027 }
1028 }
1029 }
1030exit:
1031 return ret;
1032}
1033
Ming Leid7aaba12009-07-16 15:44:29 +02001034static inline int __bfs_forwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001035 void *data,
1036 int (*match)(struct lock_list *entry, void *data),
1037 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001038{
Ming Lei9e2d5512009-07-16 15:44:29 +02001039 return __bfs(src_entry, data, match, target_entry, 1);
Ming Leic94aa5c2009-07-16 15:44:29 +02001040
1041}
1042
Ming Leid7aaba12009-07-16 15:44:29 +02001043static inline int __bfs_backwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001044 void *data,
1045 int (*match)(struct lock_list *entry, void *data),
1046 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001047{
Ming Lei9e2d5512009-07-16 15:44:29 +02001048 return __bfs(src_entry, data, match, target_entry, 0);
Ming Leic94aa5c2009-07-16 15:44:29 +02001049
1050}
1051
Peter Zijlstra8e182572007-07-19 01:48:54 -07001052/*
1053 * Recursive, forwards-direction lock-dependency checking, used for
1054 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1055 * checking.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001056 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001057
1058/*
1059 * Print a dependency chain entry (this is only done when a deadlock
1060 * has been detected):
1061 */
1062static noinline int
Ming Lei24208ca2009-07-16 15:44:29 +02001063print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001064{
1065 if (debug_locks_silent)
1066 return 0;
1067 printk("\n-> #%u", depth);
1068 print_lock_name(target->class);
1069 printk(":\n");
1070 print_stack_trace(&target->trace, 6);
1071
1072 return 0;
1073}
1074
Steven Rostedtf4185812011-04-20 21:41:55 -04001075static void
1076print_circular_lock_scenario(struct held_lock *src,
1077 struct held_lock *tgt,
1078 struct lock_list *prt)
1079{
1080 struct lock_class *source = hlock_class(src);
1081 struct lock_class *target = hlock_class(tgt);
1082 struct lock_class *parent = prt->class;
1083
1084 /*
1085 * A direct locking problem where unsafe_class lock is taken
1086 * directly by safe_class lock, then all we need to show
1087 * is the deadlock scenario, as it is obvious that the
1088 * unsafe lock is taken under the safe lock.
1089 *
1090 * But if there is a chain instead, where the safe lock takes
1091 * an intermediate lock (middle_class) where this lock is
1092 * not the same as the safe lock, then the lock chain is
1093 * used to describe the problem. Otherwise we would need
1094 * to show a different CPU case for each link in the chain
1095 * from the safe_class lock to the unsafe_class lock.
1096 */
1097 if (parent != source) {
1098 printk("Chain exists of:\n ");
1099 __print_lock_name(source);
1100 printk(" --> ");
1101 __print_lock_name(parent);
1102 printk(" --> ");
1103 __print_lock_name(target);
1104 printk("\n\n");
1105 }
1106
1107 printk(" Possible unsafe locking scenario:\n\n");
1108 printk(" CPU0 CPU1\n");
1109 printk(" ---- ----\n");
1110 printk(" lock(");
1111 __print_lock_name(target);
1112 printk(");\n");
1113 printk(" lock(");
1114 __print_lock_name(parent);
1115 printk(");\n");
1116 printk(" lock(");
1117 __print_lock_name(target);
1118 printk(");\n");
1119 printk(" lock(");
1120 __print_lock_name(source);
1121 printk(");\n");
1122 printk("\n *** DEADLOCK ***\n\n");
1123}
1124
Peter Zijlstra8e182572007-07-19 01:48:54 -07001125/*
1126 * When a circular dependency is detected, print the
1127 * header first:
1128 */
1129static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001130print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1131 struct held_lock *check_src,
1132 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001133{
1134 struct task_struct *curr = current;
1135
Ming Leic94aa5c2009-07-16 15:44:29 +02001136 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001137 return 0;
1138
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001139 printk("\n");
1140 printk("======================================================\n");
1141 printk("[ INFO: possible circular locking dependency detected ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001142 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001143 printk("-------------------------------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001144 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001145 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001146 print_lock(check_src);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001147 printk("\nbut task is already holding lock:\n");
Ming Leidb0002a2009-07-16 15:44:29 +02001148 print_lock(check_tgt);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001149 printk("\nwhich lock already depends on the new lock.\n\n");
1150 printk("\nthe existing dependency chain (in reverse order) is:\n");
1151
1152 print_circular_bug_entry(entry, depth);
1153
1154 return 0;
1155}
1156
Ming Lei9e2d5512009-07-16 15:44:29 +02001157static inline int class_equal(struct lock_list *entry, void *data)
1158{
1159 return entry->class == data;
1160}
1161
Ming Leidb0002a2009-07-16 15:44:29 +02001162static noinline int print_circular_bug(struct lock_list *this,
1163 struct lock_list *target,
1164 struct held_lock *check_src,
1165 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001166{
1167 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001168 struct lock_list *parent;
Steven Rostedtf4185812011-04-20 21:41:55 -04001169 struct lock_list *first_parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001170 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001171
Ming Leic94aa5c2009-07-16 15:44:29 +02001172 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001173 return 0;
1174
Ming Leidb0002a2009-07-16 15:44:29 +02001175 if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001176 return 0;
1177
Ming Leic94aa5c2009-07-16 15:44:29 +02001178 depth = get_lock_depth(target);
1179
Ming Leidb0002a2009-07-16 15:44:29 +02001180 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001181
1182 parent = get_lock_parent(target);
Steven Rostedtf4185812011-04-20 21:41:55 -04001183 first_parent = parent;
Ming Leic94aa5c2009-07-16 15:44:29 +02001184
1185 while (parent) {
1186 print_circular_bug_entry(parent, --depth);
1187 parent = get_lock_parent(parent);
1188 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001189
1190 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001191 print_circular_lock_scenario(check_src, check_tgt,
1192 first_parent);
1193
Peter Zijlstra8e182572007-07-19 01:48:54 -07001194 lockdep_print_held_locks(curr);
1195
1196 printk("\nstack backtrace:\n");
1197 dump_stack();
1198
1199 return 0;
1200}
1201
Ming Leidb0002a2009-07-16 15:44:29 +02001202static noinline int print_bfs_bug(int ret)
1203{
1204 if (!debug_locks_off_graph_unlock())
1205 return 0;
1206
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001207 /*
1208 * Breadth-first-search failed, graph got corrupted?
1209 */
Ming Leidb0002a2009-07-16 15:44:29 +02001210 WARN(1, "lockdep bfs error:%d\n", ret);
1211
1212 return 0;
1213}
1214
Ming Leief681022009-07-16 15:44:29 +02001215static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001216{
Ming Leief681022009-07-16 15:44:29 +02001217 (*(unsigned long *)data)++;
1218 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001219}
1220
Fengguang Wu5216d532013-11-09 00:55:35 +08001221static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
Ming Leief681022009-07-16 15:44:29 +02001222{
1223 unsigned long count = 0;
1224 struct lock_list *uninitialized_var(target_entry);
1225
1226 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1227
1228 return count;
1229}
David Miller419ca3f2008-07-29 21:45:03 -07001230unsigned long lockdep_count_forward_deps(struct lock_class *class)
1231{
1232 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001233 struct lock_list this;
1234
1235 this.parent = NULL;
1236 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001237
1238 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001239 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001240 ret = __lockdep_count_forward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001241 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001242 local_irq_restore(flags);
1243
1244 return ret;
1245}
1246
Fengguang Wu5216d532013-11-09 00:55:35 +08001247static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001248{
Ming Leief681022009-07-16 15:44:29 +02001249 unsigned long count = 0;
1250 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001251
Ming Leief681022009-07-16 15:44:29 +02001252 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001253
Ming Leief681022009-07-16 15:44:29 +02001254 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001255}
1256
1257unsigned long lockdep_count_backward_deps(struct lock_class *class)
1258{
1259 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001260 struct lock_list this;
1261
1262 this.parent = NULL;
1263 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001264
1265 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001266 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001267 ret = __lockdep_count_backward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001268 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001269 local_irq_restore(flags);
1270
1271 return ret;
1272}
1273
Peter Zijlstra8e182572007-07-19 01:48:54 -07001274/*
1275 * Prove that the dependency graph starting at <entry> can not
1276 * lead to <target>. Print an error and return 0 if it does.
1277 */
1278static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001279check_noncircular(struct lock_list *root, struct lock_class *target,
1280 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001281{
Ming Leidb0002a2009-07-16 15:44:29 +02001282 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001283
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001284 debug_atomic_inc(nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001285
Ming Leid7aaba12009-07-16 15:44:29 +02001286 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001287
1288 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001289}
1290
Steven Rostedt81d68a92008-05-12 21:20:42 +02001291#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001292/*
1293 * Forwards and backwards subgraph searching, for the purposes of
1294 * proving that two subgraphs can be connected by a new dependency
1295 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1296 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001297
Ming Leid7aaba12009-07-16 15:44:29 +02001298static inline int usage_match(struct lock_list *entry, void *bit)
1299{
1300 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1301}
1302
1303
1304
Peter Zijlstra8e182572007-07-19 01:48:54 -07001305/*
1306 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001307 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001308 *
Ming Leid7aaba12009-07-16 15:44:29 +02001309 * Return 0 if such a node exists in the subgraph, and put that node
1310 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001311 *
Ming Leid7aaba12009-07-16 15:44:29 +02001312 * Return 1 otherwise and keep *@target_entry unchanged.
1313 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001314 */
Ming Leid7aaba12009-07-16 15:44:29 +02001315static int
1316find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1317 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001318{
Ming Leid7aaba12009-07-16 15:44:29 +02001319 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001320
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001321 debug_atomic_inc(nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001322
Ming Leid7aaba12009-07-16 15:44:29 +02001323 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1324
1325 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001326}
1327
1328/*
1329 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001330 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001331 *
Ming Leid7aaba12009-07-16 15:44:29 +02001332 * Return 0 if such a node exists in the subgraph, and put that node
1333 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001334 *
Ming Leid7aaba12009-07-16 15:44:29 +02001335 * Return 1 otherwise and keep *@target_entry unchanged.
1336 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001337 */
Ming Leid7aaba12009-07-16 15:44:29 +02001338static int
1339find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1340 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001341{
Ming Leid7aaba12009-07-16 15:44:29 +02001342 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001343
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001344 debug_atomic_inc(nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001345
Ming Leid7aaba12009-07-16 15:44:29 +02001346 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001347
Ming Leid7aaba12009-07-16 15:44:29 +02001348 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001349}
1350
Peter Zijlstraaf012962009-07-16 15:44:29 +02001351static void print_lock_class_header(struct lock_class *class, int depth)
1352{
1353 int bit;
1354
1355 printk("%*s->", depth, "");
1356 print_lock_name(class);
1357 printk(" ops: %lu", class->ops);
1358 printk(" {\n");
1359
1360 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1361 if (class->usage_mask & (1 << bit)) {
1362 int len = depth;
1363
1364 len += printk("%*s %s", depth, "", usage_str[bit]);
1365 len += printk(" at:\n");
1366 print_stack_trace(class->usage_traces + bit, len);
1367 }
1368 }
1369 printk("%*s }\n", depth, "");
1370
1371 printk("%*s ... key at: ",depth,"");
1372 print_ip_sym((unsigned long)class->key);
1373}
1374
1375/*
1376 * printk the shortest lock dependencies from @start to @end in reverse order:
1377 */
1378static void __used
1379print_shortest_lock_dependencies(struct lock_list *leaf,
1380 struct lock_list *root)
1381{
1382 struct lock_list *entry = leaf;
1383 int depth;
1384
1385 /*compute depth from generated tree by BFS*/
1386 depth = get_lock_depth(leaf);
1387
1388 do {
1389 print_lock_class_header(entry->class, depth);
1390 printk("%*s ... acquired at:\n", depth, "");
1391 print_stack_trace(&entry->trace, 2);
1392 printk("\n");
1393
1394 if (depth == 0 && (entry != root)) {
Steven Rostedt6be8c392011-04-20 21:41:58 -04001395 printk("lockdep:%s bad path found in chain graph\n", __func__);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001396 break;
1397 }
1398
1399 entry = get_lock_parent(entry);
1400 depth--;
1401 } while (entry && (depth >= 0));
1402
1403 return;
1404}
Ming Leid7aaba12009-07-16 15:44:29 +02001405
Steven Rostedt3003eba2011-04-20 21:41:54 -04001406static void
1407print_irq_lock_scenario(struct lock_list *safe_entry,
1408 struct lock_list *unsafe_entry,
Steven Rostedtdad3d742011-04-20 21:41:57 -04001409 struct lock_class *prev_class,
1410 struct lock_class *next_class)
Steven Rostedt3003eba2011-04-20 21:41:54 -04001411{
1412 struct lock_class *safe_class = safe_entry->class;
1413 struct lock_class *unsafe_class = unsafe_entry->class;
Steven Rostedtdad3d742011-04-20 21:41:57 -04001414 struct lock_class *middle_class = prev_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001415
1416 if (middle_class == safe_class)
Steven Rostedtdad3d742011-04-20 21:41:57 -04001417 middle_class = next_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001418
1419 /*
1420 * A direct locking problem where unsafe_class lock is taken
1421 * directly by safe_class lock, then all we need to show
1422 * is the deadlock scenario, as it is obvious that the
1423 * unsafe lock is taken under the safe lock.
1424 *
1425 * But if there is a chain instead, where the safe lock takes
1426 * an intermediate lock (middle_class) where this lock is
1427 * not the same as the safe lock, then the lock chain is
1428 * used to describe the problem. Otherwise we would need
1429 * to show a different CPU case for each link in the chain
1430 * from the safe_class lock to the unsafe_class lock.
1431 */
1432 if (middle_class != unsafe_class) {
1433 printk("Chain exists of:\n ");
1434 __print_lock_name(safe_class);
1435 printk(" --> ");
1436 __print_lock_name(middle_class);
1437 printk(" --> ");
1438 __print_lock_name(unsafe_class);
1439 printk("\n\n");
1440 }
1441
1442 printk(" Possible interrupt unsafe locking scenario:\n\n");
1443 printk(" CPU0 CPU1\n");
1444 printk(" ---- ----\n");
1445 printk(" lock(");
1446 __print_lock_name(unsafe_class);
1447 printk(");\n");
1448 printk(" local_irq_disable();\n");
1449 printk(" lock(");
1450 __print_lock_name(safe_class);
1451 printk(");\n");
1452 printk(" lock(");
1453 __print_lock_name(middle_class);
1454 printk(");\n");
1455 printk(" <Interrupt>\n");
1456 printk(" lock(");
1457 __print_lock_name(safe_class);
1458 printk(");\n");
1459 printk("\n *** DEADLOCK ***\n\n");
1460}
1461
Peter Zijlstra8e182572007-07-19 01:48:54 -07001462static int
1463print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001464 struct lock_list *prev_root,
1465 struct lock_list *next_root,
1466 struct lock_list *backwards_entry,
1467 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001468 struct held_lock *prev,
1469 struct held_lock *next,
1470 enum lock_usage_bit bit1,
1471 enum lock_usage_bit bit2,
1472 const char *irqclass)
1473{
1474 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1475 return 0;
1476
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001477 printk("\n");
1478 printk("======================================================\n");
1479 printk("[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07001480 irqclass, irqclass);
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001481 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001482 printk("------------------------------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001483 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 -07001484 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001485 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1486 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1487 curr->hardirqs_enabled,
1488 curr->softirqs_enabled);
1489 print_lock(next);
1490
1491 printk("\nand this task is already holding:\n");
1492 print_lock(prev);
1493 printk("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001494 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001495 printk(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001496 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001497 printk("\n");
1498
1499 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1500 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001501 print_lock_name(backwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001502 printk("\n... which became %s-irq-safe at:\n", irqclass);
1503
Ming Lei24208ca2009-07-16 15:44:29 +02001504 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001505
1506 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001507 print_lock_name(forwards_entry->class);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001508 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1509 printk("...");
1510
Ming Lei24208ca2009-07-16 15:44:29 +02001511 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001512
1513 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04001514 print_irq_lock_scenario(backwards_entry, forwards_entry,
1515 hlock_class(prev), hlock_class(next));
Steven Rostedt3003eba2011-04-20 21:41:54 -04001516
Peter Zijlstra8e182572007-07-19 01:48:54 -07001517 lockdep_print_held_locks(curr);
1518
Ming Lei24208ca2009-07-16 15:44:29 +02001519 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1520 printk(" and the holding lock:\n");
1521 if (!save_trace(&prev_root->trace))
1522 return 0;
1523 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001524
Ming Lei24208ca2009-07-16 15:44:29 +02001525 printk("\nthe dependencies between the lock to be acquired");
1526 printk(" and %s-irq-unsafe lock:\n", irqclass);
1527 if (!save_trace(&next_root->trace))
1528 return 0;
1529 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001530
1531 printk("\nstack backtrace:\n");
1532 dump_stack();
1533
1534 return 0;
1535}
1536
1537static int
1538check_usage(struct task_struct *curr, struct held_lock *prev,
1539 struct held_lock *next, enum lock_usage_bit bit_backwards,
1540 enum lock_usage_bit bit_forwards, const char *irqclass)
1541{
1542 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001543 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001544 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001545 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001546
Ming Leid7aaba12009-07-16 15:44:29 +02001547 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001548
Ming Leid7aaba12009-07-16 15:44:29 +02001549 this.class = hlock_class(prev);
1550 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001551 if (ret < 0)
1552 return print_bfs_bug(ret);
1553 if (ret == 1)
1554 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001555
Ming Lei24208ca2009-07-16 15:44:29 +02001556 that.parent = NULL;
1557 that.class = hlock_class(next);
1558 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001559 if (ret < 0)
1560 return print_bfs_bug(ret);
1561 if (ret == 1)
1562 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001563
Ming Lei24208ca2009-07-16 15:44:29 +02001564 return print_bad_irq_dependency(curr, &this, &that,
1565 target_entry, target_entry1,
1566 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001567 bit_backwards, bit_forwards, irqclass);
1568}
1569
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001570static const char *state_names[] = {
1571#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001572 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001573#include "lockdep_states.h"
1574#undef LOCKDEP_STATE
1575};
1576
1577static const char *state_rnames[] = {
1578#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001579 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001580#include "lockdep_states.h"
1581#undef LOCKDEP_STATE
1582};
1583
1584static inline const char *state_name(enum lock_usage_bit bit)
1585{
1586 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1587}
1588
1589static int exclusive_bit(int new_bit)
1590{
1591 /*
1592 * USED_IN
1593 * USED_IN_READ
1594 * ENABLED
1595 * ENABLED_READ
1596 *
1597 * bit 0 - write/read
1598 * bit 1 - used_in/enabled
1599 * bit 2+ state
1600 */
1601
1602 int state = new_bit & ~3;
1603 int dir = new_bit & 2;
1604
1605 /*
1606 * keep state, bit flip the direction and strip read.
1607 */
1608 return state | (dir ^ 2);
1609}
1610
1611static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1612 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001613{
1614 /*
1615 * Prove that the new dependency does not connect a hardirq-safe
1616 * lock with a hardirq-unsafe lock - to achieve this we search
1617 * the backwards-subgraph starting at <prev>, and the
1618 * forwards-subgraph starting at <next>:
1619 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001620 if (!check_usage(curr, prev, next, bit,
1621 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001622 return 0;
1623
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001624 bit++; /* _READ */
1625
Peter Zijlstra8e182572007-07-19 01:48:54 -07001626 /*
1627 * Prove that the new dependency does not connect a hardirq-safe-read
1628 * lock with a hardirq-unsafe lock - to achieve this we search
1629 * the backwards-subgraph starting at <prev>, and the
1630 * forwards-subgraph starting at <next>:
1631 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001632 if (!check_usage(curr, prev, next, bit,
1633 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001634 return 0;
1635
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001636 return 1;
1637}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001638
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001639static int
1640check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1641 struct held_lock *next)
1642{
1643#define LOCKDEP_STATE(__STATE) \
1644 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001645 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001646#include "lockdep_states.h"
1647#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001648
Peter Zijlstra8e182572007-07-19 01:48:54 -07001649 return 1;
1650}
1651
1652static void inc_chains(void)
1653{
1654 if (current->hardirq_context)
1655 nr_hardirq_chains++;
1656 else {
1657 if (current->softirq_context)
1658 nr_softirq_chains++;
1659 else
1660 nr_process_chains++;
1661 }
1662}
1663
1664#else
1665
1666static inline int
1667check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1668 struct held_lock *next)
1669{
1670 return 1;
1671}
1672
1673static inline void inc_chains(void)
1674{
1675 nr_process_chains++;
1676}
1677
1678#endif
1679
Steven Rostedt48702ec2011-04-20 21:41:56 -04001680static void
1681print_deadlock_scenario(struct held_lock *nxt,
1682 struct held_lock *prv)
1683{
1684 struct lock_class *next = hlock_class(nxt);
1685 struct lock_class *prev = hlock_class(prv);
1686
1687 printk(" Possible unsafe locking scenario:\n\n");
1688 printk(" CPU0\n");
1689 printk(" ----\n");
1690 printk(" lock(");
1691 __print_lock_name(prev);
1692 printk(");\n");
1693 printk(" lock(");
1694 __print_lock_name(next);
1695 printk(");\n");
1696 printk("\n *** DEADLOCK ***\n\n");
1697 printk(" May be due to missing lock nesting notation\n\n");
1698}
1699
Peter Zijlstra8e182572007-07-19 01:48:54 -07001700static int
1701print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1702 struct held_lock *next)
1703{
1704 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1705 return 0;
1706
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001707 printk("\n");
1708 printk("=============================================\n");
1709 printk("[ INFO: possible recursive locking detected ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001710 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07001711 printk("---------------------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001712 printk("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001713 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001714 print_lock(next);
1715 printk("\nbut task is already holding lock:\n");
1716 print_lock(prev);
1717
1718 printk("\nother info that might help us debug this:\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001719 print_deadlock_scenario(next, prev);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001720 lockdep_print_held_locks(curr);
1721
1722 printk("\nstack backtrace:\n");
1723 dump_stack();
1724
1725 return 0;
1726}
1727
1728/*
1729 * Check whether we are holding such a class already.
1730 *
1731 * (Note that this has to be done separately, because the graph cannot
1732 * detect such classes of deadlocks.)
1733 *
1734 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1735 */
1736static int
1737check_deadlock(struct task_struct *curr, struct held_lock *next,
1738 struct lockdep_map *next_instance, int read)
1739{
1740 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001741 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001742 int i;
1743
1744 for (i = 0; i < curr->lockdep_depth; i++) {
1745 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001746
1747 if (prev->instance == next->nest_lock)
1748 nest = prev;
1749
Dave Jonesf82b2172008-08-11 09:30:23 +02001750 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001751 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001752
Peter Zijlstra8e182572007-07-19 01:48:54 -07001753 /*
1754 * Allow read-after-read recursion of the same
1755 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1756 */
1757 if ((read == 2) && prev->read)
1758 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001759
1760 /*
1761 * We're holding the nest_lock, which serializes this lock's
1762 * nesting behaviour.
1763 */
1764 if (nest)
1765 return 2;
1766
Peter Zijlstra8e182572007-07-19 01:48:54 -07001767 return print_deadlock_bug(curr, prev, next);
1768 }
1769 return 1;
1770}
1771
1772/*
1773 * There was a chain-cache miss, and we are about to add a new dependency
1774 * to a previous lock. We recursively validate the following rules:
1775 *
1776 * - would the adding of the <prev> -> <next> dependency create a
1777 * circular dependency in the graph? [== circular deadlock]
1778 *
1779 * - does the new prev->next dependency connect any hardirq-safe lock
1780 * (in the full backwards-subgraph starting at <prev>) with any
1781 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1782 * <next>)? [== illegal lock inversion with hardirq contexts]
1783 *
1784 * - does the new prev->next dependency connect any softirq-safe lock
1785 * (in the full backwards-subgraph starting at <prev>) with any
1786 * softirq-unsafe lock (in the full forwards-subgraph starting at
1787 * <next>)? [== illegal lock inversion with softirq contexts]
1788 *
1789 * any of these scenarios could lead to a deadlock.
1790 *
1791 * Then if all the validations pass, we add the forwards and backwards
1792 * dependency.
1793 */
1794static int
1795check_prev_add(struct task_struct *curr, struct held_lock *prev,
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001796 struct held_lock *next, int distance, int *stack_saved)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001797{
1798 struct lock_list *entry;
1799 int ret;
Ming Leidb0002a2009-07-16 15:44:29 +02001800 struct lock_list this;
1801 struct lock_list *uninitialized_var(target_entry);
Yong Zhang4726f2a2010-05-04 14:16:48 +08001802 /*
1803 * Static variable, serialized by the graph_lock().
1804 *
1805 * We use this static variable to save the stack trace in case
1806 * we call into this function multiple times due to encountering
1807 * trylocks in the held lock stack.
1808 */
1809 static struct stack_trace trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001810
1811 /*
1812 * Prove that the new <prev> -> <next> dependency would not
1813 * create a circular dependency in the graph. (We do this by
1814 * forward-recursing into the graph starting at <next>, and
1815 * checking whether we can reach <prev>.)
1816 *
1817 * We are using global variables to control the recursion, to
1818 * keep the stackframe size of the recursive functions low:
1819 */
Ming Leidb0002a2009-07-16 15:44:29 +02001820 this.class = hlock_class(next);
1821 this.parent = NULL;
1822 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1823 if (unlikely(!ret))
1824 return print_circular_bug(&this, target_entry, next, prev);
1825 else if (unlikely(ret < 0))
1826 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02001827
Peter Zijlstra8e182572007-07-19 01:48:54 -07001828 if (!check_prev_add_irq(curr, prev, next))
1829 return 0;
1830
1831 /*
1832 * For recursive read-locks we do all the dependency checks,
1833 * but we dont store read-triggered dependencies (only
1834 * write-triggered dependencies). This ensures that only the
1835 * write-side dependencies matter, and that if for example a
1836 * write-lock never takes any other locks, then the reads are
1837 * equivalent to a NOP.
1838 */
1839 if (next->read == 2 || prev->read == 2)
1840 return 1;
1841 /*
1842 * Is the <prev> -> <next> dependency already present?
1843 *
1844 * (this may occur even though this is a new chain: consider
1845 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1846 * chains - the second one will be new, but L1 already has
1847 * L2 added to its dependency list, due to the first chain.)
1848 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001849 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1850 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001851 if (distance == 1)
1852 entry->distance = 1;
1853 return 2;
1854 }
1855 }
1856
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001857 if (!*stack_saved) {
1858 if (!save_trace(&trace))
1859 return 0;
1860 *stack_saved = 1;
1861 }
Yong Zhang4726f2a2010-05-04 14:16:48 +08001862
Peter Zijlstra8e182572007-07-19 01:48:54 -07001863 /*
1864 * Ok, all validations passed, add the new lock
1865 * to the previous lock's dependency list:
1866 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001867 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1868 &hlock_class(prev)->locks_after,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001869 next->acquire_ip, distance, &trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001870
1871 if (!ret)
1872 return 0;
1873
Dave Jonesf82b2172008-08-11 09:30:23 +02001874 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1875 &hlock_class(next)->locks_before,
Yong Zhang4726f2a2010-05-04 14:16:48 +08001876 next->acquire_ip, distance, &trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001877 if (!ret)
1878 return 0;
1879
1880 /*
1881 * Debugging printouts:
1882 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001883 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001884 /* We drop graph lock, so another thread can overwrite trace. */
1885 *stack_saved = 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001886 graph_unlock();
1887 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001888 print_lock_name(hlock_class(prev));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001889 printk(" => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001890 print_lock_name(hlock_class(next));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001891 printk("\n");
1892 dump_stack();
1893 return graph_lock();
1894 }
1895 return 1;
1896}
1897
1898/*
1899 * Add the dependency to all directly-previous locks that are 'relevant'.
1900 * The ones that are relevant are (in increasing distance from curr):
1901 * all consecutive trylock entries and the final non-trylock entry - or
1902 * the end of this context's lock-chain - whichever comes first.
1903 */
1904static int
1905check_prevs_add(struct task_struct *curr, struct held_lock *next)
1906{
1907 int depth = curr->lockdep_depth;
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001908 int stack_saved = 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001909 struct held_lock *hlock;
1910
1911 /*
1912 * Debugging checks.
1913 *
1914 * Depth must not be zero for a non-head lock:
1915 */
1916 if (!depth)
1917 goto out_bug;
1918 /*
1919 * At least two relevant locks must exist for this
1920 * to be a head:
1921 */
1922 if (curr->held_locks[depth].irq_context !=
1923 curr->held_locks[depth-1].irq_context)
1924 goto out_bug;
1925
1926 for (;;) {
1927 int distance = curr->lockdep_depth - depth + 1;
Oleg Nesterov1b5ff812014-01-20 19:20:10 +01001928 hlock = curr->held_locks + depth - 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001929 /*
1930 * Only non-recursive-read entries get new dependencies
1931 * added:
1932 */
Oleg Nesterov1b5ff812014-01-20 19:20:10 +01001933 if (hlock->read != 2 && hlock->check) {
Yong Zhang4726f2a2010-05-04 14:16:48 +08001934 if (!check_prev_add(curr, hlock, next,
Dmitry Vyukov8a5fd562016-02-04 14:40:40 +01001935 distance, &stack_saved))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001936 return 0;
1937 /*
1938 * Stop after the first non-trylock entry,
1939 * as non-trylock entries have added their
1940 * own direct dependencies already, so this
1941 * lock is connected to them indirectly:
1942 */
1943 if (!hlock->trylock)
1944 break;
1945 }
1946 depth--;
1947 /*
1948 * End of lock-stack?
1949 */
1950 if (!depth)
1951 break;
1952 /*
1953 * Stop the search if we cross into another context:
1954 */
1955 if (curr->held_locks[depth].irq_context !=
1956 curr->held_locks[depth-1].irq_context)
1957 break;
1958 }
1959 return 1;
1960out_bug:
1961 if (!debug_locks_off_graph_unlock())
1962 return 0;
1963
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001964 /*
1965 * Clearly we all shouldn't be here, but since we made it we
1966 * can reliable say we messed up our state. See the above two
1967 * gotos for reasons why we could possibly end up here.
1968 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001969 WARN_ON(1);
1970
1971 return 0;
1972}
1973
1974unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08001975struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08001976int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08001977static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1978
1979struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1980{
1981 return lock_classes + chain_hlocks[chain->base + i];
1982}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001983
1984/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001985 * Look up a dependency chain. If the key is not present yet then
Jarek Poplawski9e860d02007-05-08 00:30:12 -07001986 * add it and return 1 - in this case the new dependency chain is
1987 * validated. If the key is already hashed, return 0.
1988 * (On return with 1 graph_lock is held.)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001989 */
Huang, Ying443cd502008-06-20 16:39:21 +08001990static inline int lookup_chain_cache(struct task_struct *curr,
1991 struct held_lock *hlock,
1992 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001993{
Dave Jonesf82b2172008-08-11 09:30:23 +02001994 struct lock_class *class = hlock_class(hlock);
Andrew Mortona63f38c2016-02-03 13:44:12 -08001995 struct hlist_head *hash_head = chainhashentry(chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001996 struct lock_chain *chain;
Hong Zhiguobfaf4af2013-04-04 15:01:21 +08001997 struct held_lock *hlock_curr;
Steven Rostedte0944ee2011-04-20 21:42:00 -04001998 int i, j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001999
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002000 /*
2001 * We might need to take the graph lock, ensure we've got IRQs
2002 * disabled to make this an IRQ-safe lock.. for recursion reasons
2003 * lockdep won't complain about its own locking errors.
2004 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08002005 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2006 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002007 /*
2008 * We can walk it lock-free, because entries only get added
2009 * to the hash:
2010 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08002011 hlist_for_each_entry_rcu(chain, hash_head, entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002012 if (chain->chain_key == chain_key) {
2013cache_hit:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002014 debug_atomic_inc(chain_lookup_hits);
Ingo Molnar81fc6852006-12-13 00:34:40 -08002015 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08002016 printk("\nhash chain already cached, key: "
2017 "%016Lx tail class: [%p] %s\n",
2018 (unsigned long long)chain_key,
2019 class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002020 return 0;
2021 }
2022 }
Ingo Molnar81fc6852006-12-13 00:34:40 -08002023 if (very_verbose(class))
Andrew Morton755cd902006-12-29 16:49:14 -08002024 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2025 (unsigned long long)chain_key, class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002026 /*
2027 * Allocate a new chain entry from the static array, and add
2028 * it to the hash:
2029 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08002030 if (!graph_lock())
2031 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002032 /*
2033 * We have to walk the chain again locked - to avoid duplicates:
2034 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08002035 hlist_for_each_entry(chain, hash_head, entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002036 if (chain->chain_key == chain_key) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002037 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002038 goto cache_hit;
2039 }
2040 }
2041 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002042 if (!debug_locks_off_graph_unlock())
2043 return 0;
2044
Dave Jones2c522832013-04-25 13:40:02 -04002045 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002046 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002047 return 0;
2048 }
2049 chain = lock_chains + nr_lock_chains++;
2050 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08002051 chain->irq_context = hlock->irq_context;
2052 /* Find the first held_lock of current chain */
Huang, Ying443cd502008-06-20 16:39:21 +08002053 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2054 hlock_curr = curr->held_locks + i;
Hong Zhiguobfaf4af2013-04-04 15:01:21 +08002055 if (hlock_curr->irq_context != hlock->irq_context)
Huang, Ying443cd502008-06-20 16:39:21 +08002056 break;
Huang, Ying443cd502008-06-20 16:39:21 +08002057 }
2058 i++;
2059 chain->depth = curr->lockdep_depth + 1 - i;
Steven Rostedte0944ee2011-04-20 21:42:00 -04002060 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2061 chain->base = nr_chain_hlocks;
2062 nr_chain_hlocks += chain->depth;
Huang, Ying443cd502008-06-20 16:39:21 +08002063 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02002064 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08002065 chain_hlocks[chain->base + j] = lock_id;
2066 }
2067 chain_hlocks[chain->base + j] = class - lock_classes;
2068 }
Andrew Mortona63f38c2016-02-03 13:44:12 -08002069 hlist_add_head_rcu(&chain->entry, hash_head);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002070 debug_atomic_inc(chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002071 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002072
2073 return 1;
2074}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002075
2076static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07002077 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002078{
2079 /*
2080 * Trylock needs to maintain the stack of held locks, but it
2081 * does not add new dependencies, because trylock can be done
2082 * in any order.
2083 *
2084 * We look up the chain_key and do the O(N^2) check and update of
2085 * the dependencies only if this is a new dependency chain.
2086 * (If lookup_chain_cache() returns with 1 it acquires
2087 * graph_lock for us)
2088 */
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01002089 if (!hlock->trylock && hlock->check &&
Huang, Ying443cd502008-06-20 16:39:21 +08002090 lookup_chain_cache(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002091 /*
2092 * Check whether last held lock:
2093 *
2094 * - is irq-safe, if this lock is irq-unsafe
2095 * - is softirq-safe, if this lock is hardirq-unsafe
2096 *
2097 * And check whether the new lock's dependency graph
2098 * could lead back to the previous lock.
2099 *
2100 * any of these scenarios could lead to a deadlock. If
2101 * All validations
2102 */
2103 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2104
2105 if (!ret)
2106 return 0;
2107 /*
2108 * Mark recursive read, as we jump over it when
2109 * building dependencies (just like we jump over
2110 * trylock entries):
2111 */
2112 if (ret == 2)
2113 hlock->read = 2;
2114 /*
2115 * Add dependency only if this lock is not the head
2116 * of the chain, and if it's not a secondary read-lock:
2117 */
2118 if (!chain_head && ret != 2)
2119 if (!check_prevs_add(curr, hlock))
2120 return 0;
2121 graph_unlock();
2122 } else
2123 /* after lookup_chain_cache(): */
2124 if (unlikely(!debug_locks))
2125 return 0;
2126
2127 return 1;
2128}
2129#else
2130static inline int validate_chain(struct task_struct *curr,
2131 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002132 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002133{
2134 return 1;
2135}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07002136#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002137
2138/*
2139 * We are building curr_chain_key incrementally, so double-check
2140 * it from scratch, to make sure that it's done correctly:
2141 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002142static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002143{
2144#ifdef CONFIG_DEBUG_LOCKDEP
2145 struct held_lock *hlock, *prev_hlock = NULL;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002146 unsigned int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002147 u64 chain_key = 0;
2148
2149 for (i = 0; i < curr->lockdep_depth; i++) {
2150 hlock = curr->held_locks + i;
2151 if (chain_key != hlock->prev_chain_key) {
2152 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002153 /*
2154 * We got mighty confused, our chain keys don't match
2155 * with what we expect, someone trample on our task state?
2156 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002157 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002158 curr->lockdep_depth, i,
2159 (unsigned long long)chain_key,
2160 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002161 return;
2162 }
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002163 /*
2164 * Whoops ran out of static storage again?
2165 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002166 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
Jarek Poplawski381a2292007-02-10 01:44:58 -08002167 return;
2168
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002169 if (prev_hlock && (prev_hlock->irq_context !=
2170 hlock->irq_context))
2171 chain_key = 0;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002172 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002173 prev_hlock = hlock;
2174 }
2175 if (chain_key != curr->curr_chain_key) {
2176 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002177 /*
2178 * More smoking hash instead of calculating it, damn see these
2179 * numbers float.. I bet that a pink elephant stepped on my memory.
2180 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002181 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002182 curr->lockdep_depth, i,
2183 (unsigned long long)chain_key,
2184 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002185 }
2186#endif
2187}
2188
Steven Rostedt282b5c22011-04-20 21:41:59 -04002189static void
2190print_usage_bug_scenario(struct held_lock *lock)
2191{
2192 struct lock_class *class = hlock_class(lock);
2193
2194 printk(" Possible unsafe locking scenario:\n\n");
2195 printk(" CPU0\n");
2196 printk(" ----\n");
2197 printk(" lock(");
2198 __print_lock_name(class);
2199 printk(");\n");
2200 printk(" <Interrupt>\n");
2201 printk(" lock(");
2202 __print_lock_name(class);
2203 printk(");\n");
2204 printk("\n *** DEADLOCK ***\n\n");
2205}
2206
Peter Zijlstra8e182572007-07-19 01:48:54 -07002207static int
2208print_usage_bug(struct task_struct *curr, struct held_lock *this,
2209 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2210{
2211 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2212 return 0;
2213
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002214 printk("\n");
2215 printk("=================================\n");
2216 printk("[ INFO: inconsistent lock state ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002217 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002218 printk("---------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002219
2220 printk("inconsistent {%s} -> {%s} usage.\n",
2221 usage_str[prev_bit], usage_str[new_bit]);
2222
2223 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002224 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002225 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2226 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2227 trace_hardirqs_enabled(curr),
2228 trace_softirqs_enabled(curr));
2229 print_lock(this);
2230
2231 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002232 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002233
2234 print_irqtrace_events(curr);
2235 printk("\nother info that might help us debug this:\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002236 print_usage_bug_scenario(this);
2237
Peter Zijlstra8e182572007-07-19 01:48:54 -07002238 lockdep_print_held_locks(curr);
2239
2240 printk("\nstack backtrace:\n");
2241 dump_stack();
2242
2243 return 0;
2244}
2245
2246/*
2247 * Print out an error if an invalid bit is set:
2248 */
2249static inline int
2250valid_state(struct task_struct *curr, struct held_lock *this,
2251 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2252{
Dave Jonesf82b2172008-08-11 09:30:23 +02002253 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002254 return print_usage_bug(curr, this, bad_bit, new_bit);
2255 return 1;
2256}
2257
2258static int mark_lock(struct task_struct *curr, struct held_lock *this,
2259 enum lock_usage_bit new_bit);
2260
Steven Rostedt81d68a92008-05-12 21:20:42 +02002261#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002262
2263/*
2264 * print irq inversion bug:
2265 */
2266static int
Ming Lei24208ca2009-07-16 15:44:29 +02002267print_irq_inversion_bug(struct task_struct *curr,
2268 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002269 struct held_lock *this, int forwards,
2270 const char *irqclass)
2271{
Steven Rostedtdad3d742011-04-20 21:41:57 -04002272 struct lock_list *entry = other;
2273 struct lock_list *middle = NULL;
2274 int depth;
2275
Ingo Molnar74c383f2006-12-13 00:34:43 -08002276 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002277 return 0;
2278
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002279 printk("\n");
2280 printk("=========================================================\n");
2281 printk("[ INFO: possible irq lock inversion dependency detected ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002282 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07002283 printk("---------------------------------------------------------\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002284 printk("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002285 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002286 print_lock(this);
2287 if (forwards)
Peter Zijlstra26575e22009-03-04 14:53:24 +01002288 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002289 else
Peter Zijlstra26575e22009-03-04 14:53:24 +01002290 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002291 print_lock_name(other->class);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002292 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2293
2294 printk("\nother info that might help us debug this:\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04002295
2296 /* Find a middle lock (if one exists) */
2297 depth = get_lock_depth(other);
2298 do {
2299 if (depth == 0 && (entry != root)) {
2300 printk("lockdep:%s bad path found in chain graph\n", __func__);
2301 break;
2302 }
2303 middle = entry;
2304 entry = get_lock_parent(entry);
2305 depth--;
2306 } while (entry && entry != root && (depth >= 0));
2307 if (forwards)
2308 print_irq_lock_scenario(root, other,
2309 middle ? middle->class : root->class, other->class);
2310 else
2311 print_irq_lock_scenario(other, root,
2312 middle ? middle->class : other->class, root->class);
2313
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002314 lockdep_print_held_locks(curr);
2315
Ming Lei24208ca2009-07-16 15:44:29 +02002316 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2317 if (!save_trace(&root->trace))
2318 return 0;
2319 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002320
2321 printk("\nstack backtrace:\n");
2322 dump_stack();
2323
2324 return 0;
2325}
2326
2327/*
2328 * Prove that in the forwards-direction subgraph starting at <this>
2329 * there is no lock matching <mask>:
2330 */
2331static int
2332check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2333 enum lock_usage_bit bit, const char *irqclass)
2334{
2335 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002336 struct lock_list root;
2337 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002338
Ming Leid7aaba12009-07-16 15:44:29 +02002339 root.parent = NULL;
2340 root.class = hlock_class(this);
2341 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002342 if (ret < 0)
2343 return print_bfs_bug(ret);
2344 if (ret == 1)
2345 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002346
Ming Lei24208ca2009-07-16 15:44:29 +02002347 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002348 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002349}
2350
2351/*
2352 * Prove that in the backwards-direction subgraph starting at <this>
2353 * there is no lock matching <mask>:
2354 */
2355static int
2356check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2357 enum lock_usage_bit bit, const char *irqclass)
2358{
2359 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002360 struct lock_list root;
2361 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002362
Ming Leid7aaba12009-07-16 15:44:29 +02002363 root.parent = NULL;
2364 root.class = hlock_class(this);
2365 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002366 if (ret < 0)
2367 return print_bfs_bug(ret);
2368 if (ret == 1)
2369 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002370
Ming Lei24208ca2009-07-16 15:44:29 +02002371 return print_irq_inversion_bug(curr, &root, target_entry,
Oleg Nesterov48d50672010-01-26 19:16:41 +01002372 this, 0, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002373}
2374
Ingo Molnar3117df02006-12-13 00:34:43 -08002375void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002376{
2377 printk("irq event stamp: %u\n", curr->irq_events);
2378 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2379 print_ip_sym(curr->hardirq_enable_ip);
2380 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2381 print_ip_sym(curr->hardirq_disable_ip);
2382 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2383 print_ip_sym(curr->softirq_enable_ip);
2384 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2385 print_ip_sym(curr->softirq_disable_ip);
2386}
2387
Peter Zijlstracd953022009-01-22 16:38:21 +01002388static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002389{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002390#if HARDIRQ_VERBOSE
2391 return class_filter(class);
2392#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002393 return 0;
2394}
2395
Peter Zijlstracd953022009-01-22 16:38:21 +01002396static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002397{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002398#if SOFTIRQ_VERBOSE
2399 return class_filter(class);
2400#endif
2401 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002402}
2403
Peter Zijlstracd953022009-01-22 16:38:21 +01002404static int RECLAIM_FS_verbose(struct lock_class *class)
Nick Piggincf40bd12009-01-21 08:12:39 +01002405{
2406#if RECLAIM_VERBOSE
2407 return class_filter(class);
2408#endif
2409 return 0;
2410}
2411
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002412#define STRICT_READ_CHECKS 1
2413
Peter Zijlstracd953022009-01-22 16:38:21 +01002414static int (*state_verbose_f[])(struct lock_class *class) = {
2415#define LOCKDEP_STATE(__STATE) \
2416 __STATE##_verbose,
2417#include "lockdep_states.h"
2418#undef LOCKDEP_STATE
2419};
2420
2421static inline int state_verbose(enum lock_usage_bit bit,
2422 struct lock_class *class)
2423{
2424 return state_verbose_f[bit >> 2](class);
2425}
2426
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002427typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2428 enum lock_usage_bit bit, const char *name);
2429
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002430static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002431mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2432 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002433{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002434 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002435 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002436 int dir = new_bit & 2;
2437
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002438 /*
2439 * mark USED_IN has to look forwards -- to ensure no dependency
2440 * has ENABLED state, which would allow recursion deadlocks.
2441 *
2442 * mark ENABLED has to look backwards -- to ensure no dependee
2443 * has USED_IN state, which, again, would allow recursion deadlocks.
2444 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002445 check_usage_f usage = dir ?
2446 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002447
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002448 /*
2449 * Validate that this particular lock does not have conflicting
2450 * usage states.
2451 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002452 if (!valid_state(curr, this, new_bit, excl_bit))
2453 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002454
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002455 /*
2456 * Validate that the lock dependencies don't have conflicting usage
2457 * states.
2458 */
2459 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002460 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002461 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002462
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002463 /*
2464 * Check for read in write conflicts
2465 */
2466 if (!read) {
2467 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2468 return 0;
2469
2470 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002471 !usage(curr, this, excl_bit + 1,
2472 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002473 return 0;
2474 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002475
Peter Zijlstracd953022009-01-22 16:38:21 +01002476 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002477 return 2;
2478
2479 return 1;
2480}
2481
Nick Piggincf40bd12009-01-21 08:12:39 +01002482enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002483#define LOCKDEP_STATE(__STATE) __STATE,
2484#include "lockdep_states.h"
2485#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002486};
2487
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002488/*
2489 * Mark all held locks with a usage bit:
2490 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002491static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002492mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002493{
2494 enum lock_usage_bit usage_bit;
2495 struct held_lock *hlock;
2496 int i;
2497
2498 for (i = 0; i < curr->lockdep_depth; i++) {
2499 hlock = curr->held_locks + i;
2500
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002501 usage_bit = 2 + (mark << 2); /* ENABLED */
2502 if (hlock->read)
2503 usage_bit += 1; /* READ */
2504
2505 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002506
Oleg Nesterov34d0ed52014-01-20 19:20:13 +01002507 if (!hlock->check)
Peter Zijlstraefbe2ee2011-07-07 11:39:45 +02002508 continue;
2509
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002510 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002511 return 0;
2512 }
2513
2514 return 1;
2515}
2516
2517/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002518 * Hardirqs will be enabled:
2519 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002520static void __trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002521{
2522 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002523
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002524 /* we'll do an OFF -> ON transition: */
2525 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002526
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002527 /*
2528 * We are going to turn hardirqs on, so set the
2529 * usage bit for all held locks:
2530 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002531 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002532 return;
2533 /*
2534 * If we have softirqs enabled, then set the usage
2535 * bit for all held locks. (disabled hardirqs prevented
2536 * this bit from being set before)
2537 */
2538 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002539 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002540 return;
2541
2542 curr->hardirq_enable_ip = ip;
2543 curr->hardirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002544 debug_atomic_inc(hardirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002545}
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002546
Andi Kleenb35f8302014-02-08 08:52:02 +01002547__visible void trace_hardirqs_on_caller(unsigned long ip)
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002548{
2549 time_hardirqs_on(CALLER_ADDR0, ip);
2550
2551 if (unlikely(!debug_locks || current->lockdep_recursion))
2552 return;
2553
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002554 if (unlikely(current->hardirqs_enabled)) {
2555 /*
2556 * Neither irq nor preemption are disabled here
2557 * so this is racy by nature but losing one hit
2558 * in a stat is not a big deal.
2559 */
2560 __debug_atomic_inc(redundant_hardirqs_on);
2561 return;
2562 }
2563
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002564 /*
2565 * We're enabling irqs and according to our state above irqs weren't
2566 * already enabled, yet we find the hardware thinks they are in fact
2567 * enabled.. someone messed up their IRQ state tracing.
2568 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002569 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2570 return;
2571
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002572 /*
2573 * See the fine text that goes along with this variable definition.
2574 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002575 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2576 return;
2577
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002578 /*
2579 * Can't allow enabling interrupts while in an interrupt handler,
2580 * that's general bad form and such. Recursion, limited stack etc..
2581 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002582 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2583 return;
2584
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002585 current->lockdep_recursion = 1;
2586 __trace_hardirqs_on_caller(ip);
2587 current->lockdep_recursion = 0;
2588}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002589EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002590
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002591void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002592{
2593 trace_hardirqs_on_caller(CALLER_ADDR0);
2594}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002595EXPORT_SYMBOL(trace_hardirqs_on);
2596
2597/*
2598 * Hardirqs were disabled:
2599 */
Andi Kleenb35f8302014-02-08 08:52:02 +01002600__visible void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002601{
2602 struct task_struct *curr = current;
2603
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002604 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002605
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002606 if (unlikely(!debug_locks || current->lockdep_recursion))
2607 return;
2608
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002609 /*
2610 * So we're supposed to get called after you mask local IRQs, but for
2611 * some reason the hardware doesn't quite think you did a proper job.
2612 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002613 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2614 return;
2615
2616 if (curr->hardirqs_enabled) {
2617 /*
2618 * We have done an ON -> OFF transition:
2619 */
2620 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002621 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002622 curr->hardirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002623 debug_atomic_inc(hardirqs_off_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002624 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002625 debug_atomic_inc(redundant_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002626}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002627EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002628
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002629void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002630{
2631 trace_hardirqs_off_caller(CALLER_ADDR0);
2632}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002633EXPORT_SYMBOL(trace_hardirqs_off);
2634
2635/*
2636 * Softirqs will be enabled:
2637 */
2638void trace_softirqs_on(unsigned long ip)
2639{
2640 struct task_struct *curr = current;
2641
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002642 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002643 return;
2644
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002645 /*
2646 * We fancy IRQs being disabled here, see softirq.c, avoids
2647 * funny state and nesting things.
2648 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002649 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2650 return;
2651
2652 if (curr->softirqs_enabled) {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002653 debug_atomic_inc(redundant_softirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002654 return;
2655 }
2656
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002657 current->lockdep_recursion = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002658 /*
2659 * We'll do an OFF -> ON transition:
2660 */
2661 curr->softirqs_enabled = 1;
2662 curr->softirq_enable_ip = ip;
2663 curr->softirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002664 debug_atomic_inc(softirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002665 /*
2666 * We are going to turn softirqs on, so set the
2667 * usage bit for all held locks, if hardirqs are
2668 * enabled too:
2669 */
2670 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002671 mark_held_locks(curr, SOFTIRQ);
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002672 current->lockdep_recursion = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002673}
2674
2675/*
2676 * Softirqs were disabled:
2677 */
2678void trace_softirqs_off(unsigned long ip)
2679{
2680 struct task_struct *curr = current;
2681
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002682 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002683 return;
2684
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002685 /*
2686 * We fancy IRQs being disabled here, see softirq.c
2687 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002688 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2689 return;
2690
2691 if (curr->softirqs_enabled) {
2692 /*
2693 * We have done an ON -> OFF transition:
2694 */
2695 curr->softirqs_enabled = 0;
2696 curr->softirq_disable_ip = ip;
2697 curr->softirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002698 debug_atomic_inc(softirqs_off_events);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002699 /*
2700 * Whoops, we wanted softirqs off, so why aren't they?
2701 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002702 DEBUG_LOCKS_WARN_ON(!softirq_count());
2703 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002704 debug_atomic_inc(redundant_softirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002705}
2706
Peter Zijlstra2f850182009-03-20 11:13:20 +01002707static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
Nick Piggincf40bd12009-01-21 08:12:39 +01002708{
2709 struct task_struct *curr = current;
2710
2711 if (unlikely(!debug_locks))
2712 return;
2713
2714 /* no reclaim without waiting on it */
Mel Gormand0164ad2015-11-06 16:28:21 -08002715 if (!(gfp_mask & __GFP_DIRECT_RECLAIM))
Nick Piggincf40bd12009-01-21 08:12:39 +01002716 return;
2717
2718 /* this guy won't enter reclaim */
2719 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2720 return;
2721
2722 /* We're only interested __GFP_FS allocations for now */
2723 if (!(gfp_mask & __GFP_FS))
2724 return;
2725
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002726 /*
2727 * Oi! Can't be having __GFP_FS allocations with IRQs disabled.
2728 */
Peter Zijlstra2f850182009-03-20 11:13:20 +01002729 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
Nick Piggincf40bd12009-01-21 08:12:39 +01002730 return;
2731
2732 mark_held_locks(curr, RECLAIM_FS);
2733}
2734
Peter Zijlstra2f850182009-03-20 11:13:20 +01002735static void check_flags(unsigned long flags);
2736
2737void lockdep_trace_alloc(gfp_t gfp_mask)
2738{
2739 unsigned long flags;
2740
2741 if (unlikely(current->lockdep_recursion))
2742 return;
2743
2744 raw_local_irq_save(flags);
2745 check_flags(flags);
2746 current->lockdep_recursion = 1;
2747 __lockdep_trace_alloc(gfp_mask, flags);
2748 current->lockdep_recursion = 0;
2749 raw_local_irq_restore(flags);
2750}
2751
Peter Zijlstra8e182572007-07-19 01:48:54 -07002752static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2753{
2754 /*
2755 * If non-trylock use in a hardirq or softirq context, then
2756 * mark the lock as used in these contexts:
2757 */
2758 if (!hlock->trylock) {
2759 if (hlock->read) {
2760 if (curr->hardirq_context)
2761 if (!mark_lock(curr, hlock,
2762 LOCK_USED_IN_HARDIRQ_READ))
2763 return 0;
2764 if (curr->softirq_context)
2765 if (!mark_lock(curr, hlock,
2766 LOCK_USED_IN_SOFTIRQ_READ))
2767 return 0;
2768 } else {
2769 if (curr->hardirq_context)
2770 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2771 return 0;
2772 if (curr->softirq_context)
2773 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2774 return 0;
2775 }
2776 }
2777 if (!hlock->hardirqs_off) {
2778 if (hlock->read) {
2779 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002780 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002781 return 0;
2782 if (curr->softirqs_enabled)
2783 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002784 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002785 return 0;
2786 } else {
2787 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002788 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002789 return 0;
2790 if (curr->softirqs_enabled)
2791 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002792 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002793 return 0;
2794 }
2795 }
2796
Nick Piggincf40bd12009-01-21 08:12:39 +01002797 /*
2798 * We reuse the irq context infrastructure more broadly as a general
2799 * context checking code. This tests GFP_FS recursion (a lock taken
2800 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2801 * allocation).
2802 */
2803 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2804 if (hlock->read) {
2805 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2806 return 0;
2807 } else {
2808 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2809 return 0;
2810 }
2811 }
2812
Peter Zijlstra8e182572007-07-19 01:48:54 -07002813 return 1;
2814}
2815
2816static int separate_irq_context(struct task_struct *curr,
2817 struct held_lock *hlock)
2818{
2819 unsigned int depth = curr->lockdep_depth;
2820
2821 /*
2822 * Keep track of points where we cross into an interrupt context:
2823 */
2824 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2825 curr->softirq_context;
2826 if (depth) {
2827 struct held_lock *prev_hlock;
2828
2829 prev_hlock = curr->held_locks + depth-1;
2830 /*
2831 * If we cross into another context, reset the
2832 * hash key (this also prevents the checking and the
2833 * adding of the dependency to 'prev'):
2834 */
2835 if (prev_hlock->irq_context != hlock->irq_context)
2836 return 1;
2837 }
2838 return 0;
2839}
2840
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002841#else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002842
2843static inline
2844int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2845 enum lock_usage_bit new_bit)
2846{
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002847 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002848 return 1;
2849}
2850
2851static inline int mark_irqflags(struct task_struct *curr,
2852 struct held_lock *hlock)
2853{
2854 return 1;
2855}
2856
2857static inline int separate_irq_context(struct task_struct *curr,
2858 struct held_lock *hlock)
2859{
2860 return 0;
2861}
2862
Peter Zijlstra868a23a2009-02-15 00:25:21 +01002863void lockdep_trace_alloc(gfp_t gfp_mask)
2864{
2865}
2866
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002867#endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002868
2869/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002870 * Mark a lock with a usage bit, and validate the state transition:
2871 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002872static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02002873 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002874{
2875 unsigned int new_mask = 1 << new_bit, ret = 1;
2876
2877 /*
2878 * If already set then do not dirty the cacheline,
2879 * nor do any checks:
2880 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002881 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002882 return 1;
2883
2884 if (!graph_lock())
2885 return 0;
2886 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002887 * Make sure we didn't race:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002888 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002889 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002890 graph_unlock();
2891 return 1;
2892 }
2893
Dave Jonesf82b2172008-08-11 09:30:23 +02002894 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002895
Dave Jonesf82b2172008-08-11 09:30:23 +02002896 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002897 return 0;
2898
2899 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01002900#define LOCKDEP_STATE(__STATE) \
2901 case LOCK_USED_IN_##__STATE: \
2902 case LOCK_USED_IN_##__STATE##_READ: \
2903 case LOCK_ENABLED_##__STATE: \
2904 case LOCK_ENABLED_##__STATE##_READ:
2905#include "lockdep_states.h"
2906#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07002907 ret = mark_lock_irq(curr, this, new_bit);
2908 if (!ret)
2909 return 0;
2910 break;
2911 case LOCK_USED:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002912 debug_atomic_dec(nr_unused_locks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002913 break;
2914 default:
2915 if (!debug_locks_off_graph_unlock())
2916 return 0;
2917 WARN_ON(1);
2918 return 0;
2919 }
2920
2921 graph_unlock();
2922
2923 /*
2924 * We must printk outside of the graph_lock:
2925 */
2926 if (ret == 2) {
2927 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2928 print_lock(this);
2929 print_irqtrace_events(curr);
2930 dump_stack();
2931 }
2932
2933 return ret;
2934}
2935
2936/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002937 * Initialize a lock instance's lock-class mapping info:
2938 */
2939void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002940 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002941{
Yong Zhangd3d03d42011-11-09 16:04:51 +08002942 int i;
2943
2944 kmemcheck_mark_initialized(lock, sizeof(*lock));
2945
2946 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
2947 lock->class_cache[i] = NULL;
Hitoshi Mitake62016252010-10-05 18:01:51 +09002948
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002949#ifdef CONFIG_LOCK_STAT
2950 lock->cpu = raw_smp_processor_id();
2951#endif
2952
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002953 /*
2954 * Can't be having no nameless bastards around this place!
2955 */
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002956 if (DEBUG_LOCKS_WARN_ON(!name)) {
2957 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002958 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002959 }
2960
2961 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002962
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002963 /*
2964 * No key, no joy, we need to hash something.
2965 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002966 if (DEBUG_LOCKS_WARN_ON(!key))
2967 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002968 /*
2969 * Sanity check, the lock-class key must be persistent:
2970 */
2971 if (!static_obj(key)) {
2972 printk("BUG: key %p not in .data!\n", key);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002973 /*
2974 * What it says above ^^^^^, I suggest you read it.
2975 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002976 DEBUG_LOCKS_WARN_ON(1);
2977 return;
2978 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002979 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02002980
2981 if (unlikely(!debug_locks))
2982 return;
2983
Peter Zijlstra35a93932015-02-26 16:23:11 +01002984 if (subclass) {
2985 unsigned long flags;
2986
2987 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
2988 return;
2989
2990 raw_local_irq_save(flags);
2991 current->lockdep_recursion = 1;
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04002992 register_lock_class(lock, subclass, 1);
Peter Zijlstra35a93932015-02-26 16:23:11 +01002993 current->lockdep_recursion = 0;
2994 raw_local_irq_restore(flags);
2995 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002996}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002997EXPORT_SYMBOL_GPL(lockdep_init_map);
2998
Peter Zijlstra1704f472010-03-19 01:37:42 +01002999struct lock_class_key __lockdep_no_validate__;
Kent Overstreetea6749c2012-12-27 22:21:58 -08003000EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003001
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003002static int
3003print_lock_nested_lock_not_held(struct task_struct *curr,
3004 struct held_lock *hlock,
3005 unsigned long ip)
3006{
3007 if (!debug_locks_off())
3008 return 0;
3009 if (debug_locks_silent)
3010 return 0;
3011
3012 printk("\n");
3013 printk("==================================\n");
3014 printk("[ BUG: Nested lock was not taken ]\n");
3015 print_kernel_ident();
3016 printk("----------------------------------\n");
3017
3018 printk("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
3019 print_lock(hlock);
3020
3021 printk("\nbut this task is not holding:\n");
3022 printk("%s\n", hlock->nest_lock->name);
3023
3024 printk("\nstack backtrace:\n");
3025 dump_stack();
3026
3027 printk("\nother info that might help us debug this:\n");
3028 lockdep_print_held_locks(curr);
3029
3030 printk("\nstack backtrace:\n");
3031 dump_stack();
3032
3033 return 0;
3034}
3035
3036static int __lock_is_held(struct lockdep_map *lock);
3037
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003038/*
3039 * This gets called for every mutex_lock*()/spin_lock*() operation.
3040 * We maintain the dependency maps and validate the locking attempt:
3041 */
3042static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3043 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003044 struct lockdep_map *nest_lock, unsigned long ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003045 int references, int pin_count)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003046{
3047 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07003048 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003049 struct held_lock *hlock;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003050 unsigned int depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003051 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003052 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003053 u64 chain_key;
3054
3055 if (unlikely(!debug_locks))
3056 return 0;
3057
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003058 /*
3059 * Lockdep should run with IRQs disabled, otherwise we could
3060 * get an interrupt which would want to take locks, which would
3061 * end up in lockdep and have you got a head-ache already?
3062 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003063 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3064 return 0;
3065
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003066 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3067 check = 0;
Peter Zijlstra1704f472010-03-19 01:37:42 +01003068
Hitoshi Mitake62016252010-10-05 18:01:51 +09003069 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3070 class = lock->class_cache[subclass];
Ingo Molnard6d897c2006-07-10 04:44:04 -07003071 /*
Hitoshi Mitake62016252010-10-05 18:01:51 +09003072 * Not cached?
Ingo Molnard6d897c2006-07-10 04:44:04 -07003073 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003074 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003075 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003076 if (!class)
3077 return 0;
3078 }
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003079 atomic_inc((atomic_t *)&class->ops);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003080 if (very_verbose(class)) {
3081 printk("\nacquire class [%p] %s", class->key, class->name);
3082 if (class->name_version > 1)
3083 printk("#%d", class->name_version);
3084 printk("\n");
3085 dump_stack();
3086 }
3087
3088 /*
3089 * Add the lock to the list of currently held locks.
3090 * (we dont increase the depth just yet, up until the
3091 * dependency checks are done)
3092 */
3093 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003094 /*
3095 * Ran out of static storage for our per-task lock stack again have we?
3096 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003097 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3098 return 0;
3099
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003100 class_idx = class - lock_classes + 1;
3101
3102 if (depth) {
3103 hlock = curr->held_locks + depth - 1;
3104 if (hlock->class_idx == class_idx && nest_lock) {
3105 if (hlock->references)
3106 hlock->references++;
3107 else
3108 hlock->references = 2;
3109
3110 return 1;
3111 }
3112 }
3113
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003114 hlock = curr->held_locks + depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003115 /*
3116 * Plain impossible, we just registered it and checked it weren't no
3117 * NULL like.. I bet this mushroom I ate was good!
3118 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003119 if (DEBUG_LOCKS_WARN_ON(!class))
3120 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003121 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003122 hlock->acquire_ip = ip;
3123 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003124 hlock->nest_lock = nest_lock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003125 hlock->trylock = trylock;
3126 hlock->read = read;
3127 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04003128 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003129 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003130#ifdef CONFIG_LOCK_STAT
3131 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003132 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003133#endif
Peter Zijlstra21199f22015-09-16 16:10:40 +02003134 hlock->pin_count = pin_count;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003135
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003136 if (check && !mark_irqflags(curr, hlock))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003137 return 0;
3138
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003139 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07003140 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003141 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003142
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003143 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01003144 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003145 * lock keys along the dependency chain. We save the hash value
3146 * at every step so that we can get the current hash easily
3147 * after unlock. The chain hash is then used to cache dependency
3148 * results.
3149 *
3150 * The 'key ID' is what is the most compact key value to drive
3151 * the hash, not class->key.
3152 */
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003153 /*
3154 * Whoops, we did it again.. ran straight out of our static allocation.
3155 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003156 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003157 return 0;
3158
3159 chain_key = curr->curr_chain_key;
3160 if (!depth) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003161 /*
3162 * How can we have a chain hash when we ain't got no keys?!
3163 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003164 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3165 return 0;
3166 chain_head = 1;
3167 }
3168
3169 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003170 if (separate_irq_context(curr, hlock)) {
3171 chain_key = 0;
3172 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003173 }
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003174 chain_key = iterate_chain_key(chain_key, class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003175
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003176 if (nest_lock && !__lock_is_held(nest_lock))
3177 return print_lock_nested_lock_not_held(curr, hlock, ip);
3178
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003179 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003180 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08003181
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003182 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003183 curr->lockdep_depth++;
3184 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08003185#ifdef CONFIG_DEBUG_LOCKDEP
3186 if (unlikely(!debug_locks))
3187 return 0;
3188#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003189 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3190 debug_locks_off();
Dave Jones2c522832013-04-25 13:40:02 -04003191 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3192 printk(KERN_DEBUG "depth: %i max: %lu!\n",
Ben Greearc0540602013-02-06 10:56:19 -08003193 curr->lockdep_depth, MAX_LOCK_DEPTH);
Ben Greearc0540602013-02-06 10:56:19 -08003194
3195 lockdep_print_held_locks(current);
3196 debug_show_all_locks();
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01003197 dump_stack();
Ben Greearc0540602013-02-06 10:56:19 -08003198
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003199 return 0;
3200 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08003201
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003202 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3203 max_lockdep_depth = curr->lockdep_depth;
3204
3205 return 1;
3206}
3207
3208static int
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303209print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003210 unsigned long ip)
3211{
3212 if (!debug_locks_off())
3213 return 0;
3214 if (debug_locks_silent)
3215 return 0;
3216
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003217 printk("\n");
3218 printk("=====================================\n");
3219 printk("[ BUG: bad unlock balance detected! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003220 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003221 printk("-------------------------------------\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003222 printk("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003223 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003224 print_lockdep_cache(lock);
3225 printk(") at:\n");
3226 print_ip_sym(ip);
3227 printk("but there are no more locks to release!\n");
3228 printk("\nother info that might help us debug this:\n");
3229 lockdep_print_held_locks(curr);
3230
3231 printk("\nstack backtrace:\n");
3232 dump_stack();
3233
3234 return 0;
3235}
3236
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003237static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3238{
3239 if (hlock->instance == lock)
3240 return 1;
3241
3242 if (hlock->references) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09003243 struct lock_class *class = lock->class_cache[0];
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003244
3245 if (!class)
3246 class = look_up_lock_class(lock, 0);
3247
Peter Zijlstra80e04012011-08-05 14:26:17 +02003248 /*
3249 * If look_up_lock_class() failed to find a class, we're trying
3250 * to test if we hold a lock that has never yet been acquired.
3251 * Clearly if the lock hasn't been acquired _ever_, we're not
3252 * holding it either, so report failure.
3253 */
3254 if (!class)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003255 return 0;
3256
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003257 /*
3258 * References, but not a lock we're actually ref-counting?
3259 * State got messed up, follow the sites that change ->references
3260 * and try to make sense of it.
3261 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003262 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3263 return 0;
3264
3265 if (hlock->class_idx == class - lock_classes + 1)
3266 return 1;
3267 }
3268
3269 return 0;
3270}
3271
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003272static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003273__lock_set_class(struct lockdep_map *lock, const char *name,
3274 struct lock_class_key *key, unsigned int subclass,
3275 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003276{
3277 struct task_struct *curr = current;
3278 struct held_lock *hlock, *prev_hlock;
3279 struct lock_class *class;
3280 unsigned int depth;
3281 int i;
3282
3283 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003284 /*
3285 * This function is about (re)setting the class of a held lock,
3286 * yet we're not actually holding any locks. Naughty user!
3287 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003288 if (DEBUG_LOCKS_WARN_ON(!depth))
3289 return 0;
3290
3291 prev_hlock = NULL;
3292 for (i = depth-1; i >= 0; i--) {
3293 hlock = curr->held_locks + i;
3294 /*
3295 * We must not cross into another context:
3296 */
3297 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3298 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003299 if (match_held_lock(hlock, lock))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003300 goto found_it;
3301 prev_hlock = hlock;
3302 }
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303303 return print_unlock_imbalance_bug(curr, lock, ip);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003304
3305found_it:
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003306 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003307 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02003308 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003309
3310 curr->lockdep_depth = i;
3311 curr->curr_chain_key = hlock->prev_chain_key;
3312
3313 for (; i < depth; i++) {
3314 hlock = curr->held_locks + i;
3315 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003316 hlock_class(hlock)->subclass, hlock->trylock,
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003317 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003318 hlock->nest_lock, hlock->acquire_ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003319 hlock->references, hlock->pin_count))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003320 return 0;
3321 }
3322
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003323 /*
3324 * I took it apart and put it back together again, except now I have
3325 * these 'spare' parts.. where shall I put them.
3326 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003327 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3328 return 0;
3329 return 1;
3330}
3331
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003332/*
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003333 * Remove the lock to the list of currently held locks - this gets
3334 * called on mutex_unlock()/spin_unlock*() (or on a failed
3335 * mutex_lock_interruptible()).
3336 *
3337 * @nested is an hysterical artifact, needs a tree wide cleanup.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003338 */
3339static int
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003340__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003341{
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003342 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003343 struct held_lock *hlock, *prev_hlock;
3344 unsigned int depth;
3345 int i;
3346
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003347 if (unlikely(!debug_locks))
3348 return 0;
3349
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003350 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003351 /*
3352 * So we're all set to release this lock.. wait what lock? We don't
3353 * own any locks, you've been drinking again?
3354 */
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003355 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3356 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003357
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003358 /*
3359 * Check whether the lock exists in the current stack
3360 * of held locks:
3361 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003362 prev_hlock = NULL;
3363 for (i = depth-1; i >= 0; i--) {
3364 hlock = curr->held_locks + i;
3365 /*
3366 * We must not cross into another context:
3367 */
3368 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3369 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003370 if (match_held_lock(hlock, lock))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003371 goto found_it;
3372 prev_hlock = hlock;
3373 }
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303374 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003375
3376found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003377 if (hlock->instance == lock)
3378 lock_release_holdtime(hlock);
3379
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003380 WARN(hlock->pin_count, "releasing a pinned lock\n");
3381
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003382 if (hlock->references) {
3383 hlock->references--;
3384 if (hlock->references) {
3385 /*
3386 * We had, and after removing one, still have
3387 * references, the current lock stack is still
3388 * valid. We're done!
3389 */
3390 return 1;
3391 }
3392 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003393
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003394 /*
3395 * We have the right lock to unlock, 'hlock' points to it.
3396 * Now we remove it from the stack, and add back the other
3397 * entries (if any), recalculating the hash along the way:
3398 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003399
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003400 curr->lockdep_depth = i;
3401 curr->curr_chain_key = hlock->prev_chain_key;
3402
3403 for (i++; i < depth; i++) {
3404 hlock = curr->held_locks + i;
3405 if (!__lock_acquire(hlock->instance,
Dave Jonesf82b2172008-08-11 09:30:23 +02003406 hlock_class(hlock)->subclass, hlock->trylock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003407 hlock->read, hlock->check, hlock->hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003408 hlock->nest_lock, hlock->acquire_ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003409 hlock->references, hlock->pin_count))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003410 return 0;
3411 }
3412
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003413 /*
3414 * We had N bottles of beer on the wall, we drank one, but now
3415 * there's not N-1 bottles of beer left on the wall...
3416 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003417 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3418 return 0;
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003419
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003420 return 1;
3421}
3422
Peter Zijlstraf607c662009-07-20 19:16:29 +02003423static int __lock_is_held(struct lockdep_map *lock)
3424{
3425 struct task_struct *curr = current;
3426 int i;
3427
3428 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003429 struct held_lock *hlock = curr->held_locks + i;
3430
3431 if (match_held_lock(hlock, lock))
Peter Zijlstraf607c662009-07-20 19:16:29 +02003432 return 1;
3433 }
3434
3435 return 0;
3436}
3437
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003438static void __lock_pin_lock(struct lockdep_map *lock)
3439{
3440 struct task_struct *curr = current;
3441 int i;
3442
3443 if (unlikely(!debug_locks))
3444 return;
3445
3446 for (i = 0; i < curr->lockdep_depth; i++) {
3447 struct held_lock *hlock = curr->held_locks + i;
3448
3449 if (match_held_lock(hlock, lock)) {
3450 hlock->pin_count++;
3451 return;
3452 }
3453 }
3454
3455 WARN(1, "pinning an unheld lock\n");
3456}
3457
3458static void __lock_unpin_lock(struct lockdep_map *lock)
3459{
3460 struct task_struct *curr = current;
3461 int i;
3462
3463 if (unlikely(!debug_locks))
3464 return;
3465
3466 for (i = 0; i < curr->lockdep_depth; i++) {
3467 struct held_lock *hlock = curr->held_locks + i;
3468
3469 if (match_held_lock(hlock, lock)) {
3470 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
3471 return;
3472
3473 hlock->pin_count--;
3474 return;
3475 }
3476 }
3477
3478 WARN(1, "unpinning an unheld lock\n");
3479}
3480
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003481/*
3482 * Check whether we follow the irq-flags state precisely:
3483 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003484static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003485{
Ingo Molnar992860e2008-07-14 10:28:38 +02003486#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3487 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003488 if (!debug_locks)
3489 return;
3490
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01003491 if (irqs_disabled_flags(flags)) {
3492 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3493 printk("possible reason: unannotated irqs-off.\n");
3494 }
3495 } else {
3496 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3497 printk("possible reason: unannotated irqs-on.\n");
3498 }
3499 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003500
3501 /*
3502 * We dont accurately track softirq state in e.g.
3503 * hardirq contexts (such as on 4KSTACKS), so only
3504 * check if not in hardirq contexts:
3505 */
3506 if (!hardirq_count()) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003507 if (softirq_count()) {
3508 /* like the above, but with softirqs */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003509 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003510 } else {
3511 /* lick the above, does it taste good? */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003512 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003513 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003514 }
3515
3516 if (!debug_locks)
3517 print_irqtrace_events(current);
3518#endif
3519}
3520
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003521void lock_set_class(struct lockdep_map *lock, const char *name,
3522 struct lock_class_key *key, unsigned int subclass,
3523 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003524{
3525 unsigned long flags;
3526
3527 if (unlikely(current->lockdep_recursion))
3528 return;
3529
3530 raw_local_irq_save(flags);
3531 current->lockdep_recursion = 1;
3532 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003533 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003534 check_chain_key(current);
3535 current->lockdep_recursion = 0;
3536 raw_local_irq_restore(flags);
3537}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003538EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003539
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003540/*
3541 * We are not always called with irqs disabled - do that here,
3542 * and also avoid lockdep recursion:
3543 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003544void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003545 int trylock, int read, int check,
3546 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003547{
3548 unsigned long flags;
3549
3550 if (unlikely(current->lockdep_recursion))
3551 return;
3552
3553 raw_local_irq_save(flags);
3554 check_flags(flags);
3555
3556 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003557 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003558 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003559 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003560 current->lockdep_recursion = 0;
3561 raw_local_irq_restore(flags);
3562}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003563EXPORT_SYMBOL_GPL(lock_acquire);
3564
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003565void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003566 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003567{
3568 unsigned long flags;
3569
3570 if (unlikely(current->lockdep_recursion))
3571 return;
3572
3573 raw_local_irq_save(flags);
3574 check_flags(flags);
3575 current->lockdep_recursion = 1;
Frederic Weisbecker93135432010-05-08 06:24:25 +02003576 trace_lock_release(lock, ip);
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003577 if (__lock_release(lock, nested, ip))
3578 check_chain_key(current);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003579 current->lockdep_recursion = 0;
3580 raw_local_irq_restore(flags);
3581}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003582EXPORT_SYMBOL_GPL(lock_release);
3583
Peter Zijlstraf607c662009-07-20 19:16:29 +02003584int lock_is_held(struct lockdep_map *lock)
3585{
3586 unsigned long flags;
3587 int ret = 0;
3588
3589 if (unlikely(current->lockdep_recursion))
Peter Zijlstraf2513cd2011-06-06 12:32:43 +02003590 return 1; /* avoid false negative lockdep_assert_held() */
Peter Zijlstraf607c662009-07-20 19:16:29 +02003591
3592 raw_local_irq_save(flags);
3593 check_flags(flags);
3594
3595 current->lockdep_recursion = 1;
3596 ret = __lock_is_held(lock);
3597 current->lockdep_recursion = 0;
3598 raw_local_irq_restore(flags);
3599
3600 return ret;
3601}
3602EXPORT_SYMBOL_GPL(lock_is_held);
3603
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003604void lock_pin_lock(struct lockdep_map *lock)
3605{
3606 unsigned long flags;
3607
3608 if (unlikely(current->lockdep_recursion))
3609 return;
3610
3611 raw_local_irq_save(flags);
3612 check_flags(flags);
3613
3614 current->lockdep_recursion = 1;
3615 __lock_pin_lock(lock);
3616 current->lockdep_recursion = 0;
3617 raw_local_irq_restore(flags);
3618}
3619EXPORT_SYMBOL_GPL(lock_pin_lock);
3620
3621void lock_unpin_lock(struct lockdep_map *lock)
3622{
3623 unsigned long flags;
3624
3625 if (unlikely(current->lockdep_recursion))
3626 return;
3627
3628 raw_local_irq_save(flags);
3629 check_flags(flags);
3630
3631 current->lockdep_recursion = 1;
3632 __lock_unpin_lock(lock);
3633 current->lockdep_recursion = 0;
3634 raw_local_irq_restore(flags);
3635}
3636EXPORT_SYMBOL_GPL(lock_unpin_lock);
3637
Nick Piggincf40bd12009-01-21 08:12:39 +01003638void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3639{
3640 current->lockdep_reclaim_gfp = gfp_mask;
3641}
3642
3643void lockdep_clear_current_reclaim_state(void)
3644{
3645 current->lockdep_reclaim_gfp = 0;
3646}
3647
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003648#ifdef CONFIG_LOCK_STAT
3649static int
3650print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3651 unsigned long ip)
3652{
3653 if (!debug_locks_off())
3654 return 0;
3655 if (debug_locks_silent)
3656 return 0;
3657
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003658 printk("\n");
3659 printk("=================================\n");
3660 printk("[ BUG: bad contention detected! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003661 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07003662 printk("---------------------------------\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003663 printk("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003664 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003665 print_lockdep_cache(lock);
3666 printk(") at:\n");
3667 print_ip_sym(ip);
3668 printk("but there are no locks held!\n");
3669 printk("\nother info that might help us debug this:\n");
3670 lockdep_print_held_locks(curr);
3671
3672 printk("\nstack backtrace:\n");
3673 dump_stack();
3674
3675 return 0;
3676}
3677
3678static void
3679__lock_contended(struct lockdep_map *lock, unsigned long ip)
3680{
3681 struct task_struct *curr = current;
3682 struct held_lock *hlock, *prev_hlock;
3683 struct lock_class_stats *stats;
3684 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003685 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003686
3687 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003688 /*
3689 * Whee, we contended on this lock, except it seems we're not
3690 * actually trying to acquire anything much at all..
3691 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003692 if (DEBUG_LOCKS_WARN_ON(!depth))
3693 return;
3694
3695 prev_hlock = NULL;
3696 for (i = depth-1; i >= 0; i--) {
3697 hlock = curr->held_locks + i;
3698 /*
3699 * We must not cross into another context:
3700 */
3701 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3702 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003703 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003704 goto found_it;
3705 prev_hlock = hlock;
3706 }
3707 print_lock_contention_bug(curr, lock, ip);
3708 return;
3709
3710found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003711 if (hlock->instance != lock)
3712 return;
3713
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003714 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003715
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003716 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3717 contending_point = lock_point(hlock_class(hlock)->contending_point,
3718 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003719
Dave Jonesf82b2172008-08-11 09:30:23 +02003720 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003721 if (contention_point < LOCKSTAT_POINTS)
3722 stats->contention_point[contention_point]++;
3723 if (contending_point < LOCKSTAT_POINTS)
3724 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07003725 if (lock->cpu != smp_processor_id())
3726 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003727 put_lock_stats(stats);
3728}
3729
3730static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003731__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003732{
3733 struct task_struct *curr = current;
3734 struct held_lock *hlock, *prev_hlock;
3735 struct lock_class_stats *stats;
3736 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003737 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07003738 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003739
3740 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003741 /*
3742 * Yay, we acquired ownership of this lock we didn't try to
3743 * acquire, how the heck did that happen?
3744 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003745 if (DEBUG_LOCKS_WARN_ON(!depth))
3746 return;
3747
3748 prev_hlock = NULL;
3749 for (i = depth-1; i >= 0; i--) {
3750 hlock = curr->held_locks + i;
3751 /*
3752 * We must not cross into another context:
3753 */
3754 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3755 break;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003756 if (match_held_lock(hlock, lock))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003757 goto found_it;
3758 prev_hlock = hlock;
3759 }
3760 print_lock_contention_bug(curr, lock, _RET_IP_);
3761 return;
3762
3763found_it:
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003764 if (hlock->instance != lock)
3765 return;
3766
Peter Zijlstra96645672007-07-19 01:49:00 -07003767 cpu = smp_processor_id();
3768 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003769 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07003770 waittime = now - hlock->waittime_stamp;
3771 hlock->holdtime_stamp = now;
3772 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003773
Frederic Weisbecker883a2a32010-05-08 06:16:11 +02003774 trace_lock_acquired(lock, ip);
Frederic Weisbecker20625012009-04-06 01:49:33 +02003775
Dave Jonesf82b2172008-08-11 09:30:23 +02003776 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07003777 if (waittime) {
3778 if (hlock->read)
3779 lock_time_inc(&stats->read_waittime, waittime);
3780 else
3781 lock_time_inc(&stats->write_waittime, waittime);
3782 }
3783 if (lock->cpu != cpu)
3784 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003785 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07003786
3787 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003788 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003789}
3790
3791void lock_contended(struct lockdep_map *lock, unsigned long ip)
3792{
3793 unsigned long flags;
3794
3795 if (unlikely(!lock_stat))
3796 return;
3797
3798 if (unlikely(current->lockdep_recursion))
3799 return;
3800
3801 raw_local_irq_save(flags);
3802 check_flags(flags);
3803 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003804 trace_lock_contended(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003805 __lock_contended(lock, ip);
3806 current->lockdep_recursion = 0;
3807 raw_local_irq_restore(flags);
3808}
3809EXPORT_SYMBOL_GPL(lock_contended);
3810
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003811void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003812{
3813 unsigned long flags;
3814
3815 if (unlikely(!lock_stat))
3816 return;
3817
3818 if (unlikely(current->lockdep_recursion))
3819 return;
3820
3821 raw_local_irq_save(flags);
3822 check_flags(flags);
3823 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003824 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003825 current->lockdep_recursion = 0;
3826 raw_local_irq_restore(flags);
3827}
3828EXPORT_SYMBOL_GPL(lock_acquired);
3829#endif
3830
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003831/*
3832 * Used by the testsuite, sanitize the validator state
3833 * after a simulated failure:
3834 */
3835
3836void lockdep_reset(void)
3837{
3838 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003839 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003840
3841 raw_local_irq_save(flags);
3842 current->curr_chain_key = 0;
3843 current->lockdep_depth = 0;
3844 current->lockdep_recursion = 0;
3845 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3846 nr_hardirq_chains = 0;
3847 nr_softirq_chains = 0;
3848 nr_process_chains = 0;
3849 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08003850 for (i = 0; i < CHAINHASH_SIZE; i++)
Andrew Mortona63f38c2016-02-03 13:44:12 -08003851 INIT_HLIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003852 raw_local_irq_restore(flags);
3853}
3854
3855static void zap_class(struct lock_class *class)
3856{
3857 int i;
3858
3859 /*
3860 * Remove all dependencies this lock is
3861 * involved in:
3862 */
3863 for (i = 0; i < nr_list_entries; i++) {
3864 if (list_entries[i].class == class)
3865 list_del_rcu(&list_entries[i].entry);
3866 }
3867 /*
3868 * Unhash the class and remove it from the all_lock_classes list:
3869 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08003870 hlist_del_rcu(&class->hash_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003871 list_del_rcu(&class->lock_entry);
3872
Peter Zijlstracee34d82015-06-02 12:50:13 +02003873 RCU_INIT_POINTER(class->key, NULL);
3874 RCU_INIT_POINTER(class->name, NULL);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003875}
3876
Arjan van de Venfabe8742008-01-24 07:00:45 +01003877static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003878{
3879 return addr >= start && addr < start + size;
3880}
3881
Peter Zijlstra35a93932015-02-26 16:23:11 +01003882/*
3883 * Used in module.c to remove lock classes from memory that is going to be
3884 * freed; and possibly re-used by other modules.
3885 *
3886 * We will have had one sync_sched() before getting here, so we're guaranteed
3887 * nobody will look up these exact classes -- they're properly dead but still
3888 * allocated.
3889 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003890void lockdep_free_key_range(void *start, unsigned long size)
3891{
Peter Zijlstra35a93932015-02-26 16:23:11 +01003892 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08003893 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003894 unsigned long flags;
3895 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01003896 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003897
3898 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01003899 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003900
3901 /*
3902 * Unhash all classes that were created by this module:
3903 */
3904 for (i = 0; i < CLASSHASH_SIZE; i++) {
3905 head = classhash_table + i;
Andrew Mortona63f38c2016-02-03 13:44:12 -08003906 hlist_for_each_entry_rcu(class, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003907 if (within(class->key, start, size))
3908 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01003909 else if (within(class->name, start, size))
3910 zap_class(class);
3911 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003912 }
3913
Nick Piggin5a26db52008-01-16 09:51:58 +01003914 if (locked)
3915 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003916 raw_local_irq_restore(flags);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003917
3918 /*
3919 * Wait for any possible iterators from look_up_lock_class() to pass
3920 * before continuing to free the memory they refer to.
3921 *
3922 * sync_sched() is sufficient because the read-side is IRQ disable.
3923 */
3924 synchronize_sched();
3925
3926 /*
3927 * XXX at this point we could return the resources to the pool;
3928 * instead we leak them. We would need to change to bitmap allocators
3929 * instead of the linear allocators we have now.
3930 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003931}
3932
3933void lockdep_reset_lock(struct lockdep_map *lock)
3934{
Peter Zijlstra35a93932015-02-26 16:23:11 +01003935 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08003936 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003937 unsigned long flags;
3938 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01003939 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003940
3941 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003942
3943 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07003944 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003945 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07003946 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3947 /*
3948 * If the class exists we look it up and zap it:
3949 */
3950 class = look_up_lock_class(lock, j);
3951 if (class)
3952 zap_class(class);
3953 }
3954 /*
3955 * Debug check: in the end all mapped classes should
3956 * be gone.
3957 */
Nick Piggin5a26db52008-01-16 09:51:58 +01003958 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003959 for (i = 0; i < CLASSHASH_SIZE; i++) {
3960 head = classhash_table + i;
Andrew Mortona63f38c2016-02-03 13:44:12 -08003961 hlist_for_each_entry_rcu(class, head, hash_entry) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09003962 int match = 0;
3963
3964 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
3965 match |= class == lock->class_cache[j];
3966
3967 if (unlikely(match)) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003968 if (debug_locks_off_graph_unlock()) {
3969 /*
3970 * We all just reset everything, how did it match?
3971 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08003972 WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003973 }
Ingo Molnard6d897c2006-07-10 04:44:04 -07003974 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003975 }
3976 }
3977 }
Nick Piggin5a26db52008-01-16 09:51:58 +01003978 if (locked)
3979 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07003980
3981out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003982 raw_local_irq_restore(flags);
3983}
3984
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003985void __init lockdep_info(void)
3986{
3987 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3988
Li Zefanb0788ca2008-11-21 15:57:32 +08003989 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003990 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3991 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08003992 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003993 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3994 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3995 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3996
3997 printk(" memory used by lock dependency info: %lu kB\n",
3998 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3999 sizeof(struct list_head) * CLASSHASH_SIZE +
4000 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
4001 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei90629202009-08-02 21:43:36 +08004002 sizeof(struct list_head) * CHAINHASH_SIZE
Ming Lei4dd861d2009-07-16 15:44:29 +02004003#ifdef CONFIG_PROVE_LOCKING
Ming Leie351b662009-07-22 22:48:09 +08004004 + sizeof(struct circular_queue)
Ming Lei4dd861d2009-07-16 15:44:29 +02004005#endif
Ming Lei90629202009-08-02 21:43:36 +08004006 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02004007 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004008
4009 printk(" per task-struct memory footprint: %lu bytes\n",
4010 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004011}
4012
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004013static void
4014print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07004015 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004016{
4017 if (!debug_locks_off())
4018 return;
4019 if (debug_locks_silent)
4020 return;
4021
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004022 printk("\n");
4023 printk("=========================\n");
4024 printk("[ BUG: held lock freed! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004025 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004026 printk("-------------------------\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004027 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004028 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07004029 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004030 lockdep_print_held_locks(curr);
4031
4032 printk("\nstack backtrace:\n");
4033 dump_stack();
4034}
4035
Oleg Nesterov54561782007-12-05 15:46:09 +01004036static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4037 const void* lock_from, unsigned long lock_len)
4038{
4039 return lock_from + lock_len <= mem_from ||
4040 mem_from + mem_len <= lock_from;
4041}
4042
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004043/*
4044 * Called when kernel memory is freed (or unmapped), or if a lock
4045 * is destroyed or reinitialized - this code checks whether there is
4046 * any held lock in the memory range of <from> to <to>:
4047 */
4048void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4049{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004050 struct task_struct *curr = current;
4051 struct held_lock *hlock;
4052 unsigned long flags;
4053 int i;
4054
4055 if (unlikely(!debug_locks))
4056 return;
4057
4058 local_irq_save(flags);
4059 for (i = 0; i < curr->lockdep_depth; i++) {
4060 hlock = curr->held_locks + i;
4061
Oleg Nesterov54561782007-12-05 15:46:09 +01004062 if (not_in_range(mem_from, mem_len, hlock->instance,
4063 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004064 continue;
4065
Oleg Nesterov54561782007-12-05 15:46:09 +01004066 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004067 break;
4068 }
4069 local_irq_restore(flags);
4070}
Peter Zijlstraed075362006-12-06 20:35:24 -08004071EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004072
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004073static void print_held_locks_bug(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004074{
4075 if (!debug_locks_off())
4076 return;
4077 if (debug_locks_silent)
4078 return;
4079
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004080 printk("\n");
4081 printk("=====================================\n");
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004082 printk("[ BUG: %s/%d still has locks held! ]\n",
4083 current->comm, task_pid_nr(current));
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004084 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004085 printk("-------------------------------------\n");
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004086 lockdep_print_held_locks(current);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004087 printk("\nstack backtrace:\n");
4088 dump_stack();
4089}
4090
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004091void debug_check_no_locks_held(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004092{
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004093 if (unlikely(current->lockdep_depth > 0))
4094 print_held_locks_bug();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004095}
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004096EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004097
Sasha Levin8dce7a92013-06-13 18:41:16 -04004098#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004099void debug_show_all_locks(void)
4100{
4101 struct task_struct *g, *p;
4102 int count = 10;
4103 int unlock = 1;
4104
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004105 if (unlikely(!debug_locks)) {
4106 printk("INFO: lockdep is turned off.\n");
4107 return;
4108 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004109 printk("\nShowing all locks held in the system:\n");
4110
4111 /*
4112 * Here we try to get the tasklist_lock as hard as possible,
4113 * if not successful after 2 seconds we ignore it (but keep
4114 * trying). This is to enable a debug printout even if a
4115 * tasklist_lock-holding task deadlocks or crashes.
4116 */
4117retry:
4118 if (!read_trylock(&tasklist_lock)) {
4119 if (count == 10)
4120 printk("hm, tasklist_lock locked, retrying... ");
4121 if (count) {
4122 count--;
4123 printk(" #%d", 10-count);
4124 mdelay(200);
4125 goto retry;
4126 }
4127 printk(" ignoring it.\n");
4128 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08004129 } else {
4130 if (count != 10)
4131 printk(KERN_CONT " locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004132 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004133
4134 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01004135 /*
4136 * It's not reliable to print a task's held locks
4137 * if it's not sleeping (or if it's not the current
4138 * task):
4139 */
4140 if (p->state == TASK_RUNNING && p != current)
4141 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004142 if (p->lockdep_depth)
4143 lockdep_print_held_locks(p);
4144 if (!unlock)
4145 if (read_trylock(&tasklist_lock))
4146 unlock = 1;
4147 } while_each_thread(g, p);
4148
4149 printk("\n");
4150 printk("=============================================\n\n");
4151
4152 if (unlock)
4153 read_unlock(&tasklist_lock);
4154}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004155EXPORT_SYMBOL_GPL(debug_show_all_locks);
Sasha Levin8dce7a92013-06-13 18:41:16 -04004156#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004157
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004158/*
4159 * Careful: only use this function if you are sure that
4160 * the task cannot run in parallel!
4161 */
John Kacurf1b499f2010-08-05 17:10:53 +02004162void debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004163{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004164 if (unlikely(!debug_locks)) {
4165 printk("INFO: lockdep is turned off.\n");
4166 return;
4167 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004168 lockdep_print_held_locks(task);
4169}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004170EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02004171
Andi Kleen722a9f92014-05-02 00:44:38 +02004172asmlinkage __visible void lockdep_sys_exit(void)
Peter Zijlstrab351d162007-10-11 22:11:12 +02004173{
4174 struct task_struct *curr = current;
4175
4176 if (unlikely(curr->lockdep_depth)) {
4177 if (!debug_locks_off())
4178 return;
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004179 printk("\n");
4180 printk("================================================\n");
4181 printk("[ BUG: lock held when returning to user space! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004182 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004183 printk("------------------------------------------------\n");
Peter Zijlstrab351d162007-10-11 22:11:12 +02004184 printk("%s/%d is leaving the kernel with locks still held!\n",
4185 curr->comm, curr->pid);
4186 lockdep_print_held_locks(curr);
4187 }
4188}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004189
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004190void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004191{
4192 struct task_struct *curr = current;
4193
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004194#ifndef CONFIG_PROVE_RCU_REPEATEDLY
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004195 if (!debug_locks_off())
4196 return;
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004197#endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4198 /* Note: the following can be executed concurrently, so be careful. */
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004199 printk("\n");
4200 printk("===============================\n");
4201 printk("[ INFO: suspicious RCU usage. ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004202 print_kernel_ident();
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004203 printk("-------------------------------\n");
4204 printk("%s:%d %s!\n", file, line, s);
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004205 printk("\nother info that might help us debug this:\n\n");
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004206 printk("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4207 !rcu_lockdep_current_cpu_online()
4208 ? "RCU used illegally from offline CPU!\n"
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004209 : !rcu_is_watching()
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004210 ? "RCU used illegally from idle CPU!\n"
4211 : "",
4212 rcu_scheduler_active, debug_locks);
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004213
4214 /*
4215 * If a CPU is in the RCU-free window in idle (ie: in the section
4216 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4217 * considers that CPU to be in an "extended quiescent state",
4218 * which means that RCU will be completely ignoring that CPU.
4219 * Therefore, rcu_read_lock() and friends have absolutely no
4220 * effect on a CPU running in that state. In other words, even if
4221 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4222 * delete data structures out from under it. RCU really has no
4223 * choice here: we need to keep an RCU-free window in idle where
4224 * the CPU may possibly enter into low power mode. This way we can
4225 * notice an extended quiescent state to other CPUs that started a grace
4226 * period. Otherwise we would delay any grace period as long as we run
4227 * in the idle task.
4228 *
4229 * So complain bitterly if someone does call rcu_read_lock(),
4230 * rcu_read_lock_bh() and so on from extended quiescent states.
4231 */
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004232 if (!rcu_is_watching())
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004233 printk("RCU used illegally from extended quiescent state!\n");
4234
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004235 lockdep_print_held_locks(curr);
4236 printk("\nstack backtrace:\n");
4237 dump_stack();
4238}
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004239EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);