Skip to contentSkip to conversation

PCI/MSI: Fix memory leak in pci_alloc_irq_vectors_affinity INTx path

PATCH Superseded neilfsun 4 participants 5 messages Apr 20 – Apr 21 PCI

lore
Share & tools
lore

neilfsun

author
#0 lore raw

PATCH 1/1 PCI/MSI: Fix memory leak in pci_alloc_irq_vectors_affinity INTx path

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

---
 drivers/pci/msi/api.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pci/msi/api.c +4 -1
index c18559b6272c..c1377f74b8f3 100644
@@ -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;
 		}
 	}
Signature · 1 line
2.52.0
#1 lore raw
Quoted 17 lines · neilfsun

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

---
  drivers/pci/msi/api.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/pci/msi/api.c +0 -0
index c18559b6272c..c1377f74b8f3 100644
@@ -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) {

drivers/pci/msi/api.c int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs, · line 291

Quoted 10 lines
drivers/pci/msi/api.c +3 -1
+			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:

drivers/pci/msi/api.c +2 -1
index c18559b6272c..0ce34db2b2bf 100644
@@ -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

Quoted 5 lines
drivers/pci/msi/api.c +1 -0
  			pci_intx(dev, 1);
+			kfree(masks);
  			return 1;
  		}
  	}
author
#2 lore raw

PATCH v2 1/1 PCI/MSI: Fix memory leak in pci_alloc_irq_vectors_affinity INTx path

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

---
v2: use __free to auto-release pointers

---
 drivers/pci/msi/api.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pci/msi/api.c +4 -2
index c18559b6272c..994f12813ce3 100644
@@ -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;
 		}
Signature · 1 line
2.52.0
#3 lore raw

On 2026/04/21 Tuesday 11:20, neilfsun Wrote:

Quoted 6 lines

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

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

Quoted 11 lines

---
v2: use __free to auto-release pointers

---
  drivers/pci/msi/api.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pci/msi/api.c +0 -0
index c18559b6272c..994f12813ce3 100644

drivers/pci/msi/api.c int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs, · line 290

Quoted 8 lines
drivers/pci/msi/api.c +3 -2
@@ -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))...

Quoted 4 lines
drivers/pci/msi/api.c +1 -0
+			}
  			pci_intx(dev, 1);
  			return 1;
  		}
#4 lore raw
In reply to neilfsun

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

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

Quoted 2 lines

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.

Quoted 1 line
                 ^^^^^^^^

Please provide your real name and not your nickname. See Documentation/process/...

Quoted 1 line

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

Quoted 1 line

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.

drivers/pci/msi/api.c int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs, · line 290

Quoted 9 lines
drivers/pci/msi/api.c +3 -2
@@ -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