mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: James Hilliard <james.hilliard1@gmail.com>
To: "Srinivas Kandagatla" <srini@kernel.org>,
	"Mathieu Dubois-Briand" <mathieu.dubois-briand@bootlin.com>,
	"Michael Walle" <mwalle@kernel.org>,
	"Rafał Miłecki" <rafal@milecki.pl>,
	"Michael Walle" <michael@walle.cc>,
	"Linus Walleij" <linusw@kernel.org>,
	"Khouloud Touil" <ktouil@baylibre.com>,
	"Bartosz Golaszewski" <brgl@kernel.org>
Cc: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>,
	 Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 Miquel Raynal <miquel.raynal@bootlin.com>,
	 Paul Cercueil <paul@crapouillou.net>,
	linux-kernel@vger.kernel.org,  linux-mtd@lists.infradead.org,
	James Hilliard <james.hilliard1@gmail.com>
Subject: [PATCH v2 09/11] nvmem: disconnect published devices before registration rollback
Date: Thu, 19 Nov 2026 09:42:07 -0700	[thread overview]
Message-ID: <20261119-submit-nvmem-provider-lifetime-v1-v2-9-8fca3ec5f18c@gmail.com> (raw)
In-Reply-To: <20261119-submit-nvmem-provider-lifetime-v1-v2-0-8fca3ec5f18c@gmail.com>

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


  parent reply	other threads:[~2026-11-19 16:42 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` [PATCH v2 03/11] nvmem: exclude disconnected providers from callback admission James Hilliard
2026-11-19 16:42 ` [PATCH v2 04/11] nvmem: release write protection when the provider disconnects James Hilliard
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 ` [PATCH v2 06/11] nvmem: manage cell attributes independently of device resources James Hilliard
2026-11-19 16:42 ` [PATCH v2 07/11] nvmem: distinguish layout cells from provider cells James Hilliard
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119-submit-nvmem-provider-lifetime-v1-v2-9-8fca3ec5f18c@gmail.com \
    --to=james.hilliard1@gmail.com \
    --cc=bartosz.golaszewski@oss.qualcomm.com \
    --cc=brgl@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=ktouil@baylibre.com \
    --cc=linusw@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=mathieu.dubois-briand@bootlin.com \
    --cc=michael@walle.cc \
    --cc=miquel.raynal@bootlin.com \
    --cc=mwalle@kernel.org \
    --cc=paul@crapouillou.net \
    --cc=rafal@milecki.pl \
    --cc=srini@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®