Skip to contentSkip to conversation

nvmem: core: Fix race in nvmem_register()

PATCH Superseded Hector Martin 3 participants 3 messages Dec 14, 2022 – Jan 3, 2023 NVMEM framework

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmem: core: Fix race in nvmem_register()

nvmem_register() currently registers the device before adding the nvmem cells, which creates a race window where consumers may find the nvmem device (and not get PROBE_DEFERred), but then fail to find the cells and error out.

Move device registration to the end of nvmem_register(), to close the race.

Observed when the stars line up on Apple Silicon machines with the (not yet upstream, but trivial) spmi nvmem driver and the macsmc-rtc client:

[    0.487375] macsmc-rtc macsmc-rtc: error -ENOENT: Failed to get rtc_offset NVMEM cell
Fixes eace75cfdcf7 ("nvmem: Add a simple NVMEM framework for nvmem providers")
---
 drivers/nvmem/core.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/nvmem/core.c +8 -10
index 321d7d63e068..d255feca5e17 100644
@@ -837,20 +837,16 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 
 	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;
+			return ERR_PTR(rval);
 	}
 
 	if (config->compat) {
 		rval = nvmem_sysfs_setup_compat(nvmem, config);
 		if (rval)
-			goto err_device_del;
+			return ERR_PTR(rval);
 	}
 
 	if (config->cells) {
@@ -867,19 +863,21 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	if (rval)
 		goto err_remove_cells;
 
+	rval = device_register(&nvmem->dev);
+	if (rval)
+		goto err_put_device;
+
 	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
 
 	return nvmem;
 
+err_put_device:
+	put_device(&nvmem->dev);
 err_remove_cells:
 	nvmem_device_remove_all_cells(nvmem);
 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);
 
 	return ERR_PTR(rval);
 }

--
2.35.1

Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Hector Martin

nvmem_register() currently registers the device before adding the nvmem cells, which creates a race window where consumers may find the nvmem device (and not get PROBE_DEFERred), but then fail to find the cells and error out.

Move device registration to the end of nvmem_register(), to close the race.

Quoted 7 lines

Observed when the stars line up on Apple Silicon machines with the (not yet upstream, but trivial) spmi nvmem driver and the macsmc-rtc client:

[    0.487375] macsmc-rtc macsmc-rtc: error -ENOENT: Failed to get rtc_offset NVMEM cell
Fixes eace75cfdcf7 ("nvmem: Add a simple NVMEM framework for nvmem providers")
Quoted 61 lines
---
 drivers/nvmem/core.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
drivers/nvmem/core.c +8 -11
index 321d7d63e068..d255feca5e17 100644
@@ -837,20 +837,16 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 
        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;
+                       return ERR_PTR(rval);
        }
 
        if (config->compat) {
                rval = nvmem_sysfs_setup_compat(nvmem, config);
                if (rval)
-                       goto err_device_del;
+                       return ERR_PTR(rval);
        }
 
        if (config->cells) {
@@ -867,19 +863,21 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
        if (rval)
                goto err_remove_cells;
 
+       rval = device_register(&nvmem->dev);
+       if (rval)
+               goto err_put_device;
+
        blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
 
        return nvmem;
 
+err_put_device:
+       put_device(&nvmem->dev);
 err_remove_cells:
        nvmem_device_remove_all_cells(nvmem);
 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);
 
        return ERR_PTR(rval);
 }
--

2.35.1

#2 lore raw
In reply to Hector Martin

Thanks Hector for the patch,

Quoted 8 lines · Hector Martin

nvmem_register() currently registers the device before adding the nvmem cells, which creates a race window where consumers may find the nvmem device (and not get PROBE_DEFERred), but then fail to find the cells and error out.

Move device registration to the end of nvmem_register(), to close the race.

Quoted 7 lines

Observed when the stars line up on Apple Silicon machines with the (not yet upstream, but trivial) spmi nvmem driver and the macsmc-rtc client:

[    0.487375] macsmc-rtc macsmc-rtc: error -ENOENT: Failed to get rtc_offset NVMEM cell
Fixes eace75cfdcf7 ("nvmem: Add a simple NVMEM framework for nvmem providers")

Missing Cc Stable

Quoted 11 lines
---
  drivers/nvmem/core.c | 18 ++++++++----------
  1 file changed, 8 insertions(+), 10 deletions(-)
drivers/nvmem/core.c +0 -0
index 321d7d63e068..d255feca5e17 100644
@@ -837,20 +837,16 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 
  	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 843

Quoted 10 lines
drivers/nvmem/core.c +1 -5
 
-	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;
+			return ERR_PTR(rval);

you can not do this as this will leak ida and nvmem itself.

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

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;
+			return ERR_PTR(rval);

same here.

Quoted 7 lines
drivers/nvmem/core.c +0 -0
  	}
 
  	if (config->cells) {
@@ -867,19 +863,21 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
  	if (rval)
  		goto err_remove_cells;
 

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

Quoted 10 lines
drivers/nvmem/core.c +6 -0
+	rval = device_register(&nvmem->dev);
+	if (rval)
+		goto err_put_device;
+
  	blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
 
  	return nvmem;
 
+err_put_device:
+	put_device(&nvmem->dev);

this will free nvmem and further down you are going to de-reference the freed pointer.

--srini

Quoted 15 lines
drivers/nvmem/core.c +0 -5
  err_remove_cells:
  	nvmem_device_remove_all_cells(nvmem);
  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);
 
  	return ERR_PTR(rval);
  }
--

2.35.1