dri-devel.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] drm/i915/tv: avoid possible division by zero
@ 2023-07-17  6:22 Su Hui
  2023-07-17 14:52 ` Andrzej Hajda
  0 siblings, 1 reply; 7+ messages in thread
From: Su Hui @ 2023-07-17  6:22 UTC (permalink / raw)
  To: jani.nikula, joonas.lahtinen, rodrigo.vivi, tvrtko.ursulin,
	airlied, daniel, nathan, ndesaulniers, trix
  Cc: Su Hui, intel-gfx, llvm, kernel-janitors, linux-kernel,
	dri-devel, mripard, andrzej.hajda, ankit.k.nautiyal

Clang warning: drivers/gpu/drm/i915/display/intel_tv.c:
line 991, column 22 Division by zero.
Assuming tv_mode->oversample=1 and (!tv_mode->progressive)=1,
then division by zero will happen.

Fixes: 1bba5543e4fe ("drm/i915: Fix TV encoder clock computation")
Signed-off-by: Su Hui <suhui@nfschina.com>
---
 drivers/gpu/drm/i915/display/intel_tv.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/i915/display/intel_tv.c b/drivers/gpu/drm/i915/display/intel_tv.c
index 36b479b46b60..82b54af51f23 100644
--- a/drivers/gpu/drm/i915/display/intel_tv.c
+++ b/drivers/gpu/drm/i915/display/intel_tv.c
@@ -988,7 +988,8 @@ intel_tv_mode_to_mode(struct drm_display_mode *mode,
 		      const struct tv_mode *tv_mode,
 		      int clock)
 {
-	mode->clock = clock / (tv_mode->oversample >> !tv_mode->progressive);
+	mode->clock = clock / (tv_mode->oversample != 1 ?
+			tv_mode->oversample >> !tv_mode->progressive : 1);
 
 	/*
 	 * tv_mode horizontal timings:
-- 
2.30.2


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

* Re: [PATCH] drm/i915/tv: avoid possible division by zero
  2023-07-17  6:22 [PATCH] drm/i915/tv: avoid possible division by zero Su Hui
@ 2023-07-17 14:52 ` Andrzej Hajda
  2023-07-18  1:13   ` Su Hui
  2023-07-18  5:39   ` Dan Carpenter
  0 siblings, 2 replies; 7+ messages in thread
From: Andrzej Hajda @ 2023-07-17 14:52 UTC (permalink / raw)
  To: Su Hui, jani.nikula, joonas.lahtinen, rodrigo.vivi,
	tvrtko.ursulin, airlied, daniel, nathan, ndesaulniers, trix
  Cc: intel-gfx, llvm, kernel-janitors, linux-kernel, dri-devel,
	mripard, ankit.k.nautiyal



On 17.07.2023 08:22, Su Hui wrote:
> Clang warning: drivers/gpu/drm/i915/display/intel_tv.c:
> line 991, column 22 Division by zero.
> Assuming tv_mode->oversample=1 and (!tv_mode->progressive)=1,
> then division by zero will happen.
>
> Fixes: 1bba5543e4fe ("drm/i915: Fix TV encoder clock computation")
> Signed-off-by: Su Hui <suhui@nfschina.com>
> ---
>   drivers/gpu/drm/i915/display/intel_tv.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/i915/display/intel_tv.c b/drivers/gpu/drm/i915/display/intel_tv.c
> index 36b479b46b60..82b54af51f23 100644
> --- a/drivers/gpu/drm/i915/display/intel_tv.c
> +++ b/drivers/gpu/drm/i915/display/intel_tv.c
> @@ -988,7 +988,8 @@ intel_tv_mode_to_mode(struct drm_display_mode *mode,
>   		      const struct tv_mode *tv_mode,
>   		      int clock)
>   {
> -	mode->clock = clock / (tv_mode->oversample >> !tv_mode->progressive);
> +	mode->clock = clock / (tv_mode->oversample != 1 ?
> +			tv_mode->oversample >> !tv_mode->progressive : 1);

Seems too smart to me, why not just:
mode->clock = clock / tv_mode->oversample;
if (!tv_mode->progressive)
     mode->clock <<= 1;
Or trying being smart:
mode->clock = clock / tv_mode->oversample << !tv_mode->progressive;

Btw in both cases there is assumption tv_mode->oversample != 0, I guess 
it is true.

Regards
Andrzej

>   
>   	/*
>   	 * tv_mode horizontal timings:


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

* Re: [PATCH] drm/i915/tv: avoid possible division by zero
  2023-07-17 14:52 ` Andrzej Hajda
@ 2023-07-18  1:13   ` Su Hui
  2023-07-18  5:39   ` Dan Carpenter
  1 sibling, 0 replies; 7+ messages in thread
From: Su Hui @ 2023-07-18  1:13 UTC (permalink / raw)
  To: Andrzej Hajda, jani.nikula, joonas.lahtinen, rodrigo.vivi,
	tvrtko.ursulin, airlied, daniel, nathan, ndesaulniers, trix
  Cc: intel-gfx, llvm, kernel-janitors, linux-kernel, dri-devel,
	mripard, ankit.k.nautiyal

On 2023/7/17 22:52, Andrzej Hajda wrote:
>
>
> On 17.07.2023 08:22, Su Hui wrote:
>> Clang warning: drivers/gpu/drm/i915/display/intel_tv.c:
>> line 991, column 22 Division by zero.
>> Assuming tv_mode->oversample=1 and (!tv_mode->progressive)=1,
>> then division by zero will happen.
>>
>> Fixes: 1bba5543e4fe ("drm/i915: Fix TV encoder clock computation")
>> Signed-off-by: Su Hui <suhui@nfschina.com>
>> ---
>>   drivers/gpu/drm/i915/display/intel_tv.c | 3 ++-
>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/gpu/drm/i915/display/intel_tv.c 
>> b/drivers/gpu/drm/i915/display/intel_tv.c
>> index 36b479b46b60..82b54af51f23 100644
>> --- a/drivers/gpu/drm/i915/display/intel_tv.c
>> +++ b/drivers/gpu/drm/i915/display/intel_tv.c
>> @@ -988,7 +988,8 @@ intel_tv_mode_to_mode(struct drm_display_mode *mode,
>>                 const struct tv_mode *tv_mode,
>>                 int clock)
>>   {
>> -    mode->clock = clock / (tv_mode->oversample >> 
>> !tv_mode->progressive);
>> +    mode->clock = clock / (tv_mode->oversample != 1 ?
>> +            tv_mode->oversample >> !tv_mode->progressive : 1);
>
> Seems too smart to me, why not just:
> mode->clock = clock / tv_mode->oversample;
> if (!tv_mode->progressive)
>     mode->clock <<= 1;
> Or trying being smart:
> mode->clock = clock / tv_mode->oversample << !tv_mode->progressive;

Hi,

Yes, this is more readable and clear.
Thanks four your advice. I will send v2 soon.

Su Hui

>
> Btw in both cases there is assumption tv_mode->oversample != 0, I 
> guess it is true.
>
> Regards
> Andrzej
>
>>         /*
>>        * tv_mode horizontal timings:
>

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

* Re: [PATCH] drm/i915/tv: avoid possible division by zero
  2023-07-17 14:52 ` Andrzej Hajda
  2023-07-18  1:13   ` Su Hui
@ 2023-07-18  5:39   ` Dan Carpenter
  2023-07-18 10:10     ` Su Hui
  1 sibling, 1 reply; 7+ messages in thread
From: Dan Carpenter @ 2023-07-18  5:39 UTC (permalink / raw)
  To: Andrzej Hajda
  Cc: dri-devel, tvrtko.ursulin, kernel-janitors, llvm, Su Hui, trix,
	intel-gfx, ndesaulniers, linux-kernel, nathan, mripard,
	rodrigo.vivi, ankit.k.nautiyal

On Mon, Jul 17, 2023 at 04:52:51PM +0200, Andrzej Hajda wrote:
> 
> 
> On 17.07.2023 08:22, Su Hui wrote:
> > Clang warning: drivers/gpu/drm/i915/display/intel_tv.c:
> > line 991, column 22 Division by zero.
> > Assuming tv_mode->oversample=1 and (!tv_mode->progressive)=1,
> > then division by zero will happen.
> > 
> > Fixes: 1bba5543e4fe ("drm/i915: Fix TV encoder clock computation")
> > Signed-off-by: Su Hui <suhui@nfschina.com>
> > ---
> >   drivers/gpu/drm/i915/display/intel_tv.c | 3 ++-
> >   1 file changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/gpu/drm/i915/display/intel_tv.c b/drivers/gpu/drm/i915/display/intel_tv.c
> > index 36b479b46b60..82b54af51f23 100644
> > --- a/drivers/gpu/drm/i915/display/intel_tv.c
> > +++ b/drivers/gpu/drm/i915/display/intel_tv.c
> > @@ -988,7 +988,8 @@ intel_tv_mode_to_mode(struct drm_display_mode *mode,
> >   		      const struct tv_mode *tv_mode,
> >   		      int clock)
> >   {
> > -	mode->clock = clock / (tv_mode->oversample >> !tv_mode->progressive);
> > +	mode->clock = clock / (tv_mode->oversample != 1 ?
> > +			tv_mode->oversample >> !tv_mode->progressive : 1);
> 
> Seems too smart to me, why not just:
> mode->clock = clock / tv_mode->oversample;
> if (!tv_mode->progressive)
>     mode->clock <<= 1;

This is nice.

regards,
dan carpenter

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

* Re: [PATCH] drm/i915/tv: avoid possible division by zero
  2023-07-18  5:39   ` Dan Carpenter
@ 2023-07-18 10:10     ` Su Hui
  2023-07-18 11:28       ` Andrzej Hajda
  0 siblings, 1 reply; 7+ messages in thread
From: Su Hui @ 2023-07-18 10:10 UTC (permalink / raw)
  To: Dan Carpenter, Andrzej Hajda
  Cc: tvrtko.ursulin, kernel-janitors, llvm, dri-devel, trix,
	intel-gfx, ndesaulniers, linux-kernel, nathan, mripard,
	rodrigo.vivi, ankit.k.nautiyal

On 2023/7/18 13:39, Dan Carpenter wrote:
> On Mon, Jul 17, 2023 at 04:52:51PM +0200, Andrzej Hajda wrote:
>>
>> On 17.07.2023 08:22, Su Hui wrote:
>>> Clang warning: drivers/gpu/drm/i915/display/intel_tv.c:
>>> line 991, column 22 Division by zero.
>>> Assuming tv_mode->oversample=1 and (!tv_mode->progressive)=1,
>>> then division by zero will happen.
>>>
>>> Fixes: 1bba5543e4fe ("drm/i915: Fix TV encoder clock computation")
>>> Signed-off-by: Su Hui <suhui@nfschina.com>
>>> ---
>>>    drivers/gpu/drm/i915/display/intel_tv.c | 3 ++-
>>>    1 file changed, 2 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/gpu/drm/i915/display/intel_tv.c b/drivers/gpu/drm/i915/display/intel_tv.c
>>> index 36b479b46b60..82b54af51f23 100644
>>> --- a/drivers/gpu/drm/i915/display/intel_tv.c
>>> +++ b/drivers/gpu/drm/i915/display/intel_tv.c
>>> @@ -988,7 +988,8 @@ intel_tv_mode_to_mode(struct drm_display_mode *mode,
>>>    		      const struct tv_mode *tv_mode,
>>>    		      int clock)
>>>    {
>>> -	mode->clock = clock / (tv_mode->oversample >> !tv_mode->progressive);
>>> +	mode->clock = clock / (tv_mode->oversample != 1 ?
>>> +			tv_mode->oversample >> !tv_mode->progressive : 1);
>> Seems too smart to me, why not just:
>> mode->clock = clock / tv_mode->oversample;
>> if (!tv_mode->progressive)
>>      mode->clock <<= 1;
> This is nice.

mode->clock = clock / tv_mode->oversample << !tv_mode->progressive;

But I think this one is much better,  it has less code and run faster.
Should I resend v3 to add some explanation or follow Dan's advice?

Su Hui

> regards,
> dan carpenter

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

* Re: [PATCH] drm/i915/tv: avoid possible division by zero
  2023-07-18 10:10     ` Su Hui
@ 2023-07-18 11:28       ` Andrzej Hajda
  2023-07-19  2:12         ` Su Hui
  0 siblings, 1 reply; 7+ messages in thread
From: Andrzej Hajda @ 2023-07-18 11:28 UTC (permalink / raw)
  To: Su Hui, Dan Carpenter
  Cc: tvrtko.ursulin, kernel-janitors, llvm, dri-devel, trix,
	intel-gfx, ndesaulniers, linux-kernel, nathan, mripard,
	rodrigo.vivi, ankit.k.nautiyal



On 18.07.2023 12:10, Su Hui wrote:
> On 2023/7/18 13:39, Dan Carpenter wrote:
>> On Mon, Jul 17, 2023 at 04:52:51PM +0200, Andrzej Hajda wrote:
>>>
>>> On 17.07.2023 08:22, Su Hui wrote:
>>>> Clang warning: drivers/gpu/drm/i915/display/intel_tv.c:
>>>> line 991, column 22 Division by zero.
>>>> Assuming tv_mode->oversample=1 and (!tv_mode->progressive)=1,
>>>> then division by zero will happen.
>>>>
>>>> Fixes: 1bba5543e4fe ("drm/i915: Fix TV encoder clock computation")
>>>> Signed-off-by: Su Hui <suhui@nfschina.com>
>>>> ---
>>>>    drivers/gpu/drm/i915/display/intel_tv.c | 3 ++-
>>>>    1 file changed, 2 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/drivers/gpu/drm/i915/display/intel_tv.c 
>>>> b/drivers/gpu/drm/i915/display/intel_tv.c
>>>> index 36b479b46b60..82b54af51f23 100644
>>>> --- a/drivers/gpu/drm/i915/display/intel_tv.c
>>>> +++ b/drivers/gpu/drm/i915/display/intel_tv.c
>>>> @@ -988,7 +988,8 @@ intel_tv_mode_to_mode(struct drm_display_mode 
>>>> *mode,
>>>>                  const struct tv_mode *tv_mode,
>>>>                  int clock)
>>>>    {
>>>> -    mode->clock = clock / (tv_mode->oversample >> 
>>>> !tv_mode->progressive);
>>>> +    mode->clock = clock / (tv_mode->oversample != 1 ?
>>>> +            tv_mode->oversample >> !tv_mode->progressive : 1);
>>> Seems too smart to me, why not just:
>>> mode->clock = clock / tv_mode->oversample;
>>> if (!tv_mode->progressive)
>>>      mode->clock <<= 1;
>> This is nice.
>
> mode->clock = clock / tv_mode->oversample << !tv_mode->progressive;
>
> But I think this one is much better,  it has less code and run faster.
> Should I resend v3 to add some explanation or follow Dan's advice?

Speed gain here is irrelevant here, and disputable.

One thing which could be problematic is that we could loose the least 
significant bit in mode->clock,
in case non-progressive, but I am not sure if it really matters, as 
mode->clock is not precise value anyway.
Alternatively we could 1st shift, then divide, but in this case overflow 
can occur, at least in theory - I suspect there are no such big clocks 
(in kHz).

Finally I would agree with Dan, readability is better with conditional.

Regards
Andrzej

>
> Su Hui
>
>> regards,
>> dan carpenter


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

* Re: [PATCH] drm/i915/tv: avoid possible division by zero
  2023-07-18 11:28       ` Andrzej Hajda
@ 2023-07-19  2:12         ` Su Hui
  0 siblings, 0 replies; 7+ messages in thread
From: Su Hui @ 2023-07-19  2:12 UTC (permalink / raw)
  To: Andrzej Hajda
  Cc: tvrtko.ursulin, kernel-janitors, llvm, dri-devel, trix,
	intel-gfx, ndesaulniers, linux-kernel, nathan, Dan Carpenter,
	mripard, rodrigo.vivi, ankit.k.nautiyal

On 2023/7/18 19:28, Andrzej Hajda wrote:
> On 18.07.2023 12:10, Su Hui wrote:
>> On 2023/7/18 13:39, Dan Carpenter wrote:
>>> On Mon, Jul 17, 2023 at 04:52:51PM +0200, Andrzej Hajda wrote:
>>>> On 17.07.2023 08:22, Su Hui wrote:
>>>>> Clang warning: drivers/gpu/drm/i915/display/intel_tv.c:
>>>>> line 991, column 22 Division by zero.
>>>>> Assuming tv_mode->oversample=1 and (!tv_mode->progressive)=1,
>>>>> then division by zero will happen.
>>>>>
>>>>> Fixes: 1bba5543e4fe ("drm/i915: Fix TV encoder clock computation")
>>>>> Signed-off-by: Su Hui <suhui@nfschina.com>
>>>>> ---
>>>>>    drivers/gpu/drm/i915/display/intel_tv.c | 3 ++-
>>>>>    1 file changed, 2 insertions(+), 1 deletion(-)
>>>>>
>>>>> diff --git a/drivers/gpu/drm/i915/display/intel_tv.c 
>>>>> b/drivers/gpu/drm/i915/display/intel_tv.c
>>>>> index 36b479b46b60..82b54af51f23 100644
>>>>> --- a/drivers/gpu/drm/i915/display/intel_tv.c
>>>>> +++ b/drivers/gpu/drm/i915/display/intel_tv.c
>>>>> @@ -988,7 +988,8 @@ intel_tv_mode_to_mode(struct drm_display_mode 
>>>>> *mode,
>>>>>                  const struct tv_mode *tv_mode,
>>>>>                  int clock)
>>>>>    {
>>>>> -    mode->clock = clock / (tv_mode->oversample >> 
>>>>> !tv_mode->progressive);
>>>>> +    mode->clock = clock / (tv_mode->oversample != 1 ?
>>>>> +            tv_mode->oversample >> !tv_mode->progressive : 1);
>>>> Seems too smart to me, why not just:
>>>> mode->clock = clock / tv_mode->oversample;
>>>> if (!tv_mode->progressive)
>>>>      mode->clock <<= 1;
>>> This is nice.
>>
>> mode->clock = clock / tv_mode->oversample << !tv_mode->progressive;
>>
>> But I think this one is much better,  it has less code and run faster.
>> Should I resend v3 to add some explanation or follow Dan's advice?
>
> Speed gain here is irrelevant here, and disputable.
>
> One thing which could be problematic is that we could loose the least 
> significant bit in mode->clock,
> in case non-progressive, but I am not sure if it really matters, as 
> mode->clock is not precise value anyway.
> Alternatively we could 1st shift, then divide, but in this case 
> overflow can occur, at least in theory - I suspect there are no such 
> big clocks (in kHz).
>
> Finally I would agree with Dan, readability is better with conditional.
>
How about this one?

-       mode->clock = clock / (tv_mode->oversample >> !tv_mode->progressive);
+       mode->clock = clock;
+       if (tv_mode->oversample >> !tv_mode->progressive)
+               mode->clock /= tv_mode->oversample >> 1;

Prevent loss of accuracy and also make it more readable.
If it's OK, I will send v3 patch.

By the way, do we need to print some error messages or do some things  when
"tv_mode->oversample << !tv_mode->progressive" is zero? I'm not sure about
this.

Su Hui

> Regards
> Andrzej

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

end of thread, other threads:[~2023-07-19  2:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-17  6:22 [PATCH] drm/i915/tv: avoid possible division by zero Su Hui
2023-07-17 14:52 ` Andrzej Hajda
2023-07-18  1:13   ` Su Hui
2023-07-18  5:39   ` Dan Carpenter
2023-07-18 10:10     ` Su Hui
2023-07-18 11:28       ` Andrzej Hajda
2023-07-19  2:12         ` Su Hui

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