Skip to contentSkip to conversation

btrfs: fix NULL end_io call when submitting a repair read fails

PATCH In review Miroslav Cimerman 3 participants 4 messages Nov 23 – Nov 24 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: fix NULL end_io call when submitting a repair read fails

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")

---
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(+)
fs/btrfs/bio.c +13 -0
index efbce4d549ff..46df424e9667 100644
@@ -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

Signature · 1 line
2.53.0
#1 lore raw
Quoted 94 lines · 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")

---
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(+)
fs/btrfs/bio.c +1 -0
index efbce4d549ff..46df424e9667 100644
@@ -840,6 +840,19 @@ static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num)
  		btrfs_bio_end_io(remaining, status);
  	}
  end_bbio:
+	/*

fs/btrfs/bio.c static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num) · line 853

Quoted 10 lines
fs/btrfs/bio.c +10 -0
+	 * 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?

Quoted 7 lines
fs/btrfs/bio.c +2 -0
+		return true;
+	}
  	btrfs_bio_end_io(bbio, status);
  	/* Do not submit another chunk */
  	return true;

base-commit: 65101fe10b3008186b813b628ee3a611b0e70ce6

#2 lore raw
Quoted 3 lines · Qu Wenruo

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

#3 lore raw
In reply to Miroslav Cimerman
Quoted 11 lines · Miroslav Cimerman
fs/btrfs/bio.c +3 -0
index efbce4d549ff..46df424e9667 100644
@@ -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

fs/btrfs/bio.c static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num) · line 855

Quoted 10 lines
fs/btrfs/bio.c +10 -0
+	 * 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?