All of lore.kernel.org
 help / color / mirror / Atom feed
* non-emulated rdtsc: a smoking gun!
@ 2009-10-22 20:53 Dan Magenheimer
  2009-10-22 21:24 ` Jeremy Fitzhardinge
  0 siblings, 1 reply; 2+ messages in thread
From: Dan Magenheimer @ 2009-10-22 20:53 UTC (permalink / raw)
  To: Xen-devel
  Cc: kurt.hackel, Jeremy Fitzhardinge, Avi Kivity, chris.mason,
	Keir Fraser

I just found a Linux kernel use of rdtsc that
MAY cause a significant failure if rdtsc is
unemulated and a poorly timed migration (or
save/restore) occurs under Xen or KVM.

The problem is that a call to __udelay() --
or any member of the delay() family -- may
return prematurely**.  Since these functions
"must guarantee that we wait at least the
amount of time" specified, there are likely
unknown kernel circumstances where a
premature return will cause problems.
(Disclaimer: I haven't gone through every use
of every call site of every member of the delay
function family to prove this.)

I observed this use of rdtsc on a real running
released EL5U2-32b PV kernel, but the problem also
exists on 2.6.31 and probably on any currently
shipping PV kernel.  AND due to a bug(?) in HVM
management of TSC, I think it will occur in any
Linux HVM as well.  And, other than Xen/KVM
guaranteeing rdtsc is monotonically-increasing
(and tracks wallclock time across a migration
which Xen's emulated rdtsc doesn't yet do),
I don't think there is a solution.

The problem can occur if a migration or
save/restore results in the appearance that
the physical TSC went backwards.  For example:
1) A live migration occurs from machine A
   to machine B, and machine B was much more
   recently booted than machine A; or
2) A guest is saved on machine A, machine A
   has been running for a long time, machine
   A is rebooted, and the guest is restored
   on machine A shortly after it is booted.

If a delay() function is currently executing
in the guest kernel when the above occurs
and the rdtsc instruction is unemulated,
the delay() function will return immediately**
when the kernel vcpu regains control.
True, in many circumstances, the overhead
incurred by the migration or save/restore will
expire the intended delay, and so perhaps serve
the same purpose as the intended delay, but
there may also be circumstances where this is
not true.

** Note that some clever coding in the Linux
kernel sources averts a much worse disaster,
namely a very extended spinwait for hours or
days or more!  This cleverness may not exist
in all kernels -- or in applications that might
implement a similar rdtsc-based __udelay()-like
technique.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: non-emulated rdtsc: a smoking gun!
  2009-10-22 20:53 non-emulated rdtsc: a smoking gun! Dan Magenheimer
@ 2009-10-22 21:24 ` Jeremy Fitzhardinge
  0 siblings, 0 replies; 2+ messages in thread
From: Jeremy Fitzhardinge @ 2009-10-22 21:24 UTC (permalink / raw)
  To: Dan Magenheimer
  Cc: Xen-devel, kurt.hackel, Jeremy Fitzhardinge, Keir Fraser,
	chris.mason, Avi Kivity

On 10/22/09 13:53, Dan Magenheimer wrote:
> I just found a Linux kernel use of rdtsc that
> MAY cause a significant failure if rdtsc is
> unemulated and a poorly timed migration (or
> save/restore) occurs under Xen or KVM.
>
> The problem is that a call to __udelay() --
> or any member of the delay() family -- may
> return prematurely**.  Since these functions
> "must guarantee that we wait at least the
> amount of time" specified, there are likely
> unknown kernel circumstances where a
> premature return will cause problems.
> (Disclaimer: I haven't gone through every use
> of every call site of every member of the delay
> function family to prove this.)
>   

It won't matter, as they're only used to control timing to external IO
devices.  If a domain has a passthrough device, it can't be migrated or
save/restored.  Nobody should be using them for software timing.

    J

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-10-22 21:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-10-22 20:53 non-emulated rdtsc: a smoking gun! Dan Magenheimer
2009-10-22 21:24 ` Jeremy Fitzhardinge

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.