From 80a40c767018796ca0e28fb6012071dd7f8c504d Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Sun, 8 Nov 2026 20:40:09 -0500 Subject: [PATCH] btrfs: do not hold the bio counter across the data csum lookup Device replace can hang forever at the end, after "replace status" already reports it finished, when data is read while it finishes. This happens with any RAID profile: btrfs replace start btrfs_rm_dev_replace_blocked <- btrfs_dev_replace_finishing reader btrfs_bio_counter_inc_blocked <- btrfs_submit_bbio <- read_extent_buffer_pages <- read_block_for_search <- btrfs_search_slot <- btrfs_lookup_csum <- btrfs_lookup_bio_sums <- btrfs_submit_bbio <- btrfs_dio_submit_io btrfs_submit_chunk() takes the bio counter before mapping the bio, and for a data read it then looks up the data checksums. When the csum tree leaf is not cached, the lookup reads it, and that read goes through btrfs_submit_chunk() and btrfs_bio_counter_inc_blocked() again. If btrfs_dev_replace_finishing() has set BTRFS_FS_STATE_DEV_REPLACING in between, the reader sleeps on the second reference while it holds the first, and replace finishing waits for the first to be dropped. Every other bio submission then blocks behind them. It was reproduced on a 4-device RAID5 with one device missing, mounted with thread_pool=64, 8 O_DIRECT readers and 2 writers: replacing the missing device hung in round 7. The counter was 2, and exactly two readers were sleeping in the nested btrfs_bio_counter_inc_blocked() under btrfs_lookup_bio_sums(). The counter only keeps the result of btrfs_map_block() valid until the bio is submitted. So drop it, and the mapping, for the csum lookup, take it again afterwards and map the bio again. That costs one more btrfs_map_block() call per data read bio. The mirror picked by the first mapping is passed back in, so the bio is mapped to the same copy. [zygo: 7.3 backport: clean pick on top of the previous patch.] Fixes: 1c2b3ee3b0ec4 ("btrfs: pre-load data checksum for reads in btrfs_submit_bio") Assisted-by: Claude:claude-opus-5-5 --- fs/btrfs/bio.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/bio.c b/fs/btrfs/bio.c index 1c94515d32d69..a5fd6d43adf68 100644 --- a/fs/btrfs/bio.c +++ b/fs/btrfs/bio.c @@ -820,6 +820,53 @@ static u64 btrfs_append_map_length(struct btrfs_bio *bbio, u64 map_length) return map_length; } +/* + * Look up the data checksums of a read without holding the bio counter, then + * map the bio again. + * + * The lookup can read csum tree blocks, and those reads go through + * btrfs_submit_chunk() and btrfs_bio_counter_inc_blocked() again. If device + * replace starts finishing in between, it waits for our counter reference to + * be dropped while we wait for it to finish, and every new bio blocks behind + * it. The counter only protects the mapping, so drop both for the lookup. + * + * Called and returns with the bio counter held. + */ +static int btrfs_lookup_sums_and_remap(struct btrfs_bio *bbio, + struct btrfs_io_context **bioc, + struct btrfs_io_stripe *smap, int *mirror_num) +{ + struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info; + struct bio *bio = &bbio->bio; + const u64 logical = bio->bi_iter.bi_sector << SECTOR_SHIFT; + const u64 length = bio->bi_iter.bi_size; + u64 map_length = length; + int ret; + + btrfs_put_bioc(*bioc); + *bioc = NULL; + btrfs_bio_counter_dec(fs_info); + + ret = btrfs_lookup_bio_sums(bbio); + + btrfs_bio_counter_inc_blocked(fs_info); + if (ret) + return ret; + + /* @mirror_num is the mirror the first mapping picked, so this maps the same copy. */ + ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, bioc, + smap, mirror_num); + if (ret) + return ret; + /* The chunk can not change under a read of it. */ + if (WARN_ON_ONCE(map_length < length)) { + btrfs_put_bioc(*bioc); + *bioc = NULL; + return -EUCLEAN; + } + return 0; +} + static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num) { struct btrfs_inode *inode = bbio->inode; @@ -880,7 +927,7 @@ static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num) */ if (bio_op(bio) == REQ_OP_READ && is_data_bbio(bbio)) { bbio->saved_iter = bio->bi_iter; - ret = btrfs_lookup_bio_sums(bbio); + ret = btrfs_lookup_sums_and_remap(bbio, &bioc, &smap, &mirror_num); status = errno_to_blk_status(ret); if (status) goto fail; -- 2.53.0