From patchwork Fri Mar 9 18:42:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 131210 Delivered-To: patches@linaro.org Received: by 10.46.66.2 with SMTP id p2csp1324130lja; Fri, 9 Mar 2018 10:42:58 -0800 (PST) X-Received: by 10.99.112.20 with SMTP id l20mr25617917pgc.412.1520620978108; Fri, 09 Mar 2018 10:42:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520620978; cv=none; d=google.com; s=arc-20160816; b=QdiWGb95wka1A4ZmDQCgKx6AYq8PAW2HpPkQbjR5+7mzWCglEkxByz/8E9iJ+rdfo5 2y1vwEPurqS2PZ3/NAupB8/5CLdwG2gqt9CWRyn4vC8HFXnr0j/HJul81YVBC6tLo513 jaImREtUEwXbXZblBS8RkgriURH/+fPXnUECkc7uWFhSgM/s6Ofr+YP7oScA9fNucQ5f XSpz7eNVphPVyHBA8odjBQ4kiDY0qD83NWuO1QX62j18NM7WP1Eh+S2qR0YGu/0z7IWl R8ckk3uDfYEtbsTGvvxyT4miZGdJTXMs+LpViR1/h2coFVMRGIAasFHQVLQNTGTc0UcJ 9QQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=dDhQrW8k/crEqc16SqOAjwwmtcmK3nmq6diyp2pWXoY=; b=gDMQp9rCXOFgWvCsDhxHuVwgGFih0Gq8lCnGlMz+RjCbcIOGk4qgPflzrQCOr3A9n8 YMSizvGuEfTBCj1SE0R9oCAObsxeIdYDHj9nSao8uR9fTlPOytJlAFI/jxR6hwmmY64l UgSQqlkN+HBiLs1Wjs2WN0veGxG57cuzhCiWlaUeX+XGdFjvx5CIzsCviZc3QtRN+zm+ cuCF8MLYIIAsNyzEs7lVd8UIZHibNb9H953OZmwl7wgpLJg8OW99ul1g6eFhfcdAY9HC o1m5rseOCF1E8t0oUY14M1Bzkb0+yCoeAlr4Cm7cZv2p5j7sgPAqHaZyJR8ZDRujlxF5 vz6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=R7sj9NBY; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 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-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id c1-v6sor723042plz.130.2018.03.09.10.42.57 for (Google Transport Security); Fri, 09 Mar 2018 10:42:58 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=R7sj9NBY; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=dDhQrW8k/crEqc16SqOAjwwmtcmK3nmq6diyp2pWXoY=; b=R7sj9NBYHct4p4Y64frwipMAeW42nCTIenz7Lt8V7z1ijjtLOJv8dJvT1js+NoNnM2 Mo2jNt889CUGOp0UAt38QCcAjdpfsAQNOht1Kuabzd0Kn7TXJMq368il4cs5pTwiYHqn xCr9PPE+By3RaeNL5FfPsu81LU0FGrOHdkJoY= 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:in-reply-to :references; bh=dDhQrW8k/crEqc16SqOAjwwmtcmK3nmq6diyp2pWXoY=; b=kEpJsr+0UTvc/ULxveYzDRDJKAZvvfnm5E+J7wNegik2qPonZn4Js/FG9XHWBqZL0+ tqaiLD5ZDBmsUEq/fvsLJuixbSAzYjSO0rerD6nfNSs0IUbG4x9nSFuRRFIoe6jH+MIk 6Lqz39hhsW6MYcPlTXt0sdTDTooQ/n4A0k7fP4tqIjg2KufZGW7ebn3NdYtC4Kb3fbJS O0BrFvkpkDDXJFgXdW5t1D6lOh0v/Od9Ch7YsXCkwgQYwapmouJVPKlfGAXf2zJI163c qwHtl9Re7f2HsgwrH0y/109HgpXWYq5CnCsH2WeblcvGVQWNZbSh0K2CY/I7S4X6H4tJ sJnw== X-Gm-Message-State: APf1xPAHYurPZmPDwGnYTVSELymzQ9Mahc0HbUM35JQimoL+ZTIWav90 7aiYWuVeCinHrbqPEuvo9DcjarwF X-Google-Smtp-Source: AG47ELuITQin7v0zJ2C1PyL4csXYe35oqq9LKAEMViD/hUi3TSpASr9Rk9qFSZ3u/n2B0zETsCH5GQ== X-Received: by 2002:a17:902:3041:: with SMTP id u59-v6mr28605532plb.115.1520620977605; Fri, 09 Mar 2018 10:42:57 -0800 (PST) Return-Path: Received: from localhost.localdomain ([2601:1c2:600:5100:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id y5sm4333162pfd.85.2018.03.09.10.42.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 09 Mar 2018 10:42:56 -0800 (PST) From: John Stultz To: lkml Cc: Miroslav Lichvar , Thomas Gleixner , Ingo Molnar , Richard Cochran , Prarit Bhargava , Stephen Boyd , John Stultz Subject: [PATCH 1/4] timekeeping: Don't align frequency adjustments to ticks Date: Fri, 9 Mar 2018 10:42:47 -0800 Message-Id: <1520620971-9567-2-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1520620971-9567-1-git-send-email-john.stultz@linaro.org> References: <1520620971-9567-1-git-send-email-john.stultz@linaro.org> From: Miroslav Lichvar When the timekeeping multiplier is changed, the NTP error is updated to correct the clock for the delay between the tick and the update of the clock. This error is corrected in later updates and the clock appears as if the frequency was changed exactly on the tick. Remove this correction to keep the point where the frequency is effectively changed at the time of the update. This removes a major source of the NTP error. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Miroslav Lichvar Cc: Richard Cochran Cc: Prarit Bhargava Cc: Stephen Boyd Signed-off-by: Miroslav Lichvar Signed-off-by: John Stultz --- kernel/time/timekeeping.c | 3 --- 1 file changed, 3 deletions(-) -- 2.7.4 diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index cd03317..c1a0ac1 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1860,8 +1860,6 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, * xtime_nsec_2 = xtime_nsec_1 - offset * Which simplfies to: * xtime_nsec -= offset - * - * XXX - TODO: Doc ntp_error calculation. */ if ((mult_adj > 0) && (tk->tkr_mono.mult + mult_adj < mult_adj)) { /* NTP adjustment caused clocksource mult overflow */ @@ -1872,7 +1870,6 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, tk->tkr_mono.mult += mult_adj; tk->xtime_interval += interval; tk->tkr_mono.xtime_nsec -= offset; - tk->ntp_error -= (interval - offset) << tk->ntp_error_shift; } /* From patchwork Fri Mar 9 18:42:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 131211 Delivered-To: patches@linaro.org Received: by 10.46.66.2 with SMTP id p2csp1324145lja; Fri, 9 Mar 2018 10:42:59 -0800 (PST) X-Received: by 2002:a17:902:1681:: with SMTP id h1-v6mr6300185plh.240.1520620979627; Fri, 09 Mar 2018 10:42:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520620979; cv=none; d=google.com; s=arc-20160816; b=JrhzaQdZ1X5hzEFvjiKCXQC27n436Qvq675n0lS75w3nbnb6uCRbXwemZSbq0XajBL NoqlNWztlqTsLA3ZuNem7yoGk0ETa5rDaz6hAkQpB0P/nPrGwpFKNC26lWi3zQ3a0cjz oJ4dh57AjZut9Fcdg1y+NnmtXjZbM5/HFcIfcdQzs+boaLCkTQXycXqzb1OPEoKQ2eEq AC1qeqRvdFLxnNtkaZuVy5LK3iolUSufvsfglyYRs4Y93vKo9YpOMg935ribfLPT0DZ+ 8712x+GzM7xtx4Ouqa2SJL6uuezW4KTzvLzhoBLs9b+PruU+8BuNwFSDUDAuwqdJR4au OB3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=Am4LZgLEV2ooVArVA5EbEeuKvgD9ylRsWQWuj6srB9k=; b=ZzGsaRaVHx44i1IQCscMMQknaipgsrFym4o1VV+hoTa3hc7l/LDAByJh9dgvjo/gqp vGbFYCQAWzRXDEH9PS4bKt4+fZLMHJC+yQ48MNcOxFxJ8xUBZjXC217wyDV0xcZf0Uvs 8zRVvIXO9zd1H57Ad30ZlJ1PHglhVE5cCZMtDFOs3dGSEn56tdB0fB6/efLd+FH8NfI7 aw5oGk7HQ/tAsxfilmReSqulcOgiHFU0bQ6Ja9IZ+34SXVgF7q4MG+XDLhvAEtlYOj8C iefB+rPJ4WYylxA6V1pq6+dGGfVBylDEt7GpxYbCQcCU+nOU5Th4PcTgE8G+M4+xjRUY sPKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=N4nPv0Dv; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 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-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id g3-v6sor823828pld.65.2018.03.09.10.42.59 for (Google Transport Security); Fri, 09 Mar 2018 10:42:59 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=N4nPv0Dv; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Am4LZgLEV2ooVArVA5EbEeuKvgD9ylRsWQWuj6srB9k=; b=N4nPv0DvpBa3VVnBUEFOsV+iKoY7pDoOpT6kpP9OTXJDECC7G/zjlurWoYf71Hl3xJ igZkuw3waQcnUvJ1Ch2yOATZqI+S1qD1i3QMuGMlxfYh9sD6zSdVps/iIcgEwnOraPK0 0le18Owa2JOnpbNjwtx449BcW4ambvYzVZMiM= 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:in-reply-to :references; bh=Am4LZgLEV2ooVArVA5EbEeuKvgD9ylRsWQWuj6srB9k=; b=buBXQAZ+fb7Fuv70dKgEtvAMW1gkEx7ZNIu+MC8vdxdfx9gq/Dtkf1vqqR/gxEBcJp S7bZ+Kc4E4Bgfh1BCYnaubX+0gzQD3nkeAiAvYfLjS4plUyLZI1YIYauAFZANd6WEATv mSumIhZnjN8NZwh71jI4JKQArbZ8QAR9DUJCw/W1KOsag4NS/w00Fm/nm6iVHN2aSeEA pT7e45abst3VaIhXdCGxY6q5sknluSkDjluKd0QMJPJRbp1AcIA5w/FjR7Yqkxswy0u2 kWgI8PkFq5ERkTjsz93Fravjb0gsaEkfB2lCT97Q96RlsYKoT3GX5VOe+cfgKfckrdBr bj3Q== X-Gm-Message-State: APf1xPAoa7NYeXzGnRIULbpLbgpVsYnXlF2X0C3A3S96MqvJzm5DsUDf C9ZpxJ2KUc/kE6grc0b8LvqxpbAn X-Google-Smtp-Source: AG47ELu9kPk5jXlIQGynHyshIWmoOVhEXSz7+F0WPAcC1kdqKsStXL8FfNUA36eauDuuCKd0Tk7WVw== X-Received: by 2002:a17:902:7404:: with SMTP id g4-v6mr28461366pll.235.1520620979106; Fri, 09 Mar 2018 10:42:59 -0800 (PST) Return-Path: Received: from localhost.localdomain ([2601:1c2:600:5100:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id y5sm4333162pfd.85.2018.03.09.10.42.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 09 Mar 2018 10:42:58 -0800 (PST) From: John Stultz To: lkml Cc: Miroslav Lichvar , Thomas Gleixner , Ingo Molnar , Richard Cochran , Prarit Bhargava , Stephen Boyd , John Stultz Subject: [PATCH 2/4] timekeeping: Determine multiplier directly from NTP tick length Date: Fri, 9 Mar 2018 10:42:48 -0800 Message-Id: <1520620971-9567-3-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1520620971-9567-1-git-send-email-john.stultz@linaro.org> References: <1520620971-9567-1-git-send-email-john.stultz@linaro.org> From: Miroslav Lichvar When the length of the NTP tick changes significantly, e.g. when an NTP/PTP application is correcting the initial offset of the clock, a large value may accumulate in the NTP error before the multiplier converges to the correct value. It may then take a very long time (hours or even days) before the error is corrected. This causes the clock to have an unstable frequency offset, which has a negative impact on the stability of synchronization with precise time sources (e.g. NTP/PTP using hardware timestamping or the PTP KVM clock). Use division to determine the correct multiplier directly from the NTP tick length and replace the iterative approach. This removes the last major source of the NTP error. The only remaining source is now limited resolution of the multiplier, which is corrected by adding 1 to the multiplier when the system clock is behind the NTP time. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Miroslav Lichvar Cc: Richard Cochran Cc: Prarit Bhargava Cc: Stephen Boyd Signed-off-by: Miroslav Lichvar Signed-off-by: John Stultz --- include/linux/timekeeper_internal.h | 2 + kernel/time/timekeeping.c | 138 ++++++++++++------------------------ 2 files changed, 49 insertions(+), 91 deletions(-) -- 2.7.4 diff --git a/include/linux/timekeeper_internal.h b/include/linux/timekeeper_internal.h index d315c3d..7acb953 100644 --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -117,6 +117,8 @@ struct timekeeper { s64 ntp_error; u32 ntp_error_shift; u32 ntp_err_mult; + /* Flag used to avoid updating NTP twice with same second */ + u32 skip_second_overflow; #ifdef CONFIG_DEBUG_TIMEKEEPING long last_warning; /* diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index c1a0ac1..e1176012 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -332,6 +332,7 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) tk->tkr_mono.mult = clock->mult; tk->tkr_raw.mult = clock->mult; tk->ntp_err_mult = 0; + tk->skip_second_overflow = 0; } /* Timekeeper helper functions. */ @@ -1799,20 +1800,19 @@ device_initcall(timekeeping_init_ops); */ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, s64 offset, - bool negative, - int adj_scale) + s32 mult_adj) { s64 interval = tk->cycle_interval; - s32 mult_adj = 1; - if (negative) { - mult_adj = -mult_adj; + if (mult_adj == 0) { + return; + } else if (mult_adj == -1) { interval = -interval; - offset = -offset; + offset = -offset; + } else if (mult_adj != 1) { + interval *= mult_adj; + offset *= mult_adj; } - mult_adj <<= adj_scale; - interval <<= adj_scale; - offset <<= adj_scale; /* * So the following can be confusing. @@ -1873,85 +1873,35 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, } /* - * Calculate the multiplier adjustment needed to match the frequency - * specified by NTP + * Adjust the timekeeper's multiplier to the correct frequency + * and also to reduce the accumulated error value. */ -static __always_inline void timekeeping_freqadjust(struct timekeeper *tk, - s64 offset) +static void timekeeping_adjust(struct timekeeper *tk, s64 offset) { - s64 interval = tk->cycle_interval; - s64 xinterval = tk->xtime_interval; - u32 base = tk->tkr_mono.clock->mult; - u32 max = tk->tkr_mono.clock->maxadj; - u32 cur_adj = tk->tkr_mono.mult; - s64 tick_error; - bool negative; - u32 adj_scale; - - /* Remove any current error adj from freq calculation */ - if (tk->ntp_err_mult) - xinterval -= tk->cycle_interval; - - tk->ntp_tick = ntp_tick_length(); - - /* Calculate current error per tick */ - tick_error = ntp_tick_length() >> tk->ntp_error_shift; - tick_error -= (xinterval + tk->xtime_remainder); - - /* Don't worry about correcting it if its small */ - if (likely((tick_error >= 0) && (tick_error <= interval))) - return; - - /* preserve the direction of correction */ - negative = (tick_error < 0); + u32 mult; - /* If any adjustment would pass the max, just return */ - if (negative && (cur_adj - 1) <= (base - max)) - return; - if (!negative && (cur_adj + 1) >= (base + max)) - return; /* - * Sort out the magnitude of the correction, but - * avoid making so large a correction that we go - * over the max adjustment. + * Determine the multiplier from the current NTP tick length. + * Avoid expensive division when the tick length doesn't change. */ - adj_scale = 0; - tick_error = abs(tick_error); - while (tick_error > interval) { - u32 adj = 1 << (adj_scale + 1); - - /* Check if adjustment gets us within 1 unit from the max */ - if (negative && (cur_adj - adj) <= (base - max)) - break; - if (!negative && (cur_adj + adj) >= (base + max)) - break; - - adj_scale++; - tick_error >>= 1; + if (likely(tk->ntp_tick == ntp_tick_length())) { + mult = tk->tkr_mono.mult - tk->ntp_err_mult; + } else { + tk->ntp_tick = ntp_tick_length(); + mult = div64_u64((tk->ntp_tick >> tk->ntp_error_shift) - + tk->xtime_remainder, tk->cycle_interval); } - /* scale the corrections */ - timekeeping_apply_adjustment(tk, offset, negative, adj_scale); -} + /* + * If the clock is behind the NTP time, increase the multiplier by 1 + * to catch up with it. If it's ahead and there was a remainder in the + * tick division, the clock will slow down. Otherwise it will stay + * ahead until the tick length changes to a non-divisible value. + */ + tk->ntp_err_mult = tk->ntp_error > 0 ? 1 : 0; + mult += tk->ntp_err_mult; -/* - * Adjust the timekeeper's multiplier to the correct frequency - * and also to reduce the accumulated error value. - */ -static void timekeeping_adjust(struct timekeeper *tk, s64 offset) -{ - /* Correct for the current frequency error */ - timekeeping_freqadjust(tk, offset); - - /* Next make a small adjustment to fix any cumulative error */ - if (!tk->ntp_err_mult && (tk->ntp_error > 0)) { - tk->ntp_err_mult = 1; - timekeeping_apply_adjustment(tk, offset, 0, 0); - } else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) { - /* Undo any existing error adjustment */ - timekeeping_apply_adjustment(tk, offset, 1, 0); - tk->ntp_err_mult = 0; - } + timekeeping_apply_adjustment(tk, offset, mult - tk->tkr_mono.mult); if (unlikely(tk->tkr_mono.clock->maxadj && (abs(tk->tkr_mono.mult - tk->tkr_mono.clock->mult) @@ -1968,18 +1918,15 @@ static void timekeeping_adjust(struct timekeeper *tk, s64 offset) * in the code above, its possible the required corrective factor to * xtime_nsec could cause it to underflow. * - * Now, since we already accumulated the second, cannot simply roll - * the accumulated second back, since the NTP subsystem has been - * notified via second_overflow. So instead we push xtime_nsec forward - * by the amount we underflowed, and add that amount into the error. - * - * We'll correct this error next time through this function, when - * xtime_nsec is not as small. + * Now, since we have already accumulated the second and the NTP + * subsystem has been notified via second_overflow(), we need to skip + * the next update. */ if (unlikely((s64)tk->tkr_mono.xtime_nsec < 0)) { - s64 neg = -(s64)tk->tkr_mono.xtime_nsec; - tk->tkr_mono.xtime_nsec = 0; - tk->ntp_error += neg << tk->ntp_error_shift; + tk->tkr_mono.xtime_nsec += (u64)NSEC_PER_SEC << + tk->tkr_mono.shift; + tk->xtime_sec--; + tk->skip_second_overflow = 1; } } @@ -2002,6 +1949,15 @@ static inline unsigned int accumulate_nsecs_to_secs(struct timekeeper *tk) tk->tkr_mono.xtime_nsec -= nsecps; tk->xtime_sec++; + /* + * Skip NTP update if this second was accumulated before, + * i.e. xtime_nsec underflowed in timekeeping_adjust() + */ + if (unlikely(tk->skip_second_overflow)) { + tk->skip_second_overflow = 0; + continue; + } + /* Figure out if its a leap sec and apply if needed */ leap = second_overflow(tk->xtime_sec); if (unlikely(leap)) { @@ -2118,7 +2074,7 @@ void update_wall_time(void) shift--; } - /* correct the clock when NTP error is too big */ + /* Adjust the multiplier to correct NTP error */ timekeeping_adjust(tk, offset); /* From patchwork Fri Mar 9 18:42:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 131212 Delivered-To: patches@linaro.org Received: by 10.46.66.2 with SMTP id p2csp1324180lja; Fri, 9 Mar 2018 10:43:01 -0800 (PST) X-Received: by 10.101.96.5 with SMTP id m5mr25217208pgu.374.1520620981747; Fri, 09 Mar 2018 10:43:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520620981; cv=none; d=google.com; s=arc-20160816; b=NlbbytGyFhDfUBGa9mqiqFmPhKQ+V2XB5JFWnUk8Oh/skC1hOXCtVcEmy356uZG+Fb 1xfxaDD1FqglUv38ECgdG1bDtLawJk/aseAcA7HhVzx6XP/m31Tu6zvr3gygDgFJm00x AkKGMpR1mgq9kNLLLoR0nd2a9+NEiUf/uXkCYsxU6nMRlt2CtxiwDhGxol0fjDEVuv/B IdIJSuH1L+lVZL6byxjOuMmSVvJdAHaCoTDpeQWR3Vp2SDv8lIhhNuR+k99hbTti4A2D P9O0WVbcyvtCJu/jUhgQOeyaIDVqBrjhaDBxyF1M683566ENLXRwqpUz0+DMcXUStqN/ 2gAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=SqKDls5c/gElYj4F2g4Nfq0lsuUBjbvBfKSRetlNOdQ=; b=n4y4lfs6Qr6mDjPINKyMkdAKkcgVTlVF3B295qT79WZ7aSoiWNaUbZT6BQUE00MYnr ks6ufzOOtoXep+GYVvSE/HHwwH9y2nllWBhBoyDM0jOQ1CCI140+VtvTwYwha7Hnx4qc t4P9lwqj10IKW+RvccITsPpjibnldhBD0aVK2wC/xUWga4gN99SWSQLD4eWJPMsxfdov 0S8dZhI8Q69iQuRdYSE0iuaWhf782lhm8QyRX77YYPu4PGMbejjcp4EeHEFFkDcSGvCs J7n057blcB+Nj6IbcXFhHQzuEWRV+yELnMW7WBm8ijBVb8RaXlsDFY/D6n0EUiZADpXA KXvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=C/mNSeCw; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 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-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id 92-v6sor776750pli.109.2018.03.09.10.43.01 for (Google Transport Security); Fri, 09 Mar 2018 10:43:01 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=C/mNSeCw; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=SqKDls5c/gElYj4F2g4Nfq0lsuUBjbvBfKSRetlNOdQ=; b=C/mNSeCwCVltJnOZge1aKOaPnELd4NJFR1ohwomdVy5F25RX8Y6iv/XA/93CFOCWUw XNWFqyp5T+2peJW9jcfESR4loevqDBl7JyCCPmSYGSKHW6Ga/gU35Dc3AHAnL4uM9BF9 DbqDCJEB2HFxRVOGKE+VJTv7Ex+sLfK5sB7yc= 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:in-reply-to :references; bh=SqKDls5c/gElYj4F2g4Nfq0lsuUBjbvBfKSRetlNOdQ=; b=CElt4LcV0/Iag9bdcWs6dQTch+/ZbGd8cYSJC+nAlyeOQjfwSy1G+8qN9qtqBE+Bbt 45CtRKZ/4k/Ru0EX30ynPky1aIlUT9gr8YmcNLd/ri6nmLQ82phbE3gbKStcp8OtJ+P4 1V1CT0NGgQ2U6L8JOW+6QkV/ixM6/joZk+Pohf0bIckhn9zGhSWTgpS57XETHeFTQdse rn+UE+8DeYQBbhZGfVWmdWkji3k2X+QhGK8ehoNecBlw/43FhIyC396soxfl6djRxC2p DWiK1mVWLYJWprCcoJHgs1/dQeroZiYSYXHDe8VfKoeJP+qawzILntGTe2TIIqKOhzOo Ilzw== X-Gm-Message-State: APf1xPDKM8gkLsBPr2Qm1k3LijQOnQ6lZuRSmRDblE6CbPvU0XX3JvXU sfSOgaJBlAFqWmrgY6eOP38G+B/0 X-Google-Smtp-Source: AG47ELsNgzbtFual4/IrEahkk3fJIYb6BOXPEybs0PKnJyEVxZ+g673J0ejmRnLYus1IZrUcr6cSXQ== X-Received: by 2002:a17:902:42e:: with SMTP id 43-v6mr29389070ple.186.1520620981186; Fri, 09 Mar 2018 10:43:01 -0800 (PST) Return-Path: Received: from localhost.localdomain ([2601:1c2:600:5100:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id y5sm4333162pfd.85.2018.03.09.10.42.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 09 Mar 2018 10:43:00 -0800 (PST) From: John Stultz To: lkml Cc: Arnd Bergmann , Thomas Gleixner , Ingo Molnar , Miroslav Lichvar , Richard Cochran , Prarit Bhargava , Stephen Boyd , John Stultz Subject: [PATCH 3/4] y2038: time: Introduce struct __kernel_old_timeval Date: Fri, 9 Mar 2018 10:42:49 -0800 Message-Id: <1520620971-9567-4-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1520620971-9567-1-git-send-email-john.stultz@linaro.org> References: <1520620971-9567-1-git-send-email-john.stultz@linaro.org> From: Arnd Bergmann Dealing with 'struct timeval' users in the y2038 series is a bit tricky: We have two definitions of timeval that are visible to user space, one comes from glibc (or some other C library), the other comes from linux/time.h. The kernel copy is what we want to be used for a number of structures defined by the kernel itself, e.g. elf_prstatus (used it core dumps), sysinfo and rusage (used in system calls). These generally tend to be used for passing time intervals rather than absolute (epoch-based) times, so they do not suffer from the y2038 overflow. Some of them could be changed to use 64-bit timestamps by creating new system calls, others like the core files cannot easily be changed. An application using these interfaces likely also uses gettimeofday() or other interfaces that use absolute times, and pass 'struct timeval' pointers directly into kernel interfaces, so glibc must redefine their timeval based on a 64-bit time_t when they introduce their y2038-safe interfaces. The only reasonable way forward I see is to remove the 'timeval' definition from the kernel's uapi headers, and change the interfaces that we do not want to (or cannot) duplicate for 64-bit times to use a new __kernel_old_timeval definition instead. This type should be avoided for all new interfaces (those can use 64-bit nanoseconds, or the 64-bit version of timespec instead), and should be used with great care when converting existing interfaces from timeval, to be sure they don't suffer from the y2038 overflow, and only with consensus for the particular user that using __kernel_old_timeval is better than moving to a 64-bit based interface. The structure name is intentionally chosen to not conflict with user space types, and to be ugly enough to discourage its use. Note that ioctl based interfaces that pass a bare 'timeval' pointer cannot change to '__kernel_old_timeval' because the user space source code refers to 'timeval' instead, and we don't want to modify the user space sources if possible. However, any application that relies on a structure to contain an embedded 'timeval' (e.g. by passing a pointer to the member into a function call that expects a timeval pointer) is broken when that structure gets converted to __kernel_old_timeval. I don't see any way around that, and we have to rely on the compiler to produce a warning or compile failure that will alert users when they recompile their sources against a new libc. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Miroslav Lichvar Cc: Richard Cochran Cc: Prarit Bhargava Cc: Stephen Boyd Signed-off-by: Arnd Bergmann Signed-off-by: John Stultz --- include/linux/time32.h | 1 + include/uapi/linux/time.h | 12 ++++++++++++ kernel/time/time.c | 12 ++++++++++++ 3 files changed, 25 insertions(+) -- 2.7.4 diff --git a/include/linux/time32.h b/include/linux/time32.h index 65b1de2..3febcaf 100644 --- a/include/linux/time32.h +++ b/include/linux/time32.h @@ -217,5 +217,6 @@ static inline s64 timeval_to_ns(const struct timeval *tv) * Returns the timeval representation of the nsec parameter. */ extern struct timeval ns_to_timeval(const s64 nsec); +extern struct __kernel_old_timeval ns_to_kernel_old_timeval(const s64 nsec); #endif diff --git a/include/uapi/linux/time.h b/include/uapi/linux/time.h index 53f8dd8..888da62 100644 --- a/include/uapi/linux/time.h +++ b/include/uapi/linux/time.h @@ -43,6 +43,18 @@ struct itimerval { }; /* + * legacy timeval structure, only embedded in structures that + * traditionally used 'timeval' to pass time intervals (not absolute + * times). Do not add new users. If user space fails to compile + * here, this is probably because it is not y2038 safe and needs to + * be changed to use another interface. + */ +struct __kernel_old_timeval { + __kernel_long_t tv_sec; /* seconds */ + __kernel_long_t tv_usec; /* seconds */ +}; + +/* * The IDs of the various system clocks (for POSIX.1b interval timers): */ #define CLOCK_REALTIME 0 diff --git a/kernel/time/time.c b/kernel/time/time.c index bd4e6c7..cbb3c71 100644 --- a/kernel/time/time.c +++ b/kernel/time/time.c @@ -488,6 +488,18 @@ struct timeval ns_to_timeval(const s64 nsec) } EXPORT_SYMBOL(ns_to_timeval); +struct __kernel_old_timeval ns_to_kernel_old_timeval(const s64 nsec) +{ + struct timespec64 ts = ns_to_timespec64(nsec); + struct __kernel_old_timeval tv; + + tv.tv_sec = ts.tv_sec; + tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000; + + return tv; +} +EXPORT_SYMBOL(ns_to_kernel_old_timeval); + /** * set_normalized_timespec - set timespec sec and nsec parts and normalize * From patchwork Fri Mar 9 18:42:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 131213 Delivered-To: patches@linaro.org Received: by 10.46.66.2 with SMTP id p2csp1324203lja; Fri, 9 Mar 2018 10:43:03 -0800 (PST) X-Received: by 10.101.98.205 with SMTP id m13mr24947321pgv.100.1520620983288; Fri, 09 Mar 2018 10:43:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520620983; cv=none; d=google.com; s=arc-20160816; b=Wwb5u3Y2tygcVpf1aYnXmwolIfNhOySDFikZTDF/zweN/xcFA8Yphfs4ZvSts2yb/f n8ZU4BnbirpYNVgQ2FN56ciWVyIAoMvqn89QUD4MmIDjpU04K30d8IAWirlCwDaGbBZ6 UIrojn+o9HrS4lnlHylR/a6fQyn8BuLN5xvo5nZLTb0ZrNKOBqZj0znG+vtNwgsmm4vd NGmVQ/tXCDC3q7L3Yc4WuooEfWJIu1XxaECtn/J1zvSgt0hL9kJiPA3Yz8ZGLHZJ717z oZoBglbAQpMtHfIsPS6ab4N4OI9ftglRq74j1ql4g7USHhp68YZaZMjiTjDjPBS2Qb9x mVTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=kK/wr9HIlYK3wMgDQeXGvMJaQgIQSbQanTu0A/BFDPQ=; b=cWiXAwRIa2VEBYMLzTXUVzy573Wrags+PLIlbfcerzA9GHAv+ktNwmkkJ5lfoVPlYl bYFALNq/Ec+/Epnk/eZyfkbtVzWW7s/b2YOvARLNEDo8bMk1XrLIbFItzeqMUfGaIeQ7 iQDSDGVjpWt0UPfZAHAHfdbYFrGGM5tDlA2BXIPUhjjhBI8gNXAevoLF5Co2E1kURd1o gsWEa9X5fLWFEgKQIaLtVwj72TYm61S7acOXScEqWXNgIShS1nQ1pOAdGLCqlFnkXahs pP8UZBZ4CFGDGEG6ujyY15aPIwolkvSUYXezgoXTeeovCQOoN5Ty1T4EDAystveHi2DN MBFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Yr7YUdkh; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 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-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id k11-v6sor764963pls.94.2018.03.09.10.43.03 for (Google Transport Security); Fri, 09 Mar 2018 10:43:03 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Yr7YUdkh; spf=pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=kK/wr9HIlYK3wMgDQeXGvMJaQgIQSbQanTu0A/BFDPQ=; b=Yr7YUdkhxqj0eHxfTxicf+tLgBLvDSiqj1V/0S6tmOJOeez3ZRPFGuLmIyUQDOtkJq LqYdO3GpynOGAU97kS5MbB9l7va5oD10E7Z/gWbRhWtzTBzofbfMx83S++Xpk7gaIbeE NvPKwgxIsuQXUxkv1JQqh2dZdvp8lEsCS6qZ4= 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:in-reply-to :references; bh=kK/wr9HIlYK3wMgDQeXGvMJaQgIQSbQanTu0A/BFDPQ=; b=Xv0lhNZ5VnaZcRofD3ZUaMuWvDIjW36nbRcswRXfLRzrRBIu5zTrc0aIhDS0sKIU/K g5xgkBNhQEnrLvfvFamx1xg5vfLmj2oikbzBB/Q560gmAg7mnCVwsPh8YPHSBYEYLSMl XGQ6GJ8H5eqvETgZ3iQuZpIWYh5+2V8NEmgMdri+y4x5gWDmya11i2okLwLvan3hYiRL 7co7w7oYakpuccEZWW/AjVU1rDPrfEMMUeTdj81Bel3QjiLXo/rHELRTbSJ6u7Ws4h97 q75BEb1b1bLHhOkZ/4bcuV/mxoBq+sPTk9Igp4QnvTCABHBAvervth7Mi+4MmWIpbhyy GSFw== X-Gm-Message-State: APf1xPBYbPqBbbEbiizNFnO4yT6ObH9pL4LVwi0eov8W7QB/s3yaw3gI Kqurr2y5nWMkdDm81I8FmNzUpsyA X-Google-Smtp-Source: AG47ELuC+ADm+mSj85N2Uxe8U7xZhMlIgy8HC6tsOntc6Fg0gV7vUSGekx5hsOjQNm2xdGUIZD67FA== X-Received: by 2002:a17:902:8f89:: with SMTP id z9-v6mr29117043plo.370.1520620982794; Fri, 09 Mar 2018 10:43:02 -0800 (PST) Return-Path: Received: from localhost.localdomain ([2601:1c2:600:5100:4e72:b9ff:fe99:466a]) by smtp.gmail.com with ESMTPSA id y5sm4333162pfd.85.2018.03.09.10.43.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 09 Mar 2018 10:43:01 -0800 (PST) From: John Stultz To: lkml Cc: Benjamin Gaignard , Arnd Bergmann , Thomas Gleixner , Ingo Molnar , x86@kernel.org, Miroslav Lichvar , Richard Cochran , Prarit Bhargava , Stephen Boyd , Alexandre Belloni , John Stultz Subject: [PATCH 4/4] x86: rtc: Stop using rtc deprecated functions Date: Fri, 9 Mar 2018 10:42:50 -0800 Message-Id: <1520620971-9567-5-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1520620971-9567-1-git-send-email-john.stultz@linaro.org> References: <1520620971-9567-1-git-send-email-john.stultz@linaro.org> From: Benjamin Gaignard rtc_time_to_tm() and rtc_tm_to_time() are deprecated because they rely on 32bits variables and that will make rtc break in y2038/2016. Stop using those two functions to safer 64bits ones. Cc: Arnd Bergmann Cc: Thomas Gleixner Cc: Ingo Molnar Cc: x86@kernel.org Cc: Miroslav Lichvar Cc: Richard Cochran Cc: Prarit Bhargava Cc: Stephen Boyd Cc: Alexandre Belloni Signed-off-by: Benjamin Gaignard Signed-off-by: John Stultz --- arch/x86/kernel/rtc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) -- 2.7.4 diff --git a/arch/x86/kernel/rtc.c b/arch/x86/kernel/rtc.c index 69ac9cb..f7b82ed 100644 --- a/arch/x86/kernel/rtc.c +++ b/arch/x86/kernel/rtc.c @@ -41,11 +41,11 @@ EXPORT_SYMBOL(rtc_lock); */ int mach_set_rtc_mmss(const struct timespec *now) { - unsigned long nowtime = now->tv_sec; + unsigned long long nowtime = now->tv_sec; struct rtc_time tm; int retval = 0; - rtc_time_to_tm(nowtime, &tm); + rtc_time64_to_tm(nowtime, &tm); if (!rtc_valid_tm(&tm)) { retval = mc146818_set_time(&tm); if (retval) @@ -53,7 +53,7 @@ int mach_set_rtc_mmss(const struct timespec *now) __func__, retval); } else { printk(KERN_ERR - "%s: Invalid RTC value: write of %lx to RTC failed\n", + "%s: Invalid RTC value: write of %llx to RTC failed\n", __func__, nowtime); retval = -EINVAL; }