All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ming Lin <mlin@kernel.org>
To: linux-kernel@vger.kernel.org
Cc: Christoph Hellwig <hch@lst.de>, Jens Axboe <axboe@kernel.dk>,
	Kent Overstreet <kent.overstreet@gmail.com>,
	Dongsu Park <dpark@posteo.net>, Mike Snitzer <snitzer@redhat.com>,
	Ming Lin <mlin@kernel.org>, Christoph Hellwig <hch@infradead.org>,
	Ming Lin <ming.l@ssi.samsung.com>
Subject: [PATCH v5 02/11] block: simplify bio_add_page()
Date: Mon,  6 Jul 2015 00:44:41 -0700	[thread overview]
Message-ID: <1436168690-32102-2-git-send-email-mlin@kernel.org> (raw)
In-Reply-To: <1436168690-32102-1-git-send-email-mlin@kernel.org>

From: Kent Overstreet <kent.overstreet@gmail.com>

Since generic_make_request() can now handle arbitrary size bios, all we
have to do is make sure the bvec array doesn't overflow.
__bio_add_page() doesn't need to call ->merge_bvec_fn(), where
we can get rid of unnecessary code paths.

Removing the call to ->merge_bvec_fn() is also fine, as no driver that
implements support for BLOCK_PC commands even has a ->merge_bvec_fn()
method.

Cc: Christoph Hellwig <hch@infradead.org>
Cc: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
[dpark: rebase and resolve merge conflicts, change a couple of comments,
 make bio_add_page() warn once upon a cloned bio.]
Signed-off-by: Dongsu Park <dpark@posteo.net>
Signed-off-by: Ming Lin <ming.l@ssi.samsung.com>
---
 block/bio.c | 135 +++++++++++++++++++++++++-----------------------------------
 1 file changed, 55 insertions(+), 80 deletions(-)

diff --git a/block/bio.c b/block/bio.c
index 2a00d34..da15e9a 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -714,9 +714,23 @@ int bio_get_nr_vecs(struct block_device *bdev)
 }
 EXPORT_SYMBOL(bio_get_nr_vecs);
 
-static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
-			  *page, unsigned int len, unsigned int offset,
-			  unsigned int max_sectors)
+/**
+ *	bio_add_pc_page	-	attempt to add page to bio
+ *	@q: the target queue
+ *	@bio: destination bio
+ *	@page: page to add
+ *	@len: vec entry length
+ *	@offset: vec entry offset
+ *
+ *	Attempt to add a page to the bio_vec maplist. This can fail for a
+ *	number of reasons, such as the bio being full or target block device
+ *	limitations. The target block device must allow bio's up to PAGE_SIZE,
+ *	so it is always possible to add a single page to an empty bio.
+ *
+ *	This should only be used by REQ_PC bios.
+ */
+int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page
+		    *page, unsigned int len, unsigned int offset)
 {
 	int retried_segments = 0;
 	struct bio_vec *bvec;
@@ -727,7 +741,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	if (unlikely(bio_flagged(bio, BIO_CLONED)))
 		return 0;
 
-	if (((bio->bi_iter.bi_size + len) >> 9) > max_sectors)
+	if (((bio->bi_iter.bi_size + len) >> 9) > queue_max_hw_sectors(q))
 		return 0;
 
 	/*
@@ -740,28 +754,7 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 
 		if (page == prev->bv_page &&
 		    offset == prev->bv_offset + prev->bv_len) {
-			unsigned int prev_bv_len = prev->bv_len;
 			prev->bv_len += len;
-
-			if (q->merge_bvec_fn) {
-				struct bvec_merge_data bvm = {
-					/* prev_bvec is already charged in
-					   bi_size, discharge it in order to
-					   simulate merging updated prev_bvec
-					   as new bvec. */
-					.bi_bdev = bio->bi_bdev,
-					.bi_sector = bio->bi_iter.bi_sector,
-					.bi_size = bio->bi_iter.bi_size -
-						prev_bv_len,
-					.bi_rw = bio->bi_rw,
-				};
-
-				if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) {
-					prev->bv_len -= len;
-					return 0;
-				}
-			}
-
 			bio->bi_iter.bi_size += len;
 			goto done;
 		}
@@ -804,27 +797,6 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 		blk_recount_segments(q, bio);
 	}
 
-	/*
-	 * if queue has other restrictions (eg varying max sector size
-	 * depending on offset), it can specify a merge_bvec_fn in the
-	 * queue to get further control
-	 */
-	if (q->merge_bvec_fn) {
-		struct bvec_merge_data bvm = {
-			.bi_bdev = bio->bi_bdev,
-			.bi_sector = bio->bi_iter.bi_sector,
-			.bi_size = bio->bi_iter.bi_size - len,
-			.bi_rw = bio->bi_rw,
-		};
-
-		/*
-		 * merge_bvec_fn() returns number of bytes it can accept
-		 * at this offset
-		 */
-		if (q->merge_bvec_fn(q, &bvm, bvec) < bvec->bv_len)
-			goto failed;
-	}
-
 	/* If we may be able to merge these biovecs, force a recount */
 	if (bio->bi_vcnt > 1 && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec)))
 		bio->bi_flags &= ~(1 << BIO_SEG_VALID);
@@ -841,28 +813,6 @@ static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
 	blk_recount_segments(q, bio);
 	return 0;
 }
-
-/**
- *	bio_add_pc_page	-	attempt to add page to bio
- *	@q: the target queue
- *	@bio: destination bio
- *	@page: page to add
- *	@len: vec entry length
- *	@offset: vec entry offset
- *
- *	Attempt to add a page to the bio_vec maplist. This can fail for a
- *	number of reasons, such as the bio being full or target block device
- *	limitations. The target block device must allow bio's up to PAGE_SIZE,
- *	so it is always possible to add a single page to an empty bio.
- *
- *	This should only be used by REQ_PC bios.
- */
-int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page *page,
-		    unsigned int len, unsigned int offset)
-{
-	return __bio_add_page(q, bio, page, len, offset,
-			      queue_max_hw_sectors(q));
-}
 EXPORT_SYMBOL(bio_add_pc_page);
 
 /**
@@ -872,22 +822,47 @@ EXPORT_SYMBOL(bio_add_pc_page);
  *	@len: vec entry length
  *	@offset: vec entry offset
  *
- *	Attempt to add a page to the bio_vec maplist. This can fail for a
- *	number of reasons, such as the bio being full or target block device
- *	limitations. The target block device must allow bio's up to PAGE_SIZE,
- *	so it is always possible to add a single page to an empty bio.
+ *	Attempt to add a page to the bio_vec maplist. This will only fail
+ *	if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio.
  */
-int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
-		 unsigned int offset)
+int bio_add_page(struct bio *bio, struct page *page,
+		 unsigned int len, unsigned int offset)
 {
-	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
-	unsigned int max_sectors;
+	struct bio_vec *bv;
 
-	max_sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector);
-	if ((max_sectors < (len >> 9)) && !bio->bi_iter.bi_size)
-		max_sectors = len >> 9;
+	/*
+	 * cloned bio must not modify vec list
+	 */
+	if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
+		return 0;
 
-	return __bio_add_page(q, bio, page, len, offset, max_sectors);
+	/*
+	 * For filesystems with a blocksize smaller than the pagesize
+	 * we will often be called with the same page as last time and
+	 * a consecutive offset.  Optimize this special case.
+	 */
+	if (bio->bi_vcnt > 0) {
+		bv = &bio->bi_io_vec[bio->bi_vcnt - 1];
+
+		if (page == bv->bv_page &&
+		    offset == bv->bv_offset + bv->bv_len) {
+			bv->bv_len += len;
+			goto done;
+		}
+	}
+
+	if (bio->bi_vcnt >= bio->bi_max_vecs)
+		return 0;
+
+	bv		= &bio->bi_io_vec[bio->bi_vcnt];
+	bv->bv_page	= page;
+	bv->bv_len	= len;
+	bv->bv_offset	= offset;
+
+	bio->bi_vcnt++;
+done:
+	bio->bi_iter.bi_size += len;
+	return len;
 }
 EXPORT_SYMBOL(bio_add_page);
 
-- 
1.9.1


  reply	other threads:[~2015-07-06  7:46 UTC|newest]

Thread overview: 103+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-06  7:44 [PATCH v5 01/11] block: make generic_make_request handle arbitrarily sized bios Ming Lin
2015-07-06  7:44 ` Ming Lin [this message]
2015-07-06  7:44 ` [PATCH v5 03/11] bcache: remove driver private bio splitting code Ming Lin
2015-07-06  7:44 ` [PATCH v5 04/11] btrfs: remove bio splitting and merge_bvec_fn() calls Ming Lin
2015-07-06  7:44 ` [PATCH v5 05/11] block: remove split code in blkdev_issue_discard Ming Lin
2015-07-06  7:44 ` [PATCH v5 06/11] md/raid5: split bio for chunk_aligned_read Ming Lin
2015-07-06  7:44 ` [PATCH v5 07/11] md/raid5: get rid of bio_fits_rdev() Ming Lin
2015-07-06  7:44 ` [PATCH v5 08/11] block: kill merge_bvec_fn() completely Ming Lin
2015-07-06  7:44 ` [PATCH v5 09/11] fs: use helper bio_add_page() instead of open coding on bi_io_vec Ming Lin
2015-07-06  7:44 ` [PATCH v5 10/11] block: remove bio_get_nr_vecs() Ming Lin
2015-07-06 10:58   ` Steven Whitehouse
2015-07-06 17:21     ` Ming Lin
2015-07-07  9:04       ` Steven Whitehouse
2015-07-06  7:44 ` [PATCH v5 11/11] Documentation: update notes in biovecs about arbitrarily sized bios Ming Lin
2015-07-31 19:23 ` [PATCH v5 01/11] block: make generic_make_request handle " Mike Snitzer
2015-07-31 21:19   ` Ming Lin
2015-07-31 21:19     ` Ming Lin
2015-07-31 21:38     ` Mike Snitzer
2015-07-31 21:38       ` Mike Snitzer
2015-07-31 22:02       ` Ming Lin
2015-07-31 22:18         ` Ming Lin
2015-08-01  6:58       ` Ming Lin
2015-08-01  6:58         ` Ming Lin
2015-08-01 16:33         ` Mike Snitzer
2015-08-03  5:58           ` Ming Lin
2015-08-03  5:58             ` Ming Lin
2015-08-04 11:36             ` Christoph Hellwig
2015-08-05  6:03               ` Ming Lin
2015-08-05  6:03                 ` Ming Lin
2015-08-07  7:30                 ` Christoph Hellwig
2015-08-07  7:30                   ` Christoph Hellwig
2015-08-07 23:40                   ` Ming Lin
2015-08-07 23:40                     ` Ming Lin
2015-08-08  0:30                     ` Kent Overstreet
2015-08-08  5:17                       ` Ming Lin
2015-08-08  5:17                         ` Ming Lin
2015-08-08  5:22                         ` Kent Overstreet
2015-08-08 12:35                           ` Christoph Hellwig
2015-08-08 12:35                             ` Christoph Hellwig
2015-08-08  8:52                     ` [dm-devel] " Hannes Reinecke
2015-08-08  9:02                       ` Kent Overstreet
2015-08-13  6:04                         ` Hannes Reinecke
2015-08-07  0:00               ` Kent Overstreet
2015-08-07  7:30                 ` Christoph Hellwig
2015-08-07  7:30                   ` Christoph Hellwig
2015-08-08 16:19           ` [dm-devel] " Martin K. Petersen
2015-08-08 16:19             ` Martin K. Petersen
2015-08-09  5:59             ` Ming Lin
2015-08-09  5:59               ` Ming Lin
2015-08-09  6:41               ` Christoph Hellwig
2015-08-09  6:41                 ` Christoph Hellwig
2015-08-09  6:55                 ` Ming Lin
2015-08-09  6:55                   ` Ming Lin
2015-08-09  7:01                   ` Christoph Hellwig
2015-08-09  7:01                     ` Christoph Hellwig
2015-08-09  7:18                     ` Ming Lin
2015-08-09  7:18                       ` Ming Lin
2015-08-10 15:02                       ` Mike Snitzer
2015-08-10 15:02                         ` Mike Snitzer
2015-08-10 16:14                         ` Ming Lin
2015-08-10 16:14                           ` Ming Lin
2015-08-10 16:18                           ` Ming Lin
2015-08-10 16:18                             ` Ming Lin
2015-08-10 16:40                           ` Martin K. Petersen
2015-08-10 16:40                             ` Martin K. Petersen
2015-08-10 18:13                           ` Mike Snitzer
2015-08-10 22:30                             ` Ming Lin
2015-08-10 22:30                               ` Ming Lin
2015-08-10 16:22                         ` Martin K. Petersen
2015-08-10 16:22                           ` Martin K. Petersen
2015-08-10 18:18                           ` Ming Lin
2015-08-11  2:00                             ` Martin K. Petersen
2015-08-11  2:00                               ` Martin K. Petersen
2015-08-11  2:41                               ` Mike Snitzer
2015-08-11  2:41                                 ` Mike Snitzer
2015-08-11  3:38                                 ` Kent Overstreet
2015-08-11 14:08                                   ` Mike Snitzer
2015-08-11 14:08                                     ` Mike Snitzer
2015-08-11 17:49                                   ` Martin K. Petersen
2015-08-11 17:49                                     ` Martin K. Petersen
2015-08-11 18:05                                     ` Martin K. Petersen
2015-08-11 18:05                                       ` Martin K. Petersen
2015-08-11 20:56                                       ` Ming Lin
2015-08-11 20:56                                         ` Ming Lin
2015-08-12  0:24                                         ` Martin K. Petersen
2015-08-12  0:24                                           ` Martin K. Petersen
2015-08-12  4:41                                           ` Ming Lin
2015-08-12  4:41                                             ` Ming Lin
2015-08-11 17:36                                 ` Martin K. Petersen
2015-08-11 17:36                                   ` Martin K. Petersen
2015-08-11 17:47                                   ` Mike Snitzer
2015-08-11 17:47                                     ` Mike Snitzer
2015-08-11 18:01                                     ` [dm-devel] " Martin K. Petersen
2015-08-11 18:01                                       ` Martin K. Petersen
2015-08-18  5:09                         ` Ming Lin
2015-08-18  7:04                           ` Ming Lin
2015-08-18 14:45                             ` Mike Snitzer
2015-08-18 17:32                               ` Ming Lin
2015-08-18 19:59                                 ` Mike Snitzer
2015-08-18 21:16                                   ` Ming Lin
2015-08-18 21:22                                     ` Mike Snitzer
2015-08-18 22:17                                       ` Ming Lin
  -- strict thread matches above, loose matches on Subject: below --
2015-07-06  7:11 [PATCH v5 00/11] simplify block layer based on immutable biovecs mlin
2015-07-06  7:11 ` [PATCH v5 02/11] block: simplify bio_add_page() mlin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1436168690-32102-2-git-send-email-mlin@kernel.org \
    --to=mlin@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=dpark@posteo.net \
    --cc=hch@infradead.org \
    --cc=hch@lst.de \
    --cc=kent.overstreet@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ming.l@ssi.samsung.com \
    --cc=snitzer@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.