[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <87o700ppaj.fsf@intel.com>
Date: Tue, 21 Jan 2025 10:03:00 +0200
From: Jani Nikula <jani.nikula@...ux.intel.com>
To: Guenter Roeck <linux@...ck-us.net>
Cc: Rodrigo Vivi <rodrigo.vivi@...el.com>, Joonas Lahtinen
<joonas.lahtinen@...ux.intel.com>, Tvrtko Ursulin <tursulin@...ulin.net>,
David Airlie <airlied@...il.com>, Simona Vetter <simona@...ll.ch>,
intel-gfx@...ts.freedesktop.org, intel-xe@...ts.freedesktop.org,
dri-devel@...ts.freedesktop.org, linux-kernel@...r.kernel.org, Guenter
Roeck <linux@...ck-us.net>, Linus Torvalds
<torvalds@...ux-foundation.org>, David Laight
<david.laight.linux@...il.com>, Andy Shevchenko
<andriy.shevchenko@...ux.intel.com>
Subject: Re: [PATCH] drm/i915/backlight: Return immediately when scale()
finds invalid parameters
On Mon, 20 Jan 2025, Guenter Roeck <linux@...ck-us.net> wrote:
> The scale() functions detects invalid parameters, but continues
> its calculations anyway. This causes bad results if negative values
> are used for unsigned operations. Worst case, a division by 0 error
> will be seen if source_min == source_max.
>
> On top of that, after v6.13, the sequence of WARN_ON() followed by clamp()
> may result in a build error with gcc 13.x.
>
> drivers/gpu/drm/i915/display/intel_backlight.c: In function 'scale':
> include/linux/compiler_types.h:542:45: error:
> call to '__compiletime_assert_415' declared with attribute error:
> clamp() low limit source_min greater than high limit source_max
>
> This happens if the compiler decides to rearrange the code as follows.
>
> if (source_min > source_max) {
> WARN(..);
> /* Do the clamp() knowing that source_min > source_max */
> source_val = clamp(source_val, source_min, source_max);
> } else {
> /* Do the clamp knowing that source_min <= source_max */
> source_val = clamp(source_val, source_min, source_max);
> }
>
> Fix the problem by evaluating the return values from WARN_ON and returning
> immediately after a warning. While at it, fix divide by zero error seen
> if source_min == source_max.
Thanks for the effort in tracking this down.
> Analyzed-by: Linus Torvalds <torvalds@...ux-foundation.org>
> Suggested-by: Linus Torvalds <torvalds@...ux-foundation.org>
> Suggested-by: David Laight <david.laight.linux@...il.com>
> Cc: David Laight <david.laight.linux@...il.com>
> Cc: Jani Nikula <jani.nikula@...ux.intel.com>
> Cc: Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
> Signed-off-by: Guenter Roeck <linux@...ck-us.net>
> ---
> drivers/gpu/drm/i915/display/intel_backlight.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/gpu/drm/i915/display/intel_backlight.c b/drivers/gpu/drm/i915/display/intel_backlight.c
> index 3f81a726cc7d..ad49bd4a1c12 100644
> --- a/drivers/gpu/drm/i915/display/intel_backlight.c
> +++ b/drivers/gpu/drm/i915/display/intel_backlight.c
> @@ -40,8 +40,11 @@ static u32 scale(u32 source_val,
> {
> u64 target_val;
>
> - WARN_ON(source_min > source_max);
> - WARN_ON(target_min > target_max);
> + if (WARN_ON(target_min > target_max))
> + return target_min;
> +
> + if (WARN_ON(source_min > source_max) || source_min == source_max)
> + return target_min + (target_max - target_min) / 2;
There's really no need to be this fancy, though. Years down the line
someone's going to think that mean value calculation is something we
need to preserve, but we don't. I'd just return target_max everywhere.
And source_min == source_max could be a warn too.
BR,
Jani.
>
> /* defensive */
> source_val = clamp(source_val, source_min, source_max);
--
Jani Nikula, Intel
Powered by blists - more mailing lists