public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH]: fix 32bits integer overflow in loops_per_jiffy calculation
@ 2002-08-22  9:50 Yoann Vandoorselaere
  2002-08-22 10:21 ` Gabriel Paubert
  0 siblings, 1 reply; 13+ messages in thread
From: Yoann Vandoorselaere @ 2002-08-22  9:50 UTC (permalink / raw)
  To: cpufreq; +Cc: cpufreq, linux-kernel, benh@kernel.crashing.org

[-- Attachment #1: Type: text/plain, Size: 513 bytes --]

Hi,

The "low_part * mult" multiplication of the old function may overflow a
32bits integer...

This patch both fix the overflow issue (tested with frequencies up to
20Ghz), and make the result of the function lose less precision.

Please apply, 

-- 
Yoann Vandoorselaere, http://www.prelude-ids.org

"Programming is a race between programmers, who try and make more and 
 more idiot-proof software, and universe, which produces more and more 
 remarkable idiots. Until now, universe leads the race"  -- R. Cook

[-- Attachment #2: cpufreq-overflow.diff --]
[-- Type: text/plain, Size: 747 bytes --]

--- linux-benh/kernel/cpufreq.c	2002-08-21 17:27:52.000000000 +0200
+++ linux-yoann/kernel/cpufreq.c	2002-08-22 11:27:09.000000000 +0200
@@ -78,14 +78,16 @@ static unsigned int             cpufreq_
  */
 static unsigned long scale(unsigned long old, u_int div, u_int mult)
 {
-	unsigned long low_part, high_part;
-
-	high_part  = old / div;
-	low_part   = (old % div) / 100;
-	high_part *= mult;
-	low_part   = low_part * mult / div;
-
-	return high_part + low_part * 100;
+        unsigned long val, carry = 0;
+        
+        mult /= 100;
+        div  /= 100;
+        val = old / div * mult;
+
+        carry = old % div;
+        carry = carry * mult / div;
+                
+        return val + carry;
 }
 
 

^ permalink raw reply	[flat|nested] 13+ messages in thread
* Re: [PATCH]: fix 32bits integer overflow in loops_per_jiffy calculation
@ 2002-08-22 13:02 Benjamin Herrenschmidt
  2002-08-22 12:12 ` Gabriel Paubert
  0 siblings, 1 reply; 13+ messages in thread
From: Benjamin Herrenschmidt @ 2002-08-22 13:02 UTC (permalink / raw)
  To: Gabriel Paubert, Yoann Vandoorselaere; +Cc: cpufreq, cpufreq, linux-kernel

Hi Gabriel !

>if(abs(div)<100) div=0;
>
>> +        val = old / div * mult;
>
>Now happily divide by zero.
>
>> +
>> +        carry = old % div;
>
>Again.
>
>> +        carry = carry * mult / div;
>
>Again.
>
>> +                
>> +        return val + carry;
>>  }

None of the above can happen in the domain of application of this
function. It's used to scale up/down the loops_per_jiffy value when
scaling the CPU frequency. Anyway, the above isn't worse than the
original function. Ideally, we would want 64 bits arithmetics, but
we decided long ago not to bring the libcc support routines for that
in the kernel.
>
>And I can't see how it can be more precise, you divide the numerator and
>denominator of the fraction by 100 and then proceed forgetting 
>everything about the rest. Basically this looses about 7 bits of precision.

Which is mostly ok for what we need. I think Yoann didn't mean it's
more precise that what it replace, but rather more precise than his
original implementation that divided by 1000 ;) Anyway, it's not
significantly worse than what we had and won't overflow as easily
which is all we want for this routine now.

>Now altogether I believe that such a function pertains to a per arch 
>optimized routine.

Maybe... though in the context of cpufreq, it may not make that much
sense.

Ben.



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

end of thread, other threads:[~2002-08-22 17:57 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-08-22  9:50 [PATCH]: fix 32bits integer overflow in loops_per_jiffy calculation Yoann Vandoorselaere
2002-08-22 10:21 ` Gabriel Paubert
2002-08-22 13:00   ` Benjamin Herrenschmidt
  -- strict thread matches above, loose matches on Subject: below --
2002-08-22 13:02 Benjamin Herrenschmidt
2002-08-22 12:12 ` Gabriel Paubert
2002-08-22 14:31   ` Benjamin Herrenschmidt
2002-08-22 15:23     ` Gabriel Paubert
2002-08-22 15:59       ` Yoann Vandoorselaere
2002-08-22 16:51       ` Dominik Brodowski
2002-08-22 19:35         ` Benjamin Herrenschmidt
2002-08-22 17:46           ` Dominik Brodowski
2002-08-22 18:02             ` Yoann Vandoorselaere
2002-08-22 20:00             ` Benjamin Herrenschmidt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox