* [PATCH v2] KVM: selftests: x86: Prioritize getting max_gfn from GuestPhysBits
@ 2024-05-10 2:03 Tao Su
2024-05-11 7:08 ` Xiaoyao Li
2024-05-13 1:44 ` Chao Gao
0 siblings, 2 replies; 6+ messages in thread
From: Tao Su @ 2024-05-10 2:03 UTC (permalink / raw)
To: kvm; +Cc: seanjc, pbonzini, chao.gao, xiaoyao.li, yi1.lai, tao1.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.
Signed-off-by: Tao Su <tao1.su@linux.intel.com>
Tested-by: Yi Lai <yi1.lai@intel.com>
---
This patch is based on https://github.com/kvm-x86/linux/commit/b628cb523c65
Changelog:
v1 -> v2:
- Only adjust vm->max_gfn in vm_compute_max_gfn()
- Add Yi Lai's Tested-by
v1: https://lore.kernel.org/all/20240508064205.15301-1-tao1.su@linux.intel.com/
---
tools/testing/selftests/kvm/include/x86_64/processor.h | 1 +
tools/testing/selftests/kvm/lib/x86_64/processor.c | 10 ++++++++--
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
index 81ce37ec407d..ff99f66d81a0 100644
--- a/tools/testing/selftests/kvm/include/x86_64/processor.h
+++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
@@ -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)
diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
index 74a4c736c9ae..aa9966ead543 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
@@ -1293,10 +1293,16 @@ 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, max_bits = 0;
uint8_t maxphyaddr;
- max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1;
+ if (kvm_cpu_has_p(X86_PROPERTY_MAX_GUEST_PHY_ADDR))
+ max_bits = kvm_cpu_property(X86_PROPERTY_MAX_GUEST_PHY_ADDR);
+
+ if (!max_bits)
+ max_bits = vm->pa_bits;
+
+ max_gfn = (1ULL << (max_bits - vm->page_shift)) - 1;
/* Avoid reserved HyperTransport region on AMD processors. */
if (!host_cpu_is_amd)
base-commit: 448b3fe5a0eab5b625a7e15c67c7972169e47ff8
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2] KVM: selftests: x86: Prioritize getting max_gfn from GuestPhysBits
2024-05-10 2:03 [PATCH v2] KVM: selftests: x86: Prioritize getting max_gfn from GuestPhysBits Tao Su
@ 2024-05-11 7:08 ` Xiaoyao Li
2024-05-11 9:13 ` Tao Su
2024-05-13 1:44 ` Chao Gao
1 sibling, 1 reply; 6+ messages in thread
From: Xiaoyao Li @ 2024-05-11 7:08 UTC (permalink / raw)
To: Tao Su, kvm; +Cc: seanjc, pbonzini, chao.gao, yi1.lai
On 5/10/2024 10:03 AM, Tao Su wrote:
> 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.
>
> Signed-off-by: Tao Su <tao1.su@linux.intel.com>
> Tested-by: Yi Lai <yi1.lai@intel.com>
> ---
> This patch is based on https://github.com/kvm-x86/linux/commit/b628cb523c65
>
> Changelog:
> v1 -> v2:
> - Only adjust vm->max_gfn in vm_compute_max_gfn()
> - Add Yi Lai's Tested-by
>
> v1: https://lore.kernel.org/all/20240508064205.15301-1-tao1.su@linux.intel.com/
> ---
> tools/testing/selftests/kvm/include/x86_64/processor.h | 1 +
> tools/testing/selftests/kvm/lib/x86_64/processor.c | 10 ++++++++--
> 2 files changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
> index 81ce37ec407d..ff99f66d81a0 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/processor.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
> @@ -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)
>
> diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> index 74a4c736c9ae..aa9966ead543 100644
> --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
> +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> @@ -1293,10 +1293,16 @@ 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, max_bits = 0;
> uint8_t maxphyaddr;
>
> - max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1;
> + if (kvm_cpu_has_p(X86_PROPERTY_MAX_GUEST_PHY_ADDR))
> + max_bits = kvm_cpu_property(X86_PROPERTY_MAX_GUEST_PHY_ADDR);
We can get rid of the kvm_cpu_has_p(X86_PROPERTY_MAX_GUEST_PHY_ADDR)
check and call kvm_cpu_property() unconditionally. As a bonus, we don't
need to init max_bits as 0.
BTW, could we just name it guest_pa_bits?
Otherwise,
Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com>
> +
> + if (!max_bits)
> + max_bits = vm->pa_bits;
> +
> + max_gfn = (1ULL << (max_bits - vm->page_shift)) - 1;
>
> /* Avoid reserved HyperTransport region on AMD processors. */
> if (!host_cpu_is_amd)
>
> base-commit: 448b3fe5a0eab5b625a7e15c67c7972169e47ff8
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] KVM: selftests: x86: Prioritize getting max_gfn from GuestPhysBits
2024-05-11 7:08 ` Xiaoyao Li
@ 2024-05-11 9:13 ` Tao Su
0 siblings, 0 replies; 6+ messages in thread
From: Tao Su @ 2024-05-11 9:13 UTC (permalink / raw)
To: Xiaoyao Li; +Cc: kvm, seanjc, pbonzini, chao.gao, yi1.lai
On Sat, May 11, 2024 at 03:08:16PM +0800, Xiaoyao Li wrote:
[...]
> > diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> > index 74a4c736c9ae..aa9966ead543 100644
> > --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
> > +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> > @@ -1293,10 +1293,16 @@ 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, max_bits = 0;
> > uint8_t maxphyaddr;
> > - max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1;
> > + if (kvm_cpu_has_p(X86_PROPERTY_MAX_GUEST_PHY_ADDR))
> > + max_bits = kvm_cpu_property(X86_PROPERTY_MAX_GUEST_PHY_ADDR);
>
> We can get rid of the kvm_cpu_has_p(X86_PROPERTY_MAX_GUEST_PHY_ADDR) check
> and call kvm_cpu_property() unconditionally. As a bonus, we don't need to
> init max_bits as 0.
Thanks, good suggestion!
>
> BTW, could we just name it guest_pa_bits?
Yes, it will be more accurate.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] KVM: selftests: x86: Prioritize getting max_gfn from GuestPhysBits
2024-05-10 2:03 [PATCH v2] KVM: selftests: x86: Prioritize getting max_gfn from GuestPhysBits Tao Su
2024-05-11 7:08 ` Xiaoyao Li
@ 2024-05-13 1:44 ` Chao Gao
2024-05-13 2:00 ` Tao Su
1 sibling, 1 reply; 6+ messages in thread
From: Chao Gao @ 2024-05-13 1:44 UTC (permalink / raw)
To: Tao Su; +Cc: kvm, seanjc, pbonzini, xiaoyao.li, yi1.lai
On Fri, May 10, 2024 at 10:03:46AM +0800, Tao Su wrote:
>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.
>
>Signed-off-by: Tao Su <tao1.su@linux.intel.com>
>Tested-by: Yi Lai <yi1.lai@intel.com>
>---
>This patch is based on https://github.com/kvm-x86/linux/commit/b628cb523c65
>
>Changelog:
>v1 -> v2:
> - Only adjust vm->max_gfn in vm_compute_max_gfn()
> - Add Yi Lai's Tested-by
>
>v1: https://lore.kernel.org/all/20240508064205.15301-1-tao1.su@linux.intel.com/
>---
> tools/testing/selftests/kvm/include/x86_64/processor.h | 1 +
> tools/testing/selftests/kvm/lib/x86_64/processor.c | 10 ++++++++--
> 2 files changed, 9 insertions(+), 2 deletions(-)
>
>diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
>index 81ce37ec407d..ff99f66d81a0 100644
>--- a/tools/testing/selftests/kvm/include/x86_64/processor.h
>+++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
>@@ -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)
>
>diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
>index 74a4c736c9ae..aa9966ead543 100644
>--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
>+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
>@@ -1293,10 +1293,16 @@ 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, max_bits = 0;
nit: max_bits has only 8 bits. so max_bits should be uint8_t.
> uint8_t maxphyaddr;
>
>- max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1;
>+ if (kvm_cpu_has_p(X86_PROPERTY_MAX_GUEST_PHY_ADDR))
>+ max_bits = kvm_cpu_property(X86_PROPERTY_MAX_GUEST_PHY_ADDR);
>+
>+ if (!max_bits)
>+ max_bits = vm->pa_bits;
>+
>+ max_gfn = (1ULL << (max_bits - vm->page_shift)) - 1;
>
> /* Avoid reserved HyperTransport region on AMD processors. */
> if (!host_cpu_is_amd)
>
>base-commit: 448b3fe5a0eab5b625a7e15c67c7972169e47ff8
>--
>2.34.1
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] KVM: selftests: x86: Prioritize getting max_gfn from GuestPhysBits
2024-05-13 1:44 ` Chao Gao
@ 2024-05-13 2:00 ` Tao Su
2024-05-13 2:48 ` Chao Gao
0 siblings, 1 reply; 6+ messages in thread
From: Tao Su @ 2024-05-13 2:00 UTC (permalink / raw)
To: Chao Gao; +Cc: kvm, seanjc, pbonzini, xiaoyao.li, yi1.lai
On Mon, May 13, 2024 at 09:44:32AM +0800, Chao Gao wrote:
> On Fri, May 10, 2024 at 10:03:46AM +0800, Tao Su wrote:
> >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.
> >
> >Signed-off-by: Tao Su <tao1.su@linux.intel.com>
> >Tested-by: Yi Lai <yi1.lai@intel.com>
> >---
> >This patch is based on https://github.com/kvm-x86/linux/commit/b628cb523c65
> >
> >Changelog:
> >v1 -> v2:
> > - Only adjust vm->max_gfn in vm_compute_max_gfn()
> > - Add Yi Lai's Tested-by
> >
> >v1: https://lore.kernel.org/all/20240508064205.15301-1-tao1.su@linux.intel.com/
> >---
> > tools/testing/selftests/kvm/include/x86_64/processor.h | 1 +
> > tools/testing/selftests/kvm/lib/x86_64/processor.c | 10 ++++++++--
> > 2 files changed, 9 insertions(+), 2 deletions(-)
> >
> >diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
> >index 81ce37ec407d..ff99f66d81a0 100644
> >--- a/tools/testing/selftests/kvm/include/x86_64/processor.h
> >+++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
> >@@ -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)
> >
> >diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> >index 74a4c736c9ae..aa9966ead543 100644
> >--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
> >+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
> >@@ -1293,10 +1293,16 @@ 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, max_bits = 0;
>
> nit: max_bits has only 8 bits. so max_bits should be uint8_t.
Because vm->pa_bits is unsigned int, I'm worried that the compiler will
complain on stricter compilation, what do you think?
>
> > uint8_t maxphyaddr;
> >
> >- max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1;
> >+ if (kvm_cpu_has_p(X86_PROPERTY_MAX_GUEST_PHY_ADDR))
> >+ max_bits = kvm_cpu_property(X86_PROPERTY_MAX_GUEST_PHY_ADDR);
> >+
> >+ if (!max_bits)
> >+ max_bits = vm->pa_bits;
> >+
> >+ max_gfn = (1ULL << (max_bits - vm->page_shift)) - 1;
> >
> > /* Avoid reserved HyperTransport region on AMD processors. */
> > if (!host_cpu_is_amd)
> >
> >base-commit: 448b3fe5a0eab5b625a7e15c67c7972169e47ff8
> >--
> >2.34.1
> >
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2] KVM: selftests: x86: Prioritize getting max_gfn from GuestPhysBits
2024-05-13 2:00 ` Tao Su
@ 2024-05-13 2:48 ` Chao Gao
0 siblings, 0 replies; 6+ messages in thread
From: Chao Gao @ 2024-05-13 2:48 UTC (permalink / raw)
To: Tao Su; +Cc: kvm, seanjc, pbonzini, xiaoyao.li, yi1.lai
On Mon, May 13, 2024 at 10:00:35AM +0800, Tao Su wrote:
>On Mon, May 13, 2024 at 09:44:32AM +0800, Chao Gao wrote:
>> On Fri, May 10, 2024 at 10:03:46AM +0800, Tao Su wrote:
>> >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.
>> >
>> >Signed-off-by: Tao Su <tao1.su@linux.intel.com>
>> >Tested-by: Yi Lai <yi1.lai@intel.com>
>> >---
>> >This patch is based on https://github.com/kvm-x86/linux/commit/b628cb523c65
>> >
>> >Changelog:
>> >v1 -> v2:
>> > - Only adjust vm->max_gfn in vm_compute_max_gfn()
>> > - Add Yi Lai's Tested-by
>> >
>> >v1: https://lore.kernel.org/all/20240508064205.15301-1-tao1.su@linux.intel.com/
>> >---
>> > tools/testing/selftests/kvm/include/x86_64/processor.h | 1 +
>> > tools/testing/selftests/kvm/lib/x86_64/processor.c | 10 ++++++++--
>> > 2 files changed, 9 insertions(+), 2 deletions(-)
>> >
>> >diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
>> >index 81ce37ec407d..ff99f66d81a0 100644
>> >--- a/tools/testing/selftests/kvm/include/x86_64/processor.h
>> >+++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
>> >@@ -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)
>> >
>> >diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c
>> >index 74a4c736c9ae..aa9966ead543 100644
>> >--- a/tools/testing/selftests/kvm/lib/x86_64/processor.c
>> >+++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c
>> >@@ -1293,10 +1293,16 @@ 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, max_bits = 0;
>>
>> nit: max_bits has only 8 bits. so max_bits should be uint8_t.
>
>Because vm->pa_bits is unsigned int, I'm worried that the compiler will
>complain on stricter compilation, what do you think?
@maxphyaddr (right below) is in the same situation.
And if it was a problem for the compiler, casting vm->page_shift to uint8_t
explicitly would be a better solution.
>
>>
>> > uint8_t maxphyaddr;
>> >
>> >- max_gfn = (1ULL << (vm->pa_bits - vm->page_shift)) - 1;
>> >+ if (kvm_cpu_has_p(X86_PROPERTY_MAX_GUEST_PHY_ADDR))
>> >+ max_bits = kvm_cpu_property(X86_PROPERTY_MAX_GUEST_PHY_ADDR);
>> >+
>> >+ if (!max_bits)
>> >+ max_bits = vm->pa_bits;
>> >+
>> >+ max_gfn = (1ULL << (max_bits - vm->page_shift)) - 1;
>> >
>> > /* Avoid reserved HyperTransport region on AMD processors. */
>> > if (!host_cpu_is_amd)
>> >
>> >base-commit: 448b3fe5a0eab5b625a7e15c67c7972169e47ff8
>> >--
>> >2.34.1
>> >
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-05-13 2:49 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-10 2:03 [PATCH v2] KVM: selftests: x86: Prioritize getting max_gfn from GuestPhysBits Tao Su
2024-05-11 7:08 ` Xiaoyao Li
2024-05-11 9:13 ` Tao Su
2024-05-13 1:44 ` Chao Gao
2024-05-13 2:00 ` Tao Su
2024-05-13 2:48 ` Chao Gao
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox