linux-mips
[Top] [All Lists]

Re: [PATCH] MIPS: Don't overflow cevt-r4k.c calculations at high clock r

To: David Daney <ddaney@caviumnetworks.com>
Subject: Re: [PATCH] MIPS: Don't overflow cevt-r4k.c calculations at high clock rates.
From: Sergei Shtylyov <sshtylyov@mvista.com>
Date: Wed, 19 May 2010 13:52:45 +0400
Cc: linux-mips@linux-mips.org, ralf@linux-mips.org, Thomas Gleixner <tglx@linutronix.de>
In-reply-to: <1274220676-4562-1-git-send-email-ddaney@caviumnetworks.com>
Original-recipient: rfc822;linux-mips@linux-mips.org
References: <1274220676-4562-1-git-send-email-ddaney@caviumnetworks.com>
Sender: linux-mips-bounce@linux-mips.org
User-agent: Thunderbird 2.0.0.24 (Windows/20100228)
Hello.

David Daney wrote:

The 'mult' element of struct clock_event_device must never be wider
than 32-bits.  If it were, it would get truncated when used by
clockevent_delta2ns() when this calls do_div().

We meet the requirement by ensuring that the relationship:

 (mips_hpt_frequency >> (32 - shift)) < NSEC_PER_SEC

Always holds.

Signed-off-by: David Daney <ddaney@caviumnetworks.com>
CC: Thomas Gleixner <tglx@linutronix.de>
[...]
diff --git a/arch/mips/kernel/cevt-r4k.c b/arch/mips/kernel/cevt-r4k.c
index 0b2450c..4495158 100644
--- a/arch/mips/kernel/cevt-r4k.c
+++ b/arch/mips/kernel/cevt-r4k.c
[...]
@@ -189,8 +190,17 @@ int __cpuinit r4k_clockevent_init(void)
        cd->features         = CLOCK_EVT_FEAT_ONESHOT;
/* Calculate the min / max delta */
-       cd->mult     = div_sc((unsigned long) mips_freq, NSEC_PER_SEC, 32);
-       cd->shift            = 32;
+       shift = 32;
+       while (scaled_freq >= NSEC_PER_SEC && shift) {
+               scaled_freq = scaled_freq >> 1;

   Why not:

                scaled_freq >>= 1;

   It's C after all. :-)

WBR, Sergei

<Prev in Thread] Current Thread [Next in Thread>