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;
36 memset(&ret, 0, sizeof(ret));
38 if (IS_ERR_OR_NULL(b))
41 trans_for_each_path(trans, path)
42 if (path != skip && &path->l[level].b->c == b) {
43 int t = btree_node_locked_type(path, level);
45 if (t != BTREE_NODE_UNLOCKED)
54 void bch2_btree_node_unlock_write(struct btree_trans *trans,
55 struct btree_path *path, struct btree *b)
57 bch2_btree_node_unlock_write_inlined(trans, path, b);
63 * @trans wants to lock @b with type @type
65 struct trans_waiting_for_lock {
66 struct btree_trans *trans;
67 struct btree_bkey_cached_common *node_want;
68 enum six_lock_type lock_want;
70 /* for iterating over held locks :*/
77 struct trans_waiting_for_lock g[8];
81 static noinline void print_cycle(struct printbuf *out, struct lock_graph *g)
83 struct trans_waiting_for_lock *i;
85 prt_printf(out, "Found lock cycle (%u entries):", g->nr);
88 for (i = g->g; i < g->g + g->nr; i++)
89 bch2_btree_trans_to_text(out, i->trans);
92 static noinline void print_chain(struct printbuf *out, struct lock_graph *g)
94 struct trans_waiting_for_lock *i;
96 for (i = g->g; i != g->g + g->nr; i++) {
99 prt_printf(out, "%u ", i->trans->locking_wait.task->pid);
104 static void lock_graph_up(struct lock_graph *g)
106 closure_put(&g->g[--g->nr].trans->ref);
109 static noinline void lock_graph_pop_all(struct lock_graph *g)
115 static void __lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
117 g->g[g->nr++] = (struct trans_waiting_for_lock) {
119 .node_want = trans->locking,
120 .lock_want = trans->locking_wait.lock_want,
124 static void lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
126 closure_get(&trans->ref);
127 __lock_graph_down(g, trans);
130 static bool lock_graph_remove_non_waiters(struct lock_graph *g)
132 struct trans_waiting_for_lock *i;
134 for (i = g->g + 1; i < g->g + g->nr; i++)
135 if (i->trans->locking != i->node_want ||
136 i->trans->locking_wait.start_time != i[-1].lock_start_time) {
137 while (g->g + g->nr > i)
145 static void trace_would_deadlock(struct lock_graph *g, struct btree_trans *trans,
148 struct bch_fs *c = trans->c;
150 count_event(c, trans_restart_would_deadlock);
152 if (trace_trans_restart_would_deadlock_enabled()) {
153 struct printbuf buf = PRINTBUF;
156 print_cycle(&buf, g);
158 trace_trans_restart_would_deadlock(trans, ip, buf.buf);
163 static int abort_lock(struct lock_graph *g, struct trans_waiting_for_lock *i)
166 trace_would_deadlock(g, i->trans, _RET_IP_);
167 return btree_trans_restart(i->trans, BCH_ERR_transaction_restart_would_deadlock);
169 i->trans->lock_must_abort = true;
170 wake_up_process(i->trans->locking_wait.task);
175 static int btree_trans_abort_preference(struct btree_trans *trans)
177 if (trans->lock_may_not_fail)
179 if (trans->locking_wait.lock_want == SIX_LOCK_write)
181 if (!trans->in_traverse_all)
186 static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
188 struct trans_waiting_for_lock *i, *abort = NULL;
189 unsigned best = 0, pref;
192 if (lock_graph_remove_non_waiters(g))
195 /* Only checking, for debugfs: */
197 print_cycle(cycle, g);
202 for (i = g->g; i < g->g + g->nr; i++) {
203 pref = btree_trans_abort_preference(i->trans);
210 if (unlikely(!best)) {
211 struct printbuf buf = PRINTBUF;
213 prt_printf(&buf, bch2_fmt(g->g->trans->c, "cycle of nofail locks"));
215 for (i = g->g; i < g->g + g->nr; i++) {
216 struct btree_trans *trans = i->trans;
218 bch2_btree_trans_to_text(&buf, trans);
220 prt_printf(&buf, "backtrace:");
222 printbuf_indent_add(&buf, 2);
223 bch2_prt_task_backtrace(&buf, trans->locking_wait.task);
224 printbuf_indent_sub(&buf, 2);
228 bch2_print_string_as_lines(KERN_ERR, buf.buf);
233 ret = abort_lock(g, abort);
241 static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
242 struct printbuf *cycle)
244 struct btree_trans *orig_trans = g->g->trans;
245 struct trans_waiting_for_lock *i;
247 for (i = g->g; i < g->g + g->nr; i++)
248 if (i->trans == trans) {
249 closure_put(&trans->ref);
250 return break_cycle(g, cycle);
253 if (g->nr == ARRAY_SIZE(g->g)) {
254 closure_put(&trans->ref);
256 if (orig_trans->lock_may_not_fail)
265 trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
266 return btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
269 __lock_graph_down(g, trans);
273 static bool lock_type_conflicts(enum six_lock_type t1, enum six_lock_type t2)
278 int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
281 struct trans_waiting_for_lock *top;
282 struct btree_bkey_cached_common *b;
283 struct btree_path *path;
289 if (trans->lock_must_abort) {
293 trace_would_deadlock(&g, trans, _RET_IP_);
294 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
297 lock_graph_down(&g, trans);
302 top = &g.g[g.nr - 1];
304 trans_for_each_path_safe_from(top->trans, path, path_idx, top->path_idx) {
305 if (!path->nodes_locked)
308 if (path_idx != top->path_idx) {
309 top->path_idx = path_idx;
311 top->lock_start_time = 0;
315 top->level < BTREE_MAX_DEPTH;
316 top->level++, top->lock_start_time = 0) {
317 int lock_held = btree_node_locked_type(path, top->level);
319 if (lock_held == BTREE_NODE_UNLOCKED)
322 b = &READ_ONCE(path->l[top->level].b)->c;
324 if (IS_ERR_OR_NULL(b)) {
326 * If we get here, it means we raced with the
327 * other thread updating its btree_path
328 * structures - which means it can't be blocked
331 if (!lock_graph_remove_non_waiters(&g)) {
333 * If lock_graph_remove_non_waiters()
334 * didn't do anything, it must be
335 * because we're being called by debugfs
336 * checking for lock cycles, which
337 * invokes us on btree_transactions that
338 * aren't actually waiting on anything.
341 lock_graph_pop_all(&g);
347 if (list_empty_careful(&b->lock.wait_list))
350 raw_spin_lock(&b->lock.wait_lock);
351 list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
352 BUG_ON(b != trans->locking);
354 if (top->lock_start_time &&
355 time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
358 top->lock_start_time = trans->locking_wait.start_time;
360 /* Don't check for self deadlock: */
361 if (trans == top->trans ||
362 !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
365 closure_get(&trans->ref);
366 raw_spin_unlock(&b->lock.wait_lock);
368 ret = lock_graph_descend(&g, trans, cycle);
374 raw_spin_unlock(&b->lock.wait_lock);
378 if (g.nr > 1 && cycle)
379 print_chain(cycle, &g);
384 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
386 struct btree_trans *trans = p;
388 return bch2_check_for_deadlock(trans, NULL);
391 int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
392 struct btree_bkey_cached_common *b,
393 bool lock_may_not_fail)
395 int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
399 * Must drop our read locks before calling six_lock_write() -
400 * six_unlock() won't do wakeups until the reader count
401 * goes to 0, and it's safe because we have the node intent
404 six_lock_readers_add(&b->lock, -readers);
405 ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write,
406 lock_may_not_fail, _RET_IP_);
407 six_lock_readers_add(&b->lock, readers);
410 mark_btree_node_locked_noreset(path, b->level, BTREE_NODE_INTENT_LOCKED);
415 void bch2_btree_node_lock_write_nofail(struct btree_trans *trans,
416 struct btree_path *path,
417 struct btree_bkey_cached_common *b)
419 struct btree_path *linked;
426 * Drop all read locks before taking a write lock:
428 * This is a hack, because bch2_btree_node_lock_write_nofail() is a
429 * hack - but by dropping read locks first, this should never fail, and
430 * we only use this in code paths where whatever read locks we've
431 * already taken are no longer needed:
434 trans_for_each_path(trans, linked) {
435 if (!linked->nodes_locked)
438 for (i = 0; i < BTREE_MAX_DEPTH; i++)
439 if (btree_node_read_locked(linked, i)) {
440 btree_node_unlock(trans, linked, i);
441 btree_path_set_dirty(linked, BTREE_ITER_NEED_RELOCK);
445 ret = __btree_node_lock_write(trans, path, b, true);
451 static inline bool btree_path_get_locks(struct btree_trans *trans,
452 struct btree_path *path,
454 struct get_locks_fail *f)
456 unsigned l = path->level;
460 if (!btree_path_node(path, l))
464 ? bch2_btree_node_upgrade(trans, path, l)
465 : bch2_btree_node_relock(trans, path, l))) {
475 } while (l < path->locks_want);
478 * When we fail to get a lock, we have to ensure that any child nodes
479 * can't be relocked so bch2_btree_path_traverse has to walk back up to
480 * the node that we failed to relock:
483 __bch2_btree_path_unlock(trans, path);
484 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
487 path->l[fail_idx].b = upgrade
488 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
489 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
491 } while (fail_idx >= 0);
494 if (path->uptodate == BTREE_ITER_NEED_RELOCK)
495 path->uptodate = BTREE_ITER_UPTODATE;
497 bch2_trans_verify_locks(trans);
499 return path->uptodate < BTREE_ITER_NEED_RELOCK;
502 bool __bch2_btree_node_relock(struct btree_trans *trans,
503 struct btree_path *path, unsigned level,
506 struct btree *b = btree_path_node(path, level);
507 int want = __btree_lock_want(path, level);
512 if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
513 (btree_node_lock_seq_matches(path, b, level) &&
514 btree_node_lock_increment(trans, &b->c, level, want))) {
515 mark_btree_node_locked(trans, path, level, want);
519 if (trace && !trans->notrace_relock_fail)
520 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
526 bool bch2_btree_node_upgrade(struct btree_trans *trans,
527 struct btree_path *path, unsigned level)
529 struct btree *b = path->l[level].b;
530 struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
532 if (!is_btree_node(path, level))
535 switch (btree_lock_want(path, level)) {
536 case BTREE_NODE_UNLOCKED:
537 BUG_ON(btree_node_locked(path, level));
539 case BTREE_NODE_READ_LOCKED:
540 BUG_ON(btree_node_intent_locked(path, level));
541 return bch2_btree_node_relock(trans, path, level);
542 case BTREE_NODE_INTENT_LOCKED:
544 case BTREE_NODE_WRITE_LOCKED:
548 if (btree_node_intent_locked(path, level))
554 if (btree_node_locked(path, level)) {
557 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
558 ret = six_lock_tryupgrade(&b->c.lock);
559 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
564 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
569 * Do we already have an intent lock via another path? If so, just bump
572 if (btree_node_lock_seq_matches(path, b, level) &&
573 btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
574 btree_node_unlock(trans, path, level);
578 trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
581 mark_btree_node_locked_noreset(path, level, BTREE_NODE_INTENT_LOCKED);
585 /* Btree path locking: */
588 * Only for btree_cache.c - only relocks intent locks
590 int bch2_btree_path_relock_intent(struct btree_trans *trans,
591 struct btree_path *path)
595 for (l = path->level;
596 l < path->locks_want && btree_path_node(path, l);
598 if (!bch2_btree_node_relock(trans, path, l)) {
599 __bch2_btree_path_unlock(trans, path);
600 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
601 trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
602 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
610 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
611 struct btree_path *path, unsigned long trace_ip)
613 struct get_locks_fail f;
615 return btree_path_get_locks(trans, path, false, &f);
618 int __bch2_btree_path_relock(struct btree_trans *trans,
619 struct btree_path *path, unsigned long trace_ip)
621 if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
622 trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
623 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
629 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
630 struct btree_path *path,
631 unsigned new_locks_want,
632 struct get_locks_fail *f)
634 EBUG_ON(path->locks_want >= new_locks_want);
636 path->locks_want = new_locks_want;
638 return btree_path_get_locks(trans, path, true, f);
641 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
642 struct btree_path *path,
643 unsigned new_locks_want,
644 struct get_locks_fail *f)
646 struct btree_path *linked;
648 if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want, f))
652 * XXX: this is ugly - we'd prefer to not be mucking with other
653 * iterators in the btree_trans here.
655 * On failure to upgrade the iterator, setting iter->locks_want and
656 * calling get_locks() is sufficient to make bch2_btree_path_traverse()
657 * get the locks we want on transaction restart.
659 * But if this iterator was a clone, on transaction restart what we did
660 * to this iterator isn't going to be preserved.
662 * Possibly we could add an iterator field for the parent iterator when
663 * an iterator is a copy - for now, we'll just upgrade any other
664 * iterators with the same btree id.
666 * The code below used to be needed to ensure ancestor nodes get locked
667 * before interior nodes - now that's handled by
668 * bch2_btree_path_traverse_all().
670 if (!path->cached && !trans->in_traverse_all)
671 trans_for_each_path(trans, linked)
672 if (linked != path &&
673 linked->cached == path->cached &&
674 linked->btree_id == path->btree_id &&
675 linked->locks_want < new_locks_want) {
676 linked->locks_want = new_locks_want;
677 btree_path_get_locks(trans, linked, true, NULL);
683 void __bch2_btree_path_downgrade(struct btree_trans *trans,
684 struct btree_path *path,
685 unsigned new_locks_want)
687 unsigned l, old_locks_want = path->locks_want;
689 if (trans->restarted)
692 EBUG_ON(path->locks_want < new_locks_want);
694 path->locks_want = new_locks_want;
696 while (path->nodes_locked &&
697 (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
698 if (l > path->level) {
699 btree_node_unlock(trans, path, l);
701 if (btree_node_intent_locked(path, l)) {
702 six_lock_downgrade(&path->l[l].b->c.lock);
703 mark_btree_node_locked_noreset(path, l, BTREE_NODE_READ_LOCKED);
709 bch2_btree_path_verify_locks(path);
711 path->downgrade_seq++;
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;
721 if (trans->restarted)
724 trans_for_each_path(trans, path)
725 bch2_btree_path_downgrade(trans, path);
728 int bch2_trans_relock(struct btree_trans *trans)
730 struct btree_path *path;
732 if (unlikely(trans->restarted))
733 return -((int) trans->restarted);
735 trans_for_each_path(trans, path)
736 if (path->should_be_locked &&
737 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
738 trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
739 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
744 int bch2_trans_relock_notrace(struct btree_trans *trans)
746 struct btree_path *path;
748 if (unlikely(trans->restarted))
749 return -((int) trans->restarted);
751 trans_for_each_path(trans, path)
752 if (path->should_be_locked &&
753 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
754 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
759 void bch2_trans_unlock_noassert(struct btree_trans *trans)
761 struct btree_path *path;
763 trans_for_each_path(trans, path)
764 __bch2_btree_path_unlock(trans, path);
767 void bch2_trans_unlock(struct btree_trans *trans)
769 struct btree_path *path;
771 trans_for_each_path(trans, path)
772 __bch2_btree_path_unlock(trans, path);
775 void bch2_trans_unlock_long(struct btree_trans *trans)
777 bch2_trans_unlock(trans);
778 bch2_trans_srcu_unlock(trans);
781 bool bch2_trans_locked(struct btree_trans *trans)
783 struct btree_path *path;
785 trans_for_each_path(trans, path)
786 if (path->nodes_locked)
791 int __bch2_trans_mutex_lock(struct btree_trans *trans,
794 int ret = drop_locks_do(trans, (mutex_lock(lock), 0));
803 #ifdef CONFIG_BCACHEFS_DEBUG
805 void bch2_btree_path_verify_locks(struct btree_path *path)
809 if (!path->nodes_locked) {
810 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
811 btree_path_node(path, path->level));
815 for (l = 0; l < BTREE_MAX_DEPTH; l++) {
816 int want = btree_lock_want(path, l);
817 int have = btree_node_locked_type(path, l);
819 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
821 BUG_ON(is_btree_node(path, l) &&
822 (want == BTREE_NODE_UNLOCKED ||
823 have != BTREE_NODE_WRITE_LOCKED) &&
828 void bch2_trans_verify_locks(struct btree_trans *trans)
830 struct btree_path *path;
832 trans_for_each_path(trans, path)
833 bch2_btree_path_verify_locks(path);