netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next] net: emaclite: Add error handling for of_address_ and phy read functions
@ 2020-11-03 13:31 Radhey Shyam Pandey
  2020-11-03 16:20 ` Andrew Lunn
  2020-11-07 19:35 ` Jakub Kicinski
  0 siblings, 2 replies; 4+ messages in thread
From: Radhey Shyam Pandey @ 2020-11-03 13:31 UTC (permalink / raw)
  To: davem, kuba, michal.simek
  Cc: netdev, linux-arm-kernel, linux-kernel, git, Shravya Kumbham,
	Radhey Shyam Pandey

From: Shravya Kumbham <shravya.kumbham@xilinx.com>

Add ret variable, conditions to check the return value and it's error
path for of_address_to_resource() and phy_read() functions.

Addresses-Coverity: Event check_return value.
Signed-off-by: Shravya Kumbham <shravya.kumbham@xilinx.com>
Signed-off-by: Radhey Shyam Pandey <radhey.shyam.pandey@xilinx.com>
---
 drivers/net/ethernet/xilinx/xilinx_emaclite.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/xilinx/xilinx_emaclite.c b/drivers/net/ethernet/xilinx/xilinx_emaclite.c
index 0c26f5b..fc5ccd1 100644
--- a/drivers/net/ethernet/xilinx/xilinx_emaclite.c
+++ b/drivers/net/ethernet/xilinx/xilinx_emaclite.c
@@ -820,7 +820,7 @@ static int xemaclite_mdio_write(struct mii_bus *bus, int phy_id, int reg,
 static int xemaclite_mdio_setup(struct net_local *lp, struct device *dev)
 {
 	struct mii_bus *bus;
-	int rc;
+	int rc, ret;
 	struct resource res;
 	struct device_node *np = of_get_parent(lp->phy_node);
 	struct device_node *npp;
@@ -834,7 +834,13 @@ static int xemaclite_mdio_setup(struct net_local *lp, struct device *dev)
 	}
 	npp = of_get_parent(np);
 
-	of_address_to_resource(npp, 0, &res);
+	ret = of_address_to_resource(npp, 0, &res);
+	if (ret) {
+		dev_err(dev, "%s resource error!\n",
+			dev->of_node->full_name);
+		of_node_put(lp->phy_node);
+		return ret;
+	}
 	if (lp->ndev->mem_start != res.start) {
 		struct phy_device *phydev;
 		phydev = of_phy_find_device(lp->phy_node);
@@ -923,7 +929,7 @@ static int xemaclite_open(struct net_device *dev)
 	xemaclite_disable_interrupts(lp);
 
 	if (lp->phy_node) {
-		u32 bmcr;
+		int bmcr;
 
 		lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
 					     xemaclite_adjust_link, 0,
@@ -945,6 +951,13 @@ static int xemaclite_open(struct net_device *dev)
 
 		/* Restart auto negotiation */
 		bmcr = phy_read(lp->phy_dev, MII_BMCR);
+		if (bmcr < 0) {
+			dev_err(&lp->ndev->dev, "phy_read failed\n");
+			phy_disconnect(lp->phy_dev);
+			lp->phy_dev = NULL;
+
+			return bmcr;
+		}
 		bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
 		phy_write(lp->phy_dev, MII_BMCR, bmcr);
 
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net-next] net: emaclite: Add error handling for of_address_ and phy read functions
  2020-11-03 13:31 [PATCH net-next] net: emaclite: Add error handling for of_address_ and phy read functions Radhey Shyam Pandey
@ 2020-11-03 16:20 ` Andrew Lunn
  2020-11-07 19:35 ` Jakub Kicinski
  1 sibling, 0 replies; 4+ messages in thread
From: Andrew Lunn @ 2020-11-03 16:20 UTC (permalink / raw)
  To: Radhey Shyam Pandey
  Cc: davem, kuba, michal.simek, netdev, linux-arm-kernel,
	linux-kernel, git, Shravya Kumbham

On Tue, Nov 03, 2020 at 07:01:05PM +0530, Radhey Shyam Pandey wrote:
> From: Shravya Kumbham <shravya.kumbham@xilinx.com>
> 
> Add ret variable, conditions to check the return value and it's error
> path for of_address_to_resource() and phy_read() functions.
> 
> Addresses-Coverity: Event check_return value.

Hi Radhey

This is well out of scope of a Coverity fix, but looking at the patch
i noticed some bad things.

> @@ -923,7 +929,7 @@ static int xemaclite_open(struct net_device *dev)
>  	xemaclite_disable_interrupts(lp);
>  
>  	if (lp->phy_node) {
> -		u32 bmcr;
> +		int bmcr;
>  
>  		lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
>  					     xemaclite_adjust_link, 0,
> @@ -945,6 +951,13 @@ static int xemaclite_open(struct net_device *dev)
>  
>  		/* Restart auto negotiation */
>  		bmcr = phy_read(lp->phy_dev, MII_BMCR);
> +		if (bmcr < 0) {
> +			dev_err(&lp->ndev->dev, "phy_read failed\n");
> +			phy_disconnect(lp->phy_dev);
> +			lp->phy_dev = NULL;
> +
> +			return bmcr;
> +		}
>  		bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
>  		phy_write(lp->phy_dev, MII_BMCR, bmcr);

A MAC driver should not be touching the PHY. The call to
phy_set_max_speed() should prevent the PHY from advertising 1G speeds,
so there is no need to poke the advertise registers. And phy_start()
will start auto-get if it is enabled.

It would be nice if this code got cleaned up.

   Andrew

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net-next] net: emaclite: Add error handling for of_address_ and phy read functions
  2020-11-03 13:31 [PATCH net-next] net: emaclite: Add error handling for of_address_ and phy read functions Radhey Shyam Pandey
  2020-11-03 16:20 ` Andrew Lunn
@ 2020-11-07 19:35 ` Jakub Kicinski
  2020-11-10  6:02   ` Radhey Shyam Pandey
  1 sibling, 1 reply; 4+ messages in thread
From: Jakub Kicinski @ 2020-11-07 19:35 UTC (permalink / raw)
  To: Radhey Shyam Pandey
  Cc: davem, michal.simek, netdev, linux-arm-kernel, linux-kernel, git,
	Shravya Kumbham

On Tue, 3 Nov 2020 19:01:05 +0530 Radhey Shyam Pandey wrote:
> From: Shravya Kumbham <shravya.kumbham@xilinx.com>
> 
> Add ret variable, conditions to check the return value and it's error
> path for of_address_to_resource() and phy_read() functions.
> 
> Addresses-Coverity: Event check_return value.
> Signed-off-by: Shravya Kumbham <shravya.kumbham@xilinx.com>
> Signed-off-by: Radhey Shyam Pandey <radhey.shyam.pandey@xilinx.com>

Any reason not to apply this to net as a fix?

> diff --git a/drivers/net/ethernet/xilinx/xilinx_emaclite.c b/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> index 0c26f5b..fc5ccd1 100644
> --- a/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> +++ b/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> @@ -820,7 +820,7 @@ static int xemaclite_mdio_write(struct mii_bus *bus, int phy_id, int reg,
>  static int xemaclite_mdio_setup(struct net_local *lp, struct device *dev)
>  {
>  	struct mii_bus *bus;
> -	int rc;
> +	int rc, ret;
>  	struct resource res;
>  	struct device_node *np = of_get_parent(lp->phy_node);
>  	struct device_node *npp;
> @@ -834,7 +834,13 @@ static int xemaclite_mdio_setup(struct net_local *lp, struct device *dev)
>  	}
>  	npp = of_get_parent(np);
>  
> -	of_address_to_resource(npp, 0, &res);
> +	ret = of_address_to_resource(npp, 0, &res);
> +	if (ret) {
> +		dev_err(dev, "%s resource error!\n",
> +			dev->of_node->full_name);
> +		of_node_put(lp->phy_node);

I'm always confused by the of_* refcounting. Why do you need to put
phy_node here, and nowhere else in this function?

> +		return ret;
> +	}

>  		/* Restart auto negotiation */
>  		bmcr = phy_read(lp->phy_dev, MII_BMCR);
> +		if (bmcr < 0) {
> +			dev_err(&lp->ndev->dev, "phy_read failed\n");
> +			phy_disconnect(lp->phy_dev);
> +			lp->phy_dev = NULL;
> +
> +			return bmcr;
> +		}
>  		bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
>  		phy_write(lp->phy_dev, MII_BMCR, bmcr);

Does it really make much sense to validate the return value of
phy_read() but not check any errors from phy_write()s?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: [PATCH net-next] net: emaclite: Add error handling for of_address_ and phy read functions
  2020-11-07 19:35 ` Jakub Kicinski
@ 2020-11-10  6:02   ` Radhey Shyam Pandey
  0 siblings, 0 replies; 4+ messages in thread
From: Radhey Shyam Pandey @ 2020-11-10  6:02 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: davem, Michal Simek, netdev, linux-arm-kernel, linux-kernel, git,
	Shravya Kumbham

> -----Original Message-----
> From: Jakub Kicinski <kuba@kernel.org>
> Sent: Sunday, November 8, 2020 1:05 AM
> To: Radhey Shyam Pandey <radheys@xilinx.com>
> Cc: davem@davemloft.net; Michal Simek <michals@xilinx.com>;
> netdev@vger.kernel.org; linux-arm-kernel@lists.infradead.org; linux-
> kernel@vger.kernel.org; git <git@xilinx.com>; Shravya Kumbham
> <shravyak@xilinx.com>
> Subject: Re: [PATCH net-next] net: emaclite: Add error handling for
> of_address_ and phy read functions
> 
> On Tue, 3 Nov 2020 19:01:05 +0530 Radhey Shyam Pandey wrote:
> > From: Shravya Kumbham <shravya.kumbham@xilinx.com>
> >
> > Add ret variable, conditions to check the return value and it's error
> > path for of_address_to_resource() and phy_read() functions.
> >
> > Addresses-Coverity: Event check_return value.
> > Signed-off-by: Shravya Kumbham <shravya.kumbham@xilinx.com>
> > Signed-off-by: Radhey Shyam Pandey <radhey.shyam.pandey@xilinx.com>
> 
> Any reason not to apply this to net as a fix?
Yes, it can be applied to net as a fix. 
> 
> > diff --git a/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> > b/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> > index 0c26f5b..fc5ccd1 100644
> > --- a/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> > +++ b/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> > @@ -820,7 +820,7 @@ static int xemaclite_mdio_write(struct mii_bus
> > *bus, int phy_id, int reg,  static int xemaclite_mdio_setup(struct
> > net_local *lp, struct device *dev)  {
> >  	struct mii_bus *bus;
> > -	int rc;
> > +	int rc, ret;
> >  	struct resource res;
> >  	struct device_node *np = of_get_parent(lp->phy_node);
> >  	struct device_node *npp;
> > @@ -834,7 +834,13 @@ static int xemaclite_mdio_setup(struct net_local
> *lp, struct device *dev)
> >  	}
> >  	npp = of_get_parent(np);
> >
> > -	of_address_to_resource(npp, 0, &res);
> > +	ret = of_address_to_resource(npp, 0, &res);
> > +	if (ret) {
> > +		dev_err(dev, "%s resource error!\n",
> > +			dev->of_node->full_name);
> > +		of_node_put(lp->phy_node);
> 
> I'm always confused by the of_* refcounting. Why do you need to put
> phy_node here, and nowhere else in this function?

Initially, we added of_node_put(phy_node) thinking about this 
particular coverity change. But agree it has to be added for
all error path i.e better place would be in xemaclite_of_probe()
error label.

> 
> > +		return ret;
> > +	}
> 
> >  		/* Restart auto negotiation */
> >  		bmcr = phy_read(lp->phy_dev, MII_BMCR);
> > +		if (bmcr < 0) {
> > +			dev_err(&lp->ndev->dev, "phy_read failed\n");
> > +			phy_disconnect(lp->phy_dev);
> > +			lp->phy_dev = NULL;
> > +
> > +			return bmcr;
> > +		}
> >  		bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
> >  		phy_write(lp->phy_dev, MII_BMCR, bmcr);
> 
> Does it really make much sense to validate the return value of
> phy_read() but not check any errors from phy_write()s?
Error handling was added for phy_read as it was using return value
and reported by coverity. But yes we in a follow-up patch we
can extend error handling for phy_write as well.
 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-11-10  6:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-03 13:31 [PATCH net-next] net: emaclite: Add error handling for of_address_ and phy read functions Radhey Shyam Pandey
2020-11-03 16:20 ` Andrew Lunn
2020-11-07 19:35 ` Jakub Kicinski
2020-11-10  6:02   ` Radhey Shyam Pandey

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