From d8a20a54b05e2c9adb5198b1439608391450dd0b Mon Sep 17 00:00:00 2001 From: Louis Burda Date: Mon, 8 Aug 2022 19:21:29 +0200 Subject: Added ioctl interface and debuged single access evictions --- patch.diff | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 125 insertions(+), 16 deletions(-) (limited to 'patch.diff') diff --git a/patch.diff b/patch.diff index 45d19db..e4e4c16 100755 --- a/patch.diff +++ b/patch.diff @@ -1,7 +1,15 @@ diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile -index b804444e16d4..c94f8c4460f1 100644 +index b804444e16d4..17167ccfca22 100644 --- a/arch/x86/kvm/Makefile +++ b/arch/x86/kvm/Makefile +@@ -1,6 +1,6 @@ + # SPDX-License-Identifier: GPL-2.0 + +-ccflags-y += -Iarch/x86/kvm ++ccflags-y += -Iarch/x86/kvm -O2 + ccflags-$(CONFIG_KVM_WERROR) += -Werror + + ifeq ($(CONFIG_FRAME_POINTER),y) @@ -10,7 +10,9 @@ endif KVM := ../../../virt/kvm @@ -24,7 +32,7 @@ index b804444e16d4..c94f8c4460f1 100644 obj-$(CONFIG_KVM) += kvm.o obj-$(CONFIG_KVM_INTEL) += kvm-intel.o diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c -index 7b3cfbe8f7e3..f9a6b37eb36a 100644 +index 7b3cfbe8f7e3..71697d08e9e4 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2,6 +2,8 @@ @@ -36,7 +44,7 @@ index 7b3cfbe8f7e3..f9a6b37eb36a 100644 #include "irq.h" #include "mmu.h" #include "kvm_cache_regs.h" -@@ -3785,8 +3787,19 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu, +@@ -3785,8 +3787,18 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu, static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) { @@ -48,8 +56,7 @@ index 7b3cfbe8f7e3..f9a6b37eb36a 100644 + struct vcpu_svm *svm; + + printk(KERN_WARNING "CachePC: svm_cpu_enter_exit()\n"); -+ printk(KERN_WARNING "Vincent CachePC: svm_cpu_enter_exit()\n"); -+ cachepc_init_counters(); ++ + if (!ctx) ctx = cachepc_get_ctx(L1); + if (!ds) ds = cachepc_prepare_ds(ctx); @@ -57,7 +64,7 @@ index 7b3cfbe8f7e3..f9a6b37eb36a 100644 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; -@@ -3835,8 +3848,15 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) +@@ -3835,8 +3847,14 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) */ x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl); @@ -67,13 +74,12 @@ index 7b3cfbe8f7e3..f9a6b37eb36a 100644 + cachepc_probe(head); + //cachepc_print_msrmts(head); -+ printk(KERN_WARNING "Vincent: Saving measurements\n"); + cachepc_save_msrmts(head); + /* * We do not use IBRS in the kernel. If this vCPU has used the * SPEC_CTRL MSR it may have left it on; save the value and -@@ -3912,6 +3932,8 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) +@@ -3912,6 +3930,8 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) if (is_guest_mode(vcpu)) return EXIT_FASTPATH_NONE; @@ -83,7 +89,7 @@ index 7b3cfbe8f7e3..f9a6b37eb36a 100644 } diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c -index 2541a17ff1c4..1c3c3b63baba 100644 +index 2541a17ff1c4..116ca17af03a 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -51,6 +51,9 @@ @@ -124,7 +130,7 @@ index 2541a17ff1c4..1c3c3b63baba 100644 __visible bool kvm_rebooting; EXPORT_SYMBOL_GPL(kvm_rebooting); -@@ -4765,12 +4782,95 @@ static void check_processor_compat(void *data) +@@ -4765,12 +4782,197 @@ static void check_processor_compat(void *data) *c->ret = kvm_arch_check_processor_compat(c->opaque); } @@ -175,6 +181,78 @@ index 2541a17ff1c4..1c3c3b63baba 100644 +} + +void ++kvm_cachepc_single_access_test(void *p) ++{ ++ cacheline *ptr; ++ uint64_t pre, post; ++ volatile register uint64_t i asm("r11"); ++ int *cnt; ++ ++ cnt = p; ++ ++ ptr = cachepc_prepare_victim(cachepc_ctx, 48); ++ ++ cachepc_mfence(); ++ cachepc_cpuid(); ++ ++ cachepc_prime(cachepc_ds); ++ ++ cachepc_mfence(); ++ cachepc_cpuid(); ++ ++ for (i = 0; i < 100000000LLU; i++); ++ ++ cachepc_mfence(); ++ cachepc_cpuid(); ++ ++ pre = cachepc_readpmc(0); ++ ++ cachepc_mfence(); ++ cachepc_cpuid(); ++ ++ pre += cachepc_readpmc(1); ++ ++ cachepc_mfence(); ++ cachepc_cpuid(); ++ ++ pre += cachepc_readpmc(2); ++ ++ cachepc_mfence(); ++ cachepc_cpuid(); ++ ++ cachepc_victim(ptr); ++ ++ cachepc_mfence(); ++ cachepc_cpuid(); ++ ++ for (i = 0; i < 100000000LLU; i++); ++ ++ cachepc_mfence(); ++ cachepc_cpuid(); ++ ++ post = cachepc_readpmc(0); ++ ++ cachepc_mfence(); ++ cachepc_cpuid(); ++ ++ post += cachepc_readpmc(1); ++ ++ cachepc_mfence(); ++ cachepc_cpuid(); ++ ++ post += cachepc_readpmc(2); ++ ++ cachepc_mfence(); ++ cachepc_cpuid(); ++ ++ printk(KERN_WARNING "CachePC: Single access test done, result: %llu", post - pre); ++ ++ if (cnt) *cnt = post - pre; ++ ++ cachepc_release_victim(cachepc_ctx, ptr); ++} ++ ++void +kvm_cachepc_single_eviction_test(void *p) +{ + cacheline *head; @@ -182,11 +260,11 @@ index 2541a17ff1c4..1c3c3b63baba 100644 + + ptr = cachepc_prepare_victim(cachepc_ctx, 48); + head = cachepc_prime(cachepc_ds); -+ cachepc_victim(ptr); ++ //cachepc_victim(ptr); + cachepc_probe(head); + -+ printk(KERN_WARNING "CachePC: Test done, results:"); -+ cachepc_print_msrmts(head); ++ printk(KERN_WARNING "CachePC: Single eviction test done\n"); ++ //cachepc_print_msrmts(head); + cachepc_save_msrmts(head); + + cachepc_release_victim(cachepc_ctx, ptr); @@ -197,6 +275,8 @@ index 2541a17ff1c4..1c3c3b63baba 100644 +{ + int cpu; + ++ local_irq_disable(); ++ + cpu = get_cpu(); + + printk(KERN_WARNING "CachePC: Running on core %i\n", cpu); @@ -206,15 +286,43 @@ index 2541a17ff1c4..1c3c3b63baba 100644 + cachepc_ctx = cachepc_get_ctx(L1); + cachepc_ds = cachepc_prepare_ds(cachepc_ctx); + ++ kvm_cachepc_single_access_test(p); + kvm_cachepc_single_eviction_test(p); + + put_cpu(); ++ ++ local_irq_enable(); ++} ++ ++long ++kvm_cachepc_ioctl(struct file *file, unsigned int cmd, unsigned long argp) ++{ ++ int r; ++ void __user *arg_user; ++ int cnt; ++ ++ arg_user = (void __user *)argp; ++ switch (cmd) { ++ case CACHEPC_IOCTL_ACCESS_TEST: ++ printk(KERN_WARNING "CachePC: ioctl access test\n"); ++ r = smp_call_function_single(2, ++ kvm_cachepc_single_access_test, &cnt, true); ++ WARN_ON(r != 0); ++ if (arg_user) { ++ if (copy_to_user(arg_user, &cnt, sizeof(int))) ++ return -EFAULT; ++ } ++ break; ++ default: ++ return -EINVAL; ++ } ++ ++ return 0; +} + int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, struct module *module) { -+ printk(KERN_WARNING "Vincent: KVM Init called\n"); struct kvm_cpu_compat_check c; - int r; - int cpu; @@ -222,7 +330,7 @@ index 2541a17ff1c4..1c3c3b63baba 100644 r = kvm_arch_init(opaque); if (r) -@@ -4848,6 +4948,20 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, +@@ -4848,6 +5050,21 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align, r = kvm_vfio_ops_init(); WARN_ON(r); @@ -238,12 +346,13 @@ index 2541a17ff1c4..1c3c3b63baba 100644 + cachepc_proc_ops.proc_read = kvm_cachepc_read; + cachepc_proc_ops.proc_write = kvm_cachepc_write; + cachepc_proc_ops.proc_release = kvm_cachepc_close; ++ cachepc_proc_ops.proc_ioctl = kvm_cachepc_ioctl; + proc_create("cachepc", 0644, NULL, &cachepc_proc_ops); + return 0; out_unreg: -@@ -4872,6 +4986,12 @@ EXPORT_SYMBOL_GPL(kvm_init); +@@ -4872,6 +5089,12 @@ EXPORT_SYMBOL_GPL(kvm_init); void kvm_exit(void) { -- cgit v1.2.3-71-gd317