All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM: x86: Use cpu_feature_enabled() for PKU instead of #ifdef
@ 2023-06-02  1:05 Sean Christopherson
  2023-06-02 15:51 ` Jon Kohler
  2023-06-07  0:53 ` Sean Christopherson
  0 siblings, 2 replies; 6+ messages in thread
From: Sean Christopherson @ 2023-06-02  1:05 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini; +Cc: kvm, linux-kernel, Jon Kohler

Replace an #ifdef on CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS with a
cpu_feature_enabled() check on X86_FEATURE_PKU.  The macro magic of
DISABLED_MASK_BIT_SET() means that cpu_feature_enabled() provides the
same end result (no code generated) when PKU is disabled by Kconfig.

No functional change intended.

Cc: Jon Kohler <jon@nutanix.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/x86.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index ceb7c5e9cf9e..eed1f0629023 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1017,13 +1017,11 @@ void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
 			wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
 	}
 
-#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
-	if (static_cpu_has(X86_FEATURE_PKU) &&
+	if (cpu_feature_enabled(X86_FEATURE_PKU) &&
 	    vcpu->arch.pkru != vcpu->arch.host_pkru &&
 	    ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) ||
 	     kvm_is_cr4_bit_set(vcpu, X86_CR4_PKE)))
 		write_pkru(vcpu->arch.pkru);
-#endif /* CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS */
 }
 EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
 
@@ -1032,15 +1030,13 @@ void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
 	if (vcpu->arch.guest_state_protected)
 		return;
 
-#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
-	if (static_cpu_has(X86_FEATURE_PKU) &&
+	if (cpu_feature_enabled(X86_FEATURE_PKU) &&
 	    ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) ||
 	     kvm_is_cr4_bit_set(vcpu, X86_CR4_PKE))) {
 		vcpu->arch.pkru = rdpkru();
 		if (vcpu->arch.pkru != vcpu->arch.host_pkru)
 			write_pkru(vcpu->arch.host_pkru);
 	}
-#endif /* CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS */
 
 	if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
 

base-commit: a053a0e4a9f8c52f3acf8a9d2520c4bf39077a7e
-- 
2.41.0.rc2.161.g9c6817b8e7-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] KVM: x86: Use cpu_feature_enabled() for PKU instead of #ifdef
  2023-06-02  1:05 [PATCH] KVM: x86: Use cpu_feature_enabled() for PKU instead of #ifdef Sean Christopherson
@ 2023-06-02 15:51 ` Jon Kohler
  2023-06-02 18:30   ` Jim Mattson
  2023-06-07  0:53 ` Sean Christopherson
  1 sibling, 1 reply; 6+ messages in thread
From: Jon Kohler @ 2023-06-02 15:51 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: Paolo Bonzini, kvm, linux-kernel



> On Jun 1, 2023, at 9:05 PM, Sean Christopherson <seanjc@google.com> wrote:
> 
> Replace an #ifdef on CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS with a
> cpu_feature_enabled() check on X86_FEATURE_PKU.  The macro magic of
> DISABLED_MASK_BIT_SET() means that cpu_feature_enabled() provides the
> same end result (no code generated) when PKU is disabled by Kconfig.
> 
> No functional change intended.
> 
> Cc: Jon Kohler <jon@nutanix.com>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
> arch/x86/kvm/x86.c | 8 ++------
> 1 file changed, 2 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index ceb7c5e9cf9e..eed1f0629023 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -1017,13 +1017,11 @@ void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
> 			wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
> 	}
> 
> -#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
> -	if (static_cpu_has(X86_FEATURE_PKU) &&
> +	if (cpu_feature_enabled(X86_FEATURE_PKU) &&
> 	    vcpu->arch.pkru != vcpu->arch.host_pkru &&
> 	    ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) ||
> 	     kvm_is_cr4_bit_set(vcpu, X86_CR4_PKE)))
> 		write_pkru(vcpu->arch.pkru);
> -#endif /* CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS */
> }
> EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
> 
> @@ -1032,15 +1030,13 @@ void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
> 	if (vcpu->arch.guest_state_protected)
> 		return;
> 
> -#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
> -	if (static_cpu_has(X86_FEATURE_PKU) &&
> +	if (cpu_feature_enabled(X86_FEATURE_PKU) &&
> 	    ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) ||
> 	     kvm_is_cr4_bit_set(vcpu, X86_CR4_PKE))) {
> 		vcpu->arch.pkru = rdpkru();
> 		if (vcpu->arch.pkru != vcpu->arch.host_pkru)
> 			write_pkru(vcpu->arch.host_pkru);
> 	}
> -#endif /* CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS */
> 
> 	if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
> 
> 
> base-commit: a053a0e4a9f8c52f3acf8a9d2520c4bf39077a7e
> -- 
> 2.41.0.rc2.161.g9c6817b8e7-goog
> 

Thanks for the cleanup!

Reviewed-by: Jon Kohler <jon@nutanix.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] KVM: x86: Use cpu_feature_enabled() for PKU instead of #ifdef
  2023-06-02 15:51 ` Jon Kohler
@ 2023-06-02 18:30   ` Jim Mattson
  2023-06-02 20:58     ` Mingwei Zhang
  2023-06-02 21:49     ` Sean Christopherson
  0 siblings, 2 replies; 6+ messages in thread
From: Jim Mattson @ 2023-06-02 18:30 UTC (permalink / raw)
  To: Jon Kohler, Mingwei Zhang
  Cc: Sean Christopherson, Paolo Bonzini, kvm, linux-kernel

On Fri, Jun 2, 2023 at 8:51 AM Jon Kohler <jon@nutanix.com> wrote:
>
>
>
> > On Jun 1, 2023, at 9:05 PM, Sean Christopherson <seanjc@google.com> wrote:
> >
> > Replace an #ifdef on CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS with a
> > cpu_feature_enabled() check on X86_FEATURE_PKU.  The macro magic of
> > DISABLED_MASK_BIT_SET() means that cpu_feature_enabled() provides the
> > same end result (no code generated) when PKU is disabled by Kconfig.
> >
> > No functional change intended.
> >
> > Cc: Jon Kohler <jon@nutanix.com>
> > Signed-off-by: Sean Christopherson <seanjc@google.com>
> > ---
> > arch/x86/kvm/x86.c | 8 ++------
> > 1 file changed, 2 insertions(+), 6 deletions(-)
> >
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index ceb7c5e9cf9e..eed1f0629023 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -1017,13 +1017,11 @@ void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
> >                       wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
> >       }
> >
> > -#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
> > -     if (static_cpu_has(X86_FEATURE_PKU) &&
> > +     if (cpu_feature_enabled(X86_FEATURE_PKU) &&
> >           vcpu->arch.pkru != vcpu->arch.host_pkru &&
> >           ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) ||
> >            kvm_is_cr4_bit_set(vcpu, X86_CR4_PKE)))
> >               write_pkru(vcpu->arch.pkru);
> > -#endif /* CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS */
> > }
> > EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
> >
> > @@ -1032,15 +1030,13 @@ void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
> >       if (vcpu->arch.guest_state_protected)
> >               return;
> >
> > -#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
> > -     if (static_cpu_has(X86_FEATURE_PKU) &&
> > +     if (cpu_feature_enabled(X86_FEATURE_PKU) &&
> >           ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) ||
> >            kvm_is_cr4_bit_set(vcpu, X86_CR4_PKE))) {
> >               vcpu->arch.pkru = rdpkru();
> >               if (vcpu->arch.pkru != vcpu->arch.host_pkru)
> >                       write_pkru(vcpu->arch.host_pkru);
> >       }
> > -#endif /* CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS */
> >
> >       if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
> >
> >
> > base-commit: a053a0e4a9f8c52f3acf8a9d2520c4bf39077a7e
> > --
> > 2.41.0.rc2.161.g9c6817b8e7-goog
> >
>
> Thanks for the cleanup!
>
> Reviewed-by: Jon Kohler <jon@nutanix.com>

+Mingwei Zhang

As we move towards enabling PKRU on the host, due to some customer
requests, I have to wonder if PKRU-disabled is the norm.

In other words, is this a likely() or unlikely() optimization?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] KVM: x86: Use cpu_feature_enabled() for PKU instead of #ifdef
  2023-06-02 18:30   ` Jim Mattson
@ 2023-06-02 20:58     ` Mingwei Zhang
  2023-06-02 21:49     ` Sean Christopherson
  1 sibling, 0 replies; 6+ messages in thread
From: Mingwei Zhang @ 2023-06-02 20:58 UTC (permalink / raw)
  To: Jim Mattson
  Cc: Jon Kohler, Sean Christopherson, Paolo Bonzini, kvm, linux-kernel

>
> As we move towards enabling PKRU on the host, due to some customer
> requests, I have to wonder if PKRU-disabled is the norm.
>
> In other words, is this a likely() or unlikely() optimization?

I think it should be likely() as PKU was introduced very early in the
Skylake-SP server cores many years ago. Today I think all recent
client CPUs should have PKU on default if I am not mistaken. So yeah,
adding a likely() probably should help prevent the compiler from
evicting this code chunk to the end of function.

Thanks.
-Mingwei

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] KVM: x86: Use cpu_feature_enabled() for PKU instead of #ifdef
  2023-06-02 18:30   ` Jim Mattson
  2023-06-02 20:58     ` Mingwei Zhang
@ 2023-06-02 21:49     ` Sean Christopherson
  1 sibling, 0 replies; 6+ messages in thread
From: Sean Christopherson @ 2023-06-02 21:49 UTC (permalink / raw)
  To: Jim Mattson; +Cc: Jon Kohler, Mingwei Zhang, Paolo Bonzini, kvm, linux-kernel

On Fri, Jun 02, 2023, Jim Mattson wrote:
> On Fri, Jun 2, 2023 at 8:51 AM Jon Kohler <jon@nutanix.com> wrote:
> > > On Jun 1, 2023, at 9:05 PM, Sean Christopherson <seanjc@google.com> wrote:
> > > @@ -1032,15 +1030,13 @@ void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
> > >       if (vcpu->arch.guest_state_protected)
> > >               return;
> > >
> > > -#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
> > > -     if (static_cpu_has(X86_FEATURE_PKU) &&
> > > +     if (cpu_feature_enabled(X86_FEATURE_PKU) &&
> > >           ((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) ||
> > >            kvm_is_cr4_bit_set(vcpu, X86_CR4_PKE))) {
> > >               vcpu->arch.pkru = rdpkru();
> > >               if (vcpu->arch.pkru != vcpu->arch.host_pkru)
> > >                       write_pkru(vcpu->arch.host_pkru);
> > >       }
> > > -#endif /* CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS */
> > >
> > >       if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
> > >
> > >
> > > base-commit: a053a0e4a9f8c52f3acf8a9d2520c4bf39077a7e
> > > --
> > > 2.41.0.rc2.161.g9c6817b8e7-goog
> > >
> >
> > Thanks for the cleanup!
> >
> > Reviewed-by: Jon Kohler <jon@nutanix.com>
> 
> +Mingwei Zhang
> 
> As we move towards enabling PKRU on the host, due to some customer
> requests, I have to wonder if PKRU-disabled is the norm.
> 
> In other words, is this a likely() or unlikely() optimization?

Neither?  I don't see any reason to speculate on guest state.  I'll bet dollars
to donuts that adding (un)likely() is negligible in terms of performance.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] KVM: x86: Use cpu_feature_enabled() for PKU instead of #ifdef
  2023-06-02  1:05 [PATCH] KVM: x86: Use cpu_feature_enabled() for PKU instead of #ifdef Sean Christopherson
  2023-06-02 15:51 ` Jon Kohler
@ 2023-06-07  0:53 ` Sean Christopherson
  1 sibling, 0 replies; 6+ messages in thread
From: Sean Christopherson @ 2023-06-07  0:53 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini; +Cc: kvm, linux-kernel, Jon Kohler

On Thu, 01 Jun 2023 18:05:50 -0700, Sean Christopherson wrote:
> Replace an #ifdef on CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS with a
> cpu_feature_enabled() check on X86_FEATURE_PKU.  The macro magic of
> DISABLED_MASK_BIT_SET() means that cpu_feature_enabled() provides the
> same end result (no code generated) when PKU is disabled by Kconfig.
> 
> No functional change intended.
> 
> [...]

Applied to kvm-x86 misc, thanks!

[1/1] KVM: x86: Use cpu_feature_enabled() for PKU instead of #ifdef
      https://github.com/kvm-x86/linux/commit/056b9919a16a

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-06-07  0:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-02  1:05 [PATCH] KVM: x86: Use cpu_feature_enabled() for PKU instead of #ifdef Sean Christopherson
2023-06-02 15:51 ` Jon Kohler
2023-06-02 18:30   ` Jim Mattson
2023-06-02 20:58     ` Mingwei Zhang
2023-06-02 21:49     ` Sean Christopherson
2023-06-07  0:53 ` Sean Christopherson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.