Skip to contentSkip to conversation

Normalize timespec for negative values in ns_to_timespec

PATCH In review Thomas Gleixner 3 participants 4 messages Jan 21, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Normalize timespec for negative values in ns_to_timespec

From: George Anzinger <[email protected]>

In case of a negative nsec value the result of the division must be normalized.
Remove inline from an exported function.

kernel/time.c +7 -6
Index: linux-2.6.15/kernel/time.c
===================================================================
@@ -658,15 +658,16 @@ 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)
-		ts.tv_sec = div_long_long_rem_signed(nsec, NSEC_PER_SEC,
-						     &ts.tv_nsec);
-	else
-		ts.tv_sec = ts.tv_nsec = 0;
+	if (!nsec)
+		return (struct timespec) {0, 0};
+
+	ts.tv_sec = div_long_long_rem_signed(nsec, NSEC_PER_SEC, &ts.tv_nsec);
+	if (unlikely(nsec < 0))
+		set_normalized_timespec(&ts, ts.tv_sec, ts.tv_nsec);
 
 	return ts;
 }

--

#1 lore raw
Quoted 3 lines · Thomas Gleixner

Cc: George Anzinger <[email protected]>
From: George Anzinger <[email protected]>

How many email addresses does the man have, and which is preferred?

Quoted 2 lines

In case of a negative nsec value the result of the division must be normalized.

What effect did this problem have? oopses? Userspace misbehaviour? ...

#2 lore raw
Quoted 6 lines · Andrew Morton
Quoted 3 lines · Thomas Gleixner

Cc: George Anzinger <[email protected]>
From: George Anzinger <[email protected]>

How many email addresses does the man have, and which is preferred?

Sorry, my bad. I copied the wrong one into the cc-list

<[email protected]> is the correct one.

Quoted 4 lines
Quoted 2 lines

In case of a negative nsec value the result of the division must be normalized.

What effect did this problem have? oopses? Userspace misbehaviour? ...

Both

tglx

#3 lore raw

PATCH 1/1 Normalize timespec for negative values in ns_to_timespec

Here is the patch again. This time with a header.

Signature · 2 lines
George Anzinger   [email protected]
HRT (High-res-timers):  http://sourceforge.net/projects/high-res-timers/

Source: George Anzinger<[email protected]>

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<[email protected]>

 kernel/time.c |   13 ++++++++-----
 1 files changed, 8 insertions(+), 5 deletions(-)
kernel/time.c +8 -5
Index: 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;
 }