mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
To: Srinivas Kandagatla <srini@kernel.org>,
	Bartosz Golaszewski <brgl@kernel.org>
Cc: linux-kernel@vger.kernel.org, brgl@kernel.org,
	Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Subject: [PATCH v2 6/7] nvmem: split struct nvmem_device into refcounted and provider-owned data
Date: Mon, 23 Feb 2026 11:57:07 +0100	[thread overview]
Message-ID: <20260223-nvmem-unbind-v2-6-0df33a933dca@oss.qualcomm.com> (raw)
In-Reply-To: <20260223-nvmem-unbind-v2-0-0df33a933dca@oss.qualcomm.com>

Data owned by the nvmem provider must not be part of the
reference-counted struct nvmem_device. Ahead of protecting that data
with SRCU, move it into a separate structure the address of which is
stored in nvmem_device.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
 drivers/nvmem/core.c      | 39 ++++++++++++++++++++++++++++-----------
 drivers/nvmem/internals.h | 14 +++++++++++---
 2 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 30aee8089f1dc65b1fa07dac8fb35e1dbbcd0ce7..9ff97330682975ef724b542fee6089fa7cd5414a 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -57,25 +57,28 @@ static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
 static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
 			    void *val, size_t bytes)
 {
-	if (!nvmem->reg_read)
+	struct nvmem_impl *impl = nvmem->impl;
+
+	if (!impl->reg_read)
 		return -EOPNOTSUPP;
 
-	return nvmem->reg_read(nvmem->priv, offset, val, bytes);
+	return impl->reg_read(impl->priv, offset, val, bytes);
 }
 
 static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
 			     void *val, size_t bytes)
 {
+	struct nvmem_impl *impl = nvmem->impl;
 	int ret, written;
 
-	if (!nvmem->reg_write)
+	if (!impl->reg_write)
 		return -EOPNOTSUPP;
 
 	ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
 	if (ret)
 		return ret;
 
-	written = nvmem->reg_write(nvmem->priv, offset, val, bytes);
+	written = impl->reg_write(impl->priv, offset, val, bytes);
 
 	ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
 	if (ret)
@@ -286,6 +289,8 @@ static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
 
 static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
 {
+	struct nvmem_impl *impl = nvmem->impl;
+
 	umode_t mode = 0400;
 
 	if (!nvmem->root_only)
@@ -294,10 +299,10 @@ static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
 	if (!nvmem->read_only)
 		mode |= 0200;
 
-	if (!nvmem->reg_write)
+	if (!impl->reg_write)
 		mode &= ~0200;
 
-	if (!nvmem->reg_read)
+	if (!impl->reg_read)
 		mode &= ~0444;
 
 	return mode;
@@ -328,6 +333,7 @@ static umode_t nvmem_attr_is_visible(struct kobject *kobj,
 {
 	struct device *dev = kobj_to_dev(kobj);
 	struct nvmem_device *nvmem = to_nvmem_device(dev);
+	struct nvmem_impl *impl = nvmem->impl;
 
 	/*
 	 * If the device has no .reg_write operation, do not allow
@@ -336,7 +342,7 @@ static umode_t nvmem_attr_is_visible(struct kobject *kobj,
 	 * can be forced into read-write mode using the 'force_ro'
 	 * attribute.
 	 */
-	if (attr == &dev_attr_force_ro.attr && !nvmem->reg_write)
+	if (attr == &dev_attr_force_ro.attr && !impl->reg_write)
 		return 0;	/* Attribute not visible */
 
 	return attr->mode;
@@ -537,6 +543,7 @@ static void nvmem_release(struct device *dev)
 
 	ida_free(&nvmem_ida, nvmem->id);
 	gpiod_put(nvmem->wp_gpio);
+	kfree(nvmem->impl);
 	kfree(nvmem);
 }
 
@@ -898,6 +905,7 @@ EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
 struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 {
 	struct nvmem_device *nvmem;
+	struct nvmem_impl *impl;
 	int rval;
 
 	if (!config->dev)
@@ -910,8 +918,15 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	if (!nvmem)
 		return ERR_PTR(-ENOMEM);
 
+	impl = kzalloc_obj(*impl, GFP_KERNEL);
+	if (!impl) {
+		kfree(nvmem);
+		return ERR_PTR(-ENOMEM);
+	}
+
 	rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
 	if (rval < 0) {
+		kfree(impl);
 		kfree(nvmem);
 		return ERR_PTR(rval);
 	}
@@ -937,6 +952,11 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	INIT_LIST_HEAD(&nvmem->cells);
 	nvmem->fixup_dt_cell_info = config->fixup_dt_cell_info;
 
+	impl->priv = config->priv;
+	impl->reg_read = config->reg_read;
+	impl->reg_write = config->reg_write;
+
+	nvmem->impl = impl;
 	nvmem->owner = config->owner;
 	if (!nvmem->owner && config->dev->driver)
 		nvmem->owner = config->dev->driver->owner;
@@ -944,10 +964,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	nvmem->word_size = config->word_size ?: 1;
 	nvmem->size = config->size;
 	nvmem->root_only = config->root_only;
-	nvmem->priv = config->priv;
 	nvmem->type = config->type;
-	nvmem->reg_read = config->reg_read;
-	nvmem->reg_write = config->reg_write;
 	nvmem->keepout = config->keepout;
 	nvmem->nkeepout = config->nkeepout;
 	if (config->of_node)
@@ -973,7 +990,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 		goto err_put_device;
 
 	nvmem->read_only = device_property_present(config->dev, "read-only") ||
-			   config->read_only || !nvmem->reg_write;
+			   config->read_only || !impl->reg_write;
 
 #ifdef CONFIG_NVMEM_SYSFS
 	nvmem->dev.groups = nvmem_dev_groups;
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index 7cbc55f40259fc4315c41979ad8bf75c36bcb056..05197074799ff3e2a6720f6552878a9e1354a5c3 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -7,9 +7,20 @@
 #include <linux/nvmem-consumer.h>
 #include <linux/nvmem-provider.h>
 
+/*
+ * Holds data owned by the provider of the nvmem implementation. This goes
+ * away immediately the moment nvmem_unregister() is called.
+ */
+struct nvmem_impl {
+	nvmem_reg_read_t	reg_read;
+	nvmem_reg_write_t	reg_write;
+	void			*priv;
+};
+
 struct nvmem_device {
 	struct module		*owner;
 	struct device		dev;
+	struct nvmem_impl	*impl;
 	int			stride;
 	int			word_size;
 	int			id;
@@ -26,11 +37,8 @@ struct nvmem_device {
 				   struct nvmem_cell_info *cell);
 	const struct nvmem_keepout *keepout;
 	unsigned int		nkeepout;
-	nvmem_reg_read_t	reg_read;
-	nvmem_reg_write_t	reg_write;
 	struct gpio_desc	*wp_gpio;
 	struct nvmem_layout	*layout;
-	void *priv;
 	bool			sysfs_cells_populated;
 };
 

-- 
2.47.3


  parent reply	other threads:[~2026-02-23 10:58 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-23 10:57 [PATCH v2 0/7] nvmem: survive unbind with active consumers Bartosz Golaszewski
2026-02-23 10:57 ` [PATCH v2 1/7] nvmem: remove unused field from struct nvmem_device Bartosz Golaszewski
2026-03-23 16:00   ` Johan Hovold
2026-02-23 10:57 ` [PATCH v2 2/7] nvmem: return -EOPNOTSUPP to in-kernel users on missing callbacks Bartosz Golaszewski
2026-03-23 16:21   ` Johan Hovold
2026-04-29 15:45     ` Bartosz Golaszewski
2026-02-23 10:57 ` [PATCH v2 3/7] nvmem: check the return value of gpiod_set_value_cansleep() Bartosz Golaszewski
2026-03-23 16:28   ` Johan Hovold
2026-02-23 10:57 ` [PATCH v2 4/7] nvmem: simplify locking with guard() Bartosz Golaszewski
2026-03-23 16:35   ` Johan Hovold
2026-04-29 15:45     ` Bartosz Golaszewski
2026-02-23 10:57 ` [PATCH v2 5/7] nvmem: remove unneeded __nvmem_device_put() Bartosz Golaszewski
2026-03-23 16:44   ` Johan Hovold
2026-04-29 15:45     ` Bartosz Golaszewski
2026-02-23 10:57 ` Bartosz Golaszewski [this message]
2026-03-17 10:44   ` [PATCH v2 6/7] nvmem: split struct nvmem_device into refcounted and provider-owned data Johan Hovold
2026-03-17 12:52     ` Bartosz Golaszewski
2026-03-23 16:45       ` Johan Hovold
2026-02-23 10:57 ` [PATCH v2 7/7] nvmem: synchronize nvmem device unregistering with SRCU Bartosz Golaszewski
2026-03-17 11:31   ` Johan Hovold

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=20260223-nvmem-unbind-v2-6-0df33a933dca@oss.qualcomm.com \
    --to=bartosz.golaszewski@oss.qualcomm.com \
    --cc=brgl@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --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®