Skip to contentSkip to conversation

KVM: s390: extend selftests add more input checks

GIT PULL Janosch Frank 2 participants 10 messages Sep 5, 2019 – Sep 11, 2019

Subsystems (5)
Documentation KVM Kselftest s390 +1 more subsystems
lore
Share & tools
lore

Janosch Frank

author maintainer
#0 lore raw

Paolo, Radim,

Thomas extended the selftests and added some input checks to KVM s390 ioctls. Cornelia contributed the documentation changes needed after the added checks.

Please pull.

The following changes since commit 609488bc979f99f805f34e9a32c1e3b71179d10b:

Linux 5.3-rc2 (2019-07-28 12:47:02 -0700)

are available in the Git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/kvms390/linux.git tags/kvm-s390-next-5.4-1

for you to fetch changes up to 81cb736c0c928444e2f4707513c167d5d39844a4:

KVM: selftests: Test invalid bits in kvm_valid_regs and kvm_dirty_regs on s390x (2019-09-04 15:38:05 +0200)

----------------------------------------------------------------
* More selftests
* Improved KVM_S390_MEM_OP ioctl input checking
* Add kvm_valid_regs and kvm_dirty_regs invalid bit checking
----------------------------------------------------------------
Cornelia Huck (1):
KVM: s390: improve documentation for S390_MEM_OP

Thomas Huth (7):
KVM: selftests: Split ucall.c into architecture specific files
KVM: selftests: Implement ucall() for s390x
KVM: selftests: Enable dirty_log_test on s390x
KVM: s390: Test for bad access register and size at the start of S390_MEM_OP
KVM: selftests: Add a test for the KVM_S390_MEM_OP ioctl
KVM: s390: Disallow invalid bits in kvm_valid_regs and kvm_dirty_regs
KVM: selftests: Test invalid bits in kvm_valid_regs and kvm_dirty_regs on s390x

 Documentation/virt/kvm/api.txt                     |  14 +-
 arch/s390/include/uapi/asm/kvm.h                   |   6 +
 arch/s390/kvm/kvm-s390.c                           |   6 +-
 tools/testing/selftests/kvm/Makefile               |  10 +-
 tools/testing/selftests/kvm/dirty_log_test.c       |  61 +++++++-
 tools/testing/selftests/kvm/include/kvm_util.h     |   8 +-
 tools/testing/selftests/kvm/lib/aarch64/ucall.c    | 112 ++++++++++++++
 tools/testing/selftests/kvm/lib/s390x/ucall.c      |  56 +++++++
 tools/testing/selftests/kvm/lib/ucall.c            | 157 -------------------
 tools/testing/selftests/kvm/lib/x86_64/ucall.c     |  56 +++++++
 tools/testing/selftests/kvm/s390x/memop.c          | 166 +++++++++++++++++++++
 tools/testing/selftests/kvm/s390x/sync_regs_test.c |  36 ++++-
 12 files changed, 503 insertions(+), 185 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/lib/aarch64/ucall.c
 create mode 100644 tools/testing/selftests/kvm/lib/s390x/ucall.c
 delete mode 100644 tools/testing/selftests/kvm/lib/ucall.c
 create mode 100644 tools/testing/selftests/kvm/lib/x86_64/ucall.c
 create mode 100644 tools/testing/selftests/kvm/s390x/memop.c
author maintainer
#1 lore raw

[GIT PULL 1/8] KVM: selftests: Split ucall.c into architecture specific files

From: Thomas Huth <[email protected]>

The way we exit from a guest to userspace is very specific to the
architecture: On x86, we use PIO, on aarch64 we are using MMIO and on s390x we're going to use an instruction instead. The possibility to select a type via the ucall_type_t enum is currently also completely unused, so the code in ucall.c currently looks more complex than required. Let's split this up into architecture specific ucall.c files instead, so we can get rid of the #ifdefs and the unnecessary ucall_type_t handling.

---
 tools/testing/selftests/kvm/Makefile          |   6 +-
 tools/testing/selftests/kvm/dirty_log_test.c  |   2 +-
 .../testing/selftests/kvm/include/kvm_util.h  |   8 +-
 .../testing/selftests/kvm/lib/aarch64/ucall.c | 112 +++++++++++++
 tools/testing/selftests/kvm/lib/ucall.c       | 157 ------------------
 .../testing/selftests/kvm/lib/x86_64/ucall.c  |  56 +++++++
 6 files changed, 173 insertions(+), 168 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/lib/aarch64/ucall.c
 delete mode 100644 tools/testing/selftests/kvm/lib/ucall.c
 create mode 100644 tools/testing/selftests/kvm/lib/x86_64/ucall.c
tools/testing/selftests/kvm/Makefile +3 -3
index ba7849751989..a51e3b83df40 100644
@@ -7,9 +7,9 @@ top_srcdir = ../../../..
 KSFT_KHDR_INSTALL := 1
 UNAME_M := $(shell uname -m)
 
-LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/ucall.c lib/sparsebit.c
-LIBKVM_x86_64 = lib/x86_64/processor.c lib/x86_64/vmx.c
-LIBKVM_aarch64 = lib/aarch64/processor.c
+LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/sparsebit.c
+LIBKVM_x86_64 = lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/ucall.c
+LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c
 LIBKVM_s390x = lib/s390x/processor.c
 
 TEST_GEN_PROGS_x86_64 = x86_64/cr4_cpuid_sync_test
tools/testing/selftests/kvm/dirty_log_test.c +1 -1
index ceb52b952637..5d5ae1be4984 100644
@@ -337,7 +337,7 @@ static void run_test(enum vm_guest_mode mode, unsigned long iterations,
 	vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
 #endif
 #ifdef __aarch64__
-	ucall_init(vm, UCALL_MMIO, NULL);
+	ucall_init(vm, NULL);
 #endif
 
 	/* Export the shared variables to the guest */
tools/testing/selftests/kvm/include/kvm_util.h +1 -7
index e0e66b115ef2..5463b7896a0a 100644
@@ -165,12 +165,6 @@ int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd);
 	memcpy(&(g), _p, sizeof(g));				\
 })
 
-/* ucall implementation types */
-typedef enum {
-	UCALL_PIO,
-	UCALL_MMIO,
-} ucall_type_t;
-
 /* Common ucalls */
 enum {
 	UCALL_NONE,
@@ -186,7 +180,7 @@ struct ucall {
 	uint64_t args[UCALL_MAX_ARGS];
 };
 
-void ucall_init(struct kvm_vm *vm, ucall_type_t type, void *arg);
+void ucall_init(struct kvm_vm *vm, void *arg);
 void ucall_uninit(struct kvm_vm *vm);
 void ucall(uint64_t cmd, int nargs, ...);
 uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc);
tools/testing/selftests/kvm/lib/aarch64/ucall.c new file +112 -0

112 lines not on this page · raw message

tools/testing/selftests/kvm/lib/ucall.c deleted +0 -157

157 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86_64/ucall.c new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.20.1
author maintainer
#2 lore raw

[GIT PULL 3/8] KVM: selftests: Enable dirty_log_test on s390x

In reply to Janosch Frank

From: Thomas Huth <[email protected]>

To run the dirty_log_test on s390x, we have to make sure that we access the dirty log bitmap with little endian byte ordering and we have to properly align the memslot of the guest. Also all dirty bits of a segment are set once on s390x when one of the pages of a segment are written to for the first time, so we have to make sure that we touch all pages during the first iteration to keep the test in sync here. DEFAULT_GUEST_TEST_MEM needs an adjustment, too. On some s390x distributions, the ELF binary is linked to address 0x80000000, so we have to avoid that our test region overlaps into this area. 0xc0000000 seems to be a good alternative that should work on x86 and aarch64, too.

---
 tools/testing/selftests/kvm/Makefile         |  1 +
 tools/testing/selftests/kvm/dirty_log_test.c | 59 +++++++++++++++++---
 2 files changed, 53 insertions(+), 7 deletions(-)
tools/testing/selftests/kvm/Makefile +1 -0
index 75ea1ecbf85a..1b48a94b4350 100644
@@ -33,6 +33,7 @@ TEST_GEN_PROGS_aarch64 += dirty_log_test
 TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
 
 TEST_GEN_PROGS_s390x += s390x/sync_regs_test
+TEST_GEN_PROGS_s390x += dirty_log_test
 TEST_GEN_PROGS_s390x += kvm_create_max_vcpus
 
 TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
tools/testing/selftests/kvm/dirty_log_test.c +52 -7

117 lines not on this page · raw message

Signature · 1 line
2.20.1
author maintainer
#3 lore raw

[GIT PULL 5/8] KVM: s390: improve documentation for S390_MEM_OP

In reply to Janosch Frank

From: Cornelia Huck <[email protected]>

Explicitly specify the valid ranges for size and ar, and reword buf requirements a bit.

---
 Documentation/virt/kvm/api.txt | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
Documentation/virt/kvm/api.txt +8 -6
index 2d067767b617..76c9d6fdbfdb 100644
@@ -3079,12 +3079,14 @@ This exception is also raised directly at the corresponding VCPU if the
 flag KVM_S390_MEMOP_F_INJECT_EXCEPTION is set in the "flags" field.
 
 The start address of the memory region has to be specified in the "gaddr"
-field, and the length of the region in the "size" field. "buf" is the buffer
-supplied by the userspace application where the read data should be written
-to for KVM_S390_MEMOP_LOGICAL_READ, or where the data that should be written
-is stored for a KVM_S390_MEMOP_LOGICAL_WRITE. "buf" is unused and can be NULL
-when KVM_S390_MEMOP_F_CHECK_ONLY is specified. "ar" designates the access
-register number to be used.
+field, and the length of the region in the "size" field (which must not
+be 0). The maximum value for "size" can be obtained by checking the
+KVM_CAP_S390_MEM_OP capability. "buf" is the buffer supplied by the
+userspace application where the read data should be written to for
+KVM_S390_MEMOP_LOGICAL_READ, or where the data that should be written is
+stored for a KVM_S390_MEMOP_LOGICAL_WRITE. When KVM_S390_MEMOP_F_CHECK_ONLY
+is specified, "buf" is unused and can be NULL. "ar" designates the access
+register number to be used; the valid range is 0..15.
 
 The "reserved" field is meant for future extensions. It is not used by
 KVM with the currently defined set of flags.
Signature · 1 line
2.20.1
author maintainer
#4 lore raw

[GIT PULL 2/8] KVM: selftests: Implement ucall() for s390x

In reply to Janosch Frank

From: Thomas Huth <[email protected]>

On s390x, we can neither exit via PIO nor MMIO, but have to use an instruction like DIAGNOSE. Now that ucall() is implemented, we can use it in the sync_reg_test on s390x, too.

---
 tools/testing/selftests/kvm/Makefile          |  2 +-
 tools/testing/selftests/kvm/lib/s390x/ucall.c | 56 +++++++++++++++++++
 .../selftests/kvm/s390x/sync_regs_test.c      |  6 +-
 3 files changed, 61 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/lib/s390x/ucall.c
tools/testing/selftests/kvm/Makefile +1 -1
index a51e3b83df40..75ea1ecbf85a 100644
@@ -10,7 +10,7 @@ UNAME_M := $(shell uname -m)
 LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/sparsebit.c
 LIBKVM_x86_64 = lib/x86_64/processor.c lib/x86_64/vmx.c lib/x86_64/ucall.c
 LIBKVM_aarch64 = lib/aarch64/processor.c lib/aarch64/ucall.c
-LIBKVM_s390x = lib/s390x/processor.c
+LIBKVM_s390x = lib/s390x/processor.c lib/s390x/ucall.c
 
 TEST_GEN_PROGS_x86_64 = x86_64/cr4_cpuid_sync_test
 TEST_GEN_PROGS_x86_64 += x86_64/evmcs_test
tools/testing/selftests/kvm/lib/s390x/ucall.c new file +56 -0

56 lines not on this page · raw message

tools/testing/selftests/kvm/s390x/sync_regs_test.c +4 -2
index e85ff0d69548..bbc93094519b 100644
@@ -25,9 +25,11 @@
 
 static void guest_code(void)
 {
+	register u64 stage asm("11") = 0;
+
 	for (;;) {
-		asm volatile ("diag 0,0,0x501");
-		asm volatile ("ahi 11,1");
+		GUEST_SYNC(0);
+		asm volatile ("ahi %0,1" : : "r"(stage));
 	}
 }
 
Signature · 1 line
2.20.1
author maintainer
#5 lore raw

[GIT PULL 7/8] KVM: s390: Disallow invalid bits in kvm_valid_regs and kvm_dirty_regs

In reply to Janosch Frank

From: Thomas Huth <[email protected]>

If unknown bits are set in kvm_valid_regs or kvm_dirty_regs, this clearly indicates that something went wrong in the KVM userspace application. The x86 variant of KVM already contains a check for bad bits, so let's do the same on s390x now, too.

---
 arch/s390/include/uapi/asm/kvm.h | 6 ++++++
 arch/s390/kvm/kvm-s390.c         | 4 ++++
 2 files changed, 10 insertions(+)
arch/s390/include/uapi/asm/kvm.h +6 -0
index 47104e5b47fd..436ec7636927 100644
@@ -231,6 +231,12 @@ struct kvm_guest_debug_arch {
 #define KVM_SYNC_GSCB   (1UL << 9)
 #define KVM_SYNC_BPBC   (1UL << 10)
 #define KVM_SYNC_ETOKEN (1UL << 11)
+
+#define KVM_SYNC_S390_VALID_FIELDS \
+	(KVM_SYNC_PREFIX | KVM_SYNC_GPRS | KVM_SYNC_ACRS | KVM_SYNC_CRS | \
+	 KVM_SYNC_ARCH0 | KVM_SYNC_PFAULT | KVM_SYNC_VRS | KVM_SYNC_RICCB | \
+	 KVM_SYNC_FPRS | KVM_SYNC_GSCB | KVM_SYNC_BPBC | KVM_SYNC_ETOKEN)
+
 /* length and alignment of the sdnx as a power of two */
 #define SDNXC 8
 #define SDNXL (1UL << SDNXC)
arch/s390/kvm/kvm-s390.c +4 -0
index d41c091546b7..a3d4527ac23b 100644
@@ -4008,6 +4008,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
 	if (kvm_run->immediate_exit)
 		return -EINTR;
 
+	if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
+	    kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS)
+		return -EINVAL;
+
 	vcpu_load(vcpu);
 
 	if (guestdbg_exit_pending(vcpu)) {
Signature · 1 line
2.20.1
author maintainer
#6 lore raw

[GIT PULL 4/8] KVM: s390: Test for bad access register and size at the start of S390_MEM_OP

In reply to Janosch Frank

From: Thomas Huth <[email protected]>

If the KVM_S390_MEM_OP ioctl is called with an access register >= 16, then there is certainly a bug in the calling userspace application. We check for wrong access registers, but only if the vCPU was already in the access register mode before (i.e. the SIE block has recorded it). The check is also buried somewhere deep in the calling chain (in the function ar_translation()), so this is somewhat hard to find.

It's better to always report an error to the userspace in case this field is set wrong, and it's safer in the KVM code if we block wrong values here early instead of relying on a check somewhere deep down the calling chain, so let's add another check to kvm_s390_guest_mem_op() directly.

We also should check that the "size" is non-zero here (thanks to Janosch Frank for the hint!). If we do not check the size, we could call vmalloc() with this 0 value, and this will cause a kernel warning.

---
 arch/s390/kvm/kvm-s390.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/kvm/kvm-s390.c +1 -1
index 3f520cd837fb..d41c091546b7 100644
@@ -4265,7 +4265,7 @@ static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
 	const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
 				    | KVM_S390_MEMOP_F_CHECK_ONLY;
 
-	if (mop->flags & ~supported_flags)
+	if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size)
 		return -EINVAL;
 
 	if (mop->size > MEM_OP_MAX_SIZE)
Signature · 1 line
2.20.1
author maintainer
#7 lore raw

[GIT PULL 6/8] KVM: selftests: Add a test for the KVM_S390_MEM_OP ioctl

In reply to Janosch Frank

From: Thomas Huth <[email protected]>

Check that we can write and read the guest memory with this s390x ioctl, and that some error cases are handled correctly.

---
 tools/testing/selftests/kvm/Makefile      |   1 +
 tools/testing/selftests/kvm/s390x/memop.c | 166 ++++++++++++++++++++++
 2 files changed, 167 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/s390x/memop.c
tools/testing/selftests/kvm/Makefile +1 -0
index 1b48a94b4350..62c591f87dab 100644
@@ -32,6 +32,7 @@ TEST_GEN_PROGS_aarch64 += clear_dirty_log_test
 TEST_GEN_PROGS_aarch64 += dirty_log_test
 TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
 
+TEST_GEN_PROGS_s390x = s390x/memop
 TEST_GEN_PROGS_s390x += s390x/sync_regs_test
 TEST_GEN_PROGS_s390x += dirty_log_test
 TEST_GEN_PROGS_s390x += kvm_create_max_vcpus
tools/testing/selftests/kvm/s390x/memop.c new file +166 -0

166 lines not on this page · raw message

Signature · 1 line
2.20.1
author maintainer
#8 lore raw

[GIT PULL 8/8] KVM: selftests: Test invalid bits in kvm_valid_regs and kvm_dirty_regs on s390x

In reply to Janosch Frank

From: Thomas Huth <[email protected]>

Now that we disallow invalid bits in kvm_valid_regs and kvm_dirty_regs on s390x, too, we should also check this condition in the selftests. The code has been taken from the x86-version of the sync_regs_test.

---
 .../selftests/kvm/s390x/sync_regs_test.c      | 30 +++++++++++++++++++
 1 file changed, 30 insertions(+)
tools/testing/selftests/kvm/s390x/sync_regs_test.c +30 -0
index bbc93094519b..d5290b4ad636 100644
@@ -85,6 +85,36 @@ int main(int argc, char *argv[])
 
 	run = vcpu_state(vm, VCPU_ID);
 
+	/* Request reading invalid register set from VCPU. */
+	run->kvm_valid_regs = INVALID_SYNC_FIELD;
+	rv = _vcpu_run(vm, VCPU_ID);
+	TEST_ASSERT(rv < 0 && errno == EINVAL,
+		    "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
+		    rv);
+	vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0;
+
+	run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
+	rv = _vcpu_run(vm, VCPU_ID);
+	TEST_ASSERT(rv < 0 && errno == EINVAL,
+		    "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
+		    rv);
+	vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0;
+
+	/* Request setting invalid register set into VCPU. */
+	run->kvm_dirty_regs = INVALID_SYNC_FIELD;
+	rv = _vcpu_run(vm, VCPU_ID);
+	TEST_ASSERT(rv < 0 && errno == EINVAL,
+		    "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
+		    rv);
+	vcpu_state(vm, VCPU_ID)->kvm_dirty_regs = 0;
+
+	run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
+	rv = _vcpu_run(vm, VCPU_ID);
+	TEST_ASSERT(rv < 0 && errno == EINVAL,
+		    "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
+		    rv);
+	vcpu_state(vm, VCPU_ID)->kvm_dirty_regs = 0;
+
 	/* Request and verify all valid register sets. */
 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
 	rv = _vcpu_run(vm, VCPU_ID);
Signature · 1 line
2.20.1