Subject: Re: [PATCH 3/3] net-sysfs: Fix error path for kobject_init_and_add()

From: Tyler Hicks <[email-masked]>
Date: Tue, 30 Apr 2019 10:37:31 -0500

On 2019-04-30 10:28:17, Tobin C. Harding wrote:
> Currently error return from kobject_init_and_add() is not followed by a
> call to kobject_put().  This means there is a memory leak.
> 
> Add call to kobject_put() in error path of kobject_init_and_add().
> 
> Signed-off-by: Tobin C. Harding <[email-masked]>
> ---
>  net/core/net-sysfs.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
> index 8f8b7b6c2945..9d4e3f47b789 100644
> --- a/net/core/net-sysfs.c
> +++ b/net/core/net-sysfs.c
> @@ -925,8 +925,10 @@ static int rx_queue_add_kobject(struct net_device *dev, int index)
>  	kobj->kset = dev->queues_kset;
>  	error = kobject_init_and_add(kobj, &rx_queue_ktype, NULL,
>  				     "rx-%u", index);
> -	if (error)
> +	if (error) {
> +		kobject_put(kobj);
>  		return error;
> +	}

The commit message of the second patch in this series states, "The
correct cleanup function if a call to kobject_init_and_add() has
returned _successfully_ is kobject_del()." Doesn't that mean that
kobject_del() needs to be called instead of kobject_put() when
sysfs_create_group() fails a little lower in this function?

>  
>  	dev_hold(queue->dev);
>  
> @@ -1462,8 +1464,10 @@ static int netdev_queue_add_kobject(struct net_device *dev, int index)
>  	kobj->kset = dev->queues_kset;
>  	error = kobject_init_and_add(kobj, &netdev_queue_ktype, NULL,
>  				     "tx-%u", index);
> -	if (error)
> +	if (error) {
> +		kobject_put(kobj);
>  		return error;
> +	}
>  
>  	dev_hold(queue->dev);

I think the same s/kobject_put/kobject_del/ substitution may be needed
in this function when sysfs_create_group() fails.

Tyler

> -- 
> 2.21.0