Skip to contentSkip to conversation

bio: ensure __bio_clone_fast copies bi_partno

PATCH In review Michael Lyle 8 participants 10 messages Nov 17, 2017 – Nov 21, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bio: ensure __bio_clone_fast copies bi_partno

A new field was introduced in 74d46992e0d9dee7f1f376de0d56d31614c8a17a, bi_partno, instead of using bdev->bd_contains and encoding the partition information in the bi_bdev field. __bio_clone_fast was changed to copy the disk information, but not the partition information. At minimum, this regressed bcache and caused data corruption.

Fixes 74d46992e0d9dee7f1f376de0d56d31614c8a17a
Cc Christoph Hellwig <[email protected]>
Cc Jens Axboe <[email protected]>
---
 block/bio.c | 1 +
 1 file changed, 1 insertion(+)
block/bio.c +1 -0
index 101c2a9b5481..33fa6b4af312 100644
@@ -597,6 +597,7 @@ void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
 	 * so we don't set nor calculate new physical/hw segment counts here
 	 */
 	bio->bi_disk = bio_src->bi_disk;
+	bio->bi_partno = bio_src->bi_partno;
 	bio_set_flag(bio, BIO_CLONED);
 	bio->bi_opf = bio_src->bi_opf;
 	bio->bi_write_hint = bio_src->bi_write_hint;
Signature · 1 line
2.14.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Michael Lyle

A new field was introduced in 74d46992e0d9dee7f1f376de0d56d31614c8a17a, bi_partno, instead of using bdev->bd_contains and encoding the partition information in the bi_bdev field. __bio_clone_fast was changed to copy the disk information, but not the partition information. At minimum, this regressed bcache and caused data corruption.

Hi Michael,

Thanks for the fix, it looks good to me.

Quoted 4 lines
Fixes 74d46992e0d9dee7f1f376de0d56d31614c8a17a

Coly Li

Quoted 20 lines
Cc Christoph Hellwig <[email protected]>
Cc Jens Axboe <[email protected]>
---
 block/bio.c | 1 +
 1 file changed, 1 insertion(+)
block/bio.c +1 -0
index 101c2a9b5481..33fa6b4af312 100644
@@ -597,6 +597,7 @@ void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
 	 * so we don't set nor calculate new physical/hw segment counts here
 	 */
 	bio->bi_disk = bio_src->bi_disk;
+	bio->bi_partno = bio_src->bi_partno;
 	bio_set_flag(bio, BIO_CLONED);
 	bio->bi_opf = bio_src->bi_opf;
 	bio->bi_write_hint = bio_src->bi_write_hint;
Reviewed-by on v1 1/1
#2 lore raw
In reply to Michael Lyle
Quoted 22 lines · Michael Lyle

A new field was introduced in 74d46992e0d9dee7f1f376de0d56d31614c8a17a, bi_partno, instead of using bdev->bd_contains and encoding the partition information in the bi_bdev field. __bio_clone_fast was changed to copy the disk information, but not the partition information. At minimum, this regressed bcache and caused data corruption.

Fixes 74d46992e0d9dee7f1f376de0d56d31614c8a17a
Cc Christoph Hellwig <[email protected]>
Cc Jens Axboe <[email protected]>
---
 block/bio.c | 1 +
 1 file changed, 1 insertion(+)
block/bio.c +0 -0
index 101c2a9b5481..33fa6b4af312 100644

block/bio.c void __bio_clone_fast(struct bio *bio, struct bio *bio_src) · line 603

Quoted 7 lines
block/bio.c +1 -0
@@ -597,6 +597,7 @@ void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
 	 * so we don't set nor calculate new physical/hw segment counts here
 	 */
 	bio->bi_disk = bio_src->bi_disk;
+	bio->bi_partno = bio_src->bi_partno;
 	bio_set_flag(bio, BIO_CLONED);
 	bio->bi_opf = bio_src->bi_opf;
 	bio->bi_write_hint = bio_src->bi_write_hint;
Signature · 1 line
Ming
maintainer
#4 lore raw
In reply to Michael Lyle
Quoted 5 lines · Michael Lyle

A new field was introduced in 74d46992e0d9dee7f1f376de0d56d31614c8a17a, bi_partno, instead of using bdev->bd_contains and encoding the partition information in the bi_bdev field. __bio_clone_fast was changed to copy the disk information, but not the partition information. At minimum, this regressed bcache and caused data corruption.

That's not good... Fix looks good to me, I'll queue this up for a pull today. Thanks for bisecting this, Michael.

Signature · 1 line
Jens Axboe
#5 lore raw
In reply to Michael Lyle

block/bio.c void __bio_clone_fast(struct bio *bio, struct bio *bio_src) · line 603

Quoted 12 lines · Michael Lyle
block/bio.c +1 -0
index 101c2a9b5481..33fa6b4af312 100644
@@ -597,6 +597,7 @@ void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
 	 * so we don't set nor calculate new physical/hw segment counts here
 	 */
 	bio->bi_disk = bio_src->bi_disk;
+	bio->bi_partno = bio_src->bi_partno;
 	bio_set_flag(bio, BIO_CLONED);
 	bio->bi_opf = bio_src->bi_opf;
 	bio->bi_write_hint = bio_src->bi_write_hint;

Have you considered to use bio_copy_dev() instead of open-coding it?

Additionally, there is more code that copies these fields, e.g. the code in bio_clone_bioset(). Shouldn't that code be modified such that it also copies bi_partno?

How about the following class of assignments in drivers/md/raid1.c:

		mbio->bi_disk = (void *)conf->mirrors[i].rdev;

Should these assignments perhaps be followed by a mbio->bi_partno assignment?

How about the following class of assignments in the NVMe code:

               bio->bi_disk = disk;

Should these assignments perhaps be followed by a bio->bi_partno assignment?

Thanks,

Bart.

#6 lore raw

Jens & everyone-- thanks for the speedy review and handling. I've updated my test cases to ensure that volumes from old releases work, even when I "don't think" there's been a disk format change.

Bart--

Quoted 1 line · Bart Van Assche

Have you considered to use bio_copy_dev() instead of open-coding it?

One could... Right now almost all the uses of bio_copy_dev are in bcache and they need to change for other reasons. (e.g. macro uses parameter more than once, function is passed in as parameter). There's a whole lot of places to change if it's desired to make bio_copy_dev universally used to copy device information.

Quoted 3 lines

Additionally, there is more code that copies these fields, e.g. the code in bio_clone_bioset(). Shouldn't that code be modified such that it also copies bi_partno?

Yes, when I was grepping around there were other things that looked possibly unsafe. I don't have test environments for all of these other subsystems.

I wanted to get the minimal fix for this in, though, because people are actively losing data to the problem it triggers with bcache.

Mike

#7 lore raw
In reply to Bart Van Assche
Quoted 6 lines · Bart Van Assche

How about the following class of assignments in drivers/md/raid1.c:

		mbio->bi_disk = (void *)conf->mirrors[i].rdev;

Should these assignments perhaps be followed by a mbio->bi_partno assignment?

No. They assign a struct md_rdev to the bi_disk pointer, abusing it for internal storage. They should not assign mbio->bi_partno. Instead we should figure out a way to get rid of this.

Quoted 6 lines

How about the following class of assignments in the NVMe code:

               bio->bi_disk = disk;

Should these assignments perhaps be followed by a bio->bi_partno assignment?

No, the multipath code always works on whole namespaces, not partitions.

#8 lore raw
In reply to Michael Lyle
Quoted 5 lines · Michael Lyle

A new field was introduced in 74d46992e0d9dee7f1f376de0d56d31614c8a17a, bi_partno, instead of using bdev->bd_contains and encoding the partition information in the bi_bdev field. __bio_clone_fast was changed to copy the disk information, but not the partition information. At minimum, this regressed bcache and caused data corruption.

Thanks alot for this Michael, I will run it up on our test servers today.

Campbell

#9 lore raw
In reply to Michael Lyle

Hello Michael,

Quoted 5 lines · you

A new field was introduced in 74d46992e0d9dee7f1f376de0d56d31614c8a17a, bi_partno, instead of using bdev->bd_contains and encoding the partition information in the bi_bdev field. __bio_clone_fast was changed to copy the disk information, but not the partition information. At minimum, this regressed bcache and caused data corruption.

Thanks for the patch. It resolved the issue for me, I'm now running 4.14 without apparent problems.

Quoted 16 lines
Fixes 74d46992e0d9dee7f1f376de0d56d31614c8a17a
Cc Christoph Hellwig <[email protected]>
Cc Jens Axboe <[email protected]>
---
 block/bio.c | 1 +
 1 file changed, 1 insertion(+)
block/bio.c +0 -0
index 101c2a9b5481..33fa6b4af312 100644

block/bio.c void __bio_clone_fast(struct bio *bio, struct bio *bio_src) · line 599

Quoted 3 lines
block/bio.c +0 -0
@@ -597,6 +597,7 @@ void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
         * so we don't set nor calculate new physical/hw segment counts here
         */
        bio->bi_disk = bio_src->bi_disk;

+ bio->>bi_partno = bio_src->bi_partno;

block/bio.c void __bio_clone_fast(struct bio *bio, struct bio *bio_src) · line 603

Quoted 3 lines
block/bio.c +0 -0
        bio_set_flag(bio, BIO_CLONED);
        bio->bi_opf = bio_src->bi_opf;
        bio->bi_write_hint = bio_src->bi_write_hint;

Pavel Goran