* [PATCH] KVM: x86: Avoid double RDPKRU when loading host/guest PKRU
@ 2024-12-21 1:16 Sean Christopherson
2025-01-09 19:47 ` Sean Christopherson
0 siblings, 1 reply; 2+ messages in thread
From: Sean Christopherson @ 2024-12-21 1:16 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini; +Cc: kvm, linux-kernel, Adrian Hunter
Use the raw wrpkru() helper when loading the guest/host's PKRU on switch
to/from guest context, as the write_pkru() wrapper incurs an unnecessary
rdpkru(). In both paths, KVM is guaranteed to have performed RDPKRU since
the last possible write, i.e. KVM has a fresh cache of the current value
in hardware.
This effectively restores KVM's behavior to that of KVM prior to commit
c806e88734b9 ("x86/pkeys: Provide *pkru() helpers"), which renamed the raw
helper from __write_pkru() => wrpkru(), and turned __write_pkru() into a
wrapper. Commit 577ff465f5a6 ("x86/fpu: Only write PKRU if it is different
from current") then added the extra RDPKRU to avoid an unnecessary WRPKRU,
but completely missed that KVM already optimized away pointless writes.
Reported-by: Adrian Hunter <adrian.hunter@intel.com>
Fixes: 577ff465f5a6 ("x86/fpu: Only write PKRU if it is different from current")
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/x86.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 4320647bd78a..9d5cece9260b 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1186,7 +1186,7 @@ void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
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);
+ wrpkru(vcpu->arch.pkru);
}
EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
@@ -1200,7 +1200,7 @@ void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
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);
+ wrpkru(vcpu->arch.host_pkru);
}
if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
base-commit: 13e98294d7cec978e31138d16824f50556a62d17
--
2.47.1.613.gc27f4b7a9f-goog
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] KVM: x86: Avoid double RDPKRU when loading host/guest PKRU
2024-12-21 1:16 [PATCH] KVM: x86: Avoid double RDPKRU when loading host/guest PKRU Sean Christopherson
@ 2025-01-09 19:47 ` Sean Christopherson
0 siblings, 0 replies; 2+ messages in thread
From: Sean Christopherson @ 2025-01-09 19:47 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini; +Cc: kvm, linux-kernel, Adrian Hunter
On Fri, 20 Dec 2024 17:16:47 -0800, Sean Christopherson wrote:
> Use the raw wrpkru() helper when loading the guest/host's PKRU on switch
> to/from guest context, as the write_pkru() wrapper incurs an unnecessary
> rdpkru(). In both paths, KVM is guaranteed to have performed RDPKRU since
> the last possible write, i.e. KVM has a fresh cache of the current value
> in hardware.
>
> This effectively restores KVM's behavior to that of KVM prior to commit
> c806e88734b9 ("x86/pkeys: Provide *pkru() helpers"), which renamed the raw
> helper from __write_pkru() => wrpkru(), and turned __write_pkru() into a
> wrapper. Commit 577ff465f5a6 ("x86/fpu: Only write PKRU if it is different
> from current") then added the extra RDPKRU to avoid an unnecessary WRPKRU,
> but completely missed that KVM already optimized away pointless writes.
>
> [...]
Applied to kvm-x86 misc, thanks!
[1/1] KVM: x86: Avoid double RDPKRU when loading host/guest PKRU
https://github.com/kvm-x86/linux/commit/4c20cd4cee92
--
https://github.com/kvm-x86/linux/tree/next
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-01-09 19:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-12-21 1:16 [PATCH] KVM: x86: Avoid double RDPKRU when loading host/guest PKRU Sean Christopherson
2025-01-09 19:47 ` Sean Christopherson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®