public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Tyson Sawyer <tyson@irobot.com>
To: linux-kernel@vger.kernel.org
Cc: Tyson Sawyer <tyson@irobot.com>
Subject: Possible BUG in sys_nanosleep() ?
Date: Tue, 22 Mar 2005 15:10:04 -0500	[thread overview]
Message-ID: <42407B9C.1000406@irobot.com> (raw)

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@irobot.com                 Robots for the Real World
781-345-0200 ext 3329              http://www.irobot.com

                 reply	other threads:[~2005-03-22 20:12 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=42407B9C.1000406@irobot.com \
    --to=tyson@irobot.com \
    --cc=linux-kernel@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox