Skip to contentSkip to conversation

block: some misc changes

PATCH In review Ming Lei 2 participants 11 messages Oct 14, 2015 – Oct 16, 2015 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

cover block: some misc changes

Hi,

The 1st three patches are optimizations related with bio splitting.

The 4th patch is to mark ctx as pending at batch in flush plug path.

Thanks,

author
#1 lore raw

PATCH 1/4 block: setup bi_phys_segments after splitting

The number of bio->bi_phys_segments is always obtained during bio splitting, so it is natural to setup it just after bio splitting, then we can avoid to compute nr_segment again during merge.

---
 block/blk-merge.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)
block/blk-merge.c +22 -7
index c4e9c37..22293fd 100644
@@ -11,13 +11,16 @@
 
 static struct bio *blk_bio_discard_split(struct request_queue *q,
 					 struct bio *bio,
-					 struct bio_set *bs)
+					 struct bio_set *bs,
+					 unsigned *nsegs)
 {
 	unsigned int max_discard_sectors, granularity;
 	int alignment;
 	sector_t tmp;
 	unsigned split_sectors;
 
+	*nsegs = 1;
+
 	/* Zero-sector (unknown) and one-sector granularities are the same.  */
 	granularity = max(q->limits.discard_granularity >> 9, 1U);
 
@@ -51,8 +54,11 @@ static struct bio *blk_bio_discard_split(struct request_queue *q,
 
 static struct bio *blk_bio_write_same_split(struct request_queue *q,
 					    struct bio *bio,
-					    struct bio_set *bs)
+					    struct bio_set *bs,
+					    unsigned *nsegs)
 {
+	*nsegs = 1;
+
 	if (!q->limits.max_write_same_sectors)
 		return NULL;
 
@@ -64,7 +70,8 @@ static struct bio *blk_bio_write_same_split(struct request_queue *q,
 
 static struct bio *blk_bio_segment_split(struct request_queue *q,
 					 struct bio *bio,
-					 struct bio_set *bs)
+					 struct bio_set *bs,
+					 unsigned *segs)
 {
 	struct bio_vec bv, bvprv, *bvprvp = NULL;
 	struct bvec_iter iter;
@@ -106,22 +113,30 @@ new_segment:
 		sectors += bv.bv_len >> 9;
 	}
 
+	*segs = nsegs;
 	return NULL;
 split:
+	*segs = nsegs;
 	return bio_split(bio, sectors, GFP_NOIO, bs);
 }
 
 void blk_queue_split(struct request_queue *q, struct bio **bio,
 		     struct bio_set *bs)
 {
-	struct bio *split;
+	struct bio *split, *res;
+	unsigned nsegs;
 
 	if ((*bio)->bi_rw & REQ_DISCARD)
-		split = blk_bio_discard_split(q, *bio, bs);
+		split = blk_bio_discard_split(q, *bio, bs, &nsegs);
 	else if ((*bio)->bi_rw & REQ_WRITE_SAME)
-		split = blk_bio_write_same_split(q, *bio, bs);
+		split = blk_bio_write_same_split(q, *bio, bs, &nsegs);
 	else
-		split = blk_bio_segment_split(q, *bio, q->bio_split);
+		split = blk_bio_segment_split(q, *bio, q->bio_split, &nsegs);
+
+	/* physical segments can be figured out during splitting */
+	res = split ? split : *bio;
+	res->bi_phys_segments = nsegs;
+	bio_set_flag(res, BIO_SEG_VALID);
 
 	if (split) {
 		bio_chain(split, *bio);
Signature · 1 line
1.9.1
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] block: setup bi_phys_segments after splitting

Quoted 6 lines · Ming Lei

The number of bio->bi_phys_segments is always obtained during bio splitting, so it is natural to setup it just after bio splitting, then we can avoid to compute nr_segment again during merge.

Quoted 84 lines
---
 block/blk-merge.c | 29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)
block/blk-merge.c +22 -7
index c4e9c37..22293fd 100644
@@ -11,13 +11,16 @@
 
 static struct bio *blk_bio_discard_split(struct request_queue *q,
 					 struct bio *bio,
-					 struct bio_set *bs)
+					 struct bio_set *bs,
+					 unsigned *nsegs)
 {
 	unsigned int max_discard_sectors, granularity;
 	int alignment;
 	sector_t tmp;
 	unsigned split_sectors;
 
+	*nsegs = 1;
+
 	/* Zero-sector (unknown) and one-sector granularities are the same.  */
 	granularity = max(q->limits.discard_granularity >> 9, 1U);
 
@@ -51,8 +54,11 @@ static struct bio *blk_bio_discard_split(struct request_queue *q,
 
 static struct bio *blk_bio_write_same_split(struct request_queue *q,
 					    struct bio *bio,
-					    struct bio_set *bs)
+					    struct bio_set *bs,
+					    unsigned *nsegs)
 {
+	*nsegs = 1;
+
 	if (!q->limits.max_write_same_sectors)
 		return NULL;
 
@@ -64,7 +70,8 @@ static struct bio *blk_bio_write_same_split(struct request_queue *q,
 
 static struct bio *blk_bio_segment_split(struct request_queue *q,
 					 struct bio *bio,
-					 struct bio_set *bs)
+					 struct bio_set *bs,
+					 unsigned *segs)
 {
 	struct bio_vec bv, bvprv, *bvprvp = NULL;
 	struct bvec_iter iter;
@@ -106,22 +113,30 @@ new_segment:
 		sectors += bv.bv_len >> 9;
 	}
 
+	*segs = nsegs;
 	return NULL;
 split:
+	*segs = nsegs;
 	return bio_split(bio, sectors, GFP_NOIO, bs);
 }
 
 void blk_queue_split(struct request_queue *q, struct bio **bio,
 		     struct bio_set *bs)
 {
-	struct bio *split;
+	struct bio *split, *res;
+	unsigned nsegs;
 
 	if ((*bio)->bi_rw & REQ_DISCARD)
-		split = blk_bio_discard_split(q, *bio, bs);
+		split = blk_bio_discard_split(q, *bio, bs, &nsegs);
 	else if ((*bio)->bi_rw & REQ_WRITE_SAME)
-		split = blk_bio_write_same_split(q, *bio, bs);
+		split = blk_bio_write_same_split(q, *bio, bs, &nsegs);
 	else
-		split = blk_bio_segment_split(q, *bio, q->bio_split);
+		split = blk_bio_segment_split(q, *bio, q->bio_split, &nsegs);
+
+	/* physical segments can be figured out during splitting */
+	res = split ? split : *bio;
+	res->bi_phys_segments = nsegs;
+	bio_set_flag(res, BIO_SEG_VALID);
 
 	if (split) {
 		bio_chain(split, *bio);
author
#3 lore raw

PATCH 2/4 block: avoid to merge splitted bio

In reply to Ming Lei

The splitted bio has been already too fat to merge, so mark it as NOMERGE.

---
 block/blk-merge.c | 3 +++
 1 file changed, 3 insertions(+)
block/blk-merge.c +3 -0
index 22293fd..de5716d8 100644
@@ -139,6 +139,9 @@ void blk_queue_split(struct request_queue *q, struct bio **bio,
 	bio_set_flag(res, BIO_SEG_VALID);
 
 	if (split) {
+		/* there isn't chance to merge the splitted bio */
+		split->bi_rw |= REQ_NOMERGE;
+
 		bio_chain(split, *bio);
 		generic_make_request(*bio);
 		*bio = split;
Signature · 1 line
1.9.1
Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] block: avoid to merge splitted bio

Quoted 4 lines · Ming Lei

The splitted bio has been already too fat to merge, so mark it as NOMERGE.

Quoted 18 lines
---
 block/blk-merge.c | 3 +++
 1 file changed, 3 insertions(+)
block/blk-merge.c +3 -0
index 22293fd..de5716d8 100644
@@ -139,6 +139,9 @@ void blk_queue_split(struct request_queue *q, struct bio **bio,
 	bio_set_flag(res, BIO_SEG_VALID);
 
 	if (split) {
+		/* there isn't chance to merge the splitted bio */
+		split->bi_rw |= REQ_NOMERGE;
+
 		bio_chain(split, *bio);
 		generic_make_request(*bio);
 		*bio = split;
author
#5 lore raw

PATCH 3/4 blk-mq: check bio_mergeable() early before merging

In reply to Ming Lei

It isn't necessary to try to merge the bio which is marked as NOMERGE.

---
 block/blk-mq.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/blk-mq.c +4 -1
index 546b3b8..deb5f4c 100644
@@ -671,6 +671,9 @@ static bool blk_mq_attempt_merge(struct request_queue *q,
 	struct request *rq;
 	int checked = 8;
 
+	if (!bio_mergeable(bio))
+		return false;
+
 	list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
 		int el_ret;
 
@@ -1140,7 +1143,7 @@ static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
 					 struct blk_mq_ctx *ctx,
 					 struct request *rq, struct bio *bio)
 {
-	if (!hctx_allow_merges(hctx)) {
+	if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) {
 		blk_mq_bio_to_request(rq, bio);
 		spin_lock(&ctx->lock);
 insert_rq:
Signature · 1 line
1.9.1
#6 lore raw

Re: [PATCH 3/4] blk-mq: check bio_mergeable() early before merging

Quoted 23 lines · Ming Lei

It isn't necessary to try to merge the bio which is marked as NOMERGE.

---
 block/blk-mq.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/blk-mq.c +3 -0
index 546b3b8..deb5f4c 100644
@@ -671,6 +671,9 @@ static bool blk_mq_attempt_merge(struct request_queue *q,
 	struct request *rq;
 	int checked = 8;
 
+	if (!bio_mergeable(bio))
+		return false;
+
 	list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
 		int el_ret;
 

block/blk-mq.c static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx, · line 1149

Quoted 8 lines
block/blk-mq.c +1 -1
@@ -1140,7 +1143,7 @@ static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
 					 struct blk_mq_ctx *ctx,
 					 struct request *rq, struct bio *bio)
 {
-	if (!hctx_allow_merges(hctx)) {
+	if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) {
 		blk_mq_bio_to_request(rq, bio);
 		spin_lock(&ctx->lock);
 insert_rq:

blk_mq_attempt_merge is only called from blk_mq_merge_queue_io. So, by adding the conditional in blk_mq_merge_queue_io, you don't need any change in blk_mq_attempt_merge.

Also, why haven't you updated the non-multiqueue code paths similarly?

Cheers,
Jeff

author
#7 lore raw

Re: [PATCH 3/4] blk-mq: check bio_mergeable() early before merging

Quoted 24 lines · Jeff Moyer
Quoted 22 lines · Ming Lei

It isn't necessary to try to merge the bio which is marked as NOMERGE.

---
 block/blk-mq.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/blk-mq.c +3 -0
index 546b3b8..deb5f4c 100644
@@ -671,6 +671,9 @@ static bool blk_mq_attempt_merge(struct request_queue *q,
      struct request *rq;
      int checked = 8;
 
+     if (!bio_mergeable(bio))
+             return false;
+
      list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
              int el_ret;
 
Quoted 13 lines

block/blk-mq.c static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx, · line 1149

Quoted 9 lines
block/blk-mq.c +1 -1
@@ -1140,7 +1143,7 @@ static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
                                       struct blk_mq_ctx *ctx,
                                       struct request *rq, struct bio *bio)
 {
-     if (!hctx_allow_merges(hctx)) {
+     if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) {
              blk_mq_bio_to_request(rq, bio);
              spin_lock(&ctx->lock);
 insert_rq:

blk_mq_attempt_merge is only called from blk_mq_merge_queue_io. So, by adding the conditional in blk_mq_merge_queue_io, you don't need any change in blk_mq_attempt_merge.

OK.

Quoted 2 lines

Also, why haven't you updated the non-multiqueue code paths similarly?

Will do it in v1.

Quoted 8 lines

Cheers,
Jeff
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

author
#8 lore raw

PATCH 4/4 blk-mq: mark ctx as pending at batch in flush plug path

In reply to Ming Lei

Most of times, flush plug should be the hottest I/O path, so mark ctx as pending after all requests in the list are inserted.

---
 block/blk-mq.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
block/blk-mq.c +13 -5
index deb5f4c..1c943b9 100644
@@ -993,18 +993,25 @@ void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
 }
 EXPORT_SYMBOL(blk_mq_delay_queue);
 
-static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
-				    struct request *rq, bool at_head)
+static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
+					    struct blk_mq_ctx *ctx,
+					    struct request *rq,
+					    bool at_head)
 {
-	struct blk_mq_ctx *ctx = rq->mq_ctx;
-
 	trace_block_rq_insert(hctx->queue, rq);
 
 	if (at_head)
 		list_add(&rq->queuelist, &ctx->rq_list);
 	else
 		list_add_tail(&rq->queuelist, &ctx->rq_list);
+}
+
+static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
+				    struct request *rq, bool at_head)
+{
+	struct blk_mq_ctx *ctx = rq->mq_ctx;
 
+	__blk_mq_insert_req_list(hctx, ctx, rq, at_head);
 	blk_mq_hctx_mark_pending(hctx, ctx);
 }
 
@@ -1060,8 +1067,9 @@ static void blk_mq_insert_requests(struct request_queue *q,
 		rq = list_first_entry(list, struct request, queuelist);
 		list_del_init(&rq->queuelist);
 		rq->mq_ctx = ctx;
-		__blk_mq_insert_request(hctx, rq, false);
+		__blk_mq_insert_req_list(hctx, ctx, rq, false);
 	}
+	blk_mq_hctx_mark_pending(hctx, ctx);
 	spin_unlock(&ctx->lock);
 
 	blk_mq_run_hw_queue(hctx, from_schedule);
Signature · 1 line
1.9.1
#9 lore raw

Re: [PATCH 4/4] blk-mq: mark ctx as pending at batch in flush plug path

Quoted 3 lines · Ming Lei

Most of times, flush plug should be the hottest I/O path, so mark ctx as pending after all requests in the list are inserted.

Hi, Ming,

Did you see some performance gain from this?

-Jeff

Quoted 51 lines
---
 block/blk-mq.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
block/blk-mq.c +13 -5
index deb5f4c..1c943b9 100644
@@ -993,18 +993,25 @@ void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
 }
 EXPORT_SYMBOL(blk_mq_delay_queue);
 
-static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
-				    struct request *rq, bool at_head)
+static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
+					    struct blk_mq_ctx *ctx,
+					    struct request *rq,
+					    bool at_head)
 {
-	struct blk_mq_ctx *ctx = rq->mq_ctx;
-
 	trace_block_rq_insert(hctx->queue, rq);
 
 	if (at_head)
 		list_add(&rq->queuelist, &ctx->rq_list);
 	else
 		list_add_tail(&rq->queuelist, &ctx->rq_list);
+}
+
+static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
+				    struct request *rq, bool at_head)
+{
+	struct blk_mq_ctx *ctx = rq->mq_ctx;
 
+	__blk_mq_insert_req_list(hctx, ctx, rq, at_head);
 	blk_mq_hctx_mark_pending(hctx, ctx);
 }
 
@@ -1060,8 +1067,9 @@ static void blk_mq_insert_requests(struct request_queue *q,
 		rq = list_first_entry(list, struct request, queuelist);
 		list_del_init(&rq->queuelist);
 		rq->mq_ctx = ctx;
-		__blk_mq_insert_request(hctx, rq, false);
+		__blk_mq_insert_req_list(hctx, ctx, rq, false);
 	}
+	blk_mq_hctx_mark_pending(hctx, ctx);
 	spin_unlock(&ctx->lock);
 
 	blk_mq_run_hw_queue(hctx, from_schedule);
author
#10 lore raw

Re: [PATCH 4/4] blk-mq: mark ctx as pending at batch in flush plug path

Hi Jeff,

Quoted 9 lines · Jeff Moyer
Quoted 3 lines · Ming Lei

Most of times, flush plug should be the hottest I/O path, so mark ctx as pending after all requests in the list are inserted.

Hi, Ming,

Did you see some performance gain from this?

Not done the test yet, but I think it is the correct thing to do because the flag can be cleared quite frequently from other CPUs, and the numberof marking pending can be decreased to ~1/16 of previous one.

Quoted 59 lines

-Jeff

Quoted 41 lines
---
 block/blk-mq.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
block/blk-mq.c +11 -4
index deb5f4c..1c943b9 100644
@@ -993,18 +993,25 @@ void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
 }
 EXPORT_SYMBOL(blk_mq_delay_queue);
 
-static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
-                                 struct request *rq, bool at_head)
+static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
+                                         struct blk_mq_ctx *ctx,
+                                         struct request *rq,
+                                         bool at_head)
 {
-     struct blk_mq_ctx *ctx = rq->mq_ctx;
-
      trace_block_rq_insert(hctx->queue, rq);
 
      if (at_head)
              list_add(&rq->queuelist, &ctx->rq_list);
      else
              list_add_tail(&rq->queuelist, &ctx->rq_list);
+}
+
+static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
+                                 struct request *rq, bool at_head)
+{
+     struct blk_mq_ctx *ctx = rq->mq_ctx;
 
+     __blk_mq_insert_req_list(hctx, ctx, rq, at_head);
      blk_mq_hctx_mark_pending(hctx, ctx);
 }
 

block/blk-mq.c static void blk_mq_insert_requests(struct request_queue *q, · line 1075

Quoted 10 lines
block/blk-mq.c +2 -1
@@ -1060,8 +1067,9 @@ static void blk_mq_insert_requests(struct request_queue *q,
              rq = list_first_entry(list, struct request, queuelist);
              list_del_init(&rq->queuelist);
              rq->mq_ctx = ctx;
-             __blk_mq_insert_request(hctx, rq, false);
+             __blk_mq_insert_req_list(hctx, ctx, rq, false);
      }
+     blk_mq_hctx_mark_pending(hctx, ctx);
      spin_unlock(&ctx->lock);
 
      blk_mq_run_hw_queue(hctx, from_schedule);

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/