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 void lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
104 closure_get(&trans->ref);
106 g->g[g->nr++] = (struct trans_waiting_for_lock) {
108 .node_want = trans->locking,
109 .lock_want = trans->locking_wait.lock_want,
113 static bool lock_graph_remove_non_waiters(struct lock_graph *g)
115 struct trans_waiting_for_lock *i;
117 for (i = g->g + 1; i < g->g + g->nr; i++)
118 if (i->trans->locking != i->node_want ||
119 i->trans->locking_wait.start_time != i[-1].lock_start_time) {
120 while (g->g + g->nr > i)
128 static int abort_lock(struct lock_graph *g, struct trans_waiting_for_lock *i)
131 trace_and_count(i->trans->c, trans_restart_would_deadlock, i->trans, _RET_IP_);
132 return btree_trans_restart(i->trans, BCH_ERR_transaction_restart_would_deadlock);
134 i->trans->lock_must_abort = true;
135 wake_up_process(i->trans->locking_wait.task);
140 static int btree_trans_abort_preference(struct btree_trans *trans)
142 if (trans->lock_may_not_fail)
144 if (trans->locking_wait.lock_want == SIX_LOCK_write)
146 if (!trans->in_traverse_all)
151 static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
153 struct trans_waiting_for_lock *i, *abort = NULL;
154 unsigned best = 0, pref;
157 if (lock_graph_remove_non_waiters(g))
160 /* Only checking, for debugfs: */
162 print_cycle(cycle, g);
167 for (i = g->g; i < g->g + g->nr; i++) {
168 pref = btree_trans_abort_preference(i->trans);
175 if (unlikely(!best)) {
176 struct printbuf buf = PRINTBUF;
178 prt_printf(&buf, bch2_fmt(g->g->trans->c, "cycle of nofail locks"));
180 for (i = g->g; i < g->g + g->nr; i++) {
181 struct btree_trans *trans = i->trans;
183 bch2_btree_trans_to_text(&buf, trans);
185 prt_printf(&buf, "backtrace:");
187 printbuf_indent_add(&buf, 2);
188 bch2_prt_backtrace(&buf, trans->locking_wait.task);
189 printbuf_indent_sub(&buf, 2);
193 bch2_print_string_as_lines(KERN_ERR, buf.buf);
198 ret = abort_lock(g, abort);
206 static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
207 struct printbuf *cycle)
209 struct btree_trans *orig_trans = g->g->trans;
210 struct trans_waiting_for_lock *i;
212 for (i = g->g; i < g->g + g->nr; i++)
213 if (i->trans == trans)
214 return break_cycle(g, cycle);
216 if (g->nr == ARRAY_SIZE(g->g)) {
217 if (orig_trans->lock_may_not_fail)
222 trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
223 return btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
226 lock_graph_down(g, trans);
230 static bool lock_type_conflicts(enum six_lock_type t1, enum six_lock_type t2)
235 int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
238 struct trans_waiting_for_lock *top;
239 struct btree_bkey_cached_common *b;
240 struct btree_path *path;
243 if (trans->lock_must_abort) {
244 trace_and_count(trans->c, trans_restart_would_deadlock, trans, _RET_IP_);
245 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
249 lock_graph_down(&g, trans);
254 top = &g.g[g.nr - 1];
256 trans_for_each_path_from(top->trans, path, top->path_idx) {
257 if (!path->nodes_locked)
260 if (top->path_idx != path->idx) {
261 top->path_idx = path->idx;
263 top->lock_start_time = 0;
267 top->level < BTREE_MAX_DEPTH;
268 top->level++, top->lock_start_time = 0) {
269 int lock_held = btree_node_locked_type(path, top->level);
271 if (lock_held == BTREE_NODE_UNLOCKED)
274 b = &READ_ONCE(path->l[top->level].b)->c;
276 if (IS_ERR_OR_NULL(b)) {
277 BUG_ON(!lock_graph_remove_non_waiters(&g));
281 if (list_empty_careful(&b->lock.wait_list))
284 raw_spin_lock(&b->lock.wait_lock);
285 list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
286 BUG_ON(b != trans->locking);
288 if (top->lock_start_time &&
289 time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
292 top->lock_start_time = trans->locking_wait.start_time;
294 /* Don't check for self deadlock: */
295 if (trans == top->trans ||
296 !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
299 ret = lock_graph_descend(&g, trans, cycle);
300 raw_spin_unlock(&b->lock.wait_lock);
307 raw_spin_unlock(&b->lock.wait_lock);
311 if (g.nr > 1 && cycle)
312 print_chain(cycle, &g);
317 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
319 struct btree_trans *trans = p;
321 return bch2_check_for_deadlock(trans, NULL);
324 int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
325 struct btree_bkey_cached_common *b,
326 bool lock_may_not_fail)
328 int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
332 * Must drop our read locks before calling six_lock_write() -
333 * six_unlock() won't do wakeups until the reader count
334 * goes to 0, and it's safe because we have the node intent
337 six_lock_readers_add(&b->lock, -readers);
338 ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write, lock_may_not_fail);
339 six_lock_readers_add(&b->lock, readers);
342 mark_btree_node_locked_noreset(path, b->level, SIX_LOCK_intent);
349 static inline bool btree_path_get_locks(struct btree_trans *trans,
350 struct btree_path *path,
353 unsigned l = path->level;
357 if (!btree_path_node(path, l))
361 ? bch2_btree_node_upgrade(trans, path, l)
362 : bch2_btree_node_relock(trans, path, l))) {
364 ? trace_node_upgrade_fail
365 : trace_node_relock_fail)(0, _RET_IP_,
366 path->btree_id, &path->pos,
367 l, path->l[l].lock_seq,
369 is_btree_node(path, l)
370 ? path->l[l].b->c.lock.state.seq
376 } while (l < path->locks_want);
379 * When we fail to get a lock, we have to ensure that any child nodes
380 * can't be relocked so bch2_btree_path_traverse has to walk back up to
381 * the node that we failed to relock:
384 __bch2_btree_path_unlock(trans, path);
385 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
388 path->l[fail_idx].b = upgrade
389 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
390 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
392 } while (fail_idx >= 0);
395 if (path->uptodate == BTREE_ITER_NEED_RELOCK)
396 path->uptodate = BTREE_ITER_UPTODATE;
398 bch2_trans_verify_locks(trans);
400 return path->uptodate < BTREE_ITER_NEED_RELOCK;
403 bool __bch2_btree_node_relock(struct btree_trans *trans,
404 struct btree_path *path, unsigned level,
407 struct btree *b = btree_path_node(path, level);
408 int want = __btree_lock_want(path, level);
413 if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
414 (btree_node_lock_seq_matches(path, b, level) &&
415 btree_node_lock_increment(trans, &b->c, level, want))) {
416 mark_btree_node_locked(trans, path, level, want);
421 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
427 bool bch2_btree_node_upgrade(struct btree_trans *trans,
428 struct btree_path *path, unsigned level)
430 struct btree *b = path->l[level].b;
431 struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
433 if (!is_btree_node(path, level))
436 switch (btree_lock_want(path, level)) {
437 case BTREE_NODE_UNLOCKED:
438 BUG_ON(btree_node_locked(path, level));
440 case BTREE_NODE_READ_LOCKED:
441 BUG_ON(btree_node_intent_locked(path, level));
442 return bch2_btree_node_relock(trans, path, level);
443 case BTREE_NODE_INTENT_LOCKED:
445 case BTREE_NODE_WRITE_LOCKED:
449 if (btree_node_intent_locked(path, level))
455 if (btree_node_locked(path, level)) {
458 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
459 ret = six_lock_tryupgrade(&b->c.lock);
460 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
465 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
470 * Do we already have an intent lock via another path? If so, just bump
473 if (btree_node_lock_seq_matches(path, b, level) &&
474 btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
475 btree_node_unlock(trans, path, level);
479 trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
482 mark_btree_node_locked_noreset(path, level, SIX_LOCK_intent);
486 /* Btree path locking: */
489 * Only for btree_cache.c - only relocks intent locks
491 int bch2_btree_path_relock_intent(struct btree_trans *trans,
492 struct btree_path *path)
496 for (l = path->level;
497 l < path->locks_want && btree_path_node(path, l);
499 if (!bch2_btree_node_relock(trans, path, l)) {
500 __bch2_btree_path_unlock(trans, path);
501 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
502 trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
503 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
511 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
512 struct btree_path *path, unsigned long trace_ip)
514 return btree_path_get_locks(trans, path, false);
518 bool bch2_btree_path_upgrade_norestart(struct btree_trans *trans,
519 struct btree_path *path, unsigned long trace_ip)
521 return btree_path_get_locks(trans, path, true);
524 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
525 struct btree_path *path,
526 unsigned new_locks_want)
528 EBUG_ON(path->locks_want >= new_locks_want);
530 path->locks_want = new_locks_want;
532 return btree_path_get_locks(trans, path, true);
535 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
536 struct btree_path *path,
537 unsigned new_locks_want)
539 struct btree_path *linked;
541 if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want))
545 * XXX: this is ugly - we'd prefer to not be mucking with other
546 * iterators in the btree_trans here.
548 * On failure to upgrade the iterator, setting iter->locks_want and
549 * calling get_locks() is sufficient to make bch2_btree_path_traverse()
550 * get the locks we want on transaction restart.
552 * But if this iterator was a clone, on transaction restart what we did
553 * to this iterator isn't going to be preserved.
555 * Possibly we could add an iterator field for the parent iterator when
556 * an iterator is a copy - for now, we'll just upgrade any other
557 * iterators with the same btree id.
559 * The code below used to be needed to ensure ancestor nodes get locked
560 * before interior nodes - now that's handled by
561 * bch2_btree_path_traverse_all().
563 if (!path->cached && !trans->in_traverse_all)
564 trans_for_each_path(trans, linked)
565 if (linked != path &&
566 linked->cached == path->cached &&
567 linked->btree_id == path->btree_id &&
568 linked->locks_want < new_locks_want) {
569 linked->locks_want = new_locks_want;
570 btree_path_get_locks(trans, linked, true);
576 void __bch2_btree_path_downgrade(struct btree_trans *trans,
577 struct btree_path *path,
578 unsigned new_locks_want)
582 EBUG_ON(path->locks_want < new_locks_want);
584 path->locks_want = new_locks_want;
586 while (path->nodes_locked &&
587 (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
588 if (l > path->level) {
589 btree_node_unlock(trans, path, l);
591 if (btree_node_intent_locked(path, l)) {
592 six_lock_downgrade(&path->l[l].b->c.lock);
593 mark_btree_node_locked_noreset(path, l, SIX_LOCK_read);
599 bch2_btree_path_verify_locks(path);
602 /* Btree transaction locking: */
604 void bch2_trans_downgrade(struct btree_trans *trans)
606 struct btree_path *path;
608 trans_for_each_path(trans, path)
609 bch2_btree_path_downgrade(trans, path);
612 int bch2_trans_relock(struct btree_trans *trans)
614 struct btree_path *path;
616 if (unlikely(trans->restarted))
617 return -((int) trans->restarted);
619 trans_for_each_path(trans, path)
620 if (path->should_be_locked &&
621 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
622 trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
623 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
628 void bch2_trans_unlock(struct btree_trans *trans)
630 struct btree_path *path;
632 trans_for_each_path(trans, path)
633 __bch2_btree_path_unlock(trans, path);
636 * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
637 * btree nodes, it implements its own walking:
639 EBUG_ON(!trans->is_initial_gc &&
640 lock_class_is_held(&bch2_btree_node_lock_key));
643 bool bch2_trans_locked(struct btree_trans *trans)
645 struct btree_path *path;
647 trans_for_each_path(trans, path)
648 if (path->nodes_locked)
655 #ifdef CONFIG_BCACHEFS_DEBUG
657 void bch2_btree_path_verify_locks(struct btree_path *path)
661 if (!path->nodes_locked) {
662 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
663 btree_path_node(path, path->level));
667 for (l = 0; l < BTREE_MAX_DEPTH; l++) {
668 int want = btree_lock_want(path, l);
669 int have = btree_node_locked_type(path, l);
671 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
673 BUG_ON(is_btree_node(path, l) &&
674 (want == BTREE_NODE_UNLOCKED ||
675 have != BTREE_NODE_WRITE_LOCKED) &&
680 void bch2_trans_verify_locks(struct btree_trans *trans)
682 struct btree_path *path;
684 trans_for_each_path(trans, path)
685 bch2_btree_path_verify_locks(path);