mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Jiri Hladky" <hladky.jiri@googlemail.com>
To: linux-kernel@vger.kernel.org
Cc: mj@atrey.karlin.mff.cuni.cz
Subject: Bug in arch/i386/lib/delay.c file, delay_loop function
Date: Fri, 30 May 2008 17:15:46 +0200	[thread overview]
Message-ID: <900048340805300815u57af94b0i3971b22a9d154d1f@mail.gmail.com> (raw)

[-- 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 --]

             reply	other threads:[~2008-05-30 15:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-30 15:15 Jiri Hladky [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=900048340805300815u57af94b0i3971b22a9d154d1f@mail.gmail.com \
    --to=hladky.jiri@googlemail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mj@atrey.karlin.mff.cuni.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®