* [PATCH] btrfs: fix NULL end_io call when submitting a repair read fails
@ 2026-11-23 17:56 Miroslav Cimerman
2026-11-23 20:32 ` Qu Wenruo
0 siblings, 1 reply; 2+ messages in thread
From: Miroslav Cimerman @ 2026-11-23 17:56 UTC (permalink / raw)
To: linux-btrfs
Cc: David Sterba, Chris Mason, Josef Bacik, Johannes Thumshirn, linux-kernel
repair_one_sector() sets up the repair bio without an end_io callback.
Its completion does not need one. btrfs_check_read_bio() recognizes a
repair bio by its bio_set and hands it to btrfs_end_repair_bio().
btrfs_submit_chunk() can fail before the bio reaches a device, when
btrfs_map_block() or btrfs_lookup_bio_sums() returns an error. It then
ends the bio with btrfs_bio_end_io(), which calls bbio->end_io. For a
repair bio, that is a NULL pointer:
BUG: kernel NULL pointer dereference, address: 0000000000000000
Workqueue: btrfs-endio simple_end_io_work
RIP: 0010:0x0
Call Trace:
btrfs_submit_bbio+0xf1a/0x20f0
btrfs_check_read_bio+0x758/0x12e0
process_one_work+0x68d/0xf30
btrfs_bio_end_io(), btrfs_submit_chunk() and repair_one_sector() have
no frame of their own in the trace.
This can happen on the first submission in repair_one_sector() and on
the submission to the next mirror in btrfs_end_repair_bio(). On the raid
stripe tree, the mapping can fail for one mirror only.
btrfs_get_raid_extent_offset() returns -ENODATA when the stripe extent
does not list the device of that mirror.
Complete a repair bio that fails in btrfs_submit_chunk() like a failed
read of its mirror, from the end I/O workqueue. A read to a missing
device completes the same way. The repair code then tries the next
mirror, or fails the read when no mirror is left.
btrfs_end_repair_bio() needs the mirror number and the saved iterator.
A submission that fails in btrfs_map_block() sets neither, so set both.
Each retry runs as a new work item, so the retries do not nest.
A mirror whose submission failed counts as a bad mirror. When a later
mirror returns good data, btrfs_repair_bbio_failure() runs for that
mirror too. On a zoned file system, it starts a relocation of the block
group. Otherwise, it writes the good data to that mirror. This write
does not use the raid stripe tree, so it does not add a missing stripe.
After a transient error like -ENOMEM, it rewrites a good mirror.
Fixes: 7609afac6775 ("btrfs: handle checksum validation and repair at the storage layer")
CC: stable@vger.kernel.org # 6.6+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
Found with FuzzyRAID, a fault-injection tester for RAID that is not
public at the moment: a repair read whose mapping failed, on zoned
RAID1.
The tests use a test-only injection that fails the mapping of repair
reads in btrfs_submit_chunk(). The file systems had bad blocks on the
data of one device, and reads went to that device. Without this patch,
the first failed repair read crashed the kernel in 5 of 5 runs: 3 on
RAID1C3, 1 on RAID10 and 1 on RAID6.
With this patch and the mapping failing for the first mirror each repair
read tries, RAID1C3 and RAID6 read all data from another mirror, in
2 of 2 runs each. RAID1 has no third copy, and its reads returned -EIO
(1 run). With the mapping failing for every mirror, RAID1C3 and RAID6
returned -EIO too (1 run each). None of the 13 runs with this patch and
the injection switched on crashed or hung. 4 of them also had Josef's
patch below. Without the injection, both kernels read all data from
another copy. The btrfs_lookup_bio_sums() failure path is not tested.
RAID10 needs "btrfs: don't drop the error of the last split bio" from
Josef's data integrity series for the -EIO. Without it, a read that
spans both mirror pairs can return wrong data without an error. In
the 2 RAID10 runs with this patch alone, sha1sum read 7 of 8 files
without an error and got wrong data. That also happens without this
patch when both copies are bad: in 2 of 2 runs, 522241 and 522210 of the
1048576 bytes of a file came back wrong. With both patches, the RAID10
reads returned -EIO in both runs, one with the first mirror failing and
one with every mirror failing.
https://lore.kernel.org/linux-btrfs/20261120-b4-btrfs-data-integrity-v3-1-4de4b91b826e@toxicpanda.com/
Kernels before 6.19 need a backport: simple_end_io_work() is
btrfs_end_bio_work() there, and 6.6 and 6.12 have no end_bbio label.
Backports can follow if needed.
The analysis, the fix and the tests were done with the help of an LLM. I
reviewed everything.
fs/btrfs/bio.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/fs/btrfs/bio.c b/fs/btrfs/bio.c
index efbce4d549ff..46df424e9667 100644
--- a/fs/btrfs/bio.c
+++ b/fs/btrfs/bio.c
@@ -840,6 +840,19 @@ static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num)
btrfs_bio_end_io(remaining, status);
}
end_bbio:
+ /*
+ * A repair bio has no end_io callback. Complete it like a failed read
+ * of its mirror, from the end I/O workqueue. The repair code then tries
+ * the next mirror. A direct call would nest once per mirror.
+ */
+ if (bbio->bio.bi_pool == &btrfs_repair_bioset) {
+ bbio->mirror_num = mirror_num;
+ bbio->saved_iter = bbio->bio.bi_iter;
+ bbio->bio.bi_status = status;
+ INIT_WORK(&bbio->end_io_work, simple_end_io_work);
+ queue_work(btrfs_end_io_wq(fs_info, bio), &bbio->end_io_work);
+ return true;
+ }
btrfs_bio_end_io(bbio, status);
/* Do not submit another chunk */
return true;
base-commit: 65101fe10b3008186b813b628ee3a611b0e70ce6
--
2.53.0
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] btrfs: fix NULL end_io call when submitting a repair read fails
2026-11-23 17:56 [PATCH] btrfs: fix NULL end_io call when submitting a repair read fails Miroslav Cimerman
@ 2026-11-23 20:32 ` Qu Wenruo
0 siblings, 0 replies; 2+ messages in thread
From: Qu Wenruo @ 2026-11-23 20:32 UTC (permalink / raw)
To: Miroslav Cimerman, linux-btrfs
Cc: David Sterba, Chris Mason, Josef Bacik, Johannes Thumshirn, linux-kernel
在 2026/11/24 04:26, Miroslav Cimerman 写道:
> repair_one_sector() sets up the repair bio without an end_io callback.
> Its completion does not need one. btrfs_check_read_bio() recognizes a
> repair bio by its bio_set and hands it to btrfs_end_repair_bio().
>
> btrfs_submit_chunk() can fail before the bio reaches a device, when
> btrfs_map_block() or btrfs_lookup_bio_sums() returns an error. It then
> ends the bio with btrfs_bio_end_io(), which calls bbio->end_io. For a
> repair bio, that is a NULL pointer:
>
> BUG: kernel NULL pointer dereference, address: 0000000000000000
> Workqueue: btrfs-endio simple_end_io_work
> RIP: 0010:0x0
> Call Trace:
> btrfs_submit_bbio+0xf1a/0x20f0
> btrfs_check_read_bio+0x758/0x12e0
> process_one_work+0x68d/0xf30
>
> btrfs_bio_end_io(), btrfs_submit_chunk() and repair_one_sector() have
> no frame of their own in the trace.
>
> This can happen on the first submission in repair_one_sector() and on
> the submission to the next mirror in btrfs_end_repair_bio(). On the raid
> stripe tree, the mapping can fail for one mirror only.
> btrfs_get_raid_extent_offset() returns -ENODATA when the stripe extent
> does not list the device of that mirror.
>
> Complete a repair bio that fails in btrfs_submit_chunk() like a failed
> read of its mirror, from the end I/O workqueue. A read to a missing
> device completes the same way. The repair code then tries the next
> mirror, or fails the read when no mirror is left.
> btrfs_end_repair_bio() needs the mirror number and the saved iterator.
> A submission that fails in btrfs_map_block() sets neither, so set both.
> Each retry runs as a new work item, so the retries do not nest.
>
> A mirror whose submission failed counts as a bad mirror. When a later
> mirror returns good data, btrfs_repair_bbio_failure() runs for that
> mirror too. On a zoned file system, it starts a relocation of the block
> group. Otherwise, it writes the good data to that mirror. This write
> does not use the raid stripe tree, so it does not add a missing stripe.
> After a transient error like -ENOMEM, it rewrites a good mirror.
>
> Fixes: 7609afac6775 ("btrfs: handle checksum validation and repair at the storage layer")
> CC: stable@vger.kernel.org # 6.6+
> Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
> ---
> Found with FuzzyRAID, a fault-injection tester for RAID that is not
> public at the moment: a repair read whose mapping failed, on zoned
> RAID1.
>
> The tests use a test-only injection that fails the mapping of repair
> reads in btrfs_submit_chunk(). The file systems had bad blocks on the
> data of one device, and reads went to that device. Without this patch,
> the first failed repair read crashed the kernel in 5 of 5 runs: 3 on
> RAID1C3, 1 on RAID10 and 1 on RAID6.
>
> With this patch and the mapping failing for the first mirror each repair
> read tries, RAID1C3 and RAID6 read all data from another mirror, in
> 2 of 2 runs each. RAID1 has no third copy, and its reads returned -EIO
> (1 run). With the mapping failing for every mirror, RAID1C3 and RAID6
> returned -EIO too (1 run each). None of the 13 runs with this patch and
> the injection switched on crashed or hung. 4 of them also had Josef's
> patch below. Without the injection, both kernels read all data from
> another copy. The btrfs_lookup_bio_sums() failure path is not tested.
>
> RAID10 needs "btrfs: don't drop the error of the last split bio" from
> Josef's data integrity series for the -EIO. Without it, a read that
> spans both mirror pairs can return wrong data without an error. In
> the 2 RAID10 runs with this patch alone, sha1sum read 7 of 8 files
> without an error and got wrong data. That also happens without this
> patch when both copies are bad: in 2 of 2 runs, 522241 and 522210 of the
> 1048576 bytes of a file came back wrong. With both patches, the RAID10
> reads returned -EIO in both runs, one with the first mirror failing and
> one with every mirror failing.
> https://lore.kernel.org/linux-btrfs/20261120-b4-btrfs-data-integrity-v3-1-4de4b91b826e@toxicpanda.com/
>
> Kernels before 6.19 need a backport: simple_end_io_work() is
> btrfs_end_bio_work() there, and 6.6 and 6.12 have no end_bbio label.
> Backports can follow if needed.
>
> The analysis, the fix and the tests were done with the help of an LLM. I
> reviewed everything.
>
> fs/btrfs/bio.c | 13 +++++++++++++
> 1 file changed, 13 insertions(+)
>
> diff --git a/fs/btrfs/bio.c b/fs/btrfs/bio.c
> index efbce4d549ff..46df424e9667 100644
> --- a/fs/btrfs/bio.c
> +++ b/fs/btrfs/bio.c
> @@ -840,6 +840,19 @@ static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num)
> btrfs_bio_end_io(remaining, status);
> }
> end_bbio:
> + /*
> + * A repair bio has no end_io callback. Complete it like a failed read
> + * of its mirror, from the end I/O workqueue. The repair code then tries
> + * the next mirror. A direct call would nest once per mirror.
> + */
> + if (bbio->bio.bi_pool == &btrfs_repair_bioset) {
> + bbio->mirror_num = mirror_num;
> + bbio->saved_iter = bbio->bio.bi_iter;
> + bbio->bio.bi_status = status;
> + INIT_WORK(&bbio->end_io_work, simple_end_io_work);
> + queue_work(btrfs_end_io_wq(fs_info, bio), &bbio->end_io_work);
This looks overkilled to me.
If the end_io is no-op, why not just define a no-op end_io for it?
> + return true;
> + }
> btrfs_bio_end_io(bbio, status);
> /* Do not submit another chunk */
> return true;
>
> base-commit: 65101fe10b3008186b813b628ee3a611b0e70ce6
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-23 20:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23 17:56 [PATCH] btrfs: fix NULL end_io call when submitting a repair read fails Miroslav Cimerman
2026-11-23 20:32 ` Qu Wenruo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®