Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 00/11] nvmem: complete provider and layout disconnection
@ 2026-11-17 19:38 James Hilliard
  2026-11-17 19:38 ` [PATCH 01/11] nvmem: retain the provider node until device release James Hilliard
                   ` (10 more replies)
  0 siblings, 11 replies; 17+ messages in thread
From: James Hilliard @ 2026-11-17 19:38 UTC (permalink / raw)
  To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
	Rafał Miłecki
  Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
	Paul Cercueil, linux-kernel, linux-mtd, James Hilliard

NVMEM unregister already disconnects register callbacks with SRCU. Some
accesses extend beyond those callbacks: keepout traversal, cell
post-processing, DT fixups and device matching can still use provider
state. Layout unbind also leaves its cells and their callbacks available
to retained consumers.

Extend the existing drain to those accesses, and retire layout-owned
cells independently of provider cells. Old handles retain core-owned
metadata and reject further access. Explicit cell references and sysfs
group ownership make layout removal and rebind independent of existing
consumers. Release the write-protect GPIO when callbacks are drained so
a retained consumer does not prevent provider reprobe.

The final patch lets providers distinguish an explicit NULL module owner
from the parent-driver fallback, for callbacks implemented by built-in
subsystem code.

This separates the NVMEM work previously included in the raw NAND
retained-handle series [0], following Paul's request for smaller patches
[1]. It builds on the NVMEM for-next tree at 6628e7d4c6b, including the
existing SRCU protection and device-reference ownership.

[0] https://lore.kernel.org/r/20261115-submit-rawnand-retained-handle-lifetime-v1-v5-1-cbc49c5c5e74@gmail.com/
[1] https://lore.kernel.org/r/9c247830067d2192acfe08080d1c5f2bb366cf1a.camel@crapouillou.net/

Assisted-by: Codex:gpt-6-astra
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
James Hilliard (11):
      nvmem: retain the provider node until device release
      nvmem: drain keepout and cell post-processing accesses
      nvmem: exclude disconnected providers from callback admission
      nvmem: release write protection when the provider disconnects
      nvmem: reference cell entries independently of their provider list
      nvmem: manage cell attributes independently of device resources
      nvmem: distinguish layout cells from provider cells
      nvmem: disconnect cells before removing their layout
      nvmem: disconnect published devices before registration rollback
      nvmem: u-boot-env: propagate cell registration errors
      nvmem: allow providers to specify an explicit NULL owner

 drivers/nvmem/core.c                 | 426 ++++++++++++++++++++++++-----------
 drivers/nvmem/internals.h            |   4 +
 drivers/nvmem/layouts.c              |   2 +
 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   |  25 +-
 include/linux/nvmem-provider.h       |  16 +-
 8 files changed, 352 insertions(+), 144 deletions(-)
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
base-tree: https://git.kernel.org/pub/scm/linux/kernel/git/srini/nvmem.git for-next
change-id: 20261117-submit-nvmem-provider-lifetime-v1-fb2b3d192fca

Best regards,
--  
James Hilliard <james.hilliard1@gmail.com>


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH 01/11] nvmem: retain the provider node until device release
  2026-11-17 19:38 [PATCH 00/11] nvmem: complete provider and layout disconnection James Hilliard
@ 2026-11-17 19:38 ` James Hilliard
  2026-11-17 19:38 ` [PATCH 02/11] nvmem: drain keepout and cell post-processing accesses James Hilliard
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 17+ messages in thread
From: James Hilliard @ 2026-11-17 19:38 UTC (permalink / raw)
  To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
	Rafał Miłecki
  Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
	Paul Cercueil, linux-kernel, linux-mtd, James Hilliard

An NVMEM device can remain referenced after its provider unregisters.
Keep a reference to its OF node so retained devices do not depend on the
provider retaining the node. Drop that reference in the device release
callback, including registration-error paths.

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..802637f375f8 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;
+		nvmem->dev.of_node = of_node_get(config->dev->of_node);
 
 	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] 17+ messages in thread

* [PATCH 02/11] nvmem: drain keepout and cell post-processing accesses
  2026-11-17 19:38 [PATCH 00/11] nvmem: complete provider and layout disconnection James Hilliard
  2026-11-17 19:38 ` [PATCH 01/11] nvmem: retain the provider node until device release James Hilliard
@ 2026-11-17 19:38 ` James Hilliard
  2026-11-17 19:50   ` sashiko-bot
  2026-11-17 19:38 ` [PATCH 03/11] nvmem: exclude disconnected providers from callback admission James Hilliard
                   ` (8 subsequent siblings)
  10 siblings, 1 reply; 17+ messages in thread
From: James Hilliard @ 2026-11-17 19:38 UTC (permalink / raw)
  To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
	Rafał Miłecki
  Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
	Paul Cercueil, linux-kernel, linux-mtd, James Hilliard

The register callbacks are protected by SRCU, but keepout traversal and
cell post-processing take place outside that protection. Unregister can
finish draining a register read and return while its caller still uses
a provider-owned keepout table or invokes a post-processing callback.

Hold the SRCU read lock across the complete access, including keepout
handling and post-processing. Reject accesses after the operations pointer
has been cleared, including reads satisfied entirely by a keepout.

Fixes: 6a7b4134fb7a ("nvmem: protect nvmem_device::ops with SRCU")
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 drivers/nvmem/core.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 802637f375f8..73aa53145f0b 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -164,6 +164,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 (!srcu_dereference(nvmem->ops, &nvmem->srcu))
+		return -ENODEV;
+
 	if (!nvmem->nkeepout)
 		return __nvmem_reg_read(nvmem, offset, val, bytes);
 
@@ -173,6 +177,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 (!srcu_dereference(nvmem->ops, &nvmem->srcu))
+		return -ENODEV;
+
 	if (!nvmem->nkeepout)
 		return __nvmem_reg_write(nvmem, offset, val, bytes);
 
@@ -1651,6 +1659,10 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
 {
 	int rc;
 
+	guard(srcu)(&nvmem->srcu);
+	if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
+		return -ENODEV;
+
 	rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
 
 	if (rc)

-- 
2.53.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 03/11] nvmem: exclude disconnected providers from callback admission
  2026-11-17 19:38 [PATCH 00/11] nvmem: complete provider and layout disconnection James Hilliard
  2026-11-17 19:38 ` [PATCH 01/11] nvmem: retain the provider node until device release James Hilliard
  2026-11-17 19:38 ` [PATCH 02/11] nvmem: drain keepout and cell post-processing accesses James Hilliard
@ 2026-11-17 19:38 ` James Hilliard
  2026-11-17 20:07   ` sashiko-bot
  2026-11-17 19:38 ` [PATCH 04/11] nvmem: release write protection when the provider disconnects James Hilliard
                   ` (7 subsequent siblings)
  10 siblings, 1 reply; 17+ messages in thread
From: James Hilliard @ 2026-11-17 19:38 UTC (permalink / raw)
  To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
	Rafał Miłecki
  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
existing SRCU domain. Serialize disconnection with consumer acquisition
under nvmem_mutex, keeping the module-reference acquisition inside that
critical section. Check sysfs permissions under SRCU as well, since the
operations pointer may already have been cleared.

Share the disconnect sequence between unregister and registration unwind.

Fixes: 6a7b4134fb7a ("nvmem: protect nvmem_device::ops with SRCU")
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
 drivers/nvmem/core.c | 77 ++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 59 insertions(+), 18 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 73aa53145f0b..c41266241f3f 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -307,10 +307,14 @@ static ssize_t bin_attr_nvmem_write(struct file *filp, struct kobject *kobj,
 
 static umode_t nvmem_bin_attr_get_umode(struct nvmem_device *nvmem)
 {
-	struct nvmem_operations *ops = rcu_dereference_raw(nvmem->ops);
-
+	struct nvmem_operations *ops;
 	umode_t mode = 0400;
 
+	guard(srcu)(&nvmem->srcu);
+	ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+	if (!ops)
+		return 0;
+
 	if (!nvmem->root_only)
 		mode |= 0044;
 
@@ -351,7 +355,12 @@ static umode_t nvmem_attr_is_visible(struct kobject *kobj,
 {
 	struct device *dev = kobj_to_dev(kobj);
 	struct nvmem_device *nvmem = to_nvmem_device(dev);
-	struct nvmem_operations *ops = rcu_dereference_raw(nvmem->ops);
+	struct nvmem_operations *ops;
+
+	guard(srcu)(&nvmem->srcu);
+	ops = srcu_dereference(nvmem->ops, &nvmem->srcu);
+	if (!ops)
+		return 0;
 
 	/*
 	 * If the device has no .reg_write operation, do not allow
@@ -673,6 +682,10 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem,
 	struct nvmem_cell_entry *cell;
 	int rval;
 
+	guard(srcu)(&nvmem->srcu);
+	if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
+		return -ENODEV;
+
 	cell = kzalloc_obj(*cell);
 	if (!cell)
 		return -ENOMEM;
@@ -815,6 +828,10 @@ int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
 	const __be32 *addr;
 	int len, ret;
 
+	guard(srcu)(&nvmem->srcu);
+	if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
+		return -ENODEV;
+
 	for_each_child_of_node_scoped(np, child) {
 		struct nvmem_cell_info info = {0};
 
@@ -871,8 +888,11 @@ int nvmem_layout_register(struct nvmem_layout *layout)
 	if (!layout->add_cells)
 		return -EINVAL;
 
-	/* Populate the cells */
-	ret = layout->add_cells(layout);
+	scoped_guard(srcu, &layout->nvmem->srcu) {
+		if (!srcu_dereference(layout->nvmem->ops, &layout->nvmem->srcu))
+			return -ENODEV;
+		ret = layout->add_cells(layout);
+	}
 	if (ret)
 		return ret;
 
@@ -894,6 +914,17 @@ void nvmem_layout_unregister(struct nvmem_layout *layout)
 }
 EXPORT_SYMBOL_GPL(nvmem_layout_unregister);
 
+static void nvmem_disconnect(struct nvmem_device *nvmem)
+{
+	struct nvmem_operations *ops;
+
+	scoped_guard(mutex, &nvmem_mutex)
+		ops = rcu_replace_pointer(nvmem->ops, NULL,
+					 lockdep_is_held(&nvmem_mutex));
+	synchronize_srcu(&nvmem->srcu);
+	kfree(ops);
+}
+
 /**
  * nvmem_register() - Register a nvmem device for given nvmem_config.
  * Also creates a binary entry in /sys/bus/nvmem/devices/dev-name/nvmem
@@ -1067,10 +1098,8 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 err_remove_compat:
 	nvmem_sysfs_remove_compat(nvmem);
 err_put_device:
-	ops = rcu_replace_pointer(nvmem->ops, NULL, true);
-	synchronize_srcu(&nvmem->srcu);
+	nvmem_disconnect(nvmem);
 	put_device(&nvmem->dev);
-	kfree(ops);
 
 	return ERR_PTR(rval);
 }
@@ -1083,19 +1112,15 @@ EXPORT_SYMBOL_GPL(nvmem_register);
  */
 void nvmem_unregister(struct nvmem_device *nvmem)
 {
-	struct nvmem_operations *ops;
-
 	if (!nvmem)
 		return;
 
 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
 
-	ops = rcu_replace_pointer(nvmem->ops, NULL, true);
-	synchronize_srcu(&nvmem->srcu);
+	nvmem_disconnect(nvmem);
 
 	nvmem_sysfs_remove_compat(nvmem);
 	nvmem_destroy_layout(nvmem);
-	kfree(ops);
 
 	device_unregister(&nvmem->dev);
 }
@@ -1135,17 +1160,33 @@ struct nvmem_device *devm_nvmem_register(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(devm_nvmem_register);
 
+struct nvmem_match_data {
+	void *data;
+	int (*match)(struct device *dev, const void *data);
+};
+
+static int nvmem_match_device(struct device *dev, const void *data)
+{
+	const struct nvmem_match_data *match = data;
+	struct nvmem_device *nvmem = to_nvmem_device(dev);
+
+	guard(srcu)(&nvmem->srcu);
+	if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
+		return 0;
+	return match->match(dev, match->data);
+}
+
 static struct nvmem_device *nvmem_device_match(void *data,
 			int (*match)(struct device *dev, const void *data))
 {
+	struct nvmem_match_data match_data = { .data = data, .match = match };
 	struct nvmem_device *nvmem = NULL;
 	struct device *dev;
 
-	scoped_guard(mutex, &nvmem_mutex) {
-		dev = bus_find_device(&nvmem_bus_type, NULL, data, match);
-		if (dev)
-			nvmem = to_nvmem_device(dev);
-	}
+	guard(mutex)(&nvmem_mutex);
+	dev = bus_find_device(&nvmem_bus_type, NULL, &match_data, nvmem_match_device);
+	if (dev)
+		nvmem = to_nvmem_device(dev);
 	if (!nvmem)
 		return ERR_PTR(-EPROBE_DEFER);
 

-- 
2.53.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 04/11] nvmem: release write protection when the provider disconnects
  2026-11-17 19:38 [PATCH 00/11] nvmem: complete provider and layout disconnection James Hilliard
                   ` (2 preceding siblings ...)
  2026-11-17 19:38 ` [PATCH 03/11] nvmem: exclude disconnected providers from callback admission James Hilliard
@ 2026-11-17 19:38 ` James Hilliard
  2026-11-17 19:38 ` [PATCH 05/11] nvmem: reference cell entries independently of their provider list James Hilliard
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 17+ messages in thread
From: James Hilliard @ 2026-11-17 19:38 UTC (permalink / raw)
  To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
	Rafał Miłecki
  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 c41266241f3f..65d89498edeb 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -584,7 +584,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);
@@ -922,6 +921,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] 17+ messages in thread

* [PATCH 05/11] nvmem: reference cell entries independently of their provider list
  2026-11-17 19:38 [PATCH 00/11] nvmem: complete provider and layout disconnection James Hilliard
                   ` (3 preceding siblings ...)
  2026-11-17 19:38 ` [PATCH 04/11] nvmem: release write protection when the provider disconnects James Hilliard
@ 2026-11-17 19:38 ` James Hilliard
  2026-11-17 19:48   ` sashiko-bot
  2026-11-17 19:38 ` [PATCH 06/11] nvmem: manage cell attributes independently of device resources James Hilliard
                   ` (5 subsequent siblings)
  10 siblings, 1 reply; 17+ messages in thread
From: James Hilliard @ 2026-11-17 19:38 UTC (permalink / raw)
  To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
	Rafał Miłecki
  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 65d89498edeb..70b6126886d9 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);
 
 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)
 {
@@ -562,22 +565,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)
 {
-	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_release(struct device *dev)
@@ -695,6 +706,7 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem,
 		return rval;
 	}
 
+	kref_init(&cell->refcnt);
 	nvmem_cell_entry_add(cell);
 
 	return 0;
@@ -761,6 +773,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;
 		}
 	}
@@ -1408,8 +1421,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;
 		}
@@ -1433,8 +1448,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;
@@ -1533,6 +1550,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);
 	}
 
@@ -1646,6 +1664,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] 17+ messages in thread

* [PATCH 06/11] nvmem: manage cell attributes independently of device resources
  2026-11-17 19:38 [PATCH 00/11] nvmem: complete provider and layout disconnection James Hilliard
                   ` (4 preceding siblings ...)
  2026-11-17 19:38 ` [PATCH 05/11] nvmem: reference cell entries independently of their provider list James Hilliard
@ 2026-11-17 19:38 ` James Hilliard
  2026-11-17 19:59   ` sashiko-bot
  2026-11-17 19:38 ` [PATCH 07/11] nvmem: distinguish layout cells from provider cells James Hilliard
                   ` (4 subsequent siblings)
  10 siblings, 1 reply; 17+ messages in thread
From: James Hilliard @ 2026-11-17 19:38 UTC (permalink / raw)
  To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
	Rafał Miłecki
  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      | 127 ++++++++++++++++++++++++++++++++--------------
 drivers/nvmem/internals.h |   3 ++
 2 files changed, 92 insertions(+), 38 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 70b6126886d9..14cc70e8ce78 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>
@@ -496,59 +497,102 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
 		device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
 }
 
+static void nvmem_sysfs_free_cell_attrs(const struct bin_attribute * const *attrs)
+{
+	if (!attrs)
+		return;
+
+	for (int i = 0; attrs[i]; i++) {
+		nvmem_cell_entry_put(attrs[i]->private);
+		kfree(attrs[i]->attr.name);
+		kfree(attrs[i]);
+	}
+	kfree(attrs);
+}
+
+static void __nvmem_sysfs_remove_cells(struct nvmem_device *nvmem)
+{
+	lockdep_assert_held(&nvmem->sysfs_mutex);
+
+	if (!nvmem->sysfs_cells_populated)
+		return;
+
+	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 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;
+	const struct bin_attribute **attrs = NULL;
+	unsigned int i = 0;
+	int ret = 0;
 
-	guard(mutex)(&nvmem_mutex);
+	guard(mutex)(&nvmem->sysfs_mutex);
 
-	if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated)
-		return 0;
+	__nvmem_sysfs_remove_cells(nvmem);
 
-	/* 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;
-
-	attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
-	if (!attrs)
-		return -ENOMEM;
+	scoped_guard(mutex, &nvmem_mutex) {
+		if (!rcu_access_pointer(nvmem->ops))
+			return -ENODEV;
+		if (list_empty(&nvmem->cells))
+			return 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)
+		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;
 }
 
@@ -563,6 +607,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)
@@ -1000,6 +1048,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",
@@ -1108,6 +1157,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);
@@ -1136,6 +1186,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] 17+ messages in thread

* [PATCH 07/11] nvmem: distinguish layout cells from provider cells
  2026-11-17 19:38 [PATCH 00/11] nvmem: complete provider and layout disconnection James Hilliard
                   ` (5 preceding siblings ...)
  2026-11-17 19:38 ` [PATCH 06/11] nvmem: manage cell attributes independently of device resources James Hilliard
@ 2026-11-17 19:38 ` James Hilliard
  2026-11-17 19:51   ` sashiko-bot
  2026-11-17 19:38 ` [PATCH 08/11] nvmem: disconnect cells before removing their layout James Hilliard
                   ` (3 subsequent siblings)
  10 siblings, 1 reply; 17+ messages in thread
From: James Hilliard @ 2026-11-17 19:38 UTC (permalink / raw)
  To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
	Rafał Miłecki
  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 14cc70e8ce78..57c1da5ab547 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;
@@ -726,16 +727,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;
@@ -755,12 +749,43 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem,
 	}
 
 	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.
+ *
+ * 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
  *
@@ -882,7 +907,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;
@@ -924,7 +950,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);
@@ -934,8 +960,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] 17+ messages in thread

* [PATCH 08/11] nvmem: disconnect cells before removing their layout
  2026-11-17 19:38 [PATCH 00/11] nvmem: complete provider and layout disconnection James Hilliard
                   ` (6 preceding siblings ...)
  2026-11-17 19:38 ` [PATCH 07/11] nvmem: distinguish layout cells from provider cells James Hilliard
@ 2026-11-17 19:38 ` James Hilliard
  2026-11-17 19:38 ` [PATCH 09/11] nvmem: disconnect published devices before registration rollback James Hilliard
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 17+ messages in thread
From: James Hilliard @ 2026-11-17 19:38 UTC (permalink / raw)
  To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
	Rafał Miłecki
  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    | 84 +++++++++++++++++++++++++++----------------------
 drivers/nvmem/layouts.c |  2 ++
 2 files changed, 49 insertions(+), 37 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 57c1da5ab547..f1a254a0a5cb 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;
@@ -671,6 +672,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;
@@ -991,23 +993,50 @@ int nvmem_layout_register(struct nvmem_layout *layout)
 		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 (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);
 
@@ -1521,12 +1550,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)
@@ -1545,18 +1568,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
  *
@@ -1619,25 +1630,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);
 	}
@@ -1754,7 +1756,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);
@@ -1809,7 +1810,8 @@ static int __nvmem_cell_read(struct nvmem_device *nvmem,
 	int rc;
 
 	guard(srcu)(&nvmem->srcu);
-	if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
+	if (!srcu_dereference(nvmem->ops, &nvmem->srcu) ||
+	    READ_ONCE(cell->disconnected))
 		return -ENODEV;
 
 	rc = nvmem_reg_read(nvmem, cell->offset, buf, cell->raw_len);
@@ -1926,7 +1928,15 @@ 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 (!srcu_dereference(nvmem->ops, &nvmem->srcu) ||
+	    READ_ONCE(cell->disconnected))
+		return -ENODEV;
+
+	if (nvmem->read_only ||
 	    (cell->bit_offset == 0 && len != cell->bytes))
 		return -EINVAL;
 
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] 17+ messages in thread

* [PATCH 09/11] nvmem: disconnect published devices before registration rollback
  2026-11-17 19:38 [PATCH 00/11] nvmem: complete provider and layout disconnection James Hilliard
                   ` (7 preceding siblings ...)
  2026-11-17 19:38 ` [PATCH 08/11] nvmem: disconnect cells before removing their layout James Hilliard
@ 2026-11-17 19:38 ` James Hilliard
  2026-11-17 19:38 ` [PATCH 10/11] nvmem: u-boot-env: propagate cell registration errors James Hilliard
  2026-11-17 19:38 ` [PATCH 11/11] nvmem: allow providers to specify an explicit NULL owner James Hilliard
  10 siblings, 0 replies; 17+ messages in thread
From: James Hilliard @ 2026-11-17 19:38 UTC (permalink / raw)
  To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
	Rafał Miłecki
  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 f1a254a0a5cb..619d33a59697 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;
@@ -1202,7 +1203,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)
@@ -1211,20 +1212,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;
 	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:
-	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:
@@ -1239,13 +1237,17 @@ 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)
 {
 	if (!nvmem)
 		return;
 
-	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
+	if (nvmem->flags & FLAG_REGISTERED)
+		blocking_notifier_call_chain(&nvmem_notifier, NVMEM_REMOVE, nvmem);
 
 	nvmem_disconnect(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] 17+ messages in thread

* [PATCH 10/11] nvmem: u-boot-env: propagate cell registration errors
  2026-11-17 19:38 [PATCH 00/11] nvmem: complete provider and layout disconnection James Hilliard
                   ` (8 preceding siblings ...)
  2026-11-17 19:38 ` [PATCH 09/11] nvmem: disconnect published devices before registration rollback James Hilliard
@ 2026-11-17 19:38 ` James Hilliard
  2026-11-17 19:38 ` [PATCH 11/11] nvmem: allow providers to specify an explicit NULL owner James Hilliard
  10 siblings, 0 replies; 17+ messages in thread
From: James Hilliard @ 2026-11-17 19:38 UTC (permalink / raw)
  To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
	Rafał Miłecki
  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>
---
 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] 17+ messages in thread

* [PATCH 11/11] nvmem: allow providers to specify an explicit NULL owner
  2026-11-17 19:38 [PATCH 00/11] nvmem: complete provider and layout disconnection James Hilliard
                   ` (9 preceding siblings ...)
  2026-11-17 19:38 ` [PATCH 10/11] nvmem: u-boot-env: propagate cell registration errors James Hilliard
@ 2026-11-17 19:38 ` James Hilliard
  10 siblings, 0 replies; 17+ messages in thread
From: James Hilliard @ 2026-11-17 19:38 UTC (permalink / raw)
  To: Srinivas Kandagatla, Mathieu Dubois-Briand, Michael Walle,
	Rafał Miłecki
  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>
---
 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 619d33a59697..ce2a35cb6670 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -1132,7 +1132,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] 17+ messages in thread

* Re: [PATCH 05/11] nvmem: reference cell entries independently of their provider list
  2026-11-17 19:38 ` [PATCH 05/11] nvmem: reference cell entries independently of their provider list James Hilliard
@ 2026-11-17 19:48   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-17 19:48 UTC (permalink / raw)
  To: James Hilliard, Srinivas Kandagatla, Mathieu Dubois-Briand,
	Michael Walle, Rafał Miłecki
  Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
	Paul Cercueil, linux-mtd

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] nvmem: missing layout module reference in
  nvmem_cell_get_from_lookup() [1]
--

commit dca0d45c01829b24f06f31f8f551815c804db7a1
Author: James Hilliard <james.hilliard1@gmail.com>

nvmem: reference cell entries independently of their provider list

This commit adds a kref to struct nvmem_cell_entry so that cell handles hold a
reference to their cell entry independently of the provider's cell list.

> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 65d89498edeb3..70b6126886d9f 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c

[ ... ]

> @@ -1408,8 +1421,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;
>  		}

[Severity: Critical]
This problem wasn't introduced by this patch, but does
nvmem_cell_get_from_lookup() miss acquiring a layout module reference
[1] before creating the cell?

Unlike of_nvmem_cell_get(), this path never calls
nvmem_layout_module_get_optional(), yet nvmem_cell_put() unconditionally calls
nvmem_layout_module_put(nvmem).  Releasing a cell obtained from a lookup table
on a device with a layout will underflow the layout module refcount, allowing
the module to be unloaded while still in use.

[1] https://sashiko.dev/#/bug/linux-4d65574f-cdef-4be7-87ff-b1c197e66c04

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-submit-nvmem-provider-lifetime-v1-v1-0-3cfbd48812b5@gmail.com?part=5

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 02/11] nvmem: drain keepout and cell post-processing accesses
  2026-11-17 19:38 ` [PATCH 02/11] nvmem: drain keepout and cell post-processing accesses James Hilliard
@ 2026-11-17 19:50   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-17 19:50 UTC (permalink / raw)
  To: James Hilliard, Srinivas Kandagatla, Mathieu Dubois-Briand,
	Michael Walle, Rafał Miłecki
  Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
	Paul Cercueil, linux-mtd

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] nvmem: core: race condition in __nvmem_reg_write() during
  WP GPIO toggling [1]
--

commit 1a5a4f5f38aeaf6b1486ab8a21e16285a6795aae
Author: James Hilliard <james.hilliard1@gmail.com>

nvmem: drain keepout and cell post-processing accesses

This commit extends SRCU read-lock protection and nvmem->ops validation to
cover keepout region handling in nvmem_reg_read() and nvmem_reg_write() as
well as cell post-processing in __nvmem_cell_read().

> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 802637f375f8b..73aa53145f0b7 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c

[ ... ]

> @@ -173,6 +177,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 (!srcu_dereference(nvmem->ops, &nvmem->srcu))
> +		return -ENODEV;
> +
>  	if (!nvmem->nkeepout)
>  		return __nvmem_reg_write(nvmem, offset, val, bytes);

[Severity: Critical]
This problem wasn't introduced by this patch, but can concurrent callers of
__nvmem_reg_write() race on nvmem->wp_gpio [1]?

drivers/nvmem/core.c:__nvmem_reg_write() {
	...
	ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
	if (ret)
		return ret;

	wr_ok = ops->reg_write(nvmem->priv, offset, val, bytes);

	ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 1);
	...
}

Nothing serializes the write-protect GPIO transitions across concurrent
writes:

CPU0                                  CPU1
__nvmem_reg_write()                   __nvmem_reg_write()
  gpiod_set_value_cansleep(wp, 0)
                                        gpiod_set_value_cansleep(wp, 0)
  ops->reg_write(...)
  gpiod_set_value_cansleep(wp, 1)
                                        ops->reg_write(...)  <-- WP active

CPU0 re-asserts write protection while CPU1 is still executing
ops->reg_write(), which can cause dropped writes, hardware errors, or
silent data corruption.

[1] https://sashiko.dev/#/bug/linux-d7d845df-786a-40cf-b43c-f4ce2e295c82

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-submit-nvmem-provider-lifetime-v1-v1-0-3cfbd48812b5@gmail.com?part=2

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 07/11] nvmem: distinguish layout cells from provider cells
  2026-11-17 19:38 ` [PATCH 07/11] nvmem: distinguish layout cells from provider cells James Hilliard
@ 2026-11-17 19:51   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-17 19:51 UTC (permalink / raw)
  To: James Hilliard, Srinivas Kandagatla, Mathieu Dubois-Briand,
	Michael Walle, Rafał Miłecki
  Cc: Bartosz Golaszewski, Greg Kroah-Hartman, Miquel Raynal,
	Paul Cercueil, linux-mtd

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 2 related pre-existing issues it does not
introduce:
- [Medium] nvmem: core: memory leak on error path in
  nvmem_add_cells_from_dt() [1]
- [Critical] nvmem: layouts: onie-tlv: out-of-bounds read in
  onie_tlv_add_cells() [2]
--

commit 0c2f98425a6e2037eca692b433e46249d579c5ee
Author: James Hilliard <james.hilliard1@gmail.com>

nvmem: distinguish layout cells from provider cells

This commit records the owning layout on NVMEM cell entries and adds
layout-specific cell registration helpers so layout cells can be
distinguished from provider cells.

> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 14cc70e8ce785..57c1da5ab547c 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c

[ ... ]

> @@ -882,7 +907,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;
> @@ -924,7 +950,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);

[Severity: Medium]
This problem wasn't introduced by this patch, but does
__nvmem_add_cells_from_dt() leak info.name [1] when the "bits"
property is invalid?

drivers/nvmem/core.c:__nvmem_add_cells_from_dt() {
	...
		info.name = kasprintf(GFP_KERNEL, "%pOFn", child);

		addr = of_get_property(child, "bits", &len);
		if (addr && len == (2 * sizeof(u32))) {
			info.bit_offset = be32_to_cpup(addr++);
			info.nbits = be32_to_cpup(addr);
			if (info.bit_offset >= BITS_PER_BYTE * info.bytes ||
			    info.nbits < 1 ||
			    info.bit_offset + info.nbits > BITS_PER_BYTE * info.bytes) {
				dev_err(dev, "nvmem: invalid bits on %pOF\n", child);
				return -EINVAL;
			}
		}
	...
}

Returning -EINVAL directly skips kfree(info.name).

[ ... ]

> diff --git a/drivers/nvmem/layouts/onie-tlv.c b/drivers/nvmem/layouts/onie-tlv.c
> index 4da3a704388e9..99e7a5406b4ce 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);

[Severity: Critical]
This problem wasn't introduced by this patch, but can a malformed ONIE TLV
table trigger an out-of-bounds read in onie_tlv_add_cells() [2]?

drivers/nvmem/layouts/onie-tlv.c:onie_tlv_add_cells() {
	...
	while (offset < data_len) {
		memcpy(&tlv, data + offset, sizeof(tlv));
		if (offset + tlv.len >= data_len) {
	...
}

Neither the loop condition nor the bounds check accounts for sizeof(tlv).
If fewer than sizeof(tlv) bytes remain in data, memcpy() reads past the end
of the heap buffer, and a truncated entry with offset + sizeof(tlv) +
tlv.len > data_len can still pass the check and register a cell extending
past the table.

[1] https://sashiko.dev/#/bug/linux-c0ac748f-5eb4-43f2-ad9b-c1ed0e5f7c40
[2] https://sashiko.dev/#/bug/linux-dabf598f-cde5-44cc-a226-fd0a93723e20

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-submit-nvmem-provider-lifetime-v1-v1-0-3cfbd48812b5@gmail.com?part=7

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 06/11] nvmem: manage cell attributes independently of device resources
  2026-11-17 19:38 ` [PATCH 06/11] nvmem: manage cell attributes independently of device resources James Hilliard
@ 2026-11-17 19:59   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-17 19:59 UTC (permalink / raw)
  To: James Hilliard, Srinivas Kandagatla, Mathieu Dubois-Briand,
	Michael Walle, Rafał Miłecki
  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: mix of cleanup helpers and goto unwinding in
  nvmem_populate_sysfs_cells()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Medium] nvmem: resource leak in nvmem_layout_register() [1]
--

commit 3af327034f410c1d0e7599f729d78e0fec4f2dfa
Author: James Hilliard <james.hilliard1@gmail.com>

nvmem: manage cell attributes independently of device resources

This commit manages NVMEM sysfs cell attributes explicitly with reference
counting and a dedicated mutex so the attribute group can be rebuilt or
removed independently of device lifetime.

> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 70b6126886d9f..14cc70e8ce785 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
[ ... ]
> @@ -496,59 +497,102 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem)
[ ... ]
>  static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
>  {
> -	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;
> +	const struct bin_attribute **attrs = NULL;
> +	unsigned int i = 0;
> +	int ret = 0;
>  
> -	guard(mutex)(&nvmem_mutex);
> +	guard(mutex)(&nvmem->sysfs_mutex);
>  
> -	if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated)
> -		return 0;
> +	__nvmem_sysfs_remove_cells(nvmem);
>  
> -	/* 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;
> -
> -	attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
> -	if (!attrs)
> -		return -ENOMEM;
> +	scoped_guard(mutex, &nvmem_mutex) {
> +		if (!rcu_access_pointer(nvmem->ops))
> +			return -ENODEV;
> +		if (list_empty(&nvmem->cells))
> +			return 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)
> +		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;
>  }

[Severity: Low]
This isn't a bug, but nvmem_populate_sysfs_cells() mixes guard() and
scoped_guard() with goto err_free cleanup in the same function, whereas
cleanup.h guidelines prefer either converting all resources in a function
to scope-based cleanup or keeping traditional goto unwinding.

[Severity: Medium]
This problem wasn't introduced by this patch, but in the caller
nvmem_layout_register(), can a partial failure in layout->add_cells() leak
nvmem_cell_entry structures [1]?

drivers/nvmem/core.c:nvmem_layout_register() {
	...
	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;

#ifdef CONFIG_NVMEM_SYSFS
	ret = nvmem_populate_sysfs_cells(layout->nvmem);
	if (ret) {
		nvmem_device_remove_all_cells(layout->nvmem);
		return ret;
	}
#endif
	...
}

Unlike the nvmem_populate_sysfs_cells() error path right below it, if
layout->add_cells() fails after adding one or more cells, it returns
without calling nvmem_device_remove_all_cells(layout->nvmem), leaving those
entries attached to nvmem->cells and duplicating them if the layout is
later retried or rebound.

[1] https://sashiko.dev/#/bug/linux-014a0624-e8da-49aa-95a8-67a7c19dfa24

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-submit-nvmem-provider-lifetime-v1-v1-0-3cfbd48812b5@gmail.com?part=6

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 03/11] nvmem: exclude disconnected providers from callback admission
  2026-11-17 19:38 ` [PATCH 03/11] nvmem: exclude disconnected providers from callback admission James Hilliard
@ 2026-11-17 20:07   ` sashiko-bot
  0 siblings, 0 replies; 17+ messages in thread
From: sashiko-bot @ 2026-11-17 20:07 UTC (permalink / raw)
  To: James Hilliard, Srinivas Kandagatla, Mathieu Dubois-Briand,
	Michael Walle, Rafał Miłecki
  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:
- [Medium] nvmem: SRCU held across nvmem_cell_entry_add() without
  rechecking ops
--

commit f2f33b1005de81978c098ebed41d0fc8a71c6f7e
Author: James Hilliard <james.hilliard1@gmail.com>

nvmem: exclude disconnected providers from callback admission

This commit protects sysfs permission checks, cell addition, DT fixups,
layout population, and device matching with the provider's SRCU domain,
and serializes provider disconnection against consumer lookup under
nvmem_mutex.

> diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
> index 73aa53145f0b7..c41266241f3f9 100644
> --- a/drivers/nvmem/core.c
> +++ b/drivers/nvmem/core.c
[ ... ]
> @@ -673,6 +682,10 @@ int nvmem_add_one_cell(struct nvmem_device *nvmem,
>  	struct nvmem_cell_entry *cell;
>  	int rval;
>  
> +	guard(srcu)(&nvmem->srcu);
> +	if (!srcu_dereference(nvmem->ops, &nvmem->srcu))
> +		return -ENODEV;
> +
>  	cell = kzalloc_obj(*cell);
>  	if (!cell)
>  		return -ENOMEM;

[Severity: Medium]
Does holding nvmem->srcu across nvmem_cell_entry_add() invert the lock
ordering with nvmem_notifier.rwsem and allow cells to be added after
nvmem_unregister() has disconnected the provider?

nvmem_cell_entry_add() calls blocking_notifier_call_chain(&nvmem_notifier,
NVMEM_CELL_ADD, cell) while nvmem->srcu is held. All nvmem->srcu instances
are initialized with init_srcu_struct() and share a single static lockdep
class. Notifier callbacks such as cp500_nvmem() call nvmem_register() and
nvmem_unregister() under nvmem_notifier.rwsem, which on error unwind or
removal calls nvmem_disconnect() -> synchronize_srcu(&nvmem->srcu):

CPU0                              CPU1
----                              ----
lock(&nvmem->srcu)
                                  down_read(&nvmem_notifier.rwsem)
down_read(&nvmem_notifier.rwsem)
                                  synchronize_srcu(&nvmem->srcu)

 *** DEADLOCK ***

CPU0: nvmem_add_one_cell() -> nvmem_cell_entry_add()
CPU1: blocking_notifier_call_chain() -> cp500_nvmem() -> nvmem_register()
      -> nvmem_disconnect()

In addition, nvmem_add_one_cell() only checks nvmem->ops at entry before
nvmem_cell_entry_add() acquires nvmem_mutex:

drivers/nvmem/core.c:nvmem_cell_entry_add() {
	scoped_guard(mutex, &nvmem_mutex)
		list_add_tail(&cell->node, &cell->nvmem->cells);
	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell);
}

If nvmem_unregister() runs concurrently, it sends NVMEM_REMOVE, clears
nvmem->ops under nvmem_mutex in nvmem_disconnect(), and waits in
synchronize_srcu(). An in-flight nvmem_add_one_cell() that passed the
initial check then acquires nvmem_mutex without rechecking nvmem->ops,
appends the cell to the disconnected provider, and emits NVMEM_CELL_ADD
after NVMEM_REMOVE.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-submit-nvmem-provider-lifetime-v1-v1-0-3cfbd48812b5@gmail.com?part=3

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2026-11-17 20:07 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 19:38 [PATCH 00/11] nvmem: complete provider and layout disconnection James Hilliard
2026-11-17 19:38 ` [PATCH 01/11] nvmem: retain the provider node until device release James Hilliard
2026-11-17 19:38 ` [PATCH 02/11] nvmem: drain keepout and cell post-processing accesses James Hilliard
2026-11-17 19:50   ` sashiko-bot
2026-11-17 19:38 ` [PATCH 03/11] nvmem: exclude disconnected providers from callback admission James Hilliard
2026-11-17 20:07   ` sashiko-bot
2026-11-17 19:38 ` [PATCH 04/11] nvmem: release write protection when the provider disconnects James Hilliard
2026-11-17 19:38 ` [PATCH 05/11] nvmem: reference cell entries independently of their provider list James Hilliard
2026-11-17 19:48   ` sashiko-bot
2026-11-17 19:38 ` [PATCH 06/11] nvmem: manage cell attributes independently of device resources James Hilliard
2026-11-17 19:59   ` sashiko-bot
2026-11-17 19:38 ` [PATCH 07/11] nvmem: distinguish layout cells from provider cells James Hilliard
2026-11-17 19:51   ` sashiko-bot
2026-11-17 19:38 ` [PATCH 08/11] nvmem: disconnect cells before removing their layout James Hilliard
2026-11-17 19:38 ` [PATCH 09/11] nvmem: disconnect published devices before registration rollback James Hilliard
2026-11-17 19:38 ` [PATCH 10/11] nvmem: u-boot-env: propagate cell registration errors James Hilliard
2026-11-17 19:38 ` [PATCH 11/11] nvmem: allow providers to specify an explicit NULL owner James Hilliard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox