Skip to content

Commit 9397b53

Browse files
Paul Durrantsean-jc
authored andcommitted
KVM: selftests: map Xen's shared_info page using HVA rather than GFN
Using the HVA of the shared_info page is more efficient, so if the capability (KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA) is present use that method to do the mapping. NOTE: Have the juggle_shinfo_state() thread map and unmap using both GFN and HVA, to make sure the older mechanism is not broken. Signed-off-by: Paul Durrant <pdurrant@amazon.com> Reviewed-by: David Woodhouse <dwmw@amazon.co.uk> Link: https://lore.kernel.org/r/20240215152916.1158-15-paul@xen.org Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 3991f35 commit 9397b53

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ static int cmp_timespec(struct timespec *a, struct timespec *b)
389389
return 0;
390390
}
391391

392+
static struct shared_info *shinfo;
392393
static struct vcpu_info *vinfo;
393394
static struct kvm_vcpu *vcpu;
394395

@@ -404,20 +405,38 @@ static void *juggle_shinfo_state(void *arg)
404405
{
405406
struct kvm_vm *vm = (struct kvm_vm *)arg;
406407

407-
struct kvm_xen_hvm_attr cache_activate = {
408+
struct kvm_xen_hvm_attr cache_activate_gfn = {
408409
.type = KVM_XEN_ATTR_TYPE_SHARED_INFO,
409410
.u.shared_info.gfn = SHINFO_REGION_GPA / PAGE_SIZE
410411
};
411412

412-
struct kvm_xen_hvm_attr cache_deactivate = {
413+
struct kvm_xen_hvm_attr cache_deactivate_gfn = {
413414
.type = KVM_XEN_ATTR_TYPE_SHARED_INFO,
414415
.u.shared_info.gfn = KVM_XEN_INVALID_GFN
415416
};
416417

418+
struct kvm_xen_hvm_attr cache_activate_hva = {
419+
.type = KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA,
420+
.u.shared_info.hva = (unsigned long)shinfo
421+
};
422+
423+
struct kvm_xen_hvm_attr cache_deactivate_hva = {
424+
.type = KVM_XEN_ATTR_TYPE_SHARED_INFO,
425+
.u.shared_info.hva = 0
426+
};
427+
428+
int xen_caps = kvm_check_cap(KVM_CAP_XEN_HVM);
429+
417430
for (;;) {
418-
__vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_activate);
419-
__vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_deactivate);
431+
__vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_activate_gfn);
420432
pthread_testcancel();
433+
__vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_deactivate_gfn);
434+
435+
if (xen_caps & KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA) {
436+
__vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_activate_hva);
437+
pthread_testcancel();
438+
__vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_deactivate_hva);
439+
}
421440
}
422441

423442
return NULL;
@@ -442,6 +461,7 @@ int main(int argc, char *argv[])
442461
bool do_runstate_flag = !!(xen_caps & KVM_XEN_HVM_CONFIG_RUNSTATE_UPDATE_FLAG);
443462
bool do_eventfd_tests = !!(xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL);
444463
bool do_evtchn_tests = do_eventfd_tests && !!(xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_SEND);
464+
bool has_shinfo_hva = !!(xen_caps & KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA);
445465

446466
clock_gettime(CLOCK_REALTIME, &min_ts);
447467

@@ -452,7 +472,7 @@ int main(int argc, char *argv[])
452472
SHINFO_REGION_GPA, SHINFO_REGION_SLOT, 3, 0);
453473
virt_map(vm, SHINFO_REGION_GVA, SHINFO_REGION_GPA, 3);
454474

455-
struct shared_info *shinfo = addr_gpa2hva(vm, SHINFO_VADDR);
475+
shinfo = addr_gpa2hva(vm, SHINFO_VADDR);
456476

457477
int zero_fd = open("/dev/zero", O_RDONLY);
458478
TEST_ASSERT(zero_fd != -1, "Failed to open /dev/zero");
@@ -488,10 +508,16 @@ int main(int argc, char *argv[])
488508
"Failed to read back RUNSTATE_UPDATE_FLAG attr");
489509
}
490510

491-
struct kvm_xen_hvm_attr ha = {
492-
.type = KVM_XEN_ATTR_TYPE_SHARED_INFO,
493-
.u.shared_info.gfn = SHINFO_REGION_GPA / PAGE_SIZE,
494-
};
511+
struct kvm_xen_hvm_attr ha = {};
512+
513+
if (has_shinfo_hva) {
514+
ha.type = KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA;
515+
ha.u.shared_info.hva = (unsigned long)shinfo;
516+
} else {
517+
ha.type = KVM_XEN_ATTR_TYPE_SHARED_INFO;
518+
ha.u.shared_info.gfn = SHINFO_ADDR / PAGE_SIZE;
519+
}
520+
495521
vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &ha);
496522

497523
/*

0 commit comments

Comments
 (0)