Skip to contentSkip to conversation

PCI/PTM: Fix memory leak in pcie_ptm_create_debugfs() error path

PATCH Applied Aadityarangan Shridhar Iyengar 3 participants 5 messages Jan 11 – Jan 14 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI/PTM: Fix memory leak in pcie_ptm_create_debugfs() error path

In pcie_ptm_create_debugfs(), if devm_kasprintf() fails after successfully allocating ptm_debugfs with kzalloc(), the function returns NULL without freeing the allocated memory, resulting in a memory leak.

Fix this by adding kfree(ptm_debugfs) before returning NULL in the devm_kasprintf() error path.

This leak is particularly problematic during memory pressure situations where devm_kasprintf() is more likely to fail, potentially compounding the memory exhaustion issue.

Fixes 132833405e61 ("PCI: Add debugfs support for exposing PTM context")
---
 drivers/pci/pcie/ptm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci/pcie/ptm.c +3 -1
index ed0f9691e7d1..09c0167048a3 100644
@@ -542,8 +542,10 @@ struct pci_ptm_debugfs *pcie_ptm_create_debugfs(struct device *dev, void *pdata,
 		return NULL;
 
 	dirname = devm_kasprintf(dev, GFP_KERNEL, "pcie_ptm_%s", dev_name(dev));
-	if (!dirname)
+	if (!dirname) {
+		kfree(ptm_debugfs);
 		return NULL;
+	}
 
 	ptm_debugfs->debugfs = debugfs_create_dir(dirname, NULL);
 	ptm_debugfs->pdata = pdata;
Signature · 1 line
2.35.6
#1 lore raw

[+cc Mani, author of 132833405e61]

Quoted 7 lines · Aadityarangan Shridhar Iyengar

In pcie_ptm_create_debugfs(), if devm_kasprintf() fails after successfully allocating ptm_debugfs with kzalloc(), the function returns NULL without freeing the allocated memory, resulting in a memory leak.

Fix this by adding kfree(ptm_debugfs) before returning NULL in the devm_kasprintf() error path.

Quoted 6 lines

This leak is particularly problematic during memory pressure situations where devm_kasprintf() is more likely to fail, potentially compounding the memory exhaustion issue.

Fixes 132833405e61 ("PCI: Add debugfs support for exposing PTM context")

Applied to pci/ptm for v6.20, thanks!

Quoted 23 lines
---
 drivers/pci/pcie/ptm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci/pcie/ptm.c +3 -1
index ed0f9691e7d1..09c0167048a3 100644
@@ -542,8 +542,10 @@ struct pci_ptm_debugfs *pcie_ptm_create_debugfs(struct device *dev, void *pdata,
 		return NULL;
 
 	dirname = devm_kasprintf(dev, GFP_KERNEL, "pcie_ptm_%s", dev_name(dev));
-	if (!dirname)
+	if (!dirname) {
+		kfree(ptm_debugfs);
 		return NULL;
+	}
 
 	ptm_debugfs->debugfs = debugfs_create_dir(dirname, NULL);
 	ptm_debugfs->pdata = pdata;
Signature · 1 line
2.35.6
Reviewed-by on v1 1/1
#2 lore raw
In reply to Aadityarangan Shridhar Iyengar
Quoted 22 lines · Aadityarangan Shridhar Iyengar

In pcie_ptm_create_debugfs(), if devm_kasprintf() fails after successfully allocating ptm_debugfs with kzalloc(), the function returns NULL without freeing the allocated memory, resulting in a memory leak.

Fix this by adding kfree(ptm_debugfs) before returning NULL in the devm_kasprintf() error path.

This leak is particularly problematic during memory pressure situations where devm_kasprintf() is more likely to fail, potentially compounding the memory exhaustion issue.

Fixes 132833405e61 ("PCI: Add debugfs support for exposing PTM context")
---
 drivers/pci/pcie/ptm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci/pcie/ptm.c +0 -0
index ed0f9691e7d1..09c0167048a3 100644

drivers/pci/pcie/ptm.c struct pci_ptm_debugfs *pcie_ptm_create_debugfs(struct device *dev, void *pdata, · line 548

Quoted 8 lines
drivers/pci/pcie/ptm.c +3 -1
@@ -542,8 +542,10 @@ struct pci_ptm_debugfs *pcie_ptm_create_debugfs(struct device *dev, void *pdata,
 		return NULL;
 
 	dirname = devm_kasprintf(dev, GFP_KERNEL, "pcie_ptm_%s", dev_name(dev));
-	if (!dirname)
+	if (!dirname) {
+		kfree(ptm_debugfs);
 		return NULL;
+	}

Thanks for spotting the leak. I also forgot to remove it in pcie_ptm_destroy_debugfs(). Since this one got applied, Bjorn could you please squash the below fix as well?

drivers/pci/pcie/ptm.c +1 -0
index ed0f9691e7d1..2c27bee0773d 100644
@@ -574,6 +574,7 @@ void pcie_ptm_destroy_debugfs(struct pci_ptm_debugfs *ptm_debugfs)
 
        mutex_destroy(&ptm_debugfs->lock);
        debugfs_remove_recursive(ptm_debugfs->debugfs);
+       kfree(ptm_debugfs);
 }
 EXPORT_SYMBOL_GPL(pcie_ptm_destroy_debugfs);
 #endif

For this patch,

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
#3 lore raw
Quoted 49 lines · Manivannan Sadhasivam
Quoted 22 lines · Aadityarangan Shridhar Iyengar

In pcie_ptm_create_debugfs(), if devm_kasprintf() fails after successfully allocating ptm_debugfs with kzalloc(), the function returns NULL without freeing the allocated memory, resulting in a memory leak.

Fix this by adding kfree(ptm_debugfs) before returning NULL in the devm_kasprintf() error path.

This leak is particularly problematic during memory pressure situations where devm_kasprintf() is more likely to fail, potentially compounding the memory exhaustion issue.

Fixes 132833405e61 ("PCI: Add debugfs support for exposing PTM context")
---
 drivers/pci/pcie/ptm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/pci/pcie/ptm.c +0 -0
index ed0f9691e7d1..09c0167048a3 100644

drivers/pci/pcie/ptm.c struct pci_ptm_debugfs *pcie_ptm_create_debugfs(struct device *dev, void *pdata, · line 548

Quoted 8 lines
drivers/pci/pcie/ptm.c +3 -1
@@ -542,8 +542,10 @@ struct pci_ptm_debugfs *pcie_ptm_create_debugfs(struct device *dev, void *pdata,
 		return NULL;
 
 	dirname = devm_kasprintf(dev, GFP_KERNEL, "pcie_ptm_%s", dev_name(dev));
-	if (!dirname)
+	if (!dirname) {
+		kfree(ptm_debugfs);
 		return NULL;
+	}

Thanks for spotting the leak. I also forgot to remove it in pcie_ptm_destroy_debugfs(). Since this one got applied, Bjorn could you please squash the below fix as well?

drivers/pci/pcie/ptm.c +1 -0
index ed0f9691e7d1..2c27bee0773d 100644
@@ -574,6 +574,7 @@ void pcie_ptm_destroy_debugfs(struct pci_ptm_debugfs *ptm_debugfs)
 
        mutex_destroy(&ptm_debugfs->lock);
        debugfs_remove_recursive(ptm_debugfs->debugfs);
+       kfree(ptm_debugfs);
 }
 EXPORT_SYMBOL_GPL(pcie_ptm_destroy_debugfs);
 #endif
Quoted 3 lines

For this patch,

Added the fix and your Reviewed-by, thanks!