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