Skip to contentSkip to conversation

Wrong use of MAX_JIFFY_OFFSET?

DISCUSSION Liu Yu 2 participants 2 messages Mar 27, 2012

Subsystems (2)
lore
Share & tools
lore

Liu Yu

author
#0 lore raw

Hi guys,

I saw a couple of places in current kernel have this kind of code:

Quoted 9 lines

static inline unsigned int elapsed_jiffies_msecs(unsigned long start)

{
        unsigned long end = jiffies;

        if (end >= start)
               return jiffies_to_msecs(end - start);

        return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
}

As you know, jiffies has a type of unsigned long, so if we know which is the end and
which is the start, then (end - start) can simply figure out how much jiffies flies,
without worry about the overflow.

Look at the code above, assume that there is just an overflow happening on
jiffies: end=0 and start=~0UL.
Since end < start, then the return value of the function is jiffies_to_msecs(MAX_JIFFY_OFFSET+2).
But shouldn't the correct value be jiffies_to_msecs(1)?

could someone tell me that am I missing anything?

Thanks,
Yu

#1 lore raw
Quoted 30 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Liu Yu
Sent: 27 March 2012 02:25
To: [email protected]
Subject: Wrong use of MAX_JIFFY_OFFSET?

Hi guys,

I saw a couple of places in current kernel have this kind of code:

Quoted 1 line

static inline unsigned int elapsed_jiffies_msecs(unsigned

long start)

Quoted 7 lines

{
unsigned long end = jiffies;

if (end >= start)
return jiffies_to_msecs(end - start);

return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET -

start) + 1);

Quoted 1 line

}

As you know, jiffies has a type of unsigned long, so if we know which is the end and
which is the start, then (end - start) can simply figure out how much jiffies flies, without worry about the overflow.

Quoted 8 lines

Look at the code above, assume that there is just an overflow happening on
jiffies: end=0 and start=~0UL.
Since end < start, then the return value of the function is jiffies_to_msecs(MAX_JIFFY_OFFSET+2).
But shouldn't the correct value be jiffies_to_msecs(1)?

could someone tell me that am I missing anything?

That code has confused me...
MAX_JIFFIES_OFFSET appears to have something to do with timespec_to_jiffies() rather than being anything to so with the way 'jiffies' itself wraps.

Since arithmetic wrap is well defined for unsigned values the correct code should just be:

	return jiffies_to_ms(jiffies - start);

The code above is completly fubar when jiffies wraps.

David