Use the kselftest_harness.h interface in this test to get TAP
output, so that it is easier for the user to see what the test
is doing. (Note: We are not using the KVM_ONE_VCPU_TEST_SUITE()
macro here since these tests are creating their VMs with the
vm_create_barebones() function, not with vm_create_with_one_vcpu())
Reviewed-by: Andrew Jones <[email protected]>
Signed-off-by: Thomas Huth <[email protected]>
---
v2:
- Rebase to linux-next branch
- Make "loops" variable static
- Added Andrew's Reviewed-by
.../selftests/kvm/set_memory_region_test.c | 86 +++++++++----------
1 file changed, 42 insertions(+), 44 deletions(-)
diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c
index 68c899d27561..a5c9bee5235a 100644
--- a/tools/testing/selftests/kvm/set_memory_region_test.c
+++ b/tools/testing/selftests/kvm/set_memory_region_test.c
@@ -16,6 +16,7 @@
#include <test_util.h>
#include <kvm_util.h>
#include <processor.h>
+#include "kselftest_harness.h"
/*
* s390x needs at least 1MB alignment, and the x86_64 MOVE/DELETE tests need a
@@ -38,6 +39,8 @@ extern const uint64_t final_rip_end;
static sem_t vcpu_ready;
+static int loops;
+
static inline uint64_t guest_spin_on_val(uint64_t spin_val)
{
uint64_t val;
@@ -219,6 +222,13 @@ static void test_move_memory_region(void)
kvm_vm_free(vm);
}
+TEST(move_in_use_region)
+{
+ ksft_print_msg("Testing MOVE of in-use region, %d loops\n", loops);
+ for (int i = 0; i < loops; i++)
+ test_move_memory_region();
+}
+
static void guest_code_delete_memory_region(void)
{
uint64_t val;
@@ -308,12 +318,19 @@ static void test_delete_memory_region(void)
kvm_vm_free(vm);
}
-static void test_zero_memory_regions(void)
+TEST(delete_in_use_region)
+{
+ ksft_print_msg("Testing DELETE of in-use region, %d loops\n", loops);
+ for (int i = 0; i < loops; i++)
+ test_delete_memory_region();
+}
+
+TEST(zero_memory_regions)
{
struct kvm_vcpu *vcpu;
struct kvm_vm *vm;
- pr_info("Testing KVM_RUN with zero added memory regions\n");
+ ksft_print_msg("Testing KVM_RUN with zero added memory regions\n");
vm = vm_create_barebones();
vcpu = __vm_vcpu_add(vm, 0);
@@ -326,7 +343,7 @@ static void test_zero_memory_regions(void)
}
#endif /* __x86_64__ */
-static void test_invalid_memory_region_flags(void)
+TEST(invalid_memory_region_flags)
{
uint32_t supported_flags = KVM_MEM_LOG_DIRTY_PAGES;
const uint32_t v2_only_flags = KVM_MEM_GUEST_MEMFD;
@@ -389,7 +406,7 @@ static void test_invalid_memory_region_flags(void)
* Test it can be added memory slots up to KVM_CAP_NR_MEMSLOTS, then any
* tentative to add further slots should fail.
*/
-static void test_add_max_memory_regions(void)
+TEST(add_max_memory_regions)
{
int ret;
struct kvm_vm *vm;
@@ -408,13 +425,13 @@ static void test_add_max_memory_regions(void)
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");
- pr_info("Allowed number of memory slots: %i\n", max_mem_slots);
+ ksft_print_msg("Allowed number of memory slots: %i\n", max_mem_slots);
vm = vm_create_barebones();
/* 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",
- (max_mem_slots - 1), MEM_REGION_SIZE >> 10);
+ ksft_print_msg("Adding slots 0..%i, each memory region with %dK size\n",
+ (max_mem_slots - 1), MEM_REGION_SIZE >> 10);
mem = mmap(NULL, (size_t)max_mem_slots * MEM_REGION_SIZE + alignment,
PROT_READ | PROT_WRITE,
@@ -455,12 +472,21 @@ static void test_invalid_guest_memfd(struct kvm_vm *vm, int memfd,
TEST_ASSERT(r == -1 && errno == EINVAL, "%s", msg);
}
-static void test_add_private_memory_region(void)
+static bool has_cap_guest_memfd(void)
+{
+ return kvm_has_cap(KVM_CAP_GUEST_MEMFD) &&
+ (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM));
+}
+
+TEST(add_private_memory_region)
{
struct kvm_vm *vm, *vm2;
int memfd, i;
- pr_info("Testing ADD of KVM_MEM_GUEST_MEMFD memory regions\n");
+ if (!has_cap_guest_memfd())
+ SKIP(return, "Missing KVM_MEM_GUEST_MEMFD / KVM_X86_SW_PROTECTED_VM");
+
+ ksft_print_msg("Testing ADD of KVM_MEM_GUEST_MEMFD memory regions\n");
vm = vm_create_barebones_type(KVM_X86_SW_PROTECTED_VM);
@@ -491,13 +517,16 @@ static void test_add_private_memory_region(void)
kvm_vm_free(vm);
}
-static void test_add_overlapping_private_memory_regions(void)
+TEST(add_overlapping_private_memory_regions)
{
struct kvm_vm *vm;
int memfd;
int r;
- pr_info("Testing ADD of overlapping KVM_MEM_GUEST_MEMFD memory regions\n");
+ if (!has_cap_guest_memfd())
+ SKIP(return, "Missing KVM_MEM_GUEST_MEMFD / KVM_X86_SW_PROTECTED_VM");
+
+ ksft_print_msg("Testing ADD of overlapping KVM_MEM_GUEST_MEMFD memory regions\n");
vm = vm_create_barebones_type(KVM_X86_SW_PROTECTED_VM);
@@ -536,46 +565,15 @@ static void test_add_overlapping_private_memory_regions(void)
close(memfd);
kvm_vm_free(vm);
}
+
#endif
int main(int argc, char *argv[])
{
-#ifdef __x86_64__
- int i, loops;
-
- /*
- * FIXME: the zero-memslot test fails on aarch64 and s390x because
- * KVM_RUN fails with ENOEXEC or EFAULT.
- */
- test_zero_memory_regions();
-#endif
-
- test_invalid_memory_region_flags();
-
- test_add_max_memory_regions();
-
-#ifdef __x86_64__
- if (kvm_has_cap(KVM_CAP_GUEST_MEMFD) &&
- (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))) {
- test_add_private_memory_region();
- test_add_overlapping_private_memory_regions();
- } else {
- pr_info("Skipping tests for KVM_MEM_GUEST_MEMFD memory regions\n");
- }
-
if (argc > 1)
loops = atoi_positive("Number of iterations", argv[1]);
else
loops = 10;
- pr_info("Testing MOVE of in-use region, %d loops\n", loops);
- for (i = 0; i < loops; i++)
- test_move_memory_region();
-
- pr_info("Testing DELETE of in-use region, %d loops\n", loops);
- for (i = 0; i < loops; i++)
- test_delete_memory_region();
-#endif
-
- return 0;
+ return test_harness_run(argc, argv);
}
--
2.44.0
On 4/26/24 4:45 PM, Thomas Huth wrote:
> Use the kselftest_harness.h interface in this test to get TAP
> output, so that it is easier for the user to see what the test
> is doing. (Note: We are not using the KVM_ONE_VCPU_TEST_SUITE()
> macro here since these tests are creating their VMs with the
> vm_create_barebones() function, not with vm_create_with_one_vcpu())
>
> Reviewed-by: Andrew Jones <[email protected]>
> Signed-off-by: Thomas Huth <[email protected]>
LGTM
Reviewed-by: Muhammad Usama Anjum <[email protected]>
Tested-by: Muhammad Usama Anjum <[email protected]>
> ---
> v2:
> - Rebase to linux-next branch
> - Make "loops" variable static
> - Added Andrew's Reviewed-by
>
> .../selftests/kvm/set_memory_region_test.c | 86 +++++++++----------
> 1 file changed, 42 insertions(+), 44 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c
> index 68c899d27561..a5c9bee5235a 100644
> --- a/tools/testing/selftests/kvm/set_memory_region_test.c
> +++ b/tools/testing/selftests/kvm/set_memory_region_test.c
> @@ -16,6 +16,7 @@
> #include <test_util.h>
> #include <kvm_util.h>
> #include <processor.h>
> +#include "kselftest_harness.h"
>
> /*
> * s390x needs at least 1MB alignment, and the x86_64 MOVE/DELETE tests need a
> @@ -38,6 +39,8 @@ extern const uint64_t final_rip_end;
>
> static sem_t vcpu_ready;
>
> +static int loops;
> +
> static inline uint64_t guest_spin_on_val(uint64_t spin_val)
> {
> uint64_t val;
> @@ -219,6 +222,13 @@ static void test_move_memory_region(void)
> kvm_vm_free(vm);
> }
>
> +TEST(move_in_use_region)
> +{
> + ksft_print_msg("Testing MOVE of in-use region, %d loops\n", loops);
> + for (int i = 0; i < loops; i++)
> + test_move_memory_region();
> +}
> +
> static void guest_code_delete_memory_region(void)
> {
> uint64_t val;
> @@ -308,12 +318,19 @@ static void test_delete_memory_region(void)
> kvm_vm_free(vm);
> }
>
> -static void test_zero_memory_regions(void)
> +TEST(delete_in_use_region)
> +{
> + ksft_print_msg("Testing DELETE of in-use region, %d loops\n", loops);
> + for (int i = 0; i < loops; i++)
> + test_delete_memory_region();
> +}
> +
> +TEST(zero_memory_regions)
> {
> struct kvm_vcpu *vcpu;
> struct kvm_vm *vm;
>
> - pr_info("Testing KVM_RUN with zero added memory regions\n");
> + ksft_print_msg("Testing KVM_RUN with zero added memory regions\n");
>
> vm = vm_create_barebones();
> vcpu = __vm_vcpu_add(vm, 0);
> @@ -326,7 +343,7 @@ static void test_zero_memory_regions(void)
> }
> #endif /* __x86_64__ */
>
> -static void test_invalid_memory_region_flags(void)
> +TEST(invalid_memory_region_flags)
> {
> uint32_t supported_flags = KVM_MEM_LOG_DIRTY_PAGES;
> const uint32_t v2_only_flags = KVM_MEM_GUEST_MEMFD;
> @@ -389,7 +406,7 @@ static void test_invalid_memory_region_flags(void)
> * Test it can be added memory slots up to KVM_CAP_NR_MEMSLOTS, then any
> * tentative to add further slots should fail.
> */
> -static void test_add_max_memory_regions(void)
> +TEST(add_max_memory_regions)
> {
> int ret;
> struct kvm_vm *vm;
> @@ -408,13 +425,13 @@ static void test_add_max_memory_regions(void)
> 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");
> - pr_info("Allowed number of memory slots: %i\n", max_mem_slots);
> + ksft_print_msg("Allowed number of memory slots: %i\n", max_mem_slots);
>
> vm = vm_create_barebones();
>
> /* 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",
> - (max_mem_slots - 1), MEM_REGION_SIZE >> 10);
> + ksft_print_msg("Adding slots 0..%i, each memory region with %dK size\n",
> + (max_mem_slots - 1), MEM_REGION_SIZE >> 10);
>
> mem = mmap(NULL, (size_t)max_mem_slots * MEM_REGION_SIZE + alignment,
> PROT_READ | PROT_WRITE,
> @@ -455,12 +472,21 @@ static void test_invalid_guest_memfd(struct kvm_vm *vm, int memfd,
> TEST_ASSERT(r == -1 && errno == EINVAL, "%s", msg);
> }
>
> -static void test_add_private_memory_region(void)
> +static bool has_cap_guest_memfd(void)
> +{
> + return kvm_has_cap(KVM_CAP_GUEST_MEMFD) &&
> + (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM));
> +}
> +
> +TEST(add_private_memory_region)
> {
> struct kvm_vm *vm, *vm2;
> int memfd, i;
>
> - pr_info("Testing ADD of KVM_MEM_GUEST_MEMFD memory regions\n");
> + if (!has_cap_guest_memfd())
> + SKIP(return, "Missing KVM_MEM_GUEST_MEMFD / KVM_X86_SW_PROTECTED_VM");
> +
> + ksft_print_msg("Testing ADD of KVM_MEM_GUEST_MEMFD memory regions\n");
>
> vm = vm_create_barebones_type(KVM_X86_SW_PROTECTED_VM);
>
> @@ -491,13 +517,16 @@ static void test_add_private_memory_region(void)
> kvm_vm_free(vm);
> }
>
> -static void test_add_overlapping_private_memory_regions(void)
> +TEST(add_overlapping_private_memory_regions)
> {
> struct kvm_vm *vm;
> int memfd;
> int r;
>
> - pr_info("Testing ADD of overlapping KVM_MEM_GUEST_MEMFD memory regions\n");
> + if (!has_cap_guest_memfd())
> + SKIP(return, "Missing KVM_MEM_GUEST_MEMFD / KVM_X86_SW_PROTECTED_VM");
> +
> + ksft_print_msg("Testing ADD of overlapping KVM_MEM_GUEST_MEMFD memory regions\n");
>
> vm = vm_create_barebones_type(KVM_X86_SW_PROTECTED_VM);
>
> @@ -536,46 +565,15 @@ static void test_add_overlapping_private_memory_regions(void)
> close(memfd);
> kvm_vm_free(vm);
> }
> +
> #endif
>
> int main(int argc, char *argv[])
> {
> -#ifdef __x86_64__
> - int i, loops;
> -
> - /*
> - * FIXME: the zero-memslot test fails on aarch64 and s390x because
> - * KVM_RUN fails with ENOEXEC or EFAULT.
> - */
> - test_zero_memory_regions();
> -#endif
> -
> - test_invalid_memory_region_flags();
> -
> - test_add_max_memory_regions();
> -
> -#ifdef __x86_64__
> - if (kvm_has_cap(KVM_CAP_GUEST_MEMFD) &&
> - (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))) {
> - test_add_private_memory_region();
> - test_add_overlapping_private_memory_regions();
> - } else {
> - pr_info("Skipping tests for KVM_MEM_GUEST_MEMFD memory regions\n");
> - }
> -
> if (argc > 1)
> loops = atoi_positive("Number of iterations", argv[1]);
> else
> loops = 10;
>
> - pr_info("Testing MOVE of in-use region, %d loops\n", loops);
> - for (i = 0; i < loops; i++)
> - test_move_memory_region();
> -
> - pr_info("Testing DELETE of in-use region, %d loops\n", loops);
> - for (i = 0; i < loops; i++)
> - test_delete_memory_region();
> -#endif
> -
> - return 0;
> + return test_harness_run(argc, argv);
> }
--
BR,
Muhammad Usama Anjum
On 02/05/2024 21.37, Sean Christopherson wrote:
> On Fri, Apr 26, 2024, Thomas Huth wrote:
>> Use the kselftest_harness.h interface in this test to get TAP
>> output, so that it is easier for the user to see what the test
>> is doing. (Note: We are not using the KVM_ONE_VCPU_TEST_SUITE()
>> macro here since these tests are creating their VMs with the
>> vm_create_barebones() function, not with vm_create_with_one_vcpu())
>>
>> Reviewed-by: Andrew Jones <[email protected]>
>> Signed-off-by: Thomas Huth <[email protected]>
>> ---
>> v2:
>> - Rebase to linux-next branch
>> - Make "loops" variable static
>> - Added Andrew's Reviewed-by
>>
>> .../selftests/kvm/set_memory_region_test.c | 86 +++++++++----------
>> 1 file changed, 42 insertions(+), 44 deletions(-)
>>
>> diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c
>> index 68c899d27561..a5c9bee5235a 100644
>> --- a/tools/testing/selftests/kvm/set_memory_region_test.c
>> +++ b/tools/testing/selftests/kvm/set_memory_region_test.c
>> @@ -16,6 +16,7 @@
>> #include <test_util.h>
>> #include <kvm_util.h>
>> #include <processor.h>
>> +#include "kselftest_harness.h"
>>
>> /*
>> * s390x needs at least 1MB alignment, and the x86_64 MOVE/DELETE tests need a
>> @@ -38,6 +39,8 @@ extern const uint64_t final_rip_end;
>>
>> static sem_t vcpu_ready;
>>
>> +static int loops;
>
> ...
>
>> -static void test_add_overlapping_private_memory_regions(void)
>> +TEST(add_overlapping_private_memory_regions)
>> {
>> struct kvm_vm *vm;
>> int memfd;
>> int r;
>>
>> - pr_info("Testing ADD of overlapping KVM_MEM_GUEST_MEMFD memory regions\n");
>> + if (!has_cap_guest_memfd())
>> + SKIP(return, "Missing KVM_MEM_GUEST_MEMFD / KVM_X86_SW_PROTECTED_VM");
>
> I like that we can actually report sub-tests as being skipped, but I don't like
> having multiple ways to express requirements. And IMO, this is much less readable
> than TEST_REQUIRE(has_cap_guest_memfd());
>
> AIUI, each test runs in a child process, so TEST_REQUIRE() can simply exit(), it
> just needs to avoid ksft_exit_skip() so that a sub-test doesn't spit out the full
> test summary.
>
> And if using exit() isn't an option, setjmp()+longjmp() will do the trick (I got
> that working for KVM_ONE_VCPU_TEST() before I realized tests run as a child).
>
> The below is lightly tested, but I think it does what we want?
Not quite ... for example, if I force vmx_pmu_caps_test to skip the last
test, I get:
TAP version 13
1..5
# Starting 5 tests from 1 test cases.
# RUN vmx_pmu_caps.guest_wrmsr_perf_capabilities ...
# OK vmx_pmu_caps.guest_wrmsr_perf_capabilities
ok 1 vmx_pmu_caps.guest_wrmsr_perf_capabilities
# RUN vmx_pmu_caps.basic_perf_capabilities ...
# OK vmx_pmu_caps.basic_perf_capabilities
ok 2 vmx_pmu_caps.basic_perf_capabilities
# RUN vmx_pmu_caps.fungible_perf_capabilities ...
# OK vmx_pmu_caps.fungible_perf_capabilities
ok 3 vmx_pmu_caps.fungible_perf_capabilities
# RUN vmx_pmu_caps.immutable_perf_capabilities ...
# OK vmx_pmu_caps.immutable_perf_capabilities
ok 4 vmx_pmu_caps.immutable_perf_capabilities
# RUN vmx_pmu_caps.lbr_perf_capabilities ...
ok 5 # SKIP - Requirement not met: host_cap.lbr_format && 0
# OK vmx_pmu_caps.lbr_perf_capabilities
ok 5 vmx_pmu_caps.lbr_perf_capabilities
# PASSED: 5 / 5 tests passed.
# Totals: pass:5 fail:0 xfail:0 xpass:0 skip:0 error:0
As you can see, the "ok 5" line is duplicated now, once marked with "# SKIP"
and once as successfull. I don't think that this is valid TAP anymore?
> I also think we would effectively forbid direct use of TEST(). Partly because
> it's effectively necessary to use TEST_REQUIRE(), but also so that all tests will
> have an existing single point of contact if we need/want to make similar changes
> in the future.
Ok, but I wrote in the patch description, KVM_ONE_VCPU_TEST_SUITE() does not
work for the set_memory_region test since it does not like to have a
pre-defined vcpu ... so if we want to forbid TEST(), I assume we'd need
another macro like KVM_BAREBONE_TEST_SUITE() ?
Not sure whether I really like it, though, since I'd prefer if we could keep
the possibility to use the original selftest macros (for people who are
already used to those macros from other selftests).
Thomas