Skip to contentSkip to conversation

clocksource: avoid unnecessary overflow in cyclecounter_cyc2ns()

RFC PATCH In review Mike Galbraith 2 participants 5 messages Mar 4, 2014 – Mar 5, 2014 Timekeeping, clocksource core, NTP, alarmtimer

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 clocksource: avoid unnecessary overflow in cyclecounter_cyc2ns()

(crap crap crap... M.A.I.N.T.A.I.N.E.R.S _dummy_)

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 <[email protected]>
Cc John Stultz <[email protected]>,
---
 include/linux/clocksource.h |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
include/linux/clocksource.h +8 -3
@@ -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.
  */
 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);
 }
 
 /**
#1 lore raw
Quoted 10 lines · Mike Galbraith

(crap crap crap... M.A.I.N.T.A.I.N.E.R.S _dummy_)

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 <[email protected]>
Cc John Stultz <[email protected]>,

Thanks for sending this in! Curious exactly how the issue was being triggered? To some extent the cyclecounter/timecounter code never got the adoption I expected, so I've sort of had it on my list to see about killing that code off and merging its users w/ clocksources (since the clocksource has been simplified since cyclecounters landed). So curious to see what its actually being used for.. particularly since the timecounter code does have accumulation logic which should avoid overflows (and deal with hardware counters that wrap).

I'll put this in my 3.15 queue.

thanks
-john

#2 lore raw
Quoted 8 lines · John Stultz
Quoted 7 lines · Mike Galbraith

(crap crap crap... M.A.I.N.T.A.I.N.E.R.S _dummy_)

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.

Quoted 6 lines
Quoted 3 lines
Cc Salman Qazi <[email protected]>
Cc John Stultz <[email protected]>,

Thanks for sending this in! Curious exactly how the issue was being triggered?

Dunno that it is. This is the result of me rummaging around, looking for any excuse what-so-ever for a small and identical group of weird a$$ boxen running old 2.6.32 kernels (w. 208 day fix!) to manage to hop back and forth in time by exactly 208 days. Grep showed me that function, so I scurried off and swiped the fix.

-Mike

#3 lore raw
Quoted 13 lines · Mike Galbraith
Quoted 12 lines · John Stultz
Quoted 10 lines · Mike Galbraith

(crap crap crap... M.A.I.N.T.A.I.N.E.R.S _dummy_)

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 <[email protected]>
Cc John Stultz <[email protected]>,
Quoted 8 lines
Quoted 2 lines

Thanks for sending this in! Curious exactly how the issue was being triggered?

Dunno that it is. This is the result of me rummaging around, looking for any excuse what-so-ever for a small and identical group of weird a$$ boxen running old 2.6.32 kernels (w. 208 day fix!) to manage to hop back and forth in time by exactly 208 days. Grep showed me that function, so I scurried off and swiped the fix.

So.. this makes me a bit more hesitant to really queue this, particularly since the timecounter logic is supposed to periodically accumulate cycles so you don't run into these overflow issues (the earlier fix was for sched_clock which didn't do any accumulation).

So, if you're seeing time jump around, that's probably clocksource or timekeping related, and not tied to the cyclecounter code. Do you have any other info about these systems? What clocksource are they using, etc?

thanks
-john

#4 lore raw
Quoted 23 lines · John Stultz
Quoted 13 lines · Mike Galbraith
Quoted 12 lines · John Stultz
Quoted 10 lines · Mike Galbraith

(crap crap crap... M.A.I.N.T.A.I.N.E.R.S _dummy_)

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 <[email protected]>
Cc John Stultz <[email protected]>,
Quoted 8 lines
Quoted 2 lines

Thanks for sending this in! Curious exactly how the issue was being triggered?

Dunno that it is. This is the result of me rummaging around, looking for any excuse what-so-ever for a small and identical group of weird a$$ boxen running old 2.6.32 kernels (w. 208 day fix!) to manage to hop back and forth in time by exactly 208 days. Grep showed me that function, so I scurried off and swiped the fix.

Quoted 4 lines

So.. this makes me a bit more hesitant to really queue this, particularly since the timecounter logic is supposed to periodically accumulate cycles so you don't run into these overflow issues (the earlier fix was for sched_clock which didn't do any accumulation).

Ok.

Quoted 4 lines

So, if you're seeing time jump around, that's probably clocksource or timekeping related, and not tied to the cyclecounter code. Do you have any other info about these systems? What clocksource are they using, etc?

Not much, clocksource is TSC, with CPUs that should make it reliable. They're interested now, so I'll be hearing more digging more.

-Mike