[PATCH v3 05/11] nvmem: reference cell entries independently of their provider list
From: James Hilliard
Date: Fri Nov 20 2026 - 15:09:15 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 8c9dfd2e2a63..d9f1bb64a4a5 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);
SRCU_NOTIFIER_HEAD_STATIC(nvmem_notifier);
+static void nvmem_cell_entry_put(struct nvmem_cell_entry *cell);
+
static int nvmem_access_chunk(struct nvmem_device *nvmem,
const struct nvmem_operations *ops,
unsigned int offset, void *val, size_t bytes, bool write)
@@ -542,22 +545,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)
{
- srcu_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) {
+ srcu_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)
@@ -584,6 +595,7 @@ static int nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
scoped_guard(mutex, &nvmem_mutex) {
if (!rcu_access_pointer(cell->nvmem->ops))
return -ENODEV;
+ kref_init(&cell->refcnt);
list_add_tail(&cell->node, &cell->nvmem->cells);
}
srcu_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
@@ -751,6 +763,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;
}
}
@@ -1397,8 +1410,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;
}
@@ -1422,8 +1437,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;
@@ -1522,6 +1539,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);
}
@@ -1635,6 +1653,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