2019-01-26 17:50:33

by Pasha Tatashin

[permalink] [raw]
Subject: [PATCH] x86/kvmclock: set offset for kvm unstable clock

VMs may show incorrect uptime and dmesg printk offsets on hypervisors with
unstable clock. The problem is produced when VM is rebooted without exiting
from qemu.

The fix is to calculate clock offset not only for stable clock but for
unstable clock as well, and use kvm_sched_clock_read() which substracts
the offset for both clocks.

This is safe, because pvclock_clocksource_read() does the right thing and
makes sure that clock always goes forward, so once offset is calculated
with unstable clock, we won't get new reads that are smaller than offset,
and thus won't get negative results.

Thank you Jon DeVree for helping to reproduce this issue.

Fixes: 857baa87b642 ("sched/clock: Enable sched clock early")

Reported-by: Dominique Martinet <[email protected]>
Signed-off-by: Pavel Tatashin <[email protected]>
---
arch/x86/kernel/kvmclock.c | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c
index e811d4d1c824..d908a37bf3f3 100644
--- a/arch/x86/kernel/kvmclock.c
+++ b/arch/x86/kernel/kvmclock.c
@@ -104,12 +104,8 @@ static u64 kvm_sched_clock_read(void)

static inline void kvm_sched_clock_init(bool stable)
{
- if (!stable) {
- pv_ops.time.sched_clock = kvm_clock_read;
+ if (!stable)
clear_sched_clock_stable();
- return;
- }
-
kvm_sched_clock_offset = kvm_clock_read();
pv_ops.time.sched_clock = kvm_sched_clock_read;

--
2.20.1



2019-01-28 09:16:47

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [PATCH] x86/kvmclock: set offset for kvm unstable clock

On 26/01/19 18:49, Pavel Tatashin wrote:
> VMs may show incorrect uptime and dmesg printk offsets on hypervisors with
> unstable clock. The problem is produced when VM is rebooted without exiting
> from qemu.
>
> The fix is to calculate clock offset not only for stable clock but for
> unstable clock as well, and use kvm_sched_clock_read() which substracts
> the offset for both clocks.
>
> This is safe, because pvclock_clocksource_read() does the right thing and
> makes sure that clock always goes forward, so once offset is calculated
> with unstable clock, we won't get new reads that are smaller than offset,
> and thus won't get negative results.
>
> Thank you Jon DeVree for helping to reproduce this issue.
>
> Fixes: 857baa87b642 ("sched/clock: Enable sched clock early")
>
> Reported-by: Dominique Martinet <[email protected]>
> Signed-off-by: Pavel Tatashin <[email protected]>
> ---
> arch/x86/kernel/kvmclock.c | 6 +-----
> 1 file changed, 1 insertion(+), 5 deletions(-)
>
> diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c
> index e811d4d1c824..d908a37bf3f3 100644
> --- a/arch/x86/kernel/kvmclock.c
> +++ b/arch/x86/kernel/kvmclock.c
> @@ -104,12 +104,8 @@ static u64 kvm_sched_clock_read(void)
>
> static inline void kvm_sched_clock_init(bool stable)
> {
> - if (!stable) {
> - pv_ops.time.sched_clock = kvm_clock_read;
> + if (!stable)
> clear_sched_clock_stable();
> - return;
> - }
> -
> kvm_sched_clock_offset = kvm_clock_read();
> pv_ops.time.sched_clock = kvm_sched_clock_read;
>
>

Queued, thanks.

Pa

2019-01-28 16:23:12

by Jon DeVree

[permalink] [raw]
Subject: Re: [PATCH] x86/kvmclock: set offset for kvm unstable clock

Thanks a bunch Pavel, patch works great for me on 5.0-rc4. I also
backported it to 4.19.18 and tested it there. Should this be CC'ed to
stable@ as well?

--
Jon
X(7): A program for managing terminal windows. See also screen(1) and tmux(1).

2019-01-30 16:08:43

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [PATCH] x86/kvmclock: set offset for kvm unstable clock

On 28/01/19 17:22, Jon DeVree wrote:
> Thanks a bunch Pavel, patch works great for me on 5.0-rc4. I also
> backported it to 4.19.18 and tested it there. Should this be CC'ed to
> stable@ as well?

Yes, I've queued the patch and added the stable tag. Thanks,

Paolo