Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1523716

Re: [PATCH] net: dsa: fix fixed-link-phy device leaks

From Florian Fainelli <f.fainelli@gmail.com>
Newsgroups linux.kernel
Subject Re: [PATCH] net: dsa: fix fixed-link-phy device leaks
Date 2016-11-16 19:20 +0100
Message-ID <sEcYp-6si-5@gated-at.bofh.it> (permalink)
References <sE9Hb-4c9-29@gated-at.bofh.it> <sEbSG-5Ll-9@gated-at.bofh.it> <sEc2l-5Qq-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On 11/16/2016 09:11 AM, Johan Hovold wrote:
> On Wed, Nov 16, 2016 at 09:06:26AM -0800, Florian Fainelli wrote:
>> On 11/16/2016 06:47 AM, Johan Hovold wrote:
>>> Make sure to drop the reference taken by of_phy_find_device() when
>>> registering and deregistering the fixed-link PHY-device.
>>>
>>> Note that we need to put both references held at deregistration.
>>>
>>> Fixes: 39b0c705195e ("net: dsa: Allow configuration of CPU & DSA port
>>> speeds/duplex")
>>> Signed-off-by: Johan Hovold <johan@kernel.org>
>>> ---
>>>
>>> Hi,
>>>
>>> This is one has been compile tested only, but fixes a couple of leaks
>>> similar to one that was found in the cpsw driver for which I just posted
>>> a patch.
>>>
>>> It turns out all drivers but DSA fail to deregister the fixed-link PHYs
>>> registered by of_phy_register_fixed_link(). Due to the way this
>>> interface was designed, deregistering such a PHY is a bit cumbersome and
>>> looks like it would benefit from a common helper.
>>>
>>> However, perhaps the interface should instead be changed so that the PHY
>>> device is returned so that drivers do not need to use
>>> of_phy_find_device() when they need to access properties of the fixed
>>> link (e.g. as in dsu_cpu_dsa_setup below).
>>>
>>> Thoughts?
>>>
>>> Thanks,
>>> Johan
>>>
>>>
>>>  net/dsa/dsa.c | 8 +++++++-
>>>  1 file changed, 7 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
>>> index a6902c1e2f28..798a6a776a5f 100644
>>> --- a/net/dsa/dsa.c
>>> +++ b/net/dsa/dsa.c
>>> @@ -233,6 +233,8 @@ int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
>>>  		genphy_read_status(phydev);
>>>  		if (ds->ops->adjust_link)
>>>  			ds->ops->adjust_link(ds, port, phydev);
>>> +
>>> +		phy_device_free(phydev);	/* of_phy_find_device */
>>>  	}
>>>  
>>>  	return 0;
>>> @@ -509,8 +511,12 @@ void dsa_cpu_dsa_destroy(struct device_node *port_dn)
>>>  	if (of_phy_is_fixed_link(port_dn)) {
>>>  		phydev = of_phy_find_device(port_dn);
>>>  		if (phydev) {
>>> -			phy_device_free(phydev);
>>>  			fixed_phy_unregister(phydev);
>>> +			/* Put references taken by of_phy_find_device() and
>>> +			 * of_phy_register_fixed_link().
>>> +			 */
>>> +			phy_device_free(phydev);
>>> +			phy_device_free(phydev);
>>
>> Double free, this looks bogus here. Actually would not this mean a
>> triple free since you already free in dsa_cpu_dsa_setup() which is
>> paired with dsa_cpu_dsa_destroy()?
> 
> The naming of phy_device_free() is unfortunate when it's really a put():
> 
> void phy_device_free(struct phy_device *phydev)
> {
> 	put_device(&phydev->mdio.dev);
> }

Indeed, should have looked a little harder.

> 
> which may need to be called multiple times, specifically after a call to
> of_phy_find_device() which takes another reference.
> 
> With this patch the refcounts are properly balanced.

The intent of your patch is good, but it still feels like having to
double imbalance the refcount is symptomatic of a larger issue here, it
does not seem like having several refcounts are necessary, so we may
really want to rework the API.
-- 
Florian

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH] net: dsa: fix fixed-link-phy device leaks Johan Hovold <johan@kernel.org> - 2016-11-16 15:50 +0100
  Re: [PATCH] net: dsa: fix fixed-link-phy device leaks Florian Fainelli <f.fainelli@gmail.com> - 2016-11-16 18:10 +0100
    Re: [PATCH] net: dsa: fix fixed-link-phy device leaks Johan Hovold <johan@kernel.org> - 2016-11-16 18:20 +0100
      Re: [PATCH] net: dsa: fix fixed-link-phy device leaks Florian Fainelli <f.fainelli@gmail.com> - 2016-11-16 19:20 +0100
        Re: [PATCH] net: dsa: fix fixed-link-phy device leaks Johan Hovold <johan@kernel.org> - 2016-11-17 11:00 +0100
          Re: [PATCH] net: dsa: fix fixed-link-phy device leaks Johan Hovold <johan@kernel.org> - 2016-11-17 18:00 +0100

csiph-web