1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_locking.h"
5 #include "btree_types.h"
7 static struct lock_class_key bch2_btree_node_lock_key;
9 void bch2_btree_lock_init(struct btree_bkey_cached_common *b,
10 enum six_lock_init_flags flags)
12 __six_lock_init(&b->lock, "b->c.lock", &bch2_btree_node_lock_key, flags);
13 #ifdef CONFIG_DEBUG_LOCK_ALLOC
14 lockdep_set_no_check_recursion(&b->lock.dep_map);
19 void bch2_assert_btree_nodes_not_locked(void)
21 BUG_ON(lock_class_is_held(&bch2_btree_node_lock_key));
25 /* Btree node locking: */
27 struct six_lock_count bch2_btree_node_lock_counts(struct btree_trans *trans,
28 struct btree_path *skip,
29 struct btree_bkey_cached_common *b,
32 struct btree_path *path;
33 struct six_lock_count ret;
35 memset(&ret, 0, sizeof(ret));
37 if (IS_ERR_OR_NULL(b))
40 trans_for_each_path(trans, path)
41 if (path != skip && &path->l[level].b->c == b) {
42 int t = btree_node_locked_type(path, level);
44 if (t != BTREE_NODE_UNLOCKED)
53 void bch2_btree_node_unlock_write(struct btree_trans *trans,
54 struct btree_path *path, struct btree *b)
56 bch2_btree_node_unlock_write_inlined(trans, path, b);
62 * @trans wants to lock @b with type @type
64 struct trans_waiting_for_lock {
65 struct btree_trans *trans;
66 struct btree_bkey_cached_common *node_want;
67 enum six_lock_type lock_want;
69 /* for iterating over held locks :*/
76 struct trans_waiting_for_lock g[8];
80 static noinline void print_cycle(struct printbuf *out, struct lock_graph *g)
82 struct trans_waiting_for_lock *i;
84 prt_printf(out, "Found lock cycle (%u entries):", g->nr);
87 for (i = g->g; i < g->g + g->nr; i++)
88 bch2_btree_trans_to_text(out, i->trans);
91 static noinline void print_chain(struct printbuf *out, struct lock_graph *g)
93 struct trans_waiting_for_lock *i;
95 for (i = g->g; i != g->g + g->nr; i++) {
98 prt_printf(out, "%u ", i->trans->locking_wait.task->pid);
103 static void lock_graph_up(struct lock_graph *g)
105 closure_put(&g->g[--g->nr].trans->ref);
108 static noinline void lock_graph_pop_all(struct lock_graph *g)
114 static void __lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
116 g->g[g->nr++] = (struct trans_waiting_for_lock) {
118 .node_want = trans->locking,
119 .lock_want = trans->locking_wait.lock_want,
123 static void lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
125 closure_get(&trans->ref);
126 __lock_graph_down(g, trans);
129 static bool lock_graph_remove_non_waiters(struct lock_graph *g)
131 struct trans_waiting_for_lock *i;
133 for (i = g->g + 1; i < g->g + g->nr; i++)
134 if (i->trans->locking != i->node_want ||
135 i->trans->locking_wait.start_time != i[-1].lock_start_time) {
136 while (g->g + g->nr > i)
144 static int abort_lock(struct lock_graph *g, struct trans_waiting_for_lock *i)
147 trace_and_count(i->trans->c, trans_restart_would_deadlock, i->trans, _RET_IP_);
148 return btree_trans_restart(i->trans, BCH_ERR_transaction_restart_would_deadlock);
150 i->trans->lock_must_abort = true;
151 wake_up_process(i->trans->locking_wait.task);
156 static int btree_trans_abort_preference(struct btree_trans *trans)
158 if (trans->lock_may_not_fail)
160 if (trans->locking_wait.lock_want == SIX_LOCK_write)
162 if (!trans->in_traverse_all)
167 static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
169 struct trans_waiting_for_lock *i, *abort = NULL;
170 unsigned best = 0, pref;
173 if (lock_graph_remove_non_waiters(g))
176 /* Only checking, for debugfs: */
178 print_cycle(cycle, g);
183 for (i = g->g; i < g->g + g->nr; i++) {
184 pref = btree_trans_abort_preference(i->trans);
191 if (unlikely(!best)) {
192 struct printbuf buf = PRINTBUF;
194 prt_printf(&buf, bch2_fmt(g->g->trans->c, "cycle of nofail locks"));
196 for (i = g->g; i < g->g + g->nr; i++) {
197 struct btree_trans *trans = i->trans;
199 bch2_btree_trans_to_text(&buf, trans);
201 prt_printf(&buf, "backtrace:");
203 printbuf_indent_add(&buf, 2);
204 bch2_prt_task_backtrace(&buf, trans->locking_wait.task);
205 printbuf_indent_sub(&buf, 2);
209 bch2_print_string_as_lines(KERN_ERR, buf.buf);
214 ret = abort_lock(g, abort);
222 static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
223 struct printbuf *cycle)
225 struct btree_trans *orig_trans = g->g->trans;
226 struct trans_waiting_for_lock *i;
228 for (i = g->g; i < g->g + g->nr; i++)
229 if (i->trans == trans) {
230 closure_put(&trans->ref);
231 return break_cycle(g, cycle);
234 if (g->nr == ARRAY_SIZE(g->g)) {
235 closure_put(&trans->ref);
237 if (orig_trans->lock_may_not_fail)
246 trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
247 return btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
250 __lock_graph_down(g, trans);
254 static bool lock_type_conflicts(enum six_lock_type t1, enum six_lock_type t2)
259 int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
262 struct trans_waiting_for_lock *top;
263 struct btree_bkey_cached_common *b;
264 struct btree_path *path;
268 if (trans->lock_must_abort) {
272 trace_and_count(trans->c, trans_restart_would_deadlock, trans, _RET_IP_);
273 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
277 lock_graph_down(&g, trans);
282 top = &g.g[g.nr - 1];
284 trans_for_each_path_safe_from(top->trans, path, path_idx, top->path_idx) {
285 if (!path->nodes_locked)
288 if (path_idx != top->path_idx) {
289 top->path_idx = path_idx;
291 top->lock_start_time = 0;
295 top->level < BTREE_MAX_DEPTH;
296 top->level++, top->lock_start_time = 0) {
297 int lock_held = btree_node_locked_type(path, top->level);
299 if (lock_held == BTREE_NODE_UNLOCKED)
302 b = &READ_ONCE(path->l[top->level].b)->c;
304 if (IS_ERR_OR_NULL(b)) {
306 * If we get here, it means we raced with the
307 * other thread updating its btree_path
308 * structures - which means it can't be blocked
311 if (!lock_graph_remove_non_waiters(&g)) {
313 * If lock_graph_remove_non_waiters()
314 * didn't do anything, it must be
315 * because we're being called by debugfs
316 * checking for lock cycles, which
317 * invokes us on btree_transactions that
318 * aren't actually waiting on anything.
321 lock_graph_pop_all(&g);
327 if (list_empty_careful(&b->lock.wait_list))
330 raw_spin_lock(&b->lock.wait_lock);
331 list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
332 BUG_ON(b != trans->locking);
334 if (top->lock_start_time &&
335 time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
338 top->lock_start_time = trans->locking_wait.start_time;
340 /* Don't check for self deadlock: */
341 if (trans == top->trans ||
342 !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
345 closure_get(&trans->ref);
346 raw_spin_unlock(&b->lock.wait_lock);
348 ret = lock_graph_descend(&g, trans, cycle);
354 raw_spin_unlock(&b->lock.wait_lock);
358 if (g.nr > 1 && cycle)
359 print_chain(cycle, &g);
364 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
366 struct btree_trans *trans = p;
368 return bch2_check_for_deadlock(trans, NULL);
371 int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
372 struct btree_bkey_cached_common *b,
373 bool lock_may_not_fail)
375 int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
379 * Must drop our read locks before calling six_lock_write() -
380 * six_unlock() won't do wakeups until the reader count
381 * goes to 0, and it's safe because we have the node intent
384 six_lock_readers_add(&b->lock, -readers);
385 ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write,
386 lock_may_not_fail, _RET_IP_);
387 six_lock_readers_add(&b->lock, readers);
390 mark_btree_node_locked_noreset(path, b->level, BTREE_NODE_INTENT_LOCKED);
395 void bch2_btree_node_lock_write_nofail(struct btree_trans *trans,
396 struct btree_path *path,
397 struct btree_bkey_cached_common *b)
399 struct btree_path *linked;
406 * Drop all read locks before taking a write lock:
408 * This is a hack, because bch2_btree_node_lock_write_nofail() is a
409 * hack - but by dropping read locks first, this should never fail, and
410 * we only use this in code paths where whatever read locks we've
411 * already taken are no longer needed:
414 trans_for_each_path(trans, linked) {
415 if (!linked->nodes_locked)
418 for (i = 0; i < BTREE_MAX_DEPTH; i++)
419 if (btree_node_read_locked(linked, i)) {
420 btree_node_unlock(trans, linked, i);
421 btree_path_set_dirty(linked, BTREE_ITER_NEED_RELOCK);
425 ret = __btree_node_lock_write(trans, path, b, true);
431 static inline bool btree_path_get_locks(struct btree_trans *trans,
432 struct btree_path *path,
434 struct get_locks_fail *f)
436 unsigned l = path->level;
440 if (!btree_path_node(path, l))
444 ? bch2_btree_node_upgrade(trans, path, l)
445 : bch2_btree_node_relock(trans, path, l))) {
455 } while (l < path->locks_want);
458 * When we fail to get a lock, we have to ensure that any child nodes
459 * can't be relocked so bch2_btree_path_traverse has to walk back up to
460 * the node that we failed to relock:
463 __bch2_btree_path_unlock(trans, path);
464 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
467 path->l[fail_idx].b = upgrade
468 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
469 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
471 } while (fail_idx >= 0);
474 if (path->uptodate == BTREE_ITER_NEED_RELOCK)
475 path->uptodate = BTREE_ITER_UPTODATE;
477 bch2_trans_verify_locks(trans);
479 return path->uptodate < BTREE_ITER_NEED_RELOCK;
482 bool __bch2_btree_node_relock(struct btree_trans *trans,
483 struct btree_path *path, unsigned level,
486 struct btree *b = btree_path_node(path, level);
487 int want = __btree_lock_want(path, level);
492 if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
493 (btree_node_lock_seq_matches(path, b, level) &&
494 btree_node_lock_increment(trans, &b->c, level, want))) {
495 mark_btree_node_locked(trans, path, level, want);
499 if (trace && !trans->notrace_relock_fail)
500 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
506 bool bch2_btree_node_upgrade(struct btree_trans *trans,
507 struct btree_path *path, unsigned level)
509 struct btree *b = path->l[level].b;
510 struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
512 if (!is_btree_node(path, level))
515 switch (btree_lock_want(path, level)) {
516 case BTREE_NODE_UNLOCKED:
517 BUG_ON(btree_node_locked(path, level));
519 case BTREE_NODE_READ_LOCKED:
520 BUG_ON(btree_node_intent_locked(path, level));
521 return bch2_btree_node_relock(trans, path, level);
522 case BTREE_NODE_INTENT_LOCKED:
524 case BTREE_NODE_WRITE_LOCKED:
528 if (btree_node_intent_locked(path, level))
534 if (btree_node_locked(path, level)) {
537 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
538 ret = six_lock_tryupgrade(&b->c.lock);
539 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
544 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
549 * Do we already have an intent lock via another path? If so, just bump
552 if (btree_node_lock_seq_matches(path, b, level) &&
553 btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
554 btree_node_unlock(trans, path, level);
558 trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
561 mark_btree_node_locked_noreset(path, level, BTREE_NODE_INTENT_LOCKED);
565 /* Btree path locking: */
568 * Only for btree_cache.c - only relocks intent locks
570 int bch2_btree_path_relock_intent(struct btree_trans *trans,
571 struct btree_path *path)
575 for (l = path->level;
576 l < path->locks_want && btree_path_node(path, l);
578 if (!bch2_btree_node_relock(trans, path, l)) {
579 __bch2_btree_path_unlock(trans, path);
580 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
581 trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
582 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
590 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
591 struct btree_path *path, unsigned long trace_ip)
593 struct get_locks_fail f;
595 return btree_path_get_locks(trans, path, false, &f);
598 int __bch2_btree_path_relock(struct btree_trans *trans,
599 struct btree_path *path, unsigned long trace_ip)
601 if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
602 trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
603 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
609 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
610 struct btree_path *path,
611 unsigned new_locks_want,
612 struct get_locks_fail *f)
614 EBUG_ON(path->locks_want >= new_locks_want);
616 path->locks_want = new_locks_want;
618 return btree_path_get_locks(trans, path, true, f);
621 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
622 struct btree_path *path,
623 unsigned new_locks_want,
624 struct get_locks_fail *f)
626 struct btree_path *linked;
628 if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want, f))
632 * XXX: this is ugly - we'd prefer to not be mucking with other
633 * iterators in the btree_trans here.
635 * On failure to upgrade the iterator, setting iter->locks_want and
636 * calling get_locks() is sufficient to make bch2_btree_path_traverse()
637 * get the locks we want on transaction restart.
639 * But if this iterator was a clone, on transaction restart what we did
640 * to this iterator isn't going to be preserved.
642 * Possibly we could add an iterator field for the parent iterator when
643 * an iterator is a copy - for now, we'll just upgrade any other
644 * iterators with the same btree id.
646 * The code below used to be needed to ensure ancestor nodes get locked
647 * before interior nodes - now that's handled by
648 * bch2_btree_path_traverse_all().
650 if (!path->cached && !trans->in_traverse_all)
651 trans_for_each_path(trans, linked)
652 if (linked != path &&
653 linked->cached == path->cached &&
654 linked->btree_id == path->btree_id &&
655 linked->locks_want < new_locks_want) {
656 linked->locks_want = new_locks_want;
657 btree_path_get_locks(trans, linked, true, NULL);
663 void __bch2_btree_path_downgrade(struct btree_trans *trans,
664 struct btree_path *path,
665 unsigned new_locks_want)
667 unsigned l, old_locks_want = path->locks_want;
669 if (trans->restarted)
672 EBUG_ON(path->locks_want < new_locks_want);
674 path->locks_want = new_locks_want;
676 while (path->nodes_locked &&
677 (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
678 if (l > path->level) {
679 btree_node_unlock(trans, path, l);
681 if (btree_node_intent_locked(path, l)) {
682 six_lock_downgrade(&path->l[l].b->c.lock);
683 mark_btree_node_locked_noreset(path, l, BTREE_NODE_READ_LOCKED);
689 bch2_btree_path_verify_locks(path);
691 path->downgrade_seq++;
692 trace_path_downgrade(trans, _RET_IP_, path, old_locks_want);
695 /* Btree transaction locking: */
697 void bch2_trans_downgrade(struct btree_trans *trans)
699 struct btree_path *path;
701 if (trans->restarted)
704 trans_for_each_path(trans, path)
705 bch2_btree_path_downgrade(trans, path);
708 int bch2_trans_relock(struct btree_trans *trans)
710 struct btree_path *path;
712 if (unlikely(trans->restarted))
713 return -((int) trans->restarted);
715 trans_for_each_path(trans, path)
716 if (path->should_be_locked &&
717 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
718 trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
719 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
724 int bch2_trans_relock_notrace(struct btree_trans *trans)
726 struct btree_path *path;
728 if (unlikely(trans->restarted))
729 return -((int) trans->restarted);
731 trans_for_each_path(trans, path)
732 if (path->should_be_locked &&
733 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
734 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
739 void bch2_trans_unlock_noassert(struct btree_trans *trans)
741 struct btree_path *path;
743 trans_for_each_path(trans, path)
744 __bch2_btree_path_unlock(trans, path);
747 void bch2_trans_unlock(struct btree_trans *trans)
749 struct btree_path *path;
751 trans_for_each_path(trans, path)
752 __bch2_btree_path_unlock(trans, path);
755 * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
756 * btree nodes, it implements its own walking:
758 if (!trans->is_initial_gc)
759 bch2_assert_btree_nodes_not_locked();
762 void bch2_trans_unlock_long(struct btree_trans *trans)
764 bch2_trans_unlock(trans);
765 bch2_trans_srcu_unlock(trans);
768 bool bch2_trans_locked(struct btree_trans *trans)
770 struct btree_path *path;
772 trans_for_each_path(trans, path)
773 if (path->nodes_locked)
778 int __bch2_trans_mutex_lock(struct btree_trans *trans,
781 int ret = drop_locks_do(trans, (mutex_lock(lock), 0));
790 #ifdef CONFIG_BCACHEFS_DEBUG
792 void bch2_btree_path_verify_locks(struct btree_path *path)
796 if (!path->nodes_locked) {
797 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
798 btree_path_node(path, path->level));
802 for (l = 0; l < BTREE_MAX_DEPTH; l++) {
803 int want = btree_lock_want(path, l);
804 int have = btree_node_locked_type(path, l);
806 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
808 BUG_ON(is_btree_node(path, l) &&
809 (want == BTREE_NODE_UNLOCKED ||
810 have != BTREE_NODE_WRITE_LOCKED) &&
815 void bch2_trans_verify_locks(struct btree_trans *trans)
817 struct btree_path *path;
819 trans_for_each_path(trans, path)
820 bch2_btree_path_verify_locks(path);