From patchwork Mon May 22 19:08:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 100313 Delivered-To: patches@linaro.org Received: by 10.140.96.100 with SMTP id j91csp387576qge; Mon, 22 May 2017 12:08:09 -0700 (PDT) X-Received: by 10.99.8.5 with SMTP id 5mr27195045pgi.220.1495480089579; Mon, 22 May 2017 12:08:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1495480089; cv=none; d=google.com; s=arc-20160816; b=d+9G05c66TPI4Gm8GRpV93SX74b8zb4FXT/vae9Dxvqjjzs/O6tNg0RA705NfsVNVf K7t64giEqQKwJInckPlzzT8kELqxnpAsao8e4M//AkRIIDQowV2DEDQgPygk4rZ7/ANG WWHyEDrVwUI2gQUBYP6Dg+eanNDv7aaQECE9vrX1dwEQC4zSVWlnLbyBski0OvYkOZQC +nEzDgwOO5iBwHVY+sNZ+2kAfwWRZlUtI990NrQa7EZeX5pJSehMmbfMjIO2OBOoDhU/ POGUGeozKkQ5oCDf9xLL/Ca47Tli1ZkdeCOHfdjHpfESwm2zDWyAsNnbuCfq8Sj+jqVR qJ6w== 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=8vyawUyuSVTKDLaklHzgQURa+fePyLQPB2yxsyv9G1A=; b=ocLqLVXPFc4vf9qH7/tKOCCU/U/W3n4EnCs/4yQg5rPdnR4wzIYSDTkVWx7H0hPPo4 dVFf3aunR7eELKZHJ7POzZLVTgxAet0DbePOfmAn/WO4iP0GerdpZHgrB8bUS8tYjFCs yiVMdGMnjPy5/hcxw4DDkEhmSWZqizFIvhWjKwUTd0HNSHdlahvGVI0nONlufZZdYAaZ /9fGAvKO+Ng88YLHcpd/sT+TOVk8B/3HassKuK692vuDakgo/vqCJ2aTl+sED+XkNexG Wdl6gB7ibdYJdK4VjCeWVh5CK4RfT76EOrqyI4gRm7kRZuSDiUjg22pN9RSXRFzc2P84 Ez2A== 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::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-pf0-x229.google.com (mail-pf0-x229.google.com. [2607:f8b0:400e:c00::229]) by mx.google.com with ESMTPS id p68si3038815pga.307.2017.05.22.12.08.09 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 May 2017 12:08:09 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::229 as permitted sender) client-ip=2607:f8b0:400e:c00::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:c00::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-pf0-x229.google.com with SMTP id n23so90826065pfb.2 for ; Mon, 22 May 2017 12:08:09 -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=8vyawUyuSVTKDLaklHzgQURa+fePyLQPB2yxsyv9G1A=; b=KI8Z32oCkzIBjsiznaUFrdA2eVJtCTTy0xhp1jSAVDau7YN8oOy5xHTqnLoc6RHg5d rcNGp9mZvfOHAhnfvzVypxVR5c3zjsIWme0Q2UAEYnVoDk+VupYtqAgsSV71/izmU7Fl wd0R5soU5Bs2Izxpz2+xgfplc2QtWMZwySgII= 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=8vyawUyuSVTKDLaklHzgQURa+fePyLQPB2yxsyv9G1A=; b=l0SiLe+1B0DE6tn/GEV0/KyT1No9G6wKV0II13a7gMOoNkwIPAjhn2YHAtiNnkM2Ft AB3AQDNvYMR2mXaVoU+ZQUja6i4s+9EqXPz+saigOjIaAnIvh08b6sNqsZkXSWLQgTv0 d9O9SX0imA3Gb+CaX4ezWY6Cqj+VYoegu8r2JagF8vGVPqtroBrWECFuK8mHqlGqGJiL OppHeaLUHEPKT9tvf9bfhOLUq+J7UlxwqEVstPQTo2b8GSyxgWbfNomAGvA8J7wARLPX 2segW8RvKKI4YjQW7FEO2Gk27njuThys3gpm6u4hnEUac62fMdK4HUapkIsSjMMQ5jdK F2VQ== X-Gm-Message-State: AODbwcC6QLlAbpOoZAsKlR0a860wTu3kOfKWnDhrKN8KvP/9KdsIVpUK KL1k+PF6T4weAsXBNYQ= X-Received: by 10.98.214.14 with SMTP id r14mr27202687pfg.156.1495480089109; Mon, 22 May 2017 12:08:09 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([2601:1c2:1002:83f0:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id o66sm24359871pga.64.2017.05.22.12.08.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 22 May 2017 12:08:08 -0700 (PDT) From: John Stultz To: Will Deacon , Kevin Brodsky Cc: John Stultz Subject: [DRAFT][PATCH] time: Fix CLOCK_MONOTONIC_RAW sub-nanosecond accounting Date: Mon, 22 May 2017 12:08:02 -0700 Message-Id: <1495480082-7167-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 8c1523d..7490452 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) { @@ -1977,7 +1977,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) @@ -1992,14 +1992,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;