Skip to content

Commit e40bcf9

Browse files
minipli-osssean-jc
authored andcommitted
KVM: x86: Ignore CR0.WP toggles in non-paging mode
If paging is disabled, there are no permission bits to emulate. Micro-optimize this case to avoid unnecessary work. Suggested-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Mathias Krause <minipli@grsecurity.net> Link: https://lore.kernel.org/r/20230322013731.102955-4-minipli@grsecurity.net Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 01b3171 commit e40bcf9

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

arch/x86/kvm/x86.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -908,14 +908,20 @@ void kvm_post_set_cr0(struct kvm_vcpu *vcpu, unsigned long old_cr0, unsigned lon
908908
{
909909
/*
910910
* CR0.WP is incorporated into the MMU role, but only for non-nested,
911-
* indirect shadow MMUs. If TDP is enabled, the MMU's metadata needs
912-
* to be updated, e.g. so that emulating guest translations does the
913-
* right thing, but there's no need to unload the root as CR0.WP
914-
* doesn't affect SPTEs.
911+
* indirect shadow MMUs. If paging is disabled, no updates are needed
912+
* as there are no permission bits to emulate. If TDP is enabled, the
913+
* MMU's metadata needs to be updated, e.g. so that emulating guest
914+
* translations does the right thing, but there's no need to unload the
915+
* root as CR0.WP doesn't affect SPTEs.
915916
*/
916-
if (tdp_enabled && (cr0 ^ old_cr0) == X86_CR0_WP) {
917-
kvm_init_mmu(vcpu);
918-
return;
917+
if ((cr0 ^ old_cr0) == X86_CR0_WP) {
918+
if (!(cr0 & X86_CR0_PG))
919+
return;
920+
921+
if (tdp_enabled) {
922+
kvm_init_mmu(vcpu);
923+
return;
924+
}
919925
}
920926

921927
if ((cr0 ^ old_cr0) & X86_CR0_PG) {

0 commit comments

Comments
 (0)