All of lore.kernel.org
 help / color / mirror / Atom feed
From: srini@kernel.org
To: gregkh@linuxfoundation.org
Cc: linux-kernel@vger.kernel.org,
	Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>,
	Srinivas Kandagatla <srini@kernel.org>
Subject: [PATCH 6/8] nvmem: don't use devres in subsystem code
Date: Tue,  1 Dec 2026 23:27:34 +0000	[thread overview]
Message-ID: <20261201232736.537847-7-srini@kernel.org> (raw)
In-Reply-To: <20261201232736.537847-1-srini@kernel.org>

From: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>

Using device-managed APIs at sub-system level can cause issues due to
lack of a strict API contract, especially when relying on managed
resources being freed on device release. Devres should only be used in
drivers, ideally only when it's bound to a driver.

In nvmem, the release of the memory allocated for sysfs attributes,
happens when the last reference to nvmem->dev is dropped, not in
nvmem_unregister(). This means that consumer holding references to nvmem
devices keep the memory around after sysfs files are gone which happens
in device_del().

The rest of nvmem/core.c manages resources explicitly, while using
devres in sysfs registration hides the details of what goes on and is
inconsistent.

Switch to non-managed interfaces exclusively in nvmem core.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Signed-off-by: Srinivas Kandagatla <srini@kernel.org>
---
 drivers/nvmem/core.c      | 60 ++++++++++++++++++++++++++++-----------
 drivers/nvmem/internals.h |  6 ++++
 2 files changed, 50 insertions(+), 16 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index d976c0d6603b..afe9d3d66914 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -478,12 +478,7 @@ 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;
 
@@ -494,43 +489,72 @@ static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
 
 	/* Allocate an array of attributes with a sentinel */
 	ncells = list_count_nodes(&nvmem->cells);
-	pattrs = devm_kzalloc_objs(&nvmem->dev, *pattrs, ncells + 1);
+
+	const struct bin_attribute **pattrs __free(kfree) = kzalloc_objs(*pattrs, ncells + 1);
 	if (!pattrs)
 		return -ENOMEM;
 
-	attrs = devm_kzalloc_objs(&nvmem->dev, struct bin_attribute, ncells);
+	struct bin_attribute *attrs __free(kfree) = kzalloc_objs(*attrs, ncells);
 	if (!attrs)
 		return -ENOMEM;
 
 	/* 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.name = kasprintf(GFP_KERNEL, "%s@%x,%x", entry->name,
+					       entry->offset, entry->bit_offset);
+		if (!attrs[i].attr.name) {
+			list_for_each_entry_continue_reverse(entry, &nvmem->cells, node)
+				kfree(attrs[--i].attr.name);
+			return -ENOMEM;
+		}
+
 		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;
 
 		pattrs[i] = &attrs[i];
 		i++;
 	}
 
-	group.bin_attrs = pattrs;
+	memset(&nvmem->sysfs_group, 0, sizeof(nvmem->sysfs_group));
+	nvmem->sysfs_group.name = "cells";
+	nvmem->sysfs_group.bin_attrs = pattrs;
 
-	ret = device_add_group(&nvmem->dev, &group);
-	if (ret)
+	ret = device_add_group(&nvmem->dev, &nvmem->sysfs_group);
+	if (ret) {
+		i = 0;
+		list_for_each_entry(entry, &nvmem->cells, node)
+			kfree(attrs[i++].attr.name);
 		return ret;
+	}
 
 	nvmem->sysfs_cells_populated = true;
+	nvmem->sysfs_attrs = no_free_ptr(attrs);
+	nvmem->sysfs_pattrs = no_free_ptr(pattrs);
 
 	return ret;
 }
 
+static void nvmem_sysfs_remove_cells(struct nvmem_device *nvmem)
+{
+	int i;
+
+	if (!nvmem->sysfs_cells_populated)
+		return;
+
+	device_remove_group(&nvmem->dev, &nvmem->sysfs_group);
+
+	for (i = 0; nvmem->sysfs_pattrs[i]; i++)
+		kfree(nvmem->sysfs_pattrs[i]->attr.name);
+
+	kfree(nvmem->sysfs_attrs);
+	kfree(nvmem->sysfs_pattrs);
+
+	nvmem->sysfs_cells_populated = false;
+}
+
 #else /* CONFIG_NVMEM_SYSFS */
 
 static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
@@ -541,6 +565,9 @@ static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
 static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
 {
 }
+static void nvmem_sysfs_remove_cells(struct nvmem_device *nvmem)
+{
+}
 
 #endif /* CONFIG_NVMEM_SYSFS */
 
@@ -1085,6 +1112,7 @@ void nvmem_unregister(struct nvmem_device *nvmem)
 
 	nvmem_sysfs_remove_compat(nvmem);
 	nvmem_destroy_layout(nvmem);
+	nvmem_sysfs_remove_cells(nvmem);
 	kfree(ops);
 
 	device_unregister(&nvmem->dev);
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index bc7a99f5aefb..b461ec6a0e86 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -7,6 +7,7 @@
 #include <linux/nvmem-consumer.h>
 #include <linux/nvmem-provider.h>
 #include <linux/srcu.h>
+#include <linux/sysfs.h>
 
 /* Hold pointers to callbacks owned by the nvmem provider module. */
 struct nvmem_operations {
@@ -37,7 +38,12 @@ struct nvmem_device {
 	struct nvmem_layout	*layout;
 	struct nvmem_operations __rcu *ops;
 	void *priv;
+#if IS_ENABLED(CONFIG_NVMEM_SYSFS)
 	bool			sysfs_cells_populated;
+	struct attribute_group	sysfs_group;
+	struct bin_attribute	*sysfs_attrs;
+	const struct bin_attribute **sysfs_pattrs;
+#endif /* CONFIG_NVMEM_SYSFS */
 };
 
 int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np);
-- 
2.53.0


  parent reply	other threads:[~2026-12-01 23:28 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 23:27 [PATCH 0/8] nvmem: patches for v7.5 srini
2026-12-01 23:27 ` [PATCH 1/8] dt-bindings: nvmem: Add compatible for MSM8952 srini
2026-12-01 23:27 ` [PATCH 2/8] dt-bindings: nvmem: qfprom: Add SM8350 compatible srini
2026-12-01 23:27 ` [PATCH 3/8] nvmem: qfprom: Add the SM8350 keepout regions srini
2026-12-01 23:27 ` [PATCH 4/8] nvmem: m24lr: Remove use of i2c_match_id() srini
2026-12-01 23:27 ` [PATCH 5/8] dt-bindings: nvmem: st,stm32-romem: align patternProperties regex on nvmem-deprecated-cells.yaml srini
2026-12-01 23:27 ` srini [this message]
2026-12-01 23:27 ` [PATCH 7/8] nvmem: use const APIs for legacy sysfs attributes srini
2026-12-01 23:27 ` [PATCH 8/8] dt-bindings: nvmem: SID: Add binding for A733 SID controller srini

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=20261201232736.537847-7-srini@kernel.org \
    --to=srini@kernel.org \
    --cc=bartosz.golaszewski@oss.qualcomm.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.