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 lockdep_set_novalidate_class(&b->lock);
17 void bch2_assert_btree_nodes_not_locked(void)
20 //Re-enable when lock_class_is_held() is merged:
21 BUG_ON(lock_class_is_held(&bch2_btree_node_lock_key));
26 /* Btree node locking: */
28 struct six_lock_count bch2_btree_node_lock_counts(struct btree_trans *trans,
29 struct btree_path *skip,
30 struct btree_bkey_cached_common *b,
33 struct btree_path *path;
34 struct six_lock_count ret;
37 memset(&ret, 0, sizeof(ret));
39 if (IS_ERR_OR_NULL(b))
42 trans_for_each_path(trans, path, i)
43 if (path != skip && &path->l[level].b->c == b) {
44 int t = btree_node_locked_type(path, level);
46 if (t != BTREE_NODE_UNLOCKED)
55 void bch2_btree_node_unlock_write(struct btree_trans *trans,
56 struct btree_path *path, struct btree *b)
58 bch2_btree_node_unlock_write_inlined(trans, path, b);
64 * @trans wants to lock @b with type @type
66 struct trans_waiting_for_lock {
67 struct btree_trans *trans;
68 struct btree_bkey_cached_common *node_want;
69 enum six_lock_type lock_want;
71 /* for iterating over held locks :*/
78 struct trans_waiting_for_lock g[8];
82 static noinline void print_cycle(struct printbuf *out, struct lock_graph *g)
84 struct trans_waiting_for_lock *i;
86 prt_printf(out, "Found lock cycle (%u entries):\n", g->nr);
88 for (i = g->g; i < g->g + g->nr; i++) {
89 struct task_struct *task = READ_ONCE(i->trans->locking_wait.task);
93 bch2_btree_trans_to_text(out, i->trans);
94 bch2_prt_task_backtrace(out, task, i == g->g ? 5 : 1, GFP_NOWAIT);
98 static noinline void print_chain(struct printbuf *out, struct lock_graph *g)
100 struct trans_waiting_for_lock *i;
102 for (i = g->g; i != g->g + g->nr; i++) {
103 struct task_struct *task = i->trans->locking_wait.task;
106 prt_printf(out, "%u ", task ?task->pid : 0);
111 static void lock_graph_up(struct lock_graph *g)
113 closure_put(&g->g[--g->nr].trans->ref);
116 static noinline void lock_graph_pop_all(struct lock_graph *g)
122 static void __lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
124 g->g[g->nr++] = (struct trans_waiting_for_lock) {
126 .node_want = trans->locking,
127 .lock_want = trans->locking_wait.lock_want,
131 static void lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
133 closure_get(&trans->ref);
134 __lock_graph_down(g, trans);
137 static bool lock_graph_remove_non_waiters(struct lock_graph *g)
139 struct trans_waiting_for_lock *i;
141 for (i = g->g + 1; i < g->g + g->nr; i++)
142 if (i->trans->locking != i->node_want ||
143 i->trans->locking_wait.start_time != i[-1].lock_start_time) {
144 while (g->g + g->nr > i)
152 static void trace_would_deadlock(struct lock_graph *g, struct btree_trans *trans)
154 struct bch_fs *c = trans->c;
156 count_event(c, trans_restart_would_deadlock);
158 if (trace_trans_restart_would_deadlock_enabled()) {
159 struct printbuf buf = PRINTBUF;
162 print_cycle(&buf, g);
164 trace_trans_restart_would_deadlock(trans, buf.buf);
169 static int abort_lock(struct lock_graph *g, struct trans_waiting_for_lock *i)
172 trace_would_deadlock(g, i->trans);
173 return btree_trans_restart(i->trans, BCH_ERR_transaction_restart_would_deadlock);
175 i->trans->lock_must_abort = true;
176 wake_up_process(i->trans->locking_wait.task);
181 static int btree_trans_abort_preference(struct btree_trans *trans)
183 if (trans->lock_may_not_fail)
185 if (trans->locking_wait.lock_want == SIX_LOCK_write)
187 if (!trans->in_traverse_all)
192 static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
194 struct trans_waiting_for_lock *i, *abort = NULL;
195 unsigned best = 0, pref;
198 if (lock_graph_remove_non_waiters(g))
201 /* Only checking, for debugfs: */
203 print_cycle(cycle, g);
208 for (i = g->g; i < g->g + g->nr; i++) {
209 pref = btree_trans_abort_preference(i->trans);
216 if (unlikely(!best)) {
217 struct printbuf buf = PRINTBUF;
219 prt_printf(&buf, bch2_fmt(g->g->trans->c, "cycle of nofail locks"));
221 for (i = g->g; i < g->g + g->nr; i++) {
222 struct btree_trans *trans = i->trans;
224 bch2_btree_trans_to_text(&buf, trans);
226 prt_printf(&buf, "backtrace:\n");
227 printbuf_indent_add(&buf, 2);
228 bch2_prt_task_backtrace(&buf, trans->locking_wait.task, 2, GFP_NOWAIT);
229 printbuf_indent_sub(&buf, 2);
233 bch2_print_string_as_lines(KERN_ERR, buf.buf);
238 ret = abort_lock(g, abort);
246 static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
247 struct printbuf *cycle)
249 struct btree_trans *orig_trans = g->g->trans;
250 struct trans_waiting_for_lock *i;
252 for (i = g->g; i < g->g + g->nr; i++)
253 if (i->trans == trans) {
254 closure_put(&trans->ref);
255 return break_cycle(g, cycle);
258 if (g->nr == ARRAY_SIZE(g->g)) {
259 closure_put(&trans->ref);
261 if (orig_trans->lock_may_not_fail)
270 trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
271 return btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
274 __lock_graph_down(g, trans);
278 static bool lock_type_conflicts(enum six_lock_type t1, enum six_lock_type t2)
283 int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
286 struct trans_waiting_for_lock *top;
287 struct btree_bkey_cached_common *b;
288 btree_path_idx_t path_idx;
293 if (trans->lock_must_abort) {
297 trace_would_deadlock(&g, trans);
298 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
301 lock_graph_down(&g, trans);
303 /* trans->paths is rcu protected vs. freeing */
311 top = &g.g[g.nr - 1];
313 struct btree_path *paths = rcu_dereference(top->trans->paths);
317 unsigned long *paths_allocated = trans_paths_allocated(paths);
319 trans_for_each_path_idx_from(paths_allocated, *trans_paths_nr(paths),
320 path_idx, top->path_idx) {
321 struct btree_path *path = paths + path_idx;
322 if (!path->nodes_locked)
325 if (path_idx != top->path_idx) {
326 top->path_idx = path_idx;
328 top->lock_start_time = 0;
332 top->level < BTREE_MAX_DEPTH;
333 top->level++, top->lock_start_time = 0) {
334 int lock_held = btree_node_locked_type(path, top->level);
336 if (lock_held == BTREE_NODE_UNLOCKED)
339 b = &READ_ONCE(path->l[top->level].b)->c;
341 if (IS_ERR_OR_NULL(b)) {
343 * If we get here, it means we raced with the
344 * other thread updating its btree_path
345 * structures - which means it can't be blocked
348 if (!lock_graph_remove_non_waiters(&g)) {
350 * If lock_graph_remove_non_waiters()
351 * didn't do anything, it must be
352 * because we're being called by debugfs
353 * checking for lock cycles, which
354 * invokes us on btree_transactions that
355 * aren't actually waiting on anything.
358 lock_graph_pop_all(&g);
364 if (list_empty_careful(&b->lock.wait_list))
367 raw_spin_lock(&b->lock.wait_lock);
368 list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
369 BUG_ON(b != trans->locking);
371 if (top->lock_start_time &&
372 time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
375 top->lock_start_time = trans->locking_wait.start_time;
377 /* Don't check for self deadlock: */
378 if (trans == top->trans ||
379 !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
382 closure_get(&trans->ref);
383 raw_spin_unlock(&b->lock.wait_lock);
385 ret = lock_graph_descend(&g, trans, cycle);
391 raw_spin_unlock(&b->lock.wait_lock);
395 if (g.nr > 1 && cycle)
396 print_chain(cycle, &g);
406 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
408 struct btree_trans *trans = p;
410 return bch2_check_for_deadlock(trans, NULL);
413 int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
414 struct btree_bkey_cached_common *b,
415 bool lock_may_not_fail)
417 int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
421 * Must drop our read locks before calling six_lock_write() -
422 * six_unlock() won't do wakeups until the reader count
423 * goes to 0, and it's safe because we have the node intent
426 six_lock_readers_add(&b->lock, -readers);
427 ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write,
428 lock_may_not_fail, _RET_IP_);
429 six_lock_readers_add(&b->lock, readers);
432 mark_btree_node_locked_noreset(path, b->level, BTREE_NODE_INTENT_LOCKED);
437 void bch2_btree_node_lock_write_nofail(struct btree_trans *trans,
438 struct btree_path *path,
439 struct btree_bkey_cached_common *b)
441 int ret = __btree_node_lock_write(trans, path, b, true);
447 static inline bool btree_path_get_locks(struct btree_trans *trans,
448 struct btree_path *path,
450 struct get_locks_fail *f)
452 unsigned l = path->level;
456 if (!btree_path_node(path, l))
460 ? bch2_btree_node_upgrade(trans, path, l)
461 : bch2_btree_node_relock(trans, path, l))) {
471 } while (l < path->locks_want);
474 * When we fail to get a lock, we have to ensure that any child nodes
475 * can't be relocked so bch2_btree_path_traverse has to walk back up to
476 * the node that we failed to relock:
479 __bch2_btree_path_unlock(trans, path);
480 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
483 path->l[fail_idx].b = upgrade
484 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
485 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
487 } while (fail_idx >= 0);
490 if (path->uptodate == BTREE_ITER_NEED_RELOCK)
491 path->uptodate = BTREE_ITER_UPTODATE;
493 return path->uptodate < BTREE_ITER_NEED_RELOCK;
496 bool __bch2_btree_node_relock(struct btree_trans *trans,
497 struct btree_path *path, unsigned level,
500 struct btree *b = btree_path_node(path, level);
501 int want = __btree_lock_want(path, level);
506 if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
507 (btree_node_lock_seq_matches(path, b, level) &&
508 btree_node_lock_increment(trans, &b->c, level, want))) {
509 mark_btree_node_locked(trans, path, level, want);
513 if (trace && !trans->notrace_relock_fail)
514 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
520 bool bch2_btree_node_upgrade(struct btree_trans *trans,
521 struct btree_path *path, unsigned level)
523 struct btree *b = path->l[level].b;
524 struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
526 if (!is_btree_node(path, level))
529 switch (btree_lock_want(path, level)) {
530 case BTREE_NODE_UNLOCKED:
531 BUG_ON(btree_node_locked(path, level));
533 case BTREE_NODE_READ_LOCKED:
534 BUG_ON(btree_node_intent_locked(path, level));
535 return bch2_btree_node_relock(trans, path, level);
536 case BTREE_NODE_INTENT_LOCKED:
538 case BTREE_NODE_WRITE_LOCKED:
542 if (btree_node_intent_locked(path, level))
548 if (btree_node_locked(path, level)) {
551 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
552 ret = six_lock_tryupgrade(&b->c.lock);
553 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
558 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
563 * Do we already have an intent lock via another path? If so, just bump
566 if (btree_node_lock_seq_matches(path, b, level) &&
567 btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
568 btree_node_unlock(trans, path, level);
572 trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
575 mark_btree_node_locked_noreset(path, level, BTREE_NODE_INTENT_LOCKED);
579 /* Btree path locking: */
582 * Only for btree_cache.c - only relocks intent locks
584 int bch2_btree_path_relock_intent(struct btree_trans *trans,
585 struct btree_path *path)
589 for (l = path->level;
590 l < path->locks_want && btree_path_node(path, l);
592 if (!bch2_btree_node_relock(trans, path, l)) {
593 __bch2_btree_path_unlock(trans, path);
594 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
595 trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
596 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
604 bool bch2_btree_path_relock_norestart(struct btree_trans *trans, struct btree_path *path)
606 struct get_locks_fail f;
608 bool ret = btree_path_get_locks(trans, path, false, &f);
609 bch2_trans_verify_locks(trans);
613 int __bch2_btree_path_relock(struct btree_trans *trans,
614 struct btree_path *path, unsigned long trace_ip)
616 if (!bch2_btree_path_relock_norestart(trans, path)) {
617 trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
618 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
624 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
625 struct btree_path *path,
626 unsigned new_locks_want,
627 struct get_locks_fail *f)
629 EBUG_ON(path->locks_want >= new_locks_want);
631 path->locks_want = new_locks_want;
633 bool ret = btree_path_get_locks(trans, path, true, f);
634 bch2_trans_verify_locks(trans);
638 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
639 struct btree_path *path,
640 unsigned new_locks_want,
641 struct get_locks_fail *f)
643 bool ret = bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want, f);
648 * XXX: this is ugly - we'd prefer to not be mucking with other
649 * iterators in the btree_trans here.
651 * On failure to upgrade the iterator, setting iter->locks_want and
652 * calling get_locks() is sufficient to make bch2_btree_path_traverse()
653 * get the locks we want on transaction restart.
655 * But if this iterator was a clone, on transaction restart what we did
656 * to this iterator isn't going to be preserved.
658 * Possibly we could add an iterator field for the parent iterator when
659 * an iterator is a copy - for now, we'll just upgrade any other
660 * iterators with the same btree id.
662 * The code below used to be needed to ensure ancestor nodes get locked
663 * before interior nodes - now that's handled by
664 * bch2_btree_path_traverse_all().
666 if (!path->cached && !trans->in_traverse_all) {
667 struct btree_path *linked;
670 trans_for_each_path(trans, linked, i)
671 if (linked != path &&
672 linked->cached == path->cached &&
673 linked->btree_id == path->btree_id &&
674 linked->locks_want < new_locks_want) {
675 linked->locks_want = new_locks_want;
676 btree_path_get_locks(trans, linked, true, NULL);
680 bch2_trans_verify_locks(trans);
684 void __bch2_btree_path_downgrade(struct btree_trans *trans,
685 struct btree_path *path,
686 unsigned new_locks_want)
688 unsigned l, old_locks_want = path->locks_want;
690 if (trans->restarted)
693 EBUG_ON(path->locks_want < new_locks_want);
695 path->locks_want = new_locks_want;
697 while (path->nodes_locked &&
698 (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
699 if (l > path->level) {
700 btree_node_unlock(trans, path, l);
702 if (btree_node_intent_locked(path, l)) {
703 six_lock_downgrade(&path->l[l].b->c.lock);
704 mark_btree_node_locked_noreset(path, l, BTREE_NODE_READ_LOCKED);
710 bch2_btree_path_verify_locks(path);
712 trace_path_downgrade(trans, _RET_IP_, path, old_locks_want);
715 /* Btree transaction locking: */
717 void bch2_trans_downgrade(struct btree_trans *trans)
719 struct btree_path *path;
722 if (trans->restarted)
725 trans_for_each_path(trans, path, i)
727 bch2_btree_path_downgrade(trans, path);
730 static inline void __bch2_trans_unlock(struct btree_trans *trans)
732 struct btree_path *path;
735 trans_for_each_path(trans, path, i)
736 __bch2_btree_path_unlock(trans, path);
739 static noinline __cold int bch2_trans_relock_fail(struct btree_trans *trans, struct btree_path *path,
740 struct get_locks_fail *f, bool trace)
745 if (trace_trans_restart_relock_enabled()) {
746 struct printbuf buf = PRINTBUF;
748 bch2_bpos_to_text(&buf, path->pos);
749 prt_printf(&buf, " l=%u seq=%u node seq=", f->l, path->l[f->l].lock_seq);
750 if (IS_ERR_OR_NULL(f->b)) {
751 prt_str(&buf, bch2_err_str(PTR_ERR(f->b)));
753 prt_printf(&buf, "%u", f->b->c.lock.seq);
755 struct six_lock_count c =
756 bch2_btree_node_lock_counts(trans, NULL, &f->b->c, f->l);
757 prt_printf(&buf, " self locked %u.%u.%u", c.n[0], c.n[1], c.n[2]);
759 c = six_lock_counts(&f->b->c.lock);
760 prt_printf(&buf, " total locked %u.%u.%u", c.n[0], c.n[1], c.n[2]);
763 trace_trans_restart_relock(trans, _RET_IP_, buf.buf);
767 count_event(trans->c, trans_restart_relock);
769 __bch2_trans_unlock(trans);
770 bch2_trans_verify_locks(trans);
771 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
774 static inline int __bch2_trans_relock(struct btree_trans *trans, bool trace)
776 bch2_trans_verify_locks(trans);
778 if (unlikely(trans->restarted))
779 return -((int) trans->restarted);
780 if (unlikely(trans->locked))
783 struct btree_path *path;
786 trans_for_each_path(trans, path, i) {
787 struct get_locks_fail f;
789 if (path->should_be_locked &&
790 !btree_path_get_locks(trans, path, false, &f))
791 return bch2_trans_relock_fail(trans, path, &f, trace);
794 trans->locked = true;
796 bch2_trans_verify_locks(trans);
800 int bch2_trans_relock(struct btree_trans *trans)
802 return __bch2_trans_relock(trans, true);
805 int bch2_trans_relock_notrace(struct btree_trans *trans)
807 return __bch2_trans_relock(trans, false);
810 void bch2_trans_unlock_noassert(struct btree_trans *trans)
812 __bch2_trans_unlock(trans);
814 trans->locked = false;
815 trans->last_unlock_ip = _RET_IP_;
818 void bch2_trans_unlock(struct btree_trans *trans)
820 __bch2_trans_unlock(trans);
822 trans->locked = false;
823 trans->last_unlock_ip = _RET_IP_;
826 void bch2_trans_unlock_long(struct btree_trans *trans)
828 bch2_trans_unlock(trans);
829 bch2_trans_srcu_unlock(trans);
832 int __bch2_trans_mutex_lock(struct btree_trans *trans,
835 int ret = drop_locks_do(trans, (mutex_lock(lock), 0));
844 #ifdef CONFIG_BCACHEFS_DEBUG
846 void bch2_btree_path_verify_locks(struct btree_path *path)
849 * A path may be uptodate and yet have nothing locked if and only if
850 * there is no node at path->level, which generally means we were
851 * iterating over all nodes and got to the end of the btree
853 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
854 btree_path_node(path, path->level) &&
855 !path->nodes_locked);
857 if (!path->nodes_locked)
860 for (unsigned l = 0; l < BTREE_MAX_DEPTH; l++) {
861 int want = btree_lock_want(path, l);
862 int have = btree_node_locked_type(path, l);
864 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
866 BUG_ON(is_btree_node(path, l) &&
867 (want == BTREE_NODE_UNLOCKED ||
868 have != BTREE_NODE_WRITE_LOCKED) &&
873 static bool bch2_trans_locked(struct btree_trans *trans)
875 struct btree_path *path;
878 trans_for_each_path(trans, path, i)
879 if (path->nodes_locked)
884 void bch2_trans_verify_locks(struct btree_trans *trans)
886 if (!trans->locked) {
887 BUG_ON(bch2_trans_locked(trans));
891 struct btree_path *path;
894 trans_for_each_path(trans, path, i)
895 bch2_btree_path_verify_locks(path);