KVM: X86: Make fpu allocation a common function
PATCH 1/1 KVM: X86: Make fpu allocation a common function
They are duplicated codes to create vcpu.arch.{user,guest}_fpu in VMX and SVM. Make them common functions.
No functional change intended.
--- arch/x86/kvm/svm.c | 20 +++----------------- arch/x86/kvm/vmx/vmx.c | 20 +++----------------- arch/x86/kvm/x86.h | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 34 deletions(-)
arch/x86/kvm/svm.c +3 -17
@@ -2156,21 +2156,9 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) goto out; } - svm->vcpu.arch.user_fpu =kmem_cache_zalloc (x86_fpu_cache, -GFP_KERNEL_ACCOUNT ); -if (!svm->vcpu.arch.user_fpu) { -printk (KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n" ); - err = -ENOMEM ; + err =kvm_vcpu_create_fpu (&svm->vcpu); +if (err) goto free_partial_svm; - } - - svm->vcpu.arch.guest_fpu =kmem_cache_zalloc (x86_fpu_cache, -GFP_KERNEL_ACCOUNT ); -if (!svm->vcpu.arch.guest_fpu) { -printk (KERN_ERR "kvm: failed to allocate vcpu's fpu\n" ); - err = -ENOMEM ; - goto free_user_fpu; - } err =kvm_vcpu_init (&svm->vcpu, kvm, id);if (err)
@@ -2231,9 +2219,7 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) uninit:kvm_vcpu_uninit (&svm->vcpu); free_svm: -kmem_cache_free (x86_fpu_cache, svm->vcpu.arch.guest_fpu); -free_user_fpu: -kmem_cache_free (x86_fpu_cache, svm->vcpu.arch.user_fpu); +kvm_vcpu_free_fpu (&svm->vcpu); free_partial_svm:kmem_cache_free (kvm_vcpu_cache, svm); out:
arch/x86/kvm/vmx/vmx.c +3 -17
@@ -6710,21 +6710,9 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)if (!vmx)return ERR_PTR (-ENOMEM ); - vmx->vcpu.arch.user_fpu =kmem_cache_zalloc (x86_fpu_cache, -GFP_KERNEL_ACCOUNT ); -if (!vmx->vcpu.arch.user_fpu) { -printk (KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n" ); - err = -ENOMEM ; + err =kvm_vcpu_create_fpu (&vmx->vcpu); +if (err) goto free_partial_vcpu; - } - - vmx->vcpu.arch.guest_fpu =kmem_cache_zalloc (x86_fpu_cache, -GFP_KERNEL_ACCOUNT ); -if (!vmx->vcpu.arch.guest_fpu) { -printk (KERN_ERR "kvm: failed to allocate vcpu's fpu\n" ); - err = -ENOMEM ; - goto free_user_fpu; - } vmx->vpid =allocate_vpid ();
@@ -6825,9 +6813,7 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)kvm_vcpu_uninit (&vmx->vcpu); free_vcpu:free_vpid (vmx->vpid); -kmem_cache_free (x86_fpu_cache, vmx->vcpu.arch.guest_fpu); -free_user_fpu: -kmem_cache_free (x86_fpu_cache, vmx->vcpu.arch.user_fpu); +kvm_vcpu_free_fpu (&vmx->vcpu); free_partial_vcpu:kmem_cache_free (kvm_vcpu_cache, vmx);return ERR_PTR (err);
arch/x86/kvm/x86.h +26 -0
@@ -367,4 +367,30 @@ static inline bool kvm_pat_valid(u64 data)void kvm_load_guest_xcr0 (struct kvm_vcpu *vcpu);void kvm_put_guest_xcr0 (struct kvm_vcpu *vcpu); +static inline int kvm_vcpu_create_fpu (struct kvm_vcpu *vcpu) +{ + vcpu->arch.user_fpu =kmem_cache_zalloc (x86_fpu_cache, +GFP_KERNEL_ACCOUNT ); +if (!vcpu->arch.user_fpu) { +printk (KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n" ); +return -ENOMEM ; + } + + vcpu->arch.guest_fpu =kmem_cache_zalloc (x86_fpu_cache, +GFP_KERNEL_ACCOUNT ); +if (!vcpu->arch.guest_fpu) { +printk (KERN_ERR "kvm: failed to allocate vcpu's fpu\n" ); +kmem_cache_free (x86_fpu_cache, vcpu->arch.user_fpu); +return -ENOMEM ; + } + +return 0 ; +} + +static inline void kvm_vcpu_free_fpu (struct kvm_vcpu *vcpu) +{ +kmem_cache_free (x86_fpu_cache, vcpu->arch.guest_fpu); +kmem_cache_free (x86_fpu_cache, vcpu->arch.user_fpu); +} +#endif
Signature · 1 line
2.19.1
Quoted 4 lines · Xiaoyao Li
They are duplicated codes to create vcpu.arch.{user,guest}_fpu in VMX and SVM. Make them common functions.
No functional change intended.
Would it rather make sense to move this code to kvm_arch_vcpu_create()/kvm_arch_vcpu_destroy() instead?
Quoted 121 lines
Signed-off-by Xiaoyao Li <[email protected]>--- arch/x86/kvm/svm.c | 20 +++----------------- arch/x86/kvm/vmx/vmx.c | 20 +++----------------- arch/x86/kvm/x86.h | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 34 deletions(-)arch/x86/kvm/svm.c +3
-17@@ -2156,21 +2156,9 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) goto out; } - svm->vcpu.arch.user_fpu =kmem_cache_zalloc (x86_fpu_cache, -GFP_KERNEL_ACCOUNT ); -if (!svm->vcpu.arch.user_fpu) { -printk (KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n" ); - err = -ENOMEM ; + err =kvm_vcpu_create_fpu (&svm->vcpu); +if (err) goto free_partial_svm; - } - - svm->vcpu.arch.guest_fpu =kmem_cache_zalloc (x86_fpu_cache, -GFP_KERNEL_ACCOUNT ); -if (!svm->vcpu.arch.guest_fpu) { -printk (KERN_ERR "kvm: failed to allocate vcpu's fpu\n" ); - err = -ENOMEM ; - goto free_user_fpu; - } err =kvm_vcpu_init (&svm->vcpu, kvm, id);if (err)@@ -2231,9 +2219,7 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) uninit:kvm_vcpu_uninit (&svm->vcpu); free_svm: -kmem_cache_free (x86_fpu_cache, svm->vcpu.arch.guest_fpu); -free_user_fpu: -kmem_cache_free (x86_fpu_cache, svm->vcpu.arch.user_fpu); +kvm_vcpu_free_fpu (&svm->vcpu); free_partial_svm:kmem_cache_free (kvm_vcpu_cache, svm); out:arch/x86/kvm/vmx/vmx.c +3
-17@@ -6710,21 +6710,9 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)if (!vmx)return ERR_PTR (-ENOMEM ); - vmx->vcpu.arch.user_fpu =kmem_cache_zalloc (x86_fpu_cache, -GFP_KERNEL_ACCOUNT ); -if (!vmx->vcpu.arch.user_fpu) { -printk (KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n" ); - err = -ENOMEM ; + err =kvm_vcpu_create_fpu (&vmx->vcpu); +if (err) goto free_partial_vcpu; - } - - vmx->vcpu.arch.guest_fpu =kmem_cache_zalloc (x86_fpu_cache, -GFP_KERNEL_ACCOUNT ); -if (!vmx->vcpu.arch.guest_fpu) { -printk (KERN_ERR "kvm: failed to allocate vcpu's fpu\n" ); - err = -ENOMEM ; - goto free_user_fpu; - } vmx->vpid =allocate_vpid ();@@ -6825,9 +6813,7 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)kvm_vcpu_uninit (&vmx->vcpu); free_vcpu:free_vpid (vmx->vpid); -kmem_cache_free (x86_fpu_cache, vmx->vcpu.arch.guest_fpu); -free_user_fpu: -kmem_cache_free (x86_fpu_cache, vmx->vcpu.arch.user_fpu); +kvm_vcpu_free_fpu (&vmx->vcpu); free_partial_vcpu:kmem_cache_free (kvm_vcpu_cache, vmx);return ERR_PTR (err);arch/x86/kvm/x86.h +26
-0@@ -367,4 +367,30 @@ static inline bool kvm_pat_valid(u64 data)void kvm_load_guest_xcr0 (struct kvm_vcpu *vcpu);void kvm_put_guest_xcr0 (struct kvm_vcpu *vcpu); +static inline int kvm_vcpu_create_fpu (struct kvm_vcpu *vcpu) +{ + vcpu->arch.user_fpu =kmem_cache_zalloc (x86_fpu_cache, +GFP_KERNEL_ACCOUNT ); +if (!vcpu->arch.user_fpu) { +printk (KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n" ); +return -ENOMEM ; + } + + vcpu->arch.guest_fpu =kmem_cache_zalloc (x86_fpu_cache, +GFP_KERNEL_ACCOUNT ); +if (!vcpu->arch.guest_fpu) { +printk (KERN_ERR "kvm: failed to allocate vcpu's fpu\n" ); +kmem_cache_free (x86_fpu_cache, vcpu->arch.user_fpu); +return -ENOMEM ; + } + +return 0 ; +} + +static inline void kvm_vcpu_free_fpu (struct kvm_vcpu *vcpu) +{ +kmem_cache_free (x86_fpu_cache, vcpu->arch.guest_fpu); +kmem_cache_free (x86_fpu_cache, vcpu->arch.user_fpu); +} +#endif
Signature · 1 line
Vitaly
Quoted 9 lines · Vitaly Kuznetsov
Quoted 4 lines · Xiaoyao Li
They are duplicated codes to create vcpu.arch.{user,guest}_fpu in VMX and SVM. Make them common functions.
No functional change intended.
Would it rather make sense to move this code to kvm_arch_vcpu_create()/kvm_arch_vcpu_destroy() instead?
Does it make sense? Yes. Would it actually work? No. Well, not without other shenanigans.
FPU allocation can't be placed after the call to .create_vcpu() becuase
it's consumed in kvm_arch_vcpu_init(). FPU allocation can't come before
.create_vcpu() because the vCPU struct itself hasn't been allocated. The latter could be solved by passed the FPU pointer into .create_vcpu(), but that's a bit ugly and is not a precedent we want to set.
At a glance, FPU allocation can be moved to kvm_arch_vcpu_init(), maybe right before the call to fx_init().
Quoted 11 lines · Sean Christopherson
Quoted 9 lines · Vitaly Kuznetsov
Quoted 4 lines · Xiaoyao Li
They are duplicated codes to create vcpu.arch.{user,guest}_fpu in VMX and SVM. Make them common functions.
No functional change intended.
Would it rather make sense to move this code to kvm_arch_vcpu_create()/kvm_arch_vcpu_destroy() instead?
Quoted 9 lines
Does it make sense? Yes. Would it actually work? No. Well, not without other shenanigans.
FPU allocation can't be placed after the call to .create_vcpu() becuase
it's consumed inkvm_arch_vcpu_init(). FPU allocation can't come before
.create_vcpu() because the vCPU struct itself hasn't been allocated. The latter could be solved by passed the FPU pointer into .create_vcpu(), but that's a bit ugly and is not a precedent we want to set.
That's exactly what I found.
Quoted 3 lines
At a glance, FPU allocation can be moved to
kvm_arch_vcpu_init(), maybe right before the call tofx_init().
Yeah, putting here is better.
I'm wondering the semantic of create, init, setup. There are vcpu_{create,init,setup}, and IIUC, vcpu_create is mainly for data structure allocation and vcpu_{init,setup} should be for data structure initialization/setup (and maybe they could/should merge into one)
But I feel the current codes for vcpu creation a bit messed, especially of vmx.
Quoted 11 lines · Sean Christopherson
Quoted 9 lines · Vitaly Kuznetsov
Quoted 4 lines · Xiaoyao Li
They are duplicated codes to create vcpu.arch.{user,guest}_fpu in VMX and SVM. Make them common functions.
No functional change intended.
Would it rather make sense to move this code to kvm_arch_vcpu_create()/kvm_arch_vcpu_destroy() instead?
Quoted 6 lines
Does it make sense? Yes. Would it actually work? No. Well, not without other shenanigans.
FPU allocation can't be placed after the call to .create_vcpu() becuase
it's consumed inkvm_arch_vcpu_init(). FPU allocation can't come before
.create_vcpu() because the vCPU struct itself hasn't been allocated.
A very theoretical question: why do we have 'struct vcpu' embedded in vcpu_vmx/vcpu_svm and not the other way around (e.g. in a union)? That would've allowed us to allocate memory in common code and then fill in vendor-specific details in .create_vcpu().
Signature · 1 line
Vitaly
Paolo Bonzini
maintainerQuoted 4 lines · Vitaly Kuznetsov
A very theoretical question: why do we have 'struct vcpu' embedded in vcpu_vmx/vcpu_svm and not the other way around (e.g. in a union)? That would've allowed us to allocate memory in common code and then fill in vendor-specific details in .create_vcpu().
Probably "because it's always been like that" is the most accurate answer.
Paolo
Quoted 8 lines · Paolo Bonzini
Quoted 4 lines · Vitaly Kuznetsov
A very theoretical question: why do we have 'struct vcpu' embedded in vcpu_vmx/vcpu_svm and not the other way around (e.g. in a union)? That would've allowed us to allocate memory in common code and then fill in vendor-specific details in .create_vcpu().
Probably "because it's always been like that" is the most accurate answer.
OK, so let me make my question a bit less theoretical: would you be in favor of changing the status quo? :-)
Signature · 1 line
Vitaly
Quoted 7 lines · Vitaly Kuznetsov
Quoted 5 lines · Paolo Bonzini
Quoted 4 lines · Vitaly Kuznetsov
A very theoretical question: why do we have 'struct vcpu' embedded in vcpu_vmx/vcpu_svm and not the other way around (e.g. in a union)? That would've allowed us to allocate memory in common code and then fill in vendor-specific details in .create_vcpu().
A union would waste a non-trivial amount of memory on SVM.
SVM: struct size = 14560
VMX: struct size = 16192
There are ways around that, but...
Quoted 6 lines
Quoted 3 lines
Probably "because it's always been like that" is the most accurate answer.
OK, so let me make my question a bit less theoretical: would you be in favor of changing the status quo? :-)
... we don't need to invert the strut embedding to re-order the create flow. 'struct kvm_vcpu' must be at offset zero and the size of the vcpu is vendor defined, so kvm_arch_vcpu_create() can allocate the struct and directly cast it to a 'struct kvm_vcpu *'.
Paolo Bonzini
maintainerQuoted 9 lines · Vitaly Kuznetsov
Quoted 7 lines · Vitaly Kuznetsov
Quoted 4 lines
A very theoretical question: why do we have 'struct vcpu' embedded in vcpu_vmx/vcpu_svm and not the other way around (e.g. in a union)? That would've allowed us to allocate memory in common code and then fill in vendor-specific details in .create_vcpu().
Probably "because it's always been like that" is the most accurate answer.
OK, so let me make my question a bit less theoretical: would you be in favor of changing the status quo? :-)
Not really, it would be a lot of churn for debatable benefit.
Paolo
Paolo Bonzini
maintainerQuoted 9 lines · Vitaly Kuznetsov
Quoted 4 lines · Xiaoyao Li
They are duplicated codes to create vcpu.arch.{user,guest}_fpu in VMX and SVM. Make them common functions.
No functional change intended.
Would it rather make sense to move this code to kvm_arch_vcpu_create()/kvm_arch_vcpu_destroy() instead?
user_fpu could be made percpu too... That would save a bit of memory for each vCPU. I'm holding on Xiaoyao's patch because a lot of the code he's touching would go away then.
Paolo
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Vitaly Kuznetsov
Quoted 3 lines · Xiaoyao Li
They are duplicated codes to create vcpu.arch.{user,guest}_fpu in VMX and SVM. Make them common functions.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
No functional change intended.
Would it rather make sense to move this code to kvm_arch_vcpu_create()/kvm_arch_vcpu_destroy() instead?
user_fpucould be made percpu too... That would save a bit of memory for each vCPU. I'm holding on Xiaoyao's patch because a lot of the code he's touching would go away then.
Sorry, I don't get clear your attitude.
Do you mean the generic common function is not so better that I'd better to implement the percpu solution?
Quoted 2 lines
Paolo
Paolo Bonzini
maintainerQuoted 8 lines · Xiaoyao Li
Quoted 4 lines
user_fpucould be made percpu too... That would save a bit of memory for each vCPU. I'm holding on Xiaoyao's patch because a lot of the code he's touching would go away then.Sorry, I don't get clear your attitude.
Do you mean the generic common function is not so better that I'd better to implement the percpu solution?
I wanted some time to give further thought to the percpu user_fpu idea.
But kvm_load_guest_fpu and kvm_put_guest_fpu are not part of vcpu_load,
so it would not be so easy. I'll just apply your patch now.
Paolo
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Xiaoyao Li
Quoted 4 lines
user_fpucould be made percpu too... That would save a bit of memory for each vCPU. I'm holding on Xiaoyao's patch because a lot of the code he's touching would go away then.
Quoted 7 lines
Quoted 3 lines
Sorry, I don't get clear your attitude.
Do you mean the generic common function is not so better that I'd better to implement the percpu solution?I wanted some time to give further thought to the percpu
user_fpuidea.
Butkvm_load_guest_fpuandkvm_put_guest_fpuare not part ofvcpu_load,
so it would not be so easy. I'll just apply your patch now.
Got it, thanks.
BTW, could you have a look at the series I sent yesterday to refactor the vcpu creation flow, which is inspired partly by this issue. Any comment and suggestion is welcomed since I don't want to waste time on wrong direction.
Paolo Bonzini
maintainerQuoted 4 lines · Xiaoyao Li
BTW, could you have a look at the series I sent yesterday to refactor the vcpu creation flow, which is inspired partly by this issue. Any comment and suggestion is welcomed since I don't want to waste time on wrong direction.
Yes, that's the series from which I'll take your patch.
Paolo
Quoted 7 lines · Paolo Bonzini
Quoted 4 lines · Xiaoyao Li
BTW, could you have a look at the series I sent yesterday to refactor the vcpu creation flow, which is inspired partly by this issue. Any comment and suggestion is welcomed since I don't want to waste time on wrong direction.
Yes, that's the series from which I'll take your patch.
Can you hold off on taking that patch? I'm pretty sure we can do more cleanup in that area, with less code.
Paolo Bonzini
maintainerQuoted 7 lines · Sean Christopherson
Quoted 6 lines · Paolo Bonzini
Quoted 4 lines · Xiaoyao Li
BTW, could you have a look at the series I sent yesterday to refactor the vcpu creation flow, which is inspired partly by this issue. Any comment and suggestion is welcomed since I don't want to waste time on wrong direction.
Quoted 5 lines
Quoted 1 line
Yes, that's the series from which I'll take your patch.
Can you hold off on taking that patch? I'm pretty sure we can do more cleanup in that area, with less code.
Should I hold off on the whole "Refactor vcpu creation flow of x86 arch" series then?
Paolo
Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · Sean Christopherson
Quoted 6 lines · Paolo Bonzini
Quoted 4 lines · Xiaoyao Li
BTW, could you have a look at the series I sent yesterday to refactor the vcpu creation flow, which is inspired partly by this issue. Any comment and suggestion is welcomed since I don't want to waste time on wrong direction.
Quoted 9 lines
Quoted 5 lines
Quoted 1 line
Yes, that's the series from which I'll take your patch.
Can you hold off on taking that patch? I'm pretty sure we can do more cleanup in that area, with less code.
Should I hold off on the whole "Refactor vcpu creation flow of x86 arch" series then?
Yes, please just leave them aside.
If could, you can have an eye on my "v3 Minor cleanup and refactor about vmcs"
Thanks,
-Xiaoyao