* [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
2026-11-24 6:37 ` Johannes Thumshirn
0 siblings, 2 replies; 5+ 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 related [flat|nested] 5+ 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
2026-11-23 23:17 ` Miroslav Cimerman
2026-11-24 6:37 ` Johannes Thumshirn
1 sibling, 1 reply; 5+ 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] 5+ messages in thread
* Re: [PATCH] btrfs: fix NULL end_io call when submitting a repair read fails
2026-11-23 20:32 ` Qu Wenruo
@ 2026-11-23 23:17 ` Miroslav Cimerman
0 siblings, 0 replies; 5+ messages in thread
From: Miroslav Cimerman @ 2026-11-23 23:17 UTC (permalink / raw)
To: Qu Wenruo
Cc: linux-btrfs, David Sterba, Chris Mason, Josef Bacik,
Johannes Thumshirn, linux-kernel
Qu Wenruo <wqu@suse.com> wrote:
> This looks overkilled to me.
>
> If the end_io is no-op, why not just define a no-op end_io for it?
A no-op end_io avoids the NULL call, but then btrfs_end_repair_bio()
never runs for that repair bio. The original read waits for it and never
finishes, and no other mirror is tried. I tried it with the same
test-only injection, and the read hung in 5 of 5 runs. A direct call to
btrfs_end_repair_bio() would nest once per mirror, and on RAID6 the
number of mirrors is the number of devices. So it is queued on the end
I/O workqueue.
Miroslav
^ permalink raw reply [flat|nested] 5+ 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
@ 2026-11-24 6:37 ` Johannes Thumshirn
2026-11-25 12:10 ` Miroslav Cimerman
1 sibling, 1 reply; 5+ messages in thread
From: Johannes Thumshirn @ 2026-11-24 6:37 UTC (permalink / raw)
To: Miroslav Cimerman, linux-btrfs
Cc: David Sterba, Chris Mason, Josef Bacik, linux-kernel
On 11/23/26 6:56 PM, Miroslav Cimerman wrote:
> 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;
> + }
Maybe I'm missing something (sorry wall of text in the commit message
and 7am here), but isn't it enough to set a dummy endio (like Qu also
suggested) and then bio_io_error() out?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] btrfs: fix NULL end_io call when submitting a repair read fails
2026-11-24 6:37 ` Johannes Thumshirn
@ 2026-11-25 12:10 ` Miroslav Cimerman
0 siblings, 0 replies; 5+ messages in thread
From: Miroslav Cimerman @ 2026-11-25 12:10 UTC (permalink / raw)
To: Johannes Thumshirn
Cc: linux-btrfs, David Sterba, Chris Mason, Josef Bacik, linux-kernel
Johannes Thumshirn <johannes.thumshirn@wdc.com> wrote:
> Maybe I'm missing something (sorry wall of text in the commit message
> and 7am here), but isn't it enough to set a dummy endio (like Qu also
> suggested) and then bio_io_error() out?
btrfs_end_repair_bio() has to run for the failed repair bio. It
decrements repair_count of the original read. Without that, the original
read never completes and hangs. It also tries the next mirror. Without
that, the read is not repaired from the other copies.
I tried a dummy end_io with bio_io_error(): the read hung in 3 of 3
runs, and no repair read went to another mirror after the failure.
Miroslav
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-25 12:10 UTC | newest]
Thread overview: 5+ 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
2026-11-23 23:17 ` Miroslav Cimerman
2026-11-24 6:37 ` Johannes Thumshirn
2026-11-25 12:10 ` Miroslav Cimerman
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.