blob: 2288aa2fa4c68f6c532d99e0ad4ef2ededc72766 [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>
Bart Van Asscheace35a72019-02-14 15:00:47 -080048#include <linux/bitmap.h>
Ming Leid588e462009-07-16 15:44:29 +020049#include <linux/bitops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/gfp.h>
Peter Zijlstrae7904a22015-08-01 19:25:08 +020051#include <linux/random.h>
Peter Zijlstradfaaf3f2016-05-30 18:31:33 +020052#include <linux/jhash.h>
Tejun Heo88f1c872018-01-22 14:00:55 -080053#include <linux/nmi.h>
Bart Van Asschea0b0fd52019-02-14 15:00:46 -080054#include <linux/rcupdate.h>
Masami Hiramatsu2f43c602019-02-13 01:15:05 +090055#include <linux/kprobes.h>
Peter Zijlstraaf012962009-07-16 15:44:29 +020056
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070057#include <asm/sections.h>
58
59#include "lockdep_internals.h"
60
Steven Rostedta8d154b2009-04-10 09:36:00 -040061#define CREATE_TRACE_POINTS
Frederic Weisbecker67178762009-11-13 10:06:34 +010062#include <trace/events/lock.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040063
Peter Zijlstraf20786f2007-07-19 01:48:56 -070064#ifdef CONFIG_PROVE_LOCKING
65int prove_locking = 1;
66module_param(prove_locking, int, 0644);
67#else
68#define prove_locking 0
69#endif
70
71#ifdef CONFIG_LOCK_STAT
72int lock_stat = 1;
73module_param(lock_stat, int, 0644);
74#else
75#define lock_stat 0
76#endif
77
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070078/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080079 * lockdep_lock: protects the lockdep graph, the hashes and the
80 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070081 *
82 * This is one of the rare exceptions where it's justified
83 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -080084 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070085 */
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010086static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Bart Van Asschecdc84d72019-02-14 15:00:44 -080087static struct task_struct *lockdep_selftest_task_struct;
Ingo Molnar74c383f2006-12-13 00:34:43 -080088
89static int graph_lock(void)
90{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010091 arch_spin_lock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -080092 /*
93 * Make sure that if another CPU detected a bug while
94 * walking the graph we dont change it (while the other
95 * CPU is busy printing out stuff with the graph lock
96 * dropped already)
97 */
98 if (!debug_locks) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010099 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800100 return 0;
101 }
Steven Rostedtbb065af2008-05-12 21:21:00 +0200102 /* prevent any recursions within lockdep from causing deadlocks */
103 current->lockdep_recursion++;
Ingo Molnar74c383f2006-12-13 00:34:43 -0800104 return 1;
105}
106
107static inline int graph_unlock(void)
108{
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200109 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
110 /*
111 * The lockdep graph lock isn't locked while we expect it to
112 * be, we're confused now, bye!
113 */
Jarek Poplawski381a2292007-02-10 01:44:58 -0800114 return DEBUG_LOCKS_WARN_ON(1);
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200115 }
Jarek Poplawski381a2292007-02-10 01:44:58 -0800116
Steven Rostedtbb065af2008-05-12 21:21:00 +0200117 current->lockdep_recursion--;
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100118 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800119 return 0;
120}
121
122/*
123 * Turn lock debugging off and return with 0 if it was off already,
124 * and also release the graph lock:
125 */
126static inline int debug_locks_off_graph_unlock(void)
127{
128 int ret = debug_locks_off();
129
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100130 arch_spin_unlock(&lockdep_lock);
Ingo Molnar74c383f2006-12-13 00:34:43 -0800131
132 return ret;
133}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700134
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700135unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200136static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Bart Van Asscheace35a72019-02-14 15:00:47 -0800137static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700138
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700139/*
140 * All data structures here are protected by the global debug_lock.
141 *
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800142 * nr_lock_classes is the number of elements of lock_classes[] that is
143 * in use.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700144 */
Bart Van Assche108c1482019-02-14 15:00:53 -0800145#define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
146#define KEYHASH_SIZE (1UL << KEYHASH_BITS)
147static struct hlist_head lock_keys_hash[KEYHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700148unsigned long nr_lock_classes;
Bart Van Assche1431a5d2018-12-06 17:11:32 -0800149#ifndef CONFIG_DEBUG_LOCKDEP
150static
151#endif
Waiman Long8ca2b56c2018-10-03 13:07:18 -0400152struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700153
Dave Jonesf82b2172008-08-11 09:30:23 +0200154static inline struct lock_class *hlock_class(struct held_lock *hlock)
155{
156 if (!hlock->class_idx) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200157 /*
158 * Someone passed in garbage, we give up.
159 */
Dave Jonesf82b2172008-08-11 09:30:23 +0200160 DEBUG_LOCKS_WARN_ON(1);
161 return NULL;
162 }
163 return lock_classes + hlock->class_idx - 1;
164}
165
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700166#ifdef CONFIG_LOCK_STAT
Peter Zijlstra25528212016-03-15 14:52:49 -0700167static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700168
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200169static inline u64 lockstat_clock(void)
170{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200171 return local_clock();
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200172}
173
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200174static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700175{
176 int i;
177
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200178 for (i = 0; i < LOCKSTAT_POINTS; i++) {
179 if (points[i] == 0) {
180 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700181 break;
182 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200183 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700184 break;
185 }
186
187 return i;
188}
189
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200190static void lock_time_inc(struct lock_time *lt, u64 time)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700191{
192 if (time > lt->max)
193 lt->max = time;
194
Frank Rowand109d71c2009-11-19 13:42:06 -0800195 if (time < lt->min || !lt->nr)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700196 lt->min = time;
197
198 lt->total += time;
199 lt->nr++;
200}
201
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700202static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
203{
Frank Rowand109d71c2009-11-19 13:42:06 -0800204 if (!src->nr)
205 return;
206
207 if (src->max > dst->max)
208 dst->max = src->max;
209
210 if (src->min < dst->min || !dst->nr)
211 dst->min = src->min;
212
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700213 dst->total += src->total;
214 dst->nr += src->nr;
215}
216
217struct lock_class_stats lock_stats(struct lock_class *class)
218{
219 struct lock_class_stats stats;
220 int cpu, i;
221
222 memset(&stats, 0, sizeof(struct lock_class_stats));
223 for_each_possible_cpu(cpu) {
224 struct lock_class_stats *pcs =
Tejun Heo1871e522009-10-29 22:34:13 +0900225 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700226
227 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
228 stats.contention_point[i] += pcs->contention_point[i];
229
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200230 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
231 stats.contending_point[i] += pcs->contending_point[i];
232
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700233 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
234 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
235
236 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
237 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700238
239 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
240 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700241 }
242
243 return stats;
244}
245
246void clear_lock_stats(struct lock_class *class)
247{
248 int cpu;
249
250 for_each_possible_cpu(cpu) {
251 struct lock_class_stats *cpu_stats =
Tejun Heo1871e522009-10-29 22:34:13 +0900252 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700253
254 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
255 }
256 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200257 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700258}
259
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700260static struct lock_class_stats *get_lock_stats(struct lock_class *class)
261{
Joel Fernandes (Google)01f38492018-07-30 15:24:21 -0700262 return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes];
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700263}
264
265static void lock_release_holdtime(struct held_lock *hlock)
266{
267 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200268 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700269
270 if (!lock_stat)
271 return;
272
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200273 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700274
Dave Jonesf82b2172008-08-11 09:30:23 +0200275 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700276 if (hlock->read)
277 lock_time_inc(&stats->read_holdtime, holdtime);
278 else
279 lock_time_inc(&stats->write_holdtime, holdtime);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700280}
281#else
282static inline void lock_release_holdtime(struct held_lock *hlock)
283{
284}
285#endif
286
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700287/*
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800288 * We keep a global list of all lock classes. The list is only accessed with
289 * the lockdep spinlock lock held. free_lock_classes is a list with free
290 * elements. These elements are linked together by the lock_entry member in
291 * struct lock_class.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700292 */
293LIST_HEAD(all_lock_classes);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800294static LIST_HEAD(free_lock_classes);
295
296/**
297 * struct pending_free - information about data structures about to be freed
298 * @zapped: Head of a list with struct lock_class elements.
Bart Van Asschede4643a2019-02-14 15:00:50 -0800299 * @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements
300 * are about to be freed.
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800301 */
302struct pending_free {
303 struct list_head zapped;
Bart Van Asschede4643a2019-02-14 15:00:50 -0800304 DECLARE_BITMAP(lock_chains_being_freed, MAX_LOCKDEP_CHAINS);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800305};
306
307/**
308 * struct delayed_free - data structures used for delayed freeing
309 *
310 * A data structure for delayed freeing of data structures that may be
311 * accessed by RCU readers at the time these were freed.
312 *
313 * @rcu_head: Used to schedule an RCU callback for freeing data structures.
314 * @index: Index of @pf to which freed data structures are added.
315 * @scheduled: Whether or not an RCU callback has been scheduled.
316 * @pf: Array with information about data structures about to be freed.
317 */
318static struct delayed_free {
319 struct rcu_head rcu_head;
320 int index;
321 int scheduled;
322 struct pending_free pf[2];
323} delayed_free;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700324
325/*
326 * The lockdep classes are in a hash-table as well, for fast lookup:
327 */
328#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
329#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700330#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700331#define classhashentry(key) (classhash_table + __classhashfn((key)))
332
Andrew Mortona63f38c2016-02-03 13:44:12 -0800333static struct hlist_head classhash_table[CLASSHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700334
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700335/*
336 * We put the lock dependency chains into a hash-table as well, to cache
337 * their existence:
338 */
339#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
340#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700341#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700342#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
343
Andrew Mortona63f38c2016-02-03 13:44:12 -0800344static struct hlist_head chainhash_table[CHAINHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700345
346/*
347 * The hash key of the lock dependency chains is a hash itself too:
348 * it's a hash of all locks taken up to that lock, including that lock.
349 * It's a 64-bit hash, because it's important for the keys to be
350 * unique.
351 */
Peter Zijlstradfaaf3f2016-05-30 18:31:33 +0200352static inline u64 iterate_chain_key(u64 key, u32 idx)
353{
354 u32 k0 = key, k1 = key >> 32;
355
356 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
357
358 return k0 | (u64)k1 << 32;
359}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700360
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200361void lockdep_off(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700362{
363 current->lockdep_recursion++;
364}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700365EXPORT_SYMBOL(lockdep_off);
366
Steven Rostedt1d09daa2008-05-12 21:20:55 +0200367void lockdep_on(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700368{
369 current->lockdep_recursion--;
370}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700371EXPORT_SYMBOL(lockdep_on);
372
Bart Van Asschecdc84d72019-02-14 15:00:44 -0800373void lockdep_set_selftest_task(struct task_struct *task)
374{
375 lockdep_selftest_task_struct = task;
376}
377
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700378/*
379 * Debugging switches:
380 */
381
382#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800383#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700384
385#if VERBOSE
386# define HARDIRQ_VERBOSE 1
387# define SOFTIRQ_VERBOSE 1
388#else
389# define HARDIRQ_VERBOSE 0
390# define SOFTIRQ_VERBOSE 0
391#endif
392
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +0100393#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700394/*
395 * Quick filtering for interesting events:
396 */
397static int class_filter(struct lock_class *class)
398{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700399#if 0
400 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700401 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700402 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700403 return 1;
404 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700405 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700406 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700407#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800408 /* Filter everything else. 1 would be to allow everything else */
409 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700410}
411#endif
412
413static int verbose(struct lock_class *class)
414{
415#if VERBOSE
416 return class_filter(class);
417#endif
418 return 0;
419}
420
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700421/*
422 * Stack-trace: tightly packed array of stack backtrace
Ingo Molnar74c383f2006-12-13 00:34:43 -0800423 * addresses. Protected by the graph_lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700424 */
425unsigned long nr_stack_trace_entries;
426static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
427
Dave Jones2c522832013-04-25 13:40:02 -0400428static void print_lockdep_off(const char *bug_msg)
429{
430 printk(KERN_DEBUG "%s\n", bug_msg);
431 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200432#ifdef CONFIG_LOCK_STAT
Dave Jones2c522832013-04-25 13:40:02 -0400433 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200434#endif
Dave Jones2c522832013-04-25 13:40:02 -0400435}
436
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700437static int save_trace(struct stack_trace *trace)
438{
439 trace->nr_entries = 0;
440 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
441 trace->entries = stack_trace + nr_stack_trace_entries;
442
Andi Kleen5a1b3992006-09-26 10:52:34 +0200443 trace->skip = 3;
Andi Kleen5a1b3992006-09-26 10:52:34 +0200444
Christoph Hellwigab1b6f02007-05-08 00:23:29 -0700445 save_stack_trace(trace);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700446
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200447 /*
448 * Some daft arches put -1 at the end to indicate its a full trace.
449 *
450 * <rant> this is buggy anyway, since it takes a whole extra entry so a
451 * complete trace that maxes out the entries provided will be reported
452 * as incomplete, friggin useless </rant>
453 */
Luck, Tonyea5b41f2009-12-09 14:29:36 -0800454 if (trace->nr_entries != 0 &&
455 trace->entries[trace->nr_entries-1] == ULONG_MAX)
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200456 trace->nr_entries--;
457
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700458 trace->max_entries = trace->nr_entries;
459
460 nr_stack_trace_entries += trace->nr_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700461
Peter Zijlstra4f84f432009-07-20 15:27:04 +0200462 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800463 if (!debug_locks_off_graph_unlock())
464 return 0;
465
Dave Jones2c522832013-04-25 13:40:02 -0400466 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
Ingo Molnar74c383f2006-12-13 00:34:43 -0800467 dump_stack();
468
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700469 return 0;
470 }
471
472 return 1;
473}
474
475unsigned int nr_hardirq_chains;
476unsigned int nr_softirq_chains;
477unsigned int nr_process_chains;
478unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700479
480#ifdef CONFIG_DEBUG_LOCKDEP
481/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700482 * Various lockdep statistics:
483 */
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200484DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700485#endif
486
487/*
488 * Locking printouts:
489 */
490
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100491#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100492 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
493 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
494 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
495 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100496
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700497static const char *usage_str[] =
498{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100499#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
500#include "lockdep_states.h"
501#undef LOCKDEP_STATE
502 [LOCK_USED] = "INITIAL USE",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700503};
504
505const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
506{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700507 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700508}
509
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100510static inline unsigned long lock_flag(enum lock_usage_bit bit)
511{
512 return 1UL << bit;
513}
514
515static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
516{
517 char c = '.';
518
Frederic Weisbeckerc902a1e2019-04-02 18:02:42 +0200519 if (class->usage_mask & lock_flag(bit + LOCK_USAGE_DIR_MASK))
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100520 c = '+';
521 if (class->usage_mask & lock_flag(bit)) {
522 c = '-';
Frederic Weisbeckerc902a1e2019-04-02 18:02:42 +0200523 if (class->usage_mask & lock_flag(bit + LOCK_USAGE_DIR_MASK))
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100524 c = '?';
525 }
526
527 return c;
528}
529
Peter Zijlstraf510b232009-01-22 17:53:47 +0100530void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700531{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100532 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700533
Peter Zijlstraf510b232009-01-22 17:53:47 +0100534#define LOCKDEP_STATE(__STATE) \
535 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
536 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
537#include "lockdep_states.h"
538#undef LOCKDEP_STATE
539
540 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700541}
542
Steven Rostedte5e78d02011-11-02 20:24:16 -0400543static void __print_lock_name(struct lock_class *class)
Steven Rostedt3003eba2011-04-20 21:41:54 -0400544{
545 char str[KSYM_NAME_LEN];
546 const char *name;
547
548 name = class->name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700549 if (!name) {
550 name = __get_key_name(class->key, str);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100551 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700552 } else {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100553 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700554 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100555 printk(KERN_CONT "#%d", class->name_version);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700556 if (class->subclass)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100557 printk(KERN_CONT "/%d", class->subclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700558 }
Steven Rostedte5e78d02011-11-02 20:24:16 -0400559}
560
561static void print_lock_name(struct lock_class *class)
562{
563 char usage[LOCK_USAGE_CHARS];
564
565 get_usage_chars(class, usage);
566
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100567 printk(KERN_CONT " (");
Steven Rostedte5e78d02011-11-02 20:24:16 -0400568 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100569 printk(KERN_CONT "){%s}", usage);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700570}
571
572static void print_lockdep_cache(struct lockdep_map *lock)
573{
574 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700575 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700576
577 name = lock->name;
578 if (!name)
579 name = __get_key_name(lock->key->subkeys, str);
580
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100581 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700582}
583
584static void print_lock(struct held_lock *hlock)
585{
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200586 /*
587 * We can be called locklessly through debug_show_all_locks() so be
588 * extra careful, the hlock might have been released and cleared.
589 */
590 unsigned int class_idx = hlock->class_idx;
591
592 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
593 barrier();
594
595 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100596 printk(KERN_CONT "<RELEASED>\n");
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200597 return;
598 }
599
Tetsuo Handab3c39752018-03-27 19:41:41 +0900600 printk(KERN_CONT "%p", hlock->instance);
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200601 print_lock_name(lock_classes + class_idx - 1);
Tetsuo Handab3c39752018-03-27 19:41:41 +0900602 printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700603}
604
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900605static void lockdep_print_held_locks(struct task_struct *p)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700606{
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900607 int i, depth = READ_ONCE(p->lockdep_depth);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700608
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900609 if (!depth)
610 printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p));
611 else
612 printk("%d lock%s held by %s/%d:\n", depth,
613 depth > 1 ? "s" : "", p->comm, task_pid_nr(p));
614 /*
615 * It's not reliable to print a task's held locks if it's not sleeping
616 * and it's not the current task.
617 */
618 if (p->state == TASK_RUNNING && p != current)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700619 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700620 for (i = 0; i < depth; i++) {
621 printk(" #%d: ", i);
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900622 print_lock(p->held_locks + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700623 }
624}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700625
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100626static void print_kernel_ident(void)
Dave Jones99de0552006-09-29 02:00:10 -0700627{
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100628 printk("%s %.*s %s\n", init_utsname()->release,
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700629 (int)strcspn(init_utsname()->version, " "),
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100630 init_utsname()->version,
631 print_tainted());
Dave Jones99de0552006-09-29 02:00:10 -0700632}
633
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700634static int very_verbose(struct lock_class *class)
635{
636#if VERY_VERBOSE
637 return class_filter(class);
638#endif
639 return 0;
640}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700641
642/*
643 * Is this the address of a static object:
644 */
Sasha Levin8dce7a92013-06-13 18:41:16 -0400645#ifdef __KERNEL__
Bart Van Assche108c1482019-02-14 15:00:53 -0800646static int static_obj(const void *obj)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700647{
648 unsigned long start = (unsigned long) &_stext,
649 end = (unsigned long) &_end,
650 addr = (unsigned long) obj;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700651
652 /*
653 * static variable?
654 */
655 if ((addr >= start) && (addr < end))
656 return 1;
657
Mike Frysinger2a9ad182009-09-22 16:44:16 -0700658 if (arch_is_kernel_data(addr))
659 return 1;
660
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700661 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900662 * in-kernel percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700663 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900664 if (is_kernel_percpu_address(addr))
665 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700666
667 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900668 * module static or percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700669 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900670 return is_module_address(addr) || is_module_percpu_address(addr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700671}
Sasha Levin8dce7a92013-06-13 18:41:16 -0400672#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700673
674/*
675 * To make lock name printouts unique, we calculate a unique
Bart Van Asschefe27b0d2018-12-06 17:11:37 -0800676 * class->name_version generation counter. The caller must hold the graph
677 * lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700678 */
679static int count_matching_names(struct lock_class *new_class)
680{
681 struct lock_class *class;
682 int count = 0;
683
684 if (!new_class->name)
685 return 0;
686
Bart Van Asschefe27b0d2018-12-06 17:11:37 -0800687 list_for_each_entry(class, &all_lock_classes, lock_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700688 if (new_class->key - new_class->subclass == class->key)
689 return class->name_version;
690 if (class->name && !strcmp(class->name, new_class->name))
691 count = max(count, class->name_version);
692 }
693
694 return count + 1;
695}
696
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700697static inline struct lock_class *
Matthew Wilcox08f36ff2018-01-17 07:14:13 -0800698look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700699{
700 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -0800701 struct hlist_head *hash_head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700702 struct lock_class *class;
703
Hitoshi Mitake4ba053c2010-10-13 17:30:26 +0900704 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
705 debug_locks_off();
706 printk(KERN_ERR
707 "BUG: looking up invalid subclass: %u\n", subclass);
708 printk(KERN_ERR
709 "turning off the locking correctness validator.\n");
710 dump_stack();
711 return NULL;
712 }
713
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700714 /*
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800715 * If it is not initialised then it has never been locked,
716 * so it won't be present in the hash table.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700717 */
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800718 if (unlikely(!lock->key))
719 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700720
721 /*
722 * NOTE: the class-key must be unique. For dynamic locks, a static
723 * lock_class_key variable is passed in through the mutex_init()
724 * (or spin_lock_init()) call - which acts as the key. For static
725 * locks we use the lock object itself as the key.
726 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700727 BUILD_BUG_ON(sizeof(struct lock_class_key) >
728 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700729
730 key = lock->key->subkeys + subclass;
731
732 hash_head = classhashentry(key);
733
734 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +0100735 * We do an RCU walk of the hash, see lockdep_free_key_range().
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700736 */
Peter Zijlstra35a93932015-02-26 16:23:11 +0100737 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
738 return NULL;
739
Andrew Mortona63f38c2016-02-03 13:44:12 -0800740 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700741 if (class->key == key) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200742 /*
743 * Huh! same key, different name? Did someone trample
744 * on some memory? We're most confused.
745 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700746 WARN_ON_ONCE(class->name != lock->name);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700747 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700748 }
749 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700750
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800751 return NULL;
752}
753
754/*
755 * Static locks do not have their class-keys yet - for them the key is
756 * the lock object itself. If the lock is in the per cpu area, the
757 * canonical address of the lock (per cpu offset removed) is used.
758 */
759static bool assign_lock_key(struct lockdep_map *lock)
760{
761 unsigned long can_addr, addr = (unsigned long)lock;
762
Bart Van Assche4bf50862019-02-14 15:00:52 -0800763#ifdef __KERNEL__
764 /*
765 * lockdep_free_key_range() assumes that struct lock_class_key
766 * objects do not overlap. Since we use the address of lock
767 * objects as class key for static objects, check whether the
768 * size of lock_class_key objects does not exceed the size of
769 * the smallest lock object.
770 */
771 BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(raw_spinlock_t));
772#endif
773
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800774 if (__is_kernel_percpu_address(addr, &can_addr))
775 lock->key = (void *)can_addr;
776 else if (__is_module_percpu_address(addr, &can_addr))
777 lock->key = (void *)can_addr;
778 else if (static_obj(lock))
779 lock->key = (void *)lock;
780 else {
781 /* Debug-check: all keys must be persistent! */
782 debug_locks_off();
783 pr_err("INFO: trying to register non-static key.\n");
784 pr_err("the code is fine but needs lockdep annotation.\n");
785 pr_err("turning off the locking correctness validator.\n");
786 dump_stack();
787 return false;
788 }
789
790 return true;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700791}
792
Peter Zijlstra72dcd502019-02-25 15:56:32 +0100793#ifdef CONFIG_DEBUG_LOCKDEP
794
Bart Van Asscheb526b2e2019-02-14 15:00:51 -0800795/* Check whether element @e occurs in list @h */
796static bool in_list(struct list_head *e, struct list_head *h)
797{
798 struct list_head *f;
799
800 list_for_each(f, h) {
801 if (e == f)
802 return true;
803 }
804
805 return false;
806}
807
808/*
809 * Check whether entry @e occurs in any of the locks_after or locks_before
810 * lists.
811 */
812static bool in_any_class_list(struct list_head *e)
813{
814 struct lock_class *class;
815 int i;
816
817 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
818 class = &lock_classes[i];
819 if (in_list(e, &class->locks_after) ||
820 in_list(e, &class->locks_before))
821 return true;
822 }
823 return false;
824}
825
826static bool class_lock_list_valid(struct lock_class *c, struct list_head *h)
827{
828 struct lock_list *e;
829
830 list_for_each_entry(e, h, entry) {
831 if (e->links_to != c) {
832 printk(KERN_INFO "class %s: mismatch for lock entry %ld; class %s <> %s",
833 c->name ? : "(?)",
834 (unsigned long)(e - list_entries),
835 e->links_to && e->links_to->name ?
836 e->links_to->name : "(?)",
837 e->class && e->class->name ? e->class->name :
838 "(?)");
839 return false;
840 }
841 }
842 return true;
843}
844
Arnd Bergmann3fe75222019-03-07 08:52:12 +0100845#ifdef CONFIG_PROVE_LOCKING
846static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
847#endif
Bart Van Asscheb526b2e2019-02-14 15:00:51 -0800848
849static bool check_lock_chain_key(struct lock_chain *chain)
850{
851#ifdef CONFIG_PROVE_LOCKING
852 u64 chain_key = 0;
853 int i;
854
855 for (i = chain->base; i < chain->base + chain->depth; i++)
856 chain_key = iterate_chain_key(chain_key, chain_hlocks[i] + 1);
857 /*
858 * The 'unsigned long long' casts avoid that a compiler warning
859 * is reported when building tools/lib/lockdep.
860 */
Peter Zijlstra72dcd502019-02-25 15:56:32 +0100861 if (chain->chain_key != chain_key) {
Bart Van Asscheb526b2e2019-02-14 15:00:51 -0800862 printk(KERN_INFO "chain %lld: key %#llx <> %#llx\n",
863 (unsigned long long)(chain - lock_chains),
864 (unsigned long long)chain->chain_key,
865 (unsigned long long)chain_key);
Peter Zijlstra72dcd502019-02-25 15:56:32 +0100866 return false;
867 }
Bart Van Asscheb526b2e2019-02-14 15:00:51 -0800868#endif
Peter Zijlstra72dcd502019-02-25 15:56:32 +0100869 return true;
Bart Van Asscheb526b2e2019-02-14 15:00:51 -0800870}
871
872static bool in_any_zapped_class_list(struct lock_class *class)
873{
874 struct pending_free *pf;
875 int i;
876
Peter Zijlstra72dcd502019-02-25 15:56:32 +0100877 for (i = 0, pf = delayed_free.pf; i < ARRAY_SIZE(delayed_free.pf); i++, pf++) {
Bart Van Asscheb526b2e2019-02-14 15:00:51 -0800878 if (in_list(&class->lock_entry, &pf->zapped))
879 return true;
Peter Zijlstra72dcd502019-02-25 15:56:32 +0100880 }
Bart Van Asscheb526b2e2019-02-14 15:00:51 -0800881
882 return false;
883}
884
Peter Zijlstra72dcd502019-02-25 15:56:32 +0100885static bool __check_data_structures(void)
Bart Van Asscheb526b2e2019-02-14 15:00:51 -0800886{
887 struct lock_class *class;
888 struct lock_chain *chain;
889 struct hlist_head *head;
890 struct lock_list *e;
891 int i;
892
893 /* Check whether all classes occur in a lock list. */
894 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
895 class = &lock_classes[i];
896 if (!in_list(&class->lock_entry, &all_lock_classes) &&
897 !in_list(&class->lock_entry, &free_lock_classes) &&
898 !in_any_zapped_class_list(class)) {
899 printk(KERN_INFO "class %px/%s is not in any class list\n",
900 class, class->name ? : "(?)");
901 return false;
Bart Van Asscheb526b2e2019-02-14 15:00:51 -0800902 }
903 }
904
905 /* Check whether all classes have valid lock lists. */
906 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
907 class = &lock_classes[i];
908 if (!class_lock_list_valid(class, &class->locks_before))
909 return false;
910 if (!class_lock_list_valid(class, &class->locks_after))
911 return false;
912 }
913
914 /* Check the chain_key of all lock chains. */
915 for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
916 head = chainhash_table + i;
917 hlist_for_each_entry_rcu(chain, head, entry) {
918 if (!check_lock_chain_key(chain))
919 return false;
920 }
921 }
922
923 /*
924 * Check whether all list entries that are in use occur in a class
925 * lock list.
926 */
927 for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
928 e = list_entries + i;
929 if (!in_any_class_list(&e->entry)) {
930 printk(KERN_INFO "list entry %d is not in any class list; class %s <> %s\n",
931 (unsigned int)(e - list_entries),
932 e->class->name ? : "(?)",
933 e->links_to->name ? : "(?)");
934 return false;
935 }
936 }
937
938 /*
939 * Check whether all list entries that are not in use do not occur in
940 * a class lock list.
941 */
942 for_each_clear_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
943 e = list_entries + i;
944 if (in_any_class_list(&e->entry)) {
945 printk(KERN_INFO "list entry %d occurs in a class list; class %s <> %s\n",
946 (unsigned int)(e - list_entries),
947 e->class && e->class->name ? e->class->name :
948 "(?)",
949 e->links_to && e->links_to->name ?
950 e->links_to->name : "(?)");
951 return false;
952 }
953 }
954
955 return true;
956}
957
Peter Zijlstra72dcd502019-02-25 15:56:32 +0100958int check_consistency = 0;
959module_param(check_consistency, int, 0644);
960
961static void check_data_structures(void)
962{
963 static bool once = false;
964
965 if (check_consistency && !once) {
966 if (!__check_data_structures()) {
967 once = true;
968 WARN_ON(once);
969 }
970 }
971}
972
973#else /* CONFIG_DEBUG_LOCKDEP */
974
975static inline void check_data_structures(void) { }
976
977#endif /* CONFIG_DEBUG_LOCKDEP */
978
Ingo Molnard6d897c2006-07-10 04:44:04 -0700979/*
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800980 * Initialize the lock_classes[] array elements, the free_lock_classes list
981 * and also the delayed_free structure.
Bart Van Asschefeb0a382019-02-14 15:00:42 -0800982 */
983static void init_data_structures_once(void)
984{
Bart Van Assche01265742019-03-03 10:19:01 -0800985 static bool ds_initialized, rcu_head_initialized;
Bart Van Asschefeb0a382019-02-14 15:00:42 -0800986 int i;
987
Bart Van Assche01265742019-03-03 10:19:01 -0800988 if (likely(rcu_head_initialized))
Bart Van Asschefeb0a382019-02-14 15:00:42 -0800989 return;
990
Bart Van Assche01265742019-03-03 10:19:01 -0800991 if (system_state >= SYSTEM_SCHEDULING) {
992 init_rcu_head(&delayed_free.rcu_head);
993 rcu_head_initialized = true;
994 }
Bart Van Asschefeb0a382019-02-14 15:00:42 -0800995
Bart Van Assche01265742019-03-03 10:19:01 -0800996 if (ds_initialized)
997 return;
998
999 ds_initialized = true;
1000
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001001 INIT_LIST_HEAD(&delayed_free.pf[0].zapped);
1002 INIT_LIST_HEAD(&delayed_free.pf[1].zapped);
1003
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001004 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001005 list_add_tail(&lock_classes[i].lock_entry, &free_lock_classes);
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001006 INIT_LIST_HEAD(&lock_classes[i].locks_after);
1007 INIT_LIST_HEAD(&lock_classes[i].locks_before);
1008 }
1009}
1010
Bart Van Assche108c1482019-02-14 15:00:53 -08001011static inline struct hlist_head *keyhashentry(const struct lock_class_key *key)
1012{
1013 unsigned long hash = hash_long((uintptr_t)key, KEYHASH_BITS);
1014
1015 return lock_keys_hash + hash;
1016}
1017
1018/* Register a dynamically allocated key. */
1019void lockdep_register_key(struct lock_class_key *key)
1020{
1021 struct hlist_head *hash_head;
1022 struct lock_class_key *k;
1023 unsigned long flags;
1024
1025 if (WARN_ON_ONCE(static_obj(key)))
1026 return;
1027 hash_head = keyhashentry(key);
1028
1029 raw_local_irq_save(flags);
1030 if (!graph_lock())
1031 goto restore_irqs;
1032 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
1033 if (WARN_ON_ONCE(k == key))
1034 goto out_unlock;
1035 }
1036 hlist_add_head_rcu(&key->hash_entry, hash_head);
1037out_unlock:
1038 graph_unlock();
1039restore_irqs:
1040 raw_local_irq_restore(flags);
1041}
1042EXPORT_SYMBOL_GPL(lockdep_register_key);
1043
1044/* Check whether a key has been registered as a dynamic key. */
1045static bool is_dynamic_key(const struct lock_class_key *key)
1046{
1047 struct hlist_head *hash_head;
1048 struct lock_class_key *k;
1049 bool found = false;
1050
1051 if (WARN_ON_ONCE(static_obj(key)))
1052 return false;
1053
1054 /*
1055 * If lock debugging is disabled lock_keys_hash[] may contain
1056 * pointers to memory that has already been freed. Avoid triggering
1057 * a use-after-free in that case by returning early.
1058 */
1059 if (!debug_locks)
1060 return true;
1061
1062 hash_head = keyhashentry(key);
1063
1064 rcu_read_lock();
1065 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
1066 if (k == key) {
1067 found = true;
1068 break;
1069 }
1070 }
1071 rcu_read_unlock();
1072
1073 return found;
1074}
1075
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001076/*
Ingo Molnard6d897c2006-07-10 04:44:04 -07001077 * Register a lock's class in the hash-table, if the class is not present
1078 * yet. Otherwise we look it up. We cache the result in the lock object
1079 * itself, so actual lookup of the hash should be once per lock object.
1080 */
Denys Vlasenkoc003ed92016-04-08 20:58:46 +02001081static struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04001082register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -07001083{
1084 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -08001085 struct hlist_head *hash_head;
Ingo Molnard6d897c2006-07-10 04:44:04 -07001086 struct lock_class *class;
Peter Zijlstra35a93932015-02-26 16:23:11 +01001087
1088 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
Ingo Molnard6d897c2006-07-10 04:44:04 -07001089
1090 class = look_up_lock_class(lock, subclass);
Matthew Wilcox64f29d12018-01-17 07:14:12 -08001091 if (likely(class))
Yong Zhang87cdee72011-11-09 16:07:14 +08001092 goto out_set_class_cache;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001093
Matthew Wilcox64f29d12018-01-17 07:14:12 -08001094 if (!lock->key) {
1095 if (!assign_lock_key(lock))
1096 return NULL;
Bart Van Assche108c1482019-02-14 15:00:53 -08001097 } else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001098 return NULL;
1099 }
1100
Ingo Molnard6d897c2006-07-10 04:44:04 -07001101 key = lock->key->subkeys + subclass;
1102 hash_head = classhashentry(key);
1103
Ingo Molnar74c383f2006-12-13 00:34:43 -08001104 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001105 return NULL;
1106 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001107 /*
1108 * We have to do the hash-walk again, to avoid races
1109 * with another CPU:
1110 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08001111 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001112 if (class->key == key)
1113 goto out_unlock_set;
Peter Zijlstra35a93932015-02-26 16:23:11 +01001114 }
1115
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001116 init_data_structures_once();
1117
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001118 /* Allocate a new lock class and add it to the hash. */
1119 class = list_first_entry_or_null(&free_lock_classes, typeof(*class),
1120 lock_entry);
1121 if (!class) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001122 if (!debug_locks_off_graph_unlock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001123 return NULL;
1124 }
Ingo Molnar74c383f2006-12-13 00:34:43 -08001125
Dave Jones2c522832013-04-25 13:40:02 -04001126 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01001127 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001128 return NULL;
1129 }
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001130 nr_lock_classes++;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001131 debug_atomic_inc(nr_unused_locks);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001132 class->key = key;
1133 class->name = lock->name;
1134 class->subclass = subclass;
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001135 WARN_ON_ONCE(!list_empty(&class->locks_before));
1136 WARN_ON_ONCE(!list_empty(&class->locks_after));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001137 class->name_version = count_matching_names(class);
1138 /*
1139 * We use RCU's safe list-add method to make
1140 * parallel walking of the hash-list safe:
1141 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08001142 hlist_add_head_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +01001143 /*
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001144 * Remove the class from the free list and add it to the global list
1145 * of classes.
Dale Farnsworth14811972008-02-25 23:03:02 +01001146 */
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001147 list_move_tail(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001148
1149 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001150 graph_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -08001151
Borislav Petkov04860d42018-02-26 14:49:26 +01001152 printk("\nnew class %px: %s", class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001153 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001154 printk(KERN_CONT "#%d", class->name_version);
1155 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001156 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -08001157
Ingo Molnar74c383f2006-12-13 00:34:43 -08001158 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001159 return NULL;
1160 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001161 }
1162out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -08001163 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001164
Yong Zhang87cdee72011-11-09 16:07:14 +08001165out_set_class_cache:
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04001166 if (!subclass || force)
Hitoshi Mitake62016252010-10-05 18:01:51 +09001167 lock->class_cache[0] = class;
1168 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
1169 lock->class_cache[subclass] = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001170
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001171 /*
1172 * Hash collision, did we smoke some? We found a class with a matching
1173 * hash but the subclass -- which is hashed in -- didn't match.
1174 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08001175 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
1176 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001177
1178 return class;
1179}
1180
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001181#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001182/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07001183 * Allocate a lockdep entry. (assumes the graph_lock held, returns
1184 * with NULL on failure)
1185 */
1186static struct lock_list *alloc_list_entry(void)
1187{
Bart Van Asscheace35a72019-02-14 15:00:47 -08001188 int idx = find_first_zero_bit(list_entries_in_use,
1189 ARRAY_SIZE(list_entries));
1190
1191 if (idx >= ARRAY_SIZE(list_entries)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001192 if (!debug_locks_off_graph_unlock())
1193 return NULL;
1194
Dave Jones2c522832013-04-25 13:40:02 -04001195 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01001196 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -07001197 return NULL;
1198 }
Bart Van Asscheace35a72019-02-14 15:00:47 -08001199 nr_list_entries++;
1200 __set_bit(idx, list_entries_in_use);
1201 return list_entries + idx;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001202}
1203
1204/*
1205 * Add a new dependency to the head of the list:
1206 */
Bart Van Assche86cffb82019-02-14 15:00:41 -08001207static int add_lock_to_list(struct lock_class *this,
1208 struct lock_class *links_to, struct list_head *head,
Tahsin Erdogan83f06162016-11-08 00:02:07 -08001209 unsigned long ip, int distance,
1210 struct stack_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001211{
1212 struct lock_list *entry;
1213 /*
1214 * Lock not present yet - get a new dependency struct and
1215 * add it to the list:
1216 */
1217 entry = alloc_list_entry();
1218 if (!entry)
1219 return 0;
1220
Zhu Yi74870172008-08-27 14:33:00 +08001221 entry->class = this;
Bart Van Assche86cffb82019-02-14 15:00:41 -08001222 entry->links_to = links_to;
Zhu Yi74870172008-08-27 14:33:00 +08001223 entry->distance = distance;
Yong Zhang4726f2a2010-05-04 14:16:48 +08001224 entry->trace = *trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001225 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +01001226 * Both allocation and removal are done under the graph lock; but
1227 * iteration is under RCU-sched; see look_up_lock_class() and
1228 * lockdep_free_key_range().
Peter Zijlstra8e182572007-07-19 01:48:54 -07001229 */
1230 list_add_tail_rcu(&entry->entry, head);
1231
1232 return 1;
1233}
1234
Peter Zijlstra98c33ed2009-07-21 13:19:07 +02001235/*
1236 * For good efficiency of modular, we use power of 2
1237 */
Peter Zijlstraaf012962009-07-16 15:44:29 +02001238#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
1239#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
1240
Peter Zijlstra98c33ed2009-07-21 13:19:07 +02001241/*
1242 * The circular_queue and helpers is used to implement the
Peter Zijlstraaf012962009-07-16 15:44:29 +02001243 * breadth-first search(BFS)algorithem, by which we can build
1244 * the shortest path from the next lock to be acquired to the
1245 * previous held lock if there is a circular between them.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +02001246 */
Peter Zijlstraaf012962009-07-16 15:44:29 +02001247struct circular_queue {
1248 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
1249 unsigned int front, rear;
1250};
1251
1252static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001253
Ming Lei12f3dfd2009-07-16 15:44:29 +02001254unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001255
Ming Leie351b662009-07-22 22:48:09 +08001256static unsigned int lockdep_dependency_gen_id;
1257
Peter Zijlstraaf012962009-07-16 15:44:29 +02001258static inline void __cq_init(struct circular_queue *cq)
1259{
1260 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +08001261 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001262}
1263
1264static inline int __cq_empty(struct circular_queue *cq)
1265{
1266 return (cq->front == cq->rear);
1267}
1268
1269static inline int __cq_full(struct circular_queue *cq)
1270{
1271 return ((cq->rear + 1) & CQ_MASK) == cq->front;
1272}
1273
1274static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
1275{
1276 if (__cq_full(cq))
1277 return -1;
1278
1279 cq->element[cq->rear] = elem;
1280 cq->rear = (cq->rear + 1) & CQ_MASK;
1281 return 0;
1282}
1283
1284static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
1285{
1286 if (__cq_empty(cq))
1287 return -1;
1288
1289 *elem = cq->element[cq->front];
1290 cq->front = (cq->front + 1) & CQ_MASK;
1291 return 0;
1292}
1293
1294static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
1295{
1296 return (cq->rear - cq->front) & CQ_MASK;
1297}
1298
1299static inline void mark_lock_accessed(struct lock_list *lock,
1300 struct lock_list *parent)
1301{
1302 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +02001303
Peter Zijlstraaf012962009-07-16 15:44:29 +02001304 nr = lock - list_entries;
Bart Van Asscheace35a72019-02-14 15:00:47 -08001305 WARN_ON(nr >= ARRAY_SIZE(list_entries)); /* Out-of-bounds, input fail */
Peter Zijlstraaf012962009-07-16 15:44:29 +02001306 lock->parent = parent;
Ming Leie351b662009-07-22 22:48:09 +08001307 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001308}
1309
1310static inline unsigned long lock_accessed(struct lock_list *lock)
1311{
1312 unsigned long nr;
Peter Zijlstra98c33ed2009-07-21 13:19:07 +02001313
Peter Zijlstraaf012962009-07-16 15:44:29 +02001314 nr = lock - list_entries;
Bart Van Asscheace35a72019-02-14 15:00:47 -08001315 WARN_ON(nr >= ARRAY_SIZE(list_entries)); /* Out-of-bounds, input fail */
Ming Leie351b662009-07-22 22:48:09 +08001316 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001317}
1318
1319static inline struct lock_list *get_lock_parent(struct lock_list *child)
1320{
1321 return child->parent;
1322}
1323
1324static inline int get_lock_depth(struct lock_list *child)
1325{
1326 int depth = 0;
1327 struct lock_list *parent;
1328
1329 while ((parent = get_lock_parent(child))) {
1330 child = parent;
1331 depth++;
1332 }
1333 return depth;
1334}
1335
Ming Lei9e2d5512009-07-16 15:44:29 +02001336static int __bfs(struct lock_list *source_entry,
Peter Zijlstraaf012962009-07-16 15:44:29 +02001337 void *data,
1338 int (*match)(struct lock_list *entry, void *data),
1339 struct lock_list **target_entry,
1340 int forward)
Ming Leic94aa5c2009-07-16 15:44:29 +02001341{
1342 struct lock_list *entry;
Ming Leid588e462009-07-16 15:44:29 +02001343 struct list_head *head;
Ming Leic94aa5c2009-07-16 15:44:29 +02001344 struct circular_queue *cq = &lock_cq;
1345 int ret = 1;
1346
Ming Lei9e2d5512009-07-16 15:44:29 +02001347 if (match(source_entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001348 *target_entry = source_entry;
1349 ret = 0;
1350 goto exit;
1351 }
1352
Ming Leid588e462009-07-16 15:44:29 +02001353 if (forward)
1354 head = &source_entry->class->locks_after;
1355 else
1356 head = &source_entry->class->locks_before;
1357
1358 if (list_empty(head))
1359 goto exit;
1360
1361 __cq_init(cq);
Ming Leic94aa5c2009-07-16 15:44:29 +02001362 __cq_enqueue(cq, (unsigned long)source_entry);
1363
1364 while (!__cq_empty(cq)) {
1365 struct lock_list *lock;
Ming Leic94aa5c2009-07-16 15:44:29 +02001366
1367 __cq_dequeue(cq, (unsigned long *)&lock);
1368
1369 if (!lock->class) {
1370 ret = -2;
1371 goto exit;
1372 }
1373
1374 if (forward)
1375 head = &lock->class->locks_after;
1376 else
1377 head = &lock->class->locks_before;
1378
Peter Zijlstra35a93932015-02-26 16:23:11 +01001379 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1380
1381 list_for_each_entry_rcu(entry, head, entry) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001382 if (!lock_accessed(entry)) {
Ming Lei12f3dfd2009-07-16 15:44:29 +02001383 unsigned int cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001384 mark_lock_accessed(entry, lock);
Ming Lei9e2d5512009-07-16 15:44:29 +02001385 if (match(entry, data)) {
Ming Leic94aa5c2009-07-16 15:44:29 +02001386 *target_entry = entry;
1387 ret = 0;
1388 goto exit;
1389 }
1390
1391 if (__cq_enqueue(cq, (unsigned long)entry)) {
1392 ret = -1;
1393 goto exit;
1394 }
Ming Lei12f3dfd2009-07-16 15:44:29 +02001395 cq_depth = __cq_get_elem_count(cq);
1396 if (max_bfs_queue_depth < cq_depth)
1397 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001398 }
1399 }
1400 }
1401exit:
1402 return ret;
1403}
1404
Ming Leid7aaba12009-07-16 15:44:29 +02001405static inline int __bfs_forwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001406 void *data,
1407 int (*match)(struct lock_list *entry, void *data),
1408 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001409{
Ming Lei9e2d5512009-07-16 15:44:29 +02001410 return __bfs(src_entry, data, match, target_entry, 1);
Ming Leic94aa5c2009-07-16 15:44:29 +02001411
1412}
1413
Ming Leid7aaba12009-07-16 15:44:29 +02001414static inline int __bfs_backwards(struct lock_list *src_entry,
Ming Lei9e2d5512009-07-16 15:44:29 +02001415 void *data,
1416 int (*match)(struct lock_list *entry, void *data),
1417 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001418{
Ming Lei9e2d5512009-07-16 15:44:29 +02001419 return __bfs(src_entry, data, match, target_entry, 0);
Ming Leic94aa5c2009-07-16 15:44:29 +02001420
1421}
1422
Peter Zijlstra8e182572007-07-19 01:48:54 -07001423/*
1424 * Recursive, forwards-direction lock-dependency checking, used for
1425 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1426 * checking.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001427 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001428
1429/*
1430 * Print a dependency chain entry (this is only done when a deadlock
1431 * has been detected):
1432 */
1433static noinline int
Ming Lei24208ca2009-07-16 15:44:29 +02001434print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001435{
1436 if (debug_locks_silent)
1437 return 0;
1438 printk("\n-> #%u", depth);
1439 print_lock_name(target->class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001440 printk(KERN_CONT ":\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001441 print_stack_trace(&target->trace, 6);
1442
1443 return 0;
1444}
1445
Steven Rostedtf4185812011-04-20 21:41:55 -04001446static void
1447print_circular_lock_scenario(struct held_lock *src,
1448 struct held_lock *tgt,
1449 struct lock_list *prt)
1450{
1451 struct lock_class *source = hlock_class(src);
1452 struct lock_class *target = hlock_class(tgt);
1453 struct lock_class *parent = prt->class;
1454
1455 /*
1456 * A direct locking problem where unsafe_class lock is taken
1457 * directly by safe_class lock, then all we need to show
1458 * is the deadlock scenario, as it is obvious that the
1459 * unsafe lock is taken under the safe lock.
1460 *
1461 * But if there is a chain instead, where the safe lock takes
1462 * an intermediate lock (middle_class) where this lock is
1463 * not the same as the safe lock, then the lock chain is
1464 * used to describe the problem. Otherwise we would need
1465 * to show a different CPU case for each link in the chain
1466 * from the safe_class lock to the unsafe_class lock.
1467 */
1468 if (parent != source) {
1469 printk("Chain exists of:\n ");
1470 __print_lock_name(source);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001471 printk(KERN_CONT " --> ");
Steven Rostedtf4185812011-04-20 21:41:55 -04001472 __print_lock_name(parent);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001473 printk(KERN_CONT " --> ");
Steven Rostedtf4185812011-04-20 21:41:55 -04001474 __print_lock_name(target);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001475 printk(KERN_CONT "\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001476 }
1477
Ingo Molnare966eae2017-12-12 12:31:16 +01001478 printk(" Possible unsafe locking scenario:\n\n");
1479 printk(" CPU0 CPU1\n");
1480 printk(" ---- ----\n");
1481 printk(" lock(");
1482 __print_lock_name(target);
1483 printk(KERN_CONT ");\n");
1484 printk(" lock(");
1485 __print_lock_name(parent);
1486 printk(KERN_CONT ");\n");
1487 printk(" lock(");
1488 __print_lock_name(target);
1489 printk(KERN_CONT ");\n");
1490 printk(" lock(");
1491 __print_lock_name(source);
1492 printk(KERN_CONT ");\n");
1493 printk("\n *** DEADLOCK ***\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001494}
1495
Peter Zijlstra8e182572007-07-19 01:48:54 -07001496/*
1497 * When a circular dependency is detected, print the
1498 * header first:
1499 */
1500static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001501print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1502 struct held_lock *check_src,
1503 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001504{
1505 struct task_struct *curr = current;
1506
Ming Leic94aa5c2009-07-16 15:44:29 +02001507 if (debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001508 return 0;
1509
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001510 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001511 pr_warn("======================================================\n");
1512 pr_warn("WARNING: possible circular locking dependency detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001513 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001514 pr_warn("------------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001515 pr_warn("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001516 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001517 print_lock(check_src);
Byungchul Park383a4bc2017-08-07 16:12:55 +09001518
Ingo Molnare966eae2017-12-12 12:31:16 +01001519 pr_warn("\nbut task is already holding lock:\n");
Byungchul Park383a4bc2017-08-07 16:12:55 +09001520
Ming Leidb0002a2009-07-16 15:44:29 +02001521 print_lock(check_tgt);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001522 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1523 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001524
1525 print_circular_bug_entry(entry, depth);
1526
1527 return 0;
1528}
1529
Ming Lei9e2d5512009-07-16 15:44:29 +02001530static inline int class_equal(struct lock_list *entry, void *data)
1531{
1532 return entry->class == data;
1533}
1534
Ming Leidb0002a2009-07-16 15:44:29 +02001535static noinline int print_circular_bug(struct lock_list *this,
1536 struct lock_list *target,
1537 struct held_lock *check_src,
Byungchul Park383a4bc2017-08-07 16:12:55 +09001538 struct held_lock *check_tgt,
1539 struct stack_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001540{
1541 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001542 struct lock_list *parent;
Steven Rostedtf4185812011-04-20 21:41:55 -04001543 struct lock_list *first_parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001544 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001545
Ming Leic94aa5c2009-07-16 15:44:29 +02001546 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001547 return 0;
1548
Ingo Molnare966eae2017-12-12 12:31:16 +01001549 if (!save_trace(&this->trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07001550 return 0;
1551
Ming Leic94aa5c2009-07-16 15:44:29 +02001552 depth = get_lock_depth(target);
1553
Ming Leidb0002a2009-07-16 15:44:29 +02001554 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001555
1556 parent = get_lock_parent(target);
Steven Rostedtf4185812011-04-20 21:41:55 -04001557 first_parent = parent;
Ming Leic94aa5c2009-07-16 15:44:29 +02001558
1559 while (parent) {
1560 print_circular_bug_entry(parent, --depth);
1561 parent = get_lock_parent(parent);
1562 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001563
1564 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001565 print_circular_lock_scenario(check_src, check_tgt,
1566 first_parent);
1567
Peter Zijlstra8e182572007-07-19 01:48:54 -07001568 lockdep_print_held_locks(curr);
1569
1570 printk("\nstack backtrace:\n");
1571 dump_stack();
1572
1573 return 0;
1574}
1575
Ming Leidb0002a2009-07-16 15:44:29 +02001576static noinline int print_bfs_bug(int ret)
1577{
1578 if (!debug_locks_off_graph_unlock())
1579 return 0;
1580
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001581 /*
1582 * Breadth-first-search failed, graph got corrupted?
1583 */
Ming Leidb0002a2009-07-16 15:44:29 +02001584 WARN(1, "lockdep bfs error:%d\n", ret);
1585
1586 return 0;
1587}
1588
Ming Leief681022009-07-16 15:44:29 +02001589static int noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07001590{
Ming Leief681022009-07-16 15:44:29 +02001591 (*(unsigned long *)data)++;
1592 return 0;
David Miller419ca3f2008-07-29 21:45:03 -07001593}
1594
Fengguang Wu5216d532013-11-09 00:55:35 +08001595static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
Ming Leief681022009-07-16 15:44:29 +02001596{
1597 unsigned long count = 0;
1598 struct lock_list *uninitialized_var(target_entry);
1599
1600 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1601
1602 return count;
1603}
David Miller419ca3f2008-07-29 21:45:03 -07001604unsigned long lockdep_count_forward_deps(struct lock_class *class)
1605{
1606 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001607 struct lock_list this;
1608
1609 this.parent = NULL;
1610 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001611
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04001612 raw_local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001613 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001614 ret = __lockdep_count_forward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001615 arch_spin_unlock(&lockdep_lock);
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04001616 raw_local_irq_restore(flags);
David Miller419ca3f2008-07-29 21:45:03 -07001617
1618 return ret;
1619}
1620
Fengguang Wu5216d532013-11-09 00:55:35 +08001621static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07001622{
Ming Leief681022009-07-16 15:44:29 +02001623 unsigned long count = 0;
1624 struct lock_list *uninitialized_var(target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001625
Ming Leief681022009-07-16 15:44:29 +02001626 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07001627
Ming Leief681022009-07-16 15:44:29 +02001628 return count;
David Miller419ca3f2008-07-29 21:45:03 -07001629}
1630
1631unsigned long lockdep_count_backward_deps(struct lock_class *class)
1632{
1633 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02001634 struct lock_list this;
1635
1636 this.parent = NULL;
1637 this.class = class;
David Miller419ca3f2008-07-29 21:45:03 -07001638
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04001639 raw_local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001640 arch_spin_lock(&lockdep_lock);
Ming Leief681022009-07-16 15:44:29 +02001641 ret = __lockdep_count_backward_deps(&this);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +01001642 arch_spin_unlock(&lockdep_lock);
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04001643 raw_local_irq_restore(flags);
David Miller419ca3f2008-07-29 21:45:03 -07001644
1645 return ret;
1646}
1647
Peter Zijlstra8e182572007-07-19 01:48:54 -07001648/*
1649 * Prove that the dependency graph starting at <entry> can not
1650 * lead to <target>. Print an error and return 0 if it does.
1651 */
1652static noinline int
Ming Leidb0002a2009-07-16 15:44:29 +02001653check_noncircular(struct lock_list *root, struct lock_class *target,
1654 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001655{
Ming Leidb0002a2009-07-16 15:44:29 +02001656 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001657
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001658 debug_atomic_inc(nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07001659
Ming Leid7aaba12009-07-16 15:44:29 +02001660 result = __bfs_forwards(root, target, class_equal, target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02001661
1662 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001663}
1664
Peter Zijlstraae813302017-03-03 10:13:38 +01001665static noinline int
1666check_redundant(struct lock_list *root, struct lock_class *target,
1667 struct lock_list **target_entry)
1668{
1669 int result;
1670
1671 debug_atomic_inc(nr_redundant_checks);
1672
1673 result = __bfs_forwards(root, target, class_equal, target_entry);
1674
1675 return result;
1676}
1677
Steven Rostedt81d68a92008-05-12 21:20:42 +02001678#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001679/*
1680 * Forwards and backwards subgraph searching, for the purposes of
1681 * proving that two subgraphs can be connected by a new dependency
1682 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1683 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07001684
Ming Leid7aaba12009-07-16 15:44:29 +02001685static inline int usage_match(struct lock_list *entry, void *bit)
1686{
1687 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1688}
1689
1690
1691
Peter Zijlstra8e182572007-07-19 01:48:54 -07001692/*
1693 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001694 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001695 *
Ming Leid7aaba12009-07-16 15:44:29 +02001696 * Return 0 if such a node exists in the subgraph, and put that node
1697 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001698 *
Ming Leid7aaba12009-07-16 15:44:29 +02001699 * Return 1 otherwise and keep *@target_entry unchanged.
1700 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001701 */
Ming Leid7aaba12009-07-16 15:44:29 +02001702static int
1703find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1704 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001705{
Ming Leid7aaba12009-07-16 15:44:29 +02001706 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001707
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001708 debug_atomic_inc(nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001709
Ming Leid7aaba12009-07-16 15:44:29 +02001710 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1711
1712 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001713}
1714
1715/*
1716 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02001717 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001718 *
Ming Leid7aaba12009-07-16 15:44:29 +02001719 * Return 0 if such a node exists in the subgraph, and put that node
1720 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001721 *
Ming Leid7aaba12009-07-16 15:44:29 +02001722 * Return 1 otherwise and keep *@target_entry unchanged.
1723 * Return <0 on error.
Peter Zijlstra8e182572007-07-19 01:48:54 -07001724 */
Ming Leid7aaba12009-07-16 15:44:29 +02001725static int
1726find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1727 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001728{
Ming Leid7aaba12009-07-16 15:44:29 +02001729 int result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001730
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001731 debug_atomic_inc(nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001732
Ming Leid7aaba12009-07-16 15:44:29 +02001733 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02001734
Ming Leid7aaba12009-07-16 15:44:29 +02001735 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001736}
1737
Peter Zijlstraaf012962009-07-16 15:44:29 +02001738static void print_lock_class_header(struct lock_class *class, int depth)
1739{
1740 int bit;
1741
1742 printk("%*s->", depth, "");
1743 print_lock_name(class);
Waiman Long8ca2b56c2018-10-03 13:07:18 -04001744#ifdef CONFIG_DEBUG_LOCKDEP
1745 printk(KERN_CONT " ops: %lu", debug_class_ops_read(class));
1746#endif
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001747 printk(KERN_CONT " {\n");
Peter Zijlstraaf012962009-07-16 15:44:29 +02001748
1749 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1750 if (class->usage_mask & (1 << bit)) {
1751 int len = depth;
1752
1753 len += printk("%*s %s", depth, "", usage_str[bit]);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001754 len += printk(KERN_CONT " at:\n");
Peter Zijlstraaf012962009-07-16 15:44:29 +02001755 print_stack_trace(class->usage_traces + bit, len);
1756 }
1757 }
1758 printk("%*s }\n", depth, "");
1759
Borislav Petkov04860d42018-02-26 14:49:26 +01001760 printk("%*s ... key at: [<%px>] %pS\n",
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001761 depth, "", class->key, class->key);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001762}
1763
1764/*
1765 * printk the shortest lock dependencies from @start to @end in reverse order:
1766 */
1767static void __used
1768print_shortest_lock_dependencies(struct lock_list *leaf,
1769 struct lock_list *root)
1770{
1771 struct lock_list *entry = leaf;
1772 int depth;
1773
1774 /*compute depth from generated tree by BFS*/
1775 depth = get_lock_depth(leaf);
1776
1777 do {
1778 print_lock_class_header(entry->class, depth);
1779 printk("%*s ... acquired at:\n", depth, "");
1780 print_stack_trace(&entry->trace, 2);
1781 printk("\n");
1782
1783 if (depth == 0 && (entry != root)) {
Steven Rostedt6be8c392011-04-20 21:41:58 -04001784 printk("lockdep:%s bad path found in chain graph\n", __func__);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001785 break;
1786 }
1787
1788 entry = get_lock_parent(entry);
1789 depth--;
1790 } while (entry && (depth >= 0));
1791
1792 return;
1793}
Ming Leid7aaba12009-07-16 15:44:29 +02001794
Steven Rostedt3003eba2011-04-20 21:41:54 -04001795static void
1796print_irq_lock_scenario(struct lock_list *safe_entry,
1797 struct lock_list *unsafe_entry,
Steven Rostedtdad3d742011-04-20 21:41:57 -04001798 struct lock_class *prev_class,
1799 struct lock_class *next_class)
Steven Rostedt3003eba2011-04-20 21:41:54 -04001800{
1801 struct lock_class *safe_class = safe_entry->class;
1802 struct lock_class *unsafe_class = unsafe_entry->class;
Steven Rostedtdad3d742011-04-20 21:41:57 -04001803 struct lock_class *middle_class = prev_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001804
1805 if (middle_class == safe_class)
Steven Rostedtdad3d742011-04-20 21:41:57 -04001806 middle_class = next_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04001807
1808 /*
1809 * A direct locking problem where unsafe_class lock is taken
1810 * directly by safe_class lock, then all we need to show
1811 * is the deadlock scenario, as it is obvious that the
1812 * unsafe lock is taken under the safe lock.
1813 *
1814 * But if there is a chain instead, where the safe lock takes
1815 * an intermediate lock (middle_class) where this lock is
1816 * not the same as the safe lock, then the lock chain is
1817 * used to describe the problem. Otherwise we would need
1818 * to show a different CPU case for each link in the chain
1819 * from the safe_class lock to the unsafe_class lock.
1820 */
1821 if (middle_class != unsafe_class) {
1822 printk("Chain exists of:\n ");
1823 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001824 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001825 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001826 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001827 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001828 printk(KERN_CONT "\n\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001829 }
1830
1831 printk(" Possible interrupt unsafe locking scenario:\n\n");
1832 printk(" CPU0 CPU1\n");
1833 printk(" ---- ----\n");
1834 printk(" lock(");
1835 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001836 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001837 printk(" local_irq_disable();\n");
1838 printk(" lock(");
1839 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001840 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001841 printk(" lock(");
1842 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001843 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001844 printk(" <Interrupt>\n");
1845 printk(" lock(");
1846 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001847 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04001848 printk("\n *** DEADLOCK ***\n\n");
1849}
1850
Peter Zijlstra8e182572007-07-19 01:48:54 -07001851static int
1852print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02001853 struct lock_list *prev_root,
1854 struct lock_list *next_root,
1855 struct lock_list *backwards_entry,
1856 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001857 struct held_lock *prev,
1858 struct held_lock *next,
1859 enum lock_usage_bit bit1,
1860 enum lock_usage_bit bit2,
1861 const char *irqclass)
1862{
1863 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1864 return 0;
1865
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001866 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001867 pr_warn("=====================================================\n");
1868 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07001869 irqclass, irqclass);
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001870 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001871 pr_warn("-----------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001872 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 -07001873 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07001874 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1875 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1876 curr->hardirqs_enabled,
1877 curr->softirqs_enabled);
1878 print_lock(next);
1879
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001880 pr_warn("\nand this task is already holding:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001881 print_lock(prev);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001882 pr_warn("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02001883 print_lock_name(hlock_class(prev));
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001884 pr_cont(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02001885 print_lock_name(hlock_class(next));
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001886 pr_cont("\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001887
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001888 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07001889 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001890 print_lock_name(backwards_entry->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001891 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001892
Ming Lei24208ca2009-07-16 15:44:29 +02001893 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001894
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001895 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001896 print_lock_name(forwards_entry->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001897 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
1898 pr_warn("...");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001899
Ming Lei24208ca2009-07-16 15:44:29 +02001900 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001901
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001902 pr_warn("\nother info that might help us debug this:\n\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04001903 print_irq_lock_scenario(backwards_entry, forwards_entry,
1904 hlock_class(prev), hlock_class(next));
Steven Rostedt3003eba2011-04-20 21:41:54 -04001905
Peter Zijlstra8e182572007-07-19 01:48:54 -07001906 lockdep_print_held_locks(curr);
1907
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001908 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001909 if (!save_trace(&prev_root->trace))
1910 return 0;
1911 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001912
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001913 pr_warn("\nthe dependencies between the lock to be acquired");
1914 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02001915 if (!save_trace(&next_root->trace))
1916 return 0;
1917 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001918
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001919 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001920 dump_stack();
1921
1922 return 0;
1923}
1924
1925static int
1926check_usage(struct task_struct *curr, struct held_lock *prev,
1927 struct held_lock *next, enum lock_usage_bit bit_backwards,
1928 enum lock_usage_bit bit_forwards, const char *irqclass)
1929{
1930 int ret;
Ming Lei24208ca2009-07-16 15:44:29 +02001931 struct lock_list this, that;
Ming Leid7aaba12009-07-16 15:44:29 +02001932 struct lock_list *uninitialized_var(target_entry);
Ming Lei24208ca2009-07-16 15:44:29 +02001933 struct lock_list *uninitialized_var(target_entry1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001934
Ming Leid7aaba12009-07-16 15:44:29 +02001935 this.parent = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001936
Ming Leid7aaba12009-07-16 15:44:29 +02001937 this.class = hlock_class(prev);
1938 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001939 if (ret < 0)
1940 return print_bfs_bug(ret);
1941 if (ret == 1)
1942 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001943
Ming Lei24208ca2009-07-16 15:44:29 +02001944 that.parent = NULL;
1945 that.class = hlock_class(next);
1946 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
Peter Zijlstraaf012962009-07-16 15:44:29 +02001947 if (ret < 0)
1948 return print_bfs_bug(ret);
1949 if (ret == 1)
1950 return ret;
Ming Leid7aaba12009-07-16 15:44:29 +02001951
Ming Lei24208ca2009-07-16 15:44:29 +02001952 return print_bad_irq_dependency(curr, &this, &that,
1953 target_entry, target_entry1,
1954 prev, next,
Peter Zijlstra8e182572007-07-19 01:48:54 -07001955 bit_backwards, bit_forwards, irqclass);
1956}
1957
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001958static const char *state_names[] = {
1959#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001960 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001961#include "lockdep_states.h"
1962#undef LOCKDEP_STATE
1963};
1964
1965static const char *state_rnames[] = {
1966#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01001967 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001968#include "lockdep_states.h"
1969#undef LOCKDEP_STATE
1970};
1971
1972static inline const char *state_name(enum lock_usage_bit bit)
1973{
Frederic Weisbeckerc902a1e2019-04-02 18:02:42 +02001974 if (bit & LOCK_USAGE_READ_MASK)
1975 return state_rnames[bit >> LOCK_USAGE_DIR_MASK];
1976 else
1977 return state_names[bit >> LOCK_USAGE_DIR_MASK];
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001978}
1979
1980static int exclusive_bit(int new_bit)
1981{
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01001982 int state = new_bit & LOCK_USAGE_STATE_MASK;
1983 int dir = new_bit & LOCK_USAGE_DIR_MASK;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001984
1985 /*
1986 * keep state, bit flip the direction and strip read.
1987 */
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01001988 return state | (dir ^ LOCK_USAGE_DIR_MASK);
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01001989}
1990
1991static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1992 struct held_lock *next, enum lock_usage_bit bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001993{
1994 /*
1995 * Prove that the new dependency does not connect a hardirq-safe
1996 * lock with a hardirq-unsafe lock - to achieve this we search
1997 * the backwards-subgraph starting at <prev>, and the
1998 * forwards-subgraph starting at <next>:
1999 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002000 if (!check_usage(curr, prev, next, bit,
2001 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002002 return 0;
2003
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002004 bit++; /* _READ */
2005
Peter Zijlstra8e182572007-07-19 01:48:54 -07002006 /*
2007 * Prove that the new dependency does not connect a hardirq-safe-read
2008 * lock with a hardirq-unsafe lock - to achieve this we search
2009 * the backwards-subgraph starting at <prev>, and the
2010 * forwards-subgraph starting at <next>:
2011 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002012 if (!check_usage(curr, prev, next, bit,
2013 exclusive_bit(bit), state_name(bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002014 return 0;
2015
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002016 return 1;
2017}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002018
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002019static int
2020check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
2021 struct held_lock *next)
2022{
2023#define LOCKDEP_STATE(__STATE) \
2024 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
Nick Piggincf40bd12009-01-21 08:12:39 +01002025 return 0;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002026#include "lockdep_states.h"
2027#undef LOCKDEP_STATE
Nick Piggincf40bd12009-01-21 08:12:39 +01002028
Peter Zijlstra8e182572007-07-19 01:48:54 -07002029 return 1;
2030}
2031
2032static void inc_chains(void)
2033{
2034 if (current->hardirq_context)
2035 nr_hardirq_chains++;
2036 else {
2037 if (current->softirq_context)
2038 nr_softirq_chains++;
2039 else
2040 nr_process_chains++;
2041 }
2042}
2043
2044#else
2045
2046static inline int
2047check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
2048 struct held_lock *next)
2049{
2050 return 1;
2051}
2052
2053static inline void inc_chains(void)
2054{
2055 nr_process_chains++;
2056}
2057
2058#endif
2059
Steven Rostedt48702ec2011-04-20 21:41:56 -04002060static void
2061print_deadlock_scenario(struct held_lock *nxt,
2062 struct held_lock *prv)
2063{
2064 struct lock_class *next = hlock_class(nxt);
2065 struct lock_class *prev = hlock_class(prv);
2066
2067 printk(" Possible unsafe locking scenario:\n\n");
2068 printk(" CPU0\n");
2069 printk(" ----\n");
2070 printk(" lock(");
2071 __print_lock_name(prev);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002072 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04002073 printk(" lock(");
2074 __print_lock_name(next);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002075 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04002076 printk("\n *** DEADLOCK ***\n\n");
2077 printk(" May be due to missing lock nesting notation\n\n");
2078}
2079
Peter Zijlstra8e182572007-07-19 01:48:54 -07002080static int
2081print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
2082 struct held_lock *next)
2083{
2084 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2085 return 0;
2086
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002087 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002088 pr_warn("============================================\n");
2089 pr_warn("WARNING: possible recursive locking detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002090 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002091 pr_warn("--------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002092 pr_warn("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002093 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07002094 print_lock(next);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002095 pr_warn("\nbut task is already holding lock:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002096 print_lock(prev);
2097
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002098 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04002099 print_deadlock_scenario(next, prev);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002100 lockdep_print_held_locks(curr);
2101
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002102 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002103 dump_stack();
2104
2105 return 0;
2106}
2107
2108/*
2109 * Check whether we are holding such a class already.
2110 *
2111 * (Note that this has to be done separately, because the graph cannot
2112 * detect such classes of deadlocks.)
2113 *
2114 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
2115 */
2116static int
2117check_deadlock(struct task_struct *curr, struct held_lock *next,
2118 struct lockdep_map *next_instance, int read)
2119{
2120 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002121 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002122 int i;
2123
2124 for (i = 0; i < curr->lockdep_depth; i++) {
2125 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002126
2127 if (prev->instance == next->nest_lock)
2128 nest = prev;
2129
Dave Jonesf82b2172008-08-11 09:30:23 +02002130 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002131 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002132
Peter Zijlstra8e182572007-07-19 01:48:54 -07002133 /*
2134 * Allow read-after-read recursion of the same
2135 * lock class (i.e. read_lock(lock)+read_lock(lock)):
2136 */
2137 if ((read == 2) && prev->read)
2138 return 2;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002139
2140 /*
2141 * We're holding the nest_lock, which serializes this lock's
2142 * nesting behaviour.
2143 */
2144 if (nest)
2145 return 2;
2146
Peter Zijlstra8e182572007-07-19 01:48:54 -07002147 return print_deadlock_bug(curr, prev, next);
2148 }
2149 return 1;
2150}
2151
2152/*
2153 * There was a chain-cache miss, and we are about to add a new dependency
2154 * to a previous lock. We recursively validate the following rules:
2155 *
2156 * - would the adding of the <prev> -> <next> dependency create a
2157 * circular dependency in the graph? [== circular deadlock]
2158 *
2159 * - does the new prev->next dependency connect any hardirq-safe lock
2160 * (in the full backwards-subgraph starting at <prev>) with any
2161 * hardirq-unsafe lock (in the full forwards-subgraph starting at
2162 * <next>)? [== illegal lock inversion with hardirq contexts]
2163 *
2164 * - does the new prev->next dependency connect any softirq-safe lock
2165 * (in the full backwards-subgraph starting at <prev>) with any
2166 * softirq-unsafe lock (in the full forwards-subgraph starting at
2167 * <next>)? [== illegal lock inversion with softirq contexts]
2168 *
2169 * any of these scenarios could lead to a deadlock.
2170 *
2171 * Then if all the validations pass, we add the forwards and backwards
2172 * dependency.
2173 */
2174static int
2175check_prev_add(struct task_struct *curr, struct held_lock *prev,
Byungchul Parkce07a9412017-08-07 16:12:51 +09002176 struct held_lock *next, int distance, struct stack_trace *trace,
2177 int (*save)(struct stack_trace *trace))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002178{
Ming Leidb0002a2009-07-16 15:44:29 +02002179 struct lock_list *uninitialized_var(target_entry);
Peter Zijlstra8b405d52017-10-04 11:13:37 +02002180 struct lock_list *entry;
2181 struct lock_list this;
2182 int ret;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002183
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08002184 if (!hlock_class(prev)->key || !hlock_class(next)->key) {
2185 /*
2186 * The warning statements below may trigger a use-after-free
2187 * of the class name. It is better to trigger a use-after free
2188 * and to have the class name most of the time instead of not
2189 * having the class name available.
2190 */
2191 WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key,
2192 "Detected use-after-free of lock class %px/%s\n",
2193 hlock_class(prev),
2194 hlock_class(prev)->name);
2195 WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key,
2196 "Detected use-after-free of lock class %px/%s\n",
2197 hlock_class(next),
2198 hlock_class(next)->name);
2199 return 2;
2200 }
2201
Peter Zijlstra8e182572007-07-19 01:48:54 -07002202 /*
2203 * Prove that the new <prev> -> <next> dependency would not
2204 * create a circular dependency in the graph. (We do this by
2205 * forward-recursing into the graph starting at <next>, and
2206 * checking whether we can reach <prev>.)
2207 *
2208 * We are using global variables to control the recursion, to
2209 * keep the stackframe size of the recursive functions low:
2210 */
Ming Leidb0002a2009-07-16 15:44:29 +02002211 this.class = hlock_class(next);
2212 this.parent = NULL;
2213 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
Peter Zijlstra8b405d52017-10-04 11:13:37 +02002214 if (unlikely(!ret)) {
2215 if (!trace->entries) {
2216 /*
2217 * If @save fails here, the printing might trigger
2218 * a WARN but because of the !nr_entries it should
2219 * not do bad things.
2220 */
2221 save(trace);
2222 }
Byungchul Park383a4bc2017-08-07 16:12:55 +09002223 return print_circular_bug(&this, target_entry, next, prev, trace);
Peter Zijlstra8b405d52017-10-04 11:13:37 +02002224 }
Ming Leidb0002a2009-07-16 15:44:29 +02002225 else if (unlikely(ret < 0))
2226 return print_bfs_bug(ret);
Ming Leic94aa5c2009-07-16 15:44:29 +02002227
Peter Zijlstra8e182572007-07-19 01:48:54 -07002228 if (!check_prev_add_irq(curr, prev, next))
2229 return 0;
2230
2231 /*
2232 * For recursive read-locks we do all the dependency checks,
2233 * but we dont store read-triggered dependencies (only
2234 * write-triggered dependencies). This ensures that only the
2235 * write-side dependencies matter, and that if for example a
2236 * write-lock never takes any other locks, then the reads are
2237 * equivalent to a NOP.
2238 */
2239 if (next->read == 2 || prev->read == 2)
2240 return 1;
2241 /*
2242 * Is the <prev> -> <next> dependency already present?
2243 *
2244 * (this may occur even though this is a new chain: consider
2245 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
2246 * chains - the second one will be new, but L1 already has
2247 * L2 added to its dependency list, due to the first chain.)
2248 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002249 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
2250 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002251 if (distance == 1)
2252 entry->distance = 1;
Byungchul Park70911fd2017-08-07 16:12:50 +09002253 return 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002254 }
2255 }
2256
Peter Zijlstraae813302017-03-03 10:13:38 +01002257 /*
2258 * Is the <prev> -> <next> link redundant?
2259 */
2260 this.class = hlock_class(prev);
2261 this.parent = NULL;
2262 ret = check_redundant(&this, hlock_class(next), &target_entry);
2263 if (!ret) {
2264 debug_atomic_inc(nr_redundant);
2265 return 2;
2266 }
2267 if (ret < 0)
2268 return print_bfs_bug(ret);
2269
2270
Peter Zijlstra8b405d52017-10-04 11:13:37 +02002271 if (!trace->entries && !save(trace))
Byungchul Parkce07a9412017-08-07 16:12:51 +09002272 return 0;
Yong Zhang4726f2a2010-05-04 14:16:48 +08002273
Peter Zijlstra8e182572007-07-19 01:48:54 -07002274 /*
2275 * Ok, all validations passed, add the new lock
2276 * to the previous lock's dependency list:
2277 */
Bart Van Assche86cffb82019-02-14 15:00:41 -08002278 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
Dave Jonesf82b2172008-08-11 09:30:23 +02002279 &hlock_class(prev)->locks_after,
Byungchul Parkce07a9412017-08-07 16:12:51 +09002280 next->acquire_ip, distance, trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002281
2282 if (!ret)
2283 return 0;
2284
Bart Van Assche86cffb82019-02-14 15:00:41 -08002285 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
Dave Jonesf82b2172008-08-11 09:30:23 +02002286 &hlock_class(next)->locks_before,
Byungchul Parkce07a9412017-08-07 16:12:51 +09002287 next->acquire_ip, distance, trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002288 if (!ret)
2289 return 0;
2290
Byungchul Park70911fd2017-08-07 16:12:50 +09002291 return 2;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002292}
2293
2294/*
2295 * Add the dependency to all directly-previous locks that are 'relevant'.
2296 * The ones that are relevant are (in increasing distance from curr):
2297 * all consecutive trylock entries and the final non-trylock entry - or
2298 * the end of this context's lock-chain - whichever comes first.
2299 */
2300static int
2301check_prevs_add(struct task_struct *curr, struct held_lock *next)
2302{
2303 int depth = curr->lockdep_depth;
2304 struct held_lock *hlock;
Peter Zijlstra8b405d52017-10-04 11:13:37 +02002305 struct stack_trace trace = {
2306 .nr_entries = 0,
2307 .max_entries = 0,
2308 .entries = NULL,
2309 .skip = 0,
2310 };
Peter Zijlstra8e182572007-07-19 01:48:54 -07002311
2312 /*
2313 * Debugging checks.
2314 *
2315 * Depth must not be zero for a non-head lock:
2316 */
2317 if (!depth)
2318 goto out_bug;
2319 /*
2320 * At least two relevant locks must exist for this
2321 * to be a head:
2322 */
2323 if (curr->held_locks[depth].irq_context !=
2324 curr->held_locks[depth-1].irq_context)
2325 goto out_bug;
2326
2327 for (;;) {
2328 int distance = curr->lockdep_depth - depth + 1;
Oleg Nesterov1b5ff812014-01-20 19:20:10 +01002329 hlock = curr->held_locks + depth - 1;
Byungchul Parkce07a9412017-08-07 16:12:51 +09002330
Ingo Molnare966eae2017-12-12 12:31:16 +01002331 /*
2332 * Only non-recursive-read entries get new dependencies
2333 * added:
2334 */
2335 if (hlock->read != 2 && hlock->check) {
2336 int ret = check_prev_add(curr, hlock, next, distance, &trace, save_trace);
2337 if (!ret)
2338 return 0;
2339
2340 /*
2341 * Stop after the first non-trylock entry,
2342 * as non-trylock entries have added their
2343 * own direct dependencies already, so this
2344 * lock is connected to them indirectly:
2345 */
2346 if (!hlock->trylock)
2347 break;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002348 }
Ingo Molnare966eae2017-12-12 12:31:16 +01002349
Peter Zijlstra8e182572007-07-19 01:48:54 -07002350 depth--;
2351 /*
2352 * End of lock-stack?
2353 */
2354 if (!depth)
2355 break;
2356 /*
2357 * Stop the search if we cross into another context:
2358 */
2359 if (curr->held_locks[depth].irq_context !=
2360 curr->held_locks[depth-1].irq_context)
2361 break;
2362 }
2363 return 1;
2364out_bug:
2365 if (!debug_locks_off_graph_unlock())
2366 return 0;
2367
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002368 /*
2369 * Clearly we all shouldn't be here, but since we made it we
2370 * can reliable say we messed up our state. See the above two
2371 * gotos for reasons why we could possibly end up here.
2372 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002373 WARN_ON(1);
2374
2375 return 0;
2376}
2377
Huang, Ying443cd502008-06-20 16:39:21 +08002378struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Bart Van Asschede4643a2019-02-14 15:00:50 -08002379static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS);
Huang, Yingcd1a28e2008-06-23 11:20:54 +08002380int nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08002381static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
2382
2383struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
2384{
2385 return lock_classes + chain_hlocks[chain->base + i];
2386}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002387
2388/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002389 * Returns the index of the first held_lock of the current chain
2390 */
2391static inline int get_first_held_lock(struct task_struct *curr,
2392 struct held_lock *hlock)
2393{
2394 int i;
2395 struct held_lock *hlock_curr;
2396
2397 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2398 hlock_curr = curr->held_locks + i;
2399 if (hlock_curr->irq_context != hlock->irq_context)
2400 break;
2401
2402 }
2403
2404 return ++i;
2405}
2406
Borislav Petkov5c8a0102016-04-04 10:42:07 +02002407#ifdef CONFIG_DEBUG_LOCKDEP
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002408/*
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002409 * Returns the next chain_key iteration
2410 */
2411static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
2412{
2413 u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
2414
2415 printk(" class_idx:%d -> chain_key:%016Lx",
2416 class_idx,
2417 (unsigned long long)new_chain_key);
2418 return new_chain_key;
2419}
2420
2421static void
2422print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
2423{
2424 struct held_lock *hlock;
2425 u64 chain_key = 0;
2426 int depth = curr->lockdep_depth;
2427 int i;
2428
2429 printk("depth: %u\n", depth + 1);
2430 for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
2431 hlock = curr->held_locks + i;
2432 chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
2433
2434 print_lock(hlock);
2435 }
2436
2437 print_chain_key_iteration(hlock_next->class_idx, chain_key);
2438 print_lock(hlock_next);
2439}
2440
2441static void print_chain_keys_chain(struct lock_chain *chain)
2442{
2443 int i;
2444 u64 chain_key = 0;
2445 int class_id;
2446
2447 printk("depth: %u\n", chain->depth);
2448 for (i = 0; i < chain->depth; i++) {
2449 class_id = chain_hlocks[chain->base + i];
2450 chain_key = print_chain_key_iteration(class_id + 1, chain_key);
2451
2452 print_lock_name(lock_classes + class_id);
2453 printk("\n");
2454 }
2455}
2456
2457static void print_collision(struct task_struct *curr,
2458 struct held_lock *hlock_next,
2459 struct lock_chain *chain)
2460{
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002461 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002462 pr_warn("============================\n");
2463 pr_warn("WARNING: chain_key collision\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002464 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002465 pr_warn("----------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002466 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
2467 pr_warn("Hash chain already cached but the contents don't match!\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002468
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002469 pr_warn("Held locks:");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002470 print_chain_keys_held_locks(curr, hlock_next);
2471
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002472 pr_warn("Locks in cached chain:");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002473 print_chain_keys_chain(chain);
2474
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002475 pr_warn("\nstack backtrace:\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002476 dump_stack();
2477}
Borislav Petkov5c8a0102016-04-04 10:42:07 +02002478#endif
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002479
2480/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002481 * Checks whether the chain and the current held locks are consistent
2482 * in depth and also in content. If they are not it most likely means
2483 * that there was a collision during the calculation of the chain_key.
2484 * Returns: 0 not passed, 1 passed
2485 */
2486static int check_no_collision(struct task_struct *curr,
2487 struct held_lock *hlock,
2488 struct lock_chain *chain)
2489{
2490#ifdef CONFIG_DEBUG_LOCKDEP
2491 int i, j, id;
2492
2493 i = get_first_held_lock(curr, hlock);
2494
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002495 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
2496 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002497 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002498 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002499
2500 for (j = 0; j < chain->depth - 1; j++, i++) {
2501 id = curr->held_locks[i].class_idx - 1;
2502
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002503 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
2504 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002505 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02002506 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002507 }
2508#endif
2509 return 1;
2510}
2511
2512/*
Bart Van Assche22126842019-02-14 15:00:48 -08002513 * Given an index that is >= -1, return the index of the next lock chain.
2514 * Return -2 if there is no next lock chain.
2515 */
2516long lockdep_next_lockchain(long i)
2517{
Bart Van Asschede4643a2019-02-14 15:00:50 -08002518 i = find_next_bit(lock_chains_in_use, ARRAY_SIZE(lock_chains), i + 1);
2519 return i < ARRAY_SIZE(lock_chains) ? i : -2;
Bart Van Assche22126842019-02-14 15:00:48 -08002520}
2521
2522unsigned long lock_chain_count(void)
2523{
Bart Van Asschede4643a2019-02-14 15:00:50 -08002524 return bitmap_weight(lock_chains_in_use, ARRAY_SIZE(lock_chains));
2525}
2526
2527/* Must be called with the graph lock held. */
2528static struct lock_chain *alloc_lock_chain(void)
2529{
2530 int idx = find_first_zero_bit(lock_chains_in_use,
2531 ARRAY_SIZE(lock_chains));
2532
2533 if (unlikely(idx >= ARRAY_SIZE(lock_chains)))
2534 return NULL;
2535 __set_bit(idx, lock_chains_in_use);
2536 return lock_chains + idx;
Bart Van Assche22126842019-02-14 15:00:48 -08002537}
2538
2539/*
Byungchul Park545c23f2017-08-07 16:12:48 +09002540 * Adds a dependency chain into chain hashtable. And must be called with
2541 * graph_lock held.
2542 *
2543 * Return 0 if fail, and graph_lock is released.
2544 * Return 1 if succeed, with graph_lock held.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002545 */
Byungchul Park545c23f2017-08-07 16:12:48 +09002546static inline int add_chain_cache(struct task_struct *curr,
2547 struct held_lock *hlock,
2548 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002549{
Dave Jonesf82b2172008-08-11 09:30:23 +02002550 struct lock_class *class = hlock_class(hlock);
Andrew Mortona63f38c2016-02-03 13:44:12 -08002551 struct hlist_head *hash_head = chainhashentry(chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002552 struct lock_chain *chain;
Steven Rostedte0944ee2011-04-20 21:42:00 -04002553 int i, j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002554
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002555 /*
Bart Van Assche527af3e2019-02-14 15:00:49 -08002556 * The caller must hold the graph lock, ensure we've got IRQs
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002557 * disabled to make this an IRQ-safe lock.. for recursion reasons
2558 * lockdep won't complain about its own locking errors.
2559 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08002560 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2561 return 0;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002562
Bart Van Asschede4643a2019-02-14 15:00:50 -08002563 chain = alloc_lock_chain();
2564 if (!chain) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08002565 if (!debug_locks_off_graph_unlock())
2566 return 0;
2567
Dave Jones2c522832013-04-25 13:40:02 -04002568 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01002569 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002570 return 0;
2571 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002572 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08002573 chain->irq_context = hlock->irq_context;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01002574 i = get_first_held_lock(curr, hlock);
Huang, Ying443cd502008-06-20 16:39:21 +08002575 chain->depth = curr->lockdep_depth + 1 - i;
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002576
2577 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
2578 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
2579 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
2580
Steven Rostedte0944ee2011-04-20 21:42:00 -04002581 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2582 chain->base = nr_chain_hlocks;
Huang, Ying443cd502008-06-20 16:39:21 +08002583 for (j = 0; j < chain->depth - 1; j++, i++) {
Dave Jonesf82b2172008-08-11 09:30:23 +02002584 int lock_id = curr->held_locks[i].class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08002585 chain_hlocks[chain->base + j] = lock_id;
2586 }
2587 chain_hlocks[chain->base + j] = class - lock_classes;
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002588 nr_chain_hlocks += chain->depth;
Bart Van Assche523b1132019-02-14 15:00:39 -08002589 } else {
Byungchul Parkf9af4562017-01-13 11:42:04 +09002590 if (!debug_locks_off_graph_unlock())
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002591 return 0;
2592
2593 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2594 dump_stack();
2595 return 0;
2596 }
Peter Zijlstra75dd6022016-03-30 11:36:59 +02002597
Andrew Mortona63f38c2016-02-03 13:44:12 -08002598 hlist_add_head_rcu(&chain->entry, hash_head);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002599 debug_atomic_inc(chain_lookup_misses);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002600 inc_chains();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002601
2602 return 1;
2603}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002604
Byungchul Park545c23f2017-08-07 16:12:48 +09002605/*
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08002606 * Look up a dependency chain. Must be called with either the graph lock or
2607 * the RCU read lock held.
Byungchul Park545c23f2017-08-07 16:12:48 +09002608 */
2609static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
2610{
2611 struct hlist_head *hash_head = chainhashentry(chain_key);
2612 struct lock_chain *chain;
2613
Byungchul Park545c23f2017-08-07 16:12:48 +09002614 hlist_for_each_entry_rcu(chain, hash_head, entry) {
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08002615 if (READ_ONCE(chain->chain_key) == chain_key) {
Byungchul Park545c23f2017-08-07 16:12:48 +09002616 debug_atomic_inc(chain_lookup_hits);
2617 return chain;
2618 }
2619 }
2620 return NULL;
2621}
2622
2623/*
2624 * If the key is not present yet in dependency chain cache then
2625 * add it and return 1 - in this case the new dependency chain is
2626 * validated. If the key is already hashed, return 0.
2627 * (On return with 1 graph_lock is held.)
2628 */
2629static inline int lookup_chain_cache_add(struct task_struct *curr,
2630 struct held_lock *hlock,
2631 u64 chain_key)
2632{
2633 struct lock_class *class = hlock_class(hlock);
2634 struct lock_chain *chain = lookup_chain_cache(chain_key);
2635
2636 if (chain) {
2637cache_hit:
2638 if (!check_no_collision(curr, hlock, chain))
2639 return 0;
2640
2641 if (very_verbose(class)) {
2642 printk("\nhash chain already cached, key: "
Borislav Petkov04860d42018-02-26 14:49:26 +01002643 "%016Lx tail class: [%px] %s\n",
Byungchul Park545c23f2017-08-07 16:12:48 +09002644 (unsigned long long)chain_key,
2645 class->key, class->name);
2646 }
2647
2648 return 0;
2649 }
2650
2651 if (very_verbose(class)) {
Borislav Petkov04860d42018-02-26 14:49:26 +01002652 printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
Byungchul Park545c23f2017-08-07 16:12:48 +09002653 (unsigned long long)chain_key, class->key, class->name);
2654 }
2655
2656 if (!graph_lock())
2657 return 0;
2658
2659 /*
2660 * We have to walk the chain again locked - to avoid duplicates:
2661 */
2662 chain = lookup_chain_cache(chain_key);
2663 if (chain) {
2664 graph_unlock();
2665 goto cache_hit;
2666 }
2667
2668 if (!add_chain_cache(curr, hlock, chain_key))
2669 return 0;
2670
2671 return 1;
2672}
2673
Peter Zijlstra8e182572007-07-19 01:48:54 -07002674static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
Johannes Berg4e6045f2007-10-18 23:39:55 -07002675 struct held_lock *hlock, int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002676{
2677 /*
2678 * Trylock needs to maintain the stack of held locks, but it
2679 * does not add new dependencies, because trylock can be done
2680 * in any order.
2681 *
2682 * We look up the chain_key and do the O(N^2) check and update of
2683 * the dependencies only if this is a new dependency chain.
Byungchul Park545c23f2017-08-07 16:12:48 +09002684 * (If lookup_chain_cache_add() return with 1 it acquires
Peter Zijlstra8e182572007-07-19 01:48:54 -07002685 * graph_lock for us)
2686 */
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01002687 if (!hlock->trylock && hlock->check &&
Byungchul Park545c23f2017-08-07 16:12:48 +09002688 lookup_chain_cache_add(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002689 /*
2690 * Check whether last held lock:
2691 *
2692 * - is irq-safe, if this lock is irq-unsafe
2693 * - is softirq-safe, if this lock is hardirq-unsafe
2694 *
2695 * And check whether the new lock's dependency graph
2696 * could lead back to the previous lock.
2697 *
2698 * any of these scenarios could lead to a deadlock. If
2699 * All validations
2700 */
2701 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2702
2703 if (!ret)
2704 return 0;
2705 /*
2706 * Mark recursive read, as we jump over it when
2707 * building dependencies (just like we jump over
2708 * trylock entries):
2709 */
2710 if (ret == 2)
2711 hlock->read = 2;
2712 /*
2713 * Add dependency only if this lock is not the head
2714 * of the chain, and if it's not a secondary read-lock:
2715 */
Byungchul Park545c23f2017-08-07 16:12:48 +09002716 if (!chain_head && ret != 2) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002717 if (!check_prevs_add(curr, hlock))
2718 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09002719 }
2720
Peter Zijlstra8e182572007-07-19 01:48:54 -07002721 graph_unlock();
Byungchul Park545c23f2017-08-07 16:12:48 +09002722 } else {
2723 /* after lookup_chain_cache_add(): */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002724 if (unlikely(!debug_locks))
2725 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09002726 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07002727
2728 return 1;
2729}
2730#else
2731static inline int validate_chain(struct task_struct *curr,
2732 struct lockdep_map *lock, struct held_lock *hlock,
Gregory Haskins3aa416b2007-10-11 22:11:11 +02002733 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002734{
2735 return 1;
2736}
Peter Zijlstraca58abc2007-07-19 01:48:53 -07002737#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002738
2739/*
2740 * We are building curr_chain_key incrementally, so double-check
2741 * it from scratch, to make sure that it's done correctly:
2742 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02002743static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002744{
2745#ifdef CONFIG_DEBUG_LOCKDEP
2746 struct held_lock *hlock, *prev_hlock = NULL;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002747 unsigned int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002748 u64 chain_key = 0;
2749
2750 for (i = 0; i < curr->lockdep_depth; i++) {
2751 hlock = curr->held_locks + i;
2752 if (chain_key != hlock->prev_chain_key) {
2753 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002754 /*
2755 * We got mighty confused, our chain keys don't match
2756 * with what we expect, someone trample on our task state?
2757 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002758 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002759 curr->lockdep_depth, i,
2760 (unsigned long long)chain_key,
2761 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002762 return;
2763 }
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002764 /*
2765 * Whoops ran out of static storage again?
2766 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002767 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
Jarek Poplawski381a2292007-02-10 01:44:58 -08002768 return;
2769
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002770 if (prev_hlock && (prev_hlock->irq_context !=
2771 hlock->irq_context))
2772 chain_key = 0;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01002773 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002774 prev_hlock = hlock;
2775 }
2776 if (chain_key != curr->curr_chain_key) {
2777 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002778 /*
2779 * More smoking hash instead of calculating it, damn see these
2780 * numbers float.. I bet that a pink elephant stepped on my memory.
2781 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07002782 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002783 curr->lockdep_depth, i,
2784 (unsigned long long)chain_key,
2785 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002786 }
2787#endif
2788}
2789
Frederic Weisbecker0d2cc3b2019-04-02 18:02:41 +02002790static int mark_lock(struct task_struct *curr, struct held_lock *this,
2791 enum lock_usage_bit new_bit);
2792
2793#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2794
2795
Steven Rostedt282b5c22011-04-20 21:41:59 -04002796static void
2797print_usage_bug_scenario(struct held_lock *lock)
2798{
2799 struct lock_class *class = hlock_class(lock);
2800
2801 printk(" Possible unsafe locking scenario:\n\n");
2802 printk(" CPU0\n");
2803 printk(" ----\n");
2804 printk(" lock(");
2805 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002806 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002807 printk(" <Interrupt>\n");
2808 printk(" lock(");
2809 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002810 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002811 printk("\n *** DEADLOCK ***\n\n");
2812}
2813
Peter Zijlstra8e182572007-07-19 01:48:54 -07002814static int
2815print_usage_bug(struct task_struct *curr, struct held_lock *this,
2816 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2817{
2818 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2819 return 0;
2820
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002821 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002822 pr_warn("================================\n");
2823 pr_warn("WARNING: inconsistent lock state\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002824 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002825 pr_warn("--------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002826
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002827 pr_warn("inconsistent {%s} -> {%s} usage.\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07002828 usage_str[prev_bit], usage_str[new_bit]);
2829
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002830 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002831 curr->comm, task_pid_nr(curr),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002832 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2833 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2834 trace_hardirqs_enabled(curr),
2835 trace_softirqs_enabled(curr));
2836 print_lock(this);
2837
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002838 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
Dave Jonesf82b2172008-08-11 09:30:23 +02002839 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002840
2841 print_irqtrace_events(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002842 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04002843 print_usage_bug_scenario(this);
2844
Peter Zijlstra8e182572007-07-19 01:48:54 -07002845 lockdep_print_held_locks(curr);
2846
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002847 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002848 dump_stack();
2849
2850 return 0;
2851}
2852
2853/*
2854 * Print out an error if an invalid bit is set:
2855 */
2856static inline int
2857valid_state(struct task_struct *curr, struct held_lock *this,
2858 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2859{
Dave Jonesf82b2172008-08-11 09:30:23 +02002860 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002861 return print_usage_bug(curr, this, bad_bit, new_bit);
2862 return 1;
2863}
2864
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002865
2866/*
2867 * print irq inversion bug:
2868 */
2869static int
Ming Lei24208ca2009-07-16 15:44:29 +02002870print_irq_inversion_bug(struct task_struct *curr,
2871 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002872 struct held_lock *this, int forwards,
2873 const char *irqclass)
2874{
Steven Rostedtdad3d742011-04-20 21:41:57 -04002875 struct lock_list *entry = other;
2876 struct lock_list *middle = NULL;
2877 int depth;
2878
Ingo Molnar74c383f2006-12-13 00:34:43 -08002879 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002880 return 0;
2881
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002882 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002883 pr_warn("========================================================\n");
2884 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002885 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002886 pr_warn("--------------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002887 pr_warn("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002888 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002889 print_lock(this);
2890 if (forwards)
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002891 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002892 else
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002893 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002894 print_lock_name(other->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002895 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002896
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002897 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04002898
2899 /* Find a middle lock (if one exists) */
2900 depth = get_lock_depth(other);
2901 do {
2902 if (depth == 0 && (entry != root)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002903 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
Steven Rostedtdad3d742011-04-20 21:41:57 -04002904 break;
2905 }
2906 middle = entry;
2907 entry = get_lock_parent(entry);
2908 depth--;
2909 } while (entry && entry != root && (depth >= 0));
2910 if (forwards)
2911 print_irq_lock_scenario(root, other,
2912 middle ? middle->class : root->class, other->class);
2913 else
2914 print_irq_lock_scenario(other, root,
2915 middle ? middle->class : other->class, root->class);
2916
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002917 lockdep_print_held_locks(curr);
2918
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002919 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
Ming Lei24208ca2009-07-16 15:44:29 +02002920 if (!save_trace(&root->trace))
2921 return 0;
2922 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002923
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002924 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002925 dump_stack();
2926
2927 return 0;
2928}
2929
2930/*
2931 * Prove that in the forwards-direction subgraph starting at <this>
2932 * there is no lock matching <mask>:
2933 */
2934static int
2935check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2936 enum lock_usage_bit bit, const char *irqclass)
2937{
2938 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002939 struct lock_list root;
2940 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002941
Ming Leid7aaba12009-07-16 15:44:29 +02002942 root.parent = NULL;
2943 root.class = hlock_class(this);
2944 ret = find_usage_forwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002945 if (ret < 0)
2946 return print_bfs_bug(ret);
2947 if (ret == 1)
2948 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002949
Ming Lei24208ca2009-07-16 15:44:29 +02002950 return print_irq_inversion_bug(curr, &root, target_entry,
Ming Leid7aaba12009-07-16 15:44:29 +02002951 this, 1, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002952}
2953
2954/*
2955 * Prove that in the backwards-direction subgraph starting at <this>
2956 * there is no lock matching <mask>:
2957 */
2958static int
2959check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2960 enum lock_usage_bit bit, const char *irqclass)
2961{
2962 int ret;
Ming Leid7aaba12009-07-16 15:44:29 +02002963 struct lock_list root;
2964 struct lock_list *uninitialized_var(target_entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002965
Ming Leid7aaba12009-07-16 15:44:29 +02002966 root.parent = NULL;
2967 root.class = hlock_class(this);
2968 ret = find_usage_backwards(&root, bit, &target_entry);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002969 if (ret < 0)
2970 return print_bfs_bug(ret);
2971 if (ret == 1)
2972 return ret;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002973
Ming Lei24208ca2009-07-16 15:44:29 +02002974 return print_irq_inversion_bug(curr, &root, target_entry,
Oleg Nesterov48d50672010-01-26 19:16:41 +01002975 this, 0, irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002976}
2977
Ingo Molnar3117df02006-12-13 00:34:43 -08002978void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002979{
2980 printk("irq event stamp: %u\n", curr->irq_events);
Borislav Petkov04860d42018-02-26 14:49:26 +01002981 printk("hardirqs last enabled at (%u): [<%px>] %pS\n",
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002982 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
2983 (void *)curr->hardirq_enable_ip);
Borislav Petkov04860d42018-02-26 14:49:26 +01002984 printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002985 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
2986 (void *)curr->hardirq_disable_ip);
Borislav Petkov04860d42018-02-26 14:49:26 +01002987 printk("softirqs last enabled at (%u): [<%px>] %pS\n",
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002988 curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
2989 (void *)curr->softirq_enable_ip);
Borislav Petkov04860d42018-02-26 14:49:26 +01002990 printk("softirqs last disabled at (%u): [<%px>] %pS\n",
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002991 curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
2992 (void *)curr->softirq_disable_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002993}
2994
Peter Zijlstracd953022009-01-22 16:38:21 +01002995static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002996{
Peter Zijlstra8e182572007-07-19 01:48:54 -07002997#if HARDIRQ_VERBOSE
2998 return class_filter(class);
2999#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003000 return 0;
3001}
3002
Peter Zijlstracd953022009-01-22 16:38:21 +01003003static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003004{
Peter Zijlstra8e182572007-07-19 01:48:54 -07003005#if SOFTIRQ_VERBOSE
3006 return class_filter(class);
3007#endif
3008 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003009}
3010
3011#define STRICT_READ_CHECKS 1
3012
Peter Zijlstracd953022009-01-22 16:38:21 +01003013static int (*state_verbose_f[])(struct lock_class *class) = {
3014#define LOCKDEP_STATE(__STATE) \
3015 __STATE##_verbose,
3016#include "lockdep_states.h"
3017#undef LOCKDEP_STATE
3018};
3019
3020static inline int state_verbose(enum lock_usage_bit bit,
3021 struct lock_class *class)
3022{
Frederic Weisbeckerc902a1e2019-04-02 18:02:42 +02003023 return state_verbose_f[bit >> LOCK_USAGE_DIR_MASK](class);
Peter Zijlstracd953022009-01-22 16:38:21 +01003024}
3025
Peter Zijlstra42c50d52009-01-22 16:58:16 +01003026typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
3027 enum lock_usage_bit bit, const char *name);
3028
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01003029static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01003030mark_lock_irq(struct task_struct *curr, struct held_lock *this,
3031 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01003032{
Peter Zijlstraf9892092009-01-22 16:09:59 +01003033 int excl_bit = exclusive_bit(new_bit);
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01003034 int read = new_bit & LOCK_USAGE_READ_MASK;
3035 int dir = new_bit & LOCK_USAGE_DIR_MASK;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01003036
Peter Zijlstra38aa2712009-01-27 14:53:50 +01003037 /*
3038 * mark USED_IN has to look forwards -- to ensure no dependency
3039 * has ENABLED state, which would allow recursion deadlocks.
3040 *
3041 * mark ENABLED has to look backwards -- to ensure no dependee
3042 * has USED_IN state, which, again, would allow recursion deadlocks.
3043 */
Peter Zijlstra42c50d52009-01-22 16:58:16 +01003044 check_usage_f usage = dir ?
3045 check_usage_backwards : check_usage_forwards;
Peter Zijlstraf9892092009-01-22 16:09:59 +01003046
Peter Zijlstra38aa2712009-01-27 14:53:50 +01003047 /*
3048 * Validate that this particular lock does not have conflicting
3049 * usage states.
3050 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01003051 if (!valid_state(curr, this, new_bit, excl_bit))
3052 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01003053
Peter Zijlstra38aa2712009-01-27 14:53:50 +01003054 /*
3055 * Validate that the lock dependencies don't have conflicting usage
3056 * states.
3057 */
3058 if ((!read || !dir || STRICT_READ_CHECKS) &&
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01003059 !usage(curr, this, excl_bit, state_name(new_bit & ~LOCK_USAGE_READ_MASK)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01003060 return 0;
Peter Zijlstra780e8202009-01-22 16:51:29 +01003061
Peter Zijlstra38aa2712009-01-27 14:53:50 +01003062 /*
3063 * Check for read in write conflicts
3064 */
3065 if (!read) {
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01003066 if (!valid_state(curr, this, new_bit, excl_bit + LOCK_USAGE_READ_MASK))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01003067 return 0;
3068
3069 if (STRICT_READ_CHECKS &&
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01003070 !usage(curr, this, excl_bit + LOCK_USAGE_READ_MASK,
3071 state_name(new_bit + LOCK_USAGE_READ_MASK)))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01003072 return 0;
3073 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01003074
Peter Zijlstracd953022009-01-22 16:38:21 +01003075 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01003076 return 2;
3077
3078 return 1;
3079}
3080
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003081/*
3082 * Mark all held locks with a usage bit:
3083 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003084static int
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01003085mark_held_locks(struct task_struct *curr, enum lock_usage_bit base_bit)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003086{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003087 struct held_lock *hlock;
3088 int i;
3089
3090 for (i = 0; i < curr->lockdep_depth; i++) {
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01003091 enum lock_usage_bit hlock_bit = base_bit;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003092 hlock = curr->held_locks + i;
3093
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01003094 if (hlock->read)
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01003095 hlock_bit += LOCK_USAGE_READ_MASK;
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01003096
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01003097 BUG_ON(hlock_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01003098
Oleg Nesterov34d0ed52014-01-20 19:20:13 +01003099 if (!hlock->check)
Peter Zijlstraefbe2ee2011-07-07 11:39:45 +02003100 continue;
3101
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01003102 if (!mark_lock(curr, hlock, hlock_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003103 return 0;
3104 }
3105
3106 return 1;
3107}
3108
3109/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003110 * Hardirqs will be enabled:
3111 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02003112static void __trace_hardirqs_on_caller(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003113{
3114 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003115
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003116 /* we'll do an OFF -> ON transition: */
3117 curr->hardirqs_enabled = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003118
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003119 /*
3120 * We are going to turn hardirqs on, so set the
3121 * usage bit for all held locks:
3122 */
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01003123 if (!mark_held_locks(curr, LOCK_ENABLED_HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003124 return;
3125 /*
3126 * If we have softirqs enabled, then set the usage
3127 * bit for all held locks. (disabled hardirqs prevented
3128 * this bit from being set before)
3129 */
3130 if (curr->softirqs_enabled)
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01003131 if (!mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003132 return;
3133
3134 curr->hardirq_enable_ip = ip;
3135 curr->hardirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003136 debug_atomic_inc(hardirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003137}
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02003138
Steven Rostedt (VMware)bff1b202018-08-06 15:50:58 -04003139void lockdep_hardirqs_on(unsigned long ip)
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02003140{
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02003141 if (unlikely(!debug_locks || current->lockdep_recursion))
3142 return;
3143
Peter Zijlstra7d36b262011-07-26 13:13:44 +02003144 if (unlikely(current->hardirqs_enabled)) {
3145 /*
3146 * Neither irq nor preemption are disabled here
3147 * so this is racy by nature but losing one hit
3148 * in a stat is not a big deal.
3149 */
3150 __debug_atomic_inc(redundant_hardirqs_on);
3151 return;
3152 }
3153
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003154 /*
3155 * We're enabling irqs and according to our state above irqs weren't
3156 * already enabled, yet we find the hardware thinks they are in fact
3157 * enabled.. someone messed up their IRQ state tracing.
3158 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02003159 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3160 return;
3161
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003162 /*
3163 * See the fine text that goes along with this variable definition.
3164 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02003165 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
3166 return;
3167
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003168 /*
3169 * Can't allow enabling interrupts while in an interrupt handler,
3170 * that's general bad form and such. Recursion, limited stack etc..
3171 */
Peter Zijlstra7d36b262011-07-26 13:13:44 +02003172 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
3173 return;
3174
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02003175 current->lockdep_recursion = 1;
3176 __trace_hardirqs_on_caller(ip);
3177 current->lockdep_recursion = 0;
3178}
Masami Hiramatsu2f43c602019-02-13 01:15:05 +09003179NOKPROBE_SYMBOL(lockdep_hardirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003180
3181/*
3182 * Hardirqs were disabled:
3183 */
Steven Rostedt (VMware)bff1b202018-08-06 15:50:58 -04003184void lockdep_hardirqs_off(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003185{
3186 struct task_struct *curr = current;
3187
3188 if (unlikely(!debug_locks || current->lockdep_recursion))
3189 return;
3190
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003191 /*
3192 * So we're supposed to get called after you mask local IRQs, but for
3193 * some reason the hardware doesn't quite think you did a proper job.
3194 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003195 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3196 return;
3197
3198 if (curr->hardirqs_enabled) {
3199 /*
3200 * We have done an ON -> OFF transition:
3201 */
3202 curr->hardirqs_enabled = 0;
Heiko Carstens6afe40b2008-10-28 11:14:58 +01003203 curr->hardirq_disable_ip = ip;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003204 curr->hardirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003205 debug_atomic_inc(hardirqs_off_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003206 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003207 debug_atomic_inc(redundant_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003208}
Masami Hiramatsu2f43c602019-02-13 01:15:05 +09003209NOKPROBE_SYMBOL(lockdep_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003210
3211/*
3212 * Softirqs will be enabled:
3213 */
3214void trace_softirqs_on(unsigned long ip)
3215{
3216 struct task_struct *curr = current;
3217
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02003218 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003219 return;
3220
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003221 /*
3222 * We fancy IRQs being disabled here, see softirq.c, avoids
3223 * funny state and nesting things.
3224 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003225 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3226 return;
3227
3228 if (curr->softirqs_enabled) {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003229 debug_atomic_inc(redundant_softirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003230 return;
3231 }
3232
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02003233 current->lockdep_recursion = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003234 /*
3235 * We'll do an OFF -> ON transition:
3236 */
3237 curr->softirqs_enabled = 1;
3238 curr->softirq_enable_ip = ip;
3239 curr->softirq_enable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003240 debug_atomic_inc(softirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003241 /*
3242 * We are going to turn softirqs on, so set the
3243 * usage bit for all held locks, if hardirqs are
3244 * enabled too:
3245 */
3246 if (curr->hardirqs_enabled)
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01003247 mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ);
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02003248 current->lockdep_recursion = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003249}
3250
3251/*
3252 * Softirqs were disabled:
3253 */
3254void trace_softirqs_off(unsigned long ip)
3255{
3256 struct task_struct *curr = current;
3257
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02003258 if (unlikely(!debug_locks || current->lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003259 return;
3260
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003261 /*
3262 * We fancy IRQs being disabled here, see softirq.c
3263 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003264 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3265 return;
3266
3267 if (curr->softirqs_enabled) {
3268 /*
3269 * We have done an ON -> OFF transition:
3270 */
3271 curr->softirqs_enabled = 0;
3272 curr->softirq_disable_ip = ip;
3273 curr->softirq_disable_event = ++curr->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003274 debug_atomic_inc(softirqs_off_events);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003275 /*
3276 * Whoops, we wanted softirqs off, so why aren't they?
3277 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003278 DEBUG_LOCKS_WARN_ON(!softirq_count());
3279 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003280 debug_atomic_inc(redundant_softirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003281}
3282
Peter Zijlstra8e182572007-07-19 01:48:54 -07003283static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
3284{
3285 /*
3286 * If non-trylock use in a hardirq or softirq context, then
3287 * mark the lock as used in these contexts:
3288 */
3289 if (!hlock->trylock) {
3290 if (hlock->read) {
3291 if (curr->hardirq_context)
3292 if (!mark_lock(curr, hlock,
3293 LOCK_USED_IN_HARDIRQ_READ))
3294 return 0;
3295 if (curr->softirq_context)
3296 if (!mark_lock(curr, hlock,
3297 LOCK_USED_IN_SOFTIRQ_READ))
3298 return 0;
3299 } else {
3300 if (curr->hardirq_context)
3301 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
3302 return 0;
3303 if (curr->softirq_context)
3304 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
3305 return 0;
3306 }
3307 }
3308 if (!hlock->hardirqs_off) {
3309 if (hlock->read) {
3310 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003311 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003312 return 0;
3313 if (curr->softirqs_enabled)
3314 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003315 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003316 return 0;
3317 } else {
3318 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003319 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003320 return 0;
3321 if (curr->softirqs_enabled)
3322 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01003323 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003324 return 0;
3325 }
3326 }
3327
3328 return 1;
3329}
3330
Boqun Fengc2469752016-02-16 13:57:40 +08003331static inline unsigned int task_irq_context(struct task_struct *task)
3332{
3333 return 2 * !!task->hardirq_context + !!task->softirq_context;
3334}
3335
Peter Zijlstra8e182572007-07-19 01:48:54 -07003336static int separate_irq_context(struct task_struct *curr,
3337 struct held_lock *hlock)
3338{
3339 unsigned int depth = curr->lockdep_depth;
3340
3341 /*
3342 * Keep track of points where we cross into an interrupt context:
3343 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003344 if (depth) {
3345 struct held_lock *prev_hlock;
3346
3347 prev_hlock = curr->held_locks + depth-1;
3348 /*
3349 * If we cross into another context, reset the
3350 * hash key (this also prevents the checking and the
3351 * adding of the dependency to 'prev'):
3352 */
3353 if (prev_hlock->irq_context != hlock->irq_context)
3354 return 1;
3355 }
3356 return 0;
3357}
3358
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003359#else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003360
3361static inline
3362int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
3363 enum lock_usage_bit new_bit)
3364{
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003365 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003366 return 1;
3367}
3368
3369static inline int mark_irqflags(struct task_struct *curr,
3370 struct held_lock *hlock)
3371{
3372 return 1;
3373}
3374
Boqun Fengc2469752016-02-16 13:57:40 +08003375static inline unsigned int task_irq_context(struct task_struct *task)
3376{
3377 return 0;
3378}
3379
Peter Zijlstra8e182572007-07-19 01:48:54 -07003380static inline int separate_irq_context(struct task_struct *curr,
3381 struct held_lock *hlock)
3382{
3383 return 0;
3384}
3385
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003386#endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003387
3388/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07003389 * Mark a lock with a usage bit, and validate the state transition:
3390 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003391static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02003392 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07003393{
3394 unsigned int new_mask = 1 << new_bit, ret = 1;
3395
3396 /*
3397 * If already set then do not dirty the cacheline,
3398 * nor do any checks:
3399 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003400 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003401 return 1;
3402
3403 if (!graph_lock())
3404 return 0;
3405 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003406 * Make sure we didn't race:
Peter Zijlstra8e182572007-07-19 01:48:54 -07003407 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003408 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07003409 graph_unlock();
3410 return 1;
3411 }
3412
Dave Jonesf82b2172008-08-11 09:30:23 +02003413 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003414
Dave Jonesf82b2172008-08-11 09:30:23 +02003415 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003416 return 0;
3417
3418 switch (new_bit) {
Peter Zijlstra53464172009-01-22 14:15:53 +01003419#define LOCKDEP_STATE(__STATE) \
3420 case LOCK_USED_IN_##__STATE: \
3421 case LOCK_USED_IN_##__STATE##_READ: \
3422 case LOCK_ENABLED_##__STATE: \
3423 case LOCK_ENABLED_##__STATE##_READ:
3424#include "lockdep_states.h"
3425#undef LOCKDEP_STATE
Peter Zijlstra8e182572007-07-19 01:48:54 -07003426 ret = mark_lock_irq(curr, this, new_bit);
3427 if (!ret)
3428 return 0;
3429 break;
3430 case LOCK_USED:
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003431 debug_atomic_dec(nr_unused_locks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07003432 break;
3433 default:
3434 if (!debug_locks_off_graph_unlock())
3435 return 0;
3436 WARN_ON(1);
3437 return 0;
3438 }
3439
3440 graph_unlock();
3441
3442 /*
3443 * We must printk outside of the graph_lock:
3444 */
3445 if (ret == 2) {
3446 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
3447 print_lock(this);
3448 print_irqtrace_events(curr);
3449 dump_stack();
3450 }
3451
3452 return ret;
3453}
3454
3455/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003456 * Initialize a lock instance's lock-class mapping info:
3457 */
Bart Van Assched35568b2018-12-06 17:11:33 -08003458void lockdep_init_map(struct lockdep_map *lock, const char *name,
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003459 struct lock_class_key *key, int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003460{
Yong Zhangd3d03d42011-11-09 16:04:51 +08003461 int i;
3462
Yong Zhangd3d03d42011-11-09 16:04:51 +08003463 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
3464 lock->class_cache[i] = NULL;
Hitoshi Mitake62016252010-10-05 18:01:51 +09003465
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003466#ifdef CONFIG_LOCK_STAT
3467 lock->cpu = raw_smp_processor_id();
3468#endif
3469
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003470 /*
3471 * Can't be having no nameless bastards around this place!
3472 */
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003473 if (DEBUG_LOCKS_WARN_ON(!name)) {
3474 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003475 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003476 }
3477
3478 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003479
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003480 /*
3481 * No key, no joy, we need to hash something.
3482 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003483 if (DEBUG_LOCKS_WARN_ON(!key))
3484 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003485 /*
Bart Van Assche108c1482019-02-14 15:00:53 -08003486 * Sanity check, the lock-class key must either have been allocated
3487 * statically or must have been registered as a dynamic key.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003488 */
Bart Van Assche108c1482019-02-14 15:00:53 -08003489 if (!static_obj(key) && !is_dynamic_key(key)) {
3490 if (debug_locks)
3491 printk(KERN_ERR "BUG: key %px has not been registered!\n", key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003492 DEBUG_LOCKS_WARN_ON(1);
3493 return;
3494 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003495 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02003496
3497 if (unlikely(!debug_locks))
3498 return;
3499
Peter Zijlstra35a93932015-02-26 16:23:11 +01003500 if (subclass) {
3501 unsigned long flags;
3502
3503 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3504 return;
3505
3506 raw_local_irq_save(flags);
3507 current->lockdep_recursion = 1;
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003508 register_lock_class(lock, subclass, 1);
Peter Zijlstra35a93932015-02-26 16:23:11 +01003509 current->lockdep_recursion = 0;
3510 raw_local_irq_restore(flags);
3511 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003512}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003513EXPORT_SYMBOL_GPL(lockdep_init_map);
3514
Peter Zijlstra1704f472010-03-19 01:37:42 +01003515struct lock_class_key __lockdep_no_validate__;
Kent Overstreetea6749c2012-12-27 22:21:58 -08003516EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
Peter Zijlstra1704f472010-03-19 01:37:42 +01003517
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003518static int
3519print_lock_nested_lock_not_held(struct task_struct *curr,
3520 struct held_lock *hlock,
3521 unsigned long ip)
3522{
3523 if (!debug_locks_off())
3524 return 0;
3525 if (debug_locks_silent)
3526 return 0;
3527
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003528 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003529 pr_warn("==================================\n");
3530 pr_warn("WARNING: Nested lock was not taken\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003531 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003532 pr_warn("----------------------------------\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003533
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003534 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003535 print_lock(hlock);
3536
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003537 pr_warn("\nbut this task is not holding:\n");
3538 pr_warn("%s\n", hlock->nest_lock->name);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003539
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003540 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003541 dump_stack();
3542
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003543 pr_warn("\nother info that might help us debug this:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003544 lockdep_print_held_locks(curr);
3545
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003546 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003547 dump_stack();
3548
3549 return 0;
3550}
3551
Matthew Wilcox08f36ff2018-01-17 07:14:13 -08003552static int __lock_is_held(const struct lockdep_map *lock, int read);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003553
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003554/*
3555 * This gets called for every mutex_lock*()/spin_lock*() operation.
3556 * We maintain the dependency maps and validate the locking attempt:
Waiman Long8ee10862018-10-02 16:19:17 -04003557 *
3558 * The callers must make sure that IRQs are disabled before calling it,
3559 * otherwise we could get an interrupt which would want to take locks,
3560 * which would end up in lockdep again.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003561 */
3562static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3563 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003564 struct lockdep_map *nest_lock, unsigned long ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02003565 int references, int pin_count)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003566{
3567 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07003568 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003569 struct held_lock *hlock;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003570 unsigned int depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003571 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003572 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003573 u64 chain_key;
3574
3575 if (unlikely(!debug_locks))
3576 return 0;
3577
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003578 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3579 check = 0;
Peter Zijlstra1704f472010-03-19 01:37:42 +01003580
Hitoshi Mitake62016252010-10-05 18:01:51 +09003581 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3582 class = lock->class_cache[subclass];
Ingo Molnard6d897c2006-07-10 04:44:04 -07003583 /*
Hitoshi Mitake62016252010-10-05 18:01:51 +09003584 * Not cached?
Ingo Molnard6d897c2006-07-10 04:44:04 -07003585 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003586 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04003587 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003588 if (!class)
3589 return 0;
3590 }
Waiman Long8ca2b56c2018-10-03 13:07:18 -04003591
3592 debug_class_ops_inc(class);
3593
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003594 if (very_verbose(class)) {
Borislav Petkov04860d42018-02-26 14:49:26 +01003595 printk("\nacquire class [%px] %s", class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003596 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01003597 printk(KERN_CONT "#%d", class->name_version);
3598 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003599 dump_stack();
3600 }
3601
3602 /*
3603 * Add the lock to the list of currently held locks.
3604 * (we dont increase the depth just yet, up until the
3605 * dependency checks are done)
3606 */
3607 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003608 /*
3609 * Ran out of static storage for our per-task lock stack again have we?
3610 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003611 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3612 return 0;
3613
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003614 class_idx = class - lock_classes + 1;
3615
Ingo Molnare966eae2017-12-12 12:31:16 +01003616 if (depth) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003617 hlock = curr->held_locks + depth - 1;
3618 if (hlock->class_idx == class_idx && nest_lock) {
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003619 if (hlock->references) {
3620 /*
3621 * Check: unsigned int references:12, overflow.
3622 */
3623 if (DEBUG_LOCKS_WARN_ON(hlock->references == (1 << 12)-1))
3624 return 0;
3625
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003626 hlock->references++;
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003627 } else {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003628 hlock->references = 2;
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01003629 }
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003630
3631 return 1;
3632 }
3633 }
3634
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003635 hlock = curr->held_locks + depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003636 /*
3637 * Plain impossible, we just registered it and checked it weren't no
3638 * NULL like.. I bet this mushroom I ate was good!
3639 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003640 if (DEBUG_LOCKS_WARN_ON(!class))
3641 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003642 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003643 hlock->acquire_ip = ip;
3644 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02003645 hlock->nest_lock = nest_lock;
Boqun Fengc2469752016-02-16 13:57:40 +08003646 hlock->irq_context = task_irq_context(curr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003647 hlock->trylock = trylock;
3648 hlock->read = read;
3649 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04003650 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003651 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003652#ifdef CONFIG_LOCK_STAT
3653 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02003654 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003655#endif
Peter Zijlstra21199f22015-09-16 16:10:40 +02003656 hlock->pin_count = pin_count;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003657
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003658 if (check && !mark_irqflags(curr, hlock))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003659 return 0;
3660
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003661 /* mark it as used: */
Jarek Poplawski4ff773bb2007-05-08 00:31:00 -07003662 if (!mark_lock(curr, hlock, LOCK_USED))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003663 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003664
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003665 /*
Gautham R Shenoy17aacfb92007-10-28 20:47:01 +01003666 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003667 * lock keys along the dependency chain. We save the hash value
3668 * at every step so that we can get the current hash easily
3669 * after unlock. The chain hash is then used to cache dependency
3670 * results.
3671 *
3672 * The 'key ID' is what is the most compact key value to drive
3673 * the hash, not class->key.
3674 */
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003675 /*
3676 * Whoops, we did it again.. ran straight out of our static allocation.
3677 */
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003678 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003679 return 0;
3680
3681 chain_key = curr->curr_chain_key;
3682 if (!depth) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003683 /*
3684 * How can we have a chain hash when we ain't got no keys?!
3685 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003686 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3687 return 0;
3688 chain_head = 1;
3689 }
3690
3691 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003692 if (separate_irq_context(curr, hlock)) {
3693 chain_key = 0;
3694 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003695 }
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003696 chain_key = iterate_chain_key(chain_key, class_idx);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003697
Peter Zijlstraf8319482016-11-30 14:32:25 +11003698 if (nest_lock && !__lock_is_held(nest_lock, -1))
Maarten Lankhorstd0945952012-09-13 11:39:51 +02003699 return print_lock_nested_lock_not_held(curr, hlock, ip);
3700
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08003701 if (!debug_locks_silent) {
3702 WARN_ON_ONCE(depth && !hlock_class(hlock - 1)->key);
3703 WARN_ON_ONCE(!hlock_class(hlock)->key);
3704 }
3705
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003706 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003707 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08003708
Gregory Haskins3aa416b2007-10-11 22:11:11 +02003709 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003710 curr->lockdep_depth++;
3711 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08003712#ifdef CONFIG_DEBUG_LOCKDEP
3713 if (unlikely(!debug_locks))
3714 return 0;
3715#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003716 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3717 debug_locks_off();
Dave Jones2c522832013-04-25 13:40:02 -04003718 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3719 printk(KERN_DEBUG "depth: %i max: %lu!\n",
Ben Greearc0540602013-02-06 10:56:19 -08003720 curr->lockdep_depth, MAX_LOCK_DEPTH);
Ben Greearc0540602013-02-06 10:56:19 -08003721
3722 lockdep_print_held_locks(current);
3723 debug_show_all_locks();
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01003724 dump_stack();
Ben Greearc0540602013-02-06 10:56:19 -08003725
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003726 return 0;
3727 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08003728
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003729 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3730 max_lockdep_depth = curr->lockdep_depth;
3731
3732 return 1;
3733}
3734
3735static int
Srivatsa S. Bhatf86f7552013-01-08 18:35:58 +05303736print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003737 unsigned long ip)
3738{
3739 if (!debug_locks_off())
3740 return 0;
3741 if (debug_locks_silent)
3742 return 0;
3743
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003744 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003745 pr_warn("=====================================\n");
3746 pr_warn("WARNING: bad unlock balance detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003747 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003748 pr_warn("-------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003749 pr_warn("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003750 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003751 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003752 pr_cont(") at:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003753 print_ip_sym(ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003754 pr_warn("but there are no more locks to release!\n");
3755 pr_warn("\nother info that might help us debug this:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003756 lockdep_print_held_locks(curr);
3757
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003758 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003759 dump_stack();
3760
3761 return 0;
3762}
3763
Matthew Wilcox08f36ff2018-01-17 07:14:13 -08003764static int match_held_lock(const struct held_lock *hlock,
3765 const struct lockdep_map *lock)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003766{
3767 if (hlock->instance == lock)
3768 return 1;
3769
3770 if (hlock->references) {
Matthew Wilcox08f36ff2018-01-17 07:14:13 -08003771 const struct lock_class *class = lock->class_cache[0];
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003772
3773 if (!class)
3774 class = look_up_lock_class(lock, 0);
3775
Peter Zijlstra80e04012011-08-05 14:26:17 +02003776 /*
3777 * If look_up_lock_class() failed to find a class, we're trying
3778 * to test if we hold a lock that has never yet been acquired.
3779 * Clearly if the lock hasn't been acquired _ever_, we're not
3780 * holding it either, so report failure.
3781 */
Matthew Wilcox64f29d12018-01-17 07:14:12 -08003782 if (!class)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003783 return 0;
3784
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003785 /*
3786 * References, but not a lock we're actually ref-counting?
3787 * State got messed up, follow the sites that change ->references
3788 * and try to make sense of it.
3789 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003790 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3791 return 0;
3792
3793 if (hlock->class_idx == class - lock_classes + 1)
3794 return 1;
3795 }
3796
3797 return 0;
3798}
3799
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003800/* @depth must not be zero */
3801static struct held_lock *find_held_lock(struct task_struct *curr,
3802 struct lockdep_map *lock,
3803 unsigned int depth, int *idx)
3804{
3805 struct held_lock *ret, *hlock, *prev_hlock;
3806 int i;
3807
3808 i = depth - 1;
3809 hlock = curr->held_locks + i;
3810 ret = hlock;
3811 if (match_held_lock(hlock, lock))
3812 goto out;
3813
3814 ret = NULL;
3815 for (i--, prev_hlock = hlock--;
3816 i >= 0;
3817 i--, prev_hlock = hlock--) {
3818 /*
3819 * We must not cross into another context:
3820 */
3821 if (prev_hlock->irq_context != hlock->irq_context) {
3822 ret = NULL;
3823 break;
3824 }
3825 if (match_held_lock(hlock, lock)) {
3826 ret = hlock;
3827 break;
3828 }
3829 }
3830
3831out:
3832 *idx = i;
3833 return ret;
3834}
3835
J. R. Okajimae9699702017-02-03 01:38:16 +09003836static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
3837 int idx)
3838{
3839 struct held_lock *hlock;
3840
Waiman Long8ee10862018-10-02 16:19:17 -04003841 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3842 return 0;
3843
J. R. Okajimae9699702017-02-03 01:38:16 +09003844 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
3845 if (!__lock_acquire(hlock->instance,
3846 hlock_class(hlock)->subclass,
3847 hlock->trylock,
3848 hlock->read, hlock->check,
3849 hlock->hardirqs_off,
3850 hlock->nest_lock, hlock->acquire_ip,
3851 hlock->references, hlock->pin_count))
3852 return 1;
3853 }
3854 return 0;
3855}
3856
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003857static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003858__lock_set_class(struct lockdep_map *lock, const char *name,
3859 struct lock_class_key *key, unsigned int subclass,
3860 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003861{
3862 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003863 struct held_lock *hlock;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003864 struct lock_class *class;
3865 unsigned int depth;
3866 int i;
3867
Waiman Long513e1072019-01-09 23:03:25 -05003868 if (unlikely(!debug_locks))
3869 return 0;
3870
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003871 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003872 /*
3873 * This function is about (re)setting the class of a held lock,
3874 * yet we're not actually holding any locks. Naughty user!
3875 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003876 if (DEBUG_LOCKS_WARN_ON(!depth))
3877 return 0;
3878
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003879 hlock = find_held_lock(curr, lock, depth, &i);
3880 if (!hlock)
3881 return print_unlock_imbalance_bug(curr, lock, ip);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003882
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01003883 lockdep_init_map(lock, name, key, 0);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003884 class = register_lock_class(lock, subclass, 0);
Dave Jonesf82b2172008-08-11 09:30:23 +02003885 hlock->class_idx = class - lock_classes + 1;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003886
3887 curr->lockdep_depth = i;
3888 curr->curr_chain_key = hlock->prev_chain_key;
3889
J. R. Okajimae9699702017-02-03 01:38:16 +09003890 if (reacquire_held_locks(curr, depth, i))
3891 return 0;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003892
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003893 /*
3894 * I took it apart and put it back together again, except now I have
3895 * these 'spare' parts.. where shall I put them.
3896 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02003897 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3898 return 0;
3899 return 1;
3900}
3901
J. R. Okajima6419c4a2017-02-03 01:38:17 +09003902static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3903{
3904 struct task_struct *curr = current;
3905 struct held_lock *hlock;
3906 unsigned int depth;
3907 int i;
3908
Waiman Long71492582019-01-09 23:03:25 -05003909 if (unlikely(!debug_locks))
3910 return 0;
3911
J. R. Okajima6419c4a2017-02-03 01:38:17 +09003912 depth = curr->lockdep_depth;
3913 /*
3914 * This function is about (re)setting the class of a held lock,
3915 * yet we're not actually holding any locks. Naughty user!
3916 */
3917 if (DEBUG_LOCKS_WARN_ON(!depth))
3918 return 0;
3919
3920 hlock = find_held_lock(curr, lock, depth, &i);
3921 if (!hlock)
3922 return print_unlock_imbalance_bug(curr, lock, ip);
3923
3924 curr->lockdep_depth = i;
3925 curr->curr_chain_key = hlock->prev_chain_key;
3926
3927 WARN(hlock->read, "downgrading a read lock");
3928 hlock->read = 1;
3929 hlock->acquire_ip = ip;
3930
3931 if (reacquire_held_locks(curr, depth, i))
3932 return 0;
3933
3934 /*
3935 * I took it apart and put it back together again, except now I have
3936 * these 'spare' parts.. where shall I put them.
3937 */
3938 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3939 return 0;
3940 return 1;
3941}
3942
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003943/*
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003944 * Remove the lock to the list of currently held locks - this gets
3945 * called on mutex_unlock()/spin_unlock*() (or on a failed
3946 * mutex_lock_interruptible()).
3947 *
3948 * @nested is an hysterical artifact, needs a tree wide cleanup.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003949 */
3950static int
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003951__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003952{
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003953 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003954 struct held_lock *hlock;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003955 unsigned int depth;
Ingo Molnare966eae2017-12-12 12:31:16 +01003956 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003957
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003958 if (unlikely(!debug_locks))
3959 return 0;
3960
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003961 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003962 /*
3963 * So we're all set to release this lock.. wait what lock? We don't
3964 * own any locks, you've been drinking again?
3965 */
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003966 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3967 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003968
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02003969 /*
3970 * Check whether the lock exists in the current stack
3971 * of held locks:
3972 */
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09003973 hlock = find_held_lock(curr, lock, depth, &i);
3974 if (!hlock)
3975 return print_unlock_imbalance_bug(curr, lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003976
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003977 if (hlock->instance == lock)
3978 lock_release_holdtime(hlock);
3979
Peter Zijlstraa24fc602015-06-11 14:46:53 +02003980 WARN(hlock->pin_count, "releasing a pinned lock\n");
3981
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003982 if (hlock->references) {
3983 hlock->references--;
3984 if (hlock->references) {
3985 /*
3986 * We had, and after removing one, still have
3987 * references, the current lock stack is still
3988 * valid. We're done!
3989 */
3990 return 1;
3991 }
3992 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07003993
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003994 /*
3995 * We have the right lock to unlock, 'hlock' points to it.
3996 * Now we remove it from the stack, and add back the other
3997 * entries (if any), recalculating the hash along the way:
3998 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02003999
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004000 curr->lockdep_depth = i;
4001 curr->curr_chain_key = hlock->prev_chain_key;
4002
Waiman Longce52a182018-10-02 16:19:18 -04004003 /*
4004 * The most likely case is when the unlock is on the innermost
4005 * lock. In this case, we are done!
4006 */
4007 if (i == depth-1)
4008 return 1;
4009
J. R. Okajimae9699702017-02-03 01:38:16 +09004010 if (reacquire_held_locks(curr, depth, i + 1))
4011 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004012
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004013 /*
4014 * We had N bottles of beer on the wall, we drank one, but now
4015 * there's not N-1 bottles of beer left on the wall...
4016 */
Waiman Longce52a182018-10-02 16:19:18 -04004017 DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth-1);
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02004018
Waiman Longce52a182018-10-02 16:19:18 -04004019 /*
4020 * Since reacquire_held_locks() would have called check_chain_key()
4021 * indirectly via __lock_acquire(), we don't need to do it again
4022 * on return.
4023 */
4024 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004025}
4026
Masami Hiramatsu2f43c602019-02-13 01:15:05 +09004027static nokprobe_inline
4028int __lock_is_held(const struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02004029{
4030 struct task_struct *curr = current;
4031 int i;
4032
4033 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004034 struct held_lock *hlock = curr->held_locks + i;
4035
Peter Zijlstraf8319482016-11-30 14:32:25 +11004036 if (match_held_lock(hlock, lock)) {
4037 if (read == -1 || hlock->read == read)
4038 return 1;
4039
4040 return 0;
4041 }
Peter Zijlstraf607c662009-07-20 19:16:29 +02004042 }
4043
4044 return 0;
4045}
4046
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004047static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
4048{
4049 struct pin_cookie cookie = NIL_COOKIE;
4050 struct task_struct *curr = current;
4051 int i;
4052
4053 if (unlikely(!debug_locks))
4054 return cookie;
4055
4056 for (i = 0; i < curr->lockdep_depth; i++) {
4057 struct held_lock *hlock = curr->held_locks + i;
4058
4059 if (match_held_lock(hlock, lock)) {
4060 /*
4061 * Grab 16bits of randomness; this is sufficient to not
4062 * be guessable and still allows some pin nesting in
4063 * our u32 pin_count.
4064 */
4065 cookie.val = 1 + (prandom_u32() >> 16);
4066 hlock->pin_count += cookie.val;
4067 return cookie;
4068 }
4069 }
4070
4071 WARN(1, "pinning an unheld lock\n");
4072 return cookie;
4073}
4074
4075static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004076{
4077 struct task_struct *curr = current;
4078 int i;
4079
4080 if (unlikely(!debug_locks))
4081 return;
4082
4083 for (i = 0; i < curr->lockdep_depth; i++) {
4084 struct held_lock *hlock = curr->held_locks + i;
4085
4086 if (match_held_lock(hlock, lock)) {
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004087 hlock->pin_count += cookie.val;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004088 return;
4089 }
4090 }
4091
4092 WARN(1, "pinning an unheld lock\n");
4093}
4094
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004095static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004096{
4097 struct task_struct *curr = current;
4098 int i;
4099
4100 if (unlikely(!debug_locks))
4101 return;
4102
4103 for (i = 0; i < curr->lockdep_depth; i++) {
4104 struct held_lock *hlock = curr->held_locks + i;
4105
4106 if (match_held_lock(hlock, lock)) {
4107 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
4108 return;
4109
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004110 hlock->pin_count -= cookie.val;
4111
4112 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
4113 hlock->pin_count = 0;
4114
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004115 return;
4116 }
4117 }
4118
4119 WARN(1, "unpinning an unheld lock\n");
4120}
4121
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004122/*
4123 * Check whether we follow the irq-flags state precisely:
4124 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02004125static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004126{
Ingo Molnar992860e2008-07-14 10:28:38 +02004127#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
4128 defined(CONFIG_TRACE_IRQFLAGS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004129 if (!debug_locks)
4130 return;
4131
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01004132 if (irqs_disabled_flags(flags)) {
4133 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
4134 printk("possible reason: unannotated irqs-off.\n");
4135 }
4136 } else {
4137 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
4138 printk("possible reason: unannotated irqs-on.\n");
4139 }
4140 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004141
4142 /*
4143 * We dont accurately track softirq state in e.g.
4144 * hardirq contexts (such as on 4KSTACKS), so only
4145 * check if not in hardirq contexts:
4146 */
4147 if (!hardirq_count()) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004148 if (softirq_count()) {
4149 /* like the above, but with softirqs */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004150 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004151 } else {
4152 /* lick the above, does it taste good? */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004153 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004154 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004155 }
4156
4157 if (!debug_locks)
4158 print_irqtrace_events(current);
4159#endif
4160}
4161
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01004162void lock_set_class(struct lockdep_map *lock, const char *name,
4163 struct lock_class_key *key, unsigned int subclass,
4164 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02004165{
4166 unsigned long flags;
4167
4168 if (unlikely(current->lockdep_recursion))
4169 return;
4170
4171 raw_local_irq_save(flags);
4172 current->lockdep_recursion = 1;
4173 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01004174 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02004175 check_chain_key(current);
4176 current->lockdep_recursion = 0;
4177 raw_local_irq_restore(flags);
4178}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01004179EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02004180
J. R. Okajima6419c4a2017-02-03 01:38:17 +09004181void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
4182{
4183 unsigned long flags;
4184
4185 if (unlikely(current->lockdep_recursion))
4186 return;
4187
4188 raw_local_irq_save(flags);
4189 current->lockdep_recursion = 1;
4190 check_flags(flags);
4191 if (__lock_downgrade(lock, ip))
4192 check_chain_key(current);
4193 current->lockdep_recursion = 0;
4194 raw_local_irq_restore(flags);
4195}
4196EXPORT_SYMBOL_GPL(lock_downgrade);
4197
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004198/*
4199 * We are not always called with irqs disabled - do that here,
4200 * and also avoid lockdep recursion:
4201 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02004202void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02004203 int trylock, int read, int check,
4204 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004205{
4206 unsigned long flags;
4207
4208 if (unlikely(current->lockdep_recursion))
4209 return;
4210
4211 raw_local_irq_save(flags);
4212 check_flags(flags);
4213
4214 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01004215 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004216 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra21199f22015-09-16 16:10:40 +02004217 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004218 current->lockdep_recursion = 0;
4219 raw_local_irq_restore(flags);
4220}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004221EXPORT_SYMBOL_GPL(lock_acquire);
4222
Steven Rostedt1d09daa2008-05-12 21:20:55 +02004223void lock_release(struct lockdep_map *lock, int nested,
Steven Rostedt0764d232008-05-12 21:20:44 +02004224 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004225{
4226 unsigned long flags;
4227
4228 if (unlikely(current->lockdep_recursion))
4229 return;
4230
4231 raw_local_irq_save(flags);
4232 check_flags(flags);
4233 current->lockdep_recursion = 1;
Frederic Weisbecker93135432010-05-08 06:24:25 +02004234 trace_lock_release(lock, ip);
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02004235 if (__lock_release(lock, nested, ip))
4236 check_chain_key(current);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004237 current->lockdep_recursion = 0;
4238 raw_local_irq_restore(flags);
4239}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004240EXPORT_SYMBOL_GPL(lock_release);
4241
Matthew Wilcox08f36ff2018-01-17 07:14:13 -08004242int lock_is_held_type(const struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02004243{
4244 unsigned long flags;
4245 int ret = 0;
4246
4247 if (unlikely(current->lockdep_recursion))
Peter Zijlstraf2513cd2011-06-06 12:32:43 +02004248 return 1; /* avoid false negative lockdep_assert_held() */
Peter Zijlstraf607c662009-07-20 19:16:29 +02004249
4250 raw_local_irq_save(flags);
4251 check_flags(flags);
4252
4253 current->lockdep_recursion = 1;
Peter Zijlstraf8319482016-11-30 14:32:25 +11004254 ret = __lock_is_held(lock, read);
Peter Zijlstraf607c662009-07-20 19:16:29 +02004255 current->lockdep_recursion = 0;
4256 raw_local_irq_restore(flags);
4257
4258 return ret;
4259}
Peter Zijlstraf8319482016-11-30 14:32:25 +11004260EXPORT_SYMBOL_GPL(lock_is_held_type);
Masami Hiramatsu2f43c602019-02-13 01:15:05 +09004261NOKPROBE_SYMBOL(lock_is_held_type);
Peter Zijlstraf607c662009-07-20 19:16:29 +02004262
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004263struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004264{
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004265 struct pin_cookie cookie = NIL_COOKIE;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004266 unsigned long flags;
4267
4268 if (unlikely(current->lockdep_recursion))
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004269 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004270
4271 raw_local_irq_save(flags);
4272 check_flags(flags);
4273
4274 current->lockdep_recursion = 1;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004275 cookie = __lock_pin_lock(lock);
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004276 current->lockdep_recursion = 0;
4277 raw_local_irq_restore(flags);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004278
4279 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004280}
4281EXPORT_SYMBOL_GPL(lock_pin_lock);
4282
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004283void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004284{
4285 unsigned long flags;
4286
4287 if (unlikely(current->lockdep_recursion))
4288 return;
4289
4290 raw_local_irq_save(flags);
4291 check_flags(flags);
4292
4293 current->lockdep_recursion = 1;
Peter Zijlstrae7904a22015-08-01 19:25:08 +02004294 __lock_repin_lock(lock, cookie);
4295 current->lockdep_recursion = 0;
4296 raw_local_irq_restore(flags);
4297}
4298EXPORT_SYMBOL_GPL(lock_repin_lock);
4299
4300void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
4301{
4302 unsigned long flags;
4303
4304 if (unlikely(current->lockdep_recursion))
4305 return;
4306
4307 raw_local_irq_save(flags);
4308 check_flags(flags);
4309
4310 current->lockdep_recursion = 1;
4311 __lock_unpin_lock(lock, cookie);
Peter Zijlstraa24fc602015-06-11 14:46:53 +02004312 current->lockdep_recursion = 0;
4313 raw_local_irq_restore(flags);
4314}
4315EXPORT_SYMBOL_GPL(lock_unpin_lock);
4316
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004317#ifdef CONFIG_LOCK_STAT
4318static int
4319print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
4320 unsigned long ip)
4321{
4322 if (!debug_locks_off())
4323 return 0;
4324 if (debug_locks_silent)
4325 return 0;
4326
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004327 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004328 pr_warn("=================================\n");
4329 pr_warn("WARNING: bad contention detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004330 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004331 pr_warn("---------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004332 pr_warn("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004333 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004334 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004335 pr_cont(") at:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004336 print_ip_sym(ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004337 pr_warn("but there are no locks held!\n");
4338 pr_warn("\nother info that might help us debug this:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004339 lockdep_print_held_locks(curr);
4340
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004341 pr_warn("\nstack backtrace:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004342 dump_stack();
4343
4344 return 0;
4345}
4346
4347static void
4348__lock_contended(struct lockdep_map *lock, unsigned long ip)
4349{
4350 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004351 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004352 struct lock_class_stats *stats;
4353 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004354 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004355
4356 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004357 /*
4358 * Whee, we contended on this lock, except it seems we're not
4359 * actually trying to acquire anything much at all..
4360 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004361 if (DEBUG_LOCKS_WARN_ON(!depth))
4362 return;
4363
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004364 hlock = find_held_lock(curr, lock, depth, &i);
4365 if (!hlock) {
4366 print_lock_contention_bug(curr, lock, ip);
4367 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004368 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004369
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004370 if (hlock->instance != lock)
4371 return;
4372
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004373 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004374
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004375 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
4376 contending_point = lock_point(hlock_class(hlock)->contending_point,
4377 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004378
Dave Jonesf82b2172008-08-11 09:30:23 +02004379 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004380 if (contention_point < LOCKSTAT_POINTS)
4381 stats->contention_point[contention_point]++;
4382 if (contending_point < LOCKSTAT_POINTS)
4383 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07004384 if (lock->cpu != smp_processor_id())
4385 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004386}
4387
4388static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004389__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004390{
4391 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004392 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004393 struct lock_class_stats *stats;
4394 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004395 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07004396 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004397
4398 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004399 /*
4400 * Yay, we acquired ownership of this lock we didn't try to
4401 * acquire, how the heck did that happen?
4402 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004403 if (DEBUG_LOCKS_WARN_ON(!depth))
4404 return;
4405
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004406 hlock = find_held_lock(curr, lock, depth, &i);
4407 if (!hlock) {
4408 print_lock_contention_bug(curr, lock, _RET_IP_);
4409 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004410 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004411
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004412 if (hlock->instance != lock)
4413 return;
4414
Peter Zijlstra96645672007-07-19 01:49:00 -07004415 cpu = smp_processor_id();
4416 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004417 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07004418 waittime = now - hlock->waittime_stamp;
4419 hlock->holdtime_stamp = now;
4420 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004421
Frederic Weisbecker883a2a32010-05-08 06:16:11 +02004422 trace_lock_acquired(lock, ip);
Frederic Weisbecker20625012009-04-06 01:49:33 +02004423
Dave Jonesf82b2172008-08-11 09:30:23 +02004424 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07004425 if (waittime) {
4426 if (hlock->read)
4427 lock_time_inc(&stats->read_waittime, waittime);
4428 else
4429 lock_time_inc(&stats->write_waittime, waittime);
4430 }
4431 if (lock->cpu != cpu)
4432 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07004433
4434 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004435 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004436}
4437
4438void lock_contended(struct lockdep_map *lock, unsigned long ip)
4439{
4440 unsigned long flags;
4441
Waiman Long9506a742018-10-18 21:45:17 -04004442 if (unlikely(!lock_stat || !debug_locks))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004443 return;
4444
4445 if (unlikely(current->lockdep_recursion))
4446 return;
4447
4448 raw_local_irq_save(flags);
4449 check_flags(flags);
4450 current->lockdep_recursion = 1;
Frederic Weisbeckerdb2c4c72010-02-02 23:34:40 +01004451 trace_lock_contended(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004452 __lock_contended(lock, ip);
4453 current->lockdep_recursion = 0;
4454 raw_local_irq_restore(flags);
4455}
4456EXPORT_SYMBOL_GPL(lock_contended);
4457
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004458void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004459{
4460 unsigned long flags;
4461
Waiman Long9506a742018-10-18 21:45:17 -04004462 if (unlikely(!lock_stat || !debug_locks))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004463 return;
4464
4465 if (unlikely(current->lockdep_recursion))
4466 return;
4467
4468 raw_local_irq_save(flags);
4469 check_flags(flags);
4470 current->lockdep_recursion = 1;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02004471 __lock_acquired(lock, ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004472 current->lockdep_recursion = 0;
4473 raw_local_irq_restore(flags);
4474}
4475EXPORT_SYMBOL_GPL(lock_acquired);
4476#endif
4477
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004478/*
4479 * Used by the testsuite, sanitize the validator state
4480 * after a simulated failure:
4481 */
4482
4483void lockdep_reset(void)
4484{
4485 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08004486 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004487
4488 raw_local_irq_save(flags);
4489 current->curr_chain_key = 0;
4490 current->lockdep_depth = 0;
4491 current->lockdep_recursion = 0;
4492 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
4493 nr_hardirq_chains = 0;
4494 nr_softirq_chains = 0;
4495 nr_process_chains = 0;
4496 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08004497 for (i = 0; i < CHAINHASH_SIZE; i++)
Andrew Mortona63f38c2016-02-03 13:44:12 -08004498 INIT_HLIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004499 raw_local_irq_restore(flags);
4500}
4501
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004502/* Remove a class from a lock chain. Must be called with the graph lock held. */
Bart Van Asschede4643a2019-02-14 15:00:50 -08004503static void remove_class_from_lock_chain(struct pending_free *pf,
4504 struct lock_chain *chain,
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004505 struct lock_class *class)
4506{
4507#ifdef CONFIG_PROVE_LOCKING
4508 struct lock_chain *new_chain;
4509 u64 chain_key;
4510 int i;
4511
4512 for (i = chain->base; i < chain->base + chain->depth; i++) {
4513 if (chain_hlocks[i] != class - lock_classes)
4514 continue;
4515 /* The code below leaks one chain_hlock[] entry. */
Peter Zijlstra72dcd502019-02-25 15:56:32 +01004516 if (--chain->depth > 0) {
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004517 memmove(&chain_hlocks[i], &chain_hlocks[i + 1],
4518 (chain->base + chain->depth - i) *
4519 sizeof(chain_hlocks[0]));
Peter Zijlstra72dcd502019-02-25 15:56:32 +01004520 }
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004521 /*
4522 * Each lock class occurs at most once in a lock chain so once
4523 * we found a match we can break out of this loop.
4524 */
4525 goto recalc;
4526 }
4527 /* Since the chain has not been modified, return. */
4528 return;
4529
4530recalc:
4531 chain_key = 0;
4532 for (i = chain->base; i < chain->base + chain->depth; i++)
4533 chain_key = iterate_chain_key(chain_key, chain_hlocks[i] + 1);
4534 if (chain->depth && chain->chain_key == chain_key)
4535 return;
4536 /* Overwrite the chain key for concurrent RCU readers. */
4537 WRITE_ONCE(chain->chain_key, chain_key);
4538 /*
4539 * Note: calling hlist_del_rcu() from inside a
4540 * hlist_for_each_entry_rcu() loop is safe.
4541 */
4542 hlist_del_rcu(&chain->entry);
Bart Van Asschede4643a2019-02-14 15:00:50 -08004543 __set_bit(chain - lock_chains, pf->lock_chains_being_freed);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004544 if (chain->depth == 0)
4545 return;
4546 /*
4547 * If the modified lock chain matches an existing lock chain, drop
4548 * the modified lock chain.
4549 */
4550 if (lookup_chain_cache(chain_key))
4551 return;
Bart Van Asschede4643a2019-02-14 15:00:50 -08004552 new_chain = alloc_lock_chain();
4553 if (WARN_ON_ONCE(!new_chain)) {
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004554 debug_locks_off();
4555 return;
4556 }
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004557 *new_chain = *chain;
4558 hlist_add_head_rcu(&new_chain->entry, chainhashentry(chain_key));
4559#endif
4560}
4561
4562/* Must be called with the graph lock held. */
Bart Van Asschede4643a2019-02-14 15:00:50 -08004563static void remove_class_from_lock_chains(struct pending_free *pf,
4564 struct lock_class *class)
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004565{
4566 struct lock_chain *chain;
4567 struct hlist_head *head;
4568 int i;
4569
4570 for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
4571 head = chainhash_table + i;
4572 hlist_for_each_entry_rcu(chain, head, entry) {
Bart Van Asschede4643a2019-02-14 15:00:50 -08004573 remove_class_from_lock_chain(pf, chain, class);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004574 }
4575 }
4576}
4577
Bart Van Assche786fa292018-12-06 17:11:36 -08004578/*
4579 * Remove all references to a lock class. The caller must hold the graph lock.
4580 */
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004581static void zap_class(struct pending_free *pf, struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004582{
Bart Van Assche86cffb82019-02-14 15:00:41 -08004583 struct lock_list *entry;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004584 int i;
4585
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004586 WARN_ON_ONCE(!class->key);
4587
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004588 /*
4589 * Remove all dependencies this lock is
4590 * involved in:
4591 */
Bart Van Asscheace35a72019-02-14 15:00:47 -08004592 for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
4593 entry = list_entries + i;
Bart Van Assche86cffb82019-02-14 15:00:41 -08004594 if (entry->class != class && entry->links_to != class)
4595 continue;
Bart Van Asscheace35a72019-02-14 15:00:47 -08004596 __clear_bit(i, list_entries_in_use);
4597 nr_list_entries--;
Bart Van Assche86cffb82019-02-14 15:00:41 -08004598 list_del_rcu(&entry->entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004599 }
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004600 if (list_empty(&class->locks_after) &&
4601 list_empty(&class->locks_before)) {
4602 list_move_tail(&class->lock_entry, &pf->zapped);
4603 hlist_del_rcu(&class->hash_entry);
4604 WRITE_ONCE(class->key, NULL);
4605 WRITE_ONCE(class->name, NULL);
4606 nr_lock_classes--;
4607 } else {
4608 WARN_ONCE(true, "%s() failed for class %s\n", __func__,
4609 class->name);
4610 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004611
Bart Van Asschede4643a2019-02-14 15:00:50 -08004612 remove_class_from_lock_chains(pf, class);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004613}
4614
4615static void reinit_class(struct lock_class *class)
4616{
4617 void *const p = class;
4618 const unsigned int offset = offsetof(struct lock_class, key);
4619
4620 WARN_ON_ONCE(!class->lock_entry.next);
4621 WARN_ON_ONCE(!list_empty(&class->locks_after));
4622 WARN_ON_ONCE(!list_empty(&class->locks_before));
4623 memset(p + offset, 0, sizeof(*class) - offset);
4624 WARN_ON_ONCE(!class->lock_entry.next);
4625 WARN_ON_ONCE(!list_empty(&class->locks_after));
4626 WARN_ON_ONCE(!list_empty(&class->locks_before));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004627}
4628
Arjan van de Venfabe8742008-01-24 07:00:45 +01004629static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004630{
4631 return addr >= start && addr < start + size;
4632}
4633
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004634static bool inside_selftest(void)
4635{
4636 return current == lockdep_selftest_task_struct;
4637}
4638
4639/* The caller must hold the graph lock. */
4640static struct pending_free *get_pending_free(void)
4641{
4642 return delayed_free.pf + delayed_free.index;
4643}
4644
4645static void free_zapped_rcu(struct rcu_head *cb);
4646
4647/*
4648 * Schedule an RCU callback if no RCU callback is pending. Must be called with
4649 * the graph lock held.
4650 */
4651static void call_rcu_zapped(struct pending_free *pf)
4652{
4653 WARN_ON_ONCE(inside_selftest());
4654
4655 if (list_empty(&pf->zapped))
4656 return;
4657
4658 if (delayed_free.scheduled)
4659 return;
4660
4661 delayed_free.scheduled = true;
4662
4663 WARN_ON_ONCE(delayed_free.pf + delayed_free.index != pf);
4664 delayed_free.index ^= 1;
4665
4666 call_rcu(&delayed_free.rcu_head, free_zapped_rcu);
4667}
4668
4669/* The caller must hold the graph lock. May be called from RCU context. */
4670static void __free_zapped_classes(struct pending_free *pf)
4671{
4672 struct lock_class *class;
4673
Peter Zijlstra72dcd502019-02-25 15:56:32 +01004674 check_data_structures();
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08004675
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004676 list_for_each_entry(class, &pf->zapped, lock_entry)
4677 reinit_class(class);
4678
4679 list_splice_init(&pf->zapped, &free_lock_classes);
Bart Van Asschede4643a2019-02-14 15:00:50 -08004680
4681#ifdef CONFIG_PROVE_LOCKING
4682 bitmap_andnot(lock_chains_in_use, lock_chains_in_use,
4683 pf->lock_chains_being_freed, ARRAY_SIZE(lock_chains));
4684 bitmap_clear(pf->lock_chains_being_freed, 0, ARRAY_SIZE(lock_chains));
4685#endif
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004686}
4687
4688static void free_zapped_rcu(struct rcu_head *ch)
4689{
4690 struct pending_free *pf;
4691 unsigned long flags;
4692
4693 if (WARN_ON_ONCE(ch != &delayed_free.rcu_head))
4694 return;
4695
4696 raw_local_irq_save(flags);
4697 if (!graph_lock())
4698 goto out_irq;
4699
4700 /* closed head */
4701 pf = delayed_free.pf + (delayed_free.index ^ 1);
4702 __free_zapped_classes(pf);
4703 delayed_free.scheduled = false;
4704
4705 /*
4706 * If there's anything on the open list, close and start a new callback.
4707 */
4708 call_rcu_zapped(delayed_free.pf + delayed_free.index);
4709
4710 graph_unlock();
4711out_irq:
4712 raw_local_irq_restore(flags);
4713}
4714
4715/*
4716 * Remove all lock classes from the class hash table and from the
4717 * all_lock_classes list whose key or name is in the address range [start,
4718 * start + size). Move these lock classes to the zapped_classes list. Must
4719 * be called with the graph lock held.
4720 */
4721static void __lockdep_free_key_range(struct pending_free *pf, void *start,
4722 unsigned long size)
Bart Van Assche956f3562019-02-14 15:00:43 -08004723{
4724 struct lock_class *class;
4725 struct hlist_head *head;
4726 int i;
4727
4728 /* Unhash all classes that were created by a module. */
4729 for (i = 0; i < CLASSHASH_SIZE; i++) {
4730 head = classhash_table + i;
4731 hlist_for_each_entry_rcu(class, head, hash_entry) {
4732 if (!within(class->key, start, size) &&
4733 !within(class->name, start, size))
4734 continue;
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004735 zap_class(pf, class);
Bart Van Assche956f3562019-02-14 15:00:43 -08004736 }
4737 }
4738}
4739
Peter Zijlstra35a93932015-02-26 16:23:11 +01004740/*
4741 * Used in module.c to remove lock classes from memory that is going to be
4742 * freed; and possibly re-used by other modules.
4743 *
Bart Van Assche29fc33f2019-02-14 15:00:45 -08004744 * We will have had one synchronize_rcu() before getting here, so we're
4745 * guaranteed nobody will look up these exact classes -- they're properly dead
4746 * but still allocated.
Peter Zijlstra35a93932015-02-26 16:23:11 +01004747 */
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004748static void lockdep_free_key_range_reg(void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004749{
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004750 struct pending_free *pf;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004751 unsigned long flags;
Nick Piggin5a26db52008-01-16 09:51:58 +01004752 int locked;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004753
Bart Van Asschefeb0a382019-02-14 15:00:42 -08004754 init_data_structures_once();
4755
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004756 raw_local_irq_save(flags);
Nick Piggin5a26db52008-01-16 09:51:58 +01004757 locked = graph_lock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004758 if (!locked)
4759 goto out_irq;
4760
4761 pf = get_pending_free();
4762 __lockdep_free_key_range(pf, start, size);
4763 call_rcu_zapped(pf);
4764
4765 graph_unlock();
4766out_irq:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004767 raw_local_irq_restore(flags);
Peter Zijlstra35a93932015-02-26 16:23:11 +01004768
4769 /*
4770 * Wait for any possible iterators from look_up_lock_class() to pass
4771 * before continuing to free the memory they refer to.
Peter Zijlstra35a93932015-02-26 16:23:11 +01004772 */
Paul E. McKenney51959d82018-11-06 19:06:51 -08004773 synchronize_rcu();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004774}
Peter Zijlstra35a93932015-02-26 16:23:11 +01004775
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004776/*
4777 * Free all lockdep keys in the range [start, start+size). Does not sleep.
4778 * Ignores debug_locks. Must only be used by the lockdep selftests.
4779 */
4780static void lockdep_free_key_range_imm(void *start, unsigned long size)
4781{
4782 struct pending_free *pf = delayed_free.pf;
4783 unsigned long flags;
4784
4785 init_data_structures_once();
4786
4787 raw_local_irq_save(flags);
4788 arch_spin_lock(&lockdep_lock);
4789 __lockdep_free_key_range(pf, start, size);
4790 __free_zapped_classes(pf);
4791 arch_spin_unlock(&lockdep_lock);
4792 raw_local_irq_restore(flags);
4793}
4794
4795void lockdep_free_key_range(void *start, unsigned long size)
4796{
4797 init_data_structures_once();
4798
4799 if (inside_selftest())
4800 lockdep_free_key_range_imm(start, size);
4801 else
4802 lockdep_free_key_range_reg(start, size);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004803}
4804
Bart Van Assche2904d9f2018-12-06 17:11:34 -08004805/*
4806 * Check whether any element of the @lock->class_cache[] array refers to a
4807 * registered lock class. The caller must hold either the graph lock or the
4808 * RCU read lock.
4809 */
4810static bool lock_class_cache_is_registered(struct lockdep_map *lock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004811{
Peter Zijlstra35a93932015-02-26 16:23:11 +01004812 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08004813 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004814 int i, j;
Bart Van Assche2904d9f2018-12-06 17:11:34 -08004815
4816 for (i = 0; i < CLASSHASH_SIZE; i++) {
4817 head = classhash_table + i;
4818 hlist_for_each_entry_rcu(class, head, hash_entry) {
4819 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
4820 if (lock->class_cache[j] == class)
4821 return true;
4822 }
4823 }
4824 return false;
4825}
4826
Bart Van Assche956f3562019-02-14 15:00:43 -08004827/* The caller must hold the graph lock. Does not sleep. */
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004828static void __lockdep_reset_lock(struct pending_free *pf,
4829 struct lockdep_map *lock)
Bart Van Assche2904d9f2018-12-06 17:11:34 -08004830{
4831 struct lock_class *class;
Bart Van Assche956f3562019-02-14 15:00:43 -08004832 int j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004833
4834 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07004835 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004836 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07004837 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
4838 /*
4839 * If the class exists we look it up and zap it:
4840 */
4841 class = look_up_lock_class(lock, j);
Matthew Wilcox64f29d12018-01-17 07:14:12 -08004842 if (class)
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004843 zap_class(pf, class);
Ingo Molnard6d897c2006-07-10 04:44:04 -07004844 }
4845 /*
4846 * Debug check: in the end all mapped classes should
4847 * be gone.
4848 */
Bart Van Assche956f3562019-02-14 15:00:43 -08004849 if (WARN_ON_ONCE(lock_class_cache_is_registered(lock)))
4850 debug_locks_off();
4851}
4852
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004853/*
4854 * Remove all information lockdep has about a lock if debug_locks == 1. Free
4855 * released data structures from RCU context.
4856 */
4857static void lockdep_reset_lock_reg(struct lockdep_map *lock)
Bart Van Assche956f3562019-02-14 15:00:43 -08004858{
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004859 struct pending_free *pf;
Bart Van Assche956f3562019-02-14 15:00:43 -08004860 unsigned long flags;
4861 int locked;
4862
Bart Van Assche956f3562019-02-14 15:00:43 -08004863 raw_local_irq_save(flags);
4864 locked = graph_lock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004865 if (!locked)
4866 goto out_irq;
4867
4868 pf = get_pending_free();
4869 __lockdep_reset_lock(pf, lock);
4870 call_rcu_zapped(pf);
4871
4872 graph_unlock();
4873out_irq:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004874 raw_local_irq_restore(flags);
4875}
4876
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004877/*
4878 * Reset a lock. Does not sleep. Ignores debug_locks. Must only be used by the
4879 * lockdep selftests.
4880 */
4881static void lockdep_reset_lock_imm(struct lockdep_map *lock)
4882{
4883 struct pending_free *pf = delayed_free.pf;
4884 unsigned long flags;
4885
4886 raw_local_irq_save(flags);
4887 arch_spin_lock(&lockdep_lock);
4888 __lockdep_reset_lock(pf, lock);
4889 __free_zapped_classes(pf);
4890 arch_spin_unlock(&lockdep_lock);
4891 raw_local_irq_restore(flags);
4892}
4893
4894void lockdep_reset_lock(struct lockdep_map *lock)
4895{
4896 init_data_structures_once();
4897
4898 if (inside_selftest())
4899 lockdep_reset_lock_imm(lock);
4900 else
4901 lockdep_reset_lock_reg(lock);
4902}
4903
Bart Van Assche108c1482019-02-14 15:00:53 -08004904/* Unregister a dynamically allocated key. */
4905void lockdep_unregister_key(struct lock_class_key *key)
4906{
4907 struct hlist_head *hash_head = keyhashentry(key);
4908 struct lock_class_key *k;
4909 struct pending_free *pf;
4910 unsigned long flags;
4911 bool found = false;
4912
4913 might_sleep();
4914
4915 if (WARN_ON_ONCE(static_obj(key)))
4916 return;
4917
4918 raw_local_irq_save(flags);
4919 if (!graph_lock())
4920 goto out_irq;
4921
4922 pf = get_pending_free();
4923 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
4924 if (k == key) {
4925 hlist_del_rcu(&k->hash_entry);
4926 found = true;
4927 break;
4928 }
4929 }
4930 WARN_ON_ONCE(!found);
4931 __lockdep_free_key_range(pf, key, 1);
4932 call_rcu_zapped(pf);
4933 graph_unlock();
4934out_irq:
4935 raw_local_irq_restore(flags);
4936
4937 /* Wait until is_dynamic_key() has finished accessing k->hash_entry. */
4938 synchronize_rcu();
4939}
4940EXPORT_SYMBOL_GPL(lockdep_unregister_key);
4941
Joel Fernandes (Google)c3bc8fd2018-07-30 15:24:23 -07004942void __init lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004943{
4944 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4945
Li Zefanb0788ca2008-11-21 15:57:32 +08004946 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004947 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4948 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08004949 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004950 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4951 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4952 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4953
Bart Van Assche09d75ec2019-02-14 15:00:36 -08004954 printk(" memory used by lock dependency info: %zu kB\n",
Bart Van Assche7ff85172019-02-14 15:00:37 -08004955 (sizeof(lock_classes) +
4956 sizeof(classhash_table) +
4957 sizeof(list_entries) +
Bart Van Asscheace35a72019-02-14 15:00:47 -08004958 sizeof(list_entries_in_use) +
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004959 sizeof(chainhash_table) +
4960 sizeof(delayed_free)
Ming Lei4dd861d2009-07-16 15:44:29 +02004961#ifdef CONFIG_PROVE_LOCKING
Bart Van Assche7ff85172019-02-14 15:00:37 -08004962 + sizeof(lock_cq)
Bart Van Assche15ea86b2019-02-14 15:00:38 -08004963 + sizeof(lock_chains)
Bart Van Asschede4643a2019-02-14 15:00:50 -08004964 + sizeof(lock_chains_in_use)
Bart Van Assche15ea86b2019-02-14 15:00:38 -08004965 + sizeof(chain_hlocks)
Ming Lei4dd861d2009-07-16 15:44:29 +02004966#endif
Ming Lei906292092009-08-02 21:43:36 +08004967 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02004968 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004969
Bart Van Assche09d75ec2019-02-14 15:00:36 -08004970 printk(" per task-struct memory footprint: %zu bytes\n",
Bart Van Assche7ff85172019-02-14 15:00:37 -08004971 sizeof(((struct task_struct *)NULL)->held_locks));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004972}
4973
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004974static void
4975print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07004976 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004977{
4978 if (!debug_locks_off())
4979 return;
4980 if (debug_locks_silent)
4981 return;
4982
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004983 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004984 pr_warn("=========================\n");
4985 pr_warn("WARNING: held lock freed!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004986 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004987 pr_warn("-------------------------\n");
Borislav Petkov04860d42018-02-26 14:49:26 +01004988 pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004989 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07004990 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004991 lockdep_print_held_locks(curr);
4992
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004993 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004994 dump_stack();
4995}
4996
Oleg Nesterov54561782007-12-05 15:46:09 +01004997static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4998 const void* lock_from, unsigned long lock_len)
4999{
5000 return lock_from + lock_len <= mem_from ||
5001 mem_from + mem_len <= lock_from;
5002}
5003
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005004/*
5005 * Called when kernel memory is freed (or unmapped), or if a lock
5006 * is destroyed or reinitialized - this code checks whether there is
5007 * any held lock in the memory range of <from> to <to>:
5008 */
5009void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
5010{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005011 struct task_struct *curr = current;
5012 struct held_lock *hlock;
5013 unsigned long flags;
5014 int i;
5015
5016 if (unlikely(!debug_locks))
5017 return;
5018
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04005019 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005020 for (i = 0; i < curr->lockdep_depth; i++) {
5021 hlock = curr->held_locks + i;
5022
Oleg Nesterov54561782007-12-05 15:46:09 +01005023 if (not_in_range(mem_from, mem_len, hlock->instance,
5024 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005025 continue;
5026
Oleg Nesterov54561782007-12-05 15:46:09 +01005027 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005028 break;
5029 }
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04005030 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005031}
Peter Zijlstraed075362006-12-06 20:35:24 -08005032EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005033
Colin Cross1b1d2fb2013-05-06 23:50:08 +00005034static void print_held_locks_bug(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005035{
5036 if (!debug_locks_off())
5037 return;
5038 if (debug_locks_silent)
5039 return;
5040
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005041 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08005042 pr_warn("====================================\n");
5043 pr_warn("WARNING: %s/%d still has locks held!\n",
Colin Cross1b1d2fb2013-05-06 23:50:08 +00005044 current->comm, task_pid_nr(current));
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01005045 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08005046 pr_warn("------------------------------------\n");
Colin Cross1b1d2fb2013-05-06 23:50:08 +00005047 lockdep_print_held_locks(current);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005048 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005049 dump_stack();
5050}
5051
Colin Cross1b1d2fb2013-05-06 23:50:08 +00005052void debug_check_no_locks_held(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005053{
Colin Cross1b1d2fb2013-05-06 23:50:08 +00005054 if (unlikely(current->lockdep_depth > 0))
5055 print_held_locks_bug();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005056}
Colin Cross1b1d2fb2013-05-06 23:50:08 +00005057EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005058
Sasha Levin8dce7a92013-06-13 18:41:16 -04005059#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005060void debug_show_all_locks(void)
5061{
5062 struct task_struct *g, *p;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005063
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08005064 if (unlikely(!debug_locks)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005065 pr_warn("INFO: lockdep is turned off.\n");
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08005066 return;
5067 }
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005068 pr_warn("\nShowing all locks held in the system:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005069
Tetsuo Handa0f736a52018-04-06 19:41:18 +09005070 rcu_read_lock();
5071 for_each_process_thread(g, p) {
Tetsuo Handa0f736a52018-04-06 19:41:18 +09005072 if (!p->lockdep_depth)
5073 continue;
5074 lockdep_print_held_locks(p);
Tejun Heo88f1c872018-01-22 14:00:55 -08005075 touch_nmi_watchdog();
Tetsuo Handa0f736a52018-04-06 19:41:18 +09005076 touch_all_softlockup_watchdogs();
5077 }
5078 rcu_read_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005079
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005080 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08005081 pr_warn("=============================================\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005082}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005083EXPORT_SYMBOL_GPL(debug_show_all_locks);
Sasha Levin8dce7a92013-06-13 18:41:16 -04005084#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005085
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005086/*
5087 * Careful: only use this function if you are sure that
5088 * the task cannot run in parallel!
5089 */
John Kacurf1b499f2010-08-05 17:10:53 +02005090void debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005091{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08005092 if (unlikely(!debug_locks)) {
5093 printk("INFO: lockdep is turned off.\n");
5094 return;
5095 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005096 lockdep_print_held_locks(task);
5097}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005098EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02005099
Andi Kleen722a9f92014-05-02 00:44:38 +02005100asmlinkage __visible void lockdep_sys_exit(void)
Peter Zijlstrab351d162007-10-11 22:11:12 +02005101{
5102 struct task_struct *curr = current;
5103
5104 if (unlikely(curr->lockdep_depth)) {
5105 if (!debug_locks_off())
5106 return;
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005107 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08005108 pr_warn("================================================\n");
5109 pr_warn("WARNING: lock held when returning to user space!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01005110 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08005111 pr_warn("------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005112 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
Peter Zijlstrab351d162007-10-11 22:11:12 +02005113 curr->comm, curr->pid);
5114 lockdep_print_held_locks(curr);
5115 }
Byungchul Parkb09be672017-08-07 16:12:52 +09005116
5117 /*
5118 * The lock history for each syscall should be independent. So wipe the
5119 * slate clean on return to userspace.
5120 */
Peter Zijlstraf52be572017-08-29 10:59:39 +02005121 lockdep_invariant_state(false);
Peter Zijlstrab351d162007-10-11 22:11:12 +02005122}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08005123
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07005124void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
Paul E. McKenney0632eb32010-02-22 17:04:47 -08005125{
5126 struct task_struct *curr = current;
5127
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08005128 /* Note: the following can be executed concurrently, so be careful. */
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005129 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08005130 pr_warn("=============================\n");
5131 pr_warn("WARNING: suspicious RCU usage\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01005132 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08005133 pr_warn("-----------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005134 pr_warn("%s:%d %s!\n", file, line, s);
5135 pr_warn("\nother info that might help us debug this:\n\n");
5136 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08005137 !rcu_lockdep_current_cpu_online()
5138 ? "RCU used illegally from offline CPU!\n"
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07005139 : !rcu_is_watching()
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08005140 ? "RCU used illegally from idle CPU!\n"
5141 : "",
5142 rcu_scheduler_active, debug_locks);
Frederic Weisbecker0464e932011-10-07 18:22:01 +02005143
5144 /*
5145 * If a CPU is in the RCU-free window in idle (ie: in the section
5146 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
5147 * considers that CPU to be in an "extended quiescent state",
5148 * which means that RCU will be completely ignoring that CPU.
5149 * Therefore, rcu_read_lock() and friends have absolutely no
5150 * effect on a CPU running in that state. In other words, even if
5151 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
5152 * delete data structures out from under it. RCU really has no
5153 * choice here: we need to keep an RCU-free window in idle where
5154 * the CPU may possibly enter into low power mode. This way we can
5155 * notice an extended quiescent state to other CPUs that started a grace
5156 * period. Otherwise we would delay any grace period as long as we run
5157 * in the idle task.
5158 *
5159 * So complain bitterly if someone does call rcu_read_lock(),
5160 * rcu_read_lock_bh() and so on from extended quiescent states.
5161 */
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07005162 if (!rcu_is_watching())
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005163 pr_warn("RCU used illegally from extended quiescent state!\n");
Frederic Weisbecker0464e932011-10-07 18:22:01 +02005164
Paul E. McKenney0632eb32010-02-22 17:04:47 -08005165 lockdep_print_held_locks(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005166 pr_warn("\nstack backtrace:\n");
Paul E. McKenney0632eb32010-02-22 17:04:47 -08005167 dump_stack();
5168}
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07005169EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);