Commit 9b062471 authored by Christoffer Dall's avatar Christoffer Dall Committed by Paolo Bonzini

KVM: Move vcpu_load to arch-specific kvm_arch_vcpu_ioctl

Move the calls to vcpu_load() and vcpu_put() in to the architecture
specific implementations of kvm_arch_vcpu_ioctl() which dispatches
further architecture-specific ioctls on to other functions.

Some architectures support asynchronous vcpu ioctls which cannot call
vcpu_load() or take the vcpu->mutex, because that would prevent
concurrent execution with a running VCPU, which is the intended purpose
of these ioctls, for example because they inject interrupts.

We repeat the separate checks for these specifics in the architecture
code for MIPS, S390 and PPC, and avoid taking the vcpu->mutex and
calling vcpu_load for these ioctls.
Signed-off-by: default avatarChristoffer Dall <christoffer.dall@linaro.org>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 6a96bc7f
...@@ -910,56 +910,65 @@ long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, ...@@ -910,56 +910,65 @@ long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl,
void __user *argp = (void __user *)arg; void __user *argp = (void __user *)arg;
long r; long r;
if (ioctl == KVM_INTERRUPT) {
struct kvm_mips_interrupt irq;
if (copy_from_user(&irq, argp, sizeof(irq)))
return -EFAULT;
kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
irq.irq);
return kvm_vcpu_ioctl_interrupt(vcpu, &irq);
}
vcpu_load(vcpu);
switch (ioctl) { switch (ioctl) {
case KVM_SET_ONE_REG: case KVM_SET_ONE_REG:
case KVM_GET_ONE_REG: { case KVM_GET_ONE_REG: {
struct kvm_one_reg reg; struct kvm_one_reg reg;
r = -EFAULT;
if (copy_from_user(&reg, argp, sizeof(reg))) if (copy_from_user(&reg, argp, sizeof(reg)))
return -EFAULT; break;
if (ioctl == KVM_SET_ONE_REG) if (ioctl == KVM_SET_ONE_REG)
return kvm_mips_set_reg(vcpu, &reg); r = kvm_mips_set_reg(vcpu, &reg);
else else
return kvm_mips_get_reg(vcpu, &reg); r = kvm_mips_get_reg(vcpu, &reg);
break;
} }
case KVM_GET_REG_LIST: { case KVM_GET_REG_LIST: {
struct kvm_reg_list __user *user_list = argp; struct kvm_reg_list __user *user_list = argp;
struct kvm_reg_list reg_list; struct kvm_reg_list reg_list;
unsigned n; unsigned n;
r = -EFAULT;
if (copy_from_user(&reg_list, user_list, sizeof(reg_list))) if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
return -EFAULT; break;
n = reg_list.n; n = reg_list.n;
reg_list.n = kvm_mips_num_regs(vcpu); reg_list.n = kvm_mips_num_regs(vcpu);
if (copy_to_user(user_list, &reg_list, sizeof(reg_list))) if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
return -EFAULT; break;
r = -E2BIG;
if (n < reg_list.n) if (n < reg_list.n)
return -E2BIG; break;
return kvm_mips_copy_reg_indices(vcpu, user_list->reg); r = kvm_mips_copy_reg_indices(vcpu, user_list->reg);
}
case KVM_INTERRUPT:
{
struct kvm_mips_interrupt irq;
if (copy_from_user(&irq, argp, sizeof(irq)))
return -EFAULT;
kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
irq.irq);
r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
break; break;
} }
case KVM_ENABLE_CAP: { case KVM_ENABLE_CAP: {
struct kvm_enable_cap cap; struct kvm_enable_cap cap;
r = -EFAULT;
if (copy_from_user(&cap, argp, sizeof(cap))) if (copy_from_user(&cap, argp, sizeof(cap)))
return -EFAULT; break;
r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
break; break;
} }
default: default:
r = -ENOIOCTLCMD; r = -ENOIOCTLCMD;
} }
vcpu_put(vcpu);
return r; return r;
} }
......
...@@ -1614,16 +1614,16 @@ long kvm_arch_vcpu_ioctl(struct file *filp, ...@@ -1614,16 +1614,16 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
void __user *argp = (void __user *)arg; void __user *argp = (void __user *)arg;
long r; long r;
switch (ioctl) { if (ioctl == KVM_INTERRUPT) {
case KVM_INTERRUPT: {
struct kvm_interrupt irq; struct kvm_interrupt irq;
r = -EFAULT;
if (copy_from_user(&irq, argp, sizeof(irq))) if (copy_from_user(&irq, argp, sizeof(irq)))
goto out; return -EFAULT;
r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); return kvm_vcpu_ioctl_interrupt(vcpu, &irq);
goto out;
} }
vcpu_load(vcpu);
switch (ioctl) {
case KVM_ENABLE_CAP: case KVM_ENABLE_CAP:
{ {
struct kvm_enable_cap cap; struct kvm_enable_cap cap;
...@@ -1663,6 +1663,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp, ...@@ -1663,6 +1663,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
} }
out: out:
vcpu_put(vcpu);
return r; return r;
} }
......
...@@ -3737,24 +3737,25 @@ long kvm_arch_vcpu_ioctl(struct file *filp, ...@@ -3737,24 +3737,25 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
case KVM_S390_IRQ: { case KVM_S390_IRQ: {
struct kvm_s390_irq s390irq; struct kvm_s390_irq s390irq;
r = -EFAULT;
if (copy_from_user(&s390irq, argp, sizeof(s390irq))) if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
break; return -EFAULT;
r = kvm_s390_inject_vcpu(vcpu, &s390irq); return kvm_s390_inject_vcpu(vcpu, &s390irq);
break;
} }
case KVM_S390_INTERRUPT: { case KVM_S390_INTERRUPT: {
struct kvm_s390_interrupt s390int; struct kvm_s390_interrupt s390int;
struct kvm_s390_irq s390irq; struct kvm_s390_irq s390irq;
r = -EFAULT;
if (copy_from_user(&s390int, argp, sizeof(s390int))) if (copy_from_user(&s390int, argp, sizeof(s390int)))
break; return -EFAULT;
if (s390int_to_s390irq(&s390int, &s390irq)) if (s390int_to_s390irq(&s390int, &s390irq))
return -EINVAL; return -EINVAL;
r = kvm_s390_inject_vcpu(vcpu, &s390irq); return kvm_s390_inject_vcpu(vcpu, &s390irq);
break;
} }
}
vcpu_load(vcpu);
switch (ioctl) {
case KVM_S390_STORE_STATUS: case KVM_S390_STORE_STATUS:
idx = srcu_read_lock(&vcpu->kvm->srcu); idx = srcu_read_lock(&vcpu->kvm->srcu);
r = kvm_s390_vcpu_store_status(vcpu, arg); r = kvm_s390_vcpu_store_status(vcpu, arg);
...@@ -3879,6 +3880,8 @@ long kvm_arch_vcpu_ioctl(struct file *filp, ...@@ -3879,6 +3880,8 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
default: default:
r = -ENOTTY; r = -ENOTTY;
} }
vcpu_put(vcpu);
return r; return r;
} }
......
...@@ -3484,6 +3484,8 @@ long kvm_arch_vcpu_ioctl(struct file *filp, ...@@ -3484,6 +3484,8 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
void *buffer; void *buffer;
} u; } u;
vcpu_load(vcpu);
u.buffer = NULL; u.buffer = NULL;
switch (ioctl) { switch (ioctl) {
case KVM_GET_LAPIC: { case KVM_GET_LAPIC: {
...@@ -3509,8 +3511,10 @@ long kvm_arch_vcpu_ioctl(struct file *filp, ...@@ -3509,8 +3511,10 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
if (!lapic_in_kernel(vcpu)) if (!lapic_in_kernel(vcpu))
goto out; goto out;
u.lapic = memdup_user(argp, sizeof(*u.lapic)); u.lapic = memdup_user(argp, sizeof(*u.lapic));
if (IS_ERR(u.lapic)) if (IS_ERR(u.lapic)) {
return PTR_ERR(u.lapic); r = PTR_ERR(u.lapic);
goto out_nofree;
}
r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic); r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
break; break;
...@@ -3684,8 +3688,10 @@ long kvm_arch_vcpu_ioctl(struct file *filp, ...@@ -3684,8 +3688,10 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
} }
case KVM_SET_XSAVE: { case KVM_SET_XSAVE: {
u.xsave = memdup_user(argp, sizeof(*u.xsave)); u.xsave = memdup_user(argp, sizeof(*u.xsave));
if (IS_ERR(u.xsave)) if (IS_ERR(u.xsave)) {
return PTR_ERR(u.xsave); r = PTR_ERR(u.xsave);
goto out_nofree;
}
r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave); r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
break; break;
...@@ -3707,8 +3713,10 @@ long kvm_arch_vcpu_ioctl(struct file *filp, ...@@ -3707,8 +3713,10 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
} }
case KVM_SET_XCRS: { case KVM_SET_XCRS: {
u.xcrs = memdup_user(argp, sizeof(*u.xcrs)); u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
if (IS_ERR(u.xcrs)) if (IS_ERR(u.xcrs)) {
return PTR_ERR(u.xcrs); r = PTR_ERR(u.xcrs);
goto out_nofree;
}
r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs); r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
break; break;
...@@ -3752,6 +3760,8 @@ long kvm_arch_vcpu_ioctl(struct file *filp, ...@@ -3752,6 +3760,8 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
} }
out: out:
kfree(u.buffer); kfree(u.buffer);
out_nofree:
vcpu_put(vcpu);
return r; return r;
} }
......
...@@ -1003,66 +1003,88 @@ long kvm_arch_vcpu_ioctl(struct file *filp, ...@@ -1003,66 +1003,88 @@ 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;
struct kvm_device_attr attr; struct kvm_device_attr attr;
long r;
vcpu_load(vcpu);
switch (ioctl) { switch (ioctl) {
case KVM_ARM_VCPU_INIT: { case KVM_ARM_VCPU_INIT: {
struct kvm_vcpu_init init; struct kvm_vcpu_init init;
r = -EFAULT;
if (copy_from_user(&init, argp, sizeof(init))) if (copy_from_user(&init, argp, sizeof(init)))
return -EFAULT; break;
return kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init); r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init);
break;
} }
case KVM_SET_ONE_REG: case KVM_SET_ONE_REG:
case KVM_GET_ONE_REG: { case KVM_GET_ONE_REG: {
struct kvm_one_reg reg; struct kvm_one_reg reg;
r = -ENOEXEC;
if (unlikely(!kvm_vcpu_initialized(vcpu))) if (unlikely(!kvm_vcpu_initialized(vcpu)))
return -ENOEXEC; break;
r = -EFAULT;
if (copy_from_user(&reg, argp, sizeof(reg))) if (copy_from_user(&reg, argp, sizeof(reg)))
return -EFAULT; break;
if (ioctl == KVM_SET_ONE_REG) if (ioctl == KVM_SET_ONE_REG)
return kvm_arm_set_reg(vcpu, &reg); r = kvm_arm_set_reg(vcpu, &reg);
else else
return kvm_arm_get_reg(vcpu, &reg); r = kvm_arm_get_reg(vcpu, &reg);
break;
} }
case KVM_GET_REG_LIST: { case KVM_GET_REG_LIST: {
struct kvm_reg_list __user *user_list = argp; struct kvm_reg_list __user *user_list = argp;
struct kvm_reg_list reg_list; struct kvm_reg_list reg_list;
unsigned n; unsigned n;
r = -ENOEXEC;
if (unlikely(!kvm_vcpu_initialized(vcpu))) if (unlikely(!kvm_vcpu_initialized(vcpu)))
return -ENOEXEC; break;
r = -EFAULT;
if (copy_from_user(&reg_list, user_list, sizeof(reg_list))) if (copy_from_user(&reg_list, user_list, sizeof(reg_list)))
return -EFAULT; break;
n = reg_list.n; n = reg_list.n;
reg_list.n = kvm_arm_num_regs(vcpu); reg_list.n = kvm_arm_num_regs(vcpu);
if (copy_to_user(user_list, &reg_list, sizeof(reg_list))) if (copy_to_user(user_list, &reg_list, sizeof(reg_list)))
return -EFAULT; break;
r = -E2BIG;
if (n < reg_list.n) if (n < reg_list.n)
return -E2BIG; break;
return kvm_arm_copy_reg_indices(vcpu, user_list->reg); r = kvm_arm_copy_reg_indices(vcpu, user_list->reg);
break;
} }
case KVM_SET_DEVICE_ATTR: { case KVM_SET_DEVICE_ATTR: {
r = -EFAULT;
if (copy_from_user(&attr, argp, sizeof(attr))) if (copy_from_user(&attr, argp, sizeof(attr)))
return -EFAULT; break;
return kvm_arm_vcpu_set_attr(vcpu, &attr); r = kvm_arm_vcpu_set_attr(vcpu, &attr);
break;
} }
case KVM_GET_DEVICE_ATTR: { case KVM_GET_DEVICE_ATTR: {
r = -EFAULT;
if (copy_from_user(&attr, argp, sizeof(attr))) if (copy_from_user(&attr, argp, sizeof(attr)))
return -EFAULT; break;
return kvm_arm_vcpu_get_attr(vcpu, &attr); r = kvm_arm_vcpu_get_attr(vcpu, &attr);
break;
} }
case KVM_HAS_DEVICE_ATTR: { case KVM_HAS_DEVICE_ATTR: {
r = -EFAULT;
if (copy_from_user(&attr, argp, sizeof(attr))) if (copy_from_user(&attr, argp, sizeof(attr)))
return -EFAULT; break;
return kvm_arm_vcpu_has_attr(vcpu, &attr); r = kvm_arm_vcpu_has_attr(vcpu, &attr);
break;
} }
default: default:
return -EINVAL; r = -EINVAL;
} }
vcpu_put(vcpu);
return r;
} }
/** /**
......
...@@ -2724,9 +2724,7 @@ static long kvm_vcpu_ioctl(struct file *filp, ...@@ -2724,9 +2724,7 @@ static long kvm_vcpu_ioctl(struct file *filp,
break; break;
} }
default: default:
vcpu_load(vcpu);
r = kvm_arch_vcpu_ioctl(filp, ioctl, arg); r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
vcpu_put(vcpu);
} }
out: out:
mutex_unlock(&vcpu->mutex); mutex_unlock(&vcpu->mutex);
......
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