Subject: [drivers/net/netdevsim] Question about possible memleak

From: Zijie Zhao <[email-masked]>
Date: Wed, 13 Mar 2024 21:13:18 -0500

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