From: Ben Gardon <bgardon@google.com>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>, Peter Xu <peterx@redhat.com>,
Sean Christopherson <seanjc@google.com>,
Peter Shier <pshier@google.com>,
David Dunn <daviddunn@google.com>,
Junaid Shahid <junaids@google.com>,
Jim Mattson <jmattson@google.com>,
David Matlack <dmatlack@google.com>,
Mingwei Zhang <mizhang@google.com>,
Jing Zhang <jingzhangos@google.com>,
Ben Gardon <bgardon@google.com>
Subject: [PATCH v4 10/10] KVM: selftests: Test disabling NX hugepages on a VM
Date: Mon, 11 Apr 2022 14:10:15 -0700 [thread overview]
Message-ID: <20220411211015.3091615-11-bgardon@google.com> (raw)
In-Reply-To: <20220411211015.3091615-1-bgardon@google.com>
Add an argument to the NX huge pages test to test disabling the feature
on a VM using the new capability.
Signed-off-by: Ben Gardon <bgardon@google.com>
---
.../selftests/kvm/include/kvm_util_base.h | 2 +
tools/testing/selftests/kvm/lib/kvm_util.c | 19 ++++++-
.../selftests/kvm/x86_64/nx_huge_pages_test.c | 53 +++++++++++++++----
3 files changed, 64 insertions(+), 10 deletions(-)
diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
index f9c2ac0a5b97..15f24be6d93f 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_base.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
@@ -412,4 +412,6 @@ uint64_t vm_get_single_stat(struct kvm_vm *vm, const char *stat_name);
uint32_t guest_get_vcpuid(void);
+int vm_disable_nx_huge_pages(struct kvm_vm *vm);
+
#endif /* SELFTEST_KVM_UTIL_BASE_H */
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 833c7e63d62d..5fa5608eef03 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -112,6 +112,15 @@ int vm_check_cap(struct kvm_vm *vm, long cap)
return ret;
}
+static int __vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap)
+{
+ int ret;
+
+ ret = ioctl(vm->fd, KVM_ENABLE_CAP, cap);
+
+ return ret;
+}
+
/* VM Enable Capability
*
* Input Args:
@@ -128,7 +137,7 @@ int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap)
{
int ret;
- ret = ioctl(vm->fd, KVM_ENABLE_CAP, cap);
+ ret = __vm_enable_cap(vm, cap);
TEST_ASSERT(ret == 0, "KVM_ENABLE_CAP IOCTL failed,\n"
" rc: %i errno: %i", ret, errno);
@@ -2662,3 +2671,11 @@ uint64_t vm_get_single_stat(struct kvm_vm *vm, const char *stat_name)
stat_name, ret);
return data;
}
+
+int vm_disable_nx_huge_pages(struct kvm_vm *vm)
+{
+ struct kvm_enable_cap cap = { 0 };
+
+ cap.cap = KVM_CAP_VM_DISABLE_NX_HUGE_PAGES;
+ return __vm_enable_cap(vm, &cap);
+}
diff --git a/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c
index 3f21726b22c7..f8edf7910950 100644
--- a/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c
+++ b/tools/testing/selftests/kvm/x86_64/nx_huge_pages_test.c
@@ -13,6 +13,8 @@
#include <fcntl.h>
#include <stdint.h>
#include <time.h>
+#include <linux/reboot.h>
+#include <sys/syscall.h>
#include <test_util.h>
#include "kvm_util.h"
@@ -77,14 +79,41 @@ static void check_split_count(struct kvm_vm *vm, int expected_splits)
expected_splits, actual_splits);
}
-int main(int argc, char **argv)
+void run_test(bool disable_nx)
{
struct kvm_vm *vm;
struct timespec ts;
void *hva;
+ int r;
vm = vm_create_default(0, 0, guest_code);
+ if (disable_nx) {
+ kvm_check_cap(KVM_CAP_VM_DISABLE_NX_HUGE_PAGES);
+
+ /*
+ * Check if this process has the reboot permissions needed to
+ * disable NX huge pages on a VM.
+ *
+ * The reboot call below will never have any effect because
+ * the magic values are not set correctly, however the
+ * permission check is done before the magic value check.
+ */
+ r = syscall(SYS_reboot, 0, 0, 0, NULL);
+ if (errno == EPERM) {
+ r = vm_disable_nx_huge_pages(vm);
+ TEST_ASSERT(r == EPERM,
+ "This process should not have permission to disable NX huge pages");
+ return;
+ }
+
+ TEST_ASSERT(errno == EINVAL,
+ "Reboot syscall should fail with -EINVAL");
+
+ r = vm_disable_nx_huge_pages(vm);
+ TEST_ASSERT(!r, "Disabling NX huge pages should not fail if process has reboot permissions");
+ }
+
vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_HUGETLB,
HPAGE_GPA, HPAGE_SLOT,
HPAGE_SLOT_NPAGES, 0);
@@ -118,21 +147,21 @@ int main(int argc, char **argv)
* to be remapped at 4k.
*/
vcpu_run(vm, 0);
- check_2m_page_count(vm, 1);
- check_split_count(vm, 1);
+ check_2m_page_count(vm, disable_nx ? 2 : 1);
+ check_split_count(vm, disable_nx ? 0 : 1);
/*
* Executing from the third huge page (previously unaccessed) will
* cause part to be mapped at 4k.
*/
vcpu_run(vm, 0);
- check_2m_page_count(vm, 1);
- check_split_count(vm, 2);
+ check_2m_page_count(vm, disable_nx ? 3 : 1);
+ check_split_count(vm, disable_nx ? 0 : 2);
/* Reading from the first huge page again should have no effect. */
vcpu_run(vm, 0);
- check_2m_page_count(vm, 1);
- check_split_count(vm, 2);
+ check_2m_page_count(vm, disable_nx ? 3 : 1);
+ check_split_count(vm, disable_nx ? 0 : 2);
/*
* Give recovery thread time to run. The wrapper script sets
@@ -145,7 +174,7 @@ int main(int argc, char **argv)
/*
* Now that the reclaimer has run, all the split pages should be gone.
*/
- check_2m_page_count(vm, 1);
+ check_2m_page_count(vm, disable_nx ? 3 : 1);
check_split_count(vm, 0);
/*
@@ -153,10 +182,16 @@ int main(int argc, char **argv)
* reading from it causes a huge page mapping to be installed.
*/
vcpu_run(vm, 0);
- check_2m_page_count(vm, 2);
+ check_2m_page_count(vm, disable_nx ? 3 : 2);
check_split_count(vm, 0);
kvm_vm_free(vm);
+}
+
+int main(int argc, char **argv)
+{
+ run_test(false);
+ run_test(true);
return 0;
}
--
2.35.1.1178.g4f1659d476-goog
next prev parent reply other threads:[~2022-04-11 21:11 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-11 21:10 [PATCH v4 00/10] KVM: x86: Add a cap to disable " Ben Gardon
2022-04-11 21:10 ` [PATCH v4 01/10] KVM: selftests: Remove dynamic memory allocation for stats header Ben Gardon
2022-04-11 21:52 ` David Matlack
2022-04-11 22:50 ` Mingwei Zhang
2022-04-11 21:10 ` [PATCH v4 02/10] KVM: selftests: Read binary stats header in lib Ben Gardon
2022-04-11 21:55 ` David Matlack
2022-04-11 21:10 ` [PATCH v4 03/10] KVM: selftests: Read binary stats desc " Ben Gardon
2022-04-11 22:01 ` David Matlack
2022-04-12 0:54 ` Mingwei Zhang
2022-04-12 18:56 ` Ben Gardon
2022-04-12 19:02 ` Sean Christopherson
2022-04-12 20:02 ` Sean Christopherson
2022-04-12 22:12 ` Ben Gardon
2022-04-11 21:10 ` [PATCH v4 04/10] KVM: selftests: Read binary stat data " Ben Gardon
2022-04-11 22:14 ` David Matlack
2022-04-12 19:58 ` Ben Gardon
2022-04-12 1:25 ` Mingwei Zhang
2022-04-11 21:10 ` [PATCH v4 05/10] KVM: selftests: Add NX huge pages test Ben Gardon
2022-04-11 22:27 ` David Matlack
2022-04-12 22:11 ` Ben Gardon
2022-04-12 1:32 ` Mingwei Zhang
2022-04-12 21:51 ` Ben Gardon
2022-04-11 21:10 ` [PATCH v4 06/10] KVM: x86/MMU: Factor out updating NX hugepages state for a VM Ben Gardon
2022-04-11 21:10 ` [PATCH v4 07/10] KVM: x86/MMU: Allow NX huge pages to be disabled on a per-vm basis Ben Gardon
2022-04-12 17:54 ` Sean Christopherson
2022-04-11 21:10 ` [PATCH v4 08/10] KVM: x86: Fix errant brace in KVM capability handling Ben Gardon
2022-04-11 21:10 ` [PATCH v4 09/10] KVM: x86/MMU: Require reboot permission to disable NX hugepages Ben Gardon
2022-04-12 18:08 ` Sean Christopherson
2022-04-11 21:10 ` Ben Gardon [this message]
2022-04-11 22:37 ` [PATCH v4 10/10] KVM: selftests: Test disabling NX hugepages on a VM David Matlack
2022-04-11 21:15 ` [PATCH v4 00/10] KVM: x86: Add a cap to disable " Ben Gardon
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=20220411211015.3091615-11-bgardon@google.com \
--to=bgardon@google.com \
--cc=daviddunn@google.com \
--cc=dmatlack@google.com \
--cc=jingzhangos@google.com \
--cc=jmattson@google.com \
--cc=junaids@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mizhang@google.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=pshier@google.com \
--cc=seanjc@google.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
all inboxes | Powered by JetHome®