[PATCH 3/9] KVM: selftests: Add memory size parameter to the demand paging test

Ben Gardon
2019-09-27T16:18:48Z

Add an argument to allow the demand paging test to work on larger and
smaller guest sizes.

Signed-off-by: Ben Gardon <bgardon@google.com>
---
.../selftests/kvm/demand_paging_test.c | 55 ++++++++++++-------
1 file changed, 34 insertions(+), 21 deletions(-)

diff --git a/tools/testing/selftests/kvm/demand_paging_test.c b/tools/testing/selftests/kvm/demand_paging_test.c
index 61ba4e6a8214a..19982a33a0ca2 100644
--- a/tools/testing/selftests/kvm/demand_paging_test.c
+++ b/tools/testing/selftests/kvm/demand_paging_test.c
@@ -32,6 +32,8 @@
/* Default guest test virtual memory offset */
#define DEFAULT_GUEST_TEST_MEM 0xc0000000

+#define DEFAULT_GUEST_TEST_MEM_SIZE (1 << 30) /* 1G */
+
#define __NR_userfaultfd 323

/*
@@ -255,10 +257,9 @@ static int setup_demand_paging(struct kvm_vm *vm,
return 0;
}

-#define GUEST_MEM_SHIFT 30 /* 1G */
#define PAGE_SHIFT_4K 12

-static void run_test(enum vm_guest_mode mode)
+static void run_test(enum vm_guest_mode mode, uint64_t guest_memory_bytes)
{
pthread_t vcpu_thread;
pthread_t uffd_handler_thread;
@@ -266,33 +267,40 @@ static void run_test(enum vm_guest_mode mode)
int r;

/*
- * We reserve page table for 2 times of extra dirty mem which
- * will definitely cover the original (1G+) test range. Here
- * we do the calculation with 4K page size which is the
- * smallest so the page number will be enough for all archs
- * (e.g., 64K page size guest will need even less memory for
- * page tables).
+ * We reserve page table for twice the ammount of memory we intend
+ * to use in the test region for demand paging. Here we do the
+ * calculation with 4K page size which is the smallest so the page
+ * number will be enough for all archs. (e.g., 64K page size guest
+ * will need even less memory for page tables).
*/
vm = create_vm(mode, VCPU_ID,
- 2ul << (GUEST_MEM_SHIFT - PAGE_SHIFT_4K),
+ (2 * guest_memory_bytes) >> PAGE_SHIFT_4K,
guest_code);

guest_page_size = vm_get_page_size(vm);
- /*
- * A little more than 1G of guest page sized pages. Cover the
- * case where the size is not aligned to 64 pages.
- */
- guest_num_pages = (1ul << (GUEST_MEM_SHIFT -
- vm_get_page_shift(vm))) + 16;
+
+ TEST_ASSERT(guest_memory_bytes % guest_page_size == 0,
+ "Guest memory size is not guest page size aligned.");
+
+ guest_num_pages = guest_memory_bytes / guest_page_size;
+
#ifdef __s390x__
/* Round up to multiple of 1M (segment size) */
guest_num_pages = (guest_num_pages + 0xff) & ~0xffUL;
#endif
+ /*
+ * If there should be more memory in the guest test region than there
+ * can be pages in the guest, it will definitely cause problems.
+ */
+ TEST_ASSERT(guest_num_pages < vm_get_max_gfn(vm),
+ "Requested more guest memory than address space allows.\n"
+ " guest pages: %lx max gfn: %lx\n",
+ guest_num_pages, vm_get_max_gfn(vm));

host_page_size = getpagesize();
- host_num_pages = (guest_num_pages * guest_page_size) / host_page_size +
- !!((guest_num_pages * guest_page_size) %
- host_page_size);
+ TEST_ASSERT(guest_memory_bytes % host_page_size == 0,
+ "Guest memory size is not host page size aligned.");
+ host_num_pages = guest_memory_bytes / host_page_size;

guest_test_phys_mem = (vm_get_max_gfn(vm) - guest_num_pages) *
guest_page_size;
@@ -369,7 +377,7 @@ static void help(char *name)
int i;

puts("");
- printf("usage: %s [-h] [-m mode]\n", name);
+ printf("usage: %s [-h] [-m mode] [-b bytes test memory]\n", name);
printf(" -m: specify the guest mode ID to test\n"
" (default: test all supported modes)\n"
" This option may be used multiple times.\n"
@@ -378,6 +386,8 @@ static void help(char *name)
printf(" %d: %s%s\n", i, vm_guest_mode_string(i),
vm_guest_mode_params[i].supported ? " (supported)" : "");
}
+ printf(" -b: specify the number of bytes of memory which should be\n"
+ " allocated to the guest.\n");
puts("");
exit(0);
}
@@ -385,6 +395,7 @@ static void help(char *name)
int main(int argc, char *argv[])
{
bool mode_selected = false;
+ uint64_t guest_memory_bytes = DEFAULT_GUEST_TEST_MEM_SIZE;
unsigned int mode;
int opt, i;
#ifdef __aarch64__
@@ -410,7 +421,7 @@ int main(int argc, char *argv[])
vm_guest_mode_params_init(VM_MODE_P40V48_4K, true, true);
#endif

- while ((opt = getopt(argc, argv, "hm:")) != -1) {
+ while ((opt = getopt(argc, argv, "hm:b:")) != -1) {
switch (opt) {
case 'm':
if (!mode_selected) {
@@ -423,6 +434,8 @@ int main(int argc, char *argv[])
"Guest mode ID %d too big", mode);
vm_guest_mode_params[mode].enabled = true;
break;
+ case 'b':
+ guest_memory_bytes = strtoull(optarg, NULL, 0);
case 'h':
default:
help(argv[0]);
@@ -436,7 +449,7 @@ int main(int argc, char *argv[])
TEST_ASSERT(vm_guest_mode_params[i].supported,
"Guest mode ID %d (%s) not supported.",
i, vm_guest_mode_string(i));
- run_test(i);
+ run_test(i, guest_memory_bytes);
}

return 0;
--
2.23.0.444.g18eeb5a265-goog