Skip to contentSkip to conversation

x86: unconditionally mark TSC unstable under Xen

PATCH In review Jed Smith 5 participants 15 messages Jul 14, 2010 – Aug 16, 2010 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Jed Smith

author
#0 lore raw

PATCH 1/1 x86: unconditionally mark TSC unstable under Xen

Jeremy, Jan - what do you think? Is this a bad move? I feel like there is a consequence to this that I am unaware of, but it fixes my issue.

--

x86: unconditionally mark TSC unstable under Xen

In certain domU environments (new Intel), time will rewind and jump around by seconds or more, leading to inaccurate measurements kernel-wide. This patch unilaterally marks the TSC unstable under Xen, which prevents timing from jumping around on these processors without significant penalty in all domU environments.

An example of this is the following testcase which runs many shell processes, each of which does a DNS lookup with dig(1):

http://gist.github.com/449825

When run on a Debian testing x86_64 system without this change it gives:

    $ (cd /tmp;git clone git://gist.github.com/449825.git;cd 449825;git
       pull;time perl many-digs.pl)
    [...]
    real    0m7.063s
    user    268659840m0.951s
    sys     38524003m13.072s

And with it:

    real    0m6.468s
    user    0m2.851s
    sys     0m6.789s

The issue isn't particular to that bit of code, it'll also crop when running the Git test suite in parallel, or in the TIME+ top(1) reports. Which will eventually end up displaying times like "-596523h-14:-8" for most long-lived processes on the system.

This closes bug #16314 - Erroneous idle times for processes:

https://bugzilla.kernel.org/show_bug.cgi?id=16314

It was also reported on the Linode forums as "Xen timing wonkyness":

http://www.linode.com/forums/viewtopic.php?t=5731

---
 arch/x86/kernel/cpu/intel.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
arch/x86/kernel/cpu/intel.c +8 -0
index 85f69cd..afc839a 100644
@@ -90,8 +90,14 @@ static void __cpuinit early_init_intel(struct cpuinfo_x86 *c)
 	if (c->x86_power & (1 << 8)) {
 		set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
 		set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
+#ifndef CONFIG_XEN
 		if (!check_tsc_unstable())
 			sched_clock_stable = 1;
+#else
+		/*
+		 * Under Xen, we cannot consider the TSC stable or it will
+		 * go backwards in certain circumstances.  Bug 16314.
+		 */
+		mark_tsc_unstable("Xen domain");
+#endif
 	}

/*

Signature · 10 lines
1.6.0.4


Regards,

Jed Smith
Systems Administrator
Linode, LLC
+1 (609) 593-7103 x1209
[email protected]
#1 lore raw
Quoted 3 lines · Jed Smith

Jeremy, Jan - what do you think? Is this a bad move? I feel like there is a consequence to this that I am unaware of, but it fixes my issue.

Ah, well that's interesting.

There's a couple of comments:

1. you can't do this with just a compile-time test, since the same kernel can also boot native
2. nothing in a Xen PV domU environment should be using the tsc directly, so this shouldn't have an effect. If something is using the tsc we should track it down.

I wonder, however, if you're getting the same result as Jan's suggestion of making sched_clock unstable by making the tsc unstable.

In that case, this patch may help:

Subject: [PATCH] xen: disable xen_sched_clock by default

xen_sched_clock only counts unstolen time. In principle this should be useful to the Linux scheduler so that it knows how much time a process actually consumed. But in practice this doesn't work very well as the scheduler expects the sched_clock time to be synchronized between cpus. It also uses sched_clock to measure the time a task spends sleeping, in which case "unstolen time" isn't meaningful.

So just use plain xen_clocksource_read to return wallclock nanoseconds for sched_clock.

arch/x86/xen/Kconfig +4 -0
index b83e119..6a09f01 100644
@@ -29,6 +29,10 @@ config XEN_SAVE_RESTORE
        depends on XEN && PM
        default y
 
+config XEN_SCHED_CLOCK
+       bool
+       default n
+
 config XEN_DEBUG_FS
 	bool "Enable Xen debug and tuning parameters in debugfs"
 	depends on XEN && DEBUG_FS
arch/x86/xen/enlighten.c +4 -0
index 4b57c0b..242a230 100644
@@ -939,7 +939,11 @@ static const struct pv_time_ops xen_time_ops __initdata = {
 	.set_wallclock = xen_set_wallclock,
 	.get_wallclock = xen_get_wallclock,
 	.get_tsc_khz = xen_tsc_khz,
+#ifdef CONFIG_XEN_SCHED_CLOCK
 	.sched_clock = xen_sched_clock,
+#else
+	.sched_clock = xen_clocksource_read,
+#endif
 };
 
 static const struct pv_cpu_ops xen_cpu_ops __initdata = {
arch/x86/xen/time.c +2 -1
index 9d1f853..32dc3dc 100644
@@ -154,6 +154,7 @@ static void do_stolen_accounting(void)
 	account_idle_ticks(ticks);
 }
 
+#ifdef CONFIG_XEN_SCHED_CLOCK
 /*
  * Xen sched_clock implementation.  Returns the number of unstolen
  * nanoseconds, which is nanoseconds the VCPU spent in RUNNING+BLOCKED
@@ -191,7 +192,7 @@ unsigned long long xen_sched_clock(void)
 
 	return ret;
 }
-
+#endif
 
 /* Get the TSC speed from Xen */
 unsigned long xen_tsc_khz(void)

Thanks,
J

author
#2 lore raw
Quoted 9 lines · Jeremy Fitzhardinge
Quoted 3 lines · Jed Smith

Jeremy, Jan - what do you think? Is this a bad move? I feel like there is a consequence to this that I am unaware of, but it fixes my issue.

Ah, well that's interesting.

There's a couple of comments:

Quoted 8 lines

1. you can't do this with just a compile-time test, since the same kernel can also boot native
2. nothing in a Xen PV domU environment should be using the tsc directly, so this shouldn't have an effect. If something is using the tsc we should track it down.

I wonder, however, if you're getting the same result as Jan's suggestion of making sched_clock unstable by making the tsc unstable.

That's what I went for - I had initially just commented out the assignment of sched_clock_stable to 1, which fixed it, but this felt cleaner.

Quoted 1 line

In that case, this patch may help:

Will try and report.

Regards,

Jed Smith
Systems Administrator
Linode, LLC
+1 (609) 593-7103 x1209
[email protected]

#4 lore raw
In reply to Jeremy Fitzhardinge
Quoted 29 lines · Jeremy Fitzhardinge
Quoted 3 lines · Jed Smith

Jeremy, Jan - what do you think? Is this a bad move? I feel like there is a consequence to this that I am unaware of, but it fixes my issue.

Ah, well that's interesting.

There's a couple of comments:

1. you can't do this with just a compile-time test, since the same kernel can also boot native
2. nothing in a Xen PV domU environment should be using the tsc directly, so this shouldn't have an effect. If something is using the tsc we should track it down.

I wonder, however, if you're getting the same result as Jan's suggestion of making sched_clock unstable by making the tsc unstable.

In that case, this patch may help:

Subject: [PATCH] xen: disable xen_sched_clock by default

xen_sched_clock only counts unstolen time. In principle this should be useful to the Linux scheduler so that it knows how much time a process actually consumed. But in practice this doesn't work very well as the scheduler expects the sched_clock time to be synchronized between cpus. It also uses sched_clock to measure the time a task spends sleeping, in which case "unstolen time" isn't meaningful.

Quoted 3 lines

So just use plain xen_clocksource_read to return wallclock nanoseconds for sched_clock.

I think that in this context is worth mentioning that xen_clocksource_read ends up calling pvclock_get_nsec_offset that calls native_read_tsc.

#5 lore raw
Quoted 11 lines · Stefano Stabellini
Quoted 9 lines · Jeremy Fitzhardinge

Subject: [PATCH] xen: disable xen_sched_clock by default

xen_sched_clock only counts unstolen time. In principle this should be useful to the Linux scheduler so that it knows how much time a process actually consumed. But in practice this doesn't work very well as the scheduler expects the sched_clock time to be synchronized between cpus. It also uses sched_clock to measure the time a task spends sleeping, in which case "unstolen time" isn't meaningful.

Quoted 8 lines
Quoted 4 lines

So just use plain xen_clocksource_read to return wallclock nanoseconds for sched_clock.

I think that in this context is worth mentioning that xen_clocksource_read ends up calling pvclock_get_nsec_offset that calls native_read_tsc.

That's different. That's not a general rdtsc, but a specific use of the tsc within the Xen clock ABI. We know and expect that the raw tsc value could be dubious, but we also have the Xen-provided corrections to apply to it.

J

#6 lore raw
Quoted 26 lines

From: Jeremy Fitzhardinge [mailto:[email protected]]
Sent: Thursday, July 15, 2010 10:30 AM
Subject: Re: [Xen-devel] Re: [PATCH] x86: unconditionally mark TSC unstable under Xen

Quoted 6 lines · Stefano Stabellini
Quoted 4 lines · Jeremy Fitzhardinge

Subject: [PATCH] xen: disable xen_sched_clock by default

xen_sched_clock only counts unstolen time. In principle this should be useful to the Linux scheduler so that it knows how much time a

process

Quoted 1 line
Quoted 1 line

actually consumed. But in practice this doesn't work very well as

the

Quoted 5 lines
Quoted 5 lines

scheduler expects the sched_clock time to be synchronized between cpus. It also uses sched_clock to measure the time a task spends sleeping, in which case "unstolen time" isn't meaningful.

So just use plain xen_clocksource_read to return wallclock

nanoseconds

Quoted 5 lines
Quoted 3 lines

for sched_clock.

I think that in this context is worth mentioning that xen_clocksource_read ends up calling pvclock_get_nsec_offset that

Quoted 7 lines

calls

Quoted 1 line

native_read_tsc.

That's different. That's not a general rdtsc, but a specific use of the tsc within the Xen clock ABI. We know and expect that the raw tsc value could be dubious, but we also have the Xen-provided corrections to apply to it.

Sorry if I'm a bit behind on this...

Isn't the real problem that, in a PV guest, the cpuid instructions that are testing the TSC-related CPUID bits are obtaining the actual hardware value, rather than what Xen would like the guest to believe? IOW, isn't the correct fix to use pvcpuid instead of cpuid when xen_pvdomain() is true?

#7 lore raw
Quoted 4 lines · Dan Magenheimer

Isn't the real problem that, in a PV guest, the cpuid instructions that are testing the TSC-related CPUID bits are obtaining the actual hardware value, rather than what Xen would like the guest to believe?

No, because there shouldn't be any "naked" rdtscs in the kernel.

Quoted 3 lines

IOW, isn't the correct fix to use pvcpuid instead of cpuid when xen_pvdomain() is true?

Every use of cpuid in the kernel goes via the cpuid pvop, which ends up doing the Xen cpuid rather than the native one. Usermode cpuid is still the "real" one, unless they explicitly use the Xen version.

J

#8 lore raw
Quoted 4 lines
Quoted 3 lines

Isn't the real problem that, in a PV guest, the cpuid instructions that are testing the TSC-related CPUID bits are obtaining the actual hardware value, rather than what Xen would like the guest to believe?

Quoted 8 lines

No, because there shouldn't be any "naked" rdtscs in the kernel.

Quoted 2 lines

IOW, isn't the correct fix to use pvcpuid instead of cpuid when xen_pvdomain() is true?

Every use of cpuid in the kernel goes via the cpuid pvop, which ends up doing the Xen cpuid rather than the native one. Usermode cpuid is still the "real" one, unless they explicitly use the Xen version.

OK, then I'm confused. Either:
- this is one of those recent Intel boxes where all the TSCs should be sync'ed but due to firmware issues they are not, in which case this is a Linux bug that has already been fixed upstream; or
- this isn't Xen 4.0+ but should be fixed in 4.0; or
- this is Xen 4.0+ and the default tsc_mode is being overridden

Otherwise, why is TSC not synchronized and pvclock always getting an offset of 0?

Apologies if this was stated in a differently titled thread of this conversation but, Jed, what is your Xen version?

#9 lore raw
Quoted 21 lines · Dan Magenheimer
Quoted 7 lines
Quoted 4 lines

Isn't the real problem that, in a PV guest, the cpuid instructions that are testing the TSC-related CPUID bits are obtaining the actual hardware value, rather than what Xen would like the guest to believe?

No, because there shouldn't be any "naked" rdtscs in the kernel.

Quoted 7 lines
Quoted 3 lines

IOW, isn't the correct fix to use pvcpuid instead of cpuid when xen_pvdomain() is true?

Every use of cpuid in the kernel goes via the cpuid pvop, which ends up doing the Xen cpuid rather than the native one. Usermode cpuid is still the "real" one, unless they explicitly use the Xen version.

OK, then I'm confused. Either:
- this is one of those recent Intel boxes where all the TSCs should be sync'ed but due to firmware issues they are not, in which case this is a Linux bug that has already been fixed upstream; or
- this isn't Xen 4.0+ but should be fixed in 4.0; or
- this is Xen 4.0+ and the default tsc_mode is being overridden

Quoted 2 lines

Otherwise, why is TSC not synchronized and pvclock always getting an offset of 0?

No, this bug doesn't really have anything to do with tsc sync issues.

The situation is:

* The scheduler uses its own timebase, called sched_clock
* We have a pvop for sched_clock
* The Xen implementation for sched_clock counts unstolen ns, rather than wallclock ns, since this is (somewhat, in theory) useful
* However, the scheduler checks to see if the tsc is stable (because the default sched_clock is based on the tsc), and if so, assumes that sched_clock is synced across all cpus - but of course the amount of stolen time is different for each vcpu

Unfortunately, while the idea of counting unstolen time is useful to see how much work got done in a timeslice, it pretty useless for counting how long something was asleep for (since you don't care about how much time was "stolen" while you were asleep). And the scheduler uses the same timebase for measuring both.

So the fix is to simply use plain Xen system time as the scheduler clock, as that will be synced across cpus.

J

#10 lore raw
Quoted 31 lines
Quoted 9 lines

OK, then I'm confused. Either:
- this is one of those recent Intel boxes where all the TSCs should be sync'ed but due to firmware issues they are not, in which case this is a Linux bug that has already been fixed upstream; or
- this isn't Xen 4.0+ but should be fixed in 4.0; or
- this is Xen 4.0+ and the default tsc_mode is being overridden

Otherwise, why is TSC not synchronized and pvclock always getting an offset of 0?

No, this bug doesn't really have anything to do with tsc sync issues.

The situation is:

* The scheduler uses its own timebase, called sched_clock
* We have a pvop for sched_clock
* The Xen implementation for sched_clock counts unstolen ns, rather than wallclock ns, since this is (somewhat, in theory) useful
* However, the scheduler checks to see if the tsc is stable
(because
the default sched_clock is based on the tsc), and if so, assumes that sched_clock is synced across all cpus - but of course the amount of stolen time is different for each vcpu

Unfortunately, while the idea of counting unstolen time is useful to see
how much work got done in a timeslice, it pretty useless for counting how long something was asleep for (since you don't care about how much time was "stolen" while you were asleep). And the scheduler uses the same timebase for measuring both.

Quoted 2 lines

So the fix is to simply use plain Xen system time as the scheduler clock, as that will be synced across cpus.

OK, that makes sense. Thanks for the thorough explanation.

Maybe the xen_sched_clock code should be entirely removed rather than ifdef'd since it is no longer used and "(somewhat, in theory)" led to a strange bug? Or if you are confident that it will be useful in the future by some linux scheduler, maybe add some comments about how enabling it may cause the effects Jed saw.

And maybe an even better answer is to submit a patch upstream so that the scheduler doesn't use the same timebase for measuring both, since the kernel is making a bad assumption about real vs virtual time. I'd imagine KVM users might benefit from that also.

#11 lore raw
Quoted 6 lines · Dan Magenheimer

Maybe the xen_sched_clock code should be entirely removed rather than ifdef'd since it is no longer used and "(somewhat, in theory)" led to a strange bug? Or if you are confident that it will be useful in the future by some linux scheduler, maybe add some comments about how enabling it may cause the effects Jed saw.

Yes, I can probably remove it altogether, though it isn't actually selectable without manually editing the Kconfig file.

Quoted 5 lines

And maybe an even better answer is to submit a patch upstream so that the scheduler doesn't use the same timebase for measuring both, since the kernel is making a bad assumption about real vs virtual time. I'd imagine KVM users might benefit from that also.

Its unclear how useful it is anyway. I've discussed it with Peter Zijlstra from time to time, but making the scheduler use two timebases is non-trivial I think. Or perhaps more accurate to say that I don't want to be getting into the scheduler, since it is not only a technical minefield.

J

#12 lore raw
Quoted 17 lines · Jeremy Fitzhardinge
Quoted 6 lines · Dan Magenheimer

Maybe the xen_sched_clock code should be entirely removed rather than ifdef'd since it is no longer used and "(somewhat, in theory)" led to a strange bug?  Or if you are confident that it will be useful in the future by some linux scheduler, maybe add some comments about how enabling it may cause the effects Jed saw.

Yes, I can probably remove it altogether, though it isn't actually selectable without manually editing the Kconfig file.

Quoted 5 lines

And maybe an even better answer is to submit a patch upstream so that the scheduler doesn't use the same timebase for measuring both, since the kernel is making a bad assumption about real vs virtual time. I'd imagine KVM users might benefit from that also.

Quoted 5 lines

Its unclear how useful it is anyway.  I've discussed it with Peter Zijlstra from time to time, but making the scheduler use two timebases is non-trivial I think.  Or perhaps more accurate to say that I don't want to be getting into the scheduler, since it is not only a technical minefield.

Hi all, I was wondering what the status on this issue was. Maybe I've missed something, but I've looked through the lkml and xen archives and I haven't found a re-rolled patch that addresses this issue.

I'm not very familiar with this area (well, any area) of the kernel, but if there's something needed to push this forward I'd be happy to help with that, or if this is already being addressed somewhere else a pointer to where that is would be welcome.

Thanks.

#13 lore raw
Quoted 8 lines · Ævar Arnfjörð Bjarmason

Hi all, I was wondering what the status on this issue was. Maybe I've missed something, but I've looked through the lkml and xen archives and I haven't found a re-rolled patch that addresses this issue.

I'm not very familiar with this area (well, any area) of the kernel, but if there's something needed to push this forward I'd be happy to help with that, or if this is already being addressed somewhere else a pointer to where that is would be welcome.

A patch that fixes this is in mainline and all the supported stable kernels. Itis "xen: drop xen_sched_clock in favour of using plain wallclock time".

J

#14 lore raw
Quoted 5 lines · Jeremy Fitzhardinge
Quoted 4 lines · Ævar Arnfjörð Bjarmason

Hi all, I was wondering what the status on this issue was. Maybe I've missed something, but I've looked through the lkml and xen archives and I haven't found a re-rolled patch that addresses this issue.

Quoted 8 lines
Quoted 4 lines

I'm not very familiar with this area (well, any area) of the kernel, but if there's something needed to push this forward I'd be happy to help with that, or if this is already being addressed somewhere else a pointer to where that is would be welcome.

A patch that fixes this is in mainline and all the supported stable kernels.  Itis "xen: drop xen_sched_clock in favour of using plain wallclock time".

Thanks! I didn't spot that when looking through your patches.