From patchwork Sun Feb 15 13:09:43 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xunlei Pang X-Patchwork-Id: 439857 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-qg0-x239.google.com (mail-qg0-x239.google.com [IPv6:2607:f8b0:400d:c04::239]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id E8E5014027F for ; Mon, 16 Feb 2015 00:10:45 +1100 (AEDT) Received: by mail-qg0-f57.google.com with SMTP id j5sf6506809qga.2 for ; Sun, 15 Feb 2015 05:10:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20120806; h=mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:x-original-sender:x-original-authentication-results :reply-to:content-type:precedence:mailing-list:list-id:list-post :list-help:list-archive:sender:list-subscribe:list-unsubscribe; bh=jJ1DFpkdqfoMlMgx3kASY2Q8Ko6Mx1dm0XFhe56H9K4=; b=Mu3PTHm5cc3TJSLmeDDb9I9yapAKGDyCJckFTqKn/vLHBnnYG+PsxmyslPBQOtpGbd lesNaJe5mhRSKT3fMh/rLjY75TV9XEdgrTEGXF/rjO2aGnntAohtPlmQCoXw3qzYT/Ig 28waIxswwn1cfix5Q/ufhosAt171pFNRNHqFAyy2izAqkV+Nja2GWxnT2exmVAoswNoC C4PnNkWB1/1jO1lV2mzNYCt6Wxp12PX3fPfWuV/Jj+FCI2bydrb2TvKCQHv5J8zr5+Sg aqYVmVBxW2IAdqB/vm0sucqEB99Hqvjq2bWGwCxCnlZ3qFSemdemL2SyMyHlnEnPyW5l abbA== X-Received: by 10.50.49.2 with SMTP id q2mr318569ign.12.1424005842893; Sun, 15 Feb 2015 05:10:42 -0800 (PST) MIME-Version: 1.0 X-BeenThere: rtc-linux@googlegroups.com Received: by 10.107.161.139 with SMTP id k133ls228685ioe.20.gmail; Sun, 15 Feb 2015 05:10:42 -0800 (PST) X-Received: by 10.66.193.106 with SMTP id hn10mr16718211pac.48.1424005842499; Sun, 15 Feb 2015 05:10:42 -0800 (PST) Received: from m50-110.126.com (m50-110.126.com. [123.125.50.110]) by gmr-mx.google.com with ESMTP id z4si849293igl.1.2015.02.15.05.10.41 for ; Sun, 15 Feb 2015 05:10:42 -0800 (PST) Received-SPF: pass (google.com: domain of xlpang@126.com designates 123.125.50.110 as permitted sender) client-ip=123.125.50.110; Received: from localhost.localdomain (unknown [210.21.223.3]) by smtp4 (Coremail) with SMTP id jdKowABXINyomuBUw_mjCw--.246S3; Sun, 15 Feb 2015 21:10:10 +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: [rtc-linux] [PATCH v4 2/4] time: Fix a bug in timekeeping_suspend() with no persistent clock Date: Sun, 15 Feb 2015 21:09:43 +0800 Message-Id: <1424005785-17865-2-git-send-email-xlpang@126.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1424005785-17865-1-git-send-email-xlpang@126.com> References: <1424005785-17865-1-git-send-email-xlpang@126.com> X-CM-TRANSID: jdKowABXINyomuBUw_mjCw--.246S3 X-Coremail-Antispam: 1Uf129KBjvJXoWxXr43Cry7Kw43Aw4DXrW7twb_yoWruF4Up3 y2kr13Ka1DXF1a9rnFk3Zru3sIgws5Kw47A3sIk34xC347Xrs3Gr10k3yrtryq9ryfA3s8 Zr48ta45Ww4UCFUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jJqXdUUUUU= X-Originating-IP: [210.21.223.3] X-CM-SenderInfo: p0ost0bj6rjloofrz/1tbiJxKhv01sAb9K6QAAsX X-Original-Sender: xlpang@126.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of xlpang@126.com designates 123.125.50.110 as permitted sender) smtp.mail=xlpang@126.com; dkim=pass header.i=@126.com; dmarc=pass (p=NONE dis=NONE) header.from=126.com Reply-To: rtc-linux@googlegroups.com Precedence: list Mailing-list: list rtc-linux@googlegroups.com; contact rtc-linux+owners@googlegroups.com List-ID: X-Google-Group-Id: 712029733259 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 d78a528..ec6ee9b 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 @@ -1231,8 +1231,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; } @@ -1264,14 +1265,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); @@ -1279,24 +1281,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);