Skip to contentSkip to conversation

blk-mq/nvme: cancel request synchronously

PATCH Superseded Ming Lei 3 participants 5 messages Apr 8, 2019

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

v3 cover blk-mq/nvme: cancel request synchronously

Hi,

This patchset introduces blk_mq_complete_request_sync() for canceling request synchronously in error handler context, then one race between completing request remotely and destroying contoller/queues can be fixed.

V3:
- avoid extra cost to blk_mq_complete_request

V2:
- export via EXPORT_SYMBOL_GPL
- minor commit log change

Ming Lei (2):
blk-mq: introduce blk_mq_complete_request_sync()
nvme: cancel request synchronously

 block/blk-mq.c           | 11 +++++++++++
 drivers/nvme/host/core.c |  2 +-
 include/linux/blk-mq.h   |  1 +
 3 files changed, 13 insertions(+), 1 deletion(-)
Cc Keith Busch <kbusch at kernel.org>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Bart Van Assche <bvanassche at acm.org>
Cc James Smart <james.smart at broadcom.com>
Cc Christoph Hellwig <hch at lst.de>
Cc linux-nvme at lists.infradead.org
Signature · 1 line
2.9.5
author
#1 lore raw

PATCH v3 1/2 blk-mq: introduce blk_mq_complete_request_sync()

In NVMe's error handler, follows the typical steps of tearing down hardware for recovering controller:

1) stop blk_mq hw queues
2) stop the real hw queues
3) cancel in-flight requests via blk_mq_tagset_busy_iter(tags, cancel_request, ...)
cancel_request():

	mark the request as abort
	blk_mq_complete_request(req);

4) destroy real hw queues

However, there may be race between #3 and #4, because blk_mq_complete_request() may run q->mq_ops->complete(rq) remotelly and asynchronously, and ->complete(rq) may be run after #4.

This patch introduces blk_mq_complete_request_sync() for fixing the above race.

Cc Keith Busch <kbusch at kernel.org>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Bart Van Assche <bvanassche at acm.org>
Cc James Smart <james.smart at broadcom.com>
Cc Christoph Hellwig <hch at lst.de>
Cc linux-nvme at lists.infradead.org
Signed-off-by Ming Lei <ming.lei at redhat.com>
---
 block/blk-mq.c         | 11 +++++++++++
 include/linux/blk-mq.h |  1 +
 2 files changed, 12 insertions(+)
block/blk-mq.c +11 -0
index a9354835cf51..d8d89f3514ac 100644
@@ -654,6 +654,17 @@ bool blk_mq_complete_request(struct request *rq)
 }
 EXPORT_SYMBOL(blk_mq_complete_request);
 
+bool blk_mq_complete_request_sync(struct request *rq)
+{
+	if (unlikely(blk_should_fake_timeout(rq->q)))
+		return false;
+
+	WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
+	rq->q->mq_ops->complete(rq);
+	return true;
+}
+EXPORT_SYMBOL_GPL(blk_mq_complete_request_sync);
+
 int blk_mq_request_started(struct request *rq)
 {
 	return blk_mq_rq_state(rq) != MQ_RQ_IDLE;
include/linux/blk-mq.h +1 -0
index cb2aa7ecafff..1412c983e7b8 100644
@@ -302,6 +302,7 @@ void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list);
 void blk_mq_kick_requeue_list(struct request_queue *q);
 void blk_mq_delay_kick_requeue_list(struct request_queue *q, unsigned long msecs);
 bool blk_mq_complete_request(struct request *rq);
+bool blk_mq_complete_request_sync(struct request *rq);
 bool blk_mq_bio_list_merge(struct request_queue *q, struct list_head *list,
 			   struct bio *bio);
 bool blk_mq_queue_stopped(struct request_queue *q);
Signature · 1 line
2.9.5
maintainer
#2 lore raw

Re: [PATCH V3 1/2] blk-mq: introduce blk_mq_complete_request_sync()

Quoted 39 lines · Ming Lei

In NVMe's error handler, follows the typical steps of tearing down hardware for recovering controller:

1) stop blk_mq hw queues
2) stop the real hw queues
3) cancel in-flight requests via blk_mq_tagset_busy_iter(tags, cancel_request, ...)
cancel_request():

	mark the request as abort
	blk_mq_complete_request(req);

4) destroy real hw queues

However, there may be race between #3 and #4, because blk_mq_complete_request() may run q->mq_ops->complete(rq) remotelly and asynchronously, and ->complete(rq) may be run after #4.

This patch introduces blk_mq_complete_request_sync() for fixing the above race.

Cc Keith Busch <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Bart Van Assche <[email protected]>
Cc James Smart <[email protected]>
Cc Christoph Hellwig <[email protected]>
---
 block/blk-mq.c         | 11 +++++++++++
 include/linux/blk-mq.h |  1 +
 2 files changed, 12 insertions(+)
block/blk-mq.c +0 -0
index a9354835cf51..d8d89f3514ac 100644
@@ -654,6 +654,17 @@ bool blk_mq_complete_request(struct request *rq)
 }
 EXPORT_SYMBOL(blk_mq_complete_request);
 

block/blk-mq.c bool blk_mq_complete_request(struct request *rq) · line 666

Quoted 10 lines
block/blk-mq.c +10 -0
+bool blk_mq_complete_request_sync(struct request *rq)
+{
+	if (unlikely(blk_should_fake_timeout(rq->q)))
+		return false;
+
+	WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
+	rq->q->mq_ops->complete(rq);
+	return true;
+}
+EXPORT_SYMBOL_GPL(blk_mq_complete_request_sync);

Could we possibly drop the fake timeout in this path? We're using this in error handling that is past pretending completing requests didn't happen.

Otherwise this all looks good to me.

#3 lore raw

Re: [PATCH V3 1/2] blk-mq: introduce blk_mq_complete_request_sync()

Quoted 6 lines · Keith Busch
Quoted 6 lines
block/blk-mq.c +6 -0
+bool blk_mq_complete_request_sync(struct request *rq)
+{
+	if (unlikely(blk_should_fake_timeout(rq->q)))
+		return false;
+
+	WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
Quoted 8 lines

block/blk-mq.c bool blk_mq_complete_request(struct request *rq) · line 666

Quoted 4 lines
block/blk-mq.c +4 -0
+	rq->q->mq_ops->complete(rq);
+	return true;
+}
+EXPORT_SYMBOL_GPL(blk_mq_complete_request_sync);

Could we possibly drop the fake timeout in this path? We're using this in error handling that is past pretending completing requests didn't happen.

.. and at that point we can also drop the return value.

author
#4 lore raw

PATCH v3 2/2 nvme: cancel request synchronously

In reply to Ming Lei

nvme_cancel_request() is used in error handler, and it is always reliable to cancel request synchronously, and avoids possible race in which request may be completed after real hw queue is destroyed.

One issue is reported by our customer on NVMe RDMA, in which freed ib queue pair may be used in nvme_rdma_complete_rq().

Cc Keith Busch <kbusch at kernel.org>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Bart Van Assche <bvanassche at acm.org>
Cc James Smart <james.smart at broadcom.com>
Cc Christoph Hellwig <hch at lst.de>
Cc linux-nvme at lists.infradead.org
Reviewed-by Christoph Hellwig <hch at lst.de>
Signed-off-by Ming Lei <ming.lei at redhat.com>
---
 drivers/nvme/host/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/host/core.c +1 -1
index 470601980794..2c43e12b70af 100644
@@ -288,7 +288,7 @@ bool nvme_cancel_request(struct request *req, void *data, bool reserved)
 				"Cancelling I/O %d", req->tag);
 
 	nvme_req(req)->status = NVME_SC_ABORT_REQ;
-	blk_mq_complete_request(req);
+	blk_mq_complete_request_sync(req);
 	return true;
 }
 EXPORT_SYMBOL_GPL(nvme_cancel_request);
Signature · 1 line
2.9.5