Subject: Re: [PATCH net] net: fix memory leak in register_netdevice() on error path

From: Toshiaki Makita <[email-masked]>
Date: Sun, 29 Nov 2020 22:56:38 +0900

On 2020/11/26 22:23, Yang Yingliang wrote:
...
> Reported-by: Hulk Robot <[email-masked]>
> Signed-off-by: Yang Yingliang <[email-masked]>
> ---
>   net/core/dev.c | 11 +++++++++++
>   1 file changed, 11 insertions(+)
> 
> diff --git a/net/core/dev.c b/net/core/dev.c
> index 82dc6b48e45f..907204395b64 100644
> --- a/net/core/dev.c
> +++ b/net/core/dev.c
> @@ -10000,6 +10000,17 @@ int register_netdevice(struct net_device *dev)
>   	ret = notifier_to_errno(ret);
>   	if (ret) {
>   		rollback_registered(dev);
> +		/*
> +		 * In common case, priv_destructor() will be

As per netdev-faq, the comment style should be

/* foobar blah blah blah
  * another line of text
  */

rather than

/*
  * foobar blah blah blah
  * another line of text
  */

> +		 * called in netdev_run_todo() after calling
> +		 * ndo_uninit() in rollback_registered().
> +		 * But in this case, priv_destructor() will
> +		 * never be called, then it causes memory
> +		 * leak, so we should call priv_destructor()
> +		 * here.
> +		 */
> +		if (dev->priv_destructor)
> +			dev->priv_destructor(dev);

To be in line with netdev_run_todo(), I think priv_destructor() should be
called after "dev->reg_state = NETREG_UNREGISTERED".

Toshiaki Makita

>   		rcu_barrier();
>   
>   		dev->reg_state = NETREG_UNREGISTERED;
>