netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: "wanghai (M)" <wanghai26@huawei.com>
Cc: Stephen Hemminger <stephen@networkplumber.org>,
	davem@davemloft.net, idosch@mellanox.com,
	alexander.h.duyck@intel.com, tyhicks@canonical.com,
	f.fainelli@gmail.com, amritha.nambiar@intel.com, joe@perches.com,
	dmitry.torokhov@gmail.com, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] net-sysfs: Fix memory leak in netdev_register_kobject
Date: Tue, 19 Mar 2019 16:00:19 +0200	[thread overview]
Message-ID: <20190319140019.GG9224@smile.fi.intel.com> (raw)
In-Reply-To: <18553079-7bbd-fcfe-ef1c-6717e963e0a5@huawei.com>

On Tue, Mar 19, 2019 at 08:17:01PM +0800, wanghai (M) wrote:
> 在 2019/3/19 18:30, Andy Shevchenko 写道:
> > On Tue, Mar 19, 2019 at 11:19:24AM +0800, wanghai (M) wrote:
> > > 在 2019/3/19 0:19, Andy Shevchenko 写道:

> > > If device_add(dev) or register_queue_kobjects(ndev) fails,
> > > In register_netdevice(), dev-> reg_state = NETREG_UNINITIALIZED and returns
> > > an error, causing put_device(&dev-> dev) -> ..-> kobject_cleanup() not to be
> > > called.
> > OK, that's true, but your patch is wrong.
> > See error handling in device_create_groups_vargs() for example.

> Hi Andy
> Thanks for your advice. I understand the problem of my patch.
> Can you help me see if it can be fixed like this?
> 
> diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
> index 4ff661f..6fe5b8e 100644
> --- a/net/core/net-sysfs.c
> +++ b/net/core/net-sysfs.c
> @@ -1745,17 +1745,21 @@ int netdev_register_kobject(struct net_device *ndev)
> 
>         error = device_add(dev);
>         if (error)
> -               return error;
> +               goto device_add_error;

This part seems correct now.

>         error = register_queue_kobjects(ndev);
> -       if (error) {
> -               device_del(dev);
> -               return error;
> -       }
> +       if (error)
> +               goto register_error;

Yes, seems correct order here, i.e. device_del() followed by put_device().

> 
>         pm_runtime_set_memalloc_noio(dev, true);
> 

> +out:

Better

	return 0;

>         return error;
> +register_error:

Better to describe what you will do here, i.e.

error_device_del:

> +       device_del(dev);

> +device_add_error:

Ditto.

error_put_device:

> +       put_device(dev);

> +       goto out;

Better

	return error;

>  }

-- 
With Best Regards,
Andy Shevchenko



  parent reply	other threads:[~2019-03-19 14:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-19  5:06 [PATCH] net-sysfs: Fix memory leak in netdev_register_kobject Wang Hai
2019-03-18 15:57 ` Stephen Hemminger
2019-03-18 16:19   ` Andy Shevchenko
     [not found]     ` <c1c266af-7aaa-00a7-aa7a-e61c65665741@huawei.com>
2019-03-19 10:30       ` Andy Shevchenko
     [not found]         ` <18553079-7bbd-fcfe-ef1c-6717e963e0a5@huawei.com>
2019-03-19 14:00           ` Andy Shevchenko [this message]
2019-03-19 15:44           ` Stephen Hemminger
2019-03-19  3:03   ` wanghai (M)
2019-03-19  3:15     ` Stephen Hemminger
2019-03-19  3:39       ` wanghai (M)
2019-03-19 10:22         ` Andy Shevchenko
2019-03-18 18:02 ` Eric Dumazet
2019-03-19  3:47   ` wanghai (M)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190319140019.GG9224@smile.fi.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=alexander.h.duyck@intel.com \
    --cc=amritha.nambiar@intel.com \
    --cc=davem@davemloft.net \
    --cc=dmitry.torokhov@gmail.com \
    --cc=f.fainelli@gmail.com \
    --cc=idosch@mellanox.com \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    --cc=tyhicks@canonical.com \
    --cc=wanghai26@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).