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,
435 unsigned l = path->level;
439 if (!btree_path_node(path, l))
443 ? bch2_btree_node_upgrade(trans, path, l)
444 : bch2_btree_node_relock(trans, path, l)))
448 } while (l < path->locks_want);
451 * When we fail to get a lock, we have to ensure that any child nodes
452 * can't be relocked so bch2_btree_path_traverse has to walk back up to
453 * the node that we failed to relock:
456 __bch2_btree_path_unlock(trans, path);
457 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
460 path->l[fail_idx].b = upgrade
461 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
462 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
464 } while (fail_idx >= 0);
467 if (path->uptodate == BTREE_ITER_NEED_RELOCK)
468 path->uptodate = BTREE_ITER_UPTODATE;
470 bch2_trans_verify_locks(trans);
472 return path->uptodate < BTREE_ITER_NEED_RELOCK;
475 bool __bch2_btree_node_relock(struct btree_trans *trans,
476 struct btree_path *path, unsigned level,
479 struct btree *b = btree_path_node(path, level);
480 int want = __btree_lock_want(path, level);
485 if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
486 (btree_node_lock_seq_matches(path, b, level) &&
487 btree_node_lock_increment(trans, &b->c, level, want))) {
488 mark_btree_node_locked(trans, path, level, want);
492 if (trace && !trans->notrace_relock_fail)
493 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
499 bool bch2_btree_node_upgrade(struct btree_trans *trans,
500 struct btree_path *path, unsigned level)
502 struct btree *b = path->l[level].b;
503 struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
505 if (!is_btree_node(path, level))
508 switch (btree_lock_want(path, level)) {
509 case BTREE_NODE_UNLOCKED:
510 BUG_ON(btree_node_locked(path, level));
512 case BTREE_NODE_READ_LOCKED:
513 BUG_ON(btree_node_intent_locked(path, level));
514 return bch2_btree_node_relock(trans, path, level);
515 case BTREE_NODE_INTENT_LOCKED:
517 case BTREE_NODE_WRITE_LOCKED:
521 if (btree_node_intent_locked(path, level))
527 if (btree_node_locked(path, level)) {
530 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
531 ret = six_lock_tryupgrade(&b->c.lock);
532 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
537 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
542 * Do we already have an intent lock via another path? If so, just bump
545 if (btree_node_lock_seq_matches(path, b, level) &&
546 btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
547 btree_node_unlock(trans, path, level);
551 trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
554 mark_btree_node_locked_noreset(path, level, BTREE_NODE_INTENT_LOCKED);
558 /* Btree path locking: */
561 * Only for btree_cache.c - only relocks intent locks
563 int bch2_btree_path_relock_intent(struct btree_trans *trans,
564 struct btree_path *path)
568 for (l = path->level;
569 l < path->locks_want && btree_path_node(path, l);
571 if (!bch2_btree_node_relock(trans, path, l)) {
572 __bch2_btree_path_unlock(trans, path);
573 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
574 trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
575 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
583 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
584 struct btree_path *path, unsigned long trace_ip)
586 return btree_path_get_locks(trans, path, false);
589 int __bch2_btree_path_relock(struct btree_trans *trans,
590 struct btree_path *path, unsigned long trace_ip)
592 if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
593 trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
594 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
600 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
601 struct btree_path *path,
602 unsigned new_locks_want)
604 EBUG_ON(path->locks_want >= new_locks_want);
606 path->locks_want = new_locks_want;
608 return btree_path_get_locks(trans, path, true);
611 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
612 struct btree_path *path,
613 unsigned new_locks_want)
615 struct btree_path *linked;
617 if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want))
621 * XXX: this is ugly - we'd prefer to not be mucking with other
622 * iterators in the btree_trans here.
624 * On failure to upgrade the iterator, setting iter->locks_want and
625 * calling get_locks() is sufficient to make bch2_btree_path_traverse()
626 * get the locks we want on transaction restart.
628 * But if this iterator was a clone, on transaction restart what we did
629 * to this iterator isn't going to be preserved.
631 * Possibly we could add an iterator field for the parent iterator when
632 * an iterator is a copy - for now, we'll just upgrade any other
633 * iterators with the same btree id.
635 * The code below used to be needed to ensure ancestor nodes get locked
636 * before interior nodes - now that's handled by
637 * bch2_btree_path_traverse_all().
639 if (!path->cached && !trans->in_traverse_all)
640 trans_for_each_path(trans, linked)
641 if (linked != path &&
642 linked->cached == path->cached &&
643 linked->btree_id == path->btree_id &&
644 linked->locks_want < new_locks_want) {
645 linked->locks_want = new_locks_want;
646 btree_path_get_locks(trans, linked, true);
652 void __bch2_btree_path_downgrade(struct btree_trans *trans,
653 struct btree_path *path,
654 unsigned new_locks_want)
658 EBUG_ON(path->locks_want < new_locks_want);
660 path->locks_want = new_locks_want;
662 while (path->nodes_locked &&
663 (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
664 if (l > path->level) {
665 btree_node_unlock(trans, path, l);
667 if (btree_node_intent_locked(path, l)) {
668 six_lock_downgrade(&path->l[l].b->c.lock);
669 mark_btree_node_locked_noreset(path, l, BTREE_NODE_READ_LOCKED);
675 bch2_btree_path_verify_locks(path);
678 /* Btree transaction locking: */
680 void bch2_trans_downgrade(struct btree_trans *trans)
682 struct btree_path *path;
684 trans_for_each_path(trans, path)
685 bch2_btree_path_downgrade(trans, path);
688 int bch2_trans_relock(struct btree_trans *trans)
690 struct btree_path *path;
692 if (unlikely(trans->restarted))
693 return -((int) trans->restarted);
695 trans_for_each_path(trans, path)
696 if (path->should_be_locked &&
697 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
698 trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
699 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
704 int bch2_trans_relock_notrace(struct btree_trans *trans)
706 struct btree_path *path;
708 if (unlikely(trans->restarted))
709 return -((int) trans->restarted);
711 trans_for_each_path(trans, path)
712 if (path->should_be_locked &&
713 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
714 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
719 void bch2_trans_unlock_noassert(struct btree_trans *trans)
721 struct btree_path *path;
723 trans_for_each_path(trans, path)
724 __bch2_btree_path_unlock(trans, path);
727 void bch2_trans_unlock(struct btree_trans *trans)
729 struct btree_path *path;
731 trans_for_each_path(trans, path)
732 __bch2_btree_path_unlock(trans, path);
735 * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
736 * btree nodes, it implements its own walking:
738 if (!trans->is_initial_gc)
739 bch2_assert_btree_nodes_not_locked();
742 bool bch2_trans_locked(struct btree_trans *trans)
744 struct btree_path *path;
746 trans_for_each_path(trans, path)
747 if (path->nodes_locked)
752 int __bch2_trans_mutex_lock(struct btree_trans *trans,
755 int ret = drop_locks_do(trans, (mutex_lock(lock), 0));
764 #ifdef CONFIG_BCACHEFS_DEBUG
766 void bch2_btree_path_verify_locks(struct btree_path *path)
770 if (!path->nodes_locked) {
771 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
772 btree_path_node(path, path->level));
776 for (l = 0; l < BTREE_MAX_DEPTH; l++) {
777 int want = btree_lock_want(path, l);
778 int have = btree_node_locked_type(path, l);
780 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
782 BUG_ON(is_btree_node(path, l) &&
783 (want == BTREE_NODE_UNLOCKED ||
784 have != BTREE_NODE_WRITE_LOCKED) &&
789 void bch2_trans_verify_locks(struct btree_trans *trans)
791 struct btree_path *path;
793 trans_for_each_path(trans, path)
794 bch2_btree_path_verify_locks(path);