mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Ray Lee" <madrabbit@gmail.com>
To: "Chuck Ebbert" <76306.1226@compuserve.com>
Cc: "Andreas Mohr" <andi@rhlx01.fht-esslingen.de>,
	"Andrew Morton" <akpm@osdl.org>,
	linux-kernel <linux-kernel@vger.kernel.org>,
	"Dominik Brodowski" <linux@brodo.de>,
	"John Stultz" <johnstul@us.ibm.com>,
	"Alan Cox" <alan@lxorguk.ukuu.org.uk>, "Andi Kleen" <ak@suse.de>
Subject: Re: [patch] fix delay_tsc (was Re: delay_tsc(): inefficient delay loop (2.6.16-mm1))
Date: Sun, 26 Mar 2006 16:42:22 -0800	[thread overview]
Message-ID: <2c0942db0603261642k1554be2al3f3b0fe49d2f5ff@mail.gmail.com> (raw)
In-Reply-To: <200603261647_MC3-1-BB98-CB09@compuserve.com>

On 3/26/06, Chuck Ebbert <76306.1226@compuserve.com> wrote:
> On Fri, 24 Mar 2006 09:22:51 -0800, Ray Lee wrote:
> > On 3/24/06, Andreas Mohr <andi@rhlx01.fht-esslingen.de> wrote:
> > > +       loops += bclock;
> > [...]
> > > -       } while ((now-bclock) < loops);
> > > +       } while (now < loops);
> >
> > Erm, aren't you introducing an overflow problem here?
> >
> > if loops is 2^32-1, bclock is 1, the old version would execute the
> > proper number of times, the new one will blow out in one tick.
>
> Yes, but the old version has a bug too.
[...]
> If (loops == 100000) and (bclock == 2^32-1) the loop will terminate
> immediately when the low part of the TSC overflows because (now-bclock)
> is a large number.

Er, no, it won't, because (now-bclock) won't be large.

I know thinking about math on a modulo number line such as u8/16/32 is
odd, but it's best if you just always think of "subtraction" to mean
"distance between." (Which is always true in any space or coordinate
system, even with wrap arounds.) This is the same trick used by
Andrew's ring buffers, where you let head and tail wrap around freely,
and only perform the modulo operation at dereferencing.

A simple test program will give you a better feel for what's going on
(I write a lot of these...):

#include <stdio.h>
int main() {
  unsigned int a,b,c;
  a=-1-1;
  b=1000;
  c=b-a;
  printf("%u - %u = %u\n", b, a, c);
}

ray@issola:~/work/test/overflow$ gcc -o test test.c
ray@issola:~/work/test/overflow$ ./test
1000 - 4294967294 = 1002

So, it wraps appropriately, as odd as that may seem at first blush.

Ray

      parent reply	other threads:[~2006-03-27  0:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-26 21:44 Chuck Ebbert
2006-03-26 23:18 ` Andi Kleen
2006-03-27  0:42 ` Ray Lee [this message]

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=2c0942db0603261642k1554be2al3f3b0fe49d2f5ff@mail.gmail.com \
    --to=madrabbit@gmail.com \
    --cc=76306.1226@compuserve.com \
    --cc=ak@suse.de \
    --cc=akpm@osdl.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=andi@rhlx01.fht-esslingen.de \
    --cc=johnstul@us.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@brodo.de \
    --cc=ray-gmail@madrabbit.org \
    /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®