1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_locking.h"
5 #include "btree_types.h"
7 struct lock_class_key bch2_btree_node_lock_key;
9 /* Btree node locking: */
11 static inline void six_lock_readers_add(struct six_lock *lock, int nr)
14 this_cpu_add(*lock->readers, nr);
16 atomic64_add(__SIX_VAL(read_lock, nr), &lock->state.counter);
18 atomic64_sub(__SIX_VAL(read_lock, -nr), &lock->state.counter);
21 struct six_lock_count bch2_btree_node_lock_counts(struct btree_trans *trans,
22 struct btree_path *skip,
23 struct btree_bkey_cached_common *b,
26 struct btree_path *path;
27 struct six_lock_count ret;
29 memset(&ret, 0, sizeof(ret));
31 if (IS_ERR_OR_NULL(b))
34 trans_for_each_path(trans, path)
35 if (path != skip && &path->l[level].b->c == b) {
36 int t = btree_node_locked_type(path, level);
38 if (t != BTREE_NODE_UNLOCKED)
47 void bch2_btree_node_unlock_write(struct btree_trans *trans,
48 struct btree_path *path, struct btree *b)
50 bch2_btree_node_unlock_write_inlined(trans, path, b);
56 * @trans wants to lock @b with type @type
58 struct trans_waiting_for_lock {
59 struct btree_trans *trans;
60 struct btree_bkey_cached_common *node_want;
61 enum six_lock_type lock_want;
63 /* for iterating over held locks :*/
70 struct trans_waiting_for_lock g[8];
74 static noinline void print_cycle(struct printbuf *out, struct lock_graph *g)
76 struct trans_waiting_for_lock *i;
78 prt_printf(out, "Found lock cycle (%u entries):", g->nr);
81 for (i = g->g; i < g->g + g->nr; i++)
82 bch2_btree_trans_to_text(out, i->trans);
85 static noinline void print_chain(struct printbuf *out, struct lock_graph *g)
87 struct trans_waiting_for_lock *i;
89 for (i = g->g; i != g->g + g->nr; i++) {
92 prt_printf(out, "%u ", i->trans->locking_wait.task->pid);
97 static void lock_graph_up(struct lock_graph *g)
99 closure_put(&g->g[--g->nr].trans->ref);
102 static noinline void lock_graph_pop_all(struct lock_graph *g)
108 static void lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
110 closure_get(&trans->ref);
112 g->g[g->nr++] = (struct trans_waiting_for_lock) {
114 .node_want = trans->locking,
115 .lock_want = trans->locking_wait.lock_want,
119 static bool lock_graph_remove_non_waiters(struct lock_graph *g)
121 struct trans_waiting_for_lock *i;
123 for (i = g->g + 1; i < g->g + g->nr; i++)
124 if (i->trans->locking != i->node_want ||
125 i->trans->locking_wait.start_time != i[-1].lock_start_time) {
126 while (g->g + g->nr > i)
134 static int abort_lock(struct lock_graph *g, struct trans_waiting_for_lock *i)
137 trace_and_count(i->trans->c, trans_restart_would_deadlock, i->trans, _RET_IP_);
138 return btree_trans_restart(i->trans, BCH_ERR_transaction_restart_would_deadlock);
140 i->trans->lock_must_abort = true;
141 wake_up_process(i->trans->locking_wait.task);
146 static int btree_trans_abort_preference(struct btree_trans *trans)
148 if (trans->lock_may_not_fail)
150 if (trans->locking_wait.lock_want == SIX_LOCK_write)
152 if (!trans->in_traverse_all)
157 static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
159 struct trans_waiting_for_lock *i, *abort = NULL;
160 unsigned best = 0, pref;
163 if (lock_graph_remove_non_waiters(g))
166 /* Only checking, for debugfs: */
168 print_cycle(cycle, g);
173 for (i = g->g; i < g->g + g->nr; i++) {
174 pref = btree_trans_abort_preference(i->trans);
181 if (unlikely(!best)) {
182 struct printbuf buf = PRINTBUF;
184 prt_printf(&buf, bch2_fmt(g->g->trans->c, "cycle of nofail locks"));
186 for (i = g->g; i < g->g + g->nr; i++) {
187 struct btree_trans *trans = i->trans;
189 bch2_btree_trans_to_text(&buf, trans);
191 prt_printf(&buf, "backtrace:");
193 printbuf_indent_add(&buf, 2);
194 bch2_prt_backtrace(&buf, trans->locking_wait.task);
195 printbuf_indent_sub(&buf, 2);
199 bch2_print_string_as_lines(KERN_ERR, buf.buf);
204 ret = abort_lock(g, abort);
212 static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
213 struct printbuf *cycle)
215 struct btree_trans *orig_trans = g->g->trans;
216 struct trans_waiting_for_lock *i;
218 for (i = g->g; i < g->g + g->nr; i++)
219 if (i->trans == trans)
220 return break_cycle(g, cycle);
222 if (g->nr == ARRAY_SIZE(g->g)) {
223 if (orig_trans->lock_may_not_fail)
228 trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
229 return btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
232 lock_graph_down(g, trans);
236 static bool lock_type_conflicts(enum six_lock_type t1, enum six_lock_type t2)
241 int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
244 struct trans_waiting_for_lock *top;
245 struct btree_bkey_cached_common *b;
246 struct btree_path *path;
249 if (trans->lock_must_abort) {
250 trace_and_count(trans->c, trans_restart_would_deadlock, trans, _RET_IP_);
251 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
255 lock_graph_down(&g, trans);
260 top = &g.g[g.nr - 1];
262 trans_for_each_path_from(top->trans, path, top->path_idx) {
263 if (!path->nodes_locked)
266 if (top->path_idx != path->idx) {
267 top->path_idx = path->idx;
269 top->lock_start_time = 0;
273 top->level < BTREE_MAX_DEPTH;
274 top->level++, top->lock_start_time = 0) {
275 int lock_held = btree_node_locked_type(path, top->level);
277 if (lock_held == BTREE_NODE_UNLOCKED)
280 b = &READ_ONCE(path->l[top->level].b)->c;
282 if (IS_ERR_OR_NULL(b)) {
284 * If we get here, it means we raced with the
285 * other thread updating its btree_path
286 * structures - which means it can't be blocked
289 if (!lock_graph_remove_non_waiters(&g)) {
291 * If lock_graph_remove_non_waiters()
292 * didn't do anything, it must be
293 * because we're being called by debugfs
294 * checking for lock cycles, which
295 * invokes us on btree_transactions that
296 * aren't actually waiting on anything.
299 lock_graph_pop_all(&g);
305 if (list_empty_careful(&b->lock.wait_list))
308 raw_spin_lock(&b->lock.wait_lock);
309 list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
310 BUG_ON(b != trans->locking);
312 if (top->lock_start_time &&
313 time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
316 top->lock_start_time = trans->locking_wait.start_time;
318 /* Don't check for self deadlock: */
319 if (trans == top->trans ||
320 !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
323 ret = lock_graph_descend(&g, trans, cycle);
324 raw_spin_unlock(&b->lock.wait_lock);
331 raw_spin_unlock(&b->lock.wait_lock);
335 if (g.nr > 1 && cycle)
336 print_chain(cycle, &g);
341 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
343 struct btree_trans *trans = p;
345 return bch2_check_for_deadlock(trans, NULL);
348 int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
349 struct btree_bkey_cached_common *b,
350 bool lock_may_not_fail)
352 int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
356 * Must drop our read locks before calling six_lock_write() -
357 * six_unlock() won't do wakeups until the reader count
358 * goes to 0, and it's safe because we have the node intent
361 six_lock_readers_add(&b->lock, -readers);
362 ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write,
363 lock_may_not_fail, _RET_IP_);
364 six_lock_readers_add(&b->lock, readers);
367 mark_btree_node_locked_noreset(path, b->level, SIX_LOCK_intent);
374 static inline bool btree_path_get_locks(struct btree_trans *trans,
375 struct btree_path *path,
378 unsigned l = path->level;
382 if (!btree_path_node(path, l))
386 ? bch2_btree_node_upgrade(trans, path, l)
387 : bch2_btree_node_relock(trans, path, l)))
391 } while (l < path->locks_want);
394 * When we fail to get a lock, we have to ensure that any child nodes
395 * can't be relocked so bch2_btree_path_traverse has to walk back up to
396 * the node that we failed to relock:
399 __bch2_btree_path_unlock(trans, path);
400 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
403 path->l[fail_idx].b = upgrade
404 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
405 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
407 } while (fail_idx >= 0);
410 if (path->uptodate == BTREE_ITER_NEED_RELOCK)
411 path->uptodate = BTREE_ITER_UPTODATE;
413 bch2_trans_verify_locks(trans);
415 return path->uptodate < BTREE_ITER_NEED_RELOCK;
418 bool __bch2_btree_node_relock(struct btree_trans *trans,
419 struct btree_path *path, unsigned level,
422 struct btree *b = btree_path_node(path, level);
423 int want = __btree_lock_want(path, level);
428 if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
429 (btree_node_lock_seq_matches(path, b, level) &&
430 btree_node_lock_increment(trans, &b->c, level, want))) {
431 mark_btree_node_locked(trans, path, level, want);
435 if (trace && !trans->notrace_relock_fail)
436 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
442 bool bch2_btree_node_upgrade(struct btree_trans *trans,
443 struct btree_path *path, unsigned level)
445 struct btree *b = path->l[level].b;
446 struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
448 if (!is_btree_node(path, level))
451 switch (btree_lock_want(path, level)) {
452 case BTREE_NODE_UNLOCKED:
453 BUG_ON(btree_node_locked(path, level));
455 case BTREE_NODE_READ_LOCKED:
456 BUG_ON(btree_node_intent_locked(path, level));
457 return bch2_btree_node_relock(trans, path, level);
458 case BTREE_NODE_INTENT_LOCKED:
460 case BTREE_NODE_WRITE_LOCKED:
464 if (btree_node_intent_locked(path, level))
470 if (btree_node_locked(path, level)) {
473 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
474 ret = six_lock_tryupgrade(&b->c.lock);
475 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
480 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
485 * Do we already have an intent lock via another path? If so, just bump
488 if (btree_node_lock_seq_matches(path, b, level) &&
489 btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
490 btree_node_unlock(trans, path, level);
494 trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
497 mark_btree_node_locked_noreset(path, level, SIX_LOCK_intent);
501 /* Btree path locking: */
504 * Only for btree_cache.c - only relocks intent locks
506 int bch2_btree_path_relock_intent(struct btree_trans *trans,
507 struct btree_path *path)
511 for (l = path->level;
512 l < path->locks_want && btree_path_node(path, l);
514 if (!bch2_btree_node_relock(trans, path, l)) {
515 __bch2_btree_path_unlock(trans, path);
516 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
517 trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
518 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
526 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
527 struct btree_path *path, unsigned long trace_ip)
529 return btree_path_get_locks(trans, path, false);
532 int __bch2_btree_path_relock(struct btree_trans *trans,
533 struct btree_path *path, unsigned long trace_ip)
535 if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
536 trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
537 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
544 bool bch2_btree_path_upgrade_norestart(struct btree_trans *trans,
545 struct btree_path *path, unsigned long trace_ip)
547 return btree_path_get_locks(trans, path, true);
550 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
551 struct btree_path *path,
552 unsigned new_locks_want)
554 EBUG_ON(path->locks_want >= new_locks_want);
556 path->locks_want = new_locks_want;
558 return btree_path_get_locks(trans, path, true);
561 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
562 struct btree_path *path,
563 unsigned new_locks_want)
565 struct btree_path *linked;
567 if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want))
571 * XXX: this is ugly - we'd prefer to not be mucking with other
572 * iterators in the btree_trans here.
574 * On failure to upgrade the iterator, setting iter->locks_want and
575 * calling get_locks() is sufficient to make bch2_btree_path_traverse()
576 * get the locks we want on transaction restart.
578 * But if this iterator was a clone, on transaction restart what we did
579 * to this iterator isn't going to be preserved.
581 * Possibly we could add an iterator field for the parent iterator when
582 * an iterator is a copy - for now, we'll just upgrade any other
583 * iterators with the same btree id.
585 * The code below used to be needed to ensure ancestor nodes get locked
586 * before interior nodes - now that's handled by
587 * bch2_btree_path_traverse_all().
589 if (!path->cached && !trans->in_traverse_all)
590 trans_for_each_path(trans, linked)
591 if (linked != path &&
592 linked->cached == path->cached &&
593 linked->btree_id == path->btree_id &&
594 linked->locks_want < new_locks_want) {
595 linked->locks_want = new_locks_want;
596 btree_path_get_locks(trans, linked, true);
602 void __bch2_btree_path_downgrade(struct btree_trans *trans,
603 struct btree_path *path,
604 unsigned new_locks_want)
608 EBUG_ON(path->locks_want < new_locks_want);
610 path->locks_want = new_locks_want;
612 while (path->nodes_locked &&
613 (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
614 if (l > path->level) {
615 btree_node_unlock(trans, path, l);
617 if (btree_node_intent_locked(path, l)) {
618 six_lock_downgrade(&path->l[l].b->c.lock);
619 mark_btree_node_locked_noreset(path, l, SIX_LOCK_read);
625 bch2_btree_path_verify_locks(path);
628 /* Btree transaction locking: */
630 void bch2_trans_downgrade(struct btree_trans *trans)
632 struct btree_path *path;
634 trans_for_each_path(trans, path)
635 bch2_btree_path_downgrade(trans, path);
638 int bch2_trans_relock(struct btree_trans *trans)
640 struct btree_path *path;
642 if (unlikely(trans->restarted))
643 return -((int) trans->restarted);
645 trans_for_each_path(trans, path)
646 if (path->should_be_locked &&
647 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
648 trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
649 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
654 int bch2_trans_relock_notrace(struct btree_trans *trans)
656 struct btree_path *path;
658 if (unlikely(trans->restarted))
659 return -((int) trans->restarted);
661 trans_for_each_path(trans, path)
662 if (path->should_be_locked &&
663 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
664 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
669 void bch2_trans_unlock(struct btree_trans *trans)
671 struct btree_path *path;
673 trans_for_each_path(trans, path)
674 __bch2_btree_path_unlock(trans, path);
677 * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
678 * btree nodes, it implements its own walking:
680 EBUG_ON(!trans->is_initial_gc &&
681 lock_class_is_held(&bch2_btree_node_lock_key));
684 bool bch2_trans_locked(struct btree_trans *trans)
686 struct btree_path *path;
688 trans_for_each_path(trans, path)
689 if (path->nodes_locked)
696 #ifdef CONFIG_BCACHEFS_DEBUG
698 void bch2_btree_path_verify_locks(struct btree_path *path)
702 if (!path->nodes_locked) {
703 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
704 btree_path_node(path, path->level));
708 for (l = 0; l < BTREE_MAX_DEPTH; l++) {
709 int want = btree_lock_want(path, l);
710 int have = btree_node_locked_type(path, l);
712 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
714 BUG_ON(is_btree_node(path, l) &&
715 (want == BTREE_NODE_UNLOCKED ||
716 have != BTREE_NODE_WRITE_LOCKED) &&
721 void bch2_trans_verify_locks(struct btree_trans *trans)
723 struct btree_path *path;
725 trans_for_each_path(trans, path)
726 bch2_btree_path_verify_locks(path);