#include <linux/hash.h>
 #include <linux/ftrace.h>
 #include <linux/stringify.h>
+#include <linux/bitmap.h>
 #include <linux/bitops.h>
 #include <linux/gfp.h>
 #include <linux/random.h>
 
 unsigned long nr_list_entries;
 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
+static DECLARE_BITMAP(list_entries_in_use, MAX_LOCKDEP_ENTRIES);
 
 /*
  * All data structures here are protected by the global debug_lock.
  */
 static struct lock_list *alloc_list_entry(void)
 {
-       if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
+       int idx = find_first_zero_bit(list_entries_in_use,
+                                     ARRAY_SIZE(list_entries));
+
+       if (idx >= ARRAY_SIZE(list_entries)) {
                if (!debug_locks_off_graph_unlock())
                        return NULL;
 
                dump_stack();
                return NULL;
        }
-       return list_entries + nr_list_entries++;
+       nr_list_entries++;
+       __set_bit(idx, list_entries_in_use);
+       return list_entries + idx;
 }
 
 /*
        unsigned long nr;
 
        nr = lock - list_entries;
-       WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
+       WARN_ON(nr >= ARRAY_SIZE(list_entries)); /* Out-of-bounds, input fail */
        lock->parent = parent;
        lock->class->dep_gen_id = lockdep_dependency_gen_id;
 }
        unsigned long nr;
 
        nr = lock - list_entries;
-       WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
+       WARN_ON(nr >= ARRAY_SIZE(list_entries)); /* Out-of-bounds, input fail */
        return lock->class->dep_gen_id == lockdep_dependency_gen_id;
 }
 
         * Remove all dependencies this lock is
         * involved in:
         */
-       for (i = 0, entry = list_entries; i < nr_list_entries; i++, entry++) {
+       for_each_set_bit(i, list_entries_in_use, ARRAY_SIZE(list_entries)) {
+               entry = list_entries + i;
                if (entry->class != class && entry->links_to != class)
                        continue;
+               __clear_bit(i, list_entries_in_use);
+               nr_list_entries--;
                list_del_rcu(&entry->entry);
-               /* Clear .class and .links_to to avoid double removal. */
-               WRITE_ONCE(entry->class, NULL);
-               WRITE_ONCE(entry->links_to, NULL);
        }
        if (list_empty(&class->locks_after) &&
            list_empty(&class->locks_before)) {
               (sizeof(lock_classes) +
                sizeof(classhash_table) +
                sizeof(list_entries) +
+               sizeof(list_entries_in_use) +
                sizeof(chainhash_table) +
                sizeof(delayed_free)
 #ifdef CONFIG_PROVE_LOCKING