Skip to contentSkip to conversation

block/loop: improve performance

PATCH Superseded Shaohua Li 2 participants 6 messages Aug 23, 2017 – Aug 24, 2017 Block layer

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Shaohua Li

author
#0 lore raw

cover block/loop: improve performance

From: Shaohua Li <[email protected]>

two small patches to improve performance for loop in directio mode. The goal is to increase IO size sending to underlayer disks.

Thanks,
Shaohua

Shaohua Li (2):
block/loop: set hw_sectors
block/loop: allow request merge for directio mode

 drivers/block/loop.c | 44 ++++++++++++++++++++++++++++++++++++--------
 drivers/block/loop.h |  1 +
 2 files changed, 37 insertions(+), 8 deletions(-)
Signature · 1 line
2.9.5
author
#1 lore raw

PATCH 1/2 block/loop: set hw_sectors

From: Shaohua Li <[email protected]>

Loop can handle any size of request. Limiting it to 255 sectors just burns the CPU for bio split and request merge for underlayer disk and also cause bad fs block allocation in directio mode.

---
 drivers/block/loop.c | 1 +
 1 file changed, 1 insertion(+)
drivers/block/loop.c +1 -0
index b55a1f8..428da07 100644
@@ -1799,6 +1799,7 @@ static int loop_add(struct loop_device **l, int i)
 	}
 	lo->lo_queue->queuedata = lo;
 
+	blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS);
 	/*
 	 * It doesn't make sense to enable merge because the I/O
 	 * submitted to backing file is handled page by page.
Signature · 1 line
2.9.5
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] block/loop: set hw_sectors

Quoted 5 lines · Shaohua Li

From: Shaohua Li <[email protected]>

Loop can handle any size of request. Limiting it to 255 sectors just burns the CPU for bio split and request merge for underlayer disk and also cause bad fs block allocation in directio mode.

Note that this will conflict with my loop blocksize series, we can fix up whichever series goes in second.

Quoted 20 lines
---
 drivers/block/loop.c | 1 +
 1 file changed, 1 insertion(+)
drivers/block/loop.c +1 -0
index b55a1f8..428da07 100644
@@ -1799,6 +1799,7 @@ static int loop_add(struct loop_device **l, int i)
 	}
 	lo->lo_queue->queuedata = lo;
 
+	blk_queue_max_hw_sectors(lo->lo_queue, BLK_DEF_MAX_SECTORS);
 	/*
 	 * It doesn't make sense to enable merge because the I/O
 	 * submitted to backing file is handled page by page.
Signature · 1 line
2.9.5
author
#3 lore raw

PATCH 2/2 block/loop: allow request merge for directio mode

In reply to Shaohua Li

From: Shaohua Li <[email protected]>

Currently loop disables merge. While it makes sense for buffer IO mode, directio mode can benefit from request merge. Without merge, loop could send small size IO to underlayer disk and harm performance.

---
 drivers/block/loop.c | 43 +++++++++++++++++++++++++++++++++++--------
 drivers/block/loop.h |  1 +
 2 files changed, 36 insertions(+), 8 deletions(-)
drivers/block/loop.c +35 -8
index 428da07..1bfa3ff 100644
@@ -213,10 +213,13 @@ static void __loop_update_dio(struct loop_device *lo, bool dio)
 	 */
 	blk_mq_freeze_queue(lo->lo_queue);
 	lo->use_dio = use_dio;
-	if (use_dio)
+	if (use_dio) {
+		queue_flag_clear_unlocked(QUEUE_FLAG_NOMERGES, lo->lo_queue);
 		lo->lo_flags |= LO_FLAGS_DIRECT_IO;
-	else
+	} else {
+		queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, lo->lo_queue);
 		lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
+	}
 	blk_mq_unfreeze_queue(lo->lo_queue);
 }
 
@@ -464,6 +467,8 @@ static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2)
 {
 	struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb);
 
+	kfree(cmd->bvec);
+	cmd->bvec = NULL;
 	cmd->ret = ret;
 	blk_mq_complete_request(cmd->rq);
 }
@@ -473,22 +478,44 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
 {
 	struct iov_iter iter;
 	struct bio_vec *bvec;
-	struct bio *bio = cmd->rq->bio;
+	struct request *rq = cmd->rq;
+	struct bio *bio = rq->bio;
 	struct file *file = lo->lo_backing_file;
+	unsigned int offset;
+	int segments = 0;
 	int ret;
 
-	/* nomerge for loop request queue */
-	WARN_ON(cmd->rq->bio != cmd->rq->biotail);
+	if (rq->bio != rq->biotail) {
+		struct req_iterator iter;
+		struct bio_vec tmp;
+
+		__rq_for_each_bio(bio, rq)
+			segments += bio_segments(bio);
+		bvec = kmalloc(sizeof(struct bio_vec) * segments, GFP_KERNEL);
+		if (!bvec)
+			return -EIO;
+		cmd->bvec = bvec;
+
+		rq_for_each_segment(tmp, rq, iter) {
+			*bvec = tmp;
+			bvec++;
+		}
+		bvec = cmd->bvec;
+		offset = 0;
+	} else {
+		offset = bio->bi_iter.bi_bvec_done;
+		bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
+		segments = bio_segments(bio);
+	}
 
-	bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
 	iov_iter_bvec(&iter, ITER_BVEC | rw, bvec,
-		      bio_segments(bio), blk_rq_bytes(cmd->rq));
+		      segments, blk_rq_bytes(rq));
 	/*
 	 * This bio may be started from the middle of the 'bvec'
 	 * because of bio splitting, so offset from the bvec must
 	 * be passed to iov iterator
 	 */
-	iter.iov_offset = bio->bi_iter.bi_bvec_done;
+	iter.iov_offset = offset;
 
 	cmd->iocb.ki_pos = pos;
 	cmd->iocb.ki_filp = file;
drivers/block/loop.h +1 -0
index fecd3f9..bc9cf91 100644
@@ -72,6 +72,7 @@ struct loop_cmd {
 	bool use_aio;           /* use AIO interface to handle I/O */
 	long ret;
 	struct kiocb iocb;
+	struct bio_vec *bvec;
 };
 
 /* Support for loadable transfer modules */
Signature · 1 line
2.9.5
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] block/loop: allow request merge for directio mode

Quoted 5 lines · Shaohua Li

From: Shaohua Li <[email protected]>

Currently loop disables merge. While it makes sense for buffer IO mode, directio mode can benefit from request merge. Without merge, loop could send small size IO to underlayer disk and harm performance.

A couple of nits on comments below, besides that

Quoted 13 lines
---
 drivers/block/loop.c | 43 +++++++++++++++++++++++++++++++++++--------
 drivers/block/loop.h |  1 +
 2 files changed, 36 insertions(+), 8 deletions(-)
drivers/block/loop.c +0 -0
index 428da07..1bfa3ff 100644
@@ -213,10 +213,13 @@ static void __loop_update_dio(struct loop_device *lo, bool dio)
 	 */
 	blk_mq_freeze_queue(lo->lo_queue);

drivers/block/loop.c static void __loop_update_dio(struct loop_device *lo, bool dio) · line 222

Quoted 10 lines
drivers/block/loop.c +5 -2
 	lo->use_dio = use_dio;
-	if (use_dio)
+	if (use_dio) {
+		queue_flag_clear_unlocked(QUEUE_FLAG_NOMERGES, lo->lo_queue);
 		lo->lo_flags |= LO_FLAGS_DIRECT_IO;
-	else
+	} else {
+		queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, lo->lo_queue);
 		lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
+	}

Could you please also update the comment in loop_add() where we set QUEUE_FLAG_NOMERGES to say something like

/*
* By default we do buffered I/O, so it doesn't make sense to
* enable merging because the I/O submitted to backing file is
* handled page by page. We enable merging when switching to
* direct I/O mode.
*/

Quoted 48 lines
drivers/block/loop.c +28 -3
 	blk_mq_unfreeze_queue(lo->lo_queue);
 }
 
@@ -464,6 +467,8 @@ static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2)
 {
 	struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb);
 
+	kfree(cmd->bvec);
+	cmd->bvec = NULL;
 	cmd->ret = ret;
 	blk_mq_complete_request(cmd->rq);
 }
@@ -473,22 +478,44 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
 {
 	struct iov_iter iter;
 	struct bio_vec *bvec;
-	struct bio *bio = cmd->rq->bio;
+	struct request *rq = cmd->rq;
+	struct bio *bio = rq->bio;
 	struct file *file = lo->lo_backing_file;
+	unsigned int offset;
+	int segments = 0;
 	int ret;
 
-	/* nomerge for loop request queue */
-	WARN_ON(cmd->rq->bio != cmd->rq->biotail);
+	if (rq->bio != rq->biotail) {
+		struct req_iterator iter;
+		struct bio_vec tmp;
+
+		__rq_for_each_bio(bio, rq)
+			segments += bio_segments(bio);
+		bvec = kmalloc(sizeof(struct bio_vec) * segments, GFP_KERNEL);
+		if (!bvec)
+			return -EIO;
+		cmd->bvec = bvec;
+
+		rq_for_each_segment(tmp, rq, iter) {
+			*bvec = tmp;
+			bvec++;
+		}
+		bvec = cmd->bvec;
+		offset = 0;
+	} else {
+		offset = bio->bi_iter.bi_bvec_done;
+		bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
+		segments = bio_segments(bio);
+	}

drivers/block/loop.c static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, · line 517

Quoted 10 lines
drivers/block/loop.c +1 -2
 
-	bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
 	iov_iter_bvec(&iter, ITER_BVEC | rw, bvec,
-		      bio_segments(bio), blk_rq_bytes(cmd->rq));
+		      segments, blk_rq_bytes(rq));
 	/*
 	 * This bio may be started from the middle of the 'bvec'
 	 * because of bio splitting, so offset from the bvec must
 	 * be passed to iov iterator
 	 */

This comment should be moved above to where you do

		offset = bio->bi_iter.bi_bvec_done;
Quoted 20 lines
drivers/block/loop.c +1 -1
-	iter.iov_offset = bio->bi_iter.bi_bvec_done;
+	iter.iov_offset = offset;
 
 	cmd->iocb.ki_pos = pos;
 	cmd->iocb.ki_filp = file;
drivers/block/loop.h +1 -0
index fecd3f9..bc9cf91 100644
@@ -72,6 +72,7 @@ struct loop_cmd {
 	bool use_aio;           /* use AIO interface to handle I/O */
 	long ret;
 	struct kiocb iocb;
+	struct bio_vec *bvec;
 };
 
 /* Support for loadable transfer modules */
Signature · 1 line
2.9.5
author
#5 lore raw

Re: [PATCH 2/2] block/loop: allow request merge for directio mode

Quoted 38 lines · Omar Sandoval
Quoted 5 lines · Shaohua Li

From: Shaohua Li <[email protected]>

Currently loop disables merge. While it makes sense for buffer IO mode, directio mode can benefit from request merge. Without merge, loop could send small size IO to underlayer disk and harm performance.

A couple of nits on comments below, besides that

Quoted 13 lines
---
 drivers/block/loop.c | 43 +++++++++++++++++++++++++++++++++++--------
 drivers/block/loop.h |  1 +
 2 files changed, 36 insertions(+), 8 deletions(-)
drivers/block/loop.c +0 -0
index 428da07..1bfa3ff 100644
@@ -213,10 +213,13 @@ static void __loop_update_dio(struct loop_device *lo, bool dio)
 	 */
 	blk_mq_freeze_queue(lo->lo_queue);

drivers/block/loop.c static void __loop_update_dio(struct loop_device *lo, bool dio) · line 222

Quoted 10 lines
drivers/block/loop.c +5 -2
 	lo->use_dio = use_dio;
-	if (use_dio)
+	if (use_dio) {
+		queue_flag_clear_unlocked(QUEUE_FLAG_NOMERGES, lo->lo_queue);
 		lo->lo_flags |= LO_FLAGS_DIRECT_IO;
-	else
+	} else {
+		queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, lo->lo_queue);
 		lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
+	}

Could you please also update the comment in loop_add() where we set QUEUE_FLAG_NOMERGES to say something like

Quoted 6 lines

/*
* By default we do buffered I/O, so it doesn't make sense to
* enable merging because the I/O submitted to backing file is
* handled page by page. We enable merging when switching to
* direct I/O mode.
*/

sure, will add

Quoted 61 lines
Quoted 48 lines
drivers/block/loop.c +28 -3
 	blk_mq_unfreeze_queue(lo->lo_queue);
 }
 
@@ -464,6 +467,8 @@ static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2)
 {
 	struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb);
 
+	kfree(cmd->bvec);
+	cmd->bvec = NULL;
 	cmd->ret = ret;
 	blk_mq_complete_request(cmd->rq);
 }
@@ -473,22 +478,44 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
 {
 	struct iov_iter iter;
 	struct bio_vec *bvec;
-	struct bio *bio = cmd->rq->bio;
+	struct request *rq = cmd->rq;
+	struct bio *bio = rq->bio;
 	struct file *file = lo->lo_backing_file;
+	unsigned int offset;
+	int segments = 0;
 	int ret;
 
-	/* nomerge for loop request queue */
-	WARN_ON(cmd->rq->bio != cmd->rq->biotail);
+	if (rq->bio != rq->biotail) {
+		struct req_iterator iter;
+		struct bio_vec tmp;
+
+		__rq_for_each_bio(bio, rq)
+			segments += bio_segments(bio);
+		bvec = kmalloc(sizeof(struct bio_vec) * segments, GFP_KERNEL);
+		if (!bvec)
+			return -EIO;
+		cmd->bvec = bvec;
+
+		rq_for_each_segment(tmp, rq, iter) {
+			*bvec = tmp;
+			bvec++;
+		}
+		bvec = cmd->bvec;
+		offset = 0;
+	} else {
+		offset = bio->bi_iter.bi_bvec_done;
+		bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
+		segments = bio_segments(bio);
+	}

drivers/block/loop.c static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, · line 517

Quoted 10 lines
drivers/block/loop.c +1 -2
 
-	bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
 	iov_iter_bvec(&iter, ITER_BVEC | rw, bvec,
-		      bio_segments(bio), blk_rq_bytes(cmd->rq));
+		      segments, blk_rq_bytes(rq));
 	/*
 	 * This bio may be started from the middle of the 'bvec'
 	 * because of bio splitting, so offset from the bvec must
 	 * be passed to iov iterator
 	 */

This comment should be moved above to where you do

Quoted 1 line
		offset = bio->bi_iter.bi_bvec_done;

This comment actually applies to 'rq->bio != rq->biotail' case too for each bio of the request. That's why I don't just copy bio->bi_io_vec. I'll explain this in different way.

Thanks,
Shaohua