On 19/10/2016 07:45, Xiao Guangrong wrote: > > > On 10/19/2016 10:32 AM, Jike Song wrote: > +EXPORT_SYMBOL_GPL(vfio_group_set_usrdata); >>>> + >>>> +void *vfio_group_get_usrdata(struct vfio_group *group) >>>> +{ >>>> + return group->usrdata; >>>> +} >>>> +EXPORT_SYMBOL_GPL(vfio_group_get_usrdata); >>>> + >>>> +void *vfio_group_get_usrdata_by_device(struct device *dev) >>>> +{ >>>> + struct vfio_group *vfio_group; >>>> + >>>> + vfio_group = __vfio_group_get_from_iommu(dev->iommu_group); >>> >>> We actually need to use iommu_group_get() here. Kirti adds a >>> vfio_group_get_from_dev() in v9 03/12 that does this properly. >>> >>>> + if (!vfio_group) >>>> + return NULL; >>>> + >>>> + return vfio_group_get_usrdata(vfio_group); > > I am worrying if the kvm instance got from group->usrdata is safe > enough? What happens if you get the instance after kvm released > kvm-vfio device?
It shouldn't happen if you use kvm_get_kvm and kvm_put_kvm properly. It is almost okay in the patch, just: > @@ -200,6 +216,8 @@ static int kvm_vfio_set_group(struct kvm_device *dev, > long attr, u64 arg) > > kvm_vfio_update_coherency(dev); > > + kvm_put_kvm(dev->kvm); > + > return ret; > } ... please add a new function kvm_vfio_group_clear_kvm(vfio_group) here, that does vfio_group_set_usrdata(vfio_group, NULL) and kvm_put_kvm. This should avoid use-after-free. Paolo