From patchwork Mon May 21 17:57:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fabrizio Castro X-Patchwork-Id: 136497 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp606930lji; Mon, 21 May 2018 10:57:34 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpoL9j2cUyfg4YeruavTHjyY7dwYdwXVy5q3s82gN7OO9HHMfyhMQX/l319kZATP2Ze8XVP X-Received: by 2002:aa7:84c7:: with SMTP id x7-v6mr21100241pfn.195.1526925454654; Mon, 21 May 2018 10:57:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526925454; cv=none; d=google.com; s=arc-20160816; b=uAI0EZwX9Aiv1JxST+sm2NJMcUEhO0wvjeTPnB5XuXOuZWOoRmcE/tM+jQvo9cdOCo eleSXbvw/oQym6UkKvddCrdKgYuDs60eCYvmabD30Pp/Cr3qiPdUXLsq31580mVtBVrL 7HuKPymIX1GKszRzfuoQLyOk81Zb0viSPphogokPhgGVJCVl097itPevgq3dUj87hY9b +UYzHGXnIXa8Pr80mgBRBRWjWj7XyUbjLx3GWwxnuCPLyd2MjFYrtdCNU8o67hamZMYH BFYSPofkM+/1tS5oQknK0tZ0ZIVHzF0jzCh7V9DBfn0aEw+Yo8dgR1zErDYPojQ++ubX iASA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=WgBeosU1kXs2WUFSz57MZY45ngtDUOu+DKh3Ewsfj7k=; b=TF3crC4xgKFuaxiUVV4h/PjqlViyWreFJkqoIcfsvuWRbSVNbQNFPgIFX65AzuPKoi lNiEupzuFSl+NRDbMgfaRVGbCOyskl61gs72L+JO1Bgzbj3qOaO4s/tCBHOCCYd7uCVp 4dXggaXbXSUyaXUbVtBlcK1QpPGH3bGBqCddTQ65hvbWeo7XqTXWa6uJBaEgGABNdloh QMrrup04azO+lfYH9/9RVSIr2+STy//wREzwvrqhi7f1tCqnFUh169xHkRGaZUrVHeJA Z0e2xs8mCG0WvHbrI1RFDABELIb3rJIHCgVI0qhdbghuVsNx/gRG2nyzJ9R+6J+L8+sN J/Lg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f2-v6si15884440pli.569.2018.05.21.10.57.34; Mon, 21 May 2018 10:57:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of stable-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753587AbeEUR5c (ORCPT + 11 others); Mon, 21 May 2018 13:57:32 -0400 Received: from relmlor2.renesas.com ([210.160.252.172]:22780 "EHLO relmlie1.idc.renesas.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753577AbeEUR5a (ORCPT ); Mon, 21 May 2018 13:57:30 -0400 Received: from unknown (HELO relmlir2.idc.renesas.com) ([10.200.68.152]) by relmlie1.idc.renesas.com with ESMTP; 22 May 2018 02:57:29 +0900 Received: from relmlii1.idc.renesas.com (relmlii1.idc.renesas.com [10.200.68.65]) by relmlir2.idc.renesas.com (Postfix) with ESMTP id 29AB5434DD; Tue, 22 May 2018 02:57:29 +0900 (JST) X-IronPort-AV: E=Sophos;i="5.49,426,1520866800"; d="scan'208";a="280169853" Received: from unknown (HELO fabrizio-dev.ree.adwin.renesas.com) ([10.226.36.229]) by relmlii1.idc.renesas.com with ESMTP; 22 May 2018 02:57:27 +0900 From: Fabrizio Castro To: gregkh@linuxfoundation.org Cc: Ben Hutchings , Chris Paterson , Biju Das , Fabrizio Castro , stable@vger.kernel.org Subject: [PATCH] time: Fix CLOCK_MONOTONIC_RAW sub-nanosecond accounting Date: Mon, 21 May 2018 18:57:26 +0100 Message-Id: <1526925446-6067-1-git-send-email-fabrizio.castro@bp.renesas.com> X-Mailer: git-send-email 2.7.4 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: John Stultz commit 3d88d56c5873f6eebe23e05c3da701960146b801 upstream. Due to how the MONOTONIC_RAW accumulation logic was handled, there is the potential for a 1ns discontinuity when we do accumulations. This small discontinuity has for the most part gone un-noticed, but since ARM64 enabled CLOCK_MONOTONIC_RAW in their vDSO clock_gettime implementation, we've seen failures with the inconsistency-check test in kselftest. This patch addresses the issue by using the same sub-ns accumulation handling that CLOCK_MONOTONIC uses, which avoids the issue for in-kernel users. Since the ARM64 vDSO implementation has its own clock_gettime calculation logic, this patch reduces the frequency of errors, but failures are still seen. The ARM64 vDSO will need to be updated to include the sub-nanosecond xtime_nsec values in its calculation for this issue to be completely fixed. Signed-off-by: John Stultz Tested-by: Daniel Mentz Cc: Prarit Bhargava Cc: Kevin Brodsky Cc: Richard Cochran Cc: Stephen Boyd Cc: Will Deacon Cc: "stable #4 . 8+" Cc: Miroslav Lichvar Link: http://lkml.kernel.org/r/1496965462-20003-3-git-send-email-john.stultz@linaro.org Signed-off-by: Thomas Gleixner [fabrizio: cherry-pick to 4.4. Kept cycle_t type for function logarithmic_accumulation local variable "interval". Dropped casting of "interval" variable] Signed-off-by: Fabrizio Castro Signed-off-by: Biju Das --- Hello Greg, we noticed tools/testing/selftests/timers/clocksource-switch.c was failing for us, this patch fixes the cause of the failure. Are you happy to take this patch? Thanks, Fab include/linux/timekeeper_internal.h | 4 ++-- kernel/time/timekeeping.c | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) -- 2.7.4 diff --git a/include/linux/timekeeper_internal.h b/include/linux/timekeeper_internal.h index f0f1793..115216e 100644 --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -56,7 +56,7 @@ struct tk_read_base { * interval. * @xtime_remainder: Shifted nano seconds left over when rounding * @cycle_interval - * @raw_interval: Raw nano seconds accumulated per NTP interval. + * @raw_interval: Shifted raw nano seconds accumulated per NTP interval. * @ntp_error: Difference between accumulated time and NTP time in ntp * shifted nano seconds. * @ntp_error_shift: Shift conversion between clock shifted nano seconds and @@ -97,7 +97,7 @@ struct timekeeper { cycle_t cycle_interval; u64 xtime_interval; s64 xtime_remainder; - u32 raw_interval; + u64 raw_interval; /* The ntp_tick_length() value currently being used. * This cached copy ensures we consistently apply the tick * length for an entire tick, as ntp_tick_length may change diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 6e48668..fed86b2 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -277,8 +277,7 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) /* Go back from cycles -> shifted ns */ tk->xtime_interval = (u64) interval * clock->mult; tk->xtime_remainder = ntpinterval - tk->xtime_interval; - tk->raw_interval = - ((u64) interval * clock->mult) >> clock->shift; + tk->raw_interval = interval * clock->mult; /* if changing clocks, convert xtime_nsec shift units */ if (old_clock) { @@ -1767,7 +1766,7 @@ static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset, unsigned int *clock_set) { cycle_t interval = tk->cycle_interval << shift; - u64 raw_nsecs; + u64 snsec_per_sec; /* If the offset is smaller than a shifted interval, do nothing */ if (offset < interval) @@ -1782,14 +1781,15 @@ static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset, *clock_set |= accumulate_nsecs_to_secs(tk); /* Accumulate raw time */ - raw_nsecs = (u64)tk->raw_interval << shift; - raw_nsecs += tk->raw_time.tv_nsec; - if (raw_nsecs >= NSEC_PER_SEC) { - u64 raw_secs = raw_nsecs; - raw_nsecs = do_div(raw_secs, NSEC_PER_SEC); - tk->raw_time.tv_sec += raw_secs; + tk->tkr_raw.xtime_nsec += (u64)tk->raw_time.tv_nsec << tk->tkr_raw.shift; + tk->tkr_raw.xtime_nsec += tk->raw_interval << shift; + snsec_per_sec = (u64)NSEC_PER_SEC << tk->tkr_raw.shift; + while (tk->tkr_raw.xtime_nsec >= snsec_per_sec) { + tk->tkr_raw.xtime_nsec -= snsec_per_sec; + tk->raw_time.tv_sec++; } - tk->raw_time.tv_nsec = raw_nsecs; + tk->raw_time.tv_nsec = tk->tkr_raw.xtime_nsec >> tk->tkr_raw.shift; + tk->tkr_raw.xtime_nsec -= (u64)tk->raw_time.tv_nsec << tk->tkr_raw.shift; /* Accumulate error between NTP and clock interval */ tk->ntp_error += tk->ntp_tick << shift;