All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Mark Salyzyn <salyzyn@android.com>,
	Petr Mladek <pmladek@suse.com>,
	LKML <linux-kernel@vger.kernel.org>,
	Ingo Molnar <mingo@kernel.org>, "H. Peter Anvin" <hpa@zytor.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Prarit Bhargava <prarit@redhat.com>,
	Sergey Senozhatsky <sergey.senozhatsky@gmail.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	John Stultz <john.stultz@linaro.org>
Subject: Re: [GIT pull] printk updates for 4.15
Date: Sat, 18 Nov 2017 01:26:07 +0100 (CET)	[thread overview]
Message-ID: <alpine.DEB.2.20.1711180123360.2186@nanos> (raw)
In-Reply-To: <alpine.DEB.2.20.1711160804350.2265@nanos>

On Thu, 16 Nov 2017, Thomas Gleixner wrote:
> I hope that I can find a few spare cycles to whip up a POC patch which does
> not make stuff fall apart immediately.

Here you go. It survived suspend resume in a VM.

Thanks,

	tglx

8<------------------------
Subject: timekeeping: Make monotonic behave like boottime
From: Thomas Gleixner <tglx@linutronix.de>
Date: Fri, 17 Nov 2017 11:46:48 +0100

Clock MONOTONIC is not fast forwarded by the time spent in suspend on
resume. This is only done for BOOTTIME.

It would be desired to get rid of that difference, but the difference
between clock MONOTONIC and clock BOOTTIME is well documented so there
might be applications which depend on that behaviour.

Implement it for testing purposes. 

This requires to prevent that jiffies advance, which are coupled at clock
MONOTONIC as well in the current implementation. This is achieved by
forwarding the variables which are used for the jiffies update after resume
before the tick is restarted,

In timekeeping resume, the change is rather simple. Instead of updating the
offset between clock MONOTONIC and clock REALTIME/BOOTTIME, advance the
time keeper base for the MONOTONIC and the MONOTONIC_RAW clock by the time
spent in suspend.

So MONOTONIC is now the same as BOOTTIME and the offset between clock
REALTIME and clock MONOTONIC is the same as before suspend.

WARNING:

Use this with caution. Following prerequisites are required before booting
into a kernel with this patch applied:

  - Backup of important data like the nude pictures you did not send to
    facebook yet
  
  - Functional fire estinguisher
  
  - Vaccum cleaner to deal with the fallout

You've been warned and you're responsible for any damage which might result.

NOT-Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/tick-common.c   |   15 +++++++++++++++
 kernel/time/tick-internal.h |    6 ++++++
 kernel/time/tick-sched.c    |    9 +++++++++
 kernel/time/timekeeping.c   |    9 +++++++--
 4 files changed, 37 insertions(+), 2 deletions(-)

--- a/kernel/time/tick-common.c
+++ b/kernel/time/tick-common.c
@@ -419,6 +419,19 @@ void tick_suspend_local(void)
 	clockevents_shutdown(td->evtdev);
 }
 
+static void tick_forward_next_period(void)
+{
+	ktime_t delta, now = ktime_get();
+	u64 n;
+
+	delta = ktime_sub(now, tick_next_period);
+	n = ktime_divns(delta, tick_period);
+	tick_next_period += n * tick_period;
+	if (tick_next_period < now)
+		tick_next_period += tick_period;
+	tick_sched_forward_next_period();
+}
+
 /**
  * tick_resume_local - Resume the local tick device
  *
@@ -431,6 +444,8 @@ void tick_resume_local(void)
 	struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
 	bool broadcast = tick_resume_check_broadcast();
 
+	tick_forward_next_period();
+
 	clockevents_tick_resume(td->evtdev);
 	if (!broadcast) {
 		if (td->mode == TICKDEV_MODE_PERIODIC)
--- a/kernel/time/tick-internal.h
+++ b/kernel/time/tick-internal.h
@@ -141,6 +141,12 @@ static inline void tick_check_oneshot_br
 static inline bool tick_broadcast_oneshot_available(void) { return tick_oneshot_possible(); }
 #endif /* !(BROADCAST && ONESHOT) */
 
+#if defined(CONFIG_NO_HZ_COMMON) || defined(CONFIG_HIGH_RES_TIMERS)
+extern void tick_sched_forward_next_period(void);
+#else
+static inline void tick_sched_forward_next_period(void) { }
+#endif
+
 /* NO_HZ_FULL internal */
 #ifdef CONFIG_NO_HZ_FULL
 extern void tick_nohz_init(void);
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -52,6 +52,15 @@ struct tick_sched *tick_get_tick_sched(i
 static ktime_t last_jiffies_update;
 
 /*
+ * Called after resume. Make sure that jiffies are not fast forwarded due to
+ * clock monotonic being forwarded by the suspended time.
+ */
+void tick_sched_forward_next_period(void)
+{
+	last_jiffies_update = tick_next_period;
+}
+
+/*
  * Must be called with interrupts disabled !
  */
 static void tick_do_update_jiffies64(ktime_t now)
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -138,7 +138,13 @@ static void tk_set_wall_to_mono(struct t
 
 static inline void tk_update_sleep_time(struct timekeeper *tk, ktime_t delta)
 {
-	tk->offs_boot = ktime_add(tk->offs_boot, delta);
+	/*
+	 * Update both bases so mono and raw stay coupled. Not strictly the
+	 * same as the old code which did not adjust clock monotonic after
+	 * resume, so dragons might be lurking somewhere.
+	 */
+	tk->tkr_mono.base += delta;
+	tk->tkr_raw.base += delta;
 }
 
 /*
@@ -1624,7 +1630,6 @@ static void __timekeeping_inject_sleepti
 		return;
 	}
 	tk_xtime_add(tk, delta);
-	tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, *delta));
 	tk_update_sleep_time(tk, timespec64_to_ktime(*delta));
 	tk_debug_account_sleep_time(delta);
 }

  reply	other threads:[~2017-11-18  0:26 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-13  9:36 [GIT pull] printk updates for 4.15 Thomas Gleixner
2017-11-14  1:18 ` Linus Torvalds
2017-11-14  2:48   ` Linus Torvalds
2017-11-14 10:03   ` Petr Mladek
2017-11-14 13:28     ` Prarit Bhargava
2017-11-14 15:56     ` Mark Salyzyn
2017-11-15  0:48       ` Sergey Senozhatsky
2017-11-14 17:20     ` Linus Torvalds
2017-11-14 20:21       ` Thomas Gleixner
2017-11-14 21:07         ` Linus Torvalds
2017-11-14 21:09           ` Thomas Gleixner
2017-11-14 21:16           ` Mark Salyzyn
2017-11-14 21:29             ` Linus Torvalds
2017-11-14 22:10               ` Mark Salyzyn
2017-11-14 22:37                 ` Linus Torvalds
2017-11-14 22:50                   ` Thomas Gleixner
2017-11-14 23:00                     ` Joe Perches
2017-11-14 23:00                     ` Linus Torvalds
2017-11-14 23:04                       ` Thomas Gleixner
2017-11-14 23:18                         ` Linus Torvalds
2017-11-14 23:22                           ` Thomas Gleixner
2017-11-15  0:00                     ` Linus Torvalds
2017-11-15  8:04                       ` Ingo Molnar
2017-11-15 16:26                       ` Mark Salyzyn
2017-11-15 17:42                         ` Linus Torvalds
2017-11-16  0:37                           ` Thomas Gleixner
2017-11-16  1:23                             ` John Stultz
2017-11-16  1:32                             ` Linus Torvalds
2017-11-16  7:12                               ` Thomas Gleixner
2017-11-18  0:26                                 ` Thomas Gleixner [this message]
2017-11-18  0:44                                   ` Linus Torvalds
2017-11-18  1:00                                     ` Thomas Gleixner
2017-11-20  6:20                                   ` Kevin Easton
2017-11-20  6:36                                     ` Linus Torvalds
2018-01-29 20:34                     ` Mark Salyzyn
2018-01-29 21:49                       ` Thomas Gleixner

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=alpine.DEB.2.20.1711180123360.2186@nanos \
    --to=tglx@linutronix.de \
    --cc=akpm@linux-foundation.org \
    --cc=hpa@zytor.com \
    --cc=john.stultz@linaro.org \
    --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@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.