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):", g->nr);
89 for (i = g->g; i < g->g + g->nr; i++) {
90 struct task_struct *task = READ_ONCE(i->trans->locking_wait.task);
94 bch2_btree_trans_to_text(out, i->trans);
95 bch2_prt_task_backtrace(out, task, i == g->g ? 5 : 1);
99 static noinline void print_chain(struct printbuf *out, struct lock_graph *g)
101 struct trans_waiting_for_lock *i;
103 for (i = g->g; i != g->g + g->nr; i++) {
104 struct task_struct *task = i->trans->locking_wait.task;
107 prt_printf(out, "%u ", task ?task->pid : 0);
112 static void lock_graph_up(struct lock_graph *g)
114 closure_put(&g->g[--g->nr].trans->ref);
117 static noinline void lock_graph_pop_all(struct lock_graph *g)
123 static void __lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
125 g->g[g->nr++] = (struct trans_waiting_for_lock) {
127 .node_want = trans->locking,
128 .lock_want = trans->locking_wait.lock_want,
132 static void lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
134 closure_get(&trans->ref);
135 __lock_graph_down(g, trans);
138 static bool lock_graph_remove_non_waiters(struct lock_graph *g)
140 struct trans_waiting_for_lock *i;
142 for (i = g->g + 1; i < g->g + g->nr; i++)
143 if (i->trans->locking != i->node_want ||
144 i->trans->locking_wait.start_time != i[-1].lock_start_time) {
145 while (g->g + g->nr > i)
153 static void trace_would_deadlock(struct lock_graph *g, struct btree_trans *trans)
155 struct bch_fs *c = trans->c;
157 count_event(c, trans_restart_would_deadlock);
159 if (trace_trans_restart_would_deadlock_enabled()) {
160 struct printbuf buf = PRINTBUF;
163 print_cycle(&buf, g);
165 trace_trans_restart_would_deadlock(trans, buf.buf);
170 static int abort_lock(struct lock_graph *g, struct trans_waiting_for_lock *i)
173 trace_would_deadlock(g, i->trans);
174 return btree_trans_restart(i->trans, BCH_ERR_transaction_restart_would_deadlock);
176 i->trans->lock_must_abort = true;
177 wake_up_process(i->trans->locking_wait.task);
182 static int btree_trans_abort_preference(struct btree_trans *trans)
184 if (trans->lock_may_not_fail)
186 if (trans->locking_wait.lock_want == SIX_LOCK_write)
188 if (!trans->in_traverse_all)
193 static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
195 struct trans_waiting_for_lock *i, *abort = NULL;
196 unsigned best = 0, pref;
199 if (lock_graph_remove_non_waiters(g))
202 /* Only checking, for debugfs: */
204 print_cycle(cycle, g);
209 for (i = g->g; i < g->g + g->nr; i++) {
210 pref = btree_trans_abort_preference(i->trans);
217 if (unlikely(!best)) {
218 struct printbuf buf = PRINTBUF;
220 prt_printf(&buf, bch2_fmt(g->g->trans->c, "cycle of nofail locks"));
222 for (i = g->g; i < g->g + g->nr; i++) {
223 struct btree_trans *trans = i->trans;
225 bch2_btree_trans_to_text(&buf, trans);
227 prt_printf(&buf, "backtrace:");
229 printbuf_indent_add(&buf, 2);
230 bch2_prt_task_backtrace(&buf, trans->locking_wait.task, 2);
231 printbuf_indent_sub(&buf, 2);
235 bch2_print_string_as_lines(KERN_ERR, buf.buf);
240 ret = abort_lock(g, abort);
248 static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
249 struct printbuf *cycle)
251 struct btree_trans *orig_trans = g->g->trans;
252 struct trans_waiting_for_lock *i;
254 for (i = g->g; i < g->g + g->nr; i++)
255 if (i->trans == trans) {
256 closure_put(&trans->ref);
257 return break_cycle(g, cycle);
260 if (g->nr == ARRAY_SIZE(g->g)) {
261 closure_put(&trans->ref);
263 if (orig_trans->lock_may_not_fail)
272 trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
273 return btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
276 __lock_graph_down(g, trans);
280 static bool lock_type_conflicts(enum six_lock_type t1, enum six_lock_type t2)
285 int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
288 struct trans_waiting_for_lock *top;
289 struct btree_bkey_cached_common *b;
290 btree_path_idx_t path_idx;
295 if (trans->lock_must_abort) {
299 trace_would_deadlock(&g, trans);
300 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
303 lock_graph_down(&g, trans);
305 /* trans->paths is rcu protected vs. freeing */
313 top = &g.g[g.nr - 1];
315 struct btree_path *paths = rcu_dereference(top->trans->paths);
319 unsigned long *paths_allocated = trans_paths_allocated(paths);
321 trans_for_each_path_idx_from(paths_allocated, *trans_paths_nr(paths),
322 path_idx, top->path_idx) {
323 struct btree_path *path = paths + path_idx;
324 if (!path->nodes_locked)
327 if (path_idx != top->path_idx) {
328 top->path_idx = path_idx;
330 top->lock_start_time = 0;
334 top->level < BTREE_MAX_DEPTH;
335 top->level++, top->lock_start_time = 0) {
336 int lock_held = btree_node_locked_type(path, top->level);
338 if (lock_held == BTREE_NODE_UNLOCKED)
341 b = &READ_ONCE(path->l[top->level].b)->c;
343 if (IS_ERR_OR_NULL(b)) {
345 * If we get here, it means we raced with the
346 * other thread updating its btree_path
347 * structures - which means it can't be blocked
350 if (!lock_graph_remove_non_waiters(&g)) {
352 * If lock_graph_remove_non_waiters()
353 * didn't do anything, it must be
354 * because we're being called by debugfs
355 * checking for lock cycles, which
356 * invokes us on btree_transactions that
357 * aren't actually waiting on anything.
360 lock_graph_pop_all(&g);
366 if (list_empty_careful(&b->lock.wait_list))
369 raw_spin_lock(&b->lock.wait_lock);
370 list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
371 BUG_ON(b != trans->locking);
373 if (top->lock_start_time &&
374 time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
377 top->lock_start_time = trans->locking_wait.start_time;
379 /* Don't check for self deadlock: */
380 if (trans == top->trans ||
381 !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
384 closure_get(&trans->ref);
385 raw_spin_unlock(&b->lock.wait_lock);
387 ret = lock_graph_descend(&g, trans, cycle);
393 raw_spin_unlock(&b->lock.wait_lock);
397 if (g.nr > 1 && cycle)
398 print_chain(cycle, &g);
408 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
410 struct btree_trans *trans = p;
412 return bch2_check_for_deadlock(trans, NULL);
415 int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
416 struct btree_bkey_cached_common *b,
417 bool lock_may_not_fail)
419 int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
423 * Must drop our read locks before calling six_lock_write() -
424 * six_unlock() won't do wakeups until the reader count
425 * goes to 0, and it's safe because we have the node intent
428 six_lock_readers_add(&b->lock, -readers);
429 ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write,
430 lock_may_not_fail, _RET_IP_);
431 six_lock_readers_add(&b->lock, readers);
434 mark_btree_node_locked_noreset(path, b->level, BTREE_NODE_INTENT_LOCKED);
439 void bch2_btree_node_lock_write_nofail(struct btree_trans *trans,
440 struct btree_path *path,
441 struct btree_bkey_cached_common *b)
443 struct btree_path *linked;
450 * Drop all read locks before taking a write lock:
452 * This is a hack, because bch2_btree_node_lock_write_nofail() is a
453 * hack - but by dropping read locks first, this should never fail, and
454 * we only use this in code paths where whatever read locks we've
455 * already taken are no longer needed:
458 trans_for_each_path(trans, linked, iter) {
459 if (!linked->nodes_locked)
462 for (i = 0; i < BTREE_MAX_DEPTH; i++)
463 if (btree_node_read_locked(linked, i)) {
464 btree_node_unlock(trans, linked, i);
465 btree_path_set_dirty(linked, BTREE_ITER_NEED_RELOCK);
469 ret = __btree_node_lock_write(trans, path, b, true);
475 static inline bool btree_path_get_locks(struct btree_trans *trans,
476 struct btree_path *path,
478 struct get_locks_fail *f)
480 unsigned l = path->level;
484 if (!btree_path_node(path, l))
488 ? bch2_btree_node_upgrade(trans, path, l)
489 : bch2_btree_node_relock(trans, path, l))) {
499 } while (l < path->locks_want);
502 * When we fail to get a lock, we have to ensure that any child nodes
503 * can't be relocked so bch2_btree_path_traverse has to walk back up to
504 * the node that we failed to relock:
507 __bch2_btree_path_unlock(trans, path);
508 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
511 path->l[fail_idx].b = upgrade
512 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
513 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
515 } while (fail_idx >= 0);
518 if (path->uptodate == BTREE_ITER_NEED_RELOCK)
519 path->uptodate = BTREE_ITER_UPTODATE;
521 bch2_trans_verify_locks(trans);
523 return path->uptodate < BTREE_ITER_NEED_RELOCK;
526 bool __bch2_btree_node_relock(struct btree_trans *trans,
527 struct btree_path *path, unsigned level,
530 struct btree *b = btree_path_node(path, level);
531 int want = __btree_lock_want(path, level);
536 if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
537 (btree_node_lock_seq_matches(path, b, level) &&
538 btree_node_lock_increment(trans, &b->c, level, want))) {
539 mark_btree_node_locked(trans, path, level, want);
543 if (trace && !trans->notrace_relock_fail)
544 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
550 bool bch2_btree_node_upgrade(struct btree_trans *trans,
551 struct btree_path *path, unsigned level)
553 struct btree *b = path->l[level].b;
554 struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
556 if (!is_btree_node(path, level))
559 switch (btree_lock_want(path, level)) {
560 case BTREE_NODE_UNLOCKED:
561 BUG_ON(btree_node_locked(path, level));
563 case BTREE_NODE_READ_LOCKED:
564 BUG_ON(btree_node_intent_locked(path, level));
565 return bch2_btree_node_relock(trans, path, level);
566 case BTREE_NODE_INTENT_LOCKED:
568 case BTREE_NODE_WRITE_LOCKED:
572 if (btree_node_intent_locked(path, level))
578 if (btree_node_locked(path, level)) {
581 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
582 ret = six_lock_tryupgrade(&b->c.lock);
583 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
588 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
593 * Do we already have an intent lock via another path? If so, just bump
596 if (btree_node_lock_seq_matches(path, b, level) &&
597 btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
598 btree_node_unlock(trans, path, level);
602 trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
605 mark_btree_node_locked_noreset(path, level, BTREE_NODE_INTENT_LOCKED);
609 /* Btree path locking: */
612 * Only for btree_cache.c - only relocks intent locks
614 int bch2_btree_path_relock_intent(struct btree_trans *trans,
615 struct btree_path *path)
619 for (l = path->level;
620 l < path->locks_want && btree_path_node(path, l);
622 if (!bch2_btree_node_relock(trans, path, l)) {
623 __bch2_btree_path_unlock(trans, path);
624 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
625 trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
626 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
634 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
635 struct btree_path *path, unsigned long trace_ip)
637 struct get_locks_fail f;
639 return btree_path_get_locks(trans, path, false, &f);
642 int __bch2_btree_path_relock(struct btree_trans *trans,
643 struct btree_path *path, unsigned long trace_ip)
645 if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
646 trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
647 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
653 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
654 struct btree_path *path,
655 unsigned new_locks_want,
656 struct get_locks_fail *f)
658 EBUG_ON(path->locks_want >= new_locks_want);
660 path->locks_want = new_locks_want;
662 return btree_path_get_locks(trans, path, true, f);
665 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
666 struct btree_path *path,
667 unsigned new_locks_want,
668 struct get_locks_fail *f)
670 if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want, f))
674 * XXX: this is ugly - we'd prefer to not be mucking with other
675 * iterators in the btree_trans here.
677 * On failure to upgrade the iterator, setting iter->locks_want and
678 * calling get_locks() is sufficient to make bch2_btree_path_traverse()
679 * get the locks we want on transaction restart.
681 * But if this iterator was a clone, on transaction restart what we did
682 * to this iterator isn't going to be preserved.
684 * Possibly we could add an iterator field for the parent iterator when
685 * an iterator is a copy - for now, we'll just upgrade any other
686 * iterators with the same btree id.
688 * The code below used to be needed to ensure ancestor nodes get locked
689 * before interior nodes - now that's handled by
690 * bch2_btree_path_traverse_all().
692 if (!path->cached && !trans->in_traverse_all) {
693 struct btree_path *linked;
696 trans_for_each_path(trans, linked, i)
697 if (linked != path &&
698 linked->cached == path->cached &&
699 linked->btree_id == path->btree_id &&
700 linked->locks_want < new_locks_want) {
701 linked->locks_want = new_locks_want;
702 btree_path_get_locks(trans, linked, true, NULL);
709 void __bch2_btree_path_downgrade(struct btree_trans *trans,
710 struct btree_path *path,
711 unsigned new_locks_want)
713 unsigned l, old_locks_want = path->locks_want;
715 if (trans->restarted)
718 EBUG_ON(path->locks_want < new_locks_want);
720 path->locks_want = new_locks_want;
722 while (path->nodes_locked &&
723 (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
724 if (l > path->level) {
725 btree_node_unlock(trans, path, l);
727 if (btree_node_intent_locked(path, l)) {
728 six_lock_downgrade(&path->l[l].b->c.lock);
729 mark_btree_node_locked_noreset(path, l, BTREE_NODE_READ_LOCKED);
735 bch2_btree_path_verify_locks(path);
737 trace_path_downgrade(trans, _RET_IP_, path, old_locks_want);
740 /* Btree transaction locking: */
742 void bch2_trans_downgrade(struct btree_trans *trans)
744 struct btree_path *path;
747 if (trans->restarted)
750 trans_for_each_path(trans, path, i)
751 bch2_btree_path_downgrade(trans, path);
754 int bch2_trans_relock(struct btree_trans *trans)
756 struct btree_path *path;
759 if (unlikely(trans->restarted))
760 return -((int) trans->restarted);
762 trans_for_each_path(trans, path, i)
763 if (path->should_be_locked &&
764 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
765 trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
766 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
771 int bch2_trans_relock_notrace(struct btree_trans *trans)
773 struct btree_path *path;
776 if (unlikely(trans->restarted))
777 return -((int) trans->restarted);
779 trans_for_each_path(trans, path, i)
780 if (path->should_be_locked &&
781 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
782 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
787 void bch2_trans_unlock_noassert(struct btree_trans *trans)
789 struct btree_path *path;
792 trans_for_each_path(trans, path, i)
793 __bch2_btree_path_unlock(trans, path);
796 void bch2_trans_unlock(struct btree_trans *trans)
798 struct btree_path *path;
801 trans_for_each_path(trans, path, i)
802 __bch2_btree_path_unlock(trans, path);
805 void bch2_trans_unlock_long(struct btree_trans *trans)
807 bch2_trans_unlock(trans);
808 bch2_trans_srcu_unlock(trans);
811 bool bch2_trans_locked(struct btree_trans *trans)
813 struct btree_path *path;
816 trans_for_each_path(trans, path, i)
817 if (path->nodes_locked)
822 int __bch2_trans_mutex_lock(struct btree_trans *trans,
825 int ret = drop_locks_do(trans, (mutex_lock(lock), 0));
834 #ifdef CONFIG_BCACHEFS_DEBUG
836 void bch2_btree_path_verify_locks(struct btree_path *path)
840 if (!path->nodes_locked) {
841 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
842 btree_path_node(path, path->level));
846 for (l = 0; l < BTREE_MAX_DEPTH; l++) {
847 int want = btree_lock_want(path, l);
848 int have = btree_node_locked_type(path, l);
850 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
852 BUG_ON(is_btree_node(path, l) &&
853 (want == BTREE_NODE_UNLOCKED ||
854 have != BTREE_NODE_WRITE_LOCKED) &&
859 void bch2_trans_verify_locks(struct btree_trans *trans)
861 struct btree_path *path;
864 trans_for_each_path(trans, path, i)
865 bch2_btree_path_verify_locks(path);