new time code problem
diff mbox series

Message ID 43C44A3C.6010803@mvista.com
State New, archived
Headers show
Series
  • new time code problem
Related show

Commit Message

George Anzinger Jan. 10, 2006, 11:58 p.m. UTC
The 64-bit conversion routine to convert 64-bit nsec time to a time spec. 
gives an unnormalized result if the value being converted is negative.  I 
think there are two ways to go about fixing this.  Most systems will give a 
negative remainder and so need to just normalize.  On the other hand, some 
systems will use div64 to do the division and, I think, it expects unsigned 
numbers.  The attached patch uses the conservative approach of expecting the 
div to be set up for unsigned numbers.

I came accross this when one of my tests set a time near 1 Jan 1970, i.e. it 
is a real problem.

Comments

Thomas Gleixner Jan. 11, 2006, 12:23 a.m. UTC | #1
On Tuesday 10 January 2006 23:58, George Anzinger wrote:
> The 64-bit conversion routine to convert 64-bit nsec time to a time spec.
> gives an unnormalized result if the value being converted is negative.  I
> think there are two ways to go about fixing this.  Most systems will give a
> negative remainder and so need to just normalize.  On the other hand, some
> systems will use div64 to do the division and, I think, it expects unsigned
> numbers.  The attached patch uses the conservative approach of expecting
> the div to be set up for unsigned numbers.
>
> I came accross this when one of my tests set a time near 1 Jan 1970, i.e.
> it is a real problem.

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

Err, you mean propably

	if(!nsec)
		return (struct timespec){0, 0};

	tglx

  
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
George Anzinger Jan. 11, 2006, 1:37 a.m. UTC | #2
Thomas Gleixner wrote:
> On Tuesday 10 January 2006 23:58, George Anzinger wrote:
> 
>>The 64-bit conversion routine to convert 64-bit nsec time to a time spec.
>>gives an unnormalized result if the value being converted is negative.  I
>>think there are two ways to go about fixing this.  Most systems will give a
>>negative remainder and so need to just normalize.  On the other hand, some
>>systems will use div64 to do the division and, I think, it expects unsigned
>>numbers.  The attached patch uses the conservative approach of expecting
>>the div to be set up for unsigned numbers.
>>
>>I came accross this when one of my tests set a time near 1 Jan 1970, i.e.
>>it is a real problem.
> 
> 
>> 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};
> 
> 
> Err, you mean propably
> 
> 	if(!nsec)
> 		return (struct timespec){0, 0};

Why yes I do.  I even found that and fixed it, but then failed to refresh the 
patch.  Thanks...

Patch
diff mbox series

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