PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/lockdep.c

https://github.com/mstsirkin/kvm
C | 1997 lines | 1285 code | 307 blank | 405 comment | 178 complexity | 72ba95d84312124f7f063190a3a6e0e6 MD5 | raw file
  1. /*
  2. * kernel/lockdep.c
  3. *
  4. * Runtime locking correctness validator
  5. *
  6. * Started by Ingo Molnar:
  7. *
  8. * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  9. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  10. *
  11. * this code maps all the lock dependencies as they occur in a live kernel
  12. * and will warn about the following classes of locking bugs:
  13. *
  14. * - lock inversion scenarios
  15. * - circular lock dependencies
  16. * - hardirq/softirq safe/unsafe locking bugs
  17. *
  18. * Bugs are reported even if the current locking scenario does not cause
  19. * any deadlock at this point.
  20. *
  21. * I.e. if anytime in the past two locks were taken in a different order,
  22. * even if it happened for another task, even if those were different
  23. * locks (but of the same class as this lock), this code will detect it.
  24. *
  25. * Thanks to Arjan van de Ven for coming up with the initial idea of
  26. * mapping lock dependencies runtime.
  27. */
  28. #define DISABLE_BRANCH_PROFILING
  29. #include <linux/mutex.h>
  30. #include <linux/sched.h>
  31. #include <linux/delay.h>
  32. #include <linux/module.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/kallsyms.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/stacktrace.h>
  39. #include <linux/debug_locks.h>
  40. #include <linux/irqflags.h>
  41. #include <linux/utsname.h>
  42. #include <linux/hash.h>
  43. #include <linux/ftrace.h>
  44. #include <linux/stringify.h>
  45. #include <linux/bitops.h>
  46. #include <linux/gfp.h>
  47. #include <asm/sections.h>
  48. #include "lockdep_internals.h"
  49. #define CREATE_TRACE_POINTS
  50. #include <trace/events/lock.h>
  51. #ifdef CONFIG_PROVE_LOCKING
  52. int prove_locking = 1;
  53. module_param(prove_locking, int, 0644);
  54. #else
  55. #define prove_locking 0
  56. #endif
  57. #ifdef CONFIG_LOCK_STAT
  58. int lock_stat = 1;
  59. module_param(lock_stat, int, 0644);
  60. #else
  61. #define lock_stat 0
  62. #endif
  63. /*
  64. * lockdep_lock: protects the lockdep graph, the hashes and the
  65. * class/list/hash allocators.
  66. *
  67. * This is one of the rare exceptions where it's justified
  68. * to use a raw spinlock - we really dont want the spinlock
  69. * code to recurse back into the lockdep code...
  70. */
  71. static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  72. static int graph_lock(void)
  73. {
  74. arch_spin_lock(&lockdep_lock);
  75. /*
  76. * Make sure that if another CPU detected a bug while
  77. * walking the graph we dont change it (while the other
  78. * CPU is busy printing out stuff with the graph lock
  79. * dropped already)
  80. */
  81. if (!debug_locks) {
  82. arch_spin_unlock(&lockdep_lock);
  83. return 0;
  84. }
  85. /* prevent any recursions within lockdep from causing deadlocks */
  86. current->lockdep_recursion++;
  87. return 1;
  88. }
  89. static inline int graph_unlock(void)
  90. {
  91. if (debug_locks && !arch_spin_is_locked(&lockdep_lock))
  92. return DEBUG_LOCKS_WARN_ON(1);
  93. current->lockdep_recursion--;
  94. arch_spin_unlock(&lockdep_lock);
  95. return 0;
  96. }
  97. /*
  98. * Turn lock debugging off and return with 0 if it was off already,
  99. * and also release the graph lock:
  100. */
  101. static inline int debug_locks_off_graph_unlock(void)
  102. {
  103. int ret = debug_locks_off();
  104. arch_spin_unlock(&lockdep_lock);
  105. return ret;
  106. }
  107. static int lockdep_initialized;
  108. unsigned long nr_list_entries;
  109. static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
  110. /*
  111. * All data structures here are protected by the global debug_lock.
  112. *
  113. * Mutex key structs only get allocated, once during bootup, and never
  114. * get freed - this significantly simplifies the debugging code.
  115. */
  116. unsigned long nr_lock_classes;
  117. static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
  118. static inline struct lock_class *hlock_class(struct held_lock *hlock)
  119. {
  120. if (!hlock->class_idx) {
  121. DEBUG_LOCKS_WARN_ON(1);
  122. return NULL;
  123. }
  124. return lock_classes + hlock->class_idx - 1;
  125. }
  126. #ifdef CONFIG_LOCK_STAT
  127. static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS],
  128. cpu_lock_stats);
  129. static inline u64 lockstat_clock(void)
  130. {
  131. return local_clock();
  132. }
  133. static int lock_point(unsigned long points[], unsigned long ip)
  134. {
  135. int i;
  136. for (i = 0; i < LOCKSTAT_POINTS; i++) {
  137. if (points[i] == 0) {
  138. points[i] = ip;
  139. break;
  140. }
  141. if (points[i] == ip)
  142. break;
  143. }
  144. return i;
  145. }
  146. static void lock_time_inc(struct lock_time *lt, u64 time)
  147. {
  148. if (time > lt->max)
  149. lt->max = time;
  150. if (time < lt->min || !lt->nr)
  151. lt->min = time;
  152. lt->total += time;
  153. lt->nr++;
  154. }
  155. static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
  156. {
  157. if (!src->nr)
  158. return;
  159. if (src->max > dst->max)
  160. dst->max = src->max;
  161. if (src->min < dst->min || !dst->nr)
  162. dst->min = src->min;
  163. dst->total += src->total;
  164. dst->nr += src->nr;
  165. }
  166. struct lock_class_stats lock_stats(struct lock_class *class)
  167. {
  168. struct lock_class_stats stats;
  169. int cpu, i;
  170. memset(&stats, 0, sizeof(struct lock_class_stats));
  171. for_each_possible_cpu(cpu) {
  172. struct lock_class_stats *pcs =
  173. &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
  174. for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
  175. stats.contention_point[i] += pcs->contention_point[i];
  176. for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
  177. stats.contending_point[i] += pcs->contending_point[i];
  178. lock_time_add(&pcs->read_waittime, &stats.read_waittime);
  179. lock_time_add(&pcs->write_waittime, &stats.write_waittime);
  180. lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
  181. lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
  182. for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
  183. stats.bounces[i] += pcs->bounces[i];
  184. }
  185. return stats;
  186. }
  187. void clear_lock_stats(struct lock_class *class)
  188. {
  189. int cpu;
  190. for_each_possible_cpu(cpu) {
  191. struct lock_class_stats *cpu_stats =
  192. &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
  193. memset(cpu_stats, 0, sizeof(struct lock_class_stats));
  194. }
  195. memset(class->contention_point, 0, sizeof(class->contention_point));
  196. memset(class->contending_point, 0, sizeof(class->contending_point));
  197. }
  198. static struct lock_class_stats *get_lock_stats(struct lock_class *class)
  199. {
  200. return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
  201. }
  202. static void put_lock_stats(struct lock_class_stats *stats)
  203. {
  204. put_cpu_var(cpu_lock_stats);
  205. }
  206. static void lock_release_holdtime(struct held_lock *hlock)
  207. {
  208. struct lock_class_stats *stats;
  209. u64 holdtime;
  210. if (!lock_stat)
  211. return;
  212. holdtime = lockstat_clock() - hlock->holdtime_stamp;
  213. stats = get_lock_stats(hlock_class(hlock));
  214. if (hlock->read)
  215. lock_time_inc(&stats->read_holdtime, holdtime);
  216. else
  217. lock_time_inc(&stats->write_holdtime, holdtime);
  218. put_lock_stats(stats);
  219. }
  220. #else
  221. static inline void lock_release_holdtime(struct held_lock *hlock)
  222. {
  223. }
  224. #endif
  225. /*
  226. * We keep a global list of all lock classes. The list only grows,
  227. * never shrinks. The list is only accessed with the lockdep
  228. * spinlock lock held.
  229. */
  230. LIST_HEAD(all_lock_classes);
  231. /*
  232. * The lockdep classes are in a hash-table as well, for fast lookup:
  233. */
  234. #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
  235. #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
  236. #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
  237. #define classhashentry(key) (classhash_table + __classhashfn((key)))
  238. static struct list_head classhash_table[CLASSHASH_SIZE];
  239. /*
  240. * We put the lock dependency chains into a hash-table as well, to cache
  241. * their existence:
  242. */
  243. #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
  244. #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
  245. #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
  246. #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
  247. static struct list_head chainhash_table[CHAINHASH_SIZE];
  248. /*
  249. * The hash key of the lock dependency chains is a hash itself too:
  250. * it's a hash of all locks taken up to that lock, including that lock.
  251. * It's a 64-bit hash, because it's important for the keys to be
  252. * unique.
  253. */
  254. #define iterate_chain_key(key1, key2) \
  255. (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
  256. ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
  257. (key2))
  258. void lockdep_off(void)
  259. {
  260. current->lockdep_recursion++;
  261. }
  262. EXPORT_SYMBOL(lockdep_off);
  263. void lockdep_on(void)
  264. {
  265. current->lockdep_recursion--;
  266. }
  267. EXPORT_SYMBOL(lockdep_on);
  268. /*
  269. * Debugging switches:
  270. */
  271. #define VERBOSE 0
  272. #define VERY_VERBOSE 0
  273. #if VERBOSE
  274. # define HARDIRQ_VERBOSE 1
  275. # define SOFTIRQ_VERBOSE 1
  276. # define RECLAIM_VERBOSE 1
  277. #else
  278. # define HARDIRQ_VERBOSE 0
  279. # define SOFTIRQ_VERBOSE 0
  280. # define RECLAIM_VERBOSE 0
  281. #endif
  282. #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
  283. /*
  284. * Quick filtering for interesting events:
  285. */
  286. static int class_filter(struct lock_class *class)
  287. {
  288. #if 0
  289. /* Example */
  290. if (class->name_version == 1 &&
  291. !strcmp(class->name, "lockname"))
  292. return 1;
  293. if (class->name_version == 1 &&
  294. !strcmp(class->name, "&struct->lockfield"))
  295. return 1;
  296. #endif
  297. /* Filter everything else. 1 would be to allow everything else */
  298. return 0;
  299. }
  300. #endif
  301. static int verbose(struct lock_class *class)
  302. {
  303. #if VERBOSE
  304. return class_filter(class);
  305. #endif
  306. return 0;
  307. }
  308. /*
  309. * Stack-trace: tightly packed array of stack backtrace
  310. * addresses. Protected by the graph_lock.
  311. */
  312. unsigned long nr_stack_trace_entries;
  313. static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
  314. static int save_trace(struct stack_trace *trace)
  315. {
  316. trace->nr_entries = 0;
  317. trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
  318. trace->entries = stack_trace + nr_stack_trace_entries;
  319. trace->skip = 3;
  320. save_stack_trace(trace);
  321. /*
  322. * Some daft arches put -1 at the end to indicate its a full trace.
  323. *
  324. * <rant> this is buggy anyway, since it takes a whole extra entry so a
  325. * complete trace that maxes out the entries provided will be reported
  326. * as incomplete, friggin useless </rant>
  327. */
  328. if (trace->nr_entries != 0 &&
  329. trace->entries[trace->nr_entries-1] == ULONG_MAX)
  330. trace->nr_entries--;
  331. trace->max_entries = trace->nr_entries;
  332. nr_stack_trace_entries += trace->nr_entries;
  333. if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
  334. if (!debug_locks_off_graph_unlock())
  335. return 0;
  336. printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
  337. printk("turning off the locking correctness validator.\n");
  338. dump_stack();
  339. return 0;
  340. }
  341. return 1;
  342. }
  343. unsigned int nr_hardirq_chains;
  344. unsigned int nr_softirq_chains;
  345. unsigned int nr_process_chains;
  346. unsigned int max_lockdep_depth;
  347. #ifdef CONFIG_DEBUG_LOCKDEP
  348. /*
  349. * We cannot printk in early bootup code. Not even early_printk()
  350. * might work. So we mark any initialization errors and printk
  351. * about it later on, in lockdep_info().
  352. */
  353. static int lockdep_init_error;
  354. static unsigned long lockdep_init_trace_data[20];
  355. static struct stack_trace lockdep_init_trace = {
  356. .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
  357. .entries = lockdep_init_trace_data,
  358. };
  359. /*
  360. * Various lockdep statistics:
  361. */
  362. DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
  363. #endif
  364. /*
  365. * Locking printouts:
  366. */
  367. #define __USAGE(__STATE) \
  368. [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
  369. [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
  370. [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
  371. [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
  372. static const char *usage_str[] =
  373. {
  374. #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
  375. #include "lockdep_states.h"
  376. #undef LOCKDEP_STATE
  377. [LOCK_USED] = "INITIAL USE",
  378. };
  379. const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
  380. {
  381. return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
  382. }
  383. static inline unsigned long lock_flag(enum lock_usage_bit bit)
  384. {
  385. return 1UL << bit;
  386. }
  387. static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
  388. {
  389. char c = '.';
  390. if (class->usage_mask & lock_flag(bit + 2))
  391. c = '+';
  392. if (class->usage_mask & lock_flag(bit)) {
  393. c = '-';
  394. if (class->usage_mask & lock_flag(bit + 2))
  395. c = '?';
  396. }
  397. return c;
  398. }
  399. void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
  400. {
  401. int i = 0;
  402. #define LOCKDEP_STATE(__STATE) \
  403. usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
  404. usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
  405. #include "lockdep_states.h"
  406. #undef LOCKDEP_STATE
  407. usage[i] = '\0';
  408. }
  409. static int __print_lock_name(struct lock_class *class)
  410. {
  411. char str[KSYM_NAME_LEN];
  412. const char *name;
  413. name = class->name;
  414. if (!name)
  415. name = __get_key_name(class->key, str);
  416. return printk("%s", name);
  417. }
  418. static void print_lock_name(struct lock_class *class)
  419. {
  420. char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
  421. const char *name;
  422. get_usage_chars(class, usage);
  423. name = class->name;
  424. if (!name) {
  425. name = __get_key_name(class->key, str);
  426. printk(" (%s", name);
  427. } else {
  428. printk(" (%s", name);
  429. if (class->name_version > 1)
  430. printk("#%d", class->name_version);
  431. if (class->subclass)
  432. printk("/%d", class->subclass);
  433. }
  434. printk("){%s}", usage);
  435. }
  436. static void print_lockdep_cache(struct lockdep_map *lock)
  437. {
  438. const char *name;
  439. char str[KSYM_NAME_LEN];
  440. name = lock->name;
  441. if (!name)
  442. name = __get_key_name(lock->key->subkeys, str);
  443. printk("%s", name);
  444. }
  445. static void print_lock(struct held_lock *hlock)
  446. {
  447. print_lock_name(hlock_class(hlock));
  448. printk(", at: ");
  449. print_ip_sym(hlock->acquire_ip);
  450. }
  451. static void lockdep_print_held_locks(struct task_struct *curr)
  452. {
  453. int i, depth = curr->lockdep_depth;
  454. if (!depth) {
  455. printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
  456. return;
  457. }
  458. printk("%d lock%s held by %s/%d:\n",
  459. depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
  460. for (i = 0; i < depth; i++) {
  461. printk(" #%d: ", i);
  462. print_lock(curr->held_locks + i);
  463. }
  464. }
  465. static void print_kernel_version(void)
  466. {
  467. printk("%s %.*s\n", init_utsname()->release,
  468. (int)strcspn(init_utsname()->version, " "),
  469. init_utsname()->version);
  470. }
  471. static int very_verbose(struct lock_class *class)
  472. {
  473. #if VERY_VERBOSE
  474. return class_filter(class);
  475. #endif
  476. return 0;
  477. }
  478. /*
  479. * Is this the address of a static object:
  480. */
  481. static int static_obj(void *obj)
  482. {
  483. unsigned long start = (unsigned long) &_stext,
  484. end = (unsigned long) &_end,
  485. addr = (unsigned long) obj;
  486. /*
  487. * static variable?
  488. */
  489. if ((addr >= start) && (addr < end))
  490. return 1;
  491. if (arch_is_kernel_data(addr))
  492. return 1;
  493. /*
  494. * in-kernel percpu var?
  495. */
  496. if (is_kernel_percpu_address(addr))
  497. return 1;
  498. /*
  499. * module static or percpu var?
  500. */
  501. return is_module_address(addr) || is_module_percpu_address(addr);
  502. }
  503. /*
  504. * To make lock name printouts unique, we calculate a unique
  505. * class->name_version generation counter:
  506. */
  507. static int count_matching_names(struct lock_class *new_class)
  508. {
  509. struct lock_class *class;
  510. int count = 0;
  511. if (!new_class->name)
  512. return 0;
  513. list_for_each_entry(class, &all_lock_classes, lock_entry) {
  514. if (new_class->key - new_class->subclass == class->key)
  515. return class->name_version;
  516. if (class->name && !strcmp(class->name, new_class->name))
  517. count = max(count, class->name_version);
  518. }
  519. return count + 1;
  520. }
  521. /*
  522. * Register a lock's class in the hash-table, if the class is not present
  523. * yet. Otherwise we look it up. We cache the result in the lock object
  524. * itself, so actual lookup of the hash should be once per lock object.
  525. */
  526. static inline struct lock_class *
  527. look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
  528. {
  529. struct lockdep_subclass_key *key;
  530. struct list_head *hash_head;
  531. struct lock_class *class;
  532. #ifdef CONFIG_DEBUG_LOCKDEP
  533. /*
  534. * If the architecture calls into lockdep before initializing
  535. * the hashes then we'll warn about it later. (we cannot printk
  536. * right now)
  537. */
  538. if (unlikely(!lockdep_initialized)) {
  539. lockdep_init();
  540. lockdep_init_error = 1;
  541. save_stack_trace(&lockdep_init_trace);
  542. }
  543. #endif
  544. if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
  545. debug_locks_off();
  546. printk(KERN_ERR
  547. "BUG: looking up invalid subclass: %u\n", subclass);
  548. printk(KERN_ERR
  549. "turning off the locking correctness validator.\n");
  550. dump_stack();
  551. return NULL;
  552. }
  553. /*
  554. * Static locks do not have their class-keys yet - for them the key
  555. * is the lock object itself:
  556. */
  557. if (unlikely(!lock->key))
  558. lock->key = (void *)lock;
  559. /*
  560. * NOTE: the class-key must be unique. For dynamic locks, a static
  561. * lock_class_key variable is passed in through the mutex_init()
  562. * (or spin_lock_init()) call - which acts as the key. For static
  563. * locks we use the lock object itself as the key.
  564. */
  565. BUILD_BUG_ON(sizeof(struct lock_class_key) >
  566. sizeof(struct lockdep_map));
  567. key = lock->key->subkeys + subclass;
  568. hash_head = classhashentry(key);
  569. /*
  570. * We can walk the hash lockfree, because the hash only
  571. * grows, and we are careful when adding entries to the end:
  572. */
  573. list_for_each_entry(class, hash_head, hash_entry) {
  574. if (class->key == key) {
  575. WARN_ON_ONCE(class->name != lock->name);
  576. return class;
  577. }
  578. }
  579. return NULL;
  580. }
  581. /*
  582. * Register a lock's class in the hash-table, if the class is not present
  583. * yet. Otherwise we look it up. We cache the result in the lock object
  584. * itself, so actual lookup of the hash should be once per lock object.
  585. */
  586. static inline struct lock_class *
  587. register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
  588. {
  589. struct lockdep_subclass_key *key;
  590. struct list_head *hash_head;
  591. struct lock_class *class;
  592. unsigned long flags;
  593. class = look_up_lock_class(lock, subclass);
  594. if (likely(class))
  595. return class;
  596. /*
  597. * Debug-check: all keys must be persistent!
  598. */
  599. if (!static_obj(lock->key)) {
  600. debug_locks_off();
  601. printk("INFO: trying to register non-static key.\n");
  602. printk("the code is fine but needs lockdep annotation.\n");
  603. printk("turning off the locking correctness validator.\n");
  604. dump_stack();
  605. return NULL;
  606. }
  607. key = lock->key->subkeys + subclass;
  608. hash_head = classhashentry(key);
  609. raw_local_irq_save(flags);
  610. if (!graph_lock()) {
  611. raw_local_irq_restore(flags);
  612. return NULL;
  613. }
  614. /*
  615. * We have to do the hash-walk again, to avoid races
  616. * with another CPU:
  617. */
  618. list_for_each_entry(class, hash_head, hash_entry)
  619. if (class->key == key)
  620. goto out_unlock_set;
  621. /*
  622. * Allocate a new key from the static array, and add it to
  623. * the hash:
  624. */
  625. if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
  626. if (!debug_locks_off_graph_unlock()) {
  627. raw_local_irq_restore(flags);
  628. return NULL;
  629. }
  630. raw_local_irq_restore(flags);
  631. printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
  632. printk("turning off the locking correctness validator.\n");
  633. dump_stack();
  634. return NULL;
  635. }
  636. class = lock_classes + nr_lock_classes++;
  637. debug_atomic_inc(nr_unused_locks);
  638. class->key = key;
  639. class->name = lock->name;
  640. class->subclass = subclass;
  641. INIT_LIST_HEAD(&class->lock_entry);
  642. INIT_LIST_HEAD(&class->locks_before);
  643. INIT_LIST_HEAD(&class->locks_after);
  644. class->name_version = count_matching_names(class);
  645. /*
  646. * We use RCU's safe list-add method to make
  647. * parallel walking of the hash-list safe:
  648. */
  649. list_add_tail_rcu(&class->hash_entry, hash_head);
  650. /*
  651. * Add it to the global list of classes:
  652. */
  653. list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
  654. if (verbose(class)) {
  655. graph_unlock();
  656. raw_local_irq_restore(flags);
  657. printk("\nnew class %p: %s", class->key, class->name);
  658. if (class->name_version > 1)
  659. printk("#%d", class->name_version);
  660. printk("\n");
  661. dump_stack();
  662. raw_local_irq_save(flags);
  663. if (!graph_lock()) {
  664. raw_local_irq_restore(flags);
  665. return NULL;
  666. }
  667. }
  668. out_unlock_set:
  669. graph_unlock();
  670. raw_local_irq_restore(flags);
  671. if (!subclass || force)
  672. lock->class_cache[0] = class;
  673. else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
  674. lock->class_cache[subclass] = class;
  675. if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
  676. return NULL;
  677. return class;
  678. }
  679. #ifdef CONFIG_PROVE_LOCKING
  680. /*
  681. * Allocate a lockdep entry. (assumes the graph_lock held, returns
  682. * with NULL on failure)
  683. */
  684. static struct lock_list *alloc_list_entry(void)
  685. {
  686. if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
  687. if (!debug_locks_off_graph_unlock())
  688. return NULL;
  689. printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
  690. printk("turning off the locking correctness validator.\n");
  691. dump_stack();
  692. return NULL;
  693. }
  694. return list_entries + nr_list_entries++;
  695. }
  696. /*
  697. * Add a new dependency to the head of the list:
  698. */
  699. static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
  700. struct list_head *head, unsigned long ip,
  701. int distance, struct stack_trace *trace)
  702. {
  703. struct lock_list *entry;
  704. /*
  705. * Lock not present yet - get a new dependency struct and
  706. * add it to the list:
  707. */
  708. entry = alloc_list_entry();
  709. if (!entry)
  710. return 0;
  711. entry->class = this;
  712. entry->distance = distance;
  713. entry->trace = *trace;
  714. /*
  715. * Since we never remove from the dependency list, the list can
  716. * be walked lockless by other CPUs, it's only allocation
  717. * that must be protected by the spinlock. But this also means
  718. * we must make new entries visible only once writes to the
  719. * entry become visible - hence the RCU op:
  720. */
  721. list_add_tail_rcu(&entry->entry, head);
  722. return 1;
  723. }
  724. /*
  725. * For good efficiency of modular, we use power of 2
  726. */
  727. #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
  728. #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
  729. /*
  730. * The circular_queue and helpers is used to implement the
  731. * breadth-first search(BFS)algorithem, by which we can build
  732. * the shortest path from the next lock to be acquired to the
  733. * previous held lock if there is a circular between them.
  734. */
  735. struct circular_queue {
  736. unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
  737. unsigned int front, rear;
  738. };
  739. static struct circular_queue lock_cq;
  740. unsigned int max_bfs_queue_depth;
  741. static unsigned int lockdep_dependency_gen_id;
  742. static inline void __cq_init(struct circular_queue *cq)
  743. {
  744. cq->front = cq->rear = 0;
  745. lockdep_dependency_gen_id++;
  746. }
  747. static inline int __cq_empty(struct circular_queue *cq)
  748. {
  749. return (cq->front == cq->rear);
  750. }
  751. static inline int __cq_full(struct circular_queue *cq)
  752. {
  753. return ((cq->rear + 1) & CQ_MASK) == cq->front;
  754. }
  755. static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
  756. {
  757. if (__cq_full(cq))
  758. return -1;
  759. cq->element[cq->rear] = elem;
  760. cq->rear = (cq->rear + 1) & CQ_MASK;
  761. return 0;
  762. }
  763. static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
  764. {
  765. if (__cq_empty(cq))
  766. return -1;
  767. *elem = cq->element[cq->front];
  768. cq->front = (cq->front + 1) & CQ_MASK;
  769. return 0;
  770. }
  771. static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
  772. {
  773. return (cq->rear - cq->front) & CQ_MASK;
  774. }
  775. static inline void mark_lock_accessed(struct lock_list *lock,
  776. struct lock_list *parent)
  777. {
  778. unsigned long nr;
  779. nr = lock - list_entries;
  780. WARN_ON(nr >= nr_list_entries);
  781. lock->parent = parent;
  782. lock->class->dep_gen_id = lockdep_dependency_gen_id;
  783. }
  784. static inline unsigned long lock_accessed(struct lock_list *lock)
  785. {
  786. unsigned long nr;
  787. nr = lock - list_entries;
  788. WARN_ON(nr >= nr_list_entries);
  789. return lock->class->dep_gen_id == lockdep_dependency_gen_id;
  790. }
  791. static inline struct lock_list *get_lock_parent(struct lock_list *child)
  792. {
  793. return child->parent;
  794. }
  795. static inline int get_lock_depth(struct lock_list *child)
  796. {
  797. int depth = 0;
  798. struct lock_list *parent;
  799. while ((parent = get_lock_parent(child))) {
  800. child = parent;
  801. depth++;
  802. }
  803. return depth;
  804. }
  805. static int __bfs(struct lock_list *source_entry,
  806. void *data,
  807. int (*match)(struct lock_list *entry, void *data),
  808. struct lock_list **target_entry,
  809. int forward)
  810. {
  811. struct lock_list *entry;
  812. struct list_head *head;
  813. struct circular_queue *cq = &lock_cq;
  814. int ret = 1;
  815. if (match(source_entry, data)) {
  816. *target_entry = source_entry;
  817. ret = 0;
  818. goto exit;
  819. }
  820. if (forward)
  821. head = &source_entry->class->locks_after;
  822. else
  823. head = &source_entry->class->locks_before;
  824. if (list_empty(head))
  825. goto exit;
  826. __cq_init(cq);
  827. __cq_enqueue(cq, (unsigned long)source_entry);
  828. while (!__cq_empty(cq)) {
  829. struct lock_list *lock;
  830. __cq_dequeue(cq, (unsigned long *)&lock);
  831. if (!lock->class) {
  832. ret = -2;
  833. goto exit;
  834. }
  835. if (forward)
  836. head = &lock->class->locks_after;
  837. else
  838. head = &lock->class->locks_before;
  839. list_for_each_entry(entry, head, entry) {
  840. if (!lock_accessed(entry)) {
  841. unsigned int cq_depth;
  842. mark_lock_accessed(entry, lock);
  843. if (match(entry, data)) {
  844. *target_entry = entry;
  845. ret = 0;
  846. goto exit;
  847. }
  848. if (__cq_enqueue(cq, (unsigned long)entry)) {
  849. ret = -1;
  850. goto exit;
  851. }
  852. cq_depth = __cq_get_elem_count(cq);
  853. if (max_bfs_queue_depth < cq_depth)
  854. max_bfs_queue_depth = cq_depth;
  855. }
  856. }
  857. }
  858. exit:
  859. return ret;
  860. }
  861. static inline int __bfs_forwards(struct lock_list *src_entry,
  862. void *data,
  863. int (*match)(struct lock_list *entry, void *data),
  864. struct lock_list **target_entry)
  865. {
  866. return __bfs(src_entry, data, match, target_entry, 1);
  867. }
  868. static inline int __bfs_backwards(struct lock_list *src_entry,
  869. void *data,
  870. int (*match)(struct lock_list *entry, void *data),
  871. struct lock_list **target_entry)
  872. {
  873. return __bfs(src_entry, data, match, target_entry, 0);
  874. }
  875. /*
  876. * Recursive, forwards-direction lock-dependency checking, used for
  877. * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
  878. * checking.
  879. */
  880. /*
  881. * Print a dependency chain entry (this is only done when a deadlock
  882. * has been detected):
  883. */
  884. static noinline int
  885. print_circular_bug_entry(struct lock_list *target, int depth)
  886. {
  887. if (debug_locks_silent)
  888. return 0;
  889. printk("\n-> #%u", depth);
  890. print_lock_name(target->class);
  891. printk(":\n");
  892. print_stack_trace(&target->trace, 6);
  893. return 0;
  894. }
  895. static void
  896. print_circular_lock_scenario(struct held_lock *src,
  897. struct held_lock *tgt,
  898. struct lock_list *prt)
  899. {
  900. struct lock_class *source = hlock_class(src);
  901. struct lock_class *target = hlock_class(tgt);
  902. struct lock_class *parent = prt->class;
  903. /*
  904. * A direct locking problem where unsafe_class lock is taken
  905. * directly by safe_class lock, then all we need to show
  906. * is the deadlock scenario, as it is obvious that the
  907. * unsafe lock is taken under the safe lock.
  908. *
  909. * But if there is a chain instead, where the safe lock takes
  910. * an intermediate lock (middle_class) where this lock is
  911. * not the same as the safe lock, then the lock chain is
  912. * used to describe the problem. Otherwise we would need
  913. * to show a different CPU case for each link in the chain
  914. * from the safe_class lock to the unsafe_class lock.
  915. */
  916. if (parent != source) {
  917. printk("Chain exists of:\n ");
  918. __print_lock_name(source);
  919. printk(" --> ");
  920. __print_lock_name(parent);
  921. printk(" --> ");
  922. __print_lock_name(target);
  923. printk("\n\n");
  924. }
  925. printk(" Possible unsafe locking scenario:\n\n");
  926. printk(" CPU0 CPU1\n");
  927. printk(" ---- ----\n");
  928. printk(" lock(");
  929. __print_lock_name(target);
  930. printk(");\n");
  931. printk(" lock(");
  932. __print_lock_name(parent);
  933. printk(");\n");
  934. printk(" lock(");
  935. __print_lock_name(target);
  936. printk(");\n");
  937. printk(" lock(");
  938. __print_lock_name(source);
  939. printk(");\n");
  940. printk("\n *** DEADLOCK ***\n\n");
  941. }
  942. /*
  943. * When a circular dependency is detected, print the
  944. * header first:
  945. */
  946. static noinline int
  947. print_circular_bug_header(struct lock_list *entry, unsigned int depth,
  948. struct held_lock *check_src,
  949. struct held_lock *check_tgt)
  950. {
  951. struct task_struct *curr = current;
  952. if (debug_locks_silent)
  953. return 0;
  954. printk("\n=======================================================\n");
  955. printk( "[ INFO: possible circular locking dependency detected ]\n");
  956. print_kernel_version();
  957. printk( "-------------------------------------------------------\n");
  958. printk("%s/%d is trying to acquire lock:\n",
  959. curr->comm, task_pid_nr(curr));
  960. print_lock(check_src);
  961. printk("\nbut task is already holding lock:\n");
  962. print_lock(check_tgt);
  963. printk("\nwhich lock already depends on the new lock.\n\n");
  964. printk("\nthe existing dependency chain (in reverse order) is:\n");
  965. print_circular_bug_entry(entry, depth);
  966. return 0;
  967. }
  968. static inline int class_equal(struct lock_list *entry, void *data)
  969. {
  970. return entry->class == data;
  971. }
  972. static noinline int print_circular_bug(struct lock_list *this,
  973. struct lock_list *target,
  974. struct held_lock *check_src,
  975. struct held_lock *check_tgt)
  976. {
  977. struct task_struct *curr = current;
  978. struct lock_list *parent;
  979. struct lock_list *first_parent;
  980. int depth;
  981. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  982. return 0;
  983. if (!save_trace(&this->trace))
  984. return 0;
  985. depth = get_lock_depth(target);
  986. print_circular_bug_header(target, depth, check_src, check_tgt);
  987. parent = get_lock_parent(target);
  988. first_parent = parent;
  989. while (parent) {
  990. print_circular_bug_entry(parent, --depth);
  991. parent = get_lock_parent(parent);
  992. }
  993. printk("\nother info that might help us debug this:\n\n");
  994. print_circular_lock_scenario(check_src, check_tgt,
  995. first_parent);
  996. lockdep_print_held_locks(curr);
  997. printk("\nstack backtrace:\n");
  998. dump_stack();
  999. return 0;
  1000. }
  1001. static noinline int print_bfs_bug(int ret)
  1002. {
  1003. if (!debug_locks_off_graph_unlock())
  1004. return 0;
  1005. WARN(1, "lockdep bfs error:%d\n", ret);
  1006. return 0;
  1007. }
  1008. static int noop_count(struct lock_list *entry, void *data)
  1009. {
  1010. (*(unsigned long *)data)++;
  1011. return 0;
  1012. }
  1013. unsigned long __lockdep_count_forward_deps(struct lock_list *this)
  1014. {
  1015. unsigned long count = 0;
  1016. struct lock_list *uninitialized_var(target_entry);
  1017. __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
  1018. return count;
  1019. }
  1020. unsigned long lockdep_count_forward_deps(struct lock_class *class)
  1021. {
  1022. unsigned long ret, flags;
  1023. struct lock_list this;
  1024. this.parent = NULL;
  1025. this.class = class;
  1026. local_irq_save(flags);
  1027. arch_spin_lock(&lockdep_lock);
  1028. ret = __lockdep_count_forward_deps(&this);
  1029. arch_spin_unlock(&lockdep_lock);
  1030. local_irq_restore(flags);
  1031. return ret;
  1032. }
  1033. unsigned long __lockdep_count_backward_deps(struct lock_list *this)
  1034. {
  1035. unsigned long count = 0;
  1036. struct lock_list *uninitialized_var(target_entry);
  1037. __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
  1038. return count;
  1039. }
  1040. unsigned long lockdep_count_backward_deps(struct lock_class *class)
  1041. {
  1042. unsigned long ret, flags;
  1043. struct lock_list this;
  1044. this.parent = NULL;
  1045. this.class = class;
  1046. local_irq_save(flags);
  1047. arch_spin_lock(&lockdep_lock);
  1048. ret = __lockdep_count_backward_deps(&this);
  1049. arch_spin_unlock(&lockdep_lock);
  1050. local_irq_restore(flags);
  1051. return ret;
  1052. }
  1053. /*
  1054. * Prove that the dependency graph starting at <entry> can not
  1055. * lead to <target>. Print an error and return 0 if it does.
  1056. */
  1057. static noinline int
  1058. check_noncircular(struct lock_list *root, struct lock_class *target,
  1059. struct lock_list **target_entry)
  1060. {
  1061. int result;
  1062. debug_atomic_inc(nr_cyclic_checks);
  1063. result = __bfs_forwards(root, target, class_equal, target_entry);
  1064. return result;
  1065. }
  1066. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
  1067. /*
  1068. * Forwards and backwards subgraph searching, for the purposes of
  1069. * proving that two subgraphs can be connected by a new dependency
  1070. * without creating any illegal irq-safe -> irq-unsafe lock dependency.
  1071. */
  1072. static inline int usage_match(struct lock_list *entry, void *bit)
  1073. {
  1074. return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
  1075. }
  1076. /*
  1077. * Find a node in the forwards-direction dependency sub-graph starting
  1078. * at @root->class that matches @bit.
  1079. *
  1080. * Return 0 if such a node exists in the subgraph, and put that node
  1081. * into *@target_entry.
  1082. *
  1083. * Return 1 otherwise and keep *@target_entry unchanged.
  1084. * Return <0 on error.
  1085. */
  1086. static int
  1087. find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
  1088. struct lock_list **target_entry)
  1089. {
  1090. int result;
  1091. debug_atomic_inc(nr_find_usage_forwards_checks);
  1092. result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
  1093. return result;
  1094. }
  1095. /*
  1096. * Find a node in the backwards-direction dependency sub-graph starting
  1097. * at @root->class that matches @bit.
  1098. *
  1099. * Return 0 if such a node exists in the subgraph, and put that node
  1100. * into *@target_entry.
  1101. *
  1102. * Return 1 otherwise and keep *@target_entry unchanged.
  1103. * Return <0 on error.
  1104. */
  1105. static int
  1106. find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
  1107. struct lock_list **target_entry)
  1108. {
  1109. int result;
  1110. debug_atomic_inc(nr_find_usage_backwards_checks);
  1111. result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
  1112. return result;
  1113. }
  1114. static void print_lock_class_header(struct lock_class *class, int depth)
  1115. {
  1116. int bit;
  1117. printk("%*s->", depth, "");
  1118. print_lock_name(class);
  1119. printk(" ops: %lu", class->ops);
  1120. printk(" {\n");
  1121. for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
  1122. if (class->usage_mask & (1 << bit)) {
  1123. int len = depth;
  1124. len += printk("%*s %s", depth, "", usage_str[bit]);
  1125. len += printk(" at:\n");
  1126. print_stack_trace(class->usage_traces + bit, len);
  1127. }
  1128. }
  1129. printk("%*s }\n", depth, "");
  1130. printk("%*s ... key at: ",depth,"");
  1131. print_ip_sym((unsigned long)class->key);
  1132. }
  1133. /*
  1134. * printk the shortest lock dependencies from @start to @end in reverse order:
  1135. */
  1136. static void __used
  1137. print_shortest_lock_dependencies(struct lock_list *leaf,
  1138. struct lock_list *root)
  1139. {
  1140. struct lock_list *entry = leaf;
  1141. int depth;
  1142. /*compute depth from generated tree by BFS*/
  1143. depth = get_lock_depth(leaf);
  1144. do {
  1145. print_lock_class_header(entry->class, depth);
  1146. printk("%*s ... acquired at:\n", depth, "");
  1147. print_stack_trace(&entry->trace, 2);
  1148. printk("\n");
  1149. if (depth == 0 && (entry != root)) {
  1150. printk("lockdep:%s bad path found in chain graph\n", __func__);
  1151. break;
  1152. }
  1153. entry = get_lock_parent(entry);
  1154. depth--;
  1155. } while (entry && (depth >= 0));
  1156. return;
  1157. }
  1158. static void
  1159. print_irq_lock_scenario(struct lock_list *safe_entry,
  1160. struct lock_list *unsafe_entry,
  1161. struct lock_class *prev_class,
  1162. struct lock_class *next_class)
  1163. {
  1164. struct lock_class *safe_class = safe_entry->class;
  1165. struct lock_class *unsafe_class = unsafe_entry->class;
  1166. struct lock_class *middle_class = prev_class;
  1167. if (middle_class == safe_class)
  1168. middle_class = next_class;
  1169. /*
  1170. * A direct locking problem where unsafe_class lock is taken
  1171. * directly by safe_class lock, then all we need to show
  1172. * is the deadlock scenario, as it is obvious that the
  1173. * unsafe lock is taken under the safe lock.
  1174. *
  1175. * But if there is a chain instead, where the safe lock takes
  1176. * an intermediate lock (middle_class) where this lock is
  1177. * not the same as the safe lock, then the lock chain is
  1178. * used to describe the problem. Otherwise we would need
  1179. * to show a different CPU case for each link in the chain
  1180. * from the safe_class lock to the unsafe_class lock.
  1181. */
  1182. if (middle_class != unsafe_class) {
  1183. printk("Chain exists of:\n ");
  1184. __print_lock_name(safe_class);
  1185. printk(" --> ");
  1186. __print_lock_name(middle_class);
  1187. printk(" --> ");
  1188. __print_lock_name(unsafe_class);
  1189. printk("\n\n");
  1190. }
  1191. printk(" Possible interrupt unsafe locking scenario:\n\n");
  1192. printk(" CPU0 CPU1\n");
  1193. printk(" ---- ----\n");
  1194. printk(" lock(");
  1195. __print_lock_name(unsafe_class);
  1196. printk(");\n");
  1197. printk(" local_irq_disable();\n");
  1198. printk(" lock(");
  1199. __print_lock_name(safe_class);
  1200. printk(");\n");
  1201. printk(" lock(");
  1202. __print_lock_name(middle_class);
  1203. printk(");\n");
  1204. printk(" <Interrupt>\n");
  1205. printk(" lock(");
  1206. __print_lock_name(safe_class);
  1207. printk(");\n");
  1208. printk("\n *** DEADLOCK ***\n\n");
  1209. }
  1210. static int
  1211. print_bad_irq_dependency(struct task_struct *curr,
  1212. struct lock_list *prev_root,
  1213. struct lock_list *next_root,
  1214. struct lock_list *backwards_entry,
  1215. struct lock_list *forwards_entry,
  1216. struct held_lock *prev,
  1217. struct held_lock *next,
  1218. enum lock_usage_bit bit1,
  1219. enum lock_usage_bit bit2,
  1220. const char *irqclass)
  1221. {
  1222. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1223. return 0;
  1224. printk("\n======================================================\n");
  1225. printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
  1226. irqclass, irqclass);
  1227. print_kernel_version();
  1228. printk( "------------------------------------------------------\n");
  1229. printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
  1230. curr->comm, task_pid_nr(curr),
  1231. curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
  1232. curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
  1233. curr->hardirqs_enabled,
  1234. curr->softirqs_enabled);
  1235. print_lock(next);
  1236. printk("\nand this task is already holding:\n");
  1237. print_lock(prev);
  1238. printk("which would create a new lock dependency:\n");
  1239. print_lock_name(hlock_class(prev));
  1240. printk(" ->");
  1241. print_lock_name(hlock_class(next));
  1242. printk("\n");
  1243. printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
  1244. irqclass);
  1245. print_lock_name(backwards_entry->class);
  1246. printk("\n... which became %s-irq-safe at:\n", irqclass);
  1247. print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
  1248. printk("\nto a %s-irq-unsafe lock:\n", irqclass);
  1249. print_lock_name(forwards_entry->class);
  1250. printk("\n... which became %s-irq-unsafe at:\n", irqclass);
  1251. printk("...");
  1252. print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
  1253. printk("\nother info that might help us debug this:\n\n");
  1254. print_irq_lock_scenario(backwards_entry, forwards_entry,
  1255. hlock_class(prev), hlock_class(next));
  1256. lockdep_print_held_locks(curr);
  1257. printk("\nthe dependencies between %s-irq-safe lock", irqclass);
  1258. printk(" and the holding lock:\n");
  1259. if (!save_trace(&prev_root->trace))
  1260. return 0;
  1261. print_shortest_lock_dependencies(backwards_entry, prev_root);
  1262. printk("\nthe dependencies between the lock to be acquired");
  1263. printk(" and %s-irq-unsafe lock:\n", irqclass);
  1264. if (!save_trace(&next_root->trace))
  1265. return 0;
  1266. print_shortest_lock_dependencies(forwards_entry, next_root);
  1267. printk("\nstack backtrace:\n");
  1268. dump_stack();
  1269. return 0;
  1270. }
  1271. static int
  1272. check_usage(struct task_struct *curr, struct held_lock *prev,
  1273. struct held_lock *next, enum lock_usage_bit bit_backwards,
  1274. enum lock_usage_bit bit_forwards, const char *irqclass)
  1275. {
  1276. int ret;
  1277. struct lock_list this, that;
  1278. struct lock_list *uninitialized_var(target_entry);
  1279. struct lock_list *uninitialized_var(target_entry1);
  1280. this.parent = NULL;
  1281. this.class = hlock_class(prev);
  1282. ret = find_usage_backwards(&this, bit_backwards, &target_entry);
  1283. if (ret < 0)
  1284. return print_bfs_bug(ret);
  1285. if (ret == 1)
  1286. return ret;
  1287. that.parent = NULL;
  1288. that.class = hlock_class(next);
  1289. ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
  1290. if (ret < 0)
  1291. return print_bfs_bug(ret);
  1292. if (ret == 1)
  1293. return ret;
  1294. return print_bad_irq_dependency(curr, &this, &that,
  1295. target_entry, target_entry1,
  1296. prev, next,
  1297. bit_backwards, bit_forwards, irqclass);
  1298. }
  1299. static const char *state_names[] = {
  1300. #define LOCKDEP_STATE(__STATE) \
  1301. __stringify(__STATE),
  1302. #include "lockdep_states.h"
  1303. #undef LOCKDEP_STATE
  1304. };
  1305. static const char *state_rnames[] = {
  1306. #define LOCKDEP_STATE(__STATE) \
  1307. __stringify(__STATE)"-READ",
  1308. #include "lockdep_states.h"
  1309. #undef LOCKDEP_STATE
  1310. };
  1311. static inline const char *state_name(enum lock_usage_bit bit)
  1312. {
  1313. return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
  1314. }
  1315. static int exclusive_bit(int new_bit)
  1316. {
  1317. /*
  1318. * USED_IN
  1319. * USED_IN_READ
  1320. * ENABLED
  1321. * ENABLED_READ
  1322. *
  1323. * bit 0 - write/read
  1324. * bit 1 - used_in/enabled
  1325. * bit 2+ state
  1326. */
  1327. int state = new_bit & ~3;
  1328. int dir = new_bit & 2;
  1329. /*
  1330. * keep state, bit flip the direction and strip read.
  1331. */
  1332. return state | (dir ^ 2);
  1333. }
  1334. static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
  1335. struct held_lock *next, enum lock_usage_bit bit)
  1336. {
  1337. /*
  1338. * Prove that the new dependency does not connect a hardirq-safe
  1339. * lock with a hardirq-unsafe lock - to achieve this we search
  1340. * the backwards-subgraph starting at <prev>, and the
  1341. * forwards-subgraph starting at <next>:
  1342. */
  1343. if (!check_usage(curr, prev, next, bit,
  1344. exclusive_bit(bit), state_name(bit)))
  1345. return 0;
  1346. bit++; /* _READ */
  1347. /*
  1348. * Prove that the new dependency does not connect a hardirq-safe-read
  1349. * lock with a hardirq-unsafe lock - to achieve this we search
  1350. * the backwards-subgraph starting at <prev>, and the
  1351. * forwards-subgraph starting at <next>:
  1352. */
  1353. if (!check_usage(curr, prev, next, bit,
  1354. exclusive_bit(bit), state_name(bit)))
  1355. return 0;
  1356. return 1;
  1357. }
  1358. static int
  1359. check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
  1360. struct held_lock *next)
  1361. {
  1362. #define LOCKDEP_STATE(__STATE) \
  1363. if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
  1364. return 0;
  1365. #include "lockdep_states.h"
  1366. #undef LOCKDEP_STATE
  1367. return 1;
  1368. }
  1369. static void inc_chains(void)
  1370. {
  1371. if (current->hardirq_context)
  1372. nr_hardirq_chains++;
  1373. else {
  1374. if (current->softirq_context)
  1375. nr_softirq_chains++;
  1376. else
  1377. nr_process_chains++;
  1378. }
  1379. }
  1380. #else
  1381. static inline int
  1382. check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
  1383. struct held_lock *next)
  1384. {
  1385. return 1;
  1386. }
  1387. static inline void inc_chains(void)
  1388. {
  1389. nr_process_chains++;
  1390. }
  1391. #endif
  1392. static void
  1393. print_deadlock_scenario(struct held_lock *nxt,
  1394. struct held_lock *prv)
  1395. {
  1396. struct lock_class *next = hlock_class(nxt);
  1397. struct lock_class *prev = hlock_class(prv);
  1398. printk(" Possible unsafe locking scenario:\n\n");
  1399. printk(" CPU0\n");
  1400. printk(" ----\n");
  1401. printk(" lock(");
  1402. __print_lock_name(prev);
  1403. printk(");\n");
  1404. printk(" lock(");
  1405. __print_lock_name(next);
  1406. printk(");\n");
  1407. printk("\n *** DEADLOCK ***\n\n");
  1408. printk(" May be due to missing lock nesting notation\n\n");
  1409. }
  1410. static int
  1411. print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
  1412. struct held_lock *next)
  1413. {
  1414. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1415. return 0;
  1416. printk("\n=============================================\n");
  1417. printk( "[ INFO: possible recursive locking detected ]\n");
  1418. print_kernel_version();
  1419. printk( "---------------------------------------------\n");
  1420. printk("%s/%d is trying to acquire lock:\n",
  1421. curr->comm, task_pid_nr(curr));
  1422. print_lock(next);
  1423. printk("\nbut task is already holding lock:\n");
  1424. print_lock(prev);
  1425. printk("\nother info that might help us debug this:\n");
  1426. print_deadlock_scenario(next, prev);
  1427. lockdep_print_held_locks(curr);
  1428. printk("\nstack backtrace:\n");
  1429. dump_stack();
  1430. return 0;
  1431. }
  1432. /*
  1433. * Check whether we are holding such a class already.
  1434. *
  1435. * (Note that this has to be done separately, because the graph cannot
  1436. * detect such classes of deadlocks.)
  1437. *
  1438. * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
  1439. */
  1440. static int
  1441. check_deadlock(struct task_struct *curr, struct held_lock *next,
  1442. struct lockdep_map *next_instance, int read)
  1443. {
  1444. struct held_lock *prev;
  1445. struct held_lock *nest = NULL;
  1446. int i;
  1447. for (i = 0; i < curr->lockdep_depth; i++) {
  1448. prev = curr->held_locks + i;
  1449. if (prev->instance == next->nest_lock)
  1450. nest = prev;
  1451. if (hlock_class(prev) != hlock_class(next))
  1452. continue;
  1453. /*
  1454. * Allow read-after-read recursion of the same
  1455. * lock class (i.e. read_lock(lock)+read_lock(lock)):
  1456. */
  1457. if ((read == 2) && prev->read)
  1458. return 2;
  1459. /*
  1460. * We're holding the nest_lock, which serializes this lock's
  1461. * nesting behaviour.
  1462. */
  1463. if (nest)
  1464. return 2;
  1465. return print_deadlock_bug(curr, prev, next);
  1466. }
  1467. return 1;
  1468. }
  1469. /*
  1470. * There was a chain-cache miss, and we are about to add a new dependency
  1471. * to a previous lock. We recursively validate the following rules:
  1472. *
  1473. * - would the adding of the <prev> -> <next> dependency create a
  1474. * circular dependency in the graph? [== circular deadlock]
  1475. *
  1476. * - does the new prev->next dependency connect any hardirq-safe lock
  1477. * (in the full backwards-subgraph starting at <prev>) with any
  1478. * hardirq-unsafe lock (in the full forwards-subgraph starting at
  1479. * <next>)? [== illegal lock inversion with hardirq contexts]
  1480. *
  1481. * - does the new prev->next dependency connect any softirq-safe lock
  1482. * (in the full backwards-subgraph starting at <prev>) with any
  1483. * softirq-unsafe lock (in the full forwards-subgraph starting at
  1484. * <next>)? [== illegal lock inversion with softirq contexts]
  1485. *
  1486. * any of these scenarios could lead to a deadlock.
  1487. *
  1488. * Then if all the validations pass, we add the forwards and backwards
  1489. * dependency.
  1490. */
  1491. static int
  1492. check_prev_add(struct task_struct *curr, struct held_lock *prev,
  1493. struct held_lock *next, int distance, int trylock_loop)
  1494. {
  1495. struct lock_list *entry;
  1496. int ret;
  1497. struct lock_list this;
  1498. struct lock_list *uninitialized_var(target_entry);
  1499. /*
  1500. * Static variable, serialized by the graph_lock().
  1501. *
  1502. * We use this static variable to save the stack trace in case
  1503. * we call into this function multiple times due to encountering
  1504. * trylocks in the held lock stack.
  1505. */
  1506. static struct stack_trace trace;
  1507. /*
  1508. * Prove that the new <prev> -> <next> dependency would not
  1509. * create a circular dependency in the graph. (We do this by
  1510. * forward-recursing into the graph starting at <next>, and
  1511. * checking whether we can reach <prev>.)
  1512. *
  1513. * We are using global variables to control the recursion, to
  1514. * keep the stackframe size of the recursive functions low:
  1515. */
  1516. this.class = hlock_class(next);
  1517. this.parent = NULL;
  1518. ret = check_noncircular(&this, hlock_class(prev), &target_entry);
  1519. if (unlikely(!ret))
  1520. return print_circular_bug(&this, target_entry, next, prev);
  1521. else if (unlikely(ret < 0))
  1522. return print_bfs_bug(ret);
  1523. if (!check_prev_add_irq(curr, prev, next))
  1524. return 0;
  1525. /*
  1526. * For recursive read-locks we do all the dependency checks,
  1527. * but we dont store read-triggered dependencies (only
  1528. * write-triggered dependencies). This ensures that only the
  1529. * write-side dependencies matter, and that if for example a
  1530. * write-lock never takes any other locks, then the reads are
  1531. * equivalent to a NOP.
  1532. */
  1533. if (next->read == 2 || prev->read == 2)
  1534. return 1;
  1535. /*
  1536. * Is the <prev> -> <next> dependency already present?
  1537. *
  1538. * (this may occur even though this is a new chain: consider
  1539. * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
  1540. * chains - the second one will be new, but L1 already has
  1541. * L2 added to its dependency list, due to the first chain.)
  1542. */
  1543. list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
  1544. if (entry->class == hlock_class(next)) {
  1545. if (distance == 1)
  1546. entry->distance = 1;
  1547. return 2;
  1548. }
  1549. }
  1550. if (!trylock_loop && !save_trace(&trace))
  1551. return 0;
  1552. /*
  1553. * Ok, all validations passed, add the new lock
  1554. * to the previous lock's dependency list:
  1555. */
  1556. ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
  1557. &hlock_class(prev)->locks_after,
  1558. next->acquire_ip, distance, &trace);
  1559. if (!ret)
  1560. return 0;
  1561. ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
  1562. &hlock_class(next)->locks_before,
  1563. next->acquire_ip, distance, &trace);
  1564. if (!ret)
  1565. return 0;
  1566. /*
  1567. * Debugging printouts:
  1568. */
  1569. if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
  1570. graph_unlock();
  1571. printk("\n new dependency: ");
  1572. print_lock_name(hlock_class(prev));
  1573. printk(" => ");
  1574. print_lock_name(hlock_class(next));
  1575. printk("\n");
  1576. dump_stack();
  1577. return graph_lock();
  1578. }
  1579. return 1;
  1580. }
  1581. /*
  1582. * Add the dependency to all directly-previous locks that are 'relevant'.
  1583. * The ones that are relevant are (in increasing distance from curr):
  1584. * all consecutive trylock entries and the final non-trylock entry - or
  1585. * the end of this context's lock-chain - whichever comes first.
  1586. */
  1587. static int
  1588. check_prevs_add(struct task_struct *curr, struct held_lock *next)
  1589. {
  1590. int depth = curr->lockdep_depth;
  1591. int trylock_loop = 0;
  1592. struct held_lock *hlock;
  1593. /*
  1594. * Debugging checks.
  1595. *
  1596. * Depth must not be zero for a non-head lock:
  1597. */
  1598. if (!depth)
  1599. goto out_bug;
  1600. /*
  1601. * At least two relevant locks must exist for this
  1602. * to be a head:
  1603. */
  1604. if (curr->held_locks[depth].irq_context !=
  1605. curr->held_locks[depth-1].irq_context)
  1606. goto out_bug;
  1607. for (;;) {
  1608. int distance = curr->lockdep_depth - depth + 1;
  1609. hlock = curr->held_locks + depth-1;
  1610. /*
  1611. * Only non-recursive-read entries get new dependencies
  1612. * added:
  1613. */
  1614. if (hlock->read != 2) {
  1615. if (!check_prev_add(curr, hlock, next,
  1616. distance, trylock_loop))
  1617. return 0;
  1618. /*
  1619. * Stop after the first non-trylock entry,
  1620. * as non-trylock entries have added their
  1621. * own direct dependencies already, so this
  1622. * lock is connected to them indirectly:
  1623. */
  1624. if (!hlock->trylock)
  1625. break;
  1626. }
  1627. depth--;
  1628. /*
  1629. * End of lock-stack?
  1630. */
  1631. if (!depth)
  1632. break;
  1633. /*
  1634. * Stop the search if we cross into another context:
  1635. */
  1636. if (curr->held_locks[depth].irq_context !=
  1637. curr->held_locks[depth-1].irq_context)
  1638. break;
  1639. trylock_loop = 1;
  1640. }
  1641. return 1;
  1642. out_bug:
  1643. if (!debug_locks_off_graph_unlock())
  1644. return 0;
  1645. WARN_ON(1);
  1646. return 0;
  1647. }
  1648. unsigned long nr_lock_chains;
  1649. struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
  1650. int nr_chain_hlocks;
  1651. static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
  1652. struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
  1653. {
  1654. return lock_classes + chain_hlocks[chain->base + i];
  1655. }
  1656. /*
  1657. * Look up a dependency chain. If the key is not present yet then
  1658. * add it and return 1 - in this case the new dependency chain is
  1659. * validated. If the key is already hashed, return 0.
  1660. * (On return with 1 graph_lock is held.)
  1661. */
  1662. static inline int lookup_chain_cache(struct task_struct *curr,
  1663. struct held_lock *hlock,
  1664. u64 chain_key)
  1665. {
  1666. struct lock_class *class = hlock_class(hlock);
  1667. struct list_head *hash_head = chainhashentry(chain_key);
  1668. struct lock_chain *chain;
  1669. struct held_lock *hlock_curr, *hlock_next;
  1670. int i, j;
  1671. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  1672. return 0;
  1673. /*
  1674. * We can walk it lock-free, because entries only get added
  1675. * to the hash:
  1676. */
  1677. list_for_each_entry(chain, hash_head, entry) {
  1678. if (chain->chain_key == chain_key) {
  1679. cache_hit:
  1680. debug_atomic_inc(chain_lookup_hits);
  1681. if (very_verbose(class))
  1682. printk("\nhash chain already cached, key: "
  1683. "%016Lx tail class: [%p] %s\n",
  1684. (unsigned long long)chain_key,
  1685. class->key, class->name);
  1686. return 0;
  1687. }
  1688. }
  1689. if (very_verbose(class))
  1690. printk("\nnew hash chain, k