Skip to contentSkip to conversation

block: Make __bio_clone_fast() copy bi_vcnt

PATCH In review Bart Van Assche 6 participants 15 messages Jun 27, 2018 – Jun 30, 2018 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: Make __bio_clone_fast() copy bi_vcnt

Although __bio_clone_fast() copies bi_io_vec, it does not copy bi_vcnt, the number of elements in bi_io_vec[] that contains data. Copy bi_vcnt such that code that needs this member behaves identically for original and for cloned requests.

Cc Christoph Hellwig <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Ming Lei <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
---
 block/bio.c | 1 +
 1 file changed, 1 insertion(+)
block/bio.c +1 -0
index f7e3d88bd0b6..55f8e0dedd69 100644
@@ -605,6 +605,7 @@ void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
 	bio->bi_opf = bio_src->bi_opf;
 	bio->bi_write_hint = bio_src->bi_write_hint;
 	bio->bi_iter = bio_src->bi_iter;
+	bio->bi_vcnt = bio_src->bi_vcnt;
 	bio->bi_io_vec = bio_src->bi_io_vec;
 
 	bio_clone_blkcg_association(bio, bio_src);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 20 lines · Bart Van Assche

Although __bio_clone_fast() copies bi_io_vec, it does not copy bi_vcnt, the number of elements in bi_io_vec[] that contains data. Copy bi_vcnt such that code that needs this member behaves identically for original and for cloned requests.

Cc Christoph Hellwig <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Ming Lei <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
---
 block/bio.c | 1 +
 1 file changed, 1 insertion(+)
block/bio.c +0 -0
index f7e3d88bd0b6..55f8e0dedd69 100644

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

Quoted 5 lines
block/bio.c +1 -0
@@ -605,6 +605,7 @@ void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
 	bio->bi_opf = bio_src->bi_opf;
 	bio->bi_write_hint = bio_src->bi_write_hint;
 	bio->bi_iter = bio_src->bi_iter;
+	bio->bi_vcnt = bio_src->bi_vcnt;
 	bio->bi_io_vec = bio_src->bi_io_vec;

No, don't do that.

For a cloned bio, both .bi_vcnt and .bi_io_vec can't be used directly, and we have done huge such cleanup for supporting multipage bvec, that is the great idea of immutable bvecs invented by Kent.

Thanks,
Ming

#2 lore raw
Quoted 16 lines · Ming Lei
Quoted 15 lines · Bart Van Assche

Although __bio_clone_fast() copies bi_io_vec, it does not copy bi_vcnt, the number of elements in bi_io_vec[] that contains data. Copy bi_vcnt such that code that needs this member behaves identically for original and for cloned requests.

Cc Christoph Hellwig <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Ming Lei <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
---
  block/bio.c | 1 +
  1 file changed, 1 insertion(+)
Quoted 12 lines

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

Quoted 10 lines
block/bio.c +1 -0
index f7e3d88bd0b6..55f8e0dedd69 100644
@@ -605,6 +605,7 @@ void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
  	bio->bi_opf = bio_src->bi_opf;
  	bio->bi_write_hint = bio_src->bi_write_hint;
  	bio->bi_iter = bio_src->bi_iter;
+	bio->bi_vcnt = bio_src->bi_vcnt;
  	bio->bi_io_vec = bio_src->bi_io_vec;

No, don't do that.

Why not? I think it's a huge booby trap that cloned bio's have a bi_io_vec but zero bi_vcnt.

Quoted 3 lines

For a cloned bio, both .bi_vcnt and .bi_io_vec can't be used directly, and we have done huge such cleanup for supporting multipage bvec, that is the great idea of immutable bvecs invented by Kent.

My understanding is that the above change doesn't conflict at all with the immutable biovec work -- to the contrary.

Bart.

#3 lore raw
Quoted 30 lines · Bart Van Assche
Quoted 16 lines · Ming Lei
Quoted 15 lines · Bart Van Assche

Although __bio_clone_fast() copies bi_io_vec, it does not copy bi_vcnt, the number of elements in bi_io_vec[] that contains data. Copy bi_vcnt such that code that needs this member behaves identically for original and for cloned requests.

Cc Christoph Hellwig <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Ming Lei <[email protected]>
Cc Hannes Reinecke <[email protected]>
Cc Johannes Thumshirn <[email protected]>
---
  block/bio.c | 1 +
  1 file changed, 1 insertion(+)
Quoted 12 lines

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

Quoted 10 lines
block/bio.c +1 -0
index f7e3d88bd0b6..55f8e0dedd69 100644
@@ -605,6 +605,7 @@ void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
  	bio->bi_opf = bio_src->bi_opf;
  	bio->bi_write_hint = bio_src->bi_write_hint;
  	bio->bi_iter = bio_src->bi_iter;
+	bio->bi_vcnt = bio_src->bi_vcnt;
  	bio->bi_io_vec = bio_src->bi_io_vec;

No, don't do that.

Quoted 2 lines

Why not? I think it's a huge booby trap that cloned bio's have a bi_io_vec but zero bi_vcnt.

One core idea of immutable bvec is to use bio->bi_iter and the original bvec table to iterate over anywhere in the bio. That is why .bi_io_vec needs to copy, but not see any reason why .bi_vcnt needs to do.

Do you have use cases on .bi_vcnt for cloned bio?

Thanks,
Ming

#4 lore raw
Quoted 5 lines · Ming Lei

One core idea of immutable bvec is to use bio->bi_iter and the original bvec table to iterate over anywhere in the bio. That is why .bi_io_vec needs to copy, but not see any reason why .bi_vcnt needs to do.

Do you have use cases on .bi_vcnt for cloned bio?

So far this is only a theoretical concern. There are many functions in the block layer that use .bi_vcnt, and it is a lot of work to figure out all the callers of all these functions.

Bart.

Acked-by
#5 lore raw
Quoted 10 lines · Bart Van Assche
Quoted 5 lines · Ming Lei

One core idea of immutable bvec is to use bio->bi_iter and the original bvec table to iterate over anywhere in the bio. That is why .bi_io_vec needs to copy, but not see any reason why .bi_vcnt needs to do.

Do you have use cases on .bi_vcnt for cloned bio?

So far this is only a theoretical concern. There are many functions in the block layer that use .bi_vcnt, and it is a lot of work to figure out all the callers of all these functions.

No point wasting time with that. I don't understand why Ming cares. Your change is obviously correct. The state should get transfered over to reflect reality.

This patch doesn't harm anything, it just prevents some clone-specific failure in the future.

#6 lore raw
In reply to Bart Van Assche
Quoted 6 lines · Bart Van Assche
Quoted 5 lines · Ming Lei

One core idea of immutable bvec is to use bio->bi_iter and the original bvec table to iterate over anywhere in the bio. That is why .bi_io_vec needs to copy, but not see any reason why .bi_vcnt needs to do.

Quoted 6 lines
Quoted 1 line

Do you have use cases on .bi_vcnt for cloned bio?

So far this is only a theoretical concern. There are many functions in the block layer that use .bi_vcnt, and it is a lot of work to figure out all the callers of all these functions.

No, any functions using .bi_vcnt on a cloned-bio may be a bug, and we should take a close look.

Thanks,
Ming Lei

#7 lore raw
Quoted 8 lines · Ming Lei
Quoted 6 lines · Bart Van Assche
Quoted 5 lines · Ming Lei

One core idea of immutable bvec is to use bio->bi_iter and the original bvec table to iterate over anywhere in the bio. That is why .bi_io_vec needs to copy, but not see any reason why .bi_vcnt needs to do.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

Do you have use cases on .bi_vcnt for cloned bio?

So far this is only a theoretical concern. There are many functions in the block layer that use .bi_vcnt, and it is a lot of work to figure out all the callers of all these functions.

Back when I implemented immutable biovecs I thoroughly audited all the bi_vcnt uses and removed all of them that weren't by the code that owns/submits the bio.

Grepping around I see one or two suspicious uses.. blk-merge.c in particular

Quoted 2 lines

No, any functions using .bi_vcnt on a cloned-bio may be a bug, and we should take a close look.

not just cloned bios, any code using bi_vcnt on a bio it didn't create is wrong.

so big nack to this patch (I wasn't ccd on it though and it doesn't seem to have hit lkml, so I can't find the original patch...)

#8 lore raw
Quoted 20 lines · Kent Overstreet
Quoted 8 lines · Ming Lei
Quoted 6 lines · Bart Van Assche
Quoted 5 lines · Ming Lei

One core idea of immutable bvec is to use bio->bi_iter and the original bvec table to iterate over anywhere in the bio. That is why .bi_io_vec needs to copy, but not see any reason why .bi_vcnt needs to do.

Quoted 5 lines
Quoted 5 lines
Quoted 1 line

Do you have use cases on .bi_vcnt for cloned bio?

So far this is only a theoretical concern. There are many functions in the block layer that use .bi_vcnt, and it is a lot of work to figure out all the callers of all these functions.

Back when I implemented immutable biovecs I thoroughly audited all the bi_vcnt uses and removed all of them that weren't by the code that owns/submits the bio.

Grepping around I see one or two suspicious uses.. blk-merge.c in particular

Quoted 7 lines
Quoted 2 lines

No, any functions using .bi_vcnt on a cloned-bio may be a bug, and we should take a close look.

not just cloned bios, any code using bi_vcnt on a bio it didn't create is wrong.

so big nack to this patch (I wasn't ccd on it though and it doesn't seem to have hit lkml, so I can't find the original patch...)

Hello Kent,

Thanks for chiming in. The linux-block mailing list is archived by multiple websites. The entire e-mail thread is available on e.g. https://www.mail-archive.com/[email protected]/msg23006.html.

I have a question for you: at least in kernel v4.17 bio_clone_bioset() copies bi_vcnt from the source to the destination bio. However, __bio_clone_fast() doesn't copy bi_vcnt. Isn't that an inconsistency?

Thanks,

Bart.

#9 lore raw
Quoted 31 lines · Bart Van Assche
Quoted 20 lines · Kent Overstreet
Quoted 8 lines · Ming Lei
Quoted 6 lines · Bart Van Assche
Quoted 5 lines · Ming Lei

One core idea of immutable bvec is to use bio->bi_iter and the original bvec table to iterate over anywhere in the bio. That is why .bi_io_vec needs to copy, but not see any reason why .bi_vcnt needs to do.

Quoted 5 lines
Quoted 5 lines
Quoted 1 line

Do you have use cases on .bi_vcnt for cloned bio?

So far this is only a theoretical concern. There are many functions in the block layer that use .bi_vcnt, and it is a lot of work to figure out all the callers of all these functions.

Back when I implemented immutable biovecs I thoroughly audited all the bi_vcnt uses and removed all of them that weren't by the code that owns/submits the bio.

Grepping around I see one or two suspicious uses.. blk-merge.c in particular

Quoted 7 lines
Quoted 2 lines

No, any functions using .bi_vcnt on a cloned-bio may be a bug, and we should take a close look.

not just cloned bios, any code using bi_vcnt on a bio it didn't create is wrong.

so big nack to this patch (I wasn't ccd on it though and it doesn't seem to have hit lkml, so I can't find the original patch...)

Hello Kent,

Quoted 7 lines

Thanks for chiming in. The linux-block mailing list is archived by multiple websites. The entire e-mail thread is available on e.g.
https://www.mail-archive.com/[email protected]/msg23006.html.

I have a question for you: at least in kernel v4.17 bio_clone_bioset() copies bi_vcnt from the source to the destination bio. However, __bio_clone_fast() doesn't copy bi_vcnt. Isn't that an inconsistency?

No - when you use bio_clone_bioset() you get a bio that you own and can do whatever you want with, so it does make sense for it to initialize bi_vcnt.

e.g. you could use bio_clone_bioset() when you're going to be bouncing a bio, iterating over each bvec and allocating a new page and copying data from the old page to the new page.

#10 lore raw
Quoted 9 lines · Kent Overstreet
Quoted 7 lines · Bart Van Assche

Thanks for chiming in. The linux-block mailing list is archived by multiple websites. The entire e-mail thread is available on e.g.
https://www.mail-archive.com/[email protected]/msg23006.html.

I have a question for you: at least in kernel v4.17 bio_clone_bioset() copies bi_vcnt from the source to the destination bio. However, __bio_clone_fast() doesn't copy bi_vcnt. Isn't that an inconsistency?

Quoted 6 lines

No - when you use bio_clone_bioset() you get a bio that you own and can do whatever you want with, so it does make sense for it to initialize bi_vcnt.

e.g. you could use bio_clone_bioset() when you're going to be bouncing a bio, iterating over each bvec and allocating a new page and copying data from the old page to the new page.

Hello Kent,

Have you considered to explain this in a comment above __bio_clone_fast() to avoid that the next person who reads that function gets confused?

Bart.

#11 lore raw
Quoted 13 lines · Bart Van Assche
Quoted 12 lines · Kent Overstreet
Quoted 7 lines · Bart Van Assche

Thanks for chiming in. The linux-block mailing list is archived by multiple websites. The entire e-mail thread is available on e.g.
https://www.mail-archive.com/[email protected]/msg23006.html.

I have a question for you: at least in kernel v4.17 bio_clone_bioset() copies bi_vcnt from the source to the destination bio. However, __bio_clone_fast() doesn't copy bi_vcnt. Isn't that an inconsistency?

No - when you use bio_clone_bioset() you get a bio that you own and can do whatever you want with, so it does make sense for it to initialize bi_vcnt.

Quoted 8 lines
Quoted 3 lines

e.g. you could use bio_clone_bioset() when you're going to be bouncing a bio, iterating over each bvec and allocating a new page and copying data from the old page to the new page.

Hello Kent,

Have you considered to explain this in a comment above __bio_clone_fast() to avoid that the next person who reads that function gets confused?

Well, there is a large comment in bio_clone_bioset() that you must have found...

maintainer
#12 lore raw
In reply to Kent Overstreet
Quoted 21 lines · Kent Overstreet
Quoted 8 lines · Ming Lei
Quoted 6 lines · Bart Van Assche
Quoted 5 lines · Ming Lei

One core idea of immutable bvec is to use bio->bi_iter and the original bvec table to iterate over anywhere in the bio. That is why .bi_io_vec needs to copy, but not see any reason why .bi_vcnt needs to do.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

Do you have use cases on .bi_vcnt for cloned bio?

So far this is only a theoretical concern. There are many functions in the block layer that use .bi_vcnt, and it is a lot of work to figure out all the callers of all these functions.

Back when I implemented immutable biovecs I thoroughly audited all the bi_vcnt uses and removed all of them that weren't by the code that owns/submits the bio.

Grepping around I see one or two suspicious uses.. blk-merge.c in particular

Quoted 7 lines
Quoted 2 lines

No, any functions using .bi_vcnt on a cloned-bio may be a bug, and we should take a close look.

not just cloned bios, any code using bi_vcnt on a bio it didn't create is wrong.

so big nack to this patch (I wasn't ccd on it though and it doesn't seem to have hit lkml, so I can't find the original patch...)

Yeah, you are both right, I was smoking crack.

Signature · 1 line
Jens Axboe
maintainer
#13 lore raw
In reply to Bart Van Assche
Quoted 4 lines · Bart Van Assche

Although __bio_clone_fast() copies bi_io_vec, it does not copy bi_vcnt, the number of elements in bi_io_vec[] that contains data. Copy bi_vcnt such that code that needs this member behaves identically for original and for cloned requests.

Applied - it's correct for the current base.

Signature · 1 line
Jens Axboe
#14 lore raw
Quoted 8 lines · Jens Axboe
Quoted 4 lines · Bart Van Assche

Although __bio_clone_fast() copies bi_io_vec, it does not copy bi_vcnt, the number of elements in bi_io_vec[] that contains data. Copy bi_vcnt such that code that needs this member behaves identically for original and for cloned requests.

Applied - it's correct for the current base.

Any users of cloned bio shouldn't use this bio's .bi_vcnt directly, since this counter represents the original bio's actual io vector number, nothing related with the cloned bio.

So I don't understand why we need to copy it.

Thanks,
Ming