Lines Matching +full:dont +full:- +full:validate
1 // SPDX-License-Identifier: GPL-2.0-only
15 * - lock inversion scenarios
16 * - circular lock dependencies
17 * - hardirq/softirq safe/unsafe locking bugs
122 if (current->lockdep_recursion) in lockdep_enabled()
133 * to use a raw spinlock - we really dont want the spinlock
175 * walking the graph we dont change it (while the other in graph_lock()
214 #define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
225 unsigned int class_idx = hlock->class_idx; in hlock_class()
227 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfield */ in hlock_class()
239 * At this point, if the passed hlock->class_idx is still garbage, in hlock_class()
271 if (time > lt->max) in lock_time_inc()
272 lt->max = time; in lock_time_inc()
274 if (time < lt->min || !lt->nr) in lock_time_inc()
275 lt->min = time; in lock_time_inc()
277 lt->total += time; in lock_time_inc()
278 lt->nr++; in lock_time_inc()
283 if (!src->nr) in lock_time_add()
286 if (src->max > dst->max) in lock_time_add()
287 dst->max = src->max; in lock_time_add()
289 if (src->min < dst->min || !dst->nr) in lock_time_add()
290 dst->min = src->min; in lock_time_add()
292 dst->total += src->total; in lock_time_add()
293 dst->nr += src->nr; in lock_time_add()
304 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; in lock_stats()
307 stats.contention_point[i] += pcs->contention_point[i]; in lock_stats()
310 stats.contending_point[i] += pcs->contending_point[i]; in lock_stats()
312 lock_time_add(&pcs->read_waittime, &stats.read_waittime); in lock_stats()
313 lock_time_add(&pcs->write_waittime, &stats.write_waittime); in lock_stats()
315 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime); in lock_stats()
316 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime); in lock_stats()
319 stats.bounces[i] += pcs->bounces[i]; in lock_stats()
331 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; in clear_lock_stats()
335 memset(class->contention_point, 0, sizeof(class->contention_point)); in clear_lock_stats()
336 memset(class->contending_point, 0, sizeof(class->contending_point)); in clear_lock_stats()
341 return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes]; in get_lock_stats()
352 holdtime = lockstat_clock() - hlock->holdtime_stamp; in lock_release_holdtime()
355 if (hlock->read) in lock_release_holdtime()
356 lock_time_inc(&stats->read_holdtime, holdtime); in lock_release_holdtime()
358 lock_time_inc(&stats->write_holdtime, holdtime); in lock_release_holdtime()
376 * struct pending_free - information about data structures about to be freed
387 * struct delayed_free - data structures used for delayed freeing
405 * The lockdep classes are in a hash-table as well, for fast lookup:
407 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
415 * We put the lock dependency chains into a hash-table as well, to cache
418 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
432 return (hlock->class_idx | (hlock->read << MAX_LOCKDEP_KEYS_BITS)); in hlock_id()
437 return hlock_id & (MAX_LOCKDEP_KEYS - 1); in chain_hlock_class_idx()
443 * It's a 64-bit hash, because it's important for the keys to be
457 task->lockdep_depth = 0; /* no locks held yet */ in lockdep_init_task()
458 task->curr_chain_key = INITIAL_CHAIN_KEY; in lockdep_init_task()
459 task->lockdep_recursion = 0; in lockdep_init_task()
501 if (class->name_version == 1 && in class_filter()
502 !strcmp(class->name, "lockname")) in class_filter()
504 if (class->name_version == 1 && in class_filter()
505 !strcmp(class->name, "&struct->lockfield")) in class_filter()
534 * struct lock_trace - single stack backtrace
549 * Stack-trace: sequence of lock_trace structures. Protected by the graph_lock.
556 return t1->hash == t2->hash && t1->nr_entries == t2->nr_entries && in traces_identical()
557 memcmp(t1->entries, t2->entries, in traces_identical()
558 t1->nr_entries * sizeof(t1->entries[0])) == 0; in traces_identical()
572 max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries - in save_trace()
586 trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3); in save_trace()
588 hash = jhash(trace->entries, trace->nr_entries * in save_trace()
589 sizeof(trace->entries[0]), 0); in save_trace()
590 trace->hash = hash; in save_trace()
591 hash_head = stack_trace_hash + (hash & (STACK_TRACE_HASH_SIZE - 1)); in save_trace()
596 nr_stack_trace_entries += LOCK_TRACE_SIZE_IN_LONGS + trace->nr_entries; in save_trace()
597 hlist_add_head(&trace->hash_entry, hash_head); in save_trace()
650 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
651 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
652 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
653 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
663 [LOCK_USAGE_STATES] = "IN-NMI",
689 * - '+': irq is enabled and not in irq context in get_usage_char()
690 * - '-': in irq context and irq is disabled in get_usage_char()
691 * - '?': in irq context and irq is enabled in get_usage_char()
693 if (class->usage_mask & lock_flag(bit + LOCK_USAGE_DIR_MASK)) { in get_usage_char()
695 if (class->usage_mask & lock_flag(bit)) in get_usage_char()
697 } else if (class->usage_mask & lock_flag(bit)) in get_usage_char()
698 c = '-'; in get_usage_char()
721 name = class->name; in __print_lock_name()
723 name = __get_key_name(class->key, str); in __print_lock_name()
727 if (class->name_version > 1) in __print_lock_name()
728 printk(KERN_CONT "#%d", class->name_version); in __print_lock_name()
729 if (class->subclass) in __print_lock_name()
730 printk(KERN_CONT "/%d", class->subclass); in __print_lock_name()
731 if (hlock && class->print_fn) in __print_lock_name()
732 class->print_fn(hlock->instance); in __print_lock_name()
744 printk(KERN_CONT "){%s}-{%d:%d}", usage, in print_lock_name()
745 class->wait_type_outer ?: class->wait_type_inner, in print_lock_name()
746 class->wait_type_inner); in print_lock_name()
754 name = lock->name; in print_lockdep_cache()
756 name = __get_key_name(lock->key->subkeys, str); in print_lockdep_cache()
780 printk(KERN_CONT "%px", hlock->instance); in print_lock()
782 printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip); in print_lock()
787 int i, depth = READ_ONCE(p->lockdep_depth); in lockdep_print_held_locks()
790 printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p)); in lockdep_print_held_locks()
793 str_plural(depth), p->comm, task_pid_nr(p)); in lockdep_print_held_locks()
802 print_lock(p->held_locks + i); in lockdep_print_held_locks()
808 printk("%s %.*s %s\n", init_utsname()->release, in print_kernel_ident()
809 (int)strcspn(init_utsname()->version, " "), in print_kernel_ident()
810 init_utsname()->version, in print_kernel_ident()
849 * in-kernel percpu var? in static_obj()
863 * class->name_version generation counter. The caller must hold the graph
871 if (!new_class->name) in count_matching_names()
875 if (new_class->key - new_class->subclass == class->key) in count_matching_names()
876 return class->name_version; in count_matching_names()
877 if (class->name && !strcmp(class->name, new_class->name)) in count_matching_names()
878 count = max(count, class->name_version); in count_matching_names()
884 /* used from NMI context -- must be lockless */
910 if (unlikely(!lock->key)) in look_up_lock_class()
914 * NOTE: the class-key must be unique. For dynamic locks, a static in look_up_lock_class()
916 * (or spin_lock_init()) call - which acts as the key. For static in look_up_lock_class()
922 key = lock->key->subkeys + subclass; in look_up_lock_class()
933 if (class->key == key) { in look_up_lock_class()
938 WARN_ONCE(class->name != lock->name && in look_up_lock_class()
939 lock->key != &__lockdep_no_validate__, in look_up_lock_class()
941 lock->name, lock->key, class->name); in look_up_lock_class()
950 * Static locks do not have their class-keys yet - for them the key is
970 lock->key = (void *)can_addr; in assign_lock_key()
972 lock->key = (void *)can_addr; in assign_lock_key()
974 lock->key = (void *)lock; in assign_lock_key()
976 /* Debug-check: all keys must be persistent! */ in assign_lock_key()
979 pr_err("INFO: trying to register non-static key.\n"); in assign_lock_key()
1017 if (in_list(e, &class->locks_after) || in in_any_class_list()
1018 in_list(e, &class->locks_before)) in in_any_class_list()
1029 if (e->links_to != c) { in class_lock_list_valid()
1031 c->name ? : "(?)", in class_lock_list_valid()
1032 (unsigned long)(e - list_entries), in class_lock_list_valid()
1033 e->links_to && e->links_to->name ? in class_lock_list_valid()
1034 e->links_to->name : "(?)", in class_lock_list_valid()
1035 e->class && e->class->name ? e->class->name : in class_lock_list_valid()
1053 for (i = chain->base; i < chain->base + chain->depth; i++) in check_lock_chain_key()
1059 if (chain->chain_key != chain_key) { in check_lock_chain_key()
1061 (unsigned long long)(chain - lock_chains), in check_lock_chain_key()
1062 (unsigned long long)chain->chain_key, in check_lock_chain_key()
1076 if (in_list(&class->lock_entry, &pf->zapped)) in in_any_zapped_class_list()
1094 if (!in_list(&class->lock_entry, &all_lock_classes) && in __check_data_structures()
1095 !in_list(&class->lock_entry, &free_lock_classes) && in __check_data_structures()
1098 class, class->name ? : "(?)"); in __check_data_structures()
1106 if (!class_lock_list_valid(class, &class->locks_before)) in __check_data_structures()
1108 if (!class_lock_list_valid(class, &class->locks_after)) in __check_data_structures()
1127 if (!in_any_class_list(&e->entry)) { in __check_data_structures()
1129 (unsigned int)(e - list_entries), in __check_data_structures()
1130 e->class->name ? : "(?)", in __check_data_structures()
1131 e->links_to->name ? : "(?)"); in __check_data_structures()
1142 if (in_any_class_list(&e->entry)) { in __check_data_structures()
1144 (unsigned int)(e - list_entries), in __check_data_structures()
1145 e->class && e->class->name ? e->class->name : in __check_data_structures()
1147 e->links_to && e->links_to->name ? in __check_data_structures()
1148 e->links_to->name : "(?)"); in __check_data_structures()
1237 hlist_add_head_rcu(&key->hash_entry, hash_head); in lockdep_register_key()
1258 * a use-after-free in that case by returning early. in is_dynamic_key()
1278 * Register a lock's class in the hash-table, if the class is not present
1296 if (!lock->key) { in register_lock_class()
1299 } else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) { in register_lock_class()
1303 key = lock->key->subkeys + subclass; in register_lock_class()
1310 * We have to do the hash-walk again, to avoid races in register_lock_class()
1314 if (class->key == key) in register_lock_class()
1335 __set_bit(class - lock_classes, lock_classes_in_use); in register_lock_class()
1337 class->key = key; in register_lock_class()
1338 class->name = lock->name; in register_lock_class()
1339 class->subclass = subclass; in register_lock_class()
1340 WARN_ON_ONCE(!list_empty(&class->locks_before)); in register_lock_class()
1341 WARN_ON_ONCE(!list_empty(&class->locks_after)); in register_lock_class()
1342 class->name_version = count_matching_names(class); in register_lock_class()
1343 class->wait_type_inner = lock->wait_type_inner; in register_lock_class()
1344 class->wait_type_outer = lock->wait_type_outer; in register_lock_class()
1345 class->lock_type = lock->lock_type; in register_lock_class()
1347 * We use RCU's safe list-add method to make in register_lock_class()
1348 * parallel walking of the hash-list safe: in register_lock_class()
1350 hlist_add_head_rcu(&class->hash_entry, hash_head); in register_lock_class()
1355 list_move_tail(&class->lock_entry, &all_lock_classes); in register_lock_class()
1356 idx = class - lock_classes; in register_lock_class()
1364 printk("\nnew class %px: %s", class->key, class->name); in register_lock_class()
1365 if (class->name_version > 1) in register_lock_class()
1366 printk(KERN_CONT "#%d", class->name_version); in register_lock_class()
1380 lock->class_cache[0] = class; in register_lock_class()
1382 lock->class_cache[subclass] = class; in register_lock_class()
1386 * hash but the subclass -- which is hashed in -- didn't match. in register_lock_class()
1388 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass)) in register_lock_class()
1429 * Lock not present yet - get a new dependency struct and in add_lock_to_list()
1436 entry->class = this; in add_lock_to_list()
1437 entry->links_to = links_to; in add_lock_to_list()
1438 entry->dep = dep; in add_lock_to_list()
1439 entry->distance = distance; in add_lock_to_list()
1440 entry->trace = trace; in add_lock_to_list()
1443 * iteration is under RCU-sched; see look_up_lock_class() and in add_lock_to_list()
1446 list_add_tail_rcu(&entry->entry, head); in add_lock_to_list()
1455 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
1459 * breadth-first search (BFS) algorithm, by which we can determine
1462 * indicates that adding the <prev> -> <next> lock dependency will
1463 * produce a circle in the graph. Breadth-first search instead of
1464 * depth-first search is used in order to find the shortest (circular)
1480 cq->front = cq->rear = 0; in __cq_init()
1486 return (cq->front == cq->rear); in __cq_empty()
1491 return ((cq->rear + 1) & CQ_MASK) == cq->front; in __cq_full()
1497 return -1; in __cq_enqueue()
1499 cq->element[cq->rear] = elem; in __cq_enqueue()
1500 cq->rear = (cq->rear + 1) & CQ_MASK; in __cq_enqueue()
1515 lock = cq->element[cq->front]; in __cq_dequeue()
1516 cq->front = (cq->front + 1) & CQ_MASK; in __cq_dequeue()
1523 return (cq->rear - cq->front) & CQ_MASK; in __cq_get_elem_count()
1528 lock->class->dep_gen_id = lockdep_dependency_gen_id; in mark_lock_accessed()
1534 lock->parent = parent; in visit_lock_entry()
1539 return lock->class->dep_gen_id == lockdep_dependency_gen_id; in lock_accessed()
1544 return child->parent; in get_lock_parent()
1568 void *lock_class = lock->class; in get_dep_list()
1589 BFS_EINVALIDNODE = -2,
1590 BFS_EQUEUEFULL = -1,
1606 * For dependency @prev -> @next:
1608 * SR: @prev is shared reader (->read != 0) and @next is recursive reader
1609 * (->read == 2)
1610 * ER: @prev is exclusive locker (->read == 0) and @next is recursive reader
1611 * SN: @prev is shared reader and @next is non-recursive locker (->read != 2)
1612 * EN: @prev is exclusive locker and @next is non-recursive locker
1615 * bit0 is prev->read == 0
1616 * bit1 is next->read != 2
1631 return (prev->read == 0) + ((next->read != 2) << 1); in __calc_dep_bit()
1646 return (next->read != 2) + ((prev->read == 0) << 1); in __calc_dep_bitb()
1661 lock->class = class; in __bfs_init_root()
1662 lock->parent = NULL; in __bfs_init_root()
1663 lock->only_xr = 0; in __bfs_init_root()
1670 * ->only_xr of the initial lock node is set to @hlock->read == 2, to make sure
1671 * that <prev> -> @hlock and @hlock -> <whatever __bfs() found> is not -(*R)->
1672 * and -(S*)->.
1678 lock->only_xr = (hlock->read == 2); in bfs_init_root()
1684 * ->only_xr of the initial lock node is set to @hlock->read != 0, to make sure
1685 * that <next> -> @hlock and @hlock -> <whatever backwards BFS found> is not
1686 * -(*S)-> and -(R*)-> (reverse order of -(*R)-> and -(S*)->).
1692 lock->only_xr = (hlock->read != 0); in bfs_init_rootb()
1697 if (!lock || !lock->parent) in __bfs_next()
1700 return list_next_or_null_rcu(get_dep_list(lock->parent, offset), in __bfs_next()
1701 &lock->entry, struct lock_list, entry); in __bfs_next()
1705 * Breadth-First Search to find a strong path in the dependency graph.
1718 * dependencies as -(*R)-> -(S*)->, please see:
1720 * Documentation/locking/lockdep-design.rst
1727 * has -(*R)-> in the search, and if it does (prev only has -(*R)->), we
1728 * filter out any -(S*)-> in the current dependency and after that, the
1729 * ->only_xr is set according to whether we only have -(*R)-> left.
1751 if (!lock->class) in __bfs()
1759 * @lock->class->locks_{after,before}) we skip, otherwise go in __bfs()
1772 if (lock->parent) { /* Parent exists, check prev dependency */ in __bfs()
1773 u8 dep = lock->dep; in __bfs()
1774 bool prev_only_xr = lock->parent->only_xr; in __bfs()
1777 * Mask out all -(S*)-> if we only have *R in previous in __bfs()
1778 * step, because -(*R)-> -(S*)-> don't make up a strong in __bfs()
1788 /* If there are only -(*R)-> left, set that for the next step */ in __bfs()
1789 lock->only_xr = !(dep & (DEP_SN_MASK | DEP_EN_MASK)); in __bfs()
1866 stack_trace_print(trace->entries, trace->nr_entries, spaces); in print_lock_trace()
1878 printk("\n-> #%u", depth); in print_circular_bug_entry()
1879 print_lock_name(NULL, target->class); in print_circular_bug_entry()
1881 print_lock_trace(target->trace, 6); in print_circular_bug_entry()
1891 struct lock_class *parent = prt->class; in print_circular_lock_scenario()
1892 int src_read = src->read; in print_circular_lock_scenario()
1893 int tgt_read = tgt->read; in print_circular_lock_scenario()
1911 printk(KERN_CONT " --> "); in print_circular_lock_scenario()
1913 printk(KERN_CONT " --> "); in print_circular_lock_scenario()
1920 printk(" ---- ----\n"); in print_circular_lock_scenario()
1935 else if (src->sync) in print_circular_lock_scenario()
1962 pr_warn("------------------------------------------------------\n"); in print_circular_bug_header()
1964 curr->comm, task_pid_nr(curr)); in print_circular_bug_header()
1977 * We are about to add A -> B into the dependency graph, and in __bfs() a
1978 * strong dependency path A -> .. -> B is found: hlock_class equals
1979 * entry->class.
1981 * If A -> .. -> B can replace A -> B in any __bfs() search (means the former
1982 * is _stronger_ than or equal to the latter), we consider A -> B as redundant.
1983 * For example if A -> .. -> B is -(EN)-> (i.e. A -(E*)-> .. -(*N)-> B), and A
1984 * -> B is -(ER)-> or -(EN)->, then we don't need to add A -> B into the
1985 * dependency graph, as any strong path ..-> A -> B ->.. we can get with
1986 * having dependency A -> B, we could already get a equivalent path ..-> A ->
1987 * .. -> B -> .. with A -> .. -> B. Therefore A -> B is redundant.
1989 * We need to make sure both the start and the end of A -> .. -> B is not
1990 * weaker than A -> B. For the start part, please see the comment in
1995 * a) A -> B is -(*R)-> (everything is not weaker than that)
1999 * b) A -> .. -> B is -(*N)-> (nothing is stronger than this)
2006 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */ in hlock_equal()
2007 (hlock->read == 2 || /* A -> B is -(*R)-> */ in hlock_equal()
2008 !entry->only_xr); /* A -> .. -> B is -(*N)-> */ in hlock_equal()
2012 * We are about to add B -> A into the dependency graph, and in __bfs() a
2013 * strong dependency path A -> .. -> B is found: hlock_class equals
2014 * entry->class.
2016 * We will have a deadlock case (conflict) if A -> .. -> B -> A is a strong
2021 * a) B -> A is -(E*)->
2025 * b) A -> .. -> B is -(*N)-> (i.e. A -> .. -(*N)-> B)
2027 * as then we don't have -(*R)-> -(S*)-> in the cycle.
2033 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */ in hlock_conflict()
2034 (hlock->read == 0 || /* B -> A is -(E*)-> */ in hlock_conflict()
2035 !entry->only_xr); /* A -> .. -> B is -(*N)-> */ in hlock_conflict()
2051 this->trace = save_trace(); in print_circular_bug()
2052 if (!this->trace) in print_circular_bug()
2065 print_circular_bug_entry(parent, --depth); in print_circular_bug()
2087 * Breadth-first-search failed, graph got corrupted? in print_bfs_bug()
2177 * <target> -> <src> dependency.
2205 if (src->class_idx == target->class_idx) in check_noncircular()
2219 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
2221 * A irq safe->unsafe deadlock happens with the following conditions:
2223 * 1) We have a strong dependency path A -> ... -> B
2226 * irq can create a new dependency B -> A (consider the case that a holder
2229 * 3) the dependency circle A -> ... -> B -> A we get from 1) and 2) is a
2233 * a) if A -> B is -(*N)->, then B -> A could be any type, so any
2235 * b) if A -> B is -(*R)->, then B -> A must be -(E*)->, so only
2239 * c) if A -> B is -(E*)->, then B -> A could be any type, so any
2241 * d) if A -> B is -(S*)->, then B -> A must be -(*N)->, so only
2246 * There is a strong dependency path in the dependency graph: A -> B, and now
2248 * safe->unsafe bugs.
2250 * Note that usage_accumulate() is used in backwards search, so ->only_xr
2251 * stands for whether A -> B only has -(S*)-> (in this case ->only_xr is true).
2253 * As above, if only_xr is false, which means A -> B has -(E*)-> dependency
2259 if (!entry->only_xr) in usage_accumulate()
2260 *(unsigned long *)mask |= entry->class->usage_mask; in usage_accumulate()
2262 *(unsigned long *)mask |= (entry->class->usage_mask & LOCKF_IRQ); in usage_accumulate()
2268 * There is a strong dependency path in the dependency graph: A -> B, and now
2270 * i.e. which usage bit of B may introduce safe->unsafe deadlocks.
2272 * As above, if only_xr is false, which means A -> B has -(*N)-> dependency
2278 if (!entry->only_xr) in usage_match()
2279 return !!(entry->class->usage_mask & *(unsigned long *)mask); in usage_match()
2281 return !!((entry->class->usage_mask & LOCKF_IRQ) & *(unsigned long *)mask); in usage_match()
2286 if (entry->class->lock_type == LD_LOCK_NORMAL) in usage_skip()
2302 * where lock(B) cannot sleep, and we have a dependency B -> ... -> A. in usage_skip()
2305 * have the observation for any lock chain L1 -> ... -> Ln, for any in usage_skip()
2310 * way the local_lock() exists in the dependency B -> ... -> A. in usage_skip()
2315 if (entry->class->lock_type == LD_LOCK_PERCPU && in usage_skip()
2316 DEBUG_LOCKS_WARN_ON(entry->class->wait_type_inner < LD_WAIT_CONFIG)) in usage_skip()
2320 * Skip WAIT_OVERRIDE for irq inversion detection -- it's not actually in usage_skip()
2328 * Find a node in the forwards-direction dependency sub-graph starting
2329 * at @root->class that matches @bit.
2348 * Find a node in the backwards-direction dependency sub-graph starting
2349 * at @root->class that matches @bit.
2368 printk("%*s->", depth, ""); in print_lock_class_header()
2376 if (class->usage_mask & (1 << bit)) { in print_lock_class_header()
2381 print_lock_trace(class->usage_traces[bit], len); in print_lock_class_header()
2387 depth, "", class->key, class->key); in print_lock_class_header()
2393 * After BFS we get a lock dependency path (linked via ->parent of lock_list),
2399 * for a lock dependency A -> B, there are two lock_lists:
2401 * a) lock_list in the ->locks_after list of A, whose ->class is B and
2402 * ->links_to is A. In this case, we can say the lock_list is
2403 * "A -> B" (forwards case).
2405 * b) lock_list in the ->locks_before list of B, whose ->class is A
2406 * and ->links_to is B. In this case, we can say the lock_list is
2407 * "B <- A" (bacwards case).
2409 * The ->trace of both a) and b) point to the call trace where B was
2415 * ->class is A, as a result BFS will search all dependencies starting with
2416 * A, e.g. A -> B or A -> C.
2418 * The notation of a forwards helper lock_list is like "-> A", which means
2419 * we should search the forwards dependencies starting with "A", e.g A -> B
2420 * or A -> C.
2422 * The notation of a bacwards helper lock_list is like "<- B", which means
2424 * B <- A or B <- C.
2435 * ->parent ->parent
2436 * | lock_list | <--------- | lock_list | ... | lock_list | <--------- | lock_list |
2437 * | -> L1 | | L1 -> L2 | ... |Ln-2 -> Ln-1| | Ln-1 -> Ln|
2439 * , so it's natural that we start from @leaf and print every ->class and
2440 * ->trace until we reach the @root.
2453 print_lock_class_header(entry->class, depth); in print_shortest_lock_dependencies()
2455 print_lock_trace(entry->trace, 2); in print_shortest_lock_dependencies()
2464 depth--; in print_shortest_lock_dependencies()
2476 * ->parent ->parent
2477 * | lock_list | ---------> | lock_list | ... | lock_list | ---------> | lock_list |
2478 * | L2 <- L1 | | L3 <- L2 | ... | Ln <- Ln-1 | | <- Ln |
2481 * dependency path L1 -> L2 -> .. -> Ln in the non-reverse order.
2483 * Another thing to notice here is that ->class of L2 <- L1 is L1, while the
2484 * ->trace of L2 <- L1 is the call trace of L2, in fact we don't have the call
2500 print_lock_class_header(entry->class, depth); in print_shortest_lock_dependencies_backwards()
2511 trace = entry->trace; in print_shortest_lock_dependencies_backwards()
2519 depth--; in print_shortest_lock_dependencies_backwards()
2529 struct lock_class *safe_class = safe_entry->class; in print_irq_lock_scenario()
2530 struct lock_class *unsafe_class = unsafe_entry->class; in print_irq_lock_scenario()
2552 printk(KERN_CONT " --> "); in print_irq_lock_scenario()
2554 printk(KERN_CONT " --> "); in print_irq_lock_scenario()
2561 printk(" ---- ----\n"); in print_irq_lock_scenario()
2598 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n", in print_bad_irq_dependency()
2601 pr_warn("-----------------------------------------------------\n"); in print_bad_irq_dependency()
2603 curr->comm, task_pid_nr(curr), in print_bad_irq_dependency()
2605 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT, in print_bad_irq_dependency()
2607 curr->softirqs_enabled); in print_bad_irq_dependency()
2614 pr_cont(" ->"); in print_bad_irq_dependency()
2618 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n", in print_bad_irq_dependency()
2620 print_lock_name(NULL, backwards_entry->class); in print_bad_irq_dependency()
2621 pr_warn("\n... which became %s-irq-safe at:\n", irqclass); in print_bad_irq_dependency()
2623 print_lock_trace(backwards_entry->class->usage_traces[bit1], 1); in print_bad_irq_dependency()
2625 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass); in print_bad_irq_dependency()
2626 print_lock_name(NULL, forwards_entry->class); in print_bad_irq_dependency()
2627 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass); in print_bad_irq_dependency()
2630 print_lock_trace(forwards_entry->class->usage_traces[bit2], 1); in print_bad_irq_dependency()
2638 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass); in print_bad_irq_dependency()
2642 pr_warn(" and %s-irq-unsafe lock:\n", irqclass); in print_bad_irq_dependency()
2643 next_root->trace = save_trace(); in print_bad_irq_dependency()
2644 if (!next_root->trace) in print_bad_irq_dependency()
2663 __stringify(__STATE)"-READ",
2681 * bit2-n: state
2696 * right shift of the mask transforms the individual bitnrs as -1 and
2737 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*
2738 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*
2739 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*_READ
2740 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*_READ
2804 return -1; in find_exclusive_match()
2808 * Prove that the new dependency does not connect a hardirq-safe(-read)
2809 * lock with a hardirq-unsafe lock - to achieve this we search
2810 * the backwards-subgraph starting at <prev>, and the
2811 * forwards-subgraph starting at <next>:
2863 * When trying to add A -> B to the graph, we find that there is a in check_irq_usage()
2864 * hardirq-safe L, that L -> ... -> A, and another hardirq-unsafe M, in check_irq_usage()
2865 * that B -> ... -> M. However M is **softirq-safe**, if we use exact in check_irq_usage()
2867 * **softirq-unsafe** and N -> ... -> A, however N -> .. -> M will not in check_irq_usage()
2870 backward_mask = original_mask(target_entry1->class->usage_mask & LOCKF_ENABLED_IRQ_ALL); in check_irq_usage()
2884 ret = find_exclusive_match(target_entry->class->usage_mask, in check_irq_usage()
2885 target_entry1->class->usage_mask, in check_irq_usage()
2887 if (DEBUG_LOCKS_WARN_ON(ret == -1)) in check_irq_usage()
2917 * <target> or not. If it can, <src> -> <target> dependency is already
2935 * is equal to or stronger than <src> -> <target>. So if <src> is E, in check_redundant()
2936 * we need to let __bfs() only search for a path starting at a -(E*)->, in check_redundant()
2937 * we achieve this by setting the initial node's ->only_xr to true in in check_redundant()
2938 * that case. And if <prev> is S, we set initial ->only_xr to false in check_redundant()
2939 * because both -(S*)-> (equal) and -(E*)-> (stronger) are redundant. in check_redundant()
2941 src_entry.only_xr = src->read == 0; in check_redundant()
2947 * comment in usage_skip(), A -> local_lock() -> B and A -> B are not in check_redundant()
2981 nr_hardirq_chains--; in dec_chains()
2983 nr_softirq_chains--; in dec_chains()
2985 nr_process_chains--; in dec_chains()
2996 printk(" ----\n"); in print_deadlock_scenario()
3022 pr_warn("--------------------------------------------\n"); in print_deadlock_bug()
3024 curr->comm, task_pid_nr(curr)); in print_deadlock_bug()
3029 if (class->cmp_fn) { in print_deadlock_bug()
3031 class->cmp_fn(prev->instance, next->instance)); in print_deadlock_bug()
3062 for (i = 0; i < curr->lockdep_depth; i++) { in check_deadlock()
3063 prev = curr->held_locks + i; in check_deadlock()
3065 if (prev->instance == next->nest_lock) in check_deadlock()
3072 * Allow read-after-read recursion of the same in check_deadlock()
3075 if ((next->read == 2) && prev->read) in check_deadlock()
3080 if (class->cmp_fn && in check_deadlock()
3081 class->cmp_fn(prev->instance, next->instance) < 0) in check_deadlock()
3098 * There was a chain-cache miss, and we are about to add a new dependency
3099 * to a previous lock. We validate the following rules:
3101 * - would the adding of the <prev> -> <next> dependency create a
3104 * - does the new prev->next dependency connect any hardirq-safe lock
3105 * (in the full backwards-subgraph starting at <prev>) with any
3106 * hardirq-unsafe lock (in the full forwards-subgraph starting at
3109 * - does the new prev->next dependency connect any softirq-safe lock
3110 * (in the full backwards-subgraph starting at <prev>) with any
3111 * softirq-unsafe lock (in the full forwards-subgraph starting at
3127 if (!hlock_class(prev)->key || !hlock_class(next)->key) { in check_prev_add()
3129 * The warning statements below may trigger a use-after-free in check_prev_add()
3130 * of the class name. It is better to trigger a use-after free in check_prev_add()
3134 WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key, in check_prev_add()
3135 "Detected use-after-free of lock class %px/%s\n", in check_prev_add()
3137 hlock_class(prev)->name); in check_prev_add()
3138 WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key, in check_prev_add()
3139 "Detected use-after-free of lock class %px/%s\n", in check_prev_add()
3141 hlock_class(next)->name); in check_prev_add()
3145 if (prev->class_idx == next->class_idx) { in check_prev_add()
3148 if (class->cmp_fn && in check_prev_add()
3149 class->cmp_fn(prev->instance, next->instance) < 0) in check_prev_add()
3154 * Prove that the new <prev> -> <next> dependency would not in check_prev_add()
3156 * a breadth-first search into the graph starting at <next>, in check_prev_add()
3171 * Is the <prev> -> <next> dependency already present? in check_prev_add()
3174 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3 in check_prev_add()
3175 * chains - the second one will be new, but L1 already has in check_prev_add()
3178 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) { in check_prev_add()
3179 if (entry->class == hlock_class(next)) { in check_prev_add()
3181 entry->distance = 1; in check_prev_add()
3182 entry->dep |= calc_dep(prev, next); in check_prev_add()
3186 * ->locks_before list. in check_prev_add()
3200 list_for_each_entry(entry, &hlock_class(next)->locks_before, entry) { in check_prev_add()
3201 if (entry->class == hlock_class(prev)) { in check_prev_add()
3203 entry->distance = 1; in check_prev_add()
3204 entry->dep |= calc_depb(prev, next); in check_prev_add()
3215 * Is the <prev> -> <next> link redundant? in check_prev_add()
3234 &hlock_class(prev)->locks_after, distance, in check_prev_add()
3241 &hlock_class(next)->locks_before, distance, in check_prev_add()
3250 * Add the dependency to all directly-previous locks that are 'relevant'.
3252 * all consecutive trylock entries and the final non-trylock entry - or
3253 * the end of this context's lock-chain - whichever comes first.
3259 int depth = curr->lockdep_depth; in check_prevs_add()
3265 * Depth must not be zero for a non-head lock: in check_prevs_add()
3273 if (curr->held_locks[depth].irq_context != in check_prevs_add()
3274 curr->held_locks[depth-1].irq_context) in check_prevs_add()
3278 u16 distance = curr->lockdep_depth - depth + 1; in check_prevs_add()
3279 hlock = curr->held_locks + depth - 1; in check_prevs_add()
3281 if (hlock->check) { in check_prevs_add()
3287 * Stop after the first non-trylock entry, in check_prevs_add()
3288 * as non-trylock entries have added their in check_prevs_add()
3292 if (!hlock->trylock) in check_prevs_add()
3296 depth--; in check_prevs_add()
3298 * End of lock-stack? in check_prevs_add()
3305 if (curr->held_locks[depth].irq_context != in check_prevs_add()
3306 curr->held_locks[depth-1].irq_context) in check_prevs_add()
3337 * Bit 15 - always set to 1 (it is not a class index)
3338 * Bits 0-14 - upper 15 bits of the next block index
3339 * entry[1] - lower 16 bits of next block index
3343 * On the unsized bucket (bucket-0), the 3rd and 4th entries contain
3346 * entry[2] - upper 16 bits of the chain block size
3347 * entry[3] - lower 16 bits of the chain block size
3360 return size - 1; in size_to_bucket()
3367 for ((prev) = -1, (curr) = chain_block_buckets[bucket]; \
3372 * next block or -1
3381 return -1; in chain_block_next()
3391 * bucket-0 only
3421 * the block before it is re-added. in add_chain_block()
3466 nr_free_chain_hlocks -= size; in del_chain_block()
3470 nr_large_chain_blocks--; in del_chain_block()
3478 chain_block_buckets[i] = -1; in init_chain_block_buckets()
3484 * Return offset of a chain block of the right size or -1 if not found.
3486 * Fairly simple worst-fit allocator with the addition of a number of size
3497 BUILD_BUG_ON((MAX_LOCKDEP_KEYS-1) & CHAIN_BLK_FLAG); in alloc_chain_hlocks()
3502 return -1; in alloc_chain_hlocks()
3530 add_chain_block(curr + req, size - req); in alloc_chain_hlocks()
3538 for (size = MAX_CHAIN_BUCKETS; size > req; size--) { in alloc_chain_hlocks()
3545 add_chain_block(curr + req, size - req); in alloc_chain_hlocks()
3549 return -1; in alloc_chain_hlocks()
3559 u16 chain_hlock = chain_hlocks[chain->base + i]; in lock_chain_get_class()
3574 for (i = curr->lockdep_depth - 1; i >= 0; i--) { in get_first_held_lock()
3575 hlock_curr = curr->held_locks + i; in get_first_held_lock()
3576 if (hlock_curr->irq_context != hlock->irq_context) in get_first_held_lock()
3592 printk(" hlock_id:%d -> chain_key:%016Lx", in print_chain_key_iteration()
3603 int depth = curr->lockdep_depth; in print_chain_keys_held_locks()
3606 printk("depth: %u (irq_context %u)\n", depth - i + 1, in print_chain_keys_held_locks()
3607 hlock_next->irq_context); in print_chain_keys_held_locks()
3609 hlock = curr->held_locks + i; in print_chain_keys_held_locks()
3625 printk("depth: %u\n", chain->depth); in print_chain_keys_chain()
3626 for (i = 0; i < chain->depth; i++) { in print_chain_keys_chain()
3627 hlock_id = chain_hlocks[chain->base + i]; in print_chain_keys_chain()
3645 pr_warn("----------------------------\n"); in print_collision()
3646 pr_warn("%s/%d: ", current->comm, task_pid_nr(current)); in print_collision()
3677 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) { in check_no_collision()
3682 for (j = 0; j < chain->depth - 1; j++, i++) { in check_no_collision()
3683 id = hlock_id(&curr->held_locks[i]); in check_no_collision()
3685 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) { in check_no_collision()
3695 * Given an index that is >= -1, return the index of the next lock chain.
3696 * Return -2 if there is no next lock chain.
3701 return i < ARRAY_SIZE(lock_chains) ? i : -2; in lockdep_next_lockchain()
3738 * disabled to make this an IRQ-safe lock.. for recursion reasons in add_chain_cache()
3755 chain->chain_key = chain_key; in add_chain_cache()
3756 chain->irq_context = hlock->irq_context; in add_chain_cache()
3758 chain->depth = curr->lockdep_depth + 1 - i; in add_chain_cache()
3761 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks)); in add_chain_cache()
3764 j = alloc_chain_hlocks(chain->depth); in add_chain_cache()
3776 chain->base = j; in add_chain_cache()
3777 for (j = 0; j < chain->depth - 1; j++, i++) { in add_chain_cache()
3778 int lock_id = hlock_id(curr->held_locks + i); in add_chain_cache()
3780 chain_hlocks[chain->base + j] = lock_id; in add_chain_cache()
3782 chain_hlocks[chain->base + j] = hlock_id(hlock); in add_chain_cache()
3783 hlist_add_head_rcu(&chain->entry, hash_head); in add_chain_cache()
3785 inc_chains(chain->irq_context); in add_chain_cache()
3800 if (READ_ONCE(chain->chain_key) == chain_key) { in lookup_chain_cache()
3810 * add it and return 1 - in this case the new dependency chain is
3830 class->key, class->name); in lookup_chain_cache_add()
3838 (unsigned long long)chain_key, class->key, class->name); in lookup_chain_cache_add()
3845 * We have to walk the chain again locked - to avoid duplicates: in lookup_chain_cache_add()
3873 if (!hlock->trylock && hlock->check && in validate_chain()
3878 * - is irq-safe, if this lock is irq-unsafe in validate_chain()
3879 * - is softirq-safe, if this lock is hardirq-unsafe in validate_chain()
3884 * - within the current held-lock stack in validate_chain()
3885 * - across our accumulated lock dependency records in validate_chain()
3931 * We are building curr_chain_key incrementally, so double-check
3941 for (i = 0; i < curr->lockdep_depth; i++) { in check_chain_key()
3942 hlock = curr->held_locks + i; in check_chain_key()
3943 if (chain_key != hlock->prev_chain_key) { in check_chain_key()
3950 curr->lockdep_depth, i, in check_chain_key()
3952 (unsigned long long)hlock->prev_chain_key); in check_chain_key()
3957 * hlock->class_idx can't go beyond MAX_LOCKDEP_KEYS, but is in check_chain_key()
3960 if (DEBUG_LOCKS_WARN_ON(!test_bit(hlock->class_idx, lock_classes_in_use))) in check_chain_key()
3963 if (prev_hlock && (prev_hlock->irq_context != in check_chain_key()
3964 hlock->irq_context)) in check_chain_key()
3969 if (chain_key != curr->curr_chain_key) { in check_chain_key()
3976 curr->lockdep_depth, i, in check_chain_key()
3978 (unsigned long long)curr->curr_chain_key); in check_chain_key()
3993 printk(" ----\n"); in print_usage_bug_scenario()
4017 pr_warn("--------------------------------\n"); in print_usage_bug()
4019 pr_warn("inconsistent {%s} -> {%s} usage.\n", in print_usage_bug()
4023 curr->comm, task_pid_nr(curr), in print_usage_bug()
4031 print_lock_trace(hlock_class(this)->usage_traces[prev_bit], 1); in print_usage_bug()
4052 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) { in valid_state()
4083 pr_warn("--------------------------------------------------------\n"); in print_irq_inversion_bug()
4085 curr->comm, task_pid_nr(curr)); in print_irq_inversion_bug()
4088 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass); in print_irq_inversion_bug()
4090 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass); in print_irq_inversion_bug()
4091 print_lock_name(NULL, other->class); in print_irq_inversion_bug()
4105 depth--; in print_irq_inversion_bug()
4109 middle ? middle->class : root->class, other->class); in print_irq_inversion_bug()
4112 middle ? middle->class : other->class, root->class); in print_irq_inversion_bug()
4117 root->trace = save_trace(); in print_irq_inversion_bug()
4118 if (!root->trace) in print_irq_inversion_bug()
4129 * Prove that in the forwards-direction subgraph starting at <this>
4152 if (target_entry->class->usage_mask & lock_flag(bit)) { in check_usage_forwards()
4164 * Prove that in the backwards-direction subgraph starting at <this>
4187 if (target_entry->class->usage_mask & lock_flag(bit)) { in check_usage_backwards()
4200 const struct irqtrace_events *trace = &curr->irqtrace; in print_irqtrace_events()
4204 printk("irq event stamp: %u\n", trace->irq_events); in print_irqtrace_events()
4206 trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip, in print_irqtrace_events()
4207 (void *)trace->hardirq_enable_ip); in print_irqtrace_events()
4209 trace->hardirq_disable_event, (void *)trace->hardirq_disable_ip, in print_irqtrace_events()
4210 (void *)trace->hardirq_disable_ip); in print_irqtrace_events()
4212 trace->softirq_enable_event, (void *)trace->softirq_enable_ip, in print_irqtrace_events()
4213 (void *)trace->softirq_enable_ip); in print_irqtrace_events()
4215 trace->softirq_disable_event, (void *)trace->softirq_disable_ip, in print_irqtrace_events()
4216 (void *)trace->softirq_disable_ip); in print_irqtrace_events()
4262 * Validate that this particular lock does not have conflicting in mark_lock_irq()
4277 * Validate that the lock dependencies don't have conflicting usage in mark_lock_irq()
4282 * mark ENABLED has to look backwards -- to ensure no dependee in mark_lock_irq()
4289 * mark USED_IN has to look forwards -- to ensure no dependency in mark_lock_irq()
4311 for (i = 0; i < curr->lockdep_depth; i++) { in mark_held_locks()
4313 hlock = curr->held_locks + i; in mark_held_locks()
4315 if (hlock->read) in mark_held_locks()
4320 if (!hlock->check) in mark_held_locks()
4348 if (curr->softirqs_enabled) in __trace_hardirqs_on_caller()
4353 * lockdep_hardirqs_on_prepare - Prepare for enabling interrupts
4405 current->hardirq_chain_key = current->curr_chain_key; in lockdep_hardirqs_on_prepare()
4415 struct irqtrace_events *trace = ¤t->irqtrace; in lockdep_hardirqs_on()
4433 * - recursion check, because NMI can hit lockdep; in lockdep_hardirqs_on()
4434 * - hardware state check, because above; in lockdep_hardirqs_on()
4435 * - chain_key check, see lockdep_hardirqs_on_prepare(). in lockdep_hardirqs_on()
4465 DEBUG_LOCKS_WARN_ON(current->hardirq_chain_key != in lockdep_hardirqs_on()
4466 current->curr_chain_key); in lockdep_hardirqs_on()
4469 /* we'll do an OFF -> ON transition: */ in lockdep_hardirqs_on()
4471 trace->hardirq_enable_ip = ip; in lockdep_hardirqs_on()
4472 trace->hardirq_enable_event = ++trace->irq_events; in lockdep_hardirqs_on()
4504 struct irqtrace_events *trace = ¤t->irqtrace; in lockdep_hardirqs_off()
4507 * We have done an ON -> OFF transition: in lockdep_hardirqs_off()
4510 trace->hardirq_disable_ip = ip; in lockdep_hardirqs_off()
4511 trace->hardirq_disable_event = ++trace->irq_events; in lockdep_hardirqs_off()
4524 struct irqtrace_events *trace = ¤t->irqtrace; in lockdep_softirqs_on()
4536 if (current->softirqs_enabled) { in lockdep_softirqs_on()
4543 * We'll do an OFF -> ON transition: in lockdep_softirqs_on()
4545 current->softirqs_enabled = 1; in lockdep_softirqs_on()
4546 trace->softirq_enable_ip = ip; in lockdep_softirqs_on()
4547 trace->softirq_enable_event = ++trace->irq_events; in lockdep_softirqs_on()
4573 if (current->softirqs_enabled) { in lockdep_softirqs_off()
4574 struct irqtrace_events *trace = ¤t->irqtrace; in lockdep_softirqs_off()
4577 * We have done an ON -> OFF transition: in lockdep_softirqs_off()
4579 current->softirqs_enabled = 0; in lockdep_softirqs_off()
4580 trace->softirq_disable_ip = ip; in lockdep_softirqs_off()
4581 trace->softirq_disable_event = ++trace->irq_events; in lockdep_softirqs_off()
4592 * lockdep_cleanup_dead_cpu - Ensure CPU lockdep state is cleanly stopped
4622 * If non-trylock use in a hardirq or softirq context, then in mark_usage()
4625 if (!hlock->trylock) { in mark_usage()
4626 if (hlock->read) { in mark_usage()
4631 if (curr->softirq_context) in mark_usage()
4639 if (curr->softirq_context) in mark_usage()
4650 if (!hlock->hardirqs_off && !hlock->sync) { in mark_usage()
4651 if (hlock->read) { in mark_usage()
4655 if (curr->softirqs_enabled) in mark_usage()
4663 if (curr->softirqs_enabled) in mark_usage()
4681 LOCK_CHAIN_SOFTIRQ_CONTEXT * !!task->softirq_context; in task_irq_context()
4687 unsigned int depth = curr->lockdep_depth; in separate_irq_context()
4695 prev_hlock = curr->held_locks + depth-1; in separate_irq_context()
4701 if (prev_hlock->irq_context != hlock->irq_context) in separate_irq_context()
4708 * Mark a lock with a usage bit, and validate the state transition:
4720 if (new_bit == LOCK_USED && this->read) in mark_lock()
4729 if (likely(hlock_class(this)->usage_mask & new_mask)) in mark_lock()
4737 if (unlikely(hlock_class(this)->usage_mask & new_mask)) in mark_lock()
4740 if (!hlock_class(this)->usage_mask) in mark_lock()
4743 hlock_class(this)->usage_mask |= new_mask; in mark_lock()
4746 if (!(hlock_class(this)->usage_traces[new_bit] = save_trace())) in mark_lock()
4784 if (curr->hardirq_threaded || curr->irq_config) in task_wait_context()
4788 } else if (curr->softirq_context) { in task_wait_context()
4815 pr_warn("-----------------------------\n"); in print_lock_invalid_wait_context()
4817 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr)); in print_lock_invalid_wait_context()
4823 pr_warn("context-{%d:%d}\n", curr_inner, curr_inner); in print_lock_invalid_wait_context()
4838 * This check validates we take locks in the right wait-type order; that is it
4852 u8 next_inner = hlock_class(next)->wait_type_inner; in check_wait_context()
4853 u8 next_outer = hlock_class(next)->wait_type_outer; in check_wait_context()
4857 if (!next_inner || next->trylock) in check_wait_context()
4866 for (depth = curr->lockdep_depth - 1; depth >= 0; depth--) { in check_wait_context()
4867 struct held_lock *prev = curr->held_locks + depth; in check_wait_context()
4868 if (prev->irq_context != next->irq_context) in check_wait_context()
4875 for (; depth < curr->lockdep_depth; depth++) { in check_wait_context()
4876 struct held_lock *prev = curr->held_locks + depth; in check_wait_context()
4878 u8 prev_inner = class->wait_type_inner; in check_wait_context()
4890 * Allow override for annotations -- this is typically in check_wait_context()
4894 if (unlikely(class->lock_type == LD_LOCK_WAIT_OVERRIDE)) in check_wait_context()
4933 * Initialize a lock instance's lock-class mapping info:
4942 lock->class_cache[i] = NULL; in lockdep_init_map_type()
4945 lock->cpu = raw_smp_processor_id(); in lockdep_init_map_type()
4952 lock->name = "NULL"; in lockdep_init_map_type()
4956 lock->name = name; in lockdep_init_map_type()
4958 lock->wait_type_outer = outer; in lockdep_init_map_type()
4959 lock->wait_type_inner = inner; in lockdep_init_map_type()
4960 lock->lock_type = lock_type; in lockdep_init_map_type()
4968 * Sanity check, the lock-class key must either have been allocated in lockdep_init_map_type()
4977 lock->key = key; in lockdep_init_map_type()
5007 struct lock_class *class = lock->class_cache[0]; in lockdep_set_lock_cmp_fn()
5017 WARN_ON(class->cmp_fn && class->cmp_fn != cmp_fn); in lockdep_set_lock_cmp_fn()
5018 WARN_ON(class->print_fn && class->print_fn != print_fn); in lockdep_set_lock_cmp_fn()
5020 class->cmp_fn = cmp_fn; in lockdep_set_lock_cmp_fn()
5021 class->print_fn = print_fn; in lockdep_set_lock_cmp_fn()
5045 pr_warn("----------------------------------\n"); in print_lock_nested_lock_not_held()
5047 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr)); in print_lock_nested_lock_not_held()
5051 pr_warn("%s\n", hlock->nest_lock->name); in print_lock_nested_lock_not_held()
5069 * We maintain the dependency maps and validate the locking attempt:
5091 if (unlikely(lock->key == &__lockdep_no_track__)) in __lock_acquire()
5094 if (!prove_locking || lock->key == &__lockdep_no_validate__) in __lock_acquire()
5098 class = lock->class_cache[subclass]; in __lock_acquire()
5112 printk("\nacquire class [%px] %s", class->key, class->name); in __lock_acquire()
5113 if (class->name_version > 1) in __lock_acquire()
5114 printk(KERN_CONT "#%d", class->name_version); in __lock_acquire()
5122 * (we dont increase the depth just yet, up until the in __lock_acquire()
5125 depth = curr->lockdep_depth; in __lock_acquire()
5127 * Ran out of static storage for our per-task lock stack again have we? in __lock_acquire()
5132 class_idx = class - lock_classes; in __lock_acquire()
5136 hlock = curr->held_locks + depth - 1; in __lock_acquire()
5137 if (hlock->class_idx == class_idx && nest_lock) { in __lock_acquire()
5141 if (!hlock->references) in __lock_acquire()
5142 hlock->references++; in __lock_acquire()
5144 hlock->references += references; in __lock_acquire()
5147 if (DEBUG_LOCKS_WARN_ON(hlock->references < references)) in __lock_acquire()
5154 hlock = curr->held_locks + depth; in __lock_acquire()
5161 hlock->class_idx = class_idx; in __lock_acquire()
5162 hlock->acquire_ip = ip; in __lock_acquire()
5163 hlock->instance = lock; in __lock_acquire()
5164 hlock->nest_lock = nest_lock; in __lock_acquire()
5165 hlock->irq_context = task_irq_context(curr); in __lock_acquire()
5166 hlock->trylock = trylock; in __lock_acquire()
5167 hlock->read = read; in __lock_acquire()
5168 hlock->check = check; in __lock_acquire()
5169 hlock->sync = !!sync; in __lock_acquire()
5170 hlock->hardirqs_off = !!hardirqs_off; in __lock_acquire()
5171 hlock->references = references; in __lock_acquire()
5173 hlock->waittime_stamp = 0; in __lock_acquire()
5174 hlock->holdtime_stamp = lockstat_clock(); in __lock_acquire()
5176 hlock->pin_count = pin_count; in __lock_acquire()
5193 * the hash, not class->key. in __lock_acquire()
5201 chain_key = curr->curr_chain_key; in __lock_acquire()
5211 hlock->prev_chain_key = chain_key; in __lock_acquire()
5218 if (nest_lock && !__lock_is_held(nest_lock, -1)) { in __lock_acquire()
5224 WARN_ON_ONCE(depth && !hlock_class(hlock - 1)->key); in __lock_acquire()
5225 WARN_ON_ONCE(!hlock_class(hlock)->key); in __lock_acquire()
5232 if (hlock->sync) in __lock_acquire()
5235 curr->curr_chain_key = chain_key; in __lock_acquire()
5236 curr->lockdep_depth++; in __lock_acquire()
5242 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) { in __lock_acquire()
5247 curr->lockdep_depth, MAX_LOCK_DEPTH); in __lock_acquire()
5257 if (unlikely(curr->lockdep_depth > max_lockdep_depth)) in __lock_acquire()
5258 max_lockdep_depth = curr->lockdep_depth; in __lock_acquire()
5278 pr_warn("-------------------------------------\n"); in print_unlock_imbalance_bug()
5280 curr->comm, task_pid_nr(curr)); in print_unlock_imbalance_bug()
5297 if (hlock->instance == lock) in match_held_lock()
5300 if (hlock->references) { in match_held_lock()
5301 const struct lock_class *class = lock->class_cache[0]; in match_held_lock()
5316 * References, but not a lock we're actually ref-counting? in match_held_lock()
5317 * State got messed up, follow the sites that change ->references in match_held_lock()
5320 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock)) in match_held_lock()
5323 if (hlock->class_idx == class - lock_classes) in match_held_lock()
5338 i = depth - 1; in find_held_lock()
5339 hlock = curr->held_locks + i; in find_held_lock()
5345 for (i--, prev_hlock = hlock--; in find_held_lock()
5347 i--, prev_hlock = hlock--) { in find_held_lock()
5351 if (prev_hlock->irq_context != hlock->irq_context) { in find_held_lock()
5375 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) { in reacquire_held_locks()
5376 switch (__lock_acquire(hlock->instance, in reacquire_held_locks()
5377 hlock_class(hlock)->subclass, in reacquire_held_locks()
5378 hlock->trylock, in reacquire_held_locks()
5379 hlock->read, hlock->check, in reacquire_held_locks()
5380 hlock->hardirqs_off, in reacquire_held_locks()
5381 hlock->nest_lock, hlock->acquire_ip, in reacquire_held_locks()
5382 hlock->references, hlock->pin_count, 0)) { in reacquire_held_locks()
5412 depth = curr->lockdep_depth; in __lock_set_class()
5427 lock->wait_type_inner, in __lock_set_class()
5428 lock->wait_type_outer, in __lock_set_class()
5429 lock->lock_type); in __lock_set_class()
5431 hlock->class_idx = class - lock_classes; in __lock_set_class()
5433 curr->lockdep_depth = i; in __lock_set_class()
5434 curr->curr_chain_key = hlock->prev_chain_key; in __lock_set_class()
5443 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged)) in __lock_set_class()
5458 depth = curr->lockdep_depth; in __lock_downgrade()
5472 curr->lockdep_depth = i; in __lock_downgrade()
5473 curr->curr_chain_key = hlock->prev_chain_key; in __lock_downgrade()
5475 WARN(hlock->read, "downgrading a read lock"); in __lock_downgrade()
5476 hlock->read = 1; in __lock_downgrade()
5477 hlock->acquire_ip = ip; in __lock_downgrade()
5490 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth)) in __lock_downgrade()
5497 * Remove the lock from the list of currently held locks - this gets
5512 depth = curr->lockdep_depth; in __lock_release()
5532 if (hlock->instance == lock) in __lock_release()
5535 WARN(hlock->pin_count, "releasing a pinned lock\n"); in __lock_release()
5537 if (hlock->references) { in __lock_release()
5538 hlock->references--; in __lock_release()
5539 if (hlock->references) { in __lock_release()
5555 curr->lockdep_depth = i; in __lock_release()
5556 curr->curr_chain_key = hlock->prev_chain_key; in __lock_release()
5562 if (i == depth-1) in __lock_release()
5570 * there's not N-1 bottles of beer left on the wall... in __lock_release()
5573 DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged); in __lock_release()
5589 for (i = 0; i < curr->lockdep_depth; i++) { in __lock_is_held()
5590 struct held_lock *hlock = curr->held_locks + i; in __lock_is_held()
5593 if (read == -1 || !!hlock->read == read) in __lock_is_held()
5612 for (i = 0; i < curr->lockdep_depth; i++) { in __lock_pin_lock()
5613 struct held_lock *hlock = curr->held_locks + i; in __lock_pin_lock()
5622 hlock->pin_count += cookie.val; in __lock_pin_lock()
5639 for (i = 0; i < curr->lockdep_depth; i++) { in __lock_repin_lock()
5640 struct held_lock *hlock = curr->held_locks + i; in __lock_repin_lock()
5643 hlock->pin_count += cookie.val; in __lock_repin_lock()
5659 for (i = 0; i < curr->lockdep_depth; i++) { in __lock_unpin_lock()
5660 struct held_lock *hlock = curr->held_locks + i; in __lock_unpin_lock()
5663 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n")) in __lock_unpin_lock()
5666 hlock->pin_count -= cookie.val; in __lock_unpin_lock()
5668 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n")) in __lock_unpin_lock()
5669 hlock->pin_count = 0; in __lock_unpin_lock()
5679 * Check whether we follow the irq-flags state precisely:
5692 printk("possible reason: unannotated irqs-off.\n"); in check_flags()
5696 printk("possible reason: unannotated irqs-on.\n"); in check_flags()
5702 * We dont accurately track softirq state in e.g. in check_flags()
5709 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled); in check_flags()
5712 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled); in check_flags()
5773 * READ locks, there is no deadlock possible -- RCU. in verify_lock_unused()
5775 if (!hlock->read) in verify_lock_unused()
5778 if (!(class->usage_mask & mask)) in verify_lock_unused()
5781 hlock->class_idx = class - lock_classes; in verify_lock_unused()
5813 * We are not always called with irqs disabled - do that here,
5865 lock->key == &__lockdep_no_track__)) in lock_release()
5880 * lock_sync() - A special annotation for synchronize_{s,}rcu()-like API.
6002 pr_warn("---------------------------------\n"); in print_lock_contention_bug()
6004 curr->comm, task_pid_nr(curr)); in print_lock_contention_bug()
6027 depth = curr->lockdep_depth; in __lock_contended()
6035 if (unlikely(lock->key == &__lockdep_no_track__)) in __lock_contended()
6044 if (hlock->instance != lock) in __lock_contended()
6047 hlock->waittime_stamp = lockstat_clock(); in __lock_contended()
6049 contention_point = lock_point(hlock_class(hlock)->contention_point, ip); in __lock_contended()
6050 contending_point = lock_point(hlock_class(hlock)->contending_point, in __lock_contended()
6051 lock->ip); in __lock_contended()
6055 stats->contention_point[contention_point]++; in __lock_contended()
6057 stats->contending_point[contending_point]++; in __lock_contended()
6058 if (lock->cpu != smp_processor_id()) in __lock_contended()
6059 stats->bounces[bounce_contended + !!hlock->read]++; in __lock_contended()
6072 depth = curr->lockdep_depth; in __lock_acquired()
6080 if (unlikely(lock->key == &__lockdep_no_track__)) in __lock_acquired()
6089 if (hlock->instance != lock) in __lock_acquired()
6093 if (hlock->waittime_stamp) { in __lock_acquired()
6095 waittime = now - hlock->waittime_stamp; in __lock_acquired()
6096 hlock->holdtime_stamp = now; in __lock_acquired()
6101 if (hlock->read) in __lock_acquired()
6102 lock_time_inc(&stats->read_waittime, waittime); in __lock_acquired()
6104 lock_time_inc(&stats->write_waittime, waittime); in __lock_acquired()
6106 if (lock->cpu != cpu) in __lock_acquired()
6107 stats->bounces[bounce_acquired + !!hlock->read]++; in __lock_acquired()
6109 lock->cpu = cpu; in __lock_acquired()
6110 lock->ip = ip; in __lock_acquired()
6162 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock)); in lockdep_reset()
6180 for (i = chain->base; i < chain->base + chain->depth; i++) { in remove_class_from_lock_chain()
6181 if (chain_hlock_class_idx(chain_hlocks[i]) != class - lock_classes) in remove_class_from_lock_chain()
6193 free_chain_hlocks(chain->base, chain->depth); in remove_class_from_lock_chain()
6195 WRITE_ONCE(chain->chain_key, INITIAL_CHAIN_KEY); in remove_class_from_lock_chain()
6196 dec_chains(chain->irq_context); in remove_class_from_lock_chain()
6202 hlist_del_rcu(&chain->entry); in remove_class_from_lock_chain()
6203 __set_bit(chain - lock_chains, pf->lock_chains_being_freed); in remove_class_from_lock_chain()
6232 WARN_ON_ONCE(!class->key); in zap_class()
6240 if (entry->class != class && entry->links_to != class) in zap_class()
6243 nr_list_entries--; in zap_class()
6244 list_del_rcu(&entry->entry); in zap_class()
6246 if (list_empty(&class->locks_after) && in zap_class()
6247 list_empty(&class->locks_before)) { in zap_class()
6248 list_move_tail(&class->lock_entry, &pf->zapped); in zap_class()
6249 hlist_del_rcu(&class->hash_entry); in zap_class()
6250 WRITE_ONCE(class->key, NULL); in zap_class()
6251 WRITE_ONCE(class->name, NULL); in zap_class()
6252 /* Class allocated but not used, -1 in nr_unused_locks */ in zap_class()
6253 if (class->usage_mask == 0) in zap_class()
6255 nr_lock_classes--; in zap_class()
6256 __clear_bit(class - lock_classes, lock_classes_in_use); in zap_class()
6257 if (class - lock_classes == max_lock_class_idx) in zap_class()
6258 max_lock_class_idx--; in zap_class()
6261 class->name); in zap_class()
6270 WARN_ON_ONCE(!class->lock_entry.next); in reinit_class()
6271 WARN_ON_ONCE(!list_empty(&class->locks_after)); in reinit_class()
6272 WARN_ON_ONCE(!list_empty(&class->locks_before)); in reinit_class()
6274 WARN_ON_ONCE(!class->lock_entry.next); in reinit_class()
6275 WARN_ON_ONCE(!list_empty(&class->locks_after)); in reinit_class()
6276 WARN_ON_ONCE(!list_empty(&class->locks_before)); in reinit_class()
6307 if (list_empty(&pf->zapped)) in prepare_call_rcu_zapped()
6328 list_for_each_entry(class, &pf->zapped, lock_entry) in __free_zapped_classes()
6331 list_splice_init(&pf->zapped, &free_lock_classes); in __free_zapped_classes()
6335 pf->lock_chains_being_freed, ARRAY_SIZE(lock_chains)); in __free_zapped_classes()
6336 bitmap_clear(pf->lock_chains_being_freed, 0, ARRAY_SIZE(lock_chains)); in __free_zapped_classes()
6387 if (!within(class->key, start, size) && in __lockdep_free_key_range()
6388 !within(class->name, start, size)) in __lockdep_free_key_range()
6397 * freed; and possibly re-used by other modules.
6400 * guaranteed nobody will look up these exact classes -- they're properly dead
6457 * Check whether any element of the @lock->class_cache[] array refers to a
6471 if (lock->class_cache[j] == class) in lock_class_cache_is_registered()
6584 hlist_del_rcu(&k->hash_entry); in lockdep_unregister_key()
6601 /* Wait until is_dynamic_key() has finished accessing k->hash_entry. */ in lockdep_unregister_key()
6641 pr_info(" per task-struct memory footprint: %zu bytes\n", in lockdep_init()
6642 sizeof(((struct task_struct *)NULL)->held_locks)); in lockdep_init()
6660 pr_warn("-------------------------\n"); in print_freed_lock_bug()
6661 pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n", in print_freed_lock_bug()
6662 curr->comm, task_pid_nr(curr), mem_from, mem_to-1); in print_freed_lock_bug()
6681 * is destroyed or reinitialized - this code checks whether there is
6695 for (i = 0; i < curr->lockdep_depth; i++) { in debug_check_no_locks_freed()
6696 hlock = curr->held_locks + i; in debug_check_no_locks_freed()
6698 if (not_in_range(mem_from, mem_len, hlock->instance, in debug_check_no_locks_freed()
6699 sizeof(*hlock->instance))) in debug_check_no_locks_freed()
6721 current->comm, task_pid_nr(current)); in print_held_locks_bug()
6723 pr_warn("------------------------------------\n"); in print_held_locks_bug()
6733 if (unlikely(current->lockdep_depth > 0)) in debug_check_no_locks_held()
6751 if (!p->lockdep_depth) in debug_show_all_locks()
6783 if (unlikely(curr->lockdep_depth)) { in lockdep_sys_exit()
6791 pr_warn("------------------------------------------------\n"); in lockdep_sys_exit()
6793 curr->comm, curr->pid); in lockdep_sys_exit()
6817 pr_warn("-----------------------------\n"); in lockdep_rcu_suspicious()
6828 * If a CPU is in the RCU-free window in idle (ie: in the section in lockdep_rcu_suspicious()
6834 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well in lockdep_rcu_suspicious()
6836 * choice here: we need to keep an RCU-free window in idle where in lockdep_rcu_suspicious()