LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: Linus Torvalds <torvalds@linuxfoundation.org>,
	Prarit Bhargava <prarit@redhat.com>,
	Mark Salyzyn <salyzyn@android.com>,
	Petr Mladek <pmladek@suse.com>, Ingo Molnar <mingo@kernel.org>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Sergey Senozhatsky <sergey.senozhatsky@gmail.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Joe Perches <joe@perches.com>
Subject: [RFC patch 7/7] timekeeping: Hack to use fine grained timestamps during boot
Date: Wed, 15 Nov 2017 19:15:38 +0100	[thread overview]
Message-ID: <20171115182657.703928462@linutronix.de> (raw)
In-Reply-To: 20171115181531.322572387@linutronix.de

[-- Attachment #1: timekeeping--Hack-to-use-fine-grained-timestamps-during-boot.patch --]
[-- Type: text/plain, Size: 2968 bytes --]

For demonstration purposes only.

Add a disgusting hack to work around the fact that high resolution clock
MONOTONIC accessors are not available during early boot and return stale
time stamps accross suspend/resume when the current clocksource is not
flagged with CLOCK_SOURCE_SUSPEND_ACCESS_OK.

Use local_clock() to provide timestamps in early boot and when the
clocksource is not accessible after timekeeping_suspend(). In the
suspend/resume case this might cause non monotonic timestamps.

Not-Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/timekeeping.c |   25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -16,6 +16,7 @@
 #include <linux/mm.h>
 #include <linux/nmi.h>
 #include <linux/sched.h>
+#include <linux/sched/clock.h>
 #include <linux/sched/loadavg.h>
 #include <linux/syscore_ops.h>
 #include <linux/clocksource.h>
@@ -499,17 +500,19 @@ EXPORT_SYMBOL_GPL(ktime_get_boot_fast_ns
 /*
  * See comment for __ktime_get_fast_ns() vs. timestamp ordering
  */
-static notrace u64 __ktime_get_real_fast(struct tk_fast *tkf, u64 *mono)
+static bool __ktime_get_real_fast(struct tk_fast *tkf, u64 *real, u64 *mono)
 {
 	struct tk_read_base *tkr;
 	u64 basem, baser, delta;
 	unsigned int seq;
+	bool hres;
 
 	do {
 		seq = raw_read_seqcount_latch(&tkf->seq);
 		tkr = tkf->base + (seq & 0x01);
 		basem = ktime_to_ns(tkr->base);
 		baser = ktime_to_ns(tkr->base_real);
+		hres = tkr->clock->flags & CLOCK_SOURCE_VALID_FOR_HRES;
 
 		delta = timekeeping_delta_to_ns(tkr,
 				clocksource_delta(tk_clock_read(tkr),
@@ -518,15 +521,23 @@ static notrace u64 __ktime_get_real_fast
 
 	if (mono)
 		*mono = basem + delta;
-	return baser + delta;
+	*real = baser + delta;
+	return hres;
 }
 
 /**
  * ktime_get_real_fast_ns: - NMI safe and fast access to clock realtime.
+ *
+ * Returns the wall clock (UTC) timestamp. Caveats:
+ *  - Returns 0 before timekeeping is initialized
+ *  - Returns a stale value accross suspend/resume
  */
 u64 ktime_get_real_fast_ns(void)
 {
-	return __ktime_get_real_fast(&tk_fast_mono, NULL);
+	u64 real;
+
+	__ktime_get_real_fast(&tk_fast_mono, &real, NULL);
+	return real;
 }
 EXPORT_SYMBOL_GPL(ktime_get_real_fast_ns);
 
@@ -535,13 +546,19 @@ EXPORT_SYMBOL_GPL(ktime_get_real_fast_ns
  * @ts:		Pointer to timestamp storage
  *
  * Stores clock monotonic, boottime and realtime time stamps
+ *
+ * Note: This is a special implementation for printk. The early boot time
+ * stamps, i.e. before timekeeping is available are taken from local_clock().
  */
 void ktime_get_fast_timestamps(struct system_timestamps *ts)
 {
 	struct timekeeper *tk = &tk_core.timekeeper;
+	bool hres;
 
-	ts->real = __ktime_get_real_fast(&tk_fast_mono, &ts->mono);
+	hres = __ktime_get_real_fast(&tk_fast_mono, &ts->real, &ts->mono);
 	ts->boot = ts->mono + ktime_to_ns(tk->offs_boot);
+	if (!hres)
+		ts->mono = local_clock();
 }
 
 /**

  parent reply	other threads:[~2017-11-15 18:29 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-15 18:15 [RFC patch 0/7] printk: Switch to CLOCK_MONOTONIC and store extra time stamps Thomas Gleixner
2017-11-15 18:15 ` [RFC patch 1/7] timekeeping: Do not unconditionally suspend NMI safe timekeepers Thomas Gleixner
2017-11-15 18:15 ` [RFC patch 2/7] x86/tsc: Set clocksource CLOCK_SOURCE_SUSPEND_ACCESS_OK Thomas Gleixner
2017-11-15 18:15 ` [RFC patch 3/7] printk: Use clock MONOTONIC for timestamps Thomas Gleixner
2017-11-16  7:58   ` Sergey Senozhatsky
2017-11-15 18:15 ` [RFC patch 4/7] timekeeping: Add NMI safe accessor to mono/boot/real clocks Thomas Gleixner
2017-11-17 23:00   ` Steven Rostedt
2017-11-17 23:12     ` Linus Torvalds
2017-11-17 23:43       ` Thomas Gleixner
2017-11-15 18:15 ` [RFC patch 5/7] crash: Add VMCOREINFO_FIELD_AND_OFFSET() Thomas Gleixner
2017-11-23 12:46   ` Petr Mladek
2017-11-15 18:15 ` [RFC patch 6/7] printk: Store mono/boot/real time timestamps Thomas Gleixner
2017-11-23 13:36   ` Petr Mladek
2017-11-15 18:15 ` Thomas Gleixner [this message]
2017-11-23 12:58   ` [RFC patch 7/7] timekeeping: Hack to use fine grained timestamps during boot Petr Mladek
2017-11-28 18:43     ` Prarit Bhargava
2017-11-28 18:47       ` Thomas Gleixner
2017-12-08 11:23         ` Petr Mladek
2017-12-08 19:51           ` Thomas Gleixner
2017-11-28 19:10       ` Mark Salyzyn
2017-11-28 19:45         ` Steven Rostedt
2017-11-28 20:29           ` Mark Salyzyn
2017-11-28 20:38             ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171115182657.703928462@linutronix.de \
    --to=tglx@linutronix.de \
    --cc=akpm@linux-foundation.org \
    --cc=hpa@zytor.com \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=prarit@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=salyzyn@android.com \
    --cc=sergey.senozhatsky@gmail.com \
    --cc=torvalds@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).