* [PATCH v2 0/2] Rework vgic_attr_regs_access @ 2016-08-16 17:34 Christoffer Dall 2016-08-16 17:35 ` [PATCH v2 1/2] KVM: arm/arm64: Factor out vgic_attr_regs_access functionality Christoffer Dall 2016-08-16 17:35 ` [PATCH v2 2/2] KVM: arm/arm64: Rename vgic_attr_regs_access to vgic_attr_regs_access_v2 Christoffer Dall 0 siblings, 2 replies; 5+ messages in thread From: Christoffer Dall @ 2016-08-16 17:34 UTC (permalink / raw) To: linux-arm-kernel Two small patches to split up the functionality in vgic_attr_regs_access to make life simpler when having to deal with GICv3 save/restore. Changes from v1: - Comments and white space Christoffer Dall (2): KVM: arm/arm64: Factor out vgic_attr_regs_access functionality KVM: arm/arm64: Rename vgic_attr_regs_access to vgic_attr_regs_access_v2 virt/kvm/arm/vgic/vgic-kvm-device.c | 122 ++++++++++++++++++++++++------------ 1 file changed, 82 insertions(+), 40 deletions(-) -- 2.9.0 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 1/2] KVM: arm/arm64: Factor out vgic_attr_regs_access functionality 2016-08-16 17:34 [PATCH v2 0/2] Rework vgic_attr_regs_access Christoffer Dall @ 2016-08-16 17:35 ` Christoffer Dall 2016-08-17 5:46 ` Auger Eric 2016-08-16 17:35 ` [PATCH v2 2/2] KVM: arm/arm64: Rename vgic_attr_regs_access to vgic_attr_regs_access_v2 Christoffer Dall 1 sibling, 1 reply; 5+ messages in thread From: Christoffer Dall @ 2016-08-16 17:35 UTC (permalink / raw) To: linux-arm-kernel As we are about to deal with multiple data types and situations where the vgic should not be initialized when doing userspace accesses on the register attributes, factor out the functionality of vgic_attr_regs_access into smaller bits which can be reused by a new function later. Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org> --- virt/kvm/arm/vgic/vgic-kvm-device.c | 100 ++++++++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 27 deletions(-) diff --git a/virt/kvm/arm/vgic/vgic-kvm-device.c b/virt/kvm/arm/vgic/vgic-kvm-device.c index 1813f93..19fa331 100644 --- a/virt/kvm/arm/vgic/vgic-kvm-device.c +++ b/virt/kvm/arm/vgic/vgic-kvm-device.c @@ -233,6 +233,67 @@ int kvm_register_vgic_device(unsigned long type) return ret; } +struct vgic_reg_attr { + struct kvm_vcpu *vcpu; + gpa_t addr; +}; + +static int parse_vgic_v2_attr(struct kvm_device *dev, + struct kvm_device_attr *attr, + struct vgic_reg_attr *reg_attr) +{ + int cpuid; + + cpuid = (attr->attr & KVM_DEV_ARM_VGIC_CPUID_MASK) >> + KVM_DEV_ARM_VGIC_CPUID_SHIFT; + + if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) + return -EINVAL; + + reg_attr->vcpu = kvm_get_vcpu(dev->kvm, cpuid); + reg_attr->addr = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK; + + return 0; +} + +/* unlocks vcpus from @vcpu_lock_idx and smaller */ +static void unlock_vcpus(struct kvm *kvm, int vcpu_lock_idx) +{ + struct kvm_vcpu *tmp_vcpu; + + for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) { + tmp_vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx); + mutex_unlock(&tmp_vcpu->mutex); + } +} + +static void unlock_all_vcpus(struct kvm *kvm) +{ + unlock_vcpus(kvm, atomic_read(&kvm->online_vcpus) - 1); +} + +/* Returns true if all vcpus were locked, false otherwise */ +static bool lock_all_vcpus(struct kvm *kvm) +{ + struct kvm_vcpu *tmp_vcpu; + int c; + + /* + * Any time a vcpu is run, vcpu_load is called which tries to grab the + * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure + * that no other VCPUs are run and fiddle with the vgic state while we + * access it. + */ + kvm_for_each_vcpu(c, tmp_vcpu, kvm) { + if (!mutex_trylock(&tmp_vcpu->mutex)) { + unlock_vcpus(kvm, c - 1); + return false; + } + } + + return true; +} + /** vgic_attr_regs_access: allows user space to read/write VGIC registers * * @dev: kvm device handle @@ -245,15 +306,17 @@ static int vgic_attr_regs_access(struct kvm_device *dev, struct kvm_device_attr *attr, u32 *reg, bool is_write) { + struct vgic_reg_attr reg_attr; gpa_t addr; - int cpuid, ret, c; - struct kvm_vcpu *vcpu, *tmp_vcpu; - int vcpu_lock_idx = -1; + struct kvm_vcpu *vcpu; + int ret; - cpuid = (attr->attr & KVM_DEV_ARM_VGIC_CPUID_MASK) >> - KVM_DEV_ARM_VGIC_CPUID_SHIFT; - vcpu = kvm_get_vcpu(dev->kvm, cpuid); - addr = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK; + ret = parse_vgic_v2_attr(dev, attr, ®_attr); + if (ret) + return ret; + + vcpu = reg_attr.vcpu; + addr = reg_attr.addr; mutex_lock(&dev->kvm->lock); @@ -261,24 +324,11 @@ static int vgic_attr_regs_access(struct kvm_device *dev, if (ret) goto out; - if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) { - ret = -EINVAL; + if (!lock_all_vcpus(dev->kvm)) { + ret = -EBUSY; goto out; } - /* - * Any time a vcpu is run, vcpu_load is called which tries to grab the - * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure - * that no other VCPUs are run and fiddle with the vgic state while we - * access it. - */ - ret = -EBUSY; - kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm) { - if (!mutex_trylock(&tmp_vcpu->mutex)) - goto out; - vcpu_lock_idx = c; - } - switch (attr->group) { case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: ret = vgic_v2_cpuif_uaccess(vcpu, is_write, addr, reg); @@ -291,12 +341,8 @@ static int vgic_attr_regs_access(struct kvm_device *dev, break; } + unlock_all_vcpus(dev->kvm); out: - for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) { - tmp_vcpu = kvm_get_vcpu(dev->kvm, vcpu_lock_idx); - mutex_unlock(&tmp_vcpu->mutex); - } - mutex_unlock(&dev->kvm->lock); return ret; } -- 2.9.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 1/2] KVM: arm/arm64: Factor out vgic_attr_regs_access functionality 2016-08-16 17:35 ` [PATCH v2 1/2] KVM: arm/arm64: Factor out vgic_attr_regs_access functionality Christoffer Dall @ 2016-08-17 5:46 ` Auger Eric 0 siblings, 0 replies; 5+ messages in thread From: Auger Eric @ 2016-08-17 5:46 UTC (permalink / raw) To: linux-arm-kernel Hi Christoffer, On 16/08/2016 19:35, Christoffer Dall wrote: > As we are about to deal with multiple data types and situations where > the vgic should not be initialized when doing userspace accesses on the > register attributes, factor out the functionality of > vgic_attr_regs_access into smaller bits which can be reused by a new > function later. > > Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org> > --- > virt/kvm/arm/vgic/vgic-kvm-device.c | 100 ++++++++++++++++++++++++++---------- > 1 file changed, 73 insertions(+), 27 deletions(-) > > diff --git a/virt/kvm/arm/vgic/vgic-kvm-device.c b/virt/kvm/arm/vgic/vgic-kvm-device.c > index 1813f93..19fa331 100644 > --- a/virt/kvm/arm/vgic/vgic-kvm-device.c > +++ b/virt/kvm/arm/vgic/vgic-kvm-device.c > @@ -233,6 +233,67 @@ int kvm_register_vgic_device(unsigned long type) > return ret; > } > > +struct vgic_reg_attr { > + struct kvm_vcpu *vcpu; > + gpa_t addr; > +}; > + > +static int parse_vgic_v2_attr(struct kvm_device *dev, > + struct kvm_device_attr *attr, > + struct vgic_reg_attr *reg_attr) > +{ > + int cpuid; > + > + cpuid = (attr->attr & KVM_DEV_ARM_VGIC_CPUID_MASK) >> > + KVM_DEV_ARM_VGIC_CPUID_SHIFT; > + > + if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) > + return -EINVAL; > + > + reg_attr->vcpu = kvm_get_vcpu(dev->kvm, cpuid); > + reg_attr->addr = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK; > + > + return 0; > +} > + > +/* unlocks vcpus from @vcpu_lock_idx and smaller */ > +static void unlock_vcpus(struct kvm *kvm, int vcpu_lock_idx) > +{ > + struct kvm_vcpu *tmp_vcpu; > + > + for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) { > + tmp_vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx); > + mutex_unlock(&tmp_vcpu->mutex); > + } > +} > + > +static void unlock_all_vcpus(struct kvm *kvm) > +{ > + unlock_vcpus(kvm, atomic_read(&kvm->online_vcpus) - 1); > +} > + > +/* Returns true if all vcpus were locked, false otherwise */ > +static bool lock_all_vcpus(struct kvm *kvm) > +{ > + struct kvm_vcpu *tmp_vcpu; > + int c; > + > + /* > + * Any time a vcpu is run, vcpu_load is called which tries to grab the > + * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure > + * that no other VCPUs are run and fiddle with the vgic state while we > + * access it. > + */ > + kvm_for_each_vcpu(c, tmp_vcpu, kvm) { > + if (!mutex_trylock(&tmp_vcpu->mutex)) { > + unlock_vcpus(kvm, c - 1); > + return false; > + } > + } > + > + return true; > +} > + > /** vgic_attr_regs_access: allows user space to read/write VGIC registers > * > * @dev: kvm device handle > @@ -245,15 +306,17 @@ static int vgic_attr_regs_access(struct kvm_device *dev, > struct kvm_device_attr *attr, > u32 *reg, bool is_write) > { > + struct vgic_reg_attr reg_attr; > gpa_t addr; > - int cpuid, ret, c; > - struct kvm_vcpu *vcpu, *tmp_vcpu; > - int vcpu_lock_idx = -1; > + struct kvm_vcpu *vcpu; > + int ret; > > - cpuid = (attr->attr & KVM_DEV_ARM_VGIC_CPUID_MASK) >> > - KVM_DEV_ARM_VGIC_CPUID_SHIFT; > - vcpu = kvm_get_vcpu(dev->kvm, cpuid); > - addr = attr->attr & KVM_DEV_ARM_VGIC_OFFSET_MASK; > + ret = parse_vgic_v2_attr(dev, attr, ®_attr); > + if (ret) > + return ret; > + > + vcpu = reg_attr.vcpu; > + addr = reg_attr.addr; > > mutex_lock(&dev->kvm->lock); > > @@ -261,24 +324,11 @@ static int vgic_attr_regs_access(struct kvm_device *dev, > if (ret) > goto out; > > - if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) { > - ret = -EINVAL; > + if (!lock_all_vcpus(dev->kvm)) { > + ret = -EBUSY; > goto out; > } > > - /* > - * Any time a vcpu is run, vcpu_load is called which tries to grab the > - * vcpu->mutex. By grabbing the vcpu->mutex of all VCPUs we ensure > - * that no other VCPUs are run and fiddle with the vgic state while we > - * access it. > - */ > - ret = -EBUSY; > - kvm_for_each_vcpu(c, tmp_vcpu, dev->kvm) { > - if (!mutex_trylock(&tmp_vcpu->mutex)) > - goto out; > - vcpu_lock_idx = c; > - } > - > switch (attr->group) { > case KVM_DEV_ARM_VGIC_GRP_CPU_REGS: > ret = vgic_v2_cpuif_uaccess(vcpu, is_write, addr, reg); > @@ -291,12 +341,8 @@ static int vgic_attr_regs_access(struct kvm_device *dev, > break; > } > > + unlock_all_vcpus(dev->kvm); > out: > - for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) { > - tmp_vcpu = kvm_get_vcpu(dev->kvm, vcpu_lock_idx); > - mutex_unlock(&tmp_vcpu->mutex); > - } > - > mutex_unlock(&dev->kvm->lock); > return ret; > } > Reviewed-by: Eric Auger <eric.auger@redhat.com> Eric ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] KVM: arm/arm64: Rename vgic_attr_regs_access to vgic_attr_regs_access_v2 2016-08-16 17:34 [PATCH v2 0/2] Rework vgic_attr_regs_access Christoffer Dall 2016-08-16 17:35 ` [PATCH v2 1/2] KVM: arm/arm64: Factor out vgic_attr_regs_access functionality Christoffer Dall @ 2016-08-16 17:35 ` Christoffer Dall 2016-08-17 5:46 ` Auger Eric 1 sibling, 1 reply; 5+ messages in thread From: Christoffer Dall @ 2016-08-16 17:35 UTC (permalink / raw) To: linux-arm-kernel Just a rename so we can implement a v3-specific function later. We take the chance to get rid of the V2/V3 ops comments as well. No functional change. Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org> --- virt/kvm/arm/vgic/vgic-kvm-device.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/virt/kvm/arm/vgic/vgic-kvm-device.c b/virt/kvm/arm/vgic/vgic-kvm-device.c index 19fa331..163b057 100644 --- a/virt/kvm/arm/vgic/vgic-kvm-device.c +++ b/virt/kvm/arm/vgic/vgic-kvm-device.c @@ -294,17 +294,17 @@ static bool lock_all_vcpus(struct kvm *kvm) return true; } -/** vgic_attr_regs_access: allows user space to read/write VGIC registers - * - * @dev: kvm device handle - * @attr: kvm device attribute - * @reg: address the value is read or written - * @is_write: write flag +/** + * vgic_attr_regs_access_v2 - allows user space to access VGIC v2 state * + * @dev: kvm device handle + * @attr: kvm device attribute + * @reg: address the value is read or written + * @is_write: true if userspace is writing a register */ -static int vgic_attr_regs_access(struct kvm_device *dev, - struct kvm_device_attr *attr, - u32 *reg, bool is_write) +static int vgic_attr_regs_access_v2(struct kvm_device *dev, + struct kvm_device_attr *attr, + u32 *reg, bool is_write) { struct vgic_reg_attr reg_attr; gpa_t addr; @@ -347,8 +347,6 @@ out: return ret; } -/* V2 ops */ - static int vgic_v2_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr) { @@ -367,7 +365,7 @@ static int vgic_v2_set_attr(struct kvm_device *dev, if (get_user(reg, uaddr)) return -EFAULT; - return vgic_attr_regs_access(dev, attr, ®, true); + return vgic_attr_regs_access_v2(dev, attr, ®, true); } } @@ -389,7 +387,7 @@ static int vgic_v2_get_attr(struct kvm_device *dev, u32 __user *uaddr = (u32 __user *)(long)attr->addr; u32 reg = 0; - ret = vgic_attr_regs_access(dev, attr, ®, false); + ret = vgic_attr_regs_access_v2(dev, attr, ®, false); if (ret) return ret; return put_user(reg, uaddr); @@ -433,8 +431,6 @@ struct kvm_device_ops kvm_arm_vgic_v2_ops = { .has_attr = vgic_v2_has_attr, }; -/* V3 ops */ - #ifdef CONFIG_KVM_ARM_VGIC_V3 static int vgic_v3_set_attr(struct kvm_device *dev, -- 2.9.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] KVM: arm/arm64: Rename vgic_attr_regs_access to vgic_attr_regs_access_v2 2016-08-16 17:35 ` [PATCH v2 2/2] KVM: arm/arm64: Rename vgic_attr_regs_access to vgic_attr_regs_access_v2 Christoffer Dall @ 2016-08-17 5:46 ` Auger Eric 0 siblings, 0 replies; 5+ messages in thread From: Auger Eric @ 2016-08-17 5:46 UTC (permalink / raw) To: linux-arm-kernel Hi, On 16/08/2016 19:35, Christoffer Dall wrote: > Just a rename so we can implement a v3-specific function later. > > We take the chance to get rid of the V2/V3 ops comments as well. > > No functional change. > > Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org> > --- > virt/kvm/arm/vgic/vgic-kvm-device.c | 26 +++++++++++--------------- > 1 file changed, 11 insertions(+), 15 deletions(-) > > diff --git a/virt/kvm/arm/vgic/vgic-kvm-device.c b/virt/kvm/arm/vgic/vgic-kvm-device.c > index 19fa331..163b057 100644 > --- a/virt/kvm/arm/vgic/vgic-kvm-device.c > +++ b/virt/kvm/arm/vgic/vgic-kvm-device.c > @@ -294,17 +294,17 @@ static bool lock_all_vcpus(struct kvm *kvm) > return true; > } > > -/** vgic_attr_regs_access: allows user space to read/write VGIC registers > - * > - * @dev: kvm device handle > - * @attr: kvm device attribute > - * @reg: address the value is read or written > - * @is_write: write flag > +/** > + * vgic_attr_regs_access_v2 - allows user space to access VGIC v2 state > * > + * @dev: kvm device handle > + * @attr: kvm device attribute > + * @reg: address the value is read or written > + * @is_write: true if userspace is writing a register > */ > -static int vgic_attr_regs_access(struct kvm_device *dev, > - struct kvm_device_attr *attr, > - u32 *reg, bool is_write) > +static int vgic_attr_regs_access_v2(struct kvm_device *dev, > + struct kvm_device_attr *attr, > + u32 *reg, bool is_write) > { > struct vgic_reg_attr reg_attr; > gpa_t addr; > @@ -347,8 +347,6 @@ out: > return ret; > } > > -/* V2 ops */ > - > static int vgic_v2_set_attr(struct kvm_device *dev, > struct kvm_device_attr *attr) > { > @@ -367,7 +365,7 @@ static int vgic_v2_set_attr(struct kvm_device *dev, > if (get_user(reg, uaddr)) > return -EFAULT; > > - return vgic_attr_regs_access(dev, attr, ®, true); > + return vgic_attr_regs_access_v2(dev, attr, ®, true); > } > } > > @@ -389,7 +387,7 @@ static int vgic_v2_get_attr(struct kvm_device *dev, > u32 __user *uaddr = (u32 __user *)(long)attr->addr; > u32 reg = 0; > > - ret = vgic_attr_regs_access(dev, attr, ®, false); > + ret = vgic_attr_regs_access_v2(dev, attr, ®, false); > if (ret) > return ret; > return put_user(reg, uaddr); > @@ -433,8 +431,6 @@ struct kvm_device_ops kvm_arm_vgic_v2_ops = { > .has_attr = vgic_v2_has_attr, > }; > > -/* V3 ops */ > - > #ifdef CONFIG_KVM_ARM_VGIC_V3 > > static int vgic_v3_set_attr(struct kvm_device *dev, > Reviewed-by: Eric Auger <eric.auger@redhat.com> Thanks Eric ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2016-08-17 5:46 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2016-08-16 17:34 [PATCH v2 0/2] Rework vgic_attr_regs_access Christoffer Dall 2016-08-16 17:35 ` [PATCH v2 1/2] KVM: arm/arm64: Factor out vgic_attr_regs_access functionality Christoffer Dall 2016-08-17 5:46 ` Auger Eric 2016-08-16 17:35 ` [PATCH v2 2/2] KVM: arm/arm64: Rename vgic_attr_regs_access to vgic_attr_regs_access_v2 Christoffer Dall 2016-08-17 5:46 ` Auger Eric
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).