diff mbox series

[net-next,RFC,v2] net: dsa: mv88e6xxx: Correct check for empty list

Message ID 20240427-mv88e6xx-list_empty-v2-1-b7ce47c77bc7@kernel.org (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [net-next,RFC,v2] net: dsa: mv88e6xxx: Correct check for empty list | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 926 this patch: 926
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 7 of 7 maintainers
netdev/build_clang success Errors and warnings before: 937 this patch: 937
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 937 this patch: 937
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 10 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Simon Horman April 27, 2024, 8:52 a.m. UTC
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, use list_first_entry() which does return NULL if the list is
empty.

Flagged by Smatch.
Compile tested only.

Signed-off-by: Simon Horman <horms@kernel.org>
---
Changes in v2:
- Use list_first_entry_or_null() instead of open-coding
  a condition on list_empty().
  Suggested by Dan Carpenter.
- Update commit message.
- Link to v1: https://lore.kernel.org/r/20240419-mv88e6xx-list_empty-v1-1-64fd6d1059a8@kernel.org
---
As discussed in v1, this is not being considered a fix
as it has been like this for a long time without any
reported problems.
---
 drivers/net/dsa/mv88e6xxx/chip.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Simon Horman April 27, 2024, 8:53 a.m. UTC | #1
On Sat, Apr 27, 2024 at 09:52:03AM +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, use list_first_entry() which does return NULL if the list is
> empty.
> 
> Flagged by Smatch.
> Compile tested only.
> 
> Signed-off-by: Simon Horman <horms@kernel.org>

Sorry, I forgot to drop the RFC tag.
I'll send a v3 after waiting for review.

> ---
> Changes in v2:
> - Use list_first_entry_or_null() instead of open-coding
>   a condition on list_empty().
>   Suggested by Dan Carpenter.
> - Update commit message.
> - Link to v1: https://lore.kernel.org/r/20240419-mv88e6xx-list_empty-v1-1-64fd6d1059a8@kernel.org
> ---
> As discussed in v1, this is not being considered a fix
> as it has been like this for a long time without any
> reported problems.
> ---
>  drivers/net/dsa/mv88e6xxx/chip.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/dsa/mv88e6xxx/chip.c b/drivers/net/dsa/mv88e6xxx/chip.c
> index f29ef72a2f1d..fc6e2e3ab0f0 100644
> --- a/drivers/net/dsa/mv88e6xxx/chip.c
> +++ b/drivers/net/dsa/mv88e6xxx/chip.c
> @@ -131,8 +131,8 @@ struct mii_bus *mv88e6xxx_default_mdio_bus(struct mv88e6xxx_chip *chip)
>  {
>  	struct mv88e6xxx_mdio_bus *mdio_bus;
>  
> -	mdio_bus = list_first_entry(&chip->mdios, struct mv88e6xxx_mdio_bus,
> -				    list);
> +	mdio_bus = list_first_entry_or_null(&chip->mdios,
> +					    struct mv88e6xxx_mdio_bus, list);
>  	if (!mdio_bus)
>  		return NULL;
>  
> 
>
Dan Carpenter April 27, 2024, 9:09 a.m. UTC | #2
On Sat, Apr 27, 2024 at 09:53:06AM +0100, Simon Horman wrote:
> On Sat, Apr 27, 2024 at 09:52:03AM +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, use list_first_entry() which does return NULL if the list is
                 ^^^^^^^^^^^^^^^^^^
list_first_entry_or_null()

regards,
dan carpenter
Andrew Lunn April 27, 2024, 1:23 p.m. UTC | #3
On Sat, Apr 27, 2024 at 09:52:03AM +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, use list_first_entry() which does return NULL if the list is
> empty.
> 
> Flagged by Smatch.
> Compile tested only.
> 
> Signed-off-by: Simon Horman <horms@kernel.org>

For the code itself:

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

    Andrew
diff mbox series

Patch

diff --git a/drivers/net/dsa/mv88e6xxx/chip.c b/drivers/net/dsa/mv88e6xxx/chip.c
index f29ef72a2f1d..fc6e2e3ab0f0 100644
--- a/drivers/net/dsa/mv88e6xxx/chip.c
+++ b/drivers/net/dsa/mv88e6xxx/chip.c
@@ -131,8 +131,8 @@  struct mii_bus *mv88e6xxx_default_mdio_bus(struct mv88e6xxx_chip *chip)
 {
 	struct mv88e6xxx_mdio_bus *mdio_bus;
 
-	mdio_bus = list_first_entry(&chip->mdios, struct mv88e6xxx_mdio_bus,
-				    list);
+	mdio_bus = list_first_entry_or_null(&chip->mdios,
+					    struct mv88e6xxx_mdio_bus, list);
 	if (!mdio_bus)
 		return NULL;