]> git.baikalelectronics.ru Git - kernel.git/commitdiff
nvmem: core: fix cleanup after dev_set_name()
authorRussell King (Oracle) <rmk+kernel@armlinux.org.uk>
Fri, 27 Jan 2023 10:40:10 +0000 (10:40 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 9 Feb 2023 10:28:24 +0000 (11:28 +0100)
commit 560181d3ace61825f4ca9dd3481d6c0ee6709fa8 upstream.

If dev_set_name() fails, we leak nvmem->wp_gpio as the cleanup does not
put this. While a minimal fix for this would be to add the gpiod_put()
call, we can do better if we split device_register(), and use the
tested nvmem_release() cleanup code by initialising the device early,
and putting the device.

This results in a slightly larger fix, but results in clear code.

Note: this patch depends on "nvmem: core: initialise nvmem->id early"
and "nvmem: core: remove nvmem_config wp_gpio".

Fixes: b42fc845b6f5 ("nvmem: core: add error handling for dev_set_name")
Cc: stable@vger.kernel.org
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <error27@gmail.com>
Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
[Srini: Fixed subject line and error code handing with wp_gpio while applying.]
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
Link: https://lore.kernel.org/r/20230127104015.23839-6-srinivas.kandagatla@linaro.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/nvmem/core.c

index 608f3ad2e2e43cd91d64c22a7def725130f69f7f..ac77a019aed7e0908ac042ab2173e29793c6eb94 100644 (file)
@@ -772,14 +772,18 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 
        nvmem->id = rval;
 
+       nvmem->dev.type = &nvmem_provider_type;
+       nvmem->dev.bus = &nvmem_bus_type;
+       nvmem->dev.parent = config->dev;
+
+       device_initialize(&nvmem->dev);
+
        if (!config->ignore_wp)
                nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
                                                    GPIOD_OUT_HIGH);
        if (IS_ERR(nvmem->wp_gpio)) {
-               ida_free(&nvmem_ida, nvmem->id);
                rval = PTR_ERR(nvmem->wp_gpio);
-               kfree(nvmem);
-               return ERR_PTR(rval);
+               goto err_put_device;
        }
 
        kref_init(&nvmem->refcnt);
@@ -791,9 +795,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
        nvmem->stride = config->stride ?: 1;
        nvmem->word_size = config->word_size ?: 1;
        nvmem->size = config->size;
-       nvmem->dev.type = &nvmem_provider_type;
-       nvmem->dev.bus = &nvmem_bus_type;
-       nvmem->dev.parent = config->dev;
        nvmem->root_only = config->root_only;
        nvmem->priv = config->priv;
        nvmem->type = config->type;
@@ -821,11 +822,8 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
                break;
        }
 
-       if (rval) {
-               ida_free(&nvmem_ida, nvmem->id);
-               kfree(nvmem);
-               return ERR_PTR(rval);
-       }
+       if (rval)
+               goto err_put_device;
 
        nvmem->read_only = device_property_present(config->dev, "read-only") ||
                           config->read_only || !nvmem->reg_write;
@@ -836,7 +834,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 
        dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
 
-       rval = device_register(&nvmem->dev);
+       rval = device_add(&nvmem->dev);
        if (rval)
                goto err_put_device;