Skip to content

Commit 560181d

Browse files
Russell King (Oracle)gregkh
authored andcommitted
nvmem: core: fix cleanup after dev_set_name()
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: 5544e90 ("nvmem: core: add error handling for dev_set_name") Cc: [email protected] Reported-by: kernel test robot <[email protected]> Reported-by: Dan Carpenter <[email protected]> Signed-off-by: Russell King (Oracle) <[email protected]> [Srini: Fixed subject line and error code handing with wp_gpio while applying.] Signed-off-by: Srinivas Kandagatla <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 569653f commit 560181d

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

drivers/nvmem/core.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -772,14 +772,18 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
772772

773773
nvmem->id = rval;
774774

775+
nvmem->dev.type = &nvmem_provider_type;
776+
nvmem->dev.bus = &nvmem_bus_type;
777+
nvmem->dev.parent = config->dev;
778+
779+
device_initialize(&nvmem->dev);
780+
775781
if (!config->ignore_wp)
776782
nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
777783
GPIOD_OUT_HIGH);
778784
if (IS_ERR(nvmem->wp_gpio)) {
779-
ida_free(&nvmem_ida, nvmem->id);
780785
rval = PTR_ERR(nvmem->wp_gpio);
781-
kfree(nvmem);
782-
return ERR_PTR(rval);
786+
goto err_put_device;
783787
}
784788

785789
kref_init(&nvmem->refcnt);
@@ -791,9 +795,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
791795
nvmem->stride = config->stride ?: 1;
792796
nvmem->word_size = config->word_size ?: 1;
793797
nvmem->size = config->size;
794-
nvmem->dev.type = &nvmem_provider_type;
795-
nvmem->dev.bus = &nvmem_bus_type;
796-
nvmem->dev.parent = config->dev;
797798
nvmem->root_only = config->root_only;
798799
nvmem->priv = config->priv;
799800
nvmem->type = config->type;
@@ -821,11 +822,8 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
821822
break;
822823
}
823824

824-
if (rval) {
825-
ida_free(&nvmem_ida, nvmem->id);
826-
kfree(nvmem);
827-
return ERR_PTR(rval);
828-
}
825+
if (rval)
826+
goto err_put_device;
829827

830828
nvmem->read_only = device_property_present(config->dev, "read-only") ||
831829
config->read_only || !nvmem->reg_write;
@@ -836,7 +834,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
836834

837835
dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
838836

839-
rval = device_register(&nvmem->dev);
837+
rval = device_add(&nvmem->dev);
840838
if (rval)
841839
goto err_put_device;
842840

0 commit comments

Comments
 (0)