nvme: use nvme_set_queue_dying() during namespace rescanning
RFC PATCH v2 1/1 nvme: use nvme_set_queue_dying() during namespace rescanning
From: Hannes Reinecke <[email protected]>
There is a race condition between namespace rescanning and controller reset; during controller reset all namespaces are quiesed vie nams_stop_ctrl(), and after reset all namespaces are unquiesced again.
When namespace scanning was active by the time controller reset was triggered the rescan code will call nvme_ns_remove(), which then will cause a kernel crash in nvme_start_ctrl() as it'll trip over uninitialized namespaces.
This patch calls nvme_set_queue_dying() during namespace rescan, which will already unquiesce the queue. Hence we can skip all namespaces with the 'DEAD' flag during unquiesce in nvme_start_ctrl() and eliminate this issue.
--- drivers/nvme/host/core.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/nvme/host/core.c +10 -4
@@ -3359,7 +3359,7 @@ static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid) ns =nvme_find_get_ns (ctrl, nsid);if (ns) {if (ns->disk &&revalidate_disk (ns->disk)) -nvme_ns_remove (ns); +nvme_set_queue_dying (ns);nvme_put_ns (ns); }else nvme_alloc_ns (ctrl, nsid);
@@ -3409,7 +3409,7 @@ static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)while (++prev < nsid) { ns =nvme_find_get_ns (ctrl, prev);if (ns) { -nvme_ns_remove (ns); +nvme_set_queue_dying (ns);nvme_put_ns (ns); } }
@@ -3871,8 +3871,11 @@ void nvme_stop_queues(struct nvme_ctrl *ctrl)struct nvme_ns *ns;down_read (&ctrl->namespaces_rwsem); -list_for_each_entry (ns, &ctrl->namespaces, list) +list_for_each_entry (ns, &ctrl->namespaces, list) { +if (test_bit (NVME_NS_DEAD , &ns->flags)) +continue ;blk_mq_quiesce_queue (ns->queue); + }up_read (&ctrl->namespaces_rwsem); }EXPORT_SYMBOL_GPL (nvme_stop_queues);
@@ -3882,8 +3885,11 @@ void nvme_start_queues(struct nvme_ctrl *ctrl)struct nvme_ns *ns;down_read (&ctrl->namespaces_rwsem); -list_for_each_entry (ns, &ctrl->namespaces, list) +list_for_each_entry (ns, &ctrl->namespaces, list) { +if (test_bit (NVME_NS_DEAD , &ns->flags)) +continue ;blk_mq_unquiesce_queue (ns->queue); + }up_read (&ctrl->namespaces_rwsem); }EXPORT_SYMBOL_GPL (nvme_start_queues);
Signature · 1 line
2.16.4
RFC PATCH v2 1/1 nvme: use nvme_set_queue_dying() during namespace rescanning
Quoted 71 lines · Hannes Reinecke
From: Hannes Reinecke <hare at suse.com>
There is a race condition between namespace rescanning and controller reset; during controller reset all namespaces are quiesed vie
nams_stop_ctrl(), and after reset all namespaces are unquiesced again.
When namespace scanning was active by the time controller reset was triggered the rescan code will callnvme_ns_remove(), which then will cause a kernel crash innvme_start_ctrl()as it'll trip over uninitialized namespaces.This patch calls
nvme_set_queue_dying()during namespace rescan, which will already unquiesce the queue. Hence we can skip all namespaces with the 'DEAD' flag during unquiesce innvme_start_ctrl()and eliminate this issue.Signed-off-by Hannes Reinecke <hare at suse.com>--- drivers/nvme/host/core.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)drivers/nvme/host/core.c +10
-4@@ -3359,7 +3359,7 @@ static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid) ns =nvme_find_get_ns (ctrl, nsid);if (ns) {if (ns->disk &&revalidate_disk (ns->disk)) -nvme_ns_remove (ns); +nvme_set_queue_dying (ns);nvme_put_ns (ns); }else nvme_alloc_ns (ctrl, nsid);@@ -3409,7 +3409,7 @@ static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)while (++prev < nsid) { ns =nvme_find_get_ns (ctrl, prev);if (ns) { -nvme_ns_remove (ns); +nvme_set_queue_dying (ns);nvme_put_ns (ns); } }@@ -3871,8 +3871,11 @@ void nvme_stop_queues(struct nvme_ctrl *ctrl)struct nvme_ns *ns;down_read (&ctrl->namespaces_rwsem); -list_for_each_entry (ns, &ctrl->namespaces, list) +list_for_each_entry (ns, &ctrl->namespaces, list) { +if (test_bit (NVME_NS_DEAD , &ns->flags)) +continue ;blk_mq_quiesce_queue (ns->queue); + }up_read (&ctrl->namespaces_rwsem); }EXPORT_SYMBOL_GPL (nvme_stop_queues);@@ -3882,8 +3885,11 @@ void nvme_start_queues(struct nvme_ctrl *ctrl)struct nvme_ns *ns;down_read (&ctrl->namespaces_rwsem); -list_for_each_entry (ns, &ctrl->namespaces, list) +list_for_each_entry (ns, &ctrl->namespaces, list) { +if (test_bit (NVME_NS_DEAD , &ns->flags)) +continue ;blk_mq_unquiesce_queue (ns->queue); + }up_read (&ctrl->namespaces_rwsem); }EXPORT_SYMBOL_GPL (nvme_start_queues);Signature · 1 line
2.16.4
Another candidate is to hold ns's refcount and switch to remove the ns from 'ctrl->namespaces' in nvme_free_ns() via the following patch[1]. Together with patch "blk-mq: free hw queue's resource in hctx's release handler" in the following link:
https://lore.kernel.org/linux-block/20190413071829.GB9108 at ming.t460p/T/#m41c04517a37cbc1b4c61357f8cb52cd3cbf31f1b
[1] fix race between nvme scan and reset
drivers/nvme/host/core.c +27 -4
@@ -402,6 +402,10 @@ static void nvme_free_ns(struct kref *kref) { struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); +down_write (&ns->ctrl->namespaces_rwsem); +list_del_init (&ns->list); +up_write (&ns->ctrl->namespaces_rwsem); +if (ns->ndev)nvme_nvm_unregister (ns);
@@ -3166,6 +3170,29 @@ static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)return nsa->head->ns_id - nsb->head->ns_id; } +void nvme_get_all_ns (struct nvme_ctrl *ctrl) +{ +struct nvme_ns *ns; + +down_read (&ctrl->namespaces_rwsem); +list_for_each_entry (ns, &ctrl->namespaces, list) +if (kref_get_unless_zero (&ns->kref)) +continue ; +up_read (&ctrl->namespaces_rwsem); +} +EXPORT_SYMBOL_GPL (nvme_get_all_ns); + +void nvme_put_all_ns (struct nvme_ctrl *ctrl) +{ +struct nvme_ns *ns; + +down_read (&ctrl->namespaces_rwsem); +list_for_each_entry (ns, &ctrl->namespaces, list) +nvme_put_ns (ns); +up_read (&ctrl->namespaces_rwsem); +} +EXPORT_SYMBOL_GPL (nvme_put_all_ns); +static struct nvme_ns *nvme_find_get_ns (struct nvme_ctrl *ctrl,unsigned nsid) {struct nvme_ns *ns, *ret =NULL ;
@@ -3329,10 +3356,6 @@ static void nvme_ns_remove(struct nvme_ns *ns)nvme_mpath_clear_current_path (ns);mutex_unlock (&ns->ctrl->subsys->lock); -down_write (&ns->ctrl->namespaces_rwsem); -list_del_init (&ns->list); -up_write (&ns->ctrl->namespaces_rwsem); -synchronize_srcu (&ns->head->srcu);nvme_mpath_check_last_path (ns);nvme_put_ns (ns);
drivers/nvme/host/nvme.h +3 -0
@@ -430,6 +430,9 @@ void nvme_stop_ctrl(struct nvme_ctrl *ctrl);void nvme_put_ctrl (struct nvme_ctrl *ctrl);int nvme_init_identify (struct nvme_ctrl *ctrl); +void nvme_get_all_ns (struct nvme_ctrl *ctrl); +void nvme_put_all_ns (struct nvme_ctrl *ctrl); +void nvme_remove_namespaces (struct nvme_ctrl *ctrl); int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
drivers/nvme/host/pci.c +3 -0
@@ -2496,6 +2496,8 @@ static void nvme_reset_work(struct work_struct *work)int result = -ENODEV ;enum nvme_ctrl_state new_state =NVME_CTRL_LIVE ; +nvme_get_all_ns (&dev->ctrl); +if (WARN_ON (dev->ctrl.state !=NVME_CTRL_RESETTING )) goto out;
@@ -2603,6 +2605,7 @@ static void nvme_reset_work(struct work_struct *work) out_unlock:mutex_unlock (&dev->shutdown_lock); out: +nvme_put_all_ns (&dev->ctrl);nvme_remove_dead_ctrl (dev, result); }
Thanks,
Ming
RFC PATCH v2 1/1 nvme: use nvme_set_queue_dying() during namespace rescanning
Quoted 98 lines · Ming Lei
Quoted 71 lines · Hannes Reinecke
From: Hannes Reinecke <hare at suse.com>
There is a race condition between namespace rescanning and controller reset; during controller reset all namespaces are quiesed vie
nams_stop_ctrl(), and after reset all namespaces are unquiesced again.
When namespace scanning was active by the time controller reset was triggered the rescan code will callnvme_ns_remove(), which then will cause a kernel crash innvme_start_ctrl()as it'll trip over uninitialized namespaces.This patch calls
nvme_set_queue_dying()during namespace rescan, which will already unquiesce the queue. Hence we can skip all namespaces with the 'DEAD' flag during unquiesce innvme_start_ctrl()and eliminate this issue.Signed-off-by Hannes Reinecke <hare at suse.com>--- drivers/nvme/host/core.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)drivers/nvme/host/core.c +10
-4@@ -3359,7 +3359,7 @@ static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid) ns =nvme_find_get_ns (ctrl, nsid);if (ns) {if (ns->disk &&revalidate_disk (ns->disk)) -nvme_ns_remove (ns); +nvme_set_queue_dying (ns);nvme_put_ns (ns); }else nvme_alloc_ns (ctrl, nsid);@@ -3409,7 +3409,7 @@ static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)while (++prev < nsid) { ns =nvme_find_get_ns (ctrl, prev);if (ns) { -nvme_ns_remove (ns); +nvme_set_queue_dying (ns);nvme_put_ns (ns); } }@@ -3871,8 +3871,11 @@ void nvme_stop_queues(struct nvme_ctrl *ctrl)struct nvme_ns *ns;down_read (&ctrl->namespaces_rwsem); -list_for_each_entry (ns, &ctrl->namespaces, list) +list_for_each_entry (ns, &ctrl->namespaces, list) { +if (test_bit (NVME_NS_DEAD , &ns->flags)) +continue ;blk_mq_quiesce_queue (ns->queue); + }up_read (&ctrl->namespaces_rwsem); }EXPORT_SYMBOL_GPL (nvme_stop_queues);@@ -3882,8 +3885,11 @@ void nvme_start_queues(struct nvme_ctrl *ctrl)struct nvme_ns *ns;down_read (&ctrl->namespaces_rwsem); -list_for_each_entry (ns, &ctrl->namespaces, list) +list_for_each_entry (ns, &ctrl->namespaces, list) { +if (test_bit (NVME_NS_DEAD , &ns->flags)) +continue ;blk_mq_unquiesce_queue (ns->queue); + }up_read (&ctrl->namespaces_rwsem); }EXPORT_SYMBOL_GPL (nvme_start_queues);Signature · 1 line
2.16.4Another candidate is to hold ns's refcount and switch to remove the ns from '
ctrl->namespaces' innvme_free_ns()via the following patch[1]. Together with patch "blk-mq: free hw queue's resource in hctx's release handler" in the following link:https://lore.kernel.org/linux-block/20190413071829.GB9108 at ming.t460p/T/#m41c04517a37cbc1b4c61357f8cb52cd3cbf31f1b
[1] fix race between nvme scan and reset
drivers/nvme/host/core.c +4
-0@@ -402,6 +402,10 @@ static void nvme_free_ns(struct kref *kref) { struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); +down_write (&ns->ctrl->namespaces_rwsem); +list_del_init (&ns->list); +up_write (&ns->ctrl->namespaces_rwsem); +if (ns->ndev)nvme_nvm_unregister (ns);
drivers/nvme/host/core.c static int ns_cmp(void *priv, struct list_head *a, struct list_head *b) · line 3179
Quoted 10 lines
drivers/nvme/host/core.c +7
-0@@ -3166,6 +3170,29 @@ static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)return nsa->head->ns_id - nsb->head->ns_id; } +void nvme_get_all_ns (struct nvme_ctrl *ctrl) +{ +struct nvme_ns *ns; + +down_read (&ctrl->namespaces_rwsem); +list_for_each_entry (ns, &ctrl->namespaces, list) +if (kref_get_unless_zero (&ns->kref))
Kref_get() is enough.
Quoted 4 lines
drivers/nvme/host/core.c +4
-0+continue ; +up_read (&ctrl->namespaces_rwsem); +} +EXPORT_SYMBOL_GPL (nvme_get_all_ns);
drivers/nvme/host/core.c static int ns_cmp(void *priv, struct list_head *a, struct list_head *b) · line 3193
Quoted 10 lines
drivers/nvme/host/core.c +10
-0+ +void nvme_put_all_ns (struct nvme_ctrl *ctrl) +{ +struct nvme_ns *ns; + +down_read (&ctrl->namespaces_rwsem); +list_for_each_entry (ns, &ctrl->namespaces, list) +nvme_put_ns (ns); +up_read (&ctrl->namespaces_rwsem); +}
The above will cause deadlock.
Follows V2:
drivers/nvme/host/core.c +40 -4
@@ -402,6 +402,10 @@ static void nvme_free_ns(struct kref *kref) { struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); +down_write (&ns->ctrl->namespaces_rwsem); +list_del_init (&ns->list); +up_write (&ns->ctrl->namespaces_rwsem); +if (ns->ndev)nvme_nvm_unregister (ns);
@@ -3166,6 +3170,42 @@ static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)return nsa->head->ns_id - nsb->head->ns_id; } +void nvme_get_all_ns (struct nvme_ctrl *ctrl) +{ +struct nvme_ns *ns; + +down_read (&ctrl->namespaces_rwsem); +list_for_each_entry (ns, &ctrl->namespaces, list) +kref_get (&ns->kref); +up_read (&ctrl->namespaces_rwsem); +} +EXPORT_SYMBOL_GPL (nvme_get_all_ns); + +static void nvme_free_ns_nop (struct kref *kref) +{ +} + +void nvme_put_all_ns (struct nvme_ctrl *ctrl) +{ +struct nvme_ns *ns; +LIST_HEAD (ns_list); + +down_write (&ctrl->namespaces_rwsem); +list_for_each_entry (ns, &ctrl->namespaces, list) { +if (kref_put (&ns->kref, nvme_free_ns_nop)) { +list_del_init (&ns->list); +list_add (&ns->list, &ns_list); + } + } +up_write (&ctrl->namespaces_rwsem); + +while (!list_empty (&ns_list)) { + ns =list_first_entry (&ns_list,typeof (*ns), list); +nvme_free_ns (&ns->kref); + } +} +EXPORT_SYMBOL_GPL (nvme_put_all_ns); +static struct nvme_ns *nvme_find_get_ns (struct nvme_ctrl *ctrl,unsigned nsid) {struct nvme_ns *ns, *ret =NULL ;
@@ -3329,10 +3369,6 @@ static void nvme_ns_remove(struct nvme_ns *ns)nvme_mpath_clear_current_path (ns);mutex_unlock (&ns->ctrl->subsys->lock); -down_write (&ns->ctrl->namespaces_rwsem); -list_del_init (&ns->list); -up_write (&ns->ctrl->namespaces_rwsem); -synchronize_srcu (&ns->head->srcu);nvme_mpath_check_last_path (ns);nvme_put_ns (ns);
drivers/nvme/host/nvme.h +3 -0
@@ -430,6 +430,9 @@ void nvme_stop_ctrl(struct nvme_ctrl *ctrl);void nvme_put_ctrl (struct nvme_ctrl *ctrl);int nvme_init_identify (struct nvme_ctrl *ctrl); +void nvme_get_all_ns (struct nvme_ctrl *ctrl); +void nvme_put_all_ns (struct nvme_ctrl *ctrl); +void nvme_remove_namespaces (struct nvme_ctrl *ctrl); int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
drivers/nvme/host/pci.c +3 -0
@@ -2496,6 +2496,8 @@ static void nvme_reset_work(struct work_struct *work)int result = -ENODEV ;enum nvme_ctrl_state new_state =NVME_CTRL_LIVE ; +nvme_get_all_ns (&dev->ctrl); +if (WARN_ON (dev->ctrl.state !=NVME_CTRL_RESETTING )) goto out;
@@ -2603,6 +2605,7 @@ static void nvme_reset_work(struct work_struct *work) out_unlock:mutex_unlock (&dev->shutdown_lock); out: +nvme_put_all_ns (&dev->ctrl);nvme_remove_dead_ctrl (dev, result); }
Thanks,
Ming
RFC PATCH v2 1/1 nvme: use nvme_set_queue_dying() during namespace rescanning
In reply to Ming LeiOn 4/13/19 10:29 AM, Ming Lei wrote:
[ .. ]
Quoted 93 lines
Another candidate is to hold ns's refcount and switch to remove the ns from '
ctrl->namespaces' innvme_free_ns()via the following patch[1]. Together with patch "blk-mq: free hw queue's resource in hctx's release handler" in the following link:https://lore.kernel.org/linux-block/20190413071829.GB9108 at ming.t460p/T/#m41c04517a37cbc1b4c61357f8cb52cd3cbf31f1b
[1] fix race between nvme scan and reset
drivers/nvme/host/core.c +27
-4@@ -402,6 +402,10 @@ static void nvme_free_ns(struct kref *kref) { struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); +down_write (&ns->ctrl->namespaces_rwsem); +list_del_init (&ns->list); +up_write (&ns->ctrl->namespaces_rwsem); +if (ns->ndev)nvme_nvm_unregister (ns);@@ -3166,6 +3170,29 @@ static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)return nsa->head->ns_id - nsb->head->ns_id; } +void nvme_get_all_ns (struct nvme_ctrl *ctrl) +{ +struct nvme_ns *ns; + +down_read (&ctrl->namespaces_rwsem); +list_for_each_entry (ns, &ctrl->namespaces, list) +if (kref_get_unless_zero (&ns->kref)) +continue ; +up_read (&ctrl->namespaces_rwsem); +} +EXPORT_SYMBOL_GPL (nvme_get_all_ns); + +void nvme_put_all_ns (struct nvme_ctrl *ctrl) +{ +struct nvme_ns *ns; + +down_read (&ctrl->namespaces_rwsem); +list_for_each_entry (ns, &ctrl->namespaces, list) +nvme_put_ns (ns); +up_read (&ctrl->namespaces_rwsem); +} +EXPORT_SYMBOL_GPL (nvme_put_all_ns); +static struct nvme_ns *nvme_find_get_ns (struct nvme_ctrl *ctrl,unsigned nsid) {struct nvme_ns *ns, *ret =NULL ;@@ -3329,10 +3356,6 @@ static void nvme_ns_remove(struct nvme_ns *ns)nvme_mpath_clear_current_path (ns);mutex_unlock (&ns->ctrl->subsys->lock); -down_write (&ns->ctrl->namespaces_rwsem); -list_del_init (&ns->list); -up_write (&ns->ctrl->namespaces_rwsem); -synchronize_srcu (&ns->head->srcu);nvme_mpath_check_last_path (ns);nvme_put_ns (ns);drivers/nvme/host/nvme.h +3
-0@@ -430,6 +430,9 @@ void nvme_stop_ctrl(struct nvme_ctrl *ctrl);void nvme_put_ctrl (struct nvme_ctrl *ctrl);int nvme_init_identify (struct nvme_ctrl *ctrl); +void nvme_get_all_ns (struct nvme_ctrl *ctrl); +void nvme_put_all_ns (struct nvme_ctrl *ctrl); +void nvme_remove_namespaces (struct nvme_ctrl *ctrl); int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,drivers/nvme/host/pci.c +2
-0@@ -2496,6 +2496,8 @@ static void nvme_reset_work(struct work_struct *work)int result = -ENODEV ;enum nvme_ctrl_state new_state =NVME_CTRL_LIVE ; +nvme_get_all_ns (&dev->ctrl); +if (WARN_ON (dev->ctrl.state !=NVME_CTRL_RESETTING )) goto out;
drivers/nvme/host/pci.c static void nvme_reset_work(struct work_struct *work) · line 2611
Quoted 7 lines
drivers/nvme/host/pci.c +1
-0@@ -2603,6 +2605,7 @@ static void nvme_reset_work(struct work_struct *work) out_unlock:mutex_unlock (&dev->shutdown_lock); out: +nvme_put_all_ns (&dev->ctrl);nvme_remove_dead_ctrl (dev, result); }
Hmm.
Might; I'll have to check.
The entire condition under which this particular error is triggered is very convoluted, and this issue isn't the only one contributing to it. Will be posting my findings once I have confirmation.
Cheers,
Hannes
Signature · 5 lines
Dr. Hannes Reinecke Teamlead Storage & Networking hare at suse.de +49 911 74053 688 SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N?rnberg GF: Felix Imend?rffer, Mary Higgins, Sri Rasiah HRB 21284 (AG N?rnberg)
RFC PATCH v2 1/1 nvme: use nvme_set_queue_dying() during namespace rescanning
Quoted 87 lines · Hannes Reinecke
On 4/13/19 10:29 AM, Ming Lei wrote:
[ .. ]Quoted 85 lines
Another candidate is to hold ns's refcount and switch to remove the ns from '
ctrl->namespaces' innvme_free_ns()via the following patch[1]. Together with patch "blk-mq: free hw queue's resource in hctx's release handler" in the following link:https://lore.kernel.org/linux-block/20190413071829.GB9108 at ming.t460p/T/#m41c04517a37cbc1b4c61357f8cb52cd3cbf31f1b
[1] fix race between nvme scan and reset
drivers/nvme/host/core.c +27
-4@@ -402,6 +402,10 @@ static void nvme_free_ns(struct kref *kref) { struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref); +down_write (&ns->ctrl->namespaces_rwsem); +list_del_init (&ns->list); +up_write (&ns->ctrl->namespaces_rwsem); +if (ns->ndev)nvme_nvm_unregister (ns);@@ -3166,6 +3170,29 @@ static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)return nsa->head->ns_id - nsb->head->ns_id; } +void nvme_get_all_ns (struct nvme_ctrl *ctrl) +{ +struct nvme_ns *ns; + +down_read (&ctrl->namespaces_rwsem); +list_for_each_entry (ns, &ctrl->namespaces, list) +if (kref_get_unless_zero (&ns->kref)) +continue ; +up_read (&ctrl->namespaces_rwsem); +} +EXPORT_SYMBOL_GPL (nvme_get_all_ns); + +void nvme_put_all_ns (struct nvme_ctrl *ctrl) +{ +struct nvme_ns *ns; + +down_read (&ctrl->namespaces_rwsem); +list_for_each_entry (ns, &ctrl->namespaces, list) +nvme_put_ns (ns); +up_read (&ctrl->namespaces_rwsem); +} +EXPORT_SYMBOL_GPL (nvme_put_all_ns); +static struct nvme_ns *nvme_find_get_ns (struct nvme_ctrl *ctrl,unsigned nsid) {struct nvme_ns *ns, *ret =NULL ;@@ -3329,10 +3356,6 @@ static void nvme_ns_remove(struct nvme_ns *ns)nvme_mpath_clear_current_path (ns);mutex_unlock (&ns->ctrl->subsys->lock); -down_write (&ns->ctrl->namespaces_rwsem); -list_del_init (&ns->list); -up_write (&ns->ctrl->namespaces_rwsem); -synchronize_srcu (&ns->head->srcu);nvme_mpath_check_last_path (ns);nvme_put_ns (ns);drivers/nvme/host/nvme.h +3
-0@@ -430,6 +430,9 @@ void nvme_stop_ctrl(struct nvme_ctrl *ctrl);void nvme_put_ctrl (struct nvme_ctrl *ctrl);int nvme_init_identify (struct nvme_ctrl *ctrl); +void nvme_get_all_ns (struct nvme_ctrl *ctrl); +void nvme_put_all_ns (struct nvme_ctrl *ctrl); +void nvme_remove_namespaces (struct nvme_ctrl *ctrl); int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,drivers/nvme/host/pci.c +2
-0@@ -2496,6 +2496,8 @@ static void nvme_reset_work(struct work_struct *work)int result = -ENODEV ;enum nvme_ctrl_state new_state =NVME_CTRL_LIVE ; +nvme_get_all_ns (&dev->ctrl); +if (WARN_ON (dev->ctrl.state !=NVME_CTRL_RESETTING )) goto out;@@ -2603,6 +2605,7 @@ static void nvme_reset_work(struct work_struct *work)
Quoted 8 lines
drivers/nvme/host/pci.c static void nvme_reset_work(struct work_struct *work) · line 2610
Quoted 6 lines
drivers/nvme/host/pci.c +1
-0out_unlock:mutex_unlock (&dev->shutdown_lock); out: +nvme_put_all_ns (&dev->ctrl);nvme_remove_dead_ctrl (dev, result); }Hmm.
Might; I'll have to check.
The patch actually isn't correct, especially in case that new ns is added during resetting.
Quoted 3 lines
The entire condition under which this particular error is triggered is very convoluted, and this issue isn't the only one contributing to it. Will be posting my findings once I have confirmation.
I have posted the queue freeing patch V6, which should cover this issue, especially by the following two:
https://lore.kernel.org/linux-block/ec6aed0d-a6dc-f829-46f4-10140c7c37df at suse.de/T/#u
https://lore.kernel.org/linux-block/bc4b8607-9fe7-aece-97f7-9d7d1c2c4e0b at suse.de/T/#u
Thanks,
Ming
Sagi Grimberg
maintainerRFC PATCH v2 1/1 nvme: use nvme_set_queue_dying() during namespace rescanning
In reply to Hannes ReineckeQuoted 12 lines
From: Hannes Reinecke <hare at suse.com>
There is a race condition between namespace rescanning and controller reset; during controller reset all namespaces are quiesed vie
nams_stop_ctrl(), and after reset all namespaces are unquiesced again.
When namespace scanning was active by the time controller reset was triggered the rescan code will callnvme_ns_remove(), which then will cause a kernel crash innvme_start_ctrl()as it'll trip over uninitialized namespaces.
Quoted 4 lines
This patch calls
nvme_set_queue_dying()during namespace rescan, which will already unquiesce the queue. Hence we can skip all namespaces with the 'DEAD' flag during unquiesce innvme_start_ctrl()and eliminate this issue.
Hannes, won't that make the namespace enumeration inconsistent?
Quoted 55 lines
Signed-off-by Hannes Reinecke <hare at suse.com>--- drivers/nvme/host/core.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)drivers/nvme/host/core.c +10
-4@@ -3359,7 +3359,7 @@ static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid) ns =nvme_find_get_ns (ctrl, nsid);if (ns) {if (ns->disk &&revalidate_disk (ns->disk)) -nvme_ns_remove (ns); +nvme_set_queue_dying (ns);nvme_put_ns (ns); }else nvme_alloc_ns (ctrl, nsid);@@ -3409,7 +3409,7 @@ static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)while (++prev < nsid) { ns =nvme_find_get_ns (ctrl, prev);if (ns) { -nvme_ns_remove (ns); +nvme_set_queue_dying (ns);nvme_put_ns (ns); } }@@ -3871,8 +3871,11 @@ void nvme_stop_queues(struct nvme_ctrl *ctrl)struct nvme_ns *ns;down_read (&ctrl->namespaces_rwsem); -list_for_each_entry (ns, &ctrl->namespaces, list) +list_for_each_entry (ns, &ctrl->namespaces, list) { +if (test_bit (NVME_NS_DEAD , &ns->flags)) +continue ;blk_mq_quiesce_queue (ns->queue); + }up_read (&ctrl->namespaces_rwsem); }EXPORT_SYMBOL_GPL (nvme_stop_queues);@@ -3882,8 +3885,11 @@ void nvme_start_queues(struct nvme_ctrl *ctrl)struct nvme_ns *ns;down_read (&ctrl->namespaces_rwsem); -list_for_each_entry (ns, &ctrl->namespaces, list) +list_for_each_entry (ns, &ctrl->namespaces, list) { +if (test_bit (NVME_NS_DEAD , &ns->flags)) +continue ;blk_mq_unquiesce_queue (ns->queue); + }up_read (&ctrl->namespaces_rwsem); }EXPORT_SYMBOL_GPL (nvme_start_queues);
RFC PATCH v2 1/1 nvme: use nvme_set_queue_dying() during namespace rescanning
Quoted 12 lines · Sagi Grimberg
Quoted 12 lines
From: Hannes Reinecke <hare at suse.com>
There is a race condition between namespace rescanning and controller reset; during controller reset all namespaces are quiesed vie
nams_stop_ctrl(), and after reset all namespaces are unquiesced again.
When namespace scanning was active by the time controller reset was triggered the rescan code will callnvme_ns_remove(), which then will cause a kernel crash innvme_start_ctrl()as it'll trip over uninitialized namespaces.
Quoted 7 lines
Quoted 4 lines
This patch calls
nvme_set_queue_dying()during namespace rescan, which will already unquiesce the queue. Hence we can skip all namespaces with the 'DEAD' flag during unquiesce innvme_start_ctrl()and eliminate this issue.Hannes, won't that make the namespace enumeration inconsistent?
How so?
The basic problem is that nvme_validate_ns() will more-or-less automatically _remove_ all namespaces if it's called concurrently during a reset (as trivially no I/O is possible).
However, after reset we'll be rescanning all namespace _anyway_, re-establishing all previously deleted namespaces.
With this patch we'll just deferring the deletion (which we would've done anyway).
However, this patch has been folded into Ming Leis patchset (as the last patch in the series), so I guess we can discard it.
Cheers,
Hannes
Signature · 5 lines
Dr. Hannes Reinecke zSeries & Storage hare at suse.com +49 911 74053 688 SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N?rnberg GF: F. Imend?rffer, J. Smithard, D. Upmanyu, G. Norton HRB 21284 (AG N?rnberg)