From patchwork Tue May 22 10:41:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fabrizio Castro X-Patchwork-Id: 136528 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp1445969lji; Tue, 22 May 2018 03:42:45 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrvwVph/eEKGuoEmzoSQKFtr5dC17bPBoIV9U0ON2qfkNTGtAjnfFmVkhYhav+UYauYvaIH X-Received: by 2002:a17:902:462:: with SMTP id 89-v6mr23856992ple.300.1526985765340; Tue, 22 May 2018 03:42:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526985765; cv=none; d=google.com; s=arc-20160816; b=nl5wAusVPPQlxxw3atnuqTsOyjlkusOzQusPgcYHP+Qgo5IXN9WKuXs4aScWnlsPsv YNNdhJIf2gkab3Wsy4bPUdqPiW6MELk2r886VZkMdRYNOPU0a5nBTgQvxXm6hvDiPHsV wclBvCSO3s+95PYFzKyoKNH3xIsyExDVVbA9Xo9ggVDOkT+dNGjaqcs+5WtMibBKfe1a 1KME2zKoj8Ej/Das6/a7iIAL4SXKclfLv54iNhy0aPcNpJfeCl+jCz+sVqmQHYvnIaOR xoaS1KGSrkqi2eyDt9OogDv1sxHctUr1NfSfbXrsrZoo1sQd8xUGzQWCgnTbFL7r2V4n LnsQ== 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=P4ojDodjlz4OzGX6OpFgTGQ8mABk27UCqoCtVFyPmFE=; b=vFrYGmbfxs5Wt9MIr/lm4CwYg9VtbtTpdRi260qgTW3Kzlfr0Q/Lbx/7AxIkFN0xZv F8pmxCaD1WDfygFLwgioifcvxxXnFM+4oWWu54vs43Se2w5ii9cgMh1ydzrlIp8JXpwG BqYNY8rWOMS2tdDM7kPV3m9KX82Tdjgvu5TDY/pD86lJSWwYsDqbHErWf6ljvZwkVCdY u8p7AZfsscnX9UdFb4nv+kDTmgt9ZPjY6K5m9YMvH9Au4T5b2fd/U5XDDlbK6nvwgl45 eVlhXsh8IBGxouImgptsCrPvhdQ8LCj8SUFepk25zbzbAuht/g36obHpA9j7n5fat9Q1 bFmg== 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 f11-v6si16620486plj.58.2018.05.22.03.42.45; Tue, 22 May 2018 03:42:45 -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 S1751002AbeEVKmo (ORCPT + 11 others); Tue, 22 May 2018 06:42:44 -0400 Received: from relmlor2.renesas.com ([210.160.252.172]:28893 "EHLO relmlie1.idc.renesas.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750733AbeEVKmo (ORCPT ); Tue, 22 May 2018 06:42:44 -0400 Received: from unknown (HELO relmlir4.idc.renesas.com) ([10.200.68.154]) by relmlie1.idc.renesas.com with ESMTP; 22 May 2018 19:42:42 +0900 Received: from relmlii2.idc.renesas.com (relmlii2.idc.renesas.com [10.200.68.66]) by relmlir4.idc.renesas.com (Postfix) with ESMTP id A3BED88963; Tue, 22 May 2018 19:42:42 +0900 (JST) X-IronPort-AV: E=Sophos;i="5.49,429,1520866800"; d="scan'208";a="281710575" Received: from unknown (HELO fabrizio-dev.ree.adwin.renesas.com) ([10.226.36.229]) by relmlii2.idc.renesas.com with ESMTP; 22 May 2018 19:42:38 +0900 From: Fabrizio Castro To: Greg Kroah-Hartman Cc: John Stultz , Thomas Gleixner , Biju Das , Ingo Molnar , Chris Paterson , Fabrizio Castro , stable@vger.kernel.org, Ben Hutchings , cip-dev@lists.cip-project.org, Prarit Bhargava , Kevin Brodsky , Richard Cochran , Stephen Boyd , Will Deacon , Miroslav Lichvar Subject: [PATCH repost] time: Fix CLOCK_MONOTONIC_RAW sub-nanosecond accounting Date: Tue, 22 May 2018 11:41:50 +0100 Message-Id: <1526985710-15696-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, I am reposting this patch to include the relevant people in the email. Could you please consider this patch for 4.4.y? Testing 4.4.y without this patch makes tool tools/testing/selftests/timers/clocksource-switch.c fail on Koelsch board while running "Consistent CLOCK_MONOTONIC_RAW" with message "Delta: 1 ns". This patch fixes the problem. 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;