Skip to contentSkip to conversation

KVM: selftests: fix steal_time for arm64

PATCH Applied Sebastian Ott 4 participants 9 messages May 4 – Sep 14

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: selftests: fix steal_time for arm64

Fix the following failure to the steal_time test on arm64 by making the timer address known to the guest.

==== Test Assertion Failure ====
steal_time.c:229: !ret
pid=18514 tid=18514 errno=22 - Invalid argument

     1  0x000000000040252f: check_steal_time_uapi at steal_time.c:229 (discriminator 20)
     2   (inlined by) main at steal_time.c:537 (discriminator 20)
     3  0x0000ffffa23d621b: ?? ??:0
     4  0x0000ffffa23d62fb: ?? ??:0
     5  0x0000000000402b6f: _start at ??:?

KVM_SET_DEVICE_ATTR failed, rc: -1 errno: 22 (Invalid argument)

Fixes 40351ed924dd ("KVM: selftests: Refactor UAPI tests into dedicated function")
---
 tools/testing/selftests/kvm/steal_time.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/kvm/steal_time.c +2 -0
index 7df2bc8eec02..76fcdd1fd3cb 100644
@@ -220,6 +220,8 @@ static void check_steal_time_uapi(void)
 	};
 
 	vcpu_ioctl(vcpu, KVM_HAS_DEVICE_ATTR, &dev);
+	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, 1, 0);
+	virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, 1);
 
 	st_ipa = (ulong)ST_GPA_BASE | 1;
 	ret = __vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev);

base-commit: 6d35786de28116ecf78797a62b84e6bf3c45aa5a

Signature · 1 line
2.52.0
maintainer Applied
#1 lore raw
Quoted 6 lines · Sebastian Ott

Fix the following failure to the steal_time test on arm64 by making the timer address known to the guest.

==== Test Assertion Failure ====
steal_time.c:229: !ret
pid=18514 tid=18514 errno=22 - Invalid argument

Quoted 8 lines
     1  0x000000000040252f: check_steal_time_uapi at steal_time.c:229 (discriminator 20)
     2   (inlined by) main at steal_time.c:537 (discriminator 20)
     3  0x0000ffffa23d621b: ?? ??:0
     4  0x0000ffffa23d62fb: ?? ??:0
     5  0x0000000000402b6f: _start at ??:?

KVM_SET_DEVICE_ATTR failed, rc: -1 errno: 22 (Invalid argument)

[...]

Applied to fixes, thanks!

[1/1] KVM: selftests: fix steal_time for arm64

      commit: fc240715fc5003538ff530e3cfb985e7769b7171

Cheers,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#2 lore raw
In reply to Sebastian Ott

Hi Sebastian,

Quoted 24 lines · Sebastian Ott

Fix the following failure to the steal_time test on arm64 by making the timer address known to the guest.

==== Test Assertion Failure ====
steal_time.c:229: !ret
pid=18514 tid=18514 errno=22 - Invalid argument

     1  0x000000000040252f: check_steal_time_uapi at steal_time.c:229 (discriminator 20)
     2   (inlined by) main at steal_time.c:537 (discriminator 20)
     3  0x0000ffffa23d621b: ?? ??:0
     4  0x0000ffffa23d62fb: ?? ??:0
     5  0x0000000000402b6f: _start at ??:?

KVM_SET_DEVICE_ATTR failed, rc: -1 errno: 22 (Invalid argument)

Fixes 40351ed924dd ("KVM: selftests: Refactor UAPI tests into dedicated function")
---
 tools/testing/selftests/kvm/steal_time.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/kvm/steal_time.c +0 -0
index 7df2bc8eec02..76fcdd1fd3cb 100644

tools/testing/selftests/kvm/steal_time.c static void check_steal_time_uapi(void) · line 223

Quoted 4 lines
tools/testing/selftests/kvm/steal_time.c +1 -0
@@ -220,6 +220,8 @@ static void check_steal_time_uapi(void)
 	};
 
 	vcpu_ioctl(vcpu, KVM_HAS_DEVICE_ATTR, &dev);
+	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, 1, 0);

This leads to the following failure when the host page size is greater than the guest's:

[root@localhost kvm]# getconf PAGESIZE
16384
[root@localhost kvm]# ./steal_time
Random seed: 0x6b8b4567
__vm_create: mode='PA-bits:40, VA-bits:48, 4K pages' type='0', pages='672' TAP version 13
1..4
__vm_create: mode='PA-bits:40, VA-bits:48, 4K pages' type='0', pages='660' ==== Test Assertion Failure ====
lib/kvm_util.c:991: vm_adjust_num_guest_pages(vm->mode, npages) == npages pid=873 tid=873 errno=0 - Success

     1	0x0000000000405a27: vm_mem_add at kvm_util.c:991
     2	0x000000000040241f: check_steal_time_uapi at steal_time.c:223 (discriminator 7)
     3	 (inlined by) main at steal_time.c:539 (discriminator 7)
     4	0x00007fff8b57af3b: ?? ??:0
     5	0x00007fff8b57b007: ?? ??:0
     6	0x0000000000402b6f: _start at ??:?

Number of guest pages is not compatible with the host. Try npages=4

tools/testing/selftests/kvm/steal_time.c static void check_steal_time_uapi(void) · line 227

Quoted 4 lines
tools/testing/selftests/kvm/steal_time.c +1 -0
+	virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, 1);
 
 	st_ipa = (ulong)ST_GPA_BASE | 1;
 	ret = __vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev);

Thanks,
Zenghui

#3 lore raw

PATCH 1/1 KVM: selftests: fix steal_time for arm64 with host page size > 4K

Fix the following failure when running with 16K host page size:
==== Test Assertion Failure ====
lib/kvm_util.c:991: vm_adjust_num_guest_pages(vm->mode, npages) == npages pid=873 tid=873 errno=0 - Success

     1  0x0000000000405a27: vm_mem_add at kvm_util.c:991
     2  0x000000000040241f: check_steal_time_uapi at steal_time.c:223 (discriminator 7)
     3   (inlined by) main at steal_time.c:539 (discriminator 7)
     4  0x00007fff8b57af3b: ?? ??:0
     5  0x00007fff8b57b007: ?? ??:0
     6  0x0000000000402b6f: _start at ??:?

Number of guest pages is not compatible with the host. Try npages=4

Fixes fc240715fc50 ("KVM: selftests: arm64: Fix steal_time test after UAPI refactoring")
---
 tools/testing/selftests/kvm/steal_time.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/steal_time.c +4 -2
index 76fcdd1fd3cb..cdb81f3ee4b2 100644
@@ -208,6 +208,7 @@ static void check_steal_time_uapi(void)
 {
 	struct kvm_vm *vm;
 	struct kvm_vcpu *vcpu;
+	unsigned int gpages;
 	u64 st_ipa;
 	int ret;
 
@@ -220,8 +221,9 @@ static void check_steal_time_uapi(void)
 	};
 
 	vcpu_ioctl(vcpu, KVM_HAS_DEVICE_ATTR, &dev);
-	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, 1, 0);
-	virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, 1);
+	gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, 1);
+	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, gpages, 0);
+	virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, gpages);
 
 	st_ipa = (ulong)ST_GPA_BASE | 1;
 	ret = __vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev);
Signature · 1 line
2.54.0
Reviewed-by on v1 1/1
#4 lore raw

Re: [PATCH] KVM: selftests: fix steal_time for arm64 with host page size > 4K

Quoted 11 lines · Sebastian Ott

Fix the following failure when running with 16K host page size:
==== Test Assertion Failure ====
lib/kvm_util.c:991: vm_adjust_num_guest_pages(vm->mode, npages) == npages pid=873 tid=873 errno=0 - Success

     1  0x0000000000405a27: vm_mem_add at kvm_util.c:991
     2  0x000000000040241f: check_steal_time_uapi at steal_time.c:223 (discriminator 7)
     3   (inlined by) main at steal_time.c:539 (discriminator 7)
     4  0x00007fff8b57af3b: ?? ??:0
     5  0x00007fff8b57b007: ?? ??:0
     6  0x0000000000402b6f: _start at ??:?
Quoted 6 lines

Number of guest pages is not compatible with the host. Try npages=4

Fixes fc240715fc50 ("KVM: selftests: arm64: Fix steal_time test after UAPI refactoring")

Looks good to me,

Thanks,
Zenghui

#5 lore raw

Re: [PATCH] KVM: selftests: fix steal_time for arm64 with host page size > 4K

In reply to Sebastian Ott
Quoted 34 lines · Sebastian Ott

Fix the following failure when running with 16K host page size:
==== Test Assertion Failure ====
lib/kvm_util.c:991: vm_adjust_num_guest_pages(vm->mode, npages) == npages pid=873 tid=873 errno=0 - Success

     1  0x0000000000405a27: vm_mem_add at kvm_util.c:991
     2  0x000000000040241f: check_steal_time_uapi at steal_time.c:223 (discriminator 7)
     3   (inlined by) main at steal_time.c:539 (discriminator 7)
     4  0x00007fff8b57af3b: ?? ??:0
     5  0x00007fff8b57b007: ?? ??:0
     6  0x0000000000402b6f: _start at ??:?

Number of guest pages is not compatible with the host. Try npages=4

Fixes fc240715fc50 ("KVM: selftests: arm64: Fix steal_time test after UAPI refactoring")
---
 tools/testing/selftests/kvm/steal_time.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/steal_time.c +1 -0
index 76fcdd1fd3cb..cdb81f3ee4b2 100644
@@ -208,6 +208,7 @@ static void check_steal_time_uapi(void)
 {
     struct kvm_vm *vm;
     struct kvm_vcpu *vcpu;
+    unsigned int gpages;
     u64 st_ipa;
     int ret;
 

tools/testing/selftests/kvm/steal_time.c static void check_steal_time_uapi(void) · line 224

Quoted 6 lines
tools/testing/selftests/kvm/steal_time.c +1 -2
@@ -220,8 +221,9 @@ static void check_steal_time_uapi(void)
     };
 
     vcpu_ioctl(vcpu, KVM_HAS_DEVICE_ATTR, &dev);
-    vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, 1, 0);
-    virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, 1);
+    gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, 1);

Would vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE) be better to describe the number of pages we want to add?

Quoted 5 lines
tools/testing/selftests/kvm/steal_time.c +2 -0
+    vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, gpages, 0);
+    virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, gpages);
 
     st_ipa = (ulong)ST_GPA_BASE | 1;
     ret = __vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev);
#6 lore raw

Re: [PATCH] KVM: selftests: fix steal_time for arm64 with host page size > 4K

Quoted 37 lines · Zenghui Yu
Quoted 36 lines · Sebastian Ott

Fix the following failure when running with 16K host page size:
==== Test Assertion Failure ====
lib/kvm_util.c:991: vm_adjust_num_guest_pages(vm->mode, npages) == npages pid=873 tid=873 errno=0 - Success

     1  0x0000000000405a27: vm_mem_add at kvm_util.c:991
     2  0x000000000040241f: check_steal_time_uapi at steal_time.c:223 (discriminator 7)
     3   (inlined by) main at steal_time.c:539 (discriminator 7)
     4  0x00007fff8b57af3b: ?? ??:0
     5  0x00007fff8b57b007: ?? ??:0
     6  0x0000000000402b6f: _start at ??:?

Number of guest pages is not compatible with the host. Try npages=4

Fixes fc240715fc50 ("KVM: selftests: arm64: Fix steal_time test after UAPI refactoring")
---
 tools/testing/selftests/kvm/steal_time.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/steal_time.c +1 -0
index 76fcdd1fd3cb..cdb81f3ee4b2 100644
@@ -208,6 +208,7 @@ static void check_steal_time_uapi(void)
 {
     struct kvm_vm *vm;
     struct kvm_vcpu *vcpu;
+    unsigned int gpages;
     u64 st_ipa;
     int ret;
 
@@ -220,8 +221,9 @@ static void check_steal_time_uapi(void)
     };
 
Quoted 7 lines

tools/testing/selftests/kvm/steal_time.c static void check_steal_time_uapi(void) · line 224

Quoted 4 lines
tools/testing/selftests/kvm/steal_time.c +1 -2
     vcpu_ioctl(vcpu, KVM_HAS_DEVICE_ATTR, &dev);
-    vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, 1, 0);
-    virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, 1);
+    gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, 1);

Would vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE) be better to describe the number of pages we want to add?

Hm, I don't think we care. This is just for the uapi checker - the vm/vcpu doesn't even run..

Thanks,
Sebastian

#7 lore raw

Re: [PATCH] KVM: selftests: fix steal_time for arm64 with host page size > 4K

Quoted 47 lines · Sebastian Ott
Quoted 37 lines · Zenghui Yu
Quoted 36 lines · Sebastian Ott

Fix the following failure when running with 16K host page size:
==== Test Assertion Failure ====
lib/kvm_util.c:991: vm_adjust_num_guest_pages(vm->mode, npages) == npages pid=873 tid=873 errno=0 - Success

     1  0x0000000000405a27: vm_mem_add at kvm_util.c:991
     2  0x000000000040241f: check_steal_time_uapi at steal_time.c:223 (discriminator 7)
     3   (inlined by) main at steal_time.c:539 (discriminator 7)
     4  0x00007fff8b57af3b: ?? ??:0
     5  0x00007fff8b57b007: ?? ??:0
     6  0x0000000000402b6f: _start at ??:?

Number of guest pages is not compatible with the host. Try npages=4

Fixes fc240715fc50 ("KVM: selftests: arm64: Fix steal_time test after UAPI refactoring")
---
 tools/testing/selftests/kvm/steal_time.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/steal_time.c +1 -0
index 76fcdd1fd3cb..cdb81f3ee4b2 100644
@@ -208,6 +208,7 @@ static void check_steal_time_uapi(void)
 {
     struct kvm_vm *vm;
     struct kvm_vcpu *vcpu;
+    unsigned int gpages;
     u64 st_ipa;
     int ret;
 
@@ -220,8 +221,9 @@ static void check_steal_time_uapi(void)
     };
 
Quoted 7 lines

tools/testing/selftests/kvm/steal_time.c static void check_steal_time_uapi(void) · line 224

Quoted 4 lines
tools/testing/selftests/kvm/steal_time.c +1 -2
     vcpu_ioctl(vcpu, KVM_HAS_DEVICE_ATTR, &dev);
-    vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, 1, 0);
-    virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, 1);
+    gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, 1);

Would vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE) be better to describe the number of pages we want to add?

Quoted 2 lines

Hm, I don't think we care. This is just for the uapi checker - the vm/vcpu doesn't even run..

Even though we don't truly care, we can "fix" that and dedup code at the same time. E.g.

tools/testing/selftests/kvm/steal_time.c +19 -9
index bc3c62b72c58..c0256d4c4b09 100644
@@ -27,6 +27,9 @@
 static void *st_gva[NR_VCPUS];
 static u64 guest_stolen_time[NR_VCPUS];
 
+static struct kvm_vm *vm_create_steal_time(u32 nr_vcpus, void *guest_code,
+                                          struct kvm_vcpu *vcpus[]);
+
 #if defined(__x86_64__)
 
 /* steal_time must have 64-byte alignment */
@@ -210,17 +213,14 @@ static void check_steal_time_uapi(void)
        u64 st_ipa;
        int ret;
 
-       vm = vm_create_with_one_vcpu(&vcpu, NULL);
-
        struct kvm_device_attr dev = {
                .group = KVM_ARM_VCPU_PVTIME_CTRL,
                .attr = KVM_ARM_VCPU_PVTIME_IPA,
                .addr = (u64)&st_ipa,
        };
 
+       vm = vm_create_steal_time(1, NULL, &vcpu);
        vcpu_ioctl(vcpu, KVM_HAS_DEVICE_ATTR, &dev);
-       vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, 1, 0);
-       virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, 1);
 
        st_ipa = (ulong)ST_GPA_BASE | 1;
        ret = __vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev);
@@ -500,13 +500,26 @@ static void run_vcpu(struct kvm_vcpu *vcpu)
        }
 }
 
+static struct kvm_vm *vm_create_steal_time(u32 nr_vcpus, void *guest_code,
+                                          struct kvm_vcpu *vcpus[])
+{
+       unsigned int gpages;
+       struct kvm_vm *vm;
+
+       vm = vm_create_with_vcpus(NR_VCPUS, guest_code, vcpus);
+       gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE * nr_vcpus);
+       vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, gpages, 0);
+       virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, gpages);
+
+       return vm;
+}
+
 int main(int ac, char **av)
 {
        struct kvm_vcpu *vcpus[NR_VCPUS];
        struct kvm_vm *vm;
        pthread_t thread;
        cpu_set_t cpuset;
-       unsigned int gpages;
        long stolen_time;
        long run_delay;
        bool verbose;
@@ -518,10 +531,7 @@ int main(int ac, char **av)
        cpu = pin_self_to_any_cpu();
 
        /* Create a VM and an identity mapped memslot for the steal time structure */
-       vm = vm_create_with_vcpus(NR_VCPUS, guest_code, vcpus);
-       gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE * NR_VCPUS);
-       vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, gpages, 0);
-       virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, gpages);
+       vm = vm_create_steal_time(NR_VCPUS, guest_code, vcpus);
 
        ksft_print_header();
        TEST_REQUIRE(is_steal_time_supported(vcpus[0]));
#8 lore raw

Re: [PATCH] KVM: selftests: fix steal_time for arm64 with host page size > 4K

Quoted 118 lines · Sean Christopherson
Quoted 47 lines · Sebastian Ott
Quoted 37 lines · Zenghui Yu
Quoted 36 lines · Sebastian Ott

Fix the following failure when running with 16K host page size:
==== Test Assertion Failure ====
lib/kvm_util.c:991: vm_adjust_num_guest_pages(vm->mode, npages) == npages pid=873 tid=873 errno=0 - Success

     1  0x0000000000405a27: vm_mem_add at kvm_util.c:991
     2  0x000000000040241f: check_steal_time_uapi at steal_time.c:223 (discriminator 7)
     3   (inlined by) main at steal_time.c:539 (discriminator 7)
     4  0x00007fff8b57af3b: ?? ??:0
     5  0x00007fff8b57b007: ?? ??:0
     6  0x0000000000402b6f: _start at ??:?

Number of guest pages is not compatible with the host. Try npages=4

Fixes fc240715fc50 ("KVM: selftests: arm64: Fix steal_time test after UAPI refactoring")
---
 tools/testing/selftests/kvm/steal_time.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/steal_time.c +1 -0
index 76fcdd1fd3cb..cdb81f3ee4b2 100644
@@ -208,6 +208,7 @@ static void check_steal_time_uapi(void)
 {
     struct kvm_vm *vm;
     struct kvm_vcpu *vcpu;
+    unsigned int gpages;
     u64 st_ipa;
     int ret;
 
@@ -220,8 +221,9 @@ static void check_steal_time_uapi(void)
     };
 
Quoted 7 lines

tools/testing/selftests/kvm/steal_time.c static void check_steal_time_uapi(void)

Quoted 4 lines
tools/testing/selftests/kvm/steal_time.c +1 -2
     vcpu_ioctl(vcpu, KVM_HAS_DEVICE_ATTR, &dev);
-    vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, 1, 0);
-    virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, 1);
+    gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, 1);

Would vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE) be better to describe the number of pages we want to add?

Quoted 2 lines

Hm, I don't think we care. This is just for the uapi checker - the vm/vcpu doesn't even run..

Even though we don't truly care, we can "fix" that and dedup code at the same time. E.g.

tools/testing/selftests/kvm/steal_time.c +18 -5
index bc3c62b72c58..c0256d4c4b09 100644
@@ -27,6 +27,9 @@
 static void *st_gva[NR_VCPUS];
 static u64 guest_stolen_time[NR_VCPUS];
 
+static struct kvm_vm *vm_create_steal_time(u32 nr_vcpus, void *guest_code,
+                                          struct kvm_vcpu *vcpus[]);
+
 #if defined(__x86_64__)
 
 /* steal_time must have 64-byte alignment */
@@ -210,17 +213,14 @@ static void check_steal_time_uapi(void)
       u64 st_ipa;
       int ret;
 
-       vm = vm_create_with_one_vcpu(&vcpu, NULL);
-
       struct kvm_device_attr dev = {
               .group = KVM_ARM_VCPU_PVTIME_CTRL,
               .attr = KVM_ARM_VCPU_PVTIME_IPA,
               .addr = (u64)&st_ipa,
       };
 
+       vm = vm_create_steal_time(1, NULL, &vcpu);
       vcpu_ioctl(vcpu, KVM_HAS_DEVICE_ATTR, &dev);
-       vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, 1, 0);
-       virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, 1);
 
       st_ipa = (ulong)ST_GPA_BASE | 1;
       ret = __vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev);
@@ -500,13 +500,26 @@ static void run_vcpu(struct kvm_vcpu *vcpu)
       }
 }
 
+static struct kvm_vm *vm_create_steal_time(u32 nr_vcpus, void *guest_code,
+                                          struct kvm_vcpu *vcpus[])
+{
+       unsigned int gpages;
+       struct kvm_vm *vm;
+
+       vm = vm_create_with_vcpus(NR_VCPUS, guest_code, vcpus);
+       gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE * nr_vcpus);
+       vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, gpages, 0);
+       virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, gpages);
+
+       return vm;
+}
+
 int main(int ac, char **av)
 {
       struct kvm_vcpu *vcpus[NR_VCPUS];
       struct kvm_vm *vm;
       pthread_t thread;
       cpu_set_t cpuset;
-       unsigned int gpages;
       long stolen_time;
       long run_delay;
       bool verbose;
@@ -518,10 +531,7 @@ int main(int ac, char **av)
       cpu = pin_self_to_any_cpu();

tools/testing/selftests/kvm/steal_time.c int main(int ac, char **av) · line 537

Quoted 10 lines
tools/testing/selftests/kvm/steal_time.c +1 -4
 
       /* Create a VM and an identity mapped memslot for the steal time structure */
-       vm = vm_create_with_vcpus(NR_VCPUS, guest_code, vcpus);
-       gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE * NR_VCPUS);
-       vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, gpages, 0);
-       virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, gpages);
+       vm = vm_create_steal_time(NR_VCPUS, guest_code, vcpus);
 
       ksft_print_header();
       TEST_REQUIRE(is_steal_time_supported(vcpus[0]));

Ugh, somehow I thought that this was already merged. V2 can be found
here: https://lore.kernel.org/kvmarm/[email protected]/

Thanks,
Sebastian