ffs_closed can race with configfs_rmdir which will call config_item_release, so
add an extra check to avoid calling the unregister_gadget_item with an null
gadget item.
Signed-off-by: Rui Miguel Silva <rui.silva@linaro.org>
Signed-off-by: Felipe Balbi <balbi@ti.com>
 static void ffs_closed(struct ffs_data *ffs)
 {
        struct ffs_dev *ffs_obj;
+       struct f_fs_opts *opts;
 
        ENTER();
        ffs_dev_lock();
        if (ffs_obj->ffs_closed_callback)
                ffs_obj->ffs_closed_callback(ffs);
 
-       if (!ffs_obj->opts || ffs_obj->opts->no_configfs
-           || !ffs_obj->opts->func_inst.group.cg_item.ci_parent)
+       if (ffs_obj->opts)
+               opts = ffs_obj->opts;
+       else
+               goto done;
+
+       if (opts->no_configfs || !opts->func_inst.group.cg_item.ci_parent
+           || !atomic_read(&opts->func_inst.group.cg_item.ci_kref.refcount))
                goto done;
 
        unregister_gadget_item(ffs_obj->opts->