diff mbox series

[net-next,v2,1/4] net: phy: fixed_phy: Fix return value check for fixed_phy_get_gpiod

Message ID 20230817121631.1878897-2-ruanjinjie@huawei.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: Fix return value check for fixed_phy_register() | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
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: 1330 this patch: 1330
netdev/cc_maintainers fail 1 blamed authors not CCed: f.fainelli@gmail.com; 1 maintainers not CCed: f.fainelli@gmail.com
netdev/build_clang success Errors and warnings before: 1353 this patch: 1353
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 Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 1353 this patch: 1353
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 10 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Jinjie Ruan Aug. 17, 2023, 12:16 p.m. UTC
Since fixed_phy_get_gpiod() return NULL instead of ERR_PTR(),
if it fails, the IS_ERR() can never return the error. So check NULL
and return ERR_PTR(-EINVAL) if fails.

Fixes: 71bd106d2567 ("net: fixed-phy: Add fixed_phy_register_with_gpiod() API")
Signed-off-by: Ruan Jinjie <ruanjinjie@huawei.com>
---
 drivers/net/phy/fixed_phy.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Andrew Lunn Aug. 17, 2023, 12:39 p.m. UTC | #1
On Thu, Aug 17, 2023 at 08:16:28PM +0800, Ruan Jinjie wrote:
> Since fixed_phy_get_gpiod() return NULL instead of ERR_PTR(),
> if it fails, the IS_ERR() can never return the error. So check NULL
> and return ERR_PTR(-EINVAL) if fails.
> 
> Fixes: 71bd106d2567 ("net: fixed-phy: Add fixed_phy_register_with_gpiod() API")
> Signed-off-by: Ruan Jinjie <ruanjinjie@huawei.com>

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

    Andrew
Russell King (Oracle) Aug. 17, 2023, 1:10 p.m. UTC | #2
On Thu, Aug 17, 2023 at 08:16:28PM +0800, Ruan Jinjie wrote:
> Since fixed_phy_get_gpiod() return NULL instead of ERR_PTR(),
> if it fails, the IS_ERR() can never return the error. So check NULL
> and return ERR_PTR(-EINVAL) if fails.

No, this is totally and utterly wrong, and this patch introduces a new
bug. The original code is _correct_.

> Fixes: 71bd106d2567 ("net: fixed-phy: Add fixed_phy_register_with_gpiod() API")
> Signed-off-by: Ruan Jinjie <ruanjinjie@huawei.com>
> ---
>  drivers/net/phy/fixed_phy.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/phy/fixed_phy.c b/drivers/net/phy/fixed_phy.c
> index aef739c20ac4..4e7406455b6e 100644
> --- a/drivers/net/phy/fixed_phy.c
> +++ b/drivers/net/phy/fixed_phy.c
> @@ -239,8 +239,8 @@ static struct phy_device *__fixed_phy_register(unsigned int irq,
>  	/* Check if we have a GPIO associated with this fixed phy */
>  	if (!gpiod) {
>  		gpiod = fixed_phy_get_gpiod(np);
> -		if (IS_ERR(gpiod))
> -			return ERR_CAST(gpiod);
> +		if (!gpiod)
> +			return ERR_PTR(-EINVAL);

Let's look at fixed_phy_get_gpiod():

        gpiod = fwnode_gpiod_get_index(of_fwnode_handle(fixed_link_node),
                                       "link", 0, GPIOD_IN, "mdio");
        if (IS_ERR(gpiod) && PTR_ERR(gpiod) != -EPROBE_DEFER) {
...
		gpiod = NULL;
	}
...
	return gpiod;

If fwnode_gpiod_get_index() returns -EPROBE_DEFER, _then_ we return an
error pointer. So it _does_ return an error pointer.

It also returns NULL when there is no device node passed to it, or
if there is no fixed-link specifier, or there is some other error
from fwnode_gpiod_get_index().

Otherwise, it returns a valid pointer to a gpio descriptor.

The gpio is optional. The device node is optional. When
fixed_phy_get_gpiod() returns NULL, it is _not_ an error, it means
that we don't have a GPIO. Just because something returns NULL does
_not_ mean it's an error - please get out of that thinking, because
if you don't your patches will introduce lots of new bugs.

Only when fwnode_gpiod_get_index() wants to defer probe do we return
an error.

So, sorry but NAK to this patch, it is incorrect.
Jinjie Ruan Aug. 17, 2023, 1:32 p.m. UTC | #3
On 2023/8/17 21:10, Russell King (Oracle) wrote:
> On Thu, Aug 17, 2023 at 08:16:28PM +0800, Ruan Jinjie wrote:
>> Since fixed_phy_get_gpiod() return NULL instead of ERR_PTR(),
>> if it fails, the IS_ERR() can never return the error. So check NULL
>> and return ERR_PTR(-EINVAL) if fails.
> 
> No, this is totally and utterly wrong, and this patch introduces a new
> bug. The original code is _correct_.
> 
>> Fixes: 71bd106d2567 ("net: fixed-phy: Add fixed_phy_register_with_gpiod() API")
>> Signed-off-by: Ruan Jinjie <ruanjinjie@huawei.com>
>> ---
>>  drivers/net/phy/fixed_phy.c | 4 ++--
>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/net/phy/fixed_phy.c b/drivers/net/phy/fixed_phy.c
>> index aef739c20ac4..4e7406455b6e 100644
>> --- a/drivers/net/phy/fixed_phy.c
>> +++ b/drivers/net/phy/fixed_phy.c
>> @@ -239,8 +239,8 @@ static struct phy_device *__fixed_phy_register(unsigned int irq,
>>  	/* Check if we have a GPIO associated with this fixed phy */
>>  	if (!gpiod) {
>>  		gpiod = fixed_phy_get_gpiod(np);
>> -		if (IS_ERR(gpiod))
>> -			return ERR_CAST(gpiod);
>> +		if (!gpiod)
>> +			return ERR_PTR(-EINVAL);
> 
> Let's look at fixed_phy_get_gpiod():
> 
>         gpiod = fwnode_gpiod_get_index(of_fwnode_handle(fixed_link_node),
>                                        "link", 0, GPIOD_IN, "mdio");
>         if (IS_ERR(gpiod) && PTR_ERR(gpiod) != -EPROBE_DEFER) {
> ...
> 		gpiod = NULL;
> 	}
> ...
> 	return gpiod;
> 
> If fwnode_gpiod_get_index() returns -EPROBE_DEFER, _then_ we return an
> error pointer. So it _does_ return an error pointer.
> 
> It also returns NULL when there is no device node passed to it, or
> if there is no fixed-link specifier, or there is some other error
> from fwnode_gpiod_get_index().
> 
> Otherwise, it returns a valid pointer to a gpio descriptor.
> 
> The gpio is optional. The device node is optional. When
> fixed_phy_get_gpiod() returns NULL, it is _not_ an error, it means
> that we don't have a GPIO. Just because something returns NULL does
> _not_ mean it's an error - please get out of that thinking, because
> if you don't your patches will introduce lots of new bugs.

Thank you, I understand what you mean, NULL is not an error here, so it
is not handled.

> 
> Only when fwnode_gpiod_get_index() wants to defer probe do we return
> an error.
> 
> So, sorry but NAK to this patch, it is incorrect.
>
diff mbox series

Patch

diff --git a/drivers/net/phy/fixed_phy.c b/drivers/net/phy/fixed_phy.c
index aef739c20ac4..4e7406455b6e 100644
--- a/drivers/net/phy/fixed_phy.c
+++ b/drivers/net/phy/fixed_phy.c
@@ -239,8 +239,8 @@  static struct phy_device *__fixed_phy_register(unsigned int irq,
 	/* Check if we have a GPIO associated with this fixed phy */
 	if (!gpiod) {
 		gpiod = fixed_phy_get_gpiod(np);
-		if (IS_ERR(gpiod))
-			return ERR_CAST(gpiod);
+		if (!gpiod)
+			return ERR_PTR(-EINVAL);
 	}
 
 	/* Get the next available PHY address, up to PHY_MAX_ADDR */