[PATCH v9 3/8] nvmem: core: take a reference on dev.of_node in nvmem_register()

From: Manikandan Muralidharan

Date: Fri Oct 16 2026 - 06:43:45 EST


nvmem_register() stores config->of_node (or config->dev->of_node)
into nvmem->dev.of_node without taking its own reference. Callers
that get the node via of_get_compatible_child() etc. and drop their
reference right after registering leave nvmem->dev.of_node backed by
no reference at all.

Take an of_node_get() when assigning nvmem->dev.of_node and release
it in nvmem_release(). Existing callers that hold their own get/put
pair around the registration call need no changes, since get/put on
a device_node is per-holder and independent of core now also holding
one.

Reviewed-by: Miquel Raynal <miquel.raynal@xxxxxxxxxxx>
Reviewed-by: Michael Walle <mwalle@xxxxxxxxxx>
Suggested-by: Michael Walle <mwalle@xxxxxxxxxx>
Signed-off-by: Manikandan Muralidharan <manikandan.m@xxxxxxxxxxxxx>
---
drivers/nvmem/core.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 0556d140170a..98192597b935 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -541,6 +541,7 @@ static void nvmem_release(struct device *dev)

ida_free(&nvmem_ida, nvmem->id);
gpiod_put(nvmem->wp_gpio);
+ of_node_put(nvmem->dev.of_node);
kfree(nvmem->ops);
kfree(nvmem);
}
@@ -950,9 +951,9 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem->keepout = config->keepout;
nvmem->nkeepout = config->nkeepout;
if (config->of_node)
- nvmem->dev.of_node = config->of_node;
+ nvmem->dev.of_node = of_node_get(config->of_node);
else
- nvmem->dev.of_node = config->dev->of_node;
+ nvmem->dev.of_node = of_node_get(config->dev->of_node);

switch (config->id) {
case NVMEM_DEVID_NONE:
--
2.43.0