Skip to contentSkip to conversation

Possible memleak in pci_bus_set_aer_ops

drivers/pci New Zijie Zhao 2 participants 4 messages Mar 14, 2024 PCI

lore
Share & tools
lore

Zijie Zhao

author
#0 lore raw

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

#1 lore raw
Quoted 30 lines · Zijie Zhao

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;
}
```
Quoted 3 lines

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!

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
author
#2 lore raw

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
index 2dab275d252f..0c84fadbfd2e 100644
@@ -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
#3 lore raw
In reply to Kuppuswamy Sathyanarayanan
Quoted 31 lines · Kuppuswamy Sathyanarayanan
Quoted 30 lines · Zijie Zhao

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;
}
```
Quoted 4 lines
Quoted 3 lines

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!

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