linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] regulator: core: fix error path for regulator_set_voltage_unlocked
@ 2019-03-18 15:32 Steve Twiss
  2019-03-18 16:03 ` Dmitry Osipenko
  0 siblings, 1 reply; 4+ messages in thread
From: Steve Twiss @ 2019-03-18 15:32 UTC (permalink / raw)
  To: Liam Girdwood, Mark Brown
  Cc: LKML, Maciej Purski, Dmitry Osipenko, Support Opensource

During several error paths in the function
regulator_set_voltage_unlocked() the value of 'ret' can take on negative
error values. However, in calls that go through the 'goto out' statement,
this return value is lost and return 0 is used instead, indicating a
'pass'.

There are several cases where this function should legitimately return a
fail instead of a pass: one such case includes constraints check during
voltage selection in the call to regulator_check_voltage(), which can
have -EINVAL for the case when an unsupported voltage is incorrectly
requested. In that case, -22 is expected as the return value, not 0.

Fixes: 9243a195be7a ("regulator: core: Change voltage setting path")
Signed-off-by: Steve Twiss <stwiss.opensource@diasemi.com>
---
 drivers/regulator/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index 68473d0..caf8743 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -3326,7 +3326,7 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator,
 		goto out2;
 
 out:
-	return 0;
+	return ret;
 out2:
 	voltage->min_uV = old_min_uV;
 	voltage->max_uV = old_max_uV;
-- 
1.9.3


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

* Re: [PATCH] regulator: core: fix error path for regulator_set_voltage_unlocked
  2019-03-18 15:32 [PATCH] regulator: core: fix error path for regulator_set_voltage_unlocked Steve Twiss
@ 2019-03-18 16:03 ` Dmitry Osipenko
  2019-03-18 16:10   ` Dmitry Osipenko
  2019-03-18 16:11   ` Steve Twiss
  0 siblings, 2 replies; 4+ messages in thread
From: Dmitry Osipenko @ 2019-03-18 16:03 UTC (permalink / raw)
  To: Steve Twiss, Liam Girdwood, Mark Brown
  Cc: LKML, Maciej Purski, Support Opensource

18.03.2019 18:32, Steve Twiss пишет:
> During several error paths in the function
> regulator_set_voltage_unlocked() the value of 'ret' can take on negative
> error values. However, in calls that go through the 'goto out' statement,
> this return value is lost and return 0 is used instead, indicating a
> 'pass'.
> 
> There are several cases where this function should legitimately return a
> fail instead of a pass: one such case includes constraints check during
> voltage selection in the call to regulator_check_voltage(), which can
> have -EINVAL for the case when an unsupported voltage is incorrectly
> requested. In that case, -22 is expected as the return value, not 0.
> 
> Fixes: 9243a195be7a ("regulator: core: Change voltage setting path")
> Signed-off-by: Steve Twiss <stwiss.opensource@diasemi.com>
> ---
>  drivers/regulator/core.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
> index 68473d0..caf8743 100644
> --- a/drivers/regulator/core.c
> +++ b/drivers/regulator/core.c
> @@ -3326,7 +3326,7 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator,
>  		goto out2;
>  
>  out:
> -	return 0;
> +	return ret;
>  out2:
>  	voltage->min_uV = old_min_uV;
>  	voltage->max_uV = old_max_uV;
> 

Looks like a good catch.

Probably will be a bit better to write this as:

        /* for not coupled regulators this will just set the voltage */
        ret = regulator_balance_voltage(rdev, state);
-       if (ret < 0)
-               goto out2;
-
+       if (ret < 0) {
+               voltage->min_uV = old_min_uV;
+               voltage->max_uV = old_max_uV;
+       }
 out:
-       return 0;
-out2:
-       voltage->min_uV = old_min_uV;
-       voltage->max_uV = old_max_uV;
-
        return ret;
 }

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

* Re: [PATCH] regulator: core: fix error path for regulator_set_voltage_unlocked
  2019-03-18 16:03 ` Dmitry Osipenko
@ 2019-03-18 16:10   ` Dmitry Osipenko
  2019-03-18 16:11   ` Steve Twiss
  1 sibling, 0 replies; 4+ messages in thread
From: Dmitry Osipenko @ 2019-03-18 16:10 UTC (permalink / raw)
  To: Steve Twiss, Liam Girdwood, Mark Brown
  Cc: LKML, Maciej Purski, Support Opensource

18.03.2019 19:03, Dmitry Osipenko пишет:
> 18.03.2019 18:32, Steve Twiss пишет:
>> During several error paths in the function
>> regulator_set_voltage_unlocked() the value of 'ret' can take on negative
>> error values. However, in calls that go through the 'goto out' statement,
>> this return value is lost and return 0 is used instead, indicating a
>> 'pass'.
>>
>> There are several cases where this function should legitimately return a
>> fail instead of a pass: one such case includes constraints check during
>> voltage selection in the call to regulator_check_voltage(), which can
>> have -EINVAL for the case when an unsupported voltage is incorrectly
>> requested. In that case, -22 is expected as the return value, not 0.
>>
>> Fixes: 9243a195be7a ("regulator: core: Change voltage setting path")
>> Signed-off-by: Steve Twiss <stwiss.opensource@diasemi.com>
>> ---
>>  drivers/regulator/core.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
>> index 68473d0..caf8743 100644
>> --- a/drivers/regulator/core.c
>> +++ b/drivers/regulator/core.c
>> @@ -3326,7 +3326,7 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator,
>>  		goto out2;
>>  
>>  out:
>> -	return 0;
>> +	return ret;
>>  out2:
>>  	voltage->min_uV = old_min_uV;
>>  	voltage->max_uV = old_max_uV;
>>
> 
> Looks like a good catch.
> 
> Probably will be a bit better to write this as:
> 
>         /* for not coupled regulators this will just set the voltage */
>         ret = regulator_balance_voltage(rdev, state);
> -       if (ret < 0)
> -               goto out2;
> -
> +       if (ret < 0) {
> +               voltage->min_uV = old_min_uV;
> +               voltage->max_uV = old_max_uV;
> +       }
>  out:
> -       return 0;
> -out2:
> -       voltage->min_uV = old_min_uV;
> -       voltage->max_uV = old_max_uV;
> -
>         return ret;
>  }
> 

Also, probably won't hurt to add a stable tag "Cc: stable <stable@vger.kernel.org>" to get the fix backported.

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

* RE: [PATCH] regulator: core: fix error path for regulator_set_voltage_unlocked
  2019-03-18 16:03 ` Dmitry Osipenko
  2019-03-18 16:10   ` Dmitry Osipenko
@ 2019-03-18 16:11   ` Steve Twiss
  1 sibling, 0 replies; 4+ messages in thread
From: Steve Twiss @ 2019-03-18 16:11 UTC (permalink / raw)
  To: Dmitry Osipenko, Liam Girdwood, Mark Brown, Adam Thomson
  Cc: LKML, Maciej Purski, Support Opensource

Hi Dmitry,

Thanks,

On 18 March 2019 16:03, Dmitry Osipenko wrote:

> Subject: Re: [PATCH] regulator: core: fix error path for
> regulator_set_voltage_unlocked
> 
> 18.03.2019 18:32, Steve Twiss пишет:
> > During several error paths in the function
> > regulator_set_voltage_unlocked() the value of 'ret' can take on negative
> > error values. However, in calls that go through the 'goto out' statement,
> > this return value is lost and return 0 is used instead, indicating a
> > 'pass'.
> >
> > There are several cases where this function should legitimately return a
> > fail instead of a pass: one such case includes constraints check during
> > voltage selection in the call to regulator_check_voltage(), which can
> > have -EINVAL for the case when an unsupported voltage is incorrectly
> > requested. In that case, -22 is expected as the return value, not 0.
> >
> > Fixes: 9243a195be7a ("regulator: core: Change voltage setting path")
> > Signed-off-by: Steve Twiss <stwiss.opensource@diasemi.com>
> > ---
> >  drivers/regulator/core.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
> > index 68473d0..caf8743 100644
> > --- a/drivers/regulator/core.c
> > +++ b/drivers/regulator/core.c
> > @@ -3326,7 +3326,7 @@ static int regulator_set_voltage_unlocked(struct
> regulator *regulator,
> >  		goto out2;
> >
> >  out:
> > -	return 0;
> > +	return ret;
> >  out2:
> >  	voltage->min_uV = old_min_uV;
> >  	voltage->max_uV = old_max_uV;
> >
> 
> Looks like a good catch.
> 
> Probably will be a bit better to write this as:
> 
>         /* for not coupled regulators this will just set the voltage */
>         ret = regulator_balance_voltage(rdev, state);
> -       if (ret < 0)
> -               goto out2;
> -
> +       if (ret < 0) {
> +               voltage->min_uV = old_min_uV;
> +               voltage->max_uV = old_max_uV;
> +       }
>  out:
> -       return 0;
> -out2:
> -       voltage->min_uV = old_min_uV;
> -       voltage->max_uV = old_max_uV;
> -
>         return ret;
>  }

I've just had a very similar conversation with Adam Thomson who sits near me and also 
said the two gotos make it look confusing.

Honestly -- I wasn't convinced because it looked obvious to me, but you are the second
person to say it .. 
CC: Adam Thomson

So, ok. Agreed. :)
I'll make the change and resend.

Regards,
Steve

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

end of thread, other threads:[~2019-03-18 16:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-18 15:32 [PATCH] regulator: core: fix error path for regulator_set_voltage_unlocked Steve Twiss
2019-03-18 16:03 ` Dmitry Osipenko
2019-03-18 16:10   ` Dmitry Osipenko
2019-03-18 16:11   ` Steve Twiss

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