[PATCH v2 5/8] block: Add support for small segments in blk_rq_map_user_iov()

Bart Van Assche
2022-11-23T20:58:27Z

Before changing the return value of bio_add_hw_page() into a value in
the range [0, len], make blk_rq_map_user_iov() fall back to copying data
if mapping the data is not possible due to the segment limit.

Cc: Christoph Hellwig <hch@lst.de>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Keith Busch <kbusch@kernel.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
block/blk-map.c | 27 ++++++++++++++++++++++-----
1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/block/blk-map.c b/block/blk-map.c
index d2d6ee098514..f3f2ed9c6183 100644
--- a/block/blk-map.c
+++ b/block/blk-map.c
@@ -308,17 +308,26 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,
else {
for (j = 0; j < npages; j++) {
struct page *page = pages[j];
- unsigned int n = PAGE_SIZE - offs;
+ unsigned int n = PAGE_SIZE - offs, added;
bool same_page = false;

if (n > bytes)
n = bytes;

- if (!bio_add_hw_page(rq->q, bio, page, n, offs,
- max_sectors, &same_page)) {
+ added = bio_add_hw_page(rq->q, bio, page, n,
+ offs, max_sectors, &same_page);
+ if (added == 0) {
if (same_page)
put_page(page);
break;
+ } else if (added != n) {
+ /*
+ * The segment size is smaller than the
+ * page size and an iov exceeds the
+ * segment size. Give up.
+ */
+ ret = -EREMOTEIO;
+ goto out_unmap;
}

bytes -= n;
@@ -672,10 +681,18 @@ int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,

i = *iter;
do {
- if (copy)
+ if (copy) {
ret = bio_copy_user_iov(rq, map_data, &i, gfp_mask);
- else
+ } else {
ret = bio_map_user_iov(rq, &i, gfp_mask);
+ /*
+ * Fall back to copying the data if bio_map_user_iov()
+ * returns -EREMOTEIO.
+ */
+ if (ret == -EREMOTEIO)
+ ret = bio_copy_user_iov(rq, map_data, &i,
+ gfp_mask);
+ }
if (ret)
goto unmap_rq;
if (!bio)