Possible BUG in sys_nanosleep() ?

From: Tyson Sawyer
Date: Tue Mar 22 2005 - 15:16:37 EST


I have searched archives of linux-kernel and not found any reference to this behavior. This situation exists in both 2.4 and 2.6 kernels. I'm not quite prepared to call it a bug because I have not yet consulted with anyone closer to the code and that is the purpose of this message:


From kernel/timer.c - sys_nanosleep():
expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);


From include/linux/jiffies.h - timespec_to_jiffies():
/*
* The TICK_NSEC - 1 rounds up the value to the next resolution. Note
* that a remainder subtract here would not do the right thing as the
[...]


Observed problem:

Processes seem to sleep for at least 2 timer ticks even when asked to sleep for less than 1. Specifically, on a 2.4 kernel with HZ=100, a sleep for 5ms becomes a sleep for 20ms when 10ms is the expected behavior.

Source code inspection:

sys_nanosleep() rounds up the value returned by timespec_to_jiffies() by adding 1 unless the requested sleep time is zero.

timespec_to_jiffies() also rounds the returned number of jiffies, except in the case of an even number of jiffies being requested. Thus, nanoseconds returns zero jiffies, but 1 nanosecond returns 1 jiffy.

The effect of both functions rounding up is that it is possible to sleep for zero nanoseconds (no sleep), but otherwise 1 is added to the number of jiffies to sleep. Thus, what should be a sleep for one jiffie (wake up on next timer tick) becomes two jiffies (and wakes up on the 2nd timer tick).

Conclusion:

sys_nanosleep() should never add 1 to the value of expire as timespec_to_jiffies() already rounds up.

I post this as a question because I don't know that timespec_to_jiffies() isn't where the behavior should be changed or perhaps there is a good reason that I can't think of for the current behavior.

I am not subscribed to linux-kernel. Please CC me on all replies.

Thanks!
Ty

--
Tyson D Sawyer iRobot Corporation
Lead Systems Engineer Government & Industrial Robotics
tsawyer@xxxxxxxxxx Robots for the Real World
781-345-0200 ext 3329 http://www.irobot.com
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/