Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2 | /* |
| 3 | * kernel/lockdep.c |
| 4 | * |
| 5 | * Runtime locking correctness validator |
| 6 | * |
| 7 | * Started by Ingo Molnar: |
| 8 | * |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 9 | * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
Peter Zijlstra | 90eec10 | 2015-11-16 11:08:45 +0100 | [diff] [blame] | 10 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 11 | * |
| 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 Rostedt | a5e2588 | 2008-12-02 15:34:05 -0500 | [diff] [blame] | 29 | #define DISABLE_BRANCH_PROFILING |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 30 | #include <linux/mutex.h> |
| 31 | #include <linux/sched.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 32 | #include <linux/sched/clock.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 33 | #include <linux/sched/task.h> |
Nikolay Borisov | 6d7225f | 2017-05-03 14:53:05 -0700 | [diff] [blame] | 34 | #include <linux/sched/mm.h> |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 35 | #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 Jones | 99de055 | 2006-09-29 02:00:10 -0700 | [diff] [blame] | 45 | #include <linux/utsname.h> |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 46 | #include <linux/hash.h> |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 47 | #include <linux/ftrace.h> |
Peter Zijlstra | b4b136f | 2009-01-29 14:50:36 +0100 | [diff] [blame] | 48 | #include <linux/stringify.h> |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 49 | #include <linux/bitmap.h> |
Ming Lei | d588e46 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 50 | #include <linux/bitops.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 51 | #include <linux/gfp.h> |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 52 | #include <linux/random.h> |
Peter Zijlstra | dfaaf3f | 2016-05-30 18:31:33 +0200 | [diff] [blame] | 53 | #include <linux/jhash.h> |
Tejun Heo | 88f1c87 | 2018-01-22 14:00:55 -0800 | [diff] [blame] | 54 | #include <linux/nmi.h> |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 55 | #include <linux/rcupdate.h> |
Masami Hiramatsu | 2f43c60 | 2019-02-13 01:15:05 +0900 | [diff] [blame] | 56 | #include <linux/kprobes.h> |
Shuah Khan | f8cfa46 | 2021-02-26 17:06:59 -0700 | [diff] [blame] | 57 | #include <linux/lockdep.h> |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 58 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 59 | #include <asm/sections.h> |
| 60 | |
| 61 | #include "lockdep_internals.h" |
| 62 | |
Steven Rostedt | a8d154b | 2009-04-10 09:36:00 -0400 | [diff] [blame] | 63 | #define CREATE_TRACE_POINTS |
Frederic Weisbecker | 6717876 | 2009-11-13 10:06:34 +0100 | [diff] [blame] | 64 | #include <trace/events/lock.h> |
Steven Rostedt | a8d154b | 2009-04-10 09:36:00 -0400 | [diff] [blame] | 65 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 66 | #ifdef CONFIG_PROVE_LOCKING |
| 67 | int prove_locking = 1; |
| 68 | module_param(prove_locking, int, 0644); |
| 69 | #else |
| 70 | #define prove_locking 0 |
| 71 | #endif |
| 72 | |
| 73 | #ifdef CONFIG_LOCK_STAT |
| 74 | int lock_stat = 1; |
| 75 | module_param(lock_stat, int, 0644); |
| 76 | #else |
| 77 | #define lock_stat 0 |
| 78 | #endif |
| 79 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 80 | DEFINE_PER_CPU(unsigned int, lockdep_recursion); |
| 81 | EXPORT_PER_CPU_SYMBOL_GPL(lockdep_recursion); |
| 82 | |
Peter Zijlstra | 0afda3a | 2021-01-06 15:36:22 +0100 | [diff] [blame] | 83 | static __always_inline bool lockdep_enabled(void) |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 84 | { |
| 85 | if (!debug_locks) |
| 86 | return false; |
| 87 | |
Peter Zijlstra | d48e385 | 2020-10-26 16:22:56 +0100 | [diff] [blame] | 88 | if (this_cpu_read(lockdep_recursion)) |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 89 | return false; |
| 90 | |
| 91 | if (current->lockdep_recursion) |
| 92 | return false; |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 97 | /* |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 98 | * lockdep_lock: protects the lockdep graph, the hashes and the |
| 99 | * class/list/hash allocators. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 100 | * |
| 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 Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 103 | * code to recurse back into the lockdep code... |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 104 | */ |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 105 | static arch_spinlock_t __lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
| 106 | static struct task_struct *__owner; |
| 107 | |
| 108 | static inline void lockdep_lock(void) |
| 109 | { |
| 110 | DEBUG_LOCKS_WARN_ON(!irqs_disabled()); |
| 111 | |
Boqun Feng | 43be438 | 2020-11-13 19:05:03 +0800 | [diff] [blame] | 112 | __this_cpu_inc(lockdep_recursion); |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 113 | arch_spin_lock(&__lock); |
| 114 | __owner = current; |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static inline void lockdep_unlock(void) |
| 118 | { |
Boqun Feng | 43be438 | 2020-11-13 19:05:03 +0800 | [diff] [blame] | 119 | DEBUG_LOCKS_WARN_ON(!irqs_disabled()); |
| 120 | |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 121 | if (debug_locks && DEBUG_LOCKS_WARN_ON(__owner != current)) |
| 122 | return; |
| 123 | |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 124 | __owner = NULL; |
| 125 | arch_spin_unlock(&__lock); |
Boqun Feng | 43be438 | 2020-11-13 19:05:03 +0800 | [diff] [blame] | 126 | __this_cpu_dec(lockdep_recursion); |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static inline bool lockdep_assert_locked(void) |
| 130 | { |
| 131 | return DEBUG_LOCKS_WARN_ON(__owner != current); |
| 132 | } |
| 133 | |
Bart Van Assche | cdc84d7 | 2019-02-14 15:00:44 -0800 | [diff] [blame] | 134 | static struct task_struct *lockdep_selftest_task_struct; |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 135 | |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 136 | |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 137 | static int graph_lock(void) |
| 138 | { |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 139 | lockdep_lock(); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 140 | /* |
| 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 Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 147 | lockdep_unlock(); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | return 1; |
| 151 | } |
| 152 | |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 153 | static inline void graph_unlock(void) |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 154 | { |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 155 | lockdep_unlock(); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Turn lock debugging off and return with 0 if it was off already, |
| 160 | * and also release the graph lock: |
| 161 | */ |
| 162 | static inline int debug_locks_off_graph_unlock(void) |
| 163 | { |
| 164 | int ret = debug_locks_off(); |
| 165 | |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 166 | lockdep_unlock(); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 167 | |
| 168 | return ret; |
| 169 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 170 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 171 | unsigned long nr_list_entries; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 172 | static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES]; |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 173 | static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 174 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 175 | /* |
| 176 | * All data structures here are protected by the global debug_lock. |
| 177 | * |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 178 | * nr_lock_classes is the number of elements of lock_classes[] that is |
| 179 | * in use. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 180 | */ |
Bart Van Assche | 108c148 | 2019-02-14 15:00:53 -0800 | [diff] [blame] | 181 | #define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1) |
| 182 | #define KEYHASH_SIZE (1UL << KEYHASH_BITS) |
| 183 | static struct hlist_head lock_keys_hash[KEYHASH_SIZE]; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 184 | unsigned long nr_lock_classes; |
Waiman Long | 1d44bcb | 2020-02-06 10:24:05 -0500 | [diff] [blame] | 185 | unsigned long nr_zapped_classes; |
Bart Van Assche | 1431a5d | 2018-12-06 17:11:32 -0800 | [diff] [blame] | 186 | #ifndef CONFIG_DEBUG_LOCKDEP |
| 187 | static |
| 188 | #endif |
Waiman Long | 8ca2b56c | 2018-10-03 13:07:18 -0400 | [diff] [blame] | 189 | struct lock_class lock_classes[MAX_LOCKDEP_KEYS]; |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 190 | static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 191 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 192 | static inline struct lock_class *hlock_class(struct held_lock *hlock) |
| 193 | { |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 194 | 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 Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 200 | /* |
| 201 | * Someone passed in garbage, we give up. |
| 202 | */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 203 | DEBUG_LOCKS_WARN_ON(1); |
| 204 | return NULL; |
| 205 | } |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 206 | |
| 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 Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 214 | #ifdef CONFIG_LOCK_STAT |
Peter Zijlstra | 2552821 | 2016-03-15 14:52:49 -0700 | [diff] [blame] | 215 | static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 216 | |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 217 | static inline u64 lockstat_clock(void) |
| 218 | { |
Peter Zijlstra | c676329 | 2010-05-25 10:48:51 +0200 | [diff] [blame] | 219 | return local_clock(); |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 222 | static int lock_point(unsigned long points[], unsigned long ip) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 223 | { |
| 224 | int i; |
| 225 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 226 | for (i = 0; i < LOCKSTAT_POINTS; i++) { |
| 227 | if (points[i] == 0) { |
| 228 | points[i] = ip; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 229 | break; |
| 230 | } |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 231 | if (points[i] == ip) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 232 | break; |
| 233 | } |
| 234 | |
| 235 | return i; |
| 236 | } |
| 237 | |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 238 | static void lock_time_inc(struct lock_time *lt, u64 time) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 239 | { |
| 240 | if (time > lt->max) |
| 241 | lt->max = time; |
| 242 | |
Frank Rowand | 109d71c | 2009-11-19 13:42:06 -0800 | [diff] [blame] | 243 | if (time < lt->min || !lt->nr) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 244 | lt->min = time; |
| 245 | |
| 246 | lt->total += time; |
| 247 | lt->nr++; |
| 248 | } |
| 249 | |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 250 | static inline void lock_time_add(struct lock_time *src, struct lock_time *dst) |
| 251 | { |
Frank Rowand | 109d71c | 2009-11-19 13:42:06 -0800 | [diff] [blame] | 252 | 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 Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 261 | dst->total += src->total; |
| 262 | dst->nr += src->nr; |
| 263 | } |
| 264 | |
| 265 | struct 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 Heo | 1871e52 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 273 | &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 274 | |
| 275 | for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++) |
| 276 | stats.contention_point[i] += pcs->contention_point[i]; |
| 277 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 278 | for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++) |
| 279 | stats.contending_point[i] += pcs->contending_point[i]; |
| 280 | |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 281 | 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 Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 286 | |
| 287 | for (i = 0; i < ARRAY_SIZE(stats.bounces); i++) |
| 288 | stats.bounces[i] += pcs->bounces[i]; |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | return stats; |
| 292 | } |
| 293 | |
| 294 | void 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 Heo | 1871e52 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 300 | &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 301 | |
| 302 | memset(cpu_stats, 0, sizeof(struct lock_class_stats)); |
| 303 | } |
| 304 | memset(class->contention_point, 0, sizeof(class->contention_point)); |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 305 | memset(class->contending_point, 0, sizeof(class->contending_point)); |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 308 | static struct lock_class_stats *get_lock_stats(struct lock_class *class) |
| 309 | { |
Joel Fernandes (Google) | 01f3849 | 2018-07-30 15:24:21 -0700 | [diff] [blame] | 310 | return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes]; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static void lock_release_holdtime(struct held_lock *hlock) |
| 314 | { |
| 315 | struct lock_class_stats *stats; |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 316 | u64 holdtime; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 317 | |
| 318 | if (!lock_stat) |
| 319 | return; |
| 320 | |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 321 | holdtime = lockstat_clock() - hlock->holdtime_stamp; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 322 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 323 | stats = get_lock_stats(hlock_class(hlock)); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 324 | if (hlock->read) |
| 325 | lock_time_inc(&stats->read_holdtime, holdtime); |
| 326 | else |
| 327 | lock_time_inc(&stats->write_holdtime, holdtime); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 328 | } |
| 329 | #else |
| 330 | static inline void lock_release_holdtime(struct held_lock *hlock) |
| 331 | { |
| 332 | } |
| 333 | #endif |
| 334 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 335 | /* |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 336 | * 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 340 | */ |
| 341 | LIST_HEAD(all_lock_classes); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 342 | static 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 Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame] | 347 | * @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements |
| 348 | * are about to be freed. |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 349 | */ |
| 350 | struct pending_free { |
| 351 | struct list_head zapped; |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame] | 352 | DECLARE_BITMAP(lock_chains_being_freed, MAX_LOCKDEP_CHAINS); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 353 | }; |
| 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 | */ |
| 366 | static 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 372 | |
| 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 Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 378 | #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 379 | #define classhashentry(key) (classhash_table + __classhashfn((key))) |
| 380 | |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 381 | static struct hlist_head classhash_table[CLASSHASH_SIZE]; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 382 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 383 | /* |
| 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 Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 389 | #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 390 | #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain))) |
| 391 | |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 392 | static struct hlist_head chainhash_table[CHAINHASH_SIZE]; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 393 | |
| 394 | /* |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 395 | * the id of held_lock |
| 396 | */ |
| 397 | static 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 | |
| 404 | static inline unsigned int chain_hlock_class_idx(u16 hlock_id) |
| 405 | { |
| 406 | return hlock_id & (MAX_LOCKDEP_KEYS - 1); |
| 407 | } |
| 408 | |
| 409 | /* |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 410 | * 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 Zijlstra | dfaaf3f | 2016-05-30 18:31:33 +0200 | [diff] [blame] | 415 | static 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 423 | |
Yuyang Du | e196e47 | 2019-05-06 16:19:23 +0800 | [diff] [blame] | 424 | void lockdep_init_task(struct task_struct *task) |
| 425 | { |
| 426 | task->lockdep_depth = 0; /* no locks held yet */ |
Yuyang Du | f6ec882 | 2019-05-06 16:19:24 +0800 | [diff] [blame] | 427 | task->curr_chain_key = INITIAL_CHAIN_KEY; |
Yuyang Du | e196e47 | 2019-05-06 16:19:23 +0800 | [diff] [blame] | 428 | task->lockdep_recursion = 0; |
| 429 | } |
| 430 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 431 | static __always_inline void lockdep_recursion_inc(void) |
| 432 | { |
| 433 | __this_cpu_inc(lockdep_recursion); |
| 434 | } |
| 435 | |
Peter Zijlstra | 6eebad1 | 2020-06-03 13:40:21 +0200 | [diff] [blame] | 436 | static __always_inline void lockdep_recursion_finish(void) |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 437 | { |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 438 | if (WARN_ON_ONCE(__this_cpu_dec_return(lockdep_recursion))) |
| 439 | __this_cpu_write(lockdep_recursion, 0); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Bart Van Assche | cdc84d7 | 2019-02-14 15:00:44 -0800 | [diff] [blame] | 442 | void lockdep_set_selftest_task(struct task_struct *task) |
| 443 | { |
| 444 | lockdep_selftest_task_struct = task; |
| 445 | } |
| 446 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 447 | /* |
| 448 | * Debugging switches: |
| 449 | */ |
| 450 | |
| 451 | #define VERBOSE 0 |
Ingo Molnar | 33e94e9 | 2006-12-13 00:34:41 -0800 | [diff] [blame] | 452 | #define VERY_VERBOSE 0 |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 453 | |
| 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 Zijlstra | d92a8cf | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 462 | #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 463 | /* |
| 464 | * Quick filtering for interesting events: |
| 465 | */ |
| 466 | static int class_filter(struct lock_class *class) |
| 467 | { |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 468 | #if 0 |
| 469 | /* Example */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 470 | if (class->name_version == 1 && |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 471 | !strcmp(class->name, "lockname")) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 472 | return 1; |
| 473 | if (class->name_version == 1 && |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 474 | !strcmp(class->name, "&struct->lockfield")) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 475 | return 1; |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 476 | #endif |
Ingo Molnar | a664089 | 2006-12-13 00:34:39 -0800 | [diff] [blame] | 477 | /* Filter everything else. 1 would be to allow everything else */ |
| 478 | return 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 479 | } |
| 480 | #endif |
| 481 | |
| 482 | static int verbose(struct lock_class *class) |
| 483 | { |
| 484 | #if VERBOSE |
| 485 | return class_filter(class); |
| 486 | #endif |
| 487 | return 0; |
| 488 | } |
| 489 | |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 490 | static 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 Gruenbacher | acf5937 | 2014-07-15 21:10:52 +0200 | [diff] [blame] | 494 | #ifdef CONFIG_LOCK_STAT |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 495 | printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n"); |
Andreas Gruenbacher | acf5937 | 2014-07-15 21:10:52 +0200 | [diff] [blame] | 496 | #endif |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 497 | } |
| 498 | |
Arnd Bergmann | 886532a | 2019-06-17 14:47:05 +0200 | [diff] [blame] | 499 | unsigned long nr_stack_trace_entries; |
| 500 | |
Arnd Bergmann | 30a35f7 | 2019-06-28 12:29:03 +0200 | [diff] [blame] | 501 | #ifdef CONFIG_PROVE_LOCKING |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 502 | /** |
| 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 | */ |
| 509 | struct lock_trace { |
| 510 | struct hlist_node hash_entry; |
| 511 | u32 hash; |
| 512 | u32 nr_entries; |
Gustavo A. R. Silva | db78538 | 2020-05-07 13:58:04 -0500 | [diff] [blame] | 513 | unsigned long entries[] __aligned(sizeof(unsigned long)); |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 514 | }; |
| 515 | #define LOCK_TRACE_SIZE_IN_LONGS \ |
| 516 | (sizeof(struct lock_trace) / sizeof(unsigned long)) |
Arnd Bergmann | 886532a | 2019-06-17 14:47:05 +0200 | [diff] [blame] | 517 | /* |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 518 | * Stack-trace: sequence of lock_trace structures. Protected by the graph_lock. |
Arnd Bergmann | 886532a | 2019-06-17 14:47:05 +0200 | [diff] [blame] | 519 | */ |
| 520 | static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES]; |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 521 | static struct hlist_head stack_trace_hash[STACK_TRACE_HASH_SIZE]; |
Arnd Bergmann | 886532a | 2019-06-17 14:47:05 +0200 | [diff] [blame] | 522 | |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 523 | static bool traces_identical(struct lock_trace *t1, struct lock_trace *t2) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 524 | { |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 525 | 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 | |
| 530 | static struct lock_trace *save_trace(void) |
| 531 | { |
| 532 | struct lock_trace *trace, *t2; |
| 533 | struct hlist_head *hash_head; |
| 534 | u32 hash; |
Waiman Long | d91f305 | 2019-12-20 08:51:28 -0500 | [diff] [blame] | 535 | int max_entries; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 536 | |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 537 | 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 539 | |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 540 | 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 Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 543 | |
Waiman Long | d91f305 | 2019-12-20 08:51:28 -0500 | [diff] [blame] | 544 | if (max_entries <= 0) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 545 | if (!debug_locks_off_graph_unlock()) |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 546 | return NULL; |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 547 | |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 548 | print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!"); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 549 | dump_stack(); |
| 550 | |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 551 | return NULL; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 552 | } |
Waiman Long | d91f305 | 2019-12-20 08:51:28 -0500 | [diff] [blame] | 553 | trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 554 | |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 555 | 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 567 | } |
Bart Van Assche | 8c77922 | 2019-07-22 11:24:43 -0700 | [diff] [blame] | 568 | |
| 569 | /* Return the number of stack traces in the stack_trace[] array. */ |
| 570 | u64 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. */ |
| 586 | u64 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 596 | } |
Arnd Bergmann | 886532a | 2019-06-17 14:47:05 +0200 | [diff] [blame] | 597 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 598 | |
| 599 | unsigned int nr_hardirq_chains; |
| 600 | unsigned int nr_softirq_chains; |
| 601 | unsigned int nr_process_chains; |
| 602 | unsigned int max_lockdep_depth; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 603 | |
| 604 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 605 | /* |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 606 | * Various lockdep statistics: |
| 607 | */ |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 608 | DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 609 | #endif |
| 610 | |
Arnd Bergmann | 30a35f7 | 2019-06-28 12:29:03 +0200 | [diff] [blame] | 611 | #ifdef CONFIG_PROVE_LOCKING |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 612 | /* |
| 613 | * Locking printouts: |
| 614 | */ |
| 615 | |
Peter Zijlstra | fabe9c4 | 2009-01-22 14:51:01 +0100 | [diff] [blame] | 616 | #define __USAGE(__STATE) \ |
Peter Zijlstra | b4b136f | 2009-01-29 14:50:36 +0100 | [diff] [blame] | 617 | [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 Zijlstra | fabe9c4 | 2009-01-22 14:51:01 +0100 | [diff] [blame] | 621 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 622 | static const char *usage_str[] = |
| 623 | { |
Peter Zijlstra | fabe9c4 | 2009-01-22 14:51:01 +0100 | [diff] [blame] | 624 | #define LOCKDEP_STATE(__STATE) __USAGE(__STATE) |
| 625 | #include "lockdep_states.h" |
| 626 | #undef LOCKDEP_STATE |
| 627 | [LOCK_USED] = "INITIAL USE", |
Peter Zijlstra | 2bb8945 | 2020-09-30 11:49:37 +0200 | [diff] [blame] | 628 | [LOCK_USED_READ] = "INITIAL READ USE", |
| 629 | /* abused as string storage for verify_lock_unused() */ |
Peter Zijlstra | f6f48e1 | 2020-02-20 09:45:02 +0100 | [diff] [blame] | 630 | [LOCK_USAGE_STATES] = "IN-NMI", |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 631 | }; |
Arnd Bergmann | 886532a | 2019-06-17 14:47:05 +0200 | [diff] [blame] | 632 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 633 | |
Bart Van Assche | 364f6af | 2019-07-22 11:24:40 -0700 | [diff] [blame] | 634 | const char *__get_key_name(const struct lockdep_subclass_key *key, char *str) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 635 | { |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 636 | return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Peter Zijlstra | 3ff176c | 2009-01-22 17:40:42 +0100 | [diff] [blame] | 639 | static inline unsigned long lock_flag(enum lock_usage_bit bit) |
| 640 | { |
| 641 | return 1UL << bit; |
| 642 | } |
| 643 | |
| 644 | static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit) |
| 645 | { |
Yuyang Du | c52478f | 2019-05-06 16:19:19 +0800 | [diff] [blame] | 646 | /* |
| 647 | * The usage character defaults to '.' (i.e., irqs disabled and not in |
| 648 | * irq context), which is the safest usage category. |
| 649 | */ |
Peter Zijlstra | 3ff176c | 2009-01-22 17:40:42 +0100 | [diff] [blame] | 650 | char c = '.'; |
| 651 | |
Yuyang Du | c52478f | 2019-05-06 16:19:19 +0800 | [diff] [blame] | 652 | /* |
| 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 Zijlstra | 3ff176c | 2009-01-22 17:40:42 +0100 | [diff] [blame] | 661 | c = '+'; |
Yuyang Du | c52478f | 2019-05-06 16:19:19 +0800 | [diff] [blame] | 662 | if (class->usage_mask & lock_flag(bit)) |
Peter Zijlstra | 3ff176c | 2009-01-22 17:40:42 +0100 | [diff] [blame] | 663 | c = '?'; |
Yuyang Du | c52478f | 2019-05-06 16:19:19 +0800 | [diff] [blame] | 664 | } else if (class->usage_mask & lock_flag(bit)) |
| 665 | c = '-'; |
Peter Zijlstra | 3ff176c | 2009-01-22 17:40:42 +0100 | [diff] [blame] | 666 | |
| 667 | return c; |
| 668 | } |
| 669 | |
Peter Zijlstra | f510b23 | 2009-01-22 17:53:47 +0100 | [diff] [blame] | 670 | void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS]) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 671 | { |
Peter Zijlstra | f510b23 | 2009-01-22 17:53:47 +0100 | [diff] [blame] | 672 | int i = 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 673 | |
Peter Zijlstra | f510b23 | 2009-01-22 17:53:47 +0100 | [diff] [blame] | 674 | #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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Steven Rostedt | e5e78d0 | 2011-11-02 20:24:16 -0400 | [diff] [blame] | 683 | static void __print_lock_name(struct lock_class *class) |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 684 | { |
| 685 | char str[KSYM_NAME_LEN]; |
| 686 | const char *name; |
| 687 | |
| 688 | name = class->name; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 689 | if (!name) { |
| 690 | name = __get_key_name(class->key, str); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 691 | printk(KERN_CONT "%s", name); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 692 | } else { |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 693 | printk(KERN_CONT "%s", name); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 694 | if (class->name_version > 1) |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 695 | printk(KERN_CONT "#%d", class->name_version); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 696 | if (class->subclass) |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 697 | printk(KERN_CONT "/%d", class->subclass); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 698 | } |
Steven Rostedt | e5e78d0 | 2011-11-02 20:24:16 -0400 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static void print_lock_name(struct lock_class *class) |
| 702 | { |
| 703 | char usage[LOCK_USAGE_CHARS]; |
| 704 | |
| 705 | get_usage_chars(class, usage); |
| 706 | |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 707 | printk(KERN_CONT " ("); |
Steven Rostedt | e5e78d0 | 2011-11-02 20:24:16 -0400 | [diff] [blame] | 708 | __print_lock_name(class); |
Arnd Bergmann | 6d48b79 | 2021-03-22 12:55:25 +0100 | [diff] [blame] | 709 | printk(KERN_CONT "){%s}-{%d:%d}", usage, |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 710 | class->wait_type_outer ?: class->wait_type_inner, |
| 711 | class->wait_type_inner); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static void print_lockdep_cache(struct lockdep_map *lock) |
| 715 | { |
| 716 | const char *name; |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 717 | char str[KSYM_NAME_LEN]; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 718 | |
| 719 | name = lock->name; |
| 720 | if (!name) |
| 721 | name = __get_key_name(lock->key->subkeys, str); |
| 722 | |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 723 | printk(KERN_CONT "%s", name); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | static void print_lock(struct held_lock *hlock) |
| 727 | { |
Peter Zijlstra | d7bc319 | 2015-04-15 17:11:57 +0200 | [diff] [blame] | 728 | /* |
| 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 Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 731 | * |
| 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 Zijlstra | d7bc319 | 2015-04-15 17:11:57 +0200 | [diff] [blame] | 737 | */ |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 738 | struct lock_class *lock = hlock_class(hlock); |
Peter Zijlstra | d7bc319 | 2015-04-15 17:11:57 +0200 | [diff] [blame] | 739 | |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 740 | if (!lock) { |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 741 | printk(KERN_CONT "<RELEASED>\n"); |
Peter Zijlstra | d7bc319 | 2015-04-15 17:11:57 +0200 | [diff] [blame] | 742 | return; |
| 743 | } |
| 744 | |
Paul E. McKenney | 519248f | 2019-05-30 05:39:25 -0700 | [diff] [blame] | 745 | printk(KERN_CONT "%px", hlock->instance); |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 746 | print_lock_name(lock); |
Tetsuo Handa | b3c3975 | 2018-03-27 19:41:41 +0900 | [diff] [blame] | 747 | printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Tetsuo Handa | 8cc05c71 | 2018-04-06 19:41:19 +0900 | [diff] [blame] | 750 | static void lockdep_print_held_locks(struct task_struct *p) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 751 | { |
Tetsuo Handa | 8cc05c71 | 2018-04-06 19:41:19 +0900 | [diff] [blame] | 752 | int i, depth = READ_ONCE(p->lockdep_depth); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 753 | |
Tetsuo Handa | 8cc05c71 | 2018-04-06 19:41:19 +0900 | [diff] [blame] | 754 | 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 Zijlstra | b03fbd4 | 2021-06-11 10:28:12 +0200 | [diff] [blame] | 763 | if (p != current && task_is_running(p)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 764 | return; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 765 | for (i = 0; i < depth; i++) { |
| 766 | printk(" #%d: ", i); |
Tetsuo Handa | 8cc05c71 | 2018-04-06 19:41:19 +0900 | [diff] [blame] | 767 | print_lock(p->held_locks + i); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 768 | } |
| 769 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 770 | |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 771 | static void print_kernel_ident(void) |
Dave Jones | 99de055 | 2006-09-29 02:00:10 -0700 | [diff] [blame] | 772 | { |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 773 | printk("%s %.*s %s\n", init_utsname()->release, |
Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 774 | (int)strcspn(init_utsname()->version, " "), |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 775 | init_utsname()->version, |
| 776 | print_tainted()); |
Dave Jones | 99de055 | 2006-09-29 02:00:10 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 779 | static int very_verbose(struct lock_class *class) |
| 780 | { |
| 781 | #if VERY_VERBOSE |
| 782 | return class_filter(class); |
| 783 | #endif |
| 784 | return 0; |
| 785 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 786 | |
| 787 | /* |
| 788 | * Is this the address of a static object: |
| 789 | */ |
Sasha Levin | 8dce7a9 | 2013-06-13 18:41:16 -0400 | [diff] [blame] | 790 | #ifdef __KERNEL__ |
Christophe Leroy | e5ae372 | 2021-11-05 13:40:43 -0700 | [diff] [blame] | 791 | /* |
| 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 |
| 797 | static 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 Assche | 108c148 | 2019-02-14 15:00:53 -0800 | [diff] [blame] | 806 | static int static_obj(const void *obj) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 807 | { |
| 808 | unsigned long start = (unsigned long) &_stext, |
| 809 | end = (unsigned long) &_end, |
| 810 | addr = (unsigned long) obj; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 811 | |
Gerald Schaefer | 7a5da02 | 2019-04-18 16:24:50 +0200 | [diff] [blame] | 812 | if (arch_is_kernel_initmem_freed(addr)) |
| 813 | return 0; |
| 814 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 815 | /* |
| 816 | * static variable? |
| 817 | */ |
| 818 | if ((addr >= start) && (addr < end)) |
| 819 | return 1; |
| 820 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 821 | /* |
Tejun Heo | 10fad5e | 2010-03-10 18:57:54 +0900 | [diff] [blame] | 822 | * in-kernel percpu var? |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 823 | */ |
Tejun Heo | 10fad5e | 2010-03-10 18:57:54 +0900 | [diff] [blame] | 824 | if (is_kernel_percpu_address(addr)) |
| 825 | return 1; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 826 | |
| 827 | /* |
Tejun Heo | 10fad5e | 2010-03-10 18:57:54 +0900 | [diff] [blame] | 828 | * module static or percpu var? |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 829 | */ |
Tejun Heo | 10fad5e | 2010-03-10 18:57:54 +0900 | [diff] [blame] | 830 | return is_module_address(addr) || is_module_percpu_address(addr); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 831 | } |
Sasha Levin | 8dce7a9 | 2013-06-13 18:41:16 -0400 | [diff] [blame] | 832 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 833 | |
| 834 | /* |
| 835 | * To make lock name printouts unique, we calculate a unique |
Bart Van Assche | fe27b0d | 2018-12-06 17:11:37 -0800 | [diff] [blame] | 836 | * class->name_version generation counter. The caller must hold the graph |
| 837 | * lock. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 838 | */ |
| 839 | static 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 Assche | fe27b0d | 2018-12-06 17:11:37 -0800 | [diff] [blame] | 847 | list_for_each_entry(class, &all_lock_classes, lock_entry) { |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 848 | 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 Zijlstra | f6f48e1 | 2020-02-20 09:45:02 +0100 | [diff] [blame] | 857 | /* used from NMI context -- must be lockless */ |
Peter Zijlstra | 49faa77 | 2021-06-21 13:12:38 +0200 | [diff] [blame] | 858 | static noinstr struct lock_class * |
Matthew Wilcox | 08f36ff | 2018-01-17 07:14:13 -0800 | [diff] [blame] | 859 | look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 860 | { |
| 861 | struct lockdep_subclass_key *key; |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 862 | struct hlist_head *hash_head; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 863 | struct lock_class *class; |
| 864 | |
Hitoshi Mitake | 4ba053c | 2010-10-13 17:30:26 +0900 | [diff] [blame] | 865 | if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) { |
Peter Zijlstra | 49faa77 | 2021-06-21 13:12:38 +0200 | [diff] [blame] | 866 | instrumentation_begin(); |
Hitoshi Mitake | 4ba053c | 2010-10-13 17:30:26 +0900 | [diff] [blame] | 867 | 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 Zijlstra | 49faa77 | 2021-06-21 13:12:38 +0200 | [diff] [blame] | 873 | instrumentation_end(); |
Hitoshi Mitake | 4ba053c | 2010-10-13 17:30:26 +0900 | [diff] [blame] | 874 | return NULL; |
| 875 | } |
| 876 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 877 | /* |
Matthew Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 878 | * If it is not initialised then it has never been locked, |
| 879 | * so it won't be present in the hash table. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 880 | */ |
Matthew Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 881 | if (unlikely(!lock->key)) |
| 882 | return NULL; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 883 | |
| 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 Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 890 | BUILD_BUG_ON(sizeof(struct lock_class_key) > |
| 891 | sizeof(struct lockdep_map)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 892 | |
| 893 | key = lock->key->subkeys + subclass; |
| 894 | |
| 895 | hash_head = classhashentry(key); |
| 896 | |
| 897 | /* |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 898 | * We do an RCU walk of the hash, see lockdep_free_key_range(). |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 899 | */ |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 900 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 901 | return NULL; |
| 902 | |
Peter Zijlstra | ce0b9c8 | 2021-06-24 11:41:10 +0200 | [diff] [blame] | 903 | hlist_for_each_entry_rcu_notrace(class, hash_head, hash_entry) { |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 904 | if (class->key == key) { |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 905 | /* |
| 906 | * Huh! same key, different name? Did someone trample |
| 907 | * on some memory? We're most confused. |
| 908 | */ |
Sebastian Andrzej Siewior | 9783154 | 2019-05-17 23:22:34 +0200 | [diff] [blame] | 909 | WARN_ON_ONCE(class->name != lock->name && |
| 910 | lock->key != &__lockdep_no_validate__); |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 911 | return class; |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 912 | } |
| 913 | } |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 914 | |
Matthew Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 915 | 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 | */ |
| 923 | static bool assign_lock_key(struct lockdep_map *lock) |
| 924 | { |
| 925 | unsigned long can_addr, addr = (unsigned long)lock; |
| 926 | |
Bart Van Assche | 4bf5086 | 2019-02-14 15:00:52 -0800 | [diff] [blame] | 927 | #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 Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 938 | 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 Handa | 3a85969 | 2021-03-21 15:49:13 +0900 | [diff] [blame] | 948 | 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 Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 950 | pr_err("turning off the locking correctness validator.\n"); |
| 951 | dump_stack(); |
| 952 | return false; |
| 953 | } |
| 954 | |
| 955 | return true; |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Peter Zijlstra | 72dcd50 | 2019-02-25 15:56:32 +0100 | [diff] [blame] | 958 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 959 | |
Bart Van Assche | b526b2e | 2019-02-14 15:00:51 -0800 | [diff] [blame] | 960 | /* Check whether element @e occurs in list @h */ |
| 961 | static 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 | */ |
| 977 | static 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 | |
| 991 | static 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 Bergmann | 3fe7522 | 2019-03-07 08:52:12 +0100 | [diff] [blame] | 1010 | #ifdef CONFIG_PROVE_LOCKING |
| 1011 | static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS]; |
| 1012 | #endif |
Bart Van Assche | b526b2e | 2019-02-14 15:00:51 -0800 | [diff] [blame] | 1013 | |
| 1014 | static bool check_lock_chain_key(struct lock_chain *chain) |
| 1015 | { |
| 1016 | #ifdef CONFIG_PROVE_LOCKING |
Yuyang Du | f6ec882 | 2019-05-06 16:19:24 +0800 | [diff] [blame] | 1017 | u64 chain_key = INITIAL_CHAIN_KEY; |
Bart Van Assche | b526b2e | 2019-02-14 15:00:51 -0800 | [diff] [blame] | 1018 | int i; |
| 1019 | |
| 1020 | for (i = chain->base; i < chain->base + chain->depth; i++) |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 1021 | chain_key = iterate_chain_key(chain_key, chain_hlocks[i]); |
Bart Van Assche | b526b2e | 2019-02-14 15:00:51 -0800 | [diff] [blame] | 1022 | /* |
| 1023 | * The 'unsigned long long' casts avoid that a compiler warning |
| 1024 | * is reported when building tools/lib/lockdep. |
| 1025 | */ |
Peter Zijlstra | 72dcd50 | 2019-02-25 15:56:32 +0100 | [diff] [blame] | 1026 | if (chain->chain_key != chain_key) { |
Bart Van Assche | b526b2e | 2019-02-14 15:00:51 -0800 | [diff] [blame] | 1027 | 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 Zijlstra | 72dcd50 | 2019-02-25 15:56:32 +0100 | [diff] [blame] | 1031 | return false; |
| 1032 | } |
Bart Van Assche | b526b2e | 2019-02-14 15:00:51 -0800 | [diff] [blame] | 1033 | #endif |
Peter Zijlstra | 72dcd50 | 2019-02-25 15:56:32 +0100 | [diff] [blame] | 1034 | return true; |
Bart Van Assche | b526b2e | 2019-02-14 15:00:51 -0800 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | static bool in_any_zapped_class_list(struct lock_class *class) |
| 1038 | { |
| 1039 | struct pending_free *pf; |
| 1040 | int i; |
| 1041 | |
Peter Zijlstra | 72dcd50 | 2019-02-25 15:56:32 +0100 | [diff] [blame] | 1042 | for (i = 0, pf = delayed_free.pf; i < ARRAY_SIZE(delayed_free.pf); i++, pf++) { |
Bart Van Assche | b526b2e | 2019-02-14 15:00:51 -0800 | [diff] [blame] | 1043 | if (in_list(&class->lock_entry, &pf->zapped)) |
| 1044 | return true; |
Peter Zijlstra | 72dcd50 | 2019-02-25 15:56:32 +0100 | [diff] [blame] | 1045 | } |
Bart Van Assche | b526b2e | 2019-02-14 15:00:51 -0800 | [diff] [blame] | 1046 | |
| 1047 | return false; |
| 1048 | } |
| 1049 | |
Peter Zijlstra | 72dcd50 | 2019-02-25 15:56:32 +0100 | [diff] [blame] | 1050 | static bool __check_data_structures(void) |
Bart Van Assche | b526b2e | 2019-02-14 15:00:51 -0800 | [diff] [blame] | 1051 | { |
| 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 Assche | b526b2e | 2019-02-14 15:00:51 -0800 | [diff] [blame] | 1067 | } |
| 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 Zijlstra | 72dcd50 | 2019-02-25 15:56:32 +0100 | [diff] [blame] | 1123 | int check_consistency = 0; |
| 1124 | module_param(check_consistency, int, 0644); |
| 1125 | |
| 1126 | static 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 | |
| 1140 | static inline void check_data_structures(void) { } |
| 1141 | |
| 1142 | #endif /* CONFIG_DEBUG_LOCKDEP */ |
| 1143 | |
Waiman Long | 810507f | 2020-02-06 10:24:08 -0500 | [diff] [blame] | 1144 | static void init_chain_block_buckets(void); |
| 1145 | |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 1146 | /* |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 1147 | * Initialize the lock_classes[] array elements, the free_lock_classes list |
| 1148 | * and also the delayed_free structure. |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 1149 | */ |
| 1150 | static void init_data_structures_once(void) |
| 1151 | { |
Waiman Long | 810507f | 2020-02-06 10:24:08 -0500 | [diff] [blame] | 1152 | static bool __read_mostly ds_initialized, rcu_head_initialized; |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 1153 | int i; |
| 1154 | |
Bart Van Assche | 0126574 | 2019-03-03 10:19:01 -0800 | [diff] [blame] | 1155 | if (likely(rcu_head_initialized)) |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 1156 | return; |
| 1157 | |
Bart Van Assche | 0126574 | 2019-03-03 10:19:01 -0800 | [diff] [blame] | 1158 | if (system_state >= SYSTEM_SCHEDULING) { |
| 1159 | init_rcu_head(&delayed_free.rcu_head); |
| 1160 | rcu_head_initialized = true; |
| 1161 | } |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 1162 | |
Bart Van Assche | 0126574 | 2019-03-03 10:19:01 -0800 | [diff] [blame] | 1163 | if (ds_initialized) |
| 1164 | return; |
| 1165 | |
| 1166 | ds_initialized = true; |
| 1167 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 1168 | INIT_LIST_HEAD(&delayed_free.pf[0].zapped); |
| 1169 | INIT_LIST_HEAD(&delayed_free.pf[1].zapped); |
| 1170 | |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 1171 | for (i = 0; i < ARRAY_SIZE(lock_classes); i++) { |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 1172 | list_add_tail(&lock_classes[i].lock_entry, &free_lock_classes); |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 1173 | INIT_LIST_HEAD(&lock_classes[i].locks_after); |
| 1174 | INIT_LIST_HEAD(&lock_classes[i].locks_before); |
| 1175 | } |
Waiman Long | 810507f | 2020-02-06 10:24:08 -0500 | [diff] [blame] | 1176 | init_chain_block_buckets(); |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
Bart Van Assche | 108c148 | 2019-02-14 15:00:53 -0800 | [diff] [blame] | 1179 | static 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. */ |
| 1187 | void 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); |
| 1205 | out_unlock: |
| 1206 | graph_unlock(); |
| 1207 | restore_irqs: |
| 1208 | raw_local_irq_restore(flags); |
| 1209 | } |
| 1210 | EXPORT_SYMBOL_GPL(lockdep_register_key); |
| 1211 | |
| 1212 | /* Check whether a key has been registered as a dynamic key. */ |
| 1213 | static 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 Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 1244 | /* |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 1245 | * 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 Vlasenko | c003ed9 | 2016-04-08 20:58:46 +0200 | [diff] [blame] | 1249 | static struct lock_class * |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 1250 | register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force) |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 1251 | { |
| 1252 | struct lockdep_subclass_key *key; |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 1253 | struct hlist_head *hash_head; |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 1254 | struct lock_class *class; |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 1255 | |
| 1256 | DEBUG_LOCKS_WARN_ON(!irqs_disabled()); |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 1257 | |
| 1258 | class = look_up_lock_class(lock, subclass); |
Matthew Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 1259 | if (likely(class)) |
Yong Zhang | 87cdee7 | 2011-11-09 16:07:14 +0800 | [diff] [blame] | 1260 | goto out_set_class_cache; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1261 | |
Matthew Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 1262 | if (!lock->key) { |
| 1263 | if (!assign_lock_key(lock)) |
| 1264 | return NULL; |
Bart Van Assche | 108c148 | 2019-02-14 15:00:53 -0800 | [diff] [blame] | 1265 | } else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) { |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1266 | return NULL; |
| 1267 | } |
| 1268 | |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 1269 | key = lock->key->subkeys + subclass; |
| 1270 | hash_head = classhashentry(key); |
| 1271 | |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1272 | if (!graph_lock()) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1273 | return NULL; |
| 1274 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1275 | /* |
| 1276 | * We have to do the hash-walk again, to avoid races |
| 1277 | * with another CPU: |
| 1278 | */ |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 1279 | hlist_for_each_entry_rcu(class, hash_head, hash_entry) { |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1280 | if (class->key == key) |
| 1281 | goto out_unlock_set; |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 1282 | } |
| 1283 | |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 1284 | init_data_structures_once(); |
| 1285 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 1286 | /* 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 Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1290 | if (!debug_locks_off_graph_unlock()) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1291 | return NULL; |
| 1292 | } |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1293 | |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 1294 | print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!"); |
Peter Zijlstra | eedeeab | 2009-03-18 12:38:47 +0100 | [diff] [blame] | 1295 | dump_stack(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1296 | return NULL; |
| 1297 | } |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 1298 | nr_lock_classes++; |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 1299 | __set_bit(class - lock_classes, lock_classes_in_use); |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 1300 | debug_atomic_inc(nr_unused_locks); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1301 | class->key = key; |
| 1302 | class->name = lock->name; |
| 1303 | class->subclass = subclass; |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 1304 | WARN_ON_ONCE(!list_empty(&class->locks_before)); |
| 1305 | WARN_ON_ONCE(!list_empty(&class->locks_after)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1306 | class->name_version = count_matching_names(class); |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 1307 | class->wait_type_inner = lock->wait_type_inner; |
| 1308 | class->wait_type_outer = lock->wait_type_outer; |
Peter Zijlstra | dfd5e3f | 2020-12-09 16:06:21 +0100 | [diff] [blame] | 1309 | class->lock_type = lock->lock_type; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1310 | /* |
| 1311 | * We use RCU's safe list-add method to make |
| 1312 | * parallel walking of the hash-list safe: |
| 1313 | */ |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 1314 | hlist_add_head_rcu(&class->hash_entry, hash_head); |
Dale Farnsworth | 1481197 | 2008-02-25 23:03:02 +0100 | [diff] [blame] | 1315 | /* |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 1316 | * Remove the class from the free list and add it to the global list |
| 1317 | * of classes. |
Dale Farnsworth | 1481197 | 2008-02-25 23:03:02 +0100 | [diff] [blame] | 1318 | */ |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 1319 | list_move_tail(&class->lock_entry, &all_lock_classes); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1320 | |
| 1321 | if (verbose(class)) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1322 | graph_unlock(); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1323 | |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 1324 | printk("\nnew class %px: %s", class->key, class->name); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1325 | if (class->name_version > 1) |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1326 | printk(KERN_CONT "#%d", class->name_version); |
| 1327 | printk(KERN_CONT "\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1328 | dump_stack(); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1329 | |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1330 | if (!graph_lock()) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1331 | return NULL; |
| 1332 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1333 | } |
| 1334 | out_unlock_set: |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 1335 | graph_unlock(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1336 | |
Yong Zhang | 87cdee7 | 2011-11-09 16:07:14 +0800 | [diff] [blame] | 1337 | out_set_class_cache: |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 1338 | if (!subclass || force) |
Hitoshi Mitake | 6201625 | 2010-10-05 18:01:51 +0900 | [diff] [blame] | 1339 | lock->class_cache[0] = class; |
| 1340 | else if (subclass < NR_LOCKDEP_CACHING_CLASSES) |
| 1341 | lock->class_cache[subclass] = class; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1342 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 1343 | /* |
| 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 Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 1347 | if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass)) |
| 1348 | return NULL; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1349 | |
| 1350 | return class; |
| 1351 | } |
| 1352 | |
Peter Zijlstra | ca58abc | 2007-07-19 01:48:53 -0700 | [diff] [blame] | 1353 | #ifdef CONFIG_PROVE_LOCKING |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1354 | /* |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1355 | * Allocate a lockdep entry. (assumes the graph_lock held, returns |
| 1356 | * with NULL on failure) |
| 1357 | */ |
| 1358 | static struct lock_list *alloc_list_entry(void) |
| 1359 | { |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 1360 | int idx = find_first_zero_bit(list_entries_in_use, |
| 1361 | ARRAY_SIZE(list_entries)); |
| 1362 | |
| 1363 | if (idx >= ARRAY_SIZE(list_entries)) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1364 | if (!debug_locks_off_graph_unlock()) |
| 1365 | return NULL; |
| 1366 | |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 1367 | print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!"); |
Peter Zijlstra | eedeeab | 2009-03-18 12:38:47 +0100 | [diff] [blame] | 1368 | dump_stack(); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1369 | return NULL; |
| 1370 | } |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 1371 | nr_list_entries++; |
| 1372 | __set_bit(idx, list_entries_in_use); |
| 1373 | return list_entries + idx; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | /* |
| 1377 | * Add a new dependency to the head of the list: |
| 1378 | */ |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 1379 | static int add_lock_to_list(struct lock_class *this, |
| 1380 | struct lock_class *links_to, struct list_head *head, |
Boqun Feng | 3454a36 | 2020-08-07 15:42:25 +0800 | [diff] [blame] | 1381 | unsigned long ip, u16 distance, u8 dep, |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 1382 | const struct lock_trace *trace) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1383 | { |
| 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 Yi | 7487017 | 2008-08-27 14:33:00 +0800 | [diff] [blame] | 1393 | entry->class = this; |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 1394 | entry->links_to = links_to; |
Boqun Feng | 3454a36 | 2020-08-07 15:42:25 +0800 | [diff] [blame] | 1395 | entry->dep = dep; |
Zhu Yi | 7487017 | 2008-08-27 14:33:00 +0800 | [diff] [blame] | 1396 | entry->distance = distance; |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 1397 | entry->trace = trace; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1398 | /* |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 1399 | * 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1402 | */ |
| 1403 | list_add_tail_rcu(&entry->entry, head); |
| 1404 | |
| 1405 | return 1; |
| 1406 | } |
| 1407 | |
Peter Zijlstra | 98c33ed | 2009-07-21 13:19:07 +0200 | [diff] [blame] | 1408 | /* |
| 1409 | * For good efficiency of modular, we use power of 2 |
| 1410 | */ |
Tetsuo Handa | 5dc3359 | 2021-04-05 20:33:57 +0900 | [diff] [blame] | 1411 | #define MAX_CIRCULAR_QUEUE_SIZE (1UL << CONFIG_LOCKDEP_CIRCULAR_QUEUE_BITS) |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1412 | #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1) |
| 1413 | |
Peter Zijlstra | 98c33ed | 2009-07-21 13:19:07 +0200 | [diff] [blame] | 1414 | /* |
Yuyang Du | aa48077 | 2019-05-06 16:19:28 +0800 | [diff] [blame] | 1415 | * 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 Zijlstra | 98c33ed | 2009-07-21 13:19:07 +0200 | [diff] [blame] | 1423 | */ |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1424 | struct circular_queue { |
Yuyang Du | aa48077 | 2019-05-06 16:19:28 +0800 | [diff] [blame] | 1425 | struct lock_list *element[MAX_CIRCULAR_QUEUE_SIZE]; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1426 | unsigned int front, rear; |
| 1427 | }; |
| 1428 | |
| 1429 | static struct circular_queue lock_cq; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1430 | |
Ming Lei | 12f3dfd | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1431 | unsigned int max_bfs_queue_depth; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1432 | |
Ming Lei | e351b66 | 2009-07-22 22:48:09 +0800 | [diff] [blame] | 1433 | static unsigned int lockdep_dependency_gen_id; |
| 1434 | |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1435 | static inline void __cq_init(struct circular_queue *cq) |
| 1436 | { |
| 1437 | cq->front = cq->rear = 0; |
Ming Lei | e351b66 | 2009-07-22 22:48:09 +0800 | [diff] [blame] | 1438 | lockdep_dependency_gen_id++; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | static inline int __cq_empty(struct circular_queue *cq) |
| 1442 | { |
| 1443 | return (cq->front == cq->rear); |
| 1444 | } |
| 1445 | |
| 1446 | static inline int __cq_full(struct circular_queue *cq) |
| 1447 | { |
| 1448 | return ((cq->rear + 1) & CQ_MASK) == cq->front; |
| 1449 | } |
| 1450 | |
Yuyang Du | aa48077 | 2019-05-06 16:19:28 +0800 | [diff] [blame] | 1451 | static inline int __cq_enqueue(struct circular_queue *cq, struct lock_list *elem) |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1452 | { |
| 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 Du | c166132 | 2019-05-06 16:19:29 +0800 | [diff] [blame] | 1461 | /* |
| 1462 | * Dequeue an element from the circular_queue, return a lock_list if |
| 1463 | * the queue is not empty, or NULL if otherwise. |
| 1464 | */ |
| 1465 | static inline struct lock_list * __cq_dequeue(struct circular_queue *cq) |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1466 | { |
Yuyang Du | c166132 | 2019-05-06 16:19:29 +0800 | [diff] [blame] | 1467 | struct lock_list * lock; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1468 | |
Yuyang Du | c166132 | 2019-05-06 16:19:29 +0800 | [diff] [blame] | 1469 | if (__cq_empty(cq)) |
| 1470 | return NULL; |
| 1471 | |
| 1472 | lock = cq->element[cq->front]; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1473 | cq->front = (cq->front + 1) & CQ_MASK; |
Yuyang Du | c166132 | 2019-05-06 16:19:29 +0800 | [diff] [blame] | 1474 | |
| 1475 | return lock; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | static inline unsigned int __cq_get_elem_count(struct circular_queue *cq) |
| 1479 | { |
| 1480 | return (cq->rear - cq->front) & CQ_MASK; |
| 1481 | } |
| 1482 | |
Boqun Feng | d563bc6 | 2020-08-07 15:42:23 +0800 | [diff] [blame] | 1483 | static inline void mark_lock_accessed(struct lock_list *lock) |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1484 | { |
Ming Lei | e351b66 | 2009-07-22 22:48:09 +0800 | [diff] [blame] | 1485 | lock->class->dep_gen_id = lockdep_dependency_gen_id; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1486 | } |
| 1487 | |
Boqun Feng | d563bc6 | 2020-08-07 15:42:23 +0800 | [diff] [blame] | 1488 | static inline void visit_lock_entry(struct lock_list *lock, |
| 1489 | struct lock_list *parent) |
| 1490 | { |
| 1491 | lock->parent = parent; |
| 1492 | } |
| 1493 | |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1494 | static inline unsigned long lock_accessed(struct lock_list *lock) |
| 1495 | { |
Ming Lei | e351b66 | 2009-07-22 22:48:09 +0800 | [diff] [blame] | 1496 | return lock->class->dep_gen_id == lockdep_dependency_gen_id; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | static inline struct lock_list *get_lock_parent(struct lock_list *child) |
| 1500 | { |
| 1501 | return child->parent; |
| 1502 | } |
| 1503 | |
| 1504 | static 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 Du | 77a8069 | 2019-05-06 16:19:30 +0800 | [diff] [blame] | 1516 | /* |
| 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 | */ |
| 1523 | static 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 Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 1529 | /* |
| 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 | */ |
| 1545 | enum 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 | */ |
| 1555 | static inline bool bfs_error(enum bfs_result res) |
| 1556 | { |
| 1557 | return res < 0; |
| 1558 | } |
Yuyang Du | 77a8069 | 2019-05-06 16:19:30 +0800 | [diff] [blame] | 1559 | |
Yuyang Du | 154f185 | 2019-05-06 16:19:31 +0800 | [diff] [blame] | 1560 | /* |
Boqun Feng | 3454a36 | 2020-08-07 15:42:25 +0800 | [diff] [blame] | 1561 | * 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 | |
| 1585 | static 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 | |
| 1591 | static 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 | */ |
| 1600 | static 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 | |
| 1606 | static 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 Feng | 6971c0f | 2020-08-07 15:42:26 +0800 | [diff] [blame] | 1612 | * Initialize a lock_list entry @lock belonging to @class as the root for a BFS |
| 1613 | * search. |
| 1614 | */ |
| 1615 | static 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 | */ |
| 1631 | static 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 | */ |
| 1645 | static 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 Feng | 6d1823c | 2020-09-17 16:01:50 +0800 | [diff] [blame] | 1652 | static 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 Feng | 6971c0f | 2020-08-07 15:42:26 +0800 | [diff] [blame] | 1661 | /* |
| 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 Du | 154f185 | 2019-05-06 16:19:31 +0800 | [diff] [blame] | 1687 | */ |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 1688 | static enum bfs_result __bfs(struct lock_list *source_entry, |
| 1689 | void *data, |
Boqun Feng | 61775ed | 2020-08-07 15:42:27 +0800 | [diff] [blame] | 1690 | bool (*match)(struct lock_list *entry, void *data), |
Boqun Feng | bc2dd71 | 2020-12-10 11:02:40 +0100 | [diff] [blame] | 1691 | bool (*skip)(struct lock_list *entry, void *data), |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 1692 | struct lock_list **target_entry, |
| 1693 | int offset) |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1694 | { |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1695 | struct circular_queue *cq = &lock_cq; |
Boqun Feng | 6d1823c | 2020-09-17 16:01:50 +0800 | [diff] [blame] | 1696 | struct lock_list *lock = NULL; |
| 1697 | struct lock_list *entry; |
| 1698 | struct list_head *head; |
| 1699 | unsigned int cq_depth; |
| 1700 | bool first; |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1701 | |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 1702 | lockdep_assert_locked(); |
| 1703 | |
Ming Lei | d588e46 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1704 | __cq_init(cq); |
Yuyang Du | aa48077 | 2019-05-06 16:19:28 +0800 | [diff] [blame] | 1705 | __cq_enqueue(cq, source_entry); |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1706 | |
Boqun Feng | 6d1823c | 2020-09-17 16:01:50 +0800 | [diff] [blame] | 1707 | while ((lock = __bfs_next(lock, offset)) || (lock = __cq_dequeue(cq))) { |
| 1708 | if (!lock->class) |
| 1709 | return BFS_EINVALIDNODE; |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1710 | |
Boqun Feng | d563bc6 | 2020-08-07 15:42:23 +0800 | [diff] [blame] | 1711 | /* |
Boqun Feng | 6d1823c | 2020-09-17 16:01:50 +0800 | [diff] [blame] | 1712 | * Step 1: check whether we already finish on this one. |
| 1713 | * |
Boqun Feng | d563bc6 | 2020-08-07 15:42:23 +0800 | [diff] [blame] | 1714 | * 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 Feng | 6d1823c | 2020-09-17 16:01:50 +0800 | [diff] [blame] | 1725 | /* |
| 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 Feng | 6971c0f | 2020-08-07 15:42:26 +0800 | [diff] [blame] | 1732 | |
| 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 Feng | 6d1823c | 2020-09-17 16:01:50 +0800 | [diff] [blame] | 1746 | lock->only_xr = !(dep & (DEP_SN_MASK | DEP_EN_MASK)); |
| 1747 | } |
Boqun Feng | d563bc6 | 2020-08-07 15:42:23 +0800 | [diff] [blame] | 1748 | |
Boqun Feng | 6d1823c | 2020-09-17 16:01:50 +0800 | [diff] [blame] | 1749 | /* |
| 1750 | * Step 3: we haven't visited this and there is a strong |
| 1751 | * dependency path to this, so check with @match. |
Boqun Feng | bc2dd71 | 2020-12-10 11:02:40 +0100 | [diff] [blame] | 1752 | * If @skip is provide and returns true, we skip this |
| 1753 | * lock (and any path this lock is in). |
Boqun Feng | 6d1823c | 2020-09-17 16:01:50 +0800 | [diff] [blame] | 1754 | */ |
Boqun Feng | bc2dd71 | 2020-12-10 11:02:40 +0100 | [diff] [blame] | 1755 | if (skip && skip(lock, data)) |
| 1756 | continue; |
| 1757 | |
Boqun Feng | 6d1823c | 2020-09-17 16:01:50 +0800 | [diff] [blame] | 1758 | 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 Molnar | e2db759 | 2021-03-22 02:35:05 +0100 | [diff] [blame] | 1765 | * forward or backwards dependencies in the search |
Boqun Feng | 6d1823c | 2020-09-17 16:01:50 +0800 | [diff] [blame] | 1766 | * |
| 1767 | */ |
| 1768 | first = true; |
| 1769 | head = get_dep_list(lock, offset); |
| 1770 | list_for_each_entry_rcu(entry, head, entry) { |
Boqun Feng | d563bc6 | 2020-08-07 15:42:23 +0800 | [diff] [blame] | 1771 | visit_lock_entry(entry, lock); |
Boqun Feng | d563bc6 | 2020-08-07 15:42:23 +0800 | [diff] [blame] | 1772 | |
Boqun Feng | 6d1823c | 2020-09-17 16:01:50 +0800 | [diff] [blame] | 1773 | /* |
| 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 Feng | d563bc6 | 2020-08-07 15:42:23 +0800 | [diff] [blame] | 1787 | cq_depth = __cq_get_elem_count(cq); |
| 1788 | if (max_bfs_queue_depth < cq_depth) |
| 1789 | max_bfs_queue_depth = cq_depth; |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1790 | } |
| 1791 | } |
Boqun Feng | 6d1823c | 2020-09-17 16:01:50 +0800 | [diff] [blame] | 1792 | |
| 1793 | return BFS_RNOMATCH; |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1794 | } |
| 1795 | |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 1796 | static inline enum bfs_result |
| 1797 | __bfs_forwards(struct lock_list *src_entry, |
| 1798 | void *data, |
Boqun Feng | 61775ed | 2020-08-07 15:42:27 +0800 | [diff] [blame] | 1799 | bool (*match)(struct lock_list *entry, void *data), |
Boqun Feng | bc2dd71 | 2020-12-10 11:02:40 +0100 | [diff] [blame] | 1800 | bool (*skip)(struct lock_list *entry, void *data), |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 1801 | struct lock_list **target_entry) |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1802 | { |
Boqun Feng | bc2dd71 | 2020-12-10 11:02:40 +0100 | [diff] [blame] | 1803 | return __bfs(src_entry, data, match, skip, target_entry, |
Yuyang Du | 77a8069 | 2019-05-06 16:19:30 +0800 | [diff] [blame] | 1804 | offsetof(struct lock_class, locks_after)); |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1805 | |
| 1806 | } |
| 1807 | |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 1808 | static inline enum bfs_result |
| 1809 | __bfs_backwards(struct lock_list *src_entry, |
| 1810 | void *data, |
Boqun Feng | 61775ed | 2020-08-07 15:42:27 +0800 | [diff] [blame] | 1811 | bool (*match)(struct lock_list *entry, void *data), |
Boqun Feng | bc2dd71 | 2020-12-10 11:02:40 +0100 | [diff] [blame] | 1812 | bool (*skip)(struct lock_list *entry, void *data), |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 1813 | struct lock_list **target_entry) |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1814 | { |
Boqun Feng | bc2dd71 | 2020-12-10 11:02:40 +0100 | [diff] [blame] | 1815 | return __bfs(src_entry, data, match, skip, target_entry, |
Yuyang Du | 77a8069 | 2019-05-06 16:19:30 +0800 | [diff] [blame] | 1816 | offsetof(struct lock_class, locks_before)); |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1817 | |
| 1818 | } |
| 1819 | |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 1820 | static void print_lock_trace(const struct lock_trace *trace, |
| 1821 | unsigned int spaces) |
Thomas Gleixner | c120bce | 2019-04-25 11:45:12 +0200 | [diff] [blame] | 1822 | { |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 1823 | stack_trace_print(trace->entries, trace->nr_entries, spaces); |
Thomas Gleixner | c120bce | 2019-04-25 11:45:12 +0200 | [diff] [blame] | 1824 | } |
| 1825 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1826 | /* |
| 1827 | * Print a dependency chain entry (this is only done when a deadlock |
| 1828 | * has been detected): |
| 1829 | */ |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 1830 | static noinline void |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1831 | print_circular_bug_entry(struct lock_list *target, int depth) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1832 | { |
| 1833 | if (debug_locks_silent) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 1834 | return; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1835 | printk("\n-> #%u", depth); |
| 1836 | print_lock_name(target->class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1837 | printk(KERN_CONT ":\n"); |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 1838 | print_lock_trace(target->trace, 6); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1841 | static void |
| 1842 | print_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 Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1866 | printk(KERN_CONT " --> "); |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1867 | __print_lock_name(parent); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1868 | printk(KERN_CONT " --> "); |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1869 | __print_lock_name(target); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1870 | printk(KERN_CONT "\n\n"); |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1871 | } |
| 1872 | |
Ingo Molnar | e966eae | 2017-12-12 12:31:16 +0100 | [diff] [blame] | 1873 | 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 Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1889 | } |
| 1890 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1891 | /* |
| 1892 | * When a circular dependency is detected, print the |
| 1893 | * header first: |
| 1894 | */ |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 1895 | static noinline void |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1896 | print_circular_bug_header(struct lock_list *entry, unsigned int depth, |
| 1897 | struct held_lock *check_src, |
| 1898 | struct held_lock *check_tgt) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1899 | { |
| 1900 | struct task_struct *curr = current; |
| 1901 | |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1902 | if (debug_locks_silent) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 1903 | return; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1904 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1905 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 1906 | pr_warn("======================================================\n"); |
| 1907 | pr_warn("WARNING: possible circular locking dependency detected\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 1908 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 1909 | pr_warn("------------------------------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1910 | pr_warn("%s/%d is trying to acquire lock:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 1911 | curr->comm, task_pid_nr(curr)); |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1912 | print_lock(check_src); |
Byungchul Park | 383a4bc | 2017-08-07 16:12:55 +0900 | [diff] [blame] | 1913 | |
Ingo Molnar | e966eae | 2017-12-12 12:31:16 +0100 | [diff] [blame] | 1914 | pr_warn("\nbut task is already holding lock:\n"); |
Byungchul Park | 383a4bc | 2017-08-07 16:12:55 +0900 | [diff] [blame] | 1915 | |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1916 | print_lock(check_tgt); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1917 | 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1919 | |
| 1920 | print_circular_bug_entry(entry, depth); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
Boqun Feng | 68e3056 | 2020-08-07 15:42:29 +0800 | [diff] [blame] | 1923 | /* |
| 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 Molnar | e2db759 | 2021-03-22 02:35:05 +0100 | [diff] [blame] | 1934 | * .. -> B -> .. with A -> .. -> B. Therefore A -> B is redundant. |
Boqun Feng | 68e3056 | 2020-08-07 15:42:29 +0800 | [diff] [blame] | 1935 | * |
| 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 | */ |
| 1949 | static inline bool hlock_equal(struct lock_list *entry, void *data) |
Ming Lei | 9e2d551 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1950 | { |
Boqun Feng | 68e3056 | 2020-08-07 15:42:29 +0800 | [diff] [blame] | 1951 | 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 Lei | 9e2d551 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1956 | } |
| 1957 | |
Boqun Feng | 9de0c9b | 2020-08-07 15:42:28 +0800 | [diff] [blame] | 1958 | /* |
| 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 | */ |
| 1976 | static 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 Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 1985 | static noinline void print_circular_bug(struct lock_list *this, |
Boqun Feng | 9de0c9b | 2020-08-07 15:42:28 +0800 | [diff] [blame] | 1986 | struct lock_list *target, |
| 1987 | struct held_lock *check_src, |
| 1988 | struct held_lock *check_tgt) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1989 | { |
| 1990 | struct task_struct *curr = current; |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1991 | struct lock_list *parent; |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1992 | struct lock_list *first_parent; |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1993 | int depth; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1994 | |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1995 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 1996 | return; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1997 | |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 1998 | this->trace = save_trace(); |
| 1999 | if (!this->trace) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2000 | return; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2001 | |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2002 | depth = get_lock_depth(target); |
| 2003 | |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2004 | print_circular_bug_header(target, depth, check_src, check_tgt); |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2005 | |
| 2006 | parent = get_lock_parent(target); |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 2007 | first_parent = parent; |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2008 | |
| 2009 | while (parent) { |
| 2010 | print_circular_bug_entry(parent, --depth); |
| 2011 | parent = get_lock_parent(parent); |
| 2012 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2013 | |
| 2014 | printk("\nother info that might help us debug this:\n\n"); |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 2015 | print_circular_lock_scenario(check_src, check_tgt, |
| 2016 | first_parent); |
| 2017 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2018 | lockdep_print_held_locks(curr); |
| 2019 | |
| 2020 | printk("\nstack backtrace:\n"); |
| 2021 | dump_stack(); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2024 | static noinline void print_bfs_bug(int ret) |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2025 | { |
| 2026 | if (!debug_locks_off_graph_unlock()) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2027 | return; |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2028 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2029 | /* |
| 2030 | * Breadth-first-search failed, graph got corrupted? |
| 2031 | */ |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2032 | WARN(1, "lockdep bfs error:%d\n", ret); |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2033 | } |
| 2034 | |
Boqun Feng | 61775ed | 2020-08-07 15:42:27 +0800 | [diff] [blame] | 2035 | static bool noop_count(struct lock_list *entry, void *data) |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 2036 | { |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2037 | (*(unsigned long *)data)++; |
Boqun Feng | 61775ed | 2020-08-07 15:42:27 +0800 | [diff] [blame] | 2038 | return false; |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 2039 | } |
| 2040 | |
Fengguang Wu | 5216d53 | 2013-11-09 00:55:35 +0800 | [diff] [blame] | 2041 | static unsigned long __lockdep_count_forward_deps(struct lock_list *this) |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2042 | { |
| 2043 | unsigned long count = 0; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 2044 | struct lock_list *target_entry; |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2045 | |
Boqun Feng | bc2dd71 | 2020-12-10 11:02:40 +0100 | [diff] [blame] | 2046 | __bfs_forwards(this, (void *)&count, noop_count, NULL, &target_entry); |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2047 | |
| 2048 | return count; |
| 2049 | } |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 2050 | unsigned long lockdep_count_forward_deps(struct lock_class *class) |
| 2051 | { |
| 2052 | unsigned long ret, flags; |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2053 | struct lock_list this; |
| 2054 | |
Boqun Feng | 6971c0f | 2020-08-07 15:42:26 +0800 | [diff] [blame] | 2055 | __bfs_init_root(&this, class); |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 2056 | |
Steven Rostedt (VMware) | fcc784b | 2018-04-04 14:06:30 -0400 | [diff] [blame] | 2057 | raw_local_irq_save(flags); |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 2058 | lockdep_lock(); |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2059 | ret = __lockdep_count_forward_deps(&this); |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 2060 | lockdep_unlock(); |
Steven Rostedt (VMware) | fcc784b | 2018-04-04 14:06:30 -0400 | [diff] [blame] | 2061 | raw_local_irq_restore(flags); |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 2062 | |
| 2063 | return ret; |
| 2064 | } |
| 2065 | |
Fengguang Wu | 5216d53 | 2013-11-09 00:55:35 +0800 | [diff] [blame] | 2066 | static unsigned long __lockdep_count_backward_deps(struct lock_list *this) |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 2067 | { |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2068 | unsigned long count = 0; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 2069 | struct lock_list *target_entry; |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 2070 | |
Boqun Feng | bc2dd71 | 2020-12-10 11:02:40 +0100 | [diff] [blame] | 2071 | __bfs_backwards(this, (void *)&count, noop_count, NULL, &target_entry); |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 2072 | |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2073 | return count; |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | unsigned long lockdep_count_backward_deps(struct lock_class *class) |
| 2077 | { |
| 2078 | unsigned long ret, flags; |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2079 | struct lock_list this; |
| 2080 | |
Boqun Feng | 6971c0f | 2020-08-07 15:42:26 +0800 | [diff] [blame] | 2081 | __bfs_init_root(&this, class); |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 2082 | |
Steven Rostedt (VMware) | fcc784b | 2018-04-04 14:06:30 -0400 | [diff] [blame] | 2083 | raw_local_irq_save(flags); |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 2084 | lockdep_lock(); |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2085 | ret = __lockdep_count_backward_deps(&this); |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 2086 | lockdep_unlock(); |
Steven Rostedt (VMware) | fcc784b | 2018-04-04 14:06:30 -0400 | [diff] [blame] | 2087 | raw_local_irq_restore(flags); |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 2088 | |
| 2089 | return ret; |
| 2090 | } |
| 2091 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2092 | /* |
Yuyang Du | 8c2c2b4 | 2019-05-06 16:19:35 +0800 | [diff] [blame] | 2093 | * Check that the dependency graph starting at <src> can lead to |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2094 | * <target> or not. |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2095 | */ |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2096 | static noinline enum bfs_result |
Boqun Feng | 9de0c9b | 2020-08-07 15:42:28 +0800 | [diff] [blame] | 2097 | check_path(struct held_lock *target, struct lock_list *src_entry, |
| 2098 | bool (*match)(struct lock_list *entry, void *data), |
Boqun Feng | bc2dd71 | 2020-12-10 11:02:40 +0100 | [diff] [blame] | 2099 | bool (*skip)(struct lock_list *entry, void *data), |
Yuyang Du | 8c2c2b4 | 2019-05-06 16:19:35 +0800 | [diff] [blame] | 2100 | struct lock_list **target_entry) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2101 | { |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2102 | enum bfs_result ret; |
Yuyang Du | 8c2c2b4 | 2019-05-06 16:19:35 +0800 | [diff] [blame] | 2103 | |
Boqun Feng | bc2dd71 | 2020-12-10 11:02:40 +0100 | [diff] [blame] | 2104 | ret = __bfs_forwards(src_entry, target, match, skip, target_entry); |
Yuyang Du | 8c2c2b4 | 2019-05-06 16:19:35 +0800 | [diff] [blame] | 2105 | |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2106 | if (unlikely(bfs_error(ret))) |
Yuyang Du | 8c2c2b4 | 2019-05-06 16:19:35 +0800 | [diff] [blame] | 2107 | 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 Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2117 | * Print an error and return BFS_RMATCH if it does. |
Yuyang Du | 8c2c2b4 | 2019-05-06 16:19:35 +0800 | [diff] [blame] | 2118 | */ |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2119 | static noinline enum bfs_result |
Yuyang Du | 8c2c2b4 | 2019-05-06 16:19:35 +0800 | [diff] [blame] | 2120 | check_noncircular(struct held_lock *src, struct held_lock *target, |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 2121 | struct lock_trace **const trace) |
Yuyang Du | 8c2c2b4 | 2019-05-06 16:19:35 +0800 | [diff] [blame] | 2122 | { |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2123 | enum bfs_result ret; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 2124 | struct lock_list *target_entry; |
Boqun Feng | 6971c0f | 2020-08-07 15:42:26 +0800 | [diff] [blame] | 2125 | struct lock_list src_entry; |
| 2126 | |
| 2127 | bfs_init_root(&src_entry, src); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2128 | |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 2129 | debug_atomic_inc(nr_cyclic_checks); |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 2130 | |
Boqun Feng | bc2dd71 | 2020-12-10 11:02:40 +0100 | [diff] [blame] | 2131 | ret = check_path(target, &src_entry, hlock_conflict, NULL, &target_entry); |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2132 | |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2133 | if (unlikely(ret == BFS_RMATCH)) { |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 2134 | if (!*trace) { |
Yuyang Du | 8c2c2b4 | 2019-05-06 16:19:35 +0800 | [diff] [blame] | 2135 | /* |
| 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 Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 2140 | *trace = save_trace(); |
Yuyang Du | 8c2c2b4 | 2019-05-06 16:19:35 +0800 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | print_circular_bug(&src_entry, target_entry, src, target); |
| 2144 | } |
| 2145 | |
| 2146 | return ret; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
Yuyang Du | e7a38f6 | 2019-05-06 16:19:20 +0800 | [diff] [blame] | 2149 | #ifdef CONFIG_TRACE_IRQFLAGS |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2150 | |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 2151 | /* |
| 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 Feng | 61775ed | 2020-08-07 15:42:27 +0800 | [diff] [blame] | 2192 | static inline bool usage_accumulate(struct lock_list *entry, void *mask) |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2193 | { |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 2194 | 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 Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2198 | |
Boqun Feng | 61775ed | 2020-08-07 15:42:27 +0800 | [diff] [blame] | 2199 | return false; |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2200 | } |
| 2201 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2202 | /* |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 2203 | * 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2210 | */ |
Boqun Feng | 61775ed | 2020-08-07 15:42:27 +0800 | [diff] [blame] | 2211 | static inline bool usage_match(struct lock_list *entry, void *mask) |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2212 | { |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 2213 | 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 Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2217 | } |
| 2218 | |
Boqun Feng | 5f29624 | 2020-12-10 11:15:00 +0100 | [diff] [blame] | 2219 | static 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2257 | /* |
| 2258 | * Find a node in the forwards-direction dependency sub-graph starting |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2259 | * at @root->class that matches @bit. |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2260 | * |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2261 | * Return BFS_MATCH if such a node exists in the subgraph, and put that node |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2262 | * into *@target_entry. |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2263 | */ |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2264 | static enum bfs_result |
Frederic Weisbecker | 627f364 | 2019-04-02 18:02:43 +0200 | [diff] [blame] | 2265 | find_usage_forwards(struct lock_list *root, unsigned long usage_mask, |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2266 | struct lock_list **target_entry) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2267 | { |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2268 | enum bfs_result result; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2269 | |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 2270 | debug_atomic_inc(nr_find_usage_forwards_checks); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2271 | |
Boqun Feng | 5f29624 | 2020-12-10 11:15:00 +0100 | [diff] [blame] | 2272 | result = __bfs_forwards(root, &usage_mask, usage_match, usage_skip, target_entry); |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2273 | |
| 2274 | return result; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | /* |
| 2278 | * Find a node in the backwards-direction dependency sub-graph starting |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2279 | * at @root->class that matches @bit. |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2280 | */ |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2281 | static enum bfs_result |
Frederic Weisbecker | 627f364 | 2019-04-02 18:02:43 +0200 | [diff] [blame] | 2282 | find_usage_backwards(struct lock_list *root, unsigned long usage_mask, |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2283 | struct lock_list **target_entry) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2284 | { |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2285 | enum bfs_result result; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2286 | |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 2287 | debug_atomic_inc(nr_find_usage_backwards_checks); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2288 | |
Boqun Feng | 5f29624 | 2020-12-10 11:15:00 +0100 | [diff] [blame] | 2289 | result = __bfs_backwards(root, &usage_mask, usage_match, usage_skip, target_entry); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2290 | |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2291 | return result; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2292 | } |
| 2293 | |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2294 | static 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 Long | 8ca2b56c | 2018-10-03 13:07:18 -0400 | [diff] [blame] | 2300 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 2301 | printk(KERN_CONT " ops: %lu", debug_class_ops_read(class)); |
| 2302 | #endif |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2303 | printk(KERN_CONT " {\n"); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2304 | |
Peter Zijlstra | 2bb8945 | 2020-09-30 11:49:37 +0200 | [diff] [blame] | 2305 | for (bit = 0; bit < LOCK_TRACE_STATES; bit++) { |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2306 | if (class->usage_mask & (1 << bit)) { |
| 2307 | int len = depth; |
| 2308 | |
| 2309 | len += printk("%*s %s", depth, "", usage_str[bit]); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2310 | len += printk(KERN_CONT " at:\n"); |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 2311 | print_lock_trace(class->usage_traces[bit], len); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2312 | } |
| 2313 | } |
| 2314 | printk("%*s }\n", depth, ""); |
| 2315 | |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 2316 | printk("%*s ... key at: [<%px>] %pS\n", |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2317 | depth, "", class->key, class->key); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | /* |
Boqun Feng | 69c7a5f | 2021-06-19 01:01:07 +0800 | [diff] [blame] | 2321 | * 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 Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2371 | */ |
| 2372 | static void __used |
| 2373 | print_shortest_lock_dependencies(struct lock_list *leaf, |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2374 | struct lock_list *root) |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2375 | { |
| 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 Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 2385 | print_lock_trace(entry->trace, 2); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2386 | printk("\n"); |
| 2387 | |
| 2388 | if (depth == 0 && (entry != root)) { |
Steven Rostedt | 6be8c39 | 2011-04-20 21:41:58 -0400 | [diff] [blame] | 2389 | printk("lockdep:%s bad path found in chain graph\n", __func__); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2390 | break; |
| 2391 | } |
| 2392 | |
| 2393 | entry = get_lock_parent(entry); |
| 2394 | depth--; |
| 2395 | } while (entry && (depth >= 0)); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2396 | } |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2397 | |
Boqun Feng | 69c7a5f | 2021-06-19 01:01:07 +0800 | [diff] [blame] | 2398 | /* |
| 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 | */ |
| 2418 | static void __used |
| 2419 | print_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 Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 2453 | static void |
| 2454 | print_irq_lock_scenario(struct lock_list *safe_entry, |
| 2455 | struct lock_list *unsafe_entry, |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 2456 | struct lock_class *prev_class, |
| 2457 | struct lock_class *next_class) |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 2458 | { |
| 2459 | struct lock_class *safe_class = safe_entry->class; |
| 2460 | struct lock_class *unsafe_class = unsafe_entry->class; |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 2461 | struct lock_class *middle_class = prev_class; |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 2462 | |
| 2463 | if (middle_class == safe_class) |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 2464 | middle_class = next_class; |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 2465 | |
| 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 Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2482 | printk(KERN_CONT " --> "); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 2483 | __print_lock_name(middle_class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2484 | printk(KERN_CONT " --> "); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 2485 | __print_lock_name(unsafe_class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2486 | printk(KERN_CONT "\n\n"); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 2487 | } |
| 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 Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2494 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 2495 | printk(" local_irq_disable();\n"); |
| 2496 | printk(" lock("); |
| 2497 | __print_lock_name(safe_class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2498 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 2499 | printk(" lock("); |
| 2500 | __print_lock_name(middle_class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2501 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 2502 | printk(" <Interrupt>\n"); |
| 2503 | printk(" lock("); |
| 2504 | __print_lock_name(safe_class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2505 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 2506 | printk("\n *** DEADLOCK ***\n\n"); |
| 2507 | } |
| 2508 | |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2509 | static void |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2510 | print_bad_irq_dependency(struct task_struct *curr, |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2511 | struct lock_list *prev_root, |
| 2512 | struct lock_list *next_root, |
| 2513 | struct lock_list *backwards_entry, |
| 2514 | struct lock_list *forwards_entry, |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2515 | 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 Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2522 | return; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2523 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2524 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 2525 | pr_warn("=====================================================\n"); |
| 2526 | pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n", |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2527 | irqclass, irqclass); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 2528 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 2529 | pr_warn("-----------------------------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2530 | pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 2531 | curr->comm, task_pid_nr(curr), |
Peter Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 2532 | lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT, |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2533 | curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT, |
Peter Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 2534 | lockdep_hardirqs_enabled(), |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2535 | curr->softirqs_enabled); |
| 2536 | print_lock(next); |
| 2537 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2538 | pr_warn("\nand this task is already holding:\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2539 | print_lock(prev); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2540 | pr_warn("which would create a new lock dependency:\n"); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2541 | print_lock_name(hlock_class(prev)); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2542 | pr_cont(" ->"); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2543 | print_lock_name(hlock_class(next)); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2544 | pr_cont("\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2545 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2546 | pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n", |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2547 | irqclass); |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2548 | print_lock_name(backwards_entry->class); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2549 | pr_warn("\n... which became %s-irq-safe at:\n", irqclass); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2550 | |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 2551 | print_lock_trace(backwards_entry->class->usage_traces[bit1], 1); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2552 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2553 | pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass); |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2554 | print_lock_name(forwards_entry->class); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2555 | pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass); |
| 2556 | pr_warn("..."); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2557 | |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 2558 | print_lock_trace(forwards_entry->class->usage_traces[bit2], 1); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2559 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2560 | pr_warn("\nother info that might help us debug this:\n\n"); |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 2561 | print_irq_lock_scenario(backwards_entry, forwards_entry, |
| 2562 | hlock_class(prev), hlock_class(next)); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 2563 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2564 | lockdep_print_held_locks(curr); |
| 2565 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2566 | pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass); |
Boqun Feng | 69c7a5f | 2021-06-19 01:01:07 +0800 | [diff] [blame] | 2567 | print_shortest_lock_dependencies_backwards(backwards_entry, prev_root); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2568 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2569 | pr_warn("\nthe dependencies between the lock to be acquired"); |
| 2570 | pr_warn(" and %s-irq-unsafe lock:\n", irqclass); |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 2571 | next_root->trace = save_trace(); |
| 2572 | if (!next_root->trace) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2573 | return; |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2574 | print_shortest_lock_dependencies(forwards_entry, next_root); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2575 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2576 | pr_warn("\nstack backtrace:\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2577 | dump_stack(); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2578 | } |
| 2579 | |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 2580 | static const char *state_names[] = { |
| 2581 | #define LOCKDEP_STATE(__STATE) \ |
Peter Zijlstra | b4b136f | 2009-01-29 14:50:36 +0100 | [diff] [blame] | 2582 | __stringify(__STATE), |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 2583 | #include "lockdep_states.h" |
| 2584 | #undef LOCKDEP_STATE |
| 2585 | }; |
| 2586 | |
| 2587 | static const char *state_rnames[] = { |
| 2588 | #define LOCKDEP_STATE(__STATE) \ |
Peter Zijlstra | b4b136f | 2009-01-29 14:50:36 +0100 | [diff] [blame] | 2589 | __stringify(__STATE)"-READ", |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 2590 | #include "lockdep_states.h" |
| 2591 | #undef LOCKDEP_STATE |
| 2592 | }; |
| 2593 | |
| 2594 | static inline const char *state_name(enum lock_usage_bit bit) |
| 2595 | { |
Frederic Weisbecker | c902a1e | 2019-04-02 18:02:42 +0200 | [diff] [blame] | 2596 | 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 Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 2600 | } |
| 2601 | |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2602 | /* |
| 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 Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 2609 | static int exclusive_bit(int new_bit) |
| 2610 | { |
Frederic Weisbecker | bba2a8f | 2018-12-28 06:02:01 +0100 | [diff] [blame] | 2611 | int state = new_bit & LOCK_USAGE_STATE_MASK; |
| 2612 | int dir = new_bit & LOCK_USAGE_DIR_MASK; |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 2613 | |
| 2614 | /* |
| 2615 | * keep state, bit flip the direction and strip read. |
| 2616 | */ |
Frederic Weisbecker | bba2a8f | 2018-12-28 06:02:01 +0100 | [diff] [blame] | 2617 | return state | (dir ^ LOCK_USAGE_DIR_MASK); |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 2618 | } |
| 2619 | |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2620 | /* |
| 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 | */ |
| 2634 | static unsigned long invert_dir_mask(unsigned long mask) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2635 | { |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2636 | unsigned long excl = 0; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2637 | |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2638 | /* 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 Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 2641 | |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2642 | return excl; |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 2643 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2644 | |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2645 | /* |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 2646 | * 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 Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2672 | */ |
| 2673 | static unsigned long exclusive_mask(unsigned long mask) |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 2674 | { |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2675 | unsigned long excl = invert_dir_mask(mask); |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 2676 | |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2677 | excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK; |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 2678 | excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK; |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2679 | |
| 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 | */ |
| 2690 | static unsigned long original_mask(unsigned long mask) |
| 2691 | { |
| 2692 | unsigned long excl = invert_dir_mask(mask); |
| 2693 | |
| 2694 | /* Include read in existing usages */ |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 2695 | excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK; |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2696 | 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 | */ |
| 2705 | static 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 Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 2710 | int bit, excl, excl_read; |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2711 | |
| 2712 | for_each_set_bit(bit, &mask, LOCK_USED) { |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 2713 | /* |
| 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 Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2718 | excl = exclusive_bit(bit); |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 2719 | excl_read = excl | LOCK_USAGE_READ_MASK; |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2720 | if (excl_mask & lock_flag(excl)) { |
| 2721 | *bitp = bit; |
| 2722 | *excl_bitp = excl; |
| 2723 | return 0; |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 2724 | } else if (excl_mask & lock_flag(excl_read)) { |
| 2725 | *bitp = bit; |
| 2726 | *excl_bitp = excl_read; |
| 2727 | return 0; |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2728 | } |
| 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 | */ |
| 2739 | static 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 Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 2744 | struct lock_list *target_entry1; |
| 2745 | struct lock_list *target_entry; |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2746 | struct lock_list this, that; |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2747 | enum bfs_result ret; |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2748 | |
| 2749 | /* |
| 2750 | * Step 1: gather all hard/soft IRQs usages backward in an |
| 2751 | * accumulated usage mask. |
| 2752 | */ |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 2753 | bfs_init_rootb(&this, prev); |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2754 | |
Boqun Feng | 5f29624 | 2020-12-10 11:15:00 +0100 | [diff] [blame] | 2755 | ret = __bfs_backwards(&this, &usage_mask, usage_accumulate, usage_skip, NULL); |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2756 | if (bfs_error(ret)) { |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2757 | print_bfs_bug(ret); |
| 2758 | return 0; |
| 2759 | } |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2760 | |
| 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 Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 2771 | bfs_init_root(&that, next); |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2772 | |
| 2773 | ret = find_usage_forwards(&that, forward_mask, &target_entry1); |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2774 | if (bfs_error(ret)) { |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2775 | print_bfs_bug(ret); |
| 2776 | return 0; |
| 2777 | } |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2778 | if (ret == BFS_RNOMATCH) |
| 2779 | return 1; |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2780 | |
| 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 Feng | 7b1f8c61 | 2021-06-19 01:01:09 +0800 | [diff] [blame] | 2785 | * |
| 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 Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2795 | */ |
Boqun Feng | 7b1f8c61 | 2021-06-19 01:01:09 +0800 | [diff] [blame] | 2796 | backward_mask = original_mask(target_entry1->class->usage_mask & LOCKF_ENABLED_IRQ_ALL); |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2797 | |
| 2798 | ret = find_usage_backwards(&this, backward_mask, &target_entry); |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2799 | if (bfs_error(ret)) { |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2800 | print_bfs_bug(ret); |
| 2801 | return 0; |
| 2802 | } |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 2803 | if (DEBUG_LOCKS_WARN_ON(ret == BFS_RNOMATCH)) |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2804 | 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 Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2816 | 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2823 | } |
| 2824 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2825 | #else |
| 2826 | |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 2827 | static inline int check_irq_usage(struct task_struct *curr, |
| 2828 | struct held_lock *prev, struct held_lock *next) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2829 | { |
| 2830 | return 1; |
| 2831 | } |
Boqun Feng | 5f29624 | 2020-12-10 11:15:00 +0100 | [diff] [blame] | 2832 | |
| 2833 | static inline bool usage_skip(struct lock_list *entry, void *mask) |
| 2834 | { |
| 2835 | return false; |
| 2836 | } |
| 2837 | |
Waiman Long | b3b9c18 | 2020-02-06 10:24:03 -0500 | [diff] [blame] | 2838 | #endif /* CONFIG_TRACE_IRQFLAGS */ |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2839 | |
Peter Zijlstra | 175b1a6 | 2020-12-10 11:16:34 +0100 | [diff] [blame] | 2840 | #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 Song | 0e8a89d | 2021-06-18 21:02:30 +0800 | [diff] [blame] | 2846 | * Return BFS_RMATCH if it does, or BFS_RNOMATCH if it does not, return BFS_E* if |
Peter Zijlstra | 175b1a6 | 2020-12-10 11:16:34 +0100 | [diff] [blame] | 2847 | * any error appears in the bfs search. |
| 2848 | */ |
| 2849 | static noinline enum bfs_result |
| 2850 | check_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 Feng | 5f29624 | 2020-12-10 11:15:00 +0100 | [diff] [blame] | 2871 | /* |
| 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 Zijlstra | 175b1a6 | 2020-12-10 11:16:34 +0100 | [diff] [blame] | 2877 | |
| 2878 | if (ret == BFS_RMATCH) |
| 2879 | debug_atomic_inc(nr_redundant); |
| 2880 | |
| 2881 | return ret; |
| 2882 | } |
| 2883 | |
| 2884 | #else |
| 2885 | |
| 2886 | static inline enum bfs_result |
| 2887 | check_redundant(struct held_lock *src, struct held_lock *target) |
| 2888 | { |
| 2889 | return BFS_RNOMATCH; |
| 2890 | } |
| 2891 | |
| 2892 | #endif |
| 2893 | |
Waiman Long | b3b9c18 | 2020-02-06 10:24:03 -0500 | [diff] [blame] | 2894 | static void inc_chains(int irq_context) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2895 | { |
Waiman Long | b3b9c18 | 2020-02-06 10:24:03 -0500 | [diff] [blame] | 2896 | 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2902 | } |
| 2903 | |
Waiman Long | b3b9c18 | 2020-02-06 10:24:03 -0500 | [diff] [blame] | 2904 | static 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2913 | |
Steven Rostedt | 48702ec | 2011-04-20 21:41:56 -0400 | [diff] [blame] | 2914 | static void |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2915 | print_deadlock_scenario(struct held_lock *nxt, struct held_lock *prv) |
Steven Rostedt | 48702ec | 2011-04-20 21:41:56 -0400 | [diff] [blame] | 2916 | { |
| 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 Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2925 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 48702ec | 2011-04-20 21:41:56 -0400 | [diff] [blame] | 2926 | printk(" lock("); |
| 2927 | __print_lock_name(next); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2928 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 48702ec | 2011-04-20 21:41:56 -0400 | [diff] [blame] | 2929 | printk("\n *** DEADLOCK ***\n\n"); |
| 2930 | printk(" May be due to missing lock nesting notation\n\n"); |
| 2931 | } |
| 2932 | |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2933 | static void |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2934 | print_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 Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2938 | return; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2939 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2940 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 2941 | pr_warn("============================================\n"); |
| 2942 | pr_warn("WARNING: possible recursive locking detected\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 2943 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 2944 | pr_warn("--------------------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2945 | pr_warn("%s/%d is trying to acquire lock:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 2946 | curr->comm, task_pid_nr(curr)); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2947 | print_lock(next); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2948 | pr_warn("\nbut task is already holding lock:\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2949 | print_lock(prev); |
| 2950 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2951 | pr_warn("\nother info that might help us debug this:\n"); |
Steven Rostedt | 48702ec | 2011-04-20 21:41:56 -0400 | [diff] [blame] | 2952 | print_deadlock_scenario(next, prev); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2953 | lockdep_print_held_locks(curr); |
| 2954 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2955 | pr_warn("\nstack backtrace:\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2956 | dump_stack(); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2957 | } |
| 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 Feng | d61fc96a | 2020-11-02 13:37:41 +0800 | [diff] [blame] | 2965 | * 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2968 | */ |
| 2969 | static int |
Yuyang Du | 4609c4f | 2019-05-06 16:19:33 +0800 | [diff] [blame] | 2970 | check_deadlock(struct task_struct *curr, struct held_lock *next) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2971 | { |
| 2972 | struct held_lock *prev; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 2973 | struct held_lock *nest = NULL; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2974 | int i; |
| 2975 | |
| 2976 | for (i = 0; i < curr->lockdep_depth; i++) { |
| 2977 | prev = curr->held_locks + i; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 2978 | |
| 2979 | if (prev->instance == next->nest_lock) |
| 2980 | nest = prev; |
| 2981 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2982 | if (hlock_class(prev) != hlock_class(next)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2983 | continue; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 2984 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2985 | /* |
| 2986 | * Allow read-after-read recursion of the same |
| 2987 | * lock class (i.e. read_lock(lock)+read_lock(lock)): |
| 2988 | */ |
Yuyang Du | 4609c4f | 2019-05-06 16:19:33 +0800 | [diff] [blame] | 2989 | if ((next->read == 2) && prev->read) |
Boqun Feng | d61fc96a | 2020-11-02 13:37:41 +0800 | [diff] [blame] | 2990 | continue; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 2991 | |
| 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 Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 2999 | print_deadlock_bug(curr, prev, next); |
| 3000 | return 0; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3001 | } |
| 3002 | return 1; |
| 3003 | } |
| 3004 | |
| 3005 | /* |
| 3006 | * There was a chain-cache miss, and we are about to add a new dependency |
Yuyang Du | 154f185 | 2019-05-06 16:19:31 +0800 | [diff] [blame] | 3007 | * to a previous lock. We validate the following rules: |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3008 | * |
| 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 | */ |
| 3027 | static int |
| 3028 | check_prev_add(struct task_struct *curr, struct held_lock *prev, |
Boqun Feng | bd76eca | 2020-08-07 15:42:24 +0800 | [diff] [blame] | 3029 | struct held_lock *next, u16 distance, |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 3030 | struct lock_trace **const trace) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3031 | { |
Peter Zijlstra | 8b405d5 | 2017-10-04 11:13:37 +0200 | [diff] [blame] | 3032 | struct lock_list *entry; |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 3033 | enum bfs_result ret; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3034 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 3035 | 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3053 | /* |
| 3054 | * Prove that the new <prev> -> <next> dependency would not |
| 3055 | * create a circular dependency in the graph. (We do this by |
Yuyang Du | 154f185 | 2019-05-06 16:19:31 +0800 | [diff] [blame] | 3056 | * a breadth-first search into the graph starting at <next>, |
| 3057 | * and check whether we can reach <prev>.) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3058 | * |
Yuyang Du | 154f185 | 2019-05-06 16:19:31 +0800 | [diff] [blame] | 3059 | * 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3062 | */ |
Yuyang Du | 8c2c2b4 | 2019-05-06 16:19:35 +0800 | [diff] [blame] | 3063 | ret = check_noncircular(next, prev, trace); |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 3064 | if (unlikely(bfs_error(ret) || ret == BFS_RMATCH)) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 3065 | return 0; |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 3066 | |
Frederic Weisbecker | 948f837 | 2019-04-02 18:02:44 +0200 | [diff] [blame] | 3067 | if (!check_irq_usage(curr, prev, next)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3068 | return 0; |
| 3069 | |
| 3070 | /* |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3071 | * 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 Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 3078 | list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) { |
| 3079 | if (entry->class == hlock_class(next)) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3080 | if (distance == 1) |
| 3081 | entry->distance = 1; |
Boqun Feng | 3454a36 | 2020-08-07 15:42:25 +0800 | [diff] [blame] | 3082 | 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3111 | } |
| 3112 | } |
| 3113 | |
Peter Zijlstra | ae81330 | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 3114 | /* |
| 3115 | * Is the <prev> -> <next> link redundant? |
| 3116 | */ |
Yuyang Du | 8c2c2b4 | 2019-05-06 16:19:35 +0800 | [diff] [blame] | 3117 | ret = check_redundant(prev, next); |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 3118 | if (bfs_error(ret)) |
| 3119 | return 0; |
| 3120 | else if (ret == BFS_RMATCH) |
| 3121 | return 2; |
Peter Zijlstra | ae81330 | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 3122 | |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 3123 | if (!*trace) { |
| 3124 | *trace = save_trace(); |
| 3125 | if (!*trace) |
| 3126 | return 0; |
| 3127 | } |
Yong Zhang | 4726f2a | 2010-05-04 14:16:48 +0800 | [diff] [blame] | 3128 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3129 | /* |
| 3130 | * Ok, all validations passed, add the new lock |
| 3131 | * to the previous lock's dependency list: |
| 3132 | */ |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 3133 | ret = add_lock_to_list(hlock_class(next), hlock_class(prev), |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 3134 | &hlock_class(prev)->locks_after, |
Boqun Feng | 3454a36 | 2020-08-07 15:42:25 +0800 | [diff] [blame] | 3135 | next->acquire_ip, distance, |
| 3136 | calc_dep(prev, next), |
| 3137 | *trace); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3138 | |
| 3139 | if (!ret) |
| 3140 | return 0; |
| 3141 | |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 3142 | ret = add_lock_to_list(hlock_class(prev), hlock_class(next), |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 3143 | &hlock_class(next)->locks_before, |
Boqun Feng | 3454a36 | 2020-08-07 15:42:25 +0800 | [diff] [blame] | 3144 | next->acquire_ip, distance, |
| 3145 | calc_depb(prev, next), |
| 3146 | *trace); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3147 | if (!ret) |
| 3148 | return 0; |
| 3149 | |
Byungchul Park | 70911fd | 2017-08-07 16:12:50 +0900 | [diff] [blame] | 3150 | return 2; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3151 | } |
| 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 | */ |
| 3159 | static int |
| 3160 | check_prevs_add(struct task_struct *curr, struct held_lock *next) |
| 3161 | { |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 3162 | struct lock_trace *trace = NULL; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3163 | 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 Feng | bd76eca | 2020-08-07 15:42:24 +0800 | [diff] [blame] | 3182 | u16 distance = curr->lockdep_depth - depth + 1; |
Oleg Nesterov | 1b5ff81 | 2014-01-20 19:20:10 +0100 | [diff] [blame] | 3183 | hlock = curr->held_locks + depth - 1; |
Byungchul Park | ce07a941 | 2017-08-07 16:12:51 +0900 | [diff] [blame] | 3184 | |
Boqun Feng | 621c9da | 2020-08-07 15:42:31 +0800 | [diff] [blame] | 3185 | if (hlock->check) { |
| 3186 | int ret = check_prev_add(curr, hlock, next, distance, &trace); |
Ingo Molnar | e966eae | 2017-12-12 12:31:16 +0100 | [diff] [blame] | 3187 | 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3198 | } |
Ingo Molnar | e966eae | 2017-12-12 12:31:16 +0100 | [diff] [blame] | 3199 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3200 | 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; |
| 3214 | out_bug: |
| 3215 | if (!debug_locks_off_graph_unlock()) |
| 3216 | return 0; |
| 3217 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3218 | /* |
| 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3223 | WARN_ON(1); |
| 3224 | |
| 3225 | return 0; |
| 3226 | } |
| 3227 | |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 3228 | struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS]; |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame] | 3229 | static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS); |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 3230 | static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS]; |
Waiman Long | 797b82eb | 2020-02-06 10:24:07 -0500 | [diff] [blame] | 3231 | unsigned long nr_zapped_lock_chains; |
Waiman Long | 810507f | 2020-02-06 10:24:08 -0500 | [diff] [blame] | 3232 | unsigned int nr_free_chain_hlocks; /* Free chain_hlocks in buckets */ |
| 3233 | unsigned int nr_lost_chain_hlocks; /* Lost chain_hlocks */ |
| 3234 | unsigned 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 | |
| 3257 | static int chain_block_buckets[MAX_CHAIN_BUCKETS]; |
| 3258 | |
| 3259 | static 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 | */ |
| 3278 | static 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 | */ |
| 3297 | static inline int chain_block_size(int offset) |
| 3298 | { |
| 3299 | return (chain_hlocks[offset + 2] << 16) | chain_hlocks[offset + 3]; |
| 3300 | } |
| 3301 | |
| 3302 | static 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 | |
| 3313 | static 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 | */ |
| 3368 | static 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 | |
| 3377 | static 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 | */ |
| 3393 | static 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 | |
| 3455 | static inline void free_chain_hlocks(int base, int size) |
| 3456 | { |
| 3457 | add_chain_block(base, max(size, 2)); |
| 3458 | } |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 3459 | |
| 3460 | struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i) |
| 3461 | { |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 3462 | 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, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 3466 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3467 | |
| 3468 | /* |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 3469 | * Returns the index of the first held_lock of the current chain |
| 3470 | */ |
| 3471 | static 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 Petkov | 5c8a010 | 2016-04-04 10:42:07 +0200 | [diff] [blame] | 3487 | #ifdef CONFIG_DEBUG_LOCKDEP |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 3488 | /* |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3489 | * Returns the next chain_key iteration |
| 3490 | */ |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 3491 | static u64 print_chain_key_iteration(u16 hlock_id, u64 chain_key) |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3492 | { |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 3493 | u64 new_chain_key = iterate_chain_key(chain_key, hlock_id); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3494 | |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 3495 | printk(" hlock_id:%d -> chain_key:%016Lx", |
| 3496 | (unsigned int)hlock_id, |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3497 | (unsigned long long)new_chain_key); |
| 3498 | return new_chain_key; |
| 3499 | } |
| 3500 | |
| 3501 | static void |
| 3502 | print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next) |
| 3503 | { |
| 3504 | struct held_lock *hlock; |
Yuyang Du | f6ec882 | 2019-05-06 16:19:24 +0800 | [diff] [blame] | 3505 | u64 chain_key = INITIAL_CHAIN_KEY; |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3506 | int depth = curr->lockdep_depth; |
Yuyang Du | 834494b | 2019-05-06 16:19:21 +0800 | [diff] [blame] | 3507 | int i = get_first_held_lock(curr, hlock_next); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3508 | |
Yuyang Du | 834494b | 2019-05-06 16:19:21 +0800 | [diff] [blame] | 3509 | printk("depth: %u (irq_context %u)\n", depth - i + 1, |
| 3510 | hlock_next->irq_context); |
| 3511 | for (; i < depth; i++) { |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3512 | hlock = curr->held_locks + i; |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 3513 | chain_key = print_chain_key_iteration(hlock_id(hlock), chain_key); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3514 | |
| 3515 | print_lock(hlock); |
| 3516 | } |
| 3517 | |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 3518 | print_chain_key_iteration(hlock_id(hlock_next), chain_key); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3519 | print_lock(hlock_next); |
| 3520 | } |
| 3521 | |
| 3522 | static void print_chain_keys_chain(struct lock_chain *chain) |
| 3523 | { |
| 3524 | int i; |
Yuyang Du | f6ec882 | 2019-05-06 16:19:24 +0800 | [diff] [blame] | 3525 | u64 chain_key = INITIAL_CHAIN_KEY; |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 3526 | u16 hlock_id; |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3527 | |
| 3528 | printk("depth: %u\n", chain->depth); |
| 3529 | for (i = 0; i < chain->depth; i++) { |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 3530 | hlock_id = chain_hlocks[chain->base + i]; |
| 3531 | chain_key = print_chain_key_iteration(hlock_id, chain_key); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3532 | |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 3533 | print_lock_name(lock_classes + chain_hlock_class_idx(hlock_id) - 1); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3534 | printk("\n"); |
| 3535 | } |
| 3536 | } |
| 3537 | |
| 3538 | static void print_collision(struct task_struct *curr, |
| 3539 | struct held_lock *hlock_next, |
| 3540 | struct lock_chain *chain) |
| 3541 | { |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3542 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 3543 | pr_warn("============================\n"); |
| 3544 | pr_warn("WARNING: chain_key collision\n"); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3545 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 3546 | pr_warn("----------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3547 | 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 Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3549 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3550 | pr_warn("Held locks:"); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3551 | print_chain_keys_held_locks(curr, hlock_next); |
| 3552 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3553 | pr_warn("Locks in cached chain:"); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3554 | print_chain_keys_chain(chain); |
| 3555 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3556 | pr_warn("\nstack backtrace:\n"); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3557 | dump_stack(); |
| 3558 | } |
Borislav Petkov | 5c8a010 | 2016-04-04 10:42:07 +0200 | [diff] [blame] | 3559 | #endif |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3560 | |
| 3561 | /* |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 3562 | * 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 | */ |
| 3567 | static 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 Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3576 | if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) { |
| 3577 | print_collision(curr, hlock, chain); |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 3578 | return 0; |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3579 | } |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 3580 | |
| 3581 | for (j = 0; j < chain->depth - 1; j++, i++) { |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 3582 | id = hlock_id(&curr->held_locks[i]); |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 3583 | |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3584 | if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) { |
| 3585 | print_collision(curr, hlock, chain); |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 3586 | return 0; |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 3587 | } |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 3588 | } |
| 3589 | #endif |
| 3590 | return 1; |
| 3591 | } |
| 3592 | |
| 3593 | /* |
Bart Van Assche | 2212684 | 2019-02-14 15:00:48 -0800 | [diff] [blame] | 3594 | * 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 | */ |
| 3597 | long lockdep_next_lockchain(long i) |
| 3598 | { |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame] | 3599 | 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 Assche | 2212684 | 2019-02-14 15:00:48 -0800 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | unsigned long lock_chain_count(void) |
| 3604 | { |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame] | 3605 | return bitmap_weight(lock_chains_in_use, ARRAY_SIZE(lock_chains)); |
| 3606 | } |
| 3607 | |
| 3608 | /* Must be called with the graph lock held. */ |
| 3609 | static 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 Assche | 2212684 | 2019-02-14 15:00:48 -0800 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | /* |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3621 | * 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3626 | */ |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3627 | static inline int add_chain_cache(struct task_struct *curr, |
| 3628 | struct held_lock *hlock, |
| 3629 | u64 chain_key) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3630 | { |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 3631 | struct hlist_head *hash_head = chainhashentry(chain_key); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3632 | struct lock_chain *chain; |
Steven Rostedt | e0944ee | 2011-04-20 21:42:00 -0400 | [diff] [blame] | 3633 | int i, j; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3634 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3635 | /* |
Bart Van Assche | 527af3e | 2019-02-14 15:00:49 -0800 | [diff] [blame] | 3636 | * The caller must hold the graph lock, ensure we've got IRQs |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3637 | * disabled to make this an IRQ-safe lock.. for recursion reasons |
| 3638 | * lockdep won't complain about its own locking errors. |
| 3639 | */ |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 3640 | if (lockdep_assert_locked()) |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 3641 | return 0; |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 3642 | |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame] | 3643 | chain = alloc_lock_chain(); |
| 3644 | if (!chain) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 3645 | if (!debug_locks_off_graph_unlock()) |
| 3646 | return 0; |
| 3647 | |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 3648 | print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!"); |
Peter Zijlstra | eedeeab | 2009-03-18 12:38:47 +0100 | [diff] [blame] | 3649 | dump_stack(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3650 | return 0; |
| 3651 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3652 | chain->chain_key = chain_key; |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 3653 | chain->irq_context = hlock->irq_context; |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 3654 | i = get_first_held_lock(curr, hlock); |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 3655 | chain->depth = curr->lockdep_depth + 1 - i; |
Peter Zijlstra | 75dd602 | 2016-03-30 11:36:59 +0200 | [diff] [blame] | 3656 | |
| 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 Long | 810507f | 2020-02-06 10:24:08 -0500 | [diff] [blame] | 3661 | j = alloc_chain_hlocks(chain->depth); |
| 3662 | if (j < 0) { |
Byungchul Park | f9af456 | 2017-01-13 11:42:04 +0900 | [diff] [blame] | 3663 | if (!debug_locks_off_graph_unlock()) |
Peter Zijlstra | 75dd602 | 2016-03-30 11:36:59 +0200 | [diff] [blame] | 3664 | return 0; |
| 3665 | |
| 3666 | print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!"); |
| 3667 | dump_stack(); |
| 3668 | return 0; |
| 3669 | } |
Peter Zijlstra | 75dd602 | 2016-03-30 11:36:59 +0200 | [diff] [blame] | 3670 | |
Waiman Long | 810507f | 2020-02-06 10:24:08 -0500 | [diff] [blame] | 3671 | chain->base = j; |
| 3672 | for (j = 0; j < chain->depth - 1; j++, i++) { |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 3673 | int lock_id = hlock_id(curr->held_locks + i); |
Waiman Long | 810507f | 2020-02-06 10:24:08 -0500 | [diff] [blame] | 3674 | |
| 3675 | chain_hlocks[chain->base + j] = lock_id; |
| 3676 | } |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 3677 | chain_hlocks[chain->base + j] = hlock_id(hlock); |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 3678 | hlist_add_head_rcu(&chain->entry, hash_head); |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 3679 | debug_atomic_inc(chain_lookup_misses); |
Waiman Long | b3b9c18 | 2020-02-06 10:24:03 -0500 | [diff] [blame] | 3680 | inc_chains(chain->irq_context); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3681 | |
| 3682 | return 1; |
| 3683 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3684 | |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3685 | /* |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 3686 | * Look up a dependency chain. Must be called with either the graph lock or |
| 3687 | * the RCU read lock held. |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3688 | */ |
| 3689 | static 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 Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3694 | hlist_for_each_entry_rcu(chain, hash_head, entry) { |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 3695 | if (READ_ONCE(chain->chain_key) == chain_key) { |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3696 | 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 | */ |
| 3709 | static 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) { |
| 3717 | cache_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 Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 3723 | "%016Lx tail class: [%px] %s\n", |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3724 | (unsigned long long)chain_key, |
| 3725 | class->key, class->name); |
| 3726 | } |
| 3727 | |
| 3728 | return 0; |
| 3729 | } |
| 3730 | |
| 3731 | if (very_verbose(class)) { |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 3732 | printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n", |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3733 | (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 Du | 0b9fc8e | 2019-05-06 16:19:26 +0800 | [diff] [blame] | 3754 | static int validate_chain(struct task_struct *curr, |
| 3755 | struct held_lock *hlock, |
| 3756 | int chain_head, u64 chain_key) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3757 | { |
| 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 Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3765 | * (If lookup_chain_cache_add() return with 1 it acquires |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3766 | * graph_lock for us) |
| 3767 | */ |
Oleg Nesterov | fb9edbe | 2014-01-20 19:20:06 +0100 | [diff] [blame] | 3768 | if (!hlock->trylock && hlock->check && |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3769 | lookup_chain_cache_add(curr, hlock, chain_key)) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3770 | /* |
| 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 Du | 31a490e | 2019-05-06 16:19:27 +0800 | [diff] [blame] | 3777 | * could lead back to the previous lock: |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3778 | * |
Yuyang Du | 31a490e | 2019-05-06 16:19:27 +0800 | [diff] [blame] | 3779 | * - 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3787 | */ |
Yuyang Du | 4609c4f | 2019-05-06 16:19:33 +0800 | [diff] [blame] | 3788 | int ret = check_deadlock(curr, hlock); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3789 | |
| 3790 | if (!ret) |
| 3791 | return 0; |
| 3792 | /* |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3793 | * Add dependency only if this lock is not the head |
Boqun Feng | d61fc96a | 2020-11-02 13:37:41 +0800 | [diff] [blame] | 3794 | * 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3799 | */ |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3800 | if (!chain_head && ret != 2) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3801 | if (!check_prevs_add(curr, hlock)) |
| 3802 | return 0; |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3803 | } |
| 3804 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3805 | graph_unlock(); |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3806 | } else { |
| 3807 | /* after lookup_chain_cache_add(): */ |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3808 | if (unlikely(!debug_locks)) |
| 3809 | return 0; |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 3810 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3811 | |
| 3812 | return 1; |
| 3813 | } |
| 3814 | #else |
| 3815 | static inline int validate_chain(struct task_struct *curr, |
Yuyang Du | 0b9fc8e | 2019-05-06 16:19:26 +0800 | [diff] [blame] | 3816 | struct held_lock *hlock, |
| 3817 | int chain_head, u64 chain_key) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3818 | { |
| 3819 | return 1; |
| 3820 | } |
Waiman Long | 810507f | 2020-02-06 10:24:08 -0500 | [diff] [blame] | 3821 | |
| 3822 | static void init_chain_block_buckets(void) { } |
Yuyang Du | e7a38f6 | 2019-05-06 16:19:20 +0800 | [diff] [blame] | 3823 | #endif /* CONFIG_PROVE_LOCKING */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3824 | |
| 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 Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 3829 | static void check_chain_key(struct task_struct *curr) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3830 | { |
| 3831 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 3832 | struct held_lock *hlock, *prev_hlock = NULL; |
Alfredo Alvarez Fernandez | 5f18ab5 | 2016-02-11 00:33:32 +0100 | [diff] [blame] | 3833 | unsigned int i; |
Yuyang Du | f6ec882 | 2019-05-06 16:19:24 +0800 | [diff] [blame] | 3834 | u64 chain_key = INITIAL_CHAIN_KEY; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3835 | |
| 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 Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3840 | /* |
| 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 Ven | 2df8b1d | 2008-07-30 12:43:11 -0700 | [diff] [blame] | 3844 | WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n", |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3845 | curr->lockdep_depth, i, |
| 3846 | (unsigned long long)chain_key, |
| 3847 | (unsigned long long)hlock->prev_chain_key); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3848 | return; |
| 3849 | } |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 3850 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3851 | /* |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 3852 | * hlock->class_idx can't go beyond MAX_LOCKDEP_KEYS, but is |
| 3853 | * it registered lock class index? |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3854 | */ |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 3855 | if (DEBUG_LOCKS_WARN_ON(!test_bit(hlock->class_idx, lock_classes_in_use))) |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 3856 | return; |
| 3857 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3858 | if (prev_hlock && (prev_hlock->irq_context != |
| 3859 | hlock->irq_context)) |
Yuyang Du | f6ec882 | 2019-05-06 16:19:24 +0800 | [diff] [blame] | 3860 | chain_key = INITIAL_CHAIN_KEY; |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 3861 | chain_key = iterate_chain_key(chain_key, hlock_id(hlock)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3862 | prev_hlock = hlock; |
| 3863 | } |
| 3864 | if (chain_key != curr->curr_chain_key) { |
| 3865 | debug_locks_off(); |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3866 | /* |
| 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 Ven | 2df8b1d | 2008-07-30 12:43:11 -0700 | [diff] [blame] | 3870 | WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n", |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3871 | curr->lockdep_depth, i, |
| 3872 | (unsigned long long)chain_key, |
| 3873 | (unsigned long long)curr->curr_chain_key); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3874 | } |
| 3875 | #endif |
| 3876 | } |
| 3877 | |
Arnd Bergmann | 30a35f7 | 2019-06-28 12:29:03 +0200 | [diff] [blame] | 3878 | #ifdef CONFIG_PROVE_LOCKING |
Frederic Weisbecker | 0d2cc3b | 2019-04-02 18:02:41 +0200 | [diff] [blame] | 3879 | static int mark_lock(struct task_struct *curr, struct held_lock *this, |
| 3880 | enum lock_usage_bit new_bit); |
| 3881 | |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 3882 | static void print_usage_bug_scenario(struct held_lock *lock) |
Steven Rostedt | 282b5c2 | 2011-04-20 21:41:59 -0400 | [diff] [blame] | 3883 | { |
| 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 Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 3891 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 282b5c2 | 2011-04-20 21:41:59 -0400 | [diff] [blame] | 3892 | printk(" <Interrupt>\n"); |
| 3893 | printk(" lock("); |
| 3894 | __print_lock_name(class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 3895 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 282b5c2 | 2011-04-20 21:41:59 -0400 | [diff] [blame] | 3896 | printk("\n *** DEADLOCK ***\n\n"); |
| 3897 | } |
| 3898 | |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 3899 | static void |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3900 | print_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 Zijlstra | 7f82e63 | 2021-02-01 11:55:38 +0100 | [diff] [blame] | 3903 | if (!debug_locks_off() || debug_locks_silent) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 3904 | return; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3905 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3906 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 3907 | pr_warn("================================\n"); |
| 3908 | pr_warn("WARNING: inconsistent lock state\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 3909 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 3910 | pr_warn("--------------------------------\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3911 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3912 | pr_warn("inconsistent {%s} -> {%s} usage.\n", |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3913 | usage_str[prev_bit], usage_str[new_bit]); |
| 3914 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3915 | pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 3916 | curr->comm, task_pid_nr(curr), |
Peter Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 3917 | lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT, |
Peter Zijlstra | ef99691 | 2020-03-20 12:56:42 +0100 | [diff] [blame] | 3918 | lockdep_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT, |
Peter Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 3919 | lockdep_hardirqs_enabled(), |
Peter Zijlstra | ef99691 | 2020-03-20 12:56:42 +0100 | [diff] [blame] | 3920 | lockdep_softirqs_enabled(curr)); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3921 | print_lock(this); |
| 3922 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3923 | pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]); |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 3924 | print_lock_trace(hlock_class(this)->usage_traces[prev_bit], 1); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3925 | |
| 3926 | print_irqtrace_events(curr); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3927 | pr_warn("\nother info that might help us debug this:\n"); |
Steven Rostedt | 282b5c2 | 2011-04-20 21:41:59 -0400 | [diff] [blame] | 3928 | print_usage_bug_scenario(this); |
| 3929 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3930 | lockdep_print_held_locks(curr); |
| 3931 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3932 | pr_warn("\nstack backtrace:\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3933 | dump_stack(); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | /* |
| 3937 | * Print out an error if an invalid bit is set: |
| 3938 | */ |
| 3939 | static inline int |
| 3940 | valid_state(struct task_struct *curr, struct held_lock *this, |
| 3941 | enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit) |
| 3942 | { |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 3943 | if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) { |
Peter Zijlstra | 7f82e63 | 2021-02-01 11:55:38 +0100 | [diff] [blame] | 3944 | graph_unlock(); |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 3945 | print_usage_bug(curr, this, bad_bit, new_bit); |
| 3946 | return 0; |
| 3947 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3948 | return 1; |
| 3949 | } |
| 3950 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3951 | |
| 3952 | /* |
| 3953 | * print irq inversion bug: |
| 3954 | */ |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 3955 | static void |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 3956 | print_irq_inversion_bug(struct task_struct *curr, |
| 3957 | struct lock_list *root, struct lock_list *other, |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3958 | struct held_lock *this, int forwards, |
| 3959 | const char *irqclass) |
| 3960 | { |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 3961 | struct lock_list *entry = other; |
| 3962 | struct lock_list *middle = NULL; |
| 3963 | int depth; |
| 3964 | |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 3965 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 3966 | return; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3967 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3968 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 3969 | pr_warn("========================================================\n"); |
| 3970 | pr_warn("WARNING: possible irq lock inversion dependency detected\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 3971 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 3972 | pr_warn("--------------------------------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3973 | pr_warn("%s/%d just changed the state of lock:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 3974 | curr->comm, task_pid_nr(curr)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3975 | print_lock(this); |
| 3976 | if (forwards) |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3977 | pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3978 | else |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3979 | pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass); |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 3980 | print_lock_name(other->class); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3981 | pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3982 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3983 | pr_warn("\nother info that might help us debug this:\n"); |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 3984 | |
| 3985 | /* Find a middle lock (if one exists) */ |
| 3986 | depth = get_lock_depth(other); |
| 3987 | do { |
| 3988 | if (depth == 0 && (entry != root)) { |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3989 | pr_warn("lockdep:%s bad path found in chain graph\n", __func__); |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 3990 | 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4003 | lockdep_print_held_locks(curr); |
| 4004 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4005 | pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n"); |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 4006 | root->trace = save_trace(); |
| 4007 | if (!root->trace) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 4008 | return; |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 4009 | print_shortest_lock_dependencies(other, root); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4010 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4011 | pr_warn("\nstack backtrace:\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4012 | dump_stack(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | /* |
| 4016 | * Prove that in the forwards-direction subgraph starting at <this> |
| 4017 | * there is no lock matching <mask>: |
| 4018 | */ |
| 4019 | static int |
| 4020 | check_usage_forwards(struct task_struct *curr, struct held_lock *this, |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 4021 | enum lock_usage_bit bit) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4022 | { |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 4023 | enum bfs_result ret; |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 4024 | struct lock_list root; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 4025 | struct lock_list *target_entry; |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 4026 | enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK; |
| 4027 | unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4028 | |
Boqun Feng | 6971c0f | 2020-08-07 15:42:26 +0800 | [diff] [blame] | 4029 | bfs_init_root(&root, this); |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 4030 | ret = find_usage_forwards(&root, usage_mask, &target_entry); |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 4031 | if (bfs_error(ret)) { |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 4032 | print_bfs_bug(ret); |
| 4033 | return 0; |
| 4034 | } |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 4035 | if (ret == BFS_RNOMATCH) |
| 4036 | return 1; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4037 | |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 4038 | /* 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 Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 4047 | return 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | /* |
| 4051 | * Prove that in the backwards-direction subgraph starting at <this> |
| 4052 | * there is no lock matching <mask>: |
| 4053 | */ |
| 4054 | static int |
| 4055 | check_usage_backwards(struct task_struct *curr, struct held_lock *this, |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 4056 | enum lock_usage_bit bit) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4057 | { |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 4058 | enum bfs_result ret; |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 4059 | struct lock_list root; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 4060 | struct lock_list *target_entry; |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 4061 | enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK; |
| 4062 | unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4063 | |
Boqun Feng | 6971c0f | 2020-08-07 15:42:26 +0800 | [diff] [blame] | 4064 | bfs_init_rootb(&root, this); |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 4065 | ret = find_usage_backwards(&root, usage_mask, &target_entry); |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 4066 | if (bfs_error(ret)) { |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 4067 | print_bfs_bug(ret); |
| 4068 | return 0; |
| 4069 | } |
Boqun Feng | b11be02 | 2020-08-07 15:42:22 +0800 | [diff] [blame] | 4070 | if (ret == BFS_RNOMATCH) |
| 4071 | return 1; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4072 | |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 4073 | /* 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 Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 4082 | return 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4083 | } |
| 4084 | |
Ingo Molnar | 3117df0 | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 4085 | void print_irqtrace_events(struct task_struct *curr) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4086 | { |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4087 | const struct irqtrace_events *trace = &curr->irqtrace; |
| 4088 | |
| 4089 | printk("irq event stamp: %u\n", trace->irq_events); |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 4090 | printk("hardirqs last enabled at (%u): [<%px>] %pS\n", |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4091 | trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip, |
| 4092 | (void *)trace->hardirq_enable_ip); |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 4093 | printk("hardirqs last disabled at (%u): [<%px>] %pS\n", |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4094 | trace->hardirq_disable_event, (void *)trace->hardirq_disable_ip, |
| 4095 | (void *)trace->hardirq_disable_ip); |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 4096 | printk("softirqs last enabled at (%u): [<%px>] %pS\n", |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4097 | trace->softirq_enable_event, (void *)trace->softirq_enable_ip, |
| 4098 | (void *)trace->softirq_enable_ip); |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 4099 | printk("softirqs last disabled at (%u): [<%px>] %pS\n", |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4100 | trace->softirq_disable_event, (void *)trace->softirq_disable_ip, |
| 4101 | (void *)trace->softirq_disable_ip); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4102 | } |
| 4103 | |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 4104 | static int HARDIRQ_verbose(struct lock_class *class) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4105 | { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4106 | #if HARDIRQ_VERBOSE |
| 4107 | return class_filter(class); |
| 4108 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4109 | return 0; |
| 4110 | } |
| 4111 | |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 4112 | static int SOFTIRQ_verbose(struct lock_class *class) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4113 | { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4114 | #if SOFTIRQ_VERBOSE |
| 4115 | return class_filter(class); |
| 4116 | #endif |
| 4117 | return 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4118 | } |
| 4119 | |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 4120 | static 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 | |
| 4127 | static inline int state_verbose(enum lock_usage_bit bit, |
| 4128 | struct lock_class *class) |
| 4129 | { |
Frederic Weisbecker | c902a1e | 2019-04-02 18:02:42 +0200 | [diff] [blame] | 4130 | return state_verbose_f[bit >> LOCK_USAGE_DIR_MASK](class); |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 4131 | } |
| 4132 | |
Peter Zijlstra | 42c50d5 | 2009-01-22 16:58:16 +0100 | [diff] [blame] | 4133 | typedef int (*check_usage_f)(struct task_struct *, struct held_lock *, |
| 4134 | enum lock_usage_bit bit, const char *name); |
| 4135 | |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 4136 | static int |
Peter Zijlstra | 1c21f14 | 2009-03-04 13:51:13 +0100 | [diff] [blame] | 4137 | mark_lock_irq(struct task_struct *curr, struct held_lock *this, |
| 4138 | enum lock_usage_bit new_bit) |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 4139 | { |
Peter Zijlstra | f989209 | 2009-01-22 16:09:59 +0100 | [diff] [blame] | 4140 | int excl_bit = exclusive_bit(new_bit); |
Frederic Weisbecker | bba2a8f | 2018-12-28 06:02:01 +0100 | [diff] [blame] | 4141 | int read = new_bit & LOCK_USAGE_READ_MASK; |
| 4142 | int dir = new_bit & LOCK_USAGE_DIR_MASK; |
Peter Zijlstra | 42c50d5 | 2009-01-22 16:58:16 +0100 | [diff] [blame] | 4143 | |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 4144 | /* |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 4145 | * Validate that this particular lock does not have conflicting |
| 4146 | * usage states. |
| 4147 | */ |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 4148 | if (!valid_state(curr, this, new_bit, excl_bit)) |
| 4149 | return 0; |
Peter Zijlstra | 9d3651a | 2009-01-22 17:18:32 +0100 | [diff] [blame] | 4150 | |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 4151 | /* |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 4152 | * 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 Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 4160 | * Validate that the lock dependencies don't have conflicting usage |
| 4161 | * states. |
| 4162 | */ |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 4163 | 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 Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 4169 | return 0; |
Boqun Feng | f08e388 | 2020-08-07 15:42:30 +0800 | [diff] [blame] | 4170 | } 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 Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 4176 | return 0; |
| 4177 | } |
Peter Zijlstra | 780e820 | 2009-01-22 16:51:29 +0100 | [diff] [blame] | 4178 | |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 4179 | if (state_verbose(new_bit, hlock_class(this))) |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 4180 | return 2; |
| 4181 | |
| 4182 | return 1; |
| 4183 | } |
| 4184 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4185 | /* |
| 4186 | * Mark all held locks with a usage bit: |
| 4187 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 4188 | static int |
Frederic Weisbecker | 436a49a | 2018-12-28 06:02:00 +0100 | [diff] [blame] | 4189 | mark_held_locks(struct task_struct *curr, enum lock_usage_bit base_bit) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4190 | { |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4191 | struct held_lock *hlock; |
| 4192 | int i; |
| 4193 | |
| 4194 | for (i = 0; i < curr->lockdep_depth; i++) { |
Frederic Weisbecker | 436a49a | 2018-12-28 06:02:00 +0100 | [diff] [blame] | 4195 | enum lock_usage_bit hlock_bit = base_bit; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4196 | hlock = curr->held_locks + i; |
| 4197 | |
Peter Zijlstra | cf2ad4d | 2009-01-27 13:58:08 +0100 | [diff] [blame] | 4198 | if (hlock->read) |
Frederic Weisbecker | bba2a8f | 2018-12-28 06:02:01 +0100 | [diff] [blame] | 4199 | hlock_bit += LOCK_USAGE_READ_MASK; |
Peter Zijlstra | cf2ad4d | 2009-01-27 13:58:08 +0100 | [diff] [blame] | 4200 | |
Frederic Weisbecker | 436a49a | 2018-12-28 06:02:00 +0100 | [diff] [blame] | 4201 | BUG_ON(hlock_bit >= LOCK_USAGE_STATES); |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 4202 | |
Oleg Nesterov | 34d0ed5 | 2014-01-20 19:20:13 +0100 | [diff] [blame] | 4203 | if (!hlock->check) |
Peter Zijlstra | efbe2ee | 2011-07-07 11:39:45 +0200 | [diff] [blame] | 4204 | continue; |
| 4205 | |
Frederic Weisbecker | 436a49a | 2018-12-28 06:02:00 +0100 | [diff] [blame] | 4206 | if (!mark_lock(curr, hlock, hlock_bit)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4207 | return 0; |
| 4208 | } |
| 4209 | |
| 4210 | return 1; |
| 4211 | } |
| 4212 | |
| 4213 | /* |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4214 | * Hardirqs will be enabled: |
| 4215 | */ |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4216 | static void __trace_hardirqs_on_caller(void) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4217 | { |
| 4218 | struct task_struct *curr = current; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4219 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4220 | /* |
| 4221 | * We are going to turn hardirqs on, so set the |
| 4222 | * usage bit for all held locks: |
| 4223 | */ |
Frederic Weisbecker | 436a49a | 2018-12-28 06:02:00 +0100 | [diff] [blame] | 4224 | if (!mark_held_locks(curr, LOCK_ENABLED_HARDIRQ)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4225 | 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 Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4232 | mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4233 | } |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 4234 | |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4235 | /** |
| 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 | */ |
| 4244 | void lockdep_hardirqs_on_prepare(unsigned long ip) |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 4245 | { |
Peter Zijlstra | 859d069 | 2020-05-27 15:00:57 +0200 | [diff] [blame] | 4246 | if (unlikely(!debug_locks)) |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 4247 | return; |
| 4248 | |
Peter Zijlstra | 859d069 | 2020-05-27 15:00:57 +0200 | [diff] [blame] | 4249 | /* |
| 4250 | * NMIs do not (and cannot) track lock dependencies, nothing to do. |
| 4251 | */ |
| 4252 | if (unlikely(in_nmi())) |
| 4253 | return; |
| 4254 | |
Peter Zijlstra | f8e48a3 | 2020-10-22 12:23:02 +0200 | [diff] [blame] | 4255 | if (unlikely(this_cpu_read(lockdep_recursion))) |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 4256 | return; |
| 4257 | |
Peter Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 4258 | if (unlikely(lockdep_hardirqs_enabled())) { |
Peter Zijlstra | 7d36b26 | 2011-07-26 13:13:44 +0200 | [diff] [blame] | 4259 | /* |
| 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 Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4268 | /* |
| 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 Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 4273 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 4274 | return; |
| 4275 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4276 | /* |
| 4277 | * See the fine text that goes along with this variable definition. |
| 4278 | */ |
zhengbin | d671002 | 2019-04-29 20:26:31 +0800 | [diff] [blame] | 4279 | if (DEBUG_LOCKS_WARN_ON(early_boot_irqs_disabled)) |
Peter Zijlstra | 7d36b26 | 2011-07-26 13:13:44 +0200 | [diff] [blame] | 4280 | return; |
| 4281 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4282 | /* |
| 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 Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 4286 | if (DEBUG_LOCKS_WARN_ON(lockdep_hardirq_context())) |
Peter Zijlstra | 7d36b26 | 2011-07-26 13:13:44 +0200 | [diff] [blame] | 4287 | return; |
| 4288 | |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4289 | current->hardirq_chain_key = current->curr_chain_key; |
| 4290 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 4291 | lockdep_recursion_inc(); |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4292 | __trace_hardirqs_on_caller(); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 4293 | lockdep_recursion_finish(); |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 4294 | } |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4295 | EXPORT_SYMBOL_GPL(lockdep_hardirqs_on_prepare); |
| 4296 | |
| 4297 | void noinstr lockdep_hardirqs_on(unsigned long ip) |
| 4298 | { |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4299 | struct irqtrace_events *trace = ¤t->irqtrace; |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4300 | |
Peter Zijlstra | 859d069 | 2020-05-27 15:00:57 +0200 | [diff] [blame] | 4301 | if (unlikely(!debug_locks)) |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4302 | return; |
| 4303 | |
Peter Zijlstra | 859d069 | 2020-05-27 15:00:57 +0200 | [diff] [blame] | 4304 | /* |
| 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.org | ed00495 | 2020-07-27 14:48:52 +0200 | [diff] [blame] | 4312 | if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI)) |
| 4313 | return; |
| 4314 | |
Peter Zijlstra | 859d069 | 2020-05-27 15:00:57 +0200 | [diff] [blame] | 4315 | /* |
| 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 Zijlstra | f8e48a3 | 2020-10-22 12:23:02 +0200 | [diff] [blame] | 4324 | if (unlikely(this_cpu_read(lockdep_recursion))) |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4325 | return; |
| 4326 | |
Peter Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 4327 | if (lockdep_hardirqs_enabled()) { |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4328 | /* |
| 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 Zijlstra | 859d069 | 2020-05-27 15:00:57 +0200 | [diff] [blame] | 4352 | skip_checks: |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4353 | /* we'll do an OFF -> ON transition: */ |
Peter Zijlstra | fddf905 | 2020-08-20 09:13:30 +0200 | [diff] [blame] | 4354 | __this_cpu_write(hardirqs_enabled, 1); |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4355 | trace->hardirq_enable_ip = ip; |
| 4356 | trace->hardirq_enable_event = ++trace->irq_events; |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4357 | debug_atomic_inc(hardirqs_on_events); |
| 4358 | } |
| 4359 | EXPORT_SYMBOL_GPL(lockdep_hardirqs_on); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4360 | |
| 4361 | /* |
| 4362 | * Hardirqs were disabled: |
| 4363 | */ |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4364 | void noinstr lockdep_hardirqs_off(unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4365 | { |
Peter Zijlstra | 859d069 | 2020-05-27 15:00:57 +0200 | [diff] [blame] | 4366 | if (unlikely(!debug_locks)) |
| 4367 | return; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4368 | |
Peter Zijlstra | 859d069 | 2020-05-27 15:00:57 +0200 | [diff] [blame] | 4369 | /* |
| 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.org | ed00495 | 2020-07-27 14:48:52 +0200 | [diff] [blame] | 4374 | if (in_nmi()) { |
| 4375 | if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI)) |
| 4376 | return; |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 4377 | } else if (__this_cpu_read(lockdep_recursion)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4378 | return; |
| 4379 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4380 | /* |
| 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4384 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 4385 | return; |
| 4386 | |
Peter Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 4387 | if (lockdep_hardirqs_enabled()) { |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4388 | struct irqtrace_events *trace = ¤t->irqtrace; |
| 4389 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4390 | /* |
| 4391 | * We have done an ON -> OFF transition: |
| 4392 | */ |
Peter Zijlstra | fddf905 | 2020-08-20 09:13:30 +0200 | [diff] [blame] | 4393 | __this_cpu_write(hardirqs_enabled, 0); |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4394 | trace->hardirq_disable_ip = ip; |
| 4395 | trace->hardirq_disable_event = ++trace->irq_events; |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 4396 | debug_atomic_inc(hardirqs_off_events); |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4397 | } else { |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 4398 | debug_atomic_inc(redundant_hardirqs_off); |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4399 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4400 | } |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 4401 | EXPORT_SYMBOL_GPL(lockdep_hardirqs_off); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4402 | |
| 4403 | /* |
| 4404 | * Softirqs will be enabled: |
| 4405 | */ |
Peter Zijlstra | 0d38453 | 2020-03-20 12:56:41 +0100 | [diff] [blame] | 4406 | void lockdep_softirqs_on(unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4407 | { |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4408 | struct irqtrace_events *trace = ¤t->irqtrace; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4409 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 4410 | if (unlikely(!lockdep_enabled())) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4411 | return; |
| 4412 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4413 | /* |
| 4414 | * We fancy IRQs being disabled here, see softirq.c, avoids |
| 4415 | * funny state and nesting things. |
| 4416 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4417 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 4418 | return; |
| 4419 | |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4420 | if (current->softirqs_enabled) { |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 4421 | debug_atomic_inc(redundant_softirqs_on); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4422 | return; |
| 4423 | } |
| 4424 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 4425 | lockdep_recursion_inc(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4426 | /* |
| 4427 | * We'll do an OFF -> ON transition: |
| 4428 | */ |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4429 | current->softirqs_enabled = 1; |
| 4430 | trace->softirq_enable_ip = ip; |
| 4431 | trace->softirq_enable_event = ++trace->irq_events; |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 4432 | debug_atomic_inc(softirqs_on_events); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4433 | /* |
| 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 Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 4438 | if (lockdep_hardirqs_enabled()) |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4439 | mark_held_locks(current, LOCK_ENABLED_SOFTIRQ); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 4440 | lockdep_recursion_finish(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4441 | } |
| 4442 | |
| 4443 | /* |
| 4444 | * Softirqs were disabled: |
| 4445 | */ |
Peter Zijlstra | 0d38453 | 2020-03-20 12:56:41 +0100 | [diff] [blame] | 4446 | void lockdep_softirqs_off(unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4447 | { |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 4448 | if (unlikely(!lockdep_enabled())) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4449 | return; |
| 4450 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4451 | /* |
| 4452 | * We fancy IRQs being disabled here, see softirq.c |
| 4453 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4454 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 4455 | return; |
| 4456 | |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4457 | if (current->softirqs_enabled) { |
| 4458 | struct irqtrace_events *trace = ¤t->irqtrace; |
| 4459 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4460 | /* |
| 4461 | * We have done an ON -> OFF transition: |
| 4462 | */ |
Marco Elver | 0584df9 | 2020-07-29 13:09:15 +0200 | [diff] [blame] | 4463 | current->softirqs_enabled = 0; |
| 4464 | trace->softirq_disable_ip = ip; |
| 4465 | trace->softirq_disable_event = ++trace->irq_events; |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 4466 | debug_atomic_inc(softirqs_off_events); |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4467 | /* |
| 4468 | * Whoops, we wanted softirqs off, so why aren't they? |
| 4469 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4470 | DEBUG_LOCKS_WARN_ON(!softirq_count()); |
| 4471 | } else |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 4472 | debug_atomic_inc(redundant_softirqs_off); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4473 | } |
| 4474 | |
Yuyang Du | 0918065 | 2019-05-06 16:19:37 +0800 | [diff] [blame] | 4475 | static int |
| 4476 | mark_usage(struct task_struct *curr, struct held_lock *hlock, int check) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4477 | { |
Yuyang Du | 0918065 | 2019-05-06 16:19:37 +0800 | [diff] [blame] | 4478 | if (!check) |
| 4479 | goto lock_used; |
| 4480 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4481 | /* |
| 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 Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 4487 | if (lockdep_hardirq_context()) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4488 | 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 Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 4496 | if (lockdep_hardirq_context()) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4497 | 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 Zijlstra | 4fc95e8 | 2009-01-22 13:10:52 +0100 | [diff] [blame] | 4507 | LOCK_ENABLED_HARDIRQ_READ)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4508 | return 0; |
| 4509 | if (curr->softirqs_enabled) |
| 4510 | if (!mark_lock(curr, hlock, |
Peter Zijlstra | 4fc95e8 | 2009-01-22 13:10:52 +0100 | [diff] [blame] | 4511 | LOCK_ENABLED_SOFTIRQ_READ)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4512 | return 0; |
| 4513 | } else { |
| 4514 | if (!mark_lock(curr, hlock, |
Peter Zijlstra | 4fc95e8 | 2009-01-22 13:10:52 +0100 | [diff] [blame] | 4515 | LOCK_ENABLED_HARDIRQ)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4516 | return 0; |
| 4517 | if (curr->softirqs_enabled) |
| 4518 | if (!mark_lock(curr, hlock, |
Peter Zijlstra | 4fc95e8 | 2009-01-22 13:10:52 +0100 | [diff] [blame] | 4519 | LOCK_ENABLED_SOFTIRQ)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4520 | return 0; |
| 4521 | } |
| 4522 | } |
| 4523 | |
Yuyang Du | 0918065 | 2019-05-06 16:19:37 +0800 | [diff] [blame] | 4524 | lock_used: |
| 4525 | /* mark it as used: */ |
| 4526 | if (!mark_lock(curr, hlock, LOCK_USED)) |
| 4527 | return 0; |
| 4528 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4529 | return 1; |
| 4530 | } |
| 4531 | |
Boqun Feng | c246975 | 2016-02-16 13:57:40 +0800 | [diff] [blame] | 4532 | static inline unsigned int task_irq_context(struct task_struct *task) |
| 4533 | { |
Peter Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 4534 | return LOCK_CHAIN_HARDIRQ_CONTEXT * !!lockdep_hardirq_context() + |
Waiman Long | b3b9c18 | 2020-02-06 10:24:03 -0500 | [diff] [blame] | 4535 | LOCK_CHAIN_SOFTIRQ_CONTEXT * !!task->softirq_context; |
Boqun Feng | c246975 | 2016-02-16 13:57:40 +0800 | [diff] [blame] | 4536 | } |
| 4537 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4538 | static 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 Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4546 | 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4561 | /* |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4562 | * Mark a lock with a usage bit, and validate the state transition: |
| 4563 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 4564 | static int mark_lock(struct task_struct *curr, struct held_lock *this, |
Steven Rostedt | 0764d23 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 4565 | enum lock_usage_bit new_bit) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4566 | { |
Peter Zijlstra | 2bb8945 | 2020-09-30 11:49:37 +0200 | [diff] [blame] | 4567 | unsigned int new_mask, ret = 1; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4568 | |
Yuyang Du | 4d56330 | 2019-05-06 16:19:38 +0800 | [diff] [blame] | 4569 | if (new_bit >= LOCK_USAGE_STATES) { |
| 4570 | DEBUG_LOCKS_WARN_ON(1); |
| 4571 | return 0; |
| 4572 | } |
| 4573 | |
peterz@infradead.org | 23870f1 | 2020-09-02 18:03:23 +0200 | [diff] [blame] | 4574 | if (new_bit == LOCK_USED && this->read) |
| 4575 | new_bit = LOCK_USED_READ; |
| 4576 | |
| 4577 | new_mask = 1 << new_bit; |
| 4578 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4579 | /* |
| 4580 | * If already set then do not dirty the cacheline, |
| 4581 | * nor do any checks: |
| 4582 | */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 4583 | if (likely(hlock_class(this)->usage_mask & new_mask)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4584 | return 1; |
| 4585 | |
| 4586 | if (!graph_lock()) |
| 4587 | return 0; |
| 4588 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4589 | * Make sure we didn't race: |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4590 | */ |
peterz@infradead.org | 23870f1 | 2020-09-02 18:03:23 +0200 | [diff] [blame] | 4591 | if (unlikely(hlock_class(this)->usage_mask & new_mask)) |
| 4592 | goto unlock; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4593 | |
Peter Zijlstra | 1a39340 | 2020-10-27 13:48:34 +0100 | [diff] [blame] | 4594 | if (!hlock_class(this)->usage_mask) |
| 4595 | debug_atomic_dec(nr_unused_locks); |
| 4596 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 4597 | hlock_class(this)->usage_mask |= new_mask; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4598 | |
Peter Zijlstra | 2bb8945 | 2020-09-30 11:49:37 +0200 | [diff] [blame] | 4599 | if (new_bit < LOCK_TRACE_STATES) { |
| 4600 | if (!(hlock_class(this)->usage_traces[new_bit] = save_trace())) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4601 | return 0; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4602 | } |
| 4603 | |
Peter Zijlstra | 1a39340 | 2020-10-27 13:48:34 +0100 | [diff] [blame] | 4604 | if (new_bit < LOCK_USED) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4605 | ret = mark_lock_irq(curr, this, new_bit); |
| 4606 | if (!ret) |
| 4607 | return 0; |
| 4608 | } |
| 4609 | |
peterz@infradead.org | 23870f1 | 2020-09-02 18:03:23 +0200 | [diff] [blame] | 4610 | unlock: |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4611 | 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 Zijlstra | 9a019db | 2020-03-31 20:38:12 +0200 | [diff] [blame] | 4626 | static 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 Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 4632 | if (lockdep_hardirq_context()) { |
Peter Zijlstra | 9a019db | 2020-03-31 20:38:12 +0200 | [diff] [blame] | 4633 | /* |
| 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 Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4650 | static int |
| 4651 | print_lock_invalid_wait_context(struct task_struct *curr, |
| 4652 | struct held_lock *hlock) |
| 4653 | { |
Peter Zijlstra | 9a019db | 2020-03-31 20:38:12 +0200 | [diff] [blame] | 4654 | short curr_inner; |
| 4655 | |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4656 | 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 Zijlstra | 9a019db | 2020-03-31 20:38:12 +0200 | [diff] [blame] | 4671 | |
| 4672 | curr_inner = task_wait_context(curr); |
| 4673 | pr_warn("context-{%d:%d}\n", curr_inner, curr_inner); |
| 4674 | |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4675 | 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 Zhou | a2e05dd | 2021-08-11 10:59:20 +0800 | [diff] [blame] | 4686 | * This check validates we take locks in the right wait-type order; that is it |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4687 | * 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 | */ |
| 4698 | static int check_wait_context(struct task_struct *curr, struct held_lock *next) |
| 4699 | { |
Peter Zijlstra | dfd5e3f | 2020-12-09 16:06:21 +0100 | [diff] [blame] | 4700 | u8 next_inner = hlock_class(next)->wait_type_inner; |
| 4701 | u8 next_outer = hlock_class(next)->wait_type_outer; |
| 4702 | u8 curr_inner; |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4703 | int depth; |
| 4704 | |
Peter Zijlstra | f8b298c | 2021-06-17 20:57:18 +0200 | [diff] [blame] | 4705 | if (!next_inner || next->trylock) |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4706 | 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 Zijlstra | 9a019db | 2020-03-31 20:38:12 +0200 | [diff] [blame] | 4721 | curr_inner = task_wait_context(curr); |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4722 | |
| 4723 | for (; depth < curr->lockdep_depth; depth++) { |
| 4724 | struct held_lock *prev = curr->held_locks + depth; |
Peter Zijlstra | dfd5e3f | 2020-12-09 16:06:21 +0100 | [diff] [blame] | 4725 | u8 prev_inner = hlock_class(prev)->wait_type_inner; |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4726 | |
| 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 Bergmann | 30a35f7 | 2019-06-28 12:29:03 +0200 | [diff] [blame] | 4744 | #else /* CONFIG_PROVE_LOCKING */ |
Arnd Bergmann | 886532a | 2019-06-17 14:47:05 +0200 | [diff] [blame] | 4745 | |
| 4746 | static inline int |
| 4747 | mark_usage(struct task_struct *curr, struct held_lock *hlock, int check) |
| 4748 | { |
| 4749 | return 1; |
| 4750 | } |
| 4751 | |
| 4752 | static inline unsigned int task_irq_context(struct task_struct *task) |
| 4753 | { |
| 4754 | return 0; |
| 4755 | } |
| 4756 | |
| 4757 | static inline int separate_irq_context(struct task_struct *curr, |
| 4758 | struct held_lock *hlock) |
| 4759 | { |
| 4760 | return 0; |
| 4761 | } |
| 4762 | |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4763 | static inline int check_wait_context(struct task_struct *curr, |
| 4764 | struct held_lock *next) |
| 4765 | { |
| 4766 | return 0; |
| 4767 | } |
| 4768 | |
Arnd Bergmann | 30a35f7 | 2019-06-28 12:29:03 +0200 | [diff] [blame] | 4769 | #endif /* CONFIG_PROVE_LOCKING */ |
Arnd Bergmann | 886532a | 2019-06-17 14:47:05 +0200 | [diff] [blame] | 4770 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4771 | /* |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4772 | * Initialize a lock instance's lock-class mapping info: |
| 4773 | */ |
Peter Zijlstra | dfd5e3f | 2020-12-09 16:06:21 +0100 | [diff] [blame] | 4774 | void lockdep_init_map_type(struct lockdep_map *lock, const char *name, |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4775 | struct lock_class_key *key, int subclass, |
Peter Zijlstra | dfd5e3f | 2020-12-09 16:06:21 +0100 | [diff] [blame] | 4776 | u8 inner, u8 outer, u8 lock_type) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4777 | { |
Yong Zhang | d3d03d4 | 2011-11-09 16:04:51 +0800 | [diff] [blame] | 4778 | int i; |
| 4779 | |
Yong Zhang | d3d03d4 | 2011-11-09 16:04:51 +0800 | [diff] [blame] | 4780 | for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++) |
| 4781 | lock->class_cache[i] = NULL; |
Hitoshi Mitake | 6201625 | 2010-10-05 18:01:51 +0900 | [diff] [blame] | 4782 | |
Peter Zijlstra | c8a2500 | 2009-04-17 09:40:49 +0200 | [diff] [blame] | 4783 | #ifdef CONFIG_LOCK_STAT |
| 4784 | lock->cpu = raw_smp_processor_id(); |
| 4785 | #endif |
| 4786 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4787 | /* |
| 4788 | * Can't be having no nameless bastards around this place! |
| 4789 | */ |
Peter Zijlstra | c8a2500 | 2009-04-17 09:40:49 +0200 | [diff] [blame] | 4790 | if (DEBUG_LOCKS_WARN_ON(!name)) { |
| 4791 | lock->name = "NULL"; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4792 | return; |
Peter Zijlstra | c8a2500 | 2009-04-17 09:40:49 +0200 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | lock->name = name; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4796 | |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4797 | lock->wait_type_outer = outer; |
| 4798 | lock->wait_type_inner = inner; |
Peter Zijlstra | dfd5e3f | 2020-12-09 16:06:21 +0100 | [diff] [blame] | 4799 | lock->lock_type = lock_type; |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4800 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4801 | /* |
| 4802 | * No key, no joy, we need to hash something. |
| 4803 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4804 | if (DEBUG_LOCKS_WARN_ON(!key)) |
| 4805 | return; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4806 | /* |
Bart Van Assche | 108c148 | 2019-02-14 15:00:53 -0800 | [diff] [blame] | 4807 | * Sanity check, the lock-class key must either have been allocated |
| 4808 | * statically or must have been registered as a dynamic key. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4809 | */ |
Bart Van Assche | 108c148 | 2019-02-14 15:00:53 -0800 | [diff] [blame] | 4810 | 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4813 | DEBUG_LOCKS_WARN_ON(1); |
| 4814 | return; |
| 4815 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4816 | lock->key = key; |
Peter Zijlstra | c8a2500 | 2009-04-17 09:40:49 +0200 | [diff] [blame] | 4817 | |
| 4818 | if (unlikely(!debug_locks)) |
| 4819 | return; |
| 4820 | |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 4821 | if (subclass) { |
| 4822 | unsigned long flags; |
| 4823 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 4824 | if (DEBUG_LOCKS_WARN_ON(!lockdep_enabled())) |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 4825 | return; |
| 4826 | |
| 4827 | raw_local_irq_save(flags); |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 4828 | lockdep_recursion_inc(); |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 4829 | register_lock_class(lock, subclass, 1); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 4830 | lockdep_recursion_finish(); |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 4831 | raw_local_irq_restore(flags); |
| 4832 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4833 | } |
Peter Zijlstra | dfd5e3f | 2020-12-09 16:06:21 +0100 | [diff] [blame] | 4834 | EXPORT_SYMBOL_GPL(lockdep_init_map_type); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4835 | |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 4836 | struct lock_class_key __lockdep_no_validate__; |
Kent Overstreet | ea6749c | 2012-12-27 22:21:58 -0800 | [diff] [blame] | 4837 | EXPORT_SYMBOL_GPL(__lockdep_no_validate__); |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 4838 | |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 4839 | static void |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 4840 | print_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 Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 4845 | return; |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 4846 | if (debug_locks_silent) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 4847 | return; |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 4848 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4849 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 4850 | pr_warn("==================================\n"); |
| 4851 | pr_warn("WARNING: Nested lock was not taken\n"); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 4852 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 4853 | pr_warn("----------------------------------\n"); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 4854 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4855 | pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr)); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 4856 | print_lock(hlock); |
| 4857 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4858 | pr_warn("\nbut this task is not holding:\n"); |
| 4859 | pr_warn("%s\n", hlock->nest_lock->name); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 4860 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4861 | pr_warn("\nstack backtrace:\n"); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 4862 | dump_stack(); |
| 4863 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4864 | pr_warn("\nother info that might help us debug this:\n"); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 4865 | lockdep_print_held_locks(curr); |
| 4866 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4867 | pr_warn("\nstack backtrace:\n"); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 4868 | dump_stack(); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 4869 | } |
| 4870 | |
Matthew Wilcox | 08f36ff | 2018-01-17 07:14:13 -0800 | [diff] [blame] | 4871 | static int __lock_is_held(const struct lockdep_map *lock, int read); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 4872 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4873 | /* |
| 4874 | * This gets called for every mutex_lock*()/spin_lock*() operation. |
| 4875 | * We maintain the dependency maps and validate the locking attempt: |
Waiman Long | 8ee1086 | 2018-10-02 16:19:17 -0400 | [diff] [blame] | 4876 | * |
| 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4880 | */ |
| 4881 | static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, |
| 4882 | int trylock, int read, int check, int hardirqs_off, |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 4883 | struct lockdep_map *nest_lock, unsigned long ip, |
Peter Zijlstra | 21199f2 | 2015-09-16 16:10:40 +0200 | [diff] [blame] | 4884 | int references, int pin_count) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4885 | { |
| 4886 | struct task_struct *curr = current; |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 4887 | struct lock_class *class = NULL; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4888 | struct held_lock *hlock; |
Alfredo Alvarez Fernandez | 5f18ab5 | 2016-02-11 00:33:32 +0100 | [diff] [blame] | 4889 | unsigned int depth; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4890 | int chain_head = 0; |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 4891 | int class_idx; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4892 | u64 chain_key; |
| 4893 | |
| 4894 | if (unlikely(!debug_locks)) |
| 4895 | return 0; |
| 4896 | |
Oleg Nesterov | fb9edbe | 2014-01-20 19:20:06 +0100 | [diff] [blame] | 4897 | if (!prove_locking || lock->key == &__lockdep_no_validate__) |
| 4898 | check = 0; |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 4899 | |
Hitoshi Mitake | 6201625 | 2010-10-05 18:01:51 +0900 | [diff] [blame] | 4900 | if (subclass < NR_LOCKDEP_CACHING_CLASSES) |
| 4901 | class = lock->class_cache[subclass]; |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 4902 | /* |
Hitoshi Mitake | 6201625 | 2010-10-05 18:01:51 +0900 | [diff] [blame] | 4903 | * Not cached? |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 4904 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4905 | if (unlikely(!class)) { |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 4906 | class = register_lock_class(lock, subclass, 0); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4907 | if (!class) |
| 4908 | return 0; |
| 4909 | } |
Waiman Long | 8ca2b56c | 2018-10-03 13:07:18 -0400 | [diff] [blame] | 4910 | |
| 4911 | debug_class_ops_inc(class); |
| 4912 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4913 | if (very_verbose(class)) { |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 4914 | printk("\nacquire class [%px] %s", class->key, class->name); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4915 | if (class->name_version > 1) |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 4916 | printk(KERN_CONT "#%d", class->name_version); |
| 4917 | printk(KERN_CONT "\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4918 | 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 Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4927 | /* |
| 4928 | * Ran out of static storage for our per-task lock stack again have we? |
| 4929 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4930 | if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH)) |
| 4931 | return 0; |
| 4932 | |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 4933 | class_idx = class - lock_classes; |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 4934 | |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4935 | if (depth) { /* we're holding locks */ |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 4936 | hlock = curr->held_locks + depth - 1; |
| 4937 | if (hlock->class_idx == class_idx && nest_lock) { |
Imre Deak | d934985 | 2019-05-24 23:15:09 +0300 | [diff] [blame] | 4938 | if (!references) |
| 4939 | references++; |
Peter Zijlstra | 7fb4a2c | 2017-03-01 16:23:30 +0100 | [diff] [blame] | 4940 | |
Imre Deak | d934985 | 2019-05-24 23:15:09 +0300 | [diff] [blame] | 4941 | if (!hlock->references) |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 4942 | hlock->references++; |
Imre Deak | d934985 | 2019-05-24 23:15:09 +0300 | [diff] [blame] | 4943 | |
| 4944 | hlock->references += references; |
| 4945 | |
| 4946 | /* Overflow */ |
| 4947 | if (DEBUG_LOCKS_WARN_ON(hlock->references < references)) |
| 4948 | return 0; |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 4949 | |
Imre Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 4950 | return 2; |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 4951 | } |
| 4952 | } |
| 4953 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4954 | hlock = curr->held_locks + depth; |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4955 | /* |
| 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 Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 4959 | if (DEBUG_LOCKS_WARN_ON(!class)) |
| 4960 | return 0; |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 4961 | hlock->class_idx = class_idx; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4962 | hlock->acquire_ip = ip; |
| 4963 | hlock->instance = lock; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 4964 | hlock->nest_lock = nest_lock; |
Boqun Feng | c246975 | 2016-02-16 13:57:40 +0800 | [diff] [blame] | 4965 | hlock->irq_context = task_irq_context(curr); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4966 | hlock->trylock = trylock; |
| 4967 | hlock->read = read; |
| 4968 | hlock->check = check; |
Dmitry Baryshkov | 6951b12 | 2008-08-18 04:26:37 +0400 | [diff] [blame] | 4969 | hlock->hardirqs_off = !!hardirqs_off; |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 4970 | hlock->references = references; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4971 | #ifdef CONFIG_LOCK_STAT |
| 4972 | hlock->waittime_stamp = 0; |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 4973 | hlock->holdtime_stamp = lockstat_clock(); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4974 | #endif |
Peter Zijlstra | 21199f2 | 2015-09-16 16:10:40 +0200 | [diff] [blame] | 4975 | hlock->pin_count = pin_count; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4976 | |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 4977 | if (check_wait_context(curr, hlock)) |
| 4978 | return 0; |
| 4979 | |
Yuyang Du | 0918065 | 2019-05-06 16:19:37 +0800 | [diff] [blame] | 4980 | /* Initialize the lock usage bit */ |
| 4981 | if (!mark_usage(curr, hlock, check)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4982 | return 0; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 4983 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4984 | /* |
Gautham R Shenoy | 17aacfb9 | 2007-10-28 20:47:01 +0100 | [diff] [blame] | 4985 | * Calculate the chain hash: it's the combined hash of all the |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4986 | * 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 Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4994 | /* |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 4995 | * Whoops, we did it again.. class_idx is invalid. |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4996 | */ |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 4997 | if (DEBUG_LOCKS_WARN_ON(!test_bit(class_idx, lock_classes_in_use))) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4998 | return 0; |
| 4999 | |
| 5000 | chain_key = curr->curr_chain_key; |
| 5001 | if (!depth) { |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 5002 | /* |
| 5003 | * How can we have a chain hash when we ain't got no keys?! |
| 5004 | */ |
Yuyang Du | f6ec882 | 2019-05-06 16:19:24 +0800 | [diff] [blame] | 5005 | if (DEBUG_LOCKS_WARN_ON(chain_key != INITIAL_CHAIN_KEY)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5006 | return 0; |
| 5007 | chain_head = 1; |
| 5008 | } |
| 5009 | |
| 5010 | hlock->prev_chain_key = chain_key; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 5011 | if (separate_irq_context(curr, hlock)) { |
Yuyang Du | f6ec882 | 2019-05-06 16:19:24 +0800 | [diff] [blame] | 5012 | chain_key = INITIAL_CHAIN_KEY; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 5013 | chain_head = 1; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5014 | } |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 5015 | chain_key = iterate_chain_key(chain_key, hlock_id(hlock)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5016 | |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 5017 | if (nest_lock && !__lock_is_held(nest_lock, -1)) { |
| 5018 | print_lock_nested_lock_not_held(curr, hlock, ip); |
| 5019 | return 0; |
| 5020 | } |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 5021 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5022 | 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 Du | 0b9fc8e | 2019-05-06 16:19:26 +0800 | [diff] [blame] | 5027 | if (!validate_chain(curr, hlock, chain_head, chain_key)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 5028 | return 0; |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 5029 | |
Gregory Haskins | 3aa416b | 2007-10-11 22:11:11 +0200 | [diff] [blame] | 5030 | curr->curr_chain_key = chain_key; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5031 | curr->lockdep_depth++; |
| 5032 | check_chain_key(curr); |
Jarek Poplawski | 60e114d | 2007-02-20 13:58:00 -0800 | [diff] [blame] | 5033 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 5034 | if (unlikely(!debug_locks)) |
| 5035 | return 0; |
| 5036 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5037 | if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) { |
| 5038 | debug_locks_off(); |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 5039 | print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!"); |
| 5040 | printk(KERN_DEBUG "depth: %i max: %lu!\n", |
Ben Greear | c054060 | 2013-02-06 10:56:19 -0800 | [diff] [blame] | 5041 | curr->lockdep_depth, MAX_LOCK_DEPTH); |
Ben Greear | c054060 | 2013-02-06 10:56:19 -0800 | [diff] [blame] | 5042 | |
| 5043 | lockdep_print_held_locks(current); |
| 5044 | debug_show_all_locks(); |
Peter Zijlstra | eedeeab | 2009-03-18 12:38:47 +0100 | [diff] [blame] | 5045 | dump_stack(); |
Ben Greear | c054060 | 2013-02-06 10:56:19 -0800 | [diff] [blame] | 5046 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5047 | return 0; |
| 5048 | } |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 5049 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5050 | if (unlikely(curr->lockdep_depth > max_lockdep_depth)) |
| 5051 | max_lockdep_depth = curr->lockdep_depth; |
| 5052 | |
| 5053 | return 1; |
| 5054 | } |
| 5055 | |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 5056 | static void print_unlock_imbalance_bug(struct task_struct *curr, |
| 5057 | struct lockdep_map *lock, |
| 5058 | unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5059 | { |
| 5060 | if (!debug_locks_off()) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 5061 | return; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5062 | if (debug_locks_silent) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 5063 | return; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5064 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 5065 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 5066 | pr_warn("=====================================\n"); |
| 5067 | pr_warn("WARNING: bad unlock balance detected!\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 5068 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 5069 | pr_warn("-------------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 5070 | pr_warn("%s/%d is trying to release lock (", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5071 | curr->comm, task_pid_nr(curr)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5072 | print_lockdep_cache(lock); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 5073 | pr_cont(") at:\n"); |
Dmitry Safonov | 2062a4e | 2020-06-08 21:29:56 -0700 | [diff] [blame] | 5074 | print_ip_sym(KERN_WARNING, ip); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 5075 | pr_warn("but there are no more locks to release!\n"); |
| 5076 | pr_warn("\nother info that might help us debug this:\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5077 | lockdep_print_held_locks(curr); |
| 5078 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 5079 | pr_warn("\nstack backtrace:\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5080 | dump_stack(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5081 | } |
| 5082 | |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 5083 | static noinstr int match_held_lock(const struct held_lock *hlock, |
| 5084 | const struct lockdep_map *lock) |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 5085 | { |
| 5086 | if (hlock->instance == lock) |
| 5087 | return 1; |
| 5088 | |
| 5089 | if (hlock->references) { |
Matthew Wilcox | 08f36ff | 2018-01-17 07:14:13 -0800 | [diff] [blame] | 5090 | const struct lock_class *class = lock->class_cache[0]; |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 5091 | |
| 5092 | if (!class) |
| 5093 | class = look_up_lock_class(lock, 0); |
| 5094 | |
Peter Zijlstra | 80e0401 | 2011-08-05 14:26:17 +0200 | [diff] [blame] | 5095 | /* |
| 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 Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 5101 | if (!class) |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 5102 | return 0; |
| 5103 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 5104 | /* |
| 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 Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 5109 | if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock)) |
| 5110 | return 0; |
| 5111 | |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 5112 | if (hlock->class_idx == class - lock_classes) |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 5113 | return 1; |
| 5114 | } |
| 5115 | |
| 5116 | return 0; |
| 5117 | } |
| 5118 | |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 5119 | /* @depth must not be zero */ |
| 5120 | static 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 | |
| 5150 | out: |
| 5151 | *idx = i; |
| 5152 | return ret; |
| 5153 | } |
| 5154 | |
J. R. Okajima | e969970 | 2017-02-03 01:38:16 +0900 | [diff] [blame] | 5155 | static int reacquire_held_locks(struct task_struct *curr, unsigned int depth, |
Imre Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5156 | int idx, unsigned int *merged) |
J. R. Okajima | e969970 | 2017-02-03 01:38:16 +0900 | [diff] [blame] | 5157 | { |
| 5158 | struct held_lock *hlock; |
Imre Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5159 | int first_idx = idx; |
J. R. Okajima | e969970 | 2017-02-03 01:38:16 +0900 | [diff] [blame] | 5160 | |
Waiman Long | 8ee1086 | 2018-10-02 16:19:17 -0400 | [diff] [blame] | 5161 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 5162 | return 0; |
| 5163 | |
J. R. Okajima | e969970 | 2017-02-03 01:38:16 +0900 | [diff] [blame] | 5164 | for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) { |
Imre Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5165 | switch (__lock_acquire(hlock->instance, |
J. R. Okajima | e969970 | 2017-02-03 01:38:16 +0900 | [diff] [blame] | 5166 | hlock_class(hlock)->subclass, |
| 5167 | hlock->trylock, |
| 5168 | hlock->read, hlock->check, |
| 5169 | hlock->hardirqs_off, |
| 5170 | hlock->nest_lock, hlock->acquire_ip, |
Imre Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5171 | hlock->references, hlock->pin_count)) { |
| 5172 | case 0: |
J. R. Okajima | e969970 | 2017-02-03 01:38:16 +0900 | [diff] [blame] | 5173 | return 1; |
Imre Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5174 | 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. Okajima | e969970 | 2017-02-03 01:38:16 +0900 | [diff] [blame] | 5183 | } |
| 5184 | return 0; |
| 5185 | } |
| 5186 | |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5187 | static int |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 5188 | __lock_set_class(struct lockdep_map *lock, const char *name, |
| 5189 | struct lock_class_key *key, unsigned int subclass, |
| 5190 | unsigned long ip) |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5191 | { |
| 5192 | struct task_struct *curr = current; |
Imre Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5193 | unsigned int depth, merged = 0; |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 5194 | struct held_lock *hlock; |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5195 | struct lock_class *class; |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5196 | int i; |
| 5197 | |
Waiman Long | 513e107 | 2019-01-09 23:03:25 -0500 | [diff] [blame] | 5198 | if (unlikely(!debug_locks)) |
| 5199 | return 0; |
| 5200 | |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5201 | depth = curr->lockdep_depth; |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 5202 | /* |
| 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 Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5206 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
| 5207 | return 0; |
| 5208 | |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 5209 | hlock = find_held_lock(curr, lock, depth, &i); |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 5210 | if (!hlock) { |
| 5211 | print_unlock_imbalance_bug(curr, lock, ip); |
| 5212 | return 0; |
| 5213 | } |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5214 | |
Peter Zijlstra | de8f5e4 | 2020-03-21 12:26:01 +0100 | [diff] [blame] | 5215 | lockdep_init_map_waits(lock, name, key, 0, |
| 5216 | lock->wait_type_inner, |
| 5217 | lock->wait_type_outer); |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5218 | class = register_lock_class(lock, subclass, 0); |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 5219 | hlock->class_idx = class - lock_classes; |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5220 | |
| 5221 | curr->lockdep_depth = i; |
| 5222 | curr->curr_chain_key = hlock->prev_chain_key; |
| 5223 | |
Imre Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5224 | if (reacquire_held_locks(curr, depth, i, &merged)) |
J. R. Okajima | e969970 | 2017-02-03 01:38:16 +0900 | [diff] [blame] | 5225 | return 0; |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5226 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 5227 | /* |
| 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 Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5231 | if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged)) |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5232 | return 0; |
| 5233 | return 1; |
| 5234 | } |
| 5235 | |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 5236 | static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip) |
| 5237 | { |
| 5238 | struct task_struct *curr = current; |
Imre Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5239 | unsigned int depth, merged = 0; |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 5240 | struct held_lock *hlock; |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 5241 | int i; |
| 5242 | |
Waiman Long | 7149258 | 2019-01-09 23:03:25 -0500 | [diff] [blame] | 5243 | if (unlikely(!debug_locks)) |
| 5244 | return 0; |
| 5245 | |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 5246 | 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 Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 5255 | if (!hlock) { |
| 5256 | print_unlock_imbalance_bug(curr, lock, ip); |
| 5257 | return 0; |
| 5258 | } |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 5259 | |
| 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 Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5267 | 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. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 5272 | 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 Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5280 | |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 5281 | return 1; |
| 5282 | } |
| 5283 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5284 | /* |
Dan Carpenter | c759bc4 | 2019-11-04 12:12:52 +0300 | [diff] [blame] | 5285 | * Remove the lock from the list of currently held locks - this gets |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 5286 | * called on mutex_unlock()/spin_unlock*() (or on a failed |
| 5287 | * mutex_lock_interruptible()). |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5288 | */ |
| 5289 | static int |
Yuyang Du | b4adfe8e | 2019-05-06 16:19:34 +0800 | [diff] [blame] | 5290 | __lock_release(struct lockdep_map *lock, unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5291 | { |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 5292 | struct task_struct *curr = current; |
Imre Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5293 | unsigned int depth, merged = 1; |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 5294 | struct held_lock *hlock; |
Ingo Molnar | e966eae | 2017-12-12 12:31:16 +0100 | [diff] [blame] | 5295 | int i; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5296 | |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 5297 | if (unlikely(!debug_locks)) |
| 5298 | return 0; |
| 5299 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5300 | depth = curr->lockdep_depth; |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 5301 | /* |
| 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 Wu | dd471ef | 2019-05-30 19:59:35 +0800 | [diff] [blame] | 5305 | if (depth <= 0) { |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 5306 | print_unlock_imbalance_bug(curr, lock, ip); |
| 5307 | return 0; |
| 5308 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5309 | |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 5310 | /* |
| 5311 | * Check whether the lock exists in the current stack |
| 5312 | * of held locks: |
| 5313 | */ |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 5314 | hlock = find_held_lock(curr, lock, depth, &i); |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 5315 | if (!hlock) { |
| 5316 | print_unlock_imbalance_bug(curr, lock, ip); |
| 5317 | return 0; |
| 5318 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5319 | |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 5320 | if (hlock->instance == lock) |
| 5321 | lock_release_holdtime(hlock); |
| 5322 | |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 5323 | WARN(hlock->pin_count, "releasing a pinned lock\n"); |
| 5324 | |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 5325 | 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 Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5336 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5337 | /* |
| 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 Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 5342 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5343 | curr->lockdep_depth = i; |
| 5344 | curr->curr_chain_key = hlock->prev_chain_key; |
| 5345 | |
Waiman Long | ce52a18 | 2018-10-02 16:19:18 -0400 | [diff] [blame] | 5346 | /* |
| 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 Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5353 | if (reacquire_held_locks(curr, depth, i + 1, &merged)) |
J. R. Okajima | e969970 | 2017-02-03 01:38:16 +0900 | [diff] [blame] | 5354 | return 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5355 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 5356 | /* |
| 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 Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5359 | * Pouring two of the bottles together is acceptable. |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 5360 | */ |
Imre Deak | 8c8889d | 2019-05-24 23:15:08 +0300 | [diff] [blame] | 5361 | DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged); |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 5362 | |
Waiman Long | ce52a18 | 2018-10-02 16:19:18 -0400 | [diff] [blame] | 5363 | /* |
| 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5369 | } |
| 5370 | |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 5371 | static __always_inline |
Masami Hiramatsu | 2f43c60 | 2019-02-13 01:15:05 +0900 | [diff] [blame] | 5372 | int __lock_is_held(const struct lockdep_map *lock, int read) |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 5373 | { |
| 5374 | struct task_struct *curr = current; |
| 5375 | int i; |
| 5376 | |
| 5377 | for (i = 0; i < curr->lockdep_depth; i++) { |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 5378 | struct held_lock *hlock = curr->held_locks + i; |
| 5379 | |
Peter Zijlstra | f831948 | 2016-11-30 14:32:25 +1100 | [diff] [blame] | 5380 | if (match_held_lock(hlock, lock)) { |
Sebastian Andrzej Siewior | 2507003 | 2021-09-03 10:40:01 +0200 | [diff] [blame] | 5381 | if (read == -1 || !!hlock->read == read) |
Shuah Khan | f8cfa46 | 2021-02-26 17:06:59 -0700 | [diff] [blame] | 5382 | return LOCK_STATE_HELD; |
Peter Zijlstra | f831948 | 2016-11-30 14:32:25 +1100 | [diff] [blame] | 5383 | |
Shuah Khan | f8cfa46 | 2021-02-26 17:06:59 -0700 | [diff] [blame] | 5384 | return LOCK_STATE_NOT_HELD; |
Peter Zijlstra | f831948 | 2016-11-30 14:32:25 +1100 | [diff] [blame] | 5385 | } |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 5386 | } |
| 5387 | |
Shuah Khan | f8cfa46 | 2021-02-26 17:06:59 -0700 | [diff] [blame] | 5388 | return LOCK_STATE_NOT_HELD; |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 5389 | } |
| 5390 | |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5391 | static 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 | |
| 5419 | static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie) |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 5420 | { |
| 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 Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5431 | hlock->pin_count += cookie.val; |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 5432 | return; |
| 5433 | } |
| 5434 | } |
| 5435 | |
| 5436 | WARN(1, "pinning an unheld lock\n"); |
| 5437 | } |
| 5438 | |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5439 | static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie) |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 5440 | { |
| 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 Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5454 | 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 Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 5459 | return; |
| 5460 | } |
| 5461 | } |
| 5462 | |
| 5463 | WARN(1, "unpinning an unheld lock\n"); |
| 5464 | } |
| 5465 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5466 | /* |
| 5467 | * Check whether we follow the irq-flags state precisely: |
| 5468 | */ |
Peter Zijlstra | 77ca93a | 2021-01-06 15:36:23 +0100 | [diff] [blame] | 5469 | static noinstr void check_flags(unsigned long flags) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5470 | { |
Arnd Bergmann | 30a35f7 | 2019-06-28 12:29:03 +0200 | [diff] [blame] | 5471 | #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5472 | if (!debug_locks) |
| 5473 | return; |
| 5474 | |
Peter Zijlstra | 77ca93a | 2021-01-06 15:36:23 +0100 | [diff] [blame] | 5475 | /* Get the warning out.. */ |
| 5476 | instrumentation_begin(); |
| 5477 | |
Ingo Molnar | 5f9fa8a | 2007-12-07 19:02:47 +0100 | [diff] [blame] | 5478 | if (irqs_disabled_flags(flags)) { |
Peter Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 5479 | if (DEBUG_LOCKS_WARN_ON(lockdep_hardirqs_enabled())) { |
Ingo Molnar | 5f9fa8a | 2007-12-07 19:02:47 +0100 | [diff] [blame] | 5480 | printk("possible reason: unannotated irqs-off.\n"); |
| 5481 | } |
| 5482 | } else { |
Peter Zijlstra | f9ad4a5 | 2020-05-27 13:03:26 +0200 | [diff] [blame] | 5483 | if (DEBUG_LOCKS_WARN_ON(!lockdep_hardirqs_enabled())) { |
Ingo Molnar | 5f9fa8a | 2007-12-07 19:02:47 +0100 | [diff] [blame] | 5484 | printk("possible reason: unannotated irqs-on.\n"); |
| 5485 | } |
| 5486 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5487 | |
| 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 Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 5494 | if (softirq_count()) { |
| 5495 | /* like the above, but with softirqs */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5496 | DEBUG_LOCKS_WARN_ON(current->softirqs_enabled); |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 5497 | } else { |
| 5498 | /* lick the above, does it taste good? */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5499 | DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled); |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 5500 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | if (!debug_locks) |
| 5504 | print_irqtrace_events(current); |
Peter Zijlstra | 77ca93a | 2021-01-06 15:36:23 +0100 | [diff] [blame] | 5505 | |
| 5506 | instrumentation_end(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5507 | #endif |
| 5508 | } |
| 5509 | |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 5510 | void lock_set_class(struct lockdep_map *lock, const char *name, |
| 5511 | struct lock_class_key *key, unsigned int subclass, |
| 5512 | unsigned long ip) |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5513 | { |
| 5514 | unsigned long flags; |
| 5515 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5516 | if (unlikely(!lockdep_enabled())) |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5517 | return; |
| 5518 | |
| 5519 | raw_local_irq_save(flags); |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5520 | lockdep_recursion_inc(); |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5521 | check_flags(flags); |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 5522 | if (__lock_set_class(lock, name, key, subclass, ip)) |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5523 | check_chain_key(current); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 5524 | lockdep_recursion_finish(); |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5525 | raw_local_irq_restore(flags); |
| 5526 | } |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 5527 | EXPORT_SYMBOL_GPL(lock_set_class); |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 5528 | |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 5529 | void lock_downgrade(struct lockdep_map *lock, unsigned long ip) |
| 5530 | { |
| 5531 | unsigned long flags; |
| 5532 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5533 | if (unlikely(!lockdep_enabled())) |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 5534 | return; |
| 5535 | |
| 5536 | raw_local_irq_save(flags); |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5537 | lockdep_recursion_inc(); |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 5538 | check_flags(flags); |
| 5539 | if (__lock_downgrade(lock, ip)) |
| 5540 | check_chain_key(current); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 5541 | lockdep_recursion_finish(); |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 5542 | raw_local_irq_restore(flags); |
| 5543 | } |
| 5544 | EXPORT_SYMBOL_GPL(lock_downgrade); |
| 5545 | |
Peter Zijlstra | f6f48e1 | 2020-02-20 09:45:02 +0100 | [diff] [blame] | 5546 | /* NMI context !!! */ |
| 5547 | static 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.org | 23870f1 | 2020-09-02 18:03:23 +0200 | [diff] [blame] | 5551 | unsigned long mask = LOCKF_USED; |
Peter Zijlstra | f6f48e1 | 2020-02-20 09:45:02 +0100 | [diff] [blame] | 5552 | |
| 5553 | /* if it doesn't have a class (yet), it certainly hasn't been used yet */ |
| 5554 | if (!class) |
| 5555 | return; |
| 5556 | |
peterz@infradead.org | 23870f1 | 2020-09-02 18:03:23 +0200 | [diff] [blame] | 5557 | /* |
| 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 Zijlstra | f6f48e1 | 2020-02-20 09:45:02 +0100 | [diff] [blame] | 5565 | 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 | |
| 5573 | static bool lockdep_nmi(void) |
| 5574 | { |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5575 | if (raw_cpu_read(lockdep_recursion)) |
Peter Zijlstra | f6f48e1 | 2020-02-20 09:45:02 +0100 | [diff] [blame] | 5576 | return false; |
| 5577 | |
| 5578 | if (!in_nmi()) |
| 5579 | return false; |
| 5580 | |
| 5581 | return true; |
| 5582 | } |
| 5583 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5584 | /* |
Boqun Feng | e918188 | 2020-08-07 15:42:20 +0800 | [diff] [blame] | 5585 | * 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 | */ |
| 5590 | bool read_lock_is_recursive(void) |
| 5591 | { |
| 5592 | return force_read_lock_recursive || |
| 5593 | !IS_ENABLED(CONFIG_QUEUED_RWLOCKS) || |
| 5594 | in_interrupt(); |
| 5595 | } |
| 5596 | EXPORT_SYMBOL_GPL(read_lock_is_recursive); |
| 5597 | |
| 5598 | /* |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5599 | * We are not always called with irqs disabled - do that here, |
| 5600 | * and also avoid lockdep recursion: |
| 5601 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 5602 | void lock_acquire(struct lockdep_map *lock, unsigned int subclass, |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 5603 | int trylock, int read, int check, |
| 5604 | struct lockdep_map *nest_lock, unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5605 | { |
| 5606 | unsigned long flags; |
| 5607 | |
Peter Zijlstra | eb1f002 | 2020-08-07 20:53:16 +0200 | [diff] [blame] | 5608 | trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip); |
| 5609 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5610 | if (!debug_locks) |
| 5611 | return; |
| 5612 | |
| 5613 | if (unlikely(!lockdep_enabled())) { |
Peter Zijlstra | f6f48e1 | 2020-02-20 09:45:02 +0100 | [diff] [blame] | 5614 | /* 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5630 | return; |
Peter Zijlstra | f6f48e1 | 2020-02-20 09:45:02 +0100 | [diff] [blame] | 5631 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5632 | |
| 5633 | raw_local_irq_save(flags); |
| 5634 | check_flags(flags); |
| 5635 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5636 | lockdep_recursion_inc(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5637 | __lock_acquire(lock, subclass, trylock, read, check, |
Peter Zijlstra | 21199f2 | 2015-09-16 16:10:40 +0200 | [diff] [blame] | 5638 | irqs_disabled_flags(flags), nest_lock, ip, 0, 0); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 5639 | lockdep_recursion_finish(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5640 | raw_local_irq_restore(flags); |
| 5641 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5642 | EXPORT_SYMBOL_GPL(lock_acquire); |
| 5643 | |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 5644 | void lock_release(struct lockdep_map *lock, unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5645 | { |
| 5646 | unsigned long flags; |
| 5647 | |
Peter Zijlstra | eb1f002 | 2020-08-07 20:53:16 +0200 | [diff] [blame] | 5648 | trace_lock_release(lock, ip); |
| 5649 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5650 | if (unlikely(!lockdep_enabled())) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5651 | return; |
| 5652 | |
| 5653 | raw_local_irq_save(flags); |
| 5654 | check_flags(flags); |
Peter Zijlstra | eb1f002 | 2020-08-07 20:53:16 +0200 | [diff] [blame] | 5655 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5656 | lockdep_recursion_inc(); |
Yuyang Du | b4adfe8e | 2019-05-06 16:19:34 +0800 | [diff] [blame] | 5657 | if (__lock_release(lock, ip)) |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 5658 | check_chain_key(current); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 5659 | lockdep_recursion_finish(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5660 | raw_local_irq_restore(flags); |
| 5661 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5662 | EXPORT_SYMBOL_GPL(lock_release); |
| 5663 | |
Peter Zijlstra | c86e9b9 | 2020-03-18 14:22:03 +0100 | [diff] [blame] | 5664 | noinstr int lock_is_held_type(const struct lockdep_map *lock, int read) |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 5665 | { |
| 5666 | unsigned long flags; |
Shuah Khan | f8cfa46 | 2021-02-26 17:06:59 -0700 | [diff] [blame] | 5667 | int ret = LOCK_STATE_NOT_HELD; |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 5668 | |
Shuah Khan | 3e31f94 | 2021-02-26 17:06:58 -0700 | [diff] [blame] | 5669 | /* |
| 5670 | * Avoid false negative lockdep_assert_held() and |
| 5671 | * lockdep_assert_not_held(). |
| 5672 | */ |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5673 | if (unlikely(!lockdep_enabled())) |
Shuah Khan | f8cfa46 | 2021-02-26 17:06:59 -0700 | [diff] [blame] | 5674 | return LOCK_STATE_UNKNOWN; |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 5675 | |
| 5676 | raw_local_irq_save(flags); |
| 5677 | check_flags(flags); |
| 5678 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5679 | lockdep_recursion_inc(); |
Peter Zijlstra | f831948 | 2016-11-30 14:32:25 +1100 | [diff] [blame] | 5680 | ret = __lock_is_held(lock, read); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 5681 | lockdep_recursion_finish(); |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 5682 | raw_local_irq_restore(flags); |
| 5683 | |
| 5684 | return ret; |
| 5685 | } |
Peter Zijlstra | f831948 | 2016-11-30 14:32:25 +1100 | [diff] [blame] | 5686 | EXPORT_SYMBOL_GPL(lock_is_held_type); |
Masami Hiramatsu | 2f43c60 | 2019-02-13 01:15:05 +0900 | [diff] [blame] | 5687 | NOKPROBE_SYMBOL(lock_is_held_type); |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 5688 | |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5689 | struct pin_cookie lock_pin_lock(struct lockdep_map *lock) |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 5690 | { |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5691 | struct pin_cookie cookie = NIL_COOKIE; |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 5692 | unsigned long flags; |
| 5693 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5694 | if (unlikely(!lockdep_enabled())) |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5695 | return cookie; |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 5696 | |
| 5697 | raw_local_irq_save(flags); |
| 5698 | check_flags(flags); |
| 5699 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5700 | lockdep_recursion_inc(); |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5701 | cookie = __lock_pin_lock(lock); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 5702 | lockdep_recursion_finish(); |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 5703 | raw_local_irq_restore(flags); |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5704 | |
| 5705 | return cookie; |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 5706 | } |
| 5707 | EXPORT_SYMBOL_GPL(lock_pin_lock); |
| 5708 | |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5709 | void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie) |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 5710 | { |
| 5711 | unsigned long flags; |
| 5712 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5713 | if (unlikely(!lockdep_enabled())) |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 5714 | return; |
| 5715 | |
| 5716 | raw_local_irq_save(flags); |
| 5717 | check_flags(flags); |
| 5718 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5719 | lockdep_recursion_inc(); |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5720 | __lock_repin_lock(lock, cookie); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 5721 | lockdep_recursion_finish(); |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5722 | raw_local_irq_restore(flags); |
| 5723 | } |
| 5724 | EXPORT_SYMBOL_GPL(lock_repin_lock); |
| 5725 | |
| 5726 | void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie) |
| 5727 | { |
| 5728 | unsigned long flags; |
| 5729 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5730 | if (unlikely(!lockdep_enabled())) |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5731 | return; |
| 5732 | |
| 5733 | raw_local_irq_save(flags); |
| 5734 | check_flags(flags); |
| 5735 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5736 | lockdep_recursion_inc(); |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 5737 | __lock_unpin_lock(lock, cookie); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 5738 | lockdep_recursion_finish(); |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 5739 | raw_local_irq_restore(flags); |
| 5740 | } |
| 5741 | EXPORT_SYMBOL_GPL(lock_unpin_lock); |
| 5742 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5743 | #ifdef CONFIG_LOCK_STAT |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 5744 | static void print_lock_contention_bug(struct task_struct *curr, |
| 5745 | struct lockdep_map *lock, |
| 5746 | unsigned long ip) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5747 | { |
| 5748 | if (!debug_locks_off()) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 5749 | return; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5750 | if (debug_locks_silent) |
Yuyang Du | f7c1c6b | 2019-05-06 16:19:17 +0800 | [diff] [blame] | 5751 | return; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5752 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 5753 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 5754 | pr_warn("=================================\n"); |
| 5755 | pr_warn("WARNING: bad contention detected!\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 5756 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 5757 | pr_warn("---------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 5758 | pr_warn("%s/%d is trying to contend lock (", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5759 | curr->comm, task_pid_nr(curr)); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5760 | print_lockdep_cache(lock); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 5761 | pr_cont(") at:\n"); |
Dmitry Safonov | 2062a4e | 2020-06-08 21:29:56 -0700 | [diff] [blame] | 5762 | print_ip_sym(KERN_WARNING, ip); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 5763 | pr_warn("but there are no locks held!\n"); |
| 5764 | pr_warn("\nother info that might help us debug this:\n"); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5765 | lockdep_print_held_locks(curr); |
| 5766 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 5767 | pr_warn("\nstack backtrace:\n"); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5768 | dump_stack(); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5769 | } |
| 5770 | |
| 5771 | static void |
| 5772 | __lock_contended(struct lockdep_map *lock, unsigned long ip) |
| 5773 | { |
| 5774 | struct task_struct *curr = current; |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 5775 | struct held_lock *hlock; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5776 | struct lock_class_stats *stats; |
| 5777 | unsigned int depth; |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 5778 | int i, contention_point, contending_point; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5779 | |
| 5780 | depth = curr->lockdep_depth; |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 5781 | /* |
| 5782 | * Whee, we contended on this lock, except it seems we're not |
| 5783 | * actually trying to acquire anything much at all.. |
| 5784 | */ |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5785 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
| 5786 | return; |
| 5787 | |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 5788 | hlock = find_held_lock(curr, lock, depth, &i); |
| 5789 | if (!hlock) { |
| 5790 | print_lock_contention_bug(curr, lock, ip); |
| 5791 | return; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5792 | } |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5793 | |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 5794 | if (hlock->instance != lock) |
| 5795 | return; |
| 5796 | |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 5797 | hlock->waittime_stamp = lockstat_clock(); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5798 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 5799 | contention_point = lock_point(hlock_class(hlock)->contention_point, ip); |
| 5800 | contending_point = lock_point(hlock_class(hlock)->contending_point, |
| 5801 | lock->ip); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5802 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 5803 | stats = get_lock_stats(hlock_class(hlock)); |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 5804 | if (contention_point < LOCKSTAT_POINTS) |
| 5805 | stats->contention_point[contention_point]++; |
| 5806 | if (contending_point < LOCKSTAT_POINTS) |
| 5807 | stats->contending_point[contending_point]++; |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 5808 | if (lock->cpu != smp_processor_id()) |
| 5809 | stats->bounces[bounce_contended + !!hlock->read]++; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5810 | } |
| 5811 | |
| 5812 | static void |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 5813 | __lock_acquired(struct lockdep_map *lock, unsigned long ip) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5814 | { |
| 5815 | struct task_struct *curr = current; |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 5816 | struct held_lock *hlock; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5817 | struct lock_class_stats *stats; |
| 5818 | unsigned int depth; |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 5819 | u64 now, waittime = 0; |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 5820 | int i, cpu; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5821 | |
| 5822 | depth = curr->lockdep_depth; |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 5823 | /* |
| 5824 | * Yay, we acquired ownership of this lock we didn't try to |
| 5825 | * acquire, how the heck did that happen? |
| 5826 | */ |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5827 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
| 5828 | return; |
| 5829 | |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 5830 | hlock = find_held_lock(curr, lock, depth, &i); |
| 5831 | if (!hlock) { |
| 5832 | print_lock_contention_bug(curr, lock, _RET_IP_); |
| 5833 | return; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5834 | } |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5835 | |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 5836 | if (hlock->instance != lock) |
| 5837 | return; |
| 5838 | |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 5839 | cpu = smp_processor_id(); |
| 5840 | if (hlock->waittime_stamp) { |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 5841 | now = lockstat_clock(); |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 5842 | waittime = now - hlock->waittime_stamp; |
| 5843 | hlock->holdtime_stamp = now; |
| 5844 | } |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5845 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 5846 | stats = get_lock_stats(hlock_class(hlock)); |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 5847 | 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 Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 5855 | |
| 5856 | lock->cpu = cpu; |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 5857 | lock->ip = ip; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5858 | } |
| 5859 | |
| 5860 | void lock_contended(struct lockdep_map *lock, unsigned long ip) |
| 5861 | { |
| 5862 | unsigned long flags; |
| 5863 | |
Leo Yan | 89e70d5 | 2021-05-12 20:09:37 +0800 | [diff] [blame] | 5864 | trace_lock_contended(lock, ip); |
Peter Zijlstra | eb1f002 | 2020-08-07 20:53:16 +0200 | [diff] [blame] | 5865 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5866 | if (unlikely(!lock_stat || !lockdep_enabled())) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5867 | return; |
| 5868 | |
| 5869 | raw_local_irq_save(flags); |
| 5870 | check_flags(flags); |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5871 | lockdep_recursion_inc(); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5872 | __lock_contended(lock, ip); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 5873 | lockdep_recursion_finish(); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5874 | raw_local_irq_restore(flags); |
| 5875 | } |
| 5876 | EXPORT_SYMBOL_GPL(lock_contended); |
| 5877 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 5878 | void lock_acquired(struct lockdep_map *lock, unsigned long ip) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5879 | { |
| 5880 | unsigned long flags; |
| 5881 | |
Leo Yan | 89e70d5 | 2021-05-12 20:09:37 +0800 | [diff] [blame] | 5882 | trace_lock_acquired(lock, ip); |
Peter Zijlstra | eb1f002 | 2020-08-07 20:53:16 +0200 | [diff] [blame] | 5883 | |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5884 | if (unlikely(!lock_stat || !lockdep_enabled())) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5885 | return; |
| 5886 | |
| 5887 | raw_local_irq_save(flags); |
| 5888 | check_flags(flags); |
Peter Zijlstra | 4d00409 | 2020-10-02 11:04:21 +0200 | [diff] [blame] | 5889 | lockdep_recursion_inc(); |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 5890 | __lock_acquired(lock, ip); |
Peter Zijlstra | 10476e6 | 2020-03-13 09:56:38 +0100 | [diff] [blame] | 5891 | lockdep_recursion_finish(); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 5892 | raw_local_irq_restore(flags); |
| 5893 | } |
| 5894 | EXPORT_SYMBOL_GPL(lock_acquired); |
| 5895 | #endif |
| 5896 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5897 | /* |
| 5898 | * Used by the testsuite, sanitize the validator state |
| 5899 | * after a simulated failure: |
| 5900 | */ |
| 5901 | |
| 5902 | void lockdep_reset(void) |
| 5903 | { |
| 5904 | unsigned long flags; |
Ingo Molnar | 23d95a0 | 2006-12-13 00:34:40 -0800 | [diff] [blame] | 5905 | int i; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5906 | |
| 5907 | raw_local_irq_save(flags); |
Yuyang Du | e196e47 | 2019-05-06 16:19:23 +0800 | [diff] [blame] | 5908 | lockdep_init_task(current); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5909 | 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 Molnar | 23d95a0 | 2006-12-13 00:34:40 -0800 | [diff] [blame] | 5914 | for (i = 0; i < CHAINHASH_SIZE; i++) |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 5915 | INIT_HLIST_HEAD(chainhash_table + i); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5916 | raw_local_irq_restore(flags); |
| 5917 | } |
| 5918 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5919 | /* Remove a class from a lock chain. Must be called with the graph lock held. */ |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame] | 5920 | static void remove_class_from_lock_chain(struct pending_free *pf, |
| 5921 | struct lock_chain *chain, |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5922 | struct lock_class *class) |
| 5923 | { |
| 5924 | #ifdef CONFIG_PROVE_LOCKING |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5925 | int i; |
| 5926 | |
| 5927 | for (i = chain->base; i < chain->base + chain->depth; i++) { |
Boqun Feng | f611e8c | 2020-08-07 15:42:33 +0800 | [diff] [blame] | 5928 | if (chain_hlock_class_idx(chain_hlocks[i]) != class - lock_classes) |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5929 | continue; |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5930 | /* |
| 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 Long | 836bd74 | 2020-02-06 10:24:06 -0500 | [diff] [blame] | 5934 | goto free_lock_chain; |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5935 | } |
| 5936 | /* Since the chain has not been modified, return. */ |
| 5937 | return; |
| 5938 | |
Waiman Long | 836bd74 | 2020-02-06 10:24:06 -0500 | [diff] [blame] | 5939 | free_lock_chain: |
Waiman Long | 810507f | 2020-02-06 10:24:08 -0500 | [diff] [blame] | 5940 | free_chain_hlocks(chain->base, chain->depth); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5941 | /* Overwrite the chain key for concurrent RCU readers. */ |
Waiman Long | 836bd74 | 2020-02-06 10:24:06 -0500 | [diff] [blame] | 5942 | WRITE_ONCE(chain->chain_key, INITIAL_CHAIN_KEY); |
Waiman Long | b3b9c18 | 2020-02-06 10:24:03 -0500 | [diff] [blame] | 5943 | dec_chains(chain->irq_context); |
| 5944 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5945 | /* |
| 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 Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame] | 5950 | __set_bit(chain - lock_chains, pf->lock_chains_being_freed); |
Waiman Long | 797b82eb | 2020-02-06 10:24:07 -0500 | [diff] [blame] | 5951 | nr_zapped_lock_chains++; |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5952 | #endif |
| 5953 | } |
| 5954 | |
| 5955 | /* Must be called with the graph lock held. */ |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame] | 5956 | static void remove_class_from_lock_chains(struct pending_free *pf, |
| 5957 | struct lock_class *class) |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5958 | { |
| 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 Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame] | 5966 | remove_class_from_lock_chain(pf, chain, class); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5967 | } |
| 5968 | } |
| 5969 | } |
| 5970 | |
Bart Van Assche | 786fa29 | 2018-12-06 17:11:36 -0800 | [diff] [blame] | 5971 | /* |
| 5972 | * Remove all references to a lock class. The caller must hold the graph lock. |
| 5973 | */ |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5974 | static void zap_class(struct pending_free *pf, struct lock_class *class) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5975 | { |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 5976 | struct lock_list *entry; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5977 | int i; |
| 5978 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5979 | WARN_ON_ONCE(!class->key); |
| 5980 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5981 | /* |
| 5982 | * Remove all dependencies this lock is |
| 5983 | * involved in: |
| 5984 | */ |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 5985 | for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) { |
| 5986 | entry = list_entries + i; |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 5987 | if (entry->class != class && entry->links_to != class) |
| 5988 | continue; |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 5989 | __clear_bit(i, list_entries_in_use); |
| 5990 | nr_list_entries--; |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 5991 | list_del_rcu(&entry->entry); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 5992 | } |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 5993 | 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 Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 6000 | __clear_bit(class - lock_classes, lock_classes_in_use); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6001 | } else { |
| 6002 | WARN_ONCE(true, "%s() failed for class %s\n", __func__, |
| 6003 | class->name); |
| 6004 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6005 | |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame] | 6006 | remove_class_from_lock_chains(pf, class); |
Waiman Long | 1d44bcb | 2020-02-06 10:24:05 -0500 | [diff] [blame] | 6007 | nr_zapped_classes++; |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6008 | } |
| 6009 | |
| 6010 | static 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6022 | } |
| 6023 | |
Arjan van de Ven | fabe874 | 2008-01-24 07:00:45 +0100 | [diff] [blame] | 6024 | static inline int within(const void *addr, void *start, unsigned long size) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6025 | { |
| 6026 | return addr >= start && addr < start + size; |
| 6027 | } |
| 6028 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6029 | static bool inside_selftest(void) |
| 6030 | { |
| 6031 | return current == lockdep_selftest_task_struct; |
| 6032 | } |
| 6033 | |
| 6034 | /* The caller must hold the graph lock. */ |
| 6035 | static struct pending_free *get_pending_free(void) |
| 6036 | { |
| 6037 | return delayed_free.pf + delayed_free.index; |
| 6038 | } |
| 6039 | |
| 6040 | static 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 | */ |
| 6046 | static 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. */ |
| 6065 | static void __free_zapped_classes(struct pending_free *pf) |
| 6066 | { |
| 6067 | struct lock_class *class; |
| 6068 | |
Peter Zijlstra | 72dcd50 | 2019-02-25 15:56:32 +0100 | [diff] [blame] | 6069 | check_data_structures(); |
Bart Van Assche | b526b2e | 2019-02-14 15:00:51 -0800 | [diff] [blame] | 6070 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6071 | 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 Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame] | 6075 | |
| 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 Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6081 | } |
| 6082 | |
| 6083 | static 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 Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 6092 | lockdep_lock(); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6093 | |
| 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 Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 6104 | lockdep_unlock(); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6105 | 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 | */ |
| 6114 | static void __lockdep_free_key_range(struct pending_free *pf, void *start, |
| 6115 | unsigned long size) |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 6116 | { |
| 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 Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6128 | zap_class(pf, class); |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 6129 | } |
| 6130 | } |
| 6131 | } |
| 6132 | |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 6133 | /* |
| 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 Assche | 29fc33f | 2019-02-14 15:00:45 -0800 | [diff] [blame] | 6137 | * 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 Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 6140 | */ |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6141 | static void lockdep_free_key_range_reg(void *start, unsigned long size) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6142 | { |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6143 | struct pending_free *pf; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6144 | unsigned long flags; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6145 | |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 6146 | init_data_structures_once(); |
| 6147 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6148 | raw_local_irq_save(flags); |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 6149 | lockdep_lock(); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6150 | pf = get_pending_free(); |
| 6151 | __lockdep_free_key_range(pf, start, size); |
| 6152 | call_rcu_zapped(pf); |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 6153 | lockdep_unlock(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6154 | raw_local_irq_restore(flags); |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 6155 | |
| 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 Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 6159 | */ |
Paul E. McKenney | 51959d8 | 2018-11-06 19:06:51 -0800 | [diff] [blame] | 6160 | synchronize_rcu(); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6161 | } |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 6162 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6163 | /* |
| 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 | */ |
| 6167 | static 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 Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 6175 | lockdep_lock(); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6176 | __lockdep_free_key_range(pf, start, size); |
| 6177 | __free_zapped_classes(pf); |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 6178 | lockdep_unlock(); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6179 | raw_local_irq_restore(flags); |
| 6180 | } |
| 6181 | |
| 6182 | void 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6190 | } |
| 6191 | |
Bart Van Assche | 2904d9f | 2018-12-06 17:11:34 -0800 | [diff] [blame] | 6192 | /* |
| 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 | */ |
| 6197 | static bool lock_class_cache_is_registered(struct lockdep_map *lock) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6198 | { |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 6199 | struct lock_class *class; |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 6200 | struct hlist_head *head; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6201 | int i, j; |
Bart Van Assche | 2904d9f | 2018-12-06 17:11:34 -0800 | [diff] [blame] | 6202 | |
| 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 Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 6214 | /* The caller must hold the graph lock. Does not sleep. */ |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6215 | static void __lockdep_reset_lock(struct pending_free *pf, |
| 6216 | struct lockdep_map *lock) |
Bart Van Assche | 2904d9f | 2018-12-06 17:11:34 -0800 | [diff] [blame] | 6217 | { |
| 6218 | struct lock_class *class; |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 6219 | int j; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6220 | |
| 6221 | /* |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 6222 | * Remove all classes this lock might have: |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6223 | */ |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 6224 | 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 Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 6229 | if (class) |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6230 | zap_class(pf, class); |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 6231 | } |
| 6232 | /* |
| 6233 | * Debug check: in the end all mapped classes should |
| 6234 | * be gone. |
| 6235 | */ |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 6236 | if (WARN_ON_ONCE(lock_class_cache_is_registered(lock))) |
| 6237 | debug_locks_off(); |
| 6238 | } |
| 6239 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6240 | /* |
| 6241 | * Remove all information lockdep has about a lock if debug_locks == 1. Free |
| 6242 | * released data structures from RCU context. |
| 6243 | */ |
| 6244 | static void lockdep_reset_lock_reg(struct lockdep_map *lock) |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 6245 | { |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6246 | struct pending_free *pf; |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 6247 | unsigned long flags; |
| 6248 | int locked; |
| 6249 | |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 6250 | raw_local_irq_save(flags); |
| 6251 | locked = graph_lock(); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6252 | 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(); |
| 6260 | out_irq: |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6261 | raw_local_irq_restore(flags); |
| 6262 | } |
| 6263 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6264 | /* |
| 6265 | * Reset a lock. Does not sleep. Ignores debug_locks. Must only be used by the |
| 6266 | * lockdep selftests. |
| 6267 | */ |
| 6268 | static 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 Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 6274 | lockdep_lock(); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6275 | __lockdep_reset_lock(pf, lock); |
| 6276 | __free_zapped_classes(pf); |
Peter Zijlstra | 248efb2 | 2020-03-13 11:09:49 +0100 | [diff] [blame] | 6277 | lockdep_unlock(); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6278 | raw_local_irq_restore(flags); |
| 6279 | } |
| 6280 | |
| 6281 | void 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 Assche | 108c148 | 2019-02-14 15:00:53 -0800 | [diff] [blame] | 6291 | /* Unregister a dynamically allocated key. */ |
| 6292 | void 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 Assche | 8b39adb | 2019-04-15 10:05:38 -0700 | [diff] [blame] | 6306 | if (!graph_lock()) |
| 6307 | goto out_irq; |
| 6308 | |
Bart Van Assche | 108c148 | 2019-02-14 15:00:53 -0800 | [diff] [blame] | 6309 | 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 Assche | 8b39adb | 2019-04-15 10:05:38 -0700 | [diff] [blame] | 6320 | graph_unlock(); |
| 6321 | out_irq: |
Bart Van Assche | 108c148 | 2019-02-14 15:00:53 -0800 | [diff] [blame] | 6322 | raw_local_irq_restore(flags); |
| 6323 | |
| 6324 | /* Wait until is_dynamic_key() has finished accessing k->hash_entry. */ |
| 6325 | synchronize_rcu(); |
| 6326 | } |
| 6327 | EXPORT_SYMBOL_GPL(lockdep_unregister_key); |
| 6328 | |
Joel Fernandes (Google) | c3bc8fd | 2018-07-30 15:24:23 -0700 | [diff] [blame] | 6329 | void __init lockdep_init(void) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6330 | { |
| 6331 | printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n"); |
| 6332 | |
Li Zefan | b0788ca | 2008-11-21 15:57:32 +0800 | [diff] [blame] | 6333 | printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6334 | printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH); |
| 6335 | printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS); |
Li Zefan | b0788ca | 2008-11-21 15:57:32 +0800 | [diff] [blame] | 6336 | printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6337 | 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 Assche | 09d75ec | 2019-02-14 15:00:36 -0800 | [diff] [blame] | 6341 | printk(" memory used by lock dependency info: %zu kB\n", |
Bart Van Assche | 7ff8517 | 2019-02-14 15:00:37 -0800 | [diff] [blame] | 6342 | (sizeof(lock_classes) + |
Yuyang Du | 01bb6f0 | 2019-05-06 16:19:25 +0800 | [diff] [blame] | 6343 | sizeof(lock_classes_in_use) + |
Bart Van Assche | 7ff8517 | 2019-02-14 15:00:37 -0800 | [diff] [blame] | 6344 | sizeof(classhash_table) + |
| 6345 | sizeof(list_entries) + |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 6346 | sizeof(list_entries_in_use) + |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 6347 | sizeof(chainhash_table) + |
| 6348 | sizeof(delayed_free) |
Ming Lei | 4dd861d | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 6349 | #ifdef CONFIG_PROVE_LOCKING |
Bart Van Assche | 7ff8517 | 2019-02-14 15:00:37 -0800 | [diff] [blame] | 6350 | + sizeof(lock_cq) |
Bart Van Assche | 15ea86b | 2019-02-14 15:00:38 -0800 | [diff] [blame] | 6351 | + sizeof(lock_chains) |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame] | 6352 | + sizeof(lock_chains_in_use) |
Bart Van Assche | 15ea86b | 2019-02-14 15:00:38 -0800 | [diff] [blame] | 6353 | + sizeof(chain_hlocks) |
Ming Lei | 4dd861d | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 6354 | #endif |
Ming Lei | 90629209 | 2009-08-02 21:43:36 +0800 | [diff] [blame] | 6355 | ) / 1024 |
Ming Lei | 4dd861d | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 6356 | ); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6357 | |
Bart Van Assche | 12593b7 | 2019-07-22 11:24:42 -0700 | [diff] [blame] | 6358 | #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 Assche | 09d75ec | 2019-02-14 15:00:36 -0800 | [diff] [blame] | 6364 | printk(" per task-struct memory footprint: %zu bytes\n", |
Bart Van Assche | 7ff8517 | 2019-02-14 15:00:37 -0800 | [diff] [blame] | 6365 | sizeof(((struct task_struct *)NULL)->held_locks)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6366 | } |
| 6367 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6368 | static void |
| 6369 | print_freed_lock_bug(struct task_struct *curr, const void *mem_from, |
Arjan van de Ven | 55794a4 | 2006-07-10 04:44:03 -0700 | [diff] [blame] | 6370 | const void *mem_to, struct held_lock *hlock) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6371 | { |
| 6372 | if (!debug_locks_off()) |
| 6373 | return; |
| 6374 | if (debug_locks_silent) |
| 6375 | return; |
| 6376 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 6377 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 6378 | pr_warn("=========================\n"); |
| 6379 | pr_warn("WARNING: held lock freed!\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 6380 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 6381 | pr_warn("-------------------------\n"); |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 6382 | pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6383 | curr->comm, task_pid_nr(curr), mem_from, mem_to-1); |
Arjan van de Ven | 55794a4 | 2006-07-10 04:44:03 -0700 | [diff] [blame] | 6384 | print_lock(hlock); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6385 | lockdep_print_held_locks(curr); |
| 6386 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 6387 | pr_warn("\nstack backtrace:\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6388 | dump_stack(); |
| 6389 | } |
| 6390 | |
Oleg Nesterov | 5456178 | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6391 | static 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 Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6398 | /* |
| 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 | */ |
| 6403 | void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len) |
| 6404 | { |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6405 | 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) | fcc784b | 2018-04-04 14:06:30 -0400 | [diff] [blame] | 6413 | raw_local_irq_save(flags); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6414 | for (i = 0; i < curr->lockdep_depth; i++) { |
| 6415 | hlock = curr->held_locks + i; |
| 6416 | |
Oleg Nesterov | 5456178 | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6417 | if (not_in_range(mem_from, mem_len, hlock->instance, |
| 6418 | sizeof(*hlock->instance))) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6419 | continue; |
| 6420 | |
Oleg Nesterov | 5456178 | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 6421 | print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6422 | break; |
| 6423 | } |
Steven Rostedt (VMware) | fcc784b | 2018-04-04 14:06:30 -0400 | [diff] [blame] | 6424 | raw_local_irq_restore(flags); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6425 | } |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 6426 | EXPORT_SYMBOL_GPL(debug_check_no_locks_freed); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6427 | |
Colin Cross | 1b1d2fb | 2013-05-06 23:50:08 +0000 | [diff] [blame] | 6428 | static void print_held_locks_bug(void) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6429 | { |
| 6430 | if (!debug_locks_off()) |
| 6431 | return; |
| 6432 | if (debug_locks_silent) |
| 6433 | return; |
| 6434 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 6435 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 6436 | pr_warn("====================================\n"); |
| 6437 | pr_warn("WARNING: %s/%d still has locks held!\n", |
Colin Cross | 1b1d2fb | 2013-05-06 23:50:08 +0000 | [diff] [blame] | 6438 | current->comm, task_pid_nr(current)); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 6439 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 6440 | pr_warn("------------------------------------\n"); |
Colin Cross | 1b1d2fb | 2013-05-06 23:50:08 +0000 | [diff] [blame] | 6441 | lockdep_print_held_locks(current); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 6442 | pr_warn("\nstack backtrace:\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6443 | dump_stack(); |
| 6444 | } |
| 6445 | |
Colin Cross | 1b1d2fb | 2013-05-06 23:50:08 +0000 | [diff] [blame] | 6446 | void debug_check_no_locks_held(void) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6447 | { |
Colin Cross | 1b1d2fb | 2013-05-06 23:50:08 +0000 | [diff] [blame] | 6448 | if (unlikely(current->lockdep_depth > 0)) |
| 6449 | print_held_locks_bug(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6450 | } |
Colin Cross | 1b1d2fb | 2013-05-06 23:50:08 +0000 | [diff] [blame] | 6451 | EXPORT_SYMBOL_GPL(debug_check_no_locks_held); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6452 | |
Sasha Levin | 8dce7a9 | 2013-06-13 18:41:16 -0400 | [diff] [blame] | 6453 | #ifdef __KERNEL__ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6454 | void debug_show_all_locks(void) |
| 6455 | { |
| 6456 | struct task_struct *g, *p; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6457 | |
Jarek Poplawski | 9c35dd7 | 2007-03-22 00:11:28 -0800 | [diff] [blame] | 6458 | if (unlikely(!debug_locks)) { |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 6459 | pr_warn("INFO: lockdep is turned off.\n"); |
Jarek Poplawski | 9c35dd7 | 2007-03-22 00:11:28 -0800 | [diff] [blame] | 6460 | return; |
| 6461 | } |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 6462 | pr_warn("\nShowing all locks held in the system:\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6463 | |
Tetsuo Handa | 0f736a5 | 2018-04-06 19:41:18 +0900 | [diff] [blame] | 6464 | rcu_read_lock(); |
| 6465 | for_each_process_thread(g, p) { |
Tetsuo Handa | 0f736a5 | 2018-04-06 19:41:18 +0900 | [diff] [blame] | 6466 | if (!p->lockdep_depth) |
| 6467 | continue; |
| 6468 | lockdep_print_held_locks(p); |
Tejun Heo | 88f1c87 | 2018-01-22 14:00:55 -0800 | [diff] [blame] | 6469 | touch_nmi_watchdog(); |
Tetsuo Handa | 0f736a5 | 2018-04-06 19:41:18 +0900 | [diff] [blame] | 6470 | touch_all_softlockup_watchdogs(); |
| 6471 | } |
| 6472 | rcu_read_unlock(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6473 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 6474 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 6475 | pr_warn("=============================================\n\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6476 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6477 | EXPORT_SYMBOL_GPL(debug_show_all_locks); |
Sasha Levin | 8dce7a9 | 2013-06-13 18:41:16 -0400 | [diff] [blame] | 6478 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6479 | |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 6480 | /* |
| 6481 | * Careful: only use this function if you are sure that |
| 6482 | * the task cannot run in parallel! |
| 6483 | */ |
John Kacur | f1b499f | 2010-08-05 17:10:53 +0200 | [diff] [blame] | 6484 | void debug_show_held_locks(struct task_struct *task) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6485 | { |
Jarek Poplawski | 9c35dd7 | 2007-03-22 00:11:28 -0800 | [diff] [blame] | 6486 | if (unlikely(!debug_locks)) { |
| 6487 | printk("INFO: lockdep is turned off.\n"); |
| 6488 | return; |
| 6489 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6490 | lockdep_print_held_locks(task); |
| 6491 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 6492 | EXPORT_SYMBOL_GPL(debug_show_held_locks); |
Peter Zijlstra | b351d16 | 2007-10-11 22:11:12 +0200 | [diff] [blame] | 6493 | |
Andi Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 6494 | asmlinkage __visible void lockdep_sys_exit(void) |
Peter Zijlstra | b351d16 | 2007-10-11 22:11:12 +0200 | [diff] [blame] | 6495 | { |
| 6496 | struct task_struct *curr = current; |
| 6497 | |
| 6498 | if (unlikely(curr->lockdep_depth)) { |
| 6499 | if (!debug_locks_off()) |
| 6500 | return; |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 6501 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 6502 | pr_warn("================================================\n"); |
| 6503 | pr_warn("WARNING: lock held when returning to user space!\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 6504 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 6505 | pr_warn("------------------------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 6506 | pr_warn("%s/%d is leaving the kernel with locks still held!\n", |
Peter Zijlstra | b351d16 | 2007-10-11 22:11:12 +0200 | [diff] [blame] | 6507 | curr->comm, curr->pid); |
| 6508 | lockdep_print_held_locks(curr); |
| 6509 | } |
Byungchul Park | b09be67 | 2017-08-07 16:12:52 +0900 | [diff] [blame] | 6510 | |
| 6511 | /* |
| 6512 | * The lock history for each syscall should be independent. So wipe the |
| 6513 | * slate clean on return to userspace. |
| 6514 | */ |
Peter Zijlstra | f52be57 | 2017-08-29 10:59:39 +0200 | [diff] [blame] | 6515 | lockdep_invariant_state(false); |
Peter Zijlstra | b351d16 | 2007-10-11 22:11:12 +0200 | [diff] [blame] | 6516 | } |
Paul E. McKenney | 0632eb3 | 2010-02-22 17:04:47 -0800 | [diff] [blame] | 6517 | |
Paul E. McKenney | b3fbab0 | 2011-05-24 08:31:09 -0700 | [diff] [blame] | 6518 | void lockdep_rcu_suspicious(const char *file, const int line, const char *s) |
Paul E. McKenney | 0632eb3 | 2010-02-22 17:04:47 -0800 | [diff] [blame] | 6519 | { |
| 6520 | struct task_struct *curr = current; |
Paul E. McKenney | 1feb2cc | 2021-04-05 09:47:59 -0700 | [diff] [blame] | 6521 | int dl = READ_ONCE(debug_locks); |
Paul E. McKenney | 0632eb3 | 2010-02-22 17:04:47 -0800 | [diff] [blame] | 6522 | |
Lai Jiangshan | 2b3fc35 | 2010-04-20 16:23:07 +0800 | [diff] [blame] | 6523 | /* Note: the following can be executed concurrently, so be careful. */ |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 6524 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 6525 | pr_warn("=============================\n"); |
| 6526 | pr_warn("WARNING: suspicious RCU usage\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 6527 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 6528 | pr_warn("-----------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 6529 | pr_warn("%s:%d %s!\n", file, line, s); |
| 6530 | pr_warn("\nother info that might help us debug this:\n\n"); |
Paul E. McKenney | 1feb2cc | 2021-04-05 09:47:59 -0700 | [diff] [blame] | 6531 | pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n%s", |
Paul E. McKenney | c5fdcec | 2012-01-30 08:46:32 -0800 | [diff] [blame] | 6532 | !rcu_lockdep_current_cpu_online() |
| 6533 | ? "RCU used illegally from offline CPU!\n" |
Paul E. McKenney | d29e0b2 | 2020-05-28 08:49:29 -0700 | [diff] [blame] | 6534 | : "", |
Paul E. McKenney | 1feb2cc | 2021-04-05 09:47:59 -0700 | [diff] [blame] | 6535 | rcu_scheduler_active, dl, |
| 6536 | dl ? "" : "Possible false positive due to lockdep disabling via debug_locks = 0\n"); |
Frederic Weisbecker | 0464e93 | 2011-10-07 18:22:01 +0200 | [diff] [blame] | 6537 | |
| 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. McKenney | 5c173eb | 2013-09-13 17:20:11 -0700 | [diff] [blame] | 6556 | if (!rcu_is_watching()) |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 6557 | pr_warn("RCU used illegally from extended quiescent state!\n"); |
Frederic Weisbecker | 0464e93 | 2011-10-07 18:22:01 +0200 | [diff] [blame] | 6558 | |
Paul E. McKenney | 0632eb3 | 2010-02-22 17:04:47 -0800 | [diff] [blame] | 6559 | lockdep_print_held_locks(curr); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 6560 | pr_warn("\nstack backtrace:\n"); |
Paul E. McKenney | 0632eb3 | 2010-02-22 17:04:47 -0800 | [diff] [blame] | 6561 | dump_stack(); |
| 6562 | } |
Paul E. McKenney | b3fbab0 | 2011-05-24 08:31:09 -0700 | [diff] [blame] | 6563 | EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious); |