Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * kernel/lockdep.c |
| 3 | * |
| 4 | * Runtime locking correctness validator |
| 5 | * |
| 6 | * Started by Ingo Molnar: |
| 7 | * |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 8 | * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
Peter Zijlstra | 90eec10 | 2015-11-16 11:08:45 +0100 | [diff] [blame] | 9 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 10 | * |
| 11 | * this code maps all the lock dependencies as they occur in a live kernel |
| 12 | * and will warn about the following classes of locking bugs: |
| 13 | * |
| 14 | * - lock inversion scenarios |
| 15 | * - circular lock dependencies |
| 16 | * - hardirq/softirq safe/unsafe locking bugs |
| 17 | * |
| 18 | * Bugs are reported even if the current locking scenario does not cause |
| 19 | * any deadlock at this point. |
| 20 | * |
| 21 | * I.e. if anytime in the past two locks were taken in a different order, |
| 22 | * even if it happened for another task, even if those were different |
| 23 | * locks (but of the same class as this lock), this code will detect it. |
| 24 | * |
| 25 | * Thanks to Arjan van de Ven for coming up with the initial idea of |
| 26 | * mapping lock dependencies runtime. |
| 27 | */ |
Steven Rostedt | a5e2588 | 2008-12-02 15:34:05 -0500 | [diff] [blame] | 28 | #define DISABLE_BRANCH_PROFILING |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 29 | #include <linux/mutex.h> |
| 30 | #include <linux/sched.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 31 | #include <linux/sched/clock.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 32 | #include <linux/sched/task.h> |
Nikolay Borisov | 6d7225f | 2017-05-03 14:53:05 -0700 | [diff] [blame] | 33 | #include <linux/sched/mm.h> |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 34 | #include <linux/delay.h> |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/proc_fs.h> |
| 37 | #include <linux/seq_file.h> |
| 38 | #include <linux/spinlock.h> |
| 39 | #include <linux/kallsyms.h> |
| 40 | #include <linux/interrupt.h> |
| 41 | #include <linux/stacktrace.h> |
| 42 | #include <linux/debug_locks.h> |
| 43 | #include <linux/irqflags.h> |
Dave Jones | 99de055 | 2006-09-29 02:00:10 -0700 | [diff] [blame] | 44 | #include <linux/utsname.h> |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 45 | #include <linux/hash.h> |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 46 | #include <linux/ftrace.h> |
Peter Zijlstra | b4b136f | 2009-01-29 14:50:36 +0100 | [diff] [blame] | 47 | #include <linux/stringify.h> |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 48 | #include <linux/bitmap.h> |
Ming Lei | d588e46 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 49 | #include <linux/bitops.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 50 | #include <linux/gfp.h> |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 51 | #include <linux/random.h> |
Peter Zijlstra | dfaaf3f | 2016-05-30 18:31:33 +0200 | [diff] [blame] | 52 | #include <linux/jhash.h> |
Tejun Heo | 88f1c87 | 2018-01-22 14:00:55 -0800 | [diff] [blame] | 53 | #include <linux/nmi.h> |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 54 | #include <linux/rcupdate.h> |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 55 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 56 | #include <asm/sections.h> |
| 57 | |
| 58 | #include "lockdep_internals.h" |
| 59 | |
Steven Rostedt | a8d154b | 2009-04-10 09:36:00 -0400 | [diff] [blame] | 60 | #define CREATE_TRACE_POINTS |
Frederic Weisbecker | 6717876 | 2009-11-13 10:06:34 +0100 | [diff] [blame] | 61 | #include <trace/events/lock.h> |
Steven Rostedt | a8d154b | 2009-04-10 09:36:00 -0400 | [diff] [blame] | 62 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 63 | #ifdef CONFIG_PROVE_LOCKING |
| 64 | int prove_locking = 1; |
| 65 | module_param(prove_locking, int, 0644); |
| 66 | #else |
| 67 | #define prove_locking 0 |
| 68 | #endif |
| 69 | |
| 70 | #ifdef CONFIG_LOCK_STAT |
| 71 | int lock_stat = 1; |
| 72 | module_param(lock_stat, int, 0644); |
| 73 | #else |
| 74 | #define lock_stat 0 |
| 75 | #endif |
| 76 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 77 | /* |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 78 | * lockdep_lock: protects the lockdep graph, the hashes and the |
| 79 | * class/list/hash allocators. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 80 | * |
| 81 | * This is one of the rare exceptions where it's justified |
| 82 | * to use a raw spinlock - we really dont want the spinlock |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 83 | * code to recurse back into the lockdep code... |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 84 | */ |
Thomas Gleixner | edc35bd | 2009-12-03 12:38:57 +0100 | [diff] [blame] | 85 | static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; |
Bart Van Assche | cdc84d7 | 2019-02-14 15:00:44 -0800 | [diff] [blame] | 86 | static struct task_struct *lockdep_selftest_task_struct; |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 87 | |
| 88 | static int graph_lock(void) |
| 89 | { |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 90 | arch_spin_lock(&lockdep_lock); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 91 | /* |
| 92 | * Make sure that if another CPU detected a bug while |
| 93 | * walking the graph we dont change it (while the other |
| 94 | * CPU is busy printing out stuff with the graph lock |
| 95 | * dropped already) |
| 96 | */ |
| 97 | if (!debug_locks) { |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 98 | arch_spin_unlock(&lockdep_lock); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 99 | return 0; |
| 100 | } |
Steven Rostedt | bb065af | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 101 | /* prevent any recursions within lockdep from causing deadlocks */ |
| 102 | current->lockdep_recursion++; |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | static inline int graph_unlock(void) |
| 107 | { |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 108 | if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) { |
| 109 | /* |
| 110 | * The lockdep graph lock isn't locked while we expect it to |
| 111 | * be, we're confused now, bye! |
| 112 | */ |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 113 | return DEBUG_LOCKS_WARN_ON(1); |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 114 | } |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 115 | |
Steven Rostedt | bb065af | 2008-05-12 21:21:00 +0200 | [diff] [blame] | 116 | current->lockdep_recursion--; |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 117 | arch_spin_unlock(&lockdep_lock); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Turn lock debugging off and return with 0 if it was off already, |
| 123 | * and also release the graph lock: |
| 124 | */ |
| 125 | static inline int debug_locks_off_graph_unlock(void) |
| 126 | { |
| 127 | int ret = debug_locks_off(); |
| 128 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 129 | arch_spin_unlock(&lockdep_lock); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 130 | |
| 131 | return ret; |
| 132 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 133 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 134 | unsigned long nr_list_entries; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 135 | static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES]; |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 136 | static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 137 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 138 | /* |
| 139 | * All data structures here are protected by the global debug_lock. |
| 140 | * |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 141 | * nr_lock_classes is the number of elements of lock_classes[] that is |
| 142 | * in use. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 143 | */ |
| 144 | unsigned long nr_lock_classes; |
Bart Van Assche | 1431a5d | 2018-12-06 17:11:32 -0800 | [diff] [blame] | 145 | #ifndef CONFIG_DEBUG_LOCKDEP |
| 146 | static |
| 147 | #endif |
Waiman Long | 8ca2b56c | 2018-10-03 13:07:18 -0400 | [diff] [blame] | 148 | struct lock_class lock_classes[MAX_LOCKDEP_KEYS]; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 149 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 150 | static inline struct lock_class *hlock_class(struct held_lock *hlock) |
| 151 | { |
| 152 | if (!hlock->class_idx) { |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 153 | /* |
| 154 | * Someone passed in garbage, we give up. |
| 155 | */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 156 | DEBUG_LOCKS_WARN_ON(1); |
| 157 | return NULL; |
| 158 | } |
| 159 | return lock_classes + hlock->class_idx - 1; |
| 160 | } |
| 161 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 162 | #ifdef CONFIG_LOCK_STAT |
Peter Zijlstra | 2552821 | 2016-03-15 14:52:49 -0700 | [diff] [blame] | 163 | 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] | 164 | |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 165 | static inline u64 lockstat_clock(void) |
| 166 | { |
Peter Zijlstra | c676329 | 2010-05-25 10:48:51 +0200 | [diff] [blame] | 167 | return local_clock(); |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 170 | static int lock_point(unsigned long points[], unsigned long ip) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 171 | { |
| 172 | int i; |
| 173 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 174 | for (i = 0; i < LOCKSTAT_POINTS; i++) { |
| 175 | if (points[i] == 0) { |
| 176 | points[i] = ip; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 177 | break; |
| 178 | } |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 179 | if (points[i] == ip) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 180 | break; |
| 181 | } |
| 182 | |
| 183 | return i; |
| 184 | } |
| 185 | |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 186 | static void lock_time_inc(struct lock_time *lt, u64 time) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 187 | { |
| 188 | if (time > lt->max) |
| 189 | lt->max = time; |
| 190 | |
Frank Rowand | 109d71c | 2009-11-19 13:42:06 -0800 | [diff] [blame] | 191 | if (time < lt->min || !lt->nr) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 192 | lt->min = time; |
| 193 | |
| 194 | lt->total += time; |
| 195 | lt->nr++; |
| 196 | } |
| 197 | |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 198 | static inline void lock_time_add(struct lock_time *src, struct lock_time *dst) |
| 199 | { |
Frank Rowand | 109d71c | 2009-11-19 13:42:06 -0800 | [diff] [blame] | 200 | if (!src->nr) |
| 201 | return; |
| 202 | |
| 203 | if (src->max > dst->max) |
| 204 | dst->max = src->max; |
| 205 | |
| 206 | if (src->min < dst->min || !dst->nr) |
| 207 | dst->min = src->min; |
| 208 | |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 209 | dst->total += src->total; |
| 210 | dst->nr += src->nr; |
| 211 | } |
| 212 | |
| 213 | struct lock_class_stats lock_stats(struct lock_class *class) |
| 214 | { |
| 215 | struct lock_class_stats stats; |
| 216 | int cpu, i; |
| 217 | |
| 218 | memset(&stats, 0, sizeof(struct lock_class_stats)); |
| 219 | for_each_possible_cpu(cpu) { |
| 220 | struct lock_class_stats *pcs = |
Tejun Heo | 1871e52 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 221 | &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 222 | |
| 223 | for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++) |
| 224 | stats.contention_point[i] += pcs->contention_point[i]; |
| 225 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 226 | for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++) |
| 227 | stats.contending_point[i] += pcs->contending_point[i]; |
| 228 | |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 229 | lock_time_add(&pcs->read_waittime, &stats.read_waittime); |
| 230 | lock_time_add(&pcs->write_waittime, &stats.write_waittime); |
| 231 | |
| 232 | lock_time_add(&pcs->read_holdtime, &stats.read_holdtime); |
| 233 | lock_time_add(&pcs->write_holdtime, &stats.write_holdtime); |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 234 | |
| 235 | for (i = 0; i < ARRAY_SIZE(stats.bounces); i++) |
| 236 | stats.bounces[i] += pcs->bounces[i]; |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | return stats; |
| 240 | } |
| 241 | |
| 242 | void clear_lock_stats(struct lock_class *class) |
| 243 | { |
| 244 | int cpu; |
| 245 | |
| 246 | for_each_possible_cpu(cpu) { |
| 247 | struct lock_class_stats *cpu_stats = |
Tejun Heo | 1871e52 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 248 | &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 249 | |
| 250 | memset(cpu_stats, 0, sizeof(struct lock_class_stats)); |
| 251 | } |
| 252 | memset(class->contention_point, 0, sizeof(class->contention_point)); |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 253 | memset(class->contending_point, 0, sizeof(class->contending_point)); |
Peter Zijlstra | c46261d | 2007-07-19 01:48:57 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 256 | static struct lock_class_stats *get_lock_stats(struct lock_class *class) |
| 257 | { |
Joel Fernandes (Google) | 01f3849 | 2018-07-30 15:24:21 -0700 | [diff] [blame] | 258 | return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes]; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static void lock_release_holdtime(struct held_lock *hlock) |
| 262 | { |
| 263 | struct lock_class_stats *stats; |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 264 | u64 holdtime; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 265 | |
| 266 | if (!lock_stat) |
| 267 | return; |
| 268 | |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 269 | holdtime = lockstat_clock() - hlock->holdtime_stamp; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 270 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 271 | stats = get_lock_stats(hlock_class(hlock)); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 272 | if (hlock->read) |
| 273 | lock_time_inc(&stats->read_holdtime, holdtime); |
| 274 | else |
| 275 | lock_time_inc(&stats->write_holdtime, holdtime); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 276 | } |
| 277 | #else |
| 278 | static inline void lock_release_holdtime(struct held_lock *hlock) |
| 279 | { |
| 280 | } |
| 281 | #endif |
| 282 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 283 | /* |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 284 | * We keep a global list of all lock classes. The list is only accessed with |
| 285 | * the lockdep spinlock lock held. free_lock_classes is a list with free |
| 286 | * elements. These elements are linked together by the lock_entry member in |
| 287 | * struct lock_class. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 288 | */ |
| 289 | LIST_HEAD(all_lock_classes); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 290 | static LIST_HEAD(free_lock_classes); |
| 291 | |
| 292 | /** |
| 293 | * struct pending_free - information about data structures about to be freed |
| 294 | * @zapped: Head of a list with struct lock_class elements. |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame^] | 295 | * @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements |
| 296 | * are about to be freed. |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 297 | */ |
| 298 | struct pending_free { |
| 299 | struct list_head zapped; |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame^] | 300 | DECLARE_BITMAP(lock_chains_being_freed, MAX_LOCKDEP_CHAINS); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | /** |
| 304 | * struct delayed_free - data structures used for delayed freeing |
| 305 | * |
| 306 | * A data structure for delayed freeing of data structures that may be |
| 307 | * accessed by RCU readers at the time these were freed. |
| 308 | * |
| 309 | * @rcu_head: Used to schedule an RCU callback for freeing data structures. |
| 310 | * @index: Index of @pf to which freed data structures are added. |
| 311 | * @scheduled: Whether or not an RCU callback has been scheduled. |
| 312 | * @pf: Array with information about data structures about to be freed. |
| 313 | */ |
| 314 | static struct delayed_free { |
| 315 | struct rcu_head rcu_head; |
| 316 | int index; |
| 317 | int scheduled; |
| 318 | struct pending_free pf[2]; |
| 319 | } delayed_free; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 320 | |
| 321 | /* |
| 322 | * The lockdep classes are in a hash-table as well, for fast lookup: |
| 323 | */ |
| 324 | #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1) |
| 325 | #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS) |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 326 | #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 327 | #define classhashentry(key) (classhash_table + __classhashfn((key))) |
| 328 | |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 329 | static struct hlist_head classhash_table[CLASSHASH_SIZE]; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 330 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 331 | /* |
| 332 | * We put the lock dependency chains into a hash-table as well, to cache |
| 333 | * their existence: |
| 334 | */ |
| 335 | #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1) |
| 336 | #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS) |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 337 | #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 338 | #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain))) |
| 339 | |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 340 | static struct hlist_head chainhash_table[CHAINHASH_SIZE]; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 341 | |
| 342 | /* |
| 343 | * The hash key of the lock dependency chains is a hash itself too: |
| 344 | * it's a hash of all locks taken up to that lock, including that lock. |
| 345 | * It's a 64-bit hash, because it's important for the keys to be |
| 346 | * unique. |
| 347 | */ |
Peter Zijlstra | dfaaf3f | 2016-05-30 18:31:33 +0200 | [diff] [blame] | 348 | static inline u64 iterate_chain_key(u64 key, u32 idx) |
| 349 | { |
| 350 | u32 k0 = key, k1 = key >> 32; |
| 351 | |
| 352 | __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */ |
| 353 | |
| 354 | return k0 | (u64)k1 << 32; |
| 355 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 356 | |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 357 | void lockdep_off(void) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 358 | { |
| 359 | current->lockdep_recursion++; |
| 360 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 361 | EXPORT_SYMBOL(lockdep_off); |
| 362 | |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 363 | void lockdep_on(void) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 364 | { |
| 365 | current->lockdep_recursion--; |
| 366 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 367 | EXPORT_SYMBOL(lockdep_on); |
| 368 | |
Bart Van Assche | cdc84d7 | 2019-02-14 15:00:44 -0800 | [diff] [blame] | 369 | void lockdep_set_selftest_task(struct task_struct *task) |
| 370 | { |
| 371 | lockdep_selftest_task_struct = task; |
| 372 | } |
| 373 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 374 | /* |
| 375 | * Debugging switches: |
| 376 | */ |
| 377 | |
| 378 | #define VERBOSE 0 |
Ingo Molnar | 33e94e9 | 2006-12-13 00:34:41 -0800 | [diff] [blame] | 379 | #define VERY_VERBOSE 0 |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 380 | |
| 381 | #if VERBOSE |
| 382 | # define HARDIRQ_VERBOSE 1 |
| 383 | # define SOFTIRQ_VERBOSE 1 |
| 384 | #else |
| 385 | # define HARDIRQ_VERBOSE 0 |
| 386 | # define SOFTIRQ_VERBOSE 0 |
| 387 | #endif |
| 388 | |
Peter Zijlstra | d92a8cf | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 389 | #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 390 | /* |
| 391 | * Quick filtering for interesting events: |
| 392 | */ |
| 393 | static int class_filter(struct lock_class *class) |
| 394 | { |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 395 | #if 0 |
| 396 | /* Example */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 397 | if (class->name_version == 1 && |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 398 | !strcmp(class->name, "lockname")) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 399 | return 1; |
| 400 | if (class->name_version == 1 && |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 401 | !strcmp(class->name, "&struct->lockfield")) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 402 | return 1; |
Andi Kleen | f9829cc | 2006-07-10 04:44:01 -0700 | [diff] [blame] | 403 | #endif |
Ingo Molnar | a664089 | 2006-12-13 00:34:39 -0800 | [diff] [blame] | 404 | /* Filter everything else. 1 would be to allow everything else */ |
| 405 | return 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 406 | } |
| 407 | #endif |
| 408 | |
| 409 | static int verbose(struct lock_class *class) |
| 410 | { |
| 411 | #if VERBOSE |
| 412 | return class_filter(class); |
| 413 | #endif |
| 414 | return 0; |
| 415 | } |
| 416 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 417 | /* |
| 418 | * Stack-trace: tightly packed array of stack backtrace |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 419 | * addresses. Protected by the graph_lock. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 420 | */ |
| 421 | unsigned long nr_stack_trace_entries; |
| 422 | static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES]; |
| 423 | |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 424 | static void print_lockdep_off(const char *bug_msg) |
| 425 | { |
| 426 | printk(KERN_DEBUG "%s\n", bug_msg); |
| 427 | printk(KERN_DEBUG "turning off the locking correctness validator.\n"); |
Andreas Gruenbacher | acf5937 | 2014-07-15 21:10:52 +0200 | [diff] [blame] | 428 | #ifdef CONFIG_LOCK_STAT |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 429 | 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] | 430 | #endif |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 433 | static int save_trace(struct stack_trace *trace) |
| 434 | { |
| 435 | trace->nr_entries = 0; |
| 436 | trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries; |
| 437 | trace->entries = stack_trace + nr_stack_trace_entries; |
| 438 | |
Andi Kleen | 5a1b399 | 2006-09-26 10:52:34 +0200 | [diff] [blame] | 439 | trace->skip = 3; |
Andi Kleen | 5a1b399 | 2006-09-26 10:52:34 +0200 | [diff] [blame] | 440 | |
Christoph Hellwig | ab1b6f0 | 2007-05-08 00:23:29 -0700 | [diff] [blame] | 441 | save_stack_trace(trace); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 442 | |
Peter Zijlstra | 4f84f43 | 2009-07-20 15:27:04 +0200 | [diff] [blame] | 443 | /* |
| 444 | * Some daft arches put -1 at the end to indicate its a full trace. |
| 445 | * |
| 446 | * <rant> this is buggy anyway, since it takes a whole extra entry so a |
| 447 | * complete trace that maxes out the entries provided will be reported |
| 448 | * as incomplete, friggin useless </rant> |
| 449 | */ |
Luck, Tony | ea5b41f | 2009-12-09 14:29:36 -0800 | [diff] [blame] | 450 | if (trace->nr_entries != 0 && |
| 451 | trace->entries[trace->nr_entries-1] == ULONG_MAX) |
Peter Zijlstra | 4f84f43 | 2009-07-20 15:27:04 +0200 | [diff] [blame] | 452 | trace->nr_entries--; |
| 453 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 454 | trace->max_entries = trace->nr_entries; |
| 455 | |
| 456 | nr_stack_trace_entries += trace->nr_entries; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 457 | |
Peter Zijlstra | 4f84f43 | 2009-07-20 15:27:04 +0200 | [diff] [blame] | 458 | if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 459 | if (!debug_locks_off_graph_unlock()) |
| 460 | return 0; |
| 461 | |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 462 | print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!"); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 463 | dump_stack(); |
| 464 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | return 1; |
| 469 | } |
| 470 | |
| 471 | unsigned int nr_hardirq_chains; |
| 472 | unsigned int nr_softirq_chains; |
| 473 | unsigned int nr_process_chains; |
| 474 | unsigned int max_lockdep_depth; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 475 | |
| 476 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 477 | /* |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 478 | * Various lockdep statistics: |
| 479 | */ |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 480 | DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 481 | #endif |
| 482 | |
| 483 | /* |
| 484 | * Locking printouts: |
| 485 | */ |
| 486 | |
Peter Zijlstra | fabe9c4 | 2009-01-22 14:51:01 +0100 | [diff] [blame] | 487 | #define __USAGE(__STATE) \ |
Peter Zijlstra | b4b136f | 2009-01-29 14:50:36 +0100 | [diff] [blame] | 488 | [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \ |
| 489 | [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \ |
| 490 | [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\ |
| 491 | [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R", |
Peter Zijlstra | fabe9c4 | 2009-01-22 14:51:01 +0100 | [diff] [blame] | 492 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 493 | static const char *usage_str[] = |
| 494 | { |
Peter Zijlstra | fabe9c4 | 2009-01-22 14:51:01 +0100 | [diff] [blame] | 495 | #define LOCKDEP_STATE(__STATE) __USAGE(__STATE) |
| 496 | #include "lockdep_states.h" |
| 497 | #undef LOCKDEP_STATE |
| 498 | [LOCK_USED] = "INITIAL USE", |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 499 | }; |
| 500 | |
| 501 | const char * __get_key_name(struct lockdep_subclass_key *key, char *str) |
| 502 | { |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 503 | return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Peter Zijlstra | 3ff176c | 2009-01-22 17:40:42 +0100 | [diff] [blame] | 506 | static inline unsigned long lock_flag(enum lock_usage_bit bit) |
| 507 | { |
| 508 | return 1UL << bit; |
| 509 | } |
| 510 | |
| 511 | static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit) |
| 512 | { |
| 513 | char c = '.'; |
| 514 | |
| 515 | if (class->usage_mask & lock_flag(bit + 2)) |
| 516 | c = '+'; |
| 517 | if (class->usage_mask & lock_flag(bit)) { |
| 518 | c = '-'; |
| 519 | if (class->usage_mask & lock_flag(bit + 2)) |
| 520 | c = '?'; |
| 521 | } |
| 522 | |
| 523 | return c; |
| 524 | } |
| 525 | |
Peter Zijlstra | f510b23 | 2009-01-22 17:53:47 +0100 | [diff] [blame] | 526 | 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] | 527 | { |
Peter Zijlstra | f510b23 | 2009-01-22 17:53:47 +0100 | [diff] [blame] | 528 | int i = 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 529 | |
Peter Zijlstra | f510b23 | 2009-01-22 17:53:47 +0100 | [diff] [blame] | 530 | #define LOCKDEP_STATE(__STATE) \ |
| 531 | usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \ |
| 532 | usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ); |
| 533 | #include "lockdep_states.h" |
| 534 | #undef LOCKDEP_STATE |
| 535 | |
| 536 | usage[i] = '\0'; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Steven Rostedt | e5e78d0 | 2011-11-02 20:24:16 -0400 | [diff] [blame] | 539 | static void __print_lock_name(struct lock_class *class) |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 540 | { |
| 541 | char str[KSYM_NAME_LEN]; |
| 542 | const char *name; |
| 543 | |
| 544 | name = class->name; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 545 | if (!name) { |
| 546 | name = __get_key_name(class->key, str); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 547 | printk(KERN_CONT "%s", name); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 548 | } else { |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 549 | printk(KERN_CONT "%s", name); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 550 | if (class->name_version > 1) |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 551 | printk(KERN_CONT "#%d", class->name_version); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 552 | if (class->subclass) |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 553 | printk(KERN_CONT "/%d", class->subclass); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 554 | } |
Steven Rostedt | e5e78d0 | 2011-11-02 20:24:16 -0400 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | static void print_lock_name(struct lock_class *class) |
| 558 | { |
| 559 | char usage[LOCK_USAGE_CHARS]; |
| 560 | |
| 561 | get_usage_chars(class, usage); |
| 562 | |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 563 | printk(KERN_CONT " ("); |
Steven Rostedt | e5e78d0 | 2011-11-02 20:24:16 -0400 | [diff] [blame] | 564 | __print_lock_name(class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 565 | printk(KERN_CONT "){%s}", usage); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static void print_lockdep_cache(struct lockdep_map *lock) |
| 569 | { |
| 570 | const char *name; |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 571 | char str[KSYM_NAME_LEN]; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 572 | |
| 573 | name = lock->name; |
| 574 | if (!name) |
| 575 | name = __get_key_name(lock->key->subkeys, str); |
| 576 | |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 577 | printk(KERN_CONT "%s", name); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static void print_lock(struct held_lock *hlock) |
| 581 | { |
Peter Zijlstra | d7bc319 | 2015-04-15 17:11:57 +0200 | [diff] [blame] | 582 | /* |
| 583 | * We can be called locklessly through debug_show_all_locks() so be |
| 584 | * extra careful, the hlock might have been released and cleared. |
| 585 | */ |
| 586 | unsigned int class_idx = hlock->class_idx; |
| 587 | |
| 588 | /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */ |
| 589 | barrier(); |
| 590 | |
| 591 | if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) { |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 592 | printk(KERN_CONT "<RELEASED>\n"); |
Peter Zijlstra | d7bc319 | 2015-04-15 17:11:57 +0200 | [diff] [blame] | 593 | return; |
| 594 | } |
| 595 | |
Tetsuo Handa | b3c3975 | 2018-03-27 19:41:41 +0900 | [diff] [blame] | 596 | printk(KERN_CONT "%p", hlock->instance); |
Peter Zijlstra | d7bc319 | 2015-04-15 17:11:57 +0200 | [diff] [blame] | 597 | print_lock_name(lock_classes + class_idx - 1); |
Tetsuo Handa | b3c3975 | 2018-03-27 19:41:41 +0900 | [diff] [blame] | 598 | printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Tetsuo Handa | 8cc05c71 | 2018-04-06 19:41:19 +0900 | [diff] [blame] | 601 | static void lockdep_print_held_locks(struct task_struct *p) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 602 | { |
Tetsuo Handa | 8cc05c71 | 2018-04-06 19:41:19 +0900 | [diff] [blame] | 603 | int i, depth = READ_ONCE(p->lockdep_depth); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 604 | |
Tetsuo Handa | 8cc05c71 | 2018-04-06 19:41:19 +0900 | [diff] [blame] | 605 | if (!depth) |
| 606 | printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p)); |
| 607 | else |
| 608 | printk("%d lock%s held by %s/%d:\n", depth, |
| 609 | depth > 1 ? "s" : "", p->comm, task_pid_nr(p)); |
| 610 | /* |
| 611 | * It's not reliable to print a task's held locks if it's not sleeping |
| 612 | * and it's not the current task. |
| 613 | */ |
| 614 | if (p->state == TASK_RUNNING && p != current) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 615 | return; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 616 | for (i = 0; i < depth; i++) { |
| 617 | printk(" #%d: ", i); |
Tetsuo Handa | 8cc05c71 | 2018-04-06 19:41:19 +0900 | [diff] [blame] | 618 | print_lock(p->held_locks + i); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 621 | |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 622 | static void print_kernel_ident(void) |
Dave Jones | 99de055 | 2006-09-29 02:00:10 -0700 | [diff] [blame] | 623 | { |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 624 | printk("%s %.*s %s\n", init_utsname()->release, |
Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 625 | (int)strcspn(init_utsname()->version, " "), |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 626 | init_utsname()->version, |
| 627 | print_tainted()); |
Dave Jones | 99de055 | 2006-09-29 02:00:10 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 630 | static int very_verbose(struct lock_class *class) |
| 631 | { |
| 632 | #if VERY_VERBOSE |
| 633 | return class_filter(class); |
| 634 | #endif |
| 635 | return 0; |
| 636 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * Is this the address of a static object: |
| 640 | */ |
Sasha Levin | 8dce7a9 | 2013-06-13 18:41:16 -0400 | [diff] [blame] | 641 | #ifdef __KERNEL__ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 642 | static int static_obj(void *obj) |
| 643 | { |
| 644 | unsigned long start = (unsigned long) &_stext, |
| 645 | end = (unsigned long) &_end, |
| 646 | addr = (unsigned long) obj; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 647 | |
| 648 | /* |
| 649 | * static variable? |
| 650 | */ |
| 651 | if ((addr >= start) && (addr < end)) |
| 652 | return 1; |
| 653 | |
Mike Frysinger | 2a9ad18 | 2009-09-22 16:44:16 -0700 | [diff] [blame] | 654 | if (arch_is_kernel_data(addr)) |
| 655 | return 1; |
| 656 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 657 | /* |
Tejun Heo | 10fad5e | 2010-03-10 18:57:54 +0900 | [diff] [blame] | 658 | * in-kernel percpu var? |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 659 | */ |
Tejun Heo | 10fad5e | 2010-03-10 18:57:54 +0900 | [diff] [blame] | 660 | if (is_kernel_percpu_address(addr)) |
| 661 | return 1; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 662 | |
| 663 | /* |
Tejun Heo | 10fad5e | 2010-03-10 18:57:54 +0900 | [diff] [blame] | 664 | * module static or percpu var? |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 665 | */ |
Tejun Heo | 10fad5e | 2010-03-10 18:57:54 +0900 | [diff] [blame] | 666 | return is_module_address(addr) || is_module_percpu_address(addr); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 667 | } |
Sasha Levin | 8dce7a9 | 2013-06-13 18:41:16 -0400 | [diff] [blame] | 668 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 669 | |
| 670 | /* |
| 671 | * To make lock name printouts unique, we calculate a unique |
Bart Van Assche | fe27b0d | 2018-12-06 17:11:37 -0800 | [diff] [blame] | 672 | * class->name_version generation counter. The caller must hold the graph |
| 673 | * lock. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 674 | */ |
| 675 | static int count_matching_names(struct lock_class *new_class) |
| 676 | { |
| 677 | struct lock_class *class; |
| 678 | int count = 0; |
| 679 | |
| 680 | if (!new_class->name) |
| 681 | return 0; |
| 682 | |
Bart Van Assche | fe27b0d | 2018-12-06 17:11:37 -0800 | [diff] [blame] | 683 | list_for_each_entry(class, &all_lock_classes, lock_entry) { |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 684 | if (new_class->key - new_class->subclass == class->key) |
| 685 | return class->name_version; |
| 686 | if (class->name && !strcmp(class->name, new_class->name)) |
| 687 | count = max(count, class->name_version); |
| 688 | } |
| 689 | |
| 690 | return count + 1; |
| 691 | } |
| 692 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 693 | static inline struct lock_class * |
Matthew Wilcox | 08f36ff | 2018-01-17 07:14:13 -0800 | [diff] [blame] | 694 | look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 695 | { |
| 696 | struct lockdep_subclass_key *key; |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 697 | struct hlist_head *hash_head; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 698 | struct lock_class *class; |
| 699 | |
Hitoshi Mitake | 4ba053c | 2010-10-13 17:30:26 +0900 | [diff] [blame] | 700 | if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) { |
| 701 | debug_locks_off(); |
| 702 | printk(KERN_ERR |
| 703 | "BUG: looking up invalid subclass: %u\n", subclass); |
| 704 | printk(KERN_ERR |
| 705 | "turning off the locking correctness validator.\n"); |
| 706 | dump_stack(); |
| 707 | return NULL; |
| 708 | } |
| 709 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 710 | /* |
Matthew Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 711 | * If it is not initialised then it has never been locked, |
| 712 | * so it won't be present in the hash table. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 713 | */ |
Matthew Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 714 | if (unlikely(!lock->key)) |
| 715 | return NULL; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 716 | |
| 717 | /* |
| 718 | * NOTE: the class-key must be unique. For dynamic locks, a static |
| 719 | * lock_class_key variable is passed in through the mutex_init() |
| 720 | * (or spin_lock_init()) call - which acts as the key. For static |
| 721 | * locks we use the lock object itself as the key. |
| 722 | */ |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 723 | BUILD_BUG_ON(sizeof(struct lock_class_key) > |
| 724 | sizeof(struct lockdep_map)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 725 | |
| 726 | key = lock->key->subkeys + subclass; |
| 727 | |
| 728 | hash_head = classhashentry(key); |
| 729 | |
| 730 | /* |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 731 | * 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] | 732 | */ |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 733 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 734 | return NULL; |
| 735 | |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 736 | hlist_for_each_entry_rcu(class, hash_head, hash_entry) { |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 737 | if (class->key == key) { |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 738 | /* |
| 739 | * Huh! same key, different name? Did someone trample |
| 740 | * on some memory? We're most confused. |
| 741 | */ |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 742 | WARN_ON_ONCE(class->name != lock->name); |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 743 | return class; |
Peter Zijlstra | 4b32d0a | 2007-07-19 01:48:59 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 746 | |
Matthew Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 747 | return NULL; |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | * Static locks do not have their class-keys yet - for them the key is |
| 752 | * the lock object itself. If the lock is in the per cpu area, the |
| 753 | * canonical address of the lock (per cpu offset removed) is used. |
| 754 | */ |
| 755 | static bool assign_lock_key(struct lockdep_map *lock) |
| 756 | { |
| 757 | unsigned long can_addr, addr = (unsigned long)lock; |
| 758 | |
| 759 | if (__is_kernel_percpu_address(addr, &can_addr)) |
| 760 | lock->key = (void *)can_addr; |
| 761 | else if (__is_module_percpu_address(addr, &can_addr)) |
| 762 | lock->key = (void *)can_addr; |
| 763 | else if (static_obj(lock)) |
| 764 | lock->key = (void *)lock; |
| 765 | else { |
| 766 | /* Debug-check: all keys must be persistent! */ |
| 767 | debug_locks_off(); |
| 768 | pr_err("INFO: trying to register non-static key.\n"); |
| 769 | pr_err("the code is fine but needs lockdep annotation.\n"); |
| 770 | pr_err("turning off the locking correctness validator.\n"); |
| 771 | dump_stack(); |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | return true; |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /* |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 779 | * Initialize the lock_classes[] array elements, the free_lock_classes list |
| 780 | * and also the delayed_free structure. |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 781 | */ |
| 782 | static void init_data_structures_once(void) |
| 783 | { |
| 784 | static bool initialization_happened; |
| 785 | int i; |
| 786 | |
| 787 | if (likely(initialization_happened)) |
| 788 | return; |
| 789 | |
| 790 | initialization_happened = true; |
| 791 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 792 | init_rcu_head(&delayed_free.rcu_head); |
| 793 | INIT_LIST_HEAD(&delayed_free.pf[0].zapped); |
| 794 | INIT_LIST_HEAD(&delayed_free.pf[1].zapped); |
| 795 | |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 796 | for (i = 0; i < ARRAY_SIZE(lock_classes); i++) { |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 797 | list_add_tail(&lock_classes[i].lock_entry, &free_lock_classes); |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 798 | INIT_LIST_HEAD(&lock_classes[i].locks_after); |
| 799 | INIT_LIST_HEAD(&lock_classes[i].locks_before); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | /* |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 804 | * Register a lock's class in the hash-table, if the class is not present |
| 805 | * yet. Otherwise we look it up. We cache the result in the lock object |
| 806 | * itself, so actual lookup of the hash should be once per lock object. |
| 807 | */ |
Denys Vlasenko | c003ed9 | 2016-04-08 20:58:46 +0200 | [diff] [blame] | 808 | static struct lock_class * |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 809 | register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force) |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 810 | { |
| 811 | struct lockdep_subclass_key *key; |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 812 | struct hlist_head *hash_head; |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 813 | struct lock_class *class; |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 814 | |
| 815 | DEBUG_LOCKS_WARN_ON(!irqs_disabled()); |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 816 | |
| 817 | class = look_up_lock_class(lock, subclass); |
Matthew Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 818 | if (likely(class)) |
Yong Zhang | 87cdee7 | 2011-11-09 16:07:14 +0800 | [diff] [blame] | 819 | goto out_set_class_cache; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 820 | |
Matthew Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 821 | if (!lock->key) { |
| 822 | if (!assign_lock_key(lock)) |
| 823 | return NULL; |
| 824 | } else if (!static_obj(lock->key)) { |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 825 | return NULL; |
| 826 | } |
| 827 | |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 828 | key = lock->key->subkeys + subclass; |
| 829 | hash_head = classhashentry(key); |
| 830 | |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 831 | if (!graph_lock()) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 832 | return NULL; |
| 833 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 834 | /* |
| 835 | * We have to do the hash-walk again, to avoid races |
| 836 | * with another CPU: |
| 837 | */ |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 838 | hlist_for_each_entry_rcu(class, hash_head, hash_entry) { |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 839 | if (class->key == key) |
| 840 | goto out_unlock_set; |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 841 | } |
| 842 | |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 843 | init_data_structures_once(); |
| 844 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 845 | /* Allocate a new lock class and add it to the hash. */ |
| 846 | class = list_first_entry_or_null(&free_lock_classes, typeof(*class), |
| 847 | lock_entry); |
| 848 | if (!class) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 849 | if (!debug_locks_off_graph_unlock()) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 850 | return NULL; |
| 851 | } |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 852 | |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 853 | print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!"); |
Peter Zijlstra | eedeeab | 2009-03-18 12:38:47 +0100 | [diff] [blame] | 854 | dump_stack(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 855 | return NULL; |
| 856 | } |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 857 | nr_lock_classes++; |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 858 | debug_atomic_inc(nr_unused_locks); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 859 | class->key = key; |
| 860 | class->name = lock->name; |
| 861 | class->subclass = subclass; |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 862 | WARN_ON_ONCE(!list_empty(&class->locks_before)); |
| 863 | WARN_ON_ONCE(!list_empty(&class->locks_after)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 864 | class->name_version = count_matching_names(class); |
| 865 | /* |
| 866 | * We use RCU's safe list-add method to make |
| 867 | * parallel walking of the hash-list safe: |
| 868 | */ |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 869 | hlist_add_head_rcu(&class->hash_entry, hash_head); |
Dale Farnsworth | 1481197 | 2008-02-25 23:03:02 +0100 | [diff] [blame] | 870 | /* |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 871 | * Remove the class from the free list and add it to the global list |
| 872 | * of classes. |
Dale Farnsworth | 1481197 | 2008-02-25 23:03:02 +0100 | [diff] [blame] | 873 | */ |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 874 | list_move_tail(&class->lock_entry, &all_lock_classes); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 875 | |
| 876 | if (verbose(class)) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 877 | graph_unlock(); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 878 | |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 879 | printk("\nnew class %px: %s", class->key, class->name); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 880 | if (class->name_version > 1) |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 881 | printk(KERN_CONT "#%d", class->name_version); |
| 882 | printk(KERN_CONT "\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 883 | dump_stack(); |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 884 | |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 885 | if (!graph_lock()) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 886 | return NULL; |
| 887 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 888 | } |
| 889 | out_unlock_set: |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 890 | graph_unlock(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 891 | |
Yong Zhang | 87cdee7 | 2011-11-09 16:07:14 +0800 | [diff] [blame] | 892 | out_set_class_cache: |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 893 | if (!subclass || force) |
Hitoshi Mitake | 6201625 | 2010-10-05 18:01:51 +0900 | [diff] [blame] | 894 | lock->class_cache[0] = class; |
| 895 | else if (subclass < NR_LOCKDEP_CACHING_CLASSES) |
| 896 | lock->class_cache[subclass] = class; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 897 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 898 | /* |
| 899 | * Hash collision, did we smoke some? We found a class with a matching |
| 900 | * hash but the subclass -- which is hashed in -- didn't match. |
| 901 | */ |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 902 | if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass)) |
| 903 | return NULL; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 904 | |
| 905 | return class; |
| 906 | } |
| 907 | |
Peter Zijlstra | ca58abc | 2007-07-19 01:48:53 -0700 | [diff] [blame] | 908 | #ifdef CONFIG_PROVE_LOCKING |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 909 | /* |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 910 | * Allocate a lockdep entry. (assumes the graph_lock held, returns |
| 911 | * with NULL on failure) |
| 912 | */ |
| 913 | static struct lock_list *alloc_list_entry(void) |
| 914 | { |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 915 | int idx = find_first_zero_bit(list_entries_in_use, |
| 916 | ARRAY_SIZE(list_entries)); |
| 917 | |
| 918 | if (idx >= ARRAY_SIZE(list_entries)) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 919 | if (!debug_locks_off_graph_unlock()) |
| 920 | return NULL; |
| 921 | |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 922 | print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!"); |
Peter Zijlstra | eedeeab | 2009-03-18 12:38:47 +0100 | [diff] [blame] | 923 | dump_stack(); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 924 | return NULL; |
| 925 | } |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 926 | nr_list_entries++; |
| 927 | __set_bit(idx, list_entries_in_use); |
| 928 | return list_entries + idx; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /* |
| 932 | * Add a new dependency to the head of the list: |
| 933 | */ |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 934 | static int add_lock_to_list(struct lock_class *this, |
| 935 | struct lock_class *links_to, struct list_head *head, |
Tahsin Erdogan | 83f0616 | 2016-11-08 00:02:07 -0800 | [diff] [blame] | 936 | unsigned long ip, int distance, |
| 937 | struct stack_trace *trace) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 938 | { |
| 939 | struct lock_list *entry; |
| 940 | /* |
| 941 | * Lock not present yet - get a new dependency struct and |
| 942 | * add it to the list: |
| 943 | */ |
| 944 | entry = alloc_list_entry(); |
| 945 | if (!entry) |
| 946 | return 0; |
| 947 | |
Zhu Yi | 7487017 | 2008-08-27 14:33:00 +0800 | [diff] [blame] | 948 | entry->class = this; |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 949 | entry->links_to = links_to; |
Zhu Yi | 7487017 | 2008-08-27 14:33:00 +0800 | [diff] [blame] | 950 | entry->distance = distance; |
Yong Zhang | 4726f2a | 2010-05-04 14:16:48 +0800 | [diff] [blame] | 951 | entry->trace = *trace; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 952 | /* |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 953 | * Both allocation and removal are done under the graph lock; but |
| 954 | * iteration is under RCU-sched; see look_up_lock_class() and |
| 955 | * lockdep_free_key_range(). |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 956 | */ |
| 957 | list_add_tail_rcu(&entry->entry, head); |
| 958 | |
| 959 | return 1; |
| 960 | } |
| 961 | |
Peter Zijlstra | 98c33ed | 2009-07-21 13:19:07 +0200 | [diff] [blame] | 962 | /* |
| 963 | * For good efficiency of modular, we use power of 2 |
| 964 | */ |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 965 | #define MAX_CIRCULAR_QUEUE_SIZE 4096UL |
| 966 | #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1) |
| 967 | |
Peter Zijlstra | 98c33ed | 2009-07-21 13:19:07 +0200 | [diff] [blame] | 968 | /* |
| 969 | * The circular_queue and helpers is used to implement the |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 970 | * breadth-first search(BFS)algorithem, by which we can build |
| 971 | * the shortest path from the next lock to be acquired to the |
| 972 | * previous held lock if there is a circular between them. |
Peter Zijlstra | 98c33ed | 2009-07-21 13:19:07 +0200 | [diff] [blame] | 973 | */ |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 974 | struct circular_queue { |
| 975 | unsigned long element[MAX_CIRCULAR_QUEUE_SIZE]; |
| 976 | unsigned int front, rear; |
| 977 | }; |
| 978 | |
| 979 | static struct circular_queue lock_cq; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 980 | |
Ming Lei | 12f3dfd | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 981 | unsigned int max_bfs_queue_depth; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 982 | |
Ming Lei | e351b66 | 2009-07-22 22:48:09 +0800 | [diff] [blame] | 983 | static unsigned int lockdep_dependency_gen_id; |
| 984 | |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 985 | static inline void __cq_init(struct circular_queue *cq) |
| 986 | { |
| 987 | cq->front = cq->rear = 0; |
Ming Lei | e351b66 | 2009-07-22 22:48:09 +0800 | [diff] [blame] | 988 | lockdep_dependency_gen_id++; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | static inline int __cq_empty(struct circular_queue *cq) |
| 992 | { |
| 993 | return (cq->front == cq->rear); |
| 994 | } |
| 995 | |
| 996 | static inline int __cq_full(struct circular_queue *cq) |
| 997 | { |
| 998 | return ((cq->rear + 1) & CQ_MASK) == cq->front; |
| 999 | } |
| 1000 | |
| 1001 | static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem) |
| 1002 | { |
| 1003 | if (__cq_full(cq)) |
| 1004 | return -1; |
| 1005 | |
| 1006 | cq->element[cq->rear] = elem; |
| 1007 | cq->rear = (cq->rear + 1) & CQ_MASK; |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem) |
| 1012 | { |
| 1013 | if (__cq_empty(cq)) |
| 1014 | return -1; |
| 1015 | |
| 1016 | *elem = cq->element[cq->front]; |
| 1017 | cq->front = (cq->front + 1) & CQ_MASK; |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | static inline unsigned int __cq_get_elem_count(struct circular_queue *cq) |
| 1022 | { |
| 1023 | return (cq->rear - cq->front) & CQ_MASK; |
| 1024 | } |
| 1025 | |
| 1026 | static inline void mark_lock_accessed(struct lock_list *lock, |
| 1027 | struct lock_list *parent) |
| 1028 | { |
| 1029 | unsigned long nr; |
Peter Zijlstra | 98c33ed | 2009-07-21 13:19:07 +0200 | [diff] [blame] | 1030 | |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1031 | nr = lock - list_entries; |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 1032 | WARN_ON(nr >= ARRAY_SIZE(list_entries)); /* Out-of-bounds, input fail */ |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1033 | lock->parent = parent; |
Ming Lei | e351b66 | 2009-07-22 22:48:09 +0800 | [diff] [blame] | 1034 | lock->class->dep_gen_id = lockdep_dependency_gen_id; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | static inline unsigned long lock_accessed(struct lock_list *lock) |
| 1038 | { |
| 1039 | unsigned long nr; |
Peter Zijlstra | 98c33ed | 2009-07-21 13:19:07 +0200 | [diff] [blame] | 1040 | |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1041 | nr = lock - list_entries; |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 1042 | WARN_ON(nr >= ARRAY_SIZE(list_entries)); /* Out-of-bounds, input fail */ |
Ming Lei | e351b66 | 2009-07-22 22:48:09 +0800 | [diff] [blame] | 1043 | return lock->class->dep_gen_id == lockdep_dependency_gen_id; |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | static inline struct lock_list *get_lock_parent(struct lock_list *child) |
| 1047 | { |
| 1048 | return child->parent; |
| 1049 | } |
| 1050 | |
| 1051 | static inline int get_lock_depth(struct lock_list *child) |
| 1052 | { |
| 1053 | int depth = 0; |
| 1054 | struct lock_list *parent; |
| 1055 | |
| 1056 | while ((parent = get_lock_parent(child))) { |
| 1057 | child = parent; |
| 1058 | depth++; |
| 1059 | } |
| 1060 | return depth; |
| 1061 | } |
| 1062 | |
Ming Lei | 9e2d551 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1063 | static int __bfs(struct lock_list *source_entry, |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1064 | void *data, |
| 1065 | int (*match)(struct lock_list *entry, void *data), |
| 1066 | struct lock_list **target_entry, |
| 1067 | int forward) |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1068 | { |
| 1069 | struct lock_list *entry; |
Ming Lei | d588e46 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1070 | struct list_head *head; |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1071 | struct circular_queue *cq = &lock_cq; |
| 1072 | int ret = 1; |
| 1073 | |
Ming Lei | 9e2d551 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1074 | if (match(source_entry, data)) { |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1075 | *target_entry = source_entry; |
| 1076 | ret = 0; |
| 1077 | goto exit; |
| 1078 | } |
| 1079 | |
Ming Lei | d588e46 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1080 | if (forward) |
| 1081 | head = &source_entry->class->locks_after; |
| 1082 | else |
| 1083 | head = &source_entry->class->locks_before; |
| 1084 | |
| 1085 | if (list_empty(head)) |
| 1086 | goto exit; |
| 1087 | |
| 1088 | __cq_init(cq); |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1089 | __cq_enqueue(cq, (unsigned long)source_entry); |
| 1090 | |
| 1091 | while (!__cq_empty(cq)) { |
| 1092 | struct lock_list *lock; |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1093 | |
| 1094 | __cq_dequeue(cq, (unsigned long *)&lock); |
| 1095 | |
| 1096 | if (!lock->class) { |
| 1097 | ret = -2; |
| 1098 | goto exit; |
| 1099 | } |
| 1100 | |
| 1101 | if (forward) |
| 1102 | head = &lock->class->locks_after; |
| 1103 | else |
| 1104 | head = &lock->class->locks_before; |
| 1105 | |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 1106 | DEBUG_LOCKS_WARN_ON(!irqs_disabled()); |
| 1107 | |
| 1108 | list_for_each_entry_rcu(entry, head, entry) { |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1109 | if (!lock_accessed(entry)) { |
Ming Lei | 12f3dfd | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1110 | unsigned int cq_depth; |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1111 | mark_lock_accessed(entry, lock); |
Ming Lei | 9e2d551 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1112 | if (match(entry, data)) { |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1113 | *target_entry = entry; |
| 1114 | ret = 0; |
| 1115 | goto exit; |
| 1116 | } |
| 1117 | |
| 1118 | if (__cq_enqueue(cq, (unsigned long)entry)) { |
| 1119 | ret = -1; |
| 1120 | goto exit; |
| 1121 | } |
Ming Lei | 12f3dfd | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1122 | cq_depth = __cq_get_elem_count(cq); |
| 1123 | if (max_bfs_queue_depth < cq_depth) |
| 1124 | max_bfs_queue_depth = cq_depth; |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | exit: |
| 1129 | return ret; |
| 1130 | } |
| 1131 | |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1132 | static inline int __bfs_forwards(struct lock_list *src_entry, |
Ming Lei | 9e2d551 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1133 | void *data, |
| 1134 | int (*match)(struct lock_list *entry, void *data), |
| 1135 | struct lock_list **target_entry) |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1136 | { |
Ming Lei | 9e2d551 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1137 | return __bfs(src_entry, data, match, target_entry, 1); |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1138 | |
| 1139 | } |
| 1140 | |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1141 | static inline int __bfs_backwards(struct lock_list *src_entry, |
Ming Lei | 9e2d551 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1142 | void *data, |
| 1143 | int (*match)(struct lock_list *entry, void *data), |
| 1144 | struct lock_list **target_entry) |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1145 | { |
Ming Lei | 9e2d551 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1146 | return __bfs(src_entry, data, match, target_entry, 0); |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1147 | |
| 1148 | } |
| 1149 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1150 | /* |
| 1151 | * Recursive, forwards-direction lock-dependency checking, used for |
| 1152 | * both noncyclic checking and for hardirq-unsafe/softirq-unsafe |
| 1153 | * checking. |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1154 | */ |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1155 | |
| 1156 | /* |
| 1157 | * Print a dependency chain entry (this is only done when a deadlock |
| 1158 | * has been detected): |
| 1159 | */ |
| 1160 | static noinline int |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1161 | print_circular_bug_entry(struct lock_list *target, int depth) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1162 | { |
| 1163 | if (debug_locks_silent) |
| 1164 | return 0; |
| 1165 | printk("\n-> #%u", depth); |
| 1166 | print_lock_name(target->class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1167 | printk(KERN_CONT ":\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1168 | print_stack_trace(&target->trace, 6); |
| 1169 | |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1173 | static void |
| 1174 | print_circular_lock_scenario(struct held_lock *src, |
| 1175 | struct held_lock *tgt, |
| 1176 | struct lock_list *prt) |
| 1177 | { |
| 1178 | struct lock_class *source = hlock_class(src); |
| 1179 | struct lock_class *target = hlock_class(tgt); |
| 1180 | struct lock_class *parent = prt->class; |
| 1181 | |
| 1182 | /* |
| 1183 | * A direct locking problem where unsafe_class lock is taken |
| 1184 | * directly by safe_class lock, then all we need to show |
| 1185 | * is the deadlock scenario, as it is obvious that the |
| 1186 | * unsafe lock is taken under the safe lock. |
| 1187 | * |
| 1188 | * But if there is a chain instead, where the safe lock takes |
| 1189 | * an intermediate lock (middle_class) where this lock is |
| 1190 | * not the same as the safe lock, then the lock chain is |
| 1191 | * used to describe the problem. Otherwise we would need |
| 1192 | * to show a different CPU case for each link in the chain |
| 1193 | * from the safe_class lock to the unsafe_class lock. |
| 1194 | */ |
| 1195 | if (parent != source) { |
| 1196 | printk("Chain exists of:\n "); |
| 1197 | __print_lock_name(source); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1198 | printk(KERN_CONT " --> "); |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1199 | __print_lock_name(parent); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1200 | printk(KERN_CONT " --> "); |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1201 | __print_lock_name(target); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1202 | printk(KERN_CONT "\n\n"); |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1203 | } |
| 1204 | |
Ingo Molnar | e966eae | 2017-12-12 12:31:16 +0100 | [diff] [blame] | 1205 | printk(" Possible unsafe locking scenario:\n\n"); |
| 1206 | printk(" CPU0 CPU1\n"); |
| 1207 | printk(" ---- ----\n"); |
| 1208 | printk(" lock("); |
| 1209 | __print_lock_name(target); |
| 1210 | printk(KERN_CONT ");\n"); |
| 1211 | printk(" lock("); |
| 1212 | __print_lock_name(parent); |
| 1213 | printk(KERN_CONT ");\n"); |
| 1214 | printk(" lock("); |
| 1215 | __print_lock_name(target); |
| 1216 | printk(KERN_CONT ");\n"); |
| 1217 | printk(" lock("); |
| 1218 | __print_lock_name(source); |
| 1219 | printk(KERN_CONT ");\n"); |
| 1220 | printk("\n *** DEADLOCK ***\n\n"); |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1221 | } |
| 1222 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1223 | /* |
| 1224 | * When a circular dependency is detected, print the |
| 1225 | * header first: |
| 1226 | */ |
| 1227 | static noinline int |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1228 | print_circular_bug_header(struct lock_list *entry, unsigned int depth, |
| 1229 | struct held_lock *check_src, |
| 1230 | struct held_lock *check_tgt) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1231 | { |
| 1232 | struct task_struct *curr = current; |
| 1233 | |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1234 | if (debug_locks_silent) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1235 | return 0; |
| 1236 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1237 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 1238 | pr_warn("======================================================\n"); |
| 1239 | pr_warn("WARNING: possible circular locking dependency detected\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 1240 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 1241 | pr_warn("------------------------------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1242 | pr_warn("%s/%d is trying to acquire lock:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 1243 | curr->comm, task_pid_nr(curr)); |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1244 | print_lock(check_src); |
Byungchul Park | 383a4bc | 2017-08-07 16:12:55 +0900 | [diff] [blame] | 1245 | |
Ingo Molnar | e966eae | 2017-12-12 12:31:16 +0100 | [diff] [blame] | 1246 | pr_warn("\nbut task is already holding lock:\n"); |
Byungchul Park | 383a4bc | 2017-08-07 16:12:55 +0900 | [diff] [blame] | 1247 | |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1248 | print_lock(check_tgt); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1249 | pr_warn("\nwhich lock already depends on the new lock.\n\n"); |
| 1250 | pr_warn("\nthe existing dependency chain (in reverse order) is:\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1251 | |
| 1252 | print_circular_bug_entry(entry, depth); |
| 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
Ming Lei | 9e2d551 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1257 | static inline int class_equal(struct lock_list *entry, void *data) |
| 1258 | { |
| 1259 | return entry->class == data; |
| 1260 | } |
| 1261 | |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1262 | static noinline int print_circular_bug(struct lock_list *this, |
| 1263 | struct lock_list *target, |
| 1264 | struct held_lock *check_src, |
Byungchul Park | 383a4bc | 2017-08-07 16:12:55 +0900 | [diff] [blame] | 1265 | struct held_lock *check_tgt, |
| 1266 | struct stack_trace *trace) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1267 | { |
| 1268 | struct task_struct *curr = current; |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1269 | struct lock_list *parent; |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1270 | struct lock_list *first_parent; |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1271 | int depth; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1272 | |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1273 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1274 | return 0; |
| 1275 | |
Ingo Molnar | e966eae | 2017-12-12 12:31:16 +0100 | [diff] [blame] | 1276 | if (!save_trace(&this->trace)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1277 | return 0; |
| 1278 | |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1279 | depth = get_lock_depth(target); |
| 1280 | |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1281 | print_circular_bug_header(target, depth, check_src, check_tgt); |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1282 | |
| 1283 | parent = get_lock_parent(target); |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1284 | first_parent = parent; |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1285 | |
| 1286 | while (parent) { |
| 1287 | print_circular_bug_entry(parent, --depth); |
| 1288 | parent = get_lock_parent(parent); |
| 1289 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1290 | |
| 1291 | printk("\nother info that might help us debug this:\n\n"); |
Steven Rostedt | f418581 | 2011-04-20 21:41:55 -0400 | [diff] [blame] | 1292 | print_circular_lock_scenario(check_src, check_tgt, |
| 1293 | first_parent); |
| 1294 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1295 | lockdep_print_held_locks(curr); |
| 1296 | |
| 1297 | printk("\nstack backtrace:\n"); |
| 1298 | dump_stack(); |
| 1299 | |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1303 | static noinline int print_bfs_bug(int ret) |
| 1304 | { |
| 1305 | if (!debug_locks_off_graph_unlock()) |
| 1306 | return 0; |
| 1307 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 1308 | /* |
| 1309 | * Breadth-first-search failed, graph got corrupted? |
| 1310 | */ |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1311 | WARN(1, "lockdep bfs error:%d\n", ret); |
| 1312 | |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1316 | static int noop_count(struct lock_list *entry, void *data) |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1317 | { |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1318 | (*(unsigned long *)data)++; |
| 1319 | return 0; |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
Fengguang Wu | 5216d53 | 2013-11-09 00:55:35 +0800 | [diff] [blame] | 1322 | static unsigned long __lockdep_count_forward_deps(struct lock_list *this) |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1323 | { |
| 1324 | unsigned long count = 0; |
| 1325 | struct lock_list *uninitialized_var(target_entry); |
| 1326 | |
| 1327 | __bfs_forwards(this, (void *)&count, noop_count, &target_entry); |
| 1328 | |
| 1329 | return count; |
| 1330 | } |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1331 | unsigned long lockdep_count_forward_deps(struct lock_class *class) |
| 1332 | { |
| 1333 | unsigned long ret, flags; |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1334 | struct lock_list this; |
| 1335 | |
| 1336 | this.parent = NULL; |
| 1337 | this.class = class; |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1338 | |
Steven Rostedt (VMware) | fcc784b | 2018-04-04 14:06:30 -0400 | [diff] [blame] | 1339 | raw_local_irq_save(flags); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 1340 | arch_spin_lock(&lockdep_lock); |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1341 | ret = __lockdep_count_forward_deps(&this); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 1342 | arch_spin_unlock(&lockdep_lock); |
Steven Rostedt (VMware) | fcc784b | 2018-04-04 14:06:30 -0400 | [diff] [blame] | 1343 | raw_local_irq_restore(flags); |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1344 | |
| 1345 | return ret; |
| 1346 | } |
| 1347 | |
Fengguang Wu | 5216d53 | 2013-11-09 00:55:35 +0800 | [diff] [blame] | 1348 | static unsigned long __lockdep_count_backward_deps(struct lock_list *this) |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1349 | { |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1350 | unsigned long count = 0; |
| 1351 | struct lock_list *uninitialized_var(target_entry); |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1352 | |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1353 | __bfs_backwards(this, (void *)&count, noop_count, &target_entry); |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1354 | |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1355 | return count; |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | unsigned long lockdep_count_backward_deps(struct lock_class *class) |
| 1359 | { |
| 1360 | unsigned long ret, flags; |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1361 | struct lock_list this; |
| 1362 | |
| 1363 | this.parent = NULL; |
| 1364 | this.class = class; |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1365 | |
Steven Rostedt (VMware) | fcc784b | 2018-04-04 14:06:30 -0400 | [diff] [blame] | 1366 | raw_local_irq_save(flags); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 1367 | arch_spin_lock(&lockdep_lock); |
Ming Lei | ef68102 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1368 | ret = __lockdep_count_backward_deps(&this); |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 1369 | arch_spin_unlock(&lockdep_lock); |
Steven Rostedt (VMware) | fcc784b | 2018-04-04 14:06:30 -0400 | [diff] [blame] | 1370 | raw_local_irq_restore(flags); |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1371 | |
| 1372 | return ret; |
| 1373 | } |
| 1374 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1375 | /* |
| 1376 | * Prove that the dependency graph starting at <entry> can not |
| 1377 | * lead to <target>. Print an error and return 0 if it does. |
| 1378 | */ |
| 1379 | static noinline int |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1380 | check_noncircular(struct lock_list *root, struct lock_class *target, |
| 1381 | struct lock_list **target_entry) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1382 | { |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1383 | int result; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1384 | |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 1385 | debug_atomic_inc(nr_cyclic_checks); |
David Miller | 419ca3f | 2008-07-29 21:45:03 -0700 | [diff] [blame] | 1386 | |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1387 | result = __bfs_forwards(root, target, class_equal, target_entry); |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1388 | |
| 1389 | return result; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Peter Zijlstra | ae81330 | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 1392 | static noinline int |
| 1393 | check_redundant(struct lock_list *root, struct lock_class *target, |
| 1394 | struct lock_list **target_entry) |
| 1395 | { |
| 1396 | int result; |
| 1397 | |
| 1398 | debug_atomic_inc(nr_redundant_checks); |
| 1399 | |
| 1400 | result = __bfs_forwards(root, target, class_equal, target_entry); |
| 1401 | |
| 1402 | return result; |
| 1403 | } |
| 1404 | |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 1405 | #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1406 | /* |
| 1407 | * Forwards and backwards subgraph searching, for the purposes of |
| 1408 | * proving that two subgraphs can be connected by a new dependency |
| 1409 | * without creating any illegal irq-safe -> irq-unsafe lock dependency. |
| 1410 | */ |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1411 | |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1412 | static inline int usage_match(struct lock_list *entry, void *bit) |
| 1413 | { |
| 1414 | return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit); |
| 1415 | } |
| 1416 | |
| 1417 | |
| 1418 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1419 | /* |
| 1420 | * Find a node in the forwards-direction dependency sub-graph starting |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1421 | * at @root->class that matches @bit. |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1422 | * |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1423 | * Return 0 if such a node exists in the subgraph, and put that node |
| 1424 | * into *@target_entry. |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1425 | * |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1426 | * Return 1 otherwise and keep *@target_entry unchanged. |
| 1427 | * Return <0 on error. |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1428 | */ |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1429 | static int |
| 1430 | find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit, |
| 1431 | struct lock_list **target_entry) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1432 | { |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1433 | int result; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1434 | |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 1435 | debug_atomic_inc(nr_find_usage_forwards_checks); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1436 | |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1437 | result = __bfs_forwards(root, (void *)bit, usage_match, target_entry); |
| 1438 | |
| 1439 | return result; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | /* |
| 1443 | * Find a node in the backwards-direction dependency sub-graph starting |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1444 | * at @root->class that matches @bit. |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1445 | * |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1446 | * Return 0 if such a node exists in the subgraph, and put that node |
| 1447 | * into *@target_entry. |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1448 | * |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1449 | * Return 1 otherwise and keep *@target_entry unchanged. |
| 1450 | * Return <0 on error. |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1451 | */ |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1452 | static int |
| 1453 | find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit, |
| 1454 | struct lock_list **target_entry) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1455 | { |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1456 | int result; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1457 | |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 1458 | debug_atomic_inc(nr_find_usage_backwards_checks); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1459 | |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1460 | result = __bfs_backwards(root, (void *)bit, usage_match, target_entry); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1461 | |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1462 | return result; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1465 | static void print_lock_class_header(struct lock_class *class, int depth) |
| 1466 | { |
| 1467 | int bit; |
| 1468 | |
| 1469 | printk("%*s->", depth, ""); |
| 1470 | print_lock_name(class); |
Waiman Long | 8ca2b56c | 2018-10-03 13:07:18 -0400 | [diff] [blame] | 1471 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 1472 | printk(KERN_CONT " ops: %lu", debug_class_ops_read(class)); |
| 1473 | #endif |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1474 | printk(KERN_CONT " {\n"); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1475 | |
| 1476 | for (bit = 0; bit < LOCK_USAGE_STATES; bit++) { |
| 1477 | if (class->usage_mask & (1 << bit)) { |
| 1478 | int len = depth; |
| 1479 | |
| 1480 | len += printk("%*s %s", depth, "", usage_str[bit]); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1481 | len += printk(KERN_CONT " at:\n"); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1482 | print_stack_trace(class->usage_traces + bit, len); |
| 1483 | } |
| 1484 | } |
| 1485 | printk("%*s }\n", depth, ""); |
| 1486 | |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 1487 | printk("%*s ... key at: [<%px>] %pS\n", |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1488 | depth, "", class->key, class->key); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | /* |
| 1492 | * printk the shortest lock dependencies from @start to @end in reverse order: |
| 1493 | */ |
| 1494 | static void __used |
| 1495 | print_shortest_lock_dependencies(struct lock_list *leaf, |
| 1496 | struct lock_list *root) |
| 1497 | { |
| 1498 | struct lock_list *entry = leaf; |
| 1499 | int depth; |
| 1500 | |
| 1501 | /*compute depth from generated tree by BFS*/ |
| 1502 | depth = get_lock_depth(leaf); |
| 1503 | |
| 1504 | do { |
| 1505 | print_lock_class_header(entry->class, depth); |
| 1506 | printk("%*s ... acquired at:\n", depth, ""); |
| 1507 | print_stack_trace(&entry->trace, 2); |
| 1508 | printk("\n"); |
| 1509 | |
| 1510 | if (depth == 0 && (entry != root)) { |
Steven Rostedt | 6be8c39 | 2011-04-20 21:41:58 -0400 | [diff] [blame] | 1511 | printk("lockdep:%s bad path found in chain graph\n", __func__); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1512 | break; |
| 1513 | } |
| 1514 | |
| 1515 | entry = get_lock_parent(entry); |
| 1516 | depth--; |
| 1517 | } while (entry && (depth >= 0)); |
| 1518 | |
| 1519 | return; |
| 1520 | } |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1521 | |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 1522 | static void |
| 1523 | print_irq_lock_scenario(struct lock_list *safe_entry, |
| 1524 | struct lock_list *unsafe_entry, |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 1525 | struct lock_class *prev_class, |
| 1526 | struct lock_class *next_class) |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 1527 | { |
| 1528 | struct lock_class *safe_class = safe_entry->class; |
| 1529 | struct lock_class *unsafe_class = unsafe_entry->class; |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 1530 | struct lock_class *middle_class = prev_class; |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 1531 | |
| 1532 | if (middle_class == safe_class) |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 1533 | middle_class = next_class; |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 1534 | |
| 1535 | /* |
| 1536 | * A direct locking problem where unsafe_class lock is taken |
| 1537 | * directly by safe_class lock, then all we need to show |
| 1538 | * is the deadlock scenario, as it is obvious that the |
| 1539 | * unsafe lock is taken under the safe lock. |
| 1540 | * |
| 1541 | * But if there is a chain instead, where the safe lock takes |
| 1542 | * an intermediate lock (middle_class) where this lock is |
| 1543 | * not the same as the safe lock, then the lock chain is |
| 1544 | * used to describe the problem. Otherwise we would need |
| 1545 | * to show a different CPU case for each link in the chain |
| 1546 | * from the safe_class lock to the unsafe_class lock. |
| 1547 | */ |
| 1548 | if (middle_class != unsafe_class) { |
| 1549 | printk("Chain exists of:\n "); |
| 1550 | __print_lock_name(safe_class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1551 | printk(KERN_CONT " --> "); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 1552 | __print_lock_name(middle_class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1553 | printk(KERN_CONT " --> "); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 1554 | __print_lock_name(unsafe_class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1555 | printk(KERN_CONT "\n\n"); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | printk(" Possible interrupt unsafe locking scenario:\n\n"); |
| 1559 | printk(" CPU0 CPU1\n"); |
| 1560 | printk(" ---- ----\n"); |
| 1561 | printk(" lock("); |
| 1562 | __print_lock_name(unsafe_class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1563 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 1564 | printk(" local_irq_disable();\n"); |
| 1565 | printk(" lock("); |
| 1566 | __print_lock_name(safe_class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1567 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 1568 | printk(" lock("); |
| 1569 | __print_lock_name(middle_class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1570 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 1571 | printk(" <Interrupt>\n"); |
| 1572 | printk(" lock("); |
| 1573 | __print_lock_name(safe_class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1574 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 1575 | printk("\n *** DEADLOCK ***\n\n"); |
| 1576 | } |
| 1577 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1578 | static int |
| 1579 | print_bad_irq_dependency(struct task_struct *curr, |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1580 | struct lock_list *prev_root, |
| 1581 | struct lock_list *next_root, |
| 1582 | struct lock_list *backwards_entry, |
| 1583 | struct lock_list *forwards_entry, |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1584 | struct held_lock *prev, |
| 1585 | struct held_lock *next, |
| 1586 | enum lock_usage_bit bit1, |
| 1587 | enum lock_usage_bit bit2, |
| 1588 | const char *irqclass) |
| 1589 | { |
| 1590 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
| 1591 | return 0; |
| 1592 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1593 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 1594 | pr_warn("=====================================================\n"); |
| 1595 | pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n", |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1596 | irqclass, irqclass); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 1597 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 1598 | pr_warn("-----------------------------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1599 | 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] | 1600 | curr->comm, task_pid_nr(curr), |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1601 | curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT, |
| 1602 | curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT, |
| 1603 | curr->hardirqs_enabled, |
| 1604 | curr->softirqs_enabled); |
| 1605 | print_lock(next); |
| 1606 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1607 | pr_warn("\nand this task is already holding:\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1608 | print_lock(prev); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1609 | pr_warn("which would create a new lock dependency:\n"); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1610 | print_lock_name(hlock_class(prev)); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1611 | pr_cont(" ->"); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1612 | print_lock_name(hlock_class(next)); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1613 | pr_cont("\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1614 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1615 | 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] | 1616 | irqclass); |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1617 | print_lock_name(backwards_entry->class); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1618 | pr_warn("\n... which became %s-irq-safe at:\n", irqclass); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1619 | |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1620 | print_stack_trace(backwards_entry->class->usage_traces + bit1, 1); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1621 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1622 | pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass); |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1623 | print_lock_name(forwards_entry->class); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1624 | pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass); |
| 1625 | pr_warn("..."); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1626 | |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1627 | print_stack_trace(forwards_entry->class->usage_traces + bit2, 1); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1628 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1629 | pr_warn("\nother info that might help us debug this:\n\n"); |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 1630 | print_irq_lock_scenario(backwards_entry, forwards_entry, |
| 1631 | hlock_class(prev), hlock_class(next)); |
Steven Rostedt | 3003eba | 2011-04-20 21:41:54 -0400 | [diff] [blame] | 1632 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1633 | lockdep_print_held_locks(curr); |
| 1634 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1635 | pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass); |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1636 | if (!save_trace(&prev_root->trace)) |
| 1637 | return 0; |
| 1638 | print_shortest_lock_dependencies(backwards_entry, prev_root); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1639 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1640 | pr_warn("\nthe dependencies between the lock to be acquired"); |
| 1641 | pr_warn(" and %s-irq-unsafe lock:\n", irqclass); |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1642 | if (!save_trace(&next_root->trace)) |
| 1643 | return 0; |
| 1644 | print_shortest_lock_dependencies(forwards_entry, next_root); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1645 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1646 | pr_warn("\nstack backtrace:\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1647 | dump_stack(); |
| 1648 | |
| 1649 | return 0; |
| 1650 | } |
| 1651 | |
| 1652 | static int |
| 1653 | check_usage(struct task_struct *curr, struct held_lock *prev, |
| 1654 | struct held_lock *next, enum lock_usage_bit bit_backwards, |
| 1655 | enum lock_usage_bit bit_forwards, const char *irqclass) |
| 1656 | { |
| 1657 | int ret; |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1658 | struct lock_list this, that; |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1659 | struct lock_list *uninitialized_var(target_entry); |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1660 | struct lock_list *uninitialized_var(target_entry1); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1661 | |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1662 | this.parent = NULL; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1663 | |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1664 | this.class = hlock_class(prev); |
| 1665 | ret = find_usage_backwards(&this, bit_backwards, &target_entry); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1666 | if (ret < 0) |
| 1667 | return print_bfs_bug(ret); |
| 1668 | if (ret == 1) |
| 1669 | return ret; |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1670 | |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1671 | that.parent = NULL; |
| 1672 | that.class = hlock_class(next); |
| 1673 | ret = find_usage_forwards(&that, bit_forwards, &target_entry1); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1674 | if (ret < 0) |
| 1675 | return print_bfs_bug(ret); |
| 1676 | if (ret == 1) |
| 1677 | return ret; |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1678 | |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1679 | return print_bad_irq_dependency(curr, &this, &that, |
| 1680 | target_entry, target_entry1, |
| 1681 | prev, next, |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1682 | bit_backwards, bit_forwards, irqclass); |
| 1683 | } |
| 1684 | |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1685 | static const char *state_names[] = { |
| 1686 | #define LOCKDEP_STATE(__STATE) \ |
Peter Zijlstra | b4b136f | 2009-01-29 14:50:36 +0100 | [diff] [blame] | 1687 | __stringify(__STATE), |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1688 | #include "lockdep_states.h" |
| 1689 | #undef LOCKDEP_STATE |
| 1690 | }; |
| 1691 | |
| 1692 | static const char *state_rnames[] = { |
| 1693 | #define LOCKDEP_STATE(__STATE) \ |
Peter Zijlstra | b4b136f | 2009-01-29 14:50:36 +0100 | [diff] [blame] | 1694 | __stringify(__STATE)"-READ", |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1695 | #include "lockdep_states.h" |
| 1696 | #undef LOCKDEP_STATE |
| 1697 | }; |
| 1698 | |
| 1699 | static inline const char *state_name(enum lock_usage_bit bit) |
| 1700 | { |
Frederic Weisbecker | bba2a8f | 2018-12-28 06:02:01 +0100 | [diff] [blame] | 1701 | return (bit & LOCK_USAGE_READ_MASK) ? state_rnames[bit >> 2] : state_names[bit >> 2]; |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | static int exclusive_bit(int new_bit) |
| 1705 | { |
Frederic Weisbecker | bba2a8f | 2018-12-28 06:02:01 +0100 | [diff] [blame] | 1706 | int state = new_bit & LOCK_USAGE_STATE_MASK; |
| 1707 | int dir = new_bit & LOCK_USAGE_DIR_MASK; |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1708 | |
| 1709 | /* |
| 1710 | * keep state, bit flip the direction and strip read. |
| 1711 | */ |
Frederic Weisbecker | bba2a8f | 2018-12-28 06:02:01 +0100 | [diff] [blame] | 1712 | return state | (dir ^ LOCK_USAGE_DIR_MASK); |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | static int check_irq_usage(struct task_struct *curr, struct held_lock *prev, |
| 1716 | struct held_lock *next, enum lock_usage_bit bit) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1717 | { |
| 1718 | /* |
| 1719 | * Prove that the new dependency does not connect a hardirq-safe |
| 1720 | * lock with a hardirq-unsafe lock - to achieve this we search |
| 1721 | * the backwards-subgraph starting at <prev>, and the |
| 1722 | * forwards-subgraph starting at <next>: |
| 1723 | */ |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1724 | if (!check_usage(curr, prev, next, bit, |
| 1725 | exclusive_bit(bit), state_name(bit))) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1726 | return 0; |
| 1727 | |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1728 | bit++; /* _READ */ |
| 1729 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1730 | /* |
| 1731 | * Prove that the new dependency does not connect a hardirq-safe-read |
| 1732 | * lock with a hardirq-unsafe lock - to achieve this we search |
| 1733 | * the backwards-subgraph starting at <prev>, and the |
| 1734 | * forwards-subgraph starting at <next>: |
| 1735 | */ |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1736 | if (!check_usage(curr, prev, next, bit, |
| 1737 | exclusive_bit(bit), state_name(bit))) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1738 | return 0; |
| 1739 | |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1740 | return 1; |
| 1741 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1742 | |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1743 | static int |
| 1744 | check_prev_add_irq(struct task_struct *curr, struct held_lock *prev, |
| 1745 | struct held_lock *next) |
| 1746 | { |
| 1747 | #define LOCKDEP_STATE(__STATE) \ |
| 1748 | if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \ |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 1749 | return 0; |
Peter Zijlstra | 4f367d8a | 2009-01-22 18:10:42 +0100 | [diff] [blame] | 1750 | #include "lockdep_states.h" |
| 1751 | #undef LOCKDEP_STATE |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 1752 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1753 | return 1; |
| 1754 | } |
| 1755 | |
| 1756 | static void inc_chains(void) |
| 1757 | { |
| 1758 | if (current->hardirq_context) |
| 1759 | nr_hardirq_chains++; |
| 1760 | else { |
| 1761 | if (current->softirq_context) |
| 1762 | nr_softirq_chains++; |
| 1763 | else |
| 1764 | nr_process_chains++; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | #else |
| 1769 | |
| 1770 | static inline int |
| 1771 | check_prev_add_irq(struct task_struct *curr, struct held_lock *prev, |
| 1772 | struct held_lock *next) |
| 1773 | { |
| 1774 | return 1; |
| 1775 | } |
| 1776 | |
| 1777 | static inline void inc_chains(void) |
| 1778 | { |
| 1779 | nr_process_chains++; |
| 1780 | } |
| 1781 | |
| 1782 | #endif |
| 1783 | |
Steven Rostedt | 48702ec | 2011-04-20 21:41:56 -0400 | [diff] [blame] | 1784 | static void |
| 1785 | print_deadlock_scenario(struct held_lock *nxt, |
| 1786 | struct held_lock *prv) |
| 1787 | { |
| 1788 | struct lock_class *next = hlock_class(nxt); |
| 1789 | struct lock_class *prev = hlock_class(prv); |
| 1790 | |
| 1791 | printk(" Possible unsafe locking scenario:\n\n"); |
| 1792 | printk(" CPU0\n"); |
| 1793 | printk(" ----\n"); |
| 1794 | printk(" lock("); |
| 1795 | __print_lock_name(prev); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1796 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 48702ec | 2011-04-20 21:41:56 -0400 | [diff] [blame] | 1797 | printk(" lock("); |
| 1798 | __print_lock_name(next); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 1799 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 48702ec | 2011-04-20 21:41:56 -0400 | [diff] [blame] | 1800 | printk("\n *** DEADLOCK ***\n\n"); |
| 1801 | printk(" May be due to missing lock nesting notation\n\n"); |
| 1802 | } |
| 1803 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1804 | static int |
| 1805 | print_deadlock_bug(struct task_struct *curr, struct held_lock *prev, |
| 1806 | struct held_lock *next) |
| 1807 | { |
| 1808 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
| 1809 | return 0; |
| 1810 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1811 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 1812 | pr_warn("============================================\n"); |
| 1813 | pr_warn("WARNING: possible recursive locking detected\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 1814 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 1815 | pr_warn("--------------------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1816 | pr_warn("%s/%d is trying to acquire lock:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 1817 | curr->comm, task_pid_nr(curr)); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1818 | print_lock(next); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1819 | pr_warn("\nbut task is already holding lock:\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1820 | print_lock(prev); |
| 1821 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1822 | pr_warn("\nother info that might help us debug this:\n"); |
Steven Rostedt | 48702ec | 2011-04-20 21:41:56 -0400 | [diff] [blame] | 1823 | print_deadlock_scenario(next, prev); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1824 | lockdep_print_held_locks(curr); |
| 1825 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 1826 | pr_warn("\nstack backtrace:\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1827 | dump_stack(); |
| 1828 | |
| 1829 | return 0; |
| 1830 | } |
| 1831 | |
| 1832 | /* |
| 1833 | * Check whether we are holding such a class already. |
| 1834 | * |
| 1835 | * (Note that this has to be done separately, because the graph cannot |
| 1836 | * detect such classes of deadlocks.) |
| 1837 | * |
| 1838 | * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read |
| 1839 | */ |
| 1840 | static int |
| 1841 | check_deadlock(struct task_struct *curr, struct held_lock *next, |
| 1842 | struct lockdep_map *next_instance, int read) |
| 1843 | { |
| 1844 | struct held_lock *prev; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 1845 | struct held_lock *nest = NULL; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1846 | int i; |
| 1847 | |
| 1848 | for (i = 0; i < curr->lockdep_depth; i++) { |
| 1849 | prev = curr->held_locks + i; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 1850 | |
| 1851 | if (prev->instance == next->nest_lock) |
| 1852 | nest = prev; |
| 1853 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1854 | if (hlock_class(prev) != hlock_class(next)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1855 | continue; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 1856 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1857 | /* |
| 1858 | * Allow read-after-read recursion of the same |
| 1859 | * lock class (i.e. read_lock(lock)+read_lock(lock)): |
| 1860 | */ |
| 1861 | if ((read == 2) && prev->read) |
| 1862 | return 2; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 1863 | |
| 1864 | /* |
| 1865 | * We're holding the nest_lock, which serializes this lock's |
| 1866 | * nesting behaviour. |
| 1867 | */ |
| 1868 | if (nest) |
| 1869 | return 2; |
| 1870 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1871 | return print_deadlock_bug(curr, prev, next); |
| 1872 | } |
| 1873 | return 1; |
| 1874 | } |
| 1875 | |
| 1876 | /* |
| 1877 | * There was a chain-cache miss, and we are about to add a new dependency |
| 1878 | * to a previous lock. We recursively validate the following rules: |
| 1879 | * |
| 1880 | * - would the adding of the <prev> -> <next> dependency create a |
| 1881 | * circular dependency in the graph? [== circular deadlock] |
| 1882 | * |
| 1883 | * - does the new prev->next dependency connect any hardirq-safe lock |
| 1884 | * (in the full backwards-subgraph starting at <prev>) with any |
| 1885 | * hardirq-unsafe lock (in the full forwards-subgraph starting at |
| 1886 | * <next>)? [== illegal lock inversion with hardirq contexts] |
| 1887 | * |
| 1888 | * - does the new prev->next dependency connect any softirq-safe lock |
| 1889 | * (in the full backwards-subgraph starting at <prev>) with any |
| 1890 | * softirq-unsafe lock (in the full forwards-subgraph starting at |
| 1891 | * <next>)? [== illegal lock inversion with softirq contexts] |
| 1892 | * |
| 1893 | * any of these scenarios could lead to a deadlock. |
| 1894 | * |
| 1895 | * Then if all the validations pass, we add the forwards and backwards |
| 1896 | * dependency. |
| 1897 | */ |
| 1898 | static int |
| 1899 | check_prev_add(struct task_struct *curr, struct held_lock *prev, |
Byungchul Park | ce07a941 | 2017-08-07 16:12:51 +0900 | [diff] [blame] | 1900 | struct held_lock *next, int distance, struct stack_trace *trace, |
| 1901 | int (*save)(struct stack_trace *trace)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1902 | { |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1903 | struct lock_list *uninitialized_var(target_entry); |
Peter Zijlstra | 8b405d5 | 2017-10-04 11:13:37 +0200 | [diff] [blame] | 1904 | struct lock_list *entry; |
| 1905 | struct lock_list this; |
| 1906 | int ret; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1907 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 1908 | if (!hlock_class(prev)->key || !hlock_class(next)->key) { |
| 1909 | /* |
| 1910 | * The warning statements below may trigger a use-after-free |
| 1911 | * of the class name. It is better to trigger a use-after free |
| 1912 | * and to have the class name most of the time instead of not |
| 1913 | * having the class name available. |
| 1914 | */ |
| 1915 | WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key, |
| 1916 | "Detected use-after-free of lock class %px/%s\n", |
| 1917 | hlock_class(prev), |
| 1918 | hlock_class(prev)->name); |
| 1919 | WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key, |
| 1920 | "Detected use-after-free of lock class %px/%s\n", |
| 1921 | hlock_class(next), |
| 1922 | hlock_class(next)->name); |
| 1923 | return 2; |
| 1924 | } |
| 1925 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1926 | /* |
| 1927 | * Prove that the new <prev> -> <next> dependency would not |
| 1928 | * create a circular dependency in the graph. (We do this by |
| 1929 | * forward-recursing into the graph starting at <next>, and |
| 1930 | * checking whether we can reach <prev>.) |
| 1931 | * |
| 1932 | * We are using global variables to control the recursion, to |
| 1933 | * keep the stackframe size of the recursive functions low: |
| 1934 | */ |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1935 | this.class = hlock_class(next); |
| 1936 | this.parent = NULL; |
| 1937 | ret = check_noncircular(&this, hlock_class(prev), &target_entry); |
Peter Zijlstra | 8b405d5 | 2017-10-04 11:13:37 +0200 | [diff] [blame] | 1938 | if (unlikely(!ret)) { |
| 1939 | if (!trace->entries) { |
| 1940 | /* |
| 1941 | * If @save fails here, the printing might trigger |
| 1942 | * a WARN but because of the !nr_entries it should |
| 1943 | * not do bad things. |
| 1944 | */ |
| 1945 | save(trace); |
| 1946 | } |
Byungchul Park | 383a4bc | 2017-08-07 16:12:55 +0900 | [diff] [blame] | 1947 | return print_circular_bug(&this, target_entry, next, prev, trace); |
Peter Zijlstra | 8b405d5 | 2017-10-04 11:13:37 +0200 | [diff] [blame] | 1948 | } |
Ming Lei | db0002a | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1949 | else if (unlikely(ret < 0)) |
| 1950 | return print_bfs_bug(ret); |
Ming Lei | c94aa5c | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 1951 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1952 | if (!check_prev_add_irq(curr, prev, next)) |
| 1953 | return 0; |
| 1954 | |
| 1955 | /* |
| 1956 | * For recursive read-locks we do all the dependency checks, |
| 1957 | * but we dont store read-triggered dependencies (only |
| 1958 | * write-triggered dependencies). This ensures that only the |
| 1959 | * write-side dependencies matter, and that if for example a |
| 1960 | * write-lock never takes any other locks, then the reads are |
| 1961 | * equivalent to a NOP. |
| 1962 | */ |
| 1963 | if (next->read == 2 || prev->read == 2) |
| 1964 | return 1; |
| 1965 | /* |
| 1966 | * Is the <prev> -> <next> dependency already present? |
| 1967 | * |
| 1968 | * (this may occur even though this is a new chain: consider |
| 1969 | * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3 |
| 1970 | * chains - the second one will be new, but L1 already has |
| 1971 | * L2 added to its dependency list, due to the first chain.) |
| 1972 | */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 1973 | list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) { |
| 1974 | if (entry->class == hlock_class(next)) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1975 | if (distance == 1) |
| 1976 | entry->distance = 1; |
Byungchul Park | 70911fd | 2017-08-07 16:12:50 +0900 | [diff] [blame] | 1977 | return 1; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | |
Peter Zijlstra | ae81330 | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 1981 | /* |
| 1982 | * Is the <prev> -> <next> link redundant? |
| 1983 | */ |
| 1984 | this.class = hlock_class(prev); |
| 1985 | this.parent = NULL; |
| 1986 | ret = check_redundant(&this, hlock_class(next), &target_entry); |
| 1987 | if (!ret) { |
| 1988 | debug_atomic_inc(nr_redundant); |
| 1989 | return 2; |
| 1990 | } |
| 1991 | if (ret < 0) |
| 1992 | return print_bfs_bug(ret); |
| 1993 | |
| 1994 | |
Peter Zijlstra | 8b405d5 | 2017-10-04 11:13:37 +0200 | [diff] [blame] | 1995 | if (!trace->entries && !save(trace)) |
Byungchul Park | ce07a941 | 2017-08-07 16:12:51 +0900 | [diff] [blame] | 1996 | return 0; |
Yong Zhang | 4726f2a | 2010-05-04 14:16:48 +0800 | [diff] [blame] | 1997 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 1998 | /* |
| 1999 | * Ok, all validations passed, add the new lock |
| 2000 | * to the previous lock's dependency list: |
| 2001 | */ |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 2002 | ret = add_lock_to_list(hlock_class(next), hlock_class(prev), |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2003 | &hlock_class(prev)->locks_after, |
Byungchul Park | ce07a941 | 2017-08-07 16:12:51 +0900 | [diff] [blame] | 2004 | next->acquire_ip, distance, trace); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2005 | |
| 2006 | if (!ret) |
| 2007 | return 0; |
| 2008 | |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 2009 | ret = add_lock_to_list(hlock_class(prev), hlock_class(next), |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2010 | &hlock_class(next)->locks_before, |
Byungchul Park | ce07a941 | 2017-08-07 16:12:51 +0900 | [diff] [blame] | 2011 | next->acquire_ip, distance, trace); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2012 | if (!ret) |
| 2013 | return 0; |
| 2014 | |
Byungchul Park | 70911fd | 2017-08-07 16:12:50 +0900 | [diff] [blame] | 2015 | return 2; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | /* |
| 2019 | * Add the dependency to all directly-previous locks that are 'relevant'. |
| 2020 | * The ones that are relevant are (in increasing distance from curr): |
| 2021 | * all consecutive trylock entries and the final non-trylock entry - or |
| 2022 | * the end of this context's lock-chain - whichever comes first. |
| 2023 | */ |
| 2024 | static int |
| 2025 | check_prevs_add(struct task_struct *curr, struct held_lock *next) |
| 2026 | { |
| 2027 | int depth = curr->lockdep_depth; |
| 2028 | struct held_lock *hlock; |
Peter Zijlstra | 8b405d5 | 2017-10-04 11:13:37 +0200 | [diff] [blame] | 2029 | struct stack_trace trace = { |
| 2030 | .nr_entries = 0, |
| 2031 | .max_entries = 0, |
| 2032 | .entries = NULL, |
| 2033 | .skip = 0, |
| 2034 | }; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2035 | |
| 2036 | /* |
| 2037 | * Debugging checks. |
| 2038 | * |
| 2039 | * Depth must not be zero for a non-head lock: |
| 2040 | */ |
| 2041 | if (!depth) |
| 2042 | goto out_bug; |
| 2043 | /* |
| 2044 | * At least two relevant locks must exist for this |
| 2045 | * to be a head: |
| 2046 | */ |
| 2047 | if (curr->held_locks[depth].irq_context != |
| 2048 | curr->held_locks[depth-1].irq_context) |
| 2049 | goto out_bug; |
| 2050 | |
| 2051 | for (;;) { |
| 2052 | int distance = curr->lockdep_depth - depth + 1; |
Oleg Nesterov | 1b5ff81 | 2014-01-20 19:20:10 +0100 | [diff] [blame] | 2053 | hlock = curr->held_locks + depth - 1; |
Byungchul Park | ce07a941 | 2017-08-07 16:12:51 +0900 | [diff] [blame] | 2054 | |
Ingo Molnar | e966eae | 2017-12-12 12:31:16 +0100 | [diff] [blame] | 2055 | /* |
| 2056 | * Only non-recursive-read entries get new dependencies |
| 2057 | * added: |
| 2058 | */ |
| 2059 | if (hlock->read != 2 && hlock->check) { |
| 2060 | int ret = check_prev_add(curr, hlock, next, distance, &trace, save_trace); |
| 2061 | if (!ret) |
| 2062 | return 0; |
| 2063 | |
| 2064 | /* |
| 2065 | * Stop after the first non-trylock entry, |
| 2066 | * as non-trylock entries have added their |
| 2067 | * own direct dependencies already, so this |
| 2068 | * lock is connected to them indirectly: |
| 2069 | */ |
| 2070 | if (!hlock->trylock) |
| 2071 | break; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2072 | } |
Ingo Molnar | e966eae | 2017-12-12 12:31:16 +0100 | [diff] [blame] | 2073 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2074 | depth--; |
| 2075 | /* |
| 2076 | * End of lock-stack? |
| 2077 | */ |
| 2078 | if (!depth) |
| 2079 | break; |
| 2080 | /* |
| 2081 | * Stop the search if we cross into another context: |
| 2082 | */ |
| 2083 | if (curr->held_locks[depth].irq_context != |
| 2084 | curr->held_locks[depth-1].irq_context) |
| 2085 | break; |
| 2086 | } |
| 2087 | return 1; |
| 2088 | out_bug: |
| 2089 | if (!debug_locks_off_graph_unlock()) |
| 2090 | return 0; |
| 2091 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2092 | /* |
| 2093 | * Clearly we all shouldn't be here, but since we made it we |
| 2094 | * can reliable say we messed up our state. See the above two |
| 2095 | * gotos for reasons why we could possibly end up here. |
| 2096 | */ |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2097 | WARN_ON(1); |
| 2098 | |
| 2099 | return 0; |
| 2100 | } |
| 2101 | |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 2102 | struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS]; |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame^] | 2103 | static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS); |
Huang, Ying | cd1a28e | 2008-06-23 11:20:54 +0800 | [diff] [blame] | 2104 | int nr_chain_hlocks; |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 2105 | static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS]; |
| 2106 | |
| 2107 | struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i) |
| 2108 | { |
| 2109 | return lock_classes + chain_hlocks[chain->base + i]; |
| 2110 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2111 | |
| 2112 | /* |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 2113 | * Returns the index of the first held_lock of the current chain |
| 2114 | */ |
| 2115 | static inline int get_first_held_lock(struct task_struct *curr, |
| 2116 | struct held_lock *hlock) |
| 2117 | { |
| 2118 | int i; |
| 2119 | struct held_lock *hlock_curr; |
| 2120 | |
| 2121 | for (i = curr->lockdep_depth - 1; i >= 0; i--) { |
| 2122 | hlock_curr = curr->held_locks + i; |
| 2123 | if (hlock_curr->irq_context != hlock->irq_context) |
| 2124 | break; |
| 2125 | |
| 2126 | } |
| 2127 | |
| 2128 | return ++i; |
| 2129 | } |
| 2130 | |
Borislav Petkov | 5c8a010 | 2016-04-04 10:42:07 +0200 | [diff] [blame] | 2131 | #ifdef CONFIG_DEBUG_LOCKDEP |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 2132 | /* |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 2133 | * Returns the next chain_key iteration |
| 2134 | */ |
| 2135 | static u64 print_chain_key_iteration(int class_idx, u64 chain_key) |
| 2136 | { |
| 2137 | u64 new_chain_key = iterate_chain_key(chain_key, class_idx); |
| 2138 | |
| 2139 | printk(" class_idx:%d -> chain_key:%016Lx", |
| 2140 | class_idx, |
| 2141 | (unsigned long long)new_chain_key); |
| 2142 | return new_chain_key; |
| 2143 | } |
| 2144 | |
| 2145 | static void |
| 2146 | print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next) |
| 2147 | { |
| 2148 | struct held_lock *hlock; |
| 2149 | u64 chain_key = 0; |
| 2150 | int depth = curr->lockdep_depth; |
| 2151 | int i; |
| 2152 | |
| 2153 | printk("depth: %u\n", depth + 1); |
| 2154 | for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) { |
| 2155 | hlock = curr->held_locks + i; |
| 2156 | chain_key = print_chain_key_iteration(hlock->class_idx, chain_key); |
| 2157 | |
| 2158 | print_lock(hlock); |
| 2159 | } |
| 2160 | |
| 2161 | print_chain_key_iteration(hlock_next->class_idx, chain_key); |
| 2162 | print_lock(hlock_next); |
| 2163 | } |
| 2164 | |
| 2165 | static void print_chain_keys_chain(struct lock_chain *chain) |
| 2166 | { |
| 2167 | int i; |
| 2168 | u64 chain_key = 0; |
| 2169 | int class_id; |
| 2170 | |
| 2171 | printk("depth: %u\n", chain->depth); |
| 2172 | for (i = 0; i < chain->depth; i++) { |
| 2173 | class_id = chain_hlocks[chain->base + i]; |
| 2174 | chain_key = print_chain_key_iteration(class_id + 1, chain_key); |
| 2175 | |
| 2176 | print_lock_name(lock_classes + class_id); |
| 2177 | printk("\n"); |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | static void print_collision(struct task_struct *curr, |
| 2182 | struct held_lock *hlock_next, |
| 2183 | struct lock_chain *chain) |
| 2184 | { |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2185 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 2186 | pr_warn("============================\n"); |
| 2187 | pr_warn("WARNING: chain_key collision\n"); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 2188 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 2189 | pr_warn("----------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2190 | pr_warn("%s/%d: ", current->comm, task_pid_nr(current)); |
| 2191 | 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] | 2192 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2193 | pr_warn("Held locks:"); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 2194 | print_chain_keys_held_locks(curr, hlock_next); |
| 2195 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2196 | pr_warn("Locks in cached chain:"); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 2197 | print_chain_keys_chain(chain); |
| 2198 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2199 | pr_warn("\nstack backtrace:\n"); |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 2200 | dump_stack(); |
| 2201 | } |
Borislav Petkov | 5c8a010 | 2016-04-04 10:42:07 +0200 | [diff] [blame] | 2202 | #endif |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 2203 | |
| 2204 | /* |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 2205 | * Checks whether the chain and the current held locks are consistent |
| 2206 | * in depth and also in content. If they are not it most likely means |
| 2207 | * that there was a collision during the calculation of the chain_key. |
| 2208 | * Returns: 0 not passed, 1 passed |
| 2209 | */ |
| 2210 | static int check_no_collision(struct task_struct *curr, |
| 2211 | struct held_lock *hlock, |
| 2212 | struct lock_chain *chain) |
| 2213 | { |
| 2214 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 2215 | int i, j, id; |
| 2216 | |
| 2217 | i = get_first_held_lock(curr, hlock); |
| 2218 | |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 2219 | if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) { |
| 2220 | print_collision(curr, hlock, chain); |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 2221 | return 0; |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 2222 | } |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 2223 | |
| 2224 | for (j = 0; j < chain->depth - 1; j++, i++) { |
| 2225 | id = curr->held_locks[i].class_idx - 1; |
| 2226 | |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 2227 | if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) { |
| 2228 | print_collision(curr, hlock, chain); |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 2229 | return 0; |
Alfredo Alvarez Fernandez | 39e2e17 | 2016-03-30 19:03:36 +0200 | [diff] [blame] | 2230 | } |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 2231 | } |
| 2232 | #endif |
| 2233 | return 1; |
| 2234 | } |
| 2235 | |
| 2236 | /* |
Bart Van Assche | 2212684 | 2019-02-14 15:00:48 -0800 | [diff] [blame] | 2237 | * Given an index that is >= -1, return the index of the next lock chain. |
| 2238 | * Return -2 if there is no next lock chain. |
| 2239 | */ |
| 2240 | long lockdep_next_lockchain(long i) |
| 2241 | { |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame^] | 2242 | i = find_next_bit(lock_chains_in_use, ARRAY_SIZE(lock_chains), i + 1); |
| 2243 | return i < ARRAY_SIZE(lock_chains) ? i : -2; |
Bart Van Assche | 2212684 | 2019-02-14 15:00:48 -0800 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | unsigned long lock_chain_count(void) |
| 2247 | { |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame^] | 2248 | return bitmap_weight(lock_chains_in_use, ARRAY_SIZE(lock_chains)); |
| 2249 | } |
| 2250 | |
| 2251 | /* Must be called with the graph lock held. */ |
| 2252 | static struct lock_chain *alloc_lock_chain(void) |
| 2253 | { |
| 2254 | int idx = find_first_zero_bit(lock_chains_in_use, |
| 2255 | ARRAY_SIZE(lock_chains)); |
| 2256 | |
| 2257 | if (unlikely(idx >= ARRAY_SIZE(lock_chains))) |
| 2258 | return NULL; |
| 2259 | __set_bit(idx, lock_chains_in_use); |
| 2260 | return lock_chains + idx; |
Bart Van Assche | 2212684 | 2019-02-14 15:00:48 -0800 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | /* |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2264 | * Adds a dependency chain into chain hashtable. And must be called with |
| 2265 | * graph_lock held. |
| 2266 | * |
| 2267 | * Return 0 if fail, and graph_lock is released. |
| 2268 | * Return 1 if succeed, with graph_lock held. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2269 | */ |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2270 | static inline int add_chain_cache(struct task_struct *curr, |
| 2271 | struct held_lock *hlock, |
| 2272 | u64 chain_key) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2273 | { |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2274 | struct lock_class *class = hlock_class(hlock); |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 2275 | struct hlist_head *hash_head = chainhashentry(chain_key); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2276 | struct lock_chain *chain; |
Steven Rostedt | e0944ee | 2011-04-20 21:42:00 -0400 | [diff] [blame] | 2277 | int i, j; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2278 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2279 | /* |
Bart Van Assche | 527af3e | 2019-02-14 15:00:49 -0800 | [diff] [blame] | 2280 | * The caller must hold the graph lock, ensure we've got IRQs |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2281 | * disabled to make this an IRQ-safe lock.. for recursion reasons |
| 2282 | * lockdep won't complain about its own locking errors. |
| 2283 | */ |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 2284 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 2285 | return 0; |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 2286 | |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame^] | 2287 | chain = alloc_lock_chain(); |
| 2288 | if (!chain) { |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 2289 | if (!debug_locks_off_graph_unlock()) |
| 2290 | return 0; |
| 2291 | |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 2292 | print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!"); |
Peter Zijlstra | eedeeab | 2009-03-18 12:38:47 +0100 | [diff] [blame] | 2293 | dump_stack(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2294 | return 0; |
| 2295 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2296 | chain->chain_key = chain_key; |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 2297 | chain->irq_context = hlock->irq_context; |
Ingo Molnar | 9e4e755 | 2016-02-29 10:03:58 +0100 | [diff] [blame] | 2298 | i = get_first_held_lock(curr, hlock); |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 2299 | chain->depth = curr->lockdep_depth + 1 - i; |
Peter Zijlstra | 75dd602 | 2016-03-30 11:36:59 +0200 | [diff] [blame] | 2300 | |
| 2301 | BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks)); |
| 2302 | BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks)); |
| 2303 | BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes)); |
| 2304 | |
Steven Rostedt | e0944ee | 2011-04-20 21:42:00 -0400 | [diff] [blame] | 2305 | if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) { |
| 2306 | chain->base = nr_chain_hlocks; |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 2307 | for (j = 0; j < chain->depth - 1; j++, i++) { |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2308 | int lock_id = curr->held_locks[i].class_idx - 1; |
Huang, Ying | 443cd50 | 2008-06-20 16:39:21 +0800 | [diff] [blame] | 2309 | chain_hlocks[chain->base + j] = lock_id; |
| 2310 | } |
| 2311 | chain_hlocks[chain->base + j] = class - lock_classes; |
Peter Zijlstra | 75dd602 | 2016-03-30 11:36:59 +0200 | [diff] [blame] | 2312 | nr_chain_hlocks += chain->depth; |
Bart Van Assche | 523b113 | 2019-02-14 15:00:39 -0800 | [diff] [blame] | 2313 | } else { |
Byungchul Park | f9af456 | 2017-01-13 11:42:04 +0900 | [diff] [blame] | 2314 | if (!debug_locks_off_graph_unlock()) |
Peter Zijlstra | 75dd602 | 2016-03-30 11:36:59 +0200 | [diff] [blame] | 2315 | return 0; |
| 2316 | |
| 2317 | print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!"); |
| 2318 | dump_stack(); |
| 2319 | return 0; |
| 2320 | } |
Peter Zijlstra | 75dd602 | 2016-03-30 11:36:59 +0200 | [diff] [blame] | 2321 | |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 2322 | hlist_add_head_rcu(&chain->entry, hash_head); |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 2323 | debug_atomic_inc(chain_lookup_misses); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2324 | inc_chains(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2325 | |
| 2326 | return 1; |
| 2327 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2328 | |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2329 | /* |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 2330 | * Look up a dependency chain. Must be called with either the graph lock or |
| 2331 | * the RCU read lock held. |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2332 | */ |
| 2333 | static inline struct lock_chain *lookup_chain_cache(u64 chain_key) |
| 2334 | { |
| 2335 | struct hlist_head *hash_head = chainhashentry(chain_key); |
| 2336 | struct lock_chain *chain; |
| 2337 | |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2338 | hlist_for_each_entry_rcu(chain, hash_head, entry) { |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 2339 | if (READ_ONCE(chain->chain_key) == chain_key) { |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2340 | debug_atomic_inc(chain_lookup_hits); |
| 2341 | return chain; |
| 2342 | } |
| 2343 | } |
| 2344 | return NULL; |
| 2345 | } |
| 2346 | |
| 2347 | /* |
| 2348 | * If the key is not present yet in dependency chain cache then |
| 2349 | * add it and return 1 - in this case the new dependency chain is |
| 2350 | * validated. If the key is already hashed, return 0. |
| 2351 | * (On return with 1 graph_lock is held.) |
| 2352 | */ |
| 2353 | static inline int lookup_chain_cache_add(struct task_struct *curr, |
| 2354 | struct held_lock *hlock, |
| 2355 | u64 chain_key) |
| 2356 | { |
| 2357 | struct lock_class *class = hlock_class(hlock); |
| 2358 | struct lock_chain *chain = lookup_chain_cache(chain_key); |
| 2359 | |
| 2360 | if (chain) { |
| 2361 | cache_hit: |
| 2362 | if (!check_no_collision(curr, hlock, chain)) |
| 2363 | return 0; |
| 2364 | |
| 2365 | if (very_verbose(class)) { |
| 2366 | printk("\nhash chain already cached, key: " |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 2367 | "%016Lx tail class: [%px] %s\n", |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2368 | (unsigned long long)chain_key, |
| 2369 | class->key, class->name); |
| 2370 | } |
| 2371 | |
| 2372 | return 0; |
| 2373 | } |
| 2374 | |
| 2375 | if (very_verbose(class)) { |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 2376 | printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n", |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2377 | (unsigned long long)chain_key, class->key, class->name); |
| 2378 | } |
| 2379 | |
| 2380 | if (!graph_lock()) |
| 2381 | return 0; |
| 2382 | |
| 2383 | /* |
| 2384 | * We have to walk the chain again locked - to avoid duplicates: |
| 2385 | */ |
| 2386 | chain = lookup_chain_cache(chain_key); |
| 2387 | if (chain) { |
| 2388 | graph_unlock(); |
| 2389 | goto cache_hit; |
| 2390 | } |
| 2391 | |
| 2392 | if (!add_chain_cache(curr, hlock, chain_key)) |
| 2393 | return 0; |
| 2394 | |
| 2395 | return 1; |
| 2396 | } |
| 2397 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2398 | static int validate_chain(struct task_struct *curr, struct lockdep_map *lock, |
Johannes Berg | 4e6045f | 2007-10-18 23:39:55 -0700 | [diff] [blame] | 2399 | struct held_lock *hlock, int chain_head, u64 chain_key) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2400 | { |
| 2401 | /* |
| 2402 | * Trylock needs to maintain the stack of held locks, but it |
| 2403 | * does not add new dependencies, because trylock can be done |
| 2404 | * in any order. |
| 2405 | * |
| 2406 | * We look up the chain_key and do the O(N^2) check and update of |
| 2407 | * the dependencies only if this is a new dependency chain. |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2408 | * (If lookup_chain_cache_add() return with 1 it acquires |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2409 | * graph_lock for us) |
| 2410 | */ |
Oleg Nesterov | fb9edbe | 2014-01-20 19:20:06 +0100 | [diff] [blame] | 2411 | if (!hlock->trylock && hlock->check && |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2412 | lookup_chain_cache_add(curr, hlock, chain_key)) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2413 | /* |
| 2414 | * Check whether last held lock: |
| 2415 | * |
| 2416 | * - is irq-safe, if this lock is irq-unsafe |
| 2417 | * - is softirq-safe, if this lock is hardirq-unsafe |
| 2418 | * |
| 2419 | * And check whether the new lock's dependency graph |
| 2420 | * could lead back to the previous lock. |
| 2421 | * |
| 2422 | * any of these scenarios could lead to a deadlock. If |
| 2423 | * All validations |
| 2424 | */ |
| 2425 | int ret = check_deadlock(curr, hlock, lock, hlock->read); |
| 2426 | |
| 2427 | if (!ret) |
| 2428 | return 0; |
| 2429 | /* |
| 2430 | * Mark recursive read, as we jump over it when |
| 2431 | * building dependencies (just like we jump over |
| 2432 | * trylock entries): |
| 2433 | */ |
| 2434 | if (ret == 2) |
| 2435 | hlock->read = 2; |
| 2436 | /* |
| 2437 | * Add dependency only if this lock is not the head |
| 2438 | * of the chain, and if it's not a secondary read-lock: |
| 2439 | */ |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2440 | if (!chain_head && ret != 2) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2441 | if (!check_prevs_add(curr, hlock)) |
| 2442 | return 0; |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2443 | } |
| 2444 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2445 | graph_unlock(); |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2446 | } else { |
| 2447 | /* after lookup_chain_cache_add(): */ |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2448 | if (unlikely(!debug_locks)) |
| 2449 | return 0; |
Byungchul Park | 545c23f | 2017-08-07 16:12:48 +0900 | [diff] [blame] | 2450 | } |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2451 | |
| 2452 | return 1; |
| 2453 | } |
| 2454 | #else |
| 2455 | static inline int validate_chain(struct task_struct *curr, |
| 2456 | struct lockdep_map *lock, struct held_lock *hlock, |
Gregory Haskins | 3aa416b | 2007-10-11 22:11:11 +0200 | [diff] [blame] | 2457 | int chain_head, u64 chain_key) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2458 | { |
| 2459 | return 1; |
| 2460 | } |
Peter Zijlstra | ca58abc | 2007-07-19 01:48:53 -0700 | [diff] [blame] | 2461 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2462 | |
| 2463 | /* |
| 2464 | * We are building curr_chain_key incrementally, so double-check |
| 2465 | * it from scratch, to make sure that it's done correctly: |
| 2466 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 2467 | static void check_chain_key(struct task_struct *curr) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2468 | { |
| 2469 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 2470 | struct held_lock *hlock, *prev_hlock = NULL; |
Alfredo Alvarez Fernandez | 5f18ab5 | 2016-02-11 00:33:32 +0100 | [diff] [blame] | 2471 | unsigned int i; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2472 | u64 chain_key = 0; |
| 2473 | |
| 2474 | for (i = 0; i < curr->lockdep_depth; i++) { |
| 2475 | hlock = curr->held_locks + i; |
| 2476 | if (chain_key != hlock->prev_chain_key) { |
| 2477 | debug_locks_off(); |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2478 | /* |
| 2479 | * We got mighty confused, our chain keys don't match |
| 2480 | * with what we expect, someone trample on our task state? |
| 2481 | */ |
Arjan van de Ven | 2df8b1d | 2008-07-30 12:43:11 -0700 | [diff] [blame] | 2482 | WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n", |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2483 | curr->lockdep_depth, i, |
| 2484 | (unsigned long long)chain_key, |
| 2485 | (unsigned long long)hlock->prev_chain_key); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2486 | return; |
| 2487 | } |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2488 | /* |
| 2489 | * Whoops ran out of static storage again? |
| 2490 | */ |
Alfredo Alvarez Fernandez | 5f18ab5 | 2016-02-11 00:33:32 +0100 | [diff] [blame] | 2491 | if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS)) |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 2492 | return; |
| 2493 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2494 | if (prev_hlock && (prev_hlock->irq_context != |
| 2495 | hlock->irq_context)) |
| 2496 | chain_key = 0; |
Alfredo Alvarez Fernandez | 5f18ab5 | 2016-02-11 00:33:32 +0100 | [diff] [blame] | 2497 | chain_key = iterate_chain_key(chain_key, hlock->class_idx); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2498 | prev_hlock = hlock; |
| 2499 | } |
| 2500 | if (chain_key != curr->curr_chain_key) { |
| 2501 | debug_locks_off(); |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2502 | /* |
| 2503 | * More smoking hash instead of calculating it, damn see these |
| 2504 | * numbers float.. I bet that a pink elephant stepped on my memory. |
| 2505 | */ |
Arjan van de Ven | 2df8b1d | 2008-07-30 12:43:11 -0700 | [diff] [blame] | 2506 | WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n", |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2507 | curr->lockdep_depth, i, |
| 2508 | (unsigned long long)chain_key, |
| 2509 | (unsigned long long)curr->curr_chain_key); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2510 | } |
| 2511 | #endif |
| 2512 | } |
| 2513 | |
Steven Rostedt | 282b5c2 | 2011-04-20 21:41:59 -0400 | [diff] [blame] | 2514 | static void |
| 2515 | print_usage_bug_scenario(struct held_lock *lock) |
| 2516 | { |
| 2517 | struct lock_class *class = hlock_class(lock); |
| 2518 | |
| 2519 | printk(" Possible unsafe locking scenario:\n\n"); |
| 2520 | printk(" CPU0\n"); |
| 2521 | printk(" ----\n"); |
| 2522 | printk(" lock("); |
| 2523 | __print_lock_name(class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2524 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 282b5c2 | 2011-04-20 21:41:59 -0400 | [diff] [blame] | 2525 | printk(" <Interrupt>\n"); |
| 2526 | printk(" lock("); |
| 2527 | __print_lock_name(class); |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2528 | printk(KERN_CONT ");\n"); |
Steven Rostedt | 282b5c2 | 2011-04-20 21:41:59 -0400 | [diff] [blame] | 2529 | printk("\n *** DEADLOCK ***\n\n"); |
| 2530 | } |
| 2531 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2532 | static int |
| 2533 | print_usage_bug(struct task_struct *curr, struct held_lock *this, |
| 2534 | enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit) |
| 2535 | { |
| 2536 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
| 2537 | return 0; |
| 2538 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2539 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 2540 | pr_warn("================================\n"); |
| 2541 | pr_warn("WARNING: inconsistent lock state\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 2542 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 2543 | pr_warn("--------------------------------\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2544 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2545 | pr_warn("inconsistent {%s} -> {%s} usage.\n", |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2546 | usage_str[prev_bit], usage_str[new_bit]); |
| 2547 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2548 | 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] | 2549 | curr->comm, task_pid_nr(curr), |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2550 | trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT, |
| 2551 | trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT, |
| 2552 | trace_hardirqs_enabled(curr), |
| 2553 | trace_softirqs_enabled(curr)); |
| 2554 | print_lock(this); |
| 2555 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2556 | pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2557 | print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2558 | |
| 2559 | print_irqtrace_events(curr); |
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"); |
Steven Rostedt | 282b5c2 | 2011-04-20 21:41:59 -0400 | [diff] [blame] | 2561 | print_usage_bug_scenario(this); |
| 2562 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2563 | lockdep_print_held_locks(curr); |
| 2564 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2565 | pr_warn("\nstack backtrace:\n"); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2566 | dump_stack(); |
| 2567 | |
| 2568 | return 0; |
| 2569 | } |
| 2570 | |
| 2571 | /* |
| 2572 | * Print out an error if an invalid bit is set: |
| 2573 | */ |
| 2574 | static inline int |
| 2575 | valid_state(struct task_struct *curr, struct held_lock *this, |
| 2576 | enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit) |
| 2577 | { |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 2578 | if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2579 | return print_usage_bug(curr, this, bad_bit, new_bit); |
| 2580 | return 1; |
| 2581 | } |
| 2582 | |
| 2583 | static int mark_lock(struct task_struct *curr, struct held_lock *this, |
| 2584 | enum lock_usage_bit new_bit); |
| 2585 | |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2586 | #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2587 | |
| 2588 | /* |
| 2589 | * print irq inversion bug: |
| 2590 | */ |
| 2591 | static int |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2592 | print_irq_inversion_bug(struct task_struct *curr, |
| 2593 | struct lock_list *root, struct lock_list *other, |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2594 | struct held_lock *this, int forwards, |
| 2595 | const char *irqclass) |
| 2596 | { |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 2597 | struct lock_list *entry = other; |
| 2598 | struct lock_list *middle = NULL; |
| 2599 | int depth; |
| 2600 | |
Ingo Molnar | 74c383f | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 2601 | if (!debug_locks_off_graph_unlock() || debug_locks_silent) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2602 | return 0; |
| 2603 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2604 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 2605 | pr_warn("========================================================\n"); |
| 2606 | pr_warn("WARNING: possible irq lock inversion dependency detected\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 2607 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 2608 | pr_warn("--------------------------------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2609 | pr_warn("%s/%d just changed the state of lock:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 2610 | curr->comm, task_pid_nr(curr)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2611 | print_lock(this); |
| 2612 | if (forwards) |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2613 | 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] | 2614 | else |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2615 | 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] | 2616 | print_lock_name(other->class); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2617 | 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] | 2618 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2619 | pr_warn("\nother info that might help us debug this:\n"); |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 2620 | |
| 2621 | /* Find a middle lock (if one exists) */ |
| 2622 | depth = get_lock_depth(other); |
| 2623 | do { |
| 2624 | if (depth == 0 && (entry != root)) { |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2625 | pr_warn("lockdep:%s bad path found in chain graph\n", __func__); |
Steven Rostedt | dad3d74 | 2011-04-20 21:41:57 -0400 | [diff] [blame] | 2626 | break; |
| 2627 | } |
| 2628 | middle = entry; |
| 2629 | entry = get_lock_parent(entry); |
| 2630 | depth--; |
| 2631 | } while (entry && entry != root && (depth >= 0)); |
| 2632 | if (forwards) |
| 2633 | print_irq_lock_scenario(root, other, |
| 2634 | middle ? middle->class : root->class, other->class); |
| 2635 | else |
| 2636 | print_irq_lock_scenario(other, root, |
| 2637 | middle ? middle->class : other->class, root->class); |
| 2638 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2639 | lockdep_print_held_locks(curr); |
| 2640 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2641 | pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n"); |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2642 | if (!save_trace(&root->trace)) |
| 2643 | return 0; |
| 2644 | print_shortest_lock_dependencies(other, root); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2645 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 2646 | pr_warn("\nstack backtrace:\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2647 | dump_stack(); |
| 2648 | |
| 2649 | return 0; |
| 2650 | } |
| 2651 | |
| 2652 | /* |
| 2653 | * Prove that in the forwards-direction subgraph starting at <this> |
| 2654 | * there is no lock matching <mask>: |
| 2655 | */ |
| 2656 | static int |
| 2657 | check_usage_forwards(struct task_struct *curr, struct held_lock *this, |
| 2658 | enum lock_usage_bit bit, const char *irqclass) |
| 2659 | { |
| 2660 | int ret; |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2661 | struct lock_list root; |
| 2662 | struct lock_list *uninitialized_var(target_entry); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2663 | |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2664 | root.parent = NULL; |
| 2665 | root.class = hlock_class(this); |
| 2666 | ret = find_usage_forwards(&root, bit, &target_entry); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2667 | if (ret < 0) |
| 2668 | return print_bfs_bug(ret); |
| 2669 | if (ret == 1) |
| 2670 | return ret; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2671 | |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2672 | return print_irq_inversion_bug(curr, &root, target_entry, |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2673 | this, 1, irqclass); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | /* |
| 2677 | * Prove that in the backwards-direction subgraph starting at <this> |
| 2678 | * there is no lock matching <mask>: |
| 2679 | */ |
| 2680 | static int |
| 2681 | check_usage_backwards(struct task_struct *curr, struct held_lock *this, |
| 2682 | enum lock_usage_bit bit, const char *irqclass) |
| 2683 | { |
| 2684 | int ret; |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2685 | struct lock_list root; |
| 2686 | struct lock_list *uninitialized_var(target_entry); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2687 | |
Ming Lei | d7aaba1 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2688 | root.parent = NULL; |
| 2689 | root.class = hlock_class(this); |
| 2690 | ret = find_usage_backwards(&root, bit, &target_entry); |
Peter Zijlstra | af01296 | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2691 | if (ret < 0) |
| 2692 | return print_bfs_bug(ret); |
| 2693 | if (ret == 1) |
| 2694 | return ret; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2695 | |
Ming Lei | 24208ca | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 2696 | return print_irq_inversion_bug(curr, &root, target_entry, |
Oleg Nesterov | 48d5067 | 2010-01-26 19:16:41 +0100 | [diff] [blame] | 2697 | this, 0, irqclass); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2698 | } |
| 2699 | |
Ingo Molnar | 3117df0 | 2006-12-13 00:34:43 -0800 | [diff] [blame] | 2700 | void print_irqtrace_events(struct task_struct *curr) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2701 | { |
| 2702 | printk("irq event stamp: %u\n", curr->irq_events); |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 2703 | printk("hardirqs last enabled at (%u): [<%px>] %pS\n", |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2704 | curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip, |
| 2705 | (void *)curr->hardirq_enable_ip); |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 2706 | printk("hardirqs last disabled at (%u): [<%px>] %pS\n", |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2707 | curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip, |
| 2708 | (void *)curr->hardirq_disable_ip); |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 2709 | printk("softirqs last enabled at (%u): [<%px>] %pS\n", |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2710 | curr->softirq_enable_event, (void *)curr->softirq_enable_ip, |
| 2711 | (void *)curr->softirq_enable_ip); |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 2712 | printk("softirqs last disabled at (%u): [<%px>] %pS\n", |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 2713 | curr->softirq_disable_event, (void *)curr->softirq_disable_ip, |
| 2714 | (void *)curr->softirq_disable_ip); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2715 | } |
| 2716 | |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 2717 | static int HARDIRQ_verbose(struct lock_class *class) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2718 | { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2719 | #if HARDIRQ_VERBOSE |
| 2720 | return class_filter(class); |
| 2721 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2722 | return 0; |
| 2723 | } |
| 2724 | |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 2725 | static int SOFTIRQ_verbose(struct lock_class *class) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2726 | { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 2727 | #if SOFTIRQ_VERBOSE |
| 2728 | return class_filter(class); |
| 2729 | #endif |
| 2730 | return 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | #define STRICT_READ_CHECKS 1 |
| 2734 | |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 2735 | static int (*state_verbose_f[])(struct lock_class *class) = { |
| 2736 | #define LOCKDEP_STATE(__STATE) \ |
| 2737 | __STATE##_verbose, |
| 2738 | #include "lockdep_states.h" |
| 2739 | #undef LOCKDEP_STATE |
| 2740 | }; |
| 2741 | |
| 2742 | static inline int state_verbose(enum lock_usage_bit bit, |
| 2743 | struct lock_class *class) |
| 2744 | { |
| 2745 | return state_verbose_f[bit >> 2](class); |
| 2746 | } |
| 2747 | |
Peter Zijlstra | 42c50d5 | 2009-01-22 16:58:16 +0100 | [diff] [blame] | 2748 | typedef int (*check_usage_f)(struct task_struct *, struct held_lock *, |
| 2749 | enum lock_usage_bit bit, const char *name); |
| 2750 | |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 2751 | static int |
Peter Zijlstra | 1c21f14 | 2009-03-04 13:51:13 +0100 | [diff] [blame] | 2752 | mark_lock_irq(struct task_struct *curr, struct held_lock *this, |
| 2753 | enum lock_usage_bit new_bit) |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 2754 | { |
Peter Zijlstra | f989209 | 2009-01-22 16:09:59 +0100 | [diff] [blame] | 2755 | int excl_bit = exclusive_bit(new_bit); |
Frederic Weisbecker | bba2a8f | 2018-12-28 06:02:01 +0100 | [diff] [blame] | 2756 | int read = new_bit & LOCK_USAGE_READ_MASK; |
| 2757 | int dir = new_bit & LOCK_USAGE_DIR_MASK; |
Peter Zijlstra | 42c50d5 | 2009-01-22 16:58:16 +0100 | [diff] [blame] | 2758 | |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 2759 | /* |
| 2760 | * mark USED_IN has to look forwards -- to ensure no dependency |
| 2761 | * has ENABLED state, which would allow recursion deadlocks. |
| 2762 | * |
| 2763 | * mark ENABLED has to look backwards -- to ensure no dependee |
| 2764 | * has USED_IN state, which, again, would allow recursion deadlocks. |
| 2765 | */ |
Peter Zijlstra | 42c50d5 | 2009-01-22 16:58:16 +0100 | [diff] [blame] | 2766 | check_usage_f usage = dir ? |
| 2767 | check_usage_backwards : check_usage_forwards; |
Peter Zijlstra | f989209 | 2009-01-22 16:09:59 +0100 | [diff] [blame] | 2768 | |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 2769 | /* |
| 2770 | * Validate that this particular lock does not have conflicting |
| 2771 | * usage states. |
| 2772 | */ |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 2773 | if (!valid_state(curr, this, new_bit, excl_bit)) |
| 2774 | return 0; |
Peter Zijlstra | 9d3651a | 2009-01-22 17:18:32 +0100 | [diff] [blame] | 2775 | |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 2776 | /* |
| 2777 | * Validate that the lock dependencies don't have conflicting usage |
| 2778 | * states. |
| 2779 | */ |
| 2780 | if ((!read || !dir || STRICT_READ_CHECKS) && |
Frederic Weisbecker | bba2a8f | 2018-12-28 06:02:01 +0100 | [diff] [blame] | 2781 | !usage(curr, this, excl_bit, state_name(new_bit & ~LOCK_USAGE_READ_MASK))) |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 2782 | return 0; |
Peter Zijlstra | 780e820 | 2009-01-22 16:51:29 +0100 | [diff] [blame] | 2783 | |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 2784 | /* |
| 2785 | * Check for read in write conflicts |
| 2786 | */ |
| 2787 | if (!read) { |
Frederic Weisbecker | bba2a8f | 2018-12-28 06:02:01 +0100 | [diff] [blame] | 2788 | if (!valid_state(curr, this, new_bit, excl_bit + LOCK_USAGE_READ_MASK)) |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 2789 | return 0; |
| 2790 | |
| 2791 | if (STRICT_READ_CHECKS && |
Frederic Weisbecker | bba2a8f | 2018-12-28 06:02:01 +0100 | [diff] [blame] | 2792 | !usage(curr, this, excl_bit + LOCK_USAGE_READ_MASK, |
| 2793 | state_name(new_bit + LOCK_USAGE_READ_MASK))) |
Peter Zijlstra | 38aa271 | 2009-01-27 14:53:50 +0100 | [diff] [blame] | 2794 | return 0; |
| 2795 | } |
Peter Zijlstra | 780e820 | 2009-01-22 16:51:29 +0100 | [diff] [blame] | 2796 | |
Peter Zijlstra | cd95302 | 2009-01-22 16:38:21 +0100 | [diff] [blame] | 2797 | if (state_verbose(new_bit, hlock_class(this))) |
Peter Zijlstra | 6a6904d | 2009-01-22 16:07:44 +0100 | [diff] [blame] | 2798 | return 2; |
| 2799 | |
| 2800 | return 1; |
| 2801 | } |
| 2802 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2803 | /* |
| 2804 | * Mark all held locks with a usage bit: |
| 2805 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 2806 | static int |
Frederic Weisbecker | 436a49a | 2018-12-28 06:02:00 +0100 | [diff] [blame] | 2807 | 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] | 2808 | { |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2809 | struct held_lock *hlock; |
| 2810 | int i; |
| 2811 | |
| 2812 | for (i = 0; i < curr->lockdep_depth; i++) { |
Frederic Weisbecker | 436a49a | 2018-12-28 06:02:00 +0100 | [diff] [blame] | 2813 | enum lock_usage_bit hlock_bit = base_bit; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2814 | hlock = curr->held_locks + i; |
| 2815 | |
Peter Zijlstra | cf2ad4d | 2009-01-27 13:58:08 +0100 | [diff] [blame] | 2816 | if (hlock->read) |
Frederic Weisbecker | bba2a8f | 2018-12-28 06:02:01 +0100 | [diff] [blame] | 2817 | hlock_bit += LOCK_USAGE_READ_MASK; |
Peter Zijlstra | cf2ad4d | 2009-01-27 13:58:08 +0100 | [diff] [blame] | 2818 | |
Frederic Weisbecker | 436a49a | 2018-12-28 06:02:00 +0100 | [diff] [blame] | 2819 | BUG_ON(hlock_bit >= LOCK_USAGE_STATES); |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 2820 | |
Oleg Nesterov | 34d0ed5 | 2014-01-20 19:20:13 +0100 | [diff] [blame] | 2821 | if (!hlock->check) |
Peter Zijlstra | efbe2ee | 2011-07-07 11:39:45 +0200 | [diff] [blame] | 2822 | continue; |
| 2823 | |
Frederic Weisbecker | 436a49a | 2018-12-28 06:02:00 +0100 | [diff] [blame] | 2824 | if (!mark_lock(curr, hlock, hlock_bit)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2825 | return 0; |
| 2826 | } |
| 2827 | |
| 2828 | return 1; |
| 2829 | } |
| 2830 | |
| 2831 | /* |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2832 | * Hardirqs will be enabled: |
| 2833 | */ |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 2834 | static void __trace_hardirqs_on_caller(unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2835 | { |
| 2836 | struct task_struct *curr = current; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2837 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2838 | /* we'll do an OFF -> ON transition: */ |
| 2839 | curr->hardirqs_enabled = 1; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2840 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2841 | /* |
| 2842 | * We are going to turn hardirqs on, so set the |
| 2843 | * usage bit for all held locks: |
| 2844 | */ |
Frederic Weisbecker | 436a49a | 2018-12-28 06:02:00 +0100 | [diff] [blame] | 2845 | if (!mark_held_locks(curr, LOCK_ENABLED_HARDIRQ)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2846 | return; |
| 2847 | /* |
| 2848 | * If we have softirqs enabled, then set the usage |
| 2849 | * bit for all held locks. (disabled hardirqs prevented |
| 2850 | * this bit from being set before) |
| 2851 | */ |
| 2852 | if (curr->softirqs_enabled) |
Frederic Weisbecker | 436a49a | 2018-12-28 06:02:00 +0100 | [diff] [blame] | 2853 | if (!mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2854 | return; |
| 2855 | |
| 2856 | curr->hardirq_enable_ip = ip; |
| 2857 | curr->hardirq_enable_event = ++curr->irq_events; |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 2858 | debug_atomic_inc(hardirqs_on_events); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2859 | } |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 2860 | |
Steven Rostedt (VMware) | bff1b20 | 2018-08-06 15:50:58 -0400 | [diff] [blame] | 2861 | void lockdep_hardirqs_on(unsigned long ip) |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 2862 | { |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 2863 | if (unlikely(!debug_locks || current->lockdep_recursion)) |
| 2864 | return; |
| 2865 | |
Peter Zijlstra | 7d36b26 | 2011-07-26 13:13:44 +0200 | [diff] [blame] | 2866 | if (unlikely(current->hardirqs_enabled)) { |
| 2867 | /* |
| 2868 | * Neither irq nor preemption are disabled here |
| 2869 | * so this is racy by nature but losing one hit |
| 2870 | * in a stat is not a big deal. |
| 2871 | */ |
| 2872 | __debug_atomic_inc(redundant_hardirqs_on); |
| 2873 | return; |
| 2874 | } |
| 2875 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2876 | /* |
| 2877 | * We're enabling irqs and according to our state above irqs weren't |
| 2878 | * already enabled, yet we find the hardware thinks they are in fact |
| 2879 | * enabled.. someone messed up their IRQ state tracing. |
| 2880 | */ |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 2881 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 2882 | return; |
| 2883 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2884 | /* |
| 2885 | * See the fine text that goes along with this variable definition. |
| 2886 | */ |
Peter Zijlstra | 7d36b26 | 2011-07-26 13:13:44 +0200 | [diff] [blame] | 2887 | if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled))) |
| 2888 | return; |
| 2889 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2890 | /* |
| 2891 | * Can't allow enabling interrupts while in an interrupt handler, |
| 2892 | * that's general bad form and such. Recursion, limited stack etc.. |
| 2893 | */ |
Peter Zijlstra | 7d36b26 | 2011-07-26 13:13:44 +0200 | [diff] [blame] | 2894 | if (DEBUG_LOCKS_WARN_ON(current->hardirq_context)) |
| 2895 | return; |
| 2896 | |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 2897 | current->lockdep_recursion = 1; |
| 2898 | __trace_hardirqs_on_caller(ip); |
| 2899 | current->lockdep_recursion = 0; |
| 2900 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2901 | |
| 2902 | /* |
| 2903 | * Hardirqs were disabled: |
| 2904 | */ |
Steven Rostedt (VMware) | bff1b20 | 2018-08-06 15:50:58 -0400 | [diff] [blame] | 2905 | void lockdep_hardirqs_off(unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2906 | { |
| 2907 | struct task_struct *curr = current; |
| 2908 | |
| 2909 | if (unlikely(!debug_locks || current->lockdep_recursion)) |
| 2910 | return; |
| 2911 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2912 | /* |
| 2913 | * So we're supposed to get called after you mask local IRQs, but for |
| 2914 | * some reason the hardware doesn't quite think you did a proper job. |
| 2915 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2916 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 2917 | return; |
| 2918 | |
| 2919 | if (curr->hardirqs_enabled) { |
| 2920 | /* |
| 2921 | * We have done an ON -> OFF transition: |
| 2922 | */ |
| 2923 | curr->hardirqs_enabled = 0; |
Heiko Carstens | 6afe40b | 2008-10-28 11:14:58 +0100 | [diff] [blame] | 2924 | curr->hardirq_disable_ip = ip; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2925 | curr->hardirq_disable_event = ++curr->irq_events; |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 2926 | debug_atomic_inc(hardirqs_off_events); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2927 | } else |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 2928 | debug_atomic_inc(redundant_hardirqs_off); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2929 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2930 | |
| 2931 | /* |
| 2932 | * Softirqs will be enabled: |
| 2933 | */ |
| 2934 | void trace_softirqs_on(unsigned long ip) |
| 2935 | { |
| 2936 | struct task_struct *curr = current; |
| 2937 | |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 2938 | if (unlikely(!debug_locks || current->lockdep_recursion)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2939 | return; |
| 2940 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2941 | /* |
| 2942 | * We fancy IRQs being disabled here, see softirq.c, avoids |
| 2943 | * funny state and nesting things. |
| 2944 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2945 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 2946 | return; |
| 2947 | |
| 2948 | if (curr->softirqs_enabled) { |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 2949 | debug_atomic_inc(redundant_softirqs_on); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2950 | return; |
| 2951 | } |
| 2952 | |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 2953 | current->lockdep_recursion = 1; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2954 | /* |
| 2955 | * We'll do an OFF -> ON transition: |
| 2956 | */ |
| 2957 | curr->softirqs_enabled = 1; |
| 2958 | curr->softirq_enable_ip = ip; |
| 2959 | curr->softirq_enable_event = ++curr->irq_events; |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 2960 | debug_atomic_inc(softirqs_on_events); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2961 | /* |
| 2962 | * We are going to turn softirqs on, so set the |
| 2963 | * usage bit for all held locks, if hardirqs are |
| 2964 | * enabled too: |
| 2965 | */ |
| 2966 | if (curr->hardirqs_enabled) |
Frederic Weisbecker | 436a49a | 2018-12-28 06:02:00 +0100 | [diff] [blame] | 2967 | mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ); |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 2968 | current->lockdep_recursion = 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | /* |
| 2972 | * Softirqs were disabled: |
| 2973 | */ |
| 2974 | void trace_softirqs_off(unsigned long ip) |
| 2975 | { |
| 2976 | struct task_struct *curr = current; |
| 2977 | |
Peter Zijlstra | dd4e5d3 | 2011-06-21 17:17:27 +0200 | [diff] [blame] | 2978 | if (unlikely(!debug_locks || current->lockdep_recursion)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2979 | return; |
| 2980 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2981 | /* |
| 2982 | * We fancy IRQs being disabled here, see softirq.c |
| 2983 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2984 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 2985 | return; |
| 2986 | |
| 2987 | if (curr->softirqs_enabled) { |
| 2988 | /* |
| 2989 | * We have done an ON -> OFF transition: |
| 2990 | */ |
| 2991 | curr->softirqs_enabled = 0; |
| 2992 | curr->softirq_disable_ip = ip; |
| 2993 | curr->softirq_disable_event = ++curr->irq_events; |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 2994 | debug_atomic_inc(softirqs_off_events); |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 2995 | /* |
| 2996 | * Whoops, we wanted softirqs off, so why aren't they? |
| 2997 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 2998 | DEBUG_LOCKS_WARN_ON(!softirq_count()); |
| 2999 | } else |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 3000 | debug_atomic_inc(redundant_softirqs_off); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3001 | } |
| 3002 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3003 | static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock) |
| 3004 | { |
| 3005 | /* |
| 3006 | * If non-trylock use in a hardirq or softirq context, then |
| 3007 | * mark the lock as used in these contexts: |
| 3008 | */ |
| 3009 | if (!hlock->trylock) { |
| 3010 | if (hlock->read) { |
| 3011 | if (curr->hardirq_context) |
| 3012 | if (!mark_lock(curr, hlock, |
| 3013 | LOCK_USED_IN_HARDIRQ_READ)) |
| 3014 | return 0; |
| 3015 | if (curr->softirq_context) |
| 3016 | if (!mark_lock(curr, hlock, |
| 3017 | LOCK_USED_IN_SOFTIRQ_READ)) |
| 3018 | return 0; |
| 3019 | } else { |
| 3020 | if (curr->hardirq_context) |
| 3021 | if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ)) |
| 3022 | return 0; |
| 3023 | if (curr->softirq_context) |
| 3024 | if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ)) |
| 3025 | return 0; |
| 3026 | } |
| 3027 | } |
| 3028 | if (!hlock->hardirqs_off) { |
| 3029 | if (hlock->read) { |
| 3030 | if (!mark_lock(curr, hlock, |
Peter Zijlstra | 4fc95e8 | 2009-01-22 13:10:52 +0100 | [diff] [blame] | 3031 | LOCK_ENABLED_HARDIRQ_READ)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3032 | return 0; |
| 3033 | if (curr->softirqs_enabled) |
| 3034 | if (!mark_lock(curr, hlock, |
Peter Zijlstra | 4fc95e8 | 2009-01-22 13:10:52 +0100 | [diff] [blame] | 3035 | LOCK_ENABLED_SOFTIRQ_READ)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3036 | return 0; |
| 3037 | } else { |
| 3038 | if (!mark_lock(curr, hlock, |
Peter Zijlstra | 4fc95e8 | 2009-01-22 13:10:52 +0100 | [diff] [blame] | 3039 | LOCK_ENABLED_HARDIRQ)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3040 | return 0; |
| 3041 | if (curr->softirqs_enabled) |
| 3042 | if (!mark_lock(curr, hlock, |
Peter Zijlstra | 4fc95e8 | 2009-01-22 13:10:52 +0100 | [diff] [blame] | 3043 | LOCK_ENABLED_SOFTIRQ)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3044 | return 0; |
| 3045 | } |
| 3046 | } |
| 3047 | |
| 3048 | return 1; |
| 3049 | } |
| 3050 | |
Boqun Feng | c246975 | 2016-02-16 13:57:40 +0800 | [diff] [blame] | 3051 | static inline unsigned int task_irq_context(struct task_struct *task) |
| 3052 | { |
| 3053 | return 2 * !!task->hardirq_context + !!task->softirq_context; |
| 3054 | } |
| 3055 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3056 | static int separate_irq_context(struct task_struct *curr, |
| 3057 | struct held_lock *hlock) |
| 3058 | { |
| 3059 | unsigned int depth = curr->lockdep_depth; |
| 3060 | |
| 3061 | /* |
| 3062 | * Keep track of points where we cross into an interrupt context: |
| 3063 | */ |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3064 | if (depth) { |
| 3065 | struct held_lock *prev_hlock; |
| 3066 | |
| 3067 | prev_hlock = curr->held_locks + depth-1; |
| 3068 | /* |
| 3069 | * If we cross into another context, reset the |
| 3070 | * hash key (this also prevents the checking and the |
| 3071 | * adding of the dependency to 'prev'): |
| 3072 | */ |
| 3073 | if (prev_hlock->irq_context != hlock->irq_context) |
| 3074 | return 1; |
| 3075 | } |
| 3076 | return 0; |
| 3077 | } |
| 3078 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3079 | #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */ |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3080 | |
| 3081 | static inline |
| 3082 | int mark_lock_irq(struct task_struct *curr, struct held_lock *this, |
| 3083 | enum lock_usage_bit new_bit) |
| 3084 | { |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3085 | WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */ |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3086 | return 1; |
| 3087 | } |
| 3088 | |
| 3089 | static inline int mark_irqflags(struct task_struct *curr, |
| 3090 | struct held_lock *hlock) |
| 3091 | { |
| 3092 | return 1; |
| 3093 | } |
| 3094 | |
Boqun Feng | c246975 | 2016-02-16 13:57:40 +0800 | [diff] [blame] | 3095 | static inline unsigned int task_irq_context(struct task_struct *task) |
| 3096 | { |
| 3097 | return 0; |
| 3098 | } |
| 3099 | |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3100 | static inline int separate_irq_context(struct task_struct *curr, |
| 3101 | struct held_lock *hlock) |
| 3102 | { |
| 3103 | return 0; |
| 3104 | } |
| 3105 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3106 | #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3107 | |
| 3108 | /* |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3109 | * Mark a lock with a usage bit, and validate the state transition: |
| 3110 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 3111 | static int mark_lock(struct task_struct *curr, struct held_lock *this, |
Steven Rostedt | 0764d23 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 3112 | enum lock_usage_bit new_bit) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3113 | { |
| 3114 | unsigned int new_mask = 1 << new_bit, ret = 1; |
| 3115 | |
| 3116 | /* |
| 3117 | * If already set then do not dirty the cacheline, |
| 3118 | * nor do any checks: |
| 3119 | */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 3120 | if (likely(hlock_class(this)->usage_mask & new_mask)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3121 | return 1; |
| 3122 | |
| 3123 | if (!graph_lock()) |
| 3124 | return 0; |
| 3125 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3126 | * Make sure we didn't race: |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3127 | */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 3128 | if (unlikely(hlock_class(this)->usage_mask & new_mask)) { |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3129 | graph_unlock(); |
| 3130 | return 1; |
| 3131 | } |
| 3132 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 3133 | hlock_class(this)->usage_mask |= new_mask; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3134 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 3135 | if (!save_trace(hlock_class(this)->usage_traces + new_bit)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3136 | return 0; |
| 3137 | |
| 3138 | switch (new_bit) { |
Peter Zijlstra | 5346417 | 2009-01-22 14:15:53 +0100 | [diff] [blame] | 3139 | #define LOCKDEP_STATE(__STATE) \ |
| 3140 | case LOCK_USED_IN_##__STATE: \ |
| 3141 | case LOCK_USED_IN_##__STATE##_READ: \ |
| 3142 | case LOCK_ENABLED_##__STATE: \ |
| 3143 | case LOCK_ENABLED_##__STATE##_READ: |
| 3144 | #include "lockdep_states.h" |
| 3145 | #undef LOCKDEP_STATE |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3146 | ret = mark_lock_irq(curr, this, new_bit); |
| 3147 | if (!ret) |
| 3148 | return 0; |
| 3149 | break; |
| 3150 | case LOCK_USED: |
Frederic Weisbecker | bd6d29c | 2010-04-06 00:10:17 +0200 | [diff] [blame] | 3151 | debug_atomic_dec(nr_unused_locks); |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3152 | break; |
| 3153 | default: |
| 3154 | if (!debug_locks_off_graph_unlock()) |
| 3155 | return 0; |
| 3156 | WARN_ON(1); |
| 3157 | return 0; |
| 3158 | } |
| 3159 | |
| 3160 | graph_unlock(); |
| 3161 | |
| 3162 | /* |
| 3163 | * We must printk outside of the graph_lock: |
| 3164 | */ |
| 3165 | if (ret == 2) { |
| 3166 | printk("\nmarked lock as {%s}:\n", usage_str[new_bit]); |
| 3167 | print_lock(this); |
| 3168 | print_irqtrace_events(curr); |
| 3169 | dump_stack(); |
| 3170 | } |
| 3171 | |
| 3172 | return ret; |
| 3173 | } |
| 3174 | |
| 3175 | /* |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3176 | * Initialize a lock instance's lock-class mapping info: |
| 3177 | */ |
Bart Van Assche | d35568b | 2018-12-06 17:11:33 -0800 | [diff] [blame] | 3178 | void lockdep_init_map(struct lockdep_map *lock, const char *name, |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 3179 | struct lock_class_key *key, int subclass) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3180 | { |
Yong Zhang | d3d03d4 | 2011-11-09 16:04:51 +0800 | [diff] [blame] | 3181 | int i; |
| 3182 | |
Yong Zhang | d3d03d4 | 2011-11-09 16:04:51 +0800 | [diff] [blame] | 3183 | for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++) |
| 3184 | lock->class_cache[i] = NULL; |
Hitoshi Mitake | 6201625 | 2010-10-05 18:01:51 +0900 | [diff] [blame] | 3185 | |
Peter Zijlstra | c8a2500 | 2009-04-17 09:40:49 +0200 | [diff] [blame] | 3186 | #ifdef CONFIG_LOCK_STAT |
| 3187 | lock->cpu = raw_smp_processor_id(); |
| 3188 | #endif |
| 3189 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3190 | /* |
| 3191 | * Can't be having no nameless bastards around this place! |
| 3192 | */ |
Peter Zijlstra | c8a2500 | 2009-04-17 09:40:49 +0200 | [diff] [blame] | 3193 | if (DEBUG_LOCKS_WARN_ON(!name)) { |
| 3194 | lock->name = "NULL"; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3195 | return; |
Peter Zijlstra | c8a2500 | 2009-04-17 09:40:49 +0200 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | lock->name = name; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3199 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3200 | /* |
| 3201 | * No key, no joy, we need to hash something. |
| 3202 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3203 | if (DEBUG_LOCKS_WARN_ON(!key)) |
| 3204 | return; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3205 | /* |
| 3206 | * Sanity check, the lock-class key must be persistent: |
| 3207 | */ |
| 3208 | if (!static_obj(key)) { |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 3209 | printk("BUG: key %px not in .data!\n", key); |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3210 | /* |
| 3211 | * What it says above ^^^^^, I suggest you read it. |
| 3212 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3213 | DEBUG_LOCKS_WARN_ON(1); |
| 3214 | return; |
| 3215 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3216 | lock->key = key; |
Peter Zijlstra | c8a2500 | 2009-04-17 09:40:49 +0200 | [diff] [blame] | 3217 | |
| 3218 | if (unlikely(!debug_locks)) |
| 3219 | return; |
| 3220 | |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 3221 | if (subclass) { |
| 3222 | unsigned long flags; |
| 3223 | |
| 3224 | if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion)) |
| 3225 | return; |
| 3226 | |
| 3227 | raw_local_irq_save(flags); |
| 3228 | current->lockdep_recursion = 1; |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 3229 | register_lock_class(lock, subclass, 1); |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 3230 | current->lockdep_recursion = 0; |
| 3231 | raw_local_irq_restore(flags); |
| 3232 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3233 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3234 | EXPORT_SYMBOL_GPL(lockdep_init_map); |
| 3235 | |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 3236 | struct lock_class_key __lockdep_no_validate__; |
Kent Overstreet | ea6749c | 2012-12-27 22:21:58 -0800 | [diff] [blame] | 3237 | EXPORT_SYMBOL_GPL(__lockdep_no_validate__); |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 3238 | |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 3239 | static int |
| 3240 | print_lock_nested_lock_not_held(struct task_struct *curr, |
| 3241 | struct held_lock *hlock, |
| 3242 | unsigned long ip) |
| 3243 | { |
| 3244 | if (!debug_locks_off()) |
| 3245 | return 0; |
| 3246 | if (debug_locks_silent) |
| 3247 | return 0; |
| 3248 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3249 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 3250 | pr_warn("==================================\n"); |
| 3251 | pr_warn("WARNING: Nested lock was not taken\n"); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 3252 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 3253 | pr_warn("----------------------------------\n"); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 3254 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3255 | 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] | 3256 | print_lock(hlock); |
| 3257 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3258 | pr_warn("\nbut this task is not holding:\n"); |
| 3259 | pr_warn("%s\n", hlock->nest_lock->name); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 3260 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3261 | pr_warn("\nstack backtrace:\n"); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 3262 | dump_stack(); |
| 3263 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3264 | pr_warn("\nother info that might help us debug this:\n"); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 3265 | lockdep_print_held_locks(curr); |
| 3266 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3267 | pr_warn("\nstack backtrace:\n"); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 3268 | dump_stack(); |
| 3269 | |
| 3270 | return 0; |
| 3271 | } |
| 3272 | |
Matthew Wilcox | 08f36ff | 2018-01-17 07:14:13 -0800 | [diff] [blame] | 3273 | static int __lock_is_held(const struct lockdep_map *lock, int read); |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 3274 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3275 | /* |
| 3276 | * This gets called for every mutex_lock*()/spin_lock*() operation. |
| 3277 | * We maintain the dependency maps and validate the locking attempt: |
Waiman Long | 8ee1086 | 2018-10-02 16:19:17 -0400 | [diff] [blame] | 3278 | * |
| 3279 | * The callers must make sure that IRQs are disabled before calling it, |
| 3280 | * otherwise we could get an interrupt which would want to take locks, |
| 3281 | * which would end up in lockdep again. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3282 | */ |
| 3283 | static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass, |
| 3284 | int trylock, int read, int check, int hardirqs_off, |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3285 | struct lockdep_map *nest_lock, unsigned long ip, |
Peter Zijlstra | 21199f2 | 2015-09-16 16:10:40 +0200 | [diff] [blame] | 3286 | int references, int pin_count) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3287 | { |
| 3288 | struct task_struct *curr = current; |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 3289 | struct lock_class *class = NULL; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3290 | struct held_lock *hlock; |
Alfredo Alvarez Fernandez | 5f18ab5 | 2016-02-11 00:33:32 +0100 | [diff] [blame] | 3291 | unsigned int depth; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3292 | int chain_head = 0; |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3293 | int class_idx; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3294 | u64 chain_key; |
| 3295 | |
| 3296 | if (unlikely(!debug_locks)) |
| 3297 | return 0; |
| 3298 | |
Oleg Nesterov | fb9edbe | 2014-01-20 19:20:06 +0100 | [diff] [blame] | 3299 | if (!prove_locking || lock->key == &__lockdep_no_validate__) |
| 3300 | check = 0; |
Peter Zijlstra | 1704f47 | 2010-03-19 01:37:42 +0100 | [diff] [blame] | 3301 | |
Hitoshi Mitake | 6201625 | 2010-10-05 18:01:51 +0900 | [diff] [blame] | 3302 | if (subclass < NR_LOCKDEP_CACHING_CLASSES) |
| 3303 | class = lock->class_cache[subclass]; |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 3304 | /* |
Hitoshi Mitake | 6201625 | 2010-10-05 18:01:51 +0900 | [diff] [blame] | 3305 | * Not cached? |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 3306 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3307 | if (unlikely(!class)) { |
Peter Zijlstra | 4dfbb9d | 2006-10-11 01:45:14 -0400 | [diff] [blame] | 3308 | class = register_lock_class(lock, subclass, 0); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3309 | if (!class) |
| 3310 | return 0; |
| 3311 | } |
Waiman Long | 8ca2b56c | 2018-10-03 13:07:18 -0400 | [diff] [blame] | 3312 | |
| 3313 | debug_class_ops_inc(class); |
| 3314 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3315 | if (very_verbose(class)) { |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 3316 | printk("\nacquire class [%px] %s", class->key, class->name); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3317 | if (class->name_version > 1) |
Dmitry Vyukov | f943fe0 | 2016-11-28 15:24:43 +0100 | [diff] [blame] | 3318 | printk(KERN_CONT "#%d", class->name_version); |
| 3319 | printk(KERN_CONT "\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3320 | dump_stack(); |
| 3321 | } |
| 3322 | |
| 3323 | /* |
| 3324 | * Add the lock to the list of currently held locks. |
| 3325 | * (we dont increase the depth just yet, up until the |
| 3326 | * dependency checks are done) |
| 3327 | */ |
| 3328 | depth = curr->lockdep_depth; |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3329 | /* |
| 3330 | * Ran out of static storage for our per-task lock stack again have we? |
| 3331 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3332 | if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH)) |
| 3333 | return 0; |
| 3334 | |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3335 | class_idx = class - lock_classes + 1; |
| 3336 | |
Ingo Molnar | e966eae | 2017-12-12 12:31:16 +0100 | [diff] [blame] | 3337 | if (depth) { |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3338 | hlock = curr->held_locks + depth - 1; |
| 3339 | if (hlock->class_idx == class_idx && nest_lock) { |
Peter Zijlstra | 7fb4a2c | 2017-03-01 16:23:30 +0100 | [diff] [blame] | 3340 | if (hlock->references) { |
| 3341 | /* |
| 3342 | * Check: unsigned int references:12, overflow. |
| 3343 | */ |
| 3344 | if (DEBUG_LOCKS_WARN_ON(hlock->references == (1 << 12)-1)) |
| 3345 | return 0; |
| 3346 | |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3347 | hlock->references++; |
Peter Zijlstra | 7fb4a2c | 2017-03-01 16:23:30 +0100 | [diff] [blame] | 3348 | } else { |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3349 | hlock->references = 2; |
Peter Zijlstra | 7fb4a2c | 2017-03-01 16:23:30 +0100 | [diff] [blame] | 3350 | } |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3351 | |
| 3352 | return 1; |
| 3353 | } |
| 3354 | } |
| 3355 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3356 | hlock = curr->held_locks + depth; |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3357 | /* |
| 3358 | * Plain impossible, we just registered it and checked it weren't no |
| 3359 | * NULL like.. I bet this mushroom I ate was good! |
| 3360 | */ |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 3361 | if (DEBUG_LOCKS_WARN_ON(!class)) |
| 3362 | return 0; |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3363 | hlock->class_idx = class_idx; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3364 | hlock->acquire_ip = ip; |
| 3365 | hlock->instance = lock; |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 3366 | hlock->nest_lock = nest_lock; |
Boqun Feng | c246975 | 2016-02-16 13:57:40 +0800 | [diff] [blame] | 3367 | hlock->irq_context = task_irq_context(curr); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3368 | hlock->trylock = trylock; |
| 3369 | hlock->read = read; |
| 3370 | hlock->check = check; |
Dmitry Baryshkov | 6951b12 | 2008-08-18 04:26:37 +0400 | [diff] [blame] | 3371 | hlock->hardirqs_off = !!hardirqs_off; |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3372 | hlock->references = references; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 3373 | #ifdef CONFIG_LOCK_STAT |
| 3374 | hlock->waittime_stamp = 0; |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 3375 | hlock->holdtime_stamp = lockstat_clock(); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 3376 | #endif |
Peter Zijlstra | 21199f2 | 2015-09-16 16:10:40 +0200 | [diff] [blame] | 3377 | hlock->pin_count = pin_count; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3378 | |
Oleg Nesterov | fb9edbe | 2014-01-20 19:20:06 +0100 | [diff] [blame] | 3379 | if (check && !mark_irqflags(curr, hlock)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3380 | return 0; |
| 3381 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3382 | /* mark it as used: */ |
Jarek Poplawski | 4ff773bb | 2007-05-08 00:31:00 -0700 | [diff] [blame] | 3383 | if (!mark_lock(curr, hlock, LOCK_USED)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3384 | return 0; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3385 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3386 | /* |
Gautham R Shenoy | 17aacfb9 | 2007-10-28 20:47:01 +0100 | [diff] [blame] | 3387 | * Calculate the chain hash: it's the combined hash of all the |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3388 | * lock keys along the dependency chain. We save the hash value |
| 3389 | * at every step so that we can get the current hash easily |
| 3390 | * after unlock. The chain hash is then used to cache dependency |
| 3391 | * results. |
| 3392 | * |
| 3393 | * The 'key ID' is what is the most compact key value to drive |
| 3394 | * the hash, not class->key. |
| 3395 | */ |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3396 | /* |
| 3397 | * Whoops, we did it again.. ran straight out of our static allocation. |
| 3398 | */ |
Alfredo Alvarez Fernandez | 5f18ab5 | 2016-02-11 00:33:32 +0100 | [diff] [blame] | 3399 | if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS)) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3400 | return 0; |
| 3401 | |
| 3402 | chain_key = curr->curr_chain_key; |
| 3403 | if (!depth) { |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3404 | /* |
| 3405 | * How can we have a chain hash when we ain't got no keys?! |
| 3406 | */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3407 | if (DEBUG_LOCKS_WARN_ON(chain_key != 0)) |
| 3408 | return 0; |
| 3409 | chain_head = 1; |
| 3410 | } |
| 3411 | |
| 3412 | hlock->prev_chain_key = chain_key; |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3413 | if (separate_irq_context(curr, hlock)) { |
| 3414 | chain_key = 0; |
| 3415 | chain_head = 1; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3416 | } |
Alfredo Alvarez Fernandez | 5f18ab5 | 2016-02-11 00:33:32 +0100 | [diff] [blame] | 3417 | chain_key = iterate_chain_key(chain_key, class_idx); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3418 | |
Peter Zijlstra | f831948 | 2016-11-30 14:32:25 +1100 | [diff] [blame] | 3419 | if (nest_lock && !__lock_is_held(nest_lock, -1)) |
Maarten Lankhorst | d094595 | 2012-09-13 11:39:51 +0200 | [diff] [blame] | 3420 | return print_lock_nested_lock_not_held(curr, hlock, ip); |
| 3421 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 3422 | if (!debug_locks_silent) { |
| 3423 | WARN_ON_ONCE(depth && !hlock_class(hlock - 1)->key); |
| 3424 | WARN_ON_ONCE(!hlock_class(hlock)->key); |
| 3425 | } |
| 3426 | |
Gregory Haskins | 3aa416b | 2007-10-11 22:11:11 +0200 | [diff] [blame] | 3427 | if (!validate_chain(curr, lock, hlock, chain_head, chain_key)) |
Peter Zijlstra | 8e18257 | 2007-07-19 01:48:54 -0700 | [diff] [blame] | 3428 | return 0; |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 3429 | |
Gregory Haskins | 3aa416b | 2007-10-11 22:11:11 +0200 | [diff] [blame] | 3430 | curr->curr_chain_key = chain_key; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3431 | curr->lockdep_depth++; |
| 3432 | check_chain_key(curr); |
Jarek Poplawski | 60e114d | 2007-02-20 13:58:00 -0800 | [diff] [blame] | 3433 | #ifdef CONFIG_DEBUG_LOCKDEP |
| 3434 | if (unlikely(!debug_locks)) |
| 3435 | return 0; |
| 3436 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3437 | if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) { |
| 3438 | debug_locks_off(); |
Dave Jones | 2c52283 | 2013-04-25 13:40:02 -0400 | [diff] [blame] | 3439 | print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!"); |
| 3440 | printk(KERN_DEBUG "depth: %i max: %lu!\n", |
Ben Greear | c054060 | 2013-02-06 10:56:19 -0800 | [diff] [blame] | 3441 | curr->lockdep_depth, MAX_LOCK_DEPTH); |
Ben Greear | c054060 | 2013-02-06 10:56:19 -0800 | [diff] [blame] | 3442 | |
| 3443 | lockdep_print_held_locks(current); |
| 3444 | debug_show_all_locks(); |
Peter Zijlstra | eedeeab | 2009-03-18 12:38:47 +0100 | [diff] [blame] | 3445 | dump_stack(); |
Ben Greear | c054060 | 2013-02-06 10:56:19 -0800 | [diff] [blame] | 3446 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3447 | return 0; |
| 3448 | } |
Jarek Poplawski | 381a229 | 2007-02-10 01:44:58 -0800 | [diff] [blame] | 3449 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3450 | if (unlikely(curr->lockdep_depth > max_lockdep_depth)) |
| 3451 | max_lockdep_depth = curr->lockdep_depth; |
| 3452 | |
| 3453 | return 1; |
| 3454 | } |
| 3455 | |
| 3456 | static int |
Srivatsa S. Bhat | f86f755 | 2013-01-08 18:35:58 +0530 | [diff] [blame] | 3457 | print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock, |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3458 | unsigned long ip) |
| 3459 | { |
| 3460 | if (!debug_locks_off()) |
| 3461 | return 0; |
| 3462 | if (debug_locks_silent) |
| 3463 | return 0; |
| 3464 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3465 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 3466 | pr_warn("=====================================\n"); |
| 3467 | pr_warn("WARNING: bad unlock balance detected!\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 3468 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 3469 | pr_warn("-------------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3470 | pr_warn("%s/%d is trying to release lock (", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 3471 | curr->comm, task_pid_nr(curr)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3472 | print_lockdep_cache(lock); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3473 | pr_cont(") at:\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3474 | print_ip_sym(ip); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3475 | pr_warn("but there are no more locks to release!\n"); |
| 3476 | pr_warn("\nother info that might help us debug this:\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3477 | lockdep_print_held_locks(curr); |
| 3478 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 3479 | pr_warn("\nstack backtrace:\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3480 | dump_stack(); |
| 3481 | |
| 3482 | return 0; |
| 3483 | } |
| 3484 | |
Matthew Wilcox | 08f36ff | 2018-01-17 07:14:13 -0800 | [diff] [blame] | 3485 | static int match_held_lock(const struct held_lock *hlock, |
| 3486 | const struct lockdep_map *lock) |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3487 | { |
| 3488 | if (hlock->instance == lock) |
| 3489 | return 1; |
| 3490 | |
| 3491 | if (hlock->references) { |
Matthew Wilcox | 08f36ff | 2018-01-17 07:14:13 -0800 | [diff] [blame] | 3492 | const struct lock_class *class = lock->class_cache[0]; |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3493 | |
| 3494 | if (!class) |
| 3495 | class = look_up_lock_class(lock, 0); |
| 3496 | |
Peter Zijlstra | 80e0401 | 2011-08-05 14:26:17 +0200 | [diff] [blame] | 3497 | /* |
| 3498 | * If look_up_lock_class() failed to find a class, we're trying |
| 3499 | * to test if we hold a lock that has never yet been acquired. |
| 3500 | * Clearly if the lock hasn't been acquired _ever_, we're not |
| 3501 | * holding it either, so report failure. |
| 3502 | */ |
Matthew Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 3503 | if (!class) |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3504 | return 0; |
| 3505 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3506 | /* |
| 3507 | * References, but not a lock we're actually ref-counting? |
| 3508 | * State got messed up, follow the sites that change ->references |
| 3509 | * and try to make sense of it. |
| 3510 | */ |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3511 | if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock)) |
| 3512 | return 0; |
| 3513 | |
| 3514 | if (hlock->class_idx == class - lock_classes + 1) |
| 3515 | return 1; |
| 3516 | } |
| 3517 | |
| 3518 | return 0; |
| 3519 | } |
| 3520 | |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 3521 | /* @depth must not be zero */ |
| 3522 | static struct held_lock *find_held_lock(struct task_struct *curr, |
| 3523 | struct lockdep_map *lock, |
| 3524 | unsigned int depth, int *idx) |
| 3525 | { |
| 3526 | struct held_lock *ret, *hlock, *prev_hlock; |
| 3527 | int i; |
| 3528 | |
| 3529 | i = depth - 1; |
| 3530 | hlock = curr->held_locks + i; |
| 3531 | ret = hlock; |
| 3532 | if (match_held_lock(hlock, lock)) |
| 3533 | goto out; |
| 3534 | |
| 3535 | ret = NULL; |
| 3536 | for (i--, prev_hlock = hlock--; |
| 3537 | i >= 0; |
| 3538 | i--, prev_hlock = hlock--) { |
| 3539 | /* |
| 3540 | * We must not cross into another context: |
| 3541 | */ |
| 3542 | if (prev_hlock->irq_context != hlock->irq_context) { |
| 3543 | ret = NULL; |
| 3544 | break; |
| 3545 | } |
| 3546 | if (match_held_lock(hlock, lock)) { |
| 3547 | ret = hlock; |
| 3548 | break; |
| 3549 | } |
| 3550 | } |
| 3551 | |
| 3552 | out: |
| 3553 | *idx = i; |
| 3554 | return ret; |
| 3555 | } |
| 3556 | |
J. R. Okajima | e969970 | 2017-02-03 01:38:16 +0900 | [diff] [blame] | 3557 | static int reacquire_held_locks(struct task_struct *curr, unsigned int depth, |
| 3558 | int idx) |
| 3559 | { |
| 3560 | struct held_lock *hlock; |
| 3561 | |
Waiman Long | 8ee1086 | 2018-10-02 16:19:17 -0400 | [diff] [blame] | 3562 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled())) |
| 3563 | return 0; |
| 3564 | |
J. R. Okajima | e969970 | 2017-02-03 01:38:16 +0900 | [diff] [blame] | 3565 | for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) { |
| 3566 | if (!__lock_acquire(hlock->instance, |
| 3567 | hlock_class(hlock)->subclass, |
| 3568 | hlock->trylock, |
| 3569 | hlock->read, hlock->check, |
| 3570 | hlock->hardirqs_off, |
| 3571 | hlock->nest_lock, hlock->acquire_ip, |
| 3572 | hlock->references, hlock->pin_count)) |
| 3573 | return 1; |
| 3574 | } |
| 3575 | return 0; |
| 3576 | } |
| 3577 | |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 3578 | static int |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 3579 | __lock_set_class(struct lockdep_map *lock, const char *name, |
| 3580 | struct lock_class_key *key, unsigned int subclass, |
| 3581 | unsigned long ip) |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 3582 | { |
| 3583 | struct task_struct *curr = current; |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 3584 | struct held_lock *hlock; |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 3585 | struct lock_class *class; |
| 3586 | unsigned int depth; |
| 3587 | int i; |
| 3588 | |
Waiman Long | 513e107 | 2019-01-09 23:03:25 -0500 | [diff] [blame] | 3589 | if (unlikely(!debug_locks)) |
| 3590 | return 0; |
| 3591 | |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 3592 | depth = curr->lockdep_depth; |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3593 | /* |
| 3594 | * This function is about (re)setting the class of a held lock, |
| 3595 | * yet we're not actually holding any locks. Naughty user! |
| 3596 | */ |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 3597 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
| 3598 | return 0; |
| 3599 | |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 3600 | hlock = find_held_lock(curr, lock, depth, &i); |
| 3601 | if (!hlock) |
| 3602 | return print_unlock_imbalance_bug(curr, lock, ip); |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 3603 | |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 3604 | lockdep_init_map(lock, name, key, 0); |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 3605 | class = register_lock_class(lock, subclass, 0); |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 3606 | hlock->class_idx = class - lock_classes + 1; |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 3607 | |
| 3608 | curr->lockdep_depth = i; |
| 3609 | curr->curr_chain_key = hlock->prev_chain_key; |
| 3610 | |
J. R. Okajima | e969970 | 2017-02-03 01:38:16 +0900 | [diff] [blame] | 3611 | if (reacquire_held_locks(curr, depth, i)) |
| 3612 | return 0; |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 3613 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3614 | /* |
| 3615 | * I took it apart and put it back together again, except now I have |
| 3616 | * these 'spare' parts.. where shall I put them. |
| 3617 | */ |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 3618 | if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth)) |
| 3619 | return 0; |
| 3620 | return 1; |
| 3621 | } |
| 3622 | |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 3623 | static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip) |
| 3624 | { |
| 3625 | struct task_struct *curr = current; |
| 3626 | struct held_lock *hlock; |
| 3627 | unsigned int depth; |
| 3628 | int i; |
| 3629 | |
Waiman Long | 7149258 | 2019-01-09 23:03:25 -0500 | [diff] [blame] | 3630 | if (unlikely(!debug_locks)) |
| 3631 | return 0; |
| 3632 | |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 3633 | depth = curr->lockdep_depth; |
| 3634 | /* |
| 3635 | * This function is about (re)setting the class of a held lock, |
| 3636 | * yet we're not actually holding any locks. Naughty user! |
| 3637 | */ |
| 3638 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
| 3639 | return 0; |
| 3640 | |
| 3641 | hlock = find_held_lock(curr, lock, depth, &i); |
| 3642 | if (!hlock) |
| 3643 | return print_unlock_imbalance_bug(curr, lock, ip); |
| 3644 | |
| 3645 | curr->lockdep_depth = i; |
| 3646 | curr->curr_chain_key = hlock->prev_chain_key; |
| 3647 | |
| 3648 | WARN(hlock->read, "downgrading a read lock"); |
| 3649 | hlock->read = 1; |
| 3650 | hlock->acquire_ip = ip; |
| 3651 | |
| 3652 | if (reacquire_held_locks(curr, depth, i)) |
| 3653 | return 0; |
| 3654 | |
| 3655 | /* |
| 3656 | * I took it apart and put it back together again, except now I have |
| 3657 | * these 'spare' parts.. where shall I put them. |
| 3658 | */ |
| 3659 | if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth)) |
| 3660 | return 0; |
| 3661 | return 1; |
| 3662 | } |
| 3663 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3664 | /* |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 3665 | * Remove the lock to the list of currently held locks - this gets |
| 3666 | * called on mutex_unlock()/spin_unlock*() (or on a failed |
| 3667 | * mutex_lock_interruptible()). |
| 3668 | * |
| 3669 | * @nested is an hysterical artifact, needs a tree wide cleanup. |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3670 | */ |
| 3671 | static int |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 3672 | __lock_release(struct lockdep_map *lock, int nested, unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3673 | { |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 3674 | struct task_struct *curr = current; |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 3675 | struct held_lock *hlock; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3676 | unsigned int depth; |
Ingo Molnar | e966eae | 2017-12-12 12:31:16 +0100 | [diff] [blame] | 3677 | int i; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3678 | |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 3679 | if (unlikely(!debug_locks)) |
| 3680 | return 0; |
| 3681 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3682 | depth = curr->lockdep_depth; |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3683 | /* |
| 3684 | * So we're all set to release this lock.. wait what lock? We don't |
| 3685 | * own any locks, you've been drinking again? |
| 3686 | */ |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 3687 | if (DEBUG_LOCKS_WARN_ON(depth <= 0)) |
| 3688 | return print_unlock_imbalance_bug(curr, lock, ip); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3689 | |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 3690 | /* |
| 3691 | * Check whether the lock exists in the current stack |
| 3692 | * of held locks: |
| 3693 | */ |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 3694 | hlock = find_held_lock(curr, lock, depth, &i); |
| 3695 | if (!hlock) |
| 3696 | return print_unlock_imbalance_bug(curr, lock, ip); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3697 | |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3698 | if (hlock->instance == lock) |
| 3699 | lock_release_holdtime(hlock); |
| 3700 | |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 3701 | WARN(hlock->pin_count, "releasing a pinned lock\n"); |
| 3702 | |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3703 | if (hlock->references) { |
| 3704 | hlock->references--; |
| 3705 | if (hlock->references) { |
| 3706 | /* |
| 3707 | * We had, and after removing one, still have |
| 3708 | * references, the current lock stack is still |
| 3709 | * valid. We're done! |
| 3710 | */ |
| 3711 | return 1; |
| 3712 | } |
| 3713 | } |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 3714 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3715 | /* |
| 3716 | * We have the right lock to unlock, 'hlock' points to it. |
| 3717 | * Now we remove it from the stack, and add back the other |
| 3718 | * entries (if any), recalculating the hash along the way: |
| 3719 | */ |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3720 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3721 | curr->lockdep_depth = i; |
| 3722 | curr->curr_chain_key = hlock->prev_chain_key; |
| 3723 | |
Waiman Long | ce52a18 | 2018-10-02 16:19:18 -0400 | [diff] [blame] | 3724 | /* |
| 3725 | * The most likely case is when the unlock is on the innermost |
| 3726 | * lock. In this case, we are done! |
| 3727 | */ |
| 3728 | if (i == depth-1) |
| 3729 | return 1; |
| 3730 | |
J. R. Okajima | e969970 | 2017-02-03 01:38:16 +0900 | [diff] [blame] | 3731 | if (reacquire_held_locks(curr, depth, i + 1)) |
| 3732 | return 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3733 | |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3734 | /* |
| 3735 | * We had N bottles of beer on the wall, we drank one, but now |
| 3736 | * there's not N-1 bottles of beer left on the wall... |
| 3737 | */ |
Waiman Long | ce52a18 | 2018-10-02 16:19:18 -0400 | [diff] [blame] | 3738 | DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth-1); |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 3739 | |
Waiman Long | ce52a18 | 2018-10-02 16:19:18 -0400 | [diff] [blame] | 3740 | /* |
| 3741 | * Since reacquire_held_locks() would have called check_chain_key() |
| 3742 | * indirectly via __lock_acquire(), we don't need to do it again |
| 3743 | * on return. |
| 3744 | */ |
| 3745 | return 0; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3746 | } |
| 3747 | |
Matthew Wilcox | 08f36ff | 2018-01-17 07:14:13 -0800 | [diff] [blame] | 3748 | static int __lock_is_held(const struct lockdep_map *lock, int read) |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 3749 | { |
| 3750 | struct task_struct *curr = current; |
| 3751 | int i; |
| 3752 | |
| 3753 | for (i = 0; i < curr->lockdep_depth; i++) { |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 3754 | struct held_lock *hlock = curr->held_locks + i; |
| 3755 | |
Peter Zijlstra | f831948 | 2016-11-30 14:32:25 +1100 | [diff] [blame] | 3756 | if (match_held_lock(hlock, lock)) { |
| 3757 | if (read == -1 || hlock->read == read) |
| 3758 | return 1; |
| 3759 | |
| 3760 | return 0; |
| 3761 | } |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | return 0; |
| 3765 | } |
| 3766 | |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 3767 | static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock) |
| 3768 | { |
| 3769 | struct pin_cookie cookie = NIL_COOKIE; |
| 3770 | struct task_struct *curr = current; |
| 3771 | int i; |
| 3772 | |
| 3773 | if (unlikely(!debug_locks)) |
| 3774 | return cookie; |
| 3775 | |
| 3776 | for (i = 0; i < curr->lockdep_depth; i++) { |
| 3777 | struct held_lock *hlock = curr->held_locks + i; |
| 3778 | |
| 3779 | if (match_held_lock(hlock, lock)) { |
| 3780 | /* |
| 3781 | * Grab 16bits of randomness; this is sufficient to not |
| 3782 | * be guessable and still allows some pin nesting in |
| 3783 | * our u32 pin_count. |
| 3784 | */ |
| 3785 | cookie.val = 1 + (prandom_u32() >> 16); |
| 3786 | hlock->pin_count += cookie.val; |
| 3787 | return cookie; |
| 3788 | } |
| 3789 | } |
| 3790 | |
| 3791 | WARN(1, "pinning an unheld lock\n"); |
| 3792 | return cookie; |
| 3793 | } |
| 3794 | |
| 3795 | 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] | 3796 | { |
| 3797 | struct task_struct *curr = current; |
| 3798 | int i; |
| 3799 | |
| 3800 | if (unlikely(!debug_locks)) |
| 3801 | return; |
| 3802 | |
| 3803 | for (i = 0; i < curr->lockdep_depth; i++) { |
| 3804 | struct held_lock *hlock = curr->held_locks + i; |
| 3805 | |
| 3806 | if (match_held_lock(hlock, lock)) { |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 3807 | hlock->pin_count += cookie.val; |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 3808 | return; |
| 3809 | } |
| 3810 | } |
| 3811 | |
| 3812 | WARN(1, "pinning an unheld lock\n"); |
| 3813 | } |
| 3814 | |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 3815 | 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] | 3816 | { |
| 3817 | struct task_struct *curr = current; |
| 3818 | int i; |
| 3819 | |
| 3820 | if (unlikely(!debug_locks)) |
| 3821 | return; |
| 3822 | |
| 3823 | for (i = 0; i < curr->lockdep_depth; i++) { |
| 3824 | struct held_lock *hlock = curr->held_locks + i; |
| 3825 | |
| 3826 | if (match_held_lock(hlock, lock)) { |
| 3827 | if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n")) |
| 3828 | return; |
| 3829 | |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 3830 | hlock->pin_count -= cookie.val; |
| 3831 | |
| 3832 | if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n")) |
| 3833 | hlock->pin_count = 0; |
| 3834 | |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 3835 | return; |
| 3836 | } |
| 3837 | } |
| 3838 | |
| 3839 | WARN(1, "unpinning an unheld lock\n"); |
| 3840 | } |
| 3841 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3842 | /* |
| 3843 | * Check whether we follow the irq-flags state precisely: |
| 3844 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 3845 | static void check_flags(unsigned long flags) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3846 | { |
Ingo Molnar | 992860e | 2008-07-14 10:28:38 +0200 | [diff] [blame] | 3847 | #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \ |
| 3848 | defined(CONFIG_TRACE_IRQFLAGS) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3849 | if (!debug_locks) |
| 3850 | return; |
| 3851 | |
Ingo Molnar | 5f9fa8a | 2007-12-07 19:02:47 +0100 | [diff] [blame] | 3852 | if (irqs_disabled_flags(flags)) { |
| 3853 | if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) { |
| 3854 | printk("possible reason: unannotated irqs-off.\n"); |
| 3855 | } |
| 3856 | } else { |
| 3857 | if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) { |
| 3858 | printk("possible reason: unannotated irqs-on.\n"); |
| 3859 | } |
| 3860 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3861 | |
| 3862 | /* |
| 3863 | * We dont accurately track softirq state in e.g. |
| 3864 | * hardirq contexts (such as on 4KSTACKS), so only |
| 3865 | * check if not in hardirq contexts: |
| 3866 | */ |
| 3867 | if (!hardirq_count()) { |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3868 | if (softirq_count()) { |
| 3869 | /* like the above, but with softirqs */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3870 | DEBUG_LOCKS_WARN_ON(current->softirqs_enabled); |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3871 | } else { |
| 3872 | /* lick the above, does it taste good? */ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3873 | DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled); |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 3874 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | if (!debug_locks) |
| 3878 | print_irqtrace_events(current); |
| 3879 | #endif |
| 3880 | } |
| 3881 | |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 3882 | void lock_set_class(struct lockdep_map *lock, const char *name, |
| 3883 | struct lock_class_key *key, unsigned int subclass, |
| 3884 | unsigned long ip) |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 3885 | { |
| 3886 | unsigned long flags; |
| 3887 | |
| 3888 | if (unlikely(current->lockdep_recursion)) |
| 3889 | return; |
| 3890 | |
| 3891 | raw_local_irq_save(flags); |
| 3892 | current->lockdep_recursion = 1; |
| 3893 | check_flags(flags); |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 3894 | if (__lock_set_class(lock, name, key, subclass, ip)) |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 3895 | check_chain_key(current); |
| 3896 | current->lockdep_recursion = 0; |
| 3897 | raw_local_irq_restore(flags); |
| 3898 | } |
Peter Zijlstra | 00ef9f7 | 2008-12-04 09:00:17 +0100 | [diff] [blame] | 3899 | EXPORT_SYMBOL_GPL(lock_set_class); |
Peter Zijlstra | 64aa348 | 2008-08-11 09:30:21 +0200 | [diff] [blame] | 3900 | |
J. R. Okajima | 6419c4a | 2017-02-03 01:38:17 +0900 | [diff] [blame] | 3901 | void lock_downgrade(struct lockdep_map *lock, unsigned long ip) |
| 3902 | { |
| 3903 | unsigned long flags; |
| 3904 | |
| 3905 | if (unlikely(current->lockdep_recursion)) |
| 3906 | return; |
| 3907 | |
| 3908 | raw_local_irq_save(flags); |
| 3909 | current->lockdep_recursion = 1; |
| 3910 | check_flags(flags); |
| 3911 | if (__lock_downgrade(lock, ip)) |
| 3912 | check_chain_key(current); |
| 3913 | current->lockdep_recursion = 0; |
| 3914 | raw_local_irq_restore(flags); |
| 3915 | } |
| 3916 | EXPORT_SYMBOL_GPL(lock_downgrade); |
| 3917 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3918 | /* |
| 3919 | * We are not always called with irqs disabled - do that here, |
| 3920 | * and also avoid lockdep recursion: |
| 3921 | */ |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 3922 | void lock_acquire(struct lockdep_map *lock, unsigned int subclass, |
Peter Zijlstra | 7531e2f | 2008-08-11 09:30:24 +0200 | [diff] [blame] | 3923 | int trylock, int read, int check, |
| 3924 | struct lockdep_map *nest_lock, unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3925 | { |
| 3926 | unsigned long flags; |
| 3927 | |
| 3928 | if (unlikely(current->lockdep_recursion)) |
| 3929 | return; |
| 3930 | |
| 3931 | raw_local_irq_save(flags); |
| 3932 | check_flags(flags); |
| 3933 | |
| 3934 | current->lockdep_recursion = 1; |
Frederic Weisbecker | db2c4c7 | 2010-02-02 23:34:40 +0100 | [diff] [blame] | 3935 | trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3936 | __lock_acquire(lock, subclass, trylock, read, check, |
Peter Zijlstra | 21199f2 | 2015-09-16 16:10:40 +0200 | [diff] [blame] | 3937 | irqs_disabled_flags(flags), nest_lock, ip, 0, 0); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3938 | current->lockdep_recursion = 0; |
| 3939 | raw_local_irq_restore(flags); |
| 3940 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3941 | EXPORT_SYMBOL_GPL(lock_acquire); |
| 3942 | |
Steven Rostedt | 1d09daa | 2008-05-12 21:20:55 +0200 | [diff] [blame] | 3943 | void lock_release(struct lockdep_map *lock, int nested, |
Steven Rostedt | 0764d23 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 3944 | unsigned long ip) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3945 | { |
| 3946 | unsigned long flags; |
| 3947 | |
| 3948 | if (unlikely(current->lockdep_recursion)) |
| 3949 | return; |
| 3950 | |
| 3951 | raw_local_irq_save(flags); |
| 3952 | check_flags(flags); |
| 3953 | current->lockdep_recursion = 1; |
Frederic Weisbecker | 9313543 | 2010-05-08 06:24:25 +0200 | [diff] [blame] | 3954 | trace_lock_release(lock, ip); |
Peter Zijlstra | e0f56fd | 2015-06-11 14:46:52 +0200 | [diff] [blame] | 3955 | if (__lock_release(lock, nested, ip)) |
| 3956 | check_chain_key(current); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3957 | current->lockdep_recursion = 0; |
| 3958 | raw_local_irq_restore(flags); |
| 3959 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 3960 | EXPORT_SYMBOL_GPL(lock_release); |
| 3961 | |
Matthew Wilcox | 08f36ff | 2018-01-17 07:14:13 -0800 | [diff] [blame] | 3962 | int lock_is_held_type(const struct lockdep_map *lock, int read) |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 3963 | { |
| 3964 | unsigned long flags; |
| 3965 | int ret = 0; |
| 3966 | |
| 3967 | if (unlikely(current->lockdep_recursion)) |
Peter Zijlstra | f2513cd | 2011-06-06 12:32:43 +0200 | [diff] [blame] | 3968 | return 1; /* avoid false negative lockdep_assert_held() */ |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 3969 | |
| 3970 | raw_local_irq_save(flags); |
| 3971 | check_flags(flags); |
| 3972 | |
| 3973 | current->lockdep_recursion = 1; |
Peter Zijlstra | f831948 | 2016-11-30 14:32:25 +1100 | [diff] [blame] | 3974 | ret = __lock_is_held(lock, read); |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 3975 | current->lockdep_recursion = 0; |
| 3976 | raw_local_irq_restore(flags); |
| 3977 | |
| 3978 | return ret; |
| 3979 | } |
Peter Zijlstra | f831948 | 2016-11-30 14:32:25 +1100 | [diff] [blame] | 3980 | EXPORT_SYMBOL_GPL(lock_is_held_type); |
Peter Zijlstra | f607c66 | 2009-07-20 19:16:29 +0200 | [diff] [blame] | 3981 | |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 3982 | struct pin_cookie lock_pin_lock(struct lockdep_map *lock) |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 3983 | { |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 3984 | struct pin_cookie cookie = NIL_COOKIE; |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 3985 | unsigned long flags; |
| 3986 | |
| 3987 | if (unlikely(current->lockdep_recursion)) |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 3988 | return cookie; |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 3989 | |
| 3990 | raw_local_irq_save(flags); |
| 3991 | check_flags(flags); |
| 3992 | |
| 3993 | current->lockdep_recursion = 1; |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 3994 | cookie = __lock_pin_lock(lock); |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 3995 | current->lockdep_recursion = 0; |
| 3996 | raw_local_irq_restore(flags); |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 3997 | |
| 3998 | return cookie; |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 3999 | } |
| 4000 | EXPORT_SYMBOL_GPL(lock_pin_lock); |
| 4001 | |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 4002 | void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie) |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 4003 | { |
| 4004 | unsigned long flags; |
| 4005 | |
| 4006 | if (unlikely(current->lockdep_recursion)) |
| 4007 | return; |
| 4008 | |
| 4009 | raw_local_irq_save(flags); |
| 4010 | check_flags(flags); |
| 4011 | |
| 4012 | current->lockdep_recursion = 1; |
Peter Zijlstra | e7904a2 | 2015-08-01 19:25:08 +0200 | [diff] [blame] | 4013 | __lock_repin_lock(lock, cookie); |
| 4014 | current->lockdep_recursion = 0; |
| 4015 | raw_local_irq_restore(flags); |
| 4016 | } |
| 4017 | EXPORT_SYMBOL_GPL(lock_repin_lock); |
| 4018 | |
| 4019 | void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie) |
| 4020 | { |
| 4021 | unsigned long flags; |
| 4022 | |
| 4023 | if (unlikely(current->lockdep_recursion)) |
| 4024 | return; |
| 4025 | |
| 4026 | raw_local_irq_save(flags); |
| 4027 | check_flags(flags); |
| 4028 | |
| 4029 | current->lockdep_recursion = 1; |
| 4030 | __lock_unpin_lock(lock, cookie); |
Peter Zijlstra | a24fc60 | 2015-06-11 14:46:53 +0200 | [diff] [blame] | 4031 | current->lockdep_recursion = 0; |
| 4032 | raw_local_irq_restore(flags); |
| 4033 | } |
| 4034 | EXPORT_SYMBOL_GPL(lock_unpin_lock); |
| 4035 | |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4036 | #ifdef CONFIG_LOCK_STAT |
| 4037 | static int |
| 4038 | print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock, |
| 4039 | unsigned long ip) |
| 4040 | { |
| 4041 | if (!debug_locks_off()) |
| 4042 | return 0; |
| 4043 | if (debug_locks_silent) |
| 4044 | return 0; |
| 4045 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4046 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 4047 | pr_warn("=================================\n"); |
| 4048 | pr_warn("WARNING: bad contention detected!\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 4049 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 4050 | pr_warn("---------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4051 | pr_warn("%s/%d is trying to contend lock (", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 4052 | curr->comm, task_pid_nr(curr)); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4053 | print_lockdep_cache(lock); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4054 | pr_cont(") at:\n"); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4055 | print_ip_sym(ip); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4056 | pr_warn("but there are no locks held!\n"); |
| 4057 | pr_warn("\nother info that might help us debug this:\n"); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4058 | lockdep_print_held_locks(curr); |
| 4059 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4060 | pr_warn("\nstack backtrace:\n"); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4061 | dump_stack(); |
| 4062 | |
| 4063 | return 0; |
| 4064 | } |
| 4065 | |
| 4066 | static void |
| 4067 | __lock_contended(struct lockdep_map *lock, unsigned long ip) |
| 4068 | { |
| 4069 | struct task_struct *curr = current; |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 4070 | struct held_lock *hlock; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4071 | struct lock_class_stats *stats; |
| 4072 | unsigned int depth; |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 4073 | int i, contention_point, contending_point; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4074 | |
| 4075 | depth = curr->lockdep_depth; |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4076 | /* |
| 4077 | * Whee, we contended on this lock, except it seems we're not |
| 4078 | * actually trying to acquire anything much at all.. |
| 4079 | */ |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4080 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
| 4081 | return; |
| 4082 | |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 4083 | hlock = find_held_lock(curr, lock, depth, &i); |
| 4084 | if (!hlock) { |
| 4085 | print_lock_contention_bug(curr, lock, ip); |
| 4086 | return; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4087 | } |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4088 | |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 4089 | if (hlock->instance != lock) |
| 4090 | return; |
| 4091 | |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 4092 | hlock->waittime_stamp = lockstat_clock(); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4093 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 4094 | contention_point = lock_point(hlock_class(hlock)->contention_point, ip); |
| 4095 | contending_point = lock_point(hlock_class(hlock)->contending_point, |
| 4096 | lock->ip); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4097 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 4098 | stats = get_lock_stats(hlock_class(hlock)); |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 4099 | if (contention_point < LOCKSTAT_POINTS) |
| 4100 | stats->contention_point[contention_point]++; |
| 4101 | if (contending_point < LOCKSTAT_POINTS) |
| 4102 | stats->contending_point[contending_point]++; |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 4103 | if (lock->cpu != smp_processor_id()) |
| 4104 | stats->bounces[bounce_contended + !!hlock->read]++; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | static void |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 4108 | __lock_acquired(struct lockdep_map *lock, unsigned long ip) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4109 | { |
| 4110 | struct task_struct *curr = current; |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 4111 | struct held_lock *hlock; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4112 | struct lock_class_stats *stats; |
| 4113 | unsigned int depth; |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 4114 | u64 now, waittime = 0; |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 4115 | int i, cpu; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4116 | |
| 4117 | depth = curr->lockdep_depth; |
Peter Zijlstra | 0119fee | 2011-09-02 01:30:29 +0200 | [diff] [blame] | 4118 | /* |
| 4119 | * Yay, we acquired ownership of this lock we didn't try to |
| 4120 | * acquire, how the heck did that happen? |
| 4121 | */ |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4122 | if (DEBUG_LOCKS_WARN_ON(!depth)) |
| 4123 | return; |
| 4124 | |
J. R. Okajima | 41c2c5b | 2017-02-03 01:38:15 +0900 | [diff] [blame] | 4125 | hlock = find_held_lock(curr, lock, depth, &i); |
| 4126 | if (!hlock) { |
| 4127 | print_lock_contention_bug(curr, lock, _RET_IP_); |
| 4128 | return; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4129 | } |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4130 | |
Peter Zijlstra | bb97a91 | 2009-07-20 19:15:35 +0200 | [diff] [blame] | 4131 | if (hlock->instance != lock) |
| 4132 | return; |
| 4133 | |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 4134 | cpu = smp_processor_id(); |
| 4135 | if (hlock->waittime_stamp) { |
Peter Zijlstra | 3365e779 | 2009-10-09 10:12:41 +0200 | [diff] [blame] | 4136 | now = lockstat_clock(); |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 4137 | waittime = now - hlock->waittime_stamp; |
| 4138 | hlock->holdtime_stamp = now; |
| 4139 | } |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4140 | |
Frederic Weisbecker | 883a2a3 | 2010-05-08 06:16:11 +0200 | [diff] [blame] | 4141 | trace_lock_acquired(lock, ip); |
Frederic Weisbecker | 2062501 | 2009-04-06 01:49:33 +0200 | [diff] [blame] | 4142 | |
Dave Jones | f82b217 | 2008-08-11 09:30:23 +0200 | [diff] [blame] | 4143 | stats = get_lock_stats(hlock_class(hlock)); |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 4144 | if (waittime) { |
| 4145 | if (hlock->read) |
| 4146 | lock_time_inc(&stats->read_waittime, waittime); |
| 4147 | else |
| 4148 | lock_time_inc(&stats->write_waittime, waittime); |
| 4149 | } |
| 4150 | if (lock->cpu != cpu) |
| 4151 | stats->bounces[bounce_acquired + !!hlock->read]++; |
Peter Zijlstra | 9664567 | 2007-07-19 01:49:00 -0700 | [diff] [blame] | 4152 | |
| 4153 | lock->cpu = cpu; |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 4154 | lock->ip = ip; |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4155 | } |
| 4156 | |
| 4157 | void lock_contended(struct lockdep_map *lock, unsigned long ip) |
| 4158 | { |
| 4159 | unsigned long flags; |
| 4160 | |
Waiman Long | 9506a74 | 2018-10-18 21:45:17 -0400 | [diff] [blame] | 4161 | if (unlikely(!lock_stat || !debug_locks)) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4162 | return; |
| 4163 | |
| 4164 | if (unlikely(current->lockdep_recursion)) |
| 4165 | return; |
| 4166 | |
| 4167 | raw_local_irq_save(flags); |
| 4168 | check_flags(flags); |
| 4169 | current->lockdep_recursion = 1; |
Frederic Weisbecker | db2c4c7 | 2010-02-02 23:34:40 +0100 | [diff] [blame] | 4170 | trace_lock_contended(lock, ip); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4171 | __lock_contended(lock, ip); |
| 4172 | current->lockdep_recursion = 0; |
| 4173 | raw_local_irq_restore(flags); |
| 4174 | } |
| 4175 | EXPORT_SYMBOL_GPL(lock_contended); |
| 4176 | |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 4177 | void lock_acquired(struct lockdep_map *lock, unsigned long ip) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4178 | { |
| 4179 | unsigned long flags; |
| 4180 | |
Waiman Long | 9506a74 | 2018-10-18 21:45:17 -0400 | [diff] [blame] | 4181 | if (unlikely(!lock_stat || !debug_locks)) |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4182 | return; |
| 4183 | |
| 4184 | if (unlikely(current->lockdep_recursion)) |
| 4185 | return; |
| 4186 | |
| 4187 | raw_local_irq_save(flags); |
| 4188 | check_flags(flags); |
| 4189 | current->lockdep_recursion = 1; |
Peter Zijlstra | c7e78cf | 2008-10-16 23:17:09 +0200 | [diff] [blame] | 4190 | __lock_acquired(lock, ip); |
Peter Zijlstra | f20786f | 2007-07-19 01:48:56 -0700 | [diff] [blame] | 4191 | current->lockdep_recursion = 0; |
| 4192 | raw_local_irq_restore(flags); |
| 4193 | } |
| 4194 | EXPORT_SYMBOL_GPL(lock_acquired); |
| 4195 | #endif |
| 4196 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4197 | /* |
| 4198 | * Used by the testsuite, sanitize the validator state |
| 4199 | * after a simulated failure: |
| 4200 | */ |
| 4201 | |
| 4202 | void lockdep_reset(void) |
| 4203 | { |
| 4204 | unsigned long flags; |
Ingo Molnar | 23d95a0 | 2006-12-13 00:34:40 -0800 | [diff] [blame] | 4205 | int i; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4206 | |
| 4207 | raw_local_irq_save(flags); |
| 4208 | current->curr_chain_key = 0; |
| 4209 | current->lockdep_depth = 0; |
| 4210 | current->lockdep_recursion = 0; |
| 4211 | memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock)); |
| 4212 | nr_hardirq_chains = 0; |
| 4213 | nr_softirq_chains = 0; |
| 4214 | nr_process_chains = 0; |
| 4215 | debug_locks = 1; |
Ingo Molnar | 23d95a0 | 2006-12-13 00:34:40 -0800 | [diff] [blame] | 4216 | for (i = 0; i < CHAINHASH_SIZE; i++) |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 4217 | INIT_HLIST_HEAD(chainhash_table + i); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4218 | raw_local_irq_restore(flags); |
| 4219 | } |
| 4220 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4221 | /* 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^] | 4222 | static void remove_class_from_lock_chain(struct pending_free *pf, |
| 4223 | struct lock_chain *chain, |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4224 | struct lock_class *class) |
| 4225 | { |
| 4226 | #ifdef CONFIG_PROVE_LOCKING |
| 4227 | struct lock_chain *new_chain; |
| 4228 | u64 chain_key; |
| 4229 | int i; |
| 4230 | |
| 4231 | for (i = chain->base; i < chain->base + chain->depth; i++) { |
| 4232 | if (chain_hlocks[i] != class - lock_classes) |
| 4233 | continue; |
| 4234 | /* The code below leaks one chain_hlock[] entry. */ |
| 4235 | if (--chain->depth > 0) |
| 4236 | memmove(&chain_hlocks[i], &chain_hlocks[i + 1], |
| 4237 | (chain->base + chain->depth - i) * |
| 4238 | sizeof(chain_hlocks[0])); |
| 4239 | /* |
| 4240 | * Each lock class occurs at most once in a lock chain so once |
| 4241 | * we found a match we can break out of this loop. |
| 4242 | */ |
| 4243 | goto recalc; |
| 4244 | } |
| 4245 | /* Since the chain has not been modified, return. */ |
| 4246 | return; |
| 4247 | |
| 4248 | recalc: |
| 4249 | chain_key = 0; |
| 4250 | for (i = chain->base; i < chain->base + chain->depth; i++) |
| 4251 | chain_key = iterate_chain_key(chain_key, chain_hlocks[i] + 1); |
| 4252 | if (chain->depth && chain->chain_key == chain_key) |
| 4253 | return; |
| 4254 | /* Overwrite the chain key for concurrent RCU readers. */ |
| 4255 | WRITE_ONCE(chain->chain_key, chain_key); |
| 4256 | /* |
| 4257 | * Note: calling hlist_del_rcu() from inside a |
| 4258 | * hlist_for_each_entry_rcu() loop is safe. |
| 4259 | */ |
| 4260 | hlist_del_rcu(&chain->entry); |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame^] | 4261 | __set_bit(chain - lock_chains, pf->lock_chains_being_freed); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4262 | if (chain->depth == 0) |
| 4263 | return; |
| 4264 | /* |
| 4265 | * If the modified lock chain matches an existing lock chain, drop |
| 4266 | * the modified lock chain. |
| 4267 | */ |
| 4268 | if (lookup_chain_cache(chain_key)) |
| 4269 | return; |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame^] | 4270 | new_chain = alloc_lock_chain(); |
| 4271 | if (WARN_ON_ONCE(!new_chain)) { |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4272 | debug_locks_off(); |
| 4273 | return; |
| 4274 | } |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4275 | *new_chain = *chain; |
| 4276 | hlist_add_head_rcu(&new_chain->entry, chainhashentry(chain_key)); |
| 4277 | #endif |
| 4278 | } |
| 4279 | |
| 4280 | /* Must be called with the graph lock held. */ |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame^] | 4281 | static void remove_class_from_lock_chains(struct pending_free *pf, |
| 4282 | struct lock_class *class) |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4283 | { |
| 4284 | struct lock_chain *chain; |
| 4285 | struct hlist_head *head; |
| 4286 | int i; |
| 4287 | |
| 4288 | for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) { |
| 4289 | head = chainhash_table + i; |
| 4290 | hlist_for_each_entry_rcu(chain, head, entry) { |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame^] | 4291 | remove_class_from_lock_chain(pf, chain, class); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4292 | } |
| 4293 | } |
| 4294 | } |
| 4295 | |
Bart Van Assche | 786fa29 | 2018-12-06 17:11:36 -0800 | [diff] [blame] | 4296 | /* |
| 4297 | * Remove all references to a lock class. The caller must hold the graph lock. |
| 4298 | */ |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4299 | static void zap_class(struct pending_free *pf, struct lock_class *class) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4300 | { |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 4301 | struct lock_list *entry; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4302 | int i; |
| 4303 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4304 | WARN_ON_ONCE(!class->key); |
| 4305 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4306 | /* |
| 4307 | * Remove all dependencies this lock is |
| 4308 | * involved in: |
| 4309 | */ |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 4310 | for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) { |
| 4311 | entry = list_entries + i; |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 4312 | if (entry->class != class && entry->links_to != class) |
| 4313 | continue; |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 4314 | __clear_bit(i, list_entries_in_use); |
| 4315 | nr_list_entries--; |
Bart Van Assche | 86cffb8 | 2019-02-14 15:00:41 -0800 | [diff] [blame] | 4316 | list_del_rcu(&entry->entry); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4317 | } |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4318 | if (list_empty(&class->locks_after) && |
| 4319 | list_empty(&class->locks_before)) { |
| 4320 | list_move_tail(&class->lock_entry, &pf->zapped); |
| 4321 | hlist_del_rcu(&class->hash_entry); |
| 4322 | WRITE_ONCE(class->key, NULL); |
| 4323 | WRITE_ONCE(class->name, NULL); |
| 4324 | nr_lock_classes--; |
| 4325 | } else { |
| 4326 | WARN_ONCE(true, "%s() failed for class %s\n", __func__, |
| 4327 | class->name); |
| 4328 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4329 | |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame^] | 4330 | remove_class_from_lock_chains(pf, class); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4331 | } |
| 4332 | |
| 4333 | static void reinit_class(struct lock_class *class) |
| 4334 | { |
| 4335 | void *const p = class; |
| 4336 | const unsigned int offset = offsetof(struct lock_class, key); |
| 4337 | |
| 4338 | WARN_ON_ONCE(!class->lock_entry.next); |
| 4339 | WARN_ON_ONCE(!list_empty(&class->locks_after)); |
| 4340 | WARN_ON_ONCE(!list_empty(&class->locks_before)); |
| 4341 | memset(p + offset, 0, sizeof(*class) - offset); |
| 4342 | WARN_ON_ONCE(!class->lock_entry.next); |
| 4343 | WARN_ON_ONCE(!list_empty(&class->locks_after)); |
| 4344 | WARN_ON_ONCE(!list_empty(&class->locks_before)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4345 | } |
| 4346 | |
Arjan van de Ven | fabe874 | 2008-01-24 07:00:45 +0100 | [diff] [blame] | 4347 | static inline int within(const void *addr, void *start, unsigned long size) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4348 | { |
| 4349 | return addr >= start && addr < start + size; |
| 4350 | } |
| 4351 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4352 | static bool inside_selftest(void) |
| 4353 | { |
| 4354 | return current == lockdep_selftest_task_struct; |
| 4355 | } |
| 4356 | |
| 4357 | /* The caller must hold the graph lock. */ |
| 4358 | static struct pending_free *get_pending_free(void) |
| 4359 | { |
| 4360 | return delayed_free.pf + delayed_free.index; |
| 4361 | } |
| 4362 | |
| 4363 | static void free_zapped_rcu(struct rcu_head *cb); |
| 4364 | |
| 4365 | /* |
| 4366 | * Schedule an RCU callback if no RCU callback is pending. Must be called with |
| 4367 | * the graph lock held. |
| 4368 | */ |
| 4369 | static void call_rcu_zapped(struct pending_free *pf) |
| 4370 | { |
| 4371 | WARN_ON_ONCE(inside_selftest()); |
| 4372 | |
| 4373 | if (list_empty(&pf->zapped)) |
| 4374 | return; |
| 4375 | |
| 4376 | if (delayed_free.scheduled) |
| 4377 | return; |
| 4378 | |
| 4379 | delayed_free.scheduled = true; |
| 4380 | |
| 4381 | WARN_ON_ONCE(delayed_free.pf + delayed_free.index != pf); |
| 4382 | delayed_free.index ^= 1; |
| 4383 | |
| 4384 | call_rcu(&delayed_free.rcu_head, free_zapped_rcu); |
| 4385 | } |
| 4386 | |
| 4387 | /* The caller must hold the graph lock. May be called from RCU context. */ |
| 4388 | static void __free_zapped_classes(struct pending_free *pf) |
| 4389 | { |
| 4390 | struct lock_class *class; |
| 4391 | |
| 4392 | list_for_each_entry(class, &pf->zapped, lock_entry) |
| 4393 | reinit_class(class); |
| 4394 | |
| 4395 | list_splice_init(&pf->zapped, &free_lock_classes); |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame^] | 4396 | |
| 4397 | #ifdef CONFIG_PROVE_LOCKING |
| 4398 | bitmap_andnot(lock_chains_in_use, lock_chains_in_use, |
| 4399 | pf->lock_chains_being_freed, ARRAY_SIZE(lock_chains)); |
| 4400 | bitmap_clear(pf->lock_chains_being_freed, 0, ARRAY_SIZE(lock_chains)); |
| 4401 | #endif |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4402 | } |
| 4403 | |
| 4404 | static void free_zapped_rcu(struct rcu_head *ch) |
| 4405 | { |
| 4406 | struct pending_free *pf; |
| 4407 | unsigned long flags; |
| 4408 | |
| 4409 | if (WARN_ON_ONCE(ch != &delayed_free.rcu_head)) |
| 4410 | return; |
| 4411 | |
| 4412 | raw_local_irq_save(flags); |
| 4413 | if (!graph_lock()) |
| 4414 | goto out_irq; |
| 4415 | |
| 4416 | /* closed head */ |
| 4417 | pf = delayed_free.pf + (delayed_free.index ^ 1); |
| 4418 | __free_zapped_classes(pf); |
| 4419 | delayed_free.scheduled = false; |
| 4420 | |
| 4421 | /* |
| 4422 | * If there's anything on the open list, close and start a new callback. |
| 4423 | */ |
| 4424 | call_rcu_zapped(delayed_free.pf + delayed_free.index); |
| 4425 | |
| 4426 | graph_unlock(); |
| 4427 | out_irq: |
| 4428 | raw_local_irq_restore(flags); |
| 4429 | } |
| 4430 | |
| 4431 | /* |
| 4432 | * Remove all lock classes from the class hash table and from the |
| 4433 | * all_lock_classes list whose key or name is in the address range [start, |
| 4434 | * start + size). Move these lock classes to the zapped_classes list. Must |
| 4435 | * be called with the graph lock held. |
| 4436 | */ |
| 4437 | static void __lockdep_free_key_range(struct pending_free *pf, void *start, |
| 4438 | unsigned long size) |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 4439 | { |
| 4440 | struct lock_class *class; |
| 4441 | struct hlist_head *head; |
| 4442 | int i; |
| 4443 | |
| 4444 | /* Unhash all classes that were created by a module. */ |
| 4445 | for (i = 0; i < CLASSHASH_SIZE; i++) { |
| 4446 | head = classhash_table + i; |
| 4447 | hlist_for_each_entry_rcu(class, head, hash_entry) { |
| 4448 | if (!within(class->key, start, size) && |
| 4449 | !within(class->name, start, size)) |
| 4450 | continue; |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4451 | zap_class(pf, class); |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 4452 | } |
| 4453 | } |
| 4454 | } |
| 4455 | |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 4456 | /* |
| 4457 | * Used in module.c to remove lock classes from memory that is going to be |
| 4458 | * freed; and possibly re-used by other modules. |
| 4459 | * |
Bart Van Assche | 29fc33f | 2019-02-14 15:00:45 -0800 | [diff] [blame] | 4460 | * We will have had one synchronize_rcu() before getting here, so we're |
| 4461 | * guaranteed nobody will look up these exact classes -- they're properly dead |
| 4462 | * but still allocated. |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 4463 | */ |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4464 | static void lockdep_free_key_range_reg(void *start, unsigned long size) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4465 | { |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4466 | struct pending_free *pf; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4467 | unsigned long flags; |
Nick Piggin | 5a26db5 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 4468 | int locked; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4469 | |
Bart Van Assche | feb0a38 | 2019-02-14 15:00:42 -0800 | [diff] [blame] | 4470 | init_data_structures_once(); |
| 4471 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4472 | raw_local_irq_save(flags); |
Nick Piggin | 5a26db5 | 2008-01-16 09:51:58 +0100 | [diff] [blame] | 4473 | locked = graph_lock(); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4474 | if (!locked) |
| 4475 | goto out_irq; |
| 4476 | |
| 4477 | pf = get_pending_free(); |
| 4478 | __lockdep_free_key_range(pf, start, size); |
| 4479 | call_rcu_zapped(pf); |
| 4480 | |
| 4481 | graph_unlock(); |
| 4482 | out_irq: |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4483 | raw_local_irq_restore(flags); |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 4484 | |
| 4485 | /* |
| 4486 | * Wait for any possible iterators from look_up_lock_class() to pass |
| 4487 | * before continuing to free the memory they refer to. |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 4488 | */ |
Paul E. McKenney | 51959d8 | 2018-11-06 19:06:51 -0800 | [diff] [blame] | 4489 | synchronize_rcu(); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4490 | } |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 4491 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4492 | /* |
| 4493 | * Free all lockdep keys in the range [start, start+size). Does not sleep. |
| 4494 | * Ignores debug_locks. Must only be used by the lockdep selftests. |
| 4495 | */ |
| 4496 | static void lockdep_free_key_range_imm(void *start, unsigned long size) |
| 4497 | { |
| 4498 | struct pending_free *pf = delayed_free.pf; |
| 4499 | unsigned long flags; |
| 4500 | |
| 4501 | init_data_structures_once(); |
| 4502 | |
| 4503 | raw_local_irq_save(flags); |
| 4504 | arch_spin_lock(&lockdep_lock); |
| 4505 | __lockdep_free_key_range(pf, start, size); |
| 4506 | __free_zapped_classes(pf); |
| 4507 | arch_spin_unlock(&lockdep_lock); |
| 4508 | raw_local_irq_restore(flags); |
| 4509 | } |
| 4510 | |
| 4511 | void lockdep_free_key_range(void *start, unsigned long size) |
| 4512 | { |
| 4513 | init_data_structures_once(); |
| 4514 | |
| 4515 | if (inside_selftest()) |
| 4516 | lockdep_free_key_range_imm(start, size); |
| 4517 | else |
| 4518 | lockdep_free_key_range_reg(start, size); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4519 | } |
| 4520 | |
Bart Van Assche | 2904d9f | 2018-12-06 17:11:34 -0800 | [diff] [blame] | 4521 | /* |
| 4522 | * Check whether any element of the @lock->class_cache[] array refers to a |
| 4523 | * registered lock class. The caller must hold either the graph lock or the |
| 4524 | * RCU read lock. |
| 4525 | */ |
| 4526 | static bool lock_class_cache_is_registered(struct lockdep_map *lock) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4527 | { |
Peter Zijlstra | 35a9393 | 2015-02-26 16:23:11 +0100 | [diff] [blame] | 4528 | struct lock_class *class; |
Andrew Morton | a63f38c | 2016-02-03 13:44:12 -0800 | [diff] [blame] | 4529 | struct hlist_head *head; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4530 | int i, j; |
Bart Van Assche | 2904d9f | 2018-12-06 17:11:34 -0800 | [diff] [blame] | 4531 | |
| 4532 | for (i = 0; i < CLASSHASH_SIZE; i++) { |
| 4533 | head = classhash_table + i; |
| 4534 | hlist_for_each_entry_rcu(class, head, hash_entry) { |
| 4535 | for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++) |
| 4536 | if (lock->class_cache[j] == class) |
| 4537 | return true; |
| 4538 | } |
| 4539 | } |
| 4540 | return false; |
| 4541 | } |
| 4542 | |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 4543 | /* The caller must hold the graph lock. Does not sleep. */ |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4544 | static void __lockdep_reset_lock(struct pending_free *pf, |
| 4545 | struct lockdep_map *lock) |
Bart Van Assche | 2904d9f | 2018-12-06 17:11:34 -0800 | [diff] [blame] | 4546 | { |
| 4547 | struct lock_class *class; |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 4548 | int j; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4549 | |
| 4550 | /* |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 4551 | * Remove all classes this lock might have: |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4552 | */ |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 4553 | for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) { |
| 4554 | /* |
| 4555 | * If the class exists we look it up and zap it: |
| 4556 | */ |
| 4557 | class = look_up_lock_class(lock, j); |
Matthew Wilcox | 64f29d1 | 2018-01-17 07:14:12 -0800 | [diff] [blame] | 4558 | if (class) |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4559 | zap_class(pf, class); |
Ingo Molnar | d6d897c | 2006-07-10 04:44:04 -0700 | [diff] [blame] | 4560 | } |
| 4561 | /* |
| 4562 | * Debug check: in the end all mapped classes should |
| 4563 | * be gone. |
| 4564 | */ |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 4565 | if (WARN_ON_ONCE(lock_class_cache_is_registered(lock))) |
| 4566 | debug_locks_off(); |
| 4567 | } |
| 4568 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4569 | /* |
| 4570 | * Remove all information lockdep has about a lock if debug_locks == 1. Free |
| 4571 | * released data structures from RCU context. |
| 4572 | */ |
| 4573 | static void lockdep_reset_lock_reg(struct lockdep_map *lock) |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 4574 | { |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4575 | struct pending_free *pf; |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 4576 | unsigned long flags; |
| 4577 | int locked; |
| 4578 | |
Bart Van Assche | 956f356 | 2019-02-14 15:00:43 -0800 | [diff] [blame] | 4579 | raw_local_irq_save(flags); |
| 4580 | locked = graph_lock(); |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4581 | if (!locked) |
| 4582 | goto out_irq; |
| 4583 | |
| 4584 | pf = get_pending_free(); |
| 4585 | __lockdep_reset_lock(pf, lock); |
| 4586 | call_rcu_zapped(pf); |
| 4587 | |
| 4588 | graph_unlock(); |
| 4589 | out_irq: |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4590 | raw_local_irq_restore(flags); |
| 4591 | } |
| 4592 | |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4593 | /* |
| 4594 | * Reset a lock. Does not sleep. Ignores debug_locks. Must only be used by the |
| 4595 | * lockdep selftests. |
| 4596 | */ |
| 4597 | static void lockdep_reset_lock_imm(struct lockdep_map *lock) |
| 4598 | { |
| 4599 | struct pending_free *pf = delayed_free.pf; |
| 4600 | unsigned long flags; |
| 4601 | |
| 4602 | raw_local_irq_save(flags); |
| 4603 | arch_spin_lock(&lockdep_lock); |
| 4604 | __lockdep_reset_lock(pf, lock); |
| 4605 | __free_zapped_classes(pf); |
| 4606 | arch_spin_unlock(&lockdep_lock); |
| 4607 | raw_local_irq_restore(flags); |
| 4608 | } |
| 4609 | |
| 4610 | void lockdep_reset_lock(struct lockdep_map *lock) |
| 4611 | { |
| 4612 | init_data_structures_once(); |
| 4613 | |
| 4614 | if (inside_selftest()) |
| 4615 | lockdep_reset_lock_imm(lock); |
| 4616 | else |
| 4617 | lockdep_reset_lock_reg(lock); |
| 4618 | } |
| 4619 | |
Joel Fernandes (Google) | c3bc8fd | 2018-07-30 15:24:23 -0700 | [diff] [blame] | 4620 | void __init lockdep_init(void) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4621 | { |
| 4622 | printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n"); |
| 4623 | |
Li Zefan | b0788ca | 2008-11-21 15:57:32 +0800 | [diff] [blame] | 4624 | printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4625 | printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH); |
| 4626 | printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS); |
Li Zefan | b0788ca | 2008-11-21 15:57:32 +0800 | [diff] [blame] | 4627 | printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4628 | printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES); |
| 4629 | printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS); |
| 4630 | printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE); |
| 4631 | |
Bart Van Assche | 09d75ec | 2019-02-14 15:00:36 -0800 | [diff] [blame] | 4632 | printk(" memory used by lock dependency info: %zu kB\n", |
Bart Van Assche | 7ff8517 | 2019-02-14 15:00:37 -0800 | [diff] [blame] | 4633 | (sizeof(lock_classes) + |
| 4634 | sizeof(classhash_table) + |
| 4635 | sizeof(list_entries) + |
Bart Van Assche | ace35a7 | 2019-02-14 15:00:47 -0800 | [diff] [blame] | 4636 | sizeof(list_entries_in_use) + |
Bart Van Assche | a0b0fd5 | 2019-02-14 15:00:46 -0800 | [diff] [blame] | 4637 | sizeof(chainhash_table) + |
| 4638 | sizeof(delayed_free) |
Ming Lei | 4dd861d | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 4639 | #ifdef CONFIG_PROVE_LOCKING |
Bart Van Assche | 7ff8517 | 2019-02-14 15:00:37 -0800 | [diff] [blame] | 4640 | + sizeof(lock_cq) |
Bart Van Assche | 15ea86b | 2019-02-14 15:00:38 -0800 | [diff] [blame] | 4641 | + sizeof(lock_chains) |
Bart Van Assche | de4643a | 2019-02-14 15:00:50 -0800 | [diff] [blame^] | 4642 | + sizeof(lock_chains_in_use) |
Bart Van Assche | 15ea86b | 2019-02-14 15:00:38 -0800 | [diff] [blame] | 4643 | + sizeof(chain_hlocks) |
Ming Lei | 4dd861d | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 4644 | #endif |
Ming Lei | 90629209 | 2009-08-02 21:43:36 +0800 | [diff] [blame] | 4645 | ) / 1024 |
Ming Lei | 4dd861d | 2009-07-16 15:44:29 +0200 | [diff] [blame] | 4646 | ); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4647 | |
Bart Van Assche | 09d75ec | 2019-02-14 15:00:36 -0800 | [diff] [blame] | 4648 | printk(" per task-struct memory footprint: %zu bytes\n", |
Bart Van Assche | 7ff8517 | 2019-02-14 15:00:37 -0800 | [diff] [blame] | 4649 | sizeof(((struct task_struct *)NULL)->held_locks)); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4650 | } |
| 4651 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4652 | static void |
| 4653 | 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] | 4654 | const void *mem_to, struct held_lock *hlock) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4655 | { |
| 4656 | if (!debug_locks_off()) |
| 4657 | return; |
| 4658 | if (debug_locks_silent) |
| 4659 | return; |
| 4660 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4661 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 4662 | pr_warn("=========================\n"); |
| 4663 | pr_warn("WARNING: held lock freed!\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 4664 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 4665 | pr_warn("-------------------------\n"); |
Borislav Petkov | 04860d4 | 2018-02-26 14:49:26 +0100 | [diff] [blame] | 4666 | 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] | 4667 | 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] | 4668 | print_lock(hlock); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4669 | lockdep_print_held_locks(curr); |
| 4670 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4671 | pr_warn("\nstack backtrace:\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4672 | dump_stack(); |
| 4673 | } |
| 4674 | |
Oleg Nesterov | 5456178 | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4675 | static inline int not_in_range(const void* mem_from, unsigned long mem_len, |
| 4676 | const void* lock_from, unsigned long lock_len) |
| 4677 | { |
| 4678 | return lock_from + lock_len <= mem_from || |
| 4679 | mem_from + mem_len <= lock_from; |
| 4680 | } |
| 4681 | |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4682 | /* |
| 4683 | * Called when kernel memory is freed (or unmapped), or if a lock |
| 4684 | * is destroyed or reinitialized - this code checks whether there is |
| 4685 | * any held lock in the memory range of <from> to <to>: |
| 4686 | */ |
| 4687 | void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len) |
| 4688 | { |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4689 | struct task_struct *curr = current; |
| 4690 | struct held_lock *hlock; |
| 4691 | unsigned long flags; |
| 4692 | int i; |
| 4693 | |
| 4694 | if (unlikely(!debug_locks)) |
| 4695 | return; |
| 4696 | |
Steven Rostedt (VMware) | fcc784b | 2018-04-04 14:06:30 -0400 | [diff] [blame] | 4697 | raw_local_irq_save(flags); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4698 | for (i = 0; i < curr->lockdep_depth; i++) { |
| 4699 | hlock = curr->held_locks + i; |
| 4700 | |
Oleg Nesterov | 5456178 | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4701 | if (not_in_range(mem_from, mem_len, hlock->instance, |
| 4702 | sizeof(*hlock->instance))) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4703 | continue; |
| 4704 | |
Oleg Nesterov | 5456178 | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 4705 | print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4706 | break; |
| 4707 | } |
Steven Rostedt (VMware) | fcc784b | 2018-04-04 14:06:30 -0400 | [diff] [blame] | 4708 | raw_local_irq_restore(flags); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4709 | } |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 4710 | EXPORT_SYMBOL_GPL(debug_check_no_locks_freed); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4711 | |
Colin Cross | 1b1d2fb | 2013-05-06 23:50:08 +0000 | [diff] [blame] | 4712 | static void print_held_locks_bug(void) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4713 | { |
| 4714 | if (!debug_locks_off()) |
| 4715 | return; |
| 4716 | if (debug_locks_silent) |
| 4717 | return; |
| 4718 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4719 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 4720 | pr_warn("====================================\n"); |
| 4721 | pr_warn("WARNING: %s/%d still has locks held!\n", |
Colin Cross | 1b1d2fb | 2013-05-06 23:50:08 +0000 | [diff] [blame] | 4722 | current->comm, task_pid_nr(current)); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 4723 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 4724 | pr_warn("------------------------------------\n"); |
Colin Cross | 1b1d2fb | 2013-05-06 23:50:08 +0000 | [diff] [blame] | 4725 | lockdep_print_held_locks(current); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4726 | pr_warn("\nstack backtrace:\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4727 | dump_stack(); |
| 4728 | } |
| 4729 | |
Colin Cross | 1b1d2fb | 2013-05-06 23:50:08 +0000 | [diff] [blame] | 4730 | void debug_check_no_locks_held(void) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4731 | { |
Colin Cross | 1b1d2fb | 2013-05-06 23:50:08 +0000 | [diff] [blame] | 4732 | if (unlikely(current->lockdep_depth > 0)) |
| 4733 | print_held_locks_bug(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4734 | } |
Colin Cross | 1b1d2fb | 2013-05-06 23:50:08 +0000 | [diff] [blame] | 4735 | EXPORT_SYMBOL_GPL(debug_check_no_locks_held); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4736 | |
Sasha Levin | 8dce7a9 | 2013-06-13 18:41:16 -0400 | [diff] [blame] | 4737 | #ifdef __KERNEL__ |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4738 | void debug_show_all_locks(void) |
| 4739 | { |
| 4740 | struct task_struct *g, *p; |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4741 | |
Jarek Poplawski | 9c35dd7 | 2007-03-22 00:11:28 -0800 | [diff] [blame] | 4742 | if (unlikely(!debug_locks)) { |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4743 | pr_warn("INFO: lockdep is turned off.\n"); |
Jarek Poplawski | 9c35dd7 | 2007-03-22 00:11:28 -0800 | [diff] [blame] | 4744 | return; |
| 4745 | } |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4746 | pr_warn("\nShowing all locks held in the system:\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4747 | |
Tetsuo Handa | 0f736a5 | 2018-04-06 19:41:18 +0900 | [diff] [blame] | 4748 | rcu_read_lock(); |
| 4749 | for_each_process_thread(g, p) { |
Tetsuo Handa | 0f736a5 | 2018-04-06 19:41:18 +0900 | [diff] [blame] | 4750 | if (!p->lockdep_depth) |
| 4751 | continue; |
| 4752 | lockdep_print_held_locks(p); |
Tejun Heo | 88f1c87 | 2018-01-22 14:00:55 -0800 | [diff] [blame] | 4753 | touch_nmi_watchdog(); |
Tetsuo Handa | 0f736a5 | 2018-04-06 19:41:18 +0900 | [diff] [blame] | 4754 | touch_all_softlockup_watchdogs(); |
| 4755 | } |
| 4756 | rcu_read_unlock(); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4757 | |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4758 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 4759 | pr_warn("=============================================\n\n"); |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4760 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4761 | EXPORT_SYMBOL_GPL(debug_show_all_locks); |
Sasha Levin | 8dce7a9 | 2013-06-13 18:41:16 -0400 | [diff] [blame] | 4762 | #endif |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4763 | |
Ingo Molnar | 82a1fcb | 2008-01-25 21:08:02 +0100 | [diff] [blame] | 4764 | /* |
| 4765 | * Careful: only use this function if you are sure that |
| 4766 | * the task cannot run in parallel! |
| 4767 | */ |
John Kacur | f1b499f | 2010-08-05 17:10:53 +0200 | [diff] [blame] | 4768 | void debug_show_held_locks(struct task_struct *task) |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4769 | { |
Jarek Poplawski | 9c35dd7 | 2007-03-22 00:11:28 -0800 | [diff] [blame] | 4770 | if (unlikely(!debug_locks)) { |
| 4771 | printk("INFO: lockdep is turned off.\n"); |
| 4772 | return; |
| 4773 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4774 | lockdep_print_held_locks(task); |
| 4775 | } |
Ingo Molnar | fbb9ce95 | 2006-07-03 00:24:50 -0700 | [diff] [blame] | 4776 | EXPORT_SYMBOL_GPL(debug_show_held_locks); |
Peter Zijlstra | b351d16 | 2007-10-11 22:11:12 +0200 | [diff] [blame] | 4777 | |
Andi Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 4778 | asmlinkage __visible void lockdep_sys_exit(void) |
Peter Zijlstra | b351d16 | 2007-10-11 22:11:12 +0200 | [diff] [blame] | 4779 | { |
| 4780 | struct task_struct *curr = current; |
| 4781 | |
| 4782 | if (unlikely(curr->lockdep_depth)) { |
| 4783 | if (!debug_locks_off()) |
| 4784 | return; |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4785 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 4786 | pr_warn("================================================\n"); |
| 4787 | pr_warn("WARNING: lock held when returning to user space!\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 4788 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 4789 | pr_warn("------------------------------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4790 | 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] | 4791 | curr->comm, curr->pid); |
| 4792 | lockdep_print_held_locks(curr); |
| 4793 | } |
Byungchul Park | b09be67 | 2017-08-07 16:12:52 +0900 | [diff] [blame] | 4794 | |
| 4795 | /* |
| 4796 | * The lock history for each syscall should be independent. So wipe the |
| 4797 | * slate clean on return to userspace. |
| 4798 | */ |
Peter Zijlstra | f52be57 | 2017-08-29 10:59:39 +0200 | [diff] [blame] | 4799 | lockdep_invariant_state(false); |
Peter Zijlstra | b351d16 | 2007-10-11 22:11:12 +0200 | [diff] [blame] | 4800 | } |
Paul E. McKenney | 0632eb3 | 2010-02-22 17:04:47 -0800 | [diff] [blame] | 4801 | |
Paul E. McKenney | b3fbab0 | 2011-05-24 08:31:09 -0700 | [diff] [blame] | 4802 | 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] | 4803 | { |
| 4804 | struct task_struct *curr = current; |
| 4805 | |
Lai Jiangshan | 2b3fc35 | 2010-04-20 16:23:07 +0800 | [diff] [blame] | 4806 | /* Note: the following can be executed concurrently, so be careful. */ |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4807 | pr_warn("\n"); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 4808 | pr_warn("=============================\n"); |
| 4809 | pr_warn("WARNING: suspicious RCU usage\n"); |
Ben Hutchings | fbdc4b9 | 2011-10-28 04:36:55 +0100 | [diff] [blame] | 4810 | print_kernel_ident(); |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 4811 | pr_warn("-----------------------------\n"); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4812 | pr_warn("%s:%d %s!\n", file, line, s); |
| 4813 | pr_warn("\nother info that might help us debug this:\n\n"); |
| 4814 | pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n", |
Paul E. McKenney | c5fdcec | 2012-01-30 08:46:32 -0800 | [diff] [blame] | 4815 | !rcu_lockdep_current_cpu_online() |
| 4816 | ? "RCU used illegally from offline CPU!\n" |
Paul E. McKenney | 5c173eb | 2013-09-13 17:20:11 -0700 | [diff] [blame] | 4817 | : !rcu_is_watching() |
Paul E. McKenney | c5fdcec | 2012-01-30 08:46:32 -0800 | [diff] [blame] | 4818 | ? "RCU used illegally from idle CPU!\n" |
| 4819 | : "", |
| 4820 | rcu_scheduler_active, debug_locks); |
Frederic Weisbecker | 0464e93 | 2011-10-07 18:22:01 +0200 | [diff] [blame] | 4821 | |
| 4822 | /* |
| 4823 | * If a CPU is in the RCU-free window in idle (ie: in the section |
| 4824 | * between rcu_idle_enter() and rcu_idle_exit(), then RCU |
| 4825 | * considers that CPU to be in an "extended quiescent state", |
| 4826 | * which means that RCU will be completely ignoring that CPU. |
| 4827 | * Therefore, rcu_read_lock() and friends have absolutely no |
| 4828 | * effect on a CPU running in that state. In other words, even if |
| 4829 | * such an RCU-idle CPU has called rcu_read_lock(), RCU might well |
| 4830 | * delete data structures out from under it. RCU really has no |
| 4831 | * choice here: we need to keep an RCU-free window in idle where |
| 4832 | * the CPU may possibly enter into low power mode. This way we can |
| 4833 | * notice an extended quiescent state to other CPUs that started a grace |
| 4834 | * period. Otherwise we would delay any grace period as long as we run |
| 4835 | * in the idle task. |
| 4836 | * |
| 4837 | * So complain bitterly if someone does call rcu_read_lock(), |
| 4838 | * rcu_read_lock_bh() and so on from extended quiescent states. |
| 4839 | */ |
Paul E. McKenney | 5c173eb | 2013-09-13 17:20:11 -0700 | [diff] [blame] | 4840 | if (!rcu_is_watching()) |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4841 | pr_warn("RCU used illegally from extended quiescent state!\n"); |
Frederic Weisbecker | 0464e93 | 2011-10-07 18:22:01 +0200 | [diff] [blame] | 4842 | |
Paul E. McKenney | 0632eb3 | 2010-02-22 17:04:47 -0800 | [diff] [blame] | 4843 | lockdep_print_held_locks(curr); |
Paul E. McKenney | 681fbec | 2017-05-04 15:44:38 -0700 | [diff] [blame] | 4844 | pr_warn("\nstack backtrace:\n"); |
Paul E. McKenney | 0632eb3 | 2010-02-22 17:04:47 -0800 | [diff] [blame] | 4845 | dump_stack(); |
| 4846 | } |
Paul E. McKenney | b3fbab0 | 2011-05-24 08:31:09 -0700 | [diff] [blame] | 4847 | EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious); |