From patchwork Mon Mar 9 06:27:49 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xunlei Pang X-Patchwork-Id: 45527 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f198.google.com (mail-lb0-f198.google.com [209.85.217.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id BB321214AE for ; Mon, 9 Mar 2015 06:30:24 +0000 (UTC) Received: by lbvn10 with SMTP id n10sf10763706lbv.1 for ; Sun, 08 Mar 2015 23:30:23 -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:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=jGQqWLtOfHz17lrnFQHzDo5BPik1b5Hkd+6IVB10YOA=; b=TqHtEzx6OZurRg+eyBrjIKfJk+G24gc2tBz7pQAv5ZEt+U2V1Bi+A4LrpRgZMl7hI5 JfvF7BwmHhw1wrQZddmwSiVZlD8/YOEdhJyyZ8cF78sWxeFrjMA/hBFMN3voAJDMP6I4 bcZcjJmiUmWj31wJNWXAu5pHvNxjhMIF83Cjl+NICpGbUMp6VKjjxSs3SwX0LlF/5WFv 4/cj6zOGlSJP/n7eqL4jZyRHUkUTxrv6o8oCe68WwBpdE5cuwHzP05I7EmIWhIEE9nAG 08UeiP0LohGSflyAS9EU1HsBHuBD/rvSXj6OFei3/iZJ4TAPs041Rwr/mk3pHJrOLGXx rxHw== X-Gm-Message-State: ALoCoQnMekaZuazWq6LDemCQMMJu1bnNJ53kP/zXyesOQUlOk1nTtbSbagdYEVpp6kUOswBWwKfH X-Received: by 10.112.199.69 with SMTP id ji5mr3617812lbc.0.1425882622996; Sun, 08 Mar 2015 23:30:22 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.21.130 with SMTP id v2ls219275lae.98.gmail; Sun, 08 Mar 2015 23:30:22 -0700 (PDT) X-Received: by 10.112.130.39 with SMTP id ob7mr24810365lbb.32.1425882622786; Sun, 08 Mar 2015 23:30:22 -0700 (PDT) Received: from mail-la0-x235.google.com (mail-la0-x235.google.com. [2a00:1450:4010:c03::235]) by mx.google.com with ESMTPS id t9si13426079laz.113.2015.03.08.23.30.22 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 08 Mar 2015 23:30:22 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c03::235 as permitted sender) client-ip=2a00:1450:4010:c03::235; Received: by labgd6 with SMTP id gd6so21703903lab.6 for ; Sun, 08 Mar 2015 23:30:22 -0700 (PDT) X-Received: by 10.152.206.70 with SMTP id lm6mr24477191lac.35.1425882622364; Sun, 08 Mar 2015 23:30:22 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.35.133 with SMTP id h5csp1282255lbj; Sun, 8 Mar 2015 23:30:20 -0700 (PDT) X-Received: by 10.70.0.11 with SMTP id 11mr20359451pda.32.1425882619051; Sun, 08 Mar 2015 23:30:19 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id fu7si10425782pdb.48.2015.03.08.23.30.17; Sun, 08 Mar 2015 23:30:19 -0700 (PDT) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752208AbbCIGaL (ORCPT + 28 others); Mon, 9 Mar 2015 02:30:11 -0400 Received: from m50-111.126.com ([123.125.50.111]:57223 "EHLO m50-111.126.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752089AbbCIGaH (ORCPT ); Mon, 9 Mar 2015 02:30:07 -0400 Received: from localhost.localdomain (unknown [210.21.223.3]) by smtp5 (Coremail) with SMTP id jtKowAD3dKpwPf1UBXZyAA--.189S3; Mon, 09 Mar 2015 14:28:14 +0800 (CST) From: Xunlei Pang To: linux-kernel@vger.kernel.org Cc: rtc-linux@googlegroups.com, Thomas Gleixner , Alessandro Zummo , John Stultz , Arnd Bergmann , Peter Zijlstra , Xunlei Pang Subject: [PATCH v5 2/4] time: Fix a bug in timekeeping_suspend() with no persistent clock Date: Mon, 9 Mar 2015 14:27:49 +0800 Message-Id: <1425882471-5591-2-git-send-email-xlpang@126.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1425882471-5591-1-git-send-email-xlpang@126.com> References: <1425882471-5591-1-git-send-email-xlpang@126.com> X-CM-TRANSID: jtKowAD3dKpwPf1UBXZyAA--.189S3 X-Coremail-Antispam: 1Uf129KBjvJXoWxXr43Cry7Kw43Aw4DXrW7twb_yoWruF48p3 y2kr13Ka1DXF1a9rnFk3Zru3s0gws5Kw47A3sIk34xC347Xrs7Gr10k3yrtryq9ryfA3s8 Zr48ta45Ww4UCFUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jJqXdUUUUU= X-Originating-IP: [210.21.223.3] X-CM-SenderInfo: p0ost0bj6rjloofrz/1tbijA+3v1GfUQ0j8wAAsJ Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Original-Sender: xlpang@126.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c03::235 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=neutral (body hash did not verify) header.i=@; dmarc=fail (p=NONE dis=NONE) header.from=126.com Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Xunlei Pang When there's no persistent clock, normally timekeeping_suspend_time should always be zero, but this can break in timekeeping_suspend(). At T1, there was a system suspend, so old_delta was assigned T1. After some time, one time adjustment happened, and xtime got the value of T1-dt(0s --- kernel/time/timekeeping.c | 50 ++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 49b1643..2aae419 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1100,8 +1100,8 @@ void __init timekeeping_init(void) raw_spin_unlock_irqrestore(&timekeeper_lock, flags); } -/* time in seconds when suspend began */ -static struct timespec64 timekeeping_suspend_time; +/* time in seconds when suspend began for persistent clock */ +static struct timespec64 persistent_clock_suspendtime; /** * __timekeeping_inject_sleeptime - Internal function to add sleep interval @@ -1229,8 +1229,9 @@ static void timekeeping_resume(void) ts_delta = ns_to_timespec64(nsec); suspendtime_found = true; - } else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) { - ts_delta = timespec64_sub(ts_new, timekeeping_suspend_time); + } else if (timespec64_compare(&ts_new, + &persistent_clock_suspendtime) > 0) { + ts_delta = timespec64_sub(ts_new, persistent_clock_suspendtime); suspendtime_found = true; } @@ -1262,14 +1263,15 @@ static int timekeeping_suspend(void) struct timespec tmp; read_persistent_clock(&tmp); - timekeeping_suspend_time = timespec_to_timespec64(tmp); + persistent_clock_suspendtime = timespec_to_timespec64(tmp); /* * On some systems the persistent_clock can not be detected at * timekeeping_init by its return value, so if we see a valid * value returned, update the persistent_clock_exists flag. */ - if (timekeeping_suspend_time.tv_sec || timekeeping_suspend_time.tv_nsec) + if (persistent_clock_suspendtime.tv_sec || + persistent_clock_suspendtime.tv_nsec) persistent_clock_exist = true; raw_spin_lock_irqsave(&timekeeper_lock, flags); @@ -1277,24 +1279,28 @@ static int timekeeping_suspend(void) timekeeping_forward_now(tk); timekeeping_suspended = 1; - /* - * To avoid drift caused by repeated suspend/resumes, - * which each can add ~1 second drift error, - * try to compensate so the difference in system time - * and persistent_clock time stays close to constant. - */ - delta = timespec64_sub(tk_xtime(tk), timekeeping_suspend_time); - delta_delta = timespec64_sub(delta, old_delta); - if (abs(delta_delta.tv_sec) >= 2) { + if (has_persistent_clock()) { /* - * if delta_delta is too large, assume time correction - * has occured and set old_delta to the current delta. + * To avoid drift caused by repeated suspend/resumes, + * which each can add ~1 second drift error, + * try to compensate so the difference in system time + * and persistent_clock time stays close to constant. */ - old_delta = delta; - } else { - /* Otherwise try to adjust old_system to compensate */ - timekeeping_suspend_time = - timespec64_add(timekeeping_suspend_time, delta_delta); + delta = timespec64_sub(tk_xtime(tk), + persistent_clock_suspendtime); + delta_delta = timespec64_sub(delta, old_delta); + if (abs(delta_delta.tv_sec) >= 2) { + /* + * if delta_delta is too large, assume time correction + * has occurred and set old_delta to the current delta. + */ + old_delta = delta; + } else { + /* Otherwise try to adjust old_system to compensate */ + persistent_clock_suspendtime = + timespec64_add(persistent_clock_suspendtime, + delta_delta); + } } timekeeping_update(tk, TK_MIRROR);