PageRenderTime 62ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel/lockdep.c

https://gitlab.com/LiquidSmooth-Devices/android_kernel_htc_msm8974
C | 3390 lines | 2746 code | 617 blank | 27 comment | 495 complexity | 657f67662f507af6e9a8184ea8aab5eb MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full 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 <linux/kmemcheck.h>
  48. #include <asm/sections.h>
  49. #include "lockdep_internals.h"
  50. #define CREATE_TRACE_POINTS
  51. #include <trace/events/lock.h>
  52. #ifdef CONFIG_PROVE_LOCKING
  53. int prove_locking = 1;
  54. module_param(prove_locking, int, 0644);
  55. #else
  56. #define prove_locking 0
  57. #endif
  58. #ifdef CONFIG_LOCK_STAT
  59. int lock_stat = 1;
  60. module_param(lock_stat, int, 0644);
  61. #else
  62. #define lock_stat 0
  63. #endif
  64. static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  65. static int graph_lock(void)
  66. {
  67. arch_spin_lock(&lockdep_lock);
  68. if (!debug_locks) {
  69. arch_spin_unlock(&lockdep_lock);
  70. return 0;
  71. }
  72. current->lockdep_recursion++;
  73. return 1;
  74. }
  75. static inline int graph_unlock(void)
  76. {
  77. if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
  78. return DEBUG_LOCKS_WARN_ON(1);
  79. }
  80. current->lockdep_recursion--;
  81. arch_spin_unlock(&lockdep_lock);
  82. return 0;
  83. }
  84. static inline int debug_locks_off_graph_unlock(void)
  85. {
  86. int ret = debug_locks_off();
  87. arch_spin_unlock(&lockdep_lock);
  88. return ret;
  89. }
  90. static int lockdep_initialized;
  91. unsigned long nr_list_entries;
  92. static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
  93. unsigned long nr_lock_classes;
  94. static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
  95. static inline struct lock_class *hlock_class(struct held_lock *hlock)
  96. {
  97. if (!hlock->class_idx) {
  98. DEBUG_LOCKS_WARN_ON(1);
  99. return NULL;
  100. }
  101. return lock_classes + hlock->class_idx - 1;
  102. }
  103. #ifdef CONFIG_LOCK_STAT
  104. static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS],
  105. cpu_lock_stats);
  106. static inline u64 lockstat_clock(void)
  107. {
  108. return local_clock();
  109. }
  110. static int lock_point(unsigned long points[], unsigned long ip)
  111. {
  112. int i;
  113. for (i = 0; i < LOCKSTAT_POINTS; i++) {
  114. if (points[i] == 0) {
  115. points[i] = ip;
  116. break;
  117. }
  118. if (points[i] == ip)
  119. break;
  120. }
  121. return i;
  122. }
  123. static void lock_time_inc(struct lock_time *lt, u64 time)
  124. {
  125. if (time > lt->max)
  126. lt->max = time;
  127. if (time < lt->min || !lt->nr)
  128. lt->min = time;
  129. lt->total += time;
  130. lt->nr++;
  131. }
  132. static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
  133. {
  134. if (!src->nr)
  135. return;
  136. if (src->max > dst->max)
  137. dst->max = src->max;
  138. if (src->min < dst->min || !dst->nr)
  139. dst->min = src->min;
  140. dst->total += src->total;
  141. dst->nr += src->nr;
  142. }
  143. struct lock_class_stats lock_stats(struct lock_class *class)
  144. {
  145. struct lock_class_stats stats;
  146. int cpu, i;
  147. memset(&stats, 0, sizeof(struct lock_class_stats));
  148. for_each_possible_cpu(cpu) {
  149. struct lock_class_stats *pcs =
  150. &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
  151. for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
  152. stats.contention_point[i] += pcs->contention_point[i];
  153. for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
  154. stats.contending_point[i] += pcs->contending_point[i];
  155. lock_time_add(&pcs->read_waittime, &stats.read_waittime);
  156. lock_time_add(&pcs->write_waittime, &stats.write_waittime);
  157. lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
  158. lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
  159. for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
  160. stats.bounces[i] += pcs->bounces[i];
  161. }
  162. return stats;
  163. }
  164. void clear_lock_stats(struct lock_class *class)
  165. {
  166. int cpu;
  167. for_each_possible_cpu(cpu) {
  168. struct lock_class_stats *cpu_stats =
  169. &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
  170. memset(cpu_stats, 0, sizeof(struct lock_class_stats));
  171. }
  172. memset(class->contention_point, 0, sizeof(class->contention_point));
  173. memset(class->contending_point, 0, sizeof(class->contending_point));
  174. }
  175. static struct lock_class_stats *get_lock_stats(struct lock_class *class)
  176. {
  177. return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
  178. }
  179. static void put_lock_stats(struct lock_class_stats *stats)
  180. {
  181. put_cpu_var(cpu_lock_stats);
  182. }
  183. static void lock_release_holdtime(struct held_lock *hlock)
  184. {
  185. struct lock_class_stats *stats;
  186. u64 holdtime;
  187. if (!lock_stat)
  188. return;
  189. holdtime = lockstat_clock() - hlock->holdtime_stamp;
  190. stats = get_lock_stats(hlock_class(hlock));
  191. if (hlock->read)
  192. lock_time_inc(&stats->read_holdtime, holdtime);
  193. else
  194. lock_time_inc(&stats->write_holdtime, holdtime);
  195. put_lock_stats(stats);
  196. }
  197. #else
  198. static inline void lock_release_holdtime(struct held_lock *hlock)
  199. {
  200. }
  201. #endif
  202. LIST_HEAD(all_lock_classes);
  203. #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
  204. #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
  205. #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
  206. #define classhashentry(key) (classhash_table + __classhashfn((key)))
  207. static struct list_head classhash_table[CLASSHASH_SIZE];
  208. #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
  209. #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
  210. #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
  211. #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
  212. static struct list_head chainhash_table[CHAINHASH_SIZE];
  213. #define iterate_chain_key(key1, key2) \
  214. (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
  215. ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
  216. (key2))
  217. void lockdep_off(void)
  218. {
  219. current->lockdep_recursion++;
  220. }
  221. EXPORT_SYMBOL(lockdep_off);
  222. void lockdep_on(void)
  223. {
  224. current->lockdep_recursion--;
  225. }
  226. EXPORT_SYMBOL(lockdep_on);
  227. #define VERBOSE 0
  228. #define VERY_VERBOSE 0
  229. #if VERBOSE
  230. # define HARDIRQ_VERBOSE 1
  231. # define SOFTIRQ_VERBOSE 1
  232. # define RECLAIM_VERBOSE 1
  233. #else
  234. # define HARDIRQ_VERBOSE 0
  235. # define SOFTIRQ_VERBOSE 0
  236. # define RECLAIM_VERBOSE 0
  237. #endif
  238. #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
  239. static int class_filter(struct lock_class *class)
  240. {
  241. #if 0
  242. if (class->name_version == 1 &&
  243. !strcmp(class->name, "lockname"))
  244. return 1;
  245. if (class->name_version == 1 &&
  246. !strcmp(class->name, "&struct->lockfield"))
  247. return 1;
  248. #endif
  249. return 0;
  250. }
  251. #endif
  252. static int verbose(struct lock_class *class)
  253. {
  254. #if VERBOSE
  255. return class_filter(class);
  256. #endif
  257. return 0;
  258. }
  259. unsigned long nr_stack_trace_entries;
  260. static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
  261. static int save_trace(struct stack_trace *trace)
  262. {
  263. trace->nr_entries = 0;
  264. trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
  265. trace->entries = stack_trace + nr_stack_trace_entries;
  266. trace->skip = 3;
  267. save_stack_trace(trace);
  268. if (trace->nr_entries != 0 &&
  269. trace->entries[trace->nr_entries-1] == ULONG_MAX)
  270. trace->nr_entries--;
  271. trace->max_entries = trace->nr_entries;
  272. nr_stack_trace_entries += trace->nr_entries;
  273. if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
  274. if (!debug_locks_off_graph_unlock())
  275. return 0;
  276. printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
  277. printk("turning off the locking correctness validator.\n");
  278. dump_stack();
  279. return 0;
  280. }
  281. return 1;
  282. }
  283. unsigned int nr_hardirq_chains;
  284. unsigned int nr_softirq_chains;
  285. unsigned int nr_process_chains;
  286. unsigned int max_lockdep_depth;
  287. #ifdef CONFIG_DEBUG_LOCKDEP
  288. static int lockdep_init_error;
  289. static const char *lock_init_error;
  290. static unsigned long lockdep_init_trace_data[20];
  291. static struct stack_trace lockdep_init_trace = {
  292. .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
  293. .entries = lockdep_init_trace_data,
  294. };
  295. DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
  296. #endif
  297. #define __USAGE(__STATE) \
  298. [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
  299. [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
  300. [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
  301. [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
  302. static const char *usage_str[] =
  303. {
  304. #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
  305. #include "lockdep_states.h"
  306. #undef LOCKDEP_STATE
  307. [LOCK_USED] = "INITIAL USE",
  308. };
  309. const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
  310. {
  311. return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
  312. }
  313. static inline unsigned long lock_flag(enum lock_usage_bit bit)
  314. {
  315. return 1UL << bit;
  316. }
  317. static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
  318. {
  319. char c = '.';
  320. if (class->usage_mask & lock_flag(bit + 2))
  321. c = '+';
  322. if (class->usage_mask & lock_flag(bit)) {
  323. c = '-';
  324. if (class->usage_mask & lock_flag(bit + 2))
  325. c = '?';
  326. }
  327. return c;
  328. }
  329. void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
  330. {
  331. int i = 0;
  332. #define LOCKDEP_STATE(__STATE) \
  333. usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
  334. usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
  335. #include "lockdep_states.h"
  336. #undef LOCKDEP_STATE
  337. usage[i] = '\0';
  338. }
  339. static void __print_lock_name(struct lock_class *class)
  340. {
  341. char str[KSYM_NAME_LEN];
  342. const char *name;
  343. name = class->name;
  344. if (!name) {
  345. name = __get_key_name(class->key, str);
  346. printk("%s", name);
  347. } else {
  348. printk("%s", name);
  349. if (class->name_version > 1)
  350. printk("#%d", class->name_version);
  351. if (class->subclass)
  352. printk("/%d", class->subclass);
  353. }
  354. }
  355. static void print_lock_name(struct lock_class *class)
  356. {
  357. char usage[LOCK_USAGE_CHARS];
  358. get_usage_chars(class, usage);
  359. printk(" (");
  360. __print_lock_name(class);
  361. printk("){%s}", usage);
  362. }
  363. static void print_lockdep_cache(struct lockdep_map *lock)
  364. {
  365. const char *name;
  366. char str[KSYM_NAME_LEN];
  367. name = lock->name;
  368. if (!name)
  369. name = __get_key_name(lock->key->subkeys, str);
  370. printk("%s", name);
  371. }
  372. static void print_lock(struct held_lock *hlock)
  373. {
  374. print_lock_name(hlock_class(hlock));
  375. printk(", at: ");
  376. print_ip_sym(hlock->acquire_ip);
  377. }
  378. static void lockdep_print_held_locks(struct task_struct *curr)
  379. {
  380. int i, depth = curr->lockdep_depth;
  381. if (!depth) {
  382. printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
  383. return;
  384. }
  385. printk("%d lock%s held by %s/%d:\n",
  386. depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
  387. for (i = 0; i < depth; i++) {
  388. printk(" #%d: ", i);
  389. print_lock(curr->held_locks + i);
  390. }
  391. }
  392. static void print_kernel_ident(void)
  393. {
  394. printk("%s %.*s %s\n", init_utsname()->release,
  395. (int)strcspn(init_utsname()->version, " "),
  396. init_utsname()->version,
  397. print_tainted());
  398. }
  399. static int very_verbose(struct lock_class *class)
  400. {
  401. #if VERY_VERBOSE
  402. return class_filter(class);
  403. #endif
  404. return 0;
  405. }
  406. static int static_obj(void *obj)
  407. {
  408. unsigned long start = (unsigned long) &_stext,
  409. end = (unsigned long) &_end,
  410. addr = (unsigned long) obj;
  411. if ((addr >= start) && (addr < end))
  412. return 1;
  413. if (arch_is_kernel_data(addr))
  414. return 1;
  415. if (is_kernel_percpu_address(addr))
  416. return 1;
  417. return is_module_address(addr) || is_module_percpu_address(addr);
  418. }
  419. static int count_matching_names(struct lock_class *new_class)
  420. {
  421. struct lock_class *class;
  422. int count = 0;
  423. if (!new_class->name)
  424. return 0;
  425. list_for_each_entry(class, &all_lock_classes, lock_entry) {
  426. if (new_class->key - new_class->subclass == class->key)
  427. return class->name_version;
  428. if (class->name && !strcmp(class->name, new_class->name))
  429. count = max(count, class->name_version);
  430. }
  431. return count + 1;
  432. }
  433. static inline struct lock_class *
  434. look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
  435. {
  436. struct lockdep_subclass_key *key;
  437. struct list_head *hash_head;
  438. struct lock_class *class;
  439. #ifdef CONFIG_DEBUG_LOCKDEP
  440. if (unlikely(!lockdep_initialized)) {
  441. lockdep_init();
  442. lockdep_init_error = 1;
  443. lock_init_error = lock->name;
  444. save_stack_trace(&lockdep_init_trace);
  445. }
  446. #endif
  447. if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
  448. debug_locks_off();
  449. printk(KERN_ERR
  450. "BUG: looking up invalid subclass: %u\n", subclass);
  451. printk(KERN_ERR
  452. "turning off the locking correctness validator.\n");
  453. dump_stack();
  454. return NULL;
  455. }
  456. if (unlikely(!lock->key))
  457. lock->key = (void *)lock;
  458. BUILD_BUG_ON(sizeof(struct lock_class_key) >
  459. sizeof(struct lockdep_map));
  460. key = lock->key->subkeys + subclass;
  461. hash_head = classhashentry(key);
  462. list_for_each_entry(class, hash_head, hash_entry) {
  463. if (class->key == key) {
  464. WARN_ON_ONCE(class->name != lock->name);
  465. return class;
  466. }
  467. }
  468. return NULL;
  469. }
  470. static inline struct lock_class *
  471. register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
  472. {
  473. struct lockdep_subclass_key *key;
  474. struct list_head *hash_head;
  475. struct lock_class *class;
  476. unsigned long flags;
  477. class = look_up_lock_class(lock, subclass);
  478. if (likely(class))
  479. goto out_set_class_cache;
  480. if (!static_obj(lock->key)) {
  481. debug_locks_off();
  482. printk("INFO: trying to register non-static key.\n");
  483. printk("the code is fine but needs lockdep annotation.\n");
  484. printk("turning off the locking correctness validator.\n");
  485. dump_stack();
  486. return NULL;
  487. }
  488. key = lock->key->subkeys + subclass;
  489. hash_head = classhashentry(key);
  490. raw_local_irq_save(flags);
  491. if (!graph_lock()) {
  492. raw_local_irq_restore(flags);
  493. return NULL;
  494. }
  495. list_for_each_entry(class, hash_head, hash_entry)
  496. if (class->key == key)
  497. goto out_unlock_set;
  498. if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
  499. if (!debug_locks_off_graph_unlock()) {
  500. raw_local_irq_restore(flags);
  501. return NULL;
  502. }
  503. raw_local_irq_restore(flags);
  504. printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
  505. printk("turning off the locking correctness validator.\n");
  506. dump_stack();
  507. return NULL;
  508. }
  509. class = lock_classes + nr_lock_classes++;
  510. debug_atomic_inc(nr_unused_locks);
  511. class->key = key;
  512. class->name = lock->name;
  513. class->subclass = subclass;
  514. INIT_LIST_HEAD(&class->lock_entry);
  515. INIT_LIST_HEAD(&class->locks_before);
  516. INIT_LIST_HEAD(&class->locks_after);
  517. class->name_version = count_matching_names(class);
  518. list_add_tail_rcu(&class->hash_entry, hash_head);
  519. list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
  520. if (verbose(class)) {
  521. graph_unlock();
  522. raw_local_irq_restore(flags);
  523. printk("\nnew class %p: %s", class->key, class->name);
  524. if (class->name_version > 1)
  525. printk("#%d", class->name_version);
  526. printk("\n");
  527. dump_stack();
  528. raw_local_irq_save(flags);
  529. if (!graph_lock()) {
  530. raw_local_irq_restore(flags);
  531. return NULL;
  532. }
  533. }
  534. out_unlock_set:
  535. graph_unlock();
  536. raw_local_irq_restore(flags);
  537. out_set_class_cache:
  538. if (!subclass || force)
  539. lock->class_cache[0] = class;
  540. else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
  541. lock->class_cache[subclass] = class;
  542. if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
  543. return NULL;
  544. return class;
  545. }
  546. #ifdef CONFIG_PROVE_LOCKING
  547. static struct lock_list *alloc_list_entry(void)
  548. {
  549. if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
  550. if (!debug_locks_off_graph_unlock())
  551. return NULL;
  552. printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
  553. printk("turning off the locking correctness validator.\n");
  554. dump_stack();
  555. return NULL;
  556. }
  557. return list_entries + nr_list_entries++;
  558. }
  559. static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
  560. struct list_head *head, unsigned long ip,
  561. int distance, struct stack_trace *trace)
  562. {
  563. struct lock_list *entry;
  564. entry = alloc_list_entry();
  565. if (!entry)
  566. return 0;
  567. entry->class = this;
  568. entry->distance = distance;
  569. entry->trace = *trace;
  570. list_add_tail_rcu(&entry->entry, head);
  571. return 1;
  572. }
  573. #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
  574. #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
  575. struct circular_queue {
  576. unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
  577. unsigned int front, rear;
  578. };
  579. static struct circular_queue lock_cq;
  580. unsigned int max_bfs_queue_depth;
  581. static unsigned int lockdep_dependency_gen_id;
  582. static inline void __cq_init(struct circular_queue *cq)
  583. {
  584. cq->front = cq->rear = 0;
  585. lockdep_dependency_gen_id++;
  586. }
  587. static inline int __cq_empty(struct circular_queue *cq)
  588. {
  589. return (cq->front == cq->rear);
  590. }
  591. static inline int __cq_full(struct circular_queue *cq)
  592. {
  593. return ((cq->rear + 1) & CQ_MASK) == cq->front;
  594. }
  595. static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
  596. {
  597. if (__cq_full(cq))
  598. return -1;
  599. cq->element[cq->rear] = elem;
  600. cq->rear = (cq->rear + 1) & CQ_MASK;
  601. return 0;
  602. }
  603. static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
  604. {
  605. if (__cq_empty(cq))
  606. return -1;
  607. *elem = cq->element[cq->front];
  608. cq->front = (cq->front + 1) & CQ_MASK;
  609. return 0;
  610. }
  611. static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
  612. {
  613. return (cq->rear - cq->front) & CQ_MASK;
  614. }
  615. static inline void mark_lock_accessed(struct lock_list *lock,
  616. struct lock_list *parent)
  617. {
  618. unsigned long nr;
  619. nr = lock - list_entries;
  620. WARN_ON(nr >= nr_list_entries);
  621. lock->parent = parent;
  622. lock->class->dep_gen_id = lockdep_dependency_gen_id;
  623. }
  624. static inline unsigned long lock_accessed(struct lock_list *lock)
  625. {
  626. unsigned long nr;
  627. nr = lock - list_entries;
  628. WARN_ON(nr >= nr_list_entries);
  629. return lock->class->dep_gen_id == lockdep_dependency_gen_id;
  630. }
  631. static inline struct lock_list *get_lock_parent(struct lock_list *child)
  632. {
  633. return child->parent;
  634. }
  635. static inline int get_lock_depth(struct lock_list *child)
  636. {
  637. int depth = 0;
  638. struct lock_list *parent;
  639. while ((parent = get_lock_parent(child))) {
  640. child = parent;
  641. depth++;
  642. }
  643. return depth;
  644. }
  645. static int __bfs(struct lock_list *source_entry,
  646. void *data,
  647. int (*match)(struct lock_list *entry, void *data),
  648. struct lock_list **target_entry,
  649. int forward)
  650. {
  651. struct lock_list *entry;
  652. struct list_head *head;
  653. struct circular_queue *cq = &lock_cq;
  654. int ret = 1;
  655. if (match(source_entry, data)) {
  656. *target_entry = source_entry;
  657. ret = 0;
  658. goto exit;
  659. }
  660. if (forward)
  661. head = &source_entry->class->locks_after;
  662. else
  663. head = &source_entry->class->locks_before;
  664. if (list_empty(head))
  665. goto exit;
  666. __cq_init(cq);
  667. __cq_enqueue(cq, (unsigned long)source_entry);
  668. while (!__cq_empty(cq)) {
  669. struct lock_list *lock;
  670. __cq_dequeue(cq, (unsigned long *)&lock);
  671. if (!lock->class) {
  672. ret = -2;
  673. goto exit;
  674. }
  675. if (forward)
  676. head = &lock->class->locks_after;
  677. else
  678. head = &lock->class->locks_before;
  679. list_for_each_entry(entry, head, entry) {
  680. if (!lock_accessed(entry)) {
  681. unsigned int cq_depth;
  682. mark_lock_accessed(entry, lock);
  683. if (match(entry, data)) {
  684. *target_entry = entry;
  685. ret = 0;
  686. goto exit;
  687. }
  688. if (__cq_enqueue(cq, (unsigned long)entry)) {
  689. ret = -1;
  690. goto exit;
  691. }
  692. cq_depth = __cq_get_elem_count(cq);
  693. if (max_bfs_queue_depth < cq_depth)
  694. max_bfs_queue_depth = cq_depth;
  695. }
  696. }
  697. }
  698. exit:
  699. return ret;
  700. }
  701. static inline int __bfs_forwards(struct lock_list *src_entry,
  702. void *data,
  703. int (*match)(struct lock_list *entry, void *data),
  704. struct lock_list **target_entry)
  705. {
  706. return __bfs(src_entry, data, match, target_entry, 1);
  707. }
  708. static inline int __bfs_backwards(struct lock_list *src_entry,
  709. void *data,
  710. int (*match)(struct lock_list *entry, void *data),
  711. struct lock_list **target_entry)
  712. {
  713. return __bfs(src_entry, data, match, target_entry, 0);
  714. }
  715. static noinline int
  716. print_circular_bug_entry(struct lock_list *target, int depth)
  717. {
  718. if (debug_locks_silent)
  719. return 0;
  720. printk("\n-> #%u", depth);
  721. print_lock_name(target->class);
  722. printk(":\n");
  723. print_stack_trace(&target->trace, 6);
  724. return 0;
  725. }
  726. static void
  727. print_circular_lock_scenario(struct held_lock *src,
  728. struct held_lock *tgt,
  729. struct lock_list *prt)
  730. {
  731. struct lock_class *source = hlock_class(src);
  732. struct lock_class *target = hlock_class(tgt);
  733. struct lock_class *parent = prt->class;
  734. if (parent != source) {
  735. printk("Chain exists of:\n ");
  736. __print_lock_name(source);
  737. printk(" --> ");
  738. __print_lock_name(parent);
  739. printk(" --> ");
  740. __print_lock_name(target);
  741. printk("\n\n");
  742. }
  743. printk(" Possible unsafe locking scenario:\n\n");
  744. printk(" CPU0 CPU1\n");
  745. printk(" ---- ----\n");
  746. printk(" lock(");
  747. __print_lock_name(target);
  748. printk(");\n");
  749. printk(" lock(");
  750. __print_lock_name(parent);
  751. printk(");\n");
  752. printk(" lock(");
  753. __print_lock_name(target);
  754. printk(");\n");
  755. printk(" lock(");
  756. __print_lock_name(source);
  757. printk(");\n");
  758. printk("\n *** DEADLOCK ***\n\n");
  759. }
  760. static noinline int
  761. print_circular_bug_header(struct lock_list *entry, unsigned int depth,
  762. struct held_lock *check_src,
  763. struct held_lock *check_tgt)
  764. {
  765. struct task_struct *curr = current;
  766. if (debug_locks_silent)
  767. return 0;
  768. printk("\n");
  769. printk("======================================================\n");
  770. printk("[ INFO: possible circular locking dependency detected ]\n");
  771. print_kernel_ident();
  772. printk("-------------------------------------------------------\n");
  773. printk("%s/%d is trying to acquire lock:\n",
  774. curr->comm, task_pid_nr(curr));
  775. print_lock(check_src);
  776. printk("\nbut task is already holding lock:\n");
  777. print_lock(check_tgt);
  778. printk("\nwhich lock already depends on the new lock.\n\n");
  779. printk("\nthe existing dependency chain (in reverse order) is:\n");
  780. print_circular_bug_entry(entry, depth);
  781. return 0;
  782. }
  783. static inline int class_equal(struct lock_list *entry, void *data)
  784. {
  785. return entry->class == data;
  786. }
  787. static noinline int print_circular_bug(struct lock_list *this,
  788. struct lock_list *target,
  789. struct held_lock *check_src,
  790. struct held_lock *check_tgt)
  791. {
  792. struct task_struct *curr = current;
  793. struct lock_list *parent;
  794. struct lock_list *first_parent;
  795. int depth;
  796. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  797. return 0;
  798. if (!save_trace(&this->trace))
  799. return 0;
  800. depth = get_lock_depth(target);
  801. print_circular_bug_header(target, depth, check_src, check_tgt);
  802. parent = get_lock_parent(target);
  803. first_parent = parent;
  804. while (parent) {
  805. print_circular_bug_entry(parent, --depth);
  806. parent = get_lock_parent(parent);
  807. }
  808. printk("\nother info that might help us debug this:\n\n");
  809. print_circular_lock_scenario(check_src, check_tgt,
  810. first_parent);
  811. lockdep_print_held_locks(curr);
  812. printk("\nstack backtrace:\n");
  813. dump_stack();
  814. return 0;
  815. }
  816. static noinline int print_bfs_bug(int ret)
  817. {
  818. if (!debug_locks_off_graph_unlock())
  819. return 0;
  820. WARN(1, "lockdep bfs error:%d\n", ret);
  821. return 0;
  822. }
  823. static int noop_count(struct lock_list *entry, void *data)
  824. {
  825. (*(unsigned long *)data)++;
  826. return 0;
  827. }
  828. unsigned long __lockdep_count_forward_deps(struct lock_list *this)
  829. {
  830. unsigned long count = 0;
  831. struct lock_list *uninitialized_var(target_entry);
  832. __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
  833. return count;
  834. }
  835. unsigned long lockdep_count_forward_deps(struct lock_class *class)
  836. {
  837. unsigned long ret, flags;
  838. struct lock_list this;
  839. this.parent = NULL;
  840. this.class = class;
  841. local_irq_save(flags);
  842. arch_spin_lock(&lockdep_lock);
  843. ret = __lockdep_count_forward_deps(&this);
  844. arch_spin_unlock(&lockdep_lock);
  845. local_irq_restore(flags);
  846. return ret;
  847. }
  848. unsigned long __lockdep_count_backward_deps(struct lock_list *this)
  849. {
  850. unsigned long count = 0;
  851. struct lock_list *uninitialized_var(target_entry);
  852. __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
  853. return count;
  854. }
  855. unsigned long lockdep_count_backward_deps(struct lock_class *class)
  856. {
  857. unsigned long ret, flags;
  858. struct lock_list this;
  859. this.parent = NULL;
  860. this.class = class;
  861. local_irq_save(flags);
  862. arch_spin_lock(&lockdep_lock);
  863. ret = __lockdep_count_backward_deps(&this);
  864. arch_spin_unlock(&lockdep_lock);
  865. local_irq_restore(flags);
  866. return ret;
  867. }
  868. static noinline int
  869. check_noncircular(struct lock_list *root, struct lock_class *target,
  870. struct lock_list **target_entry)
  871. {
  872. int result;
  873. debug_atomic_inc(nr_cyclic_checks);
  874. result = __bfs_forwards(root, target, class_equal, target_entry);
  875. return result;
  876. }
  877. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
  878. static inline int usage_match(struct lock_list *entry, void *bit)
  879. {
  880. return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
  881. }
  882. static int
  883. find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
  884. struct lock_list **target_entry)
  885. {
  886. int result;
  887. debug_atomic_inc(nr_find_usage_forwards_checks);
  888. result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
  889. return result;
  890. }
  891. static int
  892. find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
  893. struct lock_list **target_entry)
  894. {
  895. int result;
  896. debug_atomic_inc(nr_find_usage_backwards_checks);
  897. result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
  898. return result;
  899. }
  900. static void print_lock_class_header(struct lock_class *class, int depth)
  901. {
  902. int bit;
  903. printk("%*s->", depth, "");
  904. print_lock_name(class);
  905. printk(" ops: %lu", class->ops);
  906. printk(" {\n");
  907. for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
  908. if (class->usage_mask & (1 << bit)) {
  909. int len = depth;
  910. len += printk("%*s %s", depth, "", usage_str[bit]);
  911. len += printk(" at:\n");
  912. print_stack_trace(class->usage_traces + bit, len);
  913. }
  914. }
  915. printk("%*s }\n", depth, "");
  916. printk("%*s ... key at: ",depth,"");
  917. print_ip_sym((unsigned long)class->key);
  918. }
  919. static void __used
  920. print_shortest_lock_dependencies(struct lock_list *leaf,
  921. struct lock_list *root)
  922. {
  923. struct lock_list *entry = leaf;
  924. int depth;
  925. depth = get_lock_depth(leaf);
  926. do {
  927. print_lock_class_header(entry->class, depth);
  928. printk("%*s ... acquired at:\n", depth, "");
  929. print_stack_trace(&entry->trace, 2);
  930. printk("\n");
  931. if (depth == 0 && (entry != root)) {
  932. printk("lockdep:%s bad path found in chain graph\n", __func__);
  933. break;
  934. }
  935. entry = get_lock_parent(entry);
  936. depth--;
  937. } while (entry && (depth >= 0));
  938. return;
  939. }
  940. static void
  941. print_irq_lock_scenario(struct lock_list *safe_entry,
  942. struct lock_list *unsafe_entry,
  943. struct lock_class *prev_class,
  944. struct lock_class *next_class)
  945. {
  946. struct lock_class *safe_class = safe_entry->class;
  947. struct lock_class *unsafe_class = unsafe_entry->class;
  948. struct lock_class *middle_class = prev_class;
  949. if (middle_class == safe_class)
  950. middle_class = next_class;
  951. if (middle_class != unsafe_class) {
  952. printk("Chain exists of:\n ");
  953. __print_lock_name(safe_class);
  954. printk(" --> ");
  955. __print_lock_name(middle_class);
  956. printk(" --> ");
  957. __print_lock_name(unsafe_class);
  958. printk("\n\n");
  959. }
  960. printk(" Possible interrupt unsafe locking scenario:\n\n");
  961. printk(" CPU0 CPU1\n");
  962. printk(" ---- ----\n");
  963. printk(" lock(");
  964. __print_lock_name(unsafe_class);
  965. printk(");\n");
  966. printk(" local_irq_disable();\n");
  967. printk(" lock(");
  968. __print_lock_name(safe_class);
  969. printk(");\n");
  970. printk(" lock(");
  971. __print_lock_name(middle_class);
  972. printk(");\n");
  973. printk(" <Interrupt>\n");
  974. printk(" lock(");
  975. __print_lock_name(safe_class);
  976. printk(");\n");
  977. printk("\n *** DEADLOCK ***\n\n");
  978. }
  979. static int
  980. print_bad_irq_dependency(struct task_struct *curr,
  981. struct lock_list *prev_root,
  982. struct lock_list *next_root,
  983. struct lock_list *backwards_entry,
  984. struct lock_list *forwards_entry,
  985. struct held_lock *prev,
  986. struct held_lock *next,
  987. enum lock_usage_bit bit1,
  988. enum lock_usage_bit bit2,
  989. const char *irqclass)
  990. {
  991. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  992. return 0;
  993. printk("\n");
  994. printk("======================================================\n");
  995. printk("[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
  996. irqclass, irqclass);
  997. print_kernel_ident();
  998. printk("------------------------------------------------------\n");
  999. printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
  1000. curr->comm, task_pid_nr(curr),
  1001. curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
  1002. curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
  1003. curr->hardirqs_enabled,
  1004. curr->softirqs_enabled);
  1005. print_lock(next);
  1006. printk("\nand this task is already holding:\n");
  1007. print_lock(prev);
  1008. printk("which would create a new lock dependency:\n");
  1009. print_lock_name(hlock_class(prev));
  1010. printk(" ->");
  1011. print_lock_name(hlock_class(next));
  1012. printk("\n");
  1013. printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
  1014. irqclass);
  1015. print_lock_name(backwards_entry->class);
  1016. printk("\n... which became %s-irq-safe at:\n", irqclass);
  1017. print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
  1018. printk("\nto a %s-irq-unsafe lock:\n", irqclass);
  1019. print_lock_name(forwards_entry->class);
  1020. printk("\n... which became %s-irq-unsafe at:\n", irqclass);
  1021. printk("...");
  1022. print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
  1023. printk("\nother info that might help us debug this:\n\n");
  1024. print_irq_lock_scenario(backwards_entry, forwards_entry,
  1025. hlock_class(prev), hlock_class(next));
  1026. lockdep_print_held_locks(curr);
  1027. printk("\nthe dependencies between %s-irq-safe lock", irqclass);
  1028. printk(" and the holding lock:\n");
  1029. if (!save_trace(&prev_root->trace))
  1030. return 0;
  1031. print_shortest_lock_dependencies(backwards_entry, prev_root);
  1032. printk("\nthe dependencies between the lock to be acquired");
  1033. printk(" and %s-irq-unsafe lock:\n", irqclass);
  1034. if (!save_trace(&next_root->trace))
  1035. return 0;
  1036. print_shortest_lock_dependencies(forwards_entry, next_root);
  1037. printk("\nstack backtrace:\n");
  1038. dump_stack();
  1039. return 0;
  1040. }
  1041. static int
  1042. check_usage(struct task_struct *curr, struct held_lock *prev,
  1043. struct held_lock *next, enum lock_usage_bit bit_backwards,
  1044. enum lock_usage_bit bit_forwards, const char *irqclass)
  1045. {
  1046. int ret;
  1047. struct lock_list this, that;
  1048. struct lock_list *uninitialized_var(target_entry);
  1049. struct lock_list *uninitialized_var(target_entry1);
  1050. this.parent = NULL;
  1051. this.class = hlock_class(prev);
  1052. ret = find_usage_backwards(&this, bit_backwards, &target_entry);
  1053. if (ret < 0)
  1054. return print_bfs_bug(ret);
  1055. if (ret == 1)
  1056. return ret;
  1057. that.parent = NULL;
  1058. that.class = hlock_class(next);
  1059. ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
  1060. if (ret < 0)
  1061. return print_bfs_bug(ret);
  1062. if (ret == 1)
  1063. return ret;
  1064. return print_bad_irq_dependency(curr, &this, &that,
  1065. target_entry, target_entry1,
  1066. prev, next,
  1067. bit_backwards, bit_forwards, irqclass);
  1068. }
  1069. static const char *state_names[] = {
  1070. #define LOCKDEP_STATE(__STATE) \
  1071. __stringify(__STATE),
  1072. #include "lockdep_states.h"
  1073. #undef LOCKDEP_STATE
  1074. };
  1075. static const char *state_rnames[] = {
  1076. #define LOCKDEP_STATE(__STATE) \
  1077. __stringify(__STATE)"-READ",
  1078. #include "lockdep_states.h"
  1079. #undef LOCKDEP_STATE
  1080. };
  1081. static inline const char *state_name(enum lock_usage_bit bit)
  1082. {
  1083. return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
  1084. }
  1085. static int exclusive_bit(int new_bit)
  1086. {
  1087. int state = new_bit & ~3;
  1088. int dir = new_bit & 2;
  1089. return state | (dir ^ 2);
  1090. }
  1091. static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
  1092. struct held_lock *next, enum lock_usage_bit bit)
  1093. {
  1094. if (!check_usage(curr, prev, next, bit,
  1095. exclusive_bit(bit), state_name(bit)))
  1096. return 0;
  1097. bit++;
  1098. if (!check_usage(curr, prev, next, bit,
  1099. exclusive_bit(bit), state_name(bit)))
  1100. return 0;
  1101. return 1;
  1102. }
  1103. static int
  1104. check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
  1105. struct held_lock *next)
  1106. {
  1107. #define LOCKDEP_STATE(__STATE) \
  1108. if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
  1109. return 0;
  1110. #include "lockdep_states.h"
  1111. #undef LOCKDEP_STATE
  1112. return 1;
  1113. }
  1114. static void inc_chains(void)
  1115. {
  1116. if (current->hardirq_context)
  1117. nr_hardirq_chains++;
  1118. else {
  1119. if (current->softirq_context)
  1120. nr_softirq_chains++;
  1121. else
  1122. nr_process_chains++;
  1123. }
  1124. }
  1125. #else
  1126. static inline int
  1127. check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
  1128. struct held_lock *next)
  1129. {
  1130. return 1;
  1131. }
  1132. static inline void inc_chains(void)
  1133. {
  1134. nr_process_chains++;
  1135. }
  1136. #endif
  1137. static void
  1138. print_deadlock_scenario(struct held_lock *nxt,
  1139. struct held_lock *prv)
  1140. {
  1141. struct lock_class *next = hlock_class(nxt);
  1142. struct lock_class *prev = hlock_class(prv);
  1143. printk(" Possible unsafe locking scenario:\n\n");
  1144. printk(" CPU0\n");
  1145. printk(" ----\n");
  1146. printk(" lock(");
  1147. __print_lock_name(prev);
  1148. printk(");\n");
  1149. printk(" lock(");
  1150. __print_lock_name(next);
  1151. printk(");\n");
  1152. printk("\n *** DEADLOCK ***\n\n");
  1153. printk(" May be due to missing lock nesting notation\n\n");
  1154. }
  1155. static int
  1156. print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
  1157. struct held_lock *next)
  1158. {
  1159. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1160. return 0;
  1161. printk("\n");
  1162. printk("=============================================\n");
  1163. printk("[ INFO: possible recursive locking detected ]\n");
  1164. print_kernel_ident();
  1165. printk("---------------------------------------------\n");
  1166. printk("%s/%d is trying to acquire lock:\n",
  1167. curr->comm, task_pid_nr(curr));
  1168. print_lock(next);
  1169. printk("\nbut task is already holding lock:\n");
  1170. print_lock(prev);
  1171. printk("\nother info that might help us debug this:\n");
  1172. print_deadlock_scenario(next, prev);
  1173. lockdep_print_held_locks(curr);
  1174. printk("\nstack backtrace:\n");
  1175. dump_stack();
  1176. return 0;
  1177. }
  1178. static int
  1179. check_deadlock(struct task_struct *curr, struct held_lock *next,
  1180. struct lockdep_map *next_instance, int read)
  1181. {
  1182. struct held_lock *prev;
  1183. struct held_lock *nest = NULL;
  1184. int i;
  1185. for (i = 0; i < curr->lockdep_depth; i++) {
  1186. prev = curr->held_locks + i;
  1187. if (prev->instance == next->nest_lock)
  1188. nest = prev;
  1189. if (hlock_class(prev) != hlock_class(next))
  1190. continue;
  1191. if ((read == 2) && prev->read)
  1192. return 2;
  1193. if (nest)
  1194. return 2;
  1195. return print_deadlock_bug(curr, prev, next);
  1196. }
  1197. return 1;
  1198. }
  1199. static int
  1200. check_prev_add(struct task_struct *curr, struct held_lock *prev,
  1201. struct held_lock *next, int distance, int trylock_loop)
  1202. {
  1203. struct lock_list *entry;
  1204. int ret;
  1205. struct lock_list this;
  1206. struct lock_list *uninitialized_var(target_entry);
  1207. static struct stack_trace trace;
  1208. this.class = hlock_class(next);
  1209. this.parent = NULL;
  1210. ret = check_noncircular(&this, hlock_class(prev), &target_entry);
  1211. if (unlikely(!ret))
  1212. return print_circular_bug(&this, target_entry, next, prev);
  1213. else if (unlikely(ret < 0))
  1214. return print_bfs_bug(ret);
  1215. if (!check_prev_add_irq(curr, prev, next))
  1216. return 0;
  1217. if (next->read == 2 || prev->read == 2)
  1218. return 1;
  1219. list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
  1220. if (entry->class == hlock_class(next)) {
  1221. if (distance == 1)
  1222. entry->distance = 1;
  1223. return 2;
  1224. }
  1225. }
  1226. if (!trylock_loop && !save_trace(&trace))
  1227. return 0;
  1228. ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
  1229. &hlock_class(prev)->locks_after,
  1230. next->acquire_ip, distance, &trace);
  1231. if (!ret)
  1232. return 0;
  1233. ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
  1234. &hlock_class(next)->locks_before,
  1235. next->acquire_ip, distance, &trace);
  1236. if (!ret)
  1237. return 0;
  1238. if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
  1239. graph_unlock();
  1240. printk("\n new dependency: ");
  1241. print_lock_name(hlock_class(prev));
  1242. printk(" => ");
  1243. print_lock_name(hlock_class(next));
  1244. printk("\n");
  1245. dump_stack();
  1246. return graph_lock();
  1247. }
  1248. return 1;
  1249. }
  1250. static int
  1251. check_prevs_add(struct task_struct *curr, struct held_lock *next)
  1252. {
  1253. int depth = curr->lockdep_depth;
  1254. int trylock_loop = 0;
  1255. struct held_lock *hlock;
  1256. if (!depth)
  1257. goto out_bug;
  1258. if (curr->held_locks[depth].irq_context !=
  1259. curr->held_locks[depth-1].irq_context)
  1260. goto out_bug;
  1261. for (;;) {
  1262. int distance = curr->lockdep_depth - depth + 1;
  1263. hlock = curr->held_locks + depth-1;
  1264. if (hlock->read != 2) {
  1265. if (!check_prev_add(curr, hlock, next,
  1266. distance, trylock_loop))
  1267. return 0;
  1268. if (!hlock->trylock)
  1269. break;
  1270. }
  1271. depth--;
  1272. if (!depth)
  1273. break;
  1274. if (curr->held_locks[depth].irq_context !=
  1275. curr->held_locks[depth-1].irq_context)
  1276. break;
  1277. trylock_loop = 1;
  1278. }
  1279. return 1;
  1280. out_bug:
  1281. if (!debug_locks_off_graph_unlock())
  1282. return 0;
  1283. WARN_ON(1);
  1284. return 0;
  1285. }
  1286. unsigned long nr_lock_chains;
  1287. struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
  1288. int nr_chain_hlocks;
  1289. static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
  1290. struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
  1291. {
  1292. return lock_classes + chain_hlocks[chain->base + i];
  1293. }
  1294. static inline int lookup_chain_cache(struct task_struct *curr,
  1295. struct held_lock *hlock,
  1296. u64 chain_key)
  1297. {
  1298. struct lock_class *class = hlock_class(hlock);
  1299. struct list_head *hash_head = chainhashentry(chain_key);
  1300. struct lock_chain *chain;
  1301. struct held_lock *hlock_curr, *hlock_next;
  1302. int i, j;
  1303. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  1304. return 0;
  1305. list_for_each_entry(chain, hash_head, entry) {
  1306. if (chain->chain_key == chain_key) {
  1307. cache_hit:
  1308. debug_atomic_inc(chain_lookup_hits);
  1309. if (very_verbose(class))
  1310. printk("\nhash chain already cached, key: "
  1311. "%016Lx tail class: [%p] %s\n",
  1312. (unsigned long long)chain_key,
  1313. class->key, class->name);
  1314. return 0;
  1315. }
  1316. }
  1317. if (very_verbose(class))
  1318. printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
  1319. (unsigned long long)chain_key, class->key, class->name);
  1320. if (!graph_lock())
  1321. return 0;
  1322. list_for_each_entry(chain, hash_head, entry) {
  1323. if (chain->chain_key == chain_key) {
  1324. graph_unlock();
  1325. goto cache_hit;
  1326. }
  1327. }
  1328. if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
  1329. if (!debug_locks_off_graph_unlock())
  1330. return 0;
  1331. printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
  1332. printk("turning off the locking correctness validator.\n");
  1333. dump_stack();
  1334. return 0;
  1335. }
  1336. chain = lock_chains + nr_lock_chains++;
  1337. chain->chain_key = chain_key;
  1338. chain->irq_context = hlock->irq_context;
  1339. hlock_next = hlock;
  1340. for (i = curr->lockdep_depth - 1; i >= 0; i--) {
  1341. hlock_curr = curr->held_locks + i;
  1342. if (hlock_curr->irq_context != hlock_next->irq_context)
  1343. break;
  1344. hlock_next = hlock;
  1345. }
  1346. i++;
  1347. chain->depth = curr->lockdep_depth + 1 - i;
  1348. if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
  1349. chain->base = nr_chain_hlocks;
  1350. nr_chain_hlocks += chain->depth;
  1351. for (j = 0; j < chain->depth - 1; j++, i++) {
  1352. int lock_id = curr->held_locks[i].class_idx - 1;
  1353. chain_hlocks[chain->base + j] = lock_id;
  1354. }
  1355. chain_hlocks[chain->base + j] = class - lock_classes;
  1356. }
  1357. list_add_tail_rcu(&chain->entry, hash_head);
  1358. debug_atomic_inc(chain_lookup_misses);
  1359. inc_chains();
  1360. return 1;
  1361. }
  1362. static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
  1363. struct held_lock *hlock, int chain_head, u64 chain_key)
  1364. {
  1365. if (!hlock->trylock && (hlock->check == 2) &&
  1366. lookup_chain_cache(curr, hlock, chain_key)) {
  1367. int ret = check_deadlock(curr, hlock, lock, hlock->read);
  1368. if (!ret)
  1369. return 0;
  1370. if (ret == 2)
  1371. hlock->read = 2;
  1372. if (!chain_head && ret != 2)
  1373. if (!check_prevs_add(curr, hlock))
  1374. return 0;
  1375. graph_unlock();
  1376. } else
  1377. if (unlikely(!debug_locks))
  1378. return 0;
  1379. return 1;
  1380. }
  1381. #else
  1382. static inline int validate_chain(struct task_struct *curr,
  1383. struct lockdep_map *lock, struct held_lock *hlock,
  1384. int chain_head, u64 chain_key)
  1385. {
  1386. return 1;
  1387. }
  1388. #endif
  1389. static void check_chain_key(struct task_struct *curr)
  1390. {
  1391. #ifdef CONFIG_DEBUG_LOCKDEP
  1392. struct held_lock *hlock, *prev_hlock = NULL;
  1393. unsigned int i, id;
  1394. u64 chain_key = 0;
  1395. for (i = 0; i < curr->lockdep_depth; i++) {
  1396. hlock = curr->held_locks + i;
  1397. if (chain_key != hlock->prev_chain_key) {
  1398. debug_locks_off();
  1399. WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
  1400. curr->lockdep_depth, i,
  1401. (unsigned long long)chain_key,
  1402. (unsigned long long)hlock->prev_chain_key);
  1403. return;
  1404. }
  1405. id = hlock->class_idx - 1;
  1406. if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
  1407. return;
  1408. if (prev_hlock && (prev_hlock->irq_context !=
  1409. hlock->irq_context))
  1410. chain_key = 0;
  1411. chain_key = iterate_chain_key(chain_key, id);
  1412. prev_hlock = hlock;
  1413. }
  1414. if (chain_key != curr->curr_chain_key) {
  1415. debug_locks_off();
  1416. WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
  1417. curr->lockdep_depth, i,
  1418. (unsigned long long)chain_key,
  1419. (unsigned long long)curr->curr_chain_key);
  1420. }
  1421. #endif
  1422. }
  1423. static void
  1424. print_usage_bug_scenario(struct held_lock *lock)
  1425. {
  1426. struct lock_class *class = hlock_class(lock);
  1427. printk(" Possible unsafe locking scenario:\n\n");
  1428. printk(" CPU0\n");
  1429. printk(" ----\n");
  1430. printk(" lock(");
  1431. __print_lock_name(class);
  1432. printk(");\n");
  1433. printk(" <Interrupt>\n");
  1434. printk(" lock(");
  1435. __print_lock_name(class);
  1436. printk(");\n");
  1437. printk("\n *** DEADLOCK ***\n\n");
  1438. }
  1439. static int
  1440. print_usage_bug(struct task_struct *curr, struct held_lock *this,
  1441. enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
  1442. {
  1443. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1444. return 0;
  1445. printk("\n");
  1446. printk("=================================\n");
  1447. printk("[ INFO: inconsistent lock state ]\n");
  1448. print_kernel_ident();
  1449. printk("---------------------------------\n");
  1450. printk("inconsistent {%s} -> {%s} usage.\n",
  1451. usage_str[prev_bit], usage_str[new_bit]);
  1452. printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
  1453. curr->comm, task_pid_nr(curr),
  1454. trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
  1455. trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
  1456. trace_hardirqs_enabled(curr),
  1457. trace_softirqs_enabled(curr));
  1458. print_lock(this);
  1459. printk("{%s} state was registered at:\n", usage_str[prev_bit]);
  1460. print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
  1461. print_irqtrace_events(curr);
  1462. printk("\nother info that might help us debug this:\n");
  1463. print_usage_bug_scenario(this);
  1464. lockdep_print_held_locks(curr);
  1465. printk("\nstack backtrace:\n");
  1466. dump_stack();
  1467. return 0;
  1468. }
  1469. static inline int
  1470. valid_state(struct task_struct *curr, struct held_lock *this,
  1471. enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
  1472. {
  1473. if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
  1474. return print_usage_bug(curr, this, bad_bit, new_bit);
  1475. return 1;
  1476. }
  1477. static int mark_lock(struct task_struct *curr, struct held_lock *this,
  1478. enum lock_usage_bit new_bit);
  1479. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
  1480. static int
  1481. print_irq_inversion_bug(struct task_struct *curr,
  1482. struct lock_list *root, struct lock_list *other,
  1483. struct held_lock *this, int forwards,
  1484. const char *irqclass)
  1485. {
  1486. struct lock_list *entry = other;
  1487. struct lock_list *middle = NULL;
  1488. int depth;
  1489. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1490. return 0;
  1491. printk("\n");
  1492. printk("=========================================================\n");
  1493. printk("[ INFO: possible irq lock inversion dependency detected ]\n");
  1494. print_kernel_ident();
  1495. printk("---------------------------------------------------------\n");
  1496. printk("%s/%d just changed the state of lock:\n",
  1497. curr->comm, task_pid_nr(curr));
  1498. print_lock(this);
  1499. if (forwards)
  1500. printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
  1501. else
  1502. printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
  1503. print_lock_name(other->class);
  1504. printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
  1505. printk("\nother info that might help us debug this:\n");
  1506. depth = get_lock_depth(other);
  1507. do {
  1508. if (depth == 0 && (entry != root)) {
  1509. printk("lockdep:%s bad path found in chain graph\n", __func__);
  1510. break;
  1511. }
  1512. middle = entry;
  1513. entry = get_lock_parent(entry);
  1514. depth--;
  1515. } while (entry && entry != root && (depth >= 0));
  1516. if (forwards)
  1517. print_irq_lock_scenario(root, other,
  1518. middle ? middle->class : root->class, other->class);
  1519. else
  1520. print_irq_lock_scenario(other, root,
  1521. middle ? middle->class : other->class, root->class);
  1522. lockdep_print_held_locks(curr);
  1523. printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
  1524. if (!save_trace(&root->trace))
  1525. return 0;
  1526. print_shortest_lock_dependencies(other, root);
  1527. printk("\nstack backtrace:\n");
  1528. dump_stack();
  1529. return 0;
  1530. }
  1531. static int
  1532. check_usage_forwards(struct task_struct *curr, struct held_lock *this,
  1533. enum lock_usage_bit bit, const char *irqclass)
  1534. {
  1535. int ret;
  1536. struct lock_list root;
  1537. struct lock_list *uninitialized_var(target_entry);
  1538. root.parent = NULL;
  1539. root.class = hlock_class(this);
  1540. ret = find_usage_forwards(&root, bit, &target_entry);
  1541. if (ret < 0)
  1542. return print_bfs_bug(ret);
  1543. if (ret == 1)
  1544. return ret;
  1545. return print_irq_inversion_bug(curr, &root, target_entry,
  1546. this, 1, irqclass);
  1547. }
  1548. static int
  1549. check_usage_backwards(struct task_struct *curr, struct held_lock *this,
  1550. enum lock_usage_bit bit, const char *irqclass)
  1551. {
  1552. int ret;
  1553. struct lock_list root;
  1554. struct lock_list *uninitialized_var(target_entry);
  1555. root.parent = NULL;
  1556. root.class = hlock_class(this);
  1557. ret = find_usage_backwards(&root, bit, &target_entry);
  1558. if (ret < 0)
  1559. return print_bfs_bug(ret);
  1560. if (ret == 1)
  1561. return ret;
  1562. return print_irq_inversion_bug(curr, &root, target_entry,
  1563. this, 0, irqclass);
  1564. }
  1565. void print_irqtrace_events(struct task_struct *curr)
  1566. {
  1567. printk("irq event stamp: %u\n", curr->irq_events);
  1568. printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
  1569. print_ip_sym(curr->hardirq_enable_ip);
  1570. printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
  1571. print_ip_sym(curr->hardirq_disable_ip);
  1572. printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
  1573. print_ip_sym(curr->softirq_enable_ip);
  1574. printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
  1575. print_ip_sym(curr->softirq_disable_ip);
  1576. }
  1577. static int HARDIRQ_verbose(struct lock_class *class)
  1578. {
  1579. #if HARDIRQ_VERBOSE
  1580. return class_filter(class);
  1581. #endif
  1582. return 0;
  1583. }
  1584. static int SOFTIRQ_verbose(struct lock_class *class)
  1585. {
  1586. #if SOFTIRQ_VERBOSE
  1587. return class_filter(class);
  1588. #endif
  1589. return 0;
  1590. }
  1591. static int RECLAIM_FS_verbose(struct lock_class *class)
  1592. {
  1593. #if RECLAIM_VERBOSE
  1594. return class_filter(class);
  1595. #endif
  1596. return 0;
  1597. }
  1598. #define STRICT_READ_CHECKS 1
  1599. static int (*state_verbose_f[])(struct lock_class *class) = {
  1600. #define LOCKDEP_STATE(__STATE) \
  1601. __STATE##_verbose,
  1602. #include "lockdep_states.h"
  1603. #undef LOCKDEP_STATE
  1604. };
  1605. static inline int state_verbose(enum lock_usage_bit bit,
  1606. struct lock_class *class)
  1607. {
  1608. return state_verbose_f[bit >> 2](class);
  1609. }
  1610. typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
  1611. enum lock_usage_bit bit, const char *name);
  1612. static int
  1613. mark_lock_irq(struct task_struct *curr, struct held_lock *this,
  1614. enum lock_usage_bit new_bit)
  1615. {
  1616. int excl_bit = exclusive_bit(new_bit);
  1617. int read = new_bit & 1;
  1618. int dir = new_bit & 2;
  1619. check_usage_f usage = dir ?
  1620. check_usage_backwards : check_usage_forwards;
  1621. if (!valid_state(curr, this, new_bit, excl_bit))
  1622. return 0;
  1623. if ((!read || !dir || STRICT_READ_CHECKS) &&
  1624. !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
  1625. return 0;
  1626. if (!read) {
  1627. if (!valid_state(curr, this, new_bit, excl_bit + 1))
  1628. return 0;
  1629. if (STRICT_READ_CHECKS &&
  1630. !usage(curr, this, excl_bit + 1,
  1631. state_name(new_bit + 1)))
  1632. return 0;
  1633. }
  1634. if (state_verbose(new_bit, hlock_class(this)))
  1635. return 2;
  1636. return 1;
  1637. }
  1638. enum mark_type {
  1639. #define LOCKDEP_STATE(__STATE) __STATE,
  1640. #include "lockdep_states.h"
  1641. #undef LOCKDEP_STATE
  1642. };
  1643. static int
  1644. mark_held_locks(struct task_struct *curr, enum mark_type mark)
  1645. {
  1646. enum lock_usage_bit usage_bit;
  1647. struct held_lock *hlock;
  1648. int i;
  1649. for (i = 0; i < curr->lockdep_depth; i++) {
  1650. hlock = curr->held_locks + i;
  1651. usage_bit = 2 + (mark << 2);
  1652. if (hlock->read)
  1653. usage_bit += 1;
  1654. BUG_ON(usage_bit >= LOCK_USAGE_STATES);
  1655. if (hlock_class(hlock)->key == __lockdep_no_validate__.subkeys)
  1656. continue;
  1657. if (!mark_lock(curr, hlock, usage_bit))
  1658. return 0;
  1659. }
  1660. return 1;
  1661. }
  1662. static void __trace_hardirqs_on_caller(unsigned long ip)
  1663. {
  1664. struct task_struct *curr = current;
  1665. curr->hardirqs_enabled = 1;
  1666. if (!mark_held_locks(curr, HARDIRQ))
  1667. return;
  1668. if (curr->softirqs_enabled)
  1669. if (!mark_held_locks(curr, SOFTIRQ))
  1670. return;
  1671. curr->hardirq_enable_ip = ip;
  1672. curr->hardirq_enable_event = ++curr->irq_events;
  1673. debug_atomic_inc(hardirqs_on_events);
  1674. }
  1675. void trace_hardirqs_on_caller(unsigned long ip)
  1676. {
  1677. time_hardirqs_on(CALLER_ADDR0, ip);
  1678. if (unlikely(!debug_locks || current->lockdep_recursion))
  1679. return;
  1680. if (unlikely(current->hardirqs_enabled)) {
  1681. __debug_atomic_inc(redundant_hardirqs_on);
  1682. return;
  1683. }
  1684. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  1685. return;
  1686. if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
  1687. return;
  1688. if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
  1689. return;
  1690. current->lockdep_recursion = 1;
  1691. __trace_hardirqs_on_caller(ip);
  1692. current->lockdep_recursion = 0;
  1693. }
  1694. EXPORT_SYMBOL(trace_hardirqs_on_caller);
  1695. void trace_hardirqs_on(void)
  1696. {
  1697. trace_hardirqs_on_caller(CALLER_ADDR0);
  1698. }
  1699. EXPORT_SYMBOL(trace_hardirqs_on);
  1700. void trace_hardirqs_off_caller(unsigned long ip)
  1701. {
  1702. struct task_struct *curr = current;
  1703. time_hardirqs_off(CALLER_ADDR0, ip);
  1704. if (unlikely(!debug_locks || current->lockdep_recursion))
  1705. return;
  1706. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  1707. return;
  1708. if (curr->hardirqs_enabled) {
  1709. curr->hardirqs_enabled =

Large files files are truncated, but you can click here to view the full file