mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* ktime_set() does not check for nanoseconds > one second
@ 2008-09-01 10:38 Matt Fleming
  2008-09-01 11:20 ` Thomas Gleixner
  0 siblings, 1 reply; 7+ messages in thread
From: Matt Fleming @ 2008-09-01 10:38 UTC (permalink / raw)
  To: linux-kernel

Hi,

is it intentional that ktime_set() does not check whether the
nanoseconds argument is greater than the number of nanoseconds in a
second? I've run into a problem where a value of 1600000000
nanoseconds was passed as an argument to ktime_set() and the return
value was then used in a ktime_add() call, which returned an incorrect
result. Should the caller of ktime_set() make this check or is it
possible to move this logic in to the function itself?

Matt

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: ktime_set() does not check for nanoseconds > one second
  2008-09-01 10:38 ktime_set() does not check for nanoseconds > one second Matt Fleming
@ 2008-09-01 11:20 ` Thomas Gleixner
  2008-09-01 11:57   ` Matt Fleming
  0 siblings, 1 reply; 7+ messages in thread
From: Thomas Gleixner @ 2008-09-01 11:20 UTC (permalink / raw)
  To: Matt Fleming; +Cc: linux-kernel

On Mon, 1 Sep 2008, Matt Fleming wrote:
> 
> is it intentional that ktime_set() does not check whether the
> nanoseconds argument is greater than the number of nanoseconds in a
> second? I've run into a problem where a value of 1600000000
> nanoseconds was passed as an argument to ktime_set() and the return
> value was then used in a ktime_add() call, which returned an incorrect
> result. Should the caller of ktime_set() make this check or is it
> possible to move this logic in to the function itself?

Yeah, a check for this in ktime_set() might make sense. Currently it's
up to the programmer to provide sane values. :)

Thanks,

	tglx

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: ktime_set() does not check for nanoseconds > one second
  2008-09-01 11:20 ` Thomas Gleixner
@ 2008-09-01 11:57   ` Matt Fleming
  2008-09-01 13:06     ` Thomas Gleixner
  0 siblings, 1 reply; 7+ messages in thread
From: Matt Fleming @ 2008-09-01 11:57 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1641 bytes --]

2008/9/1 Thomas Gleixner <tglx@linutronix.de>:
> On Mon, 1 Sep 2008, Matt Fleming wrote:
>>
>> is it intentional that ktime_set() does not check whether the
>> nanoseconds argument is greater than the number of nanoseconds in a
>> second? I've run into a problem where a value of 1600000000
>> nanoseconds was passed as an argument to ktime_set() and the return
>> value was then used in a ktime_add() call, which returned an incorrect
>> result. Should the caller of ktime_set() make this check or is it
>> possible to move this logic in to the function itself?
>
> Yeah, a check for this in ktime_set() might make sense. Currently it's
> up to the programmer to provide sane values. :)
>
> Thanks,
>
>        tglx
>

How about the attached patch?

Matt

diff --git a/include/linux/ktime.h b/include/linux/ktime.h
index ce59832..7c0ad35 100644
--- a/include/linux/ktime.h
+++ b/include/linux/ktime.h
@@ -150,7 +150,19 @@ static inline ktime_t timeval_to_ktime(struct timeval tv)
 /* Set a ktime_t variable to a value in sec/nsec representation: */
 static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
 {
-       return (ktime_t) { .tv = { .sec = secs, .nsec = nsecs } };
+       ktime_t res = { .tv = { .sec = secs, .nsec = nsecs }};;
+
+       /*
+        * performance trick: the (u32) -NSEC gives 0x00000000Fxxxxxxx
+        * so we subtract NSEC_PER_SEC and add 1 to the upper 32 bit.
+        *
+        * it's equivalent to:
+        *   tv.nsec -= NSEC_PER_SEC
+        *   tv.sec ++;
+        */
+       if (nsecs >= NSEC_PER_SEC)
+               res.tv64 += (u32)-NSEC_PER_SEC;
+       return res;
 }

 /**

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: ktime-check-for-seconds-in-nanoseconds.patch --]
[-- Type: text/x-diff; name=ktime-check-for-seconds-in-nanoseconds.patch, Size: 802 bytes --]

diff --git a/include/linux/ktime.h b/include/linux/ktime.h
index ce59832..7c0ad35 100644
--- a/include/linux/ktime.h
+++ b/include/linux/ktime.h
@@ -150,7 +150,19 @@ static inline ktime_t timeval_to_ktime(struct timeval tv)
 /* Set a ktime_t variable to a value in sec/nsec representation: */
 static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
 {
-	return (ktime_t) { .tv = { .sec = secs, .nsec = nsecs } };
+	ktime_t res = { .tv = { .sec = secs, .nsec = nsecs }};;
+	
+	/*
+	 * performance trick: the (u32) -NSEC gives 0x00000000Fxxxxxxx
+	 * so we subtract NSEC_PER_SEC and add 1 to the upper 32 bit.
+	 *
+	 * it's equivalent to:
+	 *   tv.nsec -= NSEC_PER_SEC
+	 *   tv.sec ++;
+	 */
+	if (nsecs >= NSEC_PER_SEC)
+		res.tv64 += (u32)-NSEC_PER_SEC;
+	return res;
 }
 
 /**

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: ktime_set() does not check for nanoseconds > one second
  2008-09-01 11:57   ` Matt Fleming
@ 2008-09-01 13:06     ` Thomas Gleixner
  2008-09-01 13:11       ` Matt Fleming
  0 siblings, 1 reply; 7+ messages in thread
From: Thomas Gleixner @ 2008-09-01 13:06 UTC (permalink / raw)
  To: Matt Fleming; +Cc: linux-kernel

On Mon, 1 Sep 2008, Matt Fleming wrote:
> > Yeah, a check for this in ktime_set() might make sense. Currently it's
> > up to the programmer to provide sane values. :)
> >
> > Thanks,
> >
> >        tglx
> >
> 
> How about the attached patch?

Won't work with arbitrary numbers. i.e. >= 2* NSEC_PER_SEC. Looking at
it, I'm even less convinced that it is a good idea.

      tglx

> Matt
> 
> diff --git a/include/linux/ktime.h b/include/linux/ktime.h
> index ce59832..7c0ad35 100644
> --- a/include/linux/ktime.h
> +++ b/include/linux/ktime.h
> @@ -150,7 +150,19 @@ static inline ktime_t timeval_to_ktime(struct timeval tv)
>  /* Set a ktime_t variable to a value in sec/nsec representation: */
>  static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
>  {
> -       return (ktime_t) { .tv = { .sec = secs, .nsec = nsecs } };
> +       ktime_t res = { .tv = { .sec = secs, .nsec = nsecs }};;
> +
> +       /*
> +        * performance trick: the (u32) -NSEC gives 0x00000000Fxxxxxxx
> +        * so we subtract NSEC_PER_SEC and add 1 to the upper 32 bit.
> +        *
> +        * it's equivalent to:
> +        *   tv.nsec -= NSEC_PER_SEC
> +        *   tv.sec ++;
> +        */
> +       if (nsecs >= NSEC_PER_SEC)
> +               res.tv64 += (u32)-NSEC_PER_SEC;
> +       return res;
>  }
> 
>  /**
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: ktime_set() does not check for nanoseconds > one second
  2008-09-01 13:06     ` Thomas Gleixner
@ 2008-09-01 13:11       ` Matt Fleming
  2008-09-01 13:16         ` Thomas Gleixner
  0 siblings, 1 reply; 7+ messages in thread
From: Matt Fleming @ 2008-09-01 13:11 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: linux-kernel

2008/9/1 Thomas Gleixner <tglx@linutronix.de>:
>
> Won't work with arbitrary numbers. i.e. >= 2* NSEC_PER_SEC. Looking at
> it, I'm even less convinced that it is a good idea.
>
>      tglx
>

Do you have another suggestion? Or should I just account for the >= 1
sec case in the calling code?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: ktime_set() does not check for nanoseconds > one second
  2008-09-01 13:11       ` Matt Fleming
@ 2008-09-01 13:16         ` Thomas Gleixner
  2008-09-01 13:33           ` Matt Fleming
  0 siblings, 1 reply; 7+ messages in thread
From: Thomas Gleixner @ 2008-09-01 13:16 UTC (permalink / raw)
  To: Matt Fleming; +Cc: linux-kernel

On Mon, 1 Sep 2008, Matt Fleming wrote:

> 2008/9/1 Thomas Gleixner <tglx@linutronix.de>:
> >
> > Won't work with arbitrary numbers. i.e. >= 2* NSEC_PER_SEC. Looking at
> > it, I'm even less convinced that it is a good idea.
> >
> >      tglx
> >
> 
> Do you have another suggestion? Or should I just account for the >= 1
> sec case in the calling code?

If you use runtime generated nsec values, why don't you use
ktime_add_ns() and feed the nsec value into it ?

Thanks,

	tglx

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: ktime_set() does not check for nanoseconds > one second
  2008-09-01 13:16         ` Thomas Gleixner
@ 2008-09-01 13:33           ` Matt Fleming
  0 siblings, 0 replies; 7+ messages in thread
From: Matt Fleming @ 2008-09-01 13:33 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: linux-kernel

2008/9/1 Thomas Gleixner <tglx@linutronix.de>:
> On Mon, 1 Sep 2008, Matt Fleming wrote:
>
>> 2008/9/1 Thomas Gleixner <tglx@linutronix.de>:
>> >
>> > Won't work with arbitrary numbers. i.e. >= 2* NSEC_PER_SEC. Looking at
>> > it, I'm even less convinced that it is a good idea.
>> >
>> >      tglx
>> >
>>
>> Do you have another suggestion? Or should I just account for the >= 1
>> sec case in the calling code?
>
> If you use runtime generated nsec values, why don't you use
> ktime_add_ns() and feed the nsec value into it ?
>

Hmm.. good point. I'll do it that way. Thanks for your help Thomas.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-09-01 13:33 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-01 10:38 ktime_set() does not check for nanoseconds > one second Matt Fleming
2008-09-01 11:20 ` Thomas Gleixner
2008-09-01 11:57   ` Matt Fleming
2008-09-01 13:06     ` Thomas Gleixner
2008-09-01 13:11       ` Matt Fleming
2008-09-01 13:16         ` Thomas Gleixner
2008-09-01 13:33           ` Matt Fleming

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®