diff options
| author | Richard Cochran <richardcochran@gmail.com> | 2014-12-21 13:47:06 -0500 |
|---|---|---|
| committer | David S. Miller <davem@davemloft.net> | 2014-12-30 18:29:27 -0500 |
| commit | 2eebdde6528a722fbf8e2cffcf7aa52cbb4c2de0 (patch) | |
| tree | c4b176d1e40eecb2caa00952eae95ecb2b2a046b /kernel/time | |
| parent | f25a30be359d0535fb1c7c1619cabb0ad17cfbf1 (diff) | |
timecounter: keep track of accumulated fractional nanoseconds
The current timecounter implementation will drop a variable amount
of resolution, depending on the magnitude of the time delta. In
other words, reading the clock too often or too close to a time
stamp conversion will introduce errors into the time values. This
patch fixes the issue by introducing a fractional nanosecond field
that accumulates the low order bits.
Reported-by: Janusz Użycki <j.uzycki@elproma.com.pl>
Signed-off-by: Richard Cochran <richardcochran@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'kernel/time')
| -rw-r--r-- | kernel/time/timecounter.c | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/kernel/time/timecounter.c b/kernel/time/timecounter.c index 59a1ec3a57cb..4687b3104bae 100644 --- a/kernel/time/timecounter.c +++ b/kernel/time/timecounter.c | |||
| @@ -25,6 +25,8 @@ void timecounter_init(struct timecounter *tc, | |||
| 25 | tc->cc = cc; | 25 | tc->cc = cc; |
| 26 | tc->cycle_last = cc->read(cc); | 26 | tc->cycle_last = cc->read(cc); |
| 27 | tc->nsec = start_tstamp; | 27 | tc->nsec = start_tstamp; |
| 28 | tc->mask = (1ULL << cc->shift) - 1; | ||
| 29 | tc->frac = 0; | ||
| 28 | } | 30 | } |
| 29 | EXPORT_SYMBOL_GPL(timecounter_init); | 31 | EXPORT_SYMBOL_GPL(timecounter_init); |
| 30 | 32 | ||
| @@ -51,7 +53,8 @@ static u64 timecounter_read_delta(struct timecounter *tc) | |||
| 51 | cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask; | 53 | cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask; |
| 52 | 54 | ||
| 53 | /* convert to nanoseconds: */ | 55 | /* convert to nanoseconds: */ |
| 54 | ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta); | 56 | ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta, |
| 57 | tc->mask, &tc->frac); | ||
| 55 | 58 | ||
| 56 | /* update time stamp of timecounter_read_delta() call: */ | 59 | /* update time stamp of timecounter_read_delta() call: */ |
| 57 | tc->cycle_last = cycle_now; | 60 | tc->cycle_last = cycle_now; |
| @@ -72,22 +75,36 @@ u64 timecounter_read(struct timecounter *tc) | |||
| 72 | } | 75 | } |
| 73 | EXPORT_SYMBOL_GPL(timecounter_read); | 76 | EXPORT_SYMBOL_GPL(timecounter_read); |
| 74 | 77 | ||
| 78 | /* | ||
| 79 | * This is like cyclecounter_cyc2ns(), but it is used for computing a | ||
| 80 | * time previous to the time stored in the cycle counter. | ||
| 81 | */ | ||
| 82 | static u64 cc_cyc2ns_backwards(const struct cyclecounter *cc, | ||
| 83 | cycle_t cycles, u64 mask, u64 frac) | ||
| 84 | { | ||
| 85 | u64 ns = (u64) cycles; | ||
| 86 | |||
| 87 | ns = ((ns * cc->mult) - frac) >> cc->shift; | ||
| 88 | |||
| 89 | return ns; | ||
| 90 | } | ||
| 91 | |||
| 75 | u64 timecounter_cyc2time(struct timecounter *tc, | 92 | u64 timecounter_cyc2time(struct timecounter *tc, |
| 76 | cycle_t cycle_tstamp) | 93 | cycle_t cycle_tstamp) |
| 77 | { | 94 | { |
| 78 | u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask; | 95 | u64 delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask; |
| 79 | u64 nsec; | 96 | u64 nsec = tc->nsec, frac = tc->frac; |
| 80 | 97 | ||
| 81 | /* | 98 | /* |
| 82 | * Instead of always treating cycle_tstamp as more recent | 99 | * Instead of always treating cycle_tstamp as more recent |
| 83 | * than tc->cycle_last, detect when it is too far in the | 100 | * than tc->cycle_last, detect when it is too far in the |
| 84 | * future and treat it as old time stamp instead. | 101 | * future and treat it as old time stamp instead. |
| 85 | */ | 102 | */ |
| 86 | if (cycle_delta > tc->cc->mask / 2) { | 103 | if (delta > tc->cc->mask / 2) { |
| 87 | cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask; | 104 | delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask; |
| 88 | nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta); | 105 | nsec -= cc_cyc2ns_backwards(tc->cc, delta, tc->mask, frac); |
| 89 | } else { | 106 | } else { |
| 90 | nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec; | 107 | nsec += cyclecounter_cyc2ns(tc->cc, delta, tc->mask, &frac); |
| 91 | } | 108 | } |
| 92 | 109 | ||
| 93 | return nsec; | 110 | return nsec; |
