]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/btree_locking.c
Update bcachefs sources to 8dbfede1d9 fixup! bcachefs: More info on check_bucket_ref...
[bcachefs-tools-debian] / libbcachefs / btree_locking.c
index 93a6ebed3aba25ebf2873845d02d9b5da0510a3d..80398e497bd53a3a546a77c45bafd522c065ad4d 100644 (file)
@@ -99,6 +99,12 @@ static void lock_graph_up(struct lock_graph *g)
        closure_put(&g->g[--g->nr].trans->ref);
 }
 
+static noinline void lock_graph_pop_all(struct lock_graph *g)
+{
+       while (g->nr)
+               lock_graph_up(g);
+}
+
 static void lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
 {
        closure_get(&trans->ref);
@@ -173,10 +179,9 @@ static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
        }
 
        if (unlikely(!best)) {
-               struct bch_fs *c = g->g->trans->c;
                struct printbuf buf = PRINTBUF;
 
-               bch_err(c, "cycle of nofail locks");
+               prt_printf(&buf, bch2_fmt(g->g->trans->c, "cycle of nofail locks"));
 
                for (i = g->g; i < g->g + g->nr; i++) {
                        struct btree_trans *trans = i->trans;
@@ -186,7 +191,7 @@ static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
                        prt_printf(&buf, "backtrace:");
                        prt_newline(&buf);
                        printbuf_indent_add(&buf, 2);
-                       bch2_prt_backtrace(&buf, trans->locking_wait.task);
+                       bch2_prt_task_backtrace(&buf, trans->locking_wait.task);
                        printbuf_indent_sub(&buf, 2);
                        prt_newline(&buf);
                }
@@ -220,6 +225,10 @@ static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
 
                while (g->nr)
                        lock_graph_up(g);
+
+               if (cycle)
+                       return 0;
+
                trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
                return btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
        }
@@ -242,6 +251,9 @@ int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
        int ret;
 
        if (trans->lock_must_abort) {
+               if (cycle)
+                       return -1;
+
                trace_and_count(trans->c, trans_restart_would_deadlock, trans, _RET_IP_);
                return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
        }
@@ -274,8 +286,26 @@ next:
 
                        b = &READ_ONCE(path->l[top->level].b)->c;
 
-                       if (unlikely(IS_ERR_OR_NULL(b))) {
-                               BUG_ON(!lock_graph_remove_non_waiters(&g));
+                       if (IS_ERR_OR_NULL(b)) {
+                               /*
+                                * If we get here, it means we raced with the
+                                * other thread updating its btree_path
+                                * structures - which means it can't be blocked
+                                * waiting on a lock:
+                                */
+                               if (!lock_graph_remove_non_waiters(&g)) {
+                                       /*
+                                        * If lock_graph_remove_non_waiters()
+                                        * didn't do anything, it must be
+                                        * because we're being called by debugfs
+                                        * checking for lock cycles, which
+                                        * invokes us on btree_transactions that
+                                        * aren't actually waiting on anything.
+                                        * Just bail out:
+                                        */
+                                       lock_graph_pop_all(&g);
+                               }
+
                                goto next;
                        }
 
@@ -336,7 +366,8 @@ int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *p
         * locked:
         */
        six_lock_readers_add(&b->lock, -readers);
-       ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write, lock_may_not_fail);
+       ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write,
+                                      lock_may_not_fail, _RET_IP_);
        six_lock_readers_add(&b->lock, readers);
 
        if (ret)
@@ -408,7 +439,7 @@ bool __bch2_btree_node_relock(struct btree_trans *trans,
                return true;
        }
 fail:
-       if (trace)
+       if (trace && !trans->notrace_relock_fail)
                trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
        return false;
 }
@@ -505,6 +536,17 @@ bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
        return btree_path_get_locks(trans, path, false);
 }
 
+int __bch2_btree_path_relock(struct btree_trans *trans,
+                       struct btree_path *path, unsigned long trace_ip)
+{
+       if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
+               trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
+               return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
+       }
+
+       return 0;
+}
+
 __flatten
 bool bch2_btree_path_upgrade_norestart(struct btree_trans *trans,
                        struct btree_path *path, unsigned long trace_ip)
@@ -605,7 +647,7 @@ int bch2_trans_relock(struct btree_trans *trans)
        struct btree_path *path;
 
        if (unlikely(trans->restarted))
-               return - ((int) trans->restarted);
+               return -((int) trans->restarted);
 
        trans_for_each_path(trans, path)
                if (path->should_be_locked &&
@@ -616,6 +658,21 @@ int bch2_trans_relock(struct btree_trans *trans)
        return 0;
 }
 
+int bch2_trans_relock_notrace(struct btree_trans *trans)
+{
+       struct btree_path *path;
+
+       if (unlikely(trans->restarted))
+               return -((int) trans->restarted);
+
+       trans_for_each_path(trans, path)
+               if (path->should_be_locked &&
+                   !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
+                       return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
+               }
+       return 0;
+}
+
 void bch2_trans_unlock(struct btree_trans *trans)
 {
        struct btree_path *path;