* [PATCH v2 01/11] nvmem: retain the provider node until device release
2026-11-19 16:41 [PATCH v2 00/11] nvmem: complete provider and layout disconnection James Hilliard
@ 2026-11-19 16:41 ` James Hilliard
2026-11-19 16:42 ` [PATCH v2 02/11] nvmem: drain keepout and cell post-processing accesses James Hilliard
` (9 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-11-19 16:41 UTC (permalink / raw)
To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
Rafał Miłecki, Michael Walle, Linus Walleij,
Khouloud Touil, Bartosz Golaszewski
Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
Paul Cercueil, linux-kernel, linux-mtd, James Hilliard
An NVMEM device can outlive its provider after unregister. Take a
reference to the configured OF node, or reuse the provider device's node
when none is specified. Drop the reference in the device release path
so the node remains valid for the lifetime of the NVMEM device.
Fixes: 1333a6779501 ("nvmem: core: allow specifying of_node")
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/nvmem/core.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 7d4cce8e2742..03ea878874e4 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -570,6 +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);
+ of_node_put(nvmem->dev.of_node);
cleanup_srcu_struct(&nvmem->srcu);
kfree(nvmem);
}
@@ -973,9 +974,9 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
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;
+ device_set_of_node_from_dev(&nvmem->dev, config->dev);
switch (config->id) {
case NVMEM_DEVID_NONE:
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v2 02/11] nvmem: drain keepout and cell post-processing accesses
2026-11-19 16:41 [PATCH v2 00/11] nvmem: complete provider and layout disconnection James Hilliard
2026-11-19 16:41 ` [PATCH v2 01/11] nvmem: retain the provider node until device release James Hilliard
@ 2026-11-19 16:42 ` James Hilliard
2026-11-19 16:42 ` [PATCH v2 03/11] nvmem: exclude disconnected providers from callback admission James Hilliard
` (8 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-11-19 16:42 UTC (permalink / raw)
To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
Rafał Miłecki, Michael Walle, Linus Walleij,
Khouloud Touil, Bartosz Golaszewski
Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
Paul Cercueil, linux-kernel, linux-mtd, James Hilliard
A provider can unregister while a read is still walking its keepout table
or running a cell's post-processing callback. Waiting for the register
callback alone does not keep that provider state alive.
Take the SRCU read lock once for each register or cell access. Use the
same operations pointer for every chunk, including the reads needed for
a partial-byte write. Unregister then waits for the entire operation;
new accesses fail even if they would only touch a keepout.
Fixes: 6a7b4134fb7a ("nvmem: protect nvmem_device::ops with SRCU")
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/nvmem/core.c | 110 ++++++++++++++++++++++++++-------------------------
1 file changed, 56 insertions(+), 54 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 03ea878874e4..32d36ca904cd 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -54,32 +54,18 @@ static LIST_HEAD(nvmem_lookup_list);
static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
-static int __nvmem_reg_read(struct nvmem_device *nvmem, unsigned int offset,
- void *val, size_t bytes)
+static int nvmem_access_chunk(struct nvmem_device *nvmem,
+ const struct nvmem_operations *ops,
+ unsigned int offset, void *val, size_t bytes, bool write)
{
- 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;
-
- return ops->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;
int ret, wr_ok;
- guard(srcu)(&nvmem->srcu);
- ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
- if (!ops)
- return -ENODEV;
+ if (!write) {
+ if (!ops->reg_read)
+ return -EOPNOTSUPP;
+
+ return ops->reg_read(nvmem->priv, offset, val, bytes);
+ }
if (!ops->reg_write)
return -EOPNOTSUPP;
@@ -97,17 +83,20 @@ static int __nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
return wr_ok;
}
-static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
- unsigned int offset, void *val,
- size_t bytes, int write)
+static int nvmem_access_locked(struct nvmem_device *nvmem,
+ const struct nvmem_operations *ops,
+ unsigned int offset, void *val, size_t bytes, bool write)
{
-
unsigned int end = offset + bytes;
unsigned int kend, ksize;
const struct nvmem_keepout *keepout = nvmem->keepout;
- const struct nvmem_keepout *keepoutend = keepout + nvmem->nkeepout;
+ const struct nvmem_keepout *keepoutend;
int rc;
+ if (!nvmem->nkeepout)
+ return nvmem_access_chunk(nvmem, ops, offset, val, bytes, write);
+
+ keepoutend = keepout + nvmem->nkeepout;
/*
* Skip all keepouts before the range being accessed.
* Keepouts are sorted.
@@ -120,11 +109,7 @@ static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
if (offset < keepout->start) {
kend = min(end, keepout->start);
ksize = kend - offset;
- if (write)
- rc = __nvmem_reg_write(nvmem, offset, val, ksize);
- else
- rc = __nvmem_reg_read(nvmem, offset, val, ksize);
-
+ rc = nvmem_access_chunk(nvmem, ops, offset, val, ksize, write);
if (rc)
return rc;
@@ -150,13 +135,8 @@ static int nvmem_access_with_keepouts(struct nvmem_device *nvmem,
* If we ran out of keepouts but there's still stuff to do, send it
* down directly
*/
- if (offset < end) {
- ksize = end - offset;
- if (write)
- return __nvmem_reg_write(nvmem, offset, val, ksize);
- else
- return __nvmem_reg_read(nvmem, offset, val, ksize);
- }
+ if (offset < end)
+ return nvmem_access_chunk(nvmem, ops, offset, val, end - offset, write);
return 0;
}
@@ -164,19 +144,27 @@ 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)
{
- if (!nvmem->nkeepout)
- return __nvmem_reg_read(nvmem, offset, val, bytes);
+ const struct nvmem_operations *ops;
+
+ guard(srcu)(&nvmem->srcu);
+ ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+ if (!ops)
+ return -ENODEV;
- return nvmem_access_with_keepouts(nvmem, offset, val, bytes, false);
+ return nvmem_access_locked(nvmem, ops, offset, val, bytes, false);
}
static int nvmem_reg_write(struct nvmem_device *nvmem, unsigned int offset,
void *val, size_t bytes)
{
- if (!nvmem->nkeepout)
- return __nvmem_reg_write(nvmem, offset, val, bytes);
+ const struct nvmem_operations *ops;
+
+ guard(srcu)(&nvmem->srcu);
+ ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+ if (!ops)
+ return -ENODEV;
- return nvmem_access_with_keepouts(nvmem, offset, val, bytes, true);
+ return nvmem_access_locked(nvmem, ops, offset, val, bytes, true);
}
#ifdef CONFIG_NVMEM_SYSFS
@@ -1649,10 +1637,15 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
struct nvmem_cell_entry *cell,
void *buf, size_t *len, const char *id, int index)
{
+ const struct nvmem_operations *ops;
int rc;
- rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
+ guard(srcu)(&nvmem->srcu);
+ ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+ if (!ops)
+ return -ENODEV;
+ rc = nvmem_access_locked(nvmem, ops, cell->offset, buf, cell->raw_len, false);
if (rc)
return rc;
@@ -1708,6 +1701,7 @@ void *nvmem_cell_read(struct nvmem_cell *cell, size_t *len)
EXPORT_SYMBOL_GPL(nvmem_cell_read);
static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
+ const struct nvmem_operations *ops,
u8 *_buf, int len)
{
struct nvmem_device *nvmem = cell->nvmem;
@@ -1727,7 +1721,7 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
*b <<= bit_offset;
/* setup the first byte with lsb bits from nvmem */
- rc = nvmem_reg_read(nvmem, cell->offset, &v, 1);
+ rc = nvmem_access_locked(nvmem, ops, cell->offset, &v, 1, false);
if (rc)
goto err;
*b++ |= GENMASK(bit_offset - 1, 0) & v;
@@ -1746,8 +1740,8 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
/* if it's not end on byte boundary */
if ((nbits + bit_offset) % BITS_PER_BYTE) {
/* setup the last byte with msb bits from nvmem */
- rc = nvmem_reg_read(nvmem,
- cell->offset + cell->bytes - 1, &v, 1);
+ rc = nvmem_access_locked(nvmem, ops, cell->offset + cell->bytes - 1,
+ &v, 1, false);
if (rc)
goto err;
*p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
@@ -1763,10 +1757,18 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, size_t len)
{
struct nvmem_device *nvmem = cell->nvmem;
+ const struct nvmem_operations *ops;
int rc;
- if (!nvmem || nvmem->read_only ||
- (cell->bit_offset == 0 && len != cell->bytes))
+ if (!nvmem)
+ return -EINVAL;
+
+ guard(srcu)(&nvmem->srcu);
+ ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+ if (!ops)
+ return -ENODEV;
+
+ if (nvmem->read_only || (cell->bit_offset == 0 && len != cell->bytes))
return -EINVAL;
/*
@@ -1780,12 +1782,12 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si
if (cell->bit_offset || cell->nbits) {
if (len != BITS_TO_BYTES(cell->nbits) && len != cell->bytes)
return -EINVAL;
- buf = nvmem_cell_prepare_write_buffer(cell, buf, len);
+ buf = nvmem_cell_prepare_write_buffer(cell, ops, buf, len);
if (IS_ERR(buf))
return PTR_ERR(buf);
}
- rc = nvmem_reg_write(nvmem, cell->offset, buf, cell->bytes);
+ rc = nvmem_access_locked(nvmem, ops, cell->offset, buf, cell->bytes, true);
/* free the tmp buffer */
if (cell->bit_offset || cell->nbits)
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v2 03/11] nvmem: exclude disconnected providers from callback admission
2026-11-19 16:41 [PATCH v2 00/11] nvmem: complete provider and layout disconnection James Hilliard
2026-11-19 16:41 ` [PATCH v2 01/11] nvmem: retain the provider node until device release James Hilliard
2026-11-19 16:42 ` [PATCH v2 02/11] nvmem: drain keepout and cell post-processing accesses James Hilliard
@ 2026-11-19 16:42 ` James Hilliard
2026-11-19 16:42 ` [PATCH v2 04/11] nvmem: release write protection when the provider disconnects James Hilliard
` (7 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-11-19 16:42 UTC (permalink / raw)
To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
Rafał Miłecki, Michael Walle, Linus Walleij,
Khouloud Touil, Bartosz Golaszewski
Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
Paul Cercueil, linux-kernel, linux-mtd, James Hilliard
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
provider SRCU domain. Serialize acquiring the module reference and
publishing new cells with disconnection under nvmem_mutex. Check sysfs
permissions under SRCU as well.
Cell addition notifies listeners while it is still covered by the
provider drain. A listener such as cp500 can register a different NVMEM
device or unwind its registration. Use a separate SRCU notifier chain so
nested notifications do not block on a waiting notifier writer. Notify
listeners of removal only after admitted cell additions have finished.
Fixes: 6a7b4134fb7a ("nvmem: protect nvmem_device::ops with SRCU")
Link: https://lore.kernel.org/r/sashiko-outbox-219563@kernel.org/
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/nvmem/core.c | 112 +++++++++++++++++++++++++++++++++++++--------------
1 file changed, 81 insertions(+), 31 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 32d36ca904cd..7d75076cda49 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -52,7 +52,7 @@ static DEFINE_IDA(nvmem_ida);
static DEFINE_MUTEX(nvmem_lookup_mutex);
static LIST_HEAD(nvmem_lookup_list);
-static BLOCKING_NOTIFIER_HEAD(nvmem_notifier);
+SRCU_NOTIFIER_HEAD_STATIC(nvmem_notifier);
static int nvmem_access_chunk(struct nvmem_device *nvmem,
const struct nvmem_operations *ops,
@@ -287,10 +287,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;
@@ -331,7 +335,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
@@ -535,7 +544,7 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
{
- blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
+ srcu_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
scoped_guard(mutex, &nvmem_mutex)
list_del(&cell->node);
of_node_put(cell->np);
@@ -571,11 +580,15 @@ static const struct bus_type nvmem_bus_type = {
.name = "nvmem",
};
-static void nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
+static int nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
{
- scoped_guard(mutex, &nvmem_mutex)
+ scoped_guard(mutex, &nvmem_mutex) {
+ if (!rcu_access_pointer(cell->nvmem->ops))
+ return -ENODEV;
list_add_tail(&cell->node, &cell->nvmem->cells);
- blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
+ }
+ srcu_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
+ return 0;
}
static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
@@ -653,6 +666,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;
@@ -663,9 +680,12 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem,
return rval;
}
- nvmem_cell_entry_add(cell);
-
- return 0;
+ rval = nvmem_cell_entry_add(cell);
+ if (rval) {
+ kfree_const(cell->name);
+ kfree(cell);
+ }
+ return rval;
}
EXPORT_SYMBOL_GPL(nvmem_add_one_cell);
@@ -698,11 +718,14 @@ static int nvmem_add_cells(struct nvmem_device *nvmem,
*
* @nb: notifier block to be called on nvmem events.
*
+ * A cell-add notification runs as part of the operation that unregister
+ * drains. Do not synchronously unregister that provider from its callback.
+ *
* Return: 0 on success, negative error number on failure.
*/
int nvmem_register_notifier(struct notifier_block *nb)
{
- return blocking_notifier_chain_register(&nvmem_notifier, nb);
+ return srcu_notifier_chain_register(&nvmem_notifier, nb);
}
EXPORT_SYMBOL_GPL(nvmem_register_notifier);
@@ -715,7 +738,7 @@ EXPORT_SYMBOL_GPL(nvmem_register_notifier);
*/
int nvmem_unregister_notifier(struct notifier_block *nb)
{
- return blocking_notifier_chain_unregister(&nvmem_notifier, nb);
+ return srcu_notifier_chain_unregister(&nvmem_notifier, nb);
}
EXPORT_SYMBOL_GPL(nvmem_unregister_notifier);
@@ -795,6 +818,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};
@@ -851,8 +878,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;
@@ -874,6 +904,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
@@ -1034,7 +1075,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
goto err_destroy_layout;
#endif
- blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
+ srcu_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
return nvmem;
@@ -1047,10 +1088,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);
}
@@ -1063,19 +1102,14 @@ 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);
+ srcu_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
nvmem_sysfs_remove_compat(nvmem);
nvmem_destroy_layout(nvmem);
- kfree(ops);
device_unregister(&nvmem->dev);
}
@@ -1115,17 +1149,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
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v2 04/11] nvmem: release write protection when the provider disconnects
2026-11-19 16:41 [PATCH v2 00/11] nvmem: complete provider and layout disconnection James Hilliard
` (2 preceding siblings ...)
2026-11-19 16:42 ` [PATCH v2 03/11] nvmem: exclude disconnected providers from callback admission James Hilliard
@ 2026-11-19 16:42 ` James Hilliard
2026-11-19 16:52 ` sashiko-bot
2026-11-19 16:42 ` [PATCH v2 05/11] nvmem: reference cell entries independently of their provider list James Hilliard
` (6 subsequent siblings)
10 siblings, 1 reply; 13+ messages in thread
From: James Hilliard @ 2026-11-19 16:42 UTC (permalink / raw)
To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
Rafał Miłecki, Michael Walle, Linus Walleij,
Khouloud Touil, Bartosz Golaszewski
Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
Paul Cercueil, linux-kernel, linux-mtd, James Hilliard
Retained NVMEM consumers keep the device allocation alive after unregister.
Keeping the write-protect GPIO until final device release prevents a
replacement provider from requesting the same GPIO.
Release it after draining provider callbacks, when no write can still
use it. Do this for registration failures as well as unregister.
Fixes: 2a127da461a9 ("nvmem: add support for the write-protect pin")
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/nvmem/core.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 7d75076cda49..8c9dfd2e2a63 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -564,7 +564,6 @@ static void nvmem_release(struct device *dev)
{
struct nvmem_device *nvmem = to_nvmem_device(dev);
- gpiod_put(nvmem->wp_gpio);
nvmem_device_remove_all_cells(nvmem);
ida_free(&nvmem_ida, nvmem->id);
of_node_put(nvmem->dev.of_node);
@@ -912,6 +911,8 @@ static void nvmem_disconnect(struct nvmem_device *nvmem)
ops = rcu_replace_pointer(nvmem->ops, NULL,
lockdep_is_held(&nvmem_mutex));
synchronize_srcu(&nvmem->srcu);
+ gpiod_put(nvmem->wp_gpio);
+ nvmem->wp_gpio = NULL;
kfree(ops);
}
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* Re: [PATCH v2 04/11] nvmem: release write protection when the provider disconnects
2026-11-19 16:42 ` [PATCH v2 04/11] nvmem: release write protection when the provider disconnects James Hilliard
@ 2026-11-19 16:52 ` sashiko-bot
0 siblings, 0 replies; 13+ messages in thread
From: sashiko-bot @ 2026-11-19 16:52 UTC (permalink / raw)
To: James Hilliard, Srinivas Kandagatla, Mathieu Dubois-Briand,
Michael Walle, Rafał Miłecki, Michael Walle,
Linus Walleij, Khouloud Touil, Bartosz Golaszewski
Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
Paul Cercueil, linux-mtd
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] nvmem: inaccurate Fixes trailer in commit message for wp_gpio
release
--
commit 02c6a5d986a679decc94c88b75af03c4912c7f6d
Author: James Hilliard <james.hilliard1@gmail.com>
> nvmem: release write protection when the provider disconnects
>
> Retained NVMEM consumers keep the device allocation alive after unregister.
> Keeping the write-protect GPIO until final device release prevents a
> replacement provider from requesting the same GPIO.
>
> Release it after draining provider callbacks, when no write can still
> use it. Do this for registration failures as well as unregister.
>
> Fixes: 2a127da461a9 ("nvmem: add support for the write-protect pin")
> Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
[Severity: Low]
Does the Fixes tag point to the commit that actually caused wp_gpio to
remain acquired across nvmem_unregister()?
Commit 2a127da461a9 ("nvmem: add support for the write-protect pin") did
not release wp_gpio at all; commit a9c3766cb19c ("nvmem: release the
write-protect pin") later added gpiod_put(nvmem->wp_gpio) to
nvmem_release(). At that point nvmem_unregister() only dropped
nvmem->refcnt, so device unregistration waited for all consumers. It was
commit 87341c4dd5de ("nvmem: remove duplicated reference counting") and
commit 6a7b4134fb7a ("nvmem: protect nvmem_device::ops with SRCU") that
changed nvmem_unregister() to immediately unregister and disconnect the
provider while leaving gpiod_put(nvmem->wp_gpio) in nvmem_release().
Should this instead use:
Fixes: 6a7b4134fb7a ("nvmem: protect nvmem_device::ops with SRCU")
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119-submit-nvmem-provider-lifetime-v1-v2-0-8fca3ec5f18c@gmail.com?part=4
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH v2 05/11] nvmem: reference cell entries independently of their provider list
2026-11-19 16:41 [PATCH v2 00/11] nvmem: complete provider and layout disconnection James Hilliard
` (3 preceding siblings ...)
2026-11-19 16:42 ` [PATCH v2 04/11] nvmem: release write protection when the provider disconnects James Hilliard
@ 2026-11-19 16:42 ` James Hilliard
2026-11-19 16:42 ` [PATCH v2 06/11] nvmem: manage cell attributes independently of device resources James Hilliard
` (5 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-11-19 16:42 UTC (permalink / raw)
To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
Rafał Miłecki, Michael Walle, Linus Walleij,
Khouloud Touil, Bartosz Golaszewski
Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
Paul Cercueil, linux-kernel, linux-mtd, James Hilliard
Cell handles currently borrow entries from the provider list. Give each
list entry a reference and acquire another during lookup, before releasing
nvmem_mutex. Return that reference when the consumer releases its handle
or handle allocation fails.
This separates cell metadata lifetime from list membership, so a cell can
be unpublished while existing consumers retain its description.
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/nvmem/core.c | 35 +++++++++++++++++++++++++++--------
1 file changed, 27 insertions(+), 8 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 8c9dfd2e2a63..d9f1bb64a4a5 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -27,6 +27,7 @@
#define FLAG_COMPAT BIT(0)
struct nvmem_cell_entry {
+ struct kref refcnt;
const char *name;
int offset;
size_t raw_len;
@@ -54,6 +55,8 @@ static LIST_HEAD(nvmem_lookup_list);
SRCU_NOTIFIER_HEAD_STATIC(nvmem_notifier);
+static void nvmem_cell_entry_put(struct nvmem_cell_entry *cell);
+
static int nvmem_access_chunk(struct nvmem_device *nvmem,
const struct nvmem_operations *ops,
unsigned int offset, void *val, size_t bytes, bool write)
@@ -542,22 +545,30 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
#endif /* CONFIG_NVMEM_SYSFS */
-static void nvmem_cell_entry_drop(struct nvmem_cell_entry *cell)
+static void nvmem_cell_entry_release(struct kref *ref)
{
- srcu_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) {
+ srcu_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_release(struct device *dev)
@@ -584,6 +595,7 @@ static int nvmem_cell_entry_add(struct nvmem_cell_entry *cell)
scoped_guard(mutex, &nvmem_mutex) {
if (!rcu_access_pointer(cell->nvmem->ops))
return -ENODEV;
+ kref_init(&cell->refcnt);
list_add_tail(&cell->node, &cell->nvmem->cells);
}
srcu_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
@@ -751,6 +763,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;
}
}
@@ -1397,8 +1410,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;
}
@@ -1422,8 +1437,10 @@ 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;
@@ -1522,6 +1539,7 @@ struct nvmem_cell *of_nvmem_cell_get(struct device_node *np, const char *id)
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);
}
@@ -1635,6 +1653,7 @@ 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);
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v2 06/11] nvmem: manage cell attributes independently of device resources
2026-11-19 16:41 [PATCH v2 00/11] nvmem: complete provider and layout disconnection James Hilliard
` (4 preceding siblings ...)
2026-11-19 16:42 ` [PATCH v2 05/11] nvmem: reference cell entries independently of their provider list James Hilliard
@ 2026-11-19 16:42 ` James Hilliard
2026-11-19 16:42 ` [PATCH v2 07/11] nvmem: distinguish layout cells from provider cells James Hilliard
` (4 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-11-19 16:42 UTC (permalink / raw)
To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
Rafał Miłecki, Michael Walle, Linus Walleij,
Khouloud Touil, Bartosz Golaszewski
Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
Paul Cercueil, linux-kernel, linux-mtd, James Hilliard
Cell attributes are populated once and remain until device deletion. A
layout bound after provider registration therefore cannot publish its
cells through an existing group, and the group cannot be refreshed when
cells are removed.
Keep the group in the NVMEM device and explicitly allocate and release
its attributes. Each attribute holds a cell reference until sysfs has
drained its readers. Rebuild the group from the current cell list, and
unwind partial allocations if population fails.
Serialize group replacement separately from cell-list updates so removal
can wait for sysfs readers without holding nvmem_mutex. Remove the group
before device deletion on both unregister and registration failure.
Fixes: 0331c611949f ("nvmem: core: Expose cells through sysfs")
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/nvmem/core.c | 131 +++++++++++++++++++++++++++++++---------------
drivers/nvmem/internals.h | 3 ++
2 files changed, 92 insertions(+), 42 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index d9f1bb64a4a5..c74e3f4e1de6 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>
@@ -476,59 +477,98 @@ 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(*pattrs), 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;
+}
- /* 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)
- return -ENOMEM;
+static void nvmem_sysfs_remove_cells(struct nvmem_device *nvmem)
+{
+ guard(mutex)(&nvmem->sysfs_mutex);
+ __nvmem_sysfs_remove_cells(nvmem);
+}
- pattrs[i] = &attrs[i];
- i++;
- }
+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;
- group.bin_attrs = pattrs;
+ guard(mutex)(&nvmem->sysfs_mutex);
- ret = device_add_group(&nvmem->dev, &group);
- if (ret)
- return ret;
+ __nvmem_sysfs_remove_cells(nvmem);
+
+ scoped_guard(mutex, &nvmem_mutex) {
+ if (!rcu_access_pointer(nvmem->ops))
+ return -ENODEV;
+ if (list_empty(&nvmem->cells))
+ return 0;
- nvmem->sysfs_cells_populated = true;
+ attrs = kcalloc(list_count_nodes(&nvmem->cells) + 1,
+ sizeof(*attrs), GFP_KERNEL);
+ if (!attrs)
+ return -ENOMEM;
+ 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) {
+ nvmem->cells_group.name = "cells";
+ nvmem->cells_group.bin_attrs = attrs;
+
+ ret = device_add_group(&nvmem->dev, &nvmem->cells_group);
+ if (!ret) {
+ nvmem->sysfs_cells_populated = true;
+ return 0;
+ }
+ nvmem->cells_group.bin_attrs = NULL;
+ }
+
+ nvmem_sysfs_free_cell_attrs(attrs);
return ret;
}
@@ -543,6 +583,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_cell_entry_release(struct kref *ref)
@@ -990,6 +1034,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
rcu_assign_pointer(nvmem->ops, ops);
device_initialize(&nvmem->dev);
+ mutex_init(&nvmem->sysfs_mutex);
if (!config->ignore_wp)
nvmem->wp_gpio = gpiod_get_optional(config->dev, "wp",
@@ -1098,6 +1143,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
nvmem_destroy_layout(nvmem);
#endif
err_remove_dev:
+ nvmem_sysfs_remove_cells(nvmem);
device_del(&nvmem->dev);
err_remove_compat:
nvmem_sysfs_remove_compat(nvmem);
@@ -1125,6 +1171,7 @@ void nvmem_unregister(struct nvmem_device *nvmem)
nvmem_sysfs_remove_compat(nvmem);
nvmem_destroy_layout(nvmem);
+ nvmem_sysfs_remove_cells(nvmem);
device_unregister(&nvmem->dev);
}
EXPORT_SYMBOL_GPL(nvmem_unregister);
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index bc7a99f5aefb..79ea56822ebe 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -4,6 +4,7 @@
#define _LINUX_NVMEM_INTERNALS_H
#include <linux/device.h>
+#include <linux/mutex.h>
#include <linux/nvmem-consumer.h>
#include <linux/nvmem-provider.h>
#include <linux/srcu.h>
@@ -37,7 +38,9 @@ struct nvmem_device {
struct nvmem_layout *layout;
struct nvmem_operations __rcu *ops;
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);
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v2 07/11] nvmem: distinguish layout cells from provider cells
2026-11-19 16:41 [PATCH v2 00/11] nvmem: complete provider and layout disconnection James Hilliard
` (5 preceding siblings ...)
2026-11-19 16:42 ` [PATCH v2 06/11] nvmem: manage cell attributes independently of device resources James Hilliard
@ 2026-11-19 16:42 ` James Hilliard
2026-11-19 16:42 ` [PATCH v2 08/11] nvmem: disconnect cells before removing their layout James Hilliard
` (3 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-11-19 16:42 UTC (permalink / raw)
To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
Rafał Miłecki, Michael Walle, Linus Walleij,
Khouloud Touil, Bartosz Golaszewski
Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
Paul Cercueil, linux-kernel, linux-mtd, James Hilliard
Layouts and providers currently add cells through the same interface.
Record the owner when a layout adds a cell so removing that layout can
leave independently registered provider cells intact.
Add layout-specific registration helpers and convert all layout drivers.
Keep the shared U-Boot environment parser usable by both its layout
driver and the legacy provider, recording layout ownership only for the
layout caller.
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/nvmem/core.c | 61 +++++++++++++++++++++++++++++-------
drivers/nvmem/internals.h | 1 +
drivers/nvmem/layouts/fixed-layout.c | 2 +-
drivers/nvmem/layouts/onie-tlv.c | 19 +++++------
drivers/nvmem/layouts/sl28vpd.c | 2 +-
drivers/nvmem/layouts/u-boot-env.c | 22 +++++++++----
include/linux/nvmem-provider.h | 10 +++++-
7 files changed, 87 insertions(+), 30 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index c74e3f4e1de6..49e80389e198 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -29,6 +29,7 @@
#define FLAG_COMPAT BIT(0)
struct nvmem_cell_entry {
struct kref refcnt;
+ struct nvmem_layout *layout;
const char *name;
int offset;
size_t raw_len;
@@ -707,16 +708,9 @@ 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;
@@ -735,6 +729,7 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem,
return rval;
}
+ cell->layout = layout;
rval = nvmem_cell_entry_add(cell);
if (rval) {
kfree_const(cell->name);
@@ -742,8 +737,38 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem,
}
return rval;
}
+
+/**
+ * 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.
+ *
+ * Associate the cell with the layout rather than the provider.
+ *
+ * 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
*
@@ -868,7 +893,8 @@ 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;
@@ -910,7 +936,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);
@@ -920,8 +946,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);
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index 79ea56822ebe..667955893b5c 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -44,6 +44,7 @@ struct nvmem_device {
};
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/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 4da3a704388e..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,13 +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(cell.np);
- of_node_put(layout);
+ of_node_put(layout_np);
return ret;
}
@@ -137,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;
}
@@ -222,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 79de1e6947d0..1e65a0b6c61f 100644
--- a/drivers/nvmem/layouts/sl28vpd.c
+++ b/drivers/nvmem/layouts/sl28vpd.c
@@ -124,7 +124,7 @@ 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);
diff --git a/drivers/nvmem/layouts/u-boot-env.c b/drivers/nvmem/layouts/u-boot-env.c
index 33ec2350386f..f1fb206846ff 100644
--- a/drivers/nvmem/layouts/u-boot-env.c
+++ b/drivers/nvmem/layouts/u-boot-env.c
@@ -49,7 +49,8 @@ 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;
@@ -78,14 +79,17 @@ 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);
+ if (layout)
+ nvmem_layout_add_cell(layout, &info);
+ else
+ nvmem_add_one_cell(nvmem, &info);
}
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 +160,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 +182,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..e0638bd50d1f 100644
--- a/include/linux/nvmem-provider.h
+++ b/include/linux/nvmem-provider.h
@@ -144,7 +144,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 +173,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);
@@ -214,6 +216,12 @@ static inline int nvmem_layout_register(struct nvmem_layout *layout)
static inline void nvmem_layout_unregister(struct nvmem_layout *layout) {}
+static inline int nvmem_layout_add_cell(struct nvmem_layout *layout,
+ const struct nvmem_cell_info *info)
+{
+ return -EOPNOTSUPP;
+}
+
static inline int nvmem_add_cells_from_dt(struct nvmem_device *nvmem,
struct device_node *np)
{
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v2 08/11] nvmem: disconnect cells before removing their layout
2026-11-19 16:41 [PATCH v2 00/11] nvmem: complete provider and layout disconnection James Hilliard
` (6 preceding siblings ...)
2026-11-19 16:42 ` [PATCH v2 07/11] nvmem: distinguish layout cells from provider cells James Hilliard
@ 2026-11-19 16:42 ` James Hilliard
2026-11-19 16:42 ` [PATCH v2 09/11] nvmem: disconnect published devices before registration rollback James Hilliard
` (2 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-11-19 16:42 UTC (permalink / raw)
To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
Rafał Miłecki, Michael Walle, Linus Walleij,
Khouloud Touil, Bartosz Golaszewski
Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
Paul Cercueil, linux-kernel, linux-mtd, James Hilliard
A layout can be unbound while NVMEM consumers retain its cells. Keeping
the layout module loaded does not retain its binding or private data,
and looking up the module again at cell put can access a removed layout.
A failed add_cells callback can also leave cells registered even though
the layout never bound. The sysfs failure path instead removes every
cell, including cells supplied by the provider.
Remove the layout-owned cells from lookup and mark them disconnected.
Drain their sysfs attributes and SRCU-protected accesses before returning
to the layout driver. Old handles retain only cell metadata and reject
I/O; a later layout probe creates new cells. Use the same cleanup when
cell registration or sysfs population fails.
Replace the layout module get/put paths with this drain. Leave provider
cells registered and rebuild their attributes after layout removal.
Clear the provider-to-layout link when the layout device is unregistered.
Fixes: fc29fd821d9a ("nvmem: core: Rework layouts to become regular devices")
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/nvmem/core.c | 78 +++++++++++++++++++++++--------------------------
drivers/nvmem/layouts.c | 2 ++
2 files changed, 38 insertions(+), 42 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 49e80389e198..edc2f61e6e0a 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -29,6 +29,7 @@
#define FLAG_COMPAT BIT(0)
struct nvmem_cell_entry {
struct kref refcnt;
+ bool disconnected;
struct nvmem_layout *layout;
const char *name;
int offset;
@@ -652,6 +653,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;
@@ -976,24 +978,44 @@ int nvmem_layout_register(struct nvmem_layout *layout)
return -ENODEV;
ret = layout->add_cells(layout);
}
- if (ret)
- return ret;
-
#ifdef CONFIG_NVMEM_SYSFS
- ret = nvmem_populate_sysfs_cells(layout->nvmem);
- if (ret) {
- nvmem_device_remove_all_cells(layout->nvmem);
- return ret;
- }
+ if (!ret)
+ ret = nvmem_populate_sysfs_cells(layout->nvmem);
#endif
-
- return 0;
+ if (ret)
+ 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) {
+ srcu_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_REMOVE, cell);
+ list_del(&cell->node);
+ nvmem_cell_entry_put(cell);
+ }
+#ifdef CONFIG_NVMEM_SYSFS
+ if (rcu_access_pointer(nvmem->ops) && nvmem_populate_sysfs_cells(nvmem))
+ dev_warn(&nvmem->dev, "Failed to restore provider cell attributes\n");
+#endif
}
EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
@@ -1506,12 +1528,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)
@@ -1530,18 +1546,6 @@ nvmem_find_cell_entry_by_node(struct nvmem_device *nvmem, struct device_node *np
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
*
@@ -1604,25 +1608,16 @@ 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);
}
@@ -1739,7 +1734,6 @@ void nvmem_cell_put(struct nvmem_cell *cell)
nvmem_cell_entry_put(cell->entry);
kfree(cell);
- nvmem_layout_module_put(nvmem);
nvmem_device_put(nvmem);
}
EXPORT_SYMBOL_GPL(nvmem_cell_put);
@@ -1796,7 +1790,7 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
guard(srcu)(&nvmem->srcu);
ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
- if (!ops)
+ if (!ops || READ_ONCE(cell->disconnected))
return -ENODEV;
rc = nvmem_access_locked(nvmem, ops, cell->offset, buf, cell->raw_len, false);
@@ -1919,7 +1913,7 @@ static int __nvmem_cell_entry_write(struct nvmem_cell_entry *cell, void *buf, si
guard(srcu)(&nvmem->srcu);
ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
- if (!ops)
+ if (!ops || READ_ONCE(cell->disconnected))
return -ENODEV;
if (nvmem->read_only || (cell->bit_offset == 0 && len != cell->bytes))
diff --git a/drivers/nvmem/layouts.c b/drivers/nvmem/layouts.c
index 409909e3244f..442947873591 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;
}
@@ -203,6 +204,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)
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v2 09/11] nvmem: disconnect published devices before registration rollback
2026-11-19 16:41 [PATCH v2 00/11] nvmem: complete provider and layout disconnection James Hilliard
` (7 preceding siblings ...)
2026-11-19 16:42 ` [PATCH v2 08/11] nvmem: disconnect cells before removing their layout James Hilliard
@ 2026-11-19 16:42 ` James Hilliard
2026-11-19 16:42 ` [PATCH v2 10/11] nvmem: u-boot-env: propagate cell registration errors James Hilliard
2026-11-19 16:42 ` [PATCH v2 11/11] nvmem: allow providers to specify an explicit NULL owner James Hilliard
10 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-11-19 16:42 UTC (permalink / raw)
To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
Rafał Miłecki, Michael Walle, Linus Walleij,
Khouloud Touil, Bartosz Golaszewski
Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
Paul Cercueil, linux-kernel, linux-mtd, James Hilliard
Registration can fail after the NVMEM device is visible and a layout has
published cells. Disconnect the provider before destroying that layout
and deleting the device, just as for normal unregister. This prevents
layout removal from rebuilding attributes while rollback removes them.
Reuse nvmem_unregister() for failures after device_add(). Track whether
the registration notification was emitted so an unsuccessful registration
does not generate an unmatched removal notification.
Document when providers may release their callback state.
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
drivers/nvmem/core.c | 22 ++++++++++++----------
include/linux/nvmem-provider.h | 3 +++
2 files changed, 15 insertions(+), 10 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index edc2f61e6e0a..e878931f375f 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -27,6 +27,7 @@
#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;
@@ -1181,7 +1182,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)
@@ -1190,20 +1191,17 @@ 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;
srcu_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:
- nvmem_sysfs_remove_cells(nvmem);
- device_del(&nvmem->dev);
+err_unregister:
+ nvmem_unregister(nvmem);
+ return ERR_PTR(rval);
err_remove_compat:
nvmem_sysfs_remove_compat(nvmem);
err_put_device:
@@ -1218,6 +1216,9 @@ EXPORT_SYMBOL_GPL(nvmem_register);
* 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)
{
@@ -1225,7 +1226,8 @@ void nvmem_unregister(struct nvmem_device *nvmem)
return;
nvmem_disconnect(nvmem);
- srcu_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
+ if (nvmem->flags & FLAG_REGISTERED)
+ srcu_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
nvmem_sysfs_remove_compat(nvmem);
nvmem_destroy_layout(nvmem);
diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
index e0638bd50d1f..ba11c807593b 100644
--- a/include/linux/nvmem-provider.h
+++ b/include/linux/nvmem-provider.h
@@ -107,6 +107,9 @@ 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;
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v2 10/11] nvmem: u-boot-env: propagate cell registration errors
2026-11-19 16:41 [PATCH v2 00/11] nvmem: complete provider and layout disconnection James Hilliard
` (8 preceding siblings ...)
2026-11-19 16:42 ` [PATCH v2 09/11] nvmem: disconnect published devices before registration rollback James Hilliard
@ 2026-11-19 16:42 ` James Hilliard
2026-11-19 16:42 ` [PATCH v2 11/11] nvmem: allow providers to specify an explicit NULL owner James Hilliard
10 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-11-19 16:42 UTC (permalink / raw)
To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
Rafał Miłecki, Michael Walle, Linus Walleij,
Khouloud Touil, Bartosz Golaszewski
Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
Paul Cercueil, linux-kernel, linux-mtd, James Hilliard
The environment parser ignores cell registration failures, so allocation
failures can leave a partially populated layout reported as successfully
registered. The OF node reference obtained for a rejected cell is also
leaked.
Return the registration error and release the node reference that was
not transferred to a cell.
Fixes: 7c8979b42b1a ("nvmem: u-boot-env: use nvmem_add_one_cell() nvmem subsystem helper")
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
Reviewed-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
drivers/nvmem/layouts/u-boot-env.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/drivers/nvmem/layouts/u-boot-env.c b/drivers/nvmem/layouts/u-boot-env.c
index f1fb206846ff..83c687e43e5a 100644
--- a/drivers/nvmem/layouts/u-boot-env.c
+++ b/drivers/nvmem/layouts/u-boot-env.c
@@ -55,6 +55,7 @@ static int u_boot_env_parse_cells(struct device *dev, struct nvmem_device *nvmem
{
char *data = buf + data_offset;
char *var, *value, *eq;
+ int ret;
for (var = data;
var < data + data_len && *var;
@@ -79,10 +80,12 @@ 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;
}
- if (layout)
- nvmem_layout_add_cell(layout, &info);
- else
- 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;
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread* [PATCH v2 11/11] nvmem: allow providers to specify an explicit NULL owner
2026-11-19 16:41 [PATCH v2 00/11] nvmem: complete provider and layout disconnection James Hilliard
` (9 preceding siblings ...)
2026-11-19 16:42 ` [PATCH v2 10/11] nvmem: u-boot-env: propagate cell registration errors James Hilliard
@ 2026-11-19 16:42 ` James Hilliard
10 siblings, 0 replies; 13+ messages in thread
From: James Hilliard @ 2026-11-19 16:42 UTC (permalink / raw)
To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
Rafał Miłecki, Michael Walle, Linus Walleij,
Khouloud Touil, Bartosz Golaszewski
Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
Paul Cercueil, linux-kernel, linux-mtd, James Hilliard
The owner fallback treats NULL as an unspecified module. A provider whose
operations belong to built-in subsystem code therefore cannot distinguish
its deliberate NULL owner from a request to use the parent driver module.
Add owner_is_explicit so such providers can select their actual callback
owner. Leave existing callers on the parent-module fallback. Unregister
still disconnects and drains callbacks before the provider releases its
state.
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
Reviewed-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
---
drivers/nvmem/core.c | 2 +-
include/linux/nvmem-provider.h | 3 +++
2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index e878931f375f..bf6f2cd50970 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1111,7 +1111,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
ops->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;
diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
index ba11c807593b..019fdf04cafb 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.
@@ -116,6 +118,7 @@ struct nvmem_config {
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;
--
2.53.0
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 13+ messages in thread