public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] PCI/MSI: Fix memory leak in pci_alloc_irq_vectors_affinity INTx path
@ 2026-04-20  4:09 neilfsun
  2026-04-20  4:47 ` Hans Zhang
  0 siblings, 1 reply; 5+ messages in thread
From: neilfsun @ 2026-04-20  4:09 UTC (permalink / raw)
  To: bhelgaas, shawn.lin, hans.zhang, tglx, linux-pci, linux-kernel
  Cc: neilfsun, Sun Feng

In the INTx fallback path of pci_alloc_irq_vectors_affinity(),
affinity masks are created and never freed.

Signed-off-by: neilfsun <neilfsun@tencent.com>
Signed-off-by: Sun Feng <loyou85@gmail.com>
---
 drivers/pci/msi/api.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/msi/api.c b/drivers/pci/msi/api.c
index c18559b6272c..c1377f74b8f3 100644
--- a/drivers/pci/msi/api.c
+++ b/drivers/pci/msi/api.c
@@ -280,14 +280,17 @@ int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
 	/* use INTx IRQ if allowed */
 	if (flags & PCI_IRQ_INTX) {
 		if (min_vecs == 1 && dev->irq) {
+			struct irq_affinity_desc *masks = NULL;
+
 			/*
 			 * Invoke the affinity spreading logic to ensure that
 			 * the device driver can adjust queue configuration
 			 * for the single interrupt case.
 			 */
 			if (affd)
-				irq_create_affinity_masks(1, affd);
+				masks = irq_create_affinity_masks(1, affd);
 			pci_intx(dev, 1);
+			kfree(masks);
 			return 1;
 		}
 	}
-- 
2.52.0


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

* Re: [PATCH] PCI/MSI: Fix memory leak in pci_alloc_irq_vectors_affinity INTx path
  2026-04-20  4:09 [PATCH] PCI/MSI: Fix memory leak in pci_alloc_irq_vectors_affinity INTx path neilfsun
@ 2026-04-20  4:47 ` Hans Zhang
  2026-04-21  3:20   ` [PATCH v2 v2] " neilfsun
  0 siblings, 1 reply; 5+ messages in thread
From: Hans Zhang @ 2026-04-20  4:47 UTC (permalink / raw)
  To: neilfsun, bhelgaas, shawn.lin, hans.zhang, tglx, linux-pci,
	linux-kernel
  Cc: neilfsun



On 4/20/26 12:09, neilfsun wrote:
> In the INTx fallback path of pci_alloc_irq_vectors_affinity(),
> affinity masks are created and never freed.
> 
> Signed-off-by: neilfsun <neilfsun@tencent.com>
> Signed-off-by: Sun Feng <loyou85@gmail.com>
> ---
>   drivers/pci/msi/api.c | 5 ++++-
>   1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/pci/msi/api.c b/drivers/pci/msi/api.c
> index c18559b6272c..c1377f74b8f3 100644
> --- a/drivers/pci/msi/api.c
> +++ b/drivers/pci/msi/api.c
> @@ -280,14 +280,17 @@ int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
>   	/* use INTx IRQ if allowed */
>   	if (flags & PCI_IRQ_INTX) {
>   		if (min_vecs == 1 && dev->irq) {
> +			struct irq_affinity_desc *masks = NULL;
> +
>   			/*
>   			 * Invoke the affinity spreading logic to ensure that
>   			 * the device driver can adjust queue configuration
>   			 * for the single interrupt case.
>   			 */
>   			if (affd)
> -				irq_create_affinity_masks(1, affd);
> +				masks = irq_create_affinity_masks(1, affd);

Hi,

Suggestion:

diff --git a/drivers/pci/msi/api.c b/drivers/pci/msi/api.c
index c18559b6272c..0ce34db2b2bf 100644
--- a/drivers/pci/msi/api.c
+++ b/drivers/pci/msi/api.c
@@ -286,7 +286,8 @@ int pci_alloc_irq_vectors_affinity(struct pci_dev 
*dev, unsigned int min_vecs,
                          * for the single interrupt case.
                          */
                         if (affd)
-                               irq_create_affinity_masks(1, affd);
+                               struct irq_affinity_desc *masks 
__free(kfree) =
+                                       irq_create_affinity_masks(1, affd);
                         pci_intx(dev, 1);
                         return 1;
                 }


Best regards,
Hans

>   			pci_intx(dev, 1);
> +			kfree(masks);
>   			return 1;
>   		}
>   	}


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

* [PATCH v2 v2] PCI/MSI: Fix memory leak in pci_alloc_irq_vectors_affinity INTx path
  2026-04-20  4:47 ` Hans Zhang
@ 2026-04-21  3:20   ` neilfsun
  2026-04-21  6:24     ` Shawn Lin
  2026-04-21  8:06     ` Thomas Gleixner
  0 siblings, 2 replies; 5+ messages in thread
From: neilfsun @ 2026-04-21  3:20 UTC (permalink / raw)
  To: 18255117159
  Cc: bhelgaas, hans.zhang, linux-kernel, linux-pci, loyou85, neilfsun,
	shawn.lin, tglx

In the INTx fallback path of pci_alloc_irq_vectors_affinity(),
affinity masks are created and never freed.

Signed-off-by: neilfsun <neilfsun@tencent.com>
Signed-off-by: Sun Feng <loyou85@gmail.com>
Reviewed-by: Hans Zhang <18255117159@163.com>
---
v2: use __free to auto-release pointers
---
 drivers/pci/msi/api.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/msi/api.c b/drivers/pci/msi/api.c
index c18559b6272c..994f12813ce3 100644
--- a/drivers/pci/msi/api.c
+++ b/drivers/pci/msi/api.c
@@ -285,8 +285,10 @@ int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
 			 * the device driver can adjust queue configuration
 			 * for the single interrupt case.
 			 */
-			if (affd)
-				irq_create_affinity_masks(1, affd);
+			if (affd) {
+				struct irq_affinity_desc *masks __free(kfree) =
+					irq_create_affinity_masks(1, affd);
+			}
 			pci_intx(dev, 1);
 			return 1;
 		}
-- 
2.52.0


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

* Re: [PATCH v2 v2] PCI/MSI: Fix memory leak in pci_alloc_irq_vectors_affinity INTx path
  2026-04-21  3:20   ` [PATCH v2 v2] " neilfsun
@ 2026-04-21  6:24     ` Shawn Lin
  2026-04-21  8:06     ` Thomas Gleixner
  1 sibling, 0 replies; 5+ messages in thread
From: Shawn Lin @ 2026-04-21  6:24 UTC (permalink / raw)
  To: neilfsun
  Cc: shawn.lin, bhelgaas, hans.zhang, linux-kernel, linux-pci,
	neilfsun, tglx, 18255117159

On 2026/04/21 Tuesday 11:20, neilfsun Wrote:
> In the INTx fallback path of pci_alloc_irq_vectors_affinity(),
> affinity masks are created and never freed.
> 
> Signed-off-by: neilfsun <neilfsun@tencent.com>
> Signed-off-by: Sun Feng <loyou85@gmail.com>
> Reviewed-by: Hans Zhang <18255117159@163.com>

I don't see when Hans provided his tag in v1.

> ---
> v2: use __free to auto-release pointers
> ---
>   drivers/pci/msi/api.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/pci/msi/api.c b/drivers/pci/msi/api.c
> index c18559b6272c..994f12813ce3 100644
> --- a/drivers/pci/msi/api.c
> +++ b/drivers/pci/msi/api.c
> @@ -285,8 +285,10 @@ int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
>   			 * the device driver can adjust queue configuration
>   			 * for the single interrupt case.
>   			 */
> -			if (affd)
> -				irq_create_affinity_masks(1, affd);
> +			if (affd) {
> +				struct irq_affinity_desc *masks __free(kfree) =
> +					irq_create_affinity_masks(1, affd);


The __free() attribute is typically used for ensuring cleanup across
multiple exit paths​ within a function. In this case, the allocated masks
is not used and its scope ends immediately after creation. Why can't
we simply kfree(irq_create_affinity_masks(1, affd))...


> +			}
>   			pci_intx(dev, 1);
>   			return 1;
>   		}

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

* Re: [PATCH v2 v2] PCI/MSI: Fix memory leak in pci_alloc_irq_vectors_affinity INTx path
  2026-04-21  3:20   ` [PATCH v2 v2] " neilfsun
  2026-04-21  6:24     ` Shawn Lin
@ 2026-04-21  8:06     ` Thomas Gleixner
  1 sibling, 0 replies; 5+ messages in thread
From: Thomas Gleixner @ 2026-04-21  8:06 UTC (permalink / raw)
  To: neilfsun, 18255117159
  Cc: bhelgaas, hans.zhang, linux-kernel, linux-pci, loyou85, neilfsun,
	shawn.lin

On Tue, Apr 21 2026 at 11:20, neilfsun wrote:

Please use function() notation in the subject as well.

> In the INTx fallback path of pci_alloc_irq_vectors_affinity(),
> affinity masks are created and never freed.

And then?

https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#changelog


This also lacks a Fixes: tag to identify the commit which introduced the
problem. Hint: It was not commit beddb5efb43ee.

> Signed-off-by: neilfsun <neilfsun@tencent.com>
                 ^^^^^^^^
Please provide your real name and not your nickname. See
Documentation/process/...

> Signed-off-by: Sun Feng <loyou85@gmail.com>

This Signed-off-by chain is broken. See Documentation/process/...

> Reviewed-by: Hans Zhang <18255117159@163.com>

Hans did not provide you a Reviewed-by tag. The fact that he reviewed V1
and made suggestions how to improve does not imply that.

> @@ -285,8 +285,10 @@ int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs,
>  			 * the device driver can adjust queue configuration
>  			 * for the single interrupt case.
>  			 */
> -			if (affd)
> -				irq_create_affinity_masks(1, affd);
> +			if (affd) {
> +				struct irq_affinity_desc *masks __free(kfree) =
> +					irq_create_affinity_masks(1, affd);

There is no point to use __free() here. The only reason why this is
invoked for the INTX case is to ensure that the affinity descriptor is
updated and an eventually provided calc_sets() callback is invoked. The
returned mask is not used at all, so this can be simplified to:

         kfree(affd ? irq_create_affinity_masks(1, affd) : NULL);

Along with a proper comment.

Thanks,

        tglx

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

end of thread, other threads:[~2026-04-21  8:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-20  4:09 [PATCH] PCI/MSI: Fix memory leak in pci_alloc_irq_vectors_affinity INTx path neilfsun
2026-04-20  4:47 ` Hans Zhang
2026-04-21  3:20   ` [PATCH v2 v2] " neilfsun
2026-04-21  6:24     ` Shawn Lin
2026-04-21  8:06     ` Thomas Gleixner

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