Skip to content

Commit 2eebdde

Browse files
richardcochrandavem330
authored andcommitted
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>
1 parent f25a30b commit 2eebdde

4 files changed

Lines changed: 40 additions & 17 deletions

File tree

‎drivers/net/ethernet/mellanox/mlx4/en_clock.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void mlx4_en_init_timestamp(struct mlx4_en_dev *mdev)
240240
{
241241
struct mlx4_dev *dev = mdev->dev;
242242
unsigned long flags;
243-
u64 ns;
243+
u64 ns, zero = 0;
244244

245245
rwlock_init(&mdev->clock_lock);
246246

@@ -265,7 +265,7 @@ void mlx4_en_init_timestamp(struct mlx4_en_dev *mdev)
265265
/* Calculate period in seconds to call the overflow watchdog - to make
266266
* sure counter is checked at least once every wrap around.
267267
*/
268-
ns = cyclecounter_cyc2ns(&mdev->cycles, mdev->cycles.mask);
268+
ns = cyclecounter_cyc2ns(&mdev->cycles, mdev->cycles.mask, zero, &zero);
269269
do_div(ns, NSEC_PER_SEC / 2 / HZ);
270270
mdev->overflow_period = ns;
271271

‎include/linux/timecounter.h‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,32 @@ struct cyclecounter {
5555
* @cycle_last: most recent cycle counter value seen by
5656
* timecounter_read()
5757
* @nsec: continuously increasing count
58+
* @mask: bit mask for maintaining the 'frac' field
59+
* @frac: accumulated fractional nanoseconds
5860
*/
5961
struct timecounter {
6062
const struct cyclecounter *cc;
6163
cycle_t cycle_last;
6264
u64 nsec;
65+
u64 mask;
66+
u64 frac;
6367
};
6468

6569
/**
6670
* cyclecounter_cyc2ns - converts cycle counter cycles to nanoseconds
6771
* @cc: Pointer to cycle counter.
6872
* @cycles: Cycles
69-
*
70-
* XXX - This could use some mult_lxl_ll() asm optimization. Same code
71-
* as in cyc2ns, but with unsigned result.
73+
* @mask: bit mask for maintaining the 'frac' field
74+
* @frac: pointer to storage for the fractional nanoseconds.
7275
*/
7376
static inline u64 cyclecounter_cyc2ns(const struct cyclecounter *cc,
74-
cycle_t cycles)
77+
cycle_t cycles, u64 mask, u64 *frac)
7578
{
76-
u64 ret = (u64)cycles;
77-
ret = (ret * cc->mult) >> cc->shift;
78-
return ret;
79+
u64 ns = (u64) cycles;
80+
81+
ns = (ns * cc->mult) + *frac;
82+
*frac = ns & mask;
83+
return ns >> cc->shift;
7984
}
8085

8186
/**

‎kernel/time/timecounter.c‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ void timecounter_init(struct timecounter *tc,
2525
tc->cc = cc;
2626
tc->cycle_last = cc->read(cc);
2727
tc->nsec = start_tstamp;
28+
tc->mask = (1ULL << cc->shift) - 1;
29+
tc->frac = 0;
2830
}
2931
EXPORT_SYMBOL_GPL(timecounter_init);
3032

@@ -51,7 +53,8 @@ static u64 timecounter_read_delta(struct timecounter *tc)
5153
cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
5254

5355
/* 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);
5558

5659
/* update time stamp of timecounter_read_delta() call: */
5760
tc->cycle_last = cycle_now;
@@ -72,22 +75,36 @@ u64 timecounter_read(struct timecounter *tc)
7275
}
7376
EXPORT_SYMBOL_GPL(timecounter_read);
7477

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+
7592
u64 timecounter_cyc2time(struct timecounter *tc,
7693
cycle_t cycle_tstamp)
7794
{
78-
u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
79-
u64 nsec;
95+
u64 delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
96+
u64 nsec = tc->nsec, frac = tc->frac;
8097

8198
/*
8299
* Instead of always treating cycle_tstamp as more recent
83100
* than tc->cycle_last, detect when it is too far in the
84101
* future and treat it as old time stamp instead.
85102
*/
86-
if (cycle_delta > tc->cc->mask / 2) {
87-
cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
88-
nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
103+
if (delta > tc->cc->mask / 2) {
104+
delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
105+
nsec -= cc_cyc2ns_backwards(tc->cc, delta, tc->mask, frac);
89106
} else {
90-
nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
107+
nsec += cyclecounter_cyc2ns(tc->cc, delta, tc->mask, &frac);
91108
}
92109

93110
return nsec;

‎virt/kvm/arm/arch_timer.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
152152
return;
153153
}
154154

155-
ns = cyclecounter_cyc2ns(timecounter->cc, cval - now);
155+
ns = cyclecounter_cyc2ns(timecounter->cc, cval - now, timecounter->mask,
156+
&timecounter->frac);
156157
timer_arm(timer, ns);
157158
}
158159

0 commit comments

Comments
 (0)