From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf0-f181.google.com ([209.85.192.181]:35116 "EHLO mail-pf0-f181.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1424403AbcBREYX (ORCPT ); Wed, 17 Feb 2016 23:24:23 -0500 Date: Wed, 17 Feb 2016 19:24:18 -0900 From: Kent Overstreet To: Ming Lei Cc: Sagi Grimberg , Jens Axboe , linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Christoph Hellwig , stable@vger.kernel.org, tom.leiming@gmail.com, Keith Busch Subject: Re: [PATCH 1/4] block: bio: introduce helpers to get the 1st and last bvec Message-ID: <20160218042418.GA27042@kmo-pixel> References: <1455519687-23873-1-git-send-email-ming.lei@canonical.com> <1455519687-23873-2-git-send-email-ming.lei@canonical.com> <56C18A25.50803@dev.mellanox.co.il> <20160215174212.648098b0@tom-T450> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20160215174212.648098b0@tom-T450> Sender: stable-owner@vger.kernel.org List-ID: On Mon, Feb 15, 2016 at 05:42:12PM +0800, Ming Lei wrote: > Cc Kent and Keith. > > Follows another version which should be more efficient. > Kent and Keith, I appreciate much if you may give a review on it. > > diff --git a/include/linux/bio.h b/include/linux/bio.h > index 56d2db8..ef45fec 100644 > --- a/include/linux/bio.h > +++ b/include/linux/bio.h > @@ -278,11 +278,21 @@ static inline void bio_get_first_bvec(struct bio *bio, struct bio_vec *bv) > */ > static inline void bio_get_last_bvec(struct bio *bio, struct bio_vec *bv) > { > - struct bvec_iter iter; > + struct bvec_iter iter = bio->bi_iter; > + int idx; > + > + bio_advance_iter(bio, &iter, iter.bi_size); > + > + WARN_ON(!iter.bi_idx && !iter.bi_bvec_done); > + > + if (!iter.bi_bvec_done) > + idx = iter.bi_idx - 1; > + else /* in the middle of bvec */ > + idx = iter.bi_idx; > > - bio_for_each_segment(*bv, bio, iter) > - if (bv->bv_len == iter.bi_size) > - break; > + *bv = bio->bi_io_vec[idx]; > + if (iter.bi_bvec_done) > + bv->bv_len = iter.bi_bvec_done; > } It can't be done correctly without a loop. The reason is that if the bio was split in the middle of a segment, bv->bv_len on the last biovec will be larger than what's actually used by the bio (it's being shared between the two splits!). You have to iterate over all the biovecs so that you can see where bi_iter->bi_size ends.