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 closure_get(&trans->ref);
118 g->g[g->nr++] = (struct trans_waiting_for_lock) {
120 .node_want = trans->locking,
121 .lock_want = trans->locking_wait.lock_want,
125 static bool lock_graph_remove_non_waiters(struct lock_graph *g)
127 struct trans_waiting_for_lock *i;
129 for (i = g->g + 1; i < g->g + g->nr; i++)
130 if (i->trans->locking != i->node_want ||
131 i->trans->locking_wait.start_time != i[-1].lock_start_time) {
132 while (g->g + g->nr > i)
140 static int abort_lock(struct lock_graph *g, struct trans_waiting_for_lock *i)
143 trace_and_count(i->trans->c, trans_restart_would_deadlock, i->trans, _RET_IP_);
144 return btree_trans_restart(i->trans, BCH_ERR_transaction_restart_would_deadlock);
146 i->trans->lock_must_abort = true;
147 wake_up_process(i->trans->locking_wait.task);
152 static int btree_trans_abort_preference(struct btree_trans *trans)
154 if (trans->lock_may_not_fail)
156 if (trans->locking_wait.lock_want == SIX_LOCK_write)
158 if (!trans->in_traverse_all)
163 static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
165 struct trans_waiting_for_lock *i, *abort = NULL;
166 unsigned best = 0, pref;
169 if (lock_graph_remove_non_waiters(g))
172 /* Only checking, for debugfs: */
174 print_cycle(cycle, g);
179 for (i = g->g; i < g->g + g->nr; i++) {
180 pref = btree_trans_abort_preference(i->trans);
187 if (unlikely(!best)) {
188 struct printbuf buf = PRINTBUF;
190 prt_printf(&buf, bch2_fmt(g->g->trans->c, "cycle of nofail locks"));
192 for (i = g->g; i < g->g + g->nr; i++) {
193 struct btree_trans *trans = i->trans;
195 bch2_btree_trans_to_text(&buf, trans);
197 prt_printf(&buf, "backtrace:");
199 printbuf_indent_add(&buf, 2);
200 bch2_prt_task_backtrace(&buf, trans->locking_wait.task);
201 printbuf_indent_sub(&buf, 2);
205 bch2_print_string_as_lines(KERN_ERR, buf.buf);
210 ret = abort_lock(g, abort);
218 static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
219 struct printbuf *cycle)
221 struct btree_trans *orig_trans = g->g->trans;
222 struct trans_waiting_for_lock *i;
224 for (i = g->g; i < g->g + g->nr; i++)
225 if (i->trans == trans)
226 return break_cycle(g, cycle);
228 if (g->nr == ARRAY_SIZE(g->g)) {
229 if (orig_trans->lock_may_not_fail)
238 trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
239 return btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
242 lock_graph_down(g, trans);
246 static bool lock_type_conflicts(enum six_lock_type t1, enum six_lock_type t2)
251 int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
254 struct trans_waiting_for_lock *top;
255 struct btree_bkey_cached_common *b;
256 struct btree_path *path;
260 if (trans->lock_must_abort) {
264 trace_and_count(trans->c, trans_restart_would_deadlock, trans, _RET_IP_);
265 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
269 lock_graph_down(&g, trans);
274 top = &g.g[g.nr - 1];
276 trans_for_each_path_safe_from(top->trans, path, path_idx, top->path_idx) {
277 if (!path->nodes_locked)
280 if (path_idx != top->path_idx) {
281 top->path_idx = path_idx;
283 top->lock_start_time = 0;
287 top->level < BTREE_MAX_DEPTH;
288 top->level++, top->lock_start_time = 0) {
289 int lock_held = btree_node_locked_type(path, top->level);
291 if (lock_held == BTREE_NODE_UNLOCKED)
294 b = &READ_ONCE(path->l[top->level].b)->c;
296 if (IS_ERR_OR_NULL(b)) {
298 * If we get here, it means we raced with the
299 * other thread updating its btree_path
300 * structures - which means it can't be blocked
303 if (!lock_graph_remove_non_waiters(&g)) {
305 * If lock_graph_remove_non_waiters()
306 * didn't do anything, it must be
307 * because we're being called by debugfs
308 * checking for lock cycles, which
309 * invokes us on btree_transactions that
310 * aren't actually waiting on anything.
313 lock_graph_pop_all(&g);
319 if (list_empty_careful(&b->lock.wait_list))
322 raw_spin_lock(&b->lock.wait_lock);
323 list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
324 BUG_ON(b != trans->locking);
326 if (top->lock_start_time &&
327 time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
330 top->lock_start_time = trans->locking_wait.start_time;
332 /* Don't check for self deadlock: */
333 if (trans == top->trans ||
334 !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
337 ret = lock_graph_descend(&g, trans, cycle);
338 raw_spin_unlock(&b->lock.wait_lock);
345 raw_spin_unlock(&b->lock.wait_lock);
349 if (g.nr > 1 && cycle)
350 print_chain(cycle, &g);
355 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
357 struct btree_trans *trans = p;
359 return bch2_check_for_deadlock(trans, NULL);
362 int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
363 struct btree_bkey_cached_common *b,
364 bool lock_may_not_fail)
366 int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
370 * Must drop our read locks before calling six_lock_write() -
371 * six_unlock() won't do wakeups until the reader count
372 * goes to 0, and it's safe because we have the node intent
375 six_lock_readers_add(&b->lock, -readers);
376 ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write,
377 lock_may_not_fail, _RET_IP_);
378 six_lock_readers_add(&b->lock, readers);
381 mark_btree_node_locked_noreset(path, b->level, SIX_LOCK_intent);
386 void bch2_btree_node_lock_write_nofail(struct btree_trans *trans,
387 struct btree_path *path,
388 struct btree_bkey_cached_common *b)
390 struct btree_path *linked;
397 * Drop all read locks before taking a write lock:
399 * This is a hack, because bch2_btree_node_lock_write_nofail() is a
400 * hack - but by dropping read locks first, this should never fail, and
401 * we only use this in code paths where whatever read locks we've
402 * already taken are no longer needed:
405 trans_for_each_path(trans, linked) {
406 if (!linked->nodes_locked)
409 for (i = 0; i < BTREE_MAX_DEPTH; i++)
410 if (btree_node_read_locked(linked, i)) {
411 btree_node_unlock(trans, linked, i);
412 btree_path_set_dirty(linked, BTREE_ITER_NEED_RELOCK);
416 ret = __btree_node_lock_write(trans, path, b, true);
422 static inline bool btree_path_get_locks(struct btree_trans *trans,
423 struct btree_path *path,
426 unsigned l = path->level;
430 if (!btree_path_node(path, l))
434 ? bch2_btree_node_upgrade(trans, path, l)
435 : bch2_btree_node_relock(trans, path, l)))
439 } while (l < path->locks_want);
442 * When we fail to get a lock, we have to ensure that any child nodes
443 * can't be relocked so bch2_btree_path_traverse has to walk back up to
444 * the node that we failed to relock:
447 __bch2_btree_path_unlock(trans, path);
448 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
451 path->l[fail_idx].b = upgrade
452 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
453 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
455 } while (fail_idx >= 0);
458 if (path->uptodate == BTREE_ITER_NEED_RELOCK)
459 path->uptodate = BTREE_ITER_UPTODATE;
461 bch2_trans_verify_locks(trans);
463 return path->uptodate < BTREE_ITER_NEED_RELOCK;
466 bool __bch2_btree_node_relock(struct btree_trans *trans,
467 struct btree_path *path, unsigned level,
470 struct btree *b = btree_path_node(path, level);
471 int want = __btree_lock_want(path, level);
476 if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
477 (btree_node_lock_seq_matches(path, b, level) &&
478 btree_node_lock_increment(trans, &b->c, level, want))) {
479 mark_btree_node_locked(trans, path, level, want);
483 if (trace && !trans->notrace_relock_fail)
484 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
490 bool bch2_btree_node_upgrade(struct btree_trans *trans,
491 struct btree_path *path, unsigned level)
493 struct btree *b = path->l[level].b;
494 struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
496 if (!is_btree_node(path, level))
499 switch (btree_lock_want(path, level)) {
500 case BTREE_NODE_UNLOCKED:
501 BUG_ON(btree_node_locked(path, level));
503 case BTREE_NODE_READ_LOCKED:
504 BUG_ON(btree_node_intent_locked(path, level));
505 return bch2_btree_node_relock(trans, path, level);
506 case BTREE_NODE_INTENT_LOCKED:
508 case BTREE_NODE_WRITE_LOCKED:
512 if (btree_node_intent_locked(path, level))
518 if (btree_node_locked(path, level)) {
521 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
522 ret = six_lock_tryupgrade(&b->c.lock);
523 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
528 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
533 * Do we already have an intent lock via another path? If so, just bump
536 if (btree_node_lock_seq_matches(path, b, level) &&
537 btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
538 btree_node_unlock(trans, path, level);
542 trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
545 mark_btree_node_locked_noreset(path, level, SIX_LOCK_intent);
549 /* Btree path locking: */
552 * Only for btree_cache.c - only relocks intent locks
554 int bch2_btree_path_relock_intent(struct btree_trans *trans,
555 struct btree_path *path)
559 for (l = path->level;
560 l < path->locks_want && btree_path_node(path, l);
562 if (!bch2_btree_node_relock(trans, path, l)) {
563 __bch2_btree_path_unlock(trans, path);
564 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
565 trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
566 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
574 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
575 struct btree_path *path, unsigned long trace_ip)
577 return btree_path_get_locks(trans, path, false);
580 int __bch2_btree_path_relock(struct btree_trans *trans,
581 struct btree_path *path, unsigned long trace_ip)
583 if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
584 trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
585 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
592 bool bch2_btree_path_upgrade_norestart(struct btree_trans *trans,
593 struct btree_path *path, unsigned long trace_ip)
595 return btree_path_get_locks(trans, path, true);
598 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
599 struct btree_path *path,
600 unsigned new_locks_want)
602 EBUG_ON(path->locks_want >= new_locks_want);
604 path->locks_want = new_locks_want;
606 return btree_path_get_locks(trans, path, true);
609 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
610 struct btree_path *path,
611 unsigned new_locks_want)
613 struct btree_path *linked;
615 if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want))
619 * XXX: this is ugly - we'd prefer to not be mucking with other
620 * iterators in the btree_trans here.
622 * On failure to upgrade the iterator, setting iter->locks_want and
623 * calling get_locks() is sufficient to make bch2_btree_path_traverse()
624 * get the locks we want on transaction restart.
626 * But if this iterator was a clone, on transaction restart what we did
627 * to this iterator isn't going to be preserved.
629 * Possibly we could add an iterator field for the parent iterator when
630 * an iterator is a copy - for now, we'll just upgrade any other
631 * iterators with the same btree id.
633 * The code below used to be needed to ensure ancestor nodes get locked
634 * before interior nodes - now that's handled by
635 * bch2_btree_path_traverse_all().
637 if (!path->cached && !trans->in_traverse_all)
638 trans_for_each_path(trans, linked)
639 if (linked != path &&
640 linked->cached == path->cached &&
641 linked->btree_id == path->btree_id &&
642 linked->locks_want < new_locks_want) {
643 linked->locks_want = new_locks_want;
644 btree_path_get_locks(trans, linked, true);
650 void __bch2_btree_path_downgrade(struct btree_trans *trans,
651 struct btree_path *path,
652 unsigned new_locks_want)
656 EBUG_ON(path->locks_want < new_locks_want);
658 path->locks_want = new_locks_want;
660 while (path->nodes_locked &&
661 (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
662 if (l > path->level) {
663 btree_node_unlock(trans, path, l);
665 if (btree_node_intent_locked(path, l)) {
666 six_lock_downgrade(&path->l[l].b->c.lock);
667 mark_btree_node_locked_noreset(path, l, SIX_LOCK_read);
673 bch2_btree_path_verify_locks(path);
676 /* Btree transaction locking: */
678 void bch2_trans_downgrade(struct btree_trans *trans)
680 struct btree_path *path;
682 trans_for_each_path(trans, path)
683 bch2_btree_path_downgrade(trans, path);
686 int bch2_trans_relock(struct btree_trans *trans)
688 struct btree_path *path;
690 if (unlikely(trans->restarted))
691 return -((int) trans->restarted);
693 trans_for_each_path(trans, path)
694 if (path->should_be_locked &&
695 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
696 trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
697 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
702 int bch2_trans_relock_notrace(struct btree_trans *trans)
704 struct btree_path *path;
706 if (unlikely(trans->restarted))
707 return -((int) trans->restarted);
709 trans_for_each_path(trans, path)
710 if (path->should_be_locked &&
711 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
712 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
717 void bch2_trans_unlock(struct btree_trans *trans)
719 struct btree_path *path;
721 trans_for_each_path(trans, path)
722 __bch2_btree_path_unlock(trans, path);
725 * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
726 * btree nodes, it implements its own walking:
728 if (!trans->is_initial_gc)
729 bch2_assert_btree_nodes_not_locked();
732 bool bch2_trans_locked(struct btree_trans *trans)
734 struct btree_path *path;
736 trans_for_each_path(trans, path)
737 if (path->nodes_locked)
742 int __bch2_trans_mutex_lock(struct btree_trans *trans,
745 int ret = drop_locks_do(trans, (mutex_lock(lock), 0));
754 #ifdef CONFIG_BCACHEFS_DEBUG
756 void bch2_btree_path_verify_locks(struct btree_path *path)
760 if (!path->nodes_locked) {
761 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
762 btree_path_node(path, path->level));
766 for (l = 0; l < BTREE_MAX_DEPTH; l++) {
767 int want = btree_lock_want(path, l);
768 int have = btree_node_locked_type(path, l);
770 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
772 BUG_ON(is_btree_node(path, l) &&
773 (want == BTREE_NODE_UNLOCKED ||
774 have != BTREE_NODE_WRITE_LOCKED) &&
779 void bch2_trans_verify_locks(struct btree_trans *trans)
781 struct btree_path *path;
783 trans_for_each_path(trans, path)
784 bch2_btree_path_verify_locks(path);