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 bch_fs *c = g->g->trans->c;
177 struct printbuf buf = PRINTBUF;
179 bch_err(c, "cycle of nofail locks");
181 for (i = g->g; i < g->g + g->nr; i++) {
182 struct btree_trans *trans = i->trans;
184 bch2_btree_trans_to_text(&buf, trans);
186 prt_printf(&buf, "backtrace:");
188 printbuf_indent_add(&buf, 2);
189 bch2_prt_backtrace(&buf, trans->locking_wait.task);
190 printbuf_indent_sub(&buf, 2);
194 bch2_print_string_as_lines(KERN_ERR, buf.buf);
199 ret = abort_lock(g, abort);
207 static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
208 struct printbuf *cycle)
210 struct btree_trans *orig_trans = g->g->trans;
211 struct trans_waiting_for_lock *i;
213 for (i = g->g; i < g->g + g->nr; i++)
214 if (i->trans == trans)
215 return break_cycle(g, cycle);
217 if (g->nr == ARRAY_SIZE(g->g)) {
218 if (orig_trans->lock_may_not_fail)
223 trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
224 return btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
227 lock_graph_down(g, trans);
231 static bool lock_type_conflicts(enum six_lock_type t1, enum six_lock_type t2)
236 int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
239 struct trans_waiting_for_lock *top;
240 struct btree_bkey_cached_common *b;
241 struct btree_path *path;
244 if (trans->lock_must_abort) {
245 trace_and_count(trans->c, trans_restart_would_deadlock, trans, _RET_IP_);
246 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
250 lock_graph_down(&g, trans);
255 top = &g.g[g.nr - 1];
257 trans_for_each_path_from(top->trans, path, top->path_idx) {
258 if (!path->nodes_locked)
261 if (top->path_idx != path->idx) {
262 top->path_idx = path->idx;
264 top->lock_start_time = 0;
268 top->level < BTREE_MAX_DEPTH;
269 top->level++, top->lock_start_time = 0) {
270 int lock_held = btree_node_locked_type(path, top->level);
272 if (lock_held == BTREE_NODE_UNLOCKED)
275 b = &READ_ONCE(path->l[top->level].b)->c;
277 if (unlikely(IS_ERR_OR_NULL(b))) {
278 BUG_ON(!lock_graph_remove_non_waiters(&g));
282 if (list_empty_careful(&b->lock.wait_list))
285 raw_spin_lock(&b->lock.wait_lock);
286 list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
287 BUG_ON(b != trans->locking);
289 if (top->lock_start_time &&
290 time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
293 top->lock_start_time = trans->locking_wait.start_time;
295 /* Don't check for self deadlock: */
296 if (trans == top->trans ||
297 !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
300 ret = lock_graph_descend(&g, trans, cycle);
301 raw_spin_unlock(&b->lock.wait_lock);
308 raw_spin_unlock(&b->lock.wait_lock);
312 if (g.nr > 1 && cycle)
313 print_chain(cycle, &g);
318 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
320 struct btree_trans *trans = p;
322 return bch2_check_for_deadlock(trans, NULL);
325 int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
326 struct btree_bkey_cached_common *b,
327 bool lock_may_not_fail)
329 int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
333 * Must drop our read locks before calling six_lock_write() -
334 * six_unlock() won't do wakeups until the reader count
335 * goes to 0, and it's safe because we have the node intent
338 six_lock_readers_add(&b->lock, -readers);
339 ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write, lock_may_not_fail);
340 six_lock_readers_add(&b->lock, readers);
343 mark_btree_node_locked_noreset(path, b->level, SIX_LOCK_intent);
350 static inline bool btree_path_get_locks(struct btree_trans *trans,
351 struct btree_path *path,
354 unsigned l = path->level;
358 if (!btree_path_node(path, l))
362 ? bch2_btree_node_upgrade(trans, path, l)
363 : bch2_btree_node_relock(trans, path, l)))
367 } while (l < path->locks_want);
370 * When we fail to get a lock, we have to ensure that any child nodes
371 * can't be relocked so bch2_btree_path_traverse has to walk back up to
372 * the node that we failed to relock:
375 __bch2_btree_path_unlock(trans, path);
376 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
379 path->l[fail_idx].b = upgrade
380 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
381 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
383 } while (fail_idx >= 0);
386 if (path->uptodate == BTREE_ITER_NEED_RELOCK)
387 path->uptodate = BTREE_ITER_UPTODATE;
389 bch2_trans_verify_locks(trans);
391 return path->uptodate < BTREE_ITER_NEED_RELOCK;
394 bool __bch2_btree_node_relock(struct btree_trans *trans,
395 struct btree_path *path, unsigned level,
398 struct btree *b = btree_path_node(path, level);
399 int want = __btree_lock_want(path, level);
404 if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
405 (btree_node_lock_seq_matches(path, b, level) &&
406 btree_node_lock_increment(trans, &b->c, level, want))) {
407 mark_btree_node_locked(trans, path, level, want);
412 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
418 bool bch2_btree_node_upgrade(struct btree_trans *trans,
419 struct btree_path *path, unsigned level)
421 struct btree *b = path->l[level].b;
422 struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
424 if (!is_btree_node(path, level))
427 switch (btree_lock_want(path, level)) {
428 case BTREE_NODE_UNLOCKED:
429 BUG_ON(btree_node_locked(path, level));
431 case BTREE_NODE_READ_LOCKED:
432 BUG_ON(btree_node_intent_locked(path, level));
433 return bch2_btree_node_relock(trans, path, level);
434 case BTREE_NODE_INTENT_LOCKED:
436 case BTREE_NODE_WRITE_LOCKED:
440 if (btree_node_intent_locked(path, level))
446 if (btree_node_locked(path, level)) {
449 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
450 ret = six_lock_tryupgrade(&b->c.lock);
451 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
456 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
461 * Do we already have an intent lock via another path? If so, just bump
464 if (btree_node_lock_seq_matches(path, b, level) &&
465 btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
466 btree_node_unlock(trans, path, level);
470 trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
473 mark_btree_node_locked_noreset(path, level, SIX_LOCK_intent);
477 /* Btree path locking: */
480 * Only for btree_cache.c - only relocks intent locks
482 int bch2_btree_path_relock_intent(struct btree_trans *trans,
483 struct btree_path *path)
487 for (l = path->level;
488 l < path->locks_want && btree_path_node(path, l);
490 if (!bch2_btree_node_relock(trans, path, l)) {
491 __bch2_btree_path_unlock(trans, path);
492 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
493 trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
494 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
502 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
503 struct btree_path *path, unsigned long trace_ip)
505 return btree_path_get_locks(trans, path, false);
509 bool bch2_btree_path_upgrade_norestart(struct btree_trans *trans,
510 struct btree_path *path, unsigned long trace_ip)
512 return btree_path_get_locks(trans, path, true);
515 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
516 struct btree_path *path,
517 unsigned new_locks_want)
519 EBUG_ON(path->locks_want >= new_locks_want);
521 path->locks_want = new_locks_want;
523 return btree_path_get_locks(trans, path, true);
526 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
527 struct btree_path *path,
528 unsigned new_locks_want)
530 struct btree_path *linked;
532 if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want))
536 * XXX: this is ugly - we'd prefer to not be mucking with other
537 * iterators in the btree_trans here.
539 * On failure to upgrade the iterator, setting iter->locks_want and
540 * calling get_locks() is sufficient to make bch2_btree_path_traverse()
541 * get the locks we want on transaction restart.
543 * But if this iterator was a clone, on transaction restart what we did
544 * to this iterator isn't going to be preserved.
546 * Possibly we could add an iterator field for the parent iterator when
547 * an iterator is a copy - for now, we'll just upgrade any other
548 * iterators with the same btree id.
550 * The code below used to be needed to ensure ancestor nodes get locked
551 * before interior nodes - now that's handled by
552 * bch2_btree_path_traverse_all().
554 if (!path->cached && !trans->in_traverse_all)
555 trans_for_each_path(trans, linked)
556 if (linked != path &&
557 linked->cached == path->cached &&
558 linked->btree_id == path->btree_id &&
559 linked->locks_want < new_locks_want) {
560 linked->locks_want = new_locks_want;
561 btree_path_get_locks(trans, linked, true);
567 void __bch2_btree_path_downgrade(struct btree_trans *trans,
568 struct btree_path *path,
569 unsigned new_locks_want)
573 EBUG_ON(path->locks_want < new_locks_want);
575 path->locks_want = new_locks_want;
577 while (path->nodes_locked &&
578 (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
579 if (l > path->level) {
580 btree_node_unlock(trans, path, l);
582 if (btree_node_intent_locked(path, l)) {
583 six_lock_downgrade(&path->l[l].b->c.lock);
584 mark_btree_node_locked_noreset(path, l, SIX_LOCK_read);
590 bch2_btree_path_verify_locks(path);
593 /* Btree transaction locking: */
595 void bch2_trans_downgrade(struct btree_trans *trans)
597 struct btree_path *path;
599 trans_for_each_path(trans, path)
600 bch2_btree_path_downgrade(trans, path);
603 int bch2_trans_relock(struct btree_trans *trans)
605 struct btree_path *path;
607 if (unlikely(trans->restarted))
608 return - ((int) trans->restarted);
610 trans_for_each_path(trans, path)
611 if (path->should_be_locked &&
612 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
613 trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
614 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
619 void bch2_trans_unlock(struct btree_trans *trans)
621 struct btree_path *path;
623 trans_for_each_path(trans, path)
624 __bch2_btree_path_unlock(trans, path);
627 * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
628 * btree nodes, it implements its own walking:
630 EBUG_ON(!trans->is_initial_gc &&
631 lock_class_is_held(&bch2_btree_node_lock_key));
634 bool bch2_trans_locked(struct btree_trans *trans)
636 struct btree_path *path;
638 trans_for_each_path(trans, path)
639 if (path->nodes_locked)
646 #ifdef CONFIG_BCACHEFS_DEBUG
648 void bch2_btree_path_verify_locks(struct btree_path *path)
652 if (!path->nodes_locked) {
653 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
654 btree_path_node(path, path->level));
658 for (l = 0; l < BTREE_MAX_DEPTH; l++) {
659 int want = btree_lock_want(path, l);
660 int have = btree_node_locked_type(path, l);
662 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
664 BUG_ON(is_btree_node(path, l) &&
665 (want == BTREE_NODE_UNLOCKED ||
666 have != BTREE_NODE_WRITE_LOCKED) &&
671 void bch2_trans_verify_locks(struct btree_trans *trans)
673 struct btree_path *path;
675 trans_for_each_path(trans, path)
676 bch2_btree_path_verify_locks(path);