diff mbox series

[1/7] hyperv: Detect Nested virtualization support for SVM

Message ID e14dac75ff1088b2c4bea361954b37e414edd03c.1617804573.git.viremana@linux.microsoft.com (mailing list archive)
State New, archived
Headers show
Series Hyper-V nested virt enlightenments for SVM | expand

Commit Message

Vineeth Pillai April 7, 2021, 2:41 p.m. UTC
Detect nested features exposed by Hyper-V if SVM is enabled.

Signed-off-by: Vineeth Pillai <viremana@linux.microsoft.com>
---
 arch/x86/kernel/cpu/mshyperv.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

Comments

Borislav Petkov April 7, 2021, 4:09 p.m. UTC | #1
On Wed, Apr 07, 2021 at 02:41:22PM +0000, Vineeth Pillai wrote:
> Detect nested features exposed by Hyper-V if SVM is enabled.
> 
> Signed-off-by: Vineeth Pillai <viremana@linux.microsoft.com>
> ---
>  arch/x86/kernel/cpu/mshyperv.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c
> index 3546d3e21787..4d364acfe95d 100644
> --- a/arch/x86/kernel/cpu/mshyperv.c
> +++ b/arch/x86/kernel/cpu/mshyperv.c
> @@ -325,9 +325,17 @@ static void __init ms_hyperv_init_platform(void)
>  			ms_hyperv.isolation_config_a, ms_hyperv.isolation_config_b);
>  	}
>  
> -	if (ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) {
> +	/*
> +	 * AMD does not need enlightened VMCS as VMCB is already a
> +	 * datastructure in memory. We need to get the nested
> +	 * features if SVM is enabled.
> +	 */
> +	if (boot_cpu_has(X86_FEATURE_SVM) ||

Pls use:

	    cpu_feature_enabled

here.
Michael Kelley (LINUX) April 7, 2021, 7:55 p.m. UTC | #2
From: Vineeth Pillai <viremana@linux.microsoft.com> Sent: Wednesday, April 7, 2021 7:41 AM
> 
> Detect nested features exposed by Hyper-V if SVM is enabled.
> 
> Signed-off-by: Vineeth Pillai <viremana@linux.microsoft.com>
> ---
>  arch/x86/kernel/cpu/mshyperv.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c
> index 3546d3e21787..4d364acfe95d 100644
> --- a/arch/x86/kernel/cpu/mshyperv.c
> +++ b/arch/x86/kernel/cpu/mshyperv.c
> @@ -325,9 +325,17 @@ static void __init ms_hyperv_init_platform(void)
>  			ms_hyperv.isolation_config_a, ms_hyperv.isolation_config_b);
>  	}
> 
> -	if (ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) {
> +	/*
> +	 * AMD does not need enlightened VMCS as VMCB is already a
> +	 * datastructure in memory. We need to get the nested
> +	 * features if SVM is enabled.
> +	 */
> +	if (boot_cpu_has(X86_FEATURE_SVM) ||
> +	    ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) {
>  		ms_hyperv.nested_features =
>  			cpuid_eax(HYPERV_CPUID_NESTED_FEATURES);
> +		pr_info("Hyper-V nested_features: 0x%x\n",

Nit:  Most other similar lines put the colon in a different place:

		pr_info("Hyper-V: nested features 0x%x\n",

One of these days, I'm going to fix the ones that don't follow this
pattern. :-)

> +			ms_hyperv.nested_features);
>  	}
> 
>  	/*
> --
> 2.25.1
Sean Christopherson April 7, 2021, 10:48 p.m. UTC | #3
On Wed, Apr 07, 2021, Michael Kelley wrote:
> From: Vineeth Pillai <viremana@linux.microsoft.com> Sent: Wednesday, April 7, 2021 7:41 AM
> > 
> > Detect nested features exposed by Hyper-V if SVM is enabled.
> > 
> > Signed-off-by: Vineeth Pillai <viremana@linux.microsoft.com>
> > ---
> >  arch/x86/kernel/cpu/mshyperv.c | 10 +++++++++-
> >  1 file changed, 9 insertions(+), 1 deletion(-)
> > 
> > diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c
> > index 3546d3e21787..4d364acfe95d 100644
> > --- a/arch/x86/kernel/cpu/mshyperv.c
> > +++ b/arch/x86/kernel/cpu/mshyperv.c
> > @@ -325,9 +325,17 @@ static void __init ms_hyperv_init_platform(void)
> >  			ms_hyperv.isolation_config_a, ms_hyperv.isolation_config_b);
> >  	}
> > 
> > -	if (ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) {
> > +	/*
> > +	 * AMD does not need enlightened VMCS as VMCB is already a
> > +	 * datastructure in memory. We need to get the nested
> > +	 * features if SVM is enabled.
> > +	 */
> > +	if (boot_cpu_has(X86_FEATURE_SVM) ||
> > +	    ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) {
> >  		ms_hyperv.nested_features =
> >  			cpuid_eax(HYPERV_CPUID_NESTED_FEATURES);
> > +		pr_info("Hyper-V nested_features: 0x%x\n",
> 
> Nit:  Most other similar lines put the colon in a different place:
> 
> 		pr_info("Hyper-V: nested features 0x%x\n",
> 
> One of these days, I'm going to fix the ones that don't follow this
> pattern. :-)

Any reason not to use pr_fmt?
Vitaly Kuznetsov April 8, 2021, 11:06 a.m. UTC | #4
Vineeth Pillai <viremana@linux.microsoft.com> writes:

> Detect nested features exposed by Hyper-V if SVM is enabled.
>
> Signed-off-by: Vineeth Pillai <viremana@linux.microsoft.com>
> ---
>  arch/x86/kernel/cpu/mshyperv.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c
> index 3546d3e21787..4d364acfe95d 100644
> --- a/arch/x86/kernel/cpu/mshyperv.c
> +++ b/arch/x86/kernel/cpu/mshyperv.c
> @@ -325,9 +325,17 @@ static void __init ms_hyperv_init_platform(void)
>  			ms_hyperv.isolation_config_a, ms_hyperv.isolation_config_b);
>  	}
>  
> -	if (ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) {
> +	/*
> +	 * AMD does not need enlightened VMCS as VMCB is already a
> +	 * datastructure in memory. 

Well, VMCS is also a structure in memory, isn't it? It's just that we
don't have a 'clean field' concept for it and we can't use normal memory
accesses.

> 	We need to get the nested
> +	 * features if SVM is enabled.
> +	 */
> +	if (boot_cpu_has(X86_FEATURE_SVM) ||
> +	    ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) {

Do I understand correctly that we can just look at CPUID.0x40000000.EAX
and in case it is >= 0x4000000A we can read HYPERV_CPUID_NESTED_FEATURES
leaf? I'd suggest we do that intead then.

>  		ms_hyperv.nested_features =
>  			cpuid_eax(HYPERV_CPUID_NESTED_FEATURES);
> +		pr_info("Hyper-V nested_features: 0x%x\n",
> +			ms_hyperv.nested_features);
>  	}
>  
>  	/*
Vineeth Pillai April 8, 2021, 1:15 p.m. UTC | #5
Hi Vitaly,

On 4/8/21 7:06 AM, Vitaly Kuznetsov wrote:
> -	if (ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) {
> +	/*
> +	 * AMD does not need enlightened VMCS as VMCB is already a
> +	 * datastructure in memory.
> Well, VMCS is also a structure in memory, isn't it? It's just that we
> don't have a 'clean field' concept for it and we can't use normal memory
> accesses.

Yes, you are right. I was referring to the fact that we cant use normal
memory accesses, but is a bit mis-worded.

>
>> 	We need to get the nested
>> +	 * features if SVM is enabled.
>> +	 */
>> +	if (boot_cpu_has(X86_FEATURE_SVM) ||
>> +	    ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) {
> Do I understand correctly that we can just look at CPUID.0x40000000.EAX
> and in case it is >= 0x4000000A we can read HYPERV_CPUID_NESTED_FEATURES
> leaf? I'd suggest we do that intead then.
I agree, that is a better way to consolidate both the cases.
Will change it in the next iteration. Probably the above code
comment is not needed when we consolidate the cases here.

Thanks,
Vineeth
Vineeth Pillai April 8, 2021, 1:26 p.m. UTC | #6
On 4/7/21 6:48 PM, Sean Christopherson wrote:
> On Wed, Apr 07, 2021, Michael Kelley wrote:
>>
>>> +		pr_info("Hyper-V nested_features: 0x%x\n",
>> Nit:  Most other similar lines put the colon in a different place:
>>
>> 		pr_info("Hyper-V: nested features 0x%x\n",
>>
>> One of these days, I'm going to fix the ones that don't follow this
>> pattern. :-)
> Any reason not to use pr_fmt?
Yes, that would be the best way to go. As Michael suggested,
it would be better to fix the whole file as a cleanup patch.
I shall fix this one to conform to the previous style and use
pr_fmt as a separate fixup patch.

Thanks,
Vineeth
Sean Christopherson April 8, 2021, 3:24 p.m. UTC | #7
On Thu, Apr 08, 2021, Vineeth Pillai wrote:
> Hi Vitaly,
> 
> On 4/8/21 7:06 AM, Vitaly Kuznetsov wrote:
> > -	if (ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) {
> > +	/*
> > +	 * AMD does not need enlightened VMCS as VMCB is already a
> > +	 * datastructure in memory.
> > Well, VMCS is also a structure in memory, isn't it? It's just that we
> > don't have a 'clean field' concept for it and we can't use normal memory
> > accesses.

Technically, you can use normal memory accesses, so long as software guarantees
the VMCS isn't resident in the VMCS cache and knows the field offsets for the
underlying CPU.  The lack of an architecturally defined layout is the biggest
issue, e.g. tacking on dirty bits through a PV ABI would be trivial.

> Yes, you are right. I was referring to the fact that we cant use normal
> memory accesses, but is a bit mis-worded.

If you slot in "architectural" it will read nicely, i.e. "VMCB is already an
architectural datastructure in memory".
Vineeth Pillai April 9, 2021, 12:23 p.m. UTC | #8
On 4/8/21 11:24 AM, Sean Christopherson wrote:
>
> Technically, you can use normal memory accesses, so long as software guarantees
> the VMCS isn't resident in the VMCS cache and knows the field offsets for the
> underlying CPU.  The lack of an architecturally defined layout is the biggest
> issue, e.g. tacking on dirty bits through a PV ABI would be trivial.
>
>> Yes, you are right. I was referring to the fact that we cant use normal
>> memory accesses, but is a bit mis-worded.
> If you slot in "architectural" it will read nicely, i.e. "VMCB is already an
> architectural datastructure in memory".
Yes, this makes sense. Thanks for the suggestion, will reword as you
suggested.

Thanks,
Vineeth
diff mbox series

Patch

diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c
index 3546d3e21787..4d364acfe95d 100644
--- a/arch/x86/kernel/cpu/mshyperv.c
+++ b/arch/x86/kernel/cpu/mshyperv.c
@@ -325,9 +325,17 @@  static void __init ms_hyperv_init_platform(void)
 			ms_hyperv.isolation_config_a, ms_hyperv.isolation_config_b);
 	}
 
-	if (ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) {
+	/*
+	 * AMD does not need enlightened VMCS as VMCB is already a
+	 * datastructure in memory. We need to get the nested
+	 * features if SVM is enabled.
+	 */
+	if (boot_cpu_has(X86_FEATURE_SVM) ||
+	    ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) {
 		ms_hyperv.nested_features =
 			cpuid_eax(HYPERV_CPUID_NESTED_FEATURES);
+		pr_info("Hyper-V nested_features: 0x%x\n",
+			ms_hyperv.nested_features);
 	}
 
 	/*