Skip to contentSkip to conversation

Reduce code duplication

PATCH Superseded Bart Van Assche 3 participants 13 messages Apr 18, 2017 – Apr 19, 2017

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Reduce code duplication

Hello Jens,

This series contains three patches intended to reduce code duplication and two patches that simplify / optimize I/O priority handling. Please consider these patches for kernel v4.12.

Thanks,

Bart.

Bart Van Assche (5):
block: Export blk_init_request_from_bio()
null_blk: Use blk_init_request_from_bio() instead of open-coding it
lightnvm: Use blk_init_request_from_bio() instead of open-coding it
block: Inline blk_rq_set_prio()
block: Optimize ioprio_best()

 block/blk-core.c             | 12 +++++++-----
 block/blk-mq.c               |  2 +-
 block/blk.h                  |  1 -
 block/ioprio.c               | 12 +-----------
 drivers/block/null_blk.c     |  9 +--------
 drivers/nvme/host/lightnvm.c |  6 +-----
 include/linux/blkdev.h       | 15 +--------------
 7 files changed, 12 insertions(+), 45 deletions(-)
Signature · 1 line
2.12.2
#1 lore raw

PATCH 1/5 block: Export blk_init_request_from_bio()

Export this function such that it becomes available to block drivers.

Cc Christoph Hellwig <[email protected]>
Cc Matias Bjørling <[email protected]>
Cc Adam Manzanares <[email protected]>
---
 block/blk-core.c       | 5 +++--
 block/blk-mq.c         | 2 +-
 block/blk.h            | 1 -
 include/linux/blkdev.h | 1 +
 4 files changed, 5 insertions(+), 4 deletions(-)
block/blk-core.c +3 -2
index ab1ecf71cb27..c274aed2ca3f 100644
@@ -1633,7 +1633,7 @@ unsigned int blk_plug_queued_count(struct request_queue *q)
 	return ret;
 }
 
-void init_request_from_bio(struct request *req, struct bio *bio)
+void blk_init_request_from_bio(struct request *req, struct bio *bio)
 {
 	if (bio->bi_opf & REQ_RAHEAD)
 		req->cmd_flags |= REQ_FAILFAST_MASK;
@@ -1645,6 +1645,7 @@ void init_request_from_bio(struct request *req, struct bio *bio)
 		req->ioprio = bio_prio(bio);
 	blk_rq_bio_prep(req->q, req, bio);
 }
+EXPORT_SYMBOL(blk_init_request_from_bio);
 
 static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
 {
@@ -1735,7 +1736,7 @@ static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
 	 * We don't worry about that case for efficiency. It won't happen
 	 * often, and the elevators are able to handle it.
 	 */
-	init_request_from_bio(req, bio);
+	blk_init_request_from_bio(req, bio);
 
 	if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
 		req->cpu = raw_smp_processor_id();
block/blk-mq.c +1 -1
index 4d8368f657ac..a3a2e9c40453 100644
@@ -1410,7 +1410,7 @@ void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
 
 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
 {
-	init_request_from_bio(rq, bio);
+	blk_init_request_from_bio(rq, bio);
 
 	blk_account_io_start(rq, true);
 }
block/blk.h +0 -1
index 07d375183f31..cc8e61cdc229 100644
@@ -60,7 +60,6 @@ void blk_free_flush_queue(struct blk_flush_queue *q);
 int blk_init_rl(struct request_list *rl, struct request_queue *q,
 		gfp_t gfp_mask);
 void blk_exit_rl(struct request_list *rl);
-void init_request_from_bio(struct request *req, struct bio *bio);
 void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
 			struct bio *bio);
 void blk_queue_bypass_start(struct request_queue *q);
include/linux/blkdev.h +1 -0
index 7cadaa0cc0b9..e1ea875ec048 100644
@@ -924,6 +924,7 @@ extern int blk_register_queue(struct gendisk *disk);
 extern void blk_unregister_queue(struct gendisk *disk);
 extern blk_qc_t generic_make_request(struct bio *bio);
 extern void blk_rq_init(struct request_queue *q, struct request *rq);
+extern void blk_init_request_from_bio(struct request *req, struct bio *bio);
 extern void blk_put_request(struct request *);
 extern void __blk_put_request(struct request_queue *, struct request *);
 extern struct request *blk_get_request(struct request_queue *, int, gfp_t);
Signature · 1 line
2.12.2
#3 lore raw

PATCH 2/5 null_blk: Use blk_init_request_from_bio() instead of open-coding it

In reply to Bart Van Assche

This patch changes the behavior of the null_blk driver for the LightNVM mode as follows:
* REQ_FAILFAST_MASK is set for read-ahead requests.
* If no I/O priority has been set in the bio, the I/O priority is copied from the I/O context.
* The rq_disk member is initialized if bio->bi_bdev != NULL.
* req->errors is initialized to zero.

Cc Christoph Hellwig <[email protected]>
Cc Matias Bjørling <[email protected]>
Cc Adam Manzanares <[email protected]>
---
 drivers/block/null_blk.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/block/null_blk.c +1 -8
index f93906ff31e8..e79e3d24e229 100644
@@ -443,14 +443,7 @@ static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
 	if (IS_ERR(rq))
 		return -ENOMEM;
 
-	rq->__sector = bio->bi_iter.bi_sector;
-	rq->ioprio = bio_prio(bio);
-
-	if (bio_has_data(bio))
-		rq->nr_phys_segments = bio_phys_segments(q, bio);
-
-	rq->__data_len = bio->bi_iter.bi_size;
-	rq->bio = rq->biotail = bio;
+	blk_init_request_from_bio(rq, bio);
 
 	rq->end_io_data = rqd;
 
Signature · 1 line
2.12.2
#5 lore raw

PATCH 3/5 lightnvm: Use blk_init_request_from_bio() instead of open-coding it

In reply to Bart Van Assche

This patch changes the behavior of the lightnvm driver as follows:
* REQ_FAILFAST_MASK is set for read-ahead requests.
* If no I/O priority has been set in the bio, the I/O priority is copied from the I/O context.
* The rq_disk member is initialized if bio->bi_bdev != NULL.
* The bio sector offset is copied into req->__sector instead of retaining the value -1 set by blk_mq_alloc_request().
* req->errors is initialized to zero.

Cc Christoph Hellwig <[email protected]>
Cc Matias Bjørling <[email protected]>
Cc Adam Manzanares <[email protected]>
---
 drivers/nvme/host/lightnvm.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/nvme/host/lightnvm.c +1 -5
index 4b78090518e1..b76e2e36fef4 100644
@@ -511,11 +511,7 @@ static int nvme_nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
 	rq->cmd_flags &= ~REQ_FAILFAST_DRIVER;
 
 	if (bio) {
-		rq->ioprio = bio_prio(bio);
-		rq->__data_len = bio->bi_iter.bi_size;
-		rq->bio = rq->biotail = bio;
-		if (bio_has_data(bio))
-			rq->nr_phys_segments = bio_phys_segments(q, bio);
+		blk_init_request_from_bio(rq, bio);
 	} else {
 		rq->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
 		rq->__data_len = 0;
Signature · 1 line
2.12.2
#7 lore raw

PATCH 4/5 block: Inline blk_rq_set_prio()

In reply to Bart Van Assche

Since only a single caller remains, inline blk_rq_set_prio(). Initialize req->ioprio even if no I/O priority has been set in the bio nor in the I/O context.

---
 block/blk-core.c       |  7 ++++---
 include/linux/blkdev.h | 14 --------------
 2 files changed, 4 insertions(+), 17 deletions(-)
block/blk-core.c +4 -3
index c274aed2ca3f..7374b02370fa 100644
@@ -1635,14 +1635,15 @@ unsigned int blk_plug_queued_count(struct request_queue *q)
 
 void blk_init_request_from_bio(struct request *req, struct bio *bio)
 {
+	struct io_context *ioc = rq_ioc(bio);
+
 	if (bio->bi_opf & REQ_RAHEAD)
 		req->cmd_flags |= REQ_FAILFAST_MASK;
 
 	req->errors = 0;
 	req->__sector = bio->bi_iter.bi_sector;
-	blk_rq_set_prio(req, rq_ioc(bio));
-	if (ioprio_valid(bio_prio(bio)))
-		req->ioprio = bio_prio(bio);
+	req->ioprio = ioprio_valid(bio_prio(bio)) ? bio_prio(bio) : ioc ?
+		ioc->ioprio : IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
 	blk_rq_bio_prep(req->q, req, bio);
 }
 EXPORT_SYMBOL(blk_init_request_from_bio);
include/linux/blkdev.h +0 -14
index e1ea875ec048..28f713803871 100644
@@ -1088,20 +1088,6 @@ static inline unsigned int blk_rq_count_bios(struct request *rq)
 }
 
 /*
- * blk_rq_set_prio - associate a request with prio from ioc
- * @rq: request of interest
- * @ioc: target iocontext
- *
- * Assocate request prio with ioc prio so request based drivers
- * can leverage priority information.
- */
-static inline void blk_rq_set_prio(struct request *rq, struct io_context *ioc)
-{
-	if (ioc)
-		rq->ioprio = ioc->ioprio;
-}
-
-/*
  * Request issue related functions.
  */
 extern struct request *blk_peek_request(struct request_queue *q);
Signature · 1 line
2.12.2
#11 lore raw

PATCH 5/5 block: Optimize ioprio_best()

In reply to Bart Van Assche

Since ioprio_best() translates IOPRIO_CLASS_NONE into IOPRIO_CLASS_BE and since lower numerical priority values represent a higher priority a simple numerical comparison is sufficient.

---
 block/ioprio.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)
block/ioprio.c +1 -11
index 0c47a00f92a8..4b120c9cf7e8 100644
@@ -163,22 +163,12 @@ static int get_task_ioprio(struct task_struct *p)
 
 int ioprio_best(unsigned short aprio, unsigned short bprio)
 {
-	unsigned short aclass;
-	unsigned short bclass;
-
 	if (!ioprio_valid(aprio))
 		aprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
 	if (!ioprio_valid(bprio))
 		bprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, IOPRIO_NORM);
 
-	aclass = IOPRIO_PRIO_CLASS(aprio);
-	bclass = IOPRIO_PRIO_CLASS(bprio);
-	if (aclass == bclass)
-		return min(aprio, bprio);
-	if (aclass > bclass)
-		return bprio;
-	else
-		return aprio;
+	return min(aprio, bprio);
 }
 
 SYSCALL_DEFINE2(ioprio_get, int, which, int, who)
Signature · 1 line
2.12.2
maintainer Reviewed-by on v1 4/5
#8 lore raw

Re: [PATCH 4/5] block: Inline blk_rq_set_prio()

In reply to Bart Van Assche

block/blk-core.c unsigned int blk_plug_queued_count(struct request_queue *q) · line 1646

Quoted 2 lines
block/blk-core.c +2 -0
+	req->ioprio = ioprio_valid(bio_prio(bio)) ? bio_prio(bio) : ioc ?
+		ioc->ioprio : IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);

I think this would be a tad cleaner with a traditional if / else if / else chain, e.g.

	if (ioprio_valid(bio_prio(bio)))
		req->ioprio = bio_prio(bio);
	else if (ioc)
		req->ioprio = ioc->ioprio;
	else
		req->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);

But otherwise the patch looks good to me:

maintainer
#9 lore raw

Re: [PATCH 4/5] block: Inline blk_rq_set_prio()

In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig

block/blk-core.c unsigned int blk_plug_queued_count(struct request_queue *q) · line 1646

Quoted 2 lines
block/blk-core.c +2 -0
+	req->ioprio = ioprio_valid(bio_prio(bio)) ? bio_prio(bio) : ioc ?
+		ioc->ioprio : IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);

I think this would be a tad cleaner with a traditional if / else if / else chain, e.g.

Quoted 6 lines
	if (ioprio_valid(bio_prio(bio)))
		req->ioprio = bio_prio(bio);
	else if (ioc)
		req->ioprio = ioc->ioprio;
	else
		req->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);

Agree, I hate ternaries with a vengeance, and a doubly nested one is almost a shooting offense.

Bart, care to respin with this fixed and the GPL export modification?

Signature · 1 line
Jens Axboe
#10 lore raw

Re: [PATCH 4/5] block: Inline blk_rq_set_prio()

Quoted 5 lines · Jens Axboe
Quoted 4 lines · Christoph Hellwig

Review

Quoted 2 lines
(quoted diff) +2 -0
+	req->ioprio =3D ioprio_valid(bio_prio(bio)) ? bio_prio(bio) : ioc ?
+		ioc->ioprio : IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);

=20
I think this would be a tad cleaner with a traditional if / else if / e=

lse

Quoted 8 lines
Quoted 8 lines

chain, e.g.
=20

	if (ioprio_valid(bio_prio(bio)))
		req->ioprio =3D bio_prio(bio);
	else if (ioc)
		req->ioprio =3D ioc->ioprio;
	else
		req->ioprio =3D IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
Quoted 5 lines

=20
Agree, I hate ternaries with a vengeance, and a doubly nested one is almost a shooting offense.
=20
Bart, care to respin with this fixed and the GPL export modification?

Hello Christoph and Jens,

Thanks for the review and the feedback. I will make the proposed changes and repost this patch series.

Bart.=