blob: 511d30f88bce59db9ea7584ddc9d54d629bc88f2 [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>
Tejun Heo88f1c872018-01-22 14:00:55 -080052#include <linux/nmi.h>
Peter Zijlstraaf012962009-07-16 15:44:29 +020053
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070054#include <asm/sections.h>
55
56#include "lockdep_internals.h"
57
Joel Fernandes (Google)c3bc8fd2018-07-30 15:24:23 -070058#include <trace/events/preemptirq.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040059#define CREATE_TRACE_POINTS
Frederic Weisbecker67178762009-11-13 10:06:34 +010060#include <trace/events/lock.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040061
Peter Zijlstraf20786f2007-07-19 01:48:56 -070062#ifdef CONFIG_PROVE_LOCKING
63int prove_locking = 1;
64module_param(prove_locking, int, 0644);
65#else
66#define prove_locking 0
67#endif
68
69#ifdef CONFIG_LOCK_STAT
70int lock_stat = 1;
71module_param(lock_stat, int, 0644);
72#else
73#define lock_stat 0
74#endif
75
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070076/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080077 * lockdep_lock: protects the lockdep graph, the hashes and the
78 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070079 *
80 * This is one of the rare exceptions where it's justified
81 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080082 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070083 */
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010084static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Ingo Molnar74c383f2006-12-13 00:34:43 -080085
86static int graph_lock(void)
87{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010088 arch_spin_lock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080089 /*
90 * Make sure that if another CPU detected a bug while
91 * walking the graph we dont change it (while the other
92 * CPU is busy printing out stuff with the graph lock
93 * dropped already)
94 */
95 if (!debug_locks) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010096 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080097 return 0;
98 }
Steven Rostedtbb065af2008-05-12 21:21:00 +020099 /* prevent any recursions within lockdep from causing deadlocks */
100 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -0800101 return 1;
102}
103
104static inline int graph_unlock(void)
105{
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200106 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
107 /*
108 * The lockdep graph lock isn't locked while we expect it to
109 * be, we're confused now, bye!
110 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800111 return DEBUG_LOCKS_WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200112 }
Jarek Poplawski381a2292007-02-10 01:44:58 -0800113
Steven Rostedtbb065af2008-05-12 21:21:00 +0200114 current->lockdep_recursion--;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100115 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800116 return 0;
117}
118
119/*
120 * Turn lock debugging off and return with 0 if it was off already,
121 * and also release the graph lock:
122 */
123static inline int debug_locks_off_graph_unlock(void)
124{
125 int ret = debug_locks_off();
126
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100127 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800128
129 return ret;
130}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700131
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700132unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200133static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700134
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700135/*
136 * All data structures here are protected by the global debug_lock.
137 *
138 * Mutex key structs only get allocated, once during bootup, and never
139 * get freed - this significantly simplifies the debugging code.
140 */
141unsigned long nr_lock_classes;
142static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
143
Dave Jonesf82b2172008-08-11 09:30:23 +0200144static inline struct lock_class *hlock_class(struct held_lock *hlock)
145{
146 if (!hlock->class_idx) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200147 /*
148 * Someone passed in garbage, we give up.
149 */
Dave Jonesf82b2172008-08-11 09:30:23 +0200150 DEBUG_LOCKS_WARN_ON(1);
151 return NULL;
152 }
153 return lock_classes + hlock->class_idx - 1;
154}
155
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700156#ifdef CONFIG_LOCK_STAT
Peter Zijlstra25528212016-03-15 14:52:49 -0700157static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700158
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200159static inline u64 lockstat_clock(void)
160{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200161 return local_clock();
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200162}
163
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200164static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700165{
166 int i;
167
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200168 for (i = 0; i < LOCKSTAT_POINTS; i++) {
169 if (points[i] == 0) {
170 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700171 break;
172 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200173 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700174 break;
175 }
176
177 return i;
178}
179
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200180static void lock_time_inc(struct lock_time *lt, u64 time)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700181{
182 if (time > lt->max)
183 lt->max = time;
184
Frank Rowand109d71c2009-11-19 13:42:06 -0800185 if (time < lt->min || !lt->nr)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700186 lt->min = time;
187
188 lt->total += time;
189 lt->nr++;
190}
191
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700192static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
193{
Frank Rowand109d71c2009-11-19 13:42:06 -0800194 if (!src->nr)
195 return;
196
197 if (src->max > dst->max)
198 dst->max = src->max;
199
200 if (src->min < dst->min || !dst->nr)
201 dst->min = src->min;
202
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700203 dst->total += src->total;
204 dst->nr += src->nr;
205}
206
207struct lock_class_stats lock_stats(struct lock_class *class)
208{
209 struct lock_class_stats stats;
210 int cpu, i;
211
212 memset(&stats, 0, sizeof(struct lock_class_stats));
213 for_each_possible_cpu(cpu) {
214 struct lock_class_stats *pcs =
Tejun Heo1871e522009-10-29 22:34:13 +0900215 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700216
217 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
218 stats.contention_point[i] += pcs->contention_point[i];
219
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200220 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
221 stats.contending_point[i] += pcs->contending_point[i];
222
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700223 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
224 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
225
226 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
227 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700228
229 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
230 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700231 }
232
233 return stats;
234}
235
236void clear_lock_stats(struct lock_class *class)
237{
238 int cpu;
239
240 for_each_possible_cpu(cpu) {
241 struct lock_class_stats *cpu_stats =
Tejun Heo1871e522009-10-29 22:34:13 +0900242 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700243
244 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
245 }
246 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200247 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700248}
249
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700250static struct lock_class_stats *get_lock_stats(struct lock_class *class)
251{
Joel Fernandes (Google)01f38492018-07-30 15:24:21 -0700252 return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes];
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700253}
254
255static void lock_release_holdtime(struct held_lock *hlock)
256{
257 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200258 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700259
260 if (!lock_stat)
261 return;
262
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200263 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700264
Dave Jonesf82b2172008-08-11 09:30:23 +0200265 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700266 if (hlock->read)
267 lock_time_inc(&stats->read_holdtime, holdtime);
268 else
269 lock_time_inc(&stats->write_holdtime, holdtime);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700270}
271#else
272static inline void lock_release_holdtime(struct held_lock *hlock)
273{
274}
275#endif
276
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700277/*
278 * We keep a global list of all lock classes. The list only grows,
279 * never shrinks. The list is only accessed with the lockdep
280 * spinlock lock held.
281 */
282LIST_HEAD(all_lock_classes);
283
284/*
285 * The lockdep classes are in a hash-table as well, for fast lookup:
286 */
287#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
288#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700289#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700290#define classhashentry(key) (classhash_table + __classhashfn((key)))
291
Andrew Mortona63f38c2016-02-03 13:44:12 -0800292static struct hlist_head classhash_table[CLASSHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700293
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700294/*
295 * We put the lock dependency chains into a hash-table as well, to cache
296 * their existence:
297 */
298#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
299#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700300#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700301#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
302
Andrew Mortona63f38c2016-02-03 13:44:12 -0800303static struct hlist_head chainhash_table[CHAINHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700304
305/*
306 * The hash key of the lock dependency chains is a hash itself too:
307 * it's a hash of all locks taken up to that lock, including that lock.
308 * It's a 64-bit hash, because it's important for the keys to be
309 * unique.
310 */
Peter Zijlstradfaaf3f2016-05-30 18:31:33 +0200311static inline u64 iterate_chain_key(u64 key, u32 idx)
312{
313 u32 k0 = key, k1 = key >> 32;
314
315 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
316
317 return k0 | (u64)k1 << 32;
318}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700319
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200320void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700321{
322 current->lockdep_recursion++;
323}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700324EXPORT_SYMBOL(lockdep_off);
325
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200326void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700327{
328 current->lockdep_recursion--;
329}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700330EXPORT_SYMBOL(lockdep_on);
331
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700332/*
333 * Debugging switches:
334 */
335
336#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800337#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700338
339#if VERBOSE
340# define HARDIRQ_VERBOSE 1
341# define SOFTIRQ_VERBOSE 1
342#else
343# define HARDIRQ_VERBOSE 0
344# define SOFTIRQ_VERBOSE 0
345#endif
346
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +0100347#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700348/*
349 * Quick filtering for interesting events:
350 */
351static int class_filter(struct lock_class *class)
352{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700353#if 0
354 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700355 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700356 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700357 return 1;
358 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700359 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700360 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700361#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800362 /* Filter everything else. 1 would be to allow everything else */
363 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700364}
365#endif
366
367static int verbose(struct lock_class *class)
368{
369#if VERBOSE
370 return class_filter(class);
371#endif
372 return 0;
373}
374
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700375/*
376 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800377 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700378 */
379unsigned long nr_stack_trace_entries;
380static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
381
Dave Jones2c522832013-04-25 13:40:02 -0400382static void print_lockdep_off(const char *bug_msg)
383{
384 printk(KERN_DEBUG "%s\n", bug_msg);
385 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200386#ifdef CONFIG_LOCK_STAT
Dave Jones2c522832013-04-25 13:40:02 -0400387 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200388#endif
Dave Jones2c522832013-04-25 13:40:02 -0400389}
390
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700391static int save_trace(struct stack_trace *trace)
392{
393 trace->nr_entries = 0;
394 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
395 trace->entries = stack_trace + nr_stack_trace_entries;
396
Andi Kleen5a1b3992006-09-26 10:52:34 +0200397 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200398
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700399 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700400
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200401 /*
402 * Some daft arches put -1 at the end to indicate its a full trace.
403 *
404 * <rant> this is buggy anyway, since it takes a whole extra entry so a
405 * complete trace that maxes out the entries provided will be reported
406 * as incomplete, friggin useless </rant>
407 */
Luck, Tonyea5b41f2009-12-09 14:29:36 -0800408 if (trace->nr_entries != 0 &&
409 trace->entries[trace->nr_entries-1] == ULONG_MAX)
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200410 trace->nr_entries--;
411
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700412 trace->max_entries = trace->nr_entries;
413
414 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700415
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200416 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800417 if (!debug_locks_off_graph_unlock())
418 return 0;
419
Dave Jones2c522832013-04-25 13:40:02 -0400420 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
Ingo Molnar74c383f2006-12-13 00:34:43 -0800421 dump_stack();
422
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700423 return 0;
424 }
425
426 return 1;
427}
428
429unsigned int nr_hardirq_chains;
430unsigned int nr_softirq_chains;
431unsigned int nr_process_chains;
432unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700433
434#ifdef CONFIG_DEBUG_LOCKDEP
435/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700436 * Various lockdep statistics:
437 */
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200438DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700439#endif
440
441/*
442 * Locking printouts:
443 */
444
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100445#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100446 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
447 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
448 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
449 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100450
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700451static const char *usage_str[] =
452{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100453#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
454#include "lockdep_states.h"
455#undef LOCKDEP_STATE
456 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700457};
458
459const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
460{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700461 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700462}
463
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100464static inline unsigned long lock_flag(enum lock_usage_bit bit)
465{
466 return 1UL << bit;
467}
468
469static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
470{
471 char c = '.';
472
473 if (class->usage_mask & lock_flag(bit + 2))
474 c = '+';
475 if (class->usage_mask & lock_flag(bit)) {
476 c = '-';
477 if (class->usage_mask & lock_flag(bit + 2))
478 c = '?';
479 }
480
481 return c;
482}
483
Peter Zijlstraf510b232009-01-22 17:53:47 +0100484void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700485{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100486 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700487
Peter Zijlstraf510b232009-01-22 17:53:47 +0100488#define LOCKDEP_STATE(__STATE) \
489 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
490 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
491#include "lockdep_states.h"
492#undef LOCKDEP_STATE
493
494 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700495}
496
Steven Rostedte5e78d02011-11-02 20:24:16 -0400497static void __print_lock_name(struct lock_class *class)
Steven Rostedt3003eba2011-04-20 21:41:54 -0400498{
499 char str[KSYM_NAME_LEN];
500 const char *name;
501
502 name = class->name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700503 if (!name) {
504 name = __get_key_name(class->key, str);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100505 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700506 } else {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100507 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700508 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100509 printk(KERN_CONT "#%d", class->name_version);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700510 if (class->subclass)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100511 printk(KERN_CONT "/%d", class->subclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700512 }
Steven Rostedte5e78d02011-11-02 20:24:16 -0400513}
514
515static void print_lock_name(struct lock_class *class)
516{
517 char usage[LOCK_USAGE_CHARS];
518
519 get_usage_chars(class, usage);
520
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100521 printk(KERN_CONT " (");
Steven Rostedte5e78d02011-11-02 20:24:16 -0400522 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100523 printk(KERN_CONT "){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700524}
525
526static void print_lockdep_cache(struct lockdep_map *lock)
527{
528 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700529 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700530
531 name = lock->name;
532 if (!name)
533 name = __get_key_name(lock->key->subkeys, str);
534
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100535 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700536}
537
538static void print_lock(struct held_lock *hlock)
539{
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200540 /*
541 * We can be called locklessly through debug_show_all_locks() so be
542 * extra careful, the hlock might have been released and cleared.
543 */
544 unsigned int class_idx = hlock->class_idx;
545
546 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
547 barrier();
548
549 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100550 printk(KERN_CONT "<RELEASED>\n");
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200551 return;
552 }
553
Tetsuo Handab3c39752018-03-27 19:41:41 +0900554 printk(KERN_CONT "%p", hlock->instance);
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200555 print_lock_name(lock_classes + class_idx - 1);
Tetsuo Handab3c39752018-03-27 19:41:41 +0900556 printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700557}
558
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900559static void lockdep_print_held_locks(struct task_struct *p)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700560{
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900561 int i, depth = READ_ONCE(p->lockdep_depth);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700562
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900563 if (!depth)
564 printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p));
565 else
566 printk("%d lock%s held by %s/%d:\n", depth,
567 depth > 1 ? "s" : "", p->comm, task_pid_nr(p));
568 /*
569 * It's not reliable to print a task's held locks if it's not sleeping
570 * and it's not the current task.
571 */
572 if (p->state == TASK_RUNNING && p != current)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700573 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700574 for (i = 0; i < depth; i++) {
575 printk(" #%d: ", i);
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900576 print_lock(p->held_locks + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700577 }
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 *
Matthew Wilcox08f36ff2018-01-17 07:14:13 -0800651look_up_lock_class(const 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
Borislav Petkov04860d42018-02-26 14:49:26 +0100810 printk("\nnew class %px: %s", class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700811 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
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04001263 raw_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);
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04001267 raw_local_irq_restore(flags);
David Miller419ca3f2008-07-29 21:45:03 -07001268
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
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04001290 raw_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);
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04001294 raw_local_irq_restore(flags);
David Miller419ca3f2008-07-29 21:45:03 -07001295
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
Borislav Petkov04860d42018-02-26 14:49:26 +01001409 printk("%*s ... key at: [<%px>] %pS\n",
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001410 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 Park545c23f2017-08-07 16:12:48 +09002152 * Adds a dependency chain into chain hashtable. And must be called with
2153 * graph_lock held.
2154 *
2155 * Return 0 if fail, and graph_lock is released.
2156 * Return 1 if succeed, with graph_lock held.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002157 */
Byungchul Park545c23f2017-08-07 16:12:48 +09002158static inline int add_chain_cache(struct task_struct *curr,
2159 struct held_lock *hlock,
2160 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002161{
Dave Jonesf82b2172008-08-11 09:30:23 +02002162 struct lock_class *class = hlock_class(hlock);
Andrew Mortona63f38c2016-02-03 13:44:12 -08002163 struct hlist_head *hash_head = chainhashentry(chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002164 struct lock_chain *chain;
Steven Rostedte0944ee2011-04-20 21:42:00 -04002165 int i, j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002166
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002167 /*
Byungchul Park545c23f2017-08-07 16:12:48 +09002168 * Allocate a new chain entry from the static array, and add
2169 * it to the hash:
2170 */
2171
2172 /*
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002173 * We might need to take the graph lock, ensure we've got IRQs
2174 * disabled to make this an IRQ-safe lock.. for recursion reasons
2175 * lockdep won't complain about its own locking errors.
2176 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08002177 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2178 return 0;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002179
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002180 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002181 if (!debug_locks_off_graph_unlock())
2182 return 0;
2183
Dave Jones2c522832013-04-25 13:40:02 -04002184 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002185 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002186 return 0;
2187 }
2188 chain = lock_chains + nr_lock_chains++;
2189 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08002190 chain->irq_context = hlock->irq_context;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002191 i = get_first_held_lock(curr, hlock);
Huang, Ying443cd502008-06-20 16:39:21 +08002192 chain->depth = curr->lockdep_depth + 1 - i;
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002193
2194 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
2195 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
2196 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
2197
Steven Rostedte0944ee2011-04-20 21:42:00 -04002198 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2199 chain->base = nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08002200 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02002201 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08002202 chain_hlocks[chain->base + j] = lock_id;
2203 }
2204 chain_hlocks[chain->base + j] = class - lock_classes;
2205 }
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002206
2207 if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS)
2208 nr_chain_hlocks += chain->depth;
2209
2210#ifdef CONFIG_DEBUG_LOCKDEP
2211 /*
2212 * Important for check_no_collision().
2213 */
2214 if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) {
Byungchul Parkf9af4562017-01-13 11:42:04 +09002215 if (!debug_locks_off_graph_unlock())
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002216 return 0;
2217
2218 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2219 dump_stack();
2220 return 0;
2221 }
2222#endif
2223
Andrew Mortona63f38c2016-02-03 13:44:12 -08002224 hlist_add_head_rcu(&chain->entry, hash_head);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002225 debug_atomic_inc(chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002226 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002227
2228 return 1;
2229}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002230
Byungchul Park545c23f2017-08-07 16:12:48 +09002231/*
2232 * Look up a dependency chain.
2233 */
2234static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
2235{
2236 struct hlist_head *hash_head = chainhashentry(chain_key);
2237 struct lock_chain *chain;
2238
2239 /*
2240 * We can walk it lock-free, because entries only get added
2241 * to the hash:
2242 */
2243 hlist_for_each_entry_rcu(chain, hash_head, entry) {
2244 if (chain->chain_key == chain_key) {
2245 debug_atomic_inc(chain_lookup_hits);
2246 return chain;
2247 }
2248 }
2249 return NULL;
2250}
2251
2252/*
2253 * If the key is not present yet in dependency chain cache then
2254 * add it and return 1 - in this case the new dependency chain is
2255 * validated. If the key is already hashed, return 0.
2256 * (On return with 1 graph_lock is held.)
2257 */
2258static inline int lookup_chain_cache_add(struct task_struct *curr,
2259 struct held_lock *hlock,
2260 u64 chain_key)
2261{
2262 struct lock_class *class = hlock_class(hlock);
2263 struct lock_chain *chain = lookup_chain_cache(chain_key);
2264
2265 if (chain) {
2266cache_hit:
2267 if (!check_no_collision(curr, hlock, chain))
2268 return 0;
2269
2270 if (very_verbose(class)) {
2271 printk("\nhash chain already cached, key: "
Borislav Petkov04860d42018-02-26 14:49:26 +01002272 "%016Lx tail class: [%px] %s\n",
Byungchul Park545c23f2017-08-07 16:12:48 +09002273 (unsigned long long)chain_key,
2274 class->key, class->name);
2275 }
2276
2277 return 0;
2278 }
2279
2280 if (very_verbose(class)) {
Borislav Petkov04860d42018-02-26 14:49:26 +01002281 printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
Byungchul Park545c23f2017-08-07 16:12:48 +09002282 (unsigned long long)chain_key, class->key, class->name);
2283 }
2284
2285 if (!graph_lock())
2286 return 0;
2287
2288 /*
2289 * We have to walk the chain again locked - to avoid duplicates:
2290 */
2291 chain = lookup_chain_cache(chain_key);
2292 if (chain) {
2293 graph_unlock();
2294 goto cache_hit;
2295 }
2296
2297 if (!add_chain_cache(curr, hlock, chain_key))
2298 return 0;
2299
2300 return 1;
2301}
2302
Peter Zijlstra8e182572007-07-19 01:48:54 -07002303static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07002304 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002305{
2306 /*
2307 * Trylock needs to maintain the stack of held locks, but it
2308 * does not add new dependencies, because trylock can be done
2309 * in any order.
2310 *
2311 * We look up the chain_key and do the O(N^2) check and update of
2312 * the dependencies only if this is a new dependency chain.
Byungchul Park545c23f2017-08-07 16:12:48 +09002313 * (If lookup_chain_cache_add() return with 1 it acquires
Peter Zijlstra8e182572007-07-19 01:48:54 -07002314 * graph_lock for us)
2315 */
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01002316 if (!hlock->trylock && hlock->check &&
Byungchul Park545c23f2017-08-07 16:12:48 +09002317 lookup_chain_cache_add(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002318 /*
2319 * Check whether last held lock:
2320 *
2321 * - is irq-safe, if this lock is irq-unsafe
2322 * - is softirq-safe, if this lock is hardirq-unsafe
2323 *
2324 * And check whether the new lock's dependency graph
2325 * could lead back to the previous lock.
2326 *
2327 * any of these scenarios could lead to a deadlock. If
2328 * All validations
2329 */
2330 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2331
2332 if (!ret)
2333 return 0;
2334 /*
2335 * Mark recursive read, as we jump over it when
2336 * building dependencies (just like we jump over
2337 * trylock entries):
2338 */
2339 if (ret == 2)
2340 hlock->read = 2;
2341 /*
2342 * Add dependency only if this lock is not the head
2343 * of the chain, and if it's not a secondary read-lock:
2344 */
Byungchul Park545c23f2017-08-07 16:12:48 +09002345 if (!chain_head && ret != 2) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002346 if (!check_prevs_add(curr, hlock))
2347 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09002348 }
2349
Peter Zijlstra8e182572007-07-19 01:48:54 -07002350 graph_unlock();
Byungchul Park545c23f2017-08-07 16:12:48 +09002351 } else {
2352 /* after lookup_chain_cache_add(): */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002353 if (unlikely(!debug_locks))
2354 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09002355 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07002356
2357 return 1;
2358}
2359#else
2360static inline int validate_chain(struct task_struct *curr,
2361 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002362 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002363{
2364 return 1;
2365}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07002366#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002367
2368/*
2369 * We are building curr_chain_key incrementally, so double-check
2370 * it from scratch, to make sure that it's done correctly:
2371 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002372static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002373{
2374#ifdef CONFIG_DEBUG_LOCKDEP
2375 struct held_lock *hlock, *prev_hlock = NULL;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002376 unsigned int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002377 u64 chain_key = 0;
2378
2379 for (i = 0; i < curr->lockdep_depth; i++) {
2380 hlock = curr->held_locks + i;
2381 if (chain_key != hlock->prev_chain_key) {
2382 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002383 /*
2384 * We got mighty confused, our chain keys don't match
2385 * with what we expect, someone trample on our task state?
2386 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002387 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002388 curr->lockdep_depth, i,
2389 (unsigned long long)chain_key,
2390 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002391 return;
2392 }
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002393 /*
2394 * Whoops ran out of static storage again?
2395 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002396 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
Jarek Poplawski381a2292007-02-10 01:44:58 -08002397 return;
2398
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002399 if (prev_hlock && (prev_hlock->irq_context !=
2400 hlock->irq_context))
2401 chain_key = 0;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002402 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002403 prev_hlock = hlock;
2404 }
2405 if (chain_key != curr->curr_chain_key) {
2406 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002407 /*
2408 * More smoking hash instead of calculating it, damn see these
2409 * numbers float.. I bet that a pink elephant stepped on my memory.
2410 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002411 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002412 curr->lockdep_depth, i,
2413 (unsigned long long)chain_key,
2414 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002415 }
2416#endif
2417}
2418
Steven Rostedt282b5c22011-04-20 21:41:59 -04002419static void
2420print_usage_bug_scenario(struct held_lock *lock)
2421{
2422 struct lock_class *class = hlock_class(lock);
2423
2424 printk(" Possible unsafe locking scenario:\n\n");
2425 printk(" CPU0\n");
2426 printk(" ----\n");
2427 printk(" lock(");
2428 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002429 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002430 printk(" <Interrupt>\n");
2431 printk(" lock(");
2432 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002433 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002434 printk("\n *** DEADLOCK ***\n\n");
2435}
2436
Peter Zijlstra8e182572007-07-19 01:48:54 -07002437static int
2438print_usage_bug(struct task_struct *curr, struct held_lock *this,
2439 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2440{
2441 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2442 return 0;
2443
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002444 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002445 pr_warn("================================\n");
2446 pr_warn("WARNING: inconsistent lock state\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002447 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002448 pr_warn("--------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002449
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002450 pr_warn("inconsistent {%s} -> {%s} usage.\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07002451 usage_str[prev_bit], usage_str[new_bit]);
2452
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002453 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002454 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002455 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2456 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2457 trace_hardirqs_enabled(curr),
2458 trace_softirqs_enabled(curr));
2459 print_lock(this);
2460
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002461 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002462 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002463
2464 print_irqtrace_events(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002465 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002466 print_usage_bug_scenario(this);
2467
Peter Zijlstra8e182572007-07-19 01:48:54 -07002468 lockdep_print_held_locks(curr);
2469
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002470 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002471 dump_stack();
2472
2473 return 0;
2474}
2475
2476/*
2477 * Print out an error if an invalid bit is set:
2478 */
2479static inline int
2480valid_state(struct task_struct *curr, struct held_lock *this,
2481 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2482{
Dave Jonesf82b2172008-08-11 09:30:23 +02002483 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002484 return print_usage_bug(curr, this, bad_bit, new_bit);
2485 return 1;
2486}
2487
2488static int mark_lock(struct task_struct *curr, struct held_lock *this,
2489 enum lock_usage_bit new_bit);
2490
Steven Rostedt81d68a92008-05-12 21:20:42 +02002491#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002492
2493/*
2494 * print irq inversion bug:
2495 */
2496static int
Ming Lei24208ca2009-07-16 15:44:29 +02002497print_irq_inversion_bug(struct task_struct *curr,
2498 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002499 struct held_lock *this, int forwards,
2500 const char *irqclass)
2501{
Steven Rostedtdad3d742011-04-20 21:41:57 -04002502 struct lock_list *entry = other;
2503 struct lock_list *middle = NULL;
2504 int depth;
2505
Ingo Molnar74c383f2006-12-13 00:34:43 -08002506 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002507 return 0;
2508
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002509 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002510 pr_warn("========================================================\n");
2511 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002512 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002513 pr_warn("--------------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002514 pr_warn("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002515 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002516 print_lock(this);
2517 if (forwards)
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002518 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002519 else
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002520 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002521 print_lock_name(other->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002522 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002523
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002524 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04002525
2526 /* Find a middle lock (if one exists) */
2527 depth = get_lock_depth(other);
2528 do {
2529 if (depth == 0 && (entry != root)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002530 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
Steven Rostedtdad3d742011-04-20 21:41:57 -04002531 break;
2532 }
2533 middle = entry;
2534 entry = get_lock_parent(entry);
2535 depth--;
2536 } while (entry && entry != root && (depth >= 0));
2537 if (forwards)
2538 print_irq_lock_scenario(root, other,
2539 middle ? middle->class : root->class, other->class);
2540 else
2541 print_irq_lock_scenario(other, root,
2542 middle ? middle->class : other->class, root->class);
2543
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002544 lockdep_print_held_locks(curr);
2545
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002546 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
Ming Lei24208ca2009-07-16 15:44:29 +02002547 if (!save_trace(&root->trace))
2548 return 0;
2549 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002550
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002551 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002552 dump_stack();
2553
2554 return 0;
2555}
2556
2557/*
2558 * Prove that in the forwards-direction subgraph starting at <this>
2559 * there is no lock matching <mask>:
2560 */
2561static int
2562check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2563 enum lock_usage_bit bit, const char *irqclass)
2564{
2565 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002566 struct lock_list root;
2567 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002568
Ming Leid7aaba12009-07-16 15:44:29 +02002569 root.parent = NULL;
2570 root.class = hlock_class(this);
2571 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002572 if (ret < 0)
2573 return print_bfs_bug(ret);
2574 if (ret == 1)
2575 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002576
Ming Lei24208ca2009-07-16 15:44:29 +02002577 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002578 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002579}
2580
2581/*
2582 * Prove that in the backwards-direction subgraph starting at <this>
2583 * there is no lock matching <mask>:
2584 */
2585static int
2586check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2587 enum lock_usage_bit bit, const char *irqclass)
2588{
2589 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002590 struct lock_list root;
2591 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002592
Ming Leid7aaba12009-07-16 15:44:29 +02002593 root.parent = NULL;
2594 root.class = hlock_class(this);
2595 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002596 if (ret < 0)
2597 return print_bfs_bug(ret);
2598 if (ret == 1)
2599 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002600
Ming Lei24208ca2009-07-16 15:44:29 +02002601 return print_irq_inversion_bug(curr, &root, target_entry,
Oleg Nesterov48d50672010-01-26 19:16:41 +01002602 this, 0, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002603}
2604
Ingo Molnar3117df02006-12-13 00:34:43 -08002605void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002606{
2607 printk("irq event stamp: %u\n", curr->irq_events);
Borislav Petkov04860d42018-02-26 14:49:26 +01002608 printk("hardirqs last enabled at (%u): [<%px>] %pS\n",
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002609 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
2610 (void *)curr->hardirq_enable_ip);
Borislav Petkov04860d42018-02-26 14:49:26 +01002611 printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002612 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
2613 (void *)curr->hardirq_disable_ip);
Borislav Petkov04860d42018-02-26 14:49:26 +01002614 printk("softirqs last enabled at (%u): [<%px>] %pS\n",
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002615 curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
2616 (void *)curr->softirq_enable_ip);
Borislav Petkov04860d42018-02-26 14:49:26 +01002617 printk("softirqs last disabled at (%u): [<%px>] %pS\n",
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002618 curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
2619 (void *)curr->softirq_disable_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002620}
2621
Peter Zijlstracd953022009-01-22 16:38:21 +01002622static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002623{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002624#if HARDIRQ_VERBOSE
2625 return class_filter(class);
2626#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002627 return 0;
2628}
2629
Peter Zijlstracd953022009-01-22 16:38:21 +01002630static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002631{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002632#if SOFTIRQ_VERBOSE
2633 return class_filter(class);
2634#endif
2635 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002636}
2637
2638#define STRICT_READ_CHECKS 1
2639
Peter Zijlstracd953022009-01-22 16:38:21 +01002640static int (*state_verbose_f[])(struct lock_class *class) = {
2641#define LOCKDEP_STATE(__STATE) \
2642 __STATE##_verbose,
2643#include "lockdep_states.h"
2644#undef LOCKDEP_STATE
2645};
2646
2647static inline int state_verbose(enum lock_usage_bit bit,
2648 struct lock_class *class)
2649{
2650 return state_verbose_f[bit >> 2](class);
2651}
2652
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002653typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2654 enum lock_usage_bit bit, const char *name);
2655
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002656static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002657mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2658 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002659{
Peter Zijlstraf9892092009-01-22 16:09:59 +01002660 int excl_bit = exclusive_bit(new_bit);
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002661 int read = new_bit & 1;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002662 int dir = new_bit & 2;
2663
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002664 /*
2665 * mark USED_IN has to look forwards -- to ensure no dependency
2666 * has ENABLED state, which would allow recursion deadlocks.
2667 *
2668 * mark ENABLED has to look backwards -- to ensure no dependee
2669 * has USED_IN state, which, again, would allow recursion deadlocks.
2670 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01002671 check_usage_f usage = dir ?
2672 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01002673
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002674 /*
2675 * Validate that this particular lock does not have conflicting
2676 * usage states.
2677 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002678 if (!valid_state(curr, this, new_bit, excl_bit))
2679 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01002680
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002681 /*
2682 * Validate that the lock dependencies don't have conflicting usage
2683 * states.
2684 */
2685 if ((!read || !dir || STRICT_READ_CHECKS) &&
Peter Zijlstra1c21f142009-03-04 13:51:13 +01002686 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002687 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01002688
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002689 /*
2690 * Check for read in write conflicts
2691 */
2692 if (!read) {
2693 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2694 return 0;
2695
2696 if (STRICT_READ_CHECKS &&
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002697 !usage(curr, this, excl_bit + 1,
2698 state_name(new_bit + 1)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01002699 return 0;
2700 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01002701
Peter Zijlstracd953022009-01-22 16:38:21 +01002702 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01002703 return 2;
2704
2705 return 1;
2706}
2707
Nick Piggincf40bd12009-01-21 08:12:39 +01002708enum mark_type {
Peter Zijlstra36bfb9b2009-01-22 14:12:41 +01002709#define LOCKDEP_STATE(__STATE) __STATE,
2710#include "lockdep_states.h"
2711#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002712};
2713
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002714/*
2715 * Mark all held locks with a usage bit:
2716 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002717static int
Nick Piggincf40bd12009-01-21 08:12:39 +01002718mark_held_locks(struct task_struct *curr, enum mark_type mark)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002719{
2720 enum lock_usage_bit usage_bit;
2721 struct held_lock *hlock;
2722 int i;
2723
2724 for (i = 0; i < curr->lockdep_depth; i++) {
2725 hlock = curr->held_locks + i;
2726
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01002727 usage_bit = 2 + (mark << 2); /* ENABLED */
2728 if (hlock->read)
2729 usage_bit += 1; /* READ */
2730
2731 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01002732
Oleg Nesterov34d0ed52014-01-20 19:20:13 +01002733 if (!hlock->check)
Peter Zijlstraefbe2ee2011-07-07 11:39:45 +02002734 continue;
2735
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07002736 if (!mark_lock(curr, hlock, usage_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002737 return 0;
2738 }
2739
2740 return 1;
2741}
2742
2743/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002744 * Hardirqs will be enabled:
2745 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002746static void __trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002747{
2748 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002749
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002750 /* we'll do an OFF -> ON transition: */
2751 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002752
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002753 /*
2754 * We are going to turn hardirqs on, so set the
2755 * usage bit for all held locks:
2756 */
Nick Piggincf40bd12009-01-21 08:12:39 +01002757 if (!mark_held_locks(curr, HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002758 return;
2759 /*
2760 * If we have softirqs enabled, then set the usage
2761 * bit for all held locks. (disabled hardirqs prevented
2762 * this bit from being set before)
2763 */
2764 if (curr->softirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002765 if (!mark_held_locks(curr, SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002766 return;
2767
2768 curr->hardirq_enable_ip = ip;
2769 curr->hardirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002770 debug_atomic_inc(hardirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002771}
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002772
Steven Rostedt (VMware)bff1b202018-08-06 15:50:58 -04002773void lockdep_hardirqs_on(unsigned long ip)
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002774{
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002775 if (unlikely(!debug_locks || current->lockdep_recursion))
2776 return;
2777
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002778 if (unlikely(current->hardirqs_enabled)) {
2779 /*
2780 * Neither irq nor preemption are disabled here
2781 * so this is racy by nature but losing one hit
2782 * in a stat is not a big deal.
2783 */
2784 __debug_atomic_inc(redundant_hardirqs_on);
2785 return;
2786 }
2787
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002788 /*
2789 * We're enabling irqs and according to our state above irqs weren't
2790 * already enabled, yet we find the hardware thinks they are in fact
2791 * enabled.. someone messed up their IRQ state tracing.
2792 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002793 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2794 return;
2795
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002796 /*
2797 * See the fine text that goes along with this variable definition.
2798 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002799 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2800 return;
2801
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002802 /*
2803 * Can't allow enabling interrupts while in an interrupt handler,
2804 * that's general bad form and such. Recursion, limited stack etc..
2805 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02002806 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2807 return;
2808
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002809 current->lockdep_recursion = 1;
2810 __trace_hardirqs_on_caller(ip);
2811 current->lockdep_recursion = 0;
2812}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002813
2814/*
2815 * Hardirqs were disabled:
2816 */
Steven Rostedt (VMware)bff1b202018-08-06 15:50:58 -04002817void lockdep_hardirqs_off(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002818{
2819 struct task_struct *curr = current;
2820
2821 if (unlikely(!debug_locks || current->lockdep_recursion))
2822 return;
2823
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002824 /*
2825 * So we're supposed to get called after you mask local IRQs, but for
2826 * some reason the hardware doesn't quite think you did a proper job.
2827 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002828 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2829 return;
2830
2831 if (curr->hardirqs_enabled) {
2832 /*
2833 * We have done an ON -> OFF transition:
2834 */
2835 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01002836 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002837 curr->hardirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002838 debug_atomic_inc(hardirqs_off_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002839 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002840 debug_atomic_inc(redundant_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002841}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002842
2843/*
2844 * Softirqs will be enabled:
2845 */
2846void trace_softirqs_on(unsigned long ip)
2847{
2848 struct task_struct *curr = current;
2849
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002850 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002851 return;
2852
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002853 /*
2854 * We fancy IRQs being disabled here, see softirq.c, avoids
2855 * funny state and nesting things.
2856 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002857 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2858 return;
2859
2860 if (curr->softirqs_enabled) {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002861 debug_atomic_inc(redundant_softirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002862 return;
2863 }
2864
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002865 current->lockdep_recursion = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002866 /*
2867 * We'll do an OFF -> ON transition:
2868 */
2869 curr->softirqs_enabled = 1;
2870 curr->softirq_enable_ip = ip;
2871 curr->softirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002872 debug_atomic_inc(softirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002873 /*
2874 * We are going to turn softirqs on, so set the
2875 * usage bit for all held locks, if hardirqs are
2876 * enabled too:
2877 */
2878 if (curr->hardirqs_enabled)
Nick Piggincf40bd12009-01-21 08:12:39 +01002879 mark_held_locks(curr, SOFTIRQ);
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002880 current->lockdep_recursion = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002881}
2882
2883/*
2884 * Softirqs were disabled:
2885 */
2886void trace_softirqs_off(unsigned long ip)
2887{
2888 struct task_struct *curr = current;
2889
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02002890 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002891 return;
2892
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002893 /*
2894 * We fancy IRQs being disabled here, see softirq.c
2895 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002896 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2897 return;
2898
2899 if (curr->softirqs_enabled) {
2900 /*
2901 * We have done an ON -> OFF transition:
2902 */
2903 curr->softirqs_enabled = 0;
2904 curr->softirq_disable_ip = ip;
2905 curr->softirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002906 debug_atomic_inc(softirqs_off_events);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002907 /*
2908 * Whoops, we wanted softirqs off, so why aren't they?
2909 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002910 DEBUG_LOCKS_WARN_ON(!softirq_count());
2911 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002912 debug_atomic_inc(redundant_softirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002913}
2914
Peter Zijlstra8e182572007-07-19 01:48:54 -07002915static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2916{
2917 /*
2918 * If non-trylock use in a hardirq or softirq context, then
2919 * mark the lock as used in these contexts:
2920 */
2921 if (!hlock->trylock) {
2922 if (hlock->read) {
2923 if (curr->hardirq_context)
2924 if (!mark_lock(curr, hlock,
2925 LOCK_USED_IN_HARDIRQ_READ))
2926 return 0;
2927 if (curr->softirq_context)
2928 if (!mark_lock(curr, hlock,
2929 LOCK_USED_IN_SOFTIRQ_READ))
2930 return 0;
2931 } else {
2932 if (curr->hardirq_context)
2933 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2934 return 0;
2935 if (curr->softirq_context)
2936 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2937 return 0;
2938 }
2939 }
2940 if (!hlock->hardirqs_off) {
2941 if (hlock->read) {
2942 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002943 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002944 return 0;
2945 if (curr->softirqs_enabled)
2946 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002947 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002948 return 0;
2949 } else {
2950 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002951 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002952 return 0;
2953 if (curr->softirqs_enabled)
2954 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01002955 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002956 return 0;
2957 }
2958 }
2959
2960 return 1;
2961}
2962
Boqun Fengc2469752016-02-16 13:57:40 +08002963static inline unsigned int task_irq_context(struct task_struct *task)
2964{
2965 return 2 * !!task->hardirq_context + !!task->softirq_context;
2966}
2967
Peter Zijlstra8e182572007-07-19 01:48:54 -07002968static int separate_irq_context(struct task_struct *curr,
2969 struct held_lock *hlock)
2970{
2971 unsigned int depth = curr->lockdep_depth;
2972
2973 /*
2974 * Keep track of points where we cross into an interrupt context:
2975 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002976 if (depth) {
2977 struct held_lock *prev_hlock;
2978
2979 prev_hlock = curr->held_locks + depth-1;
2980 /*
2981 * If we cross into another context, reset the
2982 * hash key (this also prevents the checking and the
2983 * adding of the dependency to 'prev'):
2984 */
2985 if (prev_hlock->irq_context != hlock->irq_context)
2986 return 1;
2987 }
2988 return 0;
2989}
2990
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002991#else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002992
2993static inline
2994int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2995 enum lock_usage_bit new_bit)
2996{
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002997 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002998 return 1;
2999}
3000
3001static inline int mark_irqflags(struct task_struct *curr,
3002 struct held_lock *hlock)
3003{
3004 return 1;
3005}
3006
Boqun Fengc2469752016-02-16 13:57:40 +08003007static inline unsigned int task_irq_context(struct task_struct *task)
3008{
3009 return 0;
3010}
3011
Peter Zijlstra8e182572007-07-19 01:48:54 -07003012static inline int separate_irq_context(struct task_struct *curr,
3013 struct held_lock *hlock)
3014{
3015 return 0;
3016}
3017
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003018#endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003019
3020/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07003021 * Mark a lock with a usage bit, and validate the state transition:
3022 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003023static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02003024 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07003025{
3026 unsigned int new_mask = 1 << new_bit, ret = 1;
3027
3028 /*
3029 * If already set then do not dirty the cacheline,
3030 * nor do any checks:
3031 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003032 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003033 return 1;
3034
3035 if (!graph_lock())
3036 return 0;
3037 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003038 * Make sure we didn't race:
Peter Zijlstra8e182572007-07-19 01:48:54 -07003039 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003040 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07003041 graph_unlock();
3042 return 1;
3043 }
3044
Dave Jonesf82b2172008-08-11 09:30:23 +02003045 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003046
Dave Jonesf82b2172008-08-11 09:30:23 +02003047 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003048 return 0;
3049
3050 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01003051#define LOCKDEP_STATE(__STATE) \
3052 case LOCK_USED_IN_##__STATE: \
3053 case LOCK_USED_IN_##__STATE##_READ: \
3054 case LOCK_ENABLED_##__STATE: \
3055 case LOCK_ENABLED_##__STATE##_READ:
3056#include "lockdep_states.h"
3057#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07003058 ret = mark_lock_irq(curr, this, new_bit);
3059 if (!ret)
3060 return 0;
3061 break;
3062 case LOCK_USED:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003063 debug_atomic_dec(nr_unused_locks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07003064 break;
3065 default:
3066 if (!debug_locks_off_graph_unlock())
3067 return 0;
3068 WARN_ON(1);
3069 return 0;
3070 }
3071
3072 graph_unlock();
3073
3074 /*
3075 * We must printk outside of the graph_lock:
3076 */
3077 if (ret == 2) {
3078 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
3079 print_lock(this);
3080 print_irqtrace_events(curr);
3081 dump_stack();
3082 }
3083
3084 return ret;
3085}
3086
3087/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003088 * Initialize a lock instance's lock-class mapping info:
3089 */
Byungchul Parkb09be672017-08-07 16:12:52 +09003090static void __lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003091 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003092{
Yong Zhangd3d03d42011-11-09 16:04:51 +08003093 int i;
3094
Yong Zhangd3d03d42011-11-09 16:04:51 +08003095 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
3096 lock->class_cache[i] = NULL;
Hitoshi Mitake62016252010-10-05 18:01:51 +09003097
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003098#ifdef CONFIG_LOCK_STAT
3099 lock->cpu = raw_smp_processor_id();
3100#endif
3101
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003102 /*
3103 * Can't be having no nameless bastards around this place!
3104 */
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003105 if (DEBUG_LOCKS_WARN_ON(!name)) {
3106 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003107 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003108 }
3109
3110 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003111
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003112 /*
3113 * No key, no joy, we need to hash something.
3114 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003115 if (DEBUG_LOCKS_WARN_ON(!key))
3116 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003117 /*
3118 * Sanity check, the lock-class key must be persistent:
3119 */
3120 if (!static_obj(key)) {
Borislav Petkov04860d42018-02-26 14:49:26 +01003121 printk("BUG: key %px not in .data!\n", key);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003122 /*
3123 * What it says above ^^^^^, I suggest you read it.
3124 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003125 DEBUG_LOCKS_WARN_ON(1);
3126 return;
3127 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003128 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003129
3130 if (unlikely(!debug_locks))
3131 return;
3132
Peter Zijlstra35a93932015-02-26 16:23:11 +01003133 if (subclass) {
3134 unsigned long flags;
3135
3136 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3137 return;
3138
3139 raw_local_irq_save(flags);
3140 current->lockdep_recursion = 1;
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003141 register_lock_class(lock, subclass, 1);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003142 current->lockdep_recursion = 0;
3143 raw_local_irq_restore(flags);
3144 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003145}
Byungchul Parkb09be672017-08-07 16:12:52 +09003146
3147void lockdep_init_map(struct lockdep_map *lock, const char *name,
3148 struct lock_class_key *key, int subclass)
3149{
Byungchul Parkb09be672017-08-07 16:12:52 +09003150 __lockdep_init_map(lock, name, key, subclass);
3151}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003152EXPORT_SYMBOL_GPL(lockdep_init_map);
3153
Peter Zijlstra1704f472010-03-19 01:37:42 +01003154struct lock_class_key __lockdep_no_validate__;
Kent Overstreetea6749c2012-12-27 22:21:58 -08003155EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003156
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003157static int
3158print_lock_nested_lock_not_held(struct task_struct *curr,
3159 struct held_lock *hlock,
3160 unsigned long ip)
3161{
3162 if (!debug_locks_off())
3163 return 0;
3164 if (debug_locks_silent)
3165 return 0;
3166
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003167 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003168 pr_warn("==================================\n");
3169 pr_warn("WARNING: Nested lock was not taken\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003170 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003171 pr_warn("----------------------------------\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003172
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003173 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003174 print_lock(hlock);
3175
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003176 pr_warn("\nbut this task is not holding:\n");
3177 pr_warn("%s\n", hlock->nest_lock->name);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003178
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003179 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003180 dump_stack();
3181
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003182 pr_warn("\nother info that might help us debug this:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003183 lockdep_print_held_locks(curr);
3184
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003185 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003186 dump_stack();
3187
3188 return 0;
3189}
3190
Matthew Wilcox08f36ff2018-01-17 07:14:13 -08003191static int __lock_is_held(const struct lockdep_map *lock, int read);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003192
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003193/*
3194 * This gets called for every mutex_lock*()/spin_lock*() operation.
3195 * We maintain the dependency maps and validate the locking attempt:
Waiman Long8ee10862018-10-02 16:19:17 -04003196 *
3197 * The callers must make sure that IRQs are disabled before calling it,
3198 * otherwise we could get an interrupt which would want to take locks,
3199 * which would end up in lockdep again.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003200 */
3201static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3202 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003203 struct lockdep_map *nest_lock, unsigned long ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003204 int references, int pin_count)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003205{
3206 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07003207 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003208 struct held_lock *hlock;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003209 unsigned int depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003210 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003211 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003212 u64 chain_key;
3213
3214 if (unlikely(!debug_locks))
3215 return 0;
3216
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003217 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3218 check = 0;
Peter Zijlstra1704f472010-03-19 01:37:42 +01003219
Hitoshi Mitake62016252010-10-05 18:01:51 +09003220 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3221 class = lock->class_cache[subclass];
Ingo Molnard6d897c2006-07-10 04:44:04 -07003222 /*
Hitoshi Mitake62016252010-10-05 18:01:51 +09003223 * Not cached?
Ingo Molnard6d897c2006-07-10 04:44:04 -07003224 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003225 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003226 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003227 if (!class)
3228 return 0;
3229 }
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003230 atomic_inc((atomic_t *)&class->ops);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003231 if (very_verbose(class)) {
Borislav Petkov04860d42018-02-26 14:49:26 +01003232 printk("\nacquire class [%px] %s", class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003233 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01003234 printk(KERN_CONT "#%d", class->name_version);
3235 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003236 dump_stack();
3237 }
3238
3239 /*
3240 * Add the lock to the list of currently held locks.
3241 * (we dont increase the depth just yet, up until the
3242 * dependency checks are done)
3243 */
3244 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003245 /*
3246 * Ran out of static storage for our per-task lock stack again have we?
3247 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003248 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3249 return 0;
3250
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003251 class_idx = class - lock_classes + 1;
3252
Ingo Molnare966eae2017-12-12 12:31:16 +01003253 if (depth) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003254 hlock = curr->held_locks + depth - 1;
3255 if (hlock->class_idx == class_idx && nest_lock) {
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003256 if (hlock->references) {
3257 /*
3258 * Check: unsigned int references:12, overflow.
3259 */
3260 if (DEBUG_LOCKS_WARN_ON(hlock->references == (1 << 12)-1))
3261 return 0;
3262
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003263 hlock->references++;
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003264 } else {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003265 hlock->references = 2;
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003266 }
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003267
3268 return 1;
3269 }
3270 }
3271
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003272 hlock = curr->held_locks + depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003273 /*
3274 * Plain impossible, we just registered it and checked it weren't no
3275 * NULL like.. I bet this mushroom I ate was good!
3276 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003277 if (DEBUG_LOCKS_WARN_ON(!class))
3278 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003279 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003280 hlock->acquire_ip = ip;
3281 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003282 hlock->nest_lock = nest_lock;
Boqun Fengc2469752016-02-16 13:57:40 +08003283 hlock->irq_context = task_irq_context(curr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003284 hlock->trylock = trylock;
3285 hlock->read = read;
3286 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04003287 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003288 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003289#ifdef CONFIG_LOCK_STAT
3290 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003291 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003292#endif
Peter Zijlstra21199f22015-09-16 16:10:40 +02003293 hlock->pin_count = pin_count;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003294
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003295 if (check && !mark_irqflags(curr, hlock))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003296 return 0;
3297
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003298 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07003299 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003300 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003301
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003302 /*
Gautham R Shenoy17aacfb92007-10-28 20:47:01 +01003303 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003304 * lock keys along the dependency chain. We save the hash value
3305 * at every step so that we can get the current hash easily
3306 * after unlock. The chain hash is then used to cache dependency
3307 * results.
3308 *
3309 * The 'key ID' is what is the most compact key value to drive
3310 * the hash, not class->key.
3311 */
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003312 /*
3313 * Whoops, we did it again.. ran straight out of our static allocation.
3314 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003315 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003316 return 0;
3317
3318 chain_key = curr->curr_chain_key;
3319 if (!depth) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003320 /*
3321 * How can we have a chain hash when we ain't got no keys?!
3322 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003323 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3324 return 0;
3325 chain_head = 1;
3326 }
3327
3328 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003329 if (separate_irq_context(curr, hlock)) {
3330 chain_key = 0;
3331 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003332 }
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003333 chain_key = iterate_chain_key(chain_key, class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003334
Peter Zijlstraf8319482016-11-30 14:32:25 +11003335 if (nest_lock && !__lock_is_held(nest_lock, -1))
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003336 return print_lock_nested_lock_not_held(curr, hlock, ip);
3337
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003338 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003339 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08003340
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003341 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003342 curr->lockdep_depth++;
3343 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08003344#ifdef CONFIG_DEBUG_LOCKDEP
3345 if (unlikely(!debug_locks))
3346 return 0;
3347#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003348 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3349 debug_locks_off();
Dave Jones2c522832013-04-25 13:40:02 -04003350 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3351 printk(KERN_DEBUG "depth: %i max: %lu!\n",
Ben Greearc0540602013-02-06 10:56:19 -08003352 curr->lockdep_depth, MAX_LOCK_DEPTH);
Ben Greearc0540602013-02-06 10:56:19 -08003353
3354 lockdep_print_held_locks(current);
3355 debug_show_all_locks();
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01003356 dump_stack();
Ben Greearc0540602013-02-06 10:56:19 -08003357
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003358 return 0;
3359 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08003360
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003361 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3362 max_lockdep_depth = curr->lockdep_depth;
3363
3364 return 1;
3365}
3366
3367static int
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303368print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003369 unsigned long ip)
3370{
3371 if (!debug_locks_off())
3372 return 0;
3373 if (debug_locks_silent)
3374 return 0;
3375
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003376 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003377 pr_warn("=====================================\n");
3378 pr_warn("WARNING: bad unlock balance detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003379 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003380 pr_warn("-------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003381 pr_warn("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003382 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003383 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003384 pr_cont(") at:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003385 print_ip_sym(ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003386 pr_warn("but there are no more locks to release!\n");
3387 pr_warn("\nother info that might help us debug this:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003388 lockdep_print_held_locks(curr);
3389
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003390 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003391 dump_stack();
3392
3393 return 0;
3394}
3395
Matthew Wilcox08f36ff2018-01-17 07:14:13 -08003396static int match_held_lock(const struct held_lock *hlock,
3397 const struct lockdep_map *lock)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003398{
3399 if (hlock->instance == lock)
3400 return 1;
3401
3402 if (hlock->references) {
Matthew Wilcox08f36ff2018-01-17 07:14:13 -08003403 const struct lock_class *class = lock->class_cache[0];
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003404
3405 if (!class)
3406 class = look_up_lock_class(lock, 0);
3407
Peter Zijlstra80e04012011-08-05 14:26:17 +02003408 /*
3409 * If look_up_lock_class() failed to find a class, we're trying
3410 * to test if we hold a lock that has never yet been acquired.
3411 * Clearly if the lock hasn't been acquired _ever_, we're not
3412 * holding it either, so report failure.
3413 */
Matthew Wilcox64f29d12018-01-17 07:14:12 -08003414 if (!class)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003415 return 0;
3416
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003417 /*
3418 * References, but not a lock we're actually ref-counting?
3419 * State got messed up, follow the sites that change ->references
3420 * and try to make sense of it.
3421 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003422 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3423 return 0;
3424
3425 if (hlock->class_idx == class - lock_classes + 1)
3426 return 1;
3427 }
3428
3429 return 0;
3430}
3431
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003432/* @depth must not be zero */
3433static struct held_lock *find_held_lock(struct task_struct *curr,
3434 struct lockdep_map *lock,
3435 unsigned int depth, int *idx)
3436{
3437 struct held_lock *ret, *hlock, *prev_hlock;
3438 int i;
3439
3440 i = depth - 1;
3441 hlock = curr->held_locks + i;
3442 ret = hlock;
3443 if (match_held_lock(hlock, lock))
3444 goto out;
3445
3446 ret = NULL;
3447 for (i--, prev_hlock = hlock--;
3448 i >= 0;
3449 i--, prev_hlock = hlock--) {
3450 /*
3451 * We must not cross into another context:
3452 */
3453 if (prev_hlock->irq_context != hlock->irq_context) {
3454 ret = NULL;
3455 break;
3456 }
3457 if (match_held_lock(hlock, lock)) {
3458 ret = hlock;
3459 break;
3460 }
3461 }
3462
3463out:
3464 *idx = i;
3465 return ret;
3466}
3467
J. R. Okajimae9699702017-02-03 01:38:16 +09003468static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
3469 int idx)
3470{
3471 struct held_lock *hlock;
3472
Waiman Long8ee10862018-10-02 16:19:17 -04003473 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3474 return 0;
3475
J. R. Okajimae9699702017-02-03 01:38:16 +09003476 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
3477 if (!__lock_acquire(hlock->instance,
3478 hlock_class(hlock)->subclass,
3479 hlock->trylock,
3480 hlock->read, hlock->check,
3481 hlock->hardirqs_off,
3482 hlock->nest_lock, hlock->acquire_ip,
3483 hlock->references, hlock->pin_count))
3484 return 1;
3485 }
3486 return 0;
3487}
3488
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003489static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003490__lock_set_class(struct lockdep_map *lock, const char *name,
3491 struct lock_class_key *key, unsigned int subclass,
3492 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003493{
3494 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003495 struct held_lock *hlock;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003496 struct lock_class *class;
3497 unsigned int depth;
3498 int i;
3499
3500 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003501 /*
3502 * This function is about (re)setting the class of a held lock,
3503 * yet we're not actually holding any locks. Naughty user!
3504 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003505 if (DEBUG_LOCKS_WARN_ON(!depth))
3506 return 0;
3507
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003508 hlock = find_held_lock(curr, lock, depth, &i);
3509 if (!hlock)
3510 return print_unlock_imbalance_bug(curr, lock, ip);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003511
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003512 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003513 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02003514 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003515
3516 curr->lockdep_depth = i;
3517 curr->curr_chain_key = hlock->prev_chain_key;
3518
J. R. Okajimae9699702017-02-03 01:38:16 +09003519 if (reacquire_held_locks(curr, depth, i))
3520 return 0;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003521
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003522 /*
3523 * I took it apart and put it back together again, except now I have
3524 * these 'spare' parts.. where shall I put them.
3525 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003526 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3527 return 0;
3528 return 1;
3529}
3530
J. R. Okajima6419c4a2017-02-03 01:38:17 +09003531static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3532{
3533 struct task_struct *curr = current;
3534 struct held_lock *hlock;
3535 unsigned int depth;
3536 int i;
3537
3538 depth = curr->lockdep_depth;
3539 /*
3540 * This function is about (re)setting the class of a held lock,
3541 * yet we're not actually holding any locks. Naughty user!
3542 */
3543 if (DEBUG_LOCKS_WARN_ON(!depth))
3544 return 0;
3545
3546 hlock = find_held_lock(curr, lock, depth, &i);
3547 if (!hlock)
3548 return print_unlock_imbalance_bug(curr, lock, ip);
3549
3550 curr->lockdep_depth = i;
3551 curr->curr_chain_key = hlock->prev_chain_key;
3552
3553 WARN(hlock->read, "downgrading a read lock");
3554 hlock->read = 1;
3555 hlock->acquire_ip = ip;
3556
3557 if (reacquire_held_locks(curr, depth, i))
3558 return 0;
3559
3560 /*
3561 * I took it apart and put it back together again, except now I have
3562 * these 'spare' parts.. where shall I put them.
3563 */
3564 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3565 return 0;
3566 return 1;
3567}
3568
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003569/*
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003570 * Remove the lock to the list of currently held locks - this gets
3571 * called on mutex_unlock()/spin_unlock*() (or on a failed
3572 * mutex_lock_interruptible()).
3573 *
3574 * @nested is an hysterical artifact, needs a tree wide cleanup.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003575 */
3576static int
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003577__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003578{
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003579 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003580 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003581 unsigned int depth;
Ingo Molnare966eae2017-12-12 12:31:16 +01003582 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003583
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003584 if (unlikely(!debug_locks))
3585 return 0;
3586
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003587 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003588 /*
3589 * So we're all set to release this lock.. wait what lock? We don't
3590 * own any locks, you've been drinking again?
3591 */
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003592 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3593 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003594
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003595 /*
3596 * Check whether the lock exists in the current stack
3597 * of held locks:
3598 */
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003599 hlock = find_held_lock(curr, lock, depth, &i);
3600 if (!hlock)
3601 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003602
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003603 if (hlock->instance == lock)
3604 lock_release_holdtime(hlock);
3605
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003606 WARN(hlock->pin_count, "releasing a pinned lock\n");
3607
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003608 if (hlock->references) {
3609 hlock->references--;
3610 if (hlock->references) {
3611 /*
3612 * We had, and after removing one, still have
3613 * references, the current lock stack is still
3614 * valid. We're done!
3615 */
3616 return 1;
3617 }
3618 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003619
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003620 /*
3621 * We have the right lock to unlock, 'hlock' points to it.
3622 * Now we remove it from the stack, and add back the other
3623 * entries (if any), recalculating the hash along the way:
3624 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003625
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003626 curr->lockdep_depth = i;
3627 curr->curr_chain_key = hlock->prev_chain_key;
3628
Waiman Longce52a182018-10-02 16:19:18 -04003629 /*
3630 * The most likely case is when the unlock is on the innermost
3631 * lock. In this case, we are done!
3632 */
3633 if (i == depth-1)
3634 return 1;
3635
J. R. Okajimae9699702017-02-03 01:38:16 +09003636 if (reacquire_held_locks(curr, depth, i + 1))
3637 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003638
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003639 /*
3640 * We had N bottles of beer on the wall, we drank one, but now
3641 * there's not N-1 bottles of beer left on the wall...
3642 */
Waiman Longce52a182018-10-02 16:19:18 -04003643 DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth-1);
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003644
Waiman Longce52a182018-10-02 16:19:18 -04003645 /*
3646 * Since reacquire_held_locks() would have called check_chain_key()
3647 * indirectly via __lock_acquire(), we don't need to do it again
3648 * on return.
3649 */
3650 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003651}
3652
Matthew Wilcox08f36ff2018-01-17 07:14:13 -08003653static int __lock_is_held(const struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02003654{
3655 struct task_struct *curr = current;
3656 int i;
3657
3658 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003659 struct held_lock *hlock = curr->held_locks + i;
3660
Peter Zijlstraf8319482016-11-30 14:32:25 +11003661 if (match_held_lock(hlock, lock)) {
3662 if (read == -1 || hlock->read == read)
3663 return 1;
3664
3665 return 0;
3666 }
Peter Zijlstraf607c662009-07-20 19:16:29 +02003667 }
3668
3669 return 0;
3670}
3671
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003672static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
3673{
3674 struct pin_cookie cookie = NIL_COOKIE;
3675 struct task_struct *curr = current;
3676 int i;
3677
3678 if (unlikely(!debug_locks))
3679 return cookie;
3680
3681 for (i = 0; i < curr->lockdep_depth; i++) {
3682 struct held_lock *hlock = curr->held_locks + i;
3683
3684 if (match_held_lock(hlock, lock)) {
3685 /*
3686 * Grab 16bits of randomness; this is sufficient to not
3687 * be guessable and still allows some pin nesting in
3688 * our u32 pin_count.
3689 */
3690 cookie.val = 1 + (prandom_u32() >> 16);
3691 hlock->pin_count += cookie.val;
3692 return cookie;
3693 }
3694 }
3695
3696 WARN(1, "pinning an unheld lock\n");
3697 return cookie;
3698}
3699
3700static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003701{
3702 struct task_struct *curr = current;
3703 int i;
3704
3705 if (unlikely(!debug_locks))
3706 return;
3707
3708 for (i = 0; i < curr->lockdep_depth; i++) {
3709 struct held_lock *hlock = curr->held_locks + i;
3710
3711 if (match_held_lock(hlock, lock)) {
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003712 hlock->pin_count += cookie.val;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003713 return;
3714 }
3715 }
3716
3717 WARN(1, "pinning an unheld lock\n");
3718}
3719
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003720static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003721{
3722 struct task_struct *curr = current;
3723 int i;
3724
3725 if (unlikely(!debug_locks))
3726 return;
3727
3728 for (i = 0; i < curr->lockdep_depth; i++) {
3729 struct held_lock *hlock = curr->held_locks + i;
3730
3731 if (match_held_lock(hlock, lock)) {
3732 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
3733 return;
3734
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003735 hlock->pin_count -= cookie.val;
3736
3737 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
3738 hlock->pin_count = 0;
3739
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003740 return;
3741 }
3742 }
3743
3744 WARN(1, "unpinning an unheld lock\n");
3745}
3746
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003747/*
3748 * Check whether we follow the irq-flags state precisely:
3749 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003750static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003751{
Ingo Molnar992860e2008-07-14 10:28:38 +02003752#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3753 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003754 if (!debug_locks)
3755 return;
3756
Ingo Molnar5f9fa8a62007-12-07 19:02:47 +01003757 if (irqs_disabled_flags(flags)) {
3758 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3759 printk("possible reason: unannotated irqs-off.\n");
3760 }
3761 } else {
3762 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3763 printk("possible reason: unannotated irqs-on.\n");
3764 }
3765 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003766
3767 /*
3768 * We dont accurately track softirq state in e.g.
3769 * hardirq contexts (such as on 4KSTACKS), so only
3770 * check if not in hardirq contexts:
3771 */
3772 if (!hardirq_count()) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003773 if (softirq_count()) {
3774 /* like the above, but with softirqs */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003775 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003776 } else {
3777 /* lick the above, does it taste good? */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003778 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003779 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003780 }
3781
3782 if (!debug_locks)
3783 print_irqtrace_events(current);
3784#endif
3785}
3786
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003787void lock_set_class(struct lockdep_map *lock, const char *name,
3788 struct lock_class_key *key, unsigned int subclass,
3789 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003790{
3791 unsigned long flags;
3792
3793 if (unlikely(current->lockdep_recursion))
3794 return;
3795
3796 raw_local_irq_save(flags);
3797 current->lockdep_recursion = 1;
3798 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003799 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003800 check_chain_key(current);
3801 current->lockdep_recursion = 0;
3802 raw_local_irq_restore(flags);
3803}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003804EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003805
J. R. Okajima6419c4a2017-02-03 01:38:17 +09003806void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3807{
3808 unsigned long flags;
3809
3810 if (unlikely(current->lockdep_recursion))
3811 return;
3812
3813 raw_local_irq_save(flags);
3814 current->lockdep_recursion = 1;
3815 check_flags(flags);
3816 if (__lock_downgrade(lock, ip))
3817 check_chain_key(current);
3818 current->lockdep_recursion = 0;
3819 raw_local_irq_restore(flags);
3820}
3821EXPORT_SYMBOL_GPL(lock_downgrade);
3822
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003823/*
3824 * We are not always called with irqs disabled - do that here,
3825 * and also avoid lockdep recursion:
3826 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003827void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003828 int trylock, int read, int check,
3829 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003830{
3831 unsigned long flags;
3832
3833 if (unlikely(current->lockdep_recursion))
3834 return;
3835
3836 raw_local_irq_save(flags);
3837 check_flags(flags);
3838
3839 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01003840 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003841 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003842 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003843 current->lockdep_recursion = 0;
3844 raw_local_irq_restore(flags);
3845}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003846EXPORT_SYMBOL_GPL(lock_acquire);
3847
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003848void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02003849 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003850{
3851 unsigned long flags;
3852
3853 if (unlikely(current->lockdep_recursion))
3854 return;
3855
3856 raw_local_irq_save(flags);
3857 check_flags(flags);
3858 current->lockdep_recursion = 1;
Frederic Weisbecker93135432010-05-08 06:24:25 +02003859 trace_lock_release(lock, ip);
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003860 if (__lock_release(lock, nested, ip))
3861 check_chain_key(current);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003862 current->lockdep_recursion = 0;
3863 raw_local_irq_restore(flags);
3864}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003865EXPORT_SYMBOL_GPL(lock_release);
3866
Matthew Wilcox08f36ff2018-01-17 07:14:13 -08003867int lock_is_held_type(const struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02003868{
3869 unsigned long flags;
3870 int ret = 0;
3871
3872 if (unlikely(current->lockdep_recursion))
Peter Zijlstraf2513cd2011-06-06 12:32:43 +02003873 return 1; /* avoid false negative lockdep_assert_held() */
Peter Zijlstraf607c662009-07-20 19:16:29 +02003874
3875 raw_local_irq_save(flags);
3876 check_flags(flags);
3877
3878 current->lockdep_recursion = 1;
Peter Zijlstraf8319482016-11-30 14:32:25 +11003879 ret = __lock_is_held(lock, read);
Peter Zijlstraf607c662009-07-20 19:16:29 +02003880 current->lockdep_recursion = 0;
3881 raw_local_irq_restore(flags);
3882
3883 return ret;
3884}
Peter Zijlstraf8319482016-11-30 14:32:25 +11003885EXPORT_SYMBOL_GPL(lock_is_held_type);
Peter Zijlstraf607c662009-07-20 19:16:29 +02003886
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003887struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003888{
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003889 struct pin_cookie cookie = NIL_COOKIE;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003890 unsigned long flags;
3891
3892 if (unlikely(current->lockdep_recursion))
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003893 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003894
3895 raw_local_irq_save(flags);
3896 check_flags(flags);
3897
3898 current->lockdep_recursion = 1;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003899 cookie = __lock_pin_lock(lock);
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003900 current->lockdep_recursion = 0;
3901 raw_local_irq_restore(flags);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003902
3903 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003904}
3905EXPORT_SYMBOL_GPL(lock_pin_lock);
3906
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003907void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003908{
3909 unsigned long flags;
3910
3911 if (unlikely(current->lockdep_recursion))
3912 return;
3913
3914 raw_local_irq_save(flags);
3915 check_flags(flags);
3916
3917 current->lockdep_recursion = 1;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02003918 __lock_repin_lock(lock, cookie);
3919 current->lockdep_recursion = 0;
3920 raw_local_irq_restore(flags);
3921}
3922EXPORT_SYMBOL_GPL(lock_repin_lock);
3923
3924void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3925{
3926 unsigned long flags;
3927
3928 if (unlikely(current->lockdep_recursion))
3929 return;
3930
3931 raw_local_irq_save(flags);
3932 check_flags(flags);
3933
3934 current->lockdep_recursion = 1;
3935 __lock_unpin_lock(lock, cookie);
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003936 current->lockdep_recursion = 0;
3937 raw_local_irq_restore(flags);
3938}
3939EXPORT_SYMBOL_GPL(lock_unpin_lock);
3940
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003941#ifdef CONFIG_LOCK_STAT
3942static int
3943print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3944 unsigned long ip)
3945{
3946 if (!debug_locks_off())
3947 return 0;
3948 if (debug_locks_silent)
3949 return 0;
3950
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003951 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003952 pr_warn("=================================\n");
3953 pr_warn("WARNING: bad contention detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003954 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003955 pr_warn("---------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003956 pr_warn("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003957 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003958 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003959 pr_cont(") at:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003960 print_ip_sym(ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003961 pr_warn("but there are no locks held!\n");
3962 pr_warn("\nother info that might help us debug this:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003963 lockdep_print_held_locks(curr);
3964
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003965 pr_warn("\nstack backtrace:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003966 dump_stack();
3967
3968 return 0;
3969}
3970
3971static void
3972__lock_contended(struct lockdep_map *lock, unsigned long ip)
3973{
3974 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003975 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003976 struct lock_class_stats *stats;
3977 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003978 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003979
3980 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003981 /*
3982 * Whee, we contended on this lock, except it seems we're not
3983 * actually trying to acquire anything much at all..
3984 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003985 if (DEBUG_LOCKS_WARN_ON(!depth))
3986 return;
3987
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003988 hlock = find_held_lock(curr, lock, depth, &i);
3989 if (!hlock) {
3990 print_lock_contention_bug(curr, lock, ip);
3991 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003992 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003993
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003994 if (hlock->instance != lock)
3995 return;
3996
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003997 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003998
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02003999 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
4000 contending_point = lock_point(hlock_class(hlock)->contending_point,
4001 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004002
Dave Jonesf82b2172008-08-11 09:30:23 +02004003 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004004 if (contention_point < LOCKSTAT_POINTS)
4005 stats->contention_point[contention_point]++;
4006 if (contending_point < LOCKSTAT_POINTS)
4007 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07004008 if (lock->cpu != smp_processor_id())
4009 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004010}
4011
4012static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004013__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004014{
4015 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004016 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004017 struct lock_class_stats *stats;
4018 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004019 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07004020 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004021
4022 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004023 /*
4024 * Yay, we acquired ownership of this lock we didn't try to
4025 * acquire, how the heck did that happen?
4026 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004027 if (DEBUG_LOCKS_WARN_ON(!depth))
4028 return;
4029
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004030 hlock = find_held_lock(curr, lock, depth, &i);
4031 if (!hlock) {
4032 print_lock_contention_bug(curr, lock, _RET_IP_);
4033 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004034 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004035
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004036 if (hlock->instance != lock)
4037 return;
4038
Peter Zijlstra96645672007-07-19 01:49:00 -07004039 cpu = smp_processor_id();
4040 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004041 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07004042 waittime = now - hlock->waittime_stamp;
4043 hlock->holdtime_stamp = now;
4044 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004045
Frederic Weisbecker883a2a32010-05-08 06:16:11 +02004046 trace_lock_acquired(lock, ip);
Frederic Weisbecker20625012009-04-06 01:49:33 +02004047
Dave Jonesf82b2172008-08-11 09:30:23 +02004048 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07004049 if (waittime) {
4050 if (hlock->read)
4051 lock_time_inc(&stats->read_waittime, waittime);
4052 else
4053 lock_time_inc(&stats->write_waittime, waittime);
4054 }
4055 if (lock->cpu != cpu)
4056 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07004057
4058 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004059 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004060}
4061
4062void lock_contended(struct lockdep_map *lock, unsigned long ip)
4063{
4064 unsigned long flags;
4065
4066 if (unlikely(!lock_stat))
4067 return;
4068
4069 if (unlikely(current->lockdep_recursion))
4070 return;
4071
4072 raw_local_irq_save(flags);
4073 check_flags(flags);
4074 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01004075 trace_lock_contended(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004076 __lock_contended(lock, ip);
4077 current->lockdep_recursion = 0;
4078 raw_local_irq_restore(flags);
4079}
4080EXPORT_SYMBOL_GPL(lock_contended);
4081
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004082void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004083{
4084 unsigned long flags;
4085
4086 if (unlikely(!lock_stat))
4087 return;
4088
4089 if (unlikely(current->lockdep_recursion))
4090 return;
4091
4092 raw_local_irq_save(flags);
4093 check_flags(flags);
4094 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004095 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004096 current->lockdep_recursion = 0;
4097 raw_local_irq_restore(flags);
4098}
4099EXPORT_SYMBOL_GPL(lock_acquired);
4100#endif
4101
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004102/*
4103 * Used by the testsuite, sanitize the validator state
4104 * after a simulated failure:
4105 */
4106
4107void lockdep_reset(void)
4108{
4109 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08004110 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004111
4112 raw_local_irq_save(flags);
4113 current->curr_chain_key = 0;
4114 current->lockdep_depth = 0;
4115 current->lockdep_recursion = 0;
4116 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
4117 nr_hardirq_chains = 0;
4118 nr_softirq_chains = 0;
4119 nr_process_chains = 0;
4120 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08004121 for (i = 0; i < CHAINHASH_SIZE; i++)
Andrew Mortona63f38c2016-02-03 13:44:12 -08004122 INIT_HLIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004123 raw_local_irq_restore(flags);
4124}
4125
4126static void zap_class(struct lock_class *class)
4127{
4128 int i;
4129
4130 /*
4131 * Remove all dependencies this lock is
4132 * involved in:
4133 */
4134 for (i = 0; i < nr_list_entries; i++) {
4135 if (list_entries[i].class == class)
4136 list_del_rcu(&list_entries[i].entry);
4137 }
4138 /*
4139 * Unhash the class and remove it from the all_lock_classes list:
4140 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08004141 hlist_del_rcu(&class->hash_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004142 list_del_rcu(&class->lock_entry);
4143
Peter Zijlstracee34d82015-06-02 12:50:13 +02004144 RCU_INIT_POINTER(class->key, NULL);
4145 RCU_INIT_POINTER(class->name, NULL);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004146}
4147
Arjan van de Venfabe8742008-01-24 07:00:45 +01004148static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004149{
4150 return addr >= start && addr < start + size;
4151}
4152
Peter Zijlstra35a93932015-02-26 16:23:11 +01004153/*
4154 * Used in module.c to remove lock classes from memory that is going to be
4155 * freed; and possibly re-used by other modules.
4156 *
4157 * We will have had one sync_sched() before getting here, so we're guaranteed
4158 * nobody will look up these exact classes -- they're properly dead but still
4159 * allocated.
4160 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004161void lockdep_free_key_range(void *start, unsigned long size)
4162{
Peter Zijlstra35a93932015-02-26 16:23:11 +01004163 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004164 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004165 unsigned long flags;
4166 int i;
Nick Piggin5a26db52008-01-16 09:51:58 +01004167 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004168
4169 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01004170 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004171
4172 /*
4173 * Unhash all classes that were created by this module:
4174 */
4175 for (i = 0; i < CLASSHASH_SIZE; i++) {
4176 head = classhash_table + i;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004177 hlist_for_each_entry_rcu(class, head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004178 if (within(class->key, start, size))
4179 zap_class(class);
Arjan van de Venfabe8742008-01-24 07:00:45 +01004180 else if (within(class->name, start, size))
4181 zap_class(class);
4182 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004183 }
4184
Nick Piggin5a26db52008-01-16 09:51:58 +01004185 if (locked)
4186 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004187 raw_local_irq_restore(flags);
Peter Zijlstra35a93932015-02-26 16:23:11 +01004188
4189 /*
4190 * Wait for any possible iterators from look_up_lock_class() to pass
4191 * before continuing to free the memory they refer to.
4192 *
4193 * sync_sched() is sufficient because the read-side is IRQ disable.
4194 */
4195 synchronize_sched();
4196
4197 /*
4198 * XXX at this point we could return the resources to the pool;
4199 * instead we leak them. We would need to change to bitmap allocators
4200 * instead of the linear allocators we have now.
4201 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004202}
4203
4204void lockdep_reset_lock(struct lockdep_map *lock)
4205{
Peter Zijlstra35a93932015-02-26 16:23:11 +01004206 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004207 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004208 unsigned long flags;
4209 int i, j;
Nick Piggin5a26db52008-01-16 09:51:58 +01004210 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004211
4212 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004213
4214 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07004215 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004216 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07004217 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
4218 /*
4219 * If the class exists we look it up and zap it:
4220 */
4221 class = look_up_lock_class(lock, j);
Matthew Wilcox64f29d12018-01-17 07:14:12 -08004222 if (class)
Ingo Molnard6d897c2006-07-10 04:44:04 -07004223 zap_class(class);
4224 }
4225 /*
4226 * Debug check: in the end all mapped classes should
4227 * be gone.
4228 */
Nick Piggin5a26db52008-01-16 09:51:58 +01004229 locked = graph_lock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004230 for (i = 0; i < CLASSHASH_SIZE; i++) {
4231 head = classhash_table + i;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004232 hlist_for_each_entry_rcu(class, head, hash_entry) {
Hitoshi Mitake62016252010-10-05 18:01:51 +09004233 int match = 0;
4234
4235 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
4236 match |= class == lock->class_cache[j];
4237
4238 if (unlikely(match)) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004239 if (debug_locks_off_graph_unlock()) {
4240 /*
4241 * We all just reset everything, how did it match?
4242 */
Ingo Molnar74c383f2006-12-13 00:34:43 -08004243 WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004244 }
Ingo Molnard6d897c2006-07-10 04:44:04 -07004245 goto out_restore;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004246 }
4247 }
4248 }
Nick Piggin5a26db52008-01-16 09:51:58 +01004249 if (locked)
4250 graph_unlock();
Ingo Molnard6d897c2006-07-10 04:44:04 -07004251
4252out_restore:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004253 raw_local_irq_restore(flags);
4254}
4255
Joel Fernandes (Google)c3bc8fd2018-07-30 15:24:23 -07004256void __init lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004257{
4258 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4259
Li Zefanb0788ca2008-11-21 15:57:32 +08004260 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004261 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4262 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08004263 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004264 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4265 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4266 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4267
4268 printk(" memory used by lock dependency info: %lu kB\n",
4269 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
4270 sizeof(struct list_head) * CLASSHASH_SIZE +
4271 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
4272 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
Ming Lei906292092009-08-02 21:43:36 +08004273 sizeof(struct list_head) * CHAINHASH_SIZE
Ming Lei4dd861d2009-07-16 15:44:29 +02004274#ifdef CONFIG_PROVE_LOCKING
Ming Leie351b662009-07-22 22:48:09 +08004275 + sizeof(struct circular_queue)
Ming Lei4dd861d2009-07-16 15:44:29 +02004276#endif
Ming Lei906292092009-08-02 21:43:36 +08004277 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02004278 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004279
4280 printk(" per task-struct memory footprint: %lu bytes\n",
4281 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004282}
4283
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004284static void
4285print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07004286 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004287{
4288 if (!debug_locks_off())
4289 return;
4290 if (debug_locks_silent)
4291 return;
4292
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004293 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004294 pr_warn("=========================\n");
4295 pr_warn("WARNING: held lock freed!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004296 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004297 pr_warn("-------------------------\n");
Borislav Petkov04860d42018-02-26 14:49:26 +01004298 pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004299 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07004300 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004301 lockdep_print_held_locks(curr);
4302
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004303 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004304 dump_stack();
4305}
4306
Oleg Nesterov54561782007-12-05 15:46:09 +01004307static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4308 const void* lock_from, unsigned long lock_len)
4309{
4310 return lock_from + lock_len <= mem_from ||
4311 mem_from + mem_len <= lock_from;
4312}
4313
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004314/*
4315 * Called when kernel memory is freed (or unmapped), or if a lock
4316 * is destroyed or reinitialized - this code checks whether there is
4317 * any held lock in the memory range of <from> to <to>:
4318 */
4319void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4320{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004321 struct task_struct *curr = current;
4322 struct held_lock *hlock;
4323 unsigned long flags;
4324 int i;
4325
4326 if (unlikely(!debug_locks))
4327 return;
4328
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04004329 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004330 for (i = 0; i < curr->lockdep_depth; i++) {
4331 hlock = curr->held_locks + i;
4332
Oleg Nesterov54561782007-12-05 15:46:09 +01004333 if (not_in_range(mem_from, mem_len, hlock->instance,
4334 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004335 continue;
4336
Oleg Nesterov54561782007-12-05 15:46:09 +01004337 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004338 break;
4339 }
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04004340 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004341}
Peter Zijlstraed075362006-12-06 20:35:24 -08004342EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004343
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004344static void print_held_locks_bug(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004345{
4346 if (!debug_locks_off())
4347 return;
4348 if (debug_locks_silent)
4349 return;
4350
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004351 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004352 pr_warn("====================================\n");
4353 pr_warn("WARNING: %s/%d still has locks held!\n",
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004354 current->comm, task_pid_nr(current));
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004355 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004356 pr_warn("------------------------------------\n");
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004357 lockdep_print_held_locks(current);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004358 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004359 dump_stack();
4360}
4361
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004362void debug_check_no_locks_held(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004363{
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004364 if (unlikely(current->lockdep_depth > 0))
4365 print_held_locks_bug();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004366}
Colin Cross1b1d2fb2013-05-06 23:50:08 +00004367EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004368
Sasha Levin8dce7a92013-06-13 18:41:16 -04004369#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004370void debug_show_all_locks(void)
4371{
4372 struct task_struct *g, *p;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004373
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004374 if (unlikely(!debug_locks)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004375 pr_warn("INFO: lockdep is turned off.\n");
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004376 return;
4377 }
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004378 pr_warn("\nShowing all locks held in the system:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004379
Tetsuo Handa0f736a52018-04-06 19:41:18 +09004380 rcu_read_lock();
4381 for_each_process_thread(g, p) {
Tetsuo Handa0f736a52018-04-06 19:41:18 +09004382 if (!p->lockdep_depth)
4383 continue;
4384 lockdep_print_held_locks(p);
Tejun Heo88f1c872018-01-22 14:00:55 -08004385 touch_nmi_watchdog();
Tetsuo Handa0f736a52018-04-06 19:41:18 +09004386 touch_all_softlockup_watchdogs();
4387 }
4388 rcu_read_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004389
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004390 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004391 pr_warn("=============================================\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004392}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004393EXPORT_SYMBOL_GPL(debug_show_all_locks);
Sasha Levin8dce7a92013-06-13 18:41:16 -04004394#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004395
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01004396/*
4397 * Careful: only use this function if you are sure that
4398 * the task cannot run in parallel!
4399 */
John Kacurf1b499f2010-08-05 17:10:53 +02004400void debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004401{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08004402 if (unlikely(!debug_locks)) {
4403 printk("INFO: lockdep is turned off.\n");
4404 return;
4405 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004406 lockdep_print_held_locks(task);
4407}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004408EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02004409
Andi Kleen722a9f92014-05-02 00:44:38 +02004410asmlinkage __visible void lockdep_sys_exit(void)
Peter Zijlstrab351d162007-10-11 22:11:12 +02004411{
4412 struct task_struct *curr = current;
4413
4414 if (unlikely(curr->lockdep_depth)) {
4415 if (!debug_locks_off())
4416 return;
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004417 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004418 pr_warn("================================================\n");
4419 pr_warn("WARNING: lock held when returning to user space!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004420 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004421 pr_warn("------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004422 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
Peter Zijlstrab351d162007-10-11 22:11:12 +02004423 curr->comm, curr->pid);
4424 lockdep_print_held_locks(curr);
4425 }
Byungchul Parkb09be672017-08-07 16:12:52 +09004426
4427 /*
4428 * The lock history for each syscall should be independent. So wipe the
4429 * slate clean on return to userspace.
4430 */
Peter Zijlstraf52be572017-08-29 10:59:39 +02004431 lockdep_invariant_state(false);
Peter Zijlstrab351d162007-10-11 22:11:12 +02004432}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004433
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004434void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004435{
4436 struct task_struct *curr = current;
4437
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08004438 /* Note: the following can be executed concurrently, so be careful. */
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004439 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004440 pr_warn("=============================\n");
4441 pr_warn("WARNING: suspicious RCU usage\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004442 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004443 pr_warn("-----------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004444 pr_warn("%s:%d %s!\n", file, line, s);
4445 pr_warn("\nother info that might help us debug this:\n\n");
4446 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004447 !rcu_lockdep_current_cpu_online()
4448 ? "RCU used illegally from offline CPU!\n"
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004449 : !rcu_is_watching()
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08004450 ? "RCU used illegally from idle CPU!\n"
4451 : "",
4452 rcu_scheduler_active, debug_locks);
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004453
4454 /*
4455 * If a CPU is in the RCU-free window in idle (ie: in the section
4456 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4457 * considers that CPU to be in an "extended quiescent state",
4458 * which means that RCU will be completely ignoring that CPU.
4459 * Therefore, rcu_read_lock() and friends have absolutely no
4460 * effect on a CPU running in that state. In other words, even if
4461 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4462 * delete data structures out from under it. RCU really has no
4463 * choice here: we need to keep an RCU-free window in idle where
4464 * the CPU may possibly enter into low power mode. This way we can
4465 * notice an extended quiescent state to other CPUs that started a grace
4466 * period. Otherwise we would delay any grace period as long as we run
4467 * in the idle task.
4468 *
4469 * So complain bitterly if someone does call rcu_read_lock(),
4470 * rcu_read_lock_bh() and so on from extended quiescent states.
4471 */
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07004472 if (!rcu_is_watching())
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004473 pr_warn("RCU used illegally from extended quiescent state!\n");
Frederic Weisbecker0464e932011-10-07 18:22:01 +02004474
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004475 lockdep_print_held_locks(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004476 pr_warn("\nstack backtrace:\n");
Paul E. McKenney0632eb32010-02-22 17:04:47 -08004477 dump_stack();
4478}
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004479EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);