LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: Jens Axboe <axboe@kernel.dk>, Dongsu Park <dongsu.park@profitbricks.com>
Cc: Kent Overstreet <kmo@daterainc.com>, linux-kernel@vger.kernel.org
Subject: [PATCH 1/7] block: simplify bio_map_kern
Date: Sun, 18 Jan 2015 16:16:28 +0100	[thread overview]
Message-ID: <1421594194-25918-2-git-send-email-hch@lst.de> (raw)
In-Reply-To: <1421594194-25918-1-git-send-email-hch@lst.de>

Just open code the trivial mapping from a kernel virtual address to
a bio instead of going through the complex user address mapping
machinery.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 block/bio.c | 60 ++++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 44 insertions(+), 16 deletions(-)

diff --git a/block/bio.c b/block/bio.c
index 471d738..54da51e 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -1563,9 +1563,8 @@ static void bio_copy_kern_endio(struct bio *bio, int err)
 {
 	struct bio_vec *bvec;
 	const int read = bio_data_dir(bio) == READ;
-	struct bio_map_data *bmd = bio->bi_private;
+	char *p = bio->bi_private;
 	int i;
-	char *p = bmd->sgvecs[0].iov_base;
 
 	bio_for_each_segment_all(bvec, bio, i) {
 		char *addr = page_address(bvec->bv_page);
@@ -1577,7 +1576,6 @@ static void bio_copy_kern_endio(struct bio *bio, int err)
 		p += bvec->bv_len;
 	}
 
-	kfree(bmd);
 	bio_put(bio);
 }
 
@@ -1595,28 +1593,58 @@ static void bio_copy_kern_endio(struct bio *bio, int err)
 struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
 			  gfp_t gfp_mask, int reading)
 {
-	struct bio *bio;
+	unsigned long kaddr = (unsigned long)data;
+	unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
+	unsigned long start = kaddr >> PAGE_SHIFT;
 	struct bio_vec *bvec;
-	int i;
+	struct bio *bio;
+	void *p = data;
+	int nr_pages = 0, i;
 
-	bio = bio_copy_user(q, NULL, (unsigned long)data, len, 1, gfp_mask);
-	if (IS_ERR(bio))
-		return bio;
+	/*
+	 * Overflow, abort
+	 */
+	if (end < start)
+		return ERR_PTR(-EINVAL);
 
-	if (!reading) {
-		void *p = data;
+	nr_pages = end - start;
+	bio = bio_kmalloc(gfp_mask, nr_pages);
+	if (!bio)
+		return ERR_PTR(-ENOMEM);
 
-		bio_for_each_segment_all(bvec, bio, i) {
-			char *addr = page_address(bvec->bv_page);
+	while (len) {
+		struct page *page;
+		unsigned int bytes = PAGE_SIZE;
 
-			memcpy(addr, p, bvec->bv_len);
-			p += bvec->bv_len;
-		}
+		if (bytes > len)
+			bytes = len;
+
+		page = alloc_page(q->bounce_gfp | gfp_mask);
+		if (!page)
+			goto cleanup;
+
+		if (!reading)
+			memcpy(page_address(page), p, bytes);
+
+		if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes)
+			break;
+
+		len -= bytes;
+		p += bytes;
 	}
 
-	bio->bi_end_io = bio_copy_kern_endio;
+	if (!reading)
+		bio->bi_rw |= REQ_WRITE;
 
+	bio->bi_private = data;
+	bio->bi_end_io = bio_copy_kern_endio;
 	return bio;
+
+cleanup:
+	bio_for_each_segment_all(bvec, bio, i)
+		__free_page(bvec->bv_page);
+	bio_put(bio);
+	return ERR_PTR(-ENOMEM);
 }
 EXPORT_SYMBOL(bio_copy_kern);
 
-- 
1.9.1


  reply	other threads:[~2015-01-18 15:17 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-18 15:16 cleanup and refactor BLOCK_PC mapping helpers V2 Christoph Hellwig
2015-01-18 15:16 ` Christoph Hellwig [this message]
2015-01-18 15:16 ` [PATCH 2/7] block: use blk_rq_map_user_iov to implement blk_rq_map_user Christoph Hellwig
2015-01-18 15:16 ` [PATCH 3/7] block: add a helper to free bio bounce buffer pages Christoph Hellwig
2015-01-18 15:16 ` [PATCH 4/7] block: pass iov_iter to the BLOCK_PC mapping functions Christoph Hellwig
2015-01-18 15:16 ` [PATCH 5/7] block: merge __bio_map_kern into bio_map_kern Christoph Hellwig
2015-01-18 15:16 ` [PATCH 6/7] block: merge __bio_map_user_iov into bio_map_user_iov Christoph Hellwig
2015-01-18 15:16 ` [PATCH 7/7] block: rewrite and split __bio_copy_iov() Christoph Hellwig
2015-01-25 10:29 ` cleanup and refactor BLOCK_PC mapping helpers V2 Ming Lei
2015-02-02 13:19   ` Christoph Hellwig
2015-02-05 11:24     ` Christoph Hellwig
2015-02-05 16:28     ` Jens Axboe
2015-02-09 16:34       ` Dongsu Park
  -- strict thread matches above, loose matches on Subject: below --
2015-01-16 12:04 cleanup and refactor BLOCK_PC mapping helpers Christoph Hellwig
2015-01-16 12:04 ` [PATCH 1/7] block: simplify bio_map_kern Christoph Hellwig

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=1421594194-25918-2-git-send-email-hch@lst.de \
    --to=hch@lst.de \
    --cc=axboe@kernel.dk \
    --cc=dongsu.park@profitbricks.com \
    --cc=kmo@daterainc.com \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: [PATCH 1/7] block: simplify bio_map_kern' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).