Patchwork x86/PCI: fix a memory leak bug

login
register
mail settings
Submitter Wenwen Wang
Date April 15, 2019, 2:54 p.m.
Message ID <1555340058-3065-1-git-send-email-wang6495@umn.edu>
Download mbox | patch
Permalink /patch/773237/
State New
Headers show

Comments

Wenwen Wang - April 15, 2019, 2:54 p.m.
In pcibios_irq_init(), the PCI IRQ routing table 'pirq_table' is firstly
found through pirq_find_routing_table(). If the table is not found and
'CONFIG_PCI_BIOS' is defined, the table is then allocated in
pcibios_get_irq_routing_table() using kmalloc(). In the following
execution, if the I/O APIC is used, this table is actually not used.
However, in that case, the allocated table is not freed, which can lead to
a memory leak bug.

To fix this issue, this patch frees the allocated table if it is not used.

Signed-off-by: Wenwen Wang <wang6495@umn.edu>
---
 arch/x86/pci/irq.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
Ingo Molnar - April 16, 2019, 7:23 a.m.
* Wenwen Wang <wang6495@umn.edu> wrote:

> In pcibios_irq_init(), the PCI IRQ routing table 'pirq_table' is firstly
> found through pirq_find_routing_table(). If the table is not found and
> 'CONFIG_PCI_BIOS' is defined, the table is then allocated in
> pcibios_get_irq_routing_table() using kmalloc(). In the following
> execution, if the I/O APIC is used, this table is actually not used.
> However, in that case, the allocated table is not freed, which can lead to
> a memory leak bug.
> 
> To fix this issue, this patch frees the allocated table if it is not used.
> 
> Signed-off-by: Wenwen Wang <wang6495@umn.edu>
> ---
>  arch/x86/pci/irq.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/pci/irq.c b/arch/x86/pci/irq.c
> index 52e5510..d9bcb96 100644
> --- a/arch/x86/pci/irq.c
> +++ b/arch/x86/pci/irq.c
> @@ -1119,6 +1119,8 @@ static const struct dmi_system_id pciirq_dmi_table[] __initconst = {
>  
>  void __init pcibios_irq_init(void)
>  {
> +	struct irq_routing_table *itable = NULL;
> +

Minor style nit: the canonical name of irq_routing_table local variables 
is usually 'rtable' or 'rt' - let's not add a third naming variant with 
'itable'?

>  	DBG(KERN_DEBUG "PCI: IRQ init\n");
>  
>  	if (raw_pci_ops == NULL)
> @@ -1129,8 +1131,10 @@ void __init pcibios_irq_init(void)
>  	pirq_table = pirq_find_routing_table();
>  
>  #ifdef CONFIG_PCI_BIOS
> -	if (!pirq_table && (pci_probe & PCI_BIOS_IRQ_SCAN))
> +	if (!pirq_table && (pci_probe & PCI_BIOS_IRQ_SCAN)) {
>  		pirq_table = pcibios_get_irq_routing_table();
> +		itable = pirq_table;
> +	}
>  #endif
>  	if (pirq_table) {
>  		pirq_peer_trick();
> @@ -1145,8 +1149,10 @@ void __init pcibios_irq_init(void)
>  		 * If we're using the I/O APIC, avoid using the PCI IRQ
>  		 * routing table
>  		 */
> -		if (io_apic_assign_pci_irqs)
> +		if (io_apic_assign_pci_irqs) {
> +			kfree(itable);
>  			pirq_table = NULL;
> +		}

Reviewed-by: Ingo Molnar <mingo@kernel.org>

Thanks,

	Ingo
Wenwen Wang - April 16, 2019, 1:37 p.m.
On Tue, Apr 16, 2019 at 2:23 AM Ingo Molnar <mingo@kernel.org> wrote:
>
>
> * Wenwen Wang <wang6495@umn.edu> wrote:
>
> > In pcibios_irq_init(), the PCI IRQ routing table 'pirq_table' is firstly
> > found through pirq_find_routing_table(). If the table is not found and
> > 'CONFIG_PCI_BIOS' is defined, the table is then allocated in
> > pcibios_get_irq_routing_table() using kmalloc(). In the following
> > execution, if the I/O APIC is used, this table is actually not used.
> > However, in that case, the allocated table is not freed, which can lead to
> > a memory leak bug.
> >
> > To fix this issue, this patch frees the allocated table if it is not used.
> >
> > Signed-off-by: Wenwen Wang <wang6495@umn.edu>
> > ---
> >  arch/x86/pci/irq.c | 10 ++++++++--
> >  1 file changed, 8 insertions(+), 2 deletions(-)
> >
> > diff --git a/arch/x86/pci/irq.c b/arch/x86/pci/irq.c
> > index 52e5510..d9bcb96 100644
> > --- a/arch/x86/pci/irq.c
> > +++ b/arch/x86/pci/irq.c
> > @@ -1119,6 +1119,8 @@ static const struct dmi_system_id pciirq_dmi_table[] __initconst = {
> >
> >  void __init pcibios_irq_init(void)
> >  {
> > +     struct irq_routing_table *itable = NULL;
> > +
>
> Minor style nit: the canonical name of irq_routing_table local variables
> is usually 'rtable' or 'rt' - let's not add a third naming variant with
> 'itable'?
>

Thanks for your comment! I will rework the patch.

Wenwen

> >       DBG(KERN_DEBUG "PCI: IRQ init\n");
> >
> >       if (raw_pci_ops == NULL)
> > @@ -1129,8 +1131,10 @@ void __init pcibios_irq_init(void)
> >       pirq_table = pirq_find_routing_table();
> >
> >  #ifdef CONFIG_PCI_BIOS
> > -     if (!pirq_table && (pci_probe & PCI_BIOS_IRQ_SCAN))
> > +     if (!pirq_table && (pci_probe & PCI_BIOS_IRQ_SCAN)) {
> >               pirq_table = pcibios_get_irq_routing_table();
> > +             itable = pirq_table;
> > +     }
> >  #endif
> >       if (pirq_table) {
> >               pirq_peer_trick();
> > @@ -1145,8 +1149,10 @@ void __init pcibios_irq_init(void)
> >                * If we're using the I/O APIC, avoid using the PCI IRQ
> >                * routing table
> >                */
> > -             if (io_apic_assign_pci_irqs)
> > +             if (io_apic_assign_pci_irqs) {
> > +                     kfree(itable);
> >                       pirq_table = NULL;
> > +             }
>
> Reviewed-by: Ingo Molnar <mingo@kernel.org>
>
> Thanks,
>
>         Ingo

Patch

diff --git a/arch/x86/pci/irq.c b/arch/x86/pci/irq.c
index 52e5510..d9bcb96 100644
--- a/arch/x86/pci/irq.c
+++ b/arch/x86/pci/irq.c
@@ -1119,6 +1119,8 @@  static const struct dmi_system_id pciirq_dmi_table[] __initconst = {
 
 void __init pcibios_irq_init(void)
 {
+	struct irq_routing_table *itable = NULL;
+
 	DBG(KERN_DEBUG "PCI: IRQ init\n");
 
 	if (raw_pci_ops == NULL)
@@ -1129,8 +1131,10 @@  void __init pcibios_irq_init(void)
 	pirq_table = pirq_find_routing_table();
 
 #ifdef CONFIG_PCI_BIOS
-	if (!pirq_table && (pci_probe & PCI_BIOS_IRQ_SCAN))
+	if (!pirq_table && (pci_probe & PCI_BIOS_IRQ_SCAN)) {
 		pirq_table = pcibios_get_irq_routing_table();
+		itable = pirq_table;
+	}
 #endif
 	if (pirq_table) {
 		pirq_peer_trick();
@@ -1145,8 +1149,10 @@  void __init pcibios_irq_init(void)
 		 * If we're using the I/O APIC, avoid using the PCI IRQ
 		 * routing table
 		 */
-		if (io_apic_assign_pci_irqs)
+		if (io_apic_assign_pci_irqs) {
+			kfree(itable);
 			pirq_table = NULL;
+		}
 	}
 
 	x86_init.pci.fixup_irqs();