Skip to contentSkip to conversation

block: make sure that bvec length can't be overflowed

PATCH Superseded Ming Lei 4 participants 5 messages Apr 16, 2019 – Apr 17, 2019 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 block: make sure that bvec length can't be overflowed

bvec->bv_offset may be bigger than PAGE_SIZE sometimes, such as, when one bio is splitted in the middle of one bvec via bio_split(), and bi_iter.bi_bvec_done is used to build offset of the 1st bvec of remained bio.

So we have to make sure that every bvec's offset is less than PAGE_SIZE from bio_for_each_segment().

This patch fixes this issue reported by Zhang Yi When running nvme/011.

Cc Christoph Hellwig <[email protected]>
Cc Yi Zhang <[email protected]>
Fixes 6dc4f100c175 ("block: allow bio_for_each_segment_all() to iterate over multi-page bvec")
---
 include/linux/bvec.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
include/linux/bvec.h +3 -2
index 3bc91879e1e2..f179b370066f 100644
@@ -160,8 +160,9 @@ static inline void bvec_advance(const struct bio_vec *bvec,
 		bv->bv_page = nth_page(bv->bv_page, 1);
 		bv->bv_offset = 0;
 	} else {
-		bv->bv_page = bvec->bv_page;
-		bv->bv_offset = bvec->bv_offset;
+		bv->bv_page = bvec_nth_page(bvec->bv_page, bvec->bv_offset /
+					    PAGE_SIZE);
+		bv->bv_offset = bvec->bv_offset % PAGE_SIZE;
 	}
 	bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset,
 			   bvec->bv_len - iter_all->done);
Signature · 1 line
2.9.5
Reviewed-by on v1 1/1
#1 lore raw

s/overflowed/overflow/ in the subject.

Otherwise this looks good to me:

Although this will create yet another conflict between Linus' tree and the 5.2 block tree.

Although maybe Jens still reset the tree and move the merge past this..

maintainer
#2 lore raw
Quoted 10 lines · Christoph Hellwig

s/overflowed/overflow/ in the subject.

Otherwise this looks good to me:

Although this will create yet another conflict between Linus' tree and the 5.2 block tree.

Although maybe Jens still reset the tree and move the merge past this..

That was my main worry too... But I'll probably just deal with it and redo the merge, I don't want to do two, and I don't want to have to deal with answering for why we get repeated conflicts in this area.

For the patch, I do generally prefer doing an AND with PAGE_MASK rather than a modulo with PAGE_SIZE. I know the compiler will take care of it, but still, it's more prudent imho.

Signature · 1 line
Jens Axboe
author
#3 lore raw
Quoted 7 lines · Jens Axboe
Quoted 6 lines · Christoph Hellwig

s/overflowed/overflow/ in the subject.

Otherwise this looks good to me:

Quoted 8 lines
Quoted 4 lines

Although this will create yet another conflict between Linus' tree and the 5.2 block tree.

Although maybe Jens still reset the tree and move the merge past this..

That was my main worry too... But I'll probably just deal with it and redo the merge, I don't want to do two, and I don't want to have to deal with answering for why we get repeated conflicts in this area.

This one is a fix which should affect on loop and nvme-loop only in theory.

That is typical conflict between fix and feature(improvement).

Anyway, sorry for the a bit late fix.

Quoted 4 lines

For the patch, I do generally prefer doing an AND with PAGE_MASK rather than a modulo with PAGE_SIZE. I know the compiler will take care of it, but still, it's more prudent imho.

OK, do it in V2.

Thanks,
Ming

Reviewed-by on v1 1/1
#4 lore raw
In reply to Ming Lei
Quoted 26 lines · Ming Lei

bvec->bv_offset may be bigger than PAGE_SIZE sometimes, such as, when one bio is splitted in the middle of one bvec via bio_split(), and bi_iter.bi_bvec_done is used to build offset of the 1st bvec of remained bio.

So we have to make sure that every bvec's offset is less than PAGE_SIZE from bio_for_each_segment().

This patch fixes this issue reported by Zhang Yi When running nvme/011.

Cc Christoph Hellwig <[email protected]>
Cc Yi Zhang <[email protected]>
Fixes 6dc4f100c175 ("block: allow bio_for_each_segment_all() to iterate over multi-page bvec")
---
  include/linux/bvec.h | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
include/linux/bvec.h +0 -0
index 3bc91879e1e2..f179b370066f 100644
@@ -160,8 +160,9 @@ static inline void bvec_advance(const struct bio_vec *bvec,
  		bv->bv_page = nth_page(bv->bv_page, 1);

include/linux/bvec.h static inline void bvec_advance(const struct bio_vec *bvec, · line 168

Quoted 10 lines
include/linux/bvec.h +3 -2
  		bv->bv_offset = 0;
  	} else {
-		bv->bv_page = bvec->bv_page;
-		bv->bv_offset = bvec->bv_offset;
+		bv->bv_page = bvec_nth_page(bvec->bv_page, bvec->bv_offset /
+					    PAGE_SIZE);
+		bv->bv_offset = bvec->bv_offset % PAGE_SIZE;
  	}
  	bv->bv_len = min_t(unsigned int, PAGE_SIZE - bv->bv_offset,
  			   bvec->bv_len - iter_all->done);

Looks okay.

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke            Teamlead Storage & Networking
[email protected]                              +49 911 74053 688
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Mary Higgins, Sri Rasiah
HRB 21284 (AG Nürnberg)