[PATCH 06/11] nvmem: manage cell attributes independently of device resources
sashiko-bot at kernel.org
sashiko-bot at kernel.org
Tue Nov 17 11:59:55 PST 2026
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] nvmem: mix of cleanup helpers and goto unwinding in
nvmem_populate_sysfs_cells()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Medium] nvmem: resource leak in nvmem_layout_register() [1]
--
commit 3af327034f410c1d0e7599f729d78e0fec4f2dfa
Author: James Hilliard <james.hilliard1 at gmail.com>
nvmem: manage cell attributes independently of device resources
This commit manages NVMEM sysfs cell attributes explicitly with reference
counting and a dedicated mutex so the attribute group can be rebuilt or
removed independently of device lifetime.
> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 70b6126886d9f..14cc70e8ce785 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
[ ... ]
> @@ -496,59 +497,102 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
[ ... ]
> static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
> {
> - 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;
> + const struct bin_attribute **attrs = NULL;
> + unsigned int i = 0;
> + int ret = 0;
>
> - guard(mutex)(&nvmem_mutex);
> + guard(mutex)(&nvmem->sysfs_mutex);
>
> - if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated)
> - return 0;
> + __nvmem_sysfs_remove_cells(nvmem);
>
> - /* 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;
> -
> - attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
> - if (!attrs)
> - return -ENOMEM;
> + scoped_guard(mutex, &nvmem_mutex) {
> + if (!rcu_access_pointer(nvmem->ops))
> + return -ENODEV;
> + if (list_empty(&nvmem->cells))
> + return 0;
>
> - /* 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)
> + attrs = kcalloc(list_count_nodes(&nvmem->cells) + 1,
> + sizeof(*attrs), GFP_KERNEL);
> + if (!attrs)
> return -ENOMEM;
>
> - pattrs[i] = &attrs[i];
> - i++;
> + 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)
> + goto err_free;
>
> - group.bin_attrs = pattrs;
> + nvmem->cells_group.name = "cells";
> + nvmem->cells_group.bin_attrs = attrs;
>
> - ret = device_add_group(&nvmem->dev, &group);
> - if (ret)
> - return ret;
> + ret = device_add_group(&nvmem->dev, &nvmem->cells_group);
> + if (ret) {
> + nvmem->cells_group.bin_attrs = NULL;
> + goto err_free;
> + }
>
> nvmem->sysfs_cells_populated = true;
> + return 0;
>
> +err_free:
> + nvmem_sysfs_free_cell_attrs(attrs);
> return ret;
> }
[Severity: Low]
This isn't a bug, but nvmem_populate_sysfs_cells() mixes guard() and
scoped_guard() with goto err_free cleanup in the same function, whereas
cleanup.h guidelines prefer either converting all resources in a function
to scope-based cleanup or keeping traditional goto unwinding.
[Severity: Medium]
This problem wasn't introduced by this patch, but in the caller
nvmem_layout_register(), can a partial failure in layout->add_cells() leak
nvmem_cell_entry structures [1]?
drivers/nvmem/core.c:nvmem_layout_register() {
...
scoped_guard(srcu, &layout->nvmem->srcu) {
if (!srcu_dereference(layout->nvmem->ops, &layout->nvmem->srcu))
return -ENODEV;
ret = layout->add_cells(layout);
}
if (ret)
return ret;
#ifdef CONFIG_NVMEM_SYSFS
ret = nvmem_populate_sysfs_cells(layout->nvmem);
if (ret) {
nvmem_device_remove_all_cells(layout->nvmem);
return ret;
}
#endif
...
}
Unlike the nvmem_populate_sysfs_cells() error path right below it, if
layout->add_cells() fails after adding one or more cells, it returns
without calling nvmem_device_remove_all_cells(layout->nvmem), leaving those
entries attached to nvmem->cells and duplicating them if the layout is
later retried or rebound.
[1] https://sashiko.dev/#/bug/linux-014a0624-e8da-49aa-95a8-67a7c19dfa24
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-submit-nvmem-provider-lifetime-v1-v1-0-3cfbd48812b5@gmail.com?part=6
More information about the linux-mtd
mailing list