[PATCH] Normalize timespec for negative values in ns_to_timespec

From: George Anzinger
Date: Sat Jan 21 2006 - 12:46:31 EST


Here is the patch again. This time with a header.
--
George Anzinger george@xxxxxxxxxxxxxxxxxxx
HRT (High-res-timers): http://sourceforge.net/projects/high-res-timers/
Source: George Anzinger<george@xxxxxxxxxxxxxxxxxx>

Bug fix.

Description:

The timespec ns_to_timespec() inline in kernel/time.c mishandles
negative times in that it generates unnormalized timespecs.

There are several ways to handle this, the attached being the most
conservative.

Signed off: George Anzinger<george@xxxxxxxxxxxxxxxxxxx>

kernel/time.c | 13 ++++++++-----
1 files changed, 8 insertions(+), 5 deletions(-)

Index: linux-2.6.16-rc/kernel/time.c
===================================================================
--- linux-2.6.16-rc.orig/kernel/time.c
+++ linux-2.6.16-rc/kernel/time.c
@@ -702,16 +702,19 @@ void set_normalized_timespec(struct time
*
* Returns the timespec representation of the nsec parameter.
*/
-inline struct timespec ns_to_timespec(const nsec_t nsec)
+struct timespec ns_to_timespec(const nsec_t nsec)
{
struct timespec ts;

- if (nsec)
+ if (!nsec) return (struct timespec){0, 0};
+
+ if (nsec < 0) {
+ ts.tv_sec = div_long_long_rem_signed(-nsec, NSEC_PER_SEC,
+ &ts.tv_nsec);
+ set_normalized_timespec(&ts, -ts.tv_sec, -ts.tv_nsec);
+ } else
ts.tv_sec = div_long_long_rem_signed(nsec, NSEC_PER_SEC,
&ts.tv_nsec);
- else
- ts.tv_sec = ts.tv_nsec = 0;
-
return ts;
}