mbox series

[RFC,0/2] kvm: Use host timekeeping in guest.

Message ID 20190920062713.78503-1-suleiman@google.com (mailing list archive)
Headers show
Series kvm: Use host timekeeping in guest. | expand

Message

Suleiman Souhlal Sept. 20, 2019, 6:27 a.m. UTC
This RFC is to try to solve the following problem:

We have some applications that are currently running in their
own namespace, that still talk to other processes on the
machine, using IPC, and expect to run on the same machine.

We want to move them into a virtual machine, for the usual
benefits of virtualization.

However, some of these programs use CLOCK_MONOTONIC and 
CLOCK_BOOTTIME timestamps, as part of their protocol, when talking
to the host.

Generally speaking, we have multiple event sources, for example
sensors, input devices, display controller vsync, etc and we would
like to rely on them in the guest for various scenarios.

As a specific example, we are trying to run some wayland clients
(in the guest) who talk to the server (in the host), and the server
gives input events based on host time. Additionally, there are also
vsync events that the clients use for timing their rendering.

Another use case we have are timestamps from IIO sensors and cameras.
There are applications that need to determine how the timestamps
relate to the current time and the only way to get current time is
clock_gettime(), which would return a value from a different time
domain than the timestamps.

In this case, it is not feasible to change these programs, due to
the number of the places we would have to change.

We spent some time thinking about this, and the best solution we
could come up with was the following:

Make the guest kernel return the same CLOCK_MONOTONIC and
CLOCK_GETTIME timestamps as the host.

To do that, I am changing kvmclock to request to the host to copy
its timekeeping parameters (mult, base, cycle_last, etc), so that
the guest timekeeper can use the same values, so that time can
be synchronized between the guest and the host.

Any suggestions or feedback would be highly appreciated.

Suleiman Souhlal (2):
  kvm: Mechanism to copy host timekeeping parameters into guest.
  x86/kvmclock: Use host timekeeping.

 arch/x86/Kconfig                     |   9 ++
 arch/x86/include/asm/kvm_host.h      |   3 +
 arch/x86/include/asm/kvmclock.h      |   2 +
 arch/x86/include/asm/pvclock-abi.h   |  27 ++++++
 arch/x86/include/uapi/asm/kvm_para.h |   1 +
 arch/x86/kernel/kvmclock.c           | 127 ++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c                   | 121 +++++++++++++++++++++++++
 kernel/time/timekeeping.c            |  21 +++++
 8 files changed, 307 insertions(+), 4 deletions(-)

Comments

Paolo Bonzini Sept. 20, 2019, 7:48 a.m. UTC | #1
On 20/09/19 08:27, Suleiman Souhlal wrote:
> To do that, I am changing kvmclock to request to the host to copy
> its timekeeping parameters (mult, base, cycle_last, etc), so that
> the guest timekeeper can use the same values, so that time can
> be synchronized between the guest and the host.
> 
> Any suggestions or feedback would be highly appreciated.

I'm not a timekeeping maintainer, but I don't think the
kernel/time/timekeeping.c changes are acceptable.

Is the PTP driver not enough?

Paolo
Thomas Gleixner Sept. 20, 2019, 10:23 a.m. UTC | #2
On Fri, 20 Sep 2019, Paolo Bonzini wrote:

> On 20/09/19 08:27, Suleiman Souhlal wrote:
> > To do that, I am changing kvmclock to request to the host to copy
> > its timekeeping parameters (mult, base, cycle_last, etc), so that
> > the guest timekeeper can use the same values, so that time can
> > be synchronized between the guest and the host.
> > 
> > Any suggestions or feedback would be highly appreciated.
> 
> I'm not a timekeeping maintainer, but I don't think the
> kernel/time/timekeeping.c changes are acceptable.

Indeed. #ifdef WHATEVERTHEHECK does not go anywhere. If at all this needs
to be a runtime switch, but I have yet to understand the whole picture of
this.

Thanks,

	tglx
Suleiman Souhlal Sept. 24, 2019, 8:08 a.m. UTC | #3
On Fri, Sep 20, 2019 at 7:23 PM Thomas Gleixner <tglx@linutronix.de> wrote:
>
> On Fri, 20 Sep 2019, Paolo Bonzini wrote:
>
> > On 20/09/19 08:27, Suleiman Souhlal wrote:
> > > To do that, I am changing kvmclock to request to the host to copy
> > > its timekeeping parameters (mult, base, cycle_last, etc), so that
> > > the guest timekeeper can use the same values, so that time can
> > > be synchronized between the guest and the host.
> > >
> > > Any suggestions or feedback would be highly appreciated.
> >
> > I'm not a timekeeping maintainer, but I don't think the
> > kernel/time/timekeeping.c changes are acceptable.
>
> Indeed. #ifdef WHATEVERTHEHECK does not go anywhere. If at all this needs
> to be a runtime switch, but I have yet to understand the whole picture of
> this.

Yeah, I will try to make this a runtime switch.

As for the PTP driver, I don't think it will work for us because we
need CLOCK_MONOTONIC and CLOCK_BOOTTIME to match the host, and from my
understanding, PTP doesn't solve that.

Thanks,
-- Suleiman