From patchwork Thu Apr 2 03:34:38 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 46692 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-la0-f70.google.com (mail-la0-f70.google.com [209.85.215.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 1CDC7216D1 for ; Thu, 2 Apr 2015 03:35:44 +0000 (UTC) Received: by lajy8 with SMTP id y8sf15165399laj.3 for ; Wed, 01 Apr 2015 20:35:43 -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=M1Gmf3sI6T6rS8V5eHHHguztdpsVtatoQblAx1YA+J0=; b=D3Nq0FrKVaRCGBKdsxI6l3M/AuFFAiyNHfyEOw5wyUwC+rcOGxyUPJtXnSKXRi/ulA f6n3BFp6Sx6mv8tiaDZvX1H/V+BGWqSf0TIGN/J7tqZHdhF/xBH0Be/dy+47CiLECItM KJUxTrsilT3fZPxnHbCgrJFQc8n2xEhLgJ3QAsG5aGgWh4f7TVMrLH8qBnjQCNNSXtFa jiKwDnOCbFw6GfORQvRiniP/g/dEBVqzsYJOjCLAT0FGb5L8VvBkDPCQ3a3NP4SOPeqK WmDN8JHb1Kjo/FLcnH0156bnsHpAf63Q0K1CYmW5wWuaEwMBFsB1xZmk+yYiZVgRTj3C uoww== X-Gm-Message-State: ALoCoQkRkt251yVdwVuflM+TQKzLZ2nFCm7fLO2LJm6/CqQgUqaZLIrFmhCWPk3QFt4mAMaN4Oe6 X-Received: by 10.112.130.71 with SMTP id oc7mr6147033lbb.13.1427945743080; Wed, 01 Apr 2015 20:35:43 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.43.199 with SMTP id y7ls157889lal.63.gmail; Wed, 01 Apr 2015 20:35:42 -0700 (PDT) X-Received: by 10.112.14.106 with SMTP id o10mr37447073lbc.2.1427945742802; Wed, 01 Apr 2015 20:35:42 -0700 (PDT) Received: from mail-lb0-f173.google.com (mail-lb0-f173.google.com. [209.85.217.173]) by mx.google.com with ESMTPS id xv10si3113596lab.162.2015.04.01.20.35.42 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 Apr 2015 20:35:42 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.173 as permitted sender) client-ip=209.85.217.173; Received: by lbbug6 with SMTP id ug6so50493932lbb.3 for ; Wed, 01 Apr 2015 20:35:42 -0700 (PDT) X-Received: by 10.152.116.11 with SMTP id js11mr38792623lab.106.1427945742562; Wed, 01 Apr 2015 20:35:42 -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.112.57.201 with SMTP id k9csp1063432lbq; Wed, 1 Apr 2015 20:35:40 -0700 (PDT) X-Received: by 10.66.66.196 with SMTP id h4mr82030033pat.127.1427945709596; Wed, 01 Apr 2015 20:35:09 -0700 (PDT) Received: from mail-pa0-f54.google.com (mail-pa0-f54.google.com. [209.85.220.54]) by mx.google.com with ESMTPS id pg6si5470516pbb.183.2015.04.01.20.35.08 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 Apr 2015 20:35:09 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.54 as permitted sender) client-ip=209.85.220.54; Received: by pacgg7 with SMTP id gg7so71306855pac.0 for ; Wed, 01 Apr 2015 20:35:08 -0700 (PDT) X-Received: by 10.66.186.110 with SMTP id fj14mr11019506pac.17.1427945708688; Wed, 01 Apr 2015 20:35:08 -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 da10sm3572603pac.42.2015.04.01.20.35.07 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 01 Apr 2015 20:35:07 -0700 (PDT) From: John Stultz To: lkml Cc: Xunlei Pang , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , John Stultz Subject: [PATCH 18/21] time: rtc: Don't bother into rtc_resume() for the nonstop clocksource Date: Wed, 1 Apr 2015 20:34:38 -0700 Message-Id: <1427945681-29972-19-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1427945681-29972-1-git-send-email-john.stultz@linaro.org> References: <1427945681-29972-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=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.173 as permitted sender) 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: , From: Xunlei Pang If a system does not provide a persistent_clock(), the time will be updated on resume by rtc_resume(). With the addition of the non-stop clocksources for suspend timing, those systems set the time on resume in timekeeping_resume(), but may not provide a valid persistent_clock(). This results in the rtc_resume() logic thinking no one has set the time and it then will over-write the suspend time again, which is not necessary and only increases clock error. So, fix this for rtc_resume(). This patch also improves the name of persistent_clock_exist to make it more grammatical. Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Ingo Molnar Signed-off-by: Xunlei Pang Signed-off-by: John Stultz --- drivers/rtc/class.c | 4 +-- include/linux/timekeeping.h | 9 +++---- kernel/time/timekeeping.c | 66 +++++++++++++++++++++++++++++++++------------ 3 files changed, 54 insertions(+), 25 deletions(-) diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c index d40760a..c29ba7e 100644 --- a/drivers/rtc/class.c +++ b/drivers/rtc/class.c @@ -55,7 +55,7 @@ static int rtc_suspend(struct device *dev) struct timespec64 delta, delta_delta; int err; - if (has_persistent_clock()) + if (timekeeping_rtc_skipsuspend()) return 0; if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) @@ -102,7 +102,7 @@ static int rtc_resume(struct device *dev) struct timespec64 sleep_time; int err; - if (has_persistent_clock()) + if (timekeeping_rtc_skipresume()) return 0; rtc_hctosys_ret = -ENODEV; diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h index 7a2369d..99176af 100644 --- a/include/linux/timekeeping.h +++ b/include/linux/timekeeping.h @@ -248,6 +248,9 @@ static inline void timekeeping_clocktai(struct timespec *ts) /* * RTC specific */ +extern bool timekeeping_rtc_skipsuspend(void); +extern bool timekeeping_rtc_skipresume(void); + extern void timekeeping_inject_sleeptime64(struct timespec64 *delta); /* @@ -259,14 +262,8 @@ extern void getnstime_raw_and_real(struct timespec *ts_raw, /* * Persistent clock related interfaces */ -extern bool persistent_clock_exist; extern int persistent_clock_is_local; -static inline bool has_persistent_clock(void) -{ - return persistent_clock_exist; -} - extern void read_persistent_clock(struct timespec *ts); extern void read_persistent_clock64(struct timespec64 *ts); extern void read_boot_clock(struct timespec *ts); diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 6dafcea..fe91e8e 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -64,9 +64,6 @@ static struct tk_fast tk_fast_raw ____cacheline_aligned; /* flag for if timekeeping is suspended */ int __read_mostly timekeeping_suspended; -/* Flag for if there is a persistent clock on this platform */ -bool __read_mostly persistent_clock_exist = false; - static inline void tk_normalize_xtime(struct timekeeper *tk) { while (tk->tkr_mono.xtime_nsec >= ((u64)NSEC_PER_SEC << tk->tkr_mono.shift)) { @@ -1204,6 +1201,12 @@ void __weak read_boot_clock64(struct timespec64 *ts64) *ts64 = timespec_to_timespec64(ts); } +/* Flag for if timekeeping_resume() has injected sleeptime */ +static bool sleeptime_injected; + +/* Flag for if there is a persistent clock on this platform */ +static bool persistent_clock_exists; + /* * timekeeping_init - Initializes the clocksource and common timekeeping values */ @@ -1221,7 +1224,7 @@ void __init timekeeping_init(void) now.tv_sec = 0; now.tv_nsec = 0; } else if (now.tv_sec || now.tv_nsec) - persistent_clock_exist = true; + persistent_clock_exists = true; read_boot_clock64(&boot); if (!timespec64_valid_strict(&boot)) { @@ -1282,11 +1285,47 @@ static void __timekeeping_inject_sleeptime(struct timekeeper *tk, #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_RTC_HCTOSYS_DEVICE) /** + * We have three kinds of time sources to use for sleep time + * injection, the preference order is: + * 1) non-stop clocksource + * 2) persistent clock (ie: RTC accessible when irqs are off) + * 3) RTC + * + * 1) and 2) are used by timekeeping, 3) by RTC subsystem. + * If system has neither 1) nor 2), 3) will be used finally. + * + * + * If timekeeping has injected sleeptime via either 1) or 2), + * 3) becomes needless, so in this case we don't need to call + * rtc_resume(), and this is what timekeeping_rtc_skipresume() + * means. + */ +bool timekeeping_rtc_skipresume(void) +{ + return sleeptime_injected; +} + +/** + * 1) can be determined whether to use or not only when doing + * timekeeping_resume() which is invoked after rtc_suspend(), + * so we can't skip rtc_suspend() surely if system has 1). + * + * But if system has 2), 2) will definitely be used, so in this + * case we don't need to call rtc_suspend(), and this is what + * timekeeping_rtc_skipsuspend() means. + */ +bool timekeeping_rtc_skipsuspend(void) +{ + return persistent_clock_exists; +} + +/** * timekeeping_inject_sleeptime64 - Adds suspend interval to timeekeeping values * @delta: pointer to a timespec64 delta value * * This hook is for architectures that cannot support read_persistent_clock64 * because their RTC/persistent clock is only accessible when irqs are enabled. + * and also don't have an effective nonstop clocksource. * * This function should only be called by rtc_resume(), and allows * a suspend offset to be injected into the timekeeping values. @@ -1296,13 +1335,6 @@ void timekeeping_inject_sleeptime64(struct timespec64 *delta) struct timekeeper *tk = &tk_core.timekeeper; unsigned long flags; - /* - * Make sure we don't set the clock twice, as timekeeping_resume() - * already did it - */ - if (has_persistent_clock()) - return; - raw_spin_lock_irqsave(&timekeeper_lock, flags); write_seqcount_begin(&tk_core.seq); @@ -1334,8 +1366,8 @@ void timekeeping_resume(void) unsigned long flags; struct timespec64 ts_new, ts_delta; cycle_t cycle_now, cycle_delta; - bool suspendtime_found = false; + sleeptime_injected = false; read_persistent_clock64(&ts_new); clockevents_resume(); @@ -1381,13 +1413,13 @@ void timekeeping_resume(void) nsec += ((u64) cycle_delta * mult) >> shift; ts_delta = ns_to_timespec64(nsec); - suspendtime_found = true; + sleeptime_injected = true; } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) { ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time); - suspendtime_found = true; + sleeptime_injected = true; } - if (suspendtime_found) + if (sleeptime_injected) __timekeeping_inject_sleeptime(tk, &ts_delta); /* Re-base the last cycle value */ @@ -1423,14 +1455,14 @@ int timekeeping_suspend(void) * value returned, update the persistent_clock_exists flag. */ if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec) - persistent_clock_exist = true; + persistent_clock_exists = true; raw_spin_lock_irqsave(&timekeeper_lock, flags); write_seqcount_begin(&tk_core.seq); timekeeping_forward_now(tk); timekeeping_suspended = 1; - if (has_persistent_clock()) { + if (persistent_clock_exists) { /* * To avoid drift caused by repeated suspend/resumes, * which each can add ~1 second drift error,