This patch adds a counter, which is responsible for tracking inflight
target creations.

When creation process is still in progress, target is not yet on
targets list. This causes a chance for removing whole lightnvm
subsystem by calling nvm_unregister() in the meantime and finally by
causing kernel panic inside target init function.

With this patch we are able to track such a scenarios and wait with
completing nvm_unregister() and freeing memory until target creation
will be completed.

Signed-off-by: Igor Konopko <[email protected]>
---
 drivers/lightnvm/core.c  | 19 ++++++++++++++++++-
 include/linux/lightnvm.h |  2 ++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/drivers/lightnvm/core.c b/drivers/lightnvm/core.c
index 838c3d8..490ec6e 100644
--- a/drivers/lightnvm/core.c
+++ b/drivers/lightnvm/core.c
@@ -1083,6 +1083,7 @@ static int nvm_core_init(struct nvm_dev *dev)
        INIT_LIST_HEAD(&dev->targets);
        mutex_init(&dev->mlock);
        spin_lock_init(&dev->lock);
+       dev->create_inflight = 0;
 
        ret = nvm_register_map(dev);
        if (ret)
@@ -1180,6 +1181,11 @@ void nvm_unregister(struct nvm_dev *dev)
        struct nvm_target *t, *tmp;
 
        mutex_lock(&dev->mlock);
+       while (dev->create_inflight > 0) {
+               mutex_unlock(&dev->mlock);
+               io_schedule();
+               mutex_lock(&dev->mlock);
+       }
        list_for_each_entry_safe(t, tmp, &dev->targets, list) {
                if (t->dev->parent != dev)
                        continue;
@@ -1213,6 +1219,7 @@ static void nvm_normalize_path(char *path)
 static int __nvm_configure_create(struct nvm_ioctl_create *create)
 {
        struct nvm_dev *dev;
+       int ret;
 
        down_write(&nvm_lock);
        dev = nvm_find_nvm_dev(create->dev);
@@ -1223,7 +1230,17 @@ static int __nvm_configure_create(struct 
nvm_ioctl_create *create)
                return -EINVAL;
        }
 
-       return nvm_create_tgt(dev, create);
+       mutex_lock(&dev->mlock);
+       dev->create_inflight++;
+       mutex_unlock(&dev->mlock);
+
+       ret = nvm_create_tgt(dev, create);
+
+       mutex_lock(&dev->mlock);
+       dev->create_inflight--;
+       mutex_unlock(&dev->mlock);
+
+       return ret;
 }
 
 static long nvm_ioctl_info(struct file *file, void __user *arg)
diff --git a/include/linux/lightnvm.h b/include/linux/lightnvm.h
index d3b0270..e462d1d 100644
--- a/include/linux/lightnvm.h
+++ b/include/linux/lightnvm.h
@@ -428,6 +428,8 @@ struct nvm_dev {
        char name[DISK_NAME_LEN];
        void *private_data;
 
+       int create_inflight;
+
        void *rmap;
 
        struct mutex mlock;
-- 
2.9.5

Reply via email to