From: Daniel Thompson <daniel.thompson@linaro.org>
To: Thomas Gleixner <tglx@linutronix.de>,
John Stultz <john.stultz@linaro.org>
Cc: Daniel Thompson <daniel.thompson@linaro.org>,
linux-kernel@vger.kernel.org, patches@linaro.org,
linaro-kernel@lists.linaro.org,
Sumit Semwal <sumit.semwal@linaro.org>,
Stephen Boyd <sboyd@codeaurora.org>,
Steven Rostedt <rostedt@goodmis.org>,
Russell King <linux@arm.linux.org.uk>,
Will Deacon <will.deacon@arm.com>,
Catalin Marinas <catalin.marinas@arm.com>
Subject: [PATCH v3 4/4] sched_clock: Avoid deadlock during read from NMI
Date: Fri, 30 Jan 2015 19:03:22 +0000 [thread overview]
Message-ID: <1422644602-11953-5-git-send-email-daniel.thompson@linaro.org> (raw)
In-Reply-To: <1422644602-11953-1-git-send-email-daniel.thompson@linaro.org>
Currently it is possible for an NMI (or FIQ on ARM) to come in and
read sched_clock() whilst update_sched_clock() has locked the seqcount
for writing. This results in the NMI handler locking up when it calls
raw_read_seqcount_begin().
This patch fixes the NMI safety issues by providing banked clock data.
This is a similar approach to the one used in Thomas Gleixner's
4396e058c52e("timekeeping: Provide fast and NMI safe access to
CLOCK_MONOTONIC").
Suggested-by: Stephen Boyd <sboyd@codeaurora.org>
Signed-off-by: Daniel Thompson <daniel.thompson@linaro.org>
Cc: Russell King <linux@arm.linux.org.uk>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
---
kernel/time/sched_clock.c | 91 ++++++++++++++++++++++++++++++-----------------
1 file changed, 58 insertions(+), 33 deletions(-)
diff --git a/kernel/time/sched_clock.c b/kernel/time/sched_clock.c
index 638c765131fa..2b1a465f1c00 100644
--- a/kernel/time/sched_clock.c
+++ b/kernel/time/sched_clock.c
@@ -40,12 +40,12 @@ struct clock_read_data {
* registration of a new clock source)
*
* The ordering of this structure has been chosen to optimize cache
- * performance. In particular seq and read_data (combined) should fit
+ * performance. In particular seq and read_data[0] (combined) should fit
* into a single 64 byte cache line.
*/
struct clock_data {
seqcount_t seq;
- struct clock_read_data read_data;
+ struct clock_read_data read_data[2];
ktime_t wrap_kt;
unsigned long rate;
u64 (*actual_read_sched_clock)(void);
@@ -66,10 +66,9 @@ static u64 notrace jiffy_sched_clock_read(void)
}
static struct clock_data cd ____cacheline_aligned = {
- .read_data = { .mult = NSEC_PER_SEC / HZ,
- .read_sched_clock = jiffy_sched_clock_read, },
+ .read_data[0] = { .mult = NSEC_PER_SEC / HZ,
+ .read_sched_clock = jiffy_sched_clock_read, },
.actual_read_sched_clock = jiffy_sched_clock_read,
-
};
static inline u64 notrace cyc_to_ns(u64 cyc, u32 mult, u32 shift)
@@ -81,10 +80,11 @@ unsigned long long notrace sched_clock(void)
{
u64 cyc, res;
unsigned long seq;
- struct clock_read_data *rd = &cd.read_data;
+ struct clock_read_data *rd;
do {
- seq = raw_read_seqcount_begin(&cd.seq);
+ seq = raw_read_seqcount(&cd.seq);
+ rd = cd.read_data + (seq & 1);
res = rd->epoch_ns;
if (rd->read_sched_clock) {
@@ -98,26 +98,50 @@ unsigned long long notrace sched_clock(void)
}
/*
+ * Updating the data required to read the clock.
+ *
+ * sched_clock will never observe mis-matched data even if called from
+ * an NMI. We do this by maintaining an odd/even copy of the data and
+ * steering sched_clock to one or the other using a sequence counter.
+ * In order to preserve the data cache profile of sched_clock as much
+ * as possible the system reverts back to the even copy when the update
+ * completes; the odd copy is used *only* during an update.
+ */
+static void update_clock_read_data(struct clock_read_data *rd)
+{
+ /* update the backup (odd) copy with the new data */
+ cd.read_data[1] = *rd;
+
+ /* steer readers towards the new data */
+ raw_write_seqcount_latch(&cd.seq);
+
+ /* now its safe for us to update the normal (even) copy */
+ cd.read_data[0] = *rd;
+
+ /* switch readers back to the even copy */
+ raw_write_seqcount_latch(&cd.seq);
+}
+
+/*
* Atomically update the sched_clock epoch.
*/
static void notrace update_sched_clock(void)
{
- unsigned long flags;
u64 cyc;
u64 ns;
- struct clock_read_data *rd = &cd.read_data;
+ struct clock_read_data rd;
+
+ rd = cd.read_data[0];
cyc = cd.actual_read_sched_clock();
- ns = rd->epoch_ns +
- cyc_to_ns((cyc - rd->epoch_cyc) & rd->sched_clock_mask,
- rd->mult, rd->shift);
-
- raw_local_irq_save(flags);
- raw_write_seqcount_begin(&cd.seq);
- rd->epoch_ns = ns;
- rd->epoch_cyc = cyc;
- raw_write_seqcount_end(&cd.seq);
- raw_local_irq_restore(flags);
+ ns = rd.epoch_ns +
+ cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask,
+ rd.mult, rd.shift);
+
+ rd.epoch_ns = ns;
+ rd.epoch_cyc = cyc;
+
+ update_clock_read_data(&rd);
}
static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
@@ -134,7 +158,7 @@ void __init sched_clock_register(u64 (*read)(void), int bits,
u32 new_mult, new_shift;
unsigned long r;
char r_unit;
- struct clock_read_data *rd = &cd.read_data;
+ struct clock_read_data rd;
if (cd.rate > rate)
return;
@@ -151,22 +175,23 @@ void __init sched_clock_register(u64 (*read)(void), int bits,
wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask);
cd.wrap_kt = ns_to_ktime(wrap - (wrap >> 3));
+ rd = cd.read_data[0];
+
/* update epoch for new counter and update epoch_ns from old counter*/
new_epoch = read();
cyc = cd.actual_read_sched_clock();
- ns = rd->epoch_ns +
- cyc_to_ns((cyc - rd->epoch_cyc) & rd->sched_clock_mask,
- rd->mult, rd->shift);
+ ns = rd.epoch_ns +
+ cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask,
+ rd.mult, rd.shift);
cd.actual_read_sched_clock = read;
- raw_write_seqcount_begin(&cd.seq);
- rd->read_sched_clock = read;
- rd->sched_clock_mask = new_mask;
- rd->mult = new_mult;
- rd->shift = new_shift;
- rd->epoch_cyc = new_epoch;
- rd->epoch_ns = ns;
- raw_write_seqcount_end(&cd.seq);
+ rd.read_sched_clock = read;
+ rd.sched_clock_mask = new_mask;
+ rd.mult = new_mult;
+ rd.shift = new_shift;
+ rd.epoch_cyc = new_epoch;
+ rd.epoch_ns = ns;
+ update_clock_read_data(&rd);
r = rate;
if (r >= 4000000) {
@@ -213,7 +238,7 @@ void __init sched_clock_postinit(void)
static int sched_clock_suspend(void)
{
- struct clock_read_data *rd = &cd.read_data;
+ struct clock_read_data *rd = &cd.read_data[0];
update_sched_clock();
hrtimer_cancel(&sched_clock_timer);
@@ -223,7 +248,7 @@ static int sched_clock_suspend(void)
static void sched_clock_resume(void)
{
- struct clock_read_data *rd = &cd.read_data;
+ struct clock_read_data *rd = &cd.read_data[0];
rd->epoch_cyc = cd.actual_read_sched_clock();
hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
--
1.9.3
next prev parent reply other threads:[~2015-01-30 19:04 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-01-21 16:53 [RFC PATCH] sched_clock: Avoid tearing " Daniel Thompson
2015-01-21 17:29 ` John Stultz
2015-01-21 20:20 ` Daniel Thompson
2015-01-21 20:58 ` Stephen Boyd
2015-01-22 13:06 ` [PATCH v2] sched_clock: Avoid deadlock " Daniel Thompson
2015-01-30 19:03 ` [PATCH v3 0/4] sched_clock: Optimize and avoid " Daniel Thompson
2015-01-30 19:03 ` [PATCH v3 1/4] sched_clock: Match scope of read and write seqcounts Daniel Thompson
2015-01-30 19:03 ` [PATCH v3 2/4] sched_clock: Optimize cache line usage Daniel Thompson
2015-02-05 1:14 ` Stephen Boyd
2015-02-05 10:21 ` Daniel Thompson
2015-01-30 19:03 ` [PATCH v3 3/4] sched_clock: Remove suspend from clock_read_data Daniel Thompson
2015-01-30 19:03 ` Daniel Thompson [this message]
2015-02-05 1:23 ` [PATCH v3 4/4] sched_clock: Avoid deadlock during read from NMI Stephen Boyd
2015-02-05 1:48 ` Steven Rostedt
2015-02-05 6:23 ` Stephen Boyd
2015-02-05 0:50 ` [PATCH v3 0/4] sched_clock: Optimize and avoid " Stephen Boyd
2015-02-05 9:05 ` Daniel Thompson
2015-02-08 12:09 ` Daniel Thompson
2015-02-09 22:08 ` Stephen Boyd
2015-02-08 12:02 ` [PATCH v4 0/5] " Daniel Thompson
2015-02-08 12:02 ` [PATCH v4 1/5] sched_clock: Match scope of read and write seqcounts Daniel Thompson
2015-02-08 12:02 ` [PATCH v4 2/5] sched_clock: Optimize cache line usage Daniel Thompson
2015-02-09 1:28 ` Will Deacon
2015-02-09 9:47 ` Daniel Thompson
2015-02-10 2:37 ` Stephen Boyd
2015-02-08 12:02 ` [PATCH v4 3/5] sched_clock: Remove suspend from clock_read_data Daniel Thompson
2015-02-08 12:02 ` [PATCH v4 4/5] sched_clock: Remove redundant notrace from update function Daniel Thompson
2015-02-08 12:02 ` [PATCH v4 5/5] sched_clock: Avoid deadlock during read from NMI Daniel Thompson
2015-02-13 3:49 ` [PATCH v4 0/5] sched_clock: Optimize and avoid " Stephen Boyd
2015-03-02 15:56 ` [PATCH v5 " Daniel Thompson
2015-03-02 15:56 ` [PATCH v5 1/5] sched_clock: Match scope of read and write seqcounts Daniel Thompson
2015-03-02 15:56 ` [PATCH v5 2/5] sched_clock: Optimize cache line usage Daniel Thompson
2015-03-02 15:56 ` [PATCH v5 3/5] sched_clock: Remove suspend from clock_read_data Daniel Thompson
2015-03-02 15:56 ` [PATCH v5 4/5] sched_clock: Remove redundant notrace from update function Daniel Thompson
2015-03-02 15:56 ` [PATCH v5 5/5] sched_clock: Avoid deadlock during read from NMI Daniel Thompson
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=1422644602-11953-5-git-send-email-daniel.thompson@linaro.org \
--to=daniel.thompson@linaro.org \
--cc=catalin.marinas@arm.com \
--cc=john.stultz@linaro.org \
--cc=linaro-kernel@lists.linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@arm.linux.org.uk \
--cc=patches@linaro.org \
--cc=rostedt@goodmis.org \
--cc=sboyd@codeaurora.org \
--cc=sumit.semwal@linaro.org \
--cc=tglx@linutronix.de \
--cc=will.deacon@arm.com \
/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®