blob: e36e652d996fe682157c52768949f0b9c7f1e6d1 [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>
Ingo Molnare6017572017-02-01 16:36:40 +010031#include <linux/sched/clock.h>
Ingo Molnar29930022017-02-08 18:51:36 +010032#include <linux/sched/task.h>
Nikolay Borisov6d7225f2017-05-03 14:53:05 -070033#include <linux/sched/mm.h>
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070034#include <linux/delay.h>
35#include <linux/module.h>
36#include <linux/proc_fs.h>
37#include <linux/seq_file.h>
38#include <linux/spinlock.h>
39#include <linux/kallsyms.h>
40#include <linux/interrupt.h>
41#include <linux/stacktrace.h>
42#include <linux/debug_locks.h>
43#include <linux/irqflags.h>
Dave Jones99de0552006-09-29 02:00:10 -070044#include <linux/utsname.h>
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -070045#include <linux/hash.h>
Steven Rostedt81d68a92008-05-12 21:20:42 +020046#include <linux/ftrace.h>
Peter Zijlstrab4b136f2009-01-29 14:50:36 +010047#include <linux/stringify.h>
Ming Leid588e462009-07-16 15:44:29 +020048#include <linux/bitops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/gfp.h>
Yong Zhangd3d03d42011-11-09 16:04:51 +080050#include <linux/kmemcheck.h>
Peter Zijlstrae7904a22015-08-01 19:25:08 +020051#include <linux/random.h>
Peter Zijlstradfaaf3f2016-05-30 18:31:33 +020052#include <linux/jhash.h>
Peter Zijlstraaf012962009-07-16 15:44:29 +020053
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070054#include <asm/sections.h>
55
56#include "lockdep_internals.h"
57
Steven Rostedta8d154b2009-04-10 09:36:00 -040058#define CREATE_TRACE_POINTS
Frederic Weisbecker67178762009-11-13 10:06:34 +010059#include <trace/events/lock.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040060
Byungchul Parkb09be672017-08-07 16:12:52 +090061#ifdef CONFIG_LOCKDEP_CROSSRELEASE
62#include <linux/slab.h>
63#endif
64
Peter Zijlstraf20786f2007-07-19 01:48:56 -070065#ifdef CONFIG_PROVE_LOCKING
66int prove_locking = 1;
67module_param(prove_locking, int, 0644);
68#else
69#define prove_locking 0
70#endif
71
72#ifdef CONFIG_LOCK_STAT
73int lock_stat = 1;
74module_param(lock_stat, int, 0644);
75#else
76#define lock_stat 0
77#endif
78
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070079/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080080 * lockdep_lock: protects the lockdep graph, the hashes and the
81 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070082 *
83 * This is one of the rare exceptions where it's justified
84 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080085 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070086 */
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010087static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Ingo Molnar74c383f2006-12-13 00:34:43 -080088
89static int graph_lock(void)
90{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010091 arch_spin_lock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080092 /*
93 * Make sure that if another CPU detected a bug while
94 * walking the graph we dont change it (while the other
95 * CPU is busy printing out stuff with the graph lock
96 * dropped already)
97 */
98 if (!debug_locks) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010099 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800100 return 0;
101 }
Steven Rostedtbb065af2008-05-12 21:21:00 +0200102 /* prevent any recursions within lockdep from causing deadlocks */
103 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -0800104 return 1;
105}
106
107static inline int graph_unlock(void)
108{
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200109 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
110 /*
111 * The lockdep graph lock isn't locked while we expect it to
112 * be, we're confused now, bye!
113 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800114 return DEBUG_LOCKS_WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200115 }
Jarek Poplawski381a2292007-02-10 01:44:58 -0800116
Steven Rostedtbb065af2008-05-12 21:21:00 +0200117 current->lockdep_recursion--;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100118 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800119 return 0;
120}
121
122/*
123 * Turn lock debugging off and return with 0 if it was off already,
124 * and also release the graph lock:
125 */
126static inline int debug_locks_off_graph_unlock(void)
127{
128 int ret = debug_locks_off();
129
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100130 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800131
132 return ret;
133}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700134
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700135unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200136static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700137
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700138/*
139 * All data structures here are protected by the global debug_lock.
140 *
141 * Mutex key structs only get allocated, once during bootup, and never
142 * get freed - this significantly simplifies the debugging code.
143 */
144unsigned long nr_lock_classes;
145static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
146
Dave Jonesf82b2172008-08-11 09:30:23 +0200147static inline struct lock_class *hlock_class(struct held_lock *hlock)
148{
149 if (!hlock->class_idx) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200150 /*
151 * Someone passed in garbage, we give up.
152 */
Dave Jonesf82b2172008-08-11 09:30:23 +0200153 DEBUG_LOCKS_WARN_ON(1);
154 return NULL;
155 }
156 return lock_classes + hlock->class_idx - 1;
157}
158
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700159#ifdef CONFIG_LOCK_STAT
Peter Zijlstra25528212016-03-15 14:52:49 -0700160static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700161
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200162static inline u64 lockstat_clock(void)
163{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200164 return local_clock();
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200165}
166
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200167static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700168{
169 int i;
170
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200171 for (i = 0; i < LOCKSTAT_POINTS; i++) {
172 if (points[i] == 0) {
173 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700174 break;
175 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200176 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700177 break;
178 }
179
180 return i;
181}
182
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200183static void lock_time_inc(struct lock_time *lt, u64 time)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700184{
185 if (time > lt->max)
186 lt->max = time;
187
Frank Rowand109d71c2009-11-19 13:42:06 -0800188 if (time < lt->min || !lt->nr)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700189 lt->min = time;
190
191 lt->total += time;
192 lt->nr++;
193}
194
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700195static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
196{
Frank Rowand109d71c2009-11-19 13:42:06 -0800197 if (!src->nr)
198 return;
199
200 if (src->max > dst->max)
201 dst->max = src->max;
202
203 if (src->min < dst->min || !dst->nr)
204 dst->min = src->min;
205
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700206 dst->total += src->total;
207 dst->nr += src->nr;
208}
209
210struct lock_class_stats lock_stats(struct lock_class *class)
211{
212 struct lock_class_stats stats;
213 int cpu, i;
214
215 memset(&stats, 0, sizeof(struct lock_class_stats));
216 for_each_possible_cpu(cpu) {
217 struct lock_class_stats *pcs =
Tejun Heo1871e522009-10-29 22:34:13 +0900218 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700219
220 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
221 stats.contention_point[i] += pcs->contention_point[i];
222
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200223 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
224 stats.contending_point[i] += pcs->contending_point[i];
225
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700226 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
227 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
228
229 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
230 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700231
232 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
233 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700234 }
235
236 return stats;
237}
238
239void clear_lock_stats(struct lock_class *class)
240{
241 int cpu;
242
243 for_each_possible_cpu(cpu) {
244 struct lock_class_stats *cpu_stats =
Tejun Heo1871e522009-10-29 22:34:13 +0900245 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700246
247 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
248 }
249 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200250 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700251}
252
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700253static struct lock_class_stats *get_lock_stats(struct lock_class *class)
254{
Tejun Heo1871e522009-10-29 22:34:13 +0900255 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700256}
257
258static void put_lock_stats(struct lock_class_stats *stats)
259{
Tejun Heo1871e522009-10-29 22:34:13 +0900260 put_cpu_var(cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700261}
262
263static void lock_release_holdtime(struct held_lock *hlock)
264{
265 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200266 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700267
268 if (!lock_stat)
269 return;
270
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200271 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700272
Dave Jonesf82b2172008-08-11 09:30:23 +0200273 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700274 if (hlock->read)
275 lock_time_inc(&stats->read_holdtime, holdtime);
276 else
277 lock_time_inc(&stats->write_holdtime, holdtime);
278 put_lock_stats(stats);
279}
280#else
281static inline void lock_release_holdtime(struct held_lock *hlock)
282{
283}
284#endif
285
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700286/*
287 * We keep a global list of all lock classes. The list only grows,
288 * never shrinks. The list is only accessed with the lockdep
289 * spinlock lock held.
290 */
291LIST_HEAD(all_lock_classes);
292
293/*
294 * The lockdep classes are in a hash-table as well, for fast lookup:
295 */
296#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
297#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700298#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700299#define classhashentry(key) (classhash_table + __classhashfn((key)))
300
Andrew Mortona63f38c2016-02-03 13:44:12 -0800301static struct hlist_head classhash_table[CLASSHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700302
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700303/*
304 * We put the lock dependency chains into a hash-table as well, to cache
305 * their existence:
306 */
307#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
308#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700309#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700310#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
311
Andrew Mortona63f38c2016-02-03 13:44:12 -0800312static struct hlist_head chainhash_table[CHAINHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700313
314/*
315 * The hash key of the lock dependency chains is a hash itself too:
316 * it's a hash of all locks taken up to that lock, including that lock.
317 * It's a 64-bit hash, because it's important for the keys to be
318 * unique.
319 */
Peter Zijlstradfaaf3f2016-05-30 18:31:33 +0200320static inline u64 iterate_chain_key(u64 key, u32 idx)
321{
322 u32 k0 = key, k1 = key >> 32;
323
324 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
325
326 return k0 | (u64)k1 << 32;
327}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700328
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200329void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700330{
331 current->lockdep_recursion++;
332}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700333EXPORT_SYMBOL(lockdep_off);
334
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200335void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700336{
337 current->lockdep_recursion--;
338}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700339EXPORT_SYMBOL(lockdep_on);
340
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700341/*
342 * Debugging switches:
343 */
344
345#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800346#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700347
348#if VERBOSE
349# define HARDIRQ_VERBOSE 1
350# define SOFTIRQ_VERBOSE 1
351#else
352# define HARDIRQ_VERBOSE 0
353# define SOFTIRQ_VERBOSE 0
354#endif
355
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +0100356#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700357/*
358 * Quick filtering for interesting events:
359 */
360static int class_filter(struct lock_class *class)
361{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700362#if 0
363 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700364 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700365 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700366 return 1;
367 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700368 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700369 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700370#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800371 /* Filter everything else. 1 would be to allow everything else */
372 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700373}
374#endif
375
376static int verbose(struct lock_class *class)
377{
378#if VERBOSE
379 return class_filter(class);
380#endif
381 return 0;
382}
383
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700384/*
385 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800386 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700387 */
388unsigned long nr_stack_trace_entries;
389static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
390
Dave Jones2c522832013-04-25 13:40:02 -0400391static void print_lockdep_off(const char *bug_msg)
392{
393 printk(KERN_DEBUG "%s\n", bug_msg);
394 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200395#ifdef CONFIG_LOCK_STAT
Dave Jones2c522832013-04-25 13:40:02 -0400396 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200397#endif
Dave Jones2c522832013-04-25 13:40:02 -0400398}
399
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700400static int save_trace(struct stack_trace *trace)
401{
402 trace->nr_entries = 0;
403 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
404 trace->entries = stack_trace + nr_stack_trace_entries;
405
Andi Kleen5a1b3992006-09-26 10:52:34 +0200406 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200407
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700408 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700409
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200410 /*
411 * Some daft arches put -1 at the end to indicate its a full trace.
412 *
413 * <rant> this is buggy anyway, since it takes a whole extra entry so a
414 * complete trace that maxes out the entries provided will be reported
415 * as incomplete, friggin useless </rant>
416 */
Luck, Tonyea5b41f2009-12-09 14:29:36 -0800417 if (trace->nr_entries != 0 &&
418 trace->entries[trace->nr_entries-1] == ULONG_MAX)
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200419 trace->nr_entries--;
420
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700421 trace->max_entries = trace->nr_entries;
422
423 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700424
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200425 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800426 if (!debug_locks_off_graph_unlock())
427 return 0;
428
Dave Jones2c522832013-04-25 13:40:02 -0400429 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
Ingo Molnar74c383f2006-12-13 00:34:43 -0800430 dump_stack();
431
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700432 return 0;
433 }
434
435 return 1;
436}
437
438unsigned int nr_hardirq_chains;
439unsigned int nr_softirq_chains;
440unsigned int nr_process_chains;
441unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700442
443#ifdef CONFIG_DEBUG_LOCKDEP
444/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700445 * Various lockdep statistics:
446 */
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200447DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700448#endif
449
450/*
451 * Locking printouts:
452 */
453
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100454#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100455 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
456 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
457 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
458 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100459
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700460static const char *usage_str[] =
461{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100462#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
463#include "lockdep_states.h"
464#undef LOCKDEP_STATE
465 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700466};
467
468const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
469{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700470 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700471}
472
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100473static inline unsigned long lock_flag(enum lock_usage_bit bit)
474{
475 return 1UL << bit;
476}
477
478static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
479{
480 char c = '.';
481
482 if (class->usage_mask & lock_flag(bit + 2))
483 c = '+';
484 if (class->usage_mask & lock_flag(bit)) {
485 c = '-';
486 if (class->usage_mask & lock_flag(bit + 2))
487 c = '?';
488 }
489
490 return c;
491}
492
Peter Zijlstraf510b232009-01-22 17:53:47 +0100493void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700494{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100495 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700496
Peter Zijlstraf510b232009-01-22 17:53:47 +0100497#define LOCKDEP_STATE(__STATE) \
498 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
499 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
500#include "lockdep_states.h"
501#undef LOCKDEP_STATE
502
503 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700504}
505
Steven Rostedte5e78d02011-11-02 20:24:16 -0400506static void __print_lock_name(struct lock_class *class)
Steven Rostedt3003eba2011-04-20 21:41:54 -0400507{
508 char str[KSYM_NAME_LEN];
509 const char *name;
510
511 name = class->name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700512 if (!name) {
513 name = __get_key_name(class->key, str);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100514 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700515 } else {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100516 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700517 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100518 printk(KERN_CONT "#%d", class->name_version);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700519 if (class->subclass)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100520 printk(KERN_CONT "/%d", class->subclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700521 }
Steven Rostedte5e78d02011-11-02 20:24:16 -0400522}
523
524static void print_lock_name(struct lock_class *class)
525{
526 char usage[LOCK_USAGE_CHARS];
527
528 get_usage_chars(class, usage);
529
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100530 printk(KERN_CONT " (");
Steven Rostedte5e78d02011-11-02 20:24:16 -0400531 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100532 printk(KERN_CONT "){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700533}
534
535static void print_lockdep_cache(struct lockdep_map *lock)
536{
537 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700538 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700539
540 name = lock->name;
541 if (!name)
542 name = __get_key_name(lock->key->subkeys, str);
543
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100544 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700545}
546
547static void print_lock(struct held_lock *hlock)
548{
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200549 /*
550 * We can be called locklessly through debug_show_all_locks() so be
551 * extra careful, the hlock might have been released and cleared.
552 */
553 unsigned int class_idx = hlock->class_idx;
554
555 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
556 barrier();
557
558 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100559 printk(KERN_CONT "<RELEASED>\n");
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200560 return;
561 }
562
563 print_lock_name(lock_classes + class_idx - 1);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100564 printk(KERN_CONT ", at: [<%p>] %pS\n",
565 (void *)hlock->acquire_ip, (void *)hlock->acquire_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700566}
567
568static void lockdep_print_held_locks(struct task_struct *curr)
569{
570 int i, depth = curr->lockdep_depth;
571
572 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700573 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700574 return;
575 }
576 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700577 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700578
579 for (i = 0; i < depth; i++) {
580 printk(" #%d: ", i);
581 print_lock(curr->held_locks + i);
582 }
583}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700584
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100585static void print_kernel_ident(void)
Dave Jones99de0552006-09-29 02:00:10 -0700586{
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100587 printk("%s %.*s %s\n", init_utsname()->release,
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700588 (int)strcspn(init_utsname()->version, " "),
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100589 init_utsname()->version,
590 print_tainted());
Dave Jones99de0552006-09-29 02:00:10 -0700591}
592
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700593static int very_verbose(struct lock_class *class)
594{
595#if VERY_VERBOSE
596 return class_filter(class);
597#endif
598 return 0;
599}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700600
601/*
602 * Is this the address of a static object:
603 */
Sasha Levin8dce7a92013-06-13 18:41:16 -0400604#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700605static int static_obj(void *obj)
606{
607 unsigned long start = (unsigned long) &_stext,
608 end = (unsigned long) &_end,
609 addr = (unsigned long) obj;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700610
611 /*
612 * static variable?
613 */
614 if ((addr >= start) && (addr < end))
615 return 1;
616
Mike Frysinger2a9ad182009-09-22 16:44:16 -0700617 if (arch_is_kernel_data(addr))
618 return 1;
619
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700620 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900621 * in-kernel percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700622 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900623 if (is_kernel_percpu_address(addr))
624 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700625
626 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900627 * module static or percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700628 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900629 return is_module_address(addr) || is_module_percpu_address(addr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700630}
Sasha Levin8dce7a92013-06-13 18:41:16 -0400631#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700632
633/*
634 * To make lock name printouts unique, we calculate a unique
635 * class->name_version generation counter:
636 */
637static int count_matching_names(struct lock_class *new_class)
638{
639 struct lock_class *class;
640 int count = 0;
641
642 if (!new_class->name)
643 return 0;
644
Peter Zijlstra35a93932015-02-26 16:23:11 +0100645 list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700646 if (new_class->key - new_class->subclass == class->key)
647 return class->name_version;
648 if (class->name && !strcmp(class->name, new_class->name))
649 count = max(count, class->name_version);
650 }
651
652 return count + 1;
653}
654
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700655/*
656 * Register a lock's class in the hash-table, if the class is not present
657 * yet. Otherwise we look it up. We cache the result in the lock object
658 * itself, so actual lookup of the hash should be once per lock object.
659 */
660static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700661look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700662{
663 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -0800664 struct hlist_head *hash_head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700665 struct lock_class *class;
Thomas Gleixner383776f2017-02-27 15:37:36 +0100666 bool is_static = false;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700667
Hitoshi Mitake4ba053c2010-10-13 17:30:26 +0900668 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
669 debug_locks_off();
670 printk(KERN_ERR
671 "BUG: looking up invalid subclass: %u\n", subclass);
672 printk(KERN_ERR
673 "turning off the locking correctness validator.\n");
674 dump_stack();
675 return NULL;
676 }
677
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700678 /*
679 * Static locks do not have their class-keys yet - for them the key
Thomas Gleixner383776f2017-02-27 15:37:36 +0100680 * is the lock object itself. If the lock is in the per cpu area,
681 * the canonical address of the lock (per cpu offset removed) is
682 * used.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700683 */
Thomas Gleixner383776f2017-02-27 15:37:36 +0100684 if (unlikely(!lock->key)) {
685 unsigned long can_addr, addr = (unsigned long)lock;
686
687 if (__is_kernel_percpu_address(addr, &can_addr))
688 lock->key = (void *)can_addr;
689 else if (__is_module_percpu_address(addr, &can_addr))
690 lock->key = (void *)can_addr;
691 else if (static_obj(lock))
692 lock->key = (void *)lock;
693 else
694 return ERR_PTR(-EINVAL);
695 is_static = true;
696 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700697
698 /*
699 * NOTE: the class-key must be unique. For dynamic locks, a static
700 * lock_class_key variable is passed in through the mutex_init()
701 * (or spin_lock_init()) call - which acts as the key. For static
702 * locks we use the lock object itself as the key.
703 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700704 BUILD_BUG_ON(sizeof(struct lock_class_key) >
705 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700706
707 key = lock->key->subkeys + subclass;
708
709 hash_head = classhashentry(key);
710
711 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +0100712 * We do an RCU walk of the hash, see lockdep_free_key_range().
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700713 */
Peter Zijlstra35a93932015-02-26 16:23:11 +0100714 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
715 return NULL;
716
Andrew Mortona63f38c2016-02-03 13:44:12 -0800717 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700718 if (class->key == key) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200719 /*
720 * Huh! same key, different name? Did someone trample
721 * on some memory? We're most confused.
722 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700723 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700724 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700725 }
726 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700727
Thomas Gleixner383776f2017-02-27 15:37:36 +0100728 return is_static || static_obj(lock->key) ? NULL : ERR_PTR(-EINVAL);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700729}
730
Byungchul Parkb09be672017-08-07 16:12:52 +0900731#ifdef CONFIG_LOCKDEP_CROSSRELEASE
732static void cross_init(struct lockdep_map *lock, int cross);
733static int cross_lock(struct lockdep_map *lock);
734static int lock_acquire_crosslock(struct held_lock *hlock);
735static int lock_release_crosslock(struct lockdep_map *lock);
736#else
737static inline void cross_init(struct lockdep_map *lock, int cross) {}
738static inline int cross_lock(struct lockdep_map *lock) { return 0; }
739static inline int lock_acquire_crosslock(struct held_lock *hlock) { return 2; }
740static inline int lock_release_crosslock(struct lockdep_map *lock) { return 2; }
741#endif
742
Ingo Molnard6d897c2006-07-10 04:44:04 -0700743/*
744 * Register a lock's class in the hash-table, if the class is not present
745 * yet. Otherwise we look it up. We cache the result in the lock object
746 * itself, so actual lookup of the hash should be once per lock object.
747 */
Denys Vlasenkoc003ed92016-04-08 20:58:46 +0200748static struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400749register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700750{
751 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -0800752 struct hlist_head *hash_head;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700753 struct lock_class *class;
Peter Zijlstra35a93932015-02-26 16:23:11 +0100754
755 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
Ingo Molnard6d897c2006-07-10 04:44:04 -0700756
757 class = look_up_lock_class(lock, subclass);
Thomas Gleixner383776f2017-02-27 15:37:36 +0100758 if (likely(!IS_ERR_OR_NULL(class)))
Yong Zhang87cdee72011-11-09 16:07:14 +0800759 goto out_set_class_cache;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700760
761 /*
762 * Debug-check: all keys must be persistent!
Thomas Gleixner383776f2017-02-27 15:37:36 +0100763 */
764 if (IS_ERR(class)) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700765 debug_locks_off();
766 printk("INFO: trying to register non-static key.\n");
767 printk("the code is fine but needs lockdep annotation.\n");
768 printk("turning off the locking correctness validator.\n");
769 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700770 return NULL;
771 }
772
Ingo Molnard6d897c2006-07-10 04:44:04 -0700773 key = lock->key->subkeys + subclass;
774 hash_head = classhashentry(key);
775
Ingo Molnar74c383f2006-12-13 00:34:43 -0800776 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800777 return NULL;
778 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700779 /*
780 * We have to do the hash-walk again, to avoid races
781 * with another CPU:
782 */
Andrew Mortona63f38c2016-02-03 13:44:12 -0800783 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700784 if (class->key == key)
785 goto out_unlock_set;
Peter Zijlstra35a93932015-02-26 16:23:11 +0100786 }
787
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700788 /*
789 * Allocate a new key from the static array, and add it to
790 * the hash:
791 */
792 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800793 if (!debug_locks_off_graph_unlock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800794 return NULL;
795 }
Ingo Molnar74c383f2006-12-13 00:34:43 -0800796
Dave Jones2c522832013-04-25 13:40:02 -0400797 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100798 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700799 return NULL;
800 }
801 class = lock_classes + nr_lock_classes++;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200802 debug_atomic_inc(nr_unused_locks);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700803 class->key = key;
804 class->name = lock->name;
805 class->subclass = subclass;
806 INIT_LIST_HEAD(&class->lock_entry);
807 INIT_LIST_HEAD(&class->locks_before);
808 INIT_LIST_HEAD(&class->locks_after);
809 class->name_version = count_matching_names(class);
810 /*
811 * We use RCU's safe list-add method to make
812 * parallel walking of the hash-list safe:
813 */
Andrew Mortona63f38c2016-02-03 13:44:12 -0800814 hlist_add_head_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100815 /*
816 * Add it to the global list of classes:
817 */
818 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700819
820 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800821 graph_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800822
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700823 printk("\nnew class %p: %s", class->key, class->name);
824 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100825 printk(KERN_CONT "#%d", class->name_version);
826 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700827 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800828
Ingo Molnar74c383f2006-12-13 00:34:43 -0800829 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800830 return NULL;
831 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700832 }
833out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800834 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700835
Yong Zhang87cdee72011-11-09 16:07:14 +0800836out_set_class_cache:
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400837 if (!subclass || force)
Hitoshi Mitake62016252010-10-05 18:01:51 +0900838 lock->class_cache[0] = class;
839 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
840 lock->class_cache[subclass] = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700841
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200842 /*
843 * Hash collision, did we smoke some? We found a class with a matching
844 * hash but the subclass -- which is hashed in -- didn't match.
845 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800846 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
847 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700848
849 return class;
850}
851
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700852#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700853/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700854 * Allocate a lockdep entry. (assumes the graph_lock held, returns
855 * with NULL on failure)
856 */
857static struct lock_list *alloc_list_entry(void)
858{
859 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
860 if (!debug_locks_off_graph_unlock())
861 return NULL;
862
Dave Jones2c522832013-04-25 13:40:02 -0400863 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100864 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700865 return NULL;
866 }
867 return list_entries + nr_list_entries++;
868}
869
870/*
871 * Add a new dependency to the head of the list:
872 */
Tahsin Erdogan83f06162016-11-08 00:02:07 -0800873static int add_lock_to_list(struct lock_class *this, struct list_head *head,
874 unsigned long ip, int distance,
875 struct stack_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -0700876{
877 struct lock_list *entry;
878 /*
879 * Lock not present yet - get a new dependency struct and
880 * add it to the list:
881 */
882 entry = alloc_list_entry();
883 if (!entry)
884 return 0;
885
Zhu Yi74870172008-08-27 14:33:00 +0800886 entry->class = this;
887 entry->distance = distance;
Yong Zhang4726f2a2010-05-04 14:16:48 +0800888 entry->trace = *trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700889 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +0100890 * Both allocation and removal are done under the graph lock; but
891 * iteration is under RCU-sched; see look_up_lock_class() and
892 * lockdep_free_key_range().
Peter Zijlstra8e182572007-07-19 01:48:54 -0700893 */
894 list_add_tail_rcu(&entry->entry, head);
895
896 return 1;
897}
898
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200899/*
900 * For good efficiency of modular, we use power of 2
901 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200902#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
903#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
904
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200905/*
906 * The circular_queue and helpers is used to implement the
Peter Zijlstraaf012962009-07-16 15:44:29 +0200907 * breadth-first search(BFS)algorithem, by which we can build
908 * the shortest path from the next lock to be acquired to the
909 * previous held lock if there is a circular between them.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200910 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200911struct circular_queue {
912 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
913 unsigned int front, rear;
914};
915
916static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200917
Ming Lei12f3dfd2009-07-16 15:44:29 +0200918unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200919
Ming Leie351b662009-07-22 22:48:09 +0800920static unsigned int lockdep_dependency_gen_id;
921
Peter Zijlstraaf012962009-07-16 15:44:29 +0200922static inline void __cq_init(struct circular_queue *cq)
923{
924 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +0800925 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200926}
927
928static inline int __cq_empty(struct circular_queue *cq)
929{
930 return (cq->front == cq->rear);
931}
932
933static inline int __cq_full(struct circular_queue *cq)
934{
935 return ((cq->rear + 1) & CQ_MASK) == cq->front;
936}
937
938static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
939{
940 if (__cq_full(cq))
941 return -1;
942
943 cq->element[cq->rear] = elem;
944 cq->rear = (cq->rear + 1) & CQ_MASK;
945 return 0;
946}
947
948static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
949{
950 if (__cq_empty(cq))
951 return -1;
952
953 *elem = cq->element[cq->front];
954 cq->front = (cq->front + 1) & CQ_MASK;
955 return 0;
956}
957
958static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
959{
960 return (cq->rear - cq->front) & CQ_MASK;
961}
962
963static inline void mark_lock_accessed(struct lock_list *lock,
964 struct lock_list *parent)
965{
966 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200967
Peter Zijlstraaf012962009-07-16 15:44:29 +0200968 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200969 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200970 lock->parent = parent;
Ming Leie351b662009-07-22 22:48:09 +0800971 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200972}
973
974static inline unsigned long lock_accessed(struct lock_list *lock)
975{
976 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200977
Peter Zijlstraaf012962009-07-16 15:44:29 +0200978 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200979 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Ming Leie351b662009-07-22 22:48:09 +0800980 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200981}
982
983static inline struct lock_list *get_lock_parent(struct lock_list *child)
984{
985 return child->parent;
986}
987
988static inline int get_lock_depth(struct lock_list *child)
989{
990 int depth = 0;
991 struct lock_list *parent;
992
993 while ((parent = get_lock_parent(child))) {
994 child = parent;
995 depth++;
996 }
997 return depth;
998}
999
Ming Lei9e2d5512009-07-16 15:44:29 +02001000static int __bfs(struct lock_list *source_entry,
Peter Zijlstraaf012962009-07-16 15:44:29 +02001001 void *data,
1002 int (*match)(struct lock_list *entry, void *data),
1003 struct lock_list **target_entry,
1004 int forward)
Ming Leic94aa5c2009-07-16 15:44:29 +02001005{
1006 struct lock_list *entry;
Ming Leid588e462009-07-16 15:44:29 +02001007 struct list_head *head;
Ming Leic94aa5c2009-07-16 15:44:29 +02001008 struct circular_queue *cq = &lock_cq;
1009 int ret = 1;
1010
Ming Lei9e2d5512009-07-16 15:44:29 +02001011 if (match(source_entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001012 *target_entry = source_entry;
1013 ret = 0;
1014 goto exit;
1015 }
1016
Ming Leid588e462009-07-16 15:44:29 +02001017 if (forward)
1018 head = &source_entry->class->locks_after;
1019 else
1020 head = &source_entry->class->locks_before;
1021
1022 if (list_empty(head))
1023 goto exit;
1024
1025 __cq_init(cq);
Ming Leic94aa5c2009-07-16 15:44:29 +02001026 __cq_enqueue(cq, (unsigned long)source_entry);
1027
1028 while (!__cq_empty(cq)) {
1029 struct lock_list *lock;
Ming Leic94aa5c2009-07-16 15:44:29 +02001030
1031 __cq_dequeue(cq, (unsigned long *)&lock);
1032
1033 if (!lock->class) {
1034 ret = -2;
1035 goto exit;
1036 }
1037
1038 if (forward)
1039 head = &lock->class->locks_after;
1040 else
1041 head = &lock->class->locks_before;
1042
Peter Zijlstra35a93932015-02-26 16:23:11 +01001043 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1044
1045 list_for_each_entry_rcu(entry, head, entry) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001046 if (!lock_accessed(entry)) {
Ming Lei12f3dfd2009-07-16 15:44:29 +02001047 unsigned int cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001048 mark_lock_accessed(entry, lock);
Ming Lei9e2d5512009-07-16 15:44:29 +02001049 if (match(entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001050 *target_entry = entry;
1051 ret = 0;
1052 goto exit;
1053 }
1054
1055 if (__cq_enqueue(cq, (unsigned long)entry)) {
1056 ret = -1;
1057 goto exit;
1058 }
Ming Lei12f3dfd2009-07-16 15:44:29 +02001059 cq_depth = __cq_get_elem_count(cq);
1060 if (max_bfs_queue_depth < cq_depth)
1061 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001062 }
1063 }
1064 }
1065exit:
1066 return ret;
1067}
1068
Ming Leid7aaba12009-07-16 15:44:29 +02001069static inline int __bfs_forwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001070 void *data,
1071 int (*match)(struct lock_list *entry, void *data),
1072 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001073{
Ming Lei9e2d5512009-07-16 15:44:29 +02001074 return __bfs(src_entry, data, match, target_entry, 1);
Ming Leic94aa5c2009-07-16 15:44:29 +02001075
1076}
1077
Ming Leid7aaba12009-07-16 15:44:29 +02001078static inline int __bfs_backwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001079 void *data,
1080 int (*match)(struct lock_list *entry, void *data),
1081 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001082{
Ming Lei9e2d5512009-07-16 15:44:29 +02001083 return __bfs(src_entry, data, match, target_entry, 0);
Ming Leic94aa5c2009-07-16 15:44:29 +02001084
1085}
1086
Peter Zijlstra8e182572007-07-19 01:48:54 -07001087/*
1088 * Recursive, forwards-direction lock-dependency checking, used for
1089 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1090 * checking.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001091 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001092
1093/*
1094 * Print a dependency chain entry (this is only done when a deadlock
1095 * has been detected):
1096 */
1097static noinline int
Ming Lei24208ca2009-07-16 15:44:29 +02001098print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001099{
1100 if (debug_locks_silent)
1101 return 0;
1102 printk("\n-> #%u", depth);
1103 print_lock_name(target->class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001104 printk(KERN_CONT ":\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001105 print_stack_trace(&target->trace, 6);
1106
1107 return 0;
1108}
1109
Steven Rostedtf4185812011-04-20 21:41:55 -04001110static void
1111print_circular_lock_scenario(struct held_lock *src,
1112 struct held_lock *tgt,
1113 struct lock_list *prt)
1114{
1115 struct lock_class *source = hlock_class(src);
1116 struct lock_class *target = hlock_class(tgt);
1117 struct lock_class *parent = prt->class;
1118
1119 /*
1120 * A direct locking problem where unsafe_class lock is taken
1121 * directly by safe_class lock, then all we need to show
1122 * is the deadlock scenario, as it is obvious that the
1123 * unsafe lock is taken under the safe lock.
1124 *
1125 * But if there is a chain instead, where the safe lock takes
1126 * an intermediate lock (middle_class) where this lock is
1127 * not the same as the safe lock, then the lock chain is
1128 * used to describe the problem. Otherwise we would need
1129 * to show a different CPU case for each link in the chain
1130 * from the safe_class lock to the unsafe_class lock.
1131 */
1132 if (parent != source) {
1133 printk("Chain exists of:\n ");
1134 __print_lock_name(source);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001135 printk(KERN_CONT " --> ");
Steven Rostedtf4185812011-04-20 21:41:55 -04001136 __print_lock_name(parent);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001137 printk(KERN_CONT " --> ");
Steven Rostedtf4185812011-04-20 21:41:55 -04001138 __print_lock_name(target);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001139 printk(KERN_CONT "\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001140 }
1141
Byungchul Park383a4bc2017-08-07 16:12:55 +09001142 if (cross_lock(tgt->instance)) {
1143 printk(" Possible unsafe locking scenario by crosslock:\n\n");
1144 printk(" CPU0 CPU1\n");
1145 printk(" ---- ----\n");
1146 printk(" lock(");
1147 __print_lock_name(parent);
1148 printk(KERN_CONT ");\n");
1149 printk(" lock(");
1150 __print_lock_name(target);
1151 printk(KERN_CONT ");\n");
1152 printk(" lock(");
1153 __print_lock_name(source);
1154 printk(KERN_CONT ");\n");
1155 printk(" unlock(");
1156 __print_lock_name(target);
1157 printk(KERN_CONT ");\n");
1158 printk("\n *** DEADLOCK ***\n\n");
1159 } else {
1160 printk(" Possible unsafe locking scenario:\n\n");
1161 printk(" CPU0 CPU1\n");
1162 printk(" ---- ----\n");
1163 printk(" lock(");
1164 __print_lock_name(target);
1165 printk(KERN_CONT ");\n");
1166 printk(" lock(");
1167 __print_lock_name(parent);
1168 printk(KERN_CONT ");\n");
1169 printk(" lock(");
1170 __print_lock_name(target);
1171 printk(KERN_CONT ");\n");
1172 printk(" lock(");
1173 __print_lock_name(source);
1174 printk(KERN_CONT ");\n");
1175 printk("\n *** DEADLOCK ***\n\n");
1176 }
Steven Rostedtf4185812011-04-20 21:41:55 -04001177}
1178
Peter Zijlstra8e182572007-07-19 01:48:54 -07001179/*
1180 * When a circular dependency is detected, print the
1181 * header first:
1182 */
1183static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001184print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1185 struct held_lock *check_src,
1186 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001187{
1188 struct task_struct *curr = current;
1189
Ming Leic94aa5c2009-07-16 15:44:29 +02001190 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001191 return 0;
1192
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001193 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001194 pr_warn("======================================================\n");
1195 pr_warn("WARNING: possible circular locking dependency detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001196 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001197 pr_warn("------------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001198 pr_warn("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001199 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001200 print_lock(check_src);
Byungchul Park383a4bc2017-08-07 16:12:55 +09001201
1202 if (cross_lock(check_tgt->instance))
1203 pr_warn("\nbut now in release context of a crosslock acquired at the following:\n");
1204 else
1205 pr_warn("\nbut task is already holding lock:\n");
1206
Ming Leidb0002a2009-07-16 15:44:29 +02001207 print_lock(check_tgt);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001208 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1209 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001210
1211 print_circular_bug_entry(entry, depth);
1212
1213 return 0;
1214}
1215
Ming Lei9e2d5512009-07-16 15:44:29 +02001216static inline int class_equal(struct lock_list *entry, void *data)
1217{
1218 return entry->class == data;
1219}
1220
Ming Leidb0002a2009-07-16 15:44:29 +02001221static noinline int print_circular_bug(struct lock_list *this,
1222 struct lock_list *target,
1223 struct held_lock *check_src,
Byungchul Park383a4bc2017-08-07 16:12:55 +09001224 struct held_lock *check_tgt,
1225 struct stack_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001226{
1227 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001228 struct lock_list *parent;
Steven Rostedtf4185812011-04-20 21:41:55 -04001229 struct lock_list *first_parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001230 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001231
Ming Leic94aa5c2009-07-16 15:44:29 +02001232 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001233 return 0;
1234
Byungchul Park383a4bc2017-08-07 16:12:55 +09001235 if (cross_lock(check_tgt->instance))
1236 this->trace = *trace;
1237 else if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001238 return 0;
1239
Ming Leic94aa5c2009-07-16 15:44:29 +02001240 depth = get_lock_depth(target);
1241
Ming Leidb0002a2009-07-16 15:44:29 +02001242 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001243
1244 parent = get_lock_parent(target);
Steven Rostedtf4185812011-04-20 21:41:55 -04001245 first_parent = parent;
Ming Leic94aa5c2009-07-16 15:44:29 +02001246
1247 while (parent) {
1248 print_circular_bug_entry(parent, --depth);
1249 parent = get_lock_parent(parent);
1250 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001251
1252 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001253 print_circular_lock_scenario(check_src, check_tgt,
1254 first_parent);
1255
Peter Zijlstra8e182572007-07-19 01:48:54 -07001256 lockdep_print_held_locks(curr);
1257
1258 printk("\nstack backtrace:\n");
1259 dump_stack();
1260
1261 return 0;
1262}
1263
Ming Leidb0002a2009-07-16 15:44:29 +02001264static noinline int print_bfs_bug(int ret)
1265{
1266 if (!debug_locks_off_graph_unlock())
1267 return 0;
1268
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001269 /*
1270 * Breadth-first-search failed, graph got corrupted?
1271 */
Ming Leidb0002a2009-07-16 15:44:29 +02001272 WARN(1, "lockdep bfs error:%d\n", ret);
1273
1274 return 0;
1275}
1276
Ming Leief681022009-07-16 15:44:29 +02001277static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001278{
Ming Leief681022009-07-16 15:44:29 +02001279 (*(unsigned long *)data)++;
1280 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001281}
1282
Fengguang Wu5216d532013-11-09 00:55:35 +08001283static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
Ming Leief681022009-07-16 15:44:29 +02001284{
1285 unsigned long count = 0;
1286 struct lock_list *uninitialized_var(target_entry);
1287
1288 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1289
1290 return count;
1291}
David Miller419ca3f2008-07-29 21:45:03 -07001292unsigned long lockdep_count_forward_deps(struct lock_class *class)
1293{
1294 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001295 struct lock_list this;
1296
1297 this.parent = NULL;
1298 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001299
1300 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001301 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001302 ret = __lockdep_count_forward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001303 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001304 local_irq_restore(flags);
1305
1306 return ret;
1307}
1308
Fengguang Wu5216d532013-11-09 00:55:35 +08001309static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001310{
Ming Leief681022009-07-16 15:44:29 +02001311 unsigned long count = 0;
1312 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001313
Ming Leief681022009-07-16 15:44:29 +02001314 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001315
Ming Leief681022009-07-16 15:44:29 +02001316 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001317}
1318
1319unsigned long lockdep_count_backward_deps(struct lock_class *class)
1320{
1321 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001322 struct lock_list this;
1323
1324 this.parent = NULL;
1325 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001326
1327 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001328 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001329 ret = __lockdep_count_backward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001330 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001331 local_irq_restore(flags);
1332
1333 return ret;
1334}
1335
Peter Zijlstra8e182572007-07-19 01:48:54 -07001336/*
1337 * Prove that the dependency graph starting at <entry> can not
1338 * lead to <target>. Print an error and return 0 if it does.
1339 */
1340static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001341check_noncircular(struct lock_list *root, struct lock_class *target,
1342 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001343{
Ming Leidb0002a2009-07-16 15:44:29 +02001344 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001345
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001346 debug_atomic_inc(nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001347
Ming Leid7aaba12009-07-16 15:44:29 +02001348 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001349
1350 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001351}
1352
Peter Zijlstraae813302017-03-03 10:13:38 +01001353static noinline int
1354check_redundant(struct lock_list *root, struct lock_class *target,
1355 struct lock_list **target_entry)
1356{
1357 int result;
1358
1359 debug_atomic_inc(nr_redundant_checks);
1360
1361 result = __bfs_forwards(root, target, class_equal, target_entry);
1362
1363 return result;
1364}
1365
Steven Rostedt81d68a92008-05-12 21:20:42 +02001366#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001367/*
1368 * Forwards and backwards subgraph searching, for the purposes of
1369 * proving that two subgraphs can be connected by a new dependency
1370 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1371 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001372
Ming Leid7aaba12009-07-16 15:44:29 +02001373static inline int usage_match(struct lock_list *entry, void *bit)
1374{
1375 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1376}
1377
1378
1379
Peter Zijlstra8e182572007-07-19 01:48:54 -07001380/*
1381 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001382 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001383 *
Ming Leid7aaba12009-07-16 15:44:29 +02001384 * Return 0 if such a node exists in the subgraph, and put that node
1385 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001386 *
Ming Leid7aaba12009-07-16 15:44:29 +02001387 * Return 1 otherwise and keep *@target_entry unchanged.
1388 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001389 */
Ming Leid7aaba12009-07-16 15:44:29 +02001390static int
1391find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1392 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001393{
Ming Leid7aaba12009-07-16 15:44:29 +02001394 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001395
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001396 debug_atomic_inc(nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001397
Ming Leid7aaba12009-07-16 15:44:29 +02001398 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1399
1400 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001401}
1402
1403/*
1404 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001405 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001406 *
Ming Leid7aaba12009-07-16 15:44:29 +02001407 * Return 0 if such a node exists in the subgraph, and put that node
1408 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001409 *
Ming Leid7aaba12009-07-16 15:44:29 +02001410 * Return 1 otherwise and keep *@target_entry unchanged.
1411 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001412 */
Ming Leid7aaba12009-07-16 15:44:29 +02001413static int
1414find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1415 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001416{
Ming Leid7aaba12009-07-16 15:44:29 +02001417 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001418
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001419 debug_atomic_inc(nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001420
Ming Leid7aaba12009-07-16 15:44:29 +02001421 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001422
Ming Leid7aaba12009-07-16 15:44:29 +02001423 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001424}
1425
Peter Zijlstraaf012962009-07-16 15:44:29 +02001426static void print_lock_class_header(struct lock_class *class, int depth)
1427{
1428 int bit;
1429
1430 printk("%*s->", depth, "");
1431 print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001432 printk(KERN_CONT " ops: %lu", class->ops);
1433 printk(KERN_CONT " {\n");
Peter Zijlstraaf012962009-07-16 15:44:29 +02001434
1435 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1436 if (class->usage_mask & (1 << bit)) {
1437 int len = depth;
1438
1439 len += printk("%*s %s", depth, "", usage_str[bit]);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001440 len += printk(KERN_CONT " at:\n");
Peter Zijlstraaf012962009-07-16 15:44:29 +02001441 print_stack_trace(class->usage_traces + bit, len);
1442 }
1443 }
1444 printk("%*s }\n", depth, "");
1445
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001446 printk("%*s ... key at: [<%p>] %pS\n",
1447 depth, "", class->key, class->key);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001448}
1449
1450/*
1451 * printk the shortest lock dependencies from @start to @end in reverse order:
1452 */
1453static void __used
1454print_shortest_lock_dependencies(struct lock_list *leaf,
1455 struct lock_list *root)
1456{
1457 struct lock_list *entry = leaf;
1458 int depth;
1459
1460 /*compute depth from generated tree by BFS*/
1461 depth = get_lock_depth(leaf);
1462
1463 do {
1464 print_lock_class_header(entry->class, depth);
1465 printk("%*s ... acquired at:\n", depth, "");
1466 print_stack_trace(&entry->trace, 2);
1467 printk("\n");
1468
1469 if (depth == 0 && (entry != root)) {
Steven Rostedt6be8c392011-04-20 21:41:58 -04001470 printk("lockdep:%s bad path found in chain graph\n", __func__);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001471 break;
1472 }
1473
1474 entry = get_lock_parent(entry);
1475 depth--;
1476 } while (entry && (depth >= 0));
1477
1478 return;
1479}
Ming Leid7aaba12009-07-16 15:44:29 +02001480
Steven Rostedt3003eba2011-04-20 21:41:54 -04001481static void
1482print_irq_lock_scenario(struct lock_list *safe_entry,
1483 struct lock_list *unsafe_entry,
Steven Rostedtdad3d742011-04-20 21:41:57 -04001484 struct lock_class *prev_class,
1485 struct lock_class *next_class)
Steven Rostedt3003eba2011-04-20 21:41:54 -04001486{
1487 struct lock_class *safe_class = safe_entry->class;
1488 struct lock_class *unsafe_class = unsafe_entry->class;
Steven Rostedtdad3d742011-04-20 21:41:57 -04001489 struct lock_class *middle_class = prev_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001490
1491 if (middle_class == safe_class)
Steven Rostedtdad3d742011-04-20 21:41:57 -04001492 middle_class = next_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001493
1494 /*
1495 * A direct locking problem where unsafe_class lock is taken
1496 * directly by safe_class lock, then all we need to show
1497 * is the deadlock scenario, as it is obvious that the
1498 * unsafe lock is taken under the safe lock.
1499 *
1500 * But if there is a chain instead, where the safe lock takes
1501 * an intermediate lock (middle_class) where this lock is
1502 * not the same as the safe lock, then the lock chain is
1503 * used to describe the problem. Otherwise we would need
1504 * to show a different CPU case for each link in the chain
1505 * from the safe_class lock to the unsafe_class lock.
1506 */
1507 if (middle_class != unsafe_class) {
1508 printk("Chain exists of:\n ");
1509 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001510 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001511 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001512 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001513 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001514 printk(KERN_CONT "\n\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001515 }
1516
1517 printk(" Possible interrupt unsafe locking scenario:\n\n");
1518 printk(" CPU0 CPU1\n");
1519 printk(" ---- ----\n");
1520 printk(" lock(");
1521 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001522 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001523 printk(" local_irq_disable();\n");
1524 printk(" lock(");
1525 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001526 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001527 printk(" lock(");
1528 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001529 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001530 printk(" <Interrupt>\n");
1531 printk(" lock(");
1532 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001533 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001534 printk("\n *** DEADLOCK ***\n\n");
1535}
1536
Peter Zijlstra8e182572007-07-19 01:48:54 -07001537static int
1538print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001539 struct lock_list *prev_root,
1540 struct lock_list *next_root,
1541 struct lock_list *backwards_entry,
1542 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001543 struct held_lock *prev,
1544 struct held_lock *next,
1545 enum lock_usage_bit bit1,
1546 enum lock_usage_bit bit2,
1547 const char *irqclass)
1548{
1549 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1550 return 0;
1551
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001552 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001553 pr_warn("=====================================================\n");
1554 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07001555 irqclass, irqclass);
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001556 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001557 pr_warn("-----------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001558 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001559 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001560 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1561 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1562 curr->hardirqs_enabled,
1563 curr->softirqs_enabled);
1564 print_lock(next);
1565
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001566 pr_warn("\nand this task is already holding:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001567 print_lock(prev);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001568 pr_warn("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001569 print_lock_name(hlock_class(prev));
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001570 pr_cont(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001571 print_lock_name(hlock_class(next));
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001572 pr_cont("\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001573
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001574 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07001575 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001576 print_lock_name(backwards_entry->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001577 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001578
Ming Lei24208ca2009-07-16 15:44:29 +02001579 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001580
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001581 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001582 print_lock_name(forwards_entry->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001583 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
1584 pr_warn("...");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001585
Ming Lei24208ca2009-07-16 15:44:29 +02001586 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001587
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001588 pr_warn("\nother info that might help us debug this:\n\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04001589 print_irq_lock_scenario(backwards_entry, forwards_entry,
1590 hlock_class(prev), hlock_class(next));
Steven Rostedt3003eba2011-04-20 21:41:54 -04001591
Peter Zijlstra8e182572007-07-19 01:48:54 -07001592 lockdep_print_held_locks(curr);
1593
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001594 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001595 if (!save_trace(&prev_root->trace))
1596 return 0;
1597 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001598
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001599 pr_warn("\nthe dependencies between the lock to be acquired");
1600 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001601 if (!save_trace(&next_root->trace))
1602 return 0;
1603 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001604
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001605 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001606 dump_stack();
1607
1608 return 0;
1609}
1610
1611static int
1612check_usage(struct task_struct *curr, struct held_lock *prev,
1613 struct held_lock *next, enum lock_usage_bit bit_backwards,
1614 enum lock_usage_bit bit_forwards, const char *irqclass)
1615{
1616 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001617 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001618 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001619 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001620
Ming Leid7aaba12009-07-16 15:44:29 +02001621 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001622
Ming Leid7aaba12009-07-16 15:44:29 +02001623 this.class = hlock_class(prev);
1624 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001625 if (ret < 0)
1626 return print_bfs_bug(ret);
1627 if (ret == 1)
1628 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001629
Ming Lei24208ca2009-07-16 15:44:29 +02001630 that.parent = NULL;
1631 that.class = hlock_class(next);
1632 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001633 if (ret < 0)
1634 return print_bfs_bug(ret);
1635 if (ret == 1)
1636 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001637
Ming Lei24208ca2009-07-16 15:44:29 +02001638 return print_bad_irq_dependency(curr, &this, &that,
1639 target_entry, target_entry1,
1640 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001641 bit_backwards, bit_forwards, irqclass);
1642}
1643
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001644static const char *state_names[] = {
1645#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001646 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001647#include "lockdep_states.h"
1648#undef LOCKDEP_STATE
1649};
1650
1651static const char *state_rnames[] = {
1652#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001653 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001654#include "lockdep_states.h"
1655#undef LOCKDEP_STATE
1656};
1657
1658static inline const char *state_name(enum lock_usage_bit bit)
1659{
1660 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1661}
1662
1663static int exclusive_bit(int new_bit)
1664{
1665 /*
1666 * USED_IN
1667 * USED_IN_READ
1668 * ENABLED
1669 * ENABLED_READ
1670 *
1671 * bit 0 - write/read
1672 * bit 1 - used_in/enabled
1673 * bit 2+ state
1674 */
1675
1676 int state = new_bit & ~3;
1677 int dir = new_bit & 2;
1678
1679 /*
1680 * keep state, bit flip the direction and strip read.
1681 */
1682 return state | (dir ^ 2);
1683}
1684
1685static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1686 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001687{
1688 /*
1689 * Prove that the new dependency does not connect a hardirq-safe
1690 * lock with a hardirq-unsafe lock - to achieve this we search
1691 * the backwards-subgraph starting at <prev>, and the
1692 * forwards-subgraph starting at <next>:
1693 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001694 if (!check_usage(curr, prev, next, bit,
1695 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001696 return 0;
1697
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001698 bit++; /* _READ */
1699
Peter Zijlstra8e182572007-07-19 01:48:54 -07001700 /*
1701 * Prove that the new dependency does not connect a hardirq-safe-read
1702 * lock with a hardirq-unsafe lock - to achieve this we search
1703 * the backwards-subgraph starting at <prev>, and the
1704 * forwards-subgraph starting at <next>:
1705 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001706 if (!check_usage(curr, prev, next, bit,
1707 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001708 return 0;
1709
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001710 return 1;
1711}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001712
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001713static int
1714check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1715 struct held_lock *next)
1716{
1717#define LOCKDEP_STATE(__STATE) \
1718 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001719 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001720#include "lockdep_states.h"
1721#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001722
Peter Zijlstra8e182572007-07-19 01:48:54 -07001723 return 1;
1724}
1725
1726static void inc_chains(void)
1727{
1728 if (current->hardirq_context)
1729 nr_hardirq_chains++;
1730 else {
1731 if (current->softirq_context)
1732 nr_softirq_chains++;
1733 else
1734 nr_process_chains++;
1735 }
1736}
1737
1738#else
1739
1740static inline int
1741check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1742 struct held_lock *next)
1743{
1744 return 1;
1745}
1746
1747static inline void inc_chains(void)
1748{
1749 nr_process_chains++;
1750}
1751
1752#endif
1753
Steven Rostedt48702ec2011-04-20 21:41:56 -04001754static void
1755print_deadlock_scenario(struct held_lock *nxt,
1756 struct held_lock *prv)
1757{
1758 struct lock_class *next = hlock_class(nxt);
1759 struct lock_class *prev = hlock_class(prv);
1760
1761 printk(" Possible unsafe locking scenario:\n\n");
1762 printk(" CPU0\n");
1763 printk(" ----\n");
1764 printk(" lock(");
1765 __print_lock_name(prev);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001766 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001767 printk(" lock(");
1768 __print_lock_name(next);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001769 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001770 printk("\n *** DEADLOCK ***\n\n");
1771 printk(" May be due to missing lock nesting notation\n\n");
1772}
1773
Peter Zijlstra8e182572007-07-19 01:48:54 -07001774static int
1775print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1776 struct held_lock *next)
1777{
1778 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1779 return 0;
1780
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001781 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001782 pr_warn("============================================\n");
1783 pr_warn("WARNING: possible recursive locking detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001784 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001785 pr_warn("--------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001786 pr_warn("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001787 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001788 print_lock(next);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001789 pr_warn("\nbut task is already holding lock:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001790 print_lock(prev);
1791
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001792 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001793 print_deadlock_scenario(next, prev);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001794 lockdep_print_held_locks(curr);
1795
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001796 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001797 dump_stack();
1798
1799 return 0;
1800}
1801
1802/*
1803 * Check whether we are holding such a class already.
1804 *
1805 * (Note that this has to be done separately, because the graph cannot
1806 * detect such classes of deadlocks.)
1807 *
1808 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1809 */
1810static int
1811check_deadlock(struct task_struct *curr, struct held_lock *next,
1812 struct lockdep_map *next_instance, int read)
1813{
1814 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001815 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001816 int i;
1817
1818 for (i = 0; i < curr->lockdep_depth; i++) {
1819 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001820
1821 if (prev->instance == next->nest_lock)
1822 nest = prev;
1823
Dave Jonesf82b2172008-08-11 09:30:23 +02001824 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001825 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001826
Peter Zijlstra8e182572007-07-19 01:48:54 -07001827 /*
1828 * Allow read-after-read recursion of the same
1829 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1830 */
1831 if ((read == 2) && prev->read)
1832 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001833
1834 /*
1835 * We're holding the nest_lock, which serializes this lock's
1836 * nesting behaviour.
1837 */
1838 if (nest)
1839 return 2;
1840
Byungchul Parkb09be672017-08-07 16:12:52 +09001841 if (cross_lock(prev->instance))
1842 continue;
1843
Peter Zijlstra8e182572007-07-19 01:48:54 -07001844 return print_deadlock_bug(curr, prev, next);
1845 }
1846 return 1;
1847}
1848
1849/*
1850 * There was a chain-cache miss, and we are about to add a new dependency
1851 * to a previous lock. We recursively validate the following rules:
1852 *
1853 * - would the adding of the <prev> -> <next> dependency create a
1854 * circular dependency in the graph? [== circular deadlock]
1855 *
1856 * - does the new prev->next dependency connect any hardirq-safe lock
1857 * (in the full backwards-subgraph starting at <prev>) with any
1858 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1859 * <next>)? [== illegal lock inversion with hardirq contexts]
1860 *
1861 * - does the new prev->next dependency connect any softirq-safe lock
1862 * (in the full backwards-subgraph starting at <prev>) with any
1863 * softirq-unsafe lock (in the full forwards-subgraph starting at
1864 * <next>)? [== illegal lock inversion with softirq contexts]
1865 *
1866 * any of these scenarios could lead to a deadlock.
1867 *
1868 * Then if all the validations pass, we add the forwards and backwards
1869 * dependency.
1870 */
1871static int
1872check_prev_add(struct task_struct *curr, struct held_lock *prev,
Byungchul Parkce07a9412017-08-07 16:12:51 +09001873 struct held_lock *next, int distance, struct stack_trace *trace,
1874 int (*save)(struct stack_trace *trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001875{
Ming Leidb0002a2009-07-16 15:44:29 +02001876 struct lock_list *uninitialized_var(target_entry);
Peter Zijlstra8b405d52017-10-04 11:13:37 +02001877 struct lock_list *entry;
1878 struct lock_list this;
1879 int ret;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001880
1881 /*
1882 * Prove that the new <prev> -> <next> dependency would not
1883 * create a circular dependency in the graph. (We do this by
1884 * forward-recursing into the graph starting at <next>, and
1885 * checking whether we can reach <prev>.)
1886 *
1887 * We are using global variables to control the recursion, to
1888 * keep the stackframe size of the recursive functions low:
1889 */
Ming Leidb0002a2009-07-16 15:44:29 +02001890 this.class = hlock_class(next);
1891 this.parent = NULL;
1892 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
Peter Zijlstra8b405d52017-10-04 11:13:37 +02001893 if (unlikely(!ret)) {
1894 if (!trace->entries) {
1895 /*
1896 * If @save fails here, the printing might trigger
1897 * a WARN but because of the !nr_entries it should
1898 * not do bad things.
1899 */
1900 save(trace);
1901 }
Byungchul Park383a4bc2017-08-07 16:12:55 +09001902 return print_circular_bug(&this, target_entry, next, prev, trace);
Peter Zijlstra8b405d52017-10-04 11:13:37 +02001903 }
Ming Leidb0002a2009-07-16 15:44:29 +02001904 else if (unlikely(ret < 0))
1905 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02001906
Peter Zijlstra8e182572007-07-19 01:48:54 -07001907 if (!check_prev_add_irq(curr, prev, next))
1908 return 0;
1909
1910 /*
1911 * For recursive read-locks we do all the dependency checks,
1912 * but we dont store read-triggered dependencies (only
1913 * write-triggered dependencies). This ensures that only the
1914 * write-side dependencies matter, and that if for example a
1915 * write-lock never takes any other locks, then the reads are
1916 * equivalent to a NOP.
1917 */
1918 if (next->read == 2 || prev->read == 2)
1919 return 1;
1920 /*
1921 * Is the <prev> -> <next> dependency already present?
1922 *
1923 * (this may occur even though this is a new chain: consider
1924 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1925 * chains - the second one will be new, but L1 already has
1926 * L2 added to its dependency list, due to the first chain.)
1927 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001928 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1929 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001930 if (distance == 1)
1931 entry->distance = 1;
Byungchul Park70911fd2017-08-07 16:12:50 +09001932 return 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001933 }
1934 }
1935
Peter Zijlstraae813302017-03-03 10:13:38 +01001936 /*
1937 * Is the <prev> -> <next> link redundant?
1938 */
1939 this.class = hlock_class(prev);
1940 this.parent = NULL;
1941 ret = check_redundant(&this, hlock_class(next), &target_entry);
1942 if (!ret) {
1943 debug_atomic_inc(nr_redundant);
1944 return 2;
1945 }
1946 if (ret < 0)
1947 return print_bfs_bug(ret);
1948
1949
Peter Zijlstra8b405d52017-10-04 11:13:37 +02001950 if (!trace->entries && !save(trace))
Byungchul Parkce07a9412017-08-07 16:12:51 +09001951 return 0;
Yong Zhang4726f2a2010-05-04 14:16:48 +08001952
Peter Zijlstra8e182572007-07-19 01:48:54 -07001953 /*
1954 * Ok, all validations passed, add the new lock
1955 * to the previous lock's dependency list:
1956 */
Tahsin Erdogan83f06162016-11-08 00:02:07 -08001957 ret = add_lock_to_list(hlock_class(next),
Dave Jonesf82b2172008-08-11 09:30:23 +02001958 &hlock_class(prev)->locks_after,
Byungchul Parkce07a9412017-08-07 16:12:51 +09001959 next->acquire_ip, distance, trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001960
1961 if (!ret)
1962 return 0;
1963
Tahsin Erdogan83f06162016-11-08 00:02:07 -08001964 ret = add_lock_to_list(hlock_class(prev),
Dave Jonesf82b2172008-08-11 09:30:23 +02001965 &hlock_class(next)->locks_before,
Byungchul Parkce07a9412017-08-07 16:12:51 +09001966 next->acquire_ip, distance, trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001967 if (!ret)
1968 return 0;
1969
Byungchul Park70911fd2017-08-07 16:12:50 +09001970 return 2;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001971}
1972
1973/*
1974 * Add the dependency to all directly-previous locks that are 'relevant'.
1975 * The ones that are relevant are (in increasing distance from curr):
1976 * all consecutive trylock entries and the final non-trylock entry - or
1977 * the end of this context's lock-chain - whichever comes first.
1978 */
1979static int
1980check_prevs_add(struct task_struct *curr, struct held_lock *next)
1981{
1982 int depth = curr->lockdep_depth;
1983 struct held_lock *hlock;
Peter Zijlstra8b405d52017-10-04 11:13:37 +02001984 struct stack_trace trace = {
1985 .nr_entries = 0,
1986 .max_entries = 0,
1987 .entries = NULL,
1988 .skip = 0,
1989 };
Peter Zijlstra8e182572007-07-19 01:48:54 -07001990
1991 /*
1992 * Debugging checks.
1993 *
1994 * Depth must not be zero for a non-head lock:
1995 */
1996 if (!depth)
1997 goto out_bug;
1998 /*
1999 * At least two relevant locks must exist for this
2000 * to be a head:
2001 */
2002 if (curr->held_locks[depth].irq_context !=
2003 curr->held_locks[depth-1].irq_context)
2004 goto out_bug;
2005
2006 for (;;) {
2007 int distance = curr->lockdep_depth - depth + 1;
Oleg Nesterov1b5ff812014-01-20 19:20:10 +01002008 hlock = curr->held_locks + depth - 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002009 /*
Byungchul Parkb09be672017-08-07 16:12:52 +09002010 * Only non-crosslock entries get new dependencies added.
2011 * Crosslock entries will be added by commit later:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002012 */
Byungchul Parkb09be672017-08-07 16:12:52 +09002013 if (!cross_lock(hlock->instance)) {
Byungchul Parkce07a9412017-08-07 16:12:51 +09002014 /*
Byungchul Parkb09be672017-08-07 16:12:52 +09002015 * Only non-recursive-read entries get new dependencies
2016 * added:
Byungchul Parkce07a9412017-08-07 16:12:51 +09002017 */
Byungchul Parkb09be672017-08-07 16:12:52 +09002018 if (hlock->read != 2 && hlock->check) {
2019 int ret = check_prev_add(curr, hlock, next,
Peter Zijlstra8b405d52017-10-04 11:13:37 +02002020 distance, &trace, save_trace);
Byungchul Parkb09be672017-08-07 16:12:52 +09002021 if (!ret)
2022 return 0;
Byungchul Parkce07a9412017-08-07 16:12:51 +09002023
Byungchul Parkb09be672017-08-07 16:12:52 +09002024 /*
Byungchul Parkb09be672017-08-07 16:12:52 +09002025 * Stop after the first non-trylock entry,
2026 * as non-trylock entries have added their
2027 * own direct dependencies already, so this
2028 * lock is connected to them indirectly:
2029 */
2030 if (!hlock->trylock)
2031 break;
2032 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07002033 }
2034 depth--;
2035 /*
2036 * End of lock-stack?
2037 */
2038 if (!depth)
2039 break;
2040 /*
2041 * Stop the search if we cross into another context:
2042 */
2043 if (curr->held_locks[depth].irq_context !=
2044 curr->held_locks[depth-1].irq_context)
2045 break;
2046 }
2047 return 1;
2048out_bug:
2049 if (!debug_locks_off_graph_unlock())
2050 return 0;
2051
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002052 /*
2053 * Clearly we all shouldn't be here, but since we made it we
2054 * can reliable say we messed up our state. See the above two
2055 * gotos for reasons why we could possibly end up here.
2056 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002057 WARN_ON(1);
2058
2059 return 0;
2060}
2061
2062unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08002063struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08002064int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08002065static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
2066
2067struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
2068{
2069 return lock_classes + chain_hlocks[chain->base + i];
2070}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002071
2072/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002073 * Returns the index of the first held_lock of the current chain
2074 */
2075static inline int get_first_held_lock(struct task_struct *curr,
2076 struct held_lock *hlock)
2077{
2078 int i;
2079 struct held_lock *hlock_curr;
2080
2081 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2082 hlock_curr = curr->held_locks + i;
2083 if (hlock_curr->irq_context != hlock->irq_context)
2084 break;
2085
2086 }
2087
2088 return ++i;
2089}
2090
Borislav Petkov5c8a0102016-04-04 10:42:07 +02002091#ifdef CONFIG_DEBUG_LOCKDEP
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002092/*
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002093 * Returns the next chain_key iteration
2094 */
2095static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
2096{
2097 u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
2098
2099 printk(" class_idx:%d -> chain_key:%016Lx",
2100 class_idx,
2101 (unsigned long long)new_chain_key);
2102 return new_chain_key;
2103}
2104
2105static void
2106print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
2107{
2108 struct held_lock *hlock;
2109 u64 chain_key = 0;
2110 int depth = curr->lockdep_depth;
2111 int i;
2112
2113 printk("depth: %u\n", depth + 1);
2114 for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
2115 hlock = curr->held_locks + i;
2116 chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
2117
2118 print_lock(hlock);
2119 }
2120
2121 print_chain_key_iteration(hlock_next->class_idx, chain_key);
2122 print_lock(hlock_next);
2123}
2124
2125static void print_chain_keys_chain(struct lock_chain *chain)
2126{
2127 int i;
2128 u64 chain_key = 0;
2129 int class_id;
2130
2131 printk("depth: %u\n", chain->depth);
2132 for (i = 0; i < chain->depth; i++) {
2133 class_id = chain_hlocks[chain->base + i];
2134 chain_key = print_chain_key_iteration(class_id + 1, chain_key);
2135
2136 print_lock_name(lock_classes + class_id);
2137 printk("\n");
2138 }
2139}
2140
2141static void print_collision(struct task_struct *curr,
2142 struct held_lock *hlock_next,
2143 struct lock_chain *chain)
2144{
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002145 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002146 pr_warn("============================\n");
2147 pr_warn("WARNING: chain_key collision\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002148 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002149 pr_warn("----------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002150 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
2151 pr_warn("Hash chain already cached but the contents don't match!\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002152
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002153 pr_warn("Held locks:");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002154 print_chain_keys_held_locks(curr, hlock_next);
2155
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002156 pr_warn("Locks in cached chain:");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002157 print_chain_keys_chain(chain);
2158
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002159 pr_warn("\nstack backtrace:\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002160 dump_stack();
2161}
Borislav Petkov5c8a0102016-04-04 10:42:07 +02002162#endif
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002163
2164/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002165 * Checks whether the chain and the current held locks are consistent
2166 * in depth and also in content. If they are not it most likely means
2167 * that there was a collision during the calculation of the chain_key.
2168 * Returns: 0 not passed, 1 passed
2169 */
2170static int check_no_collision(struct task_struct *curr,
2171 struct held_lock *hlock,
2172 struct lock_chain *chain)
2173{
2174#ifdef CONFIG_DEBUG_LOCKDEP
2175 int i, j, id;
2176
2177 i = get_first_held_lock(curr, hlock);
2178
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002179 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
2180 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002181 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002182 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002183
2184 for (j = 0; j < chain->depth - 1; j++, i++) {
2185 id = curr->held_locks[i].class_idx - 1;
2186
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002187 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
2188 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002189 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002190 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002191 }
2192#endif
2193 return 1;
2194}
2195
2196/*
Byungchul Park49347a92017-08-07 16:12:49 +09002197 * This is for building a chain between just two different classes,
2198 * instead of adding a new hlock upon current, which is done by
2199 * add_chain_cache().
2200 *
2201 * This can be called in any context with two classes, while
2202 * add_chain_cache() must be done within the lock owener's context
2203 * since it uses hlock which might be racy in another context.
2204 */
2205static inline int add_chain_cache_classes(unsigned int prev,
2206 unsigned int next,
2207 unsigned int irq_context,
2208 u64 chain_key)
2209{
2210 struct hlist_head *hash_head = chainhashentry(chain_key);
2211 struct lock_chain *chain;
2212
2213 /*
2214 * Allocate a new chain entry from the static array, and add
2215 * it to the hash:
2216 */
2217
2218 /*
2219 * We might need to take the graph lock, ensure we've got IRQs
2220 * disabled to make this an IRQ-safe lock.. for recursion reasons
2221 * lockdep won't complain about its own locking errors.
2222 */
2223 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2224 return 0;
2225
2226 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2227 if (!debug_locks_off_graph_unlock())
2228 return 0;
2229
2230 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2231 dump_stack();
2232 return 0;
2233 }
2234
2235 chain = lock_chains + nr_lock_chains++;
2236 chain->chain_key = chain_key;
2237 chain->irq_context = irq_context;
2238 chain->depth = 2;
2239 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2240 chain->base = nr_chain_hlocks;
2241 nr_chain_hlocks += chain->depth;
2242 chain_hlocks[chain->base] = prev - 1;
2243 chain_hlocks[chain->base + 1] = next -1;
2244 }
2245#ifdef CONFIG_DEBUG_LOCKDEP
2246 /*
2247 * Important for check_no_collision().
2248 */
2249 else {
2250 if (!debug_locks_off_graph_unlock())
2251 return 0;
2252
2253 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2254 dump_stack();
2255 return 0;
2256 }
2257#endif
2258
2259 hlist_add_head_rcu(&chain->entry, hash_head);
2260 debug_atomic_inc(chain_lookup_misses);
2261 inc_chains();
2262
2263 return 1;
2264}
2265
2266/*
Byungchul Park545c23f2017-08-07 16:12:48 +09002267 * Adds a dependency chain into chain hashtable. And must be called with
2268 * graph_lock held.
2269 *
2270 * Return 0 if fail, and graph_lock is released.
2271 * Return 1 if succeed, with graph_lock held.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002272 */
Byungchul Park545c23f2017-08-07 16:12:48 +09002273static inline int add_chain_cache(struct task_struct *curr,
2274 struct held_lock *hlock,
2275 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002276{
Dave Jonesf82b2172008-08-11 09:30:23 +02002277 struct lock_class *class = hlock_class(hlock);
Andrew Mortona63f38c2016-02-03 13:44:12 -08002278 struct hlist_head *hash_head = chainhashentry(chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002279 struct lock_chain *chain;
Steven Rostedte0944ee2011-04-20 21:42:00 -04002280 int i, j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002281
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002282 /*
Byungchul Park545c23f2017-08-07 16:12:48 +09002283 * Allocate a new chain entry from the static array, and add
2284 * it to the hash:
2285 */
2286
2287 /*
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002288 * We might need to take the graph lock, ensure we've got IRQs
2289 * disabled to make this an IRQ-safe lock.. for recursion reasons
2290 * lockdep won't complain about its own locking errors.
2291 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08002292 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2293 return 0;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002294
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002295 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002296 if (!debug_locks_off_graph_unlock())
2297 return 0;
2298
Dave Jones2c522832013-04-25 13:40:02 -04002299 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002300 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002301 return 0;
2302 }
2303 chain = lock_chains + nr_lock_chains++;
2304 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08002305 chain->irq_context = hlock->irq_context;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002306 i = get_first_held_lock(curr, hlock);
Huang, Ying443cd502008-06-20 16:39:21 +08002307 chain->depth = curr->lockdep_depth + 1 - i;
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002308
2309 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
2310 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
2311 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
2312
Steven Rostedte0944ee2011-04-20 21:42:00 -04002313 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2314 chain->base = nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08002315 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02002316 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08002317 chain_hlocks[chain->base + j] = lock_id;
2318 }
2319 chain_hlocks[chain->base + j] = class - lock_classes;
2320 }
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002321
2322 if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS)
2323 nr_chain_hlocks += chain->depth;
2324
2325#ifdef CONFIG_DEBUG_LOCKDEP
2326 /*
2327 * Important for check_no_collision().
2328 */
2329 if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) {
Byungchul Parkf9af4562017-01-13 11:42:04 +09002330 if (!debug_locks_off_graph_unlock())
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002331 return 0;
2332
2333 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2334 dump_stack();
2335 return 0;
2336 }
2337#endif
2338
Andrew Mortona63f38c2016-02-03 13:44:12 -08002339 hlist_add_head_rcu(&chain->entry, hash_head);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002340 debug_atomic_inc(chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002341 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002342
2343 return 1;
2344}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002345
Byungchul Park545c23f2017-08-07 16:12:48 +09002346/*
2347 * Look up a dependency chain.
2348 */
2349static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
2350{
2351 struct hlist_head *hash_head = chainhashentry(chain_key);
2352 struct lock_chain *chain;
2353
2354 /*
2355 * We can walk it lock-free, because entries only get added
2356 * to the hash:
2357 */
2358 hlist_for_each_entry_rcu(chain, hash_head, entry) {
2359 if (chain->chain_key == chain_key) {
2360 debug_atomic_inc(chain_lookup_hits);
2361 return chain;
2362 }
2363 }
2364 return NULL;
2365}
2366
2367/*
2368 * If the key is not present yet in dependency chain cache then
2369 * add it and return 1 - in this case the new dependency chain is
2370 * validated. If the key is already hashed, return 0.
2371 * (On return with 1 graph_lock is held.)
2372 */
2373static inline int lookup_chain_cache_add(struct task_struct *curr,
2374 struct held_lock *hlock,
2375 u64 chain_key)
2376{
2377 struct lock_class *class = hlock_class(hlock);
2378 struct lock_chain *chain = lookup_chain_cache(chain_key);
2379
2380 if (chain) {
2381cache_hit:
2382 if (!check_no_collision(curr, hlock, chain))
2383 return 0;
2384
2385 if (very_verbose(class)) {
2386 printk("\nhash chain already cached, key: "
2387 "%016Lx tail class: [%p] %s\n",
2388 (unsigned long long)chain_key,
2389 class->key, class->name);
2390 }
2391
2392 return 0;
2393 }
2394
2395 if (very_verbose(class)) {
2396 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2397 (unsigned long long)chain_key, class->key, class->name);
2398 }
2399
2400 if (!graph_lock())
2401 return 0;
2402
2403 /*
2404 * We have to walk the chain again locked - to avoid duplicates:
2405 */
2406 chain = lookup_chain_cache(chain_key);
2407 if (chain) {
2408 graph_unlock();
2409 goto cache_hit;
2410 }
2411
2412 if (!add_chain_cache(curr, hlock, chain_key))
2413 return 0;
2414
2415 return 1;
2416}
2417
Peter Zijlstra8e182572007-07-19 01:48:54 -07002418static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07002419 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002420{
2421 /*
2422 * Trylock needs to maintain the stack of held locks, but it
2423 * does not add new dependencies, because trylock can be done
2424 * in any order.
2425 *
2426 * We look up the chain_key and do the O(N^2) check and update of
2427 * the dependencies only if this is a new dependency chain.
Byungchul Park545c23f2017-08-07 16:12:48 +09002428 * (If lookup_chain_cache_add() return with 1 it acquires
Peter Zijlstra8e182572007-07-19 01:48:54 -07002429 * graph_lock for us)
2430 */
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01002431 if (!hlock->trylock && hlock->check &&
Byungchul Park545c23f2017-08-07 16:12:48 +09002432 lookup_chain_cache_add(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002433 /*
2434 * Check whether last held lock:
2435 *
2436 * - is irq-safe, if this lock is irq-unsafe
2437 * - is softirq-safe, if this lock is hardirq-unsafe
2438 *
2439 * And check whether the new lock's dependency graph
2440 * could lead back to the previous lock.
2441 *
2442 * any of these scenarios could lead to a deadlock. If
2443 * All validations
2444 */
2445 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2446
2447 if (!ret)
2448 return 0;
2449 /*
2450 * Mark recursive read, as we jump over it when
2451 * building dependencies (just like we jump over
2452 * trylock entries):
2453 */
2454 if (ret == 2)
2455 hlock->read = 2;
2456 /*
2457 * Add dependency only if this lock is not the head
2458 * of the chain, and if it's not a secondary read-lock:
2459 */
Byungchul Park545c23f2017-08-07 16:12:48 +09002460 if (!chain_head && ret != 2) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002461 if (!check_prevs_add(curr, hlock))
2462 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09002463 }
2464
Peter Zijlstra8e182572007-07-19 01:48:54 -07002465 graph_unlock();
Byungchul Park545c23f2017-08-07 16:12:48 +09002466 } else {
2467 /* after lookup_chain_cache_add(): */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002468 if (unlikely(!debug_locks))
2469 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09002470 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07002471
2472 return 1;
2473}
2474#else
2475static inline int validate_chain(struct task_struct *curr,
2476 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002477 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002478{
2479 return 1;
2480}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07002481#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002482
2483/*
2484 * We are building curr_chain_key incrementally, so double-check
2485 * it from scratch, to make sure that it's done correctly:
2486 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002487static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002488{
2489#ifdef CONFIG_DEBUG_LOCKDEP
2490 struct held_lock *hlock, *prev_hlock = NULL;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002491 unsigned int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002492 u64 chain_key = 0;
2493
2494 for (i = 0; i < curr->lockdep_depth; i++) {
2495 hlock = curr->held_locks + i;
2496 if (chain_key != hlock->prev_chain_key) {
2497 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002498 /*
2499 * We got mighty confused, our chain keys don't match
2500 * with what we expect, someone trample on our task state?
2501 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002502 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002503 curr->lockdep_depth, i,
2504 (unsigned long long)chain_key,
2505 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002506 return;
2507 }
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002508 /*
2509 * Whoops ran out of static storage again?
2510 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002511 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
Jarek Poplawski381a2292007-02-10 01:44:58 -08002512 return;
2513
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002514 if (prev_hlock && (prev_hlock->irq_context !=
2515 hlock->irq_context))
2516 chain_key = 0;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002517 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002518 prev_hlock = hlock;
2519 }
2520 if (chain_key != curr->curr_chain_key) {
2521 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002522 /*
2523 * More smoking hash instead of calculating it, damn see these
2524 * numbers float.. I bet that a pink elephant stepped on my memory.
2525 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002526 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002527 curr->lockdep_depth, i,
2528 (unsigned long long)chain_key,
2529 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002530 }
2531#endif
2532}
2533
Steven Rostedt282b5c22011-04-20 21:41:59 -04002534static void
2535print_usage_bug_scenario(struct held_lock *lock)
2536{
2537 struct lock_class *class = hlock_class(lock);
2538
2539 printk(" Possible unsafe locking scenario:\n\n");
2540 printk(" CPU0\n");
2541 printk(" ----\n");
2542 printk(" lock(");
2543 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002544 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002545 printk(" <Interrupt>\n");
2546 printk(" lock(");
2547 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002548 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002549 printk("\n *** DEADLOCK ***\n\n");
2550}
2551
Peter Zijlstra8e182572007-07-19 01:48:54 -07002552static int
2553print_usage_bug(struct task_struct *curr, struct held_lock *this,
2554 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2555{
2556 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2557 return 0;
2558
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002559 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002560 pr_warn("================================\n");
2561 pr_warn("WARNING: inconsistent lock state\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002562 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002563 pr_warn("--------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002564
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002565 pr_warn("inconsistent {%s} -> {%s} usage.\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07002566 usage_str[prev_bit], usage_str[new_bit]);
2567
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002568 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002569 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002570 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2571 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2572 trace_hardirqs_enabled(curr),
2573 trace_softirqs_enabled(curr));
2574 print_lock(this);
2575
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002576 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002577 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002578
2579 print_irqtrace_events(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002580 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002581 print_usage_bug_scenario(this);
2582
Peter Zijlstra8e182572007-07-19 01:48:54 -07002583 lockdep_print_held_locks(curr);
2584
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002585 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002586 dump_stack();
2587
2588 return 0;
2589}
2590
2591/*
2592 * Print out an error if an invalid bit is set:
2593 */
2594static inline int
2595valid_state(struct task_struct *curr, struct held_lock *this,
2596 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2597{
Dave Jonesf82b2172008-08-11 09:30:23 +02002598 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002599 return print_usage_bug(curr, this, bad_bit, new_bit);
2600 return 1;
2601}
2602
2603static int mark_lock(struct task_struct *curr, struct held_lock *this,
2604 enum lock_usage_bit new_bit);
2605
Steven Rostedt81d68a92008-05-12 21:20:42 +02002606#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002607
2608/*
2609 * print irq inversion bug:
2610 */
2611static int
Ming Lei24208ca2009-07-16 15:44:29 +02002612print_irq_inversion_bug(struct task_struct *curr,
2613 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002614 struct held_lock *this, int forwards,
2615 const char *irqclass)
2616{
Steven Rostedtdad3d742011-04-20 21:41:57 -04002617 struct lock_list *entry = other;
2618 struct lock_list *middle = NULL;
2619 int depth;
2620
Ingo Molnar74c383f2006-12-13 00:34:43 -08002621 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002622 return 0;
2623
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002624 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002625 pr_warn("========================================================\n");
2626 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002627 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002628 pr_warn("--------------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002629 pr_warn("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002630 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002631 print_lock(this);
2632 if (forwards)
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002633 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002634 else
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002635 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002636 print_lock_name(other->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002637 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002638
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002639 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04002640
2641 /* Find a middle lock (if one exists) */
2642 depth = get_lock_depth(other);
2643 do {
2644 if (depth == 0 && (entry != root)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002645 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
Steven Rostedtdad3d742011-04-20 21:41:57 -04002646 break;
2647 }
2648 middle = entry;
2649 entry = get_lock_parent(entry);
2650 depth--;
2651 } while (entry && entry != root && (depth >= 0));
2652 if (forwards)
2653 print_irq_lock_scenario(root, other,
2654 middle ? middle->class : root->class, other->class);
2655 else
2656 print_irq_lock_scenario(other, root,
2657 middle ? middle->class : other->class, root->class);
2658
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002659 lockdep_print_held_locks(curr);
2660
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002661 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
Ming Lei24208ca2009-07-16 15:44:29 +02002662 if (!save_trace(&root->trace))
2663 return 0;
2664 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002665
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002666 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002667 dump_stack();
2668
2669 return 0;
2670}
2671
2672/*
2673 * Prove that in the forwards-direction subgraph starting at <this>
2674 * there is no lock matching <mask>:
2675 */
2676static int
2677check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2678 enum lock_usage_bit bit, const char *irqclass)
2679{
2680 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002681 struct lock_list root;
2682 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002683
Ming Leid7aaba12009-07-16 15:44:29 +02002684 root.parent = NULL;
2685 root.class = hlock_class(this);
2686 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002687 if (ret < 0)
2688 return print_bfs_bug(ret);
2689 if (ret == 1)
2690 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002691
Ming Lei24208ca2009-07-16 15:44:29 +02002692 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002693 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002694}
2695
2696/*
2697 * Prove that in the backwards-direction subgraph starting at <this>
2698 * there is no lock matching <mask>:
2699 */
2700static int
2701check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2702 enum lock_usage_bit bit, const char *irqclass)
2703{
2704 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002705 struct lock_list root;
2706 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002707
Ming Leid7aaba12009-07-16 15:44:29 +02002708 root.parent = NULL;
2709 root.class = hlock_class(this);
2710 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002711 if (ret < 0)
2712 return print_bfs_bug(ret);
2713 if (ret == 1)
2714 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002715
Ming Lei24208ca2009-07-16 15:44:29 +02002716 return print_irq_inversion_bug(curr, &root, target_entry,
Oleg Nesterov48d50672010-01-26 19:16:41 +01002717 this, 0, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002718}
2719
Ingo Molnar3117df02006-12-13 00:34:43 -08002720void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002721{
2722 printk("irq event stamp: %u\n", curr->irq_events);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002723 printk("hardirqs last enabled at (%u): [<%p>] %pS\n",
2724 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
2725 (void *)curr->hardirq_enable_ip);
2726 printk("hardirqs last disabled at (%u): [<%p>] %pS\n",
2727 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
2728 (void *)curr->hardirq_disable_ip);
2729 printk("softirqs last enabled at (%u): [<%p>] %pS\n",
2730 curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
2731 (void *)curr->softirq_enable_ip);
2732 printk("softirqs last disabled at (%u): [<%p>] %pS\n",
2733 curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
2734 (void *)curr->softirq_disable_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002735}
2736
Peter Zijlstracd953022009-01-22 16:38:21 +01002737static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002738{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002739#if HARDIRQ_VERBOSE
2740 return class_filter(class);
2741#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002742 return 0;
2743}
2744
Peter Zijlstracd953022009-01-22 16:38:21 +01002745static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002746{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002747#if SOFTIRQ_VERBOSE
2748 return class_filter(class);
2749#endif
2750 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002751}
2752
2753#define STRICT_READ_CHECKS 1
2754
Peter Zijlstracd953022009-01-22 16:38:21 +01002755static int (*state_verbose_f[])(struct lock_class *class) = {
2756#define LOCKDEP_STATE(__STATE) \
2757 __STATE##_verbose,
2758#include "lockdep_states.h"
2759#undef LOCKDEP_STATE
2760};
2761
2762static inline int state_verbose(enum lock_usage_bit bit,
2763 struct lock_class *class)
2764{
2765 return state_verbose_f[bit >> 2](class);
2766}
2767
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002768typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2769 enum lock_usage_bit bit, const char *name);
2770
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002771static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002772mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2773 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002774{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002775 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002776 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002777 int dir = new_bit & 2;
2778
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002779 /*
2780 * mark USED_IN has to look forwards -- to ensure no dependency
2781 * has ENABLED state, which would allow recursion deadlocks.
2782 *
2783 * mark ENABLED has to look backwards -- to ensure no dependee
2784 * has USED_IN state, which, again, would allow recursion deadlocks.
2785 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002786 check_usage_f usage = dir ?
2787 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002788
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002789 /*
2790 * Validate that this particular lock does not have conflicting
2791 * usage states.
2792 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002793 if (!valid_state(curr, this, new_bit, excl_bit))
2794 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002795
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002796 /*
2797 * Validate that the lock dependencies don't have conflicting usage
2798 * states.
2799 */
2800 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002801 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002802 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002803
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002804 /*
2805 * Check for read in write conflicts
2806 */
2807 if (!read) {
2808 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2809 return 0;
2810
2811 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002812 !usage(curr, this, excl_bit + 1,
2813 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002814 return 0;
2815 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002816
Peter Zijlstracd953022009-01-22 16:38:21 +01002817 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002818 return 2;
2819
2820 return 1;
2821}
2822
Nick Piggincf40bd12009-01-21 08:12:39 +01002823enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002824#define LOCKDEP_STATE(__STATE) __STATE,
2825#include "lockdep_states.h"
2826#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002827};
2828
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002829/*
2830 * Mark all held locks with a usage bit:
2831 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002832static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002833mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002834{
2835 enum lock_usage_bit usage_bit;
2836 struct held_lock *hlock;
2837 int i;
2838
2839 for (i = 0; i < curr->lockdep_depth; i++) {
2840 hlock = curr->held_locks + i;
2841
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002842 usage_bit = 2 + (mark << 2); /* ENABLED */
2843 if (hlock->read)
2844 usage_bit += 1; /* READ */
2845
2846 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002847
Oleg Nesterov34d0ed52014-01-20 19:20:13 +01002848 if (!hlock->check)
Peter Zijlstraefbe2ee2011-07-07 11:39:45 +02002849 continue;
2850
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002851 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002852 return 0;
2853 }
2854
2855 return 1;
2856}
2857
2858/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002859 * Hardirqs will be enabled:
2860 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002861static void __trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002862{
2863 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002864
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002865 /* we'll do an OFF -> ON transition: */
2866 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002867
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002868 /*
2869 * We are going to turn hardirqs on, so set the
2870 * usage bit for all held locks:
2871 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002872 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002873 return;
2874 /*
2875 * If we have softirqs enabled, then set the usage
2876 * bit for all held locks. (disabled hardirqs prevented
2877 * this bit from being set before)
2878 */
2879 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002880 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002881 return;
2882
2883 curr->hardirq_enable_ip = ip;
2884 curr->hardirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002885 debug_atomic_inc(hardirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002886}
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002887
Andi Kleenb35f8302014-02-08 08:52:02 +01002888__visible void trace_hardirqs_on_caller(unsigned long ip)
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002889{
2890 time_hardirqs_on(CALLER_ADDR0, ip);
2891
2892 if (unlikely(!debug_locks || current->lockdep_recursion))
2893 return;
2894
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002895 if (unlikely(current->hardirqs_enabled)) {
2896 /*
2897 * Neither irq nor preemption are disabled here
2898 * so this is racy by nature but losing one hit
2899 * in a stat is not a big deal.
2900 */
2901 __debug_atomic_inc(redundant_hardirqs_on);
2902 return;
2903 }
2904
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002905 /*
2906 * We're enabling irqs and according to our state above irqs weren't
2907 * already enabled, yet we find the hardware thinks they are in fact
2908 * enabled.. someone messed up their IRQ state tracing.
2909 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002910 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2911 return;
2912
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002913 /*
2914 * See the fine text that goes along with this variable definition.
2915 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002916 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2917 return;
2918
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002919 /*
2920 * Can't allow enabling interrupts while in an interrupt handler,
2921 * that's general bad form and such. Recursion, limited stack etc..
2922 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002923 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2924 return;
2925
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002926 current->lockdep_recursion = 1;
2927 __trace_hardirqs_on_caller(ip);
2928 current->lockdep_recursion = 0;
2929}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002930EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002931
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002932void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002933{
2934 trace_hardirqs_on_caller(CALLER_ADDR0);
2935}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002936EXPORT_SYMBOL(trace_hardirqs_on);
2937
2938/*
2939 * Hardirqs were disabled:
2940 */
Andi Kleenb35f8302014-02-08 08:52:02 +01002941__visible void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002942{
2943 struct task_struct *curr = current;
2944
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002945 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002946
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002947 if (unlikely(!debug_locks || current->lockdep_recursion))
2948 return;
2949
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002950 /*
2951 * So we're supposed to get called after you mask local IRQs, but for
2952 * some reason the hardware doesn't quite think you did a proper job.
2953 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002954 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2955 return;
2956
2957 if (curr->hardirqs_enabled) {
2958 /*
2959 * We have done an ON -> OFF transition:
2960 */
2961 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002962 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002963 curr->hardirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002964 debug_atomic_inc(hardirqs_off_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002965 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002966 debug_atomic_inc(redundant_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002967}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002968EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002969
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002970void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002971{
2972 trace_hardirqs_off_caller(CALLER_ADDR0);
2973}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002974EXPORT_SYMBOL(trace_hardirqs_off);
2975
2976/*
2977 * Softirqs will be enabled:
2978 */
2979void trace_softirqs_on(unsigned long ip)
2980{
2981 struct task_struct *curr = current;
2982
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002983 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002984 return;
2985
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002986 /*
2987 * We fancy IRQs being disabled here, see softirq.c, avoids
2988 * funny state and nesting things.
2989 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002990 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2991 return;
2992
2993 if (curr->softirqs_enabled) {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002994 debug_atomic_inc(redundant_softirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002995 return;
2996 }
2997
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002998 current->lockdep_recursion = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002999 /*
3000 * We'll do an OFF -> ON transition:
3001 */
3002 curr->softirqs_enabled = 1;
3003 curr->softirq_enable_ip = ip;
3004 curr->softirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003005 debug_atomic_inc(softirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003006 /*
3007 * We are going to turn softirqs on, so set the
3008 * usage bit for all held locks, if hardirqs are
3009 * enabled too:
3010 */
3011 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01003012 mark_held_locks(curr, SOFTIRQ);
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02003013 current->lockdep_recursion = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003014}
3015
3016/*
3017 * Softirqs were disabled:
3018 */
3019void trace_softirqs_off(unsigned long ip)
3020{
3021 struct task_struct *curr = current;
3022
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02003023 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003024 return;
3025
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003026 /*
3027 * We fancy IRQs being disabled here, see softirq.c
3028 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003029 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3030 return;
3031
3032 if (curr->softirqs_enabled) {
3033 /*
3034 * We have done an ON -> OFF transition:
3035 */
3036 curr->softirqs_enabled = 0;
3037 curr->softirq_disable_ip = ip;
3038 curr->softirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003039 debug_atomic_inc(softirqs_off_events);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003040 /*
3041 * Whoops, we wanted softirqs off, so why aren't they?
3042 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003043 DEBUG_LOCKS_WARN_ON(!softirq_count());
3044 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003045 debug_atomic_inc(redundant_softirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003046}
3047
Peter Zijlstra8e182572007-07-19 01:48:54 -07003048static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
3049{
3050 /*
3051 * If non-trylock use in a hardirq or softirq context, then
3052 * mark the lock as used in these contexts:
3053 */
3054 if (!hlock->trylock) {
3055 if (hlock->read) {
3056 if (curr->hardirq_context)
3057 if (!mark_lock(curr, hlock,
3058 LOCK_USED_IN_HARDIRQ_READ))
3059 return 0;
3060 if (curr->softirq_context)
3061 if (!mark_lock(curr, hlock,
3062 LOCK_USED_IN_SOFTIRQ_READ))
3063 return 0;
3064 } else {
3065 if (curr->hardirq_context)
3066 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
3067 return 0;
3068 if (curr->softirq_context)
3069 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
3070 return 0;
3071 }
3072 }
3073 if (!hlock->hardirqs_off) {
3074 if (hlock->read) {
3075 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003076 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003077 return 0;
3078 if (curr->softirqs_enabled)
3079 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003080 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003081 return 0;
3082 } else {
3083 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003084 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003085 return 0;
3086 if (curr->softirqs_enabled)
3087 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003088 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003089 return 0;
3090 }
3091 }
3092
3093 return 1;
3094}
3095
Boqun Fengc2469752016-02-16 13:57:40 +08003096static inline unsigned int task_irq_context(struct task_struct *task)
3097{
3098 return 2 * !!task->hardirq_context + !!task->softirq_context;
3099}
3100
Peter Zijlstra8e182572007-07-19 01:48:54 -07003101static int separate_irq_context(struct task_struct *curr,
3102 struct held_lock *hlock)
3103{
3104 unsigned int depth = curr->lockdep_depth;
3105
3106 /*
3107 * Keep track of points where we cross into an interrupt context:
3108 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003109 if (depth) {
3110 struct held_lock *prev_hlock;
3111
3112 prev_hlock = curr->held_locks + depth-1;
3113 /*
3114 * If we cross into another context, reset the
3115 * hash key (this also prevents the checking and the
3116 * adding of the dependency to 'prev'):
3117 */
3118 if (prev_hlock->irq_context != hlock->irq_context)
3119 return 1;
3120 }
3121 return 0;
3122}
3123
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003124#else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003125
3126static inline
3127int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
3128 enum lock_usage_bit new_bit)
3129{
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003130 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003131 return 1;
3132}
3133
3134static inline int mark_irqflags(struct task_struct *curr,
3135 struct held_lock *hlock)
3136{
3137 return 1;
3138}
3139
Boqun Fengc2469752016-02-16 13:57:40 +08003140static inline unsigned int task_irq_context(struct task_struct *task)
3141{
3142 return 0;
3143}
3144
Peter Zijlstra8e182572007-07-19 01:48:54 -07003145static inline int separate_irq_context(struct task_struct *curr,
3146 struct held_lock *hlock)
3147{
3148 return 0;
3149}
3150
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003151#endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003152
3153/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07003154 * Mark a lock with a usage bit, and validate the state transition:
3155 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003156static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02003157 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07003158{
3159 unsigned int new_mask = 1 << new_bit, ret = 1;
3160
3161 /*
3162 * If already set then do not dirty the cacheline,
3163 * nor do any checks:
3164 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003165 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003166 return 1;
3167
3168 if (!graph_lock())
3169 return 0;
3170 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003171 * Make sure we didn't race:
Peter Zijlstra8e182572007-07-19 01:48:54 -07003172 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003173 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07003174 graph_unlock();
3175 return 1;
3176 }
3177
Dave Jonesf82b2172008-08-11 09:30:23 +02003178 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003179
Dave Jonesf82b2172008-08-11 09:30:23 +02003180 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003181 return 0;
3182
3183 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01003184#define LOCKDEP_STATE(__STATE) \
3185 case LOCK_USED_IN_##__STATE: \
3186 case LOCK_USED_IN_##__STATE##_READ: \
3187 case LOCK_ENABLED_##__STATE: \
3188 case LOCK_ENABLED_##__STATE##_READ:
3189#include "lockdep_states.h"
3190#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07003191 ret = mark_lock_irq(curr, this, new_bit);
3192 if (!ret)
3193 return 0;
3194 break;
3195 case LOCK_USED:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003196 debug_atomic_dec(nr_unused_locks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07003197 break;
3198 default:
3199 if (!debug_locks_off_graph_unlock())
3200 return 0;
3201 WARN_ON(1);
3202 return 0;
3203 }
3204
3205 graph_unlock();
3206
3207 /*
3208 * We must printk outside of the graph_lock:
3209 */
3210 if (ret == 2) {
3211 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
3212 print_lock(this);
3213 print_irqtrace_events(curr);
3214 dump_stack();
3215 }
3216
3217 return ret;
3218}
3219
3220/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003221 * Initialize a lock instance's lock-class mapping info:
3222 */
Byungchul Parkb09be672017-08-07 16:12:52 +09003223static void __lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003224 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003225{
Yong Zhangd3d03d42011-11-09 16:04:51 +08003226 int i;
3227
3228 kmemcheck_mark_initialized(lock, sizeof(*lock));
3229
3230 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
3231 lock->class_cache[i] = NULL;
Hitoshi Mitake62016252010-10-05 18:01:51 +09003232
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003233#ifdef CONFIG_LOCK_STAT
3234 lock->cpu = raw_smp_processor_id();
3235#endif
3236
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003237 /*
3238 * Can't be having no nameless bastards around this place!
3239 */
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003240 if (DEBUG_LOCKS_WARN_ON(!name)) {
3241 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003242 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003243 }
3244
3245 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003246
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003247 /*
3248 * No key, no joy, we need to hash something.
3249 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003250 if (DEBUG_LOCKS_WARN_ON(!key))
3251 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003252 /*
3253 * Sanity check, the lock-class key must be persistent:
3254 */
3255 if (!static_obj(key)) {
3256 printk("BUG: key %p not in .data!\n", key);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003257 /*
3258 * What it says above ^^^^^, I suggest you read it.
3259 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003260 DEBUG_LOCKS_WARN_ON(1);
3261 return;
3262 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003263 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003264
3265 if (unlikely(!debug_locks))
3266 return;
3267
Peter Zijlstra35a93932015-02-26 16:23:11 +01003268 if (subclass) {
3269 unsigned long flags;
3270
3271 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3272 return;
3273
3274 raw_local_irq_save(flags);
3275 current->lockdep_recursion = 1;
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003276 register_lock_class(lock, subclass, 1);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003277 current->lockdep_recursion = 0;
3278 raw_local_irq_restore(flags);
3279 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003280}
Byungchul Parkb09be672017-08-07 16:12:52 +09003281
3282void lockdep_init_map(struct lockdep_map *lock, const char *name,
3283 struct lock_class_key *key, int subclass)
3284{
3285 cross_init(lock, 0);
3286 __lockdep_init_map(lock, name, key, subclass);
3287}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003288EXPORT_SYMBOL_GPL(lockdep_init_map);
3289
Byungchul Parkb09be672017-08-07 16:12:52 +09003290#ifdef CONFIG_LOCKDEP_CROSSRELEASE
3291void lockdep_init_map_crosslock(struct lockdep_map *lock, const char *name,
3292 struct lock_class_key *key, int subclass)
3293{
3294 cross_init(lock, 1);
3295 __lockdep_init_map(lock, name, key, subclass);
3296}
3297EXPORT_SYMBOL_GPL(lockdep_init_map_crosslock);
3298#endif
3299
Peter Zijlstra1704f472010-03-19 01:37:42 +01003300struct lock_class_key __lockdep_no_validate__;
Kent Overstreetea6749c2012-12-27 22:21:58 -08003301EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003302
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003303static int
3304print_lock_nested_lock_not_held(struct task_struct *curr,
3305 struct held_lock *hlock,
3306 unsigned long ip)
3307{
3308 if (!debug_locks_off())
3309 return 0;
3310 if (debug_locks_silent)
3311 return 0;
3312
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003313 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003314 pr_warn("==================================\n");
3315 pr_warn("WARNING: Nested lock was not taken\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003316 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003317 pr_warn("----------------------------------\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003318
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003319 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003320 print_lock(hlock);
3321
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003322 pr_warn("\nbut this task is not holding:\n");
3323 pr_warn("%s\n", hlock->nest_lock->name);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003324
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003325 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003326 dump_stack();
3327
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003328 pr_warn("\nother info that might help us debug this:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003329 lockdep_print_held_locks(curr);
3330
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003331 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003332 dump_stack();
3333
3334 return 0;
3335}
3336
Peter Zijlstraf8319482016-11-30 14:32:25 +11003337static int __lock_is_held(struct lockdep_map *lock, int read);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003338
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003339/*
3340 * This gets called for every mutex_lock*()/spin_lock*() operation.
3341 * We maintain the dependency maps and validate the locking attempt:
3342 */
3343static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3344 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003345 struct lockdep_map *nest_lock, unsigned long ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003346 int references, int pin_count)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003347{
3348 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07003349 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003350 struct held_lock *hlock;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003351 unsigned int depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003352 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003353 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003354 u64 chain_key;
Byungchul Parkb09be672017-08-07 16:12:52 +09003355 int ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003356
3357 if (unlikely(!debug_locks))
3358 return 0;
3359
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003360 /*
3361 * Lockdep should run with IRQs disabled, otherwise we could
3362 * get an interrupt which would want to take locks, which would
3363 * end up in lockdep and have you got a head-ache already?
3364 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003365 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3366 return 0;
3367
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003368 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3369 check = 0;
Peter Zijlstra1704f472010-03-19 01:37:42 +01003370
Hitoshi Mitake62016252010-10-05 18:01:51 +09003371 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3372 class = lock->class_cache[subclass];
Ingo Molnard6d897c2006-07-10 04:44:04 -07003373 /*
Hitoshi Mitake62016252010-10-05 18:01:51 +09003374 * Not cached?
Ingo Molnard6d897c2006-07-10 04:44:04 -07003375 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003376 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003377 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003378 if (!class)
3379 return 0;
3380 }
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003381 atomic_inc((atomic_t *)&class->ops);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003382 if (very_verbose(class)) {
3383 printk("\nacquire class [%p] %s", class->key, class->name);
3384 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01003385 printk(KERN_CONT "#%d", class->name_version);
3386 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003387 dump_stack();
3388 }
3389
3390 /*
3391 * Add the lock to the list of currently held locks.
3392 * (we dont increase the depth just yet, up until the
3393 * dependency checks are done)
3394 */
3395 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003396 /*
3397 * Ran out of static storage for our per-task lock stack again have we?
3398 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003399 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3400 return 0;
3401
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003402 class_idx = class - lock_classes + 1;
3403
Byungchul Parkb09be672017-08-07 16:12:52 +09003404 /* TODO: nest_lock is not implemented for crosslock yet. */
3405 if (depth && !cross_lock(lock)) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003406 hlock = curr->held_locks + depth - 1;
3407 if (hlock->class_idx == class_idx && nest_lock) {
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003408 if (hlock->references) {
3409 /*
3410 * Check: unsigned int references:12, overflow.
3411 */
3412 if (DEBUG_LOCKS_WARN_ON(hlock->references == (1 << 12)-1))
3413 return 0;
3414
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003415 hlock->references++;
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003416 } else {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003417 hlock->references = 2;
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003418 }
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003419
3420 return 1;
3421 }
3422 }
3423
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003424 hlock = curr->held_locks + depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003425 /*
3426 * Plain impossible, we just registered it and checked it weren't no
3427 * NULL like.. I bet this mushroom I ate was good!
3428 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003429 if (DEBUG_LOCKS_WARN_ON(!class))
3430 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003431 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003432 hlock->acquire_ip = ip;
3433 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003434 hlock->nest_lock = nest_lock;
Boqun Fengc2469752016-02-16 13:57:40 +08003435 hlock->irq_context = task_irq_context(curr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003436 hlock->trylock = trylock;
3437 hlock->read = read;
3438 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04003439 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003440 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003441#ifdef CONFIG_LOCK_STAT
3442 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003443 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003444#endif
Peter Zijlstra21199f22015-09-16 16:10:40 +02003445 hlock->pin_count = pin_count;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003446
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003447 if (check && !mark_irqflags(curr, hlock))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003448 return 0;
3449
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003450 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07003451 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003452 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003453
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003454 /*
Gautham R Shenoy17aacfb2007-10-28 20:47:01 +01003455 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003456 * lock keys along the dependency chain. We save the hash value
3457 * at every step so that we can get the current hash easily
3458 * after unlock. The chain hash is then used to cache dependency
3459 * results.
3460 *
3461 * The 'key ID' is what is the most compact key value to drive
3462 * the hash, not class->key.
3463 */
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003464 /*
3465 * Whoops, we did it again.. ran straight out of our static allocation.
3466 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003467 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003468 return 0;
3469
3470 chain_key = curr->curr_chain_key;
3471 if (!depth) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003472 /*
3473 * How can we have a chain hash when we ain't got no keys?!
3474 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003475 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3476 return 0;
3477 chain_head = 1;
3478 }
3479
3480 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003481 if (separate_irq_context(curr, hlock)) {
3482 chain_key = 0;
3483 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003484 }
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003485 chain_key = iterate_chain_key(chain_key, class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003486
Peter Zijlstraf8319482016-11-30 14:32:25 +11003487 if (nest_lock && !__lock_is_held(nest_lock, -1))
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003488 return print_lock_nested_lock_not_held(curr, hlock, ip);
3489
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003490 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003491 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08003492
Byungchul Parkb09be672017-08-07 16:12:52 +09003493 ret = lock_acquire_crosslock(hlock);
3494 /*
3495 * 2 means normal acquire operations are needed. Otherwise, it's
3496 * ok just to return with '0:fail, 1:success'.
3497 */
3498 if (ret != 2)
3499 return ret;
3500
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003501 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003502 curr->lockdep_depth++;
3503 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08003504#ifdef CONFIG_DEBUG_LOCKDEP
3505 if (unlikely(!debug_locks))
3506 return 0;
3507#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003508 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3509 debug_locks_off();
Dave Jones2c522832013-04-25 13:40:02 -04003510 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3511 printk(KERN_DEBUG "depth: %i max: %lu!\n",
Ben Greearc0540602013-02-06 10:56:19 -08003512 curr->lockdep_depth, MAX_LOCK_DEPTH);
Ben Greearc0540602013-02-06 10:56:19 -08003513
3514 lockdep_print_held_locks(current);
3515 debug_show_all_locks();
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01003516 dump_stack();
Ben Greearc0540602013-02-06 10:56:19 -08003517
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003518 return 0;
3519 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08003520
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003521 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3522 max_lockdep_depth = curr->lockdep_depth;
3523
3524 return 1;
3525}
3526
3527static int
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303528print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003529 unsigned long ip)
3530{
3531 if (!debug_locks_off())
3532 return 0;
3533 if (debug_locks_silent)
3534 return 0;
3535
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003536 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003537 pr_warn("=====================================\n");
3538 pr_warn("WARNING: bad unlock balance detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003539 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003540 pr_warn("-------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003541 pr_warn("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003542 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003543 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003544 pr_cont(") at:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003545 print_ip_sym(ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003546 pr_warn("but there are no more locks to release!\n");
3547 pr_warn("\nother info that might help us debug this:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003548 lockdep_print_held_locks(curr);
3549
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003550 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003551 dump_stack();
3552
3553 return 0;
3554}
3555
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003556static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3557{
3558 if (hlock->instance == lock)
3559 return 1;
3560
3561 if (hlock->references) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09003562 struct lock_class *class = lock->class_cache[0];
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003563
3564 if (!class)
3565 class = look_up_lock_class(lock, 0);
3566
Peter Zijlstra80e04012011-08-05 14:26:17 +02003567 /*
3568 * If look_up_lock_class() failed to find a class, we're trying
3569 * to test if we hold a lock that has never yet been acquired.
3570 * Clearly if the lock hasn't been acquired _ever_, we're not
3571 * holding it either, so report failure.
3572 */
Thomas Gleixner383776f2017-02-27 15:37:36 +01003573 if (IS_ERR_OR_NULL(class))
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003574 return 0;
3575
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003576 /*
3577 * References, but not a lock we're actually ref-counting?
3578 * State got messed up, follow the sites that change ->references
3579 * and try to make sense of it.
3580 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003581 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3582 return 0;
3583
3584 if (hlock->class_idx == class - lock_classes + 1)
3585 return 1;
3586 }
3587
3588 return 0;
3589}
3590
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003591/* @depth must not be zero */
3592static struct held_lock *find_held_lock(struct task_struct *curr,
3593 struct lockdep_map *lock,
3594 unsigned int depth, int *idx)
3595{
3596 struct held_lock *ret, *hlock, *prev_hlock;
3597 int i;
3598
3599 i = depth - 1;
3600 hlock = curr->held_locks + i;
3601 ret = hlock;
3602 if (match_held_lock(hlock, lock))
3603 goto out;
3604
3605 ret = NULL;
3606 for (i--, prev_hlock = hlock--;
3607 i >= 0;
3608 i--, prev_hlock = hlock--) {
3609 /*
3610 * We must not cross into another context:
3611 */
3612 if (prev_hlock->irq_context != hlock->irq_context) {
3613 ret = NULL;
3614 break;
3615 }
3616 if (match_held_lock(hlock, lock)) {
3617 ret = hlock;
3618 break;
3619 }
3620 }
3621
3622out:
3623 *idx = i;
3624 return ret;
3625}
3626
J. R. Okajimae9699702017-02-03 01:38:16 +09003627static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
3628 int idx)
3629{
3630 struct held_lock *hlock;
3631
3632 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
3633 if (!__lock_acquire(hlock->instance,
3634 hlock_class(hlock)->subclass,
3635 hlock->trylock,
3636 hlock->read, hlock->check,
3637 hlock->hardirqs_off,
3638 hlock->nest_lock, hlock->acquire_ip,
3639 hlock->references, hlock->pin_count))
3640 return 1;
3641 }
3642 return 0;
3643}
3644
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003645static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003646__lock_set_class(struct lockdep_map *lock, const char *name,
3647 struct lock_class_key *key, unsigned int subclass,
3648 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003649{
3650 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003651 struct held_lock *hlock;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003652 struct lock_class *class;
3653 unsigned int depth;
3654 int i;
3655
3656 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003657 /*
3658 * This function is about (re)setting the class of a held lock,
3659 * yet we're not actually holding any locks. Naughty user!
3660 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003661 if (DEBUG_LOCKS_WARN_ON(!depth))
3662 return 0;
3663
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003664 hlock = find_held_lock(curr, lock, depth, &i);
3665 if (!hlock)
3666 return print_unlock_imbalance_bug(curr, lock, ip);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003667
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003668 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003669 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02003670 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003671
3672 curr->lockdep_depth = i;
3673 curr->curr_chain_key = hlock->prev_chain_key;
3674
J. R. Okajimae9699702017-02-03 01:38:16 +09003675 if (reacquire_held_locks(curr, depth, i))
3676 return 0;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003677
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003678 /*
3679 * I took it apart and put it back together again, except now I have
3680 * these 'spare' parts.. where shall I put them.
3681 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003682 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3683 return 0;
3684 return 1;
3685}
3686
J. R. Okajima6419c4a2017-02-03 01:38:17 +09003687static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3688{
3689 struct task_struct *curr = current;
3690 struct held_lock *hlock;
3691 unsigned int depth;
3692 int i;
3693
3694 depth = curr->lockdep_depth;
3695 /*
3696 * This function is about (re)setting the class of a held lock,
3697 * yet we're not actually holding any locks. Naughty user!
3698 */
3699 if (DEBUG_LOCKS_WARN_ON(!depth))
3700 return 0;
3701
3702 hlock = find_held_lock(curr, lock, depth, &i);
3703 if (!hlock)
3704 return print_unlock_imbalance_bug(curr, lock, ip);
3705
3706 curr->lockdep_depth = i;
3707 curr->curr_chain_key = hlock->prev_chain_key;
3708
3709 WARN(hlock->read, "downgrading a read lock");
3710 hlock->read = 1;
3711 hlock->acquire_ip = ip;
3712
3713 if (reacquire_held_locks(curr, depth, i))
3714 return 0;
3715
3716 /*
3717 * I took it apart and put it back together again, except now I have
3718 * these 'spare' parts.. where shall I put them.
3719 */
3720 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3721 return 0;
3722 return 1;
3723}
3724
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003725/*
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003726 * Remove the lock to the list of currently held locks - this gets
3727 * called on mutex_unlock()/spin_unlock*() (or on a failed
3728 * mutex_lock_interruptible()).
3729 *
3730 * @nested is an hysterical artifact, needs a tree wide cleanup.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003731 */
3732static int
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003733__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003734{
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003735 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003736 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003737 unsigned int depth;
Byungchul Parkb09be672017-08-07 16:12:52 +09003738 int ret, i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003739
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003740 if (unlikely(!debug_locks))
3741 return 0;
3742
Byungchul Parkb09be672017-08-07 16:12:52 +09003743 ret = lock_release_crosslock(lock);
3744 /*
3745 * 2 means normal release operations are needed. Otherwise, it's
3746 * ok just to return with '0:fail, 1:success'.
3747 */
3748 if (ret != 2)
3749 return ret;
3750
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003751 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003752 /*
3753 * So we're all set to release this lock.. wait what lock? We don't
3754 * own any locks, you've been drinking again?
3755 */
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003756 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3757 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003758
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003759 /*
3760 * Check whether the lock exists in the current stack
3761 * of held locks:
3762 */
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003763 hlock = find_held_lock(curr, lock, depth, &i);
3764 if (!hlock)
3765 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003766
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003767 if (hlock->instance == lock)
3768 lock_release_holdtime(hlock);
3769
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003770 WARN(hlock->pin_count, "releasing a pinned lock\n");
3771
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003772 if (hlock->references) {
3773 hlock->references--;
3774 if (hlock->references) {
3775 /*
3776 * We had, and after removing one, still have
3777 * references, the current lock stack is still
3778 * valid. We're done!
3779 */
3780 return 1;
3781 }
3782 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003783
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003784 /*
3785 * We have the right lock to unlock, 'hlock' points to it.
3786 * Now we remove it from the stack, and add back the other
3787 * entries (if any), recalculating the hash along the way:
3788 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003789
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003790 curr->lockdep_depth = i;
3791 curr->curr_chain_key = hlock->prev_chain_key;
3792
J. R. Okajimae9699702017-02-03 01:38:16 +09003793 if (reacquire_held_locks(curr, depth, i + 1))
3794 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003795
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003796 /*
3797 * We had N bottles of beer on the wall, we drank one, but now
3798 * there's not N-1 bottles of beer left on the wall...
3799 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003800 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3801 return 0;
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003802
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003803 return 1;
3804}
3805
Peter Zijlstraf8319482016-11-30 14:32:25 +11003806static int __lock_is_held(struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02003807{
3808 struct task_struct *curr = current;
3809 int i;
3810
3811 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003812 struct held_lock *hlock = curr->held_locks + i;
3813
Peter Zijlstraf8319482016-11-30 14:32:25 +11003814 if (match_held_lock(hlock, lock)) {
3815 if (read == -1 || hlock->read == read)
3816 return 1;
3817
3818 return 0;
3819 }
Peter Zijlstraf607c662009-07-20 19:16:29 +02003820 }
3821
3822 return 0;
3823}
3824
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003825static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
3826{
3827 struct pin_cookie cookie = NIL_COOKIE;
3828 struct task_struct *curr = current;
3829 int i;
3830
3831 if (unlikely(!debug_locks))
3832 return cookie;
3833
3834 for (i = 0; i < curr->lockdep_depth; i++) {
3835 struct held_lock *hlock = curr->held_locks + i;
3836
3837 if (match_held_lock(hlock, lock)) {
3838 /*
3839 * Grab 16bits of randomness; this is sufficient to not
3840 * be guessable and still allows some pin nesting in
3841 * our u32 pin_count.
3842 */
3843 cookie.val = 1 + (prandom_u32() >> 16);
3844 hlock->pin_count += cookie.val;
3845 return cookie;
3846 }
3847 }
3848
3849 WARN(1, "pinning an unheld lock\n");
3850 return cookie;
3851}
3852
3853static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003854{
3855 struct task_struct *curr = current;
3856 int i;
3857
3858 if (unlikely(!debug_locks))
3859 return;
3860
3861 for (i = 0; i < curr->lockdep_depth; i++) {
3862 struct held_lock *hlock = curr->held_locks + i;
3863
3864 if (match_held_lock(hlock, lock)) {
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003865 hlock->pin_count += cookie.val;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003866 return;
3867 }
3868 }
3869
3870 WARN(1, "pinning an unheld lock\n");
3871}
3872
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003873static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003874{
3875 struct task_struct *curr = current;
3876 int i;
3877
3878 if (unlikely(!debug_locks))
3879 return;
3880
3881 for (i = 0; i < curr->lockdep_depth; i++) {
3882 struct held_lock *hlock = curr->held_locks + i;
3883
3884 if (match_held_lock(hlock, lock)) {
3885 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
3886 return;
3887
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003888 hlock->pin_count -= cookie.val;
3889
3890 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
3891 hlock->pin_count = 0;
3892
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003893 return;
3894 }
3895 }
3896
3897 WARN(1, "unpinning an unheld lock\n");
3898}
3899
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003900/*
3901 * Check whether we follow the irq-flags state precisely:
3902 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003903static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003904{
Ingo Molnar992860e2008-07-14 10:28:38 +02003905#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3906 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003907 if (!debug_locks)
3908 return;
3909
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01003910 if (irqs_disabled_flags(flags)) {
3911 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3912 printk("possible reason: unannotated irqs-off.\n");
3913 }
3914 } else {
3915 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3916 printk("possible reason: unannotated irqs-on.\n");
3917 }
3918 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003919
3920 /*
3921 * We dont accurately track softirq state in e.g.
3922 * hardirq contexts (such as on 4KSTACKS), so only
3923 * check if not in hardirq contexts:
3924 */
3925 if (!hardirq_count()) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003926 if (softirq_count()) {
3927 /* like the above, but with softirqs */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003928 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003929 } else {
3930 /* lick the above, does it taste good? */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003931 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003932 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003933 }
3934
3935 if (!debug_locks)
3936 print_irqtrace_events(current);
3937#endif
3938}
3939
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003940void lock_set_class(struct lockdep_map *lock, const char *name,
3941 struct lock_class_key *key, unsigned int subclass,
3942 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003943{
3944 unsigned long flags;
3945
3946 if (unlikely(current->lockdep_recursion))
3947 return;
3948
3949 raw_local_irq_save(flags);
3950 current->lockdep_recursion = 1;
3951 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003952 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003953 check_chain_key(current);
3954 current->lockdep_recursion = 0;
3955 raw_local_irq_restore(flags);
3956}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003957EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003958
J. R. Okajima6419c4a2017-02-03 01:38:17 +09003959void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3960{
3961 unsigned long flags;
3962
3963 if (unlikely(current->lockdep_recursion))
3964 return;
3965
3966 raw_local_irq_save(flags);
3967 current->lockdep_recursion = 1;
3968 check_flags(flags);
3969 if (__lock_downgrade(lock, ip))
3970 check_chain_key(current);
3971 current->lockdep_recursion = 0;
3972 raw_local_irq_restore(flags);
3973}
3974EXPORT_SYMBOL_GPL(lock_downgrade);
3975
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003976/*
3977 * We are not always called with irqs disabled - do that here,
3978 * and also avoid lockdep recursion:
3979 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003980void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003981 int trylock, int read, int check,
3982 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003983{
3984 unsigned long flags;
3985
3986 if (unlikely(current->lockdep_recursion))
3987 return;
3988
3989 raw_local_irq_save(flags);
3990 check_flags(flags);
3991
3992 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003993 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003994 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003995 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003996 current->lockdep_recursion = 0;
3997 raw_local_irq_restore(flags);
3998}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003999EXPORT_SYMBOL_GPL(lock_acquire);
4000
Steven Rostedt1d09daa2008-05-12 21:20:55 +02004001void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02004002 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004003{
4004 unsigned long flags;
4005
4006 if (unlikely(current->lockdep_recursion))
4007 return;
4008
4009 raw_local_irq_save(flags);
4010 check_flags(flags);
4011 current->lockdep_recursion = 1;
Frederic Weisbecker93135432010-05-08 06:24:25 +02004012 trace_lock_release(lock, ip);
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02004013 if (__lock_release(lock, nested, ip))
4014 check_chain_key(current);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004015 current->lockdep_recursion = 0;
4016 raw_local_irq_restore(flags);
4017}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004018EXPORT_SYMBOL_GPL(lock_release);
4019
Peter Zijlstraf8319482016-11-30 14:32:25 +11004020int lock_is_held_type(struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02004021{
4022 unsigned long flags;
4023 int ret = 0;
4024
4025 if (unlikely(current->lockdep_recursion))
Peter Zijlstraf2513cd2011-06-06 12:32:43 +02004026 return 1; /* avoid false negative lockdep_assert_held() */
Peter Zijlstraf607c662009-07-20 19:16:29 +02004027
4028 raw_local_irq_save(flags);
4029 check_flags(flags);
4030
4031 current->lockdep_recursion = 1;
Peter Zijlstraf8319482016-11-30 14:32:25 +11004032 ret = __lock_is_held(lock, read);
Peter Zijlstraf607c662009-07-20 19:16:29 +02004033 current->lockdep_recursion = 0;
4034 raw_local_irq_restore(flags);
4035
4036 return ret;
4037}
Peter Zijlstraf8319482016-11-30 14:32:25 +11004038EXPORT_SYMBOL_GPL(lock_is_held_type);
Peter Zijlstraf607c662009-07-20 19:16:29 +02004039
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004040struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004041{
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004042 struct pin_cookie cookie = NIL_COOKIE;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004043 unsigned long flags;
4044
4045 if (unlikely(current->lockdep_recursion))
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004046 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004047
4048 raw_local_irq_save(flags);
4049 check_flags(flags);
4050
4051 current->lockdep_recursion = 1;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004052 cookie = __lock_pin_lock(lock);
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004053 current->lockdep_recursion = 0;
4054 raw_local_irq_restore(flags);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004055
4056 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004057}
4058EXPORT_SYMBOL_GPL(lock_pin_lock);
4059
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004060void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004061{
4062 unsigned long flags;
4063
4064 if (unlikely(current->lockdep_recursion))
4065 return;
4066
4067 raw_local_irq_save(flags);
4068 check_flags(flags);
4069
4070 current->lockdep_recursion = 1;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004071 __lock_repin_lock(lock, cookie);
4072 current->lockdep_recursion = 0;
4073 raw_local_irq_restore(flags);
4074}
4075EXPORT_SYMBOL_GPL(lock_repin_lock);
4076
4077void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
4078{
4079 unsigned long flags;
4080
4081 if (unlikely(current->lockdep_recursion))
4082 return;
4083
4084 raw_local_irq_save(flags);
4085 check_flags(flags);
4086
4087 current->lockdep_recursion = 1;
4088 __lock_unpin_lock(lock, cookie);
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004089 current->lockdep_recursion = 0;
4090 raw_local_irq_restore(flags);
4091}
4092EXPORT_SYMBOL_GPL(lock_unpin_lock);
4093
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004094#ifdef CONFIG_LOCK_STAT
4095static int
4096print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
4097 unsigned long ip)
4098{
4099 if (!debug_locks_off())
4100 return 0;
4101 if (debug_locks_silent)
4102 return 0;
4103
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004104 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004105 pr_warn("=================================\n");
4106 pr_warn("WARNING: bad contention detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004107 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004108 pr_warn("---------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004109 pr_warn("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004110 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004111 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004112 pr_cont(") at:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004113 print_ip_sym(ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004114 pr_warn("but there are no locks held!\n");
4115 pr_warn("\nother info that might help us debug this:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004116 lockdep_print_held_locks(curr);
4117
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004118 pr_warn("\nstack backtrace:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004119 dump_stack();
4120
4121 return 0;
4122}
4123
4124static void
4125__lock_contended(struct lockdep_map *lock, unsigned long ip)
4126{
4127 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004128 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004129 struct lock_class_stats *stats;
4130 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004131 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004132
4133 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004134 /*
4135 * Whee, we contended on this lock, except it seems we're not
4136 * actually trying to acquire anything much at all..
4137 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004138 if (DEBUG_LOCKS_WARN_ON(!depth))
4139 return;
4140
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004141 hlock = find_held_lock(curr, lock, depth, &i);
4142 if (!hlock) {
4143 print_lock_contention_bug(curr, lock, ip);
4144 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004145 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004146
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004147 if (hlock->instance != lock)
4148 return;
4149
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004150 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004151
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004152 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
4153 contending_point = lock_point(hlock_class(hlock)->contending_point,
4154 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004155
Dave Jonesf82b2172008-08-11 09:30:23 +02004156 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004157 if (contention_point < LOCKSTAT_POINTS)
4158 stats->contention_point[contention_point]++;
4159 if (contending_point < LOCKSTAT_POINTS)
4160 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07004161 if (lock->cpu != smp_processor_id())
4162 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004163 put_lock_stats(stats);
4164}
4165
4166static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004167__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004168{
4169 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004170 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004171 struct lock_class_stats *stats;
4172 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004173 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07004174 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004175
4176 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004177 /*
4178 * Yay, we acquired ownership of this lock we didn't try to
4179 * acquire, how the heck did that happen?
4180 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004181 if (DEBUG_LOCKS_WARN_ON(!depth))
4182 return;
4183
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004184 hlock = find_held_lock(curr, lock, depth, &i);
4185 if (!hlock) {
4186 print_lock_contention_bug(curr, lock, _RET_IP_);
4187 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004188 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004189
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004190 if (hlock->instance != lock)
4191 return;
4192
Peter Zijlstra96645672007-07-19 01:49:00 -07004193 cpu = smp_processor_id();
4194 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004195 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07004196 waittime = now - hlock->waittime_stamp;
4197 hlock->holdtime_stamp = now;
4198 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004199
Frederic Weisbecker883a2a32010-05-08 06:16:11 +02004200 trace_lock_acquired(lock, ip);
Frederic Weisbecker20625012009-04-06 01:49:33 +02004201
Dave Jonesf82b2172008-08-11 09:30:23 +02004202 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07004203 if (waittime) {
4204 if (hlock->read)
4205 lock_time_inc(&stats->read_waittime, waittime);
4206 else
4207 lock_time_inc(&stats->write_waittime, waittime);
4208 }
4209 if (lock->cpu != cpu)
4210 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004211 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07004212
4213 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004214 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004215}
4216
4217void lock_contended(struct lockdep_map *lock, unsigned long ip)
4218{
4219 unsigned long flags;
4220
4221 if (unlikely(!lock_stat))
4222 return;
4223
4224 if (unlikely(current->lockdep_recursion))
4225 return;
4226
4227 raw_local_irq_save(flags);
4228 check_flags(flags);
4229 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01004230 trace_lock_contended(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004231 __lock_contended(lock, ip);
4232 current->lockdep_recursion = 0;
4233 raw_local_irq_restore(flags);
4234}
4235EXPORT_SYMBOL_GPL(lock_contended);
4236
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004237void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004238{
4239 unsigned long flags;
4240
4241 if (unlikely(!lock_stat))
4242 return;
4243
4244 if (unlikely(current->lockdep_recursion))
4245 return;
4246
4247 raw_local_irq_save(flags);
4248 check_flags(flags);
4249 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004250 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004251 current->lockdep_recursion = 0;
4252 raw_local_irq_restore(flags);
4253}
4254EXPORT_SYMBOL_GPL(lock_acquired);
4255#endif
4256
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004257/*
4258 * Used by the testsuite, sanitize the validator state
4259 * after a simulated failure:
4260 */
4261
4262void lockdep_reset(void)
4263{
4264 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08004265 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004266
4267 raw_local_irq_save(flags);
4268 current->curr_chain_key = 0;
4269 current->lockdep_depth = 0;
4270 current->lockdep_recursion = 0;
4271 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
4272 nr_hardirq_chains = 0;
4273 nr_softirq_chains = 0;
4274 nr_process_chains = 0;
4275 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08004276 for (i = 0; i < CHAINHASH_SIZE; i++)
Andrew Mortona63f38c2016-02-03 13:44:12 -08004277 INIT_HLIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004278 raw_local_irq_restore(flags);
4279}
4280
4281static void zap_class(struct lock_class *class)
4282{
4283 int i;
4284
4285 /*
4286 * Remove all dependencies this lock is
4287 * involved in:
4288 */
4289 for (i = 0; i < nr_list_entries; i++) {
4290 if (list_entries[i].class == class)
4291 list_del_rcu(&list_entries[i].entry);
4292 }
4293 /*
4294 * Unhash the class and remove it from the all_lock_classes list:
4295 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08004296 hlist_del_rcu(&class->hash_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004297 list_del_rcu(&class->lock_entry);
4298
Peter Zijlstracee34d82015-06-02 12:50:13 +02004299 RCU_INIT_POINTER(class->key, NULL);
4300 RCU_INIT_POINTER(class->name, NULL);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004301}
4302
Arjan van de Venfabe8742008-01-24 07:00:45 +01004303static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004304{
4305 return addr >= start && addr < start + size;
4306}
4307
Peter Zijlstra35a93932015-02-26 16:23:11 +01004308/*
4309 * Used in module.c to remove lock classes from memory that is going to be
4310 * freed; and possibly re-used by other modules.
4311 *
4312 * We will have had one sync_sched() before getting here, so we're guaranteed
4313 * nobody will look up these exact classes -- they're properly dead but still
4314 * allocated.
4315 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004316void lockdep_free_key_range(void *start, unsigned long size)
4317{
Peter Zijlstra35a93932015-02-26 16:23:11 +01004318 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004319 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004320 unsigned long flags;
4321 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01004322 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004323
4324 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01004325 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004326
4327 /*
4328 * Unhash all classes that were created by this module:
4329 */
4330 for (i = 0; i < CLASSHASH_SIZE; i++) {
4331 head = classhash_table + i;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004332 hlist_for_each_entry_rcu(class, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004333 if (within(class->key, start, size))
4334 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01004335 else if (within(class->name, start, size))
4336 zap_class(class);
4337 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004338 }
4339
Nick Piggin5a26db52008-01-16 09:51:58 +01004340 if (locked)
4341 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004342 raw_local_irq_restore(flags);
Peter Zijlstra35a93932015-02-26 16:23:11 +01004343
4344 /*
4345 * Wait for any possible iterators from look_up_lock_class() to pass
4346 * before continuing to free the memory they refer to.
4347 *
4348 * sync_sched() is sufficient because the read-side is IRQ disable.
4349 */
4350 synchronize_sched();
4351
4352 /*
4353 * XXX at this point we could return the resources to the pool;
4354 * instead we leak them. We would need to change to bitmap allocators
4355 * instead of the linear allocators we have now.
4356 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004357}
4358
4359void lockdep_reset_lock(struct lockdep_map *lock)
4360{
Peter Zijlstra35a93932015-02-26 16:23:11 +01004361 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004362 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004363 unsigned long flags;
4364 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01004365 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004366
4367 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004368
4369 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07004370 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004371 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07004372 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
4373 /*
4374 * If the class exists we look it up and zap it:
4375 */
4376 class = look_up_lock_class(lock, j);
Thomas Gleixner383776f2017-02-27 15:37:36 +01004377 if (!IS_ERR_OR_NULL(class))
Ingo Molnard6d897c2006-07-10 04:44:04 -07004378 zap_class(class);
4379 }
4380 /*
4381 * Debug check: in the end all mapped classes should
4382 * be gone.
4383 */
Nick Piggin5a26db52008-01-16 09:51:58 +01004384 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004385 for (i = 0; i < CLASSHASH_SIZE; i++) {
4386 head = classhash_table + i;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004387 hlist_for_each_entry_rcu(class, head, hash_entry) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09004388 int match = 0;
4389
4390 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
4391 match |= class == lock->class_cache[j];
4392
4393 if (unlikely(match)) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004394 if (debug_locks_off_graph_unlock()) {
4395 /*
4396 * We all just reset everything, how did it match?
4397 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08004398 WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004399 }
Ingo Molnard6d897c2006-07-10 04:44:04 -07004400 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004401 }
4402 }
4403 }
Nick Piggin5a26db52008-01-16 09:51:58 +01004404 if (locked)
4405 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07004406
4407out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004408 raw_local_irq_restore(flags);
4409}
4410
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004411void __init lockdep_info(void)
4412{
4413 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4414
Li Zefanb0788ca2008-11-21 15:57:32 +08004415 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004416 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4417 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08004418 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004419 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4420 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4421 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4422
4423 printk(" memory used by lock dependency info: %lu kB\n",
4424 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
4425 sizeof(struct list_head) * CLASSHASH_SIZE +
4426 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
4427 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei90629202009-08-02 21:43:36 +08004428 sizeof(struct list_head) * CHAINHASH_SIZE
Ming Lei4dd861d2009-07-16 15:44:29 +02004429#ifdef CONFIG_PROVE_LOCKING
Ming Leie351b662009-07-22 22:48:09 +08004430 + sizeof(struct circular_queue)
Ming Lei4dd861d2009-07-16 15:44:29 +02004431#endif
Ming Lei90629202009-08-02 21:43:36 +08004432 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02004433 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004434
4435 printk(" per task-struct memory footprint: %lu bytes\n",
4436 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004437}
4438
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004439static void
4440print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07004441 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004442{
4443 if (!debug_locks_off())
4444 return;
4445 if (debug_locks_silent)
4446 return;
4447
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004448 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004449 pr_warn("=========================\n");
4450 pr_warn("WARNING: held lock freed!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004451 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004452 pr_warn("-------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004453 pr_warn("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004454 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07004455 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004456 lockdep_print_held_locks(curr);
4457
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004458 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004459 dump_stack();
4460}
4461
Oleg Nesterov54561782007-12-05 15:46:09 +01004462static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4463 const void* lock_from, unsigned long lock_len)
4464{
4465 return lock_from + lock_len <= mem_from ||
4466 mem_from + mem_len <= lock_from;
4467}
4468
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004469/*
4470 * Called when kernel memory is freed (or unmapped), or if a lock
4471 * is destroyed or reinitialized - this code checks whether there is
4472 * any held lock in the memory range of <from> to <to>:
4473 */
4474void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4475{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004476 struct task_struct *curr = current;
4477 struct held_lock *hlock;
4478 unsigned long flags;
4479 int i;
4480
4481 if (unlikely(!debug_locks))
4482 return;
4483
4484 local_irq_save(flags);
4485 for (i = 0; i < curr->lockdep_depth; i++) {
4486 hlock = curr->held_locks + i;
4487
Oleg Nesterov54561782007-12-05 15:46:09 +01004488 if (not_in_range(mem_from, mem_len, hlock->instance,
4489 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004490 continue;
4491
Oleg Nesterov54561782007-12-05 15:46:09 +01004492 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004493 break;
4494 }
4495 local_irq_restore(flags);
4496}
Peter Zijlstraed075362006-12-06 20:35:24 -08004497EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004498
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004499static void print_held_locks_bug(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004500{
4501 if (!debug_locks_off())
4502 return;
4503 if (debug_locks_silent)
4504 return;
4505
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004506 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004507 pr_warn("====================================\n");
4508 pr_warn("WARNING: %s/%d still has locks held!\n",
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004509 current->comm, task_pid_nr(current));
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004510 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004511 pr_warn("------------------------------------\n");
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004512 lockdep_print_held_locks(current);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004513 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004514 dump_stack();
4515}
4516
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004517void debug_check_no_locks_held(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004518{
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004519 if (unlikely(current->lockdep_depth > 0))
4520 print_held_locks_bug();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004521}
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004522EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004523
Sasha Levin8dce7a92013-06-13 18:41:16 -04004524#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004525void debug_show_all_locks(void)
4526{
4527 struct task_struct *g, *p;
4528 int count = 10;
4529 int unlock = 1;
4530
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004531 if (unlikely(!debug_locks)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004532 pr_warn("INFO: lockdep is turned off.\n");
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004533 return;
4534 }
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004535 pr_warn("\nShowing all locks held in the system:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004536
4537 /*
4538 * Here we try to get the tasklist_lock as hard as possible,
4539 * if not successful after 2 seconds we ignore it (but keep
4540 * trying). This is to enable a debug printout even if a
4541 * tasklist_lock-holding task deadlocks or crashes.
4542 */
4543retry:
4544 if (!read_trylock(&tasklist_lock)) {
4545 if (count == 10)
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004546 pr_warn("hm, tasklist_lock locked, retrying... ");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004547 if (count) {
4548 count--;
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004549 pr_cont(" #%d", 10-count);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004550 mdelay(200);
4551 goto retry;
4552 }
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004553 pr_cont(" ignoring it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004554 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08004555 } else {
4556 if (count != 10)
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004557 pr_cont(" locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004558 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004559
4560 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01004561 /*
4562 * It's not reliable to print a task's held locks
4563 * if it's not sleeping (or if it's not the current
4564 * task):
4565 */
4566 if (p->state == TASK_RUNNING && p != current)
4567 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004568 if (p->lockdep_depth)
4569 lockdep_print_held_locks(p);
4570 if (!unlock)
4571 if (read_trylock(&tasklist_lock))
4572 unlock = 1;
4573 } while_each_thread(g, p);
4574
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004575 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004576 pr_warn("=============================================\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004577
4578 if (unlock)
4579 read_unlock(&tasklist_lock);
4580}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004581EXPORT_SYMBOL_GPL(debug_show_all_locks);
Sasha Levin8dce7a92013-06-13 18:41:16 -04004582#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004583
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004584/*
4585 * Careful: only use this function if you are sure that
4586 * the task cannot run in parallel!
4587 */
John Kacurf1b499f2010-08-05 17:10:53 +02004588void debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004589{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004590 if (unlikely(!debug_locks)) {
4591 printk("INFO: lockdep is turned off.\n");
4592 return;
4593 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004594 lockdep_print_held_locks(task);
4595}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004596EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02004597
Andi Kleen722a9f92014-05-02 00:44:38 +02004598asmlinkage __visible void lockdep_sys_exit(void)
Peter Zijlstrab351d162007-10-11 22:11:12 +02004599{
4600 struct task_struct *curr = current;
4601
4602 if (unlikely(curr->lockdep_depth)) {
4603 if (!debug_locks_off())
4604 return;
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004605 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004606 pr_warn("================================================\n");
4607 pr_warn("WARNING: lock held when returning to user space!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004608 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004609 pr_warn("------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004610 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
Peter Zijlstrab351d162007-10-11 22:11:12 +02004611 curr->comm, curr->pid);
4612 lockdep_print_held_locks(curr);
4613 }
Byungchul Parkb09be672017-08-07 16:12:52 +09004614
4615 /*
4616 * The lock history for each syscall should be independent. So wipe the
4617 * slate clean on return to userspace.
4618 */
Peter Zijlstraf52be572017-08-29 10:59:39 +02004619 lockdep_invariant_state(false);
Peter Zijlstrab351d162007-10-11 22:11:12 +02004620}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004621
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004622void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004623{
4624 struct task_struct *curr = current;
4625
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004626 /* Note: the following can be executed concurrently, so be careful. */
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004627 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004628 pr_warn("=============================\n");
4629 pr_warn("WARNING: suspicious RCU usage\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004630 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004631 pr_warn("-----------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004632 pr_warn("%s:%d %s!\n", file, line, s);
4633 pr_warn("\nother info that might help us debug this:\n\n");
4634 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004635 !rcu_lockdep_current_cpu_online()
4636 ? "RCU used illegally from offline CPU!\n"
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004637 : !rcu_is_watching()
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004638 ? "RCU used illegally from idle CPU!\n"
4639 : "",
4640 rcu_scheduler_active, debug_locks);
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004641
4642 /*
4643 * If a CPU is in the RCU-free window in idle (ie: in the section
4644 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4645 * considers that CPU to be in an "extended quiescent state",
4646 * which means that RCU will be completely ignoring that CPU.
4647 * Therefore, rcu_read_lock() and friends have absolutely no
4648 * effect on a CPU running in that state. In other words, even if
4649 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4650 * delete data structures out from under it. RCU really has no
4651 * choice here: we need to keep an RCU-free window in idle where
4652 * the CPU may possibly enter into low power mode. This way we can
4653 * notice an extended quiescent state to other CPUs that started a grace
4654 * period. Otherwise we would delay any grace period as long as we run
4655 * in the idle task.
4656 *
4657 * So complain bitterly if someone does call rcu_read_lock(),
4658 * rcu_read_lock_bh() and so on from extended quiescent states.
4659 */
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004660 if (!rcu_is_watching())
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004661 pr_warn("RCU used illegally from extended quiescent state!\n");
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004662
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004663 lockdep_print_held_locks(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004664 pr_warn("\nstack backtrace:\n");
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004665 dump_stack();
4666}
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004667EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);
Byungchul Parkb09be672017-08-07 16:12:52 +09004668
4669#ifdef CONFIG_LOCKDEP_CROSSRELEASE
4670
4671/*
4672 * Crossrelease works by recording a lock history for each thread and
4673 * connecting those historic locks that were taken after the
4674 * wait_for_completion() in the complete() context.
4675 *
4676 * Task-A Task-B
4677 *
4678 * mutex_lock(&A);
4679 * mutex_unlock(&A);
4680 *
4681 * wait_for_completion(&C);
4682 * lock_acquire_crosslock();
4683 * atomic_inc_return(&cross_gen_id);
4684 * |
4685 * | mutex_lock(&B);
4686 * | mutex_unlock(&B);
4687 * |
4688 * | complete(&C);
4689 * `-- lock_commit_crosslock();
4690 *
4691 * Which will then add a dependency between B and C.
4692 */
4693
4694#define xhlock(i) (current->xhlocks[(i) % MAX_XHLOCKS_NR])
4695
4696/*
4697 * Whenever a crosslock is held, cross_gen_id will be increased.
4698 */
4699static atomic_t cross_gen_id; /* Can be wrapped */
4700
4701/*
Byungchul Park23f873d2017-08-07 16:12:53 +09004702 * Make an entry of the ring buffer invalid.
4703 */
4704static inline void invalidate_xhlock(struct hist_lock *xhlock)
4705{
4706 /*
4707 * Normally, xhlock->hlock.instance must be !NULL.
4708 */
4709 xhlock->hlock.instance = NULL;
4710}
4711
4712/*
Peter Zijlstraf52be572017-08-29 10:59:39 +02004713 * Lock history stacks; we have 2 nested lock history stacks:
Byungchul Parkb09be672017-08-07 16:12:52 +09004714 *
Peter Zijlstrae6f3faa2017-08-23 13:23:30 +02004715 * HARD(IRQ)
4716 * SOFT(IRQ)
Byungchul Parkb09be672017-08-07 16:12:52 +09004717 *
Peter Zijlstrae6f3faa2017-08-23 13:23:30 +02004718 * The thing is that once we complete a HARD/SOFT IRQ the future task locks
4719 * should not depend on any of the locks observed while running the IRQ. So
4720 * what we do is rewind the history buffer and erase all our knowledge of that
4721 * temporal event.
Peter Zijlstraf52be572017-08-29 10:59:39 +02004722 */
4723
4724void crossrelease_hist_start(enum xhlock_context_t c)
4725{
4726 struct task_struct *cur = current;
4727
4728 if (!cur->xhlocks)
4729 return;
4730
4731 cur->xhlock_idx_hist[c] = cur->xhlock_idx;
4732 cur->hist_id_save[c] = cur->hist_id;
4733}
4734
4735void crossrelease_hist_end(enum xhlock_context_t c)
4736{
4737 struct task_struct *cur = current;
4738
4739 if (cur->xhlocks) {
4740 unsigned int idx = cur->xhlock_idx_hist[c];
4741 struct hist_lock *h = &xhlock(idx);
4742
4743 cur->xhlock_idx = idx;
4744
4745 /* Check if the ring was overwritten. */
4746 if (h->hist_id != cur->hist_id_save[c])
4747 invalidate_xhlock(h);
4748 }
4749}
4750
4751/*
4752 * lockdep_invariant_state() is used to annotate independence inside a task, to
4753 * make one task look like multiple independent 'tasks'.
Peter Zijlstrae6f3faa2017-08-23 13:23:30 +02004754 *
4755 * Take for instance workqueues; each work is independent of the last. The
4756 * completion of a future work does not depend on the completion of a past work
4757 * (in general). Therefore we must not carry that (lock) dependency across
4758 * works.
Byungchul Parkb09be672017-08-07 16:12:52 +09004759 *
4760 * This is true for many things; pretty much all kthreads fall into this
Peter Zijlstrae6f3faa2017-08-23 13:23:30 +02004761 * pattern, where they have an invariant state and future completions do not
Byungchul Parkb09be672017-08-07 16:12:52 +09004762 * depend on past completions. Its just that since they all have the 'same'
4763 * form -- the kthread does the same over and over -- it doesn't typically
4764 * matter.
4765 *
4766 * The same is true for system-calls, once a system call is completed (we've
4767 * returned to userspace) the next system call does not depend on the lock
4768 * history of the previous system call.
Peter Zijlstrae6f3faa2017-08-23 13:23:30 +02004769 *
4770 * They key property for independence, this invariant state, is that it must be
4771 * a point where we hold no locks and have no history. Because if we were to
4772 * hold locks, the restore at _end() would not necessarily recover it's history
4773 * entry. Similarly, independence per-definition means it does not depend on
4774 * prior state.
Byungchul Parkb09be672017-08-07 16:12:52 +09004775 */
Peter Zijlstraf52be572017-08-29 10:59:39 +02004776void lockdep_invariant_state(bool force)
Byungchul Parkb09be672017-08-07 16:12:52 +09004777{
Peter Zijlstrae6f3faa2017-08-23 13:23:30 +02004778 /*
4779 * We call this at an invariant point, no current state, no history.
Peter Zijlstraf52be572017-08-29 10:59:39 +02004780 * Verify the former, enforce the latter.
Peter Zijlstrae6f3faa2017-08-23 13:23:30 +02004781 */
Peter Zijlstraf52be572017-08-29 10:59:39 +02004782 WARN_ON_ONCE(!force && current->lockdep_depth);
4783 invalidate_xhlock(&xhlock(current->xhlock_idx));
Byungchul Parkb09be672017-08-07 16:12:52 +09004784}
4785
4786static int cross_lock(struct lockdep_map *lock)
4787{
4788 return lock ? lock->cross : 0;
4789}
4790
4791/*
4792 * This is needed to decide the relationship between wrapable variables.
4793 */
4794static inline int before(unsigned int a, unsigned int b)
4795{
4796 return (int)(a - b) < 0;
4797}
4798
4799static inline struct lock_class *xhlock_class(struct hist_lock *xhlock)
4800{
4801 return hlock_class(&xhlock->hlock);
4802}
4803
4804static inline struct lock_class *xlock_class(struct cross_lock *xlock)
4805{
4806 return hlock_class(&xlock->hlock);
4807}
4808
4809/*
4810 * Should we check a dependency with previous one?
4811 */
4812static inline int depend_before(struct held_lock *hlock)
4813{
4814 return hlock->read != 2 && hlock->check && !hlock->trylock;
4815}
4816
4817/*
4818 * Should we check a dependency with next one?
4819 */
4820static inline int depend_after(struct held_lock *hlock)
4821{
4822 return hlock->read != 2 && hlock->check;
4823}
4824
4825/*
4826 * Check if the xhlock is valid, which would be false if,
4827 *
4828 * 1. Has not used after initializaion yet.
Byungchul Park23f873d2017-08-07 16:12:53 +09004829 * 2. Got invalidated.
Byungchul Parkb09be672017-08-07 16:12:52 +09004830 *
4831 * Remind hist_lock is implemented as a ring buffer.
4832 */
4833static inline int xhlock_valid(struct hist_lock *xhlock)
4834{
4835 /*
4836 * xhlock->hlock.instance must be !NULL.
4837 */
4838 return !!xhlock->hlock.instance;
4839}
4840
4841/*
4842 * Record a hist_lock entry.
4843 *
4844 * Irq disable is only required.
4845 */
4846static void add_xhlock(struct held_lock *hlock)
4847{
4848 unsigned int idx = ++current->xhlock_idx;
4849 struct hist_lock *xhlock = &xhlock(idx);
4850
4851#ifdef CONFIG_DEBUG_LOCKDEP
4852 /*
4853 * This can be done locklessly because they are all task-local
4854 * state, we must however ensure IRQs are disabled.
4855 */
4856 WARN_ON_ONCE(!irqs_disabled());
4857#endif
4858
4859 /* Initialize hist_lock's members */
4860 xhlock->hlock = *hlock;
Byungchul Park907dc162017-08-14 16:00:52 +09004861 xhlock->hist_id = ++current->hist_id;
Byungchul Parkb09be672017-08-07 16:12:52 +09004862
4863 xhlock->trace.nr_entries = 0;
4864 xhlock->trace.max_entries = MAX_XHLOCK_TRACE_ENTRIES;
4865 xhlock->trace.entries = xhlock->trace_entries;
4866 xhlock->trace.skip = 3;
4867 save_stack_trace(&xhlock->trace);
4868}
4869
4870static inline int same_context_xhlock(struct hist_lock *xhlock)
4871{
4872 return xhlock->hlock.irq_context == task_irq_context(current);
4873}
4874
4875/*
4876 * This should be lockless as far as possible because this would be
4877 * called very frequently.
4878 */
4879static void check_add_xhlock(struct held_lock *hlock)
4880{
4881 /*
4882 * Record a hist_lock, only in case that acquisitions ahead
4883 * could depend on the held_lock. For example, if the held_lock
4884 * is trylock then acquisitions ahead never depends on that.
4885 * In that case, we don't need to record it. Just return.
4886 */
4887 if (!current->xhlocks || !depend_before(hlock))
4888 return;
4889
4890 add_xhlock(hlock);
4891}
4892
4893/*
4894 * For crosslock.
4895 */
4896static int add_xlock(struct held_lock *hlock)
4897{
4898 struct cross_lock *xlock;
4899 unsigned int gen_id;
4900
4901 if (!graph_lock())
4902 return 0;
4903
4904 xlock = &((struct lockdep_map_cross *)hlock->instance)->xlock;
4905
Byungchul Park28a903f2017-08-07 16:12:54 +09004906 /*
4907 * When acquisitions for a crosslock are overlapped, we use
4908 * nr_acquire to perform commit for them, based on cross_gen_id
4909 * of the first acquisition, which allows to add additional
4910 * dependencies.
4911 *
4912 * Moreover, when no acquisition of a crosslock is in progress,
4913 * we should not perform commit because the lock might not exist
4914 * any more, which might cause incorrect memory access. So we
4915 * have to track the number of acquisitions of a crosslock.
4916 *
4917 * depend_after() is necessary to initialize only the first
4918 * valid xlock so that the xlock can be used on its commit.
4919 */
4920 if (xlock->nr_acquire++ && depend_after(&xlock->hlock))
4921 goto unlock;
4922
Byungchul Parkb09be672017-08-07 16:12:52 +09004923 gen_id = (unsigned int)atomic_inc_return(&cross_gen_id);
4924 xlock->hlock = *hlock;
4925 xlock->hlock.gen_id = gen_id;
Byungchul Park28a903f2017-08-07 16:12:54 +09004926unlock:
Byungchul Parkb09be672017-08-07 16:12:52 +09004927 graph_unlock();
Byungchul Parkb09be672017-08-07 16:12:52 +09004928 return 1;
4929}
4930
4931/*
4932 * Called for both normal and crosslock acquires. Normal locks will be
4933 * pushed on the hist_lock queue. Cross locks will record state and
4934 * stop regular lock_acquire() to avoid being placed on the held_lock
4935 * stack.
4936 *
4937 * Return: 0 - failure;
4938 * 1 - crosslock, done;
4939 * 2 - normal lock, continue to held_lock[] ops.
4940 */
4941static int lock_acquire_crosslock(struct held_lock *hlock)
4942{
4943 /*
4944 * CONTEXT 1 CONTEXT 2
4945 * --------- ---------
4946 * lock A (cross)
4947 * X = atomic_inc_return(&cross_gen_id)
4948 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4949 * Y = atomic_read_acquire(&cross_gen_id)
4950 * lock B
4951 *
4952 * atomic_read_acquire() is for ordering between A and B,
4953 * IOW, A happens before B, when CONTEXT 2 see Y >= X.
4954 *
4955 * Pairs with atomic_inc_return() in add_xlock().
4956 */
4957 hlock->gen_id = (unsigned int)atomic_read_acquire(&cross_gen_id);
4958
4959 if (cross_lock(hlock->instance))
4960 return add_xlock(hlock);
4961
4962 check_add_xhlock(hlock);
4963 return 2;
4964}
4965
4966static int copy_trace(struct stack_trace *trace)
4967{
4968 unsigned long *buf = stack_trace + nr_stack_trace_entries;
4969 unsigned int max_nr = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
4970 unsigned int nr = min(max_nr, trace->nr_entries);
4971
4972 trace->nr_entries = nr;
4973 memcpy(buf, trace->entries, nr * sizeof(trace->entries[0]));
4974 trace->entries = buf;
4975 nr_stack_trace_entries += nr;
4976
4977 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
4978 if (!debug_locks_off_graph_unlock())
4979 return 0;
4980
4981 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
4982 dump_stack();
4983
4984 return 0;
4985 }
4986
4987 return 1;
4988}
4989
4990static int commit_xhlock(struct cross_lock *xlock, struct hist_lock *xhlock)
4991{
4992 unsigned int xid, pid;
4993 u64 chain_key;
4994
4995 xid = xlock_class(xlock) - lock_classes;
4996 chain_key = iterate_chain_key((u64)0, xid);
4997 pid = xhlock_class(xhlock) - lock_classes;
4998 chain_key = iterate_chain_key(chain_key, pid);
4999
5000 if (lookup_chain_cache(chain_key))
5001 return 1;
5002
5003 if (!add_chain_cache_classes(xid, pid, xhlock->hlock.irq_context,
5004 chain_key))
5005 return 0;
5006
5007 if (!check_prev_add(current, &xlock->hlock, &xhlock->hlock, 1,
5008 &xhlock->trace, copy_trace))
5009 return 0;
5010
5011 return 1;
5012}
5013
5014static void commit_xhlocks(struct cross_lock *xlock)
5015{
5016 unsigned int cur = current->xhlock_idx;
Byungchul Park23f873d2017-08-07 16:12:53 +09005017 unsigned int prev_hist_id = xhlock(cur).hist_id;
Byungchul Parkb09be672017-08-07 16:12:52 +09005018 unsigned int i;
5019
5020 if (!graph_lock())
5021 return;
5022
Byungchul Park28a903f2017-08-07 16:12:54 +09005023 if (xlock->nr_acquire) {
5024 for (i = 0; i < MAX_XHLOCKS_NR; i++) {
5025 struct hist_lock *xhlock = &xhlock(cur - i);
Byungchul Parkb09be672017-08-07 16:12:52 +09005026
Byungchul Park28a903f2017-08-07 16:12:54 +09005027 if (!xhlock_valid(xhlock))
5028 break;
Byungchul Parkb09be672017-08-07 16:12:52 +09005029
Byungchul Park28a903f2017-08-07 16:12:54 +09005030 if (before(xhlock->hlock.gen_id, xlock->hlock.gen_id))
5031 break;
Byungchul Parkb09be672017-08-07 16:12:52 +09005032
Byungchul Park28a903f2017-08-07 16:12:54 +09005033 if (!same_context_xhlock(xhlock))
5034 break;
Byungchul Parkb09be672017-08-07 16:12:52 +09005035
Byungchul Park28a903f2017-08-07 16:12:54 +09005036 /*
Byungchul Park907dc162017-08-14 16:00:52 +09005037 * Filter out the cases where the ring buffer was
5038 * overwritten and the current entry has a bigger
5039 * hist_id than the previous one, which is impossible
5040 * otherwise:
Byungchul Park28a903f2017-08-07 16:12:54 +09005041 */
Byungchul Park907dc162017-08-14 16:00:52 +09005042 if (unlikely(before(prev_hist_id, xhlock->hist_id)))
Byungchul Park28a903f2017-08-07 16:12:54 +09005043 break;
Byungchul Park23f873d2017-08-07 16:12:53 +09005044
Byungchul Park28a903f2017-08-07 16:12:54 +09005045 prev_hist_id = xhlock->hist_id;
Byungchul Park23f873d2017-08-07 16:12:53 +09005046
Byungchul Park28a903f2017-08-07 16:12:54 +09005047 /*
5048 * commit_xhlock() returns 0 with graph_lock already
5049 * released if fail.
5050 */
5051 if (!commit_xhlock(xlock, xhlock))
5052 return;
5053 }
Byungchul Parkb09be672017-08-07 16:12:52 +09005054 }
5055
5056 graph_unlock();
5057}
5058
5059void lock_commit_crosslock(struct lockdep_map *lock)
5060{
5061 struct cross_lock *xlock;
5062 unsigned long flags;
5063
5064 if (unlikely(!debug_locks || current->lockdep_recursion))
5065 return;
5066
5067 if (!current->xhlocks)
5068 return;
5069
5070 /*
5071 * Do commit hist_locks with the cross_lock, only in case that
5072 * the cross_lock could depend on acquisitions after that.
5073 *
5074 * For example, if the cross_lock does not have the 'check' flag
5075 * then we don't need to check dependencies and commit for that.
5076 * Just skip it. In that case, of course, the cross_lock does
5077 * not depend on acquisitions ahead, either.
5078 *
5079 * WARNING: Don't do that in add_xlock() in advance. When an
5080 * acquisition context is different from the commit context,
5081 * invalid(skipped) cross_lock might be accessed.
5082 */
5083 if (!depend_after(&((struct lockdep_map_cross *)lock)->xlock.hlock))
5084 return;
5085
5086 raw_local_irq_save(flags);
5087 check_flags(flags);
5088 current->lockdep_recursion = 1;
5089 xlock = &((struct lockdep_map_cross *)lock)->xlock;
5090 commit_xhlocks(xlock);
5091 current->lockdep_recursion = 0;
5092 raw_local_irq_restore(flags);
5093}
5094EXPORT_SYMBOL_GPL(lock_commit_crosslock);
5095
5096/*
Byungchul Park28a903f2017-08-07 16:12:54 +09005097 * Return: 0 - failure;
5098 * 1 - crosslock, done;
Byungchul Parkb09be672017-08-07 16:12:52 +09005099 * 2 - normal lock, continue to held_lock[] ops.
5100 */
5101static int lock_release_crosslock(struct lockdep_map *lock)
5102{
Byungchul Park28a903f2017-08-07 16:12:54 +09005103 if (cross_lock(lock)) {
5104 if (!graph_lock())
5105 return 0;
5106 ((struct lockdep_map_cross *)lock)->xlock.nr_acquire--;
5107 graph_unlock();
5108 return 1;
5109 }
5110 return 2;
Byungchul Parkb09be672017-08-07 16:12:52 +09005111}
5112
5113static void cross_init(struct lockdep_map *lock, int cross)
5114{
Byungchul Park28a903f2017-08-07 16:12:54 +09005115 if (cross)
5116 ((struct lockdep_map_cross *)lock)->xlock.nr_acquire = 0;
5117
Byungchul Parkb09be672017-08-07 16:12:52 +09005118 lock->cross = cross;
5119
5120 /*
5121 * Crossrelease assumes that the ring buffer size of xhlocks
5122 * is aligned with power of 2. So force it on build.
5123 */
5124 BUILD_BUG_ON(MAX_XHLOCKS_NR & (MAX_XHLOCKS_NR - 1));
5125}
5126
5127void lockdep_init_task(struct task_struct *task)
5128{
5129 int i;
5130
5131 task->xhlock_idx = UINT_MAX;
Byungchul Park23f873d2017-08-07 16:12:53 +09005132 task->hist_id = 0;
Byungchul Parkb09be672017-08-07 16:12:52 +09005133
Byungchul Park23f873d2017-08-07 16:12:53 +09005134 for (i = 0; i < XHLOCK_CTX_NR; i++) {
Byungchul Parkb09be672017-08-07 16:12:52 +09005135 task->xhlock_idx_hist[i] = UINT_MAX;
Byungchul Park23f873d2017-08-07 16:12:53 +09005136 task->hist_id_save[i] = 0;
5137 }
Byungchul Parkb09be672017-08-07 16:12:52 +09005138
5139 task->xhlocks = kzalloc(sizeof(struct hist_lock) * MAX_XHLOCKS_NR,
5140 GFP_KERNEL);
5141}
5142
5143void lockdep_free_task(struct task_struct *task)
5144{
5145 if (task->xhlocks) {
5146 void *tmp = task->xhlocks;
5147 /* Diable crossrelease for current */
5148 task->xhlocks = NULL;
5149 kfree(tmp);
5150 }
5151}
5152#endif