From: Dongsu Park <dongsu.park@profitbricks.com>
To: linux-kernel@vger.kernel.org
Cc: Christoph Hellwig <hch@infradead.org>,
Kent Overstreet <kmo@daterainc.com>, Jens Axboe <axboe@kernel.dk>,
Dongsu Park <dongsu.park@profitbricks.com>,
Al Viro <viro@zeniv.linux.org.uk>
Subject: [PATCH v2 2/7] block: rewrite __bio_copy_iov()
Date: Mon, 12 Jan 2015 12:43:59 +0100 [thread overview]
Message-ID: <0bdbbd2ef9d449be2d00e01ca7dc7f101b6cfb88.1421052656.git.dongsu.park@profitbricks.com> (raw)
In-Reply-To: <19cceed4e701430bfb307301a38cdf8d40fbfcac.1421052656.git.dongsu.park@profitbricks.com>
In-Reply-To: <cover.1421052656.git.dongsu.park@profitbricks.com>
Rewrite __bio_copy_iov() so that it can call either _read() or _write()
variant, which is determined by direction to_iov, given as either READ
or WRITE. Moreover, make __bio_copy_iov() take its parameter iov_iter
by value, to avoid awkward situations like ref-/dereferencing pointer
and value repeatedly.
This commit should contain only literal replacements, without
functional changes.
Suggested-by: Christoph Hellwig <hch@infradead.org>
Cc: Kent Overstreet <kmo@daterainc.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Dongsu Park <dongsu.park@profitbricks.com>
---
block/bio.c | 113 ++++++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 75 insertions(+), 38 deletions(-)
diff --git a/block/bio.c b/block/bio.c
index 8267676..7b1aed3 100644
--- a/block/bio.c
+++ b/block/bio.c
@@ -1046,46 +1046,84 @@ static struct bio_map_data *bio_alloc_map_data(unsigned int iov_count,
sizeof(struct sg_iovec) * iov_count, gfp_mask);
}
-static int __bio_copy_iov(struct bio *bio, const struct iov_iter *iter,
- int to_user, int from_user, int do_free_page)
+/**
+ * __bio_copy_iov_read - copy all pages from iov_iter to bio
+ * @bio: The &struct bio which describes the I/O as destination
+ * @iter: iov_iter as source
+ *
+ * Copy all pages from iov_iter to bio.
+ * Returns 0 on success, or error on failure.
+ */
+static int __bio_copy_iov_read(struct bio *bio, struct iov_iter iter)
{
- int ret = 0, i;
+ int i;
struct bio_vec *bvec;
- struct iov_iter iov_iter = *iter;
bio_for_each_segment_all(bvec, bio, i) {
- char *bv_addr = page_address(bvec->bv_page);
- unsigned int bv_len = bvec->bv_len;
-
- while (bv_len && iov_iter.count) {
- struct iovec iov = iov_iter_iovec(&iov_iter);
- unsigned int bytes = min_t(unsigned int, bv_len,
- iov.iov_len);
-
- if (!ret) {
- if (to_user)
- ret = copy_to_user(iov.iov_base,
- bv_addr, bytes);
-
- if (from_user)
- ret = copy_from_user(bv_addr,
- iov.iov_base,
- bytes);
-
- if (ret)
- ret = -EFAULT;
- }
+ ssize_t ret;
- bv_len -= bytes;
- bv_addr += bytes;
- iov_iter_advance(&iov_iter, bytes);
- }
+ ret = copy_page_from_iter(bvec->bv_page,
+ bvec->bv_offset,
+ bvec->bv_len,
+ &iter);
- if (do_free_page)
- __free_page(bvec->bv_page);
+ if (!iov_iter_count(&iter))
+ break;
+
+ if (ret < bvec->bv_len)
+ return -EFAULT;
}
- return ret;
+ return 0;
+}
+
+/**
+ * __bio_copy_iov_write - copy all pages from bio to iov_iter
+ * @bio: The &struct bio which describes the I/O as source
+ * @iter: iov_iter as destination
+ *
+ * Copy all pages from bio to iov_iter.
+ * Returns 0 on success, or error on failure.
+ */
+static int __bio_copy_iov_write(struct bio *bio, struct iov_iter iter)
+{
+ int i;
+ struct bio_vec *bvec;
+
+ bio_for_each_segment_all(bvec, bio, i) {
+ ssize_t ret;
+
+ ret = copy_page_to_iter(bvec->bv_page,
+ bvec->bv_offset,
+ bvec->bv_len,
+ &iter);
+
+ if (!iov_iter_count(&iter))
+ break;
+
+ if (ret < bvec->bv_len)
+ return -EFAULT;
+ }
+
+ return 0;
+}
+
+/**
+ * __bio_copy_iov - copy all pages between bio and iov_iter
+ * @bio: The &struct bio which describes the I/O
+ * @iter: iov_iter either as source or destination
+ * @to_iov: whether to %READ (0) or %WRITE (1)
+ *
+ * Simple wrapper around __bio_copy_iov_{write,read}().
+ * Returns 0 on success, or the error returned as-is on failure.
+ */
+static inline int __bio_copy_iov(struct bio *bio, struct iov_iter iter,
+ int to_iov)
+{
+ if (to_iov == WRITE)
+ return __bio_copy_iov_write(bio, iter);
+ else
+ return __bio_copy_iov_read(bio, iter);
}
/**
@@ -1106,11 +1144,10 @@ int bio_uncopy_user(struct bio *bio)
* if we're in a workqueue, the request is orphaned, so
* don't copy into a random user address space, just free.
*/
- if (current->mm)
- ret = __bio_copy_iov(bio, &bmd->iter,
- bio_data_dir(bio) == READ,
- 0, bmd->is_our_pages);
- else if (bmd->is_our_pages)
+ if (current->mm && bio_data_dir(bio) == READ)
+ ret = __bio_copy_iov(bio, bmd->iter, WRITE);
+
+ if (bmd->is_our_pages)
bio_for_each_segment_all(bvec, bio, i)
__free_page(bvec->bv_page);
}
@@ -1233,7 +1270,7 @@ struct bio *bio_copy_user_iov(struct request_queue *q,
*/
if ((!write_to_vm && (!map_data || !map_data->null_mapped)) ||
(map_data && map_data->from_user)) {
- ret = __bio_copy_iov(bio, iter, 0, 1, 0);
+ ret = __bio_copy_iov(bio, *iter, READ);
if (ret)
goto cleanup;
}
--
2.1.0
next prev parent reply other threads:[~2015-01-12 11:51 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-01-12 11:43 [RFC PATCH v2 0/7] preparation for block layer simplification Dongsu Park
2015-01-12 11:43 ` [PATCH v2 1/7] block: replace sg_iovec with iov_iter Dongsu Park
2015-01-12 11:43 ` Dongsu Park [this message]
2015-01-12 11:44 ` [PATCH v2 3/7] block: refactor iov_count_pages() from bio_{copy,map}_user_iov() Dongsu Park
2015-01-12 11:44 ` [PATCH v2 4/7] block: refactor bio_get_user_pages() from __bio_map_user_iov() Dongsu Park
2015-01-12 11:44 ` [PATCH v2 5/7] md/raid10: make sync_request_write() call bio_copy_data() Dongsu Park
2015-01-12 11:44 ` [PATCH v2 6/7] fs: make _submit_bh consistent with generic bio chaining Dongsu Park
2015-01-12 11:44 ` [PATCH v2 7/7] PM: submit bio in a sane way in cases without bio_chain Dongsu Park
2015-01-30 0:49 ` Rafael J. Wysocki
2015-01-16 11:44 ` [PATCH v2 4/7] block: refactor bio_get_user_pages() from __bio_map_user_iov() Christoph Hellwig
2015-01-16 11:41 ` [PATCH v2 3/7] block: refactor iov_count_pages() from bio_{copy,map}_user_iov() Christoph Hellwig
2015-01-15 17:10 ` [PATCH v2 2/7] block: rewrite __bio_copy_iov() Christoph Hellwig
2015-01-15 18:18 ` Christoph Hellwig
2015-01-16 11:31 ` Christoph Hellwig
2015-01-16 11:58 ` Dongsu Park
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=0bdbbd2ef9d449be2d00e01ca7dc7f101b6cfb88.1421052656.git.dongsu.park@profitbricks.com \
--to=dongsu.park@profitbricks.com \
--cc=axboe@kernel.dk \
--cc=hch@infradead.org \
--cc=kmo@daterainc.com \
--cc=linux-kernel@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®