Possible memleak in pci_bus_set_aer_ops
Dear PCI Developers,
We are curious whether the function pci_bus_set_aer_ops might have a memory leak.
The function is https://elixir.bootlin.com/linux/v6.8/source/drivers/pci/pcie/aer_inject.c#L297
and the relevant code is
```
static int pci_bus_set_aer_ops(struct pci_bus *bus)
{
struct pci_ops *ops;
struct pci_bus_ops *bus_ops;
unsigned long flags;
bus_ops = kmalloc(sizeof(*bus_ops), GFP_KERNEL);
if (!bus_ops)
return -ENOMEM;
ops = pci_bus_set_ops(bus, &aer_inj_pci_ops);
spin_lock_irqsave(&inject_lock, flags);
if (ops == &aer_inj_pci_ops)
goto out;
pci_bus_ops_init(bus_ops, bus, ops);
list_add(&bus_ops->list, &pci_bus_ops_list);
bus_ops = NULL;
out:
spin_unlock_irqrestore(&inject_lock, flags);
kfree(bus_ops);
return 0;
}
```Here if the goto statement does not jump to out, the bus_ops will be assigned with NULL and then kfree(bus_ops) will not free the allocated memory.
Please kindly correct us if we missed any key information. Looking forward to your response!
Best,
Zijie
Quoted 30 lines · Zijie Zhao
Dear PCI Developers,
We are curious whether the function
pci_bus_set_aer_opsmight have a memory leak.The function is https://elixir.bootlin.com/linux/v6.8/source/drivers/pci/pcie/aer_inject.c#L297
and the relevant code is``` static int pci_bus_set_aer_ops(struct pci_bus *bus) { struct pci_ops *ops; struct pci_bus_ops *bus_ops; unsigned long flags; bus_ops = kmalloc(sizeof(*bus_ops), GFP_KERNEL); if (!bus_ops) return -ENOMEM; ops = pci_bus_set_ops(bus, &aer_inj_pci_ops); spin_lock_irqsave(&inject_lock, flags); if (ops == &aer_inj_pci_ops) goto out; pci_bus_ops_init(bus_ops, bus, ops); list_add(&bus_ops->list, &pci_bus_ops_list); bus_ops = NULL; out: spin_unlock_irqrestore(&inject_lock, flags); kfree(bus_ops); return 0; } ```
Quoted 3 lines
Here if the goto statement does not jump to
out, thebus_opswill be assigned withNULLand thenkfree(bus_ops)will not free the allocated memory.Please kindly correct us if we missed any key information. Looking forward to your response!
I think it is a valid issue that needs to be fixed. If you would like, please send a patch to fix it.
Quoted 4 lines
Best,
Zijie
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
PATCH 1/1 fix memory leak in pci_bus_set_aer_ops
Remove unnecessary bus_ops = NULL assignment in pci_bus_set_aer_ops to prevent a memory leak when ops are equal to &aer_inj_pci_ops. This change ensures allocated bus_ops memory is properly freed.
--- drivers/pci/pcie/aer_inject.c | 1 - 1 file changed, 1 deletion(-)
drivers/pci/pcie/aer_inject.c +0 -1
@@ -309,7 +309,6 @@ static int pci_bus_set_aer_ops(struct pci_bus *bus) goto out;pci_bus_ops_init (bus_ops, bus, ops);list_add (&bus_ops->list, &pci_bus_ops_list); - bus_ops =NULL ; out:spin_unlock_irqrestore (&inject_lock, flags);kfree (bus_ops);
Signature · 1 line
2.34.1
Quoted 31 lines · Kuppuswamy Sathyanarayanan
Quoted 30 lines · Zijie Zhao
Dear PCI Developers,
We are curious whether the function
pci_bus_set_aer_opsmight have a memory leak.The function is https://elixir.bootlin.com/linux/v6.8/source/drivers/pci/pcie/aer_inject.c#L297
and the relevant code is``` static int pci_bus_set_aer_ops(struct pci_bus *bus) { struct pci_ops *ops; struct pci_bus_ops *bus_ops; unsigned long flags; bus_ops = kmalloc(sizeof(*bus_ops), GFP_KERNEL); if (!bus_ops) return -ENOMEM; ops = pci_bus_set_ops(bus, &aer_inj_pci_ops); spin_lock_irqsave(&inject_lock, flags); if (ops == &aer_inj_pci_ops) goto out; pci_bus_ops_init(bus_ops, bus, ops); list_add(&bus_ops->list, &pci_bus_ops_list); bus_ops = NULL; out: spin_unlock_irqrestore(&inject_lock, flags); kfree(bus_ops); return 0; } ```
Quoted 4 lines
Quoted 3 lines
Here if the goto statement does not jump to
out, thebus_opswill be assigned withNULLand thenkfree(bus_ops)will not free the allocated memory.Please kindly correct us if we missed any key information. Looking forward to your response!
I think it is a valid issue that needs to be fixed. If you would like, please send a patch to fix it.
Sorry, I misread it. I think it is not a issue. For a valid case, the bus_ops is added to pci_bus_ops_list, which is freed in module exit function. Ignore my previous comments.
Quoted 4 lines
Quoted 4 lines
Best,
Zijie
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer