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 void lock_graph_pop(struct lock_graph *g)
76 closure_put(&g->g[--g->nr].trans->ref);
79 static noinline void print_cycle(struct printbuf *out, struct lock_graph *g)
81 struct trans_waiting_for_lock *i;
83 prt_printf(out, "Found lock cycle (%u entries):", g->nr);
86 for (i = g->g; i < g->g + g->nr; i++)
87 bch2_btree_trans_to_text(out, i->trans);
90 static int abort_lock(struct lock_graph *g, struct trans_waiting_for_lock *i)
95 trace_and_count(i->trans->c, trans_restart_would_deadlock, i->trans, _RET_IP_);
96 ret = btree_trans_restart(i->trans, BCH_ERR_transaction_restart_would_deadlock);
98 i->trans->lock_must_abort = true;
102 for (i = g->g + 1; i < g->g + g->nr; i++)
103 wake_up_process(i->trans->locking_wait.task);
107 static noinline int break_cycle(struct lock_graph *g)
109 struct trans_waiting_for_lock *i;
111 for (i = g->g; i < g->g + g->nr; i++) {
112 if (i->trans->lock_may_not_fail ||
113 i->trans->locking_wait.lock_want == SIX_LOCK_write)
116 return abort_lock(g, i);
119 for (i = g->g; i < g->g + g->nr; i++) {
120 if (i->trans->lock_may_not_fail ||
121 !i->trans->in_traverse_all)
124 return abort_lock(g, i);
127 for (i = g->g; i < g->g + g->nr; i++) {
128 if (i->trans->lock_may_not_fail)
131 return abort_lock(g, i);
137 static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
138 struct printbuf *cycle)
140 struct btree_trans *orig_trans = g->g->trans;
141 struct trans_waiting_for_lock *i;
144 for (i = g->g; i < g->g + g->nr; i++) {
145 if (i->trans->locking != i->node_want)
146 while (g->g + g->nr >= i) {
151 if (i->trans == trans) {
154 print_cycle(cycle, g);
157 ret = break_cycle(g);
163 * If we didn't abort (instead telling another
164 * transaction to abort), keep checking:
169 if (g->nr == ARRAY_SIZE(g->g)) {
170 if (orig_trans->lock_may_not_fail)
173 trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
174 ret = btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
178 closure_get(&trans->ref);
180 g->g[g->nr++] = (struct trans_waiting_for_lock) {
182 .node_want = trans->locking,
183 .lock_want = trans->locking_wait.lock_want,
193 static noinline void lock_graph_remove_non_waiters(struct lock_graph *g)
195 struct trans_waiting_for_lock *i;
197 for (i = g->g + 1; i < g->g + g->nr; i++)
198 if (i->trans->locking != i->node_want ||
199 i->trans->locking_wait.start_time != i[-1].lock_start_time) {
200 while (g->g + g->nr >= i)
207 static bool lock_type_conflicts(enum six_lock_type t1, enum six_lock_type t2)
212 int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
215 struct trans_waiting_for_lock *top;
216 struct btree_bkey_cached_common *b;
217 struct btree_path *path;
220 if (trans->lock_must_abort) {
221 trace_and_count(trans->c, trans_restart_would_deadlock, trans, _RET_IP_);
222 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
226 ret = lock_graph_descend(&g, trans, cycle);
232 top = &g.g[g.nr - 1];
234 trans_for_each_path_from(top->trans, path, top->path_idx) {
235 if (!path->nodes_locked)
238 if (top->path_idx != path->idx) {
239 top->path_idx = path->idx;
241 top->lock_start_time = 0;
245 top->level < BTREE_MAX_DEPTH;
246 top->level++, top->lock_start_time = 0) {
247 int lock_held = btree_node_locked_type(path, top->level);
249 if (lock_held == BTREE_NODE_UNLOCKED)
252 b = &READ_ONCE(path->l[top->level].b)->c;
254 if (unlikely(IS_ERR_OR_NULL(b))) {
255 lock_graph_remove_non_waiters(&g);
259 if (list_empty_careful(&b->lock.wait_list))
262 raw_spin_lock(&b->lock.wait_lock);
263 list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
264 BUG_ON(b != trans->locking);
266 if (top->lock_start_time &&
267 time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
270 top->lock_start_time = trans->locking_wait.start_time;
272 /* Don't check for self deadlock: */
273 if (trans == top->trans ||
274 !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
277 ret = lock_graph_descend(&g, trans, cycle);
278 raw_spin_unlock(&b->lock.wait_lock);
281 return ret < 0 ? ret : 0;
285 raw_spin_unlock(&b->lock.wait_lock);
293 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
295 struct btree_trans *trans = p;
297 return bch2_check_for_deadlock(trans, NULL);
300 int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
301 struct btree_bkey_cached_common *b,
302 bool lock_may_not_fail)
304 int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
308 * Must drop our read locks before calling six_lock_write() -
309 * six_unlock() won't do wakeups until the reader count
310 * goes to 0, and it's safe because we have the node intent
313 six_lock_readers_add(&b->lock, -readers);
314 ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write, lock_may_not_fail);
315 six_lock_readers_add(&b->lock, readers);
318 mark_btree_node_locked_noreset(path, b->level, SIX_LOCK_intent);
325 static inline bool btree_path_get_locks(struct btree_trans *trans,
326 struct btree_path *path,
329 unsigned l = path->level;
333 if (!btree_path_node(path, l))
337 ? bch2_btree_node_upgrade(trans, path, l)
338 : bch2_btree_node_relock(trans, path, l)))
342 } while (l < path->locks_want);
345 * When we fail to get a lock, we have to ensure that any child nodes
346 * can't be relocked so bch2_btree_path_traverse has to walk back up to
347 * the node that we failed to relock:
350 __bch2_btree_path_unlock(trans, path);
351 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
354 path->l[fail_idx].b = upgrade
355 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
356 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
358 } while (fail_idx >= 0);
361 if (path->uptodate == BTREE_ITER_NEED_RELOCK)
362 path->uptodate = BTREE_ITER_UPTODATE;
364 bch2_trans_verify_locks(trans);
366 return path->uptodate < BTREE_ITER_NEED_RELOCK;
369 bool __bch2_btree_node_relock(struct btree_trans *trans,
370 struct btree_path *path, unsigned level,
373 struct btree *b = btree_path_node(path, level);
374 int want = __btree_lock_want(path, level);
379 if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
380 (btree_node_lock_seq_matches(path, b, level) &&
381 btree_node_lock_increment(trans, &b->c, level, want))) {
382 mark_btree_node_locked(trans, path, level, want);
387 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
393 bool bch2_btree_node_upgrade(struct btree_trans *trans,
394 struct btree_path *path, unsigned level)
396 struct btree *b = path->l[level].b;
397 struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
399 if (!is_btree_node(path, level))
402 switch (btree_lock_want(path, level)) {
403 case BTREE_NODE_UNLOCKED:
404 BUG_ON(btree_node_locked(path, level));
406 case BTREE_NODE_READ_LOCKED:
407 BUG_ON(btree_node_intent_locked(path, level));
408 return bch2_btree_node_relock(trans, path, level);
409 case BTREE_NODE_INTENT_LOCKED:
411 case BTREE_NODE_WRITE_LOCKED:
415 if (btree_node_intent_locked(path, level))
421 if (btree_node_locked(path, level)) {
424 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
425 ret = six_lock_tryupgrade(&b->c.lock);
426 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
431 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
436 * Do we already have an intent lock via another path? If so, just bump
439 if (btree_node_lock_seq_matches(path, b, level) &&
440 btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
441 btree_node_unlock(trans, path, level);
445 trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
448 mark_btree_node_locked_noreset(path, level, SIX_LOCK_intent);
452 /* Btree path locking: */
455 * Only for btree_cache.c - only relocks intent locks
457 int bch2_btree_path_relock_intent(struct btree_trans *trans,
458 struct btree_path *path)
462 for (l = path->level;
463 l < path->locks_want && btree_path_node(path, l);
465 if (!bch2_btree_node_relock(trans, path, l)) {
466 __bch2_btree_path_unlock(trans, path);
467 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
468 trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
469 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
477 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
478 struct btree_path *path, unsigned long trace_ip)
480 return btree_path_get_locks(trans, path, false);
484 bool bch2_btree_path_upgrade_norestart(struct btree_trans *trans,
485 struct btree_path *path, unsigned long trace_ip)
487 return btree_path_get_locks(trans, path, true);
490 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
491 struct btree_path *path,
492 unsigned new_locks_want)
494 EBUG_ON(path->locks_want >= new_locks_want);
496 path->locks_want = new_locks_want;
498 return btree_path_get_locks(trans, path, true);
501 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
502 struct btree_path *path,
503 unsigned new_locks_want)
505 struct btree_path *linked;
507 if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want))
511 * XXX: this is ugly - we'd prefer to not be mucking with other
512 * iterators in the btree_trans here.
514 * On failure to upgrade the iterator, setting iter->locks_want and
515 * calling get_locks() is sufficient to make bch2_btree_path_traverse()
516 * get the locks we want on transaction restart.
518 * But if this iterator was a clone, on transaction restart what we did
519 * to this iterator isn't going to be preserved.
521 * Possibly we could add an iterator field for the parent iterator when
522 * an iterator is a copy - for now, we'll just upgrade any other
523 * iterators with the same btree id.
525 * The code below used to be needed to ensure ancestor nodes get locked
526 * before interior nodes - now that's handled by
527 * bch2_btree_path_traverse_all().
529 if (!path->cached && !trans->in_traverse_all)
530 trans_for_each_path(trans, linked)
531 if (linked != path &&
532 linked->cached == path->cached &&
533 linked->btree_id == path->btree_id &&
534 linked->locks_want < new_locks_want) {
535 linked->locks_want = new_locks_want;
536 btree_path_get_locks(trans, linked, true);
542 void __bch2_btree_path_downgrade(struct btree_trans *trans,
543 struct btree_path *path,
544 unsigned new_locks_want)
548 EBUG_ON(path->locks_want < new_locks_want);
550 path->locks_want = new_locks_want;
552 while (path->nodes_locked &&
553 (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
554 if (l > path->level) {
555 btree_node_unlock(trans, path, l);
557 if (btree_node_intent_locked(path, l)) {
558 six_lock_downgrade(&path->l[l].b->c.lock);
559 mark_btree_node_locked_noreset(path, l, SIX_LOCK_read);
565 bch2_btree_path_verify_locks(path);
568 /* Btree transaction locking: */
570 void bch2_trans_downgrade(struct btree_trans *trans)
572 struct btree_path *path;
574 trans_for_each_path(trans, path)
575 bch2_btree_path_downgrade(trans, path);
578 int bch2_trans_relock(struct btree_trans *trans)
580 struct btree_path *path;
582 if (unlikely(trans->restarted))
583 return - ((int) trans->restarted);
585 trans_for_each_path(trans, path)
586 if (path->should_be_locked &&
587 !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
588 trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
589 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
594 void bch2_trans_unlock(struct btree_trans *trans)
596 struct btree_path *path;
598 trans_for_each_path(trans, path)
599 __bch2_btree_path_unlock(trans, path);
602 * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
603 * btree nodes, it implements its own walking:
605 BUG_ON(!trans->is_initial_gc &&
606 lock_class_is_held(&bch2_btree_node_lock_key));
611 #ifdef CONFIG_BCACHEFS_DEBUG
613 void bch2_btree_path_verify_locks(struct btree_path *path)
617 if (!path->nodes_locked) {
618 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
619 btree_path_node(path, path->level));
623 for (l = 0; l < BTREE_MAX_DEPTH; l++) {
624 int want = btree_lock_want(path, l);
625 int have = btree_node_locked_type(path, l);
627 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
629 BUG_ON(is_btree_node(path, l) &&
630 (want == BTREE_NODE_UNLOCKED ||
631 have != BTREE_NODE_WRITE_LOCKED) &&
636 void bch2_trans_verify_locks(struct btree_trans *trans)
638 struct btree_path *path;
640 trans_for_each_path(trans, path)
641 bch2_btree_path_verify_locks(path);