Skip to content

Commit 298ba0e

Browse files
author
Christoph Hellwig
committed
nvme: keep ctrl->namespaces ordered
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. Fixes: 540c801 ("NVMe: Implement namespace list scanning") Reported-by: Anton Eidelman <[email protected]> Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Keith Busch <[email protected]> Reviewed-by: Sagi Grimberg <[email protected]> Reviewed-by: Chaitanya Kulkarni <[email protected]> Reviewed-by: Damien Le Moal <[email protected]>
1 parent e371af0 commit 298ba0e

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

drivers/nvme/host/core.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <linux/kernel.h>
1414
#include <linux/module.h>
1515
#include <linux/backing-dev.h>
16-
#include <linux/list_sort.h>
1716
#include <linux/slab.h>
1817
#include <linux/types.h>
1918
#include <linux/pr.h>
@@ -3716,15 +3715,6 @@ static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid,
37163715
return ret;
37173716
}
37183717

3719-
static int ns_cmp(void *priv, const struct list_head *a,
3720-
const struct list_head *b)
3721-
{
3722-
struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
3723-
struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
3724-
3725-
return nsa->head->ns_id - nsb->head->ns_id;
3726-
}
3727-
37283718
struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
37293719
{
37303720
struct nvme_ns *ns, *ret = NULL;
@@ -3745,6 +3735,22 @@ struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
37453735
}
37463736
EXPORT_SYMBOL_NS_GPL(nvme_find_get_ns, NVME_TARGET_PASSTHRU);
37473737

3738+
/*
3739+
* Add the namespace to the controller list while keeping the list ordered.
3740+
*/
3741+
static void nvme_ns_add_to_ctrl_list(struct nvme_ns *ns)
3742+
{
3743+
struct nvme_ns *tmp;
3744+
3745+
list_for_each_entry_reverse(tmp, &ns->ctrl->namespaces, list) {
3746+
if (tmp->head->ns_id < ns->head->ns_id) {
3747+
list_add(&ns->list, &tmp->list);
3748+
return;
3749+
}
3750+
}
3751+
list_add(&ns->list, &ns->ctrl->namespaces);
3752+
}
3753+
37483754
static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid,
37493755
struct nvme_ns_ids *ids)
37503756
{
@@ -3795,9 +3801,8 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid,
37953801
goto out_unlink_ns;
37963802

37973803
down_write(&ctrl->namespaces_rwsem);
3798-
list_add_tail(&ns->list, &ctrl->namespaces);
3804+
nvme_ns_add_to_ctrl_list(ns);
37993805
up_write(&ctrl->namespaces_rwsem);
3800-
38013806
nvme_get_ctrl(ctrl);
38023807

38033808
if (device_add_disk(ctrl->device, ns->disk, nvme_ns_id_attr_groups))
@@ -4080,10 +4085,6 @@ static void nvme_scan_work(struct work_struct *work)
40804085
if (nvme_scan_ns_list(ctrl) != 0)
40814086
nvme_scan_ns_sequential(ctrl);
40824087
mutex_unlock(&ctrl->scan_lock);
4083-
4084-
down_write(&ctrl->namespaces_rwsem);
4085-
list_sort(NULL, &ctrl->namespaces, ns_cmp);
4086-
up_write(&ctrl->namespaces_rwsem);
40874088
}
40884089

40894090
/*

0 commit comments

Comments
 (0)