]> git.hungrycats.org Git - linux/commit
btrfs: do not hold the bio counter across the data csum lookup misc-next/topics/replace-finish-deadlock
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 9 Nov 2026 01:40:09 +0000 (20:40 -0500)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 10 Nov 2026 04:44:16 +0000 (23:44 -0500)
commit0c648f0b012b529ce47aaf8c0700051b8f492252
tree831cdd140f79ce16800cfafd641b24a37f20a24a
parent0545110141e28b6526e0da88bb59070174e47ae4
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.

Fixes: 1c2b3ee3b0ec4 ("btrfs: pre-load data checksum for reads in btrfs_submit_bio")
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/bio.c