]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
bcachefs: Work around deadlock to btree node rewrites in journal replay
authorKent Overstreet <kent.overstreet@linux.dev>
Tue, 1 Jul 2025 17:36:51 +0000 (13:36 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Tue, 1 Jul 2025 23:33:46 +0000 (19:33 -0400)
Don't mark btree nodes for rewrites, if they are or would be degraded,
if journal replay hasn't finished, to avoid a deadlock.

This is because btree node rewrites generate more updates for the
interior updates (alloc, backpointers), and if those updates touch
new nodes and generate more rewrites - we can only have so many interior
btree updates in flight before we deadlock on open_buckets.

The biggest cause is that we don't use the btree write buffer (for
the backpointer updates - this needs some real thought on locking in
order to fix.

The problem with this workaround (not doing the rewrite for degraded
nodes in journal replay) is that those degraded nodes persist, and we
don't want that (this is a real bug when a btree node write completes
with fewer replicas than we wanted and leaves a degraded node due to
device _removal_, i.e. the device went away mid write).

It's less of a bug here, but still a problem because we don't yet
have a way of tracking degraded data - we another index (all
extents/btree nodes, by replicas entry) in order to fix properly
(re-replicate degraded data at the earliest possible time).

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/btree_io.c

index 08b22bddd7470f18a5c9567c76264cff826ffd6f..e874a4357f64c4a20fee88f2d810de1139050cf5 100644 (file)
@@ -1337,15 +1337,42 @@ int bch2_btree_node_read_done(struct bch_fs *c, struct bch_dev *ca,
 
        btree_node_reset_sib_u64s(b);
 
-       scoped_guard(rcu)
-               bkey_for_each_ptr(bch2_bkey_ptrs(bkey_i_to_s(&b->key)), ptr) {
-                       struct bch_dev *ca2 = bch2_dev_rcu(c, ptr->dev);
-
-                       if (!ca2 || ca2->mi.state != BCH_MEMBER_STATE_rw) {
-                               set_btree_node_need_rewrite(b);
-                               set_btree_node_need_rewrite_degraded(b);
+       /*
+        * XXX:
+        *
+        * We deadlock if too many btree updates require node rewrites while
+        * we're still in journal replay.
+        *
+        * This is because btree node rewrites generate more updates for the
+        * interior updates (alloc, backpointers), and if those updates touch
+        * new nodes and generate more rewrites - well, you see the problem.
+        *
+        * The biggest cause is that we don't use the btree write buffer (for
+        * the backpointer updates - this needs some real thought on locking in
+        * order to fix.
+        *
+        * The problem with this workaround (not doing the rewrite for degraded
+        * nodes in journal replay) is that those degraded nodes persist, and we
+        * don't want that (this is a real bug when a btree node write completes
+        * with fewer replicas than we wanted and leaves a degraded node due to
+        * device _removal_, i.e. the device went away mid write).
+        *
+        * It's less of a bug here, but still a problem because we don't yet
+        * have a way of tracking degraded data - we another index (all
+        * extents/btree nodes, by replicas entry) in order to fix properly
+        * (re-replicate degraded data at the earliest possible time).
+        */
+       if (c->recovery.passes_complete & BIT_ULL(BCH_RECOVERY_PASS_journal_replay)) {
+               scoped_guard(rcu)
+                       bkey_for_each_ptr(bch2_bkey_ptrs(bkey_i_to_s(&b->key)), ptr) {
+                               struct bch_dev *ca2 = bch2_dev_rcu(c, ptr->dev);
+
+                               if (!ca2 || ca2->mi.state != BCH_MEMBER_STATE_rw) {
+                                       set_btree_node_need_rewrite(b);
+                                       set_btree_node_need_rewrite_degraded(b);
+                               }
                        }
-               }
+       }
 
        if (!ptr_written) {
                set_btree_node_need_rewrite(b);