Skip to contentSkip to conversation

nvme: Sync request queues on reset

PATCH Superseded Keith Busch 3 participants 25 messages May 18, 2018 – May 22, 2018

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#14 lore raw

PATCH 5/6 nvme-pci: Attempt reset retry for IO failures

In reply to Keith Busch

If the reset failed due to a non-fatal error, this patch will attempt to reset the controller again, with a maximum of 4 attempts.

Since the failed reset case has changed purpose, this patch provides a more appropriate name and warning message for the reset failure.

---
 drivers/nvme/host/pci.c | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)
drivers/nvme/host/pci.c +23 -3
index 6a7cbc631d92..ddfeb186d129 100644
@@ -37,6 +37,8 @@
 
 #define SGES_PER_PAGE	(PAGE_SIZE / sizeof(struct nvme_sgl_desc))
 
+#define MAX_RESET_FAILURES 4
+
 static int use_threaded_interrupts;
 module_param(use_threaded_interrupts, int, 0);
 
@@ -101,6 +103,8 @@ struct nvme_dev {
 	struct completion ioq_wait;
 	bool queues_froze;
 
+	int reset_failures;
+
 	/* shadow doorbell buffer support: */
 	u32 *dbbuf_dbs;
 	dma_addr_t dbbuf_dbs_dma_addr;
@@ -2307,9 +2311,23 @@ static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
 	kfree(dev);
 }
 
-static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
+static void nvme_reset_failure(struct nvme_dev *dev, int status)
 {
-	dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
+	dev->reset_failures++;
+	dev_warn(dev->ctrl.device, "Reset failure status: %d, failures:%d\n",
+		status, dev->reset_failures);
+
+	/* IO and Interrupted Call may indicate a retryable error */
+	switch (status) {
+	case -EIO:
+	case -EINTR:
+		if (dev->reset_failures < MAX_RESET_FAILURES &&
+		    !nvme_reset_ctrl(&dev->ctrl))
+			return;
+		break;
+	default:
+		break;
+	}
 
 	nvme_get_ctrl(&dev->ctrl);
 	nvme_dev_disable(dev, false);
@@ -2410,14 +2428,16 @@ static void nvme_reset_work(struct work_struct *work)
 	if (!nvme_change_ctrl_state(&dev->ctrl, new_state)) {
 		dev_warn(dev->ctrl.device,
 			"failed to mark controller state %d\n", new_state);
+		result = -ENODEV;
 		goto out;
 	}
 
+	dev->reset_failures = 0;
 	nvme_start_ctrl(&dev->ctrl);
 	return;
 
  out:
-	nvme_remove_dead_ctrl(dev, result);
+	nvme_reset_failure(dev, result);
 }
 
 static void nvme_remove_dead_ctrl_work(struct work_struct *work)
Signature · 1 line
2.14.3
#0 lore raw

PATCH 1/6 nvme: Sync request queues on reset

This patch fixes races that occur with simultaneous controller resets by synchronizing request queues prior to initializing the controller. Withouth this, a thread may attempt disabling a controller at the same time as we're trying to enable it.

---
 drivers/nvme/host/core.c | 21 +++++++++++++++++++--
 drivers/nvme/host/nvme.h |  1 +
 drivers/nvme/host/pci.c  |  1 +
 3 files changed, 21 insertions(+), 2 deletions(-)
drivers/nvme/host/core.c +19 -2
index 99b857e5a7a9..1de68b56b318 100644
@@ -3471,6 +3471,12 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
 }
 EXPORT_SYMBOL_GPL(nvme_init_ctrl);
 
+static void nvme_start_queue(struct nvme_ns *ns)
+{
+	blk_mq_unquiesce_queue(ns->queue);
+	blk_mq_kick_requeue_list(ns->queue);
+}
+
 /**
  * nvme_kill_queues(): Ends all namespace queues
  * @ctrl: the dead controller that needs to end
@@ -3499,7 +3505,7 @@ void nvme_kill_queues(struct nvme_ctrl *ctrl)
 		blk_set_queue_dying(ns->queue);
 
 		/* Forcibly unquiesce queues to avoid blocking dispatch */
-		blk_mq_unquiesce_queue(ns->queue);
+		nvme_start_queue(ns);
 	}
 	up_read(&ctrl->namespaces_rwsem);
 }
@@ -3569,11 +3575,22 @@ void nvme_start_queues(struct nvme_ctrl *ctrl)
 
 	down_read(&ctrl->namespaces_rwsem);
 	list_for_each_entry(ns, &ctrl->namespaces, list)
-		blk_mq_unquiesce_queue(ns->queue);
+		nvme_start_queue(ns);
 	up_read(&ctrl->namespaces_rwsem);
 }
 EXPORT_SYMBOL_GPL(nvme_start_queues);
 
+void nvme_sync_queues(struct nvme_ctrl *ctrl)
+{
+	struct nvme_ns *ns;
+
+	down_read(&ctrl->namespaces_rwsem);
+	list_for_each_entry(ns, &ctrl->namespaces, list)
+		blk_sync_queue(ns->queue);
+	up_read(&ctrl->namespaces_rwsem);
+}
+EXPORT_SYMBOL_GPL(nvme_sync_queues);
+
 int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
 {
 	if (!ctrl->ops->reinit_request)
drivers/nvme/host/nvme.h +1 -0
index 17d2f7cf3fed..c15c2ee7f61a 100644
@@ -407,6 +407,7 @@ int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
 void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
 		union nvme_result *res);
 
+void nvme_sync_queues(struct nvme_ctrl *ctrl);
 void nvme_stop_queues(struct nvme_ctrl *ctrl);
 void nvme_start_queues(struct nvme_ctrl *ctrl);
 void nvme_kill_queues(struct nvme_ctrl *ctrl);
drivers/nvme/host/pci.c +1 -0
index 17a0190bd88f..8da63402d474 100644
@@ -2312,6 +2312,7 @@ static void nvme_reset_work(struct work_struct *work)
 	 */
 	if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
 		nvme_dev_disable(dev, false);
+	nvme_sync_queues(&dev->ctrl);
 
 	/*
 	 * Introduce CONNECTING state from nvme-fc/rdma transports to mark the
Signature · 1 line
2.14.3
#1 lore raw

PATCH 2/6 nvme-pci: Fix queue freeze criteria on reset

The driver had been relying on the pci_dev to maintain the state of the pci device to know when starting a freeze would be appropriate. The blktests block/011 however shows us that users may alter the state of pci_dev out from under drivers and break the criteria we had been using.

This patch uses the private nvme controller struct to track the enabling/disabling state. Since we're relying on that now, the reset will unconditionally disable the device on reset. This is necessary anyway on a controller failure reset, and was already being done in the reset during admin bring up, and is not harmful to do a second time.

---
 drivers/nvme/host/pci.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/nvme/host/pci.c +8 -10
index 8da63402d474..2bd9d84f58d0 100644
@@ -2196,24 +2196,22 @@ static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
 	struct pci_dev *pdev = to_pci_dev(dev->dev);
 
 	mutex_lock(&dev->shutdown_lock);
-	if (pci_is_enabled(pdev)) {
+	if (dev->ctrl.ctrl_config & NVME_CC_ENABLE &&
+	    (dev->ctrl.state == NVME_CTRL_LIVE ||
+	     dev->ctrl.state == NVME_CTRL_RESETTING)) {
 		u32 csts = readl(dev->bar + NVME_REG_CSTS);
 
-		if (dev->ctrl.state == NVME_CTRL_LIVE ||
-		    dev->ctrl.state == NVME_CTRL_RESETTING)
-			nvme_start_freeze(&dev->ctrl);
+		nvme_start_freeze(&dev->ctrl);
 		dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
-			pdev->error_state  != pci_channel_io_normal);
+			pci_channel_offline(pdev) || !pci_is_enabled(pdev));
 	}
 
 	/*
 	 * Give the controller a chance to complete all entered requests if
 	 * doing a safe shutdown.
 	 */
-	if (!dead) {
-		if (shutdown)
-			nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
-	}
+	if (!dead && shutdown)
+		nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
 
 	nvme_stop_queues(&dev->ctrl);
 
@@ -2227,8 +2225,8 @@ static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
 		if (dev->host_mem_descs)
 			nvme_set_host_mem(dev, 0);
 		nvme_disable_io_queues(dev);
-		nvme_disable_admin_queue(dev, shutdown);
 	}
+	nvme_disable_admin_queue(dev, shutdown);
 	for (i = dev->ctrl.queue_count - 1; i >= 0; i--)
 		nvme_suspend_queue(&dev->queues[i]);
 
Signature · 1 line
2.14.3
#2 lore raw

PATCH 3/6 nvme: Move all IO out of controller reset

In reply to Keith Busch

IO may be retryable, so don't wait for them in the reset path. These commands may trigger a reset if that IO expires without a completion, placing it on the requeue list. Waiting for these would then deadlock the reset handler.

To fix the theoretical deadlock, this patch unblocks IO submission from the reset_work as before, but moves the waiting to the IO safe scan_work so that the reset_work may proceed to completion. Since the unfreezing happens in the controller LIVE state, the nvme device has to track if the queues were frozen now to prevent incorrect freeze depths.

This patch is also renaming the function 'nvme_dev_add' to a more appropriate name that describes what it's actually doing:
nvme_alloc_io_tags.

---
 drivers/nvme/host/core.c |  3 +++
 drivers/nvme/host/nvme.h |  1 +
 drivers/nvme/host/pci.c  | 46 +++++++++++++++++++++++++++++++++-------------
 3 files changed, 37 insertions(+), 13 deletions(-)
drivers/nvme/host/core.c +3 -0
index 1de68b56b318..34d7731f1419 100644
@@ -214,6 +214,7 @@ static inline bool nvme_req_needs_retry(struct request *req)
 	if (blk_noretry_request(req))
 		return false;
 	if (nvme_req(req)->status & NVME_SC_DNR)
+
 		return false;
 	if (nvme_req(req)->retries >= nvme_max_retries)
 		return false;
@@ -3177,6 +3178,8 @@ static void nvme_scan_work(struct work_struct *work)
 	struct nvme_id_ctrl *id;
 	unsigned nn;
 
+	if (ctrl->ops->update_hw_ctx)
+		ctrl->ops->update_hw_ctx(ctrl);
 	if (ctrl->state != NVME_CTRL_LIVE)
 		return;
 
drivers/nvme/host/nvme.h +1 -0
index c15c2ee7f61a..230c5424b197 100644
@@ -320,6 +320,7 @@ struct nvme_ctrl_ops {
 	int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size);
 	int (*reinit_request)(void *data, struct request *rq);
 	void (*stop_ctrl)(struct nvme_ctrl *ctrl);
+	void (*update_hw_ctx)(struct nvme_ctrl *ctrl);
 };
 
 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
drivers/nvme/host/pci.c +33 -13

85 lines not on this page · raw message

Signature · 1 line
2.14.3
#13 lore raw

PATCH 4/6 nvme: Allow reset from CONNECTING state

In reply to Keith Busch

A failed connection may be retryable. This patch allows the connecting state to initiate a reset so that it may try to connect again.

---
 drivers/nvme/host/core.c | 1 +
 1 file changed, 1 insertion(+)
drivers/nvme/host/core.c +1 -0
index 34d7731f1419..bccc92206fba 100644
@@ -293,6 +293,7 @@ bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
 		case NVME_CTRL_NEW:
 		case NVME_CTRL_LIVE:
 		case NVME_CTRL_ADMIN_ONLY:
+		case NVME_CTRL_CONNECTING:
 			changed = true;
 			/* FALLTHRU */
 		default:
Signature · 1 line
2.14.3
#15 lore raw

PATCH 6/6 nvme-pci: Rate limit the nvme timeout warnings

In reply to Keith Busch

The block layer's timeout handling currently refuses to let the driver complete commands outside the timeout callback once blk-mq decides they've expired. If a device breaks, this could potentially create many thousands of timed out commands. There's nothing of value to be gleaned from observing each of those messages, so this patch adds a ratelimit on them.

---
 drivers/nvme/host/pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/host/pci.c +1 -1
index ddfeb186d129..e4b91c246e36 100644
@@ -1251,7 +1251,7 @@ static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
  	 * returned to the driver, or if this is the admin queue.
 	 */
 	if (!nvmeq->qid || iod->aborted) {
-		dev_warn(dev->ctrl.device,
+		dev_warn_ratelimited(dev->ctrl.device,
 			 "I/O %d QID %d timeout, reset controller\n",
 			 req->tag, nvmeq->qid);
 		nvme_dev_disable(dev, false);
Signature · 1 line
2.14.3
#16 lore raw
In reply to Keith Busch
Quoted 48 lines · Keith Busch

This patch fixes races that occur with simultaneous controller resets by synchronizing request queues prior to initializing the controller. Withouth this, a thread may attempt disabling a controller at the same time as we're trying to enable it.

---
 drivers/nvme/host/core.c | 21 +++++++++++++++++++--
 drivers/nvme/host/nvme.h |  1 +
 drivers/nvme/host/pci.c  |  1 +
 3 files changed, 21 insertions(+), 2 deletions(-)
drivers/nvme/host/core.c +8 -2
index 99b857e5a7a9..1de68b56b318 100644
@@ -3471,6 +3471,12 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
 }
 EXPORT_SYMBOL_GPL(nvme_init_ctrl);
 
+static void nvme_start_queue(struct nvme_ns *ns)
+{
+	blk_mq_unquiesce_queue(ns->queue);
+	blk_mq_kick_requeue_list(ns->queue);
+}
+
 /**
  * nvme_kill_queues(): Ends all namespace queues
  * @ctrl: the dead controller that needs to end
@@ -3499,7 +3505,7 @@ void nvme_kill_queues(struct nvme_ctrl *ctrl)
 		blk_set_queue_dying(ns->queue);
 
 		/* Forcibly unquiesce queues to avoid blocking dispatch */
-		blk_mq_unquiesce_queue(ns->queue);
+		nvme_start_queue(ns);
 	}
 	up_read(&ctrl->namespaces_rwsem);
 }
@@ -3569,11 +3575,22 @@ void nvme_start_queues(struct nvme_ctrl *ctrl)
 
 	down_read(&ctrl->namespaces_rwsem);
 	list_for_each_entry(ns, &ctrl->namespaces, list)
-		blk_mq_unquiesce_queue(ns->queue);
+		nvme_start_queue(ns);
 	up_read(&ctrl->namespaces_rwsem);
 }
 EXPORT_SYMBOL_GPL(nvme_start_queues);
 

drivers/nvme/host/core.c void nvme_start_queues(struct nvme_ctrl *ctrl) · line 3592

Quoted 10 lines
drivers/nvme/host/core.c +10 -0
+void nvme_sync_queues(struct nvme_ctrl *ctrl)
+{
+	struct nvme_ns *ns;
+
+	down_read(&ctrl->namespaces_rwsem);
+	list_for_each_entry(ns, &ctrl->namespaces, list)
+		blk_sync_queue(ns->queue);
+	up_read(&ctrl->namespaces_rwsem);
+}
+EXPORT_SYMBOL_GPL(nvme_sync_queues);

This way can't sync timeout reliably, since timeout events can come from two NS at the same time, and one may be handled as RESET_TIMER, and another one can be handled as EH_HANDLED.

Thanks,
Ming

#3 lore raw

Re: [PATCH 3/6] nvme: Move all IO out of controller reset

In reply to Keith Busch
Quoted 69 lines · Keith Busch

IO may be retryable, so don't wait for them in the reset path. These commands may trigger a reset if that IO expires without a completion, placing it on the requeue list. Waiting for these would then deadlock the reset handler.

To fix the theoretical deadlock, this patch unblocks IO submission from the reset_work as before, but moves the waiting to the IO safe scan_work so that the reset_work may proceed to completion. Since the unfreezing happens in the controller LIVE state, the nvme device has to track if the queues were frozen now to prevent incorrect freeze depths.

This patch is also renaming the function 'nvme_dev_add' to a more appropriate name that describes what it's actually doing:
nvme_alloc_io_tags.

---
 drivers/nvme/host/core.c |  3 +++
 drivers/nvme/host/nvme.h |  1 +
 drivers/nvme/host/pci.c  | 46 +++++++++++++++++++++++++++++++++-------------
 3 files changed, 37 insertions(+), 13 deletions(-)
drivers/nvme/host/core.c +3 -0
index 1de68b56b318..34d7731f1419 100644
@@ -214,6 +214,7 @@ static inline bool nvme_req_needs_retry(struct request *req)
 	if (blk_noretry_request(req))
 		return false;
 	if (nvme_req(req)->status & NVME_SC_DNR)
+
 		return false;
 	if (nvme_req(req)->retries >= nvme_max_retries)
 		return false;
@@ -3177,6 +3178,8 @@ static void nvme_scan_work(struct work_struct *work)
 	struct nvme_id_ctrl *id;
 	unsigned nn;
 
+	if (ctrl->ops->update_hw_ctx)
+		ctrl->ops->update_hw_ctx(ctrl);
 	if (ctrl->state != NVME_CTRL_LIVE)
 		return;
 
drivers/nvme/host/nvme.h +1 -0
index c15c2ee7f61a..230c5424b197 100644
@@ -320,6 +320,7 @@ struct nvme_ctrl_ops {
 	int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size);
 	int (*reinit_request)(void *data, struct request *rq);
 	void (*stop_ctrl)(struct nvme_ctrl *ctrl);
+	void (*update_hw_ctx)(struct nvme_ctrl *ctrl);
 };
 
 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
drivers/nvme/host/pci.c +1 -0
index 2bd9d84f58d0..6a7cbc631d92 100644
@@ -99,6 +99,7 @@ struct nvme_dev {
 	u32 cmbloc;
 	struct nvme_ctrl ctrl;
 	struct completion ioq_wait;
+	bool queues_froze;
 
 	/* shadow doorbell buffer support: */
 	u32 *dbbuf_dbs;
@@ -2065,10 +2066,33 @@ static void nvme_disable_io_queues(struct nvme_dev *dev)
 	}

drivers/nvme/host/pci.c static void nvme_disable_io_queues(struct nvme_dev *dev) · line 2076

Quoted 10 lines
drivers/nvme/host/pci.c +8 -0
 }
 
+static void nvme_pci_update_hw_ctx(struct nvme_ctrl *ctrl)
+{
+	struct nvme_dev *dev = to_nvme_dev(ctrl);
+	bool unfreeze;
+
+	mutex_lock(&dev->shutdown_lock);
+	unfreeze = dev->queues_froze;
+	mutex_unlock(&dev->shutdown_lock);

What if nvme_dev_disable() just sets the .queues_froze flag and userspace sends a RESCAN command at the same time?

drivers/nvme/host/pci.c static void nvme_disable_io_queues(struct nvme_dev *dev) · line 2080

Quoted 4 lines
drivers/nvme/host/pci.c +4 -0
+
+	if (unfreeze)
+		nvme_wait_freeze(&dev->ctrl);
+

timeout may comes just before&during blk_mq_update_nr_hw_queues() or the above nvme_wait_freeze(), then both two may hang forever.

drivers/nvme/host/pci.c static void nvme_disable_io_queues(struct nvme_dev *dev) · line 2089

Quoted 9 lines
drivers/nvme/host/pci.c +9 -0
+	blk_mq_update_nr_hw_queues(ctrl->tagset, dev->online_queues - 1);
+	nvme_free_queues(dev, dev->online_queues);
+
+	if (unfreeze)
+		nvme_unfreeze(&dev->ctrl);
+
+	mutex_lock(&dev->shutdown_lock);
+	dev->queues_froze = false;
+	mutex_unlock(&dev->shutdown_lock);

If the running scan work is triggered via user-space, the above code may clear the .queues_froze flag wrong.

Thanks,
Ming

#17 lore raw
In reply to Ming Lei
Quoted 3 lines · Ming Lei

This way can't sync timeout reliably, since timeout events can come from two NS at the same time, and one may be handled as RESET_TIMER, and another one can be handled as EH_HANDLED.

You keep saying that, but the controller state is global to the controller. It doesn't matter which namespace request_queue started the
reset: every namespaces request queue sees the RESETTING controller state from the point the syncing occurs, and they don't return RESET_TIMER, and on top of that, the reset reclaims every single IO command no matter what namespace request_queue initiated the reset.

#18 lore raw
Quoted 8 lines · Keith Busch
Quoted 3 lines · Ming Lei

This way can't sync timeout reliably, since timeout events can come from two NS at the same time, and one may be handled as RESET_TIMER, and another one can be handled as EH_HANDLED.

You keep saying that, but the controller state is global to the controller. It doesn't matter which namespace request_queue started the
reset: every namespaces request queue sees the RESETTING controller state

When timeouts come, the global state of RESETTING may not be updated yet, so all the timeouts may not observe the state.

Please see my previous explanation:

https://marc.info/?l=linux-block&m=152600464317808&w=2

Thanks,
Ming

#19 lore raw
Quoted 6 lines · Ming Lei
Quoted 3 lines

You keep saying that, but the controller state is global to the controller. It doesn't matter which namespace request_queue started the
reset: every namespaces request queue sees the RESETTING controller state

When timeouts come, the global state of RESETTING may not be updated yet, so all the timeouts may not observe the state.

Even prior to the RESETING state, every single command, no matter which namespace or request_queue it came on, is reclaimed by the driver. There *should* be no requests to timeout after nvme_dev_disable is called because the nvme driver returned control of all requests in the tagset to blk-mq.

In any case, if blk-mq decides it won't complete those requests, we can just swap the order in the reset_work: sync first, uncondintionally disable. Does the following snippet look more okay?

---

drivers/nvme/host/pci.c +7 -4
index 17a0190bd88f..42af077ee07a 100644
@@ -2307,11 +2307,14 @@ static void nvme_reset_work(struct work_struct *work)
 		goto out;
 
 	/*
-	 * If we're called to reset a live controller first shut it down before
-	 * moving on.
+	 * Ensure there are no timeout work in progress prior to forcefully
+	 * disabling the queue. There is no harm in disabling the device even
+	 * when it was already disabled, as this will forcefully reclaim any
+	 * IOs that are stuck due to blk-mq's timeout handling that prevents
+	 * timed out requests from completing.
 	 */
-	if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
-		nvme_dev_disable(dev, false);
+	nvme_sync_queues(&dev->ctrl);
+	nvme_dev_disable(dev, false);
 
 	/*
 	 * Introduce CONNECTING state from nvme-fc/rdma transports to mark the

--

#4 lore raw

Re: [PATCH 3/6] nvme: Move all IO out of controller reset

In reply to Ming Lei
Quoted 8 lines · Ming Lei

drivers/nvme/host/pci.c static void nvme_disable_io_queues(struct nvme_dev *dev) · line 2080

Quoted 4 lines · Keith Busch
drivers/nvme/host/pci.c +4 -0
+
+	if (unfreeze)
+		nvme_wait_freeze(&dev->ctrl);
+

timeout may comes just before&during blk_mq_update_nr_hw_queues() or the above nvme_wait_freeze(), then both two may hang forever.

Why would it hang forever? The scan_work doesn't stop a timeout from triggering a reset to reclaim requests necessary to complete a freeze.

#5 lore raw

Re: [PATCH 3/6] nvme: Move all IO out of controller reset

Quoted 10 lines · Keith Busch
Quoted 8 lines · Ming Lei

drivers/nvme/host/pci.c static void nvme_disable_io_queues(struct nvme_dev *dev) · line 2080

Quoted 4 lines · Keith Busch
drivers/nvme/host/pci.c +4 -0
+
+	if (unfreeze)
+		nvme_wait_freeze(&dev->ctrl);
+

timeout may comes just before&during blk_mq_update_nr_hw_queues() or the above nvme_wait_freeze(), then both two may hang forever.

Quoted 2 lines

Why would it hang forever? The scan_work doesn't stop a timeout from triggering a reset to reclaim requests necessary to complete a freeze.

nvme_dev_disable() will quiesce queues, then nvme_wait_freeze() or blk_mq_update_nr_hw_queues() may hang forever.

Thanks,
Ming

#6 lore raw

Re: [PATCH 3/6] nvme: Move all IO out of controller reset

Quoted 11 lines · Ming Lei
Quoted 10 lines · Keith Busch
Quoted 8 lines · Ming Lei

drivers/nvme/host/pci.c static void nvme_disable_io_queues(struct nvme_dev *dev) · line 2080

Quoted 4 lines · Keith Busch
drivers/nvme/host/pci.c +4 -0
+
+	if (unfreeze)
+		nvme_wait_freeze(&dev->ctrl);
+

timeout may comes just before&during blk_mq_update_nr_hw_queues() or the above nvme_wait_freeze(), then both two may hang forever.

Quoted 5 lines
Quoted 2 lines

Why would it hang forever? The scan_work doesn't stop a timeout from triggering a reset to reclaim requests necessary to complete a freeze.

nvme_dev_disable() will quiesce queues, then nvme_wait_freeze() or blk_mq_update_nr_hw_queues() may hang forever.

nvme_dev_disable is just the first part of the timeout sequence. You have to follow it through to the reset_work that either restarts or kills the queues.

#20 lore raw
In reply to Keith Busch
Quoted 5 lines · Keith Busch
Quoted 4 lines · Ming Lei
Quoted 3 lines

You keep saying that, but the controller state is global to the controller. It doesn't matter which namespace request_queue started the
reset: every namespaces request queue sees the RESETTING controller state

Quoted 8 lines
Quoted 2 lines

When timeouts come, the global state of RESETTING may not be updated yet, so all the timeouts may not observe the state.

Even prior to the RESETING state, every single command, no matter which namespace or request_queue it came on, is reclaimed by the driver. There *should* be no requests to timeout after nvme_dev_disable is called because the nvme driver returned control of all requests in the tagset to blk-mq.

The timed-out requests won't be canceled by nvme_dev_disable().

If the timed-out requests is handled as RESET_TIMER, there may be new timeout event triggered again.

Quoted 16 lines

In any case, if blk-mq decides it won't complete those requests, we can just swap the order in the reset_work: sync first, uncondintionally disable. Does the following snippet look more okay?

---

drivers/nvme/host/pci.c +0 -2
index 17a0190bd88f..42af077ee07a 100644
@@ -2307,11 +2307,14 @@ static void nvme_reset_work(struct work_struct *work)
 		goto out;
 
 	/*
-	 * If we're called to reset a live controller first shut it down before
-	 * moving on.

drivers/nvme/host/pci.c static void nvme_reset_work(struct work_struct *work) · line 2317

Quoted 10 lines
drivers/nvme/host/pci.c +7 -2
+	 * Ensure there are no timeout work in progress prior to forcefully
+	 * disabling the queue. There is no harm in disabling the device even
+	 * when it was already disabled, as this will forcefully reclaim any
+	 * IOs that are stuck due to blk-mq's timeout handling that prevents
+	 * timed out requests from completing.
 	 */
-	if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
-		nvme_dev_disable(dev, false);
+	nvme_sync_queues(&dev->ctrl);
+	nvme_dev_disable(dev, false);

That may not work reliably too.

For example, request A from NS_0 is timed-out and handled as RESET_TIMER, meantime request B from NS_1 is timed-out and handled as EH_HANDLED.

When the above reset work is run for handling timeout of req B, new timeout event on request A may come just between the above nvme_sync_queues() and nvme_dev_disable(), then nvme_dev_disable() can't cover request A, and finally the timed-out event for req A will nvme_dev_disable() when the current reset is just in-progress, then this reset can't move on, and IO hang is caused.

Thanks,
Ming

#7 lore raw

Re: [PATCH 3/6] nvme: Move all IO out of controller reset

In reply to Keith Busch
Quoted 15 lines · Keith Busch
Quoted 14 lines · Ming Lei
Quoted 12 lines · Keith Busch
Quoted 8 lines · Ming Lei

drivers/nvme/host/pci.c static void nvme_disable_io_queues(struct nvme_dev *dev) · line 2080

Quoted 4 lines · Keith Busch
drivers/nvme/host/pci.c +4 -0
+
+	if (unfreeze)
+		nvme_wait_freeze(&dev->ctrl);
+

timeout may comes just before&during blk_mq_update_nr_hw_queues() or the above nvme_wait_freeze(), then both two may hang forever.

Why would it hang forever? The scan_work doesn't stop a timeout from triggering a reset to reclaim requests necessary to complete a freeze.

Quoted 6 lines
Quoted 2 lines

nvme_dev_disable() will quiesce queues, then nvme_wait_freeze() or blk_mq_update_nr_hw_queues() may hang forever.

nvme_dev_disable is just the first part of the timeout sequence. You have to follow it through to the reset_work that either restarts or kills the queues.

nvme_dev_disable() quiesces queues first before killing queues.

If queues are quiesced during or before nvme_wait_freeze() is run from the 2nd part of reset, the 2nd part can't move on, and IO hang is caused. Finally no reset can be scheduled at all.

Thanks,
Ming

#8 lore raw

Re: [PATCH 3/6] nvme: Move all IO out of controller reset

Quoted 5 lines · Ming Lei

nvme_dev_disable() quiesces queues first before killing queues.

If queues are quiesced during or before nvme_wait_freeze() is run from the 2nd part of reset, the 2nd part can't move on, and IO hang is caused. Finally no reset can be scheduled at all.

But this patch moves nvme_wait_freeze outside the reset path, so I'm afraid I'm unable to follow how you've concluded the wait freeze is somehow part of the reset.

#21 lore raw
In reply to Ming Lei
Quoted 9 lines · Ming Lei
Quoted 8 lines · Keith Busch
Quoted 6 lines · Ming Lei
Quoted 3 lines

You keep saying that, but the controller state is global to the controller. It doesn't matter which namespace request_queue started the
reset: every namespaces request queue sees the RESETTING controller state

When timeouts come, the global state of RESETTING may not be updated yet, so all the timeouts may not observe the state.

Quoted 7 lines
Quoted 5 lines

Even prior to the RESETING state, every single command, no matter which namespace or request_queue it came on, is reclaimed by the driver. There *should* be no requests to timeout after nvme_dev_disable is called because the nvme driver returned control of all requests in the tagset to blk-mq.

The timed-out requests won't be canceled by nvme_dev_disable().

??? nvme_dev_disable cancels all started requests. There are no exceptions.

Quoted 32 lines

If the timed-out requests is handled as RESET_TIMER, there may be new timeout event triggered again.

Quoted 16 lines

In any case, if blk-mq decides it won't complete those requests, we can just swap the order in the reset_work: sync first, uncondintionally disable. Does the following snippet look more okay?

---

drivers/nvme/host/pci.c +0 -2
index 17a0190bd88f..42af077ee07a 100644
@@ -2307,11 +2307,14 @@ static void nvme_reset_work(struct work_struct *work)
 		goto out;
 
 	/*
-	 * If we're called to reset a live controller first shut it down before
-	 * moving on.

drivers/nvme/host/pci.c static void nvme_reset_work(struct work_struct *work) · line 2317

Quoted 10 lines
drivers/nvme/host/pci.c +7 -2
+	 * Ensure there are no timeout work in progress prior to forcefully
+	 * disabling the queue. There is no harm in disabling the device even
+	 * when it was already disabled, as this will forcefully reclaim any
+	 * IOs that are stuck due to blk-mq's timeout handling that prevents
+	 * timed out requests from completing.
 	 */
-	if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
-		nvme_dev_disable(dev, false);
+	nvme_sync_queues(&dev->ctrl);
+	nvme_dev_disable(dev, false);

That may not work reliably too.

Quoted 2 lines

For example, request A from NS_0 is timed-out and handled as RESET_TIMER, meantime request B from NS_1 is timed-out and handled as EH_HANDLED.

Meanwhile, request B's nvme_dev_disable prior to returning EH_HANDLED cancels all requests, which includes request A.

Quoted 3 lines

When the above reset work is run for handling timeout of req B, new timeout event on request A may come just between the above nvme_sync_queues() and nvme_dev_disable()

The sync queues either stops the timer from running, or waits for it to complete. We are in the RESETTING state: if request A's timeout happens to be running, we're not restarting the timer; we're returning EH_HANDLED.

Quoted 4 lines

then nvme_dev_disable()
can't cover request A, and finally the timed-out event for req A will nvme_dev_disable() when the current reset is just in-progress, then this reset can't move on, and IO hang is caused.

At no point in the nvme_reset are we waiting for any IO to complete. Reset continues to make forward progress.

#9 lore raw

Re: [PATCH 3/6] nvme: Move all IO out of controller reset

In reply to Keith Busch
Quoted 10 lines · Keith Busch
Quoted 5 lines · Ming Lei

nvme_dev_disable() quiesces queues first before killing queues.

If queues are quiesced during or before nvme_wait_freeze() is run from the 2nd part of reset, the 2nd part can't move on, and IO hang is caused. Finally no reset can be scheduled at all.

But this patch moves nvme_wait_freeze outside the reset path, so I'm afraid I'm unable to follow how you've concluded the wait freeze is somehow part of the reset.

For example:

1) the 1st timeout event:

- nvme_dev_disable()
- reset
- scan_work

2) the 2nd timeout event:

nvme_dev_disable() may come just after nvme_start_queues() in the above reset of the 1st timeout. And nvme_timeout() won't schedule a new reset since the controller state is NVME_CTRL_CONNECTING.

Then scan_work in 1st timeout still may hang for ever.

Thanks,
Ming

#22 lore raw
In reply to Keith Busch
Quoted 16 lines · Keith Busch
Quoted 15 lines · Ming Lei
Quoted 13 lines · Keith Busch
Quoted 6 lines · Ming Lei
Quoted 3 lines

You keep saying that, but the controller state is global to the controller. It doesn't matter which namespace request_queue started the
reset: every namespaces request queue sees the RESETTING controller state

When timeouts come, the global state of RESETTING may not be updated yet, so all the timeouts may not observe the state.

Even prior to the RESETING state, every single command, no matter which namespace or request_queue it came on, is reclaimed by the driver. There *should* be no requests to timeout after nvme_dev_disable is called because the nvme driver returned control of all requests in the tagset to blk-mq.

Quoted 3 lines
Quoted 1 line

The timed-out requests won't be canceled by nvme_dev_disable().

??? nvme_dev_disable cancels all started requests. There are no exceptions.

Please take a look at blk_mq_complete_request(). Even with Bart's change, the request still won't be completed by driver. The request can only be completed by either driver or blk-mq, not both.

Quoted 33 lines
Quoted 33 lines

If the timed-out requests is handled as RESET_TIMER, there may be new timeout event triggered again.

Quoted 16 lines

In any case, if blk-mq decides it won't complete those requests, we can just swap the order in the reset_work: sync first, uncondintionally disable. Does the following snippet look more okay?

---

drivers/nvme/host/pci.c +0 -2
index 17a0190bd88f..42af077ee07a 100644
@@ -2307,11 +2307,14 @@ static void nvme_reset_work(struct work_struct *work)
 		goto out;
 
 	/*
-	 * If we're called to reset a live controller first shut it down before
-	 * moving on.

drivers/nvme/host/pci.c static void nvme_reset_work(struct work_struct *work) · line 2317

Quoted 10 lines
drivers/nvme/host/pci.c +7 -2
+	 * Ensure there are no timeout work in progress prior to forcefully
+	 * disabling the queue. There is no harm in disabling the device even
+	 * when it was already disabled, as this will forcefully reclaim any
+	 * IOs that are stuck due to blk-mq's timeout handling that prevents
+	 * timed out requests from completing.
 	 */
-	if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
-		nvme_dev_disable(dev, false);
+	nvme_sync_queues(&dev->ctrl);
+	nvme_dev_disable(dev, false);

That may not work reliably too.

Quoted 5 lines
Quoted 2 lines

For example, request A from NS_0 is timed-out and handled as RESET_TIMER, meantime request B from NS_1 is timed-out and handled as EH_HANDLED.

Meanwhile, request B's nvme_dev_disable prior to returning EH_HANDLED cancels all requests, which includes request A.

No, please see my above comment.

Thanks,
Ming

#10 lore raw

Re: [PATCH 3/6] nvme: Move all IO out of controller reset

In reply to Ming Lei
Quoted 20 lines · Ming Lei
Quoted 10 lines · Keith Busch
Quoted 5 lines · Ming Lei

nvme_dev_disable() quiesces queues first before killing queues.

If queues are quiesced during or before nvme_wait_freeze() is run from the 2nd part of reset, the 2nd part can't move on, and IO hang is caused. Finally no reset can be scheduled at all.

But this patch moves nvme_wait_freeze outside the reset path, so I'm afraid I'm unable to follow how you've concluded the wait freeze is somehow part of the reset.

For example:

1) the 1st timeout event:

- nvme_dev_disable()
- reset
- scan_work

Quoted 5 lines

2) the 2nd timeout event:

nvme_dev_disable() may come just after nvme_start_queues() in the above reset of the 1st timeout. And nvme_timeout() won't schedule a new reset since the controller state is NVME_CTRL_CONNECTING.

Let me get this straight -- you're saying nvme_start_queues is going to somehow immediately trigger timeout work? I can't see how that could possibly happen in real life, but we can just remove it and use the existing nvme_start_ctrl to handle that in the LIVE state.

#23 lore raw
In reply to Ming Lei
Quoted 3 lines · Ming Lei

Please take a look at blk_mq_complete_request(). Even with Bart's change, the request still won't be completed by driver. The request can only be completed by either driver or blk-mq, not both.

So you're saying blk-mq can't complete a request the driver returned to blk-mq to complete. And that's the nvme driver's problem to fix?

#11 lore raw

Re: [PATCH 3/6] nvme: Move all IO out of controller reset

In reply to Keith Busch
Quoted 21 lines · Keith Busch
Quoted 20 lines · Ming Lei
Quoted 10 lines · Keith Busch
Quoted 5 lines · Ming Lei

nvme_dev_disable() quiesces queues first before killing queues.

If queues are quiesced during or before nvme_wait_freeze() is run from the 2nd part of reset, the 2nd part can't move on, and IO hang is caused. Finally no reset can be scheduled at all.

But this patch moves nvme_wait_freeze outside the reset path, so I'm afraid I'm unable to follow how you've concluded the wait freeze is somehow part of the reset.

For example:

1) the 1st timeout event:

- nvme_dev_disable()
- reset
- scan_work

Quoted 8 lines
Quoted 5 lines

2) the 2nd timeout event:

nvme_dev_disable() may come just after nvme_start_queues() in the above reset of the 1st timeout. And nvme_timeout() won't schedule a new reset since the controller state is NVME_CTRL_CONNECTING.

Let me get this straight -- you're saying nvme_start_queues is going to somehow immediately trigger timeout work? I can't see how that could

It may be difficult to trigger in reality, but won't be impossible since the timeout value can be adjusted via module parameter, and any schedule delay can be introduced in one busy system.

It isn't a good practice to rely on timing for avoiding race, IMO.

Quoted 2 lines

possibly happen in real life, but we can just remove it and use the existing nvme_start_ctrl to handle that in the LIVE state.

OK, please fix other issues mentioned in the following comment together, then I will review further and see if it can work well.

https://marc.info/?l=linux-block&m=152668465710591&w=2

Thanks,
Ming

#24 lore raw
In reply to Keith Busch
Quoted 7 lines · Keith Busch
Quoted 3 lines · Ming Lei

Please take a look at blk_mq_complete_request(). Even with Bart's change, the request still won't be completed by driver. The request can only be completed by either driver or blk-mq, not both.

So you're saying blk-mq can't complete a request the driver returned to blk-mq to complete. And that's the nvme driver's problem to fix?

For avoiding use-after-free, one request can only be completed by one path, either by timeout path or normal completion(irq or cancel) from driver.

So before handling this req's timeout, this request has to be marked as completed by blk-mq timeout code already, then nvme_cancel_request() can't cover this timed-out request.

Thanks,
Ming

#12 lore raw

Re: [PATCH 3/6] nvme: Move all IO out of controller reset

In reply to Ming Lei
Quoted 1 line · Ming Lei

It isn't a good practice to rely on timing for avoiding race, IMO.

Sure thing, and it's easy enough to avoid this one.

Quoted 4 lines

OK, please fix other issues mentioned in the following comment together, then I will review further and see if it can work well.

https://marc.info/?l=linux-block&m=152668465710591&w=2

Will do, v2 coming up by end of today.