From: "Rafael J. Wysocki" <rjw@rjwysocki.net>
To: John Stultz <john.stultz@linaro.org>
Cc: Thomas Gleixner <tglx@linutronix.de>,
Peter Zijlstra <peterz@infradead.org>,
Alan Cox <alan@linux.intel.com>,
"Li, Aubrey" <aubrey.li@linux.intel.com>,
LKML <linux-kernel@vger.kernel.org>,
Linux PM list <linux-pm@vger.kernel.org>,
ACPI Devel Maling List <linux-acpi@vger.kernel.org>,
Kristen Carlson Accardi <kristen@linux.intel.com>,
Len Brown <len.brown@intel.com>
Subject: Re: [PATCH 3/6] timekeeping: Make it safe to use the fast timekeeper while suspended
Date: Fri, 13 Feb 2015 15:32:17 +0100 [thread overview]
Message-ID: <2177558.2TEykqeHuz@vostro.rjw.lan> (raw)
In-Reply-To: <CALAqxLWviqCvcgOgJOdWuTw+qHM9RW0480Ge5dNHf5Nonv75bQ@mail.gmail.com>
On Friday, February 13, 2015 05:03:51 PM John Stultz wrote:
> On Fri, Feb 13, 2015 at 10:03 AM, Rafael J. Wysocki <rjw@rjwysocki.net> wrote:
> > On Friday, February 13, 2015 08:53:38 AM John Stultz wrote:
> >> On Wed, Feb 11, 2015 at 12:03 PM, Rafael J. Wysocki <rjw@rjwysocki.net> wrote:
> >> > From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> >> >
> >> > Theoretically, ktime_get_mono_fast_ns() may be executed after
> >> > timekeeping has been suspended (or before it is resumed) which
> >> > in turn may lead to undefined behavior, for example, when the
> >> > clocksource read from timekeeping_get_ns() called by it is
> >> > not accessible at that time.
> >>
> >> And the callers of the ktime_get_mono_fast_ns() have to get back a
> >> value?
> >
> > Yes, they do.
> >
> >> Or can we return an error on timekeeping_suspended like we do
> >> w/ __getnstimeofday64()?
> >
> > No, we can't.
> >
> >> Also, what exactly is the case when the clocksource being read isn't
> >> accessible? I see this is conditionalized on
> >> CLOCK_SOURCE_SUSPEND_NONSTOP, so is the concern on resume we read the
> >> clocksource and its been reset causing a crazy time value?
> >
> > The clocksource's ->suspend method may have been called (during suspend)
> > and depending on what that did we may even crash things theoretically.
> >
> > During resume, before the clocksource's ->resume callback, it may just
> > be undefined behavior (random data etc).
> >
> > For system suspend as we have today the window is quite narrow, but after
> > patch [4/6] from this series suspend-to-idle may suspend timekeeping and
> > just sit there in idle for extended time (hours even) which broadens the
> > potential exposure quite a bit.
> >
> > Of course, it does that with interrupts disabled, but ktime_get_mono_fast_ns()
> > is for NMI, so theoretically, if an NMI happens while we're in suspend-to-idle
> > with timekeeping suspended and the clocksource is not CLOCK_SOURCE_SUSPEND_NONSTOP
> > and the NMI calls ktime_get_mono_fast_ns(), strange and undesirable things may
> > happen.
>
> Ok.. No objection to the approach then. But maybe could you wrap the
> new logic in a halt_fast_timekeeper() function? Also is there much
> value in not halting it for SUSPEND_NONSTOP clocksources? If not,
> might as well halt it in all cases just to simplify the conditions we
> have to keep track of in our heads. :)
I don't see a problem with doing that unconditionally.
What about the appended version of the patch, then?
Rafael
---
From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Subject: timekeeping: Make it safe to use the fast timekeeper while suspended
Theoretically, ktime_get_mono_fast_ns() may be executed after
timekeeping has been suspended (or before it is resumed) which
in turn may lead to undefined behavior, for example, when the
clocksource read from timekeeping_get_ns() called by it is
not accessible at that time.
Prevent that from happening by setting up a dummy readout base for
the fast timekeeper during timekeeping_suspend() such that it will
always return the same number of cycles.
After the last timekeeping_update() in timekeeping_suspend() the
clocksource is read and the result is stored as cycles_at_suspend.
The readout base from the current timekeeper is copied onto the
dummy and the ->read pointer of the dummy is set to a routine
unconditionally returning cycles_at_suspend. Next, the dummy is
passed to update_fast_timekeeper().
Then, ktime_get_mono_fast_ns() will work until the subsequent
timekeeping_resume() and the proper readout base for the fast
timekeeper will be restored by the timekeeping_update() called
right after clearing timekeeping_suspended.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
kernel/time/timekeeping.c | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
Index: linux-pm/kernel/time/timekeeping.c
===================================================================
--- linux-pm.orig/kernel/time/timekeeping.c
+++ linux-pm/kernel/time/timekeeping.c
@@ -332,6 +332,35 @@ u64 notrace ktime_get_mono_fast_ns(void)
}
EXPORT_SYMBOL_GPL(ktime_get_mono_fast_ns);
+/* Suspend-time cycles value for halted fast timekeeper. */
+static cycle_t cycles_at_suspend;
+
+static cycle_t dummy_clock_read(struct clocksource *cs)
+{
+ return cycles_at_suspend;
+}
+
+/**
+ * halt_fast_timekeeper - Prevent fast timekeeper from accessing clocksource.
+ * @tk: Timekeeper to snapshot.
+ *
+ * It generally is unsafe to access the clocksource after timekeeping has been
+ * suspended, so take a snapshot of the readout base of @tk and use it as the
+ * fast timekeeper's readout base while suspended. It will return the same
+ * number of cycles every time until timekeeping is resumed at which time the
+ * proper readout base for the fast timekeeper will be restored automatically.
+ */
+static void halt_fast_timekeeper(struct timekeeper *tk)
+{
+ static struct tk_read_base tkr_dummy;
+ struct tk_read_base *tkr = &tk->tkr;
+
+ memcpy(&tkr_dummy, tkr, sizeof(tkr_dummy));
+ cycles_at_suspend = tkr->read(tkr->clock);
+ tkr_dummy.read = dummy_clock_read;
+ update_fast_timekeeper(&tkr_dummy);
+}
+
#ifdef CONFIG_GENERIC_TIME_VSYSCALL_OLD
static inline void update_vsyscall(struct timekeeper *tk)
@@ -1294,6 +1323,7 @@ static int timekeeping_suspend(void)
}
timekeeping_update(tk, TK_MIRROR);
+ halt_fast_timekeeper(tk);
write_seqcount_end(&tk_core.seq);
raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
next prev parent reply other threads:[~2015-02-13 14:09 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-02-11 4:00 [PATCH 0/6] PM / sleep: Support for quiescing timers during suspend-to-idle Rafael J. Wysocki
2015-02-11 4:01 ` [PATCH 1/6] PM / sleep: Re-implement suspend-to-idle handling Rafael J. Wysocki
2015-02-12 13:14 ` Peter Zijlstra
2015-02-12 16:18 ` Rafael J. Wysocki
2015-02-12 22:33 ` [Update][PATCH 1/6 v2] " Rafael J. Wysocki
2015-02-11 4:01 ` [PATCH 2/6] timekeeping: Pass readout base to update_fast_timekeeper() Rafael J. Wysocki
2015-02-13 0:29 ` John Stultz
2015-02-11 4:03 ` [PATCH 3/6] timekeeping: Make it safe to use the fast timekeeper while suspended Rafael J. Wysocki
2015-02-13 0:53 ` John Stultz
2015-02-13 2:03 ` Rafael J. Wysocki
2015-02-13 2:59 ` Travis
2015-02-13 9:03 ` John Stultz
2015-02-13 14:32 ` Rafael J. Wysocki [this message]
2015-02-14 17:30 ` John Stultz
2015-02-11 4:03 ` [PATCH 4/6] PM / sleep: Make it possible to quiesce timers during suspend-to-idle Rafael J. Wysocki
2015-02-12 13:24 ` Peter Zijlstra
2015-02-12 16:19 ` Rafael J. Wysocki
2015-02-12 22:36 ` [Update][PATCH 4/6 v2] " Rafael J. Wysocki
2015-02-11 4:04 ` [PATCH 5/6] intel_idle: Add ->enter_freeze callbacks Rafael J. Wysocki
2015-02-12 13:26 ` Peter Zijlstra
2015-02-12 16:24 ` Rafael J. Wysocki
2015-02-12 16:25 ` Peter Zijlstra
2015-03-04 23:50 ` Li, Aubrey
2015-03-05 0:18 ` Rafael J. Wysocki
2015-03-05 0:09 ` Li, Aubrey
2015-02-11 4:04 ` [PATCH 6/6] ACPI / idle: Implement ->enter_freeze callback routine Rafael J. Wysocki
2015-02-13 8:13 ` [PATCH 0/6] PM / sleep: Support for quiescing timers during suspend-to-idle Peter Zijlstra
2015-02-13 14:29 ` Rafael J. Wysocki
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=2177558.2TEykqeHuz@vostro.rjw.lan \
--to=rjw@rjwysocki.net \
--cc=alan@linux.intel.com \
--cc=aubrey.li@linux.intel.com \
--cc=john.stultz@linaro.org \
--cc=kristen@linux.intel.com \
--cc=len.brown@intel.com \
--cc=linux-acpi@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
/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
all inboxes | Powered by JetHome®