pwm_bl: Fix overflow condition

Message ID 20170828200033.40673-1-dbasehore@chromium.org
State New
Headers show
Series
  • pwm_bl: Fix overflow condition
Related show

Commit Message

dbasehore . Aug. 28, 2017, 8 p.m.
This fixes and overflow condition that happens with a high value of
brightness-levels-scale by using a 64-bit variable. The issue would
prevent a range of higher brightness levels from being set.

Signed-off-by: Derek Basehore <dbasehore@chromium.org>
---
 drivers/video/backlight/pwm_bl.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Comments

Thierry Reding Aug. 29, 2017, 2:05 p.m. | #1
On Mon, Aug 28, 2017 at 01:00:33PM -0700, Derek Basehore wrote:
> This fixes and overflow condition that happens with a high value of
> brightness-levels-scale by using a 64-bit variable. The issue would
> prevent a range of higher brightness levels from being set.
> 
> Signed-off-by: Derek Basehore <dbasehore@chromium.org>
> ---
>  drivers/video/backlight/pwm_bl.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c
> index 76311ec5e400..e7ffd2108acf 100644
> --- a/drivers/video/backlight/pwm_bl.c
> +++ b/drivers/video/backlight/pwm_bl.c
> @@ -88,14 +88,17 @@ static void pwm_backlight_power_off(struct pwm_bl_data *pb)
>  static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
>  {
>  	unsigned int lth = pb->lth_brightness;
> -	int duty_cycle;
> +	s64 duty_cycle;
>  
>  	if (pb->levels)
>  		duty_cycle = pb->levels[brightness];
>  	else
>  		duty_cycle = brightness;
>  
> -	return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
> +	duty_cycle *= pb->period - lth;
> +	do_div(duty_cycle, pb->scale);
> +
> +	return duty_cycle + lth;
>  }

I don't think your commit message accurately describes the change here.
The overflow that you're preventing might happen with a large value of
pb->period (or rather, in combination with a large value of duty_cycle)
but it's unrelated to pb->scale.

Also, the semantics of do_div() are that it takes an unsigned dividend,
so your duty_cycle should be a u64.

Thierry
dbasehore . Aug. 29, 2017, 6:45 p.m. | #2
On Tue, Aug 29, 2017 at 7:05 AM, Thierry Reding
<thierry.reding@gmail.com> wrote:
> On Mon, Aug 28, 2017 at 01:00:33PM -0700, Derek Basehore wrote:
>> This fixes and overflow condition that happens with a high value of
>> brightness-levels-scale by using a 64-bit variable. The issue would
>> prevent a range of higher brightness levels from being set.
>>
>> Signed-off-by: Derek Basehore <dbasehore@chromium.org>
>> ---
>>  drivers/video/backlight/pwm_bl.c | 7 +++++--
>>  1 file changed, 5 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c
>> index 76311ec5e400..e7ffd2108acf 100644
>> --- a/drivers/video/backlight/pwm_bl.c
>> +++ b/drivers/video/backlight/pwm_bl.c
>> @@ -88,14 +88,17 @@ static void pwm_backlight_power_off(struct pwm_bl_data *pb)
>>  static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
>>  {
>>       unsigned int lth = pb->lth_brightness;
>> -     int duty_cycle;
>> +     s64 duty_cycle;
>>
>>       if (pb->levels)
>>               duty_cycle = pb->levels[brightness];
>>       else
>>               duty_cycle = brightness;
>>
>> -     return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
>> +     duty_cycle *= pb->period - lth;
>> +     do_div(duty_cycle, pb->scale);
>> +
>> +     return duty_cycle + lth;
>>  }
>
> I don't think your commit message accurately describes the change here.
> The overflow that you're preventing might happen with a large value of
> pb->period (or rather, in combination with a large value of duty_cycle)
> but it's unrelated to pb->scale.

I'm referring to the of property brightness-levels-scale. If there
aren't levels defined in a DTS, duty_cycle can be up to this value.
I'll change the CM to describe what's happening based on the variable
names from the function instead.

>
> Also, the semantics of do_div() are that it takes an unsigned dividend,
> so your duty_cycle should be a u64.

I'll change it.

>
> Thierry
--
To unsubscribe from this list: send the line "unsubscribe linux-pwm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Thierry Reding Aug. 30, 2017, 2:13 p.m. | #3
On Tue, Aug 29, 2017 at 11:45:43AM -0700, dbasehore . wrote:
> On Tue, Aug 29, 2017 at 7:05 AM, Thierry Reding
> <thierry.reding@gmail.com> wrote:
> > On Mon, Aug 28, 2017 at 01:00:33PM -0700, Derek Basehore wrote:
> >> This fixes and overflow condition that happens with a high value of
> >> brightness-levels-scale by using a 64-bit variable. The issue would
> >> prevent a range of higher brightness levels from being set.
> >>
> >> Signed-off-by: Derek Basehore <dbasehore@chromium.org>
> >> ---
> >>  drivers/video/backlight/pwm_bl.c | 7 +++++--
> >>  1 file changed, 5 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c
> >> index 76311ec5e400..e7ffd2108acf 100644
> >> --- a/drivers/video/backlight/pwm_bl.c
> >> +++ b/drivers/video/backlight/pwm_bl.c
> >> @@ -88,14 +88,17 @@ static void pwm_backlight_power_off(struct pwm_bl_data *pb)
> >>  static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
> >>  {
> >>       unsigned int lth = pb->lth_brightness;
> >> -     int duty_cycle;
> >> +     s64 duty_cycle;
> >>
> >>       if (pb->levels)
> >>               duty_cycle = pb->levels[brightness];
> >>       else
> >>               duty_cycle = brightness;
> >>
> >> -     return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
> >> +     duty_cycle *= pb->period - lth;
> >> +     do_div(duty_cycle, pb->scale);
> >> +
> >> +     return duty_cycle + lth;
> >>  }
> >
> > I don't think your commit message accurately describes the change here.
> > The overflow that you're preventing might happen with a large value of
> > pb->period (or rather, in combination with a large value of duty_cycle)
> > but it's unrelated to pb->scale.
> 
> I'm referring to the of property brightness-levels-scale.

I don't see a brightness-levels-scale property in either the DT bindings
or the pwm-backlight driver. Do you have any pointers?

Thierry

Patch

diff --git a/drivers/video/backlight/pwm_bl.c b/drivers/video/backlight/pwm_bl.c
index 76311ec5e400..e7ffd2108acf 100644
--- a/drivers/video/backlight/pwm_bl.c
+++ b/drivers/video/backlight/pwm_bl.c
@@ -88,14 +88,17 @@  static void pwm_backlight_power_off(struct pwm_bl_data *pb)
 static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
 {
 	unsigned int lth = pb->lth_brightness;
-	int duty_cycle;
+	s64 duty_cycle;
 
 	if (pb->levels)
 		duty_cycle = pb->levels[brightness];
 	else
 		duty_cycle = brightness;
 
-	return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
+	duty_cycle *= pb->period - lth;
+	do_div(duty_cycle, pb->scale);
+
+	return duty_cycle + lth;
 }
 
 static int pwm_backlight_update_status(struct backlight_device *bl)