[PATCH 03/11] nvmem: exclude disconnected providers from callback admission
From: James Hilliard
Date: Tue Nov 17 2026 - 14:39:01 EST
A provider remains visible until device_unregister(), after its operations
have been disconnected. Device matching can still call into it during
that interval, and consumer acquisition can use its module pointer after
unregister has returned. Cell registration and DT fixups also lack a
callback drain.
Cover matching, cell addition, DT fixups and layout population with the
existing SRCU domain. Serialize disconnection with consumer acquisition
under nvmem_mutex, keeping the module-reference acquisition inside that
critical section. Check sysfs permissions under SRCU as well, since the
operations pointer may already have been cleared.
Share the disconnect sequence between unregister and registration unwind.
Fixes: 6a7b4134fb7a ("nvmem: protect nvmem_device::ops with SRCU")
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/nvmem/core.c | 77 ++++++++++++++++++++++++++++++++++++++++------------
1 file changed, 59 insertions(+), 18 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 73aa53145f0b..c41266241f3f 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -307,10 +307,14 @@ 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 = rcu_dereference_raw(nvmem->ops);
-
+ struct nvmem_operations *ops;
umode_t mode = 0400;
+ guard(srcu)(&nvmem->srcu);
+ ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+ if (!ops)
+ return 0;
+
if (!nvmem->root_only)
mode |= 0044;
@@ -351,7 +355,12 @@ 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 = rcu_dereference_raw(nvmem->ops);
+ struct nvmem_operations *ops;
+
+ guard(srcu)(&nvmem->srcu);
+ ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+ if (!ops)
+ return 0;
/*
* If the device has no .reg_write operation, do not allow
@@ -673,6 +682,10 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem,
struct nvmem_cell_entry *cell;
int rval;
+ guard(srcu)(&nvmem->srcu);
+ if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
+ return -ENODEV;
+
cell = kzalloc_obj(*cell);
if (!cell)
return -ENOMEM;
@@ -815,6 +828,10 @@ int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
const __be32 *addr;
int len, ret;
+ guard(srcu)(&nvmem->srcu);
+ if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
+ return -ENODEV;
+
for_each_child_of_node_scoped(np, child) {
struct nvmem_cell_info info = {0};
@@ -871,8 +888,11 @@ int nvmem_layout_register(struct nvmem_layout *layout)
if (!layout->add_cells)
return -EINVAL;
- /* Populate the cells */
- ret = layout->add_cells(layout);
+ scoped_guard(srcu, &layout->nvmem->srcu) {
+ if (!srcu_dereference(layout->nvmem->ops, &layout->nvmem->srcu))
+ return -ENODEV;
+ ret = layout->add_cells(layout);
+ }
if (ret)
return ret;
@@ -894,6 +914,17 @@ void nvmem_layout_unregister(struct nvmem_layout *layout)
}
EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
+static void nvmem_disconnect(struct nvmem_device *nvmem)
+{
+ struct nvmem_operations *ops;
+
+ scoped_guard(mutex, &nvmem_mutex)
+ ops = rcu_replace_pointer(nvmem->ops, NULL,
+ lockdep_is_held(&nvmem_mutex));
+ synchronize_srcu(&nvmem->srcu);
+ kfree(ops);
+}
+
/**
* nvmem_register() - Register a nvmem device for given nvmem_config.
* Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
@@ -1067,10 +1098,8 @@ 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);
+ nvmem_disconnect(nvmem);
put_device(&nvmem->dev);
- kfree(ops);
return ERR_PTR(rval);
}
@@ -1083,19 +1112,15 @@ 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_disconnect(nvmem);
nvmem_sysfs_remove_compat(nvmem);
nvmem_destroy_layout(nvmem);
- kfree(ops);
device_unregister(&nvmem->dev);
}
@@ -1135,17 +1160,33 @@ struct nvmem_device *devm_nvmem_register(struct device *dev,
}
EXPORT_SYMBOL_GPL(devm_nvmem_register);
+struct nvmem_match_data {
+ void *data;
+ int (*match)(struct device *dev, const void *data);
+};
+
+static int nvmem_match_device(struct device *dev, const void *data)
+{
+ const struct nvmem_match_data *match = data;
+ struct nvmem_device *nvmem = to_nvmem_device(dev);
+
+ guard(srcu)(&nvmem->srcu);
+ if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
+ return 0;
+ return match->match(dev, match->data);
+}
+
static struct nvmem_device *nvmem_device_match(void *data,
int (*match)(struct device *dev, const void *data))
{
+ struct nvmem_match_data match_data = { .data = data, .match = match };
struct nvmem_device *nvmem = NULL;
struct device *dev;
- scoped_guard(mutex, &nvmem_mutex) {
- dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
- if (dev)
- nvmem = to_nvmem_device(dev);
- }
+ guard(mutex)(&nvmem_mutex);
+ dev = bus_find_device(&nvmem_bus_type, NULL, &match_data, nvmem_match_device);
+ if (dev)
+ nvmem = to_nvmem_device(dev);
if (!nvmem)
return ERR_PTR(-EPROBE_DEFER);
--
2.53.0