Skip to content

Commit 72a6c08

Browse files
KAGA-KOKOsuryasaimadhu
authored andcommitted
x86/pkru: Remove xstate fiddling from write_pkru()
The PKRU value of a task is stored in task->thread.pkru when the task is scheduled out. PKRU is restored on schedule in from there. So keeping the XSAVE buffer up to date is a pointless exercise. Remove the xstate fiddling and cleanup all related functions. Signed-off-by: Thomas Gleixner <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Reviewed-by: Borislav Petkov <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 0e8c54f commit 72a6c08

File tree

3 files changed

+7
-28
lines changed

3 files changed

+7
-28
lines changed

arch/x86/include/asm/pkru.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,14 @@ static inline u32 read_pkru(void)
4141

4242
static inline void write_pkru(u32 pkru)
4343
{
44-
struct pkru_state *pk;
45-
4644
if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
4745
return;
48-
49-
pk = get_xsave_addr(&current->thread.fpu.state.xsave, XFEATURE_PKRU);
50-
5146
/*
52-
* The PKRU value in xstate needs to be in sync with the value that is
53-
* written to the CPU. The FPU restore on return to userland would
54-
* otherwise load the previous value again.
47+
* WRPKRU is relatively expensive compared to RDPKRU.
48+
* Avoid WRPKRU when it would not change the value.
5549
*/
56-
fpregs_lock();
57-
if (pk)
58-
pk->pkru = pkru;
59-
__write_pkru(pkru);
60-
fpregs_unlock();
50+
if (pkru != rdpkru())
51+
wrpkru(pkru);
6152
}
6253

6354
static inline void pkru_write_default(void)

arch/x86/include/asm/special_insns.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,13 @@ static inline void wrpkru(u32 pkru)
104104
: : "a" (pkru), "c"(ecx), "d"(edx));
105105
}
106106

107-
static inline void __write_pkru(u32 pkru)
108-
{
109-
/*
110-
* WRPKRU is relatively expensive compared to RDPKRU.
111-
* Avoid WRPKRU when it would not change the value.
112-
*/
113-
if (pkru == rdpkru())
114-
return;
115-
116-
wrpkru(pkru);
117-
}
118-
119107
#else
120108
static inline u32 rdpkru(void)
121109
{
122110
return 0;
123111
}
124112

125-
static inline void __write_pkru(u32 pkru)
113+
static inline void wrpkru(u32 pkru)
126114
{
127115
}
128116
#endif

arch/x86/kvm/x86.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
943943
(kvm_read_cr4_bits(vcpu, X86_CR4_PKE) ||
944944
(vcpu->arch.xcr0 & XFEATURE_MASK_PKRU)) &&
945945
vcpu->arch.pkru != vcpu->arch.host_pkru)
946-
__write_pkru(vcpu->arch.pkru);
946+
write_pkru(vcpu->arch.pkru);
947947
}
948948
EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
949949

@@ -957,7 +957,7 @@ void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
957957
(vcpu->arch.xcr0 & XFEATURE_MASK_PKRU))) {
958958
vcpu->arch.pkru = rdpkru();
959959
if (vcpu->arch.pkru != vcpu->arch.host_pkru)
960-
__write_pkru(vcpu->arch.host_pkru);
960+
write_pkru(vcpu->arch.host_pkru);
961961
}
962962

963963
if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {

0 commit comments

Comments
 (0)