All of lore.kernel.org
 help / color / mirror / Atom feed
From: Henrik Austad <henrik@austad.us>
To: Mike Galbraith <bitbucket@online.de>
Cc: John Stultz <johnstul@us.ibm.com>, Salman Qazi <sqazi@google.com>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [RFC][PATCH] clocksource: avoid unnecessary overflow in cyclecounter_cyc2ns()
Date: Tue, 4 Mar 2014 08:20:20 +0100	[thread overview]
Message-ID: <20140304072020.GA18178@austad.us> (raw)
In-Reply-To: <1393910409.6415.9.camel@marge.simpson.net>

On Tue, Mar 04, 2014 at 06:20:09AM +0100, Mike Galbraith wrote:
> Greetings,
> 
> While rummaging around looking for HTH a gaggle of weird a$$ machines
> can manage to timewarp back and forth by exactly 208 days, I stumbled
> across $subject which looks like it may want to borrow Salman's fix.
> 
> clocksource: avoid unnecessary overflow in cyclecounter_cyc2ns()
> 
> As per 4cecf6d401a "sched, x86: Avoid unnecessary overflow in sched_clock",
> cycles * mult >> shift is overflow prone. so give it the same treatment.
> 
> Cc: Salman Qazi <sqazi@google.com>
> Cc: John Stultz <johnstul@us.ibm.com>
> Signed-off-by: Mike Galbraith <bitbucket@online.de>
> ---
>  include/linux/clocksource.h |   11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)
> 
> --- a/include/linux/clocksource.h
> +++ b/include/linux/clocksource.h
> @@ -77,13 +77,18 @@ struct timecounter {
>   *
>   * XXX - This could use some mult_lxl_ll() asm optimization. Same code
>   * as in cyc2ns, but with unsigned result.
> + *
> + * Because it is the same as x86 __cycles_2_ns, give it the same treatment as
> + * commit 4cecf6d401a "sched, x86: Avoid unnecessary overflow in sched_clock"
> + * to avoid a potential cycles * mult overflow.

Do we normally reference a particular commit in a comment? Why not just 
grab the same comment and add a "this is grabbed from arch/x86/... ?

>   */
>  static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc,
>  				      cycle_t cycles)
>  {
> -	u64 ret = (u64)cycles;
> -	ret = (ret * cc->mult) >> cc->shift;
> -	return ret;
> +	u64 quot = (u64)cycles >> cc->shift;
> +	u64 rem = (u64)cycles & ((1ULL << cc->shift) - 1);
> +
> +	return  quot * cc->mult + ((rem * cc->mult) >> cc->shift);
>  }

Makes sense to me, for whatever that's worth :)

Also, tile could probably do with a similar approach for ns2cycles (not 
that I have observed any problems, but in the sense of being consistent and 
all). I'll send a patch in a separate email as not to clutter this thread 
too much :)

-- 
Henrik Austad

  reply	other threads:[~2014-03-04  7:23 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-04  5:20 [RFC][PATCH] clocksource: avoid unnecessary overflow in cyclecounter_cyc2ns() Mike Galbraith
2014-03-04  7:20 ` Henrik Austad [this message]
2014-03-04  7:31   ` Mike Galbraith
2014-03-04  7:36     ` Mike Galbraith
2014-03-04  8:02       ` Henrik Austad
2014-03-04  8:24         ` Mike Galbraith
  -- strict thread matches above, loose matches on Subject: below --
2014-03-04  5:38 Mike Galbraith
2014-03-04  6:40 ` John Stultz
2014-03-04  7:10   ` Mike Galbraith
2014-03-05  0:58     ` John Stultz
2014-03-05  2:56       ` Mike Galbraith

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=20140304072020.GA18178@austad.us \
    --to=henrik@austad.us \
    --cc=bitbucket@online.de \
    --cc=johnstul@us.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sqazi@google.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.