Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2138230imm; Thu, 19 Jul 2018 13:58:52 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcFKmZumHGUeeMeos8qQXos0vcD6jX2CwHKwwrgB1VV5BgZXj7lYjFPwjhStX4NqfiPZAwb X-Received: by 2002:a17:902:28a4:: with SMTP id f33-v6mr2051470plb.297.1532033932797; Thu, 19 Jul 2018 13:58:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532033932; cv=none; d=google.com; s=arc-20160816; b=q007IANVK4bHvZZknczE++ZpIFrLIaIr/UCqG2gZ3PfCs7yFEqToh1vAnbtrCGpfD7 zPCv6Zf6Nwwf+tjPqmkHSjr5w4sDkcKGKubXM0PFHAgh5LD0V51gHYwkEd0jBielIUVx ijbhkoAzWsQ9PKKIYo5XMYSlqxWRCtam/WSECEc7IFiq4wjFwl4AsSc6Jw20yPnVzRqQ 9B2b2mH3ZxPmGT2hpSk0MEVfa1mNz4OjzfNgFZIJbE03Whdo2yn8Yf5Y6GDFxlRg6zjz 3V33cFDb9Ho93yx/KbvefO6YvkZ7H+OKG3yT0KTYavJphAJyETTbpIH+K438lVhGa0io fYfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:to:from:dkim-signature:arc-authentication-results; bh=sihJJRcPkgVcee/j7Al8pfHTdyt8UqPwoKy0rRGL4Qw=; b=Agy8VQuGmA8Y4TScvGI4vcEEzjPWRV26EAlbZ7Rkhl8Keq+1nVLUirGukJJTcwSgOj pdpekgYr9FNKkZ+kHiaYFCioGUHZ2jejeFt6VfviH02WnoqKsdQ8SLZbmYTGeBpCeocR JzKUVGf7jHw3TNoQgQ1AKNdAbbJYP+kve3oiEDKJQ7568ZCG3rodAcyEGFZKpCRhdlAF AVl5F0RoiFur4ApwW1EPotPts4kvfUpx85JaBJqRzimsRId163mygJ46ZxdNs7YeozqP nIWtRbdzd54VAyHtGJIOI7iDMlczuz4qlZ7F7H73VHrDOqQ63/kAPmd0v1boU4hwye15 0/JQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b="x0a/JSGQ"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a24-v6si142920pgh.357.2018.07.19.13.58.38; Thu, 19 Jul 2018 13:58:52 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b="x0a/JSGQ"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731479AbeGSVma (ORCPT + 99 others); Thu, 19 Jul 2018 17:42:30 -0400 Received: from aserp2130.oracle.com ([141.146.126.79]:55850 "EHLO aserp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729209AbeGSVma (ORCPT ); Thu, 19 Jul 2018 17:42:30 -0400 Received: from pps.filterd (aserp2130.oracle.com [127.0.0.1]) by aserp2130.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w6JKsKeE185995; Thu, 19 Jul 2018 20:56:41 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : subject : date : message-id : in-reply-to : references; s=corp-2018-07-02; bh=sihJJRcPkgVcee/j7Al8pfHTdyt8UqPwoKy0rRGL4Qw=; b=x0a/JSGQZ7L83pYPj50FeUKhKx8aAwtdvCehPTCqdX4Qx8ujmLSXYpF98zVJycZVPCe6 pYdY8zsztCHwVzmc57EnMclQGW8oUcx7gI2JUdFoR0WEFPYrRIjcpKMsZKvPdtVu9Yyi P1qOeBRlVgqqBtcEMxFMJbiGAP6TB2HUZAGc/s3ugw29kG9B2iLhUZsFUI5qZdnlRkjn ArPwed9w99+Sdq1dQ/TLGUTlFPz9m5yk84aDAWSADU02romcbkD46H3jCV/F1J3RNlPB +6yMaCwNrY5Qsuog2U4sTaMDEmFLTkeSGiO2/qxw4FDJC+cI+27X0mhvfwg1zhsk9PbA ZA== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by aserp2130.oracle.com with ESMTP id 2k7a3tca6t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 Jul 2018 20:56:41 +0000 Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w6JKue7Z010751 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 19 Jul 2018 20:56:40 GMT Received: from abhmp0012.oracle.com (abhmp0012.oracle.com [141.146.116.18]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w6JKueHR003738; Thu, 19 Jul 2018 20:56:40 GMT Received: from localhost.localdomain (/73.69.118.222) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 19 Jul 2018 20:56:39 +0000 From: Pavel Tatashin To: steven.sistare@oracle.com, daniel.m.jordan@oracle.com, linux@armlinux.org.uk, schwidefsky@de.ibm.com, heiko.carstens@de.ibm.com, john.stultz@linaro.org, sboyd@codeaurora.org, x86@kernel.org, linux-kernel@vger.kernel.org, mingo@redhat.com, tglx@linutronix.de, hpa@zytor.com, douly.fnst@cn.fujitsu.com, peterz@infradead.org, prarit@redhat.com, feng.tang@intel.com, pmladek@suse.com, gnomes@lxorguk.ukuu.org.uk, linux-s390@vger.kernel.org, pasha.tatashin@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, pbonzini@redhat.com Subject: [PATCH v15 15/26] time: replace read_boot_clock64() with read_persistent_wall_and_boot_offset() Date: Thu, 19 Jul 2018 16:55:34 -0400 Message-Id: <20180719205545.16512-16-pasha.tatashin@oracle.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180719205545.16512-1-pasha.tatashin@oracle.com> References: <20180719205545.16512-1-pasha.tatashin@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8959 signatures=668706 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=621 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1806210000 definitions=main-1807190218 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If architecture does not support exact boot time, it is challenging to estimate boot time without having a reference to the current persistent clock value. Yet, it cannot read the persistent clock time again, because this may lead to math discrepancies with the caller of read_boot_clock64() who have read the persistent clock at a different time. This is why it is better to provide two values simultaneously: the persistent clock value, and the boot time. Replace read_boot_clock64() with: read_persistent_wall_and_boot_offset(wall_time, boot_offset) Where wall_time is returned by read_persistent_clock() And boot_offset is wall_time - boot time, which defaults to 0. Signed-off-by: Pavel Tatashin --- include/linux/timekeeping.h | 3 +- kernel/time/timekeeping.c | 59 +++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h index 86bc2026efce..686bc27acef0 100644 --- a/include/linux/timekeeping.h +++ b/include/linux/timekeeping.h @@ -243,7 +243,8 @@ extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot); extern int persistent_clock_is_local; extern void read_persistent_clock64(struct timespec64 *ts); -extern void read_boot_clock64(struct timespec64 *ts); +void read_persistent_clock_and_boot_offset(struct timespec64 *wall_clock, + struct timespec64 *boot_offset); extern int update_persistent_clock64(struct timespec64 now); /* diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 4786df904c22..cb738f825c12 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -1496,18 +1497,20 @@ void __weak read_persistent_clock64(struct timespec64 *ts64) } /** - * read_boot_clock64 - Return time of the system start. + * read_persistent_wall_and_boot_offset - Read persistent clock, and also offset + * from the boot. * * Weak dummy function for arches that do not yet support it. - * Function to read the exact time the system has been started. - * Returns a timespec64 with tv_sec=0 and tv_nsec=0 if unsupported. - * - * XXX - Do be sure to remove it once all arches implement it. + * wall_time - current time as returned by persistent clock + * boot_offset - offset that is defined as wall_time - boot_time + * default to 0. */ -void __weak read_boot_clock64(struct timespec64 *ts) +void __weak __init +read_persistent_wall_and_boot_offset(struct timespec64 *wall_time, + struct timespec64 *boot_offset) { - ts->tv_sec = 0; - ts->tv_nsec = 0; + read_persistent_clock64(wall_time); + *boot_offset = (struct timespec64){0}; } /* Flag for if timekeeping_resume() has injected sleeptime */ @@ -1521,28 +1524,29 @@ static bool persistent_clock_exists; */ void __init timekeeping_init(void) { + struct timespec64 wall_time, boot_offset, wall_to_mono; struct timekeeper *tk = &tk_core.timekeeper; struct clocksource *clock; unsigned long flags; - struct timespec64 now, boot, tmp; - - read_persistent_clock64(&now); - if (!timespec64_valid_strict(&now)) { - pr_warn("WARNING: Persistent clock returned invalid value!\n" - " Check your CMOS/BIOS settings.\n"); - now.tv_sec = 0; - now.tv_nsec = 0; - } else if (now.tv_sec || now.tv_nsec) - persistent_clock_exists = true; - read_boot_clock64(&boot); - if (!timespec64_valid_strict(&boot)) { - pr_warn("WARNING: Boot clock returned invalid value!\n" - " Check your CMOS/BIOS settings.\n"); - boot.tv_sec = 0; - boot.tv_nsec = 0; + read_persistent_wall_and_boot_offset(&wall_time, &boot_offset); + if (timespec64_valid_strict(&wall_time) && + timespec64_to_ns(&wall_time) > 0) { + persistent_clock_exists = true; + } else { + pr_warn("Persistent clock returned invalid value"); + wall_time = (struct timespec64){0}; } + if (timespec64_compare(&wall_time, &boot_offset) < 0) + boot_offset = (struct timespec64){0}; + + /* + * We want set wall_to_mono, so the following is true: + * wall time + wall_to_mono = boot time + */ + wall_to_mono = timespec64_sub(boot_offset, wall_time); + raw_spin_lock_irqsave(&timekeeper_lock, flags); write_seqcount_begin(&tk_core.seq); ntp_init(); @@ -1552,13 +1556,10 @@ void __init timekeeping_init(void) clock->enable(clock); tk_setup_internals(tk, clock); - tk_set_xtime(tk, &now); + tk_set_xtime(tk, &wall_time); tk->raw_sec = 0; - if (boot.tv_sec == 0 && boot.tv_nsec == 0) - boot = tk_xtime(tk); - set_normalized_timespec64(&tmp, -boot.tv_sec, -boot.tv_nsec); - tk_set_wall_to_mono(tk, tmp); + tk_set_wall_to_mono(tk, wall_to_mono); timekeeping_update(tk, TK_MIRROR | TK_CLOCK_WAS_SET); -- 2.18.0