Skip to contentSkip to conversation

nvmem: core: Prevent memory leak when device is unregistered

PATCH In review Mika Westerberg 4 participants 5 messages May 15, 2017 – May 17, 2017 NVMEM framework

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmem: core: Prevent memory leak when device is unregistered

The nvmem_unregister() calls device_del() for the device but forgets to call put_device() to actually release the device object which causes that memory to be leaked.

Fix this by calling device_unregister() for the device intead which also calls put_device() for the device releasing it eventually.

---
 drivers/nvmem/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvmem/core.c +1 -1
index 8c830a80a648..112c8072e0f3 100644
@@ -528,7 +528,7 @@ int nvmem_unregister(struct nvmem_device *nvmem)
 		device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
 
 	nvmem_device_remove_all_cells(nvmem);
-	device_del(&nvmem->dev);
+	device_unregister(&nvmem->dev);
 
 	return 0;
 }
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Mika Westerberg

The nvmem_unregister() calls device_del() for the device but forgets to call put_device() to actually release the device object which causes that memory to be leaked.

Fix this by calling device_unregister() for the device intead which also

instead

Quoted 2 lines

calls put_device() for the device releasing it eventually.

Other than that, FWIW:

Quoted 21 lines
---
 drivers/nvmem/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvmem/core.c +1 -2
index 8c830a80a648..112c8072e0f3 100644
@@ -528,7 +528,7 @@ int nvmem_unregister(struct nvmem_device *nvmem)
                device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
 
        nvmem_device_remove_all_cells(nvmem);
-       device_del(&nvmem->dev);
+       device_unregister(&nvmem->dev);
 
        return 0;
 }
--

2.11.0

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
In reply to Mika Westerberg
Quoted 6 lines · Mika Westerberg

The nvmem_unregister() calls device_del() for the device but forgets to call put_device() to actually release the device object which causes that memory to be leaked.

Fix this by calling device_unregister() for the device intead which also calls put_device() for the device releasing it eventually.

I was gonna suggest that you fix up the related leaks in the registration error paths as well, but since they are really distinct I just submitted a patch to fix those up separately instead (kfree was being just instead of put_device and the device was never deregistered on late probe errors).

Quoted 10 lines
---
 drivers/nvmem/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvmem/core.c +0 -0
index 8c830a80a648..112c8072e0f3 100644

drivers/nvmem/core.c int nvmem_unregister(struct nvmem_device *nvmem) · line 531

Quoted 5 lines
drivers/nvmem/core.c +1 -1
@@ -528,7 +528,7 @@ int nvmem_unregister(struct nvmem_device *nvmem)
 		device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
 
 	nvmem_device_remove_all_cells(nvmem);
-	device_del(&nvmem->dev);
+	device_unregister(&nvmem->dev);

Might be cleaner to use an explicit call to put_device() here since the driver currently does not use device_register().

drivers/nvmem/core.c int nvmem_unregister(struct nvmem_device *nvmem) · line 534

Quoted 3 lines
drivers/nvmem/core.c +0 -0
 	return 0;
 }

Thanks,
Johan

#3 lore raw
In reply to Mika Westerberg
Quoted 9 lines · Mika Westerberg

The nvmem_unregister() calls device_del() for the device but forgets to call put_device() to actually release the device object which causes that memory to be leaked.

Fix this by calling device_unregister() for the device intead which also calls put_device() for the device releasing it eventually.

---

FWIW, I submitted similar fix for this here:

lkml.kernel.org/r/[email protected]

Quoted 19 lines
 drivers/nvmem/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvmem/core.c +1 -2
index 8c830a80a648..112c8072e0f3 100644
@@ -528,7 +528,7 @@ int nvmem_unregister(struct nvmem_device *nvmem)
                device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
 
        nvmem_device_remove_all_cells(nvmem);
-       device_del(&nvmem->dev);
+       device_unregister(&nvmem->dev);
 
        return 0;
 }
--

2.11.0

#4 lore raw
Quoted 9 lines · Andrey Smirnov
Quoted 7 lines · Mika Westerberg

The nvmem_unregister() calls device_del() for the device but forgets to call put_device() to actually release the device object which causes that memory to be leaked.

Fix this by calling device_unregister() for the device intead which also calls put_device() for the device releasing it eventually.

Quoted 6 lines
Quoted 2 lines

---

FWIW, I submitted similar fix for this here:

lkml.kernel.org/r/[email protected]

Cool, let's go with that one instead then :)