Skip to contentSkip to conversation

nvme: pci: fix & improve timeout handling

PATCH Superseded Ming Lei 3 participants 19 messages May 3, 2018 – May 5, 2018

Subsystems (2)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

v3 cover nvme: pci: fix & improve timeout handling

Hi,

The 1st patch introduces blk_quiesce_timeout() and blk_unquiesce_timeout() for NVMe, meantime fixes blk_sync_queue().

The 2nd patch covers timeout for admin commands for recovering controller for avoiding possible deadlock.

The 3~5 patches fixes race between nvme_start_freeze() and nvme_unfreeze().

The last 3 patches fixes several races wrt. NVMe timeout handler, and finally can make blktests block/011 passed. Meantime the NVMe timeout model gets simplified a lot.

gitweb:

	https://github.com/ming1/linux/commits/v4.17-rc-nvme-timeout.V3

V3:
- fix one new race related freezing in patch 4, nvme_reset_work() may hang forever without this patch
- rewrite the last 3 patches, and avoid to break nvme_reset_ctrl*()

V2:
- fix draining timeout work, so no need to change return value from .timeout()
- fix race between nvme_start_freeze() and nvme_unfreeze()
- cover timeout for admin commands running in EH

Ming Lei (8):
block: introduce blk_quiesce_timeout() and blk_unquiesce_timeout()
nvme: pci: cover timeout for admin commands running in EH
nvme: pci: only wait freezing if queue is frozen
nvme: pci: freeze queue in nvme_dev_disable() in case of error
recovery
nvme: fix race between freeze queues and unfreeze queues
nvme: pci: split controller resetting into two parts
nvme: pci: recover controller reliably
nvme: pci: simplify timeout handling

 block/blk-core.c         |  21 ++-
 block/blk-mq.c           |   9 ++
 block/blk-timeout.c      |   5 +-
 drivers/nvme/host/core.c |  47 ++++++-
 drivers/nvme/host/nvme.h |   6 +
 drivers/nvme/host/pci.c  | 344 ++++++++++++++++++++++++++++++++++++++++-------
 include/linux/blkdev.h   |  13 ++
 7 files changed, 391 insertions(+), 54 deletions(-)
Cc Jianchao Wang <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Laurence Oberman <[email protected]>
Signature · 1 line
2.9.5
author
#1 lore raw

PATCH v3 1/8 block: introduce blk_quiesce_timeout() and blk_unquiesce_timeout()

Turns out the current way can't drain timout completely because mod_timer() can be triggered in the work func, which can be just run inside the synced timeout work:

        del_timer_sync(&q->timeout);
        cancel_work_sync(&q->timeout_work);

This patch introduces one flag of 'timeout_off' for fixing this issue, turns out this simple way does work.

Also blk_quiesce_timeout() and blk_unquiesce_timeout() are introduced for draining timeout, which is needed by NVMe.

Cc Bart Van Assche <[email protected]>
Cc Jianchao Wang <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Laurence Oberman <[email protected]>
---
 block/blk-core.c       | 21 +++++++++++++++++++--
 block/blk-mq.c         |  9 +++++++++
 block/blk-timeout.c    |  5 ++++-
 include/linux/blkdev.h | 13 +++++++++++++
 4 files changed, 45 insertions(+), 3 deletions(-)
block/blk-core.c +19 -2
index 85909b431eb0..c277f1023703 100644
@@ -392,6 +392,22 @@ void blk_stop_queue(struct request_queue *q)
 }
 EXPORT_SYMBOL(blk_stop_queue);
 
+void blk_unquiesce_timeout(struct request_queue *q)
+{
+	blk_mark_timeout_quiesce(q, false);
+	mod_timer(&q->timeout, jiffies + q->rq_timeout);
+}
+EXPORT_SYMBOL(blk_unquiesce_timeout);
+
+void blk_quiesce_timeout(struct request_queue *q)
+{
+	blk_mark_timeout_quiesce(q, true);
+
+	del_timer_sync(&q->timeout);
+	cancel_work_sync(&q->timeout_work);
+}
+EXPORT_SYMBOL(blk_quiesce_timeout);
+
 /**
  * blk_sync_queue - cancel any pending callbacks on a queue
  * @q: the queue
@@ -412,8 +428,7 @@ EXPORT_SYMBOL(blk_stop_queue);
  */
 void blk_sync_queue(struct request_queue *q)
 {
-	del_timer_sync(&q->timeout);
-	cancel_work_sync(&q->timeout_work);
+	blk_quiesce_timeout(q);
 
 	if (q->mq_ops) {
 		struct blk_mq_hw_ctx *hctx;
@@ -425,6 +440,8 @@ void blk_sync_queue(struct request_queue *q)
 	} else {
 		cancel_delayed_work_sync(&q->delay_work);
 	}
+
+	blk_mark_timeout_quiesce(q, false);
 }
 EXPORT_SYMBOL(blk_sync_queue);
 
block/blk-mq.c +9 -0
index c3621453ad87..d0a5dc29c8ef 100644
@@ -901,6 +901,15 @@ static void blk_mq_timeout_work(struct work_struct *work)
 	};
 	struct blk_mq_hw_ctx *hctx;
 	int i;
+	bool timeout_off;
+	unsigned long flags;
+
+	spin_lock_irqsave(q->queue_lock, flags);
+	timeout_off = q->timeout_off;
+	spin_unlock_irqrestore(q->queue_lock, flags);
+
+	if (timeout_off)
+		return;
 
 	/* A deadlock might occur if a request is stuck requiring a
 	 * timeout at the same time a queue freeze is waiting
block/blk-timeout.c +4 -1
index 652d4d4d3e97..ffd0b609091e 100644
@@ -136,12 +136,15 @@ void blk_timeout_work(struct work_struct *work)
 
 	spin_lock_irqsave(q->queue_lock, flags);
 
+	if (q->timeout_off)
+		goto exit;
+
 	list_for_each_entry_safe(rq, tmp, &q->timeout_list, timeout_list)
 		blk_rq_check_expired(rq, &next, &next_set);
 
 	if (next_set)
 		mod_timer(&q->timeout, round_jiffies_up(next));
-
+exit:
 	spin_unlock_irqrestore(q->queue_lock, flags);
 }
 
include/linux/blkdev.h +13 -0
index 5c4eee043191..a2cc4aaecf50 100644
@@ -584,6 +584,7 @@ struct request_queue {
 	struct timer_list	timeout;
 	struct work_struct	timeout_work;
 	struct list_head	timeout_list;
+	bool			timeout_off;
 
 	struct list_head	icq_list;
 #ifdef CONFIG_BLK_CGROUP
@@ -1017,6 +1018,18 @@ extern void blk_execute_rq(struct request_queue *, struct gendisk *,
 extern void blk_execute_rq_nowait(struct request_queue *, struct gendisk *,
 				  struct request *, int, rq_end_io_fn *);
 
+static inline void blk_mark_timeout_quiesce(struct request_queue *q, bool quiesce)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(q->queue_lock, flags);
+	q->timeout_off = quiesce;
+	spin_unlock_irqrestore(q->queue_lock, flags);
+}
+
+extern void blk_quiesce_timeout(struct request_queue *q);
+extern void blk_unquiesce_timeout(struct request_queue *q);
+
 int blk_status_to_errno(blk_status_t status);
 blk_status_t errno_to_blk_status(int errno);
 
Signature · 1 line
2.9.5
author
#2 lore raw

PATCH v3 2/8 nvme: pci: cover timeout for admin commands running in EH

In reply to Ming Lei

When admin commands are used in EH for recovering controller, we have to cover their timeout and can't depend on block's timeout since deadlock may be caused when these commands are timed-out by block layer again.

Cc Jianchao Wang <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Laurence Oberman <[email protected]>
---
 drivers/nvme/host/pci.c | 77 ++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 66 insertions(+), 11 deletions(-)
drivers/nvme/host/pci.c +66 -11
index fbc71fac6f1e..0e6cd605164a 100644
@@ -1733,21 +1733,28 @@ static inline void nvme_release_cmb(struct nvme_dev *dev)
 	}
 }
 
-static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
+static void nvme_init_set_host_mem_cmd(struct nvme_dev *dev,
+		struct nvme_command *c, u32 bits)
 {
 	u64 dma_addr = dev->host_mem_descs_dma;
+
+	memset(c, 0, sizeof(*c));
+	c->features.opcode	= nvme_admin_set_features;
+	c->features.fid		= cpu_to_le32(NVME_FEAT_HOST_MEM_BUF);
+	c->features.dword11	= cpu_to_le32(bits);
+	c->features.dword12	= cpu_to_le32(dev->host_mem_size >>
+					      ilog2(dev->ctrl.page_size));
+	c->features.dword13	= cpu_to_le32(lower_32_bits(dma_addr));
+	c->features.dword14	= cpu_to_le32(upper_32_bits(dma_addr));
+	c->features.dword15	= cpu_to_le32(dev->nr_host_mem_descs);
+}
+
+static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
+{
 	struct nvme_command c;
 	int ret;
 
-	memset(&c, 0, sizeof(c));
-	c.features.opcode	= nvme_admin_set_features;
-	c.features.fid		= cpu_to_le32(NVME_FEAT_HOST_MEM_BUF);
-	c.features.dword11	= cpu_to_le32(bits);
-	c.features.dword12	= cpu_to_le32(dev->host_mem_size >>
-					      ilog2(dev->ctrl.page_size));
-	c.features.dword13	= cpu_to_le32(lower_32_bits(dma_addr));
-	c.features.dword14	= cpu_to_le32(upper_32_bits(dma_addr));
-	c.features.dword15	= cpu_to_le32(dev->nr_host_mem_descs);
+	nvme_init_set_host_mem_cmd(dev, &c, bits);
 
 	ret = nvme_submit_sync_cmd(dev->ctrl.admin_q, &c, NULL, 0);
 	if (ret) {
@@ -1758,6 +1765,54 @@ static int nvme_set_host_mem(struct nvme_dev *dev, u32 bits)
 	return ret;
 }
 
+static void nvme_set_host_mem_end_io(struct request *rq, blk_status_t sts)
+{
+	struct completion *waiting = rq->end_io_data;
+
+	rq->end_io_data = NULL;
+
+	/*
+	 * complete last, if this is a stack request the process (and thus
+	 * the rq pointer) could be invalid right after this complete()
+	 */
+	complete(waiting);
+}
+
+/*
+ * This function can be used inside timeout handler, when the block layer
+ * timeout may not work, so this function has to cover the timeout by itself.
+ */
+static int nvme_set_host_mem_timeout(struct nvme_dev *dev, u32 bits)
+{
+	DECLARE_COMPLETION_ONSTACK(wait);
+	struct nvme_command c;
+	struct request_queue *q = dev->ctrl.admin_q;
+	struct request *req;
+	int ret;
+
+	nvme_init_set_host_mem_cmd(dev, &c, bits);
+
+	req = nvme_alloc_request(q, &c, 0, NVME_QID_ANY);
+	if (IS_ERR(req))
+		return PTR_ERR(req);
+
+	req->timeout = ADMIN_TIMEOUT;
+	req->end_io_data = &wait;
+
+	blk_execute_rq_nowait(q, NULL, req, false,
+			nvme_set_host_mem_end_io);
+	wait_for_completion_io_timeout(&wait, ADMIN_TIMEOUT);
+
+	if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
+		ret = -EINTR;
+	else
+		ret = nvme_req(req)->status;
+
+	blk_mq_free_request(req);
+
+	return ret;
+}
+
 static void nvme_free_host_mem(struct nvme_dev *dev)
 {
 	int i;
@@ -2216,7 +2271,7 @@ static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
 		 * but I'd rather be safe than sorry..
 		 */
 		if (dev->host_mem_descs)
-			nvme_set_host_mem(dev, 0);
+			nvme_set_host_mem_timeout(dev, 0);
 		nvme_disable_io_queues(dev);
 		nvme_disable_admin_queue(dev, shutdown);
 	}
Signature · 1 line
2.9.5
author
#3 lore raw

PATCH v3 3/8 nvme: pci: only wait freezing if queue is frozen

In reply to Ming Lei

In nvme_dev_disable() called during shutting down controler, nvme_wait_freeze_timeout() may be done on the controller not frozen yet, so add the check for avoiding the case.

Cc Jianchao Wang <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Laurence Oberman <[email protected]>
---
 drivers/nvme/host/pci.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/nvme/host/pci.c +5 -2
index 0e6cd605164a..8172ee584130 100644
@@ -2240,14 +2240,17 @@ static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
 	int i;
 	bool dead = true;
 	struct pci_dev *pdev = to_pci_dev(dev->dev);
+	bool frozen = false;
 
 	mutex_lock(&dev->shutdown_lock);
 	if (pci_is_enabled(pdev)) {
 		u32 csts = readl(dev->bar + NVME_REG_CSTS);
 
 		if (dev->ctrl.state == NVME_CTRL_LIVE ||
-		    dev->ctrl.state == NVME_CTRL_RESETTING)
+		    dev->ctrl.state == NVME_CTRL_RESETTING) {
 			nvme_start_freeze(&dev->ctrl);
+			frozen = true;
+		}
 		dead = !!((csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
 			pdev->error_state  != pci_channel_io_normal);
 	}
@@ -2257,7 +2260,7 @@ static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
 	 * doing a safe shutdown.
 	 */
 	if (!dead) {
-		if (shutdown)
+		if (shutdown && frozen)
 			nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
 	}
 
Signature · 1 line
2.9.5
author
#4 lore raw

PATCH v3 4/8 nvme: pci: freeze queue in nvme_dev_disable() in case of error recovery

In reply to Ming Lei

When nvme_dev_disable() is used for error recovery, we should always freeze queues before shutdown controller:

- reset handler supposes queues are frozen, and will wait_freeze & unfreeze them explicitly, if queues aren't frozen during nvme_dev_disable(), reset handler may wait forever even though there isn't any requests allocated.

- this way may avoid to cancel lots of requests during error recovery

This patch introduces the parameter of 'freeze_queue' for fixing this issue.

Cc Jianchao Wang <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Laurence Oberman <[email protected]>
---
 drivers/nvme/host/pci.c | 47 ++++++++++++++++++++++++++++++++---------------
 1 file changed, 32 insertions(+), 15 deletions(-)
drivers/nvme/host/pci.c +32 -15
index 8172ee584130..ef80e064a62c 100644
@@ -69,7 +69,8 @@ struct nvme_dev;
 struct nvme_queue;
 
 static void nvme_process_cq(struct nvme_queue *nvmeq);
-static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown);
+static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown, bool
+		freeze_queue);
 
 /*
  * Represents an NVM Express device.  Each nvme_dev is a PCI function.
@@ -1197,7 +1198,7 @@ static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
 	 */
 	if (nvme_should_reset(dev, csts)) {
 		nvme_warn_reset(dev, csts);
-		nvme_dev_disable(dev, false);
+		nvme_dev_disable(dev, false, true);
 		nvme_reset_ctrl(&dev->ctrl);
 		return BLK_EH_HANDLED;
 	}
@@ -1224,7 +1225,7 @@ static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
 		dev_warn(dev->ctrl.device,
 			 "I/O %d QID %d timeout, disable controller\n",
 			 req->tag, nvmeq->qid);
-		nvme_dev_disable(dev, false);
+		nvme_dev_disable(dev, false, false);
 		nvme_req(req)->flags |= NVME_REQ_CANCELLED;
 		return BLK_EH_HANDLED;
 	default:
@@ -1240,7 +1241,7 @@ static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
 		dev_warn(dev->ctrl.device,
 			 "I/O %d QID %d timeout, reset controller\n",
 			 req->tag, nvmeq->qid);
-		nvme_dev_disable(dev, false);
+		nvme_dev_disable(dev, false, true);
 		nvme_reset_ctrl(&dev->ctrl);
 
 		/*
@@ -2235,19 +2236,35 @@ static void nvme_pci_disable(struct nvme_dev *dev)
 	}
 }
 
-static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
+/*
+ * Resetting often follows nvme_dev_disable(), so queues need to be frozen
+ * before resetting.
+ */
+static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown, bool
+		freeze_queue)
 {
 	int i;
 	bool dead = true;
 	struct pci_dev *pdev = to_pci_dev(dev->dev);
 	bool frozen = false;
 
+	/*
+	 * 'freeze_queue' is only valid for non-shutdown, and we do
+	 * inline freeze & wait_freeze_timeout for shutdown just for
+	 * completing as many as possible requests before shutdown
+	 */
+	if (shutdown)
+		freeze_queue = false;
+
+	if (freeze_queue)
+		nvme_start_freeze(&dev->ctrl);
+
 	mutex_lock(&dev->shutdown_lock);
 	if (pci_is_enabled(pdev)) {
 		u32 csts = readl(dev->bar + NVME_REG_CSTS);
 
-		if (dev->ctrl.state == NVME_CTRL_LIVE ||
-		    dev->ctrl.state == NVME_CTRL_RESETTING) {
+		if (shutdown && (dev->ctrl.state == NVME_CTRL_LIVE ||
+		    dev->ctrl.state == NVME_CTRL_RESETTING)) {
 			nvme_start_freeze(&dev->ctrl);
 			frozen = true;
 		}
@@ -2339,7 +2356,7 @@ static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
 	dev_warn(dev->ctrl.device, "Removing after probe failure status: %d\n", status);
 
 	nvme_get_ctrl(&dev->ctrl);
-	nvme_dev_disable(dev, false);
+	nvme_dev_disable(dev, false, false);
 	if (!queue_work(nvme_wq, &dev->remove_work))
 		nvme_put_ctrl(&dev->ctrl);
 }
@@ -2360,7 +2377,7 @@ static void nvme_reset_work(struct work_struct *work)
 	 * moving on.
 	 */
 	if (dev->ctrl.ctrl_config & NVME_CC_ENABLE)
-		nvme_dev_disable(dev, false);
+		nvme_dev_disable(dev, false, false);
 
 	/*
 	 * Introduce CONNECTING state from nvme-fc/rdma transports to mark the
@@ -2609,7 +2626,7 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 static void nvme_reset_prepare(struct pci_dev *pdev)
 {
 	struct nvme_dev *dev = pci_get_drvdata(pdev);
-	nvme_dev_disable(dev, false);
+	nvme_dev_disable(dev, false, true);
 }
 
 static void nvme_reset_done(struct pci_dev *pdev)
@@ -2621,7 +2638,7 @@ static void nvme_reset_done(struct pci_dev *pdev)
 static void nvme_shutdown(struct pci_dev *pdev)
 {
 	struct nvme_dev *dev = pci_get_drvdata(pdev);
-	nvme_dev_disable(dev, true);
+	nvme_dev_disable(dev, true, false);
 }
 
 /*
@@ -2640,13 +2657,13 @@ static void nvme_remove(struct pci_dev *pdev)
 
 	if (!pci_device_is_present(pdev)) {
 		nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_DEAD);
-		nvme_dev_disable(dev, false);
+		nvme_dev_disable(dev, false, false);
 	}
 
 	flush_work(&dev->ctrl.reset_work);
 	nvme_stop_ctrl(&dev->ctrl);
 	nvme_remove_namespaces(&dev->ctrl);
-	nvme_dev_disable(dev, true);
+	nvme_dev_disable(dev, true, false);
 	nvme_free_host_mem(dev);
 	nvme_dev_remove_admin(dev);
 	nvme_free_queues(dev, 0);
@@ -2680,7 +2697,7 @@ static int nvme_suspend(struct device *dev)
 	struct pci_dev *pdev = to_pci_dev(dev);
 	struct nvme_dev *ndev = pci_get_drvdata(pdev);
 
-	nvme_dev_disable(ndev, true);
+	nvme_dev_disable(ndev, true, false);
 	return 0;
 }
 
@@ -2712,7 +2729,7 @@ static pci_ers_result_t nvme_error_detected(struct pci_dev *pdev,
 	case pci_channel_io_frozen:
 		dev_warn(dev->ctrl.device,
 			"frozen state error detected, reset controller\n");
-		nvme_dev_disable(dev, false);
+		nvme_dev_disable(dev, false, true);
 		return PCI_ERS_RESULT_NEED_RESET;
 	case pci_channel_io_perm_failure:
 		dev_warn(dev->ctrl.device,
Signature · 1 line
2.9.5
author
#5 lore raw

PATCH v3 5/8 nvme: fix race between freeze queues and unfreeze queues

In reply to Ming Lei

nvme_dev_disable() and resetting controller are required for recovering controller, but the two are run from different contexts.

nvme_start_freeze() is run from nvme_dev_disable(), and nvme_unfreeze() is run from resetting context. Unfortunatley timeout may be triggered when draining IO from resetting controller, so nvme_start_freeze() may be run several times.

This patch fixes the race between nvme_start_freeze() and nvme_unfreeze() by introducing flags of 'queues_freezed' and the lock of 'freeze_mutex'.

Cc Jianchao Wang <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Laurence Oberman <[email protected]>
---
 drivers/nvme/host/core.c | 29 +++++++++++++++++++++--------
 drivers/nvme/host/nvme.h |  3 +++
 2 files changed, 24 insertions(+), 8 deletions(-)
drivers/nvme/host/core.c +21 -8
index 9df4f71e58ca..f9028873298e 100644
@@ -3447,6 +3447,9 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
 	INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
 	INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
 
+	ctrl->queues_freezed = false;
+	mutex_init(&ctrl->freeze_mutex);
+
 	ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
 	if (ret < 0)
 		goto out;
@@ -3526,10 +3529,15 @@ void nvme_unfreeze(struct nvme_ctrl *ctrl)
 {
 	struct nvme_ns *ns;
 
-	down_read(&ctrl->namespaces_rwsem);
-	list_for_each_entry(ns, &ctrl->namespaces, list)
-		blk_mq_unfreeze_queue(ns->queue);
-	up_read(&ctrl->namespaces_rwsem);
+	mutex_lock(&ctrl->freeze_mutex);
+	if (ctrl->queues_freezed) {
+		down_read(&ctrl->namespaces_rwsem);
+		list_for_each_entry(ns, &ctrl->namespaces, list)
+			blk_mq_unfreeze_queue(ns->queue);
+		up_read(&ctrl->namespaces_rwsem);
+		ctrl->queues_freezed = false;
+	}
+	mutex_unlock(&ctrl->freeze_mutex);
 }
 EXPORT_SYMBOL_GPL(nvme_unfreeze);
 
@@ -3562,10 +3570,15 @@ void nvme_start_freeze(struct nvme_ctrl *ctrl)
 {
 	struct nvme_ns *ns;
 
-	down_read(&ctrl->namespaces_rwsem);
-	list_for_each_entry(ns, &ctrl->namespaces, list)
-		blk_freeze_queue_start(ns->queue);
-	up_read(&ctrl->namespaces_rwsem);
+	mutex_lock(&ctrl->freeze_mutex);
+	if (!ctrl->queues_freezed) {
+		down_read(&ctrl->namespaces_rwsem);
+		list_for_each_entry(ns, &ctrl->namespaces, list)
+			blk_freeze_queue_start(ns->queue);
+		up_read(&ctrl->namespaces_rwsem);
+		ctrl->queues_freezed = true;
+	}
+	mutex_unlock(&ctrl->freeze_mutex);
 }
 EXPORT_SYMBOL_GPL(nvme_start_freeze);
 
drivers/nvme/host/nvme.h +3 -0
index 061fecfd44f5..a19b7f04ac24 100644
@@ -149,6 +149,9 @@ struct nvme_ctrl {
 	struct work_struct reset_work;
 	struct work_struct delete_work;
 
+	bool queues_freezed;
+	struct mutex freeze_mutex;
+
 	struct nvme_subsystem *subsys;
 	struct list_head subsys_entry;
 
Signature · 1 line
2.9.5
author
#6 lore raw

PATCH v3 6/8 nvme: pci: split controller resetting into two parts

In reply to Ming Lei

This patch splits controller resetting into the following two parts:

1) the real resetting part

2) the 2nd part for draining IO and updating controller state

The patch prepares for supporting reliable controller recovery, for example, IO timeout still may be triggered when running the above part 2, so we still have to recover hardware under this situation.

No functional change.

Cc Jianchao Wang <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Laurence Oberman <[email protected]>
---
 drivers/nvme/host/pci.c | 37 +++++++++++++++++++++++++++----------
 1 file changed, 27 insertions(+), 10 deletions(-)
drivers/nvme/host/pci.c +27 -10
index ef80e064a62c..16d7507bfd79 100644
@@ -2361,16 +2361,10 @@ static void nvme_remove_dead_ctrl(struct nvme_dev *dev, int status)
 		nvme_put_ctrl(&dev->ctrl);
 }
 
-static void nvme_reset_work(struct work_struct *work)
+static int nvme_pre_reset_dev(struct nvme_dev *dev)
 {
-	struct nvme_dev *dev =
-		container_of(work, struct nvme_dev, ctrl.reset_work);
 	bool was_suspend = !!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL);
 	int result = -ENODEV;
-	enum nvme_ctrl_state new_state = NVME_CTRL_LIVE;
-
-	if (WARN_ON(dev->ctrl.state != NVME_CTRL_RESETTING))
-		goto out;
 
 	/*
 	 * If we're called to reset a live controller first shut it down before
@@ -2430,8 +2424,13 @@ static void nvme_reset_work(struct work_struct *work)
 	}
 
 	result = nvme_setup_io_queues(dev);
-	if (result)
-		goto out;
+ out:
+	return result;
+}
+
+static void nvme_post_reset_dev(struct nvme_dev *dev)
+{
+	enum nvme_ctrl_state new_state = NVME_CTRL_LIVE;
 
 	/*
 	 * Keep the controller around but remove all namespaces if we don't have
@@ -2465,7 +2464,25 @@ static void nvme_reset_work(struct work_struct *work)
 	return;
 
  out:
-	nvme_remove_dead_ctrl(dev, result);
+	nvme_remove_dead_ctrl(dev, 0);
+}
+
+static void nvme_reset_work(struct work_struct *work)
+{
+	struct nvme_dev *dev =
+		container_of(work, struct nvme_dev, ctrl.reset_work);
+	int ret;
+
+	if (WARN_ON(dev->ctrl.state != NVME_CTRL_RESETTING))
+		return;
+
+	ret = nvme_pre_reset_dev(dev);
+	if (ret) {
+		nvme_remove_dead_ctrl(dev, ret);
+		return;
+	}
+
+	nvme_post_reset_dev(dev);
 }
 
 static void nvme_remove_dead_ctrl_work(struct work_struct *work)
Signature · 1 line
2.9.5
author
#7 lore raw

PATCH v3 7/8 nvme: pci: recover controller reliably

In reply to Ming Lei

During draining IO in resetting controller, error still may happen and timeout can be triggered, the current implementation can't recover controller any more for this situation, this patch fixes the issue by the following approach:

- introduces eh_reset_work, and moves draining IO and updating controller state into this work function.

- resets controller just after nvme_dev_disable(), then schedule eh_reset_work for draining IO & updating controller state

- introduce reset lock to sync between the above two parts

- move nvme_start_queues() into the part for resetting controller, so that timeout even won't quiesce queue any more during draining IO

Cc Jianchao Wang <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Laurence Oberman <[email protected]>
---
 drivers/nvme/host/pci.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 58 insertions(+), 3 deletions(-)
drivers/nvme/host/pci.c +58 -3
index 16d7507bfd79..64bbccdb5091 100644
@@ -71,6 +71,7 @@ struct nvme_queue;
 static void nvme_process_cq(struct nvme_queue *nvmeq);
 static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown, bool
 		freeze_queue);
+static int nvme_eh_reset(struct nvme_dev *dev);
 
 /*
  * Represents an NVM Express device.  Each nvme_dev is a PCI function.
@@ -113,6 +114,10 @@ struct nvme_dev {
 	dma_addr_t host_mem_descs_dma;
 	struct nvme_host_mem_buf_desc *host_mem_descs;
 	void **host_mem_desc_bufs;
+
+	/* reset for timeout */
+	struct mutex reset_lock;
+	struct work_struct eh_reset_work;
 };
 
 static int io_queue_depth_set(const char *val, const struct kernel_param *kp)
@@ -1199,7 +1204,7 @@ static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
 	if (nvme_should_reset(dev, csts)) {
 		nvme_warn_reset(dev, csts);
 		nvme_dev_disable(dev, false, true);
-		nvme_reset_ctrl(&dev->ctrl);
+		nvme_eh_reset(dev);
 		return BLK_EH_HANDLED;
 	}
 
@@ -1242,7 +1247,7 @@ static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
 			 "I/O %d QID %d timeout, reset controller\n",
 			 req->tag, nvmeq->qid);
 		nvme_dev_disable(dev, false, true);
-		nvme_reset_ctrl(&dev->ctrl);
+		nvme_eh_reset(dev);
 
 		/*
 		 * Mark the request as handled, since the inline shutdown
@@ -2424,6 +2429,10 @@ static int nvme_pre_reset_dev(struct nvme_dev *dev)
 	}
 
 	result = nvme_setup_io_queues(dev);
+	if (result)
+		goto out;
+
+	nvme_start_queues(&dev->ctrl);
  out:
 	return result;
 }
@@ -2432,6 +2441,7 @@ static void nvme_post_reset_dev(struct nvme_dev *dev)
 {
 	enum nvme_ctrl_state new_state = NVME_CTRL_LIVE;
 
+	mutex_lock(&dev->reset_lock);
 	/*
 	 * Keep the controller around but remove all namespaces if we don't have
 	 * any working I/O queue.
@@ -2442,8 +2452,12 @@ static void nvme_post_reset_dev(struct nvme_dev *dev)
 		nvme_remove_namespaces(&dev->ctrl);
 		new_state = NVME_CTRL_ADMIN_ONLY;
 	} else {
-		nvme_start_queues(&dev->ctrl);
+		mutex_unlock(&dev->reset_lock);
+
+		/* error may happen during draining IO again */
 		nvme_wait_freeze(&dev->ctrl);
+
+		mutex_lock(&dev->reset_lock);
 		/* hit this only when allocate tagset fails */
 		if (nvme_dev_add(dev))
 			new_state = NVME_CTRL_ADMIN_ONLY;
@@ -2461,10 +2475,12 @@ static void nvme_post_reset_dev(struct nvme_dev *dev)
 	}
 
 	nvme_start_ctrl(&dev->ctrl);
+	mutex_unlock(&dev->reset_lock);
 	return;
 
  out:
 	nvme_remove_dead_ctrl(dev, 0);
+	mutex_unlock(&dev->reset_lock);
 }
 
 static void nvme_reset_work(struct work_struct *work)
@@ -2485,6 +2501,43 @@ static void nvme_reset_work(struct work_struct *work)
 	nvme_post_reset_dev(dev);
 }
 
+static void nvme_eh_reset_work(struct work_struct *work)
+{
+	struct nvme_dev *dev =
+		container_of(work, struct nvme_dev, eh_reset_work);
+
+	nvme_post_reset_dev(dev);
+}
+
+static int nvme_eh_reset(struct nvme_dev *dev)
+{
+	int ret = -EBUSY;
+
+	mutex_lock(&dev->reset_lock);
+	if (dev->ctrl.state != NVME_CTRL_RESETTING &&
+	    dev->ctrl.state != NVME_CTRL_CONNECTING) {
+	    if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING))
+		goto exit;
+	}
+
+	ret = nvme_pre_reset_dev(dev);
+	if (ret) {
+		dev_warn(dev->ctrl.device,
+			"failed to pre-reset controller: %d\n", ret);
+		nvme_remove_dead_ctrl(dev, ret);
+		goto exit;
+	}
+
+	mutex_unlock(&dev->reset_lock);
+
+	queue_work(nvme_reset_wq, &dev->eh_reset_work);
+
+	return 0;
+ exit:
+	mutex_unlock(&dev->reset_lock);
+	return ret;
+}
+
 static void nvme_remove_dead_ctrl_work(struct work_struct *work)
 {
 	struct nvme_dev *dev = container_of(work, struct nvme_dev, remove_work);
@@ -2606,6 +2659,8 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 	if (result)
 		goto put_pci;
 
+	mutex_init(&dev->reset_lock);
+	INIT_WORK(&dev->eh_reset_work, nvme_eh_reset_work);
 	INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work);
 	INIT_WORK(&dev->remove_work, nvme_remove_dead_ctrl_work);
 	mutex_init(&dev->shutdown_lock);
Signature · 1 line
2.9.5
#8 lore raw

Re: [PATCH V3 7/8] nvme: pci: recover controller reliably

Hi ming

Quoted 11 lines · Ming Lei

static int io_queue_depth_set(const char *val, const struct kernel_param *kp)

drivers/nvme/host/pci.c +1 -1
@@ -1199,7 +1204,7 @@ static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
 	if (nvme_should_reset(dev, csts)) {
 		nvme_warn_reset(dev, csts);
 		nvme_dev_disable(dev, false, true);
-		nvme_reset_ctrl(&dev->ctrl);
+		nvme_eh_reset(dev);
 		return BLK_EH_HANDLED;
 	}
 

drivers/nvme/host/pci.c static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved) · line 1250

Quoted 5 lines
drivers/nvme/host/pci.c +1 -1
@@ -1242,7 +1247,7 @@ static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
 			 "I/O %d QID %d timeout, reset controller\n",
 			 req->tag, nvmeq->qid);
 		nvme_dev_disable(dev, false, true);
-		nvme_reset_ctrl(&dev->ctrl);
+		nvme_eh_reset(dev);

w/o the 8th patch, invoke nvme_eh_reset in nvme_timeout is dangerous. nvme_pre_reset_dev will send a lot of admin io when initialize the controller.
if this admin ios timeout, the nvme_timeout cannot handle this because the timeout work is sleeping to wait admin ios.

In addition, even if we take the nvme_wait_freeze out of nvme_eh_reset and put it into another context, but the ctrl state is still CONNECTING, the nvme_eh_reset cannot move forward.

Actually, I used to report this issue to Keith. I met io hung when the controller die in nvme_reset_work -> nvme_wait_freeze. As you know, the nvme_reset_work cannot be scheduled because it is waiting. Here is Keith's commit for this:
http://lists.infradead.org/pipermail/linux-nvme/2018-February/015603.html

Thanks
Jianchao

author
#9 lore raw

Re: [PATCH V3 7/8] nvme: pci: recover controller reliably

Quoted 13 lines · jianchao.wang

Hi ming

Quoted 10 lines · Ming Lei

static int io_queue_depth_set(const char *val, const struct kernel_param *kp)

drivers/nvme/host/pci.c +1 -1
@@ -1199,7 +1204,7 @@ static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
 	if (nvme_should_reset(dev, csts)) {
 		nvme_warn_reset(dev, csts);
 		nvme_dev_disable(dev, false, true);
-		nvme_reset_ctrl(&dev->ctrl);
+		nvme_eh_reset(dev);
 		return BLK_EH_HANDLED;
 	}
 
Quoted 11 lines

drivers/nvme/host/pci.c static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved) · line 1250

Quoted 6 lines
drivers/nvme/host/pci.c +1 -1
@@ -1242,7 +1247,7 @@ static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved)
 			 "I/O %d QID %d timeout, reset controller\n",
 			 req->tag, nvmeq->qid);
 		nvme_dev_disable(dev, false, true);
-		nvme_reset_ctrl(&dev->ctrl);
+		nvme_eh_reset(dev);

w/o the 8th patch, invoke nvme_eh_reset in nvme_timeout is dangerous. nvme_pre_reset_dev will send a lot of admin io when initialize the controller.
if this admin ios timeout, the nvme_timeout cannot handle this because the timeout work is sleeping to wait admin ios.

I just tried to not make the 8th patch too big, but looks we have to merge the two.

Once the EH kthread is introduced in 8th patch, there isn't such issue any more.

Quoted 3 lines

In addition, even if we take the nvme_wait_freeze out of nvme_eh_reset and put it into another context, but the ctrl state is still CONNECTING, the nvme_eh_reset cannot move forward.

nvme_eh_reset() can move on, if controller state is either CONNECTING or RESETTING, nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING) won't be called in nvme_eh_reset(), and nvme_pre_reset_dev() will be called directly, then follows scheduling of the eh_reset_work.

Quoted 5 lines

Actually, I used to report this issue to Keith. I met io hung when the controller die in nvme_reset_work -> nvme_wait_freeze. As you know, the nvme_reset_work cannot be scheduled because it is waiting. Here is Keith's commit for this:
http://lists.infradead.org/pipermail/linux-nvme/2018-February/015603.html

There are actually two issues here, both are covered in this patchset:

1) when nvme_wait_freeze() is for draining IO, controller error may happen again, this patch can shutdown & reset controller again to recover it.

2) there is also another issue: queues may not be put into freeze state in nvme_dev_disable(), then nvme_wait_freeze() will wait forever. This issue is addressed by 4th patch.

Both two can be observed in blktests block/011 and verified by this patches.

Thanks for your great review, please let me know if there are other issues.

Thanks,
Ming

#10 lore raw

Re: [PATCH V3 7/8] nvme: pci: recover controller reliably

Hi Ming

Thanks for your kindly response.

Quoted 4 lines · Ming Lei

nvme_eh_reset() can move on, if controller state is either CONNECTING or RESETTING, nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING) won't be called in nvme_eh_reset(), and nvme_pre_reset_dev() will be called directly, then follows scheduling of the eh_reset_work.

We have to consider another context, nvme_reset_work.
There are two contexts that will invoke nvme_pre_reset_dev.
nvme_eh_reset could invoke nvme_pre_reset_dev even if the state is RESETTING or CONNECTING.

nvme_error_handler nvme_reset_ctrl

                                    -> change state to RESETTING
                                    -> queue reset work
                                  nvme_reset_work          
  -> nvme_dev_disable                                     
  -> nvme_eh_reset
    -> nvme_pre_reset_dev          -> nvme_pre_reset_dev
      -> change state to CONNECTING   -> change state fails
                                   -> nvme_remove_dead_ctrl

There seems to be other race scenarios between them.

Just invoke nvme_dev_disable in nvme_error_handler context and hand over the other things to nvme_reset_work as the v2 patch series seems clearer.
The primary target of nvme_error_handler is to drain IOs and disable controller. reset_work could take over the left things of the recovery work

IMO, modify the nvme_reset_ctrl_sync in nvme_error_handler to nvme_reset_ctrl should be ok. If the admin ios in reset_work timeout, the nvme_error_handler could also provide help to complete them.

Quoted 6 lines

There are actually two issues here, both are covered in this patchset:

1) when nvme_wait_freeze() is for draining IO, controller error may happen again, this patch can shutdown & reset controller again to recover it.

We could split the nvme_reset_work into two contexts instead of introduce another nvme_eh_reset. Otherwise, the code looks really complicated.
In addition, this fix for this issue could be deferred after your great EH kthread solution is submitted.

Thanks
Jianchao

Quoted 8 lines

2) there is also another issue: queues may not be put into freeze state in nvme_dev_disable(), then nvme_wait_freeze() will wait forever. This issue is addressed by 4th patch.

Both two can be observed in blktests block/011 and verified by this patches.

Thanks for your great review, please let me know if there are other issues.

author
#11 lore raw

Re: [PATCH V3 7/8] nvme: pci: recover controller reliably

Quoted 23 lines · jianchao.wang

Hi Ming

Thanks for your kindly response.

Quoted 4 lines · Ming Lei

nvme_eh_reset() can move on, if controller state is either CONNECTING or RESETTING, nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_RESETTING) won't be called in nvme_eh_reset(), and nvme_pre_reset_dev() will be called directly, then follows scheduling of the eh_reset_work.

We have to consider another context, nvme_reset_work.
There are two contexts that will invoke nvme_pre_reset_dev.
nvme_eh_reset could invoke nvme_pre_reset_dev even if the state is RESETTING or CONNECTING.

nvme_error_handler nvme_reset_ctrl

                                    -> change state to RESETTING
                                    -> queue reset work
                                  nvme_reset_work          
  -> nvme_dev_disable                                     
  -> nvme_eh_reset
    -> nvme_pre_reset_dev          -> nvme_pre_reset_dev
      -> change state to CONNECTING   -> change state fails
                                   -> nvme_remove_dead_ctrl
Quoted 1 line

There seems to be other race scenarios between them.

IMO it is fine to change the change state in nvme_pre_reset_dev() as the following way:

if (dev->ctrl.state != NVME_CTRL_CONNECTING)

	if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_CONNECTING))
		goto out;

Also dev->reset_lock has been introduced, and we may hold it for nvme_pre_reset_dev() in the path of nvme_reset_ctrl() too, then there isn't the above race any more.

Quoted 3 lines

Just invoke nvme_dev_disable in nvme_error_handler context and hand over the other things to nvme_reset_work as the v2 patch series seems clearer.

That way may not fix the current race: nvme_dev_disable() will quiesce/freeze queue again when resetting is in-progress, then nothing can move on.

One big change in V3 is to run nvme_dev_disable() & nvme_pre_reset_dev() in the EH thread, and make sure queues are started once nvme_pre_reset_dev() returns. But as you pointed, it may not work well enough if admin requests are timed-out in nvme_pre_reset_dev().

Quoted 2 lines

The primary target of nvme_error_handler is to drain IOs and disable controller. reset_work could take over the left things of the recovery work

Draining IOs isn't necessary, we can remove freezing queues & wait_freeze() from nvme_dev_disable() & nvme_reset_work() for non-shutdown, that can be easy to fix all these races since the .eh_reset_work isn't needed any more, because quiescing is enough to prevent IOs from entering driver/device.

The only issue is that more requests may be failed when reset failed, so I don't want to try that, and just follows the current behaviour.

IMO the primary goal of nvme EH is to recover controller, that is what nvme_dev_disable() and resetting should do, if IO isn't drained, timeout still may be triggered.

The big trouble is about percpu_ref, once the q_usage_counter is killed to start freezing for preventing IO from entering block layer, we have to run blk_mq_freeze_queue_wait() before unfreezing the usage couner. If blk_mq_freeze_queue_wait() isn't needed, things can be much easier for us.

Quoted 4 lines

IMO, modify the nvme_reset_ctrl_sync in nvme_error_handler to nvme_reset_ctrl should be ok. If the admin ios in reset_work timeout, the nvme_error_handler could also provide help to complete them.

Right, that is one goal of this patchset.

Quoted 9 lines
Quoted 6 lines

There are actually two issues here, both are covered in this patchset:

1) when nvme_wait_freeze() is for draining IO, controller error may happen again, this patch can shutdown & reset controller again to recover it.

We could split the nvme_reset_work into two contexts instead of introduce another nvme_eh_reset. Otherwise, the code looks really complicated.

Yeah, that is what I plan to do in V4, introduce .eh_pre_reset_work and .eh_post_reset_work, and run the following things in .eh_pre_reset_work:

- nvme_pre_reset_dev()
- schedule .eh_post_reset_work

and run draining IO & updating controller state in .eh_post_reset_work. .eh_pre_reset_work will be scheduled in nvme_error_handler().

This way may address the issue of admin req timeout in nvme_pre_reset_dev(), what do you think of this approach?

Thanks,
Ming

#12 lore raw

Re: [PATCH V3 7/8] nvme: pci: recover controller reliably

Hi ming

Quoted 5 lines · Ming Lei
Quoted 2 lines

Just invoke nvme_dev_disable in nvme_error_handler context and hand over the other things to nvme_reset_work as the v2 patch series seems clearer.

That way may not fix the current race: nvme_dev_disable() will quiesce/freeze queue again when resetting is in-progress, then nothing can move on.

With timeout quiesce and nvme EH kthread, the nvme_dev_disable could ensure all the in-flight requests to be handled. When we queue the reset_work in nvme_error_handler, there will be no timeout anymore.
If there is timeout due to the admin io in reset_work, this is expected, reset_work will be interrupted and fail.
If the io timeout when reset_work wait freeze, because the reset_work is waiting, things cannot move on. we have multiple method to handle this:
1. as Keith's solution, change ctrl state to DELETING and fail the reset_work.
2. as current patch set, try to recovery it. but we have to split reset_work into two contexts.

Actually, there are many other places where the nvme_dev_disable will be invoked.

Quoted 16 lines

One big change in V3 is to run nvme_dev_disable() & nvme_pre_reset_dev() in the EH thread, and make sure queues are started once nvme_pre_reset_dev() returns. But as you pointed, it may not work well enough if admin requests are timed-out in nvme_pre_reset_dev().

Quoted 2 lines

The primary target of nvme_error_handler is to drain IOs and disable controller. reset_work could take over the left things of the recovery work

Draining IOs isn't necessary, we can remove freezing queues & wait_freeze() from nvme_dev_disable() & nvme_reset_work() for non-shutdown, that can be easy to fix all these races since the .eh_reset_work isn't needed any more, because quiescing is enough to prevent IOs from entering driver/device.

The only issue is that more requests may be failed when reset failed, so I don't want to try that, and just follows the current behaviour.

Quoted 8 lines

IMO the primary goal of nvme EH is to recover controller, that is what nvme_dev_disable() and resetting should do, if IO isn't drained, timeout still may be triggered.

The big trouble is about percpu_ref, once the q_usage_counter is killed to start freezing for preventing IO from entering block layer, we have to run blk_mq_freeze_queue_wait() before unfreezing the usage couner. If blk_mq_freeze_queue_wait() isn't needed, things can be much easier for us.

Sorry for my bad description. I mean nvme_dev_disable will drain all the in-flight requests and requeue or fail them, then we will not get any timeout again.

In fact, I used to talk with Keith about remove freezing queues & wait_freeze for non-shutdown. There is an obstacle, nvme_dev_add -> blk_mq_update_nr_hw_queues.
When the ctrl comes back, the number of hw queues maybe changed.
Even if we move the wait_freeze, blk_mq_update_nr_hw_queues will do that.
On the other hand, if not freeze the queue, the requests that are submitted to the dying hw queue during the resetting, will be sacrificed.

Quoted 17 lines
Quoted 4 lines

IMO, modify the nvme_reset_ctrl_sync in nvme_error_handler to nvme_reset_ctrl should be ok. If the admin ios in reset_work timeout, the nvme_error_handler could also provide help to complete them.

Right, that is one goal of this patchset.

Quoted 8 lines
Quoted 6 lines

There are actually two issues here, both are covered in this patchset:

1) when nvme_wait_freeze() is for draining IO, controller error may happen again, this patch can shutdown & reset controller again to recover it.

We could split the nvme_reset_work into two contexts instead of introduce another nvme_eh_reset. Otherwise, the code looks really complicated.

Yeah, that is what I plan to do in V4, introduce .eh_pre_reset_work and .eh_post_reset_work, and run the following things in .eh_pre_reset_work:

Quoted 8 lines

- nvme_pre_reset_dev()
- schedule .eh_post_reset_work

and run draining IO & updating controller state in .eh_post_reset_work. .eh_pre_reset_work will be scheduled in nvme_error_handler().

This way may address the issue of admin req timeout in nvme_pre_reset_dev(), what do you think of this approach?

nvme_error_handler should invoke nvme_reset_ctrl instead of introducing another interface. Then it is more convenient to ensure that there will be only one resetting instance running.

Thanks
Jianchao

#13 lore raw

Re: [PATCH V3 7/8] nvme: pci: recover controller reliably

Oh sorry.

Quoted 2 lines · jianchao.wang

nvme_error_handler should invoke nvme_reset_ctrl instead of introducing another interface. Then it is more convenient to ensure that there will be only one resetting instance running.

ctrl state is still in RESETTING state, nvme_reset_ctrl cannot work.

Thanks
Jianchao

author
#14 lore raw

Re: [PATCH V3 7/8] nvme: pci: recover controller reliably

In reply to jianchao.wang
Quoted 5 lines · jianchao.wang

Hi ming

Quoted 2 lines · Ming Lei
Quoted 2 lines

Just invoke nvme_dev_disable in nvme_error_handler context and hand over the other things to nvme_reset_work as the v2 patch series seems clearer.

Quoted 7 lines
Quoted 3 lines

That way may not fix the current race: nvme_dev_disable() will quiesce/freeze queue again when resetting is in-progress, then nothing can move on.

With timeout quiesce and nvme EH kthread, the nvme_dev_disable could ensure all the in-flight requests to be handled. When we queue the reset_work in nvme_error_handler, there will be no timeout anymore.

Right.

Quoted 2 lines

If there is timeout due to the admin io in reset_work, this is expected, reset_work will be interrupted and fail.

At least with V3, the admin io submitted in reset_work won't be handled well. When EH thread hangs in the sync admin io, nvme_eh_schedule() can't wakeup the EH thread any more, then the admin io may hang forever in reset_work. Then we have to run nvme_pre_reset_dev() in another context.

Quoted 5 lines

If the io timeout when reset_work wait freeze, because the reset_work is waiting, things cannot move on. we have multiple method to handle this:
1. as Keith's solution, change ctrl state to DELETING and fail the reset_work.
2. as current patch set, try to recovery it. but we have to split reset_work into two contexts.

Right, nvme_dev_disable() can guarantee forward progress, and we have to make sure that nvme_dev_disable() is called when timeout happens.

Quoted 21 lines

Actually, there are many other places where the nvme_dev_disable will be invoked.

Quoted 20 lines

One big change in V3 is to run nvme_dev_disable() & nvme_pre_reset_dev() in the EH thread, and make sure queues are started once nvme_pre_reset_dev() returns. But as you pointed, it may not work well enough if admin requests are timed-out in nvme_pre_reset_dev().

Quoted 2 lines

The primary target of nvme_error_handler is to drain IOs and disable controller. reset_work could take over the left things of the recovery work

Draining IOs isn't necessary, we can remove freezing queues & wait_freeze() from nvme_dev_disable() & nvme_reset_work() for non-shutdown, that can be easy to fix all these races since the .eh_reset_work isn't needed any more, because quiescing is enough to prevent IOs from entering driver/device.

The only issue is that more requests may be failed when reset failed, so I don't want to try that, and just follows the current behaviour.

IMO the primary goal of nvme EH is to recover controller, that is what nvme_dev_disable() and resetting should do, if IO isn't drained, timeout still may be triggered.

Quoted 8 lines
Quoted 4 lines

The big trouble is about percpu_ref, once the q_usage_counter is killed to start freezing for preventing IO from entering block layer, we have to run blk_mq_freeze_queue_wait() before unfreezing the usage couner. If blk_mq_freeze_queue_wait() isn't needed, things can be much easier for us.

Sorry for my bad description. I mean nvme_dev_disable will drain all the in-flight requests and requeue or fail them, then we will not get any timeout again.

OK, got it, sorry for misunderstanding your point.

Quoted 7 lines

In fact, I used to talk with Keith about remove freezing queues & wait_freeze for non-shutdown. There is an obstacle, nvme_dev_add -> blk_mq_update_nr_hw_queues.
When the ctrl comes back, the number of hw queues maybe changed.
Even if we move the wait_freeze, blk_mq_update_nr_hw_queues will do that.
On the other hand, if not freeze the queue, the requests that are submitted to the dying hw queue during the resetting, will be sacrificed.

Right.

Quoted 22 lines
Quoted 22 lines
Quoted 6 lines

IMO, modify the nvme_reset_ctrl_sync in nvme_error_handler to nvme_reset_ctrl should be ok. If the admin ios in reset_work timeout, the nvme_error_handler could also provide help to complete them.

Right, that is one goal of this patchset.

Quoted 8 lines
Quoted 6 lines

There are actually two issues here, both are covered in this patchset:

1) when nvme_wait_freeze() is for draining IO, controller error may happen again, this patch can shutdown & reset controller again to recover it.

We could split the nvme_reset_work into two contexts instead of introduce another nvme_eh_reset. Otherwise, the code looks really complicated.

Yeah, that is what I plan to do in V4, introduce .eh_pre_reset_work and .eh_post_reset_work, and run the following things in .eh_pre_reset_work:

- nvme_pre_reset_dev()
- schedule .eh_post_reset_work

Quoted 9 lines
Quoted 5 lines

and run draining IO & updating controller state in .eh_post_reset_work. .eh_pre_reset_work will be scheduled in nvme_error_handler().

This way may address the issue of admin req timeout in nvme_pre_reset_dev(), what do you think of this approach?

nvme_error_handler should invoke nvme_reset_ctrl instead of introducing another interface. Then it is more convenient to ensure that there will be only one resetting instance running.

But as you mentioned above, reset_work has to be splitted into two contexts for handling IO timeout during wait_freeze in reset_work, so single instance of nvme_reset_ctrl() may not work well.

Thanks,
Ming

#15 lore raw

Re: [PATCH V3 7/8] nvme: pci: recover controller reliably

Hi ming

Quoted 6 lines · Ming Lei
Quoted 3 lines

nvme_error_handler should invoke nvme_reset_ctrl instead of introducing another interface. Then it is more convenient to ensure that there will be only one resetting instance running.

But as you mentioned above, reset_work has to be splitted into two contexts for handling IO timeout during wait_freeze in reset_work, so single instance of nvme_reset_ctrl() may not work well.

I mean the EH kthread and the reset_work which both could reset the ctrl instead of the pre and post rest context.

Honestly, I suspect a bit that whether it is worthy to try to recover from [1]. The Eh kthread solution could make things easier, but the codes for recovery from [1] has made code really complicated. It is more difficult to unify the nvme-pci, rdma and fc.

How about just fail the resetting as the Keith's solution ?

[1] io timeout when nvme_reset_work or the new nvme_post_reset_dev invoke nvme_wait_freeze.

Thanks
Jianchao

author
#16 lore raw

Re: [PATCH V3 7/8] nvme: pci: recover controller reliably

Quoted 6 lines · jianchao.wang

Hi ming

Quoted 3 lines · Ming Lei
Quoted 3 lines

nvme_error_handler should invoke nvme_reset_ctrl instead of introducing another interface. Then it is more convenient to ensure that there will be only one resetting instance running.

Quoted 6 lines
Quoted 3 lines

But as you mentioned above, reset_work has to be splitted into two contexts for handling IO timeout during wait_freeze in reset_work, so single instance of nvme_reset_ctrl() may not work well.

I mean the EH kthread and the reset_work which both could reset the ctrl instead of the pre and post rest context.

That may run nvme_pre_reset_dev() two times from both EH thread and reset_work, looks not good.

Quoted 4 lines

Honestly, I suspect a bit that whether it is worthy to try to recover from [1]. The Eh kthread solution could make things easier, but the codes for recovery from [1] has made code really complicated. It is more difficult to unify the nvme-pci, rdma and fc.

IMO the model is not complicated(one EH thread with two-stage resetting), and it may be cloned to rdma, fc, .. without much difficulty.

Follows the model:

1) single EH thread, in which controller should be guaranteed to shutdown, and EH thread is waken up when controller needs to be recovered.

2) 1st stage resetting: recover controller, which has to run in one work context, since admin commands for recover may timeout.

3) 2nd stage resetting: draining IO & updating controller state to live, which has to run in another context, since IOs during this stage may timeout too.

The reset lock is used to sync between 1st stage resetting and 2nd stage resetting. The '1st stage resetting' is scheduled from EH thread, and the '2nd state resetting' is scheduled after the '1st stage resetting' is done.

The implementation might not be so complicated too, since V3 has partitioned reset_work into two parts, then what we just need to do in V4 is to run each in one work from EH thread.

Quoted 4 lines

How about just fail the resetting as the Keith's solution ?

[1] io timeout when nvme_reset_work or the new nvme_post_reset_dev invoke nvme_wait_freeze.

I'd suggest to not change controller state as DELETING in this case, otherwise it may be reported as another bug, in which controller becomes completely unusable. This issue can be easily triggered by blktests block/011, in this test case, controller is always recoverable.

I will post out V4, and see if all current issues can be covered, and how complicated the implementation is.

Thanks
Ming

#17 lore raw

Re: [PATCH V3 7/8] nvme: pci: recover controller reliably

In reply to jianchao.wang
Quoted 10 lines · jianchao.wang

Hi ming

Quoted 6 lines · Ming Lei
Quoted 3 lines

nvme_error_handler should invoke nvme_reset_ctrl instead of introducing another interface. Then it is more convenient to ensure that there will be only one resetting instance running.

But as you mentioned above, reset_work has to be splitted into two contexts for handling IO timeout during wait_freeze in reset_work, so single instance of nvme_reset_ctrl() may not work well.

Quoted 6 lines

I mean the EH kthread and the reset_work which both could reset the ctrl instead of the pre and post rest context.

Honestly, I suspect a bit that whether it is worthy to try to recover from [1]. The Eh kthread solution could make things easier, but the codes for recovery from [1] has made code really complicated. It is more difficult to unify the nvme-pci, rdma and fc.

Another choice may be nested EH, which should be easier to implement:

- run the whole recovery procedures(shutdown & reset) in one single context
- and start a new context to handle new timeout during last recovery in the same way

The two approaches is just like sync IO vs AIO.

Thanks,
Ming Lei

author
#18 lore raw

PATCH v3 8/8 nvme: pci: simplify timeout handling

In reply to Ming Lei

When one req is timed out, now nvme_timeout() handles it by the following way:

	nvme_dev_disable(dev, false);
	nvme_reset_ctrl(&dev->ctrl);
	return BLK_EH_HANDLED.

And block's timeout handler is per-request-queue, that means each namespace's error handling has to shutdown & reset the whole controller.

This patch moves the error handler into one per-controller kthread:

- Move timeout handling into one EH thread, and wakeup this thread for handling timedout event.
- Returns BLK_EH_RESET_TIMER for timed-out request so that this request is handled in EH thread after controller is shutdown, and memory corrupt issue won't be worried
- Inside the EH handler, timeout work is drained before canceling in-flight requrests, so all requests can canceled(completed) by EH handler.

With this approach, we don't need to worry about all kinds of race any more, and the model is much simpler than current concurrent timeout model.

Cc Jianchao Wang <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Laurence Oberman <[email protected]>
---
 drivers/nvme/host/core.c |  22 ++++++++
 drivers/nvme/host/nvme.h |   3 ++
 drivers/nvme/host/pci.c  | 133 ++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 145 insertions(+), 13 deletions(-)
drivers/nvme/host/core.c +22 -0
index f9028873298e..664a268accd1 100644
@@ -3582,6 +3582,28 @@ void nvme_start_freeze(struct nvme_ctrl *ctrl)
 }
 EXPORT_SYMBOL_GPL(nvme_start_freeze);
 
+void nvme_unquiesce_timeout(struct nvme_ctrl *ctrl)
+{
+	struct nvme_ns *ns;
+
+	down_read(&ctrl->namespaces_rwsem);
+	list_for_each_entry(ns, &ctrl->namespaces, list)
+		blk_unquiesce_timeout(ns->queue);
+	up_read(&ctrl->namespaces_rwsem);
+}
+EXPORT_SYMBOL_GPL(nvme_unquiesce_timeout);
+
+void nvme_quiesce_timeout(struct nvme_ctrl *ctrl)
+{
+	struct nvme_ns *ns;
+
+	down_read(&ctrl->namespaces_rwsem);
+	list_for_each_entry(ns, &ctrl->namespaces, list)
+		blk_quiesce_timeout(ns->queue);
+	up_read(&ctrl->namespaces_rwsem);
+}
+EXPORT_SYMBOL_GPL(nvme_quiesce_timeout);
+
 void nvme_stop_queues(struct nvme_ctrl *ctrl)
 {
 	struct nvme_ns *ns;
drivers/nvme/host/nvme.h +3 -0
index a19b7f04ac24..956ee19ff403 100644
@@ -19,6 +19,7 @@
 #include <linux/pci.h>
 #include <linux/kref.h>
 #include <linux/blk-mq.h>
+#include <linux/blkdev.h>
 #include <linux/lightnvm.h>
 #include <linux/sed-opal.h>
 #include <linux/fault-inject.h>
@@ -405,6 +406,8 @@ 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_unquiesce_timeout(struct nvme_ctrl *ctrl);
+void nvme_quiesce_timeout(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 +120 -13

219 lines not on this page · raw message

Signature · 1 line
2.9.5