From: Manali Shukla <manali.shukla@amd.com>
To: <kvm@vger.kernel.org>, <linux-kselftest@vger.kernel.org>
Cc: <pbonzini@redhat.com>, <seanjc@google.com>, <shuah@kernel.org>,
<nikunj@amd.com>, <thomas.lendacky@amd.com>,
<vkuznets@redhat.com>, <manali.shukla@amd.com>, <bp@alien8.de>,
<babu.moger@amd.com>
Subject: [RFC PATCH v1 4/4] KVM: selftests: Add bus lock exit test
Date: Tue, 9 Jul 2024 17:51:45 +0000 [thread overview]
Message-ID: <20240709175145.9986-5-manali.shukla@amd.com> (raw)
In-Reply-To: <20240709175145.9986-1-manali.shukla@amd.com>
From: Nikunj A Dadhania <nikunj@amd.com>
Malicious guests can cause bus locks to degrade the performance of
a system. The Bus Lock Threshold feature is beneficial for
hypervisors aiming to restrict the ability of the guests to perform
excessive bus locks and slow down the system for all the tenants.
Add a test case to verify the Bus Lock Threshold feature for SVM.
[Manali:
- The KVM_CAP_X86_BUS_LOCK_EXIT capability is not enabled while
vcpus are created, changed the VM and vCPU creation logic to
resolve the mentioned issue.
- Added nested guest test case for bus lock exit.
- massage commit message.
- misc cleanups. ]
Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
Co-developed-by: Manali Shukla <manali.shukla@amd.com>
Signed-off-by: Manali Shukla <manali.shukla@amd.com>
---
tools/testing/selftests/kvm/Makefile | 1 +
.../selftests/kvm/x86_64/svm_buslock_test.c | 114 ++++++++++++++++++
2 files changed, 115 insertions(+)
create mode 100644 tools/testing/selftests/kvm/x86_64/svm_buslock_test.c
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
index ce8ff8e8ce3a..711ec195e386 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -94,6 +94,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test
TEST_GEN_PROGS_x86_64 += x86_64/smm_test
TEST_GEN_PROGS_x86_64 += x86_64/state_test
TEST_GEN_PROGS_x86_64 += x86_64/vmx_preemption_timer_test
+TEST_GEN_PROGS_x86_64 += x86_64/svm_buslock_test
TEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test
TEST_GEN_PROGS_x86_64 += x86_64/svm_int_ctl_test
TEST_GEN_PROGS_x86_64 += x86_64/svm_nested_shutdown_test
diff --git a/tools/testing/selftests/kvm/x86_64/svm_buslock_test.c b/tools/testing/selftests/kvm/x86_64/svm_buslock_test.c
new file mode 100644
index 000000000000..dcb595999046
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86_64/svm_buslock_test.c
@@ -0,0 +1,114 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * svm_buslock_test
+ *
+ * Copyright (C) 2024 Advanced Micro Devices, Inc.
+ *
+ * SVM testing: Buslock exit
+ */
+
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+#include "svm_util.h"
+
+#define NO_ITERATIONS 100
+#define __cacheline_aligned __aligned(128)
+
+struct buslock_test {
+ unsigned char pad[126];
+ atomic_long_t val;
+} __packed;
+
+struct buslock_test test __cacheline_aligned;
+
+static __always_inline void buslock_atomic_add(int i, atomic_long_t *v)
+{
+ asm volatile(LOCK_PREFIX "addl %1,%0"
+ : "+m" (v->counter)
+ : "ir" (i) : "memory");
+}
+
+static void buslock_add(void)
+{
+ /*
+ * Increment a cache unaligned variable atomically.
+ * This should generate a bus lock exit.
+ */
+ for (int i = 0; i < NO_ITERATIONS; i++)
+ buslock_atomic_add(2, &test.val);
+}
+
+static void l2_guest_code(void)
+{
+ buslock_add();
+ GUEST_DONE();
+}
+
+static void l1_guest_code(struct svm_test_data *svm)
+{
+ #define L2_GUEST_STACK_SIZE 64
+ unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
+ struct vmcb *vmcb = svm->vmcb;
+
+ generic_svm_setup(svm, l2_guest_code,
+ &l2_guest_stack[L2_GUEST_STACK_SIZE]);
+ run_guest(vmcb, svm->vmcb_gpa);
+ GUEST_ASSERT(vmcb->control.exit_code == SVM_EXIT_VMMCALL);
+ GUEST_DONE();
+}
+
+static void guest_code(struct svm_test_data *svm)
+{
+ buslock_add();
+
+ if (this_cpu_has(X86_FEATURE_SVM))
+ l1_guest_code(svm);
+}
+
+int main(int argc, char *argv[])
+{
+ struct kvm_vcpu *vcpu;
+ struct kvm_run *run;
+ struct kvm_vm *vm;
+ vm_vaddr_t svm_gva;
+
+ TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
+ TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_BUS_LOCK_EXIT));
+
+ vm = vm_create(1);
+ vm_enable_cap(vm, KVM_CAP_X86_BUS_LOCK_EXIT, KVM_BUS_LOCK_DETECTION_EXIT);
+ vcpu = vm_vcpu_add(vm, 0, guest_code);
+
+ vcpu_alloc_svm(vm, &svm_gva);
+ vcpu_args_set(vcpu, 1, svm_gva);
+
+ run = vcpu->run;
+
+ for (;;) {
+ struct ucall uc;
+
+ vcpu_run(vcpu);
+
+ if (run->exit_reason == KVM_EXIT_X86_BUS_LOCK) {
+ run->flags &= ~KVM_RUN_X86_BUS_LOCK;
+ run->exit_reason = 0;
+ continue;
+ }
+
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ /* NOT REACHED */
+ case UCALL_SYNC:
+ break;
+ case UCALL_DONE:
+ goto done;
+ default:
+ TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
+ }
+ }
+done:
+ kvm_vm_free(vm);
+ return 0;
+}
--
2.34.1
next prev parent reply other threads:[~2024-07-09 17:52 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-09 17:51 [RFC PATCH v1 0/4] Add support for the Bus Lock Threshold Manali Shukla
2024-07-09 17:51 ` [RFC PATCH v1 1/4] x86/cpufeatures: Add CPUID feature bit " Manali Shukla
2024-08-16 19:37 ` Sean Christopherson
2024-08-22 9:43 ` Manali Shukla
2024-08-29 6:48 ` Borislav Petkov
2024-08-30 4:42 ` Sean Christopherson
2024-08-30 8:21 ` Borislav Petkov
2024-09-20 5:53 ` Manali Shukla
2024-07-09 17:51 ` [RFC PATCH v1 2/4] KVM: SVM: Enable Bus lock threshold exit Manali Shukla
2024-08-16 19:54 ` Sean Christopherson
2024-08-24 5:35 ` Manali Shukla
2024-08-26 16:15 ` Sean Christopherson
2024-08-29 6:37 ` Manali Shukla
2024-08-28 16:44 ` Manali Shukla
2024-07-09 17:51 ` [RFC PATCH v1 3/4] KVM: x86: nSVM: Implement support for nested Bus Lock Threshold Manali Shukla
2024-08-16 20:05 ` Sean Christopherson
2024-08-28 15:52 ` Manali Shukla
2024-08-16 20:14 ` Sean Christopherson
2024-08-29 14:32 ` Manali Shukla
2024-07-09 17:51 ` Manali Shukla [this message]
2024-08-16 20:21 ` [RFC PATCH v1 4/4] KVM: selftests: Add bus lock exit test Sean Christopherson
2024-08-26 10:29 ` Manali Shukla
2024-08-26 16:06 ` Sean Christopherson
2024-08-29 9:41 ` Manali Shukla
2024-07-30 4:52 ` [RFC PATCH v1 0/4] Add support for the Bus Lock Threshold Manali Shukla
2024-08-07 3:55 ` Manali Shukla
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=20240709175145.9986-5-manali.shukla@amd.com \
--to=manali.shukla@amd.com \
--cc=babu.moger@amd.com \
--cc=bp@alien8.de \
--cc=kvm@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=nikunj@amd.com \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
--cc=thomas.lendacky@amd.com \
--cc=vkuznets@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