History

Merge tag 'for-linus-6.0' of git://git.kernel.org/pub/scm/virt/kvm/kvm

Pull kvm fixes from Paolo Bonzini: "A small fix to the reported set of supported CPUID bits, and selftests fixes: - Skip tests that require EPT when it is not available - Do not hang when a test fails with an empty stack trace - avoid spurious failure when running access_tracking_perf_test in a KVM guest - work around GCC's tendency to optimize loops into mem*() functions, which breaks because the guest code in selftests cannot call into PLTs - fix -Warray-bounds error in fix_hypercall_test" * tag 'for-linus-6.0' of git://git.kernel.org/pub/scm/virt/kvm/kvm: KVM: selftests: Compare insn opcodes directly in fix_hypercall_test KVM: selftests: Implement memcmp(), memcpy(), and memset() for guest use KVM: x86: Hide IA32_PLATFORM_DCA_CAP[31:0] from the guest KVM: selftests: Gracefully handle empty stack traces KVM: selftests: replace assertion with warning in access_tracking_perf_test KVM: selftests: Skip tests that require EPT when it is not available

Linus Torvalds committed 4 years ago

Commit 920541b2 parents 70575e7 39426508 files · +115 −37Browse files

Unified · Split

8 files
  1. arch/x86/kvm/cpuid.c +0 −2
  2. tools/testing/selftests/kvm/Makefile +10 −1
  3. tools/testing/selftests/kvm/access_tracking_perf_test.c +16 −9
  4. tools/testing/selftests/kvm/include/x86_64/vmx.h +1 −0
  5. tools/testing/selftests/kvm/lib/assert.c +13 −7
  6. tools/testing/selftests/kvm/lib/string_override.c +39 −0
  7. tools/testing/selftests/kvm/lib/x86_64/vmx.c +20 −0
  8. tools/testing/selftests/kvm/x86_64/fix_hypercall_test.c +16 −18
arch/x86/kvm/cpuid.c+0−2
@@ -902,8 +902,6 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) · 901 unmodified lines
902 entry->edx = 0;902 entry->edx = 0;
903 }903 }
904 break;904 break;
905 case 9:
906 break;
907 case 0xa: { /* Architectural Performance Monitoring */905 case 0xa: { /* Architectural Performance Monitoring */
908 union cpuid10_eax eax;906 union cpuid10_eax eax;
909 union cpuid10_edx edx;907 union cpuid10_edx edx;
tools/testing/selftests/kvm/Makefile+10−1
@@ -48,6 +48,8 @@ LIBKVM += lib/rbtree.c · 47 unmodified lines
48LIBKVM += lib/sparsebit.c48LIBKVM += lib/sparsebit.c
49LIBKVM += lib/test_util.c49LIBKVM += lib/test_util.c
50 50
51LIBKVM_STRING += lib/string_override.c
52
51LIBKVM_x86_64 += lib/x86_64/apic.c53LIBKVM_x86_64 += lib/x86_64/apic.c
52LIBKVM_x86_64 += lib/x86_64/handlers.S54LIBKVM_x86_64 += lib/x86_64/handlers.S
53LIBKVM_x86_64 += lib/x86_64/perf_test_util.c55LIBKVM_x86_64 += lib/x86_64/perf_test_util.c
@@ -220,7 +222,8 @@ LIBKVM_C := $(filter %.c,$(LIBKVM)) · 166 unmodified lines
220LIBKVM_S := $(filter %.S,$(LIBKVM))222LIBKVM_S := $(filter %.S,$(LIBKVM))
221LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C))223LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C))
222LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S))224LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S))
223LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ)225LIBKVM_STRING_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING))
226LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ)
224 227
225EXTRA_CLEAN += $(LIBKVM_OBJS) cscope.*228EXTRA_CLEAN += $(LIBKVM_OBJS) cscope.*
226 229
@@ -231,6 +234,12 @@ $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c · 4 unmodified lines
231$(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S234$(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S
232 $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@235 $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
233 236
237# Compile the string overrides as freestanding to prevent the compiler from
238# generating self-referential code, e.g. without "freestanding" the compiler may
239# "optimize" memcmp() by invoking memcmp(), thus causing infinite recursion.
240$(LIBKVM_STRING_OBJ): $(OUTPUT)/%.o: %.c
241 $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -ffreestanding $< -o $@
242
234x := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS))))243x := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS))))
235$(TEST_GEN_PROGS): $(LIBKVM_OBJS)244$(TEST_GEN_PROGS): $(LIBKVM_OBJS)
236$(TEST_GEN_PROGS_EXTENDED): $(LIBKVM_OBJS)245$(TEST_GEN_PROGS_EXTENDED): $(LIBKVM_OBJS)
tools/testing/selftests/kvm/access_tracking_perf_test.c+16−9
@@ -31,8 +31,9 @@ · 30 unmodified lines
31 * These limitations are worked around in this test by using a large enough31 * These limitations are worked around in this test by using a large enough
32 * region of memory for each vCPU such that the number of translations cached in32 * region of memory for each vCPU such that the number of translations cached in
33 * the TLB and the number of pages held in pagevecs are a small fraction of the33 * the TLB and the number of pages held in pagevecs are a small fraction of the
34 * overall workload. And if either of those conditions are not true this test34 * overall workload. And if either of those conditions are not true (for example
35 * will fail rather than silently passing.35 * in nesting, where TLB size is unlimited) this test will print a warning
36 * rather than silently passing.
36 */37 */
37#include <inttypes.h>38#include <inttypes.h>
38#include <limits.h>39#include <limits.h>
@@ -172,17 +173,23 @@ static void mark_vcpu_memory_idle(struct kvm_vm *vm, · 133 unmodified lines
172 vcpu_idx, no_pfn, pages);173 vcpu_idx, no_pfn, pages);
173 174
174 /*175 /*
175 * Test that at least 90% of memory has been marked idle (the rest might176 * Check that at least 90% of memory has been marked idle (the rest
176 * not be marked idle because the pages have not yet made it to an LRU177 * might not be marked idle because the pages have not yet made it to an
177 * list or the translations are still cached in the TLB). 90% is178 * LRU list or the translations are still cached in the TLB). 90% is
178 * arbitrary; high enough that we ensure most memory access went through179 * arbitrary; high enough that we ensure most memory access went through
179 * access tracking but low enough as to not make the test too brittle180 * access tracking but low enough as to not make the test too brittle
180 * over time and across architectures.181 * over time and across architectures.
182 *
183 * Note that when run in nested virtualization, this check will trigger
184 * much more frequently because TLB size is unlimited and since no flush
185 * happens, much more pages are cached there and guest won't see the
186 * "idle" bit cleared.
181 */187 */
182 TEST_ASSERT(still_idle < pages / 10,188 if (still_idle < pages / 10)
183 "vCPU%d: Too many pages still idle (%"PRIu64 " out of %"189 printf("WARNING: vCPU%d: Too many pages still idle (%" PRIu64
184 PRIu64 ").\n",190 "out of %" PRIu64 "), this will affect performance results"
185 vcpu_idx, still_idle, pages);191 ".\n",
192 vcpu_idx, still_idle, pages);
186 193
187 close(page_idle_fd);194 close(page_idle_fd);
188 close(pagemap_fd);195 close(pagemap_fd);
tools/testing/selftests/kvm/include/x86_64/vmx.h+1−0
@@ -617,6 +617,7 @@ void nested_map_memslot(struct vmx_pages *vmx, struct kvm_vm *vm, · 616 unmodified lines
617 uint32_t memslot);617 uint32_t memslot);
618void nested_identity_map_1g(struct vmx_pages *vmx, struct kvm_vm *vm,618void nested_identity_map_1g(struct vmx_pages *vmx, struct kvm_vm *vm,
619 uint64_t addr, uint64_t size);619 uint64_t addr, uint64_t size);
620bool kvm_vm_has_ept(struct kvm_vm *vm);
620void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm,621void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm,
621 uint32_t eptp_memslot);622 uint32_t eptp_memslot);
622void prepare_virtualize_apic_accesses(struct vmx_pages *vmx, struct kvm_vm *vm);623void prepare_virtualize_apic_accesses(struct vmx_pages *vmx, struct kvm_vm *vm);
tools/testing/selftests/kvm/lib/assert.c+13−7
@@ -22,7 +22,7 @@ static void test_dump_stack(void) · 21 unmodified lines
22 * Build and run this command:22 * Build and run this command:
23 *23 *
24 * addr2line -s -e /proc/$PPID/exe -fpai {backtrace addresses} | \24 * addr2line -s -e /proc/$PPID/exe -fpai {backtrace addresses} | \
25 * grep -v test_dump_stack | cat -n 1>&225 * cat -n 1>&2
26 *26 *
27 * Note that the spacing is different and there's no newline.27 * Note that the spacing is different and there's no newline.
28 */28 */
@@ -36,18 +36,24 @@ static void test_dump_stack(void) · 7 unmodified lines
36 n * (((sizeof(void *)) * 2) + 1) +36 n * (((sizeof(void *)) * 2) + 1) +
37 /* Null terminator: */37 /* Null terminator: */
38 1];38 1];
39 char *c;39 char *c = cmd;
40 40
41 n = backtrace(stack, n);41 n = backtrace(stack, n);
42 c = &cmd[0];
43 c += sprintf(c, "%s", addr2line);
44 /*42 /*
45 * Skip the first 3 frames: backtrace, test_dump_stack, and43 * Skip the first 2 frames, which should be test_dump_stack() and
46 * test_assert. We hope that backtrace isn't inlined and the other two44 * test_assert(); both of which are declared noinline. Bail if the
47 * we've declared noinline.45 * resulting stack trace would be empty. Otherwise, addr2line will block
46 * waiting for addresses to be passed in via stdin.
48 */47 */
48 if (n <= 2) {
49 fputs(" (stack trace empty)\n", stderr);
50 return;
51 }
52
53 c += sprintf(c, "%s", addr2line);
49 for (i = 2; i < n; i++)54 for (i = 2; i < n; i++)
50 c += sprintf(c, " %lx", ((unsigned long) stack[i]) - 1);55 c += sprintf(c, " %lx", ((unsigned long) stack[i]) - 1);
56
51 c += sprintf(c, "%s", pipeline);57 c += sprintf(c, "%s", pipeline);
52#pragma GCC diagnostic push58#pragma GCC diagnostic push
53#pragma GCC diagnostic ignored "-Wunused-result"59#pragma GCC diagnostic ignored "-Wunused-result"
tools/testing/selftests/kvm/lib/string_override.c+39−0
@@ -0,0 +1,39 @@
1// SPDX-License-Identifier: GPL-2.0-only
2#include <stddef.h>
3
4/*
5 * Override the "basic" built-in string helpers so that they can be used in
6 * guest code. KVM selftests don't support dynamic loading in guest code and
7 * will jump into the weeds if the compiler decides to insert an out-of-line
8 * call via the PLT.
9 */
10int memcmp(const void *cs, const void *ct, size_t count)
11{
12 const unsigned char *su1, *su2;
13 int res = 0;
14
15 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) {
16 if ((res = *su1 - *su2) != 0)
17 break;
18 }
19 return res;
20}
21
22void *memcpy(void *dest, const void *src, size_t count)
23{
24 char *tmp = dest;
25 const char *s = src;
26
27 while (count--)
28 *tmp++ = *s++;
29 return dest;
30}
31
32void *memset(void *s, int c, size_t count)
33{
34 char *xs = s;
35
36 while (count--)
37 *xs++ = c;
38 return s;
39}
tools/testing/selftests/kvm/lib/x86_64/vmx.c+20−0
@@ -5,6 +5,8 @@ · 4 unmodified lines
5 * Copyright (C) 2018, Google LLC.5 * Copyright (C) 2018, Google LLC.
6 */6 */
7 7
8#include <asm/msr-index.h>
9
8#include "test_util.h"10#include "test_util.h"
9#include "kvm_util.h"11#include "kvm_util.h"
10#include "processor.h"12#include "processor.h"
@@ -542,9 +544,27 @@ void nested_identity_map_1g(struct vmx_pages *vmx, struct kvm_vm *vm, · 531 unmodified lines
542 __nested_map(vmx, vm, addr, addr, size, PG_LEVEL_1G);544 __nested_map(vmx, vm, addr, addr, size, PG_LEVEL_1G);
543}545}
544 546
547bool kvm_vm_has_ept(struct kvm_vm *vm)
548{
549 struct kvm_vcpu *vcpu;
550 uint64_t ctrl;
551
552 vcpu = list_first_entry(&vm->vcpus, struct kvm_vcpu, list);
553 TEST_ASSERT(vcpu, "Cannot determine EPT support without vCPUs.\n");
554
555 ctrl = vcpu_get_msr(vcpu, MSR_IA32_VMX_TRUE_PROCBASED_CTLS) >> 32;
556 if (!(ctrl & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
557 return false;
558
559 ctrl = vcpu_get_msr(vcpu, MSR_IA32_VMX_PROCBASED_CTLS2) >> 32;
560 return ctrl & SECONDARY_EXEC_ENABLE_EPT;
561}
562
545void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm,563void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm,
546 uint32_t eptp_memslot)564 uint32_t eptp_memslot)
547{565{
566 TEST_REQUIRE(kvm_vm_has_ept(vm));
567
548 vmx->eptp = (void *)vm_vaddr_alloc_page(vm);568 vmx->eptp = (void *)vm_vaddr_alloc_page(vm);
549 vmx->eptp_hva = addr_gva2hva(vm, (uintptr_t)vmx->eptp);569 vmx->eptp_hva = addr_gva2hva(vm, (uintptr_t)vmx->eptp);
550 vmx->eptp_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->eptp);570 vmx->eptp_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->eptp);
tools/testing/selftests/kvm/x86_64/fix_hypercall_test.c+16−18
@@ -14,6 +14,9 @@ · 13 unmodified lines
14#include "kvm_util.h"14#include "kvm_util.h"
15#include "processor.h"15#include "processor.h"
16 16
17/* VMCALL and VMMCALL are both 3-byte opcodes. */
18#define HYPERCALL_INSN_SIZE 3
19
17static bool ud_expected;20static bool ud_expected;
18 21
19static void guest_ud_handler(struct ex_regs *regs)22static void guest_ud_handler(struct ex_regs *regs)
@@ -22,7 +25,7 @@ static void guest_ud_handler(struct ex_regs *regs) · 2 unmodified lines
22 GUEST_DONE();25 GUEST_DONE();
23}26}
24 27
25extern unsigned char svm_hypercall_insn;28extern uint8_t svm_hypercall_insn[HYPERCALL_INSN_SIZE];
26static uint64_t svm_do_sched_yield(uint8_t apic_id)29static uint64_t svm_do_sched_yield(uint8_t apic_id)
27{30{
28 uint64_t ret;31 uint64_t ret;
@@ -39,7 +42,7 @@ static uint64_t svm_do_sched_yield(uint8_t apic_id) · 10 unmodified lines
39 return ret;42 return ret;
40}43}
41 44
42extern unsigned char vmx_hypercall_insn;45extern uint8_t vmx_hypercall_insn[HYPERCALL_INSN_SIZE];
43static uint64_t vmx_do_sched_yield(uint8_t apic_id)46static uint64_t vmx_do_sched_yield(uint8_t apic_id)
44{47{
45 uint64_t ret;48 uint64_t ret;
@@ -56,30 +59,20 @@ static uint64_t vmx_do_sched_yield(uint8_t apic_id) · 10 unmodified lines
56 return ret;59 return ret;
57}60}
58 61
59static void assert_hypercall_insn(unsigned char *exp_insn, unsigned char *obs_insn)
60{
61 uint32_t exp = 0, obs = 0;
62
63 memcpy(&exp, exp_insn, sizeof(exp));
64 memcpy(&obs, obs_insn, sizeof(obs));
65
66 GUEST_ASSERT_EQ(exp, obs);
67}
68
69static void guest_main(void)62static void guest_main(void)
70{63{
71 unsigned char *native_hypercall_insn, *hypercall_insn;64 uint8_t *native_hypercall_insn, *hypercall_insn;
72 uint8_t apic_id;65 uint8_t apic_id;
73 66
74 apic_id = GET_APIC_ID_FIELD(xapic_read_reg(APIC_ID));67 apic_id = GET_APIC_ID_FIELD(xapic_read_reg(APIC_ID));
75 68
76 if (is_intel_cpu()) {69 if (is_intel_cpu()) {
77 native_hypercall_insn = &vmx_hypercall_insn;70 native_hypercall_insn = vmx_hypercall_insn;
78 hypercall_insn = &svm_hypercall_insn;71 hypercall_insn = svm_hypercall_insn;
79 svm_do_sched_yield(apic_id);72 svm_do_sched_yield(apic_id);
80 } else if (is_amd_cpu()) {73 } else if (is_amd_cpu()) {
81 native_hypercall_insn = &svm_hypercall_insn;74 native_hypercall_insn = svm_hypercall_insn;
82 hypercall_insn = &vmx_hypercall_insn;75 hypercall_insn = vmx_hypercall_insn;
83 vmx_do_sched_yield(apic_id);76 vmx_do_sched_yield(apic_id);
84 } else {77 } else {
85 GUEST_ASSERT(0);78 GUEST_ASSERT(0);
@@ -87,8 +80,13 @@ static void guest_main(void) · 1 unmodified line
87 return;80 return;
88 }81 }
89 82
83 /*
84 * The hypercall didn't #UD (guest_ud_handler() signals "done" if a #UD
85 * occurs). Verify that a #UD is NOT expected and that KVM patched in
86 * the native hypercall.
87 */
90 GUEST_ASSERT(!ud_expected);88 GUEST_ASSERT(!ud_expected);
91 assert_hypercall_insn(native_hypercall_insn, hypercall_insn);89 GUEST_ASSERT(!memcmp(native_hypercall_insn, hypercall_insn, HYPERCALL_INSN_SIZE));
92 GUEST_DONE();90 GUEST_DONE();
93}91}
94 92

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t