PCI/PTM: Fix memory leak in pcie_ptm_create_debugfs() error path
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.
--- drivers/pci/pcie/ptm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
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 ; + } ptm_debugfs->debugfs =debugfs_create_dir (dirname,NULL ); ptm_debugfs->pdata = pdata;
Signature · 1 line
2.35.6
Bjorn Helgaas
Applied[+cc Mani, author of 132833405e61]
Quoted 7 lines · Aadityarangan Shridhar Iyengar
In
pcie_ptm_create_debugfs(), ifdevm_kasprintf()fails after successfully allocatingptm_debugfswithkzalloc(), 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 thedevm_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")Signed-off-by Aadityarangan Shridhar Iyengar <[email protected]>
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@@ -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
Quoted 22 lines · Aadityarangan Shridhar Iyengar
In
pcie_ptm_create_debugfs(), ifdevm_kasprintf()fails after successfully allocatingptm_debugfswithkzalloc(), 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 thedevm_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")Signed-off-by Aadityarangan Shridhar Iyengar <[email protected]>--- drivers/pci/pcie/ptm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/pci/pcie/ptm.c +0
-0
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
@@ -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
மணிவண்ணன் சதாசிவம்
Quoted 49 lines · Manivannan Sadhasivam
Quoted 22 lines · Aadityarangan Shridhar Iyengar
In
pcie_ptm_create_debugfs(), ifdevm_kasprintf()fails after successfully allocatingptm_debugfswithkzalloc(), 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 thedevm_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")Signed-off-by Aadityarangan Shridhar Iyengar <[email protected]>--- drivers/pci/pcie/ptm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/pci/pcie/ptm.c +0
-0drivers/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@@ -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,
Reviewed-by Manivannan Sadhasivam <[email protected]>
Added the fix and your Reviewed-by, thanks!
PATCH 1/1 PCI/AER: Fix device reference leak in aer_inject()
In reply to Aadityarangan Shridhar IyengarThanks Bjorn, Mani,
Aditya