Skip to contentSkip to conversation

net: dsa: mt7530: Add some return-value checks

PATCH Superseded Alex Dewar 3 participants 10 messages Sep 16, 2020 – Sep 24, 2020

Subsystems (5)
lore
Share & tools
lore

Alex Dewar

author
#0 lore raw

PATCH 1/1 net: dsa: mt7530: Add some return-value checks

In mt7531_cpu_port_config(), if the variable port is neither 5 nor 5, then variable interface will be used uninitialised. Change the function to return -EINVAL in this case.

As the return value of mt7531_cpu_port_config() is never checked (even though it returns an int) add a check in the correct place so that the error can be passed up the call stack. Now that we correctly handle errors thrown in this function, also check the return value of mt7531_mac_config() in case an error occurs here.

Addresses-Coverity: 1496993 ("Uninitialized variables")

Fixes c288575f7810 ("net: dsa: mt7530: Add the support of MT7531 switch")

---

If it is not expected that these functions will throw errors (i.e. because the parameters passed will always be correct), we could dispense with the use of EINVAL errors and just use BUG*() macros instead. Let me know if you'd rather I fix things up in that way.

Best,
Alex

 drivers/net/dsa/mt7530.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/net/dsa/mt7530.c +12 -4
index 61388945d316..157d0a01faae 100644
@@ -945,10 +945,14 @@ static int
 mt753x_cpu_port_enable(struct dsa_switch *ds, int port)
 {
 	struct mt7530_priv *priv = ds->priv;
+	int ret;
 
 	/* Setup max capability of CPU port at first */
-	if (priv->info->cpu_port_config)
-		priv->info->cpu_port_config(ds, port);
+	if (priv->info->cpu_port_config) {
+		ret = priv->info->cpu_port_config(ds, port);
+		if (ret)
+			return ret;
+	}
 
 	/* Enable Mediatek header mode on the cpu port */
 	mt7530_write(priv, MT7530_PVC_P(port),
@@ -2275,7 +2279,7 @@ mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 {
 	struct mt7530_priv *priv = ds->priv;
 	phy_interface_t interface;
-	int speed;
+	int ret, speed;
 
 	switch (port) {
 	case 5:
@@ -2293,6 +2297,8 @@ mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 
 		priv->p6_interface = interface;
 		break;
+	default:
+		return -EINVAL;
 	}
 
 	if (interface == PHY_INTERFACE_MODE_2500BASEX)
@@ -2300,7 +2306,9 @@ mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 	else
 		speed = SPEED_1000;
 
-	mt7531_mac_config(ds, port, MLO_AN_FIXED, interface);
+	ret = mt7531_mac_config(ds, port, MLO_AN_FIXED, interface);
+	if (ret)
+		return ret;
 	mt7530_write(priv, MT7530_PMCR_P(port),
 		     PMCR_CPU_PORT_SETTING(priv->id));
 	mt753x_phylink_mac_link_up(ds, port, MLO_AN_FIXED, interface, NULL,
Signature · 7 lines
2.28.0


_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
#1 lore raw

On 9/16/20 14:50, Alex Dewar wrote:

[..]

Quoted 4 lines
 drivers/net/dsa/mt7530.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

[..]

drivers/net/dsa/mt7530.c mt7531_cpu_port_config(struct dsa_switch *ds, int port) · line 2282

Quoted 9 lines
drivers/net/dsa/mt7530.c +1 -1
 	/* Enable Mediatek header mode on the cpu port */
 	mt7530_write(priv, MT7530_PVC_P(port),
@@ -2275,7 +2279,7 @@ mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 {
 	struct mt7530_priv *priv = ds->priv;
 	phy_interface_t interface;
-	int speed;
+	int ret, speed;

Don't do this. Instead, declare each variable on its own line. In this case, speed before ret.

Thanks
--
Gustavo

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek

author
#2 lore raw

[snip]

Quoted 12 lines

drivers/net/dsa/mt7530.c mt7531_cpu_port_config(struct dsa_switch *ds, int port) · line 2282

Quoted 9 lines
drivers/net/dsa/mt7530.c +1 -1
 	/* Enable Mediatek header mode on the cpu port */
 	mt7530_write(priv, MT7530_PVC_P(port),
@@ -2275,7 +2279,7 @@ mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 {
 	struct mt7530_priv *priv = ds->priv;
 	phy_interface_t interface;
-	int speed;
+	int ret, speed;

Don't do this. Instead, declare each variable on its own line. In this case, speed before ret.

Good point. I'll do this in v2 :-)

Quoted 4 lines

Thanks
--
Gustavo

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek

#4 lore raw
In reply to Alex Dewar

Hi Alex,

Thanks for your review and fixing.
On Thu, 2020-09-17 at 03:50 +0800, Alex Dewar wrote:
[..]

Quoted 5 lines

If it is not expected that these functions will throw errors (i.e. because the parameters passed will always be correct), we could dispense with the use of EINVAL errors and just use BUG*() macros instead. Let me know if you'd rather I fix things up in that way.

The cpu port setting is passed by dts. Use EINVAL to catch unexpected setting is fine.

Quoted 15 lines

Best,
Alex

 drivers/net/dsa/mt7530.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/net/dsa/mt7530.c +0 -0
index 61388945d316..157d0a01faae 100644
@@ -945,10 +945,14 @@ static int
 mt753x_cpu_port_enable(struct dsa_switch *ds, int port)
 {
 	struct mt7530_priv *priv = ds->priv;

drivers/net/dsa/mt7530.c static int · line 955

Quoted 10 lines
drivers/net/dsa/mt7530.c +6 -2
+	int ret;
 
 	/* Setup max capability of CPU port at first */
-	if (priv->info->cpu_port_config)
-		priv->info->cpu_port_config(ds, port);
+	if (priv->info->cpu_port_config) {
+		ret = priv->info->cpu_port_config(ds, port);
+		if (ret)
+			return ret;
+	}

How about check return value in caller function, mt7530_setup() and mt7531_setup(), too?

                if (dsa_is_cpu_port(ds, i)) {
                        ret = mt753x_cpu_port_enable(ds, i);
			if (ret)
				return ret;
		} else {

[..]
regards,
landen

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek

author
#5 lore raw

PATCH v2 1/1 net: dsa: mt7530: Add some return-value checks

In mt7531_cpu_port_config(), if the variable port is neither 5 nor 6, then variable interface will be used uninitialised. Change the function to return -EINVAL in this case.

As the return value of mt7531_cpu_port_config() is never checked (even though it returns an int) add a check in the correct place so that the error can be passed up the call stack. Now that we correctly handle errors thrown in this function, also check the return value of mt7531_mac_config() in case an error occurs here. Also add misisng checks to mt7530_setup() and mt7531_setup(), which are another level further up the call stack.

Fixes c288575f7810 ("net: dsa: mt7530: Add the support of MT7531 switch")

Addresses-Coverity: 1496993 ("Uninitialized variables")

---
v2:
- fix typo in commit message
- split variable declarations onto multiple lines (Gustavo)
- add additional checks for mt753*_setup (Landen)

 drivers/net/dsa/mt7530.c | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)
drivers/net/dsa/mt7530.c +22 -9
index 61388945d316..cb3efa7de7a8 100644
@@ -945,10 +945,14 @@ static int
 mt753x_cpu_port_enable(struct dsa_switch *ds, int port)
 {
 	struct mt7530_priv *priv = ds->priv;
+	int ret;
 
 	/* Setup max capability of CPU port at first */
-	if (priv->info->cpu_port_config)
-		priv->info->cpu_port_config(ds, port);
+	if (priv->info->cpu_port_config) {
+		ret = priv->info->cpu_port_config(ds, port);
+		if (ret)
+			return ret;
+	}
 
 	/* Enable Mediatek header mode on the cpu port */
 	mt7530_write(priv, MT7530_PVC_P(port),
@@ -1631,9 +1635,11 @@ mt7530_setup(struct dsa_switch *ds)
 		mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
 			   PCR_MATRIX_CLR);
 
-		if (dsa_is_cpu_port(ds, i))
-			mt753x_cpu_port_enable(ds, i);
-		else
+		if (dsa_is_cpu_port(ds, i)) {
+			ret = mt753x_cpu_port_enable(ds, i);
+			if (ret)
+				return ret;
+		} else
 			mt7530_port_disable(ds, i);
 
 		/* Enable consistent egress tag */
@@ -1785,9 +1791,11 @@ mt7531_setup(struct dsa_switch *ds)
 
 		mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR);
 
-		if (dsa_is_cpu_port(ds, i))
-			mt753x_cpu_port_enable(ds, i);
-		else
+		if (dsa_is_cpu_port(ds, i)) {
+			ret = mt753x_cpu_port_enable(ds, i);
+			if (ret)
+				return ret;
+		} else
 			mt7530_port_disable(ds, i);
 
 		/* Enable consistent egress tag */
@@ -2276,6 +2284,7 @@ mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 	struct mt7530_priv *priv = ds->priv;
 	phy_interface_t interface;
 	int speed;
+	int ret;
 
 	switch (port) {
 	case 5:
@@ -2293,6 +2302,8 @@ mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 
 		priv->p6_interface = interface;
 		break;
+	default:
+		return -EINVAL;
 	}
 
 	if (interface == PHY_INTERFACE_MODE_2500BASEX)
@@ -2300,7 +2311,9 @@ mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 	else
 		speed = SPEED_1000;
 
-	mt7531_mac_config(ds, port, MLO_AN_FIXED, interface);
+	ret = mt7531_mac_config(ds, port, MLO_AN_FIXED, interface);
+	if (ret)
+		return ret;
 	mt7530_write(priv, MT7530_PMCR_P(port),
 		     PMCR_CPU_PORT_SETTING(priv->id));
 	mt753x_phylink_mac_link_up(ds, port, MLO_AN_FIXED, interface, NULL,
Signature · 1 line
2.28.0
#6 lore raw

Hi Alex,

Thanks for your patch. By linux/scripts/checkpatch.pl

On Sun, 2020-09-20 at 03:28 +0800, Alex Dewar wrote:
[..]

drivers/net/dsa/mt7530.c mt7530_setup(struct dsa_switch *ds) · line 1643

Quoted 13 lines
drivers/net/dsa/mt7530.c +5 -3
@@ -1631,9 +1635,11 @@ mt7530_setup(struct dsa_switch *ds)
 		mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
 			   PCR_MATRIX_CLR);
 
-		if (dsa_is_cpu_port(ds, i))
-			mt753x_cpu_port_enable(ds, i);
-		else
+		if (dsa_is_cpu_port(ds, i)) {
+			ret = mt753x_cpu_port_enable(ds, i);
+			if (ret)
+				return ret;
+		} else
 			mt7530_port_disable(ds, i);

CHECK: braces {} should be used on all arms of this statement
CHECK: Unbalanced braces around else statement

Quoted 5 lines
drivers/net/dsa/mt7530.c +0 -0
 		/* Enable consistent egress tag */
@@ -1785,9 +1791,11 @@ mt7531_setup(struct dsa_switch *ds)
 
 		mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR);

drivers/net/dsa/mt7530.c mt7531_setup(struct dsa_switch *ds) · line 1799

Quoted 10 lines
drivers/net/dsa/mt7530.c +5 -3
 
-		if (dsa_is_cpu_port(ds, i))
-			mt753x_cpu_port_enable(ds, i);
-		else
+		if (dsa_is_cpu_port(ds, i)) {
+			ret = mt753x_cpu_port_enable(ds, i);
+			if (ret)
+				return ret;
+		} else
 			mt7530_port_disable(ds, i);

CHECK: braces {} should be used on all arms of this statement
CHECK: Unbalanced braces around else statement

[..]
regards landen

author
#7 lore raw
Quoted 39 lines · Landen Chao

Hi Alex,

Thanks for your patch. By linux/scripts/checkpatch.pl

On Sun, 2020-09-20 at 03:28 +0800, Alex Dewar wrote:
[..]

drivers/net/dsa/mt7530.c mt7530_setup(struct dsa_switch *ds) · line 1643

Quoted 13 lines
drivers/net/dsa/mt7530.c +5 -3
@@ -1631,9 +1635,11 @@ mt7530_setup(struct dsa_switch *ds)
  		mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
  			   PCR_MATRIX_CLR);
 
-		if (dsa_is_cpu_port(ds, i))
-			mt753x_cpu_port_enable(ds, i);
-		else
+		if (dsa_is_cpu_port(ds, i)) {
+			ret = mt753x_cpu_port_enable(ds, i);
+			if (ret)
+				return ret;
+		} else
  			mt7530_port_disable(ds, i);

CHECK: braces {} should be used on all arms of this statement
CHECK: Unbalanced braces around else statement

Quoted 5 lines
drivers/net/dsa/mt7530.c +0 -0
  		/* Enable consistent egress tag */
@@ -1785,9 +1791,11 @@ mt7531_setup(struct dsa_switch *ds)
 
  		mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR);

drivers/net/dsa/mt7530.c mt7531_setup(struct dsa_switch *ds) · line 1799

Quoted 10 lines
drivers/net/dsa/mt7530.c +5 -3
 
-		if (dsa_is_cpu_port(ds, i))
-			mt753x_cpu_port_enable(ds, i);
-		else
+		if (dsa_is_cpu_port(ds, i)) {
+			ret = mt753x_cpu_port_enable(ds, i);
+			if (ret)
+				return ret;
+		} else
  			mt7530_port_disable(ds, i);

CHECK: braces {} should be used on all arms of this statement
CHECK: Unbalanced braces around else statement

Quoted 2 lines

[..]
regards landen

Hi Landen,

Sorry about this... I usually run checkpatch over my patches. Would you like me to send a separate fix or a v3?

Best,
Alex

#8 lore raw
Quoted 42 lines · Alex Dewar
Quoted 39 lines · Landen Chao

Hi Alex,

Thanks for your patch. By linux/scripts/checkpatch.pl

On Sun, 2020-09-20 at 03:28 +0800, Alex Dewar wrote:
[..]

drivers/net/dsa/mt7530.c mt7530_setup(struct dsa_switch *ds) · line 1643

Quoted 13 lines
drivers/net/dsa/mt7530.c +5 -3
@@ -1631,9 +1635,11 @@ mt7530_setup(struct dsa_switch *ds)
  		mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
  			   PCR_MATRIX_CLR);
 
-		if (dsa_is_cpu_port(ds, i))
-			mt753x_cpu_port_enable(ds, i);
-		else
+		if (dsa_is_cpu_port(ds, i)) {
+			ret = mt753x_cpu_port_enable(ds, i);
+			if (ret)
+				return ret;
+		} else
  			mt7530_port_disable(ds, i);

CHECK: braces {} should be used on all arms of this statement
CHECK: Unbalanced braces around else statement

Quoted 5 lines
drivers/net/dsa/mt7530.c +0 -0
  		/* Enable consistent egress tag */
@@ -1785,9 +1791,11 @@ mt7531_setup(struct dsa_switch *ds)
 
  		mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR);

drivers/net/dsa/mt7530.c mt7531_setup(struct dsa_switch *ds) · line 1799

Quoted 10 lines
drivers/net/dsa/mt7530.c +5 -3
 
-		if (dsa_is_cpu_port(ds, i))
-			mt753x_cpu_port_enable(ds, i);
-		else
+		if (dsa_is_cpu_port(ds, i)) {
+			ret = mt753x_cpu_port_enable(ds, i);
+			if (ret)
+				return ret;
+		} else
  			mt7530_port_disable(ds, i);

CHECK: braces {} should be used on all arms of this statement
CHECK: Unbalanced braces around else statement

Quoted 2 lines

[..]
regards landen

Quoted 7 lines

Hi Landen,

Sorry about this... I usually run checkpatch over my patches. Would you like me to send a separate fix or a v3?

Best,
Alex

Hi Alex,

Because v2 has not been merged yet, could you help to fix it in v3?

regards landen

author
#9 lore raw

PATCH v3 1/1 net: dsa: mt7530: Add some return-value checks

In mt7531_cpu_port_config(), if the variable port is neither 5 nor 6, then variable interface will be used uninitialised. Change the function to return -EINVAL in this case.

As the return value of mt7531_cpu_port_config() is never checked (even though it returns an int) add a check in the correct place so that the error can be passed up the call stack. Now that we correctly handle errors thrown in this function, also check the return value of mt7531_mac_config() in case an error occurs here. Also add misisng checks to mt7530_setup() and mt7531_setup(), which are another level further up the call stack.

Fixes c288575f7810 ("net: dsa: mt7530: Add the support of MT7531 switch")

Addresses-Coverity: 1496993 ("Uninitialized variables")

---

Hi Landen,

Here you go!

v3:
- fix checkpatch warning about braces (Landen)
v2:
- fix typo in commit message
- split variable declarations onto multiple lines (Gustavo)
- add additional checks for mt753*_setup (Landen)

 drivers/net/dsa/mt7530.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)
drivers/net/dsa/mt7530.c +24 -9
index 61388945d316..23b2386318b2 100644
@@ -945,10 +945,14 @@ static int
 mt753x_cpu_port_enable(struct dsa_switch *ds, int port)
 {
 	struct mt7530_priv *priv = ds->priv;
+	int ret;
 
 	/* Setup max capability of CPU port at first */
-	if (priv->info->cpu_port_config)
-		priv->info->cpu_port_config(ds, port);
+	if (priv->info->cpu_port_config) {
+		ret = priv->info->cpu_port_config(ds, port);
+		if (ret)
+			return ret;
+	}
 
 	/* Enable Mediatek header mode on the cpu port */
 	mt7530_write(priv, MT7530_PVC_P(port),
@@ -1631,10 +1635,13 @@ mt7530_setup(struct dsa_switch *ds)
 		mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK,
 			   PCR_MATRIX_CLR);
 
-		if (dsa_is_cpu_port(ds, i))
-			mt753x_cpu_port_enable(ds, i);
-		else
+		if (dsa_is_cpu_port(ds, i)) {
+			ret = mt753x_cpu_port_enable(ds, i);
+			if (ret)
+				return ret;
+		} else {
 			mt7530_port_disable(ds, i);
+		}
 
 		/* Enable consistent egress tag */
 		mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
@@ -1785,10 +1792,13 @@ mt7531_setup(struct dsa_switch *ds)
 
 		mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR);
 
-		if (dsa_is_cpu_port(ds, i))
-			mt753x_cpu_port_enable(ds, i);
-		else
+		if (dsa_is_cpu_port(ds, i)) {
+			ret = mt753x_cpu_port_enable(ds, i);
+			if (ret)
+				return ret;
+		} else {
 			mt7530_port_disable(ds, i);
+		}
 
 		/* Enable consistent egress tag */
 		mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK,
@@ -2276,6 +2286,7 @@ mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 	struct mt7530_priv *priv = ds->priv;
 	phy_interface_t interface;
 	int speed;
+	int ret;
 
 	switch (port) {
 	case 5:
@@ -2293,6 +2304,8 @@ mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 
 		priv->p6_interface = interface;
 		break;
+	default:
+		return -EINVAL;
 	}
 
 	if (interface == PHY_INTERFACE_MODE_2500BASEX)
@@ -2300,7 +2313,9 @@ mt7531_cpu_port_config(struct dsa_switch *ds, int port)
 	else
 		speed = SPEED_1000;
 
-	mt7531_mac_config(ds, port, MLO_AN_FIXED, interface);
+	ret = mt7531_mac_config(ds, port, MLO_AN_FIXED, interface);
+	if (ret)
+		return ret;
 	mt7530_write(priv, MT7530_PMCR_P(port),
 		     PMCR_CPU_PORT_SETTING(priv->id));
 	mt753x_phylink_mac_link_up(ds, port, MLO_AN_FIXED, interface, NULL,
Signature · 7 lines
2.28.0


_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek