netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next RFC] net: dsa: mv88e6xxx: Correct check for empty list
@ 2024-04-19 12:17 Simon Horman
  2024-04-19 15:44 ` Andrew Lunn
  2024-04-22 10:40 ` Dan Carpenter
  0 siblings, 2 replies; 4+ messages in thread
From: Simon Horman @ 2024-04-19 12:17 UTC (permalink / raw)
  To: Andrew Lunn, Florian Fainelli, Vladimir Oltean
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Dan Carpenter, netdev

Since commit a3c53be55c95 ("net: dsa: mv88e6xxx: Support multiple MDIO
busses") mv88e6xxx_default_mdio_bus() has checked that the
return value of list_first_entry() is non-NULL. This appears to be
intended to guard against the list chip->mdios being empty.
However, it is not the correct check as the implementation of
list_first_entry is not designed to return NULL for empty lists.

Instead check directly if the list is empty.

Flagged by Smatch

Signed-off-by: Simon Horman <horms@kernel.org>
---
I'm unsure if this should be considered a fix: it's been around since
v4.11 and the patch is dated January 2017. Perhaps an empty list simply
cannot occur. If so, the function could be simplified by not checking
for an empty list. And, if mdio_bus->bus, then perhaps caller may be
simplified not to check for an error condition.

It is because I am unsure that I have marked this as an RFC.

diff --git a/drivers/net/dsa/mv88e6xxx/chip.c b/drivers/net/dsa/mv88e6xxx/chip.c
index e950a634a3c7..a236c9fe6a74 100644
--- a/drivers/net/dsa/mv88e6xxx/chip.c
+++ b/drivers/net/dsa/mv88e6xxx/chip.c
@@ -131,10 +131,11 @@ struct mii_bus *mv88e6xxx_default_mdio_bus(struct mv88e6xxx_chip *chip)
 {
 	struct mv88e6xxx_mdio_bus *mdio_bus;

+	if (list_empty(&chip->mdios))
+		return NULL;
+
 	mdio_bus = list_first_entry(&chip->mdios, struct mv88e6xxx_mdio_bus,
 				    list);
-	if (!mdio_bus)
-		return NULL;

 	return mdio_bus->bus;
 }
---
 drivers/net/dsa/mv88e6xxx/chip.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/net/dsa/mv88e6xxx/chip.c b/drivers/net/dsa/mv88e6xxx/chip.c
index e950a634a3c7..a236c9fe6a74 100644
--- a/drivers/net/dsa/mv88e6xxx/chip.c
+++ b/drivers/net/dsa/mv88e6xxx/chip.c
@@ -131,10 +131,11 @@ struct mii_bus *mv88e6xxx_default_mdio_bus(struct mv88e6xxx_chip *chip)
 {
 	struct mv88e6xxx_mdio_bus *mdio_bus;
 
+	if (list_empty(&chip->mdios))
+		return NULL;
+
 	mdio_bus = list_first_entry(&chip->mdios, struct mv88e6xxx_mdio_bus,
 				    list);
-	if (!mdio_bus)
-		return NULL;
 
 	return mdio_bus->bus;
 }


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

* Re: [PATCH net-next RFC] net: dsa: mv88e6xxx: Correct check for empty list
  2024-04-19 12:17 [PATCH net-next RFC] net: dsa: mv88e6xxx: Correct check for empty list Simon Horman
@ 2024-04-19 15:44 ` Andrew Lunn
  2024-04-22 10:40 ` Dan Carpenter
  1 sibling, 0 replies; 4+ messages in thread
From: Andrew Lunn @ 2024-04-19 15:44 UTC (permalink / raw)
  To: Simon Horman
  Cc: Florian Fainelli, Vladimir Oltean, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Dan Carpenter, netdev

On Fri, Apr 19, 2024 at 01:17:48PM +0100, Simon Horman wrote:
> Since commit a3c53be55c95 ("net: dsa: mv88e6xxx: Support multiple MDIO
> busses") mv88e6xxx_default_mdio_bus() has checked that the
> return value of list_first_entry() is non-NULL. This appears to be
> intended to guard against the list chip->mdios being empty.
> However, it is not the correct check as the implementation of
> list_first_entry is not designed to return NULL for empty lists.
> 
> Instead check directly if the list is empty.
> 
> Flagged by Smatch
> 
> Signed-off-by: Simon Horman <horms@kernel.org>

Hi Simon

This looks good to me. I would not consider it a fix. As you say, it
has been like this a long time and never bothered anybody, which is
one of the stable rules. It might be possible to have an empty list,
if there are no nodes in DT. But that is something which a novice
would do when writing the DT, and so probably would of reported it.

However, list_first_entry() does document:

* Note, that list is expected to be not empty.

So testing it first is wise.

Reviewed-by: Andrew Lunn <andrew@lunn.ch>

Thanks
	Andrew

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

* Re: [PATCH net-next RFC] net: dsa: mv88e6xxx: Correct check for empty list
  2024-04-19 12:17 [PATCH net-next RFC] net: dsa: mv88e6xxx: Correct check for empty list Simon Horman
  2024-04-19 15:44 ` Andrew Lunn
@ 2024-04-22 10:40 ` Dan Carpenter
  2024-04-23 19:52   ` Simon Horman
  1 sibling, 1 reply; 4+ messages in thread
From: Dan Carpenter @ 2024-04-22 10:40 UTC (permalink / raw)
  To: Simon Horman
  Cc: Andrew Lunn, Florian Fainelli, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev

On Fri, Apr 19, 2024 at 01:17:48PM +0100, Simon Horman wrote:
> Since commit a3c53be55c95 ("net: dsa: mv88e6xxx: Support multiple MDIO
> busses") mv88e6xxx_default_mdio_bus() has checked that the
> return value of list_first_entry() is non-NULL. This appears to be
> intended to guard against the list chip->mdios being empty.
> However, it is not the correct check as the implementation of
> list_first_entry is not designed to return NULL for empty lists.
> 
> Instead check directly if the list is empty.
> 
> Flagged by Smatch
> 
> Signed-off-by: Simon Horman <horms@kernel.org>
> ---
> I'm unsure if this should be considered a fix: it's been around since
> v4.11 and the patch is dated January 2017. Perhaps an empty list simply
> cannot occur. If so, the function could be simplified by not checking
> for an empty list. And, if mdio_bus->bus, then perhaps caller may be
> simplified not to check for an error condition.
> 
> It is because I am unsure that I have marked this as an RFC.
> 
> diff --git a/drivers/net/dsa/mv88e6xxx/chip.c b/drivers/net/dsa/mv88e6xxx/chip.c
> index e950a634a3c7..a236c9fe6a74 100644
> --- a/drivers/net/dsa/mv88e6xxx/chip.c
> +++ b/drivers/net/dsa/mv88e6xxx/chip.c
> @@ -131,10 +131,11 @@ struct mii_bus *mv88e6xxx_default_mdio_bus(struct mv88e6xxx_chip *chip)
>  {
>  	struct mv88e6xxx_mdio_bus *mdio_bus;
> 
> +	if (list_empty(&chip->mdios))
> +		return NULL;
> +
>  	mdio_bus = list_first_entry(&chip->mdios, struct mv88e6xxx_mdio_bus,
>  				    list);
> -	if (!mdio_bus)
> -		return NULL;

The other option here would have been to use list_first_entry_or_null().

regards,
dan carpenter


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

* Re: [PATCH net-next RFC] net: dsa: mv88e6xxx: Correct check for empty list
  2024-04-22 10:40 ` Dan Carpenter
@ 2024-04-23 19:52   ` Simon Horman
  0 siblings, 0 replies; 4+ messages in thread
From: Simon Horman @ 2024-04-23 19:52 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Andrew Lunn, Florian Fainelli, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev

On Mon, Apr 22, 2024 at 01:40:01PM +0300, Dan Carpenter wrote:
> On Fri, Apr 19, 2024 at 01:17:48PM +0100, Simon Horman wrote:
> > Since commit a3c53be55c95 ("net: dsa: mv88e6xxx: Support multiple MDIO
> > busses") mv88e6xxx_default_mdio_bus() has checked that the
> > return value of list_first_entry() is non-NULL. This appears to be
> > intended to guard against the list chip->mdios being empty.
> > However, it is not the correct check as the implementation of
> > list_first_entry is not designed to return NULL for empty lists.
> > 
> > Instead check directly if the list is empty.
> > 
> > Flagged by Smatch
> > 
> > Signed-off-by: Simon Horman <horms@kernel.org>
> > ---
> > I'm unsure if this should be considered a fix: it's been around since
> > v4.11 and the patch is dated January 2017. Perhaps an empty list simply
> > cannot occur. If so, the function could be simplified by not checking
> > for an empty list. And, if mdio_bus->bus, then perhaps caller may be
> > simplified not to check for an error condition.
> > 
> > It is because I am unsure that I have marked this as an RFC.
> > 
> > diff --git a/drivers/net/dsa/mv88e6xxx/chip.c b/drivers/net/dsa/mv88e6xxx/chip.c
> > index e950a634a3c7..a236c9fe6a74 100644
> > --- a/drivers/net/dsa/mv88e6xxx/chip.c
> > +++ b/drivers/net/dsa/mv88e6xxx/chip.c
> > @@ -131,10 +131,11 @@ struct mii_bus *mv88e6xxx_default_mdio_bus(struct mv88e6xxx_chip *chip)
> >  {
> >  	struct mv88e6xxx_mdio_bus *mdio_bus;
> > 
> > +	if (list_empty(&chip->mdios))
> > +		return NULL;
> > +
> >  	mdio_bus = list_first_entry(&chip->mdios, struct mv88e6xxx_mdio_bus,
> >  				    list);
> > -	if (!mdio_bus)
> > -		return NULL;
> 
> The other option here would have been to use list_first_entry_or_null().

Thanks, I guess that is nicer than the open-coded approach I suggested.


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

end of thread, other threads:[~2024-04-23 19:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-19 12:17 [PATCH net-next RFC] net: dsa: mv88e6xxx: Correct check for empty list Simon Horman
2024-04-19 15:44 ` Andrew Lunn
2024-04-22 10:40 ` Dan Carpenter
2024-04-23 19:52   ` Simon Horman

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