[PATCH v3 9/9] nvmem: protect nvmem_device::ops with SRCU
From: Bartosz Golaszewski
Date: Wed Apr 29 2026 - 11:56:07 EST
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@xxxxxxxxxxxxxxxx>
---
drivers/nvmem/core.c | 42 ++++++++++++++++++++++++++++++++++++------
drivers/nvmem/internals.h | 4 +++-
2 files changed, 39 insertions(+), 7 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 590d880ede944670c09dd62b394626a959e5cdd1..9ec2d831ece6e51f918b5a815e45db346f30be7f 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 = rcu_dereference(nvmem->ops);
+ 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, written;
+ guard(srcu)(&nvmem->srcu);
+ ops = rcu_dereference(nvmem->ops);
+ if (!ops)
+ return -ENODEV;
+
if (!ops->reg_write)
return -EOPNOTSUPP;
@@ -283,7 +293,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;
@@ -327,7 +337,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
@@ -554,7 +564,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);
}
@@ -930,6 +940,18 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem->dev.bus = &nvmem_bus_type;
nvmem->dev.parent = config->dev;
+ /*
+ * 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);
+ }
+
device_initialize(&nvmem->dev);
if (!config->ignore_wp)
@@ -947,7 +969,8 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
ops->reg_read = config->reg_read;
ops->reg_write = config->reg_write;
- nvmem->ops = ops;
+ rcu_assign_pointer(nvmem->ops, ops);
+
nvmem->owner = config->owner;
if (!nvmem->owner && config->dev->driver)
nvmem->owner = config->dev->driver->owner;
@@ -1046,6 +1069,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem_sysfs_remove_compat(nvmem);
err_put_device:
put_device(&nvmem->dev);
+ kfree(ops);
return ERR_PTR(rval);
}
@@ -1058,10 +1082,16 @@ EXPORT_SYMBOL_GPL(nvmem_register);
*/
void nvmem_unregister(struct nvmem_device *nvmem)
{
+ struct nvmem_operations *ops;
+
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 60daa79c404dc915872481bd3b02de2258d33406..17418fd0dcc92b101b56082fc15d74042386107d 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.47.3