| 1 | |
| 2 | |
| 3 | * kernel/lockdep.c
|
| 4 | *
|
| 5 | * Runtime locking correctness validator
|
| 6 | *
|
| 7 | * Started by Ingo Molnar:
|
| 8 | *
|
| 9 | * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <[email protected]>
|
| 10 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
|
| 11 | *
|
| 12 | * this code maps all the lock dependencies as they occur in a live kernel
|
| 13 | * and will warn about the following classes of locking bugs:
|
| 14 | *
|
| 15 | * - lock inversion scenarios
|
| 16 | * - circular lock dependencies
|
| 17 | * - hardirq/softirq safe/unsafe locking bugs
|
| 18 | *
|
| 19 | * Bugs are reported even if the current locking scenario does not cause
|
| 20 | * any deadlock at this point.
|
| 21 | *
|
| 22 | * I.e. if anytime in the past two locks were taken in a different order,
|
| 23 | * even if it happened for another task, even if those were different
|
| 24 | * locks (but of the same class as this lock), this code will detect it.
|
| 25 | *
|
| 26 | * Thanks to Arjan van de Ven for coming up with the initial idea of
|
| 27 | * mapping lock dependencies runtime.
|
| 28 |
|
| 29 | #define DISABLE_BRANCH_PROFILING
|
| 30 | #include <linux/mutex.h>
|
| 31 | #include <linux/sched.h>
|
| 32 | #include <linux/sched/clock.h>
|
| 33 | #include <linux/sched/task.h>
|
| 34 | #include <linux/sched/mm.h>
|
| 35 | #include <linux/delay.h>
|
| 36 | #include <linux/module.h>
|
| 37 | #include <linux/proc_fs.h>
|
| 38 | #include <linux/seq_file.h>
|
| 39 | #include <linux/spinlock.h>
|
| 40 | #include <linux/kallsyms.h>
|
| 41 | #include <linux/interrupt.h>
|
| 42 | #include <linux/stacktrace.h>
|
| 43 | #include <linux/debug_locks.h>
|
| 44 | #include <linux/irqflags.h>
|
| 45 | #include <linux/utsname.h>
|
| 46 | #include <linux/hash.h>
|
| 47 | #include <linux/ftrace.h>
|
| 48 | #include <linux/stringify.h>
|
| 49 | #include <linux/bitmap.h>
|
| 50 | #include <linux/bitops.h>
|
| 51 | #include <linux/gfp.h>
|
| 52 | #include <linux/random.h>
|
| 53 | #include <linux/jhash.h>
|
| 54 | #include <linux/nmi.h>
|
| 55 | #include <linux/rcupdate.h>
|
| 56 | #include <linux/kprobes.h>
|
| 57 | #include <linux/lockdep.h>
|
| 58 | #include <linux/context_tracking.h>
|
| 59 | #include <linux/console.h>
|
| 60 | #include <linux/kasan.h>
|
| 61 |
|
| 62 | #include <asm/sections.h>
|
| 63 |
|
| 64 | #include "lockdep_internals.h"
|
| 65 | #include "lock_events.h"
|
| 66 |
|
| 67 | #include <trace/events/lock.h>
|
| 68 |
|
| 69 | #ifdef CONFIG_PROVE_LOCKING
|
| 70 | static int prove_locking = 1;
|
| 71 | module_param(prove_locking, int, 0644);
|
| 72 | #else
|
| 73 | #define prove_locking 0
|
| 74 | #endif
|
| 75 |
|
| 76 | #ifdef CONFIG_LOCK_STAT
|
| 77 | static int lock_stat = 1;
|
| 78 | module_param(lock_stat, int, 0644);
|
| 79 | #else
|
| 80 | #define lock_stat 0
|
| 81 | #endif
|
| 82 |
|
| 83 | #ifdef CONFIG_SYSCTL
|
| 84 | static const struct ctl_table kern_lockdep_table[] = {
|
| 85 | #ifdef CONFIG_PROVE_LOCKING
|
| 86 | {
|
| 87 | .procname = "prove_locking",
|
| 88 | .data = &prove_locking,
|
| 89 | .maxlen = sizeof(int),
|
| 90 | .mode = 0644,
|
| 91 | .proc_handler = proc_dointvec,
|
| 92 | },
|
| 93 | #endif
|
| 94 | #ifdef CONFIG_LOCK_STAT
|
| 95 | {
|
| 96 | .procname = "lock_stat",
|
| 97 | .data = &lock_stat,
|
| 98 | .maxlen = sizeof(int),
|
| 99 | .mode = 0644,
|
| 100 | .proc_handler = proc_dointvec,
|
| 101 | },
|
| 102 | #endif
|
| 103 | };
|
| 104 |
|
| 105 | static __init int kernel_lockdep_sysctls_init(void)
|
| 106 | {
|
| 107 | register_sysctl_init("kernel", kern_lockdep_table);
|
| 108 | return 0;
|
| 109 | }
|
| 110 | late_initcall(kernel_lockdep_sysctls_init);
|
| 111 | #endif
|
| 112 |
|
| 113 | DEFINE_PER_CPU(unsigned int, lockdep_recursion);
|
| 114 | EXPORT_PER_CPU_SYMBOL_GPL(lockdep_recursion);
|
| 115 |
|
| 116 | static __always_inline bool lockdep_enabled(void)
|
| 117 | {
|
| 118 | if (!debug_locks)
|
| 119 | return false;
|
| 120 |
|
| 121 | if (this_cpu_read(lockdep_recursion))
|
| 122 | return false;
|
| 123 |
|
| 124 | if (current->lockdep_recursion)
|
| 125 | return false;
|
| 126 |
|
| 127 | return true;
|
| 128 | }
|
| 129 |
|
| 130 | |
| 131 | * lockdep_lock: protects the lockdep graph, the hashes and the
|
| 132 | * class/list/hash allocators.
|
| 133 | *
|
| 134 | * This is one of the rare exceptions where it's justified
|
| 135 | * to use a raw spinlock - we really dont want the spinlock
|
| 136 | * code to recurse back into the lockdep code...
|
| 137 |
|
| 138 | static arch_spinlock_t __lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
|
| 139 | static struct task_struct *__owner;
|
| 140 |
|
| 141 | static inline void lockdep_lock(void)
|
| 142 | {
|
| 143 | DEBUG_LOCKS_WARN_ON(!irqs_disabled());
|
| 144 |
|
| 145 | __this_cpu_inc(lockdep_recursion);
|
| 146 | arch_spin_lock(&__lock);
|
| 147 | __owner = current;
|
| 148 | }
|
| 149 |
|
| 150 | static inline void lockdep_unlock(void)
|
| 151 | {
|
| 152 | DEBUG_LOCKS_WARN_ON(!irqs_disabled());
|
| 153 |
|
| 154 | if (debug_locks && DEBUG_LOCKS_WARN_ON(__owner != current))
|
| 155 | return;
|
| 156 |
|
| 157 | __owner = NULL;
|
| 158 | arch_spin_unlock(&__lock);
|
| 159 | __this_cpu_dec(lockdep_recursion);
|
| 160 | }
|
| 161 |
|
| 162 | #ifdef CONFIG_PROVE_LOCKING
|
| 163 | static inline bool lockdep_assert_locked(void)
|
| 164 | {
|
| 165 | return DEBUG_LOCKS_WARN_ON(__owner != current);
|
| 166 | }
|
| 167 | #endif
|
| 168 |
|
| 169 | static struct task_struct *lockdep_selftest_task_struct;
|
| 170 |
|
| 171 |
|
| 172 | static int graph_lock(void)
|
| 173 | {
|
| 174 | lockdep_lock();
|
| 175 | lockevent_inc(lockdep_lock);
|
| 176 | |
| 177 | * Make sure that if another CPU detected a bug while
|
| 178 | * walking the graph we dont change it (while the other
|
| 179 | * CPU is busy printing out stuff with the graph lock
|
| 180 | * dropped already)
|
| 181 |
|
| 182 | if (!debug_locks) {
|
| 183 | lockdep_unlock();
|
| 184 | return 0;
|
| 185 | }
|
| 186 | return 1;
|
| 187 | }
|
| 188 |
|
| 189 | static inline void graph_unlock(void)
|
| 190 | {
|
| 191 | lockdep_unlock();
|
| 192 | }
|
| 193 |
|
| 194 | |
| 195 | * Turn lock debugging off and return with 0 if it was off already,
|
| 196 | * and also release the graph lock:
|
| 197 |
|
| 198 | static inline int debug_locks_off_graph_unlock(void)
|
| 199 | {
|
| 200 | int ret = debug_locks_off();
|
| 201 |
|
| 202 | lockdep_unlock();
|
| 203 |
|
| 204 | return ret;
|
| 205 | }
|
| 206 |
|
| 207 | unsigned long nr_list_entries;
|
| 208 | static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
|
| 209 | static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES);
|
| 210 |
|
| 211 | |
| 212 | * All data structures here are protected by the global debug_lock.
|
| 213 | *
|
| 214 | * nr_lock_classes is the number of elements of lock_classes[] that is
|
| 215 | * in use.
|
| 216 |
|
| 217 | #define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
|
| 218 | #define KEYHASH_SIZE (1UL << KEYHASH_BITS)
|
| 219 | static struct hlist_head lock_keys_hash[KEYHASH_SIZE];
|
| 220 | unsigned long nr_lock_classes;
|
| 221 | unsigned long nr_zapped_classes;
|
| 222 | unsigned long nr_dynamic_keys;
|
| 223 | unsigned long max_lock_class_idx;
|
| 224 | struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
|
| 225 | DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
|
| 226 |
|
| 227 | static inline struct lock_class *hlock_class(struct held_lock *hlock)
|
| 228 | {
|
| 229 | unsigned int class_idx = hlock->class_idx;
|
| 230 |
|
| 231 |
|
| 232 | barrier();
|
| 233 |
|
| 234 | if (!test_bit(class_idx, lock_classes_in_use)) {
|
| 235 | |
| 236 | * Someone passed in garbage, we give up.
|
| 237 |
|
| 238 | DEBUG_LOCKS_WARN_ON(1);
|
| 239 | return NULL;
|
| 240 | }
|
| 241 |
|
| 242 | |
| 243 | * At this point, if the passed hlock->class_idx is still garbage,
|
| 244 | * we just have to live with it
|
| 245 |
|
| 246 | return lock_classes + class_idx;
|
| 247 | }
|
| 248 |
|
| 249 | #ifdef CONFIG_LOCK_STAT
|
| 250 | static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
|
| 251 |
|
| 252 | static inline u64 lockstat_clock(void)
|
| 253 | {
|
| 254 | return local_clock();
|
| 255 | }
|
| 256 |
|
| 257 | static int lock_point(unsigned long points[], unsigned long ip)
|
| 258 | {
|
| 259 | int i;
|
| 260 |
|
| 261 | for (i = 0; i < LOCKSTAT_POINTS; i++) {
|
| 262 | if (points[i] == 0) {
|
| 263 | points[i] = ip;
|
| 264 | break;
|
| 265 | }
|
| 266 | if (points[i] == ip)
|
| 267 | break;
|
| 268 | }
|
| 269 |
|
| 270 | return i;
|
| 271 | }
|
| 272 |
|
| 273 | static void lock_time_inc(struct lock_time *lt, u64 time)
|
| 274 | {
|
| 275 | if (time > lt->max)
|
| 276 | lt->max = time;
|
| 277 |
|
| 278 | if (time < lt->min || !lt->nr)
|
| 279 | lt->min = time;
|
| 280 |
|
| 281 | lt->total += time;
|
| 282 | lt->nr++;
|
| 283 | }
|
| 284 |
|
| 285 | static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
|
| 286 | {
|
| 287 | if (!src->nr)
|
| 288 | return;
|
| 289 |
|
| 290 | if (src->max > dst->max)
|
| 291 | dst->max = src->max;
|
| 292 |
|
| 293 | if (src->min < dst->min || !dst->nr)
|
| 294 | dst->min = src->min;
|
| 295 |
|
| 296 | dst->total += src->total;
|
| 297 | dst->nr += src->nr;
|
| 298 | }
|
| 299 |
|
| 300 | void lock_stats(struct lock_class *class, struct lock_class_stats *stats)
|
| 301 | {
|
| 302 | int cpu, i;
|
| 303 |
|
| 304 | memset(stats, 0, sizeof(struct lock_class_stats));
|
| 305 | for_each_possible_cpu(cpu) {
|
| 306 | struct lock_class_stats *pcs =
|
| 307 | &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
|
| 308 |
|
| 309 | for (i = 0; i < ARRAY_SIZE(stats->contention_point); i++)
|
| 310 | stats->contention_point[i] += pcs->contention_point[i];
|
| 311 |
|
| 312 | for (i = 0; i < ARRAY_SIZE(stats->contending_point); i++)
|
| 313 | stats->contending_point[i] += pcs->contending_point[i];
|
| 314 |
|
| 315 | lock_time_add(&pcs->read_waittime, &stats->read_waittime);
|
| 316 | lock_time_add(&pcs->write_waittime, &stats->write_waittime);
|
| 317 |
|
| 318 | lock_time_add(&pcs->read_holdtime, &stats->read_holdtime);
|
| 319 | lock_time_add(&pcs->write_holdtime, &stats->write_holdtime);
|
| 320 |
|
| 321 | for (i = 0; i < ARRAY_SIZE(stats->bounces); i++)
|
| 322 | stats->bounces[i] += pcs->bounces[i];
|
| 323 | }
|
| 324 | }
|
| 325 |
|
| 326 | void clear_lock_stats(struct lock_class *class)
|
| 327 | {
|
| 328 | int cpu;
|
| 329 |
|
| 330 | for_each_possible_cpu(cpu) {
|
| 331 | struct lock_class_stats *cpu_stats =
|
| 332 | &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
|
| 333 |
|
| 334 | memset(cpu_stats, 0, sizeof(struct lock_class_stats));
|
| 335 | }
|
| 336 | memset(class->contention_point, 0, sizeof(class->contention_point));
|
| 337 | memset(class->contending_point, 0, sizeof(class->contending_point));
|
| 338 | }
|
| 339 |
|
| 340 | static struct lock_class_stats *get_lock_stats(struct lock_class *class)
|
| 341 | {
|
| 342 | return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes];
|
| 343 | }
|
| 344 |
|
| 345 | static void lock_release_holdtime(struct held_lock *hlock)
|
| 346 | {
|
| 347 | struct lock_class_stats *stats;
|
| 348 | u64 holdtime;
|
| 349 |
|
| 350 | if (!lock_stat)
|
| 351 | return;
|
| 352 |
|
| 353 | holdtime = lockstat_clock() - hlock->holdtime_stamp;
|
| 354 |
|
| 355 | stats = get_lock_stats(hlock_class(hlock));
|
| 356 | if (hlock->read)
|
| 357 | lock_time_inc(&stats->read_holdtime, holdtime);
|
| 358 | else
|
| 359 | lock_time_inc(&stats->write_holdtime, holdtime);
|
| 360 | }
|
| 361 | #else
|
| 362 | static inline void lock_release_holdtime(struct held_lock *hlock)
|
| 363 | {
|
| 364 | }
|
| 365 | #endif
|
| 366 |
|
| 367 | |
| 368 | * We keep a global list of all lock classes. The list is only accessed with
|
| 369 | * the lockdep spinlock lock held. free_lock_classes is a list with free
|
| 370 | * elements. These elements are linked together by the lock_entry member in
|
| 371 | * struct lock_class.
|
| 372 |
|
| 373 | static LIST_HEAD(all_lock_classes);
|
| 374 | static LIST_HEAD(free_lock_classes);
|
| 375 |
|
| 376 | |
| 377 | * struct pending_free - information about data structures about to be freed
|
| 378 | * @zapped: Head of a list with struct lock_class elements.
|
| 379 | * @lock_chains_being_freed: Bitmap that indicates which lock_chains[] elements
|
| 380 | * are about to be freed.
|
| 381 |
|
| 382 | struct pending_free {
|
| 383 | struct list_head zapped;
|
| 384 | DECLARE_BITMAP(lock_chains_being_freed, MAX_LOCKDEP_CHAINS);
|
| 385 | };
|
| 386 |
|
| 387 | |
| 388 | * struct delayed_free - data structures used for delayed freeing
|
| 389 | *
|
| 390 | * A data structure for delayed freeing of data structures that may be
|
| 391 | * accessed by RCU readers at the time these were freed.
|
| 392 | *
|
| 393 | * @rcu_head: Used to schedule an RCU callback for freeing data structures.
|
| 394 | * @index: Index of @pf to which freed data structures are added.
|
| 395 | * @scheduled: Whether or not an RCU callback has been scheduled.
|
| 396 | * @pf: Array with information about data structures about to be freed.
|
| 397 |
|
| 398 | static struct delayed_free {
|
| 399 | struct rcu_head rcu_head;
|
| 400 | int index;
|
| 401 | int scheduled;
|
| 402 | struct pending_free pf[2];
|
| 403 | } delayed_free;
|
| 404 |
|
| 405 | |
| 406 | * The lockdep classes are in a hash-table as well, for fast lookup:
|
| 407 |
|
| 408 | #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
|
| 409 | #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
|
| 410 | #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
|
| 411 | #define classhashentry(key) (classhash_table + __classhashfn((key)))
|
| 412 |
|
| 413 | static struct hlist_head classhash_table[CLASSHASH_SIZE];
|
| 414 |
|
| 415 | |
| 416 | * We put the lock dependency chains into a hash-table as well, to cache
|
| 417 | * their existence:
|
| 418 |
|
| 419 | #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
|
| 420 | #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
|
| 421 | #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
|
| 422 | #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
|
| 423 |
|
| 424 | static struct hlist_head chainhash_table[CHAINHASH_SIZE];
|
| 425 |
|
| 426 | |
| 427 | * the id of held_lock
|
| 428 |
|
| 429 | static inline u16 hlock_id(struct held_lock *hlock)
|
| 430 | {
|
| 431 | BUILD_BUG_ON(MAX_LOCKDEP_KEYS_BITS + 2 > 16);
|
| 432 |
|
| 433 | return (hlock->class_idx | (hlock->read << MAX_LOCKDEP_KEYS_BITS));
|
| 434 | }
|
| 435 |
|
| 436 | static inline __maybe_unused unsigned int chain_hlock_class_idx(u16 hlock_id)
|
| 437 | {
|
| 438 | return hlock_id & (MAX_LOCKDEP_KEYS - 1);
|
| 439 | }
|
| 440 |
|
| 441 | |
| 442 | * The hash key of the lock dependency chains is a hash itself too:
|
| 443 | * it's a hash of all locks taken up to that lock, including that lock.
|
| 444 | * It's a 64-bit hash, because it's important for the keys to be
|
| 445 | * unique.
|
| 446 |
|
| 447 | static inline u64 iterate_chain_key(u64 key, u32 idx)
|
| 448 | {
|
| 449 | u32 k0 = key, k1 = key >> 32;
|
| 450 |
|
| 451 | __jhash_mix(idx, k0, k1);
|
| 452 |
|
| 453 | return k0 | (u64)k1 << 32;
|
| 454 | }
|
| 455 |
|
| 456 | void lockdep_init_task(struct task_struct *task)
|
| 457 | {
|
| 458 | task->lockdep_depth = 0;
|
| 459 | task->curr_chain_key = INITIAL_CHAIN_KEY;
|
| 460 | task->lockdep_recursion = 0;
|
| 461 | }
|
| 462 |
|
| 463 | static __always_inline void lockdep_recursion_inc(void)
|
| 464 | {
|
| 465 | __this_cpu_inc(lockdep_recursion);
|
| 466 | }
|
| 467 |
|
| 468 | static __always_inline void lockdep_recursion_finish(void)
|
| 469 | {
|
| 470 | if (WARN_ON_ONCE(__this_cpu_dec_return(lockdep_recursion)))
|
| 471 | __this_cpu_write(lockdep_recursion, 0);
|
| 472 | }
|
| 473 |
|
| 474 | void lockdep_set_selftest_task(struct task_struct *task)
|
| 475 | {
|
| 476 | lockdep_selftest_task_struct = task;
|
| 477 | }
|
| 478 |
|
| 479 | |
| 480 | * Debugging switches:
|
| 481 |
|
| 482 |
|
| 483 | #define VERBOSE 0
|
| 484 | #define VERY_VERBOSE 0
|
| 485 |
|
| 486 | #if VERBOSE
|
| 487 | # define HARDIRQ_VERBOSE 1
|
| 488 | # define SOFTIRQ_VERBOSE 1
|
| 489 | #else
|
| 490 | # define HARDIRQ_VERBOSE 0
|
| 491 | # define SOFTIRQ_VERBOSE 0
|
| 492 | #endif
|
| 493 |
|
| 494 | #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
|
| 495 | |
| 496 | * Quick filtering for interesting events:
|
| 497 |
|
| 498 | static int class_filter(struct lock_class *class)
|
| 499 | {
|
| 500 | #if 0 |
| 501 | /* Example */
|
| 502 | if (class->name_version == 1 &&
|
| 503 | !strcmp(class->name, "lockname"))
|
| 504 | return 1;
|
| 505 | if (class->name_version == 1 &&
|
| 506 | !strcmp(class->name, "&struct->lockfield"))
|
| 507 | return 1;
|
| 508 | #endif
|
| 509 |
|
| 510 | return 0;
|
| 511 | }
|
| 512 | #endif
|
| 513 |
|
| 514 | static int verbose(struct lock_class *class)
|
| 515 | {
|
| 516 | #if VERBOSE
|
| 517 | return class_filter(class);
|
| 518 | #endif
|
| 519 | return 0;
|
| 520 | }
|
| 521 |
|
| 522 | static void print_lockdep_off(const char *bug_msg)
|
| 523 | {
|
| 524 | printk(KERN_DEBUG "%s\n", bug_msg);
|
| 525 | printk(KERN_DEBUG "turning off the locking correctness validator.\n");
|
| 526 | #ifdef CONFIG_LOCK_STAT
|
| 527 | printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
|
| 528 | #endif
|
| 529 | }
|
| 530 |
|
| 531 | unsigned long nr_stack_trace_entries;
|
| 532 |
|
| 533 | #ifdef CONFIG_PROVE_LOCKING
|
| 534 | |
| 535 | * struct lock_trace - single stack backtrace
|
| 536 | * @hash_entry: Entry in a stack_trace_hash[] list.
|
| 537 | * @hash: jhash() of @entries.
|
| 538 | * @nr_entries: Number of entries in @entries.
|
| 539 | * @entries: Actual stack backtrace.
|
| 540 |
|
| 541 | struct lock_trace {
|
| 542 | struct hlist_node hash_entry;
|
| 543 | u32 hash;
|
| 544 | u32 nr_entries;
|
| 545 | unsigned long entries[] __aligned(sizeof(unsigned long));
|
| 546 | };
|
| 547 | #define LOCK_TRACE_SIZE_IN_LONGS \
|
| 548 | (sizeof(struct lock_trace) / sizeof(unsigned long))
|
| 549 | |
| 550 | * Stack-trace: sequence of lock_trace structures. Protected by the graph_lock.
|
| 551 |
|
| 552 | static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
|
| 553 | static struct hlist_head stack_trace_hash[STACK_TRACE_HASH_SIZE];
|
| 554 |
|
| 555 | static bool traces_identical(struct lock_trace *t1, struct lock_trace *t2)
|
| 556 | {
|
| 557 | return t1->hash == t2->hash && t1->nr_entries == t2->nr_entries &&
|
| 558 | memcmp(t1->entries, t2->entries,
|
| 559 | t1->nr_entries * sizeof(t1->entries[0])) == 0;
|
| 560 | }
|
| 561 |
|
| 562 | static struct lock_trace *save_trace(void)
|
| 563 | {
|
| 564 | struct lock_trace *trace, *t2;
|
| 565 | struct hlist_head *hash_head;
|
| 566 | u32 hash;
|
| 567 | int max_entries;
|
| 568 |
|
| 569 | BUILD_BUG_ON_NOT_POWER_OF_2(STACK_TRACE_HASH_SIZE);
|
| 570 | BUILD_BUG_ON(LOCK_TRACE_SIZE_IN_LONGS >= MAX_STACK_TRACE_ENTRIES);
|
| 571 |
|
| 572 | trace = (struct lock_trace *)(stack_trace + nr_stack_trace_entries);
|
| 573 | max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries -
|
| 574 | LOCK_TRACE_SIZE_IN_LONGS;
|
| 575 |
|
| 576 | if (max_entries <= 0) {
|
| 577 | if (!debug_locks_off_graph_unlock())
|
| 578 | return NULL;
|
| 579 |
|
| 580 | nbcon_cpu_emergency_enter();
|
| 581 | print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
|
| 582 | dump_stack();
|
| 583 | nbcon_cpu_emergency_exit();
|
| 584 |
|
| 585 | return NULL;
|
| 586 | }
|
| 587 | trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3);
|
| 588 |
|
| 589 | hash = jhash(trace->entries, trace->nr_entries *
|
| 590 | sizeof(trace->entries[0]), 0);
|
| 591 | trace->hash = hash;
|
| 592 | hash_head = stack_trace_hash + (hash & (STACK_TRACE_HASH_SIZE - 1));
|
| 593 | hlist_for_each_entry(t2, hash_head, hash_entry) {
|
| 594 | if (traces_identical(trace, t2))
|
| 595 | return t2;
|
| 596 | }
|
| 597 | nr_stack_trace_entries += LOCK_TRACE_SIZE_IN_LONGS + trace->nr_entries;
|
| 598 | hlist_add_head(&trace->hash_entry, hash_head);
|
| 599 |
|
| 600 | return trace;
|
| 601 | }
|
| 602 |
|
| 603 |
|
| 604 | u64 lockdep_stack_trace_count(void)
|
| 605 | {
|
| 606 | struct lock_trace *trace;
|
| 607 | u64 c = 0;
|
| 608 | int i;
|
| 609 |
|
| 610 | for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++) {
|
| 611 | hlist_for_each_entry(trace, &stack_trace_hash[i], hash_entry) {
|
| 612 | c++;
|
| 613 | }
|
| 614 | }
|
| 615 |
|
| 616 | return c;
|
| 617 | }
|
| 618 |
|
| 619 |
|
| 620 | u64 lockdep_stack_hash_count(void)
|
| 621 | {
|
| 622 | u64 c = 0;
|
| 623 | int i;
|
| 624 |
|
| 625 | for (i = 0; i < ARRAY_SIZE(stack_trace_hash); i++)
|
| 626 | if (!hlist_empty(&stack_trace_hash[i]))
|
| 627 | c++;
|
| 628 |
|
| 629 | return c;
|
| 630 | }
|
| 631 | #endif
|
| 632 |
|
| 633 | unsigned int nr_hardirq_chains;
|
| 634 | unsigned int nr_softirq_chains;
|
| 635 | unsigned int nr_process_chains;
|
| 636 | unsigned int max_lockdep_depth;
|
| 637 |
|
| 638 | #ifdef CONFIG_DEBUG_LOCKDEP
|
| 639 | |
| 640 | * Various lockdep statistics:
|
| 641 |
|
| 642 | DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
|
| 643 | #endif
|
| 644 |
|
| 645 | #ifdef CONFIG_PROVE_LOCKING
|
| 646 | |
| 647 | * Locking printouts:
|
| 648 |
|
| 649 |
|
| 650 | #define __USAGE(__STATE) \
|
| 651 | [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
|
| 652 | [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
|
| 653 | [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
|
| 654 | [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
|
| 655 |
|
| 656 | static const char *usage_str[] =
|
| 657 | {
|
| 658 | #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
|
| 659 | #include "lockdep_states.h"
|
| 660 | #undef LOCKDEP_STATE
|
| 661 | [LOCK_USED] = "INITIAL USE",
|
| 662 | [LOCK_USED_READ] = "INITIAL READ USE",
|
| 663 |
|
| 664 | [LOCK_USAGE_STATES] = "IN-NMI",
|
| 665 | };
|
| 666 | #endif
|
| 667 |
|
| 668 | const char *__get_key_name(const struct lockdep_subclass_key *key, char *str)
|
| 669 | {
|
| 670 | return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
|
| 671 | }
|
| 672 |
|
| 673 | static inline unsigned long lock_flag(enum lock_usage_bit bit)
|
| 674 | {
|
| 675 | return 1UL << bit;
|
| 676 | }
|
| 677 |
|
| 678 | static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
|
| 679 | {
|
| 680 | |
| 681 | * The usage character defaults to '.' (i.e., irqs disabled and not in
|
| 682 | * irq context), which is the safest usage category.
|
| 683 |
|
| 684 | char c = '.';
|
| 685 |
|
| 686 | |
| 687 | * The order of the following usage checks matters, which will
|
| 688 | * result in the outcome character as follows:
|
| 689 | *
|
| 690 | * - '+': irq is enabled and not in irq context
|
| 691 | * - '-': in irq context and irq is disabled
|
| 692 | * - '?': in irq context and irq is enabled
|
| 693 |
|
| 694 | if (class->usage_mask & lock_flag(bit + LOCK_USAGE_DIR_MASK)) {
|
| 695 | c = '+';
|
| 696 | if (class->usage_mask & lock_flag(bit))
|
| 697 | c = '?';
|
| 698 | } else if (class->usage_mask & lock_flag(bit))
|
| 699 | c = '-';
|
| 700 |
|
| 701 | return c;
|
| 702 | }
|
| 703 |
|
| 704 | void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
|
| 705 | {
|
| 706 | int i = 0;
|
| 707 |
|
| 708 | #define LOCKDEP_STATE(__STATE) \
|
| 709 | usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
|
| 710 | usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
|
| 711 | #include "lockdep_states.h"
|
| 712 | #undef LOCKDEP_STATE
|
| 713 |
|
| 714 | usage[i] = '\0';
|
| 715 | }
|
| 716 |
|
| 717 | static void __print_lock_name(struct held_lock *hlock, struct lock_class *class)
|
| 718 | {
|
| 719 | char str[KSYM_NAME_LEN];
|
| 720 | const char *name;
|
| 721 |
|
| 722 | name = class->name;
|
| 723 | if (!name) {
|
| 724 | name = __get_key_name(class->key, str);
|
| 725 | printk(KERN_CONT "%s", name);
|
| 726 | } else {
|
| 727 | printk(KERN_CONT "%s", name);
|
| 728 | if (class->name_version > 1)
|
| 729 | printk(KERN_CONT "#%d", class->name_version);
|
| 730 | if (class->subclass)
|
| 731 | printk(KERN_CONT "/%d", class->subclass);
|
| 732 | if (hlock && class->print_fn)
|
| 733 | class->print_fn(hlock->instance);
|
| 734 | }
|
| 735 | }
|
| 736 |
|
| 737 | static void print_lock_name(struct held_lock *hlock, struct lock_class *class)
|
| 738 | {
|
| 739 | char usage[LOCK_USAGE_CHARS];
|
| 740 |
|
| 741 | get_usage_chars(class, usage);
|
| 742 |
|
| 743 | printk(KERN_CONT " (");
|
| 744 | __print_lock_name(hlock, class);
|
| 745 | printk(KERN_CONT "){%s}-{%d:%d}", usage,
|
| 746 | class->wait_type_outer ?: class->wait_type_inner,
|
| 747 | class->wait_type_inner);
|
| 748 | }
|
| 749 |
|
| 750 | static void print_lockdep_cache(struct lockdep_map *lock)
|
| 751 | {
|
| 752 | const char *name;
|
| 753 | char str[KSYM_NAME_LEN];
|
| 754 |
|
| 755 | name = lock->name;
|
| 756 | if (!name)
|
| 757 | name = __get_key_name(lock->key->subkeys, str);
|
| 758 |
|
| 759 | printk(KERN_CONT "%s", name);
|
| 760 | }
|
| 761 |
|
| 762 | static void print_lock(struct held_lock *hlock)
|
| 763 | {
|
| 764 | |
| 765 | * We can be called locklessly through debug_show_all_locks() so be
|
| 766 | * extra careful, the hlock might have been released and cleared.
|
| 767 | *
|
| 768 | * If this indeed happens, lets pretend it does not hurt to continue
|
| 769 | * to print the lock unless the hlock class_idx does not point to a
|
| 770 | * registered class. The rationale here is: since we don't attempt
|
| 771 | * to distinguish whether we are in this situation, if it just
|
| 772 | * happened we can't count on class_idx to tell either.
|
| 773 |
|
| 774 | struct lock_class *lock = hlock_class(hlock);
|
| 775 |
|
| 776 | if (!lock) {
|
| 777 | printk(KERN_CONT "<RELEASED>\n");
|
| 778 | return;
|
| 779 | }
|
| 780 |
|
| 781 | printk(KERN_CONT "%px", hlock->instance);
|
| 782 | print_lock_name(hlock, lock);
|
| 783 | printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip);
|
| 784 | }
|
| 785 |
|
| 786 | static void lockdep_print_held_locks(struct task_struct *p)
|
| 787 | {
|
| 788 | int i, depth = READ_ONCE(p->lockdep_depth);
|
| 789 |
|
| 790 | |
| 791 | * Note that it's always somewhat unreliable to print held locks
|
| 792 | * of a task that is running on another CPU, but we cannot guarantee
|
| 793 | * the stability of ->held_locks without actually stopping all active
|
| 794 | * remote CPUs, which we absolutely do not want to do because it's
|
| 795 | * very intrusive and thus slow.
|
| 796 | *
|
| 797 | * So we do the next best thing here: we print out the held lock
|
| 798 | * array on a best-effort basis, without crashing even if the
|
| 799 | * fields are being modified on another CPU. Note the careful
|
| 800 | * construction of print_lock() so that it never crashes.
|
| 801 | *
|
| 802 | * We also print out the CPU the task is or was last running on, with
|
| 803 | * the message saying 'on CPU...' if the task is running, and
|
| 804 | * 'last CPU' if it's not.
|
| 805 | *
|
| 806 | * Also note that the task_is_running(p) information is fundamentally
|
| 807 | * racy: even if the message says the task is 'on CPU', the task may
|
| 808 | * have scheduled out already, or if it says 'last CPU', it may just
|
| 809 | * have scheduled in on another CPU. But even with these limitations
|
| 810 | * it's still useful debuggining information.
|
| 811 |
|
| 812 | printk("locks held by %s/%d: %d, %s CPU#%d%s\n",
|
| 813 | p->comm, task_pid_nr(p), depth,
|
| 814 | task_is_running(p) ? "last" : "on", task_cpu(p),
|
| 815 | depth > 0 ? ":" : "");
|
| 816 |
|
| 817 | for (i = 0; i < depth; i++) {
|
| 818 | printk(" #%d: ", i);
|
| 819 | print_lock(p->held_locks + i);
|
| 820 | }
|
| 821 | }
|
| 822 |
|
| 823 | static void print_kernel_ident(void)
|
| 824 | {
|
| 825 | printk("%s %.*s %s\n", init_utsname()->release,
|
| 826 | (int)strcspn(init_utsname()->version, " "),
|
| 827 | init_utsname()->version,
|
| 828 | print_tainted());
|
| 829 | }
|
| 830 |
|
| 831 | static int very_verbose(struct lock_class *class)
|
| 832 | {
|
| 833 | #if VERY_VERBOSE
|
| 834 | return class_filter(class);
|
| 835 | #endif
|
| 836 | return 0;
|
| 837 | }
|
| 838 |
|
| 839 | |
| 840 | * Is this the address of a static object:
|
| 841 |
|
| 842 | #ifdef __KERNEL__
|
| 843 | static int static_obj(const void *obj)
|
| 844 | {
|
| 845 | unsigned long addr = (unsigned long) obj;
|
| 846 |
|
| 847 | if (is_kernel_core_data(addr))
|
| 848 | return 1;
|
| 849 |
|
| 850 | |
| 851 | * keys are allowed in the __ro_after_init section.
|
| 852 |
|
| 853 | if (is_kernel_rodata(addr))
|
| 854 | return 1;
|
| 855 |
|
| 856 | |
| 857 | * in initdata section and used during bootup only?
|
| 858 | * NOTE: On some platforms the initdata section is
|
| 859 | * outside of the _stext ... _end range.
|
| 860 |
|
| 861 | if (system_state < SYSTEM_FREEING_INITMEM &&
|
| 862 | init_section_contains((void *)addr, 1))
|
| 863 | return 1;
|
| 864 |
|
| 865 | |
| 866 | * in-kernel percpu var?
|
| 867 |
|
| 868 | if (is_kernel_percpu_address(addr))
|
| 869 | return 1;
|
| 870 |
|
| 871 | |
| 872 | * module static or percpu var?
|
| 873 |
|
| 874 | return is_module_address(addr) || is_module_percpu_address(addr);
|
| 875 | }
|
| 876 | #endif
|
| 877 |
|
| 878 | |
| 879 | * To make lock name printouts unique, we calculate a unique
|
| 880 | * class->name_version generation counter. The caller must hold the graph
|
| 881 | * lock.
|
| 882 |
|
| 883 | static int count_matching_names(struct lock_class *new_class)
|
| 884 | {
|
| 885 | struct lock_class *class;
|
| 886 | int count = 0;
|
| 887 |
|
| 888 | if (!new_class->name)
|
| 889 | return 0;
|
| 890 |
|
| 891 | list_for_each_entry(class, &all_lock_classes, lock_entry) {
|
| 892 | if (new_class->key - new_class->subclass == class->key)
|
| 893 | return class->name_version;
|
| 894 | if (class->name && !strcmp(class->name, new_class->name))
|
| 895 | count = max(count, class->name_version);
|
| 896 | }
|
| 897 |
|
| 898 | return count + 1;
|
| 899 | }
|
| 900 |
|
| 901 |
|
| 902 | static noinstr struct lock_class *
|
| 903 | look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
|
| 904 | {
|
| 905 | struct lockdep_subclass_key *key;
|
| 906 | struct hlist_head *hash_head;
|
| 907 | struct lock_class *class;
|
| 908 |
|
| 909 | if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
|
| 910 | instrumentation_begin();
|
| 911 | debug_locks_off();
|
| 912 | nbcon_cpu_emergency_enter();
|
| 913 | printk(KERN_ERR
|
| 914 | "BUG: looking up invalid subclass: %u\n", subclass);
|
| 915 | printk(KERN_ERR
|
| 916 | "turning off the locking correctness validator.\n");
|
| 917 | dump_stack();
|
| 918 | nbcon_cpu_emergency_exit();
|
| 919 | instrumentation_end();
|
| 920 | return NULL;
|
| 921 | }
|
| 922 |
|
| 923 | |
| 924 | * If it is not initialised then it has never been locked,
|
| 925 | * so it won't be present in the hash table.
|
| 926 |
|
| 927 | if (unlikely(!lock->key))
|
| 928 | return NULL;
|
| 929 |
|
| 930 | |
| 931 | * NOTE: the class-key must be unique. For dynamic locks, a static
|
| 932 | * lock_class_key variable is passed in through the mutex_init()
|
| 933 | * (or spin_lock_init()) call - which acts as the key. For static
|
| 934 | * locks we use the lock object itself as the key.
|
| 935 |
|
| 936 | BUILD_BUG_ON(sizeof(struct lock_class_key) >
|
| 937 | sizeof(struct lockdep_map));
|
| 938 |
|
| 939 | key = lock->key->subkeys + subclass;
|
| 940 |
|
| 941 | hash_head = classhashentry(key);
|
| 942 |
|
| 943 | |
| 944 | * We do an RCU walk of the hash, see lockdep_free_key_range().
|
| 945 |
|
| 946 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
|
| 947 | return NULL;
|
| 948 |
|
| 949 | hlist_for_each_entry_rcu_notrace(class, hash_head, hash_entry) {
|
| 950 | if (class->key == key) {
|
| 951 | |
| 952 | * Huh! same key, different name? Did someone trample
|
| 953 | * on some memory? We're most confused.
|
| 954 |
|
| 955 | WARN_ONCE(class->name != lock->name &&
|
| 956 | lock->key != &__lockdep_no_validate__,
|
| 957 | "Looking for class \"%s\" with key %ps, but found a different class \"%s\" with the same key\n",
|
| 958 | lock->name, lock->key, class->name);
|
| 959 | return class;
|
| 960 | }
|
| 961 | }
|
| 962 |
|
| 963 | return NULL;
|
| 964 | }
|
| 965 |
|
| 966 | static __always_inline bool lock_class_cache_is_valid(const struct lockdep_map *lock,
|
| 967 | const struct lock_class *class,
|
| 968 | unsigned int subclass)
|
| 969 | {
|
| 970 | unsigned int class_subclass;
|
| 971 |
|
| 972 | if (!class)
|
| 973 | return false;
|
| 974 |
|
| 975 | if (unlikely(class < lock_classes || class >= lock_classes + MAX_LOCKDEP_KEYS))
|
| 976 | return false;
|
| 977 |
|
| 978 | if (unlikely(!arch_test_bit(class - lock_classes, lock_classes_in_use)))
|
| 979 | return false;
|
| 980 |
|
| 981 | if (unlikely(!lock->key))
|
| 982 | return false;
|
| 983 |
|
| 984 | class_subclass = subclass ? subclass : class->subclass;
|
| 985 | if (unlikely(class_subclass >= MAX_LOCKDEP_SUBCLASSES))
|
| 986 | return false;
|
| 987 |
|
| 988 | if (unlikely(READ_ONCE(class->key) != lock->key->subkeys + class_subclass))
|
| 989 | return false;
|
| 990 |
|
| 991 | return true;
|
| 992 | }
|
| 993 |
|
| 994 | |
| 995 | * Static locks do not have their class-keys yet - for them the key is
|
| 996 | * the lock object itself. If the lock is in the per cpu area, the
|
| 997 | * canonical address of the lock (per cpu offset removed) is used.
|
| 998 |
|
| 999 | static bool assign_lock_key(struct lockdep_map *lock)
|
| 1000 | {
|
| 1001 | unsigned long can_addr, addr = (unsigned long)lock;
|
| 1002 |
|
| 1003 | #ifdef __KERNEL__
|
| 1004 | |
| 1005 | * lockdep_free_key_range() assumes that struct lock_class_key
|
| 1006 | * objects do not overlap. Since we use the address of lock
|
| 1007 | * objects as class key for static objects, check whether the
|
| 1008 | * size of lock_class_key objects does not exceed the size of
|
| 1009 | * the smallest lock object.
|
| 1010 |
|
| 1011 | BUILD_BUG_ON(sizeof(struct lock_class_key) > sizeof(raw_spinlock_t));
|
| 1012 | #endif
|
| 1013 |
|
| 1014 | if (__is_kernel_percpu_address(addr, &can_addr))
|
| 1015 | lock->key = (void *)can_addr;
|
| 1016 | else if (__is_module_percpu_address(addr, &can_addr))
|
| 1017 | lock->key = (void *)can_addr;
|
| 1018 | else if (static_obj(lock))
|
| 1019 | lock->key = (void *)lock;
|
| 1020 | else {
|
| 1021 |
|
| 1022 | debug_locks_off();
|
| 1023 | nbcon_cpu_emergency_enter();
|
| 1024 | pr_err("INFO: trying to register non-static key.\n");
|
| 1025 | pr_err("The code is fine but needs lockdep annotation, or maybe\n");
|
| 1026 | pr_err("you didn't initialize this object before use?\n");
|
| 1027 | pr_err("turning off the locking correctness validator.\n");
|
| 1028 | dump_stack();
|
| 1029 | nbcon_cpu_emergency_exit();
|
| 1030 | return false;
|
| 1031 | }
|
| 1032 |
|
| 1033 | return true;
|
| 1034 | }
|
| 1035 |
|
| 1036 | #ifdef CONFIG_DEBUG_LOCKDEP
|
| 1037 |
|
| 1038 |
|
| 1039 | static bool in_list(struct list_head *e, struct list_head *h)
|
| 1040 | {
|
| 1041 | struct list_head *f;
|
| 1042 |
|
| 1043 | list_for_each(f, h) {
|
| 1044 | if (e == f)
|
| 1045 | return true;
|
| 1046 | }
|
| 1047 |
|
| 1048 | return false;
|
| 1049 | }
|
| 1050 |
|
| 1051 | |
| 1052 | * Check whether entry @e occurs in any of the locks_after or locks_before
|
| 1053 | * lists.
|
| 1054 |
|
| 1055 | static bool in_any_class_list(struct list_head *e)
|
| 1056 | {
|
| 1057 | struct lock_class *class;
|
| 1058 | int i;
|
| 1059 |
|
| 1060 | for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
|
| 1061 | class = &lock_classes[i];
|
| 1062 | if (in_list(e, &class->locks_after) ||
|
| 1063 | in_list(e, &class->locks_before))
|
| 1064 | return true;
|
| 1065 | }
|
| 1066 | return false;
|
| 1067 | }
|
| 1068 |
|
| 1069 | static bool class_lock_list_valid(struct lock_class *c, struct list_head *h)
|
| 1070 | {
|
| 1071 | struct lock_list *e;
|
| 1072 |
|
| 1073 | list_for_each_entry(e, h, entry) {
|
| 1074 | if (e->links_to != c) {
|
| 1075 | printk(KERN_INFO "class %s: mismatch for lock entry %ld; class %s <> %s",
|
| 1076 | c->name ? : "(?)",
|
| 1077 | (unsigned long)(e - list_entries),
|
| 1078 | e->links_to && e->links_to->name ?
|
| 1079 | e->links_to->name : "(?)",
|
| 1080 | e->class && e->class->name ? e->class->name :
|
| 1081 | "(?)");
|
| 1082 | return false;
|
| 1083 | }
|
| 1084 | }
|
| 1085 | return true;
|
| 1086 | }
|
| 1087 |
|
| 1088 | #ifdef CONFIG_PROVE_LOCKING
|
| 1089 | static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
|
| 1090 | #endif
|
| 1091 |
|
| 1092 | static bool check_lock_chain_key(struct lock_chain *chain)
|
| 1093 | {
|
| 1094 | #ifdef CONFIG_PROVE_LOCKING
|
| 1095 | u64 chain_key = INITIAL_CHAIN_KEY;
|
| 1096 | int i;
|
| 1097 |
|
| 1098 | for (i = chain->base; i < chain->base + chain->depth; i++)
|
| 1099 | chain_key = iterate_chain_key(chain_key, chain_hlocks[i]);
|
| 1100 | |
| 1101 | * The 'unsigned long long' casts avoid that a compiler warning
|
| 1102 | * is reported when building tools/lib/lockdep.
|
| 1103 |
|
| 1104 | if (chain->chain_key != chain_key) {
|
| 1105 | printk(KERN_INFO "chain %lld: key %#llx <> %#llx\n",
|
| 1106 | (unsigned long long)(chain - lock_chains),
|
| 1107 | (unsigned long long)chain->chain_key,
|
| 1108 | (unsigned long long)chain_key);
|
| 1109 | return false;
|
| 1110 | }
|
| 1111 | #endif
|
| 1112 | return true;
|
| 1113 | }
|
| 1114 |
|
| 1115 | static bool in_any_zapped_class_list(struct lock_class *class)
|
| 1116 | {
|
| 1117 | struct pending_free *pf;
|
| 1118 | int i;
|
| 1119 |
|
| 1120 | for (i = 0, pf = delayed_free.pf; i < ARRAY_SIZE(delayed_free.pf); i++, pf++) {
|
| 1121 | if (in_list(&class->lock_entry, &pf->zapped))
|
| 1122 | return true;
|
| 1123 | }
|
| 1124 |
|
| 1125 | return false;
|
| 1126 | }
|
| 1127 |
|
| 1128 | static bool __check_data_structures(void)
|
| 1129 | {
|
| 1130 | struct lock_class *class;
|
| 1131 | struct lock_chain *chain;
|
| 1132 | struct hlist_head *head;
|
| 1133 | struct lock_list *e;
|
| 1134 | int i;
|
| 1135 |
|
| 1136 |
|
| 1137 | for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
|
| 1138 | class = &lock_classes[i];
|
| 1139 | if (!in_list(&class->lock_entry, &all_lock_classes) &&
|
| 1140 | !in_list(&class->lock_entry, &free_lock_classes) &&
|
| 1141 | !in_any_zapped_class_list(class)) {
|
| 1142 | printk(KERN_INFO "class %px/%s is not in any class list\n",
|
| 1143 | class, class->name ? : "(?)");
|
| 1144 | return false;
|
| 1145 | }
|
| 1146 | }
|
| 1147 |
|
| 1148 |
|
| 1149 | for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
|
| 1150 | class = &lock_classes[i];
|
| 1151 | if (!class_lock_list_valid(class, &class->locks_before))
|
| 1152 | return false;
|
| 1153 | if (!class_lock_list_valid(class, &class->locks_after))
|
| 1154 | return false;
|
| 1155 | }
|
| 1156 |
|
| 1157 |
|
| 1158 | for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
|
| 1159 | head = chainhash_table + i;
|
| 1160 | hlist_for_each_entry_rcu(chain, head, entry) {
|
| 1161 | if (!check_lock_chain_key(chain))
|
| 1162 | return false;
|
| 1163 | }
|
| 1164 | }
|
| 1165 |
|
| 1166 | |
| 1167 | * Check whether all list entries that are in use occur in a class
|
| 1168 | * lock list.
|
| 1169 |
|
| 1170 | for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
|
| 1171 | e = list_entries + i;
|
| 1172 | if (!in_any_class_list(&e->entry)) {
|
| 1173 | printk(KERN_INFO "list entry %d is not in any class list; class %s <> %s\n",
|
| 1174 | (unsigned int)(e - list_entries),
|
| 1175 | e->class->name ? : "(?)",
|
| 1176 | e->links_to->name ? : "(?)");
|
| 1177 | return false;
|
| 1178 | }
|
| 1179 | }
|
| 1180 |
|
| 1181 | |
| 1182 | * Check whether all list entries that are not in use do not occur in
|
| 1183 | * a class lock list.
|
| 1184 |
|
| 1185 | for_each_clear_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
|
| 1186 | e = list_entries + i;
|
| 1187 | if (in_any_class_list(&e->entry)) {
|
| 1188 | printk(KERN_INFO "list entry %d occurs in a class list; class %s <> %s\n",
|
| 1189 | (unsigned int)(e - list_entries),
|
| 1190 | e->class && e->class->name ? e->class->name :
|
| 1191 | "(?)",
|
| 1192 | e->links_to && e->links_to->name ?
|
| 1193 | e->links_to->name : "(?)");
|
| 1194 | return false;
|
| 1195 | }
|
| 1196 | }
|
| 1197 |
|
| 1198 | return true;
|
| 1199 | }
|
| 1200 |
|
| 1201 | int check_consistency = 0;
|
| 1202 | module_param(check_consistency, int, 0644);
|
| 1203 |
|
| 1204 | static void check_data_structures(void)
|
| 1205 | {
|
| 1206 | static bool once = false;
|
| 1207 |
|
| 1208 | if (check_consistency && !once) {
|
| 1209 | if (!__check_data_structures()) {
|
| 1210 | once = true;
|
| 1211 | WARN_ON(once);
|
| 1212 | }
|
| 1213 | }
|
| 1214 | }
|
| 1215 |
|
| 1216 | #else
|
| 1217 |
|
| 1218 | static inline void check_data_structures(void) { }
|
| 1219 |
|
| 1220 | #endif
|
| 1221 |
|
| 1222 | static void init_chain_block_buckets(void);
|
| 1223 |
|
| 1224 | |
| 1225 | * Initialize the lock_classes[] array elements, the free_lock_classes list
|
| 1226 | * and also the delayed_free structure.
|
| 1227 |
|
| 1228 | static void init_data_structures_once(void)
|
| 1229 | {
|
| 1230 | static bool __read_mostly ds_initialized, rcu_head_initialized;
|
| 1231 | int i;
|
| 1232 |
|
| 1233 | if (likely(rcu_head_initialized))
|
| 1234 | return;
|
| 1235 |
|
| 1236 | if (system_state >= SYSTEM_SCHEDULING) {
|
| 1237 | init_rcu_head(&delayed_free.rcu_head);
|
| 1238 | rcu_head_initialized = true;
|
| 1239 | }
|
| 1240 |
|
| 1241 | if (ds_initialized)
|
| 1242 | return;
|
| 1243 |
|
| 1244 | ds_initialized = true;
|
| 1245 |
|
| 1246 | INIT_LIST_HEAD(&delayed_free.pf[0].zapped);
|
| 1247 | INIT_LIST_HEAD(&delayed_free.pf[1].zapped);
|
| 1248 |
|
| 1249 | for (i = 0; i < ARRAY_SIZE(lock_classes); i++) {
|
| 1250 | list_add_tail(&lock_classes[i].lock_entry, &free_lock_classes);
|
| 1251 | INIT_LIST_HEAD(&lock_classes[i].locks_after);
|
| 1252 | INIT_LIST_HEAD(&lock_classes[i].locks_before);
|
| 1253 | }
|
| 1254 | init_chain_block_buckets();
|
| 1255 | }
|
| 1256 |
|
| 1257 | static inline struct hlist_head *keyhashentry(const struct lock_class_key *key)
|
| 1258 | {
|
| 1259 | unsigned long hash = hash_long((uintptr_t)key, KEYHASH_BITS);
|
| 1260 |
|
| 1261 | return lock_keys_hash + hash;
|
| 1262 | }
|
| 1263 |
|
| 1264 |
|
| 1265 | void lockdep_register_key(struct lock_class_key *key)
|
| 1266 | {
|
| 1267 | struct hlist_head *hash_head;
|
| 1268 | struct lock_class_key *k;
|
| 1269 | unsigned long flags;
|
| 1270 |
|
| 1271 | if (WARN_ON_ONCE(static_obj(key)))
|
| 1272 | return;
|
| 1273 | hash_head = keyhashentry(key);
|
| 1274 |
|
| 1275 | raw_local_irq_save(flags);
|
| 1276 | if (!graph_lock())
|
| 1277 | goto restore_irqs;
|
| 1278 | hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
|
| 1279 | if (WARN_ON_ONCE(k == key))
|
| 1280 | goto out_unlock;
|
| 1281 | }
|
| 1282 | hlist_add_head_rcu(&key->hash_entry, hash_head);
|
| 1283 | nr_dynamic_keys++;
|
| 1284 | out_unlock:
|
| 1285 | graph_unlock();
|
| 1286 | restore_irqs:
|
| 1287 | raw_local_irq_restore(flags);
|
| 1288 | }
|
| 1289 | EXPORT_SYMBOL_GPL(lockdep_register_key);
|
| 1290 |
|
| 1291 |
|
| 1292 | static bool is_dynamic_key(const struct lock_class_key *key)
|
| 1293 | {
|
| 1294 | struct hlist_head *hash_head;
|
| 1295 | struct lock_class_key *k;
|
| 1296 | bool found = false;
|
| 1297 |
|
| 1298 | if (WARN_ON_ONCE(static_obj(key)))
|
| 1299 | return false;
|
| 1300 |
|
| 1301 | |
| 1302 | * If lock debugging is disabled lock_keys_hash[] may contain
|
| 1303 | * pointers to memory that has already been freed. Avoid triggering
|
| 1304 | * a use-after-free in that case by returning early.
|
| 1305 |
|
| 1306 | if (!debug_locks)
|
| 1307 | return true;
|
| 1308 |
|
| 1309 | hash_head = keyhashentry(key);
|
| 1310 |
|
| 1311 | rcu_read_lock();
|
| 1312 | hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
|
| 1313 | if (k == key) {
|
| 1314 | found = true;
|
| 1315 | break;
|
| 1316 | }
|
| 1317 | }
|
| 1318 | rcu_read_unlock();
|
| 1319 |
|
| 1320 | return found;
|
| 1321 | }
|
| 1322 |
|
| 1323 | |
| 1324 | * Register a lock's class in the hash-table, if the class is not present
|
| 1325 | * yet. Otherwise we look it up. We cache the result in the lock object
|
| 1326 | * itself, so actual lookup of the hash should be once per lock object.
|
| 1327 |
|
| 1328 | static struct lock_class *
|
| 1329 | register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
|
| 1330 | {
|
| 1331 | struct lockdep_subclass_key *key;
|
| 1332 | struct hlist_head *hash_head;
|
| 1333 | struct lock_class *class;
|
| 1334 | int idx;
|
| 1335 |
|
| 1336 | DEBUG_LOCKS_WARN_ON(!irqs_disabled());
|
| 1337 |
|
| 1338 | class = look_up_lock_class(lock, subclass);
|
| 1339 | if (likely(class))
|
| 1340 | goto out_set_class_cache;
|
| 1341 |
|
| 1342 | if (!lock->key) {
|
| 1343 | if (!assign_lock_key(lock))
|
| 1344 | return NULL;
|
| 1345 | } else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) {
|
| 1346 | return NULL;
|
| 1347 | }
|
| 1348 |
|
| 1349 | key = lock->key->subkeys + subclass;
|
| 1350 | hash_head = classhashentry(key);
|
| 1351 |
|
| 1352 | if (!graph_lock()) {
|
| 1353 | return NULL;
|
| 1354 | }
|
| 1355 | |
| 1356 | * We have to do the hash-walk again, to avoid races
|
| 1357 | * with another CPU:
|
| 1358 |
|
| 1359 | hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
|
| 1360 | if (class->key == key)
|
| 1361 | goto out_unlock_set;
|
| 1362 | }
|
| 1363 |
|
| 1364 | init_data_structures_once();
|
| 1365 |
|
| 1366 |
|
| 1367 | class = list_first_entry_or_null(&free_lock_classes, typeof(*class),
|
| 1368 | lock_entry);
|
| 1369 | if (!class) {
|
| 1370 | if (!debug_locks_off_graph_unlock()) {
|
| 1371 | return NULL;
|
| 1372 | }
|
| 1373 |
|
| 1374 | nbcon_cpu_emergency_enter();
|
| 1375 | print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
|
| 1376 | dump_stack();
|
| 1377 | nbcon_cpu_emergency_exit();
|
| 1378 | return NULL;
|
| 1379 | }
|
| 1380 | nr_lock_classes++;
|
| 1381 | __set_bit(class - lock_classes, lock_classes_in_use);
|
| 1382 | debug_atomic_inc(nr_unused_locks);
|
| 1383 | class->key = key;
|
| 1384 | class->name = lock->name;
|
| 1385 | class->subclass = subclass;
|
| 1386 | WARN_ON_ONCE(!list_empty(&class->locks_before));
|
| 1387 | WARN_ON_ONCE(!list_empty(&class->locks_after));
|
| 1388 | class->name_version = count_matching_names(class);
|
| 1389 | class->wait_type_inner = lock->wait_type_inner;
|
| 1390 | class->wait_type_outer = lock->wait_type_outer;
|
| 1391 | class->lock_type = lock->lock_type;
|
| 1392 | |
| 1393 | * We use RCU's safe list-add method to make
|
| 1394 | * parallel walking of the hash-list safe:
|
| 1395 |
|
| 1396 | hlist_add_head_rcu(&class->hash_entry, hash_head);
|
| 1397 | |
| 1398 | * Remove the class from the free list and add it to the global list
|
| 1399 | * of classes.
|
| 1400 |
|
| 1401 | list_move_tail(&class->lock_entry, &all_lock_classes);
|
| 1402 | idx = class - lock_classes;
|
| 1403 | if (idx > max_lock_class_idx)
|
| 1404 | max_lock_class_idx = idx;
|
| 1405 |
|
| 1406 | if (verbose(class)) {
|
| 1407 | graph_unlock();
|
| 1408 |
|
| 1409 | nbcon_cpu_emergency_enter();
|
| 1410 | printk("\nnew class %px: %s", class->key, class->name);
|
| 1411 | if (class->name_version > 1)
|
| 1412 | printk(KERN_CONT "#%d", class->name_version);
|
| 1413 | printk(KERN_CONT "\n");
|
| 1414 | dump_stack();
|
| 1415 | nbcon_cpu_emergency_exit();
|
| 1416 |
|
| 1417 | if (!graph_lock()) {
|
| 1418 | return NULL;
|
| 1419 | }
|
| 1420 | }
|
| 1421 | out_unlock_set:
|
| 1422 | graph_unlock();
|
| 1423 |
|
| 1424 | out_set_class_cache:
|
| 1425 | if (!subclass || force)
|
| 1426 | WRITE_ONCE(lock->class_cache[0], class);
|
| 1427 | else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
|
| 1428 | WRITE_ONCE(lock->class_cache[subclass], class);
|
| 1429 |
|
| 1430 | |
| 1431 | * Hash collision, did we smoke some? We found a class with a matching
|
| 1432 | * hash but the subclass -- which is hashed in -- didn't match.
|
| 1433 |
|
| 1434 | if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
|
| 1435 | return NULL;
|
| 1436 |
|
| 1437 | return class;
|
| 1438 | }
|
| 1439 |
|
| 1440 | #ifdef CONFIG_PROVE_LOCKING
|
| 1441 | |
| 1442 | * Allocate a lockdep entry. (assumes the graph_lock held, returns
|
| 1443 | * with NULL on failure)
|
| 1444 |
|
| 1445 | static struct lock_list *alloc_list_entry(void)
|
| 1446 | {
|
| 1447 | int idx = find_first_zero_bit(list_entries_in_use,
|
| 1448 | ARRAY_SIZE(list_entries));
|
| 1449 |
|
| 1450 | if (idx >= ARRAY_SIZE(list_entries)) {
|
| 1451 | if (!debug_locks_off_graph_unlock())
|
| 1452 | return NULL;
|
| 1453 |
|
| 1454 | nbcon_cpu_emergency_enter();
|
| 1455 | print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
|
| 1456 | dump_stack();
|
| 1457 | nbcon_cpu_emergency_exit();
|
| 1458 | return NULL;
|
| 1459 | }
|
| 1460 | nr_list_entries++;
|
| 1461 | __set_bit(idx, list_entries_in_use);
|
| 1462 | return list_entries + idx;
|
| 1463 | }
|
| 1464 |
|
| 1465 | |
| 1466 | * Add a new dependency to the head of the list:
|
| 1467 |
|
| 1468 | static int add_lock_to_list(struct lock_class *this,
|
| 1469 | struct lock_class *links_to, struct list_head *head,
|
| 1470 | u16 distance, u8 dep,
|
| 1471 | const struct lock_trace *trace)
|
| 1472 | {
|
| 1473 | struct lock_list *entry;
|
| 1474 | |
| 1475 | * Lock not present yet - get a new dependency struct and
|
| 1476 | * add it to the list:
|
| 1477 |
|
| 1478 | entry = alloc_list_entry();
|
| 1479 | if (!entry)
|
| 1480 | return 0;
|
| 1481 |
|
| 1482 | entry->class = this;
|
| 1483 | entry->links_to = links_to;
|
| 1484 | entry->dep = dep;
|
| 1485 | entry->distance = distance;
|
| 1486 | entry->trace = trace;
|
| 1487 | |
| 1488 | * Both allocation and removal are done under the graph lock; but
|
| 1489 | * iteration is under RCU-sched; see look_up_lock_class() and
|
| 1490 | * lockdep_free_key_range().
|
| 1491 |
|
| 1492 | list_add_tail_rcu(&entry->entry, head);
|
| 1493 |
|
| 1494 | return 1;
|
| 1495 | }
|
| 1496 |
|
| 1497 | |
| 1498 | * For good efficiency of modular, we use power of 2
|
| 1499 |
|
| 1500 | #define MAX_CIRCULAR_QUEUE_SIZE (1UL << CONFIG_LOCKDEP_CIRCULAR_QUEUE_BITS)
|
| 1501 | #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
|
| 1502 |
|
| 1503 | |
| 1504 | * The circular_queue and helpers are used to implement graph
|
| 1505 | * breadth-first search (BFS) algorithm, by which we can determine
|
| 1506 | * whether there is a path from a lock to another. In deadlock checks,
|
| 1507 | * a path from the next lock to be acquired to a previous held lock
|
| 1508 | * indicates that adding the <prev> -> <next> lock dependency will
|
| 1509 | * produce a circle in the graph. Breadth-first search instead of
|
| 1510 | * depth-first search is used in order to find the shortest (circular)
|
| 1511 | * path.
|
| 1512 |
|
| 1513 | struct circular_queue {
|
| 1514 | struct lock_list *element[MAX_CIRCULAR_QUEUE_SIZE];
|
| 1515 | unsigned int front, rear;
|
| 1516 | };
|
| 1517 |
|
| 1518 | static struct circular_queue lock_cq;
|
| 1519 |
|
| 1520 | unsigned int max_bfs_queue_depth;
|
| 1521 |
|
| 1522 | static unsigned int lockdep_dependency_gen_id;
|
| 1523 |
|
| 1524 | static inline void __cq_init(struct circular_queue *cq)
|
| 1525 | {
|
| 1526 | cq->front = cq->rear = 0;
|
| 1527 | lockdep_dependency_gen_id++;
|
| 1528 | }
|
| 1529 |
|
| 1530 | static inline int __cq_empty(struct circular_queue *cq)
|
| 1531 | {
|
| 1532 | return (cq->front == cq->rear);
|
| 1533 | }
|
| 1534 |
|
| 1535 | static inline int __cq_full(struct circular_queue *cq)
|
| 1536 | {
|
| 1537 | return ((cq->rear + 1) & CQ_MASK) == cq->front;
|
| 1538 | }
|
| 1539 |
|
| 1540 | static inline int __cq_enqueue(struct circular_queue *cq, struct lock_list *elem)
|
| 1541 | {
|
| 1542 | if (__cq_full(cq))
|
| 1543 | return -1;
|
| 1544 |
|
| 1545 | cq->element[cq->rear] = elem;
|
| 1546 | cq->rear = (cq->rear + 1) & CQ_MASK;
|
| 1547 | return 0;
|
| 1548 | }
|
| 1549 |
|
| 1550 | |
| 1551 | * Dequeue an element from the circular_queue, return a lock_list if
|
| 1552 | * the queue is not empty, or NULL if otherwise.
|
| 1553 |
|
| 1554 | static inline struct lock_list * __cq_dequeue(struct circular_queue *cq)
|
| 1555 | {
|
| 1556 | struct lock_list * lock;
|
| 1557 |
|
| 1558 | if (__cq_empty(cq))
|
| 1559 | return NULL;
|
| 1560 |
|
| 1561 | lock = cq->element[cq->front];
|
| 1562 | cq->front = (cq->front + 1) & CQ_MASK;
|
| 1563 |
|
| 1564 | return lock;
|
| 1565 | }
|
| 1566 |
|
| 1567 | static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
|
| 1568 | {
|
| 1569 | return (cq->rear - cq->front) & CQ_MASK;
|
| 1570 | }
|
| 1571 |
|
| 1572 | static inline void mark_lock_accessed(struct lock_list *lock)
|
| 1573 | {
|
| 1574 | lock->class->dep_gen_id = lockdep_dependency_gen_id;
|
| 1575 | }
|
| 1576 |
|
| 1577 | static inline void visit_lock_entry(struct lock_list *lock,
|
| 1578 | struct lock_list *parent)
|
| 1579 | {
|
| 1580 | lock->parent = parent;
|
| 1581 | }
|
| 1582 |
|
| 1583 | static inline unsigned long lock_accessed(struct lock_list *lock)
|
| 1584 | {
|
| 1585 | return lock->class->dep_gen_id == lockdep_dependency_gen_id;
|
| 1586 | }
|
| 1587 |
|
| 1588 | static inline struct lock_list *get_lock_parent(struct lock_list *child)
|
| 1589 | {
|
| 1590 | return child->parent;
|
| 1591 | }
|
| 1592 |
|
| 1593 | static inline int get_lock_depth(struct lock_list *child)
|
| 1594 | {
|
| 1595 | int depth = 0;
|
| 1596 | struct lock_list *parent;
|
| 1597 |
|
| 1598 | while ((parent = get_lock_parent(child))) {
|
| 1599 | child = parent;
|
| 1600 | depth++;
|
| 1601 | }
|
| 1602 | return depth;
|
| 1603 | }
|
| 1604 |
|
| 1605 | |
| 1606 | * Return the forward or backward dependency list.
|
| 1607 | *
|
| 1608 | * @lock: the lock_list to get its class's dependency list
|
| 1609 | * @offset: the offset to struct lock_class to determine whether it is
|
| 1610 | * locks_after or locks_before
|
| 1611 |
|
| 1612 | static inline struct list_head *get_dep_list(struct lock_list *lock, int offset)
|
| 1613 | {
|
| 1614 | void *lock_class = lock->class;
|
| 1615 |
|
| 1616 | return lock_class + offset;
|
| 1617 | }
|
| 1618 | |
| 1619 | * Return values of a bfs search:
|
| 1620 | *
|
| 1621 | * BFS_E* indicates an error
|
| 1622 | * BFS_R* indicates a result (match or not)
|
| 1623 | *
|
| 1624 | * BFS_EINVALIDNODE: Find a invalid node in the graph.
|
| 1625 | *
|
| 1626 | * BFS_EQUEUEFULL: The queue is full while doing the bfs.
|
| 1627 | *
|
| 1628 | * BFS_RMATCH: Find the matched node in the graph, and put that node into
|
| 1629 | * *@target_entry.
|
| 1630 | *
|
| 1631 | * BFS_RNOMATCH: Haven't found the matched node and keep *@target_entry
|
| 1632 | * _unchanged_.
|
| 1633 |
|
| 1634 | enum bfs_result {
|
| 1635 | BFS_EINVALIDNODE = -2,
|
| 1636 | BFS_EQUEUEFULL = -1,
|
| 1637 | BFS_RMATCH = 0,
|
| 1638 | BFS_RNOMATCH = 1,
|
| 1639 | };
|
| 1640 |
|
| 1641 | |
| 1642 | * bfs_result < 0 means error
|
| 1643 |
|
| 1644 | static inline bool bfs_error(enum bfs_result res)
|
| 1645 | {
|
| 1646 | return res < 0;
|
| 1647 | }
|
| 1648 |
|
| 1649 | |
| 1650 | * DEP_*_BIT in lock_list::dep
|
| 1651 | *
|
| 1652 | * For dependency @prev -> @next:
|
| 1653 | *
|
| 1654 | * SR: @prev is shared reader (->read != 0) and @next is recursive reader
|
| 1655 | * (->read == 2)
|
| 1656 | * ER: @prev is exclusive locker (->read == 0) and @next is recursive reader
|
| 1657 | * SN: @prev is shared reader and @next is non-recursive locker (->read != 2)
|
| 1658 | * EN: @prev is exclusive locker and @next is non-recursive locker
|
| 1659 | *
|
| 1660 | * Note that we define the value of DEP_*_BITs so that:
|
| 1661 | * bit0 is prev->read == 0
|
| 1662 | * bit1 is next->read != 2
|
| 1663 |
|
| 1664 | #define DEP_SR_BIT (0 + (0 << 1))
|
| 1665 | #define DEP_ER_BIT (1 + (0 << 1))
|
| 1666 | #define DEP_SN_BIT (0 + (1 << 1))
|
| 1667 | #define DEP_EN_BIT (1 + (1 << 1))
|
| 1668 |
|
| 1669 | #define DEP_SR_MASK (1U << (DEP_SR_BIT))
|
| 1670 | #define DEP_ER_MASK (1U << (DEP_ER_BIT))
|
| 1671 | #define DEP_SN_MASK (1U << (DEP_SN_BIT))
|
| 1672 | #define DEP_EN_MASK (1U << (DEP_EN_BIT))
|
| 1673 |
|
| 1674 | static inline unsigned int
|
| 1675 | __calc_dep_bit(struct held_lock *prev, struct held_lock *next)
|
| 1676 | {
|
| 1677 | return (prev->read == 0) + ((next->read != 2) << 1);
|
| 1678 | }
|
| 1679 |
|
| 1680 | static inline u8 calc_dep(struct held_lock *prev, struct held_lock *next)
|
| 1681 | {
|
| 1682 | return 1U << __calc_dep_bit(prev, next);
|
| 1683 | }
|
| 1684 |
|
| 1685 | |
| 1686 | * calculate the dep_bit for backwards edges. We care about whether @prev is
|
| 1687 | * shared and whether @next is recursive.
|
| 1688 |
|
| 1689 | static inline unsigned int
|
| 1690 | __calc_dep_bitb(struct held_lock *prev, struct held_lock *next)
|
| 1691 | {
|
| 1692 | return (next->read != 2) + ((prev->read == 0) << 1);
|
| 1693 | }
|
| 1694 |
|
| 1695 | static inline u8 calc_depb(struct held_lock *prev, struct held_lock *next)
|
| 1696 | {
|
| 1697 | return 1U << __calc_dep_bitb(prev, next);
|
| 1698 | }
|
| 1699 |
|
| 1700 | |
| 1701 | * Initialize a lock_list entry @lock belonging to @class as the root for a BFS
|
| 1702 | * search.
|
| 1703 |
|
| 1704 | static inline void __bfs_init_root(struct lock_list *lock,
|
| 1705 | struct lock_class *class)
|
| 1706 | {
|
| 1707 | lock->class = class;
|
| 1708 | lock->parent = NULL;
|
| 1709 | lock->only_xr = 0;
|
| 1710 | }
|
| 1711 |
|
| 1712 | |
| 1713 | * Initialize a lock_list entry @lock based on a lock acquisition @hlock as the
|
| 1714 | * root for a BFS search.
|
| 1715 | *
|
| 1716 | * ->only_xr of the initial lock node is set to @hlock->read == 2, to make sure
|
| 1717 | * that <prev> -> @hlock and @hlock -> <whatever __bfs() found> is not -(*R)->
|
| 1718 | * and -(S*)->.
|
| 1719 |
|
| 1720 | static inline void bfs_init_root(struct lock_list *lock,
|
| 1721 | struct held_lock *hlock)
|
| 1722 | {
|
| 1723 | __bfs_init_root(lock, hlock_class(hlock));
|
| 1724 | lock->only_xr = (hlock->read == 2);
|
| 1725 | }
|
| 1726 |
|
| 1727 | |
| 1728 | * Similar to bfs_init_root() but initialize the root for backwards BFS.
|
| 1729 | *
|
| 1730 | * ->only_xr of the initial lock node is set to @hlock->read != 0, to make sure
|
| 1731 | * that <next> -> @hlock and @hlock -> <whatever backwards BFS found> is not
|
| 1732 | * -(*S)-> and -(R*)-> (reverse order of -(*R)-> and -(S*)->).
|
| 1733 |
|
| 1734 | static inline void bfs_init_rootb(struct lock_list *lock,
|
| 1735 | struct held_lock *hlock)
|
| 1736 | {
|
| 1737 | __bfs_init_root(lock, hlock_class(hlock));
|
| 1738 | lock->only_xr = (hlock->read != 0);
|
| 1739 | }
|
| 1740 |
|
| 1741 | static inline struct lock_list *__bfs_next(struct lock_list *lock, int offset)
|
| 1742 | {
|
| 1743 | if (!lock || !lock->parent)
|
| 1744 | return NULL;
|
| 1745 |
|
| 1746 | return list_next_or_null_rcu(get_dep_list(lock->parent, offset),
|
| 1747 | &lock->entry, struct lock_list, entry);
|
| 1748 | }
|
| 1749 |
|
| 1750 | |
| 1751 | * Breadth-First Search to find a strong path in the dependency graph.
|
| 1752 | *
|
| 1753 | * @source_entry: the source of the path we are searching for.
|
| 1754 | * @data: data used for the second parameter of @match function
|
| 1755 | * @match: match function for the search
|
| 1756 | * @target_entry: pointer to the target of a matched path
|
| 1757 | * @offset: the offset to struct lock_class to determine whether it is
|
| 1758 | * locks_after or locks_before
|
| 1759 | *
|
| 1760 | * We may have multiple edges (considering different kinds of dependencies,
|
| 1761 | * e.g. ER and SN) between two nodes in the dependency graph. But
|
| 1762 | * only the strong dependency path in the graph is relevant to deadlocks. A
|
| 1763 | * strong dependency path is a dependency path that doesn't have two adjacent
|
| 1764 | * dependencies as -(*R)-> -(S*)->, please see:
|
| 1765 | *
|
| 1766 | * Documentation/locking/lockdep-design.rst
|
| 1767 | *
|
| 1768 | * for more explanation of the definition of strong dependency paths
|
| 1769 | *
|
| 1770 | * In __bfs(), we only traverse in the strong dependency path:
|
| 1771 | *
|
| 1772 | * In lock_list::only_xr, we record whether the previous dependency only
|
| 1773 | * has -(*R)-> in the search, and if it does (prev only has -(*R)->), we
|
| 1774 | * filter out any -(S*)-> in the current dependency and after that, the
|
| 1775 | * ->only_xr is set according to whether we only have -(*R)-> left.
|
| 1776 |
|
| 1777 | static enum bfs_result __bfs(struct lock_list *source_entry,
|
| 1778 | void *data,
|
| 1779 | bool (*match)(struct lock_list *entry, void *data),
|
| 1780 | bool (*skip)(struct lock_list *entry, void *data),
|
| 1781 | struct lock_list **target_entry,
|
| 1782 | int offset)
|
| 1783 | {
|
| 1784 | struct circular_queue *cq = &lock_cq;
|
| 1785 | struct lock_list *lock = NULL;
|
| 1786 | struct lock_list *entry;
|
| 1787 | struct list_head *head;
|
| 1788 | unsigned int cq_depth;
|
| 1789 | bool first;
|
| 1790 |
|
| 1791 | lockdep_assert_locked();
|
| 1792 |
|
| 1793 | __cq_init(cq);
|
| 1794 | __cq_enqueue(cq, source_entry);
|
| 1795 |
|
| 1796 | while ((lock = __bfs_next(lock, offset)) || (lock = __cq_dequeue(cq))) {
|
| 1797 | if (!lock->class)
|
| 1798 | return BFS_EINVALIDNODE;
|
| 1799 |
|
| 1800 | |
| 1801 | * Step 1: check whether we already finish on this one.
|
| 1802 | *
|
| 1803 | * If we have visited all the dependencies from this @lock to
|
| 1804 | * others (iow, if we have visited all lock_list entries in
|
| 1805 | * @lock->class->locks_{after,before}) we skip, otherwise go
|
| 1806 | * and visit all the dependencies in the list and mark this
|
| 1807 | * list accessed.
|
| 1808 |
|
| 1809 | if (lock_accessed(lock))
|
| 1810 | continue;
|
| 1811 | else
|
| 1812 | mark_lock_accessed(lock);
|
| 1813 |
|
| 1814 | |
| 1815 | * Step 2: check whether prev dependency and this form a strong
|
| 1816 | * dependency path.
|
| 1817 |
|
| 1818 | if (lock->parent) {
|
| 1819 | u8 dep = lock->dep;
|
| 1820 | bool prev_only_xr = lock->parent->only_xr;
|
| 1821 |
|
| 1822 | |
| 1823 | * Mask out all -(S*)-> if we only have *R in previous
|
| 1824 | * step, because -(*R)-> -(S*)-> don't make up a strong
|
| 1825 | * dependency.
|
| 1826 |
|
| 1827 | if (prev_only_xr)
|
| 1828 | dep &= ~(DEP_SR_MASK | DEP_SN_MASK);
|
| 1829 |
|
| 1830 |
|
| 1831 | if (!dep)
|
| 1832 | continue;
|
| 1833 |
|
| 1834 |
|
| 1835 | lock->only_xr = !(dep & (DEP_SN_MASK | DEP_EN_MASK));
|
| 1836 | }
|
| 1837 |
|
| 1838 | |
| 1839 | * Step 3: we haven't visited this and there is a strong
|
| 1840 | * dependency path to this, so check with @match.
|
| 1841 | * If @skip is provide and returns true, we skip this
|
| 1842 | * lock (and any path this lock is in).
|
| 1843 |
|
| 1844 | if (skip && skip(lock, data))
|
| 1845 | continue;
|
| 1846 |
|
| 1847 | if (match(lock, data)) {
|
| 1848 | *target_entry = lock;
|
| 1849 | return BFS_RMATCH;
|
| 1850 | }
|
| 1851 |
|
| 1852 | |
| 1853 | * Step 4: if not match, expand the path by adding the
|
| 1854 | * forward or backwards dependencies in the search
|
| 1855 | *
|
| 1856 |
|
| 1857 | first = true;
|
| 1858 | head = get_dep_list(lock, offset);
|
| 1859 | list_for_each_entry_rcu(entry, head, entry) {
|
| 1860 | visit_lock_entry(entry, lock);
|
| 1861 |
|
| 1862 | |
| 1863 | * Note we only enqueue the first of the list into the
|
| 1864 | * queue, because we can always find a sibling
|
| 1865 | * dependency from one (see __bfs_next()), as a result
|
| 1866 | * the space of queue is saved.
|
| 1867 |
|
| 1868 | if (!first)
|
| 1869 | continue;
|
| 1870 |
|
| 1871 | first = false;
|
| 1872 |
|
| 1873 | if (__cq_enqueue(cq, entry))
|
| 1874 | return BFS_EQUEUEFULL;
|
| 1875 |
|
| 1876 | cq_depth = __cq_get_elem_count(cq);
|
| 1877 | if (max_bfs_queue_depth < cq_depth)
|
| 1878 | max_bfs_queue_depth = cq_depth;
|
| 1879 | }
|
| 1880 | }
|
| 1881 |
|
| 1882 | return BFS_RNOMATCH;
|
| 1883 | }
|
| 1884 |
|
| 1885 | static inline enum bfs_result
|
| 1886 | __bfs_forwards(struct lock_list *src_entry,
|
| 1887 | void *data,
|
| 1888 | bool (*match)(struct lock_list *entry, void *data),
|
| 1889 | bool (*skip)(struct lock_list *entry, void *data),
|
| 1890 | struct lock_list **target_entry)
|
| 1891 | {
|
| 1892 | return __bfs(src_entry, data, match, skip, target_entry,
|
| 1893 | offsetof(struct lock_class, locks_after));
|
| 1894 |
|
| 1895 | }
|
| 1896 |
|
| 1897 | static inline enum bfs_result
|
| 1898 | __bfs_backwards(struct lock_list *src_entry,
|
| 1899 | void *data,
|
| 1900 | bool (*match)(struct lock_list *entry, void *data),
|
| 1901 | bool (*skip)(struct lock_list *entry, void *data),
|
| 1902 | struct lock_list **target_entry)
|
| 1903 | {
|
| 1904 | return __bfs(src_entry, data, match, skip, target_entry,
|
| 1905 | offsetof(struct lock_class, locks_before));
|
| 1906 |
|
| 1907 | }
|
| 1908 |
|
| 1909 | static void print_lock_trace(const struct lock_trace *trace,
|
| 1910 | unsigned int spaces)
|
| 1911 | {
|
| 1912 | stack_trace_print(trace->entries, trace->nr_entries, spaces);
|
| 1913 | }
|
| 1914 |
|
| 1915 | |
| 1916 | * Print a dependency chain entry (this is only done when a deadlock
|
| 1917 | * has been detected):
|
| 1918 |
|
| 1919 | static noinline void
|
| 1920 | print_circular_bug_entry(struct lock_list *target, int depth)
|
| 1921 | {
|
| 1922 | if (debug_locks_silent)
|
| 1923 | return;
|
| 1924 | printk("\n-> #%u", depth);
|
| 1925 | print_lock_name(NULL, target->class);
|
| 1926 | printk(KERN_CONT ":\n");
|
| 1927 | print_lock_trace(target->trace, 6);
|
| 1928 | }
|
| 1929 |
|
| 1930 | static void
|
| 1931 | print_circular_lock_scenario(struct held_lock *src,
|
| 1932 | struct held_lock *tgt,
|
| 1933 | struct lock_list *prt)
|
| 1934 | {
|
| 1935 | struct lock_class *source = hlock_class(src);
|
| 1936 | struct lock_class *target = hlock_class(tgt);
|
| 1937 | struct lock_class *parent = prt->class;
|
| 1938 | int src_read = src->read;
|
| 1939 | int tgt_read = tgt->read;
|
| 1940 |
|
| 1941 | |
| 1942 | * A direct locking problem where unsafe_class lock is taken
|
| 1943 | * directly by safe_class lock, then all we need to show
|
| 1944 | * is the deadlock scenario, as it is obvious that the
|
| 1945 | * unsafe lock is taken under the safe lock.
|
| 1946 | *
|
| 1947 | * But if there is a chain instead, where the safe lock takes
|
| 1948 | * an intermediate lock (middle_class) where this lock is
|
| 1949 | * not the same as the safe lock, then the lock chain is
|
| 1950 | * used to describe the problem. Otherwise we would need
|
| 1951 | * to show a different CPU case for each link in the chain
|
| 1952 | * from the safe_class lock to the unsafe_class lock.
|
| 1953 |
|
| 1954 | if (parent != source) {
|
| 1955 | printk("Chain exists of:\n ");
|
| 1956 | __print_lock_name(src, source);
|
| 1957 | printk(KERN_CONT " --> ");
|
| 1958 | __print_lock_name(NULL, parent);
|
| 1959 | printk(KERN_CONT " --> ");
|
| 1960 | __print_lock_name(tgt, target);
|
| 1961 | printk(KERN_CONT "\n\n");
|
| 1962 | }
|
| 1963 |
|
| 1964 | printk(" Possible unsafe locking scenario:\n\n");
|
| 1965 | printk(" CPU0 CPU1\n");
|
| 1966 | printk(" ---- ----\n");
|
| 1967 | if (tgt_read != 0)
|
| 1968 | printk(" rlock(");
|
| 1969 | else
|
| 1970 | printk(" lock(");
|
| 1971 | __print_lock_name(tgt, target);
|
| 1972 | printk(KERN_CONT ");\n");
|
| 1973 | printk(" lock(");
|
| 1974 | __print_lock_name(NULL, parent);
|
| 1975 | printk(KERN_CONT ");\n");
|
| 1976 | printk(" lock(");
|
| 1977 | __print_lock_name(tgt, target);
|
| 1978 | printk(KERN_CONT ");\n");
|
| 1979 | if (src_read != 0)
|
| 1980 | printk(" rlock(");
|
| 1981 | else if (src->sync)
|
| 1982 | printk(" sync(");
|
| 1983 | else
|
| 1984 | printk(" lock(");
|
| 1985 | __print_lock_name(src, source);
|
| 1986 | printk(KERN_CONT ");\n");
|
| 1987 | printk("\n *** DEADLOCK ***\n\n");
|
| 1988 | }
|
| 1989 |
|
| 1990 | |
| 1991 | * When a circular dependency is detected, print the
|
| 1992 | * header first:
|
| 1993 |
|
| 1994 | static noinline void
|
| 1995 | print_circular_bug_header(struct lock_list *entry, unsigned int depth,
|
| 1996 | struct held_lock *check_src,
|
| 1997 | struct held_lock *check_tgt)
|
| 1998 | {
|
| 1999 | struct task_struct *curr = current;
|
| 2000 |
|
| 2001 | if (debug_locks_silent)
|
| 2002 | return;
|
| 2003 |
|
| 2004 | pr_warn("\n");
|
| 2005 | pr_warn("======================================================\n");
|
| 2006 | pr_warn("WARNING: possible circular locking dependency detected\n");
|
| 2007 | print_kernel_ident();
|
| 2008 | pr_warn("------------------------------------------------------\n");
|
| 2009 | pr_warn("%s/%d is trying to acquire lock:\n",
|
| 2010 | curr->comm, task_pid_nr(curr));
|
| 2011 | print_lock(check_src);
|
| 2012 |
|
| 2013 | pr_warn("\nbut task is already holding lock:\n");
|
| 2014 |
|
| 2015 | print_lock(check_tgt);
|
| 2016 | pr_warn("\nwhich lock already depends on the new lock.\n\n");
|
| 2017 | pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
|
| 2018 |
|
| 2019 | print_circular_bug_entry(entry, depth);
|
| 2020 | }
|
| 2021 |
|
| 2022 | |
| 2023 | * We are about to add B -> A into the dependency graph, and in __bfs() a
|
| 2024 | * strong dependency path A -> .. -> B is found: hlock_class equals
|
| 2025 | * entry->class.
|
| 2026 | *
|
| 2027 | * We will have a deadlock case (conflict) if A -> .. -> B -> A is a strong
|
| 2028 | * dependency cycle, that means:
|
| 2029 | *
|
| 2030 | * Either
|
| 2031 | *
|
| 2032 | * a) B -> A is -(E*)->
|
| 2033 | *
|
| 2034 | * or
|
| 2035 | *
|
| 2036 | * b) A -> .. -> B is -(*N)-> (i.e. A -> .. -(*N)-> B)
|
| 2037 | *
|
| 2038 | * as then we don't have -(*R)-> -(S*)-> in the cycle.
|
| 2039 |
|
| 2040 | static inline bool hlock_conflict(struct lock_list *entry, void *data)
|
| 2041 | {
|
| 2042 | struct held_lock *hlock = (struct held_lock *)data;
|
| 2043 |
|
| 2044 | return hlock_class(hlock) == entry->class &&
|
| 2045 | (hlock->read == 0 ||
|
| 2046 | !entry->only_xr);
|
| 2047 | }
|
| 2048 |
|
| 2049 | static noinline void print_circular_bug(struct lock_list *this,
|
| 2050 | struct lock_list *target,
|
| 2051 | struct held_lock *check_src,
|
| 2052 | struct held_lock *check_tgt)
|
| 2053 | {
|
| 2054 | struct task_struct *curr = current;
|
| 2055 | struct lock_list *parent;
|
| 2056 | struct lock_list *first_parent;
|
| 2057 | int depth;
|
| 2058 |
|
| 2059 | if (!debug_locks_off_graph_unlock() || debug_locks_silent)
|
| 2060 | return;
|
| 2061 |
|
| 2062 | this->trace = save_trace();
|
| 2063 | if (!this->trace)
|
| 2064 | return;
|
| 2065 |
|
| 2066 | depth = get_lock_depth(target);
|
| 2067 |
|
| 2068 | nbcon_cpu_emergency_enter();
|
| 2069 |
|
| 2070 | print_circular_bug_header(target, depth, check_src, check_tgt);
|
| 2071 |
|
| 2072 | parent = get_lock_parent(target);
|
| 2073 | first_parent = parent;
|
| 2074 |
|
| 2075 | while (parent) {
|
| 2076 | print_circular_bug_entry(parent, --depth);
|
| 2077 | parent = get_lock_parent(parent);
|
| 2078 | }
|
| 2079 |
|
| 2080 | printk("\nother info that might help us debug this:\n\n");
|
| 2081 | print_circular_lock_scenario(check_src, check_tgt,
|
| 2082 | first_parent);
|
| 2083 |
|
| 2084 | lockdep_print_held_locks(curr);
|
| 2085 |
|
| 2086 | printk("\nstack backtrace:\n");
|
| 2087 | dump_stack();
|
| 2088 |
|
| 2089 | nbcon_cpu_emergency_exit();
|
| 2090 | }
|
| 2091 |
|
| 2092 | static noinline void print_bfs_bug(int ret)
|
| 2093 | {
|
| 2094 | if (!debug_locks_off_graph_unlock())
|
| 2095 | return;
|
| 2096 |
|
| 2097 | |
| 2098 | * Breadth-first-search failed, graph got corrupted?
|
| 2099 |
|
| 2100 | if (ret == BFS_EQUEUEFULL)
|
| 2101 | pr_warn("Increase LOCKDEP_CIRCULAR_QUEUE_BITS to avoid this warning:\n");
|
| 2102 |
|
| 2103 | WARN(1, "lockdep bfs error:%d\n", ret);
|
| 2104 | }
|
| 2105 |
|
| 2106 | static bool noop_count(struct lock_list *entry, void *data)
|
| 2107 | {
|
| 2108 | (*(unsigned long *)data)++;
|
| 2109 | return false;
|
| 2110 | }
|
| 2111 |
|
| 2112 | static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
|
| 2113 | {
|
| 2114 | unsigned long count = 0;
|
| 2115 | struct lock_list *target_entry;
|
| 2116 |
|
| 2117 | __bfs_forwards(this, (void *)&count, noop_count, NULL, &target_entry);
|
| 2118 |
|
| 2119 | return count;
|
| 2120 | }
|
| 2121 | unsigned long lockdep_count_forward_deps(struct lock_class *class)
|
| 2122 | {
|
| 2123 | unsigned long ret, flags;
|
| 2124 | struct lock_list this;
|
| 2125 |
|
| 2126 | __bfs_init_root(&this, class);
|
| 2127 |
|
| 2128 | raw_local_irq_save(flags);
|
| 2129 | lockdep_lock();
|
| 2130 | ret = __lockdep_count_forward_deps(&this);
|
| 2131 | lockdep_unlock();
|
| 2132 | raw_local_irq_restore(flags);
|
| 2133 |
|
| 2134 | return ret;
|
| 2135 | }
|
| 2136 |
|
| 2137 | static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
|
| 2138 | {
|
| 2139 | unsigned long count = 0;
|
| 2140 | struct lock_list *target_entry;
|
| 2141 |
|
| 2142 | __bfs_backwards(this, (void *)&count, noop_count, NULL, &target_entry);
|
| 2143 |
|
| 2144 | return count;
|
| 2145 | }
|
| 2146 |
|
| 2147 | unsigned long lockdep_count_backward_deps(struct lock_class *class)
|
| 2148 | {
|
| 2149 | unsigned long ret, flags;
|
| 2150 | struct lock_list this;
|
| 2151 |
|
| 2152 | __bfs_init_root(&this, class);
|
| 2153 |
|
| 2154 | raw_local_irq_save(flags);
|
| 2155 | lockdep_lock();
|
| 2156 | ret = __lockdep_count_backward_deps(&this);
|
| 2157 | lockdep_unlock();
|
| 2158 | raw_local_irq_restore(flags);
|
| 2159 |
|
| 2160 | return ret;
|
| 2161 | }
|
| 2162 |
|
| 2163 | |
| 2164 | * Check that the dependency graph starting at <src> can lead to
|
| 2165 | * <target> or not.
|
| 2166 |
|
| 2167 | static noinline enum bfs_result
|
| 2168 | check_path(struct held_lock *target, struct lock_list *src_entry,
|
| 2169 | bool (*match)(struct lock_list *entry, void *data),
|
| 2170 | bool (*skip)(struct lock_list *entry, void *data),
|
| 2171 | struct lock_list **target_entry)
|
| 2172 | {
|
| 2173 | enum bfs_result ret;
|
| 2174 |
|
| 2175 | ret = __bfs_forwards(src_entry, target, match, skip, target_entry);
|
| 2176 |
|
| 2177 | if (unlikely(bfs_error(ret)))
|
| 2178 | print_bfs_bug(ret);
|
| 2179 |
|
| 2180 | return ret;
|
| 2181 | }
|
| 2182 |
|
| 2183 | static void print_deadlock_bug(struct task_struct *, struct held_lock *, struct held_lock *);
|
| 2184 |
|
| 2185 | |
| 2186 | * Prove that the dependency graph starting at <src> can not
|
| 2187 | * lead to <target>. If it can, there is a circle when adding
|
| 2188 | * <target> -> <src> dependency.
|
| 2189 | *
|
| 2190 | * Print an error and return BFS_RMATCH if it does.
|
| 2191 |
|
| 2192 | static noinline enum bfs_result
|
| 2193 | check_noncircular(struct held_lock *src, struct held_lock *target,
|
| 2194 | struct lock_trace **const trace)
|
| 2195 | {
|
| 2196 | enum bfs_result ret;
|
| 2197 | struct lock_list *target_entry;
|
| 2198 | struct lock_list src_entry;
|
| 2199 |
|
| 2200 | bfs_init_root(&src_entry, src);
|
| 2201 |
|
| 2202 | debug_atomic_inc(nr_cyclic_checks);
|
| 2203 |
|
| 2204 | ret = check_path(target, &src_entry, hlock_conflict, NULL, &target_entry);
|
| 2205 |
|
| 2206 | if (unlikely(ret == BFS_RMATCH)) {
|
| 2207 | if (!*trace) {
|
| 2208 | |
| 2209 | * If save_trace fails here, the printing might
|
| 2210 | * trigger a WARN but because of the !nr_entries it
|
| 2211 | * should not do bad things.
|
| 2212 |
|
| 2213 | *trace = save_trace();
|
| 2214 | }
|
| 2215 |
|
| 2216 | if (src->class_idx == target->class_idx)
|
| 2217 | print_deadlock_bug(current, src, target);
|
| 2218 | else
|
| 2219 | print_circular_bug(&src_entry, target_entry, src, target);
|
| 2220 | }
|
| 2221 |
|
| 2222 | return ret;
|
| 2223 | }
|
| 2224 |
|
| 2225 | #ifdef CONFIG_TRACE_IRQFLAGS
|
| 2226 |
|
| 2227 | |
| 2228 | * Forwards and backwards subgraph searching, for the purposes of
|
| 2229 | * proving that two subgraphs can be connected by a new dependency
|
| 2230 | * without creating any illegal irq-safe -> irq-unsafe lock dependency.
|
| 2231 | *
|
| 2232 | * A irq safe->unsafe deadlock happens with the following conditions:
|
| 2233 | *
|
| 2234 | * 1) We have a strong dependency path A -> ... -> B
|
| 2235 | *
|
| 2236 | * 2) and we have ENABLED_IRQ usage of B and USED_IN_IRQ usage of A, therefore
|
| 2237 | * irq can create a new dependency B -> A (consider the case that a holder
|
| 2238 | * of B gets interrupted by an irq whose handler will try to acquire A).
|
| 2239 | *
|
| 2240 | * 3) the dependency circle A -> ... -> B -> A we get from 1) and 2) is a
|
| 2241 | * strong circle:
|
| 2242 | *
|
| 2243 | * For the usage bits of B:
|
| 2244 | * a) if A -> B is -(*N)->, then B -> A could be any type, so any
|
| 2245 | * ENABLED_IRQ usage suffices.
|
| 2246 | * b) if A -> B is -(*R)->, then B -> A must be -(E*)->, so only
|
| 2247 | * ENABLED_IRQ_*_READ usage suffices.
|
| 2248 | *
|
| 2249 | * For the usage bits of A:
|
| 2250 | * c) if A -> B is -(E*)->, then B -> A could be any type, so any
|
| 2251 | * USED_IN_IRQ usage suffices.
|
| 2252 | * d) if A -> B is -(S*)->, then B -> A must be -(*N)->, so only
|
| 2253 | * USED_IN_IRQ_*_READ usage suffices.
|
| 2254 |
|
| 2255 |
|
| 2256 | |
| 2257 | * There is a strong dependency path in the dependency graph: A -> B, and now
|
| 2258 | * we need to decide which usage bit of A should be accumulated to detect
|
| 2259 | * safe->unsafe bugs.
|
| 2260 | *
|
| 2261 | * Note that usage_accumulate() is used in backwards search, so ->only_xr
|
| 2262 | * stands for whether A -> B only has -(S*)-> (in this case ->only_xr is true).
|
| 2263 | *
|
| 2264 | * As above, if only_xr is false, which means A -> B has -(E*)-> dependency
|
| 2265 | * path, any usage of A should be considered. Otherwise, we should only
|
| 2266 | * consider _READ usage.
|
| 2267 |
|
| 2268 | static inline bool usage_accumulate(struct lock_list *entry, void *mask)
|
| 2269 | {
|
| 2270 | if (!entry->only_xr)
|
| 2271 | *(unsigned long *)mask |= entry->class->usage_mask;
|
| 2272 | else
|
| 2273 | *(unsigned long *)mask |= (entry->class->usage_mask & LOCKF_IRQ);
|
| 2274 |
|
| 2275 | return false;
|
| 2276 | }
|
| 2277 |
|
| 2278 | |
| 2279 | * There is a strong dependency path in the dependency graph: A -> B, and now
|
| 2280 | * we need to decide which usage bit of B conflicts with the usage bits of A,
|
| 2281 | * i.e. which usage bit of B may introduce safe->unsafe deadlocks.
|
| 2282 | *
|
| 2283 | * As above, if only_xr is false, which means A -> B has -(*N)-> dependency
|
| 2284 | * path, any usage of B should be considered. Otherwise, we should only
|
| 2285 | * consider _READ usage.
|
| 2286 |
|
| 2287 | static inline bool usage_match(struct lock_list *entry, void *mask)
|
| 2288 | {
|
| 2289 | if (!entry->only_xr)
|
| 2290 | return !!(entry->class->usage_mask & *(unsigned long *)mask);
|
| 2291 | else
|
| 2292 | return !!((entry->class->usage_mask & LOCKF_IRQ) & *(unsigned long *)mask);
|
| 2293 | }
|
| 2294 |
|
| 2295 | static inline bool usage_skip(struct lock_list *entry, void *mask)
|
| 2296 | {
|
| 2297 | if (entry->class->lock_type == LD_LOCK_NORMAL)
|
| 2298 | return false;
|
| 2299 |
|
| 2300 | |
| 2301 | * Skip local_lock() for irq inversion detection.
|
| 2302 | *
|
| 2303 | * For !RT, local_lock() is not a real lock, so it won't carry any
|
| 2304 | * dependency.
|
| 2305 | *
|
| 2306 | * For RT, an irq inversion happens when we have lock A and B, and on
|
| 2307 | * some CPU we can have:
|
| 2308 | *
|
| 2309 | * lock(A);
|
| 2310 | * <interrupted>
|
| 2311 | * lock(B);
|
| 2312 | *
|
| 2313 | * where lock(B) cannot sleep, and we have a dependency B -> ... -> A.
|
| 2314 | *
|
| 2315 | * Now we prove local_lock() cannot exist in that dependency. First we
|
| 2316 | * have the observation for any lock chain L1 -> ... -> Ln, for any
|
| 2317 | * 1 <= i <= n, Li.inner_wait_type <= L1.inner_wait_type, otherwise
|
| 2318 | * wait context check will complain. And since B is not a sleep lock,
|
| 2319 | * therefore B.inner_wait_type >= 2, and since the inner_wait_type of
|
| 2320 | * local_lock() is 3, which is greater than 2, therefore there is no
|
| 2321 | * way the local_lock() exists in the dependency B -> ... -> A.
|
| 2322 | *
|
| 2323 | * As a result, we will skip local_lock(), when we search for irq
|
| 2324 | * inversion bugs.
|
| 2325 |
|
| 2326 | if (entry->class->lock_type == LD_LOCK_PERCPU &&
|
| 2327 | DEBUG_LOCKS_WARN_ON(entry->class->wait_type_inner < LD_WAIT_CONFIG))
|
| 2328 | return false;
|
| 2329 |
|
| 2330 | |
| 2331 | * Skip WAIT_OVERRIDE for irq inversion detection -- it's not actually
|
| 2332 | * a lock and only used to override the wait_type.
|
| 2333 |
|
| 2334 |
|
| 2335 | return true;
|
| 2336 | }
|
| 2337 |
|
| 2338 | |
| 2339 | * Find a node in the forwards-direction dependency sub-graph starting
|
| 2340 | * at @root->class that matches @bit.
|
| 2341 | *
|
| 2342 | * Return BFS_MATCH if such a node exists in the subgraph, and put that node
|
| 2343 | * into *@target_entry.
|
| 2344 |
|
| 2345 | static enum bfs_result
|
| 2346 | find_usage_forwards(struct lock_list *root, unsigned long usage_mask,
|
| 2347 | struct lock_list **target_entry)
|
| 2348 | {
|
| 2349 | enum bfs_result result;
|
| 2350 |
|
| 2351 | debug_atomic_inc(nr_find_usage_forwards_checks);
|
| 2352 |
|
| 2353 | result = __bfs_forwards(root, &usage_mask, usage_match, usage_skip, target_entry);
|
| 2354 |
|
| 2355 | return result;
|
| 2356 | }
|
| 2357 |
|
| 2358 | |
| 2359 | * Find a node in the backwards-direction dependency sub-graph starting
|
| 2360 | * at @root->class that matches @bit.
|
| 2361 |
|
| 2362 | static enum bfs_result
|
| 2363 | find_usage_backwards(struct lock_list *root, unsigned long usage_mask,
|
| 2364 | struct lock_list **target_entry)
|
| 2365 | {
|
| 2366 | enum bfs_result result;
|
| 2367 |
|
| 2368 | debug_atomic_inc(nr_find_usage_backwards_checks);
|
| 2369 |
|
| 2370 | result = __bfs_backwards(root, &usage_mask, usage_match, usage_skip, target_entry);
|
| 2371 |
|
| 2372 | return result;
|
| 2373 | }
|
| 2374 |
|
| 2375 | static void print_lock_class_header(struct lock_class *class, int depth)
|
| 2376 | {
|
| 2377 | int bit;
|
| 2378 |
|
| 2379 | printk("%*s->", depth, "");
|
| 2380 | print_lock_name(NULL, class);
|
| 2381 | #ifdef CONFIG_DEBUG_LOCKDEP
|
| 2382 | printk(KERN_CONT " ops: %lu", debug_class_ops_read(class));
|
| 2383 | #endif
|
| 2384 | printk(KERN_CONT " {\n");
|
| 2385 |
|
| 2386 | for (bit = 0; bit < LOCK_TRACE_STATES; bit++) {
|
| 2387 | if (class->usage_mask & (1 << bit)) {
|
| 2388 | int len = depth;
|
| 2389 |
|
| 2390 | len += printk("%*s %s", depth, "", usage_str[bit]);
|
| 2391 | len += printk(KERN_CONT " at:\n");
|
| 2392 | print_lock_trace(class->usage_traces[bit], len);
|
| 2393 | }
|
| 2394 | }
|
| 2395 | printk("%*s }\n", depth, "");
|
| 2396 |
|
| 2397 | printk("%*s ... key at: [<%px>] %pS\n",
|
| 2398 | depth, "", class->key, class->key);
|
| 2399 | }
|
| 2400 |
|
| 2401 | |
| 2402 | * Dependency path printing:
|
| 2403 | *
|
| 2404 | * After BFS we get a lock dependency path (linked via ->parent of lock_list),
|
| 2405 | * printing out each lock in the dependency path will help on understanding how
|
| 2406 | * the deadlock could happen. Here are some details about dependency path
|
| 2407 | * printing:
|
| 2408 | *
|
| 2409 | * 1) A lock_list can be either forwards or backwards for a lock dependency,
|
| 2410 | * for a lock dependency A -> B, there are two lock_lists:
|
| 2411 | *
|
| 2412 | * a) lock_list in the ->locks_after list of A, whose ->class is B and
|
| 2413 | * ->links_to is A. In this case, we can say the lock_list is
|
| 2414 | * "A -> B" (forwards case).
|
| 2415 | *
|
| 2416 | * b) lock_list in the ->locks_before list of B, whose ->class is A
|
| 2417 | * and ->links_to is B. In this case, we can say the lock_list is
|
| 2418 | * "B <- A" (bacwards case).
|
| 2419 | *
|
| 2420 | * The ->trace of both a) and b) point to the call trace where B was
|
| 2421 | * acquired with A held.
|
| 2422 | *
|
| 2423 | * 2) A "helper" lock_list is introduced during BFS, this lock_list doesn't
|
| 2424 | * represent a certain lock dependency, it only provides an initial entry
|
| 2425 | * for BFS. For example, BFS may introduce a "helper" lock_list whose
|
| 2426 | * ->class is A, as a result BFS will search all dependencies starting with
|
| 2427 | * A, e.g. A -> B or A -> C.
|
| 2428 | *
|
| 2429 | * The notation of a forwards helper lock_list is like "-> A", which means
|
| 2430 | * we should search the forwards dependencies starting with "A", e.g A -> B
|
| 2431 | * or A -> C.
|
| 2432 | *
|
| 2433 | * The notation of a bacwards helper lock_list is like "<- B", which means
|
| 2434 | * we should search the backwards dependencies ending with "B", e.g.
|
| 2435 | * B <- A or B <- C.
|
| 2436 |
|
| 2437 |
|
| 2438 | |
| 2439 | * printk the shortest lock dependencies from @root to @leaf in reverse order.
|
| 2440 | *
|
| 2441 | * We have a lock dependency path as follow:
|
| 2442 | *
|
| 2443 | * @root @leaf
|
| 2444 | * | |
|
| 2445 | * V V
|
| 2446 | * ->parent ->parent
|
| 2447 | * | lock_list | <--------- | lock_list | ... | lock_list | <--------- | lock_list |
|
| 2448 | * | -> L1 | | L1 -> L2 | ... |Ln-2 -> Ln-1| | Ln-1 -> Ln|
|
| 2449 | *
|
| 2450 | * , so it's natural that we start from @leaf and print every ->class and
|
| 2451 | * ->trace until we reach the @root.
|
| 2452 |
|
| 2453 | static void __used
|
| 2454 | print_shortest_lock_dependencies(struct lock_list *leaf,
|
| 2455 | struct lock_list *root)
|
| 2456 | {
|
| 2457 | struct lock_list *entry = leaf;
|
| 2458 | int depth;
|
| 2459 |
|
| 2460 |
|
| 2461 | depth = get_lock_depth(leaf);
|
| 2462 |
|
| 2463 | do {
|
| 2464 | print_lock_class_header(entry->class, depth);
|
| 2465 | printk("%*s ... acquired at:\n", depth, "");
|
| 2466 | print_lock_trace(entry->trace, 2);
|
| 2467 | printk("\n");
|
| 2468 |
|
| 2469 | if (depth == 0 && (entry != root)) {
|
| 2470 | printk("lockdep:%s bad path found in chain graph\n", __func__);
|
| 2471 | break;
|
| 2472 | }
|
| 2473 |
|
| 2474 | entry = get_lock_parent(entry);
|
| 2475 | depth--;
|
| 2476 | } while (entry && (depth >= 0));
|
| 2477 | }
|
| 2478 |
|
| 2479 | |
| 2480 | * printk the shortest lock dependencies from @leaf to @root.
|
| 2481 | *
|
| 2482 | * We have a lock dependency path (from a backwards search) as follow:
|
| 2483 | *
|
| 2484 | * @leaf @root
|
| 2485 | * | |
|
| 2486 | * V V
|
| 2487 | * ->parent ->parent
|
| 2488 | * | lock_list | ---------> | lock_list | ... | lock_list | ---------> | lock_list |
|
| 2489 | * | L2 <- L1 | | L3 <- L2 | ... | Ln <- Ln-1 | | <- Ln |
|
| 2490 | *
|
| 2491 | * , so when we iterate from @leaf to @root, we actually print the lock
|
| 2492 | * dependency path L1 -> L2 -> .. -> Ln in the non-reverse order.
|
| 2493 | *
|
| 2494 | * Another thing to notice here is that ->class of L2 <- L1 is L1, while the
|
| 2495 | * ->trace of L2 <- L1 is the call trace of L2, in fact we don't have the call
|
| 2496 | * trace of L1 in the dependency path, which is alright, because most of the
|
| 2497 | * time we can figure out where L1 is held from the call trace of L2.
|
| 2498 |
|
| 2499 | static void __used
|
| 2500 | print_shortest_lock_dependencies_backwards(struct lock_list *leaf,
|
| 2501 | struct lock_list *root)
|
| 2502 | {
|
| 2503 | struct lock_list *entry = leaf;
|
| 2504 | const struct lock_trace *trace = NULL;
|
| 2505 | int depth;
|
| 2506 |
|
| 2507 |
|
| 2508 | depth = get_lock_depth(leaf);
|
| 2509 |
|
| 2510 | do {
|
| 2511 | print_lock_class_header(entry->class, depth);
|
| 2512 | if (trace) {
|
| 2513 | printk("%*s ... acquired at:\n", depth, "");
|
| 2514 | print_lock_trace(trace, 2);
|
| 2515 | printk("\n");
|
| 2516 | }
|
| 2517 |
|
| 2518 | |
| 2519 | * Record the pointer to the trace for the next lock_list
|
| 2520 | * entry, see the comments for the function.
|
| 2521 |
|
| 2522 | trace = entry->trace;
|
| 2523 |
|
| 2524 | if (depth == 0 && (entry != root)) {
|
| 2525 | printk("lockdep:%s bad path found in chain graph\n", __func__);
|
| 2526 | break;
|
| 2527 | }
|
| 2528 |
|
| 2529 | entry = get_lock_parent(entry);
|
| 2530 | depth--;
|
| 2531 | } while (entry && (depth >= 0));
|
| 2532 | }
|
| 2533 |
|
| 2534 | static void
|
| 2535 | print_irq_lock_scenario(struct lock_list *safe_entry,
|
| 2536 | struct lock_list *unsafe_entry,
|
| 2537 | struct lock_class *prev_class,
|
| 2538 | struct lock_class *next_class)
|
| 2539 | {
|
| 2540 | struct lock_class *safe_class = safe_entry->class;
|
| 2541 | struct lock_class *unsafe_class = unsafe_entry->class;
|
| 2542 | struct lock_class *middle_class = prev_class;
|
| 2543 |
|
| 2544 | if (middle_class == safe_class)
|
| 2545 | middle_class = next_class;
|
| 2546 |
|
| 2547 | |
| 2548 | * A direct locking problem where unsafe_class lock is taken
|
| 2549 | * directly by safe_class lock, then all we need to show
|
| 2550 | * is the deadlock scenario, as it is obvious that the
|
| 2551 | * unsafe lock is taken under the safe lock.
|
| 2552 | *
|
| 2553 | * But if there is a chain instead, where the safe lock takes
|
| 2554 | * an intermediate lock (middle_class) where this lock is
|
| 2555 | * not the same as the safe lock, then the lock chain is
|
| 2556 | * used to describe the problem. Otherwise we would need
|
| 2557 | * to show a different CPU case for each link in the chain
|
| 2558 | * from the safe_class lock to the unsafe_class lock.
|
| 2559 |
|
| 2560 | if (middle_class != unsafe_class) {
|
| 2561 | printk("Chain exists of:\n ");
|
| 2562 | __print_lock_name(NULL, safe_class);
|
| 2563 | printk(KERN_CONT " --> ");
|
| 2564 | __print_lock_name(NULL, middle_class);
|
| 2565 | printk(KERN_CONT " --> ");
|
| 2566 | __print_lock_name(NULL, unsafe_class);
|
| 2567 | printk(KERN_CONT "\n\n");
|
| 2568 | }
|
| 2569 |
|
| 2570 | printk(" Possible interrupt unsafe locking scenario:\n\n");
|
| 2571 | printk(" CPU0 CPU1\n");
|
| 2572 | printk(" ---- ----\n");
|
| 2573 | printk(" lock(");
|
| 2574 | __print_lock_name(NULL, unsafe_class);
|
| 2575 | printk(KERN_CONT ");\n");
|
| 2576 | printk(" local_irq_disable();\n");
|
| 2577 | printk(" lock(");
|
| 2578 | __print_lock_name(NULL, safe_class);
|
| 2579 | printk(KERN_CONT ");\n");
|
| 2580 | printk(" lock(");
|
| 2581 | __print_lock_name(NULL, middle_class);
|
| 2582 | printk(KERN_CONT ");\n");
|
| 2583 | printk(" <Interrupt>\n");
|
| 2584 | printk(" lock(");
|
| 2585 | __print_lock_name(NULL, safe_class);
|
| 2586 | printk(KERN_CONT ");\n");
|
| 2587 | printk("\n *** DEADLOCK ***\n\n");
|
| 2588 | }
|
| 2589 |
|
| 2590 | static void
|
| 2591 | print_bad_irq_dependency(struct task_struct *curr,
|
| 2592 | struct lock_list *prev_root,
|
| 2593 | struct lock_list *next_root,
|
| 2594 | struct lock_list *backwards_entry,
|
| 2595 | struct lock_list *forwards_entry,
|
| 2596 | struct held_lock *prev,
|
| 2597 | struct held_lock *next,
|
| 2598 | enum lock_usage_bit bit1,
|
| 2599 | enum lock_usage_bit bit2,
|
| 2600 | const char *irqclass)
|
| 2601 | {
|
| 2602 | if (!debug_locks_off_graph_unlock() || debug_locks_silent)
|
| 2603 | return;
|
| 2604 |
|
| 2605 | nbcon_cpu_emergency_enter();
|
| 2606 |
|
| 2607 | pr_warn("\n");
|
| 2608 | pr_warn("=====================================================\n");
|
| 2609 | pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
|
| 2610 | irqclass, irqclass);
|
| 2611 | print_kernel_ident();
|
| 2612 | pr_warn("-----------------------------------------------------\n");
|
| 2613 | pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
|
| 2614 | curr->comm, task_pid_nr(curr),
|
| 2615 | lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT,
|
| 2616 | curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
|
| 2617 | lockdep_hardirqs_enabled(),
|
| 2618 | curr->softirqs_enabled);
|
| 2619 | print_lock(next);
|
| 2620 |
|
| 2621 | pr_warn("\nand this task is already holding:\n");
|
| 2622 | print_lock(prev);
|
| 2623 | pr_warn("which would create a new lock dependency:\n");
|
| 2624 | print_lock_name(prev, hlock_class(prev));
|
| 2625 | pr_cont(" ->");
|
| 2626 | print_lock_name(next, hlock_class(next));
|
| 2627 | pr_cont("\n");
|
| 2628 |
|
| 2629 | pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
|
| 2630 | irqclass);
|
| 2631 | print_lock_name(NULL, backwards_entry->class);
|
| 2632 | pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
|
| 2633 |
|
| 2634 | print_lock_trace(backwards_entry->class->usage_traces[bit1], 1);
|
| 2635 |
|
| 2636 | pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
|
| 2637 | print_lock_name(NULL, forwards_entry->class);
|
| 2638 | pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
|
| 2639 | pr_warn("...");
|
| 2640 |
|
| 2641 | print_lock_trace(forwards_entry->class->usage_traces[bit2], 1);
|
| 2642 |
|
| 2643 | pr_warn("\nother info that might help us debug this:\n\n");
|
| 2644 | print_irq_lock_scenario(backwards_entry, forwards_entry,
|
| 2645 | hlock_class(prev), hlock_class(next));
|
| 2646 |
|
| 2647 | lockdep_print_held_locks(curr);
|
| 2648 |
|
| 2649 | pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
|
| 2650 | print_shortest_lock_dependencies_backwards(backwards_entry, prev_root);
|
| 2651 |
|
| 2652 | pr_warn("\nthe dependencies between the lock to be acquired");
|
| 2653 | pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
|
| 2654 | next_root->trace = save_trace();
|
| 2655 | if (!next_root->trace)
|
| 2656 | goto out;
|
| 2657 | print_shortest_lock_dependencies(forwards_entry, next_root);
|
| 2658 |
|
| 2659 | pr_warn("\nstack backtrace:\n");
|
| 2660 | dump_stack();
|
| 2661 | out:
|
| 2662 | nbcon_cpu_emergency_exit();
|
| 2663 | }
|
| 2664 |
|
| 2665 | static const char *state_names[] = {
|
| 2666 | #define LOCKDEP_STATE(__STATE) \
|
| 2667 | __stringify(__STATE),
|
| 2668 | #include "lockdep_states.h"
|
| 2669 | #undef LOCKDEP_STATE
|
| 2670 | };
|
| 2671 |
|
| 2672 | static const char *state_rnames[] = {
|
| 2673 | #define LOCKDEP_STATE(__STATE) \
|
| 2674 | __stringify(__STATE)"-READ",
|
| 2675 | #include "lockdep_states.h"
|
| 2676 | #undef LOCKDEP_STATE
|
| 2677 | };
|
| 2678 |
|
| 2679 | static inline const char *state_name(enum lock_usage_bit bit)
|
| 2680 | {
|
| 2681 | if (bit & LOCK_USAGE_READ_MASK)
|
| 2682 | return state_rnames[bit >> LOCK_USAGE_DIR_MASK];
|
| 2683 | else
|
| 2684 | return state_names[bit >> LOCK_USAGE_DIR_MASK];
|
| 2685 | }
|
| 2686 |
|
| 2687 | |
| 2688 | * The bit number is encoded like:
|
| 2689 | *
|
| 2690 | * bit0: 0 exclusive, 1 read lock
|
| 2691 | * bit1: 0 used in irq, 1 irq enabled
|
| 2692 | * bit2-n: state
|
| 2693 |
|
| 2694 | static int exclusive_bit(int new_bit)
|
| 2695 | {
|
| 2696 | int state = new_bit & LOCK_USAGE_STATE_MASK;
|
| 2697 | int dir = new_bit & LOCK_USAGE_DIR_MASK;
|
| 2698 |
|
| 2699 | |
| 2700 | * keep state, bit flip the direction and strip read.
|
| 2701 |
|
| 2702 | return state | (dir ^ LOCK_USAGE_DIR_MASK);
|
| 2703 | }
|
| 2704 |
|
| 2705 | |
| 2706 | * Observe that when given a bitmask where each bitnr is encoded as above, a
|
| 2707 | * right shift of the mask transforms the individual bitnrs as -1 and
|
| 2708 | * conversely, a left shift transforms into +1 for the individual bitnrs.
|
| 2709 | *
|
| 2710 | * So for all bits whose number have LOCK_ENABLED_* set (bitnr1 == 1), we can
|
| 2711 | * create the mask with those bit numbers using LOCK_USED_IN_* (bitnr1 == 0)
|
| 2712 | * instead by subtracting the bit number by 2, or shifting the mask right by 2.
|
| 2713 | *
|
| 2714 | * Similarly, bitnr1 == 0 becomes bitnr1 == 1 by adding 2, or shifting left 2.
|
| 2715 | *
|
| 2716 | * So split the mask (note that LOCKF_ENABLED_IRQ_ALL|LOCKF_USED_IN_IRQ_ALL is
|
| 2717 | * all bits set) and recompose with bitnr1 flipped.
|
| 2718 |
|
| 2719 | static unsigned long invert_dir_mask(unsigned long mask)
|
| 2720 | {
|
| 2721 | unsigned long excl = 0;
|
| 2722 |
|
| 2723 |
|
| 2724 | excl |= (mask & LOCKF_ENABLED_IRQ_ALL) >> LOCK_USAGE_DIR_MASK;
|
| 2725 | excl |= (mask & LOCKF_USED_IN_IRQ_ALL) << LOCK_USAGE_DIR_MASK;
|
| 2726 |
|
| 2727 | return excl;
|
| 2728 | }
|
| 2729 |
|
| 2730 | |
| 2731 | * Note that a LOCK_ENABLED_IRQ_*_READ usage and a LOCK_USED_IN_IRQ_*_READ
|
| 2732 | * usage may cause deadlock too, for example:
|
| 2733 | *
|
| 2734 | * P1 P2
|
| 2735 | * <irq disabled>
|
| 2736 | * write_lock(l1); <irq enabled>
|
| 2737 | * read_lock(l2);
|
| 2738 | * write_lock(l2);
|
| 2739 | * <in irq>
|
| 2740 | * read_lock(l1);
|
| 2741 | *
|
| 2742 | * , in above case, l1 will be marked as LOCK_USED_IN_IRQ_HARDIRQ_READ and l2
|
| 2743 | * will marked as LOCK_ENABLE_IRQ_HARDIRQ_READ, and this is a possible
|
| 2744 | * deadlock.
|
| 2745 | *
|
| 2746 | * In fact, all of the following cases may cause deadlocks:
|
| 2747 | *
|
| 2748 | * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*
|
| 2749 | * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*
|
| 2750 | * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*_READ
|
| 2751 | * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*_READ
|
| 2752 | *
|
| 2753 | * As a result, to calculate the "exclusive mask", first we invert the
|
| 2754 | * direction (USED_IN/ENABLED) of the original mask, and 1) for all bits with
|
| 2755 | * bitnr0 set (LOCK_*_READ), add those with bitnr0 cleared (LOCK_*). 2) for all
|
| 2756 | * bits with bitnr0 cleared (LOCK_*_READ), add those with bitnr0 set (LOCK_*).
|
| 2757 |
|
| 2758 | static unsigned long exclusive_mask(unsigned long mask)
|
| 2759 | {
|
| 2760 | unsigned long excl = invert_dir_mask(mask);
|
| 2761 |
|
| 2762 | excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK;
|
| 2763 | excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK;
|
| 2764 |
|
| 2765 | return excl;
|
| 2766 | }
|
| 2767 |
|
| 2768 | |
| 2769 | * Retrieve the _possible_ original mask to which @mask is
|
| 2770 | * exclusive. Ie: this is the opposite of exclusive_mask().
|
| 2771 | * Note that 2 possible original bits can match an exclusive
|
| 2772 | * bit: one has LOCK_USAGE_READ_MASK set, the other has it
|
| 2773 | * cleared. So both are returned for each exclusive bit.
|
| 2774 |
|
| 2775 | static unsigned long original_mask(unsigned long mask)
|
| 2776 | {
|
| 2777 | unsigned long excl = invert_dir_mask(mask);
|
| 2778 |
|
| 2779 |
|
| 2780 | excl |= (excl & LOCKF_IRQ_READ) >> LOCK_USAGE_READ_MASK;
|
| 2781 | excl |= (excl & LOCKF_IRQ) << LOCK_USAGE_READ_MASK;
|
| 2782 |
|
| 2783 | return excl;
|
| 2784 | }
|
| 2785 |
|
| 2786 | |
| 2787 | * Find the first pair of bit match between an original
|
| 2788 | * usage mask and an exclusive usage mask.
|
| 2789 |
|
| 2790 | static int find_exclusive_match(unsigned long mask,
|
| 2791 | unsigned long excl_mask,
|
| 2792 | enum lock_usage_bit *bitp,
|
| 2793 | enum lock_usage_bit *excl_bitp)
|
| 2794 | {
|
| 2795 | int bit, excl, excl_read;
|
| 2796 |
|
| 2797 | for_each_set_bit(bit, &mask, LOCK_USED) {
|
| 2798 | |
| 2799 | * exclusive_bit() strips the read bit, however,
|
| 2800 | * LOCK_ENABLED_IRQ_*_READ may cause deadlocks too, so we need
|
| 2801 | * to search excl | LOCK_USAGE_READ_MASK as well.
|
| 2802 |
|
| 2803 | excl = exclusive_bit(bit);
|
| 2804 | excl_read = excl | LOCK_USAGE_READ_MASK;
|
| 2805 | if (excl_mask & lock_flag(excl)) {
|
| 2806 | *bitp = bit;
|
| 2807 | *excl_bitp = excl;
|
| 2808 | return 0;
|
| 2809 | } else if (excl_mask & lock_flag(excl_read)) {
|
| 2810 | *bitp = bit;
|
| 2811 | *excl_bitp = excl_read;
|
| 2812 | return 0;
|
| 2813 | }
|
| 2814 | }
|
| 2815 | return -1;
|
| 2816 | }
|
| 2817 |
|
| 2818 | |
| 2819 | * Prove that the new dependency does not connect a hardirq-safe(-read)
|
| 2820 | * lock with a hardirq-unsafe lock - to achieve this we search
|
| 2821 | * the backwards-subgraph starting at <prev>, and the
|
| 2822 | * forwards-subgraph starting at <next>:
|
| 2823 |
|
| 2824 | static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
|
| 2825 | struct held_lock *next)
|
| 2826 | {
|
| 2827 | unsigned long usage_mask = 0, forward_mask, backward_mask;
|
| 2828 | enum lock_usage_bit forward_bit = 0, backward_bit = 0;
|
| 2829 | struct lock_list *target_entry1;
|
| 2830 | struct lock_list *target_entry;
|
| 2831 | struct lock_list this, that;
|
| 2832 | enum bfs_result ret;
|
| 2833 |
|
| 2834 | |
| 2835 | * Step 1: gather all hard/soft IRQs usages backward in an
|
| 2836 | * accumulated usage mask.
|
| 2837 |
|
| 2838 | bfs_init_rootb(&this, prev);
|
| 2839 |
|
| 2840 | ret = __bfs_backwards(&this, &usage_mask, usage_accumulate, usage_skip, NULL);
|
| 2841 | if (bfs_error(ret)) {
|
| 2842 | print_bfs_bug(ret);
|
| 2843 | return 0;
|
| 2844 | }
|
| 2845 |
|
| 2846 | usage_mask &= LOCKF_USED_IN_IRQ_ALL;
|
| 2847 | if (!usage_mask)
|
| 2848 | return 1;
|
| 2849 |
|
| 2850 | |
| 2851 | * Step 2: find exclusive uses forward that match the previous
|
| 2852 | * backward accumulated mask.
|
| 2853 |
|
| 2854 | forward_mask = exclusive_mask(usage_mask);
|
| 2855 |
|
| 2856 | bfs_init_root(&that, next);
|
| 2857 |
|
| 2858 | ret = find_usage_forwards(&that, forward_mask, &target_entry1);
|
| 2859 | if (bfs_error(ret)) {
|
| 2860 | print_bfs_bug(ret);
|
| 2861 | return 0;
|
| 2862 | }
|
| 2863 | if (ret == BFS_RNOMATCH)
|
| 2864 | return 1;
|
| 2865 |
|
| 2866 | |
| 2867 | * Step 3: we found a bad match! Now retrieve a lock from the backward
|
| 2868 | * list whose usage mask matches the exclusive usage mask from the
|
| 2869 | * lock found on the forward list.
|
| 2870 | *
|
| 2871 | * Note, we should only keep the LOCKF_ENABLED_IRQ_ALL bits, considering
|
| 2872 | * the follow case:
|
| 2873 | *
|
| 2874 | * When trying to add A -> B to the graph, we find that there is a
|
| 2875 | * hardirq-safe L, that L -> ... -> A, and another hardirq-unsafe M,
|
| 2876 | * that B -> ... -> M. However M is **softirq-safe**, if we use exact
|
| 2877 | * invert bits of M's usage_mask, we will find another lock N that is
|
| 2878 | * **softirq-unsafe** and N -> ... -> A, however N -> .. -> M will not
|
| 2879 | * cause a inversion deadlock.
|
| 2880 |
|
| 2881 | backward_mask = original_mask(target_entry1->class->usage_mask & LOCKF_ENABLED_IRQ_ALL);
|
| 2882 |
|
| 2883 | ret = find_usage_backwards(&this, backward_mask, &target_entry);
|
| 2884 | if (bfs_error(ret)) {
|
| 2885 | print_bfs_bug(ret);
|
| 2886 | return 0;
|
| 2887 | }
|
| 2888 | if (DEBUG_LOCKS_WARN_ON(ret == BFS_RNOMATCH))
|
| 2889 | return 1;
|
| 2890 |
|
| 2891 | |
| 2892 | * Step 4: narrow down to a pair of incompatible usage bits
|
| 2893 | * and report it.
|
| 2894 |
|
| 2895 | ret = find_exclusive_match(target_entry->class->usage_mask,
|
| 2896 | target_entry1->class->usage_mask,
|
| 2897 | &backward_bit, &forward_bit);
|
| 2898 | if (DEBUG_LOCKS_WARN_ON(ret == -1))
|
| 2899 | return 1;
|
| 2900 |
|
| 2901 | print_bad_irq_dependency(curr, &this, &that,
|
| 2902 | target_entry, target_entry1,
|
| 2903 | prev, next,
|
| 2904 | backward_bit, forward_bit,
|
| 2905 | state_name(backward_bit));
|
| 2906 |
|
| 2907 | return 0;
|
| 2908 | }
|
| 2909 |
|
| 2910 | #else
|
| 2911 |
|
| 2912 | static inline int check_irq_usage(struct task_struct *curr,
|
| 2913 | struct held_lock *prev, struct held_lock *next)
|
| 2914 | {
|
| 2915 | return 1;
|
| 2916 | }
|
| 2917 |
|
| 2918 | static inline bool usage_skip(struct lock_list *entry, void *mask)
|
| 2919 | {
|
| 2920 | return false;
|
| 2921 | }
|
| 2922 |
|
| 2923 | #endif
|
| 2924 |
|
| 2925 | #ifdef CONFIG_LOCKDEP_SMALL
|
| 2926 | |
| 2927 | * We are about to add A -> B into the dependency graph, and in __bfs() a
|
| 2928 | * strong dependency path A -> .. -> B is found: hlock_class equals
|
| 2929 | * entry->class.
|
| 2930 | *
|
| 2931 | * If A -> .. -> B can replace A -> B in any __bfs() search (means the former
|
| 2932 | * is _stronger_ than or equal to the latter), we consider A -> B as redundant.
|
| 2933 | * For example if A -> .. -> B is -(EN)-> (i.e. A -(E*)-> .. -(*N)-> B), and A
|
| 2934 | * -> B is -(ER)-> or -(EN)->, then we don't need to add A -> B into the
|
| 2935 | * dependency graph, as any strong path ..-> A -> B ->.. we can get with
|
| 2936 | * having dependency A -> B, we could already get a equivalent path ..-> A ->
|
| 2937 | * .. -> B -> .. with A -> .. -> B. Therefore A -> B is redundant.
|
| 2938 | *
|
| 2939 | * We need to make sure both the start and the end of A -> .. -> B is not
|
| 2940 | * weaker than A -> B. For the start part, please see the comment in
|
| 2941 | * check_redundant(). For the end part, we need:
|
| 2942 | *
|
| 2943 | * Either
|
| 2944 | *
|
| 2945 | * a) A -> B is -(*R)-> (everything is not weaker than that)
|
| 2946 | *
|
| 2947 | * or
|
| 2948 | *
|
| 2949 | * b) A -> .. -> B is -(*N)-> (nothing is stronger than this)
|
| 2950 | *
|
| 2951 |
|
| 2952 | static inline bool hlock_equal(struct lock_list *entry, void *data)
|
| 2953 | {
|
| 2954 | struct held_lock *hlock = (struct held_lock *)data;
|
| 2955 |
|
| 2956 | return hlock_class(hlock) == entry->class &&
|
| 2957 | (hlock->read == 2 ||
|
| 2958 | !entry->only_xr);
|
| 2959 | }
|
| 2960 |
|
| 2961 | |
| 2962 | * Check that the dependency graph starting at <src> can lead to
|
| 2963 | * <target> or not. If it can, <src> -> <target> dependency is already
|
| 2964 | * in the graph.
|
| 2965 | *
|
| 2966 | * Return BFS_RMATCH if it does, or BFS_RNOMATCH if it does not, return BFS_E* if
|
| 2967 | * any error appears in the bfs search.
|
| 2968 |
|
| 2969 | static noinline enum bfs_result
|
| 2970 | check_redundant(struct held_lock *src, struct held_lock *target)
|
| 2971 | {
|
| 2972 | enum bfs_result ret;
|
| 2973 | struct lock_list *target_entry;
|
| 2974 | struct lock_list src_entry;
|
| 2975 |
|
| 2976 | bfs_init_root(&src_entry, src);
|
| 2977 | |
| 2978 | * Special setup for check_redundant().
|
| 2979 | *
|
| 2980 | * To report redundant, we need to find a strong dependency path that
|
| 2981 | * is equal to or stronger than <src> -> <target>. So if <src> is E,
|
| 2982 | * we need to let __bfs() only search for a path starting at a -(E*)->,
|
| 2983 | * we achieve this by setting the initial node's ->only_xr to true in
|
| 2984 | * that case. And if <prev> is S, we set initial ->only_xr to false
|
| 2985 | * because both -(S*)-> (equal) and -(E*)-> (stronger) are redundant.
|
| 2986 |
|
| 2987 | src_entry.only_xr = src->read == 0;
|
| 2988 |
|
| 2989 | debug_atomic_inc(nr_redundant_checks);
|
| 2990 |
|
| 2991 | |
| 2992 | * Note: we skip local_lock() for redundant check, because as the
|
| 2993 | * comment in usage_skip(), A -> local_lock() -> B and A -> B are not
|
| 2994 | * the same.
|
| 2995 |
|
| 2996 | ret = check_path(target, &src_entry, hlock_equal, usage_skip, &target_entry);
|
| 2997 |
|
| 2998 | if (ret == BFS_RMATCH)
|
| 2999 | debug_atomic_inc(nr_redundant);
|
| 3000 |
|
| 3001 | return ret;
|
| 3002 | }
|
| 3003 |
|
| 3004 | #else
|
| 3005 |
|
| 3006 | static inline enum bfs_result
|
| 3007 | check_redundant(struct held_lock *src, struct held_lock *target)
|
| 3008 | {
|
| 3009 | return BFS_RNOMATCH;
|
| 3010 | }
|
| 3011 |
|
| 3012 | #endif
|
| 3013 |
|
| 3014 | static void inc_chains(int irq_context)
|
| 3015 | {
|
| 3016 | if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT)
|
| 3017 | nr_hardirq_chains++;
|
| 3018 | else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT)
|
| 3019 | nr_softirq_chains++;
|
| 3020 | else
|
| 3021 | nr_process_chains++;
|
| 3022 | }
|
| 3023 |
|
| 3024 | static void dec_chains(int irq_context)
|
| 3025 | {
|
| 3026 | if (irq_context & LOCK_CHAIN_HARDIRQ_CONTEXT)
|
| 3027 | nr_hardirq_chains--;
|
| 3028 | else if (irq_context & LOCK_CHAIN_SOFTIRQ_CONTEXT)
|
| 3029 | nr_softirq_chains--;
|
| 3030 | else
|
| 3031 | nr_process_chains--;
|
| 3032 | }
|
| 3033 |
|
| 3034 | static void
|
| 3035 | print_deadlock_scenario(struct held_lock *nxt, struct held_lock *prv)
|
| 3036 | {
|
| 3037 | struct lock_class *next = hlock_class(nxt);
|
| 3038 | struct lock_class *prev = hlock_class(prv);
|
| 3039 |
|
| 3040 | printk(" Possible unsafe locking scenario:\n\n");
|
| 3041 | printk(" CPU0\n");
|
| 3042 | printk(" ----\n");
|
| 3043 | printk(" lock(");
|
| 3044 | __print_lock_name(prv, prev);
|
| 3045 | printk(KERN_CONT ");\n");
|
| 3046 | printk(" lock(");
|
| 3047 | __print_lock_name(nxt, next);
|
| 3048 | printk(KERN_CONT ");\n");
|
| 3049 | printk("\n *** DEADLOCK ***\n\n");
|
| 3050 | printk(" May be due to missing lock nesting notation\n\n");
|
| 3051 | }
|
| 3052 |
|
| 3053 | static void
|
| 3054 | print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
|
| 3055 | struct held_lock *next)
|
| 3056 | {
|
| 3057 | struct lock_class *class = hlock_class(prev);
|
| 3058 |
|
| 3059 | if (!debug_locks_off_graph_unlock() || debug_locks_silent)
|
| 3060 | return;
|
| 3061 |
|
| 3062 | nbcon_cpu_emergency_enter();
|
| 3063 |
|
| 3064 | pr_warn("\n");
|
| 3065 | pr_warn("============================================\n");
|
| 3066 | pr_warn("WARNING: possible recursive locking detected\n");
|
| 3067 | print_kernel_ident();
|
| 3068 | pr_warn("--------------------------------------------\n");
|
| 3069 | pr_warn("%s/%d is trying to acquire lock:\n",
|
| 3070 | curr->comm, task_pid_nr(curr));
|
| 3071 | print_lock(next);
|
| 3072 | pr_warn("\nbut task is already holding lock:\n");
|
| 3073 | print_lock(prev);
|
| 3074 |
|
| 3075 | if (class->cmp_fn) {
|
| 3076 | pr_warn("and the lock comparison function returns %i:\n",
|
| 3077 | class->cmp_fn(prev->instance, next->instance));
|
| 3078 | }
|
| 3079 |
|
| 3080 | pr_warn("\nother info that might help us debug this:\n");
|
| 3081 | print_deadlock_scenario(next, prev);
|
| 3082 | lockdep_print_held_locks(curr);
|
| 3083 |
|
| 3084 | pr_warn("\nstack backtrace:\n");
|
| 3085 | dump_stack();
|
| 3086 |
|
| 3087 | nbcon_cpu_emergency_exit();
|
| 3088 | }
|
| 3089 |
|
| 3090 | |
| 3091 | * Check whether we are holding such a class already.
|
| 3092 | *
|
| 3093 | * (Note that this has to be done separately, because the graph cannot
|
| 3094 | * detect such classes of deadlocks.)
|
| 3095 | *
|
| 3096 | * Returns: 0 on deadlock detected, 1 on OK, 2 if another lock with the same
|
| 3097 | * lock class is held but nest_lock is also held, i.e. we rely on the
|
| 3098 | * nest_lock to avoid the deadlock.
|
| 3099 |
|
| 3100 | static int
|
| 3101 | check_deadlock(struct task_struct *curr, struct held_lock *next)
|
| 3102 | {
|
| 3103 | struct lock_class *class;
|
| 3104 | struct held_lock *prev;
|
| 3105 | struct held_lock *nest = NULL;
|
| 3106 | int i;
|
| 3107 |
|
| 3108 | for (i = 0; i < curr->lockdep_depth; i++) {
|
| 3109 | prev = curr->held_locks + i;
|
| 3110 |
|
| 3111 | if (prev->instance == next->nest_lock)
|
| 3112 | nest = prev;
|
| 3113 |
|
| 3114 | if (hlock_class(prev) != hlock_class(next))
|
| 3115 | continue;
|
| 3116 |
|
| 3117 | |
| 3118 | * Allow read-after-read recursion of the same
|
| 3119 | * lock class (i.e. read_lock(lock)+read_lock(lock)):
|
| 3120 |
|
| 3121 | if ((next->read == 2) && prev->read)
|
| 3122 | continue;
|
| 3123 |
|
| 3124 | class = hlock_class(prev);
|
| 3125 |
|
| 3126 | if (class->cmp_fn &&
|
| 3127 | class->cmp_fn(prev->instance, next->instance) < 0)
|
| 3128 | continue;
|
| 3129 |
|
| 3130 | |
| 3131 | * We're holding the nest_lock, which serializes this lock's
|
| 3132 | * nesting behaviour.
|
| 3133 |
|
| 3134 | if (nest)
|
| 3135 | return 2;
|
| 3136 |
|
| 3137 | print_deadlock_bug(curr, prev, next);
|
| 3138 | return 0;
|
| 3139 | }
|
| 3140 | return 1;
|
| 3141 | }
|
| 3142 |
|
| 3143 | |
| 3144 | * There was a chain-cache miss, and we are about to add a new dependency
|
| 3145 | * to a previous lock. We validate the following rules:
|
| 3146 | *
|
| 3147 | * - would the adding of the <prev> -> <next> dependency create a
|
| 3148 | * circular dependency in the graph? [== circular deadlock]
|
| 3149 | *
|
| 3150 | * - does the new prev->next dependency connect any hardirq-safe lock
|
| 3151 | * (in the full backwards-subgraph starting at <prev>) with any
|
| 3152 | * hardirq-unsafe lock (in the full forwards-subgraph starting at
|
| 3153 | * <next>)? [== illegal lock inversion with hardirq contexts]
|
| 3154 | *
|
| 3155 | * - does the new prev->next dependency connect any softirq-safe lock
|
| 3156 | * (in the full backwards-subgraph starting at <prev>) with any
|
| 3157 | * softirq-unsafe lock (in the full forwards-subgraph starting at
|
| 3158 | * <next>)? [== illegal lock inversion with softirq contexts]
|
| 3159 | *
|
| 3160 | * any of these scenarios could lead to a deadlock.
|
| 3161 | *
|
| 3162 | * Then if all the validations pass, we add the forwards and backwards
|
| 3163 | * dependency.
|
| 3164 |
|
| 3165 | static int
|
| 3166 | check_prev_add(struct task_struct *curr, struct held_lock *prev,
|
| 3167 | struct held_lock *next, u16 distance,
|
| 3168 | struct lock_trace **const trace)
|
| 3169 | {
|
| 3170 | struct lock_list *entry;
|
| 3171 | enum bfs_result ret;
|
| 3172 |
|
| 3173 | if (!hlock_class(prev)->key || !hlock_class(next)->key) {
|
| 3174 | |
| 3175 | * The warning statements below may trigger a use-after-free
|
| 3176 | * of the class name. It is better to trigger a use-after free
|
| 3177 | * and to have the class name most of the time instead of not
|
| 3178 | * having the class name available.
|
| 3179 |
|
| 3180 | WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key,
|
| 3181 | "Detected use-after-free of lock class %px/%s\n",
|
| 3182 | hlock_class(prev),
|
| 3183 | hlock_class(prev)->name);
|
| 3184 | WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key,
|
| 3185 | "Detected use-after-free of lock class %px/%s\n",
|
| 3186 | hlock_class(next),
|
| 3187 | hlock_class(next)->name);
|
| 3188 | return 2;
|
| 3189 | }
|
| 3190 |
|
| 3191 | if (prev->class_idx == next->class_idx) {
|
| 3192 | struct lock_class *class = hlock_class(prev);
|
| 3193 |
|
| 3194 | if (class->cmp_fn &&
|
| 3195 | class->cmp_fn(prev->instance, next->instance) < 0)
|
| 3196 | return 2;
|
| 3197 | }
|
| 3198 |
|
| 3199 | |
| 3200 | * Prove that the new <prev> -> <next> dependency would not
|
| 3201 | * create a circular dependency in the graph. (We do this by
|
| 3202 | * a breadth-first search into the graph starting at <next>,
|
| 3203 | * and check whether we can reach <prev>.)
|
| 3204 | *
|
| 3205 | * The search is limited by the size of the circular queue (i.e.,
|
| 3206 | * MAX_CIRCULAR_QUEUE_SIZE) which keeps track of a breadth of nodes
|
| 3207 | * in the graph whose neighbours are to be checked.
|
| 3208 |
|
| 3209 | ret = check_noncircular(next, prev, trace);
|
| 3210 | if (unlikely(bfs_error(ret) || ret == BFS_RMATCH))
|
| 3211 | return 0;
|
| 3212 |
|
| 3213 | if (!check_irq_usage(curr, prev, next))
|
| 3214 | return 0;
|
| 3215 |
|
| 3216 | |
| 3217 | * Is the <prev> -> <next> dependency already present?
|
| 3218 | *
|
| 3219 | * (this may occur even though this is a new chain: consider
|
| 3220 | * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
|
| 3221 | * chains - the second one will be new, but L1 already has
|
| 3222 | * L2 added to its dependency list, due to the first chain.)
|
| 3223 |
|
| 3224 | list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
|
| 3225 | if (entry->class == hlock_class(next)) {
|
| 3226 | if (distance == 1)
|
| 3227 | entry->distance = 1;
|
| 3228 | entry->dep |= calc_dep(prev, next);
|
| 3229 |
|
| 3230 | |
| 3231 | * Also, update the reverse dependency in @next's
|
| 3232 | * ->locks_before list.
|
| 3233 | *
|
| 3234 | * Here we reuse @entry as the cursor, which is fine
|
| 3235 | * because we won't go to the next iteration of the
|
| 3236 | * outer loop:
|
| 3237 | *
|
| 3238 | * For normal cases, we return in the inner loop.
|
| 3239 | *
|
| 3240 | * If we fail to return, we have inconsistency, i.e.
|
| 3241 | * <prev>::locks_after contains <next> while
|
| 3242 | * <next>::locks_before doesn't contain <prev>. In
|
| 3243 | * that case, we return after the inner and indicate
|
| 3244 | * something is wrong.
|
| 3245 |
|
| 3246 | list_for_each_entry(entry, &hlock_class(next)->locks_before, entry) {
|
| 3247 | if (entry->class == hlock_class(prev)) {
|
| 3248 | if (distance == 1)
|
| 3249 | entry->distance = 1;
|
| 3250 | entry->dep |= calc_depb(prev, next);
|
| 3251 | return 1;
|
| 3252 | }
|
| 3253 | }
|
| 3254 |
|
| 3255 |
|
| 3256 | return 0;
|
| 3257 | }
|
| 3258 | }
|
| 3259 |
|
| 3260 | |
| 3261 | * Is the <prev> -> <next> link redundant?
|
| 3262 |
|
| 3263 | ret = check_redundant(prev, next);
|
| 3264 | if (bfs_error(ret))
|
| 3265 | return 0;
|
| 3266 | else if (ret == BFS_RMATCH)
|
| 3267 | return 2;
|
| 3268 |
|
| 3269 | if (!*trace) {
|
| 3270 | *trace = save_trace();
|
| 3271 | if (!*trace)
|
| 3272 | return 0;
|
| 3273 | }
|
| 3274 |
|
| 3275 | |
| 3276 | * Ok, all validations passed, add the new lock
|
| 3277 | * to the previous lock's dependency list:
|
| 3278 |
|
| 3279 | ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
|
| 3280 | &hlock_class(prev)->locks_after, distance,
|
| 3281 | calc_dep(prev, next), *trace);
|
| 3282 |
|
| 3283 | if (!ret)
|
| 3284 | return 0;
|
| 3285 |
|
| 3286 | ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
|
| 3287 | &hlock_class(next)->locks_before, distance,
|
| 3288 | calc_depb(prev, next), *trace);
|
| 3289 | if (!ret)
|
| 3290 | return 0;
|
| 3291 |
|
| 3292 | return 2;
|
| 3293 | }
|
| 3294 |
|
| 3295 | |
| 3296 | * Add the dependency to all directly-previous locks that are 'relevant'.
|
| 3297 | * The ones that are relevant are (in increasing distance from curr):
|
| 3298 | * all consecutive trylock entries and the final non-trylock entry - or
|
| 3299 | * the end of this context's lock-chain - whichever comes first.
|
| 3300 |
|
| 3301 | static int
|
| 3302 | check_prevs_add(struct task_struct *curr, struct held_lock *next)
|
| 3303 | {
|
| 3304 | struct lock_trace *trace = NULL;
|
| 3305 | int depth = curr->lockdep_depth;
|
| 3306 | struct held_lock *hlock;
|
| 3307 |
|
| 3308 | |
| 3309 | * Debugging checks.
|
| 3310 | *
|
| 3311 | * Depth must not be zero for a non-head lock:
|
| 3312 |
|
| 3313 | if (!depth)
|
| 3314 | goto out_bug;
|
| 3315 | |
| 3316 | * At least two relevant locks must exist for this
|
| 3317 | * to be a head:
|
| 3318 |
|
| 3319 | if (curr->held_locks[depth].irq_context !=
|
| 3320 | curr->held_locks[depth-1].irq_context)
|
| 3321 | goto out_bug;
|
| 3322 |
|
| 3323 | for (;;) {
|
| 3324 | u16 distance = curr->lockdep_depth - depth + 1;
|
| 3325 | hlock = curr->held_locks + depth - 1;
|
| 3326 |
|
| 3327 | if (hlock->check) {
|
| 3328 | int ret = check_prev_add(curr, hlock, next, distance, &trace);
|
| 3329 | if (!ret)
|
| 3330 | return 0;
|
| 3331 |
|
| 3332 | |
| 3333 | * Stop after the first non-trylock entry,
|
| 3334 | * as non-trylock entries have added their
|
| 3335 | * own direct dependencies already, so this
|
| 3336 | * lock is connected to them indirectly:
|
| 3337 |
|
| 3338 | if (!hlock->trylock)
|
| 3339 | break;
|
| 3340 | }
|
| 3341 |
|
| 3342 | depth--;
|
| 3343 | |
| 3344 | * End of lock-stack?
|
| 3345 |
|
| 3346 | if (!depth)
|
| 3347 | break;
|
| 3348 | |
| 3349 | * Stop the search if we cross into another context:
|
| 3350 |
|
| 3351 | if (curr->held_locks[depth].irq_context !=
|
| 3352 | curr->held_locks[depth-1].irq_context)
|
| 3353 | break;
|
| 3354 | }
|
| 3355 | return 1;
|
| 3356 | out_bug:
|
| 3357 | if (!debug_locks_off_graph_unlock())
|
| 3358 | return 0;
|
| 3359 |
|
| 3360 | |
| 3361 | * Clearly we all shouldn't be here, but since we made it we
|
| 3362 | * can reliable say we messed up our state. See the above two
|
| 3363 | * gotos for reasons why we could possibly end up here.
|
| 3364 |
|
| 3365 | WARN_ON(1);
|
| 3366 |
|
| 3367 | return 0;
|
| 3368 | }
|
| 3369 |
|
| 3370 | struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
|
| 3371 | static DECLARE_BITMAP(lock_chains_in_use, MAX_LOCKDEP_CHAINS);
|
| 3372 | static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
|
| 3373 | unsigned long nr_zapped_lock_chains;
|
| 3374 | unsigned int nr_free_chain_hlocks;
|
| 3375 | unsigned int nr_lost_chain_hlocks;
|
| 3376 | unsigned int nr_large_chain_blocks;
|
| 3377 |
|
| 3378 | |
| 3379 | * The first 2 chain_hlocks entries in the chain block in the bucket
|
| 3380 | * list contains the following meta data:
|
| 3381 | *
|
| 3382 | * entry[0]:
|
| 3383 | * Bit 15 - always set to 1 (it is not a class index)
|
| 3384 | * Bits 0-14 - upper 15 bits of the next block index
|
| 3385 | * entry[1] - lower 16 bits of next block index
|
| 3386 | *
|
| 3387 | * A next block index of all 1 bits means it is the end of the list.
|
| 3388 | *
|
| 3389 | * On the unsized bucket (bucket-0), the 3rd and 4th entries contain
|
| 3390 | * the chain block size:
|
| 3391 | *
|
| 3392 | * entry[2] - upper 16 bits of the chain block size
|
| 3393 | * entry[3] - lower 16 bits of the chain block size
|
| 3394 |
|
| 3395 | #define MAX_CHAIN_BUCKETS 16
|
| 3396 | #define CHAIN_BLK_FLAG (1U << 15)
|
| 3397 | #define CHAIN_BLK_LIST_END 0xFFFFU
|
| 3398 |
|
| 3399 | static int chain_block_buckets[MAX_CHAIN_BUCKETS];
|
| 3400 |
|
| 3401 | static inline int size_to_bucket(int size)
|
| 3402 | {
|
| 3403 | if (size > MAX_CHAIN_BUCKETS)
|
| 3404 | return 0;
|
| 3405 |
|
| 3406 | return size - 1;
|
| 3407 | }
|
| 3408 |
|
| 3409 | |
| 3410 | * Iterate all the chain blocks in a bucket.
|
| 3411 |
|
| 3412 | #define for_each_chain_block(bucket, prev, curr) \
|
| 3413 | for ((prev) = -1, (curr) = chain_block_buckets[bucket]; \
|
| 3414 | (curr) >= 0; \
|
| 3415 | (prev) = (curr), (curr) = chain_block_next(curr))
|
| 3416 |
|
| 3417 | |
| 3418 | * next block or -1
|
| 3419 |
|
| 3420 | static inline int chain_block_next(int offset)
|
| 3421 | {
|
| 3422 | int next = chain_hlocks[offset];
|
| 3423 |
|
| 3424 | WARN_ON_ONCE(!(next & CHAIN_BLK_FLAG));
|
| 3425 |
|
| 3426 | if (next == CHAIN_BLK_LIST_END)
|
| 3427 | return -1;
|
| 3428 |
|
| 3429 | next &= ~CHAIN_BLK_FLAG;
|
| 3430 | next <<= 16;
|
| 3431 | next |= chain_hlocks[offset + 1];
|
| 3432 |
|
| 3433 | return next;
|
| 3434 | }
|
| 3435 |
|
| 3436 | |
| 3437 | * bucket-0 only
|
| 3438 |
|
| 3439 | static inline int chain_block_size(int offset)
|
| 3440 | {
|
| 3441 | return (chain_hlocks[offset + 2] << 16) | chain_hlocks[offset + 3];
|
| 3442 | }
|
| 3443 |
|
| 3444 | static inline void init_chain_block(int offset, int next, int bucket, int size)
|
| 3445 | {
|
| 3446 | chain_hlocks[offset] = (next >> 16) | CHAIN_BLK_FLAG;
|
| 3447 | chain_hlocks[offset + 1] = (u16)next;
|
| 3448 |
|
| 3449 | if (size && !bucket) {
|
| 3450 | chain_hlocks[offset + 2] = size >> 16;
|
| 3451 | chain_hlocks[offset + 3] = (u16)size;
|
| 3452 | }
|
| 3453 | }
|
| 3454 |
|
| 3455 | static inline void add_chain_block(int offset, int size)
|
| 3456 | {
|
| 3457 | int bucket = size_to_bucket(size);
|
| 3458 | int next = chain_block_buckets[bucket];
|
| 3459 | int prev, curr;
|
| 3460 |
|
| 3461 | if (unlikely(size < 2)) {
|
| 3462 | |
| 3463 | * We can't store single entries on the freelist. Leak them.
|
| 3464 | *
|
| 3465 | * One possible way out would be to uniquely mark them, other
|
| 3466 | * than with CHAIN_BLK_FLAG, such that we can recover them when
|
| 3467 | * the block before it is re-added.
|
| 3468 |
|
| 3469 | if (size)
|
| 3470 | nr_lost_chain_hlocks++;
|
| 3471 | return;
|
| 3472 | }
|
| 3473 |
|
| 3474 | nr_free_chain_hlocks += size;
|
| 3475 | if (!bucket) {
|
| 3476 | nr_large_chain_blocks++;
|
| 3477 |
|
| 3478 | |
| 3479 | * Variable sized, sort large to small.
|
| 3480 |
|
| 3481 | for_each_chain_block(0, prev, curr) {
|
| 3482 | if (size >= chain_block_size(curr))
|
| 3483 | break;
|
| 3484 | }
|
| 3485 | init_chain_block(offset, curr, 0, size);
|
| 3486 | if (prev < 0)
|
| 3487 | chain_block_buckets[0] = offset;
|
| 3488 | else
|
| 3489 | init_chain_block(prev, offset, 0, 0);
|
| 3490 | return;
|
| 3491 | }
|
| 3492 | |
| 3493 | * Fixed size, add to head.
|
| 3494 |
|
| 3495 | init_chain_block(offset, next, bucket, size);
|
| 3496 | chain_block_buckets[bucket] = offset;
|
| 3497 | }
|
| 3498 |
|
| 3499 | |
| 3500 | * Only the first block in the list can be deleted.
|
| 3501 | *
|
| 3502 | * For the variable size bucket[0], the first block (the largest one) is
|
| 3503 | * returned, broken up and put back into the pool. So if a chain block of
|
| 3504 | * length > MAX_CHAIN_BUCKETS is ever used and zapped, it will just be
|
| 3505 | * queued up after the primordial chain block and never be used until the
|
| 3506 | * hlock entries in the primordial chain block is almost used up. That
|
| 3507 | * causes fragmentation and reduce allocation efficiency. That can be
|
| 3508 | * monitored by looking at the "large chain blocks" number in lockdep_stats.
|
| 3509 |
|
| 3510 | static inline void del_chain_block(int bucket, int size, int next)
|
| 3511 | {
|
| 3512 | nr_free_chain_hlocks -= size;
|
| 3513 | chain_block_buckets[bucket] = next;
|
| 3514 |
|
| 3515 | if (!bucket)
|
| 3516 | nr_large_chain_blocks--;
|
| 3517 | }
|
| 3518 |
|
| 3519 | static void init_chain_block_buckets(void)
|
| 3520 | {
|
| 3521 | int i;
|
| 3522 |
|
| 3523 | for (i = 0; i < MAX_CHAIN_BUCKETS; i++)
|
| 3524 | chain_block_buckets[i] = -1;
|
| 3525 |
|
| 3526 | add_chain_block(0, ARRAY_SIZE(chain_hlocks));
|
| 3527 | }
|
| 3528 |
|
| 3529 | |
| 3530 | * Return offset of a chain block of the right size or -1 if not found.
|
| 3531 | *
|
| 3532 | * Fairly simple worst-fit allocator with the addition of a number of size
|
| 3533 | * specific free lists.
|
| 3534 |
|
| 3535 | static int alloc_chain_hlocks(int req)
|
| 3536 | {
|
| 3537 | int bucket, curr, size;
|
| 3538 |
|
| 3539 | |
| 3540 | * We rely on the MSB to act as an escape bit to denote freelist
|
| 3541 | * pointers. Make sure this bit isn't set in 'normal' class_idx usage.
|
| 3542 |
|
| 3543 | BUILD_BUG_ON((MAX_LOCKDEP_KEYS-1) & CHAIN_BLK_FLAG);
|
| 3544 |
|
| 3545 | init_data_structures_once();
|
| 3546 |
|
| 3547 | if (nr_free_chain_hlocks < req)
|
| 3548 | return -1;
|
| 3549 |
|
| 3550 | |
| 3551 | * We require a minimum of 2 (u16) entries to encode a freelist
|
| 3552 | * 'pointer'.
|
| 3553 |
|
| 3554 | req = max(req, 2);
|
| 3555 | bucket = size_to_bucket(req);
|
| 3556 | curr = chain_block_buckets[bucket];
|
| 3557 |
|
| 3558 | if (bucket) {
|
| 3559 | if (curr >= 0) {
|
| 3560 | del_chain_block(bucket, req, chain_block_next(curr));
|
| 3561 | return curr;
|
| 3562 | }
|
| 3563 |
|
| 3564 | curr = chain_block_buckets[0];
|
| 3565 | }
|
| 3566 |
|
| 3567 | |
| 3568 | * The variable sized freelist is sorted by size; the first entry is
|
| 3569 | * the largest. Use it if it fits.
|
| 3570 |
|
| 3571 | if (curr >= 0) {
|
| 3572 | size = chain_block_size(curr);
|
| 3573 | if (likely(size >= req)) {
|
| 3574 | del_chain_block(0, size, chain_block_next(curr));
|
| 3575 | if (size > req)
|
| 3576 | add_chain_block(curr + req, size - req);
|
| 3577 | return curr;
|
| 3578 | }
|
| 3579 | }
|
| 3580 |
|
| 3581 | |
| 3582 | * Last resort, split a block in a larger sized bucket.
|
| 3583 |
|
| 3584 | for (size = MAX_CHAIN_BUCKETS; size > req; size--) {
|
| 3585 | bucket = size_to_bucket(size);
|
| 3586 | curr = chain_block_buckets[bucket];
|
| 3587 | if (curr < 0)
|
| 3588 | continue;
|
| 3589 |
|
| 3590 | del_chain_block(bucket, size, chain_block_next(curr));
|
| 3591 | add_chain_block(curr + req, size - req);
|
| 3592 | return curr;
|
| 3593 | }
|
| 3594 |
|
| 3595 | return -1;
|
| 3596 | }
|
| 3597 |
|
| 3598 | static inline void free_chain_hlocks(int base, int size)
|
| 3599 | {
|
| 3600 | add_chain_block(base, max(size, 2));
|
| 3601 | }
|
| 3602 |
|
| 3603 | struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
|
| 3604 | {
|
| 3605 | u16 chain_hlock = chain_hlocks[chain->base + i];
|
| 3606 | unsigned int class_idx = chain_hlock_class_idx(chain_hlock);
|
| 3607 |
|
| 3608 | return lock_classes + class_idx;
|
| 3609 | }
|
| 3610 |
|
| 3611 | |
| 3612 | * Returns the index of the first held_lock of the current chain
|
| 3613 |
|
| 3614 | static inline int get_first_held_lock(struct task_struct *curr,
|
| 3615 | struct held_lock *hlock)
|
| 3616 | {
|
| 3617 | int i;
|
| 3618 | struct held_lock *hlock_curr;
|
| 3619 |
|
| 3620 | for (i = curr->lockdep_depth - 1; i >= 0; i--) {
|
| 3621 | hlock_curr = curr->held_locks + i;
|
| 3622 | if (hlock_curr->irq_context != hlock->irq_context)
|
| 3623 | break;
|
| 3624 |
|
| 3625 | }
|
| 3626 |
|
| 3627 | return ++i;
|
| 3628 | }
|
| 3629 |
|
| 3630 | #ifdef CONFIG_DEBUG_LOCKDEP
|
| 3631 | |
| 3632 | * Returns the next chain_key iteration
|
| 3633 |
|
| 3634 | static u64 print_chain_key_iteration(u16 hlock_id, u64 chain_key)
|
| 3635 | {
|
| 3636 | u64 new_chain_key = iterate_chain_key(chain_key, hlock_id);
|
| 3637 |
|
| 3638 | printk(" hlock_id:%d -> chain_key:%016Lx",
|
| 3639 | (unsigned int)hlock_id,
|
| 3640 | (unsigned long long)new_chain_key);
|
| 3641 | return new_chain_key;
|
| 3642 | }
|
| 3643 |
|
| 3644 | static void
|
| 3645 | print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
|
| 3646 | {
|
| 3647 | struct held_lock *hlock;
|
| 3648 | u64 chain_key = INITIAL_CHAIN_KEY;
|
| 3649 | int depth = curr->lockdep_depth;
|
| 3650 | int i = get_first_held_lock(curr, hlock_next);
|
| 3651 |
|
| 3652 | printk("depth: %u (irq_context %u)\n", depth - i + 1,
|
| 3653 | hlock_next->irq_context);
|
| 3654 | for (; i < depth; i++) {
|
| 3655 | hlock = curr->held_locks + i;
|
| 3656 | chain_key = print_chain_key_iteration(hlock_id(hlock), chain_key);
|
| 3657 |
|
| 3658 | print_lock(hlock);
|
| 3659 | }
|
| 3660 |
|
| 3661 | print_chain_key_iteration(hlock_id(hlock_next), chain_key);
|
| 3662 | print_lock(hlock_next);
|
| 3663 | }
|
| 3664 |
|
| 3665 | static void print_chain_keys_chain(struct lock_chain *chain)
|
| 3666 | {
|
| 3667 | int i;
|
| 3668 | u64 chain_key = INITIAL_CHAIN_KEY;
|
| 3669 | u16 hlock_id;
|
| 3670 |
|
| 3671 | printk("depth: %u\n", chain->depth);
|
| 3672 | for (i = 0; i < chain->depth; i++) {
|
| 3673 | hlock_id = chain_hlocks[chain->base + i];
|
| 3674 | chain_key = print_chain_key_iteration(hlock_id, chain_key);
|
| 3675 |
|
| 3676 | print_lock_name(NULL, lock_classes + chain_hlock_class_idx(hlock_id));
|
| 3677 | printk("\n");
|
| 3678 | }
|
| 3679 | }
|
| 3680 |
|
| 3681 | static void print_collision(struct task_struct *curr,
|
| 3682 | struct held_lock *hlock_next,
|
| 3683 | struct lock_chain *chain)
|
| 3684 | {
|
| 3685 | nbcon_cpu_emergency_enter();
|
| 3686 |
|
| 3687 | pr_warn("\n");
|
| 3688 | pr_warn("============================\n");
|
| 3689 | pr_warn("WARNING: chain_key collision\n");
|
| 3690 | print_kernel_ident();
|
| 3691 | pr_warn("----------------------------\n");
|
| 3692 | pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
|
| 3693 | pr_warn("Hash chain already cached but the contents don't match!\n");
|
| 3694 |
|
| 3695 | pr_warn("Held locks:");
|
| 3696 | print_chain_keys_held_locks(curr, hlock_next);
|
| 3697 |
|
| 3698 | pr_warn("Locks in cached chain:");
|
| 3699 | print_chain_keys_chain(chain);
|
| 3700 |
|
| 3701 | pr_warn("\nstack backtrace:\n");
|
| 3702 | dump_stack();
|
| 3703 |
|
| 3704 | nbcon_cpu_emergency_exit();
|
| 3705 | }
|
| 3706 | #endif
|
| 3707 |
|
| 3708 | |
| 3709 | * Checks whether the chain and the current held locks are consistent
|
| 3710 | * in depth and also in content. If they are not it most likely means
|
| 3711 | * that there was a collision during the calculation of the chain_key.
|
| 3712 | * Returns: 0 not passed, 1 passed
|
| 3713 |
|
| 3714 | static int check_no_collision(struct task_struct *curr,
|
| 3715 | struct held_lock *hlock,
|
| 3716 | struct lock_chain *chain)
|
| 3717 | {
|
| 3718 | #ifdef CONFIG_DEBUG_LOCKDEP
|
| 3719 | int i, j, id;
|
| 3720 |
|
| 3721 | i = get_first_held_lock(curr, hlock);
|
| 3722 |
|
| 3723 | if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
|
| 3724 | print_collision(curr, hlock, chain);
|
| 3725 | return 0;
|
| 3726 | }
|
| 3727 |
|
| 3728 | for (j = 0; j < chain->depth - 1; j++, i++) {
|
| 3729 | id = hlock_id(&curr->held_locks[i]);
|
| 3730 |
|
| 3731 | if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
|
| 3732 | print_collision(curr, hlock, chain);
|
| 3733 | return 0;
|
| 3734 | }
|
| 3735 | }
|
| 3736 | #endif
|
| 3737 | return 1;
|
| 3738 | }
|
| 3739 |
|
| 3740 | |
| 3741 | * Given an index that is >= -1, return the index of the next lock chain.
|
| 3742 | * Return -2 if there is no next lock chain.
|
| 3743 |
|
| 3744 | long lockdep_next_lockchain(long i)
|
| 3745 | {
|
| 3746 | i = find_next_bit(lock_chains_in_use, ARRAY_SIZE(lock_chains), i + 1);
|
| 3747 | return i < ARRAY_SIZE(lock_chains) ? i : -2;
|
| 3748 | }
|
| 3749 |
|
| 3750 | unsigned long lock_chain_count(void)
|
| 3751 | {
|
| 3752 | return bitmap_weight(lock_chains_in_use, ARRAY_SIZE(lock_chains));
|
| 3753 | }
|
| 3754 |
|
| 3755 |
|
| 3756 | static struct lock_chain *alloc_lock_chain(void)
|
| 3757 | {
|
| 3758 | int idx = find_first_zero_bit(lock_chains_in_use,
|
| 3759 | ARRAY_SIZE(lock_chains));
|
| 3760 |
|
| 3761 | if (unlikely(idx >= ARRAY_SIZE(lock_chains)))
|
| 3762 | return NULL;
|
| 3763 | __set_bit(idx, lock_chains_in_use);
|
| 3764 | return lock_chains + idx;
|
| 3765 | }
|
| 3766 |
|
| 3767 | |
| 3768 | * Adds a dependency chain into chain hashtable. And must be called with
|
| 3769 | * graph_lock held.
|
| 3770 | *
|
| 3771 | * Return 0 if fail, and graph_lock is released.
|
| 3772 | * Return 1 if succeed, with graph_lock held.
|
| 3773 |
|
| 3774 | static inline int add_chain_cache(struct task_struct *curr,
|
| 3775 | struct held_lock *hlock,
|
| 3776 | u64 chain_key)
|
| 3777 | {
|
| 3778 | struct hlist_head *hash_head = chainhashentry(chain_key);
|
| 3779 | struct lock_chain *chain;
|
| 3780 | int i, j;
|
| 3781 |
|
| 3782 | |
| 3783 | * The caller must hold the graph lock, ensure we've got IRQs
|
| 3784 | * disabled to make this an IRQ-safe lock.. for recursion reasons
|
| 3785 | * lockdep won't complain about its own locking errors.
|
| 3786 |
|
| 3787 | if (lockdep_assert_locked())
|
| 3788 | return 0;
|
| 3789 |
|
| 3790 | chain = alloc_lock_chain();
|
| 3791 | if (!chain) {
|
| 3792 | if (!debug_locks_off_graph_unlock())
|
| 3793 | return 0;
|
| 3794 |
|
| 3795 | nbcon_cpu_emergency_enter();
|
| 3796 | print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
|
| 3797 | dump_stack();
|
| 3798 | nbcon_cpu_emergency_exit();
|
| 3799 | return 0;
|
| 3800 | }
|
| 3801 | chain->chain_key = chain_key;
|
| 3802 | chain->irq_context = hlock->irq_context;
|
| 3803 | i = get_first_held_lock(curr, hlock);
|
| 3804 | chain->depth = curr->lockdep_depth + 1 - i;
|
| 3805 |
|
| 3806 | BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
|
| 3807 | BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
|
| 3808 | BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
|
| 3809 |
|
| 3810 | j = alloc_chain_hlocks(chain->depth);
|
| 3811 | if (j < 0) {
|
| 3812 | if (!debug_locks_off_graph_unlock())
|
| 3813 | return 0;
|
| 3814 |
|
| 3815 | nbcon_cpu_emergency_enter();
|
| 3816 | print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
|
| 3817 | dump_stack();
|
| 3818 | nbcon_cpu_emergency_exit();
|
| 3819 | return 0;
|
| 3820 | }
|
| 3821 |
|
| 3822 | chain->base = j;
|
| 3823 | for (j = 0; j < chain->depth - 1; j++, i++) {
|
| 3824 | int lock_id = hlock_id(curr->held_locks + i);
|
| 3825 |
|
| 3826 | chain_hlocks[chain->base + j] = lock_id;
|
| 3827 | }
|
| 3828 | chain_hlocks[chain->base + j] = hlock_id(hlock);
|
| 3829 | hlist_add_head_rcu(&chain->entry, hash_head);
|
| 3830 | debug_atomic_inc(chain_lookup_misses);
|
| 3831 | inc_chains(chain->irq_context);
|
| 3832 |
|
| 3833 | return 1;
|
| 3834 | }
|
| 3835 |
|
| 3836 | |
| 3837 | * Look up a dependency chain. Must be called with either the graph lock or
|
| 3838 | * the RCU read lock held.
|
| 3839 |
|
| 3840 | static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
|
| 3841 | {
|
| 3842 | struct hlist_head *hash_head = chainhashentry(chain_key);
|
| 3843 | struct lock_chain *chain;
|
| 3844 |
|
| 3845 | hlist_for_each_entry_rcu(chain, hash_head, entry) {
|
| 3846 | if (READ_ONCE(chain->chain_key) == chain_key) {
|
| 3847 | debug_atomic_inc(chain_lookup_hits);
|
| 3848 | return chain;
|
| 3849 | }
|
| 3850 | }
|
| 3851 | return NULL;
|
| 3852 | }
|
| 3853 |
|
| 3854 | |
| 3855 | * If the key is not present yet in dependency chain cache then
|
| 3856 | * add it and return 1 - in this case the new dependency chain is
|
| 3857 | * validated. If the key is already hashed, return 0.
|
| 3858 | * (On return with 1 graph_lock is held.)
|
| 3859 |
|
| 3860 | static inline int lookup_chain_cache_add(struct task_struct *curr,
|
| 3861 | struct held_lock *hlock,
|
| 3862 | u64 chain_key)
|
| 3863 | {
|
| 3864 | struct lock_class *class = hlock_class(hlock);
|
| 3865 | struct lock_chain *chain = lookup_chain_cache(chain_key);
|
| 3866 |
|
| 3867 | if (chain) {
|
| 3868 | cache_hit:
|
| 3869 | if (!check_no_collision(curr, hlock, chain))
|
| 3870 | return 0;
|
| 3871 |
|
| 3872 | if (very_verbose(class)) {
|
| 3873 | printk("\nhash chain already cached, key: "
|
| 3874 | "%016Lx tail class: [%px] %s\n",
|
| 3875 | (unsigned long long)chain_key,
|
| 3876 | class->key, class->name);
|
| 3877 | }
|
| 3878 |
|
| 3879 | return 0;
|
| 3880 | }
|
| 3881 |
|
| 3882 | if (very_verbose(class)) {
|
| 3883 | printk("\nnew hash chain, key: %016Lx tail class: [%px] %s\n",
|
| 3884 | (unsigned long long)chain_key, class->key, class->name);
|
| 3885 | }
|
| 3886 |
|
| 3887 | if (!graph_lock())
|
| 3888 | return 0;
|
| 3889 |
|
| 3890 | |
| 3891 | * We have to walk the chain again locked - to avoid duplicates:
|
| 3892 |
|
| 3893 | chain = lookup_chain_cache(chain_key);
|
| 3894 | if (chain) {
|
| 3895 | graph_unlock();
|
| 3896 | goto cache_hit;
|
| 3897 | }
|
| 3898 |
|
| 3899 | if (!add_chain_cache(curr, hlock, chain_key))
|
| 3900 | return 0;
|
| 3901 |
|
| 3902 | return 1;
|
| 3903 | }
|
| 3904 |
|
| 3905 | static int validate_chain(struct task_struct *curr,
|
| 3906 | struct held_lock *hlock,
|
| 3907 | int chain_head, u64 chain_key)
|
| 3908 | {
|
| 3909 | |
| 3910 | * Trylock needs to maintain the stack of held locks, but it
|
| 3911 | * does not add new dependencies, because trylock can be done
|
| 3912 | * in any order.
|
| 3913 | *
|
| 3914 | * We look up the chain_key and do the O(N^2) check and update of
|
| 3915 | * the dependencies only if this is a new dependency chain.
|
| 3916 | * (If lookup_chain_cache_add() return with 1 it acquires
|
| 3917 | * graph_lock for us)
|
| 3918 |
|
| 3919 | if (!hlock->trylock && hlock->check &&
|
| 3920 | lookup_chain_cache_add(curr, hlock, chain_key)) {
|
| 3921 | |
| 3922 | * Check whether last held lock:
|
| 3923 | *
|
| 3924 | * - is irq-safe, if this lock is irq-unsafe
|
| 3925 | * - is softirq-safe, if this lock is hardirq-unsafe
|
| 3926 | *
|
| 3927 | * And check whether the new lock's dependency graph
|
| 3928 | * could lead back to the previous lock:
|
| 3929 | *
|
| 3930 | * - within the current held-lock stack
|
| 3931 | * - across our accumulated lock dependency records
|
| 3932 | *
|
| 3933 | * any of these scenarios could lead to a deadlock.
|
| 3934 |
|
| 3935 | |
| 3936 | * The simple case: does the current hold the same lock
|
| 3937 | * already?
|
| 3938 |
|
| 3939 | int ret = check_deadlock(curr, hlock);
|
| 3940 |
|
| 3941 | if (!ret)
|
| 3942 | return 0;
|
| 3943 | |
| 3944 | * Add dependency only if this lock is not the head
|
| 3945 | * of the chain, and if the new lock introduces no more
|
| 3946 | * lock dependency (because we already hold a lock with the
|
| 3947 | * same lock class) nor deadlock (because the nest_lock
|
| 3948 | * serializes nesting locks), see the comments for
|
| 3949 | * check_deadlock().
|
| 3950 |
|
| 3951 | if (!chain_head && ret != 2) {
|
| 3952 | if (!check_prevs_add(curr, hlock))
|
| 3953 | return 0;
|
| 3954 | }
|
| 3955 |
|
| 3956 | graph_unlock();
|
| 3957 | } else {
|
| 3958 |
|
| 3959 | if (unlikely(!debug_locks))
|
| 3960 | return 0;
|
| 3961 | }
|
| 3962 |
|
| 3963 | return 1;
|
| 3964 | }
|
| 3965 | #else
|
| 3966 | static inline int validate_chain(struct task_struct *curr,
|
| 3967 | struct held_lock *hlock,
|
| 3968 | int chain_head, u64 chain_key)
|
| 3969 | {
|
| 3970 | return 1;
|
| 3971 | }
|
| 3972 |
|
| 3973 | static void init_chain_block_buckets(void) { }
|
| 3974 | #endif
|
| 3975 |
|
| 3976 | |
| 3977 | * We are building curr_chain_key incrementally, so double-check
|
| 3978 | * it from scratch, to make sure that it's done correctly:
|
| 3979 |
|
| 3980 | static void check_chain_key(struct task_struct *curr)
|
| 3981 | {
|
| 3982 | #ifdef CONFIG_DEBUG_LOCKDEP
|
| 3983 | struct held_lock *hlock, *prev_hlock = NULL;
|
| 3984 | unsigned int i;
|
| 3985 | u64 chain_key = INITIAL_CHAIN_KEY;
|
| 3986 |
|
| 3987 | for (i = 0; i < curr->lockdep_depth; i++) {
|
| 3988 | hlock = curr->held_locks + i;
|
| 3989 | if (chain_key != hlock->prev_chain_key) {
|
| 3990 | debug_locks_off();
|
| 3991 | |
| 3992 | * We got mighty confused, our chain keys don't match
|
| 3993 | * with what we expect, someone trample on our task state?
|
| 3994 |
|
| 3995 | WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
|
| 3996 | curr->lockdep_depth, i,
|
| 3997 | (unsigned long long)chain_key,
|
| 3998 | (unsigned long long)hlock->prev_chain_key);
|
| 3999 | return;
|
| 4000 | }
|
| 4001 |
|
| 4002 | |
| 4003 | * hlock->class_idx can't go beyond MAX_LOCKDEP_KEYS, but is
|
| 4004 | * it registered lock class index?
|
| 4005 |
|
| 4006 | if (DEBUG_LOCKS_WARN_ON(!test_bit(hlock->class_idx, lock_classes_in_use)))
|
| 4007 | return;
|
| 4008 |
|
| 4009 | if (prev_hlock && (prev_hlock->irq_context !=
|
| 4010 | hlock->irq_context))
|
| 4011 | chain_key = INITIAL_CHAIN_KEY;
|
| 4012 | chain_key = iterate_chain_key(chain_key, hlock_id(hlock));
|
| 4013 | prev_hlock = hlock;
|
| 4014 | }
|
| 4015 | if (chain_key != curr->curr_chain_key) {
|
| 4016 | debug_locks_off();
|
| 4017 | |
| 4018 | * More smoking hash instead of calculating it, damn see these
|
| 4019 | * numbers float.. I bet that a pink elephant stepped on my memory.
|
| 4020 |
|
| 4021 | WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
|
| 4022 | curr->lockdep_depth, i,
|
| 4023 | (unsigned long long)chain_key,
|
| 4024 | (unsigned long long)curr->curr_chain_key);
|
| 4025 | }
|
| 4026 | #endif
|
| 4027 | }
|
| 4028 |
|
| 4029 | #ifdef CONFIG_PROVE_LOCKING
|
| 4030 | static int mark_lock(struct task_struct *curr, struct held_lock *this,
|
| 4031 | enum lock_usage_bit new_bit);
|
| 4032 |
|
| 4033 | static void print_usage_bug_scenario(struct held_lock *lock)
|
| 4034 | {
|
| 4035 | struct lock_class *class = hlock_class(lock);
|
| 4036 |
|
| 4037 | printk(" Possible unsafe locking scenario:\n\n");
|
| 4038 | printk(" CPU0\n");
|
| 4039 | printk(" ----\n");
|
| 4040 | printk(" lock(");
|
| 4041 | __print_lock_name(lock, class);
|
| 4042 | printk(KERN_CONT ");\n");
|
| 4043 | printk(" <Interrupt>\n");
|
| 4044 | printk(" lock(");
|
| 4045 | __print_lock_name(lock, class);
|
| 4046 | printk(KERN_CONT ");\n");
|
| 4047 | printk("\n *** DEADLOCK ***\n\n");
|
| 4048 | }
|
| 4049 |
|
| 4050 | static void
|
| 4051 | print_usage_bug(struct task_struct *curr, struct held_lock *this,
|
| 4052 | enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
|
| 4053 | {
|
| 4054 | if (!debug_locks_off() || debug_locks_silent)
|
| 4055 | return;
|
| 4056 |
|
| 4057 | nbcon_cpu_emergency_enter();
|
| 4058 |
|
| 4059 | pr_warn("\n");
|
| 4060 | pr_warn("================================\n");
|
| 4061 | pr_warn("WARNING: inconsistent lock state\n");
|
| 4062 | print_kernel_ident();
|
| 4063 | pr_warn("--------------------------------\n");
|
| 4064 |
|
| 4065 | pr_warn("inconsistent {%s} -> {%s} usage.\n",
|
| 4066 | usage_str[prev_bit], usage_str[new_bit]);
|
| 4067 |
|
| 4068 | pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
|
| 4069 | curr->comm, task_pid_nr(curr),
|
| 4070 | lockdep_hardirq_context(), hardirq_count() >> HARDIRQ_SHIFT,
|
| 4071 | lockdep_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
|
| 4072 | lockdep_hardirqs_enabled(),
|
| 4073 | lockdep_softirqs_enabled(curr));
|
| 4074 | print_lock(this);
|
| 4075 |
|
| 4076 | pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
|
| 4077 | print_lock_trace(hlock_class(this)->usage_traces[prev_bit], 1);
|
| 4078 |
|
| 4079 | print_irqtrace_events(curr);
|
| 4080 | pr_warn("\nother info that might help us debug this:\n");
|
| 4081 | print_usage_bug_scenario(this);
|
| 4082 |
|
| 4083 | lockdep_print_held_locks(curr);
|
| 4084 |
|
| 4085 | pr_warn("\nstack backtrace:\n");
|
| 4086 | dump_stack();
|
| 4087 |
|
| 4088 | nbcon_cpu_emergency_exit();
|
| 4089 | }
|
| 4090 |
|
| 4091 | |
| 4092 | * Print out an error if an invalid bit is set:
|
| 4093 |
|
| 4094 | static inline int
|
| 4095 | valid_state(struct task_struct *curr, struct held_lock *this,
|
| 4096 | enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
|
| 4097 | {
|
| 4098 | if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) {
|
| 4099 | graph_unlock();
|
| 4100 | print_usage_bug(curr, this, bad_bit, new_bit);
|
| 4101 | return 0;
|
| 4102 | }
|
| 4103 | return 1;
|
| 4104 | }
|
| 4105 |
|
| 4106 |
|
| 4107 | |
| 4108 | * print irq inversion bug:
|
| 4109 |
|
| 4110 | static void
|
| 4111 | print_irq_inversion_bug(struct task_struct *curr,
|
| 4112 | struct lock_list *root, struct lock_list *other,
|
| 4113 | struct held_lock *this, int forwards,
|
| 4114 | const char *irqclass)
|
| 4115 | {
|
| 4116 | struct lock_list *entry = other;
|
| 4117 | struct lock_list *middle = NULL;
|
| 4118 | int depth;
|
| 4119 |
|
| 4120 | if (!debug_locks_off_graph_unlock() || debug_locks_silent)
|
| 4121 | return;
|
| 4122 |
|
| 4123 | nbcon_cpu_emergency_enter();
|
| 4124 |
|
| 4125 | pr_warn("\n");
|
| 4126 | pr_warn("========================================================\n");
|
| 4127 | pr_warn("WARNING: possible irq lock inversion dependency detected\n");
|
| 4128 | print_kernel_ident();
|
| 4129 | pr_warn("--------------------------------------------------------\n");
|
| 4130 | pr_warn("%s/%d just changed the state of lock:\n",
|
| 4131 | curr->comm, task_pid_nr(curr));
|
| 4132 | print_lock(this);
|
| 4133 | if (forwards)
|
| 4134 | pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
|
| 4135 | else
|
| 4136 | pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
|
| 4137 | print_lock_name(NULL, other->class);
|
| 4138 | pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
|
| 4139 |
|
| 4140 | pr_warn("\nother info that might help us debug this:\n");
|
| 4141 |
|
| 4142 |
|
| 4143 | depth = get_lock_depth(other);
|
| 4144 | do {
|
| 4145 | if (depth == 0 && (entry != root)) {
|
| 4146 | pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
|
| 4147 | break;
|
| 4148 | }
|
| 4149 | middle = entry;
|
| 4150 | entry = get_lock_parent(entry);
|
| 4151 | depth--;
|
| 4152 | } while (entry && entry != root && (depth >= 0));
|
| 4153 | if (forwards)
|
| 4154 | print_irq_lock_scenario(root, other,
|
| 4155 | middle ? middle->class : root->class, other->class);
|
| 4156 | else
|
| 4157 | print_irq_lock_scenario(other, root,
|
| 4158 | middle ? middle->class : other->class, root->class);
|
| 4159 |
|
| 4160 | lockdep_print_held_locks(curr);
|
| 4161 |
|
| 4162 | pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
|
| 4163 | root->trace = save_trace();
|
| 4164 | if (!root->trace)
|
| 4165 | goto out;
|
| 4166 | print_shortest_lock_dependencies(other, root);
|
| 4167 |
|
| 4168 | pr_warn("\nstack backtrace:\n");
|
| 4169 | dump_stack();
|
| 4170 | out:
|
| 4171 | nbcon_cpu_emergency_exit();
|
| 4172 | }
|
| 4173 |
|
| 4174 | |
| 4175 | * Prove that in the forwards-direction subgraph starting at <this>
|
| 4176 | * there is no lock matching <mask>:
|
| 4177 |
|
| 4178 | static int
|
| 4179 | check_usage_forwards(struct task_struct *curr, struct held_lock *this,
|
| 4180 | enum lock_usage_bit bit)
|
| 4181 | {
|
| 4182 | enum bfs_result ret;
|
| 4183 | struct lock_list root;
|
| 4184 | struct lock_list *target_entry;
|
| 4185 | enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK;
|
| 4186 | unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit);
|
| 4187 |
|
| 4188 | bfs_init_root(&root, this);
|
| 4189 | ret = find_usage_forwards(&root, usage_mask, &target_entry);
|
| 4190 | if (bfs_error(ret)) {
|
| 4191 | print_bfs_bug(ret);
|
| 4192 | return 0;
|
| 4193 | }
|
| 4194 | if (ret == BFS_RNOMATCH)
|
| 4195 | return 1;
|
| 4196 |
|
| 4197 |
|
| 4198 | if (target_entry->class->usage_mask & lock_flag(bit)) {
|
| 4199 | print_irq_inversion_bug(curr, &root, target_entry,
|
| 4200 | this, 1, state_name(bit));
|
| 4201 | } else {
|
| 4202 | print_irq_inversion_bug(curr, &root, target_entry,
|
| 4203 | this, 1, state_name(read_bit));
|
| 4204 | }
|
| 4205 |
|
| 4206 | return 0;
|
| 4207 | }
|
| 4208 |
|
| 4209 | |
| 4210 | * Prove that in the backwards-direction subgraph starting at <this>
|
| 4211 | * there is no lock matching <mask>:
|
| 4212 |
|
| 4213 | static int
|
| 4214 | check_usage_backwards(struct task_struct *curr, struct held_lock *this,
|
| 4215 | enum lock_usage_bit bit)
|
| 4216 | {
|
| 4217 | enum bfs_result ret;
|
| 4218 | struct lock_list root;
|
| 4219 | struct lock_list *target_entry;
|
| 4220 | enum lock_usage_bit read_bit = bit + LOCK_USAGE_READ_MASK;
|
| 4221 | unsigned usage_mask = lock_flag(bit) | lock_flag(read_bit);
|
| 4222 |
|
| 4223 | bfs_init_rootb(&root, this);
|
| 4224 | ret = find_usage_backwards(&root, usage_mask, &target_entry);
|
| 4225 | if (bfs_error(ret)) {
|
| 4226 | print_bfs_bug(ret);
|
| 4227 | return 0;
|
| 4228 | }
|
| 4229 | if (ret == BFS_RNOMATCH)
|
| 4230 | return 1;
|
| 4231 |
|
| 4232 |
|
| 4233 | if (target_entry->class->usage_mask & lock_flag(bit)) {
|
| 4234 | print_irq_inversion_bug(curr, &root, target_entry,
|
| 4235 | this, 0, state_name(bit));
|
| 4236 | } else {
|
| 4237 | print_irq_inversion_bug(curr, &root, target_entry,
|
| 4238 | this, 0, state_name(read_bit));
|
| 4239 | }
|
| 4240 |
|
| 4241 | return 0;
|
| 4242 | }
|
| 4243 |
|
| 4244 | void print_irqtrace_events(struct task_struct *curr)
|
| 4245 | {
|
| 4246 | const struct irqtrace_events *trace = &curr->irqtrace;
|
| 4247 |
|
| 4248 | nbcon_cpu_emergency_enter();
|
| 4249 |
|
| 4250 | printk("irq event stamp: %u\n", trace->irq_events);
|
| 4251 | printk("hardirqs last enabled at (%u): [<%px>] %pS\n",
|
| 4252 | trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip,
|
| 4253 | (void *)trace->hardirq_enable_ip);
|
| 4254 | printk("hardirqs last disabled at (%u): [<%px>] %pS\n",
|
| 4255 | trace->hardirq_disable_event, (void *)trace->hardirq_disable_ip,
|
| 4256 | (void *)trace->hardirq_disable_ip);
|
| 4257 | printk("softirqs last enabled at (%u): [<%px>] %pS\n",
|
| 4258 | trace->softirq_enable_event, (void *)trace->softirq_enable_ip,
|
| 4259 | (void *)trace->softirq_enable_ip);
|
| 4260 | printk("softirqs last disabled at (%u): [<%px>] %pS\n",
|
| 4261 | trace->softirq_disable_event, (void *)trace->softirq_disable_ip,
|
| 4262 | (void *)trace->softirq_disable_ip);
|
| 4263 |
|
| 4264 | nbcon_cpu_emergency_exit();
|
| 4265 | }
|
| 4266 |
|
| 4267 | static int HARDIRQ_verbose(struct lock_class *class)
|
| 4268 | {
|
| 4269 | #if HARDIRQ_VERBOSE
|
| 4270 | return class_filter(class);
|
| 4271 | #endif
|
| 4272 | return 0;
|
| 4273 | }
|
| 4274 |
|
| 4275 | static int SOFTIRQ_verbose(struct lock_class *class)
|
| 4276 | {
|
| 4277 | #if SOFTIRQ_VERBOSE
|
| 4278 | return class_filter(class);
|
| 4279 | #endif
|
| 4280 | return 0;
|
| 4281 | }
|
| 4282 |
|
| 4283 | static int (*state_verbose_f[])(struct lock_class *class) = {
|
| 4284 | #define LOCKDEP_STATE(__STATE) \
|
| 4285 | __STATE##_verbose,
|
| 4286 | #include "lockdep_states.h"
|
| 4287 | #undef LOCKDEP_STATE
|
| 4288 | };
|
| 4289 |
|
| 4290 | static inline int state_verbose(enum lock_usage_bit bit,
|
| 4291 | struct lock_class *class)
|
| 4292 | {
|
| 4293 | return state_verbose_f[bit >> LOCK_USAGE_DIR_MASK](class);
|
| 4294 | }
|
| 4295 |
|
| 4296 | typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
|
| 4297 | enum lock_usage_bit bit, const char *name);
|
| 4298 |
|
| 4299 | static int
|
| 4300 | mark_lock_irq(struct task_struct *curr, struct held_lock *this,
|
| 4301 | enum lock_usage_bit new_bit)
|
| 4302 | {
|
| 4303 | int excl_bit = exclusive_bit(new_bit);
|
| 4304 | int read = new_bit & LOCK_USAGE_READ_MASK;
|
| 4305 | int dir = new_bit & LOCK_USAGE_DIR_MASK;
|
| 4306 |
|
| 4307 | |
| 4308 | * Validate that this particular lock does not have conflicting
|
| 4309 | * usage states.
|
| 4310 |
|
| 4311 | if (!valid_state(curr, this, new_bit, excl_bit))
|
| 4312 | return 0;
|
| 4313 |
|
| 4314 | |
| 4315 | * Check for read in write conflicts
|
| 4316 |
|
| 4317 | if (!read && !valid_state(curr, this, new_bit,
|
| 4318 | excl_bit + LOCK_USAGE_READ_MASK))
|
| 4319 | return 0;
|
| 4320 |
|
| 4321 |
|
| 4322 | |
| 4323 | * Validate that the lock dependencies don't have conflicting usage
|
| 4324 | * states.
|
| 4325 |
|
| 4326 | if (dir) {
|
| 4327 | |
| 4328 | * mark ENABLED has to look backwards -- to ensure no dependee
|
| 4329 | * has USED_IN state, which, again, would allow recursion deadlocks.
|
| 4330 |
|
| 4331 | if (!check_usage_backwards(curr, this, excl_bit))
|
| 4332 | return 0;
|
| 4333 | } else {
|
| 4334 | |
| 4335 | * mark USED_IN has to look forwards -- to ensure no dependency
|
| 4336 | * has ENABLED state, which would allow recursion deadlocks.
|
| 4337 |
|
| 4338 | if (!check_usage_forwards(curr, this, excl_bit))
|
| 4339 | return 0;
|
| 4340 | }
|
| 4341 |
|
| 4342 | if (state_verbose(new_bit, hlock_class(this)))
|
| 4343 | return 2;
|
| 4344 |
|
| 4345 | return 1;
|
| 4346 | }
|
| 4347 |
|
| 4348 | |
| 4349 | * Mark all held locks with a usage bit:
|
| 4350 |
|
| 4351 | static int
|
| 4352 | mark_held_locks(struct task_struct *curr, enum lock_usage_bit base_bit)
|
| 4353 | {
|
| 4354 | struct held_lock *hlock;
|
| 4355 | int i;
|
| 4356 |
|
| 4357 | for (i = 0; i < curr->lockdep_depth; i++) {
|
| 4358 | enum lock_usage_bit hlock_bit = base_bit;
|
| 4359 | hlock = curr->held_locks + i;
|
| 4360 |
|
| 4361 | if (hlock->read)
|
| 4362 | hlock_bit += LOCK_USAGE_READ_MASK;
|
| 4363 |
|
| 4364 | BUG_ON(hlock_bit >= LOCK_USAGE_STATES);
|
| 4365 |
|
| 4366 | if (!hlock->check)
|
| 4367 | continue;
|
| 4368 |
|
| 4369 | if (!mark_lock(curr, hlock, hlock_bit))
|
| 4370 | return 0;
|
| 4371 | }
|
| 4372 |
|
| 4373 | return 1;
|
| 4374 | }
|
| 4375 |
|
| 4376 | |
| 4377 | * Hardirqs will be enabled:
|
| 4378 |
|
| 4379 | static void __trace_hardirqs_on_caller(void)
|
| 4380 | {
|
| 4381 | struct task_struct *curr = current;
|
| 4382 |
|
| 4383 | |
| 4384 | * We are going to turn hardirqs on, so set the
|
| 4385 | * usage bit for all held locks:
|
| 4386 |
|
| 4387 | if (!mark_held_locks(curr, LOCK_ENABLED_HARDIRQ))
|
| 4388 | return;
|
| 4389 | |
| 4390 | * If we have softirqs enabled, then set the usage
|
| 4391 | * bit for all held locks. (disabled hardirqs prevented
|
| 4392 | * this bit from being set before)
|
| 4393 |
|
| 4394 | if (curr->softirqs_enabled)
|
| 4395 | mark_held_locks(curr, LOCK_ENABLED_SOFTIRQ);
|
| 4396 | }
|
| 4397 |
|
| 4398 | |
| 4399 | * lockdep_hardirqs_on_prepare - Prepare for enabling interrupts
|
| 4400 | *
|
| 4401 | * Invoked before a possible transition to RCU idle from exit to user or
|
| 4402 | * guest mode. This ensures that all RCU operations are done before RCU
|
| 4403 | * stops watching. After the RCU transition lockdep_hardirqs_on() has to be
|
| 4404 | * invoked to set the final state.
|
| 4405 |
|
| 4406 | void lockdep_hardirqs_on_prepare(void)
|
| 4407 | {
|
| 4408 | if (unlikely(!debug_locks))
|
| 4409 | return;
|
| 4410 |
|
| 4411 | |
| 4412 | * NMIs do not (and cannot) track lock dependencies, nothing to do.
|
| 4413 |
|
| 4414 | if (unlikely(in_nmi()))
|
| 4415 | return;
|
| 4416 |
|
| 4417 | if (unlikely(this_cpu_read(lockdep_recursion)))
|
| 4418 | return;
|
| 4419 |
|
| 4420 | if (unlikely(lockdep_hardirqs_enabled())) {
|
| 4421 | |
| 4422 | * Neither irq nor preemption are disabled here
|
| 4423 | * so this is racy by nature but losing one hit
|
| 4424 | * in a stat is not a big deal.
|
| 4425 |
|
| 4426 | __debug_atomic_inc(redundant_hardirqs_on);
|
| 4427 | return;
|
| 4428 | }
|
| 4429 |
|
| 4430 | |
| 4431 | * We're enabling irqs and according to our state above irqs weren't
|
| 4432 | * already enabled, yet we find the hardware thinks they are in fact
|
| 4433 | * enabled.. someone messed up their IRQ state tracing.
|
| 4434 |
|
| 4435 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
|
| 4436 | return;
|
| 4437 |
|
| 4438 | |
| 4439 | * See the fine text that goes along with this variable definition.
|
| 4440 |
|
| 4441 | if (DEBUG_LOCKS_WARN_ON(early_boot_irqs_disabled))
|
| 4442 | return;
|
| 4443 |
|
| 4444 | |
| 4445 | * Can't allow enabling interrupts while in an interrupt handler,
|
| 4446 | * that's general bad form and such. Recursion, limited stack etc..
|
| 4447 |
|
| 4448 | if (DEBUG_LOCKS_WARN_ON(lockdep_hardirq_context()))
|
| 4449 | return;
|
| 4450 |
|
| 4451 | current->hardirq_chain_key = current->curr_chain_key;
|
| 4452 |
|
| 4453 | lockdep_recursion_inc();
|
| 4454 | __trace_hardirqs_on_caller();
|
| 4455 | lockdep_recursion_finish();
|
| 4456 | }
|
| 4457 | EXPORT_SYMBOL_GPL(lockdep_hardirqs_on_prepare);
|
| 4458 |
|
| 4459 | void noinstr lockdep_hardirqs_on(unsigned long ip)
|
| 4460 | {
|
| 4461 | struct irqtrace_events *trace = ¤t->irqtrace;
|
| 4462 |
|
| 4463 | if (unlikely(!debug_locks))
|
| 4464 | return;
|
| 4465 |
|
| 4466 | |
| 4467 | * NMIs can happen in the middle of local_irq_{en,dis}able() where the
|
| 4468 | * tracking state and hardware state are out of sync.
|
| 4469 | *
|
| 4470 | * NMIs must save lockdep_hardirqs_enabled() to restore IRQ state from,
|
| 4471 | * and not rely on hardware state like normal interrupts.
|
| 4472 |
|
| 4473 | if (unlikely(in_nmi())) {
|
| 4474 | if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI))
|
| 4475 | return;
|
| 4476 |
|
| 4477 | |
| 4478 | * Skip:
|
| 4479 | * - recursion check, because NMI can hit lockdep;
|
| 4480 | * - hardware state check, because above;
|
| 4481 | * - chain_key check, see lockdep_hardirqs_on_prepare().
|
| 4482 |
|
| 4483 | goto skip_checks;
|
| 4484 | }
|
| 4485 |
|
| 4486 | if (unlikely(this_cpu_read(lockdep_recursion)))
|
| 4487 | return;
|
| 4488 |
|
| 4489 | if (lockdep_hardirqs_enabled()) {
|
| 4490 | |
| 4491 | * Neither irq nor preemption are disabled here
|
| 4492 | * so this is racy by nature but losing one hit
|
| 4493 | * in a stat is not a big deal.
|
| 4494 |
|
| 4495 | __debug_atomic_inc(redundant_hardirqs_on);
|
| 4496 | return;
|
| 4497 | }
|
| 4498 |
|
| 4499 | |
| 4500 | * We're enabling irqs and according to our state above irqs weren't
|
| 4501 | * already enabled, yet we find the hardware thinks they are in fact
|
| 4502 | * enabled.. someone messed up their IRQ state tracing.
|
| 4503 |
|
| 4504 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
|
| 4505 | return;
|
| 4506 |
|
| 4507 | |
| 4508 | * Ensure the lock stack remained unchanged between
|
| 4509 | * lockdep_hardirqs_on_prepare() and lockdep_hardirqs_on().
|
| 4510 |
|
| 4511 | DEBUG_LOCKS_WARN_ON(current->hardirq_chain_key !=
|
| 4512 | current->curr_chain_key);
|
| 4513 |
|
| 4514 | skip_checks:
|
| 4515 |
|
| 4516 | __this_cpu_write(hardirqs_enabled, 1);
|
| 4517 | trace->hardirq_enable_ip = ip;
|
| 4518 | trace->hardirq_enable_event = ++trace->irq_events;
|
| 4519 | debug_atomic_inc(hardirqs_on_events);
|
| 4520 | }
|
| 4521 | EXPORT_SYMBOL_GPL(lockdep_hardirqs_on);
|
| 4522 |
|
| 4523 | |
| 4524 | * Hardirqs were disabled:
|
| 4525 |
|
| 4526 | void noinstr lockdep_hardirqs_off(unsigned long ip)
|
| 4527 | {
|
| 4528 | if (unlikely(!debug_locks))
|
| 4529 | return;
|
| 4530 |
|
| 4531 | |
| 4532 | * Matching lockdep_hardirqs_on(), allow NMIs in the middle of lockdep;
|
| 4533 | * they will restore the software state. This ensures the software
|
| 4534 | * state is consistent inside NMIs as well.
|
| 4535 |
|
| 4536 | if (in_nmi()) {
|
| 4537 | if (!IS_ENABLED(CONFIG_TRACE_IRQFLAGS_NMI))
|
| 4538 | return;
|
| 4539 | } else if (__this_cpu_read(lockdep_recursion))
|
| 4540 | return;
|
| 4541 |
|
| 4542 | |
| 4543 | * So we're supposed to get called after you mask local IRQs, but for
|
| 4544 | * some reason the hardware doesn't quite think you did a proper job.
|
| 4545 |
|
| 4546 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
|
| 4547 | return;
|
| 4548 |
|
| 4549 | if (lockdep_hardirqs_enabled()) {
|
| 4550 | struct irqtrace_events *trace = ¤t->irqtrace;
|
| 4551 |
|
| 4552 | |
| 4553 | * We have done an ON -> OFF transition:
|
| 4554 |
|
| 4555 | __this_cpu_write(hardirqs_enabled, 0);
|
| 4556 | trace->hardirq_disable_ip = ip;
|
| 4557 | trace->hardirq_disable_event = ++trace->irq_events;
|
| 4558 | debug_atomic_inc(hardirqs_off_events);
|
| 4559 | } else {
|
| 4560 | debug_atomic_inc(redundant_hardirqs_off);
|
| 4561 | }
|
| 4562 | }
|
| 4563 | EXPORT_SYMBOL_GPL(lockdep_hardirqs_off);
|
| 4564 |
|
| 4565 | |
| 4566 | * Softirqs will be enabled:
|
| 4567 |
|
| 4568 | void lockdep_softirqs_on(unsigned long ip)
|
| 4569 | {
|
| 4570 | struct irqtrace_events *trace = ¤t->irqtrace;
|
| 4571 |
|
| 4572 | if (unlikely(!lockdep_enabled()))
|
| 4573 | return;
|
| 4574 |
|
| 4575 | |
| 4576 | * We fancy IRQs being disabled here, see softirq.c, avoids
|
| 4577 | * funny state and nesting things.
|
| 4578 |
|
| 4579 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
|
| 4580 | return;
|
| 4581 |
|
| 4582 | if (current->softirqs_enabled) {
|
| 4583 | debug_atomic_inc(redundant_softirqs_on);
|
| 4584 | return;
|
| 4585 | }
|
| 4586 |
|
| 4587 | lockdep_recursion_inc();
|
| 4588 | |
| 4589 | * We'll do an OFF -> ON transition:
|
| 4590 |
|
| 4591 | current->softirqs_enabled = 1;
|
| 4592 | trace->softirq_enable_ip = ip;
|
| 4593 | trace->softirq_enable_event = ++trace->irq_events;
|
| 4594 | debug_atomic_inc(softirqs_on_events);
|
| 4595 | |
| 4596 | * We are going to turn softirqs on, so set the
|
| 4597 | * usage bit for all held locks, if hardirqs are
|
| 4598 | * enabled too:
|
| 4599 |
|
| 4600 | if (lockdep_hardirqs_enabled())
|
| 4601 | mark_held_locks(current, LOCK_ENABLED_SOFTIRQ);
|
| 4602 | lockdep_recursion_finish();
|
| 4603 | }
|
| 4604 |
|
| 4605 | |
| 4606 | * Softirqs were disabled:
|
| 4607 |
|
| 4608 | void lockdep_softirqs_off(unsigned long ip)
|
| 4609 | {
|
| 4610 | if (unlikely(!lockdep_enabled()))
|
| 4611 | return;
|
| 4612 |
|
| 4613 | |
| 4614 | * We fancy IRQs being disabled here, see softirq.c
|
| 4615 |
|
| 4616 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
|
| 4617 | return;
|
| 4618 |
|
| 4619 | if (current->softirqs_enabled) {
|
| 4620 | struct irqtrace_events *trace = ¤t->irqtrace;
|
| 4621 |
|
| 4622 | |
| 4623 | * We have done an ON -> OFF transition:
|
| 4624 |
|
| 4625 | current->softirqs_enabled = 0;
|
| 4626 | trace->softirq_disable_ip = ip;
|
| 4627 | trace->softirq_disable_event = ++trace->irq_events;
|
| 4628 | debug_atomic_inc(softirqs_off_events);
|
| 4629 | |
| 4630 | * Whoops, we wanted softirqs off, so why aren't they?
|
| 4631 |
|
| 4632 | DEBUG_LOCKS_WARN_ON(!softirq_count());
|
| 4633 | } else
|
| 4634 | debug_atomic_inc(redundant_softirqs_off);
|
| 4635 | }
|
| 4636 |
|
| 4637 | |
| 4638 | * lockdep_cleanup_dead_cpu - Ensure CPU lockdep state is cleanly stopped
|
| 4639 | *
|
| 4640 | * @cpu: index of offlined CPU
|
| 4641 | * @idle: task pointer for offlined CPU's idle thread
|
| 4642 | *
|
| 4643 | * Invoked after the CPU is dead. Ensures that the tracing infrastructure
|
| 4644 | * is left in a suitable state for the CPU to be subsequently brought
|
| 4645 | * online again.
|
| 4646 |
|
| 4647 | void lockdep_cleanup_dead_cpu(unsigned int cpu, struct task_struct *idle)
|
| 4648 | {
|
| 4649 | if (unlikely(!debug_locks))
|
| 4650 | return;
|
| 4651 |
|
| 4652 | if (unlikely(per_cpu(hardirqs_enabled, cpu))) {
|
| 4653 | pr_warn("CPU %u left hardirqs enabled!", cpu);
|
| 4654 | if (idle)
|
| 4655 | print_irqtrace_events(idle);
|
| 4656 |
|
| 4657 | per_cpu(hardirqs_enabled, cpu) = 0;
|
| 4658 | }
|
| 4659 | }
|
| 4660 |
|
| 4661 | static int
|
| 4662 | mark_usage(struct task_struct *curr, struct held_lock *hlock, int check)
|
| 4663 | {
|
| 4664 | if (!check)
|
| 4665 | goto lock_used;
|
| 4666 |
|
| 4667 | |
| 4668 | * If non-trylock use in a hardirq or softirq context, then
|
| 4669 | * mark the lock as used in these contexts:
|
| 4670 |
|
| 4671 | if (!hlock->trylock) {
|
| 4672 | if (hlock->read) {
|
| 4673 | if (lockdep_hardirq_context())
|
| 4674 | if (!mark_lock(curr, hlock,
|
| 4675 | LOCK_USED_IN_HARDIRQ_READ))
|
| 4676 | return 0;
|
| 4677 | if (curr->softirq_context)
|
| 4678 | if (!mark_lock(curr, hlock,
|
| 4679 | LOCK_USED_IN_SOFTIRQ_READ))
|
| 4680 | return 0;
|
| 4681 | } else {
|
| 4682 | if (lockdep_hardirq_context())
|
| 4683 | if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
|
| 4684 | return 0;
|
| 4685 | if (curr->softirq_context)
|
| 4686 | if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
|
| 4687 | return 0;
|
| 4688 | }
|
| 4689 | }
|
| 4690 |
|
| 4691 | |
| 4692 | * For lock_sync(), don't mark the ENABLED usage, since lock_sync()
|
| 4693 | * creates no critical section and no extra dependency can be introduced
|
| 4694 | * by interrupts
|
| 4695 |
|
| 4696 | if (!hlock->hardirqs_off && !hlock->sync) {
|
| 4697 | if (hlock->read) {
|
| 4698 | if (!mark_lock(curr, hlock,
|
| 4699 | LOCK_ENABLED_HARDIRQ_READ))
|
| 4700 | return 0;
|
| 4701 | if (curr->softirqs_enabled)
|
| 4702 | if (!mark_lock(curr, hlock,
|
| 4703 | LOCK_ENABLED_SOFTIRQ_READ))
|
| 4704 | return 0;
|
| 4705 | } else {
|
| 4706 | if (!mark_lock(curr, hlock,
|
| 4707 | LOCK_ENABLED_HARDIRQ))
|
| 4708 | return 0;
|
| 4709 | if (curr->softirqs_enabled)
|
| 4710 | if (!mark_lock(curr, hlock,
|
| 4711 | LOCK_ENABLED_SOFTIRQ))
|
| 4712 | return 0;
|
| 4713 | }
|
| 4714 | }
|
| 4715 |
|
| 4716 | lock_used:
|
| 4717 |
|
| 4718 | if (!mark_lock(curr, hlock, LOCK_USED))
|
| 4719 | return 0;
|
| 4720 |
|
| 4721 | return 1;
|
| 4722 | }
|
| 4723 |
|
| 4724 | static inline unsigned int task_irq_context(struct task_struct *task)
|
| 4725 | {
|
| 4726 | return LOCK_CHAIN_HARDIRQ_CONTEXT * !!lockdep_hardirq_context() +
|
| 4727 | LOCK_CHAIN_SOFTIRQ_CONTEXT * !!task->softirq_context;
|
| 4728 | }
|
| 4729 |
|
| 4730 | static int separate_irq_context(struct task_struct *curr,
|
| 4731 | struct held_lock *hlock)
|
| 4732 | {
|
| 4733 | unsigned int depth = curr->lockdep_depth;
|
| 4734 |
|
| 4735 | |
| 4736 | * Keep track of points where we cross into an interrupt context:
|
| 4737 |
|
| 4738 | if (depth) {
|
| 4739 | struct held_lock *prev_hlock;
|
| 4740 |
|
| 4741 | prev_hlock = curr->held_locks + depth-1;
|
| 4742 | |
| 4743 | * If we cross into another context, reset the
|
| 4744 | * hash key (this also prevents the checking and the
|
| 4745 | * adding of the dependency to 'prev'):
|
| 4746 |
|
| 4747 | if (prev_hlock->irq_context != hlock->irq_context)
|
| 4748 | return 1;
|
| 4749 | }
|
| 4750 | return 0;
|
| 4751 | }
|
| 4752 |
|
| 4753 | |
| 4754 | * Mark a lock with a usage bit, and validate the state transition:
|
| 4755 |
|
| 4756 | static int mark_lock(struct task_struct *curr, struct held_lock *this,
|
| 4757 | enum lock_usage_bit new_bit)
|
| 4758 | {
|
| 4759 | unsigned int new_mask, ret = 1;
|
| 4760 |
|
| 4761 | if (new_bit >= LOCK_USAGE_STATES) {
|
| 4762 | DEBUG_LOCKS_WARN_ON(1);
|
| 4763 | return 0;
|
| 4764 | }
|
| 4765 |
|
| 4766 | if (new_bit == LOCK_USED && this->read)
|
| 4767 | new_bit = LOCK_USED_READ;
|
| 4768 |
|
| 4769 | new_mask = 1 << new_bit;
|
| 4770 |
|
| 4771 | |
| 4772 | * If already set then do not dirty the cacheline,
|
| 4773 | * nor do any checks:
|
| 4774 |
|
| 4775 | if (likely(hlock_class(this)->usage_mask & new_mask))
|
| 4776 | return 1;
|
| 4777 |
|
| 4778 | if (!graph_lock())
|
| 4779 | return 0;
|
| 4780 | |
| 4781 | * Make sure we didn't race:
|
| 4782 |
|
| 4783 | if (unlikely(hlock_class(this)->usage_mask & new_mask))
|
| 4784 | goto unlock;
|
| 4785 |
|
| 4786 | if (!hlock_class(this)->usage_mask)
|
| 4787 | debug_atomic_dec(nr_unused_locks);
|
| 4788 |
|
| 4789 | hlock_class(this)->usage_mask |= new_mask;
|
| 4790 |
|
| 4791 | if (new_bit < LOCK_TRACE_STATES) {
|
| 4792 | if (!(hlock_class(this)->usage_traces[new_bit] = save_trace()))
|
| 4793 | return 0;
|
| 4794 | }
|
| 4795 |
|
| 4796 | if (new_bit < LOCK_USED) {
|
| 4797 | ret = mark_lock_irq(curr, this, new_bit);
|
| 4798 | if (!ret)
|
| 4799 | return 0;
|
| 4800 | }
|
| 4801 |
|
| 4802 | unlock:
|
| 4803 | graph_unlock();
|
| 4804 |
|
| 4805 | |
| 4806 | * We must printk outside of the graph_lock:
|
| 4807 |
|
| 4808 | if (ret == 2) {
|
| 4809 | nbcon_cpu_emergency_enter();
|
| 4810 | printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
|
| 4811 | print_lock(this);
|
| 4812 | print_irqtrace_events(curr);
|
| 4813 | dump_stack();
|
| 4814 | nbcon_cpu_emergency_exit();
|
| 4815 | }
|
| 4816 |
|
| 4817 | return ret;
|
| 4818 | }
|
| 4819 |
|
| 4820 | static inline short task_wait_context(struct task_struct *curr)
|
| 4821 | {
|
| 4822 | |
| 4823 | * Set appropriate wait type for the context; for IRQs we have to take
|
| 4824 | * into account force_irqthread as that is implied by PREEMPT_RT.
|
| 4825 |
|
| 4826 | if (lockdep_hardirq_context()) {
|
| 4827 | |
| 4828 | * Check if force_irqthreads will run us threaded.
|
| 4829 |
|
| 4830 | if (curr->hardirq_threaded || curr->irq_config)
|
| 4831 | return LD_WAIT_CONFIG;
|
| 4832 |
|
| 4833 | return LD_WAIT_SPIN;
|
| 4834 | } else if (curr->softirq_context) {
|
| 4835 | |
| 4836 | * Softirqs are always threaded.
|
| 4837 |
|
| 4838 | return LD_WAIT_CONFIG;
|
| 4839 | }
|
| 4840 |
|
| 4841 | return LD_WAIT_MAX;
|
| 4842 | }
|
| 4843 |
|
| 4844 | static int
|
| 4845 | print_lock_invalid_wait_context(struct task_struct *curr,
|
| 4846 | struct held_lock *hlock)
|
| 4847 | {
|
| 4848 | short curr_inner;
|
| 4849 |
|
| 4850 | if (!debug_locks_off())
|
| 4851 | return 0;
|
| 4852 | if (debug_locks_silent)
|
| 4853 | return 0;
|
| 4854 |
|
| 4855 | nbcon_cpu_emergency_enter();
|
| 4856 |
|
| 4857 | pr_warn("\n");
|
| 4858 | pr_warn("=============================\n");
|
| 4859 | pr_warn("[ BUG: Invalid wait context ]\n");
|
| 4860 | print_kernel_ident();
|
| 4861 | pr_warn("-----------------------------\n");
|
| 4862 |
|
| 4863 | pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
|
| 4864 | print_lock(hlock);
|
| 4865 |
|
| 4866 | pr_warn("other info that might help us debug this:\n");
|
| 4867 |
|
| 4868 | curr_inner = task_wait_context(curr);
|
| 4869 | pr_warn("context-{%d:%d}\n", curr_inner, curr_inner);
|
| 4870 |
|
| 4871 | lockdep_print_held_locks(curr);
|
| 4872 |
|
| 4873 | pr_warn("stack backtrace:\n");
|
| 4874 | dump_stack();
|
| 4875 |
|
| 4876 | nbcon_cpu_emergency_exit();
|
| 4877 |
|
| 4878 | return 0;
|
| 4879 | }
|
| 4880 |
|
| 4881 | |
| 4882 | * Verify the wait_type context.
|
| 4883 | *
|
| 4884 | * This check validates we take locks in the right wait-type order; that is it
|
| 4885 | * ensures that we do not take mutexes inside spinlocks and do not attempt to
|
| 4886 | * acquire spinlocks inside raw_spinlocks and the sort.
|
| 4887 | *
|
| 4888 | * The entire thing is slightly more complex because of RCU, RCU is a lock that
|
| 4889 | * can be taken from (pretty much) any context but also has constraints.
|
| 4890 | * However when taken in a stricter environment the RCU lock does not loosen
|
| 4891 | * the constraints.
|
| 4892 | *
|
| 4893 | * Therefore we must look for the strictest environment in the lock stack and
|
| 4894 | * compare that to the lock we're trying to acquire.
|
| 4895 |
|
| 4896 | static int check_wait_context(struct task_struct *curr, struct held_lock *next)
|
| 4897 | {
|
| 4898 | u8 next_inner = hlock_class(next)->wait_type_inner;
|
| 4899 | u8 next_outer = hlock_class(next)->wait_type_outer;
|
| 4900 | u8 curr_inner;
|
| 4901 | int depth;
|
| 4902 |
|
| 4903 | if (!next_inner || next->trylock)
|
| 4904 | return 0;
|
| 4905 |
|
| 4906 | if (!next_outer)
|
| 4907 | next_outer = next_inner;
|
| 4908 |
|
| 4909 | |
| 4910 | * Find start of current irq_context..
|
| 4911 |
|
| 4912 | for (depth = curr->lockdep_depth - 1; depth >= 0; depth--) {
|
| 4913 | struct held_lock *prev = curr->held_locks + depth;
|
| 4914 | if (prev->irq_context != next->irq_context)
|
| 4915 | break;
|
| 4916 | }
|
| 4917 | depth++;
|
| 4918 |
|
| 4919 | curr_inner = task_wait_context(curr);
|
| 4920 |
|
| 4921 | for (; depth < curr->lockdep_depth; depth++) {
|
| 4922 | struct held_lock *prev = curr->held_locks + depth;
|
| 4923 | struct lock_class *class = hlock_class(prev);
|
| 4924 | u8 prev_inner = class->wait_type_inner;
|
| 4925 |
|
| 4926 | if (prev_inner) {
|
| 4927 | |
| 4928 | * We can have a bigger inner than a previous one
|
| 4929 | * when outer is smaller than inner, as with RCU.
|
| 4930 | *
|
| 4931 | * Also due to trylocks.
|
| 4932 |
|
| 4933 | curr_inner = min(curr_inner, prev_inner);
|
| 4934 |
|
| 4935 | |
| 4936 | * Allow override for annotations -- this is typically
|
| 4937 | * only valid/needed for code that only exists when
|
| 4938 | * CONFIG_PREEMPT_RT=n.
|
| 4939 |
|
| 4940 | if (unlikely(class->lock_type == LD_LOCK_WAIT_OVERRIDE))
|
| 4941 | curr_inner = prev_inner;
|
| 4942 | }
|
| 4943 | }
|
| 4944 |
|
| 4945 | if (next_outer > curr_inner)
|
| 4946 | return print_lock_invalid_wait_context(curr, next);
|
| 4947 |
|
| 4948 | return 0;
|
| 4949 | }
|
| 4950 |
|
| 4951 | #else
|
| 4952 |
|
| 4953 | static inline int
|
| 4954 | mark_usage(struct task_struct *curr, struct held_lock *hlock, int check)
|
| 4955 | {
|
| 4956 | return 1;
|
| 4957 | }
|
| 4958 |
|
| 4959 | static inline unsigned int task_irq_context(struct task_struct *task)
|
| 4960 | {
|
| 4961 | return 0;
|
| 4962 | }
|
| 4963 |
|
| 4964 | static inline int separate_irq_context(struct task_struct *curr,
|
| 4965 | struct held_lock *hlock)
|
| 4966 | {
|
| 4967 | return 0;
|
| 4968 | }
|
| 4969 |
|
| 4970 | static inline int check_wait_context(struct task_struct *curr,
|
| 4971 | struct held_lock *next)
|
| 4972 | {
|
| 4973 | return 0;
|
| 4974 | }
|
| 4975 |
|
| 4976 | #endif
|
| 4977 |
|
| 4978 | |
| 4979 | * Initialize a lock instance's lock-class mapping info:
|
| 4980 |
|
| 4981 | void lockdep_init_map_type(struct lockdep_map *lock, const char *name,
|
| 4982 | struct lock_class_key *key, int subclass,
|
| 4983 | u8 inner, u8 outer, u8 lock_type)
|
| 4984 | {
|
| 4985 | int i;
|
| 4986 |
|
| 4987 | for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
|
| 4988 | WRITE_ONCE(lock->class_cache[i], NULL);
|
| 4989 |
|
| 4990 | #ifdef CONFIG_LOCK_STAT
|
| 4991 | lock->cpu = raw_smp_processor_id();
|
| 4992 | #endif
|
| 4993 |
|
| 4994 | |
| 4995 | * Can't be having no nameless bastards around this place!
|
| 4996 |
|
| 4997 | if (DEBUG_LOCKS_WARN_ON(!name)) {
|
| 4998 | lock->name = "NULL";
|
| 4999 | return;
|
| 5000 | }
|
| 5001 |
|
| 5002 | lock->name = name;
|
| 5003 |
|
| 5004 | lock->wait_type_outer = outer;
|
| 5005 | lock->wait_type_inner = inner;
|
| 5006 | lock->lock_type = lock_type;
|
| 5007 |
|
| 5008 | |
| 5009 | * No key, no joy, we need to hash something.
|
| 5010 |
|
| 5011 | if (DEBUG_LOCKS_WARN_ON(!key))
|
| 5012 | return;
|
| 5013 | |
| 5014 | * Sanity check, the lock-class key must either have been allocated
|
| 5015 | * statically or must have been registered as a dynamic key.
|
| 5016 |
|
| 5017 | if (!static_obj(key) && !is_dynamic_key(key)) {
|
| 5018 | if (debug_locks)
|
| 5019 | printk(KERN_ERR "BUG: key %px has not been registered!\n", key);
|
| 5020 | DEBUG_LOCKS_WARN_ON(1);
|
| 5021 | return;
|
| 5022 | }
|
| 5023 | lock->key = key;
|
| 5024 |
|
| 5025 | if (unlikely(!debug_locks))
|
| 5026 | return;
|
| 5027 |
|
| 5028 | if (subclass) {
|
| 5029 | unsigned long flags;
|
| 5030 |
|
| 5031 | if (DEBUG_LOCKS_WARN_ON(!lockdep_enabled()))
|
| 5032 | return;
|
| 5033 |
|
| 5034 | raw_local_irq_save(flags);
|
| 5035 | lockdep_recursion_inc();
|
| 5036 | register_lock_class(lock, subclass, 1);
|
| 5037 | lockdep_recursion_finish();
|
| 5038 | raw_local_irq_restore(flags);
|
| 5039 | }
|
| 5040 | }
|
| 5041 | EXPORT_SYMBOL_GPL(lockdep_init_map_type);
|
| 5042 |
|
| 5043 | struct lock_class_key __lockdep_no_validate__;
|
| 5044 | EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
|
| 5045 |
|
| 5046 | struct lock_class_key __lockdep_no_track__;
|
| 5047 | EXPORT_SYMBOL_GPL(__lockdep_no_track__);
|
| 5048 |
|
| 5049 | #ifdef CONFIG_PROVE_LOCKING
|
| 5050 | void lockdep_set_lock_cmp_fn(struct lockdep_map *lock, lock_cmp_fn cmp_fn,
|
| 5051 | lock_print_fn print_fn)
|
| 5052 | {
|
| 5053 | struct lock_class *class = READ_ONCE(lock->class_cache[0]);
|
| 5054 | unsigned long flags;
|
| 5055 |
|
| 5056 | raw_local_irq_save(flags);
|
| 5057 | lockdep_recursion_inc();
|
| 5058 |
|
| 5059 | if (!lock_class_cache_is_valid(lock, class, 0))
|
| 5060 | class = NULL;
|
| 5061 |
|
| 5062 | if (!class)
|
| 5063 | class = register_lock_class(lock, 0, 0);
|
| 5064 |
|
| 5065 | if (class) {
|
| 5066 | WARN_ON(class->cmp_fn && class->cmp_fn != cmp_fn);
|
| 5067 | WARN_ON(class->print_fn && class->print_fn != print_fn);
|
| 5068 |
|
| 5069 | class->cmp_fn = cmp_fn;
|
| 5070 | class->print_fn = print_fn;
|
| 5071 | }
|
| 5072 |
|
| 5073 | lockdep_recursion_finish();
|
| 5074 | raw_local_irq_restore(flags);
|
| 5075 | }
|
| 5076 | EXPORT_SYMBOL_GPL(lockdep_set_lock_cmp_fn);
|
| 5077 | #endif
|
| 5078 |
|
| 5079 | static void
|
| 5080 | print_lock_nested_lock_not_held(struct task_struct *curr,
|
| 5081 | struct held_lock *hlock)
|
| 5082 | {
|
| 5083 | if (!debug_locks_off())
|
| 5084 | return;
|
| 5085 | if (debug_locks_silent)
|
| 5086 | return;
|
| 5087 |
|
| 5088 | nbcon_cpu_emergency_enter();
|
| 5089 |
|
| 5090 | pr_warn("\n");
|
| 5091 | pr_warn("==================================\n");
|
| 5092 | pr_warn("WARNING: Nested lock was not taken\n");
|
| 5093 | print_kernel_ident();
|
| 5094 | pr_warn("----------------------------------\n");
|
| 5095 |
|
| 5096 | pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
|
| 5097 | print_lock(hlock);
|
| 5098 |
|
| 5099 | pr_warn("\nbut this task is not holding:\n");
|
| 5100 | pr_warn("%s\n", hlock->nest_lock->name);
|
| 5101 |
|
| 5102 | pr_warn("\nstack backtrace:\n");
|
| 5103 | dump_stack();
|
| 5104 |
|
| 5105 | pr_warn("\nother info that might help us debug this:\n");
|
| 5106 | lockdep_print_held_locks(curr);
|
| 5107 |
|
| 5108 | pr_warn("\nstack backtrace:\n");
|
| 5109 | dump_stack();
|
| 5110 |
|
| 5111 | nbcon_cpu_emergency_exit();
|
| 5112 | }
|
| 5113 |
|
| 5114 | static int __lock_is_held(const struct lockdep_map *lock, int read);
|
| 5115 |
|
| 5116 | |
| 5117 | * This gets called for every mutex_lock*()/spin_lock*() operation.
|
| 5118 | * We maintain the dependency maps and validate the locking attempt:
|
| 5119 | *
|
| 5120 | * The callers must make sure that IRQs are disabled before calling it,
|
| 5121 | * otherwise we could get an interrupt which would want to take locks,
|
| 5122 | * which would end up in lockdep again.
|
| 5123 |
|
| 5124 | static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
|
| 5125 | int trylock, int read, int check, int hardirqs_off,
|
| 5126 | struct lockdep_map *nest_lock, unsigned long ip,
|
| 5127 | int references, int pin_count, int sync, int seq)
|
| 5128 | {
|
| 5129 | struct task_struct *curr = current;
|
| 5130 | struct lock_class *class = NULL;
|
| 5131 | struct held_lock *hlock;
|
| 5132 | unsigned int depth;
|
| 5133 | int chain_head = 0;
|
| 5134 | int class_idx;
|
| 5135 | u64 chain_key;
|
| 5136 |
|
| 5137 | if (unlikely(!debug_locks))
|
| 5138 | return 0;
|
| 5139 |
|
| 5140 | if (unlikely(lock->key == &__lockdep_no_track__))
|
| 5141 | return 0;
|
| 5142 |
|
| 5143 | lockevent_inc(lockdep_acquire);
|
| 5144 |
|
| 5145 | if (!prove_locking || lock->key == &__lockdep_no_validate__) {
|
| 5146 | check = 0;
|
| 5147 | lockevent_inc(lockdep_nocheck);
|
| 5148 | }
|
| 5149 |
|
| 5150 | if (DEBUG_LOCKS_WARN_ON(subclass >= MAX_LOCKDEP_SUBCLASSES))
|
| 5151 | return 0;
|
| 5152 |
|
| 5153 | if (subclass < NR_LOCKDEP_CACHING_CLASSES) {
|
| 5154 | class = READ_ONCE(lock->class_cache[subclass]);
|
| 5155 | if (!lock_class_cache_is_valid(lock, class, subclass))
|
| 5156 | class = NULL;
|
| 5157 | }
|
| 5158 | |
| 5159 | * Not cached?
|
| 5160 |
|
| 5161 | if (unlikely(!class)) {
|
| 5162 | class = register_lock_class(lock, subclass, 0);
|
| 5163 | if (!class)
|
| 5164 | return 0;
|
| 5165 | }
|
| 5166 |
|
| 5167 | debug_class_ops_inc(class);
|
| 5168 |
|
| 5169 | if (very_verbose(class)) {
|
| 5170 | nbcon_cpu_emergency_enter();
|
| 5171 | printk("\nacquire class [%px] %s", class->key, class->name);
|
| 5172 | if (class->name_version > 1)
|
| 5173 | printk(KERN_CONT "#%d", class->name_version);
|
| 5174 | printk(KERN_CONT "\n");
|
| 5175 | dump_stack();
|
| 5176 | nbcon_cpu_emergency_exit();
|
| 5177 | }
|
| 5178 |
|
| 5179 | |
| 5180 | * Add the lock to the list of currently held locks.
|
| 5181 | * (we dont increase the depth just yet, up until the
|
| 5182 | * dependency checks are done)
|
| 5183 |
|
| 5184 | depth = curr->lockdep_depth;
|
| 5185 | |
| 5186 | * Ran out of static storage for our per-task lock stack again have we?
|
| 5187 |
|
| 5188 | if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
|
| 5189 | return 0;
|
| 5190 |
|
| 5191 | class_idx = class - lock_classes;
|
| 5192 |
|
| 5193 | if (depth && !sync) {
|
| 5194 |
|
| 5195 | hlock = curr->held_locks + depth - 1;
|
| 5196 | if (hlock->class_idx == class_idx && nest_lock) {
|
| 5197 | if (!references)
|
| 5198 | references++;
|
| 5199 |
|
| 5200 | if (!hlock->references)
|
| 5201 | hlock->references++;
|
| 5202 |
|
| 5203 | hlock->references += references;
|
| 5204 |
|
| 5205 |
|
| 5206 | if (DEBUG_LOCKS_WARN_ON(hlock->references < references))
|
| 5207 | return 0;
|
| 5208 |
|
| 5209 | return 2;
|
| 5210 | }
|
| 5211 | }
|
| 5212 |
|
| 5213 | hlock = curr->held_locks + depth;
|
| 5214 | |
| 5215 | * Plain impossible, we just registered it and checked it weren't no
|
| 5216 | * NULL like.. I bet this mushroom I ate was good!
|
| 5217 |
|
| 5218 | if (DEBUG_LOCKS_WARN_ON(!class))
|
| 5219 | return 0;
|
| 5220 | hlock->class_idx = class_idx;
|
| 5221 | hlock->acquire_ip = ip;
|
| 5222 | hlock->instance = lock;
|
| 5223 | hlock->nest_lock = nest_lock;
|
| 5224 | hlock->irq_context = task_irq_context(curr);
|
| 5225 | hlock->trylock = trylock;
|
| 5226 | hlock->read = read;
|
| 5227 | hlock->check = check;
|
| 5228 | hlock->sync = !!sync;
|
| 5229 | hlock->hardirqs_off = !!hardirqs_off;
|
| 5230 | hlock->references = references;
|
| 5231 | #ifdef CONFIG_LOCK_STAT
|
| 5232 | hlock->waittime_stamp = 0;
|
| 5233 | hlock->holdtime_stamp = lockstat_clock();
|
| 5234 | #endif
|
| 5235 | hlock->pin_count = pin_count;
|
| 5236 | hlock->seq_count = seq;
|
| 5237 |
|
| 5238 | if (check_wait_context(curr, hlock))
|
| 5239 | return 0;
|
| 5240 |
|
| 5241 |
|
| 5242 | if (!mark_usage(curr, hlock, check))
|
| 5243 | return 0;
|
| 5244 |
|
| 5245 | |
| 5246 | * Calculate the chain hash: it's the combined hash of all the
|
| 5247 | * lock keys along the dependency chain. We save the hash value
|
| 5248 | * at every step so that we can get the current hash easily
|
| 5249 | * after unlock. The chain hash is then used to cache dependency
|
| 5250 | * results.
|
| 5251 | *
|
| 5252 | * The 'key ID' is what is the most compact key value to drive
|
| 5253 | * the hash, not class->key.
|
| 5254 |
|
| 5255 | |
| 5256 | * Whoops, we did it again.. class_idx is invalid.
|
| 5257 |
|
| 5258 | if (DEBUG_LOCKS_WARN_ON(!test_bit(class_idx, lock_classes_in_use)))
|
| 5259 | return 0;
|
| 5260 |
|
| 5261 | chain_key = curr->curr_chain_key;
|
| 5262 | if (!depth) {
|
| 5263 | |
| 5264 | * How can we have a chain hash when we ain't got no keys?!
|
| 5265 |
|
| 5266 | if (DEBUG_LOCKS_WARN_ON(chain_key != INITIAL_CHAIN_KEY))
|
| 5267 | return 0;
|
| 5268 | chain_head = 1;
|
| 5269 | }
|
| 5270 |
|
| 5271 | hlock->prev_chain_key = chain_key;
|
| 5272 | if (separate_irq_context(curr, hlock)) {
|
| 5273 | chain_key = INITIAL_CHAIN_KEY;
|
| 5274 | chain_head = 1;
|
| 5275 | }
|
| 5276 | chain_key = iterate_chain_key(chain_key, hlock_id(hlock));
|
| 5277 |
|
| 5278 | if (nest_lock && !__lock_is_held(nest_lock, -1)) {
|
| 5279 | print_lock_nested_lock_not_held(curr, hlock);
|
| 5280 | return 0;
|
| 5281 | }
|
| 5282 |
|
| 5283 | if (!debug_locks_silent) {
|
| 5284 | WARN_ON_ONCE(depth && !hlock_class(hlock - 1)->key);
|
| 5285 | WARN_ON_ONCE(!hlock_class(hlock)->key);
|
| 5286 | }
|
| 5287 |
|
| 5288 | if (!validate_chain(curr, hlock, chain_head, chain_key))
|
| 5289 | return 0;
|
| 5290 |
|
| 5291 |
|
| 5292 | if (hlock->sync)
|
| 5293 | return 1;
|
| 5294 |
|
| 5295 | curr->curr_chain_key = chain_key;
|
| 5296 | curr->lockdep_depth++;
|
| 5297 | check_chain_key(curr);
|
| 5298 | #ifdef CONFIG_DEBUG_LOCKDEP
|
| 5299 | if (unlikely(!debug_locks))
|
| 5300 | return 0;
|
| 5301 | #endif
|
| 5302 | if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
|
| 5303 | debug_locks_off();
|
| 5304 | nbcon_cpu_emergency_enter();
|
| 5305 | print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
|
| 5306 | printk(KERN_DEBUG "depth: %i max: %lu!\n",
|
| 5307 | curr->lockdep_depth, MAX_LOCK_DEPTH);
|
| 5308 |
|
| 5309 | lockdep_print_held_locks(current);
|
| 5310 | debug_show_all_locks();
|
| 5311 | dump_stack();
|
| 5312 | nbcon_cpu_emergency_exit();
|
| 5313 |
|
| 5314 | return 0;
|
| 5315 | }
|
| 5316 |
|
| 5317 | if (unlikely(curr->lockdep_depth > max_lockdep_depth))
|
| 5318 | max_lockdep_depth = curr->lockdep_depth;
|
| 5319 |
|
| 5320 | return 1;
|
| 5321 | }
|
| 5322 |
|
| 5323 | static void print_unlock_imbalance_bug(struct task_struct *curr,
|
| 5324 | struct lockdep_map *lock,
|
| 5325 | unsigned long ip)
|
| 5326 | {
|
| 5327 | if (!debug_locks_off())
|
| 5328 | return;
|
| 5329 | if (debug_locks_silent)
|
| 5330 | return;
|
| 5331 |
|
| 5332 | nbcon_cpu_emergency_enter();
|
| 5333 |
|
| 5334 | pr_warn("\n");
|
| 5335 | pr_warn("=====================================\n");
|
| 5336 | pr_warn("WARNING: bad unlock balance detected!\n");
|
| 5337 | print_kernel_ident();
|
| 5338 | pr_warn("-------------------------------------\n");
|
| 5339 | pr_warn("%s/%d is trying to release lock (",
|
| 5340 | curr->comm, task_pid_nr(curr));
|
| 5341 | print_lockdep_cache(lock);
|
| 5342 | pr_cont(") at:\n");
|
| 5343 | print_ip_sym(KERN_WARNING, ip);
|
| 5344 | pr_warn("but there are no more locks to release!\n");
|
| 5345 | pr_warn("\nother info that might help us debug this:\n");
|
| 5346 | lockdep_print_held_locks(curr);
|
| 5347 |
|
| 5348 | pr_warn("\nstack backtrace:\n");
|
| 5349 | dump_stack();
|
| 5350 |
|
| 5351 | nbcon_cpu_emergency_exit();
|
| 5352 | }
|
| 5353 |
|
| 5354 | static noinstr int match_held_lock(const struct held_lock *hlock,
|
| 5355 | const struct lockdep_map *lock)
|
| 5356 | {
|
| 5357 | if (hlock->instance == lock)
|
| 5358 | return 1;
|
| 5359 |
|
| 5360 | if (hlock->references) {
|
| 5361 | const struct lock_class *class = READ_ONCE(lock->class_cache[0]);
|
| 5362 |
|
| 5363 | if (!lock_class_cache_is_valid(lock, class, 0))
|
| 5364 | class = look_up_lock_class(lock, 0);
|
| 5365 |
|
| 5366 | |
| 5367 | * If look_up_lock_class() failed to find a class, we're trying
|
| 5368 | * to test if we hold a lock that has never yet been acquired.
|
| 5369 | * Clearly if the lock hasn't been acquired _ever_, we're not
|
| 5370 | * holding it either, so report failure.
|
| 5371 |
|
| 5372 | if (!class)
|
| 5373 | return 0;
|
| 5374 |
|
| 5375 | |
| 5376 | * References, but not a lock we're actually ref-counting?
|
| 5377 | * State got messed up, follow the sites that change ->references
|
| 5378 | * and try to make sense of it.
|
| 5379 |
|
| 5380 | if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
|
| 5381 | return 0;
|
| 5382 |
|
| 5383 | if (hlock->class_idx == class - lock_classes)
|
| 5384 | return 1;
|
| 5385 | }
|
| 5386 |
|
| 5387 | return 0;
|
| 5388 | }
|
| 5389 |
|
| 5390 |
|
| 5391 | static struct held_lock *find_held_lock(struct task_struct *curr,
|
| 5392 | struct lockdep_map *lock,
|
| 5393 | unsigned int depth, int *idx)
|
| 5394 | {
|
| 5395 | struct held_lock *ret, *hlock, *prev_hlock;
|
| 5396 | int i;
|
| 5397 |
|
| 5398 | i = depth - 1;
|
| 5399 | hlock = curr->held_locks + i;
|
| 5400 | ret = hlock;
|
| 5401 | if (match_held_lock(hlock, lock))
|
| 5402 | goto out;
|
| 5403 |
|
| 5404 | ret = NULL;
|
| 5405 | for (i--, prev_hlock = hlock--;
|
| 5406 | i >= 0;
|
| 5407 | i--, prev_hlock = hlock--) {
|
| 5408 | |
| 5409 | * We must not cross into another context:
|
| 5410 |
|
| 5411 | if (prev_hlock->irq_context != hlock->irq_context) {
|
| 5412 | ret = NULL;
|
| 5413 | break;
|
| 5414 | }
|
| 5415 | if (match_held_lock(hlock, lock)) {
|
| 5416 | ret = hlock;
|
| 5417 | break;
|
| 5418 | }
|
| 5419 | }
|
| 5420 |
|
| 5421 | out:
|
| 5422 | *idx = i;
|
| 5423 | return ret;
|
| 5424 | }
|
| 5425 |
|
| 5426 | static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
|
| 5427 | int idx, unsigned int *merged)
|
| 5428 | {
|
| 5429 | struct held_lock *hlock;
|
| 5430 | int first_idx = idx;
|
| 5431 |
|
| 5432 | if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
|
| 5433 | return 0;
|
| 5434 |
|
| 5435 | for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
|
| 5436 | switch (__lock_acquire(hlock->instance,
|
| 5437 | hlock_class(hlock)->subclass,
|
| 5438 | hlock->trylock,
|
| 5439 | hlock->read, hlock->check,
|
| 5440 | hlock->hardirqs_off,
|
| 5441 | hlock->nest_lock, hlock->acquire_ip,
|
| 5442 | hlock->references, hlock->pin_count, 0, hlock->seq_count)) {
|
| 5443 | case 0:
|
| 5444 | return 1;
|
| 5445 | case 1:
|
| 5446 | break;
|
| 5447 | case 2:
|
| 5448 | *merged += (idx == first_idx);
|
| 5449 | break;
|
| 5450 | default:
|
| 5451 | WARN_ON(1);
|
| 5452 | return 0;
|
| 5453 | }
|
| 5454 | }
|
| 5455 | return 0;
|
| 5456 | }
|
| 5457 |
|
| 5458 | static int
|
| 5459 | __lock_set_class(struct lockdep_map *lock, const char *name,
|
| 5460 | struct lock_class_key *key, unsigned int subclass,
|
| 5461 | unsigned long ip)
|
| 5462 | {
|
| 5463 | struct task_struct *curr = current;
|
| 5464 | unsigned int depth, merged = 0;
|
| 5465 | struct held_lock *hlock;
|
| 5466 | struct lock_class *class;
|
| 5467 | int i;
|
| 5468 |
|
| 5469 | if (unlikely(!debug_locks))
|
| 5470 | return 0;
|
| 5471 |
|
| 5472 | depth = curr->lockdep_depth;
|
| 5473 | |
| 5474 | * This function is about (re)setting the class of a held lock,
|
| 5475 | * yet we're not actually holding any locks. Naughty user!
|
| 5476 |
|
| 5477 | if (DEBUG_LOCKS_WARN_ON(!depth))
|
| 5478 | return 0;
|
| 5479 |
|
| 5480 | hlock = find_held_lock(curr, lock, depth, &i);
|
| 5481 | if (!hlock) {
|
| 5482 | print_unlock_imbalance_bug(curr, lock, ip);
|
| 5483 | return 0;
|
| 5484 | }
|
| 5485 |
|
| 5486 | lockdep_init_map_type(lock, name, key, 0,
|
| 5487 | lock->wait_type_inner,
|
| 5488 | lock->wait_type_outer,
|
| 5489 | lock->lock_type);
|
| 5490 | class = register_lock_class(lock, subclass, 0);
|
| 5491 | if (!class)
|
| 5492 | return 0;
|
| 5493 | hlock->class_idx = class - lock_classes;
|
| 5494 |
|
| 5495 | curr->lockdep_depth = i;
|
| 5496 | curr->curr_chain_key = hlock->prev_chain_key;
|
| 5497 |
|
| 5498 | if (reacquire_held_locks(curr, depth, i, &merged))
|
| 5499 | return 0;
|
| 5500 |
|
| 5501 | |
| 5502 | * I took it apart and put it back together again, except now I have
|
| 5503 | * these 'spare' parts.. where shall I put them.
|
| 5504 |
|
| 5505 | if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged))
|
| 5506 | return 0;
|
| 5507 | return 1;
|
| 5508 | }
|
| 5509 |
|
| 5510 | static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
|
| 5511 | {
|
| 5512 | struct task_struct *curr = current;
|
| 5513 | unsigned int depth, merged = 0;
|
| 5514 | struct held_lock *hlock;
|
| 5515 | int i;
|
| 5516 |
|
| 5517 | if (unlikely(!debug_locks))
|
| 5518 | return 0;
|
| 5519 |
|
| 5520 | depth = curr->lockdep_depth;
|
| 5521 | |
| 5522 | * This function is about (re)setting the class of a held lock,
|
| 5523 | * yet we're not actually holding any locks. Naughty user!
|
| 5524 |
|
| 5525 | if (DEBUG_LOCKS_WARN_ON(!depth))
|
| 5526 | return 0;
|
| 5527 |
|
| 5528 | hlock = find_held_lock(curr, lock, depth, &i);
|
| 5529 | if (!hlock) {
|
| 5530 | print_unlock_imbalance_bug(curr, lock, ip);
|
| 5531 | return 0;
|
| 5532 | }
|
| 5533 |
|
| 5534 | curr->lockdep_depth = i;
|
| 5535 | curr->curr_chain_key = hlock->prev_chain_key;
|
| 5536 |
|
| 5537 | WARN(hlock->read, "downgrading a read lock");
|
| 5538 | hlock->read = 1;
|
| 5539 | hlock->acquire_ip = ip;
|
| 5540 |
|
| 5541 | if (reacquire_held_locks(curr, depth, i, &merged))
|
| 5542 | return 0;
|
| 5543 |
|
| 5544 |
|
| 5545 | if (DEBUG_LOCKS_WARN_ON(merged))
|
| 5546 | return 0;
|
| 5547 |
|
| 5548 | |
| 5549 | * I took it apart and put it back together again, except now I have
|
| 5550 | * these 'spare' parts.. where shall I put them.
|
| 5551 |
|
| 5552 | if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
|
| 5553 | return 0;
|
| 5554 |
|
| 5555 | return 1;
|
| 5556 | }
|
| 5557 |
|
| 5558 | |
| 5559 | * Remove the lock from the list of currently held locks - this gets
|
| 5560 | * called on mutex_unlock()/spin_unlock*() (or on a failed
|
| 5561 | * mutex_lock_interruptible()).
|
| 5562 |
|
| 5563 | static int
|
| 5564 | __lock_release(struct lockdep_map *lock, unsigned long ip)
|
| 5565 | {
|
| 5566 | struct task_struct *curr = current;
|
| 5567 | unsigned int depth, merged = 1;
|
| 5568 | struct held_lock *hlock;
|
| 5569 | int i;
|
| 5570 |
|
| 5571 | if (unlikely(!debug_locks))
|
| 5572 | return 0;
|
| 5573 |
|
| 5574 | depth = curr->lockdep_depth;
|
| 5575 | |
| 5576 | * So we're all set to release this lock.. wait what lock? We don't
|
| 5577 | * own any locks, you've been drinking again?
|
| 5578 |
|
| 5579 | if (depth <= 0) {
|
| 5580 | print_unlock_imbalance_bug(curr, lock, ip);
|
| 5581 | return 0;
|
| 5582 | }
|
| 5583 |
|
| 5584 | |
| 5585 | * Check whether the lock exists in the current stack
|
| 5586 | * of held locks:
|
| 5587 |
|
| 5588 | hlock = find_held_lock(curr, lock, depth, &i);
|
| 5589 | if (!hlock) {
|
| 5590 | print_unlock_imbalance_bug(curr, lock, ip);
|
| 5591 | return 0;
|
| 5592 | }
|
| 5593 |
|
| 5594 | if (hlock->instance == lock)
|
| 5595 | lock_release_holdtime(hlock);
|
| 5596 |
|
| 5597 | WARN(hlock->pin_count, "releasing a pinned lock\n");
|
| 5598 |
|
| 5599 | if (hlock->references) {
|
| 5600 | hlock->references--;
|
| 5601 | if (hlock->references) {
|
| 5602 | |
| 5603 | * We had, and after removing one, still have
|
| 5604 | * references, the current lock stack is still
|
| 5605 | * valid. We're done!
|
| 5606 |
|
| 5607 | return 1;
|
| 5608 | }
|
| 5609 | }
|
| 5610 |
|
| 5611 | |
| 5612 | * We have the right lock to unlock, 'hlock' points to it.
|
| 5613 | * Now we remove it from the stack, and add back the other
|
| 5614 | * entries (if any), recalculating the hash along the way:
|
| 5615 |
|
| 5616 |
|
| 5617 | curr->lockdep_depth = i;
|
| 5618 | curr->curr_chain_key = hlock->prev_chain_key;
|
| 5619 |
|
| 5620 | |
| 5621 | * The most likely case is when the unlock is on the innermost
|
| 5622 | * lock. In this case, we are done!
|
| 5623 |
|
| 5624 | if (i == depth-1)
|
| 5625 | return 1;
|
| 5626 |
|
| 5627 | if (reacquire_held_locks(curr, depth, i + 1, &merged))
|
| 5628 | return 0;
|
| 5629 |
|
| 5630 | |
| 5631 | * We had N bottles of beer on the wall, we drank one, but now
|
| 5632 | * there's not N-1 bottles of beer left on the wall...
|
| 5633 | * Pouring two of the bottles together is acceptable.
|
| 5634 |
|
| 5635 | DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged);
|
| 5636 |
|
| 5637 | |
| 5638 | * Since reacquire_held_locks() would have called check_chain_key()
|
| 5639 | * indirectly via __lock_acquire(), we don't need to do it again
|
| 5640 | * on return.
|
| 5641 |
|
| 5642 | return 0;
|
| 5643 | }
|
| 5644 |
|
| 5645 | static __always_inline
|
| 5646 | int __lock_is_held(const struct lockdep_map *lock, int read)
|
| 5647 | {
|
| 5648 | struct task_struct *curr = current;
|
| 5649 | int i;
|
| 5650 |
|
| 5651 | for (i = 0; i < curr->lockdep_depth; i++) {
|
| 5652 | struct held_lock *hlock = curr->held_locks + i;
|
| 5653 |
|
| 5654 | if (match_held_lock(hlock, lock)) {
|
| 5655 | if (read == -1 || !!hlock->read == read)
|
| 5656 | return LOCK_STATE_HELD;
|
| 5657 |
|
| 5658 | return LOCK_STATE_NOT_HELD;
|
| 5659 | }
|
| 5660 | }
|
| 5661 |
|
| 5662 | return LOCK_STATE_NOT_HELD;
|
| 5663 | }
|
| 5664 |
|
| 5665 | static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
|
| 5666 | {
|
| 5667 | struct pin_cookie cookie = NIL_COOKIE;
|
| 5668 | struct task_struct *curr = current;
|
| 5669 | int i;
|
| 5670 |
|
| 5671 | if (unlikely(!debug_locks))
|
| 5672 | return cookie;
|
| 5673 |
|
| 5674 | for (i = 0; i < curr->lockdep_depth; i++) {
|
| 5675 | struct held_lock *hlock = curr->held_locks + i;
|
| 5676 |
|
| 5677 | if (match_held_lock(hlock, lock)) {
|
| 5678 | |
| 5679 | * Grab 16bits of randomness; this is sufficient to not
|
| 5680 | * be guessable and still allows some pin nesting in
|
| 5681 | * our u32 pin_count.
|
| 5682 |
|
| 5683 | cookie.val = 1 + (sched_clock() & 0xffff);
|
| 5684 | hlock->pin_count += cookie.val;
|
| 5685 | return cookie;
|
| 5686 | }
|
| 5687 | }
|
| 5688 |
|
| 5689 | WARN(1, "pinning an unheld lock\n");
|
| 5690 | return cookie;
|
| 5691 | }
|
| 5692 |
|
| 5693 | static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
|
| 5694 | {
|
| 5695 | struct task_struct *curr = current;
|
| 5696 | int i;
|
| 5697 |
|
| 5698 | if (unlikely(!debug_locks))
|
| 5699 | return;
|
| 5700 |
|
| 5701 | for (i = 0; i < curr->lockdep_depth; i++) {
|
| 5702 | struct held_lock *hlock = curr->held_locks + i;
|
| 5703 |
|
| 5704 | if (match_held_lock(hlock, lock)) {
|
| 5705 | hlock->pin_count += cookie.val;
|
| 5706 | return;
|
| 5707 | }
|
| 5708 | }
|
| 5709 |
|
| 5710 | WARN(1, "pinning an unheld lock\n");
|
| 5711 | }
|
| 5712 |
|
| 5713 | static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
|
| 5714 | {
|
| 5715 | struct task_struct *curr = current;
|
| 5716 | int i;
|
| 5717 |
|
| 5718 | if (unlikely(!debug_locks))
|
| 5719 | return;
|
| 5720 |
|
| 5721 | for (i = 0; i < curr->lockdep_depth; i++) {
|
| 5722 | struct held_lock *hlock = curr->held_locks + i;
|
| 5723 |
|
| 5724 | if (match_held_lock(hlock, lock)) {
|
| 5725 | int pin_count;
|
| 5726 |
|
| 5727 | if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
|
| 5728 | return;
|
| 5729 |
|
| 5730 | pin_count = hlock->pin_count - cookie.val;
|
| 5731 |
|
| 5732 | if (WARN(pin_count < 0, "pin count corrupted\n"))
|
| 5733 | pin_count = 0;
|
| 5734 |
|
| 5735 | hlock->pin_count = pin_count;
|
| 5736 | return;
|
| 5737 | }
|
| 5738 | }
|
| 5739 |
|
| 5740 | WARN(1, "unpinning an unheld lock\n");
|
| 5741 | }
|
| 5742 |
|
| 5743 | static u32 __lock_sequence(struct lockdep_map *lock)
|
| 5744 | {
|
| 5745 | struct task_struct *curr = current;
|
| 5746 | int i;
|
| 5747 |
|
| 5748 | if (unlikely(!debug_locks))
|
| 5749 | return ~0;
|
| 5750 |
|
| 5751 | for (i = 0; i < curr->lockdep_depth; i++) {
|
| 5752 | struct held_lock *hlock = curr->held_locks + i;
|
| 5753 |
|
| 5754 | if (match_held_lock(hlock, lock))
|
| 5755 | return hlock->seq_count;
|
| 5756 | }
|
| 5757 |
|
| 5758 | return ~0;
|
| 5759 | }
|
| 5760 |
|
| 5761 | |
| 5762 | * Check whether we follow the irq-flags state precisely:
|
| 5763 |
|
| 5764 | static noinstr void check_flags(unsigned long flags)
|
| 5765 | {
|
| 5766 | #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP)
|
| 5767 | if (!debug_locks)
|
| 5768 | return;
|
| 5769 |
|
| 5770 |
|
| 5771 | instrumentation_begin();
|
| 5772 |
|
| 5773 | if (irqs_disabled_flags(flags)) {
|
| 5774 | if (DEBUG_LOCKS_WARN_ON(lockdep_hardirqs_enabled())) {
|
| 5775 | printk("possible reason: unannotated irqs-off.\n");
|
| 5776 | }
|
| 5777 | } else {
|
| 5778 | if (DEBUG_LOCKS_WARN_ON(!lockdep_hardirqs_enabled())) {
|
| 5779 | printk("possible reason: unannotated irqs-on.\n");
|
| 5780 | }
|
| 5781 | }
|
| 5782 |
|
| 5783 | #ifndef CONFIG_PREEMPT_RT
|
| 5784 | |
| 5785 | * We dont accurately track softirq state in e.g.
|
| 5786 | * hardirq contexts (such as on 4KSTACKS), so only
|
| 5787 | * check if not in hardirq contexts:
|
| 5788 |
|
| 5789 | if (!hardirq_count()) {
|
| 5790 | if (softirq_count()) {
|
| 5791 |
|
| 5792 | DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
|
| 5793 | } else {
|
| 5794 |
|
| 5795 | DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
|
| 5796 | }
|
| 5797 | }
|
| 5798 | #endif
|
| 5799 |
|
| 5800 | if (!debug_locks)
|
| 5801 | print_irqtrace_events(current);
|
| 5802 |
|
| 5803 | instrumentation_end();
|
| 5804 | #endif
|
| 5805 | }
|
| 5806 |
|
| 5807 | void lock_set_class(struct lockdep_map *lock, const char *name,
|
| 5808 | struct lock_class_key *key, unsigned int subclass,
|
| 5809 | unsigned long ip)
|
| 5810 | {
|
| 5811 | unsigned long flags;
|
| 5812 |
|
| 5813 | if (unlikely(!lockdep_enabled()))
|
| 5814 | return;
|
| 5815 |
|
| 5816 | raw_local_irq_save(flags);
|
| 5817 | lockdep_recursion_inc();
|
| 5818 | check_flags(flags);
|
| 5819 | if (__lock_set_class(lock, name, key, subclass, ip))
|
| 5820 | check_chain_key(current);
|
| 5821 | lockdep_recursion_finish();
|
| 5822 | raw_local_irq_restore(flags);
|
| 5823 | }
|
| 5824 | EXPORT_SYMBOL_GPL(lock_set_class);
|
| 5825 |
|
| 5826 | void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
|
| 5827 | {
|
| 5828 | unsigned long flags;
|
| 5829 |
|
| 5830 | if (unlikely(!lockdep_enabled()))
|
| 5831 | return;
|
| 5832 |
|
| 5833 | raw_local_irq_save(flags);
|
| 5834 | lockdep_recursion_inc();
|
| 5835 | check_flags(flags);
|
| 5836 | if (__lock_downgrade(lock, ip))
|
| 5837 | check_chain_key(current);
|
| 5838 | lockdep_recursion_finish();
|
| 5839 | raw_local_irq_restore(flags);
|
| 5840 | }
|
| 5841 | EXPORT_SYMBOL_GPL(lock_downgrade);
|
| 5842 |
|
| 5843 |
|
| 5844 | static void verify_lock_unused(struct lockdep_map *lock, struct held_lock *hlock, int subclass)
|
| 5845 | {
|
| 5846 | #ifdef CONFIG_PROVE_LOCKING
|
| 5847 | struct lock_class *class = look_up_lock_class(lock, subclass);
|
| 5848 | unsigned long mask = LOCKF_USED;
|
| 5849 |
|
| 5850 |
|
| 5851 | if (!class)
|
| 5852 | return;
|
| 5853 |
|
| 5854 | |
| 5855 | * READ locks only conflict with USED, such that if we only ever use
|
| 5856 | * READ locks, there is no deadlock possible -- RCU.
|
| 5857 |
|
| 5858 | if (!hlock->read)
|
| 5859 | mask |= LOCKF_USED_READ;
|
| 5860 |
|
| 5861 | if (!(class->usage_mask & mask))
|
| 5862 | return;
|
| 5863 |
|
| 5864 | hlock->class_idx = class - lock_classes;
|
| 5865 |
|
| 5866 | print_usage_bug(current, hlock, LOCK_USED, LOCK_USAGE_STATES);
|
| 5867 | #endif
|
| 5868 | }
|
| 5869 |
|
| 5870 | static bool lockdep_nmi(void)
|
| 5871 | {
|
| 5872 | if (raw_cpu_read(lockdep_recursion))
|
| 5873 | return false;
|
| 5874 |
|
| 5875 | if (!in_nmi())
|
| 5876 | return false;
|
| 5877 |
|
| 5878 | return true;
|
| 5879 | }
|
| 5880 |
|
| 5881 | |
| 5882 | * read_lock() is recursive if:
|
| 5883 | * 1. We force lockdep think this way in selftests or
|
| 5884 | * 2. The implementation is not queued read/write lock or
|
| 5885 | * 3. The locker is at an in_interrupt() context.
|
| 5886 |
|
| 5887 | bool read_lock_is_recursive(void)
|
| 5888 | {
|
| 5889 | return force_read_lock_recursive ||
|
| 5890 | !IS_ENABLED(CONFIG_QUEUED_RWLOCKS) ||
|
| 5891 | in_interrupt();
|
| 5892 | }
|
| 5893 | EXPORT_SYMBOL_GPL(read_lock_is_recursive);
|
| 5894 |
|
| 5895 | |
| 5896 | * We are not always called with irqs disabled - do that here,
|
| 5897 | * and also avoid lockdep recursion:
|
| 5898 |
|
| 5899 | void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
|
| 5900 | int trylock, int read, int check,
|
| 5901 | struct lockdep_map *nest_lock, unsigned long ip)
|
| 5902 | {
|
| 5903 | unsigned long flags;
|
| 5904 |
|
| 5905 | trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
|
| 5906 |
|
| 5907 | if (!debug_locks)
|
| 5908 | return;
|
| 5909 |
|
| 5910 | |
| 5911 | * As KASAN instrumentation is disabled and lock_acquire() is usually
|
| 5912 | * the first lockdep call when a task tries to acquire a lock, add
|
| 5913 | * kasan_check_byte() here to check for use-after-free and other
|
| 5914 | * memory errors.
|
| 5915 |
|
| 5916 | kasan_check_byte(lock);
|
| 5917 |
|
| 5918 | if (unlikely(!lockdep_enabled())) {
|
| 5919 |
|
| 5920 | if (lockdep_nmi() && !trylock) {
|
| 5921 | struct held_lock hlock;
|
| 5922 |
|
| 5923 | hlock.acquire_ip = ip;
|
| 5924 | hlock.instance = lock;
|
| 5925 | hlock.nest_lock = nest_lock;
|
| 5926 | hlock.irq_context = 2; |
| 5927 | hlock.trylock = trylock;
|
| 5928 | hlock.read = read;
|
| 5929 | hlock.check = check;
|
| 5930 | hlock.hardirqs_off = true;
|
| 5931 | hlock.references = 0;
|
| 5932 |
|
| 5933 | verify_lock_unused(lock, &hlock, subclass);
|
| 5934 | }
|
| 5935 | return;
|
| 5936 | }
|
| 5937 |
|
| 5938 | raw_local_irq_save(flags);
|
| 5939 | check_flags(flags);
|
| 5940 |
|
| 5941 | lockdep_recursion_inc();
|
| 5942 | __lock_acquire(lock, subclass, trylock, read, check,
|
| 5943 | irqs_disabled_flags(flags), nest_lock, ip, 0, 0, 0,
|
| 5944 | ++current->lockdep_seq);
|
| 5945 | lockdep_recursion_finish();
|
| 5946 | raw_local_irq_restore(flags);
|
| 5947 | }
|
| 5948 | EXPORT_SYMBOL_GPL(lock_acquire);
|
| 5949 |
|
| 5950 | void lock_release(struct lockdep_map *lock, unsigned long ip)
|
| 5951 | {
|
| 5952 | unsigned long flags;
|
| 5953 |
|
| 5954 | trace_lock_release(lock, ip);
|
| 5955 |
|
| 5956 | if (unlikely(!lockdep_enabled() ||
|
| 5957 | lock->key == &__lockdep_no_track__))
|
| 5958 | return;
|
| 5959 |
|
| 5960 | raw_local_irq_save(flags);
|
| 5961 | check_flags(flags);
|
| 5962 |
|
| 5963 | lockdep_recursion_inc();
|
| 5964 | if (__lock_release(lock, ip))
|
| 5965 | check_chain_key(current);
|
| 5966 | lockdep_recursion_finish();
|
| 5967 | raw_local_irq_restore(flags);
|
| 5968 | }
|
| 5969 | EXPORT_SYMBOL_GPL(lock_release);
|
| 5970 |
|
| 5971 | |
| 5972 | * lock_sync() - A special annotation for synchronize_{s,}rcu()-like API.
|
| 5973 | *
|
| 5974 | * No actual critical section is created by the APIs annotated with this: these
|
| 5975 | * APIs are used to wait for one or multiple critical sections (on other CPUs
|
| 5976 | * or threads), and it means that calling these APIs inside these critical
|
| 5977 | * sections is potential deadlock.
|
| 5978 |
|
| 5979 | void lock_sync(struct lockdep_map *lock, unsigned subclass, int read,
|
| 5980 | int check, struct lockdep_map *nest_lock, unsigned long ip)
|
| 5981 | {
|
| 5982 | unsigned long flags;
|
| 5983 |
|
| 5984 | if (unlikely(!lockdep_enabled()))
|
| 5985 | return;
|
| 5986 |
|
| 5987 | raw_local_irq_save(flags);
|
| 5988 | check_flags(flags);
|
| 5989 |
|
| 5990 | lockdep_recursion_inc();
|
| 5991 | __lock_acquire(lock, subclass, 0, read, check,
|
| 5992 | irqs_disabled_flags(flags), nest_lock, ip, 0, 0, 1,
|
| 5993 | ++current->lockdep_seq);
|
| 5994 | check_chain_key(current);
|
| 5995 | lockdep_recursion_finish();
|
| 5996 | raw_local_irq_restore(flags);
|
| 5997 | }
|
| 5998 | EXPORT_SYMBOL_GPL(lock_sync);
|
| 5999 |
|
| 6000 | noinstr int lock_is_held_type(const struct lockdep_map *lock, int read)
|
| 6001 | {
|
| 6002 | unsigned long flags;
|
| 6003 | int ret = LOCK_STATE_NOT_HELD;
|
| 6004 |
|
| 6005 | |
| 6006 | * Avoid false negative lockdep_assert_held() and
|
| 6007 | * lockdep_assert_not_held().
|
| 6008 |
|
| 6009 | if (unlikely(!lockdep_enabled()))
|
| 6010 | return LOCK_STATE_UNKNOWN;
|
| 6011 |
|
| 6012 | raw_local_irq_save(flags);
|
| 6013 | check_flags(flags);
|
| 6014 |
|
| 6015 | lockdep_recursion_inc();
|
| 6016 | ret = __lock_is_held(lock, read);
|
| 6017 | lockdep_recursion_finish();
|
| 6018 | raw_local_irq_restore(flags);
|
| 6019 |
|
| 6020 | return ret;
|
| 6021 | }
|
| 6022 | EXPORT_SYMBOL_GPL(lock_is_held_type);
|
| 6023 | NOKPROBE_SYMBOL(lock_is_held_type);
|
| 6024 |
|
| 6025 | struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
|
| 6026 | {
|
| 6027 | struct pin_cookie cookie = NIL_COOKIE;
|
| 6028 | unsigned long flags;
|
| 6029 |
|
| 6030 | if (unlikely(!lockdep_enabled()))
|
| 6031 | return cookie;
|
| 6032 |
|
| 6033 | raw_local_irq_save(flags);
|
| 6034 | check_flags(flags);
|
| 6035 |
|
| 6036 | lockdep_recursion_inc();
|
| 6037 | cookie = __lock_pin_lock(lock);
|
| 6038 | lockdep_recursion_finish();
|
| 6039 | raw_local_irq_restore(flags);
|
| 6040 |
|
| 6041 | return cookie;
|
| 6042 | }
|
| 6043 | EXPORT_SYMBOL_GPL(lock_pin_lock);
|
| 6044 |
|
| 6045 | void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
|
| 6046 | {
|
| 6047 | unsigned long flags;
|
| 6048 |
|
| 6049 | if (unlikely(!lockdep_enabled()))
|
| 6050 | return;
|
| 6051 |
|
| 6052 | raw_local_irq_save(flags);
|
| 6053 | check_flags(flags);
|
| 6054 |
|
| 6055 | lockdep_recursion_inc();
|
| 6056 | __lock_repin_lock(lock, cookie);
|
| 6057 | lockdep_recursion_finish();
|
| 6058 | raw_local_irq_restore(flags);
|
| 6059 | }
|
| 6060 | EXPORT_SYMBOL_GPL(lock_repin_lock);
|
| 6061 |
|
| 6062 | void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
|
| 6063 | {
|
| 6064 | unsigned long flags;
|
| 6065 |
|
| 6066 | if (unlikely(!lockdep_enabled()))
|
| 6067 | return;
|
| 6068 |
|
| 6069 | raw_local_irq_save(flags);
|
| 6070 | check_flags(flags);
|
| 6071 |
|
| 6072 | lockdep_recursion_inc();
|
| 6073 | __lock_unpin_lock(lock, cookie);
|
| 6074 | lockdep_recursion_finish();
|
| 6075 | raw_local_irq_restore(flags);
|
| 6076 | }
|
| 6077 | EXPORT_SYMBOL_GPL(lock_unpin_lock);
|
| 6078 |
|
| 6079 | u32 lock_sequence(struct lockdep_map *lock)
|
| 6080 | {
|
| 6081 | unsigned long flags;
|
| 6082 | u32 seq = ~0;
|
| 6083 |
|
| 6084 | if (unlikely(!lockdep_enabled()))
|
| 6085 | return seq;
|
| 6086 |
|
| 6087 | raw_local_irq_save(flags);
|
| 6088 | check_flags(flags);
|
| 6089 |
|
| 6090 | lockdep_recursion_inc();
|
| 6091 | seq = __lock_sequence(lock);
|
| 6092 | lockdep_recursion_finish();
|
| 6093 | raw_local_irq_restore(flags);
|
| 6094 |
|
| 6095 | return seq;
|
| 6096 | }
|
| 6097 | EXPORT_SYMBOL_GPL(lock_sequence);
|
| 6098 |
|
| 6099 | #ifdef CONFIG_LOCK_STAT
|
| 6100 | static void print_lock_contention_bug(struct task_struct *curr,
|
| 6101 | struct lockdep_map *lock,
|
| 6102 | unsigned long ip)
|
| 6103 | {
|
| 6104 | if (!debug_locks_off())
|
| 6105 | return;
|
| 6106 | if (debug_locks_silent)
|
| 6107 | return;
|
| 6108 |
|
| 6109 | nbcon_cpu_emergency_enter();
|
| 6110 |
|
| 6111 | pr_warn("\n");
|
| 6112 | pr_warn("=================================\n");
|
| 6113 | pr_warn("WARNING: bad contention detected!\n");
|
| 6114 | print_kernel_ident();
|
| 6115 | pr_warn("---------------------------------\n");
|
| 6116 | pr_warn("%s/%d is trying to contend lock (",
|
| 6117 | curr->comm, task_pid_nr(curr));
|
| 6118 | print_lockdep_cache(lock);
|
| 6119 | pr_cont(") at:\n");
|
| 6120 | print_ip_sym(KERN_WARNING, ip);
|
| 6121 | pr_warn("but there are no locks held!\n");
|
| 6122 | pr_warn("\nother info that might help us debug this:\n");
|
| 6123 | lockdep_print_held_locks(curr);
|
| 6124 |
|
| 6125 | pr_warn("\nstack backtrace:\n");
|
| 6126 | dump_stack();
|
| 6127 |
|
| 6128 | nbcon_cpu_emergency_exit();
|
| 6129 | }
|
| 6130 |
|
| 6131 | static void
|
| 6132 | __lock_contended(struct lockdep_map *lock, unsigned long ip)
|
| 6133 | {
|
| 6134 | struct task_struct *curr = current;
|
| 6135 | struct held_lock *hlock;
|
| 6136 | struct lock_class_stats *stats;
|
| 6137 | unsigned int depth;
|
| 6138 | int i, contention_point, contending_point;
|
| 6139 |
|
| 6140 | depth = curr->lockdep_depth;
|
| 6141 | |
| 6142 | * Whee, we contended on this lock, except it seems we're not
|
| 6143 | * actually trying to acquire anything much at all..
|
| 6144 |
|
| 6145 | if (DEBUG_LOCKS_WARN_ON(!depth))
|
| 6146 | return;
|
| 6147 |
|
| 6148 | if (unlikely(lock->key == &__lockdep_no_track__))
|
| 6149 | return;
|
| 6150 |
|
| 6151 | hlock = find_held_lock(curr, lock, depth, &i);
|
| 6152 | if (!hlock) {
|
| 6153 | print_lock_contention_bug(curr, lock, ip);
|
| 6154 | return;
|
| 6155 | }
|
| 6156 |
|
| 6157 | if (hlock->instance != lock)
|
| 6158 | return;
|
| 6159 |
|
| 6160 | hlock->waittime_stamp = lockstat_clock();
|
| 6161 |
|
| 6162 | contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
|
| 6163 | contending_point = lock_point(hlock_class(hlock)->contending_point,
|
| 6164 | lock->ip);
|
| 6165 |
|
| 6166 | stats = get_lock_stats(hlock_class(hlock));
|
| 6167 | if (contention_point < LOCKSTAT_POINTS)
|
| 6168 | stats->contention_point[contention_point]++;
|
| 6169 | if (contending_point < LOCKSTAT_POINTS)
|
| 6170 | stats->contending_point[contending_point]++;
|
| 6171 | if (lock->cpu != smp_processor_id())
|
| 6172 | stats->bounces[bounce_contended + !!hlock->read]++;
|
| 6173 | }
|
| 6174 |
|
| 6175 | static void
|
| 6176 | __lock_acquired(struct lockdep_map *lock, unsigned long ip)
|
| 6177 | {
|
| 6178 | struct task_struct *curr = current;
|
| 6179 | struct held_lock *hlock;
|
| 6180 | struct lock_class_stats *stats;
|
| 6181 | unsigned int depth;
|
| 6182 | u64 now, waittime = 0;
|
| 6183 | int i, cpu;
|
| 6184 |
|
| 6185 | depth = curr->lockdep_depth;
|
| 6186 | |
| 6187 | * Yay, we acquired ownership of this lock we didn't try to
|
| 6188 | * acquire, how the heck did that happen?
|
| 6189 |
|
| 6190 | if (DEBUG_LOCKS_WARN_ON(!depth))
|
| 6191 | return;
|
| 6192 |
|
| 6193 | if (unlikely(lock->key == &__lockdep_no_track__))
|
| 6194 | return;
|
| 6195 |
|
| 6196 | hlock = find_held_lock(curr, lock, depth, &i);
|
| 6197 | if (!hlock) {
|
| 6198 | print_lock_contention_bug(curr, lock, _RET_IP_);
|
| 6199 | return;
|
| 6200 | }
|
| 6201 |
|
| 6202 | if (hlock->instance != lock)
|
| 6203 | return;
|
| 6204 |
|
| 6205 | cpu = smp_processor_id();
|
| 6206 | if (hlock->waittime_stamp) {
|
| 6207 | now = lockstat_clock();
|
| 6208 | waittime = now - hlock->waittime_stamp;
|
| 6209 | hlock->holdtime_stamp = now;
|
| 6210 | }
|
| 6211 |
|
| 6212 | stats = get_lock_stats(hlock_class(hlock));
|
| 6213 | if (waittime) {
|
| 6214 | if (hlock->read)
|
| 6215 | lock_time_inc(&stats->read_waittime, waittime);
|
| 6216 | else
|
| 6217 | lock_time_inc(&stats->write_waittime, waittime);
|
| 6218 | }
|
| 6219 | if (lock->cpu != cpu)
|
| 6220 | stats->bounces[bounce_acquired + !!hlock->read]++;
|
| 6221 |
|
| 6222 | lock->cpu = cpu;
|
| 6223 | lock->ip = ip;
|
| 6224 | }
|
| 6225 |
|
| 6226 | void lock_contended(struct lockdep_map *lock, unsigned long ip)
|
| 6227 | {
|
| 6228 | unsigned long flags;
|
| 6229 |
|
| 6230 | trace_lock_contended(lock, ip);
|
| 6231 |
|
| 6232 | if (unlikely(!lock_stat || !lockdep_enabled()))
|
| 6233 | return;
|
| 6234 |
|
| 6235 | raw_local_irq_save(flags);
|
| 6236 | check_flags(flags);
|
| 6237 | lockdep_recursion_inc();
|
| 6238 | __lock_contended(lock, ip);
|
| 6239 | lockdep_recursion_finish();
|
| 6240 | raw_local_irq_restore(flags);
|
| 6241 | }
|
| 6242 | EXPORT_SYMBOL_GPL(lock_contended);
|
| 6243 |
|
| 6244 | void lock_acquired(struct lockdep_map *lock, unsigned long ip)
|
| 6245 | {
|
| 6246 | unsigned long flags;
|
| 6247 |
|
| 6248 | trace_lock_acquired(lock, ip);
|
| 6249 |
|
| 6250 | if (unlikely(!lock_stat || !lockdep_enabled()))
|
| 6251 | return;
|
| 6252 |
|
| 6253 | raw_local_irq_save(flags);
|
| 6254 | check_flags(flags);
|
| 6255 | lockdep_recursion_inc();
|
| 6256 | __lock_acquired(lock, ip);
|
| 6257 | lockdep_recursion_finish();
|
| 6258 | raw_local_irq_restore(flags);
|
| 6259 | }
|
| 6260 | EXPORT_SYMBOL_GPL(lock_acquired);
|
| 6261 | #endif
|
| 6262 |
|
| 6263 | |
| 6264 | * Used by the testsuite, sanitize the validator state
|
| 6265 | * after a simulated failure:
|
| 6266 |
|
| 6267 |
|
| 6268 | void lockdep_reset(void)
|
| 6269 | {
|
| 6270 | unsigned long flags;
|
| 6271 | int i;
|
| 6272 |
|
| 6273 | raw_local_irq_save(flags);
|
| 6274 | lockdep_init_task(current);
|
| 6275 | memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
|
| 6276 | nr_hardirq_chains = 0;
|
| 6277 | nr_softirq_chains = 0;
|
| 6278 | nr_process_chains = 0;
|
| 6279 | debug_locks = 1;
|
| 6280 | for (i = 0; i < CHAINHASH_SIZE; i++)
|
| 6281 | INIT_HLIST_HEAD(chainhash_table + i);
|
| 6282 | raw_local_irq_restore(flags);
|
| 6283 | }
|
| 6284 |
|
| 6285 |
|
| 6286 | static void remove_class_from_lock_chain(struct pending_free *pf,
|
| 6287 | struct lock_chain *chain,
|
| 6288 | struct lock_class *class)
|
| 6289 | {
|
| 6290 | #ifdef CONFIG_PROVE_LOCKING
|
| 6291 | int i;
|
| 6292 |
|
| 6293 | for (i = chain->base; i < chain->base + chain->depth; i++) {
|
| 6294 | if (chain_hlock_class_idx(chain_hlocks[i]) != class - lock_classes)
|
| 6295 | continue;
|
| 6296 | |
| 6297 | * Each lock class occurs at most once in a lock chain so once
|
| 6298 | * we found a match we can break out of this loop.
|
| 6299 |
|
| 6300 | goto free_lock_chain;
|
| 6301 | }
|
| 6302 |
|
| 6303 | return;
|
| 6304 |
|
| 6305 | free_lock_chain:
|
| 6306 | free_chain_hlocks(chain->base, chain->depth);
|
| 6307 |
|
| 6308 | WRITE_ONCE(chain->chain_key, INITIAL_CHAIN_KEY);
|
| 6309 | dec_chains(chain->irq_context);
|
| 6310 |
|
| 6311 | |
| 6312 | * Note: calling hlist_del_rcu() from inside a
|
| 6313 | * hlist_for_each_entry_rcu() loop is safe.
|
| 6314 |
|
| 6315 | hlist_del_rcu(&chain->entry);
|
| 6316 | __set_bit(chain - lock_chains, pf->lock_chains_being_freed);
|
| 6317 | nr_zapped_lock_chains++;
|
| 6318 | #endif
|
| 6319 | }
|
| 6320 |
|
| 6321 |
|
| 6322 | static void remove_class_from_lock_chains(struct pending_free *pf,
|
| 6323 | struct lock_class *class)
|
| 6324 | {
|
| 6325 | struct lock_chain *chain;
|
| 6326 | struct hlist_head *head;
|
| 6327 | int i;
|
| 6328 |
|
| 6329 | for (i = 0; i < ARRAY_SIZE(chainhash_table); i++) {
|
| 6330 | head = chainhash_table + i;
|
| 6331 | hlist_for_each_entry_rcu(chain, head, entry) {
|
| 6332 | remove_class_from_lock_chain(pf, chain, class);
|
| 6333 | }
|
| 6334 | }
|
| 6335 | }
|
| 6336 |
|
| 6337 | |
| 6338 | * Remove all references to a lock class. The caller must hold the graph lock.
|
| 6339 |
|
| 6340 | static void zap_class(struct pending_free *pf, struct lock_class *class)
|
| 6341 | {
|
| 6342 | struct lock_list *entry;
|
| 6343 | int i;
|
| 6344 |
|
| 6345 | WARN_ON_ONCE(!class->key);
|
| 6346 |
|
| 6347 | |
| 6348 | * Remove all dependencies this lock is
|
| 6349 | * involved in:
|
| 6350 |
|
| 6351 | for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
|
| 6352 | entry = list_entries + i;
|
| 6353 | if (entry->class != class && entry->links_to != class)
|
| 6354 | continue;
|
| 6355 | __clear_bit(i, list_entries_in_use);
|
| 6356 | nr_list_entries--;
|
| 6357 | list_del_rcu(&entry->entry);
|
| 6358 | }
|
| 6359 | if (list_empty(&class->locks_after) &&
|
| 6360 | list_empty(&class->locks_before)) {
|
| 6361 | list_move_tail(&class->lock_entry, &pf->zapped);
|
| 6362 | hlist_del_rcu(&class->hash_entry);
|
| 6363 | WRITE_ONCE(class->key, NULL);
|
| 6364 | WRITE_ONCE(class->name, NULL);
|
| 6365 |
|
| 6366 | if (class->usage_mask == 0)
|
| 6367 | debug_atomic_dec(nr_unused_locks);
|
| 6368 | nr_lock_classes--;
|
| 6369 | __clear_bit(class - lock_classes, lock_classes_in_use);
|
| 6370 | if (class - lock_classes == max_lock_class_idx)
|
| 6371 | max_lock_class_idx--;
|
| 6372 | } else {
|
| 6373 | WARN_ONCE(true, "%s() failed for class %s\n", __func__,
|
| 6374 | class->name);
|
| 6375 | }
|
| 6376 |
|
| 6377 | remove_class_from_lock_chains(pf, class);
|
| 6378 | nr_zapped_classes++;
|
| 6379 | }
|
| 6380 |
|
| 6381 | static void reinit_class(struct lock_class *class)
|
| 6382 | {
|
| 6383 | WARN_ON_ONCE(!class->lock_entry.next);
|
| 6384 | WARN_ON_ONCE(!list_empty(&class->locks_after));
|
| 6385 | WARN_ON_ONCE(!list_empty(&class->locks_before));
|
| 6386 | memset_startat(class, 0, key);
|
| 6387 | WARN_ON_ONCE(!class->lock_entry.next);
|
| 6388 | WARN_ON_ONCE(!list_empty(&class->locks_after));
|
| 6389 | WARN_ON_ONCE(!list_empty(&class->locks_before));
|
| 6390 | }
|
| 6391 |
|
| 6392 | static inline int within(const void *addr, void *start, unsigned long size)
|
| 6393 | {
|
| 6394 | return addr >= start && addr < start + size;
|
| 6395 | }
|
| 6396 |
|
| 6397 | static bool inside_selftest(void)
|
| 6398 | {
|
| 6399 | return current == lockdep_selftest_task_struct;
|
| 6400 | }
|
| 6401 |
|
| 6402 |
|
| 6403 | static struct pending_free *get_pending_free(void)
|
| 6404 | {
|
| 6405 | return delayed_free.pf + delayed_free.index;
|
| 6406 | }
|
| 6407 |
|
| 6408 | static void free_zapped_rcu(struct rcu_head *cb);
|
| 6409 |
|
| 6410 | |
| 6411 | * See if we need to queue an RCU callback, must called with
|
| 6412 | * the lockdep lock held, returns false if either we don't have
|
| 6413 | * any pending free or the callback is already scheduled.
|
| 6414 | * Otherwise, a call_rcu() must follow this function call.
|
| 6415 |
|
| 6416 | static bool prepare_call_rcu_zapped(struct pending_free *pf)
|
| 6417 | {
|
| 6418 | WARN_ON_ONCE(inside_selftest());
|
| 6419 |
|
| 6420 | if (list_empty(&pf->zapped))
|
| 6421 | return false;
|
| 6422 |
|
| 6423 | if (delayed_free.scheduled)
|
| 6424 | return false;
|
| 6425 |
|
| 6426 | delayed_free.scheduled = true;
|
| 6427 |
|
| 6428 | WARN_ON_ONCE(delayed_free.pf + delayed_free.index != pf);
|
| 6429 | delayed_free.index ^= 1;
|
| 6430 |
|
| 6431 | return true;
|
| 6432 | }
|
| 6433 |
|
| 6434 |
|
| 6435 | static void __free_zapped_classes(struct pending_free *pf)
|
| 6436 | {
|
| 6437 | struct lock_class *class;
|
| 6438 |
|
| 6439 | check_data_structures();
|
| 6440 |
|
| 6441 | list_for_each_entry(class, &pf->zapped, lock_entry)
|
| 6442 | reinit_class(class);
|
| 6443 |
|
| 6444 | list_splice_init(&pf->zapped, &free_lock_classes);
|
| 6445 |
|
| 6446 | #ifdef CONFIG_PROVE_LOCKING
|
| 6447 | bitmap_andnot(lock_chains_in_use, lock_chains_in_use,
|
| 6448 | pf->lock_chains_being_freed, ARRAY_SIZE(lock_chains));
|
| 6449 | bitmap_clear(pf->lock_chains_being_freed, 0, ARRAY_SIZE(lock_chains));
|
| 6450 | #endif
|
| 6451 | }
|
| 6452 |
|
| 6453 | static void free_zapped_rcu(struct rcu_head *ch)
|
| 6454 | {
|
| 6455 | struct pending_free *pf;
|
| 6456 | unsigned long flags;
|
| 6457 | bool need_callback;
|
| 6458 |
|
| 6459 | if (WARN_ON_ONCE(ch != &delayed_free.rcu_head))
|
| 6460 | return;
|
| 6461 |
|
| 6462 | raw_local_irq_save(flags);
|
| 6463 | lockdep_lock();
|
| 6464 |
|
| 6465 |
|
| 6466 | pf = delayed_free.pf + (delayed_free.index ^ 1);
|
| 6467 | __free_zapped_classes(pf);
|
| 6468 | delayed_free.scheduled = false;
|
| 6469 | need_callback =
|
| 6470 | prepare_call_rcu_zapped(delayed_free.pf + delayed_free.index);
|
| 6471 | lockdep_unlock();
|
| 6472 | raw_local_irq_restore(flags);
|
| 6473 |
|
| 6474 | |
| 6475 | * If there's pending free and its callback has not been scheduled,
|
| 6476 | * queue an RCU callback.
|
| 6477 |
|
| 6478 | if (need_callback)
|
| 6479 | call_rcu(&delayed_free.rcu_head, free_zapped_rcu);
|
| 6480 |
|
| 6481 | }
|
| 6482 |
|
| 6483 | |
| 6484 | * Remove all lock classes from the class hash table and from the
|
| 6485 | * all_lock_classes list whose key or name is in the address range [start,
|
| 6486 | * start + size). Move these lock classes to the zapped_classes list. Must
|
| 6487 | * be called with the graph lock held.
|
| 6488 |
|
| 6489 | static void __lockdep_free_key_range(struct pending_free *pf, void *start,
|
| 6490 | unsigned long size)
|
| 6491 | {
|
| 6492 | struct lock_class *class;
|
| 6493 | struct hlist_head *head;
|
| 6494 | int i;
|
| 6495 |
|
| 6496 |
|
| 6497 | for (i = 0; i < CLASSHASH_SIZE; i++) {
|
| 6498 | head = classhash_table + i;
|
| 6499 | hlist_for_each_entry_rcu(class, head, hash_entry) {
|
| 6500 | if (!within(class->key, start, size) &&
|
| 6501 | !within(class->name, start, size))
|
| 6502 | continue;
|
| 6503 | zap_class(pf, class);
|
| 6504 | }
|
| 6505 | }
|
| 6506 | }
|
| 6507 |
|
| 6508 | |
| 6509 | * Used in module.c to remove lock classes from memory that is going to be
|
| 6510 | * freed; and possibly re-used by other modules.
|
| 6511 | *
|
| 6512 | * We will have had one synchronize_rcu() before getting here, so we're
|
| 6513 | * guaranteed nobody will look up these exact classes -- they're properly dead
|
| 6514 | * but still allocated.
|
| 6515 |
|
| 6516 | static void lockdep_free_key_range_reg(void *start, unsigned long size)
|
| 6517 | {
|
| 6518 | struct pending_free *pf;
|
| 6519 | unsigned long flags;
|
| 6520 | bool need_callback;
|
| 6521 |
|
| 6522 | init_data_structures_once();
|
| 6523 |
|
| 6524 | raw_local_irq_save(flags);
|
| 6525 | lockdep_lock();
|
| 6526 | pf = get_pending_free();
|
| 6527 | __lockdep_free_key_range(pf, start, size);
|
| 6528 | need_callback = prepare_call_rcu_zapped(pf);
|
| 6529 | lockdep_unlock();
|
| 6530 | raw_local_irq_restore(flags);
|
| 6531 | if (need_callback)
|
| 6532 | call_rcu(&delayed_free.rcu_head, free_zapped_rcu);
|
| 6533 | |
| 6534 | * Wait for any possible iterators from look_up_lock_class() to pass
|
| 6535 | * before continuing to free the memory they refer to.
|
| 6536 |
|
| 6537 | synchronize_rcu();
|
| 6538 | }
|
| 6539 |
|
| 6540 | |
| 6541 | * Free all lockdep keys in the range [start, start+size). Does not sleep.
|
| 6542 | * Ignores debug_locks. Must only be used by the lockdep selftests.
|
| 6543 |
|
| 6544 | static void lockdep_free_key_range_imm(void *start, unsigned long size)
|
| 6545 | {
|
| 6546 | struct pending_free *pf = delayed_free.pf;
|
| 6547 | unsigned long flags;
|
| 6548 |
|
| 6549 | init_data_structures_once();
|
| 6550 |
|
| 6551 | raw_local_irq_save(flags);
|
| 6552 | lockdep_lock();
|
| 6553 | __lockdep_free_key_range(pf, start, size);
|
| 6554 | __free_zapped_classes(pf);
|
| 6555 | lockdep_unlock();
|
| 6556 | raw_local_irq_restore(flags);
|
| 6557 | }
|
| 6558 |
|
| 6559 | void lockdep_free_key_range(void *start, unsigned long size)
|
| 6560 | {
|
| 6561 | init_data_structures_once();
|
| 6562 |
|
| 6563 | if (inside_selftest())
|
| 6564 | lockdep_free_key_range_imm(start, size);
|
| 6565 | else
|
| 6566 | lockdep_free_key_range_reg(start, size);
|
| 6567 | }
|
| 6568 |
|
| 6569 | |
| 6570 | * Check whether any element of the @lock->class_cache[] array refers to a
|
| 6571 | * registered lock class. The caller must hold either the graph lock or the
|
| 6572 | * RCU read lock.
|
| 6573 |
|
| 6574 | static bool lock_class_cache_is_registered(struct lockdep_map *lock)
|
| 6575 | {
|
| 6576 | struct lock_class *class;
|
| 6577 | struct hlist_head *head;
|
| 6578 | int i, j;
|
| 6579 |
|
| 6580 | for (i = 0; i < CLASSHASH_SIZE; i++) {
|
| 6581 | head = classhash_table + i;
|
| 6582 | hlist_for_each_entry_rcu(class, head, hash_entry) {
|
| 6583 | for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
|
| 6584 | if (lock->class_cache[j] == class)
|
| 6585 | return true;
|
| 6586 | }
|
| 6587 | }
|
| 6588 | return false;
|
| 6589 | }
|
| 6590 |
|
| 6591 |
|
| 6592 | static void __lockdep_reset_lock(struct pending_free *pf,
|
| 6593 | struct lockdep_map *lock)
|
| 6594 | {
|
| 6595 | struct lock_class *class;
|
| 6596 | int j;
|
| 6597 |
|
| 6598 | |
| 6599 | * Remove all classes this lock might have:
|
| 6600 |
|
| 6601 | for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
|
| 6602 | |
| 6603 | * If the class exists we look it up and zap it:
|
| 6604 |
|
| 6605 | class = look_up_lock_class(lock, j);
|
| 6606 | if (class)
|
| 6607 | zap_class(pf, class);
|
| 6608 | }
|
| 6609 | |
| 6610 | * Debug check: in the end all mapped classes should
|
| 6611 | * be gone.
|
| 6612 |
|
| 6613 | if (WARN_ON_ONCE(lock_class_cache_is_registered(lock)))
|
| 6614 | debug_locks_off();
|
| 6615 | }
|
| 6616 |
|
| 6617 | |
| 6618 | * Remove all information lockdep has about a lock if debug_locks == 1. Free
|
| 6619 | * released data structures from RCU context.
|
| 6620 |
|
| 6621 | static void lockdep_reset_lock_reg(struct lockdep_map *lock)
|
| 6622 | {
|
| 6623 | struct pending_free *pf;
|
| 6624 | unsigned long flags;
|
| 6625 | int locked;
|
| 6626 | bool need_callback = false;
|
| 6627 |
|
| 6628 | raw_local_irq_save(flags);
|
| 6629 | locked = graph_lock();
|
| 6630 | if (!locked)
|
| 6631 | goto out_irq;
|
| 6632 |
|
| 6633 | pf = get_pending_free();
|
| 6634 | __lockdep_reset_lock(pf, lock);
|
| 6635 | need_callback = prepare_call_rcu_zapped(pf);
|
| 6636 |
|
| 6637 | graph_unlock();
|
| 6638 | out_irq:
|
| 6639 | raw_local_irq_restore(flags);
|
| 6640 | if (need_callback)
|
| 6641 | call_rcu(&delayed_free.rcu_head, free_zapped_rcu);
|
| 6642 | }
|
| 6643 |
|
| 6644 | |
| 6645 | * Reset a lock. Does not sleep. Ignores debug_locks. Must only be used by the
|
| 6646 | * lockdep selftests.
|
| 6647 |
|
| 6648 | static void lockdep_reset_lock_imm(struct lockdep_map *lock)
|
| 6649 | {
|
| 6650 | struct pending_free *pf = delayed_free.pf;
|
| 6651 | unsigned long flags;
|
| 6652 |
|
| 6653 | raw_local_irq_save(flags);
|
| 6654 | lockdep_lock();
|
| 6655 | __lockdep_reset_lock(pf, lock);
|
| 6656 | __free_zapped_classes(pf);
|
| 6657 | lockdep_unlock();
|
| 6658 | raw_local_irq_restore(flags);
|
| 6659 | }
|
| 6660 |
|
| 6661 | void lockdep_reset_lock(struct lockdep_map *lock)
|
| 6662 | {
|
| 6663 | init_data_structures_once();
|
| 6664 |
|
| 6665 | if (inside_selftest())
|
| 6666 | lockdep_reset_lock_imm(lock);
|
| 6667 | else
|
| 6668 | lockdep_reset_lock_reg(lock);
|
| 6669 | }
|
| 6670 |
|
| 6671 | |
| 6672 | * Unregister a dynamically allocated key.
|
| 6673 | *
|
| 6674 | * Unlike lockdep_register_key(), a search is always done to find a matching
|
| 6675 | * key irrespective of debug_locks to avoid potential invalid access to freed
|
| 6676 | * memory in lock_class entry.
|
| 6677 |
|
| 6678 | void lockdep_unregister_key(struct lock_class_key *key)
|
| 6679 | {
|
| 6680 | struct hlist_head *hash_head = keyhashentry(key);
|
| 6681 | struct lock_class_key *k;
|
| 6682 | struct pending_free *pf;
|
| 6683 | unsigned long flags;
|
| 6684 | bool found = false;
|
| 6685 | bool need_callback = false;
|
| 6686 |
|
| 6687 | might_sleep();
|
| 6688 |
|
| 6689 | if (WARN_ON_ONCE(static_obj(key)))
|
| 6690 | return;
|
| 6691 |
|
| 6692 | raw_local_irq_save(flags);
|
| 6693 | lockdep_lock();
|
| 6694 |
|
| 6695 | hlist_for_each_entry_rcu(k, hash_head, hash_entry) {
|
| 6696 | if (k == key) {
|
| 6697 | hlist_del_rcu(&k->hash_entry);
|
| 6698 | found = true;
|
| 6699 | break;
|
| 6700 | }
|
| 6701 | }
|
| 6702 | WARN_ON_ONCE(!found && debug_locks);
|
| 6703 | if (found) {
|
| 6704 | pf = get_pending_free();
|
| 6705 | __lockdep_free_key_range(pf, key, 1);
|
| 6706 | need_callback = prepare_call_rcu_zapped(pf);
|
| 6707 | nr_dynamic_keys--;
|
| 6708 | }
|
| 6709 | lockdep_unlock();
|
| 6710 | raw_local_irq_restore(flags);
|
| 6711 |
|
| 6712 | if (need_callback)
|
| 6713 | call_rcu(&delayed_free.rcu_head, free_zapped_rcu);
|
| 6714 |
|
| 6715 | |
| 6716 | * Wait until is_dynamic_key() has finished accessing k->hash_entry.
|
| 6717 | *
|
| 6718 | * Some operations like __qdisc_destroy() will call this in a debug
|
| 6719 | * kernel, and the network traffic is disabled while waiting, hence
|
| 6720 | * the delay of the wait matters in debugging cases. Currently use a
|
| 6721 | * synchronize_rcu_expedited() to speed up the wait at the cost of
|
| 6722 | * system IPIs. TODO: Replace RCU with hazptr for this.
|
| 6723 |
|
| 6724 | synchronize_rcu_expedited();
|
| 6725 | }
|
| 6726 | EXPORT_SYMBOL_GPL(lockdep_unregister_key);
|
| 6727 |
|
| 6728 | void __init lockdep_init(void)
|
| 6729 | {
|
| 6730 | pr_info("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
|
| 6731 |
|
| 6732 | pr_info("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
|
| 6733 | pr_info("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
|
| 6734 | pr_info("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
|
| 6735 | pr_info("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
|
| 6736 | pr_info("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
|
| 6737 | pr_info("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
|
| 6738 | pr_info("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
|
| 6739 |
|
| 6740 | pr_info(" memory used by lock dependency info: %zu kB\n",
|
| 6741 | (sizeof(lock_classes) +
|
| 6742 | sizeof(lock_classes_in_use) +
|
| 6743 | sizeof(classhash_table) +
|
| 6744 | sizeof(list_entries) +
|
| 6745 | sizeof(list_entries_in_use) +
|
| 6746 | sizeof(chainhash_table) +
|
| 6747 | sizeof(delayed_free)
|
| 6748 | #ifdef CONFIG_PROVE_LOCKING
|
| 6749 | + sizeof(lock_cq)
|
| 6750 | + sizeof(lock_chains)
|
| 6751 | + sizeof(lock_chains_in_use)
|
| 6752 | + sizeof(chain_hlocks)
|
| 6753 | #endif
|
| 6754 | ) / 1024
|
| 6755 | );
|
| 6756 |
|
| 6757 | #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
|
| 6758 | pr_info(" memory used for stack traces: %zu kB\n",
|
| 6759 | (sizeof(stack_trace) + sizeof(stack_trace_hash)) / 1024
|
| 6760 | );
|
| 6761 | #endif
|
| 6762 |
|
| 6763 | pr_info(" per task-struct memory footprint: %zu bytes\n",
|
| 6764 | sizeof(((struct task_struct *)NULL)->held_locks));
|
| 6765 | }
|
| 6766 |
|
| 6767 | static void
|
| 6768 | print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
|
| 6769 | const void *mem_to, struct held_lock *hlock)
|
| 6770 | {
|
| 6771 | if (!debug_locks_off())
|
| 6772 | return;
|
| 6773 | if (debug_locks_silent)
|
| 6774 | return;
|
| 6775 |
|
| 6776 | nbcon_cpu_emergency_enter();
|
| 6777 |
|
| 6778 | pr_warn("\n");
|
| 6779 | pr_warn("=========================\n");
|
| 6780 | pr_warn("WARNING: held lock freed!\n");
|
| 6781 | print_kernel_ident();
|
| 6782 | pr_warn("-------------------------\n");
|
| 6783 | pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n",
|
| 6784 | curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
|
| 6785 | print_lock(hlock);
|
| 6786 | lockdep_print_held_locks(curr);
|
| 6787 |
|
| 6788 | pr_warn("\nstack backtrace:\n");
|
| 6789 | dump_stack();
|
| 6790 |
|
| 6791 | nbcon_cpu_emergency_exit();
|
| 6792 | }
|
| 6793 |
|
| 6794 | static inline int not_in_range(const void* mem_from, unsigned long mem_len,
|
| 6795 | const void* lock_from, unsigned long lock_len)
|
| 6796 | {
|
| 6797 | return lock_from + lock_len <= mem_from ||
|
| 6798 | mem_from + mem_len <= lock_from;
|
| 6799 | }
|
| 6800 |
|
| 6801 | |
| 6802 | * Called when kernel memory is freed (or unmapped), or if a lock
|
| 6803 | * is destroyed or reinitialized - this code checks whether there is
|
| 6804 | * any held lock in the memory range of <from> to <to>:
|
| 6805 |
|
| 6806 | void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
|
| 6807 | {
|
| 6808 | struct task_struct *curr = current;
|
| 6809 | struct held_lock *hlock;
|
| 6810 | unsigned long flags;
|
| 6811 | int i;
|
| 6812 |
|
| 6813 | if (unlikely(!debug_locks))
|
| 6814 | return;
|
| 6815 |
|
| 6816 | raw_local_irq_save(flags);
|
| 6817 | for (i = 0; i < curr->lockdep_depth; i++) {
|
| 6818 | hlock = curr->held_locks + i;
|
| 6819 |
|
| 6820 | if (not_in_range(mem_from, mem_len, hlock->instance,
|
| 6821 | sizeof(*hlock->instance)))
|
| 6822 | continue;
|
| 6823 |
|
| 6824 | print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
|
| 6825 | break;
|
| 6826 | }
|
| 6827 | raw_local_irq_restore(flags);
|
| 6828 | }
|
| 6829 | EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
|
| 6830 |
|
| 6831 | static void print_held_locks_bug(void)
|
| 6832 | {
|
| 6833 | if (!debug_locks_off())
|
| 6834 | return;
|
| 6835 | if (debug_locks_silent)
|
| 6836 | return;
|
| 6837 |
|
| 6838 | nbcon_cpu_emergency_enter();
|
| 6839 |
|
| 6840 | pr_warn("\n");
|
| 6841 | pr_warn("====================================\n");
|
| 6842 | pr_warn("WARNING: %s/%d still has locks held!\n",
|
| 6843 | current->comm, task_pid_nr(current));
|
| 6844 | print_kernel_ident();
|
| 6845 | pr_warn("------------------------------------\n");
|
| 6846 | lockdep_print_held_locks(current);
|
| 6847 | pr_warn("\nstack backtrace:\n");
|
| 6848 | dump_stack();
|
| 6849 |
|
| 6850 | nbcon_cpu_emergency_exit();
|
| 6851 | }
|
| 6852 |
|
| 6853 | void debug_check_no_locks_held(void)
|
| 6854 | {
|
| 6855 | if (unlikely(current->lockdep_depth > 0))
|
| 6856 | print_held_locks_bug();
|
| 6857 | }
|
| 6858 | EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
|
| 6859 |
|
| 6860 | #ifdef __KERNEL__
|
| 6861 | void debug_show_all_locks(void)
|
| 6862 | {
|
| 6863 | struct task_struct *g, *p;
|
| 6864 |
|
| 6865 | if (unlikely(!debug_locks)) {
|
| 6866 | pr_warn("INFO: lockdep is turned off.\n");
|
| 6867 | return;
|
| 6868 | }
|
| 6869 | pr_warn("\nShowing all locks held in the system:\n");
|
| 6870 |
|
| 6871 | rcu_read_lock();
|
| 6872 | for_each_process_thread(g, p) {
|
| 6873 | if (!p->lockdep_depth)
|
| 6874 | continue;
|
| 6875 | lockdep_print_held_locks(p);
|
| 6876 | touch_nmi_watchdog();
|
| 6877 | touch_all_softlockup_watchdogs();
|
| 6878 | }
|
| 6879 | rcu_read_unlock();
|
| 6880 |
|
| 6881 | pr_warn("\n");
|
| 6882 | pr_warn("=============================================\n\n");
|
| 6883 | }
|
| 6884 | EXPORT_SYMBOL_GPL(debug_show_all_locks);
|
| 6885 | #endif
|
| 6886 |
|
| 6887 | |
| 6888 | * Careful: only use this function if you are sure that
|
| 6889 | * the task cannot run in parallel!
|
| 6890 |
|
| 6891 | void debug_show_held_locks(struct task_struct *task)
|
| 6892 | {
|
| 6893 | if (unlikely(!debug_locks)) {
|
| 6894 | printk("INFO: lockdep is turned off.\n");
|
| 6895 | return;
|
| 6896 | }
|
| 6897 | lockdep_print_held_locks(task);
|
| 6898 | }
|
| 6899 | EXPORT_SYMBOL_GPL(debug_show_held_locks);
|
| 6900 |
|
| 6901 | asmlinkage __visible void lockdep_sys_exit(void)
|
| 6902 | {
|
| 6903 | struct task_struct *curr = current;
|
| 6904 |
|
| 6905 | if (unlikely(curr->lockdep_depth)) {
|
| 6906 | if (!debug_locks_off())
|
| 6907 | return;
|
| 6908 | nbcon_cpu_emergency_enter();
|
| 6909 | pr_warn("\n");
|
| 6910 | pr_warn("================================================\n");
|
| 6911 | pr_warn("WARNING: lock held when returning to user space!\n");
|
| 6912 | print_kernel_ident();
|
| 6913 | pr_warn("------------------------------------------------\n");
|
| 6914 | pr_warn("%s/%d is leaving the kernel with locks still held!\n",
|
| 6915 | curr->comm, curr->pid);
|
| 6916 | lockdep_print_held_locks(curr);
|
| 6917 | nbcon_cpu_emergency_exit();
|
| 6918 | }
|
| 6919 |
|
| 6920 | |
| 6921 | * The lock history for each syscall should be independent. So wipe the
|
| 6922 | * slate clean on return to userspace.
|
| 6923 |
|
| 6924 | lockdep_invariant_state(false);
|
| 6925 | }
|
| 6926 |
|
| 6927 | void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
|
| 6928 | {
|
| 6929 | struct task_struct *curr = current;
|
| 6930 | int dl = READ_ONCE(debug_locks);
|
| 6931 | bool rcu = warn_rcu_enter();
|
| 6932 |
|
| 6933 |
|
| 6934 | nbcon_cpu_emergency_enter();
|
| 6935 | pr_warn("\n");
|
| 6936 | pr_warn("=============================\n");
|
| 6937 | pr_warn("WARNING: suspicious RCU usage\n");
|
| 6938 | print_kernel_ident();
|
| 6939 | pr_warn("-----------------------------\n");
|
| 6940 | pr_warn("%s:%d %s!\n", file, line, s);
|
| 6941 | pr_warn("\nother info that might help us debug this:\n\n");
|
| 6942 | pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n%s",
|
| 6943 | !rcu_lockdep_current_cpu_online()
|
| 6944 | ? "RCU used illegally from offline CPU!\n"
|
| 6945 | : "",
|
| 6946 | rcu_scheduler_active, dl,
|
| 6947 | dl ? "" : "Possible false positive due to lockdep disabling via debug_locks = 0\n");
|
| 6948 |
|
| 6949 | |
| 6950 | * If a CPU is in the RCU-free window in idle (ie: in the section
|
| 6951 | * between ct_idle_enter() and ct_idle_exit(), then RCU
|
| 6952 | * considers that CPU to be in an "extended quiescent state",
|
| 6953 | * which means that RCU will be completely ignoring that CPU.
|
| 6954 | * Therefore, rcu_read_lock() and friends have absolutely no
|
| 6955 | * effect on a CPU running in that state. In other words, even if
|
| 6956 | * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
|
| 6957 | * delete data structures out from under it. RCU really has no
|
| 6958 | * choice here: we need to keep an RCU-free window in idle where
|
| 6959 | * the CPU may possibly enter into low power mode. This way we can
|
| 6960 | * notice an extended quiescent state to other CPUs that started a grace
|
| 6961 | * period. Otherwise we would delay any grace period as long as we run
|
| 6962 | * in the idle task.
|
| 6963 | *
|
| 6964 | * So complain bitterly if someone does call rcu_read_lock(),
|
| 6965 | * rcu_read_lock_bh() and so on from extended quiescent states.
|
| 6966 |
|
| 6967 | if (!rcu_is_watching())
|
| 6968 | pr_warn("RCU used illegally from extended quiescent state!\n");
|
| 6969 |
|
| 6970 | lockdep_print_held_locks(curr);
|
| 6971 | pr_warn("\nstack backtrace:\n");
|
| 6972 | dump_stack();
|
| 6973 | nbcon_cpu_emergency_exit();
|
| 6974 | warn_rcu_exit(rcu);
|
| 6975 | }
|
| 6976 | EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);
|