From: "Matt Fleming" <mattjfleming@googlemail.com>
To: "Thomas Gleixner" <tglx@linutronix.de>
Cc: linux-kernel@vger.kernel.org
Subject: Re: ktime_set() does not check for nanoseconds > one second
Date: Mon, 1 Sep 2008 12:57:29 +0100 [thread overview]
Message-ID: <5ff4a1e50809010457l7051b7e8vc7f86d68ae42e529@mail.gmail.com> (raw)
In-Reply-To: <alpine.LFD.1.10.0809011317460.3243@apollo.tec.linutronix.de>
[-- 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;
}
/**
next prev parent reply other threads:[~2008-09-01 11:57 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-09-01 10:38 Matt Fleming
2008-09-01 11:20 ` Thomas Gleixner
2008-09-01 11:57 ` Matt Fleming [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5ff4a1e50809010457l7051b7e8vc7f86d68ae42e529@mail.gmail.com \
--to=mattjfleming@googlemail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®