Skip to contentSkip to conversation

net: dsa: fix fixed-link-phy device leaks

PATCH Superseded Johan Hovold 2 participants 6 messages Nov 16, 2016 – Nov 17, 2016

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: dsa: fix fixed-link-phy device leaks

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")

---

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(-)
net/dsa/dsa.c +7 -1
index a6902c1e2f28..798a6a776a5f 100644
@@ -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);
 		}
 	}
 }
Signature · 1 line
2.7.3
#1 lore raw
Quoted 49 lines · Johan Hovold

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")

---

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(-)
net/dsa/dsa.c +2 -0
index a6902c1e2f28..798a6a776a5f 100644
@@ -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;

net/dsa/dsa.c void dsa_cpu_dsa_destroy(struct device_node *port_dn) · line 519

Quoted 10 lines
net/dsa/dsa.c +5 -1
@@ -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()?

Signature · 1 line
Florian
#2 lore raw
Quoted 48 lines · Florian Fainelli
Quoted 47 lines · Johan Hovold

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")

---

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(-)
net/dsa/dsa.c +2 -0
index a6902c1e2f28..798a6a776a5f 100644
@@ -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 */
 	}
 
Quoted 16 lines

net/dsa/dsa.c void dsa_cpu_dsa_destroy(struct device_node *port_dn) · line 519

Quoted 12 lines
net/dsa/dsa.c +5 -1
 	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);
}

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.

Johan

#3 lore raw
Quoted 66 lines · Johan Hovold
Quoted 48 lines · Florian Fainelli
Quoted 47 lines · Johan Hovold

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")

---

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(-)
net/dsa/dsa.c +2 -0
index a6902c1e2f28..798a6a776a5f 100644
@@ -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 */
 	}
 
Quoted 16 lines

net/dsa/dsa.c void dsa_cpu_dsa_destroy(struct device_node *port_dn) · line 519

Quoted 12 lines
net/dsa/dsa.c +5 -1
 	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()?

Quoted 6 lines

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.

Quoted 5 lines

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.

Signature · 1 line
Florian
#4 lore raw
Quoted 80 lines · Florian Fainelli
Quoted 66 lines · Johan Hovold
Quoted 48 lines · Florian Fainelli
Quoted 47 lines · Johan Hovold

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")

---

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(-)
net/dsa/dsa.c +2 -0
index a6902c1e2f28..798a6a776a5f 100644
@@ -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 */
 	}
 
Quoted 16 lines

net/dsa/dsa.c void dsa_cpu_dsa_destroy(struct device_node *port_dn) · line 519

Quoted 12 lines
net/dsa/dsa.c +5 -1
 	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()?

Quoted 6 lines

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.

Quoted 4 lines

which may need to be called multiple times, specifically after a call to of_phy_find_device() which takes another reference.

Quoted 6 lines
Quoted 1 line

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.

I'll cook something up for -next, but how about using

	put_device(&phydev->mdio.dev)

for the reference taken by of_phy_find_device() (as some driver already do) to fix the immediate leaks?

Then deregistration would look like:

	phydev = of_phy_find_device(port_dn);
	if (phydev) {
-			phy_device_free(phydev);
			fixed_phy_unregister(phydev);

+ put_device(&phydev->mdio.dev)
+ phy_device_free(phydev);

Thanks,
Johan

#5 lore raw
Quoted 101 lines · Johan Hovold
Quoted 80 lines · Florian Fainelli
Quoted 66 lines · Johan Hovold
Quoted 48 lines · Florian Fainelli
Quoted 47 lines · Johan Hovold

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")

---

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(-)
net/dsa/dsa.c +2 -0
index a6902c1e2f28..798a6a776a5f 100644
@@ -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 */
 	}
 
Quoted 16 lines

net/dsa/dsa.c void dsa_cpu_dsa_destroy(struct device_node *port_dn) · line 519

Quoted 12 lines
net/dsa/dsa.c +5 -1
 	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()?

Quoted 6 lines

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.

Quoted 4 lines

which may need to be called multiple times, specifically after a call to of_phy_find_device() which takes another reference.

Quoted 6 lines
Quoted 1 line

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.

I'll cook something up for -next, but how about using

	put_device(&phydev->mdio.dev)

for the reference taken by of_phy_find_device() (as some driver already do) to fix the immediate leaks?

Then deregistration would look like:

	phydev = of_phy_find_device(port_dn);
	if (phydev) {
net/dsa/dsa.c +0 -1
-			phy_device_free(phydev);
			fixed_phy_unregister(phydev);

Review

Quoted 2 lines
(quoted diff) +2 -0
+			put_device(&phydev->mdio.dev)
+			phy_device_free(phydev);

On second thought, I suggest just renaming the phy_device_free() as phy_device_put() to reflect what it really does these days. There can never be a guarantee that the phy is freed on a call to phy_device_free() anyway.

The OF interface can still be modified to return a pointer later.

Johan