KVM: x86: Initialize tdp_level during vCPU creation
PATCH 1/1 KVM: x86: Initialize tdp_level during vCPU creation
Initialize vcpu->arch.tdp_level during vCPU creation to avoid consuming garbage if userspace calls KVM_RUN without first calling KVM_SET_CPUID.
--- arch/x86/kvm/x86.c | 1 + 1 file changed, 1 insertion(+)
arch/x86/kvm/x86.c +1 -0
@@ -9414,6 +9414,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)fx_init (vcpu); vcpu->arch.maxphyaddr =cpuid_query_maxphyaddr (vcpu); + vcpu->arch.tdp_level = kvm_x86_ops.get_tdp_level (vcpu); vcpu->arch.pat =MSR_IA32_CR_PAT_DEFAULT ;
Signature · 1 line
2.26.0
Quoted 15 lines · Sean Christopherson
Initialize
vcpu->arch.tdp_levelduring vCPU creation to avoid consuming garbage if userspace callsKVM_RUNwithout first callingKVM_SET_CPUID.Fixes e93fd3b3e89e9 ("KVM: x86/mmu: Capture TDP level when updating CPUID")Reported-by [email protected]Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 1 + 1 file changed, 1 insertion(+)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) · line 9419
Quoted 6 lines
arch/x86/kvm/x86.c +1
-0@@ -9414,6 +9414,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)fx_init (vcpu); vcpu->arch.maxphyaddr =cpuid_query_maxphyaddr (vcpu); + vcpu->arch.tdp_level = kvm_x86_ops.get_tdp_level (vcpu); vcpu->arch.pat =MSR_IA32_CR_PAT_DEFAULT ;
Looking at kvm_update_cpuid() I was thinking if it would make sense to duplicate the "/* Note, maxphyaddr must be updated before tdp_level. */" comment here (it seems to be a vmx-only thing btw), drop it from kvm_update_cpuid() or move cpuid_query_maxphyaddr() to get_tdp_level() but didn't come to a conclusive answer.
Signature · 1 line
Vitaly
Paolo Bonzini
maintainerQuoted 4 lines · Vitaly Kuznetsov
Quoted 4 lines
arch/x86/kvm/x86.c +1
-0vcpu->arch.maxphyaddr =cpuid_query_maxphyaddr (vcpu); + vcpu->arch.tdp_level = kvm_x86_ops.get_tdp_level (vcpu);
Quoted 8 lines
Quoted 1 line
vcpu->arch.pat=MSR_IA32_CR_PAT_DEFAULT;Reviewed-by Vitaly Kuznetsov <[email protected]>Looking at
kvm_update_cpuid()I was thinking if it would make sense to duplicate the "/* Note, maxphyaddr must be updated beforetdp_level. */" comment here (it seems to be a vmx-only thing btw), drop it fromkvm_update_cpuid()or movecpuid_query_maxphyaddr()toget_tdp_level()but didn't come to a conclusive answer.
Yeah, it makes sense to at least add the comment here too.
Paolo
Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · Vitaly Kuznetsov
arch/x86/kvm/x86.c int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) · line 9419
Quoted 5 lines
arch/x86/kvm/x86.c +1
-0vcpu->arch.maxphyaddr =cpuid_query_maxphyaddr (vcpu); + vcpu->arch.tdp_level = kvm_x86_ops.get_tdp_level (vcpu); vcpu->arch.pat =MSR_IA32_CR_PAT_DEFAULT ;Reviewed-by Vitaly Kuznetsov <[email protected]>
Quoted 7 lines
Quoted 5 lines
Looking at
kvm_update_cpuid()I was thinking if it would make sense to duplicate the "/* Note, maxphyaddr must be updated beforetdp_level. */" comment here (it seems to be a vmx-only thing btw), drop it fromkvm_update_cpuid()or movecpuid_query_maxphyaddr()toget_tdp_level()but didn't come to a conclusive answer.Yeah, it makes sense to at least add the comment here too.
Hmm, one option would be to make .get_tdp_level() pure function by passing in vcpu->arch.maxphyaddr. That should make the comment redundant. I don't love bleeding VMX's implementation into the prototype, but that ship has kinda already sailed.
Side topic, cpuid_query_maxphyaddr() should be unexported, the RTIT usage can and should use vcpu->arch.maxphyaddr. I'll send a patch for that.
Paolo Bonzini
maintainerQuoted 4 lines · Sean Christopherson
Hmm, one option would be to make .get_tdp_level() pure function by passing in
vcpu->arch.maxphyaddr. That should make the comment redundant. I don't love bleeding VMX's implementation into the prototype, but that ship has kinda already sailed.
Well, it's not bleeding the implementation that much, guest MAXPHYADDR is pretty much the only reason why it's a function and not a constant.
Another possibility BTW is to make the callback get_max_tdp_level and make get_tdp_level a function in mmu.c.
Paolo
Quoted 11 lines · Paolo Bonzini
Quoted 4 lines · Sean Christopherson
Hmm, one option would be to make .get_tdp_level() pure function by passing in
vcpu->arch.maxphyaddr. That should make the comment redundant. I don't love bleeding VMX's implementation into the prototype, but that ship has kinda already sailed.Well, it's not bleeding the implementation that much, guest MAXPHYADDR is pretty much the only reason why it's a function and not a constant.
Another possibility BTW is to make the callback
get_max_tdp_leveland makeget_tdp_levela function inmmu.c.
I like that idea. We could even avoid get_max_tdp_level() by passing that info into kvm_configure_mmu(), though that may not actually be cleaner. I'll play around with it.
Paolo Bonzini
maintainerQuoted 15 lines · Sean Christopherson
Initialize
vcpu->arch.tdp_levelduring vCPU creation to avoid consuming garbage if userspace callsKVM_RUNwithout first callingKVM_SET_CPUID.Fixes e93fd3b3e89e9 ("KVM: x86/mmu: Capture TDP level when updating CPUID")Reported-by [email protected]Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 1 + 1 file changed, 1 insertion(+)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) · line 9419
Quoted 6 lines
arch/x86/kvm/x86.c +1
-0@@ -9414,6 +9414,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)fx_init (vcpu); vcpu->arch.maxphyaddr =cpuid_query_maxphyaddr (vcpu); + vcpu->arch.tdp_level = kvm_x86_ops.get_tdp_level (vcpu); vcpu->arch.pat =MSR_IA32_CR_PAT_DEFAULT ;
Queued, it is probably a good idea to add a selftests testcase for this (it's even okay if it doesn't use the whole selftests infrastructure and invokes KVM_CREATE_VM/KVM_CREATE_VCPU/KVM_RUN manually).
Paolo
Quoted 18 lines · Paolo Bonzini
Quoted 17 lines · Sean Christopherson
Initialize
vcpu->arch.tdp_levelduring vCPU creation to avoid consuming garbage if userspace callsKVM_RUNwithout first callingKVM_SET_CPUID.Fixes e93fd3b3e89e9 ("KVM: x86/mmu: Capture TDP level when updating CPUID")Reported-by [email protected]Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 1 + 1 file changed, 1 insertion(+)arch/x86/kvm/x86.c +0
-0@@ -9414,6 +9414,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)fx_init (vcpu);
Quoted 8 lines
arch/x86/kvm/x86.c int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) · line 9419
Quoted 4 lines
arch/x86/kvm/x86.c +1
-0vcpu->arch.maxphyaddr =cpuid_query_maxphyaddr (vcpu); + vcpu->arch.tdp_level = kvm_x86_ops.get_tdp_level (vcpu); vcpu->arch.pat =MSR_IA32_CR_PAT_DEFAULT ;Queued, it is probably a good idea to add a selftests testcase for this (it's even okay if it doesn't use the whole selftests infrastructure and invokes KVM_CREATE_VM/KVM_CREATE_VCPU/KVM_RUN manually).
Ya. syzbot is hitting a #GP due to NULL pointer during debugfs on the exact same sequence. I haven't been able to reproduce that one (have yet to try syzbot's exact config), but it's another example of a "dumb" test hitting meaningful bugs.
Paolo Bonzini
maintainerQuoted 28 lines · Sean Christopherson
Quoted 18 lines · Paolo Bonzini
Quoted 17 lines · Sean Christopherson
Initialize
vcpu->arch.tdp_levelduring vCPU creation to avoid consuming garbage if userspace callsKVM_RUNwithout first callingKVM_SET_CPUID.Fixes e93fd3b3e89e9 ("KVM: x86/mmu: Capture TDP level when updating CPUID")Reported-by [email protected]Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 1 + 1 file changed, 1 insertion(+)arch/x86/kvm/x86.c +0
-0@@ -9414,6 +9414,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)fx_init (vcpu);Quoted 8 lines
arch/x86/kvm/x86.c int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) · line 9419
Quoted 4 lines
arch/x86/kvm/x86.c +1
-0vcpu->arch.maxphyaddr =cpuid_query_maxphyaddr (vcpu); + vcpu->arch.tdp_level = kvm_x86_ops.get_tdp_level (vcpu); vcpu->arch.pat =MSR_IA32_CR_PAT_DEFAULT ;Queued, it is probably a good idea to add a selftests testcase for this (it's even okay if it doesn't use the whole selftests infrastructure and invokes KVM_CREATE_VM/KVM_CREATE_VCPU/KVM_RUN manually).
Quoted 4 lines
Ya. syzbot is hitting a #GP due to NULL pointer during debugfs on the exact same sequence. I haven't been able to reproduce that one (have yet to try syzbot's exact config), but it's another example of a "dumb" test hitting meaningful bugs.
Saw that, it's mine. :)
Paolo
Quoted 7 lines · Paolo Bonzini
Quoted 4 lines · Sean Christopherson
Ya. syzbot is hitting a #GP due to NULL pointer during debugfs on the exact same sequence. I haven't been able to reproduce that one (have yet to try syzbot's exact config), but it's another example of a "dumb" test hitting meaningful bugs.
Saw that, it's mine. :)
All yours. I as hoping it would be easily reproducible and fixable while I was looking at the MMU BUG(), but that didn't happen.
Quoted 28 lines · Sean Christopherson
Quoted 18 lines · Paolo Bonzini
Quoted 17 lines · Sean Christopherson
Initialize
vcpu->arch.tdp_levelduring vCPU creation to avoid consuming garbage if userspace callsKVM_RUNwithout first callingKVM_SET_CPUID.Fixes e93fd3b3e89e9 ("KVM: x86/mmu: Capture TDP level when updating CPUID")Reported-by [email protected]Signed-off-by Sean Christopherson <[email protected]>--- arch/x86/kvm/x86.c | 1 + 1 file changed, 1 insertion(+)arch/x86/kvm/x86.c +0
-0@@ -9414,6 +9414,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)fx_init (vcpu);Quoted 8 lines
arch/x86/kvm/x86.c int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) · line 9419
Quoted 4 lines
arch/x86/kvm/x86.c +1
-0vcpu->arch.maxphyaddr =cpuid_query_maxphyaddr (vcpu); + vcpu->arch.tdp_level = kvm_x86_ops.get_tdp_level (vcpu); vcpu->arch.pat =MSR_IA32_CR_PAT_DEFAULT ;Queued, it is probably a good idea to add a selftests testcase for this (it's even okay if it doesn't use the whole selftests infrastructure and invokes KVM_CREATE_VM/KVM_CREATE_VCPU/KVM_RUN manually).
Quoted 4 lines
Ya. syzbot is hitting a #GP due to NULL pointer during debugfs on the exact same sequence. I haven't been able to reproduce that one (have yet to try syzbot's exact config), but it's another example of a "dumb" test hitting meaningful bugs.
So we already have a selftest for this scenario, test_zero_memory_regions() in set_memory_region_test.c. Embarrassingly, I wrote the darn thing, I'm just really neglectful when it comes to running selftests.
I'll looking into writing a script to run all selftests with a single command, unless someone already has one laying around?
Quoted 2 lines · Sean Christopherson
I'll looking into writing a script to run all selftests with a single command, unless someone already has one laying around?
Is 'make run_tests' in tools/testing/selftests/kvm/ what you're looking for?
Signature · 1 line
Vitaly
Quoted 7 lines · Vitaly Kuznetsov
Quoted 2 lines · Sean Christopherson
I'll looking into writing a script to run all selftests with a single command, unless someone already has one laying around?
Is 'make
run_tests' intools/testing/selftests/kvm/what you're looking for?
*sigh* Yes. Thanks!