blob: 2270ec68f10a172a182431c5ea36ea44d2f126de [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>
Shuah Khanf8cfa462021-02-26 17:06:59 -070057#include <linux/lockdep.h>
Peter Zijlstraaf012962009-07-16 15:44:29 +020058
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070059#include <asm/sections.h>
60
61#include "lockdep_internals.h"
62
Steven Rostedta8d154b2009-04-10 09:36:00 -040063#define CREATE_TRACE_POINTS
Frederic Weisbecker67178762009-11-13 10:06:34 +010064#include <trace/events/lock.h>
Steven Rostedta8d154b2009-04-10 09:36:00 -040065
Peter Zijlstraf20786f2007-07-19 01:48:56 -070066#ifdef CONFIG_PROVE_LOCKING
67int prove_locking = 1;
68module_param(prove_locking, int, 0644);
69#else
70#define prove_locking 0
71#endif
72
73#ifdef CONFIG_LOCK_STAT
74int lock_stat = 1;
75module_param(lock_stat, int, 0644);
76#else
77#define lock_stat 0
78#endif
79
Peter Zijlstra4d004092020-10-02 11:04:21 +020080DEFINE_PER_CPU(unsigned int, lockdep_recursion);
81EXPORT_PER_CPU_SYMBOL_GPL(lockdep_recursion);
82
Peter Zijlstra0afda3a2021-01-06 15:36:22 +010083static __always_inline bool lockdep_enabled(void)
Peter Zijlstra4d004092020-10-02 11:04:21 +020084{
85 if (!debug_locks)
86 return false;
87
Peter Zijlstrad48e3852020-10-26 16:22:56 +010088 if (this_cpu_read(lockdep_recursion))
Peter Zijlstra4d004092020-10-02 11:04:21 +020089 return false;
90
91 if (current->lockdep_recursion)
92 return false;
93
94 return true;
95}
96
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070097/*
Ingo Molnar74c383f2006-12-13 00:34:43 -080098 * lockdep_lock: protects the lockdep graph, the hashes and the
99 * class/list/hash allocators.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700100 *
101 * This is one of the rare exceptions where it's justified
102 * to use a raw spinlock - we really dont want the spinlock
Ingo Molnar74c383f2006-12-13 00:34:43 -0800103 * code to recurse back into the lockdep code...
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700104 */
Peter Zijlstra248efb22020-03-13 11:09:49 +0100105static arch_spinlock_t __lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
106static struct task_struct *__owner;
107
108static inline void lockdep_lock(void)
109{
110 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
111
Boqun Feng43be4382020-11-13 19:05:03 +0800112 __this_cpu_inc(lockdep_recursion);
Peter Zijlstra248efb22020-03-13 11:09:49 +0100113 arch_spin_lock(&__lock);
114 __owner = current;
Peter Zijlstra248efb22020-03-13 11:09:49 +0100115}
116
117static inline void lockdep_unlock(void)
118{
Boqun Feng43be4382020-11-13 19:05:03 +0800119 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
120
Peter Zijlstra248efb22020-03-13 11:09:49 +0100121 if (debug_locks && DEBUG_LOCKS_WARN_ON(__owner != current))
122 return;
123
Peter Zijlstra248efb22020-03-13 11:09:49 +0100124 __owner = NULL;
125 arch_spin_unlock(&__lock);
Boqun Feng43be4382020-11-13 19:05:03 +0800126 __this_cpu_dec(lockdep_recursion);
Peter Zijlstra248efb22020-03-13 11:09:49 +0100127}
128
129static inline bool lockdep_assert_locked(void)
130{
131 return DEBUG_LOCKS_WARN_ON(__owner != current);
132}
133
Bart Van Asschecdc84d72019-02-14 15:00:44 -0800134static struct task_struct *lockdep_selftest_task_struct;
Ingo Molnar74c383f2006-12-13 00:34:43 -0800135
Peter Zijlstra248efb22020-03-13 11:09:49 +0100136
Ingo Molnar74c383f2006-12-13 00:34:43 -0800137static int graph_lock(void)
138{
Peter Zijlstra248efb22020-03-13 11:09:49 +0100139 lockdep_lock();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800140 /*
141 * Make sure that if another CPU detected a bug while
142 * walking the graph we dont change it (while the other
143 * CPU is busy printing out stuff with the graph lock
144 * dropped already)
145 */
146 if (!debug_locks) {
Peter Zijlstra248efb22020-03-13 11:09:49 +0100147 lockdep_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800148 return 0;
149 }
150 return 1;
151}
152
Peter Zijlstra248efb22020-03-13 11:09:49 +0100153static inline void graph_unlock(void)
Ingo Molnar74c383f2006-12-13 00:34:43 -0800154{
Peter Zijlstra248efb22020-03-13 11:09:49 +0100155 lockdep_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800156}
157
158/*
159 * Turn lock debugging off and return with 0 if it was off already,
160 * and also release the graph lock:
161 */
162static inline int debug_locks_off_graph_unlock(void)
163{
164 int ret = debug_locks_off();
165
Peter Zijlstra248efb22020-03-13 11:09:49 +0100166 lockdep_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -0800167
168 return ret;
169}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700170
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700171unsigned long nr_list_entries;
Peter Zijlstraaf012962009-07-16 15:44:29 +0200172static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
Bart Van Asscheace35a72019-02-14 15:00:47 -0800173static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700174
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700175/*
176 * All data structures here are protected by the global debug_lock.
177 *
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800178 * nr_lock_classes is the number of elements of lock_classes[] that is
179 * in use.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700180 */
Bart Van Assche108c1482019-02-14 15:00:53 -0800181#define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
182#define KEYHASH_SIZE (1UL << KEYHASH_BITS)
183static struct hlist_head lock_keys_hash[KEYHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700184unsigned long nr_lock_classes;
Waiman Long1d44bcb2020-02-06 10:24:05 -0500185unsigned long nr_zapped_classes;
Bart Van Assche1431a5d2018-12-06 17:11:32 -0800186#ifndef CONFIG_DEBUG_LOCKDEP
187static
188#endif
Waiman Long8ca2b56c2018-10-03 13:07:18 -0400189struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
Yuyang Du01bb6f02019-05-06 16:19:25 +0800190static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700191
Dave Jonesf82b2172008-08-11 09:30:23 +0200192static inline struct lock_class *hlock_class(struct held_lock *hlock)
193{
Yuyang Du01bb6f02019-05-06 16:19:25 +0800194 unsigned int class_idx = hlock->class_idx;
195
196 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfield */
197 barrier();
198
199 if (!test_bit(class_idx, lock_classes_in_use)) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200200 /*
201 * Someone passed in garbage, we give up.
202 */
Dave Jonesf82b2172008-08-11 09:30:23 +0200203 DEBUG_LOCKS_WARN_ON(1);
204 return NULL;
205 }
Yuyang Du01bb6f02019-05-06 16:19:25 +0800206
207 /*
208 * At this point, if the passed hlock->class_idx is still garbage,
209 * we just have to live with it
210 */
211 return lock_classes + class_idx;
Dave Jonesf82b2172008-08-11 09:30:23 +0200212}
213
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700214#ifdef CONFIG_LOCK_STAT
Peter Zijlstra25528212016-03-15 14:52:49 -0700215static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700216
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200217static inline u64 lockstat_clock(void)
218{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200219 return local_clock();
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200220}
221
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200222static int lock_point(unsigned long points[], unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700223{
224 int i;
225
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200226 for (i = 0; i < LOCKSTAT_POINTS; i++) {
227 if (points[i] == 0) {
228 points[i] = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700229 break;
230 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200231 if (points[i] == ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700232 break;
233 }
234
235 return i;
236}
237
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200238static void lock_time_inc(struct lock_time *lt, u64 time)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700239{
240 if (time > lt->max)
241 lt->max = time;
242
Frank Rowand109d71c2009-11-19 13:42:06 -0800243 if (time < lt->min || !lt->nr)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700244 lt->min = time;
245
246 lt->total += time;
247 lt->nr++;
248}
249
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700250static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
251{
Frank Rowand109d71c2009-11-19 13:42:06 -0800252 if (!src->nr)
253 return;
254
255 if (src->max > dst->max)
256 dst->max = src->max;
257
258 if (src->min < dst->min || !dst->nr)
259 dst->min = src->min;
260
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700261 dst->total += src->total;
262 dst->nr += src->nr;
263}
264
265struct lock_class_stats lock_stats(struct lock_class *class)
266{
267 struct lock_class_stats stats;
268 int cpu, i;
269
270 memset(&stats, 0, sizeof(struct lock_class_stats));
271 for_each_possible_cpu(cpu) {
272 struct lock_class_stats *pcs =
Tejun Heo1871e522009-10-29 22:34:13 +0900273 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700274
275 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
276 stats.contention_point[i] += pcs->contention_point[i];
277
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200278 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
279 stats.contending_point[i] += pcs->contending_point[i];
280
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700281 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
282 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
283
284 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
285 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700286
287 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
288 stats.bounces[i] += pcs->bounces[i];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700289 }
290
291 return stats;
292}
293
294void clear_lock_stats(struct lock_class *class)
295{
296 int cpu;
297
298 for_each_possible_cpu(cpu) {
299 struct lock_class_stats *cpu_stats =
Tejun Heo1871e522009-10-29 22:34:13 +0900300 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700301
302 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
303 }
304 memset(class->contention_point, 0, sizeof(class->contention_point));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200305 memset(class->contending_point, 0, sizeof(class->contending_point));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700306}
307
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700308static struct lock_class_stats *get_lock_stats(struct lock_class *class)
309{
Joel Fernandes (Google)01f38492018-07-30 15:24:21 -0700310 return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes];
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700311}
312
313static void lock_release_holdtime(struct held_lock *hlock)
314{
315 struct lock_class_stats *stats;
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200316 u64 holdtime;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700317
318 if (!lock_stat)
319 return;
320
Peter Zijlstra3365e7792009-10-09 10:12:41 +0200321 holdtime = lockstat_clock() - hlock->holdtime_stamp;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700322
Dave Jonesf82b2172008-08-11 09:30:23 +0200323 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700324 if (hlock->read)
325 lock_time_inc(&stats->read_holdtime, holdtime);
326 else
327 lock_time_inc(&stats->write_holdtime, holdtime);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700328}
329#else
330static inline void lock_release_holdtime(struct held_lock *hlock)
331{
332}
333#endif
334
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700335/*
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800336 * We keep a global list of all lock classes. The list is only accessed with
337 * the lockdep spinlock lock held. free_lock_classes is a list with free
338 * elements. These elements are linked together by the lock_entry member in
339 * struct lock_class.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700340 */
341LIST_HEAD(all_lock_classes);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800342static LIST_HEAD(free_lock_classes);
343
344/**
345 * struct pending_free - information about data structures about to be freed
346 * @zapped: Head of a list with struct lock_class elements.
Bart Van Asschede4643a2019-02-14 15:00:50 -0800347 * @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements
348 * are about to be freed.
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800349 */
350struct pending_free {
351 struct list_head zapped;
Bart Van Asschede4643a2019-02-14 15:00:50 -0800352 DECLARE_BITMAP(lock_chains_being_freed, MAX_LOCKDEP_CHAINS);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -0800353};
354
355/**
356 * struct delayed_free - data structures used for delayed freeing
357 *
358 * A data structure for delayed freeing of data structures that may be
359 * accessed by RCU readers at the time these were freed.
360 *
361 * @rcu_head: Used to schedule an RCU callback for freeing data structures.
362 * @index: Index of @pf to which freed data structures are added.
363 * @scheduled: Whether or not an RCU callback has been scheduled.
364 * @pf: Array with information about data structures about to be freed.
365 */
366static struct delayed_free {
367 struct rcu_head rcu_head;
368 int index;
369 int scheduled;
370 struct pending_free pf[2];
371} delayed_free;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700372
373/*
374 * The lockdep classes are in a hash-table as well, for fast lookup:
375 */
376#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
377#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700378#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700379#define classhashentry(key) (classhash_table + __classhashfn((key)))
380
Andrew Mortona63f38c2016-02-03 13:44:12 -0800381static struct hlist_head classhash_table[CLASSHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700382
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700383/*
384 * We put the lock dependency chains into a hash-table as well, to cache
385 * their existence:
386 */
387#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
388#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700389#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700390#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
391
Andrew Mortona63f38c2016-02-03 13:44:12 -0800392static struct hlist_head chainhash_table[CHAINHASH_SIZE];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700393
394/*
Boqun Fengf611e8c2020-08-07 15:42:33 +0800395 * the id of held_lock
396 */
397static inline u16 hlock_id(struct held_lock *hlock)
398{
399 BUILD_BUG_ON(MAX_LOCKDEP_KEYS_BITS + 2 > 16);
400
401 return (hlock->class_idx | (hlock->read << MAX_LOCKDEP_KEYS_BITS));
402}
403
404static inline unsigned int chain_hlock_class_idx(u16 hlock_id)
405{
406 return hlock_id & (MAX_LOCKDEP_KEYS - 1);
407}
408
409/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700410 * The hash key of the lock dependency chains is a hash itself too:
411 * it's a hash of all locks taken up to that lock, including that lock.
412 * It's a 64-bit hash, because it's important for the keys to be
413 * unique.
414 */
Peter Zijlstradfaaf3f2016-05-30 18:31:33 +0200415static inline u64 iterate_chain_key(u64 key, u32 idx)
416{
417 u32 k0 = key, k1 = key >> 32;
418
419 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
420
421 return k0 | (u64)k1 << 32;
422}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700423
Yuyang Due196e472019-05-06 16:19:23 +0800424void lockdep_init_task(struct task_struct *task)
425{
426 task->lockdep_depth = 0; /* no locks held yet */
Yuyang Duf6ec8822019-05-06 16:19:24 +0800427 task->curr_chain_key = INITIAL_CHAIN_KEY;
Yuyang Due196e472019-05-06 16:19:23 +0800428 task->lockdep_recursion = 0;
429}
430
Peter Zijlstra4d004092020-10-02 11:04:21 +0200431static __always_inline void lockdep_recursion_inc(void)
432{
433 __this_cpu_inc(lockdep_recursion);
434}
435
Peter Zijlstra6eebad12020-06-03 13:40:21 +0200436static __always_inline void lockdep_recursion_finish(void)
Peter Zijlstra10476e62020-03-13 09:56:38 +0100437{
Peter Zijlstra4d004092020-10-02 11:04:21 +0200438 if (WARN_ON_ONCE(__this_cpu_dec_return(lockdep_recursion)))
439 __this_cpu_write(lockdep_recursion, 0);
Peter Zijlstra10476e62020-03-13 09:56:38 +0100440}
441
Bart Van Asschecdc84d72019-02-14 15:00:44 -0800442void lockdep_set_selftest_task(struct task_struct *task)
443{
444 lockdep_selftest_task_struct = task;
445}
446
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700447/*
448 * Debugging switches:
449 */
450
451#define VERBOSE 0
Ingo Molnar33e94e92006-12-13 00:34:41 -0800452#define VERY_VERBOSE 0
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700453
454#if VERBOSE
455# define HARDIRQ_VERBOSE 1
456# define SOFTIRQ_VERBOSE 1
457#else
458# define HARDIRQ_VERBOSE 0
459# define SOFTIRQ_VERBOSE 0
460#endif
461
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +0100462#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700463/*
464 * Quick filtering for interesting events:
465 */
466static int class_filter(struct lock_class *class)
467{
Andi Kleenf9829cc2006-07-10 04:44:01 -0700468#if 0
469 /* Example */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700470 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700471 !strcmp(class->name, "lockname"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700472 return 1;
473 if (class->name_version == 1 &&
Andi Kleenf9829cc2006-07-10 04:44:01 -0700474 !strcmp(class->name, "&struct->lockfield"))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700475 return 1;
Andi Kleenf9829cc2006-07-10 04:44:01 -0700476#endif
Ingo Molnara6640892006-12-13 00:34:39 -0800477 /* Filter everything else. 1 would be to allow everything else */
478 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700479}
480#endif
481
482static int verbose(struct lock_class *class)
483{
484#if VERBOSE
485 return class_filter(class);
486#endif
487 return 0;
488}
489
Dave Jones2c522832013-04-25 13:40:02 -0400490static void print_lockdep_off(const char *bug_msg)
491{
492 printk(KERN_DEBUG "%s\n", bug_msg);
493 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200494#ifdef CONFIG_LOCK_STAT
Dave Jones2c522832013-04-25 13:40:02 -0400495 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
Andreas Gruenbacheracf59372014-07-15 21:10:52 +0200496#endif
Dave Jones2c522832013-04-25 13:40:02 -0400497}
498
Arnd Bergmann886532a2019-06-17 14:47:05 +0200499unsigned long nr_stack_trace_entries;
500
Arnd Bergmann30a35f72019-06-28 12:29:03 +0200501#ifdef CONFIG_PROVE_LOCKING
Bart Van Assche12593b72019-07-22 11:24:42 -0700502/**
503 * struct lock_trace - single stack backtrace
504 * @hash_entry: Entry in a stack_trace_hash[] list.
505 * @hash: jhash() of @entries.
506 * @nr_entries: Number of entries in @entries.
507 * @entries: Actual stack backtrace.
508 */
509struct lock_trace {
510 struct hlist_node hash_entry;
511 u32 hash;
512 u32 nr_entries;
Gustavo A. R. Silvadb785382020-05-07 13:58:04 -0500513 unsigned long entries[] __aligned(sizeof(unsigned long));
Bart Van Assche12593b72019-07-22 11:24:42 -0700514};
515#define LOCK_TRACE_SIZE_IN_LONGS \
516 (sizeof(struct lock_trace) / sizeof(unsigned long))
Arnd Bergmann886532a2019-06-17 14:47:05 +0200517/*
Bart Van Assche12593b72019-07-22 11:24:42 -0700518 * Stack-trace: sequence of lock_trace structures. Protected by the graph_lock.
Arnd Bergmann886532a2019-06-17 14:47:05 +0200519 */
520static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
Bart Van Assche12593b72019-07-22 11:24:42 -0700521static struct hlist_head stack_trace_hash[STACK_TRACE_HASH_SIZE];
Arnd Bergmann886532a2019-06-17 14:47:05 +0200522
Bart Van Assche12593b72019-07-22 11:24:42 -0700523static bool traces_identical(struct lock_trace *t1, struct lock_trace *t2)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700524{
Bart Van Assche12593b72019-07-22 11:24:42 -0700525 return t1->hash == t2->hash && t1->nr_entries == t2->nr_entries &&
526 memcmp(t1->entries, t2->entries,
527 t1->nr_entries * sizeof(t1->entries[0])) == 0;
528}
529
530static struct lock_trace *save_trace(void)
531{
532 struct lock_trace *trace, *t2;
533 struct hlist_head *hash_head;
534 u32 hash;
Waiman Longd91f3052019-12-20 08:51:28 -0500535 int max_entries;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700536
Bart Van Assche12593b72019-07-22 11:24:42 -0700537 BUILD_BUG_ON_NOT_POWER_OF_2(STACK_TRACE_HASH_SIZE);
538 BUILD_BUG_ON(LOCK_TRACE_SIZE_IN_LONGS >= MAX_STACK_TRACE_ENTRIES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700539
Bart Van Assche12593b72019-07-22 11:24:42 -0700540 trace = (struct lock_trace *)(stack_trace + nr_stack_trace_entries);
541 max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries -
542 LOCK_TRACE_SIZE_IN_LONGS;
Bart Van Assche12593b72019-07-22 11:24:42 -0700543
Waiman Longd91f3052019-12-20 08:51:28 -0500544 if (max_entries <= 0) {
Ingo Molnar74c383f2006-12-13 00:34:43 -0800545 if (!debug_locks_off_graph_unlock())
Bart Van Assche12593b72019-07-22 11:24:42 -0700546 return NULL;
Ingo Molnar74c383f2006-12-13 00:34:43 -0800547
Dave Jones2c522832013-04-25 13:40:02 -0400548 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
Ingo Molnar74c383f2006-12-13 00:34:43 -0800549 dump_stack();
550
Bart Van Assche12593b72019-07-22 11:24:42 -0700551 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700552 }
Waiman Longd91f3052019-12-20 08:51:28 -0500553 trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700554
Bart Van Assche12593b72019-07-22 11:24:42 -0700555 hash = jhash(trace->entries, trace->nr_entries *
556 sizeof(trace->entries[0]), 0);
557 trace->hash = hash;
558 hash_head = stack_trace_hash + (hash & (STACK_TRACE_HASH_SIZE - 1));
559 hlist_for_each_entry(t2, hash_head, hash_entry) {
560 if (traces_identical(trace, t2))
561 return t2;
562 }
563 nr_stack_trace_entries += LOCK_TRACE_SIZE_IN_LONGS + trace->nr_entries;
564 hlist_add_head(&trace->hash_entry, hash_head);
565
566 return trace;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700567}
Bart Van Assche8c779222019-07-22 11:24:43 -0700568
569/* Return the number of stack traces in the stack_trace[] array. */
570u64 lockdep_stack_trace_count(void)
571{
572 struct lock_trace *trace;
573 u64 c = 0;
574 int i;
575
576 for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++) {
577 hlist_for_each_entry(trace, &stack_trace_hash[i], hash_entry) {
578 c++;
579 }
580 }
581
582 return c;
583}
584
585/* Return the number of stack hash chains that have at least one stack trace. */
586u64 lockdep_stack_hash_count(void)
587{
588 u64 c = 0;
589 int i;
590
591 for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++)
592 if (!hlist_empty(&stack_trace_hash[i]))
593 c++;
594
595 return c;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700596}
Arnd Bergmann886532a2019-06-17 14:47:05 +0200597#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700598
599unsigned int nr_hardirq_chains;
600unsigned int nr_softirq_chains;
601unsigned int nr_process_chains;
602unsigned int max_lockdep_depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700603
604#ifdef CONFIG_DEBUG_LOCKDEP
605/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700606 * Various lockdep statistics:
607 */
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200608DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700609#endif
610
Arnd Bergmann30a35f72019-06-28 12:29:03 +0200611#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700612/*
613 * Locking printouts:
614 */
615
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100616#define __USAGE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +0100617 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
618 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
619 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
620 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100621
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700622static const char *usage_str[] =
623{
Peter Zijlstrafabe9c42009-01-22 14:51:01 +0100624#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
625#include "lockdep_states.h"
626#undef LOCKDEP_STATE
627 [LOCK_USED] = "INITIAL USE",
Peter Zijlstra2bb89452020-09-30 11:49:37 +0200628 [LOCK_USED_READ] = "INITIAL READ USE",
629 /* abused as string storage for verify_lock_unused() */
Peter Zijlstraf6f48e12020-02-20 09:45:02 +0100630 [LOCK_USAGE_STATES] = "IN-NMI",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700631};
Arnd Bergmann886532a2019-06-17 14:47:05 +0200632#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700633
Bart Van Assche364f6af2019-07-22 11:24:40 -0700634const char *__get_key_name(const struct lockdep_subclass_key *key, char *str)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700635{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700636 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700637}
638
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100639static inline unsigned long lock_flag(enum lock_usage_bit bit)
640{
641 return 1UL << bit;
642}
643
644static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
645{
Yuyang Duc52478f2019-05-06 16:19:19 +0800646 /*
647 * The usage character defaults to '.' (i.e., irqs disabled and not in
648 * irq context), which is the safest usage category.
649 */
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100650 char c = '.';
651
Yuyang Duc52478f2019-05-06 16:19:19 +0800652 /*
653 * The order of the following usage checks matters, which will
654 * result in the outcome character as follows:
655 *
656 * - '+': irq is enabled and not in irq context
657 * - '-': in irq context and irq is disabled
658 * - '?': in irq context and irq is enabled
659 */
660 if (class->usage_mask & lock_flag(bit + LOCK_USAGE_DIR_MASK)) {
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100661 c = '+';
Yuyang Duc52478f2019-05-06 16:19:19 +0800662 if (class->usage_mask & lock_flag(bit))
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100663 c = '?';
Yuyang Duc52478f2019-05-06 16:19:19 +0800664 } else if (class->usage_mask & lock_flag(bit))
665 c = '-';
Peter Zijlstra3ff176c2009-01-22 17:40:42 +0100666
667 return c;
668}
669
Peter Zijlstraf510b232009-01-22 17:53:47 +0100670void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700671{
Peter Zijlstraf510b232009-01-22 17:53:47 +0100672 int i = 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700673
Peter Zijlstraf510b232009-01-22 17:53:47 +0100674#define LOCKDEP_STATE(__STATE) \
675 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
676 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
677#include "lockdep_states.h"
678#undef LOCKDEP_STATE
679
680 usage[i] = '\0';
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700681}
682
Steven Rostedte5e78d02011-11-02 20:24:16 -0400683static void __print_lock_name(struct lock_class *class)
Steven Rostedt3003eba2011-04-20 21:41:54 -0400684{
685 char str[KSYM_NAME_LEN];
686 const char *name;
687
688 name = class->name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700689 if (!name) {
690 name = __get_key_name(class->key, str);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100691 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700692 } else {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100693 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700694 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100695 printk(KERN_CONT "#%d", class->name_version);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700696 if (class->subclass)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100697 printk(KERN_CONT "/%d", class->subclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700698 }
Steven Rostedte5e78d02011-11-02 20:24:16 -0400699}
700
701static void print_lock_name(struct lock_class *class)
702{
703 char usage[LOCK_USAGE_CHARS];
704
705 get_usage_chars(class, usage);
706
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100707 printk(KERN_CONT " (");
Steven Rostedte5e78d02011-11-02 20:24:16 -0400708 __print_lock_name(class);
Arnd Bergmann6d48b792021-03-22 12:55:25 +0100709 printk(KERN_CONT "){%s}-{%d:%d}", usage,
Peter Zijlstrade8f5e42020-03-21 12:26:01 +0100710 class->wait_type_outer ?: class->wait_type_inner,
711 class->wait_type_inner);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700712}
713
714static void print_lockdep_cache(struct lockdep_map *lock)
715{
716 const char *name;
Tejun Heo9281ace2007-07-17 04:03:51 -0700717 char str[KSYM_NAME_LEN];
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700718
719 name = lock->name;
720 if (!name)
721 name = __get_key_name(lock->key->subkeys, str);
722
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100723 printk(KERN_CONT "%s", name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700724}
725
726static void print_lock(struct held_lock *hlock)
727{
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200728 /*
729 * We can be called locklessly through debug_show_all_locks() so be
730 * extra careful, the hlock might have been released and cleared.
Yuyang Du01bb6f02019-05-06 16:19:25 +0800731 *
732 * If this indeed happens, lets pretend it does not hurt to continue
733 * to print the lock unless the hlock class_idx does not point to a
734 * registered class. The rationale here is: since we don't attempt
735 * to distinguish whether we are in this situation, if it just
736 * happened we can't count on class_idx to tell either.
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200737 */
Yuyang Du01bb6f02019-05-06 16:19:25 +0800738 struct lock_class *lock = hlock_class(hlock);
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200739
Yuyang Du01bb6f02019-05-06 16:19:25 +0800740 if (!lock) {
Dmitry Vyukovf943fe02016-11-28 15:24:43 +0100741 printk(KERN_CONT "<RELEASED>\n");
Peter Zijlstrad7bc3192015-04-15 17:11:57 +0200742 return;
743 }
744
Paul E. McKenney519248f2019-05-30 05:39:25 -0700745 printk(KERN_CONT "%px", hlock->instance);
Yuyang Du01bb6f02019-05-06 16:19:25 +0800746 print_lock_name(lock);
Tetsuo Handab3c39752018-03-27 19:41:41 +0900747 printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700748}
749
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900750static void lockdep_print_held_locks(struct task_struct *p)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700751{
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900752 int i, depth = READ_ONCE(p->lockdep_depth);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700753
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900754 if (!depth)
755 printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p));
756 else
757 printk("%d lock%s held by %s/%d:\n", depth,
758 depth > 1 ? "s" : "", p->comm, task_pid_nr(p));
759 /*
760 * It's not reliable to print a task's held locks if it's not sleeping
761 * and it's not the current task.
762 */
Peter Zijlstrab03fbd42021-06-11 10:28:12 +0200763 if (p != current && task_is_running(p))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700764 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700765 for (i = 0; i < depth; i++) {
766 printk(" #%d: ", i);
Tetsuo Handa8cc05c712018-04-06 19:41:19 +0900767 print_lock(p->held_locks + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700768 }
769}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700770
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100771static void print_kernel_ident(void)
Dave Jones99de0552006-09-29 02:00:10 -0700772{
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100773 printk("%s %.*s %s\n", init_utsname()->release,
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700774 (int)strcspn(init_utsname()->version, " "),
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100775 init_utsname()->version,
776 print_tainted());
Dave Jones99de0552006-09-29 02:00:10 -0700777}
778
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700779static int very_verbose(struct lock_class *class)
780{
781#if VERY_VERBOSE
782 return class_filter(class);
783#endif
784 return 0;
785}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700786
787/*
788 * Is this the address of a static object:
789 */
Sasha Levin8dce7a92013-06-13 18:41:16 -0400790#ifdef __KERNEL__
Christophe Leroye5ae3722021-11-05 13:40:43 -0700791/*
792 * Check if an address is part of freed initmem. After initmem is freed,
793 * memory can be allocated from it, and such allocations would then have
794 * addresses within the range [_stext, _end].
795 */
796#ifndef arch_is_kernel_initmem_freed
797static int arch_is_kernel_initmem_freed(unsigned long addr)
798{
799 if (system_state < SYSTEM_FREEING_INITMEM)
800 return 0;
801
802 return init_section_contains((void *)addr, 1);
803}
804#endif
805
Bart Van Assche108c1482019-02-14 15:00:53 -0800806static int static_obj(const void *obj)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700807{
808 unsigned long start = (unsigned long) &_stext,
809 end = (unsigned long) &_end,
810 addr = (unsigned long) obj;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700811
Gerald Schaefer7a5da022019-04-18 16:24:50 +0200812 if (arch_is_kernel_initmem_freed(addr))
813 return 0;
814
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700815 /*
816 * static variable?
817 */
818 if ((addr >= start) && (addr < end))
819 return 1;
820
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700821 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900822 * in-kernel percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700823 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900824 if (is_kernel_percpu_address(addr))
825 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700826
827 /*
Tejun Heo10fad5e2010-03-10 18:57:54 +0900828 * module static or percpu var?
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700829 */
Tejun Heo10fad5e2010-03-10 18:57:54 +0900830 return is_module_address(addr) || is_module_percpu_address(addr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700831}
Sasha Levin8dce7a92013-06-13 18:41:16 -0400832#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700833
834/*
835 * To make lock name printouts unique, we calculate a unique
Bart Van Asschefe27b0d2018-12-06 17:11:37 -0800836 * class->name_version generation counter. The caller must hold the graph
837 * lock.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700838 */
839static int count_matching_names(struct lock_class *new_class)
840{
841 struct lock_class *class;
842 int count = 0;
843
844 if (!new_class->name)
845 return 0;
846
Bart Van Asschefe27b0d2018-12-06 17:11:37 -0800847 list_for_each_entry(class, &all_lock_classes, lock_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700848 if (new_class->key - new_class->subclass == class->key)
849 return class->name_version;
850 if (class->name && !strcmp(class->name, new_class->name))
851 count = max(count, class->name_version);
852 }
853
854 return count + 1;
855}
856
Peter Zijlstraf6f48e12020-02-20 09:45:02 +0100857/* used from NMI context -- must be lockless */
Peter Zijlstra49faa772021-06-21 13:12:38 +0200858static noinstr struct lock_class *
Matthew Wilcox08f36ff2018-01-17 07:14:13 -0800859look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700860{
861 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -0800862 struct hlist_head *hash_head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700863 struct lock_class *class;
864
Hitoshi Mitake4ba053c2010-10-13 17:30:26 +0900865 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
Peter Zijlstra49faa772021-06-21 13:12:38 +0200866 instrumentation_begin();
Hitoshi Mitake4ba053c2010-10-13 17:30:26 +0900867 debug_locks_off();
868 printk(KERN_ERR
869 "BUG: looking up invalid subclass: %u\n", subclass);
870 printk(KERN_ERR
871 "turning off the locking correctness validator.\n");
872 dump_stack();
Peter Zijlstra49faa772021-06-21 13:12:38 +0200873 instrumentation_end();
Hitoshi Mitake4ba053c2010-10-13 17:30:26 +0900874 return NULL;
875 }
876
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700877 /*
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800878 * If it is not initialised then it has never been locked,
879 * so it won't be present in the hash table.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700880 */
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800881 if (unlikely(!lock->key))
882 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700883
884 /*
885 * NOTE: the class-key must be unique. For dynamic locks, a static
886 * lock_class_key variable is passed in through the mutex_init()
887 * (or spin_lock_init()) call - which acts as the key. For static
888 * locks we use the lock object itself as the key.
889 */
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700890 BUILD_BUG_ON(sizeof(struct lock_class_key) >
891 sizeof(struct lockdep_map));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700892
893 key = lock->key->subkeys + subclass;
894
895 hash_head = classhashentry(key);
896
897 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +0100898 * We do an RCU walk of the hash, see lockdep_free_key_range().
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700899 */
Peter Zijlstra35a93932015-02-26 16:23:11 +0100900 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
901 return NULL;
902
Peter Zijlstrace0b9c82021-06-24 11:41:10 +0200903 hlist_for_each_entry_rcu_notrace(class, hash_head, hash_entry) {
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700904 if (class->key == key) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +0200905 /*
906 * Huh! same key, different name? Did someone trample
907 * on some memory? We're most confused.
908 */
Sebastian Andrzej Siewior97831542019-05-17 23:22:34 +0200909 WARN_ON_ONCE(class->name != lock->name &&
910 lock->key != &__lockdep_no_validate__);
Ingo Molnard6d897c2006-07-10 04:44:04 -0700911 return class;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700912 }
913 }
Ingo Molnard6d897c2006-07-10 04:44:04 -0700914
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800915 return NULL;
916}
917
918/*
919 * Static locks do not have their class-keys yet - for them the key is
920 * the lock object itself. If the lock is in the per cpu area, the
921 * canonical address of the lock (per cpu offset removed) is used.
922 */
923static bool assign_lock_key(struct lockdep_map *lock)
924{
925 unsigned long can_addr, addr = (unsigned long)lock;
926
Bart Van Assche4bf50862019-02-14 15:00:52 -0800927#ifdef __KERNEL__
928 /*
929 * lockdep_free_key_range() assumes that struct lock_class_key
930 * objects do not overlap. Since we use the address of lock
931 * objects as class key for static objects, check whether the
932 * size of lock_class_key objects does not exceed the size of
933 * the smallest lock object.
934 */
935 BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(raw_spinlock_t));
936#endif
937
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800938 if (__is_kernel_percpu_address(addr, &can_addr))
939 lock->key = (void *)can_addr;
940 else if (__is_module_percpu_address(addr, &can_addr))
941 lock->key = (void *)can_addr;
942 else if (static_obj(lock))
943 lock->key = (void *)lock;
944 else {
945 /* Debug-check: all keys must be persistent! */
946 debug_locks_off();
947 pr_err("INFO: trying to register non-static key.\n");
Tetsuo Handa3a859692021-03-21 15:49:13 +0900948 pr_err("The code is fine but needs lockdep annotation, or maybe\n");
949 pr_err("you didn't initialize this object before use?\n");
Matthew Wilcox64f29d12018-01-17 07:14:12 -0800950 pr_err("turning off the locking correctness validator.\n");
951 dump_stack();
952 return false;
953 }
954
955 return true;
Ingo Molnard6d897c2006-07-10 04:44:04 -0700956}
957
Peter Zijlstra72dcd502019-02-25 15:56:32 +0100958#ifdef CONFIG_DEBUG_LOCKDEP
959
Bart Van Asscheb526b2e2019-02-14 15:00:51 -0800960/* Check whether element @e occurs in list @h */
961static bool in_list(struct list_head *e, struct list_head *h)
962{
963 struct list_head *f;
964
965 list_for_each(f, h) {
966 if (e == f)
967 return true;
968 }
969
970 return false;
971}
972
973/*
974 * Check whether entry @e occurs in any of the locks_after or locks_before
975 * lists.
976 */
977static bool in_any_class_list(struct list_head *e)
978{
979 struct lock_class *class;
980 int i;
981
982 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
983 class = &lock_classes[i];
984 if (in_list(e, &class->locks_after) ||
985 in_list(e, &class->locks_before))
986 return true;
987 }
988 return false;
989}
990
991static bool class_lock_list_valid(struct lock_class *c, struct list_head *h)
992{
993 struct lock_list *e;
994
995 list_for_each_entry(e, h, entry) {
996 if (e->links_to != c) {
997 printk(KERN_INFO "class %s: mismatch for lock entry %ld; class %s <> %s",
998 c->name ? : "(?)",
999 (unsigned long)(e - list_entries),
1000 e->links_to && e->links_to->name ?
1001 e->links_to->name : "(?)",
1002 e->class && e->class->name ? e->class->name :
1003 "(?)");
1004 return false;
1005 }
1006 }
1007 return true;
1008}
1009
Arnd Bergmann3fe75222019-03-07 08:52:12 +01001010#ifdef CONFIG_PROVE_LOCKING
1011static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1012#endif
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001013
1014static bool check_lock_chain_key(struct lock_chain *chain)
1015{
1016#ifdef CONFIG_PROVE_LOCKING
Yuyang Duf6ec8822019-05-06 16:19:24 +08001017 u64 chain_key = INITIAL_CHAIN_KEY;
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001018 int i;
1019
1020 for (i = chain->base; i < chain->base + chain->depth; i++)
Yuyang Du01bb6f02019-05-06 16:19:25 +08001021 chain_key = iterate_chain_key(chain_key, chain_hlocks[i]);
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001022 /*
1023 * The 'unsigned long long' casts avoid that a compiler warning
1024 * is reported when building tools/lib/lockdep.
1025 */
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001026 if (chain->chain_key != chain_key) {
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001027 printk(KERN_INFO "chain %lld: key %#llx <> %#llx\n",
1028 (unsigned long long)(chain - lock_chains),
1029 (unsigned long long)chain->chain_key,
1030 (unsigned long long)chain_key);
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001031 return false;
1032 }
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001033#endif
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001034 return true;
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001035}
1036
1037static bool in_any_zapped_class_list(struct lock_class *class)
1038{
1039 struct pending_free *pf;
1040 int i;
1041
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001042 for (i = 0, pf = delayed_free.pf; i < ARRAY_SIZE(delayed_free.pf); i++, pf++) {
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001043 if (in_list(&class->lock_entry, &pf->zapped))
1044 return true;
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001045 }
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001046
1047 return false;
1048}
1049
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001050static bool __check_data_structures(void)
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001051{
1052 struct lock_class *class;
1053 struct lock_chain *chain;
1054 struct hlist_head *head;
1055 struct lock_list *e;
1056 int i;
1057
1058 /* Check whether all classes occur in a lock list. */
1059 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
1060 class = &lock_classes[i];
1061 if (!in_list(&class->lock_entry, &all_lock_classes) &&
1062 !in_list(&class->lock_entry, &free_lock_classes) &&
1063 !in_any_zapped_class_list(class)) {
1064 printk(KERN_INFO "class %px/%s is not in any class list\n",
1065 class, class->name ? : "(?)");
1066 return false;
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08001067 }
1068 }
1069
1070 /* Check whether all classes have valid lock lists. */
1071 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
1072 class = &lock_classes[i];
1073 if (!class_lock_list_valid(class, &class->locks_before))
1074 return false;
1075 if (!class_lock_list_valid(class, &class->locks_after))
1076 return false;
1077 }
1078
1079 /* Check the chain_key of all lock chains. */
1080 for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
1081 head = chainhash_table + i;
1082 hlist_for_each_entry_rcu(chain, head, entry) {
1083 if (!check_lock_chain_key(chain))
1084 return false;
1085 }
1086 }
1087
1088 /*
1089 * Check whether all list entries that are in use occur in a class
1090 * lock list.
1091 */
1092 for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
1093 e = list_entries + i;
1094 if (!in_any_class_list(&e->entry)) {
1095 printk(KERN_INFO "list entry %d is not in any class list; class %s <> %s\n",
1096 (unsigned int)(e - list_entries),
1097 e->class->name ? : "(?)",
1098 e->links_to->name ? : "(?)");
1099 return false;
1100 }
1101 }
1102
1103 /*
1104 * Check whether all list entries that are not in use do not occur in
1105 * a class lock list.
1106 */
1107 for_each_clear_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
1108 e = list_entries + i;
1109 if (in_any_class_list(&e->entry)) {
1110 printk(KERN_INFO "list entry %d occurs in a class list; class %s <> %s\n",
1111 (unsigned int)(e - list_entries),
1112 e->class && e->class->name ? e->class->name :
1113 "(?)",
1114 e->links_to && e->links_to->name ?
1115 e->links_to->name : "(?)");
1116 return false;
1117 }
1118 }
1119
1120 return true;
1121}
1122
Peter Zijlstra72dcd502019-02-25 15:56:32 +01001123int check_consistency = 0;
1124module_param(check_consistency, int, 0644);
1125
1126static void check_data_structures(void)
1127{
1128 static bool once = false;
1129
1130 if (check_consistency && !once) {
1131 if (!__check_data_structures()) {
1132 once = true;
1133 WARN_ON(once);
1134 }
1135 }
1136}
1137
1138#else /* CONFIG_DEBUG_LOCKDEP */
1139
1140static inline void check_data_structures(void) { }
1141
1142#endif /* CONFIG_DEBUG_LOCKDEP */
1143
Waiman Long810507f2020-02-06 10:24:08 -05001144static void init_chain_block_buckets(void);
1145
Ingo Molnard6d897c2006-07-10 04:44:04 -07001146/*
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001147 * Initialize the lock_classes[] array elements, the free_lock_classes list
1148 * and also the delayed_free structure.
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001149 */
1150static void init_data_structures_once(void)
1151{
Waiman Long810507f2020-02-06 10:24:08 -05001152 static bool __read_mostly ds_initialized, rcu_head_initialized;
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001153 int i;
1154
Bart Van Assche01265742019-03-03 10:19:01 -08001155 if (likely(rcu_head_initialized))
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001156 return;
1157
Bart Van Assche01265742019-03-03 10:19:01 -08001158 if (system_state >= SYSTEM_SCHEDULING) {
1159 init_rcu_head(&delayed_free.rcu_head);
1160 rcu_head_initialized = true;
1161 }
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001162
Bart Van Assche01265742019-03-03 10:19:01 -08001163 if (ds_initialized)
1164 return;
1165
1166 ds_initialized = true;
1167
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001168 INIT_LIST_HEAD(&delayed_free.pf[0].zapped);
1169 INIT_LIST_HEAD(&delayed_free.pf[1].zapped);
1170
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001171 for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001172 list_add_tail(&lock_classes[i].lock_entry, &free_lock_classes);
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001173 INIT_LIST_HEAD(&lock_classes[i].locks_after);
1174 INIT_LIST_HEAD(&lock_classes[i].locks_before);
1175 }
Waiman Long810507f2020-02-06 10:24:08 -05001176 init_chain_block_buckets();
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001177}
1178
Bart Van Assche108c1482019-02-14 15:00:53 -08001179static inline struct hlist_head *keyhashentry(const struct lock_class_key *key)
1180{
1181 unsigned long hash = hash_long((uintptr_t)key, KEYHASH_BITS);
1182
1183 return lock_keys_hash + hash;
1184}
1185
1186/* Register a dynamically allocated key. */
1187void lockdep_register_key(struct lock_class_key *key)
1188{
1189 struct hlist_head *hash_head;
1190 struct lock_class_key *k;
1191 unsigned long flags;
1192
1193 if (WARN_ON_ONCE(static_obj(key)))
1194 return;
1195 hash_head = keyhashentry(key);
1196
1197 raw_local_irq_save(flags);
1198 if (!graph_lock())
1199 goto restore_irqs;
1200 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
1201 if (WARN_ON_ONCE(k == key))
1202 goto out_unlock;
1203 }
1204 hlist_add_head_rcu(&key->hash_entry, hash_head);
1205out_unlock:
1206 graph_unlock();
1207restore_irqs:
1208 raw_local_irq_restore(flags);
1209}
1210EXPORT_SYMBOL_GPL(lockdep_register_key);
1211
1212/* Check whether a key has been registered as a dynamic key. */
1213static bool is_dynamic_key(const struct lock_class_key *key)
1214{
1215 struct hlist_head *hash_head;
1216 struct lock_class_key *k;
1217 bool found = false;
1218
1219 if (WARN_ON_ONCE(static_obj(key)))
1220 return false;
1221
1222 /*
1223 * If lock debugging is disabled lock_keys_hash[] may contain
1224 * pointers to memory that has already been freed. Avoid triggering
1225 * a use-after-free in that case by returning early.
1226 */
1227 if (!debug_locks)
1228 return true;
1229
1230 hash_head = keyhashentry(key);
1231
1232 rcu_read_lock();
1233 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
1234 if (k == key) {
1235 found = true;
1236 break;
1237 }
1238 }
1239 rcu_read_unlock();
1240
1241 return found;
1242}
1243
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001244/*
Ingo Molnard6d897c2006-07-10 04:44:04 -07001245 * Register a lock's class in the hash-table, if the class is not present
1246 * yet. Otherwise we look it up. We cache the result in the lock object
1247 * itself, so actual lookup of the hash should be once per lock object.
1248 */
Denys Vlasenkoc003ed92016-04-08 20:58:46 +02001249static struct lock_class *
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04001250register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
Ingo Molnard6d897c2006-07-10 04:44:04 -07001251{
1252 struct lockdep_subclass_key *key;
Andrew Mortona63f38c2016-02-03 13:44:12 -08001253 struct hlist_head *hash_head;
Ingo Molnard6d897c2006-07-10 04:44:04 -07001254 struct lock_class *class;
Peter Zijlstra35a93932015-02-26 16:23:11 +01001255
1256 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
Ingo Molnard6d897c2006-07-10 04:44:04 -07001257
1258 class = look_up_lock_class(lock, subclass);
Matthew Wilcox64f29d12018-01-17 07:14:12 -08001259 if (likely(class))
Yong Zhang87cdee72011-11-09 16:07:14 +08001260 goto out_set_class_cache;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001261
Matthew Wilcox64f29d12018-01-17 07:14:12 -08001262 if (!lock->key) {
1263 if (!assign_lock_key(lock))
1264 return NULL;
Bart Van Assche108c1482019-02-14 15:00:53 -08001265 } else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001266 return NULL;
1267 }
1268
Ingo Molnard6d897c2006-07-10 04:44:04 -07001269 key = lock->key->subkeys + subclass;
1270 hash_head = classhashentry(key);
1271
Ingo Molnar74c383f2006-12-13 00:34:43 -08001272 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001273 return NULL;
1274 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001275 /*
1276 * We have to do the hash-walk again, to avoid races
1277 * with another CPU:
1278 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08001279 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001280 if (class->key == key)
1281 goto out_unlock_set;
Peter Zijlstra35a93932015-02-26 16:23:11 +01001282 }
1283
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001284 init_data_structures_once();
1285
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001286 /* Allocate a new lock class and add it to the hash. */
1287 class = list_first_entry_or_null(&free_lock_classes, typeof(*class),
1288 lock_entry);
1289 if (!class) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001290 if (!debug_locks_off_graph_unlock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001291 return NULL;
1292 }
Ingo Molnar74c383f2006-12-13 00:34:43 -08001293
Dave Jones2c522832013-04-25 13:40:02 -04001294 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01001295 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001296 return NULL;
1297 }
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001298 nr_lock_classes++;
Yuyang Du01bb6f02019-05-06 16:19:25 +08001299 __set_bit(class - lock_classes, lock_classes_in_use);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02001300 debug_atomic_inc(nr_unused_locks);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001301 class->key = key;
1302 class->name = lock->name;
1303 class->subclass = subclass;
Bart Van Asschefeb0a382019-02-14 15:00:42 -08001304 WARN_ON_ONCE(!list_empty(&class->locks_before));
1305 WARN_ON_ONCE(!list_empty(&class->locks_after));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001306 class->name_version = count_matching_names(class);
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01001307 class->wait_type_inner = lock->wait_type_inner;
1308 class->wait_type_outer = lock->wait_type_outer;
Peter Zijlstradfd5e3f2020-12-09 16:06:21 +01001309 class->lock_type = lock->lock_type;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001310 /*
1311 * We use RCU's safe list-add method to make
1312 * parallel walking of the hash-list safe:
1313 */
Andrew Mortona63f38c2016-02-03 13:44:12 -08001314 hlist_add_head_rcu(&class->hash_entry, hash_head);
Dale Farnsworth14811972008-02-25 23:03:02 +01001315 /*
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001316 * Remove the class from the free list and add it to the global list
1317 * of classes.
Dale Farnsworth14811972008-02-25 23:03:02 +01001318 */
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08001319 list_move_tail(&class->lock_entry, &all_lock_classes);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001320
1321 if (verbose(class)) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001322 graph_unlock();
Ingo Molnar74c383f2006-12-13 00:34:43 -08001323
Borislav Petkov04860d42018-02-26 14:49:26 +01001324 printk("\nnew class %px: %s", class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001325 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001326 printk(KERN_CONT "#%d", class->name_version);
1327 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001328 dump_stack();
Ingo Molnar74c383f2006-12-13 00:34:43 -08001329
Ingo Molnar74c383f2006-12-13 00:34:43 -08001330 if (!graph_lock()) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08001331 return NULL;
1332 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001333 }
1334out_unlock_set:
Ingo Molnar74c383f2006-12-13 00:34:43 -08001335 graph_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001336
Yong Zhang87cdee72011-11-09 16:07:14 +08001337out_set_class_cache:
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04001338 if (!subclass || force)
Hitoshi Mitake62016252010-10-05 18:01:51 +09001339 lock->class_cache[0] = class;
1340 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
1341 lock->class_cache[subclass] = class;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001342
Peter Zijlstra0119fee2011-09-02 01:30:29 +02001343 /*
1344 * Hash collision, did we smoke some? We found a class with a matching
1345 * hash but the subclass -- which is hashed in -- didn't match.
1346 */
Jarek Poplawski381a2292007-02-10 01:44:58 -08001347 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
1348 return NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001349
1350 return class;
1351}
1352
Peter Zijlstraca58abc2007-07-19 01:48:53 -07001353#ifdef CONFIG_PROVE_LOCKING
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07001354/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07001355 * Allocate a lockdep entry. (assumes the graph_lock held, returns
1356 * with NULL on failure)
1357 */
1358static struct lock_list *alloc_list_entry(void)
1359{
Bart Van Asscheace35a72019-02-14 15:00:47 -08001360 int idx = find_first_zero_bit(list_entries_in_use,
1361 ARRAY_SIZE(list_entries));
1362
1363 if (idx >= ARRAY_SIZE(list_entries)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07001364 if (!debug_locks_off_graph_unlock())
1365 return NULL;
1366
Dave Jones2c522832013-04-25 13:40:02 -04001367 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01001368 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -07001369 return NULL;
1370 }
Bart Van Asscheace35a72019-02-14 15:00:47 -08001371 nr_list_entries++;
1372 __set_bit(idx, list_entries_in_use);
1373 return list_entries + idx;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001374}
1375
1376/*
1377 * Add a new dependency to the head of the list:
1378 */
Bart Van Assche86cffb82019-02-14 15:00:41 -08001379static int add_lock_to_list(struct lock_class *this,
1380 struct lock_class *links_to, struct list_head *head,
Boqun Feng3454a362020-08-07 15:42:25 +08001381 unsigned long ip, u16 distance, u8 dep,
Bart Van Assche12593b72019-07-22 11:24:42 -07001382 const struct lock_trace *trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001383{
1384 struct lock_list *entry;
1385 /*
1386 * Lock not present yet - get a new dependency struct and
1387 * add it to the list:
1388 */
1389 entry = alloc_list_entry();
1390 if (!entry)
1391 return 0;
1392
Zhu Yi74870172008-08-27 14:33:00 +08001393 entry->class = this;
Bart Van Assche86cffb82019-02-14 15:00:41 -08001394 entry->links_to = links_to;
Boqun Feng3454a362020-08-07 15:42:25 +08001395 entry->dep = dep;
Zhu Yi74870172008-08-27 14:33:00 +08001396 entry->distance = distance;
Bart Van Assche12593b72019-07-22 11:24:42 -07001397 entry->trace = trace;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001398 /*
Peter Zijlstra35a93932015-02-26 16:23:11 +01001399 * Both allocation and removal are done under the graph lock; but
1400 * iteration is under RCU-sched; see look_up_lock_class() and
1401 * lockdep_free_key_range().
Peter Zijlstra8e182572007-07-19 01:48:54 -07001402 */
1403 list_add_tail_rcu(&entry->entry, head);
1404
1405 return 1;
1406}
1407
Peter Zijlstra98c33ed2009-07-21 13:19:07 +02001408/*
1409 * For good efficiency of modular, we use power of 2
1410 */
Tetsuo Handa5dc33592021-04-05 20:33:57 +09001411#define MAX_CIRCULAR_QUEUE_SIZE (1UL << CONFIG_LOCKDEP_CIRCULAR_QUEUE_BITS)
Peter Zijlstraaf012962009-07-16 15:44:29 +02001412#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
1413
Peter Zijlstra98c33ed2009-07-21 13:19:07 +02001414/*
Yuyang Duaa480772019-05-06 16:19:28 +08001415 * The circular_queue and helpers are used to implement graph
1416 * breadth-first search (BFS) algorithm, by which we can determine
1417 * whether there is a path from a lock to another. In deadlock checks,
1418 * a path from the next lock to be acquired to a previous held lock
1419 * indicates that adding the <prev> -> <next> lock dependency will
1420 * produce a circle in the graph. Breadth-first search instead of
1421 * depth-first search is used in order to find the shortest (circular)
1422 * path.
Peter Zijlstra98c33ed2009-07-21 13:19:07 +02001423 */
Peter Zijlstraaf012962009-07-16 15:44:29 +02001424struct circular_queue {
Yuyang Duaa480772019-05-06 16:19:28 +08001425 struct lock_list *element[MAX_CIRCULAR_QUEUE_SIZE];
Peter Zijlstraaf012962009-07-16 15:44:29 +02001426 unsigned int front, rear;
1427};
1428
1429static struct circular_queue lock_cq;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001430
Ming Lei12f3dfd2009-07-16 15:44:29 +02001431unsigned int max_bfs_queue_depth;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001432
Ming Leie351b662009-07-22 22:48:09 +08001433static unsigned int lockdep_dependency_gen_id;
1434
Peter Zijlstraaf012962009-07-16 15:44:29 +02001435static inline void __cq_init(struct circular_queue *cq)
1436{
1437 cq->front = cq->rear = 0;
Ming Leie351b662009-07-22 22:48:09 +08001438 lockdep_dependency_gen_id++;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001439}
1440
1441static inline int __cq_empty(struct circular_queue *cq)
1442{
1443 return (cq->front == cq->rear);
1444}
1445
1446static inline int __cq_full(struct circular_queue *cq)
1447{
1448 return ((cq->rear + 1) & CQ_MASK) == cq->front;
1449}
1450
Yuyang Duaa480772019-05-06 16:19:28 +08001451static inline int __cq_enqueue(struct circular_queue *cq, struct lock_list *elem)
Peter Zijlstraaf012962009-07-16 15:44:29 +02001452{
1453 if (__cq_full(cq))
1454 return -1;
1455
1456 cq->element[cq->rear] = elem;
1457 cq->rear = (cq->rear + 1) & CQ_MASK;
1458 return 0;
1459}
1460
Yuyang Duc1661322019-05-06 16:19:29 +08001461/*
1462 * Dequeue an element from the circular_queue, return a lock_list if
1463 * the queue is not empty, or NULL if otherwise.
1464 */
1465static inline struct lock_list * __cq_dequeue(struct circular_queue *cq)
Peter Zijlstraaf012962009-07-16 15:44:29 +02001466{
Yuyang Duc1661322019-05-06 16:19:29 +08001467 struct lock_list * lock;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001468
Yuyang Duc1661322019-05-06 16:19:29 +08001469 if (__cq_empty(cq))
1470 return NULL;
1471
1472 lock = cq->element[cq->front];
Peter Zijlstraaf012962009-07-16 15:44:29 +02001473 cq->front = (cq->front + 1) & CQ_MASK;
Yuyang Duc1661322019-05-06 16:19:29 +08001474
1475 return lock;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001476}
1477
1478static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
1479{
1480 return (cq->rear - cq->front) & CQ_MASK;
1481}
1482
Boqun Fengd563bc62020-08-07 15:42:23 +08001483static inline void mark_lock_accessed(struct lock_list *lock)
Peter Zijlstraaf012962009-07-16 15:44:29 +02001484{
Ming Leie351b662009-07-22 22:48:09 +08001485 lock->class->dep_gen_id = lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001486}
1487
Boqun Fengd563bc62020-08-07 15:42:23 +08001488static inline void visit_lock_entry(struct lock_list *lock,
1489 struct lock_list *parent)
1490{
1491 lock->parent = parent;
1492}
1493
Peter Zijlstraaf012962009-07-16 15:44:29 +02001494static inline unsigned long lock_accessed(struct lock_list *lock)
1495{
Ming Leie351b662009-07-22 22:48:09 +08001496 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
Peter Zijlstraaf012962009-07-16 15:44:29 +02001497}
1498
1499static inline struct lock_list *get_lock_parent(struct lock_list *child)
1500{
1501 return child->parent;
1502}
1503
1504static inline int get_lock_depth(struct lock_list *child)
1505{
1506 int depth = 0;
1507 struct lock_list *parent;
1508
1509 while ((parent = get_lock_parent(child))) {
1510 child = parent;
1511 depth++;
1512 }
1513 return depth;
1514}
1515
Yuyang Du77a80692019-05-06 16:19:30 +08001516/*
1517 * Return the forward or backward dependency list.
1518 *
1519 * @lock: the lock_list to get its class's dependency list
1520 * @offset: the offset to struct lock_class to determine whether it is
1521 * locks_after or locks_before
1522 */
1523static inline struct list_head *get_dep_list(struct lock_list *lock, int offset)
1524{
1525 void *lock_class = lock->class;
1526
1527 return lock_class + offset;
1528}
Boqun Fengb11be022020-08-07 15:42:22 +08001529/*
1530 * Return values of a bfs search:
1531 *
1532 * BFS_E* indicates an error
1533 * BFS_R* indicates a result (match or not)
1534 *
1535 * BFS_EINVALIDNODE: Find a invalid node in the graph.
1536 *
1537 * BFS_EQUEUEFULL: The queue is full while doing the bfs.
1538 *
1539 * BFS_RMATCH: Find the matched node in the graph, and put that node into
1540 * *@target_entry.
1541 *
1542 * BFS_RNOMATCH: Haven't found the matched node and keep *@target_entry
1543 * _unchanged_.
1544 */
1545enum bfs_result {
1546 BFS_EINVALIDNODE = -2,
1547 BFS_EQUEUEFULL = -1,
1548 BFS_RMATCH = 0,
1549 BFS_RNOMATCH = 1,
1550};
1551
1552/*
1553 * bfs_result < 0 means error
1554 */
1555static inline bool bfs_error(enum bfs_result res)
1556{
1557 return res < 0;
1558}
Yuyang Du77a80692019-05-06 16:19:30 +08001559
Yuyang Du154f1852019-05-06 16:19:31 +08001560/*
Boqun Feng3454a362020-08-07 15:42:25 +08001561 * DEP_*_BIT in lock_list::dep
1562 *
1563 * For dependency @prev -> @next:
1564 *
1565 * SR: @prev is shared reader (->read != 0) and @next is recursive reader
1566 * (->read == 2)
1567 * ER: @prev is exclusive locker (->read == 0) and @next is recursive reader
1568 * SN: @prev is shared reader and @next is non-recursive locker (->read != 2)
1569 * EN: @prev is exclusive locker and @next is non-recursive locker
1570 *
1571 * Note that we define the value of DEP_*_BITs so that:
1572 * bit0 is prev->read == 0
1573 * bit1 is next->read != 2
1574 */
1575#define DEP_SR_BIT (0 + (0 << 1)) /* 0 */
1576#define DEP_ER_BIT (1 + (0 << 1)) /* 1 */
1577#define DEP_SN_BIT (0 + (1 << 1)) /* 2 */
1578#define DEP_EN_BIT (1 + (1 << 1)) /* 3 */
1579
1580#define DEP_SR_MASK (1U << (DEP_SR_BIT))
1581#define DEP_ER_MASK (1U << (DEP_ER_BIT))
1582#define DEP_SN_MASK (1U << (DEP_SN_BIT))
1583#define DEP_EN_MASK (1U << (DEP_EN_BIT))
1584
1585static inline unsigned int
1586__calc_dep_bit(struct held_lock *prev, struct held_lock *next)
1587{
1588 return (prev->read == 0) + ((next->read != 2) << 1);
1589}
1590
1591static inline u8 calc_dep(struct held_lock *prev, struct held_lock *next)
1592{
1593 return 1U << __calc_dep_bit(prev, next);
1594}
1595
1596/*
1597 * calculate the dep_bit for backwards edges. We care about whether @prev is
1598 * shared and whether @next is recursive.
1599 */
1600static inline unsigned int
1601__calc_dep_bitb(struct held_lock *prev, struct held_lock *next)
1602{
1603 return (next->read != 2) + ((prev->read == 0) << 1);
1604}
1605
1606static inline u8 calc_depb(struct held_lock *prev, struct held_lock *next)
1607{
1608 return 1U << __calc_dep_bitb(prev, next);
1609}
1610
1611/*
Boqun Feng6971c0f2020-08-07 15:42:26 +08001612 * Initialize a lock_list entry @lock belonging to @class as the root for a BFS
1613 * search.
1614 */
1615static inline void __bfs_init_root(struct lock_list *lock,
1616 struct lock_class *class)
1617{
1618 lock->class = class;
1619 lock->parent = NULL;
1620 lock->only_xr = 0;
1621}
1622
1623/*
1624 * Initialize a lock_list entry @lock based on a lock acquisition @hlock as the
1625 * root for a BFS search.
1626 *
1627 * ->only_xr of the initial lock node is set to @hlock->read == 2, to make sure
1628 * that <prev> -> @hlock and @hlock -> <whatever __bfs() found> is not -(*R)->
1629 * and -(S*)->.
1630 */
1631static inline void bfs_init_root(struct lock_list *lock,
1632 struct held_lock *hlock)
1633{
1634 __bfs_init_root(lock, hlock_class(hlock));
1635 lock->only_xr = (hlock->read == 2);
1636}
1637
1638/*
1639 * Similar to bfs_init_root() but initialize the root for backwards BFS.
1640 *
1641 * ->only_xr of the initial lock node is set to @hlock->read != 0, to make sure
1642 * that <next> -> @hlock and @hlock -> <whatever backwards BFS found> is not
1643 * -(*S)-> and -(R*)-> (reverse order of -(*R)-> and -(S*)->).
1644 */
1645static inline void bfs_init_rootb(struct lock_list *lock,
1646 struct held_lock *hlock)
1647{
1648 __bfs_init_root(lock, hlock_class(hlock));
1649 lock->only_xr = (hlock->read != 0);
1650}
1651
Boqun Feng6d1823c2020-09-17 16:01:50 +08001652static inline struct lock_list *__bfs_next(struct lock_list *lock, int offset)
1653{
1654 if (!lock || !lock->parent)
1655 return NULL;
1656
1657 return list_next_or_null_rcu(get_dep_list(lock->parent, offset),
1658 &lock->entry, struct lock_list, entry);
1659}
1660
Boqun Feng6971c0f2020-08-07 15:42:26 +08001661/*
1662 * Breadth-First Search to find a strong path in the dependency graph.
1663 *
1664 * @source_entry: the source of the path we are searching for.
1665 * @data: data used for the second parameter of @match function
1666 * @match: match function for the search
1667 * @target_entry: pointer to the target of a matched path
1668 * @offset: the offset to struct lock_class to determine whether it is
1669 * locks_after or locks_before
1670 *
1671 * We may have multiple edges (considering different kinds of dependencies,
1672 * e.g. ER and SN) between two nodes in the dependency graph. But
1673 * only the strong dependency path in the graph is relevant to deadlocks. A
1674 * strong dependency path is a dependency path that doesn't have two adjacent
1675 * dependencies as -(*R)-> -(S*)->, please see:
1676 *
1677 * Documentation/locking/lockdep-design.rst
1678 *
1679 * for more explanation of the definition of strong dependency paths
1680 *
1681 * In __bfs(), we only traverse in the strong dependency path:
1682 *
1683 * In lock_list::only_xr, we record whether the previous dependency only
1684 * has -(*R)-> in the search, and if it does (prev only has -(*R)->), we
1685 * filter out any -(S*)-> in the current dependency and after that, the
1686 * ->only_xr is set according to whether we only have -(*R)-> left.
Yuyang Du154f1852019-05-06 16:19:31 +08001687 */
Boqun Fengb11be022020-08-07 15:42:22 +08001688static enum bfs_result __bfs(struct lock_list *source_entry,
1689 void *data,
Boqun Feng61775ed2020-08-07 15:42:27 +08001690 bool (*match)(struct lock_list *entry, void *data),
Boqun Fengbc2dd712020-12-10 11:02:40 +01001691 bool (*skip)(struct lock_list *entry, void *data),
Boqun Fengb11be022020-08-07 15:42:22 +08001692 struct lock_list **target_entry,
1693 int offset)
Ming Leic94aa5c2009-07-16 15:44:29 +02001694{
Ming Leic94aa5c2009-07-16 15:44:29 +02001695 struct circular_queue *cq = &lock_cq;
Boqun Feng6d1823c2020-09-17 16:01:50 +08001696 struct lock_list *lock = NULL;
1697 struct lock_list *entry;
1698 struct list_head *head;
1699 unsigned int cq_depth;
1700 bool first;
Ming Leic94aa5c2009-07-16 15:44:29 +02001701
Peter Zijlstra248efb22020-03-13 11:09:49 +01001702 lockdep_assert_locked();
1703
Ming Leid588e462009-07-16 15:44:29 +02001704 __cq_init(cq);
Yuyang Duaa480772019-05-06 16:19:28 +08001705 __cq_enqueue(cq, source_entry);
Ming Leic94aa5c2009-07-16 15:44:29 +02001706
Boqun Feng6d1823c2020-09-17 16:01:50 +08001707 while ((lock = __bfs_next(lock, offset)) || (lock = __cq_dequeue(cq))) {
1708 if (!lock->class)
1709 return BFS_EINVALIDNODE;
Ming Leic94aa5c2009-07-16 15:44:29 +02001710
Boqun Fengd563bc62020-08-07 15:42:23 +08001711 /*
Boqun Feng6d1823c2020-09-17 16:01:50 +08001712 * Step 1: check whether we already finish on this one.
1713 *
Boqun Fengd563bc62020-08-07 15:42:23 +08001714 * If we have visited all the dependencies from this @lock to
1715 * others (iow, if we have visited all lock_list entries in
1716 * @lock->class->locks_{after,before}) we skip, otherwise go
1717 * and visit all the dependencies in the list and mark this
1718 * list accessed.
1719 */
1720 if (lock_accessed(lock))
1721 continue;
1722 else
1723 mark_lock_accessed(lock);
1724
Boqun Feng6d1823c2020-09-17 16:01:50 +08001725 /*
1726 * Step 2: check whether prev dependency and this form a strong
1727 * dependency path.
1728 */
1729 if (lock->parent) { /* Parent exists, check prev dependency */
1730 u8 dep = lock->dep;
1731 bool prev_only_xr = lock->parent->only_xr;
Boqun Feng6971c0f2020-08-07 15:42:26 +08001732
1733 /*
1734 * Mask out all -(S*)-> if we only have *R in previous
1735 * step, because -(*R)-> -(S*)-> don't make up a strong
1736 * dependency.
1737 */
1738 if (prev_only_xr)
1739 dep &= ~(DEP_SR_MASK | DEP_SN_MASK);
1740
1741 /* If nothing left, we skip */
1742 if (!dep)
1743 continue;
1744
1745 /* If there are only -(*R)-> left, set that for the next step */
Boqun Feng6d1823c2020-09-17 16:01:50 +08001746 lock->only_xr = !(dep & (DEP_SN_MASK | DEP_EN_MASK));
1747 }
Boqun Fengd563bc62020-08-07 15:42:23 +08001748
Boqun Feng6d1823c2020-09-17 16:01:50 +08001749 /*
1750 * Step 3: we haven't visited this and there is a strong
1751 * dependency path to this, so check with @match.
Boqun Fengbc2dd712020-12-10 11:02:40 +01001752 * If @skip is provide and returns true, we skip this
1753 * lock (and any path this lock is in).
Boqun Feng6d1823c2020-09-17 16:01:50 +08001754 */
Boqun Fengbc2dd712020-12-10 11:02:40 +01001755 if (skip && skip(lock, data))
1756 continue;
1757
Boqun Feng6d1823c2020-09-17 16:01:50 +08001758 if (match(lock, data)) {
1759 *target_entry = lock;
1760 return BFS_RMATCH;
1761 }
1762
1763 /*
1764 * Step 4: if not match, expand the path by adding the
Ingo Molnare2db7592021-03-22 02:35:05 +01001765 * forward or backwards dependencies in the search
Boqun Feng6d1823c2020-09-17 16:01:50 +08001766 *
1767 */
1768 first = true;
1769 head = get_dep_list(lock, offset);
1770 list_for_each_entry_rcu(entry, head, entry) {
Boqun Fengd563bc62020-08-07 15:42:23 +08001771 visit_lock_entry(entry, lock);
Boqun Fengd563bc62020-08-07 15:42:23 +08001772
Boqun Feng6d1823c2020-09-17 16:01:50 +08001773 /*
1774 * Note we only enqueue the first of the list into the
1775 * queue, because we can always find a sibling
1776 * dependency from one (see __bfs_next()), as a result
1777 * the space of queue is saved.
1778 */
1779 if (!first)
1780 continue;
1781
1782 first = false;
1783
1784 if (__cq_enqueue(cq, entry))
1785 return BFS_EQUEUEFULL;
1786
Boqun Fengd563bc62020-08-07 15:42:23 +08001787 cq_depth = __cq_get_elem_count(cq);
1788 if (max_bfs_queue_depth < cq_depth)
1789 max_bfs_queue_depth = cq_depth;
Ming Leic94aa5c2009-07-16 15:44:29 +02001790 }
1791 }
Boqun Feng6d1823c2020-09-17 16:01:50 +08001792
1793 return BFS_RNOMATCH;
Ming Leic94aa5c2009-07-16 15:44:29 +02001794}
1795
Boqun Fengb11be022020-08-07 15:42:22 +08001796static inline enum bfs_result
1797__bfs_forwards(struct lock_list *src_entry,
1798 void *data,
Boqun Feng61775ed2020-08-07 15:42:27 +08001799 bool (*match)(struct lock_list *entry, void *data),
Boqun Fengbc2dd712020-12-10 11:02:40 +01001800 bool (*skip)(struct lock_list *entry, void *data),
Boqun Fengb11be022020-08-07 15:42:22 +08001801 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001802{
Boqun Fengbc2dd712020-12-10 11:02:40 +01001803 return __bfs(src_entry, data, match, skip, target_entry,
Yuyang Du77a80692019-05-06 16:19:30 +08001804 offsetof(struct lock_class, locks_after));
Ming Leic94aa5c2009-07-16 15:44:29 +02001805
1806}
1807
Boqun Fengb11be022020-08-07 15:42:22 +08001808static inline enum bfs_result
1809__bfs_backwards(struct lock_list *src_entry,
1810 void *data,
Boqun Feng61775ed2020-08-07 15:42:27 +08001811 bool (*match)(struct lock_list *entry, void *data),
Boqun Fengbc2dd712020-12-10 11:02:40 +01001812 bool (*skip)(struct lock_list *entry, void *data),
Boqun Fengb11be022020-08-07 15:42:22 +08001813 struct lock_list **target_entry)
Ming Leic94aa5c2009-07-16 15:44:29 +02001814{
Boqun Fengbc2dd712020-12-10 11:02:40 +01001815 return __bfs(src_entry, data, match, skip, target_entry,
Yuyang Du77a80692019-05-06 16:19:30 +08001816 offsetof(struct lock_class, locks_before));
Ming Leic94aa5c2009-07-16 15:44:29 +02001817
1818}
1819
Bart Van Assche12593b72019-07-22 11:24:42 -07001820static void print_lock_trace(const struct lock_trace *trace,
1821 unsigned int spaces)
Thomas Gleixnerc120bce2019-04-25 11:45:12 +02001822{
Bart Van Assche12593b72019-07-22 11:24:42 -07001823 stack_trace_print(trace->entries, trace->nr_entries, spaces);
Thomas Gleixnerc120bce2019-04-25 11:45:12 +02001824}
1825
Peter Zijlstra8e182572007-07-19 01:48:54 -07001826/*
1827 * Print a dependency chain entry (this is only done when a deadlock
1828 * has been detected):
1829 */
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001830static noinline void
Ming Lei24208ca2009-07-16 15:44:29 +02001831print_circular_bug_entry(struct lock_list *target, int depth)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001832{
1833 if (debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001834 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001835 printk("\n-> #%u", depth);
1836 print_lock_name(target->class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001837 printk(KERN_CONT ":\n");
Bart Van Assche12593b72019-07-22 11:24:42 -07001838 print_lock_trace(target->trace, 6);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001839}
1840
Steven Rostedtf4185812011-04-20 21:41:55 -04001841static void
1842print_circular_lock_scenario(struct held_lock *src,
1843 struct held_lock *tgt,
1844 struct lock_list *prt)
1845{
1846 struct lock_class *source = hlock_class(src);
1847 struct lock_class *target = hlock_class(tgt);
1848 struct lock_class *parent = prt->class;
1849
1850 /*
1851 * A direct locking problem where unsafe_class lock is taken
1852 * directly by safe_class lock, then all we need to show
1853 * is the deadlock scenario, as it is obvious that the
1854 * unsafe lock is taken under the safe lock.
1855 *
1856 * But if there is a chain instead, where the safe lock takes
1857 * an intermediate lock (middle_class) where this lock is
1858 * not the same as the safe lock, then the lock chain is
1859 * used to describe the problem. Otherwise we would need
1860 * to show a different CPU case for each link in the chain
1861 * from the safe_class lock to the unsafe_class lock.
1862 */
1863 if (parent != source) {
1864 printk("Chain exists of:\n ");
1865 __print_lock_name(source);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001866 printk(KERN_CONT " --> ");
Steven Rostedtf4185812011-04-20 21:41:55 -04001867 __print_lock_name(parent);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001868 printk(KERN_CONT " --> ");
Steven Rostedtf4185812011-04-20 21:41:55 -04001869 __print_lock_name(target);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01001870 printk(KERN_CONT "\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001871 }
1872
Ingo Molnare966eae2017-12-12 12:31:16 +01001873 printk(" Possible unsafe locking scenario:\n\n");
1874 printk(" CPU0 CPU1\n");
1875 printk(" ---- ----\n");
1876 printk(" lock(");
1877 __print_lock_name(target);
1878 printk(KERN_CONT ");\n");
1879 printk(" lock(");
1880 __print_lock_name(parent);
1881 printk(KERN_CONT ");\n");
1882 printk(" lock(");
1883 __print_lock_name(target);
1884 printk(KERN_CONT ");\n");
1885 printk(" lock(");
1886 __print_lock_name(source);
1887 printk(KERN_CONT ");\n");
1888 printk("\n *** DEADLOCK ***\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04001889}
1890
Peter Zijlstra8e182572007-07-19 01:48:54 -07001891/*
1892 * When a circular dependency is detected, print the
1893 * header first:
1894 */
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001895static noinline void
Ming Leidb0002a2009-07-16 15:44:29 +02001896print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1897 struct held_lock *check_src,
1898 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001899{
1900 struct task_struct *curr = current;
1901
Ming Leic94aa5c2009-07-16 15:44:29 +02001902 if (debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001903 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001904
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001905 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001906 pr_warn("======================================================\n");
1907 pr_warn("WARNING: possible circular locking dependency detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01001908 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08001909 pr_warn("------------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001910 pr_warn("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001911 curr->comm, task_pid_nr(curr));
Ming Leidb0002a2009-07-16 15:44:29 +02001912 print_lock(check_src);
Byungchul Park383a4bc2017-08-07 16:12:55 +09001913
Ingo Molnare966eae2017-12-12 12:31:16 +01001914 pr_warn("\nbut task is already holding lock:\n");
Byungchul Park383a4bc2017-08-07 16:12:55 +09001915
Ming Leidb0002a2009-07-16 15:44:29 +02001916 print_lock(check_tgt);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07001917 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1918 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07001919
1920 print_circular_bug_entry(entry, depth);
Peter Zijlstra8e182572007-07-19 01:48:54 -07001921}
1922
Boqun Feng68e30562020-08-07 15:42:29 +08001923/*
1924 * We are about to add A -> B into the dependency graph, and in __bfs() a
1925 * strong dependency path A -> .. -> B is found: hlock_class equals
1926 * entry->class.
1927 *
1928 * If A -> .. -> B can replace A -> B in any __bfs() search (means the former
1929 * is _stronger_ than or equal to the latter), we consider A -> B as redundant.
1930 * For example if A -> .. -> B is -(EN)-> (i.e. A -(E*)-> .. -(*N)-> B), and A
1931 * -> B is -(ER)-> or -(EN)->, then we don't need to add A -> B into the
1932 * dependency graph, as any strong path ..-> A -> B ->.. we can get with
1933 * having dependency A -> B, we could already get a equivalent path ..-> A ->
Ingo Molnare2db7592021-03-22 02:35:05 +01001934 * .. -> B -> .. with A -> .. -> B. Therefore A -> B is redundant.
Boqun Feng68e30562020-08-07 15:42:29 +08001935 *
1936 * We need to make sure both the start and the end of A -> .. -> B is not
1937 * weaker than A -> B. For the start part, please see the comment in
1938 * check_redundant(). For the end part, we need:
1939 *
1940 * Either
1941 *
1942 * a) A -> B is -(*R)-> (everything is not weaker than that)
1943 *
1944 * or
1945 *
1946 * b) A -> .. -> B is -(*N)-> (nothing is stronger than this)
1947 *
1948 */
1949static inline bool hlock_equal(struct lock_list *entry, void *data)
Ming Lei9e2d5512009-07-16 15:44:29 +02001950{
Boqun Feng68e30562020-08-07 15:42:29 +08001951 struct held_lock *hlock = (struct held_lock *)data;
1952
1953 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */
1954 (hlock->read == 2 || /* A -> B is -(*R)-> */
1955 !entry->only_xr); /* A -> .. -> B is -(*N)-> */
Ming Lei9e2d5512009-07-16 15:44:29 +02001956}
1957
Boqun Feng9de0c9b2020-08-07 15:42:28 +08001958/*
1959 * We are about to add B -> A into the dependency graph, and in __bfs() a
1960 * strong dependency path A -> .. -> B is found: hlock_class equals
1961 * entry->class.
1962 *
1963 * We will have a deadlock case (conflict) if A -> .. -> B -> A is a strong
1964 * dependency cycle, that means:
1965 *
1966 * Either
1967 *
1968 * a) B -> A is -(E*)->
1969 *
1970 * or
1971 *
1972 * b) A -> .. -> B is -(*N)-> (i.e. A -> .. -(*N)-> B)
1973 *
1974 * as then we don't have -(*R)-> -(S*)-> in the cycle.
1975 */
1976static inline bool hlock_conflict(struct lock_list *entry, void *data)
1977{
1978 struct held_lock *hlock = (struct held_lock *)data;
1979
1980 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */
1981 (hlock->read == 0 || /* B -> A is -(E*)-> */
1982 !entry->only_xr); /* A -> .. -> B is -(*N)-> */
1983}
1984
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001985static noinline void print_circular_bug(struct lock_list *this,
Boqun Feng9de0c9b2020-08-07 15:42:28 +08001986 struct lock_list *target,
1987 struct held_lock *check_src,
1988 struct held_lock *check_tgt)
Peter Zijlstra8e182572007-07-19 01:48:54 -07001989{
1990 struct task_struct *curr = current;
Ming Leic94aa5c2009-07-16 15:44:29 +02001991 struct lock_list *parent;
Steven Rostedtf4185812011-04-20 21:41:55 -04001992 struct lock_list *first_parent;
Ming Lei24208ca2009-07-16 15:44:29 +02001993 int depth;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001994
Ming Leic94aa5c2009-07-16 15:44:29 +02001995 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08001996 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07001997
Bart Van Assche12593b72019-07-22 11:24:42 -07001998 this->trace = save_trace();
1999 if (!this->trace)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002000 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002001
Ming Leic94aa5c2009-07-16 15:44:29 +02002002 depth = get_lock_depth(target);
2003
Ming Leidb0002a2009-07-16 15:44:29 +02002004 print_circular_bug_header(target, depth, check_src, check_tgt);
Ming Leic94aa5c2009-07-16 15:44:29 +02002005
2006 parent = get_lock_parent(target);
Steven Rostedtf4185812011-04-20 21:41:55 -04002007 first_parent = parent;
Ming Leic94aa5c2009-07-16 15:44:29 +02002008
2009 while (parent) {
2010 print_circular_bug_entry(parent, --depth);
2011 parent = get_lock_parent(parent);
2012 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07002013
2014 printk("\nother info that might help us debug this:\n\n");
Steven Rostedtf4185812011-04-20 21:41:55 -04002015 print_circular_lock_scenario(check_src, check_tgt,
2016 first_parent);
2017
Peter Zijlstra8e182572007-07-19 01:48:54 -07002018 lockdep_print_held_locks(curr);
2019
2020 printk("\nstack backtrace:\n");
2021 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -07002022}
2023
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002024static noinline void print_bfs_bug(int ret)
Ming Leidb0002a2009-07-16 15:44:29 +02002025{
2026 if (!debug_locks_off_graph_unlock())
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002027 return;
Ming Leidb0002a2009-07-16 15:44:29 +02002028
Peter Zijlstra0119fee2011-09-02 01:30:29 +02002029 /*
2030 * Breadth-first-search failed, graph got corrupted?
2031 */
Ming Leidb0002a2009-07-16 15:44:29 +02002032 WARN(1, "lockdep bfs error:%d\n", ret);
Ming Leidb0002a2009-07-16 15:44:29 +02002033}
2034
Boqun Feng61775ed2020-08-07 15:42:27 +08002035static bool noop_count(struct lock_list *entry, void *data)
David Miller419ca3f2008-07-29 21:45:03 -07002036{
Ming Leief681022009-07-16 15:44:29 +02002037 (*(unsigned long *)data)++;
Boqun Feng61775ed2020-08-07 15:42:27 +08002038 return false;
David Miller419ca3f2008-07-29 21:45:03 -07002039}
2040
Fengguang Wu5216d532013-11-09 00:55:35 +08002041static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
Ming Leief681022009-07-16 15:44:29 +02002042{
2043 unsigned long count = 0;
Kees Cook3f649ab2020-06-03 13:09:38 -07002044 struct lock_list *target_entry;
Ming Leief681022009-07-16 15:44:29 +02002045
Boqun Fengbc2dd712020-12-10 11:02:40 +01002046 __bfs_forwards(this, (void *)&count, noop_count, NULL, &target_entry);
Ming Leief681022009-07-16 15:44:29 +02002047
2048 return count;
2049}
David Miller419ca3f2008-07-29 21:45:03 -07002050unsigned long lockdep_count_forward_deps(struct lock_class *class)
2051{
2052 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02002053 struct lock_list this;
2054
Boqun Feng6971c0f2020-08-07 15:42:26 +08002055 __bfs_init_root(&this, class);
David Miller419ca3f2008-07-29 21:45:03 -07002056
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04002057 raw_local_irq_save(flags);
Peter Zijlstra248efb22020-03-13 11:09:49 +01002058 lockdep_lock();
Ming Leief681022009-07-16 15:44:29 +02002059 ret = __lockdep_count_forward_deps(&this);
Peter Zijlstra248efb22020-03-13 11:09:49 +01002060 lockdep_unlock();
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04002061 raw_local_irq_restore(flags);
David Miller419ca3f2008-07-29 21:45:03 -07002062
2063 return ret;
2064}
2065
Fengguang Wu5216d532013-11-09 00:55:35 +08002066static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
David Miller419ca3f2008-07-29 21:45:03 -07002067{
Ming Leief681022009-07-16 15:44:29 +02002068 unsigned long count = 0;
Kees Cook3f649ab2020-06-03 13:09:38 -07002069 struct lock_list *target_entry;
David Miller419ca3f2008-07-29 21:45:03 -07002070
Boqun Fengbc2dd712020-12-10 11:02:40 +01002071 __bfs_backwards(this, (void *)&count, noop_count, NULL, &target_entry);
David Miller419ca3f2008-07-29 21:45:03 -07002072
Ming Leief681022009-07-16 15:44:29 +02002073 return count;
David Miller419ca3f2008-07-29 21:45:03 -07002074}
2075
2076unsigned long lockdep_count_backward_deps(struct lock_class *class)
2077{
2078 unsigned long ret, flags;
Ming Leief681022009-07-16 15:44:29 +02002079 struct lock_list this;
2080
Boqun Feng6971c0f2020-08-07 15:42:26 +08002081 __bfs_init_root(&this, class);
David Miller419ca3f2008-07-29 21:45:03 -07002082
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04002083 raw_local_irq_save(flags);
Peter Zijlstra248efb22020-03-13 11:09:49 +01002084 lockdep_lock();
Ming Leief681022009-07-16 15:44:29 +02002085 ret = __lockdep_count_backward_deps(&this);
Peter Zijlstra248efb22020-03-13 11:09:49 +01002086 lockdep_unlock();
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04002087 raw_local_irq_restore(flags);
David Miller419ca3f2008-07-29 21:45:03 -07002088
2089 return ret;
2090}
2091
Peter Zijlstra8e182572007-07-19 01:48:54 -07002092/*
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002093 * Check that the dependency graph starting at <src> can lead to
Boqun Fengb11be022020-08-07 15:42:22 +08002094 * <target> or not.
Peter Zijlstra8e182572007-07-19 01:48:54 -07002095 */
Boqun Fengb11be022020-08-07 15:42:22 +08002096static noinline enum bfs_result
Boqun Feng9de0c9b2020-08-07 15:42:28 +08002097check_path(struct held_lock *target, struct lock_list *src_entry,
2098 bool (*match)(struct lock_list *entry, void *data),
Boqun Fengbc2dd712020-12-10 11:02:40 +01002099 bool (*skip)(struct lock_list *entry, void *data),
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002100 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002101{
Boqun Fengb11be022020-08-07 15:42:22 +08002102 enum bfs_result ret;
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002103
Boqun Fengbc2dd712020-12-10 11:02:40 +01002104 ret = __bfs_forwards(src_entry, target, match, skip, target_entry);
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002105
Boqun Fengb11be022020-08-07 15:42:22 +08002106 if (unlikely(bfs_error(ret)))
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002107 print_bfs_bug(ret);
2108
2109 return ret;
2110}
2111
2112/*
2113 * Prove that the dependency graph starting at <src> can not
2114 * lead to <target>. If it can, there is a circle when adding
2115 * <target> -> <src> dependency.
2116 *
Boqun Fengb11be022020-08-07 15:42:22 +08002117 * Print an error and return BFS_RMATCH if it does.
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002118 */
Boqun Fengb11be022020-08-07 15:42:22 +08002119static noinline enum bfs_result
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002120check_noncircular(struct held_lock *src, struct held_lock *target,
Bart Van Assche12593b72019-07-22 11:24:42 -07002121 struct lock_trace **const trace)
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002122{
Boqun Fengb11be022020-08-07 15:42:22 +08002123 enum bfs_result ret;
Kees Cook3f649ab2020-06-03 13:09:38 -07002124 struct lock_list *target_entry;
Boqun Feng6971c0f2020-08-07 15:42:26 +08002125 struct lock_list src_entry;
2126
2127 bfs_init_root(&src_entry, src);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002128
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002129 debug_atomic_inc(nr_cyclic_checks);
David Miller419ca3f2008-07-29 21:45:03 -07002130
Boqun Fengbc2dd712020-12-10 11:02:40 +01002131 ret = check_path(target, &src_entry, hlock_conflict, NULL, &target_entry);
Ming Leidb0002a2009-07-16 15:44:29 +02002132
Boqun Fengb11be022020-08-07 15:42:22 +08002133 if (unlikely(ret == BFS_RMATCH)) {
Bart Van Assche12593b72019-07-22 11:24:42 -07002134 if (!*trace) {
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002135 /*
2136 * If save_trace fails here, the printing might
2137 * trigger a WARN but because of the !nr_entries it
2138 * should not do bad things.
2139 */
Bart Van Assche12593b72019-07-22 11:24:42 -07002140 *trace = save_trace();
Yuyang Du8c2c2b42019-05-06 16:19:35 +08002141 }
2142
2143 print_circular_bug(&src_entry, target_entry, src, target);
2144 }
2145
2146 return ret;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002147}
2148
Yuyang Due7a38f62019-05-06 16:19:20 +08002149#ifdef CONFIG_TRACE_IRQFLAGS
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002150
Boqun Fengf08e3882020-08-07 15:42:30 +08002151/*
2152 * Forwards and backwards subgraph searching, for the purposes of
2153 * proving that two subgraphs can be connected by a new dependency
2154 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
2155 *
2156 * A irq safe->unsafe deadlock happens with the following conditions:
2157 *
2158 * 1) We have a strong dependency path A -> ... -> B
2159 *
2160 * 2) and we have ENABLED_IRQ usage of B and USED_IN_IRQ usage of A, therefore
2161 * irq can create a new dependency B -> A (consider the case that a holder
2162 * of B gets interrupted by an irq whose handler will try to acquire A).
2163 *
2164 * 3) the dependency circle A -> ... -> B -> A we get from 1) and 2) is a
2165 * strong circle:
2166 *
2167 * For the usage bits of B:
2168 * a) if A -> B is -(*N)->, then B -> A could be any type, so any
2169 * ENABLED_IRQ usage suffices.
2170 * b) if A -> B is -(*R)->, then B -> A must be -(E*)->, so only
2171 * ENABLED_IRQ_*_READ usage suffices.
2172 *
2173 * For the usage bits of A:
2174 * c) if A -> B is -(E*)->, then B -> A could be any type, so any
2175 * USED_IN_IRQ usage suffices.
2176 * d) if A -> B is -(S*)->, then B -> A must be -(*N)->, so only
2177 * USED_IN_IRQ_*_READ usage suffices.
2178 */
2179
2180/*
2181 * There is a strong dependency path in the dependency graph: A -> B, and now
2182 * we need to decide which usage bit of A should be accumulated to detect
2183 * safe->unsafe bugs.
2184 *
2185 * Note that usage_accumulate() is used in backwards search, so ->only_xr
2186 * stands for whether A -> B only has -(S*)-> (in this case ->only_xr is true).
2187 *
2188 * As above, if only_xr is false, which means A -> B has -(E*)-> dependency
2189 * path, any usage of A should be considered. Otherwise, we should only
2190 * consider _READ usage.
2191 */
Boqun Feng61775ed2020-08-07 15:42:27 +08002192static inline bool usage_accumulate(struct lock_list *entry, void *mask)
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002193{
Boqun Fengf08e3882020-08-07 15:42:30 +08002194 if (!entry->only_xr)
2195 *(unsigned long *)mask |= entry->class->usage_mask;
2196 else /* Mask out _READ usage bits */
2197 *(unsigned long *)mask |= (entry->class->usage_mask & LOCKF_IRQ);
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002198
Boqun Feng61775ed2020-08-07 15:42:27 +08002199 return false;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002200}
2201
Peter Zijlstra8e182572007-07-19 01:48:54 -07002202/*
Boqun Fengf08e3882020-08-07 15:42:30 +08002203 * There is a strong dependency path in the dependency graph: A -> B, and now
2204 * we need to decide which usage bit of B conflicts with the usage bits of A,
2205 * i.e. which usage bit of B may introduce safe->unsafe deadlocks.
2206 *
2207 * As above, if only_xr is false, which means A -> B has -(*N)-> dependency
2208 * path, any usage of B should be considered. Otherwise, we should only
2209 * consider _READ usage.
Peter Zijlstra8e182572007-07-19 01:48:54 -07002210 */
Boqun Feng61775ed2020-08-07 15:42:27 +08002211static inline bool usage_match(struct lock_list *entry, void *mask)
Ming Leid7aaba12009-07-16 15:44:29 +02002212{
Boqun Fengf08e3882020-08-07 15:42:30 +08002213 if (!entry->only_xr)
2214 return !!(entry->class->usage_mask & *(unsigned long *)mask);
2215 else /* Mask out _READ usage bits */
2216 return !!((entry->class->usage_mask & LOCKF_IRQ) & *(unsigned long *)mask);
Ming Leid7aaba12009-07-16 15:44:29 +02002217}
2218
Boqun Feng5f296242020-12-10 11:15:00 +01002219static inline bool usage_skip(struct lock_list *entry, void *mask)
2220{
2221 /*
2222 * Skip local_lock() for irq inversion detection.
2223 *
2224 * For !RT, local_lock() is not a real lock, so it won't carry any
2225 * dependency.
2226 *
2227 * For RT, an irq inversion happens when we have lock A and B, and on
2228 * some CPU we can have:
2229 *
2230 * lock(A);
2231 * <interrupted>
2232 * lock(B);
2233 *
2234 * where lock(B) cannot sleep, and we have a dependency B -> ... -> A.
2235 *
2236 * Now we prove local_lock() cannot exist in that dependency. First we
2237 * have the observation for any lock chain L1 -> ... -> Ln, for any
2238 * 1 <= i <= n, Li.inner_wait_type <= L1.inner_wait_type, otherwise
2239 * wait context check will complain. And since B is not a sleep lock,
2240 * therefore B.inner_wait_type >= 2, and since the inner_wait_type of
2241 * local_lock() is 3, which is greater than 2, therefore there is no
2242 * way the local_lock() exists in the dependency B -> ... -> A.
2243 *
2244 * As a result, we will skip local_lock(), when we search for irq
2245 * inversion bugs.
2246 */
2247 if (entry->class->lock_type == LD_LOCK_PERCPU) {
2248 if (DEBUG_LOCKS_WARN_ON(entry->class->wait_type_inner < LD_WAIT_CONFIG))
2249 return false;
2250
2251 return true;
2252 }
2253
2254 return false;
2255}
2256
Peter Zijlstra8e182572007-07-19 01:48:54 -07002257/*
2258 * Find a node in the forwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02002259 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07002260 *
Boqun Fengb11be022020-08-07 15:42:22 +08002261 * Return BFS_MATCH if such a node exists in the subgraph, and put that node
Ming Leid7aaba12009-07-16 15:44:29 +02002262 * into *@target_entry.
Peter Zijlstra8e182572007-07-19 01:48:54 -07002263 */
Boqun Fengb11be022020-08-07 15:42:22 +08002264static enum bfs_result
Frederic Weisbecker627f3642019-04-02 18:02:43 +02002265find_usage_forwards(struct lock_list *root, unsigned long usage_mask,
Ming Leid7aaba12009-07-16 15:44:29 +02002266 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002267{
Boqun Fengb11be022020-08-07 15:42:22 +08002268 enum bfs_result result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002269
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002270 debug_atomic_inc(nr_find_usage_forwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002271
Boqun Feng5f296242020-12-10 11:15:00 +01002272 result = __bfs_forwards(root, &usage_mask, usage_match, usage_skip, target_entry);
Ming Leid7aaba12009-07-16 15:44:29 +02002273
2274 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002275}
2276
2277/*
2278 * Find a node in the backwards-direction dependency sub-graph starting
Ming Leid7aaba12009-07-16 15:44:29 +02002279 * at @root->class that matches @bit.
Peter Zijlstra8e182572007-07-19 01:48:54 -07002280 */
Boqun Fengb11be022020-08-07 15:42:22 +08002281static enum bfs_result
Frederic Weisbecker627f3642019-04-02 18:02:43 +02002282find_usage_backwards(struct lock_list *root, unsigned long usage_mask,
Ming Leid7aaba12009-07-16 15:44:29 +02002283 struct lock_list **target_entry)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002284{
Boqun Fengb11be022020-08-07 15:42:22 +08002285 enum bfs_result result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002286
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02002287 debug_atomic_inc(nr_find_usage_backwards_checks);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002288
Boqun Feng5f296242020-12-10 11:15:00 +01002289 result = __bfs_backwards(root, &usage_mask, usage_match, usage_skip, target_entry);
Dave Jonesf82b2172008-08-11 09:30:23 +02002290
Ming Leid7aaba12009-07-16 15:44:29 +02002291 return result;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002292}
2293
Peter Zijlstraaf012962009-07-16 15:44:29 +02002294static void print_lock_class_header(struct lock_class *class, int depth)
2295{
2296 int bit;
2297
2298 printk("%*s->", depth, "");
2299 print_lock_name(class);
Waiman Long8ca2b56c2018-10-03 13:07:18 -04002300#ifdef CONFIG_DEBUG_LOCKDEP
2301 printk(KERN_CONT " ops: %lu", debug_class_ops_read(class));
2302#endif
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002303 printk(KERN_CONT " {\n");
Peter Zijlstraaf012962009-07-16 15:44:29 +02002304
Peter Zijlstra2bb89452020-09-30 11:49:37 +02002305 for (bit = 0; bit < LOCK_TRACE_STATES; bit++) {
Peter Zijlstraaf012962009-07-16 15:44:29 +02002306 if (class->usage_mask & (1 << bit)) {
2307 int len = depth;
2308
2309 len += printk("%*s %s", depth, "", usage_str[bit]);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002310 len += printk(KERN_CONT " at:\n");
Bart Van Assche12593b72019-07-22 11:24:42 -07002311 print_lock_trace(class->usage_traces[bit], len);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002312 }
2313 }
2314 printk("%*s }\n", depth, "");
2315
Borislav Petkov04860d42018-02-26 14:49:26 +01002316 printk("%*s ... key at: [<%px>] %pS\n",
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002317 depth, "", class->key, class->key);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002318}
2319
2320/*
Boqun Feng69c7a5f2021-06-19 01:01:07 +08002321 * Dependency path printing:
2322 *
2323 * After BFS we get a lock dependency path (linked via ->parent of lock_list),
2324 * printing out each lock in the dependency path will help on understanding how
2325 * the deadlock could happen. Here are some details about dependency path
2326 * printing:
2327 *
2328 * 1) A lock_list can be either forwards or backwards for a lock dependency,
2329 * for a lock dependency A -> B, there are two lock_lists:
2330 *
2331 * a) lock_list in the ->locks_after list of A, whose ->class is B and
2332 * ->links_to is A. In this case, we can say the lock_list is
2333 * "A -> B" (forwards case).
2334 *
2335 * b) lock_list in the ->locks_before list of B, whose ->class is A
2336 * and ->links_to is B. In this case, we can say the lock_list is
2337 * "B <- A" (bacwards case).
2338 *
2339 * The ->trace of both a) and b) point to the call trace where B was
2340 * acquired with A held.
2341 *
2342 * 2) A "helper" lock_list is introduced during BFS, this lock_list doesn't
2343 * represent a certain lock dependency, it only provides an initial entry
2344 * for BFS. For example, BFS may introduce a "helper" lock_list whose
2345 * ->class is A, as a result BFS will search all dependencies starting with
2346 * A, e.g. A -> B or A -> C.
2347 *
2348 * The notation of a forwards helper lock_list is like "-> A", which means
2349 * we should search the forwards dependencies starting with "A", e.g A -> B
2350 * or A -> C.
2351 *
2352 * The notation of a bacwards helper lock_list is like "<- B", which means
2353 * we should search the backwards dependencies ending with "B", e.g.
2354 * B <- A or B <- C.
2355 */
2356
2357/*
2358 * printk the shortest lock dependencies from @root to @leaf in reverse order.
2359 *
2360 * We have a lock dependency path as follow:
2361 *
2362 * @root @leaf
2363 * | |
2364 * V V
2365 * ->parent ->parent
2366 * | lock_list | <--------- | lock_list | ... | lock_list | <--------- | lock_list |
2367 * | -> L1 | | L1 -> L2 | ... |Ln-2 -> Ln-1| | Ln-1 -> Ln|
2368 *
2369 * , so it's natural that we start from @leaf and print every ->class and
2370 * ->trace until we reach the @root.
Peter Zijlstraaf012962009-07-16 15:44:29 +02002371 */
2372static void __used
2373print_shortest_lock_dependencies(struct lock_list *leaf,
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002374 struct lock_list *root)
Peter Zijlstraaf012962009-07-16 15:44:29 +02002375{
2376 struct lock_list *entry = leaf;
2377 int depth;
2378
2379 /*compute depth from generated tree by BFS*/
2380 depth = get_lock_depth(leaf);
2381
2382 do {
2383 print_lock_class_header(entry->class, depth);
2384 printk("%*s ... acquired at:\n", depth, "");
Bart Van Assche12593b72019-07-22 11:24:42 -07002385 print_lock_trace(entry->trace, 2);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002386 printk("\n");
2387
2388 if (depth == 0 && (entry != root)) {
Steven Rostedt6be8c392011-04-20 21:41:58 -04002389 printk("lockdep:%s bad path found in chain graph\n", __func__);
Peter Zijlstraaf012962009-07-16 15:44:29 +02002390 break;
2391 }
2392
2393 entry = get_lock_parent(entry);
2394 depth--;
2395 } while (entry && (depth >= 0));
Peter Zijlstraaf012962009-07-16 15:44:29 +02002396}
Ming Leid7aaba12009-07-16 15:44:29 +02002397
Boqun Feng69c7a5f2021-06-19 01:01:07 +08002398/*
2399 * printk the shortest lock dependencies from @leaf to @root.
2400 *
2401 * We have a lock dependency path (from a backwards search) as follow:
2402 *
2403 * @leaf @root
2404 * | |
2405 * V V
2406 * ->parent ->parent
2407 * | lock_list | ---------> | lock_list | ... | lock_list | ---------> | lock_list |
2408 * | L2 <- L1 | | L3 <- L2 | ... | Ln <- Ln-1 | | <- Ln |
2409 *
2410 * , so when we iterate from @leaf to @root, we actually print the lock
2411 * dependency path L1 -> L2 -> .. -> Ln in the non-reverse order.
2412 *
2413 * Another thing to notice here is that ->class of L2 <- L1 is L1, while the
2414 * ->trace of L2 <- L1 is the call trace of L2, in fact we don't have the call
2415 * trace of L1 in the dependency path, which is alright, because most of the
2416 * time we can figure out where L1 is held from the call trace of L2.
2417 */
2418static void __used
2419print_shortest_lock_dependencies_backwards(struct lock_list *leaf,
2420 struct lock_list *root)
2421{
2422 struct lock_list *entry = leaf;
2423 const struct lock_trace *trace = NULL;
2424 int depth;
2425
2426 /*compute depth from generated tree by BFS*/
2427 depth = get_lock_depth(leaf);
2428
2429 do {
2430 print_lock_class_header(entry->class, depth);
2431 if (trace) {
2432 printk("%*s ... acquired at:\n", depth, "");
2433 print_lock_trace(trace, 2);
2434 printk("\n");
2435 }
2436
2437 /*
2438 * Record the pointer to the trace for the next lock_list
2439 * entry, see the comments for the function.
2440 */
2441 trace = entry->trace;
2442
2443 if (depth == 0 && (entry != root)) {
2444 printk("lockdep:%s bad path found in chain graph\n", __func__);
2445 break;
2446 }
2447
2448 entry = get_lock_parent(entry);
2449 depth--;
2450 } while (entry && (depth >= 0));
2451}
2452
Steven Rostedt3003eba2011-04-20 21:41:54 -04002453static void
2454print_irq_lock_scenario(struct lock_list *safe_entry,
2455 struct lock_list *unsafe_entry,
Steven Rostedtdad3d742011-04-20 21:41:57 -04002456 struct lock_class *prev_class,
2457 struct lock_class *next_class)
Steven Rostedt3003eba2011-04-20 21:41:54 -04002458{
2459 struct lock_class *safe_class = safe_entry->class;
2460 struct lock_class *unsafe_class = unsafe_entry->class;
Steven Rostedtdad3d742011-04-20 21:41:57 -04002461 struct lock_class *middle_class = prev_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04002462
2463 if (middle_class == safe_class)
Steven Rostedtdad3d742011-04-20 21:41:57 -04002464 middle_class = next_class;
Steven Rostedt3003eba2011-04-20 21:41:54 -04002465
2466 /*
2467 * A direct locking problem where unsafe_class lock is taken
2468 * directly by safe_class lock, then all we need to show
2469 * is the deadlock scenario, as it is obvious that the
2470 * unsafe lock is taken under the safe lock.
2471 *
2472 * But if there is a chain instead, where the safe lock takes
2473 * an intermediate lock (middle_class) where this lock is
2474 * not the same as the safe lock, then the lock chain is
2475 * used to describe the problem. Otherwise we would need
2476 * to show a different CPU case for each link in the chain
2477 * from the safe_class lock to the unsafe_class lock.
2478 */
2479 if (middle_class != unsafe_class) {
2480 printk("Chain exists of:\n ");
2481 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002482 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002483 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002484 printk(KERN_CONT " --> ");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002485 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002486 printk(KERN_CONT "\n\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002487 }
2488
2489 printk(" Possible interrupt unsafe locking scenario:\n\n");
2490 printk(" CPU0 CPU1\n");
2491 printk(" ---- ----\n");
2492 printk(" lock(");
2493 __print_lock_name(unsafe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002494 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002495 printk(" local_irq_disable();\n");
2496 printk(" lock(");
2497 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002498 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002499 printk(" lock(");
2500 __print_lock_name(middle_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002501 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002502 printk(" <Interrupt>\n");
2503 printk(" lock(");
2504 __print_lock_name(safe_class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002505 printk(KERN_CONT ");\n");
Steven Rostedt3003eba2011-04-20 21:41:54 -04002506 printk("\n *** DEADLOCK ***\n\n");
2507}
2508
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002509static void
Peter Zijlstra8e182572007-07-19 01:48:54 -07002510print_bad_irq_dependency(struct task_struct *curr,
Ming Lei24208ca2009-07-16 15:44:29 +02002511 struct lock_list *prev_root,
2512 struct lock_list *next_root,
2513 struct lock_list *backwards_entry,
2514 struct lock_list *forwards_entry,
Peter Zijlstra8e182572007-07-19 01:48:54 -07002515 struct held_lock *prev,
2516 struct held_lock *next,
2517 enum lock_usage_bit bit1,
2518 enum lock_usage_bit bit2,
2519 const char *irqclass)
2520{
2521 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002522 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002523
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002524 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002525 pr_warn("=====================================================\n");
2526 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07002527 irqclass, irqclass);
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002528 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002529 pr_warn("-----------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002530 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 -07002531 curr->comm, task_pid_nr(curr),
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02002532 lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT,
Peter Zijlstra8e182572007-07-19 01:48:54 -07002533 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02002534 lockdep_hardirqs_enabled(),
Peter Zijlstra8e182572007-07-19 01:48:54 -07002535 curr->softirqs_enabled);
2536 print_lock(next);
2537
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002538 pr_warn("\nand this task is already holding:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002539 print_lock(prev);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002540 pr_warn("which would create a new lock dependency:\n");
Dave Jonesf82b2172008-08-11 09:30:23 +02002541 print_lock_name(hlock_class(prev));
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002542 pr_cont(" ->");
Dave Jonesf82b2172008-08-11 09:30:23 +02002543 print_lock_name(hlock_class(next));
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002544 pr_cont("\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002545
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002546 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07002547 irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002548 print_lock_name(backwards_entry->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002549 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002550
Bart Van Assche12593b72019-07-22 11:24:42 -07002551 print_lock_trace(backwards_entry->class->usage_traces[bit1], 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002552
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002553 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02002554 print_lock_name(forwards_entry->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002555 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
2556 pr_warn("...");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002557
Bart Van Assche12593b72019-07-22 11:24:42 -07002558 print_lock_trace(forwards_entry->class->usage_traces[bit2], 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002559
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002560 pr_warn("\nother info that might help us debug this:\n\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04002561 print_irq_lock_scenario(backwards_entry, forwards_entry,
2562 hlock_class(prev), hlock_class(next));
Steven Rostedt3003eba2011-04-20 21:41:54 -04002563
Peter Zijlstra8e182572007-07-19 01:48:54 -07002564 lockdep_print_held_locks(curr);
2565
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002566 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
Boqun Feng69c7a5f2021-06-19 01:01:07 +08002567 print_shortest_lock_dependencies_backwards(backwards_entry, prev_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002568
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002569 pr_warn("\nthe dependencies between the lock to be acquired");
2570 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
Bart Van Assche12593b72019-07-22 11:24:42 -07002571 next_root->trace = save_trace();
2572 if (!next_root->trace)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002573 return;
Ming Lei24208ca2009-07-16 15:44:29 +02002574 print_shortest_lock_dependencies(forwards_entry, next_root);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002575
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002576 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002577 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -07002578}
2579
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002580static const char *state_names[] = {
2581#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01002582 __stringify(__STATE),
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002583#include "lockdep_states.h"
2584#undef LOCKDEP_STATE
2585};
2586
2587static const char *state_rnames[] = {
2588#define LOCKDEP_STATE(__STATE) \
Peter Zijlstrab4b136f2009-01-29 14:50:36 +01002589 __stringify(__STATE)"-READ",
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002590#include "lockdep_states.h"
2591#undef LOCKDEP_STATE
2592};
2593
2594static inline const char *state_name(enum lock_usage_bit bit)
2595{
Frederic Weisbeckerc902a1e2019-04-02 18:02:42 +02002596 if (bit & LOCK_USAGE_READ_MASK)
2597 return state_rnames[bit >> LOCK_USAGE_DIR_MASK];
2598 else
2599 return state_names[bit >> LOCK_USAGE_DIR_MASK];
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002600}
2601
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002602/*
2603 * The bit number is encoded like:
2604 *
2605 * bit0: 0 exclusive, 1 read lock
2606 * bit1: 0 used in irq, 1 irq enabled
2607 * bit2-n: state
2608 */
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002609static int exclusive_bit(int new_bit)
2610{
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01002611 int state = new_bit & LOCK_USAGE_STATE_MASK;
2612 int dir = new_bit & LOCK_USAGE_DIR_MASK;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002613
2614 /*
2615 * keep state, bit flip the direction and strip read.
2616 */
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01002617 return state | (dir ^ LOCK_USAGE_DIR_MASK);
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002618}
2619
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002620/*
2621 * Observe that when given a bitmask where each bitnr is encoded as above, a
2622 * right shift of the mask transforms the individual bitnrs as -1 and
2623 * conversely, a left shift transforms into +1 for the individual bitnrs.
2624 *
2625 * So for all bits whose number have LOCK_ENABLED_* set (bitnr1 == 1), we can
2626 * create the mask with those bit numbers using LOCK_USED_IN_* (bitnr1 == 0)
2627 * instead by subtracting the bit number by 2, or shifting the mask right by 2.
2628 *
2629 * Similarly, bitnr1 == 0 becomes bitnr1 == 1 by adding 2, or shifting left 2.
2630 *
2631 * So split the mask (note that LOCKF_ENABLED_IRQ_ALL|LOCKF_USED_IN_IRQ_ALL is
2632 * all bits set) and recompose with bitnr1 flipped.
2633 */
2634static unsigned long invert_dir_mask(unsigned long mask)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002635{
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002636 unsigned long excl = 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002637
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002638 /* Invert dir */
2639 excl |= (mask & LOCKF_ENABLED_IRQ_ALL) >> LOCK_USAGE_DIR_MASK;
2640 excl |= (mask & LOCKF_USED_IN_IRQ_ALL) << LOCK_USAGE_DIR_MASK;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002641
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002642 return excl;
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002643}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002644
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002645/*
Boqun Fengf08e3882020-08-07 15:42:30 +08002646 * Note that a LOCK_ENABLED_IRQ_*_READ usage and a LOCK_USED_IN_IRQ_*_READ
2647 * usage may cause deadlock too, for example:
2648 *
2649 * P1 P2
2650 * <irq disabled>
2651 * write_lock(l1); <irq enabled>
2652 * read_lock(l2);
2653 * write_lock(l2);
2654 * <in irq>
2655 * read_lock(l1);
2656 *
2657 * , in above case, l1 will be marked as LOCK_USED_IN_IRQ_HARDIRQ_READ and l2
2658 * will marked as LOCK_ENABLE_IRQ_HARDIRQ_READ, and this is a possible
2659 * deadlock.
2660 *
2661 * In fact, all of the following cases may cause deadlocks:
2662 *
2663 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*
2664 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*
2665 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*_READ
2666 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*_READ
2667 *
2668 * As a result, to calculate the "exclusive mask", first we invert the
2669 * direction (USED_IN/ENABLED) of the original mask, and 1) for all bits with
2670 * bitnr0 set (LOCK_*_READ), add those with bitnr0 cleared (LOCK_*). 2) for all
2671 * bits with bitnr0 cleared (LOCK_*_READ), add those with bitnr0 set (LOCK_*).
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002672 */
2673static unsigned long exclusive_mask(unsigned long mask)
Peter Zijlstra4f367d8a2009-01-22 18:10:42 +01002674{
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002675 unsigned long excl = invert_dir_mask(mask);
Nick Piggincf40bd12009-01-21 08:12:39 +01002676
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002677 excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK;
Boqun Fengf08e3882020-08-07 15:42:30 +08002678 excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002679
2680 return excl;
2681}
2682
2683/*
2684 * Retrieve the _possible_ original mask to which @mask is
2685 * exclusive. Ie: this is the opposite of exclusive_mask().
2686 * Note that 2 possible original bits can match an exclusive
2687 * bit: one has LOCK_USAGE_READ_MASK set, the other has it
2688 * cleared. So both are returned for each exclusive bit.
2689 */
2690static unsigned long original_mask(unsigned long mask)
2691{
2692 unsigned long excl = invert_dir_mask(mask);
2693
2694 /* Include read in existing usages */
Boqun Fengf08e3882020-08-07 15:42:30 +08002695 excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002696 excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK;
2697
2698 return excl;
2699}
2700
2701/*
2702 * Find the first pair of bit match between an original
2703 * usage mask and an exclusive usage mask.
2704 */
2705static int find_exclusive_match(unsigned long mask,
2706 unsigned long excl_mask,
2707 enum lock_usage_bit *bitp,
2708 enum lock_usage_bit *excl_bitp)
2709{
Boqun Fengf08e3882020-08-07 15:42:30 +08002710 int bit, excl, excl_read;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002711
2712 for_each_set_bit(bit, &mask, LOCK_USED) {
Boqun Fengf08e3882020-08-07 15:42:30 +08002713 /*
2714 * exclusive_bit() strips the read bit, however,
2715 * LOCK_ENABLED_IRQ_*_READ may cause deadlocks too, so we need
2716 * to search excl | LOCK_USAGE_READ_MASK as well.
2717 */
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002718 excl = exclusive_bit(bit);
Boqun Fengf08e3882020-08-07 15:42:30 +08002719 excl_read = excl | LOCK_USAGE_READ_MASK;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002720 if (excl_mask & lock_flag(excl)) {
2721 *bitp = bit;
2722 *excl_bitp = excl;
2723 return 0;
Boqun Fengf08e3882020-08-07 15:42:30 +08002724 } else if (excl_mask & lock_flag(excl_read)) {
2725 *bitp = bit;
2726 *excl_bitp = excl_read;
2727 return 0;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002728 }
2729 }
2730 return -1;
2731}
2732
2733/*
2734 * Prove that the new dependency does not connect a hardirq-safe(-read)
2735 * lock with a hardirq-unsafe lock - to achieve this we search
2736 * the backwards-subgraph starting at <prev>, and the
2737 * forwards-subgraph starting at <next>:
2738 */
2739static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
2740 struct held_lock *next)
2741{
2742 unsigned long usage_mask = 0, forward_mask, backward_mask;
2743 enum lock_usage_bit forward_bit = 0, backward_bit = 0;
Kees Cook3f649ab2020-06-03 13:09:38 -07002744 struct lock_list *target_entry1;
2745 struct lock_list *target_entry;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002746 struct lock_list this, that;
Boqun Fengb11be022020-08-07 15:42:22 +08002747 enum bfs_result ret;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002748
2749 /*
2750 * Step 1: gather all hard/soft IRQs usages backward in an
2751 * accumulated usage mask.
2752 */
Boqun Fengf08e3882020-08-07 15:42:30 +08002753 bfs_init_rootb(&this, prev);
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002754
Boqun Feng5f296242020-12-10 11:15:00 +01002755 ret = __bfs_backwards(&this, &usage_mask, usage_accumulate, usage_skip, NULL);
Boqun Fengb11be022020-08-07 15:42:22 +08002756 if (bfs_error(ret)) {
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002757 print_bfs_bug(ret);
2758 return 0;
2759 }
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002760
2761 usage_mask &= LOCKF_USED_IN_IRQ_ALL;
2762 if (!usage_mask)
2763 return 1;
2764
2765 /*
2766 * Step 2: find exclusive uses forward that match the previous
2767 * backward accumulated mask.
2768 */
2769 forward_mask = exclusive_mask(usage_mask);
2770
Boqun Fengf08e3882020-08-07 15:42:30 +08002771 bfs_init_root(&that, next);
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002772
2773 ret = find_usage_forwards(&that, forward_mask, &target_entry1);
Boqun Fengb11be022020-08-07 15:42:22 +08002774 if (bfs_error(ret)) {
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002775 print_bfs_bug(ret);
2776 return 0;
2777 }
Boqun Fengb11be022020-08-07 15:42:22 +08002778 if (ret == BFS_RNOMATCH)
2779 return 1;
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002780
2781 /*
2782 * Step 3: we found a bad match! Now retrieve a lock from the backward
2783 * list whose usage mask matches the exclusive usage mask from the
2784 * lock found on the forward list.
Boqun Feng7b1f8c612021-06-19 01:01:09 +08002785 *
2786 * Note, we should only keep the LOCKF_ENABLED_IRQ_ALL bits, considering
2787 * the follow case:
2788 *
2789 * When trying to add A -> B to the graph, we find that there is a
2790 * hardirq-safe L, that L -> ... -> A, and another hardirq-unsafe M,
2791 * that B -> ... -> M. However M is **softirq-safe**, if we use exact
2792 * invert bits of M's usage_mask, we will find another lock N that is
2793 * **softirq-unsafe** and N -> ... -> A, however N -> .. -> M will not
2794 * cause a inversion deadlock.
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002795 */
Boqun Feng7b1f8c612021-06-19 01:01:09 +08002796 backward_mask = original_mask(target_entry1->class->usage_mask & LOCKF_ENABLED_IRQ_ALL);
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002797
2798 ret = find_usage_backwards(&this, backward_mask, &target_entry);
Boqun Fengb11be022020-08-07 15:42:22 +08002799 if (bfs_error(ret)) {
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002800 print_bfs_bug(ret);
2801 return 0;
2802 }
Boqun Fengb11be022020-08-07 15:42:22 +08002803 if (DEBUG_LOCKS_WARN_ON(ret == BFS_RNOMATCH))
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002804 return 1;
2805
2806 /*
2807 * Step 4: narrow down to a pair of incompatible usage bits
2808 * and report it.
2809 */
2810 ret = find_exclusive_match(target_entry->class->usage_mask,
2811 target_entry1->class->usage_mask,
2812 &backward_bit, &forward_bit);
2813 if (DEBUG_LOCKS_WARN_ON(ret == -1))
2814 return 1;
2815
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002816 print_bad_irq_dependency(curr, &this, &that,
2817 target_entry, target_entry1,
2818 prev, next,
2819 backward_bit, forward_bit,
2820 state_name(backward_bit));
2821
2822 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002823}
2824
Peter Zijlstra8e182572007-07-19 01:48:54 -07002825#else
2826
Frederic Weisbecker948f8372019-04-02 18:02:44 +02002827static inline int check_irq_usage(struct task_struct *curr,
2828 struct held_lock *prev, struct held_lock *next)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002829{
2830 return 1;
2831}
Boqun Feng5f296242020-12-10 11:15:00 +01002832
2833static inline bool usage_skip(struct lock_list *entry, void *mask)
2834{
2835 return false;
2836}
2837
Waiman Longb3b9c182020-02-06 10:24:03 -05002838#endif /* CONFIG_TRACE_IRQFLAGS */
Peter Zijlstra8e182572007-07-19 01:48:54 -07002839
Peter Zijlstra175b1a62020-12-10 11:16:34 +01002840#ifdef CONFIG_LOCKDEP_SMALL
2841/*
2842 * Check that the dependency graph starting at <src> can lead to
2843 * <target> or not. If it can, <src> -> <target> dependency is already
2844 * in the graph.
2845 *
Xiongwei Song0e8a89d2021-06-18 21:02:30 +08002846 * Return BFS_RMATCH if it does, or BFS_RNOMATCH if it does not, return BFS_E* if
Peter Zijlstra175b1a62020-12-10 11:16:34 +01002847 * any error appears in the bfs search.
2848 */
2849static noinline enum bfs_result
2850check_redundant(struct held_lock *src, struct held_lock *target)
2851{
2852 enum bfs_result ret;
2853 struct lock_list *target_entry;
2854 struct lock_list src_entry;
2855
2856 bfs_init_root(&src_entry, src);
2857 /*
2858 * Special setup for check_redundant().
2859 *
2860 * To report redundant, we need to find a strong dependency path that
2861 * is equal to or stronger than <src> -> <target>. So if <src> is E,
2862 * we need to let __bfs() only search for a path starting at a -(E*)->,
2863 * we achieve this by setting the initial node's ->only_xr to true in
2864 * that case. And if <prev> is S, we set initial ->only_xr to false
2865 * because both -(S*)-> (equal) and -(E*)-> (stronger) are redundant.
2866 */
2867 src_entry.only_xr = src->read == 0;
2868
2869 debug_atomic_inc(nr_redundant_checks);
2870
Boqun Feng5f296242020-12-10 11:15:00 +01002871 /*
2872 * Note: we skip local_lock() for redundant check, because as the
2873 * comment in usage_skip(), A -> local_lock() -> B and A -> B are not
2874 * the same.
2875 */
2876 ret = check_path(target, &src_entry, hlock_equal, usage_skip, &target_entry);
Peter Zijlstra175b1a62020-12-10 11:16:34 +01002877
2878 if (ret == BFS_RMATCH)
2879 debug_atomic_inc(nr_redundant);
2880
2881 return ret;
2882}
2883
2884#else
2885
2886static inline enum bfs_result
2887check_redundant(struct held_lock *src, struct held_lock *target)
2888{
2889 return BFS_RNOMATCH;
2890}
2891
2892#endif
2893
Waiman Longb3b9c182020-02-06 10:24:03 -05002894static void inc_chains(int irq_context)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002895{
Waiman Longb3b9c182020-02-06 10:24:03 -05002896 if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT)
2897 nr_hardirq_chains++;
2898 else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT)
2899 nr_softirq_chains++;
2900 else
2901 nr_process_chains++;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002902}
2903
Waiman Longb3b9c182020-02-06 10:24:03 -05002904static void dec_chains(int irq_context)
2905{
2906 if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT)
2907 nr_hardirq_chains--;
2908 else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT)
2909 nr_softirq_chains--;
2910 else
2911 nr_process_chains--;
2912}
Peter Zijlstra8e182572007-07-19 01:48:54 -07002913
Steven Rostedt48702ec2011-04-20 21:41:56 -04002914static void
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002915print_deadlock_scenario(struct held_lock *nxt, struct held_lock *prv)
Steven Rostedt48702ec2011-04-20 21:41:56 -04002916{
2917 struct lock_class *next = hlock_class(nxt);
2918 struct lock_class *prev = hlock_class(prv);
2919
2920 printk(" Possible unsafe locking scenario:\n\n");
2921 printk(" CPU0\n");
2922 printk(" ----\n");
2923 printk(" lock(");
2924 __print_lock_name(prev);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002925 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04002926 printk(" lock(");
2927 __print_lock_name(next);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01002928 printk(KERN_CONT ");\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04002929 printk("\n *** DEADLOCK ***\n\n");
2930 printk(" May be due to missing lock nesting notation\n\n");
2931}
2932
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002933static void
Peter Zijlstra8e182572007-07-19 01:48:54 -07002934print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
2935 struct held_lock *next)
2936{
2937 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002938 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002939
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002940 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002941 pr_warn("============================================\n");
2942 pr_warn("WARNING: possible recursive locking detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01002943 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08002944 pr_warn("--------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002945 pr_warn("%s/%d is trying to acquire lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002946 curr->comm, task_pid_nr(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07002947 print_lock(next);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002948 pr_warn("\nbut task is already holding lock:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002949 print_lock(prev);
2950
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002951 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedt48702ec2011-04-20 21:41:56 -04002952 print_deadlock_scenario(next, prev);
Peter Zijlstra8e182572007-07-19 01:48:54 -07002953 lockdep_print_held_locks(curr);
2954
Paul E. McKenney681fbec2017-05-04 15:44:38 -07002955 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07002956 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -07002957}
2958
2959/*
2960 * Check whether we are holding such a class already.
2961 *
2962 * (Note that this has to be done separately, because the graph cannot
2963 * detect such classes of deadlocks.)
2964 *
Boqun Fengd61fc96a2020-11-02 13:37:41 +08002965 * Returns: 0 on deadlock detected, 1 on OK, 2 if another lock with the same
2966 * lock class is held but nest_lock is also held, i.e. we rely on the
2967 * nest_lock to avoid the deadlock.
Peter Zijlstra8e182572007-07-19 01:48:54 -07002968 */
2969static int
Yuyang Du4609c4f2019-05-06 16:19:33 +08002970check_deadlock(struct task_struct *curr, struct held_lock *next)
Peter Zijlstra8e182572007-07-19 01:48:54 -07002971{
2972 struct held_lock *prev;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002973 struct held_lock *nest = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07002974 int i;
2975
2976 for (i = 0; i < curr->lockdep_depth; i++) {
2977 prev = curr->held_locks + i;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002978
2979 if (prev->instance == next->nest_lock)
2980 nest = prev;
2981
Dave Jonesf82b2172008-08-11 09:30:23 +02002982 if (hlock_class(prev) != hlock_class(next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07002983 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002984
Peter Zijlstra8e182572007-07-19 01:48:54 -07002985 /*
2986 * Allow read-after-read recursion of the same
2987 * lock class (i.e. read_lock(lock)+read_lock(lock)):
2988 */
Yuyang Du4609c4f2019-05-06 16:19:33 +08002989 if ((next->read == 2) && prev->read)
Boqun Fengd61fc96a2020-11-02 13:37:41 +08002990 continue;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02002991
2992 /*
2993 * We're holding the nest_lock, which serializes this lock's
2994 * nesting behaviour.
2995 */
2996 if (nest)
2997 return 2;
2998
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08002999 print_deadlock_bug(curr, prev, next);
3000 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003001 }
3002 return 1;
3003}
3004
3005/*
3006 * There was a chain-cache miss, and we are about to add a new dependency
Yuyang Du154f1852019-05-06 16:19:31 +08003007 * to a previous lock. We validate the following rules:
Peter Zijlstra8e182572007-07-19 01:48:54 -07003008 *
3009 * - would the adding of the <prev> -> <next> dependency create a
3010 * circular dependency in the graph? [== circular deadlock]
3011 *
3012 * - does the new prev->next dependency connect any hardirq-safe lock
3013 * (in the full backwards-subgraph starting at <prev>) with any
3014 * hardirq-unsafe lock (in the full forwards-subgraph starting at
3015 * <next>)? [== illegal lock inversion with hardirq contexts]
3016 *
3017 * - does the new prev->next dependency connect any softirq-safe lock
3018 * (in the full backwards-subgraph starting at <prev>) with any
3019 * softirq-unsafe lock (in the full forwards-subgraph starting at
3020 * <next>)? [== illegal lock inversion with softirq contexts]
3021 *
3022 * any of these scenarios could lead to a deadlock.
3023 *
3024 * Then if all the validations pass, we add the forwards and backwards
3025 * dependency.
3026 */
3027static int
3028check_prev_add(struct task_struct *curr, struct held_lock *prev,
Boqun Fengbd76eca2020-08-07 15:42:24 +08003029 struct held_lock *next, u16 distance,
Bart Van Assche12593b72019-07-22 11:24:42 -07003030 struct lock_trace **const trace)
Peter Zijlstra8e182572007-07-19 01:48:54 -07003031{
Peter Zijlstra8b405d52017-10-04 11:13:37 +02003032 struct lock_list *entry;
Boqun Fengb11be022020-08-07 15:42:22 +08003033 enum bfs_result ret;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003034
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08003035 if (!hlock_class(prev)->key || !hlock_class(next)->key) {
3036 /*
3037 * The warning statements below may trigger a use-after-free
3038 * of the class name. It is better to trigger a use-after free
3039 * and to have the class name most of the time instead of not
3040 * having the class name available.
3041 */
3042 WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key,
3043 "Detected use-after-free of lock class %px/%s\n",
3044 hlock_class(prev),
3045 hlock_class(prev)->name);
3046 WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key,
3047 "Detected use-after-free of lock class %px/%s\n",
3048 hlock_class(next),
3049 hlock_class(next)->name);
3050 return 2;
3051 }
3052
Peter Zijlstra8e182572007-07-19 01:48:54 -07003053 /*
3054 * Prove that the new <prev> -> <next> dependency would not
3055 * create a circular dependency in the graph. (We do this by
Yuyang Du154f1852019-05-06 16:19:31 +08003056 * a breadth-first search into the graph starting at <next>,
3057 * and check whether we can reach <prev>.)
Peter Zijlstra8e182572007-07-19 01:48:54 -07003058 *
Yuyang Du154f1852019-05-06 16:19:31 +08003059 * The search is limited by the size of the circular queue (i.e.,
3060 * MAX_CIRCULAR_QUEUE_SIZE) which keeps track of a breadth of nodes
3061 * in the graph whose neighbours are to be checked.
Peter Zijlstra8e182572007-07-19 01:48:54 -07003062 */
Yuyang Du8c2c2b42019-05-06 16:19:35 +08003063 ret = check_noncircular(next, prev, trace);
Boqun Fengb11be022020-08-07 15:42:22 +08003064 if (unlikely(bfs_error(ret) || ret == BFS_RMATCH))
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003065 return 0;
Ming Leic94aa5c2009-07-16 15:44:29 +02003066
Frederic Weisbecker948f8372019-04-02 18:02:44 +02003067 if (!check_irq_usage(curr, prev, next))
Peter Zijlstra8e182572007-07-19 01:48:54 -07003068 return 0;
3069
3070 /*
Peter Zijlstra8e182572007-07-19 01:48:54 -07003071 * Is the <prev> -> <next> dependency already present?
3072 *
3073 * (this may occur even though this is a new chain: consider
3074 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
3075 * chains - the second one will be new, but L1 already has
3076 * L2 added to its dependency list, due to the first chain.)
3077 */
Dave Jonesf82b2172008-08-11 09:30:23 +02003078 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
3079 if (entry->class == hlock_class(next)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07003080 if (distance == 1)
3081 entry->distance = 1;
Boqun Feng3454a362020-08-07 15:42:25 +08003082 entry->dep |= calc_dep(prev, next);
3083
3084 /*
3085 * Also, update the reverse dependency in @next's
3086 * ->locks_before list.
3087 *
3088 * Here we reuse @entry as the cursor, which is fine
3089 * because we won't go to the next iteration of the
3090 * outer loop:
3091 *
3092 * For normal cases, we return in the inner loop.
3093 *
3094 * If we fail to return, we have inconsistency, i.e.
3095 * <prev>::locks_after contains <next> while
3096 * <next>::locks_before doesn't contain <prev>. In
3097 * that case, we return after the inner and indicate
3098 * something is wrong.
3099 */
3100 list_for_each_entry(entry, &hlock_class(next)->locks_before, entry) {
3101 if (entry->class == hlock_class(prev)) {
3102 if (distance == 1)
3103 entry->distance = 1;
3104 entry->dep |= calc_depb(prev, next);
3105 return 1;
3106 }
3107 }
3108
3109 /* <prev> is not found in <next>::locks_before */
3110 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003111 }
3112 }
3113
Peter Zijlstraae813302017-03-03 10:13:38 +01003114 /*
3115 * Is the <prev> -> <next> link redundant?
3116 */
Yuyang Du8c2c2b42019-05-06 16:19:35 +08003117 ret = check_redundant(prev, next);
Boqun Fengb11be022020-08-07 15:42:22 +08003118 if (bfs_error(ret))
3119 return 0;
3120 else if (ret == BFS_RMATCH)
3121 return 2;
Peter Zijlstraae813302017-03-03 10:13:38 +01003122
Bart Van Assche12593b72019-07-22 11:24:42 -07003123 if (!*trace) {
3124 *trace = save_trace();
3125 if (!*trace)
3126 return 0;
3127 }
Yong Zhang4726f2a2010-05-04 14:16:48 +08003128
Peter Zijlstra8e182572007-07-19 01:48:54 -07003129 /*
3130 * Ok, all validations passed, add the new lock
3131 * to the previous lock's dependency list:
3132 */
Bart Van Assche86cffb82019-02-14 15:00:41 -08003133 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
Dave Jonesf82b2172008-08-11 09:30:23 +02003134 &hlock_class(prev)->locks_after,
Boqun Feng3454a362020-08-07 15:42:25 +08003135 next->acquire_ip, distance,
3136 calc_dep(prev, next),
3137 *trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07003138
3139 if (!ret)
3140 return 0;
3141
Bart Van Assche86cffb82019-02-14 15:00:41 -08003142 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
Dave Jonesf82b2172008-08-11 09:30:23 +02003143 &hlock_class(next)->locks_before,
Boqun Feng3454a362020-08-07 15:42:25 +08003144 next->acquire_ip, distance,
3145 calc_depb(prev, next),
3146 *trace);
Peter Zijlstra8e182572007-07-19 01:48:54 -07003147 if (!ret)
3148 return 0;
3149
Byungchul Park70911fd2017-08-07 16:12:50 +09003150 return 2;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003151}
3152
3153/*
3154 * Add the dependency to all directly-previous locks that are 'relevant'.
3155 * The ones that are relevant are (in increasing distance from curr):
3156 * all consecutive trylock entries and the final non-trylock entry - or
3157 * the end of this context's lock-chain - whichever comes first.
3158 */
3159static int
3160check_prevs_add(struct task_struct *curr, struct held_lock *next)
3161{
Bart Van Assche12593b72019-07-22 11:24:42 -07003162 struct lock_trace *trace = NULL;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003163 int depth = curr->lockdep_depth;
3164 struct held_lock *hlock;
3165
3166 /*
3167 * Debugging checks.
3168 *
3169 * Depth must not be zero for a non-head lock:
3170 */
3171 if (!depth)
3172 goto out_bug;
3173 /*
3174 * At least two relevant locks must exist for this
3175 * to be a head:
3176 */
3177 if (curr->held_locks[depth].irq_context !=
3178 curr->held_locks[depth-1].irq_context)
3179 goto out_bug;
3180
3181 for (;;) {
Boqun Fengbd76eca2020-08-07 15:42:24 +08003182 u16 distance = curr->lockdep_depth - depth + 1;
Oleg Nesterov1b5ff812014-01-20 19:20:10 +01003183 hlock = curr->held_locks + depth - 1;
Byungchul Parkce07a9412017-08-07 16:12:51 +09003184
Boqun Feng621c9da2020-08-07 15:42:31 +08003185 if (hlock->check) {
3186 int ret = check_prev_add(curr, hlock, next, distance, &trace);
Ingo Molnare966eae2017-12-12 12:31:16 +01003187 if (!ret)
3188 return 0;
3189
3190 /*
3191 * Stop after the first non-trylock entry,
3192 * as non-trylock entries have added their
3193 * own direct dependencies already, so this
3194 * lock is connected to them indirectly:
3195 */
3196 if (!hlock->trylock)
3197 break;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003198 }
Ingo Molnare966eae2017-12-12 12:31:16 +01003199
Peter Zijlstra8e182572007-07-19 01:48:54 -07003200 depth--;
3201 /*
3202 * End of lock-stack?
3203 */
3204 if (!depth)
3205 break;
3206 /*
3207 * Stop the search if we cross into another context:
3208 */
3209 if (curr->held_locks[depth].irq_context !=
3210 curr->held_locks[depth-1].irq_context)
3211 break;
3212 }
3213 return 1;
3214out_bug:
3215 if (!debug_locks_off_graph_unlock())
3216 return 0;
3217
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003218 /*
3219 * Clearly we all shouldn't be here, but since we made it we
3220 * can reliable say we messed up our state. See the above two
3221 * gotos for reasons why we could possibly end up here.
3222 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003223 WARN_ON(1);
3224
3225 return 0;
3226}
3227
Huang, Ying443cd502008-06-20 16:39:21 +08003228struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
Bart Van Asschede4643a2019-02-14 15:00:50 -08003229static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS);
Huang, Ying443cd502008-06-20 16:39:21 +08003230static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
Waiman Long797b82eb2020-02-06 10:24:07 -05003231unsigned long nr_zapped_lock_chains;
Waiman Long810507f2020-02-06 10:24:08 -05003232unsigned int nr_free_chain_hlocks; /* Free chain_hlocks in buckets */
3233unsigned int nr_lost_chain_hlocks; /* Lost chain_hlocks */
3234unsigned int nr_large_chain_blocks; /* size > MAX_CHAIN_BUCKETS */
3235
3236/*
3237 * The first 2 chain_hlocks entries in the chain block in the bucket
3238 * list contains the following meta data:
3239 *
3240 * entry[0]:
3241 * Bit 15 - always set to 1 (it is not a class index)
3242 * Bits 0-14 - upper 15 bits of the next block index
3243 * entry[1] - lower 16 bits of next block index
3244 *
3245 * A next block index of all 1 bits means it is the end of the list.
3246 *
3247 * On the unsized bucket (bucket-0), the 3rd and 4th entries contain
3248 * the chain block size:
3249 *
3250 * entry[2] - upper 16 bits of the chain block size
3251 * entry[3] - lower 16 bits of the chain block size
3252 */
3253#define MAX_CHAIN_BUCKETS 16
3254#define CHAIN_BLK_FLAG (1U << 15)
3255#define CHAIN_BLK_LIST_END 0xFFFFU
3256
3257static int chain_block_buckets[MAX_CHAIN_BUCKETS];
3258
3259static inline int size_to_bucket(int size)
3260{
3261 if (size > MAX_CHAIN_BUCKETS)
3262 return 0;
3263
3264 return size - 1;
3265}
3266
3267/*
3268 * Iterate all the chain blocks in a bucket.
3269 */
3270#define for_each_chain_block(bucket, prev, curr) \
3271 for ((prev) = -1, (curr) = chain_block_buckets[bucket]; \
3272 (curr) >= 0; \
3273 (prev) = (curr), (curr) = chain_block_next(curr))
3274
3275/*
3276 * next block or -1
3277 */
3278static inline int chain_block_next(int offset)
3279{
3280 int next = chain_hlocks[offset];
3281
3282 WARN_ON_ONCE(!(next & CHAIN_BLK_FLAG));
3283
3284 if (next == CHAIN_BLK_LIST_END)
3285 return -1;
3286
3287 next &= ~CHAIN_BLK_FLAG;
3288 next <<= 16;
3289 next |= chain_hlocks[offset + 1];
3290
3291 return next;
3292}
3293
3294/*
3295 * bucket-0 only
3296 */
3297static inline int chain_block_size(int offset)
3298{
3299 return (chain_hlocks[offset + 2] << 16) | chain_hlocks[offset + 3];
3300}
3301
3302static inline void init_chain_block(int offset, int next, int bucket, int size)
3303{
3304 chain_hlocks[offset] = (next >> 16) | CHAIN_BLK_FLAG;
3305 chain_hlocks[offset + 1] = (u16)next;
3306
3307 if (size && !bucket) {
3308 chain_hlocks[offset + 2] = size >> 16;
3309 chain_hlocks[offset + 3] = (u16)size;
3310 }
3311}
3312
3313static inline void add_chain_block(int offset, int size)
3314{
3315 int bucket = size_to_bucket(size);
3316 int next = chain_block_buckets[bucket];
3317 int prev, curr;
3318
3319 if (unlikely(size < 2)) {
3320 /*
3321 * We can't store single entries on the freelist. Leak them.
3322 *
3323 * One possible way out would be to uniquely mark them, other
3324 * than with CHAIN_BLK_FLAG, such that we can recover them when
3325 * the block before it is re-added.
3326 */
3327 if (size)
3328 nr_lost_chain_hlocks++;
3329 return;
3330 }
3331
3332 nr_free_chain_hlocks += size;
3333 if (!bucket) {
3334 nr_large_chain_blocks++;
3335
3336 /*
3337 * Variable sized, sort large to small.
3338 */
3339 for_each_chain_block(0, prev, curr) {
3340 if (size >= chain_block_size(curr))
3341 break;
3342 }
3343 init_chain_block(offset, curr, 0, size);
3344 if (prev < 0)
3345 chain_block_buckets[0] = offset;
3346 else
3347 init_chain_block(prev, offset, 0, 0);
3348 return;
3349 }
3350 /*
3351 * Fixed size, add to head.
3352 */
3353 init_chain_block(offset, next, bucket, size);
3354 chain_block_buckets[bucket] = offset;
3355}
3356
3357/*
3358 * Only the first block in the list can be deleted.
3359 *
3360 * For the variable size bucket[0], the first block (the largest one) is
3361 * returned, broken up and put back into the pool. So if a chain block of
3362 * length > MAX_CHAIN_BUCKETS is ever used and zapped, it will just be
3363 * queued up after the primordial chain block and never be used until the
3364 * hlock entries in the primordial chain block is almost used up. That
3365 * causes fragmentation and reduce allocation efficiency. That can be
3366 * monitored by looking at the "large chain blocks" number in lockdep_stats.
3367 */
3368static inline void del_chain_block(int bucket, int size, int next)
3369{
3370 nr_free_chain_hlocks -= size;
3371 chain_block_buckets[bucket] = next;
3372
3373 if (!bucket)
3374 nr_large_chain_blocks--;
3375}
3376
3377static void init_chain_block_buckets(void)
3378{
3379 int i;
3380
3381 for (i = 0; i < MAX_CHAIN_BUCKETS; i++)
3382 chain_block_buckets[i] = -1;
3383
3384 add_chain_block(0, ARRAY_SIZE(chain_hlocks));
3385}
3386
3387/*
3388 * Return offset of a chain block of the right size or -1 if not found.
3389 *
3390 * Fairly simple worst-fit allocator with the addition of a number of size
3391 * specific free lists.
3392 */
3393static int alloc_chain_hlocks(int req)
3394{
3395 int bucket, curr, size;
3396
3397 /*
3398 * We rely on the MSB to act as an escape bit to denote freelist
3399 * pointers. Make sure this bit isn't set in 'normal' class_idx usage.
3400 */
3401 BUILD_BUG_ON((MAX_LOCKDEP_KEYS-1) & CHAIN_BLK_FLAG);
3402
3403 init_data_structures_once();
3404
3405 if (nr_free_chain_hlocks < req)
3406 return -1;
3407
3408 /*
3409 * We require a minimum of 2 (u16) entries to encode a freelist
3410 * 'pointer'.
3411 */
3412 req = max(req, 2);
3413 bucket = size_to_bucket(req);
3414 curr = chain_block_buckets[bucket];
3415
3416 if (bucket) {
3417 if (curr >= 0) {
3418 del_chain_block(bucket, req, chain_block_next(curr));
3419 return curr;
3420 }
3421 /* Try bucket 0 */
3422 curr = chain_block_buckets[0];
3423 }
3424
3425 /*
3426 * The variable sized freelist is sorted by size; the first entry is
3427 * the largest. Use it if it fits.
3428 */
3429 if (curr >= 0) {
3430 size = chain_block_size(curr);
3431 if (likely(size >= req)) {
3432 del_chain_block(0, size, chain_block_next(curr));
3433 add_chain_block(curr + req, size - req);
3434 return curr;
3435 }
3436 }
3437
3438 /*
3439 * Last resort, split a block in a larger sized bucket.
3440 */
3441 for (size = MAX_CHAIN_BUCKETS; size > req; size--) {
3442 bucket = size_to_bucket(size);
3443 curr = chain_block_buckets[bucket];
3444 if (curr < 0)
3445 continue;
3446
3447 del_chain_block(bucket, size, chain_block_next(curr));
3448 add_chain_block(curr + req, size - req);
3449 return curr;
3450 }
3451
3452 return -1;
3453}
3454
3455static inline void free_chain_hlocks(int base, int size)
3456{
3457 add_chain_block(base, max(size, 2));
3458}
Huang, Ying443cd502008-06-20 16:39:21 +08003459
3460struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
3461{
Boqun Fengf611e8c2020-08-07 15:42:33 +08003462 u16 chain_hlock = chain_hlocks[chain->base + i];
3463 unsigned int class_idx = chain_hlock_class_idx(chain_hlock);
3464
3465 return lock_classes + class_idx - 1;
Huang, Ying443cd502008-06-20 16:39:21 +08003466}
Peter Zijlstra8e182572007-07-19 01:48:54 -07003467
3468/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003469 * Returns the index of the first held_lock of the current chain
3470 */
3471static inline int get_first_held_lock(struct task_struct *curr,
3472 struct held_lock *hlock)
3473{
3474 int i;
3475 struct held_lock *hlock_curr;
3476
3477 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
3478 hlock_curr = curr->held_locks + i;
3479 if (hlock_curr->irq_context != hlock->irq_context)
3480 break;
3481
3482 }
3483
3484 return ++i;
3485}
3486
Borislav Petkov5c8a0102016-04-04 10:42:07 +02003487#ifdef CONFIG_DEBUG_LOCKDEP
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003488/*
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003489 * Returns the next chain_key iteration
3490 */
Boqun Fengf611e8c2020-08-07 15:42:33 +08003491static u64 print_chain_key_iteration(u16 hlock_id, u64 chain_key)
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003492{
Boqun Fengf611e8c2020-08-07 15:42:33 +08003493 u64 new_chain_key = iterate_chain_key(chain_key, hlock_id);
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003494
Boqun Fengf611e8c2020-08-07 15:42:33 +08003495 printk(" hlock_id:%d -> chain_key:%016Lx",
3496 (unsigned int)hlock_id,
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003497 (unsigned long long)new_chain_key);
3498 return new_chain_key;
3499}
3500
3501static void
3502print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
3503{
3504 struct held_lock *hlock;
Yuyang Duf6ec8822019-05-06 16:19:24 +08003505 u64 chain_key = INITIAL_CHAIN_KEY;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003506 int depth = curr->lockdep_depth;
Yuyang Du834494b2019-05-06 16:19:21 +08003507 int i = get_first_held_lock(curr, hlock_next);
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003508
Yuyang Du834494b2019-05-06 16:19:21 +08003509 printk("depth: %u (irq_context %u)\n", depth - i + 1,
3510 hlock_next->irq_context);
3511 for (; i < depth; i++) {
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003512 hlock = curr->held_locks + i;
Boqun Fengf611e8c2020-08-07 15:42:33 +08003513 chain_key = print_chain_key_iteration(hlock_id(hlock), chain_key);
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003514
3515 print_lock(hlock);
3516 }
3517
Boqun Fengf611e8c2020-08-07 15:42:33 +08003518 print_chain_key_iteration(hlock_id(hlock_next), chain_key);
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003519 print_lock(hlock_next);
3520}
3521
3522static void print_chain_keys_chain(struct lock_chain *chain)
3523{
3524 int i;
Yuyang Duf6ec8822019-05-06 16:19:24 +08003525 u64 chain_key = INITIAL_CHAIN_KEY;
Boqun Fengf611e8c2020-08-07 15:42:33 +08003526 u16 hlock_id;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003527
3528 printk("depth: %u\n", chain->depth);
3529 for (i = 0; i < chain->depth; i++) {
Boqun Fengf611e8c2020-08-07 15:42:33 +08003530 hlock_id = chain_hlocks[chain->base + i];
3531 chain_key = print_chain_key_iteration(hlock_id, chain_key);
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003532
Boqun Fengf611e8c2020-08-07 15:42:33 +08003533 print_lock_name(lock_classes + chain_hlock_class_idx(hlock_id) - 1);
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003534 printk("\n");
3535 }
3536}
3537
3538static void print_collision(struct task_struct *curr,
3539 struct held_lock *hlock_next,
3540 struct lock_chain *chain)
3541{
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003542 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003543 pr_warn("============================\n");
3544 pr_warn("WARNING: chain_key collision\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003545 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003546 pr_warn("----------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003547 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
3548 pr_warn("Hash chain already cached but the contents don't match!\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003549
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003550 pr_warn("Held locks:");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003551 print_chain_keys_held_locks(curr, hlock_next);
3552
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003553 pr_warn("Locks in cached chain:");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003554 print_chain_keys_chain(chain);
3555
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003556 pr_warn("\nstack backtrace:\n");
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003557 dump_stack();
3558}
Borislav Petkov5c8a0102016-04-04 10:42:07 +02003559#endif
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003560
3561/*
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003562 * Checks whether the chain and the current held locks are consistent
3563 * in depth and also in content. If they are not it most likely means
3564 * that there was a collision during the calculation of the chain_key.
3565 * Returns: 0 not passed, 1 passed
3566 */
3567static int check_no_collision(struct task_struct *curr,
3568 struct held_lock *hlock,
3569 struct lock_chain *chain)
3570{
3571#ifdef CONFIG_DEBUG_LOCKDEP
3572 int i, j, id;
3573
3574 i = get_first_held_lock(curr, hlock);
3575
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003576 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
3577 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003578 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003579 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003580
3581 for (j = 0; j < chain->depth - 1; j++, i++) {
Boqun Fengf611e8c2020-08-07 15:42:33 +08003582 id = hlock_id(&curr->held_locks[i]);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003583
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003584 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
3585 print_collision(curr, hlock, chain);
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003586 return 0;
Alfredo Alvarez Fernandez39e2e172016-03-30 19:03:36 +02003587 }
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003588 }
3589#endif
3590 return 1;
3591}
3592
3593/*
Bart Van Assche22126842019-02-14 15:00:48 -08003594 * Given an index that is >= -1, return the index of the next lock chain.
3595 * Return -2 if there is no next lock chain.
3596 */
3597long lockdep_next_lockchain(long i)
3598{
Bart Van Asschede4643a2019-02-14 15:00:50 -08003599 i = find_next_bit(lock_chains_in_use, ARRAY_SIZE(lock_chains), i + 1);
3600 return i < ARRAY_SIZE(lock_chains) ? i : -2;
Bart Van Assche22126842019-02-14 15:00:48 -08003601}
3602
3603unsigned long lock_chain_count(void)
3604{
Bart Van Asschede4643a2019-02-14 15:00:50 -08003605 return bitmap_weight(lock_chains_in_use, ARRAY_SIZE(lock_chains));
3606}
3607
3608/* Must be called with the graph lock held. */
3609static struct lock_chain *alloc_lock_chain(void)
3610{
3611 int idx = find_first_zero_bit(lock_chains_in_use,
3612 ARRAY_SIZE(lock_chains));
3613
3614 if (unlikely(idx >= ARRAY_SIZE(lock_chains)))
3615 return NULL;
3616 __set_bit(idx, lock_chains_in_use);
3617 return lock_chains + idx;
Bart Van Assche22126842019-02-14 15:00:48 -08003618}
3619
3620/*
Byungchul Park545c23f2017-08-07 16:12:48 +09003621 * Adds a dependency chain into chain hashtable. And must be called with
3622 * graph_lock held.
3623 *
3624 * Return 0 if fail, and graph_lock is released.
3625 * Return 1 if succeed, with graph_lock held.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003626 */
Byungchul Park545c23f2017-08-07 16:12:48 +09003627static inline int add_chain_cache(struct task_struct *curr,
3628 struct held_lock *hlock,
3629 u64 chain_key)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003630{
Andrew Mortona63f38c2016-02-03 13:44:12 -08003631 struct hlist_head *hash_head = chainhashentry(chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003632 struct lock_chain *chain;
Steven Rostedte0944ee2011-04-20 21:42:00 -04003633 int i, j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003634
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003635 /*
Bart Van Assche527af3e2019-02-14 15:00:49 -08003636 * The caller must hold the graph lock, ensure we've got IRQs
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003637 * disabled to make this an IRQ-safe lock.. for recursion reasons
3638 * lockdep won't complain about its own locking errors.
3639 */
Peter Zijlstra248efb22020-03-13 11:09:49 +01003640 if (lockdep_assert_locked())
Jarek Poplawski381a2292007-02-10 01:44:58 -08003641 return 0;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003642
Bart Van Asschede4643a2019-02-14 15:00:50 -08003643 chain = alloc_lock_chain();
3644 if (!chain) {
Ingo Molnar74c383f2006-12-13 00:34:43 -08003645 if (!debug_locks_off_graph_unlock())
3646 return 0;
3647
Dave Jones2c522832013-04-25 13:40:02 -04003648 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01003649 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003650 return 0;
3651 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003652 chain->chain_key = chain_key;
Huang, Ying443cd502008-06-20 16:39:21 +08003653 chain->irq_context = hlock->irq_context;
Ingo Molnar9e4e7552016-02-29 10:03:58 +01003654 i = get_first_held_lock(curr, hlock);
Huang, Ying443cd502008-06-20 16:39:21 +08003655 chain->depth = curr->lockdep_depth + 1 - i;
Peter Zijlstra75dd6022016-03-30 11:36:59 +02003656
3657 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
3658 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
3659 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
3660
Waiman Long810507f2020-02-06 10:24:08 -05003661 j = alloc_chain_hlocks(chain->depth);
3662 if (j < 0) {
Byungchul Parkf9af4562017-01-13 11:42:04 +09003663 if (!debug_locks_off_graph_unlock())
Peter Zijlstra75dd6022016-03-30 11:36:59 +02003664 return 0;
3665
3666 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
3667 dump_stack();
3668 return 0;
3669 }
Peter Zijlstra75dd6022016-03-30 11:36:59 +02003670
Waiman Long810507f2020-02-06 10:24:08 -05003671 chain->base = j;
3672 for (j = 0; j < chain->depth - 1; j++, i++) {
Boqun Fengf611e8c2020-08-07 15:42:33 +08003673 int lock_id = hlock_id(curr->held_locks + i);
Waiman Long810507f2020-02-06 10:24:08 -05003674
3675 chain_hlocks[chain->base + j] = lock_id;
3676 }
Boqun Fengf611e8c2020-08-07 15:42:33 +08003677 chain_hlocks[chain->base + j] = hlock_id(hlock);
Andrew Mortona63f38c2016-02-03 13:44:12 -08003678 hlist_add_head_rcu(&chain->entry, hash_head);
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02003679 debug_atomic_inc(chain_lookup_misses);
Waiman Longb3b9c182020-02-06 10:24:03 -05003680 inc_chains(chain->irq_context);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003681
3682 return 1;
3683}
Peter Zijlstra8e182572007-07-19 01:48:54 -07003684
Byungchul Park545c23f2017-08-07 16:12:48 +09003685/*
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08003686 * Look up a dependency chain. Must be called with either the graph lock or
3687 * the RCU read lock held.
Byungchul Park545c23f2017-08-07 16:12:48 +09003688 */
3689static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
3690{
3691 struct hlist_head *hash_head = chainhashentry(chain_key);
3692 struct lock_chain *chain;
3693
Byungchul Park545c23f2017-08-07 16:12:48 +09003694 hlist_for_each_entry_rcu(chain, hash_head, entry) {
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08003695 if (READ_ONCE(chain->chain_key) == chain_key) {
Byungchul Park545c23f2017-08-07 16:12:48 +09003696 debug_atomic_inc(chain_lookup_hits);
3697 return chain;
3698 }
3699 }
3700 return NULL;
3701}
3702
3703/*
3704 * If the key is not present yet in dependency chain cache then
3705 * add it and return 1 - in this case the new dependency chain is
3706 * validated. If the key is already hashed, return 0.
3707 * (On return with 1 graph_lock is held.)
3708 */
3709static inline int lookup_chain_cache_add(struct task_struct *curr,
3710 struct held_lock *hlock,
3711 u64 chain_key)
3712{
3713 struct lock_class *class = hlock_class(hlock);
3714 struct lock_chain *chain = lookup_chain_cache(chain_key);
3715
3716 if (chain) {
3717cache_hit:
3718 if (!check_no_collision(curr, hlock, chain))
3719 return 0;
3720
3721 if (very_verbose(class)) {
3722 printk("\nhash chain already cached, key: "
Borislav Petkov04860d42018-02-26 14:49:26 +01003723 "%016Lx tail class: [%px] %s\n",
Byungchul Park545c23f2017-08-07 16:12:48 +09003724 (unsigned long long)chain_key,
3725 class->key, class->name);
3726 }
3727
3728 return 0;
3729 }
3730
3731 if (very_verbose(class)) {
Borislav Petkov04860d42018-02-26 14:49:26 +01003732 printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
Byungchul Park545c23f2017-08-07 16:12:48 +09003733 (unsigned long long)chain_key, class->key, class->name);
3734 }
3735
3736 if (!graph_lock())
3737 return 0;
3738
3739 /*
3740 * We have to walk the chain again locked - to avoid duplicates:
3741 */
3742 chain = lookup_chain_cache(chain_key);
3743 if (chain) {
3744 graph_unlock();
3745 goto cache_hit;
3746 }
3747
3748 if (!add_chain_cache(curr, hlock, chain_key))
3749 return 0;
3750
3751 return 1;
3752}
3753
Yuyang Du0b9fc8e2019-05-06 16:19:26 +08003754static int validate_chain(struct task_struct *curr,
3755 struct held_lock *hlock,
3756 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07003757{
3758 /*
3759 * Trylock needs to maintain the stack of held locks, but it
3760 * does not add new dependencies, because trylock can be done
3761 * in any order.
3762 *
3763 * We look up the chain_key and do the O(N^2) check and update of
3764 * the dependencies only if this is a new dependency chain.
Byungchul Park545c23f2017-08-07 16:12:48 +09003765 * (If lookup_chain_cache_add() return with 1 it acquires
Peter Zijlstra8e182572007-07-19 01:48:54 -07003766 * graph_lock for us)
3767 */
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01003768 if (!hlock->trylock && hlock->check &&
Byungchul Park545c23f2017-08-07 16:12:48 +09003769 lookup_chain_cache_add(curr, hlock, chain_key)) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07003770 /*
3771 * Check whether last held lock:
3772 *
3773 * - is irq-safe, if this lock is irq-unsafe
3774 * - is softirq-safe, if this lock is hardirq-unsafe
3775 *
3776 * And check whether the new lock's dependency graph
Yuyang Du31a490e2019-05-06 16:19:27 +08003777 * could lead back to the previous lock:
Peter Zijlstra8e182572007-07-19 01:48:54 -07003778 *
Yuyang Du31a490e2019-05-06 16:19:27 +08003779 * - within the current held-lock stack
3780 * - across our accumulated lock dependency records
3781 *
3782 * any of these scenarios could lead to a deadlock.
3783 */
3784 /*
3785 * The simple case: does the current hold the same lock
3786 * already?
Peter Zijlstra8e182572007-07-19 01:48:54 -07003787 */
Yuyang Du4609c4f2019-05-06 16:19:33 +08003788 int ret = check_deadlock(curr, hlock);
Peter Zijlstra8e182572007-07-19 01:48:54 -07003789
3790 if (!ret)
3791 return 0;
3792 /*
Peter Zijlstra8e182572007-07-19 01:48:54 -07003793 * Add dependency only if this lock is not the head
Boqun Fengd61fc96a2020-11-02 13:37:41 +08003794 * of the chain, and if the new lock introduces no more
3795 * lock dependency (because we already hold a lock with the
3796 * same lock class) nor deadlock (because the nest_lock
3797 * serializes nesting locks), see the comments for
3798 * check_deadlock().
Peter Zijlstra8e182572007-07-19 01:48:54 -07003799 */
Byungchul Park545c23f2017-08-07 16:12:48 +09003800 if (!chain_head && ret != 2) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07003801 if (!check_prevs_add(curr, hlock))
3802 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09003803 }
3804
Peter Zijlstra8e182572007-07-19 01:48:54 -07003805 graph_unlock();
Byungchul Park545c23f2017-08-07 16:12:48 +09003806 } else {
3807 /* after lookup_chain_cache_add(): */
Peter Zijlstra8e182572007-07-19 01:48:54 -07003808 if (unlikely(!debug_locks))
3809 return 0;
Byungchul Park545c23f2017-08-07 16:12:48 +09003810 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07003811
3812 return 1;
3813}
3814#else
3815static inline int validate_chain(struct task_struct *curr,
Yuyang Du0b9fc8e2019-05-06 16:19:26 +08003816 struct held_lock *hlock,
3817 int chain_head, u64 chain_key)
Peter Zijlstra8e182572007-07-19 01:48:54 -07003818{
3819 return 1;
3820}
Waiman Long810507f2020-02-06 10:24:08 -05003821
3822static void init_chain_block_buckets(void) { }
Yuyang Due7a38f62019-05-06 16:19:20 +08003823#endif /* CONFIG_PROVE_LOCKING */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003824
3825/*
3826 * We are building curr_chain_key incrementally, so double-check
3827 * it from scratch, to make sure that it's done correctly:
3828 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02003829static void check_chain_key(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003830{
3831#ifdef CONFIG_DEBUG_LOCKDEP
3832 struct held_lock *hlock, *prev_hlock = NULL;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01003833 unsigned int i;
Yuyang Duf6ec8822019-05-06 16:19:24 +08003834 u64 chain_key = INITIAL_CHAIN_KEY;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003835
3836 for (i = 0; i < curr->lockdep_depth; i++) {
3837 hlock = curr->held_locks + i;
3838 if (chain_key != hlock->prev_chain_key) {
3839 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003840 /*
3841 * We got mighty confused, our chain keys don't match
3842 * with what we expect, someone trample on our task state?
3843 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07003844 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003845 curr->lockdep_depth, i,
3846 (unsigned long long)chain_key,
3847 (unsigned long long)hlock->prev_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003848 return;
3849 }
Yuyang Du01bb6f02019-05-06 16:19:25 +08003850
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003851 /*
Yuyang Du01bb6f02019-05-06 16:19:25 +08003852 * hlock->class_idx can't go beyond MAX_LOCKDEP_KEYS, but is
3853 * it registered lock class index?
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003854 */
Yuyang Du01bb6f02019-05-06 16:19:25 +08003855 if (DEBUG_LOCKS_WARN_ON(!test_bit(hlock->class_idx, lock_classes_in_use)))
Jarek Poplawski381a2292007-02-10 01:44:58 -08003856 return;
3857
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003858 if (prev_hlock && (prev_hlock->irq_context !=
3859 hlock->irq_context))
Yuyang Duf6ec8822019-05-06 16:19:24 +08003860 chain_key = INITIAL_CHAIN_KEY;
Boqun Fengf611e8c2020-08-07 15:42:33 +08003861 chain_key = iterate_chain_key(chain_key, hlock_id(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003862 prev_hlock = hlock;
3863 }
3864 if (chain_key != curr->curr_chain_key) {
3865 debug_locks_off();
Peter Zijlstra0119fee2011-09-02 01:30:29 +02003866 /*
3867 * More smoking hash instead of calculating it, damn see these
3868 * numbers float.. I bet that a pink elephant stepped on my memory.
3869 */
Arjan van de Ven2df8b1d2008-07-30 12:43:11 -07003870 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003871 curr->lockdep_depth, i,
3872 (unsigned long long)chain_key,
3873 (unsigned long long)curr->curr_chain_key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003874 }
3875#endif
3876}
3877
Arnd Bergmann30a35f72019-06-28 12:29:03 +02003878#ifdef CONFIG_PROVE_LOCKING
Frederic Weisbecker0d2cc3b2019-04-02 18:02:41 +02003879static int mark_lock(struct task_struct *curr, struct held_lock *this,
3880 enum lock_usage_bit new_bit);
3881
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003882static void print_usage_bug_scenario(struct held_lock *lock)
Steven Rostedt282b5c22011-04-20 21:41:59 -04003883{
3884 struct lock_class *class = hlock_class(lock);
3885
3886 printk(" Possible unsafe locking scenario:\n\n");
3887 printk(" CPU0\n");
3888 printk(" ----\n");
3889 printk(" lock(");
3890 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01003891 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04003892 printk(" <Interrupt>\n");
3893 printk(" lock(");
3894 __print_lock_name(class);
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01003895 printk(KERN_CONT ");\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04003896 printk("\n *** DEADLOCK ***\n\n");
3897}
3898
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003899static void
Peter Zijlstra8e182572007-07-19 01:48:54 -07003900print_usage_bug(struct task_struct *curr, struct held_lock *this,
3901 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
3902{
Peter Zijlstra7f82e632021-02-01 11:55:38 +01003903 if (!debug_locks_off() || debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003904 return;
Peter Zijlstra8e182572007-07-19 01:48:54 -07003905
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003906 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003907 pr_warn("================================\n");
3908 pr_warn("WARNING: inconsistent lock state\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003909 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003910 pr_warn("--------------------------------\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07003911
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003912 pr_warn("inconsistent {%s} -> {%s} usage.\n",
Peter Zijlstra8e182572007-07-19 01:48:54 -07003913 usage_str[prev_bit], usage_str[new_bit]);
3914
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003915 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003916 curr->comm, task_pid_nr(curr),
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02003917 lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT,
Peter Zijlstraef996912020-03-20 12:56:42 +01003918 lockdep_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02003919 lockdep_hardirqs_enabled(),
Peter Zijlstraef996912020-03-20 12:56:42 +01003920 lockdep_softirqs_enabled(curr));
Peter Zijlstra8e182572007-07-19 01:48:54 -07003921 print_lock(this);
3922
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003923 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
Bart Van Assche12593b72019-07-22 11:24:42 -07003924 print_lock_trace(hlock_class(this)->usage_traces[prev_bit], 1);
Peter Zijlstra8e182572007-07-19 01:48:54 -07003925
3926 print_irqtrace_events(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003927 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedt282b5c22011-04-20 21:41:59 -04003928 print_usage_bug_scenario(this);
3929
Peter Zijlstra8e182572007-07-19 01:48:54 -07003930 lockdep_print_held_locks(curr);
3931
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003932 pr_warn("\nstack backtrace:\n");
Peter Zijlstra8e182572007-07-19 01:48:54 -07003933 dump_stack();
Peter Zijlstra8e182572007-07-19 01:48:54 -07003934}
3935
3936/*
3937 * Print out an error if an invalid bit is set:
3938 */
3939static inline int
3940valid_state(struct task_struct *curr, struct held_lock *this,
3941 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
3942{
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003943 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) {
Peter Zijlstra7f82e632021-02-01 11:55:38 +01003944 graph_unlock();
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003945 print_usage_bug(curr, this, bad_bit, new_bit);
3946 return 0;
3947 }
Peter Zijlstra8e182572007-07-19 01:48:54 -07003948 return 1;
3949}
3950
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003951
3952/*
3953 * print irq inversion bug:
3954 */
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003955static void
Ming Lei24208ca2009-07-16 15:44:29 +02003956print_irq_inversion_bug(struct task_struct *curr,
3957 struct lock_list *root, struct lock_list *other,
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003958 struct held_lock *this, int forwards,
3959 const char *irqclass)
3960{
Steven Rostedtdad3d742011-04-20 21:41:57 -04003961 struct lock_list *entry = other;
3962 struct lock_list *middle = NULL;
3963 int depth;
3964
Ingo Molnar74c383f2006-12-13 00:34:43 -08003965 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08003966 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003967
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003968 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003969 pr_warn("========================================================\n");
3970 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01003971 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08003972 pr_warn("--------------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003973 pr_warn("%s/%d just changed the state of lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003974 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003975 print_lock(this);
3976 if (forwards)
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003977 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003978 else
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003979 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
Ming Lei24208ca2009-07-16 15:44:29 +02003980 print_lock_name(other->class);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003981 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07003982
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003983 pr_warn("\nother info that might help us debug this:\n");
Steven Rostedtdad3d742011-04-20 21:41:57 -04003984
3985 /* Find a middle lock (if one exists) */
3986 depth = get_lock_depth(other);
3987 do {
3988 if (depth == 0 && (entry != root)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07003989 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
Steven Rostedtdad3d742011-04-20 21:41:57 -04003990 break;
3991 }
3992 middle = entry;
3993 entry = get_lock_parent(entry);
3994 depth--;
3995 } while (entry && entry != root && (depth >= 0));
3996 if (forwards)
3997 print_irq_lock_scenario(root, other,
3998 middle ? middle->class : root->class, other->class);
3999 else
4000 print_irq_lock_scenario(other, root,
4001 middle ? middle->class : other->class, root->class);
4002
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004003 lockdep_print_held_locks(curr);
4004
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004005 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
Bart Van Assche12593b72019-07-22 11:24:42 -07004006 root->trace = save_trace();
4007 if (!root->trace)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004008 return;
Ming Lei24208ca2009-07-16 15:44:29 +02004009 print_shortest_lock_dependencies(other, root);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004010
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004011 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004012 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004013}
4014
4015/*
4016 * Prove that in the forwards-direction subgraph starting at <this>
4017 * there is no lock matching <mask>:
4018 */
4019static int
4020check_usage_forwards(struct task_struct *curr, struct held_lock *this,
Boqun Fengf08e3882020-08-07 15:42:30 +08004021 enum lock_usage_bit bit)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004022{
Boqun Fengb11be022020-08-07 15:42:22 +08004023 enum bfs_result ret;
Ming Leid7aaba12009-07-16 15:44:29 +02004024 struct lock_list root;
Kees Cook3f649ab2020-06-03 13:09:38 -07004025 struct lock_list *target_entry;
Boqun Fengf08e3882020-08-07 15:42:30 +08004026 enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK;
4027 unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004028
Boqun Feng6971c0f2020-08-07 15:42:26 +08004029 bfs_init_root(&root, this);
Boqun Fengf08e3882020-08-07 15:42:30 +08004030 ret = find_usage_forwards(&root, usage_mask, &target_entry);
Boqun Fengb11be022020-08-07 15:42:22 +08004031 if (bfs_error(ret)) {
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004032 print_bfs_bug(ret);
4033 return 0;
4034 }
Boqun Fengb11be022020-08-07 15:42:22 +08004035 if (ret == BFS_RNOMATCH)
4036 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004037
Boqun Fengf08e3882020-08-07 15:42:30 +08004038 /* Check whether write or read usage is the match */
4039 if (target_entry->class->usage_mask & lock_flag(bit)) {
4040 print_irq_inversion_bug(curr, &root, target_entry,
4041 this, 1, state_name(bit));
4042 } else {
4043 print_irq_inversion_bug(curr, &root, target_entry,
4044 this, 1, state_name(read_bit));
4045 }
4046
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004047 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004048}
4049
4050/*
4051 * Prove that in the backwards-direction subgraph starting at <this>
4052 * there is no lock matching <mask>:
4053 */
4054static int
4055check_usage_backwards(struct task_struct *curr, struct held_lock *this,
Boqun Fengf08e3882020-08-07 15:42:30 +08004056 enum lock_usage_bit bit)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004057{
Boqun Fengb11be022020-08-07 15:42:22 +08004058 enum bfs_result ret;
Ming Leid7aaba12009-07-16 15:44:29 +02004059 struct lock_list root;
Kees Cook3f649ab2020-06-03 13:09:38 -07004060 struct lock_list *target_entry;
Boqun Fengf08e3882020-08-07 15:42:30 +08004061 enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK;
4062 unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004063
Boqun Feng6971c0f2020-08-07 15:42:26 +08004064 bfs_init_rootb(&root, this);
Boqun Fengf08e3882020-08-07 15:42:30 +08004065 ret = find_usage_backwards(&root, usage_mask, &target_entry);
Boqun Fengb11be022020-08-07 15:42:22 +08004066 if (bfs_error(ret)) {
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004067 print_bfs_bug(ret);
4068 return 0;
4069 }
Boqun Fengb11be022020-08-07 15:42:22 +08004070 if (ret == BFS_RNOMATCH)
4071 return 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004072
Boqun Fengf08e3882020-08-07 15:42:30 +08004073 /* Check whether write or read usage is the match */
4074 if (target_entry->class->usage_mask & lock_flag(bit)) {
4075 print_irq_inversion_bug(curr, &root, target_entry,
4076 this, 0, state_name(bit));
4077 } else {
4078 print_irq_inversion_bug(curr, &root, target_entry,
4079 this, 0, state_name(read_bit));
4080 }
4081
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004082 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004083}
4084
Ingo Molnar3117df02006-12-13 00:34:43 -08004085void print_irqtrace_events(struct task_struct *curr)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004086{
Marco Elver0584df92020-07-29 13:09:15 +02004087 const struct irqtrace_events *trace = &curr->irqtrace;
4088
4089 printk("irq event stamp: %u\n", trace->irq_events);
Borislav Petkov04860d42018-02-26 14:49:26 +01004090 printk("hardirqs last enabled at (%u): [<%px>] %pS\n",
Marco Elver0584df92020-07-29 13:09:15 +02004091 trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip,
4092 (void *)trace->hardirq_enable_ip);
Borislav Petkov04860d42018-02-26 14:49:26 +01004093 printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
Marco Elver0584df92020-07-29 13:09:15 +02004094 trace->hardirq_disable_event, (void *)trace->hardirq_disable_ip,
4095 (void *)trace->hardirq_disable_ip);
Borislav Petkov04860d42018-02-26 14:49:26 +01004096 printk("softirqs last enabled at (%u): [<%px>] %pS\n",
Marco Elver0584df92020-07-29 13:09:15 +02004097 trace->softirq_enable_event, (void *)trace->softirq_enable_ip,
4098 (void *)trace->softirq_enable_ip);
Borislav Petkov04860d42018-02-26 14:49:26 +01004099 printk("softirqs last disabled at (%u): [<%px>] %pS\n",
Marco Elver0584df92020-07-29 13:09:15 +02004100 trace->softirq_disable_event, (void *)trace->softirq_disable_ip,
4101 (void *)trace->softirq_disable_ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004102}
4103
Peter Zijlstracd953022009-01-22 16:38:21 +01004104static int HARDIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004105{
Peter Zijlstra8e182572007-07-19 01:48:54 -07004106#if HARDIRQ_VERBOSE
4107 return class_filter(class);
4108#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004109 return 0;
4110}
4111
Peter Zijlstracd953022009-01-22 16:38:21 +01004112static int SOFTIRQ_verbose(struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004113{
Peter Zijlstra8e182572007-07-19 01:48:54 -07004114#if SOFTIRQ_VERBOSE
4115 return class_filter(class);
4116#endif
4117 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004118}
4119
Peter Zijlstracd953022009-01-22 16:38:21 +01004120static int (*state_verbose_f[])(struct lock_class *class) = {
4121#define LOCKDEP_STATE(__STATE) \
4122 __STATE##_verbose,
4123#include "lockdep_states.h"
4124#undef LOCKDEP_STATE
4125};
4126
4127static inline int state_verbose(enum lock_usage_bit bit,
4128 struct lock_class *class)
4129{
Frederic Weisbeckerc902a1e2019-04-02 18:02:42 +02004130 return state_verbose_f[bit >> LOCK_USAGE_DIR_MASK](class);
Peter Zijlstracd953022009-01-22 16:38:21 +01004131}
4132
Peter Zijlstra42c50d52009-01-22 16:58:16 +01004133typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
4134 enum lock_usage_bit bit, const char *name);
4135
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01004136static int
Peter Zijlstra1c21f142009-03-04 13:51:13 +01004137mark_lock_irq(struct task_struct *curr, struct held_lock *this,
4138 enum lock_usage_bit new_bit)
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01004139{
Peter Zijlstraf9892092009-01-22 16:09:59 +01004140 int excl_bit = exclusive_bit(new_bit);
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01004141 int read = new_bit & LOCK_USAGE_READ_MASK;
4142 int dir = new_bit & LOCK_USAGE_DIR_MASK;
Peter Zijlstra42c50d52009-01-22 16:58:16 +01004143
Peter Zijlstra38aa2712009-01-27 14:53:50 +01004144 /*
Peter Zijlstra38aa2712009-01-27 14:53:50 +01004145 * Validate that this particular lock does not have conflicting
4146 * usage states.
4147 */
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01004148 if (!valid_state(curr, this, new_bit, excl_bit))
4149 return 0;
Peter Zijlstra9d3651a2009-01-22 17:18:32 +01004150
Peter Zijlstra38aa2712009-01-27 14:53:50 +01004151 /*
Boqun Fengf08e3882020-08-07 15:42:30 +08004152 * Check for read in write conflicts
4153 */
4154 if (!read && !valid_state(curr, this, new_bit,
4155 excl_bit + LOCK_USAGE_READ_MASK))
4156 return 0;
4157
4158
4159 /*
Peter Zijlstra38aa2712009-01-27 14:53:50 +01004160 * Validate that the lock dependencies don't have conflicting usage
4161 * states.
4162 */
Boqun Fengf08e3882020-08-07 15:42:30 +08004163 if (dir) {
4164 /*
4165 * mark ENABLED has to look backwards -- to ensure no dependee
4166 * has USED_IN state, which, again, would allow recursion deadlocks.
4167 */
4168 if (!check_usage_backwards(curr, this, excl_bit))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01004169 return 0;
Boqun Fengf08e3882020-08-07 15:42:30 +08004170 } else {
4171 /*
4172 * mark USED_IN has to look forwards -- to ensure no dependency
4173 * has ENABLED state, which would allow recursion deadlocks.
4174 */
4175 if (!check_usage_forwards(curr, this, excl_bit))
Peter Zijlstra38aa2712009-01-27 14:53:50 +01004176 return 0;
4177 }
Peter Zijlstra780e8202009-01-22 16:51:29 +01004178
Peter Zijlstracd953022009-01-22 16:38:21 +01004179 if (state_verbose(new_bit, hlock_class(this)))
Peter Zijlstra6a6904d2009-01-22 16:07:44 +01004180 return 2;
4181
4182 return 1;
4183}
4184
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004185/*
4186 * Mark all held locks with a usage bit:
4187 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02004188static int
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01004189mark_held_locks(struct task_struct *curr, enum lock_usage_bit base_bit)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004190{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004191 struct held_lock *hlock;
4192 int i;
4193
4194 for (i = 0; i < curr->lockdep_depth; i++) {
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01004195 enum lock_usage_bit hlock_bit = base_bit;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004196 hlock = curr->held_locks + i;
4197
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01004198 if (hlock->read)
Frederic Weisbeckerbba2a8f2018-12-28 06:02:01 +01004199 hlock_bit += LOCK_USAGE_READ_MASK;
Peter Zijlstracf2ad4d2009-01-27 13:58:08 +01004200
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01004201 BUG_ON(hlock_bit >= LOCK_USAGE_STATES);
Nick Piggincf40bd12009-01-21 08:12:39 +01004202
Oleg Nesterov34d0ed52014-01-20 19:20:13 +01004203 if (!hlock->check)
Peter Zijlstraefbe2ee2011-07-07 11:39:45 +02004204 continue;
4205
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01004206 if (!mark_lock(curr, hlock, hlock_bit))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004207 return 0;
4208 }
4209
4210 return 1;
4211}
4212
4213/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004214 * Hardirqs will be enabled:
4215 */
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004216static void __trace_hardirqs_on_caller(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004217{
4218 struct task_struct *curr = current;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004219
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004220 /*
4221 * We are going to turn hardirqs on, so set the
4222 * usage bit for all held locks:
4223 */
Frederic Weisbecker436a49a2018-12-28 06:02:00 +01004224 if (!mark_held_locks(curr, LOCK_ENABLED_HARDIRQ))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004225 return;
4226 /*
4227 * If we have softirqs enabled, then set the usage
4228 * bit for all held locks. (disabled hardirqs prevented
4229 * this bit from being set before)
4230 */
4231 if (curr->softirqs_enabled)
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004232 mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004233}
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02004234
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004235/**
4236 * lockdep_hardirqs_on_prepare - Prepare for enabling interrupts
4237 * @ip: Caller address
4238 *
4239 * Invoked before a possible transition to RCU idle from exit to user or
4240 * guest mode. This ensures that all RCU operations are done before RCU
4241 * stops watching. After the RCU transition lockdep_hardirqs_on() has to be
4242 * invoked to set the final state.
4243 */
4244void lockdep_hardirqs_on_prepare(unsigned long ip)
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02004245{
Peter Zijlstra859d0692020-05-27 15:00:57 +02004246 if (unlikely(!debug_locks))
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02004247 return;
4248
Peter Zijlstra859d0692020-05-27 15:00:57 +02004249 /*
4250 * NMIs do not (and cannot) track lock dependencies, nothing to do.
4251 */
4252 if (unlikely(in_nmi()))
4253 return;
4254
Peter Zijlstraf8e48a32020-10-22 12:23:02 +02004255 if (unlikely(this_cpu_read(lockdep_recursion)))
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02004256 return;
4257
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004258 if (unlikely(lockdep_hardirqs_enabled())) {
Peter Zijlstra7d36b262011-07-26 13:13:44 +02004259 /*
4260 * Neither irq nor preemption are disabled here
4261 * so this is racy by nature but losing one hit
4262 * in a stat is not a big deal.
4263 */
4264 __debug_atomic_inc(redundant_hardirqs_on);
4265 return;
4266 }
4267
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004268 /*
4269 * We're enabling irqs and according to our state above irqs weren't
4270 * already enabled, yet we find the hardware thinks they are in fact
4271 * enabled.. someone messed up their IRQ state tracing.
4272 */
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02004273 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4274 return;
4275
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004276 /*
4277 * See the fine text that goes along with this variable definition.
4278 */
zhengbind6710022019-04-29 20:26:31 +08004279 if (DEBUG_LOCKS_WARN_ON(early_boot_irqs_disabled))
Peter Zijlstra7d36b262011-07-26 13:13:44 +02004280 return;
4281
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004282 /*
4283 * Can't allow enabling interrupts while in an interrupt handler,
4284 * that's general bad form and such. Recursion, limited stack etc..
4285 */
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004286 if (DEBUG_LOCKS_WARN_ON(lockdep_hardirq_context()))
Peter Zijlstra7d36b262011-07-26 13:13:44 +02004287 return;
4288
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004289 current->hardirq_chain_key = current->curr_chain_key;
4290
Peter Zijlstra4d004092020-10-02 11:04:21 +02004291 lockdep_recursion_inc();
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004292 __trace_hardirqs_on_caller();
Peter Zijlstra10476e62020-03-13 09:56:38 +01004293 lockdep_recursion_finish();
Peter Zijlstradd4e5d32011-06-21 17:17:27 +02004294}
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004295EXPORT_SYMBOL_GPL(lockdep_hardirqs_on_prepare);
4296
4297void noinstr lockdep_hardirqs_on(unsigned long ip)
4298{
Marco Elver0584df92020-07-29 13:09:15 +02004299 struct irqtrace_events *trace = &current->irqtrace;
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004300
Peter Zijlstra859d0692020-05-27 15:00:57 +02004301 if (unlikely(!debug_locks))
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004302 return;
4303
Peter Zijlstra859d0692020-05-27 15:00:57 +02004304 /*
4305 * NMIs can happen in the middle of local_irq_{en,dis}able() where the
4306 * tracking state and hardware state are out of sync.
4307 *
4308 * NMIs must save lockdep_hardirqs_enabled() to restore IRQ state from,
4309 * and not rely on hardware state like normal interrupts.
4310 */
4311 if (unlikely(in_nmi())) {
peterz@infradead.orged004952020-07-27 14:48:52 +02004312 if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI))
4313 return;
4314
Peter Zijlstra859d0692020-05-27 15:00:57 +02004315 /*
4316 * Skip:
4317 * - recursion check, because NMI can hit lockdep;
4318 * - hardware state check, because above;
4319 * - chain_key check, see lockdep_hardirqs_on_prepare().
4320 */
4321 goto skip_checks;
4322 }
4323
Peter Zijlstraf8e48a32020-10-22 12:23:02 +02004324 if (unlikely(this_cpu_read(lockdep_recursion)))
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004325 return;
4326
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004327 if (lockdep_hardirqs_enabled()) {
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004328 /*
4329 * Neither irq nor preemption are disabled here
4330 * so this is racy by nature but losing one hit
4331 * in a stat is not a big deal.
4332 */
4333 __debug_atomic_inc(redundant_hardirqs_on);
4334 return;
4335 }
4336
4337 /*
4338 * We're enabling irqs and according to our state above irqs weren't
4339 * already enabled, yet we find the hardware thinks they are in fact
4340 * enabled.. someone messed up their IRQ state tracing.
4341 */
4342 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4343 return;
4344
4345 /*
4346 * Ensure the lock stack remained unchanged between
4347 * lockdep_hardirqs_on_prepare() and lockdep_hardirqs_on().
4348 */
4349 DEBUG_LOCKS_WARN_ON(current->hardirq_chain_key !=
4350 current->curr_chain_key);
4351
Peter Zijlstra859d0692020-05-27 15:00:57 +02004352skip_checks:
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004353 /* we'll do an OFF -> ON transition: */
Peter Zijlstrafddf9052020-08-20 09:13:30 +02004354 __this_cpu_write(hardirqs_enabled, 1);
Marco Elver0584df92020-07-29 13:09:15 +02004355 trace->hardirq_enable_ip = ip;
4356 trace->hardirq_enable_event = ++trace->irq_events;
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004357 debug_atomic_inc(hardirqs_on_events);
4358}
4359EXPORT_SYMBOL_GPL(lockdep_hardirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004360
4361/*
4362 * Hardirqs were disabled:
4363 */
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004364void noinstr lockdep_hardirqs_off(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004365{
Peter Zijlstra859d0692020-05-27 15:00:57 +02004366 if (unlikely(!debug_locks))
4367 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004368
Peter Zijlstra859d0692020-05-27 15:00:57 +02004369 /*
4370 * Matching lockdep_hardirqs_on(), allow NMIs in the middle of lockdep;
4371 * they will restore the software state. This ensures the software
4372 * state is consistent inside NMIs as well.
4373 */
peterz@infradead.orged004952020-07-27 14:48:52 +02004374 if (in_nmi()) {
4375 if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI))
4376 return;
Peter Zijlstra4d004092020-10-02 11:04:21 +02004377 } else if (__this_cpu_read(lockdep_recursion))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004378 return;
4379
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004380 /*
4381 * So we're supposed to get called after you mask local IRQs, but for
4382 * some reason the hardware doesn't quite think you did a proper job.
4383 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004384 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4385 return;
4386
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004387 if (lockdep_hardirqs_enabled()) {
Marco Elver0584df92020-07-29 13:09:15 +02004388 struct irqtrace_events *trace = &current->irqtrace;
4389
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004390 /*
4391 * We have done an ON -> OFF transition:
4392 */
Peter Zijlstrafddf9052020-08-20 09:13:30 +02004393 __this_cpu_write(hardirqs_enabled, 0);
Marco Elver0584df92020-07-29 13:09:15 +02004394 trace->hardirq_disable_ip = ip;
4395 trace->hardirq_disable_event = ++trace->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02004396 debug_atomic_inc(hardirqs_off_events);
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004397 } else {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02004398 debug_atomic_inc(redundant_hardirqs_off);
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004399 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004400}
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01004401EXPORT_SYMBOL_GPL(lockdep_hardirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004402
4403/*
4404 * Softirqs will be enabled:
4405 */
Peter Zijlstra0d384532020-03-20 12:56:41 +01004406void lockdep_softirqs_on(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004407{
Marco Elver0584df92020-07-29 13:09:15 +02004408 struct irqtrace_events *trace = &current->irqtrace;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004409
Peter Zijlstra4d004092020-10-02 11:04:21 +02004410 if (unlikely(!lockdep_enabled()))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004411 return;
4412
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004413 /*
4414 * We fancy IRQs being disabled here, see softirq.c, avoids
4415 * funny state and nesting things.
4416 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004417 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4418 return;
4419
Marco Elver0584df92020-07-29 13:09:15 +02004420 if (current->softirqs_enabled) {
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02004421 debug_atomic_inc(redundant_softirqs_on);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004422 return;
4423 }
4424
Peter Zijlstra4d004092020-10-02 11:04:21 +02004425 lockdep_recursion_inc();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004426 /*
4427 * We'll do an OFF -> ON transition:
4428 */
Marco Elver0584df92020-07-29 13:09:15 +02004429 current->softirqs_enabled = 1;
4430 trace->softirq_enable_ip = ip;
4431 trace->softirq_enable_event = ++trace->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02004432 debug_atomic_inc(softirqs_on_events);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004433 /*
4434 * We are going to turn softirqs on, so set the
4435 * usage bit for all held locks, if hardirqs are
4436 * enabled too:
4437 */
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004438 if (lockdep_hardirqs_enabled())
Marco Elver0584df92020-07-29 13:09:15 +02004439 mark_held_locks(current, LOCK_ENABLED_SOFTIRQ);
Peter Zijlstra10476e62020-03-13 09:56:38 +01004440 lockdep_recursion_finish();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004441}
4442
4443/*
4444 * Softirqs were disabled:
4445 */
Peter Zijlstra0d384532020-03-20 12:56:41 +01004446void lockdep_softirqs_off(unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004447{
Peter Zijlstra4d004092020-10-02 11:04:21 +02004448 if (unlikely(!lockdep_enabled()))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004449 return;
4450
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004451 /*
4452 * We fancy IRQs being disabled here, see softirq.c
4453 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004454 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
4455 return;
4456
Marco Elver0584df92020-07-29 13:09:15 +02004457 if (current->softirqs_enabled) {
4458 struct irqtrace_events *trace = &current->irqtrace;
4459
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004460 /*
4461 * We have done an ON -> OFF transition:
4462 */
Marco Elver0584df92020-07-29 13:09:15 +02004463 current->softirqs_enabled = 0;
4464 trace->softirq_disable_ip = ip;
4465 trace->softirq_disable_event = ++trace->irq_events;
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02004466 debug_atomic_inc(softirqs_off_events);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004467 /*
4468 * Whoops, we wanted softirqs off, so why aren't they?
4469 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004470 DEBUG_LOCKS_WARN_ON(!softirq_count());
4471 } else
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +02004472 debug_atomic_inc(redundant_softirqs_off);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004473}
4474
Yuyang Du09180652019-05-06 16:19:37 +08004475static int
4476mark_usage(struct task_struct *curr, struct held_lock *hlock, int check)
Peter Zijlstra8e182572007-07-19 01:48:54 -07004477{
Yuyang Du09180652019-05-06 16:19:37 +08004478 if (!check)
4479 goto lock_used;
4480
Peter Zijlstra8e182572007-07-19 01:48:54 -07004481 /*
4482 * If non-trylock use in a hardirq or softirq context, then
4483 * mark the lock as used in these contexts:
4484 */
4485 if (!hlock->trylock) {
4486 if (hlock->read) {
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004487 if (lockdep_hardirq_context())
Peter Zijlstra8e182572007-07-19 01:48:54 -07004488 if (!mark_lock(curr, hlock,
4489 LOCK_USED_IN_HARDIRQ_READ))
4490 return 0;
4491 if (curr->softirq_context)
4492 if (!mark_lock(curr, hlock,
4493 LOCK_USED_IN_SOFTIRQ_READ))
4494 return 0;
4495 } else {
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004496 if (lockdep_hardirq_context())
Peter Zijlstra8e182572007-07-19 01:48:54 -07004497 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
4498 return 0;
4499 if (curr->softirq_context)
4500 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
4501 return 0;
4502 }
4503 }
4504 if (!hlock->hardirqs_off) {
4505 if (hlock->read) {
4506 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01004507 LOCK_ENABLED_HARDIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07004508 return 0;
4509 if (curr->softirqs_enabled)
4510 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01004511 LOCK_ENABLED_SOFTIRQ_READ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07004512 return 0;
4513 } else {
4514 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01004515 LOCK_ENABLED_HARDIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07004516 return 0;
4517 if (curr->softirqs_enabled)
4518 if (!mark_lock(curr, hlock,
Peter Zijlstra4fc95e82009-01-22 13:10:52 +01004519 LOCK_ENABLED_SOFTIRQ))
Peter Zijlstra8e182572007-07-19 01:48:54 -07004520 return 0;
4521 }
4522 }
4523
Yuyang Du09180652019-05-06 16:19:37 +08004524lock_used:
4525 /* mark it as used: */
4526 if (!mark_lock(curr, hlock, LOCK_USED))
4527 return 0;
4528
Peter Zijlstra8e182572007-07-19 01:48:54 -07004529 return 1;
4530}
4531
Boqun Fengc2469752016-02-16 13:57:40 +08004532static inline unsigned int task_irq_context(struct task_struct *task)
4533{
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004534 return LOCK_CHAIN_HARDIRQ_CONTEXT * !!lockdep_hardirq_context() +
Waiman Longb3b9c182020-02-06 10:24:03 -05004535 LOCK_CHAIN_SOFTIRQ_CONTEXT * !!task->softirq_context;
Boqun Fengc2469752016-02-16 13:57:40 +08004536}
4537
Peter Zijlstra8e182572007-07-19 01:48:54 -07004538static int separate_irq_context(struct task_struct *curr,
4539 struct held_lock *hlock)
4540{
4541 unsigned int depth = curr->lockdep_depth;
4542
4543 /*
4544 * Keep track of points where we cross into an interrupt context:
4545 */
Peter Zijlstra8e182572007-07-19 01:48:54 -07004546 if (depth) {
4547 struct held_lock *prev_hlock;
4548
4549 prev_hlock = curr->held_locks + depth-1;
4550 /*
4551 * If we cross into another context, reset the
4552 * hash key (this also prevents the checking and the
4553 * adding of the dependency to 'prev'):
4554 */
4555 if (prev_hlock->irq_context != hlock->irq_context)
4556 return 1;
4557 }
4558 return 0;
4559}
4560
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004561/*
Peter Zijlstra8e182572007-07-19 01:48:54 -07004562 * Mark a lock with a usage bit, and validate the state transition:
4563 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02004564static int mark_lock(struct task_struct *curr, struct held_lock *this,
Steven Rostedt0764d232008-05-12 21:20:44 +02004565 enum lock_usage_bit new_bit)
Peter Zijlstra8e182572007-07-19 01:48:54 -07004566{
Peter Zijlstra2bb89452020-09-30 11:49:37 +02004567 unsigned int new_mask, ret = 1;
Peter Zijlstra8e182572007-07-19 01:48:54 -07004568
Yuyang Du4d563302019-05-06 16:19:38 +08004569 if (new_bit >= LOCK_USAGE_STATES) {
4570 DEBUG_LOCKS_WARN_ON(1);
4571 return 0;
4572 }
4573
peterz@infradead.org23870f12020-09-02 18:03:23 +02004574 if (new_bit == LOCK_USED && this->read)
4575 new_bit = LOCK_USED_READ;
4576
4577 new_mask = 1 << new_bit;
4578
Peter Zijlstra8e182572007-07-19 01:48:54 -07004579 /*
4580 * If already set then do not dirty the cacheline,
4581 * nor do any checks:
4582 */
Dave Jonesf82b2172008-08-11 09:30:23 +02004583 if (likely(hlock_class(this)->usage_mask & new_mask))
Peter Zijlstra8e182572007-07-19 01:48:54 -07004584 return 1;
4585
4586 if (!graph_lock())
4587 return 0;
4588 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004589 * Make sure we didn't race:
Peter Zijlstra8e182572007-07-19 01:48:54 -07004590 */
peterz@infradead.org23870f12020-09-02 18:03:23 +02004591 if (unlikely(hlock_class(this)->usage_mask & new_mask))
4592 goto unlock;
Peter Zijlstra8e182572007-07-19 01:48:54 -07004593
Peter Zijlstra1a393402020-10-27 13:48:34 +01004594 if (!hlock_class(this)->usage_mask)
4595 debug_atomic_dec(nr_unused_locks);
4596
Dave Jonesf82b2172008-08-11 09:30:23 +02004597 hlock_class(this)->usage_mask |= new_mask;
Peter Zijlstra8e182572007-07-19 01:48:54 -07004598
Peter Zijlstra2bb89452020-09-30 11:49:37 +02004599 if (new_bit < LOCK_TRACE_STATES) {
4600 if (!(hlock_class(this)->usage_traces[new_bit] = save_trace()))
Peter Zijlstra8e182572007-07-19 01:48:54 -07004601 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07004602 }
4603
Peter Zijlstra1a393402020-10-27 13:48:34 +01004604 if (new_bit < LOCK_USED) {
Peter Zijlstra8e182572007-07-19 01:48:54 -07004605 ret = mark_lock_irq(curr, this, new_bit);
4606 if (!ret)
4607 return 0;
4608 }
4609
peterz@infradead.org23870f12020-09-02 18:03:23 +02004610unlock:
Peter Zijlstra8e182572007-07-19 01:48:54 -07004611 graph_unlock();
4612
4613 /*
4614 * We must printk outside of the graph_lock:
4615 */
4616 if (ret == 2) {
4617 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
4618 print_lock(this);
4619 print_irqtrace_events(curr);
4620 dump_stack();
4621 }
4622
4623 return ret;
4624}
4625
Peter Zijlstra9a019db2020-03-31 20:38:12 +02004626static inline short task_wait_context(struct task_struct *curr)
4627{
4628 /*
4629 * Set appropriate wait type for the context; for IRQs we have to take
4630 * into account force_irqthread as that is implied by PREEMPT_RT.
4631 */
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02004632 if (lockdep_hardirq_context()) {
Peter Zijlstra9a019db2020-03-31 20:38:12 +02004633 /*
4634 * Check if force_irqthreads will run us threaded.
4635 */
4636 if (curr->hardirq_threaded || curr->irq_config)
4637 return LD_WAIT_CONFIG;
4638
4639 return LD_WAIT_SPIN;
4640 } else if (curr->softirq_context) {
4641 /*
4642 * Softirqs are always threaded.
4643 */
4644 return LD_WAIT_CONFIG;
4645 }
4646
4647 return LD_WAIT_MAX;
4648}
4649
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004650static int
4651print_lock_invalid_wait_context(struct task_struct *curr,
4652 struct held_lock *hlock)
4653{
Peter Zijlstra9a019db2020-03-31 20:38:12 +02004654 short curr_inner;
4655
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004656 if (!debug_locks_off())
4657 return 0;
4658 if (debug_locks_silent)
4659 return 0;
4660
4661 pr_warn("\n");
4662 pr_warn("=============================\n");
4663 pr_warn("[ BUG: Invalid wait context ]\n");
4664 print_kernel_ident();
4665 pr_warn("-----------------------------\n");
4666
4667 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
4668 print_lock(hlock);
4669
4670 pr_warn("other info that might help us debug this:\n");
Peter Zijlstra9a019db2020-03-31 20:38:12 +02004671
4672 curr_inner = task_wait_context(curr);
4673 pr_warn("context-{%d:%d}\n", curr_inner, curr_inner);
4674
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004675 lockdep_print_held_locks(curr);
4676
4677 pr_warn("stack backtrace:\n");
4678 dump_stack();
4679
4680 return 0;
4681}
4682
4683/*
4684 * Verify the wait_type context.
4685 *
Zhouyi Zhoua2e05dd2021-08-11 10:59:20 +08004686 * This check validates we take locks in the right wait-type order; that is it
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004687 * ensures that we do not take mutexes inside spinlocks and do not attempt to
4688 * acquire spinlocks inside raw_spinlocks and the sort.
4689 *
4690 * The entire thing is slightly more complex because of RCU, RCU is a lock that
4691 * can be taken from (pretty much) any context but also has constraints.
4692 * However when taken in a stricter environment the RCU lock does not loosen
4693 * the constraints.
4694 *
4695 * Therefore we must look for the strictest environment in the lock stack and
4696 * compare that to the lock we're trying to acquire.
4697 */
4698static int check_wait_context(struct task_struct *curr, struct held_lock *next)
4699{
Peter Zijlstradfd5e3f2020-12-09 16:06:21 +01004700 u8 next_inner = hlock_class(next)->wait_type_inner;
4701 u8 next_outer = hlock_class(next)->wait_type_outer;
4702 u8 curr_inner;
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004703 int depth;
4704
Peter Zijlstraf8b298c2021-06-17 20:57:18 +02004705 if (!next_inner || next->trylock)
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004706 return 0;
4707
4708 if (!next_outer)
4709 next_outer = next_inner;
4710
4711 /*
4712 * Find start of current irq_context..
4713 */
4714 for (depth = curr->lockdep_depth - 1; depth >= 0; depth--) {
4715 struct held_lock *prev = curr->held_locks + depth;
4716 if (prev->irq_context != next->irq_context)
4717 break;
4718 }
4719 depth++;
4720
Peter Zijlstra9a019db2020-03-31 20:38:12 +02004721 curr_inner = task_wait_context(curr);
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004722
4723 for (; depth < curr->lockdep_depth; depth++) {
4724 struct held_lock *prev = curr->held_locks + depth;
Peter Zijlstradfd5e3f2020-12-09 16:06:21 +01004725 u8 prev_inner = hlock_class(prev)->wait_type_inner;
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004726
4727 if (prev_inner) {
4728 /*
4729 * We can have a bigger inner than a previous one
4730 * when outer is smaller than inner, as with RCU.
4731 *
4732 * Also due to trylocks.
4733 */
4734 curr_inner = min(curr_inner, prev_inner);
4735 }
4736 }
4737
4738 if (next_outer > curr_inner)
4739 return print_lock_invalid_wait_context(curr, next);
4740
4741 return 0;
4742}
4743
Arnd Bergmann30a35f72019-06-28 12:29:03 +02004744#else /* CONFIG_PROVE_LOCKING */
Arnd Bergmann886532a2019-06-17 14:47:05 +02004745
4746static inline int
4747mark_usage(struct task_struct *curr, struct held_lock *hlock, int check)
4748{
4749 return 1;
4750}
4751
4752static inline unsigned int task_irq_context(struct task_struct *task)
4753{
4754 return 0;
4755}
4756
4757static inline int separate_irq_context(struct task_struct *curr,
4758 struct held_lock *hlock)
4759{
4760 return 0;
4761}
4762
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004763static inline int check_wait_context(struct task_struct *curr,
4764 struct held_lock *next)
4765{
4766 return 0;
4767}
4768
Arnd Bergmann30a35f72019-06-28 12:29:03 +02004769#endif /* CONFIG_PROVE_LOCKING */
Arnd Bergmann886532a2019-06-17 14:47:05 +02004770
Peter Zijlstra8e182572007-07-19 01:48:54 -07004771/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004772 * Initialize a lock instance's lock-class mapping info:
4773 */
Peter Zijlstradfd5e3f2020-12-09 16:06:21 +01004774void lockdep_init_map_type(struct lockdep_map *lock, const char *name,
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004775 struct lock_class_key *key, int subclass,
Peter Zijlstradfd5e3f2020-12-09 16:06:21 +01004776 u8 inner, u8 outer, u8 lock_type)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004777{
Yong Zhangd3d03d42011-11-09 16:04:51 +08004778 int i;
4779
Yong Zhangd3d03d42011-11-09 16:04:51 +08004780 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
4781 lock->class_cache[i] = NULL;
Hitoshi Mitake62016252010-10-05 18:01:51 +09004782
Peter Zijlstrac8a25002009-04-17 09:40:49 +02004783#ifdef CONFIG_LOCK_STAT
4784 lock->cpu = raw_smp_processor_id();
4785#endif
4786
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004787 /*
4788 * Can't be having no nameless bastards around this place!
4789 */
Peter Zijlstrac8a25002009-04-17 09:40:49 +02004790 if (DEBUG_LOCKS_WARN_ON(!name)) {
4791 lock->name = "NULL";
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004792 return;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02004793 }
4794
4795 lock->name = name;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004796
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004797 lock->wait_type_outer = outer;
4798 lock->wait_type_inner = inner;
Peter Zijlstradfd5e3f2020-12-09 16:06:21 +01004799 lock->lock_type = lock_type;
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004800
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004801 /*
4802 * No key, no joy, we need to hash something.
4803 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004804 if (DEBUG_LOCKS_WARN_ON(!key))
4805 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004806 /*
Bart Van Assche108c1482019-02-14 15:00:53 -08004807 * Sanity check, the lock-class key must either have been allocated
4808 * statically or must have been registered as a dynamic key.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004809 */
Bart Van Assche108c1482019-02-14 15:00:53 -08004810 if (!static_obj(key) && !is_dynamic_key(key)) {
4811 if (debug_locks)
4812 printk(KERN_ERR "BUG: key %px has not been registered!\n", key);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004813 DEBUG_LOCKS_WARN_ON(1);
4814 return;
4815 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004816 lock->key = key;
Peter Zijlstrac8a25002009-04-17 09:40:49 +02004817
4818 if (unlikely(!debug_locks))
4819 return;
4820
Peter Zijlstra35a93932015-02-26 16:23:11 +01004821 if (subclass) {
4822 unsigned long flags;
4823
Peter Zijlstra4d004092020-10-02 11:04:21 +02004824 if (DEBUG_LOCKS_WARN_ON(!lockdep_enabled()))
Peter Zijlstra35a93932015-02-26 16:23:11 +01004825 return;
4826
4827 raw_local_irq_save(flags);
Peter Zijlstra4d004092020-10-02 11:04:21 +02004828 lockdep_recursion_inc();
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04004829 register_lock_class(lock, subclass, 1);
Peter Zijlstra10476e62020-03-13 09:56:38 +01004830 lockdep_recursion_finish();
Peter Zijlstra35a93932015-02-26 16:23:11 +01004831 raw_local_irq_restore(flags);
4832 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004833}
Peter Zijlstradfd5e3f2020-12-09 16:06:21 +01004834EXPORT_SYMBOL_GPL(lockdep_init_map_type);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004835
Peter Zijlstra1704f472010-03-19 01:37:42 +01004836struct lock_class_key __lockdep_no_validate__;
Kent Overstreetea6749c2012-12-27 22:21:58 -08004837EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
Peter Zijlstra1704f472010-03-19 01:37:42 +01004838
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004839static void
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004840print_lock_nested_lock_not_held(struct task_struct *curr,
4841 struct held_lock *hlock,
4842 unsigned long ip)
4843{
4844 if (!debug_locks_off())
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004845 return;
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004846 if (debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08004847 return;
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004848
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004849 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004850 pr_warn("==================================\n");
4851 pr_warn("WARNING: Nested lock was not taken\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004852 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08004853 pr_warn("----------------------------------\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004854
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004855 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004856 print_lock(hlock);
4857
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004858 pr_warn("\nbut this task is not holding:\n");
4859 pr_warn("%s\n", hlock->nest_lock->name);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004860
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004861 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004862 dump_stack();
4863
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004864 pr_warn("\nother info that might help us debug this:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004865 lockdep_print_held_locks(curr);
4866
Paul E. McKenney681fbec2017-05-04 15:44:38 -07004867 pr_warn("\nstack backtrace:\n");
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004868 dump_stack();
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004869}
4870
Matthew Wilcox08f36ff2018-01-17 07:14:13 -08004871static int __lock_is_held(const struct lockdep_map *lock, int read);
Maarten Lankhorstd0945952012-09-13 11:39:51 +02004872
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004873/*
4874 * This gets called for every mutex_lock*()/spin_lock*() operation.
4875 * We maintain the dependency maps and validate the locking attempt:
Waiman Long8ee10862018-10-02 16:19:17 -04004876 *
4877 * The callers must make sure that IRQs are disabled before calling it,
4878 * otherwise we could get an interrupt which would want to take locks,
4879 * which would end up in lockdep again.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004880 */
4881static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
4882 int trylock, int read, int check, int hardirqs_off,
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004883 struct lockdep_map *nest_lock, unsigned long ip,
Peter Zijlstra21199f22015-09-16 16:10:40 +02004884 int references, int pin_count)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004885{
4886 struct task_struct *curr = current;
Ingo Molnard6d897c2006-07-10 04:44:04 -07004887 struct lock_class *class = NULL;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004888 struct held_lock *hlock;
Alfredo Alvarez Fernandez5f18ab52016-02-11 00:33:32 +01004889 unsigned int depth;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004890 int chain_head = 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004891 int class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004892 u64 chain_key;
4893
4894 if (unlikely(!debug_locks))
4895 return 0;
4896
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +01004897 if (!prove_locking || lock->key == &__lockdep_no_validate__)
4898 check = 0;
Peter Zijlstra1704f472010-03-19 01:37:42 +01004899
Hitoshi Mitake62016252010-10-05 18:01:51 +09004900 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
4901 class = lock->class_cache[subclass];
Ingo Molnard6d897c2006-07-10 04:44:04 -07004902 /*
Hitoshi Mitake62016252010-10-05 18:01:51 +09004903 * Not cached?
Ingo Molnard6d897c2006-07-10 04:44:04 -07004904 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004905 if (unlikely(!class)) {
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -04004906 class = register_lock_class(lock, subclass, 0);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004907 if (!class)
4908 return 0;
4909 }
Waiman Long8ca2b56c2018-10-03 13:07:18 -04004910
4911 debug_class_ops_inc(class);
4912
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004913 if (very_verbose(class)) {
Borislav Petkov04860d42018-02-26 14:49:26 +01004914 printk("\nacquire class [%px] %s", class->key, class->name);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004915 if (class->name_version > 1)
Dmitry Vyukovf943fe02016-11-28 15:24:43 +01004916 printk(KERN_CONT "#%d", class->name_version);
4917 printk(KERN_CONT "\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004918 dump_stack();
4919 }
4920
4921 /*
4922 * Add the lock to the list of currently held locks.
4923 * (we dont increase the depth just yet, up until the
4924 * dependency checks are done)
4925 */
4926 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004927 /*
4928 * Ran out of static storage for our per-task lock stack again have we?
4929 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004930 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
4931 return 0;
4932
Yuyang Du01bb6f02019-05-06 16:19:25 +08004933 class_idx = class - lock_classes;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004934
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004935 if (depth) { /* we're holding locks */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004936 hlock = curr->held_locks + depth - 1;
4937 if (hlock->class_idx == class_idx && nest_lock) {
Imre Deakd9349852019-05-24 23:15:09 +03004938 if (!references)
4939 references++;
Peter Zijlstra7fb4a2c2017-03-01 16:23:30 +01004940
Imre Deakd9349852019-05-24 23:15:09 +03004941 if (!hlock->references)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004942 hlock->references++;
Imre Deakd9349852019-05-24 23:15:09 +03004943
4944 hlock->references += references;
4945
4946 /* Overflow */
4947 if (DEBUG_LOCKS_WARN_ON(hlock->references < references))
4948 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004949
Imre Deak8c8889d2019-05-24 23:15:08 +03004950 return 2;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004951 }
4952 }
4953
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004954 hlock = curr->held_locks + depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004955 /*
4956 * Plain impossible, we just registered it and checked it weren't no
4957 * NULL like.. I bet this mushroom I ate was good!
4958 */
Dave Jonesf82b2172008-08-11 09:30:23 +02004959 if (DEBUG_LOCKS_WARN_ON(!class))
4960 return 0;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004961 hlock->class_idx = class_idx;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004962 hlock->acquire_ip = ip;
4963 hlock->instance = lock;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02004964 hlock->nest_lock = nest_lock;
Boqun Fengc2469752016-02-16 13:57:40 +08004965 hlock->irq_context = task_irq_context(curr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004966 hlock->trylock = trylock;
4967 hlock->read = read;
4968 hlock->check = check;
Dmitry Baryshkov6951b122008-08-18 04:26:37 +04004969 hlock->hardirqs_off = !!hardirqs_off;
Peter Zijlstrabb97a912009-07-20 19:15:35 +02004970 hlock->references = references;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004971#ifdef CONFIG_LOCK_STAT
4972 hlock->waittime_stamp = 0;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02004973 hlock->holdtime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07004974#endif
Peter Zijlstra21199f22015-09-16 16:10:40 +02004975 hlock->pin_count = pin_count;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004976
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01004977 if (check_wait_context(curr, hlock))
4978 return 0;
4979
Yuyang Du09180652019-05-06 16:19:37 +08004980 /* Initialize the lock usage bit */
4981 if (!mark_usage(curr, hlock, check))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004982 return 0;
Peter Zijlstra8e182572007-07-19 01:48:54 -07004983
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004984 /*
Gautham R Shenoy17aacfb92007-10-28 20:47:01 +01004985 * Calculate the chain hash: it's the combined hash of all the
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004986 * lock keys along the dependency chain. We save the hash value
4987 * at every step so that we can get the current hash easily
4988 * after unlock. The chain hash is then used to cache dependency
4989 * results.
4990 *
4991 * The 'key ID' is what is the most compact key value to drive
4992 * the hash, not class->key.
4993 */
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004994 /*
Yuyang Du01bb6f02019-05-06 16:19:25 +08004995 * Whoops, we did it again.. class_idx is invalid.
Peter Zijlstra0119fee2011-09-02 01:30:29 +02004996 */
Yuyang Du01bb6f02019-05-06 16:19:25 +08004997 if (DEBUG_LOCKS_WARN_ON(!test_bit(class_idx, lock_classes_in_use)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07004998 return 0;
4999
5000 chain_key = curr->curr_chain_key;
5001 if (!depth) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005002 /*
5003 * How can we have a chain hash when we ain't got no keys?!
5004 */
Yuyang Duf6ec8822019-05-06 16:19:24 +08005005 if (DEBUG_LOCKS_WARN_ON(chain_key != INITIAL_CHAIN_KEY))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005006 return 0;
5007 chain_head = 1;
5008 }
5009
5010 hlock->prev_chain_key = chain_key;
Peter Zijlstra8e182572007-07-19 01:48:54 -07005011 if (separate_irq_context(curr, hlock)) {
Yuyang Duf6ec8822019-05-06 16:19:24 +08005012 chain_key = INITIAL_CHAIN_KEY;
Peter Zijlstra8e182572007-07-19 01:48:54 -07005013 chain_head = 1;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005014 }
Boqun Fengf611e8c2020-08-07 15:42:33 +08005015 chain_key = iterate_chain_key(chain_key, hlock_id(hlock));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005016
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005017 if (nest_lock && !__lock_is_held(nest_lock, -1)) {
5018 print_lock_nested_lock_not_held(curr, hlock, ip);
5019 return 0;
5020 }
Maarten Lankhorstd0945952012-09-13 11:39:51 +02005021
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005022 if (!debug_locks_silent) {
5023 WARN_ON_ONCE(depth && !hlock_class(hlock - 1)->key);
5024 WARN_ON_ONCE(!hlock_class(hlock)->key);
5025 }
5026
Yuyang Du0b9fc8e2019-05-06 16:19:26 +08005027 if (!validate_chain(curr, hlock, chain_head, chain_key))
Peter Zijlstra8e182572007-07-19 01:48:54 -07005028 return 0;
Jarek Poplawski381a2292007-02-10 01:44:58 -08005029
Gregory Haskins3aa416b2007-10-11 22:11:11 +02005030 curr->curr_chain_key = chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005031 curr->lockdep_depth++;
5032 check_chain_key(curr);
Jarek Poplawski60e114d2007-02-20 13:58:00 -08005033#ifdef CONFIG_DEBUG_LOCKDEP
5034 if (unlikely(!debug_locks))
5035 return 0;
5036#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005037 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
5038 debug_locks_off();
Dave Jones2c522832013-04-25 13:40:02 -04005039 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
5040 printk(KERN_DEBUG "depth: %i max: %lu!\n",
Ben Greearc0540602013-02-06 10:56:19 -08005041 curr->lockdep_depth, MAX_LOCK_DEPTH);
Ben Greearc0540602013-02-06 10:56:19 -08005042
5043 lockdep_print_held_locks(current);
5044 debug_show_all_locks();
Peter Zijlstraeedeeab2009-03-18 12:38:47 +01005045 dump_stack();
Ben Greearc0540602013-02-06 10:56:19 -08005046
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005047 return 0;
5048 }
Jarek Poplawski381a2292007-02-10 01:44:58 -08005049
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005050 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
5051 max_lockdep_depth = curr->lockdep_depth;
5052
5053 return 1;
5054}
5055
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005056static void print_unlock_imbalance_bug(struct task_struct *curr,
5057 struct lockdep_map *lock,
5058 unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005059{
5060 if (!debug_locks_off())
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005061 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005062 if (debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005063 return;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005064
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005065 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08005066 pr_warn("=====================================\n");
5067 pr_warn("WARNING: bad unlock balance detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01005068 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08005069 pr_warn("-------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005070 pr_warn("%s/%d is trying to release lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005071 curr->comm, task_pid_nr(curr));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005072 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005073 pr_cont(") at:\n");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07005074 print_ip_sym(KERN_WARNING, ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005075 pr_warn("but there are no more locks to release!\n");
5076 pr_warn("\nother info that might help us debug this:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005077 lockdep_print_held_locks(curr);
5078
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005079 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005080 dump_stack();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005081}
5082
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01005083static noinstr int match_held_lock(const struct held_lock *hlock,
5084 const struct lockdep_map *lock)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005085{
5086 if (hlock->instance == lock)
5087 return 1;
5088
5089 if (hlock->references) {
Matthew Wilcox08f36ff2018-01-17 07:14:13 -08005090 const struct lock_class *class = lock->class_cache[0];
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005091
5092 if (!class)
5093 class = look_up_lock_class(lock, 0);
5094
Peter Zijlstra80e04012011-08-05 14:26:17 +02005095 /*
5096 * If look_up_lock_class() failed to find a class, we're trying
5097 * to test if we hold a lock that has never yet been acquired.
5098 * Clearly if the lock hasn't been acquired _ever_, we're not
5099 * holding it either, so report failure.
5100 */
Matthew Wilcox64f29d12018-01-17 07:14:12 -08005101 if (!class)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005102 return 0;
5103
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005104 /*
5105 * References, but not a lock we're actually ref-counting?
5106 * State got messed up, follow the sites that change ->references
5107 * and try to make sense of it.
5108 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005109 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
5110 return 0;
5111
Yuyang Du01bb6f02019-05-06 16:19:25 +08005112 if (hlock->class_idx == class - lock_classes)
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005113 return 1;
5114 }
5115
5116 return 0;
5117}
5118
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005119/* @depth must not be zero */
5120static struct held_lock *find_held_lock(struct task_struct *curr,
5121 struct lockdep_map *lock,
5122 unsigned int depth, int *idx)
5123{
5124 struct held_lock *ret, *hlock, *prev_hlock;
5125 int i;
5126
5127 i = depth - 1;
5128 hlock = curr->held_locks + i;
5129 ret = hlock;
5130 if (match_held_lock(hlock, lock))
5131 goto out;
5132
5133 ret = NULL;
5134 for (i--, prev_hlock = hlock--;
5135 i >= 0;
5136 i--, prev_hlock = hlock--) {
5137 /*
5138 * We must not cross into another context:
5139 */
5140 if (prev_hlock->irq_context != hlock->irq_context) {
5141 ret = NULL;
5142 break;
5143 }
5144 if (match_held_lock(hlock, lock)) {
5145 ret = hlock;
5146 break;
5147 }
5148 }
5149
5150out:
5151 *idx = i;
5152 return ret;
5153}
5154
J. R. Okajimae9699702017-02-03 01:38:16 +09005155static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
Imre Deak8c8889d2019-05-24 23:15:08 +03005156 int idx, unsigned int *merged)
J. R. Okajimae9699702017-02-03 01:38:16 +09005157{
5158 struct held_lock *hlock;
Imre Deak8c8889d2019-05-24 23:15:08 +03005159 int first_idx = idx;
J. R. Okajimae9699702017-02-03 01:38:16 +09005160
Waiman Long8ee10862018-10-02 16:19:17 -04005161 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
5162 return 0;
5163
J. R. Okajimae9699702017-02-03 01:38:16 +09005164 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
Imre Deak8c8889d2019-05-24 23:15:08 +03005165 switch (__lock_acquire(hlock->instance,
J. R. Okajimae9699702017-02-03 01:38:16 +09005166 hlock_class(hlock)->subclass,
5167 hlock->trylock,
5168 hlock->read, hlock->check,
5169 hlock->hardirqs_off,
5170 hlock->nest_lock, hlock->acquire_ip,
Imre Deak8c8889d2019-05-24 23:15:08 +03005171 hlock->references, hlock->pin_count)) {
5172 case 0:
J. R. Okajimae9699702017-02-03 01:38:16 +09005173 return 1;
Imre Deak8c8889d2019-05-24 23:15:08 +03005174 case 1:
5175 break;
5176 case 2:
5177 *merged += (idx == first_idx);
5178 break;
5179 default:
5180 WARN_ON(1);
5181 return 0;
5182 }
J. R. Okajimae9699702017-02-03 01:38:16 +09005183 }
5184 return 0;
5185}
5186
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005187static int
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01005188__lock_set_class(struct lockdep_map *lock, const char *name,
5189 struct lock_class_key *key, unsigned int subclass,
5190 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005191{
5192 struct task_struct *curr = current;
Imre Deak8c8889d2019-05-24 23:15:08 +03005193 unsigned int depth, merged = 0;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005194 struct held_lock *hlock;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005195 struct lock_class *class;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005196 int i;
5197
Waiman Long513e1072019-01-09 23:03:25 -05005198 if (unlikely(!debug_locks))
5199 return 0;
5200
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005201 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005202 /*
5203 * This function is about (re)setting the class of a held lock,
5204 * yet we're not actually holding any locks. Naughty user!
5205 */
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005206 if (DEBUG_LOCKS_WARN_ON(!depth))
5207 return 0;
5208
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005209 hlock = find_held_lock(curr, lock, depth, &i);
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005210 if (!hlock) {
5211 print_unlock_imbalance_bug(curr, lock, ip);
5212 return 0;
5213 }
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005214
Peter Zijlstrade8f5e42020-03-21 12:26:01 +01005215 lockdep_init_map_waits(lock, name, key, 0,
5216 lock->wait_type_inner,
5217 lock->wait_type_outer);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005218 class = register_lock_class(lock, subclass, 0);
Yuyang Du01bb6f02019-05-06 16:19:25 +08005219 hlock->class_idx = class - lock_classes;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005220
5221 curr->lockdep_depth = i;
5222 curr->curr_chain_key = hlock->prev_chain_key;
5223
Imre Deak8c8889d2019-05-24 23:15:08 +03005224 if (reacquire_held_locks(curr, depth, i, &merged))
J. R. Okajimae9699702017-02-03 01:38:16 +09005225 return 0;
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005226
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005227 /*
5228 * I took it apart and put it back together again, except now I have
5229 * these 'spare' parts.. where shall I put them.
5230 */
Imre Deak8c8889d2019-05-24 23:15:08 +03005231 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005232 return 0;
5233 return 1;
5234}
5235
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005236static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
5237{
5238 struct task_struct *curr = current;
Imre Deak8c8889d2019-05-24 23:15:08 +03005239 unsigned int depth, merged = 0;
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005240 struct held_lock *hlock;
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005241 int i;
5242
Waiman Long71492582019-01-09 23:03:25 -05005243 if (unlikely(!debug_locks))
5244 return 0;
5245
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005246 depth = curr->lockdep_depth;
5247 /*
5248 * This function is about (re)setting the class of a held lock,
5249 * yet we're not actually holding any locks. Naughty user!
5250 */
5251 if (DEBUG_LOCKS_WARN_ON(!depth))
5252 return 0;
5253
5254 hlock = find_held_lock(curr, lock, depth, &i);
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005255 if (!hlock) {
5256 print_unlock_imbalance_bug(curr, lock, ip);
5257 return 0;
5258 }
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005259
5260 curr->lockdep_depth = i;
5261 curr->curr_chain_key = hlock->prev_chain_key;
5262
5263 WARN(hlock->read, "downgrading a read lock");
5264 hlock->read = 1;
5265 hlock->acquire_ip = ip;
5266
Imre Deak8c8889d2019-05-24 23:15:08 +03005267 if (reacquire_held_locks(curr, depth, i, &merged))
5268 return 0;
5269
5270 /* Merging can't happen with unchanged classes.. */
5271 if (DEBUG_LOCKS_WARN_ON(merged))
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005272 return 0;
5273
5274 /*
5275 * I took it apart and put it back together again, except now I have
5276 * these 'spare' parts.. where shall I put them.
5277 */
5278 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
5279 return 0;
Imre Deak8c8889d2019-05-24 23:15:08 +03005280
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005281 return 1;
5282}
5283
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005284/*
Dan Carpenterc759bc42019-11-04 12:12:52 +03005285 * Remove the lock from the list of currently held locks - this gets
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02005286 * called on mutex_unlock()/spin_unlock*() (or on a failed
5287 * mutex_lock_interruptible()).
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005288 */
5289static int
Yuyang Dub4adfe8e2019-05-06 16:19:34 +08005290__lock_release(struct lockdep_map *lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005291{
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02005292 struct task_struct *curr = current;
Imre Deak8c8889d2019-05-24 23:15:08 +03005293 unsigned int depth, merged = 1;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005294 struct held_lock *hlock;
Ingo Molnare966eae2017-12-12 12:31:16 +01005295 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005296
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02005297 if (unlikely(!debug_locks))
5298 return 0;
5299
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005300 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005301 /*
5302 * So we're all set to release this lock.. wait what lock? We don't
5303 * own any locks, you've been drinking again?
5304 */
Kobe Wudd471ef2019-05-30 19:59:35 +08005305 if (depth <= 0) {
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005306 print_unlock_imbalance_bug(curr, lock, ip);
5307 return 0;
5308 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005309
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02005310 /*
5311 * Check whether the lock exists in the current stack
5312 * of held locks:
5313 */
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005314 hlock = find_held_lock(curr, lock, depth, &i);
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005315 if (!hlock) {
5316 print_unlock_imbalance_bug(curr, lock, ip);
5317 return 0;
5318 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005319
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005320 if (hlock->instance == lock)
5321 lock_release_holdtime(hlock);
5322
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005323 WARN(hlock->pin_count, "releasing a pinned lock\n");
5324
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005325 if (hlock->references) {
5326 hlock->references--;
5327 if (hlock->references) {
5328 /*
5329 * We had, and after removing one, still have
5330 * references, the current lock stack is still
5331 * valid. We're done!
5332 */
5333 return 1;
5334 }
5335 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005336
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005337 /*
5338 * We have the right lock to unlock, 'hlock' points to it.
5339 * Now we remove it from the stack, and add back the other
5340 * entries (if any), recalculating the hash along the way:
5341 */
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005342
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005343 curr->lockdep_depth = i;
5344 curr->curr_chain_key = hlock->prev_chain_key;
5345
Waiman Longce52a182018-10-02 16:19:18 -04005346 /*
5347 * The most likely case is when the unlock is on the innermost
5348 * lock. In this case, we are done!
5349 */
5350 if (i == depth-1)
5351 return 1;
5352
Imre Deak8c8889d2019-05-24 23:15:08 +03005353 if (reacquire_held_locks(curr, depth, i + 1, &merged))
J. R. Okajimae9699702017-02-03 01:38:16 +09005354 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005355
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005356 /*
5357 * We had N bottles of beer on the wall, we drank one, but now
5358 * there's not N-1 bottles of beer left on the wall...
Imre Deak8c8889d2019-05-24 23:15:08 +03005359 * Pouring two of the bottles together is acceptable.
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005360 */
Imre Deak8c8889d2019-05-24 23:15:08 +03005361 DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged);
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02005362
Waiman Longce52a182018-10-02 16:19:18 -04005363 /*
5364 * Since reacquire_held_locks() would have called check_chain_key()
5365 * indirectly via __lock_acquire(), we don't need to do it again
5366 * on return.
5367 */
5368 return 0;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005369}
5370
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01005371static __always_inline
Masami Hiramatsu2f43c602019-02-13 01:15:05 +09005372int __lock_is_held(const struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02005373{
5374 struct task_struct *curr = current;
5375 int i;
5376
5377 for (i = 0; i < curr->lockdep_depth; i++) {
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005378 struct held_lock *hlock = curr->held_locks + i;
5379
Peter Zijlstraf8319482016-11-30 14:32:25 +11005380 if (match_held_lock(hlock, lock)) {
Sebastian Andrzej Siewior25070032021-09-03 10:40:01 +02005381 if (read == -1 || !!hlock->read == read)
Shuah Khanf8cfa462021-02-26 17:06:59 -07005382 return LOCK_STATE_HELD;
Peter Zijlstraf8319482016-11-30 14:32:25 +11005383
Shuah Khanf8cfa462021-02-26 17:06:59 -07005384 return LOCK_STATE_NOT_HELD;
Peter Zijlstraf8319482016-11-30 14:32:25 +11005385 }
Peter Zijlstraf607c662009-07-20 19:16:29 +02005386 }
5387
Shuah Khanf8cfa462021-02-26 17:06:59 -07005388 return LOCK_STATE_NOT_HELD;
Peter Zijlstraf607c662009-07-20 19:16:29 +02005389}
5390
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005391static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
5392{
5393 struct pin_cookie cookie = NIL_COOKIE;
5394 struct task_struct *curr = current;
5395 int i;
5396
5397 if (unlikely(!debug_locks))
5398 return cookie;
5399
5400 for (i = 0; i < curr->lockdep_depth; i++) {
5401 struct held_lock *hlock = curr->held_locks + i;
5402
5403 if (match_held_lock(hlock, lock)) {
5404 /*
5405 * Grab 16bits of randomness; this is sufficient to not
5406 * be guessable and still allows some pin nesting in
5407 * our u32 pin_count.
5408 */
5409 cookie.val = 1 + (prandom_u32() >> 16);
5410 hlock->pin_count += cookie.val;
5411 return cookie;
5412 }
5413 }
5414
5415 WARN(1, "pinning an unheld lock\n");
5416 return cookie;
5417}
5418
5419static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005420{
5421 struct task_struct *curr = current;
5422 int i;
5423
5424 if (unlikely(!debug_locks))
5425 return;
5426
5427 for (i = 0; i < curr->lockdep_depth; i++) {
5428 struct held_lock *hlock = curr->held_locks + i;
5429
5430 if (match_held_lock(hlock, lock)) {
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005431 hlock->pin_count += cookie.val;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005432 return;
5433 }
5434 }
5435
5436 WARN(1, "pinning an unheld lock\n");
5437}
5438
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005439static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005440{
5441 struct task_struct *curr = current;
5442 int i;
5443
5444 if (unlikely(!debug_locks))
5445 return;
5446
5447 for (i = 0; i < curr->lockdep_depth; i++) {
5448 struct held_lock *hlock = curr->held_locks + i;
5449
5450 if (match_held_lock(hlock, lock)) {
5451 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
5452 return;
5453
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005454 hlock->pin_count -= cookie.val;
5455
5456 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
5457 hlock->pin_count = 0;
5458
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005459 return;
5460 }
5461 }
5462
5463 WARN(1, "unpinning an unheld lock\n");
5464}
5465
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005466/*
5467 * Check whether we follow the irq-flags state precisely:
5468 */
Peter Zijlstra77ca93a2021-01-06 15:36:23 +01005469static noinstr void check_flags(unsigned long flags)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005470{
Arnd Bergmann30a35f72019-06-28 12:29:03 +02005471#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005472 if (!debug_locks)
5473 return;
5474
Peter Zijlstra77ca93a2021-01-06 15:36:23 +01005475 /* Get the warning out.. */
5476 instrumentation_begin();
5477
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01005478 if (irqs_disabled_flags(flags)) {
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02005479 if (DEBUG_LOCKS_WARN_ON(lockdep_hardirqs_enabled())) {
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01005480 printk("possible reason: unannotated irqs-off.\n");
5481 }
5482 } else {
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +02005483 if (DEBUG_LOCKS_WARN_ON(!lockdep_hardirqs_enabled())) {
Ingo Molnar5f9fa8a2007-12-07 19:02:47 +01005484 printk("possible reason: unannotated irqs-on.\n");
5485 }
5486 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005487
5488 /*
5489 * We dont accurately track softirq state in e.g.
5490 * hardirq contexts (such as on 4KSTACKS), so only
5491 * check if not in hardirq contexts:
5492 */
5493 if (!hardirq_count()) {
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005494 if (softirq_count()) {
5495 /* like the above, but with softirqs */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005496 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005497 } else {
5498 /* lick the above, does it taste good? */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005499 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005500 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005501 }
5502
5503 if (!debug_locks)
5504 print_irqtrace_events(current);
Peter Zijlstra77ca93a2021-01-06 15:36:23 +01005505
5506 instrumentation_end();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005507#endif
5508}
5509
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01005510void lock_set_class(struct lockdep_map *lock, const char *name,
5511 struct lock_class_key *key, unsigned int subclass,
5512 unsigned long ip)
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005513{
5514 unsigned long flags;
5515
Peter Zijlstra4d004092020-10-02 11:04:21 +02005516 if (unlikely(!lockdep_enabled()))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005517 return;
5518
5519 raw_local_irq_save(flags);
Peter Zijlstra4d004092020-10-02 11:04:21 +02005520 lockdep_recursion_inc();
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005521 check_flags(flags);
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01005522 if (__lock_set_class(lock, name, key, subclass, ip))
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005523 check_chain_key(current);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005524 lockdep_recursion_finish();
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005525 raw_local_irq_restore(flags);
5526}
Peter Zijlstra00ef9f72008-12-04 09:00:17 +01005527EXPORT_SYMBOL_GPL(lock_set_class);
Peter Zijlstra64aa3482008-08-11 09:30:21 +02005528
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005529void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
5530{
5531 unsigned long flags;
5532
Peter Zijlstra4d004092020-10-02 11:04:21 +02005533 if (unlikely(!lockdep_enabled()))
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005534 return;
5535
5536 raw_local_irq_save(flags);
Peter Zijlstra4d004092020-10-02 11:04:21 +02005537 lockdep_recursion_inc();
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005538 check_flags(flags);
5539 if (__lock_downgrade(lock, ip))
5540 check_chain_key(current);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005541 lockdep_recursion_finish();
J. R. Okajima6419c4a2017-02-03 01:38:17 +09005542 raw_local_irq_restore(flags);
5543}
5544EXPORT_SYMBOL_GPL(lock_downgrade);
5545
Peter Zijlstraf6f48e12020-02-20 09:45:02 +01005546/* NMI context !!! */
5547static void verify_lock_unused(struct lockdep_map *lock, struct held_lock *hlock, int subclass)
5548{
5549#ifdef CONFIG_PROVE_LOCKING
5550 struct lock_class *class = look_up_lock_class(lock, subclass);
peterz@infradead.org23870f12020-09-02 18:03:23 +02005551 unsigned long mask = LOCKF_USED;
Peter Zijlstraf6f48e12020-02-20 09:45:02 +01005552
5553 /* if it doesn't have a class (yet), it certainly hasn't been used yet */
5554 if (!class)
5555 return;
5556
peterz@infradead.org23870f12020-09-02 18:03:23 +02005557 /*
5558 * READ locks only conflict with USED, such that if we only ever use
5559 * READ locks, there is no deadlock possible -- RCU.
5560 */
5561 if (!hlock->read)
5562 mask |= LOCKF_USED_READ;
5563
5564 if (!(class->usage_mask & mask))
Peter Zijlstraf6f48e12020-02-20 09:45:02 +01005565 return;
5566
5567 hlock->class_idx = class - lock_classes;
5568
5569 print_usage_bug(current, hlock, LOCK_USED, LOCK_USAGE_STATES);
5570#endif
5571}
5572
5573static bool lockdep_nmi(void)
5574{
Peter Zijlstra4d004092020-10-02 11:04:21 +02005575 if (raw_cpu_read(lockdep_recursion))
Peter Zijlstraf6f48e12020-02-20 09:45:02 +01005576 return false;
5577
5578 if (!in_nmi())
5579 return false;
5580
5581 return true;
5582}
5583
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005584/*
Boqun Fenge9181882020-08-07 15:42:20 +08005585 * read_lock() is recursive if:
5586 * 1. We force lockdep think this way in selftests or
5587 * 2. The implementation is not queued read/write lock or
5588 * 3. The locker is at an in_interrupt() context.
5589 */
5590bool read_lock_is_recursive(void)
5591{
5592 return force_read_lock_recursive ||
5593 !IS_ENABLED(CONFIG_QUEUED_RWLOCKS) ||
5594 in_interrupt();
5595}
5596EXPORT_SYMBOL_GPL(read_lock_is_recursive);
5597
5598/*
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005599 * We are not always called with irqs disabled - do that here,
5600 * and also avoid lockdep recursion:
5601 */
Steven Rostedt1d09daa2008-05-12 21:20:55 +02005602void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +02005603 int trylock, int read, int check,
5604 struct lockdep_map *nest_lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005605{
5606 unsigned long flags;
5607
Peter Zijlstraeb1f0022020-08-07 20:53:16 +02005608 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
5609
Peter Zijlstra4d004092020-10-02 11:04:21 +02005610 if (!debug_locks)
5611 return;
5612
5613 if (unlikely(!lockdep_enabled())) {
Peter Zijlstraf6f48e12020-02-20 09:45:02 +01005614 /* XXX allow trylock from NMI ?!? */
5615 if (lockdep_nmi() && !trylock) {
5616 struct held_lock hlock;
5617
5618 hlock.acquire_ip = ip;
5619 hlock.instance = lock;
5620 hlock.nest_lock = nest_lock;
5621 hlock.irq_context = 2; // XXX
5622 hlock.trylock = trylock;
5623 hlock.read = read;
5624 hlock.check = check;
5625 hlock.hardirqs_off = true;
5626 hlock.references = 0;
5627
5628 verify_lock_unused(lock, &hlock, subclass);
5629 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005630 return;
Peter Zijlstraf6f48e12020-02-20 09:45:02 +01005631 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005632
5633 raw_local_irq_save(flags);
5634 check_flags(flags);
5635
Peter Zijlstra4d004092020-10-02 11:04:21 +02005636 lockdep_recursion_inc();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005637 __lock_acquire(lock, subclass, trylock, read, check,
Peter Zijlstra21199f22015-09-16 16:10:40 +02005638 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005639 lockdep_recursion_finish();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005640 raw_local_irq_restore(flags);
5641}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005642EXPORT_SYMBOL_GPL(lock_acquire);
5643
Qian Cai5facae42019-09-19 12:09:40 -04005644void lock_release(struct lockdep_map *lock, unsigned long ip)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005645{
5646 unsigned long flags;
5647
Peter Zijlstraeb1f0022020-08-07 20:53:16 +02005648 trace_lock_release(lock, ip);
5649
Peter Zijlstra4d004092020-10-02 11:04:21 +02005650 if (unlikely(!lockdep_enabled()))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005651 return;
5652
5653 raw_local_irq_save(flags);
5654 check_flags(flags);
Peter Zijlstraeb1f0022020-08-07 20:53:16 +02005655
Peter Zijlstra4d004092020-10-02 11:04:21 +02005656 lockdep_recursion_inc();
Yuyang Dub4adfe8e2019-05-06 16:19:34 +08005657 if (__lock_release(lock, ip))
Peter Zijlstrae0f56fd2015-06-11 14:46:52 +02005658 check_chain_key(current);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005659 lockdep_recursion_finish();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005660 raw_local_irq_restore(flags);
5661}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005662EXPORT_SYMBOL_GPL(lock_release);
5663
Peter Zijlstrac86e9b92020-03-18 14:22:03 +01005664noinstr int lock_is_held_type(const struct lockdep_map *lock, int read)
Peter Zijlstraf607c662009-07-20 19:16:29 +02005665{
5666 unsigned long flags;
Shuah Khanf8cfa462021-02-26 17:06:59 -07005667 int ret = LOCK_STATE_NOT_HELD;
Peter Zijlstraf607c662009-07-20 19:16:29 +02005668
Shuah Khan3e31f942021-02-26 17:06:58 -07005669 /*
5670 * Avoid false negative lockdep_assert_held() and
5671 * lockdep_assert_not_held().
5672 */
Peter Zijlstra4d004092020-10-02 11:04:21 +02005673 if (unlikely(!lockdep_enabled()))
Shuah Khanf8cfa462021-02-26 17:06:59 -07005674 return LOCK_STATE_UNKNOWN;
Peter Zijlstraf607c662009-07-20 19:16:29 +02005675
5676 raw_local_irq_save(flags);
5677 check_flags(flags);
5678
Peter Zijlstra4d004092020-10-02 11:04:21 +02005679 lockdep_recursion_inc();
Peter Zijlstraf8319482016-11-30 14:32:25 +11005680 ret = __lock_is_held(lock, read);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005681 lockdep_recursion_finish();
Peter Zijlstraf607c662009-07-20 19:16:29 +02005682 raw_local_irq_restore(flags);
5683
5684 return ret;
5685}
Peter Zijlstraf8319482016-11-30 14:32:25 +11005686EXPORT_SYMBOL_GPL(lock_is_held_type);
Masami Hiramatsu2f43c602019-02-13 01:15:05 +09005687NOKPROBE_SYMBOL(lock_is_held_type);
Peter Zijlstraf607c662009-07-20 19:16:29 +02005688
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005689struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005690{
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005691 struct pin_cookie cookie = NIL_COOKIE;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005692 unsigned long flags;
5693
Peter Zijlstra4d004092020-10-02 11:04:21 +02005694 if (unlikely(!lockdep_enabled()))
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005695 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005696
5697 raw_local_irq_save(flags);
5698 check_flags(flags);
5699
Peter Zijlstra4d004092020-10-02 11:04:21 +02005700 lockdep_recursion_inc();
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005701 cookie = __lock_pin_lock(lock);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005702 lockdep_recursion_finish();
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005703 raw_local_irq_restore(flags);
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005704
5705 return cookie;
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005706}
5707EXPORT_SYMBOL_GPL(lock_pin_lock);
5708
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005709void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005710{
5711 unsigned long flags;
5712
Peter Zijlstra4d004092020-10-02 11:04:21 +02005713 if (unlikely(!lockdep_enabled()))
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005714 return;
5715
5716 raw_local_irq_save(flags);
5717 check_flags(flags);
5718
Peter Zijlstra4d004092020-10-02 11:04:21 +02005719 lockdep_recursion_inc();
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005720 __lock_repin_lock(lock, cookie);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005721 lockdep_recursion_finish();
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005722 raw_local_irq_restore(flags);
5723}
5724EXPORT_SYMBOL_GPL(lock_repin_lock);
5725
5726void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
5727{
5728 unsigned long flags;
5729
Peter Zijlstra4d004092020-10-02 11:04:21 +02005730 if (unlikely(!lockdep_enabled()))
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005731 return;
5732
5733 raw_local_irq_save(flags);
5734 check_flags(flags);
5735
Peter Zijlstra4d004092020-10-02 11:04:21 +02005736 lockdep_recursion_inc();
Peter Zijlstrae7904a22015-08-01 19:25:08 +02005737 __lock_unpin_lock(lock, cookie);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005738 lockdep_recursion_finish();
Peter Zijlstraa24fc602015-06-11 14:46:53 +02005739 raw_local_irq_restore(flags);
5740}
5741EXPORT_SYMBOL_GPL(lock_unpin_lock);
5742
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005743#ifdef CONFIG_LOCK_STAT
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005744static void print_lock_contention_bug(struct task_struct *curr,
5745 struct lockdep_map *lock,
5746 unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005747{
5748 if (!debug_locks_off())
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005749 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005750 if (debug_locks_silent)
Yuyang Duf7c1c6b2019-05-06 16:19:17 +08005751 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005752
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005753 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08005754 pr_warn("=================================\n");
5755 pr_warn("WARNING: bad contention detected!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01005756 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08005757 pr_warn("---------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005758 pr_warn("%s/%d is trying to contend lock (",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005759 curr->comm, task_pid_nr(curr));
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005760 print_lockdep_cache(lock);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005761 pr_cont(") at:\n");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07005762 print_ip_sym(KERN_WARNING, ip);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005763 pr_warn("but there are no locks held!\n");
5764 pr_warn("\nother info that might help us debug this:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005765 lockdep_print_held_locks(curr);
5766
Paul E. McKenney681fbec2017-05-04 15:44:38 -07005767 pr_warn("\nstack backtrace:\n");
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005768 dump_stack();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005769}
5770
5771static void
5772__lock_contended(struct lockdep_map *lock, unsigned long ip)
5773{
5774 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005775 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005776 struct lock_class_stats *stats;
5777 unsigned int depth;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005778 int i, contention_point, contending_point;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005779
5780 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005781 /*
5782 * Whee, we contended on this lock, except it seems we're not
5783 * actually trying to acquire anything much at all..
5784 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005785 if (DEBUG_LOCKS_WARN_ON(!depth))
5786 return;
5787
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005788 hlock = find_held_lock(curr, lock, depth, &i);
5789 if (!hlock) {
5790 print_lock_contention_bug(curr, lock, ip);
5791 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005792 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005793
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005794 if (hlock->instance != lock)
5795 return;
5796
Peter Zijlstra3365e7792009-10-09 10:12:41 +02005797 hlock->waittime_stamp = lockstat_clock();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005798
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005799 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
5800 contending_point = lock_point(hlock_class(hlock)->contending_point,
5801 lock->ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005802
Dave Jonesf82b2172008-08-11 09:30:23 +02005803 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005804 if (contention_point < LOCKSTAT_POINTS)
5805 stats->contention_point[contention_point]++;
5806 if (contending_point < LOCKSTAT_POINTS)
5807 stats->contending_point[contending_point]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07005808 if (lock->cpu != smp_processor_id())
5809 stats->bounces[bounce_contended + !!hlock->read]++;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005810}
5811
5812static void
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005813__lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005814{
5815 struct task_struct *curr = current;
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005816 struct held_lock *hlock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005817 struct lock_class_stats *stats;
5818 unsigned int depth;
Peter Zijlstra3365e7792009-10-09 10:12:41 +02005819 u64 now, waittime = 0;
Peter Zijlstra96645672007-07-19 01:49:00 -07005820 int i, cpu;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005821
5822 depth = curr->lockdep_depth;
Peter Zijlstra0119fee2011-09-02 01:30:29 +02005823 /*
5824 * Yay, we acquired ownership of this lock we didn't try to
5825 * acquire, how the heck did that happen?
5826 */
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005827 if (DEBUG_LOCKS_WARN_ON(!depth))
5828 return;
5829
J. R. Okajima41c2c5b2017-02-03 01:38:15 +09005830 hlock = find_held_lock(curr, lock, depth, &i);
5831 if (!hlock) {
5832 print_lock_contention_bug(curr, lock, _RET_IP_);
5833 return;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005834 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005835
Peter Zijlstrabb97a912009-07-20 19:15:35 +02005836 if (hlock->instance != lock)
5837 return;
5838
Peter Zijlstra96645672007-07-19 01:49:00 -07005839 cpu = smp_processor_id();
5840 if (hlock->waittime_stamp) {
Peter Zijlstra3365e7792009-10-09 10:12:41 +02005841 now = lockstat_clock();
Peter Zijlstra96645672007-07-19 01:49:00 -07005842 waittime = now - hlock->waittime_stamp;
5843 hlock->holdtime_stamp = now;
5844 }
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005845
Dave Jonesf82b2172008-08-11 09:30:23 +02005846 stats = get_lock_stats(hlock_class(hlock));
Peter Zijlstra96645672007-07-19 01:49:00 -07005847 if (waittime) {
5848 if (hlock->read)
5849 lock_time_inc(&stats->read_waittime, waittime);
5850 else
5851 lock_time_inc(&stats->write_waittime, waittime);
5852 }
5853 if (lock->cpu != cpu)
5854 stats->bounces[bounce_acquired + !!hlock->read]++;
Peter Zijlstra96645672007-07-19 01:49:00 -07005855
5856 lock->cpu = cpu;
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005857 lock->ip = ip;
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005858}
5859
5860void lock_contended(struct lockdep_map *lock, unsigned long ip)
5861{
5862 unsigned long flags;
5863
Leo Yan89e70d52021-05-12 20:09:37 +08005864 trace_lock_contended(lock, ip);
Peter Zijlstraeb1f0022020-08-07 20:53:16 +02005865
Peter Zijlstra4d004092020-10-02 11:04:21 +02005866 if (unlikely(!lock_stat || !lockdep_enabled()))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005867 return;
5868
5869 raw_local_irq_save(flags);
5870 check_flags(flags);
Peter Zijlstra4d004092020-10-02 11:04:21 +02005871 lockdep_recursion_inc();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005872 __lock_contended(lock, ip);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005873 lockdep_recursion_finish();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005874 raw_local_irq_restore(flags);
5875}
5876EXPORT_SYMBOL_GPL(lock_contended);
5877
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005878void lock_acquired(struct lockdep_map *lock, unsigned long ip)
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005879{
5880 unsigned long flags;
5881
Leo Yan89e70d52021-05-12 20:09:37 +08005882 trace_lock_acquired(lock, ip);
Peter Zijlstraeb1f0022020-08-07 20:53:16 +02005883
Peter Zijlstra4d004092020-10-02 11:04:21 +02005884 if (unlikely(!lock_stat || !lockdep_enabled()))
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005885 return;
5886
5887 raw_local_irq_save(flags);
5888 check_flags(flags);
Peter Zijlstra4d004092020-10-02 11:04:21 +02005889 lockdep_recursion_inc();
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +02005890 __lock_acquired(lock, ip);
Peter Zijlstra10476e62020-03-13 09:56:38 +01005891 lockdep_recursion_finish();
Peter Zijlstraf20786f2007-07-19 01:48:56 -07005892 raw_local_irq_restore(flags);
5893}
5894EXPORT_SYMBOL_GPL(lock_acquired);
5895#endif
5896
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005897/*
5898 * Used by the testsuite, sanitize the validator state
5899 * after a simulated failure:
5900 */
5901
5902void lockdep_reset(void)
5903{
5904 unsigned long flags;
Ingo Molnar23d95a02006-12-13 00:34:40 -08005905 int i;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005906
5907 raw_local_irq_save(flags);
Yuyang Due196e472019-05-06 16:19:23 +08005908 lockdep_init_task(current);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005909 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
5910 nr_hardirq_chains = 0;
5911 nr_softirq_chains = 0;
5912 nr_process_chains = 0;
5913 debug_locks = 1;
Ingo Molnar23d95a02006-12-13 00:34:40 -08005914 for (i = 0; i < CHAINHASH_SIZE; i++)
Andrew Mortona63f38c2016-02-03 13:44:12 -08005915 INIT_HLIST_HEAD(chainhash_table + i);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005916 raw_local_irq_restore(flags);
5917}
5918
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005919/* Remove a class from a lock chain. Must be called with the graph lock held. */
Bart Van Asschede4643a2019-02-14 15:00:50 -08005920static void remove_class_from_lock_chain(struct pending_free *pf,
5921 struct lock_chain *chain,
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005922 struct lock_class *class)
5923{
5924#ifdef CONFIG_PROVE_LOCKING
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005925 int i;
5926
5927 for (i = chain->base; i < chain->base + chain->depth; i++) {
Boqun Fengf611e8c2020-08-07 15:42:33 +08005928 if (chain_hlock_class_idx(chain_hlocks[i]) != class - lock_classes)
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005929 continue;
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005930 /*
5931 * Each lock class occurs at most once in a lock chain so once
5932 * we found a match we can break out of this loop.
5933 */
Waiman Long836bd742020-02-06 10:24:06 -05005934 goto free_lock_chain;
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005935 }
5936 /* Since the chain has not been modified, return. */
5937 return;
5938
Waiman Long836bd742020-02-06 10:24:06 -05005939free_lock_chain:
Waiman Long810507f2020-02-06 10:24:08 -05005940 free_chain_hlocks(chain->base, chain->depth);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005941 /* Overwrite the chain key for concurrent RCU readers. */
Waiman Long836bd742020-02-06 10:24:06 -05005942 WRITE_ONCE(chain->chain_key, INITIAL_CHAIN_KEY);
Waiman Longb3b9c182020-02-06 10:24:03 -05005943 dec_chains(chain->irq_context);
5944
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005945 /*
5946 * Note: calling hlist_del_rcu() from inside a
5947 * hlist_for_each_entry_rcu() loop is safe.
5948 */
5949 hlist_del_rcu(&chain->entry);
Bart Van Asschede4643a2019-02-14 15:00:50 -08005950 __set_bit(chain - lock_chains, pf->lock_chains_being_freed);
Waiman Long797b82eb2020-02-06 10:24:07 -05005951 nr_zapped_lock_chains++;
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005952#endif
5953}
5954
5955/* Must be called with the graph lock held. */
Bart Van Asschede4643a2019-02-14 15:00:50 -08005956static void remove_class_from_lock_chains(struct pending_free *pf,
5957 struct lock_class *class)
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005958{
5959 struct lock_chain *chain;
5960 struct hlist_head *head;
5961 int i;
5962
5963 for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
5964 head = chainhash_table + i;
5965 hlist_for_each_entry_rcu(chain, head, entry) {
Bart Van Asschede4643a2019-02-14 15:00:50 -08005966 remove_class_from_lock_chain(pf, chain, class);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005967 }
5968 }
5969}
5970
Bart Van Assche786fa292018-12-06 17:11:36 -08005971/*
5972 * Remove all references to a lock class. The caller must hold the graph lock.
5973 */
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005974static void zap_class(struct pending_free *pf, struct lock_class *class)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005975{
Bart Van Assche86cffb82019-02-14 15:00:41 -08005976 struct lock_list *entry;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005977 int i;
5978
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005979 WARN_ON_ONCE(!class->key);
5980
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005981 /*
5982 * Remove all dependencies this lock is
5983 * involved in:
5984 */
Bart Van Asscheace35a72019-02-14 15:00:47 -08005985 for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
5986 entry = list_entries + i;
Bart Van Assche86cffb82019-02-14 15:00:41 -08005987 if (entry->class != class && entry->links_to != class)
5988 continue;
Bart Van Asscheace35a72019-02-14 15:00:47 -08005989 __clear_bit(i, list_entries_in_use);
5990 nr_list_entries--;
Bart Van Assche86cffb82019-02-14 15:00:41 -08005991 list_del_rcu(&entry->entry);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07005992 }
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08005993 if (list_empty(&class->locks_after) &&
5994 list_empty(&class->locks_before)) {
5995 list_move_tail(&class->lock_entry, &pf->zapped);
5996 hlist_del_rcu(&class->hash_entry);
5997 WRITE_ONCE(class->key, NULL);
5998 WRITE_ONCE(class->name, NULL);
5999 nr_lock_classes--;
Yuyang Du01bb6f02019-05-06 16:19:25 +08006000 __clear_bit(class - lock_classes, lock_classes_in_use);
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006001 } else {
6002 WARN_ONCE(true, "%s() failed for class %s\n", __func__,
6003 class->name);
6004 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006005
Bart Van Asschede4643a2019-02-14 15:00:50 -08006006 remove_class_from_lock_chains(pf, class);
Waiman Long1d44bcb2020-02-06 10:24:05 -05006007 nr_zapped_classes++;
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006008}
6009
6010static void reinit_class(struct lock_class *class)
6011{
6012 void *const p = class;
6013 const unsigned int offset = offsetof(struct lock_class, key);
6014
6015 WARN_ON_ONCE(!class->lock_entry.next);
6016 WARN_ON_ONCE(!list_empty(&class->locks_after));
6017 WARN_ON_ONCE(!list_empty(&class->locks_before));
6018 memset(p + offset, 0, sizeof(*class) - offset);
6019 WARN_ON_ONCE(!class->lock_entry.next);
6020 WARN_ON_ONCE(!list_empty(&class->locks_after));
6021 WARN_ON_ONCE(!list_empty(&class->locks_before));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006022}
6023
Arjan van de Venfabe8742008-01-24 07:00:45 +01006024static inline int within(const void *addr, void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006025{
6026 return addr >= start && addr < start + size;
6027}
6028
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006029static bool inside_selftest(void)
6030{
6031 return current == lockdep_selftest_task_struct;
6032}
6033
6034/* The caller must hold the graph lock. */
6035static struct pending_free *get_pending_free(void)
6036{
6037 return delayed_free.pf + delayed_free.index;
6038}
6039
6040static void free_zapped_rcu(struct rcu_head *cb);
6041
6042/*
6043 * Schedule an RCU callback if no RCU callback is pending. Must be called with
6044 * the graph lock held.
6045 */
6046static void call_rcu_zapped(struct pending_free *pf)
6047{
6048 WARN_ON_ONCE(inside_selftest());
6049
6050 if (list_empty(&pf->zapped))
6051 return;
6052
6053 if (delayed_free.scheduled)
6054 return;
6055
6056 delayed_free.scheduled = true;
6057
6058 WARN_ON_ONCE(delayed_free.pf + delayed_free.index != pf);
6059 delayed_free.index ^= 1;
6060
6061 call_rcu(&delayed_free.rcu_head, free_zapped_rcu);
6062}
6063
6064/* The caller must hold the graph lock. May be called from RCU context. */
6065static void __free_zapped_classes(struct pending_free *pf)
6066{
6067 struct lock_class *class;
6068
Peter Zijlstra72dcd502019-02-25 15:56:32 +01006069 check_data_structures();
Bart Van Asscheb526b2e2019-02-14 15:00:51 -08006070
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006071 list_for_each_entry(class, &pf->zapped, lock_entry)
6072 reinit_class(class);
6073
6074 list_splice_init(&pf->zapped, &free_lock_classes);
Bart Van Asschede4643a2019-02-14 15:00:50 -08006075
6076#ifdef CONFIG_PROVE_LOCKING
6077 bitmap_andnot(lock_chains_in_use, lock_chains_in_use,
6078 pf->lock_chains_being_freed, ARRAY_SIZE(lock_chains));
6079 bitmap_clear(pf->lock_chains_being_freed, 0, ARRAY_SIZE(lock_chains));
6080#endif
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006081}
6082
6083static void free_zapped_rcu(struct rcu_head *ch)
6084{
6085 struct pending_free *pf;
6086 unsigned long flags;
6087
6088 if (WARN_ON_ONCE(ch != &delayed_free.rcu_head))
6089 return;
6090
6091 raw_local_irq_save(flags);
Peter Zijlstra248efb22020-03-13 11:09:49 +01006092 lockdep_lock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006093
6094 /* closed head */
6095 pf = delayed_free.pf + (delayed_free.index ^ 1);
6096 __free_zapped_classes(pf);
6097 delayed_free.scheduled = false;
6098
6099 /*
6100 * If there's anything on the open list, close and start a new callback.
6101 */
6102 call_rcu_zapped(delayed_free.pf + delayed_free.index);
6103
Peter Zijlstra248efb22020-03-13 11:09:49 +01006104 lockdep_unlock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006105 raw_local_irq_restore(flags);
6106}
6107
6108/*
6109 * Remove all lock classes from the class hash table and from the
6110 * all_lock_classes list whose key or name is in the address range [start,
6111 * start + size). Move these lock classes to the zapped_classes list. Must
6112 * be called with the graph lock held.
6113 */
6114static void __lockdep_free_key_range(struct pending_free *pf, void *start,
6115 unsigned long size)
Bart Van Assche956f3562019-02-14 15:00:43 -08006116{
6117 struct lock_class *class;
6118 struct hlist_head *head;
6119 int i;
6120
6121 /* Unhash all classes that were created by a module. */
6122 for (i = 0; i < CLASSHASH_SIZE; i++) {
6123 head = classhash_table + i;
6124 hlist_for_each_entry_rcu(class, head, hash_entry) {
6125 if (!within(class->key, start, size) &&
6126 !within(class->name, start, size))
6127 continue;
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006128 zap_class(pf, class);
Bart Van Assche956f3562019-02-14 15:00:43 -08006129 }
6130 }
6131}
6132
Peter Zijlstra35a93932015-02-26 16:23:11 +01006133/*
6134 * Used in module.c to remove lock classes from memory that is going to be
6135 * freed; and possibly re-used by other modules.
6136 *
Bart Van Assche29fc33f2019-02-14 15:00:45 -08006137 * We will have had one synchronize_rcu() before getting here, so we're
6138 * guaranteed nobody will look up these exact classes -- they're properly dead
6139 * but still allocated.
Peter Zijlstra35a93932015-02-26 16:23:11 +01006140 */
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006141static void lockdep_free_key_range_reg(void *start, unsigned long size)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006142{
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006143 struct pending_free *pf;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006144 unsigned long flags;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006145
Bart Van Asschefeb0a382019-02-14 15:00:42 -08006146 init_data_structures_once();
6147
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006148 raw_local_irq_save(flags);
Peter Zijlstra248efb22020-03-13 11:09:49 +01006149 lockdep_lock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006150 pf = get_pending_free();
6151 __lockdep_free_key_range(pf, start, size);
6152 call_rcu_zapped(pf);
Peter Zijlstra248efb22020-03-13 11:09:49 +01006153 lockdep_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006154 raw_local_irq_restore(flags);
Peter Zijlstra35a93932015-02-26 16:23:11 +01006155
6156 /*
6157 * Wait for any possible iterators from look_up_lock_class() to pass
6158 * before continuing to free the memory they refer to.
Peter Zijlstra35a93932015-02-26 16:23:11 +01006159 */
Paul E. McKenney51959d82018-11-06 19:06:51 -08006160 synchronize_rcu();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006161}
Peter Zijlstra35a93932015-02-26 16:23:11 +01006162
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006163/*
6164 * Free all lockdep keys in the range [start, start+size). Does not sleep.
6165 * Ignores debug_locks. Must only be used by the lockdep selftests.
6166 */
6167static void lockdep_free_key_range_imm(void *start, unsigned long size)
6168{
6169 struct pending_free *pf = delayed_free.pf;
6170 unsigned long flags;
6171
6172 init_data_structures_once();
6173
6174 raw_local_irq_save(flags);
Peter Zijlstra248efb22020-03-13 11:09:49 +01006175 lockdep_lock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006176 __lockdep_free_key_range(pf, start, size);
6177 __free_zapped_classes(pf);
Peter Zijlstra248efb22020-03-13 11:09:49 +01006178 lockdep_unlock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006179 raw_local_irq_restore(flags);
6180}
6181
6182void lockdep_free_key_range(void *start, unsigned long size)
6183{
6184 init_data_structures_once();
6185
6186 if (inside_selftest())
6187 lockdep_free_key_range_imm(start, size);
6188 else
6189 lockdep_free_key_range_reg(start, size);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006190}
6191
Bart Van Assche2904d9f2018-12-06 17:11:34 -08006192/*
6193 * Check whether any element of the @lock->class_cache[] array refers to a
6194 * registered lock class. The caller must hold either the graph lock or the
6195 * RCU read lock.
6196 */
6197static bool lock_class_cache_is_registered(struct lockdep_map *lock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006198{
Peter Zijlstra35a93932015-02-26 16:23:11 +01006199 struct lock_class *class;
Andrew Mortona63f38c2016-02-03 13:44:12 -08006200 struct hlist_head *head;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006201 int i, j;
Bart Van Assche2904d9f2018-12-06 17:11:34 -08006202
6203 for (i = 0; i < CLASSHASH_SIZE; i++) {
6204 head = classhash_table + i;
6205 hlist_for_each_entry_rcu(class, head, hash_entry) {
6206 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
6207 if (lock->class_cache[j] == class)
6208 return true;
6209 }
6210 }
6211 return false;
6212}
6213
Bart Van Assche956f3562019-02-14 15:00:43 -08006214/* The caller must hold the graph lock. Does not sleep. */
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006215static void __lockdep_reset_lock(struct pending_free *pf,
6216 struct lockdep_map *lock)
Bart Van Assche2904d9f2018-12-06 17:11:34 -08006217{
6218 struct lock_class *class;
Bart Van Assche956f3562019-02-14 15:00:43 -08006219 int j;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006220
6221 /*
Ingo Molnard6d897c2006-07-10 04:44:04 -07006222 * Remove all classes this lock might have:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006223 */
Ingo Molnard6d897c2006-07-10 04:44:04 -07006224 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
6225 /*
6226 * If the class exists we look it up and zap it:
6227 */
6228 class = look_up_lock_class(lock, j);
Matthew Wilcox64f29d12018-01-17 07:14:12 -08006229 if (class)
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006230 zap_class(pf, class);
Ingo Molnard6d897c2006-07-10 04:44:04 -07006231 }
6232 /*
6233 * Debug check: in the end all mapped classes should
6234 * be gone.
6235 */
Bart Van Assche956f3562019-02-14 15:00:43 -08006236 if (WARN_ON_ONCE(lock_class_cache_is_registered(lock)))
6237 debug_locks_off();
6238}
6239
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006240/*
6241 * Remove all information lockdep has about a lock if debug_locks == 1. Free
6242 * released data structures from RCU context.
6243 */
6244static void lockdep_reset_lock_reg(struct lockdep_map *lock)
Bart Van Assche956f3562019-02-14 15:00:43 -08006245{
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006246 struct pending_free *pf;
Bart Van Assche956f3562019-02-14 15:00:43 -08006247 unsigned long flags;
6248 int locked;
6249
Bart Van Assche956f3562019-02-14 15:00:43 -08006250 raw_local_irq_save(flags);
6251 locked = graph_lock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006252 if (!locked)
6253 goto out_irq;
6254
6255 pf = get_pending_free();
6256 __lockdep_reset_lock(pf, lock);
6257 call_rcu_zapped(pf);
6258
6259 graph_unlock();
6260out_irq:
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006261 raw_local_irq_restore(flags);
6262}
6263
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006264/*
6265 * Reset a lock. Does not sleep. Ignores debug_locks. Must only be used by the
6266 * lockdep selftests.
6267 */
6268static void lockdep_reset_lock_imm(struct lockdep_map *lock)
6269{
6270 struct pending_free *pf = delayed_free.pf;
6271 unsigned long flags;
6272
6273 raw_local_irq_save(flags);
Peter Zijlstra248efb22020-03-13 11:09:49 +01006274 lockdep_lock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006275 __lockdep_reset_lock(pf, lock);
6276 __free_zapped_classes(pf);
Peter Zijlstra248efb22020-03-13 11:09:49 +01006277 lockdep_unlock();
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006278 raw_local_irq_restore(flags);
6279}
6280
6281void lockdep_reset_lock(struct lockdep_map *lock)
6282{
6283 init_data_structures_once();
6284
6285 if (inside_selftest())
6286 lockdep_reset_lock_imm(lock);
6287 else
6288 lockdep_reset_lock_reg(lock);
6289}
6290
Bart Van Assche108c1482019-02-14 15:00:53 -08006291/* Unregister a dynamically allocated key. */
6292void lockdep_unregister_key(struct lock_class_key *key)
6293{
6294 struct hlist_head *hash_head = keyhashentry(key);
6295 struct lock_class_key *k;
6296 struct pending_free *pf;
6297 unsigned long flags;
6298 bool found = false;
6299
6300 might_sleep();
6301
6302 if (WARN_ON_ONCE(static_obj(key)))
6303 return;
6304
6305 raw_local_irq_save(flags);
Bart Van Assche8b39adb2019-04-15 10:05:38 -07006306 if (!graph_lock())
6307 goto out_irq;
6308
Bart Van Assche108c1482019-02-14 15:00:53 -08006309 pf = get_pending_free();
6310 hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
6311 if (k == key) {
6312 hlist_del_rcu(&k->hash_entry);
6313 found = true;
6314 break;
6315 }
6316 }
6317 WARN_ON_ONCE(!found);
6318 __lockdep_free_key_range(pf, key, 1);
6319 call_rcu_zapped(pf);
Bart Van Assche8b39adb2019-04-15 10:05:38 -07006320 graph_unlock();
6321out_irq:
Bart Van Assche108c1482019-02-14 15:00:53 -08006322 raw_local_irq_restore(flags);
6323
6324 /* Wait until is_dynamic_key() has finished accessing k->hash_entry. */
6325 synchronize_rcu();
6326}
6327EXPORT_SYMBOL_GPL(lockdep_unregister_key);
6328
Joel Fernandes (Google)c3bc8fd2018-07-30 15:24:23 -07006329void __init lockdep_init(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006330{
6331 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
6332
Li Zefanb0788ca2008-11-21 15:57:32 +08006333 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006334 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
6335 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
Li Zefanb0788ca2008-11-21 15:57:32 +08006336 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006337 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
6338 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
6339 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
6340
Bart Van Assche09d75ec2019-02-14 15:00:36 -08006341 printk(" memory used by lock dependency info: %zu kB\n",
Bart Van Assche7ff85172019-02-14 15:00:37 -08006342 (sizeof(lock_classes) +
Yuyang Du01bb6f02019-05-06 16:19:25 +08006343 sizeof(lock_classes_in_use) +
Bart Van Assche7ff85172019-02-14 15:00:37 -08006344 sizeof(classhash_table) +
6345 sizeof(list_entries) +
Bart Van Asscheace35a72019-02-14 15:00:47 -08006346 sizeof(list_entries_in_use) +
Bart Van Asschea0b0fd52019-02-14 15:00:46 -08006347 sizeof(chainhash_table) +
6348 sizeof(delayed_free)
Ming Lei4dd861d2009-07-16 15:44:29 +02006349#ifdef CONFIG_PROVE_LOCKING
Bart Van Assche7ff85172019-02-14 15:00:37 -08006350 + sizeof(lock_cq)
Bart Van Assche15ea86b2019-02-14 15:00:38 -08006351 + sizeof(lock_chains)
Bart Van Asschede4643a2019-02-14 15:00:50 -08006352 + sizeof(lock_chains_in_use)
Bart Van Assche15ea86b2019-02-14 15:00:38 -08006353 + sizeof(chain_hlocks)
Ming Lei4dd861d2009-07-16 15:44:29 +02006354#endif
Ming Lei906292092009-08-02 21:43:36 +08006355 ) / 1024
Ming Lei4dd861d2009-07-16 15:44:29 +02006356 );
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006357
Bart Van Assche12593b72019-07-22 11:24:42 -07006358#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
6359 printk(" memory used for stack traces: %zu kB\n",
6360 (sizeof(stack_trace) + sizeof(stack_trace_hash)) / 1024
6361 );
6362#endif
6363
Bart Van Assche09d75ec2019-02-14 15:00:36 -08006364 printk(" per task-struct memory footprint: %zu bytes\n",
Bart Van Assche7ff85172019-02-14 15:00:37 -08006365 sizeof(((struct task_struct *)NULL)->held_locks));
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006366}
6367
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006368static void
6369print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
Arjan van de Ven55794a42006-07-10 04:44:03 -07006370 const void *mem_to, struct held_lock *hlock)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006371{
6372 if (!debug_locks_off())
6373 return;
6374 if (debug_locks_silent)
6375 return;
6376
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006377 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006378 pr_warn("=========================\n");
6379 pr_warn("WARNING: held lock freed!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01006380 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006381 pr_warn("-------------------------\n");
Borislav Petkov04860d42018-02-26 14:49:26 +01006382 pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006383 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
Arjan van de Ven55794a42006-07-10 04:44:03 -07006384 print_lock(hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006385 lockdep_print_held_locks(curr);
6386
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006387 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006388 dump_stack();
6389}
6390
Oleg Nesterov54561782007-12-05 15:46:09 +01006391static inline int not_in_range(const void* mem_from, unsigned long mem_len,
6392 const void* lock_from, unsigned long lock_len)
6393{
6394 return lock_from + lock_len <= mem_from ||
6395 mem_from + mem_len <= lock_from;
6396}
6397
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006398/*
6399 * Called when kernel memory is freed (or unmapped), or if a lock
6400 * is destroyed or reinitialized - this code checks whether there is
6401 * any held lock in the memory range of <from> to <to>:
6402 */
6403void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
6404{
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006405 struct task_struct *curr = current;
6406 struct held_lock *hlock;
6407 unsigned long flags;
6408 int i;
6409
6410 if (unlikely(!debug_locks))
6411 return;
6412
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04006413 raw_local_irq_save(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006414 for (i = 0; i < curr->lockdep_depth; i++) {
6415 hlock = curr->held_locks + i;
6416
Oleg Nesterov54561782007-12-05 15:46:09 +01006417 if (not_in_range(mem_from, mem_len, hlock->instance,
6418 sizeof(*hlock->instance)))
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006419 continue;
6420
Oleg Nesterov54561782007-12-05 15:46:09 +01006421 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006422 break;
6423 }
Steven Rostedt (VMware)fcc784b2018-04-04 14:06:30 -04006424 raw_local_irq_restore(flags);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006425}
Peter Zijlstraed075362006-12-06 20:35:24 -08006426EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006427
Colin Cross1b1d2fb2013-05-06 23:50:08 +00006428static void print_held_locks_bug(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006429{
6430 if (!debug_locks_off())
6431 return;
6432 if (debug_locks_silent)
6433 return;
6434
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006435 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006436 pr_warn("====================================\n");
6437 pr_warn("WARNING: %s/%d still has locks held!\n",
Colin Cross1b1d2fb2013-05-06 23:50:08 +00006438 current->comm, task_pid_nr(current));
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01006439 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006440 pr_warn("------------------------------------\n");
Colin Cross1b1d2fb2013-05-06 23:50:08 +00006441 lockdep_print_held_locks(current);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006442 pr_warn("\nstack backtrace:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006443 dump_stack();
6444}
6445
Colin Cross1b1d2fb2013-05-06 23:50:08 +00006446void debug_check_no_locks_held(void)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006447{
Colin Cross1b1d2fb2013-05-06 23:50:08 +00006448 if (unlikely(current->lockdep_depth > 0))
6449 print_held_locks_bug();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006450}
Colin Cross1b1d2fb2013-05-06 23:50:08 +00006451EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006452
Sasha Levin8dce7a92013-06-13 18:41:16 -04006453#ifdef __KERNEL__
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006454void debug_show_all_locks(void)
6455{
6456 struct task_struct *g, *p;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006457
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08006458 if (unlikely(!debug_locks)) {
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006459 pr_warn("INFO: lockdep is turned off.\n");
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08006460 return;
6461 }
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006462 pr_warn("\nShowing all locks held in the system:\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006463
Tetsuo Handa0f736a52018-04-06 19:41:18 +09006464 rcu_read_lock();
6465 for_each_process_thread(g, p) {
Tetsuo Handa0f736a52018-04-06 19:41:18 +09006466 if (!p->lockdep_depth)
6467 continue;
6468 lockdep_print_held_locks(p);
Tejun Heo88f1c872018-01-22 14:00:55 -08006469 touch_nmi_watchdog();
Tetsuo Handa0f736a52018-04-06 19:41:18 +09006470 touch_all_softlockup_watchdogs();
6471 }
6472 rcu_read_unlock();
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006473
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006474 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006475 pr_warn("=============================================\n\n");
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006476}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006477EXPORT_SYMBOL_GPL(debug_show_all_locks);
Sasha Levin8dce7a92013-06-13 18:41:16 -04006478#endif
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006479
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01006480/*
6481 * Careful: only use this function if you are sure that
6482 * the task cannot run in parallel!
6483 */
John Kacurf1b499f2010-08-05 17:10:53 +02006484void debug_show_held_locks(struct task_struct *task)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006485{
Jarek Poplawski9c35dd72007-03-22 00:11:28 -08006486 if (unlikely(!debug_locks)) {
6487 printk("INFO: lockdep is turned off.\n");
6488 return;
6489 }
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006490 lockdep_print_held_locks(task);
6491}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07006492EXPORT_SYMBOL_GPL(debug_show_held_locks);
Peter Zijlstrab351d162007-10-11 22:11:12 +02006493
Andi Kleen722a9f92014-05-02 00:44:38 +02006494asmlinkage __visible void lockdep_sys_exit(void)
Peter Zijlstrab351d162007-10-11 22:11:12 +02006495{
6496 struct task_struct *curr = current;
6497
6498 if (unlikely(curr->lockdep_depth)) {
6499 if (!debug_locks_off())
6500 return;
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006501 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006502 pr_warn("================================================\n");
6503 pr_warn("WARNING: lock held when returning to user space!\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01006504 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006505 pr_warn("------------------------------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006506 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
Peter Zijlstrab351d162007-10-11 22:11:12 +02006507 curr->comm, curr->pid);
6508 lockdep_print_held_locks(curr);
6509 }
Byungchul Parkb09be672017-08-07 16:12:52 +09006510
6511 /*
6512 * The lock history for each syscall should be independent. So wipe the
6513 * slate clean on return to userspace.
6514 */
Peter Zijlstraf52be572017-08-29 10:59:39 +02006515 lockdep_invariant_state(false);
Peter Zijlstrab351d162007-10-11 22:11:12 +02006516}
Paul E. McKenney0632eb32010-02-22 17:04:47 -08006517
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07006518void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
Paul E. McKenney0632eb32010-02-22 17:04:47 -08006519{
6520 struct task_struct *curr = current;
Paul E. McKenney1feb2cc2021-04-05 09:47:59 -07006521 int dl = READ_ONCE(debug_locks);
Paul E. McKenney0632eb32010-02-22 17:04:47 -08006522
Lai Jiangshan2b3fc352010-04-20 16:23:07 +08006523 /* Note: the following can be executed concurrently, so be careful. */
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006524 pr_warn("\n");
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006525 pr_warn("=============================\n");
6526 pr_warn("WARNING: suspicious RCU usage\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +01006527 print_kernel_ident();
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -08006528 pr_warn("-----------------------------\n");
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006529 pr_warn("%s:%d %s!\n", file, line, s);
6530 pr_warn("\nother info that might help us debug this:\n\n");
Paul E. McKenney1feb2cc2021-04-05 09:47:59 -07006531 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n%s",
Paul E. McKenneyc5fdcec2012-01-30 08:46:32 -08006532 !rcu_lockdep_current_cpu_online()
6533 ? "RCU used illegally from offline CPU!\n"
Paul E. McKenneyd29e0b22020-05-28 08:49:29 -07006534 : "",
Paul E. McKenney1feb2cc2021-04-05 09:47:59 -07006535 rcu_scheduler_active, dl,
6536 dl ? "" : "Possible false positive due to lockdep disabling via debug_locks = 0\n");
Frederic Weisbecker0464e932011-10-07 18:22:01 +02006537
6538 /*
6539 * If a CPU is in the RCU-free window in idle (ie: in the section
6540 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
6541 * considers that CPU to be in an "extended quiescent state",
6542 * which means that RCU will be completely ignoring that CPU.
6543 * Therefore, rcu_read_lock() and friends have absolutely no
6544 * effect on a CPU running in that state. In other words, even if
6545 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
6546 * delete data structures out from under it. RCU really has no
6547 * choice here: we need to keep an RCU-free window in idle where
6548 * the CPU may possibly enter into low power mode. This way we can
6549 * notice an extended quiescent state to other CPUs that started a grace
6550 * period. Otherwise we would delay any grace period as long as we run
6551 * in the idle task.
6552 *
6553 * So complain bitterly if someone does call rcu_read_lock(),
6554 * rcu_read_lock_bh() and so on from extended quiescent states.
6555 */
Paul E. McKenney5c173eb2013-09-13 17:20:11 -07006556 if (!rcu_is_watching())
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006557 pr_warn("RCU used illegally from extended quiescent state!\n");
Frederic Weisbecker0464e932011-10-07 18:22:01 +02006558
Paul E. McKenney0632eb32010-02-22 17:04:47 -08006559 lockdep_print_held_locks(curr);
Paul E. McKenney681fbec2017-05-04 15:44:38 -07006560 pr_warn("\nstack backtrace:\n");
Paul E. McKenney0632eb32010-02-22 17:04:47 -08006561 dump_stack();
6562}
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07006563EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);