diff mbox series

net/ethernet: update ret when ptp_clock is ERROR

Message ID 1604649411-24886-1-git-send-email-wangqing@vivo.com
State Changes Requested
Delegated to: David Miller
Headers show
Series net/ethernet: update ret when ptp_clock is ERROR | expand

Checks

Context Check Description
jkicinski/cover_letter success Link
jkicinski/fixes_present success Link
jkicinski/patch_count success Link
jkicinski/tree_selection success Guessed tree name to be net-next
jkicinski/subject_prefix warning Target tree name not specified in the subject
jkicinski/source_inline success Was 0 now: 0
jkicinski/verify_signedoff success Link
jkicinski/module_param success Was 0 now: 0
jkicinski/build_32bit success Errors and warnings before: 0 this patch: 0
jkicinski/kdoc success Errors and warnings before: 2 this patch: 2
jkicinski/verify_fixes success Link
jkicinski/checkpatch success total: 0 errors, 0 warnings, 0 checks, 9 lines checked
jkicinski/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
jkicinski/header_inline success Link
jkicinski/stable success Stable not CCed

Commit Message

王擎 Nov. 6, 2020, 7:56 a.m. UTC
We always have to update the value of ret, otherwise the
 error value may be the previous one.

Signed-off-by: Wang Qing <wangqing@vivo.com>
---
 drivers/net/ethernet/ti/am65-cpts.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Grygorii Strashko Nov. 6, 2020, 11:34 a.m. UTC | #1
On 06/11/2020 09:56, Wang Qing wrote:
> We always have to update the value of ret, otherwise the
>   error value may be the previous one.
> 
> Signed-off-by: Wang Qing <wangqing@vivo.com>
> ---
>   drivers/net/ethernet/ti/am65-cpts.c | 3 +--
>   1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ethernet/ti/am65-cpts.c b/drivers/net/ethernet/ti/am65-cpts.c
> index 75056c1..b77ff61
> --- a/drivers/net/ethernet/ti/am65-cpts.c
> +++ b/drivers/net/ethernet/ti/am65-cpts.c
> @@ -1001,8 +1001,7 @@ struct am65_cpts *am65_cpts_create(struct device *dev, void __iomem *regs,

there is
	cpts->ptp_clock = ptp_clock_register(&cpts->ptp_info, cpts->dev);


>   	if (IS_ERR_OR_NULL(cpts->ptp_clock)) {

And ptp_clock_register() can return NULL only if PTP support is disabled.
In which case, we should not even get here.

So, I'd propose to s/IS_ERR_OR_NULL/IS_ERR above,
and just assign ret = PTR_ERR(cpts->ptp_clock) here.

>   		dev_err(dev, "Failed to register ptp clk %ld\n",
>   			PTR_ERR(cpts->ptp_clock));
> -		if (!cpts->ptp_clock)
> -			ret = -ENODEV;
> +		ret = cpts->ptp_clock ? cpts->ptp_clock : (-ENODEV);
>   		goto refclk_disable;
>   	}
>   	cpts->phc_index = ptp_clock_index(cpts->ptp_clock);
>
Arnd Bergmann Nov. 6, 2020, 12:11 p.m. UTC | #2
On Fri, Nov 6, 2020 at 12:35 PM Grygorii Strashko
<grygorii.strashko@ti.com> wrote:
> On 06/11/2020 09:56, Wang Qing wrote:

> > +++ b/drivers/net/ethernet/ti/am65-cpts.c
> > @@ -1001,8 +1001,7 @@ struct am65_cpts *am65_cpts_create(struct device *dev, void __iomem *regs,
>
> there is
>         cpts->ptp_clock = ptp_clock_register(&cpts->ptp_info, cpts->dev);
>
>
> >       if (IS_ERR_OR_NULL(cpts->ptp_clock)) {
>
> And ptp_clock_register() can return NULL only if PTP support is disabled.
> In which case, we should not even get here.
>
> So, I'd propose to s/IS_ERR_OR_NULL/IS_ERR above,
> and just assign ret = PTR_ERR(cpts->ptp_clock) here.

Right, using IS_ERR_OR_NULL() is almost ever a mistake, either
from misunderstanding the interface, or from a badly designed
interface that needs to be changed.

     Arnd
Kurt Kanzenbach Nov. 6, 2020, 12:58 p.m. UTC | #3
On Fri Nov 06 2020, Arnd Bergmann wrote:
> On Fri, Nov 6, 2020 at 12:35 PM Grygorii Strashko
> <grygorii.strashko@ti.com> wrote:
>> On 06/11/2020 09:56, Wang Qing wrote:
>
>> > +++ b/drivers/net/ethernet/ti/am65-cpts.c
>> > @@ -1001,8 +1001,7 @@ struct am65_cpts *am65_cpts_create(struct device *dev, void __iomem *regs,
>>
>> there is
>>         cpts->ptp_clock = ptp_clock_register(&cpts->ptp_info, cpts->dev);
>>
>>
>> >       if (IS_ERR_OR_NULL(cpts->ptp_clock)) {
>>
>> And ptp_clock_register() can return NULL only if PTP support is disabled.
>> In which case, we should not even get here.
>>
>> So, I'd propose to s/IS_ERR_OR_NULL/IS_ERR above,
>> and just assign ret = PTR_ERR(cpts->ptp_clock) here.
>
> Right, using IS_ERR_OR_NULL() is almost ever a mistake, either
> from misunderstanding the interface, or from a badly designed
> interface that needs to be changed.

The NULL case should be handled differently and it is documented:

/**
 * ptp_clock_register() - register a PTP hardware clock driver
[...]
 * Returns a valid pointer on success or PTR_ERR on failure.  If PHC
 * support is missing at the configuration level, this function
 * returns NULL, and drivers are expected to gracefully handle that
 * case separately.
 */

Thanks,
Kurt
Grygorii Strashko Nov. 6, 2020, 2:48 p.m. UTC | #4
On 06/11/2020 14:58, Kurt Kanzenbach wrote:
> On Fri Nov 06 2020, Arnd Bergmann wrote:
>> On Fri, Nov 6, 2020 at 12:35 PM Grygorii Strashko
>> <grygorii.strashko@ti.com> wrote:
>>> On 06/11/2020 09:56, Wang Qing wrote:
>>
>>>> +++ b/drivers/net/ethernet/ti/am65-cpts.c
>>>> @@ -1001,8 +1001,7 @@ struct am65_cpts *am65_cpts_create(struct device *dev, void __iomem *regs,
>>>
>>> there is
>>>          cpts->ptp_clock = ptp_clock_register(&cpts->ptp_info, cpts->dev);
>>>
>>>
>>>>        if (IS_ERR_OR_NULL(cpts->ptp_clock)) {
>>>
>>> And ptp_clock_register() can return NULL only if PTP support is disabled.
>>> In which case, we should not even get here.
>>>
>>> So, I'd propose to s/IS_ERR_OR_NULL/IS_ERR above,
>>> and just assign ret = PTR_ERR(cpts->ptp_clock) here.
>>
>> Right, using IS_ERR_OR_NULL() is almost ever a mistake, either
>> from misunderstanding the interface, or from a badly designed
>> interface that needs to be changed.
> 
> The NULL case should be handled differently and it is documented:
> 
> /**
>   * ptp_clock_register() - register a PTP hardware clock driver
> [...]
>   * Returns a valid pointer on success or PTR_ERR on failure.  If PHC
>   * support is missing at the configuration level, this function
>   * returns NULL, and drivers are expected to gracefully handle that
>   * case separately.
>   */

I think, it's not the first time such question triggered, I've found [1]

I've managed to find 6 drivers which uses IS_ERR_OR_NULL check with ptp_clock_register() and, kinda,
assume to be able to work with !CONFIG_PTP_1588_CLOCK. List below with some comments:

hv
hv_util.c
hv_timesync_init, line 697:  hv_ptp_clock = ptp_clock_register(&ptp_hyperv_info, NULL);

net/ethernet
sja1105 (depends on PTP_1588_CLOCK, use IS_ERR())
sja1105_ptp.c
sja1105_ptp_clock_register, line 867:  ptp_data->clock = ptp_clock_register(&ptp_data->caps, ds->dev);

net/ethernet
chelsio (can be fixed by adding !IS_ENABLED(CONFIG_PTP_1588_CLOCK))
cxgb4
cxgb4_ptp.c
cxgb4_ptp_init, line 431:  adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,

net/ethernet
octeontx2 (can be fixed by adding !IS_ENABLED(CONFIG_PTP_1588_CLOCK))
nic
otx2_ptp.c
otx2_ptp_init, line 170:  ptp_ptr->ptp_clock = ptp_clock_register(&ptp_ptr->ptp_info, pfvf->dev);

net/ethernet
renesas (no checks - will crash if init failed or !PTP), uses imply PTP_1588_CLOCK
ravb_ptp.c
ravb_ptp_init, line 345:  priv->ptp.clock = ptp_clock_register(&priv->ptp.info, &pdev->dev);

net/phy
mscc  (no checks - will crash if init failed or !PTP, depends on NETWORK_PHY_TIMESTAMPING)
mscc_ptp.c
__vsc8584_init_ptp, line 1495:  vsc8531->ptp->ptp_clock = ptp_clock_register(&vsc8531->ptp->caps,

In general, if above updated, the return value for ptp_clock_register() can be changed to ERR_PTR(-EOPNOTSUPP)
for the !CONFIG_PTP_1588_CLOCK and so question resolved.

For hv/sja1105/cxgb4/octeontx2 below diff should solve the case, but I'm not sure about
renesas/ravb_ptp and net/phy/mscc.

For renesas/ravb_ptp - seems strict "depends on PTP_1588_CLOCK" can be the choice
For net/phy/mscc - seems code dependencies need to be changed from CONFIG_NETWORK_PHY_TIMESTAMPING to
CONFIG_PTP_1588_CLOCK.

[1] https://lore.kernel.org/lkml/c04458ed-29ee-1797-3a11-7f3f560553e6@ti.com/
Richard Cochran Nov. 7, 2020, 3:07 p.m. UTC | #5
On Fri, Nov 06, 2020 at 01:34:04PM +0200, Grygorii Strashko wrote:
> And ptp_clock_register() can return NULL only if PTP support is disabled.

Not true in general ...

> In which case, we should not even get here.

only because the Kconfig uses "depends on" instead of "implies"
PTP_1588_CLOCK.

> So, I'd propose to s/IS_ERR_OR_NULL/IS_ERR above,
> and just assign ret = PTR_ERR(cpts->ptp_clock) here.

No, please no -- don't make another bad example for people to
copy/paste.

Thanks,
Richard
Richard Cochran Nov. 7, 2020, 3:08 p.m. UTC | #6
On Fri, Nov 06, 2020 at 03:56:45PM +0800, Wang Qing wrote:
> We always have to update the value of ret, otherwise the
>  error value may be the previous one.
> 
> Signed-off-by: Wang Qing <wangqing@vivo.com>

Acked-by: Richard Cochran <richardcochran@gmail.com>


> ---
>  drivers/net/ethernet/ti/am65-cpts.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ethernet/ti/am65-cpts.c b/drivers/net/ethernet/ti/am65-cpts.c
> index 75056c1..b77ff61
> --- a/drivers/net/ethernet/ti/am65-cpts.c
> +++ b/drivers/net/ethernet/ti/am65-cpts.c
> @@ -1001,8 +1001,7 @@ struct am65_cpts *am65_cpts_create(struct device *dev, void __iomem *regs,
>  	if (IS_ERR_OR_NULL(cpts->ptp_clock)) {
>  		dev_err(dev, "Failed to register ptp clk %ld\n",
>  			PTR_ERR(cpts->ptp_clock));
> -		if (!cpts->ptp_clock)
> -			ret = -ENODEV;
> +		ret = cpts->ptp_clock ? cpts->ptp_clock : (-ENODEV);
>  		goto refclk_disable;
>  	}
>  	cpts->phc_index = ptp_clock_index(cpts->ptp_clock);
> -- 
> 2.7.4
>
Grygorii Strashko Nov. 11, 2020, 1:03 p.m. UTC | #7
On 07/11/2020 17:08, Richard Cochran wrote:
> On Fri, Nov 06, 2020 at 03:56:45PM +0800, Wang Qing wrote:
>> We always have to update the value of ret, otherwise the
>>   error value may be the previous one.
>>
>> Signed-off-by: Wang Qing <wangqing@vivo.com>
> 
> Acked-by: Richard Cochran <richardcochran@gmail.com>
> 

Following Richard's comments:

Reviewed-by: Grygorii Strashko <grygorii.strashko@ti.com>

> 
>> ---
>>   drivers/net/ethernet/ti/am65-cpts.c | 3 +--
>>   1 file changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/drivers/net/ethernet/ti/am65-cpts.c b/drivers/net/ethernet/ti/am65-cpts.c
>> index 75056c1..b77ff61
>> --- a/drivers/net/ethernet/ti/am65-cpts.c
>> +++ b/drivers/net/ethernet/ti/am65-cpts.c
>> @@ -1001,8 +1001,7 @@ struct am65_cpts *am65_cpts_create(struct device *dev, void __iomem *regs,
>>   	if (IS_ERR_OR_NULL(cpts->ptp_clock)) {
>>   		dev_err(dev, "Failed to register ptp clk %ld\n",
>>   			PTR_ERR(cpts->ptp_clock));
>> -		if (!cpts->ptp_clock)
>> -			ret = -ENODEV;
>> +		ret = cpts->ptp_clock ? cpts->ptp_clock : (-ENODEV);
>>   		goto refclk_disable;
>>   	}
>>   	cpts->phc_index = ptp_clock_index(cpts->ptp_clock);
>> -- 
>> 2.7.4
>>
diff mbox series

Patch

diff --git a/drivers/net/ethernet/ti/am65-cpts.c b/drivers/net/ethernet/ti/am65-cpts.c
index 75056c1..b77ff61
--- a/drivers/net/ethernet/ti/am65-cpts.c
+++ b/drivers/net/ethernet/ti/am65-cpts.c
@@ -1001,8 +1001,7 @@  struct am65_cpts *am65_cpts_create(struct device *dev, void __iomem *regs,
 	if (IS_ERR_OR_NULL(cpts->ptp_clock)) {
 		dev_err(dev, "Failed to register ptp clk %ld\n",
 			PTR_ERR(cpts->ptp_clock));
-		if (!cpts->ptp_clock)
-			ret = -ENODEV;
+		ret = cpts->ptp_clock ? cpts->ptp_clock : (-ENODEV);
 		goto refclk_disable;
 	}
 	cpts->phc_index = ptp_clock_index(cpts->ptp_clock);