PCI/MSI: Fix memory leak in pci_alloc_irq_vectors_affinity INTx path
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
@@ -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
Quoted 17 lines · neilfsun
In the INTx fallback path of
pci_alloc_irq_vectors_affinity(), affinity masks are created and never freed.Signed-off-by neilfsun <[email protected]>Signed-off-by Sun Feng <[email protected]>--- drivers/pci/msi/api.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/pci/msi/api.c +0
-0@@ -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
@@ -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
-0pci_intx (dev,1 ); +kfree (masks);return 1 ; } }
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
@@ -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
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.Signed-off-by neilfsun <[email protected]>Signed-off-by Sun Feng <[email protected]>Reviewed-by Hans Zhang <[email protected]>
I don't see when Hans provided his tag in v1.
Quoted 11 lines
---
v2: use__freeto auto-release pointers--- drivers/pci/msi/api.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)drivers/pci/msi/api.c +0
-0
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 ; }
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
Signed-off-by neilfsun <[email protected]>
^^^^^^^^
Please provide your real name and not your nickname. See Documentation/process/...
Quoted 1 line
Signed-off-by Sun Feng <[email protected]>
This Signed-off-by chain is broken. See Documentation/process/...
Quoted 1 line
Reviewed-by Hans Zhang <[email protected]>
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