md: use bio_clone_fast()
Subsystems (2)
cover md: use bio_clone_fast()
Hi,
This patches replaces bio_clone() with bio_fast_clone() in bio_clone_mddev() because:
1) bio_clone_mddev() is used in raid normal I/O and isn't in resync I/O path, and all the direct access to bvec table in raid happens on resync I/O only except for write behind of raid1. Write behind is treated specially, so the replacement is safe.
2) for write behind, bio_clone() is kept, but this patchset introduces bio_clone_bioset_partial() to just clone one specific bvecs range instead of whole table. Then write behind is improved too.
V1:
1) don't introduce bio_clone_slow_mddev_partial()
2) return failure if mddev->bio_set can't be created
3) remove check in bio_clone_mddev() as suggested by Christoph Hellwig.
4) rename bio_clone_mddev() as bio_clone_fast_mddev()
Ming Lei (5):
block: introduce bio_clone_bioset_partial()
md/raid1: use bio_clone_bioset_partial() in case of write behind
md: fail if mddev->bio_set can't be created
md: remove unnecessary check on mddev
md: fast clone bio in bio_clone_mddev()
block/bio.c | 61 +++++++++++++++++++++++++++++++++++++++++------------ drivers/md/faulty.c | 2 +- drivers/md/md.c | 14 ++++++------ drivers/md/md.h | 4 ++-- drivers/md/raid1.c | 26 ++++++++++++++++------- drivers/md/raid10.c | 11 +++++----- drivers/md/raid5.c | 4 ++-- include/linux/bio.h | 11 ++++++++-- 8 files changed, 92 insertions(+), 41 deletions(-)
Signature · 4 lines
2.7.4 Thanks, Ming
PATCH 1/5 block: introduce bio_clone_bioset_partial()
md still need bio clone(not the fast version) for behind write, and it is more efficient to use bio_clone_bioset_partial().
The idea is simple and just copy the bvecs range specified from parameters.
--- block/bio.c | 61 +++++++++++++++++++++++++++++++++++++++++------------ include/linux/bio.h | 11 ++++++++-- 2 files changed, 57 insertions(+), 15 deletions(-)
block/bio.c +48 -13
@@ -625,21 +625,20 @@ struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs) }EXPORT_SYMBOL (bio_clone_fast); -/** -* bio_clone_bioset - clone a bio -* @bio_src: bio to clone -* @gfp_mask: allocation priority -* @bs: bio_set to allocate from -* -* Clone bio. Caller will own the returned bio, but not the actual data it -* points to. Reference count of returned bio will be one. -*/ -struct bio *bio_clone_bioset (struct bio *bio_src,gfp_t gfp_mask, -struct bio_set *bs) +static struct bio *__bio_clone_bioset (struct bio *bio_src,gfp_t gfp_mask, +struct bio_set *bs,int offset, +int size) {struct bvec_iter iter;struct bio_vec bv;struct bio *bio; +struct bvec_iter iter_src = bio_src->bi_iter; + +/* for supporting partial clone */ +if (offset || size != bio_src->bi_iter.bi_size) { +bio_advance_iter (bio_src, &iter_src, offset); + iter_src.bi_size = size; + }/* * Pre immutable biovecs, __bio_clone() used to just do a memcpy from
@@ -663,7 +662,8 @@ struct bio *bio_clone_bioset(struct bio *bio_src, gfp_t gfp_mask,* __bio_clone_fast() anyways. */ - bio =bio_alloc_bioset (gfp_mask,bio_segments (bio_src), bs); + bio =bio_alloc_bioset (gfp_mask,__bio_segments (bio_src, + &iter_src), bs);if (!bio)return NULL ; bio->bi_bdev = bio_src->bi_bdev;
@@ -680,7 +680,7 @@ struct bio *bio_clone_bioset(struct bio *bio_src, gfp_t gfp_mask, bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0 ];break ;default : -bio_for_each_segment (bv, bio_src, iter) +__bio_for_each_segment (bv, bio_src, iter, iter_src) bio->bi_io_vec[bio->bi_vcnt++] = bv;break ; }
@@ -699,9 +699,44 @@ struct bio *bio_clone_bioset(struct bio *bio_src, gfp_t gfp_mask,return bio; } + +/** +* bio_clone_bioset - clone a bio +* @bio_src: bio to clone +* @gfp_mask: allocation priority +* @bs: bio_set to allocate from +* +* Clone bio. Caller will own the returned bio, but not the actual data it +* points to. Reference count of returned bio will be one. +*/ +struct bio *bio_clone_bioset (struct bio *bio_src,gfp_t gfp_mask, +struct bio_set *bs) +{ +return __bio_clone_bioset (bio_src, gfp_mask, bs,0 , + bio_src->bi_iter.bi_size); +}EXPORT_SYMBOL (bio_clone_bioset);/** +* bio_clone_bioset_partial - clone a partial bio +* @bio_src: bio to clone +* @gfp_mask: allocation priority +* @bs: bio_set to allocate from +* @offset: cloned starting from the offset +* @size: size for the cloned bio +* +* Clone bio. Caller will own the returned bio, but not the actual data it +* points to. Reference count of returned bio will be one. +*/ +struct bio *bio_clone_bioset_partial (struct bio *bio_src,gfp_t gfp_mask, +struct bio_set *bs,int offset, +int size) +{ +return __bio_clone_bioset (bio_src, gfp_mask, bs, offset, size); +} +EXPORT_SYMBOL (bio_clone_bioset_partial); + +/** * bio_add_pc_page - attempt to add page to bio * @q: the target queue * @bio: destination bio
include/linux/bio.h +9 -2
@@ -183,7 +183,7 @@ static inline void bio_advance_iter(struct bio *bio, struct bvec_iter *iter,#define bio_iter_last (bvec, iter) ((iter).bi_size == (bvec).bv_len) -static inline unsigned bio_segments (struct bio *bio) +static inline unsigned __bio_segments (struct bio *bio,struct bvec_iter *bvec) {unsigned segs =0 ;struct bio_vec bv;
@@ -205,12 +205,17 @@ static inline unsigned bio_segments(struct bio *bio)break ; } -bio_for_each_segment (bv, bio, iter) +__bio_for_each_segment (bv, bio, iter, *bvec) segs++;return segs; } +static inline unsigned bio_segments (struct bio *bio) +{ +return __bio_segments (bio, &bio->bi_iter); +} +/* * get a reference to a bio, so it won't disappear. the intended use is * something like:
@@ -384,6 +389,8 @@ extern void bio_put(struct bio *);extern void __bio_clone_fast (struct bio *,struct bio *);extern struct bio *bio_clone_fast (struct bio *,gfp_t ,struct bio_set *);extern struct bio *bio_clone_bioset (struct bio *,gfp_t ,struct bio_set *bs); +extern struct bio *bio_clone_bioset_partial (struct bio *,gfp_t , +struct bio_set *,int ,int );extern struct bio_set *fs_bio_set;
Signature · 1 line
2.7.4
Re: [PATCH v1 1/5] block: introduce bio_clone_bioset_partial()
Quoted 5 lines · Ming Lei
md still need bio clone(not the fast version) for behind write, and it is more efficient to use
bio_clone_bioset_partial().The idea is simple and just copy the bvecs range specified from parameters.
Given how few users bio_clone_bioset has I wonder if we shouldn't simply add the two new arguments to it instead of adding another indirection.
Otherwise looks fine:
Re: [PATCH v1 1/5] block: introduce bio_clone_bioset_partial()
Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Ming Lei
md still need bio clone(not the fast version) for behind write, and it is more efficient to use
bio_clone_bioset_partial().The idea is simple and just copy the bvecs range specified from parameters.
Given how few users
bio_clone_biosethas I wonder if we shouldn't simply add the two new arguments to it instead of adding another indirection.
For md write-behind, looks we have to provide the two arguments, could you explain a bit how we can do that by adding another indirection?
Quoted 4 lines
Otherwise looks fine:
Reviewed-by Christoph Hellwig <[email protected]>
Thanks!
Signature · 1 line
Ming Lei
Re: [PATCH v1 1/5] block: introduce bio_clone_bioset_partial()
Quoted 8 lines · Ming Lei
Quoted 7 lines · Christoph Hellwig
Quoted 5 lines · Ming Lei
md still need bio clone(not the fast version) for behind write, and it is more efficient to use
bio_clone_bioset_partial().The idea is simple and just copy the bvecs range specified from parameters.
Quoted 6 lines
Quoted 3 lines
Given how few users
bio_clone_biosethas I wonder if we shouldn't simply add the two new arguments to it instead of adding another indirection.For md write-behind, looks we have to provide the two arguments, could you explain a bit how we can do that by adding another indirection?
I meant to just pass the additional arguments that bio_clone_bioset_partial has to bio_clone_bioset.
Re: [PATCH v1 1/5] block: introduce bio_clone_bioset_partial()
Quoted 13 lines · Christoph Hellwig
Quoted 12 lines · Ming Lei
Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Ming Lei
md still need bio clone(not the fast version) for behind write, and it is more efficient to use
bio_clone_bioset_partial().The idea is simple and just copy the bvecs range specified from parameters.
Given how few users
bio_clone_biosethas I wonder if we shouldn't simply add the two new arguments to it instead of adding another indirection.
Quoted 5 lines
Quoted 2 lines
For md write-behind, looks we have to provide the two arguments, could you explain a bit how we can do that by adding another indirection?
I meant to just pass the additional arguments that
bio_clone_bioset_partialhas tobio_clone_bioset.
That may cause more changes(fs, ...) into this patchset, so I suggest to do that in another patchset, especially after we confirmed current users of bio_clone is absolutely necessary, and I will check if other bio_clone can be converted to fast clone.
Thanks,
Ming Lei
PATCH 2/5 md/raid1: use bio_clone_bioset_partial() in case of write behind
In reply to Ming LeiWrite behind need to replace pages in bio's bvecs, and we have to clone a fresh bio with new bvec table, so use the introduced bio_clone_bioset_partial() for it.
For other bio_clone_mddev() cases, we will use fast clone since they don't need to touch bvec table.
--- drivers/md/raid1.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
drivers/md/raid1.c +15 -5
@@ -1341,13 +1341,12 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio, first_clone =1 ;for (i =0 ; i < disks; i++) { -struct bio *mbio; +struct bio *mbio =NULL ; +int offset;if (!r1_bio->bios[i])continue ; - mbio =bio_clone_mddev (bio,GFP_NOIO , mddev); -bio_trim (mbio, r1_bio->sector - bio->bi_iter.bi_sector, - max_sectors); + offset = r1_bio->sector - bio->bi_iter.bi_sector;if (first_clone) {/* do behind I/O ?
@@ -1357,8 +1356,13 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,if (bitmap && (atomic_read (&bitmap->behind_writes) < mddev->bitmap_info.max_write_behind) && - !waitqueue_active (&bitmap->behind_wait)) + !waitqueue_active (&bitmap->behind_wait)) { + mbio =bio_clone_bioset_partial (bio,GFP_NOIO , + mddev->bio_set, + offset, + max_sectors);alloc_behind_pages (mbio, r1_bio); + } bitmap_startwrite(bitmap, r1_bio->sector, r1_bio->sectors,
@@ -1366,6 +1370,12 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio, &r1_bio->state)); first_clone =0 ; } + +if (!mbio) { + mbio =bio_clone_mddev (bio,GFP_NOIO , mddev); +bio_trim (mbio, offset, max_sectors); + } +if (r1_bio->behind_bvecs) {struct bio_vec *bvec;int j;
Signature · 1 line
2.7.4
Re: [PATCH v1 2/5] md/raid1: use bio_clone_bioset_partial() in case of write behind
drivers/md/raid1.c static void raid1_write_request(struct mddev *mddev, struct bio *bio, · line 1349
Quoted 9 lines
drivers/md/raid1.c +3
-3+struct bio *mbio =NULL ; +int offset;if (!r1_bio->bios[i])continue ; - mbio =bio_clone_mddev (bio,GFP_NOIO , mddev); -bio_trim (mbio, r1_bio->sector - bio->bi_iter.bi_sector, - max_sectors); + offset = r1_bio->sector - bio->bi_iter.bi_sector;
I think offset should be a sector_t.
Otherwise this looks fine:
PATCH 3/5 md: fail if mddev->bio_set can't be created
In reply to Ming LeiThe current behaviour is to fall back to allocate
bio from 'fs_bio_set', that isn't a correct way
because it might cause deadlock.
So this patch simply return failure if mddev->bio_set can't be created.
--- drivers/md/md.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/md/md.c +4 -1
@@ -5270,8 +5270,11 @@ int md_run(struct mddev *mddev)sysfs_notify_dirent_safe (rdev->sysfs_state); } -if (mddev->bio_set ==NULL ) +if (mddev->bio_set ==NULL ) { mddev->bio_set =bioset_create (BIO_POOL_SIZE ,0 ); +if (!mddev->bio_set) +return -ENOMEM ; + }spin_lock (&pers_lock); pers =find_pers (mddev->level, mddev->clevel);
Signature · 1 line
2.7.4
Re: [PATCH v1 3/5] md: fail if mddev->bio_set can't be created
Looks fine,
but this really needs to be patch 2 in this series.
PATCH 4/5 md: remove unnecessary check on mddev
In reply to Ming Leimddev is never NULL and neither is ->bio_set, so
remove the check.
--- drivers/md/md.c | 3 --- 1 file changed, 3 deletions(-)
drivers/md/md.c +0 -3
@@ -193,9 +193,6 @@ EXPORT_SYMBOL_GPL(bio_alloc_mddev);struct bio *bio_clone_mddev (struct bio *bio,gfp_t gfp_mask,struct mddev *mddev) { -if (!mddev || !mddev->bio_set) -return bio_clone (bio, gfp_mask); -return bio_clone_bioset (bio, gfp_mask, mddev->bio_set); }EXPORT_SYMBOL_GPL (bio_clone_mddev);
Signature · 1 line
2.7.4
Re: [PATCH v1 4/5] md: remove unnecessary check on mddev
Looks fine,
PATCH 5/5 md: fast clone bio in bio_clone_mddev()
In reply to Ming LeiFirstly bio_clone_mddev() is used in raid normal I/O and isn't in resync I/O path.
Secondly all the direct access to bvec table in raid happens on resync I/O except for write behind of raid1, in which we still use bio_clone() for allocating new bvec table.
So this patch replaces bio_clone() with bio_clone_fast() in bio_clone_mddev().
Rename bio_clone_mddev() as bio_clone_fast_mddev() too, as suggested by Christoph Hellwig.
--- drivers/md/faulty.c | 2 +- drivers/md/md.c | 6 +++--- drivers/md/md.h | 4 ++-- drivers/md/raid1.c | 8 ++++---- drivers/md/raid10.c | 11 +++++------ drivers/md/raid5.c | 4 ++-- 6 files changed, 17 insertions(+), 18 deletions(-)
drivers/md/faulty.c +1 -1
@@ -214,7 +214,7 @@ static void faulty_make_request(struct mddev *mddev, struct bio *bio) } }if (failit) { -struct bio *b =bio_clone_mddev (bio,GFP_NOIO , mddev); +struct bio *b =bio_clone_fast_mddev (bio,GFP_NOIO , mddev); b->bi_bdev = conf->rdev->bdev; b->bi_private = bio;
drivers/md/md.c +3 -3
@@ -190,12 +190,12 @@ struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, }EXPORT_SYMBOL_GPL (bio_alloc_mddev); -struct bio *bio_clone_mddev (struct bio *bio,gfp_t gfp_mask, +struct bio *bio_clone_fast_mddev (struct bio *bio,gfp_t gfp_mask,struct mddev *mddev) { -return bio_clone_bioset (bio, gfp_mask, mddev->bio_set); +return bio_clone_fast (bio, gfp_mask, mddev->bio_set); } -EXPORT_SYMBOL_GPL (bio_clone_mddev); +EXPORT_SYMBOL_GPL (bio_clone_fast_mddev);/* * We have a system wide 'event count' that is incremented
drivers/md/md.h +2 -2
@@ -673,8 +673,8 @@ extern void md_rdev_clear(struct md_rdev *rdev);extern void mddev_suspend (struct mddev *mddev);extern void mddev_resume (struct mddev *mddev); -extern struct bio *bio_clone_mddev (struct bio *bio,gfp_t gfp_mask, -struct mddev *mddev); +extern struct bio *bio_clone_fast_mddev (struct bio *bio,gfp_t gfp_mask, +struct mddev *mddev);extern struct bio *bio_alloc_mddev (gfp_t gfp_mask,int nr_iovecs,struct mddev *mddev);
drivers/md/raid1.c +4 -4
@@ -1108,7 +1108,7 @@ static void raid1_read_request(struct mddev *mddev, struct bio *bio, r1_bio->read_disk = rdisk; r1_bio->start_next_window =0 ; - read_bio =bio_clone_mddev (bio,GFP_NOIO , mddev); + read_bio =bio_clone_fast_mddev (bio,GFP_NOIO , mddev);bio_trim (read_bio, r1_bio->sector - bio->bi_iter.bi_sector, max_sectors);
@@ -1372,7 +1372,7 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio, }if (!mbio) { - mbio =bio_clone_mddev (bio,GFP_NOIO , mddev); + mbio =bio_clone_fast_mddev (bio,GFP_NOIO , mddev);bio_trim (mbio, offset, max_sectors); }
@@ -2283,7 +2283,7 @@ static int narrow_write_error(struct r1bio *r1_bio, int i) wbio->bi_vcnt = vcnt; }else { - wbio =bio_clone_mddev (r1_bio->master_bio,GFP_NOIO , mddev); + wbio =bio_clone_fast_mddev (r1_bio->master_bio,GFP_NOIO , mddev); }bio_set_op_attrs (wbio,REQ_OP_WRITE ,0 );
@@ -2421,7 +2421,7 @@ static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)const unsigned long do_sync = r1_bio->master_bio->bi_opf &REQ_SYNC ; r1_bio->read_disk = disk; - bio =bio_clone_mddev (r1_bio->master_bio,GFP_NOIO , mddev); + bio =bio_clone_fast_mddev (r1_bio->master_bio,GFP_NOIO , mddev);bio_trim (bio, r1_bio->sector - bio->bi_iter.bi_sector, max_sectors); r1_bio->bios[r1_bio->read_disk] = bio;
drivers/md/raid10.c +5 -6
@@ -1132,7 +1132,7 @@ static void raid10_read_request(struct mddev *mddev, struct bio *bio, } slot = r10_bio->read_slot; - read_bio =bio_clone_mddev (bio,GFP_NOIO , mddev); + read_bio =bio_clone_fast_mddev (bio,GFP_NOIO , mddev);bio_trim (read_bio, r10_bio->sector - bio->bi_iter.bi_sector, max_sectors);
@@ -1406,7 +1406,7 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,int d = r10_bio->devs[i].devnum;if (r10_bio->devs[i].bio) {struct md_rdev *rdev = conf->mirrors[d].rdev; - mbio =bio_clone_mddev (bio,GFP_NOIO , mddev); + mbio =bio_clone_fast_mddev (bio,GFP_NOIO , mddev);bio_trim (mbio, r10_bio->sector - bio->bi_iter.bi_sector, max_sectors); r10_bio->devs[i].bio = mbio;
@@ -1457,7 +1457,7 @@ static void raid10_write_request(struct mddev *mddev, struct bio *bio,smp_mb (); rdev = conf->mirrors[d].rdev; } - mbio =bio_clone_mddev (bio,GFP_NOIO , mddev); + mbio =bio_clone_fast_mddev (bio,GFP_NOIO , mddev);bio_trim (mbio, r10_bio->sector - bio->bi_iter.bi_sector, max_sectors); r10_bio->devs[i].repl_bio = mbio;
@@ -2565,7 +2565,7 @@ static int narrow_write_error(struct r10bio *r10_bio, int i)if (sectors > sect_to_write) sectors = sect_to_write;/* Write at 'sector' for 'sectors' */ - wbio =bio_clone_mddev (bio,GFP_NOIO , mddev); + wbio =bio_clone_fast_mddev (bio,GFP_NOIO , mddev);bio_trim (wbio, sector - bio->bi_iter.bi_sector, sectors); wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector); wbio->bi_iter.bi_sector = wsector +
@@ -2641,8 +2641,7 @@ static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)mdname (mddev),bdevname (rdev->bdev, b), (unsigned long long)r10_bio->sector); - bio =bio_clone_mddev (r10_bio->master_bio, -GFP_NOIO , mddev); + bio =bio_clone_fast_mddev (r10_bio->master_bio,GFP_NOIO , mddev);bio_trim (bio, r10_bio->sector - bio->bi_iter.bi_sector, max_sectors); r10_bio->devs[slot].bio = bio; r10_bio->devs[slot].rdev = rdev;
drivers/md/raid5.c +2 -2
@@ -5056,9 +5056,9 @@ static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)return 0 ; }/* -* use bio_clone_mddev to make a copy of the bio +* use bio_clone_fast_mddev to make a copy of the bio */ - align_bi =bio_clone_mddev (raid_bio,GFP_NOIO , mddev); + align_bi =bio_clone_fast_mddev (raid_bio,GFP_NOIO , mddev);if (!align_bi)return 0 ;/*
Signature · 1 line
2.7.4
Re: [PATCH v1 5/5] md: fast clone bio in bio_clone_mddev()
Please use bio_clone_fast directly and kill the wrapper.
Quoted 10 lines · Ming Lei
Hi,
This patches replaces
bio_clone()withbio_fast_clone()inbio_clone_mddev()because:1)
bio_clone_mddev()is used in raid normal I/O and isn't in resync I/O path, and all the direct access to bvec table in raid happens on resync I/O only except for write behind of raid1. Write behind is treated specially, so the replacement is safe.
Quoted 4 lines
2) for write behind,
bio_clone()is kept, but this patchset introducesbio_clone_bioset_partial()to just clone one specific bvecs range instead of whole table. Then write behind is improved too.
Thanks! this patch set looks good to me.
Jens,
can you look at the first patch? If it's ok, I'll carry it in my tree.
Thanks,
Shaohua
Quoted 30 lines
V1:
1) don't introducebio_clone_slow_mddev_partial()
2) return failure ifmddev->bio_setcan't be created
3) remove check inbio_clone_mddev()as suggested by Christoph Hellwig.
4) renamebio_clone_mddev()asbio_clone_fast_mddev()Ming Lei (5):
block: introducebio_clone_bioset_partial()
md/raid1: usebio_clone_bioset_partial()in case of write behind
md: fail ifmddev->bio_setcan't be created
md: remove unnecessary check on mddev
md: fast clone bio inbio_clone_mddev()block/bio.c | 61 +++++++++++++++++++++++++++++++++++++++++------------ drivers/md/faulty.c | 2 +- drivers/md/md.c | 14 ++++++------ drivers/md/md.h | 4 ++-- drivers/md/raid1.c | 26 ++++++++++++++++------- drivers/md/raid10.c | 11 +++++----- drivers/md/raid5.c | 4 ++-- include/linux/bio.h | 11 ++++++++-- 8 files changed, 92 insertions(+), 41 deletions(-)Signature · 4 lines
2.7.4 Thanks, Ming