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)
11 __six_lock_init(&b->lock, "b->c.lock", &bch2_btree_node_lock_key);
15 void bch2_assert_btree_nodes_not_locked(void)
17 BUG_ON(lock_class_is_held(&bch2_btree_node_lock_key));
21 /* Btree node locking: */
23 static inline void six_lock_readers_add(struct six_lock *lock, int nr)
26 this_cpu_add(*lock->readers, nr);
28 atomic64_add(__SIX_VAL(read_lock, nr), &lock->state.counter);
30 atomic64_sub(__SIX_VAL(read_lock, -nr), &lock->state.counter);
33 struct six_lock_count bch2_btree_node_lock_counts(struct btree_trans *trans,
34 struct btree_path *skip,
35 struct btree_bkey_cached_common *b,
38 struct btree_path *path;
39 struct six_lock_count ret;
41 memset(&ret, 0, sizeof(ret));
43 if (IS_ERR_OR_NULL(b))
46 trans_for_each_path(trans, path)
47 if (path != skip && &path->l[level].b->c == b) {
48 int t = btree_node_locked_type(path, level);
50 if (t != BTREE_NODE_UNLOCKED)
59 void bch2_btree_node_unlock_write(struct btree_trans *trans,
60 struct btree_path *path, struct btree *b)
62 bch2_btree_node_unlock_write_inlined(trans, path, b);
68 * @trans wants to lock @b with type @type
70 struct trans_waiting_for_lock {
71 struct btree_trans *trans;
72 struct btree_bkey_cached_common *node_want;
73 enum six_lock_type lock_want;
75 /* for iterating over held locks :*/
82 struct trans_waiting_for_lock g[8];
86 static noinline void print_cycle(struct printbuf *out, struct lock_graph *g)
88 struct trans_waiting_for_lock *i;
90 prt_printf(out, "Found lock cycle (%u entries):", g->nr);
93 for (i = g->g; i < g->g + g->nr; i++)
94 bch2_btree_trans_to_text(out, i->trans);
97 static noinline void print_chain(struct printbuf *out, struct lock_graph *g)
99 struct trans_waiting_for_lock *i;
101 for (i = g->g; i != g->g + g->nr; i++) {
104 prt_printf(out, "%u ", i->trans->locking_wait.task->pid);
109 static void lock_graph_up(struct lock_graph *g)
111 closure_put(&g->g[--g->nr].trans->ref);
114 static noinline void lock_graph_pop_all(struct lock_graph *g)
120 static void lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
122 closure_get(&trans->ref);
124 g->g[g->nr++] = (struct trans_waiting_for_lock) {
126 .node_want = trans->locking,
127 .lock_want = trans->locking_wait.lock_want,
131 static bool lock_graph_remove_non_waiters(struct lock_graph *g)
133 struct trans_waiting_for_lock *i;
135 for (i = g->g + 1; i < g->g + g->nr; i++)
136 if (i->trans->locking != i->node_want ||
137 i->trans->locking_wait.start_time != i[-1].lock_start_time) {
138 while (g->g + g->nr > i)
146 static int abort_lock(struct lock_graph *g, struct trans_waiting_for_lock *i)
149 trace_and_count(i->trans->c, trans_restart_would_deadlock, i->trans, _RET_IP_);
150 return btree_trans_restart(i->trans, BCH_ERR_transaction_restart_would_deadlock);
152 i->trans->lock_must_abort = true;
153 wake_up_process(i->trans->locking_wait.task);
158 static int btree_trans_abort_preference(struct btree_trans *trans)
160 if (trans->lock_may_not_fail)
162 if (trans->locking_wait.lock_want == SIX_LOCK_write)
164 if (!trans->in_traverse_all)
169 static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
171 struct trans_waiting_for_lock *i, *abort = NULL;
172 unsigned best = 0, pref;
175 if (lock_graph_remove_non_waiters(g))
178 /* Only checking, for debugfs: */
180 print_cycle(cycle, g);
185 for (i = g->g; i < g->g + g->nr; i++) {
186 pref = btree_trans_abort_preference(i->trans);
193 if (unlikely(!best)) {
194 struct printbuf buf = PRINTBUF;
196 prt_printf(&buf, bch2_fmt(g->g->trans->c, "cycle of nofail locks"));
198 for (i = g->g; i < g->g + g->nr; i++) {
199 struct btree_trans *trans = i->trans;
201 bch2_btree_trans_to_text(&buf, trans);
203 prt_printf(&buf, "backtrace:");
205 printbuf_indent_add(&buf, 2);
206 bch2_prt_task_backtrace(&buf, trans->locking_wait.task);
207 printbuf_indent_sub(&buf, 2);
211 bch2_print_string_as_lines(KERN_ERR, buf.buf);
216 ret = abort_lock(g, abort);
224 static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
225 struct printbuf *cycle)
227 struct btree_trans *orig_trans = g->g->trans;
228 struct trans_waiting_for_lock *i;
230 for (i = g->g; i < g->g + g->nr; i++)
231 if (i->trans == trans)
232 return break_cycle(g, cycle);
234 if (g->nr == ARRAY_SIZE(g->g)) {
235 if (orig_trans->lock_may_not_fail)
244 trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
245 return btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
248 lock_graph_down(g, trans);
252 static bool lock_type_conflicts(enum six_lock_type t1, enum six_lock_type t2)
257 int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
260 struct trans_waiting_for_lock *top;
261 struct btree_bkey_cached_common *b;
262 struct btree_path *path;
265 if (trans->lock_must_abort) {
269 trace_and_count(trans->c, trans_restart_would_deadlock, trans, _RET_IP_);
270 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
274 lock_graph_down(&g, trans);
279 top = &g.g[g.nr - 1];
281 trans_for_each_path_from(top->trans, path, top->path_idx) {
282 if (!path->nodes_locked)
285 if (top->path_idx != path->idx) {
286 top->path_idx = path->idx;
288 top->lock_start_time = 0;
292 top->level < BTREE_MAX_DEPTH;
293 top->level++, top->lock_start_time = 0) {
294 int lock_held = btree_node_locked_type(path, top->level);
296 if (lock_held == BTREE_NODE_UNLOCKED)
299 b = &READ_ONCE(path->l[top->level].b)->c;
301 if (IS_ERR_OR_NULL(b)) {
303 * If we get here, it means we raced with the
304 * other thread updating its btree_path
305 * structures - which means it can't be blocked
308 if (!lock_graph_remove_non_waiters(&g)) {
310 * If lock_graph_remove_non_waiters()
311 * didn't do anything, it must be
312 * because we're being called by debugfs
313 * checking for lock cycles, which
314 * invokes us on btree_transactions that
315 * aren't actually waiting on anything.
318 lock_graph_pop_all(&g);
324 if (list_empty_careful(&b->lock.wait_list))
327 raw_spin_lock(&b->lock.wait_lock);
328 list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
329 BUG_ON(b != trans->locking);
331 if (top->lock_start_time &&
332 time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
335 top->lock_start_time = trans->locking_wait.start_time;
337 /* Don't check for self deadlock: */
338 if (trans == top->trans ||
339 !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
342 ret = lock_graph_descend(&g, trans, cycle);
343 raw_spin_unlock(&b->lock.wait_lock);
350 raw_spin_unlock(&b->lock.wait_lock);
354 if (g.nr > 1 && cycle)
355 print_chain(cycle, &g);
360 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
362 struct btree_trans *trans = p;
364 return bch2_check_for_deadlock(trans, NULL);
367 int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
368 struct btree_bkey_cached_common *b,
369 bool lock_may_not_fail)
371 int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
375 * Must drop our read locks before calling six_lock_write() -
376 * six_unlock() won't do wakeups until the reader count
377 * goes to 0, and it's safe because we have the node intent
380 six_lock_readers_add(&b->lock, -readers);
381 ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write,
382 lock_may_not_fail, _RET_IP_);
383 six_lock_readers_add(&b->lock, readers);
386 mark_btree_node_locked_noreset(path, b->level, SIX_LOCK_intent);
391 void bch2_btree_node_lock_write_nofail(struct btree_trans *trans,
392 struct btree_path *path,
393 struct btree_bkey_cached_common *b)
395 struct btree_path *linked;
402 * Drop all read locks before taking a write lock:
404 * This is a hack, because bch2_btree_node_lock_write_nofail() is a
405 * hack - but by dropping read locks first, this should never fail, and
406 * we only use this in code paths where whatever read locks we've
407 * already taken are no longer needed:
410 trans_for_each_path(trans, linked) {
411 if (!linked->nodes_locked)
414 for (i = 0; i < BTREE_MAX_DEPTH; i++)
415 if (btree_node_read_locked(linked, i)) {
416 btree_node_unlock(trans, linked, i);
417 btree_path_set_dirty(linked, BTREE_ITER_NEED_RELOCK);
421 ret = __btree_node_lock_write(trans, path, b, true);
427 static inline bool btree_path_get_locks(struct btree_trans *trans,
428 struct btree_path *path,
431 unsigned l = path->level;
435 if (!btree_path_node(path, l))
439 ? bch2_btree_node_upgrade(trans, path, l)
440 : bch2_btree_node_relock(trans, path, l)))
444 } while (l < path->locks_want);
447 * When we fail to get a lock, we have to ensure that any child nodes
448 * can't be relocked so bch2_btree_path_traverse has to walk back up to
449 * the node that we failed to relock:
452 __bch2_btree_path_unlock(trans, path);
453 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
456 path->l[fail_idx].b = upgrade
457 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
458 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
460 } while (fail_idx >= 0);
463 if (path->uptodate == BTREE_ITER_NEED_RELOCK)
464 path->uptodate = BTREE_ITER_UPTODATE;
466 bch2_trans_verify_locks(trans);
468 return path->uptodate < BTREE_ITER_NEED_RELOCK;
471 bool __bch2_btree_node_relock(struct btree_trans *trans,
472 struct btree_path *path, unsigned level,
475 struct btree *b = btree_path_node(path, level);
476 int want = __btree_lock_want(path, level);
481 if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
482 (btree_node_lock_seq_matches(path, b, level) &&
483 btree_node_lock_increment(trans, &b->c, level, want))) {
484 mark_btree_node_locked(trans, path, level, want);
488 if (trace && !trans->notrace_relock_fail)
489 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
495 bool bch2_btree_node_upgrade(struct btree_trans *trans,
496 struct btree_path *path, unsigned level)
498 struct btree *b = path->l[level].b;
499 struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
501 if (!is_btree_node(path, level))
504 switch (btree_lock_want(path, level)) {
505 case BTREE_NODE_UNLOCKED:
506 BUG_ON(btree_node_locked(path, level));
508 case BTREE_NODE_READ_LOCKED:
509 BUG_ON(btree_node_intent_locked(path, level));
510 return bch2_btree_node_relock(trans, path, level);
511 case BTREE_NODE_INTENT_LOCKED:
513 case BTREE_NODE_WRITE_LOCKED:
517 if (btree_node_intent_locked(path, level))
523 if (btree_node_locked(path, level)) {
526 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
527 ret = six_lock_tryupgrade(&b->c.lock);
528 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
533 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
538 * Do we already have an intent lock via another path? If so, just bump
541 if (btree_node_lock_seq_matches(path, b, level) &&
542 btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
543 btree_node_unlock(trans, path, level);
547 trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
550 mark_btree_node_locked_noreset(path, level, SIX_LOCK_intent);
554 /* Btree path locking: */
557 * Only for btree_cache.c - only relocks intent locks
559 int bch2_btree_path_relock_intent(struct btree_trans *trans,
560 struct btree_path *path)
564 for (l = path->level;
565 l < path->locks_want && btree_path_node(path, l);
567 if (!bch2_btree_node_relock(trans, path, l)) {
568 __bch2_btree_path_unlock(trans, path);
569 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
570 trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
571 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
579 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
580 struct btree_path *path, unsigned long trace_ip)
582 return btree_path_get_locks(trans, path, false);
585 int __bch2_btree_path_relock(struct btree_trans *trans,
586 struct btree_path *path, unsigned long trace_ip)
588 if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
589 trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
590 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
597 bool bch2_btree_path_upgrade_norestart(struct btree_trans *trans,
598 struct btree_path *path, unsigned long trace_ip)
600 return btree_path_get_locks(trans, path, true);
603 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
604 struct btree_path *path,
605 unsigned new_locks_want)
607 EBUG_ON(path->locks_want >= new_locks_want);
609 path->locks_want = new_locks_want;
611 return btree_path_get_locks(trans, path, true);
614 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
615 struct btree_path *path,
616 unsigned new_locks_want)
618 struct btree_path *linked;
620 if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want))
624 * XXX: this is ugly - we'd prefer to not be mucking with other
625 * iterators in the btree_trans here.
627 * On failure to upgrade the iterator, setting iter->locks_want and
628 * calling get_locks() is sufficient to make bch2_btree_path_traverse()
629 * get the locks we want on transaction restart.
631 * But if this iterator was a clone, on transaction restart what we did
632 * to this iterator isn't going to be preserved.
634 * Possibly we could add an iterator field for the parent iterator when
635 * an iterator is a copy - for now, we'll just upgrade any other
636 * iterators with the same btree id.
638 * The code below used to be needed to ensure ancestor nodes get locked
639 * before interior nodes - now that's handled by
640 * bch2_btree_path_traverse_all().
642 if (!path->cached && !trans->in_traverse_all)
643 trans_for_each_path(trans, linked)
644 if (linked != path &&
645 linked->cached == path->cached &&
646 linked->btree_id == path->btree_id &&
647 linked->locks_want < new_locks_want) {
648 linked->locks_want = new_locks_want;
649 btree_path_get_locks(trans, linked, true);
655 void __bch2_btree_path_downgrade(struct btree_trans *trans,
656 struct btree_path *path,
657 unsigned new_locks_want)
661 EBUG_ON(path->locks_want < new_locks_want);
663 path->locks_want = new_locks_want;
665 while (path->nodes_locked &&
666 (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
667 if (l > path->level) {
668 btree_node_unlock(trans, path, l);
670 if (btree_node_intent_locked(path, l)) {
671 six_lock_downgrade(&path->l[l].b->c.lock);
672 mark_btree_node_locked_noreset(path, l, SIX_LOCK_read);
678 bch2_btree_path_verify_locks(path);
681 /* Btree transaction locking: */
683 void bch2_trans_downgrade(struct btree_trans *trans)
685 struct btree_path *path;
687 trans_for_each_path(trans, path)
688 bch2_btree_path_downgrade(trans, path);
691 int bch2_trans_relock(struct btree_trans *trans)
693 struct btree_path *path;
695 if (unlikely(trans->restarted))
696 return -((int) trans->restarted);
698 trans_for_each_path(trans, path)
699 if (path->should_be_locked &&
700 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
701 trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
702 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
707 int bch2_trans_relock_notrace(struct btree_trans *trans)
709 struct btree_path *path;
711 if (unlikely(trans->restarted))
712 return -((int) trans->restarted);
714 trans_for_each_path(trans, path)
715 if (path->should_be_locked &&
716 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
717 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
722 void bch2_trans_unlock(struct btree_trans *trans)
724 struct btree_path *path;
726 trans_for_each_path(trans, path)
727 __bch2_btree_path_unlock(trans, path);
730 * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
731 * btree nodes, it implements its own walking:
733 if (!trans->is_initial_gc)
734 bch2_assert_btree_nodes_not_locked();
737 bool bch2_trans_locked(struct btree_trans *trans)
739 struct btree_path *path;
741 trans_for_each_path(trans, path)
742 if (path->nodes_locked)
747 int __bch2_trans_mutex_lock(struct btree_trans *trans,
752 bch2_trans_unlock(trans);
754 ret = bch2_trans_relock(trans);
762 #ifdef CONFIG_BCACHEFS_DEBUG
764 void bch2_btree_path_verify_locks(struct btree_path *path)
768 if (!path->nodes_locked) {
769 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
770 btree_path_node(path, path->level));
774 for (l = 0; l < BTREE_MAX_DEPTH; l++) {
775 int want = btree_lock_want(path, l);
776 int have = btree_node_locked_type(path, l);
778 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
780 BUG_ON(is_btree_node(path, l) &&
781 (want == BTREE_NODE_UNLOCKED ||
782 have != BTREE_NODE_WRITE_LOCKED) &&
787 void bch2_trans_verify_locks(struct btree_trans *trans)
789 struct btree_path *path;
791 trans_for_each_path(trans, path)
792 bch2_btree_path_verify_locks(path);