Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: Wainer dos Santos Moschetta <wainersm@redhat.com>
To: pbonzini@redhat.com, kvm@vger.kernel.org
Cc: drjones@redhat.com, sean.j.christopherson@intel.com,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	krish.sadhukhan@oracle.com
Subject: [PATCH 1/1] selftests: kvm: Add overlapped memory regions test
Date: Wed, 15 Apr 2020 17:45:05 -0300	[thread overview]
Message-ID: <20200415204505.10021-2-wainersm@redhat.com> (raw)
In-Reply-To: <20200415204505.10021-1-wainersm@redhat.com>

Add the test_overlap_memory_regions() test case in
set_memory_region_test. This should check that overlapping
memory regions on the guest physical address cannot be added.

Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
---
 .../selftests/kvm/set_memory_region_test.c    | 75 ++++++++++++++++++-
 1 file changed, 74 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c
index 260e638826dc..74a987002273 100644
--- a/tools/testing/selftests/kvm/set_memory_region_test.c
+++ b/tools/testing/selftests/kvm/set_memory_region_test.c
@@ -331,6 +331,8 @@ static void test_add_max_memory_regions(void)
 	uint64_t mem_reg_npages;
 	void *mem;
 
+	pr_info("Testing KVM_CAP_NR_MEMSLOTS memory regions can be added\n");
+
 	max_mem_slots = kvm_check_cap(KVM_CAP_NR_MEMSLOTS);
 	TEST_ASSERT(max_mem_slots > 0,
 		    "KVM_CAP_NR_MEMSLOTS should be greater than 0");
@@ -338,7 +340,8 @@ static void test_add_max_memory_regions(void)
 
 	vm = vm_create(VM_MODE_DEFAULT, 0, O_RDWR);
 
-	mem_reg_npages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, MEM_REGION_SIZE);
+	mem_reg_npages = vm_calc_num_guest_pages(VM_MODE_DEFAULT,
+						 MEM_REGION_SIZE);
 
 	/* Check it can be added memory slots up to the maximum allowed */
 	pr_info("Adding slots 0..%i, each memory region with %dK size\n",
@@ -365,6 +368,75 @@ static void test_add_max_memory_regions(void)
 	kvm_vm_free(vm);
 }
 
+/*
+ * Test it cannot add memory slots with overlapped regions.
+ *
+ * The following cases are covered:
+ *
+ *             0x100000 0x300000
+ *       0x0       0x200000  0x400000
+ * slot0 |         |---2MB--|           (SUCCESS)
+ * slot1       |---2MB--|               (FAIL)
+ * slot2 |---2MB--|                     (SUCCESS)
+ * slot3           |---2MB--|           (FAIL)
+ * slot4                |---2MB--|      (FAIL)
+ * slot5                     |---2MB--| (SUCCESS)
+ */
+void test_overlap_memory_regions(void)
+{
+	int i;
+	int ret;
+	int vm_fd;
+	struct kvm_userspace_memory_region kvm_region;
+	struct kvm_vm *vm;
+	struct slot_t {
+		uint64_t guest_addr;
+		int exp_ret; /* Expected ioctl return value */
+	};
+	struct slot_t slots[] = {{0x200000,  0}, {0x100000, -1}, {0x000000,  0},
+				 {0x200000, -1}, {0x300000, -1}, {0x400000,  0}
+				};
+	uint64_t mem_reg_npages;
+	void *mem;
+
+	pr_info("Testing KVM_SET_USER_MEMORY_REGION with overlapped memory regions\n");
+
+	vm = vm_create(VM_MODE_DEFAULT, 0, O_RDWR);
+	vm_fd = vm_get_fd(vm);
+
+	pr_info("Working with memory region of %iMB\n", MEM_REGION_SIZE >> 20);
+	mem_reg_npages = vm_calc_num_guest_pages(VM_MODE_DEFAULT,
+						 MEM_REGION_SIZE);
+
+	mem = mmap(NULL, MEM_REGION_SIZE, PROT_READ | PROT_WRITE,
+		   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	TEST_ASSERT(mem != MAP_FAILED, "Failed to mmap() host");
+
+	kvm_region.flags = 0;
+	kvm_region.memory_size = MEM_REGION_SIZE;
+	kvm_region.userspace_addr = (uint64_t) mem;
+
+	for (i = 0; i < sizeof(slots)/sizeof(struct slot_t); i++) {
+		pr_info("Add slot %i, guest address 0x%06lx, expect rc=%i\n",
+			i, slots[i].guest_addr, slots[i].exp_ret);
+		if (slots[i].exp_ret == 0) {
+			vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
+						    slots[i].guest_addr, i,
+						    mem_reg_npages, 0);
+		} else {
+			kvm_region.slot = i;
+			kvm_region.guest_phys_addr = slots[i].guest_addr;
+			ret = ioctl(vm_fd, KVM_SET_USER_MEMORY_REGION,
+				    &kvm_region);
+			TEST_ASSERT(ret == -1 && errno == EEXIST,
+				    "Adding overlapped memory region should fail with EEXIT");
+		}
+	}
+
+	munmap(mem, MEM_REGION_SIZE);
+	kvm_vm_free(vm);
+}
+
 int main(int argc, char *argv[])
 {
 #ifdef __x86_64__
@@ -383,6 +455,7 @@ int main(int argc, char *argv[])
 #endif
 
 	test_add_max_memory_regions();
+	test_overlap_memory_regions();
 
 #ifdef __x86_64__
 	if (argc > 1)
-- 
2.17.2


  reply	other threads:[~2020-04-15 20:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-15 20:45 [PATCH 0/1] selftests: kvm: Overlapped memory regions test Wainer dos Santos Moschetta
2020-04-15 20:45 ` Wainer dos Santos Moschetta [this message]
2020-04-15 23:57   ` [PATCH 1/1] selftests: kvm: Add overlapped " Krish Sadhukhan
2020-04-16 13:53     ` Paolo Bonzini

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=20200415204505.10021-2-wainersm@redhat.com \
    --to=wainersm@redhat.com \
    --cc=drjones@redhat.com \
    --cc=krish.sadhukhan@oracle.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=sean.j.christopherson@intel.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