From patchwork Fri May 19 23:21:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 100222 Delivered-To: patches@linaro.org Received: by 10.140.96.100 with SMTP id j91csp539570qge; Fri, 19 May 2017 16:22:04 -0700 (PDT) X-Received: by 10.98.214.90 with SMTP id r87mr12982634pfg.204.1495236123971; Fri, 19 May 2017 16:22:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1495236123; cv=none; d=google.com; s=arc-20160816; b=ZNYPOjS+YOu36nPlCNILrCECPgZh4TFIp4x9S5IR87IakVofJ1wR6EP90ziDjs+RN7 agWRGGAGO2Xbp8wnuBP8VOCOzv0LKDjq8c82b9SUNiO2oeIQLzLIRl1s2MutXMweTZX+ eFL+BQVw12FHJBuuF0YbCYocWgbn0Y2EjEc2qPBBCgucB5C/FtrnNenDD8eX7XZ8GpRp Jd9MrjiKRllZAol3YMcMc6Ya1so1qR/qUdG7kMEC3GB8ouUhck2x+BOXaQaQo6WitQa5 jXLgRuE5OT3hbQv6RR5AOCDH94n29xRLElpVKHqMSJr7RHdKObi60bRYRG2estpV7/gE zW/A== 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=QLiKyrHEyzOYTbZMaxAcM1PPzA+tP3becxTuLiBGU9M=; b=GbbM6QwA7692XslJAuEy/iA3pWZksHJiR0tV+VCEiwOP/2NB8WQrF5KFtt2Sp+Dx2A jHsnHODtPnlF6NCPp0h4u3yhn1VXSP0BkEHNwK/oSyUmYMvsyIFAVRw+AL2DutsHU4Jr oO62vJN21foG+hQ4AFU2MflUsq2C/V4bnesJhBEgUCUV82UVLIPA8lnKBjM5z3t0DWqc mIcyVIqqLpn4PaASx+7dLSfDyUX2B3gUkLzJvb6quR7cn0KVmwYamFdPLkXsjlOcAUhh t45GAneLUk2AjD1dArHq0p5bh4C3Cbo/cNvJiGPqjuJAqhfxx7QCSoKVibOls2/EJSbv JycQ== 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:c05::229 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-pg0-x229.google.com (mail-pg0-x229.google.com. [2607:f8b0:400e:c05::229]) by mx.google.com with ESMTPS id 33si9876368plb.270.2017.05.19.16.22.03 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 May 2017 16:22:03 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c05::229 as permitted sender) client-ip=2607:f8b0:400e:c05::229; 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:c05::229 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by mail-pg0-x229.google.com with SMTP id u28so44293040pgn.1 for ; Fri, 19 May 2017 16:22:03 -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=QLiKyrHEyzOYTbZMaxAcM1PPzA+tP3becxTuLiBGU9M=; b=BcZ7EHkLgiJZy7N1Ecq4s4bLHLCNnFAbLTbZPmLP4SxmtXFlfgEg0SHqQRo/h/5kio GceqMKfHnXle0nLifqEZddyOFESQWA/w9FuP5UTyTfvi+o4qs8q1fP/bd8k5FJX+COXD JtZdAq0WM5b4irsbSHdDiUhzstxha5bZxEeUA= 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=QLiKyrHEyzOYTbZMaxAcM1PPzA+tP3becxTuLiBGU9M=; b=trXje2DqGgFF1A/9bqiqybyGM09tlOR37tScZ2jpuINiOsTc04PSbYOSnDuIfp9GvC Bt3W7MBv2ocXXZA+F4CyjljMimV1aKnTSWqlYPp3U9p66CluetcbUz7DyO8O581PN9gQ 3aRssSHdlozOG1h9v7VQnbvbn/+en6fwkPS1rNhTm/H9vkhIwcwmgH80mcv2wYWFKs8S 7Z9yqjFEJ4rdaBfrpqHKKqrATs94dAScRIh1LIKSQm0QQi4LtltwpdbgD58AY3mJ8CQk HA2kHvHB9qn1AbqPxrf/dmWMS1ozauuEKWnnjPvHs+0UVCsIcgA17AuRTDKlWjN1TKW7 Ky5g== X-Gm-Message-State: AODbwcA6KpoM7CyEB1+hfjXLqAVY3NxtoRytXcVu9D2CFhcmeXWZsOzA AnjV8uwPMuW0omkk3wc= X-Received: by 10.98.31.155 with SMTP id l27mr12983513pfj.179.1495236123432; Fri, 19 May 2017 16:22:03 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:1002:83f0:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id c77sm16494077pfe.37.2017.05.19.16.22.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 19 May 2017 16:22:02 -0700 (PDT) From: John Stultz To: Kevin Brodsky Cc: John Stultz Subject: [DRAFT][PATCH] time: Fix CLOCK_MONOTONIC_RAW sub-nanosecond accounting Date: Fri, 19 May 2017 16:21:58 -0700 Message-Id: <1495236118-17277-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. 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 9652bc5..721c7f1 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -262,7 +262,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) { @@ -1976,7 +1976,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) @@ -1991,14 +1991,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 += 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 -= tk->raw_time.tv_nsec << tk->tkr_raw.shift; /* Accumulate error between NTP and clock interval */ tk->ntp_error += tk->ntp_tick << shift;