Skip to content

Commit 4c20cd4

Browse files
committed
KVM: x86: Avoid double RDPKRU when loading host/guest PKRU
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 c806e88 ("x86/pkeys: Provide *pkru() helpers"), which renamed the raw helper from __write_pkru() => wrpkru(), and turned __write_pkru() into a wrapper. Commit 577ff46 ("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: 577ff46 ("x86/fpu: Only write PKRU if it is different from current") Signed-off-by: Adrian Hunter <adrian.hunter@intel.com> Link: https://lore.kernel.org/r/20241221011647.3747448-1-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent d647062 commit 4c20cd4

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

arch/x86/kvm/x86.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
11861186
vcpu->arch.pkru != vcpu->arch.host_pkru &&
11871187
((vcpu->arch.xcr0 & XFEATURE_MASK_PKRU) ||
11881188
kvm_is_cr4_bit_set(vcpu, X86_CR4_PKE)))
1189-
write_pkru(vcpu->arch.pkru);
1189+
wrpkru(vcpu->arch.pkru);
11901190
}
11911191
EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
11921192

@@ -1200,7 +1200,7 @@ void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
12001200
kvm_is_cr4_bit_set(vcpu, X86_CR4_PKE))) {
12011201
vcpu->arch.pkru = rdpkru();
12021202
if (vcpu->arch.pkru != vcpu->arch.host_pkru)
1203-
write_pkru(vcpu->arch.host_pkru);
1203+
wrpkru(vcpu->arch.host_pkru);
12041204
}
12051205

12061206
if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {

0 commit comments

Comments
 (0)