Skip to content

Commit 8f9c4b2

Browse files
Russell King (Oracle)gregkh
authored andcommitted
nvmem: core: fix cleanup after dev_set_name()
[ Upstream commit 560181d3ace61825f4ca9dd3481d6c0ee6709fa8 ] 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: 5544e90c8126 ("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> Stable-dep-of: ab3428cfd9aa ("nvmem: core: fix registration vs use race") Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 14eea64 commit 8f9c4b2

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
@@ -768,14 +768,18 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
768768

769769
nvmem->id = rval;
770770

771+
nvmem->dev.type = &nvmem_provider_type;
772+
nvmem->dev.bus = &nvmem_bus_type;
773+
nvmem->dev.parent = config->dev;
774+
775+
device_initialize(&nvmem->dev);
776+
771777
if (!config->ignore_wp)
772778
nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
773779
GPIOD_OUT_HIGH);
774780
if (IS_ERR(nvmem->wp_gpio)) {
775-
ida_free(&nvmem_ida, nvmem->id);
776781
rval = PTR_ERR(nvmem->wp_gpio);
777-
kfree(nvmem);
778-
return ERR_PTR(rval);
782+
goto err_put_device;
779783
}
780784

781785
kref_init(&nvmem->refcnt);
@@ -787,9 +791,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
787791
nvmem->stride = config->stride ?: 1;
788792
nvmem->word_size = config->word_size ?: 1;
789793
nvmem->size = config->size;
790-
nvmem->dev.type = &nvmem_provider_type;
791-
nvmem->dev.bus = &nvmem_bus_type;
792-
nvmem->dev.parent = config->dev;
793794
nvmem->root_only = config->root_only;
794795
nvmem->priv = config->priv;
795796
nvmem->type = config->type;
@@ -816,11 +817,8 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
816817
break;
817818
}
818819

819-
if (rval) {
820-
ida_free(&nvmem_ida, nvmem->id);
821-
kfree(nvmem);
822-
return ERR_PTR(rval);
823-
}
820+
if (rval)
821+
goto err_put_device;
824822

825823
nvmem->read_only = device_property_present(config->dev, "read-only") ||
826824
config->read_only || !nvmem->reg_write;
@@ -831,7 +829,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
831829

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

834-
rval = device_register(&nvmem->dev);
832+
rval = device_add(&nvmem->dev);
835833
if (rval)
836834
goto err_put_device;
837835

0 commit comments

Comments
 (0)