]> bbs.cooldavid.org Git - net-next-2.6.git/commitdiff
x86: fix bug in arch/i386/lib/delay.c file, delay_loop function
authorJiri Hladky <hladky.jiri@googlemail.com>
Mon, 2 Jun 2008 10:00:19 +0000 (12:00 +0200)
committerIngo Molnar <mingo@elte.hu>
Tue, 17 Jun 2008 08:55:47 +0000 (10:55 +0200)
when trying to understand how Bogomips are implemented I have found a
bug in arch/i386/lib/delay.c file, delay_loop function.

The function fails for loops > 2^31+1. It because SF is set when dec
returns numbers > 2^31.

The fix is to use jnz instruction instead of jns (and add one decl
instruction to the end to have exactly the same number of loops as in
original version).

Martin Mares observed:

> It is a long time since I have hacked that file, but you should definitely
> make sure that the function is never called with a zero argument. In such
> case, the original version made just a single pass, but your version
> makes 2^32 of them.

fixed that.

Signed-off-by: Ingo Molnar <mingo@elte.hu>
arch/x86/lib/delay_32.c

index d710f2d167bb49505e56ed4c0e9c5cc91a52546e..ef691316f8b6e6ed7a9c0467889d3f6278ba8634 100644 (file)
@@ -3,6 +3,7 @@
  *
  *     Copyright (C) 1993 Linus Torvalds
  *     Copyright (C) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
+ *     Copyright (C) 2008 Jiri Hladky <hladky _dot_ jiri _at_ gmail _dot_ com>
  *
  *     The __delay function must _NOT_ be inlined as its execution time
  *     depends wildly on alignment on many x86 processors. The additional
 /* simple loop based delay: */
 static void delay_loop(unsigned long loops)
 {
-       int d0;
-
        __asm__ __volatile__(
-               "\tjmp 1f\n"
-               ".align 16\n"
-               "1:\tjmp 2f\n"
-               ".align 16\n"
-               "2:\tdecl %0\n\tjns 2b"
-               :"=&a" (d0)
-               :"0" (loops));
+               "       test %0,%0      \n"
+               "       jz 3f           \n"
+               "       jmp 1f          \n"
+
+               ".align 16              \n"
+               "1:     jmp 2f          \n"
+
+               ".align 16              \n"
+               "2:     decl %0         \n"
+               "       jnz 2b          \n"
+               "3:     decl %0         \n"
+
+               : /* we don't need output */
+               :"a" (loops)
+       );
 }
 
 /* TSC based delay: */