[PATCH 05/11] nvmem: reference cell entries independently of their provider list
From: James Hilliard
Date: Tue Nov 17 2026 - 14:38:54 EST
Cell handles currently borrow entries from the provider list. Give each
list entry a reference and acquire another during lookup, before releasing
nvmem_mutex. Return that reference when the consumer releases its handle
or handle allocation fails.
This separates cell metadata lifetime from list membership, so a cell can
be unpublished while existing consumers retain its description.
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/nvmem/core.c | 35 +++++++++++++++++++++++++++--------
1 file changed, 27 insertions(+), 8 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 65d89498edeb..70b6126886d9 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -27,6 +27,7 @@
#define FLAG_COMPAT BIT(0)
struct nvmem_cell_entry {
+ struct kref refcnt;
const char *name;
int offset;
size_t raw_len;
@@ -54,6 +55,8 @@ static LIST_HEAD(nvmem_lookup_list);
static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
+static void nvmem_cell_entry_put(struct nvmem_cell_entry *cell);
+
static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
@@ -562,22 +565,30 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
#endif /* CONFIG_NVMEM_SYSFS */
-static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
+static void nvmem_cell_entry_release(struct kref *ref)
{
- blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
- scoped_guard(mutex, &nvmem_mutex)
- list_del(&cell->node);
+ struct nvmem_cell_entry *cell = container_of(ref, struct nvmem_cell_entry, refcnt);
+
of_node_put(cell->np);
kfree_const(cell->name);
kfree(cell);
}
+static void nvmem_cell_entry_put(struct nvmem_cell_entry *cell)
+{
+ kref_put(&cell->refcnt, nvmem_cell_entry_release);
+}
+
static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
{
struct nvmem_cell_entry *cell, *p;
- list_for_each_entry_safe(cell, p, &nvmem->cells, node)
- nvmem_cell_entry_drop(cell);
+ list_for_each_entry_safe(cell, p, &nvmem->cells, node) {
+ blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
+ scoped_guard(mutex, &nvmem_mutex)
+ list_del(&cell->node);
+ nvmem_cell_entry_put(cell);
+ }
}
static void nvmem_release(struct device *dev)
@@ -695,6 +706,7 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem,
return rval;
}
+ kref_init(&cell->refcnt);
nvmem_cell_entry_add(cell);
return 0;
@@ -761,6 +773,7 @@ nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
list_for_each_entry(iter, &nvmem->cells, node) {
if (strcmp(cell_id, iter->name) == 0) {
cell = iter;
+ kref_get(&cell->refcnt);
break;
}
}
@@ -1408,8 +1421,10 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
cell = ERR_PTR(-ENOENT);
} else {
cell = nvmem_create_cell(cell_entry, con_id, 0);
- if (IS_ERR(cell))
+ if (IS_ERR(cell)) {
+ nvmem_cell_entry_put(cell_entry);
nvmem_device_put(nvmem);
+ }
}
break;
}
@@ -1433,8 +1448,10 @@ nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np
guard(mutex)(&nvmem_mutex);
list_for_each_entry(cell, &nvmem->cells, node) {
- if (np == cell->np)
+ if (np == cell->np) {
+ kref_get(&cell->refcnt);
return cell;
+ }
}
return NULL;
@@ -1533,6 +1550,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
cell = nvmem_create_cell(cell_entry, id, cell_index);
if (IS_ERR(cell)) {
nvmem_layout_module_put(nvmem);
+ nvmem_cell_entry_put(cell_entry);
nvmem_device_put(nvmem);
}
@@ -1646,6 +1664,7 @@ void nvmem_cell_put(struct nvmem_cell *cell)
if (cell->id)
kfree_const(cell->id);
+ nvmem_cell_entry_put(cell->entry);
kfree(cell);
nvmem_layout_module_put(nvmem);
nvmem_device_put(nvmem);
--
2.53.0