blob: 7f97871d48d545c0d264985a2c83ca1926932741 [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
1142 printk(" Possible unsafe locking scenario:\n\n");
1143 printk(" CPU0 CPU1\n");
1144 printk(" ---- ----\n");
1145 printk(" lock(");
1146 __print_lock_name(target);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001147 printk(KERN_CONT ");\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001148 printk(" lock(");
1149 __print_lock_name(parent);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001150 printk(KERN_CONT ");\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001151 printk(" lock(");
1152 __print_lock_name(target);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001153 printk(KERN_CONT ");\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001154 printk(" lock(");
1155 __print_lock_name(source);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001156 printk(KERN_CONT ");\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001157 printk("\n *** DEADLOCK ***\n\n");
1158}
1159
Peter Zijlstra8e182572007-07-19 01:48:54 -07001160/*
1161 * When a circular dependency is detected, print the
1162 * header first:
1163 */
1164static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001165print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1166 struct held_lock *check_src,
1167 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001168{
1169 struct task_struct *curr = current;
1170
Ming Leic94aa5c2009-07-16 15:44:29 +02001171 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001172 return 0;
1173
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001174 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001175 pr_warn("======================================================\n");
1176 pr_warn("WARNING: possible circular locking dependency detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001177 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001178 pr_warn("------------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001179 pr_warn("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001180 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001181 print_lock(check_src);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001182 pr_warn("\nbut task is already holding lock:\n");
Ming Leidb0002a2009-07-16 15:44:29 +02001183 print_lock(check_tgt);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001184 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1185 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001186
1187 print_circular_bug_entry(entry, depth);
1188
1189 return 0;
1190}
1191
Ming Lei9e2d5512009-07-16 15:44:29 +02001192static inline int class_equal(struct lock_list *entry, void *data)
1193{
1194 return entry->class == data;
1195}
1196
Ming Leidb0002a2009-07-16 15:44:29 +02001197static noinline int print_circular_bug(struct lock_list *this,
1198 struct lock_list *target,
1199 struct held_lock *check_src,
1200 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001201{
1202 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001203 struct lock_list *parent;
Steven Rostedtf4185812011-04-20 21:41:55 -04001204 struct lock_list *first_parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001205 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001206
Ming Leic94aa5c2009-07-16 15:44:29 +02001207 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001208 return 0;
1209
Ming Leidb0002a2009-07-16 15:44:29 +02001210 if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001211 return 0;
1212
Ming Leic94aa5c2009-07-16 15:44:29 +02001213 depth = get_lock_depth(target);
1214
Ming Leidb0002a2009-07-16 15:44:29 +02001215 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001216
1217 parent = get_lock_parent(target);
Steven Rostedtf4185812011-04-20 21:41:55 -04001218 first_parent = parent;
Ming Leic94aa5c2009-07-16 15:44:29 +02001219
1220 while (parent) {
1221 print_circular_bug_entry(parent, --depth);
1222 parent = get_lock_parent(parent);
1223 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001224
1225 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001226 print_circular_lock_scenario(check_src, check_tgt,
1227 first_parent);
1228
Peter Zijlstra8e182572007-07-19 01:48:54 -07001229 lockdep_print_held_locks(curr);
1230
1231 printk("\nstack backtrace:\n");
1232 dump_stack();
1233
1234 return 0;
1235}
1236
Ming Leidb0002a2009-07-16 15:44:29 +02001237static noinline int print_bfs_bug(int ret)
1238{
1239 if (!debug_locks_off_graph_unlock())
1240 return 0;
1241
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001242 /*
1243 * Breadth-first-search failed, graph got corrupted?
1244 */
Ming Leidb0002a2009-07-16 15:44:29 +02001245 WARN(1, "lockdep bfs error:%d\n", ret);
1246
1247 return 0;
1248}
1249
Ming Leief681022009-07-16 15:44:29 +02001250static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001251{
Ming Leief681022009-07-16 15:44:29 +02001252 (*(unsigned long *)data)++;
1253 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001254}
1255
Fengguang Wu5216d532013-11-09 00:55:35 +08001256static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
Ming Leief681022009-07-16 15:44:29 +02001257{
1258 unsigned long count = 0;
1259 struct lock_list *uninitialized_var(target_entry);
1260
1261 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1262
1263 return count;
1264}
David Miller419ca3f2008-07-29 21:45:03 -07001265unsigned long lockdep_count_forward_deps(struct lock_class *class)
1266{
1267 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001268 struct lock_list this;
1269
1270 this.parent = NULL;
1271 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001272
1273 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001274 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001275 ret = __lockdep_count_forward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001276 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001277 local_irq_restore(flags);
1278
1279 return ret;
1280}
1281
Fengguang Wu5216d532013-11-09 00:55:35 +08001282static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001283{
Ming Leief681022009-07-16 15:44:29 +02001284 unsigned long count = 0;
1285 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001286
Ming Leief681022009-07-16 15:44:29 +02001287 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001288
Ming Leief681022009-07-16 15:44:29 +02001289 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001290}
1291
1292unsigned long lockdep_count_backward_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_backward_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
Peter Zijlstra8e182572007-07-19 01:48:54 -07001309/*
1310 * Prove that the dependency graph starting at <entry> can not
1311 * lead to <target>. Print an error and return 0 if it does.
1312 */
1313static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001314check_noncircular(struct lock_list *root, struct lock_class *target,
1315 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001316{
Ming Leidb0002a2009-07-16 15:44:29 +02001317 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001318
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001319 debug_atomic_inc(nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001320
Ming Leid7aaba12009-07-16 15:44:29 +02001321 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001322
1323 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001324}
1325
Peter Zijlstraae813302017-03-03 10:13:38 +01001326static noinline int
1327check_redundant(struct lock_list *root, struct lock_class *target,
1328 struct lock_list **target_entry)
1329{
1330 int result;
1331
1332 debug_atomic_inc(nr_redundant_checks);
1333
1334 result = __bfs_forwards(root, target, class_equal, target_entry);
1335
1336 return result;
1337}
1338
Steven Rostedt81d68a92008-05-12 21:20:42 +02001339#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001340/*
1341 * Forwards and backwards subgraph searching, for the purposes of
1342 * proving that two subgraphs can be connected by a new dependency
1343 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1344 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001345
Ming Leid7aaba12009-07-16 15:44:29 +02001346static inline int usage_match(struct lock_list *entry, void *bit)
1347{
1348 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1349}
1350
1351
1352
Peter Zijlstra8e182572007-07-19 01:48:54 -07001353/*
1354 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001355 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001356 *
Ming Leid7aaba12009-07-16 15:44:29 +02001357 * Return 0 if such a node exists in the subgraph, and put that node
1358 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001359 *
Ming Leid7aaba12009-07-16 15:44:29 +02001360 * Return 1 otherwise and keep *@target_entry unchanged.
1361 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001362 */
Ming Leid7aaba12009-07-16 15:44:29 +02001363static int
1364find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1365 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001366{
Ming Leid7aaba12009-07-16 15:44:29 +02001367 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001368
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001369 debug_atomic_inc(nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001370
Ming Leid7aaba12009-07-16 15:44:29 +02001371 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1372
1373 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001374}
1375
1376/*
1377 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001378 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001379 *
Ming Leid7aaba12009-07-16 15:44:29 +02001380 * Return 0 if such a node exists in the subgraph, and put that node
1381 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001382 *
Ming Leid7aaba12009-07-16 15:44:29 +02001383 * Return 1 otherwise and keep *@target_entry unchanged.
1384 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001385 */
Ming Leid7aaba12009-07-16 15:44:29 +02001386static int
1387find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1388 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001389{
Ming Leid7aaba12009-07-16 15:44:29 +02001390 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001391
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001392 debug_atomic_inc(nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001393
Ming Leid7aaba12009-07-16 15:44:29 +02001394 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001395
Ming Leid7aaba12009-07-16 15:44:29 +02001396 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001397}
1398
Peter Zijlstraaf012962009-07-16 15:44:29 +02001399static void print_lock_class_header(struct lock_class *class, int depth)
1400{
1401 int bit;
1402
1403 printk("%*s->", depth, "");
1404 print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001405 printk(KERN_CONT " ops: %lu", class->ops);
1406 printk(KERN_CONT " {\n");
Peter Zijlstraaf012962009-07-16 15:44:29 +02001407
1408 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1409 if (class->usage_mask & (1 << bit)) {
1410 int len = depth;
1411
1412 len += printk("%*s %s", depth, "", usage_str[bit]);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001413 len += printk(KERN_CONT " at:\n");
Peter Zijlstraaf012962009-07-16 15:44:29 +02001414 print_stack_trace(class->usage_traces + bit, len);
1415 }
1416 }
1417 printk("%*s }\n", depth, "");
1418
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001419 printk("%*s ... key at: [<%p>] %pS\n",
1420 depth, "", class->key, class->key);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001421}
1422
1423/*
1424 * printk the shortest lock dependencies from @start to @end in reverse order:
1425 */
1426static void __used
1427print_shortest_lock_dependencies(struct lock_list *leaf,
1428 struct lock_list *root)
1429{
1430 struct lock_list *entry = leaf;
1431 int depth;
1432
1433 /*compute depth from generated tree by BFS*/
1434 depth = get_lock_depth(leaf);
1435
1436 do {
1437 print_lock_class_header(entry->class, depth);
1438 printk("%*s ... acquired at:\n", depth, "");
1439 print_stack_trace(&entry->trace, 2);
1440 printk("\n");
1441
1442 if (depth == 0 && (entry != root)) {
Steven Rostedt6be8c392011-04-20 21:41:58 -04001443 printk("lockdep:%s bad path found in chain graph\n", __func__);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001444 break;
1445 }
1446
1447 entry = get_lock_parent(entry);
1448 depth--;
1449 } while (entry && (depth >= 0));
1450
1451 return;
1452}
Ming Leid7aaba12009-07-16 15:44:29 +02001453
Steven Rostedt3003eba2011-04-20 21:41:54 -04001454static void
1455print_irq_lock_scenario(struct lock_list *safe_entry,
1456 struct lock_list *unsafe_entry,
Steven Rostedtdad3d742011-04-20 21:41:57 -04001457 struct lock_class *prev_class,
1458 struct lock_class *next_class)
Steven Rostedt3003eba2011-04-20 21:41:54 -04001459{
1460 struct lock_class *safe_class = safe_entry->class;
1461 struct lock_class *unsafe_class = unsafe_entry->class;
Steven Rostedtdad3d742011-04-20 21:41:57 -04001462 struct lock_class *middle_class = prev_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001463
1464 if (middle_class == safe_class)
Steven Rostedtdad3d742011-04-20 21:41:57 -04001465 middle_class = next_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001466
1467 /*
1468 * A direct locking problem where unsafe_class lock is taken
1469 * directly by safe_class lock, then all we need to show
1470 * is the deadlock scenario, as it is obvious that the
1471 * unsafe lock is taken under the safe lock.
1472 *
1473 * But if there is a chain instead, where the safe lock takes
1474 * an intermediate lock (middle_class) where this lock is
1475 * not the same as the safe lock, then the lock chain is
1476 * used to describe the problem. Otherwise we would need
1477 * to show a different CPU case for each link in the chain
1478 * from the safe_class lock to the unsafe_class lock.
1479 */
1480 if (middle_class != unsafe_class) {
1481 printk("Chain exists of:\n ");
1482 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001483 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001484 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001485 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001486 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001487 printk(KERN_CONT "\n\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001488 }
1489
1490 printk(" Possible interrupt unsafe locking scenario:\n\n");
1491 printk(" CPU0 CPU1\n");
1492 printk(" ---- ----\n");
1493 printk(" lock(");
1494 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001495 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001496 printk(" local_irq_disable();\n");
1497 printk(" lock(");
1498 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001499 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001500 printk(" lock(");
1501 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001502 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001503 printk(" <Interrupt>\n");
1504 printk(" lock(");
1505 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001506 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001507 printk("\n *** DEADLOCK ***\n\n");
1508}
1509
Peter Zijlstra8e182572007-07-19 01:48:54 -07001510static int
1511print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001512 struct lock_list *prev_root,
1513 struct lock_list *next_root,
1514 struct lock_list *backwards_entry,
1515 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001516 struct held_lock *prev,
1517 struct held_lock *next,
1518 enum lock_usage_bit bit1,
1519 enum lock_usage_bit bit2,
1520 const char *irqclass)
1521{
1522 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1523 return 0;
1524
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001525 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001526 pr_warn("=====================================================\n");
1527 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07001528 irqclass, irqclass);
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001529 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001530 pr_warn("-----------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001531 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 -07001532 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001533 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1534 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1535 curr->hardirqs_enabled,
1536 curr->softirqs_enabled);
1537 print_lock(next);
1538
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001539 pr_warn("\nand this task is already holding:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001540 print_lock(prev);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001541 pr_warn("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001542 print_lock_name(hlock_class(prev));
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001543 pr_cont(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001544 print_lock_name(hlock_class(next));
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001545 pr_cont("\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001546
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001547 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07001548 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001549 print_lock_name(backwards_entry->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001550 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001551
Ming Lei24208ca2009-07-16 15:44:29 +02001552 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001553
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001554 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001555 print_lock_name(forwards_entry->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001556 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
1557 pr_warn("...");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001558
Ming Lei24208ca2009-07-16 15:44:29 +02001559 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001560
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001561 pr_warn("\nother info that might help us debug this:\n\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04001562 print_irq_lock_scenario(backwards_entry, forwards_entry,
1563 hlock_class(prev), hlock_class(next));
Steven Rostedt3003eba2011-04-20 21:41:54 -04001564
Peter Zijlstra8e182572007-07-19 01:48:54 -07001565 lockdep_print_held_locks(curr);
1566
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001567 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001568 if (!save_trace(&prev_root->trace))
1569 return 0;
1570 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001571
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001572 pr_warn("\nthe dependencies between the lock to be acquired");
1573 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001574 if (!save_trace(&next_root->trace))
1575 return 0;
1576 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001577
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001578 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001579 dump_stack();
1580
1581 return 0;
1582}
1583
1584static int
1585check_usage(struct task_struct *curr, struct held_lock *prev,
1586 struct held_lock *next, enum lock_usage_bit bit_backwards,
1587 enum lock_usage_bit bit_forwards, const char *irqclass)
1588{
1589 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001590 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001591 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001592 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001593
Ming Leid7aaba12009-07-16 15:44:29 +02001594 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001595
Ming Leid7aaba12009-07-16 15:44:29 +02001596 this.class = hlock_class(prev);
1597 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001598 if (ret < 0)
1599 return print_bfs_bug(ret);
1600 if (ret == 1)
1601 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001602
Ming Lei24208ca2009-07-16 15:44:29 +02001603 that.parent = NULL;
1604 that.class = hlock_class(next);
1605 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001606 if (ret < 0)
1607 return print_bfs_bug(ret);
1608 if (ret == 1)
1609 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001610
Ming Lei24208ca2009-07-16 15:44:29 +02001611 return print_bad_irq_dependency(curr, &this, &that,
1612 target_entry, target_entry1,
1613 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001614 bit_backwards, bit_forwards, irqclass);
1615}
1616
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001617static const char *state_names[] = {
1618#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001619 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001620#include "lockdep_states.h"
1621#undef LOCKDEP_STATE
1622};
1623
1624static const char *state_rnames[] = {
1625#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001626 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001627#include "lockdep_states.h"
1628#undef LOCKDEP_STATE
1629};
1630
1631static inline const char *state_name(enum lock_usage_bit bit)
1632{
1633 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1634}
1635
1636static int exclusive_bit(int new_bit)
1637{
1638 /*
1639 * USED_IN
1640 * USED_IN_READ
1641 * ENABLED
1642 * ENABLED_READ
1643 *
1644 * bit 0 - write/read
1645 * bit 1 - used_in/enabled
1646 * bit 2+ state
1647 */
1648
1649 int state = new_bit & ~3;
1650 int dir = new_bit & 2;
1651
1652 /*
1653 * keep state, bit flip the direction and strip read.
1654 */
1655 return state | (dir ^ 2);
1656}
1657
1658static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1659 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001660{
1661 /*
1662 * Prove that the new dependency does not connect a hardirq-safe
1663 * lock with a hardirq-unsafe lock - to achieve this we search
1664 * the backwards-subgraph starting at <prev>, and the
1665 * forwards-subgraph starting at <next>:
1666 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001667 if (!check_usage(curr, prev, next, bit,
1668 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001669 return 0;
1670
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001671 bit++; /* _READ */
1672
Peter Zijlstra8e182572007-07-19 01:48:54 -07001673 /*
1674 * Prove that the new dependency does not connect a hardirq-safe-read
1675 * lock with a hardirq-unsafe lock - to achieve this we search
1676 * the backwards-subgraph starting at <prev>, and the
1677 * forwards-subgraph starting at <next>:
1678 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001679 if (!check_usage(curr, prev, next, bit,
1680 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001681 return 0;
1682
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001683 return 1;
1684}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001685
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001686static int
1687check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1688 struct held_lock *next)
1689{
1690#define LOCKDEP_STATE(__STATE) \
1691 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001692 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001693#include "lockdep_states.h"
1694#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001695
Peter Zijlstra8e182572007-07-19 01:48:54 -07001696 return 1;
1697}
1698
1699static void inc_chains(void)
1700{
1701 if (current->hardirq_context)
1702 nr_hardirq_chains++;
1703 else {
1704 if (current->softirq_context)
1705 nr_softirq_chains++;
1706 else
1707 nr_process_chains++;
1708 }
1709}
1710
1711#else
1712
1713static inline int
1714check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1715 struct held_lock *next)
1716{
1717 return 1;
1718}
1719
1720static inline void inc_chains(void)
1721{
1722 nr_process_chains++;
1723}
1724
1725#endif
1726
Steven Rostedt48702ec2011-04-20 21:41:56 -04001727static void
1728print_deadlock_scenario(struct held_lock *nxt,
1729 struct held_lock *prv)
1730{
1731 struct lock_class *next = hlock_class(nxt);
1732 struct lock_class *prev = hlock_class(prv);
1733
1734 printk(" Possible unsafe locking scenario:\n\n");
1735 printk(" CPU0\n");
1736 printk(" ----\n");
1737 printk(" lock(");
1738 __print_lock_name(prev);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001739 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001740 printk(" lock(");
1741 __print_lock_name(next);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001742 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001743 printk("\n *** DEADLOCK ***\n\n");
1744 printk(" May be due to missing lock nesting notation\n\n");
1745}
1746
Peter Zijlstra8e182572007-07-19 01:48:54 -07001747static int
1748print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1749 struct held_lock *next)
1750{
1751 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1752 return 0;
1753
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001754 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001755 pr_warn("============================================\n");
1756 pr_warn("WARNING: possible recursive locking detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001757 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001758 pr_warn("--------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001759 pr_warn("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001760 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001761 print_lock(next);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001762 pr_warn("\nbut task is already holding lock:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001763 print_lock(prev);
1764
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001765 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001766 print_deadlock_scenario(next, prev);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001767 lockdep_print_held_locks(curr);
1768
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001769 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001770 dump_stack();
1771
1772 return 0;
1773}
1774
1775/*
1776 * Check whether we are holding such a class already.
1777 *
1778 * (Note that this has to be done separately, because the graph cannot
1779 * detect such classes of deadlocks.)
1780 *
1781 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1782 */
1783static int
1784check_deadlock(struct task_struct *curr, struct held_lock *next,
1785 struct lockdep_map *next_instance, int read)
1786{
1787 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001788 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001789 int i;
1790
1791 for (i = 0; i < curr->lockdep_depth; i++) {
1792 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001793
1794 if (prev->instance == next->nest_lock)
1795 nest = prev;
1796
Dave Jonesf82b2172008-08-11 09:30:23 +02001797 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001798 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001799
Peter Zijlstra8e182572007-07-19 01:48:54 -07001800 /*
1801 * Allow read-after-read recursion of the same
1802 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1803 */
1804 if ((read == 2) && prev->read)
1805 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001806
1807 /*
1808 * We're holding the nest_lock, which serializes this lock's
1809 * nesting behaviour.
1810 */
1811 if (nest)
1812 return 2;
1813
Byungchul Parkb09be672017-08-07 16:12:52 +09001814 if (cross_lock(prev->instance))
1815 continue;
1816
Peter Zijlstra8e182572007-07-19 01:48:54 -07001817 return print_deadlock_bug(curr, prev, next);
1818 }
1819 return 1;
1820}
1821
1822/*
1823 * There was a chain-cache miss, and we are about to add a new dependency
1824 * to a previous lock. We recursively validate the following rules:
1825 *
1826 * - would the adding of the <prev> -> <next> dependency create a
1827 * circular dependency in the graph? [== circular deadlock]
1828 *
1829 * - does the new prev->next dependency connect any hardirq-safe lock
1830 * (in the full backwards-subgraph starting at <prev>) with any
1831 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1832 * <next>)? [== illegal lock inversion with hardirq contexts]
1833 *
1834 * - does the new prev->next dependency connect any softirq-safe lock
1835 * (in the full backwards-subgraph starting at <prev>) with any
1836 * softirq-unsafe lock (in the full forwards-subgraph starting at
1837 * <next>)? [== illegal lock inversion with softirq contexts]
1838 *
1839 * any of these scenarios could lead to a deadlock.
1840 *
1841 * Then if all the validations pass, we add the forwards and backwards
1842 * dependency.
1843 */
1844static int
1845check_prev_add(struct task_struct *curr, struct held_lock *prev,
Byungchul Parkce07a9412017-08-07 16:12:51 +09001846 struct held_lock *next, int distance, struct stack_trace *trace,
1847 int (*save)(struct stack_trace *trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001848{
1849 struct lock_list *entry;
1850 int ret;
Ming Leidb0002a2009-07-16 15:44:29 +02001851 struct lock_list this;
1852 struct lock_list *uninitialized_var(target_entry);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001853
1854 /*
1855 * Prove that the new <prev> -> <next> dependency would not
1856 * create a circular dependency in the graph. (We do this by
1857 * forward-recursing into the graph starting at <next>, and
1858 * checking whether we can reach <prev>.)
1859 *
1860 * We are using global variables to control the recursion, to
1861 * keep the stackframe size of the recursive functions low:
1862 */
Ming Leidb0002a2009-07-16 15:44:29 +02001863 this.class = hlock_class(next);
1864 this.parent = NULL;
1865 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1866 if (unlikely(!ret))
1867 return print_circular_bug(&this, target_entry, next, prev);
1868 else if (unlikely(ret < 0))
1869 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02001870
Peter Zijlstra8e182572007-07-19 01:48:54 -07001871 if (!check_prev_add_irq(curr, prev, next))
1872 return 0;
1873
1874 /*
1875 * For recursive read-locks we do all the dependency checks,
1876 * but we dont store read-triggered dependencies (only
1877 * write-triggered dependencies). This ensures that only the
1878 * write-side dependencies matter, and that if for example a
1879 * write-lock never takes any other locks, then the reads are
1880 * equivalent to a NOP.
1881 */
1882 if (next->read == 2 || prev->read == 2)
1883 return 1;
1884 /*
1885 * Is the <prev> -> <next> dependency already present?
1886 *
1887 * (this may occur even though this is a new chain: consider
1888 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1889 * chains - the second one will be new, but L1 already has
1890 * L2 added to its dependency list, due to the first chain.)
1891 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001892 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1893 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001894 if (distance == 1)
1895 entry->distance = 1;
Byungchul Park70911fd2017-08-07 16:12:50 +09001896 return 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001897 }
1898 }
1899
Peter Zijlstraae813302017-03-03 10:13:38 +01001900 /*
1901 * Is the <prev> -> <next> link redundant?
1902 */
1903 this.class = hlock_class(prev);
1904 this.parent = NULL;
1905 ret = check_redundant(&this, hlock_class(next), &target_entry);
1906 if (!ret) {
1907 debug_atomic_inc(nr_redundant);
1908 return 2;
1909 }
1910 if (ret < 0)
1911 return print_bfs_bug(ret);
1912
1913
Byungchul Parkce07a9412017-08-07 16:12:51 +09001914 if (save && !save(trace))
1915 return 0;
Yong Zhang4726f2a2010-05-04 14:16:48 +08001916
Peter Zijlstra8e182572007-07-19 01:48:54 -07001917 /*
1918 * Ok, all validations passed, add the new lock
1919 * to the previous lock's dependency list:
1920 */
Tahsin Erdogan83f06162016-11-08 00:02:07 -08001921 ret = add_lock_to_list(hlock_class(next),
Dave Jonesf82b2172008-08-11 09:30:23 +02001922 &hlock_class(prev)->locks_after,
Byungchul Parkce07a9412017-08-07 16:12:51 +09001923 next->acquire_ip, distance, trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001924
1925 if (!ret)
1926 return 0;
1927
Tahsin Erdogan83f06162016-11-08 00:02:07 -08001928 ret = add_lock_to_list(hlock_class(prev),
Dave Jonesf82b2172008-08-11 09:30:23 +02001929 &hlock_class(next)->locks_before,
Byungchul Parkce07a9412017-08-07 16:12:51 +09001930 next->acquire_ip, distance, trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001931 if (!ret)
1932 return 0;
1933
1934 /*
1935 * Debugging printouts:
1936 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001937 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001938 graph_unlock();
1939 printk("\n new dependency: ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001940 print_lock_name(hlock_class(prev));
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001941 printk(KERN_CONT " => ");
Dave Jonesf82b2172008-08-11 09:30:23 +02001942 print_lock_name(hlock_class(next));
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001943 printk(KERN_CONT "\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001944 dump_stack();
Byungchul Park70911fd2017-08-07 16:12:50 +09001945 if (!graph_lock())
1946 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001947 }
Byungchul Park70911fd2017-08-07 16:12:50 +09001948 return 2;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001949}
1950
1951/*
1952 * Add the dependency to all directly-previous locks that are 'relevant'.
1953 * The ones that are relevant are (in increasing distance from curr):
1954 * all consecutive trylock entries and the final non-trylock entry - or
1955 * the end of this context's lock-chain - whichever comes first.
1956 */
1957static int
1958check_prevs_add(struct task_struct *curr, struct held_lock *next)
1959{
1960 int depth = curr->lockdep_depth;
1961 struct held_lock *hlock;
Byungchul Parkce07a9412017-08-07 16:12:51 +09001962 struct stack_trace trace;
1963 int (*save)(struct stack_trace *trace) = save_trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001964
1965 /*
1966 * Debugging checks.
1967 *
1968 * Depth must not be zero for a non-head lock:
1969 */
1970 if (!depth)
1971 goto out_bug;
1972 /*
1973 * At least two relevant locks must exist for this
1974 * to be a head:
1975 */
1976 if (curr->held_locks[depth].irq_context !=
1977 curr->held_locks[depth-1].irq_context)
1978 goto out_bug;
1979
1980 for (;;) {
1981 int distance = curr->lockdep_depth - depth + 1;
Oleg Nesterov1b5ff812014-01-20 19:20:10 +01001982 hlock = curr->held_locks + depth - 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001983 /*
Byungchul Parkb09be672017-08-07 16:12:52 +09001984 * Only non-crosslock entries get new dependencies added.
1985 * Crosslock entries will be added by commit later:
Peter Zijlstra8e182572007-07-19 01:48:54 -07001986 */
Byungchul Parkb09be672017-08-07 16:12:52 +09001987 if (!cross_lock(hlock->instance)) {
Byungchul Parkce07a9412017-08-07 16:12:51 +09001988 /*
Byungchul Parkb09be672017-08-07 16:12:52 +09001989 * Only non-recursive-read entries get new dependencies
1990 * added:
Byungchul Parkce07a9412017-08-07 16:12:51 +09001991 */
Byungchul Parkb09be672017-08-07 16:12:52 +09001992 if (hlock->read != 2 && hlock->check) {
1993 int ret = check_prev_add(curr, hlock, next,
1994 distance, &trace, save);
1995 if (!ret)
1996 return 0;
Byungchul Parkce07a9412017-08-07 16:12:51 +09001997
Byungchul Parkb09be672017-08-07 16:12:52 +09001998 /*
1999 * Stop saving stack_trace if save_trace() was
2000 * called at least once:
2001 */
2002 if (save && ret == 2)
2003 save = NULL;
2004
2005 /*
2006 * Stop after the first non-trylock entry,
2007 * as non-trylock entries have added their
2008 * own direct dependencies already, so this
2009 * lock is connected to them indirectly:
2010 */
2011 if (!hlock->trylock)
2012 break;
2013 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07002014 }
2015 depth--;
2016 /*
2017 * End of lock-stack?
2018 */
2019 if (!depth)
2020 break;
2021 /*
2022 * Stop the search if we cross into another context:
2023 */
2024 if (curr->held_locks[depth].irq_context !=
2025 curr->held_locks[depth-1].irq_context)
2026 break;
2027 }
2028 return 1;
2029out_bug:
2030 if (!debug_locks_off_graph_unlock())
2031 return 0;
2032
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002033 /*
2034 * Clearly we all shouldn't be here, but since we made it we
2035 * can reliable say we messed up our state. See the above two
2036 * gotos for reasons why we could possibly end up here.
2037 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002038 WARN_ON(1);
2039
2040 return 0;
2041}
2042
2043unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08002044struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08002045int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08002046static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
2047
2048struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
2049{
2050 return lock_classes + chain_hlocks[chain->base + i];
2051}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002052
2053/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002054 * Returns the index of the first held_lock of the current chain
2055 */
2056static inline int get_first_held_lock(struct task_struct *curr,
2057 struct held_lock *hlock)
2058{
2059 int i;
2060 struct held_lock *hlock_curr;
2061
2062 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2063 hlock_curr = curr->held_locks + i;
2064 if (hlock_curr->irq_context != hlock->irq_context)
2065 break;
2066
2067 }
2068
2069 return ++i;
2070}
2071
Borislav Petkov5c8a0102016-04-04 10:42:07 +02002072#ifdef CONFIG_DEBUG_LOCKDEP
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002073/*
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002074 * Returns the next chain_key iteration
2075 */
2076static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
2077{
2078 u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
2079
2080 printk(" class_idx:%d -> chain_key:%016Lx",
2081 class_idx,
2082 (unsigned long long)new_chain_key);
2083 return new_chain_key;
2084}
2085
2086static void
2087print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
2088{
2089 struct held_lock *hlock;
2090 u64 chain_key = 0;
2091 int depth = curr->lockdep_depth;
2092 int i;
2093
2094 printk("depth: %u\n", depth + 1);
2095 for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
2096 hlock = curr->held_locks + i;
2097 chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
2098
2099 print_lock(hlock);
2100 }
2101
2102 print_chain_key_iteration(hlock_next->class_idx, chain_key);
2103 print_lock(hlock_next);
2104}
2105
2106static void print_chain_keys_chain(struct lock_chain *chain)
2107{
2108 int i;
2109 u64 chain_key = 0;
2110 int class_id;
2111
2112 printk("depth: %u\n", chain->depth);
2113 for (i = 0; i < chain->depth; i++) {
2114 class_id = chain_hlocks[chain->base + i];
2115 chain_key = print_chain_key_iteration(class_id + 1, chain_key);
2116
2117 print_lock_name(lock_classes + class_id);
2118 printk("\n");
2119 }
2120}
2121
2122static void print_collision(struct task_struct *curr,
2123 struct held_lock *hlock_next,
2124 struct lock_chain *chain)
2125{
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002126 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002127 pr_warn("============================\n");
2128 pr_warn("WARNING: chain_key collision\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002129 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002130 pr_warn("----------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002131 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
2132 pr_warn("Hash chain already cached but the contents don't match!\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002133
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002134 pr_warn("Held locks:");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002135 print_chain_keys_held_locks(curr, hlock_next);
2136
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002137 pr_warn("Locks in cached chain:");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002138 print_chain_keys_chain(chain);
2139
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002140 pr_warn("\nstack backtrace:\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002141 dump_stack();
2142}
Borislav Petkov5c8a0102016-04-04 10:42:07 +02002143#endif
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002144
2145/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002146 * Checks whether the chain and the current held locks are consistent
2147 * in depth and also in content. If they are not it most likely means
2148 * that there was a collision during the calculation of the chain_key.
2149 * Returns: 0 not passed, 1 passed
2150 */
2151static int check_no_collision(struct task_struct *curr,
2152 struct held_lock *hlock,
2153 struct lock_chain *chain)
2154{
2155#ifdef CONFIG_DEBUG_LOCKDEP
2156 int i, j, id;
2157
2158 i = get_first_held_lock(curr, hlock);
2159
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002160 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
2161 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002162 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002163 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002164
2165 for (j = 0; j < chain->depth - 1; j++, i++) {
2166 id = curr->held_locks[i].class_idx - 1;
2167
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002168 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
2169 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002170 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002171 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002172 }
2173#endif
2174 return 1;
2175}
2176
2177/*
Byungchul Park49347a92017-08-07 16:12:49 +09002178 * This is for building a chain between just two different classes,
2179 * instead of adding a new hlock upon current, which is done by
2180 * add_chain_cache().
2181 *
2182 * This can be called in any context with two classes, while
2183 * add_chain_cache() must be done within the lock owener's context
2184 * since it uses hlock which might be racy in another context.
2185 */
2186static inline int add_chain_cache_classes(unsigned int prev,
2187 unsigned int next,
2188 unsigned int irq_context,
2189 u64 chain_key)
2190{
2191 struct hlist_head *hash_head = chainhashentry(chain_key);
2192 struct lock_chain *chain;
2193
2194 /*
2195 * Allocate a new chain entry from the static array, and add
2196 * it to the hash:
2197 */
2198
2199 /*
2200 * We might need to take the graph lock, ensure we've got IRQs
2201 * disabled to make this an IRQ-safe lock.. for recursion reasons
2202 * lockdep won't complain about its own locking errors.
2203 */
2204 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2205 return 0;
2206
2207 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2208 if (!debug_locks_off_graph_unlock())
2209 return 0;
2210
2211 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2212 dump_stack();
2213 return 0;
2214 }
2215
2216 chain = lock_chains + nr_lock_chains++;
2217 chain->chain_key = chain_key;
2218 chain->irq_context = irq_context;
2219 chain->depth = 2;
2220 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2221 chain->base = nr_chain_hlocks;
2222 nr_chain_hlocks += chain->depth;
2223 chain_hlocks[chain->base] = prev - 1;
2224 chain_hlocks[chain->base + 1] = next -1;
2225 }
2226#ifdef CONFIG_DEBUG_LOCKDEP
2227 /*
2228 * Important for check_no_collision().
2229 */
2230 else {
2231 if (!debug_locks_off_graph_unlock())
2232 return 0;
2233
2234 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2235 dump_stack();
2236 return 0;
2237 }
2238#endif
2239
2240 hlist_add_head_rcu(&chain->entry, hash_head);
2241 debug_atomic_inc(chain_lookup_misses);
2242 inc_chains();
2243
2244 return 1;
2245}
2246
2247/*
Byungchul Park545c23f2017-08-07 16:12:48 +09002248 * Adds a dependency chain into chain hashtable. And must be called with
2249 * graph_lock held.
2250 *
2251 * Return 0 if fail, and graph_lock is released.
2252 * Return 1 if succeed, with graph_lock held.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002253 */
Byungchul Park545c23f2017-08-07 16:12:48 +09002254static inline int add_chain_cache(struct task_struct *curr,
2255 struct held_lock *hlock,
2256 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002257{
Dave Jonesf82b2172008-08-11 09:30:23 +02002258 struct lock_class *class = hlock_class(hlock);
Andrew Mortona63f38c2016-02-03 13:44:12 -08002259 struct hlist_head *hash_head = chainhashentry(chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002260 struct lock_chain *chain;
Steven Rostedte0944ee2011-04-20 21:42:00 -04002261 int i, j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002262
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002263 /*
Byungchul Park545c23f2017-08-07 16:12:48 +09002264 * Allocate a new chain entry from the static array, and add
2265 * it to the hash:
2266 */
2267
2268 /*
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002269 * We might need to take the graph lock, ensure we've got IRQs
2270 * disabled to make this an IRQ-safe lock.. for recursion reasons
2271 * lockdep won't complain about its own locking errors.
2272 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08002273 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2274 return 0;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002275
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002276 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002277 if (!debug_locks_off_graph_unlock())
2278 return 0;
2279
Dave Jones2c522832013-04-25 13:40:02 -04002280 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002281 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002282 return 0;
2283 }
2284 chain = lock_chains + nr_lock_chains++;
2285 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08002286 chain->irq_context = hlock->irq_context;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002287 i = get_first_held_lock(curr, hlock);
Huang, Ying443cd502008-06-20 16:39:21 +08002288 chain->depth = curr->lockdep_depth + 1 - i;
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002289
2290 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
2291 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
2292 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
2293
Steven Rostedte0944ee2011-04-20 21:42:00 -04002294 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2295 chain->base = nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08002296 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02002297 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08002298 chain_hlocks[chain->base + j] = lock_id;
2299 }
2300 chain_hlocks[chain->base + j] = class - lock_classes;
2301 }
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002302
2303 if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS)
2304 nr_chain_hlocks += chain->depth;
2305
2306#ifdef CONFIG_DEBUG_LOCKDEP
2307 /*
2308 * Important for check_no_collision().
2309 */
2310 if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) {
Byungchul Parkf9af4562017-01-13 11:42:04 +09002311 if (!debug_locks_off_graph_unlock())
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002312 return 0;
2313
2314 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2315 dump_stack();
2316 return 0;
2317 }
2318#endif
2319
Andrew Mortona63f38c2016-02-03 13:44:12 -08002320 hlist_add_head_rcu(&chain->entry, hash_head);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002321 debug_atomic_inc(chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002322 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002323
2324 return 1;
2325}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002326
Byungchul Park545c23f2017-08-07 16:12:48 +09002327/*
2328 * Look up a dependency chain.
2329 */
2330static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
2331{
2332 struct hlist_head *hash_head = chainhashentry(chain_key);
2333 struct lock_chain *chain;
2334
2335 /*
2336 * We can walk it lock-free, because entries only get added
2337 * to the hash:
2338 */
2339 hlist_for_each_entry_rcu(chain, hash_head, entry) {
2340 if (chain->chain_key == chain_key) {
2341 debug_atomic_inc(chain_lookup_hits);
2342 return chain;
2343 }
2344 }
2345 return NULL;
2346}
2347
2348/*
2349 * If the key is not present yet in dependency chain cache then
2350 * add it and return 1 - in this case the new dependency chain is
2351 * validated. If the key is already hashed, return 0.
2352 * (On return with 1 graph_lock is held.)
2353 */
2354static inline int lookup_chain_cache_add(struct task_struct *curr,
2355 struct held_lock *hlock,
2356 u64 chain_key)
2357{
2358 struct lock_class *class = hlock_class(hlock);
2359 struct lock_chain *chain = lookup_chain_cache(chain_key);
2360
2361 if (chain) {
2362cache_hit:
2363 if (!check_no_collision(curr, hlock, chain))
2364 return 0;
2365
2366 if (very_verbose(class)) {
2367 printk("\nhash chain already cached, key: "
2368 "%016Lx tail class: [%p] %s\n",
2369 (unsigned long long)chain_key,
2370 class->key, class->name);
2371 }
2372
2373 return 0;
2374 }
2375
2376 if (very_verbose(class)) {
2377 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2378 (unsigned long long)chain_key, class->key, class->name);
2379 }
2380
2381 if (!graph_lock())
2382 return 0;
2383
2384 /*
2385 * We have to walk the chain again locked - to avoid duplicates:
2386 */
2387 chain = lookup_chain_cache(chain_key);
2388 if (chain) {
2389 graph_unlock();
2390 goto cache_hit;
2391 }
2392
2393 if (!add_chain_cache(curr, hlock, chain_key))
2394 return 0;
2395
2396 return 1;
2397}
2398
Peter Zijlstra8e182572007-07-19 01:48:54 -07002399static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07002400 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002401{
2402 /*
2403 * Trylock needs to maintain the stack of held locks, but it
2404 * does not add new dependencies, because trylock can be done
2405 * in any order.
2406 *
2407 * We look up the chain_key and do the O(N^2) check and update of
2408 * the dependencies only if this is a new dependency chain.
Byungchul Park545c23f2017-08-07 16:12:48 +09002409 * (If lookup_chain_cache_add() return with 1 it acquires
Peter Zijlstra8e182572007-07-19 01:48:54 -07002410 * graph_lock for us)
2411 */
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01002412 if (!hlock->trylock && hlock->check &&
Byungchul Park545c23f2017-08-07 16:12:48 +09002413 lookup_chain_cache_add(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002414 /*
2415 * Check whether last held lock:
2416 *
2417 * - is irq-safe, if this lock is irq-unsafe
2418 * - is softirq-safe, if this lock is hardirq-unsafe
2419 *
2420 * And check whether the new lock's dependency graph
2421 * could lead back to the previous lock.
2422 *
2423 * any of these scenarios could lead to a deadlock. If
2424 * All validations
2425 */
2426 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2427
2428 if (!ret)
2429 return 0;
2430 /*
2431 * Mark recursive read, as we jump over it when
2432 * building dependencies (just like we jump over
2433 * trylock entries):
2434 */
2435 if (ret == 2)
2436 hlock->read = 2;
2437 /*
2438 * Add dependency only if this lock is not the head
2439 * of the chain, and if it's not a secondary read-lock:
2440 */
Byungchul Park545c23f2017-08-07 16:12:48 +09002441 if (!chain_head && ret != 2) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002442 if (!check_prevs_add(curr, hlock))
2443 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09002444 }
2445
Peter Zijlstra8e182572007-07-19 01:48:54 -07002446 graph_unlock();
Byungchul Park545c23f2017-08-07 16:12:48 +09002447 } else {
2448 /* after lookup_chain_cache_add(): */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002449 if (unlikely(!debug_locks))
2450 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09002451 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07002452
2453 return 1;
2454}
2455#else
2456static inline int validate_chain(struct task_struct *curr,
2457 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002458 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002459{
2460 return 1;
2461}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07002462#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002463
2464/*
2465 * We are building curr_chain_key incrementally, so double-check
2466 * it from scratch, to make sure that it's done correctly:
2467 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002468static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002469{
2470#ifdef CONFIG_DEBUG_LOCKDEP
2471 struct held_lock *hlock, *prev_hlock = NULL;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002472 unsigned int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002473 u64 chain_key = 0;
2474
2475 for (i = 0; i < curr->lockdep_depth; i++) {
2476 hlock = curr->held_locks + i;
2477 if (chain_key != hlock->prev_chain_key) {
2478 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002479 /*
2480 * We got mighty confused, our chain keys don't match
2481 * with what we expect, someone trample on our task state?
2482 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002483 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002484 curr->lockdep_depth, i,
2485 (unsigned long long)chain_key,
2486 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002487 return;
2488 }
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002489 /*
2490 * Whoops ran out of static storage again?
2491 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002492 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
Jarek Poplawski381a2292007-02-10 01:44:58 -08002493 return;
2494
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002495 if (prev_hlock && (prev_hlock->irq_context !=
2496 hlock->irq_context))
2497 chain_key = 0;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002498 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002499 prev_hlock = hlock;
2500 }
2501 if (chain_key != curr->curr_chain_key) {
2502 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002503 /*
2504 * More smoking hash instead of calculating it, damn see these
2505 * numbers float.. I bet that a pink elephant stepped on my memory.
2506 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002507 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002508 curr->lockdep_depth, i,
2509 (unsigned long long)chain_key,
2510 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002511 }
2512#endif
2513}
2514
Steven Rostedt282b5c22011-04-20 21:41:59 -04002515static void
2516print_usage_bug_scenario(struct held_lock *lock)
2517{
2518 struct lock_class *class = hlock_class(lock);
2519
2520 printk(" Possible unsafe locking scenario:\n\n");
2521 printk(" CPU0\n");
2522 printk(" ----\n");
2523 printk(" lock(");
2524 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002525 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002526 printk(" <Interrupt>\n");
2527 printk(" lock(");
2528 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002529 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002530 printk("\n *** DEADLOCK ***\n\n");
2531}
2532
Peter Zijlstra8e182572007-07-19 01:48:54 -07002533static int
2534print_usage_bug(struct task_struct *curr, struct held_lock *this,
2535 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2536{
2537 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2538 return 0;
2539
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002540 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002541 pr_warn("================================\n");
2542 pr_warn("WARNING: inconsistent lock state\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002543 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002544 pr_warn("--------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002545
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002546 pr_warn("inconsistent {%s} -> {%s} usage.\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07002547 usage_str[prev_bit], usage_str[new_bit]);
2548
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002549 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002550 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002551 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2552 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2553 trace_hardirqs_enabled(curr),
2554 trace_softirqs_enabled(curr));
2555 print_lock(this);
2556
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002557 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002558 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002559
2560 print_irqtrace_events(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002561 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002562 print_usage_bug_scenario(this);
2563
Peter Zijlstra8e182572007-07-19 01:48:54 -07002564 lockdep_print_held_locks(curr);
2565
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002566 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002567 dump_stack();
2568
2569 return 0;
2570}
2571
2572/*
2573 * Print out an error if an invalid bit is set:
2574 */
2575static inline int
2576valid_state(struct task_struct *curr, struct held_lock *this,
2577 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2578{
Dave Jonesf82b2172008-08-11 09:30:23 +02002579 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002580 return print_usage_bug(curr, this, bad_bit, new_bit);
2581 return 1;
2582}
2583
2584static int mark_lock(struct task_struct *curr, struct held_lock *this,
2585 enum lock_usage_bit new_bit);
2586
Steven Rostedt81d68a92008-05-12 21:20:42 +02002587#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002588
2589/*
2590 * print irq inversion bug:
2591 */
2592static int
Ming Lei24208ca2009-07-16 15:44:29 +02002593print_irq_inversion_bug(struct task_struct *curr,
2594 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002595 struct held_lock *this, int forwards,
2596 const char *irqclass)
2597{
Steven Rostedtdad3d742011-04-20 21:41:57 -04002598 struct lock_list *entry = other;
2599 struct lock_list *middle = NULL;
2600 int depth;
2601
Ingo Molnar74c383f2006-12-13 00:34:43 -08002602 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002603 return 0;
2604
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002605 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002606 pr_warn("========================================================\n");
2607 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002608 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002609 pr_warn("--------------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002610 pr_warn("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002611 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002612 print_lock(this);
2613 if (forwards)
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002614 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002615 else
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002616 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002617 print_lock_name(other->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002618 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002619
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002620 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04002621
2622 /* Find a middle lock (if one exists) */
2623 depth = get_lock_depth(other);
2624 do {
2625 if (depth == 0 && (entry != root)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002626 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
Steven Rostedtdad3d742011-04-20 21:41:57 -04002627 break;
2628 }
2629 middle = entry;
2630 entry = get_lock_parent(entry);
2631 depth--;
2632 } while (entry && entry != root && (depth >= 0));
2633 if (forwards)
2634 print_irq_lock_scenario(root, other,
2635 middle ? middle->class : root->class, other->class);
2636 else
2637 print_irq_lock_scenario(other, root,
2638 middle ? middle->class : other->class, root->class);
2639
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002640 lockdep_print_held_locks(curr);
2641
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002642 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
Ming Lei24208ca2009-07-16 15:44:29 +02002643 if (!save_trace(&root->trace))
2644 return 0;
2645 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002646
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002647 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002648 dump_stack();
2649
2650 return 0;
2651}
2652
2653/*
2654 * Prove that in the forwards-direction subgraph starting at <this>
2655 * there is no lock matching <mask>:
2656 */
2657static int
2658check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2659 enum lock_usage_bit bit, const char *irqclass)
2660{
2661 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002662 struct lock_list root;
2663 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002664
Ming Leid7aaba12009-07-16 15:44:29 +02002665 root.parent = NULL;
2666 root.class = hlock_class(this);
2667 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002668 if (ret < 0)
2669 return print_bfs_bug(ret);
2670 if (ret == 1)
2671 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002672
Ming Lei24208ca2009-07-16 15:44:29 +02002673 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002674 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002675}
2676
2677/*
2678 * Prove that in the backwards-direction subgraph starting at <this>
2679 * there is no lock matching <mask>:
2680 */
2681static int
2682check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2683 enum lock_usage_bit bit, const char *irqclass)
2684{
2685 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002686 struct lock_list root;
2687 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002688
Ming Leid7aaba12009-07-16 15:44:29 +02002689 root.parent = NULL;
2690 root.class = hlock_class(this);
2691 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002692 if (ret < 0)
2693 return print_bfs_bug(ret);
2694 if (ret == 1)
2695 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002696
Ming Lei24208ca2009-07-16 15:44:29 +02002697 return print_irq_inversion_bug(curr, &root, target_entry,
Oleg Nesterov48d50672010-01-26 19:16:41 +01002698 this, 0, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002699}
2700
Ingo Molnar3117df02006-12-13 00:34:43 -08002701void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002702{
2703 printk("irq event stamp: %u\n", curr->irq_events);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002704 printk("hardirqs last enabled at (%u): [<%p>] %pS\n",
2705 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
2706 (void *)curr->hardirq_enable_ip);
2707 printk("hardirqs last disabled at (%u): [<%p>] %pS\n",
2708 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
2709 (void *)curr->hardirq_disable_ip);
2710 printk("softirqs last enabled at (%u): [<%p>] %pS\n",
2711 curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
2712 (void *)curr->softirq_enable_ip);
2713 printk("softirqs last disabled at (%u): [<%p>] %pS\n",
2714 curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
2715 (void *)curr->softirq_disable_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002716}
2717
Peter Zijlstracd953022009-01-22 16:38:21 +01002718static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002719{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002720#if HARDIRQ_VERBOSE
2721 return class_filter(class);
2722#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002723 return 0;
2724}
2725
Peter Zijlstracd953022009-01-22 16:38:21 +01002726static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002727{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002728#if SOFTIRQ_VERBOSE
2729 return class_filter(class);
2730#endif
2731 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002732}
2733
2734#define STRICT_READ_CHECKS 1
2735
Peter Zijlstracd953022009-01-22 16:38:21 +01002736static int (*state_verbose_f[])(struct lock_class *class) = {
2737#define LOCKDEP_STATE(__STATE) \
2738 __STATE##_verbose,
2739#include "lockdep_states.h"
2740#undef LOCKDEP_STATE
2741};
2742
2743static inline int state_verbose(enum lock_usage_bit bit,
2744 struct lock_class *class)
2745{
2746 return state_verbose_f[bit >> 2](class);
2747}
2748
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002749typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2750 enum lock_usage_bit bit, const char *name);
2751
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002752static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002753mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2754 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002755{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002756 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002757 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002758 int dir = new_bit & 2;
2759
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002760 /*
2761 * mark USED_IN has to look forwards -- to ensure no dependency
2762 * has ENABLED state, which would allow recursion deadlocks.
2763 *
2764 * mark ENABLED has to look backwards -- to ensure no dependee
2765 * has USED_IN state, which, again, would allow recursion deadlocks.
2766 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002767 check_usage_f usage = dir ?
2768 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002769
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002770 /*
2771 * Validate that this particular lock does not have conflicting
2772 * usage states.
2773 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002774 if (!valid_state(curr, this, new_bit, excl_bit))
2775 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002776
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002777 /*
2778 * Validate that the lock dependencies don't have conflicting usage
2779 * states.
2780 */
2781 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002782 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002783 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002784
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002785 /*
2786 * Check for read in write conflicts
2787 */
2788 if (!read) {
2789 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2790 return 0;
2791
2792 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002793 !usage(curr, this, excl_bit + 1,
2794 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002795 return 0;
2796 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002797
Peter Zijlstracd953022009-01-22 16:38:21 +01002798 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002799 return 2;
2800
2801 return 1;
2802}
2803
Nick Piggincf40bd12009-01-21 08:12:39 +01002804enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002805#define LOCKDEP_STATE(__STATE) __STATE,
2806#include "lockdep_states.h"
2807#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002808};
2809
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002810/*
2811 * Mark all held locks with a usage bit:
2812 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002813static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002814mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002815{
2816 enum lock_usage_bit usage_bit;
2817 struct held_lock *hlock;
2818 int i;
2819
2820 for (i = 0; i < curr->lockdep_depth; i++) {
2821 hlock = curr->held_locks + i;
2822
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002823 usage_bit = 2 + (mark << 2); /* ENABLED */
2824 if (hlock->read)
2825 usage_bit += 1; /* READ */
2826
2827 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002828
Oleg Nesterov34d0ed52014-01-20 19:20:13 +01002829 if (!hlock->check)
Peter Zijlstraefbe2ee2011-07-07 11:39:45 +02002830 continue;
2831
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002832 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002833 return 0;
2834 }
2835
2836 return 1;
2837}
2838
2839/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002840 * Hardirqs will be enabled:
2841 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002842static void __trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002843{
2844 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002845
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002846 /* we'll do an OFF -> ON transition: */
2847 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002848
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002849 /*
2850 * We are going to turn hardirqs on, so set the
2851 * usage bit for all held locks:
2852 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002853 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002854 return;
2855 /*
2856 * If we have softirqs enabled, then set the usage
2857 * bit for all held locks. (disabled hardirqs prevented
2858 * this bit from being set before)
2859 */
2860 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002861 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002862 return;
2863
2864 curr->hardirq_enable_ip = ip;
2865 curr->hardirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002866 debug_atomic_inc(hardirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002867}
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002868
Andi Kleenb35f8302014-02-08 08:52:02 +01002869__visible void trace_hardirqs_on_caller(unsigned long ip)
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002870{
2871 time_hardirqs_on(CALLER_ADDR0, ip);
2872
2873 if (unlikely(!debug_locks || current->lockdep_recursion))
2874 return;
2875
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002876 if (unlikely(current->hardirqs_enabled)) {
2877 /*
2878 * Neither irq nor preemption are disabled here
2879 * so this is racy by nature but losing one hit
2880 * in a stat is not a big deal.
2881 */
2882 __debug_atomic_inc(redundant_hardirqs_on);
2883 return;
2884 }
2885
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002886 /*
2887 * We're enabling irqs and according to our state above irqs weren't
2888 * already enabled, yet we find the hardware thinks they are in fact
2889 * enabled.. someone messed up their IRQ state tracing.
2890 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002891 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2892 return;
2893
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002894 /*
2895 * See the fine text that goes along with this variable definition.
2896 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002897 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2898 return;
2899
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002900 /*
2901 * Can't allow enabling interrupts while in an interrupt handler,
2902 * that's general bad form and such. Recursion, limited stack etc..
2903 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002904 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2905 return;
2906
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002907 current->lockdep_recursion = 1;
2908 __trace_hardirqs_on_caller(ip);
2909 current->lockdep_recursion = 0;
2910}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002911EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002912
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002913void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002914{
2915 trace_hardirqs_on_caller(CALLER_ADDR0);
2916}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002917EXPORT_SYMBOL(trace_hardirqs_on);
2918
2919/*
2920 * Hardirqs were disabled:
2921 */
Andi Kleenb35f8302014-02-08 08:52:02 +01002922__visible void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002923{
2924 struct task_struct *curr = current;
2925
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002926 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002927
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002928 if (unlikely(!debug_locks || current->lockdep_recursion))
2929 return;
2930
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002931 /*
2932 * So we're supposed to get called after you mask local IRQs, but for
2933 * some reason the hardware doesn't quite think you did a proper job.
2934 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002935 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2936 return;
2937
2938 if (curr->hardirqs_enabled) {
2939 /*
2940 * We have done an ON -> OFF transition:
2941 */
2942 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002943 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002944 curr->hardirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002945 debug_atomic_inc(hardirqs_off_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002946 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002947 debug_atomic_inc(redundant_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002948}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002949EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002950
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002951void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002952{
2953 trace_hardirqs_off_caller(CALLER_ADDR0);
2954}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002955EXPORT_SYMBOL(trace_hardirqs_off);
2956
2957/*
2958 * Softirqs will be enabled:
2959 */
2960void trace_softirqs_on(unsigned long ip)
2961{
2962 struct task_struct *curr = current;
2963
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002964 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002965 return;
2966
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002967 /*
2968 * We fancy IRQs being disabled here, see softirq.c, avoids
2969 * funny state and nesting things.
2970 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002971 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2972 return;
2973
2974 if (curr->softirqs_enabled) {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002975 debug_atomic_inc(redundant_softirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002976 return;
2977 }
2978
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002979 current->lockdep_recursion = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002980 /*
2981 * We'll do an OFF -> ON transition:
2982 */
2983 curr->softirqs_enabled = 1;
2984 curr->softirq_enable_ip = ip;
2985 curr->softirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002986 debug_atomic_inc(softirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002987 /*
2988 * We are going to turn softirqs on, so set the
2989 * usage bit for all held locks, if hardirqs are
2990 * enabled too:
2991 */
2992 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002993 mark_held_locks(curr, SOFTIRQ);
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002994 current->lockdep_recursion = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002995}
2996
2997/*
2998 * Softirqs were disabled:
2999 */
3000void trace_softirqs_off(unsigned long ip)
3001{
3002 struct task_struct *curr = current;
3003
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02003004 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003005 return;
3006
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003007 /*
3008 * We fancy IRQs being disabled here, see softirq.c
3009 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003010 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3011 return;
3012
3013 if (curr->softirqs_enabled) {
3014 /*
3015 * We have done an ON -> OFF transition:
3016 */
3017 curr->softirqs_enabled = 0;
3018 curr->softirq_disable_ip = ip;
3019 curr->softirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003020 debug_atomic_inc(softirqs_off_events);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003021 /*
3022 * Whoops, we wanted softirqs off, so why aren't they?
3023 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003024 DEBUG_LOCKS_WARN_ON(!softirq_count());
3025 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003026 debug_atomic_inc(redundant_softirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003027}
3028
Peter Zijlstra8e182572007-07-19 01:48:54 -07003029static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
3030{
3031 /*
3032 * If non-trylock use in a hardirq or softirq context, then
3033 * mark the lock as used in these contexts:
3034 */
3035 if (!hlock->trylock) {
3036 if (hlock->read) {
3037 if (curr->hardirq_context)
3038 if (!mark_lock(curr, hlock,
3039 LOCK_USED_IN_HARDIRQ_READ))
3040 return 0;
3041 if (curr->softirq_context)
3042 if (!mark_lock(curr, hlock,
3043 LOCK_USED_IN_SOFTIRQ_READ))
3044 return 0;
3045 } else {
3046 if (curr->hardirq_context)
3047 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
3048 return 0;
3049 if (curr->softirq_context)
3050 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
3051 return 0;
3052 }
3053 }
3054 if (!hlock->hardirqs_off) {
3055 if (hlock->read) {
3056 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003057 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003058 return 0;
3059 if (curr->softirqs_enabled)
3060 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003061 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003062 return 0;
3063 } else {
3064 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003065 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003066 return 0;
3067 if (curr->softirqs_enabled)
3068 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003069 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003070 return 0;
3071 }
3072 }
3073
3074 return 1;
3075}
3076
Boqun Fengc2469752016-02-16 13:57:40 +08003077static inline unsigned int task_irq_context(struct task_struct *task)
3078{
3079 return 2 * !!task->hardirq_context + !!task->softirq_context;
3080}
3081
Peter Zijlstra8e182572007-07-19 01:48:54 -07003082static int separate_irq_context(struct task_struct *curr,
3083 struct held_lock *hlock)
3084{
3085 unsigned int depth = curr->lockdep_depth;
3086
3087 /*
3088 * Keep track of points where we cross into an interrupt context:
3089 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003090 if (depth) {
3091 struct held_lock *prev_hlock;
3092
3093 prev_hlock = curr->held_locks + depth-1;
3094 /*
3095 * If we cross into another context, reset the
3096 * hash key (this also prevents the checking and the
3097 * adding of the dependency to 'prev'):
3098 */
3099 if (prev_hlock->irq_context != hlock->irq_context)
3100 return 1;
3101 }
3102 return 0;
3103}
3104
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003105#else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003106
3107static inline
3108int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
3109 enum lock_usage_bit new_bit)
3110{
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003111 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003112 return 1;
3113}
3114
3115static inline int mark_irqflags(struct task_struct *curr,
3116 struct held_lock *hlock)
3117{
3118 return 1;
3119}
3120
Boqun Fengc2469752016-02-16 13:57:40 +08003121static inline unsigned int task_irq_context(struct task_struct *task)
3122{
3123 return 0;
3124}
3125
Peter Zijlstra8e182572007-07-19 01:48:54 -07003126static inline int separate_irq_context(struct task_struct *curr,
3127 struct held_lock *hlock)
3128{
3129 return 0;
3130}
3131
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003132#endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003133
3134/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07003135 * Mark a lock with a usage bit, and validate the state transition:
3136 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003137static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02003138 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07003139{
3140 unsigned int new_mask = 1 << new_bit, ret = 1;
3141
3142 /*
3143 * If already set then do not dirty the cacheline,
3144 * nor do any checks:
3145 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003146 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003147 return 1;
3148
3149 if (!graph_lock())
3150 return 0;
3151 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003152 * Make sure we didn't race:
Peter Zijlstra8e182572007-07-19 01:48:54 -07003153 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003154 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07003155 graph_unlock();
3156 return 1;
3157 }
3158
Dave Jonesf82b2172008-08-11 09:30:23 +02003159 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003160
Dave Jonesf82b2172008-08-11 09:30:23 +02003161 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003162 return 0;
3163
3164 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01003165#define LOCKDEP_STATE(__STATE) \
3166 case LOCK_USED_IN_##__STATE: \
3167 case LOCK_USED_IN_##__STATE##_READ: \
3168 case LOCK_ENABLED_##__STATE: \
3169 case LOCK_ENABLED_##__STATE##_READ:
3170#include "lockdep_states.h"
3171#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07003172 ret = mark_lock_irq(curr, this, new_bit);
3173 if (!ret)
3174 return 0;
3175 break;
3176 case LOCK_USED:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003177 debug_atomic_dec(nr_unused_locks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07003178 break;
3179 default:
3180 if (!debug_locks_off_graph_unlock())
3181 return 0;
3182 WARN_ON(1);
3183 return 0;
3184 }
3185
3186 graph_unlock();
3187
3188 /*
3189 * We must printk outside of the graph_lock:
3190 */
3191 if (ret == 2) {
3192 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
3193 print_lock(this);
3194 print_irqtrace_events(curr);
3195 dump_stack();
3196 }
3197
3198 return ret;
3199}
3200
3201/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003202 * Initialize a lock instance's lock-class mapping info:
3203 */
Byungchul Parkb09be672017-08-07 16:12:52 +09003204static void __lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003205 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003206{
Yong Zhangd3d03d42011-11-09 16:04:51 +08003207 int i;
3208
3209 kmemcheck_mark_initialized(lock, sizeof(*lock));
3210
3211 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
3212 lock->class_cache[i] = NULL;
Hitoshi Mitake62016252010-10-05 18:01:51 +09003213
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003214#ifdef CONFIG_LOCK_STAT
3215 lock->cpu = raw_smp_processor_id();
3216#endif
3217
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003218 /*
3219 * Can't be having no nameless bastards around this place!
3220 */
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003221 if (DEBUG_LOCKS_WARN_ON(!name)) {
3222 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003223 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003224 }
3225
3226 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003227
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003228 /*
3229 * No key, no joy, we need to hash something.
3230 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003231 if (DEBUG_LOCKS_WARN_ON(!key))
3232 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003233 /*
3234 * Sanity check, the lock-class key must be persistent:
3235 */
3236 if (!static_obj(key)) {
3237 printk("BUG: key %p not in .data!\n", key);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003238 /*
3239 * What it says above ^^^^^, I suggest you read it.
3240 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003241 DEBUG_LOCKS_WARN_ON(1);
3242 return;
3243 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003244 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003245
3246 if (unlikely(!debug_locks))
3247 return;
3248
Peter Zijlstra35a93932015-02-26 16:23:11 +01003249 if (subclass) {
3250 unsigned long flags;
3251
3252 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3253 return;
3254
3255 raw_local_irq_save(flags);
3256 current->lockdep_recursion = 1;
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003257 register_lock_class(lock, subclass, 1);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003258 current->lockdep_recursion = 0;
3259 raw_local_irq_restore(flags);
3260 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003261}
Byungchul Parkb09be672017-08-07 16:12:52 +09003262
3263void lockdep_init_map(struct lockdep_map *lock, const char *name,
3264 struct lock_class_key *key, int subclass)
3265{
3266 cross_init(lock, 0);
3267 __lockdep_init_map(lock, name, key, subclass);
3268}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003269EXPORT_SYMBOL_GPL(lockdep_init_map);
3270
Byungchul Parkb09be672017-08-07 16:12:52 +09003271#ifdef CONFIG_LOCKDEP_CROSSRELEASE
3272void lockdep_init_map_crosslock(struct lockdep_map *lock, const char *name,
3273 struct lock_class_key *key, int subclass)
3274{
3275 cross_init(lock, 1);
3276 __lockdep_init_map(lock, name, key, subclass);
3277}
3278EXPORT_SYMBOL_GPL(lockdep_init_map_crosslock);
3279#endif
3280
Peter Zijlstra1704f472010-03-19 01:37:42 +01003281struct lock_class_key __lockdep_no_validate__;
Kent Overstreetea6749c2012-12-27 22:21:58 -08003282EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003283
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003284static int
3285print_lock_nested_lock_not_held(struct task_struct *curr,
3286 struct held_lock *hlock,
3287 unsigned long ip)
3288{
3289 if (!debug_locks_off())
3290 return 0;
3291 if (debug_locks_silent)
3292 return 0;
3293
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003294 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003295 pr_warn("==================================\n");
3296 pr_warn("WARNING: Nested lock was not taken\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003297 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003298 pr_warn("----------------------------------\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003299
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003300 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003301 print_lock(hlock);
3302
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003303 pr_warn("\nbut this task is not holding:\n");
3304 pr_warn("%s\n", hlock->nest_lock->name);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003305
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003306 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003307 dump_stack();
3308
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003309 pr_warn("\nother info that might help us debug this:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003310 lockdep_print_held_locks(curr);
3311
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003312 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003313 dump_stack();
3314
3315 return 0;
3316}
3317
Peter Zijlstraf8319482016-11-30 14:32:25 +11003318static int __lock_is_held(struct lockdep_map *lock, int read);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003319
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003320/*
3321 * This gets called for every mutex_lock*()/spin_lock*() operation.
3322 * We maintain the dependency maps and validate the locking attempt:
3323 */
3324static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3325 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003326 struct lockdep_map *nest_lock, unsigned long ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003327 int references, int pin_count)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003328{
3329 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07003330 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003331 struct held_lock *hlock;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003332 unsigned int depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003333 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003334 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003335 u64 chain_key;
Byungchul Parkb09be672017-08-07 16:12:52 +09003336 int ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003337
3338 if (unlikely(!debug_locks))
3339 return 0;
3340
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003341 /*
3342 * Lockdep should run with IRQs disabled, otherwise we could
3343 * get an interrupt which would want to take locks, which would
3344 * end up in lockdep and have you got a head-ache already?
3345 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003346 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3347 return 0;
3348
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003349 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3350 check = 0;
Peter Zijlstra1704f472010-03-19 01:37:42 +01003351
Hitoshi Mitake62016252010-10-05 18:01:51 +09003352 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3353 class = lock->class_cache[subclass];
Ingo Molnard6d897c2006-07-10 04:44:04 -07003354 /*
Hitoshi Mitake62016252010-10-05 18:01:51 +09003355 * Not cached?
Ingo Molnard6d897c2006-07-10 04:44:04 -07003356 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003357 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003358 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003359 if (!class)
3360 return 0;
3361 }
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003362 atomic_inc((atomic_t *)&class->ops);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003363 if (very_verbose(class)) {
3364 printk("\nacquire class [%p] %s", class->key, class->name);
3365 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01003366 printk(KERN_CONT "#%d", class->name_version);
3367 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003368 dump_stack();
3369 }
3370
3371 /*
3372 * Add the lock to the list of currently held locks.
3373 * (we dont increase the depth just yet, up until the
3374 * dependency checks are done)
3375 */
3376 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003377 /*
3378 * Ran out of static storage for our per-task lock stack again have we?
3379 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003380 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3381 return 0;
3382
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003383 class_idx = class - lock_classes + 1;
3384
Byungchul Parkb09be672017-08-07 16:12:52 +09003385 /* TODO: nest_lock is not implemented for crosslock yet. */
3386 if (depth && !cross_lock(lock)) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003387 hlock = curr->held_locks + depth - 1;
3388 if (hlock->class_idx == class_idx && nest_lock) {
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003389 if (hlock->references) {
3390 /*
3391 * Check: unsigned int references:12, overflow.
3392 */
3393 if (DEBUG_LOCKS_WARN_ON(hlock->references == (1 << 12)-1))
3394 return 0;
3395
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003396 hlock->references++;
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003397 } else {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003398 hlock->references = 2;
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003399 }
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003400
3401 return 1;
3402 }
3403 }
3404
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003405 hlock = curr->held_locks + depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003406 /*
3407 * Plain impossible, we just registered it and checked it weren't no
3408 * NULL like.. I bet this mushroom I ate was good!
3409 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003410 if (DEBUG_LOCKS_WARN_ON(!class))
3411 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003412 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003413 hlock->acquire_ip = ip;
3414 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003415 hlock->nest_lock = nest_lock;
Boqun Fengc2469752016-02-16 13:57:40 +08003416 hlock->irq_context = task_irq_context(curr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003417 hlock->trylock = trylock;
3418 hlock->read = read;
3419 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04003420 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003421 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003422#ifdef CONFIG_LOCK_STAT
3423 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003424 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003425#endif
Peter Zijlstra21199f22015-09-16 16:10:40 +02003426 hlock->pin_count = pin_count;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003427
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003428 if (check && !mark_irqflags(curr, hlock))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003429 return 0;
3430
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003431 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07003432 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003433 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003434
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003435 /*
Gautham R Shenoy17aacfb92007-10-28 20:47:01 +01003436 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003437 * lock keys along the dependency chain. We save the hash value
3438 * at every step so that we can get the current hash easily
3439 * after unlock. The chain hash is then used to cache dependency
3440 * results.
3441 *
3442 * The 'key ID' is what is the most compact key value to drive
3443 * the hash, not class->key.
3444 */
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003445 /*
3446 * Whoops, we did it again.. ran straight out of our static allocation.
3447 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003448 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003449 return 0;
3450
3451 chain_key = curr->curr_chain_key;
3452 if (!depth) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003453 /*
3454 * How can we have a chain hash when we ain't got no keys?!
3455 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003456 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3457 return 0;
3458 chain_head = 1;
3459 }
3460
3461 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003462 if (separate_irq_context(curr, hlock)) {
3463 chain_key = 0;
3464 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003465 }
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003466 chain_key = iterate_chain_key(chain_key, class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003467
Peter Zijlstraf8319482016-11-30 14:32:25 +11003468 if (nest_lock && !__lock_is_held(nest_lock, -1))
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003469 return print_lock_nested_lock_not_held(curr, hlock, ip);
3470
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003471 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003472 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08003473
Byungchul Parkb09be672017-08-07 16:12:52 +09003474 ret = lock_acquire_crosslock(hlock);
3475 /*
3476 * 2 means normal acquire operations are needed. Otherwise, it's
3477 * ok just to return with '0:fail, 1:success'.
3478 */
3479 if (ret != 2)
3480 return ret;
3481
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003482 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003483 curr->lockdep_depth++;
3484 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08003485#ifdef CONFIG_DEBUG_LOCKDEP
3486 if (unlikely(!debug_locks))
3487 return 0;
3488#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003489 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3490 debug_locks_off();
Dave Jones2c522832013-04-25 13:40:02 -04003491 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3492 printk(KERN_DEBUG "depth: %i max: %lu!\n",
Ben Greearc0540602013-02-06 10:56:19 -08003493 curr->lockdep_depth, MAX_LOCK_DEPTH);
Ben Greearc0540602013-02-06 10:56:19 -08003494
3495 lockdep_print_held_locks(current);
3496 debug_show_all_locks();
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01003497 dump_stack();
Ben Greearc0540602013-02-06 10:56:19 -08003498
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003499 return 0;
3500 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08003501
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003502 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3503 max_lockdep_depth = curr->lockdep_depth;
3504
3505 return 1;
3506}
3507
3508static int
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303509print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003510 unsigned long ip)
3511{
3512 if (!debug_locks_off())
3513 return 0;
3514 if (debug_locks_silent)
3515 return 0;
3516
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003517 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003518 pr_warn("=====================================\n");
3519 pr_warn("WARNING: bad unlock balance detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003520 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003521 pr_warn("-------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003522 pr_warn("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003523 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003524 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003525 pr_cont(") at:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003526 print_ip_sym(ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003527 pr_warn("but there are no more locks to release!\n");
3528 pr_warn("\nother info that might help us debug this:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003529 lockdep_print_held_locks(curr);
3530
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003531 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003532 dump_stack();
3533
3534 return 0;
3535}
3536
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003537static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3538{
3539 if (hlock->instance == lock)
3540 return 1;
3541
3542 if (hlock->references) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09003543 struct lock_class *class = lock->class_cache[0];
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003544
3545 if (!class)
3546 class = look_up_lock_class(lock, 0);
3547
Peter Zijlstra80e04012011-08-05 14:26:17 +02003548 /*
3549 * If look_up_lock_class() failed to find a class, we're trying
3550 * to test if we hold a lock that has never yet been acquired.
3551 * Clearly if the lock hasn't been acquired _ever_, we're not
3552 * holding it either, so report failure.
3553 */
Thomas Gleixner383776f2017-02-27 15:37:36 +01003554 if (IS_ERR_OR_NULL(class))
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003555 return 0;
3556
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003557 /*
3558 * References, but not a lock we're actually ref-counting?
3559 * State got messed up, follow the sites that change ->references
3560 * and try to make sense of it.
3561 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003562 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3563 return 0;
3564
3565 if (hlock->class_idx == class - lock_classes + 1)
3566 return 1;
3567 }
3568
3569 return 0;
3570}
3571
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003572/* @depth must not be zero */
3573static struct held_lock *find_held_lock(struct task_struct *curr,
3574 struct lockdep_map *lock,
3575 unsigned int depth, int *idx)
3576{
3577 struct held_lock *ret, *hlock, *prev_hlock;
3578 int i;
3579
3580 i = depth - 1;
3581 hlock = curr->held_locks + i;
3582 ret = hlock;
3583 if (match_held_lock(hlock, lock))
3584 goto out;
3585
3586 ret = NULL;
3587 for (i--, prev_hlock = hlock--;
3588 i >= 0;
3589 i--, prev_hlock = hlock--) {
3590 /*
3591 * We must not cross into another context:
3592 */
3593 if (prev_hlock->irq_context != hlock->irq_context) {
3594 ret = NULL;
3595 break;
3596 }
3597 if (match_held_lock(hlock, lock)) {
3598 ret = hlock;
3599 break;
3600 }
3601 }
3602
3603out:
3604 *idx = i;
3605 return ret;
3606}
3607
J. R. Okajimae9699702017-02-03 01:38:16 +09003608static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
3609 int idx)
3610{
3611 struct held_lock *hlock;
3612
3613 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
3614 if (!__lock_acquire(hlock->instance,
3615 hlock_class(hlock)->subclass,
3616 hlock->trylock,
3617 hlock->read, hlock->check,
3618 hlock->hardirqs_off,
3619 hlock->nest_lock, hlock->acquire_ip,
3620 hlock->references, hlock->pin_count))
3621 return 1;
3622 }
3623 return 0;
3624}
3625
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003626static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003627__lock_set_class(struct lockdep_map *lock, const char *name,
3628 struct lock_class_key *key, unsigned int subclass,
3629 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003630{
3631 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003632 struct held_lock *hlock;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003633 struct lock_class *class;
3634 unsigned int depth;
3635 int i;
3636
3637 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003638 /*
3639 * This function is about (re)setting the class of a held lock,
3640 * yet we're not actually holding any locks. Naughty user!
3641 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003642 if (DEBUG_LOCKS_WARN_ON(!depth))
3643 return 0;
3644
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003645 hlock = find_held_lock(curr, lock, depth, &i);
3646 if (!hlock)
3647 return print_unlock_imbalance_bug(curr, lock, ip);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003648
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003649 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003650 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02003651 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003652
3653 curr->lockdep_depth = i;
3654 curr->curr_chain_key = hlock->prev_chain_key;
3655
J. R. Okajimae9699702017-02-03 01:38:16 +09003656 if (reacquire_held_locks(curr, depth, i))
3657 return 0;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003658
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003659 /*
3660 * I took it apart and put it back together again, except now I have
3661 * these 'spare' parts.. where shall I put them.
3662 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003663 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3664 return 0;
3665 return 1;
3666}
3667
J. R. Okajima6419c4a2017-02-03 01:38:17 +09003668static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3669{
3670 struct task_struct *curr = current;
3671 struct held_lock *hlock;
3672 unsigned int depth;
3673 int i;
3674
3675 depth = curr->lockdep_depth;
3676 /*
3677 * This function is about (re)setting the class of a held lock,
3678 * yet we're not actually holding any locks. Naughty user!
3679 */
3680 if (DEBUG_LOCKS_WARN_ON(!depth))
3681 return 0;
3682
3683 hlock = find_held_lock(curr, lock, depth, &i);
3684 if (!hlock)
3685 return print_unlock_imbalance_bug(curr, lock, ip);
3686
3687 curr->lockdep_depth = i;
3688 curr->curr_chain_key = hlock->prev_chain_key;
3689
3690 WARN(hlock->read, "downgrading a read lock");
3691 hlock->read = 1;
3692 hlock->acquire_ip = ip;
3693
3694 if (reacquire_held_locks(curr, depth, i))
3695 return 0;
3696
3697 /*
3698 * I took it apart and put it back together again, except now I have
3699 * these 'spare' parts.. where shall I put them.
3700 */
3701 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3702 return 0;
3703 return 1;
3704}
3705
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003706/*
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003707 * Remove the lock to the list of currently held locks - this gets
3708 * called on mutex_unlock()/spin_unlock*() (or on a failed
3709 * mutex_lock_interruptible()).
3710 *
3711 * @nested is an hysterical artifact, needs a tree wide cleanup.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003712 */
3713static int
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003714__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003715{
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003716 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003717 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003718 unsigned int depth;
Byungchul Parkb09be672017-08-07 16:12:52 +09003719 int ret, i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003720
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003721 if (unlikely(!debug_locks))
3722 return 0;
3723
Byungchul Parkb09be672017-08-07 16:12:52 +09003724 ret = lock_release_crosslock(lock);
3725 /*
3726 * 2 means normal release operations are needed. Otherwise, it's
3727 * ok just to return with '0:fail, 1:success'.
3728 */
3729 if (ret != 2)
3730 return ret;
3731
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003732 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003733 /*
3734 * So we're all set to release this lock.. wait what lock? We don't
3735 * own any locks, you've been drinking again?
3736 */
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003737 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3738 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003739
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003740 /*
3741 * Check whether the lock exists in the current stack
3742 * of held locks:
3743 */
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003744 hlock = find_held_lock(curr, lock, depth, &i);
3745 if (!hlock)
3746 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003747
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003748 if (hlock->instance == lock)
3749 lock_release_holdtime(hlock);
3750
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003751 WARN(hlock->pin_count, "releasing a pinned lock\n");
3752
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003753 if (hlock->references) {
3754 hlock->references--;
3755 if (hlock->references) {
3756 /*
3757 * We had, and after removing one, still have
3758 * references, the current lock stack is still
3759 * valid. We're done!
3760 */
3761 return 1;
3762 }
3763 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003764
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003765 /*
3766 * We have the right lock to unlock, 'hlock' points to it.
3767 * Now we remove it from the stack, and add back the other
3768 * entries (if any), recalculating the hash along the way:
3769 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003770
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003771 curr->lockdep_depth = i;
3772 curr->curr_chain_key = hlock->prev_chain_key;
3773
J. R. Okajimae9699702017-02-03 01:38:16 +09003774 if (reacquire_held_locks(curr, depth, i + 1))
3775 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003776
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003777 /*
3778 * We had N bottles of beer on the wall, we drank one, but now
3779 * there's not N-1 bottles of beer left on the wall...
3780 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003781 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3782 return 0;
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003783
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003784 return 1;
3785}
3786
Peter Zijlstraf8319482016-11-30 14:32:25 +11003787static int __lock_is_held(struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02003788{
3789 struct task_struct *curr = current;
3790 int i;
3791
3792 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003793 struct held_lock *hlock = curr->held_locks + i;
3794
Peter Zijlstraf8319482016-11-30 14:32:25 +11003795 if (match_held_lock(hlock, lock)) {
3796 if (read == -1 || hlock->read == read)
3797 return 1;
3798
3799 return 0;
3800 }
Peter Zijlstraf607c662009-07-20 19:16:29 +02003801 }
3802
3803 return 0;
3804}
3805
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003806static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
3807{
3808 struct pin_cookie cookie = NIL_COOKIE;
3809 struct task_struct *curr = current;
3810 int i;
3811
3812 if (unlikely(!debug_locks))
3813 return cookie;
3814
3815 for (i = 0; i < curr->lockdep_depth; i++) {
3816 struct held_lock *hlock = curr->held_locks + i;
3817
3818 if (match_held_lock(hlock, lock)) {
3819 /*
3820 * Grab 16bits of randomness; this is sufficient to not
3821 * be guessable and still allows some pin nesting in
3822 * our u32 pin_count.
3823 */
3824 cookie.val = 1 + (prandom_u32() >> 16);
3825 hlock->pin_count += cookie.val;
3826 return cookie;
3827 }
3828 }
3829
3830 WARN(1, "pinning an unheld lock\n");
3831 return cookie;
3832}
3833
3834static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003835{
3836 struct task_struct *curr = current;
3837 int i;
3838
3839 if (unlikely(!debug_locks))
3840 return;
3841
3842 for (i = 0; i < curr->lockdep_depth; i++) {
3843 struct held_lock *hlock = curr->held_locks + i;
3844
3845 if (match_held_lock(hlock, lock)) {
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003846 hlock->pin_count += cookie.val;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003847 return;
3848 }
3849 }
3850
3851 WARN(1, "pinning an unheld lock\n");
3852}
3853
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003854static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003855{
3856 struct task_struct *curr = current;
3857 int i;
3858
3859 if (unlikely(!debug_locks))
3860 return;
3861
3862 for (i = 0; i < curr->lockdep_depth; i++) {
3863 struct held_lock *hlock = curr->held_locks + i;
3864
3865 if (match_held_lock(hlock, lock)) {
3866 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
3867 return;
3868
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003869 hlock->pin_count -= cookie.val;
3870
3871 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
3872 hlock->pin_count = 0;
3873
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003874 return;
3875 }
3876 }
3877
3878 WARN(1, "unpinning an unheld lock\n");
3879}
3880
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003881/*
3882 * Check whether we follow the irq-flags state precisely:
3883 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003884static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003885{
Ingo Molnar992860e2008-07-14 10:28:38 +02003886#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3887 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003888 if (!debug_locks)
3889 return;
3890
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01003891 if (irqs_disabled_flags(flags)) {
3892 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3893 printk("possible reason: unannotated irqs-off.\n");
3894 }
3895 } else {
3896 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3897 printk("possible reason: unannotated irqs-on.\n");
3898 }
3899 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003900
3901 /*
3902 * We dont accurately track softirq state in e.g.
3903 * hardirq contexts (such as on 4KSTACKS), so only
3904 * check if not in hardirq contexts:
3905 */
3906 if (!hardirq_count()) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003907 if (softirq_count()) {
3908 /* like the above, but with softirqs */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003909 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003910 } else {
3911 /* lick the above, does it taste good? */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003912 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003913 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003914 }
3915
3916 if (!debug_locks)
3917 print_irqtrace_events(current);
3918#endif
3919}
3920
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003921void lock_set_class(struct lockdep_map *lock, const char *name,
3922 struct lock_class_key *key, unsigned int subclass,
3923 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003924{
3925 unsigned long flags;
3926
3927 if (unlikely(current->lockdep_recursion))
3928 return;
3929
3930 raw_local_irq_save(flags);
3931 current->lockdep_recursion = 1;
3932 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003933 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003934 check_chain_key(current);
3935 current->lockdep_recursion = 0;
3936 raw_local_irq_restore(flags);
3937}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003938EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003939
J. R. Okajima6419c4a2017-02-03 01:38:17 +09003940void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3941{
3942 unsigned long flags;
3943
3944 if (unlikely(current->lockdep_recursion))
3945 return;
3946
3947 raw_local_irq_save(flags);
3948 current->lockdep_recursion = 1;
3949 check_flags(flags);
3950 if (__lock_downgrade(lock, ip))
3951 check_chain_key(current);
3952 current->lockdep_recursion = 0;
3953 raw_local_irq_restore(flags);
3954}
3955EXPORT_SYMBOL_GPL(lock_downgrade);
3956
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003957/*
3958 * We are not always called with irqs disabled - do that here,
3959 * and also avoid lockdep recursion:
3960 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003961void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003962 int trylock, int read, int check,
3963 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003964{
3965 unsigned long flags;
3966
3967 if (unlikely(current->lockdep_recursion))
3968 return;
3969
3970 raw_local_irq_save(flags);
3971 check_flags(flags);
3972
3973 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003974 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003975 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003976 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003977 current->lockdep_recursion = 0;
3978 raw_local_irq_restore(flags);
3979}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003980EXPORT_SYMBOL_GPL(lock_acquire);
3981
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003982void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003983 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003984{
3985 unsigned long flags;
3986
3987 if (unlikely(current->lockdep_recursion))
3988 return;
3989
3990 raw_local_irq_save(flags);
3991 check_flags(flags);
3992 current->lockdep_recursion = 1;
Frederic Weisbecker93135432010-05-08 06:24:25 +02003993 trace_lock_release(lock, ip);
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003994 if (__lock_release(lock, nested, ip))
3995 check_chain_key(current);
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_release);
4000
Peter Zijlstraf8319482016-11-30 14:32:25 +11004001int lock_is_held_type(struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02004002{
4003 unsigned long flags;
4004 int ret = 0;
4005
4006 if (unlikely(current->lockdep_recursion))
Peter Zijlstraf2513cd2011-06-06 12:32:43 +02004007 return 1; /* avoid false negative lockdep_assert_held() */
Peter Zijlstraf607c662009-07-20 19:16:29 +02004008
4009 raw_local_irq_save(flags);
4010 check_flags(flags);
4011
4012 current->lockdep_recursion = 1;
Peter Zijlstraf8319482016-11-30 14:32:25 +11004013 ret = __lock_is_held(lock, read);
Peter Zijlstraf607c662009-07-20 19:16:29 +02004014 current->lockdep_recursion = 0;
4015 raw_local_irq_restore(flags);
4016
4017 return ret;
4018}
Peter Zijlstraf8319482016-11-30 14:32:25 +11004019EXPORT_SYMBOL_GPL(lock_is_held_type);
Peter Zijlstraf607c662009-07-20 19:16:29 +02004020
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004021struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004022{
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004023 struct pin_cookie cookie = NIL_COOKIE;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004024 unsigned long flags;
4025
4026 if (unlikely(current->lockdep_recursion))
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004027 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004028
4029 raw_local_irq_save(flags);
4030 check_flags(flags);
4031
4032 current->lockdep_recursion = 1;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004033 cookie = __lock_pin_lock(lock);
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004034 current->lockdep_recursion = 0;
4035 raw_local_irq_restore(flags);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004036
4037 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004038}
4039EXPORT_SYMBOL_GPL(lock_pin_lock);
4040
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004041void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004042{
4043 unsigned long flags;
4044
4045 if (unlikely(current->lockdep_recursion))
4046 return;
4047
4048 raw_local_irq_save(flags);
4049 check_flags(flags);
4050
4051 current->lockdep_recursion = 1;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004052 __lock_repin_lock(lock, cookie);
4053 current->lockdep_recursion = 0;
4054 raw_local_irq_restore(flags);
4055}
4056EXPORT_SYMBOL_GPL(lock_repin_lock);
4057
4058void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
4059{
4060 unsigned long flags;
4061
4062 if (unlikely(current->lockdep_recursion))
4063 return;
4064
4065 raw_local_irq_save(flags);
4066 check_flags(flags);
4067
4068 current->lockdep_recursion = 1;
4069 __lock_unpin_lock(lock, cookie);
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004070 current->lockdep_recursion = 0;
4071 raw_local_irq_restore(flags);
4072}
4073EXPORT_SYMBOL_GPL(lock_unpin_lock);
4074
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004075#ifdef CONFIG_LOCK_STAT
4076static int
4077print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
4078 unsigned long ip)
4079{
4080 if (!debug_locks_off())
4081 return 0;
4082 if (debug_locks_silent)
4083 return 0;
4084
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004085 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004086 pr_warn("=================================\n");
4087 pr_warn("WARNING: bad contention detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004088 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004089 pr_warn("---------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004090 pr_warn("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004091 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004092 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004093 pr_cont(") at:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004094 print_ip_sym(ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004095 pr_warn("but there are no locks held!\n");
4096 pr_warn("\nother info that might help us debug this:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004097 lockdep_print_held_locks(curr);
4098
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004099 pr_warn("\nstack backtrace:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004100 dump_stack();
4101
4102 return 0;
4103}
4104
4105static void
4106__lock_contended(struct lockdep_map *lock, unsigned long ip)
4107{
4108 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004109 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004110 struct lock_class_stats *stats;
4111 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004112 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004113
4114 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004115 /*
4116 * Whee, we contended on this lock, except it seems we're not
4117 * actually trying to acquire anything much at all..
4118 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004119 if (DEBUG_LOCKS_WARN_ON(!depth))
4120 return;
4121
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004122 hlock = find_held_lock(curr, lock, depth, &i);
4123 if (!hlock) {
4124 print_lock_contention_bug(curr, lock, ip);
4125 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004126 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004127
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004128 if (hlock->instance != lock)
4129 return;
4130
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004131 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004132
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004133 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
4134 contending_point = lock_point(hlock_class(hlock)->contending_point,
4135 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004136
Dave Jonesf82b2172008-08-11 09:30:23 +02004137 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004138 if (contention_point < LOCKSTAT_POINTS)
4139 stats->contention_point[contention_point]++;
4140 if (contending_point < LOCKSTAT_POINTS)
4141 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07004142 if (lock->cpu != smp_processor_id())
4143 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004144 put_lock_stats(stats);
4145}
4146
4147static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004148__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004149{
4150 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004151 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004152 struct lock_class_stats *stats;
4153 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004154 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07004155 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004156
4157 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004158 /*
4159 * Yay, we acquired ownership of this lock we didn't try to
4160 * acquire, how the heck did that happen?
4161 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004162 if (DEBUG_LOCKS_WARN_ON(!depth))
4163 return;
4164
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004165 hlock = find_held_lock(curr, lock, depth, &i);
4166 if (!hlock) {
4167 print_lock_contention_bug(curr, lock, _RET_IP_);
4168 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004169 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004170
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004171 if (hlock->instance != lock)
4172 return;
4173
Peter Zijlstra96645672007-07-19 01:49:00 -07004174 cpu = smp_processor_id();
4175 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004176 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07004177 waittime = now - hlock->waittime_stamp;
4178 hlock->holdtime_stamp = now;
4179 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004180
Frederic Weisbecker883a2a32010-05-08 06:16:11 +02004181 trace_lock_acquired(lock, ip);
Frederic Weisbecker20625012009-04-06 01:49:33 +02004182
Dave Jonesf82b2172008-08-11 09:30:23 +02004183 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07004184 if (waittime) {
4185 if (hlock->read)
4186 lock_time_inc(&stats->read_waittime, waittime);
4187 else
4188 lock_time_inc(&stats->write_waittime, waittime);
4189 }
4190 if (lock->cpu != cpu)
4191 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004192 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07004193
4194 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004195 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004196}
4197
4198void lock_contended(struct lockdep_map *lock, unsigned long ip)
4199{
4200 unsigned long flags;
4201
4202 if (unlikely(!lock_stat))
4203 return;
4204
4205 if (unlikely(current->lockdep_recursion))
4206 return;
4207
4208 raw_local_irq_save(flags);
4209 check_flags(flags);
4210 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01004211 trace_lock_contended(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004212 __lock_contended(lock, ip);
4213 current->lockdep_recursion = 0;
4214 raw_local_irq_restore(flags);
4215}
4216EXPORT_SYMBOL_GPL(lock_contended);
4217
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004218void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004219{
4220 unsigned long flags;
4221
4222 if (unlikely(!lock_stat))
4223 return;
4224
4225 if (unlikely(current->lockdep_recursion))
4226 return;
4227
4228 raw_local_irq_save(flags);
4229 check_flags(flags);
4230 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004231 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004232 current->lockdep_recursion = 0;
4233 raw_local_irq_restore(flags);
4234}
4235EXPORT_SYMBOL_GPL(lock_acquired);
4236#endif
4237
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004238/*
4239 * Used by the testsuite, sanitize the validator state
4240 * after a simulated failure:
4241 */
4242
4243void lockdep_reset(void)
4244{
4245 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08004246 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004247
4248 raw_local_irq_save(flags);
4249 current->curr_chain_key = 0;
4250 current->lockdep_depth = 0;
4251 current->lockdep_recursion = 0;
4252 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
4253 nr_hardirq_chains = 0;
4254 nr_softirq_chains = 0;
4255 nr_process_chains = 0;
4256 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08004257 for (i = 0; i < CHAINHASH_SIZE; i++)
Andrew Mortona63f38c2016-02-03 13:44:12 -08004258 INIT_HLIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004259 raw_local_irq_restore(flags);
4260}
4261
4262static void zap_class(struct lock_class *class)
4263{
4264 int i;
4265
4266 /*
4267 * Remove all dependencies this lock is
4268 * involved in:
4269 */
4270 for (i = 0; i < nr_list_entries; i++) {
4271 if (list_entries[i].class == class)
4272 list_del_rcu(&list_entries[i].entry);
4273 }
4274 /*
4275 * Unhash the class and remove it from the all_lock_classes list:
4276 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08004277 hlist_del_rcu(&class->hash_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004278 list_del_rcu(&class->lock_entry);
4279
Peter Zijlstracee34d82015-06-02 12:50:13 +02004280 RCU_INIT_POINTER(class->key, NULL);
4281 RCU_INIT_POINTER(class->name, NULL);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004282}
4283
Arjan van de Venfabe8742008-01-24 07:00:45 +01004284static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004285{
4286 return addr >= start && addr < start + size;
4287}
4288
Peter Zijlstra35a93932015-02-26 16:23:11 +01004289/*
4290 * Used in module.c to remove lock classes from memory that is going to be
4291 * freed; and possibly re-used by other modules.
4292 *
4293 * We will have had one sync_sched() before getting here, so we're guaranteed
4294 * nobody will look up these exact classes -- they're properly dead but still
4295 * allocated.
4296 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004297void lockdep_free_key_range(void *start, unsigned long size)
4298{
Peter Zijlstra35a93932015-02-26 16:23:11 +01004299 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004300 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004301 unsigned long flags;
4302 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01004303 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004304
4305 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01004306 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004307
4308 /*
4309 * Unhash all classes that were created by this module:
4310 */
4311 for (i = 0; i < CLASSHASH_SIZE; i++) {
4312 head = classhash_table + i;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004313 hlist_for_each_entry_rcu(class, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004314 if (within(class->key, start, size))
4315 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01004316 else if (within(class->name, start, size))
4317 zap_class(class);
4318 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004319 }
4320
Nick Piggin5a26db52008-01-16 09:51:58 +01004321 if (locked)
4322 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004323 raw_local_irq_restore(flags);
Peter Zijlstra35a93932015-02-26 16:23:11 +01004324
4325 /*
4326 * Wait for any possible iterators from look_up_lock_class() to pass
4327 * before continuing to free the memory they refer to.
4328 *
4329 * sync_sched() is sufficient because the read-side is IRQ disable.
4330 */
4331 synchronize_sched();
4332
4333 /*
4334 * XXX at this point we could return the resources to the pool;
4335 * instead we leak them. We would need to change to bitmap allocators
4336 * instead of the linear allocators we have now.
4337 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004338}
4339
4340void lockdep_reset_lock(struct lockdep_map *lock)
4341{
Peter Zijlstra35a93932015-02-26 16:23:11 +01004342 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004343 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004344 unsigned long flags;
4345 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01004346 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004347
4348 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004349
4350 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07004351 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004352 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07004353 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
4354 /*
4355 * If the class exists we look it up and zap it:
4356 */
4357 class = look_up_lock_class(lock, j);
Thomas Gleixner383776f2017-02-27 15:37:36 +01004358 if (!IS_ERR_OR_NULL(class))
Ingo Molnard6d897c2006-07-10 04:44:04 -07004359 zap_class(class);
4360 }
4361 /*
4362 * Debug check: in the end all mapped classes should
4363 * be gone.
4364 */
Nick Piggin5a26db52008-01-16 09:51:58 +01004365 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004366 for (i = 0; i < CLASSHASH_SIZE; i++) {
4367 head = classhash_table + i;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004368 hlist_for_each_entry_rcu(class, head, hash_entry) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09004369 int match = 0;
4370
4371 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
4372 match |= class == lock->class_cache[j];
4373
4374 if (unlikely(match)) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004375 if (debug_locks_off_graph_unlock()) {
4376 /*
4377 * We all just reset everything, how did it match?
4378 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08004379 WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004380 }
Ingo Molnard6d897c2006-07-10 04:44:04 -07004381 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004382 }
4383 }
4384 }
Nick Piggin5a26db52008-01-16 09:51:58 +01004385 if (locked)
4386 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07004387
4388out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004389 raw_local_irq_restore(flags);
4390}
4391
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004392void __init lockdep_info(void)
4393{
4394 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4395
Li Zefanb0788ca2008-11-21 15:57:32 +08004396 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004397 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4398 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08004399 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004400 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4401 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4402 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4403
4404 printk(" memory used by lock dependency info: %lu kB\n",
4405 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
4406 sizeof(struct list_head) * CLASSHASH_SIZE +
4407 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
4408 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei906292092009-08-02 21:43:36 +08004409 sizeof(struct list_head) * CHAINHASH_SIZE
Ming Lei4dd861d2009-07-16 15:44:29 +02004410#ifdef CONFIG_PROVE_LOCKING
Ming Leie351b662009-07-22 22:48:09 +08004411 + sizeof(struct circular_queue)
Ming Lei4dd861d2009-07-16 15:44:29 +02004412#endif
Ming Lei906292092009-08-02 21:43:36 +08004413 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02004414 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004415
4416 printk(" per task-struct memory footprint: %lu bytes\n",
4417 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004418}
4419
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004420static void
4421print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07004422 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004423{
4424 if (!debug_locks_off())
4425 return;
4426 if (debug_locks_silent)
4427 return;
4428
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004429 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004430 pr_warn("=========================\n");
4431 pr_warn("WARNING: held lock freed!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004432 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004433 pr_warn("-------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004434 pr_warn("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004435 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07004436 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004437 lockdep_print_held_locks(curr);
4438
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004439 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004440 dump_stack();
4441}
4442
Oleg Nesterov54561782007-12-05 15:46:09 +01004443static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4444 const void* lock_from, unsigned long lock_len)
4445{
4446 return lock_from + lock_len <= mem_from ||
4447 mem_from + mem_len <= lock_from;
4448}
4449
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004450/*
4451 * Called when kernel memory is freed (or unmapped), or if a lock
4452 * is destroyed or reinitialized - this code checks whether there is
4453 * any held lock in the memory range of <from> to <to>:
4454 */
4455void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4456{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004457 struct task_struct *curr = current;
4458 struct held_lock *hlock;
4459 unsigned long flags;
4460 int i;
4461
4462 if (unlikely(!debug_locks))
4463 return;
4464
4465 local_irq_save(flags);
4466 for (i = 0; i < curr->lockdep_depth; i++) {
4467 hlock = curr->held_locks + i;
4468
Oleg Nesterov54561782007-12-05 15:46:09 +01004469 if (not_in_range(mem_from, mem_len, hlock->instance,
4470 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004471 continue;
4472
Oleg Nesterov54561782007-12-05 15:46:09 +01004473 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004474 break;
4475 }
4476 local_irq_restore(flags);
4477}
Peter Zijlstraed075362006-12-06 20:35:24 -08004478EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004479
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004480static void print_held_locks_bug(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004481{
4482 if (!debug_locks_off())
4483 return;
4484 if (debug_locks_silent)
4485 return;
4486
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004487 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004488 pr_warn("====================================\n");
4489 pr_warn("WARNING: %s/%d still has locks held!\n",
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004490 current->comm, task_pid_nr(current));
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004491 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004492 pr_warn("------------------------------------\n");
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004493 lockdep_print_held_locks(current);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004494 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004495 dump_stack();
4496}
4497
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004498void debug_check_no_locks_held(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004499{
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004500 if (unlikely(current->lockdep_depth > 0))
4501 print_held_locks_bug();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004502}
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004503EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004504
Sasha Levin8dce7a92013-06-13 18:41:16 -04004505#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004506void debug_show_all_locks(void)
4507{
4508 struct task_struct *g, *p;
4509 int count = 10;
4510 int unlock = 1;
4511
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004512 if (unlikely(!debug_locks)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004513 pr_warn("INFO: lockdep is turned off.\n");
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004514 return;
4515 }
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004516 pr_warn("\nShowing all locks held in the system:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004517
4518 /*
4519 * Here we try to get the tasklist_lock as hard as possible,
4520 * if not successful after 2 seconds we ignore it (but keep
4521 * trying). This is to enable a debug printout even if a
4522 * tasklist_lock-holding task deadlocks or crashes.
4523 */
4524retry:
4525 if (!read_trylock(&tasklist_lock)) {
4526 if (count == 10)
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004527 pr_warn("hm, tasklist_lock locked, retrying... ");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004528 if (count) {
4529 count--;
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004530 pr_cont(" #%d", 10-count);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004531 mdelay(200);
4532 goto retry;
4533 }
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004534 pr_cont(" ignoring it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004535 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08004536 } else {
4537 if (count != 10)
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004538 pr_cont(" locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004539 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004540
4541 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01004542 /*
4543 * It's not reliable to print a task's held locks
4544 * if it's not sleeping (or if it's not the current
4545 * task):
4546 */
4547 if (p->state == TASK_RUNNING && p != current)
4548 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004549 if (p->lockdep_depth)
4550 lockdep_print_held_locks(p);
4551 if (!unlock)
4552 if (read_trylock(&tasklist_lock))
4553 unlock = 1;
4554 } while_each_thread(g, p);
4555
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004556 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004557 pr_warn("=============================================\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004558
4559 if (unlock)
4560 read_unlock(&tasklist_lock);
4561}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004562EXPORT_SYMBOL_GPL(debug_show_all_locks);
Sasha Levin8dce7a92013-06-13 18:41:16 -04004563#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004564
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004565/*
4566 * Careful: only use this function if you are sure that
4567 * the task cannot run in parallel!
4568 */
John Kacurf1b499f2010-08-05 17:10:53 +02004569void debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004570{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004571 if (unlikely(!debug_locks)) {
4572 printk("INFO: lockdep is turned off.\n");
4573 return;
4574 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004575 lockdep_print_held_locks(task);
4576}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004577EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02004578
Andi Kleen722a9f92014-05-02 00:44:38 +02004579asmlinkage __visible void lockdep_sys_exit(void)
Peter Zijlstrab351d162007-10-11 22:11:12 +02004580{
4581 struct task_struct *curr = current;
4582
4583 if (unlikely(curr->lockdep_depth)) {
4584 if (!debug_locks_off())
4585 return;
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004586 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004587 pr_warn("================================================\n");
4588 pr_warn("WARNING: lock held when returning to user space!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004589 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004590 pr_warn("------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004591 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
Peter Zijlstrab351d162007-10-11 22:11:12 +02004592 curr->comm, curr->pid);
4593 lockdep_print_held_locks(curr);
4594 }
Byungchul Parkb09be672017-08-07 16:12:52 +09004595
4596 /*
4597 * The lock history for each syscall should be independent. So wipe the
4598 * slate clean on return to userspace.
4599 */
4600 crossrelease_hist_end(XHLOCK_PROC);
4601 crossrelease_hist_start(XHLOCK_PROC);
Peter Zijlstrab351d162007-10-11 22:11:12 +02004602}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004603
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004604void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004605{
4606 struct task_struct *curr = current;
4607
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004608 /* Note: the following can be executed concurrently, so be careful. */
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004609 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004610 pr_warn("=============================\n");
4611 pr_warn("WARNING: suspicious RCU usage\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004612 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004613 pr_warn("-----------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004614 pr_warn("%s:%d %s!\n", file, line, s);
4615 pr_warn("\nother info that might help us debug this:\n\n");
4616 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004617 !rcu_lockdep_current_cpu_online()
4618 ? "RCU used illegally from offline CPU!\n"
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004619 : !rcu_is_watching()
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004620 ? "RCU used illegally from idle CPU!\n"
4621 : "",
4622 rcu_scheduler_active, debug_locks);
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004623
4624 /*
4625 * If a CPU is in the RCU-free window in idle (ie: in the section
4626 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4627 * considers that CPU to be in an "extended quiescent state",
4628 * which means that RCU will be completely ignoring that CPU.
4629 * Therefore, rcu_read_lock() and friends have absolutely no
4630 * effect on a CPU running in that state. In other words, even if
4631 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4632 * delete data structures out from under it. RCU really has no
4633 * choice here: we need to keep an RCU-free window in idle where
4634 * the CPU may possibly enter into low power mode. This way we can
4635 * notice an extended quiescent state to other CPUs that started a grace
4636 * period. Otherwise we would delay any grace period as long as we run
4637 * in the idle task.
4638 *
4639 * So complain bitterly if someone does call rcu_read_lock(),
4640 * rcu_read_lock_bh() and so on from extended quiescent states.
4641 */
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004642 if (!rcu_is_watching())
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004643 pr_warn("RCU used illegally from extended quiescent state!\n");
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004644
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004645 lockdep_print_held_locks(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004646 pr_warn("\nstack backtrace:\n");
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004647 dump_stack();
4648}
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004649EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);
Byungchul Parkb09be672017-08-07 16:12:52 +09004650
4651#ifdef CONFIG_LOCKDEP_CROSSRELEASE
4652
4653/*
4654 * Crossrelease works by recording a lock history for each thread and
4655 * connecting those historic locks that were taken after the
4656 * wait_for_completion() in the complete() context.
4657 *
4658 * Task-A Task-B
4659 *
4660 * mutex_lock(&A);
4661 * mutex_unlock(&A);
4662 *
4663 * wait_for_completion(&C);
4664 * lock_acquire_crosslock();
4665 * atomic_inc_return(&cross_gen_id);
4666 * |
4667 * | mutex_lock(&B);
4668 * | mutex_unlock(&B);
4669 * |
4670 * | complete(&C);
4671 * `-- lock_commit_crosslock();
4672 *
4673 * Which will then add a dependency between B and C.
4674 */
4675
4676#define xhlock(i) (current->xhlocks[(i) % MAX_XHLOCKS_NR])
4677
4678/*
4679 * Whenever a crosslock is held, cross_gen_id will be increased.
4680 */
4681static atomic_t cross_gen_id; /* Can be wrapped */
4682
4683/*
Byungchul Park23f873d2017-08-07 16:12:53 +09004684 * Make an entry of the ring buffer invalid.
4685 */
4686static inline void invalidate_xhlock(struct hist_lock *xhlock)
4687{
4688 /*
4689 * Normally, xhlock->hlock.instance must be !NULL.
4690 */
4691 xhlock->hlock.instance = NULL;
4692}
4693
4694/*
Byungchul Parkb09be672017-08-07 16:12:52 +09004695 * Lock history stacks; we have 3 nested lock history stacks:
4696 *
4697 * Hard IRQ
4698 * Soft IRQ
4699 * History / Task
4700 *
4701 * The thing is that once we complete a (Hard/Soft) IRQ the future task locks
4702 * should not depend on any of the locks observed while running the IRQ.
4703 *
4704 * So what we do is rewind the history buffer and erase all our knowledge of
4705 * that temporal event.
4706 */
4707
4708/*
4709 * We need this to annotate lock history boundaries. Take for instance
4710 * workqueues; each work is independent of the last. The completion of a future
4711 * work does not depend on the completion of a past work (in general).
4712 * Therefore we must not carry that (lock) dependency across works.
4713 *
4714 * This is true for many things; pretty much all kthreads fall into this
4715 * pattern, where they have an 'idle' state and future completions do not
4716 * depend on past completions. Its just that since they all have the 'same'
4717 * form -- the kthread does the same over and over -- it doesn't typically
4718 * matter.
4719 *
4720 * The same is true for system-calls, once a system call is completed (we've
4721 * returned to userspace) the next system call does not depend on the lock
4722 * history of the previous system call.
4723 */
4724void crossrelease_hist_start(enum xhlock_context_t c)
4725{
Byungchul Park23f873d2017-08-07 16:12:53 +09004726 struct task_struct *cur = current;
4727
4728 if (cur->xhlocks) {
4729 cur->xhlock_idx_hist[c] = cur->xhlock_idx;
4730 cur->hist_id_save[c] = cur->hist_id;
4731 }
Byungchul Parkb09be672017-08-07 16:12:52 +09004732}
4733
4734void crossrelease_hist_end(enum xhlock_context_t c)
4735{
Byungchul Park23f873d2017-08-07 16:12:53 +09004736 struct task_struct *cur = current;
4737
4738 if (cur->xhlocks) {
4739 unsigned int idx = cur->xhlock_idx_hist[c];
4740 struct hist_lock *h = &xhlock(idx);
4741
4742 cur->xhlock_idx = idx;
4743
4744 /* Check if the ring was overwritten. */
4745 if (h->hist_id != cur->hist_id_save[c])
4746 invalidate_xhlock(h);
4747 }
Byungchul Parkb09be672017-08-07 16:12:52 +09004748}
4749
4750static int cross_lock(struct lockdep_map *lock)
4751{
4752 return lock ? lock->cross : 0;
4753}
4754
4755/*
4756 * This is needed to decide the relationship between wrapable variables.
4757 */
4758static inline int before(unsigned int a, unsigned int b)
4759{
4760 return (int)(a - b) < 0;
4761}
4762
4763static inline struct lock_class *xhlock_class(struct hist_lock *xhlock)
4764{
4765 return hlock_class(&xhlock->hlock);
4766}
4767
4768static inline struct lock_class *xlock_class(struct cross_lock *xlock)
4769{
4770 return hlock_class(&xlock->hlock);
4771}
4772
4773/*
4774 * Should we check a dependency with previous one?
4775 */
4776static inline int depend_before(struct held_lock *hlock)
4777{
4778 return hlock->read != 2 && hlock->check && !hlock->trylock;
4779}
4780
4781/*
4782 * Should we check a dependency with next one?
4783 */
4784static inline int depend_after(struct held_lock *hlock)
4785{
4786 return hlock->read != 2 && hlock->check;
4787}
4788
4789/*
4790 * Check if the xhlock is valid, which would be false if,
4791 *
4792 * 1. Has not used after initializaion yet.
Byungchul Park23f873d2017-08-07 16:12:53 +09004793 * 2. Got invalidated.
Byungchul Parkb09be672017-08-07 16:12:52 +09004794 *
4795 * Remind hist_lock is implemented as a ring buffer.
4796 */
4797static inline int xhlock_valid(struct hist_lock *xhlock)
4798{
4799 /*
4800 * xhlock->hlock.instance must be !NULL.
4801 */
4802 return !!xhlock->hlock.instance;
4803}
4804
4805/*
4806 * Record a hist_lock entry.
4807 *
4808 * Irq disable is only required.
4809 */
4810static void add_xhlock(struct held_lock *hlock)
4811{
4812 unsigned int idx = ++current->xhlock_idx;
4813 struct hist_lock *xhlock = &xhlock(idx);
4814
4815#ifdef CONFIG_DEBUG_LOCKDEP
4816 /*
4817 * This can be done locklessly because they are all task-local
4818 * state, we must however ensure IRQs are disabled.
4819 */
4820 WARN_ON_ONCE(!irqs_disabled());
4821#endif
4822
4823 /* Initialize hist_lock's members */
4824 xhlock->hlock = *hlock;
Byungchul Park23f873d2017-08-07 16:12:53 +09004825 xhlock->hist_id = current->hist_id++;
Byungchul Parkb09be672017-08-07 16:12:52 +09004826
4827 xhlock->trace.nr_entries = 0;
4828 xhlock->trace.max_entries = MAX_XHLOCK_TRACE_ENTRIES;
4829 xhlock->trace.entries = xhlock->trace_entries;
4830 xhlock->trace.skip = 3;
4831 save_stack_trace(&xhlock->trace);
4832}
4833
4834static inline int same_context_xhlock(struct hist_lock *xhlock)
4835{
4836 return xhlock->hlock.irq_context == task_irq_context(current);
4837}
4838
4839/*
4840 * This should be lockless as far as possible because this would be
4841 * called very frequently.
4842 */
4843static void check_add_xhlock(struct held_lock *hlock)
4844{
4845 /*
4846 * Record a hist_lock, only in case that acquisitions ahead
4847 * could depend on the held_lock. For example, if the held_lock
4848 * is trylock then acquisitions ahead never depends on that.
4849 * In that case, we don't need to record it. Just return.
4850 */
4851 if (!current->xhlocks || !depend_before(hlock))
4852 return;
4853
4854 add_xhlock(hlock);
4855}
4856
4857/*
4858 * For crosslock.
4859 */
4860static int add_xlock(struct held_lock *hlock)
4861{
4862 struct cross_lock *xlock;
4863 unsigned int gen_id;
4864
4865 if (!graph_lock())
4866 return 0;
4867
4868 xlock = &((struct lockdep_map_cross *)hlock->instance)->xlock;
4869
Byungchul Park28a903f2017-08-07 16:12:54 +09004870 /*
4871 * When acquisitions for a crosslock are overlapped, we use
4872 * nr_acquire to perform commit for them, based on cross_gen_id
4873 * of the first acquisition, which allows to add additional
4874 * dependencies.
4875 *
4876 * Moreover, when no acquisition of a crosslock is in progress,
4877 * we should not perform commit because the lock might not exist
4878 * any more, which might cause incorrect memory access. So we
4879 * have to track the number of acquisitions of a crosslock.
4880 *
4881 * depend_after() is necessary to initialize only the first
4882 * valid xlock so that the xlock can be used on its commit.
4883 */
4884 if (xlock->nr_acquire++ && depend_after(&xlock->hlock))
4885 goto unlock;
4886
Byungchul Parkb09be672017-08-07 16:12:52 +09004887 gen_id = (unsigned int)atomic_inc_return(&cross_gen_id);
4888 xlock->hlock = *hlock;
4889 xlock->hlock.gen_id = gen_id;
Byungchul Park28a903f2017-08-07 16:12:54 +09004890unlock:
Byungchul Parkb09be672017-08-07 16:12:52 +09004891 graph_unlock();
Byungchul Parkb09be672017-08-07 16:12:52 +09004892 return 1;
4893}
4894
4895/*
4896 * Called for both normal and crosslock acquires. Normal locks will be
4897 * pushed on the hist_lock queue. Cross locks will record state and
4898 * stop regular lock_acquire() to avoid being placed on the held_lock
4899 * stack.
4900 *
4901 * Return: 0 - failure;
4902 * 1 - crosslock, done;
4903 * 2 - normal lock, continue to held_lock[] ops.
4904 */
4905static int lock_acquire_crosslock(struct held_lock *hlock)
4906{
4907 /*
4908 * CONTEXT 1 CONTEXT 2
4909 * --------- ---------
4910 * lock A (cross)
4911 * X = atomic_inc_return(&cross_gen_id)
4912 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4913 * Y = atomic_read_acquire(&cross_gen_id)
4914 * lock B
4915 *
4916 * atomic_read_acquire() is for ordering between A and B,
4917 * IOW, A happens before B, when CONTEXT 2 see Y >= X.
4918 *
4919 * Pairs with atomic_inc_return() in add_xlock().
4920 */
4921 hlock->gen_id = (unsigned int)atomic_read_acquire(&cross_gen_id);
4922
4923 if (cross_lock(hlock->instance))
4924 return add_xlock(hlock);
4925
4926 check_add_xhlock(hlock);
4927 return 2;
4928}
4929
4930static int copy_trace(struct stack_trace *trace)
4931{
4932 unsigned long *buf = stack_trace + nr_stack_trace_entries;
4933 unsigned int max_nr = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
4934 unsigned int nr = min(max_nr, trace->nr_entries);
4935
4936 trace->nr_entries = nr;
4937 memcpy(buf, trace->entries, nr * sizeof(trace->entries[0]));
4938 trace->entries = buf;
4939 nr_stack_trace_entries += nr;
4940
4941 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
4942 if (!debug_locks_off_graph_unlock())
4943 return 0;
4944
4945 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
4946 dump_stack();
4947
4948 return 0;
4949 }
4950
4951 return 1;
4952}
4953
4954static int commit_xhlock(struct cross_lock *xlock, struct hist_lock *xhlock)
4955{
4956 unsigned int xid, pid;
4957 u64 chain_key;
4958
4959 xid = xlock_class(xlock) - lock_classes;
4960 chain_key = iterate_chain_key((u64)0, xid);
4961 pid = xhlock_class(xhlock) - lock_classes;
4962 chain_key = iterate_chain_key(chain_key, pid);
4963
4964 if (lookup_chain_cache(chain_key))
4965 return 1;
4966
4967 if (!add_chain_cache_classes(xid, pid, xhlock->hlock.irq_context,
4968 chain_key))
4969 return 0;
4970
4971 if (!check_prev_add(current, &xlock->hlock, &xhlock->hlock, 1,
4972 &xhlock->trace, copy_trace))
4973 return 0;
4974
4975 return 1;
4976}
4977
4978static void commit_xhlocks(struct cross_lock *xlock)
4979{
4980 unsigned int cur = current->xhlock_idx;
Byungchul Park23f873d2017-08-07 16:12:53 +09004981 unsigned int prev_hist_id = xhlock(cur).hist_id;
Byungchul Parkb09be672017-08-07 16:12:52 +09004982 unsigned int i;
4983
4984 if (!graph_lock())
4985 return;
4986
Byungchul Park28a903f2017-08-07 16:12:54 +09004987 if (xlock->nr_acquire) {
4988 for (i = 0; i < MAX_XHLOCKS_NR; i++) {
4989 struct hist_lock *xhlock = &xhlock(cur - i);
Byungchul Parkb09be672017-08-07 16:12:52 +09004990
Byungchul Park28a903f2017-08-07 16:12:54 +09004991 if (!xhlock_valid(xhlock))
4992 break;
Byungchul Parkb09be672017-08-07 16:12:52 +09004993
Byungchul Park28a903f2017-08-07 16:12:54 +09004994 if (before(xhlock->hlock.gen_id, xlock->hlock.gen_id))
4995 break;
Byungchul Parkb09be672017-08-07 16:12:52 +09004996
Byungchul Park28a903f2017-08-07 16:12:54 +09004997 if (!same_context_xhlock(xhlock))
4998 break;
Byungchul Parkb09be672017-08-07 16:12:52 +09004999
Byungchul Park28a903f2017-08-07 16:12:54 +09005000 /*
5001 * Filter out the cases that the ring buffer was
5002 * overwritten and the previous entry has a bigger
5003 * hist_id than the following one, which is impossible
5004 * otherwise.
5005 */
5006 if (unlikely(before(xhlock->hist_id, prev_hist_id)))
5007 break;
Byungchul Park23f873d2017-08-07 16:12:53 +09005008
Byungchul Park28a903f2017-08-07 16:12:54 +09005009 prev_hist_id = xhlock->hist_id;
Byungchul Park23f873d2017-08-07 16:12:53 +09005010
Byungchul Park28a903f2017-08-07 16:12:54 +09005011 /*
5012 * commit_xhlock() returns 0 with graph_lock already
5013 * released if fail.
5014 */
5015 if (!commit_xhlock(xlock, xhlock))
5016 return;
5017 }
Byungchul Parkb09be672017-08-07 16:12:52 +09005018 }
5019
5020 graph_unlock();
5021}
5022
5023void lock_commit_crosslock(struct lockdep_map *lock)
5024{
5025 struct cross_lock *xlock;
5026 unsigned long flags;
5027
5028 if (unlikely(!debug_locks || current->lockdep_recursion))
5029 return;
5030
5031 if (!current->xhlocks)
5032 return;
5033
5034 /*
5035 * Do commit hist_locks with the cross_lock, only in case that
5036 * the cross_lock could depend on acquisitions after that.
5037 *
5038 * For example, if the cross_lock does not have the 'check' flag
5039 * then we don't need to check dependencies and commit for that.
5040 * Just skip it. In that case, of course, the cross_lock does
5041 * not depend on acquisitions ahead, either.
5042 *
5043 * WARNING: Don't do that in add_xlock() in advance. When an
5044 * acquisition context is different from the commit context,
5045 * invalid(skipped) cross_lock might be accessed.
5046 */
5047 if (!depend_after(&((struct lockdep_map_cross *)lock)->xlock.hlock))
5048 return;
5049
5050 raw_local_irq_save(flags);
5051 check_flags(flags);
5052 current->lockdep_recursion = 1;
5053 xlock = &((struct lockdep_map_cross *)lock)->xlock;
5054 commit_xhlocks(xlock);
5055 current->lockdep_recursion = 0;
5056 raw_local_irq_restore(flags);
5057}
5058EXPORT_SYMBOL_GPL(lock_commit_crosslock);
5059
5060/*
Byungchul Park28a903f2017-08-07 16:12:54 +09005061 * Return: 0 - failure;
5062 * 1 - crosslock, done;
Byungchul Parkb09be672017-08-07 16:12:52 +09005063 * 2 - normal lock, continue to held_lock[] ops.
5064 */
5065static int lock_release_crosslock(struct lockdep_map *lock)
5066{
Byungchul Park28a903f2017-08-07 16:12:54 +09005067 if (cross_lock(lock)) {
5068 if (!graph_lock())
5069 return 0;
5070 ((struct lockdep_map_cross *)lock)->xlock.nr_acquire--;
5071 graph_unlock();
5072 return 1;
5073 }
5074 return 2;
Byungchul Parkb09be672017-08-07 16:12:52 +09005075}
5076
5077static void cross_init(struct lockdep_map *lock, int cross)
5078{
Byungchul Park28a903f2017-08-07 16:12:54 +09005079 if (cross)
5080 ((struct lockdep_map_cross *)lock)->xlock.nr_acquire = 0;
5081
Byungchul Parkb09be672017-08-07 16:12:52 +09005082 lock->cross = cross;
5083
5084 /*
5085 * Crossrelease assumes that the ring buffer size of xhlocks
5086 * is aligned with power of 2. So force it on build.
5087 */
5088 BUILD_BUG_ON(MAX_XHLOCKS_NR & (MAX_XHLOCKS_NR - 1));
5089}
5090
5091void lockdep_init_task(struct task_struct *task)
5092{
5093 int i;
5094
5095 task->xhlock_idx = UINT_MAX;
Byungchul Park23f873d2017-08-07 16:12:53 +09005096 task->hist_id = 0;
Byungchul Parkb09be672017-08-07 16:12:52 +09005097
Byungchul Park23f873d2017-08-07 16:12:53 +09005098 for (i = 0; i < XHLOCK_CTX_NR; i++) {
Byungchul Parkb09be672017-08-07 16:12:52 +09005099 task->xhlock_idx_hist[i] = UINT_MAX;
Byungchul Park23f873d2017-08-07 16:12:53 +09005100 task->hist_id_save[i] = 0;
5101 }
Byungchul Parkb09be672017-08-07 16:12:52 +09005102
5103 task->xhlocks = kzalloc(sizeof(struct hist_lock) * MAX_XHLOCKS_NR,
5104 GFP_KERNEL);
5105}
5106
5107void lockdep_free_task(struct task_struct *task)
5108{
5109 if (task->xhlocks) {
5110 void *tmp = task->xhlocks;
5111 /* Diable crossrelease for current */
5112 task->xhlocks = NULL;
5113 kfree(tmp);
5114 }
5115}
5116#endif