Skip to contentSkip to conversation

KVM: selftests: x86: Prioritize getting max_gfn from GuestPhysBits

PATCH Superseded Tao Su 2 participants 3 messages May 8, 2024 – May 9, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tao Su

author
#0 lore raw

PATCH 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.

Prioritize obtaining pa_bits from the GuestPhysBits field advertised by KVM, so max_gfn can be limited to the mappable range.

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

---
 tools/testing/selftests/kvm/include/x86_64/processor.h | 1 +
 tools/testing/selftests/kvm/lib/x86_64/processor.c     | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)
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 +3 -1
index 74a4c736c9ae..6c69f1dfeed2 100644
@@ -1074,7 +1074,9 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
 		*pa_bits = kvm_cpu_has(X86_FEATURE_PAE) ? 36 : 32;
 		*va_bits = 32;
 	} else {
-		*pa_bits = kvm_cpu_property(X86_PROPERTY_MAX_PHY_ADDR);
+		*pa_bits = kvm_cpu_property(X86_PROPERTY_MAX_GUEST_PHY_ADDR);
+		if (*pa_bits == 0)
+			*pa_bits = kvm_cpu_property(X86_PROPERTY_MAX_PHY_ADDR);
 		*va_bits = kvm_cpu_property(X86_PROPERTY_MAX_VIRT_ADDR);
 	}
 }

base-commit: dccb07f2914cdab2ac3a5b6c98406f765acab803

Signature · 1 line
2.34.1
#1 lore raw
Quoted 33 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.

Prioritize obtaining pa_bits from the GuestPhysBits field advertised by KVM, so max_gfn can be limited to the mappable range.

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

---
 tools/testing/selftests/kvm/include/x86_64/processor.h | 1 +
 tools/testing/selftests/kvm/lib/x86_64/processor.c     | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)
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 +0 -0
index 74a4c736c9ae..6c69f1dfeed2 100644

tools/testing/selftests/kvm/lib/x86_64/processor.c void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) · line 1079

Quoted 7 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +3 -1
@@ -1074,7 +1074,9 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
 		*pa_bits = kvm_cpu_has(X86_FEATURE_PAE) ? 36 : 32;
 		*va_bits = 32;
 	} else {
-		*pa_bits = kvm_cpu_property(X86_PROPERTY_MAX_PHY_ADDR);
+		*pa_bits = kvm_cpu_property(X86_PROPERTY_MAX_GUEST_PHY_ADDR);
+		if (*pa_bits == 0)
+			*pa_bits = kvm_cpu_property(X86_PROPERTY_MAX_PHY_ADDR);

This is the wrong place to incorporaate the max mappable GPA. The pa_bits field should reflect the "real" MAXPHYADDR, it's vm->max_gfn that needs to be adjusted, and x86 selftests already overrides vm_compute_max_gfn() specifically to deal with goofy edge cases like this.

Quoted 8 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +0 -0
 		*va_bits = kvm_cpu_property(X86_PROPERTY_MAX_VIRT_ADDR);
 	}
 }

base-commit: dccb07f2914cdab2ac3a5b6c98406f765acab803

Signature · 1 line
2.34.1
author
#2 lore raw
Quoted 38 lines · Sean Christopherson
Quoted 37 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.

Prioritize obtaining pa_bits from the GuestPhysBits field advertised by KVM, so max_gfn can be limited to the mappable range.

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

---
 tools/testing/selftests/kvm/include/x86_64/processor.h | 1 +
 tools/testing/selftests/kvm/lib/x86_64/processor.c     | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)
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 +0 -1
index 74a4c736c9ae..6c69f1dfeed2 100644
@@ -1074,7 +1074,9 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
 		*pa_bits = kvm_cpu_has(X86_FEATURE_PAE) ? 36 : 32;
 		*va_bits = 32;
 	} else {
-		*pa_bits = kvm_cpu_property(X86_PROPERTY_MAX_PHY_ADDR);
Quoted 8 lines

tools/testing/selftests/kvm/lib/x86_64/processor.c void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) · line 1079

Quoted 3 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +3 -0
+		*pa_bits = kvm_cpu_property(X86_PROPERTY_MAX_GUEST_PHY_ADDR);
+		if (*pa_bits == 0)
+			*pa_bits = kvm_cpu_property(X86_PROPERTY_MAX_PHY_ADDR);

This is the wrong place to incorporaate the max mappable GPA. The pa_bits field should reflect the "real" MAXPHYADDR, it's vm->max_gfn that needs to be adjusted, and x86 selftests already overrides vm_compute_max_gfn() specifically to deal with goofy edge cases like this.

Thanks for pointing out! I will adjust max_gfn in the vm_compute_max_gfn() overrided by x86 selftests.

Quoted 9 lines
Quoted 9 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +0 -0
 		*va_bits = kvm_cpu_property(X86_PROPERTY_MAX_VIRT_ADDR);
 	}
 }

base-commit: dccb07f2914cdab2ac3a5b6c98406f765acab803

Signature · 1 line
2.34.1