Skip to contentSkip to conversation

blk-mq: setup blk_mq_alloc_data.cmd_flags after submit_bio_checks() is done

PATCH In review Ming Lei 4 participants 12 messages Nov 12, 2021 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 blk-mq: setup blk_mq_alloc_data.cmd_flags after submit_bio_checks() is done

submit_bio_checks() may update bio->bi_opf, so we have to initialize blk_mq_alloc_data.cmd_flags after submit_bio_checks() returns when allocating new requests.

In case of using cached request, fallback to allocate new request if cached rq's cmd_flags isn't same with bio->bi_opf after submit_bio_checks().

Fixes 900e080752025f00 ("block: move queue enter logic into blk_mq_submit_bio()")
Cc Christoph Hellwig <[email protected]>
---
 block/blk-mq.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
block/blk-mq.c +4 -2
index f511db395c7f..f84044c8de3f 100644
@@ -2517,7 +2517,6 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	struct blk_mq_alloc_data data = {
 		.q		= q,
 		.nr_tags	= 1,
-		.cmd_flags	= bio->bi_opf,
 	};
 	struct request *rq;
 
@@ -2525,6 +2524,7 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 		return NULL;
 	if (unlikely(!submit_bio_checks(bio)))
 		goto put_exit;
+	data.cmd_flags	= bio->bi_opf;
 	if (blk_mq_attempt_bio_merge(q, bio, nsegs, same_queue_rq))
 		goto put_exit;
 
@@ -2564,13 +2564,15 @@ static inline struct request *blk_mq_get_request(struct request_queue *q,
 			if (blk_mq_attempt_bio_merge(q, bio, nsegs,
 						same_queue_rq))
 				return NULL;
+			if (bio->bi_opf != rq->cmd_flags)
+				goto fallback;
 			plug->cached_rq = rq_list_next(rq);
 			INIT_LIST_HEAD(&rq->queuelist);
 			rq_qos_throttle(q, bio);
 			return rq;
 		}
 	}
-
+fallback:
 	return blk_mq_get_new_requests(q, plug, bio, nsegs, same_queue_rq);
 }
 
Signature · 1 line
2.31.1
#1 lore raw

block/blk-mq.c static inline struct request *blk_mq_get_request(struct request_queue *q, · line 2568

Quoted 6 lines · Ming Lei
block/blk-mq.c +2 -0
@@ -2564,13 +2564,15 @@ static inline struct request *blk_mq_get_request(struct request_queue *q,
 			if (blk_mq_attempt_bio_merge(q, bio, nsegs,
 						same_queue_rq))
 				return NULL;
+			if (bio->bi_opf != rq->cmd_flags)
+				goto fallback;

I think this deserves a comment, as this means a read prealloc can only be used for reads, and no fua can be set if the preallocating I/O didn't use fua, etc.

What are the pitfalls of just chanigng cmd_flags?

author
#2 lore raw
Quoted 9 lines · Christoph Hellwig

block/blk-mq.c static inline struct request *blk_mq_get_request(struct request_queue *q, · line 2568

Quoted 6 lines · Ming Lei
block/blk-mq.c +2 -0
@@ -2564,13 +2564,15 @@ static inline struct request *blk_mq_get_request(struct request_queue *q,
 			if (blk_mq_attempt_bio_merge(q, bio, nsegs,
 						same_queue_rq))
 				return NULL;
+			if (bio->bi_opf != rq->cmd_flags)
+				goto fallback;

I think this deserves a comment, as this means a read prealloc

OK.

Quoted 4 lines

can only be used for reads, and no fua can be set if the preallocating I/O didn't use fua, etc.

What are the pitfalls of just chanigng cmd_flags?

Then we need to check cmd_flags carefully, such as hctx->type has to be same, flush & passthrough flags has to be same, that said all ->cmd_flags used for allocating rqs have to be same with the following bio->bi_opf.

In usual cases, I guess all IOs submitted from same plug batch should be same type. If not, we can switch to change cmd_flags.

Thanks,
Ming

#3 lore raw
Quoted 5 lines · Ming Lei
Quoted 4 lines

can only be used for reads, and no fua can be set if the preallocating I/O didn't use fua, etc.

What are the pitfalls of just chanigng cmd_flags?

Quoted 7 lines

Then we need to check cmd_flags carefully, such as hctx->type has to be same, flush & passthrough flags has to be same, that said all ->cmd_flags used for allocating rqs have to be same with the following bio->bi_opf.

In usual cases, I guess all IOs submitted from same plug batch should be same type. If not, we can switch to change cmd_flags.

Jens: is this a limit fitting into your use cases?

I guess as a quick fix this rejecting different flags is probably the best we can do for now, but I suspect we'll want to eventually relax them.

author
#4 lore raw
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Ming Lei
Quoted 4 lines

can only be used for reads, and no fua can be set if the preallocating I/O didn't use fua, etc.

What are the pitfalls of just chanigng cmd_flags?

Then we need to check cmd_flags carefully, such as hctx->type has to be same, flush & passthrough flags has to be same, that said all ->cmd_flags used for allocating rqs have to be same with the following bio->bi_opf.

Quoted 8 lines
Quoted 2 lines

In usual cases, I guess all IOs submitted from same plug batch should be same type. If not, we can switch to change cmd_flags.

Jens: is this a limit fitting into your use cases?

I guess as a quick fix this rejecting different flags is probably the best we can do for now, but I suspect we'll want to eventually relax them.

rw mixed workload will be affected, so I think we need to switch to change cmd_flags, how about the following patch?

From 9ab77b7adee768272944c20b7cffc8abdb85a35b Mon Sep 17 00:00:00 2001
From: Ming Lei <[email protected]>
Date: Fri, 12 Nov 2021 08:14:38 +0800
Subject: [PATCH] blk-mq: fix filesystem I/O request allocation

submit_bio_checks() may update bio->bi_opf, so we have to initialize blk_mq_alloc_data.cmd_flags with bio->bi_opf after submit_bio_checks() returns when allocating new request.

In case of using cached request, fallback to allocate new request if cached rq isn't compatible with the incoming bio, otherwise change rq->cmd_flags with incoming bio->bi_opf.

Fixes 900e080752025f00 ("block: move queue enter logic into blk_mq_submit_bio()")
Cc Christoph Hellwig <[email protected]>
---
 block/blk-mq.c | 39 ++++++++++++++++++++++++++++++---------
 block/blk-mq.h | 26 +++++++++++++++-----------
 2 files changed, 45 insertions(+), 20 deletions(-)
block/blk-mq.c +30 -9
index f511db395c7f..3ab34c4f20da 100644
@@ -2521,12 +2521,8 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	};
 	struct request *rq;
 
-	if (unlikely(bio_queue_enter(bio)))
-		return NULL;
-	if (unlikely(!submit_bio_checks(bio)))
-		goto put_exit;
 	if (blk_mq_attempt_bio_merge(q, bio, nsegs, same_queue_rq))
-		goto put_exit;
+		return NULL;
 
 	rq_qos_throttle(q, bio);
 
@@ -2543,19 +2539,32 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	rq_qos_cleanup(q, bio);
 	if (bio->bi_opf & REQ_NOWAIT)
 		bio_wouldblock_error(bio);
-put_exit:
-	blk_queue_exit(q);
+
 	return NULL;
 }
 
+static inline bool blk_mq_can_use_cached_rq(struct request *rq,
+		struct bio *bio)
+{
+	if (blk_mq_get_hctx_type(bio->bi_opf) != rq->mq_hctx->type)
+		return false;
+
+	if (op_is_flush(rq->cmd_flags) != op_is_flush(bio->bi_opf))
+		return false;
+
+	return true;
+}
+
 static inline struct request *blk_mq_get_request(struct request_queue *q,
 						 struct blk_plug *plug,
 						 struct bio *bio,
 						 unsigned int nsegs,
 						 bool *same_queue_rq)
 {
+	struct request *rq;
+	bool checked = false;
+
 	if (plug) {
-		struct request *rq;
 
 		rq = rq_list_peek(&plug->cached_rq);
 		if (rq && rq->q == q) {
@@ -2564,6 +2573,10 @@ static inline struct request *blk_mq_get_request(struct request_queue *q,
 			if (blk_mq_attempt_bio_merge(q, bio, nsegs,
 						same_queue_rq))
 				return NULL;
+			checked = true;
+			if (!blk_mq_can_use_cached_rq(rq, bio))
+				goto fallback;
+			rq->cmd_flags = bio->bi_opf;
 			plug->cached_rq = rq_list_next(rq);
 			INIT_LIST_HEAD(&rq->queuelist);
 			rq_qos_throttle(q, bio);
@@ -2571,7 +2584,15 @@ static inline struct request *blk_mq_get_request(struct request_queue *q,
 		}
 	}
 
-	return blk_mq_get_new_requests(q, plug, bio, nsegs, same_queue_rq);
+fallback:
+	if (unlikely(bio_queue_enter(bio)))
+		return NULL;
+	if (!checked && !submit_bio_checks(bio))
+		return NULL;
+	rq = blk_mq_get_new_requests(q, plug, bio, nsegs, same_queue_rq);
+	if (!rq)
+		blk_queue_exit(q);
+	return rq;
 }
 
 /**
block/blk-mq.h +15 -11
index cb0b5482ca5e..4d92fc1124ae 100644
@@ -89,15 +89,7 @@ static inline struct blk_mq_hw_ctx *blk_mq_map_queue_type(struct request_queue *
 	return q->queue_hw_ctx[q->tag_set->map[type].mq_map[cpu]];
 }
 
-/*
- * blk_mq_map_queue() - map (cmd_flags,type) to hardware queue
- * @q: request queue
- * @flags: request command flags
- * @ctx: software queue cpu ctx
- */
-static inline struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q,
-						     unsigned int flags,
-						     struct blk_mq_ctx *ctx)
+static inline enum hctx_type blk_mq_get_hctx_type(unsigned int flags)
 {
 	enum hctx_type type = HCTX_TYPE_DEFAULT;
 
@@ -108,8 +100,20 @@ static inline struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q,
 		type = HCTX_TYPE_POLL;
 	else if ((flags & REQ_OP_MASK) == REQ_OP_READ)
 		type = HCTX_TYPE_READ;
-	
-	return ctx->hctxs[type];
+	return type;
+}
+
+/*
+ * blk_mq_map_queue() - map (cmd_flags,type) to hardware queue
+ * @q: request queue
+ * @flags: request command flags
+ * @ctx: software queue cpu ctx
+ */
+static inline struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q,
+						     unsigned int flags,
+						     struct blk_mq_ctx *ctx)
+{
+	return ctx->hctxs[blk_mq_get_hctx_type(flags)];
 }
 
 /*
Signature · 5 lines
2.31.1


-- 
Ming
#5 lore raw

Hi Ming,

Quoted 33 lines · Ming Lei
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Ming Lei
Quoted 4 lines

can only be used for reads, and no fua can be set if the preallocating I/O didn't use fua, etc.

What are the pitfalls of just chanigng cmd_flags?

Then we need to check cmd_flags carefully, such as hctx->type has to be same, flush & passthrough flags has to be same, that said all ->cmd_flags used for allocating rqs have to be same with the following bio->bi_opf.

Quoted 8 lines
Quoted 2 lines

In usual cases, I guess all IOs submitted from same plug batch should be same type. If not, we can switch to change cmd_flags.

Jens: is this a limit fitting into your use cases?

I guess as a quick fix this rejecting different flags is probably the best we can do for now, but I suspect we'll want to eventually relax them.

rw mixed workload will be affected, so I think we need to switch to change cmd_flags, how about the following patch?

From 9ab77b7adee768272944c20b7cffc8abdb85a35b Mon Sep 17 00:00:00 2001
From: Ming Lei <[email protected]>
Date: Fri, 12 Nov 2021 08:14:38 +0800
Subject: [PATCH] blk-mq: fix filesystem I/O request allocation

submit_bio_checks() may update bio->bi_opf, so we have to initialize blk_mq_alloc_data.cmd_flags with bio->bi_opf after submit_bio_checks() returns when allocating new request.

Quoted 8 lines

In case of using cached request, fallback to allocate new request if cached rq isn't compatible with the incoming bio, otherwise change rq->cmd_flags with incoming bio->bi_opf.

Fixes 900e080752025f00 ("block: move queue enter logic into blk_mq_submit_bio()")
Cc Christoph Hellwig <[email protected]>

Thanks, this alternative patch seems to work fine, too.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

maintainer
#6 lore raw
In reply to Ming Lei
Quoted 74 lines · Ming Lei
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Ming Lei
Quoted 4 lines

can only be used for reads, and no fua can be set if the preallocating I/O didn't use fua, etc.

What are the pitfalls of just chanigng cmd_flags?

Then we need to check cmd_flags carefully, such as hctx->type has to be same, flush & passthrough flags has to be same, that said all ->cmd_flags used for allocating rqs have to be same with the following bio->bi_opf.

Quoted 8 lines
Quoted 2 lines

In usual cases, I guess all IOs submitted from same plug batch should be same type. If not, we can switch to change cmd_flags.

Jens: is this a limit fitting into your use cases?

I guess as a quick fix this rejecting different flags is probably the best we can do for now, but I suspect we'll want to eventually relax them.

rw mixed workload will be affected, so I think we need to switch to change cmd_flags, how about the following patch?

From 9ab77b7adee768272944c20b7cffc8abdb85a35b Mon Sep 17 00:00:00 2001
From: Ming Lei <[email protected]>
Date: Fri, 12 Nov 2021 08:14:38 +0800
Subject: [PATCH] blk-mq: fix filesystem I/O request allocation

submit_bio_checks() may update bio->bi_opf, so we have to initialize blk_mq_alloc_data.cmd_flags with bio->bi_opf after submit_bio_checks() returns when allocating new request.

In case of using cached request, fallback to allocate new request if cached rq isn't compatible with the incoming bio, otherwise change rq->cmd_flags with incoming bio->bi_opf.

Fixes 900e080752025f00 ("block: move queue enter logic into blk_mq_submit_bio()")
Cc Christoph Hellwig <[email protected]>
---
 block/blk-mq.c | 39 ++++++++++++++++++++++++++++++---------
 block/blk-mq.h | 26 +++++++++++++++-----------
 2 files changed, 45 insertions(+), 20 deletions(-)
block/blk-mq.c +2 -7
index f511db395c7f..3ab34c4f20da 100644
@@ -2521,12 +2521,8 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	};
 	struct request *rq;
 
-	if (unlikely(bio_queue_enter(bio)))
-		return NULL;
-	if (unlikely(!submit_bio_checks(bio)))
-		goto put_exit;
 	if (blk_mq_attempt_bio_merge(q, bio, nsegs, same_queue_rq))
-		goto put_exit;
+		return NULL;
 
 	rq_qos_throttle(q, bio);
 
@@ -2543,19 +2539,32 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	rq_qos_cleanup(q, bio);
 	if (bio->bi_opf & REQ_NOWAIT)
 		bio_wouldblock_error(bio);
-put_exit:
-	blk_queue_exit(q);
+
 	return NULL;
 }
 

block/blk-mq.c static struct request *blk_mq_get_new_requests(struct request_queue *q, · line 2555

Quoted 10 lines
block/blk-mq.c +10 -0
+static inline bool blk_mq_can_use_cached_rq(struct request *rq,
+		struct bio *bio)
+{
+	if (blk_mq_get_hctx_type(bio->bi_opf) != rq->mq_hctx->type)
+		return false;
+
+	if (op_is_flush(rq->cmd_flags) != op_is_flush(bio->bi_opf))
+		return false;
+
+	return true;

I think we should just check if hctx is the same, that should be enough. We don't need to match the type, just disallow if hw queue has changed.

Signature · 1 line
Jens Axboe
author
#7 lore raw
Quoted 75 lines · Jens Axboe
Quoted 74 lines · Ming Lei
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Ming Lei
Quoted 4 lines

can only be used for reads, and no fua can be set if the preallocating I/O didn't use fua, etc.

What are the pitfalls of just chanigng cmd_flags?

Then we need to check cmd_flags carefully, such as hctx->type has to be same, flush & passthrough flags has to be same, that said all ->cmd_flags used for allocating rqs have to be same with the following bio->bi_opf.

Quoted 8 lines
Quoted 2 lines

In usual cases, I guess all IOs submitted from same plug batch should be same type. If not, we can switch to change cmd_flags.

Jens: is this a limit fitting into your use cases?

I guess as a quick fix this rejecting different flags is probably the best we can do for now, but I suspect we'll want to eventually relax them.

rw mixed workload will be affected, so I think we need to switch to change cmd_flags, how about the following patch?

From 9ab77b7adee768272944c20b7cffc8abdb85a35b Mon Sep 17 00:00:00 2001
From: Ming Lei <[email protected]>
Date: Fri, 12 Nov 2021 08:14:38 +0800
Subject: [PATCH] blk-mq: fix filesystem I/O request allocation

submit_bio_checks() may update bio->bi_opf, so we have to initialize blk_mq_alloc_data.cmd_flags with bio->bi_opf after submit_bio_checks() returns when allocating new request.

In case of using cached request, fallback to allocate new request if cached rq isn't compatible with the incoming bio, otherwise change rq->cmd_flags with incoming bio->bi_opf.

Fixes 900e080752025f00 ("block: move queue enter logic into blk_mq_submit_bio()")
Cc Christoph Hellwig <[email protected]>
---
 block/blk-mq.c | 39 ++++++++++++++++++++++++++++++---------
 block/blk-mq.h | 26 +++++++++++++++-----------
 2 files changed, 45 insertions(+), 20 deletions(-)
block/blk-mq.c +2 -7
index f511db395c7f..3ab34c4f20da 100644
@@ -2521,12 +2521,8 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	};
 	struct request *rq;
 
-	if (unlikely(bio_queue_enter(bio)))
-		return NULL;
-	if (unlikely(!submit_bio_checks(bio)))
-		goto put_exit;
 	if (blk_mq_attempt_bio_merge(q, bio, nsegs, same_queue_rq))
-		goto put_exit;
+		return NULL;
 
 	rq_qos_throttle(q, bio);
 
@@ -2543,19 +2539,32 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	rq_qos_cleanup(q, bio);
 	if (bio->bi_opf & REQ_NOWAIT)
 		bio_wouldblock_error(bio);
-put_exit:
-	blk_queue_exit(q);
+
 	return NULL;
 }
 
Quoted 13 lines

block/blk-mq.c static struct request *blk_mq_get_new_requests(struct request_queue *q, · line 2555

Quoted 10 lines
block/blk-mq.c +10 -0
+static inline bool blk_mq_can_use_cached_rq(struct request *rq,
+		struct bio *bio)
+{
+	if (blk_mq_get_hctx_type(bio->bi_opf) != rq->mq_hctx->type)
+		return false;
+
+	if (op_is_flush(rq->cmd_flags) != op_is_flush(bio->bi_opf))
+		return false;
+
+	return true;

I think we should just check if hctx is the same, that should be enough. We don't need to match the type, just disallow if hw queue has changed.

But bio doesn't have hw queue. Figuring out exact hw queue seems necessary and needs more cpu cycles than getting hctx type.

Thanks,
Ming

maintainer
#8 lore raw
Quoted 90 lines · Ming Lei
Quoted 75 lines · Jens Axboe
Quoted 74 lines · Ming Lei
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Ming Lei
Quoted 4 lines

can only be used for reads, and no fua can be set if the preallocating I/O didn't use fua, etc.

What are the pitfalls of just chanigng cmd_flags?

Then we need to check cmd_flags carefully, such as hctx->type has to be same, flush & passthrough flags has to be same, that said all ->cmd_flags used for allocating rqs have to be same with the following bio->bi_opf.

Quoted 8 lines
Quoted 2 lines

In usual cases, I guess all IOs submitted from same plug batch should be same type. If not, we can switch to change cmd_flags.

Jens: is this a limit fitting into your use cases?

I guess as a quick fix this rejecting different flags is probably the best we can do for now, but I suspect we'll want to eventually relax them.

rw mixed workload will be affected, so I think we need to switch to change cmd_flags, how about the following patch?

From 9ab77b7adee768272944c20b7cffc8abdb85a35b Mon Sep 17 00:00:00 2001
From: Ming Lei <[email protected]>
Date: Fri, 12 Nov 2021 08:14:38 +0800
Subject: [PATCH] blk-mq: fix filesystem I/O request allocation

submit_bio_checks() may update bio->bi_opf, so we have to initialize blk_mq_alloc_data.cmd_flags with bio->bi_opf after submit_bio_checks() returns when allocating new request.

In case of using cached request, fallback to allocate new request if cached rq isn't compatible with the incoming bio, otherwise change rq->cmd_flags with incoming bio->bi_opf.

Fixes 900e080752025f00 ("block: move queue enter logic into blk_mq_submit_bio()")
Cc Christoph Hellwig <[email protected]>
---
 block/blk-mq.c | 39 ++++++++++++++++++++++++++++++---------
 block/blk-mq.h | 26 +++++++++++++++-----------
 2 files changed, 45 insertions(+), 20 deletions(-)
block/blk-mq.c +2 -7
index f511db395c7f..3ab34c4f20da 100644
@@ -2521,12 +2521,8 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	};
 	struct request *rq;
 
-	if (unlikely(bio_queue_enter(bio)))
-		return NULL;
-	if (unlikely(!submit_bio_checks(bio)))
-		goto put_exit;
 	if (blk_mq_attempt_bio_merge(q, bio, nsegs, same_queue_rq))
-		goto put_exit;
+		return NULL;
 
 	rq_qos_throttle(q, bio);
 
@@ -2543,19 +2539,32 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	rq_qos_cleanup(q, bio);
 	if (bio->bi_opf & REQ_NOWAIT)
 		bio_wouldblock_error(bio);
-put_exit:
-	blk_queue_exit(q);
+
 	return NULL;
 }
 
Quoted 13 lines

block/blk-mq.c static struct request *blk_mq_get_new_requests(struct request_queue *q, · line 2555

Quoted 10 lines
block/blk-mq.c +10 -0
+static inline bool blk_mq_can_use_cached_rq(struct request *rq,
+		struct bio *bio)
+{
+	if (blk_mq_get_hctx_type(bio->bi_opf) != rq->mq_hctx->type)
+		return false;
+
+	if (op_is_flush(rq->cmd_flags) != op_is_flush(bio->bi_opf))
+		return false;
+
+	return true;

I think we should just check if hctx is the same, that should be enough. We don't need to match the type, just disallow if hw queue has changed.

Quoted 2 lines

But bio doesn't have hw queue. Figuring out exact hw queue seems necessary and needs more cpu cycles than getting hctx type.

Thinking about it, if opf and request_queue matches, that should be enough.

Signature · 1 line
Jens Axboe
author
#9 lore raw
Quoted 91 lines · Jens Axboe
Quoted 90 lines · Ming Lei
Quoted 88 lines · Jens Axboe
Quoted 74 lines · Ming Lei
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Ming Lei
Quoted 4 lines

can only be used for reads, and no fua can be set if the preallocating I/O didn't use fua, etc.

What are the pitfalls of just chanigng cmd_flags?

Then we need to check cmd_flags carefully, such as hctx->type has to be same, flush & passthrough flags has to be same, that said all ->cmd_flags used for allocating rqs have to be same with the following bio->bi_opf.

Quoted 8 lines
Quoted 2 lines

In usual cases, I guess all IOs submitted from same plug batch should be same type. If not, we can switch to change cmd_flags.

Jens: is this a limit fitting into your use cases?

I guess as a quick fix this rejecting different flags is probably the best we can do for now, but I suspect we'll want to eventually relax them.

rw mixed workload will be affected, so I think we need to switch to change cmd_flags, how about the following patch?

From 9ab77b7adee768272944c20b7cffc8abdb85a35b Mon Sep 17 00:00:00 2001
From: Ming Lei <[email protected]>
Date: Fri, 12 Nov 2021 08:14:38 +0800
Subject: [PATCH] blk-mq: fix filesystem I/O request allocation

submit_bio_checks() may update bio->bi_opf, so we have to initialize blk_mq_alloc_data.cmd_flags with bio->bi_opf after submit_bio_checks() returns when allocating new request.

In case of using cached request, fallback to allocate new request if cached rq isn't compatible with the incoming bio, otherwise change rq->cmd_flags with incoming bio->bi_opf.

Fixes 900e080752025f00 ("block: move queue enter logic into blk_mq_submit_bio()")
Cc Christoph Hellwig <[email protected]>
---
 block/blk-mq.c | 39 ++++++++++++++++++++++++++++++---------
 block/blk-mq.h | 26 +++++++++++++++-----------
 2 files changed, 45 insertions(+), 20 deletions(-)
block/blk-mq.c +2 -7
index f511db395c7f..3ab34c4f20da 100644
@@ -2521,12 +2521,8 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	};
 	struct request *rq;
 
-	if (unlikely(bio_queue_enter(bio)))
-		return NULL;
-	if (unlikely(!submit_bio_checks(bio)))
-		goto put_exit;
 	if (blk_mq_attempt_bio_merge(q, bio, nsegs, same_queue_rq))
-		goto put_exit;
+		return NULL;
 
 	rq_qos_throttle(q, bio);
 
@@ -2543,19 +2539,32 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	rq_qos_cleanup(q, bio);
 	if (bio->bi_opf & REQ_NOWAIT)
 		bio_wouldblock_error(bio);
-put_exit:
-	blk_queue_exit(q);
+
 	return NULL;
 }
 

block/blk-mq.c static struct request *blk_mq_get_new_requests(struct request_queue *q, · line 2555

Quoted 10 lines
block/blk-mq.c +10 -0
+static inline bool blk_mq_can_use_cached_rq(struct request *rq,
+		struct bio *bio)
+{
+	if (blk_mq_get_hctx_type(bio->bi_opf) != rq->mq_hctx->type)
+		return false;
+
+	if (op_is_flush(rq->cmd_flags) != op_is_flush(bio->bi_opf))
+		return false;
+
+	return true;

I think we should just check if hctx is the same, that should be enough. We don't need to match the type, just disallow if hw queue has changed.

Quoted 5 lines
Quoted 2 lines

But bio doesn't have hw queue. Figuring out exact hw queue seems necessary and needs more cpu cycles than getting hctx type.

Thinking about it, if opf and request_queue matches, that should be enough.

I think that is same with hctx->type check: POLLED & OP needs to be same between the request and bio, and op_is_flush(), or could you explain how to run the exact check on opf?

Thanks,
Ming

maintainer
#10 lore raw
Quoted 95 lines · Ming Lei
Quoted 94 lines · Jens Axboe
Quoted 92 lines · Ming Lei
Quoted 75 lines · Jens Axboe
Quoted 74 lines · Ming Lei
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Ming Lei
Quoted 4 lines

can only be used for reads, and no fua can be set if the preallocating I/O didn't use fua, etc.

What are the pitfalls of just chanigng cmd_flags?

Then we need to check cmd_flags carefully, such as hctx->type has to be same, flush & passthrough flags has to be same, that said all ->cmd_flags used for allocating rqs have to be same with the following bio->bi_opf.

Quoted 8 lines
Quoted 2 lines

In usual cases, I guess all IOs submitted from same plug batch should be same type. If not, we can switch to change cmd_flags.

Jens: is this a limit fitting into your use cases?

I guess as a quick fix this rejecting different flags is probably the best we can do for now, but I suspect we'll want to eventually relax them.

rw mixed workload will be affected, so I think we need to switch to change cmd_flags, how about the following patch?

From 9ab77b7adee768272944c20b7cffc8abdb85a35b Mon Sep 17 00:00:00 2001
From: Ming Lei <[email protected]>
Date: Fri, 12 Nov 2021 08:14:38 +0800
Subject: [PATCH] blk-mq: fix filesystem I/O request allocation

submit_bio_checks() may update bio->bi_opf, so we have to initialize blk_mq_alloc_data.cmd_flags with bio->bi_opf after submit_bio_checks() returns when allocating new request.

In case of using cached request, fallback to allocate new request if cached rq isn't compatible with the incoming bio, otherwise change rq->cmd_flags with incoming bio->bi_opf.

Fixes 900e080752025f00 ("block: move queue enter logic into blk_mq_submit_bio()")
Cc Christoph Hellwig <[email protected]>
---
 block/blk-mq.c | 39 ++++++++++++++++++++++++++++++---------
 block/blk-mq.h | 26 +++++++++++++++-----------
 2 files changed, 45 insertions(+), 20 deletions(-)
block/blk-mq.c +2 -7
index f511db395c7f..3ab34c4f20da 100644
@@ -2521,12 +2521,8 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	};
 	struct request *rq;
 
-	if (unlikely(bio_queue_enter(bio)))
-		return NULL;
-	if (unlikely(!submit_bio_checks(bio)))
-		goto put_exit;
 	if (blk_mq_attempt_bio_merge(q, bio, nsegs, same_queue_rq))
-		goto put_exit;
+		return NULL;
 
 	rq_qos_throttle(q, bio);
 
@@ -2543,19 +2539,32 @@ static struct request *blk_mq_get_new_requests(struct request_queue *q,
 	rq_qos_cleanup(q, bio);
 	if (bio->bi_opf & REQ_NOWAIT)
 		bio_wouldblock_error(bio);
-put_exit:
-	blk_queue_exit(q);
+
 	return NULL;
 }
 
Quoted 13 lines

block/blk-mq.c static struct request *blk_mq_get_new_requests(struct request_queue *q, · line 2555

Quoted 10 lines
block/blk-mq.c +10 -0
+static inline bool blk_mq_can_use_cached_rq(struct request *rq,
+		struct bio *bio)
+{
+	if (blk_mq_get_hctx_type(bio->bi_opf) != rq->mq_hctx->type)
+		return false;
+
+	if (op_is_flush(rq->cmd_flags) != op_is_flush(bio->bi_opf))
+		return false;
+
+	return true;

I think we should just check if hctx is the same, that should be enough. We don't need to match the type, just disallow if hw queue has changed.

But bio doesn't have hw queue. Figuring out exact hw queue seems necessary and needs more cpu cycles than getting hctx type.

Quoted 6 lines
Quoted 2 lines

Thinking about it, if opf and request_queue matches, that should be enough.

I think that is same with hctx->type check: POLLED & OP needs to be same between the request and bio, and op_is_flush(), or could you explain how to run the exact check on opf?

I took a look at it, and I think your approach of checking the type is indeed the best one.

Signature · 1 line
Jens Axboe
maintainer
#11 lore raw
In reply to Christoph Hellwig
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Ming Lei
Quoted 4 lines

can only be used for reads, and no fua can be set if the preallocating I/O didn't use fua, etc.

What are the pitfalls of just chanigng cmd_flags?

Then we need to check cmd_flags carefully, such as hctx->type has to be same, flush & passthrough flags has to be same, that said all ->cmd_flags used for allocating rqs have to be same with the following bio->bi_opf.

Quoted 4 lines
Quoted 2 lines

In usual cases, I guess all IOs submitted from same plug batch should be same type. If not, we can switch to change cmd_flags.

Jens: is this a limit fitting into your use cases?

It's not a big problem for my use case, as that ones all the same type of IO. And in general I don't expect a lot of mixed uses here, I guess the main issue might be a plug for writes and an initial meta data read will "polute" the request cache with requests that we won't be using. But probably not a big enough thing to worry about in terms of efficiency.

Quoted 3 lines

I guess as a quick fix this rejecting different flags is probably the best we can do for now, but I suspect we'll want to eventually relax them.

Agree on both.

Signature · 1 line
Jens Axboe