Skip to contentSkip to conversation

KVM: selftests: x86: Prioritize getting max_gfn from GuestPhysBits

PATCH Applied Tao Su 2 participants 2 messages May 13, 2024 – Jun 4, 2024

Subsystems (2)
lore
Share & tools
lore

Tao Su

author
#0 lore raw

PATCH v3 1/1 KVM: selftests: x86: Prioritize getting max_gfn from GuestPhysBits

Use the max mappable GPA via GuestPhysBits advertised by KVM to calculate max_gfn. Currently some selftests (e.g. access_tracking_perf_test, dirty_log_test...) add RAM regions close to max_gfn, so guest may access GPA beyond its mappable range and cause infinite loop.

Adjust max_gfn in vm_compute_max_gfn() since x86 selftests already overrides vm_compute_max_gfn() specifically to deal with goofy edge cases.

---
This patch is based on https://github.com/kvm-x86/linux/commit/b628cb523c65

Changelog:
v2 -> v3:
- Drop kvm_cpu_has_p(X86_PROPERTY_MAX_GUEST_PHY_ADDR) check
- Change max_bits to guest_pa_bits
- Add Yi Lai's Reported-by and Xiaoyao's Reviewed-by

v1 -> v2:
- Only adjust vm->max_gfn in vm_compute_max_gfn()
- Add Yi Lai's Tested-by

v1: https://lore.kernel.org/all/[email protected]/

---
 tools/testing/selftests/kvm/include/x86_64/processor.h | 1 +
 tools/testing/selftests/kvm/lib/x86_64/processor.c     | 9 +++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +1 -0
index 81ce37ec407d..ff99f66d81a0 100644
@@ -282,6 +282,7 @@ struct kvm_x86_cpu_property {
 #define X86_PROPERTY_MAX_EXT_LEAF		KVM_X86_CPU_PROPERTY(0x80000000, 0, EAX, 0, 31)
 #define X86_PROPERTY_MAX_PHY_ADDR		KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 0, 7)
 #define X86_PROPERTY_MAX_VIRT_ADDR		KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 8, 15)
+#define X86_PROPERTY_MAX_GUEST_PHY_ADDR		KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 16, 23)
 #define X86_PROPERTY_SEV_C_BIT			KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 0, 5)
 #define X86_PROPERTY_PHYS_ADDR_REDUCTION	KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 6, 11)
 
tools/testing/selftests/kvm/lib/x86_64/processor.c +7 -2
index 74a4c736c9ae..9458b36a30a8 100644
@@ -1293,10 +1293,15 @@ const struct kvm_cpuid2 *vcpu_get_supported_hv_cpuid(struct kvm_vcpu *vcpu)
 unsigned long vm_compute_max_gfn(struct kvm_vm *vm)
 {
 	const unsigned long num_ht_pages = 12 << (30 - vm->page_shift); /* 12 GiB */
-	unsigned long ht_gfn, max_gfn, max_pfn;
+	unsigned long ht_gfn, max_gfn, max_pfn, guest_pa_bits;
 	uint8_t maxphyaddr;
 
-	max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1;
+	guest_pa_bits = kvm_cpu_property(X86_PROPERTY_MAX_GUEST_PHY_ADDR);
+
+	if (!guest_pa_bits)
+		guest_pa_bits = vm->pa_bits;
+
+	max_gfn = (1ULL << (guest_pa_bits - vm->page_shift)) - 1;
 
 	/* Avoid reserved HyperTransport region on AMD processors.  */
 	if (!host_cpu_is_amd)

base-commit: a38297e3fb012ddfa7ce0321a7e5a8daeb1872b6

Signature · 1 line
2.34.1
reviewer Applied
#1 lore raw
Quoted 9 lines · Tao Su

Use the max mappable GPA via GuestPhysBits advertised by KVM to calculate max_gfn. Currently some selftests (e.g. access_tracking_perf_test, dirty_log_test...) add RAM regions close to max_gfn, so guest may access GPA beyond its mappable range and cause infinite loop.

Adjust max_gfn in vm_compute_max_gfn() since x86 selftests already overrides vm_compute_max_gfn() specifically to deal with goofy edge cases.

[...]

Applied to kvm-x86 fixes, thanks!

[1/1] KVM: selftests: x86: Prioritize getting max_gfn from GuestPhysBits

      https://github.com/kvm-x86/linux/commit/b24f5cf7b2ae

--
https://github.com/kvm-x86/linux/tree/next