Skip to contentSkip to conversation

Question about possible memleak

drivers/net/netdevsim Zijie Zhao 2 participants 2 messages Mar 14, 2024

Subsystems (2)
lore
Share & tools
lore

Zijie Zhao

author
#0 lore raw

Dear Netdevsim Developers,

We are curious whether the function nsim_bus_dev_new might have a memory leak.

The function is https://elixir.bootlin.com/linux/v6.8/source/drivers/net/netdevsim/bus.c#L275
and the relevant code is

```
static struct nsim_bus_dev *
nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queues)
{
	struct nsim_bus_dev *nsim_bus_dev;
	int err;

	nsim_bus_dev = kzalloc(sizeof(*nsim_bus_dev), GFP_KERNEL);
	...

	err = device_register(&nsim_bus_dev->dev);
	if (err)
		goto err_nsim_bus_dev_id_free;

	return nsim_bus_dev;

err_nsim_bus_dev_id_free:
	ida_free(&nsim_bus_dev_ids, nsim_bus_dev->dev.id);
	put_device(&nsim_bus_dev->dev);
	nsim_bus_dev = NULL;
err_nsim_bus_dev_free:
	kfree(nsim_bus_dev);
	return ERR_PTR(err);
}
```

Here if the err_nsim_bus_dev_id_free label is entered, nsim_bus_dev will be assigned NULL and then kfree(nsim_bus_dev) will not free the allocated memory.

Please kindly correct us if we missed any key information. Looking forward to your response!

Best,
Zijie

maintainer
#1 lore raw
Quoted 6 lines · On Wed, 13 Mar 2024 21:13:18 -0500 Zijie Zhao wrote:

Here if the err_nsim_bus_dev_id_free label is entered, nsim_bus_dev will be assigned NULL and then kfree(nsim_bus_dev) will not free the allocated memory.

Please kindly correct us if we missed any key information. Looking forward to your response!

/**
* device_register - register a device with the system.
* @dev: pointer to the device structure
*
* This happens in two clean steps - initialize the device
* and add it to the system. The two steps can be called
* separately, but this is the easiest and most common.
* I.e. you should only call the two helpers separately if
* have a clearly defined need to use and refcount the device
* before it is added to the hierarchy.
*
* For more information, see the kerneldoc for device_initialize()
* and device_add().
*
* NOTE: _Never_ directly free @dev after calling this function, even
* if it returned an error! Always use put_device() to give up the
* reference initialized in this function instead.
*/
int device_register(struct device *dev)