[PATCH v4 01/58] nvmem: disconnect provider callbacks before unregister returns
From: James Hilliard
Date: Fri Nov 13 2026 - 13:43:24 EST
An NVMEM consumer or device reference can outlive provider unbind. The
reference retains the NVMEM allocation, but not the private data or
hardware resources that the provider releases during remove. Pinning the
provider module also does not prevent unbind from freeing that state.
Reject new callback access once unregister starts and drain admitted
accesses with SRCU before returning to the provider. Cover register
access, keepout handling, cell post-processing and device matching.
Existing consumers retain core-owned metadata and return -ENODEV after
disconnection. Release the write-protect GPIO after the drain so
retained references do not prevent a new provider from requesting it.
The final device release frees only core-owned data.
Give cell entries their own references and retire layout cells when
their driver is unbound. Remove and drain cell attributes and callbacks
before layout-owned state is freed. Rebinding creates new entries; held
old cells remain disconnected. Remove partially added cells when a
layout probe fails, and destroy the layout during provider unregister
rather than at the final consumer put.
Track layout ownership explicitly so provider cells added after layout
creation survive its unbind. Rebuild sysfs cell attributes when layouts
bind or unbind, retaining cell entries until their attributes have been
removed. Free partial attribute allocations on failure and remove the
group before device deletion. Only notify provider removal after a
successful registration notification.
Preserve the existing exporter-module references and allow providers
to specify an explicit NULL owner when their operations are built in.
Providers remain responsible for unregistering before releasing
callback state.
Retain the firmware node explicitly and serialize consumer-reference
acquisition with disconnection. This lets MTD unregister an OTP provider
before freeing its backing object without requiring a release callback
into the departing provider module.
Cc: Srinivas Kandagatla <srini@xxxxxxxxxx>
Cc: Tze Yee Ng <tze.yee.ng@xxxxxxxxxx>
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/nvmem/Kconfig | 1 +
drivers/nvmem/core.c | 463 +++++++++++++++++++++++------------
drivers/nvmem/internals.h | 16 +-
drivers/nvmem/layouts.c | 2 +
drivers/nvmem/layouts/fixed-layout.c | 2 +-
drivers/nvmem/layouts/onie-tlv.c | 20 +-
drivers/nvmem/layouts/sl28vpd.c | 3 +-
drivers/nvmem/layouts/u-boot-env.c | 25 +-
include/linux/nvmem-provider.h | 16 +-
9 files changed, 373 insertions(+), 175 deletions(-)
diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
index c36c2a4c2a0b..550f3118071a 100644
--- a/drivers/nvmem/Kconfig
+++ b/drivers/nvmem/Kconfig
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0-only
menuconfig NVMEM
bool "NVMEM Support"
+ select SRCU
imply NVMEM_LAYOUTS
help
Support for NVMEM(Non Volatile Memory) devices like EEPROM, EFUSES...
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 0556d140170a..b71d80d140dd 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -13,6 +13,7 @@
#include <linux/idr.h>
#include <linux/init.h>
#include <linux/kref.h>
+#include <linux/lockdep.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/nvmem-consumer.h>
@@ -26,7 +27,11 @@
#define to_nvmem_device(d) container_of(d, struct nvmem_device, dev)
#define FLAG_COMPAT BIT(0)
+#define FLAG_REGISTERED BIT(1)
struct nvmem_cell_entry {
+ struct kref refcnt;
+ bool disconnected;
+ struct nvmem_layout *layout;
const char *name;
int offset;
size_t raw_len;
@@ -54,31 +59,30 @@ static LIST_HEAD(nvmem_lookup_list);
static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
+static void nvmem_cell_entry_put(struct nvmem_cell_entry *cell);
+
static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
- struct nvmem_operations *ops = nvmem->ops;
-
- if (!ops->reg_read)
+ if (!nvmem->reg_read)
return -EOPNOTSUPP;
- return ops->reg_read(nvmem->priv, offset, val, bytes);
+ return nvmem->reg_read(nvmem->priv, offset, val, bytes);
}
static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
- struct nvmem_operations *ops = nvmem->ops;
int ret, wr_ok;
- if (!ops->reg_write)
+ if (!nvmem->reg_write)
return -EOPNOTSUPP;
ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
if (ret)
return ret;
- wr_ok = ops->reg_write(nvmem->priv, offset, val, bytes);
+ wr_ok = nvmem->reg_write(nvmem->priv, offset, val, bytes);
ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
if (ret)
@@ -154,6 +158,10 @@ static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
static int nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
+ guard(srcu)(&nvmem->srcu);
+ if (READ_ONCE(nvmem->disconnected))
+ return -ENODEV;
+
if (!nvmem->nkeepout)
return __nvmem_reg_read(nvmem, offset, val, bytes);
@@ -163,6 +171,10 @@ 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)
{
+ guard(srcu)(&nvmem->srcu);
+ if (READ_ONCE(nvmem->disconnected))
+ return -ENODEV;
+
if (!nvmem->nkeepout)
return __nvmem_reg_write(nvmem, offset, val, bytes);
@@ -289,8 +301,6 @@ 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;
-
umode_t mode = 0400;
if (!nvmem->root_only)
@@ -299,10 +309,10 @@ static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
if (!nvmem->read_only)
mode |= 0200;
- if (!ops->reg_write)
+ if (!nvmem->reg_write)
mode &= ~0200;
- if (!ops->reg_read)
+ if (!nvmem->reg_read)
mode &= ~0444;
return mode;
@@ -333,7 +343,6 @@ 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;
/*
* If the device has no .reg_write operation, do not allow
@@ -342,7 +351,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 && !ops->reg_write)
+ if (attr == &dev_attr_force_ro.attr && !nvmem->reg_write)
return 0; /* Attribute not visible */
return attr->mode;
@@ -466,59 +475,102 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
}
-static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
+static void nvmem_sysfs_free_cell_attrs(const struct bin_attribute * const *attrs)
{
- 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;
+ if (!attrs)
+ return;
- guard(mutex)(&nvmem_mutex);
+ for (int i = 0; attrs[i]; i++) {
+ nvmem_cell_entry_put(attrs[i]->private);
+ kfree(attrs[i]->attr.name);
+ kfree(attrs[i]);
+ }
+ kfree(attrs);
+}
- if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated)
- return 0;
+static void __nvmem_sysfs_remove_cells(struct nvmem_device *nvmem)
+{
+ lockdep_assert_held(&nvmem->sysfs_mutex);
- /* Allocate an array of attributes with a sentinel */
- ncells = list_count_nodes(&nvmem->cells);
- pattrs = devm_kcalloc(&nvmem->dev, ncells + 1,
- sizeof(struct bin_attribute *), GFP_KERNEL);
- if (!pattrs)
- return -ENOMEM;
+ if (!nvmem->sysfs_cells_populated)
+ return;
- attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
- if (!attrs)
- return -ENOMEM;
+ device_remove_group(&nvmem->dev, &nvmem->cells_group);
+ nvmem_sysfs_free_cell_attrs(nvmem->cells_group.bin_attrs);
+ nvmem->cells_group.bin_attrs = NULL;
+ nvmem->sysfs_cells_populated = false;
+}
+
+static void nvmem_sysfs_remove_cells(struct nvmem_device *nvmem)
+{
+ guard(mutex)(&nvmem->sysfs_mutex);
+ __nvmem_sysfs_remove_cells(nvmem);
+}
+
+static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
+{
+ struct nvmem_cell_entry *entry;
+ const struct bin_attribute **attrs = NULL;
+ unsigned int i = 0;
+ int ret = 0;
- /* 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.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)
+ guard(mutex)(&nvmem->sysfs_mutex);
+
+ __nvmem_sysfs_remove_cells(nvmem);
+
+ scoped_guard(mutex, &nvmem_mutex) {
+ if (nvmem->disconnected)
+ return -ENODEV;
+ if (list_empty(&nvmem->cells))
+ return 0;
+
+ attrs = kcalloc(list_count_nodes(&nvmem->cells) + 1,
+ sizeof(*attrs), GFP_KERNEL);
+ if (!attrs)
return -ENOMEM;
- pattrs[i] = &attrs[i];
- i++;
+ list_for_each_entry(entry, &nvmem->cells, node) {
+ struct bin_attribute *attr;
+
+ attr = kzalloc_obj(*attr);
+ if (!attr) {
+ ret = -ENOMEM;
+ break;
+ }
+ sysfs_bin_attr_init(attr);
+ attr->attr.name = kasprintf(GFP_KERNEL, "%s@%x,%x",
+ entry->name, entry->offset,
+ entry->bit_offset);
+ if (!attr->attr.name) {
+ kfree(attr);
+ ret = -ENOMEM;
+ break;
+ }
+ attr->attr.mode = 0444 & nvmem_bin_attr_get_umode(nvmem);
+ attr->size = entry->bytes;
+ attr->read = nvmem_cell_attr_read;
+ attr->private = entry;
+ kref_get(&entry->refcnt);
+ attrs[i++] = attr;
+ }
}
+ if (ret)
+ goto err_free;
- group.bin_attrs = pattrs;
+ nvmem->cells_group.name = "cells";
+ nvmem->cells_group.bin_attrs = attrs;
- ret = device_add_group(&nvmem->dev, &group);
- if (ret)
- return ret;
+ ret = device_add_group(&nvmem->dev, &nvmem->cells_group);
+ if (ret) {
+ nvmem->cells_group.bin_attrs = NULL;
+ goto err_free;
+ }
nvmem->sysfs_cells_populated = true;
+ return 0;
+err_free:
+ nvmem_sysfs_free_cell_attrs(attrs);
return ret;
}
@@ -533,6 +585,10 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
{
}
+static void nvmem_sysfs_remove_cells(struct nvmem_device *nvmem)
+{
+}
+
#endif /* CONFIG_NVMEM_SYSFS */
static void nvmem_release(struct device *dev)
@@ -540,8 +596,8 @@ static void nvmem_release(struct device *dev)
struct nvmem_device *nvmem = to_nvmem_device(dev);
ida_free(&nvmem_ida, nvmem->id);
- gpiod_put(nvmem->wp_gpio);
- kfree(nvmem->ops);
+ of_node_put(nvmem->dev.of_node);
+ cleanup_srcu_struct(&nvmem->srcu);
kfree(nvmem);
}
@@ -553,22 +609,30 @@ static const struct bus_type nvmem_bus_type = {
.name = "nvmem",
};
-static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
+static void nvmem_cell_entry_release(struct kref *ref)
{
- blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
- scoped_guard(mutex, &nvmem_mutex)
- list_del(&cell->node);
+ struct nvmem_cell_entry *cell = container_of(ref, struct nvmem_cell_entry, refcnt);
+
of_node_put(cell->np);
kfree_const(cell->name);
kfree(cell);
}
+static void nvmem_cell_entry_put(struct nvmem_cell_entry *cell)
+{
+ kref_put(&cell->refcnt, nvmem_cell_entry_release);
+}
+
static void nvmem_device_remove_all_cells(const struct nvmem_device *nvmem)
{
struct nvmem_cell_entry *cell, *p;
- list_for_each_entry_safe(cell, p, &nvmem->cells, node)
- nvmem_cell_entry_drop(cell);
+ list_for_each_entry_safe(cell, p, &nvmem->cells, node) {
+ blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
+ scoped_guard(mutex, &nvmem_mutex)
+ list_del(&cell->node);
+ nvmem_cell_entry_put(cell);
+ }
}
static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
@@ -583,6 +647,7 @@ static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
struct nvmem_cell_entry *cell)
{
cell->nvmem = nvmem;
+ cell->disconnected = false;
cell->offset = info->offset;
cell->raw_len = info->raw_len ?: info->bytes;
cell->bytes = info->bytes;
@@ -639,20 +704,17 @@ static int nvmem_cell_info_to_nvmem_cell_entry(struct nvmem_device *nvmem,
return 0;
}
-/**
- * nvmem_add_one_cell() - Add one cell information to an nvmem device
- *
- * @nvmem: nvmem device to add cells to.
- * @info: nvmem cell info to add to the device
- *
- * Return: 0 or negative error code on failure.
- */
-int nvmem_add_one_cell(struct nvmem_device *nvmem,
- const struct nvmem_cell_info *info)
+static int __nvmem_add_one_cell(struct nvmem_device *nvmem,
+ const struct nvmem_cell_info *info,
+ struct nvmem_layout *layout)
{
struct nvmem_cell_entry *cell;
int rval;
+ guard(srcu)(&nvmem->srcu);
+ if (READ_ONCE(nvmem->disconnected))
+ return -ENODEV;
+
cell = kzalloc_obj(*cell);
if (!cell)
return -ENOMEM;
@@ -663,12 +725,45 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem,
return rval;
}
+ kref_init(&cell->refcnt);
+ cell->layout = layout;
nvmem_cell_entry_add(cell);
return 0;
}
+
+/**
+ * nvmem_add_one_cell() - Add one cell information to an nvmem device
+ *
+ * @nvmem: nvmem device to add cells to.
+ * @info: nvmem cell info to add to the device
+ *
+ * Return: 0 or negative error code on failure.
+ */
+int nvmem_add_one_cell(struct nvmem_device *nvmem,
+ const struct nvmem_cell_info *info)
+{
+ return __nvmem_add_one_cell(nvmem, info, NULL);
+}
EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
+/**
+ * nvmem_layout_add_cell() - Add a cell owned by an NVMEM layout
+ * @layout: Layout registering the cell from its add_cells callback.
+ * @info: Cell description.
+ *
+ * The cell and its post-processing callback are disconnected when the layout
+ * is unregistered. Provider cells added with nvmem_add_one_cell() are retained.
+ *
+ * Return: 0 or a negative error code.
+ */
+int nvmem_layout_add_cell(struct nvmem_layout *layout,
+ const struct nvmem_cell_info *info)
+{
+ return __nvmem_add_one_cell(layout->nvmem, info, layout);
+}
+EXPORT_SYMBOL_GPL(nvmem_layout_add_cell);
+
/**
* nvmem_add_cells() - Add cell information to an nvmem device
*
@@ -729,6 +824,7 @@ nvmem_find_cell_entry_by_name(struct nvmem_device *nvmem, const char *cell_id)
list_for_each_entry(iter, &nvmem->cells, node) {
if (strcmp(cell_id, iter->name) == 0) {
cell = iter;
+ kref_get(&cell->refcnt);
break;
}
}
@@ -789,12 +885,17 @@ static int nvmem_validate_keepouts(struct nvmem_device *nvmem)
return 0;
}
-int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
+static int __nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np,
+ struct nvmem_layout *layout)
{
struct device *dev = &nvmem->dev;
const __be32 *addr;
int len, ret;
+ guard(srcu)(&nvmem->srcu);
+ if (READ_ONCE(nvmem->disconnected))
+ return -ENODEV;
+
for_each_child_of_node_scoped(np, child) {
struct nvmem_cell_info info = {0};
@@ -827,7 +928,7 @@ int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
if (nvmem->fixup_dt_cell_info)
nvmem->fixup_dt_cell_info(nvmem, &info);
- ret = nvmem_add_one_cell(nvmem, &info);
+ ret = __nvmem_add_one_cell(nvmem, &info, layout);
kfree(info.name);
if (ret) {
of_node_put(info.np);
@@ -837,8 +938,19 @@ int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
return 0;
}
+
+int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
+{
+ return __nvmem_add_cells_from_dt(nvmem, np, NULL);
+}
EXPORT_SYMBOL_GPL(nvmem_add_cells_from_dt);
+int nvmem_layout_add_cells_from_dt(struct nvmem_layout *layout, struct device_node *np)
+{
+ return __nvmem_add_cells_from_dt(layout->nvmem, np, layout);
+}
+EXPORT_SYMBOL_GPL(nvmem_layout_add_cells_from_dt);
+
static int nvmem_add_cells_from_legacy_of(struct nvmem_device *nvmem)
{
return nvmem_add_cells_from_dt(nvmem, nvmem->dev.of_node);
@@ -851,29 +963,68 @@ 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 (READ_ONCE(layout->nvmem->disconnected))
+ return -ENODEV;
+ ret = layout->add_cells(layout);
+ }
if (ret)
- return ret;
+ goto err_unregister;
#ifdef CONFIG_NVMEM_SYSFS
ret = nvmem_populate_sysfs_cells(layout->nvmem);
- if (ret) {
- nvmem_device_remove_all_cells(layout->nvmem);
- return ret;
- }
+ if (ret)
+ goto err_unregister;
#endif
return 0;
+
+err_unregister:
+ nvmem_layout_unregister(layout);
+ return ret;
}
EXPORT_SYMBOL_GPL(nvmem_layout_register);
void nvmem_layout_unregister(struct nvmem_layout *layout)
{
- /* Keep the API even with an empty stub in case we need it later */
+ struct nvmem_device *nvmem = layout->nvmem;
+ struct nvmem_cell_entry *cell, *next;
+ LIST_HEAD(cells);
+
+ scoped_guard(mutex, &nvmem_mutex) {
+ list_for_each_entry_safe(cell, next, &nvmem->cells, node) {
+ if (cell->layout != layout)
+ continue;
+ WRITE_ONCE(cell->disconnected, true);
+ list_move_tail(&cell->node, &cells);
+ }
+ }
+
+ nvmem_sysfs_remove_cells(nvmem);
+
+ /* Old cell handles retain data, never a departing layout's callbacks. */
+ synchronize_srcu(&nvmem->srcu);
+ list_for_each_entry_safe(cell, next, &cells, node) {
+ blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
+ list_del(&cell->node);
+ nvmem_cell_entry_put(cell);
+ }
+#ifdef CONFIG_NVMEM_SYSFS
+ if (!READ_ONCE(nvmem->disconnected) && nvmem_populate_sysfs_cells(nvmem))
+ dev_warn(&nvmem->dev, "Failed to restore provider cell attributes\n");
+#endif
}
EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
+static void nvmem_disconnect(struct nvmem_device *nvmem)
+{
+ scoped_guard(mutex, &nvmem_mutex)
+ WRITE_ONCE(nvmem->disconnected, true);
+ synchronize_srcu(&nvmem->srcu);
+ gpiod_put(nvmem->wp_gpio);
+ nvmem->wp_gpio = NULL;
+}
+
/**
* nvmem_register() - Register a nvmem device for given nvmem_config.
* Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
@@ -883,10 +1034,8 @@ EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
* Return: Will be an ERR_PTR() on error or a valid pointer to nvmem_device
* on success.
*/
-
struct nvmem_device *nvmem_register(const struct nvmem_config *config)
{
- struct nvmem_operations *ops;
struct nvmem_device *nvmem;
int rval;
@@ -900,15 +1049,15 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
if (!nvmem)
return ERR_PTR(-ENOMEM);
- ops = kzalloc_obj(*ops);
- if (!ops) {
+ rval = init_srcu_struct(&nvmem->srcu);
+ if (rval) {
kfree(nvmem);
- return ERR_PTR(-ENOMEM);
+ return ERR_PTR(rval);
}
rval = ida_alloc(&nvmem_ida, GFP_KERNEL);
if (rval < 0) {
- kfree(ops);
+ cleanup_srcu_struct(&nvmem->srcu);
kfree(nvmem);
return ERR_PTR(rval);
}
@@ -918,9 +1067,11 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem->dev.type = &nvmem_provider_type;
nvmem->dev.bus = &nvmem_bus_type;
nvmem->dev.parent = config->dev;
- nvmem->ops = ops;
device_initialize(&nvmem->dev);
+ mutex_init(&nvmem->sysfs_mutex);
+
+ nvmem->priv = config->priv;
if (!config->ignore_wp)
nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
@@ -935,24 +1086,23 @@ 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;
- ops->reg_read = config->reg_read;
- ops->reg_write = config->reg_write;
+ nvmem->reg_read = config->reg_read;
+ nvmem->reg_write = config->reg_write;
nvmem->owner = config->owner;
- if (!nvmem->owner && config->dev->driver)
+ if (!nvmem->owner && !config->owner_is_explicit && config->dev->driver)
nvmem->owner = config->dev->driver->owner;
nvmem->stride = config->stride ?: 1;
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->keepout = config->keepout;
nvmem->nkeepout = config->nkeepout;
if (config->of_node)
- nvmem->dev.of_node = config->of_node;
+ nvmem->dev.of_node = of_node_get(config->of_node);
else
- nvmem->dev.of_node = config->dev->of_node;
+ nvmem->dev.of_node = of_node_get(config->dev->of_node);
switch (config->id) {
case NVMEM_DEVID_NONE:
@@ -972,7 +1122,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 || !ops->reg_write;
+ config->read_only || !nvmem->reg_write;
#ifdef CONFIG_NVMEM_SYSFS
nvmem->dev.groups = nvmem_dev_groups;
@@ -1010,7 +1160,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
rval = nvmem_populate_layout(nvmem);
if (rval)
- goto err_remove_dev;
+ goto err_unregister;
/* If the device has WP GPIO, default to read-only */
if (nvmem->wp_gpio)
@@ -1019,23 +1169,25 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
#ifdef CONFIG_NVMEM_SYSFS
rval = nvmem_populate_sysfs_cells(nvmem);
if (rval)
- goto err_destroy_layout;
+ goto err_unregister;
#endif
+ nvmem->flags |= FLAG_REGISTERED;
blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
return nvmem;
-#ifdef CONFIG_NVMEM_SYSFS
-err_destroy_layout:
- nvmem_destroy_layout(nvmem);
-#endif
-err_remove_dev:
- device_del(&nvmem->dev);
+err_unregister:
+ nvmem_unregister(nvmem);
+ return ERR_PTR(rval);
err_remove_cells:
+ nvmem_disconnect(nvmem);
nvmem_device_remove_all_cells(nvmem);
nvmem_sysfs_remove_compat(nvmem);
+ goto err_put_disconnected;
err_put_device:
+ nvmem_disconnect(nvmem);
+err_put_disconnected:
put_device(&nvmem->dev);
return ERR_PTR(rval);
@@ -1048,24 +1200,31 @@ static void nvmem_device_release(struct kref *kref)
nvmem = container_of(kref, struct nvmem_device, refcnt);
- blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
-
- nvmem_sysfs_remove_compat(nvmem);
-
- nvmem_device_remove_all_cells(nvmem);
- nvmem_destroy_layout(nvmem);
- device_unregister(&nvmem->dev);
+ put_device(&nvmem->dev);
}
/**
* nvmem_unregister() - Unregister previously registered nvmem device
*
* @nvmem: Pointer to previously registered nvmem device.
+ *
+ * Stop and drain provider callbacks before returning. Existing consumers keep
+ * their core-owned metadata, but further access returns -ENODEV.
*/
void nvmem_unregister(struct nvmem_device *nvmem)
{
- if (nvmem)
- kref_put(&nvmem->refcnt, nvmem_device_release);
+ if (!nvmem)
+ return;
+
+ nvmem_disconnect(nvmem);
+ nvmem_destroy_layout(nvmem);
+ if (nvmem->flags & FLAG_REGISTERED)
+ blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
+ nvmem_sysfs_remove_compat(nvmem);
+ nvmem_sysfs_remove_cells(nvmem);
+ device_del(&nvmem->dev);
+ nvmem_device_remove_all_cells(nvmem);
+ kref_put(&nvmem->refcnt, nvmem_device_release);
}
EXPORT_SYMBOL_GPL(nvmem_unregister);
@@ -1103,17 +1262,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 (READ_ONCE(nvmem->disconnected))
+ 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);
@@ -1121,8 +1296,7 @@ static struct nvmem_device *nvmem_device_match(void *data,
dev_err(&nvmem->dev,
"could not increase module refcount for cell %s\n",
nvmem_dev_name(nvmem));
-
- put_device(&nvmem->dev);
+ put_device(dev);
return ERR_PTR(-EINVAL);
}
@@ -1337,8 +1511,10 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
cell = ERR_PTR(-ENOENT);
} else {
cell = nvmem_create_cell(cell_entry, con_id, 0);
- if (IS_ERR(cell))
+ if (IS_ERR(cell)) {
+ nvmem_cell_entry_put(cell_entry);
nvmem_device_put(nvmem);
+ }
}
break;
}
@@ -1347,12 +1523,6 @@ nvmem_cell_get_from_lookup(struct device *dev, const char *con_id)
return cell;
}
-static void nvmem_layout_module_put(struct nvmem_device *nvmem)
-{
- if (nvmem->layout && nvmem->layout->dev.driver)
- module_put(nvmem->layout->dev.driver->owner);
-}
-
#if IS_ENABLED(CONFIG_OF)
static struct nvmem_cell_entry *
nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np)
@@ -1362,25 +1532,15 @@ nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np
guard(mutex)(&nvmem_mutex);
list_for_each_entry(cell, &nvmem->cells, node) {
- if (np == cell->np)
+ if (np == cell->np) {
+ kref_get(&cell->refcnt);
return cell;
+ }
}
return NULL;
}
-static int nvmem_layout_module_get_optional(struct nvmem_device *nvmem)
-{
- if (!nvmem->layout)
- return 0;
-
- if (!nvmem->layout->dev.driver ||
- !try_module_get(nvmem->layout->dev.driver->owner))
- return -EPROBE_DEFER;
-
- return 0;
-}
-
/**
* of_nvmem_cell_get() - Get a nvmem cell from given device node and cell id
*
@@ -1443,25 +1603,17 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
return ERR_CAST(nvmem);
}
- ret = nvmem_layout_module_get_optional(nvmem);
- if (ret) {
- of_node_put(cell_np);
- nvmem_device_put(nvmem);
- return ERR_PTR(ret);
- }
-
cell_entry = nvmem_find_cell_entry_by_node(nvmem, cell_np);
of_node_put(cell_np);
if (!cell_entry) {
- nvmem_layout_module_put(nvmem);
- ret = nvmem->layout ? -EPROBE_DEFER : -ENOENT;
+ ret = READ_ONCE(nvmem->layout) ? -EPROBE_DEFER : -ENOENT;
nvmem_device_put(nvmem);
return ERR_PTR(ret);
}
cell = nvmem_create_cell(cell_entry, id, cell_index);
if (IS_ERR(cell)) {
- nvmem_layout_module_put(nvmem);
+ nvmem_cell_entry_put(cell_entry);
nvmem_device_put(nvmem);
}
@@ -1575,8 +1727,8 @@ void nvmem_cell_put(struct nvmem_cell *cell)
if (cell->id)
kfree_const(cell->id);
+ nvmem_cell_entry_put(cell->entry);
kfree(cell);
- nvmem_layout_module_put(nvmem);
nvmem_device_put(nvmem);
}
EXPORT_SYMBOL_GPL(nvmem_cell_put);
@@ -1628,6 +1780,10 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
{
int rc;
+ guard(srcu)(&nvmem->srcu);
+ if (READ_ONCE(nvmem->disconnected) || READ_ONCE(cell->disconnected))
+ return -ENODEV;
+
rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
if (rc)
@@ -1742,7 +1898,14 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si
struct nvmem_device *nvmem = cell->nvmem;
int rc;
- if (!nvmem || nvmem->read_only ||
+ if (!nvmem)
+ return -EINVAL;
+
+ guard(srcu)(&nvmem->srcu);
+ if (READ_ONCE(nvmem->disconnected) || READ_ONCE(cell->disconnected))
+ return -ENODEV;
+
+ if (nvmem->read_only ||
(cell->bit_offset == 0 && len != cell->bytes))
return -EINVAL;
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index 4e610deeaa7b..6c6d4a690e8f 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -4,14 +4,10 @@
#define _LINUX_NVMEM_INTERNALS_H
#include <linux/device.h>
+#include <linux/mutex.h>
#include <linux/nvmem-consumer.h>
#include <linux/nvmem-provider.h>
-
-/* Hold pointers to callbacks owned by the nvmem provider module. */
-struct nvmem_operations {
- nvmem_reg_read_t reg_read;
- nvmem_reg_write_t reg_write;
-};
+#include <linux/srcu.h>
struct nvmem_device {
struct module *owner;
@@ -20,6 +16,8 @@ struct nvmem_device {
int word_size;
int id;
struct kref refcnt;
+ struct srcu_struct srcu;
+ bool disconnected;
size_t size;
bool read_only;
bool root_only;
@@ -34,12 +32,16 @@ struct nvmem_device {
unsigned int nkeepout;
struct gpio_desc *wp_gpio;
struct nvmem_layout *layout;
- struct nvmem_operations *ops;
+ nvmem_reg_read_t reg_read;
+ nvmem_reg_write_t reg_write;
void *priv;
+ struct mutex sysfs_mutex;
bool sysfs_cells_populated;
+ struct attribute_group cells_group;
};
int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np);
+int nvmem_layout_add_cells_from_dt(struct nvmem_layout *layout, struct device_node *np);
#if IS_ENABLED(CONFIG_OF)
int nvmem_layout_bus_register(void);
diff --git a/drivers/nvmem/layouts.c b/drivers/nvmem/layouts.c
index 07a34be9669c..32995c9be68d 100644
--- a/drivers/nvmem/layouts.c
+++ b/drivers/nvmem/layouts.c
@@ -118,6 +118,7 @@ static int nvmem_layout_create_device(struct nvmem_device *nvmem,
ret = device_add(dev);
if (ret) {
+ nvmem->layout = NULL;
put_device(dev);
return ret;
}
@@ -192,6 +193,7 @@ void nvmem_destroy_layout(struct nvmem_device *nvmem)
dev = &nvmem->layout->dev;
of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
device_unregister(dev);
+ WRITE_ONCE(nvmem->layout, NULL);
}
int nvmem_layout_bus_register(void)
diff --git a/drivers/nvmem/layouts/fixed-layout.c b/drivers/nvmem/layouts/fixed-layout.c
index 635d448b3dd2..b97f39897a75 100644
--- a/drivers/nvmem/layouts/fixed-layout.c
+++ b/drivers/nvmem/layouts/fixed-layout.c
@@ -19,7 +19,7 @@ static int fixed_layout_add_cells(struct nvmem_layout *layout)
if (!np)
return -ENOENT;
- ret = nvmem_add_cells_from_dt(layout->nvmem, np);
+ ret = nvmem_layout_add_cells_from_dt(layout, np);
of_node_put(np);
return ret;
diff --git a/drivers/nvmem/layouts/onie-tlv.c b/drivers/nvmem/layouts/onie-tlv.c
index 8b0f3c1b8a0e..99e7a5406b4c 100644
--- a/drivers/nvmem/layouts/onie-tlv.c
+++ b/drivers/nvmem/layouts/onie-tlv.c
@@ -95,18 +95,19 @@ static nvmem_cell_post_process_t onie_tlv_read_cb(u8 type, u8 *buf)
return NULL;
}
-static int onie_tlv_add_cells(struct device *dev, struct nvmem_device *nvmem,
+static int onie_tlv_add_cells(struct nvmem_layout *layout,
size_t data_len, u8 *data)
{
+ struct device *dev = &layout->dev;
struct nvmem_cell_info cell = {};
- struct device_node *layout;
+ struct device_node *layout_np;
struct onie_tlv tlv;
unsigned int hdr_len = sizeof(struct onie_tlv_hdr);
unsigned int offset = 0;
int ret;
- layout = of_nvmem_layout_get_container(nvmem);
- if (!layout)
+ layout_np = of_nvmem_layout_get_container(layout->nvmem);
+ if (!layout_np)
return -ENOENT;
while (offset < data_len) {
@@ -123,12 +124,13 @@ static int onie_tlv_add_cells(struct device *dev, struct nvmem_device *nvmem,
cell.offset = hdr_len + offset + sizeof(tlv.type) + sizeof(tlv.len);
cell.bytes = tlv.len;
- cell.np = of_get_child_by_name(layout, cell.name);
+ cell.np = of_get_child_by_name(layout_np, cell.name);
cell.read_post_process = onie_tlv_read_cb(tlv.type, data + offset + sizeof(tlv));
- ret = nvmem_add_one_cell(nvmem, &cell);
+ ret = nvmem_layout_add_cell(layout, &cell);
if (ret) {
- of_node_put(layout);
+ of_node_put(cell.np);
+ of_node_put(layout_np);
return ret;
}
@@ -136,7 +138,7 @@ static int onie_tlv_add_cells(struct device *dev, struct nvmem_device *nvmem,
offset += sizeof(tlv) + tlv.len;
}
- of_node_put(layout);
+ of_node_put(layout_np);
return 0;
}
@@ -221,7 +223,7 @@ static int onie_tlv_parse_table(struct nvmem_layout *layout)
return -EINVAL;
data = table + hdr_len;
- ret = onie_tlv_add_cells(dev, nvmem, data_len, data);
+ ret = onie_tlv_add_cells(layout, data_len, data);
if (ret)
return ret;
diff --git a/drivers/nvmem/layouts/sl28vpd.c b/drivers/nvmem/layouts/sl28vpd.c
index e93b020b0836..1e65a0b6c61f 100644
--- a/drivers/nvmem/layouts/sl28vpd.c
+++ b/drivers/nvmem/layouts/sl28vpd.c
@@ -124,8 +124,9 @@ static int sl28vpd_add_cells(struct nvmem_layout *layout)
info.read_post_process = pinfo->read_post_process;
info.np = of_get_child_by_name(layout_np, pinfo->name);
- ret = nvmem_add_one_cell(nvmem, &info);
+ ret = nvmem_layout_add_cell(layout, &info);
if (ret) {
+ of_node_put(info.np);
of_node_put(layout_np);
return ret;
}
diff --git a/drivers/nvmem/layouts/u-boot-env.c b/drivers/nvmem/layouts/u-boot-env.c
index 33ec2350386f..83c687e43e5a 100644
--- a/drivers/nvmem/layouts/u-boot-env.c
+++ b/drivers/nvmem/layouts/u-boot-env.c
@@ -49,11 +49,13 @@ static int u_boot_env_read_post_process_ethaddr(void *context, const char *id, i
return 0;
}
-static int u_boot_env_parse_cells(struct device *dev, struct nvmem_device *nvmem, uint8_t *buf,
+static int u_boot_env_parse_cells(struct device *dev, struct nvmem_device *nvmem,
+ struct nvmem_layout *layout, uint8_t *buf,
size_t data_offset, size_t data_len)
{
char *data = buf + data_offset;
char *var, *value, *eq;
+ int ret;
for (var = data;
var < data + data_len && *var;
@@ -78,14 +80,19 @@ static int u_boot_env_parse_cells(struct device *dev, struct nvmem_device *nvmem
info.read_post_process = u_boot_env_read_post_process_ethaddr;
}
- nvmem_add_one_cell(nvmem, &info);
+ ret = layout ? nvmem_layout_add_cell(layout, &info) :
+ nvmem_add_one_cell(nvmem, &info);
+ if (ret) {
+ of_node_put(info.np);
+ return ret;
+ }
}
return 0;
}
-int u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem,
- enum u_boot_env_format format)
+static int __u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem,
+ struct nvmem_layout *layout, enum u_boot_env_format format)
{
size_t crc32_data_offset;
size_t crc32_data_len;
@@ -156,13 +163,19 @@ int u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem,
}
buf[dev_size - 1] = '\0';
- err = u_boot_env_parse_cells(dev, nvmem, buf, data_offset, data_len);
+ err = u_boot_env_parse_cells(dev, nvmem, layout, buf, data_offset, data_len);
err_kfree:
kfree(buf);
err_out:
return err;
}
+
+int u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem,
+ enum u_boot_env_format format)
+{
+ return __u_boot_env_parse(dev, nvmem, NULL, format);
+}
EXPORT_SYMBOL_GPL(u_boot_env_parse);
static int u_boot_env_add_cells(struct nvmem_layout *layout)
@@ -172,7 +185,7 @@ static int u_boot_env_add_cells(struct nvmem_layout *layout)
format = (uintptr_t)device_get_match_data(dev);
- return u_boot_env_parse(dev, layout->nvmem, format);
+ return __u_boot_env_parse(dev, layout->nvmem, layout, format);
}
static int u_boot_env_probe(struct nvmem_layout *layout)
diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
index 6063fe5b7784..87d871a6d5c6 100644
--- a/include/linux/nvmem-provider.h
+++ b/include/linux/nvmem-provider.h
@@ -81,6 +81,8 @@ struct nvmem_cell_info {
* @name: Optional name.
* @id: Optional device ID used in full name. Ignored if name is NULL.
* @owner: Pointer to exporter module. Used for refcounting.
+ * @owner_is_explicit: Use @owner even when NULL, without defaulting to the
+ * parent driver's module.
* @cells: Optional array of pre-defined NVMEM cells.
* @ncells: Number of elements in cells.
* @add_legacy_fixed_of_cells: Read fixed NVMEM cells from old OF syntax.
@@ -107,12 +109,16 @@ struct nvmem_cell_info {
*
* Note: Specifying name and setting id to -1 implies a unique device
* whose name is provided as-is (kept unaltered).
+ *
+ * Provider callbacks and @priv must remain valid until nvmem_unregister()
+ * returns, or until a failed nvmem_register() returns.
*/
struct nvmem_config {
struct device *dev;
const char *name;
int id;
struct module *owner;
+ bool owner_is_explicit;
const struct nvmem_cell_info *cells;
int ncells;
bool add_legacy_fixed_of_cells;
@@ -144,7 +150,7 @@ struct nvmem_config {
* @nvmem: The underlying NVMEM device
* @add_cells: Will be called if a nvmem device is found which
* has this layout. The function will add layout
- * specific cells with nvmem_add_one_cell().
+ * specific cells with nvmem_layout_add_cell().
*
* A nvmem device can hold a well defined structure which can just be
* evaluated during runtime. For example a TLV list, or a list of "name=val"
@@ -173,6 +179,8 @@ struct nvmem_device *devm_nvmem_register(struct device *dev,
int nvmem_add_one_cell(struct nvmem_device *nvmem,
const struct nvmem_cell_info *info);
+int nvmem_layout_add_cell(struct nvmem_layout *layout,
+ const struct nvmem_cell_info *info);
int nvmem_layout_register(struct nvmem_layout *layout);
void nvmem_layout_unregister(struct nvmem_layout *layout);
@@ -212,6 +220,12 @@ static inline int nvmem_layout_register(struct nvmem_layout *layout)
return -EOPNOTSUPP;
}
+static inline int nvmem_layout_add_cell(struct nvmem_layout *layout,
+ const struct nvmem_cell_info *info)
+{
+ return -EOPNOTSUPP;
+}
+
static inline void nvmem_layout_unregister(struct nvmem_layout *layout) {}
static inline int nvmem_add_cells_from_dt(struct nvmem_device *nvmem,
--
2.53.0