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
124 if (current->lockdep_recursion) in lockdep_enabled()
135 * to use a raw spinlock - we really dont want the spinlock
178 * walking the graph we dont change it (while the other in graph_lock()
217 #define KEYHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
228 unsigned int class_idx = hlock->class_idx; in hlock_class()
230 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfield */ in hlock_class()
242 * At this point, if the passed hlock->class_idx is still garbage, in hlock_class()
274 if (time > lt->max) in lock_time_inc()
275 lt->max = time; in lock_time_inc()
277 if (time < lt->min || !lt->nr) in lock_time_inc()
278 lt->min = time; in lock_time_inc()
280 lt->total += time; in lock_time_inc()
281 lt->nr++; in lock_time_inc()
286 if (!src->nr) in lock_time_add()
289 if (src->max > dst->max) in lock_time_add()
290 dst->max = src->max; in lock_time_add()
292 if (src->min < dst->min || !dst->nr) in lock_time_add()
293 dst->min = src->min; in lock_time_add()
295 dst->total += src->total; in lock_time_add()
296 dst->nr += src->nr; in lock_time_add()
307 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; in lock_stats()
310 stats.contention_point[i] += pcs->contention_point[i]; in lock_stats()
313 stats.contending_point[i] += pcs->contending_point[i]; in lock_stats()
315 lock_time_add(&pcs->read_waittime, &stats.read_waittime); in lock_stats()
316 lock_time_add(&pcs->write_waittime, &stats.write_waittime); in lock_stats()
318 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime); in lock_stats()
319 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime); in lock_stats()
322 stats.bounces[i] += pcs->bounces[i]; in lock_stats()
334 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes]; in clear_lock_stats()
338 memset(class->contention_point, 0, sizeof(class->contention_point)); in clear_lock_stats()
339 memset(class->contending_point, 0, sizeof(class->contending_point)); in clear_lock_stats()
344 return &this_cpu_ptr(cpu_lock_stats)[class - lock_classes]; in get_lock_stats()
355 holdtime = lockstat_clock() - hlock->holdtime_stamp; in lock_release_holdtime()
358 if (hlock->read) in lock_release_holdtime()
359 lock_time_inc(&stats->read_holdtime, holdtime); in lock_release_holdtime()
361 lock_time_inc(&stats->write_holdtime, holdtime); in lock_release_holdtime()
379 * struct pending_free - information about data structures about to be freed
390 * struct delayed_free - data structures used for delayed freeing
408 * The lockdep classes are in a hash-table as well, for fast lookup:
410 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
418 * We put the lock dependency chains into a hash-table as well, to cache
421 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
435 return (hlock->class_idx | (hlock->read << MAX_LOCKDEP_KEYS_BITS)); in hlock_id()
440 return hlock_id & (MAX_LOCKDEP_KEYS - 1); in chain_hlock_class_idx()
446 * It's a 64-bit hash, because it's important for the keys to be
460 task->lockdep_depth = 0; /* no locks held yet */ in lockdep_init_task()
461 task->curr_chain_key = INITIAL_CHAIN_KEY; in lockdep_init_task()
462 task->lockdep_recursion = 0; in lockdep_init_task()
504 if (class->name_version == 1 && in class_filter()
505 !strcmp(class->name, "lockname")) in class_filter()
507 if (class->name_version == 1 && in class_filter()
508 !strcmp(class->name, "&struct->lockfield")) in class_filter()
537 * struct lock_trace - single stack backtrace
552 * Stack-trace: sequence of lock_trace structures. Protected by the graph_lock.
559 return t1->hash == t2->hash && t1->nr_entries == t2->nr_entries && in traces_identical()
560 memcmp(t1->entries, t2->entries, in traces_identical()
561 t1->nr_entries * sizeof(t1->entries[0])) == 0; in traces_identical()
575 max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries - in save_trace()
589 trace->nr_entries = stack_trace_save(trace->entries, max_entries, 3); in save_trace()
591 hash = jhash(trace->entries, trace->nr_entries * in save_trace()
592 sizeof(trace->entries[0]), 0); in save_trace()
593 trace->hash = hash; in save_trace()
594 hash_head = stack_trace_hash + (hash & (STACK_TRACE_HASH_SIZE - 1)); in save_trace()
599 nr_stack_trace_entries += LOCK_TRACE_SIZE_IN_LONGS + trace->nr_entries; in save_trace()
600 hlist_add_head(&trace->hash_entry, hash_head); in save_trace()
653 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
654 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
655 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
656 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
666 [LOCK_USAGE_STATES] = "IN-NMI",
692 * - '+': irq is enabled and not in irq context in get_usage_char()
693 * - '-': in irq context and irq is disabled in get_usage_char()
694 * - '?': in irq context and irq is enabled in get_usage_char()
696 if (class->usage_mask & lock_flag(bit + LOCK_USAGE_DIR_MASK)) { in get_usage_char()
698 if (class->usage_mask & lock_flag(bit)) in get_usage_char()
700 } else if (class->usage_mask & lock_flag(bit)) in get_usage_char()
701 c = '-'; in get_usage_char()
724 name = class->name; in __print_lock_name()
726 name = __get_key_name(class->key, str); in __print_lock_name()
730 if (class->name_version > 1) in __print_lock_name()
731 printk(KERN_CONT "#%d", class->name_version); in __print_lock_name()
732 if (class->subclass) in __print_lock_name()
733 printk(KERN_CONT "/%d", class->subclass); in __print_lock_name()
734 if (hlock && class->print_fn) in __print_lock_name()
735 class->print_fn(hlock->instance); in __print_lock_name()
747 printk(KERN_CONT "){%s}-{%d:%d}", usage, in print_lock_name()
748 class->wait_type_outer ?: class->wait_type_inner, in print_lock_name()
749 class->wait_type_inner); in print_lock_name()
757 name = lock->name; in print_lockdep_cache()
759 name = __get_key_name(lock->key->subkeys, str); in print_lockdep_cache()
783 printk(KERN_CONT "%px", hlock->instance); in print_lock()
785 printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip); in print_lock()
790 int i, depth = READ_ONCE(p->lockdep_depth); in lockdep_print_held_locks()
793 printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p)); in lockdep_print_held_locks()
796 str_plural(depth), p->comm, task_pid_nr(p)); in lockdep_print_held_locks()
805 print_lock(p->held_locks + i); in lockdep_print_held_locks()
811 printk("%s %.*s %s\n", init_utsname()->release, in print_kernel_ident()
812 (int)strcspn(init_utsname()->version, " "), in print_kernel_ident()
813 init_utsname()->version, in print_kernel_ident()
852 * in-kernel percpu var? in static_obj()
866 * class->name_version generation counter. The caller must hold the graph
874 if (!new_class->name) in count_matching_names()
878 if (new_class->key - new_class->subclass == class->key) in count_matching_names()
879 return class->name_version; in count_matching_names()
880 if (class->name && !strcmp(class->name, new_class->name)) in count_matching_names()
881 count = max(count, class->name_version); in count_matching_names()
887 /* used from NMI context -- must be lockless */
913 if (unlikely(!lock->key)) in look_up_lock_class()
917 * NOTE: the class-key must be unique. For dynamic locks, a static in look_up_lock_class()
919 * (or spin_lock_init()) call - which acts as the key. For static in look_up_lock_class()
925 key = lock->key->subkeys + subclass; in look_up_lock_class()
936 if (class->key == key) { in look_up_lock_class()
941 WARN_ONCE(class->name != lock->name && in look_up_lock_class()
942 lock->key != &__lockdep_no_validate__, in look_up_lock_class()
944 lock->name, lock->key, class->name); in look_up_lock_class()
953 * Static locks do not have their class-keys yet - for them the key is
973 lock->key = (void *)can_addr; in assign_lock_key()
975 lock->key = (void *)can_addr; in assign_lock_key()
977 lock->key = (void *)lock; in assign_lock_key()
979 /* Debug-check: all keys must be persistent! */ in assign_lock_key()
982 pr_err("INFO: trying to register non-static key.\n"); in assign_lock_key()
1020 if (in_list(e, &class->locks_after) || in in_any_class_list()
1021 in_list(e, &class->locks_before)) in in_any_class_list()
1032 if (e->links_to != c) { in class_lock_list_valid()
1034 c->name ? : "(?)", in class_lock_list_valid()
1035 (unsigned long)(e - list_entries), in class_lock_list_valid()
1036 e->links_to && e->links_to->name ? in class_lock_list_valid()
1037 e->links_to->name : "(?)", in class_lock_list_valid()
1038 e->class && e->class->name ? e->class->name : in class_lock_list_valid()
1056 for (i = chain->base; i < chain->base + chain->depth; i++) in check_lock_chain_key()
1062 if (chain->chain_key != chain_key) { in check_lock_chain_key()
1064 (unsigned long long)(chain - lock_chains), in check_lock_chain_key()
1065 (unsigned long long)chain->chain_key, in check_lock_chain_key()
1079 if (in_list(&class->lock_entry, &pf->zapped)) in in_any_zapped_class_list()
1097 if (!in_list(&class->lock_entry, &all_lock_classes) && in __check_data_structures()
1098 !in_list(&class->lock_entry, &free_lock_classes) && in __check_data_structures()
1101 class, class->name ? : "(?)"); in __check_data_structures()
1109 if (!class_lock_list_valid(class, &class->locks_before)) in __check_data_structures()
1111 if (!class_lock_list_valid(class, &class->locks_after)) in __check_data_structures()
1130 if (!in_any_class_list(&e->entry)) { in __check_data_structures()
1132 (unsigned int)(e - list_entries), in __check_data_structures()
1133 e->class->name ? : "(?)", in __check_data_structures()
1134 e->links_to->name ? : "(?)"); in __check_data_structures()
1145 if (in_any_class_list(&e->entry)) { in __check_data_structures()
1147 (unsigned int)(e - list_entries), in __check_data_structures()
1148 e->class && e->class->name ? e->class->name : in __check_data_structures()
1150 e->links_to && e->links_to->name ? in __check_data_structures()
1151 e->links_to->name : "(?)"); in __check_data_structures()
1240 hlist_add_head_rcu(&key->hash_entry, hash_head); in lockdep_register_key()
1261 * a use-after-free in that case by returning early. in is_dynamic_key()
1281 * Register a lock's class in the hash-table, if the class is not present
1299 if (!lock->key) { in register_lock_class()
1302 } else if (!static_obj(lock->key) && !is_dynamic_key(lock->key)) { in register_lock_class()
1306 key = lock->key->subkeys + subclass; in register_lock_class()
1313 * We have to do the hash-walk again, to avoid races in register_lock_class()
1317 if (class->key == key) in register_lock_class()
1338 __set_bit(class - lock_classes, lock_classes_in_use); in register_lock_class()
1340 class->key = key; in register_lock_class()
1341 class->name = lock->name; in register_lock_class()
1342 class->subclass = subclass; in register_lock_class()
1343 WARN_ON_ONCE(!list_empty(&class->locks_before)); in register_lock_class()
1344 WARN_ON_ONCE(!list_empty(&class->locks_after)); in register_lock_class()
1345 class->name_version = count_matching_names(class); in register_lock_class()
1346 class->wait_type_inner = lock->wait_type_inner; in register_lock_class()
1347 class->wait_type_outer = lock->wait_type_outer; in register_lock_class()
1348 class->lock_type = lock->lock_type; in register_lock_class()
1350 * We use RCU's safe list-add method to make in register_lock_class()
1351 * parallel walking of the hash-list safe: in register_lock_class()
1353 hlist_add_head_rcu(&class->hash_entry, hash_head); in register_lock_class()
1358 list_move_tail(&class->lock_entry, &all_lock_classes); in register_lock_class()
1359 idx = class - lock_classes; in register_lock_class()
1367 printk("\nnew class %px: %s", class->key, class->name); in register_lock_class()
1368 if (class->name_version > 1) in register_lock_class()
1369 printk(KERN_CONT "#%d", class->name_version); in register_lock_class()
1383 lock->class_cache[0] = class; in register_lock_class()
1385 lock->class_cache[subclass] = class; in register_lock_class()
1389 * hash but the subclass -- which is hashed in -- didn't match. in register_lock_class()
1391 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass)) in register_lock_class()
1432 * Lock not present yet - get a new dependency struct and in add_lock_to_list()
1439 entry->class = this; in add_lock_to_list()
1440 entry->links_to = links_to; in add_lock_to_list()
1441 entry->dep = dep; in add_lock_to_list()
1442 entry->distance = distance; in add_lock_to_list()
1443 entry->trace = trace; in add_lock_to_list()
1446 * iteration is under RCU-sched; see look_up_lock_class() and in add_lock_to_list()
1449 list_add_tail_rcu(&entry->entry, head); in add_lock_to_list()
1458 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
1462 * breadth-first search (BFS) algorithm, by which we can determine
1465 * indicates that adding the <prev> -> <next> lock dependency will
1466 * produce a circle in the graph. Breadth-first search instead of
1467 * depth-first search is used in order to find the shortest (circular)
1483 cq->front = cq->rear = 0; in __cq_init()
1489 return (cq->front == cq->rear); in __cq_empty()
1494 return ((cq->rear + 1) & CQ_MASK) == cq->front; in __cq_full()
1500 return -1; in __cq_enqueue()
1502 cq->element[cq->rear] = elem; in __cq_enqueue()
1503 cq->rear = (cq->rear + 1) & CQ_MASK; in __cq_enqueue()
1518 lock = cq->element[cq->front]; in __cq_dequeue()
1519 cq->front = (cq->front + 1) & CQ_MASK; in __cq_dequeue()
1526 return (cq->rear - cq->front) & CQ_MASK; in __cq_get_elem_count()
1531 lock->class->dep_gen_id = lockdep_dependency_gen_id; in mark_lock_accessed()
1537 lock->parent = parent; in visit_lock_entry()
1542 return lock->class->dep_gen_id == lockdep_dependency_gen_id; in lock_accessed()
1547 return child->parent; in get_lock_parent()
1571 void *lock_class = lock->class; in get_dep_list()
1592 BFS_EINVALIDNODE = -2,
1593 BFS_EQUEUEFULL = -1,
1609 * For dependency @prev -> @next:
1611 * SR: @prev is shared reader (->read != 0) and @next is recursive reader
1612 * (->read == 2)
1613 * ER: @prev is exclusive locker (->read == 0) and @next is recursive reader
1614 * SN: @prev is shared reader and @next is non-recursive locker (->read != 2)
1615 * EN: @prev is exclusive locker and @next is non-recursive locker
1618 * bit0 is prev->read == 0
1619 * bit1 is next->read != 2
1634 return (prev->read == 0) + ((next->read != 2) << 1); in __calc_dep_bit()
1649 return (next->read != 2) + ((prev->read == 0) << 1); in __calc_dep_bitb()
1664 lock->class = class; in __bfs_init_root()
1665 lock->parent = NULL; in __bfs_init_root()
1666 lock->only_xr = 0; in __bfs_init_root()
1673 * ->only_xr of the initial lock node is set to @hlock->read == 2, to make sure
1674 * that <prev> -> @hlock and @hlock -> <whatever __bfs() found> is not -(*R)->
1675 * and -(S*)->.
1681 lock->only_xr = (hlock->read == 2); in bfs_init_root()
1687 * ->only_xr of the initial lock node is set to @hlock->read != 0, to make sure
1688 * that <next> -> @hlock and @hlock -> <whatever backwards BFS found> is not
1689 * -(*S)-> and -(R*)-> (reverse order of -(*R)-> and -(S*)->).
1695 lock->only_xr = (hlock->read != 0); in bfs_init_rootb()
1700 if (!lock || !lock->parent) in __bfs_next()
1703 return list_next_or_null_rcu(get_dep_list(lock->parent, offset), in __bfs_next()
1704 &lock->entry, struct lock_list, entry); in __bfs_next()
1708 * Breadth-First Search to find a strong path in the dependency graph.
1721 * dependencies as -(*R)-> -(S*)->, please see:
1723 * Documentation/locking/lockdep-design.rst
1730 * has -(*R)-> in the search, and if it does (prev only has -(*R)->), we
1731 * filter out any -(S*)-> in the current dependency and after that, the
1732 * ->only_xr is set according to whether we only have -(*R)-> left.
1754 if (!lock->class) in __bfs()
1762 * @lock->class->locks_{after,before}) we skip, otherwise go in __bfs()
1775 if (lock->parent) { /* Parent exists, check prev dependency */ in __bfs()
1776 u8 dep = lock->dep; in __bfs()
1777 bool prev_only_xr = lock->parent->only_xr; in __bfs()
1780 * Mask out all -(S*)-> if we only have *R in previous in __bfs()
1781 * step, because -(*R)-> -(S*)-> don't make up a strong in __bfs()
1791 /* If there are only -(*R)-> left, set that for the next step */ in __bfs()
1792 lock->only_xr = !(dep & (DEP_SN_MASK | DEP_EN_MASK)); in __bfs()
1869 stack_trace_print(trace->entries, trace->nr_entries, spaces); in print_lock_trace()
1881 printk("\n-> #%u", depth); in print_circular_bug_entry()
1882 print_lock_name(NULL, target->class); in print_circular_bug_entry()
1884 print_lock_trace(target->trace, 6); in print_circular_bug_entry()
1894 struct lock_class *parent = prt->class; in print_circular_lock_scenario()
1895 int src_read = src->read; in print_circular_lock_scenario()
1896 int tgt_read = tgt->read; in print_circular_lock_scenario()
1914 printk(KERN_CONT " --> "); in print_circular_lock_scenario()
1916 printk(KERN_CONT " --> "); in print_circular_lock_scenario()
1923 printk(" ---- ----\n"); in print_circular_lock_scenario()
1938 else if (src->sync) in print_circular_lock_scenario()
1965 pr_warn("------------------------------------------------------\n"); in print_circular_bug_header()
1967 curr->comm, task_pid_nr(curr)); in print_circular_bug_header()
1980 * We are about to add A -> B into the dependency graph, and in __bfs() a
1981 * strong dependency path A -> .. -> B is found: hlock_class equals
1982 * entry->class.
1984 * If A -> .. -> B can replace A -> B in any __bfs() search (means the former
1985 * is _stronger_ than or equal to the latter), we consider A -> B as redundant.
1986 * For example if A -> .. -> B is -(EN)-> (i.e. A -(E*)-> .. -(*N)-> B), and A
1987 * -> B is -(ER)-> or -(EN)->, then we don't need to add A -> B into the
1988 * dependency graph, as any strong path ..-> A -> B ->.. we can get with
1989 * having dependency A -> B, we could already get a equivalent path ..-> A ->
1990 * .. -> B -> .. with A -> .. -> B. Therefore A -> B is redundant.
1992 * We need to make sure both the start and the end of A -> .. -> B is not
1993 * weaker than A -> B. For the start part, please see the comment in
1998 * a) A -> B is -(*R)-> (everything is not weaker than that)
2002 * b) A -> .. -> B is -(*N)-> (nothing is stronger than this)
2009 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */ in hlock_equal()
2010 (hlock->read == 2 || /* A -> B is -(*R)-> */ in hlock_equal()
2011 !entry->only_xr); /* A -> .. -> B is -(*N)-> */ in hlock_equal()
2015 * We are about to add B -> A into the dependency graph, and in __bfs() a
2016 * strong dependency path A -> .. -> B is found: hlock_class equals
2017 * entry->class.
2019 * We will have a deadlock case (conflict) if A -> .. -> B -> A is a strong
2024 * a) B -> A is -(E*)->
2028 * b) A -> .. -> B is -(*N)-> (i.e. A -> .. -(*N)-> B)
2030 * as then we don't have -(*R)-> -(S*)-> in the cycle.
2036 return hlock_class(hlock) == entry->class && /* Found A -> .. -> B */ in hlock_conflict()
2037 (hlock->read == 0 || /* B -> A is -(E*)-> */ in hlock_conflict()
2038 !entry->only_xr); /* A -> .. -> B is -(*N)-> */ in hlock_conflict()
2054 this->trace = save_trace(); in print_circular_bug()
2055 if (!this->trace) in print_circular_bug()
2068 print_circular_bug_entry(parent, --depth); in print_circular_bug()
2090 * Breadth-first-search failed, graph got corrupted? in print_bfs_bug()
2180 * <target> -> <src> dependency.
2208 if (src->class_idx == target->class_idx) in check_noncircular()
2222 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
2224 * A irq safe->unsafe deadlock happens with the following conditions:
2226 * 1) We have a strong dependency path A -> ... -> B
2229 * irq can create a new dependency B -> A (consider the case that a holder
2232 * 3) the dependency circle A -> ... -> B -> A we get from 1) and 2) is a
2236 * a) if A -> B is -(*N)->, then B -> A could be any type, so any
2238 * b) if A -> B is -(*R)->, then B -> A must be -(E*)->, so only
2242 * c) if A -> B is -(E*)->, then B -> A could be any type, so any
2244 * d) if A -> B is -(S*)->, then B -> A must be -(*N)->, so only
2249 * There is a strong dependency path in the dependency graph: A -> B, and now
2251 * safe->unsafe bugs.
2253 * Note that usage_accumulate() is used in backwards search, so ->only_xr
2254 * stands for whether A -> B only has -(S*)-> (in this case ->only_xr is true).
2256 * As above, if only_xr is false, which means A -> B has -(E*)-> dependency
2262 if (!entry->only_xr) in usage_accumulate()
2263 *(unsigned long *)mask |= entry->class->usage_mask; in usage_accumulate()
2265 *(unsigned long *)mask |= (entry->class->usage_mask & LOCKF_IRQ); in usage_accumulate()
2271 * There is a strong dependency path in the dependency graph: A -> B, and now
2273 * i.e. which usage bit of B may introduce safe->unsafe deadlocks.
2275 * As above, if only_xr is false, which means A -> B has -(*N)-> dependency
2281 if (!entry->only_xr) in usage_match()
2282 return !!(entry->class->usage_mask & *(unsigned long *)mask); in usage_match()
2284 return !!((entry->class->usage_mask & LOCKF_IRQ) & *(unsigned long *)mask); in usage_match()
2289 if (entry->class->lock_type == LD_LOCK_NORMAL) in usage_skip()
2305 * where lock(B) cannot sleep, and we have a dependency B -> ... -> A. in usage_skip()
2308 * have the observation for any lock chain L1 -> ... -> Ln, for any in usage_skip()
2313 * way the local_lock() exists in the dependency B -> ... -> A. in usage_skip()
2318 if (entry->class->lock_type == LD_LOCK_PERCPU && in usage_skip()
2319 DEBUG_LOCKS_WARN_ON(entry->class->wait_type_inner < LD_WAIT_CONFIG)) in usage_skip()
2323 * Skip WAIT_OVERRIDE for irq inversion detection -- it's not actually in usage_skip()
2331 * Find a node in the forwards-direction dependency sub-graph starting
2332 * at @root->class that matches @bit.
2351 * Find a node in the backwards-direction dependency sub-graph starting
2352 * at @root->class that matches @bit.
2371 printk("%*s->", depth, ""); in print_lock_class_header()
2379 if (class->usage_mask & (1 << bit)) { in print_lock_class_header()
2384 print_lock_trace(class->usage_traces[bit], len); in print_lock_class_header()
2390 depth, "", class->key, class->key); in print_lock_class_header()
2396 * After BFS we get a lock dependency path (linked via ->parent of lock_list),
2402 * for a lock dependency A -> B, there are two lock_lists:
2404 * a) lock_list in the ->locks_after list of A, whose ->class is B and
2405 * ->links_to is A. In this case, we can say the lock_list is
2406 * "A -> B" (forwards case).
2408 * b) lock_list in the ->locks_before list of B, whose ->class is A
2409 * and ->links_to is B. In this case, we can say the lock_list is
2410 * "B <- A" (bacwards case).
2412 * The ->trace of both a) and b) point to the call trace where B was
2418 * ->class is A, as a result BFS will search all dependencies starting with
2419 * A, e.g. A -> B or A -> C.
2421 * The notation of a forwards helper lock_list is like "-> A", which means
2422 * we should search the forwards dependencies starting with "A", e.g A -> B
2423 * or A -> C.
2425 * The notation of a bacwards helper lock_list is like "<- B", which means
2427 * B <- A or B <- C.
2438 * ->parent ->parent
2439 * | lock_list | <--------- | lock_list | ... | lock_list | <--------- | lock_list |
2440 * | -> L1 | | L1 -> L2 | ... |Ln-2 -> Ln-1| | Ln-1 -> Ln|
2442 * , so it's natural that we start from @leaf and print every ->class and
2443 * ->trace until we reach the @root.
2456 print_lock_class_header(entry->class, depth); in print_shortest_lock_dependencies()
2458 print_lock_trace(entry->trace, 2); in print_shortest_lock_dependencies()
2467 depth--; in print_shortest_lock_dependencies()
2479 * ->parent ->parent
2480 * | lock_list | ---------> | lock_list | ... | lock_list | ---------> | lock_list |
2481 * | L2 <- L1 | | L3 <- L2 | ... | Ln <- Ln-1 | | <- Ln |
2484 * dependency path L1 -> L2 -> .. -> Ln in the non-reverse order.
2486 * Another thing to notice here is that ->class of L2 <- L1 is L1, while the
2487 * ->trace of L2 <- L1 is the call trace of L2, in fact we don't have the call
2503 print_lock_class_header(entry->class, depth); in print_shortest_lock_dependencies_backwards()
2514 trace = entry->trace; in print_shortest_lock_dependencies_backwards()
2522 depth--; in print_shortest_lock_dependencies_backwards()
2532 struct lock_class *safe_class = safe_entry->class; in print_irq_lock_scenario()
2533 struct lock_class *unsafe_class = unsafe_entry->class; in print_irq_lock_scenario()
2555 printk(KERN_CONT " --> "); in print_irq_lock_scenario()
2557 printk(KERN_CONT " --> "); in print_irq_lock_scenario()
2564 printk(" ---- ----\n"); in print_irq_lock_scenario()
2601 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n", in print_bad_irq_dependency()
2604 pr_warn("-----------------------------------------------------\n"); in print_bad_irq_dependency()
2606 curr->comm, task_pid_nr(curr), in print_bad_irq_dependency()
2608 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT, in print_bad_irq_dependency()
2610 curr->softirqs_enabled); in print_bad_irq_dependency()
2617 pr_cont(" ->"); in print_bad_irq_dependency()
2621 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n", in print_bad_irq_dependency()
2623 print_lock_name(NULL, backwards_entry->class); in print_bad_irq_dependency()
2624 pr_warn("\n... which became %s-irq-safe at:\n", irqclass); in print_bad_irq_dependency()
2626 print_lock_trace(backwards_entry->class->usage_traces[bit1], 1); in print_bad_irq_dependency()
2628 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass); in print_bad_irq_dependency()
2629 print_lock_name(NULL, forwards_entry->class); in print_bad_irq_dependency()
2630 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass); in print_bad_irq_dependency()
2633 print_lock_trace(forwards_entry->class->usage_traces[bit2], 1); in print_bad_irq_dependency()
2641 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass); in print_bad_irq_dependency()
2645 pr_warn(" and %s-irq-unsafe lock:\n", irqclass); in print_bad_irq_dependency()
2646 next_root->trace = save_trace(); in print_bad_irq_dependency()
2647 if (!next_root->trace) in print_bad_irq_dependency()
2666 __stringify(__STATE)"-READ",
2684 * bit2-n: state
2699 * right shift of the mask transforms the individual bitnrs as -1 and
2740 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*
2741 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*
2742 * LOCK_USED_IN_IRQ_* -> LOCK_ENABLED_IRQ_*_READ
2743 * LOCK_USED_IN_IRQ_*_READ -> LOCK_ENABLED_IRQ_*_READ
2807 return -1; in find_exclusive_match()
2811 * Prove that the new dependency does not connect a hardirq-safe(-read)
2812 * lock with a hardirq-unsafe lock - to achieve this we search
2813 * the backwards-subgraph starting at <prev>, and the
2814 * forwards-subgraph starting at <next>:
2866 * When trying to add A -> B to the graph, we find that there is a in check_irq_usage()
2867 * hardirq-safe L, that L -> ... -> A, and another hardirq-unsafe M, in check_irq_usage()
2868 * that B -> ... -> M. However M is **softirq-safe**, if we use exact in check_irq_usage()
2870 * **softirq-unsafe** and N -> ... -> A, however N -> .. -> M will not in check_irq_usage()
2873 backward_mask = original_mask(target_entry1->class->usage_mask & LOCKF_ENABLED_IRQ_ALL); in check_irq_usage()
2887 ret = find_exclusive_match(target_entry->class->usage_mask, in check_irq_usage()
2888 target_entry1->class->usage_mask, in check_irq_usage()
2890 if (DEBUG_LOCKS_WARN_ON(ret == -1)) in check_irq_usage()
2920 * <target> or not. If it can, <src> -> <target> dependency is already
2938 * is equal to or stronger than <src> -> <target>. So if <src> is E, in check_redundant()
2939 * we need to let __bfs() only search for a path starting at a -(E*)->, in check_redundant()
2940 * we achieve this by setting the initial node's ->only_xr to true in in check_redundant()
2941 * that case. And if <prev> is S, we set initial ->only_xr to false in check_redundant()
2942 * because both -(S*)-> (equal) and -(E*)-> (stronger) are redundant. in check_redundant()
2944 src_entry.only_xr = src->read == 0; in check_redundant()
2950 * comment in usage_skip(), A -> local_lock() -> B and A -> B are not in check_redundant()
2984 nr_hardirq_chains--; in dec_chains()
2986 nr_softirq_chains--; in dec_chains()
2988 nr_process_chains--; in dec_chains()
2999 printk(" ----\n"); in print_deadlock_scenario()
3025 pr_warn("--------------------------------------------\n"); in print_deadlock_bug()
3027 curr->comm, task_pid_nr(curr)); in print_deadlock_bug()
3032 if (class->cmp_fn) { in print_deadlock_bug()
3034 class->cmp_fn(prev->instance, next->instance)); in print_deadlock_bug()
3065 for (i = 0; i < curr->lockdep_depth; i++) { in check_deadlock()
3066 prev = curr->held_locks + i; in check_deadlock()
3068 if (prev->instance == next->nest_lock) in check_deadlock()
3075 * Allow read-after-read recursion of the same in check_deadlock()
3078 if ((next->read == 2) && prev->read) in check_deadlock()
3083 if (class->cmp_fn && in check_deadlock()
3084 class->cmp_fn(prev->instance, next->instance) < 0) in check_deadlock()
3101 * There was a chain-cache miss, and we are about to add a new dependency
3102 * to a previous lock. We validate the following rules:
3104 * - would the adding of the <prev> -> <next> dependency create a
3107 * - does the new prev->next dependency connect any hardirq-safe lock
3108 * (in the full backwards-subgraph starting at <prev>) with any
3109 * hardirq-unsafe lock (in the full forwards-subgraph starting at
3112 * - does the new prev->next dependency connect any softirq-safe lock
3113 * (in the full backwards-subgraph starting at <prev>) with any
3114 * softirq-unsafe lock (in the full forwards-subgraph starting at
3130 if (!hlock_class(prev)->key || !hlock_class(next)->key) { in check_prev_add()
3132 * The warning statements below may trigger a use-after-free in check_prev_add()
3133 * of the class name. It is better to trigger a use-after free in check_prev_add()
3137 WARN_ONCE(!debug_locks_silent && !hlock_class(prev)->key, in check_prev_add()
3138 "Detected use-after-free of lock class %px/%s\n", in check_prev_add()
3140 hlock_class(prev)->name); in check_prev_add()
3141 WARN_ONCE(!debug_locks_silent && !hlock_class(next)->key, in check_prev_add()
3142 "Detected use-after-free of lock class %px/%s\n", in check_prev_add()
3144 hlock_class(next)->name); in check_prev_add()
3148 if (prev->class_idx == next->class_idx) { in check_prev_add()
3151 if (class->cmp_fn && in check_prev_add()
3152 class->cmp_fn(prev->instance, next->instance) < 0) in check_prev_add()
3157 * Prove that the new <prev> -> <next> dependency would not in check_prev_add()
3159 * a breadth-first search into the graph starting at <next>, in check_prev_add()
3174 * Is the <prev> -> <next> dependency already present? in check_prev_add()
3177 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3 in check_prev_add()
3178 * chains - the second one will be new, but L1 already has in check_prev_add()
3181 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) { in check_prev_add()
3182 if (entry->class == hlock_class(next)) { in check_prev_add()
3184 entry->distance = 1; in check_prev_add()
3185 entry->dep |= calc_dep(prev, next); in check_prev_add()
3189 * ->locks_before list. in check_prev_add()
3203 list_for_each_entry(entry, &hlock_class(next)->locks_before, entry) { in check_prev_add()
3204 if (entry->class == hlock_class(prev)) { in check_prev_add()
3206 entry->distance = 1; in check_prev_add()
3207 entry->dep |= calc_depb(prev, next); in check_prev_add()
3218 * Is the <prev> -> <next> link redundant? in check_prev_add()
3237 &hlock_class(prev)->locks_after, distance, in check_prev_add()
3244 &hlock_class(next)->locks_before, distance, in check_prev_add()
3253 * Add the dependency to all directly-previous locks that are 'relevant'.
3255 * all consecutive trylock entries and the final non-trylock entry - or
3256 * the end of this context's lock-chain - whichever comes first.
3262 int depth = curr->lockdep_depth; in check_prevs_add()
3268 * Depth must not be zero for a non-head lock: in check_prevs_add()
3276 if (curr->held_locks[depth].irq_context != in check_prevs_add()
3277 curr->held_locks[depth-1].irq_context) in check_prevs_add()
3281 u16 distance = curr->lockdep_depth - depth + 1; in check_prevs_add()
3282 hlock = curr->held_locks + depth - 1; in check_prevs_add()
3284 if (hlock->check) { in check_prevs_add()
3290 * Stop after the first non-trylock entry, in check_prevs_add()
3291 * as non-trylock entries have added their in check_prevs_add()
3295 if (!hlock->trylock) in check_prevs_add()
3299 depth--; in check_prevs_add()
3301 * End of lock-stack? in check_prevs_add()
3308 if (curr->held_locks[depth].irq_context != in check_prevs_add()
3309 curr->held_locks[depth-1].irq_context) in check_prevs_add()
3340 * Bit 15 - always set to 1 (it is not a class index)
3341 * Bits 0-14 - upper 15 bits of the next block index
3342 * entry[1] - lower 16 bits of next block index
3346 * On the unsized bucket (bucket-0), the 3rd and 4th entries contain
3349 * entry[2] - upper 16 bits of the chain block size
3350 * entry[3] - lower 16 bits of the chain block size
3363 return size - 1; in size_to_bucket()
3370 for ((prev) = -1, (curr) = chain_block_buckets[bucket]; \
3375 * next block or -1
3384 return -1; in chain_block_next()
3394 * bucket-0 only
3424 * the block before it is re-added. in add_chain_block()
3469 nr_free_chain_hlocks -= size; in del_chain_block()
3473 nr_large_chain_blocks--; in del_chain_block()
3481 chain_block_buckets[i] = -1; in init_chain_block_buckets()
3487 * Return offset of a chain block of the right size or -1 if not found.
3489 * Fairly simple worst-fit allocator with the addition of a number of size
3500 BUILD_BUG_ON((MAX_LOCKDEP_KEYS-1) & CHAIN_BLK_FLAG); in alloc_chain_hlocks()
3505 return -1; in alloc_chain_hlocks()
3533 add_chain_block(curr + req, size - req); in alloc_chain_hlocks()
3541 for (size = MAX_CHAIN_BUCKETS; size > req; size--) { in alloc_chain_hlocks()
3548 add_chain_block(curr + req, size - req); in alloc_chain_hlocks()
3552 return -1; in alloc_chain_hlocks()
3562 u16 chain_hlock = chain_hlocks[chain->base + i]; in lock_chain_get_class()
3577 for (i = curr->lockdep_depth - 1; i >= 0; i--) { in get_first_held_lock()
3578 hlock_curr = curr->held_locks + i; in get_first_held_lock()
3579 if (hlock_curr->irq_context != hlock->irq_context) in get_first_held_lock()
3595 printk(" hlock_id:%d -> chain_key:%016Lx", in print_chain_key_iteration()
3606 int depth = curr->lockdep_depth; in print_chain_keys_held_locks()
3609 printk("depth: %u (irq_context %u)\n", depth - i + 1, in print_chain_keys_held_locks()
3610 hlock_next->irq_context); in print_chain_keys_held_locks()
3612 hlock = curr->held_locks + i; in print_chain_keys_held_locks()
3628 printk("depth: %u\n", chain->depth); in print_chain_keys_chain()
3629 for (i = 0; i < chain->depth; i++) { in print_chain_keys_chain()
3630 hlock_id = chain_hlocks[chain->base + i]; in print_chain_keys_chain()
3648 pr_warn("----------------------------\n"); in print_collision()
3649 pr_warn("%s/%d: ", current->comm, task_pid_nr(current)); in print_collision()
3680 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) { in check_no_collision()
3685 for (j = 0; j < chain->depth - 1; j++, i++) { in check_no_collision()
3686 id = hlock_id(&curr->held_locks[i]); in check_no_collision()
3688 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) { in check_no_collision()
3698 * Given an index that is >= -1, return the index of the next lock chain.
3699 * Return -2 if there is no next lock chain.
3704 return i < ARRAY_SIZE(lock_chains) ? i : -2; in lockdep_next_lockchain()
3741 * disabled to make this an IRQ-safe lock.. for recursion reasons in add_chain_cache()
3758 chain->chain_key = chain_key; in add_chain_cache()
3759 chain->irq_context = hlock->irq_context; in add_chain_cache()
3761 chain->depth = curr->lockdep_depth + 1 - i; in add_chain_cache()
3764 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks)); in add_chain_cache()
3767 j = alloc_chain_hlocks(chain->depth); in add_chain_cache()
3779 chain->base = j; in add_chain_cache()
3780 for (j = 0; j < chain->depth - 1; j++, i++) { in add_chain_cache()
3781 int lock_id = hlock_id(curr->held_locks + i); in add_chain_cache()
3783 chain_hlocks[chain->base + j] = lock_id; in add_chain_cache()
3785 chain_hlocks[chain->base + j] = hlock_id(hlock); in add_chain_cache()
3786 hlist_add_head_rcu(&chain->entry, hash_head); in add_chain_cache()
3788 inc_chains(chain->irq_context); in add_chain_cache()
3803 if (READ_ONCE(chain->chain_key) == chain_key) { in lookup_chain_cache()
3813 * add it and return 1 - in this case the new dependency chain is
3833 class->key, class->name); in lookup_chain_cache_add()
3841 (unsigned long long)chain_key, class->key, class->name); in lookup_chain_cache_add()
3848 * We have to walk the chain again locked - to avoid duplicates: in lookup_chain_cache_add()
3876 if (!hlock->trylock && hlock->check && in validate_chain()
3881 * - is irq-safe, if this lock is irq-unsafe in validate_chain()
3882 * - is softirq-safe, if this lock is hardirq-unsafe in validate_chain()
3887 * - within the current held-lock stack in validate_chain()
3888 * - across our accumulated lock dependency records in validate_chain()
3934 * We are building curr_chain_key incrementally, so double-check
3944 for (i = 0; i < curr->lockdep_depth; i++) { in check_chain_key()
3945 hlock = curr->held_locks + i; in check_chain_key()
3946 if (chain_key != hlock->prev_chain_key) { in check_chain_key()
3953 curr->lockdep_depth, i, in check_chain_key()
3955 (unsigned long long)hlock->prev_chain_key); in check_chain_key()
3960 * hlock->class_idx can't go beyond MAX_LOCKDEP_KEYS, but is in check_chain_key()
3963 if (DEBUG_LOCKS_WARN_ON(!test_bit(hlock->class_idx, lock_classes_in_use))) in check_chain_key()
3966 if (prev_hlock && (prev_hlock->irq_context != in check_chain_key()
3967 hlock->irq_context)) in check_chain_key()
3972 if (chain_key != curr->curr_chain_key) { in check_chain_key()
3979 curr->lockdep_depth, i, in check_chain_key()
3981 (unsigned long long)curr->curr_chain_key); in check_chain_key()
3996 printk(" ----\n"); in print_usage_bug_scenario()
4020 pr_warn("--------------------------------\n"); in print_usage_bug()
4022 pr_warn("inconsistent {%s} -> {%s} usage.\n", in print_usage_bug()
4026 curr->comm, task_pid_nr(curr), in print_usage_bug()
4034 print_lock_trace(hlock_class(this)->usage_traces[prev_bit], 1); in print_usage_bug()
4055 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit))) { in valid_state()
4086 pr_warn("--------------------------------------------------------\n"); in print_irq_inversion_bug()
4088 curr->comm, task_pid_nr(curr)); in print_irq_inversion_bug()
4091 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass); in print_irq_inversion_bug()
4093 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass); in print_irq_inversion_bug()
4094 print_lock_name(NULL, other->class); in print_irq_inversion_bug()
4108 depth--; in print_irq_inversion_bug()
4112 middle ? middle->class : root->class, other->class); in print_irq_inversion_bug()
4115 middle ? middle->class : other->class, root->class); in print_irq_inversion_bug()
4120 root->trace = save_trace(); in print_irq_inversion_bug()
4121 if (!root->trace) in print_irq_inversion_bug()
4132 * Prove that in the forwards-direction subgraph starting at <this>
4155 if (target_entry->class->usage_mask & lock_flag(bit)) { in check_usage_forwards()
4167 * Prove that in the backwards-direction subgraph starting at <this>
4190 if (target_entry->class->usage_mask & lock_flag(bit)) { in check_usage_backwards()
4203 const struct irqtrace_events *trace = &curr->irqtrace; in print_irqtrace_events()
4207 printk("irq event stamp: %u\n", trace->irq_events); in print_irqtrace_events()
4209 trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip, in print_irqtrace_events()
4210 (void *)trace->hardirq_enable_ip); in print_irqtrace_events()
4212 trace->hardirq_disable_event, (void *)trace->hardirq_disable_ip, in print_irqtrace_events()
4213 (void *)trace->hardirq_disable_ip); in print_irqtrace_events()
4215 trace->softirq_enable_event, (void *)trace->softirq_enable_ip, in print_irqtrace_events()
4216 (void *)trace->softirq_enable_ip); in print_irqtrace_events()
4218 trace->softirq_disable_event, (void *)trace->softirq_disable_ip, in print_irqtrace_events()
4219 (void *)trace->softirq_disable_ip); in print_irqtrace_events()
4265 * Validate that this particular lock does not have conflicting in mark_lock_irq()
4280 * Validate that the lock dependencies don't have conflicting usage in mark_lock_irq()
4285 * mark ENABLED has to look backwards -- to ensure no dependee in mark_lock_irq()
4292 * mark USED_IN has to look forwards -- to ensure no dependency in mark_lock_irq()
4314 for (i = 0; i < curr->lockdep_depth; i++) { in mark_held_locks()
4316 hlock = curr->held_locks + i; in mark_held_locks()
4318 if (hlock->read) in mark_held_locks()
4323 if (!hlock->check) in mark_held_locks()
4351 if (curr->softirqs_enabled) in __trace_hardirqs_on_caller()
4356 * lockdep_hardirqs_on_prepare - Prepare for enabling interrupts
4408 current->hardirq_chain_key = current->curr_chain_key; in lockdep_hardirqs_on_prepare()
4418 struct irqtrace_events *trace = ¤t->irqtrace; in lockdep_hardirqs_on()
4436 * - recursion check, because NMI can hit lockdep; in lockdep_hardirqs_on()
4437 * - hardware state check, because above; in lockdep_hardirqs_on()
4438 * - chain_key check, see lockdep_hardirqs_on_prepare(). in lockdep_hardirqs_on()
4468 DEBUG_LOCKS_WARN_ON(current->hardirq_chain_key != in lockdep_hardirqs_on()
4469 current->curr_chain_key); in lockdep_hardirqs_on()
4472 /* we'll do an OFF -> ON transition: */ in lockdep_hardirqs_on()
4474 trace->hardirq_enable_ip = ip; in lockdep_hardirqs_on()
4475 trace->hardirq_enable_event = ++trace->irq_events; in lockdep_hardirqs_on()
4507 struct irqtrace_events *trace = ¤t->irqtrace; in lockdep_hardirqs_off()
4510 * We have done an ON -> OFF transition: in lockdep_hardirqs_off()
4513 trace->hardirq_disable_ip = ip; in lockdep_hardirqs_off()
4514 trace->hardirq_disable_event = ++trace->irq_events; in lockdep_hardirqs_off()
4527 struct irqtrace_events *trace = ¤t->irqtrace; in lockdep_softirqs_on()
4539 if (current->softirqs_enabled) { in lockdep_softirqs_on()
4546 * We'll do an OFF -> ON transition: in lockdep_softirqs_on()
4548 current->softirqs_enabled = 1; in lockdep_softirqs_on()
4549 trace->softirq_enable_ip = ip; in lockdep_softirqs_on()
4550 trace->softirq_enable_event = ++trace->irq_events; in lockdep_softirqs_on()
4576 if (current->softirqs_enabled) { in lockdep_softirqs_off()
4577 struct irqtrace_events *trace = ¤t->irqtrace; in lockdep_softirqs_off()
4580 * We have done an ON -> OFF transition: in lockdep_softirqs_off()
4582 current->softirqs_enabled = 0; in lockdep_softirqs_off()
4583 trace->softirq_disable_ip = ip; in lockdep_softirqs_off()
4584 trace->softirq_disable_event = ++trace->irq_events; in lockdep_softirqs_off()
4595 * lockdep_cleanup_dead_cpu - Ensure CPU lockdep state is cleanly stopped
4625 * If non-trylock use in a hardirq or softirq context, then in mark_usage()
4628 if (!hlock->trylock) { in mark_usage()
4629 if (hlock->read) { in mark_usage()
4634 if (curr->softirq_context) in mark_usage()
4642 if (curr->softirq_context) in mark_usage()
4653 if (!hlock->hardirqs_off && !hlock->sync) { in mark_usage()
4654 if (hlock->read) { in mark_usage()
4658 if (curr->softirqs_enabled) in mark_usage()
4666 if (curr->softirqs_enabled) in mark_usage()
4684 LOCK_CHAIN_SOFTIRQ_CONTEXT * !!task->softirq_context; in task_irq_context()
4690 unsigned int depth = curr->lockdep_depth; in separate_irq_context()
4698 prev_hlock = curr->held_locks + depth-1; in separate_irq_context()
4704 if (prev_hlock->irq_context != hlock->irq_context) in separate_irq_context()
4711 * Mark a lock with a usage bit, and validate the state transition:
4723 if (new_bit == LOCK_USED && this->read) in mark_lock()
4732 if (likely(hlock_class(this)->usage_mask & new_mask)) in mark_lock()
4740 if (unlikely(hlock_class(this)->usage_mask & new_mask)) in mark_lock()
4743 if (!hlock_class(this)->usage_mask) in mark_lock()
4746 hlock_class(this)->usage_mask |= new_mask; in mark_lock()
4749 if (!(hlock_class(this)->usage_traces[new_bit] = save_trace())) in mark_lock()
4787 if (curr->hardirq_threaded || curr->irq_config) in task_wait_context()
4791 } else if (curr->softirq_context) { in task_wait_context()
4818 pr_warn("-----------------------------\n"); in print_lock_invalid_wait_context()
4820 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr)); in print_lock_invalid_wait_context()
4826 pr_warn("context-{%d:%d}\n", curr_inner, curr_inner); in print_lock_invalid_wait_context()
4841 * This check validates we take locks in the right wait-type order; that is it
4855 u8 next_inner = hlock_class(next)->wait_type_inner; in check_wait_context()
4856 u8 next_outer = hlock_class(next)->wait_type_outer; in check_wait_context()
4860 if (!next_inner || next->trylock) in check_wait_context()
4869 for (depth = curr->lockdep_depth - 1; depth >= 0; depth--) { in check_wait_context()
4870 struct held_lock *prev = curr->held_locks + depth; in check_wait_context()
4871 if (prev->irq_context != next->irq_context) in check_wait_context()
4878 for (; depth < curr->lockdep_depth; depth++) { in check_wait_context()
4879 struct held_lock *prev = curr->held_locks + depth; in check_wait_context()
4881 u8 prev_inner = class->wait_type_inner; in check_wait_context()
4893 * Allow override for annotations -- this is typically in check_wait_context()
4897 if (unlikely(class->lock_type == LD_LOCK_WAIT_OVERRIDE)) in check_wait_context()
4936 * Initialize a lock instance's lock-class mapping info:
4945 lock->class_cache[i] = NULL; in lockdep_init_map_type()
4948 lock->cpu = raw_smp_processor_id(); in lockdep_init_map_type()
4955 lock->name = "NULL"; in lockdep_init_map_type()
4959 lock->name = name; in lockdep_init_map_type()
4961 lock->wait_type_outer = outer; in lockdep_init_map_type()
4962 lock->wait_type_inner = inner; in lockdep_init_map_type()
4963 lock->lock_type = lock_type; in lockdep_init_map_type()
4971 * Sanity check, the lock-class key must either have been allocated in lockdep_init_map_type()
4980 lock->key = key; in lockdep_init_map_type()
5010 struct lock_class *class = lock->class_cache[0]; in lockdep_set_lock_cmp_fn()
5020 WARN_ON(class->cmp_fn && class->cmp_fn != cmp_fn); in lockdep_set_lock_cmp_fn()
5021 WARN_ON(class->print_fn && class->print_fn != print_fn); in lockdep_set_lock_cmp_fn()
5023 class->cmp_fn = cmp_fn; in lockdep_set_lock_cmp_fn()
5024 class->print_fn = print_fn; in lockdep_set_lock_cmp_fn()
5048 pr_warn("----------------------------------\n"); in print_lock_nested_lock_not_held()
5050 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr)); in print_lock_nested_lock_not_held()
5054 pr_warn("%s\n", hlock->nest_lock->name); in print_lock_nested_lock_not_held()
5072 * We maintain the dependency maps and validate the locking attempt:
5094 if (unlikely(lock->key == &__lockdep_no_track__)) in __lock_acquire()
5099 if (!prove_locking || lock->key == &__lockdep_no_validate__) { in __lock_acquire()
5105 class = lock->class_cache[subclass]; in __lock_acquire()
5119 printk("\nacquire class [%px] %s", class->key, class->name); in __lock_acquire()
5120 if (class->name_version > 1) in __lock_acquire()
5121 printk(KERN_CONT "#%d", class->name_version); in __lock_acquire()
5129 * (we dont increase the depth just yet, up until the in __lock_acquire()
5132 depth = curr->lockdep_depth; in __lock_acquire()
5134 * Ran out of static storage for our per-task lock stack again have we? in __lock_acquire()
5139 class_idx = class - lock_classes; in __lock_acquire()
5143 hlock = curr->held_locks + depth - 1; in __lock_acquire()
5144 if (hlock->class_idx == class_idx && nest_lock) { in __lock_acquire()
5148 if (!hlock->references) in __lock_acquire()
5149 hlock->references++; in __lock_acquire()
5151 hlock->references += references; in __lock_acquire()
5154 if (DEBUG_LOCKS_WARN_ON(hlock->references < references)) in __lock_acquire()
5161 hlock = curr->held_locks + depth; in __lock_acquire()
5168 hlock->class_idx = class_idx; in __lock_acquire()
5169 hlock->acquire_ip = ip; in __lock_acquire()
5170 hlock->instance = lock; in __lock_acquire()
5171 hlock->nest_lock = nest_lock; in __lock_acquire()
5172 hlock->irq_context = task_irq_context(curr); in __lock_acquire()
5173 hlock->trylock = trylock; in __lock_acquire()
5174 hlock->read = read; in __lock_acquire()
5175 hlock->check = check; in __lock_acquire()
5176 hlock->sync = !!sync; in __lock_acquire()
5177 hlock->hardirqs_off = !!hardirqs_off; in __lock_acquire()
5178 hlock->references = references; in __lock_acquire()
5180 hlock->waittime_stamp = 0; in __lock_acquire()
5181 hlock->holdtime_stamp = lockstat_clock(); in __lock_acquire()
5183 hlock->pin_count = pin_count; in __lock_acquire()
5200 * the hash, not class->key. in __lock_acquire()
5208 chain_key = curr->curr_chain_key; in __lock_acquire()
5218 hlock->prev_chain_key = chain_key; in __lock_acquire()
5225 if (nest_lock && !__lock_is_held(nest_lock, -1)) { in __lock_acquire()
5231 WARN_ON_ONCE(depth && !hlock_class(hlock - 1)->key); in __lock_acquire()
5232 WARN_ON_ONCE(!hlock_class(hlock)->key); in __lock_acquire()
5239 if (hlock->sync) in __lock_acquire()
5242 curr->curr_chain_key = chain_key; in __lock_acquire()
5243 curr->lockdep_depth++; in __lock_acquire()
5249 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) { in __lock_acquire()
5254 curr->lockdep_depth, MAX_LOCK_DEPTH); in __lock_acquire()
5264 if (unlikely(curr->lockdep_depth > max_lockdep_depth)) in __lock_acquire()
5265 max_lockdep_depth = curr->lockdep_depth; in __lock_acquire()
5285 pr_warn("-------------------------------------\n"); in print_unlock_imbalance_bug()
5287 curr->comm, task_pid_nr(curr)); in print_unlock_imbalance_bug()
5304 if (hlock->instance == lock) in match_held_lock()
5307 if (hlock->references) { in match_held_lock()
5308 const struct lock_class *class = lock->class_cache[0]; in match_held_lock()
5323 * References, but not a lock we're actually ref-counting? in match_held_lock()
5324 * State got messed up, follow the sites that change ->references in match_held_lock()
5327 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock)) in match_held_lock()
5330 if (hlock->class_idx == class - lock_classes) in match_held_lock()
5345 i = depth - 1; in find_held_lock()
5346 hlock = curr->held_locks + i; in find_held_lock()
5352 for (i--, prev_hlock = hlock--; in find_held_lock()
5354 i--, prev_hlock = hlock--) { in find_held_lock()
5358 if (prev_hlock->irq_context != hlock->irq_context) { in find_held_lock()
5382 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) { in reacquire_held_locks()
5383 switch (__lock_acquire(hlock->instance, in reacquire_held_locks()
5384 hlock_class(hlock)->subclass, in reacquire_held_locks()
5385 hlock->trylock, in reacquire_held_locks()
5386 hlock->read, hlock->check, in reacquire_held_locks()
5387 hlock->hardirqs_off, in reacquire_held_locks()
5388 hlock->nest_lock, hlock->acquire_ip, in reacquire_held_locks()
5389 hlock->references, hlock->pin_count, 0)) { in reacquire_held_locks()
5419 depth = curr->lockdep_depth; in __lock_set_class()
5434 lock->wait_type_inner, in __lock_set_class()
5435 lock->wait_type_outer, in __lock_set_class()
5436 lock->lock_type); in __lock_set_class()
5438 hlock->class_idx = class - lock_classes; in __lock_set_class()
5440 curr->lockdep_depth = i; in __lock_set_class()
5441 curr->curr_chain_key = hlock->prev_chain_key; in __lock_set_class()
5450 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged)) in __lock_set_class()
5465 depth = curr->lockdep_depth; in __lock_downgrade()
5479 curr->lockdep_depth = i; in __lock_downgrade()
5480 curr->curr_chain_key = hlock->prev_chain_key; in __lock_downgrade()
5482 WARN(hlock->read, "downgrading a read lock"); in __lock_downgrade()
5483 hlock->read = 1; in __lock_downgrade()
5484 hlock->acquire_ip = ip; in __lock_downgrade()
5497 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth)) in __lock_downgrade()
5504 * Remove the lock from the list of currently held locks - this gets
5519 depth = curr->lockdep_depth; in __lock_release()
5539 if (hlock->instance == lock) in __lock_release()
5542 WARN(hlock->pin_count, "releasing a pinned lock\n"); in __lock_release()
5544 if (hlock->references) { in __lock_release()
5545 hlock->references--; in __lock_release()
5546 if (hlock->references) { in __lock_release()
5562 curr->lockdep_depth = i; in __lock_release()
5563 curr->curr_chain_key = hlock->prev_chain_key; in __lock_release()
5569 if (i == depth-1) in __lock_release()
5577 * there's not N-1 bottles of beer left on the wall... in __lock_release()
5580 DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - merged); in __lock_release()
5596 for (i = 0; i < curr->lockdep_depth; i++) { in __lock_is_held()
5597 struct held_lock *hlock = curr->held_locks + i; in __lock_is_held()
5600 if (read == -1 || !!hlock->read == read) in __lock_is_held()
5619 for (i = 0; i < curr->lockdep_depth; i++) { in __lock_pin_lock()
5620 struct held_lock *hlock = curr->held_locks + i; in __lock_pin_lock()
5629 hlock->pin_count += cookie.val; in __lock_pin_lock()
5646 for (i = 0; i < curr->lockdep_depth; i++) { in __lock_repin_lock()
5647 struct held_lock *hlock = curr->held_locks + i; in __lock_repin_lock()
5650 hlock->pin_count += cookie.val; in __lock_repin_lock()
5666 for (i = 0; i < curr->lockdep_depth; i++) { in __lock_unpin_lock()
5667 struct held_lock *hlock = curr->held_locks + i; in __lock_unpin_lock()
5670 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n")) in __lock_unpin_lock()
5673 hlock->pin_count -= cookie.val; in __lock_unpin_lock()
5675 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n")) in __lock_unpin_lock()
5676 hlock->pin_count = 0; in __lock_unpin_lock()
5686 * Check whether we follow the irq-flags state precisely:
5699 printk("possible reason: unannotated irqs-off.\n"); in check_flags()
5703 printk("possible reason: unannotated irqs-on.\n"); in check_flags()
5709 * We dont accurately track softirq state in e.g. in check_flags()
5716 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled); in check_flags()
5719 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled); in check_flags()
5780 * READ locks, there is no deadlock possible -- RCU. in verify_lock_unused()
5782 if (!hlock->read) in verify_lock_unused()
5785 if (!(class->usage_mask & mask)) in verify_lock_unused()
5788 hlock->class_idx = class - lock_classes; in verify_lock_unused()
5820 * We are not always called with irqs disabled - do that here,
5837 * kasan_check_byte() here to check for use-after-free and other in lock_acquire()
5880 lock->key == &__lockdep_no_track__)) in lock_release()
5895 * lock_sync() - A special annotation for synchronize_{s,}rcu()-like API.
6017 pr_warn("---------------------------------\n"); in print_lock_contention_bug()
6019 curr->comm, task_pid_nr(curr)); in print_lock_contention_bug()
6042 depth = curr->lockdep_depth; in __lock_contended()
6050 if (unlikely(lock->key == &__lockdep_no_track__)) in __lock_contended()
6059 if (hlock->instance != lock) in __lock_contended()
6062 hlock->waittime_stamp = lockstat_clock(); in __lock_contended()
6064 contention_point = lock_point(hlock_class(hlock)->contention_point, ip); in __lock_contended()
6065 contending_point = lock_point(hlock_class(hlock)->contending_point, in __lock_contended()
6066 lock->ip); in __lock_contended()
6070 stats->contention_point[contention_point]++; in __lock_contended()
6072 stats->contending_point[contending_point]++; in __lock_contended()
6073 if (lock->cpu != smp_processor_id()) in __lock_contended()
6074 stats->bounces[bounce_contended + !!hlock->read]++; in __lock_contended()
6087 depth = curr->lockdep_depth; in __lock_acquired()
6095 if (unlikely(lock->key == &__lockdep_no_track__)) in __lock_acquired()
6104 if (hlock->instance != lock) in __lock_acquired()
6108 if (hlock->waittime_stamp) { in __lock_acquired()
6110 waittime = now - hlock->waittime_stamp; in __lock_acquired()
6111 hlock->holdtime_stamp = now; in __lock_acquired()
6116 if (hlock->read) in __lock_acquired()
6117 lock_time_inc(&stats->read_waittime, waittime); in __lock_acquired()
6119 lock_time_inc(&stats->write_waittime, waittime); in __lock_acquired()
6121 if (lock->cpu != cpu) in __lock_acquired()
6122 stats->bounces[bounce_acquired + !!hlock->read]++; in __lock_acquired()
6124 lock->cpu = cpu; in __lock_acquired()
6125 lock->ip = ip; in __lock_acquired()
6177 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock)); in lockdep_reset()
6195 for (i = chain->base; i < chain->base + chain->depth; i++) { in remove_class_from_lock_chain()
6196 if (chain_hlock_class_idx(chain_hlocks[i]) != class - lock_classes) in remove_class_from_lock_chain()
6208 free_chain_hlocks(chain->base, chain->depth); in remove_class_from_lock_chain()
6210 WRITE_ONCE(chain->chain_key, INITIAL_CHAIN_KEY); in remove_class_from_lock_chain()
6211 dec_chains(chain->irq_context); in remove_class_from_lock_chain()
6217 hlist_del_rcu(&chain->entry); in remove_class_from_lock_chain()
6218 __set_bit(chain - lock_chains, pf->lock_chains_being_freed); in remove_class_from_lock_chain()
6247 WARN_ON_ONCE(!class->key); in zap_class()
6255 if (entry->class != class && entry->links_to != class) in zap_class()
6258 nr_list_entries--; in zap_class()
6259 list_del_rcu(&entry->entry); in zap_class()
6261 if (list_empty(&class->locks_after) && in zap_class()
6262 list_empty(&class->locks_before)) { in zap_class()
6263 list_move_tail(&class->lock_entry, &pf->zapped); in zap_class()
6264 hlist_del_rcu(&class->hash_entry); in zap_class()
6265 WRITE_ONCE(class->key, NULL); in zap_class()
6266 WRITE_ONCE(class->name, NULL); in zap_class()
6267 /* Class allocated but not used, -1 in nr_unused_locks */ in zap_class()
6268 if (class->usage_mask == 0) in zap_class()
6270 nr_lock_classes--; in zap_class()
6271 __clear_bit(class - lock_classes, lock_classes_in_use); in zap_class()
6272 if (class - lock_classes == max_lock_class_idx) in zap_class()
6273 max_lock_class_idx--; in zap_class()
6276 class->name); in zap_class()
6285 WARN_ON_ONCE(!class->lock_entry.next); in reinit_class()
6286 WARN_ON_ONCE(!list_empty(&class->locks_after)); in reinit_class()
6287 WARN_ON_ONCE(!list_empty(&class->locks_before)); in reinit_class()
6289 WARN_ON_ONCE(!class->lock_entry.next); in reinit_class()
6290 WARN_ON_ONCE(!list_empty(&class->locks_after)); in reinit_class()
6291 WARN_ON_ONCE(!list_empty(&class->locks_before)); in reinit_class()
6322 if (list_empty(&pf->zapped)) in prepare_call_rcu_zapped()
6343 list_for_each_entry(class, &pf->zapped, lock_entry) in __free_zapped_classes()
6346 list_splice_init(&pf->zapped, &free_lock_classes); in __free_zapped_classes()
6350 pf->lock_chains_being_freed, ARRAY_SIZE(lock_chains)); in __free_zapped_classes()
6351 bitmap_clear(pf->lock_chains_being_freed, 0, ARRAY_SIZE(lock_chains)); in __free_zapped_classes()
6402 if (!within(class->key, start, size) && in __lockdep_free_key_range()
6403 !within(class->name, start, size)) in __lockdep_free_key_range()
6412 * freed; and possibly re-used by other modules.
6415 * guaranteed nobody will look up these exact classes -- they're properly dead
6472 * Check whether any element of the @lock->class_cache[] array refers to a
6486 if (lock->class_cache[j] == class) in lock_class_cache_is_registered()
6599 hlist_del_rcu(&k->hash_entry); in lockdep_unregister_key()
6616 /* Wait until is_dynamic_key() has finished accessing k->hash_entry. */ in lockdep_unregister_key()
6656 pr_info(" per task-struct memory footprint: %zu bytes\n", in lockdep_init()
6657 sizeof(((struct task_struct *)NULL)->held_locks)); in lockdep_init()
6675 pr_warn("-------------------------\n"); in print_freed_lock_bug()
6676 pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n", in print_freed_lock_bug()
6677 curr->comm, task_pid_nr(curr), mem_from, mem_to-1); in print_freed_lock_bug()
6696 * is destroyed or reinitialized - this code checks whether there is
6710 for (i = 0; i < curr->lockdep_depth; i++) { in debug_check_no_locks_freed()
6711 hlock = curr->held_locks + i; in debug_check_no_locks_freed()
6713 if (not_in_range(mem_from, mem_len, hlock->instance, in debug_check_no_locks_freed()
6714 sizeof(*hlock->instance))) in debug_check_no_locks_freed()
6736 current->comm, task_pid_nr(current)); in print_held_locks_bug()
6738 pr_warn("------------------------------------\n"); in print_held_locks_bug()
6748 if (unlikely(current->lockdep_depth > 0)) in debug_check_no_locks_held()
6766 if (!p->lockdep_depth) in debug_show_all_locks()
6798 if (unlikely(curr->lockdep_depth)) { in lockdep_sys_exit()
6806 pr_warn("------------------------------------------------\n"); in lockdep_sys_exit()
6808 curr->comm, curr->pid); in lockdep_sys_exit()
6832 pr_warn("-----------------------------\n"); in lockdep_rcu_suspicious()
6843 * If a CPU is in the RCU-free window in idle (ie: in the section in lockdep_rcu_suspicious()
6849 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well in lockdep_rcu_suspicious()
6851 * choice here: we need to keep an RCU-free window in idle where in lockdep_rcu_suspicious()