linux-nvme.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] nvme: keep ctrl->namespaces ordered
@ 2021-09-15  8:24 Christoph Hellwig
  2021-09-15  9:31 ` Sagi Grimberg
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Christoph Hellwig @ 2021-09-15  8:24 UTC (permalink / raw)
  To: kbusch, sagi; +Cc: anton.eidelman, linux-nvme

Various places in the nvme code that rely on ctrl->namespace to be
ordered.  Ensure that the namespae is inserted into the list at the
right position from the start instead of sorting it after the fact.

Also use the chance to check for duplicates.  These should not happen,
but with consumer hardware we can't ever be careful enough.

Fixes: 540c801c65eb ("NVMe: Implement namespace list scanning")
Reported-by: Anton Eidelman <anton.eidelman@gmail.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
---

Changes since v1:
 - check for duplicate NSIDs

 drivers/nvme/host/core.c | 47 +++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 97f8211cf92c1..b3b0b17cc2317 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -13,7 +13,6 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/backing-dev.h>
-#include <linux/list_sort.h>
 #include <linux/slab.h>
 #include <linux/types.h>
 #include <linux/pr.h>
@@ -3716,15 +3715,6 @@ static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid,
 	return ret;
 }
 
-static int ns_cmp(void *priv, const struct list_head *a,
-		const struct list_head *b)
-{
-	struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
-	struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
-
-	return nsa->head->ns_id - nsb->head->ns_id;
-}
-
 struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
 {
 	struct nvme_ns *ns, *ret = NULL;
@@ -3745,6 +3735,33 @@ struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
 }
 EXPORT_SYMBOL_NS_GPL(nvme_find_get_ns, NVME_TARGET_PASSTHRU);
 
+/*
+ * Add the namespace to the controller list while keeping the list ordered.
+ */
+static bool nvme_ns_add_to_ctrl_list(struct nvme_ns *ns)
+{
+	struct nvme_ns *tmp;
+
+	down_write(&ns->ctrl->namespaces_rwsem);
+	list_for_each_entry_reverse(tmp, &ns->ctrl->namespaces, list) {
+		if (tmp->head->ns_id == ns->head->ns_id)
+			goto out_duplicate;
+		if (tmp->head->ns_id < ns->head->ns_id) {
+			list_add(&tmp->list, &ns->ctrl->namespaces);
+			goto out_unlock;
+		}
+	}
+
+	list_add_tail(&ns->list, &ns->ctrl->namespaces);
+out_unlock:
+	up_write(&ns->ctrl->namespaces_rwsem);
+	return true;
+out_duplicate:
+	up_write(&ns->ctrl->namespaces_rwsem);
+	dev_warn(ns->ctrl->device, "duplicate nsid %u\n", ns->head->ns_id);
+	return false;
+}
+
 static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid,
 		struct nvme_ns_ids *ids)
 {
@@ -3794,10 +3811,8 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid,
 	if (nvme_update_ns_info(ns, id))
 		goto out_unlink_ns;
 
-	down_write(&ctrl->namespaces_rwsem);
-	list_add_tail(&ns->list, &ctrl->namespaces);
-	up_write(&ctrl->namespaces_rwsem);
-
+	if (!nvme_ns_add_to_ctrl_list(ns))
+		goto out_unlink_ns;
 	nvme_get_ctrl(ctrl);
 
 	if (device_add_disk(ctrl->device, ns->disk, nvme_ns_id_attr_groups))
@@ -4082,10 +4097,6 @@ static void nvme_scan_work(struct work_struct *work)
 	if (nvme_scan_ns_list(ctrl) != 0)
 		nvme_scan_ns_sequential(ctrl);
 	mutex_unlock(&ctrl->scan_lock);
-
-	down_write(&ctrl->namespaces_rwsem);
-	list_sort(NULL, &ctrl->namespaces, ns_cmp);
-	up_write(&ctrl->namespaces_rwsem);
 }
 
 /*
-- 
2.30.2


_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

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

* Re: [PATCH v2] nvme: keep ctrl->namespaces ordered
  2021-09-15  8:24 [PATCH v2] nvme: keep ctrl->namespaces ordered Christoph Hellwig
@ 2021-09-15  9:31 ` Sagi Grimberg
  2021-09-15  9:34 ` Sagi Grimberg
  2021-09-22 15:49 ` Max Gurtovoy
  2 siblings, 0 replies; 4+ messages in thread
From: Sagi Grimberg @ 2021-09-15  9:31 UTC (permalink / raw)
  To: Christoph Hellwig, kbusch; +Cc: anton.eidelman, linux-nvme

Reviewed-by: Sagi Grimberg <sagi@grimberg.me>

_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

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

* Re: [PATCH v2] nvme: keep ctrl->namespaces ordered
  2021-09-15  8:24 [PATCH v2] nvme: keep ctrl->namespaces ordered Christoph Hellwig
  2021-09-15  9:31 ` Sagi Grimberg
@ 2021-09-15  9:34 ` Sagi Grimberg
  2021-09-22 15:49 ` Max Gurtovoy
  2 siblings, 0 replies; 4+ messages in thread
From: Sagi Grimberg @ 2021-09-15  9:34 UTC (permalink / raw)
  To: Christoph Hellwig, kbusch; +Cc: anton.eidelman, linux-nvme


> Various places in the nvme code that rely on ctrl->namespace to be
> ordered.

Would be nice to mention the reported scenario for the patch, for
context to what "Various places" mean.

_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

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

* Re: [PATCH v2] nvme: keep ctrl->namespaces ordered
  2021-09-15  8:24 [PATCH v2] nvme: keep ctrl->namespaces ordered Christoph Hellwig
  2021-09-15  9:31 ` Sagi Grimberg
  2021-09-15  9:34 ` Sagi Grimberg
@ 2021-09-22 15:49 ` Max Gurtovoy
  2 siblings, 0 replies; 4+ messages in thread
From: Max Gurtovoy @ 2021-09-22 15:49 UTC (permalink / raw)
  To: Christoph Hellwig, kbusch, sagi; +Cc: anton.eidelman, linux-nvme


On 9/15/2021 11:24 AM, Christoph Hellwig wrote:
> static bool nvme_ns_add_to_ctrl_list(struct nvme_ns *ns)

Maybe consider returning int here instead of a bool.

Otherwise,

looks good,

Reviewed-by: Max Gurtovoy <mgurtovoy@nvidia.com>



_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

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

end of thread, other threads:[~2021-09-22 15:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-15  8:24 [PATCH v2] nvme: keep ctrl->namespaces ordered Christoph Hellwig
2021-09-15  9:31 ` Sagi Grimberg
2021-09-15  9:34 ` Sagi Grimberg
2021-09-22 15:49 ` Max Gurtovoy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).