From patchwork Tue May 23 00:58:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 100345 Delivered-To: patches@linaro.org Received: by 10.140.96.100 with SMTP id j91csp93864qge; Mon, 22 May 2017 17:58:45 -0700 (PDT) X-Received: by 10.84.228.207 with SMTP id y15mr32299710pli.172.1495501125122; Mon, 22 May 2017 17:58:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1495501125; cv=none; d=google.com; s=arc-20160816; b=ye4H4yMbPyjlVBGbBoUaLphK/AhQmDbm6J/+d4oBZ5hy6knJTwn9qDE8tAz1Q49Eop nwcx7K/q/6OmLUP/rKF+8i2xWkIWR35GKReo/D9uuzvvfxHZE5S6XTQ0K+9zPwWjG76M zGk7Z07XgW+56NnwC+B56KKgANp0jxak+wFOedCE1YJ4DtUSS7Pde8U8DY9B1cc84hYg ZDwhW2o/syRW0Szb+VLsjKpP2hgkhvCihOEEDrRq/Pj0QLOrT4bdTnIWekspZvcQFd7w Jx58J+XLGOsWKQEd2SDJ1o3Vb769C4066dKrKRwFHCPkmSBwAks9mmH3nL0DhORjp3aK Vfzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=f5h36Dvxjwq7VrdAweDbOVj2s4quyfGo2gjG+w5HQ2w=; b=IDDCrrMkJs13T1tnQdfq84nUX1K9wWqsWyZJHGFRPGxIEnOGzJo5/cSg/B2iyshSzt Q2hIS0/hCrUR71qBmiQYdwDIFq4k2zgHjxNxkNXG74XVQoO3m3bmKzk2Q/snw/jqp4v4 AD7TDJ4HVKUaOQSK1p2uaaqKmHmRCq3Tcrv5qUyishV1FijCOzBscjSP528/j34B8Z3M Ic+dY5oADT70+Com2JRnku4+H6YaJuLNHZoVbRxcmjyGgsw2rbYjm5/Nj4C58viv9O2P FEoxtOajwga01K75k/zXXh3eqj6QU7N2OCzOzGU+kg7pmx66Mq3YLzyHGJlLnsFW1hZY QmiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::22d as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-pf0-x22d.google.com (mail-pf0-x22d.google.com. [2607:f8b0:400e:c00::22d]) by mx.google.com with ESMTPS id x11si18857483pfk.187.2017.05.22.17.58.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 May 2017 17:58:45 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::22d as permitted sender) client-ip=2607:f8b0:400e:c00::22d; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::22d as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by mail-pf0-x22d.google.com with SMTP id 9so97617033pfj.1 for ; Mon, 22 May 2017 17:58:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=f5h36Dvxjwq7VrdAweDbOVj2s4quyfGo2gjG+w5HQ2w=; b=fjlc1KzmMhmMHmyBeyXqwtDjN9rHnve9TcJ2SsgIneHN5aNQSBRlBNQcZSzlKT4zR9 xqYoUAI/x7g6kDESyQXIA3oatxnwoab1vfEbXYzSYmeWhKC3sq1zVcZH7MSJVUEjLtak XHeKonkz+2g2+LnOQEtpzfDf1WDsrmHhUfJO4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=f5h36Dvxjwq7VrdAweDbOVj2s4quyfGo2gjG+w5HQ2w=; b=MbmTTXpJ3+DBm0b5WqMSfJ0dUaeia77mQ+G3FwBoZlKPA/RObYMe3nXHjpGkfNMZSn OQvup1beY/iYEpEqUjS6x6FqLblMFniXkvq3LOiZYOenQq12wSiZojeY0Jhr6mKkADqf ysBxt0oGazcoJi/Yx2h5lzVxOoD8HfnJePlFajhYStxfcy+H7EP6/xMXII3uaLzZvIFq /zYt6gb2A+7wJ4ZNpxVEiQ/0o2D85eKqjAAhHJnN7HiBJQvXZ92PGWMxH5pB65bBSKE+ J95Cvm2ZZz863+y7GcIAcoAheRXgHCT6R++JxmO2wifolUYmgGSnBidMLF93u9yZjEOa 35OQ== X-Gm-Message-State: AODbwcAf7Vs9xfMH86xCjhHtqQHsL/G9PSPgNmNvmv0t+DFfDpYJ4gOH SZt+NGfoTvZN51iOlFY= X-Received: by 10.98.80.152 with SMTP id g24mr27984661pfj.222.1495501124496; Mon, 22 May 2017 17:58:44 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:1002:83f0:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id t17sm33543918pfj.61.2017.05.22.17.58.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 22 May 2017 17:58:43 -0700 (PDT) From: John Stultz To: Cc: John Stultz , Kevin Brodsky , Will Deacon Subject: [DRAFT v2][PATCH 1/2] time: Fix CLOCK_MONOTONIC_RAW sub-nanosecond accounting Date: Mon, 22 May 2017 17:58:28 -0700 Message-Id: <1495501109-32346-1-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 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. NOTE: Ideally we'd have a fix for the ARM64 vDSO after this patch. Cc: Kevin Brodsky Cc: Will Deacon Signed-off-by: John Stultz --- include/linux/timekeeper_internal.h | 4 ++-- kernel/time/timekeeping.c | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) -- 2.7.4 diff --git a/include/linux/timekeeper_internal.h b/include/linux/timekeeper_internal.h index 110f453..528cc86 100644 --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -58,7 +58,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 @@ -100,7 +100,7 @@ struct timekeeper { u64 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 501a173..75d1bf1 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -282,7 +282,7 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) /* Go back from cycles -> shifted ns */ tk->xtime_interval = interval * clock->mult; tk->xtime_remainder = ntpinterval - tk->xtime_interval; - tk->raw_interval = (interval * clock->mult) >> clock->shift; + tk->raw_interval = interval * clock->mult; /* if changing clocks, convert xtime_nsec shift units */ if (old_clock) { @@ -1995,7 +1995,7 @@ static u64 logarithmic_accumulation(struct timekeeper *tk, u64 offset, u32 shift, unsigned int *clock_set) { u64 interval = tk->cycle_interval << shift; - u64 raw_nsecs; + u64 nsecps; /* If the offset is smaller than a shifted interval, do nothing */ if (offset < interval) @@ -2010,14 +2010,15 @@ static u64 logarithmic_accumulation(struct timekeeper *tk, u64 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; + nsecps = (u64)NSEC_PER_SEC << tk->tkr_raw.shift; + while (tk->tkr_raw.xtime_nsec >= nsecps) { + tk->tkr_raw.xtime_nsec -= nsecps; + 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;