Linux PCI subsystem development
 help / color / mirror / Atom feed
* [PATCH] PCI/PTM: Fix memory leak in pcie_ptm_create_debugfs() error path
@ 2026-01-11 16:30 Aadityarangan Shridhar Iyengar
  0 siblings, 0 replies; 6+ messages in thread
From: Aadityarangan Shridhar Iyengar @ 2026-01-11 16:30 UTC (permalink / raw)
  To: bhelgass; +Cc: linux-pci, linux-kernel, 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")
Signed-off-by: Aadityarangan Shridhar Iyengar <adiyenga@cisco.com>
---
 drivers/pci/pcie/ptm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/pcie/ptm.c b/drivers/pci/pcie/ptm.c
index ed0f9691e7d1..09c0167048a3 100644
--- a/drivers/pci/pcie/ptm.c
+++ b/drivers/pci/pcie/ptm.c
@@ -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;
-- 
2.35.6


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH] PCI/PTM: Fix memory leak in pcie_ptm_create_debugfs() error path
@ 2026-01-11 16:36 Aadityarangan Shridhar Iyengar
  2026-01-13 18:59 ` Bjorn Helgaas
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Aadityarangan Shridhar Iyengar @ 2026-01-11 16:36 UTC (permalink / raw)
  To: bhelgaas; +Cc: linux-pci, linux-kernel, 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")
Signed-off-by: Aadityarangan Shridhar Iyengar <adiyenga@cisco.com>
---
 drivers/pci/pcie/ptm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/pcie/ptm.c b/drivers/pci/pcie/ptm.c
index ed0f9691e7d1..09c0167048a3 100644
--- a/drivers/pci/pcie/ptm.c
+++ b/drivers/pci/pcie/ptm.c
@@ -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;
-- 
2.35.6


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] PCI/PTM: Fix memory leak in pcie_ptm_create_debugfs() error path
  2026-01-11 16:36 [PATCH] PCI/PTM: Fix memory leak in pcie_ptm_create_debugfs() error path Aadityarangan Shridhar Iyengar
@ 2026-01-13 18:59 ` Bjorn Helgaas
  2026-01-14  7:28 ` Manivannan Sadhasivam
  2026-01-14 16:26 ` [PATCH] PCI/AER: Fix device reference leak in aer_inject() Aadityarangan Shridhar Iyengar
  2 siblings, 0 replies; 6+ messages in thread
From: Bjorn Helgaas @ 2026-01-13 18:59 UTC (permalink / raw)
  To: Aadityarangan Shridhar Iyengar
  Cc: bhelgaas, linux-pci, linux-kernel, Manivannan Sadhasivam

[+cc Mani, author of 132833405e61]

On Sun, Jan 11, 2026 at 10:06:50PM +0530, Aadityarangan Shridhar Iyengar wrote:
> 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")
> Signed-off-by: Aadityarangan Shridhar Iyengar <adiyenga@cisco.com>

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

> ---
>  drivers/pci/pcie/ptm.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/pci/pcie/ptm.c b/drivers/pci/pcie/ptm.c
> index ed0f9691e7d1..09c0167048a3 100644
> --- a/drivers/pci/pcie/ptm.c
> +++ b/drivers/pci/pcie/ptm.c
> @@ -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;
> -- 
> 2.35.6
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] PCI/PTM: Fix memory leak in pcie_ptm_create_debugfs() error path
  2026-01-11 16:36 [PATCH] PCI/PTM: Fix memory leak in pcie_ptm_create_debugfs() error path Aadityarangan Shridhar Iyengar
  2026-01-13 18:59 ` Bjorn Helgaas
@ 2026-01-14  7:28 ` Manivannan Sadhasivam
  2026-01-14 16:39   ` Bjorn Helgaas
  2026-01-14 16:26 ` [PATCH] PCI/AER: Fix device reference leak in aer_inject() Aadityarangan Shridhar Iyengar
  2 siblings, 1 reply; 6+ messages in thread
From: Manivannan Sadhasivam @ 2026-01-14  7:28 UTC (permalink / raw)
  To: Aadityarangan Shridhar Iyengar; +Cc: bhelgaas, linux-pci, linux-kernel

On Sun, Jan 11, 2026 at 10:06:50PM +0530, Aadityarangan Shridhar Iyengar wrote:
> 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")
> Signed-off-by: Aadityarangan Shridhar Iyengar <adiyenga@cisco.com>
> ---
>  drivers/pci/pcie/ptm.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/pci/pcie/ptm.c b/drivers/pci/pcie/ptm.c
> index ed0f9691e7d1..09c0167048a3 100644
> --- a/drivers/pci/pcie/ptm.c
> +++ b/drivers/pci/pcie/ptm.c
> @@ -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?

diff --git a/drivers/pci/pcie/ptm.c b/drivers/pci/pcie/ptm.c
index ed0f9691e7d1..2c27bee0773d 100644
--- a/drivers/pci/pcie/ptm.c
+++ b/drivers/pci/pcie/ptm.c
@@ -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,

Reviewed-by: Manivannan Sadhasivam <mani@kernel.org>

- Mani

-- 
மணிவண்ணன் சதாசிவம்

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH] PCI/AER: Fix device reference leak in aer_inject()
  2026-01-11 16:36 [PATCH] PCI/PTM: Fix memory leak in pcie_ptm_create_debugfs() error path Aadityarangan Shridhar Iyengar
  2026-01-13 18:59 ` Bjorn Helgaas
  2026-01-14  7:28 ` Manivannan Sadhasivam
@ 2026-01-14 16:26 ` Aadityarangan Shridhar Iyengar
  2 siblings, 0 replies; 6+ messages in thread
From: Aadityarangan Shridhar Iyengar @ 2026-01-14 16:26 UTC (permalink / raw)
  To: bhelgaas, mani; +Cc: linux-pci, linux-kernel

Thanks Bjorn, Mani,
Aditya

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] PCI/PTM: Fix memory leak in pcie_ptm_create_debugfs() error path
  2026-01-14  7:28 ` Manivannan Sadhasivam
@ 2026-01-14 16:39   ` Bjorn Helgaas
  0 siblings, 0 replies; 6+ messages in thread
From: Bjorn Helgaas @ 2026-01-14 16:39 UTC (permalink / raw)
  To: Manivannan Sadhasivam
  Cc: Aadityarangan Shridhar Iyengar, bhelgaas, linux-pci, linux-kernel

On Wed, Jan 14, 2026 at 12:58:56PM +0530, Manivannan Sadhasivam wrote:
> On Sun, Jan 11, 2026 at 10:06:50PM +0530, Aadityarangan Shridhar Iyengar wrote:
> > 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")
> > Signed-off-by: Aadityarangan Shridhar Iyengar <adiyenga@cisco.com>
> > ---
> >  drivers/pci/pcie/ptm.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/pci/pcie/ptm.c b/drivers/pci/pcie/ptm.c
> > index ed0f9691e7d1..09c0167048a3 100644
> > --- a/drivers/pci/pcie/ptm.c
> > +++ b/drivers/pci/pcie/ptm.c
> > @@ -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?
> 
> diff --git a/drivers/pci/pcie/ptm.c b/drivers/pci/pcie/ptm.c
> index ed0f9691e7d1..2c27bee0773d 100644
> --- a/drivers/pci/pcie/ptm.c
> +++ b/drivers/pci/pcie/ptm.c
> @@ -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,
> 
> Reviewed-by: Manivannan Sadhasivam <mani@kernel.org>

Added the fix and your Reviewed-by, thanks!

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-01-14 16:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-11 16:36 [PATCH] PCI/PTM: Fix memory leak in pcie_ptm_create_debugfs() error path Aadityarangan Shridhar Iyengar
2026-01-13 18:59 ` Bjorn Helgaas
2026-01-14  7:28 ` Manivannan Sadhasivam
2026-01-14 16:39   ` Bjorn Helgaas
2026-01-14 16:26 ` [PATCH] PCI/AER: Fix device reference leak in aer_inject() Aadityarangan Shridhar Iyengar
  -- strict thread matches above, loose matches on Subject: below --
2026-01-11 16:30 [PATCH] PCI/PTM: Fix memory leak in pcie_ptm_create_debugfs() error path Aadityarangan Shridhar Iyengar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox