linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Sean Christopherson <sean.j.christopherson@intel.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Sasha Levin <sashal@kernel.org>,
	kvm@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: [PATCH AUTOSEL 5.0 86/98] KVM: selftests: complete IO before migrating guest state
Date: Mon, 22 Apr 2019 15:41:53 -0400	[thread overview]
Message-ID: <20190422194205.10404-86-sashal@kernel.org> (raw)
In-Reply-To: <20190422194205.10404-1-sashal@kernel.org>

From: Sean Christopherson <sean.j.christopherson@intel.com>

[ Upstream commit 0f73bbc851ed32d22bbd86be09e0365c460bcd2e ]

Documentation/virtual/kvm/api.txt states:

  NOTE: For KVM_EXIT_IO, KVM_EXIT_MMIO, KVM_EXIT_OSI, KVM_EXIT_PAPR and
        KVM_EXIT_EPR the corresponding operations are complete (and guest
        state is consistent) only after userspace has re-entered the
        kernel with KVM_RUN.  The kernel side will first finish incomplete
        operations and then check for pending signals.  Userspace can
        re-enter the guest with an unmasked signal pending to complete
        pending operations.

Because guest state may be inconsistent, starting state migration after
an IO exit without first completing IO may result in test failures, e.g.
a proposed change to KVM's handling of %rip in its fast PIO handling[1]
will cause the new VM, i.e. the post-migration VM, to have its %rip set
to the IN instruction that triggered KVM_EXIT_IO, leading to a test
assertion due to a stage mismatch.

For simplicitly, require KVM_CAP_IMMEDIATE_EXIT to complete IO and skip
the test if it's not available.  The addition of KVM_CAP_IMMEDIATE_EXIT
predates the state selftest by more than a year.

[1] https://patchwork.kernel.org/patch/10848545/

Fixes: fa3899add1056 ("kvm: selftests: add basic test for state save and restore")
Reported-by: Jim Mattson <jmattson@google.com>
Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Sasha Levin (Microsoft) <sashal@kernel.org>
---
 tools/testing/selftests/kvm/include/kvm_util.h |  1 +
 tools/testing/selftests/kvm/lib/kvm_util.c     | 16 ++++++++++++++++
 .../testing/selftests/kvm/x86_64/state_test.c  | 18 ++++++++++++++++--
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index a84785b02557..07b71ad9734a 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -102,6 +102,7 @@ vm_paddr_t addr_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva);
 struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid);
 void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid);
 int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid);
+void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid);
 void vcpu_set_mp_state(struct kvm_vm *vm, uint32_t vcpuid,
 		       struct kvm_mp_state *mp_state);
 void vcpu_regs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs);
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index b52cfdefecbf..efa0aad8b3c6 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -1121,6 +1121,22 @@ int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
 	return rc;
 }
 
+void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid)
+{
+	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
+	int ret;
+
+	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
+
+	vcpu->state->immediate_exit = 1;
+	ret = ioctl(vcpu->fd, KVM_RUN, NULL);
+	vcpu->state->immediate_exit = 0;
+
+	TEST_ASSERT(ret == -1 && errno == EINTR,
+		    "KVM_RUN IOCTL didn't exit immediately, rc: %i, errno: %i",
+		    ret, errno);
+}
+
 /*
  * VM VCPU Set MP State
  *
diff --git a/tools/testing/selftests/kvm/x86_64/state_test.c b/tools/testing/selftests/kvm/x86_64/state_test.c
index 4b3f556265f1..30f75856cf39 100644
--- a/tools/testing/selftests/kvm/x86_64/state_test.c
+++ b/tools/testing/selftests/kvm/x86_64/state_test.c
@@ -134,6 +134,11 @@ int main(int argc, char *argv[])
 
 	struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
 
+	if (!kvm_check_cap(KVM_CAP_IMMEDIATE_EXIT)) {
+		fprintf(stderr, "immediate_exit not available, skipping test\n");
+		exit(KSFT_SKIP);
+	}
+
 	/* Create VM */
 	vm = vm_create_default(VCPU_ID, 0, guest_code);
 	vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
@@ -156,8 +161,6 @@ int main(int argc, char *argv[])
 			    stage, run->exit_reason,
 			    exit_reason_str(run->exit_reason));
 
-		memset(&regs1, 0, sizeof(regs1));
-		vcpu_regs_get(vm, VCPU_ID, &regs1);
 		switch (get_ucall(vm, VCPU_ID, &uc)) {
 		case UCALL_ABORT:
 			TEST_ASSERT(false, "%s at %s:%d", (const char *)uc.args[0],
@@ -176,6 +179,17 @@ int main(int argc, char *argv[])
 			    uc.args[1] == stage, "Unexpected register values vmexit #%lx, got %lx",
 			    stage, (ulong)uc.args[1]);
 
+		/*
+		 * When KVM exits to userspace with KVM_EXIT_IO, KVM guarantees
+		 * guest state is consistent only after userspace re-enters the
+		 * kernel with KVM_RUN.  Complete IO prior to migrating state
+		 * to a new VM.
+		 */
+		vcpu_run_complete_io(vm, VCPU_ID);
+
+		memset(&regs1, 0, sizeof(regs1));
+		vcpu_regs_get(vm, VCPU_ID, &regs1);
+
 		state = vcpu_save_state(vm, VCPU_ID);
 		kvm_vm_release(vm);
 
-- 
2.19.1


  parent reply	other threads:[~2019-04-22 19:44 UTC|newest]

Thread overview: 104+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-22 19:40 [PATCH AUTOSEL 5.0 01/98] arm64: dts: renesas: r8a77990: Fix SCIF5 DMA channels Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 02/98] ARM: dts: bcm283x: Fix hdmi hpd gpio pull Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 03/98] s390: limit brk randomization to 32MB Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 04/98] mt76x02: fix hdr pointer in write txwi for USB Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 05/98] mt76: mt76x2: fix external LNA gain settings Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 06/98] mt76: mt76x2: fix 2.4 GHz channel " Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 07/98] net: ieee802154: fix a potential NULL pointer dereference Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 08/98] ieee802154: hwsim: propagate genlmsg_reply return code Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 09/98] Btrfs: fix file corruption after snapshotting due to mix of buffered/DIO writes Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 10/98] net: stmmac: don't set own bit too early for jumbo frames Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 11/98] net: stmmac: fix jumbo frame sending with non-linear skbs Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 12/98] qlcnic: Avoid potential NULL pointer dereference Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 13/98] xsk: fix umem memory leak on cleanup Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 14/98] staging: axis-fifo: add CONFIG_OF dependency Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 15/98] staging, mt7621-pci: fix build without pci support Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 16/98] netfilter: nft_set_rbtree: check for inactive element after flag mismatch Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 17/98] netfilter: bridge: set skb transport_header before entering NF_INET_PRE_ROUTING Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 18/98] netfilter: fix NETFILTER_XT_TARGET_TEE dependencies Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 19/98] netfilter: ip6t_srh: fix NULL pointer dereferences Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 20/98] netfilter: nf_tables: bogus EBUSY in helper removal from transaction Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 21/98] s390/qeth: fix race when initializing the IP address table Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 22/98] ARM: imx51: fix a leaked reference by adding missing of_node_put Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 23/98] sc16is7xx: missing unregister/delete driver on error in sc16is7xx_init() Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 24/98] serial: ar933x_uart: Fix build failure with disabled console Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 25/98] KVM: arm64: Reset the PMU in preemptible context Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 26/98] arm64: KVM: Always set ICH_HCR_EL2.EN if GICv4 is enabled Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 27/98] KVM: arm/arm64: vgic-its: Take the srcu lock when writing to guest memory Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 28/98] KVM: arm/arm64: vgic-its: Take the srcu lock when parsing the memslots Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 29/98] KVM: arm/arm64: Enforce PTE mappings at stage2 when needed Sasha Levin
2019-04-23  9:27   ` Suzuki K Poulose
2019-05-02 12:50     ` Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 30/98] usb: dwc3: pci: add support for Comet Lake PCH ID Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 31/98] usb: gadget: net2280: Fix overrun of OUT messages Sasha Levin
2019-04-22 19:40 ` [PATCH AUTOSEL 5.0 32/98] usb: gadget: net2280: Fix net2280_dequeue() Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 33/98] usb: gadget: net2272: Fix net2272_dequeue() Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 34/98] ARM: dts: pfla02: increase phy reset duration Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 35/98] i2c: i801: Add support for Intel Comet Lake Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 36/98] KVM: arm/arm64: Fix handling of stage2 huge mappings Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 37/98] net: ks8851: Dequeue RX packets explicitly Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 38/98] net: ks8851: Reassert reset pin if chip ID check fails Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 39/98] net: ks8851: Delay requesting IRQ until opened Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 40/98] net: ks8851: Set initial carrier state to down Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 41/98] staging: rtl8188eu: Fix potential NULL pointer dereference of kcalloc Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 42/98] staging: rtlwifi: rtl8822b: fix to avoid potential NULL pointer dereference Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 43/98] staging: rtl8712: uninitialized memory in read_bbreg_hdl() Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 44/98] staging: rtlwifi: Fix potential NULL pointer dereference of kzalloc Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 45/98] net: phy: Add DP83825I to the DP83822 driver Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 46/98] net: macb: Add null check for PCLK and HCLK Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 47/98] net/sched: don't dereference a->goto_chain to read the chain index Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 48/98] ARM: dts: imx6qdl: Fix typo in imx6qdl-icore-rqs.dtsi Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 49/98] drm/tegra: hub: Fix dereference before check Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 50/98] NFS: Fix a typo in nfs_init_timeout_values() Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 51/98] net: xilinx: fix possible object reference leak Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 52/98] net: ibm: " Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 53/98] net: ethernet: ti: " Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 54/98] drm: Fix drm_release() and device unplug Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 55/98] gpio: aspeed: fix a potential NULL pointer dereference Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 56/98] drm/meson: Fix invalid pointer in meson_drv_unbind() Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 57/98] drm/meson: Uninstall IRQ handler Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 58/98] ARM: davinci: fix build failure with allnoconfig Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 59/98] sbitmap: order READ/WRITE freed instance and setting clear bit Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 60/98] staging: vc04_services: Fix an error code in vchiq_probe() Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 61/98] libceph: fix breakage caused by multipage bvecs Sasha Levin
2019-04-23  8:28   ` Ilya Dryomov
2019-05-02 12:51     ` Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 62/98] scsi: mpt3sas: Fix kernel panic during expander reset Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 63/98] scsi: aacraid: Insure we don't access PCIe space during AER/EEH Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 64/98] scsi: qla4xxx: fix a potential NULL pointer dereference Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 65/98] usb: usb251xb: fix to avoid " Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 66/98] leds: trigger: netdev: fix refcnt leak on interface rename Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 67/98] SUNRPC: fix uninitialized variable warning Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 68/98] x86/realmode: Don't leak the trampoline kernel address Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 69/98] usb: u132-hcd: fix resource leak Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 70/98] tty: fix NULL pointer issue when tty_port ops is not set Sasha Levin
2019-04-22 20:40   ` Greg Kroah-Hartman
2019-05-02 12:53     ` Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 71/98] ceph: fix use-after-free on symlink traversal Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 72/98] scsi: zfcp: reduce flood of fcrscn1 trace records on multi-element RSCN Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 73/98] x86/mm: Don't exceed the valid physical address space Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 74/98] libata: fix using DMA buffers on stack Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 75/98] kbuild: skip parsing pre sub-make code for recursion Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 76/98] afs: Fix StoreData op marshalling Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 77/98] gpio: of: Check propname before applying "cs-gpios" quirks Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 78/98] gpio: of: Check for "spi-cs-high" in child instead of parent node Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 79/98] KVM: nVMX: Do not inherit quadrant and invalid for the root shadow EPT Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 80/98] KVM: SVM: Workaround errata#1096 (insn_len maybe zero on SMAP violation) Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 81/98] kvm/x86: Move MSR_IA32_ARCH_CAPABILITIES to array emulated_msrs Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 82/98] x86/kvm/hyper-v: avoid spurious pending stimer on vCPU init Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 83/98] KVM: selftests: assert on exit reason in CR4/cpuid sync test Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 84/98] KVM: selftests: explicitly disable PIE for tests Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 85/98] KVM: selftests: disable stack protector for all KVM tests Sasha Levin
2019-04-22 19:41 ` Sasha Levin [this message]
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 87/98] gpio: of: Fix of_gpiochip_add() error path Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 88/98] nvme-multipath: relax ANA state check Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 89/98] nvmet: fix building bvec from sg list Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 90/98] nvmet: fix error flow during ns enable Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 91/98] perf cs-etm: Add missing case value Sasha Levin
2019-04-22 19:41 ` [PATCH AUTOSEL 5.0 92/98] perf machine: Update kernel map address and re-order properly Sasha Levin
2019-04-22 19:42 ` [PATCH AUTOSEL 5.0 93/98] kconfig/[mn]conf: handle backspace (^H) key Sasha Levin
2019-04-22 19:42 ` [PATCH AUTOSEL 5.0 94/98] iommu/amd: Reserve exclusion range in iova-domain Sasha Levin
2019-04-22 19:42 ` [PATCH AUTOSEL 5.0 95/98] kasan: fix variable 'tag' set but not used warning Sasha Levin
2019-04-22 19:42 ` [PATCH AUTOSEL 5.0 96/98] ptrace: take into account saved_sigmask in PTRACE{GET,SET}SIGMASK Sasha Levin
2019-04-22 19:42 ` [PATCH AUTOSEL 5.0 97/98] leds: pca9532: fix a potential NULL pointer dereference Sasha Levin
2019-04-22 19:42 ` [PATCH AUTOSEL 5.0 98/98] leds: trigger: netdev: use memcpy in device_name_store Sasha Levin

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=20190422194205.10404-86-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=sean.j.christopherson@intel.com \
    --cc=stable@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).