[RFC][PATCH 4/4] convert sys_nanosleep() to use set_timer_nsecs()

From: Nishanth Aravamudan
Date: Thu Jul 14 2005 - 15:53:53 EST


From: Nishanth Aravamudan <nacc@xxxxxxxxxx>

Description: Add timespec and timeval conversion functions for
nanoseconds. Convert sys_nanosleep() to use schedule_timeout_nsecs().

Signed-off-by: Nishanth Aravamudan <nacc@xxxxxxxxxx>

---

include/linux/time.h | 33 +++++++++++++++++++++++++++++++++
kernel/timer.c | 24 ++++++++++++------------
2 files changed, 45 insertions(+), 12 deletions(-)

diff -urpN 2.6.13-rc3-base/include/linux/time.h 2.6.13-rc3-dev/include/linux/time.h
--- 2.6.13-rc3-base/include/linux/time.h 2005-07-14 12:46:46.000000000 -0700
+++ 2.6.13-rc3-dev/include/linux/time.h 2005-07-14 12:48:25.000000000 -0700
@@ -2,6 +2,7 @@
#define _LINUX_TIME_H

#include <linux/types.h>
+#include <asm/div64.h>

#ifdef __KERNEL__
#include <linux/seqlock.h>
@@ -126,6 +127,38 @@ set_normalized_timespec (struct timespec
ts->tv_nsec = nsec;
}

+/* Inline helper functions */
+static inline struct timeval nsecs_to_timeval(u64 ns)
+{
+ struct timeval tv;
+ tv.tv_sec = div_long_long_rem(ns, NSEC_PER_SEC, &tv.tv_usec);
+ tv.tv_usec = (tv.tv_usec + NSEC_PER_USEC/2) / NSEC_PER_USEC;
+ return tv;
+}
+
+static inline struct timespec nsecs_to_timespec(u64 ns)
+{
+ struct timespec ts;
+ ts.tv_sec = div_long_long_rem(ns, NSEC_PER_SEC, &ts.tv_nsec);
+ return ts;
+}
+
+static inline u64 timespec_to_nsecs(struct timespec* ts)
+{
+ u64 ret;
+ ret = ((u64)ts->tv_sec) * NSEC_PER_SEC;
+ ret += (u64)ts->tv_nsec;
+ return ret;
+}
+
+static inline u64 timeval_to_nsecs(struct timeval* tv)
+{
+ u64 ret;
+ ret = ((u64)tv->tv_sec) * NSEC_PER_SEC;
+ ret += ((u64)tv->tv_usec) * NSEC_PER_USEC;
+ return ret;
+}
+
#endif /* __KERNEL__ */

#define NFDBITS __NFDBITS
diff -urpN 2.6.13-rc3-base/kernel/timer.c 2.6.13-rc3-dev/kernel/timer.c
--- 2.6.13-rc3-base/kernel/timer.c 2005-07-14 12:46:46.000000000 -0700
+++ 2.6.13-rc3-dev/kernel/timer.c 2005-07-14 12:48:25.000000000 -0700
@@ -1481,21 +1481,21 @@ asmlinkage long sys_gettid(void)

static long __sched nanosleep_restart(struct restart_block *restart)
{
- unsigned long expire = restart->arg0, now = jiffies;
+ u64 expire = restart->arg0, now = do_monotonic_clock();
struct timespec __user *rmtp = (struct timespec __user *) restart->arg1;
long ret;

/* Did it expire while we handled signals? */
- if (!time_after(expire, now))
+ if (now > expire)
return 0;

- current->state = TASK_INTERRUPTIBLE;
- expire = schedule_timeout(expire - now);
+ set_current_state(TASK_INTERRUPTIBLE);
+ expire = schedule_timeout_nsecs(expire - now);

ret = 0;
if (expire) {
struct timespec t;
- jiffies_to_timespec(expire, &t);
+ t = nsecs_to_timespec(expire);

ret = -ERESTART_RESTARTBLOCK;
if (rmtp && copy_to_user(rmtp, &t, sizeof(t)))
@@ -1508,7 +1508,7 @@ static long __sched nanosleep_restart(st
asmlinkage long sys_nanosleep(struct timespec __user *rqtp, struct timespec __user *rmtp)
{
struct timespec t;
- unsigned long expire;
+ u64 expire;
long ret;

if (copy_from_user(&t, rqtp, sizeof(t)))
@@ -1517,20 +1517,20 @@ asmlinkage long sys_nanosleep(struct tim
if ((t.tv_nsec >= 1000000000L) || (t.tv_nsec < 0) || (t.tv_sec < 0))
return -EINVAL;

- expire = timespec_to_jiffies(&t) + (t.tv_sec || t.tv_nsec);
- current->state = TASK_INTERRUPTIBLE;
- expire = schedule_timeout(expire);
+ expire = timespec_to_nsecs(&t);
+ set_current_state(TASK_INTERRUPTIBLE);
+ expire = schedule_timeout_nsecs(expire);

ret = 0;
if (expire) {
struct restart_block *restart;
- jiffies_to_timespec(expire, &t);
+ t = nsecs_to_timespec(expire);
if (rmtp && copy_to_user(rmtp, &t, sizeof(t)))
return -EFAULT;

restart = &current_thread_info()->restart_block;
restart->fn = nanosleep_restart;
- restart->arg0 = jiffies + expire;
+ restart->arg0 = do_monotonic_clock() + expire;
restart->arg1 = (unsigned long) rmtp;
ret = -ERESTART_RESTARTBLOCK;
}
@@ -1642,7 +1642,7 @@ static void __devinit init_timers_cpu(in
for (j = 0; j < TVR_SIZE; j++)
INIT_LIST_HEAD(base->tv1.vec + j);

- base->timer_jiffies = jiffies;
+ base->last_timer_time = 0UL;
}

#ifdef CONFIG_HOTPLUG_CPU
-
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/