All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] selftests: kvm: Introduce the mem_slot_test test
@ 2020-03-30 20:43 Wainer dos Santos Moschetta
  2020-03-30 20:43 ` [PATCH 1/2] selftests: kvm: Add vm_get_fd() in kvm_util Wainer dos Santos Moschetta
  2020-03-30 20:43 ` [PATCH 2/2] selftests: kvm: Add mem_slot_test test Wainer dos Santos Moschetta
  0 siblings, 2 replies; 11+ messages in thread
From: Wainer dos Santos Moschetta @ 2020-03-30 20:43 UTC (permalink / raw)
  To: kvm, pbonzini; +Cc: linux-kernel, linux-kselftest, david, drjones

This series introduces a new KVM selftest (mem_slot_test) that goal
is to verify memory slots can be added up to the maximum allowed. An
extra slot is attempted which should occur on error.

The patch 01 is needed so that the VM fd can be accessed from the
test code (for the ioctl call attempting to add an extra slot).

I ran the test successfully on x86_64, aarch64, and s390x.  This
is why it is enabled to build on those arches.

Finally, I hope it is useful test!

Wainer dos Santos Moschetta (2):
  selftests: kvm: Add vm_get_fd() in kvm_util
  selftests: kvm: Add mem_slot_test test

 tools/testing/selftests/kvm/.gitignore        |  1 +
 tools/testing/selftests/kvm/Makefile          |  3 +
 .../testing/selftests/kvm/include/kvm_util.h  |  1 +
 tools/testing/selftests/kvm/lib/kvm_util.c    |  5 +
 tools/testing/selftests/kvm/mem_slot_test.c   | 92 +++++++++++++++++++
 5 files changed, 102 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/mem_slot_test.c

-- 
2.17.2


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/2] selftests: kvm: Add vm_get_fd() in kvm_util
  2020-03-30 20:43 [PATCH 0/2] selftests: kvm: Introduce the mem_slot_test test Wainer dos Santos Moschetta
@ 2020-03-30 20:43 ` Wainer dos Santos Moschetta
  2020-03-31  7:52   ` Andrew Jones
  2020-03-30 20:43 ` [PATCH 2/2] selftests: kvm: Add mem_slot_test test Wainer dos Santos Moschetta
  1 sibling, 1 reply; 11+ messages in thread
From: Wainer dos Santos Moschetta @ 2020-03-30 20:43 UTC (permalink / raw)
  To: kvm, pbonzini; +Cc: linux-kernel, linux-kselftest, david, drjones

Introduces the vm_get_fd() function in kvm_util which returns
the VM file descriptor.

Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
---
 tools/testing/selftests/kvm/include/kvm_util.h | 1 +
 tools/testing/selftests/kvm/lib/kvm_util.c     | 5 +++++
 2 files changed, 6 insertions(+)

diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index ae0d14c2540a..aa4a70f969ed 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -163,6 +163,7 @@ bool vm_is_unrestricted_guest(struct kvm_vm *vm);
 unsigned int vm_get_page_size(struct kvm_vm *vm);
 unsigned int vm_get_page_shift(struct kvm_vm *vm);
 unsigned int vm_get_max_gfn(struct kvm_vm *vm);
+unsigned int vm_get_fd(struct kvm_vm *vm);
 
 struct kvm_userspace_memory_region *
 kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index a6dd0401eb50..0961986c0d74 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -1703,3 +1703,8 @@ unsigned int vm_get_max_gfn(struct kvm_vm *vm)
 {
 	return vm->max_gfn;
 }
+
+unsigned int vm_get_fd(struct kvm_vm *vm)
+{
+	return vm->fd;
+}
-- 
2.17.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/2] selftests: kvm: Add mem_slot_test test
  2020-03-30 20:43 [PATCH 0/2] selftests: kvm: Introduce the mem_slot_test test Wainer dos Santos Moschetta
  2020-03-30 20:43 ` [PATCH 1/2] selftests: kvm: Add vm_get_fd() in kvm_util Wainer dos Santos Moschetta
@ 2020-03-30 20:43 ` Wainer dos Santos Moschetta
  2020-03-31  8:16   ` Andrew Jones
  2020-04-02  9:00   ` Andrew Jones
  1 sibling, 2 replies; 11+ messages in thread
From: Wainer dos Santos Moschetta @ 2020-03-30 20:43 UTC (permalink / raw)
  To: kvm, pbonzini; +Cc: linux-kernel, linux-kselftest, david, drjones

This patch introduces the mem_slot_test test which checks
an VM can have added memory slots up to the limit defined in
KVM_CAP_NR_MEMSLOTS. Then attempt to add one more slot to
verify it fails as expected.

Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
---
 tools/testing/selftests/kvm/.gitignore      |  1 +
 tools/testing/selftests/kvm/Makefile        |  3 +
 tools/testing/selftests/kvm/mem_slot_test.c | 92 +++++++++++++++++++++
 3 files changed, 96 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/mem_slot_test.c

diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore
index 30072c3f52fb..b1b94d50f6a2 100644
--- a/tools/testing/selftests/kvm/.gitignore
+++ b/tools/testing/selftests/kvm/.gitignore
@@ -17,3 +17,4 @@
 /clear_dirty_log_test
 /dirty_log_test
 /kvm_create_max_vcpus
+/mem_slot_test
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
index d91c53b726e6..070133349403 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -30,16 +30,19 @@ TEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test
 TEST_GEN_PROGS_x86_64 += clear_dirty_log_test
 TEST_GEN_PROGS_x86_64 += dirty_log_test
 TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
+TEST_GEN_PROGS_x86_64 += mem_slot_test
 
 TEST_GEN_PROGS_aarch64 += clear_dirty_log_test
 TEST_GEN_PROGS_aarch64 += dirty_log_test
 TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
+TEST_GEN_PROGS_aarch64 += mem_slot_test
 
 TEST_GEN_PROGS_s390x = s390x/memop
 TEST_GEN_PROGS_s390x += s390x/sync_regs_test
 TEST_GEN_PROGS_s390x += s390x/resets
 TEST_GEN_PROGS_s390x += dirty_log_test
 TEST_GEN_PROGS_s390x += kvm_create_max_vcpus
+TEST_GEN_PROGS_s390x += mem_slot_test
 
 TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
 LIBKVM += $(LIBKVM_$(UNAME_M))
diff --git a/tools/testing/selftests/kvm/mem_slot_test.c b/tools/testing/selftests/kvm/mem_slot_test.c
new file mode 100644
index 000000000000..75d2bbd71642
--- /dev/null
+++ b/tools/testing/selftests/kvm/mem_slot_test.c
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * mem_slot_test
+ *
+ * Copyright (C) 2020, Red Hat, Inc.
+ *
+ * Test it can be added memory slots up to KVM_CAP_NR_MEMSLOTS, then any
+ * tentative to add further slots should fail.
+ */
+#define _GNU_SOURCE /* for program_invocation_short_name */
+#include <linux/kvm.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include "test_util.h"
+#include "kvm_util.h"
+
+/* Memory region flags */
+#define MEM_REG_FLAGS KVM_MEM_LOG_DIRTY_PAGES
+
+/* Guest VM mode */
+#define GUEST_VM_MODE VM_MODE_DEFAULT
+
+int main(int argc, char *argv[])
+{
+	struct kvm_vm *vm;
+	/* Maximum allowed number of memory slots */
+	uint32_t max_mem_slots;
+	/* Slot number */
+	uint32_t slot;
+	/* Number of pages in a memory region */
+	uint64_t mem_reg_npages;
+	/* Memory region size */
+	uint64_t mem_reg_size;
+	/* Guest physical memory guest_address */
+	uint64_t guest_addr;
+	/* VM page size */
+	uint64_t vm_page_size;
+	int ret;
+
+	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");
+	DEBUG("Allowed number of memory slots: %i\n", max_mem_slots);
+
+	vm = vm_create(GUEST_VM_MODE, 0, O_RDWR);
+
+	/* Determine the minimal number of pages as possible per region. */
+	vm_page_size = vm_get_page_size(vm);
+#ifdef __s390x__
+	mem_reg_size = 0x100000;
+#else
+	uint64_t host_page_size = sysconf(_SC_PAGESIZE);
+
+	mem_reg_size = (host_page_size > vm_page_size) ? host_page_size :
+							 vm_page_size;
+#endif
+	mem_reg_npages = mem_reg_size / vm_page_size;
+	guest_addr = 0x0;
+
+	/* Check it can be added memory slots up to the maximum allowed */
+	DEBUG("Adding slots 0..%i, each memory region with %ldK size\n",
+	      (max_mem_slots - 1), mem_reg_size >> 10);
+	for (slot = 0; slot < max_mem_slots; slot++) {
+		vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
+					    guest_addr, slot, mem_reg_npages,
+					    MEM_REG_FLAGS);
+		guest_addr += mem_reg_size;
+	}
+
+	/* Check it cannot be added memory slots beyond the limit */
+	guest_addr += mem_reg_size;
+	void *mem = mmap(NULL, mem_reg_size, PROT_READ | PROT_WRITE,
+			 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+	TEST_ASSERT(mem != NULL, "Failed to mmap() host");
+
+	struct kvm_userspace_memory_region kvm_region = {
+		.slot = slot,
+		.flags = MEM_REG_FLAGS,
+		.guest_phys_addr = guest_addr,
+		.memory_size = mem_reg_size,
+		.userspace_addr = (uint64_t) mem,
+	};
+
+	ret = ioctl(vm_get_fd(vm), KVM_SET_USER_MEMORY_REGION, &kvm_region);
+	TEST_ASSERT(ret == -1, "Adding one more memory slot should fail");
+
+	munmap(mem, mem_reg_size);
+	kvm_vm_free(vm);
+
+	return 0;
+}
-- 
2.17.2


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/2] selftests: kvm: Add vm_get_fd() in kvm_util
  2020-03-30 20:43 ` [PATCH 1/2] selftests: kvm: Add vm_get_fd() in kvm_util Wainer dos Santos Moschetta
@ 2020-03-31  7:52   ` Andrew Jones
  0 siblings, 0 replies; 11+ messages in thread
From: Andrew Jones @ 2020-03-31  7:52 UTC (permalink / raw)
  To: Wainer dos Santos Moschetta
  Cc: kvm, pbonzini, linux-kernel, linux-kselftest, david

On Mon, Mar 30, 2020 at 05:43:09PM -0300, Wainer dos Santos Moschetta wrote:
> Introduces the vm_get_fd() function in kvm_util which returns
> the VM file descriptor.
> 
> Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
> ---
>  tools/testing/selftests/kvm/include/kvm_util.h | 1 +
>  tools/testing/selftests/kvm/lib/kvm_util.c     | 5 +++++
>  2 files changed, 6 insertions(+)
> 
> diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
> index ae0d14c2540a..aa4a70f969ed 100644
> --- a/tools/testing/selftests/kvm/include/kvm_util.h
> +++ b/tools/testing/selftests/kvm/include/kvm_util.h
> @@ -163,6 +163,7 @@ bool vm_is_unrestricted_guest(struct kvm_vm *vm);
>  unsigned int vm_get_page_size(struct kvm_vm *vm);
>  unsigned int vm_get_page_shift(struct kvm_vm *vm);
>  unsigned int vm_get_max_gfn(struct kvm_vm *vm);
> +unsigned int vm_get_fd(struct kvm_vm *vm);
>  
>  struct kvm_userspace_memory_region *
>  kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
> diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> index a6dd0401eb50..0961986c0d74 100644
> --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> @@ -1703,3 +1703,8 @@ unsigned int vm_get_max_gfn(struct kvm_vm *vm)
>  {
>  	return vm->max_gfn;
>  }
> +
> +unsigned int vm_get_fd(struct kvm_vm *vm)
> +{
> +	return vm->fd;
> +}
> -- 
> 2.17.2
>

Please use an int instead of 'unsigned int' to match the fd type.

Thanks,
drew 


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] selftests: kvm: Add mem_slot_test test
  2020-03-30 20:43 ` [PATCH 2/2] selftests: kvm: Add mem_slot_test test Wainer dos Santos Moschetta
@ 2020-03-31  8:16   ` Andrew Jones
  2020-03-31 21:42     ` Wainer dos Santos Moschetta
  2020-04-02  9:00   ` Andrew Jones
  1 sibling, 1 reply; 11+ messages in thread
From: Andrew Jones @ 2020-03-31  8:16 UTC (permalink / raw)
  To: Wainer dos Santos Moschetta
  Cc: kvm, pbonzini, linux-kernel, linux-kselftest, david

On Mon, Mar 30, 2020 at 05:43:10PM -0300, Wainer dos Santos Moschetta wrote:
> This patch introduces the mem_slot_test test which checks
> an VM can have added memory slots up to the limit defined in
> KVM_CAP_NR_MEMSLOTS. Then attempt to add one more slot to
> verify it fails as expected.
> 
> Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
> ---
>  tools/testing/selftests/kvm/.gitignore      |  1 +
>  tools/testing/selftests/kvm/Makefile        |  3 +
>  tools/testing/selftests/kvm/mem_slot_test.c | 92 +++++++++++++++++++++
>  3 files changed, 96 insertions(+)
>  create mode 100644 tools/testing/selftests/kvm/mem_slot_test.c
> 
> diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore
> index 30072c3f52fb..b1b94d50f6a2 100644
> --- a/tools/testing/selftests/kvm/.gitignore
> +++ b/tools/testing/selftests/kvm/.gitignore
> @@ -17,3 +17,4 @@
>  /clear_dirty_log_test
>  /dirty_log_test
>  /kvm_create_max_vcpus
> +/mem_slot_test
> diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
> index d91c53b726e6..070133349403 100644
> --- a/tools/testing/selftests/kvm/Makefile
> +++ b/tools/testing/selftests/kvm/Makefile
> @@ -30,16 +30,19 @@ TEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test
>  TEST_GEN_PROGS_x86_64 += clear_dirty_log_test
>  TEST_GEN_PROGS_x86_64 += dirty_log_test
>  TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
> +TEST_GEN_PROGS_x86_64 += mem_slot_test
>  
>  TEST_GEN_PROGS_aarch64 += clear_dirty_log_test
>  TEST_GEN_PROGS_aarch64 += dirty_log_test
>  TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
> +TEST_GEN_PROGS_aarch64 += mem_slot_test
>  
>  TEST_GEN_PROGS_s390x = s390x/memop
>  TEST_GEN_PROGS_s390x += s390x/sync_regs_test
>  TEST_GEN_PROGS_s390x += s390x/resets
>  TEST_GEN_PROGS_s390x += dirty_log_test
>  TEST_GEN_PROGS_s390x += kvm_create_max_vcpus
> +TEST_GEN_PROGS_s390x += mem_slot_test
>  
>  TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
>  LIBKVM += $(LIBKVM_$(UNAME_M))
> diff --git a/tools/testing/selftests/kvm/mem_slot_test.c b/tools/testing/selftests/kvm/mem_slot_test.c
> new file mode 100644
> index 000000000000..75d2bbd71642
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/mem_slot_test.c
> @@ -0,0 +1,92 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * mem_slot_test
> + *
> + * Copyright (C) 2020, Red Hat, Inc.
> + *
> + * Test it can be added memory slots up to KVM_CAP_NR_MEMSLOTS, then any
> + * tentative to add further slots should fail.
> + */
> +#define _GNU_SOURCE /* for program_invocation_short_name */
> +#include <linux/kvm.h>
> +#include <sys/mman.h>
> +#include <unistd.h>
> +
> +#include "test_util.h"
> +#include "kvm_util.h"
> +
> +/* Memory region flags */
> +#define MEM_REG_FLAGS KVM_MEM_LOG_DIRTY_PAGES
> +
> +/* Guest VM mode */
> +#define GUEST_VM_MODE VM_MODE_DEFAULT

I'm not sure what the value of the two defines above are. I'd prefer we
avoid unnecessary renaming. Also, do we need KVM_MEM_LOG_DIRTY_PAGES for
this test?

> +
> +int main(int argc, char *argv[])
> +{
> +	struct kvm_vm *vm;
> +	/* Maximum allowed number of memory slots */
> +	uint32_t max_mem_slots;
> +	/* Slot number */
> +	uint32_t slot;
> +	/* Number of pages in a memory region */
> +	uint64_t mem_reg_npages;
> +	/* Memory region size */
> +	uint64_t mem_reg_size;
> +	/* Guest physical memory guest_address */
> +	uint64_t guest_addr;
> +	/* VM page size */
> +	uint64_t vm_page_size;

nit: IMO, the variable names above are descriptive enough to drop the
comments.

> +	int ret;
> +
> +	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");
> +	DEBUG("Allowed number of memory slots: %i\n", max_mem_slots);

DEBUG() no longer exists in kvm/queue. This should now be pr_debug().

> +
> +	vm = vm_create(GUEST_VM_MODE, 0, O_RDWR);
> +
> +	/* Determine the minimal number of pages as possible per region. */
> +	vm_page_size = vm_get_page_size(vm);
> +#ifdef __s390x__
> +	mem_reg_size = 0x100000;
> +#else
> +	uint64_t host_page_size = sysconf(_SC_PAGESIZE);
> +
> +	mem_reg_size = (host_page_size > vm_page_size) ? host_page_size :
> +							 vm_page_size;
> +#endif
> +	mem_reg_npages = mem_reg_size / vm_page_size;

On kvm/queue the above 11 lines can now all be done with

  mem_reg_size = SOME_ARBITRARY_MEM_REG_SIZE;
  mem_reg_npages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, mem_reg_size);

> +	guest_addr = 0x0;
> +
> +	/* Check it can be added memory slots up to the maximum allowed */
> +	DEBUG("Adding slots 0..%i, each memory region with %ldK size\n",
> +	      (max_mem_slots - 1), mem_reg_size >> 10);
> +	for (slot = 0; slot < max_mem_slots; slot++) {
> +		vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
> +					    guest_addr, slot, mem_reg_npages,
> +					    MEM_REG_FLAGS);
> +		guest_addr += mem_reg_size;
> +	}
> +
> +	/* Check it cannot be added memory slots beyond the limit */
> +	guest_addr += mem_reg_size;

nit: shouldn't be necessary. We already incremented guest_addr on the
last loop.

> +	void *mem = mmap(NULL, mem_reg_size, PROT_READ | PROT_WRITE,
> +			 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> +	TEST_ASSERT(mem != NULL, "Failed to mmap() host");
> +
> +	struct kvm_userspace_memory_region kvm_region = {
> +		.slot = slot,
> +		.flags = MEM_REG_FLAGS,
> +		.guest_phys_addr = guest_addr,
> +		.memory_size = mem_reg_size,
> +		.userspace_addr = (uint64_t) mem,
> +	};
> +
> +	ret = ioctl(vm_get_fd(vm), KVM_SET_USER_MEMORY_REGION, &kvm_region);
> +	TEST_ASSERT(ret == -1, "Adding one more memory slot should fail");

Shouldn't we also check that we get the correct errno?

> +
> +	munmap(mem, mem_reg_size);
> +	kvm_vm_free(vm);
> +
> +	return 0;
> +}
> -- 
> 2.17.2
>

Thanks,
drew


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] selftests: kvm: Add mem_slot_test test
  2020-03-31  8:16   ` Andrew Jones
@ 2020-03-31 21:42     ` Wainer dos Santos Moschetta
  2020-04-01  6:38       ` Andrew Jones
  0 siblings, 1 reply; 11+ messages in thread
From: Wainer dos Santos Moschetta @ 2020-03-31 21:42 UTC (permalink / raw)
  To: Andrew Jones; +Cc: kvm, pbonzini, linux-kernel, linux-kselftest, david

Hi Andrew,

Nice review. Few comments below.

On 3/31/20 5:16 AM, Andrew Jones wrote:
> On Mon, Mar 30, 2020 at 05:43:10PM -0300, Wainer dos Santos Moschetta wrote:
>> This patch introduces the mem_slot_test test which checks
>> an VM can have added memory slots up to the limit defined in
>> KVM_CAP_NR_MEMSLOTS. Then attempt to add one more slot to
>> verify it fails as expected.
>>
>> Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
>> ---
>>   tools/testing/selftests/kvm/.gitignore      |  1 +
>>   tools/testing/selftests/kvm/Makefile        |  3 +
>>   tools/testing/selftests/kvm/mem_slot_test.c | 92 +++++++++++++++++++++
>>   3 files changed, 96 insertions(+)
>>   create mode 100644 tools/testing/selftests/kvm/mem_slot_test.c
>>
>> diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore
>> index 30072c3f52fb..b1b94d50f6a2 100644
>> --- a/tools/testing/selftests/kvm/.gitignore
>> +++ b/tools/testing/selftests/kvm/.gitignore
>> @@ -17,3 +17,4 @@
>>   /clear_dirty_log_test
>>   /dirty_log_test
>>   /kvm_create_max_vcpus
>> +/mem_slot_test
>> diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
>> index d91c53b726e6..070133349403 100644
>> --- a/tools/testing/selftests/kvm/Makefile
>> +++ b/tools/testing/selftests/kvm/Makefile
>> @@ -30,16 +30,19 @@ TEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test
>>   TEST_GEN_PROGS_x86_64 += clear_dirty_log_test
>>   TEST_GEN_PROGS_x86_64 += dirty_log_test
>>   TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
>> +TEST_GEN_PROGS_x86_64 += mem_slot_test
>>   
>>   TEST_GEN_PROGS_aarch64 += clear_dirty_log_test
>>   TEST_GEN_PROGS_aarch64 += dirty_log_test
>>   TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
>> +TEST_GEN_PROGS_aarch64 += mem_slot_test
>>   
>>   TEST_GEN_PROGS_s390x = s390x/memop
>>   TEST_GEN_PROGS_s390x += s390x/sync_regs_test
>>   TEST_GEN_PROGS_s390x += s390x/resets
>>   TEST_GEN_PROGS_s390x += dirty_log_test
>>   TEST_GEN_PROGS_s390x += kvm_create_max_vcpus
>> +TEST_GEN_PROGS_s390x += mem_slot_test
>>   
>>   TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
>>   LIBKVM += $(LIBKVM_$(UNAME_M))
>> diff --git a/tools/testing/selftests/kvm/mem_slot_test.c b/tools/testing/selftests/kvm/mem_slot_test.c
>> new file mode 100644
>> index 000000000000..75d2bbd71642
>> --- /dev/null
>> +++ b/tools/testing/selftests/kvm/mem_slot_test.c
>> @@ -0,0 +1,92 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +/*
>> + * mem_slot_test
>> + *
>> + * Copyright (C) 2020, Red Hat, Inc.
>> + *
>> + * Test it can be added memory slots up to KVM_CAP_NR_MEMSLOTS, then any
>> + * tentative to add further slots should fail.
>> + */
>> +#define _GNU_SOURCE /* for program_invocation_short_name */
>> +#include <linux/kvm.h>
>> +#include <sys/mman.h>
>> +#include <unistd.h>
>> +
>> +#include "test_util.h"
>> +#include "kvm_util.h"
>> +
>> +/* Memory region flags */
>> +#define MEM_REG_FLAGS KVM_MEM_LOG_DIRTY_PAGES
>> +
>> +/* Guest VM mode */
>> +#define GUEST_VM_MODE VM_MODE_DEFAULT
> I'm not sure what the value of the two defines above are. I'd prefer we
> avoid unnecessary renaming. Also, do we need KVM_MEM_LOG_DIRTY_PAGES for
> this test?

It would be nice to exercise the code by adding slots with different 
page flags. But for this test that simple checks the limit, the use of 
KVM_MEM_READONLY is enough. I will change it on v2.

>
>> +
>> +int main(int argc, char *argv[])
>> +{
>> +	struct kvm_vm *vm;
>> +	/* Maximum allowed number of memory slots */
>> +	uint32_t max_mem_slots;
>> +	/* Slot number */
>> +	uint32_t slot;
>> +	/* Number of pages in a memory region */
>> +	uint64_t mem_reg_npages;
>> +	/* Memory region size */
>> +	uint64_t mem_reg_size;
>> +	/* Guest physical memory guest_address */
>> +	uint64_t guest_addr;
>> +	/* VM page size */
>> +	uint64_t vm_page_size;
> nit: IMO, the variable names above are descriptive enough to drop the
> comments.
>
>> +	int ret;
>> +
>> +	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");
>> +	DEBUG("Allowed number of memory slots: %i\n", max_mem_slots);
> DEBUG() no longer exists in kvm/queue. This should now be pr_debug().
Great. I will rebase my code to kvm/queue...
>
>> +
>> +	vm = vm_create(GUEST_VM_MODE, 0, O_RDWR);
>> +
>> +	/* Determine the minimal number of pages as possible per region. */
>> +	vm_page_size = vm_get_page_size(vm);
>> +#ifdef __s390x__
>> +	mem_reg_size = 0x100000;
>> +#else
>> +	uint64_t host_page_size = sysconf(_SC_PAGESIZE);
>> +
>> +	mem_reg_size = (host_page_size > vm_page_size) ? host_page_size :
>> +							 vm_page_size;
>> +#endif
>> +	mem_reg_npages = mem_reg_size / vm_page_size;
> On kvm/queue the above 11 lines can now all be done with
>
>    mem_reg_size = SOME_ARBITRARY_MEM_REG_SIZE;
>    mem_reg_npages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, mem_reg_size);
>
>> +	guest_addr = 0x0;
>> +
>> +	/* Check it can be added memory slots up to the maximum allowed */
>> +	DEBUG("Adding slots 0..%i, each memory region with %ldK size\n",
>> +	      (max_mem_slots - 1), mem_reg_size >> 10);
>> +	for (slot = 0; slot < max_mem_slots; slot++) {
>> +		vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
>> +					    guest_addr, slot, mem_reg_npages,
>> +					    MEM_REG_FLAGS);
>> +		guest_addr += mem_reg_size;
>> +	}
>> +
>> +	/* Check it cannot be added memory slots beyond the limit */
>> +	guest_addr += mem_reg_size;
> nit: shouldn't be necessary. We already incremented guest_addr on the
> last loop.
Good catch.
>
>> +	void *mem = mmap(NULL, mem_reg_size, PROT_READ | PROT_WRITE,
>> +			 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
>> +	TEST_ASSERT(mem != NULL, "Failed to mmap() host");
>> +
>> +	struct kvm_userspace_memory_region kvm_region = {
>> +		.slot = slot,
>> +		.flags = MEM_REG_FLAGS,
>> +		.guest_phys_addr = guest_addr,
>> +		.memory_size = mem_reg_size,
>> +		.userspace_addr = (uint64_t) mem,
>> +	};
>> +
>> +	ret = ioctl(vm_get_fd(vm), KVM_SET_USER_MEMORY_REGION, &kvm_region);
>> +	TEST_ASSERT(ret == -1, "Adding one more memory slot should fail");
> Shouldn't we also check that we get the correct errno?

Good idea.

In the kvm API document there isn't any mention to the errno returned 
but looking at code it seems to be EINVAL for all errors (except for one 
EEXIST).

Thanks,

Wainer

>
>> +
>> +	munmap(mem, mem_reg_size);
>> +	kvm_vm_free(vm);
>> +
>> +	return 0;
>> +}
>> -- 
>> 2.17.2
>>
> Thanks,
> drew


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] selftests: kvm: Add mem_slot_test test
  2020-03-31 21:42     ` Wainer dos Santos Moschetta
@ 2020-04-01  6:38       ` Andrew Jones
  2020-04-02 13:48         ` Wainer dos Santos Moschetta
  0 siblings, 1 reply; 11+ messages in thread
From: Andrew Jones @ 2020-04-01  6:38 UTC (permalink / raw)
  To: Wainer dos Santos Moschetta
  Cc: kvm, pbonzini, linux-kernel, linux-kselftest, david

On Tue, Mar 31, 2020 at 06:42:21PM -0300, Wainer dos Santos Moschetta wrote:
> It would be nice to exercise the code by adding slots with different page
> flags. But for this test that simple checks the limit, the use of
> KVM_MEM_READONLY is enough. I will change it on v2.

It would be good to test more memslot error conditions as well. Do you plan
to expand on this test?

Thanks,
drew


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] selftests: kvm: Add mem_slot_test test
  2020-03-30 20:43 ` [PATCH 2/2] selftests: kvm: Add mem_slot_test test Wainer dos Santos Moschetta
  2020-03-31  8:16   ` Andrew Jones
@ 2020-04-02  9:00   ` Andrew Jones
  2020-04-02 13:56     ` Wainer dos Santos Moschetta
  1 sibling, 1 reply; 11+ messages in thread
From: Andrew Jones @ 2020-04-02  9:00 UTC (permalink / raw)
  To: Wainer dos Santos Moschetta
  Cc: kvm, pbonzini, linux-kernel, linux-kselftest, david

On Mon, Mar 30, 2020 at 05:43:10PM -0300, Wainer dos Santos Moschetta wrote:
> This patch introduces the mem_slot_test test which checks
> an VM can have added memory slots up to the limit defined in
> KVM_CAP_NR_MEMSLOTS. Then attempt to add one more slot to
> verify it fails as expected.
> 
> Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
> ---
>  tools/testing/selftests/kvm/.gitignore      |  1 +
>  tools/testing/selftests/kvm/Makefile        |  3 +
>  tools/testing/selftests/kvm/mem_slot_test.c | 92 +++++++++++++++++++++
>  3 files changed, 96 insertions(+)
>  create mode 100644 tools/testing/selftests/kvm/mem_slot_test.c
>

BTW, in kvm/queue we also now have

x86_64/set_memory_region_test.c

I wonder if we shouldn't try to make x86_64/set_memory_region_test.c
arch-neutral and then integrate this new test with it.

Thanks,
drew


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] selftests: kvm: Add mem_slot_test test
  2020-04-01  6:38       ` Andrew Jones
@ 2020-04-02 13:48         ` Wainer dos Santos Moschetta
  0 siblings, 0 replies; 11+ messages in thread
From: Wainer dos Santos Moschetta @ 2020-04-02 13:48 UTC (permalink / raw)
  To: Andrew Jones; +Cc: kvm, pbonzini, linux-kernel, linux-kselftest, david


On 4/1/20 3:38 AM, Andrew Jones wrote:
> On Tue, Mar 31, 2020 at 06:42:21PM -0300, Wainer dos Santos Moschetta wrote:
>> It would be nice to exercise the code by adding slots with different page
>> flags. But for this test that simple checks the limit, the use of
>> KVM_MEM_READONLY is enough. I will change it on v2.
> It would be good to test more memslot error conditions as well. Do you plan
> to expand on this test?

Yes, I do. For example, check memory slots addresses can't overlap.

>
> Thanks,
> drew


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] selftests: kvm: Add mem_slot_test test
  2020-04-02  9:00   ` Andrew Jones
@ 2020-04-02 13:56     ` Wainer dos Santos Moschetta
  2020-04-02 14:44       ` Andrew Jones
  0 siblings, 1 reply; 11+ messages in thread
From: Wainer dos Santos Moschetta @ 2020-04-02 13:56 UTC (permalink / raw)
  To: Andrew Jones; +Cc: kvm, pbonzini, linux-kernel, linux-kselftest, david


On 4/2/20 6:00 AM, Andrew Jones wrote:
> On Mon, Mar 30, 2020 at 05:43:10PM -0300, Wainer dos Santos Moschetta wrote:
>> This patch introduces the mem_slot_test test which checks
>> an VM can have added memory slots up to the limit defined in
>> KVM_CAP_NR_MEMSLOTS. Then attempt to add one more slot to
>> verify it fails as expected.
>>
>> Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
>> ---
>>   tools/testing/selftests/kvm/.gitignore      |  1 +
>>   tools/testing/selftests/kvm/Makefile        |  3 +
>>   tools/testing/selftests/kvm/mem_slot_test.c | 92 +++++++++++++++++++++
>>   3 files changed, 96 insertions(+)
>>   create mode 100644 tools/testing/selftests/kvm/mem_slot_test.c
>>
> BTW, in kvm/queue we also now have
>
> x86_64/set_memory_region_test.c
>
> I wonder if we shouldn't try to make x86_64/set_memory_region_test.c
> arch-neutral and then integrate this new test with it.

When I started work on this test I called it "add_max_mem_slots" but 
then I realized it could be rather a suite for other tests, so it was 
renamed. So yes, I think we can try to merge those memory region tests 
altogether.

I'm about to send a v2 where I address all your comments and hopefully 
we can use as the base for such as integration. Makes sense?

Thanks!

- Wainer


>
> Thanks,
> drew


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/2] selftests: kvm: Add mem_slot_test test
  2020-04-02 13:56     ` Wainer dos Santos Moschetta
@ 2020-04-02 14:44       ` Andrew Jones
  0 siblings, 0 replies; 11+ messages in thread
From: Andrew Jones @ 2020-04-02 14:44 UTC (permalink / raw)
  To: Wainer dos Santos Moschetta
  Cc: kvm, pbonzini, linux-kernel, linux-kselftest, david

On Thu, Apr 02, 2020 at 10:56:18AM -0300, Wainer dos Santos Moschetta wrote:
> 
> On 4/2/20 6:00 AM, Andrew Jones wrote:
> > On Mon, Mar 30, 2020 at 05:43:10PM -0300, Wainer dos Santos Moschetta wrote:
> > > This patch introduces the mem_slot_test test which checks
> > > an VM can have added memory slots up to the limit defined in
> > > KVM_CAP_NR_MEMSLOTS. Then attempt to add one more slot to
> > > verify it fails as expected.
> > > 
> > > Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
> > > ---
> > >   tools/testing/selftests/kvm/.gitignore      |  1 +
> > >   tools/testing/selftests/kvm/Makefile        |  3 +
> > >   tools/testing/selftests/kvm/mem_slot_test.c | 92 +++++++++++++++++++++
> > >   3 files changed, 96 insertions(+)
> > >   create mode 100644 tools/testing/selftests/kvm/mem_slot_test.c
> > > 
> > BTW, in kvm/queue we also now have
> > 
> > x86_64/set_memory_region_test.c
> > 
> > I wonder if we shouldn't try to make x86_64/set_memory_region_test.c
> > arch-neutral and then integrate this new test with it.
> 
> When I started work on this test I called it "add_max_mem_slots" but then I
> realized it could be rather a suite for other tests, so it was renamed. So
> yes, I think we can try to merge those memory region tests altogether.
> 
> I'm about to send a v2 where I address all your comments and hopefully we
> can use as the base for such as integration. Makes sense?

OK, but there's no rush (at least for me) for v2. If you want to look at
integration for v2 and send it later, then I'm fine with waiting.

Thanks,
drew
> 
> Thanks!
> 
> - Wainer
> 
> 
> > 
> > Thanks,
> > drew
> 


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-04-02 14:44 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-30 20:43 [PATCH 0/2] selftests: kvm: Introduce the mem_slot_test test Wainer dos Santos Moschetta
2020-03-30 20:43 ` [PATCH 1/2] selftests: kvm: Add vm_get_fd() in kvm_util Wainer dos Santos Moschetta
2020-03-31  7:52   ` Andrew Jones
2020-03-30 20:43 ` [PATCH 2/2] selftests: kvm: Add mem_slot_test test Wainer dos Santos Moschetta
2020-03-31  8:16   ` Andrew Jones
2020-03-31 21:42     ` Wainer dos Santos Moschetta
2020-04-01  6:38       ` Andrew Jones
2020-04-02 13:48         ` Wainer dos Santos Moschetta
2020-04-02  9:00   ` Andrew Jones
2020-04-02 13:56     ` Wainer dos Santos Moschetta
2020-04-02 14:44       ` Andrew Jones

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.