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;
259 if (trans->lock_must_abort) {
263 trace_and_count(trans->c, trans_restart_would_deadlock, trans, _RET_IP_);
264 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
268 lock_graph_down(&g, trans);
273 top = &g.g[g.nr - 1];
275 trans_for_each_path_from(top->trans, path, top->path_idx) {
276 if (!path->nodes_locked)
279 if (top->path_idx != path->idx) {
280 top->path_idx = path->idx;
282 top->lock_start_time = 0;
286 top->level < BTREE_MAX_DEPTH;
287 top->level++, top->lock_start_time = 0) {
288 int lock_held = btree_node_locked_type(path, top->level);
290 if (lock_held == BTREE_NODE_UNLOCKED)
293 b = &READ_ONCE(path->l[top->level].b)->c;
295 if (IS_ERR_OR_NULL(b)) {
297 * If we get here, it means we raced with the
298 * other thread updating its btree_path
299 * structures - which means it can't be blocked
302 if (!lock_graph_remove_non_waiters(&g)) {
304 * If lock_graph_remove_non_waiters()
305 * didn't do anything, it must be
306 * because we're being called by debugfs
307 * checking for lock cycles, which
308 * invokes us on btree_transactions that
309 * aren't actually waiting on anything.
312 lock_graph_pop_all(&g);
318 if (list_empty_careful(&b->lock.wait_list))
321 raw_spin_lock(&b->lock.wait_lock);
322 list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
323 BUG_ON(b != trans->locking);
325 if (top->lock_start_time &&
326 time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
329 top->lock_start_time = trans->locking_wait.start_time;
331 /* Don't check for self deadlock: */
332 if (trans == top->trans ||
333 !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
336 ret = lock_graph_descend(&g, trans, cycle);
337 raw_spin_unlock(&b->lock.wait_lock);
344 raw_spin_unlock(&b->lock.wait_lock);
348 if (g.nr > 1 && cycle)
349 print_chain(cycle, &g);
354 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
356 struct btree_trans *trans = p;
358 return bch2_check_for_deadlock(trans, NULL);
361 int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
362 struct btree_bkey_cached_common *b,
363 bool lock_may_not_fail)
365 int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
369 * Must drop our read locks before calling six_lock_write() -
370 * six_unlock() won't do wakeups until the reader count
371 * goes to 0, and it's safe because we have the node intent
374 six_lock_readers_add(&b->lock, -readers);
375 ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write,
376 lock_may_not_fail, _RET_IP_);
377 six_lock_readers_add(&b->lock, readers);
380 mark_btree_node_locked_noreset(path, b->level, SIX_LOCK_intent);
385 void bch2_btree_node_lock_write_nofail(struct btree_trans *trans,
386 struct btree_path *path,
387 struct btree_bkey_cached_common *b)
389 struct btree_path *linked;
396 * Drop all read locks before taking a write lock:
398 * This is a hack, because bch2_btree_node_lock_write_nofail() is a
399 * hack - but by dropping read locks first, this should never fail, and
400 * we only use this in code paths where whatever read locks we've
401 * already taken are no longer needed:
404 trans_for_each_path(trans, linked) {
405 if (!linked->nodes_locked)
408 for (i = 0; i < BTREE_MAX_DEPTH; i++)
409 if (btree_node_read_locked(linked, i)) {
410 btree_node_unlock(trans, linked, i);
411 btree_path_set_dirty(linked, BTREE_ITER_NEED_RELOCK);
415 ret = __btree_node_lock_write(trans, path, b, true);
421 static inline bool btree_path_get_locks(struct btree_trans *trans,
422 struct btree_path *path,
425 unsigned l = path->level;
429 if (!btree_path_node(path, l))
433 ? bch2_btree_node_upgrade(trans, path, l)
434 : bch2_btree_node_relock(trans, path, l)))
438 } while (l < path->locks_want);
441 * When we fail to get a lock, we have to ensure that any child nodes
442 * can't be relocked so bch2_btree_path_traverse has to walk back up to
443 * the node that we failed to relock:
446 __bch2_btree_path_unlock(trans, path);
447 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
450 path->l[fail_idx].b = upgrade
451 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
452 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
454 } while (fail_idx >= 0);
457 if (path->uptodate == BTREE_ITER_NEED_RELOCK)
458 path->uptodate = BTREE_ITER_UPTODATE;
460 bch2_trans_verify_locks(trans);
462 return path->uptodate < BTREE_ITER_NEED_RELOCK;
465 bool __bch2_btree_node_relock(struct btree_trans *trans,
466 struct btree_path *path, unsigned level,
469 struct btree *b = btree_path_node(path, level);
470 int want = __btree_lock_want(path, level);
475 if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
476 (btree_node_lock_seq_matches(path, b, level) &&
477 btree_node_lock_increment(trans, &b->c, level, want))) {
478 mark_btree_node_locked(trans, path, level, want);
482 if (trace && !trans->notrace_relock_fail)
483 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
489 bool bch2_btree_node_upgrade(struct btree_trans *trans,
490 struct btree_path *path, unsigned level)
492 struct btree *b = path->l[level].b;
493 struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
495 if (!is_btree_node(path, level))
498 switch (btree_lock_want(path, level)) {
499 case BTREE_NODE_UNLOCKED:
500 BUG_ON(btree_node_locked(path, level));
502 case BTREE_NODE_READ_LOCKED:
503 BUG_ON(btree_node_intent_locked(path, level));
504 return bch2_btree_node_relock(trans, path, level);
505 case BTREE_NODE_INTENT_LOCKED:
507 case BTREE_NODE_WRITE_LOCKED:
511 if (btree_node_intent_locked(path, level))
517 if (btree_node_locked(path, level)) {
520 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
521 ret = six_lock_tryupgrade(&b->c.lock);
522 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
527 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
532 * Do we already have an intent lock via another path? If so, just bump
535 if (btree_node_lock_seq_matches(path, b, level) &&
536 btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
537 btree_node_unlock(trans, path, level);
541 trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
544 mark_btree_node_locked_noreset(path, level, SIX_LOCK_intent);
548 /* Btree path locking: */
551 * Only for btree_cache.c - only relocks intent locks
553 int bch2_btree_path_relock_intent(struct btree_trans *trans,
554 struct btree_path *path)
558 for (l = path->level;
559 l < path->locks_want && btree_path_node(path, l);
561 if (!bch2_btree_node_relock(trans, path, l)) {
562 __bch2_btree_path_unlock(trans, path);
563 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
564 trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
565 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
573 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
574 struct btree_path *path, unsigned long trace_ip)
576 return btree_path_get_locks(trans, path, false);
579 int __bch2_btree_path_relock(struct btree_trans *trans,
580 struct btree_path *path, unsigned long trace_ip)
582 if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
583 trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
584 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
591 bool bch2_btree_path_upgrade_norestart(struct btree_trans *trans,
592 struct btree_path *path, unsigned long trace_ip)
594 return btree_path_get_locks(trans, path, true);
597 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
598 struct btree_path *path,
599 unsigned new_locks_want)
601 EBUG_ON(path->locks_want >= new_locks_want);
603 path->locks_want = new_locks_want;
605 return btree_path_get_locks(trans, path, true);
608 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
609 struct btree_path *path,
610 unsigned new_locks_want)
612 struct btree_path *linked;
614 if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want))
618 * XXX: this is ugly - we'd prefer to not be mucking with other
619 * iterators in the btree_trans here.
621 * On failure to upgrade the iterator, setting iter->locks_want and
622 * calling get_locks() is sufficient to make bch2_btree_path_traverse()
623 * get the locks we want on transaction restart.
625 * But if this iterator was a clone, on transaction restart what we did
626 * to this iterator isn't going to be preserved.
628 * Possibly we could add an iterator field for the parent iterator when
629 * an iterator is a copy - for now, we'll just upgrade any other
630 * iterators with the same btree id.
632 * The code below used to be needed to ensure ancestor nodes get locked
633 * before interior nodes - now that's handled by
634 * bch2_btree_path_traverse_all().
636 if (!path->cached && !trans->in_traverse_all)
637 trans_for_each_path(trans, linked)
638 if (linked != path &&
639 linked->cached == path->cached &&
640 linked->btree_id == path->btree_id &&
641 linked->locks_want < new_locks_want) {
642 linked->locks_want = new_locks_want;
643 btree_path_get_locks(trans, linked, true);
649 void __bch2_btree_path_downgrade(struct btree_trans *trans,
650 struct btree_path *path,
651 unsigned new_locks_want)
655 EBUG_ON(path->locks_want < new_locks_want);
657 path->locks_want = new_locks_want;
659 while (path->nodes_locked &&
660 (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
661 if (l > path->level) {
662 btree_node_unlock(trans, path, l);
664 if (btree_node_intent_locked(path, l)) {
665 six_lock_downgrade(&path->l[l].b->c.lock);
666 mark_btree_node_locked_noreset(path, l, SIX_LOCK_read);
672 bch2_btree_path_verify_locks(path);
675 /* Btree transaction locking: */
677 void bch2_trans_downgrade(struct btree_trans *trans)
679 struct btree_path *path;
681 trans_for_each_path(trans, path)
682 bch2_btree_path_downgrade(trans, path);
685 int bch2_trans_relock(struct btree_trans *trans)
687 struct btree_path *path;
689 if (unlikely(trans->restarted))
690 return -((int) trans->restarted);
692 trans_for_each_path(trans, path)
693 if (path->should_be_locked &&
694 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
695 trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
696 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
701 int bch2_trans_relock_notrace(struct btree_trans *trans)
703 struct btree_path *path;
705 if (unlikely(trans->restarted))
706 return -((int) trans->restarted);
708 trans_for_each_path(trans, path)
709 if (path->should_be_locked &&
710 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
711 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
716 void bch2_trans_unlock(struct btree_trans *trans)
718 struct btree_path *path;
720 trans_for_each_path(trans, path)
721 __bch2_btree_path_unlock(trans, path);
724 * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
725 * btree nodes, it implements its own walking:
727 if (!trans->is_initial_gc)
728 bch2_assert_btree_nodes_not_locked();
731 bool bch2_trans_locked(struct btree_trans *trans)
733 struct btree_path *path;
735 trans_for_each_path(trans, path)
736 if (path->nodes_locked)
741 int __bch2_trans_mutex_lock(struct btree_trans *trans,
744 int ret = drop_locks_do(trans, (mutex_lock(lock), 0));
753 #ifdef CONFIG_BCACHEFS_DEBUG
755 void bch2_btree_path_verify_locks(struct btree_path *path)
759 if (!path->nodes_locked) {
760 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
761 btree_path_node(path, path->level));
765 for (l = 0; l < BTREE_MAX_DEPTH; l++) {
766 int want = btree_lock_want(path, l);
767 int have = btree_node_locked_type(path, l);
769 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
771 BUG_ON(is_btree_node(path, l) &&
772 (want == BTREE_NODE_UNLOCKED ||
773 have != BTREE_NODE_WRITE_LOCKED) &&
778 void bch2_trans_verify_locks(struct btree_trans *trans)
780 struct btree_path *path;
782 trans_for_each_path(trans, path)
783 bch2_btree_path_verify_locks(path);