* something about jiffies wraparound overflow
@ 2005-12-27 4:46 jeff shia
2005-12-27 8:54 ` lk
2005-12-27 14:54 ` Ben Collins
0 siblings, 2 replies; 5+ messages in thread
From: jeff shia @ 2005-12-27 4:46 UTC (permalink / raw)
To: linux-kernel, robert love
Hello,
we use the following to solve the problem of jiffies wraparound.
#define time_after(a,b) \
(typecheck(unsigned long, a) && \
typecheck(unsigned long, b) && \
((long)(b) - (long)(a) < 0))
#define time_before(a,b) time_after(b,a)
#define time_after_eq(a,b) \
(typecheck(unsigned long, a) && \
typecheck(unsigned long, b) && \
((long)(a) - (long)(b) >= 0))
#define time_before_eq(a,b) time_after_eq(b,a)
But I cannot understand it has some differences comparing with the
following code.
/* code 2*/
unsigned long timeout = jiffies + HZ/2;
if(timeout < jiffies)
{
/* not timeout...*/
}
else
{
/* timeout processing...*/
}
questions:
1.why the macros of time_after can solve the jiffies wraparound problem?
2.Is there any other possibilities for the "code 2" to overflow
except the jiffies overflow?
Any help will be preferred .
Thank you,
Yours.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: something about jiffies wraparound overflow
2005-12-27 4:46 something about jiffies wraparound overflow jeff shia
@ 2005-12-27 8:54 ` lk
2005-12-27 9:05 ` Con Kolivas
2005-12-27 14:54 ` Ben Collins
1 sibling, 1 reply; 5+ messages in thread
From: lk @ 2005-12-27 8:54 UTC (permalink / raw)
To: jeff shia, linux-kernel, robert love
As you mentioned the code for comparison:
> /* code 2*/
> unsigned long timeout = jiffies + HZ/2;
the code has no problem with jiffies wrapping around
as long as the values are compared in a right way.
For a 32 bit platform the counter wraps around only once every 50 day
when the value of HZ 1000. so if your code is prepared to face this event
it will work fine.
> 2.Is there any other possibilities for the "code 2" to overflow
> except the jiffies overflow?
No.
The better option would be to use the inline macros:
> #define time_after(a,b) \
> (typecheck(unsigned long, a) && \
> typecheck(unsigned long, b) && \
> ((long)(b) - (long)(a) < 0))
> #define time_before(a,b) time_after(b,a)
>
> #define time_after_eq(a,b) \
> (typecheck(unsigned long, a) && \
> typecheck(unsigned long, b) && \
> ((long)(a) - (long)(b) >= 0))
> #define time_before_eq(a,b) time_after_eq(b,a)
>
> But I cannot understand it has some differences comparing with the
> following code.
> 1.why the macros of time_after can solve the jiffies
> wraparound problem?
As it is clear that time_after evaluates true,
when a, as a snapshot of jiffies, represents a time after b,
These inlines deals with the timer wrapping correctly, because
if the timer wrap changes in the future , you won't have to alter the driver
code.
Typechecks are performed at the compiled time, that variables are of the
same type.
the code works by first converting the values to unsigned long, subtracting
them and then comparing the result.
so it is the safe way and most encoraged..
If you need to know the difference
between two instances of jiffies in a safe way, you can use the same trick:
diff = (long)t2 - (long)t1;.
regards
lk.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: something about jiffies wraparound overflow
2005-12-27 8:54 ` lk
@ 2005-12-27 9:05 ` Con Kolivas
0 siblings, 0 replies; 5+ messages in thread
From: Con Kolivas @ 2005-12-27 9:05 UTC (permalink / raw)
To: lk; +Cc: jeff shia, linux-kernel, robert love
On Tuesday 27 December 2005 19:54, lk wrote:
> As you mentioned the code for comparison:
> > /* code 2*/
> > unsigned long timeout = jiffies + HZ/2;
>
> the code has no problem with jiffies wrapping around
> as long as the values are compared in a right way.
> For a 32 bit platform the counter wraps around only once every 50 day
> when the value of HZ 1000. so if your code is prepared to face this event
> it will work fine.
In 2.6 we actually roll over 5 minutes after starting (the jiffy counter is
set to about 5 minutes from roll over); that was put in on purpose to pick up
wraparound bugs easily.
Cheers,
Con
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: something about jiffies wraparound overflow
[not found] <5oeWK-5Od-11@gated-at.bofh.it>
@ 2005-12-27 10:06 ` Bodo Eggert
0 siblings, 0 replies; 5+ messages in thread
From: Bodo Eggert @ 2005-12-27 10:06 UTC (permalink / raw)
To: jeff shia, linux-kernel, robert love
jeff shia <tshxiayu@gmail.com> wrote:
> we use the following to solve the problem of jiffies wraparound.
> #define time_after(a,b) \
> (typecheck(unsigned long, a) && \
> typecheck(unsigned long, b) && \
> ((long)(b) - (long)(a) < 0))
> #define time_before(a,b) time_after(b,a)
[...]
> But I cannot understand it has some differences comparing with the
> following code.
>
> /* code 2*/
>
> unsigned long timeout = jiffies + HZ/2;
>
> if(timeout < jiffies)
> questions:
> 1.why the macros of time_after can solve the jiffies wraparound problem?
Because the overflows get compensated. It's a property of Galois Fields.
> 2.Is there any other possibilities for the "code 2" to overflow
> except the jiffies overflow?
timeout might overflow, too.
--
Ich danke GMX dafür, die Verwendung meiner Adressen mittels per SPF
verbreiteten Lügen zu sabotieren.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: something about jiffies wraparound overflow
2005-12-27 4:46 something about jiffies wraparound overflow jeff shia
2005-12-27 8:54 ` lk
@ 2005-12-27 14:54 ` Ben Collins
1 sibling, 0 replies; 5+ messages in thread
From: Ben Collins @ 2005-12-27 14:54 UTC (permalink / raw)
To: jeff shia; +Cc: linux-kernel
On Tue, 2005-12-27 at 12:46 +0800, jeff shia wrote:
> 1.why the macros of time_after can solve the jiffies wraparound problem?
Basically it assumes that the two values you are comparing will never be
more than MAX_ULONG/2 apart. Which for jiffies means 270 some odd days
on 32-bit platforms.
--
Ben Collins <ben.collins@ubuntu.com>
Developer
Ubuntu Linux
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2005-12-27 14:55 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-12-27 4:46 something about jiffies wraparound overflow jeff shia
2005-12-27 8:54 ` lk
2005-12-27 9:05 ` Con Kolivas
2005-12-27 14:54 ` Ben Collins
[not found] <5oeWK-5Od-11@gated-at.bofh.it>
2005-12-27 10:06 ` Bodo Eggert
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.