From: David Mosberger <davidm@napali.hpl.hp.com>
To: Linus Torvalds <torvalds@osdl.org>
Cc: davidm@hpl.hp.com, schwidefsky@de.ibm.com, linux-kernel@vger.kernel.org
Subject: Re: sparse warning, or why does jifies_to_msecs() return an int?
Date: Thu, 20 Jan 2005 15:46:49 -0800 [thread overview]
Message-ID: <16880.17129.508590.974315@napali.hpl.hp.com> (raw)
In-Reply-To: <Pine.LNX.4.58.0501150948120.2310@ppc970.osdl.org>
>>>>> On Sat, 15 Jan 2005 10:05:37 -0800 (PST), Linus Torvalds <torvalds@osdl.org> said:
Linus> Hmm.. I don't think your patch is wrong per se, but I do
Linus> think it's a bit too subtle. I'd almost rather make
Linus> "jiffies_to_msecs()" just test for overflow instead, and that
Linus> should also fix it.
You sure about that?
Actually, I think my patch was broken anyhow for HZ < 1000 because you
can potentially get integer-overflows in temporary results which could
make things come out wrong again.
I _think_ the attached patch works for all reasonable cases reasonably
uniformly, but if you thought the previous patch was subtle, I'm sure
you going to like this one even less.
Note that with the patch, platforms where HZ is not a power of two and
doesn't fit any of the other special cases (namely (HZ % 1000) != 0 &&
(1000 % HZ) != 0) would suffer a penalty. AFAICS, this is true only
for Alpha/Rawhide (HZ=1200). In such a case, rather than:
(j * 1000)/HZ
the new code would compute:
(j/HZ)*1000 + ((j%HZ)*1000)/HZ
It looks to me like we could get rid of all the ugly & complex
intermediate overflow-checks if we defined MAX_JIFFY_OFFSET
as:
(~0UL / 1000)
However, on a 32-bit platform that runs at 1000 Hz, this would limit
us to 4294 seconds. That may be cutting it a bit close.
--david
===== include/linux/jiffies.h 1.11 vs edited =====
--- 1.11/include/linux/jiffies.h 2005-01-04 18:48:02 -08:00
+++ edited/include/linux/jiffies.h 2005-01-20 15:21:14 -08:00
@@ -254,13 +254,32 @@
*/
static inline unsigned int jiffies_to_msecs(const unsigned long j)
{
+ unsigned long res;
+
#if HZ <= 1000 && !(1000 % HZ)
- return (1000 / HZ) * j;
+ unsigned long max = ~0UL / (1000 / HZ);
+
+ if (j > max)
+ max = j;
+ res = (1000 / HZ) * j;
#elif HZ > 1000 && !(HZ % 1000)
- return (j + (HZ / 1000) - 1)/(HZ / 1000);
+ res = (j + (HZ / 1000) - 1) / (HZ / 1000);
#else
- return (j * 1000) / HZ;
+ /*
+ * HZ better be a power of two; otherwise this gets real
+ * expensive. Better expensive than wrong, though.
+ */
+# if HZ < 1000
+ unsigned long max = (~0UL / 1000) * HZ;
+
+ if (j > max)
+ j = max;
+# endif
+ res = (j / HZ) * 1000 + ((j % HZ) * 1000) / HZ;
#endif
+ if (res > ~0U)
+ return ~0U;
+ return res;
}
static inline unsigned int jiffies_to_usecs(const unsigned long j)
next prev parent reply other threads:[~2005-01-20 23:48 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-01-15 2:21 David Mosberger
2005-01-15 4:29 ` Linus Torvalds
2005-01-15 5:57 ` David Mosberger
2005-01-15 18:05 ` Linus Torvalds
2005-01-20 23:46 ` David Mosberger [this message]
2005-05-03 18:43 ` Nish Aravamudan
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=16880.17129.508590.974315@napali.hpl.hp.com \
--to=davidm@napali.hpl.hp.com \
--cc=davidm@hpl.hp.com \
--cc=linux-kernel@vger.kernel.org \
--cc=schwidefsky@de.ibm.com \
--cc=torvalds@osdl.org \
/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®