On Wed, Sep 04, 2019 at 10:07:12AM -0600, Logan Gunthorpe wrote:
> Yes, I agree, we can't solve the mismatch problem in the general case:
> with sequences of hot plug events there will always be a case that
> mismatches. I just think we can do better in the simple common default case.

This may be something where udev can help us. I might be able to find
some time to look at that, but not today.
 
> > Can we just ensure there is never a matching controller then? This
> > patch will accomplish that and simpler than wrapping the instance in a
> > refcount'ed object:
> > 
> > http://lists.infradead.org/pipermail/linux-nvme/2019-May/024142.html
> 
> I don't really like that idea. It reduces the confusion caused by
> mismatching numbers, but causes the controller to never match the
> namespace, which is also confusing but in a different way.
> 
> I like the nvme_instance idea. It's not going to be perfect but it has
> some nice properties: the subsystem will try to match the controller's
> instance whenever possible, but in cases where it doesn't, the instance
> number of the subsystem will never be the same as an existing controller.
> 
> I'll see if I can work up a quick patch set and see what people think.

How about this: we have the subsys copy the controller's instance,
and the nvme_free_ctrl() doesn't release it if its subsys matches?

---
diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 14c0bfb55615..8a8279ece5ee 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -81,7 +81,6 @@ EXPORT_SYMBOL_GPL(nvme_reset_wq);
 struct workqueue_struct *nvme_delete_wq;
 EXPORT_SYMBOL_GPL(nvme_delete_wq);
 
-static DEFINE_IDA(nvme_subsystems_ida);
 static LIST_HEAD(nvme_subsystems);
 static DEFINE_MUTEX(nvme_subsystems_lock);
 
@@ -2344,7 +2343,8 @@ static void nvme_release_subsystem(struct device *dev)
        struct nvme_subsystem *subsys =
                container_of(dev, struct nvme_subsystem, dev);
 
-       ida_simple_remove(&nvme_subsystems_ida, subsys->instance);
+       if (subsys->instance >= 0)
+               ida_simple_remove(&nvme_instance_ida, subsys->instance);
        kfree(subsys);
 }
 
@@ -2473,12 +2473,8 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, 
struct nvme_id_ctrl *id)
        subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
        if (!subsys)
                return -ENOMEM;
-       ret = ida_simple_get(&nvme_subsystems_ida, 0, 0, GFP_KERNEL);
-       if (ret < 0) {
-               kfree(subsys);
-               return ret;
-       }
-       subsys->instance = ret;
+
+       subsys->instance = -1;
        mutex_init(&subsys->lock);
        kref_init(&subsys->ref);
        INIT_LIST_HEAD(&subsys->ctrls);
@@ -2497,7 +2493,7 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, 
struct nvme_id_ctrl *id)
        subsys->dev.class = nvme_subsys_class;
        subsys->dev.release = nvme_release_subsystem;
        subsys->dev.groups = nvme_subsys_attrs_groups;
-       dev_set_name(&subsys->dev, "nvme-subsys%d", subsys->instance);
+       dev_set_name(&subsys->dev, "nvme-subsys%d", ctrl->instance);
        device_initialize(&subsys->dev);
 
        mutex_lock(&nvme_subsystems_lock);
@@ -2528,6 +2524,8 @@ static int nvme_init_subsystem(struct nvme_ctrl *ctrl, 
struct nvme_id_ctrl *id)
                goto out_put_subsystem;
        }
 
+       if (!found)
+               subsys->instance = ctrl->instance;
        ctrl->subsys = subsys;
        list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
        mutex_unlock(&nvme_subsystems_lock);
@@ -3803,7 +3801,9 @@ static void nvme_free_ctrl(struct device *dev)
                container_of(dev, struct nvme_ctrl, ctrl_device);
        struct nvme_subsystem *subsys = ctrl->subsys;
 
-       ida_simple_remove(&nvme_instance_ida, ctrl->instance);
+       if (subsys && ctrl->instance != subsys->instance)
+               ida_simple_remove(&nvme_instance_ida, ctrl->instance);
+
        kfree(ctrl->effects);
        nvme_mpath_uninit(ctrl);
        __free_page(ctrl->discard_page);
@@ -4085,7 +4085,6 @@ static int __init nvme_core_init(void)
 
 static void __exit nvme_core_exit(void)
 {
-       ida_destroy(&nvme_subsystems_ida);
        class_destroy(nvme_subsys_class);
        class_destroy(nvme_class);
        unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
--

Reply via email to