From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf0-f195.google.com ([209.85.192.195]:35627 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752063AbcJEAa5 (ORCPT ); Tue, 4 Oct 2016 20:30:57 -0400 Date: Tue, 4 Oct 2016 17:30:51 -0700 From: Alexei Starovoitov To: John Stultz Cc: lkml , Steven Rostedt , Peter Zijlstra , Ingo Molnar , Thomas Gleixner , stable , Brendan Gregg Subject: Re: [PATCH 1/2] timekeeping: Avoid taking lock in NMI path with CONFIG_DEBUG_TIMEKEEPING Message-ID: <20161005003049.GA75907@ast-mbp.thefacebook.com> References: <1471993702-29148-1-git-send-email-john.stultz@linaro.org> <1471993702-29148-2-git-send-email-john.stultz@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1471993702-29148-2-git-send-email-john.stultz@linaro.org> Sender: stable-owner@vger.kernel.org List-ID: On Tue, Aug 23, 2016 at 04:08:21PM -0700, John Stultz wrote: > When I added some extra sanity checking in timekeeping_get_ns() under > CONFIG_DEBUG_TIMEKEEPING, I missed that the NMI safe __ktime_get_fast_ns() > method was using timekeeping_get_ns(). > > Thus the locking added to the debug checks broke the NMI-safety of > __ktime_get_fast_ns(). > > This patch open-codes the timekeeping_get_ns() logic for > __ktime_get_fast_ns(), so can avoid any deadlocks in NMI. > > Cc: Steven Rostedt > Cc: Peter Zijlstra > Cc: Ingo Molnar > Cc: Thomas Gleixner > Cc: stable # 4.1+ > Reported-by: Steven Rostedt > Reported-by: Peter Zijlstra > Signed-off-by: John Stultz > --- > kernel/time/timekeeping.c | 5 ++++- > 1 file changed, 4 insertions(+), 1 deletion(-) > > diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c > index 3b65746..e07fb09 100644 > --- a/kernel/time/timekeeping.c > +++ b/kernel/time/timekeeping.c > @@ -401,7 +401,10 @@ static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf) > do { > seq = raw_read_seqcount_latch(&tkf->seq); > tkr = tkf->base + (seq & 0x01); > - now = ktime_to_ns(tkr->base) + timekeeping_get_ns(tkr); > + now = ktime_to_ns(tkr->base); > + > + now += clocksource_delta(tkr->read(tkr->clock), > + tkr->cycle_last, tkr->mask); we're seeing the time jumping backwards between __ktime_get_fast_ns calls. and looks like this patch broke it, since delta is being added to ns. It seems it should be: now += timekeeping_delta_to_ns(clocksource_delta(...)); or better fix possible? Reported-by: Brendan Gregg # perf record -F 9 --cpu 0 --clockid CLOCK_MONOTONIC -- sleep 1 # perf script swapper 0 [000] 2942480.468981: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 2942480.471468: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 2942480.473959: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 2942480.475202: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 2942480.477691: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 2942481.470226: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 2942481.472569: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 2942481.476460: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 2942481.478943: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) Note the timestamp jump. fixed (rolled back patch): # perf record -F 9 --cpu 0 --clockid CLOCK_MONOTONIC -- sleep 1 # perf script swapper 0 [000] 589.729663: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 589.840772: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 589.951883: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 590.062994: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 590.174075: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 590.285217: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 590.396328: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 590.507439: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux) swapper 0 [000] 590.618550: 111111111 cpu-clock: ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)