Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S942054AbcJSR0X (ORCPT ); Wed, 19 Oct 2016 13:26:23 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37120 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S941332AbcJSR0V (ORCPT ); Wed, 19 Oct 2016 13:26:21 -0400 Date: Wed, 19 Oct 2016 11:26:20 -0600 From: Alex Williamson To: Kirti Wankhede Cc: , , , , , , , , Subject: Re: [PATCH v9 03/12] vfio: Rearrange functions to get vfio_group from dev Message-ID: <20161019112620.74b2b936@t450s.home> In-Reply-To: <1476739332-4911-4-git-send-email-kwankhede@nvidia.com> References: <1476739332-4911-1-git-send-email-kwankhede@nvidia.com> <1476739332-4911-4-git-send-email-kwankhede@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 19 Oct 2016 17:26:21 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3354 Lines: 115 On Tue, 18 Oct 2016 02:52:03 +0530 Kirti Wankhede wrote: > Rearrange functions to have common function to increment container_users. > > Signed-off-by: Kirti Wankhede > Signed-off-by: Neo Jia > Change-Id: I1f93262bdbab75094bc24b087b29da35ba70c4c6 > --- > drivers/vfio/vfio.c | 57 ++++++++++++++++++++++++++++++++++------------------- > 1 file changed, 37 insertions(+), 20 deletions(-) Ideally these would be two separate patches, one pulling vfio_group_get_from_dev() out of vfio_device_get_from_dev() and replacing the existing use, the other doing the same for vfio_group_add_container_user(). Otherwise it looks good. Thanks, Alex > diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c > index d1d70e0b011b..2e83bdf007fe 100644 > --- a/drivers/vfio/vfio.c > +++ b/drivers/vfio/vfio.c > @@ -480,6 +480,21 @@ static struct vfio_group *vfio_group_get_from_minor(int minor) > return group; > } > > +static struct vfio_group *vfio_group_get_from_dev(struct device *dev) > +{ > + struct iommu_group *iommu_group; > + struct vfio_group *group; > + > + iommu_group = iommu_group_get(dev); > + if (!iommu_group) > + return NULL; > + > + group = vfio_group_get_from_iommu(iommu_group); > + iommu_group_put(iommu_group); > + > + return group; > +} > + > /** > * Device objects - create, release, get, put, search > */ > @@ -811,16 +826,10 @@ EXPORT_SYMBOL_GPL(vfio_add_group_dev); > */ > struct vfio_device *vfio_device_get_from_dev(struct device *dev) > { > - struct iommu_group *iommu_group; > struct vfio_group *group; > struct vfio_device *device; > > - iommu_group = iommu_group_get(dev); > - if (!iommu_group) > - return NULL; > - > - group = vfio_group_get_from_iommu(iommu_group); > - iommu_group_put(iommu_group); > + group = vfio_group_get_from_dev(dev); > if (!group) > return NULL; > > @@ -1376,6 +1385,23 @@ static bool vfio_group_viable(struct vfio_group *group) > group, vfio_dev_viable) == 0); > } > > +static int vfio_group_add_container_user(struct vfio_group *group) > +{ > + if (!atomic_inc_not_zero(&group->container_users)) > + return -EINVAL; > + > + if (group->noiommu) { > + atomic_dec(&group->container_users); > + return -EPERM; > + } > + if (!group->container->iommu_driver || !vfio_group_viable(group)) { > + atomic_dec(&group->container_users); > + return -EINVAL; > + } > + > + return 0; > +} > + > static const struct file_operations vfio_device_fops; > > static int vfio_group_get_device_fd(struct vfio_group *group, char *buf) > @@ -1685,23 +1711,14 @@ static const struct file_operations vfio_device_fops = { > struct vfio_group *vfio_group_get_external_user(struct file *filep) > { > struct vfio_group *group = filep->private_data; > + int ret; > > if (filep->f_op != &vfio_group_fops) > return ERR_PTR(-EINVAL); > > - if (!atomic_inc_not_zero(&group->container_users)) > - return ERR_PTR(-EINVAL); > - > - if (group->noiommu) { > - atomic_dec(&group->container_users); > - return ERR_PTR(-EPERM); > - } > - > - if (!group->container->iommu_driver || > - !vfio_group_viable(group)) { > - atomic_dec(&group->container_users); > - return ERR_PTR(-EINVAL); > - } > + ret = vfio_group_add_container_user(group); > + if (ret) > + return ERR_PTR(ret); > > vfio_group_get(group); >