From: Peter Zijlstra <peterz@infradead.org>
To: tglx@linutronix.de, mingo@kernel.org
Cc: linux-kernel@vger.kernel.org, ville.syrjala@linux.intel.com,
daniel.lezcano@linaro.org, rafael.j.wysocki@intel.com,
marta.lofstedt@intel.com, martin.peres@linux.intel.com,
pasha.tatashin@oracle.com, peterz@infradead.org,
daniel.vetter@ffwll.ch
Subject: [PATCH 3/9] sched/clock: Initialize all per-cpu state before switching (back) to unstable
Date: Fri, 21 Apr 2017 16:57:59 +0200 [thread overview]
Message-ID: <20170421150734.009734988@infradead.org> (raw)
In-Reply-To: <20170421145756.305735607@infradead.org>
[-- Attachment #1: peterz-sched-clock-rubarb-4.patch --]
[-- Type: text/plain, Size: 3201 bytes --]
In preparation for not keeping the sched_clock_tick() active for
stable TSC, we need to explicitly initialize all per-cpu state
before switching back to unstable.
Note: this patch looses the __gtod_offset calculation; it will be
restored in the next one.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
kernel/sched/clock.c | 60 +++++++++++++++++++++++++++++++++------------------
1 file changed, 39 insertions(+), 21 deletions(-)
--- a/kernel/sched/clock.c
+++ b/kernel/sched/clock.c
@@ -124,6 +124,12 @@ int sched_clock_stable(void)
return static_branch_likely(&__sched_clock_stable);
}
+static void __scd_stamp(struct sched_clock_data *scd)
+{
+ scd->tick_gtod = ktime_get_ns();
+ scd->tick_raw = sched_clock();
+}
+
static void __set_sched_clock_stable(void)
{
struct sched_clock_data *scd = this_scd();
@@ -141,8 +147,37 @@ static void __set_sched_clock_stable(voi
tick_dep_clear(TICK_DEP_BIT_CLOCK_UNSTABLE);
}
+/*
+ * If we ever get here, we're screwed, because we found out -- typically after
+ * the fact -- that TSC wasn't good. This means all our clocksources (including
+ * ktime) could have reported wrong values.
+ *
+ * What we do here is an attempt to fix up and continue sort of where we left
+ * off in a coherent manner.
+ *
+ * The only way to fully avoid random clock jumps is to boot with:
+ * "tsc=unstable".
+ */
static void __sched_clock_work(struct work_struct *work)
{
+ struct sched_clock_data *scd;
+ int cpu;
+
+ /* take a current timestamp and set 'now' */
+ preempt_disable();
+ scd = this_scd();
+ __scd_stamp(scd);
+ scd->clock = scd->tick_gtod + __gtod_offset;
+ preempt_enable();
+
+ /* clone to all CPUs */
+ for_each_possible_cpu(cpu)
+ per_cpu(sched_clock_data, cpu) = *scd;
+
+ printk(KERN_INFO "sched_clock: Marking unstable (%lld, %lld)<-(%lld, %lld)\n",
+ scd->tick_gtod, __gtod_offset,
+ scd->tick_raw, __sched_clock_offset);
+
static_branch_disable(&__sched_clock_stable);
}
@@ -150,27 +185,11 @@ static DECLARE_WORK(sched_clock_work, __
static void __clear_sched_clock_stable(void)
{
- struct sched_clock_data *scd = this_scd();
-
- /*
- * Attempt to make the stable->unstable transition continuous.
- *
- * Trouble is, this is typically called from the TSC watchdog
- * timer, which is late per definition. This means the tick
- * values can already be screwy.
- *
- * Still do what we can.
- */
- __gtod_offset = (scd->tick_raw + __sched_clock_offset) - (scd->tick_gtod);
-
- printk(KERN_INFO "sched_clock: Marking unstable (%lld, %lld)<-(%lld, %lld)\n",
- scd->tick_gtod, __gtod_offset,
- scd->tick_raw, __sched_clock_offset);
+ if (!sched_clock_stable())
+ return;
tick_dep_set(TICK_DEP_BIT_CLOCK_UNSTABLE);
-
- if (sched_clock_stable())
- schedule_work(&sched_clock_work);
+ schedule_work(&sched_clock_work);
}
void clear_sched_clock_stable(void)
@@ -357,8 +376,7 @@ void sched_clock_tick(void)
* XXX arguably we can skip this if we expose tsc_clocksource_reliable
*/
scd = this_scd();
- scd->tick_raw = sched_clock();
- scd->tick_gtod = ktime_get_ns();
+ __scd_stamp(scd);
if (!sched_clock_stable() && likely(sched_clock_running))
sched_clock_local(scd);
next prev parent reply other threads:[~2017-04-21 17:08 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-21 14:57 [PATCH 0/9] sched_clock fixes Peter Zijlstra
2017-04-21 14:57 ` [PATCH 1/9] x86/tsc: Provide tsc=unstable boot parameter Peter Zijlstra
2017-04-21 14:57 ` [PATCH 2/9] x86,tsc: Feed refined TSC calibration into sched_clock Peter Zijlstra
2017-04-21 14:57 ` Peter Zijlstra [this message]
2017-04-21 14:58 ` [PATCH 4/9] x86/tsc,sched/clock,clocksource: Use clocksource watchdog to provide stable sync points Peter Zijlstra
2017-04-21 14:58 ` [PATCH 5/9] sched/clock: Remove unused argument to sched_clock_idle_wakeup_event() Peter Zijlstra
2017-04-21 14:58 ` [PATCH 6/9] sched/clock: Remove watchdog touching Peter Zijlstra
2017-04-21 15:28 ` Peter Zijlstra
2017-04-21 14:58 ` [PATCH 8/9] sched/clock: Use late_initcall() instead of sched_init_smp() Peter Zijlstra
2017-04-21 14:58 ` [PATCH 9/9] sched/clock: Print a warning recommending tsc=unstable Peter Zijlstra
2017-04-25 9:31 ` [PATCH 0/9] sched_clock fixes Lofstedt, Marta
2017-04-25 13:44 ` Peter Zijlstra
2017-04-26 6:41 ` Lofstedt, Marta
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=20170421150734.009734988@infradead.org \
--to=peterz@infradead.org \
--cc=daniel.lezcano@linaro.org \
--cc=daniel.vetter@ffwll.ch \
--cc=linux-kernel@vger.kernel.org \
--cc=marta.lofstedt@intel.com \
--cc=martin.peres@linux.intel.com \
--cc=mingo@kernel.org \
--cc=pasha.tatashin@oracle.com \
--cc=rafael.j.wysocki@intel.com \
--cc=tglx@linutronix.de \
--cc=ville.syrjala@linux.intel.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®