Skip to contentSkip to conversation

blk-mq: Export blk_mq_freeze_queue_wait

PATCH In review Keith Busch 4 participants 13 messages Feb 24, 2017 – Mar 1, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 blk-mq: Export blk_mq_freeze_queue_wait

Drivers can start a freeze, so this provides a way to wait for frozen.

---
 block/blk-mq.c         | 3 ++-
 include/linux/blk-mq.h | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)
block/blk-mq.c +2 -1
index 9e6b064..d5b9a15 100644
@@ -75,10 +75,11 @@ void blk_mq_freeze_queue_start(struct request_queue *q)
 }
 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
 
-static void blk_mq_freeze_queue_wait(struct request_queue *q)
+void blk_mq_freeze_queue_wait(struct request_queue *q)
 {
 	wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
 }
+EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
 
 /*
  * Guarantee no request is in use, so we can change any data structure of
include/linux/blk-mq.h +1 -0
index 001d30d..8dacf68 100644
@@ -245,6 +245,7 @@ void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset,
 void blk_mq_freeze_queue(struct request_queue *q);
 void blk_mq_unfreeze_queue(struct request_queue *q);
 void blk_mq_freeze_queue_start(struct request_queue *q);
+void blk_mq_freeze_queue_wait(struct request_queue *q);
 int blk_mq_reinit_tagset(struct blk_mq_tag_set *set);
 
 int blk_mq_map_queues(struct blk_mq_tag_set *set);
Signature · 1 line
2.5.5
#1 lore raw

PATCH v2 2/2 nvme: Complete all stuck requests

If the block layer has entered requests and gets a CPU hot plug event prior to the resume event, it will wait for those requests to exit. If the nvme driver is shutting down, it will not start the queues back up, preventing forward progress.

To fix that, this patch freezes the request queues when the driver intends to shut down the controller so that no new requests may enter. After the controller has been disabled, the queues will be restarted to force all entered requests to end in failure so that blk-mq's hot cpu notifier may progress. To ensure the queue usage count is 0 on a shutdown, the driver waits for freeze to complete before completing the controller shutdown.

On resume, the driver will unfreeze the queue for new requests to enter once the hardware contexts are reinitialized.

---
v1 -> v2:
Simplified the freeze and waiting, using the new blk API.

 drivers/nvme/host/core.c | 33 +++++++++++++++++++++++++++++++++
 drivers/nvme/host/nvme.h |  3 +++
 drivers/nvme/host/pci.c  | 14 ++++++++++++++
 3 files changed, 50 insertions(+)
drivers/nvme/host/core.c +33 -0
index 25ec4e5..9e99b94 100644
@@ -2344,6 +2344,39 @@ void nvme_kill_queues(struct nvme_ctrl *ctrl)
 }
 EXPORT_SYMBOL_GPL(nvme_kill_queues);
 
+void nvme_unfreeze(struct nvme_ctrl *ctrl)
+{
+	struct nvme_ns *ns;
+
+	mutex_lock(&ctrl->namespaces_mutex);
+	list_for_each_entry(ns, &ctrl->namespaces, list)
+		blk_mq_unfreeze_queue(ns->queue);
+	mutex_unlock(&ctrl->namespaces_mutex);
+}
+EXPORT_SYMBOL_GPL(nvme_unfreeze);
+
+void nvme_wait_freeze(struct nvme_ctrl *ctrl)
+{
+	struct nvme_ns *ns;
+
+	mutex_lock(&ctrl->namespaces_mutex);
+	list_for_each_entry(ns, &ctrl->namespaces, list)
+		blk_mq_freeze_queue_wait(ns->queue);
+	mutex_unlock(&ctrl->namespaces_mutex);
+}
+EXPORT_SYMBOL_GPL(nvme_wait_freeze);
+
+void nvme_start_freeze(struct nvme_ctrl *ctrl)
+{
+	struct nvme_ns *ns;
+
+	mutex_lock(&ctrl->namespaces_mutex);
+	list_for_each_entry(ns, &ctrl->namespaces, list)
+		blk_mq_freeze_queue_start(ns->queue);
+	mutex_unlock(&ctrl->namespaces_mutex);
+}
+EXPORT_SYMBOL_GPL(nvme_start_freeze);
+
 void nvme_stop_queues(struct nvme_ctrl *ctrl)
 {
 	struct nvme_ns *ns;
drivers/nvme/host/nvme.h +3 -0
index a3da1e9..62af901 100644
@@ -294,6 +294,9 @@ void nvme_queue_async_events(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);
+void nvme_unfreeze(struct nvme_ctrl *ctrl);
+void nvme_wait_freeze(struct nvme_ctrl *ctrl);
+void nvme_start_freeze(struct nvme_ctrl *ctrl);
 
 #define NVME_QID_ANY -1
 struct request *nvme_alloc_request(struct request_queue *q,
drivers/nvme/host/pci.c +14 -0
index 57a1af5..ce80cb5 100644
@@ -1676,6 +1676,8 @@ static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
 	del_timer_sync(&dev->watchdog_timer);
 
 	mutex_lock(&dev->shutdown_lock);
+	if (shutdown)
+		nvme_start_freeze(&dev->ctrl);
 	if (pci_is_enabled(to_pci_dev(dev->dev))) {
 		nvme_stop_queues(&dev->ctrl);
 		csts = readl(dev->bar + NVME_REG_CSTS);
@@ -1700,6 +1702,16 @@ static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
 
 	blk_mq_tagset_busy_iter(&dev->tagset, nvme_cancel_request, &dev->ctrl);
 	blk_mq_tagset_busy_iter(&dev->admin_tagset, nvme_cancel_request, &dev->ctrl);
+
+	/*
+	 * The driver will not be starting up queues again if shutting down so
+	 * must flush all entered requests to their failed completion to avoid
+	 * deadlocking blk-mq hot-cpu notifier.
+	 */
+	if (shutdown) {
+		nvme_start_queues(&dev->ctrl);
+		nvme_wait_freeze(&dev->ctrl);
+	}
 	mutex_unlock(&dev->shutdown_lock);
 }
 
@@ -1823,6 +1835,8 @@ static void nvme_reset_work(struct work_struct *work)
 	} else {
 		nvme_start_queues(&dev->ctrl);
 		nvme_dev_add(dev);
+		if (was_suspended)
+			nvme_unfreeze(&dev->ctrl);
 	}
 
 	if (!nvme_change_ctrl_state(&dev->ctrl, NVME_CTRL_LIVE)) {
Signature · 1 line
2.5.5
maintainer
#3 lore raw

Re: [PATCHv2 2/2] nvme: Complete all stuck requests

In reply to Keith Busch
Quoted 11 lines · Keith Busch

If the block layer has entered requests and gets a CPU hot plug event prior to the resume event, it will wait for those requests to exit. If the nvme driver is shutting down, it will not start the queues back up, preventing forward progress.

To fix that, this patch freezes the request queues when the driver intends to shut down the controller so that no new requests may enter. After the controller has been disabled, the queues will be restarted to force all entered requests to end in failure so that blk-mq's hot cpu notifier may progress. To ensure the queue usage count is 0 on a shutdown, the driver waits for freeze to complete before completing the controller shutdown.

Keith, can you explain (again) for me why is the freeze_wait must happen after the controller has been disabled, instead of starting the queues and waiting right after freeze start?

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#4 lore raw

Re: [PATCHv2 2/2] nvme: Complete all stuck requests

Quoted 13 lines · Sagi Grimberg
Quoted 11 lines · Keith Busch

If the block layer has entered requests and gets a CPU hot plug event prior to the resume event, it will wait for those requests to exit. If the nvme driver is shutting down, it will not start the queues back up, preventing forward progress.

To fix that, this patch freezes the request queues when the driver intends to shut down the controller so that no new requests may enter. After the controller has been disabled, the queues will be restarted to force all entered requests to end in failure so that blk-mq's hot cpu notifier may progress. To ensure the queue usage count is 0 on a shutdown, the driver waits for freeze to complete before completing the controller shutdown.

Quoted 3 lines

Keith, can you explain (again) for me why is the freeze_wait must happen after the controller has been disabled, instead of starting the queues and waiting right after freeze start?

Yeah, the driver needs to make forward progress even if the controller isn't functioning. If we do the freeze wait before disabling the controller, there's no way to reclaim missing completions. If the controller is working perfectly, it'd be okay, but the driver would be stuck if there's a problem.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

maintainer
#5 lore raw

Re: [PATCHv2 2/2] nvme: Complete all stuck requests

Quoted 17 lines
Quoted 13 lines
Quoted 6 lines

If the block layer has entered requests and gets a CPU hot plug event prior to the resume event, it will wait for those requests to exit. If the nvme driver is shutting down, it will not start the queues back up, preventing forward progress.

Quoted 6 lines

To fix that, this patch freezes the request queues when the driver intends to shut down the controller so that no new requests may enter. After the controller has been disabled, the queues will be restarted to force all entered requests to end in failure so that blk-mq's hot cpu notifier may progress. To ensure the queue usage count is 0 on a shutdown, the driver waits for freeze to complete before completing the controller shutdown.

Quoted 3 lines

Keith, can you explain (again) for me why is the freeze_wait must happen after the controller has been disabled, instead of starting the queues and waiting right after freeze start?

Quoted 5 lines

Yeah, the driver needs to make forward progress even if the controller isn't functioning. If we do the freeze wait before disabling the controller, there's no way to reclaim missing completions. If the controller is working perfectly, it'd be okay, but the driver would be stuck if there's a problem.

OK, I think we can get it for fabrics too, need to figure out how to handle it there too.

Do you have a reproducer?

#6 lore raw

Re: [PATCHv2 2/2] nvme: Complete all stuck requests

Quoted 4 lines · Sagi Grimberg

OK, I think we can get it for fabrics too, need to figure out how to handle it there too.

Do you have a reproducer?

To repro, I have to run a buffered writer workload then put the system into S3.

This fio job seems to reproduce for me:

  fio --name=global --filename=/dev/nvme0n1 --bsrange=4k-128k --rw=randwrite --ioengine=libaio --iodepth=8 --numjobs=8 --name=foobar

I use rtcwake to test suspend/resume:

rtcwake -m mem -s 10

Without the patch we'll get stuck after "Disabling non-boot CPUs ..." when blk-mq waits to freeze some entered queues after nvme was disabled.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#7 lore raw

Re: [PATCHv2 2/2] nvme: Complete all stuck requests

Quoted 10 lines · Keith Busch
Quoted 4 lines · Sagi Grimberg

OK, I think we can get it for fabrics too, need to figure out how to handle it there too.

Do you have a reproducer?

To repro, I have to run a buffered writer workload then put the system into S3.

This fio job seems to reproduce for me:

Quoted 8 lines
  fio --name=global --filename=/dev/nvme0n1 --bsrange=4k-128k --rw=randwrite --ioengine=libaio --iodepth=8 --numjobs=8 --name=foobar

I use rtcwake to test suspend/resume:

rtcwake -m mem -s 10

Without the patch we'll get stuck after "Disabling non-boot CPUs ..." when blk-mq waits to freeze some entered queues after nvme was disabled.

I'm observing the same thing when hibernating during mdraid resync on nvme - it hangs in blk_mq_freeze_queue_wait() after "Disabling non-boot CPUs ...". This patch did not help but when I put nvme_wait_freeze() right after nvme_start_freeze() it appeared to be working. Maybe the difference here is that requests are submitted from a non-freezable kernel thread (md sync_thread)?

Thanks,
Artur

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

maintainer
#8 lore raw

Re: [PATCHv2 2/2] nvme: Complete all stuck requests

Quoted 16 lines
Quoted 16 lines
Quoted 5 lines

OK, I think we can get it for fabrics too, need to figure out how to handle it there too.

Do you have a reproducer?

To repro, I have to run a buffered writer workload then put the system into S3.

This fio job seems to reproduce for me:

  fio --name=global --filename=/dev/nvme0n1 --bsrange=4k-128k --rw=randwrite --ioengine=libaio --iodepth=8 --numjobs=8 --name=foobar

I use rtcwake to test suspend/resume:

rtcwake -m mem -s 10

Quoted 7 lines
Quoted 2 lines

Without the patch we'll get stuck after "Disabling non-boot CPUs ..." when blk-mq waits to freeze some entered queues after nvme was disabled.

I'm observing the same thing when hibernating during mdraid resync on nvme - it hangs in blk_mq_freeze_queue_wait() after "Disabling non-boot CPUs ...". This patch did not help but when I put nvme_wait_freeze() right after nvme_start_freeze() it appeared to be working.

Interesting. did the nvme device succeeded to shutdown at all?

Quoted 3 lines

Maybe the
difference here is that requests are submitted from a non-freezable kernel thread (md sync_thread)?

Don't think its related...

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#9 lore raw

Re: [PATCHv2 2/2] nvme: Complete all stuck requests

In reply to Artur Paszkiewicz
Quoted 4 lines · Artur Paszkiewicz

I'm observing the same thing when hibernating during mdraid resync on nvme - it hangs in blk_mq_freeze_queue_wait() after "Disabling non-boot CPUs ...".

The patch guarantees forward progress for blk-mq's hot-cpu notifier on nvme request queues by failing all entered requests. It sounds like some part of your setup needs those requests to succeed in order to hibernate.

If your mdraid uses a stacking request_queue that submits retries while it's request queue is entered, that may explain how you remain stuck at blk_mq_freeze_queue_wait.

Quoted 4 lines

This patch did not help but when I put nvme_wait_freeze() right after nvme_start_freeze() it appeared to be working. Maybe the difference here is that requests are submitted from a non-freezable kernel thread (md sync_thread)?

Wait freeze prior to quiescing the queue is ok when the controller is functioning, but it'd be impossible to complete a reset if the controller is in a failed or degraded state.

We probably want to give those requests a chance to succeed, and I think we'd need to be able to timeout the freeze wait. Below are two patches I tested. Prior to these, the fio test would report IO errors from some of its jobs; no errors with these.

---
 block/blk-mq.c         | 9 +++++++++
 include/linux/blk-mq.h | 2 ++
 2 files changed, 11 insertions(+)
block/blk-mq.c +9 -0
index 8da2c04..a5e66a7 100644
@@ -81,6 +81,15 @@ void blk_mq_freeze_queue_wait(struct request_queue *q)
 }
 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
 
+int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
+				     unsigned long timeout)
+{
+	return wait_event_timeout(q->mq_freeze_wq,
+					percpu_ref_is_zero(&q->q_usage_counter),
+					timeout);
+}
+EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
+
 /*
  * Guarantee no request is in use, so we can change any data structure of
  * the queue afterward.
include/linux/blk-mq.h +2 -0
index 8dacf68..b296a90 100644
@@ -246,6 +246,8 @@ void blk_mq_freeze_queue(struct request_queue *q);
 void blk_mq_unfreeze_queue(struct request_queue *q);
 void blk_mq_freeze_queue_start(struct request_queue *q);
 void blk_mq_freeze_queue_wait(struct request_queue *q);
+int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
+				     unsigned long timeout);
 int blk_mq_reinit_tagset(struct blk_mq_tag_set *set);
 
 int blk_mq_map_queues(struct blk_mq_tag_set *set);
Signature · 5 lines
---
 drivers/nvme/host/core.c | 14 ++++++++++++++
 drivers/nvme/host/nvme.h |  1 +
 drivers/nvme/host/pci.c  | 18 ++++++++++++++----
 3 files changed, 29 insertions(+), 4 deletions(-)
drivers/nvme/host/core.c +14 -0
index 9e99b94..9b3b57f 100644
@@ -2355,6 +2355,20 @@ void nvme_unfreeze(struct nvme_ctrl *ctrl)
 }
 EXPORT_SYMBOL_GPL(nvme_unfreeze);
 
+void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
+{
+	struct nvme_ns *ns;
+
+	mutex_lock(&ctrl->namespaces_mutex);
+	list_for_each_entry(ns, &ctrl->namespaces, list) {
+		timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
+		if (timeout <= 0)
+			break;
+	}
+	mutex_unlock(&ctrl->namespaces_mutex);
+}
+EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
+
 void nvme_wait_freeze(struct nvme_ctrl *ctrl)
 {
 	struct nvme_ns *ns;
drivers/nvme/host/nvme.h +1 -0
index 62af901..2aa20e3 100644
@@ -296,6 +296,7 @@ void nvme_start_queues(struct nvme_ctrl *ctrl);
 void nvme_kill_queues(struct nvme_ctrl *ctrl);
 void nvme_unfreeze(struct nvme_ctrl *ctrl);
 void nvme_wait_freeze(struct nvme_ctrl *ctrl);
+void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout);
 void nvme_start_freeze(struct nvme_ctrl *ctrl);
 
 #define NVME_QID_ANY -1
drivers/nvme/host/pci.c +14 -4
index b266fb9..a7a423f 100644
@@ -1691,22 +1691,32 @@ static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)
 {
 	int i, queues;
 	u32 csts = -1;
+	bool dead;
+	struct pci_dev *pdev = to_pci_dev(dev->dev);
 
 	del_timer_sync(&dev->watchdog_timer);
 
 	mutex_lock(&dev->shutdown_lock);
 	if (shutdown)
 		nvme_start_freeze(&dev->ctrl);
-	if (pci_is_enabled(to_pci_dev(dev->dev))) {
-		nvme_stop_queues(&dev->ctrl);
+	if (pci_is_enabled(pdev))
 		csts = readl(dev->bar + NVME_REG_CSTS);
-	}
+	dead = (csts & NVME_CSTS_CFS) || !(csts & NVME_CSTS_RDY) ||
+		pdev->error_state  != pci_channel_io_normal;
+
+	/*
+	 * Give the controller a chance to complete all entered requests if
+	 * doing a safe shutdown.
+	 */
+	if (!dead && shutdown)
+		nvme_wait_freeze_timeout(&dev->ctrl, NVME_IO_TIMEOUT);
+	nvme_stop_queues(&dev->ctrl);
 
 	queues = dev->online_queues - 1;
 	for (i = dev->queue_count - 1; i > 0; i--)
 		nvme_suspend_queue(dev->queues[i]);
 
-	if (csts & NVME_CSTS_CFS || !(csts & NVME_CSTS_RDY)) {
+	if (dead) {
 		/* A device might become IO incapable very soon during
 		 * probe, before the admin queue is configured. Thus,
 		 * queue_count can be 0 here.
Signature · 4 lines
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#10 lore raw

Re: [PATCHv2 2/2] nvme: Complete all stuck requests

Quoted 19 lines · Keith Busch
Quoted 4 lines · Artur Paszkiewicz

I'm observing the same thing when hibernating during mdraid resync on nvme - it hangs in blk_mq_freeze_queue_wait() after "Disabling non-boot CPUs ...".

The patch guarantees forward progress for blk-mq's hot-cpu notifier on nvme request queues by failing all entered requests. It sounds like some part of your setup needs those requests to succeed in order to hibernate.

If your mdraid uses a stacking request_queue that submits retries while it's request queue is entered, that may explain how you remain stuck at blk_mq_freeze_queue_wait.

Quoted 4 lines

This patch did not help but when I put nvme_wait_freeze() right after nvme_start_freeze() it appeared to be working. Maybe the difference here is that requests are submitted from a non-freezable kernel thread (md sync_thread)?

Quoted 8 lines

Wait freeze prior to quiescing the queue is ok when the controller is functioning, but it'd be impossible to complete a reset if the controller is in a failed or degraded state.

We probably want to give those requests a chance to succeed, and I think we'd need to be able to timeout the freeze wait. Below are two patches I tested. Prior to these, the fio test would report IO errors from some of its jobs; no errors with these.

With these patches it works fine. I tested multiple iterations on 2 platforms and they were able to hibernate and resume without issues.

Thanks,
Artur

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme