mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* Bug in arch/i386/lib/delay.c file, delay_loop function
@ 2008-05-30 15:15 Jiri Hladky
  2008-05-31 16:39 ` Ingo Molnar
  2008-06-01 17:05 ` Martin Mares
  0 siblings, 2 replies; 5+ messages in thread
From: Jiri Hladky @ 2008-05-30 15:15 UTC (permalink / raw)
  To: linux-kernel; +Cc: mj

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

Hi all,

when trying to understand how Bogomips are implemented I have found
bug in arch/i386/lib/delay.c file, delay_loop function

/* 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));
}


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

__asm__ __volatile__(
	"\tjmp 1f\n"
	".align 16\n"
	"1:\tjmp 2f\n"
	".align 16\n"
	"2:\tdecl %0\n\tjnz 2b\n"
	"decl %0"
	:"=&a" (d0)
	:"0" (loops));

IMHO, d0 is not needed at all so that we can further simplify the code:
static void delay_loop(unsigned long loops)
__asm__ __volatile__(
	"\tjmp 1f\n"
	".align 16\n"
	"1:\tjmp 2f\n"
	".align 16\n"
	"2:\tdecl %0\n\tjnz 2b\n"
	"decl %0"
	:/*we don't need output */
	:"a" (loops));
}

I will attach three small C-program to test it
delay-orig.c - original loop from kernel source code
delay-fixed.c - fixed loop
delay-fixed1.c - fixed loop without d0 variable

Outputs:
============== delay-orig.c ==================
time delay-orig 2147483649
loops 2147483649
loops 2147483649
do -2147483648

real    0m0.002s
user    0m0.000s
sys     0m0.000s

================== delay-fixed.c =============
time delay-fixed 2147483649
loops 2147483649
loops 2147483649
do -1

real    0m1.025s
user    0m1.024s
sys     0m0.000s

========== delay-fixed1.c =====================
time delay-fixed1 2147483649
loops 2147483649
loops 2147483649

real    0m1.073s
user    0m1.060s
sys     0m0.004s


and update kernel source file arch/i386/lib/delay.c. Please let me
know if these modifications make sense.

Thanks a lot
Jiri

[-- Attachment #2: delay-fix.tar.bz2 --]
[-- Type: application/x-bzip2, Size: 3466 bytes --]

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

end of thread, other threads:[~2008-06-17  9:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-30 15:15 Bug in arch/i386/lib/delay.c file, delay_loop function Jiri Hladky
2008-05-31 16:39 ` Ingo Molnar
2008-06-01 17:05 ` Martin Mares
2008-06-02 10:00   ` Jiri Hladky
2008-06-17  9:03     ` Ingo Molnar

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

all inboxes | Powered by JetHome®