From: srini@kernel.org
To: gregkh@linuxfoundation.org
Cc: linux-kernel@vger.kernel.org,
Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>,
Loic Poulain <loic.poulain@oss.qualcomm.com>,
Srinivas Kandagatla <srini@kernel.org>
Subject: [PATCH 09/14] nvmem: protect nvmem_device::ops with SRCU
Date: Wed, 29 Jul 2026 10:46:42 +0100 [thread overview]
Message-ID: <20260729094647.111468-10-srini@kernel.org> (raw)
In-Reply-To: <20260729094647.111468-1-srini@kernel.org>
From: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
With the provider-owned data split out into a separate 'ops' structure,
we can now protect it with SRCU.
Protect all dereferences of nvmem->ops with an SRCU read lock.
Synchronize SRCU in nvmem_unregister() after setting the implementation
pointer to NULL. This has the effect of numbing down the device after
nvmem_unregister() returns - it will no longer accept any consumer calls
and return -ENODEV. The actual device will live on for as long as there
are references to it but we will no longer reach into the consumer's
memory which may be gone by this time.
Nvmem cell entries are destroyed in .release() now as they may be still
dereferenced via the nvmem_cell handles after nvmem_release(). The
actual calls will still go through SRCU and fail with -ENODEV if the
provider is gone.
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Tested-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
Signed-off-by: Srinivas Kandagatla <srini@kernel.org>
---
drivers/nvmem/core.c | 44 +++++++++++++++++++++++++++++++++------
drivers/nvmem/internals.h | 4 +++-
2 files changed, 41 insertions(+), 7 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 8b227db91923..f65aebe7837b 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -57,7 +57,12 @@ static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
- struct nvmem_operations *ops = nvmem->ops;
+ struct nvmem_operations *ops;
+
+ guard(srcu)(&nvmem->srcu);
+ ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+ if (!ops)
+ return -ENODEV;
if (!ops->reg_read)
return -EOPNOTSUPP;
@@ -68,9 +73,14 @@ static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
- struct nvmem_operations *ops = nvmem->ops;
+ struct nvmem_operations *ops;
int ret, wr_ok;
+ guard(srcu)(&nvmem->srcu);
+ ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+ if (!ops)
+ return -ENODEV;
+
if (!ops->reg_write)
return -EOPNOTSUPP;
@@ -289,7 +299,7 @@ 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_operations *ops = nvmem->ops;
+ struct nvmem_operations *ops = rcu_dereference_raw(nvmem->ops);
umode_t mode = 0400;
@@ -333,7 +343,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_operations *ops = nvmem->ops;
+ struct nvmem_operations *ops = rcu_dereference_raw(nvmem->ops);
/*
* If the device has no .reg_write operation, do not allow
@@ -560,7 +570,7 @@ static void nvmem_release(struct device *dev)
gpiod_put(nvmem->wp_gpio);
nvmem_device_remove_all_cells(nvmem);
ida_free(&nvmem_ida, nvmem->id);
- kfree(nvmem->ops);
+ cleanup_srcu_struct(&nvmem->srcu);
kfree(nvmem);
}
@@ -920,7 +930,20 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem->dev.bus = &nvmem_bus_type;
nvmem->dev.parent = config->dev;
INIT_LIST_HEAD(&nvmem->cells);
- nvmem->ops = ops;
+
+ /*
+ * Must happen before we assign the release() callback in
+ * device_initialize().
+ */
+ rval = init_srcu_struct(&nvmem->srcu);
+ if (rval) {
+ ida_free(&nvmem_ida, nvmem->id);
+ kfree(ops);
+ kfree(nvmem);
+ return ERR_PTR(rval);
+ }
+
+ rcu_assign_pointer(nvmem->ops, ops);
device_initialize(&nvmem->dev);
@@ -1035,7 +1058,10 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
err_remove_compat:
nvmem_sysfs_remove_compat(nvmem);
err_put_device:
+ ops = rcu_replace_pointer(nvmem->ops, NULL, true);
+ synchronize_srcu(&nvmem->srcu);
put_device(&nvmem->dev);
+ kfree(ops);
return ERR_PTR(rval);
}
@@ -1048,13 +1074,19 @@ EXPORT_SYMBOL_GPL(nvmem_register);
*/
void nvmem_unregister(struct nvmem_device *nvmem)
{
+ struct nvmem_operations *ops;
+
if (!nvmem)
return;
blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
+ ops = rcu_replace_pointer(nvmem->ops, NULL, true);
+ synchronize_srcu(&nvmem->srcu);
+
nvmem_sysfs_remove_compat(nvmem);
nvmem_destroy_layout(nvmem);
+ kfree(ops);
device_unregister(&nvmem->dev);
}
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index 2c3645a27272..bc7a99f5aefb 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -6,6 +6,7 @@
#include <linux/device.h>
#include <linux/nvmem-consumer.h>
#include <linux/nvmem-provider.h>
+#include <linux/srcu.h>
/* Hold pointers to callbacks owned by the nvmem provider module. */
struct nvmem_operations {
@@ -16,6 +17,7 @@ struct nvmem_operations {
struct nvmem_device {
struct module *owner;
struct device dev;
+ struct srcu_struct srcu;
int stride;
int word_size;
int id;
@@ -33,7 +35,7 @@ struct nvmem_device {
unsigned int nkeepout;
struct gpio_desc *wp_gpio;
struct nvmem_layout *layout;
- struct nvmem_operations *ops;
+ struct nvmem_operations __rcu *ops;
void *priv;
bool sysfs_cells_populated;
};
--
2.53.0
next prev parent reply other threads:[~2026-07-29 9:47 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-29 9:46 [PATCH 00/14] nvmem: updates for 7.3 srini
2026-07-29 9:46 ` [PATCH 01/14] nvmem: remove unused field from struct nvmem_device srini
2026-07-29 9:46 ` [PATCH 02/14] nvmem: return -EOPNOTSUPP to in-kernel users on missing callbacks srini
2026-07-29 9:46 ` [PATCH 03/14] nvmem: check the return value of gpiod_set_value_cansleep() srini
2026-07-29 9:46 ` [PATCH 04/14] nvmem: simplify locking with guard() srini
2026-07-29 9:46 ` [PATCH 05/14] nvmem: remove unneeded __nvmem_device_put() srini
2026-07-29 9:46 ` [PATCH 06/14] nvmem: split out the reg_read/write() callbacks out of struct nvmem_device srini
2026-07-29 9:46 ` [PATCH 07/14] nvmem: simplify nvmem_sysfs_remove_compat() srini
2026-07-29 9:46 ` [PATCH 08/14] nvmem: remove duplicated reference counting srini
2026-07-29 9:46 ` srini [this message]
2026-07-29 9:46 ` [PATCH 10/14] eeprom: move nvmem EEPROM drivers to drivers/nvmem/ srini
2026-07-29 9:46 ` [PATCH 11/14] nvmem: airoha: add ARM64 dependency srini
2026-07-29 9:46 ` [PATCH 12/14] nvmem: brcm_nvram: reject empty NVRAM partition srini
2026-07-29 9:46 ` [PATCH 13/14] nvmem: brcm_nvram: fix out-of-bounds access on malformed flash data srini
2026-07-29 9:46 ` [PATCH 14/14] dt-bindings: nvmem: qfprom: Add ipq5210 & ipq9650 compatible 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=20260729094647.111468-10-srini@kernel.org \
--to=srini@kernel.org \
--cc=bartosz.golaszewski@oss.qualcomm.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=loic.poulain@oss.qualcomm.com \
/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.