All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ming Lei <ming.lei@redhat.com>
To: Jens Axboe <axboe@kernel.dk>
Cc: linux-block@vger.kernel.org, Ming Lei <ming.lei@redhat.com>,
	Omar Sandoval <osandov@fb.com>, Christoph Hellwig <hch@lst.de>
Subject: [PATCH 4/6] block: put the same page when adding it to bio
Date: Sat,  9 Mar 2019 09:37:35 +0800	[thread overview]
Message-ID: <20190309013737.27741-5-ming.lei@redhat.com> (raw)
In-Reply-To: <20190309013737.27741-1-ming.lei@redhat.com>

When the added page is merged to last same page in bio_add_pc_page(),
the user may need to put this page for avoiding page leak.

bio_map_user_iov() needs this kind of handling, and now it deals with
it by itself in hack style.

Moves the handling of put page into __bio_add_pc_page(), so
bio_map_user_iov() may be simplified a bit, and maybe more users
can benefit from this change.

Cc: Omar Sandoval <osandov@fb.com>
Cc: Christoph Hellwig <hch@lst.de>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 block/bio.c         | 23 ++++++++++-------------
 include/linux/bio.h | 12 ++++++++++--
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/block/bio.c b/block/bio.c
index 62411877224c..95ec5e893265 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -666,12 +666,13 @@ page_is_mergeable(const struct bio_vec *bv, struct page *page,
 }
 
 /**
- *	bio_add_pc_page	-	attempt to add page to bio
+ *	__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
+ *	@put_same_page: put the page if it is same with last added page
  *
  *	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
@@ -680,8 +681,9 @@ page_is_mergeable(const struct bio_vec *bv, struct page *page,
  *
  *	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 __bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page
+		      *page, unsigned int len, unsigned int offset,
+		      bool put_same_page)
 {
 	int retried_segments = 0;
 	struct bio_vec *bvec;
@@ -705,6 +707,8 @@ int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page
 
 		if (page == prev->bv_page &&
 		    offset == prev->bv_offset + prev->bv_len) {
+			if (put_same_page)
+				put_page(page);
 			prev->bv_len += len;
 			bio->bi_iter.bi_size += len;
 			goto done;
@@ -763,7 +767,7 @@ int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page
 	blk_recount_segments(q, bio);
 	return 0;
 }
-EXPORT_SYMBOL(bio_add_pc_page);
+EXPORT_SYMBOL(__bio_add_pc_page);
 
 /**
  * __bio_try_merge_page - try appending data to an existing bvec.
@@ -1394,21 +1398,14 @@ struct bio *bio_map_user_iov(struct request_queue *q,
 			for (j = 0; j < npages; j++) {
 				struct page *page = pages[j];
 				unsigned int n = PAGE_SIZE - offs;
-				unsigned short prev_bi_vcnt = bio->bi_vcnt;
 
 				if (n > bytes)
 					n = bytes;
 
-				if (!bio_add_pc_page(q, bio, page, n, offs))
+				if (!__bio_add_pc_page(q, bio, page, n, offs,
+							true))
 					break;
 
-				/*
-				 * check if vector was merged with previous
-				 * drop page reference if needed
-				 */
-				if (bio->bi_vcnt == prev_bi_vcnt)
-					put_page(page);
-
 				added += n;
 				bytes -= n;
 				offs = 0;
diff --git a/include/linux/bio.h b/include/linux/bio.h
index bb6090aa165d..28b8c46de11c 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -430,8 +430,16 @@ extern void bio_reset(struct bio *);
 void bio_chain(struct bio *, struct bio *);
 
 extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
-extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
-			   unsigned int, unsigned int);
+extern int __bio_add_pc_page(struct request_queue *, struct bio *, struct page *,
+			   unsigned int, unsigned int, bool);
+
+static inline int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page
+		    *page, unsigned int len, unsigned int offset)
+{
+	return __bio_add_pc_page(q, bio, page, len, offset, false);
+}
+
+
 bool __bio_try_merge_page(struct bio *bio, struct page *page,
 		unsigned int len, unsigned int off, bool same_page);
 void __bio_add_page(struct bio *bio, struct page *page,
-- 
2.9.5


  parent reply	other threads:[~2019-03-09  1:57 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-09  1:37 [PATCH 0/6] block: enable multi-page bvec for passthrough IO Ming Lei
2019-03-09  1:37 ` Ming Lei
2019-03-09  1:37 ` [PATCH 1/6] block: pass page to xen_biovec_phys_mergeable Ming Lei
2019-03-09  1:37   ` Ming Lei
2019-03-11 14:16   ` Christoph Hellwig
2019-03-11 14:16     ` Christoph Hellwig
2019-03-11 19:57   ` Boris Ostrovsky
2019-03-11 19:57     ` Boris Ostrovsky
2019-03-09  1:37 ` [PATCH 2/6] block: don't merge adjacent bvecs to one segment in bio blk_queue_split Ming Lei
2019-03-09  1:37   ` Ming Lei
2019-03-11 14:21   ` Christoph Hellwig
2019-03-11 14:21     ` Christoph Hellwig
2019-03-12  1:22     ` Ming Lei
2019-03-12  1:22       ` Ming Lei
2019-03-11 19:58   ` Boris Ostrovsky
2019-03-11 19:58     ` Boris Ostrovsky
2019-03-09  1:37 ` [PATCH 3/6] block: check if page is mergeable in one helper Ming Lei
2019-03-11 14:23   ` Christoph Hellwig
2019-03-17  8:05     ` Ming Lei
2019-03-09  1:37 ` Ming Lei [this message]
2019-03-11 14:28   ` [PATCH 4/6] block: put the same page when adding it to bio Christoph Hellwig
2019-03-09  1:37 ` [PATCH 5/6] block: enable multi-page bvec for passthrough IO Ming Lei
2019-03-11 14:35   ` Christoph Hellwig
2019-03-12  1:06     ` Ming Lei
2019-03-09  1:37 ` [PATCH 6/6] block: don't check if adjacent bvecs in one bio can be mergeable Ming Lei
2019-03-11 14:40   ` Christoph Hellwig
2019-03-12  1:19     ` Ming Lei

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=20190309013737.27741-5-ming.lei@redhat.com \
    --to=ming.lei@redhat.com \
    --cc=axboe@kernel.dk \
    --cc=hch@lst.de \
    --cc=linux-block@vger.kernel.org \
    --cc=osandov@fb.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.