[PATCH v2 0/4] avoid double timer interrupt with nohz and Intel TSC

From: Nicolai Stange
Date: Sun Jul 10 2016 - 15:31:26 EST


With a single task running on a NOHZ CPU on an Intel Haswell, I recognized
that I did not only get the one expected local_timer APIC interrupt, but
two per second at minimum.

Further tracing showed that the first one preceedes the programmed deadline
by up to ~50us and hence, it did nothing except for reprogramming the TSC
deadline clockevent device to trigger shortly thereafter again.

FYI, the trace looks like this:

<...>-2938 [007] d.h. 420.753164: local_timer_entry: vector=239
<...>-2938 [007] d.h. 420.753164: __hrtimer_run_queues <-hrtimer_interrupt
<...>-2938 [007] d.h. 420.753184: local_timer_entry: vector=239
<...>-2938 [007] d.h. 420.753184: __hrtimer_run_queues <-hrtimer_interrupt
<...>-2938 [007] d.h. 420.753195: tick_sched_timer <-__hrtimer_run_queues
<...>-2938 [007] d.h. 421.752170: local_timer_entry: vector=239
<...>-2938 [007] d.h. 421.752171: __hrtimer_run_queues <-hrtimer_interrupt
<...>-2938 [007] d.h. 421.752202: local_timer_entry: vector=239
<...>-2938 [007] d.h. 421.752202: __hrtimer_run_queues <-hrtimer_interrupt
<...>-2938 [007] d.h. 421.752202: tick_sched_timer <-__hrtimer_run_queues

It turns out that this too early programmed TSC deadline is caused by
inaccuracies in some frequency calculations which become significant if
the timer periods become large as it is the case for nohz with one task
(delta = 10^9ns).

The first three patches address inaccuracies entering the TSC deadline
clockevent devices' frequency.

The fourth patch is the most important one as it addresses the error
of largest relative magnitude. It is caused by the assumption in the
clockevents core that the ratio of the monotonic clock's frequency to that
of the clockevent device's is a constant. Since the monotonic clock's
frequency gets dynamically adjusted in order to compensate for NTP errors,
this is not true.

With this patchset applied, the trace looks like this:

<...>-23609 [007] d.h. 1811.586658: local_timer_entry: vector=239
<...>-23609 [007] d.h. 1811.586680: __hrtimer_run_queues <-hrtimer_interrupt
<...>-23609 [007] d.h. 1811.586680: tick_sched_timer <-__hrtimer_run_queues
<...>-23609 [007] d.h. 1812.585659: local_timer_entry: vector=239
<...>-23609 [007] d.h. 1812.585666: __hrtimer_run_queues <-hrtimer_interrupt
<...>-23609 [007] d.h. 1812.585666: tick_sched_timer <-__hrtimer_run_queues
<...>-23609 [007] d.h. 1813.584661: local_timer_entry: vector=239
<...>-23609 [007] d.h. 1813.584668: __hrtimer_run_queues <-hrtimer_interrupt
<...>-23609 [007] d.h. 1813.584668: tick_sched_timer <-__hrtimer_run_queues

Please note that the first three TSC-patches might not be necessary to
get this result. In fact, [3/4] ("arch, x86, tsc: inform TSC deadline
clockevent device about") is somewhat counterproductive in the sense that
on my system, it usually corrects the TSC deadline device's frequency
towards lower values and thus, facilitates the too-early interrupt
behaviour initially described. However, I decided to send them along with
the fourth patch because
- I tested the fourth patch in this setting
- I believe that a greater accurracy of the TSC deadline device is
worthwhile on its own


Changes to v1:
- [1/4] ("arch, x86, tsc deadline clockevent dev: reduce frequency
roundoff error")
No changes to the patch. Note that the v1 mail could not be delivered
to the author of the TSC_DIVISOR introducing commit 279f1461432c
("x86: apic: Use tsc deadline for oneshot when available"),
Suresh Siddha <suresh.b.siddha@xxxxxxxxx>, so I had to remove him
from the CC list.

- [2/4] ("arch, x86, tsc deadline clockevent dev: reduce TSC_DIVISOR
to 2")
Likewise.

- [3/4] ("arch, x86, tsc: inform TSC deadline clockevent device about
recalibration")
Silence the kbuild test robot on ARCH=i386 by wrapping the new call
to lapic_update_tsc_freq() from arch/x86/kernel/tsc.c in an
#ifdef CONFIG_X86_LOCAL_APIC.

- [4/4] ("kernel/time/clockevents: compensate for monotonic clock's
dynamic frequency")
I carelessly did two non-do_div() 64 bit divisions and the kbuild
test robot complained. The first one was in charge of checking a
u64 by u32 multiplication for overflow. Purge it by splitting the
u64 factor in two halves and multiplying each on its own while
simultaneously doing the overflow checks. Make the second u64
division explicit by invoking do_div().


Nicolai Stange (4):
arch, x86, tsc deadline clockevent dev: reduce frequency roundoff
error
arch, x86, tsc deadline clockevent dev: reduce TSC_DIVISOR to 2
arch, x86, tsc: inform TSC deadline clockevent device about
recalibration
kernel/time/clockevents: compensate for monotonic clock's dynamic
frequency

arch/x86/include/asm/apic.h | 1 +
arch/x86/kernel/apic/apic.c | 29 ++++++++++++++++++++--
arch/x86/kernel/tsc.c | 6 +++++
kernel/time/clockevents.c | 1 +
kernel/time/timekeeping.c | 59 +++++++++++++++++++++++++++++++++++++++++++++
kernel/time/timekeeping.h | 1 +
6 files changed, 95 insertions(+), 2 deletions(-)

--
2.9.0