[PATCH v3 06/11] nvmem: manage cell attributes independently of device resources

From: James Hilliard

Date: Fri Nov 20 2026 - 15:09:40 EST


Cell attributes are populated once and remain until device deletion. A
layout bound after provider registration therefore cannot publish its
cells through an existing group, and the group cannot be refreshed when
cells are removed.

Keep the group in the NVMEM device and explicitly allocate and release
its attributes. Each attribute holds a cell reference until sysfs has
drained its readers. Rebuild the group from the current cell list, and
unwind partial allocations if population fails.

Serialize group replacement separately from cell-list updates so removal
can wait for sysfs readers without holding nvmem_mutex. Remove the group
before device deletion on both unregister and registration failure.

Fixes: 0331c611949f ("nvmem: core: Expose cells through sysfs")
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/nvmem/core.c | 131 +++++++++++++++++++++++++++++++---------------
drivers/nvmem/internals.h | 3 ++
2 files changed, 92 insertions(+), 42 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index d9f1bb64a4a5..c74e3f4e1de6 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -13,6 +13,7 @@
#include <linux/idr.h>
#include <linux/init.h>
#include <linux/kref.h>
+#include <linux/lockdep.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/nvmem-consumer.h>
@@ -476,59 +477,98 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
}

-static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
+static void nvmem_sysfs_free_cell_attrs(const struct bin_attribute * const *attrs)
{
- struct attribute_group group = {
- .name = "cells",
- };
- struct nvmem_cell_entry *entry;
- const struct bin_attribute **pattrs;
- struct bin_attribute *attrs;
- unsigned int ncells = 0, i = 0;
- int ret;
+ if (!attrs)
+ return;

- guard(mutex)(&nvmem_mutex);
+ for (int i = 0; attrs[i]; i++) {
+ nvmem_cell_entry_put(attrs[i]->private);
+ kfree(attrs[i]->attr.name);
+ kfree(attrs[i]);
+ }
+ kfree(attrs);
+}

- if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated)
- return 0;
+static void __nvmem_sysfs_remove_cells(struct nvmem_device *nvmem)
+{
+ lockdep_assert_held(&nvmem->sysfs_mutex);

- /* Allocate an array of attributes with a sentinel */
- ncells = list_count_nodes(&nvmem->cells);
- pattrs = devm_kcalloc(&nvmem->dev, ncells + 1,
- sizeof(*pattrs), GFP_KERNEL);
- if (!pattrs)
- return -ENOMEM;
+ if (!nvmem->sysfs_cells_populated)
+ return;

- attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
- if (!attrs)
- return -ENOMEM;
+ device_remove_group(&nvmem->dev, &nvmem->cells_group);
+ nvmem_sysfs_free_cell_attrs(nvmem->cells_group.bin_attrs);
+ nvmem->cells_group.bin_attrs = NULL;
+ nvmem->sysfs_cells_populated = false;
+}

- /* Initialize each attribute to take the name and size of the cell */
- list_for_each_entry(entry, &nvmem->cells, node) {
- sysfs_bin_attr_init(&attrs[i]);
- attrs[i].attr.name = devm_kasprintf(&nvmem->dev, GFP_KERNEL,
- "%s@%x,%x", entry->name,
- entry->offset,
- entry->bit_offset);
- attrs[i].attr.mode = 0444 & nvmem_bin_attr_get_umode(nvmem);
- attrs[i].size = entry->bytes;
- attrs[i].read = &nvmem_cell_attr_read;
- attrs[i].private = entry;
- if (!attrs[i].attr.name)
- return -ENOMEM;
+static void nvmem_sysfs_remove_cells(struct nvmem_device *nvmem)
+{
+ guard(mutex)(&nvmem->sysfs_mutex);
+ __nvmem_sysfs_remove_cells(nvmem);
+}

- pattrs[i] = &attrs[i];
- i++;
- }
+static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
+{
+ struct nvmem_cell_entry *entry;
+ const struct bin_attribute **attrs = NULL;
+ unsigned int i = 0;
+ int ret = 0;

- group.bin_attrs = pattrs;
+ guard(mutex)(&nvmem->sysfs_mutex);

- ret = device_add_group(&nvmem->dev, &group);
- if (ret)
- return ret;
+ __nvmem_sysfs_remove_cells(nvmem);
+
+ scoped_guard(mutex, &nvmem_mutex) {
+ if (!rcu_access_pointer(nvmem->ops))
+ return -ENODEV;
+ if (list_empty(&nvmem->cells))
+ return 0;

- nvmem->sysfs_cells_populated = true;
+ attrs = kcalloc(list_count_nodes(&nvmem->cells) + 1,
+ sizeof(*attrs), GFP_KERNEL);
+ if (!attrs)
+ return -ENOMEM;

+ list_for_each_entry(entry, &nvmem->cells, node) {
+ struct bin_attribute *attr;
+
+ attr = kzalloc_obj(*attr);
+ if (!attr) {
+ ret = -ENOMEM;
+ break;
+ }
+ sysfs_bin_attr_init(attr);
+ attr->attr.name = kasprintf(GFP_KERNEL, "%s@%x,%x",
+ entry->name, entry->offset,
+ entry->bit_offset);
+ if (!attr->attr.name) {
+ kfree(attr);
+ ret = -ENOMEM;
+ break;
+ }
+ attr->attr.mode = 0444 & nvmem_bin_attr_get_umode(nvmem);
+ attr->size = entry->bytes;
+ attr->read = nvmem_cell_attr_read;
+ attr->private = entry;
+ kref_get(&entry->refcnt);
+ attrs[i++] = attr;
+ }
+ }
+ if (!ret) {
+ nvmem->cells_group.name = "cells";
+ nvmem->cells_group.bin_attrs = attrs;
+
+ ret = device_add_group(&nvmem->dev, &nvmem->cells_group);
+ if (!ret) {
+ nvmem->sysfs_cells_populated = true;
+ return 0;
+ }
+ nvmem->cells_group.bin_attrs = NULL;
+ }
+
+ nvmem_sysfs_free_cell_attrs(attrs);
return ret;
}

@@ -543,6 +583,10 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
{
}

+static void nvmem_sysfs_remove_cells(struct nvmem_device *nvmem)
+{
+}
+
#endif /* CONFIG_NVMEM_SYSFS */

static void nvmem_cell_entry_release(struct kref *ref)
@@ -990,6 +1034,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
rcu_assign_pointer(nvmem->ops, ops);

device_initialize(&nvmem->dev);
+ mutex_init(&nvmem->sysfs_mutex);

if (!config->ignore_wp)
nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
@@ -1098,6 +1143,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem_destroy_layout(nvmem);
#endif
err_remove_dev:
+ nvmem_sysfs_remove_cells(nvmem);
device_del(&nvmem->dev);
err_remove_compat:
nvmem_sysfs_remove_compat(nvmem);
@@ -1125,6 +1171,7 @@ void nvmem_unregister(struct nvmem_device *nvmem)
nvmem_sysfs_remove_compat(nvmem);
nvmem_destroy_layout(nvmem);

+ nvmem_sysfs_remove_cells(nvmem);
device_unregister(&nvmem->dev);
}
EXPORT_SYMBOL_GPL(nvmem_unregister);
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index bc7a99f5aefb..79ea56822ebe 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -4,6 +4,7 @@
#define _LINUX_NVMEM_INTERNALS_H

#include <linux/device.h>
+#include <linux/mutex.h>
#include <linux/nvmem-consumer.h>
#include <linux/nvmem-provider.h>
#include <linux/srcu.h>
@@ -37,7 +38,9 @@ struct nvmem_device {
struct nvmem_layout *layout;
struct nvmem_operations __rcu *ops;
void *priv;
+ struct mutex sysfs_mutex;
bool sysfs_cells_populated;
+ struct attribute_group cells_group;
};

int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np);

--
2.53.0