From patchwork Thu Apr 24 23:04:36 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 29026 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qg0-f70.google.com (mail-qg0-f70.google.com [209.85.192.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id DBEFC2036A for ; Thu, 24 Apr 2014 23:04:50 +0000 (UTC) Received: by mail-qg0-f70.google.com with SMTP id i50sf5220914qgf.5 for ; Thu, 24 Apr 2014 16:04:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=gYRso4oPcTSFZnMRygv9iLV30Z/gnlmOfjtRvKxKSdw=; b=iAJV7dfsZQ0/r+RPEpoY2NjZJ1XetuhPAOLo8gEz73MgcrQ9+9Dsy7I1kaYJDz2IzQ Y4NLOQswRSewmkBkUxnJZMkGQrcbZZKKqXl47yJXJzp20N54jpA0lq82pNwriKNicPdU SC2H6vKwXT9J7RauJkjZIAUMA8yxdL0whQ6dpPosiSRcnhQkAyoDeskZUY93X6QCGExz E5YWsJifubjBvN5oc+mFMSDVpHkTEUbg1K4kLf6RuQATEk85gM5Q+GKKbRO0WOFHVFXI PmMEiz8ebJFfgGd39f9a4ttNPDDbVJ6SioWB8vgNF3Vnyze3D38fkVi1/iJRexQkpOUd jwVg== X-Gm-Message-State: ALoCoQmE5PNdBCRPwn621zpbIYEVv92iBMjchTFEACAKgH4ToSnEA+Wl06ForVSL786m2XA3hu8X X-Received: by 10.58.59.104 with SMTP id y8mr2625882veq.18.1398380690697; Thu, 24 Apr 2014 16:04:50 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.19.115 with SMTP id 106ls1336785qgg.75.gmail; Thu, 24 Apr 2014 16:04:50 -0700 (PDT) X-Received: by 10.58.126.4 with SMTP id mu4mr3451889veb.0.1398380690588; Thu, 24 Apr 2014 16:04:50 -0700 (PDT) Received: from mail-vc0-f177.google.com (mail-vc0-f177.google.com [209.85.220.177]) by mx.google.com with ESMTPS id sh5si1287912vdc.104.2014.04.24.16.04.50 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 24 Apr 2014 16:04:50 -0700 (PDT) Received-SPF: none (google.com: patch+caf_=patchwork-forward=linaro.org@linaro.org does not designate permitted sender hosts) client-ip=209.85.220.177; Received: by mail-vc0-f177.google.com with SMTP id if17so3790233vcb.8 for ; Thu, 24 Apr 2014 16:04:50 -0700 (PDT) X-Received: by 10.58.202.106 with SMTP id kh10mr2882178vec.31.1398380690487; Thu, 24 Apr 2014 16:04:50 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.221.72 with SMTP id ib8csp52709vcb; Thu, 24 Apr 2014 16:04:50 -0700 (PDT) X-Received: by 10.68.240.5 with SMTP id vw5mr3199937pbc.113.1398380689650; Thu, 24 Apr 2014 16:04:49 -0700 (PDT) Received: from mail-pa0-f45.google.com (mail-pa0-f45.google.com [209.85.220.45]) by mx.google.com with ESMTPS id rj9si1002497pbc.289.2014.04.24.16.04.49 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 24 Apr 2014 16:04:49 -0700 (PDT) Received-SPF: none (google.com: john.stultz@linaro.org does not designate permitted sender hosts) client-ip=209.85.220.45; Received: by mail-pa0-f45.google.com with SMTP id kq14so1797138pab.18 for ; Thu, 24 Apr 2014 16:04:49 -0700 (PDT) X-Received: by 10.68.226.197 with SMTP id ru5mr3263181pbc.77.1398380689287; Thu, 24 Apr 2014 16:04:49 -0700 (PDT) Received: from localhost.localdomain (c-67-170-153-23.hsd1.or.comcast.net. [67.170.153.23]) by mx.google.com with ESMTPSA id de5sm11437842pbc.66.2014.04.24.16.04.48 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 24 Apr 2014 16:04:48 -0700 (PDT) From: John Stultz To: LKML Cc: John Stultz , Miroslav Lichvar , Richard Cochran , Prarit Bhargava Subject: [PATCH 2/3] [RFC] timekeeping: Use cached ntp_tick_length when accumulating error Date: Thu, 24 Apr 2014 16:04:36 -0700 Message-Id: <1398380677-8684-3-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1398380677-8684-1-git-send-email-john.stultz@linaro.org> References: <1398380677-8684-1-git-send-email-john.stultz@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: john.stultz@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: patch+caf_=patchwork-forward=linaro.org@linaro.org does not designate permitted sender hosts) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , By caching the ntp_tick_length() when we correct the frequency error, and then using that cached value to accumulate error, we avoid large initial errors when the tick length is changed. This makes convergence happen much faster in the simulator, since the initial error doesn't have to be slowly whittled away. This initially seems like an accounting error, but Miroslav pointed out that ntp_tick_length() can change mid-tick, so when we apply it in the error accumulation, we are applying any recent change to the entire tick. This approach chooses to apply changes in the ntp_tick_length() only to the next tick, which allows us to calculate the freq correction before using the new tick length, which avoids accummulating error. Credit to Miroslav for pointing this out and providing the original patch this functionality has been pulled out from, along with the rational. Cc: Miroslav Lichvar Cc: Richard Cochran Cc: Prarit Bhargava Reported-by: Miroslav Lichvar Signed-off-by: John Stultz --- include/linux/timekeeper_internal.h | 7 +++++++ kernel/time/timekeeping.c | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/linux/timekeeper_internal.h b/include/linux/timekeeper_internal.h index c1825eb..83fd40f 100644 --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -34,6 +34,13 @@ struct timekeeper { /* Clock shifted nano seconds */ u64 xtime_nsec; + /* 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 + * mid-tick, and we don't want to apply that new value to + * the tick in progress. + */ + u64 ntp_tick; /* Difference between accumulated time and NTP time in ntp * shifted nano seconds. */ s64 ntp_error; diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 9aa8ccf..a130d00 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -148,6 +148,7 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) * to counteract clock drifting. */ tk->mult = clock->mult; + tk->ntp_tick = ntpinterval << tk->ntp_error_shift; } /* Timekeeper helper functions. */ @@ -1138,6 +1139,7 @@ static __always_inline void timekeeping_freqadjust(struct timekeeper *tk, bool negative; u32 adj; + tk->ntp_tick = ntp_tick_length(); /* Calculate current error per tick */ tick_error = ntp_tick_length() >> tk->ntp_error_shift; tick_error -= (tk->xtime_interval + tk->xtime_remainder); @@ -1292,7 +1294,7 @@ static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset, tk->raw_time.tv_nsec = raw_nsecs; /* Accumulate error between NTP and clock interval */ - tk->ntp_error += ntp_tick_length() << shift; + tk->ntp_error += tk->ntp_tick << shift; tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) << (tk->ntp_error_shift + shift);