Skip to content

Commit 215df43

Browse files
Christoph Hellwiggregkh
authored andcommitted
nvme: keep ctrl->namespaces ordered
[ Upstream commit 298ba0e ] 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 <anton.eidelman@gmail.com> Signed-off-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Keith Busch <kbusch@kernel.org> Reviewed-by: Sagi Grimberg <sagi@grimberg.me> Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com> Reviewed-by: Damien Le Moal <damien.lemoal@wdc.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 55e6f8b commit 215df43

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

‎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>
@@ -3801,15 +3800,6 @@ static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid,
38013800
return ret;
38023801
}
38033802

3804-
static int ns_cmp(void *priv, const struct list_head *a,
3805-
const struct list_head *b)
3806-
{
3807-
struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
3808-
struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
3809-
3810-
return nsa->head->ns_id - nsb->head->ns_id;
3811-
}
3812-
38133803
struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
38143804
{
38153805
struct nvme_ns *ns, *ret = NULL;
@@ -3830,6 +3820,22 @@ struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
38303820
}
38313821
EXPORT_SYMBOL_NS_GPL(nvme_find_get_ns, NVME_TARGET_PASSTHRU);
38323822

3823+
/*
3824+
* Add the namespace to the controller list while keeping the list ordered.
3825+
*/
3826+
static void nvme_ns_add_to_ctrl_list(struct nvme_ns *ns)
3827+
{
3828+
struct nvme_ns *tmp;
3829+
3830+
list_for_each_entry_reverse(tmp, &ns->ctrl->namespaces, list) {
3831+
if (tmp->head->ns_id < ns->head->ns_id) {
3832+
list_add(&ns->list, &tmp->list);
3833+
return;
3834+
}
3835+
}
3836+
list_add(&ns->list, &ns->ctrl->namespaces);
3837+
}
3838+
38333839
static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid,
38343840
struct nvme_ns_ids *ids)
38353841
{
@@ -3889,9 +3895,8 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid,
38893895
}
38903896

38913897
down_write(&ctrl->namespaces_rwsem);
3892-
list_add_tail(&ns->list, &ctrl->namespaces);
3898+
nvme_ns_add_to_ctrl_list(ns);
38933899
up_write(&ctrl->namespaces_rwsem);
3894-
38953900
nvme_get_ctrl(ctrl);
38963901

38973902
device_add_disk(ctrl->device, ns->disk, nvme_ns_id_attr_groups);
@@ -4160,10 +4165,6 @@ static void nvme_scan_work(struct work_struct *work)
41604165
if (nvme_scan_ns_list(ctrl) != 0)
41614166
nvme_scan_ns_sequential(ctrl);
41624167
mutex_unlock(&ctrl->scan_lock);
4163-
4164-
down_write(&ctrl->namespaces_rwsem);
4165-
list_sort(NULL, &ctrl->namespaces, ns_cmp);
4166-
up_write(&ctrl->namespaces_rwsem);
41674168
}
41684169

41694170
/*

0 commit comments

Comments
 (0)