Commit d1ac91d8 authored by Avi Kivity's avatar Avi Kivity

KVM: Consolidate load/save temporary buffer allocation and freeing

Instead of three temporary variables and three free calls, have one temporary
variable (with four names) and one free call.
Signed-off-by: default avatarAvi Kivity <avi@redhat.com>
parent a1a005f3
...@@ -2436,25 +2436,29 @@ long kvm_arch_vcpu_ioctl(struct file *filp, ...@@ -2436,25 +2436,29 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
struct kvm_vcpu *vcpu = filp->private_data; struct kvm_vcpu *vcpu = filp->private_data;
void __user *argp = (void __user *)arg; void __user *argp = (void __user *)arg;
int r; int r;
struct kvm_lapic_state *lapic = NULL; union {
struct kvm_xsave *xsave = NULL; struct kvm_lapic_state *lapic;
struct kvm_xcrs *xcrs = NULL; struct kvm_xsave *xsave;
struct kvm_xcrs *xcrs;
void *buffer;
} u;
u.buffer = NULL;
switch (ioctl) { switch (ioctl) {
case KVM_GET_LAPIC: { case KVM_GET_LAPIC: {
r = -EINVAL; r = -EINVAL;
if (!vcpu->arch.apic) if (!vcpu->arch.apic)
goto out; goto out;
lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); u.lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
r = -ENOMEM; r = -ENOMEM;
if (!lapic) if (!u.lapic)
goto out; goto out;
r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic); r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
if (r) if (r)
goto out; goto out;
r = -EFAULT; r = -EFAULT;
if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state))) if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
goto out; goto out;
r = 0; r = 0;
break; break;
...@@ -2463,14 +2467,14 @@ long kvm_arch_vcpu_ioctl(struct file *filp, ...@@ -2463,14 +2467,14 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
r = -EINVAL; r = -EINVAL;
if (!vcpu->arch.apic) if (!vcpu->arch.apic)
goto out; goto out;
lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL); u.lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
r = -ENOMEM; r = -ENOMEM;
if (!lapic) if (!u.lapic)
goto out; goto out;
r = -EFAULT; r = -EFAULT;
if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state))) if (copy_from_user(u.lapic, argp, sizeof(struct kvm_lapic_state)))
goto out; goto out;
r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic); r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
if (r) if (r)
goto out; goto out;
r = 0; r = 0;
...@@ -2634,68 +2638,66 @@ long kvm_arch_vcpu_ioctl(struct file *filp, ...@@ -2634,68 +2638,66 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
break; break;
} }
case KVM_GET_XSAVE: { case KVM_GET_XSAVE: {
xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL); u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
r = -ENOMEM; r = -ENOMEM;
if (!xsave) if (!u.xsave)
break; break;
kvm_vcpu_ioctl_x86_get_xsave(vcpu, xsave); kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
r = -EFAULT; r = -EFAULT;
if (copy_to_user(argp, xsave, sizeof(struct kvm_xsave))) if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
break; break;
r = 0; r = 0;
break; break;
} }
case KVM_SET_XSAVE: { case KVM_SET_XSAVE: {
xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL); u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL);
r = -ENOMEM; r = -ENOMEM;
if (!xsave) if (!u.xsave)
break; break;
r = -EFAULT; r = -EFAULT;
if (copy_from_user(xsave, argp, sizeof(struct kvm_xsave))) if (copy_from_user(u.xsave, argp, sizeof(struct kvm_xsave)))
break; break;
r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, xsave); r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
break; break;
} }
case KVM_GET_XCRS: { case KVM_GET_XCRS: {
xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL); u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
r = -ENOMEM; r = -ENOMEM;
if (!xcrs) if (!u.xcrs)
break; break;
kvm_vcpu_ioctl_x86_get_xcrs(vcpu, xcrs); kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
r = -EFAULT; r = -EFAULT;
if (copy_to_user(argp, xcrs, if (copy_to_user(argp, u.xcrs,
sizeof(struct kvm_xcrs))) sizeof(struct kvm_xcrs)))
break; break;
r = 0; r = 0;
break; break;
} }
case KVM_SET_XCRS: { case KVM_SET_XCRS: {
xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL); u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL);
r = -ENOMEM; r = -ENOMEM;
if (!xcrs) if (!u.xcrs)
break; break;
r = -EFAULT; r = -EFAULT;
if (copy_from_user(xcrs, argp, if (copy_from_user(u.xcrs, argp,
sizeof(struct kvm_xcrs))) sizeof(struct kvm_xcrs)))
break; break;
r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, xcrs); r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
break; break;
} }
default: default:
r = -EINVAL; r = -EINVAL;
} }
out: out:
kfree(lapic); kfree(u.buffer);
kfree(xsave);
kfree(xcrs);
return r; return r;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment