Skip to contentSkip to conversation

blk-mq: wait until completed req's complete fn is run

PATCH Superseded Ming Lei 6 participants 25 messages Jul 22, 2019 – Jul 24, 2019

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

cover blk-mq: wait until completed req's complete fn is run

Hi,

blk-mq may schedule to call queue's complete function on remote CPU via IPI, but never provide any way to synchronize the request's complete fn.

In some driver's EH(such as NVMe), hardware queue's resource may be freed & re-allocated. If the completed request's complete fn is run finally after the hardware queue's resource is released, kernel crash will be triggered.

Fixes this issue by waitting until completed req's complete fn is run.

Thanks,
Ming

Ming Lei (5):
blk-mq: introduce blk_mq_request_completed()
blk-mq: introduce blk_mq_tagset_wait_completed_request()
nvme: don't abort completed request in nvme_cancel_request
nvme: wait until all completed request's complete fn is called
blk-mq: remove blk_mq_complete_request_sync

 block/blk-mq-tag.c         | 32 ++++++++++++++++++++++++++++++++
 block/blk-mq.c             | 13 ++++++-------
 drivers/nvme/host/core.c   |  6 +++++-
 drivers/nvme/host/pci.c    |  2 ++
 drivers/nvme/host/rdma.c   |  8 ++++++--
 drivers/nvme/host/tcp.c    |  8 ++++++--
 drivers/nvme/target/loop.c |  2 ++
 include/linux/blk-mq.h     |  3 ++-
 8 files changed, 61 insertions(+), 13 deletions(-)
Cc Max Gurtovoy <maxg at mellanox.com>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Keith Busch <keith.busch at intel.com>
Cc Christoph Hellwig <hch at lst.de>
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH 1/5 blk-mq: introduce blk_mq_request_completed()

NVMe needs this function to decide if one request to be aborted has been completed in normal IO path already.

So introduce it.

Cc Max Gurtovoy <maxg at mellanox.com>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Keith Busch <keith.busch at intel.com>
Cc Christoph Hellwig <hch at lst.de>
Signed-off-by Ming Lei <ming.lei at redhat.com>
---
 block/blk-mq.c         | 6 ++++++
 include/linux/blk-mq.h | 1 +
 2 files changed, 7 insertions(+)
block/blk-mq.c +6 -0
index b038ec680e84..e1d0b4567388 100644
@@ -665,6 +665,12 @@ int blk_mq_request_started(struct request *rq)
 }
 EXPORT_SYMBOL_GPL(blk_mq_request_started);
 
+int blk_mq_request_completed(struct request *rq)
+{
+	return blk_mq_rq_state(rq) == MQ_RQ_COMPLETE;
+}
+EXPORT_SYMBOL_GPL(blk_mq_request_completed);
+
 void blk_mq_start_request(struct request *rq)
 {
 	struct request_queue *q = rq->q;
include/linux/blk-mq.h +1 -0
index 3fa1fa59f9b2..baac2926e54a 100644
@@ -296,6 +296,7 @@ static inline u16 blk_mq_unique_tag_to_tag(u32 unique_tag)
 
 
 int blk_mq_request_started(struct request *rq);
+int blk_mq_request_completed(struct request *rq);
 void blk_mq_start_request(struct request *rq);
 void blk_mq_end_request(struct request *rq, blk_status_t error);
 void __blk_mq_end_request(struct request *rq, blk_status_t error);
Signature · 1 line
2.20.1
maintainer Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] blk-mq: introduce blk_mq_request_completed()

Reviewed-by Sagi Grimberg <sagi at grimberg.me>
author
#3 lore raw

PATCH 2/5 blk-mq: introduce blk_mq_tagset_wait_completed_request()

In reply to Ming Lei

blk-mq may schedule to call queue's complete function on remote CPU via IPI, but doesn't provide any way to synchronize the request's complete fn.

In some driver's EH(such as NVMe), hardware queue's resource may be freed & re-allocated. If the completed request's complete fn is run finally after the hardware queue's resource is released, kernel crash will be triggered.

Prepare for fixing this kind of issue by introducing blk_mq_tagset_wait_completed_request().

Cc Max Gurtovoy <maxg at mellanox.com>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Keith Busch <keith.busch at intel.com>
Cc Christoph Hellwig <hch at lst.de>
Signed-off-by Ming Lei <ming.lei at redhat.com>
---
 block/blk-mq-tag.c     | 32 ++++++++++++++++++++++++++++++++
 include/linux/blk-mq.h |  1 +
 2 files changed, 33 insertions(+)
block/blk-mq-tag.c +32 -0
index da19f0bc8876..008388e82b5c 100644
@@ -10,6 +10,7 @@
 #include <linux/module.h>
 
 #include <linux/blk-mq.h>
+#include <linux/delay.h>
 #include "blk.h"
 #include "blk-mq.h"
 #include "blk-mq-tag.h"
@@ -354,6 +355,37 @@ void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
 }
 EXPORT_SYMBOL(blk_mq_tagset_busy_iter);
 
+static bool blk_mq_tagset_count_completed_rqs(struct request *rq,
+		void *data, bool reserved)
+{
+	unsigned *count = data;
+
+	if (blk_mq_request_completed(rq))
+		(*count)++;
+	return true;
+}
+
+/**
+ * blk_mq_tagset_wait_completed_request - wait until all completed req's
+ * complete funtion is run
+ * @tagset:	Tag set to drain completed request
+ *
+ * Note: This function has to be run after all IO queues are shutdown
+ */
+void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset)
+{
+	while (true) {
+		unsigned count = 0;
+
+		blk_mq_tagset_busy_iter(tagset,
+				blk_mq_tagset_count_completed_rqs, &count);
+		if (!count)
+			break;
+		msleep(5);
+	}
+}
+EXPORT_SYMBOL(blk_mq_tagset_wait_completed_request);
+
 /**
  * blk_mq_queue_tag_busy_iter - iterate over all requests with a driver tag
  * @q:		Request queue to examine.
include/linux/blk-mq.h +1 -0
index baac2926e54a..ee0719b649b6 100644
@@ -322,6 +322,7 @@ bool blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async);
 void blk_mq_run_hw_queues(struct request_queue *q, bool async);
 void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
 		busy_tag_iter_fn *fn, void *priv);
+void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset);
 void blk_mq_freeze_queue(struct request_queue *q);
 void blk_mq_unfreeze_queue(struct request_queue *q);
 void blk_freeze_queue_start(struct request_queue *q);
Signature · 1 line
2.20.1
#4 lore raw

Re: [PATCH 2/5] blk-mq: introduce blk_mq_tagset_wait_completed_request()

Quoted 10 lines · Ming Lei

blk-mq may schedule to call queue's complete function on remote CPU via IPI, but doesn't provide any way to synchronize the request's complete fn.

In some driver's EH(such as NVMe), hardware queue's resource may be freed & re-allocated. If the completed request's complete fn is run finally after the hardware queue's resource is released, kernel crash will be triggered.

Prepare for fixing this kind of issue by introducing blk_mq_tagset_wait_completed_request().

An explanation is missing of why the block layer is modified to fix this instead of the NVMe driver.

Thanks,

Bart.

author
#5 lore raw

Re: [PATCH 2/5] blk-mq: introduce blk_mq_tagset_wait_completed_request()

Quoted 9 lines · Bart Van Assche
Quoted 8 lines · Ming Lei

blk-mq may schedule to call queue's complete function on remote CPU via IPI, but doesn't provide any way to synchronize the request's complete fn.

In some driver's EH(such as NVMe), hardware queue's resource may be freed & re-allocated. If the completed request's complete fn is run finally after the hardware queue's resource is released, kernel crash will be triggered.

Quoted 5 lines
Quoted 2 lines

Prepare for fixing this kind of issue by introducing blk_mq_tagset_wait_completed_request().

An explanation is missing of why the block layer is modified to fix this instead of the NVMe driver.

The above commit log has explained that there isn't sync mechanism in blk-mq wrt. request completion, and there might be similar issue in other future drivers.

Thanks,
Ming

#6 lore raw

Re: [PATCH 2/5] blk-mq: introduce blk_mq_tagset_wait_completed_request()

Quoted 13 lines · Ming Lei
Quoted 12 lines · Bart Van Assche
Quoted 10 lines · Ming Lei

blk-mq may schedule to call queue's complete function on remote CPU via IPI, but doesn't provide any way to synchronize the request's complete fn.

In some driver's EH(such as NVMe), hardware queue's resource may be freed & re-allocated. If the completed request's complete fn is run finally after the hardware queue's resource is released, kernel crash will be triggered.

Prepare for fixing this kind of issue by introducing blk_mq_tagset_wait_completed_request().

Quoted 6 lines
Quoted 2 lines

An explanation is missing of why the block layer is modified to fix this instead of the NVMe driver.

The above commit log has explained that there isn't sync mechanism in blk-mq wrt. request completion, and there might be similar issue in other future drivers.

That is not sufficient as a motivation to modify the block layer because there is already a way to wait until request completions have finished, namely the request queue freeze mechanism. Have you considered to use that mechanism instead of introducing blk_mq_tagset_wait_completed_request()?

Thanks,

Bart.

author
#7 lore raw

Re: [PATCH 2/5] blk-mq: introduce blk_mq_tagset_wait_completed_request()

Quoted 17 lines · Bart Van Assche
Quoted 16 lines · Ming Lei
Quoted 14 lines · Bart Van Assche
Quoted 10 lines · Ming Lei

blk-mq may schedule to call queue's complete function on remote CPU via IPI, but doesn't provide any way to synchronize the request's complete fn.

In some driver's EH(such as NVMe), hardware queue's resource may be freed & re-allocated. If the completed request's complete fn is run finally after the hardware queue's resource is released, kernel crash will be triggered.

Prepare for fixing this kind of issue by introducing blk_mq_tagset_wait_completed_request().

An explanation is missing of why the block layer is modified to fix this instead of the NVMe driver.

Quoted 8 lines
Quoted 3 lines

The above commit log has explained that there isn't sync mechanism in blk-mq wrt. request completion, and there might be similar issue in other future drivers.

That is not sufficient as a motivation to modify the block layer because there is already a way to wait until request completions have finished, namely the request queue freeze mechanism. Have you considered to use that mechanism instead of introducing blk_mq_tagset_wait_completed_request()?

The introduced interface is used in EH, during which the aborted requests will stay at blk-mq sw/scheduler queue, so queue freeze will cause deadlock. We simply can't use it.

Thanks,
Ming

maintainer Reviewed-by on v1 2/5
#8 lore raw

Re: [PATCH 2/5] blk-mq: introduce blk_mq_tagset_wait_completed_request()

In reply to Ming Lei
Reviewed-by Sagi Grimberg <sagi at grimberg.me>
author
#9 lore raw

PATCH 3/5 nvme: don't abort completed request in nvme_cancel_request

In reply to Ming Lei

Before aborting in-flight requests, all IO queues have been shutdown. However, request's completion fn may not be done yet because it may be scheduled to run via IPI.

So don't abort one request if it is marked as completed, otherwise we may abort one normal completed request.

Cc Max Gurtovoy <maxg at mellanox.com>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Keith Busch <keith.busch at intel.com>
Cc Christoph Hellwig <hch at lst.de>
Signed-off-by Ming Lei <ming.lei at redhat.com>
---
 drivers/nvme/host/core.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/nvme/host/core.c +4 -0
index cc09b81fc7f4..cb8007cce4d1 100644
@@ -285,6 +285,10 @@ EXPORT_SYMBOL_GPL(nvme_complete_rq);
 
 bool nvme_cancel_request(struct request *req, void *data, bool reserved)
 {
+	/* don't abort one completed request */
+	if (blk_mq_request_completed(req))
+		return;
+
 	dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
 				"Cancelling I/O %d", req->tag);
 
Signature · 1 line
2.20.1
#10 lore raw

Re: [PATCH 3/5] nvme: don't abort completed request in nvme_cancel_request

Quoted 21 lines · Ming Lei

Before aborting in-flight requests, all IO queues have been shutdown. However, request's completion fn may not be done yet because it may be scheduled to run via IPI.

So don't abort one request if it is marked as completed, otherwise we may abort one normal completed request.

Cc Max Gurtovoy <maxg at mellanox.com>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Keith Busch <keith.busch at intel.com>
Cc Christoph Hellwig <hch at lst.de>
Signed-off-by Ming Lei <ming.lei at redhat.com>
---
  drivers/nvme/host/core.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/nvme/host/core.c +0 -0
index cc09b81fc7f4..cb8007cce4d1 100644

drivers/nvme/host/core.c EXPORT_SYMBOL_GPL(nvme_complete_rq); · line 293

Quoted 9 lines
drivers/nvme/host/core.c +4 -0
@@ -285,6 +285,10 @@ EXPORT_SYMBOL_GPL(nvme_complete_rq);
 
  bool nvme_cancel_request(struct request *req, void *data, bool reserved)
  {
+	/* don't abort one completed request */
+	if (blk_mq_request_completed(req))
+		return;
+
  	dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
  				"Cancelling I/O %d", req->tag);

Something I probably already asked before: what prevents that nvme_cancel_request() is executed concurrently with the completion handler of the same request?

Thanks,

Bart.

#11 lore raw

Re: [PATCH 3/5] nvme: don't abort completed request in nvme_cancel_request

Quoted 23 lines · Bart Van Assche
Quoted 22 lines · Ming Lei

Before aborting in-flight requests, all IO queues have been shutdown. However, request's completion fn may not be done yet because it may be scheduled to run via IPI.

So don't abort one request if it is marked as completed, otherwise we may abort one normal completed request.

Cc Max Gurtovoy <maxg at mellanox.com>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Keith Busch <keith.busch at intel.com>
Cc Christoph Hellwig <hch at lst.de>
Signed-off-by Ming Lei <ming.lei at redhat.com>
---
  drivers/nvme/host/core.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/nvme/host/core.c +0 -0
index cc09b81fc7f4..cb8007cce4d1 100644
@@ -285,6 +285,10 @@ EXPORT_SYMBOL_GPL(nvme_complete_rq);
 
Quoted 12 lines

drivers/nvme/host/core.c EXPORT_SYMBOL_GPL(nvme_complete_rq); · line 293

Quoted 8 lines
drivers/nvme/host/core.c +4 -0
  bool nvme_cancel_request(struct request *req, void *data, bool reserved)
  {
+     /* don't abort one completed request */
+     if (blk_mq_request_completed(req))
+             return;
+
      dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
                              "Cancelling I/O %d", req->tag);

Something I probably already asked before: what prevents that nvme_cancel_request() is executed concurrently with the completion handler of the same request?

At least for pci, we've shutdown the queues and their interrupts prior to tagset iteration, so we can't concurrently execute a natural completion for in-flight requests while cancelling them.

maintainer
#12 lore raw

Re: [PATCH 3/5] nvme: don't abort completed request in nvme_cancel_request

Quoted 37 lines
Quoted 24 lines · Ming Lei
Quoted 22 lines

Before aborting in-flight requests, all IO queues have been shutdown. However, request's completion fn may not be done yet because it may be scheduled to run via IPI.

So don't abort one request if it is marked as completed, otherwise we may abort one normal completed request.

Cc Max Gurtovoy <maxg at mellanox.com>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Keith Busch <keith.busch at intel.com>
Cc Christoph Hellwig <hch at lst.de>
Signed-off-by Ming Lei <ming.lei at redhat.com>
---
   drivers/nvme/host/core.c | 4 ++++
   1 file changed, 4 insertions(+)
drivers/nvme/host/core.c +0 -0
index cc09b81fc7f4..cb8007cce4d1 100644
@@ -285,6 +285,10 @@ EXPORT_SYMBOL_GPL(nvme_complete_rq);
 
Quoted 12 lines

drivers/nvme/host/core.c EXPORT_SYMBOL_GPL(nvme_complete_rq); · line 293

Quoted 8 lines
drivers/nvme/host/core.c +4 -0
   bool nvme_cancel_request(struct request *req, void *data, bool reserved)
   {
+     /* don't abort one completed request */
+     if (blk_mq_request_completed(req))
+             return;
+
       dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
                               "Cancelling I/O %d", req->tag);

Something I probably already asked before: what prevents that nvme_cancel_request() is executed concurrently with the completion handler of the same request?

Quoted 3 lines

At least for pci, we've shutdown the queues and their interrupts prior to tagset iteration, so we can't concurrently execute a natural completion for in-flight requests while cancelling them.

Same for tcp and rdma.

author
#13 lore raw

Re: [PATCH 3/5] nvme: don't abort completed request in nvme_cancel_request

In reply to Bart Van Assche
Quoted 26 lines · Bart Van Assche
Quoted 25 lines · Ming Lei

Before aborting in-flight requests, all IO queues have been shutdown. However, request's completion fn may not be done yet because it may be scheduled to run via IPI.

So don't abort one request if it is marked as completed, otherwise we may abort one normal completed request.

Cc Max Gurtovoy <maxg at mellanox.com>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Keith Busch <keith.busch at intel.com>
Cc Christoph Hellwig <hch at lst.de>
Signed-off-by Ming Lei <ming.lei at redhat.com>
---
  drivers/nvme/host/core.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/nvme/host/core.c +2 -0
index cc09b81fc7f4..cb8007cce4d1 100644
@@ -285,6 +285,10 @@ EXPORT_SYMBOL_GPL(nvme_complete_rq);
  bool nvme_cancel_request(struct request *req, void *data, bool reserved)
  {
+	/* don't abort one completed request */
+	if (blk_mq_request_completed(req))
Quoted 8 lines

drivers/nvme/host/core.c EXPORT_SYMBOL_GPL(nvme_complete_rq); · line 293

Quoted 4 lines
drivers/nvme/host/core.c +2 -0
+		return;
+
  	dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
  				"Cancelling I/O %d", req->tag);

Something I probably already asked before: what prevents that nvme_cancel_request() is executed concurrently with the completion handler of the same request?

The commit log did mention the point:

Before aborting in-flight requests, all IO queues have been shutdown.

which implies that no concurrent normal completion.

Thanks,
Ming

#14 lore raw

Re: [PATCH 3/5] nvme: don't abort completed request in nvme_cancel_request

Quoted 36 lines · Ming Lei
Quoted 26 lines · Bart Van Assche
Quoted 25 lines · Ming Lei

Before aborting in-flight requests, all IO queues have been shutdown. However, request's completion fn may not be done yet because it may be scheduled to run via IPI.

So don't abort one request if it is marked as completed, otherwise we may abort one normal completed request.

Cc Max Gurtovoy <maxg at mellanox.com>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Keith Busch <keith.busch at intel.com>
Cc Christoph Hellwig <hch at lst.de>
Signed-off-by Ming Lei <ming.lei at redhat.com>
---
   drivers/nvme/host/core.c | 4 ++++
   1 file changed, 4 insertions(+)
drivers/nvme/host/core.c +2 -0
index cc09b81fc7f4..cb8007cce4d1 100644
@@ -285,6 +285,10 @@ EXPORT_SYMBOL_GPL(nvme_complete_rq);
   bool nvme_cancel_request(struct request *req, void *data, bool reserved)
   {
+	/* don't abort one completed request */
+	if (blk_mq_request_completed(req))
Quoted 8 lines

drivers/nvme/host/core.c EXPORT_SYMBOL_GPL(nvme_complete_rq); · line 293

Quoted 4 lines
drivers/nvme/host/core.c +2 -0
+		return;
+
   	dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
   				"Cancelling I/O %d", req->tag);

Something I probably already asked before: what prevents that nvme_cancel_request() is executed concurrently with the completion handler of the same request?

Quoted 5 lines

The commit log did mention the point:

Before aborting in-flight requests, all IO queues have been shutdown.

which implies that no concurrent normal completion.

How about adding that explanation as a comment above nvme_cancel_request()? That would make that explanation much easier to find compared to having to search through commit logs.

Thanks,

Bart.

maintainer Reviewed-by on v1 3/5
#15 lore raw

Re: [PATCH 3/5] nvme: don't abort completed request in nvme_cancel_request

In reply to Ming Lei
Reviewed-by Sagi Grimberg <sagi at grimberg.me>
author
#16 lore raw

PATCH 4/5 nvme: wait until all completed request's complete fn is called

In reply to Ming Lei

When aborting in-flight request for recoverying controller, we have maken sure that queue's complete function is called on completed request before moving one. For example, the warning of WARN_ON_ONCE(qp->mrs_used > 0) in ib_destroy_qp_user() may be triggered.

Fix this issue by using blk_mq_tagset_drain_completed_request.

Cc Max Gurtovoy <maxg at mellanox.com>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Keith Busch <keith.busch at intel.com>
Cc Christoph Hellwig <hch at lst.de>
Signed-off-by Ming Lei <ming.lei at redhat.com>
---
 drivers/nvme/host/pci.c    | 2 ++
 drivers/nvme/host/rdma.c   | 8 ++++++--
 drivers/nvme/host/tcp.c    | 8 ++++++--
 drivers/nvme/target/loop.c | 2 ++
 4 files changed, 16 insertions(+), 4 deletions(-)
drivers/nvme/host/pci.c +2 -0
index bb970ca82517..47f37d9d9aa7 100644
@@ -2403,6 +2403,8 @@ static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
 
 	blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
 	blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
+	blk_mq_tagset_wait_completed_request(&dev->tagset);
+	blk_mq_tagset_wait_completed_request(&dev->admin_tagset);
 
 	/*
 	 * The driver will not be starting up queues again if shutting down so
drivers/nvme/host/rdma.c +6 -2
index a249db528d54..b313a60be1ca 100644
@@ -901,9 +901,11 @@ static void nvme_rdma_teardown_admin_queue(struct nvme_rdma_ctrl *ctrl,
 {
 	blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
 	nvme_rdma_stop_queue(&ctrl->queues[0]);
-	if (ctrl->ctrl.admin_tagset)
+	if (ctrl->ctrl.admin_tagset) {
 		blk_mq_tagset_busy_iter(ctrl->ctrl.admin_tagset,
 			nvme_cancel_request, &ctrl->ctrl);
+		blk_mq_tagset_wait_completed_request(ctrl->ctrl.admin_tagset);
+	}
 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
 	nvme_rdma_destroy_admin_queue(ctrl, remove);
 }
@@ -914,9 +916,11 @@ static void nvme_rdma_teardown_io_queues(struct nvme_rdma_ctrl *ctrl,
 	if (ctrl->ctrl.queue_count > 1) {
 		nvme_stop_queues(&ctrl->ctrl);
 		nvme_rdma_stop_io_queues(ctrl);
-		if (ctrl->ctrl.tagset)
+		if (ctrl->ctrl.tagset) {
 			blk_mq_tagset_busy_iter(ctrl->ctrl.tagset,
 				nvme_cancel_request, &ctrl->ctrl);
+			blk_mq_tagset_wait_completed_request(ctrl->ctrl.tagset);
+		}
 		if (remove)
 			nvme_start_queues(&ctrl->ctrl);
 		nvme_rdma_destroy_io_queues(ctrl, remove);
drivers/nvme/host/tcp.c +6 -2
index 606b13d35d16..cf2eaf834b36 100644
@@ -1748,9 +1748,11 @@ static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
 {
 	blk_mq_quiesce_queue(ctrl->admin_q);
 	nvme_tcp_stop_queue(ctrl, 0);
-	if (ctrl->admin_tagset)
+	if (ctrl->admin_tagset) {
 		blk_mq_tagset_busy_iter(ctrl->admin_tagset,
 			nvme_cancel_request, ctrl);
+		blk_mq_tagset_wait_completed_request(ctrl->admin_tagset);
+	}
 	blk_mq_unquiesce_queue(ctrl->admin_q);
 	nvme_tcp_destroy_admin_queue(ctrl, remove);
 }
@@ -1762,9 +1764,11 @@ static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,
 		return;
 	nvme_stop_queues(ctrl);
 	nvme_tcp_stop_io_queues(ctrl);
-	if (ctrl->tagset)
+	if (ctrl->tagset) {
 		blk_mq_tagset_busy_iter(ctrl->tagset,
 			nvme_cancel_request, ctrl);
+		blk_mq_tagset_wait_completed_request(ctrl->tagset);
+	}
 	if (remove)
 		nvme_start_queues(ctrl);
 	nvme_tcp_destroy_io_queues(ctrl, remove);
drivers/nvme/target/loop.c +2 -0
index b16dc3981c69..95c8f1732215 100644
@@ -407,6 +407,7 @@ static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl)
 		nvme_stop_queues(&ctrl->ctrl);
 		blk_mq_tagset_busy_iter(&ctrl->tag_set,
 					nvme_cancel_request, &ctrl->ctrl);
+		blk_mq_tagset_wait_completed_request(&ctrl->tag_set);
 		nvme_loop_destroy_io_queues(ctrl);
 	}
 
@@ -416,6 +417,7 @@ static void nvme_loop_shutdown_ctrl(struct nvme_loop_ctrl *ctrl)
 	blk_mq_quiesce_queue(ctrl->ctrl.admin_q);
 	blk_mq_tagset_busy_iter(&ctrl->admin_tag_set,
 				nvme_cancel_request, &ctrl->ctrl);
+	blk_mq_tagset_wait_completed_request(&ctrl->admin_tag_set);
 	blk_mq_unquiesce_queue(ctrl->ctrl.admin_q);
 	nvme_loop_destroy_admin_queue(ctrl);
 }
Signature · 1 line
2.20.1
#17 lore raw

Re: [PATCH 4/5] nvme: wait until all completed request's complete fn is called

Hi Ming,

Quoted 1 line · Ming Lei

When aborting in-flight request for recoverying controller, we have

recovering

Quoted 6 lines

maken sure that queue's complete function is called on completed request before moving one. For example, the warning of WARN_ON_ONCE(qp->mrs_used > 0) in ib_destroy_qp_user() may be triggered.

Fix this issue by using blk_mq_tagset_drain_completed_request.

Should be blk_mq_tagset_wait_completed_request but not blk_mq_tagset_drain_completed_request?

Dongli Zhang

author
#18 lore raw

Re: [PATCH 4/5] nvme: wait until all completed request's complete fn is called

Quoted 11 lines · Dongli Zhang

Hi Ming,

Quoted 1 line · Ming Lei

When aborting in-flight request for recoverying controller, we have

recovering

Quoted 4 lines

maken sure that queue's complete function is called on completed request before moving one. For example, the warning of WARN_ON_ONCE(qp->mrs_used > 0) in ib_destroy_qp_user() may be triggered.

Quoted 5 lines
Quoted 2 lines

Fix this issue by using blk_mq_tagset_drain_completed_request.

Should be blk_mq_tagset_wait_completed_request but not blk_mq_tagset_drain_completed_request?

Will fix the two in V2.

Thanks,
Ming

maintainer Reviewed-by on v1 4/5
#19 lore raw

Re: [PATCH 4/5] nvme: wait until all completed request's complete fn is called

In reply to Ming Lei
Reviewed-by Sagi Grimberg <sagi at grimberg.me>

What about fc? I know that they are not canceling a request, but they still complete it, shouldn't it also wait before removing?

author
#20 lore raw

Re: [PATCH 4/5] nvme: wait until all completed request's complete fn is called

Quoted 4 lines · Sagi Grimberg
Reviewed-by Sagi Grimberg <sagi at grimberg.me>

What about fc? I know that they are not canceling a request, but they still complete it, shouldn't it also wait before removing?

fc doesn't use blk-mq request abort mechanism, so its own approach may sync with abort well.

However, for normal completion, I think the wait is still needed, will do it in V2.

Thanks,
Ming

author
#21 lore raw

PATCH 5/5 blk-mq: remove blk_mq_complete_request_sync

In reply to Ming Lei

blk_mq_tagset_wait_completed_request() has been applied for waiting for completed request's fn, so not necessary to use blk_mq_complete_request_sync() any more.

Cc Max Gurtovoy <maxg at mellanox.com>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Keith Busch <keith.busch at intel.com>
Cc Christoph Hellwig <hch at lst.de>
Signed-off-by Ming Lei <ming.lei at redhat.com>
---
 block/blk-mq.c           | 7 -------
 drivers/nvme/host/core.c | 2 +-
 include/linux/blk-mq.h   | 1 -
 3 files changed, 1 insertion(+), 9 deletions(-)
block/blk-mq.c +0 -7
index e1d0b4567388..9836a00d8c07 100644
@@ -652,13 +652,6 @@ bool blk_mq_complete_request(struct request *rq)
 }
 EXPORT_SYMBOL(blk_mq_complete_request);
 
-void blk_mq_complete_request_sync(struct request *rq)
-{
-	WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
-	rq->q->mq_ops->complete(rq);
-}
-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;
drivers/nvme/host/core.c +1 -1
index cb8007cce4d1..4beaed3b5022 100644
@@ -293,7 +293,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_sync(req);
+	blk_mq_complete_request(req);
 	return true;
 }
 EXPORT_SYMBOL_GPL(nvme_cancel_request);
include/linux/blk-mq.h +0 -1
index ee0719b649b6..1cdd2788cfa6 100644
@@ -305,7 +305,6 @@ 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);
-void 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, unsigned int nr_segs);
 bool blk_mq_queue_stopped(struct request_queue *q);
Signature · 1 line
2.20.1
maintainer Reviewed-by on v1 5/5
#22 lore raw

Re: [PATCH 5/5] blk-mq: remove blk_mq_complete_request_sync

Reviewed-by Sagi Grimberg <sagi at grimberg.me>
#23 lore raw
In reply to Ming Lei
Quoted 10 lines · Ming Lei

Hi,

blk-mq may schedule to call queue's complete function on remote CPU via IPI, but never provide any way to synchronize the request's complete fn.

In some driver's EH(such as NVMe), hardware queue's resource may be freed & re-allocated. If the completed request's complete fn is run finally after the hardware queue's resource is released, kernel crash will be triggered.

Have you seen the crash? Anyway to emulate/verify this bug..

Quoted 27 lines

Fixes this issue by waitting until completed req's complete fn is run.

Thanks,
Ming

Ming Lei (5):
blk-mq: introduce blk_mq_request_completed()
blk-mq: introduce blk_mq_tagset_wait_completed_request()
nvme: don't abort completed request in nvme_cancel_request
nvme: wait until all completed request's complete fn is called
blk-mq: remove blk_mq_complete_request_sync

 block/blk-mq-tag.c         | 32 ++++++++++++++++++++++++++++++++
 block/blk-mq.c             | 13 ++++++-------
 drivers/nvme/host/core.c   |  6 +++++-
 drivers/nvme/host/pci.c    |  2 ++
 drivers/nvme/host/rdma.c   |  8 ++++++--
 drivers/nvme/host/tcp.c    |  8 ++++++--
 drivers/nvme/target/loop.c |  2 ++
 include/linux/blk-mq.h     |  3 ++-
 8 files changed, 61 insertions(+), 13 deletions(-)
Cc Max Gurtovoy <maxg at mellanox.com>
Cc Sagi Grimberg <sagi at grimberg.me>
Cc Keith Busch <keith.busch at intel.com>
Cc Christoph Hellwig <hch at lst.de>
author
#24 lore raw
Quoted 7 lines · Bob Liu
Quoted 6 lines · Ming Lei

Hi,

blk-mq may schedule to call queue's complete function on remote CPU via IPI, but never provide any way to synchronize the request's complete fn.

Quoted 6 lines
Quoted 4 lines

In some driver's EH(such as NVMe), hardware queue's resource may be freed & re-allocated. If the completed request's complete fn is run finally after the hardware queue's resource is released, kernel crash will be triggered.

Have you seen the crash? Anyway to emulate/verify this bug..

The crash is reported on nvme-rdma by one RH partner, and the approach of this patchset has been verified already.

Thanks,
Ming