From: James Hilliard <james.hilliard1@gmail.com>
To: "Srinivas Kandagatla" <srini@kernel.org>,
"Mathieu Dubois-Briand" <mathieu.dubois-briand@bootlin.com>,
"Michael Walle" <mwalle@kernel.org>,
"Rafał Miłecki" <rafal@milecki.pl>,
"Michael Walle" <michael@walle.cc>,
"Linus Walleij" <linusw@kernel.org>,
"Khouloud Touil" <ktouil@baylibre.com>,
"Bartosz Golaszewski" <brgl@kernel.org>
Cc: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Miquel Raynal <miquel.raynal@bootlin.com>,
Paul Cercueil <paul@crapouillou.net>,
linux-kernel@vger.kernel.org, linux-mtd@lists.infradead.org,
James Hilliard <james.hilliard1@gmail.com>
Subject: [PATCH v2 06/11] nvmem: manage cell attributes independently of device resources
Date: Thu, 19 Nov 2026 09:42:04 -0700 [thread overview]
Message-ID: <20261119-submit-nvmem-provider-lifetime-v1-v2-6-8fca3ec5f18c@gmail.com> (raw)
In-Reply-To: <20261119-submit-nvmem-provider-lifetime-v1-v2-0-8fca3ec5f18c@gmail.com>
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@gmail.com>
---
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
next prev parent reply other threads:[~2026-11-19 16:42 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 16:41 [PATCH v2 00/11] nvmem: complete provider and layout disconnection James Hilliard
2026-11-19 16:41 ` [PATCH v2 01/11] nvmem: retain the provider node until device release James Hilliard
2026-11-19 16:42 ` [PATCH v2 02/11] nvmem: drain keepout and cell post-processing accesses James Hilliard
2026-11-19 16:42 ` [PATCH v2 03/11] nvmem: exclude disconnected providers from callback admission James Hilliard
2026-11-19 16:42 ` [PATCH v2 04/11] nvmem: release write protection when the provider disconnects James Hilliard
2026-11-19 16:42 ` [PATCH v2 05/11] nvmem: reference cell entries independently of their provider list James Hilliard
2026-11-19 16:42 ` James Hilliard [this message]
2026-11-19 16:42 ` [PATCH v2 07/11] nvmem: distinguish layout cells from provider cells James Hilliard
2026-11-19 16:42 ` [PATCH v2 08/11] nvmem: disconnect cells before removing their layout James Hilliard
2026-11-19 16:42 ` [PATCH v2 09/11] nvmem: disconnect published devices before registration rollback James Hilliard
2026-11-19 16:42 ` [PATCH v2 10/11] nvmem: u-boot-env: propagate cell registration errors James Hilliard
2026-11-19 16:42 ` [PATCH v2 11/11] nvmem: allow providers to specify an explicit NULL owner James Hilliard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261119-submit-nvmem-provider-lifetime-v1-v2-6-8fca3ec5f18c@gmail.com \
--to=james.hilliard1@gmail.com \
--cc=bartosz.golaszewski@oss.qualcomm.com \
--cc=brgl@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=ktouil@baylibre.com \
--cc=linusw@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=mathieu.dubois-briand@bootlin.com \
--cc=michael@walle.cc \
--cc=miquel.raynal@bootlin.com \
--cc=mwalle@kernel.org \
--cc=paul@crapouillou.net \
--cc=rafal@milecki.pl \
--cc=srini@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®