2006-01-21 10:01:37

by Thomas Gleixner

[permalink] [raw]
Subject: [PATCH] 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.

Signed-off-by: George Anzinger <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>


Index: linux-2.6.15/kernel/time.c
===================================================================
--- linux-2.6.15.orig/kernel/time.c
+++ 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;
}

--


2006-01-21 10:19:36

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] Normalize timespec for negative values in ns_to_timespec

Thomas Gleixner <[email protected]> wrote:
>
> Cc: George Anzinger <[email protected]>
> From: George Anzinger <[email protected]>

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

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

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

2006-01-21 10:33:19

by Thomas Gleixner

[permalink] [raw]
Subject: Re: [PATCH] Normalize timespec for negative values in ns_to_timespec

On Sat, 2006-01-21 at 02:19 -0800, Andrew Morton wrote:
> Thomas Gleixner <[email protected]> wrote:
> >
> > 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.

> > 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


2006-01-21 17:27:35

by George Anzinger

[permalink] [raw]
Subject: [PATCH] Normalize timespec for negative values in ns_to_timespec

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(-)

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;
}


Attachments:
ktime_conversion.patch (1.22 kB)