[PATCH] pids: coding style: use struct pidmap
[cascardo/linux.git] / kernel / lockdep.c
index c1f34ad..e596525 100644 (file)
@@ -36,6 +36,7 @@
 #include <linux/stacktrace.h>
 #include <linux/debug_locks.h>
 #include <linux/irqflags.h>
+#include <linux/utsname.h>
 
 #include <asm/sections.h>
 
@@ -121,8 +122,8 @@ static struct list_head chainhash_table[CHAINHASH_SIZE];
  * unique.
  */
 #define iterate_chain_key(key1, key2) \
-       (((key1) << MAX_LOCKDEP_KEYS_BITS/2) ^ \
-       ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS/2)) ^ \
+       (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
+       ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
        (key2))
 
 void lockdep_off(void)
@@ -224,7 +225,14 @@ static int save_trace(struct stack_trace *trace)
        trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
        trace->entries = stack_trace + nr_stack_trace_entries;
 
-       save_stack_trace(trace, NULL, 0, 3);
+       trace->skip = 3;
+       trace->all_contexts = 0;
+
+       /* Make sure to not recurse in case the the unwinder needs to tak
+e         locks. */
+       lockdep_off();
+       save_stack_trace(trace, NULL);
+       lockdep_on();
 
        trace->max_entries = trace->nr_entries;
 
@@ -508,6 +516,13 @@ print_circular_bug_entry(struct lock_list *target, unsigned int depth)
        return 0;
 }
 
+static void print_kernel_version(void)
+{
+       printk("%s %.*s\n", system_utsname.release,
+               (int)strcspn(system_utsname.version, " "),
+               system_utsname.version);
+}
+
 /*
  * When a circular dependency is detected, print the
  * header first:
@@ -524,6 +539,7 @@ print_circular_bug_header(struct lock_list *entry, unsigned int depth)
 
        printk("\n=======================================================\n");
        printk(  "[ INFO: possible circular locking dependency detected ]\n");
+       print_kernel_version();
        printk(  "-------------------------------------------------------\n");
        printk("%s/%d is trying to acquire lock:\n",
                curr->comm, curr->pid);
@@ -705,6 +721,7 @@ print_bad_irq_dependency(struct task_struct *curr,
        printk("\n======================================================\n");
        printk(  "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
                irqclass, irqclass);
+       print_kernel_version();
        printk(  "------------------------------------------------------\n");
        printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
                curr->comm, curr->pid,
@@ -786,6 +803,7 @@ print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
 
        printk("\n=============================================\n");
        printk(  "[ INFO: possible recursive locking detected ]\n");
+       print_kernel_version();
        printk(  "---------------------------------------------\n");
        printk("%s/%d is trying to acquire lock:\n",
                curr->comm, curr->pid);
@@ -1104,7 +1122,7 @@ extern void __error_too_big_MAX_LOCKDEP_SUBCLASSES(void);
  * itself, so actual lookup of the hash should be once per lock object.
  */
 static inline struct lock_class *
-register_lock_class(struct lockdep_map *lock, unsigned int subclass)
+look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
 {
        struct lockdep_subclass_key *key;
        struct list_head *hash_head;
@@ -1148,7 +1166,26 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass)
         */
        list_for_each_entry(class, hash_head, hash_entry)
                if (class->key == key)
-                       goto out_set;
+                       return class;
+
+       return NULL;
+}
+
+/*
+ * Register a lock's class in the hash-table, if the class is not present
+ * yet. Otherwise we look it up. We cache the result in the lock object
+ * itself, so actual lookup of the hash should be once per lock object.
+ */
+static inline struct lock_class *
+register_lock_class(struct lockdep_map *lock, unsigned int subclass)
+{
+       struct lockdep_subclass_key *key;
+       struct list_head *hash_head;
+       struct lock_class *class;
+
+       class = look_up_lock_class(lock, subclass);
+       if (likely(class))
+               return class;
 
        /*
         * Debug-check: all keys must be persistent!
@@ -1163,6 +1200,9 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass)
                return NULL;
        }
 
+       key = lock->key->subkeys + subclass;
+       hash_head = classhashentry(key);
+
        __raw_spin_lock(&hash_lock);
        /*
         * We have to do the hash-walk again, to avoid races
@@ -1209,8 +1249,8 @@ register_lock_class(struct lockdep_map *lock, unsigned int subclass)
 out_unlock_set:
        __raw_spin_unlock(&hash_lock);
 
-out_set:
-       lock->class[subclass] = class;
+       if (!subclass)
+               lock->class_cache = class;
 
        DEBUG_LOCKS_WARN_ON(class->subclass != subclass);
 
@@ -1346,6 +1386,7 @@ print_irq_inversion_bug(struct task_struct *curr, struct lock_class *other,
 
        printk("\n=========================================================\n");
        printk(  "[ INFO: possible irq lock inversion dependency detected ]\n");
+       print_kernel_version();
        printk(  "---------------------------------------------------------\n");
        printk("%s/%d just changed the state of lock:\n",
                curr->comm, curr->pid);
@@ -1440,6 +1481,7 @@ print_usage_bug(struct task_struct *curr, struct held_lock *this,
 
        printk("\n=================================\n");
        printk(  "[ INFO: inconsistent lock state ]\n");
+       print_kernel_version();
        printk(  "---------------------------------\n");
 
        printk("inconsistent {%s} -> {%s} usage.\n",
@@ -1914,7 +1956,7 @@ void lockdep_init_map(struct lockdep_map *lock, const char *name,
        }
        lock->name = name;
        lock->key = key;
-       memset(lock->class, 0, sizeof(lock->class[0])*MAX_LOCKDEP_SUBCLASSES);
+       lock->class_cache = NULL;
 }
 
 EXPORT_SYMBOL_GPL(lockdep_init_map);
@@ -1928,8 +1970,8 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
                          unsigned long ip)
 {
        struct task_struct *curr = current;
+       struct lock_class *class = NULL;
        struct held_lock *hlock;
-       struct lock_class *class;
        unsigned int depth, id;
        int chain_head = 0;
        u64 chain_key;
@@ -1947,8 +1989,11 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
                return 0;
        }
 
-       class = lock->class[subclass];
-       /* not cached yet? */
+       if (!subclass)
+               class = lock->class_cache;
+       /*
+        * Not cached yet or subclass?
+        */
        if (unlikely(!class)) {
                class = register_lock_class(lock, subclass);
                if (!class)
@@ -2449,48 +2494,44 @@ void lockdep_free_key_range(void *start, unsigned long size)
 
 void lockdep_reset_lock(struct lockdep_map *lock)
 {
-       struct lock_class *class, *next, *entry;
+       struct lock_class *class, *next;
        struct list_head *head;
        unsigned long flags;
        int i, j;
 
        raw_local_irq_save(flags);
-       __raw_spin_lock(&hash_lock);
 
        /*
-        * Remove all classes this lock has:
+        * Remove all classes this lock might have:
         */
+       for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
+               /*
+                * If the class exists we look it up and zap it:
+                */
+               class = look_up_lock_class(lock, j);
+               if (class)
+                       zap_class(class);
+       }
+       /*
+        * Debug check: in the end all mapped classes should
+        * be gone.
+        */
+       __raw_spin_lock(&hash_lock);
        for (i = 0; i < CLASSHASH_SIZE; i++) {
                head = classhash_table + i;
                if (list_empty(head))
                        continue;
                list_for_each_entry_safe(class, next, head, hash_entry) {
-                       for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
-                               entry = lock->class[j];
-                               if (class == entry) {
-                                       zap_class(class);
-                                       lock->class[j] = NULL;
-                                       break;
-                               }
+                       if (unlikely(class == lock->class_cache)) {
+                               __raw_spin_unlock(&hash_lock);
+                               DEBUG_LOCKS_WARN_ON(1);
+                               goto out_restore;
                        }
                }
        }
-
-       /*
-        * Debug check: in the end all mapped classes should
-        * be gone.
-        */
-       for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
-               entry = lock->class[j];
-               if (!entry)
-                       continue;
-               __raw_spin_unlock(&hash_lock);
-               DEBUG_LOCKS_WARN_ON(1);
-               raw_local_irq_restore(flags);
-               return;
-       }
-
        __raw_spin_unlock(&hash_lock);
+
+out_restore:
        raw_local_irq_restore(flags);
 }