bio: ensure __bio_clone_fast copies bi_partno
Subsystems (3)
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.
--- block/bio.c | 1 + 1 file changed, 1 insertion(+)
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
2.14.1
Quoted 6 lines · Michael Lyle
A new field was introduced in
74d46992e0d9dee7f1f376de0d56d31614c8a17a,bi_partno, instead of usingbdev->bd_containsand encoding the partition information in thebi_bdevfield.__bio_clone_fastwas 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
Signed-off-by Michael Lyle <[email protected]>Fixes 74d46992e0d9dee7f1f376de0d56d31614c8a17aReported-by Pavel Goran <[email protected]>Reported-by Campbell Steven <[email protected]>
Coly Li
Quoted 20 lines
--- block/bio.c | 1 + 1 file changed, 1 insertion(+)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;
Quoted 22 lines · Michael Lyle
A new field was introduced in
74d46992e0d9dee7f1f376de0d56d31614c8a17a,bi_partno, instead of usingbdev->bd_containsand encoding the partition information in thebi_bdevfield.__bio_clone_fastwas changed to copy the disk information, but not the partition information. At minimum, this regressed bcache and caused data corruption.Signed-off-by Michael Lyle <[email protected]>Fixes 74d46992e0d9dee7f1f376de0d56d31614c8a17aReported-by Pavel Goran <[email protected]>Reported-by Campbell Steven <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Jens Axboe <[email protected]>Cc <[email protected]>--- block/bio.c | 1 + 1 file changed, 1 insertion(+)block/bio.c +0
-0
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
Looks good,
Jens Axboe
maintainerQuoted 5 lines · Michael Lyle
A new field was introduced in
74d46992e0d9dee7f1f376de0d56d31614c8a17a,bi_partno, instead of usingbdev->bd_containsand encoding the partition information in thebi_bdevfield.__bio_clone_fastwas 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
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@@ -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.
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 copiesbi_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
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_partnoassignment?
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_partnoassignment?
No, the multipath code always works on whole namespaces, not partitions.
Quoted 5 lines · Michael Lyle
A new field was introduced in
74d46992e0d9dee7f1f376de0d56d31614c8a17a,bi_partno, instead of usingbdev->bd_containsand encoding the partition information in thebi_bdevfield.__bio_clone_fastwas 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
Hello Michael,
Quoted 5 lines · you
A new field was introduced in
74d46992e0d9dee7f1f376de0d56d31614c8a17a,bi_partno, instead of usingbdev->bd_containsand encoding the partition information in thebi_bdevfield.__bio_clone_fastwas 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
Signed-off-by Michael Lyle <[email protected]>Fixes 74d46992e0d9dee7f1f376de0d56d31614c8a17aReported-by Pavel Goran <[email protected]>Reported-by Campbell Steven <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Jens Axboe <[email protected]>Cc <[email protected]>--- block/bio.c | 1 + 1 file changed, 1 insertion(+)block/bio.c +0
-0
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
-0bio_set_flag (bio,BIO_CLONED ); bio->bi_opf = bio_src->bi_opf; bio->bi_write_hint = bio_src->bi_write_hint;
Pavel Goran