All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] nvme: fix the dangerous reference of namespaces list
@ 2018-02-12 12:54 ` Jianchao Wang
  0 siblings, 0 replies; 22+ messages in thread
From: Jianchao Wang @ 2018-02-12 12:54 UTC (permalink / raw)
  To: keith.busch, axboe, hch, sagi; +Cc: linux-nvme, linux-kernel

nvme_remove_namespaces and nvme_remove_invalid_namespaces reference
the ctrl->namespaces list w/o holding namespaces_mutext. It is ok
to invoke nvme_ns_remove there, but what if there is others.

To be safer, reference the ctrl->namespaces list under
namespaces_mutext.

Signed-off-by: Jianchao Wang <jianchao.w.wang@oracle.com>
---
 drivers/nvme/host/core.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 2fd8688..d05855b 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -3094,11 +3094,18 @@ static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
 					unsigned nsid)
 {
 	struct nvme_ns *ns, *next;
+	LIST_HEAD(rm_list);
 
+	mutex_lock(&ctrl->namespaces_mutex);
 	list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
 		if (ns->head->ns_id > nsid)
-			nvme_ns_remove(ns);
+			list_move_tail(&ns->list, &rm_list);
 	}
+	mutex_unlock(&ctrl->namespaces_mutex);
+
+	list_for_each_entry_safe(ns, next, &rm_list, list)
+		nvme_ns_remove(ns);
+
 }
 
 static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
@@ -3198,6 +3205,7 @@ EXPORT_SYMBOL_GPL(nvme_queue_scan);
 void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
 {
 	struct nvme_ns *ns, *next;
+	LIST_HEAD(ns_list);
 
 	/*
 	 * The dead states indicates the controller was not gracefully
@@ -3208,7 +3216,11 @@ void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
 	if (ctrl->state == NVME_CTRL_DEAD)
 		nvme_kill_queues(ctrl);
 
-	list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
+	mutex_lock(&ctrl->namespaces_mutex);
+	list_splice_init(&ctrl->namespaces, &ns_list);
+	mutex_unlock(&ctrl->namespaces_mutex);
+
+	list_for_each_entry_safe(ns, next, &ns_list, list)
 		nvme_ns_remove(ns);
 }
 EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2018-02-12 20:25 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-12 12:54 [PATCH 1/3] nvme: fix the dangerous reference of namespaces list Jianchao Wang
2018-02-12 12:54 ` Jianchao Wang
2018-02-12 12:54 ` [PATCH 2/3] nvme: fix the deadlock in nvme_update_formats Jianchao Wang
2018-02-12 12:54   ` Jianchao Wang
2018-02-12 18:46   ` Sagi Grimberg
2018-02-12 18:46     ` Sagi Grimberg
2018-02-12 19:20     ` Keith Busch
2018-02-12 19:20       ` Keith Busch
2018-02-12 20:25       ` Keith Busch
2018-02-12 20:25         ` Keith Busch
2018-02-12 12:54 ` [PATCH 3/3] nvme: change namespaces_mutext to namespaces_rwsem Jianchao Wang
2018-02-12 12:54   ` Jianchao Wang
2018-02-12 18:47   ` Sagi Grimberg
2018-02-12 18:47     ` Sagi Grimberg
2018-02-12 19:30     ` Keith Busch
2018-02-12 19:30       ` Keith Busch
2018-02-12 18:46 ` [PATCH 1/3] nvme: fix the dangerous reference of namespaces list Sagi Grimberg
2018-02-12 18:46   ` Sagi Grimberg
2018-02-12 19:29 ` Keith Busch
2018-02-12 19:29   ` Keith Busch
2018-02-12 19:37   ` Sagi Grimberg
2018-02-12 19:37     ` Sagi Grimberg

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.