]> 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:47:26 +0000 (23:47 -0500)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 10 Nov 2026 04:47:26 +0000 (23:47 -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.

[zygo: 6.18 backport: on 6.18/topics/read-repair-fixes, whose head backports
 Qu Wenruo's 15ed4a938b30 (btrfs_repair_bbio_failure()); fs.h and disk-io.c
 differ in context only (compressed_write_workers, endio_freespace_worker).]

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 9fd848b323f2ad65fd445559f2c15f6e8d0310b3..c4855eb45ada08d3ee99d96c1fcb91e559da6ef6 100644 (file)
@@ -165,6 +165,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)
 {
@@ -186,22 +235,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);
 }
 
 /*
@@ -285,7 +329,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 c5abdd853afec2044c8c3c8a6bb029db267b2c4d..d8c8a2f9b6503992cb46a5c4beda831fa16ff61b 100644 (file)
@@ -1769,6 +1769,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);
        if (fs_info->compressed_write_workers)
                destroy_workqueue(fs_info->compressed_write_workers);
        btrfs_destroy_workqueue(fs_info->endio_write_workers);
@@ -1979,6 +1981,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);
@@ -2002,6 +2005,7 @@ static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info)
              fs_info->compressed_write_workers &&
              fs_info->endio_write_workers &&
              fs_info->endio_freespace_worker && 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 b03b983b7e35e7e3af2c696d1a7303f4680a09ed..1ae0c80f519e5b1ca3c71dde9032692fcbd05a1e 100644 (file)
@@ -656,6 +656,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 workqueue_struct *compressed_write_workers;
        struct btrfs_workqueue *endio_write_workers;
        struct btrfs_workqueue *endio_freespace_worker;