aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/lockdep.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/lockdep.c')
-rw-r--r--kernel/lockdep.c555
1 files changed, 290 insertions, 265 deletions
diff --git a/kernel/lockdep.c b/kernel/lockdep.c
index 06b0c3568f0b..81b5f33970b8 100644
--- a/kernel/lockdep.c
+++ b/kernel/lockdep.c
@@ -41,6 +41,8 @@
41#include <linux/utsname.h> 41#include <linux/utsname.h>
42#include <linux/hash.h> 42#include <linux/hash.h>
43#include <linux/ftrace.h> 43#include <linux/ftrace.h>
44#include <linux/stringify.h>
45#include <trace/lockdep.h>
44 46
45#include <asm/sections.h> 47#include <asm/sections.h>
46 48
@@ -310,12 +312,14 @@ EXPORT_SYMBOL(lockdep_on);
310#if VERBOSE 312#if VERBOSE
311# define HARDIRQ_VERBOSE 1 313# define HARDIRQ_VERBOSE 1
312# define SOFTIRQ_VERBOSE 1 314# define SOFTIRQ_VERBOSE 1
315# define RECLAIM_VERBOSE 1
313#else 316#else
314# define HARDIRQ_VERBOSE 0 317# define HARDIRQ_VERBOSE 0
315# define SOFTIRQ_VERBOSE 0 318# define SOFTIRQ_VERBOSE 0
319# define RECLAIM_VERBOSE 0
316#endif 320#endif
317 321
318#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE 322#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
319/* 323/*
320 * Quick filtering for interesting events: 324 * Quick filtering for interesting events:
321 */ 325 */
@@ -430,30 +434,24 @@ atomic_t nr_find_usage_forwards_checks;
430atomic_t nr_find_usage_forwards_recursions; 434atomic_t nr_find_usage_forwards_recursions;
431atomic_t nr_find_usage_backwards_checks; 435atomic_t nr_find_usage_backwards_checks;
432atomic_t nr_find_usage_backwards_recursions; 436atomic_t nr_find_usage_backwards_recursions;
433# define debug_atomic_inc(ptr) atomic_inc(ptr)
434# define debug_atomic_dec(ptr) atomic_dec(ptr)
435# define debug_atomic_read(ptr) atomic_read(ptr)
436#else
437# define debug_atomic_inc(ptr) do { } while (0)
438# define debug_atomic_dec(ptr) do { } while (0)
439# define debug_atomic_read(ptr) 0
440#endif 437#endif
441 438
442/* 439/*
443 * Locking printouts: 440 * Locking printouts:
444 */ 441 */
445 442
443#define __USAGE(__STATE) \
444 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
445 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
446 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
447 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
448
446static const char *usage_str[] = 449static const char *usage_str[] =
447{ 450{
448 [LOCK_USED] = "initial-use ", 451#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
449 [LOCK_USED_IN_HARDIRQ] = "in-hardirq-W", 452#include "lockdep_states.h"
450 [LOCK_USED_IN_SOFTIRQ] = "in-softirq-W", 453#undef LOCKDEP_STATE
451 [LOCK_ENABLED_SOFTIRQS] = "softirq-on-W", 454 [LOCK_USED] = "INITIAL USE",
452 [LOCK_ENABLED_HARDIRQS] = "hardirq-on-W",
453 [LOCK_USED_IN_HARDIRQ_READ] = "in-hardirq-R",
454 [LOCK_USED_IN_SOFTIRQ_READ] = "in-softirq-R",
455 [LOCK_ENABLED_SOFTIRQS_READ] = "softirq-on-R",
456 [LOCK_ENABLED_HARDIRQS_READ] = "hardirq-on-R",
457}; 455};
458 456
459const char * __get_key_name(struct lockdep_subclass_key *key, char *str) 457const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
@@ -461,46 +459,45 @@ const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
461 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str); 459 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
462} 460}
463 461
464void 462static inline unsigned long lock_flag(enum lock_usage_bit bit)
465get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, char *c4)
466{ 463{
467 *c1 = '.', *c2 = '.', *c3 = '.', *c4 = '.'; 464 return 1UL << bit;
468 465}
469 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
470 *c1 = '+';
471 else
472 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
473 *c1 = '-';
474 466
475 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ) 467static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
476 *c2 = '+'; 468{
477 else 469 char c = '.';
478 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
479 *c2 = '-';
480 470
481 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ) 471 if (class->usage_mask & lock_flag(bit + 2))
482 *c3 = '-'; 472 c = '+';
483 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) { 473 if (class->usage_mask & lock_flag(bit)) {
484 *c3 = '+'; 474 c = '-';
485 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ) 475 if (class->usage_mask & lock_flag(bit + 2))
486 *c3 = '?'; 476 c = '?';
487 } 477 }
488 478
489 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ) 479 return c;
490 *c4 = '-'; 480}
491 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) { 481
492 *c4 = '+'; 482void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
493 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ) 483{
494 *c4 = '?'; 484 int i = 0;
495 } 485
486#define LOCKDEP_STATE(__STATE) \
487 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
488 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
489#include "lockdep_states.h"
490#undef LOCKDEP_STATE
491
492 usage[i] = '\0';
496} 493}
497 494
498static void print_lock_name(struct lock_class *class) 495static void print_lock_name(struct lock_class *class)
499{ 496{
500 char str[KSYM_NAME_LEN], c1, c2, c3, c4; 497 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
501 const char *name; 498 const char *name;
502 499
503 get_usage_chars(class, &c1, &c2, &c3, &c4); 500 get_usage_chars(class, usage);
504 501
505 name = class->name; 502 name = class->name;
506 if (!name) { 503 if (!name) {
@@ -513,7 +510,7 @@ static void print_lock_name(struct lock_class *class)
513 if (class->subclass) 510 if (class->subclass)
514 printk("/%d", class->subclass); 511 printk("/%d", class->subclass);
515 } 512 }
516 printk("){%c%c%c%c}", c1, c2, c3, c4); 513 printk("){%s}", usage);
517} 514}
518 515
519static void print_lockdep_cache(struct lockdep_map *lock) 516static void print_lockdep_cache(struct lockdep_map *lock)
@@ -1263,9 +1260,49 @@ check_usage(struct task_struct *curr, struct held_lock *prev,
1263 bit_backwards, bit_forwards, irqclass); 1260 bit_backwards, bit_forwards, irqclass);
1264} 1261}
1265 1262
1266static int 1263static const char *state_names[] = {
1267check_prev_add_irq(struct task_struct *curr, struct held_lock *prev, 1264#define LOCKDEP_STATE(__STATE) \
1268 struct held_lock *next) 1265 __stringify(__STATE),
1266#include "lockdep_states.h"
1267#undef LOCKDEP_STATE
1268};
1269
1270static const char *state_rnames[] = {
1271#define LOCKDEP_STATE(__STATE) \
1272 __stringify(__STATE)"-READ",
1273#include "lockdep_states.h"
1274#undef LOCKDEP_STATE
1275};
1276
1277static inline const char *state_name(enum lock_usage_bit bit)
1278{
1279 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1280}
1281
1282static int exclusive_bit(int new_bit)
1283{
1284 /*
1285 * USED_IN
1286 * USED_IN_READ
1287 * ENABLED
1288 * ENABLED_READ
1289 *
1290 * bit 0 - write/read
1291 * bit 1 - used_in/enabled
1292 * bit 2+ state
1293 */
1294
1295 int state = new_bit & ~3;
1296 int dir = new_bit & 2;
1297
1298 /*
1299 * keep state, bit flip the direction and strip read.
1300 */
1301 return state | (dir ^ 2);
1302}
1303
1304static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1305 struct held_lock *next, enum lock_usage_bit bit)
1269{ 1306{
1270 /* 1307 /*
1271 * Prove that the new dependency does not connect a hardirq-safe 1308 * Prove that the new dependency does not connect a hardirq-safe
@@ -1273,38 +1310,34 @@ check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1273 * the backwards-subgraph starting at <prev>, and the 1310 * the backwards-subgraph starting at <prev>, and the
1274 * forwards-subgraph starting at <next>: 1311 * forwards-subgraph starting at <next>:
1275 */ 1312 */
1276 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ, 1313 if (!check_usage(curr, prev, next, bit,
1277 LOCK_ENABLED_HARDIRQS, "hard")) 1314 exclusive_bit(bit), state_name(bit)))
1278 return 0; 1315 return 0;
1279 1316
1317 bit++; /* _READ */
1318
1280 /* 1319 /*
1281 * Prove that the new dependency does not connect a hardirq-safe-read 1320 * Prove that the new dependency does not connect a hardirq-safe-read
1282 * lock with a hardirq-unsafe lock - to achieve this we search 1321 * lock with a hardirq-unsafe lock - to achieve this we search
1283 * the backwards-subgraph starting at <prev>, and the 1322 * the backwards-subgraph starting at <prev>, and the
1284 * forwards-subgraph starting at <next>: 1323 * forwards-subgraph starting at <next>:
1285 */ 1324 */
1286 if (!check_usage(curr, prev, next, LOCK_USED_IN_HARDIRQ_READ, 1325 if (!check_usage(curr, prev, next, bit,
1287 LOCK_ENABLED_HARDIRQS, "hard-read")) 1326 exclusive_bit(bit), state_name(bit)))
1288 return 0; 1327 return 0;
1289 1328
1290 /* 1329 return 1;
1291 * Prove that the new dependency does not connect a softirq-safe 1330}
1292 * lock with a softirq-unsafe lock - to achieve this we search 1331
1293 * the backwards-subgraph starting at <prev>, and the 1332static int
1294 * forwards-subgraph starting at <next>: 1333check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1295 */ 1334 struct held_lock *next)
1296 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ, 1335{
1297 LOCK_ENABLED_SOFTIRQS, "soft")) 1336#define LOCKDEP_STATE(__STATE) \
1298 return 0; 1337 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1299 /*
1300 * Prove that the new dependency does not connect a softirq-safe-read
1301 * lock with a softirq-unsafe lock - to achieve this we search
1302 * the backwards-subgraph starting at <prev>, and the
1303 * forwards-subgraph starting at <next>:
1304 */
1305 if (!check_usage(curr, prev, next, LOCK_USED_IN_SOFTIRQ_READ,
1306 LOCK_ENABLED_SOFTIRQS, "soft"))
1307 return 0; 1338 return 0;
1339#include "lockdep_states.h"
1340#undef LOCKDEP_STATE
1308 1341
1309 return 1; 1342 return 1;
1310} 1343}
@@ -1861,9 +1894,9 @@ print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
1861 curr->comm, task_pid_nr(curr)); 1894 curr->comm, task_pid_nr(curr));
1862 print_lock(this); 1895 print_lock(this);
1863 if (forwards) 1896 if (forwards)
1864 printk("but this lock took another, %s-irq-unsafe lock in the past:\n", irqclass); 1897 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
1865 else 1898 else
1866 printk("but this lock was taken by another, %s-irq-safe lock in the past:\n", irqclass); 1899 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
1867 print_lock_name(other); 1900 print_lock_name(other);
1868 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n"); 1901 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
1869 1902
@@ -1933,7 +1966,7 @@ void print_irqtrace_events(struct task_struct *curr)
1933 print_ip_sym(curr->softirq_disable_ip); 1966 print_ip_sym(curr->softirq_disable_ip);
1934} 1967}
1935 1968
1936static int hardirq_verbose(struct lock_class *class) 1969static int HARDIRQ_verbose(struct lock_class *class)
1937{ 1970{
1938#if HARDIRQ_VERBOSE 1971#if HARDIRQ_VERBOSE
1939 return class_filter(class); 1972 return class_filter(class);
@@ -1941,7 +1974,7 @@ static int hardirq_verbose(struct lock_class *class)
1941 return 0; 1974 return 0;
1942} 1975}
1943 1976
1944static int softirq_verbose(struct lock_class *class) 1977static int SOFTIRQ_verbose(struct lock_class *class)
1945{ 1978{
1946#if SOFTIRQ_VERBOSE 1979#if SOFTIRQ_VERBOSE
1947 return class_filter(class); 1980 return class_filter(class);
@@ -1949,185 +1982,95 @@ static int softirq_verbose(struct lock_class *class)
1949 return 0; 1982 return 0;
1950} 1983}
1951 1984
1985static int RECLAIM_FS_verbose(struct lock_class *class)
1986{
1987#if RECLAIM_VERBOSE
1988 return class_filter(class);
1989#endif
1990 return 0;
1991}
1992
1952#define STRICT_READ_CHECKS 1 1993#define STRICT_READ_CHECKS 1
1953 1994
1954static int mark_lock_irq(struct task_struct *curr, struct held_lock *this, 1995static int (*state_verbose_f[])(struct lock_class *class) = {
1996#define LOCKDEP_STATE(__STATE) \
1997 __STATE##_verbose,
1998#include "lockdep_states.h"
1999#undef LOCKDEP_STATE
2000};
2001
2002static inline int state_verbose(enum lock_usage_bit bit,
2003 struct lock_class *class)
2004{
2005 return state_verbose_f[bit >> 2](class);
2006}
2007
2008typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2009 enum lock_usage_bit bit, const char *name);
2010
2011static int
2012mark_lock_irq(struct task_struct *curr, struct held_lock *this,
1955 enum lock_usage_bit new_bit) 2013 enum lock_usage_bit new_bit)
1956{ 2014{
1957 int ret = 1; 2015 int excl_bit = exclusive_bit(new_bit);
2016 int read = new_bit & 1;
2017 int dir = new_bit & 2;
1958 2018
1959 switch(new_bit) { 2019 /*
1960 case LOCK_USED_IN_HARDIRQ: 2020 * mark USED_IN has to look forwards -- to ensure no dependency
1961 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS)) 2021 * has ENABLED state, which would allow recursion deadlocks.
1962 return 0; 2022 *
1963 if (!valid_state(curr, this, new_bit, 2023 * mark ENABLED has to look backwards -- to ensure no dependee
1964 LOCK_ENABLED_HARDIRQS_READ)) 2024 * has USED_IN state, which, again, would allow recursion deadlocks.
1965 return 0; 2025 */
1966 /* 2026 check_usage_f usage = dir ?
1967 * just marked it hardirq-safe, check that this lock 2027 check_usage_backwards : check_usage_forwards;
1968 * took no hardirq-unsafe lock in the past: 2028
1969 */ 2029 /*
1970 if (!check_usage_forwards(curr, this, 2030 * Validate that this particular lock does not have conflicting
1971 LOCK_ENABLED_HARDIRQS, "hard")) 2031 * usage states.
1972 return 0; 2032 */
1973#if STRICT_READ_CHECKS 2033 if (!valid_state(curr, this, new_bit, excl_bit))
1974 /* 2034 return 0;
1975 * just marked it hardirq-safe, check that this lock 2035
1976 * took no hardirq-unsafe-read lock in the past: 2036 /*
1977 */ 2037 * Validate that the lock dependencies don't have conflicting usage
1978 if (!check_usage_forwards(curr, this, 2038 * states.
1979 LOCK_ENABLED_HARDIRQS_READ, "hard-read")) 2039 */
1980 return 0; 2040 if ((!read || !dir || STRICT_READ_CHECKS) &&
1981#endif 2041 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
1982 if (hardirq_verbose(hlock_class(this))) 2042 return 0;
1983 ret = 2; 2043
1984 break; 2044 /*
1985 case LOCK_USED_IN_SOFTIRQ: 2045 * Check for read in write conflicts
1986 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS)) 2046 */
1987 return 0; 2047 if (!read) {
1988 if (!valid_state(curr, this, new_bit, 2048 if (!valid_state(curr, this, new_bit, excl_bit + 1))
1989 LOCK_ENABLED_SOFTIRQS_READ))
1990 return 0;
1991 /*
1992 * just marked it softirq-safe, check that this lock
1993 * took no softirq-unsafe lock in the past:
1994 */
1995 if (!check_usage_forwards(curr, this,
1996 LOCK_ENABLED_SOFTIRQS, "soft"))
1997 return 0;
1998#if STRICT_READ_CHECKS
1999 /*
2000 * just marked it softirq-safe, check that this lock
2001 * took no softirq-unsafe-read lock in the past:
2002 */
2003 if (!check_usage_forwards(curr, this,
2004 LOCK_ENABLED_SOFTIRQS_READ, "soft-read"))
2005 return 0;
2006#endif
2007 if (softirq_verbose(hlock_class(this)))
2008 ret = 2;
2009 break;
2010 case LOCK_USED_IN_HARDIRQ_READ:
2011 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_HARDIRQS))
2012 return 0;
2013 /*
2014 * just marked it hardirq-read-safe, check that this lock
2015 * took no hardirq-unsafe lock in the past:
2016 */
2017 if (!check_usage_forwards(curr, this,
2018 LOCK_ENABLED_HARDIRQS, "hard"))
2019 return 0;
2020 if (hardirq_verbose(hlock_class(this)))
2021 ret = 2;
2022 break;
2023 case LOCK_USED_IN_SOFTIRQ_READ:
2024 if (!valid_state(curr, this, new_bit, LOCK_ENABLED_SOFTIRQS))
2025 return 0;
2026 /*
2027 * just marked it softirq-read-safe, check that this lock
2028 * took no softirq-unsafe lock in the past:
2029 */
2030 if (!check_usage_forwards(curr, this,
2031 LOCK_ENABLED_SOFTIRQS, "soft"))
2032 return 0;
2033 if (softirq_verbose(hlock_class(this)))
2034 ret = 2;
2035 break;
2036 case LOCK_ENABLED_HARDIRQS:
2037 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
2038 return 0;
2039 if (!valid_state(curr, this, new_bit,
2040 LOCK_USED_IN_HARDIRQ_READ))
2041 return 0;
2042 /*
2043 * just marked it hardirq-unsafe, check that no hardirq-safe
2044 * lock in the system ever took it in the past:
2045 */
2046 if (!check_usage_backwards(curr, this,
2047 LOCK_USED_IN_HARDIRQ, "hard"))
2048 return 0;
2049#if STRICT_READ_CHECKS
2050 /*
2051 * just marked it hardirq-unsafe, check that no
2052 * hardirq-safe-read lock in the system ever took
2053 * it in the past:
2054 */
2055 if (!check_usage_backwards(curr, this,
2056 LOCK_USED_IN_HARDIRQ_READ, "hard-read"))
2057 return 0;
2058#endif
2059 if (hardirq_verbose(hlock_class(this)))
2060 ret = 2;
2061 break;
2062 case LOCK_ENABLED_SOFTIRQS:
2063 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
2064 return 0;
2065 if (!valid_state(curr, this, new_bit,
2066 LOCK_USED_IN_SOFTIRQ_READ))
2067 return 0;
2068 /*
2069 * just marked it softirq-unsafe, check that no softirq-safe
2070 * lock in the system ever took it in the past:
2071 */
2072 if (!check_usage_backwards(curr, this,
2073 LOCK_USED_IN_SOFTIRQ, "soft"))
2074 return 0;
2075#if STRICT_READ_CHECKS
2076 /*
2077 * just marked it softirq-unsafe, check that no
2078 * softirq-safe-read lock in the system ever took
2079 * it in the past:
2080 */
2081 if (!check_usage_backwards(curr, this,
2082 LOCK_USED_IN_SOFTIRQ_READ, "soft-read"))
2083 return 0;
2084#endif
2085 if (softirq_verbose(hlock_class(this)))
2086 ret = 2;
2087 break;
2088 case LOCK_ENABLED_HARDIRQS_READ:
2089 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_HARDIRQ))
2090 return 0;
2091#if STRICT_READ_CHECKS
2092 /*
2093 * just marked it hardirq-read-unsafe, check that no
2094 * hardirq-safe lock in the system ever took it in the past:
2095 */
2096 if (!check_usage_backwards(curr, this,
2097 LOCK_USED_IN_HARDIRQ, "hard"))
2098 return 0;
2099#endif
2100 if (hardirq_verbose(hlock_class(this)))
2101 ret = 2;
2102 break;
2103 case LOCK_ENABLED_SOFTIRQS_READ:
2104 if (!valid_state(curr, this, new_bit, LOCK_USED_IN_SOFTIRQ))
2105 return 0; 2049 return 0;
2106#if STRICT_READ_CHECKS 2050
2107 /* 2051 if (STRICT_READ_CHECKS &&
2108 * just marked it softirq-read-unsafe, check that no 2052 !usage(curr, this, excl_bit + 1,
2109 * softirq-safe lock in the system ever took it in the past: 2053 state_name(new_bit + 1)))
2110 */
2111 if (!check_usage_backwards(curr, this,
2112 LOCK_USED_IN_SOFTIRQ, "soft"))
2113 return 0; 2054 return 0;
2114#endif
2115 if (softirq_verbose(hlock_class(this)))
2116 ret = 2;
2117 break;
2118 default:
2119 WARN_ON(1);
2120 break;
2121 } 2055 }
2122 2056
2123 return ret; 2057 if (state_verbose(new_bit, hlock_class(this)))
2058 return 2;
2059
2060 return 1;
2124} 2061}
2125 2062
2063enum mark_type {
2064#define LOCKDEP_STATE(__STATE) __STATE,
2065#include "lockdep_states.h"
2066#undef LOCKDEP_STATE
2067};
2068
2126/* 2069/*
2127 * Mark all held locks with a usage bit: 2070 * Mark all held locks with a usage bit:
2128 */ 2071 */
2129static int 2072static int
2130mark_held_locks(struct task_struct *curr, int hardirq) 2073mark_held_locks(struct task_struct *curr, enum mark_type mark)
2131{ 2074{
2132 enum lock_usage_bit usage_bit; 2075 enum lock_usage_bit usage_bit;
2133 struct held_lock *hlock; 2076 struct held_lock *hlock;
@@ -2136,17 +2079,12 @@ mark_held_locks(struct task_struct *curr, int hardirq)
2136 for (i = 0; i < curr->lockdep_depth; i++) { 2079 for (i = 0; i < curr->lockdep_depth; i++) {
2137 hlock = curr->held_locks + i; 2080 hlock = curr->held_locks + i;
2138 2081
2139 if (hardirq) { 2082 usage_bit = 2 + (mark << 2); /* ENABLED */
2140 if (hlock->read) 2083 if (hlock->read)
2141 usage_bit = LOCK_ENABLED_HARDIRQS_READ; 2084 usage_bit += 1; /* READ */
2142 else 2085
2143 usage_bit = LOCK_ENABLED_HARDIRQS; 2086 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2144 } else { 2087
2145 if (hlock->read)
2146 usage_bit = LOCK_ENABLED_SOFTIRQS_READ;
2147 else
2148 usage_bit = LOCK_ENABLED_SOFTIRQS;
2149 }
2150 if (!mark_lock(curr, hlock, usage_bit)) 2088 if (!mark_lock(curr, hlock, usage_bit))
2151 return 0; 2089 return 0;
2152 } 2090 }
@@ -2200,7 +2138,7 @@ void trace_hardirqs_on_caller(unsigned long ip)
2200 * We are going to turn hardirqs on, so set the 2138 * We are going to turn hardirqs on, so set the
2201 * usage bit for all held locks: 2139 * usage bit for all held locks:
2202 */ 2140 */
2203 if (!mark_held_locks(curr, 1)) 2141 if (!mark_held_locks(curr, HARDIRQ))
2204 return; 2142 return;
2205 /* 2143 /*
2206 * If we have softirqs enabled, then set the usage 2144 * If we have softirqs enabled, then set the usage
@@ -2208,7 +2146,7 @@ void trace_hardirqs_on_caller(unsigned long ip)
2208 * this bit from being set before) 2146 * this bit from being set before)
2209 */ 2147 */
2210 if (curr->softirqs_enabled) 2148 if (curr->softirqs_enabled)
2211 if (!mark_held_locks(curr, 0)) 2149 if (!mark_held_locks(curr, SOFTIRQ))
2212 return; 2150 return;
2213 2151
2214 curr->hardirq_enable_ip = ip; 2152 curr->hardirq_enable_ip = ip;
@@ -2288,7 +2226,7 @@ void trace_softirqs_on(unsigned long ip)
2288 * enabled too: 2226 * enabled too:
2289 */ 2227 */
2290 if (curr->hardirqs_enabled) 2228 if (curr->hardirqs_enabled)
2291 mark_held_locks(curr, 0); 2229 mark_held_locks(curr, SOFTIRQ);
2292} 2230}
2293 2231
2294/* 2232/*
@@ -2317,6 +2255,48 @@ void trace_softirqs_off(unsigned long ip)
2317 debug_atomic_inc(&redundant_softirqs_off); 2255 debug_atomic_inc(&redundant_softirqs_off);
2318} 2256}
2319 2257
2258static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
2259{
2260 struct task_struct *curr = current;
2261
2262 if (unlikely(!debug_locks))
2263 return;
2264
2265 /* no reclaim without waiting on it */
2266 if (!(gfp_mask & __GFP_WAIT))
2267 return;
2268
2269 /* this guy won't enter reclaim */
2270 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2271 return;
2272
2273 /* We're only interested __GFP_FS allocations for now */
2274 if (!(gfp_mask & __GFP_FS))
2275 return;
2276
2277 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
2278 return;
2279
2280 mark_held_locks(curr, RECLAIM_FS);
2281}
2282
2283static void check_flags(unsigned long flags);
2284
2285void lockdep_trace_alloc(gfp_t gfp_mask)
2286{
2287 unsigned long flags;
2288
2289 if (unlikely(current->lockdep_recursion))
2290 return;
2291
2292 raw_local_irq_save(flags);
2293 check_flags(flags);
2294 current->lockdep_recursion = 1;
2295 __lockdep_trace_alloc(gfp_mask, flags);
2296 current->lockdep_recursion = 0;
2297 raw_local_irq_restore(flags);
2298}
2299
2320static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock) 2300static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2321{ 2301{
2322 /* 2302 /*
@@ -2345,19 +2325,35 @@ static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2345 if (!hlock->hardirqs_off) { 2325 if (!hlock->hardirqs_off) {
2346 if (hlock->read) { 2326 if (hlock->read) {
2347 if (!mark_lock(curr, hlock, 2327 if (!mark_lock(curr, hlock,
2348 LOCK_ENABLED_HARDIRQS_READ)) 2328 LOCK_ENABLED_HARDIRQ_READ))
2349 return 0; 2329 return 0;
2350 if (curr->softirqs_enabled) 2330 if (curr->softirqs_enabled)
2351 if (!mark_lock(curr, hlock, 2331 if (!mark_lock(curr, hlock,
2352 LOCK_ENABLED_SOFTIRQS_READ)) 2332 LOCK_ENABLED_SOFTIRQ_READ))
2353 return 0; 2333 return 0;
2354 } else { 2334 } else {
2355 if (!mark_lock(curr, hlock, 2335 if (!mark_lock(curr, hlock,
2356 LOCK_ENABLED_HARDIRQS)) 2336 LOCK_ENABLED_HARDIRQ))
2357 return 0; 2337 return 0;
2358 if (curr->softirqs_enabled) 2338 if (curr->softirqs_enabled)
2359 if (!mark_lock(curr, hlock, 2339 if (!mark_lock(curr, hlock,
2360 LOCK_ENABLED_SOFTIRQS)) 2340 LOCK_ENABLED_SOFTIRQ))
2341 return 0;
2342 }
2343 }
2344
2345 /*
2346 * We reuse the irq context infrastructure more broadly as a general
2347 * context checking code. This tests GFP_FS recursion (a lock taken
2348 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2349 * allocation).
2350 */
2351 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2352 if (hlock->read) {
2353 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2354 return 0;
2355 } else {
2356 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2361 return 0; 2357 return 0;
2362 } 2358 }
2363 } 2359 }
@@ -2412,6 +2408,10 @@ static inline int separate_irq_context(struct task_struct *curr,
2412 return 0; 2408 return 0;
2413} 2409}
2414 2410
2411void lockdep_trace_alloc(gfp_t gfp_mask)
2412{
2413}
2414
2415#endif 2415#endif
2416 2416
2417/* 2417/*
@@ -2445,14 +2445,13 @@ static int mark_lock(struct task_struct *curr, struct held_lock *this,
2445 return 0; 2445 return 0;
2446 2446
2447 switch (new_bit) { 2447 switch (new_bit) {
2448 case LOCK_USED_IN_HARDIRQ: 2448#define LOCKDEP_STATE(__STATE) \
2449 case LOCK_USED_IN_SOFTIRQ: 2449 case LOCK_USED_IN_##__STATE: \
2450 case LOCK_USED_IN_HARDIRQ_READ: 2450 case LOCK_USED_IN_##__STATE##_READ: \
2451 case LOCK_USED_IN_SOFTIRQ_READ: 2451 case LOCK_ENABLED_##__STATE: \
2452 case LOCK_ENABLED_HARDIRQS: 2452 case LOCK_ENABLED_##__STATE##_READ:
2453 case LOCK_ENABLED_SOFTIRQS: 2453#include "lockdep_states.h"
2454 case LOCK_ENABLED_HARDIRQS_READ: 2454#undef LOCKDEP_STATE
2455 case LOCK_ENABLED_SOFTIRQS_READ:
2456 ret = mark_lock_irq(curr, this, new_bit); 2455 ret = mark_lock_irq(curr, this, new_bit);
2457 if (!ret) 2456 if (!ret)
2458 return 0; 2457 return 0;
@@ -2925,6 +2924,8 @@ void lock_set_class(struct lockdep_map *lock, const char *name,
2925} 2924}
2926EXPORT_SYMBOL_GPL(lock_set_class); 2925EXPORT_SYMBOL_GPL(lock_set_class);
2927 2926
2927DEFINE_TRACE(lock_acquire);
2928
2928/* 2929/*
2929 * We are not always called with irqs disabled - do that here, 2930 * We are not always called with irqs disabled - do that here,
2930 * and also avoid lockdep recursion: 2931 * and also avoid lockdep recursion:
@@ -2935,6 +2936,8 @@ void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2935{ 2936{
2936 unsigned long flags; 2937 unsigned long flags;
2937 2938
2939 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
2940
2938 if (unlikely(current->lockdep_recursion)) 2941 if (unlikely(current->lockdep_recursion))
2939 return; 2942 return;
2940 2943
@@ -2949,11 +2952,15 @@ void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2949} 2952}
2950EXPORT_SYMBOL_GPL(lock_acquire); 2953EXPORT_SYMBOL_GPL(lock_acquire);
2951 2954
2955DEFINE_TRACE(lock_release);
2956
2952void lock_release(struct lockdep_map *lock, int nested, 2957void lock_release(struct lockdep_map *lock, int nested,
2953 unsigned long ip) 2958 unsigned long ip)
2954{ 2959{
2955 unsigned long flags; 2960 unsigned long flags;
2956 2961
2962 trace_lock_release(lock, nested, ip);
2963
2957 if (unlikely(current->lockdep_recursion)) 2964 if (unlikely(current->lockdep_recursion))
2958 return; 2965 return;
2959 2966
@@ -2966,6 +2973,16 @@ void lock_release(struct lockdep_map *lock, int nested,
2966} 2973}
2967EXPORT_SYMBOL_GPL(lock_release); 2974EXPORT_SYMBOL_GPL(lock_release);
2968 2975
2976void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
2977{
2978 current->lockdep_reclaim_gfp = gfp_mask;
2979}
2980
2981void lockdep_clear_current_reclaim_state(void)
2982{
2983 current->lockdep_reclaim_gfp = 0;
2984}
2985
2969#ifdef CONFIG_LOCK_STAT 2986#ifdef CONFIG_LOCK_STAT
2970static int 2987static int
2971print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock, 2988print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
@@ -3092,10 +3109,14 @@ found_it:
3092 lock->ip = ip; 3109 lock->ip = ip;
3093} 3110}
3094 3111
3112DEFINE_TRACE(lock_contended);
3113
3095void lock_contended(struct lockdep_map *lock, unsigned long ip) 3114void lock_contended(struct lockdep_map *lock, unsigned long ip)
3096{ 3115{
3097 unsigned long flags; 3116 unsigned long flags;
3098 3117
3118 trace_lock_contended(lock, ip);
3119
3099 if (unlikely(!lock_stat)) 3120 if (unlikely(!lock_stat))
3100 return; 3121 return;
3101 3122
@@ -3111,10 +3132,14 @@ void lock_contended(struct lockdep_map *lock, unsigned long ip)
3111} 3132}
3112EXPORT_SYMBOL_GPL(lock_contended); 3133EXPORT_SYMBOL_GPL(lock_contended);
3113 3134
3135DEFINE_TRACE(lock_acquired);
3136
3114void lock_acquired(struct lockdep_map *lock, unsigned long ip) 3137void lock_acquired(struct lockdep_map *lock, unsigned long ip)
3115{ 3138{
3116 unsigned long flags; 3139 unsigned long flags;
3117 3140
3141 trace_lock_acquired(lock, ip);
3142
3118 if (unlikely(!lock_stat)) 3143 if (unlikely(!lock_stat))
3119 return; 3144 return;
3120 3145