From patchwork Wed Jun 19 14:23:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jason A. Donenfeld" X-Patchwork-Id: 167242 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp799763ilk; Wed, 19 Jun 2019 07:24:15 -0700 (PDT) X-Google-Smtp-Source: APXvYqyk8MP4z9Id+ssxtfT7WtCFelS2zbCMicFPyJIj6LCpl8UCXAMm/1nnPJb27qiZFZ+9tc6P X-Received: by 2002:a62:e308:: with SMTP id g8mr44189555pfh.162.1560954255594; Wed, 19 Jun 2019 07:24:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560954255; cv=none; d=google.com; s=arc-20160816; b=rCdgH0aBPy31SVF8huZrAXeU/VgPTzVG2BZh59IxHXQwrGMeU57u7GkvamUhcJUzil wU7Ho3wfhxnWA/+cCcLj+wMq+LUaT0+izUJDfqPGarD2xwtWQUFj790XfeWEixFNv2QH KgnQYmeLjhfvWPFz89Fltav1QkjCp3fcfARQ+VAtZ5nAu0vWqfAOewT+tprPMTqit242 8pfq6sKKUaEznKv2eN2HBiwg1cRce7WmNTtDWgYZZl44/N5fqS+/gkF7TRp8aBL8Lyvp sHM8//5jn91PoUa3l+LkXML9VH86zZUyDNHLKrcwhRQEhfviISjEiVKkTAZhPYmEywzi OETA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=jDGTwzzIeJWFVDRNjyzE46guOAc13rhTU+jpbSsaayY=; b=juDNErOPmBbToN5Z0hKR0thhDwxzYdMF7yEb6hg9Bcj160/9JI7zdtotcUNwmSgA3E NpTwpSOSo/aOS/3KQt8s77LE3IN+Z6MU6LtJYCqqtgxdCaziCzQ13WkI3QItFsAgBfTp iccAP7FIfXF43CnzYbaJoBtPEuamg7CV46U81HTNCMYyOdrJVTcinXsRrLo53tPvjKrx 6uYU9RCFMvhrIz5jXIzfgtTuLrmIgT5O1vgeytnjlhg2h2fxOvEN8xjcjgslZC2y2adX PcVQktKsco22jR+/1SBq7Tp1/uwGkN+DRnwgQEOS4gOS9t0isga+lx9R0q8bhRRInxMH h7Fg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=mail header.b=m63Jd0nD; 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=zx2c4.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 6si4720172pfd.2.2019.06.19.07.24.15; Wed, 19 Jun 2019 07:24:15 -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=@zx2c4.com header.s=mail header.b=m63Jd0nD; 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=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729757AbfFSOYN (ORCPT + 30 others); Wed, 19 Jun 2019 10:24:13 -0400 Received: from frisell.zx2c4.com ([192.95.5.64]:55123 "EHLO frisell.zx2c4.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726496AbfFSOYN (ORCPT ); Wed, 19 Jun 2019 10:24:13 -0400 Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTP id bc50ebed; Wed, 19 Jun 2019 13:51:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=zx2c4.com; h=from:to:cc :subject:date:message-id:mime-version:content-transfer-encoding; s=mail; bh=oQdrKImMCKqyw1vdhgoyL18i+yY=; b=m63Jd0nDH93RkAadDJ0Z iHcovby5TLYtT2uaXZOQDqZTheJw8moI03cYsLMllc0PLGIot01QoBLCYQ5O7fKs SrpwqZgD7FiJRIhclOqgcjRiZfnnnuELkp3FWwVUV9WzaoLwDtV4kGuasZ6rpo3/ 6gQ/T8KlyhY+k7PEG3eM18Vqe79ME43T0o+Ldd94b09E9t4g4KLqyhqXdO0M3CLf TVmWXvYQN/+R9Z52PZo8/qMu2sQD9FCj7irUqq6ENemHMN8AGR3rrKbXqKB8Mvh/ 9ITs/tMph1oleKdfVxTVME3jCtiMGq/Pfd8jmThcCt7swCcE40EmH7PCKAf6lw2Y wg== Received: by frisell.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id bba5e044 (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256:NO); Wed, 19 Jun 2019 13:51:02 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-kernel@vger.kernel.org Cc: "Jason A. Donenfeld" , Thomas Gleixner , Peter Zijlstra , Arnd Bergmann , Andrew Morton Subject: [PATCH v2] timekeeping: get_jiffies_boot_64() for jiffies that include sleep time Date: Wed, 19 Jun 2019 16:23:50 +0200 Message-Id: <20190619142350.1985-1-Jason@zx2c4.com> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This enables using the usual get_jiffies_64() but taking into account time spent sleeping, giving the high performance characteristics of querying jiffies without the drawback. We accomplish this by precomputing the boottime jiffies offset whenever it is updated, rather than doing the expensive-ish div_u64 on each query. Since the resolution of this is in terms of jiffies, this allows determining limits for comparison in terms of jiffies too, which makes the comparisons more exact, despite jiffies being a fairly coarse stamp. Adding the suspend offset to jiffies as such doesn't actually race in a way different from the usual races associated with the suspend offset: either boot offset has been updated before the call to get_jiffies_boot_64(), in which case we're fine, or it hasn't in which case, this is no different than any of the existing suspend querying functions, which may be invoked early in system resumption before the offset is updated. Signed-off-by: Jason A. Donenfeld Cc: Thomas Gleixner Cc: Peter Zijlstra Cc: Arnd Bergmann Cc: Andrew Morton Cc; John Stultz --- include/linux/jiffies.h | 1 + include/linux/timekeeper_internal.h | 2 ++ kernel/time/timekeeping.c | 11 +++++++++++ 3 files changed, 14 insertions(+) -- 2.21.0 diff --git a/include/linux/jiffies.h b/include/linux/jiffies.h index 1b6d31da7cbc..e4a9776d8b2a 100644 --- a/include/linux/jiffies.h +++ b/include/linux/jiffies.h @@ -80,6 +80,7 @@ extern int register_refined_jiffies(long clock_tick_rate); extern u64 __cacheline_aligned_in_smp jiffies_64; extern unsigned long volatile __cacheline_aligned_in_smp __jiffy_arch_data jiffies; +u64 get_jiffies_boot_64(void); #if (BITS_PER_LONG < 64) u64 get_jiffies_64(void); #else diff --git a/include/linux/timekeeper_internal.h b/include/linux/timekeeper_internal.h index 7acb953298a7..2e4c52fe0250 100644 --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -51,6 +51,7 @@ struct tk_read_base { * @wall_to_monotonic: CLOCK_REALTIME to CLOCK_MONOTONIC offset * @offs_real: Offset clock monotonic -> clock realtime * @offs_boot: Offset clock monotonic -> clock boottime + * @offs_boot_jiffies64 Offset clock monotonic -> clock boottime in jiffies64 * @offs_tai: Offset clock monotonic -> clock tai * @tai_offset: The current UTC to TAI offset in seconds * @clock_was_set_seq: The sequence number of clock was set events @@ -93,6 +94,7 @@ struct timekeeper { struct timespec64 wall_to_monotonic; ktime_t offs_real; ktime_t offs_boot; + u64 offs_boot_jiffies64; ktime_t offs_tai; s32 tai_offset; unsigned int clock_was_set_seq; diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 85f5912d8f70..a3707b454446 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -146,6 +146,7 @@ static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec64 wtm) static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta) { tk->offs_boot = ktime_add(tk->offs_boot, delta); + tk->offs_boot_jiffies64 = nsecs_to_jiffies64(ktime_to_ns(tk->offs_boot)); } /* @@ -539,6 +540,16 @@ u64 ktime_get_real_fast_ns(void) } EXPORT_SYMBOL_GPL(ktime_get_real_fast_ns); +/** + * get_jiffies_boot_64 - The normal get_jiffies_64(), but taking into + * account the time spent sleeping. + */ +u64 get_jiffies_boot_64(void) +{ + return get_jiffies_64() + tk_core.timekeeper.offs_boot_jiffies64; +} +EXPORT_SYMBOL(get_jiffies_boot_64); + /** * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource. * @tk: Timekeeper to snapshot.