]> git.hungrycats.org Git - linux/commitdiff
btrfs: do not wait for device replace in the bio completion workers
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 10 Nov 2026 04:44:08 +0000 (23:44 -0500)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 10 Nov 2026 04:44:08 +0000 (23:44 -0500)
Device replace can hang forever at the end, after "replace status"
already reports it finished, when data that needs read repair is read
while it finishes:

  btrfs replace start   btrfs_rm_dev_replace_blocked <- btrfs_dev_replace_finishing
  btrfs-rmw (all 8)     btrfs_bio_counter_inc_blocked <- btrfs_repair_bbio_failure
                        <- btrfs_check_read_bio <- btrfs_raid56_end_io
                        <- rbio_orig_end_io <- recover_rbio

and every other task submitting bios to the filesystem blocks behind
them.

btrfs_dev_replace_finishing() sets BTRFS_FS_STATE_DEV_REPLACING and
waits for the bio counter to drop to zero.  Every bio that
btrfs_submit_chunk() counted keeps its reference until its completion
runs: a RAID56 bio until btrfs_raid56_end_io() runs in the btrfs-rmw
worker that completes its rbio, a mirrored write until
orig_write_end_io_work() runs in btrfs-endio.  Both workqueues run at
most thread_pool workers at a time.

Read repair runs in those same workers: btrfs_check_read_bio() is called
by simple_end_io_work() in btrfs-endio and by btrfs_raid56_end_io() in
btrfs-rmw.  It takes the bio counter when it submits a repair read to
the next mirror (repair_one_sector() and btrfs_end_repair_bio(), through
btrfs_submit_chunk()), and when it writes the good copy back
(btrfs_repair_bbio_failure()).  While replace finishing has the flag
set, both wait for replace to finish.  Once every worker of the pool
waits there, the counted bios queued behind them never complete, and
replace never finishes.

This is easy to hit when a missing device is replaced while its data is
read: each such read fails, is repaired by a rebuild read through
btrfs-rmw, and writes the rebuilt block back to the replace target.  On
a 4-device RAID5 with one device missing, 8 O_DIRECT readers and 2
writers, it hangs in about half the replaces.  At one hang the counter
was 15: 2 rebuild rbios queued on btrfs-rmw behind the 8 workers waiting
in the write-back, and 13 rebuild rbios waiting for the stripe locks
those 2 hold, one bio each.  On RAID6, a repair read that rebuilds a
sector from P and fails its csum goes on to rebuild from Q, and that
submission waits the same way: with the P strips corrupted, the same
test hung in the first replace, with all 8 btrfs-rmw workers in
btrfs_end_repair_bio() -> btrfs_submit_bbio() and a counter of 68 = 23
rebuild rbios queued on btrfs-rmw + 45 waiting for their stripe locks.

Run the parts of read repair that take the bio counter in a new
workqueue, btrfs-repair, that no bio completion waits for.  A worker
that waits there for replace to finish holds nothing replace waits for:
the repair bbio is not submitted yet, and the read being repaired is
held back by fbio->repair_count, which is not counted.

As before, the read being repaired completes once the write-back is
done; while replace finishes, that is once replace has finished.

Fixes: 7609afac67754 ("btrfs: handle checksum validation and repair at the storage layer")
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/bio.c
fs/btrfs/disk-io.c
fs/btrfs/fs.h

index 2c6234f6182f89e4215a48df51401f817e3d8d8c..2952022c73776a783390b5a89de4b182a7b7ff99 100644 (file)
@@ -164,6 +164,55 @@ static void btrfs_repair_done(struct btrfs_failed_bio *fbio)
        }
 }
 
+/*
+ * Read repair runs in bio completion context: btrfs_check_read_bio() is called
+ * by the btrfs-endio workers and, for RAID56, by the btrfs-rmw worker that
+ * completes the rbio.  The repair read and the write-back of the good copy both
+ * take the bio counter, which waits while device replace is finishing, and
+ * replace finishing waits for every counted bio to complete.  Counted bios
+ * complete in those same workers too (RAID56 bios in btrfs-rmw, mirrored writes
+ * in btrfs-endio), and both pools have only thread_pool workers: once all of a
+ * pool's workers wait for replace, the counted bios queued behind them never
+ * complete and replace never finishes.
+ *
+ * So the parts of read repair that take the bio counter run in
+ * fs_info->repair_workers, which no bio completion waits for.  A repair bbio
+ * is not in flight while it is queued there, so its end_io_work is free.
+ */
+static void btrfs_repair_read_work(struct work_struct *work)
+{
+       struct btrfs_bio *repair_bbio = container_of(work, struct btrfs_bio, end_io_work);
+
+       btrfs_submit_bbio(repair_bbio, repair_bbio->mirror_num);
+}
+
+static void btrfs_queue_repair_read(struct btrfs_bio *repair_bbio, int mirror)
+{
+       struct btrfs_fs_info *fs_info = repair_bbio->inode->root->fs_info;
+
+       repair_bbio->mirror_num = mirror;
+       INIT_WORK(&repair_bbio->end_io_work, btrfs_repair_read_work);
+       queue_work(fs_info->repair_workers, &repair_bbio->end_io_work);
+}
+
+/* Write the good copy in @repair_bbio back to the mirrors that failed before it. */
+static void btrfs_repair_write_work(struct work_struct *work)
+{
+       struct btrfs_bio *repair_bbio = container_of(work, struct btrfs_bio, end_io_work);
+       struct btrfs_failed_bio *fbio = repair_bbio->private;
+       struct btrfs_fs_info *fs_info = repair_bbio->inode->root->fs_info;
+       int mirror = repair_bbio->mirror_num;
+
+       do {
+               mirror = prev_repair_mirror(fbio, mirror);
+               btrfs_repair_bbio_failure(repair_bbio, &repair_bbio->saved_iter,
+                                         fs_info->sectorsize, mirror);
+       } while (mirror != fbio->bbio->mirror_num);
+
+       btrfs_repair_done(fbio);
+       bio_put(&repair_bbio->bio);
+}
+
 static void btrfs_end_repair_bio(struct btrfs_bio *repair_bbio,
                                 struct btrfs_device *dev)
 {
@@ -184,22 +233,17 @@ static void btrfs_end_repair_bio(struct btrfs_bio *repair_bbio,
                if (mirror == fbio->bbio->mirror_num) {
                        btrfs_debug(fs_info, "no mirror left");
                        fbio->bbio->bio.bi_status = BLK_STS_IOERR;
-                       goto done;
+                       btrfs_repair_done(fbio);
+                       bio_put(&repair_bbio->bio);
+                       return;
                }
 
-               btrfs_submit_bbio(repair_bbio, mirror);
+               btrfs_queue_repair_read(repair_bbio, mirror);
                return;
        }
 
-       do {
-               mirror = prev_repair_mirror(fbio, mirror);
-               btrfs_repair_bbio_failure(repair_bbio, &repair_bbio->saved_iter,
-                                         fs_info->sectorsize, mirror);
-       } while (mirror != fbio->bbio->mirror_num);
-
-done:
-       btrfs_repair_done(fbio);
-       bio_put(&repair_bbio->bio);
+       INIT_WORK(&repair_bbio->end_io_work, btrfs_repair_write_work);
+       queue_work(fs_info->repair_workers, &repair_bbio->end_io_work);
 }
 
 /*
@@ -268,7 +312,7 @@ static struct btrfs_failed_bio *repair_one_sector(struct btrfs_bio *failed_bbio,
 
        mirror = next_repair_mirror(fbio, failed_bbio->mirror_num);
        btrfs_debug(fs_info, "submitting repair read to mirror %d", mirror);
-       btrfs_submit_bbio(repair_bbio, mirror);
+       btrfs_queue_repair_read(repair_bbio, mirror);
        return fbio;
 }
 
index 6afcd6ca2a8a83f26aeb2279aaac0802b15afce9..59bd23092812c927262ab256c564c8c2bdb2efa7 100644 (file)
@@ -1779,6 +1779,8 @@ static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
                destroy_workqueue(fs_info->endio_workers);
        if (fs_info->rmw_workers)
                destroy_workqueue(fs_info->rmw_workers);
+       if (fs_info->repair_workers)
+               destroy_workqueue(fs_info->repair_workers);
        btrfs_destroy_workqueue(fs_info->endio_write_workers);
        btrfs_destroy_workqueue(fs_info->delayed_workers);
        btrfs_destroy_workqueue(fs_info->caching_workers);
@@ -2007,6 +2009,7 @@ static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info)
        fs_info->endio_meta_workers =
                alloc_workqueue("btrfs-endio-meta", flags, max_active);
        fs_info->rmw_workers = alloc_workqueue("btrfs-rmw", flags, max_active);
+       fs_info->repair_workers = alloc_workqueue("btrfs-repair", flags, max_active);
        fs_info->endio_write_workers =
                btrfs_alloc_workqueue(fs_info, "endio-write", flags,
                                      max_active, 2);
@@ -2023,6 +2026,7 @@ static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info)
              fs_info->delalloc_workers && fs_info->flush_workers &&
              fs_info->endio_workers && fs_info->endio_meta_workers &&
              fs_info->endio_write_workers && fs_info->rmw_workers &&
+             fs_info->repair_workers &&
              fs_info->caching_workers && fs_info->fixup_workers &&
              fs_info->delayed_workers && fs_info->qgroup_rescan_workers &&
              fs_info->discard_ctl.discard_workers)) {
index 2dd167c32d612f9ccdd836140288fe1e1206f27f..d728e19464866fdddb6e72f375aab02a3b601988 100644 (file)
@@ -710,6 +710,8 @@ struct btrfs_fs_info {
        struct workqueue_struct *endio_workers;
        struct workqueue_struct *endio_meta_workers;
        struct workqueue_struct *rmw_workers;
+       /* Read repair I/O, which must not wait in the bio completion workers. */
+       struct workqueue_struct *repair_workers;
        struct btrfs_workqueue *endio_write_workers;
        struct btrfs_workqueue *caching_workers;