[PATCH 09/11] nvmem: disconnect published devices before registration rollback

From: James Hilliard

Date: Tue Nov 17 2026 - 14:40:02 EST


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@xxxxxxxxx>
---
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