Skip to contentSkip to conversation

nvmem: fix registration vs use race

PATCH In review Russell King (Oracle) 3 participants 3 messages Jan 3, 2023 NVMEM framework

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 nvmem: fix registration vs use race

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.

Fixes eace75cfdcf7 ("nvmem: Add a simple NVMEM framework for nvmem providers")

---
v2: add fixes tag

 drivers/nvmem/core.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/nvmem/core.c +8 -10
index 321d7d63e068..6b89fb6fa582 100644
@@ -835,22 +835,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_register(&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) {
@@ -867,6 +861,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_register(&nvmem->dev);
+	if (rval)
+		goto err_remove_cells;
+
 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
 
 	return nvmem;
@@ -876,8 +876,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);
 
Signature · 1 line
2.30.2
#1 lore raw
Quoted 30 lines · Russell King (Oracle)

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.

Fixes eace75cfdcf7 ("nvmem: Add a simple NVMEM framework for nvmem providers")

---
v2: add fixes tag

 drivers/nvmem/core.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/nvmem/core.c +0 -1
index 321d7d63e068..6b89fb6fa582 100644
@@ -835,22 +835,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);

drivers/nvmem/core.c struct nvmem_device *nvmem_register(const struct nvmem_config *config) · line 841

Quoted 10 lines
drivers/nvmem/core.c +1 -6
-
-	rval = device_register(&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;

You can't call put_device() on a device that hasn't gone through device_initialize() yet.

drivers/nvmem/core.c struct nvmem_device *nvmem_register(const struct nvmem_config *config) · line 847

Quoted 7 lines
drivers/nvmem/core.c +1 -1
 	}
 
 	if (config->compat) {
 		rval = nvmem_sysfs_setup_compat(nvmem, config);
 		if (rval)
-			goto err_device_del;
+			goto err_put_device;

Ditto.

Quoted 17 lines
drivers/nvmem/core.c +6 -0
 	}
 
 	if (config->cells) {
@@ -867,6 +861,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_register(&nvmem->dev);
+	if (rval)
+		goto err_remove_cells;
+
 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
 
 	return nvmem;

drivers/nvmem/core.c struct nvmem_device *nvmem_register(const struct nvmem_config *config) · line 881

Quoted 8 lines
drivers/nvmem/core.c +0 -2
@@ -876,8 +876,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);
 

- Hector

#2 lore raw
Quoted 30 lines · Hector Martin
Quoted 29 lines · Russell King (Oracle)

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.

Fixes eace75cfdcf7 ("nvmem: Add a simple NVMEM framework for nvmem providers")

---
v2: add fixes tag

 drivers/nvmem/core.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/nvmem/core.c +0 -0
index 321d7d63e068..6b89fb6fa582 100644
@@ -835,22 +835,16 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	nvmem->dev.groups = nvmem_dev_groups;
 #endif
 
Quoted 14 lines

drivers/nvmem/core.c struct nvmem_device *nvmem_register(const struct nvmem_config *config) · line 841

Quoted 11 lines
drivers/nvmem/core.c +1 -7
-	dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name);
-
-	rval = device_register(&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;

You can't call put_device() on a device that hasn't gone through device_initialize() yet.

Right, which is what I just realised while writing the previous reply. We need to use device_initialize() and device_add(), so we can call put_device() on it.

Error paths are difficult to properly test. :(

Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!