mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Greg KH <gregkh@suse.de>
To: linux-kernel@vger.kernel.org, stable@kernel.org, greg@kroah.com
Cc: stable-review@kernel.org, torvalds@linux-foundation.org,
	akpm@linux-foundation.org, alan@lxorguk.ukuu.org.uk,
	"Serge E. Hallyn" <serge.hallyn@canonical.com>,
	Zachary Amsden <zamsden@redhat.com>,
	Marcelo Tosatti <mtosatti@redhat.com>,
	avi@redhat.com
Subject: [142/143] KVM: x86: Fix a possible backwards warp of kvmclock
Date: Thu, 05 May 2011 17:22:07 -0700	[thread overview]
Message-ID: <20110506002219.202521963@clark.kroah.org> (raw)
In-Reply-To: <20110506002311.GA15354@kroah.com>

2.6.32-longterm review patch.  If anyone has any objections, please let us know.

------------------

From: Zachary Amsden <zamsden@redhat.com>

(backported from commit 1d5f066e0b63271b67eac6d3752f8aa96adcbddb)


Kernel time, which advances in discrete steps may progress much slower
than TSC.  As a result, when kvmclock is adjusted to a new base, the
apparent time to the guest, which runs at a much higher, nsec scaled
rate based on the current TSC, may have already been observed to have
a larger value (kernel_ns + scaled tsc) than the value to which we are
setting it (kernel_ns + 0).

We must instead compute the clock as potentially observed by the guest
for kernel_ns to make sure it does not go backwards.

Signed-off-by: Zachary Amsden <zamsden@redhat.com>
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

BugLink: http://bugs.launchpad.net/bugs/714335

Signed-off-by: Serge E. Hallyn <serge.hallyn@canonical.com>
Reviewed-by: Stefan Bader <stefan.bader@canonical.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

---
 arch/x86/include/asm/kvm_host.h |    3 ++
 arch/x86/kvm/x86.c              |   47 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 47 insertions(+), 3 deletions(-)

--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -357,6 +357,9 @@ struct kvm_vcpu_arch {
 	struct page *time_page;
 
 	bool singlestep; /* guest is single stepped by KVM */
+	u64 last_guest_tsc;
+	u64 last_kernel_ns;
+
 	bool nmi_pending;
 	bool nmi_injected;
 
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -47,6 +47,7 @@
 #include <asm/desc.h>
 #include <asm/mtrr.h>
 #include <asm/mce.h>
+#include <asm/pvclock.h>
 
 #define MAX_IO_MSRS 256
 #define CR0_RESERVED_BITS						\
@@ -633,6 +634,8 @@ static void kvm_write_guest_time(struct
 	struct kvm_vcpu_arch *vcpu = &v->arch;
 	void *shared_kaddr;
 	unsigned long this_tsc_khz;
+	s64 kernel_ns, max_kernel_ns;
+	u64 tsc_timestamp;
 
 	if ((!vcpu->time_page))
 		return;
@@ -646,15 +649,51 @@ static void kvm_write_guest_time(struct
 
 	/* Keep irq disabled to prevent changes to the clock */
 	local_irq_save(flags);
-	kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
+	kvm_get_msr(v, MSR_IA32_TSC, &tsc_timestamp);
 	ktime_get_ts(&ts);
 	monotonic_to_bootbased(&ts);
+	kernel_ns = timespec_to_ns(&ts);
 	local_irq_restore(flags);
 
+	/*
+	 * Time as measured by the TSC may go backwards when resetting the base
+	 * tsc_timestamp.  The reason for this is that the TSC resolution is
+	 * higher than the resolution of the other clock scales.  Thus, many
+	 * possible measurments of the TSC correspond to one measurement of any
+	 * other clock, and so a spread of values is possible.  This is not a
+	 * problem for the computation of the nanosecond clock; with TSC rates
+	 * around 1GHZ, there can only be a few cycles which correspond to one
+	 * nanosecond value, and any path through this code will inevitably
+	 * take longer than that.  However, with the kernel_ns value itself,
+	 * the precision may be much lower, down to HZ granularity.  If the
+	 * first sampling of TSC against kernel_ns ends in the low part of the
+	 * range, and the second in the high end of the range, we can get:
+	 *
+	 * (TSC - offset_low) * S + kns_old > (TSC - offset_high) * S + kns_new
+	 *
+	 * As the sampling errors potentially range in the thousands of cycles,
+	 * it is possible such a time value has already been observed by the
+	 * guest.  To protect against this, we must compute the system time as
+	 * observed by the guest and ensure the new system time is greater.
+	 */
+	max_kernel_ns = 0;
+	if (vcpu->hv_clock.tsc_timestamp && vcpu->last_guest_tsc) {
+		max_kernel_ns = vcpu->last_guest_tsc -
+			vcpu->hv_clock.tsc_timestamp;
+		max_kernel_ns = pvclock_scale_delta(max_kernel_ns,
+				vcpu->hv_clock.tsc_to_system_mul,
+				vcpu->hv_clock.tsc_shift);
+		max_kernel_ns += vcpu->last_kernel_ns;
+	}
+
+	if (max_kernel_ns > kernel_ns)
+		kernel_ns = max_kernel_ns;
+
 	/* With all the info we got, fill in the values */
 
-	vcpu->hv_clock.system_time = ts.tv_nsec +
-				     (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
+	vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
+	vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
+	vcpu->last_kernel_ns = kernel_ns;
 
 	/*
 	 * The interface expects us to write an even number signaling that the
@@ -3695,6 +3734,8 @@ static int vcpu_enter_guest(struct kvm_v
 	kvm_x86_ops->prepare_guest_switch(vcpu);
 	kvm_load_guest_fpu(vcpu);
 
+	kvm_get_msr(vcpu, MSR_IA32_TSC, &vcpu->arch.last_guest_tsc);
+
 	local_irq_disable();
 
 	clear_bit(KVM_REQ_KICK, &vcpu->requests);



  parent reply	other threads:[~2011-05-06  0:26 UTC|newest]

Thread overview: 144+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-06  0:23 [000/143] 2.6.32.40-longterm review Greg KH
2011-05-06  0:19 ` [001/143] ath: add missing regdomain pair 0x5c mapping Greg KH
2011-05-06  0:19 ` [002/143] block, blk-sysfs: Fix an err return path in blk_register_queue() Greg KH
2011-05-06  0:19 ` [003/143] p54: Initialize extra_len in p54_tx_80211 Greg KH
2011-05-06  0:19 ` [004/143] intel-iommu: Unlink domain from iommu Greg KH
2011-05-06  0:19 ` [005/143] intel-iommu: Fix get_domain_for_dev() error path Greg KH
2011-05-06  0:19 ` [006/143] drm/radeon/kms: fix bad shift in atom iio table parser Greg KH
2011-05-06  0:19 ` [007/143] NFS: nfs_wcc_update_inode() should set nfsi->attr_gencount Greg KH
2011-05-06  0:19 ` [008/143] serial/imx: read cts state only after acking cts change irq Greg KH
2011-05-06  0:19 ` [009/143] ASoC: Fix output PGA enabling in wm_hubs CODECs Greg KH
2011-05-06  0:19 ` [010/143] kconfig: Avoid buffer underrun in choice input Greg KH
2011-05-06  0:19 ` [011/143] UBIFS: fix master node recovery Greg KH
2011-05-06  0:19 ` [012/143] Remove extra struct page member from the buffer info structure Greg KH
2011-05-06  0:19 ` [013/143] [S390] dasd: correct device table Greg KH
2011-05-06  0:19 ` [014/143] iwlagn: Support new 5000 microcode Greg KH
2011-05-06  0:20 ` [015/143] atl1c: duplicate atl1c_get_tpd Greg KH
2011-05-06  0:20 ` [016/143] udp: Fix bogus UFO packet generation Greg KH
2011-05-06  0:20 ` [017/143] [PARISC] slub: fix panic with DISCONTIGMEM Greg KH
2011-05-06  0:20 ` [018/143] [PARISC] set memory ranges in N_NORMAL_MEMORY when onlined Greg KH
2011-05-06  0:20 ` [019/143] [media] FLEXCOP-PCI: fix __xlate_proc_name-warning for flexcop-pci Greg KH
2011-05-06  0:20 ` [020/143] m68k/mm: Set all online nodes in N_NORMAL_MEMORY Greg KH
2011-05-06  0:20 ` [021/143] nfs: dont lose MS_SYNCHRONOUS on remount of noac mount Greg KH
2011-05-06  0:20 ` [022/143] NFSv4.1: Ensure state manager thread dies on last umount Greg KH
2011-05-06  0:20 ` [023/143] agp: fix arbitrary kernel memory writes Greg KH
2011-05-06  0:20 ` [024/143] agp: fix OOM and buffer overflow Greg KH
2011-05-06  0:20 ` [025/143] Input: xen-kbdfront - fix mouse getting stuck after save/restore Greg KH
2011-05-06  0:20 ` [026/143] [SCSI] pmcraid: reject negative request size Greg KH
2011-05-06  0:20 ` [027/143] [SCSI] mpt2sas: prevent heap overflows and unchecked reads Greg KH
2011-05-06  0:20 ` [028/143] [SCSI] put stricter guards on queue dead checks Greg KH
2011-05-06  0:20 ` [029/143] mmc: sdhci-pci: Fix error case in sdhci_pci_probe_slot() Greg KH
2011-05-06  0:20 ` [030/143] mmc: sdhci: Check mrq->cmd in sdhci_tasklet_finish Greg KH
2011-05-06  0:20 ` [031/143] mmc: sdhci: Check mrq != NULL " Greg KH
2011-05-06  0:20 ` [032/143] USB: fix regression in usbip by setting has_tt flag Greg KH
2011-05-06  0:20 ` [033/143] x86, AMD: Fix APIC timer erratum 400 affecting K8 Rev.A-E processors Greg KH
2011-05-06  0:20 ` [034/143] af_unix: Only allow recv on connected seqpacket sockets Greg KH
2011-05-06  0:20 ` [035/143] ARM: 6891/1: prevent heap corruption in OABI semtimedop Greg KH
2011-05-06  0:20 ` [036/143] i8k: Tell gcc that *regs gets clobbered Greg KH
2011-05-06  0:20 ` [037/143] Fix gcc 4.5.1 miscompiling drivers/char/i8k.c (again) Greg KH
2011-05-06  0:20 ` [038/143] Open with O_CREAT flag set fails to open existing files on non writable directories Greg KH
2011-05-06  0:20 ` [039/143] can: Add missing socket check in can/bcm release Greg KH
2011-05-06  0:20 ` [040/143] fs/partitions/ldm.c: fix oops caused by corrupted partition table Greg KH
2011-05-06  0:20 ` [041/143] netxen: module firmware hints Greg KH
2011-05-06  0:20 ` [042/143] bnx2x: declare MODULE_FIRMWARE Greg KH
2011-05-06  0:20 ` [043/143] cxgb3: " Greg KH
2011-05-06  0:20 ` [044/143] myri10ge: " Greg KH
2011-05-06  0:20 ` [045/143] netx: " Greg KH
2011-05-06  0:20 ` [046/143] pcnet-cs: " Greg KH
2011-05-06  0:20 ` [047/143] spider-net: " Greg KH
2011-05-06  0:20 ` [048/143] tms380tr: " Greg KH
2011-05-06  0:20 ` [049/143] Input: Add support of Synaptics Clickpad device Greg KH
2011-05-06  0:20 ` [050/143] Input: elantech - do not advertise relative events Greg KH
2011-05-06  0:20 ` [051/143] Input: elantech - fix firmware version check Greg KH
2011-05-06  0:20 ` [052/143] Input: elantech - allow forcing Elantech protocol Greg KH
2011-05-06  0:20 ` [053/143] Input: elantech - ignore high bits in the position coordinates Greg KH
2011-05-06  0:20 ` [054/143] Input: elantech - use all 3 bytes when checking version Greg KH
2011-05-06  0:20 ` [055/143] Input: elantech - relax signature checks Greg KH
2011-05-06  0:20 ` [056/143] Input: elantech - discard the first 2 positions on some firmwares Greg KH
2011-05-06  0:20 ` [057/143] Staging: rtl8192su: check for skb == NULL Greg KH
2011-05-06  0:20 ` [058/143] Staging: rtl8192su: Clean up in case of an error in module initialisation Greg KH
2011-05-06  0:20 ` [059/143] Staging: rtl8192su: Fix procfs code for interfaces not named wlan0 Greg KH
2011-05-06  0:20 ` [060/143] Staging: rtl8192su: remove device ids Greg KH
2011-05-06  0:20 ` [061/143] Staging: rtl8192su: add " Greg KH
2011-05-06  0:20 ` [062/143] USB: retain USB device power/wakeup setting across reconfiguration Greg KH
2011-05-06  0:20 ` [063/143] USB: dont enable remote wakeup by default Greg KH
2011-05-06  0:20 ` [064/143] USB: teach "devices" file about Wireless and SuperSpeed USB Greg KH
2011-05-06  0:20 ` [065/143] GFS2: Clean up gfs2_adjust_quota() and do_glock() Greg KH
2011-05-06  0:20 ` [066/143] GFS2: Fix writing to non-page aligned gfs2_quota structures Greg KH
2011-05-06  0:20 ` [067/143] GFS2: BUG in gfs2_adjust_quota Greg KH
2011-05-06  0:20 ` [068/143] SUNRPC: fix NFS client over TCP hangs due to packet loss (Bug 16494) Greg KH
2011-05-06  0:20 ` [069/143] nfs4: Ensure that ACL pages sent over NFS were not allocated from the slab (v3) Greg KH
2011-05-06  0:20 ` [070/143] nfs: fix compilation warning Greg KH
2011-05-06  0:20 ` [071/143] Fix corrupted OSF partition table parsing Greg KH
2011-05-06  0:20 ` [072/143] Increase OSF partition limit from 8 to 18 Greg KH
2011-05-06  0:20 ` [073/143] Please add support for Microsoft MN-120 PCMCIA network card Greg KH
2011-05-06  0:20 ` [074/143] hwmon: (applesmc) Add iMac9,1 and MacBookPro2,2 support Greg KH
2011-05-06  0:21 ` [075/143] hwmon: (applesmc) Add support for MacBook Pro 5,3 and 5,4 Greg KH
2011-05-06  0:21 ` [076/143] hwmon: (applesmc) Add generic support for MacBook Pro 6 Greg KH
2011-05-06  0:21 ` [077/143] hwmon: (applesmc) Add generic support for MacBook Pro 7 Greg KH
2011-05-06  0:21 ` [078/143] hwmon: (applesmc) Add MacBookAir3,1(3,2) support Greg KH
2011-05-06  0:21 ` [079/143] ALSA: emux: Add trivial compat ioctl handler Greg KH
2011-05-06  0:21 ` [080/143] ALSA: powermac - Reverse HP detection on G4 DA Greg KH
2011-05-06  0:21 ` [081/143] ALSA: powermac - Lineout " Greg KH
2011-05-06  0:21 ` [082/143] ALSA: hda - Add support for the new 27 inch IMacs Greg KH
2011-05-06  0:21 ` [083/143] ALSA: hda - MacBookPro 5,3 line-in support Greg KH
2011-05-06  0:21 ` [084/143] ALSA: hda - Add model=mbp55 entry for MacBookPro 7,1 Greg KH
2011-05-06  0:21 ` [085/143] ALSA: hda - MacBookAir3,1(3,2) alsa support Greg KH
2011-05-06  0:21 ` [086/143] virtio_net: Add schedule check to napi_enable call Greg KH
2011-05-06  0:21 ` [087/143] virtio_net: fix oom handling on tx Greg KH
2011-05-06  0:21 ` [088/143] mac80211: Add define for TX headroom reserved by mac80211 itself Greg KH
2011-05-06  0:21 ` [089/143] rt2x00: Centralize setting of extra TX headroom requested by rt2x00 Greg KH
2011-05-06  0:21 ` [090/143] rt2x00: Properly request tx headroom for alignment operations Greg KH
2011-05-06  0:21 ` [091/143] rt2x00: use correct headroom for transmission Greg KH
2011-05-06  0:21 ` [092/143] Bluetooth: Add support Bluetooth controller of MacbookPro 6,2 Greg KH
2011-05-06  0:21 ` [093/143] Bluetooth: Add support Bluetooth controller of MacbookPro 7,1 Greg KH
2011-05-06  0:21 ` [094/143] Bluetooth: Add MacBookAir3,1(2) support Greg KH
2011-05-06  0:21 ` [095/143] perf tools: Display better error messages on missing packages Greg KH
2011-05-06  0:21 ` [096/143] perf tools: Add make DEBUG=1 to remove the -O6 cflag Greg KH
2011-05-06  0:21 ` [097/143] perf tools: Test -fstack-protector-all compiler option for inclusion in CFLAGS Greg KH
2011-05-06  0:21 ` [098/143] perf tools: Support static build Greg KH
2011-05-06  0:21 ` [099/143] perf tools: Add V=2 option to help debug config issues Greg KH
2011-05-06  0:21 ` [100/143] perf tools: Suggest static libraries as well Greg KH
2011-05-06  0:21 ` [101/143] perf: Use default compiler mode by default Greg KH
2011-05-06  0:21 ` [102/143] perf tools: Move QUIET_STDERR def to before first use Greg KH
2011-05-06  0:21 ` [103/143] perf tools: Check if /dev/null can be used as the -o gcc argument Greg KH
2011-05-06  0:21 ` [104/143] perf symbols: allow forcing use of cplus_demangle Greg KH
2011-05-06  0:21 ` [105/143] V4L/DVB: Add Elgato EyeTV Diversity to dibcom driver Greg KH
2011-05-06  0:21 ` [106/143] mmc: fix all hangs related to mmc/sd card insert/removal during suspend/resume Greg KH
2011-05-06  0:21 ` [107/143] mmc: build fix: mmc_pm_notify is only available with CONFIG_PM=y Greg KH
2011-05-06  0:21 ` [108/143] b43: Fix warning at drivers/mmc/core/core.c:237 in mmc_wait_for_cmd Greg KH
2011-05-06  0:21 ` [109/143] econet: Fix redeclaration of symbol len Greg KH
2011-05-06  0:21 ` [110/143] econet: fix CVE-2010-3848 Greg KH
2011-05-06  0:21 ` [111/143] dell-laptop: Add another Dell laptop to the DMI whitelist Greg KH
2011-05-06  0:21 ` [112/143] dell-laptop: Add another Dell laptop family " Greg KH
2011-05-06  0:21 ` [113/143] scsi_dh_emc: fix mode select request setup Greg KH
2011-05-06  0:21 ` [114/143] scsi_dh_emc: request flag cleanup Greg KH
2011-05-06  0:21 ` [115/143] cifs: fix another memleak, in cifs_root_iget Greg KH
2011-05-06  0:21 ` [116/143] e1000e: Reset 82577/82578 PHY before first PHY register read Greg KH
2011-05-06  0:21 ` [117/143] e1000: fix Tx hangs by disabling 64-bit DMA Greg KH
2011-05-06  0:21 ` [118/143] btrfs: Require CAP_SYS_ADMIN for filesystem rebalance Greg KH
2011-05-06  0:21 ` [119/143] af_unix: limit recursion level Greg KH
2011-05-06  0:21 ` [120/143] af_unix: limit unix_tot_inflight Greg KH
2011-05-06  0:21 ` [121/143] init, sched: Fix race between init and kthreadd Greg KH
2011-05-06  0:21 ` [122/143] backlight: MacBookAir3,1(3,2) mbp-nvidia-bl support Greg KH
2011-05-06  0:21 ` [123/143] bonding: Ensure that we unshare skbs prior to calling pskb_may_pull Greg KH
2011-05-06  0:21 ` [124/143] HID: add MacBookAir 3,1 and 3,2 support Greg KH
2011-05-06  0:21 ` [125/143] intel-iommu: Force-disable IOMMU for iGFX on broken Cantiga revisions Greg KH
2011-05-06  0:21 ` [126/143] ipg: Remove device claimed by dl2k from pci id table Greg KH
2011-05-06  0:21 ` [127/143] ipv6: Silence privacy extensions initialization Greg KH
2011-05-06  0:21 ` [128/143] l2tp: Fix UDP socket reference count bugs in the pppol2tp driver Greg KH
2011-05-06  0:21 ` [129/143] MIPS: DMA: Fix computation of DMA flags from devices coherent_dma_mask Greg KH
2011-05-06  0:21 ` [130/143] mpt2sas: fix the incorrect scsi_dma_map error checking Greg KH
2011-05-06  0:21 ` [131/143] niu: Fix kernel buffer overflow for ETHTOOL_GRXCLSRLALL Greg KH
2011-05-06  0:21 ` [132/143] Phonet: device notifier only runs on initial namespace Greg KH
2011-05-06  0:21 ` [133/143] powerpc/boot/dts: Install dts from the right directory Greg KH
2011-05-06  0:21 ` [134/143] rt2500usb: fallback to SW encryption for TKIP+AES Greg KH
2011-05-06  0:22 ` [135/143] sata_via: Delay on vt6420 when starting ATAPI DMA write Greg KH
2011-05-06  0:22 ` [136/143] tehuti: Firmware filename is tehuti/bdx.bin Greg KH
2011-05-06  0:22 ` [137/143] wireless: b43: fix error path in SDIO Greg KH
2011-05-06  0:22 ` [138/143] libata: set queue DMA alignment to sector size for ATAPI too Greg KH
2011-05-06  0:22 ` [139/143] usb: musb: core: set has_tt flag Greg KH
2011-05-06  0:22 ` [140/143] iwlwifi: fix skb usage after free Greg KH
2011-05-06  0:22 ` [141/143] x86: pvclock: Move scale_delta into common header Greg KH
2011-05-06  0:22 ` Greg KH [this message]
2011-05-06  0:22 ` [143/143] KVM: x86: Fix kvmclock bug Greg KH

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=20110506002219.202521963@clark.kroah.org \
    --to=gregkh@suse.de \
    --cc=akpm@linux-foundation.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=avi@redhat.com \
    --cc=greg@kroah.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    --cc=serge.hallyn@canonical.com \
    --cc=stable-review@kernel.org \
    --cc=stable@kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=zamsden@redhat.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®