All of lore.kernel.org
 help / color / mirror / Atom feed
From: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
To: gregkh@linuxfoundation.org
Cc: linux-kernel@vger.kernel.org,
	"Russell King (Oracle)" <rmk+kernel@armlinux.org.uk>,
	stable@vger.kernel.org,
	Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
Subject: [PATCH 06/10] nvmem: core: fix registration vs use race
Date: Fri, 27 Jan 2023 10:40:11 +0000	[thread overview]
Message-ID: <20230127104015.23839-7-srinivas.kandagatla@linaro.org> (raw)
In-Reply-To: <20230127104015.23839-1-srinivas.kandagatla@linaro.org>

From: "Russell King (Oracle)" <rmk+kernel@armlinux.org.uk>

The i.MX6 CPU frequency driver sometimes fails to register at boot time
due to nvmem_cell_read_u32() sporadically returning -ENOENT.

This happens because there is a window where __nvmem_device_get() in
of_nvmem_cell_get() is able to return the nvmem device, but as cells
have been setup, nvmem_find_cell_entry_by_node() returns NULL.

The occurs because the nvmem core registration code violates one of the
fundamental principles of kernel programming: do not publish data
structures before their setup is complete.

Fix this by making nvmem core code conform with this principle.

Cc: stable@vger.kernel.org
Fixes: eace75cfdcf7 ("nvmem: Add a simple NVMEM framework for nvmem providers")
Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
---
 drivers/nvmem/core.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index ac77a019aed7..e92c6f1aadbb 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -832,22 +832,16 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	nvmem->dev.groups = nvmem_dev_groups;
 #endif
 
-	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
-
-	rval = device_add(&nvmem->dev);
-	if (rval)
-		goto err_put_device;
-
 	if (nvmem->nkeepout) {
 		rval = nvmem_validate_keepouts(nvmem);
 		if (rval)
-			goto err_device_del;
+			goto err_put_device;
 	}
 
 	if (config->compat) {
 		rval = nvmem_sysfs_setup_compat(nvmem, config);
 		if (rval)
-			goto err_device_del;
+			goto err_put_device;
 	}
 
 	if (config->cells) {
@@ -864,6 +858,12 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	if (rval)
 		goto err_remove_cells;
 
+	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
+
+	rval = device_add(&nvmem->dev);
+	if (rval)
+		goto err_remove_cells;
+
 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
 
 	return nvmem;
@@ -873,8 +873,6 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 err_teardown_compat:
 	if (config->compat)
 		nvmem_sysfs_remove_compat(nvmem, config);
-err_device_del:
-	device_del(&nvmem->dev);
 err_put_device:
 	put_device(&nvmem->dev);
 
-- 
2.25.1


  parent reply	other threads:[~2023-01-27 10:40 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-27 10:40 [PATCH 00/10] nvmem: fixes for 6.2 Srinivas Kandagatla
2023-01-27 10:40 ` [PATCH 01/10] nvmem: brcm_nvram: Add check for kzalloc Srinivas Kandagatla
2023-01-27 10:40 ` [PATCH 02/10] nvmem: sunxi_sid: Always use 32-bit MMIO reads Srinivas Kandagatla
2023-01-27 10:40 ` [PATCH 03/10] nvmem: core: initialise nvmem->id early Srinivas Kandagatla
2023-01-27 10:40 ` [PATCH 04/10] nvmem: core: remove nvmem_config wp_gpio Srinivas Kandagatla
2023-01-27 10:40 ` [PATCH 05/10] nvmem: core: fix cleanup after dev_set_name() Srinivas Kandagatla
2023-01-27 10:40 ` Srinivas Kandagatla [this message]
2023-01-27 10:40 ` [PATCH 07/10] nvmem: core: fix device node refcounting Srinivas Kandagatla
2023-01-27 10:40 ` [PATCH 08/10] nvmem: core: fix cell removal on error Srinivas Kandagatla
2023-01-27 10:40 ` [PATCH 09/10] nvmem: core: fix return value Srinivas Kandagatla
2023-01-27 10:40 ` [PATCH 10/10] nvmem: qcom-spmi-sdam: fix module autoloading Srinivas Kandagatla
2023-01-28 13:39 ` [PATCH 00/10] nvmem: fixes for 6.2 Greg KH
2023-01-30 11:03   ` Srinivas Kandagatla

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=20230127104015.23839-7-srinivas.kandagatla@linaro.org \
    --to=srinivas.kandagatla@linaro.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rmk+kernel@armlinux.org.uk \
    --cc=stable@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.