greybus: module: free resources properly on failures
authorViresh Kumar <viresh.kumar@linaro.org>
Thu, 13 Nov 2014 12:44:37 +0000 (18:14 +0530)
committerGreg Kroah-Hartman <greg@kroah.com>
Fri, 14 Nov 2014 21:17:55 +0000 (13:17 -0800)
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Alex Elder <elder@linaro.org>
Signed-off-by: Greg Kroah-Hartman <greg@kroah.com>
drivers/staging/greybus/module.c

index f5d7dc8..8cbe65b 100644 (file)
@@ -85,10 +85,6 @@ struct gb_module *gb_module_create(struct greybus_host_device *hd, u8 module_id)
        gmod->module_id = module_id;
        INIT_LIST_HEAD(&gmod->interfaces);
 
-       spin_lock_irq(&gb_modules_lock);
-       list_add_tail(&gmod->links, &hd->modules);
-       spin_unlock_irq(&gb_modules_lock);
-
        gmod->dev.parent = hd->parent;
        gmod->dev.bus = &greybus_bus_type;
        gmod->dev.type = &greybus_module_type;
@@ -102,9 +98,14 @@ struct gb_module *gb_module_create(struct greybus_host_device *hd, u8 module_id)
                pr_err("failed to add module device for id 0x%02hhx\n",
                        module_id);
                put_device(&gmod->dev);
+               kfree(gmod);
                return NULL;
        }
 
+       spin_lock_irq(&gb_modules_lock);
+       list_add_tail(&gmod->links, &hd->modules);
+       spin_unlock_irq(&gb_modules_lock);
+
        return gmod;
 }