All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andreas Dilger <adilger@turbolabs.com>
To: Krishna Kumar <kumarkr@us.ibm.com>
Cc: Linus Torvalds <torvalds@transmeta.com>,
	ak@muc.de, andrewm@uow.edu.au,
	"David S. Miller" <davem@redhat.com>,
	jgarzik@mandrakesoft.com, kuznet@ms2.inr.ac.ru,
	linux-kernel@vger.kernel.org, netdev@oss.sgi.com,
	owner-netdev@oss.sgi.com, tim@physik3.uni-rostock.de
Subject: Re: [PATCH] net/ipv4/*, net/core/neighbour.c jiffies cleanup
Date: Thu, 8 Nov 2001 11:01:09 -0700	[thread overview]
Message-ID: <20011108110108.W5922@lynx.no> (raw)
In-Reply-To: <OFE014018A.D6D3430D-ON88256AFE.005C057D@boulder.ibm.com>
In-Reply-To: <OFE014018A.D6D3430D-ON88256AFE.005C057D@boulder.ibm.com>; from kumarkr@us.ibm.com on Thu, Nov 08, 2001 at 08:55:51AM -0800

On Nov 08, 2001  08:55 -0800, Krishna Kumar wrote:
> > > In short: It is wrong to do
> > >
> > >          if (jiffies <= start+HZ)
> > >
> > > and it is _right_ to do
> > >
> > >          if (jiffies - start <= HZ)
> >
> > Actually this last part is wrong, isn't it ? jiffies <= start + HZ is
> > also a correct way to do it, since start+HZ will overflow to the current
> > value of jiffies when HZ time elapses. So the above two statements are
> > IDENTICAL.
> 
> I am sorry, but I still don't see the difference. I wrote a small program
> with different cases, but the values still come same irrespective of the
> input arguments to the checks. Could you tell under what conditions the
> checks wuold fail ? The 2's complement  works the same for addition and
> subtraction. I have included the test program below.

There are several cases where jiffies comparisons can be incorrect:

	unsigned long start = jiffies;		/* say 0xfffffff8 */
	unsigned long delta = 16;
	unsigned long end = jiffies + delta;	/* wraps to 0x00000008 */

a)	while (jiffies < end) { do something }

If jiffies is near wrap when calculating end, end will wrap and your loop
will never be executed, because 0xfffffff9 is greater than 0x00000008.

b)	while (jiffies - end < 0) { do something }

Fails because both jiffies and end are unsigned, and the difference can
never be negative.

c)	while ((long)jiffies - (long)end < 0) { do something }

Correct.  Hmm, this is just like

	while(time_before(jiffies, end)) { do something }

d) 	while (jiffies - start < delta) { do something }

This will also work because of modulo arithmetic, as long as we know in
advance that "start" is always less than "jiffies".


e)	if (jiffies > end) { fail because of timeout }

Incorrect for the same reason as (a) above, 0xfffffff9 > 0x00000008, where
we really want to wait until 0x00000009 to timeout.

f)	if (jiffies - end > 0) { fail because of timeout }

Fails for the same reason as (b) - both jiffies and end are unsigned, and
the difference can never be negative.

g)	if ((long)jiffies - (long)end > 0) { fail because of timeout }

Correct, which is just like:

	if (time_after(jiffies, end)) { fail because of timeout }


h)	if (jiffies - start > delta) { fail because of timeout }

Correct because of modulo arithmetic, as long as we know in advance that
"start" is less than "jiffies".


So it appears there are lots of ways to get it wrong that appear to be
correct.  This is why I'm pushing for the use of the macros, so that
there is no question about whether the comparison is right or wrong.

This is just the same as Linus' pedantic min() and max() macros - sure
people can get it right by themselves, but sometimes they get it wrong,
and it is easier to just make sure they don't get it wrong.

Cheers, Andreas
--
Andreas Dilger
http://sourceforge.net/projects/ext2resize/
http://www-mddsp.enel.ucalgary.ca/People/adilger/


  parent reply	other threads:[~2001-11-08 18:05 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-08 16:55 [PATCH] net/ipv4/*, net/core/neighbour.c jiffies cleanup Krishna Kumar
2001-11-08 17:10 ` Linus Torvalds
2001-11-08 18:01 ` Andreas Dilger [this message]
2001-11-09 10:43 ` Vino Thomas
  -- strict thread matches above, loose matches on Subject: below --
2001-11-08 17:47 Krishna Kumar
2001-11-08  3:07 Krishna Kumar
2001-11-08  5:26 ` Linus Torvalds
2001-11-08  0:00 Tim Schmielau
2001-11-08  0:09 ` David S. Miller
2001-11-08  0:36   ` Andreas Dilger
2001-11-08  0:44     ` David S. Miller
2001-11-08  0:58       ` Tim Schmielau
2001-11-08  1:09         ` David S. Miller
2001-11-08  1:20           ` Tim Schmielau
2001-11-08  1:36             ` David S. Miller
2001-11-08  4:32       ` Andreas Dilger
2001-11-08  4:39         ` David S. Miller
2001-11-08  1:22     ` Linus Torvalds
2001-11-08 17:54   ` kuznet
2001-11-08 18:10     ` Tim Schmielau
2001-11-08 18:10     ` Andreas Dilger
2001-11-08 18:32       ` kuznet

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=20011108110108.W5922@lynx.no \
    --to=adilger@turbolabs.com \
    --cc=ak@muc.de \
    --cc=andrewm@uow.edu.au \
    --cc=davem@redhat.com \
    --cc=jgarzik@mandrakesoft.com \
    --cc=kumarkr@us.ibm.com \
    --cc=kuznet@ms2.inr.ac.ru \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@oss.sgi.com \
    --cc=owner-netdev@oss.sgi.com \
    --cc=tim@physik3.uni-rostock.de \
    --cc=torvalds@transmeta.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.