blob: 472547dd45c32d400a733d308a6f04c3daac4f84 [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>
Peter Zijlstrae7904a22015-08-01 19:25:08 +020050#include <linux/random.h>
Peter Zijlstradfaaf3f2016-05-30 18:31:33 +020051#include <linux/jhash.h>
Peter Zijlstraaf012962009-07-16 15:44:29 +020052
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070053#include <asm/sections.h>
54
55#include "lockdep_internals.h"
56
Steven Rostedta8d154b2009-04-10 09:36:00 -040057#define CREATE_TRACE_POINTS
Frederic Weisbecker67178762009-11-13 10:06:34 +010058#include <trace/events/lock.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040059
Peter Zijlstraf20786f2007-07-19 01:48:56 -070060#ifdef CONFIG_PROVE_LOCKING
61int prove_locking = 1;
62module_param(prove_locking, int, 0644);
63#else
64#define prove_locking 0
65#endif
66
67#ifdef CONFIG_LOCK_STAT
68int lock_stat = 1;
69module_param(lock_stat, int, 0644);
70#else
71#define lock_stat 0
72#endif
73
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070074/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080075 * lockdep_lock: protects the lockdep graph, the hashes and the
76 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070077 *
78 * This is one of the rare exceptions where it's justified
79 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080080 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070081 */
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010082static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Ingo Molnar74c383f2006-12-13 00:34:43 -080083
84static int graph_lock(void)
85{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010086 arch_spin_lock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080087 /*
88 * Make sure that if another CPU detected a bug while
89 * walking the graph we dont change it (while the other
90 * CPU is busy printing out stuff with the graph lock
91 * dropped already)
92 */
93 if (!debug_locks) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010094 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080095 return 0;
96 }
Steven Rostedtbb065af2008-05-12 21:21:00 +020097 /* prevent any recursions within lockdep from causing deadlocks */
98 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -080099 return 1;
100}
101
102static inline int graph_unlock(void)
103{
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200104 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
105 /*
106 * The lockdep graph lock isn't locked while we expect it to
107 * be, we're confused now, bye!
108 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800109 return DEBUG_LOCKS_WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200110 }
Jarek Poplawski381a2292007-02-10 01:44:58 -0800111
Steven Rostedtbb065af2008-05-12 21:21:00 +0200112 current->lockdep_recursion--;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100113 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800114 return 0;
115}
116
117/*
118 * Turn lock debugging off and return with 0 if it was off already,
119 * and also release the graph lock:
120 */
121static inline int debug_locks_off_graph_unlock(void)
122{
123 int ret = debug_locks_off();
124
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100125 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800126
127 return ret;
128}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700129
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700130unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200131static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700132
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700133/*
134 * All data structures here are protected by the global debug_lock.
135 *
136 * Mutex key structs only get allocated, once during bootup, and never
137 * get freed - this significantly simplifies the debugging code.
138 */
139unsigned long nr_lock_classes;
140static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
141
Dave Jonesf82b2172008-08-11 09:30:23 +0200142static inline struct lock_class *hlock_class(struct held_lock *hlock)
143{
144 if (!hlock->class_idx) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200145 /*
146 * Someone passed in garbage, we give up.
147 */
Dave Jonesf82b2172008-08-11 09:30:23 +0200148 DEBUG_LOCKS_WARN_ON(1);
149 return NULL;
150 }
151 return lock_classes + hlock->class_idx - 1;
152}
153
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700154#ifdef CONFIG_LOCK_STAT
Peter Zijlstra25528212016-03-15 14:52:49 -0700155static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700156
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200157static inline u64 lockstat_clock(void)
158{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200159 return local_clock();
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200160}
161
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200162static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700163{
164 int i;
165
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200166 for (i = 0; i < LOCKSTAT_POINTS; i++) {
167 if (points[i] == 0) {
168 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700169 break;
170 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200171 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700172 break;
173 }
174
175 return i;
176}
177
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200178static void lock_time_inc(struct lock_time *lt, u64 time)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700179{
180 if (time > lt->max)
181 lt->max = time;
182
Frank Rowand109d71c2009-11-19 13:42:06 -0800183 if (time < lt->min || !lt->nr)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700184 lt->min = time;
185
186 lt->total += time;
187 lt->nr++;
188}
189
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700190static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
191{
Frank Rowand109d71c2009-11-19 13:42:06 -0800192 if (!src->nr)
193 return;
194
195 if (src->max > dst->max)
196 dst->max = src->max;
197
198 if (src->min < dst->min || !dst->nr)
199 dst->min = src->min;
200
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700201 dst->total += src->total;
202 dst->nr += src->nr;
203}
204
205struct lock_class_stats lock_stats(struct lock_class *class)
206{
207 struct lock_class_stats stats;
208 int cpu, i;
209
210 memset(&stats, 0, sizeof(struct lock_class_stats));
211 for_each_possible_cpu(cpu) {
212 struct lock_class_stats *pcs =
Tejun Heo1871e522009-10-29 22:34:13 +0900213 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700214
215 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
216 stats.contention_point[i] += pcs->contention_point[i];
217
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200218 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
219 stats.contending_point[i] += pcs->contending_point[i];
220
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700221 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
222 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
223
224 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
225 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700226
227 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
228 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700229 }
230
231 return stats;
232}
233
234void clear_lock_stats(struct lock_class *class)
235{
236 int cpu;
237
238 for_each_possible_cpu(cpu) {
239 struct lock_class_stats *cpu_stats =
Tejun Heo1871e522009-10-29 22:34:13 +0900240 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700241
242 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
243 }
244 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200245 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700246}
247
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700248static struct lock_class_stats *get_lock_stats(struct lock_class *class)
249{
Tejun Heo1871e522009-10-29 22:34:13 +0900250 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700251}
252
253static void put_lock_stats(struct lock_class_stats *stats)
254{
Tejun Heo1871e522009-10-29 22:34:13 +0900255 put_cpu_var(cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700256}
257
258static void lock_release_holdtime(struct held_lock *hlock)
259{
260 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200261 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700262
263 if (!lock_stat)
264 return;
265
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200266 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700267
Dave Jonesf82b2172008-08-11 09:30:23 +0200268 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700269 if (hlock->read)
270 lock_time_inc(&stats->read_holdtime, holdtime);
271 else
272 lock_time_inc(&stats->write_holdtime, holdtime);
273 put_lock_stats(stats);
274}
275#else
276static inline void lock_release_holdtime(struct held_lock *hlock)
277{
278}
279#endif
280
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700281/*
282 * We keep a global list of all lock classes. The list only grows,
283 * never shrinks. The list is only accessed with the lockdep
284 * spinlock lock held.
285 */
286LIST_HEAD(all_lock_classes);
287
288/*
289 * The lockdep classes are in a hash-table as well, for fast lookup:
290 */
291#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
292#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700293#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700294#define classhashentry(key) (classhash_table + __classhashfn((key)))
295
Andrew Mortona63f38c2016-02-03 13:44:12 -0800296static struct hlist_head classhash_table[CLASSHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700297
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700298/*
299 * We put the lock dependency chains into a hash-table as well, to cache
300 * their existence:
301 */
302#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
303#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700304#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700305#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
306
Andrew Mortona63f38c2016-02-03 13:44:12 -0800307static struct hlist_head chainhash_table[CHAINHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700308
309/*
310 * The hash key of the lock dependency chains is a hash itself too:
311 * it's a hash of all locks taken up to that lock, including that lock.
312 * It's a 64-bit hash, because it's important for the keys to be
313 * unique.
314 */
Peter Zijlstradfaaf3f2016-05-30 18:31:33 +0200315static inline u64 iterate_chain_key(u64 key, u32 idx)
316{
317 u32 k0 = key, k1 = key >> 32;
318
319 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
320
321 return k0 | (u64)k1 << 32;
322}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700323
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200324void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700325{
326 current->lockdep_recursion++;
327}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700328EXPORT_SYMBOL(lockdep_off);
329
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200330void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700331{
332 current->lockdep_recursion--;
333}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700334EXPORT_SYMBOL(lockdep_on);
335
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700336/*
337 * Debugging switches:
338 */
339
340#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800341#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700342
343#if VERBOSE
344# define HARDIRQ_VERBOSE 1
345# define SOFTIRQ_VERBOSE 1
346#else
347# define HARDIRQ_VERBOSE 0
348# define SOFTIRQ_VERBOSE 0
349#endif
350
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +0100351#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700352/*
353 * Quick filtering for interesting events:
354 */
355static int class_filter(struct lock_class *class)
356{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700357#if 0
358 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700359 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700360 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700361 return 1;
362 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700363 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700364 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700365#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800366 /* Filter everything else. 1 would be to allow everything else */
367 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700368}
369#endif
370
371static int verbose(struct lock_class *class)
372{
373#if VERBOSE
374 return class_filter(class);
375#endif
376 return 0;
377}
378
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700379/*
380 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800381 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700382 */
383unsigned long nr_stack_trace_entries;
384static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
385
Dave Jones2c522832013-04-25 13:40:02 -0400386static void print_lockdep_off(const char *bug_msg)
387{
388 printk(KERN_DEBUG "%s\n", bug_msg);
389 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200390#ifdef CONFIG_LOCK_STAT
Dave Jones2c522832013-04-25 13:40:02 -0400391 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200392#endif
Dave Jones2c522832013-04-25 13:40:02 -0400393}
394
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700395static int save_trace(struct stack_trace *trace)
396{
397 trace->nr_entries = 0;
398 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
399 trace->entries = stack_trace + nr_stack_trace_entries;
400
Andi Kleen5a1b3992006-09-26 10:52:34 +0200401 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200402
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700403 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700404
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200405 /*
406 * Some daft arches put -1 at the end to indicate its a full trace.
407 *
408 * <rant> this is buggy anyway, since it takes a whole extra entry so a
409 * complete trace that maxes out the entries provided will be reported
410 * as incomplete, friggin useless </rant>
411 */
Luck, Tonyea5b41f2009-12-09 14:29:36 -0800412 if (trace->nr_entries != 0 &&
413 trace->entries[trace->nr_entries-1] == ULONG_MAX)
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200414 trace->nr_entries--;
415
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700416 trace->max_entries = trace->nr_entries;
417
418 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700419
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200420 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800421 if (!debug_locks_off_graph_unlock())
422 return 0;
423
Dave Jones2c522832013-04-25 13:40:02 -0400424 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
Ingo Molnar74c383f2006-12-13 00:34:43 -0800425 dump_stack();
426
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700427 return 0;
428 }
429
430 return 1;
431}
432
433unsigned int nr_hardirq_chains;
434unsigned int nr_softirq_chains;
435unsigned int nr_process_chains;
436unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700437
438#ifdef CONFIG_DEBUG_LOCKDEP
439/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700440 * Various lockdep statistics:
441 */
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200442DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700443#endif
444
445/*
446 * Locking printouts:
447 */
448
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100449#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100450 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
451 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
452 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
453 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100454
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700455static const char *usage_str[] =
456{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100457#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
458#include "lockdep_states.h"
459#undef LOCKDEP_STATE
460 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700461};
462
463const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
464{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700465 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700466}
467
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100468static inline unsigned long lock_flag(enum lock_usage_bit bit)
469{
470 return 1UL << bit;
471}
472
473static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
474{
475 char c = '.';
476
477 if (class->usage_mask & lock_flag(bit + 2))
478 c = '+';
479 if (class->usage_mask & lock_flag(bit)) {
480 c = '-';
481 if (class->usage_mask & lock_flag(bit + 2))
482 c = '?';
483 }
484
485 return c;
486}
487
Peter Zijlstraf510b232009-01-22 17:53:47 +0100488void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700489{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100490 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700491
Peter Zijlstraf510b232009-01-22 17:53:47 +0100492#define LOCKDEP_STATE(__STATE) \
493 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
494 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
495#include "lockdep_states.h"
496#undef LOCKDEP_STATE
497
498 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700499}
500
Steven Rostedte5e78d02011-11-02 20:24:16 -0400501static void __print_lock_name(struct lock_class *class)
Steven Rostedt3003eba2011-04-20 21:41:54 -0400502{
503 char str[KSYM_NAME_LEN];
504 const char *name;
505
506 name = class->name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700507 if (!name) {
508 name = __get_key_name(class->key, str);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100509 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700510 } else {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100511 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700512 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100513 printk(KERN_CONT "#%d", class->name_version);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700514 if (class->subclass)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100515 printk(KERN_CONT "/%d", class->subclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700516 }
Steven Rostedte5e78d02011-11-02 20:24:16 -0400517}
518
519static void print_lock_name(struct lock_class *class)
520{
521 char usage[LOCK_USAGE_CHARS];
522
523 get_usage_chars(class, usage);
524
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100525 printk(KERN_CONT " (");
Steven Rostedte5e78d02011-11-02 20:24:16 -0400526 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100527 printk(KERN_CONT "){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700528}
529
530static void print_lockdep_cache(struct lockdep_map *lock)
531{
532 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700533 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700534
535 name = lock->name;
536 if (!name)
537 name = __get_key_name(lock->key->subkeys, str);
538
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100539 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700540}
541
542static void print_lock(struct held_lock *hlock)
543{
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200544 /*
545 * We can be called locklessly through debug_show_all_locks() so be
546 * extra careful, the hlock might have been released and cleared.
547 */
548 unsigned int class_idx = hlock->class_idx;
549
550 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
551 barrier();
552
553 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100554 printk(KERN_CONT "<RELEASED>\n");
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200555 return;
556 }
557
558 print_lock_name(lock_classes + class_idx - 1);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100559 printk(KERN_CONT ", at: [<%p>] %pS\n",
560 (void *)hlock->acquire_ip, (void *)hlock->acquire_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700561}
562
563static void lockdep_print_held_locks(struct task_struct *curr)
564{
565 int i, depth = curr->lockdep_depth;
566
567 if (!depth) {
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700568 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700569 return;
570 }
571 printk("%d lock%s held by %s/%d:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700572 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700573
574 for (i = 0; i < depth; i++) {
575 printk(" #%d: ", i);
576 print_lock(curr->held_locks + i);
577 }
578}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700579
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100580static void print_kernel_ident(void)
Dave Jones99de0552006-09-29 02:00:10 -0700581{
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100582 printk("%s %.*s %s\n", init_utsname()->release,
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700583 (int)strcspn(init_utsname()->version, " "),
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100584 init_utsname()->version,
585 print_tainted());
Dave Jones99de0552006-09-29 02:00:10 -0700586}
587
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700588static int very_verbose(struct lock_class *class)
589{
590#if VERY_VERBOSE
591 return class_filter(class);
592#endif
593 return 0;
594}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700595
596/*
597 * Is this the address of a static object:
598 */
Sasha Levin8dce7a92013-06-13 18:41:16 -0400599#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700600static int static_obj(void *obj)
601{
602 unsigned long start = (unsigned long) &_stext,
603 end = (unsigned long) &_end,
604 addr = (unsigned long) obj;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700605
606 /*
607 * static variable?
608 */
609 if ((addr >= start) && (addr < end))
610 return 1;
611
Mike Frysinger2a9ad182009-09-22 16:44:16 -0700612 if (arch_is_kernel_data(addr))
613 return 1;
614
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700615 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900616 * in-kernel percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700617 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900618 if (is_kernel_percpu_address(addr))
619 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700620
621 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900622 * module static or percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700623 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900624 return is_module_address(addr) || is_module_percpu_address(addr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700625}
Sasha Levin8dce7a92013-06-13 18:41:16 -0400626#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700627
628/*
629 * To make lock name printouts unique, we calculate a unique
630 * class->name_version generation counter:
631 */
632static int count_matching_names(struct lock_class *new_class)
633{
634 struct lock_class *class;
635 int count = 0;
636
637 if (!new_class->name)
638 return 0;
639
Peter Zijlstra35a93932015-02-26 16:23:11 +0100640 list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700641 if (new_class->key - new_class->subclass == class->key)
642 return class->name_version;
643 if (class->name && !strcmp(class->name, new_class->name))
644 count = max(count, class->name_version);
645 }
646
647 return count + 1;
648}
649
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700650static inline struct lock_class *
Ingo Molnard6d897c2006-07-10 04:44:04 -0700651look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700652{
653 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -0800654 struct hlist_head *hash_head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700655 struct lock_class *class;
656
Hitoshi Mitake4ba053c2010-10-13 17:30:26 +0900657 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
658 debug_locks_off();
659 printk(KERN_ERR
660 "BUG: looking up invalid subclass: %u\n", subclass);
661 printk(KERN_ERR
662 "turning off the locking correctness validator.\n");
663 dump_stack();
664 return NULL;
665 }
666
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700667 /*
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800668 * If it is not initialised then it has never been locked,
669 * so it won't be present in the hash table.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700670 */
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800671 if (unlikely(!lock->key))
672 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700673
674 /*
675 * NOTE: the class-key must be unique. For dynamic locks, a static
676 * lock_class_key variable is passed in through the mutex_init()
677 * (or spin_lock_init()) call - which acts as the key. For static
678 * locks we use the lock object itself as the key.
679 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700680 BUILD_BUG_ON(sizeof(struct lock_class_key) >
681 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700682
683 key = lock->key->subkeys + subclass;
684
685 hash_head = classhashentry(key);
686
687 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +0100688 * We do an RCU walk of the hash, see lockdep_free_key_range().
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700689 */
Peter Zijlstra35a93932015-02-26 16:23:11 +0100690 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
691 return NULL;
692
Andrew Mortona63f38c2016-02-03 13:44:12 -0800693 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700694 if (class->key == key) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200695 /*
696 * Huh! same key, different name? Did someone trample
697 * on some memory? We're most confused.
698 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700699 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700700 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700701 }
702 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700703
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800704 return NULL;
705}
706
707/*
708 * Static locks do not have their class-keys yet - for them the key is
709 * the lock object itself. If the lock is in the per cpu area, the
710 * canonical address of the lock (per cpu offset removed) is used.
711 */
712static bool assign_lock_key(struct lockdep_map *lock)
713{
714 unsigned long can_addr, addr = (unsigned long)lock;
715
716 if (__is_kernel_percpu_address(addr, &can_addr))
717 lock->key = (void *)can_addr;
718 else if (__is_module_percpu_address(addr, &can_addr))
719 lock->key = (void *)can_addr;
720 else if (static_obj(lock))
721 lock->key = (void *)lock;
722 else {
723 /* Debug-check: all keys must be persistent! */
724 debug_locks_off();
725 pr_err("INFO: trying to register non-static key.\n");
726 pr_err("the code is fine but needs lockdep annotation.\n");
727 pr_err("turning off the locking correctness validator.\n");
728 dump_stack();
729 return false;
730 }
731
732 return true;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700733}
734
735/*
736 * Register a lock's class in the hash-table, if the class is not present
737 * yet. Otherwise we look it up. We cache the result in the lock object
738 * itself, so actual lookup of the hash should be once per lock object.
739 */
Denys Vlasenkoc003ed92016-04-08 20:58:46 +0200740static struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400741register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -0700742{
743 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -0800744 struct hlist_head *hash_head;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700745 struct lock_class *class;
Peter Zijlstra35a93932015-02-26 16:23:11 +0100746
747 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
Ingo Molnard6d897c2006-07-10 04:44:04 -0700748
749 class = look_up_lock_class(lock, subclass);
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800750 if (likely(class))
Yong Zhang87cdee72011-11-09 16:07:14 +0800751 goto out_set_class_cache;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700752
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800753 if (!lock->key) {
754 if (!assign_lock_key(lock))
755 return NULL;
756 } else if (!static_obj(lock->key)) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700757 return NULL;
758 }
759
Ingo Molnard6d897c2006-07-10 04:44:04 -0700760 key = lock->key->subkeys + subclass;
761 hash_head = classhashentry(key);
762
Ingo Molnar74c383f2006-12-13 00:34:43 -0800763 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800764 return NULL;
765 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700766 /*
767 * We have to do the hash-walk again, to avoid races
768 * with another CPU:
769 */
Andrew Mortona63f38c2016-02-03 13:44:12 -0800770 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700771 if (class->key == key)
772 goto out_unlock_set;
Peter Zijlstra35a93932015-02-26 16:23:11 +0100773 }
774
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700775 /*
776 * Allocate a new key from the static array, and add it to
777 * the hash:
778 */
779 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800780 if (!debug_locks_off_graph_unlock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800781 return NULL;
782 }
Ingo Molnar74c383f2006-12-13 00:34:43 -0800783
Dave Jones2c522832013-04-25 13:40:02 -0400784 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100785 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700786 return NULL;
787 }
788 class = lock_classes + nr_lock_classes++;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200789 debug_atomic_inc(nr_unused_locks);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700790 class->key = key;
791 class->name = lock->name;
792 class->subclass = subclass;
793 INIT_LIST_HEAD(&class->lock_entry);
794 INIT_LIST_HEAD(&class->locks_before);
795 INIT_LIST_HEAD(&class->locks_after);
796 class->name_version = count_matching_names(class);
797 /*
798 * We use RCU's safe list-add method to make
799 * parallel walking of the hash-list safe:
800 */
Andrew Mortona63f38c2016-02-03 13:44:12 -0800801 hlist_add_head_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +0100802 /*
803 * Add it to the global list of classes:
804 */
805 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700806
807 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800808 graph_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800809
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700810 printk("\nnew class %p: %s", class->key, class->name);
811 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100812 printk(KERN_CONT "#%d", class->name_version);
813 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700814 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800815
Ingo Molnar74c383f2006-12-13 00:34:43 -0800816 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800817 return NULL;
818 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700819 }
820out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -0800821 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700822
Yong Zhang87cdee72011-11-09 16:07:14 +0800823out_set_class_cache:
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400824 if (!subclass || force)
Hitoshi Mitake62016252010-10-05 18:01:51 +0900825 lock->class_cache[0] = class;
826 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
827 lock->class_cache[subclass] = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700828
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200829 /*
830 * Hash collision, did we smoke some? We found a class with a matching
831 * hash but the subclass -- which is hashed in -- didn't match.
832 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800833 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
834 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700835
836 return class;
837}
838
Peter Zijlstraca58abc2007-07-19 01:48:53 -0700839#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700840/*
Peter Zijlstra8e182572007-07-19 01:48:54 -0700841 * Allocate a lockdep entry. (assumes the graph_lock held, returns
842 * with NULL on failure)
843 */
844static struct lock_list *alloc_list_entry(void)
845{
846 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
847 if (!debug_locks_off_graph_unlock())
848 return NULL;
849
Dave Jones2c522832013-04-25 13:40:02 -0400850 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +0100851 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -0700852 return NULL;
853 }
854 return list_entries + nr_list_entries++;
855}
856
857/*
858 * Add a new dependency to the head of the list:
859 */
Tahsin Erdogan83f06162016-11-08 00:02:07 -0800860static int add_lock_to_list(struct lock_class *this, struct list_head *head,
861 unsigned long ip, int distance,
862 struct stack_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -0700863{
864 struct lock_list *entry;
865 /*
866 * Lock not present yet - get a new dependency struct and
867 * add it to the list:
868 */
869 entry = alloc_list_entry();
870 if (!entry)
871 return 0;
872
Zhu Yi74870172008-08-27 14:33:00 +0800873 entry->class = this;
874 entry->distance = distance;
Yong Zhang4726f2a2010-05-04 14:16:48 +0800875 entry->trace = *trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -0700876 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +0100877 * Both allocation and removal are done under the graph lock; but
878 * iteration is under RCU-sched; see look_up_lock_class() and
879 * lockdep_free_key_range().
Peter Zijlstra8e182572007-07-19 01:48:54 -0700880 */
881 list_add_tail_rcu(&entry->entry, head);
882
883 return 1;
884}
885
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200886/*
887 * For good efficiency of modular, we use power of 2
888 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200889#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
890#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
891
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200892/*
893 * The circular_queue and helpers is used to implement the
Peter Zijlstraaf012962009-07-16 15:44:29 +0200894 * breadth-first search(BFS)algorithem, by which we can build
895 * the shortest path from the next lock to be acquired to the
896 * previous held lock if there is a circular between them.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200897 */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200898struct circular_queue {
899 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
900 unsigned int front, rear;
901};
902
903static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200904
Ming Lei12f3dfd2009-07-16 15:44:29 +0200905unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200906
Ming Leie351b662009-07-22 22:48:09 +0800907static unsigned int lockdep_dependency_gen_id;
908
Peter Zijlstraaf012962009-07-16 15:44:29 +0200909static inline void __cq_init(struct circular_queue *cq)
910{
911 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +0800912 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200913}
914
915static inline int __cq_empty(struct circular_queue *cq)
916{
917 return (cq->front == cq->rear);
918}
919
920static inline int __cq_full(struct circular_queue *cq)
921{
922 return ((cq->rear + 1) & CQ_MASK) == cq->front;
923}
924
925static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
926{
927 if (__cq_full(cq))
928 return -1;
929
930 cq->element[cq->rear] = elem;
931 cq->rear = (cq->rear + 1) & CQ_MASK;
932 return 0;
933}
934
935static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
936{
937 if (__cq_empty(cq))
938 return -1;
939
940 *elem = cq->element[cq->front];
941 cq->front = (cq->front + 1) & CQ_MASK;
942 return 0;
943}
944
945static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
946{
947 return (cq->rear - cq->front) & CQ_MASK;
948}
949
950static inline void mark_lock_accessed(struct lock_list *lock,
951 struct lock_list *parent)
952{
953 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200954
Peter Zijlstraaf012962009-07-16 15:44:29 +0200955 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200956 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Peter Zijlstraaf012962009-07-16 15:44:29 +0200957 lock->parent = parent;
Ming Leie351b662009-07-22 22:48:09 +0800958 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200959}
960
961static inline unsigned long lock_accessed(struct lock_list *lock)
962{
963 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +0200964
Peter Zijlstraaf012962009-07-16 15:44:29 +0200965 nr = lock - list_entries;
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200966 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
Ming Leie351b662009-07-22 22:48:09 +0800967 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200968}
969
970static inline struct lock_list *get_lock_parent(struct lock_list *child)
971{
972 return child->parent;
973}
974
975static inline int get_lock_depth(struct lock_list *child)
976{
977 int depth = 0;
978 struct lock_list *parent;
979
980 while ((parent = get_lock_parent(child))) {
981 child = parent;
982 depth++;
983 }
984 return depth;
985}
986
Ming Lei9e2d5512009-07-16 15:44:29 +0200987static int __bfs(struct lock_list *source_entry,
Peter Zijlstraaf012962009-07-16 15:44:29 +0200988 void *data,
989 int (*match)(struct lock_list *entry, void *data),
990 struct lock_list **target_entry,
991 int forward)
Ming Leic94aa5c2009-07-16 15:44:29 +0200992{
993 struct lock_list *entry;
Ming Leid588e462009-07-16 15:44:29 +0200994 struct list_head *head;
Ming Leic94aa5c2009-07-16 15:44:29 +0200995 struct circular_queue *cq = &lock_cq;
996 int ret = 1;
997
Ming Lei9e2d5512009-07-16 15:44:29 +0200998 if (match(source_entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +0200999 *target_entry = source_entry;
1000 ret = 0;
1001 goto exit;
1002 }
1003
Ming Leid588e462009-07-16 15:44:29 +02001004 if (forward)
1005 head = &source_entry->class->locks_after;
1006 else
1007 head = &source_entry->class->locks_before;
1008
1009 if (list_empty(head))
1010 goto exit;
1011
1012 __cq_init(cq);
Ming Leic94aa5c2009-07-16 15:44:29 +02001013 __cq_enqueue(cq, (unsigned long)source_entry);
1014
1015 while (!__cq_empty(cq)) {
1016 struct lock_list *lock;
Ming Leic94aa5c2009-07-16 15:44:29 +02001017
1018 __cq_dequeue(cq, (unsigned long *)&lock);
1019
1020 if (!lock->class) {
1021 ret = -2;
1022 goto exit;
1023 }
1024
1025 if (forward)
1026 head = &lock->class->locks_after;
1027 else
1028 head = &lock->class->locks_before;
1029
Peter Zijlstra35a93932015-02-26 16:23:11 +01001030 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1031
1032 list_for_each_entry_rcu(entry, head, entry) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001033 if (!lock_accessed(entry)) {
Ming Lei12f3dfd2009-07-16 15:44:29 +02001034 unsigned int cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001035 mark_lock_accessed(entry, lock);
Ming Lei9e2d5512009-07-16 15:44:29 +02001036 if (match(entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001037 *target_entry = entry;
1038 ret = 0;
1039 goto exit;
1040 }
1041
1042 if (__cq_enqueue(cq, (unsigned long)entry)) {
1043 ret = -1;
1044 goto exit;
1045 }
Ming Lei12f3dfd2009-07-16 15:44:29 +02001046 cq_depth = __cq_get_elem_count(cq);
1047 if (max_bfs_queue_depth < cq_depth)
1048 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001049 }
1050 }
1051 }
1052exit:
1053 return ret;
1054}
1055
Ming Leid7aaba12009-07-16 15:44:29 +02001056static inline int __bfs_forwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001057 void *data,
1058 int (*match)(struct lock_list *entry, void *data),
1059 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001060{
Ming Lei9e2d5512009-07-16 15:44:29 +02001061 return __bfs(src_entry, data, match, target_entry, 1);
Ming Leic94aa5c2009-07-16 15:44:29 +02001062
1063}
1064
Ming Leid7aaba12009-07-16 15:44:29 +02001065static inline int __bfs_backwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001066 void *data,
1067 int (*match)(struct lock_list *entry, void *data),
1068 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001069{
Ming Lei9e2d5512009-07-16 15:44:29 +02001070 return __bfs(src_entry, data, match, target_entry, 0);
Ming Leic94aa5c2009-07-16 15:44:29 +02001071
1072}
1073
Peter Zijlstra8e182572007-07-19 01:48:54 -07001074/*
1075 * Recursive, forwards-direction lock-dependency checking, used for
1076 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1077 * checking.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001078 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001079
1080/*
1081 * Print a dependency chain entry (this is only done when a deadlock
1082 * has been detected):
1083 */
1084static noinline int
Ming Lei24208ca2009-07-16 15:44:29 +02001085print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001086{
1087 if (debug_locks_silent)
1088 return 0;
1089 printk("\n-> #%u", depth);
1090 print_lock_name(target->class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001091 printk(KERN_CONT ":\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001092 print_stack_trace(&target->trace, 6);
1093
1094 return 0;
1095}
1096
Steven Rostedtf4185812011-04-20 21:41:55 -04001097static void
1098print_circular_lock_scenario(struct held_lock *src,
1099 struct held_lock *tgt,
1100 struct lock_list *prt)
1101{
1102 struct lock_class *source = hlock_class(src);
1103 struct lock_class *target = hlock_class(tgt);
1104 struct lock_class *parent = prt->class;
1105
1106 /*
1107 * A direct locking problem where unsafe_class lock is taken
1108 * directly by safe_class lock, then all we need to show
1109 * is the deadlock scenario, as it is obvious that the
1110 * unsafe lock is taken under the safe lock.
1111 *
1112 * But if there is a chain instead, where the safe lock takes
1113 * an intermediate lock (middle_class) where this lock is
1114 * not the same as the safe lock, then the lock chain is
1115 * used to describe the problem. Otherwise we would need
1116 * to show a different CPU case for each link in the chain
1117 * from the safe_class lock to the unsafe_class lock.
1118 */
1119 if (parent != source) {
1120 printk("Chain exists of:\n ");
1121 __print_lock_name(source);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001122 printk(KERN_CONT " --> ");
Steven Rostedtf4185812011-04-20 21:41:55 -04001123 __print_lock_name(parent);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001124 printk(KERN_CONT " --> ");
Steven Rostedtf4185812011-04-20 21:41:55 -04001125 __print_lock_name(target);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001126 printk(KERN_CONT "\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001127 }
1128
Ingo Molnare966eae2017-12-12 12:31:16 +01001129 printk(" Possible unsafe locking scenario:\n\n");
1130 printk(" CPU0 CPU1\n");
1131 printk(" ---- ----\n");
1132 printk(" lock(");
1133 __print_lock_name(target);
1134 printk(KERN_CONT ");\n");
1135 printk(" lock(");
1136 __print_lock_name(parent);
1137 printk(KERN_CONT ");\n");
1138 printk(" lock(");
1139 __print_lock_name(target);
1140 printk(KERN_CONT ");\n");
1141 printk(" lock(");
1142 __print_lock_name(source);
1143 printk(KERN_CONT ");\n");
1144 printk("\n *** DEADLOCK ***\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001145}
1146
Peter Zijlstra8e182572007-07-19 01:48:54 -07001147/*
1148 * When a circular dependency is detected, print the
1149 * header first:
1150 */
1151static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001152print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1153 struct held_lock *check_src,
1154 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001155{
1156 struct task_struct *curr = current;
1157
Ming Leic94aa5c2009-07-16 15:44:29 +02001158 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001159 return 0;
1160
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001161 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001162 pr_warn("======================================================\n");
1163 pr_warn("WARNING: possible circular locking dependency detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001164 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001165 pr_warn("------------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001166 pr_warn("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001167 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001168 print_lock(check_src);
Byungchul Park383a4bc2017-08-07 16:12:55 +09001169
Ingo Molnare966eae2017-12-12 12:31:16 +01001170 pr_warn("\nbut task is already holding lock:\n");
Byungchul Park383a4bc2017-08-07 16:12:55 +09001171
Ming Leidb0002a2009-07-16 15:44:29 +02001172 print_lock(check_tgt);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001173 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1174 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001175
1176 print_circular_bug_entry(entry, depth);
1177
1178 return 0;
1179}
1180
Ming Lei9e2d5512009-07-16 15:44:29 +02001181static inline int class_equal(struct lock_list *entry, void *data)
1182{
1183 return entry->class == data;
1184}
1185
Ming Leidb0002a2009-07-16 15:44:29 +02001186static noinline int print_circular_bug(struct lock_list *this,
1187 struct lock_list *target,
1188 struct held_lock *check_src,
Byungchul Park383a4bc2017-08-07 16:12:55 +09001189 struct held_lock *check_tgt,
1190 struct stack_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001191{
1192 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001193 struct lock_list *parent;
Steven Rostedtf4185812011-04-20 21:41:55 -04001194 struct lock_list *first_parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001195 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001196
Ming Leic94aa5c2009-07-16 15:44:29 +02001197 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001198 return 0;
1199
Ingo Molnare966eae2017-12-12 12:31:16 +01001200 if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001201 return 0;
1202
Ming Leic94aa5c2009-07-16 15:44:29 +02001203 depth = get_lock_depth(target);
1204
Ming Leidb0002a2009-07-16 15:44:29 +02001205 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001206
1207 parent = get_lock_parent(target);
Steven Rostedtf4185812011-04-20 21:41:55 -04001208 first_parent = parent;
Ming Leic94aa5c2009-07-16 15:44:29 +02001209
1210 while (parent) {
1211 print_circular_bug_entry(parent, --depth);
1212 parent = get_lock_parent(parent);
1213 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001214
1215 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001216 print_circular_lock_scenario(check_src, check_tgt,
1217 first_parent);
1218
Peter Zijlstra8e182572007-07-19 01:48:54 -07001219 lockdep_print_held_locks(curr);
1220
1221 printk("\nstack backtrace:\n");
1222 dump_stack();
1223
1224 return 0;
1225}
1226
Ming Leidb0002a2009-07-16 15:44:29 +02001227static noinline int print_bfs_bug(int ret)
1228{
1229 if (!debug_locks_off_graph_unlock())
1230 return 0;
1231
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001232 /*
1233 * Breadth-first-search failed, graph got corrupted?
1234 */
Ming Leidb0002a2009-07-16 15:44:29 +02001235 WARN(1, "lockdep bfs error:%d\n", ret);
1236
1237 return 0;
1238}
1239
Ming Leief681022009-07-16 15:44:29 +02001240static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001241{
Ming Leief681022009-07-16 15:44:29 +02001242 (*(unsigned long *)data)++;
1243 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001244}
1245
Fengguang Wu5216d532013-11-09 00:55:35 +08001246static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
Ming Leief681022009-07-16 15:44:29 +02001247{
1248 unsigned long count = 0;
1249 struct lock_list *uninitialized_var(target_entry);
1250
1251 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1252
1253 return count;
1254}
David Miller419ca3f2008-07-29 21:45:03 -07001255unsigned long lockdep_count_forward_deps(struct lock_class *class)
1256{
1257 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001258 struct lock_list this;
1259
1260 this.parent = NULL;
1261 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001262
1263 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001264 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001265 ret = __lockdep_count_forward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001266 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001267 local_irq_restore(flags);
1268
1269 return ret;
1270}
1271
Fengguang Wu5216d532013-11-09 00:55:35 +08001272static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001273{
Ming Leief681022009-07-16 15:44:29 +02001274 unsigned long count = 0;
1275 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001276
Ming Leief681022009-07-16 15:44:29 +02001277 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001278
Ming Leief681022009-07-16 15:44:29 +02001279 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001280}
1281
1282unsigned long lockdep_count_backward_deps(struct lock_class *class)
1283{
1284 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001285 struct lock_list this;
1286
1287 this.parent = NULL;
1288 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001289
1290 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001291 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001292 ret = __lockdep_count_backward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001293 arch_spin_unlock(&lockdep_lock);
David Miller419ca3f2008-07-29 21:45:03 -07001294 local_irq_restore(flags);
1295
1296 return ret;
1297}
1298
Peter Zijlstra8e182572007-07-19 01:48:54 -07001299/*
1300 * Prove that the dependency graph starting at <entry> can not
1301 * lead to <target>. Print an error and return 0 if it does.
1302 */
1303static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001304check_noncircular(struct lock_list *root, struct lock_class *target,
1305 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001306{
Ming Leidb0002a2009-07-16 15:44:29 +02001307 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001308
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001309 debug_atomic_inc(nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001310
Ming Leid7aaba12009-07-16 15:44:29 +02001311 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001312
1313 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001314}
1315
Peter Zijlstraae813302017-03-03 10:13:38 +01001316static noinline int
1317check_redundant(struct lock_list *root, struct lock_class *target,
1318 struct lock_list **target_entry)
1319{
1320 int result;
1321
1322 debug_atomic_inc(nr_redundant_checks);
1323
1324 result = __bfs_forwards(root, target, class_equal, target_entry);
1325
1326 return result;
1327}
1328
Steven Rostedt81d68a92008-05-12 21:20:42 +02001329#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001330/*
1331 * Forwards and backwards subgraph searching, for the purposes of
1332 * proving that two subgraphs can be connected by a new dependency
1333 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1334 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001335
Ming Leid7aaba12009-07-16 15:44:29 +02001336static inline int usage_match(struct lock_list *entry, void *bit)
1337{
1338 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1339}
1340
1341
1342
Peter Zijlstra8e182572007-07-19 01:48:54 -07001343/*
1344 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001345 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001346 *
Ming Leid7aaba12009-07-16 15:44:29 +02001347 * Return 0 if such a node exists in the subgraph, and put that node
1348 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001349 *
Ming Leid7aaba12009-07-16 15:44:29 +02001350 * Return 1 otherwise and keep *@target_entry unchanged.
1351 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001352 */
Ming Leid7aaba12009-07-16 15:44:29 +02001353static int
1354find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1355 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001356{
Ming Leid7aaba12009-07-16 15:44:29 +02001357 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001358
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001359 debug_atomic_inc(nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001360
Ming Leid7aaba12009-07-16 15:44:29 +02001361 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1362
1363 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001364}
1365
1366/*
1367 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001368 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001369 *
Ming Leid7aaba12009-07-16 15:44:29 +02001370 * Return 0 if such a node exists in the subgraph, and put that node
1371 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001372 *
Ming Leid7aaba12009-07-16 15:44:29 +02001373 * Return 1 otherwise and keep *@target_entry unchanged.
1374 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001375 */
Ming Leid7aaba12009-07-16 15:44:29 +02001376static int
1377find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1378 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001379{
Ming Leid7aaba12009-07-16 15:44:29 +02001380 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001381
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001382 debug_atomic_inc(nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001383
Ming Leid7aaba12009-07-16 15:44:29 +02001384 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001385
Ming Leid7aaba12009-07-16 15:44:29 +02001386 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001387}
1388
Peter Zijlstraaf012962009-07-16 15:44:29 +02001389static void print_lock_class_header(struct lock_class *class, int depth)
1390{
1391 int bit;
1392
1393 printk("%*s->", depth, "");
1394 print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001395 printk(KERN_CONT " ops: %lu", class->ops);
1396 printk(KERN_CONT " {\n");
Peter Zijlstraaf012962009-07-16 15:44:29 +02001397
1398 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1399 if (class->usage_mask & (1 << bit)) {
1400 int len = depth;
1401
1402 len += printk("%*s %s", depth, "", usage_str[bit]);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001403 len += printk(KERN_CONT " at:\n");
Peter Zijlstraaf012962009-07-16 15:44:29 +02001404 print_stack_trace(class->usage_traces + bit, len);
1405 }
1406 }
1407 printk("%*s }\n", depth, "");
1408
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001409 printk("%*s ... key at: [<%p>] %pS\n",
1410 depth, "", class->key, class->key);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001411}
1412
1413/*
1414 * printk the shortest lock dependencies from @start to @end in reverse order:
1415 */
1416static void __used
1417print_shortest_lock_dependencies(struct lock_list *leaf,
1418 struct lock_list *root)
1419{
1420 struct lock_list *entry = leaf;
1421 int depth;
1422
1423 /*compute depth from generated tree by BFS*/
1424 depth = get_lock_depth(leaf);
1425
1426 do {
1427 print_lock_class_header(entry->class, depth);
1428 printk("%*s ... acquired at:\n", depth, "");
1429 print_stack_trace(&entry->trace, 2);
1430 printk("\n");
1431
1432 if (depth == 0 && (entry != root)) {
Steven Rostedt6be8c392011-04-20 21:41:58 -04001433 printk("lockdep:%s bad path found in chain graph\n", __func__);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001434 break;
1435 }
1436
1437 entry = get_lock_parent(entry);
1438 depth--;
1439 } while (entry && (depth >= 0));
1440
1441 return;
1442}
Ming Leid7aaba12009-07-16 15:44:29 +02001443
Steven Rostedt3003eba2011-04-20 21:41:54 -04001444static void
1445print_irq_lock_scenario(struct lock_list *safe_entry,
1446 struct lock_list *unsafe_entry,
Steven Rostedtdad3d742011-04-20 21:41:57 -04001447 struct lock_class *prev_class,
1448 struct lock_class *next_class)
Steven Rostedt3003eba2011-04-20 21:41:54 -04001449{
1450 struct lock_class *safe_class = safe_entry->class;
1451 struct lock_class *unsafe_class = unsafe_entry->class;
Steven Rostedtdad3d742011-04-20 21:41:57 -04001452 struct lock_class *middle_class = prev_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001453
1454 if (middle_class == safe_class)
Steven Rostedtdad3d742011-04-20 21:41:57 -04001455 middle_class = next_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001456
1457 /*
1458 * A direct locking problem where unsafe_class lock is taken
1459 * directly by safe_class lock, then all we need to show
1460 * is the deadlock scenario, as it is obvious that the
1461 * unsafe lock is taken under the safe lock.
1462 *
1463 * But if there is a chain instead, where the safe lock takes
1464 * an intermediate lock (middle_class) where this lock is
1465 * not the same as the safe lock, then the lock chain is
1466 * used to describe the problem. Otherwise we would need
1467 * to show a different CPU case for each link in the chain
1468 * from the safe_class lock to the unsafe_class lock.
1469 */
1470 if (middle_class != unsafe_class) {
1471 printk("Chain exists of:\n ");
1472 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001473 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001474 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001475 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001476 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001477 printk(KERN_CONT "\n\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001478 }
1479
1480 printk(" Possible interrupt unsafe locking scenario:\n\n");
1481 printk(" CPU0 CPU1\n");
1482 printk(" ---- ----\n");
1483 printk(" lock(");
1484 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001485 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001486 printk(" local_irq_disable();\n");
1487 printk(" lock(");
1488 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001489 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001490 printk(" lock(");
1491 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001492 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001493 printk(" <Interrupt>\n");
1494 printk(" lock(");
1495 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001496 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001497 printk("\n *** DEADLOCK ***\n\n");
1498}
1499
Peter Zijlstra8e182572007-07-19 01:48:54 -07001500static int
1501print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001502 struct lock_list *prev_root,
1503 struct lock_list *next_root,
1504 struct lock_list *backwards_entry,
1505 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001506 struct held_lock *prev,
1507 struct held_lock *next,
1508 enum lock_usage_bit bit1,
1509 enum lock_usage_bit bit2,
1510 const char *irqclass)
1511{
1512 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1513 return 0;
1514
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001515 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001516 pr_warn("=====================================================\n");
1517 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07001518 irqclass, irqclass);
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001519 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001520 pr_warn("-----------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001521 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 -07001522 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001523 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1524 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1525 curr->hardirqs_enabled,
1526 curr->softirqs_enabled);
1527 print_lock(next);
1528
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001529 pr_warn("\nand this task is already holding:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001530 print_lock(prev);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001531 pr_warn("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001532 print_lock_name(hlock_class(prev));
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001533 pr_cont(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001534 print_lock_name(hlock_class(next));
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001535 pr_cont("\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001536
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001537 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07001538 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001539 print_lock_name(backwards_entry->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001540 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001541
Ming Lei24208ca2009-07-16 15:44:29 +02001542 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001543
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001544 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001545 print_lock_name(forwards_entry->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001546 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
1547 pr_warn("...");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001548
Ming Lei24208ca2009-07-16 15:44:29 +02001549 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001550
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001551 pr_warn("\nother info that might help us debug this:\n\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04001552 print_irq_lock_scenario(backwards_entry, forwards_entry,
1553 hlock_class(prev), hlock_class(next));
Steven Rostedt3003eba2011-04-20 21:41:54 -04001554
Peter Zijlstra8e182572007-07-19 01:48:54 -07001555 lockdep_print_held_locks(curr);
1556
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001557 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001558 if (!save_trace(&prev_root->trace))
1559 return 0;
1560 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001561
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001562 pr_warn("\nthe dependencies between the lock to be acquired");
1563 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001564 if (!save_trace(&next_root->trace))
1565 return 0;
1566 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001567
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001568 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001569 dump_stack();
1570
1571 return 0;
1572}
1573
1574static int
1575check_usage(struct task_struct *curr, struct held_lock *prev,
1576 struct held_lock *next, enum lock_usage_bit bit_backwards,
1577 enum lock_usage_bit bit_forwards, const char *irqclass)
1578{
1579 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001580 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001581 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001582 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001583
Ming Leid7aaba12009-07-16 15:44:29 +02001584 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001585
Ming Leid7aaba12009-07-16 15:44:29 +02001586 this.class = hlock_class(prev);
1587 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001588 if (ret < 0)
1589 return print_bfs_bug(ret);
1590 if (ret == 1)
1591 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001592
Ming Lei24208ca2009-07-16 15:44:29 +02001593 that.parent = NULL;
1594 that.class = hlock_class(next);
1595 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001596 if (ret < 0)
1597 return print_bfs_bug(ret);
1598 if (ret == 1)
1599 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001600
Ming Lei24208ca2009-07-16 15:44:29 +02001601 return print_bad_irq_dependency(curr, &this, &that,
1602 target_entry, target_entry1,
1603 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001604 bit_backwards, bit_forwards, irqclass);
1605}
1606
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001607static const char *state_names[] = {
1608#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001609 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001610#include "lockdep_states.h"
1611#undef LOCKDEP_STATE
1612};
1613
1614static const char *state_rnames[] = {
1615#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001616 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001617#include "lockdep_states.h"
1618#undef LOCKDEP_STATE
1619};
1620
1621static inline const char *state_name(enum lock_usage_bit bit)
1622{
1623 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1624}
1625
1626static int exclusive_bit(int new_bit)
1627{
1628 /*
1629 * USED_IN
1630 * USED_IN_READ
1631 * ENABLED
1632 * ENABLED_READ
1633 *
1634 * bit 0 - write/read
1635 * bit 1 - used_in/enabled
1636 * bit 2+ state
1637 */
1638
1639 int state = new_bit & ~3;
1640 int dir = new_bit & 2;
1641
1642 /*
1643 * keep state, bit flip the direction and strip read.
1644 */
1645 return state | (dir ^ 2);
1646}
1647
1648static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1649 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001650{
1651 /*
1652 * Prove that the new dependency does not connect a hardirq-safe
1653 * lock with a hardirq-unsafe lock - to achieve this we search
1654 * the backwards-subgraph starting at <prev>, and the
1655 * forwards-subgraph starting at <next>:
1656 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001657 if (!check_usage(curr, prev, next, bit,
1658 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001659 return 0;
1660
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001661 bit++; /* _READ */
1662
Peter Zijlstra8e182572007-07-19 01:48:54 -07001663 /*
1664 * Prove that the new dependency does not connect a hardirq-safe-read
1665 * lock with a hardirq-unsafe lock - to achieve this we search
1666 * the backwards-subgraph starting at <prev>, and the
1667 * forwards-subgraph starting at <next>:
1668 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001669 if (!check_usage(curr, prev, next, bit,
1670 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001671 return 0;
1672
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001673 return 1;
1674}
Peter Zijlstra8e182572007-07-19 01:48:54 -07001675
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001676static int
1677check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1678 struct held_lock *next)
1679{
1680#define LOCKDEP_STATE(__STATE) \
1681 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01001682 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001683#include "lockdep_states.h"
1684#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01001685
Peter Zijlstra8e182572007-07-19 01:48:54 -07001686 return 1;
1687}
1688
1689static void inc_chains(void)
1690{
1691 if (current->hardirq_context)
1692 nr_hardirq_chains++;
1693 else {
1694 if (current->softirq_context)
1695 nr_softirq_chains++;
1696 else
1697 nr_process_chains++;
1698 }
1699}
1700
1701#else
1702
1703static inline int
1704check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1705 struct held_lock *next)
1706{
1707 return 1;
1708}
1709
1710static inline void inc_chains(void)
1711{
1712 nr_process_chains++;
1713}
1714
1715#endif
1716
Steven Rostedt48702ec2011-04-20 21:41:56 -04001717static void
1718print_deadlock_scenario(struct held_lock *nxt,
1719 struct held_lock *prv)
1720{
1721 struct lock_class *next = hlock_class(nxt);
1722 struct lock_class *prev = hlock_class(prv);
1723
1724 printk(" Possible unsafe locking scenario:\n\n");
1725 printk(" CPU0\n");
1726 printk(" ----\n");
1727 printk(" lock(");
1728 __print_lock_name(prev);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001729 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001730 printk(" lock(");
1731 __print_lock_name(next);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001732 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001733 printk("\n *** DEADLOCK ***\n\n");
1734 printk(" May be due to missing lock nesting notation\n\n");
1735}
1736
Peter Zijlstra8e182572007-07-19 01:48:54 -07001737static int
1738print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1739 struct held_lock *next)
1740{
1741 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1742 return 0;
1743
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001744 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001745 pr_warn("============================================\n");
1746 pr_warn("WARNING: possible recursive locking detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001747 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001748 pr_warn("--------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001749 pr_warn("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001750 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07001751 print_lock(next);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001752 pr_warn("\nbut task is already holding lock:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001753 print_lock(prev);
1754
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001755 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04001756 print_deadlock_scenario(next, prev);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001757 lockdep_print_held_locks(curr);
1758
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001759 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001760 dump_stack();
1761
1762 return 0;
1763}
1764
1765/*
1766 * Check whether we are holding such a class already.
1767 *
1768 * (Note that this has to be done separately, because the graph cannot
1769 * detect such classes of deadlocks.)
1770 *
1771 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1772 */
1773static int
1774check_deadlock(struct task_struct *curr, struct held_lock *next,
1775 struct lockdep_map *next_instance, int read)
1776{
1777 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001778 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001779 int i;
1780
1781 for (i = 0; i < curr->lockdep_depth; i++) {
1782 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001783
1784 if (prev->instance == next->nest_lock)
1785 nest = prev;
1786
Dave Jonesf82b2172008-08-11 09:30:23 +02001787 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001788 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001789
Peter Zijlstra8e182572007-07-19 01:48:54 -07001790 /*
1791 * Allow read-after-read recursion of the same
1792 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1793 */
1794 if ((read == 2) && prev->read)
1795 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02001796
1797 /*
1798 * We're holding the nest_lock, which serializes this lock's
1799 * nesting behaviour.
1800 */
1801 if (nest)
1802 return 2;
1803
Peter Zijlstra8e182572007-07-19 01:48:54 -07001804 return print_deadlock_bug(curr, prev, next);
1805 }
1806 return 1;
1807}
1808
1809/*
1810 * There was a chain-cache miss, and we are about to add a new dependency
1811 * to a previous lock. We recursively validate the following rules:
1812 *
1813 * - would the adding of the <prev> -> <next> dependency create a
1814 * circular dependency in the graph? [== circular deadlock]
1815 *
1816 * - does the new prev->next dependency connect any hardirq-safe lock
1817 * (in the full backwards-subgraph starting at <prev>) with any
1818 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1819 * <next>)? [== illegal lock inversion with hardirq contexts]
1820 *
1821 * - does the new prev->next dependency connect any softirq-safe lock
1822 * (in the full backwards-subgraph starting at <prev>) with any
1823 * softirq-unsafe lock (in the full forwards-subgraph starting at
1824 * <next>)? [== illegal lock inversion with softirq contexts]
1825 *
1826 * any of these scenarios could lead to a deadlock.
1827 *
1828 * Then if all the validations pass, we add the forwards and backwards
1829 * dependency.
1830 */
1831static int
1832check_prev_add(struct task_struct *curr, struct held_lock *prev,
Byungchul Parkce07a9412017-08-07 16:12:51 +09001833 struct held_lock *next, int distance, struct stack_trace *trace,
1834 int (*save)(struct stack_trace *trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001835{
Ming Leidb0002a2009-07-16 15:44:29 +02001836 struct lock_list *uninitialized_var(target_entry);
Peter Zijlstra8b405d52017-10-04 11:13:37 +02001837 struct lock_list *entry;
1838 struct lock_list this;
1839 int ret;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001840
1841 /*
1842 * Prove that the new <prev> -> <next> dependency would not
1843 * create a circular dependency in the graph. (We do this by
1844 * forward-recursing into the graph starting at <next>, and
1845 * checking whether we can reach <prev>.)
1846 *
1847 * We are using global variables to control the recursion, to
1848 * keep the stackframe size of the recursive functions low:
1849 */
Ming Leidb0002a2009-07-16 15:44:29 +02001850 this.class = hlock_class(next);
1851 this.parent = NULL;
1852 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
Peter Zijlstra8b405d52017-10-04 11:13:37 +02001853 if (unlikely(!ret)) {
1854 if (!trace->entries) {
1855 /*
1856 * If @save fails here, the printing might trigger
1857 * a WARN but because of the !nr_entries it should
1858 * not do bad things.
1859 */
1860 save(trace);
1861 }
Byungchul Park383a4bc2017-08-07 16:12:55 +09001862 return print_circular_bug(&this, target_entry, next, prev, trace);
Peter Zijlstra8b405d52017-10-04 11:13:37 +02001863 }
Ming Leidb0002a2009-07-16 15:44:29 +02001864 else if (unlikely(ret < 0))
1865 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02001866
Peter Zijlstra8e182572007-07-19 01:48:54 -07001867 if (!check_prev_add_irq(curr, prev, next))
1868 return 0;
1869
1870 /*
1871 * For recursive read-locks we do all the dependency checks,
1872 * but we dont store read-triggered dependencies (only
1873 * write-triggered dependencies). This ensures that only the
1874 * write-side dependencies matter, and that if for example a
1875 * write-lock never takes any other locks, then the reads are
1876 * equivalent to a NOP.
1877 */
1878 if (next->read == 2 || prev->read == 2)
1879 return 1;
1880 /*
1881 * Is the <prev> -> <next> dependency already present?
1882 *
1883 * (this may occur even though this is a new chain: consider
1884 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1885 * chains - the second one will be new, but L1 already has
1886 * L2 added to its dependency list, due to the first chain.)
1887 */
Dave Jonesf82b2172008-08-11 09:30:23 +02001888 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1889 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001890 if (distance == 1)
1891 entry->distance = 1;
Byungchul Park70911fd2017-08-07 16:12:50 +09001892 return 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001893 }
1894 }
1895
Peter Zijlstraae813302017-03-03 10:13:38 +01001896 /*
1897 * Is the <prev> -> <next> link redundant?
1898 */
1899 this.class = hlock_class(prev);
1900 this.parent = NULL;
1901 ret = check_redundant(&this, hlock_class(next), &target_entry);
1902 if (!ret) {
1903 debug_atomic_inc(nr_redundant);
1904 return 2;
1905 }
1906 if (ret < 0)
1907 return print_bfs_bug(ret);
1908
1909
Peter Zijlstra8b405d52017-10-04 11:13:37 +02001910 if (!trace->entries && !save(trace))
Byungchul Parkce07a9412017-08-07 16:12:51 +09001911 return 0;
Yong Zhang4726f2a2010-05-04 14:16:48 +08001912
Peter Zijlstra8e182572007-07-19 01:48:54 -07001913 /*
1914 * Ok, all validations passed, add the new lock
1915 * to the previous lock's dependency list:
1916 */
Tahsin Erdogan83f06162016-11-08 00:02:07 -08001917 ret = add_lock_to_list(hlock_class(next),
Dave Jonesf82b2172008-08-11 09:30:23 +02001918 &hlock_class(prev)->locks_after,
Byungchul Parkce07a9412017-08-07 16:12:51 +09001919 next->acquire_ip, distance, trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001920
1921 if (!ret)
1922 return 0;
1923
Tahsin Erdogan83f06162016-11-08 00:02:07 -08001924 ret = add_lock_to_list(hlock_class(prev),
Dave Jonesf82b2172008-08-11 09:30:23 +02001925 &hlock_class(next)->locks_before,
Byungchul Parkce07a9412017-08-07 16:12:51 +09001926 next->acquire_ip, distance, trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001927 if (!ret)
1928 return 0;
1929
Byungchul Park70911fd2017-08-07 16:12:50 +09001930 return 2;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001931}
1932
1933/*
1934 * Add the dependency to all directly-previous locks that are 'relevant'.
1935 * The ones that are relevant are (in increasing distance from curr):
1936 * all consecutive trylock entries and the final non-trylock entry - or
1937 * the end of this context's lock-chain - whichever comes first.
1938 */
1939static int
1940check_prevs_add(struct task_struct *curr, struct held_lock *next)
1941{
1942 int depth = curr->lockdep_depth;
1943 struct held_lock *hlock;
Peter Zijlstra8b405d52017-10-04 11:13:37 +02001944 struct stack_trace trace = {
1945 .nr_entries = 0,
1946 .max_entries = 0,
1947 .entries = NULL,
1948 .skip = 0,
1949 };
Peter Zijlstra8e182572007-07-19 01:48:54 -07001950
1951 /*
1952 * Debugging checks.
1953 *
1954 * Depth must not be zero for a non-head lock:
1955 */
1956 if (!depth)
1957 goto out_bug;
1958 /*
1959 * At least two relevant locks must exist for this
1960 * to be a head:
1961 */
1962 if (curr->held_locks[depth].irq_context !=
1963 curr->held_locks[depth-1].irq_context)
1964 goto out_bug;
1965
1966 for (;;) {
1967 int distance = curr->lockdep_depth - depth + 1;
Oleg Nesterov1b5ff812014-01-20 19:20:10 +01001968 hlock = curr->held_locks + depth - 1;
Byungchul Parkce07a9412017-08-07 16:12:51 +09001969
Ingo Molnare966eae2017-12-12 12:31:16 +01001970 /*
1971 * Only non-recursive-read entries get new dependencies
1972 * added:
1973 */
1974 if (hlock->read != 2 && hlock->check) {
1975 int ret = check_prev_add(curr, hlock, next, distance, &trace, save_trace);
1976 if (!ret)
1977 return 0;
1978
1979 /*
1980 * Stop after the first non-trylock entry,
1981 * as non-trylock entries have added their
1982 * own direct dependencies already, so this
1983 * lock is connected to them indirectly:
1984 */
1985 if (!hlock->trylock)
1986 break;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001987 }
Ingo Molnare966eae2017-12-12 12:31:16 +01001988
Peter Zijlstra8e182572007-07-19 01:48:54 -07001989 depth--;
1990 /*
1991 * End of lock-stack?
1992 */
1993 if (!depth)
1994 break;
1995 /*
1996 * Stop the search if we cross into another context:
1997 */
1998 if (curr->held_locks[depth].irq_context !=
1999 curr->held_locks[depth-1].irq_context)
2000 break;
2001 }
2002 return 1;
2003out_bug:
2004 if (!debug_locks_off_graph_unlock())
2005 return 0;
2006
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002007 /*
2008 * Clearly we all shouldn't be here, but since we made it we
2009 * can reliable say we messed up our state. See the above two
2010 * gotos for reasons why we could possibly end up here.
2011 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002012 WARN_ON(1);
2013
2014 return 0;
2015}
2016
2017unsigned long nr_lock_chains;
Huang, Ying443cd502008-06-20 16:39:21 +08002018struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Huang, Yingcd1a28e2008-06-23 11:20:54 +08002019int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08002020static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
2021
2022struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
2023{
2024 return lock_classes + chain_hlocks[chain->base + i];
2025}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002026
2027/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002028 * Returns the index of the first held_lock of the current chain
2029 */
2030static inline int get_first_held_lock(struct task_struct *curr,
2031 struct held_lock *hlock)
2032{
2033 int i;
2034 struct held_lock *hlock_curr;
2035
2036 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2037 hlock_curr = curr->held_locks + i;
2038 if (hlock_curr->irq_context != hlock->irq_context)
2039 break;
2040
2041 }
2042
2043 return ++i;
2044}
2045
Borislav Petkov5c8a0102016-04-04 10:42:07 +02002046#ifdef CONFIG_DEBUG_LOCKDEP
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002047/*
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002048 * Returns the next chain_key iteration
2049 */
2050static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
2051{
2052 u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
2053
2054 printk(" class_idx:%d -> chain_key:%016Lx",
2055 class_idx,
2056 (unsigned long long)new_chain_key);
2057 return new_chain_key;
2058}
2059
2060static void
2061print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
2062{
2063 struct held_lock *hlock;
2064 u64 chain_key = 0;
2065 int depth = curr->lockdep_depth;
2066 int i;
2067
2068 printk("depth: %u\n", depth + 1);
2069 for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
2070 hlock = curr->held_locks + i;
2071 chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
2072
2073 print_lock(hlock);
2074 }
2075
2076 print_chain_key_iteration(hlock_next->class_idx, chain_key);
2077 print_lock(hlock_next);
2078}
2079
2080static void print_chain_keys_chain(struct lock_chain *chain)
2081{
2082 int i;
2083 u64 chain_key = 0;
2084 int class_id;
2085
2086 printk("depth: %u\n", chain->depth);
2087 for (i = 0; i < chain->depth; i++) {
2088 class_id = chain_hlocks[chain->base + i];
2089 chain_key = print_chain_key_iteration(class_id + 1, chain_key);
2090
2091 print_lock_name(lock_classes + class_id);
2092 printk("\n");
2093 }
2094}
2095
2096static void print_collision(struct task_struct *curr,
2097 struct held_lock *hlock_next,
2098 struct lock_chain *chain)
2099{
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002100 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002101 pr_warn("============================\n");
2102 pr_warn("WARNING: chain_key collision\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002103 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002104 pr_warn("----------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002105 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
2106 pr_warn("Hash chain already cached but the contents don't match!\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002107
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002108 pr_warn("Held locks:");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002109 print_chain_keys_held_locks(curr, hlock_next);
2110
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002111 pr_warn("Locks in cached chain:");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002112 print_chain_keys_chain(chain);
2113
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002114 pr_warn("\nstack backtrace:\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002115 dump_stack();
2116}
Borislav Petkov5c8a0102016-04-04 10:42:07 +02002117#endif
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002118
2119/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002120 * Checks whether the chain and the current held locks are consistent
2121 * in depth and also in content. If they are not it most likely means
2122 * that there was a collision during the calculation of the chain_key.
2123 * Returns: 0 not passed, 1 passed
2124 */
2125static int check_no_collision(struct task_struct *curr,
2126 struct held_lock *hlock,
2127 struct lock_chain *chain)
2128{
2129#ifdef CONFIG_DEBUG_LOCKDEP
2130 int i, j, id;
2131
2132 i = get_first_held_lock(curr, hlock);
2133
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002134 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
2135 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002136 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002137 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002138
2139 for (j = 0; j < chain->depth - 1; j++, i++) {
2140 id = curr->held_locks[i].class_idx - 1;
2141
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002142 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
2143 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002144 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002145 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002146 }
2147#endif
2148 return 1;
2149}
2150
2151/*
Byungchul Park49347a92017-08-07 16:12:49 +09002152 * This is for building a chain between just two different classes,
2153 * instead of adding a new hlock upon current, which is done by
2154 * add_chain_cache().
2155 *
2156 * This can be called in any context with two classes, while
2157 * add_chain_cache() must be done within the lock owener's context
2158 * since it uses hlock which might be racy in another context.
2159 */
2160static inline int add_chain_cache_classes(unsigned int prev,
2161 unsigned int next,
2162 unsigned int irq_context,
2163 u64 chain_key)
2164{
2165 struct hlist_head *hash_head = chainhashentry(chain_key);
2166 struct lock_chain *chain;
2167
2168 /*
2169 * Allocate a new chain entry from the static array, and add
2170 * it to the hash:
2171 */
2172
2173 /*
2174 * We might need to take the graph lock, ensure we've got IRQs
2175 * disabled to make this an IRQ-safe lock.. for recursion reasons
2176 * lockdep won't complain about its own locking errors.
2177 */
2178 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2179 return 0;
2180
2181 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2182 if (!debug_locks_off_graph_unlock())
2183 return 0;
2184
2185 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2186 dump_stack();
2187 return 0;
2188 }
2189
2190 chain = lock_chains + nr_lock_chains++;
2191 chain->chain_key = chain_key;
2192 chain->irq_context = irq_context;
2193 chain->depth = 2;
2194 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2195 chain->base = nr_chain_hlocks;
2196 nr_chain_hlocks += chain->depth;
2197 chain_hlocks[chain->base] = prev - 1;
2198 chain_hlocks[chain->base + 1] = next -1;
2199 }
2200#ifdef CONFIG_DEBUG_LOCKDEP
2201 /*
2202 * Important for check_no_collision().
2203 */
2204 else {
2205 if (!debug_locks_off_graph_unlock())
2206 return 0;
2207
2208 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2209 dump_stack();
2210 return 0;
2211 }
2212#endif
2213
2214 hlist_add_head_rcu(&chain->entry, hash_head);
2215 debug_atomic_inc(chain_lookup_misses);
2216 inc_chains();
2217
2218 return 1;
2219}
2220
2221/*
Byungchul Park545c23f2017-08-07 16:12:48 +09002222 * Adds a dependency chain into chain hashtable. And must be called with
2223 * graph_lock held.
2224 *
2225 * Return 0 if fail, and graph_lock is released.
2226 * Return 1 if succeed, with graph_lock held.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002227 */
Byungchul Park545c23f2017-08-07 16:12:48 +09002228static inline int add_chain_cache(struct task_struct *curr,
2229 struct held_lock *hlock,
2230 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002231{
Dave Jonesf82b2172008-08-11 09:30:23 +02002232 struct lock_class *class = hlock_class(hlock);
Andrew Mortona63f38c2016-02-03 13:44:12 -08002233 struct hlist_head *hash_head = chainhashentry(chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002234 struct lock_chain *chain;
Steven Rostedte0944ee2011-04-20 21:42:00 -04002235 int i, j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002236
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002237 /*
Byungchul Park545c23f2017-08-07 16:12:48 +09002238 * Allocate a new chain entry from the static array, and add
2239 * it to the hash:
2240 */
2241
2242 /*
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002243 * We might need to take the graph lock, ensure we've got IRQs
2244 * disabled to make this an IRQ-safe lock.. for recursion reasons
2245 * lockdep won't complain about its own locking errors.
2246 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08002247 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2248 return 0;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002249
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002250 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002251 if (!debug_locks_off_graph_unlock())
2252 return 0;
2253
Dave Jones2c522832013-04-25 13:40:02 -04002254 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002255 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002256 return 0;
2257 }
2258 chain = lock_chains + nr_lock_chains++;
2259 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08002260 chain->irq_context = hlock->irq_context;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002261 i = get_first_held_lock(curr, hlock);
Huang, Ying443cd502008-06-20 16:39:21 +08002262 chain->depth = curr->lockdep_depth + 1 - i;
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002263
2264 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
2265 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
2266 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
2267
Steven Rostedte0944ee2011-04-20 21:42:00 -04002268 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2269 chain->base = nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08002270 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02002271 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08002272 chain_hlocks[chain->base + j] = lock_id;
2273 }
2274 chain_hlocks[chain->base + j] = class - lock_classes;
2275 }
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002276
2277 if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS)
2278 nr_chain_hlocks += chain->depth;
2279
2280#ifdef CONFIG_DEBUG_LOCKDEP
2281 /*
2282 * Important for check_no_collision().
2283 */
2284 if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) {
Byungchul Parkf9af4562017-01-13 11:42:04 +09002285 if (!debug_locks_off_graph_unlock())
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002286 return 0;
2287
2288 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2289 dump_stack();
2290 return 0;
2291 }
2292#endif
2293
Andrew Mortona63f38c2016-02-03 13:44:12 -08002294 hlist_add_head_rcu(&chain->entry, hash_head);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002295 debug_atomic_inc(chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002296 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002297
2298 return 1;
2299}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002300
Byungchul Park545c23f2017-08-07 16:12:48 +09002301/*
2302 * Look up a dependency chain.
2303 */
2304static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
2305{
2306 struct hlist_head *hash_head = chainhashentry(chain_key);
2307 struct lock_chain *chain;
2308
2309 /*
2310 * We can walk it lock-free, because entries only get added
2311 * to the hash:
2312 */
2313 hlist_for_each_entry_rcu(chain, hash_head, entry) {
2314 if (chain->chain_key == chain_key) {
2315 debug_atomic_inc(chain_lookup_hits);
2316 return chain;
2317 }
2318 }
2319 return NULL;
2320}
2321
2322/*
2323 * If the key is not present yet in dependency chain cache then
2324 * add it and return 1 - in this case the new dependency chain is
2325 * validated. If the key is already hashed, return 0.
2326 * (On return with 1 graph_lock is held.)
2327 */
2328static inline int lookup_chain_cache_add(struct task_struct *curr,
2329 struct held_lock *hlock,
2330 u64 chain_key)
2331{
2332 struct lock_class *class = hlock_class(hlock);
2333 struct lock_chain *chain = lookup_chain_cache(chain_key);
2334
2335 if (chain) {
2336cache_hit:
2337 if (!check_no_collision(curr, hlock, chain))
2338 return 0;
2339
2340 if (very_verbose(class)) {
2341 printk("\nhash chain already cached, key: "
2342 "%016Lx tail class: [%p] %s\n",
2343 (unsigned long long)chain_key,
2344 class->key, class->name);
2345 }
2346
2347 return 0;
2348 }
2349
2350 if (very_verbose(class)) {
2351 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2352 (unsigned long long)chain_key, class->key, class->name);
2353 }
2354
2355 if (!graph_lock())
2356 return 0;
2357
2358 /*
2359 * We have to walk the chain again locked - to avoid duplicates:
2360 */
2361 chain = lookup_chain_cache(chain_key);
2362 if (chain) {
2363 graph_unlock();
2364 goto cache_hit;
2365 }
2366
2367 if (!add_chain_cache(curr, hlock, chain_key))
2368 return 0;
2369
2370 return 1;
2371}
2372
Peter Zijlstra8e182572007-07-19 01:48:54 -07002373static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07002374 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002375{
2376 /*
2377 * Trylock needs to maintain the stack of held locks, but it
2378 * does not add new dependencies, because trylock can be done
2379 * in any order.
2380 *
2381 * We look up the chain_key and do the O(N^2) check and update of
2382 * the dependencies only if this is a new dependency chain.
Byungchul Park545c23f2017-08-07 16:12:48 +09002383 * (If lookup_chain_cache_add() return with 1 it acquires
Peter Zijlstra8e182572007-07-19 01:48:54 -07002384 * graph_lock for us)
2385 */
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01002386 if (!hlock->trylock && hlock->check &&
Byungchul Park545c23f2017-08-07 16:12:48 +09002387 lookup_chain_cache_add(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002388 /*
2389 * Check whether last held lock:
2390 *
2391 * - is irq-safe, if this lock is irq-unsafe
2392 * - is softirq-safe, if this lock is hardirq-unsafe
2393 *
2394 * And check whether the new lock's dependency graph
2395 * could lead back to the previous lock.
2396 *
2397 * any of these scenarios could lead to a deadlock. If
2398 * All validations
2399 */
2400 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2401
2402 if (!ret)
2403 return 0;
2404 /*
2405 * Mark recursive read, as we jump over it when
2406 * building dependencies (just like we jump over
2407 * trylock entries):
2408 */
2409 if (ret == 2)
2410 hlock->read = 2;
2411 /*
2412 * Add dependency only if this lock is not the head
2413 * of the chain, and if it's not a secondary read-lock:
2414 */
Byungchul Park545c23f2017-08-07 16:12:48 +09002415 if (!chain_head && ret != 2) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002416 if (!check_prevs_add(curr, hlock))
2417 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09002418 }
2419
Peter Zijlstra8e182572007-07-19 01:48:54 -07002420 graph_unlock();
Byungchul Park545c23f2017-08-07 16:12:48 +09002421 } else {
2422 /* after lookup_chain_cache_add(): */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002423 if (unlikely(!debug_locks))
2424 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09002425 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07002426
2427 return 1;
2428}
2429#else
2430static inline int validate_chain(struct task_struct *curr,
2431 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002432 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002433{
2434 return 1;
2435}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07002436#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002437
2438/*
2439 * We are building curr_chain_key incrementally, so double-check
2440 * it from scratch, to make sure that it's done correctly:
2441 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002442static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002443{
2444#ifdef CONFIG_DEBUG_LOCKDEP
2445 struct held_lock *hlock, *prev_hlock = NULL;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002446 unsigned int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002447 u64 chain_key = 0;
2448
2449 for (i = 0; i < curr->lockdep_depth; i++) {
2450 hlock = curr->held_locks + i;
2451 if (chain_key != hlock->prev_chain_key) {
2452 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002453 /*
2454 * We got mighty confused, our chain keys don't match
2455 * with what we expect, someone trample on our task state?
2456 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002457 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002458 curr->lockdep_depth, i,
2459 (unsigned long long)chain_key,
2460 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002461 return;
2462 }
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002463 /*
2464 * Whoops ran out of static storage again?
2465 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002466 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
Jarek Poplawski381a2292007-02-10 01:44:58 -08002467 return;
2468
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002469 if (prev_hlock && (prev_hlock->irq_context !=
2470 hlock->irq_context))
2471 chain_key = 0;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002472 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002473 prev_hlock = hlock;
2474 }
2475 if (chain_key != curr->curr_chain_key) {
2476 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002477 /*
2478 * More smoking hash instead of calculating it, damn see these
2479 * numbers float.. I bet that a pink elephant stepped on my memory.
2480 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002481 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002482 curr->lockdep_depth, i,
2483 (unsigned long long)chain_key,
2484 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002485 }
2486#endif
2487}
2488
Steven Rostedt282b5c22011-04-20 21:41:59 -04002489static void
2490print_usage_bug_scenario(struct held_lock *lock)
2491{
2492 struct lock_class *class = hlock_class(lock);
2493
2494 printk(" Possible unsafe locking scenario:\n\n");
2495 printk(" CPU0\n");
2496 printk(" ----\n");
2497 printk(" lock(");
2498 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002499 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002500 printk(" <Interrupt>\n");
2501 printk(" lock(");
2502 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002503 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002504 printk("\n *** DEADLOCK ***\n\n");
2505}
2506
Peter Zijlstra8e182572007-07-19 01:48:54 -07002507static int
2508print_usage_bug(struct task_struct *curr, struct held_lock *this,
2509 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2510{
2511 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2512 return 0;
2513
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002514 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002515 pr_warn("================================\n");
2516 pr_warn("WARNING: inconsistent lock state\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002517 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002518 pr_warn("--------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002519
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002520 pr_warn("inconsistent {%s} -> {%s} usage.\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07002521 usage_str[prev_bit], usage_str[new_bit]);
2522
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002523 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002524 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002525 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2526 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2527 trace_hardirqs_enabled(curr),
2528 trace_softirqs_enabled(curr));
2529 print_lock(this);
2530
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002531 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002532 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002533
2534 print_irqtrace_events(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002535 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002536 print_usage_bug_scenario(this);
2537
Peter Zijlstra8e182572007-07-19 01:48:54 -07002538 lockdep_print_held_locks(curr);
2539
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002540 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002541 dump_stack();
2542
2543 return 0;
2544}
2545
2546/*
2547 * Print out an error if an invalid bit is set:
2548 */
2549static inline int
2550valid_state(struct task_struct *curr, struct held_lock *this,
2551 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2552{
Dave Jonesf82b2172008-08-11 09:30:23 +02002553 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002554 return print_usage_bug(curr, this, bad_bit, new_bit);
2555 return 1;
2556}
2557
2558static int mark_lock(struct task_struct *curr, struct held_lock *this,
2559 enum lock_usage_bit new_bit);
2560
Steven Rostedt81d68a92008-05-12 21:20:42 +02002561#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002562
2563/*
2564 * print irq inversion bug:
2565 */
2566static int
Ming Lei24208ca2009-07-16 15:44:29 +02002567print_irq_inversion_bug(struct task_struct *curr,
2568 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002569 struct held_lock *this, int forwards,
2570 const char *irqclass)
2571{
Steven Rostedtdad3d742011-04-20 21:41:57 -04002572 struct lock_list *entry = other;
2573 struct lock_list *middle = NULL;
2574 int depth;
2575
Ingo Molnar74c383f2006-12-13 00:34:43 -08002576 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002577 return 0;
2578
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002579 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002580 pr_warn("========================================================\n");
2581 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002582 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002583 pr_warn("--------------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002584 pr_warn("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002585 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002586 print_lock(this);
2587 if (forwards)
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002588 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002589 else
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002590 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002591 print_lock_name(other->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002592 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002593
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002594 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04002595
2596 /* Find a middle lock (if one exists) */
2597 depth = get_lock_depth(other);
2598 do {
2599 if (depth == 0 && (entry != root)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002600 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
Steven Rostedtdad3d742011-04-20 21:41:57 -04002601 break;
2602 }
2603 middle = entry;
2604 entry = get_lock_parent(entry);
2605 depth--;
2606 } while (entry && entry != root && (depth >= 0));
2607 if (forwards)
2608 print_irq_lock_scenario(root, other,
2609 middle ? middle->class : root->class, other->class);
2610 else
2611 print_irq_lock_scenario(other, root,
2612 middle ? middle->class : other->class, root->class);
2613
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002614 lockdep_print_held_locks(curr);
2615
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002616 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
Ming Lei24208ca2009-07-16 15:44:29 +02002617 if (!save_trace(&root->trace))
2618 return 0;
2619 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002620
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002621 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002622 dump_stack();
2623
2624 return 0;
2625}
2626
2627/*
2628 * Prove that in the forwards-direction subgraph starting at <this>
2629 * there is no lock matching <mask>:
2630 */
2631static int
2632check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2633 enum lock_usage_bit bit, const char *irqclass)
2634{
2635 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002636 struct lock_list root;
2637 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002638
Ming Leid7aaba12009-07-16 15:44:29 +02002639 root.parent = NULL;
2640 root.class = hlock_class(this);
2641 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002642 if (ret < 0)
2643 return print_bfs_bug(ret);
2644 if (ret == 1)
2645 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002646
Ming Lei24208ca2009-07-16 15:44:29 +02002647 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002648 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002649}
2650
2651/*
2652 * Prove that in the backwards-direction subgraph starting at <this>
2653 * there is no lock matching <mask>:
2654 */
2655static int
2656check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2657 enum lock_usage_bit bit, const char *irqclass)
2658{
2659 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002660 struct lock_list root;
2661 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002662
Ming Leid7aaba12009-07-16 15:44:29 +02002663 root.parent = NULL;
2664 root.class = hlock_class(this);
2665 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002666 if (ret < 0)
2667 return print_bfs_bug(ret);
2668 if (ret == 1)
2669 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002670
Ming Lei24208ca2009-07-16 15:44:29 +02002671 return print_irq_inversion_bug(curr, &root, target_entry,
Oleg Nesterov48d50672010-01-26 19:16:41 +01002672 this, 0, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002673}
2674
Ingo Molnar3117df02006-12-13 00:34:43 -08002675void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002676{
2677 printk("irq event stamp: %u\n", curr->irq_events);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002678 printk("hardirqs last enabled at (%u): [<%p>] %pS\n",
2679 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
2680 (void *)curr->hardirq_enable_ip);
2681 printk("hardirqs last disabled at (%u): [<%p>] %pS\n",
2682 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
2683 (void *)curr->hardirq_disable_ip);
2684 printk("softirqs last enabled at (%u): [<%p>] %pS\n",
2685 curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
2686 (void *)curr->softirq_enable_ip);
2687 printk("softirqs last disabled at (%u): [<%p>] %pS\n",
2688 curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
2689 (void *)curr->softirq_disable_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002690}
2691
Peter Zijlstracd953022009-01-22 16:38:21 +01002692static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002693{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002694#if HARDIRQ_VERBOSE
2695 return class_filter(class);
2696#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002697 return 0;
2698}
2699
Peter Zijlstracd953022009-01-22 16:38:21 +01002700static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002701{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002702#if SOFTIRQ_VERBOSE
2703 return class_filter(class);
2704#endif
2705 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002706}
2707
2708#define STRICT_READ_CHECKS 1
2709
Peter Zijlstracd953022009-01-22 16:38:21 +01002710static int (*state_verbose_f[])(struct lock_class *class) = {
2711#define LOCKDEP_STATE(__STATE) \
2712 __STATE##_verbose,
2713#include "lockdep_states.h"
2714#undef LOCKDEP_STATE
2715};
2716
2717static inline int state_verbose(enum lock_usage_bit bit,
2718 struct lock_class *class)
2719{
2720 return state_verbose_f[bit >> 2](class);
2721}
2722
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002723typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2724 enum lock_usage_bit bit, const char *name);
2725
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002726static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002727mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2728 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002729{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002730 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002731 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002732 int dir = new_bit & 2;
2733
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002734 /*
2735 * mark USED_IN has to look forwards -- to ensure no dependency
2736 * has ENABLED state, which would allow recursion deadlocks.
2737 *
2738 * mark ENABLED has to look backwards -- to ensure no dependee
2739 * has USED_IN state, which, again, would allow recursion deadlocks.
2740 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002741 check_usage_f usage = dir ?
2742 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002743
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002744 /*
2745 * Validate that this particular lock does not have conflicting
2746 * usage states.
2747 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002748 if (!valid_state(curr, this, new_bit, excl_bit))
2749 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002750
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002751 /*
2752 * Validate that the lock dependencies don't have conflicting usage
2753 * states.
2754 */
2755 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002756 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002757 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002758
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002759 /*
2760 * Check for read in write conflicts
2761 */
2762 if (!read) {
2763 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2764 return 0;
2765
2766 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002767 !usage(curr, this, excl_bit + 1,
2768 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002769 return 0;
2770 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002771
Peter Zijlstracd953022009-01-22 16:38:21 +01002772 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002773 return 2;
2774
2775 return 1;
2776}
2777
Nick Piggincf40bd12009-01-21 08:12:39 +01002778enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002779#define LOCKDEP_STATE(__STATE) __STATE,
2780#include "lockdep_states.h"
2781#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002782};
2783
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002784/*
2785 * Mark all held locks with a usage bit:
2786 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002787static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002788mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002789{
2790 enum lock_usage_bit usage_bit;
2791 struct held_lock *hlock;
2792 int i;
2793
2794 for (i = 0; i < curr->lockdep_depth; i++) {
2795 hlock = curr->held_locks + i;
2796
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002797 usage_bit = 2 + (mark << 2); /* ENABLED */
2798 if (hlock->read)
2799 usage_bit += 1; /* READ */
2800
2801 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002802
Oleg Nesterov34d0ed52014-01-20 19:20:13 +01002803 if (!hlock->check)
Peter Zijlstraefbe2ee2011-07-07 11:39:45 +02002804 continue;
2805
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002806 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002807 return 0;
2808 }
2809
2810 return 1;
2811}
2812
2813/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002814 * Hardirqs will be enabled:
2815 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002816static void __trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002817{
2818 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002819
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002820 /* we'll do an OFF -> ON transition: */
2821 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002822
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002823 /*
2824 * We are going to turn hardirqs on, so set the
2825 * usage bit for all held locks:
2826 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002827 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002828 return;
2829 /*
2830 * If we have softirqs enabled, then set the usage
2831 * bit for all held locks. (disabled hardirqs prevented
2832 * this bit from being set before)
2833 */
2834 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002835 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002836 return;
2837
2838 curr->hardirq_enable_ip = ip;
2839 curr->hardirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002840 debug_atomic_inc(hardirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002841}
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002842
Andi Kleenb35f8302014-02-08 08:52:02 +01002843__visible void trace_hardirqs_on_caller(unsigned long ip)
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002844{
2845 time_hardirqs_on(CALLER_ADDR0, ip);
2846
2847 if (unlikely(!debug_locks || current->lockdep_recursion))
2848 return;
2849
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002850 if (unlikely(current->hardirqs_enabled)) {
2851 /*
2852 * Neither irq nor preemption are disabled here
2853 * so this is racy by nature but losing one hit
2854 * in a stat is not a big deal.
2855 */
2856 __debug_atomic_inc(redundant_hardirqs_on);
2857 return;
2858 }
2859
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002860 /*
2861 * We're enabling irqs and according to our state above irqs weren't
2862 * already enabled, yet we find the hardware thinks they are in fact
2863 * enabled.. someone messed up their IRQ state tracing.
2864 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002865 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2866 return;
2867
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002868 /*
2869 * See the fine text that goes along with this variable definition.
2870 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002871 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2872 return;
2873
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002874 /*
2875 * Can't allow enabling interrupts while in an interrupt handler,
2876 * that's general bad form and such. Recursion, limited stack etc..
2877 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002878 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2879 return;
2880
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002881 current->lockdep_recursion = 1;
2882 __trace_hardirqs_on_caller(ip);
2883 current->lockdep_recursion = 0;
2884}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002885EXPORT_SYMBOL(trace_hardirqs_on_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002886
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002887void trace_hardirqs_on(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002888{
2889 trace_hardirqs_on_caller(CALLER_ADDR0);
2890}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002891EXPORT_SYMBOL(trace_hardirqs_on);
2892
2893/*
2894 * Hardirqs were disabled:
2895 */
Andi Kleenb35f8302014-02-08 08:52:02 +01002896__visible void trace_hardirqs_off_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002897{
2898 struct task_struct *curr = current;
2899
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002900 time_hardirqs_off(CALLER_ADDR0, ip);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002901
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002902 if (unlikely(!debug_locks || current->lockdep_recursion))
2903 return;
2904
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002905 /*
2906 * So we're supposed to get called after you mask local IRQs, but for
2907 * some reason the hardware doesn't quite think you did a proper job.
2908 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002909 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2910 return;
2911
2912 if (curr->hardirqs_enabled) {
2913 /*
2914 * We have done an ON -> OFF transition:
2915 */
2916 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002917 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002918 curr->hardirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002919 debug_atomic_inc(hardirqs_off_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002920 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002921 debug_atomic_inc(redundant_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002922}
Steven Rostedt81d68a92008-05-12 21:20:42 +02002923EXPORT_SYMBOL(trace_hardirqs_off_caller);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002924
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002925void trace_hardirqs_off(void)
Steven Rostedt81d68a92008-05-12 21:20:42 +02002926{
2927 trace_hardirqs_off_caller(CALLER_ADDR0);
2928}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002929EXPORT_SYMBOL(trace_hardirqs_off);
2930
2931/*
2932 * Softirqs will be enabled:
2933 */
2934void trace_softirqs_on(unsigned long ip)
2935{
2936 struct task_struct *curr = current;
2937
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002938 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002939 return;
2940
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002941 /*
2942 * We fancy IRQs being disabled here, see softirq.c, avoids
2943 * funny state and nesting things.
2944 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002945 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2946 return;
2947
2948 if (curr->softirqs_enabled) {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002949 debug_atomic_inc(redundant_softirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002950 return;
2951 }
2952
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002953 current->lockdep_recursion = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002954 /*
2955 * We'll do an OFF -> ON transition:
2956 */
2957 curr->softirqs_enabled = 1;
2958 curr->softirq_enable_ip = ip;
2959 curr->softirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002960 debug_atomic_inc(softirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002961 /*
2962 * We are going to turn softirqs on, so set the
2963 * usage bit for all held locks, if hardirqs are
2964 * enabled too:
2965 */
2966 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002967 mark_held_locks(curr, SOFTIRQ);
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002968 current->lockdep_recursion = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002969}
2970
2971/*
2972 * Softirqs were disabled:
2973 */
2974void trace_softirqs_off(unsigned long ip)
2975{
2976 struct task_struct *curr = current;
2977
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002978 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002979 return;
2980
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002981 /*
2982 * We fancy IRQs being disabled here, see softirq.c
2983 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002984 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2985 return;
2986
2987 if (curr->softirqs_enabled) {
2988 /*
2989 * We have done an ON -> OFF transition:
2990 */
2991 curr->softirqs_enabled = 0;
2992 curr->softirq_disable_ip = ip;
2993 curr->softirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002994 debug_atomic_inc(softirqs_off_events);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002995 /*
2996 * Whoops, we wanted softirqs off, so why aren't they?
2997 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002998 DEBUG_LOCKS_WARN_ON(!softirq_count());
2999 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003000 debug_atomic_inc(redundant_softirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003001}
3002
Peter Zijlstra8e182572007-07-19 01:48:54 -07003003static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
3004{
3005 /*
3006 * If non-trylock use in a hardirq or softirq context, then
3007 * mark the lock as used in these contexts:
3008 */
3009 if (!hlock->trylock) {
3010 if (hlock->read) {
3011 if (curr->hardirq_context)
3012 if (!mark_lock(curr, hlock,
3013 LOCK_USED_IN_HARDIRQ_READ))
3014 return 0;
3015 if (curr->softirq_context)
3016 if (!mark_lock(curr, hlock,
3017 LOCK_USED_IN_SOFTIRQ_READ))
3018 return 0;
3019 } else {
3020 if (curr->hardirq_context)
3021 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
3022 return 0;
3023 if (curr->softirq_context)
3024 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
3025 return 0;
3026 }
3027 }
3028 if (!hlock->hardirqs_off) {
3029 if (hlock->read) {
3030 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003031 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003032 return 0;
3033 if (curr->softirqs_enabled)
3034 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003035 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003036 return 0;
3037 } else {
3038 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003039 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003040 return 0;
3041 if (curr->softirqs_enabled)
3042 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003043 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003044 return 0;
3045 }
3046 }
3047
3048 return 1;
3049}
3050
Boqun Fengc2469752016-02-16 13:57:40 +08003051static inline unsigned int task_irq_context(struct task_struct *task)
3052{
3053 return 2 * !!task->hardirq_context + !!task->softirq_context;
3054}
3055
Peter Zijlstra8e182572007-07-19 01:48:54 -07003056static int separate_irq_context(struct task_struct *curr,
3057 struct held_lock *hlock)
3058{
3059 unsigned int depth = curr->lockdep_depth;
3060
3061 /*
3062 * Keep track of points where we cross into an interrupt context:
3063 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003064 if (depth) {
3065 struct held_lock *prev_hlock;
3066
3067 prev_hlock = curr->held_locks + depth-1;
3068 /*
3069 * If we cross into another context, reset the
3070 * hash key (this also prevents the checking and the
3071 * adding of the dependency to 'prev'):
3072 */
3073 if (prev_hlock->irq_context != hlock->irq_context)
3074 return 1;
3075 }
3076 return 0;
3077}
3078
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003079#else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003080
3081static inline
3082int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
3083 enum lock_usage_bit new_bit)
3084{
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003085 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003086 return 1;
3087}
3088
3089static inline int mark_irqflags(struct task_struct *curr,
3090 struct held_lock *hlock)
3091{
3092 return 1;
3093}
3094
Boqun Fengc2469752016-02-16 13:57:40 +08003095static inline unsigned int task_irq_context(struct task_struct *task)
3096{
3097 return 0;
3098}
3099
Peter Zijlstra8e182572007-07-19 01:48:54 -07003100static inline int separate_irq_context(struct task_struct *curr,
3101 struct held_lock *hlock)
3102{
3103 return 0;
3104}
3105
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003106#endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003107
3108/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07003109 * Mark a lock with a usage bit, and validate the state transition:
3110 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003111static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02003112 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07003113{
3114 unsigned int new_mask = 1 << new_bit, ret = 1;
3115
3116 /*
3117 * If already set then do not dirty the cacheline,
3118 * nor do any checks:
3119 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003120 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003121 return 1;
3122
3123 if (!graph_lock())
3124 return 0;
3125 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003126 * Make sure we didn't race:
Peter Zijlstra8e182572007-07-19 01:48:54 -07003127 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003128 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07003129 graph_unlock();
3130 return 1;
3131 }
3132
Dave Jonesf82b2172008-08-11 09:30:23 +02003133 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003134
Dave Jonesf82b2172008-08-11 09:30:23 +02003135 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003136 return 0;
3137
3138 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01003139#define LOCKDEP_STATE(__STATE) \
3140 case LOCK_USED_IN_##__STATE: \
3141 case LOCK_USED_IN_##__STATE##_READ: \
3142 case LOCK_ENABLED_##__STATE: \
3143 case LOCK_ENABLED_##__STATE##_READ:
3144#include "lockdep_states.h"
3145#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07003146 ret = mark_lock_irq(curr, this, new_bit);
3147 if (!ret)
3148 return 0;
3149 break;
3150 case LOCK_USED:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003151 debug_atomic_dec(nr_unused_locks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07003152 break;
3153 default:
3154 if (!debug_locks_off_graph_unlock())
3155 return 0;
3156 WARN_ON(1);
3157 return 0;
3158 }
3159
3160 graph_unlock();
3161
3162 /*
3163 * We must printk outside of the graph_lock:
3164 */
3165 if (ret == 2) {
3166 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
3167 print_lock(this);
3168 print_irqtrace_events(curr);
3169 dump_stack();
3170 }
3171
3172 return ret;
3173}
3174
3175/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003176 * Initialize a lock instance's lock-class mapping info:
3177 */
Byungchul Parkb09be672017-08-07 16:12:52 +09003178static void __lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003179 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003180{
Yong Zhangd3d03d42011-11-09 16:04:51 +08003181 int i;
3182
Yong Zhangd3d03d42011-11-09 16:04:51 +08003183 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
3184 lock->class_cache[i] = NULL;
Hitoshi Mitake62016252010-10-05 18:01:51 +09003185
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003186#ifdef CONFIG_LOCK_STAT
3187 lock->cpu = raw_smp_processor_id();
3188#endif
3189
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003190 /*
3191 * Can't be having no nameless bastards around this place!
3192 */
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003193 if (DEBUG_LOCKS_WARN_ON(!name)) {
3194 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003195 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003196 }
3197
3198 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003199
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003200 /*
3201 * No key, no joy, we need to hash something.
3202 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003203 if (DEBUG_LOCKS_WARN_ON(!key))
3204 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003205 /*
3206 * Sanity check, the lock-class key must be persistent:
3207 */
3208 if (!static_obj(key)) {
3209 printk("BUG: key %p not in .data!\n", key);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003210 /*
3211 * What it says above ^^^^^, I suggest you read it.
3212 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003213 DEBUG_LOCKS_WARN_ON(1);
3214 return;
3215 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003216 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003217
3218 if (unlikely(!debug_locks))
3219 return;
3220
Peter Zijlstra35a93932015-02-26 16:23:11 +01003221 if (subclass) {
3222 unsigned long flags;
3223
3224 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3225 return;
3226
3227 raw_local_irq_save(flags);
3228 current->lockdep_recursion = 1;
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003229 register_lock_class(lock, subclass, 1);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003230 current->lockdep_recursion = 0;
3231 raw_local_irq_restore(flags);
3232 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003233}
Byungchul Parkb09be672017-08-07 16:12:52 +09003234
3235void lockdep_init_map(struct lockdep_map *lock, const char *name,
3236 struct lock_class_key *key, int subclass)
3237{
Byungchul Parkb09be672017-08-07 16:12:52 +09003238 __lockdep_init_map(lock, name, key, subclass);
3239}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003240EXPORT_SYMBOL_GPL(lockdep_init_map);
3241
Peter Zijlstra1704f472010-03-19 01:37:42 +01003242struct lock_class_key __lockdep_no_validate__;
Kent Overstreetea6749c2012-12-27 22:21:58 -08003243EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003244
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003245static int
3246print_lock_nested_lock_not_held(struct task_struct *curr,
3247 struct held_lock *hlock,
3248 unsigned long ip)
3249{
3250 if (!debug_locks_off())
3251 return 0;
3252 if (debug_locks_silent)
3253 return 0;
3254
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003255 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003256 pr_warn("==================================\n");
3257 pr_warn("WARNING: Nested lock was not taken\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003258 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003259 pr_warn("----------------------------------\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003260
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003261 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003262 print_lock(hlock);
3263
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003264 pr_warn("\nbut this task is not holding:\n");
3265 pr_warn("%s\n", hlock->nest_lock->name);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003266
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003267 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003268 dump_stack();
3269
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003270 pr_warn("\nother info that might help us debug this:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003271 lockdep_print_held_locks(curr);
3272
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003273 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003274 dump_stack();
3275
3276 return 0;
3277}
3278
Peter Zijlstraf8319482016-11-30 14:32:25 +11003279static int __lock_is_held(struct lockdep_map *lock, int read);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003280
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003281/*
3282 * This gets called for every mutex_lock*()/spin_lock*() operation.
3283 * We maintain the dependency maps and validate the locking attempt:
3284 */
3285static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3286 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003287 struct lockdep_map *nest_lock, unsigned long ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003288 int references, int pin_count)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003289{
3290 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07003291 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003292 struct held_lock *hlock;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003293 unsigned int depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003294 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003295 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003296 u64 chain_key;
3297
3298 if (unlikely(!debug_locks))
3299 return 0;
3300
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003301 /*
3302 * Lockdep should run with IRQs disabled, otherwise we could
3303 * get an interrupt which would want to take locks, which would
3304 * end up in lockdep and have you got a head-ache already?
3305 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003306 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3307 return 0;
3308
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003309 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3310 check = 0;
Peter Zijlstra1704f472010-03-19 01:37:42 +01003311
Hitoshi Mitake62016252010-10-05 18:01:51 +09003312 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3313 class = lock->class_cache[subclass];
Ingo Molnard6d897c2006-07-10 04:44:04 -07003314 /*
Hitoshi Mitake62016252010-10-05 18:01:51 +09003315 * Not cached?
Ingo Molnard6d897c2006-07-10 04:44:04 -07003316 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003317 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003318 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003319 if (!class)
3320 return 0;
3321 }
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003322 atomic_inc((atomic_t *)&class->ops);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003323 if (very_verbose(class)) {
3324 printk("\nacquire class [%p] %s", class->key, class->name);
3325 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01003326 printk(KERN_CONT "#%d", class->name_version);
3327 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003328 dump_stack();
3329 }
3330
3331 /*
3332 * Add the lock to the list of currently held locks.
3333 * (we dont increase the depth just yet, up until the
3334 * dependency checks are done)
3335 */
3336 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003337 /*
3338 * Ran out of static storage for our per-task lock stack again have we?
3339 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003340 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3341 return 0;
3342
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003343 class_idx = class - lock_classes + 1;
3344
Ingo Molnare966eae2017-12-12 12:31:16 +01003345 if (depth) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003346 hlock = curr->held_locks + depth - 1;
3347 if (hlock->class_idx == class_idx && nest_lock) {
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003348 if (hlock->references) {
3349 /*
3350 * Check: unsigned int references:12, overflow.
3351 */
3352 if (DEBUG_LOCKS_WARN_ON(hlock->references == (1 << 12)-1))
3353 return 0;
3354
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003355 hlock->references++;
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003356 } else {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003357 hlock->references = 2;
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003358 }
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003359
3360 return 1;
3361 }
3362 }
3363
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003364 hlock = curr->held_locks + depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003365 /*
3366 * Plain impossible, we just registered it and checked it weren't no
3367 * NULL like.. I bet this mushroom I ate was good!
3368 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003369 if (DEBUG_LOCKS_WARN_ON(!class))
3370 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003371 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003372 hlock->acquire_ip = ip;
3373 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003374 hlock->nest_lock = nest_lock;
Boqun Fengc2469752016-02-16 13:57:40 +08003375 hlock->irq_context = task_irq_context(curr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003376 hlock->trylock = trylock;
3377 hlock->read = read;
3378 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04003379 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003380 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003381#ifdef CONFIG_LOCK_STAT
3382 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003383 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003384#endif
Peter Zijlstra21199f22015-09-16 16:10:40 +02003385 hlock->pin_count = pin_count;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003386
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003387 if (check && !mark_irqflags(curr, hlock))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003388 return 0;
3389
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003390 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07003391 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003392 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003393
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003394 /*
Gautham R Shenoy17aacfb92007-10-28 20:47:01 +01003395 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003396 * lock keys along the dependency chain. We save the hash value
3397 * at every step so that we can get the current hash easily
3398 * after unlock. The chain hash is then used to cache dependency
3399 * results.
3400 *
3401 * The 'key ID' is what is the most compact key value to drive
3402 * the hash, not class->key.
3403 */
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003404 /*
3405 * Whoops, we did it again.. ran straight out of our static allocation.
3406 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003407 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003408 return 0;
3409
3410 chain_key = curr->curr_chain_key;
3411 if (!depth) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003412 /*
3413 * How can we have a chain hash when we ain't got no keys?!
3414 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003415 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3416 return 0;
3417 chain_head = 1;
3418 }
3419
3420 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003421 if (separate_irq_context(curr, hlock)) {
3422 chain_key = 0;
3423 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003424 }
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003425 chain_key = iterate_chain_key(chain_key, class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003426
Peter Zijlstraf8319482016-11-30 14:32:25 +11003427 if (nest_lock && !__lock_is_held(nest_lock, -1))
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003428 return print_lock_nested_lock_not_held(curr, hlock, ip);
3429
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003430 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003431 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08003432
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003433 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003434 curr->lockdep_depth++;
3435 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08003436#ifdef CONFIG_DEBUG_LOCKDEP
3437 if (unlikely(!debug_locks))
3438 return 0;
3439#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003440 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3441 debug_locks_off();
Dave Jones2c522832013-04-25 13:40:02 -04003442 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3443 printk(KERN_DEBUG "depth: %i max: %lu!\n",
Ben Greearc0540602013-02-06 10:56:19 -08003444 curr->lockdep_depth, MAX_LOCK_DEPTH);
Ben Greearc0540602013-02-06 10:56:19 -08003445
3446 lockdep_print_held_locks(current);
3447 debug_show_all_locks();
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01003448 dump_stack();
Ben Greearc0540602013-02-06 10:56:19 -08003449
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003450 return 0;
3451 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08003452
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003453 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3454 max_lockdep_depth = curr->lockdep_depth;
3455
3456 return 1;
3457}
3458
3459static int
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303460print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003461 unsigned long ip)
3462{
3463 if (!debug_locks_off())
3464 return 0;
3465 if (debug_locks_silent)
3466 return 0;
3467
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003468 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003469 pr_warn("=====================================\n");
3470 pr_warn("WARNING: bad unlock balance detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003471 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003472 pr_warn("-------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003473 pr_warn("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003474 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003475 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003476 pr_cont(") at:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003477 print_ip_sym(ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003478 pr_warn("but there are no more locks to release!\n");
3479 pr_warn("\nother info that might help us debug this:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003480 lockdep_print_held_locks(curr);
3481
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003482 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003483 dump_stack();
3484
3485 return 0;
3486}
3487
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003488static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3489{
3490 if (hlock->instance == lock)
3491 return 1;
3492
3493 if (hlock->references) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09003494 struct lock_class *class = lock->class_cache[0];
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003495
3496 if (!class)
3497 class = look_up_lock_class(lock, 0);
3498
Peter Zijlstra80e04012011-08-05 14:26:17 +02003499 /*
3500 * If look_up_lock_class() failed to find a class, we're trying
3501 * to test if we hold a lock that has never yet been acquired.
3502 * Clearly if the lock hasn't been acquired _ever_, we're not
3503 * holding it either, so report failure.
3504 */
Matthew Wilcox64f29d12018-01-17 07:14:12 -08003505 if (!class)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003506 return 0;
3507
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003508 /*
3509 * References, but not a lock we're actually ref-counting?
3510 * State got messed up, follow the sites that change ->references
3511 * and try to make sense of it.
3512 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003513 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3514 return 0;
3515
3516 if (hlock->class_idx == class - lock_classes + 1)
3517 return 1;
3518 }
3519
3520 return 0;
3521}
3522
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003523/* @depth must not be zero */
3524static struct held_lock *find_held_lock(struct task_struct *curr,
3525 struct lockdep_map *lock,
3526 unsigned int depth, int *idx)
3527{
3528 struct held_lock *ret, *hlock, *prev_hlock;
3529 int i;
3530
3531 i = depth - 1;
3532 hlock = curr->held_locks + i;
3533 ret = hlock;
3534 if (match_held_lock(hlock, lock))
3535 goto out;
3536
3537 ret = NULL;
3538 for (i--, prev_hlock = hlock--;
3539 i >= 0;
3540 i--, prev_hlock = hlock--) {
3541 /*
3542 * We must not cross into another context:
3543 */
3544 if (prev_hlock->irq_context != hlock->irq_context) {
3545 ret = NULL;
3546 break;
3547 }
3548 if (match_held_lock(hlock, lock)) {
3549 ret = hlock;
3550 break;
3551 }
3552 }
3553
3554out:
3555 *idx = i;
3556 return ret;
3557}
3558
J. R. Okajimae9699702017-02-03 01:38:16 +09003559static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
3560 int idx)
3561{
3562 struct held_lock *hlock;
3563
3564 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
3565 if (!__lock_acquire(hlock->instance,
3566 hlock_class(hlock)->subclass,
3567 hlock->trylock,
3568 hlock->read, hlock->check,
3569 hlock->hardirqs_off,
3570 hlock->nest_lock, hlock->acquire_ip,
3571 hlock->references, hlock->pin_count))
3572 return 1;
3573 }
3574 return 0;
3575}
3576
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003577static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003578__lock_set_class(struct lockdep_map *lock, const char *name,
3579 struct lock_class_key *key, unsigned int subclass,
3580 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003581{
3582 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003583 struct held_lock *hlock;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003584 struct lock_class *class;
3585 unsigned int depth;
3586 int i;
3587
3588 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003589 /*
3590 * This function is about (re)setting the class of a held lock,
3591 * yet we're not actually holding any locks. Naughty user!
3592 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003593 if (DEBUG_LOCKS_WARN_ON(!depth))
3594 return 0;
3595
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003596 hlock = find_held_lock(curr, lock, depth, &i);
3597 if (!hlock)
3598 return print_unlock_imbalance_bug(curr, lock, ip);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003599
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003600 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003601 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02003602 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003603
3604 curr->lockdep_depth = i;
3605 curr->curr_chain_key = hlock->prev_chain_key;
3606
J. R. Okajimae9699702017-02-03 01:38:16 +09003607 if (reacquire_held_locks(curr, depth, i))
3608 return 0;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003609
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003610 /*
3611 * I took it apart and put it back together again, except now I have
3612 * these 'spare' parts.. where shall I put them.
3613 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003614 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3615 return 0;
3616 return 1;
3617}
3618
J. R. Okajima6419c4a2017-02-03 01:38:17 +09003619static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3620{
3621 struct task_struct *curr = current;
3622 struct held_lock *hlock;
3623 unsigned int depth;
3624 int i;
3625
3626 depth = curr->lockdep_depth;
3627 /*
3628 * This function is about (re)setting the class of a held lock,
3629 * yet we're not actually holding any locks. Naughty user!
3630 */
3631 if (DEBUG_LOCKS_WARN_ON(!depth))
3632 return 0;
3633
3634 hlock = find_held_lock(curr, lock, depth, &i);
3635 if (!hlock)
3636 return print_unlock_imbalance_bug(curr, lock, ip);
3637
3638 curr->lockdep_depth = i;
3639 curr->curr_chain_key = hlock->prev_chain_key;
3640
3641 WARN(hlock->read, "downgrading a read lock");
3642 hlock->read = 1;
3643 hlock->acquire_ip = ip;
3644
3645 if (reacquire_held_locks(curr, depth, i))
3646 return 0;
3647
3648 /*
3649 * I took it apart and put it back together again, except now I have
3650 * these 'spare' parts.. where shall I put them.
3651 */
3652 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3653 return 0;
3654 return 1;
3655}
3656
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003657/*
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003658 * Remove the lock to the list of currently held locks - this gets
3659 * called on mutex_unlock()/spin_unlock*() (or on a failed
3660 * mutex_lock_interruptible()).
3661 *
3662 * @nested is an hysterical artifact, needs a tree wide cleanup.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003663 */
3664static int
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003665__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003666{
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003667 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003668 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003669 unsigned int depth;
Ingo Molnare966eae2017-12-12 12:31:16 +01003670 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003671
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003672 if (unlikely(!debug_locks))
3673 return 0;
3674
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003675 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003676 /*
3677 * So we're all set to release this lock.. wait what lock? We don't
3678 * own any locks, you've been drinking again?
3679 */
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003680 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3681 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003682
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003683 /*
3684 * Check whether the lock exists in the current stack
3685 * of held locks:
3686 */
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003687 hlock = find_held_lock(curr, lock, depth, &i);
3688 if (!hlock)
3689 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003690
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003691 if (hlock->instance == lock)
3692 lock_release_holdtime(hlock);
3693
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003694 WARN(hlock->pin_count, "releasing a pinned lock\n");
3695
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003696 if (hlock->references) {
3697 hlock->references--;
3698 if (hlock->references) {
3699 /*
3700 * We had, and after removing one, still have
3701 * references, the current lock stack is still
3702 * valid. We're done!
3703 */
3704 return 1;
3705 }
3706 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003707
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003708 /*
3709 * We have the right lock to unlock, 'hlock' points to it.
3710 * Now we remove it from the stack, and add back the other
3711 * entries (if any), recalculating the hash along the way:
3712 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003713
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003714 curr->lockdep_depth = i;
3715 curr->curr_chain_key = hlock->prev_chain_key;
3716
J. R. Okajimae9699702017-02-03 01:38:16 +09003717 if (reacquire_held_locks(curr, depth, i + 1))
3718 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003719
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003720 /*
3721 * We had N bottles of beer on the wall, we drank one, but now
3722 * there's not N-1 bottles of beer left on the wall...
3723 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003724 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3725 return 0;
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003726
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003727 return 1;
3728}
3729
Peter Zijlstraf8319482016-11-30 14:32:25 +11003730static int __lock_is_held(struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02003731{
3732 struct task_struct *curr = current;
3733 int i;
3734
3735 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003736 struct held_lock *hlock = curr->held_locks + i;
3737
Peter Zijlstraf8319482016-11-30 14:32:25 +11003738 if (match_held_lock(hlock, lock)) {
3739 if (read == -1 || hlock->read == read)
3740 return 1;
3741
3742 return 0;
3743 }
Peter Zijlstraf607c662009-07-20 19:16:29 +02003744 }
3745
3746 return 0;
3747}
3748
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003749static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
3750{
3751 struct pin_cookie cookie = NIL_COOKIE;
3752 struct task_struct *curr = current;
3753 int i;
3754
3755 if (unlikely(!debug_locks))
3756 return cookie;
3757
3758 for (i = 0; i < curr->lockdep_depth; i++) {
3759 struct held_lock *hlock = curr->held_locks + i;
3760
3761 if (match_held_lock(hlock, lock)) {
3762 /*
3763 * Grab 16bits of randomness; this is sufficient to not
3764 * be guessable and still allows some pin nesting in
3765 * our u32 pin_count.
3766 */
3767 cookie.val = 1 + (prandom_u32() >> 16);
3768 hlock->pin_count += cookie.val;
3769 return cookie;
3770 }
3771 }
3772
3773 WARN(1, "pinning an unheld lock\n");
3774 return cookie;
3775}
3776
3777static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003778{
3779 struct task_struct *curr = current;
3780 int i;
3781
3782 if (unlikely(!debug_locks))
3783 return;
3784
3785 for (i = 0; i < curr->lockdep_depth; i++) {
3786 struct held_lock *hlock = curr->held_locks + i;
3787
3788 if (match_held_lock(hlock, lock)) {
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003789 hlock->pin_count += cookie.val;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003790 return;
3791 }
3792 }
3793
3794 WARN(1, "pinning an unheld lock\n");
3795}
3796
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003797static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003798{
3799 struct task_struct *curr = current;
3800 int i;
3801
3802 if (unlikely(!debug_locks))
3803 return;
3804
3805 for (i = 0; i < curr->lockdep_depth; i++) {
3806 struct held_lock *hlock = curr->held_locks + i;
3807
3808 if (match_held_lock(hlock, lock)) {
3809 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
3810 return;
3811
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003812 hlock->pin_count -= cookie.val;
3813
3814 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
3815 hlock->pin_count = 0;
3816
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003817 return;
3818 }
3819 }
3820
3821 WARN(1, "unpinning an unheld lock\n");
3822}
3823
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003824/*
3825 * Check whether we follow the irq-flags state precisely:
3826 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003827static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003828{
Ingo Molnar992860e2008-07-14 10:28:38 +02003829#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3830 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003831 if (!debug_locks)
3832 return;
3833
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01003834 if (irqs_disabled_flags(flags)) {
3835 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3836 printk("possible reason: unannotated irqs-off.\n");
3837 }
3838 } else {
3839 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3840 printk("possible reason: unannotated irqs-on.\n");
3841 }
3842 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003843
3844 /*
3845 * We dont accurately track softirq state in e.g.
3846 * hardirq contexts (such as on 4KSTACKS), so only
3847 * check if not in hardirq contexts:
3848 */
3849 if (!hardirq_count()) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003850 if (softirq_count()) {
3851 /* like the above, but with softirqs */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003852 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003853 } else {
3854 /* lick the above, does it taste good? */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003855 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003856 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003857 }
3858
3859 if (!debug_locks)
3860 print_irqtrace_events(current);
3861#endif
3862}
3863
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003864void lock_set_class(struct lockdep_map *lock, const char *name,
3865 struct lock_class_key *key, unsigned int subclass,
3866 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003867{
3868 unsigned long flags;
3869
3870 if (unlikely(current->lockdep_recursion))
3871 return;
3872
3873 raw_local_irq_save(flags);
3874 current->lockdep_recursion = 1;
3875 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003876 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003877 check_chain_key(current);
3878 current->lockdep_recursion = 0;
3879 raw_local_irq_restore(flags);
3880}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003881EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003882
J. R. Okajima6419c4a2017-02-03 01:38:17 +09003883void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3884{
3885 unsigned long flags;
3886
3887 if (unlikely(current->lockdep_recursion))
3888 return;
3889
3890 raw_local_irq_save(flags);
3891 current->lockdep_recursion = 1;
3892 check_flags(flags);
3893 if (__lock_downgrade(lock, ip))
3894 check_chain_key(current);
3895 current->lockdep_recursion = 0;
3896 raw_local_irq_restore(flags);
3897}
3898EXPORT_SYMBOL_GPL(lock_downgrade);
3899
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003900/*
3901 * We are not always called with irqs disabled - do that here,
3902 * and also avoid lockdep recursion:
3903 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003904void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003905 int trylock, int read, int check,
3906 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003907{
3908 unsigned long flags;
3909
3910 if (unlikely(current->lockdep_recursion))
3911 return;
3912
3913 raw_local_irq_save(flags);
3914 check_flags(flags);
3915
3916 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003917 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003918 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003919 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003920 current->lockdep_recursion = 0;
3921 raw_local_irq_restore(flags);
3922}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003923EXPORT_SYMBOL_GPL(lock_acquire);
3924
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003925void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003926 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003927{
3928 unsigned long flags;
3929
3930 if (unlikely(current->lockdep_recursion))
3931 return;
3932
3933 raw_local_irq_save(flags);
3934 check_flags(flags);
3935 current->lockdep_recursion = 1;
Frederic Weisbecker93135432010-05-08 06:24:25 +02003936 trace_lock_release(lock, ip);
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003937 if (__lock_release(lock, nested, ip))
3938 check_chain_key(current);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003939 current->lockdep_recursion = 0;
3940 raw_local_irq_restore(flags);
3941}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003942EXPORT_SYMBOL_GPL(lock_release);
3943
Peter Zijlstraf8319482016-11-30 14:32:25 +11003944int lock_is_held_type(struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02003945{
3946 unsigned long flags;
3947 int ret = 0;
3948
3949 if (unlikely(current->lockdep_recursion))
Peter Zijlstraf2513cd2011-06-06 12:32:43 +02003950 return 1; /* avoid false negative lockdep_assert_held() */
Peter Zijlstraf607c662009-07-20 19:16:29 +02003951
3952 raw_local_irq_save(flags);
3953 check_flags(flags);
3954
3955 current->lockdep_recursion = 1;
Peter Zijlstraf8319482016-11-30 14:32:25 +11003956 ret = __lock_is_held(lock, read);
Peter Zijlstraf607c662009-07-20 19:16:29 +02003957 current->lockdep_recursion = 0;
3958 raw_local_irq_restore(flags);
3959
3960 return ret;
3961}
Peter Zijlstraf8319482016-11-30 14:32:25 +11003962EXPORT_SYMBOL_GPL(lock_is_held_type);
Peter Zijlstraf607c662009-07-20 19:16:29 +02003963
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003964struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003965{
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003966 struct pin_cookie cookie = NIL_COOKIE;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003967 unsigned long flags;
3968
3969 if (unlikely(current->lockdep_recursion))
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003970 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003971
3972 raw_local_irq_save(flags);
3973 check_flags(flags);
3974
3975 current->lockdep_recursion = 1;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003976 cookie = __lock_pin_lock(lock);
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003977 current->lockdep_recursion = 0;
3978 raw_local_irq_restore(flags);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003979
3980 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003981}
3982EXPORT_SYMBOL_GPL(lock_pin_lock);
3983
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003984void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003985{
3986 unsigned long flags;
3987
3988 if (unlikely(current->lockdep_recursion))
3989 return;
3990
3991 raw_local_irq_save(flags);
3992 check_flags(flags);
3993
3994 current->lockdep_recursion = 1;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003995 __lock_repin_lock(lock, cookie);
3996 current->lockdep_recursion = 0;
3997 raw_local_irq_restore(flags);
3998}
3999EXPORT_SYMBOL_GPL(lock_repin_lock);
4000
4001void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
4002{
4003 unsigned long flags;
4004
4005 if (unlikely(current->lockdep_recursion))
4006 return;
4007
4008 raw_local_irq_save(flags);
4009 check_flags(flags);
4010
4011 current->lockdep_recursion = 1;
4012 __lock_unpin_lock(lock, cookie);
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004013 current->lockdep_recursion = 0;
4014 raw_local_irq_restore(flags);
4015}
4016EXPORT_SYMBOL_GPL(lock_unpin_lock);
4017
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004018#ifdef CONFIG_LOCK_STAT
4019static int
4020print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
4021 unsigned long ip)
4022{
4023 if (!debug_locks_off())
4024 return 0;
4025 if (debug_locks_silent)
4026 return 0;
4027
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004028 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004029 pr_warn("=================================\n");
4030 pr_warn("WARNING: bad contention detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004031 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004032 pr_warn("---------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004033 pr_warn("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004034 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004035 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004036 pr_cont(") at:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004037 print_ip_sym(ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004038 pr_warn("but there are no locks held!\n");
4039 pr_warn("\nother info that might help us debug this:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004040 lockdep_print_held_locks(curr);
4041
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004042 pr_warn("\nstack backtrace:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004043 dump_stack();
4044
4045 return 0;
4046}
4047
4048static void
4049__lock_contended(struct lockdep_map *lock, unsigned long ip)
4050{
4051 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004052 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004053 struct lock_class_stats *stats;
4054 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004055 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004056
4057 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004058 /*
4059 * Whee, we contended on this lock, except it seems we're not
4060 * actually trying to acquire anything much at all..
4061 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004062 if (DEBUG_LOCKS_WARN_ON(!depth))
4063 return;
4064
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004065 hlock = find_held_lock(curr, lock, depth, &i);
4066 if (!hlock) {
4067 print_lock_contention_bug(curr, lock, ip);
4068 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004069 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004070
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004071 if (hlock->instance != lock)
4072 return;
4073
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004074 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004075
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004076 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
4077 contending_point = lock_point(hlock_class(hlock)->contending_point,
4078 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004079
Dave Jonesf82b2172008-08-11 09:30:23 +02004080 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004081 if (contention_point < LOCKSTAT_POINTS)
4082 stats->contention_point[contention_point]++;
4083 if (contending_point < LOCKSTAT_POINTS)
4084 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07004085 if (lock->cpu != smp_processor_id())
4086 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004087 put_lock_stats(stats);
4088}
4089
4090static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004091__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004092{
4093 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004094 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004095 struct lock_class_stats *stats;
4096 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004097 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07004098 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004099
4100 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004101 /*
4102 * Yay, we acquired ownership of this lock we didn't try to
4103 * acquire, how the heck did that happen?
4104 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004105 if (DEBUG_LOCKS_WARN_ON(!depth))
4106 return;
4107
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004108 hlock = find_held_lock(curr, lock, depth, &i);
4109 if (!hlock) {
4110 print_lock_contention_bug(curr, lock, _RET_IP_);
4111 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004112 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004113
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004114 if (hlock->instance != lock)
4115 return;
4116
Peter Zijlstra96645672007-07-19 01:49:00 -07004117 cpu = smp_processor_id();
4118 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004119 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07004120 waittime = now - hlock->waittime_stamp;
4121 hlock->holdtime_stamp = now;
4122 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004123
Frederic Weisbecker883a2a32010-05-08 06:16:11 +02004124 trace_lock_acquired(lock, ip);
Frederic Weisbecker20625012009-04-06 01:49:33 +02004125
Dave Jonesf82b2172008-08-11 09:30:23 +02004126 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07004127 if (waittime) {
4128 if (hlock->read)
4129 lock_time_inc(&stats->read_waittime, waittime);
4130 else
4131 lock_time_inc(&stats->write_waittime, waittime);
4132 }
4133 if (lock->cpu != cpu)
4134 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004135 put_lock_stats(stats);
Peter Zijlstra96645672007-07-19 01:49:00 -07004136
4137 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004138 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004139}
4140
4141void lock_contended(struct lockdep_map *lock, unsigned long ip)
4142{
4143 unsigned long flags;
4144
4145 if (unlikely(!lock_stat))
4146 return;
4147
4148 if (unlikely(current->lockdep_recursion))
4149 return;
4150
4151 raw_local_irq_save(flags);
4152 check_flags(flags);
4153 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01004154 trace_lock_contended(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004155 __lock_contended(lock, ip);
4156 current->lockdep_recursion = 0;
4157 raw_local_irq_restore(flags);
4158}
4159EXPORT_SYMBOL_GPL(lock_contended);
4160
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004161void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004162{
4163 unsigned long flags;
4164
4165 if (unlikely(!lock_stat))
4166 return;
4167
4168 if (unlikely(current->lockdep_recursion))
4169 return;
4170
4171 raw_local_irq_save(flags);
4172 check_flags(flags);
4173 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004174 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004175 current->lockdep_recursion = 0;
4176 raw_local_irq_restore(flags);
4177}
4178EXPORT_SYMBOL_GPL(lock_acquired);
4179#endif
4180
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004181/*
4182 * Used by the testsuite, sanitize the validator state
4183 * after a simulated failure:
4184 */
4185
4186void lockdep_reset(void)
4187{
4188 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08004189 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004190
4191 raw_local_irq_save(flags);
4192 current->curr_chain_key = 0;
4193 current->lockdep_depth = 0;
4194 current->lockdep_recursion = 0;
4195 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
4196 nr_hardirq_chains = 0;
4197 nr_softirq_chains = 0;
4198 nr_process_chains = 0;
4199 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08004200 for (i = 0; i < CHAINHASH_SIZE; i++)
Andrew Mortona63f38c2016-02-03 13:44:12 -08004201 INIT_HLIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004202 raw_local_irq_restore(flags);
4203}
4204
4205static void zap_class(struct lock_class *class)
4206{
4207 int i;
4208
4209 /*
4210 * Remove all dependencies this lock is
4211 * involved in:
4212 */
4213 for (i = 0; i < nr_list_entries; i++) {
4214 if (list_entries[i].class == class)
4215 list_del_rcu(&list_entries[i].entry);
4216 }
4217 /*
4218 * Unhash the class and remove it from the all_lock_classes list:
4219 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08004220 hlist_del_rcu(&class->hash_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004221 list_del_rcu(&class->lock_entry);
4222
Peter Zijlstracee34d82015-06-02 12:50:13 +02004223 RCU_INIT_POINTER(class->key, NULL);
4224 RCU_INIT_POINTER(class->name, NULL);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004225}
4226
Arjan van de Venfabe8742008-01-24 07:00:45 +01004227static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004228{
4229 return addr >= start && addr < start + size;
4230}
4231
Peter Zijlstra35a93932015-02-26 16:23:11 +01004232/*
4233 * Used in module.c to remove lock classes from memory that is going to be
4234 * freed; and possibly re-used by other modules.
4235 *
4236 * We will have had one sync_sched() before getting here, so we're guaranteed
4237 * nobody will look up these exact classes -- they're properly dead but still
4238 * allocated.
4239 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004240void lockdep_free_key_range(void *start, unsigned long size)
4241{
Peter Zijlstra35a93932015-02-26 16:23:11 +01004242 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004243 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004244 unsigned long flags;
4245 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01004246 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004247
4248 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01004249 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004250
4251 /*
4252 * Unhash all classes that were created by this module:
4253 */
4254 for (i = 0; i < CLASSHASH_SIZE; i++) {
4255 head = classhash_table + i;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004256 hlist_for_each_entry_rcu(class, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004257 if (within(class->key, start, size))
4258 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01004259 else if (within(class->name, start, size))
4260 zap_class(class);
4261 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004262 }
4263
Nick Piggin5a26db52008-01-16 09:51:58 +01004264 if (locked)
4265 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004266 raw_local_irq_restore(flags);
Peter Zijlstra35a93932015-02-26 16:23:11 +01004267
4268 /*
4269 * Wait for any possible iterators from look_up_lock_class() to pass
4270 * before continuing to free the memory they refer to.
4271 *
4272 * sync_sched() is sufficient because the read-side is IRQ disable.
4273 */
4274 synchronize_sched();
4275
4276 /*
4277 * XXX at this point we could return the resources to the pool;
4278 * instead we leak them. We would need to change to bitmap allocators
4279 * instead of the linear allocators we have now.
4280 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004281}
4282
4283void lockdep_reset_lock(struct lockdep_map *lock)
4284{
Peter Zijlstra35a93932015-02-26 16:23:11 +01004285 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004286 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004287 unsigned long flags;
4288 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01004289 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004290
4291 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004292
4293 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07004294 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004295 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07004296 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
4297 /*
4298 * If the class exists we look it up and zap it:
4299 */
4300 class = look_up_lock_class(lock, j);
Matthew Wilcox64f29d12018-01-17 07:14:12 -08004301 if (class)
Ingo Molnard6d897c2006-07-10 04:44:04 -07004302 zap_class(class);
4303 }
4304 /*
4305 * Debug check: in the end all mapped classes should
4306 * be gone.
4307 */
Nick Piggin5a26db52008-01-16 09:51:58 +01004308 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004309 for (i = 0; i < CLASSHASH_SIZE; i++) {
4310 head = classhash_table + i;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004311 hlist_for_each_entry_rcu(class, head, hash_entry) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09004312 int match = 0;
4313
4314 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
4315 match |= class == lock->class_cache[j];
4316
4317 if (unlikely(match)) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004318 if (debug_locks_off_graph_unlock()) {
4319 /*
4320 * We all just reset everything, how did it match?
4321 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08004322 WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004323 }
Ingo Molnard6d897c2006-07-10 04:44:04 -07004324 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004325 }
4326 }
4327 }
Nick Piggin5a26db52008-01-16 09:51:58 +01004328 if (locked)
4329 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07004330
4331out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004332 raw_local_irq_restore(flags);
4333}
4334
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004335void __init lockdep_info(void)
4336{
4337 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4338
Li Zefanb0788ca2008-11-21 15:57:32 +08004339 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004340 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4341 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08004342 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004343 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4344 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4345 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4346
4347 printk(" memory used by lock dependency info: %lu kB\n",
4348 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
4349 sizeof(struct list_head) * CLASSHASH_SIZE +
4350 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
4351 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei906292092009-08-02 21:43:36 +08004352 sizeof(struct list_head) * CHAINHASH_SIZE
Ming Lei4dd861d2009-07-16 15:44:29 +02004353#ifdef CONFIG_PROVE_LOCKING
Ming Leie351b662009-07-22 22:48:09 +08004354 + sizeof(struct circular_queue)
Ming Lei4dd861d2009-07-16 15:44:29 +02004355#endif
Ming Lei906292092009-08-02 21:43:36 +08004356 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02004357 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004358
4359 printk(" per task-struct memory footprint: %lu bytes\n",
4360 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004361}
4362
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004363static void
4364print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07004365 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004366{
4367 if (!debug_locks_off())
4368 return;
4369 if (debug_locks_silent)
4370 return;
4371
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004372 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004373 pr_warn("=========================\n");
4374 pr_warn("WARNING: held lock freed!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004375 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004376 pr_warn("-------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004377 pr_warn("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004378 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07004379 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004380 lockdep_print_held_locks(curr);
4381
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004382 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004383 dump_stack();
4384}
4385
Oleg Nesterov54561782007-12-05 15:46:09 +01004386static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4387 const void* lock_from, unsigned long lock_len)
4388{
4389 return lock_from + lock_len <= mem_from ||
4390 mem_from + mem_len <= lock_from;
4391}
4392
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004393/*
4394 * Called when kernel memory is freed (or unmapped), or if a lock
4395 * is destroyed or reinitialized - this code checks whether there is
4396 * any held lock in the memory range of <from> to <to>:
4397 */
4398void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4399{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004400 struct task_struct *curr = current;
4401 struct held_lock *hlock;
4402 unsigned long flags;
4403 int i;
4404
4405 if (unlikely(!debug_locks))
4406 return;
4407
4408 local_irq_save(flags);
4409 for (i = 0; i < curr->lockdep_depth; i++) {
4410 hlock = curr->held_locks + i;
4411
Oleg Nesterov54561782007-12-05 15:46:09 +01004412 if (not_in_range(mem_from, mem_len, hlock->instance,
4413 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004414 continue;
4415
Oleg Nesterov54561782007-12-05 15:46:09 +01004416 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004417 break;
4418 }
4419 local_irq_restore(flags);
4420}
Peter Zijlstraed075362006-12-06 20:35:24 -08004421EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004422
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004423static void print_held_locks_bug(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004424{
4425 if (!debug_locks_off())
4426 return;
4427 if (debug_locks_silent)
4428 return;
4429
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004430 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004431 pr_warn("====================================\n");
4432 pr_warn("WARNING: %s/%d still has locks held!\n",
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004433 current->comm, task_pid_nr(current));
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004434 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004435 pr_warn("------------------------------------\n");
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004436 lockdep_print_held_locks(current);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004437 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004438 dump_stack();
4439}
4440
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004441void debug_check_no_locks_held(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004442{
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004443 if (unlikely(current->lockdep_depth > 0))
4444 print_held_locks_bug();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004445}
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004446EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004447
Sasha Levin8dce7a92013-06-13 18:41:16 -04004448#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004449void debug_show_all_locks(void)
4450{
4451 struct task_struct *g, *p;
4452 int count = 10;
4453 int unlock = 1;
4454
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004455 if (unlikely(!debug_locks)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004456 pr_warn("INFO: lockdep is turned off.\n");
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004457 return;
4458 }
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004459 pr_warn("\nShowing all locks held in the system:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004460
4461 /*
4462 * Here we try to get the tasklist_lock as hard as possible,
4463 * if not successful after 2 seconds we ignore it (but keep
4464 * trying). This is to enable a debug printout even if a
4465 * tasklist_lock-holding task deadlocks or crashes.
4466 */
4467retry:
4468 if (!read_trylock(&tasklist_lock)) {
4469 if (count == 10)
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004470 pr_warn("hm, tasklist_lock locked, retrying... ");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004471 if (count) {
4472 count--;
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004473 pr_cont(" #%d", 10-count);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004474 mdelay(200);
4475 goto retry;
4476 }
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004477 pr_cont(" ignoring it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004478 unlock = 0;
qinghuang feng46fec7a2008-10-28 17:24:28 +08004479 } else {
4480 if (count != 10)
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004481 pr_cont(" locked it.\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004482 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004483
4484 do_each_thread(g, p) {
Ingo Molnar85684872007-12-05 15:46:09 +01004485 /*
4486 * It's not reliable to print a task's held locks
4487 * if it's not sleeping (or if it's not the current
4488 * task):
4489 */
4490 if (p->state == TASK_RUNNING && p != current)
4491 continue;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004492 if (p->lockdep_depth)
4493 lockdep_print_held_locks(p);
4494 if (!unlock)
4495 if (read_trylock(&tasklist_lock))
4496 unlock = 1;
4497 } while_each_thread(g, p);
4498
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004499 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004500 pr_warn("=============================================\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004501
4502 if (unlock)
4503 read_unlock(&tasklist_lock);
4504}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004505EXPORT_SYMBOL_GPL(debug_show_all_locks);
Sasha Levin8dce7a92013-06-13 18:41:16 -04004506#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004507
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004508/*
4509 * Careful: only use this function if you are sure that
4510 * the task cannot run in parallel!
4511 */
John Kacurf1b499f2010-08-05 17:10:53 +02004512void debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004513{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004514 if (unlikely(!debug_locks)) {
4515 printk("INFO: lockdep is turned off.\n");
4516 return;
4517 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004518 lockdep_print_held_locks(task);
4519}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004520EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02004521
Andi Kleen722a9f92014-05-02 00:44:38 +02004522asmlinkage __visible void lockdep_sys_exit(void)
Peter Zijlstrab351d162007-10-11 22:11:12 +02004523{
4524 struct task_struct *curr = current;
4525
4526 if (unlikely(curr->lockdep_depth)) {
4527 if (!debug_locks_off())
4528 return;
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004529 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004530 pr_warn("================================================\n");
4531 pr_warn("WARNING: lock held when returning to user space!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004532 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004533 pr_warn("------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004534 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
Peter Zijlstrab351d162007-10-11 22:11:12 +02004535 curr->comm, curr->pid);
4536 lockdep_print_held_locks(curr);
4537 }
Byungchul Parkb09be672017-08-07 16:12:52 +09004538
4539 /*
4540 * The lock history for each syscall should be independent. So wipe the
4541 * slate clean on return to userspace.
4542 */
Peter Zijlstraf52be572017-08-29 10:59:39 +02004543 lockdep_invariant_state(false);
Peter Zijlstrab351d162007-10-11 22:11:12 +02004544}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004545
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004546void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004547{
4548 struct task_struct *curr = current;
4549
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004550 /* Note: the following can be executed concurrently, so be careful. */
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004551 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004552 pr_warn("=============================\n");
4553 pr_warn("WARNING: suspicious RCU usage\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004554 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004555 pr_warn("-----------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004556 pr_warn("%s:%d %s!\n", file, line, s);
4557 pr_warn("\nother info that might help us debug this:\n\n");
4558 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004559 !rcu_lockdep_current_cpu_online()
4560 ? "RCU used illegally from offline CPU!\n"
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004561 : !rcu_is_watching()
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004562 ? "RCU used illegally from idle CPU!\n"
4563 : "",
4564 rcu_scheduler_active, debug_locks);
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004565
4566 /*
4567 * If a CPU is in the RCU-free window in idle (ie: in the section
4568 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4569 * considers that CPU to be in an "extended quiescent state",
4570 * which means that RCU will be completely ignoring that CPU.
4571 * Therefore, rcu_read_lock() and friends have absolutely no
4572 * effect on a CPU running in that state. In other words, even if
4573 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4574 * delete data structures out from under it. RCU really has no
4575 * choice here: we need to keep an RCU-free window in idle where
4576 * the CPU may possibly enter into low power mode. This way we can
4577 * notice an extended quiescent state to other CPUs that started a grace
4578 * period. Otherwise we would delay any grace period as long as we run
4579 * in the idle task.
4580 *
4581 * So complain bitterly if someone does call rcu_read_lock(),
4582 * rcu_read_lock_bh() and so on from extended quiescent states.
4583 */
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004584 if (!rcu_is_watching())
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004585 pr_warn("RCU used illegally from extended quiescent state!\n");
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004586
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004587 lockdep_print_held_locks(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004588 pr_warn("\nstack backtrace:\n");
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004589 dump_stack();
4590}
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004591EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);