ktime_set() does not check for nanoseconds > one second
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
Quoted 8 lines · Matt Fleming
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 toktime_set()and the return value was then used in aktime_add()call, which returned an incorrect result. Should the caller ofktime_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
2008/9/1 Thomas Gleixner <[email protected]>:
Quoted 10 lines · Matt Fleming
Quoted 8 lines
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 toktime_set()and the return value was then used in aktime_add()call, which returned an incorrect result. Should the caller ofktime_set()make this check or is it possible to move this logic in to the function itself?
Quoted 7 lines
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
include/linux/ktime.h +13 -1
@@ -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; }/**
include/linux/ktime.h +13 -1
@@ -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; }/**
Quoted 9 lines · Matt Fleming
Quoted 7 lines
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
Quoted 28 lines
Matt
include/linux/ktime.h +13
-1@@ -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; }/**
2008/9/1 Thomas Gleixner <[email protected]>:
Quoted 6 lines
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?
Quoted 10 lines · Matt Fleming
2008/9/1 Thomas Gleixner <[email protected]>:
Quoted 6 lines
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
2008/9/1 Thomas Gleixner <[email protected]>:
Quoted 7 lines · Matt Fleming
Quoted 6 lines
2008/9/1 Thomas Gleixner <[email protected]>:
Quoted 4 lines
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.
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
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.