Is linux time_after () macro 100% flawless with respect to wrapping around jiffies?

Based on the code I found in linux / include / linux / jiffies.h :

41 #define time_after(a,b)         \
42         (typecheck(unsigned long, a) && \
43          typecheck(unsigned long, b) && \
44          ((long)(b) - (long)(a) < 0))

It seems to me that there is no wrapping around monitoring. So, if jiffies (a) were to turn around and come back close enough to timeout (b), then the result would be “false” if it is truly “true”.

In this example, you can use several small numbers. Let's say time_after(110,150)where 110 is the jiffies and 150 is the timeout. The result will be clearly erroneous: whether everyone is wrapped or not: 150-110 always> 0.

So, I just wanted to confirm that I did not miss anything, and it really is.

+3
source share
2 answers

, , , 110 150, time_after(110,150) ( ) false. :

time_after(a,b) returns true if the time a is after time b.

, , 0. , , , .. 8- 2 . .

, b - 253, jiffies 2. , time_after(2,253) true. ( int8_t 8- ):

(int8_t) 253 - (int8_t) 2 == -3 - 2 == -5 < 0

. , time_after(128, 127), :

(int8_t) 127 - (int8_t) 128 == 127 - (-128) == 255 == -1 (for 8-bit 2 complement) < 0

(int8_t) 127 - (int8_t) 128 int, 255. longs , time_after( 2147483648, 2147483647):

(long) 2147483647 - (long) 2147483648 == 2147483647 - (-2147483648) == 4294967295 == -1 < 0

, "" jiffies a b, time_after(a,b) false. N- 2 , a 2 ^ (N-1) b. N = 8 , a 128 b. N = 32, 2147483648 ( 1 ) 25 .

, time_after(a,b) true, ( 2 ^ N) (a-b) > 0 < 2 ^ (N-1).

+4

:

/*
 * Have the 32 bit jiffies value wrap 5 minutes after boot
 * so jiffies wrap bugs show up earlier.
 */
#define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))

One would hope that this means that it is pretty well tested.

+1
source

All Articles