nvme: pci: fix & improve timeout handling
Subsystems (2)
v2 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 other 3 patches fixes several races wrt. NVMe timeout handler, and finally can make blktests block/011 passed.
Ming Lei (5):
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: fix race between freeze queues and unfreeze queues
nvme: pci: simplify timeout handling
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
block/blk-core.c | 34 +++++++- 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 | 222 ++++++++++++++++++++++++++++++++++++++++++----- include/linux/blkdev.h | 4 + 7 files changed, 296 insertions(+), 31 deletions(-)
Signature · 1 line
2.9.5
PATCH v2 1/5 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.
--- block/blk-core.c | 34 ++++++++++++++++++++++++++++++++-- block/blk-mq.c | 9 +++++++++ block/blk-timeout.c | 5 ++++- include/linux/blkdev.h | 4 ++++ 4 files changed, 49 insertions(+), 3 deletions(-)
block/blk-core.c +32 -2
@@ -388,6 +388,35 @@ void blk_stop_queue(struct request_queue *q) }EXPORT_SYMBOL (blk_stop_queue); +static void __blk_unquiesce_timeout (struct request_queue *q) +{ +unsigned long flags; + +spin_lock_irqsave (q->queue_lock, flags); + q->timeout_off = false; +spin_unlock_irqrestore (q->queue_lock, flags); +} + +void blk_unquiesce_timeout (struct request_queue *q) +{ +__blk_unquiesce_timeout (q); +mod_timer (&q->timeout, jiffies + q->rq_timeout); +} +EXPORT_SYMBOL (blk_unquiesce_timeout); + +void blk_quiesce_timeout (struct request_queue *q) +{ +unsigned long flags; + +spin_lock_irqsave (q->queue_lock, flags); + q->timeout_off = true; +spin_unlock_irqrestore (q->queue_lock, flags); + +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
@@ -408,8 +437,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;
@@ -421,6 +449,8 @@ void blk_sync_queue(struct request_queue *q) }else {cancel_delayed_work_sync (&q->delay_work); } + +__blk_unquiesce_timeout (q); }EXPORT_SYMBOL (blk_sync_queue);
block/blk-mq.c +9 -0
@@ -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
@@ -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 +4 -0
@@ -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
@@ -1011,6 +1012,9 @@ 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 *); +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
PATCH v2 2/5 nvme: pci: cover timeout for admin commands running in EH
In reply to Ming LeiWhen 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.
--- drivers/nvme/host/pci.c | 77 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 11 deletions(-)
drivers/nvme/host/pci.c +66 -11
@@ -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
PATCH v2 3/5 nvme: pci: only wait freezing if queue is frozen
In reply to Ming LeiIn 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.
--- drivers/nvme/host/pci.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/nvme/host/pci.c +5 -2
@@ -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
PATCH v2 4/5 nvme: fix race between freeze queues and unfreeze queues
In reply to Ming Leinvme_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'.
--- 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
@@ -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
@@ -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
PATCH v2 5/5 nvme: pci: simplify timeout handling
In reply to Ming LeiWhen 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.
which may introduces the following issues:
1) the following timeout on other reqs may call nvme_dev_disable() again, which may quiesce queue again when resetting is in-progress, then finally nothing can move on.
2) when timeout is triggered in reset work function, nvme_wait_freeze() may wait forever because now controller can't be recovered at all
This patch fixes the issues by:
1) handle timeout event in one EH thread, and wakeup this thread if controller recovery is needed
2) Inside the EH handler, timeout work is drained by nvme_unquiesce_timeout() before canceling in-flight requrests, so all requests can be canceled or completed by either EH or block timeout handler.
3) Moves the draining IO and unfreezing queues into one post_eh work context, so controller can be recovered always.
This patch fixes reports from the horible test of block/011.
--- drivers/nvme/host/core.c | 22 ++++++++ drivers/nvme/host/nvme.h | 3 + drivers/nvme/host/pci.c | 140 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 155 insertions(+), 10 deletions(-)
drivers/nvme/host/core.c +22 -0
@@ -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
@@ -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 +130 -10
239 lines not on this page · raw message
Signature · 1 line
2.9.5
Re: [PATCH V2 5/5] nvme: pci: simplify timeout handling
Hi Ming
Quoted 57 lines · Ming Lei
drivers/nvme/host/pci.c +8
-8+static enum blk_eh_timer_return nvme_timeout (struct request *req,bool reserved) {struct nvme_iod *iod =blk_mq_rq_to_pdu (req);@@ -1197,8 +1297,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_reset_ctrl (&dev->ctrl); +nvme_eh_schedule (dev);return BLK_EH_HANDLED ; }@@ -1224,8 +1323,8 @@ 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_req (req)->flags |=NVME_REQ_CANCELLED ; +nvme_eh_schedule (dev);return BLK_EH_HANDLED ;default :break ;@@ -1240,14 +1339,12 @@ 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_reset_ctrl (&dev->ctrl); -/* * Mark the request as handled, since the inline shutdown * forces all outstanding requests to complete. */ nvme_req (req)->flags |=NVME_REQ_CANCELLED ; +nvme_eh_schedule (dev);return BLK_EH_HANDLED ; }@@ -2246,8 +2343,8 @@ static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)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; }@@ -2281,11 +2378,23 @@ static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)for (i = dev->ctrl.queue_count -1 ; i >=0 ; i--)nvme_suspend_queue (&dev->queues[i]); +/* safe to sync timeout after queues are quiesced */ +nvme_quiesce_timeout (&dev->ctrl);
drivers/nvme/host/pci.c static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown) · line 2392
Quoted 10 lines
drivers/nvme/host/pci.c +6
-0+blk_quiesce_timeout (dev->ctrl.admin_q); +nvme_pci_disable (dev); +/* +* Both timeout and interrupt handler have been drained, and all +* in-flight requests will be canceled now. +*/ 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);
We need to return BLK_EH_RESET_TIMER in nvme_timeout then:
1. defer the completion. we can't unmap the io request before close the controller totally, so not BLK_EH_HANDLED.
2. nvme_cancel_request could complete it. blk_mq_complete_request is invoked by nvme_cancel_request, so not BLK_EH_NOT_HANDLED.
Thanks
Jianchao
Re: [PATCH V2 1/5] block: introduce blk_quiesce_timeout() and blk_unquiesce_timeout()
In reply to Ming LeiHi ming
Quoted 59 lines · Ming Lei
block/blk-core.c +32
-2+static void __blk_unquiesce_timeout (struct request_queue *q) +{ +unsigned long flags; + +spin_lock_irqsave (q->queue_lock, flags); + q->timeout_off = false; +spin_unlock_irqrestore (q->queue_lock, flags); +} + +void blk_unquiesce_timeout (struct request_queue *q) +{ +__blk_unquiesce_timeout (q); +mod_timer (&q->timeout, jiffies + q->rq_timeout); +} +EXPORT_SYMBOL (blk_unquiesce_timeout); + +void blk_quiesce_timeout (struct request_queue *q) +{ +unsigned long flags; + +spin_lock_irqsave (q->queue_lock, flags); + q->timeout_off = true; +spin_unlock_irqrestore (q->queue_lock, flags); + +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 @@ -408,8 +437,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;@@ -421,6 +449,8 @@ void blk_sync_queue(struct request_queue *q) }else {cancel_delayed_work_sync (&q->delay_work); } + +__blk_unquiesce_timeout (q); }EXPORT_SYMBOL (blk_sync_queue);block/blk-mq.c +0
-0@@ -901,6 +901,15 @@ static void blk_mq_timeout_work(struct work_struct *work) };struct blk_mq_hw_ctx *hctx;
block/blk-mq.c static void blk_mq_timeout_work(struct work_struct *work) · line 912
Quoted 10 lines
block/blk-mq.c +9
-0int 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 ;
Looks like there is still race as following:
blk_quiesce_timeout blk_mq_timeout_work
-> timeout_off = q->timeout_off; //still a false
-> q->timeout_off = true;
-> del_timer_sync(&q->timeout);
-> mod_timer -> cancel_work_sync(&q->timeout_work);
How about this:
Introduce a new request queue flag QUEUE_FLAG_TIMEOUT_QUIESCED
void blk_quiesce_queue_timeout(struct request_queue *q)
{
unsigned long flags;
spin_lock_irqsave(q->queue_lock, flags);
queue_flag_set(QUEUE_FLAG_TIMEOUT_QUIESCED, q);
spin_unlock_irqrestore(q->queue_lock, flags);
synchronize_rcu_bh();
cancel_work_sync(&q->timeout_work);
/*
* The timeout work will not be invoked anymore.
*/
del_timer_sync(&q->timeout);
} void blk_set_queue_dying(struct request_queue *q)
{
spin_lock_irq(q->queue_lock);
@@ -867,6 +905,9 @@ static void blk_rq_timed_out_timer(struct timer_list *t)
{
struct request_queue *q = from_timer(q, t, timeout);
+ if (blk_queue_timeout_quiesced(q))
+ return;
kblockd_schedule_work(&q->timeout_work);
}
Thanks
Jianchao
Re: [PATCH V2 1/5] block: introduce blk_quiesce_timeout() and blk_unquiesce_timeout()
Quoted 75 lines · jianchao.wang
Hi ming
Quoted 59 lines · Ming Lei
block/blk-core.c +32
-2+static void __blk_unquiesce_timeout (struct request_queue *q) +{ +unsigned long flags; + +spin_lock_irqsave (q->queue_lock, flags); + q->timeout_off = false; +spin_unlock_irqrestore (q->queue_lock, flags); +} + +void blk_unquiesce_timeout (struct request_queue *q) +{ +__blk_unquiesce_timeout (q); +mod_timer (&q->timeout, jiffies + q->rq_timeout); +} +EXPORT_SYMBOL (blk_unquiesce_timeout); + +void blk_quiesce_timeout (struct request_queue *q) +{ +unsigned long flags; + +spin_lock_irqsave (q->queue_lock, flags); + q->timeout_off = true; +spin_unlock_irqrestore (q->queue_lock, flags); + +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 @@ -408,8 +437,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;@@ -421,6 +449,8 @@ void blk_sync_queue(struct request_queue *q) }else {cancel_delayed_work_sync (&q->delay_work); } + +__blk_unquiesce_timeout (q); }EXPORT_SYMBOL (blk_sync_queue);block/blk-mq.c +0
-0@@ -901,6 +901,15 @@ static void blk_mq_timeout_work(struct work_struct *work) };struct blk_mq_hw_ctx *hctx;block/blk-mq.c static void blk_mq_timeout_work(struct work_struct *work) · line 912
Quoted 10 lines
block/blk-mq.c +9
-0int 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 ;Looks like there is still race as following:
Quoted 6 lines
blk_quiesce_timeoutblk_mq_timeout_work-> timeout_off = q->timeout_off; //still a false -> q->timeout_off = true; -> del_timer_sync(&q->timeout); -> mod_timer->
cancel_work_sync(&q->timeout_work);
No, there isn't such race, the 'mod_timer' doesn't make a difference because 'q->timeout_off' will be visible in new work func after cancel_work_sync() returns. So even the timer is expired, work func still returns immediately.
That means del_timer_sync() isn't absolutely necessary too.
Thanks,
Ming
Re: [PATCH V2 5/5] nvme: pci: simplify timeout handling
In reply to jianchao.wangQuoted 64 lines · jianchao.wang
Hi Ming
Quoted 61 lines · Ming Lei
drivers/nvme/host/pci.c +10
-8+static enum blk_eh_timer_return nvme_timeout (struct request *req,bool reserved) {struct nvme_iod *iod =blk_mq_rq_to_pdu (req);@@ -1197,8 +1297,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_reset_ctrl (&dev->ctrl); +nvme_eh_schedule (dev);return BLK_EH_HANDLED ; }@@ -1224,8 +1323,8 @@ 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_req (req)->flags |=NVME_REQ_CANCELLED ; +nvme_eh_schedule (dev);return BLK_EH_HANDLED ;default :break ;@@ -1240,14 +1339,12 @@ 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_reset_ctrl (&dev->ctrl); -/* * Mark the request as handled, since the inline shutdown * forces all outstanding requests to complete. */ nvme_req (req)->flags |=NVME_REQ_CANCELLED ; +nvme_eh_schedule (dev);return BLK_EH_HANDLED ; }@@ -2246,8 +2343,8 @@ static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)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; }@@ -2281,11 +2378,23 @@ static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown)for (i = dev->ctrl.queue_count -1 ; i >=0 ; i--)nvme_suspend_queue (&dev->queues[i]); +/* safe to sync timeout after queues are quiesced */ +nvme_quiesce_timeout (&dev->ctrl); +blk_quiesce_timeout (dev->ctrl.admin_q); +nvme_pci_disable (dev);
Quoted 11 lines
drivers/nvme/host/pci.c static void nvme_dev_disable(struct nvme_dev *dev, bool shutdown) · line 2392
Quoted 6 lines
drivers/nvme/host/pci.c +4
-0+/* +* Both timeout and interrupt handler have been drained, and all +* in-flight requests will be canceled now. +*/ 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);We need to return
BLK_EH_RESET_TIMERinnvme_timeoutthen:
1. defer the completion. we can't unmap the io request before close the controller totally, so notBLK_EH_HANDLED.
2.nvme_cancel_requestcould complete it.blk_mq_complete_requestis invoked bynvme_cancel_request, so notBLK_EH_NOT_HANDLED.
We don't need to change return value of .timeout() any more after calling nvme_quiesce_timeout():
Thanks the single EH kthread, once nvme_quiesce_timeout() returns, all timed-out requests have been handled already. Some of them may be completed, and others may be handled as RESET_TIMER, but none of them are handled as NOT_HANDLED because nvme_timeout() won't return that value.
So the following blk_mq_tagset_busy_iter(nvme_cancel_request) can handle all in-flight requests because timeout is drained and quiesced.
Thanks,
Ming
Re: [PATCH V2 5/5] nvme: pci: simplify timeout handling
Hi Ming
Quoted 6 lines · Ming Lei
Quoted 3 lines
We need to return
BLK_EH_RESET_TIMERinnvme_timeoutthen:
1. defer the completion. we can't unmap the io request before close the controller totally, so notBLK_EH_HANDLED.
2.nvme_cancel_requestcould complete it.blk_mq_complete_requestis invoked bynvme_cancel_request, so notBLK_EH_NOT_HANDLED.We don't need to change return value of .timeout() any more after calling
nvme_quiesce_timeout():
Quoted 7 lines
Thanks the single EH kthread, once
nvme_quiesce_timeout()returns, all timed-out requests have been handled already. Some of them may be completed, and others may be handled asRESET_TIMER, but none of them are handled asNOT_HANDLEDbecausenvme_timeout()won't return that value.So the following
blk_mq_tagset_busy_iter(nvme_cancel_request)can handle all in-flight requests because timeout is drained and quiesced.
The key point here is we cannot unmap the io requests before we close the controller directly. The nvme controller may still hold the command after we complete and unmap the io request in nvme_timeout. This will cause memory corruption.
So we cannot just schedule the eh recovery kthread then return BLK_EH_HANDLED.
We need to deffer the completion of timeout requests until the controller has been closed totally in nvme_dev_disable.
Thanks
Jianchao
Re: [PATCH V2 1/5] block: introduce blk_quiesce_timeout() and blk_unquiesce_timeout()
In reply to Ming LeiHi Ming
Quoted 4 lines · Ming Lei
No, there isn't such race, the '
mod_timer' doesn't make a difference because 'q->timeout_off' will be visible in new work func aftercancel_work_sync()returns. So even the timer is expired, work func still returns immediately.
Yes, you are right.
even if timer is setup , but the timeout work will return.
Thanks
Jianchao
Re: [PATCH V2 5/5] nvme: pci: simplify timeout handling
In reply to jianchao.wangQuoted 17 lines · jianchao.wang
Hi Ming
Quoted 6 lines · Ming Lei
Quoted 3 lines
We need to return
BLK_EH_RESET_TIMERinnvme_timeoutthen:
1. defer the completion. we can't unmap the io request before close the controller totally, so notBLK_EH_HANDLED.
2.nvme_cancel_requestcould complete it.blk_mq_complete_requestis invoked bynvme_cancel_request, so notBLK_EH_NOT_HANDLED.We don't need to change return value of .timeout() any more after calling
nvme_quiesce_timeout():Quoted 7 lines
Thanks the single EH kthread, once
nvme_quiesce_timeout()returns, all timed-out requests have been handled already. Some of them may be completed, and others may be handled asRESET_TIMER, but none of them are handled asNOT_HANDLEDbecausenvme_timeout()won't return that value.So the following
blk_mq_tagset_busy_iter(nvme_cancel_request)can handle all in-flight requests because timeout is drained and quiesced.
Quoted 7 lines
The key point here is we cannot unmap the io requests before we close the controller directly. The nvme controller may still hold the command after we complete and unmap the io request in
nvme_timeout. This will cause memory corruption.So we cannot just schedule the eh recovery kthread then return
BLK_EH_HANDLED.
We need to deffer the completion of timeout requests until the controller has been closed totally innvme_dev_disable.
Good catch!
I am gonna document this point since it is easy to be ignored.
Yeah, looks it is simpler to return BLK_EH_RESET_TIMER for NVMe to handle this issue, but not too readable, it is something like:
we has stopped the timeout, but BLK_EH_RESET_TIMER still has to be returned for asking block layer to not complete this req.
So IMO it may be better to return BLK_EH_NOT_HANDLED, but it becomes a bit tricky to handle this request in EH thread, maybe a NVMe req flag is needed for completing the req in nvme_cancel_request().
Thanks
Ming