net: sfp: remove redundant NULL check

Message ID 20240211150824.3947-1-d.dulov@aladdin.ru
State New
Headers
Series net: sfp: remove redundant NULL check |

Commit Message

Daniil Dulov Feb. 11, 2024, 3:08 p.m. UTC
  bus->upstream_ops in sfp_register_bus() cannot be NULL. So remove
redundant NULL check.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: ce0aa27ff3f6 ("sfp: add sfp-bus to bridge between network devices and sfp cages")
Signed-off-by: Daniil Dulov <d.dulov@aladdin.ru>
---
 drivers/net/phy/sfp-bus.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
  

Comments

Paolo Abeni Feb. 13, 2024, 12:43 p.m. UTC | #1
On Sun, 2024-02-11 at 07:08 -0800, Daniil Dulov wrote:
> bus->upstream_ops in sfp_register_bus() cannot be NULL. So remove
> redundant NULL check.

I'm unsure about that?!? in theory drivers could call
sfp_bus_add_upstream()/phy_sfp_probe() with NULL ops, even it that very
likely doesn't make any sense.

@Russel, @Andrew: WDYT?

Thanks,

Paolo
  
Jakub Kicinski Feb. 14, 2024, 1:26 a.m. UTC | #2
On Tue, 13 Feb 2024 13:43:57 +0100 Paolo Abeni wrote:
> On Sun, 2024-02-11 at 07:08 -0800, Daniil Dulov wrote:
> > bus->upstream_ops in sfp_register_bus() cannot be NULL. So remove
> > redundant NULL check.  
> 
> I'm unsure about that?!? in theory drivers could call
> sfp_bus_add_upstream()/phy_sfp_probe() with NULL ops, even it that very
> likely doesn't make any sense.
> 
> @Russel, @Andrew: WDYT?

Since Russell is AFK let me discard this instead of queuing.
We'll resurrect if any of the maintainers sends review tags or alike.
  
Russell King (Oracle) Feb. 15, 2024, 4:02 p.m. UTC | #3
On Sun, Feb 11, 2024 at 07:08:24AM -0800, Daniil Dulov wrote:
> bus->upstream_ops in sfp_register_bus() cannot be NULL. So remove
> redundant NULL check.
> 
> Found by Linux Verification Center (linuxtesting.org) with SVACE.

It probably would've been better to include in here details of the two
paths that lead to this point, and indicate why it's safe to remove the
NULL check.

The first path is via sfp_register_socket(), which checks that
bus->upstream_ops is not NULL prior to calling sfp_register_bus().
Therefore, "ops" can not be NULL when sfp_register_bus() is called
via this path.

The second path is via sfp_bus_add_upstream(), and this path assumes
that the "ops" passed into this function will not be NULL. Nothing in
this code makes that guarantee, and it's up to the design(er) to
determine whether NULL is permitted or not. It's not something that
an automated checker ought to be suggesting.

In this particular instance, I, as the interface designer, do indeed
intend that "ops" will not be NULL here, so the patch can remove the
check is acceptable in this instance.

However, I'll go back to my original point: this is *not* something
that automated tools should be identifying, and it is *not* something
that should be used to throw patches randomly out, especially where
the commit message doesn't include human analysis details.


> 
> Fixes: ce0aa27ff3f6 ("sfp: add sfp-bus to bridge between network devices and sfp cages")
> Signed-off-by: Daniil Dulov <d.dulov@aladdin.ru>
> ---
>  drivers/net/phy/sfp-bus.c | 14 ++++++--------
>  1 file changed, 6 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/net/phy/sfp-bus.c b/drivers/net/phy/sfp-bus.c
> index 850915a37f4c..829cb1dccc27 100644
> --- a/drivers/net/phy/sfp-bus.c
> +++ b/drivers/net/phy/sfp-bus.c
> @@ -478,14 +478,12 @@ static int sfp_register_bus(struct sfp_bus *bus)
>  	const struct sfp_upstream_ops *ops = bus->upstream_ops;
>  	int ret;
>  
> -	if (ops) {
> -		if (ops->link_down)
> -			ops->link_down(bus->upstream);
> -		if (ops->connect_phy && bus->phydev) {
> -			ret = ops->connect_phy(bus->upstream, bus->phydev);
> -			if (ret)
> -				return ret;
> -		}
> +	if (ops->link_down)
> +		ops->link_down(bus->upstream);
> +	if (ops->connect_phy && bus->phydev) {
> +		ret = ops->connect_phy(bus->upstream, bus->phydev);
> +		if (ret)
> +			return ret;
>  	}
>  	bus->registered = true;
>  	bus->socket_ops->attach(bus->sfp);
> -- 
> 2.25.1
> 
>
  
Andrew Lunn Feb. 15, 2024, 5:32 p.m. UTC | #4
> However, I'll go back to my original point: this is *not* something
> that automated tools should be identifying, and it is *not* something
> that should be used to throw patches randomly out, especially where
> the commit message doesn't include human analysis details.

Hi Daniil

Could you work on SVACE and make it dump how it decided it was safe to
remove the NULL check. I assume it found the path via
sfp_register_socket(), and the NULL check in that. So it should be
able to dump that info in some form. sfp_bus_add_upstream() seems more
interesting and it would be interesting to know why it though a NULL
from there was impossible.

It would be great if the tool dumped some text which could be
cut/paste into the commit message as a justification for the change.

	Andrew
  

Patch

diff --git a/drivers/net/phy/sfp-bus.c b/drivers/net/phy/sfp-bus.c
index 850915a37f4c..829cb1dccc27 100644
--- a/drivers/net/phy/sfp-bus.c
+++ b/drivers/net/phy/sfp-bus.c
@@ -478,14 +478,12 @@  static int sfp_register_bus(struct sfp_bus *bus)
 	const struct sfp_upstream_ops *ops = bus->upstream_ops;
 	int ret;
 
-	if (ops) {
-		if (ops->link_down)
-			ops->link_down(bus->upstream);
-		if (ops->connect_phy && bus->phydev) {
-			ret = ops->connect_phy(bus->upstream, bus->phydev);
-			if (ret)
-				return ret;
-		}
+	if (ops->link_down)
+		ops->link_down(bus->upstream);
+	if (ops->connect_phy && bus->phydev) {
+		ret = ops->connect_phy(bus->upstream, bus->phydev);
+		if (ret)
+			return ret;
 	}
 	bus->registered = true;
 	bus->socket_ops->attach(bus->sfp);