blob: c1418b47f625a25fc32d8b18852057dc48b6cc93 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002/*
3 * kernel/lockdep.c
4 *
5 * Runtime locking correctness validator
6 *
7 * Started by Ingo Molnar:
8 *
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -07009 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
Peter Zijlstra90eec102015-11-16 11:08:45 +010010 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070011 *
12 * this code maps all the lock dependencies as they occur in a live kernel
13 * and will warn about the following classes of locking bugs:
14 *
15 * - lock inversion scenarios
16 * - circular lock dependencies
17 * - hardirq/softirq safe/unsafe locking bugs
18 *
19 * Bugs are reported even if the current locking scenario does not cause
20 * any deadlock at this point.
21 *
22 * I.e. if anytime in the past two locks were taken in a different order,
23 * even if it happened for another task, even if those were different
24 * locks (but of the same class as this lock), this code will detect it.
25 *
26 * Thanks to Arjan van de Ven for coming up with the initial idea of
27 * mapping lock dependencies runtime.
28 */
Steven Rostedta5e25882008-12-02 15:34:05 -050029#define DISABLE_BRANCH_PROFILING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070030#include <linux/mutex.h>
31#include <linux/sched.h>
Ingo Molnare6017572017-02-01 16:36:40 +010032#include <linux/sched/clock.h>
Ingo Molnar29930022017-02-08 18:51:36 +010033#include <linux/sched/task.h>
Nikolay Borisov6d7225f2017-05-03 14:53:05 -070034#include <linux/sched/mm.h>
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070035#include <linux/delay.h>
36#include <linux/module.h>
37#include <linux/proc_fs.h>
38#include <linux/seq_file.h>
39#include <linux/spinlock.h>
40#include <linux/kallsyms.h>
41#include <linux/interrupt.h>
42#include <linux/stacktrace.h>
43#include <linux/debug_locks.h>
44#include <linux/irqflags.h>
Dave Jones99de0552006-09-29 02:00:10 -070045#include <linux/utsname.h>
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -070046#include <linux/hash.h>
Steven Rostedt81d68a92008-05-12 21:20:42 +020047#include <linux/ftrace.h>
Peter Zijlstrab4b136f2009-01-29 14:50:36 +010048#include <linux/stringify.h>
Bart Van Asscheace35a72019-02-14 15:00:47 -080049#include <linux/bitmap.h>
Ming Leid588e462009-07-16 15:44:29 +020050#include <linux/bitops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/gfp.h>
Peter Zijlstrae7904a22015-08-01 19:25:08 +020052#include <linux/random.h>
Peter Zijlstradfaaf3f2016-05-30 18:31:33 +020053#include <linux/jhash.h>
Tejun Heo88f1c872018-01-22 14:00:55 -080054#include <linux/nmi.h>
Bart Van Asschea0b0fd52019-02-14 15:00:46 -080055#include <linux/rcupdate.h>
Masami Hiramatsu2f43c602019-02-13 01:15:05 +090056#include <linux/kprobes.h>
Peter Zijlstraaf012962009-07-16 15:44:29 +020057
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070058#include <asm/sections.h>
59
60#include "lockdep_internals.h"
61
Steven Rostedta8d154b2009-04-10 09:36:00 -040062#define CREATE_TRACE_POINTS
Frederic Weisbecker67178762009-11-13 10:06:34 +010063#include <trace/events/lock.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040064
Peter Zijlstraf20786f2007-07-19 01:48:56 -070065#ifdef CONFIG_PROVE_LOCKING
66int prove_locking = 1;
67module_param(prove_locking, int, 0644);
68#else
69#define prove_locking 0
70#endif
71
72#ifdef CONFIG_LOCK_STAT
73int lock_stat = 1;
74module_param(lock_stat, int, 0644);
75#else
76#define lock_stat 0
77#endif
78
Peter Zijlstra4d004092020-10-02 11:04:21 +020079DEFINE_PER_CPU(unsigned int, lockdep_recursion);
80EXPORT_PER_CPU_SYMBOL_GPL(lockdep_recursion);
81
82static inline bool lockdep_enabled(void)
83{
84 if (!debug_locks)
85 return false;
86
Peter Zijlstrad48e3852020-10-26 16:22:56 +010087 if (this_cpu_read(lockdep_recursion))
Peter Zijlstra4d004092020-10-02 11:04:21 +020088 return false;
89
90 if (current->lockdep_recursion)
91 return false;
92
93 return true;
94}
95
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070096/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080097 * lockdep_lock: protects the lockdep graph, the hashes and the
98 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070099 *
100 * This is one of the rare exceptions where it's justified
101 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -0800102 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700103 */
Peter Zijlstra248efb22020-03-13 11:09:49 +0100104static arch_spinlock_t __lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
105static struct task_struct *__owner;
106
107static inline void lockdep_lock(void)
108{
109 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
110
Boqun Feng43be4382020-11-13 19:05:03 +0800111 __this_cpu_inc(lockdep_recursion);
Peter Zijlstra248efb22020-03-13 11:09:49 +0100112 arch_spin_lock(&__lock);
113 __owner = current;
Peter Zijlstra248efb22020-03-13 11:09:49 +0100114}
115
116static inline void lockdep_unlock(void)
117{
Boqun Feng43be4382020-11-13 19:05:03 +0800118 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
119
Peter Zijlstra248efb22020-03-13 11:09:49 +0100120 if (debug_locks && DEBUG_LOCKS_WARN_ON(__owner != current))
121 return;
122
Peter Zijlstra248efb22020-03-13 11:09:49 +0100123 __owner = NULL;
124 arch_spin_unlock(&__lock);
Boqun Feng43be4382020-11-13 19:05:03 +0800125 __this_cpu_dec(lockdep_recursion);
Peter Zijlstra248efb22020-03-13 11:09:49 +0100126}
127
128static inline bool lockdep_assert_locked(void)
129{
130 return DEBUG_LOCKS_WARN_ON(__owner != current);
131}
132
Bart Van Asschecdc84d72019-02-14 15:00:44 -0800133static struct task_struct *lockdep_selftest_task_struct;
Ingo Molnar74c383f2006-12-13 00:34:43 -0800134
Peter Zijlstra248efb22020-03-13 11:09:49 +0100135
Ingo Molnar74c383f2006-12-13 00:34:43 -0800136static int graph_lock(void)
137{
Peter Zijlstra248efb22020-03-13 11:09:49 +0100138 lockdep_lock();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800139 /*
140 * Make sure that if another CPU detected a bug while
141 * walking the graph we dont change it (while the other
142 * CPU is busy printing out stuff with the graph lock
143 * dropped already)
144 */
145 if (!debug_locks) {
Peter Zijlstra248efb22020-03-13 11:09:49 +0100146 lockdep_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800147 return 0;
148 }
149 return 1;
150}
151
Peter Zijlstra248efb22020-03-13 11:09:49 +0100152static inline void graph_unlock(void)
Ingo Molnar74c383f2006-12-13 00:34:43 -0800153{
Peter Zijlstra248efb22020-03-13 11:09:49 +0100154 lockdep_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800155}
156
157/*
158 * Turn lock debugging off and return with 0 if it was off already,
159 * and also release the graph lock:
160 */
161static inline int debug_locks_off_graph_unlock(void)
162{
163 int ret = debug_locks_off();
164
Peter Zijlstra248efb22020-03-13 11:09:49 +0100165 lockdep_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800166
167 return ret;
168}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700169
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700170unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200171static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Bart Van Asscheace35a72019-02-14 15:00:47 -0800172static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700173
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700174/*
175 * All data structures here are protected by the global debug_lock.
176 *
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800177 * nr_lock_classes is the number of elements of lock_classes[] that is
178 * in use.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700179 */
Bart Van Assche108c1482019-02-14 15:00:53 -0800180#define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
181#define KEYHASH_SIZE (1UL << KEYHASH_BITS)
182static struct hlist_head lock_keys_hash[KEYHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700183unsigned long nr_lock_classes;
Waiman Long1d44bcb2020-02-06 10:24:05 -0500184unsigned long nr_zapped_classes;
Bart Van Assche1431a5d2018-12-06 17:11:32 -0800185#ifndef CONFIG_DEBUG_LOCKDEP
186static
187#endif
Waiman Long8ca2b56c2018-10-03 13:07:18 -0400188struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
Yuyang Du01bb6f02019-05-06 16:19:25 +0800189static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700190
Dave Jonesf82b2172008-08-11 09:30:23 +0200191static inline struct lock_class *hlock_class(struct held_lock *hlock)
192{
Yuyang Du01bb6f02019-05-06 16:19:25 +0800193 unsigned int class_idx = hlock->class_idx;
194
195 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfield */
196 barrier();
197
198 if (!test_bit(class_idx, lock_classes_in_use)) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200199 /*
200 * Someone passed in garbage, we give up.
201 */
Dave Jonesf82b2172008-08-11 09:30:23 +0200202 DEBUG_LOCKS_WARN_ON(1);
203 return NULL;
204 }
Yuyang Du01bb6f02019-05-06 16:19:25 +0800205
206 /*
207 * At this point, if the passed hlock->class_idx is still garbage,
208 * we just have to live with it
209 */
210 return lock_classes + class_idx;
Dave Jonesf82b2172008-08-11 09:30:23 +0200211}
212
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700213#ifdef CONFIG_LOCK_STAT
Peter Zijlstra25528212016-03-15 14:52:49 -0700214static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700215
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200216static inline u64 lockstat_clock(void)
217{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200218 return local_clock();
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200219}
220
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200221static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700222{
223 int i;
224
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200225 for (i = 0; i < LOCKSTAT_POINTS; i++) {
226 if (points[i] == 0) {
227 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700228 break;
229 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200230 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700231 break;
232 }
233
234 return i;
235}
236
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200237static void lock_time_inc(struct lock_time *lt, u64 time)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700238{
239 if (time > lt->max)
240 lt->max = time;
241
Frank Rowand109d71c2009-11-19 13:42:06 -0800242 if (time < lt->min || !lt->nr)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700243 lt->min = time;
244
245 lt->total += time;
246 lt->nr++;
247}
248
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700249static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
250{
Frank Rowand109d71c2009-11-19 13:42:06 -0800251 if (!src->nr)
252 return;
253
254 if (src->max > dst->max)
255 dst->max = src->max;
256
257 if (src->min < dst->min || !dst->nr)
258 dst->min = src->min;
259
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700260 dst->total += src->total;
261 dst->nr += src->nr;
262}
263
264struct lock_class_stats lock_stats(struct lock_class *class)
265{
266 struct lock_class_stats stats;
267 int cpu, i;
268
269 memset(&stats, 0, sizeof(struct lock_class_stats));
270 for_each_possible_cpu(cpu) {
271 struct lock_class_stats *pcs =
Tejun Heo1871e522009-10-29 22:34:13 +0900272 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700273
274 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
275 stats.contention_point[i] += pcs->contention_point[i];
276
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200277 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
278 stats.contending_point[i] += pcs->contending_point[i];
279
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700280 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
281 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
282
283 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
284 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700285
286 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
287 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700288 }
289
290 return stats;
291}
292
293void clear_lock_stats(struct lock_class *class)
294{
295 int cpu;
296
297 for_each_possible_cpu(cpu) {
298 struct lock_class_stats *cpu_stats =
Tejun Heo1871e522009-10-29 22:34:13 +0900299 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700300
301 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
302 }
303 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200304 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700305}
306
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700307static struct lock_class_stats *get_lock_stats(struct lock_class *class)
308{
Joel Fernandes (Google)01f38492018-07-30 15:24:21 -0700309 return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes];
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700310}
311
312static void lock_release_holdtime(struct held_lock *hlock)
313{
314 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200315 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700316
317 if (!lock_stat)
318 return;
319
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200320 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700321
Dave Jonesf82b2172008-08-11 09:30:23 +0200322 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700323 if (hlock->read)
324 lock_time_inc(&stats->read_holdtime, holdtime);
325 else
326 lock_time_inc(&stats->write_holdtime, holdtime);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700327}
328#else
329static inline void lock_release_holdtime(struct held_lock *hlock)
330{
331}
332#endif
333
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700334/*
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800335 * We keep a global list of all lock classes. The list is only accessed with
336 * the lockdep spinlock lock held. free_lock_classes is a list with free
337 * elements. These elements are linked together by the lock_entry member in
338 * struct lock_class.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700339 */
340LIST_HEAD(all_lock_classes);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800341static LIST_HEAD(free_lock_classes);
342
343/**
344 * struct pending_free - information about data structures about to be freed
345 * @zapped: Head of a list with struct lock_class elements.
Bart Van Asschede4643a2019-02-14 15:00:50 -0800346 * @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements
347 * are about to be freed.
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800348 */
349struct pending_free {
350 struct list_head zapped;
Bart Van Asschede4643a2019-02-14 15:00:50 -0800351 DECLARE_BITMAP(lock_chains_being_freed, MAX_LOCKDEP_CHAINS);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800352};
353
354/**
355 * struct delayed_free - data structures used for delayed freeing
356 *
357 * A data structure for delayed freeing of data structures that may be
358 * accessed by RCU readers at the time these were freed.
359 *
360 * @rcu_head: Used to schedule an RCU callback for freeing data structures.
361 * @index: Index of @pf to which freed data structures are added.
362 * @scheduled: Whether or not an RCU callback has been scheduled.
363 * @pf: Array with information about data structures about to be freed.
364 */
365static struct delayed_free {
366 struct rcu_head rcu_head;
367 int index;
368 int scheduled;
369 struct pending_free pf[2];
370} delayed_free;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700371
372/*
373 * The lockdep classes are in a hash-table as well, for fast lookup:
374 */
375#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
376#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700377#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700378#define classhashentry(key) (classhash_table + __classhashfn((key)))
379
Andrew Mortona63f38c2016-02-03 13:44:12 -0800380static struct hlist_head classhash_table[CLASSHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700381
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700382/*
383 * We put the lock dependency chains into a hash-table as well, to cache
384 * their existence:
385 */
386#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
387#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700388#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700389#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
390
Andrew Mortona63f38c2016-02-03 13:44:12 -0800391static struct hlist_head chainhash_table[CHAINHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700392
393/*
Boqun Fengf611e8c2020-08-07 15:42:33 +0800394 * the id of held_lock
395 */
396static inline u16 hlock_id(struct held_lock *hlock)
397{
398 BUILD_BUG_ON(MAX_LOCKDEP_KEYS_BITS + 2 > 16);
399
400 return (hlock->class_idx | (hlock->read << MAX_LOCKDEP_KEYS_BITS));
401}
402
403static inline unsigned int chain_hlock_class_idx(u16 hlock_id)
404{
405 return hlock_id & (MAX_LOCKDEP_KEYS - 1);
406}
407
408/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700409 * The hash key of the lock dependency chains is a hash itself too:
410 * it's a hash of all locks taken up to that lock, including that lock.
411 * It's a 64-bit hash, because it's important for the keys to be
412 * unique.
413 */
Peter Zijlstradfaaf3f2016-05-30 18:31:33 +0200414static inline u64 iterate_chain_key(u64 key, u32 idx)
415{
416 u32 k0 = key, k1 = key >> 32;
417
418 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
419
420 return k0 | (u64)k1 << 32;
421}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700422
Yuyang Due196e472019-05-06 16:19:23 +0800423void lockdep_init_task(struct task_struct *task)
424{
425 task->lockdep_depth = 0; /* no locks held yet */
Yuyang Duf6ec8822019-05-06 16:19:24 +0800426 task->curr_chain_key = INITIAL_CHAIN_KEY;
Yuyang Due196e472019-05-06 16:19:23 +0800427 task->lockdep_recursion = 0;
428}
429
Peter Zijlstra4d004092020-10-02 11:04:21 +0200430static __always_inline void lockdep_recursion_inc(void)
431{
432 __this_cpu_inc(lockdep_recursion);
433}
434
Peter Zijlstra6eebad12020-06-03 13:40:21 +0200435static __always_inline void lockdep_recursion_finish(void)
Peter Zijlstra10476e62020-03-13 09:56:38 +0100436{
Peter Zijlstra4d004092020-10-02 11:04:21 +0200437 if (WARN_ON_ONCE(__this_cpu_dec_return(lockdep_recursion)))
438 __this_cpu_write(lockdep_recursion, 0);
Peter Zijlstra10476e62020-03-13 09:56:38 +0100439}
440
Bart Van Asschecdc84d72019-02-14 15:00:44 -0800441void lockdep_set_selftest_task(struct task_struct *task)
442{
443 lockdep_selftest_task_struct = task;
444}
445
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700446/*
447 * Debugging switches:
448 */
449
450#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800451#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700452
453#if VERBOSE
454# define HARDIRQ_VERBOSE 1
455# define SOFTIRQ_VERBOSE 1
456#else
457# define HARDIRQ_VERBOSE 0
458# define SOFTIRQ_VERBOSE 0
459#endif
460
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +0100461#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700462/*
463 * Quick filtering for interesting events:
464 */
465static int class_filter(struct lock_class *class)
466{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700467#if 0
468 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700469 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700470 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700471 return 1;
472 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700473 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700474 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700475#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800476 /* Filter everything else. 1 would be to allow everything else */
477 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700478}
479#endif
480
481static int verbose(struct lock_class *class)
482{
483#if VERBOSE
484 return class_filter(class);
485#endif
486 return 0;
487}
488
Dave Jones2c522832013-04-25 13:40:02 -0400489static void print_lockdep_off(const char *bug_msg)
490{
491 printk(KERN_DEBUG "%s\n", bug_msg);
492 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200493#ifdef CONFIG_LOCK_STAT
Dave Jones2c522832013-04-25 13:40:02 -0400494 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200495#endif
Dave Jones2c522832013-04-25 13:40:02 -0400496}
497
Arnd Bergmann886532a2019-06-17 14:47:05 +0200498unsigned long nr_stack_trace_entries;
499
Arnd Bergmann30a35f72019-06-28 12:29:03 +0200500#ifdef CONFIG_PROVE_LOCKING
Bart Van Assche12593b72019-07-22 11:24:42 -0700501/**
502 * struct lock_trace - single stack backtrace
503 * @hash_entry: Entry in a stack_trace_hash[] list.
504 * @hash: jhash() of @entries.
505 * @nr_entries: Number of entries in @entries.
506 * @entries: Actual stack backtrace.
507 */
508struct lock_trace {
509 struct hlist_node hash_entry;
510 u32 hash;
511 u32 nr_entries;
Gustavo A. R. Silvadb785382020-05-07 13:58:04 -0500512 unsigned long entries[] __aligned(sizeof(unsigned long));
Bart Van Assche12593b72019-07-22 11:24:42 -0700513};
514#define LOCK_TRACE_SIZE_IN_LONGS \
515 (sizeof(struct lock_trace) / sizeof(unsigned long))
Arnd Bergmann886532a2019-06-17 14:47:05 +0200516/*
Bart Van Assche12593b72019-07-22 11:24:42 -0700517 * Stack-trace: sequence of lock_trace structures. Protected by the graph_lock.
Arnd Bergmann886532a2019-06-17 14:47:05 +0200518 */
519static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
Bart Van Assche12593b72019-07-22 11:24:42 -0700520static struct hlist_head stack_trace_hash[STACK_TRACE_HASH_SIZE];
Arnd Bergmann886532a2019-06-17 14:47:05 +0200521
Bart Van Assche12593b72019-07-22 11:24:42 -0700522static bool traces_identical(struct lock_trace *t1, struct lock_trace *t2)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700523{
Bart Van Assche12593b72019-07-22 11:24:42 -0700524 return t1->hash == t2->hash && t1->nr_entries == t2->nr_entries &&
525 memcmp(t1->entries, t2->entries,
526 t1->nr_entries * sizeof(t1->entries[0])) == 0;
527}
528
529static struct lock_trace *save_trace(void)
530{
531 struct lock_trace *trace, *t2;
532 struct hlist_head *hash_head;
533 u32 hash;
Waiman Longd91f3052019-12-20 08:51:28 -0500534 int max_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700535
Bart Van Assche12593b72019-07-22 11:24:42 -0700536 BUILD_BUG_ON_NOT_POWER_OF_2(STACK_TRACE_HASH_SIZE);
537 BUILD_BUG_ON(LOCK_TRACE_SIZE_IN_LONGS >= MAX_STACK_TRACE_ENTRIES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700538
Bart Van Assche12593b72019-07-22 11:24:42 -0700539 trace = (struct lock_trace *)(stack_trace + nr_stack_trace_entries);
540 max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries -
541 LOCK_TRACE_SIZE_IN_LONGS;
Bart Van Assche12593b72019-07-22 11:24:42 -0700542
Waiman Longd91f3052019-12-20 08:51:28 -0500543 if (max_entries <= 0) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800544 if (!debug_locks_off_graph_unlock())
Bart Van Assche12593b72019-07-22 11:24:42 -0700545 return NULL;
Ingo Molnar74c383f2006-12-13 00:34:43 -0800546
Dave Jones2c522832013-04-25 13:40:02 -0400547 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
Ingo Molnar74c383f2006-12-13 00:34:43 -0800548 dump_stack();
549
Bart Van Assche12593b72019-07-22 11:24:42 -0700550 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700551 }
Waiman Longd91f3052019-12-20 08:51:28 -0500552 trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700553
Bart Van Assche12593b72019-07-22 11:24:42 -0700554 hash = jhash(trace->entries, trace->nr_entries *
555 sizeof(trace->entries[0]), 0);
556 trace->hash = hash;
557 hash_head = stack_trace_hash + (hash & (STACK_TRACE_HASH_SIZE - 1));
558 hlist_for_each_entry(t2, hash_head, hash_entry) {
559 if (traces_identical(trace, t2))
560 return t2;
561 }
562 nr_stack_trace_entries += LOCK_TRACE_SIZE_IN_LONGS + trace->nr_entries;
563 hlist_add_head(&trace->hash_entry, hash_head);
564
565 return trace;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700566}
Bart Van Assche8c779222019-07-22 11:24:43 -0700567
568/* Return the number of stack traces in the stack_trace[] array. */
569u64 lockdep_stack_trace_count(void)
570{
571 struct lock_trace *trace;
572 u64 c = 0;
573 int i;
574
575 for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++) {
576 hlist_for_each_entry(trace, &stack_trace_hash[i], hash_entry) {
577 c++;
578 }
579 }
580
581 return c;
582}
583
584/* Return the number of stack hash chains that have at least one stack trace. */
585u64 lockdep_stack_hash_count(void)
586{
587 u64 c = 0;
588 int i;
589
590 for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++)
591 if (!hlist_empty(&stack_trace_hash[i]))
592 c++;
593
594 return c;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700595}
Arnd Bergmann886532a2019-06-17 14:47:05 +0200596#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700597
598unsigned int nr_hardirq_chains;
599unsigned int nr_softirq_chains;
600unsigned int nr_process_chains;
601unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700602
603#ifdef CONFIG_DEBUG_LOCKDEP
604/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700605 * Various lockdep statistics:
606 */
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200607DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700608#endif
609
Arnd Bergmann30a35f72019-06-28 12:29:03 +0200610#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700611/*
612 * Locking printouts:
613 */
614
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100615#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100616 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
617 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
618 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
619 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100620
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700621static const char *usage_str[] =
622{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100623#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
624#include "lockdep_states.h"
625#undef LOCKDEP_STATE
626 [LOCK_USED] = "INITIAL USE",
Peter Zijlstra2bb89452020-09-30 11:49:37 +0200627 [LOCK_USED_READ] = "INITIAL READ USE",
628 /* abused as string storage for verify_lock_unused() */
Peter Zijlstraf6f48e12020-02-20 09:45:02 +0100629 [LOCK_USAGE_STATES] = "IN-NMI",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700630};
Arnd Bergmann886532a2019-06-17 14:47:05 +0200631#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700632
Bart Van Assche364f6af2019-07-22 11:24:40 -0700633const char *__get_key_name(const struct lockdep_subclass_key *key, char *str)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700634{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700635 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700636}
637
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100638static inline unsigned long lock_flag(enum lock_usage_bit bit)
639{
640 return 1UL << bit;
641}
642
643static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
644{
Yuyang Duc52478f2019-05-06 16:19:19 +0800645 /*
646 * The usage character defaults to '.' (i.e., irqs disabled and not in
647 * irq context), which is the safest usage category.
648 */
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100649 char c = '.';
650
Yuyang Duc52478f2019-05-06 16:19:19 +0800651 /*
652 * The order of the following usage checks matters, which will
653 * result in the outcome character as follows:
654 *
655 * - '+': irq is enabled and not in irq context
656 * - '-': in irq context and irq is disabled
657 * - '?': in irq context and irq is enabled
658 */
659 if (class->usage_mask & lock_flag(bit + LOCK_USAGE_DIR_MASK)) {
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100660 c = '+';
Yuyang Duc52478f2019-05-06 16:19:19 +0800661 if (class->usage_mask & lock_flag(bit))
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100662 c = '?';
Yuyang Duc52478f2019-05-06 16:19:19 +0800663 } else if (class->usage_mask & lock_flag(bit))
664 c = '-';
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100665
666 return c;
667}
668
Peter Zijlstraf510b232009-01-22 17:53:47 +0100669void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700670{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100671 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700672
Peter Zijlstraf510b232009-01-22 17:53:47 +0100673#define LOCKDEP_STATE(__STATE) \
674 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
675 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
676#include "lockdep_states.h"
677#undef LOCKDEP_STATE
678
679 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700680}
681
Steven Rostedte5e78d02011-11-02 20:24:16 -0400682static void __print_lock_name(struct lock_class *class)
Steven Rostedt3003eba2011-04-20 21:41:54 -0400683{
684 char str[KSYM_NAME_LEN];
685 const char *name;
686
687 name = class->name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700688 if (!name) {
689 name = __get_key_name(class->key, str);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100690 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700691 } else {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100692 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700693 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100694 printk(KERN_CONT "#%d", class->name_version);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700695 if (class->subclass)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100696 printk(KERN_CONT "/%d", class->subclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700697 }
Steven Rostedte5e78d02011-11-02 20:24:16 -0400698}
699
700static void print_lock_name(struct lock_class *class)
701{
702 char usage[LOCK_USAGE_CHARS];
703
704 get_usage_chars(class, usage);
705
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100706 printk(KERN_CONT " (");
Steven Rostedte5e78d02011-11-02 20:24:16 -0400707 __print_lock_name(class);
Peter Zijlstrade8f5e42020-03-21 12:26:01 +0100708 printk(KERN_CONT "){%s}-{%hd:%hd}", usage,
709 class->wait_type_outer ?: class->wait_type_inner,
710 class->wait_type_inner);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700711}
712
713static void print_lockdep_cache(struct lockdep_map *lock)
714{
715 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700716 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700717
718 name = lock->name;
719 if (!name)
720 name = __get_key_name(lock->key->subkeys, str);
721
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100722 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700723}
724
725static void print_lock(struct held_lock *hlock)
726{
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200727 /*
728 * We can be called locklessly through debug_show_all_locks() so be
729 * extra careful, the hlock might have been released and cleared.
Yuyang Du01bb6f02019-05-06 16:19:25 +0800730 *
731 * If this indeed happens, lets pretend it does not hurt to continue
732 * to print the lock unless the hlock class_idx does not point to a
733 * registered class. The rationale here is: since we don't attempt
734 * to distinguish whether we are in this situation, if it just
735 * happened we can't count on class_idx to tell either.
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200736 */
Yuyang Du01bb6f02019-05-06 16:19:25 +0800737 struct lock_class *lock = hlock_class(hlock);
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200738
Yuyang Du01bb6f02019-05-06 16:19:25 +0800739 if (!lock) {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100740 printk(KERN_CONT "<RELEASED>\n");
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200741 return;
742 }
743
Paul E. McKenney519248f2019-05-30 05:39:25 -0700744 printk(KERN_CONT "%px", hlock->instance);
Yuyang Du01bb6f02019-05-06 16:19:25 +0800745 print_lock_name(lock);
Tetsuo Handab3c39752018-03-27 19:41:41 +0900746 printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700747}
748
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900749static void lockdep_print_held_locks(struct task_struct *p)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700750{
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900751 int i, depth = READ_ONCE(p->lockdep_depth);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700752
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900753 if (!depth)
754 printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p));
755 else
756 printk("%d lock%s held by %s/%d:\n", depth,
757 depth > 1 ? "s" : "", p->comm, task_pid_nr(p));
758 /*
759 * It's not reliable to print a task's held locks if it's not sleeping
760 * and it's not the current task.
761 */
762 if (p->state == TASK_RUNNING && p != current)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700763 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700764 for (i = 0; i < depth; i++) {
765 printk(" #%d: ", i);
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900766 print_lock(p->held_locks + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700767 }
768}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700769
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100770static void print_kernel_ident(void)
Dave Jones99de0552006-09-29 02:00:10 -0700771{
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100772 printk("%s %.*s %s\n", init_utsname()->release,
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700773 (int)strcspn(init_utsname()->version, " "),
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100774 init_utsname()->version,
775 print_tainted());
Dave Jones99de0552006-09-29 02:00:10 -0700776}
777
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700778static int very_verbose(struct lock_class *class)
779{
780#if VERY_VERBOSE
781 return class_filter(class);
782#endif
783 return 0;
784}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700785
786/*
787 * Is this the address of a static object:
788 */
Sasha Levin8dce7a92013-06-13 18:41:16 -0400789#ifdef __KERNEL__
Bart Van Assche108c1482019-02-14 15:00:53 -0800790static int static_obj(const void *obj)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700791{
792 unsigned long start = (unsigned long) &_stext,
793 end = (unsigned long) &_end,
794 addr = (unsigned long) obj;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700795
Gerald Schaefer7a5da022019-04-18 16:24:50 +0200796 if (arch_is_kernel_initmem_freed(addr))
797 return 0;
798
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700799 /*
800 * static variable?
801 */
802 if ((addr >= start) && (addr < end))
803 return 1;
804
Mike Frysinger2a9ad182009-09-22 16:44:16 -0700805 if (arch_is_kernel_data(addr))
806 return 1;
807
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700808 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900809 * in-kernel percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700810 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900811 if (is_kernel_percpu_address(addr))
812 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700813
814 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900815 * module static or percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700816 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900817 return is_module_address(addr) || is_module_percpu_address(addr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700818}
Sasha Levin8dce7a92013-06-13 18:41:16 -0400819#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700820
821/*
822 * To make lock name printouts unique, we calculate a unique
Bart Van Asschefe27b0d2018-12-06 17:11:37 -0800823 * class->name_version generation counter. The caller must hold the graph
824 * lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700825 */
826static int count_matching_names(struct lock_class *new_class)
827{
828 struct lock_class *class;
829 int count = 0;
830
831 if (!new_class->name)
832 return 0;
833
Bart Van Asschefe27b0d2018-12-06 17:11:37 -0800834 list_for_each_entry(class, &all_lock_classes, lock_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700835 if (new_class->key - new_class->subclass == class->key)
836 return class->name_version;
837 if (class->name && !strcmp(class->name, new_class->name))
838 count = max(count, class->name_version);
839 }
840
841 return count + 1;
842}
843
Peter Zijlstraf6f48e12020-02-20 09:45:02 +0100844/* used from NMI context -- must be lockless */
Peter Zijlstra6eebad12020-06-03 13:40:21 +0200845static __always_inline struct lock_class *
Matthew Wilcox08f36ff2018-01-17 07:14:13 -0800846look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700847{
848 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -0800849 struct hlist_head *hash_head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700850 struct lock_class *class;
851
Hitoshi Mitake4ba053c2010-10-13 17:30:26 +0900852 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
853 debug_locks_off();
854 printk(KERN_ERR
855 "BUG: looking up invalid subclass: %u\n", subclass);
856 printk(KERN_ERR
857 "turning off the locking correctness validator.\n");
858 dump_stack();
859 return NULL;
860 }
861
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700862 /*
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800863 * If it is not initialised then it has never been locked,
864 * so it won't be present in the hash table.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700865 */
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800866 if (unlikely(!lock->key))
867 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700868
869 /*
870 * NOTE: the class-key must be unique. For dynamic locks, a static
871 * lock_class_key variable is passed in through the mutex_init()
872 * (or spin_lock_init()) call - which acts as the key. For static
873 * locks we use the lock object itself as the key.
874 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700875 BUILD_BUG_ON(sizeof(struct lock_class_key) >
876 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700877
878 key = lock->key->subkeys + subclass;
879
880 hash_head = classhashentry(key);
881
882 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +0100883 * We do an RCU walk of the hash, see lockdep_free_key_range().
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700884 */
Peter Zijlstra35a93932015-02-26 16:23:11 +0100885 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
886 return NULL;
887
Andrew Mortona63f38c2016-02-03 13:44:12 -0800888 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700889 if (class->key == key) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200890 /*
891 * Huh! same key, different name? Did someone trample
892 * on some memory? We're most confused.
893 */
Sebastian Andrzej Siewior97831542019-05-17 23:22:34 +0200894 WARN_ON_ONCE(class->name != lock->name &&
895 lock->key != &__lockdep_no_validate__);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700896 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700897 }
898 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700899
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800900 return NULL;
901}
902
903/*
904 * Static locks do not have their class-keys yet - for them the key is
905 * the lock object itself. If the lock is in the per cpu area, the
906 * canonical address of the lock (per cpu offset removed) is used.
907 */
908static bool assign_lock_key(struct lockdep_map *lock)
909{
910 unsigned long can_addr, addr = (unsigned long)lock;
911
Bart Van Assche4bf50862019-02-14 15:00:52 -0800912#ifdef __KERNEL__
913 /*
914 * lockdep_free_key_range() assumes that struct lock_class_key
915 * objects do not overlap. Since we use the address of lock
916 * objects as class key for static objects, check whether the
917 * size of lock_class_key objects does not exceed the size of
918 * the smallest lock object.
919 */
920 BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(raw_spinlock_t));
921#endif
922
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800923 if (__is_kernel_percpu_address(addr, &can_addr))
924 lock->key = (void *)can_addr;
925 else if (__is_module_percpu_address(addr, &can_addr))
926 lock->key = (void *)can_addr;
927 else if (static_obj(lock))
928 lock->key = (void *)lock;
929 else {
930 /* Debug-check: all keys must be persistent! */
931 debug_locks_off();
932 pr_err("INFO: trying to register non-static key.\n");
933 pr_err("the code is fine but needs lockdep annotation.\n");
934 pr_err("turning off the locking correctness validator.\n");
935 dump_stack();
936 return false;
937 }
938
939 return true;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700940}
941
Peter Zijlstra72dcd502019-02-25 15:56:32 +0100942#ifdef CONFIG_DEBUG_LOCKDEP
943
Bart Van Asscheb526b2e2019-02-14 15:00:51 -0800944/* Check whether element @e occurs in list @h */
945static bool in_list(struct list_head *e, struct list_head *h)
946{
947 struct list_head *f;
948
949 list_for_each(f, h) {
950 if (e == f)
951 return true;
952 }
953
954 return false;
955}
956
957/*
958 * Check whether entry @e occurs in any of the locks_after or locks_before
959 * lists.
960 */
961static bool in_any_class_list(struct list_head *e)
962{
963 struct lock_class *class;
964 int i;
965
966 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
967 class = &lock_classes[i];
968 if (in_list(e, &class->locks_after) ||
969 in_list(e, &class->locks_before))
970 return true;
971 }
972 return false;
973}
974
975static bool class_lock_list_valid(struct lock_class *c, struct list_head *h)
976{
977 struct lock_list *e;
978
979 list_for_each_entry(e, h, entry) {
980 if (e->links_to != c) {
981 printk(KERN_INFO "class %s: mismatch for lock entry %ld; class %s <> %s",
982 c->name ? : "(?)",
983 (unsigned long)(e - list_entries),
984 e->links_to && e->links_to->name ?
985 e->links_to->name : "(?)",
986 e->class && e->class->name ? e->class->name :
987 "(?)");
988 return false;
989 }
990 }
991 return true;
992}
993
Arnd Bergmann3fe75222019-03-07 08:52:12 +0100994#ifdef CONFIG_PROVE_LOCKING
995static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
996#endif
Bart Van Asscheb526b2e2019-02-14 15:00:51 -0800997
998static bool check_lock_chain_key(struct lock_chain *chain)
999{
1000#ifdef CONFIG_PROVE_LOCKING
Yuyang Duf6ec8822019-05-06 16:19:24 +08001001 u64 chain_key = INITIAL_CHAIN_KEY;
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001002 int i;
1003
1004 for (i = chain->base; i < chain->base + chain->depth; i++)
Yuyang Du01bb6f02019-05-06 16:19:25 +08001005 chain_key = iterate_chain_key(chain_key, chain_hlocks[i]);
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001006 /*
1007 * The 'unsigned long long' casts avoid that a compiler warning
1008 * is reported when building tools/lib/lockdep.
1009 */
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001010 if (chain->chain_key != chain_key) {
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001011 printk(KERN_INFO "chain %lld: key %#llx <> %#llx\n",
1012 (unsigned long long)(chain - lock_chains),
1013 (unsigned long long)chain->chain_key,
1014 (unsigned long long)chain_key);
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001015 return false;
1016 }
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001017#endif
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001018 return true;
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001019}
1020
1021static bool in_any_zapped_class_list(struct lock_class *class)
1022{
1023 struct pending_free *pf;
1024 int i;
1025
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001026 for (i = 0, pf = delayed_free.pf; i < ARRAY_SIZE(delayed_free.pf); i++, pf++) {
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001027 if (in_list(&class->lock_entry, &pf->zapped))
1028 return true;
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001029 }
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001030
1031 return false;
1032}
1033
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001034static bool __check_data_structures(void)
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001035{
1036 struct lock_class *class;
1037 struct lock_chain *chain;
1038 struct hlist_head *head;
1039 struct lock_list *e;
1040 int i;
1041
1042 /* Check whether all classes occur in a lock list. */
1043 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
1044 class = &lock_classes[i];
1045 if (!in_list(&class->lock_entry, &all_lock_classes) &&
1046 !in_list(&class->lock_entry, &free_lock_classes) &&
1047 !in_any_zapped_class_list(class)) {
1048 printk(KERN_INFO "class %px/%s is not in any class list\n",
1049 class, class->name ? : "(?)");
1050 return false;
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001051 }
1052 }
1053
1054 /* Check whether all classes have valid lock lists. */
1055 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
1056 class = &lock_classes[i];
1057 if (!class_lock_list_valid(class, &class->locks_before))
1058 return false;
1059 if (!class_lock_list_valid(class, &class->locks_after))
1060 return false;
1061 }
1062
1063 /* Check the chain_key of all lock chains. */
1064 for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
1065 head = chainhash_table + i;
1066 hlist_for_each_entry_rcu(chain, head, entry) {
1067 if (!check_lock_chain_key(chain))
1068 return false;
1069 }
1070 }
1071
1072 /*
1073 * Check whether all list entries that are in use occur in a class
1074 * lock list.
1075 */
1076 for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
1077 e = list_entries + i;
1078 if (!in_any_class_list(&e->entry)) {
1079 printk(KERN_INFO "list entry %d is not in any class list; class %s <> %s\n",
1080 (unsigned int)(e - list_entries),
1081 e->class->name ? : "(?)",
1082 e->links_to->name ? : "(?)");
1083 return false;
1084 }
1085 }
1086
1087 /*
1088 * Check whether all list entries that are not in use do not occur in
1089 * a class lock list.
1090 */
1091 for_each_clear_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
1092 e = list_entries + i;
1093 if (in_any_class_list(&e->entry)) {
1094 printk(KERN_INFO "list entry %d occurs in a class list; class %s <> %s\n",
1095 (unsigned int)(e - list_entries),
1096 e->class && e->class->name ? e->class->name :
1097 "(?)",
1098 e->links_to && e->links_to->name ?
1099 e->links_to->name : "(?)");
1100 return false;
1101 }
1102 }
1103
1104 return true;
1105}
1106
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001107int check_consistency = 0;
1108module_param(check_consistency, int, 0644);
1109
1110static void check_data_structures(void)
1111{
1112 static bool once = false;
1113
1114 if (check_consistency && !once) {
1115 if (!__check_data_structures()) {
1116 once = true;
1117 WARN_ON(once);
1118 }
1119 }
1120}
1121
1122#else /* CONFIG_DEBUG_LOCKDEP */
1123
1124static inline void check_data_structures(void) { }
1125
1126#endif /* CONFIG_DEBUG_LOCKDEP */
1127
Waiman Long810507f2020-02-06 10:24:08 -05001128static void init_chain_block_buckets(void);
1129
Ingo Molnard6d897c2006-07-10 04:44:04 -07001130/*
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001131 * Initialize the lock_classes[] array elements, the free_lock_classes list
1132 * and also the delayed_free structure.
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001133 */
1134static void init_data_structures_once(void)
1135{
Waiman Long810507f2020-02-06 10:24:08 -05001136 static bool __read_mostly ds_initialized, rcu_head_initialized;
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001137 int i;
1138
Bart Van Assche01265742019-03-03 10:19:01 -08001139 if (likely(rcu_head_initialized))
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001140 return;
1141
Bart Van Assche01265742019-03-03 10:19:01 -08001142 if (system_state >= SYSTEM_SCHEDULING) {
1143 init_rcu_head(&delayed_free.rcu_head);
1144 rcu_head_initialized = true;
1145 }
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001146
Bart Van Assche01265742019-03-03 10:19:01 -08001147 if (ds_initialized)
1148 return;
1149
1150 ds_initialized = true;
1151
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001152 INIT_LIST_HEAD(&delayed_free.pf[0].zapped);
1153 INIT_LIST_HEAD(&delayed_free.pf[1].zapped);
1154
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001155 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001156 list_add_tail(&lock_classes[i].lock_entry, &free_lock_classes);
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001157 INIT_LIST_HEAD(&lock_classes[i].locks_after);
1158 INIT_LIST_HEAD(&lock_classes[i].locks_before);
1159 }
Waiman Long810507f2020-02-06 10:24:08 -05001160 init_chain_block_buckets();
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001161}
1162
Bart Van Assche108c1482019-02-14 15:00:53 -08001163static inline struct hlist_head *keyhashentry(const struct lock_class_key *key)
1164{
1165 unsigned long hash = hash_long((uintptr_t)key, KEYHASH_BITS);
1166
1167 return lock_keys_hash + hash;
1168}
1169
1170/* Register a dynamically allocated key. */
1171void lockdep_register_key(struct lock_class_key *key)
1172{
1173 struct hlist_head *hash_head;
1174 struct lock_class_key *k;
1175 unsigned long flags;
1176
1177 if (WARN_ON_ONCE(static_obj(key)))
1178 return;
1179 hash_head = keyhashentry(key);
1180
1181 raw_local_irq_save(flags);
1182 if (!graph_lock())
1183 goto restore_irqs;
1184 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
1185 if (WARN_ON_ONCE(k == key))
1186 goto out_unlock;
1187 }
1188 hlist_add_head_rcu(&key->hash_entry, hash_head);
1189out_unlock:
1190 graph_unlock();
1191restore_irqs:
1192 raw_local_irq_restore(flags);
1193}
1194EXPORT_SYMBOL_GPL(lockdep_register_key);
1195
1196/* Check whether a key has been registered as a dynamic key. */
1197static bool is_dynamic_key(const struct lock_class_key *key)
1198{
1199 struct hlist_head *hash_head;
1200 struct lock_class_key *k;
1201 bool found = false;
1202
1203 if (WARN_ON_ONCE(static_obj(key)))
1204 return false;
1205
1206 /*
1207 * If lock debugging is disabled lock_keys_hash[] may contain
1208 * pointers to memory that has already been freed. Avoid triggering
1209 * a use-after-free in that case by returning early.
1210 */
1211 if (!debug_locks)
1212 return true;
1213
1214 hash_head = keyhashentry(key);
1215
1216 rcu_read_lock();
1217 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
1218 if (k == key) {
1219 found = true;
1220 break;
1221 }
1222 }
1223 rcu_read_unlock();
1224
1225 return found;
1226}
1227
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001228/*
Ingo Molnard6d897c2006-07-10 04:44:04 -07001229 * Register a lock's class in the hash-table, if the class is not present
1230 * yet. Otherwise we look it up. We cache the result in the lock object
1231 * itself, so actual lookup of the hash should be once per lock object.
1232 */
Denys Vlasenkoc003ed92016-04-08 20:58:46 +02001233static struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04001234register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -07001235{
1236 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -08001237 struct hlist_head *hash_head;
Ingo Molnard6d897c2006-07-10 04:44:04 -07001238 struct lock_class *class;
Peter Zijlstra35a93932015-02-26 16:23:11 +01001239
1240 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
Ingo Molnard6d897c2006-07-10 04:44:04 -07001241
1242 class = look_up_lock_class(lock, subclass);
Matthew Wilcox64f29d12018-01-17 07:14:12 -08001243 if (likely(class))
Yong Zhang87cdee72011-11-09 16:07:14 +08001244 goto out_set_class_cache;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001245
Matthew Wilcox64f29d12018-01-17 07:14:12 -08001246 if (!lock->key) {
1247 if (!assign_lock_key(lock))
1248 return NULL;
Bart Van Assche108c1482019-02-14 15:00:53 -08001249 } else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001250 return NULL;
1251 }
1252
Ingo Molnard6d897c2006-07-10 04:44:04 -07001253 key = lock->key->subkeys + subclass;
1254 hash_head = classhashentry(key);
1255
Ingo Molnar74c383f2006-12-13 00:34:43 -08001256 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001257 return NULL;
1258 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001259 /*
1260 * We have to do the hash-walk again, to avoid races
1261 * with another CPU:
1262 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08001263 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001264 if (class->key == key)
1265 goto out_unlock_set;
Peter Zijlstra35a93932015-02-26 16:23:11 +01001266 }
1267
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001268 init_data_structures_once();
1269
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001270 /* Allocate a new lock class and add it to the hash. */
1271 class = list_first_entry_or_null(&free_lock_classes, typeof(*class),
1272 lock_entry);
1273 if (!class) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001274 if (!debug_locks_off_graph_unlock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001275 return NULL;
1276 }
Ingo Molnar74c383f2006-12-13 00:34:43 -08001277
Dave Jones2c522832013-04-25 13:40:02 -04001278 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01001279 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001280 return NULL;
1281 }
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001282 nr_lock_classes++;
Yuyang Du01bb6f02019-05-06 16:19:25 +08001283 __set_bit(class - lock_classes, lock_classes_in_use);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001284 debug_atomic_inc(nr_unused_locks);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001285 class->key = key;
1286 class->name = lock->name;
1287 class->subclass = subclass;
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001288 WARN_ON_ONCE(!list_empty(&class->locks_before));
1289 WARN_ON_ONCE(!list_empty(&class->locks_after));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001290 class->name_version = count_matching_names(class);
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01001291 class->wait_type_inner = lock->wait_type_inner;
1292 class->wait_type_outer = lock->wait_type_outer;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001293 /*
1294 * We use RCU's safe list-add method to make
1295 * parallel walking of the hash-list safe:
1296 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08001297 hlist_add_head_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +01001298 /*
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001299 * Remove the class from the free list and add it to the global list
1300 * of classes.
Dale Farnsworth14811972008-02-25 23:03:02 +01001301 */
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001302 list_move_tail(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001303
1304 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001305 graph_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -08001306
Borislav Petkov04860d42018-02-26 14:49:26 +01001307 printk("\nnew class %px: %s", class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001308 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001309 printk(KERN_CONT "#%d", class->name_version);
1310 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001311 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -08001312
Ingo Molnar74c383f2006-12-13 00:34:43 -08001313 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001314 return NULL;
1315 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001316 }
1317out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -08001318 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001319
Yong Zhang87cdee72011-11-09 16:07:14 +08001320out_set_class_cache:
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04001321 if (!subclass || force)
Hitoshi Mitake62016252010-10-05 18:01:51 +09001322 lock->class_cache[0] = class;
1323 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
1324 lock->class_cache[subclass] = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001325
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001326 /*
1327 * Hash collision, did we smoke some? We found a class with a matching
1328 * hash but the subclass -- which is hashed in -- didn't match.
1329 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08001330 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
1331 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001332
1333 return class;
1334}
1335
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001336#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001337/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07001338 * Allocate a lockdep entry. (assumes the graph_lock held, returns
1339 * with NULL on failure)
1340 */
1341static struct lock_list *alloc_list_entry(void)
1342{
Bart Van Asscheace35a72019-02-14 15:00:47 -08001343 int idx = find_first_zero_bit(list_entries_in_use,
1344 ARRAY_SIZE(list_entries));
1345
1346 if (idx >= ARRAY_SIZE(list_entries)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001347 if (!debug_locks_off_graph_unlock())
1348 return NULL;
1349
Dave Jones2c522832013-04-25 13:40:02 -04001350 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01001351 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -07001352 return NULL;
1353 }
Bart Van Asscheace35a72019-02-14 15:00:47 -08001354 nr_list_entries++;
1355 __set_bit(idx, list_entries_in_use);
1356 return list_entries + idx;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001357}
1358
1359/*
1360 * Add a new dependency to the head of the list:
1361 */
Bart Van Assche86cffb82019-02-14 15:00:41 -08001362static int add_lock_to_list(struct lock_class *this,
1363 struct lock_class *links_to, struct list_head *head,
Boqun Feng3454a362020-08-07 15:42:25 +08001364 unsigned long ip, u16 distance, u8 dep,
Bart Van Assche12593b72019-07-22 11:24:42 -07001365 const struct lock_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001366{
1367 struct lock_list *entry;
1368 /*
1369 * Lock not present yet - get a new dependency struct and
1370 * add it to the list:
1371 */
1372 entry = alloc_list_entry();
1373 if (!entry)
1374 return 0;
1375
Zhu Yi74870172008-08-27 14:33:00 +08001376 entry->class = this;
Bart Van Assche86cffb82019-02-14 15:00:41 -08001377 entry->links_to = links_to;
Boqun Feng3454a362020-08-07 15:42:25 +08001378 entry->dep = dep;
Zhu Yi74870172008-08-27 14:33:00 +08001379 entry->distance = distance;
Bart Van Assche12593b72019-07-22 11:24:42 -07001380 entry->trace = trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001381 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +01001382 * Both allocation and removal are done under the graph lock; but
1383 * iteration is under RCU-sched; see look_up_lock_class() and
1384 * lockdep_free_key_range().
Peter Zijlstra8e182572007-07-19 01:48:54 -07001385 */
1386 list_add_tail_rcu(&entry->entry, head);
1387
1388 return 1;
1389}
1390
Peter Zijlstra98c33ed2009-07-21 13:19:07 +02001391/*
1392 * For good efficiency of modular, we use power of 2
1393 */
Peter Zijlstraaf012962009-07-16 15:44:29 +02001394#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
1395#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
1396
Peter Zijlstra98c33ed2009-07-21 13:19:07 +02001397/*
Yuyang Duaa480772019-05-06 16:19:28 +08001398 * The circular_queue and helpers are used to implement graph
1399 * breadth-first search (BFS) algorithm, by which we can determine
1400 * whether there is a path from a lock to another. In deadlock checks,
1401 * a path from the next lock to be acquired to a previous held lock
1402 * indicates that adding the <prev> -> <next> lock dependency will
1403 * produce a circle in the graph. Breadth-first search instead of
1404 * depth-first search is used in order to find the shortest (circular)
1405 * path.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +02001406 */
Peter Zijlstraaf012962009-07-16 15:44:29 +02001407struct circular_queue {
Yuyang Duaa480772019-05-06 16:19:28 +08001408 struct lock_list *element[MAX_CIRCULAR_QUEUE_SIZE];
Peter Zijlstraaf012962009-07-16 15:44:29 +02001409 unsigned int front, rear;
1410};
1411
1412static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001413
Ming Lei12f3dfd2009-07-16 15:44:29 +02001414unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001415
Ming Leie351b662009-07-22 22:48:09 +08001416static unsigned int lockdep_dependency_gen_id;
1417
Peter Zijlstraaf012962009-07-16 15:44:29 +02001418static inline void __cq_init(struct circular_queue *cq)
1419{
1420 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +08001421 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001422}
1423
1424static inline int __cq_empty(struct circular_queue *cq)
1425{
1426 return (cq->front == cq->rear);
1427}
1428
1429static inline int __cq_full(struct circular_queue *cq)
1430{
1431 return ((cq->rear + 1) & CQ_MASK) == cq->front;
1432}
1433
Yuyang Duaa480772019-05-06 16:19:28 +08001434static inline int __cq_enqueue(struct circular_queue *cq, struct lock_list *elem)
Peter Zijlstraaf012962009-07-16 15:44:29 +02001435{
1436 if (__cq_full(cq))
1437 return -1;
1438
1439 cq->element[cq->rear] = elem;
1440 cq->rear = (cq->rear + 1) & CQ_MASK;
1441 return 0;
1442}
1443
Yuyang Duc1661322019-05-06 16:19:29 +08001444/*
1445 * Dequeue an element from the circular_queue, return a lock_list if
1446 * the queue is not empty, or NULL if otherwise.
1447 */
1448static inline struct lock_list * __cq_dequeue(struct circular_queue *cq)
Peter Zijlstraaf012962009-07-16 15:44:29 +02001449{
Yuyang Duc1661322019-05-06 16:19:29 +08001450 struct lock_list * lock;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001451
Yuyang Duc1661322019-05-06 16:19:29 +08001452 if (__cq_empty(cq))
1453 return NULL;
1454
1455 lock = cq->element[cq->front];
Peter Zijlstraaf012962009-07-16 15:44:29 +02001456 cq->front = (cq->front + 1) & CQ_MASK;
Yuyang Duc1661322019-05-06 16:19:29 +08001457
1458 return lock;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001459}
1460
1461static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
1462{
1463 return (cq->rear - cq->front) & CQ_MASK;
1464}
1465
Boqun Fengd563bc62020-08-07 15:42:23 +08001466static inline void mark_lock_accessed(struct lock_list *lock)
Peter Zijlstraaf012962009-07-16 15:44:29 +02001467{
Ming Leie351b662009-07-22 22:48:09 +08001468 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001469}
1470
Boqun Fengd563bc62020-08-07 15:42:23 +08001471static inline void visit_lock_entry(struct lock_list *lock,
1472 struct lock_list *parent)
1473{
1474 lock->parent = parent;
1475}
1476
Peter Zijlstraaf012962009-07-16 15:44:29 +02001477static inline unsigned long lock_accessed(struct lock_list *lock)
1478{
Ming Leie351b662009-07-22 22:48:09 +08001479 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001480}
1481
1482static inline struct lock_list *get_lock_parent(struct lock_list *child)
1483{
1484 return child->parent;
1485}
1486
1487static inline int get_lock_depth(struct lock_list *child)
1488{
1489 int depth = 0;
1490 struct lock_list *parent;
1491
1492 while ((parent = get_lock_parent(child))) {
1493 child = parent;
1494 depth++;
1495 }
1496 return depth;
1497}
1498
Yuyang Du77a80692019-05-06 16:19:30 +08001499/*
1500 * Return the forward or backward dependency list.
1501 *
1502 * @lock: the lock_list to get its class's dependency list
1503 * @offset: the offset to struct lock_class to determine whether it is
1504 * locks_after or locks_before
1505 */
1506static inline struct list_head *get_dep_list(struct lock_list *lock, int offset)
1507{
1508 void *lock_class = lock->class;
1509
1510 return lock_class + offset;
1511}
Boqun Fengb11be022020-08-07 15:42:22 +08001512/*
1513 * Return values of a bfs search:
1514 *
1515 * BFS_E* indicates an error
1516 * BFS_R* indicates a result (match or not)
1517 *
1518 * BFS_EINVALIDNODE: Find a invalid node in the graph.
1519 *
1520 * BFS_EQUEUEFULL: The queue is full while doing the bfs.
1521 *
1522 * BFS_RMATCH: Find the matched node in the graph, and put that node into
1523 * *@target_entry.
1524 *
1525 * BFS_RNOMATCH: Haven't found the matched node and keep *@target_entry
1526 * _unchanged_.
1527 */
1528enum bfs_result {
1529 BFS_EINVALIDNODE = -2,
1530 BFS_EQUEUEFULL = -1,
1531 BFS_RMATCH = 0,
1532 BFS_RNOMATCH = 1,
1533};
1534
1535/*
1536 * bfs_result < 0 means error
1537 */
1538static inline bool bfs_error(enum bfs_result res)
1539{
1540 return res < 0;
1541}
Yuyang Du77a80692019-05-06 16:19:30 +08001542
Yuyang Du154f1852019-05-06 16:19:31 +08001543/*
Boqun Feng3454a362020-08-07 15:42:25 +08001544 * DEP_*_BIT in lock_list::dep
1545 *
1546 * For dependency @prev -> @next:
1547 *
1548 * SR: @prev is shared reader (->read != 0) and @next is recursive reader
1549 * (->read == 2)
1550 * ER: @prev is exclusive locker (->read == 0) and @next is recursive reader
1551 * SN: @prev is shared reader and @next is non-recursive locker (->read != 2)
1552 * EN: @prev is exclusive locker and @next is non-recursive locker
1553 *
1554 * Note that we define the value of DEP_*_BITs so that:
1555 * bit0 is prev->read == 0
1556 * bit1 is next->read != 2
1557 */
1558#define DEP_SR_BIT (0 + (0 << 1)) /* 0 */
1559#define DEP_ER_BIT (1 + (0 << 1)) /* 1 */
1560#define DEP_SN_BIT (0 + (1 << 1)) /* 2 */
1561#define DEP_EN_BIT (1 + (1 << 1)) /* 3 */
1562
1563#define DEP_SR_MASK (1U << (DEP_SR_BIT))
1564#define DEP_ER_MASK (1U << (DEP_ER_BIT))
1565#define DEP_SN_MASK (1U << (DEP_SN_BIT))
1566#define DEP_EN_MASK (1U << (DEP_EN_BIT))
1567
1568static inline unsigned int
1569__calc_dep_bit(struct held_lock *prev, struct held_lock *next)
1570{
1571 return (prev->read == 0) + ((next->read != 2) << 1);
1572}
1573
1574static inline u8 calc_dep(struct held_lock *prev, struct held_lock *next)
1575{
1576 return 1U << __calc_dep_bit(prev, next);
1577}
1578
1579/*
1580 * calculate the dep_bit for backwards edges. We care about whether @prev is
1581 * shared and whether @next is recursive.
1582 */
1583static inline unsigned int
1584__calc_dep_bitb(struct held_lock *prev, struct held_lock *next)
1585{
1586 return (next->read != 2) + ((prev->read == 0) << 1);
1587}
1588
1589static inline u8 calc_depb(struct held_lock *prev, struct held_lock *next)
1590{
1591 return 1U << __calc_dep_bitb(prev, next);
1592}
1593
1594/*
Boqun Feng6971c0f2020-08-07 15:42:26 +08001595 * Initialize a lock_list entry @lock belonging to @class as the root for a BFS
1596 * search.
1597 */
1598static inline void __bfs_init_root(struct lock_list *lock,
1599 struct lock_class *class)
1600{
1601 lock->class = class;
1602 lock->parent = NULL;
1603 lock->only_xr = 0;
1604}
1605
1606/*
1607 * Initialize a lock_list entry @lock based on a lock acquisition @hlock as the
1608 * root for a BFS search.
1609 *
1610 * ->only_xr of the initial lock node is set to @hlock->read == 2, to make sure
1611 * that <prev> -> @hlock and @hlock -> <whatever __bfs() found> is not -(*R)->
1612 * and -(S*)->.
1613 */
1614static inline void bfs_init_root(struct lock_list *lock,
1615 struct held_lock *hlock)
1616{
1617 __bfs_init_root(lock, hlock_class(hlock));
1618 lock->only_xr = (hlock->read == 2);
1619}
1620
1621/*
1622 * Similar to bfs_init_root() but initialize the root for backwards BFS.
1623 *
1624 * ->only_xr of the initial lock node is set to @hlock->read != 0, to make sure
1625 * that <next> -> @hlock and @hlock -> <whatever backwards BFS found> is not
1626 * -(*S)-> and -(R*)-> (reverse order of -(*R)-> and -(S*)->).
1627 */
1628static inline void bfs_init_rootb(struct lock_list *lock,
1629 struct held_lock *hlock)
1630{
1631 __bfs_init_root(lock, hlock_class(hlock));
1632 lock->only_xr = (hlock->read != 0);
1633}
1634
Boqun Feng6d1823c2020-09-17 16:01:50 +08001635static inline struct lock_list *__bfs_next(struct lock_list *lock, int offset)
1636{
1637 if (!lock || !lock->parent)
1638 return NULL;
1639
1640 return list_next_or_null_rcu(get_dep_list(lock->parent, offset),
1641 &lock->entry, struct lock_list, entry);
1642}
1643
Boqun Feng6971c0f2020-08-07 15:42:26 +08001644/*
1645 * Breadth-First Search to find a strong path in the dependency graph.
1646 *
1647 * @source_entry: the source of the path we are searching for.
1648 * @data: data used for the second parameter of @match function
1649 * @match: match function for the search
1650 * @target_entry: pointer to the target of a matched path
1651 * @offset: the offset to struct lock_class to determine whether it is
1652 * locks_after or locks_before
1653 *
1654 * We may have multiple edges (considering different kinds of dependencies,
1655 * e.g. ER and SN) between two nodes in the dependency graph. But
1656 * only the strong dependency path in the graph is relevant to deadlocks. A
1657 * strong dependency path is a dependency path that doesn't have two adjacent
1658 * dependencies as -(*R)-> -(S*)->, please see:
1659 *
1660 * Documentation/locking/lockdep-design.rst
1661 *
1662 * for more explanation of the definition of strong dependency paths
1663 *
1664 * In __bfs(), we only traverse in the strong dependency path:
1665 *
1666 * In lock_list::only_xr, we record whether the previous dependency only
1667 * has -(*R)-> in the search, and if it does (prev only has -(*R)->), we
1668 * filter out any -(S*)-> in the current dependency and after that, the
1669 * ->only_xr is set according to whether we only have -(*R)-> left.
Yuyang Du154f1852019-05-06 16:19:31 +08001670 */
Boqun Fengb11be022020-08-07 15:42:22 +08001671static enum bfs_result __bfs(struct lock_list *source_entry,
1672 void *data,
Boqun Feng61775ed2020-08-07 15:42:27 +08001673 bool (*match)(struct lock_list *entry, void *data),
Boqun Fengb11be022020-08-07 15:42:22 +08001674 struct lock_list **target_entry,
1675 int offset)
Ming Leic94aa5c2009-07-16 15:44:29 +02001676{
Ming Leic94aa5c2009-07-16 15:44:29 +02001677 struct circular_queue *cq = &lock_cq;
Boqun Feng6d1823c2020-09-17 16:01:50 +08001678 struct lock_list *lock = NULL;
1679 struct lock_list *entry;
1680 struct list_head *head;
1681 unsigned int cq_depth;
1682 bool first;
Ming Leic94aa5c2009-07-16 15:44:29 +02001683
Peter Zijlstra248efb22020-03-13 11:09:49 +01001684 lockdep_assert_locked();
1685
Ming Leid588e462009-07-16 15:44:29 +02001686 __cq_init(cq);
Yuyang Duaa480772019-05-06 16:19:28 +08001687 __cq_enqueue(cq, source_entry);
Ming Leic94aa5c2009-07-16 15:44:29 +02001688
Boqun Feng6d1823c2020-09-17 16:01:50 +08001689 while ((lock = __bfs_next(lock, offset)) || (lock = __cq_dequeue(cq))) {
1690 if (!lock->class)
1691 return BFS_EINVALIDNODE;
Ming Leic94aa5c2009-07-16 15:44:29 +02001692
Boqun Fengd563bc62020-08-07 15:42:23 +08001693 /*
Boqun Feng6d1823c2020-09-17 16:01:50 +08001694 * Step 1: check whether we already finish on this one.
1695 *
Boqun Fengd563bc62020-08-07 15:42:23 +08001696 * If we have visited all the dependencies from this @lock to
1697 * others (iow, if we have visited all lock_list entries in
1698 * @lock->class->locks_{after,before}) we skip, otherwise go
1699 * and visit all the dependencies in the list and mark this
1700 * list accessed.
1701 */
1702 if (lock_accessed(lock))
1703 continue;
1704 else
1705 mark_lock_accessed(lock);
1706
Boqun Feng6d1823c2020-09-17 16:01:50 +08001707 /*
1708 * Step 2: check whether prev dependency and this form a strong
1709 * dependency path.
1710 */
1711 if (lock->parent) { /* Parent exists, check prev dependency */
1712 u8 dep = lock->dep;
1713 bool prev_only_xr = lock->parent->only_xr;
Boqun Feng6971c0f2020-08-07 15:42:26 +08001714
1715 /*
1716 * Mask out all -(S*)-> if we only have *R in previous
1717 * step, because -(*R)-> -(S*)-> don't make up a strong
1718 * dependency.
1719 */
1720 if (prev_only_xr)
1721 dep &= ~(DEP_SR_MASK | DEP_SN_MASK);
1722
1723 /* If nothing left, we skip */
1724 if (!dep)
1725 continue;
1726
1727 /* If there are only -(*R)-> left, set that for the next step */
Boqun Feng6d1823c2020-09-17 16:01:50 +08001728 lock->only_xr = !(dep & (DEP_SN_MASK | DEP_EN_MASK));
1729 }
Boqun Fengd563bc62020-08-07 15:42:23 +08001730
Boqun Feng6d1823c2020-09-17 16:01:50 +08001731 /*
1732 * Step 3: we haven't visited this and there is a strong
1733 * dependency path to this, so check with @match.
1734 */
1735 if (match(lock, data)) {
1736 *target_entry = lock;
1737 return BFS_RMATCH;
1738 }
1739
1740 /*
1741 * Step 4: if not match, expand the path by adding the
1742 * forward or backwards dependencis in the search
1743 *
1744 */
1745 first = true;
1746 head = get_dep_list(lock, offset);
1747 list_for_each_entry_rcu(entry, head, entry) {
Boqun Fengd563bc62020-08-07 15:42:23 +08001748 visit_lock_entry(entry, lock);
Boqun Fengd563bc62020-08-07 15:42:23 +08001749
Boqun Feng6d1823c2020-09-17 16:01:50 +08001750 /*
1751 * Note we only enqueue the first of the list into the
1752 * queue, because we can always find a sibling
1753 * dependency from one (see __bfs_next()), as a result
1754 * the space of queue is saved.
1755 */
1756 if (!first)
1757 continue;
1758
1759 first = false;
1760
1761 if (__cq_enqueue(cq, entry))
1762 return BFS_EQUEUEFULL;
1763
Boqun Fengd563bc62020-08-07 15:42:23 +08001764 cq_depth = __cq_get_elem_count(cq);
1765 if (max_bfs_queue_depth < cq_depth)
1766 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001767 }
1768 }
Boqun Feng6d1823c2020-09-17 16:01:50 +08001769
1770 return BFS_RNOMATCH;
Ming Leic94aa5c2009-07-16 15:44:29 +02001771}
1772
Boqun Fengb11be022020-08-07 15:42:22 +08001773static inline enum bfs_result
1774__bfs_forwards(struct lock_list *src_entry,
1775 void *data,
Boqun Feng61775ed2020-08-07 15:42:27 +08001776 bool (*match)(struct lock_list *entry, void *data),
Boqun Fengb11be022020-08-07 15:42:22 +08001777 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001778{
Yuyang Du77a80692019-05-06 16:19:30 +08001779 return __bfs(src_entry, data, match, target_entry,
1780 offsetof(struct lock_class, locks_after));
Ming Leic94aa5c2009-07-16 15:44:29 +02001781
1782}
1783
Boqun Fengb11be022020-08-07 15:42:22 +08001784static inline enum bfs_result
1785__bfs_backwards(struct lock_list *src_entry,
1786 void *data,
Boqun Feng61775ed2020-08-07 15:42:27 +08001787 bool (*match)(struct lock_list *entry, void *data),
Boqun Fengb11be022020-08-07 15:42:22 +08001788 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001789{
Yuyang Du77a80692019-05-06 16:19:30 +08001790 return __bfs(src_entry, data, match, target_entry,
1791 offsetof(struct lock_class, locks_before));
Ming Leic94aa5c2009-07-16 15:44:29 +02001792
1793}
1794
Bart Van Assche12593b72019-07-22 11:24:42 -07001795static void print_lock_trace(const struct lock_trace *trace,
1796 unsigned int spaces)
Thomas Gleixnerc120bce2019-04-25 11:45:12 +02001797{
Bart Van Assche12593b72019-07-22 11:24:42 -07001798 stack_trace_print(trace->entries, trace->nr_entries, spaces);
Thomas Gleixnerc120bce2019-04-25 11:45:12 +02001799}
1800
Peter Zijlstra8e182572007-07-19 01:48:54 -07001801/*
1802 * Print a dependency chain entry (this is only done when a deadlock
1803 * has been detected):
1804 */
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001805static noinline void
Ming Lei24208ca2009-07-16 15:44:29 +02001806print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001807{
1808 if (debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001809 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001810 printk("\n-> #%u", depth);
1811 print_lock_name(target->class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001812 printk(KERN_CONT ":\n");
Bart Van Assche12593b72019-07-22 11:24:42 -07001813 print_lock_trace(target->trace, 6);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001814}
1815
Steven Rostedtf4185812011-04-20 21:41:55 -04001816static void
1817print_circular_lock_scenario(struct held_lock *src,
1818 struct held_lock *tgt,
1819 struct lock_list *prt)
1820{
1821 struct lock_class *source = hlock_class(src);
1822 struct lock_class *target = hlock_class(tgt);
1823 struct lock_class *parent = prt->class;
1824
1825 /*
1826 * A direct locking problem where unsafe_class lock is taken
1827 * directly by safe_class lock, then all we need to show
1828 * is the deadlock scenario, as it is obvious that the
1829 * unsafe lock is taken under the safe lock.
1830 *
1831 * But if there is a chain instead, where the safe lock takes
1832 * an intermediate lock (middle_class) where this lock is
1833 * not the same as the safe lock, then the lock chain is
1834 * used to describe the problem. Otherwise we would need
1835 * to show a different CPU case for each link in the chain
1836 * from the safe_class lock to the unsafe_class lock.
1837 */
1838 if (parent != source) {
1839 printk("Chain exists of:\n ");
1840 __print_lock_name(source);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001841 printk(KERN_CONT " --> ");
Steven Rostedtf4185812011-04-20 21:41:55 -04001842 __print_lock_name(parent);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001843 printk(KERN_CONT " --> ");
Steven Rostedtf4185812011-04-20 21:41:55 -04001844 __print_lock_name(target);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001845 printk(KERN_CONT "\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001846 }
1847
Ingo Molnare966eae2017-12-12 12:31:16 +01001848 printk(" Possible unsafe locking scenario:\n\n");
1849 printk(" CPU0 CPU1\n");
1850 printk(" ---- ----\n");
1851 printk(" lock(");
1852 __print_lock_name(target);
1853 printk(KERN_CONT ");\n");
1854 printk(" lock(");
1855 __print_lock_name(parent);
1856 printk(KERN_CONT ");\n");
1857 printk(" lock(");
1858 __print_lock_name(target);
1859 printk(KERN_CONT ");\n");
1860 printk(" lock(");
1861 __print_lock_name(source);
1862 printk(KERN_CONT ");\n");
1863 printk("\n *** DEADLOCK ***\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001864}
1865
Peter Zijlstra8e182572007-07-19 01:48:54 -07001866/*
1867 * When a circular dependency is detected, print the
1868 * header first:
1869 */
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001870static noinline void
Ming Leidb0002a2009-07-16 15:44:29 +02001871print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1872 struct held_lock *check_src,
1873 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001874{
1875 struct task_struct *curr = current;
1876
Ming Leic94aa5c2009-07-16 15:44:29 +02001877 if (debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001878 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001879
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001880 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001881 pr_warn("======================================================\n");
1882 pr_warn("WARNING: possible circular locking dependency detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001883 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001884 pr_warn("------------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001885 pr_warn("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001886 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001887 print_lock(check_src);
Byungchul Park383a4bc2017-08-07 16:12:55 +09001888
Ingo Molnare966eae2017-12-12 12:31:16 +01001889 pr_warn("\nbut task is already holding lock:\n");
Byungchul Park383a4bc2017-08-07 16:12:55 +09001890
Ming Leidb0002a2009-07-16 15:44:29 +02001891 print_lock(check_tgt);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001892 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1893 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001894
1895 print_circular_bug_entry(entry, depth);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001896}
1897
Boqun Feng68e30562020-08-07 15:42:29 +08001898/*
1899 * We are about to add A -> B into the dependency graph, and in __bfs() a
1900 * strong dependency path A -> .. -> B is found: hlock_class equals
1901 * entry->class.
1902 *
1903 * If A -> .. -> B can replace A -> B in any __bfs() search (means the former
1904 * is _stronger_ than or equal to the latter), we consider A -> B as redundant.
1905 * For example if A -> .. -> B is -(EN)-> (i.e. A -(E*)-> .. -(*N)-> B), and A
1906 * -> B is -(ER)-> or -(EN)->, then we don't need to add A -> B into the
1907 * dependency graph, as any strong path ..-> A -> B ->.. we can get with
1908 * having dependency A -> B, we could already get a equivalent path ..-> A ->
1909 * .. -> B -> .. with A -> .. -> B. Therefore A -> B is reduntant.
1910 *
1911 * We need to make sure both the start and the end of A -> .. -> B is not
1912 * weaker than A -> B. For the start part, please see the comment in
1913 * check_redundant(). For the end part, we need:
1914 *
1915 * Either
1916 *
1917 * a) A -> B is -(*R)-> (everything is not weaker than that)
1918 *
1919 * or
1920 *
1921 * b) A -> .. -> B is -(*N)-> (nothing is stronger than this)
1922 *
1923 */
1924static inline bool hlock_equal(struct lock_list *entry, void *data)
Ming Lei9e2d5512009-07-16 15:44:29 +02001925{
Boqun Feng68e30562020-08-07 15:42:29 +08001926 struct held_lock *hlock = (struct held_lock *)data;
1927
1928 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */
1929 (hlock->read == 2 || /* A -> B is -(*R)-> */
1930 !entry->only_xr); /* A -> .. -> B is -(*N)-> */
Ming Lei9e2d5512009-07-16 15:44:29 +02001931}
1932
Boqun Feng9de0c9b2020-08-07 15:42:28 +08001933/*
1934 * We are about to add B -> A into the dependency graph, and in __bfs() a
1935 * strong dependency path A -> .. -> B is found: hlock_class equals
1936 * entry->class.
1937 *
1938 * We will have a deadlock case (conflict) if A -> .. -> B -> A is a strong
1939 * dependency cycle, that means:
1940 *
1941 * Either
1942 *
1943 * a) B -> A is -(E*)->
1944 *
1945 * or
1946 *
1947 * b) A -> .. -> B is -(*N)-> (i.e. A -> .. -(*N)-> B)
1948 *
1949 * as then we don't have -(*R)-> -(S*)-> in the cycle.
1950 */
1951static inline bool hlock_conflict(struct lock_list *entry, void *data)
1952{
1953 struct held_lock *hlock = (struct held_lock *)data;
1954
1955 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */
1956 (hlock->read == 0 || /* B -> A is -(E*)-> */
1957 !entry->only_xr); /* A -> .. -> B is -(*N)-> */
1958}
1959
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001960static noinline void print_circular_bug(struct lock_list *this,
Boqun Feng9de0c9b2020-08-07 15:42:28 +08001961 struct lock_list *target,
1962 struct held_lock *check_src,
1963 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001964{
1965 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001966 struct lock_list *parent;
Steven Rostedtf4185812011-04-20 21:41:55 -04001967 struct lock_list *first_parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001968 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001969
Ming Leic94aa5c2009-07-16 15:44:29 +02001970 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001971 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001972
Bart Van Assche12593b72019-07-22 11:24:42 -07001973 this->trace = save_trace();
1974 if (!this->trace)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001975 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001976
Ming Leic94aa5c2009-07-16 15:44:29 +02001977 depth = get_lock_depth(target);
1978
Ming Leidb0002a2009-07-16 15:44:29 +02001979 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02001980
1981 parent = get_lock_parent(target);
Steven Rostedtf4185812011-04-20 21:41:55 -04001982 first_parent = parent;
Ming Leic94aa5c2009-07-16 15:44:29 +02001983
1984 while (parent) {
1985 print_circular_bug_entry(parent, --depth);
1986 parent = get_lock_parent(parent);
1987 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07001988
1989 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001990 print_circular_lock_scenario(check_src, check_tgt,
1991 first_parent);
1992
Peter Zijlstra8e182572007-07-19 01:48:54 -07001993 lockdep_print_held_locks(curr);
1994
1995 printk("\nstack backtrace:\n");
1996 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -07001997}
1998
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001999static noinline void print_bfs_bug(int ret)
Ming Leidb0002a2009-07-16 15:44:29 +02002000{
2001 if (!debug_locks_off_graph_unlock())
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002002 return;
Ming Leidb0002a2009-07-16 15:44:29 +02002003
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002004 /*
2005 * Breadth-first-search failed, graph got corrupted?
2006 */
Ming Leidb0002a2009-07-16 15:44:29 +02002007 WARN(1, "lockdep bfs error:%d\n", ret);
Ming Leidb0002a2009-07-16 15:44:29 +02002008}
2009
Boqun Feng61775ed2020-08-07 15:42:27 +08002010static bool noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07002011{
Ming Leief681022009-07-16 15:44:29 +02002012 (*(unsigned long *)data)++;
Boqun Feng61775ed2020-08-07 15:42:27 +08002013 return false;
David Miller419ca3f2008-07-29 21:45:03 -07002014}
2015
Fengguang Wu5216d532013-11-09 00:55:35 +08002016static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
Ming Leief681022009-07-16 15:44:29 +02002017{
2018 unsigned long count = 0;
Kees Cook3f649ab2020-06-03 13:09:38 -07002019 struct lock_list *target_entry;
Ming Leief681022009-07-16 15:44:29 +02002020
2021 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
2022
2023 return count;
2024}
David Miller419ca3f2008-07-29 21:45:03 -07002025unsigned long lockdep_count_forward_deps(struct lock_class *class)
2026{
2027 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02002028 struct lock_list this;
2029
Boqun Feng6971c0f2020-08-07 15:42:26 +08002030 __bfs_init_root(&this, class);
David Miller419ca3f2008-07-29 21:45:03 -07002031
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04002032 raw_local_irq_save(flags);
Peter Zijlstra248efb22020-03-13 11:09:49 +01002033 lockdep_lock();
Ming Leief681022009-07-16 15:44:29 +02002034 ret = __lockdep_count_forward_deps(&this);
Peter Zijlstra248efb22020-03-13 11:09:49 +01002035 lockdep_unlock();
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04002036 raw_local_irq_restore(flags);
David Miller419ca3f2008-07-29 21:45:03 -07002037
2038 return ret;
2039}
2040
Fengguang Wu5216d532013-11-09 00:55:35 +08002041static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07002042{
Ming Leief681022009-07-16 15:44:29 +02002043 unsigned long count = 0;
Kees Cook3f649ab2020-06-03 13:09:38 -07002044 struct lock_list *target_entry;
David Miller419ca3f2008-07-29 21:45:03 -07002045
Ming Leief681022009-07-16 15:44:29 +02002046 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07002047
Ming Leief681022009-07-16 15:44:29 +02002048 return count;
David Miller419ca3f2008-07-29 21:45:03 -07002049}
2050
2051unsigned long lockdep_count_backward_deps(struct lock_class *class)
2052{
2053 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02002054 struct lock_list this;
2055
Boqun Feng6971c0f2020-08-07 15:42:26 +08002056 __bfs_init_root(&this, class);
David Miller419ca3f2008-07-29 21:45:03 -07002057
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04002058 raw_local_irq_save(flags);
Peter Zijlstra248efb22020-03-13 11:09:49 +01002059 lockdep_lock();
Ming Leief681022009-07-16 15:44:29 +02002060 ret = __lockdep_count_backward_deps(&this);
Peter Zijlstra248efb22020-03-13 11:09:49 +01002061 lockdep_unlock();
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04002062 raw_local_irq_restore(flags);
David Miller419ca3f2008-07-29 21:45:03 -07002063
2064 return ret;
2065}
2066
Peter Zijlstra8e182572007-07-19 01:48:54 -07002067/*
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002068 * Check that the dependency graph starting at <src> can lead to
Boqun Fengb11be022020-08-07 15:42:22 +08002069 * <target> or not.
Peter Zijlstra8e182572007-07-19 01:48:54 -07002070 */
Boqun Fengb11be022020-08-07 15:42:22 +08002071static noinline enum bfs_result
Boqun Feng9de0c9b2020-08-07 15:42:28 +08002072check_path(struct held_lock *target, struct lock_list *src_entry,
2073 bool (*match)(struct lock_list *entry, void *data),
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002074 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002075{
Boqun Fengb11be022020-08-07 15:42:22 +08002076 enum bfs_result ret;
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002077
Boqun Feng9de0c9b2020-08-07 15:42:28 +08002078 ret = __bfs_forwards(src_entry, target, match, target_entry);
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002079
Boqun Fengb11be022020-08-07 15:42:22 +08002080 if (unlikely(bfs_error(ret)))
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002081 print_bfs_bug(ret);
2082
2083 return ret;
2084}
2085
2086/*
2087 * Prove that the dependency graph starting at <src> can not
2088 * lead to <target>. If it can, there is a circle when adding
2089 * <target> -> <src> dependency.
2090 *
Boqun Fengb11be022020-08-07 15:42:22 +08002091 * Print an error and return BFS_RMATCH if it does.
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002092 */
Boqun Fengb11be022020-08-07 15:42:22 +08002093static noinline enum bfs_result
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002094check_noncircular(struct held_lock *src, struct held_lock *target,
Bart Van Assche12593b72019-07-22 11:24:42 -07002095 struct lock_trace **const trace)
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002096{
Boqun Fengb11be022020-08-07 15:42:22 +08002097 enum bfs_result ret;
Kees Cook3f649ab2020-06-03 13:09:38 -07002098 struct lock_list *target_entry;
Boqun Feng6971c0f2020-08-07 15:42:26 +08002099 struct lock_list src_entry;
2100
2101 bfs_init_root(&src_entry, src);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002102
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002103 debug_atomic_inc(nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07002104
Boqun Feng9de0c9b2020-08-07 15:42:28 +08002105 ret = check_path(target, &src_entry, hlock_conflict, &target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02002106
Boqun Fengb11be022020-08-07 15:42:22 +08002107 if (unlikely(ret == BFS_RMATCH)) {
Bart Van Assche12593b72019-07-22 11:24:42 -07002108 if (!*trace) {
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002109 /*
2110 * If save_trace fails here, the printing might
2111 * trigger a WARN but because of the !nr_entries it
2112 * should not do bad things.
2113 */
Bart Van Assche12593b72019-07-22 11:24:42 -07002114 *trace = save_trace();
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002115 }
2116
2117 print_circular_bug(&src_entry, target_entry, src, target);
2118 }
2119
2120 return ret;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002121}
2122
Yuyang Du68e9dc22019-05-06 16:19:36 +08002123#ifdef CONFIG_LOCKDEP_SMALL
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002124/*
2125 * Check that the dependency graph starting at <src> can lead to
2126 * <target> or not. If it can, <src> -> <target> dependency is already
2127 * in the graph.
2128 *
Boqun Fengb11be022020-08-07 15:42:22 +08002129 * Return BFS_RMATCH if it does, or BFS_RMATCH if it does not, return BFS_E* if
2130 * any error appears in the bfs search.
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002131 */
Boqun Fengb11be022020-08-07 15:42:22 +08002132static noinline enum bfs_result
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002133check_redundant(struct held_lock *src, struct held_lock *target)
Peter Zijlstraae813302017-03-03 10:13:38 +01002134{
Boqun Fengb11be022020-08-07 15:42:22 +08002135 enum bfs_result ret;
Kees Cook3f649ab2020-06-03 13:09:38 -07002136 struct lock_list *target_entry;
Boqun Feng6971c0f2020-08-07 15:42:26 +08002137 struct lock_list src_entry;
2138
2139 bfs_init_root(&src_entry, src);
Boqun Feng68e30562020-08-07 15:42:29 +08002140 /*
2141 * Special setup for check_redundant().
2142 *
2143 * To report redundant, we need to find a strong dependency path that
2144 * is equal to or stronger than <src> -> <target>. So if <src> is E,
2145 * we need to let __bfs() only search for a path starting at a -(E*)->,
2146 * we achieve this by setting the initial node's ->only_xr to true in
2147 * that case. And if <prev> is S, we set initial ->only_xr to false
2148 * because both -(S*)-> (equal) and -(E*)-> (stronger) are redundant.
2149 */
2150 src_entry.only_xr = src->read == 0;
Peter Zijlstraae813302017-03-03 10:13:38 +01002151
2152 debug_atomic_inc(nr_redundant_checks);
2153
Boqun Feng68e30562020-08-07 15:42:29 +08002154 ret = check_path(target, &src_entry, hlock_equal, &target_entry);
Peter Zijlstraae813302017-03-03 10:13:38 +01002155
Boqun Fengb11be022020-08-07 15:42:22 +08002156 if (ret == BFS_RMATCH)
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002157 debug_atomic_inc(nr_redundant);
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002158
2159 return ret;
Peter Zijlstraae813302017-03-03 10:13:38 +01002160}
Yuyang Du68e9dc22019-05-06 16:19:36 +08002161#endif
Peter Zijlstraae813302017-03-03 10:13:38 +01002162
Yuyang Due7a38f62019-05-06 16:19:20 +08002163#ifdef CONFIG_TRACE_IRQFLAGS
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002164
Boqun Fengf08e3882020-08-07 15:42:30 +08002165/*
2166 * Forwards and backwards subgraph searching, for the purposes of
2167 * proving that two subgraphs can be connected by a new dependency
2168 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
2169 *
2170 * A irq safe->unsafe deadlock happens with the following conditions:
2171 *
2172 * 1) We have a strong dependency path A -> ... -> B
2173 *
2174 * 2) and we have ENABLED_IRQ usage of B and USED_IN_IRQ usage of A, therefore
2175 * irq can create a new dependency B -> A (consider the case that a holder
2176 * of B gets interrupted by an irq whose handler will try to acquire A).
2177 *
2178 * 3) the dependency circle A -> ... -> B -> A we get from 1) and 2) is a
2179 * strong circle:
2180 *
2181 * For the usage bits of B:
2182 * a) if A -> B is -(*N)->, then B -> A could be any type, so any
2183 * ENABLED_IRQ usage suffices.
2184 * b) if A -> B is -(*R)->, then B -> A must be -(E*)->, so only
2185 * ENABLED_IRQ_*_READ usage suffices.
2186 *
2187 * For the usage bits of A:
2188 * c) if A -> B is -(E*)->, then B -> A could be any type, so any
2189 * USED_IN_IRQ usage suffices.
2190 * d) if A -> B is -(S*)->, then B -> A must be -(*N)->, so only
2191 * USED_IN_IRQ_*_READ usage suffices.
2192 */
2193
2194/*
2195 * There is a strong dependency path in the dependency graph: A -> B, and now
2196 * we need to decide which usage bit of A should be accumulated to detect
2197 * safe->unsafe bugs.
2198 *
2199 * Note that usage_accumulate() is used in backwards search, so ->only_xr
2200 * stands for whether A -> B only has -(S*)-> (in this case ->only_xr is true).
2201 *
2202 * As above, if only_xr is false, which means A -> B has -(E*)-> dependency
2203 * path, any usage of A should be considered. Otherwise, we should only
2204 * consider _READ usage.
2205 */
Boqun Feng61775ed2020-08-07 15:42:27 +08002206static inline bool usage_accumulate(struct lock_list *entry, void *mask)
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002207{
Boqun Fengf08e3882020-08-07 15:42:30 +08002208 if (!entry->only_xr)
2209 *(unsigned long *)mask |= entry->class->usage_mask;
2210 else /* Mask out _READ usage bits */
2211 *(unsigned long *)mask |= (entry->class->usage_mask & LOCKF_IRQ);
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002212
Boqun Feng61775ed2020-08-07 15:42:27 +08002213 return false;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002214}
2215
Peter Zijlstra8e182572007-07-19 01:48:54 -07002216/*
Boqun Fengf08e3882020-08-07 15:42:30 +08002217 * There is a strong dependency path in the dependency graph: A -> B, and now
2218 * we need to decide which usage bit of B conflicts with the usage bits of A,
2219 * i.e. which usage bit of B may introduce safe->unsafe deadlocks.
2220 *
2221 * As above, if only_xr is false, which means A -> B has -(*N)-> dependency
2222 * path, any usage of B should be considered. Otherwise, we should only
2223 * consider _READ usage.
Peter Zijlstra8e182572007-07-19 01:48:54 -07002224 */
Boqun Feng61775ed2020-08-07 15:42:27 +08002225static inline bool usage_match(struct lock_list *entry, void *mask)
Ming Leid7aaba12009-07-16 15:44:29 +02002226{
Boqun Fengf08e3882020-08-07 15:42:30 +08002227 if (!entry->only_xr)
2228 return !!(entry->class->usage_mask & *(unsigned long *)mask);
2229 else /* Mask out _READ usage bits */
2230 return !!((entry->class->usage_mask & LOCKF_IRQ) & *(unsigned long *)mask);
Ming Leid7aaba12009-07-16 15:44:29 +02002231}
2232
Peter Zijlstra8e182572007-07-19 01:48:54 -07002233/*
2234 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02002235 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07002236 *
Boqun Fengb11be022020-08-07 15:42:22 +08002237 * Return BFS_MATCH if such a node exists in the subgraph, and put that node
Ming Leid7aaba12009-07-16 15:44:29 +02002238 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07002239 */
Boqun Fengb11be022020-08-07 15:42:22 +08002240static enum bfs_result
Frederic Weisbecker627f3642019-04-02 18:02:43 +02002241find_usage_forwards(struct lock_list *root, unsigned long usage_mask,
Ming Leid7aaba12009-07-16 15:44:29 +02002242 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002243{
Boqun Fengb11be022020-08-07 15:42:22 +08002244 enum bfs_result result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002245
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002246 debug_atomic_inc(nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002247
Frederic Weisbecker627f3642019-04-02 18:02:43 +02002248 result = __bfs_forwards(root, &usage_mask, usage_match, target_entry);
Ming Leid7aaba12009-07-16 15:44:29 +02002249
2250 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002251}
2252
2253/*
2254 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02002255 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07002256 */
Boqun Fengb11be022020-08-07 15:42:22 +08002257static enum bfs_result
Frederic Weisbecker627f3642019-04-02 18:02:43 +02002258find_usage_backwards(struct lock_list *root, unsigned long usage_mask,
Ming Leid7aaba12009-07-16 15:44:29 +02002259 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002260{
Boqun Fengb11be022020-08-07 15:42:22 +08002261 enum bfs_result result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002262
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002263 debug_atomic_inc(nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002264
Frederic Weisbecker627f3642019-04-02 18:02:43 +02002265 result = __bfs_backwards(root, &usage_mask, usage_match, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02002266
Ming Leid7aaba12009-07-16 15:44:29 +02002267 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002268}
2269
Peter Zijlstraaf012962009-07-16 15:44:29 +02002270static void print_lock_class_header(struct lock_class *class, int depth)
2271{
2272 int bit;
2273
2274 printk("%*s->", depth, "");
2275 print_lock_name(class);
Waiman Long8ca2b56c2018-10-03 13:07:18 -04002276#ifdef CONFIG_DEBUG_LOCKDEP
2277 printk(KERN_CONT " ops: %lu", debug_class_ops_read(class));
2278#endif
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002279 printk(KERN_CONT " {\n");
Peter Zijlstraaf012962009-07-16 15:44:29 +02002280
Peter Zijlstra2bb89452020-09-30 11:49:37 +02002281 for (bit = 0; bit < LOCK_TRACE_STATES; bit++) {
Peter Zijlstraaf012962009-07-16 15:44:29 +02002282 if (class->usage_mask & (1 << bit)) {
2283 int len = depth;
2284
2285 len += printk("%*s %s", depth, "", usage_str[bit]);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002286 len += printk(KERN_CONT " at:\n");
Bart Van Assche12593b72019-07-22 11:24:42 -07002287 print_lock_trace(class->usage_traces[bit], len);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002288 }
2289 }
2290 printk("%*s }\n", depth, "");
2291
Borislav Petkov04860d42018-02-26 14:49:26 +01002292 printk("%*s ... key at: [<%px>] %pS\n",
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002293 depth, "", class->key, class->key);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002294}
2295
2296/*
2297 * printk the shortest lock dependencies from @start to @end in reverse order:
2298 */
2299static void __used
2300print_shortest_lock_dependencies(struct lock_list *leaf,
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002301 struct lock_list *root)
Peter Zijlstraaf012962009-07-16 15:44:29 +02002302{
2303 struct lock_list *entry = leaf;
2304 int depth;
2305
2306 /*compute depth from generated tree by BFS*/
2307 depth = get_lock_depth(leaf);
2308
2309 do {
2310 print_lock_class_header(entry->class, depth);
2311 printk("%*s ... acquired at:\n", depth, "");
Bart Van Assche12593b72019-07-22 11:24:42 -07002312 print_lock_trace(entry->trace, 2);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002313 printk("\n");
2314
2315 if (depth == 0 && (entry != root)) {
Steven Rostedt6be8c392011-04-20 21:41:58 -04002316 printk("lockdep:%s bad path found in chain graph\n", __func__);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002317 break;
2318 }
2319
2320 entry = get_lock_parent(entry);
2321 depth--;
2322 } while (entry && (depth >= 0));
Peter Zijlstraaf012962009-07-16 15:44:29 +02002323}
Ming Leid7aaba12009-07-16 15:44:29 +02002324
Steven Rostedt3003eba2011-04-20 21:41:54 -04002325static void
2326print_irq_lock_scenario(struct lock_list *safe_entry,
2327 struct lock_list *unsafe_entry,
Steven Rostedtdad3d742011-04-20 21:41:57 -04002328 struct lock_class *prev_class,
2329 struct lock_class *next_class)
Steven Rostedt3003eba2011-04-20 21:41:54 -04002330{
2331 struct lock_class *safe_class = safe_entry->class;
2332 struct lock_class *unsafe_class = unsafe_entry->class;
Steven Rostedtdad3d742011-04-20 21:41:57 -04002333 struct lock_class *middle_class = prev_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04002334
2335 if (middle_class == safe_class)
Steven Rostedtdad3d742011-04-20 21:41:57 -04002336 middle_class = next_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04002337
2338 /*
2339 * A direct locking problem where unsafe_class lock is taken
2340 * directly by safe_class lock, then all we need to show
2341 * is the deadlock scenario, as it is obvious that the
2342 * unsafe lock is taken under the safe lock.
2343 *
2344 * But if there is a chain instead, where the safe lock takes
2345 * an intermediate lock (middle_class) where this lock is
2346 * not the same as the safe lock, then the lock chain is
2347 * used to describe the problem. Otherwise we would need
2348 * to show a different CPU case for each link in the chain
2349 * from the safe_class lock to the unsafe_class lock.
2350 */
2351 if (middle_class != unsafe_class) {
2352 printk("Chain exists of:\n ");
2353 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002354 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002355 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002356 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002357 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002358 printk(KERN_CONT "\n\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002359 }
2360
2361 printk(" Possible interrupt unsafe locking scenario:\n\n");
2362 printk(" CPU0 CPU1\n");
2363 printk(" ---- ----\n");
2364 printk(" lock(");
2365 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002366 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002367 printk(" local_irq_disable();\n");
2368 printk(" lock(");
2369 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002370 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002371 printk(" lock(");
2372 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002373 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002374 printk(" <Interrupt>\n");
2375 printk(" lock(");
2376 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002377 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002378 printk("\n *** DEADLOCK ***\n\n");
2379}
2380
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002381static void
Peter Zijlstra8e182572007-07-19 01:48:54 -07002382print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02002383 struct lock_list *prev_root,
2384 struct lock_list *next_root,
2385 struct lock_list *backwards_entry,
2386 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07002387 struct held_lock *prev,
2388 struct held_lock *next,
2389 enum lock_usage_bit bit1,
2390 enum lock_usage_bit bit2,
2391 const char *irqclass)
2392{
2393 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002394 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002395
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002396 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002397 pr_warn("=====================================================\n");
2398 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07002399 irqclass, irqclass);
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002400 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002401 pr_warn("-----------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002402 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 -07002403 curr->comm, task_pid_nr(curr),
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02002404 lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT,
Peter Zijlstra8e182572007-07-19 01:48:54 -07002405 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02002406 lockdep_hardirqs_enabled(),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002407 curr->softirqs_enabled);
2408 print_lock(next);
2409
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002410 pr_warn("\nand this task is already holding:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002411 print_lock(prev);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002412 pr_warn("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02002413 print_lock_name(hlock_class(prev));
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002414 pr_cont(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02002415 print_lock_name(hlock_class(next));
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002416 pr_cont("\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002417
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002418 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07002419 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002420 print_lock_name(backwards_entry->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002421 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002422
Bart Van Assche12593b72019-07-22 11:24:42 -07002423 print_lock_trace(backwards_entry->class->usage_traces[bit1], 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002424
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002425 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002426 print_lock_name(forwards_entry->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002427 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
2428 pr_warn("...");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002429
Bart Van Assche12593b72019-07-22 11:24:42 -07002430 print_lock_trace(forwards_entry->class->usage_traces[bit2], 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002431
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002432 pr_warn("\nother info that might help us debug this:\n\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04002433 print_irq_lock_scenario(backwards_entry, forwards_entry,
2434 hlock_class(prev), hlock_class(next));
Steven Rostedt3003eba2011-04-20 21:41:54 -04002435
Peter Zijlstra8e182572007-07-19 01:48:54 -07002436 lockdep_print_held_locks(curr);
2437
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002438 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
Bart Van Assche12593b72019-07-22 11:24:42 -07002439 prev_root->trace = save_trace();
2440 if (!prev_root->trace)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002441 return;
Ming Lei24208ca2009-07-16 15:44:29 +02002442 print_shortest_lock_dependencies(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002443
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002444 pr_warn("\nthe dependencies between the lock to be acquired");
2445 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
Bart Van Assche12593b72019-07-22 11:24:42 -07002446 next_root->trace = save_trace();
2447 if (!next_root->trace)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002448 return;
Ming Lei24208ca2009-07-16 15:44:29 +02002449 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002450
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002451 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002452 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -07002453}
2454
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002455static const char *state_names[] = {
2456#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01002457 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002458#include "lockdep_states.h"
2459#undef LOCKDEP_STATE
2460};
2461
2462static const char *state_rnames[] = {
2463#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01002464 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002465#include "lockdep_states.h"
2466#undef LOCKDEP_STATE
2467};
2468
2469static inline const char *state_name(enum lock_usage_bit bit)
2470{
Frederic Weisbeckerc902a1e2019-04-02 18:02:42 +02002471 if (bit & LOCK_USAGE_READ_MASK)
2472 return state_rnames[bit >> LOCK_USAGE_DIR_MASK];
2473 else
2474 return state_names[bit >> LOCK_USAGE_DIR_MASK];
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002475}
2476
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002477/*
2478 * The bit number is encoded like:
2479 *
2480 * bit0: 0 exclusive, 1 read lock
2481 * bit1: 0 used in irq, 1 irq enabled
2482 * bit2-n: state
2483 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002484static int exclusive_bit(int new_bit)
2485{
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01002486 int state = new_bit & LOCK_USAGE_STATE_MASK;
2487 int dir = new_bit & LOCK_USAGE_DIR_MASK;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002488
2489 /*
2490 * keep state, bit flip the direction and strip read.
2491 */
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01002492 return state | (dir ^ LOCK_USAGE_DIR_MASK);
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002493}
2494
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002495/*
2496 * Observe that when given a bitmask where each bitnr is encoded as above, a
2497 * right shift of the mask transforms the individual bitnrs as -1 and
2498 * conversely, a left shift transforms into +1 for the individual bitnrs.
2499 *
2500 * So for all bits whose number have LOCK_ENABLED_* set (bitnr1 == 1), we can
2501 * create the mask with those bit numbers using LOCK_USED_IN_* (bitnr1 == 0)
2502 * instead by subtracting the bit number by 2, or shifting the mask right by 2.
2503 *
2504 * Similarly, bitnr1 == 0 becomes bitnr1 == 1 by adding 2, or shifting left 2.
2505 *
2506 * So split the mask (note that LOCKF_ENABLED_IRQ_ALL|LOCKF_USED_IN_IRQ_ALL is
2507 * all bits set) and recompose with bitnr1 flipped.
2508 */
2509static unsigned long invert_dir_mask(unsigned long mask)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002510{
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002511 unsigned long excl = 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002512
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002513 /* Invert dir */
2514 excl |= (mask & LOCKF_ENABLED_IRQ_ALL) >> LOCK_USAGE_DIR_MASK;
2515 excl |= (mask & LOCKF_USED_IN_IRQ_ALL) << LOCK_USAGE_DIR_MASK;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002516
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002517 return excl;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002518}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002519
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002520/*
Boqun Fengf08e3882020-08-07 15:42:30 +08002521 * Note that a LOCK_ENABLED_IRQ_*_READ usage and a LOCK_USED_IN_IRQ_*_READ
2522 * usage may cause deadlock too, for example:
2523 *
2524 * P1 P2
2525 * <irq disabled>
2526 * write_lock(l1); <irq enabled>
2527 * read_lock(l2);
2528 * write_lock(l2);
2529 * <in irq>
2530 * read_lock(l1);
2531 *
2532 * , in above case, l1 will be marked as LOCK_USED_IN_IRQ_HARDIRQ_READ and l2
2533 * will marked as LOCK_ENABLE_IRQ_HARDIRQ_READ, and this is a possible
2534 * deadlock.
2535 *
2536 * In fact, all of the following cases may cause deadlocks:
2537 *
2538 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*
2539 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*
2540 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*_READ
2541 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*_READ
2542 *
2543 * As a result, to calculate the "exclusive mask", first we invert the
2544 * direction (USED_IN/ENABLED) of the original mask, and 1) for all bits with
2545 * bitnr0 set (LOCK_*_READ), add those with bitnr0 cleared (LOCK_*). 2) for all
2546 * bits with bitnr0 cleared (LOCK_*_READ), add those with bitnr0 set (LOCK_*).
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002547 */
2548static unsigned long exclusive_mask(unsigned long mask)
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002549{
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002550 unsigned long excl = invert_dir_mask(mask);
Nick Piggincf40bd12009-01-21 08:12:39 +01002551
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002552 excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK;
Boqun Fengf08e3882020-08-07 15:42:30 +08002553 excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002554
2555 return excl;
2556}
2557
2558/*
2559 * Retrieve the _possible_ original mask to which @mask is
2560 * exclusive. Ie: this is the opposite of exclusive_mask().
2561 * Note that 2 possible original bits can match an exclusive
2562 * bit: one has LOCK_USAGE_READ_MASK set, the other has it
2563 * cleared. So both are returned for each exclusive bit.
2564 */
2565static unsigned long original_mask(unsigned long mask)
2566{
2567 unsigned long excl = invert_dir_mask(mask);
2568
2569 /* Include read in existing usages */
Boqun Fengf08e3882020-08-07 15:42:30 +08002570 excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002571 excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK;
2572
2573 return excl;
2574}
2575
2576/*
2577 * Find the first pair of bit match between an original
2578 * usage mask and an exclusive usage mask.
2579 */
2580static int find_exclusive_match(unsigned long mask,
2581 unsigned long excl_mask,
2582 enum lock_usage_bit *bitp,
2583 enum lock_usage_bit *excl_bitp)
2584{
Boqun Fengf08e3882020-08-07 15:42:30 +08002585 int bit, excl, excl_read;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002586
2587 for_each_set_bit(bit, &mask, LOCK_USED) {
Boqun Fengf08e3882020-08-07 15:42:30 +08002588 /*
2589 * exclusive_bit() strips the read bit, however,
2590 * LOCK_ENABLED_IRQ_*_READ may cause deadlocks too, so we need
2591 * to search excl | LOCK_USAGE_READ_MASK as well.
2592 */
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002593 excl = exclusive_bit(bit);
Boqun Fengf08e3882020-08-07 15:42:30 +08002594 excl_read = excl | LOCK_USAGE_READ_MASK;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002595 if (excl_mask & lock_flag(excl)) {
2596 *bitp = bit;
2597 *excl_bitp = excl;
2598 return 0;
Boqun Fengf08e3882020-08-07 15:42:30 +08002599 } else if (excl_mask & lock_flag(excl_read)) {
2600 *bitp = bit;
2601 *excl_bitp = excl_read;
2602 return 0;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002603 }
2604 }
2605 return -1;
2606}
2607
2608/*
2609 * Prove that the new dependency does not connect a hardirq-safe(-read)
2610 * lock with a hardirq-unsafe lock - to achieve this we search
2611 * the backwards-subgraph starting at <prev>, and the
2612 * forwards-subgraph starting at <next>:
2613 */
2614static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
2615 struct held_lock *next)
2616{
2617 unsigned long usage_mask = 0, forward_mask, backward_mask;
2618 enum lock_usage_bit forward_bit = 0, backward_bit = 0;
Kees Cook3f649ab2020-06-03 13:09:38 -07002619 struct lock_list *target_entry1;
2620 struct lock_list *target_entry;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002621 struct lock_list this, that;
Boqun Fengb11be022020-08-07 15:42:22 +08002622 enum bfs_result ret;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002623
2624 /*
2625 * Step 1: gather all hard/soft IRQs usages backward in an
2626 * accumulated usage mask.
2627 */
Boqun Fengf08e3882020-08-07 15:42:30 +08002628 bfs_init_rootb(&this, prev);
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002629
2630 ret = __bfs_backwards(&this, &usage_mask, usage_accumulate, NULL);
Boqun Fengb11be022020-08-07 15:42:22 +08002631 if (bfs_error(ret)) {
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002632 print_bfs_bug(ret);
2633 return 0;
2634 }
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002635
2636 usage_mask &= LOCKF_USED_IN_IRQ_ALL;
2637 if (!usage_mask)
2638 return 1;
2639
2640 /*
2641 * Step 2: find exclusive uses forward that match the previous
2642 * backward accumulated mask.
2643 */
2644 forward_mask = exclusive_mask(usage_mask);
2645
Boqun Fengf08e3882020-08-07 15:42:30 +08002646 bfs_init_root(&that, next);
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002647
2648 ret = find_usage_forwards(&that, forward_mask, &target_entry1);
Boqun Fengb11be022020-08-07 15:42:22 +08002649 if (bfs_error(ret)) {
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002650 print_bfs_bug(ret);
2651 return 0;
2652 }
Boqun Fengb11be022020-08-07 15:42:22 +08002653 if (ret == BFS_RNOMATCH)
2654 return 1;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002655
2656 /*
2657 * Step 3: we found a bad match! Now retrieve a lock from the backward
2658 * list whose usage mask matches the exclusive usage mask from the
2659 * lock found on the forward list.
2660 */
2661 backward_mask = original_mask(target_entry1->class->usage_mask);
2662
2663 ret = find_usage_backwards(&this, backward_mask, &target_entry);
Boqun Fengb11be022020-08-07 15:42:22 +08002664 if (bfs_error(ret)) {
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002665 print_bfs_bug(ret);
2666 return 0;
2667 }
Boqun Fengb11be022020-08-07 15:42:22 +08002668 if (DEBUG_LOCKS_WARN_ON(ret == BFS_RNOMATCH))
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002669 return 1;
2670
2671 /*
2672 * Step 4: narrow down to a pair of incompatible usage bits
2673 * and report it.
2674 */
2675 ret = find_exclusive_match(target_entry->class->usage_mask,
2676 target_entry1->class->usage_mask,
2677 &backward_bit, &forward_bit);
2678 if (DEBUG_LOCKS_WARN_ON(ret == -1))
2679 return 1;
2680
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002681 print_bad_irq_dependency(curr, &this, &that,
2682 target_entry, target_entry1,
2683 prev, next,
2684 backward_bit, forward_bit,
2685 state_name(backward_bit));
2686
2687 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002688}
2689
Peter Zijlstra8e182572007-07-19 01:48:54 -07002690#else
2691
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002692static inline int check_irq_usage(struct task_struct *curr,
2693 struct held_lock *prev, struct held_lock *next)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002694{
2695 return 1;
2696}
Waiman Longb3b9c182020-02-06 10:24:03 -05002697#endif /* CONFIG_TRACE_IRQFLAGS */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002698
Waiman Longb3b9c182020-02-06 10:24:03 -05002699static void inc_chains(int irq_context)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002700{
Waiman Longb3b9c182020-02-06 10:24:03 -05002701 if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT)
2702 nr_hardirq_chains++;
2703 else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT)
2704 nr_softirq_chains++;
2705 else
2706 nr_process_chains++;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002707}
2708
Waiman Longb3b9c182020-02-06 10:24:03 -05002709static void dec_chains(int irq_context)
2710{
2711 if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT)
2712 nr_hardirq_chains--;
2713 else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT)
2714 nr_softirq_chains--;
2715 else
2716 nr_process_chains--;
2717}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002718
Steven Rostedt48702ec2011-04-20 21:41:56 -04002719static void
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002720print_deadlock_scenario(struct held_lock *nxt, struct held_lock *prv)
Steven Rostedt48702ec2011-04-20 21:41:56 -04002721{
2722 struct lock_class *next = hlock_class(nxt);
2723 struct lock_class *prev = hlock_class(prv);
2724
2725 printk(" Possible unsafe locking scenario:\n\n");
2726 printk(" CPU0\n");
2727 printk(" ----\n");
2728 printk(" lock(");
2729 __print_lock_name(prev);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002730 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04002731 printk(" lock(");
2732 __print_lock_name(next);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002733 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04002734 printk("\n *** DEADLOCK ***\n\n");
2735 printk(" May be due to missing lock nesting notation\n\n");
2736}
2737
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002738static void
Peter Zijlstra8e182572007-07-19 01:48:54 -07002739print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
2740 struct held_lock *next)
2741{
2742 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002743 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002744
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002745 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002746 pr_warn("============================================\n");
2747 pr_warn("WARNING: possible recursive locking detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002748 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002749 pr_warn("--------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002750 pr_warn("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002751 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07002752 print_lock(next);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002753 pr_warn("\nbut task is already holding lock:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002754 print_lock(prev);
2755
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002756 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04002757 print_deadlock_scenario(next, prev);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002758 lockdep_print_held_locks(curr);
2759
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002760 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002761 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -07002762}
2763
2764/*
2765 * Check whether we are holding such a class already.
2766 *
2767 * (Note that this has to be done separately, because the graph cannot
2768 * detect such classes of deadlocks.)
2769 *
Boqun Fengd61fc96a2020-11-02 13:37:41 +08002770 * Returns: 0 on deadlock detected, 1 on OK, 2 if another lock with the same
2771 * lock class is held but nest_lock is also held, i.e. we rely on the
2772 * nest_lock to avoid the deadlock.
Peter Zijlstra8e182572007-07-19 01:48:54 -07002773 */
2774static int
Yuyang Du4609c4f2019-05-06 16:19:33 +08002775check_deadlock(struct task_struct *curr, struct held_lock *next)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002776{
2777 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002778 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002779 int i;
2780
2781 for (i = 0; i < curr->lockdep_depth; i++) {
2782 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002783
2784 if (prev->instance == next->nest_lock)
2785 nest = prev;
2786
Dave Jonesf82b2172008-08-11 09:30:23 +02002787 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002788 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002789
Peter Zijlstra8e182572007-07-19 01:48:54 -07002790 /*
2791 * Allow read-after-read recursion of the same
2792 * lock class (i.e. read_lock(lock)+read_lock(lock)):
2793 */
Yuyang Du4609c4f2019-05-06 16:19:33 +08002794 if ((next->read == 2) && prev->read)
Boqun Fengd61fc96a2020-11-02 13:37:41 +08002795 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002796
2797 /*
2798 * We're holding the nest_lock, which serializes this lock's
2799 * nesting behaviour.
2800 */
2801 if (nest)
2802 return 2;
2803
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002804 print_deadlock_bug(curr, prev, next);
2805 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002806 }
2807 return 1;
2808}
2809
2810/*
2811 * There was a chain-cache miss, and we are about to add a new dependency
Yuyang Du154f1852019-05-06 16:19:31 +08002812 * to a previous lock. We validate the following rules:
Peter Zijlstra8e182572007-07-19 01:48:54 -07002813 *
2814 * - would the adding of the <prev> -> <next> dependency create a
2815 * circular dependency in the graph? [== circular deadlock]
2816 *
2817 * - does the new prev->next dependency connect any hardirq-safe lock
2818 * (in the full backwards-subgraph starting at <prev>) with any
2819 * hardirq-unsafe lock (in the full forwards-subgraph starting at
2820 * <next>)? [== illegal lock inversion with hardirq contexts]
2821 *
2822 * - does the new prev->next dependency connect any softirq-safe lock
2823 * (in the full backwards-subgraph starting at <prev>) with any
2824 * softirq-unsafe lock (in the full forwards-subgraph starting at
2825 * <next>)? [== illegal lock inversion with softirq contexts]
2826 *
2827 * any of these scenarios could lead to a deadlock.
2828 *
2829 * Then if all the validations pass, we add the forwards and backwards
2830 * dependency.
2831 */
2832static int
2833check_prev_add(struct task_struct *curr, struct held_lock *prev,
Boqun Fengbd76eca2020-08-07 15:42:24 +08002834 struct held_lock *next, u16 distance,
Bart Van Assche12593b72019-07-22 11:24:42 -07002835 struct lock_trace **const trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002836{
Peter Zijlstra8b405d52017-10-04 11:13:37 +02002837 struct lock_list *entry;
Boqun Fengb11be022020-08-07 15:42:22 +08002838 enum bfs_result ret;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002839
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08002840 if (!hlock_class(prev)->key || !hlock_class(next)->key) {
2841 /*
2842 * The warning statements below may trigger a use-after-free
2843 * of the class name. It is better to trigger a use-after free
2844 * and to have the class name most of the time instead of not
2845 * having the class name available.
2846 */
2847 WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key,
2848 "Detected use-after-free of lock class %px/%s\n",
2849 hlock_class(prev),
2850 hlock_class(prev)->name);
2851 WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key,
2852 "Detected use-after-free of lock class %px/%s\n",
2853 hlock_class(next),
2854 hlock_class(next)->name);
2855 return 2;
2856 }
2857
Peter Zijlstra8e182572007-07-19 01:48:54 -07002858 /*
2859 * Prove that the new <prev> -> <next> dependency would not
2860 * create a circular dependency in the graph. (We do this by
Yuyang Du154f1852019-05-06 16:19:31 +08002861 * a breadth-first search into the graph starting at <next>,
2862 * and check whether we can reach <prev>.)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002863 *
Yuyang Du154f1852019-05-06 16:19:31 +08002864 * The search is limited by the size of the circular queue (i.e.,
2865 * MAX_CIRCULAR_QUEUE_SIZE) which keeps track of a breadth of nodes
2866 * in the graph whose neighbours are to be checked.
Peter Zijlstra8e182572007-07-19 01:48:54 -07002867 */
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002868 ret = check_noncircular(next, prev, trace);
Boqun Fengb11be022020-08-07 15:42:22 +08002869 if (unlikely(bfs_error(ret) || ret == BFS_RMATCH))
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002870 return 0;
Ming Leic94aa5c2009-07-16 15:44:29 +02002871
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002872 if (!check_irq_usage(curr, prev, next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002873 return 0;
2874
2875 /*
Peter Zijlstra8e182572007-07-19 01:48:54 -07002876 * Is the <prev> -> <next> dependency already present?
2877 *
2878 * (this may occur even though this is a new chain: consider
2879 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
2880 * chains - the second one will be new, but L1 already has
2881 * L2 added to its dependency list, due to the first chain.)
2882 */
Dave Jonesf82b2172008-08-11 09:30:23 +02002883 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
2884 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07002885 if (distance == 1)
2886 entry->distance = 1;
Boqun Feng3454a362020-08-07 15:42:25 +08002887 entry->dep |= calc_dep(prev, next);
2888
2889 /*
2890 * Also, update the reverse dependency in @next's
2891 * ->locks_before list.
2892 *
2893 * Here we reuse @entry as the cursor, which is fine
2894 * because we won't go to the next iteration of the
2895 * outer loop:
2896 *
2897 * For normal cases, we return in the inner loop.
2898 *
2899 * If we fail to return, we have inconsistency, i.e.
2900 * <prev>::locks_after contains <next> while
2901 * <next>::locks_before doesn't contain <prev>. In
2902 * that case, we return after the inner and indicate
2903 * something is wrong.
2904 */
2905 list_for_each_entry(entry, &hlock_class(next)->locks_before, entry) {
2906 if (entry->class == hlock_class(prev)) {
2907 if (distance == 1)
2908 entry->distance = 1;
2909 entry->dep |= calc_depb(prev, next);
2910 return 1;
2911 }
2912 }
2913
2914 /* <prev> is not found in <next>::locks_before */
2915 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002916 }
2917 }
2918
Yuyang Du68e9dc22019-05-06 16:19:36 +08002919#ifdef CONFIG_LOCKDEP_SMALL
Peter Zijlstraae813302017-03-03 10:13:38 +01002920 /*
2921 * Is the <prev> -> <next> link redundant?
2922 */
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002923 ret = check_redundant(prev, next);
Boqun Fengb11be022020-08-07 15:42:22 +08002924 if (bfs_error(ret))
2925 return 0;
2926 else if (ret == BFS_RMATCH)
2927 return 2;
Yuyang Du68e9dc22019-05-06 16:19:36 +08002928#endif
Peter Zijlstraae813302017-03-03 10:13:38 +01002929
Bart Van Assche12593b72019-07-22 11:24:42 -07002930 if (!*trace) {
2931 *trace = save_trace();
2932 if (!*trace)
2933 return 0;
2934 }
Yong Zhang4726f2a2010-05-04 14:16:48 +08002935
Peter Zijlstra8e182572007-07-19 01:48:54 -07002936 /*
2937 * Ok, all validations passed, add the new lock
2938 * to the previous lock's dependency list:
2939 */
Bart Van Assche86cffb82019-02-14 15:00:41 -08002940 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
Dave Jonesf82b2172008-08-11 09:30:23 +02002941 &hlock_class(prev)->locks_after,
Boqun Feng3454a362020-08-07 15:42:25 +08002942 next->acquire_ip, distance,
2943 calc_dep(prev, next),
2944 *trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002945
2946 if (!ret)
2947 return 0;
2948
Bart Van Assche86cffb82019-02-14 15:00:41 -08002949 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
Dave Jonesf82b2172008-08-11 09:30:23 +02002950 &hlock_class(next)->locks_before,
Boqun Feng3454a362020-08-07 15:42:25 +08002951 next->acquire_ip, distance,
2952 calc_depb(prev, next),
2953 *trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002954 if (!ret)
2955 return 0;
2956
Byungchul Park70911fd2017-08-07 16:12:50 +09002957 return 2;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002958}
2959
2960/*
2961 * Add the dependency to all directly-previous locks that are 'relevant'.
2962 * The ones that are relevant are (in increasing distance from curr):
2963 * all consecutive trylock entries and the final non-trylock entry - or
2964 * the end of this context's lock-chain - whichever comes first.
2965 */
2966static int
2967check_prevs_add(struct task_struct *curr, struct held_lock *next)
2968{
Bart Van Assche12593b72019-07-22 11:24:42 -07002969 struct lock_trace *trace = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002970 int depth = curr->lockdep_depth;
2971 struct held_lock *hlock;
2972
2973 /*
2974 * Debugging checks.
2975 *
2976 * Depth must not be zero for a non-head lock:
2977 */
2978 if (!depth)
2979 goto out_bug;
2980 /*
2981 * At least two relevant locks must exist for this
2982 * to be a head:
2983 */
2984 if (curr->held_locks[depth].irq_context !=
2985 curr->held_locks[depth-1].irq_context)
2986 goto out_bug;
2987
2988 for (;;) {
Boqun Fengbd76eca2020-08-07 15:42:24 +08002989 u16 distance = curr->lockdep_depth - depth + 1;
Oleg Nesterov1b5ff812014-01-20 19:20:10 +01002990 hlock = curr->held_locks + depth - 1;
Byungchul Parkce07a9412017-08-07 16:12:51 +09002991
Boqun Feng621c9da2020-08-07 15:42:31 +08002992 if (hlock->check) {
2993 int ret = check_prev_add(curr, hlock, next, distance, &trace);
Ingo Molnare966eae2017-12-12 12:31:16 +01002994 if (!ret)
2995 return 0;
2996
2997 /*
2998 * Stop after the first non-trylock entry,
2999 * as non-trylock entries have added their
3000 * own direct dependencies already, so this
3001 * lock is connected to them indirectly:
3002 */
3003 if (!hlock->trylock)
3004 break;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003005 }
Ingo Molnare966eae2017-12-12 12:31:16 +01003006
Peter Zijlstra8e182572007-07-19 01:48:54 -07003007 depth--;
3008 /*
3009 * End of lock-stack?
3010 */
3011 if (!depth)
3012 break;
3013 /*
3014 * Stop the search if we cross into another context:
3015 */
3016 if (curr->held_locks[depth].irq_context !=
3017 curr->held_locks[depth-1].irq_context)
3018 break;
3019 }
3020 return 1;
3021out_bug:
3022 if (!debug_locks_off_graph_unlock())
3023 return 0;
3024
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003025 /*
3026 * Clearly we all shouldn't be here, but since we made it we
3027 * can reliable say we messed up our state. See the above two
3028 * gotos for reasons why we could possibly end up here.
3029 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003030 WARN_ON(1);
3031
3032 return 0;
3033}
3034
Huang, Ying443cd502008-06-20 16:39:21 +08003035struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Bart Van Asschede4643a2019-02-14 15:00:50 -08003036static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS);
Huang, Ying443cd502008-06-20 16:39:21 +08003037static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
Waiman Long797b82eb2020-02-06 10:24:07 -05003038unsigned long nr_zapped_lock_chains;
Waiman Long810507f2020-02-06 10:24:08 -05003039unsigned int nr_free_chain_hlocks; /* Free chain_hlocks in buckets */
3040unsigned int nr_lost_chain_hlocks; /* Lost chain_hlocks */
3041unsigned int nr_large_chain_blocks; /* size > MAX_CHAIN_BUCKETS */
3042
3043/*
3044 * The first 2 chain_hlocks entries in the chain block in the bucket
3045 * list contains the following meta data:
3046 *
3047 * entry[0]:
3048 * Bit 15 - always set to 1 (it is not a class index)
3049 * Bits 0-14 - upper 15 bits of the next block index
3050 * entry[1] - lower 16 bits of next block index
3051 *
3052 * A next block index of all 1 bits means it is the end of the list.
3053 *
3054 * On the unsized bucket (bucket-0), the 3rd and 4th entries contain
3055 * the chain block size:
3056 *
3057 * entry[2] - upper 16 bits of the chain block size
3058 * entry[3] - lower 16 bits of the chain block size
3059 */
3060#define MAX_CHAIN_BUCKETS 16
3061#define CHAIN_BLK_FLAG (1U << 15)
3062#define CHAIN_BLK_LIST_END 0xFFFFU
3063
3064static int chain_block_buckets[MAX_CHAIN_BUCKETS];
3065
3066static inline int size_to_bucket(int size)
3067{
3068 if (size > MAX_CHAIN_BUCKETS)
3069 return 0;
3070
3071 return size - 1;
3072}
3073
3074/*
3075 * Iterate all the chain blocks in a bucket.
3076 */
3077#define for_each_chain_block(bucket, prev, curr) \
3078 for ((prev) = -1, (curr) = chain_block_buckets[bucket]; \
3079 (curr) >= 0; \
3080 (prev) = (curr), (curr) = chain_block_next(curr))
3081
3082/*
3083 * next block or -1
3084 */
3085static inline int chain_block_next(int offset)
3086{
3087 int next = chain_hlocks[offset];
3088
3089 WARN_ON_ONCE(!(next & CHAIN_BLK_FLAG));
3090
3091 if (next == CHAIN_BLK_LIST_END)
3092 return -1;
3093
3094 next &= ~CHAIN_BLK_FLAG;
3095 next <<= 16;
3096 next |= chain_hlocks[offset + 1];
3097
3098 return next;
3099}
3100
3101/*
3102 * bucket-0 only
3103 */
3104static inline int chain_block_size(int offset)
3105{
3106 return (chain_hlocks[offset + 2] << 16) | chain_hlocks[offset + 3];
3107}
3108
3109static inline void init_chain_block(int offset, int next, int bucket, int size)
3110{
3111 chain_hlocks[offset] = (next >> 16) | CHAIN_BLK_FLAG;
3112 chain_hlocks[offset + 1] = (u16)next;
3113
3114 if (size && !bucket) {
3115 chain_hlocks[offset + 2] = size >> 16;
3116 chain_hlocks[offset + 3] = (u16)size;
3117 }
3118}
3119
3120static inline void add_chain_block(int offset, int size)
3121{
3122 int bucket = size_to_bucket(size);
3123 int next = chain_block_buckets[bucket];
3124 int prev, curr;
3125
3126 if (unlikely(size < 2)) {
3127 /*
3128 * We can't store single entries on the freelist. Leak them.
3129 *
3130 * One possible way out would be to uniquely mark them, other
3131 * than with CHAIN_BLK_FLAG, such that we can recover them when
3132 * the block before it is re-added.
3133 */
3134 if (size)
3135 nr_lost_chain_hlocks++;
3136 return;
3137 }
3138
3139 nr_free_chain_hlocks += size;
3140 if (!bucket) {
3141 nr_large_chain_blocks++;
3142
3143 /*
3144 * Variable sized, sort large to small.
3145 */
3146 for_each_chain_block(0, prev, curr) {
3147 if (size >= chain_block_size(curr))
3148 break;
3149 }
3150 init_chain_block(offset, curr, 0, size);
3151 if (prev < 0)
3152 chain_block_buckets[0] = offset;
3153 else
3154 init_chain_block(prev, offset, 0, 0);
3155 return;
3156 }
3157 /*
3158 * Fixed size, add to head.
3159 */
3160 init_chain_block(offset, next, bucket, size);
3161 chain_block_buckets[bucket] = offset;
3162}
3163
3164/*
3165 * Only the first block in the list can be deleted.
3166 *
3167 * For the variable size bucket[0], the first block (the largest one) is
3168 * returned, broken up and put back into the pool. So if a chain block of
3169 * length > MAX_CHAIN_BUCKETS is ever used and zapped, it will just be
3170 * queued up after the primordial chain block and never be used until the
3171 * hlock entries in the primordial chain block is almost used up. That
3172 * causes fragmentation and reduce allocation efficiency. That can be
3173 * monitored by looking at the "large chain blocks" number in lockdep_stats.
3174 */
3175static inline void del_chain_block(int bucket, int size, int next)
3176{
3177 nr_free_chain_hlocks -= size;
3178 chain_block_buckets[bucket] = next;
3179
3180 if (!bucket)
3181 nr_large_chain_blocks--;
3182}
3183
3184static void init_chain_block_buckets(void)
3185{
3186 int i;
3187
3188 for (i = 0; i < MAX_CHAIN_BUCKETS; i++)
3189 chain_block_buckets[i] = -1;
3190
3191 add_chain_block(0, ARRAY_SIZE(chain_hlocks));
3192}
3193
3194/*
3195 * Return offset of a chain block of the right size or -1 if not found.
3196 *
3197 * Fairly simple worst-fit allocator with the addition of a number of size
3198 * specific free lists.
3199 */
3200static int alloc_chain_hlocks(int req)
3201{
3202 int bucket, curr, size;
3203
3204 /*
3205 * We rely on the MSB to act as an escape bit to denote freelist
3206 * pointers. Make sure this bit isn't set in 'normal' class_idx usage.
3207 */
3208 BUILD_BUG_ON((MAX_LOCKDEP_KEYS-1) & CHAIN_BLK_FLAG);
3209
3210 init_data_structures_once();
3211
3212 if (nr_free_chain_hlocks < req)
3213 return -1;
3214
3215 /*
3216 * We require a minimum of 2 (u16) entries to encode a freelist
3217 * 'pointer'.
3218 */
3219 req = max(req, 2);
3220 bucket = size_to_bucket(req);
3221 curr = chain_block_buckets[bucket];
3222
3223 if (bucket) {
3224 if (curr >= 0) {
3225 del_chain_block(bucket, req, chain_block_next(curr));
3226 return curr;
3227 }
3228 /* Try bucket 0 */
3229 curr = chain_block_buckets[0];
3230 }
3231
3232 /*
3233 * The variable sized freelist is sorted by size; the first entry is
3234 * the largest. Use it if it fits.
3235 */
3236 if (curr >= 0) {
3237 size = chain_block_size(curr);
3238 if (likely(size >= req)) {
3239 del_chain_block(0, size, chain_block_next(curr));
3240 add_chain_block(curr + req, size - req);
3241 return curr;
3242 }
3243 }
3244
3245 /*
3246 * Last resort, split a block in a larger sized bucket.
3247 */
3248 for (size = MAX_CHAIN_BUCKETS; size > req; size--) {
3249 bucket = size_to_bucket(size);
3250 curr = chain_block_buckets[bucket];
3251 if (curr < 0)
3252 continue;
3253
3254 del_chain_block(bucket, size, chain_block_next(curr));
3255 add_chain_block(curr + req, size - req);
3256 return curr;
3257 }
3258
3259 return -1;
3260}
3261
3262static inline void free_chain_hlocks(int base, int size)
3263{
3264 add_chain_block(base, max(size, 2));
3265}
Huang, Ying443cd502008-06-20 16:39:21 +08003266
3267struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
3268{
Boqun Fengf611e8c2020-08-07 15:42:33 +08003269 u16 chain_hlock = chain_hlocks[chain->base + i];
3270 unsigned int class_idx = chain_hlock_class_idx(chain_hlock);
3271
3272 return lock_classes + class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08003273}
Peter Zijlstra8e182572007-07-19 01:48:54 -07003274
3275/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003276 * Returns the index of the first held_lock of the current chain
3277 */
3278static inline int get_first_held_lock(struct task_struct *curr,
3279 struct held_lock *hlock)
3280{
3281 int i;
3282 struct held_lock *hlock_curr;
3283
3284 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
3285 hlock_curr = curr->held_locks + i;
3286 if (hlock_curr->irq_context != hlock->irq_context)
3287 break;
3288
3289 }
3290
3291 return ++i;
3292}
3293
Borislav Petkov5c8a0102016-04-04 10:42:07 +02003294#ifdef CONFIG_DEBUG_LOCKDEP
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003295/*
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003296 * Returns the next chain_key iteration
3297 */
Boqun Fengf611e8c2020-08-07 15:42:33 +08003298static u64 print_chain_key_iteration(u16 hlock_id, u64 chain_key)
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003299{
Boqun Fengf611e8c2020-08-07 15:42:33 +08003300 u64 new_chain_key = iterate_chain_key(chain_key, hlock_id);
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003301
Boqun Fengf611e8c2020-08-07 15:42:33 +08003302 printk(" hlock_id:%d -> chain_key:%016Lx",
3303 (unsigned int)hlock_id,
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003304 (unsigned long long)new_chain_key);
3305 return new_chain_key;
3306}
3307
3308static void
3309print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
3310{
3311 struct held_lock *hlock;
Yuyang Duf6ec8822019-05-06 16:19:24 +08003312 u64 chain_key = INITIAL_CHAIN_KEY;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003313 int depth = curr->lockdep_depth;
Yuyang Du834494b2019-05-06 16:19:21 +08003314 int i = get_first_held_lock(curr, hlock_next);
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003315
Yuyang Du834494b2019-05-06 16:19:21 +08003316 printk("depth: %u (irq_context %u)\n", depth - i + 1,
3317 hlock_next->irq_context);
3318 for (; i < depth; i++) {
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003319 hlock = curr->held_locks + i;
Boqun Fengf611e8c2020-08-07 15:42:33 +08003320 chain_key = print_chain_key_iteration(hlock_id(hlock), chain_key);
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003321
3322 print_lock(hlock);
3323 }
3324
Boqun Fengf611e8c2020-08-07 15:42:33 +08003325 print_chain_key_iteration(hlock_id(hlock_next), chain_key);
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003326 print_lock(hlock_next);
3327}
3328
3329static void print_chain_keys_chain(struct lock_chain *chain)
3330{
3331 int i;
Yuyang Duf6ec8822019-05-06 16:19:24 +08003332 u64 chain_key = INITIAL_CHAIN_KEY;
Boqun Fengf611e8c2020-08-07 15:42:33 +08003333 u16 hlock_id;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003334
3335 printk("depth: %u\n", chain->depth);
3336 for (i = 0; i < chain->depth; i++) {
Boqun Fengf611e8c2020-08-07 15:42:33 +08003337 hlock_id = chain_hlocks[chain->base + i];
3338 chain_key = print_chain_key_iteration(hlock_id, chain_key);
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003339
Boqun Fengf611e8c2020-08-07 15:42:33 +08003340 print_lock_name(lock_classes + chain_hlock_class_idx(hlock_id) - 1);
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003341 printk("\n");
3342 }
3343}
3344
3345static void print_collision(struct task_struct *curr,
3346 struct held_lock *hlock_next,
3347 struct lock_chain *chain)
3348{
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003349 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003350 pr_warn("============================\n");
3351 pr_warn("WARNING: chain_key collision\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003352 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003353 pr_warn("----------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003354 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
3355 pr_warn("Hash chain already cached but the contents don't match!\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003356
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003357 pr_warn("Held locks:");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003358 print_chain_keys_held_locks(curr, hlock_next);
3359
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003360 pr_warn("Locks in cached chain:");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003361 print_chain_keys_chain(chain);
3362
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003363 pr_warn("\nstack backtrace:\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003364 dump_stack();
3365}
Borislav Petkov5c8a0102016-04-04 10:42:07 +02003366#endif
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003367
3368/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003369 * Checks whether the chain and the current held locks are consistent
3370 * in depth and also in content. If they are not it most likely means
3371 * that there was a collision during the calculation of the chain_key.
3372 * Returns: 0 not passed, 1 passed
3373 */
3374static int check_no_collision(struct task_struct *curr,
3375 struct held_lock *hlock,
3376 struct lock_chain *chain)
3377{
3378#ifdef CONFIG_DEBUG_LOCKDEP
3379 int i, j, id;
3380
3381 i = get_first_held_lock(curr, hlock);
3382
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003383 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
3384 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003385 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003386 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003387
3388 for (j = 0; j < chain->depth - 1; j++, i++) {
Boqun Fengf611e8c2020-08-07 15:42:33 +08003389 id = hlock_id(&curr->held_locks[i]);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003390
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003391 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
3392 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003393 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003394 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003395 }
3396#endif
3397 return 1;
3398}
3399
3400/*
Bart Van Assche22126842019-02-14 15:00:48 -08003401 * Given an index that is >= -1, return the index of the next lock chain.
3402 * Return -2 if there is no next lock chain.
3403 */
3404long lockdep_next_lockchain(long i)
3405{
Bart Van Asschede4643a2019-02-14 15:00:50 -08003406 i = find_next_bit(lock_chains_in_use, ARRAY_SIZE(lock_chains), i + 1);
3407 return i < ARRAY_SIZE(lock_chains) ? i : -2;
Bart Van Assche22126842019-02-14 15:00:48 -08003408}
3409
3410unsigned long lock_chain_count(void)
3411{
Bart Van Asschede4643a2019-02-14 15:00:50 -08003412 return bitmap_weight(lock_chains_in_use, ARRAY_SIZE(lock_chains));
3413}
3414
3415/* Must be called with the graph lock held. */
3416static struct lock_chain *alloc_lock_chain(void)
3417{
3418 int idx = find_first_zero_bit(lock_chains_in_use,
3419 ARRAY_SIZE(lock_chains));
3420
3421 if (unlikely(idx >= ARRAY_SIZE(lock_chains)))
3422 return NULL;
3423 __set_bit(idx, lock_chains_in_use);
3424 return lock_chains + idx;
Bart Van Assche22126842019-02-14 15:00:48 -08003425}
3426
3427/*
Byungchul Park545c23f2017-08-07 16:12:48 +09003428 * Adds a dependency chain into chain hashtable. And must be called with
3429 * graph_lock held.
3430 *
3431 * Return 0 if fail, and graph_lock is released.
3432 * Return 1 if succeed, with graph_lock held.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003433 */
Byungchul Park545c23f2017-08-07 16:12:48 +09003434static inline int add_chain_cache(struct task_struct *curr,
3435 struct held_lock *hlock,
3436 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003437{
Andrew Mortona63f38c2016-02-03 13:44:12 -08003438 struct hlist_head *hash_head = chainhashentry(chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003439 struct lock_chain *chain;
Steven Rostedte0944ee2011-04-20 21:42:00 -04003440 int i, j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003441
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003442 /*
Bart Van Assche527af3e2019-02-14 15:00:49 -08003443 * The caller must hold the graph lock, ensure we've got IRQs
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003444 * disabled to make this an IRQ-safe lock.. for recursion reasons
3445 * lockdep won't complain about its own locking errors.
3446 */
Peter Zijlstra248efb22020-03-13 11:09:49 +01003447 if (lockdep_assert_locked())
Jarek Poplawski381a2292007-02-10 01:44:58 -08003448 return 0;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003449
Bart Van Asschede4643a2019-02-14 15:00:50 -08003450 chain = alloc_lock_chain();
3451 if (!chain) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003452 if (!debug_locks_off_graph_unlock())
3453 return 0;
3454
Dave Jones2c522832013-04-25 13:40:02 -04003455 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01003456 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003457 return 0;
3458 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003459 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08003460 chain->irq_context = hlock->irq_context;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003461 i = get_first_held_lock(curr, hlock);
Huang, Ying443cd502008-06-20 16:39:21 +08003462 chain->depth = curr->lockdep_depth + 1 - i;
Peter Zijlstra75dd6022016-03-30 11:36:59 +02003463
3464 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
3465 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
3466 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
3467
Waiman Long810507f2020-02-06 10:24:08 -05003468 j = alloc_chain_hlocks(chain->depth);
3469 if (j < 0) {
Byungchul Parkf9af4562017-01-13 11:42:04 +09003470 if (!debug_locks_off_graph_unlock())
Peter Zijlstra75dd6022016-03-30 11:36:59 +02003471 return 0;
3472
3473 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
3474 dump_stack();
3475 return 0;
3476 }
Peter Zijlstra75dd6022016-03-30 11:36:59 +02003477
Waiman Long810507f2020-02-06 10:24:08 -05003478 chain->base = j;
3479 for (j = 0; j < chain->depth - 1; j++, i++) {
Boqun Fengf611e8c2020-08-07 15:42:33 +08003480 int lock_id = hlock_id(curr->held_locks + i);
Waiman Long810507f2020-02-06 10:24:08 -05003481
3482 chain_hlocks[chain->base + j] = lock_id;
3483 }
Boqun Fengf611e8c2020-08-07 15:42:33 +08003484 chain_hlocks[chain->base + j] = hlock_id(hlock);
Andrew Mortona63f38c2016-02-03 13:44:12 -08003485 hlist_add_head_rcu(&chain->entry, hash_head);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003486 debug_atomic_inc(chain_lookup_misses);
Waiman Longb3b9c182020-02-06 10:24:03 -05003487 inc_chains(chain->irq_context);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003488
3489 return 1;
3490}
Peter Zijlstra8e182572007-07-19 01:48:54 -07003491
Byungchul Park545c23f2017-08-07 16:12:48 +09003492/*
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08003493 * Look up a dependency chain. Must be called with either the graph lock or
3494 * the RCU read lock held.
Byungchul Park545c23f2017-08-07 16:12:48 +09003495 */
3496static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
3497{
3498 struct hlist_head *hash_head = chainhashentry(chain_key);
3499 struct lock_chain *chain;
3500
Byungchul Park545c23f2017-08-07 16:12:48 +09003501 hlist_for_each_entry_rcu(chain, hash_head, entry) {
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08003502 if (READ_ONCE(chain->chain_key) == chain_key) {
Byungchul Park545c23f2017-08-07 16:12:48 +09003503 debug_atomic_inc(chain_lookup_hits);
3504 return chain;
3505 }
3506 }
3507 return NULL;
3508}
3509
3510/*
3511 * If the key is not present yet in dependency chain cache then
3512 * add it and return 1 - in this case the new dependency chain is
3513 * validated. If the key is already hashed, return 0.
3514 * (On return with 1 graph_lock is held.)
3515 */
3516static inline int lookup_chain_cache_add(struct task_struct *curr,
3517 struct held_lock *hlock,
3518 u64 chain_key)
3519{
3520 struct lock_class *class = hlock_class(hlock);
3521 struct lock_chain *chain = lookup_chain_cache(chain_key);
3522
3523 if (chain) {
3524cache_hit:
3525 if (!check_no_collision(curr, hlock, chain))
3526 return 0;
3527
3528 if (very_verbose(class)) {
3529 printk("\nhash chain already cached, key: "
Borislav Petkov04860d42018-02-26 14:49:26 +01003530 "%016Lx tail class: [%px] %s\n",
Byungchul Park545c23f2017-08-07 16:12:48 +09003531 (unsigned long long)chain_key,
3532 class->key, class->name);
3533 }
3534
3535 return 0;
3536 }
3537
3538 if (very_verbose(class)) {
Borislav Petkov04860d42018-02-26 14:49:26 +01003539 printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
Byungchul Park545c23f2017-08-07 16:12:48 +09003540 (unsigned long long)chain_key, class->key, class->name);
3541 }
3542
3543 if (!graph_lock())
3544 return 0;
3545
3546 /*
3547 * We have to walk the chain again locked - to avoid duplicates:
3548 */
3549 chain = lookup_chain_cache(chain_key);
3550 if (chain) {
3551 graph_unlock();
3552 goto cache_hit;
3553 }
3554
3555 if (!add_chain_cache(curr, hlock, chain_key))
3556 return 0;
3557
3558 return 1;
3559}
3560
Yuyang Du0b9fc8e2019-05-06 16:19:26 +08003561static int validate_chain(struct task_struct *curr,
3562 struct held_lock *hlock,
3563 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07003564{
3565 /*
3566 * Trylock needs to maintain the stack of held locks, but it
3567 * does not add new dependencies, because trylock can be done
3568 * in any order.
3569 *
3570 * We look up the chain_key and do the O(N^2) check and update of
3571 * the dependencies only if this is a new dependency chain.
Byungchul Park545c23f2017-08-07 16:12:48 +09003572 * (If lookup_chain_cache_add() return with 1 it acquires
Peter Zijlstra8e182572007-07-19 01:48:54 -07003573 * graph_lock for us)
3574 */
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003575 if (!hlock->trylock && hlock->check &&
Byungchul Park545c23f2017-08-07 16:12:48 +09003576 lookup_chain_cache_add(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07003577 /*
3578 * Check whether last held lock:
3579 *
3580 * - is irq-safe, if this lock is irq-unsafe
3581 * - is softirq-safe, if this lock is hardirq-unsafe
3582 *
3583 * And check whether the new lock's dependency graph
Yuyang Du31a490e2019-05-06 16:19:27 +08003584 * could lead back to the previous lock:
Peter Zijlstra8e182572007-07-19 01:48:54 -07003585 *
Yuyang Du31a490e2019-05-06 16:19:27 +08003586 * - within the current held-lock stack
3587 * - across our accumulated lock dependency records
3588 *
3589 * any of these scenarios could lead to a deadlock.
3590 */
3591 /*
3592 * The simple case: does the current hold the same lock
3593 * already?
Peter Zijlstra8e182572007-07-19 01:48:54 -07003594 */
Yuyang Du4609c4f2019-05-06 16:19:33 +08003595 int ret = check_deadlock(curr, hlock);
Peter Zijlstra8e182572007-07-19 01:48:54 -07003596
3597 if (!ret)
3598 return 0;
3599 /*
Peter Zijlstra8e182572007-07-19 01:48:54 -07003600 * Add dependency only if this lock is not the head
Boqun Fengd61fc96a2020-11-02 13:37:41 +08003601 * of the chain, and if the new lock introduces no more
3602 * lock dependency (because we already hold a lock with the
3603 * same lock class) nor deadlock (because the nest_lock
3604 * serializes nesting locks), see the comments for
3605 * check_deadlock().
Peter Zijlstra8e182572007-07-19 01:48:54 -07003606 */
Byungchul Park545c23f2017-08-07 16:12:48 +09003607 if (!chain_head && ret != 2) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07003608 if (!check_prevs_add(curr, hlock))
3609 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09003610 }
3611
Peter Zijlstra8e182572007-07-19 01:48:54 -07003612 graph_unlock();
Byungchul Park545c23f2017-08-07 16:12:48 +09003613 } else {
3614 /* after lookup_chain_cache_add(): */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003615 if (unlikely(!debug_locks))
3616 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09003617 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07003618
3619 return 1;
3620}
3621#else
3622static inline int validate_chain(struct task_struct *curr,
Yuyang Du0b9fc8e2019-05-06 16:19:26 +08003623 struct held_lock *hlock,
3624 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07003625{
3626 return 1;
3627}
Waiman Long810507f2020-02-06 10:24:08 -05003628
3629static void init_chain_block_buckets(void) { }
Yuyang Due7a38f62019-05-06 16:19:20 +08003630#endif /* CONFIG_PROVE_LOCKING */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003631
3632/*
3633 * We are building curr_chain_key incrementally, so double-check
3634 * it from scratch, to make sure that it's done correctly:
3635 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003636static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003637{
3638#ifdef CONFIG_DEBUG_LOCKDEP
3639 struct held_lock *hlock, *prev_hlock = NULL;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003640 unsigned int i;
Yuyang Duf6ec8822019-05-06 16:19:24 +08003641 u64 chain_key = INITIAL_CHAIN_KEY;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003642
3643 for (i = 0; i < curr->lockdep_depth; i++) {
3644 hlock = curr->held_locks + i;
3645 if (chain_key != hlock->prev_chain_key) {
3646 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003647 /*
3648 * We got mighty confused, our chain keys don't match
3649 * with what we expect, someone trample on our task state?
3650 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07003651 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003652 curr->lockdep_depth, i,
3653 (unsigned long long)chain_key,
3654 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003655 return;
3656 }
Yuyang Du01bb6f02019-05-06 16:19:25 +08003657
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003658 /*
Yuyang Du01bb6f02019-05-06 16:19:25 +08003659 * hlock->class_idx can't go beyond MAX_LOCKDEP_KEYS, but is
3660 * it registered lock class index?
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003661 */
Yuyang Du01bb6f02019-05-06 16:19:25 +08003662 if (DEBUG_LOCKS_WARN_ON(!test_bit(hlock->class_idx, lock_classes_in_use)))
Jarek Poplawski381a2292007-02-10 01:44:58 -08003663 return;
3664
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003665 if (prev_hlock && (prev_hlock->irq_context !=
3666 hlock->irq_context))
Yuyang Duf6ec8822019-05-06 16:19:24 +08003667 chain_key = INITIAL_CHAIN_KEY;
Boqun Fengf611e8c2020-08-07 15:42:33 +08003668 chain_key = iterate_chain_key(chain_key, hlock_id(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003669 prev_hlock = hlock;
3670 }
3671 if (chain_key != curr->curr_chain_key) {
3672 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003673 /*
3674 * More smoking hash instead of calculating it, damn see these
3675 * numbers float.. I bet that a pink elephant stepped on my memory.
3676 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07003677 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003678 curr->lockdep_depth, i,
3679 (unsigned long long)chain_key,
3680 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003681 }
3682#endif
3683}
3684
Arnd Bergmann30a35f72019-06-28 12:29:03 +02003685#ifdef CONFIG_PROVE_LOCKING
Frederic Weisbecker0d2cc3b2019-04-02 18:02:41 +02003686static int mark_lock(struct task_struct *curr, struct held_lock *this,
3687 enum lock_usage_bit new_bit);
3688
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003689static void print_usage_bug_scenario(struct held_lock *lock)
Steven Rostedt282b5c22011-04-20 21:41:59 -04003690{
3691 struct lock_class *class = hlock_class(lock);
3692
3693 printk(" Possible unsafe locking scenario:\n\n");
3694 printk(" CPU0\n");
3695 printk(" ----\n");
3696 printk(" lock(");
3697 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01003698 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04003699 printk(" <Interrupt>\n");
3700 printk(" lock(");
3701 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01003702 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04003703 printk("\n *** DEADLOCK ***\n\n");
3704}
3705
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003706static void
Peter Zijlstra8e182572007-07-19 01:48:54 -07003707print_usage_bug(struct task_struct *curr, struct held_lock *this,
3708 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
3709{
3710 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003711 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003712
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003713 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003714 pr_warn("================================\n");
3715 pr_warn("WARNING: inconsistent lock state\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003716 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003717 pr_warn("--------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07003718
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003719 pr_warn("inconsistent {%s} -> {%s} usage.\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07003720 usage_str[prev_bit], usage_str[new_bit]);
3721
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003722 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003723 curr->comm, task_pid_nr(curr),
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02003724 lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT,
Peter Zijlstraef996912020-03-20 12:56:42 +01003725 lockdep_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02003726 lockdep_hardirqs_enabled(),
Peter Zijlstraef996912020-03-20 12:56:42 +01003727 lockdep_softirqs_enabled(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07003728 print_lock(this);
3729
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003730 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
Bart Van Assche12593b72019-07-22 11:24:42 -07003731 print_lock_trace(hlock_class(this)->usage_traces[prev_bit], 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07003732
3733 print_irqtrace_events(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003734 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04003735 print_usage_bug_scenario(this);
3736
Peter Zijlstra8e182572007-07-19 01:48:54 -07003737 lockdep_print_held_locks(curr);
3738
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003739 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07003740 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -07003741}
3742
3743/*
3744 * Print out an error if an invalid bit is set:
3745 */
3746static inline int
3747valid_state(struct task_struct *curr, struct held_lock *this,
3748 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
3749{
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003750 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) {
3751 print_usage_bug(curr, this, bad_bit, new_bit);
3752 return 0;
3753 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07003754 return 1;
3755}
3756
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003757
3758/*
3759 * print irq inversion bug:
3760 */
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003761static void
Ming Lei24208ca2009-07-16 15:44:29 +02003762print_irq_inversion_bug(struct task_struct *curr,
3763 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003764 struct held_lock *this, int forwards,
3765 const char *irqclass)
3766{
Steven Rostedtdad3d742011-04-20 21:41:57 -04003767 struct lock_list *entry = other;
3768 struct lock_list *middle = NULL;
3769 int depth;
3770
Ingo Molnar74c383f2006-12-13 00:34:43 -08003771 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003772 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003773
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003774 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003775 pr_warn("========================================================\n");
3776 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003777 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003778 pr_warn("--------------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003779 pr_warn("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003780 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003781 print_lock(this);
3782 if (forwards)
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003783 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003784 else
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003785 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02003786 print_lock_name(other->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003787 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003788
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003789 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04003790
3791 /* Find a middle lock (if one exists) */
3792 depth = get_lock_depth(other);
3793 do {
3794 if (depth == 0 && (entry != root)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003795 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
Steven Rostedtdad3d742011-04-20 21:41:57 -04003796 break;
3797 }
3798 middle = entry;
3799 entry = get_lock_parent(entry);
3800 depth--;
3801 } while (entry && entry != root && (depth >= 0));
3802 if (forwards)
3803 print_irq_lock_scenario(root, other,
3804 middle ? middle->class : root->class, other->class);
3805 else
3806 print_irq_lock_scenario(other, root,
3807 middle ? middle->class : other->class, root->class);
3808
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003809 lockdep_print_held_locks(curr);
3810
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003811 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
Bart Van Assche12593b72019-07-22 11:24:42 -07003812 root->trace = save_trace();
3813 if (!root->trace)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003814 return;
Ming Lei24208ca2009-07-16 15:44:29 +02003815 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003816
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003817 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003818 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003819}
3820
3821/*
3822 * Prove that in the forwards-direction subgraph starting at <this>
3823 * there is no lock matching <mask>:
3824 */
3825static int
3826check_usage_forwards(struct task_struct *curr, struct held_lock *this,
Boqun Fengf08e3882020-08-07 15:42:30 +08003827 enum lock_usage_bit bit)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003828{
Boqun Fengb11be022020-08-07 15:42:22 +08003829 enum bfs_result ret;
Ming Leid7aaba12009-07-16 15:44:29 +02003830 struct lock_list root;
Kees Cook3f649ab2020-06-03 13:09:38 -07003831 struct lock_list *target_entry;
Boqun Fengf08e3882020-08-07 15:42:30 +08003832 enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK;
3833 unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003834
Boqun Feng6971c0f2020-08-07 15:42:26 +08003835 bfs_init_root(&root, this);
Boqun Fengf08e3882020-08-07 15:42:30 +08003836 ret = find_usage_forwards(&root, usage_mask, &target_entry);
Boqun Fengb11be022020-08-07 15:42:22 +08003837 if (bfs_error(ret)) {
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003838 print_bfs_bug(ret);
3839 return 0;
3840 }
Boqun Fengb11be022020-08-07 15:42:22 +08003841 if (ret == BFS_RNOMATCH)
3842 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003843
Boqun Fengf08e3882020-08-07 15:42:30 +08003844 /* Check whether write or read usage is the match */
3845 if (target_entry->class->usage_mask & lock_flag(bit)) {
3846 print_irq_inversion_bug(curr, &root, target_entry,
3847 this, 1, state_name(bit));
3848 } else {
3849 print_irq_inversion_bug(curr, &root, target_entry,
3850 this, 1, state_name(read_bit));
3851 }
3852
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003853 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003854}
3855
3856/*
3857 * Prove that in the backwards-direction subgraph starting at <this>
3858 * there is no lock matching <mask>:
3859 */
3860static int
3861check_usage_backwards(struct task_struct *curr, struct held_lock *this,
Boqun Fengf08e3882020-08-07 15:42:30 +08003862 enum lock_usage_bit bit)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003863{
Boqun Fengb11be022020-08-07 15:42:22 +08003864 enum bfs_result ret;
Ming Leid7aaba12009-07-16 15:44:29 +02003865 struct lock_list root;
Kees Cook3f649ab2020-06-03 13:09:38 -07003866 struct lock_list *target_entry;
Boqun Fengf08e3882020-08-07 15:42:30 +08003867 enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK;
3868 unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003869
Boqun Feng6971c0f2020-08-07 15:42:26 +08003870 bfs_init_rootb(&root, this);
Boqun Fengf08e3882020-08-07 15:42:30 +08003871 ret = find_usage_backwards(&root, usage_mask, &target_entry);
Boqun Fengb11be022020-08-07 15:42:22 +08003872 if (bfs_error(ret)) {
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003873 print_bfs_bug(ret);
3874 return 0;
3875 }
Boqun Fengb11be022020-08-07 15:42:22 +08003876 if (ret == BFS_RNOMATCH)
3877 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003878
Boqun Fengf08e3882020-08-07 15:42:30 +08003879 /* Check whether write or read usage is the match */
3880 if (target_entry->class->usage_mask & lock_flag(bit)) {
3881 print_irq_inversion_bug(curr, &root, target_entry,
3882 this, 0, state_name(bit));
3883 } else {
3884 print_irq_inversion_bug(curr, &root, target_entry,
3885 this, 0, state_name(read_bit));
3886 }
3887
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003888 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003889}
3890
Ingo Molnar3117df02006-12-13 00:34:43 -08003891void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003892{
Marco Elver0584df92020-07-29 13:09:15 +02003893 const struct irqtrace_events *trace = &curr->irqtrace;
3894
3895 printk("irq event stamp: %u\n", trace->irq_events);
Borislav Petkov04860d42018-02-26 14:49:26 +01003896 printk("hardirqs last enabled at (%u): [<%px>] %pS\n",
Marco Elver0584df92020-07-29 13:09:15 +02003897 trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip,
3898 (void *)trace->hardirq_enable_ip);
Borislav Petkov04860d42018-02-26 14:49:26 +01003899 printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
Marco Elver0584df92020-07-29 13:09:15 +02003900 trace->hardirq_disable_event, (void *)trace->hardirq_disable_ip,
3901 (void *)trace->hardirq_disable_ip);
Borislav Petkov04860d42018-02-26 14:49:26 +01003902 printk("softirqs last enabled at (%u): [<%px>] %pS\n",
Marco Elver0584df92020-07-29 13:09:15 +02003903 trace->softirq_enable_event, (void *)trace->softirq_enable_ip,
3904 (void *)trace->softirq_enable_ip);
Borislav Petkov04860d42018-02-26 14:49:26 +01003905 printk("softirqs last disabled at (%u): [<%px>] %pS\n",
Marco Elver0584df92020-07-29 13:09:15 +02003906 trace->softirq_disable_event, (void *)trace->softirq_disable_ip,
3907 (void *)trace->softirq_disable_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003908}
3909
Peter Zijlstracd953022009-01-22 16:38:21 +01003910static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003911{
Peter Zijlstra8e182572007-07-19 01:48:54 -07003912#if HARDIRQ_VERBOSE
3913 return class_filter(class);
3914#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003915 return 0;
3916}
3917
Peter Zijlstracd953022009-01-22 16:38:21 +01003918static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003919{
Peter Zijlstra8e182572007-07-19 01:48:54 -07003920#if SOFTIRQ_VERBOSE
3921 return class_filter(class);
3922#endif
3923 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003924}
3925
Peter Zijlstracd953022009-01-22 16:38:21 +01003926static int (*state_verbose_f[])(struct lock_class *class) = {
3927#define LOCKDEP_STATE(__STATE) \
3928 __STATE##_verbose,
3929#include "lockdep_states.h"
3930#undef LOCKDEP_STATE
3931};
3932
3933static inline int state_verbose(enum lock_usage_bit bit,
3934 struct lock_class *class)
3935{
Frederic Weisbeckerc902a1e2019-04-02 18:02:42 +02003936 return state_verbose_f[bit >> LOCK_USAGE_DIR_MASK](class);
Peter Zijlstracd953022009-01-22 16:38:21 +01003937}
3938
Peter Zijlstra42c50d52009-01-22 16:58:16 +01003939typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
3940 enum lock_usage_bit bit, const char *name);
3941
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01003942static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01003943mark_lock_irq(struct task_struct *curr, struct held_lock *this,
3944 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01003945{
Peter Zijlstraf9892092009-01-22 16:09:59 +01003946 int excl_bit = exclusive_bit(new_bit);
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01003947 int read = new_bit & LOCK_USAGE_READ_MASK;
3948 int dir = new_bit & LOCK_USAGE_DIR_MASK;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01003949
Peter Zijlstra38aa2712009-01-27 14:53:50 +01003950 /*
Peter Zijlstra38aa2712009-01-27 14:53:50 +01003951 * Validate that this particular lock does not have conflicting
3952 * usage states.
3953 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01003954 if (!valid_state(curr, this, new_bit, excl_bit))
3955 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01003956
Peter Zijlstra38aa2712009-01-27 14:53:50 +01003957 /*
Boqun Fengf08e3882020-08-07 15:42:30 +08003958 * Check for read in write conflicts
3959 */
3960 if (!read && !valid_state(curr, this, new_bit,
3961 excl_bit + LOCK_USAGE_READ_MASK))
3962 return 0;
3963
3964
3965 /*
Peter Zijlstra38aa2712009-01-27 14:53:50 +01003966 * Validate that the lock dependencies don't have conflicting usage
3967 * states.
3968 */
Boqun Fengf08e3882020-08-07 15:42:30 +08003969 if (dir) {
3970 /*
3971 * mark ENABLED has to look backwards -- to ensure no dependee
3972 * has USED_IN state, which, again, would allow recursion deadlocks.
3973 */
3974 if (!check_usage_backwards(curr, this, excl_bit))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01003975 return 0;
Boqun Fengf08e3882020-08-07 15:42:30 +08003976 } else {
3977 /*
3978 * mark USED_IN has to look forwards -- to ensure no dependency
3979 * has ENABLED state, which would allow recursion deadlocks.
3980 */
3981 if (!check_usage_forwards(curr, this, excl_bit))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01003982 return 0;
3983 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01003984
Peter Zijlstracd953022009-01-22 16:38:21 +01003985 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01003986 return 2;
3987
3988 return 1;
3989}
3990
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003991/*
3992 * Mark all held locks with a usage bit:
3993 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003994static int
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01003995mark_held_locks(struct task_struct *curr, enum lock_usage_bit base_bit)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003996{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003997 struct held_lock *hlock;
3998 int i;
3999
4000 for (i = 0; i < curr->lockdep_depth; i++) {
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01004001 enum lock_usage_bit hlock_bit = base_bit;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004002 hlock = curr->held_locks + i;
4003
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01004004 if (hlock->read)
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01004005 hlock_bit += LOCK_USAGE_READ_MASK;
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01004006
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01004007 BUG_ON(hlock_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01004008
Oleg Nesterov34d0ed52014-01-20 19:20:13 +01004009 if (!hlock->check)
Peter Zijlstraefbe2ee2011-07-07 11:39:45 +02004010 continue;
4011
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01004012 if (!mark_lock(curr, hlock, hlock_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004013 return 0;
4014 }
4015
4016 return 1;
4017}
4018
4019/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004020 * Hardirqs will be enabled:
4021 */
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004022static void __trace_hardirqs_on_caller(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004023{
4024 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004025
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004026 /*
4027 * We are going to turn hardirqs on, so set the
4028 * usage bit for all held locks:
4029 */
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01004030 if (!mark_held_locks(curr, LOCK_ENABLED_HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004031 return;
4032 /*
4033 * If we have softirqs enabled, then set the usage
4034 * bit for all held locks. (disabled hardirqs prevented
4035 * this bit from being set before)
4036 */
4037 if (curr->softirqs_enabled)
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004038 mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004039}
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02004040
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004041/**
4042 * lockdep_hardirqs_on_prepare - Prepare for enabling interrupts
4043 * @ip: Caller address
4044 *
4045 * Invoked before a possible transition to RCU idle from exit to user or
4046 * guest mode. This ensures that all RCU operations are done before RCU
4047 * stops watching. After the RCU transition lockdep_hardirqs_on() has to be
4048 * invoked to set the final state.
4049 */
4050void lockdep_hardirqs_on_prepare(unsigned long ip)
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02004051{
Peter Zijlstra859d0692020-05-27 15:00:57 +02004052 if (unlikely(!debug_locks))
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02004053 return;
4054
Peter Zijlstra859d0692020-05-27 15:00:57 +02004055 /*
4056 * NMIs do not (and cannot) track lock dependencies, nothing to do.
4057 */
4058 if (unlikely(in_nmi()))
4059 return;
4060
Peter Zijlstraf8e48a32020-10-22 12:23:02 +02004061 if (unlikely(this_cpu_read(lockdep_recursion)))
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02004062 return;
4063
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004064 if (unlikely(lockdep_hardirqs_enabled())) {
Peter Zijlstra7d36b262011-07-26 13:13:44 +02004065 /*
4066 * Neither irq nor preemption are disabled here
4067 * so this is racy by nature but losing one hit
4068 * in a stat is not a big deal.
4069 */
4070 __debug_atomic_inc(redundant_hardirqs_on);
4071 return;
4072 }
4073
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004074 /*
4075 * We're enabling irqs and according to our state above irqs weren't
4076 * already enabled, yet we find the hardware thinks they are in fact
4077 * enabled.. someone messed up their IRQ state tracing.
4078 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02004079 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4080 return;
4081
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004082 /*
4083 * See the fine text that goes along with this variable definition.
4084 */
zhengbind6710022019-04-29 20:26:31 +08004085 if (DEBUG_LOCKS_WARN_ON(early_boot_irqs_disabled))
Peter Zijlstra7d36b262011-07-26 13:13:44 +02004086 return;
4087
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004088 /*
4089 * Can't allow enabling interrupts while in an interrupt handler,
4090 * that's general bad form and such. Recursion, limited stack etc..
4091 */
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004092 if (DEBUG_LOCKS_WARN_ON(lockdep_hardirq_context()))
Peter Zijlstra7d36b262011-07-26 13:13:44 +02004093 return;
4094
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004095 current->hardirq_chain_key = current->curr_chain_key;
4096
Peter Zijlstra4d004092020-10-02 11:04:21 +02004097 lockdep_recursion_inc();
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004098 __trace_hardirqs_on_caller();
Peter Zijlstra10476e62020-03-13 09:56:38 +01004099 lockdep_recursion_finish();
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02004100}
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004101EXPORT_SYMBOL_GPL(lockdep_hardirqs_on_prepare);
4102
4103void noinstr lockdep_hardirqs_on(unsigned long ip)
4104{
Marco Elver0584df92020-07-29 13:09:15 +02004105 struct irqtrace_events *trace = &current->irqtrace;
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004106
Peter Zijlstra859d0692020-05-27 15:00:57 +02004107 if (unlikely(!debug_locks))
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004108 return;
4109
Peter Zijlstra859d0692020-05-27 15:00:57 +02004110 /*
4111 * NMIs can happen in the middle of local_irq_{en,dis}able() where the
4112 * tracking state and hardware state are out of sync.
4113 *
4114 * NMIs must save lockdep_hardirqs_enabled() to restore IRQ state from,
4115 * and not rely on hardware state like normal interrupts.
4116 */
4117 if (unlikely(in_nmi())) {
peterz@infradead.orged004952020-07-27 14:48:52 +02004118 if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI))
4119 return;
4120
Peter Zijlstra859d0692020-05-27 15:00:57 +02004121 /*
4122 * Skip:
4123 * - recursion check, because NMI can hit lockdep;
4124 * - hardware state check, because above;
4125 * - chain_key check, see lockdep_hardirqs_on_prepare().
4126 */
4127 goto skip_checks;
4128 }
4129
Peter Zijlstraf8e48a32020-10-22 12:23:02 +02004130 if (unlikely(this_cpu_read(lockdep_recursion)))
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004131 return;
4132
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004133 if (lockdep_hardirqs_enabled()) {
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004134 /*
4135 * Neither irq nor preemption are disabled here
4136 * so this is racy by nature but losing one hit
4137 * in a stat is not a big deal.
4138 */
4139 __debug_atomic_inc(redundant_hardirqs_on);
4140 return;
4141 }
4142
4143 /*
4144 * We're enabling irqs and according to our state above irqs weren't
4145 * already enabled, yet we find the hardware thinks they are in fact
4146 * enabled.. someone messed up their IRQ state tracing.
4147 */
4148 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4149 return;
4150
4151 /*
4152 * Ensure the lock stack remained unchanged between
4153 * lockdep_hardirqs_on_prepare() and lockdep_hardirqs_on().
4154 */
4155 DEBUG_LOCKS_WARN_ON(current->hardirq_chain_key !=
4156 current->curr_chain_key);
4157
Peter Zijlstra859d0692020-05-27 15:00:57 +02004158skip_checks:
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004159 /* we'll do an OFF -> ON transition: */
Peter Zijlstrafddf9052020-08-20 09:13:30 +02004160 __this_cpu_write(hardirqs_enabled, 1);
Marco Elver0584df92020-07-29 13:09:15 +02004161 trace->hardirq_enable_ip = ip;
4162 trace->hardirq_enable_event = ++trace->irq_events;
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004163 debug_atomic_inc(hardirqs_on_events);
4164}
4165EXPORT_SYMBOL_GPL(lockdep_hardirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004166
4167/*
4168 * Hardirqs were disabled:
4169 */
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004170void noinstr lockdep_hardirqs_off(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004171{
Peter Zijlstra859d0692020-05-27 15:00:57 +02004172 if (unlikely(!debug_locks))
4173 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004174
Peter Zijlstra859d0692020-05-27 15:00:57 +02004175 /*
4176 * Matching lockdep_hardirqs_on(), allow NMIs in the middle of lockdep;
4177 * they will restore the software state. This ensures the software
4178 * state is consistent inside NMIs as well.
4179 */
peterz@infradead.orged004952020-07-27 14:48:52 +02004180 if (in_nmi()) {
4181 if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI))
4182 return;
Peter Zijlstra4d004092020-10-02 11:04:21 +02004183 } else if (__this_cpu_read(lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004184 return;
4185
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004186 /*
4187 * So we're supposed to get called after you mask local IRQs, but for
4188 * some reason the hardware doesn't quite think you did a proper job.
4189 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004190 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4191 return;
4192
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004193 if (lockdep_hardirqs_enabled()) {
Marco Elver0584df92020-07-29 13:09:15 +02004194 struct irqtrace_events *trace = &current->irqtrace;
4195
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004196 /*
4197 * We have done an ON -> OFF transition:
4198 */
Peter Zijlstrafddf9052020-08-20 09:13:30 +02004199 __this_cpu_write(hardirqs_enabled, 0);
Marco Elver0584df92020-07-29 13:09:15 +02004200 trace->hardirq_disable_ip = ip;
4201 trace->hardirq_disable_event = ++trace->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02004202 debug_atomic_inc(hardirqs_off_events);
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004203 } else {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02004204 debug_atomic_inc(redundant_hardirqs_off);
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004205 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004206}
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004207EXPORT_SYMBOL_GPL(lockdep_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004208
4209/*
4210 * Softirqs will be enabled:
4211 */
Peter Zijlstra0d384532020-03-20 12:56:41 +01004212void lockdep_softirqs_on(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004213{
Marco Elver0584df92020-07-29 13:09:15 +02004214 struct irqtrace_events *trace = &current->irqtrace;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004215
Peter Zijlstra4d004092020-10-02 11:04:21 +02004216 if (unlikely(!lockdep_enabled()))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004217 return;
4218
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004219 /*
4220 * We fancy IRQs being disabled here, see softirq.c, avoids
4221 * funny state and nesting things.
4222 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004223 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4224 return;
4225
Marco Elver0584df92020-07-29 13:09:15 +02004226 if (current->softirqs_enabled) {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02004227 debug_atomic_inc(redundant_softirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004228 return;
4229 }
4230
Peter Zijlstra4d004092020-10-02 11:04:21 +02004231 lockdep_recursion_inc();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004232 /*
4233 * We'll do an OFF -> ON transition:
4234 */
Marco Elver0584df92020-07-29 13:09:15 +02004235 current->softirqs_enabled = 1;
4236 trace->softirq_enable_ip = ip;
4237 trace->softirq_enable_event = ++trace->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02004238 debug_atomic_inc(softirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004239 /*
4240 * We are going to turn softirqs on, so set the
4241 * usage bit for all held locks, if hardirqs are
4242 * enabled too:
4243 */
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004244 if (lockdep_hardirqs_enabled())
Marco Elver0584df92020-07-29 13:09:15 +02004245 mark_held_locks(current, LOCK_ENABLED_SOFTIRQ);
Peter Zijlstra10476e62020-03-13 09:56:38 +01004246 lockdep_recursion_finish();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004247}
4248
4249/*
4250 * Softirqs were disabled:
4251 */
Peter Zijlstra0d384532020-03-20 12:56:41 +01004252void lockdep_softirqs_off(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004253{
Peter Zijlstra4d004092020-10-02 11:04:21 +02004254 if (unlikely(!lockdep_enabled()))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004255 return;
4256
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004257 /*
4258 * We fancy IRQs being disabled here, see softirq.c
4259 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004260 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4261 return;
4262
Marco Elver0584df92020-07-29 13:09:15 +02004263 if (current->softirqs_enabled) {
4264 struct irqtrace_events *trace = &current->irqtrace;
4265
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004266 /*
4267 * We have done an ON -> OFF transition:
4268 */
Marco Elver0584df92020-07-29 13:09:15 +02004269 current->softirqs_enabled = 0;
4270 trace->softirq_disable_ip = ip;
4271 trace->softirq_disable_event = ++trace->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02004272 debug_atomic_inc(softirqs_off_events);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004273 /*
4274 * Whoops, we wanted softirqs off, so why aren't they?
4275 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004276 DEBUG_LOCKS_WARN_ON(!softirq_count());
4277 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02004278 debug_atomic_inc(redundant_softirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004279}
4280
Yuyang Du09180652019-05-06 16:19:37 +08004281static int
4282mark_usage(struct task_struct *curr, struct held_lock *hlock, int check)
Peter Zijlstra8e182572007-07-19 01:48:54 -07004283{
Yuyang Du09180652019-05-06 16:19:37 +08004284 if (!check)
4285 goto lock_used;
4286
Peter Zijlstra8e182572007-07-19 01:48:54 -07004287 /*
4288 * If non-trylock use in a hardirq or softirq context, then
4289 * mark the lock as used in these contexts:
4290 */
4291 if (!hlock->trylock) {
4292 if (hlock->read) {
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004293 if (lockdep_hardirq_context())
Peter Zijlstra8e182572007-07-19 01:48:54 -07004294 if (!mark_lock(curr, hlock,
4295 LOCK_USED_IN_HARDIRQ_READ))
4296 return 0;
4297 if (curr->softirq_context)
4298 if (!mark_lock(curr, hlock,
4299 LOCK_USED_IN_SOFTIRQ_READ))
4300 return 0;
4301 } else {
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004302 if (lockdep_hardirq_context())
Peter Zijlstra8e182572007-07-19 01:48:54 -07004303 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
4304 return 0;
4305 if (curr->softirq_context)
4306 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
4307 return 0;
4308 }
4309 }
4310 if (!hlock->hardirqs_off) {
4311 if (hlock->read) {
4312 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01004313 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07004314 return 0;
4315 if (curr->softirqs_enabled)
4316 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01004317 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07004318 return 0;
4319 } else {
4320 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01004321 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07004322 return 0;
4323 if (curr->softirqs_enabled)
4324 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01004325 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07004326 return 0;
4327 }
4328 }
4329
Yuyang Du09180652019-05-06 16:19:37 +08004330lock_used:
4331 /* mark it as used: */
4332 if (!mark_lock(curr, hlock, LOCK_USED))
4333 return 0;
4334
Peter Zijlstra8e182572007-07-19 01:48:54 -07004335 return 1;
4336}
4337
Boqun Fengc2469752016-02-16 13:57:40 +08004338static inline unsigned int task_irq_context(struct task_struct *task)
4339{
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004340 return LOCK_CHAIN_HARDIRQ_CONTEXT * !!lockdep_hardirq_context() +
Waiman Longb3b9c182020-02-06 10:24:03 -05004341 LOCK_CHAIN_SOFTIRQ_CONTEXT * !!task->softirq_context;
Boqun Fengc2469752016-02-16 13:57:40 +08004342}
4343
Peter Zijlstra8e182572007-07-19 01:48:54 -07004344static int separate_irq_context(struct task_struct *curr,
4345 struct held_lock *hlock)
4346{
4347 unsigned int depth = curr->lockdep_depth;
4348
4349 /*
4350 * Keep track of points where we cross into an interrupt context:
4351 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07004352 if (depth) {
4353 struct held_lock *prev_hlock;
4354
4355 prev_hlock = curr->held_locks + depth-1;
4356 /*
4357 * If we cross into another context, reset the
4358 * hash key (this also prevents the checking and the
4359 * adding of the dependency to 'prev'):
4360 */
4361 if (prev_hlock->irq_context != hlock->irq_context)
4362 return 1;
4363 }
4364 return 0;
4365}
4366
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004367/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07004368 * Mark a lock with a usage bit, and validate the state transition:
4369 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02004370static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02004371 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07004372{
Peter Zijlstra2bb89452020-09-30 11:49:37 +02004373 unsigned int new_mask, ret = 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07004374
Yuyang Du4d563302019-05-06 16:19:38 +08004375 if (new_bit >= LOCK_USAGE_STATES) {
4376 DEBUG_LOCKS_WARN_ON(1);
4377 return 0;
4378 }
4379
peterz@infradead.org23870f12020-09-02 18:03:23 +02004380 if (new_bit == LOCK_USED && this->read)
4381 new_bit = LOCK_USED_READ;
4382
4383 new_mask = 1 << new_bit;
4384
Peter Zijlstra8e182572007-07-19 01:48:54 -07004385 /*
4386 * If already set then do not dirty the cacheline,
4387 * nor do any checks:
4388 */
Dave Jonesf82b2172008-08-11 09:30:23 +02004389 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07004390 return 1;
4391
4392 if (!graph_lock())
4393 return 0;
4394 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004395 * Make sure we didn't race:
Peter Zijlstra8e182572007-07-19 01:48:54 -07004396 */
peterz@infradead.org23870f12020-09-02 18:03:23 +02004397 if (unlikely(hlock_class(this)->usage_mask & new_mask))
4398 goto unlock;
Peter Zijlstra8e182572007-07-19 01:48:54 -07004399
Peter Zijlstra1a393402020-10-27 13:48:34 +01004400 if (!hlock_class(this)->usage_mask)
4401 debug_atomic_dec(nr_unused_locks);
4402
Dave Jonesf82b2172008-08-11 09:30:23 +02004403 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07004404
Peter Zijlstra2bb89452020-09-30 11:49:37 +02004405 if (new_bit < LOCK_TRACE_STATES) {
4406 if (!(hlock_class(this)->usage_traces[new_bit] = save_trace()))
Peter Zijlstra8e182572007-07-19 01:48:54 -07004407 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07004408 }
4409
Peter Zijlstra1a393402020-10-27 13:48:34 +01004410 if (new_bit < LOCK_USED) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07004411 ret = mark_lock_irq(curr, this, new_bit);
4412 if (!ret)
4413 return 0;
4414 }
4415
peterz@infradead.org23870f12020-09-02 18:03:23 +02004416unlock:
Peter Zijlstra8e182572007-07-19 01:48:54 -07004417 graph_unlock();
4418
4419 /*
4420 * We must printk outside of the graph_lock:
4421 */
4422 if (ret == 2) {
4423 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
4424 print_lock(this);
4425 print_irqtrace_events(curr);
4426 dump_stack();
4427 }
4428
4429 return ret;
4430}
4431
Peter Zijlstra9a019db2020-03-31 20:38:12 +02004432static inline short task_wait_context(struct task_struct *curr)
4433{
4434 /*
4435 * Set appropriate wait type for the context; for IRQs we have to take
4436 * into account force_irqthread as that is implied by PREEMPT_RT.
4437 */
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004438 if (lockdep_hardirq_context()) {
Peter Zijlstra9a019db2020-03-31 20:38:12 +02004439 /*
4440 * Check if force_irqthreads will run us threaded.
4441 */
4442 if (curr->hardirq_threaded || curr->irq_config)
4443 return LD_WAIT_CONFIG;
4444
4445 return LD_WAIT_SPIN;
4446 } else if (curr->softirq_context) {
4447 /*
4448 * Softirqs are always threaded.
4449 */
4450 return LD_WAIT_CONFIG;
4451 }
4452
4453 return LD_WAIT_MAX;
4454}
4455
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004456static int
4457print_lock_invalid_wait_context(struct task_struct *curr,
4458 struct held_lock *hlock)
4459{
Peter Zijlstra9a019db2020-03-31 20:38:12 +02004460 short curr_inner;
4461
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004462 if (!debug_locks_off())
4463 return 0;
4464 if (debug_locks_silent)
4465 return 0;
4466
4467 pr_warn("\n");
4468 pr_warn("=============================\n");
4469 pr_warn("[ BUG: Invalid wait context ]\n");
4470 print_kernel_ident();
4471 pr_warn("-----------------------------\n");
4472
4473 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
4474 print_lock(hlock);
4475
4476 pr_warn("other info that might help us debug this:\n");
Peter Zijlstra9a019db2020-03-31 20:38:12 +02004477
4478 curr_inner = task_wait_context(curr);
4479 pr_warn("context-{%d:%d}\n", curr_inner, curr_inner);
4480
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004481 lockdep_print_held_locks(curr);
4482
4483 pr_warn("stack backtrace:\n");
4484 dump_stack();
4485
4486 return 0;
4487}
4488
4489/*
4490 * Verify the wait_type context.
4491 *
4492 * This check validates we takes locks in the right wait-type order; that is it
4493 * ensures that we do not take mutexes inside spinlocks and do not attempt to
4494 * acquire spinlocks inside raw_spinlocks and the sort.
4495 *
4496 * The entire thing is slightly more complex because of RCU, RCU is a lock that
4497 * can be taken from (pretty much) any context but also has constraints.
4498 * However when taken in a stricter environment the RCU lock does not loosen
4499 * the constraints.
4500 *
4501 * Therefore we must look for the strictest environment in the lock stack and
4502 * compare that to the lock we're trying to acquire.
4503 */
4504static int check_wait_context(struct task_struct *curr, struct held_lock *next)
4505{
4506 short next_inner = hlock_class(next)->wait_type_inner;
4507 short next_outer = hlock_class(next)->wait_type_outer;
4508 short curr_inner;
4509 int depth;
4510
4511 if (!curr->lockdep_depth || !next_inner || next->trylock)
4512 return 0;
4513
4514 if (!next_outer)
4515 next_outer = next_inner;
4516
4517 /*
4518 * Find start of current irq_context..
4519 */
4520 for (depth = curr->lockdep_depth - 1; depth >= 0; depth--) {
4521 struct held_lock *prev = curr->held_locks + depth;
4522 if (prev->irq_context != next->irq_context)
4523 break;
4524 }
4525 depth++;
4526
Peter Zijlstra9a019db2020-03-31 20:38:12 +02004527 curr_inner = task_wait_context(curr);
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004528
4529 for (; depth < curr->lockdep_depth; depth++) {
4530 struct held_lock *prev = curr->held_locks + depth;
4531 short prev_inner = hlock_class(prev)->wait_type_inner;
4532
4533 if (prev_inner) {
4534 /*
4535 * We can have a bigger inner than a previous one
4536 * when outer is smaller than inner, as with RCU.
4537 *
4538 * Also due to trylocks.
4539 */
4540 curr_inner = min(curr_inner, prev_inner);
4541 }
4542 }
4543
4544 if (next_outer > curr_inner)
4545 return print_lock_invalid_wait_context(curr, next);
4546
4547 return 0;
4548}
4549
Arnd Bergmann30a35f72019-06-28 12:29:03 +02004550#else /* CONFIG_PROVE_LOCKING */
Arnd Bergmann886532a2019-06-17 14:47:05 +02004551
4552static inline int
4553mark_usage(struct task_struct *curr, struct held_lock *hlock, int check)
4554{
4555 return 1;
4556}
4557
4558static inline unsigned int task_irq_context(struct task_struct *task)
4559{
4560 return 0;
4561}
4562
4563static inline int separate_irq_context(struct task_struct *curr,
4564 struct held_lock *hlock)
4565{
4566 return 0;
4567}
4568
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004569static inline int check_wait_context(struct task_struct *curr,
4570 struct held_lock *next)
4571{
4572 return 0;
4573}
4574
Arnd Bergmann30a35f72019-06-28 12:29:03 +02004575#endif /* CONFIG_PROVE_LOCKING */
Arnd Bergmann886532a2019-06-17 14:47:05 +02004576
Peter Zijlstra8e182572007-07-19 01:48:54 -07004577/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004578 * Initialize a lock instance's lock-class mapping info:
4579 */
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004580void lockdep_init_map_waits(struct lockdep_map *lock, const char *name,
4581 struct lock_class_key *key, int subclass,
4582 short inner, short outer)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004583{
Yong Zhangd3d03d42011-11-09 16:04:51 +08004584 int i;
4585
Yong Zhangd3d03d42011-11-09 16:04:51 +08004586 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
4587 lock->class_cache[i] = NULL;
Hitoshi Mitake62016252010-10-05 18:01:51 +09004588
Peter Zijlstrac8a25002009-04-17 09:40:49 +02004589#ifdef CONFIG_LOCK_STAT
4590 lock->cpu = raw_smp_processor_id();
4591#endif
4592
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004593 /*
4594 * Can't be having no nameless bastards around this place!
4595 */
Peter Zijlstrac8a25002009-04-17 09:40:49 +02004596 if (DEBUG_LOCKS_WARN_ON(!name)) {
4597 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004598 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02004599 }
4600
4601 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004602
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004603 lock->wait_type_outer = outer;
4604 lock->wait_type_inner = inner;
4605
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004606 /*
4607 * No key, no joy, we need to hash something.
4608 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004609 if (DEBUG_LOCKS_WARN_ON(!key))
4610 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004611 /*
Bart Van Assche108c1482019-02-14 15:00:53 -08004612 * Sanity check, the lock-class key must either have been allocated
4613 * statically or must have been registered as a dynamic key.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004614 */
Bart Van Assche108c1482019-02-14 15:00:53 -08004615 if (!static_obj(key) && !is_dynamic_key(key)) {
4616 if (debug_locks)
4617 printk(KERN_ERR "BUG: key %px has not been registered!\n", key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004618 DEBUG_LOCKS_WARN_ON(1);
4619 return;
4620 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004621 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02004622
4623 if (unlikely(!debug_locks))
4624 return;
4625
Peter Zijlstra35a93932015-02-26 16:23:11 +01004626 if (subclass) {
4627 unsigned long flags;
4628
Peter Zijlstra4d004092020-10-02 11:04:21 +02004629 if (DEBUG_LOCKS_WARN_ON(!lockdep_enabled()))
Peter Zijlstra35a93932015-02-26 16:23:11 +01004630 return;
4631
4632 raw_local_irq_save(flags);
Peter Zijlstra4d004092020-10-02 11:04:21 +02004633 lockdep_recursion_inc();
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04004634 register_lock_class(lock, subclass, 1);
Peter Zijlstra10476e62020-03-13 09:56:38 +01004635 lockdep_recursion_finish();
Peter Zijlstra35a93932015-02-26 16:23:11 +01004636 raw_local_irq_restore(flags);
4637 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004638}
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004639EXPORT_SYMBOL_GPL(lockdep_init_map_waits);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004640
Peter Zijlstra1704f472010-03-19 01:37:42 +01004641struct lock_class_key __lockdep_no_validate__;
Kent Overstreetea6749c2012-12-27 22:21:58 -08004642EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
Peter Zijlstra1704f472010-03-19 01:37:42 +01004643
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004644static void
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004645print_lock_nested_lock_not_held(struct task_struct *curr,
4646 struct held_lock *hlock,
4647 unsigned long ip)
4648{
4649 if (!debug_locks_off())
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004650 return;
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004651 if (debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004652 return;
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004653
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004654 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004655 pr_warn("==================================\n");
4656 pr_warn("WARNING: Nested lock was not taken\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004657 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004658 pr_warn("----------------------------------\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004659
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004660 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004661 print_lock(hlock);
4662
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004663 pr_warn("\nbut this task is not holding:\n");
4664 pr_warn("%s\n", hlock->nest_lock->name);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004665
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004666 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004667 dump_stack();
4668
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004669 pr_warn("\nother info that might help us debug this:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004670 lockdep_print_held_locks(curr);
4671
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004672 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004673 dump_stack();
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004674}
4675
Matthew Wilcox08f36ff2018-01-17 07:14:13 -08004676static int __lock_is_held(const struct lockdep_map *lock, int read);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004677
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004678/*
4679 * This gets called for every mutex_lock*()/spin_lock*() operation.
4680 * We maintain the dependency maps and validate the locking attempt:
Waiman Long8ee10862018-10-02 16:19:17 -04004681 *
4682 * The callers must make sure that IRQs are disabled before calling it,
4683 * otherwise we could get an interrupt which would want to take locks,
4684 * which would end up in lockdep again.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004685 */
4686static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
4687 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004688 struct lockdep_map *nest_lock, unsigned long ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02004689 int references, int pin_count)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004690{
4691 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07004692 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004693 struct held_lock *hlock;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01004694 unsigned int depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004695 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004696 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004697 u64 chain_key;
4698
4699 if (unlikely(!debug_locks))
4700 return 0;
4701
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01004702 if (!prove_locking || lock->key == &__lockdep_no_validate__)
4703 check = 0;
Peter Zijlstra1704f472010-03-19 01:37:42 +01004704
Hitoshi Mitake62016252010-10-05 18:01:51 +09004705 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
4706 class = lock->class_cache[subclass];
Ingo Molnard6d897c2006-07-10 04:44:04 -07004707 /*
Hitoshi Mitake62016252010-10-05 18:01:51 +09004708 * Not cached?
Ingo Molnard6d897c2006-07-10 04:44:04 -07004709 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004710 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04004711 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004712 if (!class)
4713 return 0;
4714 }
Waiman Long8ca2b56c2018-10-03 13:07:18 -04004715
4716 debug_class_ops_inc(class);
4717
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004718 if (very_verbose(class)) {
Borislav Petkov04860d42018-02-26 14:49:26 +01004719 printk("\nacquire class [%px] %s", class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004720 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01004721 printk(KERN_CONT "#%d", class->name_version);
4722 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004723 dump_stack();
4724 }
4725
4726 /*
4727 * Add the lock to the list of currently held locks.
4728 * (we dont increase the depth just yet, up until the
4729 * dependency checks are done)
4730 */
4731 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004732 /*
4733 * Ran out of static storage for our per-task lock stack again have we?
4734 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004735 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
4736 return 0;
4737
Yuyang Du01bb6f02019-05-06 16:19:25 +08004738 class_idx = class - lock_classes;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004739
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004740 if (depth) { /* we're holding locks */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004741 hlock = curr->held_locks + depth - 1;
4742 if (hlock->class_idx == class_idx && nest_lock) {
Imre Deakd9349852019-05-24 23:15:09 +03004743 if (!references)
4744 references++;
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01004745
Imre Deakd9349852019-05-24 23:15:09 +03004746 if (!hlock->references)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004747 hlock->references++;
Imre Deakd9349852019-05-24 23:15:09 +03004748
4749 hlock->references += references;
4750
4751 /* Overflow */
4752 if (DEBUG_LOCKS_WARN_ON(hlock->references < references))
4753 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004754
Imre Deak8c8889d2019-05-24 23:15:08 +03004755 return 2;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004756 }
4757 }
4758
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004759 hlock = curr->held_locks + depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004760 /*
4761 * Plain impossible, we just registered it and checked it weren't no
4762 * NULL like.. I bet this mushroom I ate was good!
4763 */
Dave Jonesf82b2172008-08-11 09:30:23 +02004764 if (DEBUG_LOCKS_WARN_ON(!class))
4765 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004766 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004767 hlock->acquire_ip = ip;
4768 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02004769 hlock->nest_lock = nest_lock;
Boqun Fengc2469752016-02-16 13:57:40 +08004770 hlock->irq_context = task_irq_context(curr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004771 hlock->trylock = trylock;
4772 hlock->read = read;
4773 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04004774 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004775 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004776#ifdef CONFIG_LOCK_STAT
4777 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004778 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004779#endif
Peter Zijlstra21199f22015-09-16 16:10:40 +02004780 hlock->pin_count = pin_count;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004781
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004782 if (check_wait_context(curr, hlock))
4783 return 0;
4784
Yuyang Du09180652019-05-06 16:19:37 +08004785 /* Initialize the lock usage bit */
4786 if (!mark_usage(curr, hlock, check))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004787 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07004788
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004789 /*
Gautham R Shenoy17aacfb92007-10-28 20:47:01 +01004790 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004791 * lock keys along the dependency chain. We save the hash value
4792 * at every step so that we can get the current hash easily
4793 * after unlock. The chain hash is then used to cache dependency
4794 * results.
4795 *
4796 * The 'key ID' is what is the most compact key value to drive
4797 * the hash, not class->key.
4798 */
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004799 /*
Yuyang Du01bb6f02019-05-06 16:19:25 +08004800 * Whoops, we did it again.. class_idx is invalid.
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004801 */
Yuyang Du01bb6f02019-05-06 16:19:25 +08004802 if (DEBUG_LOCKS_WARN_ON(!test_bit(class_idx, lock_classes_in_use)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004803 return 0;
4804
4805 chain_key = curr->curr_chain_key;
4806 if (!depth) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004807 /*
4808 * How can we have a chain hash when we ain't got no keys?!
4809 */
Yuyang Duf6ec8822019-05-06 16:19:24 +08004810 if (DEBUG_LOCKS_WARN_ON(chain_key != INITIAL_CHAIN_KEY))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004811 return 0;
4812 chain_head = 1;
4813 }
4814
4815 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07004816 if (separate_irq_context(curr, hlock)) {
Yuyang Duf6ec8822019-05-06 16:19:24 +08004817 chain_key = INITIAL_CHAIN_KEY;
Peter Zijlstra8e182572007-07-19 01:48:54 -07004818 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004819 }
Boqun Fengf611e8c2020-08-07 15:42:33 +08004820 chain_key = iterate_chain_key(chain_key, hlock_id(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004821
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004822 if (nest_lock && !__lock_is_held(nest_lock, -1)) {
4823 print_lock_nested_lock_not_held(curr, hlock, ip);
4824 return 0;
4825 }
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004826
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08004827 if (!debug_locks_silent) {
4828 WARN_ON_ONCE(depth && !hlock_class(hlock - 1)->key);
4829 WARN_ON_ONCE(!hlock_class(hlock)->key);
4830 }
4831
Yuyang Du0b9fc8e2019-05-06 16:19:26 +08004832 if (!validate_chain(curr, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07004833 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08004834
Gregory Haskins3aa416b2007-10-11 22:11:11 +02004835 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004836 curr->lockdep_depth++;
4837 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08004838#ifdef CONFIG_DEBUG_LOCKDEP
4839 if (unlikely(!debug_locks))
4840 return 0;
4841#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004842 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
4843 debug_locks_off();
Dave Jones2c522832013-04-25 13:40:02 -04004844 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
4845 printk(KERN_DEBUG "depth: %i max: %lu!\n",
Ben Greearc0540602013-02-06 10:56:19 -08004846 curr->lockdep_depth, MAX_LOCK_DEPTH);
Ben Greearc0540602013-02-06 10:56:19 -08004847
4848 lockdep_print_held_locks(current);
4849 debug_show_all_locks();
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01004850 dump_stack();
Ben Greearc0540602013-02-06 10:56:19 -08004851
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004852 return 0;
4853 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08004854
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004855 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
4856 max_lockdep_depth = curr->lockdep_depth;
4857
4858 return 1;
4859}
4860
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004861static void print_unlock_imbalance_bug(struct task_struct *curr,
4862 struct lockdep_map *lock,
4863 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004864{
4865 if (!debug_locks_off())
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004866 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004867 if (debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004868 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004869
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004870 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004871 pr_warn("=====================================\n");
4872 pr_warn("WARNING: bad unlock balance detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01004873 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004874 pr_warn("-------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004875 pr_warn("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004876 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004877 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004878 pr_cont(") at:\n");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07004879 print_ip_sym(KERN_WARNING, ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004880 pr_warn("but there are no more locks to release!\n");
4881 pr_warn("\nother info that might help us debug this:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004882 lockdep_print_held_locks(curr);
4883
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004884 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004885 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004886}
4887
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004888static noinstr int match_held_lock(const struct held_lock *hlock,
4889 const struct lockdep_map *lock)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004890{
4891 if (hlock->instance == lock)
4892 return 1;
4893
4894 if (hlock->references) {
Matthew Wilcox08f36ff2018-01-17 07:14:13 -08004895 const struct lock_class *class = lock->class_cache[0];
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004896
4897 if (!class)
4898 class = look_up_lock_class(lock, 0);
4899
Peter Zijlstra80e04012011-08-05 14:26:17 +02004900 /*
4901 * If look_up_lock_class() failed to find a class, we're trying
4902 * to test if we hold a lock that has never yet been acquired.
4903 * Clearly if the lock hasn't been acquired _ever_, we're not
4904 * holding it either, so report failure.
4905 */
Matthew Wilcox64f29d12018-01-17 07:14:12 -08004906 if (!class)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004907 return 0;
4908
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004909 /*
4910 * References, but not a lock we're actually ref-counting?
4911 * State got messed up, follow the sites that change ->references
4912 * and try to make sense of it.
4913 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004914 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
4915 return 0;
4916
Yuyang Du01bb6f02019-05-06 16:19:25 +08004917 if (hlock->class_idx == class - lock_classes)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004918 return 1;
4919 }
4920
4921 return 0;
4922}
4923
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004924/* @depth must not be zero */
4925static struct held_lock *find_held_lock(struct task_struct *curr,
4926 struct lockdep_map *lock,
4927 unsigned int depth, int *idx)
4928{
4929 struct held_lock *ret, *hlock, *prev_hlock;
4930 int i;
4931
4932 i = depth - 1;
4933 hlock = curr->held_locks + i;
4934 ret = hlock;
4935 if (match_held_lock(hlock, lock))
4936 goto out;
4937
4938 ret = NULL;
4939 for (i--, prev_hlock = hlock--;
4940 i >= 0;
4941 i--, prev_hlock = hlock--) {
4942 /*
4943 * We must not cross into another context:
4944 */
4945 if (prev_hlock->irq_context != hlock->irq_context) {
4946 ret = NULL;
4947 break;
4948 }
4949 if (match_held_lock(hlock, lock)) {
4950 ret = hlock;
4951 break;
4952 }
4953 }
4954
4955out:
4956 *idx = i;
4957 return ret;
4958}
4959
J. R. Okajimae9699702017-02-03 01:38:16 +09004960static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
Imre Deak8c8889d2019-05-24 23:15:08 +03004961 int idx, unsigned int *merged)
J. R. Okajimae9699702017-02-03 01:38:16 +09004962{
4963 struct held_lock *hlock;
Imre Deak8c8889d2019-05-24 23:15:08 +03004964 int first_idx = idx;
J. R. Okajimae9699702017-02-03 01:38:16 +09004965
Waiman Long8ee10862018-10-02 16:19:17 -04004966 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4967 return 0;
4968
J. R. Okajimae9699702017-02-03 01:38:16 +09004969 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
Imre Deak8c8889d2019-05-24 23:15:08 +03004970 switch (__lock_acquire(hlock->instance,
J. R. Okajimae9699702017-02-03 01:38:16 +09004971 hlock_class(hlock)->subclass,
4972 hlock->trylock,
4973 hlock->read, hlock->check,
4974 hlock->hardirqs_off,
4975 hlock->nest_lock, hlock->acquire_ip,
Imre Deak8c8889d2019-05-24 23:15:08 +03004976 hlock->references, hlock->pin_count)) {
4977 case 0:
J. R. Okajimae9699702017-02-03 01:38:16 +09004978 return 1;
Imre Deak8c8889d2019-05-24 23:15:08 +03004979 case 1:
4980 break;
4981 case 2:
4982 *merged += (idx == first_idx);
4983 break;
4984 default:
4985 WARN_ON(1);
4986 return 0;
4987 }
J. R. Okajimae9699702017-02-03 01:38:16 +09004988 }
4989 return 0;
4990}
4991
Peter Zijlstra64aa3482008-08-11 09:30:21 +02004992static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01004993__lock_set_class(struct lockdep_map *lock, const char *name,
4994 struct lock_class_key *key, unsigned int subclass,
4995 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02004996{
4997 struct task_struct *curr = current;
Imre Deak8c8889d2019-05-24 23:15:08 +03004998 unsigned int depth, merged = 0;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09004999 struct held_lock *hlock;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005000 struct lock_class *class;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005001 int i;
5002
Waiman Long513e1072019-01-09 23:03:25 -05005003 if (unlikely(!debug_locks))
5004 return 0;
5005
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005006 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005007 /*
5008 * This function is about (re)setting the class of a held lock,
5009 * yet we're not actually holding any locks. Naughty user!
5010 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005011 if (DEBUG_LOCKS_WARN_ON(!depth))
5012 return 0;
5013
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005014 hlock = find_held_lock(curr, lock, depth, &i);
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005015 if (!hlock) {
5016 print_unlock_imbalance_bug(curr, lock, ip);
5017 return 0;
5018 }
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005019
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01005020 lockdep_init_map_waits(lock, name, key, 0,
5021 lock->wait_type_inner,
5022 lock->wait_type_outer);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005023 class = register_lock_class(lock, subclass, 0);
Yuyang Du01bb6f02019-05-06 16:19:25 +08005024 hlock->class_idx = class - lock_classes;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005025
5026 curr->lockdep_depth = i;
5027 curr->curr_chain_key = hlock->prev_chain_key;
5028
Imre Deak8c8889d2019-05-24 23:15:08 +03005029 if (reacquire_held_locks(curr, depth, i, &merged))
J. R. Okajimae9699702017-02-03 01:38:16 +09005030 return 0;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005031
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005032 /*
5033 * I took it apart and put it back together again, except now I have
5034 * these 'spare' parts.. where shall I put them.
5035 */
Imre Deak8c8889d2019-05-24 23:15:08 +03005036 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005037 return 0;
5038 return 1;
5039}
5040
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005041static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
5042{
5043 struct task_struct *curr = current;
Imre Deak8c8889d2019-05-24 23:15:08 +03005044 unsigned int depth, merged = 0;
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005045 struct held_lock *hlock;
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005046 int i;
5047
Waiman Long71492582019-01-09 23:03:25 -05005048 if (unlikely(!debug_locks))
5049 return 0;
5050
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005051 depth = curr->lockdep_depth;
5052 /*
5053 * This function is about (re)setting the class of a held lock,
5054 * yet we're not actually holding any locks. Naughty user!
5055 */
5056 if (DEBUG_LOCKS_WARN_ON(!depth))
5057 return 0;
5058
5059 hlock = find_held_lock(curr, lock, depth, &i);
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005060 if (!hlock) {
5061 print_unlock_imbalance_bug(curr, lock, ip);
5062 return 0;
5063 }
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005064
5065 curr->lockdep_depth = i;
5066 curr->curr_chain_key = hlock->prev_chain_key;
5067
5068 WARN(hlock->read, "downgrading a read lock");
5069 hlock->read = 1;
5070 hlock->acquire_ip = ip;
5071
Imre Deak8c8889d2019-05-24 23:15:08 +03005072 if (reacquire_held_locks(curr, depth, i, &merged))
5073 return 0;
5074
5075 /* Merging can't happen with unchanged classes.. */
5076 if (DEBUG_LOCKS_WARN_ON(merged))
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005077 return 0;
5078
5079 /*
5080 * I took it apart and put it back together again, except now I have
5081 * these 'spare' parts.. where shall I put them.
5082 */
5083 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
5084 return 0;
Imre Deak8c8889d2019-05-24 23:15:08 +03005085
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005086 return 1;
5087}
5088
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005089/*
Dan Carpenterc759bc42019-11-04 12:12:52 +03005090 * Remove the lock from the list of currently held locks - this gets
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02005091 * called on mutex_unlock()/spin_unlock*() (or on a failed
5092 * mutex_lock_interruptible()).
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005093 */
5094static int
Yuyang Dub4adfe8e2019-05-06 16:19:34 +08005095__lock_release(struct lockdep_map *lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005096{
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02005097 struct task_struct *curr = current;
Imre Deak8c8889d2019-05-24 23:15:08 +03005098 unsigned int depth, merged = 1;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005099 struct held_lock *hlock;
Ingo Molnare966eae2017-12-12 12:31:16 +01005100 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005101
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02005102 if (unlikely(!debug_locks))
5103 return 0;
5104
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005105 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005106 /*
5107 * So we're all set to release this lock.. wait what lock? We don't
5108 * own any locks, you've been drinking again?
5109 */
Kobe Wudd471ef2019-05-30 19:59:35 +08005110 if (depth <= 0) {
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005111 print_unlock_imbalance_bug(curr, lock, ip);
5112 return 0;
5113 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005114
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02005115 /*
5116 * Check whether the lock exists in the current stack
5117 * of held locks:
5118 */
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005119 hlock = find_held_lock(curr, lock, depth, &i);
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005120 if (!hlock) {
5121 print_unlock_imbalance_bug(curr, lock, ip);
5122 return 0;
5123 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005124
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005125 if (hlock->instance == lock)
5126 lock_release_holdtime(hlock);
5127
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005128 WARN(hlock->pin_count, "releasing a pinned lock\n");
5129
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005130 if (hlock->references) {
5131 hlock->references--;
5132 if (hlock->references) {
5133 /*
5134 * We had, and after removing one, still have
5135 * references, the current lock stack is still
5136 * valid. We're done!
5137 */
5138 return 1;
5139 }
5140 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005141
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005142 /*
5143 * We have the right lock to unlock, 'hlock' points to it.
5144 * Now we remove it from the stack, and add back the other
5145 * entries (if any), recalculating the hash along the way:
5146 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005147
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005148 curr->lockdep_depth = i;
5149 curr->curr_chain_key = hlock->prev_chain_key;
5150
Waiman Longce52a182018-10-02 16:19:18 -04005151 /*
5152 * The most likely case is when the unlock is on the innermost
5153 * lock. In this case, we are done!
5154 */
5155 if (i == depth-1)
5156 return 1;
5157
Imre Deak8c8889d2019-05-24 23:15:08 +03005158 if (reacquire_held_locks(curr, depth, i + 1, &merged))
J. R. Okajimae9699702017-02-03 01:38:16 +09005159 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005160
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005161 /*
5162 * We had N bottles of beer on the wall, we drank one, but now
5163 * there's not N-1 bottles of beer left on the wall...
Imre Deak8c8889d2019-05-24 23:15:08 +03005164 * Pouring two of the bottles together is acceptable.
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005165 */
Imre Deak8c8889d2019-05-24 23:15:08 +03005166 DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged);
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02005167
Waiman Longce52a182018-10-02 16:19:18 -04005168 /*
5169 * Since reacquire_held_locks() would have called check_chain_key()
5170 * indirectly via __lock_acquire(), we don't need to do it again
5171 * on return.
5172 */
5173 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005174}
5175
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01005176static __always_inline
Masami Hiramatsu2f43c602019-02-13 01:15:05 +09005177int __lock_is_held(const struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02005178{
5179 struct task_struct *curr = current;
5180 int i;
5181
5182 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005183 struct held_lock *hlock = curr->held_locks + i;
5184
Peter Zijlstraf8319482016-11-30 14:32:25 +11005185 if (match_held_lock(hlock, lock)) {
5186 if (read == -1 || hlock->read == read)
5187 return 1;
5188
5189 return 0;
5190 }
Peter Zijlstraf607c662009-07-20 19:16:29 +02005191 }
5192
5193 return 0;
5194}
5195
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005196static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
5197{
5198 struct pin_cookie cookie = NIL_COOKIE;
5199 struct task_struct *curr = current;
5200 int i;
5201
5202 if (unlikely(!debug_locks))
5203 return cookie;
5204
5205 for (i = 0; i < curr->lockdep_depth; i++) {
5206 struct held_lock *hlock = curr->held_locks + i;
5207
5208 if (match_held_lock(hlock, lock)) {
5209 /*
5210 * Grab 16bits of randomness; this is sufficient to not
5211 * be guessable and still allows some pin nesting in
5212 * our u32 pin_count.
5213 */
5214 cookie.val = 1 + (prandom_u32() >> 16);
5215 hlock->pin_count += cookie.val;
5216 return cookie;
5217 }
5218 }
5219
5220 WARN(1, "pinning an unheld lock\n");
5221 return cookie;
5222}
5223
5224static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005225{
5226 struct task_struct *curr = current;
5227 int i;
5228
5229 if (unlikely(!debug_locks))
5230 return;
5231
5232 for (i = 0; i < curr->lockdep_depth; i++) {
5233 struct held_lock *hlock = curr->held_locks + i;
5234
5235 if (match_held_lock(hlock, lock)) {
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005236 hlock->pin_count += cookie.val;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005237 return;
5238 }
5239 }
5240
5241 WARN(1, "pinning an unheld lock\n");
5242}
5243
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005244static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005245{
5246 struct task_struct *curr = current;
5247 int i;
5248
5249 if (unlikely(!debug_locks))
5250 return;
5251
5252 for (i = 0; i < curr->lockdep_depth; i++) {
5253 struct held_lock *hlock = curr->held_locks + i;
5254
5255 if (match_held_lock(hlock, lock)) {
5256 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
5257 return;
5258
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005259 hlock->pin_count -= cookie.val;
5260
5261 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
5262 hlock->pin_count = 0;
5263
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005264 return;
5265 }
5266 }
5267
5268 WARN(1, "unpinning an unheld lock\n");
5269}
5270
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005271/*
5272 * Check whether we follow the irq-flags state precisely:
5273 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02005274static void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005275{
Arnd Bergmann30a35f72019-06-28 12:29:03 +02005276#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005277 if (!debug_locks)
5278 return;
5279
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01005280 if (irqs_disabled_flags(flags)) {
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02005281 if (DEBUG_LOCKS_WARN_ON(lockdep_hardirqs_enabled())) {
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01005282 printk("possible reason: unannotated irqs-off.\n");
5283 }
5284 } else {
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02005285 if (DEBUG_LOCKS_WARN_ON(!lockdep_hardirqs_enabled())) {
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01005286 printk("possible reason: unannotated irqs-on.\n");
5287 }
5288 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005289
5290 /*
5291 * We dont accurately track softirq state in e.g.
5292 * hardirq contexts (such as on 4KSTACKS), so only
5293 * check if not in hardirq contexts:
5294 */
5295 if (!hardirq_count()) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005296 if (softirq_count()) {
5297 /* like the above, but with softirqs */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005298 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005299 } else {
5300 /* lick the above, does it taste good? */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005301 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005302 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005303 }
5304
5305 if (!debug_locks)
5306 print_irqtrace_events(current);
5307#endif
5308}
5309
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01005310void lock_set_class(struct lockdep_map *lock, const char *name,
5311 struct lock_class_key *key, unsigned int subclass,
5312 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005313{
5314 unsigned long flags;
5315
Peter Zijlstra4d004092020-10-02 11:04:21 +02005316 if (unlikely(!lockdep_enabled()))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005317 return;
5318
5319 raw_local_irq_save(flags);
Peter Zijlstra4d004092020-10-02 11:04:21 +02005320 lockdep_recursion_inc();
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005321 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01005322 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005323 check_chain_key(current);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005324 lockdep_recursion_finish();
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005325 raw_local_irq_restore(flags);
5326}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01005327EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005328
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005329void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
5330{
5331 unsigned long flags;
5332
Peter Zijlstra4d004092020-10-02 11:04:21 +02005333 if (unlikely(!lockdep_enabled()))
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005334 return;
5335
5336 raw_local_irq_save(flags);
Peter Zijlstra4d004092020-10-02 11:04:21 +02005337 lockdep_recursion_inc();
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005338 check_flags(flags);
5339 if (__lock_downgrade(lock, ip))
5340 check_chain_key(current);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005341 lockdep_recursion_finish();
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005342 raw_local_irq_restore(flags);
5343}
5344EXPORT_SYMBOL_GPL(lock_downgrade);
5345
Peter Zijlstraf6f48e12020-02-20 09:45:02 +01005346/* NMI context !!! */
5347static void verify_lock_unused(struct lockdep_map *lock, struct held_lock *hlock, int subclass)
5348{
5349#ifdef CONFIG_PROVE_LOCKING
5350 struct lock_class *class = look_up_lock_class(lock, subclass);
peterz@infradead.org23870f12020-09-02 18:03:23 +02005351 unsigned long mask = LOCKF_USED;
Peter Zijlstraf6f48e12020-02-20 09:45:02 +01005352
5353 /* if it doesn't have a class (yet), it certainly hasn't been used yet */
5354 if (!class)
5355 return;
5356
peterz@infradead.org23870f12020-09-02 18:03:23 +02005357 /*
5358 * READ locks only conflict with USED, such that if we only ever use
5359 * READ locks, there is no deadlock possible -- RCU.
5360 */
5361 if (!hlock->read)
5362 mask |= LOCKF_USED_READ;
5363
5364 if (!(class->usage_mask & mask))
Peter Zijlstraf6f48e12020-02-20 09:45:02 +01005365 return;
5366
5367 hlock->class_idx = class - lock_classes;
5368
5369 print_usage_bug(current, hlock, LOCK_USED, LOCK_USAGE_STATES);
5370#endif
5371}
5372
5373static bool lockdep_nmi(void)
5374{
Peter Zijlstra4d004092020-10-02 11:04:21 +02005375 if (raw_cpu_read(lockdep_recursion))
Peter Zijlstraf6f48e12020-02-20 09:45:02 +01005376 return false;
5377
5378 if (!in_nmi())
5379 return false;
5380
5381 return true;
5382}
5383
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005384/*
Boqun Fenge9181882020-08-07 15:42:20 +08005385 * read_lock() is recursive if:
5386 * 1. We force lockdep think this way in selftests or
5387 * 2. The implementation is not queued read/write lock or
5388 * 3. The locker is at an in_interrupt() context.
5389 */
5390bool read_lock_is_recursive(void)
5391{
5392 return force_read_lock_recursive ||
5393 !IS_ENABLED(CONFIG_QUEUED_RWLOCKS) ||
5394 in_interrupt();
5395}
5396EXPORT_SYMBOL_GPL(read_lock_is_recursive);
5397
5398/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005399 * We are not always called with irqs disabled - do that here,
5400 * and also avoid lockdep recursion:
5401 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02005402void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02005403 int trylock, int read, int check,
5404 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005405{
5406 unsigned long flags;
5407
Peter Zijlstraeb1f0022020-08-07 20:53:16 +02005408 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
5409
Peter Zijlstra4d004092020-10-02 11:04:21 +02005410 if (!debug_locks)
5411 return;
5412
5413 if (unlikely(!lockdep_enabled())) {
Peter Zijlstraf6f48e12020-02-20 09:45:02 +01005414 /* XXX allow trylock from NMI ?!? */
5415 if (lockdep_nmi() && !trylock) {
5416 struct held_lock hlock;
5417
5418 hlock.acquire_ip = ip;
5419 hlock.instance = lock;
5420 hlock.nest_lock = nest_lock;
5421 hlock.irq_context = 2; // XXX
5422 hlock.trylock = trylock;
5423 hlock.read = read;
5424 hlock.check = check;
5425 hlock.hardirqs_off = true;
5426 hlock.references = 0;
5427
5428 verify_lock_unused(lock, &hlock, subclass);
5429 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005430 return;
Peter Zijlstraf6f48e12020-02-20 09:45:02 +01005431 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005432
5433 raw_local_irq_save(flags);
5434 check_flags(flags);
5435
Peter Zijlstra4d004092020-10-02 11:04:21 +02005436 lockdep_recursion_inc();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005437 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra21199f22015-09-16 16:10:40 +02005438 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005439 lockdep_recursion_finish();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005440 raw_local_irq_restore(flags);
5441}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005442EXPORT_SYMBOL_GPL(lock_acquire);
5443
Qian Cai5facae42019-09-19 12:09:40 -04005444void lock_release(struct lockdep_map *lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005445{
5446 unsigned long flags;
5447
Peter Zijlstraeb1f0022020-08-07 20:53:16 +02005448 trace_lock_release(lock, ip);
5449
Peter Zijlstra4d004092020-10-02 11:04:21 +02005450 if (unlikely(!lockdep_enabled()))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005451 return;
5452
5453 raw_local_irq_save(flags);
5454 check_flags(flags);
Peter Zijlstraeb1f0022020-08-07 20:53:16 +02005455
Peter Zijlstra4d004092020-10-02 11:04:21 +02005456 lockdep_recursion_inc();
Yuyang Dub4adfe8e2019-05-06 16:19:34 +08005457 if (__lock_release(lock, ip))
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02005458 check_chain_key(current);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005459 lockdep_recursion_finish();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005460 raw_local_irq_restore(flags);
5461}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005462EXPORT_SYMBOL_GPL(lock_release);
5463
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01005464noinstr int lock_is_held_type(const struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02005465{
5466 unsigned long flags;
5467 int ret = 0;
5468
Peter Zijlstra4d004092020-10-02 11:04:21 +02005469 if (unlikely(!lockdep_enabled()))
Peter Zijlstraf2513cd2011-06-06 12:32:43 +02005470 return 1; /* avoid false negative lockdep_assert_held() */
Peter Zijlstraf607c662009-07-20 19:16:29 +02005471
5472 raw_local_irq_save(flags);
5473 check_flags(flags);
5474
Peter Zijlstra4d004092020-10-02 11:04:21 +02005475 lockdep_recursion_inc();
Peter Zijlstraf8319482016-11-30 14:32:25 +11005476 ret = __lock_is_held(lock, read);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005477 lockdep_recursion_finish();
Peter Zijlstraf607c662009-07-20 19:16:29 +02005478 raw_local_irq_restore(flags);
5479
5480 return ret;
5481}
Peter Zijlstraf8319482016-11-30 14:32:25 +11005482EXPORT_SYMBOL_GPL(lock_is_held_type);
Masami Hiramatsu2f43c602019-02-13 01:15:05 +09005483NOKPROBE_SYMBOL(lock_is_held_type);
Peter Zijlstraf607c662009-07-20 19:16:29 +02005484
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005485struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005486{
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005487 struct pin_cookie cookie = NIL_COOKIE;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005488 unsigned long flags;
5489
Peter Zijlstra4d004092020-10-02 11:04:21 +02005490 if (unlikely(!lockdep_enabled()))
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005491 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005492
5493 raw_local_irq_save(flags);
5494 check_flags(flags);
5495
Peter Zijlstra4d004092020-10-02 11:04:21 +02005496 lockdep_recursion_inc();
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005497 cookie = __lock_pin_lock(lock);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005498 lockdep_recursion_finish();
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005499 raw_local_irq_restore(flags);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005500
5501 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005502}
5503EXPORT_SYMBOL_GPL(lock_pin_lock);
5504
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005505void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005506{
5507 unsigned long flags;
5508
Peter Zijlstra4d004092020-10-02 11:04:21 +02005509 if (unlikely(!lockdep_enabled()))
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005510 return;
5511
5512 raw_local_irq_save(flags);
5513 check_flags(flags);
5514
Peter Zijlstra4d004092020-10-02 11:04:21 +02005515 lockdep_recursion_inc();
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005516 __lock_repin_lock(lock, cookie);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005517 lockdep_recursion_finish();
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005518 raw_local_irq_restore(flags);
5519}
5520EXPORT_SYMBOL_GPL(lock_repin_lock);
5521
5522void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
5523{
5524 unsigned long flags;
5525
Peter Zijlstra4d004092020-10-02 11:04:21 +02005526 if (unlikely(!lockdep_enabled()))
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005527 return;
5528
5529 raw_local_irq_save(flags);
5530 check_flags(flags);
5531
Peter Zijlstra4d004092020-10-02 11:04:21 +02005532 lockdep_recursion_inc();
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005533 __lock_unpin_lock(lock, cookie);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005534 lockdep_recursion_finish();
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005535 raw_local_irq_restore(flags);
5536}
5537EXPORT_SYMBOL_GPL(lock_unpin_lock);
5538
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005539#ifdef CONFIG_LOCK_STAT
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005540static void print_lock_contention_bug(struct task_struct *curr,
5541 struct lockdep_map *lock,
5542 unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005543{
5544 if (!debug_locks_off())
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005545 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005546 if (debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005547 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005548
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005549 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08005550 pr_warn("=================================\n");
5551 pr_warn("WARNING: bad contention detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01005552 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08005553 pr_warn("---------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005554 pr_warn("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005555 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005556 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005557 pr_cont(") at:\n");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07005558 print_ip_sym(KERN_WARNING, ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005559 pr_warn("but there are no locks held!\n");
5560 pr_warn("\nother info that might help us debug this:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005561 lockdep_print_held_locks(curr);
5562
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005563 pr_warn("\nstack backtrace:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005564 dump_stack();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005565}
5566
5567static void
5568__lock_contended(struct lockdep_map *lock, unsigned long ip)
5569{
5570 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005571 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005572 struct lock_class_stats *stats;
5573 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005574 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005575
5576 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005577 /*
5578 * Whee, we contended on this lock, except it seems we're not
5579 * actually trying to acquire anything much at all..
5580 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005581 if (DEBUG_LOCKS_WARN_ON(!depth))
5582 return;
5583
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005584 hlock = find_held_lock(curr, lock, depth, &i);
5585 if (!hlock) {
5586 print_lock_contention_bug(curr, lock, ip);
5587 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005588 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005589
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005590 if (hlock->instance != lock)
5591 return;
5592
Peter Zijlstra3365e7792009-10-09 10:12:41 +02005593 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005594
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005595 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
5596 contending_point = lock_point(hlock_class(hlock)->contending_point,
5597 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005598
Dave Jonesf82b2172008-08-11 09:30:23 +02005599 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005600 if (contention_point < LOCKSTAT_POINTS)
5601 stats->contention_point[contention_point]++;
5602 if (contending_point < LOCKSTAT_POINTS)
5603 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07005604 if (lock->cpu != smp_processor_id())
5605 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005606}
5607
5608static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005609__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005610{
5611 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005612 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005613 struct lock_class_stats *stats;
5614 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02005615 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07005616 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005617
5618 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005619 /*
5620 * Yay, we acquired ownership of this lock we didn't try to
5621 * acquire, how the heck did that happen?
5622 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005623 if (DEBUG_LOCKS_WARN_ON(!depth))
5624 return;
5625
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005626 hlock = find_held_lock(curr, lock, depth, &i);
5627 if (!hlock) {
5628 print_lock_contention_bug(curr, lock, _RET_IP_);
5629 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005630 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005631
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005632 if (hlock->instance != lock)
5633 return;
5634
Peter Zijlstra96645672007-07-19 01:49:00 -07005635 cpu = smp_processor_id();
5636 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02005637 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07005638 waittime = now - hlock->waittime_stamp;
5639 hlock->holdtime_stamp = now;
5640 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005641
Dave Jonesf82b2172008-08-11 09:30:23 +02005642 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07005643 if (waittime) {
5644 if (hlock->read)
5645 lock_time_inc(&stats->read_waittime, waittime);
5646 else
5647 lock_time_inc(&stats->write_waittime, waittime);
5648 }
5649 if (lock->cpu != cpu)
5650 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07005651
5652 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005653 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005654}
5655
5656void lock_contended(struct lockdep_map *lock, unsigned long ip)
5657{
5658 unsigned long flags;
5659
Peter Zijlstraeb1f0022020-08-07 20:53:16 +02005660 trace_lock_acquired(lock, ip);
5661
Peter Zijlstra4d004092020-10-02 11:04:21 +02005662 if (unlikely(!lock_stat || !lockdep_enabled()))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005663 return;
5664
5665 raw_local_irq_save(flags);
5666 check_flags(flags);
Peter Zijlstra4d004092020-10-02 11:04:21 +02005667 lockdep_recursion_inc();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005668 __lock_contended(lock, ip);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005669 lockdep_recursion_finish();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005670 raw_local_irq_restore(flags);
5671}
5672EXPORT_SYMBOL_GPL(lock_contended);
5673
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005674void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005675{
5676 unsigned long flags;
5677
Peter Zijlstraeb1f0022020-08-07 20:53:16 +02005678 trace_lock_contended(lock, ip);
5679
Peter Zijlstra4d004092020-10-02 11:04:21 +02005680 if (unlikely(!lock_stat || !lockdep_enabled()))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005681 return;
5682
5683 raw_local_irq_save(flags);
5684 check_flags(flags);
Peter Zijlstra4d004092020-10-02 11:04:21 +02005685 lockdep_recursion_inc();
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005686 __lock_acquired(lock, ip);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005687 lockdep_recursion_finish();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005688 raw_local_irq_restore(flags);
5689}
5690EXPORT_SYMBOL_GPL(lock_acquired);
5691#endif
5692
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005693/*
5694 * Used by the testsuite, sanitize the validator state
5695 * after a simulated failure:
5696 */
5697
5698void lockdep_reset(void)
5699{
5700 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08005701 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005702
5703 raw_local_irq_save(flags);
Yuyang Due196e472019-05-06 16:19:23 +08005704 lockdep_init_task(current);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005705 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
5706 nr_hardirq_chains = 0;
5707 nr_softirq_chains = 0;
5708 nr_process_chains = 0;
5709 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08005710 for (i = 0; i < CHAINHASH_SIZE; i++)
Andrew Mortona63f38c2016-02-03 13:44:12 -08005711 INIT_HLIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005712 raw_local_irq_restore(flags);
5713}
5714
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005715/* Remove a class from a lock chain. Must be called with the graph lock held. */
Bart Van Asschede4643a2019-02-14 15:00:50 -08005716static void remove_class_from_lock_chain(struct pending_free *pf,
5717 struct lock_chain *chain,
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005718 struct lock_class *class)
5719{
5720#ifdef CONFIG_PROVE_LOCKING
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005721 int i;
5722
5723 for (i = chain->base; i < chain->base + chain->depth; i++) {
Boqun Fengf611e8c2020-08-07 15:42:33 +08005724 if (chain_hlock_class_idx(chain_hlocks[i]) != class - lock_classes)
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005725 continue;
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005726 /*
5727 * Each lock class occurs at most once in a lock chain so once
5728 * we found a match we can break out of this loop.
5729 */
Waiman Long836bd742020-02-06 10:24:06 -05005730 goto free_lock_chain;
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005731 }
5732 /* Since the chain has not been modified, return. */
5733 return;
5734
Waiman Long836bd742020-02-06 10:24:06 -05005735free_lock_chain:
Waiman Long810507f2020-02-06 10:24:08 -05005736 free_chain_hlocks(chain->base, chain->depth);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005737 /* Overwrite the chain key for concurrent RCU readers. */
Waiman Long836bd742020-02-06 10:24:06 -05005738 WRITE_ONCE(chain->chain_key, INITIAL_CHAIN_KEY);
Waiman Longb3b9c182020-02-06 10:24:03 -05005739 dec_chains(chain->irq_context);
5740
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005741 /*
5742 * Note: calling hlist_del_rcu() from inside a
5743 * hlist_for_each_entry_rcu() loop is safe.
5744 */
5745 hlist_del_rcu(&chain->entry);
Bart Van Asschede4643a2019-02-14 15:00:50 -08005746 __set_bit(chain - lock_chains, pf->lock_chains_being_freed);
Waiman Long797b82eb2020-02-06 10:24:07 -05005747 nr_zapped_lock_chains++;
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005748#endif
5749}
5750
5751/* Must be called with the graph lock held. */
Bart Van Asschede4643a2019-02-14 15:00:50 -08005752static void remove_class_from_lock_chains(struct pending_free *pf,
5753 struct lock_class *class)
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005754{
5755 struct lock_chain *chain;
5756 struct hlist_head *head;
5757 int i;
5758
5759 for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
5760 head = chainhash_table + i;
5761 hlist_for_each_entry_rcu(chain, head, entry) {
Bart Van Asschede4643a2019-02-14 15:00:50 -08005762 remove_class_from_lock_chain(pf, chain, class);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005763 }
5764 }
5765}
5766
Bart Van Assche786fa292018-12-06 17:11:36 -08005767/*
5768 * Remove all references to a lock class. The caller must hold the graph lock.
5769 */
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005770static void zap_class(struct pending_free *pf, struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005771{
Bart Van Assche86cffb82019-02-14 15:00:41 -08005772 struct lock_list *entry;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005773 int i;
5774
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005775 WARN_ON_ONCE(!class->key);
5776
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005777 /*
5778 * Remove all dependencies this lock is
5779 * involved in:
5780 */
Bart Van Asscheace35a72019-02-14 15:00:47 -08005781 for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
5782 entry = list_entries + i;
Bart Van Assche86cffb82019-02-14 15:00:41 -08005783 if (entry->class != class && entry->links_to != class)
5784 continue;
Bart Van Asscheace35a72019-02-14 15:00:47 -08005785 __clear_bit(i, list_entries_in_use);
5786 nr_list_entries--;
Bart Van Assche86cffb82019-02-14 15:00:41 -08005787 list_del_rcu(&entry->entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005788 }
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005789 if (list_empty(&class->locks_after) &&
5790 list_empty(&class->locks_before)) {
5791 list_move_tail(&class->lock_entry, &pf->zapped);
5792 hlist_del_rcu(&class->hash_entry);
5793 WRITE_ONCE(class->key, NULL);
5794 WRITE_ONCE(class->name, NULL);
5795 nr_lock_classes--;
Yuyang Du01bb6f02019-05-06 16:19:25 +08005796 __clear_bit(class - lock_classes, lock_classes_in_use);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005797 } else {
5798 WARN_ONCE(true, "%s() failed for class %s\n", __func__,
5799 class->name);
5800 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005801
Bart Van Asschede4643a2019-02-14 15:00:50 -08005802 remove_class_from_lock_chains(pf, class);
Waiman Long1d44bcb2020-02-06 10:24:05 -05005803 nr_zapped_classes++;
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005804}
5805
5806static void reinit_class(struct lock_class *class)
5807{
5808 void *const p = class;
5809 const unsigned int offset = offsetof(struct lock_class, key);
5810
5811 WARN_ON_ONCE(!class->lock_entry.next);
5812 WARN_ON_ONCE(!list_empty(&class->locks_after));
5813 WARN_ON_ONCE(!list_empty(&class->locks_before));
5814 memset(p + offset, 0, sizeof(*class) - offset);
5815 WARN_ON_ONCE(!class->lock_entry.next);
5816 WARN_ON_ONCE(!list_empty(&class->locks_after));
5817 WARN_ON_ONCE(!list_empty(&class->locks_before));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005818}
5819
Arjan van de Venfabe8742008-01-24 07:00:45 +01005820static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005821{
5822 return addr >= start && addr < start + size;
5823}
5824
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005825static bool inside_selftest(void)
5826{
5827 return current == lockdep_selftest_task_struct;
5828}
5829
5830/* The caller must hold the graph lock. */
5831static struct pending_free *get_pending_free(void)
5832{
5833 return delayed_free.pf + delayed_free.index;
5834}
5835
5836static void free_zapped_rcu(struct rcu_head *cb);
5837
5838/*
5839 * Schedule an RCU callback if no RCU callback is pending. Must be called with
5840 * the graph lock held.
5841 */
5842static void call_rcu_zapped(struct pending_free *pf)
5843{
5844 WARN_ON_ONCE(inside_selftest());
5845
5846 if (list_empty(&pf->zapped))
5847 return;
5848
5849 if (delayed_free.scheduled)
5850 return;
5851
5852 delayed_free.scheduled = true;
5853
5854 WARN_ON_ONCE(delayed_free.pf + delayed_free.index != pf);
5855 delayed_free.index ^= 1;
5856
5857 call_rcu(&delayed_free.rcu_head, free_zapped_rcu);
5858}
5859
5860/* The caller must hold the graph lock. May be called from RCU context. */
5861static void __free_zapped_classes(struct pending_free *pf)
5862{
5863 struct lock_class *class;
5864
Peter Zijlstra72dcd502019-02-25 15:56:32 +01005865 check_data_structures();
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08005866
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005867 list_for_each_entry(class, &pf->zapped, lock_entry)
5868 reinit_class(class);
5869
5870 list_splice_init(&pf->zapped, &free_lock_classes);
Bart Van Asschede4643a2019-02-14 15:00:50 -08005871
5872#ifdef CONFIG_PROVE_LOCKING
5873 bitmap_andnot(lock_chains_in_use, lock_chains_in_use,
5874 pf->lock_chains_being_freed, ARRAY_SIZE(lock_chains));
5875 bitmap_clear(pf->lock_chains_being_freed, 0, ARRAY_SIZE(lock_chains));
5876#endif
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005877}
5878
5879static void free_zapped_rcu(struct rcu_head *ch)
5880{
5881 struct pending_free *pf;
5882 unsigned long flags;
5883
5884 if (WARN_ON_ONCE(ch != &delayed_free.rcu_head))
5885 return;
5886
5887 raw_local_irq_save(flags);
Peter Zijlstra248efb22020-03-13 11:09:49 +01005888 lockdep_lock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005889
5890 /* closed head */
5891 pf = delayed_free.pf + (delayed_free.index ^ 1);
5892 __free_zapped_classes(pf);
5893 delayed_free.scheduled = false;
5894
5895 /*
5896 * If there's anything on the open list, close and start a new callback.
5897 */
5898 call_rcu_zapped(delayed_free.pf + delayed_free.index);
5899
Peter Zijlstra248efb22020-03-13 11:09:49 +01005900 lockdep_unlock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005901 raw_local_irq_restore(flags);
5902}
5903
5904/*
5905 * Remove all lock classes from the class hash table and from the
5906 * all_lock_classes list whose key or name is in the address range [start,
5907 * start + size). Move these lock classes to the zapped_classes list. Must
5908 * be called with the graph lock held.
5909 */
5910static void __lockdep_free_key_range(struct pending_free *pf, void *start,
5911 unsigned long size)
Bart Van Assche956f3562019-02-14 15:00:43 -08005912{
5913 struct lock_class *class;
5914 struct hlist_head *head;
5915 int i;
5916
5917 /* Unhash all classes that were created by a module. */
5918 for (i = 0; i < CLASSHASH_SIZE; i++) {
5919 head = classhash_table + i;
5920 hlist_for_each_entry_rcu(class, head, hash_entry) {
5921 if (!within(class->key, start, size) &&
5922 !within(class->name, start, size))
5923 continue;
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005924 zap_class(pf, class);
Bart Van Assche956f3562019-02-14 15:00:43 -08005925 }
5926 }
5927}
5928
Peter Zijlstra35a93932015-02-26 16:23:11 +01005929/*
5930 * Used in module.c to remove lock classes from memory that is going to be
5931 * freed; and possibly re-used by other modules.
5932 *
Bart Van Assche29fc33f2019-02-14 15:00:45 -08005933 * We will have had one synchronize_rcu() before getting here, so we're
5934 * guaranteed nobody will look up these exact classes -- they're properly dead
5935 * but still allocated.
Peter Zijlstra35a93932015-02-26 16:23:11 +01005936 */
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005937static void lockdep_free_key_range_reg(void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005938{
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005939 struct pending_free *pf;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005940 unsigned long flags;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005941
Bart Van Asschefeb0a382019-02-14 15:00:42 -08005942 init_data_structures_once();
5943
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005944 raw_local_irq_save(flags);
Peter Zijlstra248efb22020-03-13 11:09:49 +01005945 lockdep_lock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005946 pf = get_pending_free();
5947 __lockdep_free_key_range(pf, start, size);
5948 call_rcu_zapped(pf);
Peter Zijlstra248efb22020-03-13 11:09:49 +01005949 lockdep_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005950 raw_local_irq_restore(flags);
Peter Zijlstra35a93932015-02-26 16:23:11 +01005951
5952 /*
5953 * Wait for any possible iterators from look_up_lock_class() to pass
5954 * before continuing to free the memory they refer to.
Peter Zijlstra35a93932015-02-26 16:23:11 +01005955 */
Paul E. McKenney51959d82018-11-06 19:06:51 -08005956 synchronize_rcu();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005957}
Peter Zijlstra35a93932015-02-26 16:23:11 +01005958
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005959/*
5960 * Free all lockdep keys in the range [start, start+size). Does not sleep.
5961 * Ignores debug_locks. Must only be used by the lockdep selftests.
5962 */
5963static void lockdep_free_key_range_imm(void *start, unsigned long size)
5964{
5965 struct pending_free *pf = delayed_free.pf;
5966 unsigned long flags;
5967
5968 init_data_structures_once();
5969
5970 raw_local_irq_save(flags);
Peter Zijlstra248efb22020-03-13 11:09:49 +01005971 lockdep_lock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005972 __lockdep_free_key_range(pf, start, size);
5973 __free_zapped_classes(pf);
Peter Zijlstra248efb22020-03-13 11:09:49 +01005974 lockdep_unlock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005975 raw_local_irq_restore(flags);
5976}
5977
5978void lockdep_free_key_range(void *start, unsigned long size)
5979{
5980 init_data_structures_once();
5981
5982 if (inside_selftest())
5983 lockdep_free_key_range_imm(start, size);
5984 else
5985 lockdep_free_key_range_reg(start, size);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005986}
5987
Bart Van Assche2904d9f2018-12-06 17:11:34 -08005988/*
5989 * Check whether any element of the @lock->class_cache[] array refers to a
5990 * registered lock class. The caller must hold either the graph lock or the
5991 * RCU read lock.
5992 */
5993static bool lock_class_cache_is_registered(struct lockdep_map *lock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005994{
Peter Zijlstra35a93932015-02-26 16:23:11 +01005995 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08005996 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005997 int i, j;
Bart Van Assche2904d9f2018-12-06 17:11:34 -08005998
5999 for (i = 0; i < CLASSHASH_SIZE; i++) {
6000 head = classhash_table + i;
6001 hlist_for_each_entry_rcu(class, head, hash_entry) {
6002 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
6003 if (lock->class_cache[j] == class)
6004 return true;
6005 }
6006 }
6007 return false;
6008}
6009
Bart Van Assche956f3562019-02-14 15:00:43 -08006010/* The caller must hold the graph lock. Does not sleep. */
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006011static void __lockdep_reset_lock(struct pending_free *pf,
6012 struct lockdep_map *lock)
Bart Van Assche2904d9f2018-12-06 17:11:34 -08006013{
6014 struct lock_class *class;
Bart Van Assche956f3562019-02-14 15:00:43 -08006015 int j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006016
6017 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07006018 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006019 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07006020 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
6021 /*
6022 * If the class exists we look it up and zap it:
6023 */
6024 class = look_up_lock_class(lock, j);
Matthew Wilcox64f29d12018-01-17 07:14:12 -08006025 if (class)
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006026 zap_class(pf, class);
Ingo Molnard6d897c2006-07-10 04:44:04 -07006027 }
6028 /*
6029 * Debug check: in the end all mapped classes should
6030 * be gone.
6031 */
Bart Van Assche956f3562019-02-14 15:00:43 -08006032 if (WARN_ON_ONCE(lock_class_cache_is_registered(lock)))
6033 debug_locks_off();
6034}
6035
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006036/*
6037 * Remove all information lockdep has about a lock if debug_locks == 1. Free
6038 * released data structures from RCU context.
6039 */
6040static void lockdep_reset_lock_reg(struct lockdep_map *lock)
Bart Van Assche956f3562019-02-14 15:00:43 -08006041{
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006042 struct pending_free *pf;
Bart Van Assche956f3562019-02-14 15:00:43 -08006043 unsigned long flags;
6044 int locked;
6045
Bart Van Assche956f3562019-02-14 15:00:43 -08006046 raw_local_irq_save(flags);
6047 locked = graph_lock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006048 if (!locked)
6049 goto out_irq;
6050
6051 pf = get_pending_free();
6052 __lockdep_reset_lock(pf, lock);
6053 call_rcu_zapped(pf);
6054
6055 graph_unlock();
6056out_irq:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006057 raw_local_irq_restore(flags);
6058}
6059
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006060/*
6061 * Reset a lock. Does not sleep. Ignores debug_locks. Must only be used by the
6062 * lockdep selftests.
6063 */
6064static void lockdep_reset_lock_imm(struct lockdep_map *lock)
6065{
6066 struct pending_free *pf = delayed_free.pf;
6067 unsigned long flags;
6068
6069 raw_local_irq_save(flags);
Peter Zijlstra248efb22020-03-13 11:09:49 +01006070 lockdep_lock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006071 __lockdep_reset_lock(pf, lock);
6072 __free_zapped_classes(pf);
Peter Zijlstra248efb22020-03-13 11:09:49 +01006073 lockdep_unlock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006074 raw_local_irq_restore(flags);
6075}
6076
6077void lockdep_reset_lock(struct lockdep_map *lock)
6078{
6079 init_data_structures_once();
6080
6081 if (inside_selftest())
6082 lockdep_reset_lock_imm(lock);
6083 else
6084 lockdep_reset_lock_reg(lock);
6085}
6086
Bart Van Assche108c1482019-02-14 15:00:53 -08006087/* Unregister a dynamically allocated key. */
6088void lockdep_unregister_key(struct lock_class_key *key)
6089{
6090 struct hlist_head *hash_head = keyhashentry(key);
6091 struct lock_class_key *k;
6092 struct pending_free *pf;
6093 unsigned long flags;
6094 bool found = false;
6095
6096 might_sleep();
6097
6098 if (WARN_ON_ONCE(static_obj(key)))
6099 return;
6100
6101 raw_local_irq_save(flags);
Bart Van Assche8b39adb2019-04-15 10:05:38 -07006102 if (!graph_lock())
6103 goto out_irq;
6104
Bart Van Assche108c1482019-02-14 15:00:53 -08006105 pf = get_pending_free();
6106 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
6107 if (k == key) {
6108 hlist_del_rcu(&k->hash_entry);
6109 found = true;
6110 break;
6111 }
6112 }
6113 WARN_ON_ONCE(!found);
6114 __lockdep_free_key_range(pf, key, 1);
6115 call_rcu_zapped(pf);
Bart Van Assche8b39adb2019-04-15 10:05:38 -07006116 graph_unlock();
6117out_irq:
Bart Van Assche108c1482019-02-14 15:00:53 -08006118 raw_local_irq_restore(flags);
6119
6120 /* Wait until is_dynamic_key() has finished accessing k->hash_entry. */
6121 synchronize_rcu();
6122}
6123EXPORT_SYMBOL_GPL(lockdep_unregister_key);
6124
Joel Fernandes (Google)c3bc8fd2018-07-30 15:24:23 -07006125void __init lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006126{
6127 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
6128
Li Zefanb0788ca2008-11-21 15:57:32 +08006129 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006130 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
6131 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08006132 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006133 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
6134 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
6135 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
6136
Bart Van Assche09d75ec2019-02-14 15:00:36 -08006137 printk(" memory used by lock dependency info: %zu kB\n",
Bart Van Assche7ff85172019-02-14 15:00:37 -08006138 (sizeof(lock_classes) +
Yuyang Du01bb6f02019-05-06 16:19:25 +08006139 sizeof(lock_classes_in_use) +
Bart Van Assche7ff85172019-02-14 15:00:37 -08006140 sizeof(classhash_table) +
6141 sizeof(list_entries) +
Bart Van Asscheace35a72019-02-14 15:00:47 -08006142 sizeof(list_entries_in_use) +
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006143 sizeof(chainhash_table) +
6144 sizeof(delayed_free)
Ming Lei4dd861d2009-07-16 15:44:29 +02006145#ifdef CONFIG_PROVE_LOCKING
Bart Van Assche7ff85172019-02-14 15:00:37 -08006146 + sizeof(lock_cq)
Bart Van Assche15ea86b2019-02-14 15:00:38 -08006147 + sizeof(lock_chains)
Bart Van Asschede4643a2019-02-14 15:00:50 -08006148 + sizeof(lock_chains_in_use)
Bart Van Assche15ea86b2019-02-14 15:00:38 -08006149 + sizeof(chain_hlocks)
Ming Lei4dd861d2009-07-16 15:44:29 +02006150#endif
Ming Lei906292092009-08-02 21:43:36 +08006151 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02006152 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006153
Bart Van Assche12593b72019-07-22 11:24:42 -07006154#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
6155 printk(" memory used for stack traces: %zu kB\n",
6156 (sizeof(stack_trace) + sizeof(stack_trace_hash)) / 1024
6157 );
6158#endif
6159
Bart Van Assche09d75ec2019-02-14 15:00:36 -08006160 printk(" per task-struct memory footprint: %zu bytes\n",
Bart Van Assche7ff85172019-02-14 15:00:37 -08006161 sizeof(((struct task_struct *)NULL)->held_locks));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006162}
6163
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006164static void
6165print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07006166 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006167{
6168 if (!debug_locks_off())
6169 return;
6170 if (debug_locks_silent)
6171 return;
6172
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006173 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006174 pr_warn("=========================\n");
6175 pr_warn("WARNING: held lock freed!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01006176 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006177 pr_warn("-------------------------\n");
Borislav Petkov04860d42018-02-26 14:49:26 +01006178 pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006179 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07006180 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006181 lockdep_print_held_locks(curr);
6182
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006183 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006184 dump_stack();
6185}
6186
Oleg Nesterov54561782007-12-05 15:46:09 +01006187static inline int not_in_range(const void* mem_from, unsigned long mem_len,
6188 const void* lock_from, unsigned long lock_len)
6189{
6190 return lock_from + lock_len <= mem_from ||
6191 mem_from + mem_len <= lock_from;
6192}
6193
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006194/*
6195 * Called when kernel memory is freed (or unmapped), or if a lock
6196 * is destroyed or reinitialized - this code checks whether there is
6197 * any held lock in the memory range of <from> to <to>:
6198 */
6199void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
6200{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006201 struct task_struct *curr = current;
6202 struct held_lock *hlock;
6203 unsigned long flags;
6204 int i;
6205
6206 if (unlikely(!debug_locks))
6207 return;
6208
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04006209 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006210 for (i = 0; i < curr->lockdep_depth; i++) {
6211 hlock = curr->held_locks + i;
6212
Oleg Nesterov54561782007-12-05 15:46:09 +01006213 if (not_in_range(mem_from, mem_len, hlock->instance,
6214 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006215 continue;
6216
Oleg Nesterov54561782007-12-05 15:46:09 +01006217 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006218 break;
6219 }
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04006220 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006221}
Peter Zijlstraed075362006-12-06 20:35:24 -08006222EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006223
Colin Cross1b1d2fb2013-05-06 23:50:08 +00006224static void print_held_locks_bug(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006225{
6226 if (!debug_locks_off())
6227 return;
6228 if (debug_locks_silent)
6229 return;
6230
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006231 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006232 pr_warn("====================================\n");
6233 pr_warn("WARNING: %s/%d still has locks held!\n",
Colin Cross1b1d2fb2013-05-06 23:50:08 +00006234 current->comm, task_pid_nr(current));
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01006235 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006236 pr_warn("------------------------------------\n");
Colin Cross1b1d2fb2013-05-06 23:50:08 +00006237 lockdep_print_held_locks(current);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006238 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006239 dump_stack();
6240}
6241
Colin Cross1b1d2fb2013-05-06 23:50:08 +00006242void debug_check_no_locks_held(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006243{
Colin Cross1b1d2fb2013-05-06 23:50:08 +00006244 if (unlikely(current->lockdep_depth > 0))
6245 print_held_locks_bug();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006246}
Colin Cross1b1d2fb2013-05-06 23:50:08 +00006247EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006248
Sasha Levin8dce7a92013-06-13 18:41:16 -04006249#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006250void debug_show_all_locks(void)
6251{
6252 struct task_struct *g, *p;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006253
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08006254 if (unlikely(!debug_locks)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006255 pr_warn("INFO: lockdep is turned off.\n");
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08006256 return;
6257 }
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006258 pr_warn("\nShowing all locks held in the system:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006259
Tetsuo Handa0f736a52018-04-06 19:41:18 +09006260 rcu_read_lock();
6261 for_each_process_thread(g, p) {
Tetsuo Handa0f736a52018-04-06 19:41:18 +09006262 if (!p->lockdep_depth)
6263 continue;
6264 lockdep_print_held_locks(p);
Tejun Heo88f1c872018-01-22 14:00:55 -08006265 touch_nmi_watchdog();
Tetsuo Handa0f736a52018-04-06 19:41:18 +09006266 touch_all_softlockup_watchdogs();
6267 }
6268 rcu_read_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006269
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006270 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006271 pr_warn("=============================================\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006272}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006273EXPORT_SYMBOL_GPL(debug_show_all_locks);
Sasha Levin8dce7a92013-06-13 18:41:16 -04006274#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006275
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01006276/*
6277 * Careful: only use this function if you are sure that
6278 * the task cannot run in parallel!
6279 */
John Kacurf1b499f2010-08-05 17:10:53 +02006280void debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006281{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08006282 if (unlikely(!debug_locks)) {
6283 printk("INFO: lockdep is turned off.\n");
6284 return;
6285 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006286 lockdep_print_held_locks(task);
6287}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006288EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02006289
Andi Kleen722a9f92014-05-02 00:44:38 +02006290asmlinkage __visible void lockdep_sys_exit(void)
Peter Zijlstrab351d162007-10-11 22:11:12 +02006291{
6292 struct task_struct *curr = current;
6293
6294 if (unlikely(curr->lockdep_depth)) {
6295 if (!debug_locks_off())
6296 return;
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006297 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006298 pr_warn("================================================\n");
6299 pr_warn("WARNING: lock held when returning to user space!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01006300 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006301 pr_warn("------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006302 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
Peter Zijlstrab351d162007-10-11 22:11:12 +02006303 curr->comm, curr->pid);
6304 lockdep_print_held_locks(curr);
6305 }
Byungchul Parkb09be672017-08-07 16:12:52 +09006306
6307 /*
6308 * The lock history for each syscall should be independent. So wipe the
6309 * slate clean on return to userspace.
6310 */
Peter Zijlstraf52be572017-08-29 10:59:39 +02006311 lockdep_invariant_state(false);
Peter Zijlstrab351d162007-10-11 22:11:12 +02006312}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08006313
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07006314void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
Paul E. McKenney0632eb32010-02-22 17:04:47 -08006315{
6316 struct task_struct *curr = current;
6317
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08006318 /* Note: the following can be executed concurrently, so be careful. */
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006319 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006320 pr_warn("=============================\n");
6321 pr_warn("WARNING: suspicious RCU usage\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01006322 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006323 pr_warn("-----------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006324 pr_warn("%s:%d %s!\n", file, line, s);
6325 pr_warn("\nother info that might help us debug this:\n\n");
6326 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08006327 !rcu_lockdep_current_cpu_online()
6328 ? "RCU used illegally from offline CPU!\n"
Paul E. McKenneyd29e0b22020-05-28 08:49:29 -07006329 : "",
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08006330 rcu_scheduler_active, debug_locks);
Frederic Weisbecker0464e932011-10-07 18:22:01 +02006331
6332 /*
6333 * If a CPU is in the RCU-free window in idle (ie: in the section
6334 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
6335 * considers that CPU to be in an "extended quiescent state",
6336 * which means that RCU will be completely ignoring that CPU.
6337 * Therefore, rcu_read_lock() and friends have absolutely no
6338 * effect on a CPU running in that state. In other words, even if
6339 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
6340 * delete data structures out from under it. RCU really has no
6341 * choice here: we need to keep an RCU-free window in idle where
6342 * the CPU may possibly enter into low power mode. This way we can
6343 * notice an extended quiescent state to other CPUs that started a grace
6344 * period. Otherwise we would delay any grace period as long as we run
6345 * in the idle task.
6346 *
6347 * So complain bitterly if someone does call rcu_read_lock(),
6348 * rcu_read_lock_bh() and so on from extended quiescent states.
6349 */
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07006350 if (!rcu_is_watching())
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006351 pr_warn("RCU used illegally from extended quiescent state!\n");
Frederic Weisbecker0464e932011-10-07 18:22:01 +02006352
Paul E. McKenney0632eb32010-02-22 17:04:47 -08006353 lockdep_print_held_locks(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006354 pr_warn("\nstack backtrace:\n");
Paul E. McKenney0632eb32010-02-22 17:04:47 -08006355 dump_stack();
6356}
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07006357EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);